OSDN Git Service

PR middle-end/21709
[pf3gnuchains/gcc-fork.git] / gcc / fold-const.c
1 /* Fold a constant sub-tree into a single node for C-compiler
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 /*@@ This file should be rewritten to use an arbitrary precision
23   @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
24   @@ Perhaps the routines could also be used for bc/dc, and made a lib.
25   @@ The routines that translate from the ap rep should
26   @@ warn if precision et. al. is lost.
27   @@ This would also make life easier when this technology is used
28   @@ for cross-compilers.  */
29
30 /* The entry points in this file are fold, size_int_wide, size_binop
31    and force_fit_type.
32
33    fold takes a tree as argument and returns a simplified tree.
34
35    size_binop takes a tree code for an arithmetic operation
36    and two operands that are trees, and produces a tree for the
37    result, assuming the type comes from `sizetype'.
38
39    size_int takes an integer value, and creates a tree constant
40    with type from `sizetype'.
41
42    force_fit_type takes a constant, an overflowable flag and prior
43    overflow indicators.  It forces the value to fit the type and sets
44    TREE_OVERFLOW and TREE_CONSTANT_OVERFLOW as appropriate.  */
45
46 #include "config.h"
47 #include "system.h"
48 #include "coretypes.h"
49 #include "tm.h"
50 #include "flags.h"
51 #include "tree.h"
52 #include "real.h"
53 #include "rtl.h"
54 #include "expr.h"
55 #include "tm_p.h"
56 #include "toplev.h"
57 #include "ggc.h"
58 #include "hashtab.h"
59 #include "langhooks.h"
60 #include "md5.h"
61
62 /* The following constants represent a bit based encoding of GCC's
63    comparison operators.  This encoding simplifies transformations
64    on relational comparison operators, such as AND and OR.  */
65 enum comparison_code {
66   COMPCODE_FALSE = 0,
67   COMPCODE_LT = 1,
68   COMPCODE_EQ = 2,
69   COMPCODE_LE = 3,
70   COMPCODE_GT = 4,
71   COMPCODE_LTGT = 5,
72   COMPCODE_GE = 6,
73   COMPCODE_ORD = 7,
74   COMPCODE_UNORD = 8,
75   COMPCODE_UNLT = 9,
76   COMPCODE_UNEQ = 10,
77   COMPCODE_UNLE = 11,
78   COMPCODE_UNGT = 12,
79   COMPCODE_NE = 13,
80   COMPCODE_UNGE = 14,
81   COMPCODE_TRUE = 15
82 };
83
84 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
85 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
86 static bool negate_mathfn_p (enum built_in_function);
87 static bool negate_expr_p (tree);
88 static tree negate_expr (tree);
89 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
90 static tree associate_trees (tree, tree, enum tree_code, tree);
91 static tree const_binop (enum tree_code, tree, tree, int);
92 static enum tree_code invert_tree_comparison (enum tree_code, bool);
93 static enum comparison_code comparison_to_compcode (enum tree_code);
94 static enum tree_code compcode_to_comparison (enum comparison_code);
95 static tree combine_comparisons (enum tree_code, enum tree_code,
96                                  enum tree_code, tree, tree, tree);
97 static int truth_value_p (enum tree_code);
98 static int operand_equal_for_comparison_p (tree, tree, tree);
99 static int twoval_comparison_p (tree, tree *, tree *, int *);
100 static tree eval_subst (tree, tree, tree, tree, tree);
101 static tree pedantic_omit_one_operand (tree, tree, tree);
102 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
103 static tree make_bit_field_ref (tree, tree, int, int, int);
104 static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
105 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
106                                     enum machine_mode *, int *, int *,
107                                     tree *, tree *);
108 static int all_ones_mask_p (tree, int);
109 static tree sign_bit_p (tree, tree);
110 static int simple_operand_p (tree);
111 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
112 static tree make_range (tree, int *, tree *, tree *);
113 static tree build_range_check (tree, tree, int, tree, tree);
114 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
115                          tree);
116 static tree fold_range_test (enum tree_code, tree, tree, tree);
117 static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
118 static tree unextend (tree, int, int, tree);
119 static tree fold_truthop (enum tree_code, tree, tree, tree);
120 static tree optimize_minmax_comparison (enum tree_code, tree, tree, tree);
121 static tree extract_muldiv (tree, tree, enum tree_code, tree);
122 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree);
123 static int multiple_of_p (tree, tree, tree);
124 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree,
125                                                  tree, tree,
126                                                  tree, tree, int);
127 static bool fold_real_zero_addition_p (tree, tree, int);
128 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
129                                  tree, tree, tree);
130 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
131 static tree fold_div_compare (enum tree_code, tree, tree, tree);
132 static bool reorder_operands_p (tree, tree);
133 static tree fold_negate_const (tree, tree);
134 static tree fold_not_const (tree, tree);
135 static tree fold_relational_const (enum tree_code, tree, tree, tree);
136 static bool tree_expr_nonzero_p (tree);
137
138 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
139    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
140    and SUM1.  Then this yields nonzero if overflow occurred during the
141    addition.
142
143    Overflow occurs if A and B have the same sign, but A and SUM differ in
144    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
145    sign.  */
146 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
147 \f
148 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
149    We do that by representing the two-word integer in 4 words, with only
150    HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
151    number.  The value of the word is LOWPART + HIGHPART * BASE.  */
152
153 #define LOWPART(x) \
154   ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
155 #define HIGHPART(x) \
156   ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
157 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
158
159 /* Unpack a two-word integer into 4 words.
160    LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
161    WORDS points to the array of HOST_WIDE_INTs.  */
162
163 static void
164 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
165 {
166   words[0] = LOWPART (low);
167   words[1] = HIGHPART (low);
168   words[2] = LOWPART (hi);
169   words[3] = HIGHPART (hi);
170 }
171
172 /* Pack an array of 4 words into a two-word integer.
173    WORDS points to the array of words.
174    The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces.  */
175
176 static void
177 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
178         HOST_WIDE_INT *hi)
179 {
180   *low = words[0] + words[1] * BASE;
181   *hi = words[2] + words[3] * BASE;
182 }
183 \f
184 /* T is an INT_CST node.  OVERFLOWABLE indicates if we are interested
185    in overflow of the value, when >0 we are only interested in signed
186    overflow, for <0 we are interested in any overflow.  OVERFLOWED
187    indicates whether overflow has already occurred.  CONST_OVERFLOWED
188    indicates whether constant overflow has already occurred.  We force
189    T's value to be within range of T's type (by setting to 0 or 1 all
190    the bits outside the type's range).  We set TREE_OVERFLOWED if,
191         OVERFLOWED is nonzero,
192         or OVERFLOWABLE is >0 and signed overflow occurs
193         or OVERFLOWABLE is <0 and any overflow occurs
194    We set TREE_CONSTANT_OVERFLOWED if,
195         CONST_OVERFLOWED is nonzero
196         or we set TREE_OVERFLOWED.
197   We return either the original T, or a copy.  */
198
199 tree
200 force_fit_type (tree t, int overflowable,
201                 bool overflowed, bool overflowed_const)
202 {
203   unsigned HOST_WIDE_INT low;
204   HOST_WIDE_INT high;
205   unsigned int prec;
206   int sign_extended_type;
207
208   gcc_assert (TREE_CODE (t) == INTEGER_CST);
209
210   low = TREE_INT_CST_LOW (t);
211   high = TREE_INT_CST_HIGH (t);
212
213   if (POINTER_TYPE_P (TREE_TYPE (t))
214       || TREE_CODE (TREE_TYPE (t)) == OFFSET_TYPE)
215     prec = POINTER_SIZE;
216   else
217     prec = TYPE_PRECISION (TREE_TYPE (t));
218   /* Size types *are* sign extended.  */
219   sign_extended_type = (!TYPE_UNSIGNED (TREE_TYPE (t))
220                         || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
221                             && TYPE_IS_SIZETYPE (TREE_TYPE (t))));
222
223   /* First clear all bits that are beyond the type's precision.  */
224
225   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
226     ;
227   else if (prec > HOST_BITS_PER_WIDE_INT)
228     high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
229   else
230     {
231       high = 0;
232       if (prec < HOST_BITS_PER_WIDE_INT)
233         low &= ~((HOST_WIDE_INT) (-1) << prec);
234     }
235
236   if (!sign_extended_type)
237     /* No sign extension */;
238   else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
239     /* Correct width already.  */;
240   else if (prec > HOST_BITS_PER_WIDE_INT)
241     {
242       /* Sign extend top half? */
243       if (high & ((unsigned HOST_WIDE_INT)1
244                   << (prec - HOST_BITS_PER_WIDE_INT - 1)))
245         high |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
246     }
247   else if (prec == HOST_BITS_PER_WIDE_INT)
248     {
249       if ((HOST_WIDE_INT)low < 0)
250         high = -1;
251     }
252   else
253     {
254       /* Sign extend bottom half? */
255       if (low & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
256         {
257           high = -1;
258           low |= (HOST_WIDE_INT)(-1) << prec;
259         }
260     }
261
262   /* If the value changed, return a new node.  */
263   if (overflowed || overflowed_const
264       || low != TREE_INT_CST_LOW (t) || high != TREE_INT_CST_HIGH (t))
265     {
266       t = build_int_cst_wide (TREE_TYPE (t), low, high);
267
268       if (overflowed
269           || overflowable < 0
270           || (overflowable > 0 && sign_extended_type))
271         {
272           t = copy_node (t);
273           TREE_OVERFLOW (t) = 1;
274           TREE_CONSTANT_OVERFLOW (t) = 1;
275         }
276       else if (overflowed_const)
277         {
278           t = copy_node (t);
279           TREE_CONSTANT_OVERFLOW (t) = 1;
280         }
281     }
282
283   return t;
284 }
285 \f
286 /* Add two doubleword integers with doubleword result.
287    Each argument is given as two `HOST_WIDE_INT' pieces.
288    One argument is L1 and H1; the other, L2 and H2.
289    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
290
291 int
292 add_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
293             unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
294             unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
295 {
296   unsigned HOST_WIDE_INT l;
297   HOST_WIDE_INT h;
298
299   l = l1 + l2;
300   h = h1 + h2 + (l < l1);
301
302   *lv = l;
303   *hv = h;
304   return OVERFLOW_SUM_SIGN (h1, h2, h);
305 }
306
307 /* Negate a doubleword integer with doubleword result.
308    Return nonzero if the operation overflows, assuming it's signed.
309    The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
310    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
311
312 int
313 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
314             unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
315 {
316   if (l1 == 0)
317     {
318       *lv = 0;
319       *hv = - h1;
320       return (*hv & h1) < 0;
321     }
322   else
323     {
324       *lv = -l1;
325       *hv = ~h1;
326       return 0;
327     }
328 }
329 \f
330 /* Multiply two doubleword integers with doubleword result.
331    Return nonzero if the operation overflows, assuming it's signed.
332    Each argument is given as two `HOST_WIDE_INT' pieces.
333    One argument is L1 and H1; the other, L2 and H2.
334    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
335
336 int
337 mul_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
338             unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
339             unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
340 {
341   HOST_WIDE_INT arg1[4];
342   HOST_WIDE_INT arg2[4];
343   HOST_WIDE_INT prod[4 * 2];
344   unsigned HOST_WIDE_INT carry;
345   int i, j, k;
346   unsigned HOST_WIDE_INT toplow, neglow;
347   HOST_WIDE_INT tophigh, neghigh;
348
349   encode (arg1, l1, h1);
350   encode (arg2, l2, h2);
351
352   memset (prod, 0, sizeof prod);
353
354   for (i = 0; i < 4; i++)
355     {
356       carry = 0;
357       for (j = 0; j < 4; j++)
358         {
359           k = i + j;
360           /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000.  */
361           carry += arg1[i] * arg2[j];
362           /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF.  */
363           carry += prod[k];
364           prod[k] = LOWPART (carry);
365           carry = HIGHPART (carry);
366         }
367       prod[i + 4] = carry;
368     }
369
370   decode (prod, lv, hv);        /* This ignores prod[4] through prod[4*2-1] */
371
372   /* Check for overflow by calculating the top half of the answer in full;
373      it should agree with the low half's sign bit.  */
374   decode (prod + 4, &toplow, &tophigh);
375   if (h1 < 0)
376     {
377       neg_double (l2, h2, &neglow, &neghigh);
378       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
379     }
380   if (h2 < 0)
381     {
382       neg_double (l1, h1, &neglow, &neghigh);
383       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
384     }
385   return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
386 }
387 \f
388 /* Shift the doubleword integer in L1, H1 left by COUNT places
389    keeping only PREC bits of result.
390    Shift right if COUNT is negative.
391    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
392    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
393
394 void
395 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
396                HOST_WIDE_INT count, unsigned int prec,
397                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
398 {
399   unsigned HOST_WIDE_INT signmask;
400
401   if (count < 0)
402     {
403       rshift_double (l1, h1, -count, prec, lv, hv, arith);
404       return;
405     }
406
407   if (SHIFT_COUNT_TRUNCATED)
408     count %= prec;
409
410   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
411     {
412       /* Shifting by the host word size is undefined according to the
413          ANSI standard, so we must handle this as a special case.  */
414       *hv = 0;
415       *lv = 0;
416     }
417   else if (count >= HOST_BITS_PER_WIDE_INT)
418     {
419       *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
420       *lv = 0;
421     }
422   else
423     {
424       *hv = (((unsigned HOST_WIDE_INT) h1 << count)
425              | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
426       *lv = l1 << count;
427     }
428
429   /* Sign extend all bits that are beyond the precision.  */
430
431   signmask = -((prec > HOST_BITS_PER_WIDE_INT
432                 ? ((unsigned HOST_WIDE_INT) *hv
433                    >> (prec - HOST_BITS_PER_WIDE_INT - 1))
434                 : (*lv >> (prec - 1))) & 1);
435
436   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
437     ;
438   else if (prec >= HOST_BITS_PER_WIDE_INT)
439     {
440       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
441       *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
442     }
443   else
444     {
445       *hv = signmask;
446       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
447       *lv |= signmask << prec;
448     }
449 }
450
451 /* Shift the doubleword integer in L1, H1 right by COUNT places
452    keeping only PREC bits of result.  COUNT must be positive.
453    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
454    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
455
456 void
457 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
458                HOST_WIDE_INT count, unsigned int prec,
459                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
460                int arith)
461 {
462   unsigned HOST_WIDE_INT signmask;
463
464   signmask = (arith
465               ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
466               : 0);
467
468   if (SHIFT_COUNT_TRUNCATED)
469     count %= prec;
470
471   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
472     {
473       /* Shifting by the host word size is undefined according to the
474          ANSI standard, so we must handle this as a special case.  */
475       *hv = 0;
476       *lv = 0;
477     }
478   else if (count >= HOST_BITS_PER_WIDE_INT)
479     {
480       *hv = 0;
481       *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
482     }
483   else
484     {
485       *hv = (unsigned HOST_WIDE_INT) h1 >> count;
486       *lv = ((l1 >> count)
487              | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
488     }
489
490   /* Zero / sign extend all bits that are beyond the precision.  */
491
492   if (count >= (HOST_WIDE_INT)prec)
493     {
494       *hv = signmask;
495       *lv = signmask;
496     }
497   else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
498     ;
499   else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
500     {
501       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
502       *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
503     }
504   else
505     {
506       *hv = signmask;
507       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
508       *lv |= signmask << (prec - count);
509     }
510 }
511 \f
512 /* Rotate the doubleword integer in L1, H1 left by COUNT places
513    keeping only PREC bits of result.
514    Rotate right if COUNT is negative.
515    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
516
517 void
518 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
519                 HOST_WIDE_INT count, unsigned int prec,
520                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
521 {
522   unsigned HOST_WIDE_INT s1l, s2l;
523   HOST_WIDE_INT s1h, s2h;
524
525   count %= prec;
526   if (count < 0)
527     count += prec;
528
529   lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
530   rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
531   *lv = s1l | s2l;
532   *hv = s1h | s2h;
533 }
534
535 /* Rotate the doubleword integer in L1, H1 left by COUNT places
536    keeping only PREC bits of result.  COUNT must be positive.
537    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
538
539 void
540 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
541                 HOST_WIDE_INT count, unsigned int prec,
542                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
543 {
544   unsigned HOST_WIDE_INT s1l, s2l;
545   HOST_WIDE_INT s1h, s2h;
546
547   count %= prec;
548   if (count < 0)
549     count += prec;
550
551   rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
552   lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
553   *lv = s1l | s2l;
554   *hv = s1h | s2h;
555 }
556 \f
557 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
558    for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
559    CODE is a tree code for a kind of division, one of
560    TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
561    or EXACT_DIV_EXPR
562    It controls how the quotient is rounded to an integer.
563    Return nonzero if the operation overflows.
564    UNS nonzero says do unsigned division.  */
565
566 int
567 div_and_round_double (enum tree_code code, int uns,
568                       unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
569                       HOST_WIDE_INT hnum_orig,
570                       unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
571                       HOST_WIDE_INT hden_orig,
572                       unsigned HOST_WIDE_INT *lquo,
573                       HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
574                       HOST_WIDE_INT *hrem)
575 {
576   int quo_neg = 0;
577   HOST_WIDE_INT num[4 + 1];     /* extra element for scaling.  */
578   HOST_WIDE_INT den[4], quo[4];
579   int i, j;
580   unsigned HOST_WIDE_INT work;
581   unsigned HOST_WIDE_INT carry = 0;
582   unsigned HOST_WIDE_INT lnum = lnum_orig;
583   HOST_WIDE_INT hnum = hnum_orig;
584   unsigned HOST_WIDE_INT lden = lden_orig;
585   HOST_WIDE_INT hden = hden_orig;
586   int overflow = 0;
587
588   if (hden == 0 && lden == 0)
589     overflow = 1, lden = 1;
590
591   /* Calculate quotient sign and convert operands to unsigned.  */
592   if (!uns)
593     {
594       if (hnum < 0)
595         {
596           quo_neg = ~ quo_neg;
597           /* (minimum integer) / (-1) is the only overflow case.  */
598           if (neg_double (lnum, hnum, &lnum, &hnum)
599               && ((HOST_WIDE_INT) lden & hden) == -1)
600             overflow = 1;
601         }
602       if (hden < 0)
603         {
604           quo_neg = ~ quo_neg;
605           neg_double (lden, hden, &lden, &hden);
606         }
607     }
608
609   if (hnum == 0 && hden == 0)
610     {                           /* single precision */
611       *hquo = *hrem = 0;
612       /* This unsigned division rounds toward zero.  */
613       *lquo = lnum / lden;
614       goto finish_up;
615     }
616
617   if (hnum == 0)
618     {                           /* trivial case: dividend < divisor */
619       /* hden != 0 already checked.  */
620       *hquo = *lquo = 0;
621       *hrem = hnum;
622       *lrem = lnum;
623       goto finish_up;
624     }
625
626   memset (quo, 0, sizeof quo);
627
628   memset (num, 0, sizeof num);  /* to zero 9th element */
629   memset (den, 0, sizeof den);
630
631   encode (num, lnum, hnum);
632   encode (den, lden, hden);
633
634   /* Special code for when the divisor < BASE.  */
635   if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
636     {
637       /* hnum != 0 already checked.  */
638       for (i = 4 - 1; i >= 0; i--)
639         {
640           work = num[i] + carry * BASE;
641           quo[i] = work / lden;
642           carry = work % lden;
643         }
644     }
645   else
646     {
647       /* Full double precision division,
648          with thanks to Don Knuth's "Seminumerical Algorithms".  */
649       int num_hi_sig, den_hi_sig;
650       unsigned HOST_WIDE_INT quo_est, scale;
651
652       /* Find the highest nonzero divisor digit.  */
653       for (i = 4 - 1;; i--)
654         if (den[i] != 0)
655           {
656             den_hi_sig = i;
657             break;
658           }
659
660       /* Insure that the first digit of the divisor is at least BASE/2.
661          This is required by the quotient digit estimation algorithm.  */
662
663       scale = BASE / (den[den_hi_sig] + 1);
664       if (scale > 1)
665         {               /* scale divisor and dividend */
666           carry = 0;
667           for (i = 0; i <= 4 - 1; i++)
668             {
669               work = (num[i] * scale) + carry;
670               num[i] = LOWPART (work);
671               carry = HIGHPART (work);
672             }
673
674           num[4] = carry;
675           carry = 0;
676           for (i = 0; i <= 4 - 1; i++)
677             {
678               work = (den[i] * scale) + carry;
679               den[i] = LOWPART (work);
680               carry = HIGHPART (work);
681               if (den[i] != 0) den_hi_sig = i;
682             }
683         }
684
685       num_hi_sig = 4;
686
687       /* Main loop */
688       for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
689         {
690           /* Guess the next quotient digit, quo_est, by dividing the first
691              two remaining dividend digits by the high order quotient digit.
692              quo_est is never low and is at most 2 high.  */
693           unsigned HOST_WIDE_INT tmp;
694
695           num_hi_sig = i + den_hi_sig + 1;
696           work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
697           if (num[num_hi_sig] != den[den_hi_sig])
698             quo_est = work / den[den_hi_sig];
699           else
700             quo_est = BASE - 1;
701
702           /* Refine quo_est so it's usually correct, and at most one high.  */
703           tmp = work - quo_est * den[den_hi_sig];
704           if (tmp < BASE
705               && (den[den_hi_sig - 1] * quo_est
706                   > (tmp * BASE + num[num_hi_sig - 2])))
707             quo_est--;
708
709           /* Try QUO_EST as the quotient digit, by multiplying the
710              divisor by QUO_EST and subtracting from the remaining dividend.
711              Keep in mind that QUO_EST is the I - 1st digit.  */
712
713           carry = 0;
714           for (j = 0; j <= den_hi_sig; j++)
715             {
716               work = quo_est * den[j] + carry;
717               carry = HIGHPART (work);
718               work = num[i + j] - LOWPART (work);
719               num[i + j] = LOWPART (work);
720               carry += HIGHPART (work) != 0;
721             }
722
723           /* If quo_est was high by one, then num[i] went negative and
724              we need to correct things.  */
725           if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
726             {
727               quo_est--;
728               carry = 0;                /* add divisor back in */
729               for (j = 0; j <= den_hi_sig; j++)
730                 {
731                   work = num[i + j] + den[j] + carry;
732                   carry = HIGHPART (work);
733                   num[i + j] = LOWPART (work);
734                 }
735
736               num [num_hi_sig] += carry;
737             }
738
739           /* Store the quotient digit.  */
740           quo[i] = quo_est;
741         }
742     }
743
744   decode (quo, lquo, hquo);
745
746  finish_up:
747   /* If result is negative, make it so.  */
748   if (quo_neg)
749     neg_double (*lquo, *hquo, lquo, hquo);
750
751   /* Compute trial remainder:  rem = num - (quo * den)  */
752   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
753   neg_double (*lrem, *hrem, lrem, hrem);
754   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
755
756   switch (code)
757     {
758     case TRUNC_DIV_EXPR:
759     case TRUNC_MOD_EXPR:        /* round toward zero */
760     case EXACT_DIV_EXPR:        /* for this one, it shouldn't matter */
761       return overflow;
762
763     case FLOOR_DIV_EXPR:
764     case FLOOR_MOD_EXPR:        /* round toward negative infinity */
765       if (quo_neg && (*lrem != 0 || *hrem != 0))   /* ratio < 0 && rem != 0 */
766         {
767           /* quo = quo - 1;  */
768           add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT)  -1,
769                       lquo, hquo);
770         }
771       else
772         return overflow;
773       break;
774
775     case CEIL_DIV_EXPR:
776     case CEIL_MOD_EXPR:         /* round toward positive infinity */
777       if (!quo_neg && (*lrem != 0 || *hrem != 0))  /* ratio > 0 && rem != 0 */
778         {
779           add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
780                       lquo, hquo);
781         }
782       else
783         return overflow;
784       break;
785
786     case ROUND_DIV_EXPR:
787     case ROUND_MOD_EXPR:        /* round to closest integer */
788       {
789         unsigned HOST_WIDE_INT labs_rem = *lrem;
790         HOST_WIDE_INT habs_rem = *hrem;
791         unsigned HOST_WIDE_INT labs_den = lden, ltwice;
792         HOST_WIDE_INT habs_den = hden, htwice;
793
794         /* Get absolute values.  */
795         if (*hrem < 0)
796           neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
797         if (hden < 0)
798           neg_double (lden, hden, &labs_den, &habs_den);
799
800         /* If (2 * abs (lrem) >= abs (lden)) */
801         mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
802                     labs_rem, habs_rem, &ltwice, &htwice);
803
804         if (((unsigned HOST_WIDE_INT) habs_den
805              < (unsigned HOST_WIDE_INT) htwice)
806             || (((unsigned HOST_WIDE_INT) habs_den
807                  == (unsigned HOST_WIDE_INT) htwice)
808                 && (labs_den < ltwice)))
809           {
810             if (*hquo < 0)
811               /* quo = quo - 1;  */
812               add_double (*lquo, *hquo,
813                           (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
814             else
815               /* quo = quo + 1; */
816               add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
817                           lquo, hquo);
818           }
819         else
820           return overflow;
821       }
822       break;
823
824     default:
825       gcc_unreachable ();
826     }
827
828   /* Compute true remainder:  rem = num - (quo * den)  */
829   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
830   neg_double (*lrem, *hrem, lrem, hrem);
831   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
832   return overflow;
833 }
834
835 /* If ARG2 divides ARG1 with zero remainder, carries out the division
836    of type CODE and returns the quotient.
837    Otherwise returns NULL_TREE.  */
838
839 static tree
840 div_if_zero_remainder (enum tree_code code, tree arg1, tree arg2)
841 {
842   unsigned HOST_WIDE_INT int1l, int2l;
843   HOST_WIDE_INT int1h, int2h;
844   unsigned HOST_WIDE_INT quol, reml;
845   HOST_WIDE_INT quoh, remh;
846   tree type = TREE_TYPE (arg1);
847   int uns = TYPE_UNSIGNED (type);
848
849   int1l = TREE_INT_CST_LOW (arg1);
850   int1h = TREE_INT_CST_HIGH (arg1);
851   int2l = TREE_INT_CST_LOW (arg2);
852   int2h = TREE_INT_CST_HIGH (arg2);
853
854   div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
855                         &quol, &quoh, &reml, &remh);
856   if (remh != 0 || reml != 0)
857     return NULL_TREE;
858
859   return build_int_cst_wide (type, quol, quoh);
860 }
861 \f
862 /* Return true if built-in mathematical function specified by CODE
863    preserves the sign of it argument, i.e. -f(x) == f(-x).  */
864
865 static bool
866 negate_mathfn_p (enum built_in_function code)
867 {
868   switch (code)
869     {
870     case BUILT_IN_ASIN:
871     case BUILT_IN_ASINF:
872     case BUILT_IN_ASINL:
873     case BUILT_IN_ATAN:
874     case BUILT_IN_ATANF:
875     case BUILT_IN_ATANL:
876     case BUILT_IN_SIN:
877     case BUILT_IN_SINF:
878     case BUILT_IN_SINL:
879     case BUILT_IN_TAN:
880     case BUILT_IN_TANF:
881     case BUILT_IN_TANL:
882       return true;
883
884     default:
885       break;
886     }
887   return false;
888 }
889
890 /* Check whether we may negate an integer constant T without causing
891    overflow.  */
892
893 bool
894 may_negate_without_overflow_p (tree t)
895 {
896   unsigned HOST_WIDE_INT val;
897   unsigned int prec;
898   tree type;
899
900   gcc_assert (TREE_CODE (t) == INTEGER_CST);
901
902   type = TREE_TYPE (t);
903   if (TYPE_UNSIGNED (type))
904     return false;
905
906   prec = TYPE_PRECISION (type);
907   if (prec > HOST_BITS_PER_WIDE_INT)
908     {
909       if (TREE_INT_CST_LOW (t) != 0)
910         return true;
911       prec -= HOST_BITS_PER_WIDE_INT;
912       val = TREE_INT_CST_HIGH (t);
913     }
914   else
915     val = TREE_INT_CST_LOW (t);
916   if (prec < HOST_BITS_PER_WIDE_INT)
917     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
918   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
919 }
920
921 /* Determine whether an expression T can be cheaply negated using
922    the function negate_expr.  */
923
924 static bool
925 negate_expr_p (tree t)
926 {
927   tree type;
928
929   if (t == 0)
930     return false;
931
932   type = TREE_TYPE (t);
933
934   STRIP_SIGN_NOPS (t);
935   switch (TREE_CODE (t))
936     {
937     case INTEGER_CST:
938       if (TYPE_UNSIGNED (type) || ! flag_trapv)
939         return true;
940
941       /* Check that -CST will not overflow type.  */
942       return may_negate_without_overflow_p (t);
943
944     case REAL_CST:
945     case NEGATE_EXPR:
946       return true;
947
948     case COMPLEX_CST:
949       return negate_expr_p (TREE_REALPART (t))
950              && negate_expr_p (TREE_IMAGPART (t));
951
952     case PLUS_EXPR:
953       if (FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
954         return false;
955       /* -(A + B) -> (-B) - A.  */
956       if (negate_expr_p (TREE_OPERAND (t, 1))
957           && reorder_operands_p (TREE_OPERAND (t, 0),
958                                  TREE_OPERAND (t, 1)))
959         return true;
960       /* -(A + B) -> (-A) - B.  */
961       return negate_expr_p (TREE_OPERAND (t, 0));
962
963     case MINUS_EXPR:
964       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
965       return (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
966              && reorder_operands_p (TREE_OPERAND (t, 0),
967                                     TREE_OPERAND (t, 1));
968
969     case MULT_EXPR:
970       if (TYPE_UNSIGNED (TREE_TYPE (t)))
971         break;
972
973       /* Fall through.  */
974
975     case RDIV_EXPR:
976       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
977         return negate_expr_p (TREE_OPERAND (t, 1))
978                || negate_expr_p (TREE_OPERAND (t, 0));
979       break;
980
981     case NOP_EXPR:
982       /* Negate -((double)float) as (double)(-float).  */
983       if (TREE_CODE (type) == REAL_TYPE)
984         {
985           tree tem = strip_float_extensions (t);
986           if (tem != t)
987             return negate_expr_p (tem);
988         }
989       break;
990
991     case CALL_EXPR:
992       /* Negate -f(x) as f(-x).  */
993       if (negate_mathfn_p (builtin_mathfn_code (t)))
994         return negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1)));
995       break;
996
997     case RSHIFT_EXPR:
998       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
999       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1000         {
1001           tree op1 = TREE_OPERAND (t, 1);
1002           if (TREE_INT_CST_HIGH (op1) == 0
1003               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1004                  == TREE_INT_CST_LOW (op1))
1005             return true;
1006         }
1007       break;
1008
1009     default:
1010       break;
1011     }
1012   return false;
1013 }
1014
1015 /* Given T, an expression, return the negation of T.  Allow for T to be
1016    null, in which case return null.  */
1017
1018 static tree
1019 negate_expr (tree t)
1020 {
1021   tree type;
1022   tree tem;
1023
1024   if (t == 0)
1025     return 0;
1026
1027   type = TREE_TYPE (t);
1028   STRIP_SIGN_NOPS (t);
1029
1030   switch (TREE_CODE (t))
1031     {
1032     case INTEGER_CST:
1033       tem = fold_negate_const (t, type);
1034       if (! TREE_OVERFLOW (tem)
1035           || TYPE_UNSIGNED (type)
1036           || ! flag_trapv)
1037         return tem;
1038       break;
1039
1040     case REAL_CST:
1041       tem = fold_negate_const (t, type);
1042       /* Two's complement FP formats, such as c4x, may overflow.  */
1043       if (! TREE_OVERFLOW (tem) || ! flag_trapping_math)
1044         return fold_convert (type, tem);
1045       break;
1046
1047     case COMPLEX_CST:
1048       {
1049         tree rpart = negate_expr (TREE_REALPART (t));
1050         tree ipart = negate_expr (TREE_IMAGPART (t));
1051
1052         if ((TREE_CODE (rpart) == REAL_CST
1053              && TREE_CODE (ipart) == REAL_CST)
1054             || (TREE_CODE (rpart) == INTEGER_CST
1055                 && TREE_CODE (ipart) == INTEGER_CST))
1056           return build_complex (type, rpart, ipart);
1057       }
1058       break;
1059
1060     case NEGATE_EXPR:
1061       return fold_convert (type, TREE_OPERAND (t, 0));
1062
1063     case PLUS_EXPR:
1064       if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1065         {
1066           /* -(A + B) -> (-B) - A.  */
1067           if (negate_expr_p (TREE_OPERAND (t, 1))
1068               && reorder_operands_p (TREE_OPERAND (t, 0),
1069                                      TREE_OPERAND (t, 1)))
1070             {
1071               tem = negate_expr (TREE_OPERAND (t, 1));
1072               tem = fold_build2 (MINUS_EXPR, TREE_TYPE (t),
1073                                  tem, TREE_OPERAND (t, 0));
1074               return fold_convert (type, tem);
1075             }
1076
1077           /* -(A + B) -> (-A) - B.  */
1078           if (negate_expr_p (TREE_OPERAND (t, 0)))
1079             {
1080               tem = negate_expr (TREE_OPERAND (t, 0));
1081               tem = fold_build2 (MINUS_EXPR, TREE_TYPE (t),
1082                                  tem, TREE_OPERAND (t, 1));
1083               return fold_convert (type, tem);
1084             }
1085         }
1086       break;
1087
1088     case MINUS_EXPR:
1089       /* - (A - B) -> B - A  */
1090       if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1091           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1092         return fold_convert (type,
1093                              fold_build2 (MINUS_EXPR, TREE_TYPE (t),
1094                                           TREE_OPERAND (t, 1),
1095                                           TREE_OPERAND (t, 0)));
1096       break;
1097
1098     case MULT_EXPR:
1099       if (TYPE_UNSIGNED (TREE_TYPE (t)))
1100         break;
1101
1102       /* Fall through.  */
1103
1104     case RDIV_EXPR:
1105       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1106         {
1107           tem = TREE_OPERAND (t, 1);
1108           if (negate_expr_p (tem))
1109             return fold_convert (type,
1110                                  fold_build2 (TREE_CODE (t), TREE_TYPE (t),
1111                                               TREE_OPERAND (t, 0),
1112                                               negate_expr (tem)));
1113           tem = TREE_OPERAND (t, 0);
1114           if (negate_expr_p (tem))
1115             return fold_convert (type,
1116                                  fold_build2 (TREE_CODE (t), TREE_TYPE (t),
1117                                               negate_expr (tem),
1118                                               TREE_OPERAND (t, 1)));
1119         }
1120       break;
1121
1122     case NOP_EXPR:
1123       /* Convert -((double)float) into (double)(-float).  */
1124       if (TREE_CODE (type) == REAL_TYPE)
1125         {
1126           tem = strip_float_extensions (t);
1127           if (tem != t && negate_expr_p (tem))
1128             return fold_convert (type, negate_expr (tem));
1129         }
1130       break;
1131
1132     case CALL_EXPR:
1133       /* Negate -f(x) as f(-x).  */
1134       if (negate_mathfn_p (builtin_mathfn_code (t))
1135           && negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1))))
1136         {
1137           tree fndecl, arg, arglist;
1138
1139           fndecl = get_callee_fndecl (t);
1140           arg = negate_expr (TREE_VALUE (TREE_OPERAND (t, 1)));
1141           arglist = build_tree_list (NULL_TREE, arg);
1142           return build_function_call_expr (fndecl, arglist);
1143         }
1144       break;
1145
1146     case RSHIFT_EXPR:
1147       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1148       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1149         {
1150           tree op1 = TREE_OPERAND (t, 1);
1151           if (TREE_INT_CST_HIGH (op1) == 0
1152               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1153                  == TREE_INT_CST_LOW (op1))
1154             {
1155               tree ntype = TYPE_UNSIGNED (type)
1156                            ? lang_hooks.types.signed_type (type)
1157                            : lang_hooks.types.unsigned_type (type);
1158               tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1159               temp = fold_build2 (RSHIFT_EXPR, ntype, temp, op1);
1160               return fold_convert (type, temp);
1161             }
1162         }
1163       break;
1164
1165     default:
1166       break;
1167     }
1168
1169   tem = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1170   return fold_convert (type, tem);
1171 }
1172 \f
1173 /* Split a tree IN into a constant, literal and variable parts that could be
1174    combined with CODE to make IN.  "constant" means an expression with
1175    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
1176    commutative arithmetic operation.  Store the constant part into *CONP,
1177    the literal in *LITP and return the variable part.  If a part isn't
1178    present, set it to null.  If the tree does not decompose in this way,
1179    return the entire tree as the variable part and the other parts as null.
1180
1181    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
1182    case, we negate an operand that was subtracted.  Except if it is a
1183    literal for which we use *MINUS_LITP instead.
1184
1185    If NEGATE_P is true, we are negating all of IN, again except a literal
1186    for which we use *MINUS_LITP instead.
1187
1188    If IN is itself a literal or constant, return it as appropriate.
1189
1190    Note that we do not guarantee that any of the three values will be the
1191    same type as IN, but they will have the same signedness and mode.  */
1192
1193 static tree
1194 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1195             tree *minus_litp, int negate_p)
1196 {
1197   tree var = 0;
1198
1199   *conp = 0;
1200   *litp = 0;
1201   *minus_litp = 0;
1202
1203   /* Strip any conversions that don't change the machine mode or signedness.  */
1204   STRIP_SIGN_NOPS (in);
1205
1206   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
1207     *litp = in;
1208   else if (TREE_CODE (in) == code
1209            || (! FLOAT_TYPE_P (TREE_TYPE (in))
1210                /* We can associate addition and subtraction together (even
1211                   though the C standard doesn't say so) for integers because
1212                   the value is not affected.  For reals, the value might be
1213                   affected, so we can't.  */
1214                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1215                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1216     {
1217       tree op0 = TREE_OPERAND (in, 0);
1218       tree op1 = TREE_OPERAND (in, 1);
1219       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1220       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1221
1222       /* First see if either of the operands is a literal, then a constant.  */
1223       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
1224         *litp = op0, op0 = 0;
1225       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
1226         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1227
1228       if (op0 != 0 && TREE_CONSTANT (op0))
1229         *conp = op0, op0 = 0;
1230       else if (op1 != 0 && TREE_CONSTANT (op1))
1231         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1232
1233       /* If we haven't dealt with either operand, this is not a case we can
1234          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
1235       if (op0 != 0 && op1 != 0)
1236         var = in;
1237       else if (op0 != 0)
1238         var = op0;
1239       else
1240         var = op1, neg_var_p = neg1_p;
1241
1242       /* Now do any needed negations.  */
1243       if (neg_litp_p)
1244         *minus_litp = *litp, *litp = 0;
1245       if (neg_conp_p)
1246         *conp = negate_expr (*conp);
1247       if (neg_var_p)
1248         var = negate_expr (var);
1249     }
1250   else if (TREE_CONSTANT (in))
1251     *conp = in;
1252   else
1253     var = in;
1254
1255   if (negate_p)
1256     {
1257       if (*litp)
1258         *minus_litp = *litp, *litp = 0;
1259       else if (*minus_litp)
1260         *litp = *minus_litp, *minus_litp = 0;
1261       *conp = negate_expr (*conp);
1262       var = negate_expr (var);
1263     }
1264
1265   return var;
1266 }
1267
1268 /* Re-associate trees split by the above function.  T1 and T2 are either
1269    expressions to associate or null.  Return the new expression, if any.  If
1270    we build an operation, do it in TYPE and with CODE.  */
1271
1272 static tree
1273 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1274 {
1275   if (t1 == 0)
1276     return t2;
1277   else if (t2 == 0)
1278     return t1;
1279
1280   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1281      try to fold this since we will have infinite recursion.  But do
1282      deal with any NEGATE_EXPRs.  */
1283   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1284       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1285     {
1286       if (code == PLUS_EXPR)
1287         {
1288           if (TREE_CODE (t1) == NEGATE_EXPR)
1289             return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1290                            fold_convert (type, TREE_OPERAND (t1, 0)));
1291           else if (TREE_CODE (t2) == NEGATE_EXPR)
1292             return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1293                            fold_convert (type, TREE_OPERAND (t2, 0)));
1294           else if (integer_zerop (t2))
1295             return fold_convert (type, t1);
1296         }
1297       else if (code == MINUS_EXPR)
1298         {
1299           if (integer_zerop (t2))
1300             return fold_convert (type, t1);
1301         }
1302
1303       return build2 (code, type, fold_convert (type, t1),
1304                      fold_convert (type, t2));
1305     }
1306
1307   return fold_build2 (code, type, fold_convert (type, t1),
1308                       fold_convert (type, t2));
1309 }
1310 \f
1311 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1312    to produce a new constant.
1313
1314    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1315
1316 tree
1317 int_const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1318 {
1319   unsigned HOST_WIDE_INT int1l, int2l;
1320   HOST_WIDE_INT int1h, int2h;
1321   unsigned HOST_WIDE_INT low;
1322   HOST_WIDE_INT hi;
1323   unsigned HOST_WIDE_INT garbagel;
1324   HOST_WIDE_INT garbageh;
1325   tree t;
1326   tree type = TREE_TYPE (arg1);
1327   int uns = TYPE_UNSIGNED (type);
1328   int is_sizetype
1329     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1330   int overflow = 0;
1331
1332   int1l = TREE_INT_CST_LOW (arg1);
1333   int1h = TREE_INT_CST_HIGH (arg1);
1334   int2l = TREE_INT_CST_LOW (arg2);
1335   int2h = TREE_INT_CST_HIGH (arg2);
1336
1337   switch (code)
1338     {
1339     case BIT_IOR_EXPR:
1340       low = int1l | int2l, hi = int1h | int2h;
1341       break;
1342
1343     case BIT_XOR_EXPR:
1344       low = int1l ^ int2l, hi = int1h ^ int2h;
1345       break;
1346
1347     case BIT_AND_EXPR:
1348       low = int1l & int2l, hi = int1h & int2h;
1349       break;
1350
1351     case RSHIFT_EXPR:
1352       int2l = -int2l;
1353     case LSHIFT_EXPR:
1354       /* It's unclear from the C standard whether shifts can overflow.
1355          The following code ignores overflow; perhaps a C standard
1356          interpretation ruling is needed.  */
1357       lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1358                      &low, &hi, !uns);
1359       break;
1360
1361     case RROTATE_EXPR:
1362       int2l = - int2l;
1363     case LROTATE_EXPR:
1364       lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1365                       &low, &hi);
1366       break;
1367
1368     case PLUS_EXPR:
1369       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1370       break;
1371
1372     case MINUS_EXPR:
1373       neg_double (int2l, int2h, &low, &hi);
1374       add_double (int1l, int1h, low, hi, &low, &hi);
1375       overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1376       break;
1377
1378     case MULT_EXPR:
1379       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1380       break;
1381
1382     case TRUNC_DIV_EXPR:
1383     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1384     case EXACT_DIV_EXPR:
1385       /* This is a shortcut for a common special case.  */
1386       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1387           && ! TREE_CONSTANT_OVERFLOW (arg1)
1388           && ! TREE_CONSTANT_OVERFLOW (arg2)
1389           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1390         {
1391           if (code == CEIL_DIV_EXPR)
1392             int1l += int2l - 1;
1393
1394           low = int1l / int2l, hi = 0;
1395           break;
1396         }
1397
1398       /* ... fall through ...  */
1399
1400     case ROUND_DIV_EXPR:
1401       if (int2h == 0 && int2l == 1)
1402         {
1403           low = int1l, hi = int1h;
1404           break;
1405         }
1406       if (int1l == int2l && int1h == int2h
1407           && ! (int1l == 0 && int1h == 0))
1408         {
1409           low = 1, hi = 0;
1410           break;
1411         }
1412       overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1413                                        &low, &hi, &garbagel, &garbageh);
1414       break;
1415
1416     case TRUNC_MOD_EXPR:
1417     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1418       /* This is a shortcut for a common special case.  */
1419       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1420           && ! TREE_CONSTANT_OVERFLOW (arg1)
1421           && ! TREE_CONSTANT_OVERFLOW (arg2)
1422           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1423         {
1424           if (code == CEIL_MOD_EXPR)
1425             int1l += int2l - 1;
1426           low = int1l % int2l, hi = 0;
1427           break;
1428         }
1429
1430       /* ... fall through ...  */
1431
1432     case ROUND_MOD_EXPR:
1433       overflow = div_and_round_double (code, uns,
1434                                        int1l, int1h, int2l, int2h,
1435                                        &garbagel, &garbageh, &low, &hi);
1436       break;
1437
1438     case MIN_EXPR:
1439     case MAX_EXPR:
1440       if (uns)
1441         low = (((unsigned HOST_WIDE_INT) int1h
1442                 < (unsigned HOST_WIDE_INT) int2h)
1443                || (((unsigned HOST_WIDE_INT) int1h
1444                     == (unsigned HOST_WIDE_INT) int2h)
1445                    && int1l < int2l));
1446       else
1447         low = (int1h < int2h
1448                || (int1h == int2h && int1l < int2l));
1449
1450       if (low == (code == MIN_EXPR))
1451         low = int1l, hi = int1h;
1452       else
1453         low = int2l, hi = int2h;
1454       break;
1455
1456     default:
1457       gcc_unreachable ();
1458     }
1459
1460   t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1461
1462   if (notrunc)
1463     {
1464       /* Propagate overflow flags ourselves.  */
1465       if (((!uns || is_sizetype) && overflow)
1466           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1467         {
1468           t = copy_node (t);
1469           TREE_OVERFLOW (t) = 1;
1470           TREE_CONSTANT_OVERFLOW (t) = 1;
1471         }
1472       else if (TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2))
1473         {
1474           t = copy_node (t);
1475           TREE_CONSTANT_OVERFLOW (t) = 1;
1476         }
1477     }
1478   else
1479     t = force_fit_type (t, 1,
1480                         ((!uns || is_sizetype) && overflow)
1481                         | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2),
1482                         TREE_CONSTANT_OVERFLOW (arg1)
1483                         | TREE_CONSTANT_OVERFLOW (arg2));
1484
1485   return t;
1486 }
1487
1488 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1489    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1490    are the same kind of constant and the same machine mode.
1491
1492    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1493
1494 static tree
1495 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1496 {
1497   STRIP_NOPS (arg1);
1498   STRIP_NOPS (arg2);
1499
1500   if (TREE_CODE (arg1) == INTEGER_CST)
1501     return int_const_binop (code, arg1, arg2, notrunc);
1502
1503   if (TREE_CODE (arg1) == REAL_CST)
1504     {
1505       enum machine_mode mode;
1506       REAL_VALUE_TYPE d1;
1507       REAL_VALUE_TYPE d2;
1508       REAL_VALUE_TYPE value;
1509       REAL_VALUE_TYPE result;
1510       bool inexact;
1511       tree t, type;
1512
1513       d1 = TREE_REAL_CST (arg1);
1514       d2 = TREE_REAL_CST (arg2);
1515
1516       type = TREE_TYPE (arg1);
1517       mode = TYPE_MODE (type);
1518
1519       /* Don't perform operation if we honor signaling NaNs and
1520          either operand is a NaN.  */
1521       if (HONOR_SNANS (mode)
1522           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1523         return NULL_TREE;
1524
1525       /* Don't perform operation if it would raise a division
1526          by zero exception.  */
1527       if (code == RDIV_EXPR
1528           && REAL_VALUES_EQUAL (d2, dconst0)
1529           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1530         return NULL_TREE;
1531
1532       /* If either operand is a NaN, just return it.  Otherwise, set up
1533          for floating-point trap; we return an overflow.  */
1534       if (REAL_VALUE_ISNAN (d1))
1535         return arg1;
1536       else if (REAL_VALUE_ISNAN (d2))
1537         return arg2;
1538
1539       inexact = real_arithmetic (&value, code, &d1, &d2);
1540       real_convert (&result, mode, &value);
1541
1542       /* Don't constant fold this floating point operation if the
1543          result may dependent upon the run-time rounding mode and
1544          flag_rounding_math is set, or if GCC's software emulation
1545          is unable to accurately represent the result.  */
1546       
1547       if ((flag_rounding_math
1548            || (REAL_MODE_FORMAT_COMPOSITE_P (mode)
1549                && !flag_unsafe_math_optimizations))
1550           && (inexact || !real_identical (&result, &value)))
1551         return NULL_TREE;
1552
1553       t = build_real (type, result);
1554
1555       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1556       TREE_CONSTANT_OVERFLOW (t)
1557         = TREE_OVERFLOW (t)
1558           | TREE_CONSTANT_OVERFLOW (arg1)
1559           | TREE_CONSTANT_OVERFLOW (arg2);
1560       return t;
1561     }
1562   if (TREE_CODE (arg1) == COMPLEX_CST)
1563     {
1564       tree type = TREE_TYPE (arg1);
1565       tree r1 = TREE_REALPART (arg1);
1566       tree i1 = TREE_IMAGPART (arg1);
1567       tree r2 = TREE_REALPART (arg2);
1568       tree i2 = TREE_IMAGPART (arg2);
1569       tree t;
1570
1571       switch (code)
1572         {
1573         case PLUS_EXPR:
1574           t = build_complex (type,
1575                              const_binop (PLUS_EXPR, r1, r2, notrunc),
1576                              const_binop (PLUS_EXPR, i1, i2, notrunc));
1577           break;
1578
1579         case MINUS_EXPR:
1580           t = build_complex (type,
1581                              const_binop (MINUS_EXPR, r1, r2, notrunc),
1582                              const_binop (MINUS_EXPR, i1, i2, notrunc));
1583           break;
1584
1585         case MULT_EXPR:
1586           t = build_complex (type,
1587                              const_binop (MINUS_EXPR,
1588                                           const_binop (MULT_EXPR,
1589                                                        r1, r2, notrunc),
1590                                           const_binop (MULT_EXPR,
1591                                                        i1, i2, notrunc),
1592                                           notrunc),
1593                              const_binop (PLUS_EXPR,
1594                                           const_binop (MULT_EXPR,
1595                                                        r1, i2, notrunc),
1596                                           const_binop (MULT_EXPR,
1597                                                        i1, r2, notrunc),
1598                                           notrunc));
1599           break;
1600
1601         case RDIV_EXPR:
1602           {
1603             tree t1, t2, real, imag;
1604             tree magsquared
1605               = const_binop (PLUS_EXPR,
1606                              const_binop (MULT_EXPR, r2, r2, notrunc),
1607                              const_binop (MULT_EXPR, i2, i2, notrunc),
1608                              notrunc);
1609
1610             t1 = const_binop (PLUS_EXPR,
1611                               const_binop (MULT_EXPR, r1, r2, notrunc),
1612                               const_binop (MULT_EXPR, i1, i2, notrunc),
1613                               notrunc);
1614             t2 = const_binop (MINUS_EXPR,
1615                               const_binop (MULT_EXPR, i1, r2, notrunc),
1616                               const_binop (MULT_EXPR, r1, i2, notrunc),
1617                               notrunc);
1618
1619             if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1620               {
1621                 real = const_binop (TRUNC_DIV_EXPR, t1, magsquared, notrunc);
1622                 imag = const_binop (TRUNC_DIV_EXPR, t2, magsquared, notrunc);
1623               }
1624             else
1625               {
1626                 real = const_binop (RDIV_EXPR, t1, magsquared, notrunc);
1627                 imag = const_binop (RDIV_EXPR, t2, magsquared, notrunc);
1628                 if (!real || !imag)
1629                   return NULL_TREE;
1630               }
1631
1632             t = build_complex (type, real, imag);
1633           }
1634           break;
1635
1636         default:
1637           gcc_unreachable ();
1638         }
1639       return t;
1640     }
1641   return 0;
1642 }
1643
1644 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
1645    indicates which particular sizetype to create.  */
1646
1647 tree
1648 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1649 {
1650   return build_int_cst (sizetype_tab[(int) kind], number);
1651 }
1652 \f
1653 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
1654    is a tree code.  The type of the result is taken from the operands.
1655    Both must be the same type integer type and it must be a size type.
1656    If the operands are constant, so is the result.  */
1657
1658 tree
1659 size_binop (enum tree_code code, tree arg0, tree arg1)
1660 {
1661   tree type = TREE_TYPE (arg0);
1662
1663   gcc_assert (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
1664               && type == TREE_TYPE (arg1));
1665
1666   /* Handle the special case of two integer constants faster.  */
1667   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1668     {
1669       /* And some specific cases even faster than that.  */
1670       if (code == PLUS_EXPR && integer_zerop (arg0))
1671         return arg1;
1672       else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1673                && integer_zerop (arg1))
1674         return arg0;
1675       else if (code == MULT_EXPR && integer_onep (arg0))
1676         return arg1;
1677
1678       /* Handle general case of two integer constants.  */
1679       return int_const_binop (code, arg0, arg1, 0);
1680     }
1681
1682   if (arg0 == error_mark_node || arg1 == error_mark_node)
1683     return error_mark_node;
1684
1685   return fold_build2 (code, type, arg0, arg1);
1686 }
1687
1688 /* Given two values, either both of sizetype or both of bitsizetype,
1689    compute the difference between the two values.  Return the value
1690    in signed type corresponding to the type of the operands.  */
1691
1692 tree
1693 size_diffop (tree arg0, tree arg1)
1694 {
1695   tree type = TREE_TYPE (arg0);
1696   tree ctype;
1697
1698   gcc_assert (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
1699               && type == TREE_TYPE (arg1));
1700
1701   /* If the type is already signed, just do the simple thing.  */
1702   if (!TYPE_UNSIGNED (type))
1703     return size_binop (MINUS_EXPR, arg0, arg1);
1704
1705   ctype = type == bitsizetype ? sbitsizetype : ssizetype;
1706
1707   /* If either operand is not a constant, do the conversions to the signed
1708      type and subtract.  The hardware will do the right thing with any
1709      overflow in the subtraction.  */
1710   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1711     return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
1712                        fold_convert (ctype, arg1));
1713
1714   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1715      Otherwise, subtract the other way, convert to CTYPE (we know that can't
1716      overflow) and negate (which can't either).  Special-case a result
1717      of zero while we're here.  */
1718   if (tree_int_cst_equal (arg0, arg1))
1719     return fold_convert (ctype, integer_zero_node);
1720   else if (tree_int_cst_lt (arg1, arg0))
1721     return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
1722   else
1723     return size_binop (MINUS_EXPR, fold_convert (ctype, integer_zero_node),
1724                        fold_convert (ctype, size_binop (MINUS_EXPR,
1725                                                         arg1, arg0)));
1726 }
1727 \f
1728 /* A subroutine of fold_convert_const handling conversions of an
1729    INTEGER_CST to another integer type.  */
1730
1731 static tree
1732 fold_convert_const_int_from_int (tree type, tree arg1)
1733 {
1734   tree t;
1735
1736   /* Given an integer constant, make new constant with new type,
1737      appropriately sign-extended or truncated.  */
1738   t = build_int_cst_wide (type, TREE_INT_CST_LOW (arg1),
1739                           TREE_INT_CST_HIGH (arg1));
1740
1741   t = force_fit_type (t,
1742                       /* Don't set the overflow when
1743                          converting a pointer  */
1744                       !POINTER_TYPE_P (TREE_TYPE (arg1)),
1745                       (TREE_INT_CST_HIGH (arg1) < 0
1746                        && (TYPE_UNSIGNED (type)
1747                            < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1748                       | TREE_OVERFLOW (arg1),
1749                       TREE_CONSTANT_OVERFLOW (arg1));
1750
1751   return t;
1752 }
1753
1754 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1755    to an integer type.  */
1756
1757 static tree
1758 fold_convert_const_int_from_real (enum tree_code code, tree type, tree arg1)
1759 {
1760   int overflow = 0;
1761   tree t;
1762
1763   /* The following code implements the floating point to integer
1764      conversion rules required by the Java Language Specification,
1765      that IEEE NaNs are mapped to zero and values that overflow
1766      the target precision saturate, i.e. values greater than
1767      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1768      are mapped to INT_MIN.  These semantics are allowed by the
1769      C and C++ standards that simply state that the behavior of
1770      FP-to-integer conversion is unspecified upon overflow.  */
1771
1772   HOST_WIDE_INT high, low;
1773   REAL_VALUE_TYPE r;
1774   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1775
1776   switch (code)
1777     {
1778     case FIX_TRUNC_EXPR:
1779       real_trunc (&r, VOIDmode, &x);
1780       break;
1781
1782     case FIX_CEIL_EXPR:
1783       real_ceil (&r, VOIDmode, &x);
1784       break;
1785
1786     case FIX_FLOOR_EXPR:
1787       real_floor (&r, VOIDmode, &x);
1788       break;
1789
1790     case FIX_ROUND_EXPR:
1791       real_round (&r, VOIDmode, &x);
1792       break;
1793
1794     default:
1795       gcc_unreachable ();
1796     }
1797
1798   /* If R is NaN, return zero and show we have an overflow.  */
1799   if (REAL_VALUE_ISNAN (r))
1800     {
1801       overflow = 1;
1802       high = 0;
1803       low = 0;
1804     }
1805
1806   /* See if R is less than the lower bound or greater than the
1807      upper bound.  */
1808
1809   if (! overflow)
1810     {
1811       tree lt = TYPE_MIN_VALUE (type);
1812       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1813       if (REAL_VALUES_LESS (r, l))
1814         {
1815           overflow = 1;
1816           high = TREE_INT_CST_HIGH (lt);
1817           low = TREE_INT_CST_LOW (lt);
1818         }
1819     }
1820
1821   if (! overflow)
1822     {
1823       tree ut = TYPE_MAX_VALUE (type);
1824       if (ut)
1825         {
1826           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1827           if (REAL_VALUES_LESS (u, r))
1828             {
1829               overflow = 1;
1830               high = TREE_INT_CST_HIGH (ut);
1831               low = TREE_INT_CST_LOW (ut);
1832             }
1833         }
1834     }
1835
1836   if (! overflow)
1837     REAL_VALUE_TO_INT (&low, &high, r);
1838
1839   t = build_int_cst_wide (type, low, high);
1840
1841   t = force_fit_type (t, -1, overflow | TREE_OVERFLOW (arg1),
1842                       TREE_CONSTANT_OVERFLOW (arg1));
1843   return t;
1844 }
1845
1846 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1847    to another floating point type.  */
1848
1849 static tree
1850 fold_convert_const_real_from_real (tree type, tree arg1)
1851 {
1852   REAL_VALUE_TYPE value;
1853   tree t;
1854
1855   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1856   t = build_real (type, value);
1857
1858   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1859   TREE_CONSTANT_OVERFLOW (t)
1860     = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1861   return t;
1862 }
1863
1864 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1865    type TYPE.  If no simplification can be done return NULL_TREE.  */
1866
1867 static tree
1868 fold_convert_const (enum tree_code code, tree type, tree arg1)
1869 {
1870   if (TREE_TYPE (arg1) == type)
1871     return arg1;
1872
1873   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
1874     {
1875       if (TREE_CODE (arg1) == INTEGER_CST)
1876         return fold_convert_const_int_from_int (type, arg1);
1877       else if (TREE_CODE (arg1) == REAL_CST)
1878         return fold_convert_const_int_from_real (code, type, arg1);
1879     }
1880   else if (TREE_CODE (type) == REAL_TYPE)
1881     {
1882       if (TREE_CODE (arg1) == INTEGER_CST)
1883         return build_real_from_int_cst (type, arg1);
1884       if (TREE_CODE (arg1) == REAL_CST)
1885         return fold_convert_const_real_from_real (type, arg1);
1886     }
1887   return NULL_TREE;
1888 }
1889
1890 /* Construct a vector of zero elements of vector type TYPE.  */
1891
1892 static tree
1893 build_zero_vector (tree type)
1894 {
1895   tree elem, list;
1896   int i, units;
1897
1898   elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1899   units = TYPE_VECTOR_SUBPARTS (type);
1900   
1901   list = NULL_TREE;
1902   for (i = 0; i < units; i++)
1903     list = tree_cons (NULL_TREE, elem, list);
1904   return build_vector (type, list);
1905 }
1906
1907 /* Convert expression ARG to type TYPE.  Used by the middle-end for
1908    simple conversions in preference to calling the front-end's convert.  */
1909
1910 tree
1911 fold_convert (tree type, tree arg)
1912 {
1913   tree orig = TREE_TYPE (arg);
1914   tree tem;
1915
1916   if (type == orig)
1917     return arg;
1918
1919   if (TREE_CODE (arg) == ERROR_MARK
1920       || TREE_CODE (type) == ERROR_MARK
1921       || TREE_CODE (orig) == ERROR_MARK)
1922     return error_mark_node;
1923
1924   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig)
1925       || lang_hooks.types_compatible_p (TYPE_MAIN_VARIANT (type),
1926                                         TYPE_MAIN_VARIANT (orig)))
1927     return fold_build1 (NOP_EXPR, type, arg);
1928
1929   switch (TREE_CODE (type))
1930     {
1931     case INTEGER_TYPE: case CHAR_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1932     case POINTER_TYPE: case REFERENCE_TYPE:
1933     case OFFSET_TYPE:
1934       if (TREE_CODE (arg) == INTEGER_CST)
1935         {
1936           tem = fold_convert_const (NOP_EXPR, type, arg);
1937           if (tem != NULL_TREE)
1938             return tem;
1939         }
1940       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1941           || TREE_CODE (orig) == OFFSET_TYPE)
1942         return fold_build1 (NOP_EXPR, type, arg);
1943       if (TREE_CODE (orig) == COMPLEX_TYPE)
1944         {
1945           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
1946           return fold_convert (type, tem);
1947         }
1948       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
1949                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1950       return fold_build1 (NOP_EXPR, type, arg);
1951
1952     case REAL_TYPE:
1953       if (TREE_CODE (arg) == INTEGER_CST)
1954         {
1955           tem = fold_convert_const (FLOAT_EXPR, type, arg);
1956           if (tem != NULL_TREE)
1957             return tem;
1958         }
1959       else if (TREE_CODE (arg) == REAL_CST)
1960         {
1961           tem = fold_convert_const (NOP_EXPR, type, arg);
1962           if (tem != NULL_TREE)
1963             return tem;
1964         }
1965
1966       switch (TREE_CODE (orig))
1967         {
1968         case INTEGER_TYPE: case CHAR_TYPE:
1969         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1970         case POINTER_TYPE: case REFERENCE_TYPE:
1971           return fold_build1 (FLOAT_EXPR, type, arg);
1972
1973         case REAL_TYPE:
1974           return fold_build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
1975                               type, arg);
1976
1977         case COMPLEX_TYPE:
1978           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
1979           return fold_convert (type, tem);
1980
1981         default:
1982           gcc_unreachable ();
1983         }
1984
1985     case COMPLEX_TYPE:
1986       switch (TREE_CODE (orig))
1987         {
1988         case INTEGER_TYPE: case CHAR_TYPE:
1989         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1990         case POINTER_TYPE: case REFERENCE_TYPE:
1991         case REAL_TYPE:
1992           return build2 (COMPLEX_EXPR, type,
1993                          fold_convert (TREE_TYPE (type), arg),
1994                          fold_convert (TREE_TYPE (type), integer_zero_node));
1995         case COMPLEX_TYPE:
1996           {
1997             tree rpart, ipart;
1998
1999             if (TREE_CODE (arg) == COMPLEX_EXPR)
2000               {
2001                 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2002                 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2003                 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2004               }
2005
2006             arg = save_expr (arg);
2007             rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2008             ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2009             rpart = fold_convert (TREE_TYPE (type), rpart);
2010             ipart = fold_convert (TREE_TYPE (type), ipart);
2011             return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2012           }
2013
2014         default:
2015           gcc_unreachable ();
2016         }
2017
2018     case VECTOR_TYPE:
2019       if (integer_zerop (arg))
2020         return build_zero_vector (type);
2021       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2022       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2023                   || TREE_CODE (orig) == VECTOR_TYPE);
2024       return fold_build1 (NOP_EXPR, type, arg);
2025
2026     case VOID_TYPE:
2027       return fold_build1 (CONVERT_EXPR, type, fold_ignored_result (arg));
2028
2029     default:
2030       gcc_unreachable ();
2031     }
2032 }
2033 \f
2034 /* Return false if expr can be assumed not to be an value, true
2035    otherwise.  */
2036
2037 static bool
2038 maybe_lvalue_p (tree x)
2039 {
2040   /* We only need to wrap lvalue tree codes.  */
2041   switch (TREE_CODE (x))
2042   {
2043   case VAR_DECL:
2044   case PARM_DECL:
2045   case RESULT_DECL:
2046   case LABEL_DECL:
2047   case FUNCTION_DECL:
2048   case SSA_NAME:
2049
2050   case COMPONENT_REF:
2051   case INDIRECT_REF:
2052   case ALIGN_INDIRECT_REF:
2053   case MISALIGNED_INDIRECT_REF:
2054   case ARRAY_REF:
2055   case ARRAY_RANGE_REF:
2056   case BIT_FIELD_REF:
2057   case OBJ_TYPE_REF:
2058
2059   case REALPART_EXPR:
2060   case IMAGPART_EXPR:
2061   case PREINCREMENT_EXPR:
2062   case PREDECREMENT_EXPR:
2063   case SAVE_EXPR:
2064   case TRY_CATCH_EXPR:
2065   case WITH_CLEANUP_EXPR:
2066   case COMPOUND_EXPR:
2067   case MODIFY_EXPR:
2068   case TARGET_EXPR:
2069   case COND_EXPR:
2070   case BIND_EXPR:
2071   case MIN_EXPR:
2072   case MAX_EXPR:
2073     break;
2074
2075   default:
2076     /* Assume the worst for front-end tree codes.  */
2077     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2078       break;
2079     return false;
2080   }
2081
2082   return true;
2083 }
2084
2085 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2086
2087 tree
2088 non_lvalue (tree x)
2089 {
2090   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2091      us.  */
2092   if (in_gimple_form)
2093     return x;
2094
2095   if (! maybe_lvalue_p (x))
2096     return x;
2097   return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2098 }
2099
2100 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2101    Zero means allow extended lvalues.  */
2102
2103 int pedantic_lvalues;
2104
2105 /* When pedantic, return an expr equal to X but certainly not valid as a
2106    pedantic lvalue.  Otherwise, return X.  */
2107
2108 static tree
2109 pedantic_non_lvalue (tree x)
2110 {
2111   if (pedantic_lvalues)
2112     return non_lvalue (x);
2113   else
2114     return x;
2115 }
2116 \f
2117 /* Given a tree comparison code, return the code that is the logical inverse
2118    of the given code.  It is not safe to do this for floating-point
2119    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2120    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2121
2122 static enum tree_code
2123 invert_tree_comparison (enum tree_code code, bool honor_nans)
2124 {
2125   if (honor_nans && flag_trapping_math)
2126     return ERROR_MARK;
2127
2128   switch (code)
2129     {
2130     case EQ_EXPR:
2131       return NE_EXPR;
2132     case NE_EXPR:
2133       return EQ_EXPR;
2134     case GT_EXPR:
2135       return honor_nans ? UNLE_EXPR : LE_EXPR;
2136     case GE_EXPR:
2137       return honor_nans ? UNLT_EXPR : LT_EXPR;
2138     case LT_EXPR:
2139       return honor_nans ? UNGE_EXPR : GE_EXPR;
2140     case LE_EXPR:
2141       return honor_nans ? UNGT_EXPR : GT_EXPR;
2142     case LTGT_EXPR:
2143       return UNEQ_EXPR;
2144     case UNEQ_EXPR:
2145       return LTGT_EXPR;
2146     case UNGT_EXPR:
2147       return LE_EXPR;
2148     case UNGE_EXPR:
2149       return LT_EXPR;
2150     case UNLT_EXPR:
2151       return GE_EXPR;
2152     case UNLE_EXPR:
2153       return GT_EXPR;
2154     case ORDERED_EXPR:
2155       return UNORDERED_EXPR;
2156     case UNORDERED_EXPR:
2157       return ORDERED_EXPR;
2158     default:
2159       gcc_unreachable ();
2160     }
2161 }
2162
2163 /* Similar, but return the comparison that results if the operands are
2164    swapped.  This is safe for floating-point.  */
2165
2166 enum tree_code
2167 swap_tree_comparison (enum tree_code code)
2168 {
2169   switch (code)
2170     {
2171     case EQ_EXPR:
2172     case NE_EXPR:
2173       return code;
2174     case GT_EXPR:
2175       return LT_EXPR;
2176     case GE_EXPR:
2177       return LE_EXPR;
2178     case LT_EXPR:
2179       return GT_EXPR;
2180     case LE_EXPR:
2181       return GE_EXPR;
2182     default:
2183       gcc_unreachable ();
2184     }
2185 }
2186
2187
2188 /* Convert a comparison tree code from an enum tree_code representation
2189    into a compcode bit-based encoding.  This function is the inverse of
2190    compcode_to_comparison.  */
2191
2192 static enum comparison_code
2193 comparison_to_compcode (enum tree_code code)
2194 {
2195   switch (code)
2196     {
2197     case LT_EXPR:
2198       return COMPCODE_LT;
2199     case EQ_EXPR:
2200       return COMPCODE_EQ;
2201     case LE_EXPR:
2202       return COMPCODE_LE;
2203     case GT_EXPR:
2204       return COMPCODE_GT;
2205     case NE_EXPR:
2206       return COMPCODE_NE;
2207     case GE_EXPR:
2208       return COMPCODE_GE;
2209     case ORDERED_EXPR:
2210       return COMPCODE_ORD;
2211     case UNORDERED_EXPR:
2212       return COMPCODE_UNORD;
2213     case UNLT_EXPR:
2214       return COMPCODE_UNLT;
2215     case UNEQ_EXPR:
2216       return COMPCODE_UNEQ;
2217     case UNLE_EXPR:
2218       return COMPCODE_UNLE;
2219     case UNGT_EXPR:
2220       return COMPCODE_UNGT;
2221     case LTGT_EXPR:
2222       return COMPCODE_LTGT;
2223     case UNGE_EXPR:
2224       return COMPCODE_UNGE;
2225     default:
2226       gcc_unreachable ();
2227     }
2228 }
2229
2230 /* Convert a compcode bit-based encoding of a comparison operator back
2231    to GCC's enum tree_code representation.  This function is the
2232    inverse of comparison_to_compcode.  */
2233
2234 static enum tree_code
2235 compcode_to_comparison (enum comparison_code code)
2236 {
2237   switch (code)
2238     {
2239     case COMPCODE_LT:
2240       return LT_EXPR;
2241     case COMPCODE_EQ:
2242       return EQ_EXPR;
2243     case COMPCODE_LE:
2244       return LE_EXPR;
2245     case COMPCODE_GT:
2246       return GT_EXPR;
2247     case COMPCODE_NE:
2248       return NE_EXPR;
2249     case COMPCODE_GE:
2250       return GE_EXPR;
2251     case COMPCODE_ORD:
2252       return ORDERED_EXPR;
2253     case COMPCODE_UNORD:
2254       return UNORDERED_EXPR;
2255     case COMPCODE_UNLT:
2256       return UNLT_EXPR;
2257     case COMPCODE_UNEQ:
2258       return UNEQ_EXPR;
2259     case COMPCODE_UNLE:
2260       return UNLE_EXPR;
2261     case COMPCODE_UNGT:
2262       return UNGT_EXPR;
2263     case COMPCODE_LTGT:
2264       return LTGT_EXPR;
2265     case COMPCODE_UNGE:
2266       return UNGE_EXPR;
2267     default:
2268       gcc_unreachable ();
2269     }
2270 }
2271
2272 /* Return a tree for the comparison which is the combination of
2273    doing the AND or OR (depending on CODE) of the two operations LCODE
2274    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2275    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2276    if this makes the transformation invalid.  */
2277
2278 tree
2279 combine_comparisons (enum tree_code code, enum tree_code lcode,
2280                      enum tree_code rcode, tree truth_type,
2281                      tree ll_arg, tree lr_arg)
2282 {
2283   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2284   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2285   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2286   enum comparison_code compcode;
2287
2288   switch (code)
2289     {
2290     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2291       compcode = lcompcode & rcompcode;
2292       break;
2293
2294     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2295       compcode = lcompcode | rcompcode;
2296       break;
2297
2298     default:
2299       return NULL_TREE;
2300     }
2301
2302   if (!honor_nans)
2303     {
2304       /* Eliminate unordered comparisons, as well as LTGT and ORD
2305          which are not used unless the mode has NaNs.  */
2306       compcode &= ~COMPCODE_UNORD;
2307       if (compcode == COMPCODE_LTGT)
2308         compcode = COMPCODE_NE;
2309       else if (compcode == COMPCODE_ORD)
2310         compcode = COMPCODE_TRUE;
2311     }
2312    else if (flag_trapping_math)
2313      {
2314         /* Check that the original operation and the optimized ones will trap
2315            under the same condition.  */
2316         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2317                      && (lcompcode != COMPCODE_EQ)
2318                      && (lcompcode != COMPCODE_ORD);
2319         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2320                      && (rcompcode != COMPCODE_EQ)
2321                      && (rcompcode != COMPCODE_ORD);
2322         bool trap = (compcode & COMPCODE_UNORD) == 0
2323                     && (compcode != COMPCODE_EQ)
2324                     && (compcode != COMPCODE_ORD);
2325
2326         /* In a short-circuited boolean expression the LHS might be
2327            such that the RHS, if evaluated, will never trap.  For
2328            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2329            if neither x nor y is NaN.  (This is a mixed blessing: for
2330            example, the expression above will never trap, hence
2331            optimizing it to x < y would be invalid).  */
2332         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2333             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2334           rtrap = false;
2335
2336         /* If the comparison was short-circuited, and only the RHS
2337            trapped, we may now generate a spurious trap.  */
2338         if (rtrap && !ltrap
2339             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2340           return NULL_TREE;
2341
2342         /* If we changed the conditions that cause a trap, we lose.  */
2343         if ((ltrap || rtrap) != trap)
2344           return NULL_TREE;
2345       }
2346
2347   if (compcode == COMPCODE_TRUE)
2348     return constant_boolean_node (true, truth_type);
2349   else if (compcode == COMPCODE_FALSE)
2350     return constant_boolean_node (false, truth_type);
2351   else
2352     return fold_build2 (compcode_to_comparison (compcode),
2353                         truth_type, ll_arg, lr_arg);
2354 }
2355
2356 /* Return nonzero if CODE is a tree code that represents a truth value.  */
2357
2358 static int
2359 truth_value_p (enum tree_code code)
2360 {
2361   return (TREE_CODE_CLASS (code) == tcc_comparison
2362           || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2363           || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2364           || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2365 }
2366 \f
2367 /* Return nonzero if two operands (typically of the same tree node)
2368    are necessarily equal.  If either argument has side-effects this
2369    function returns zero.  FLAGS modifies behavior as follows:
2370
2371    If OEP_ONLY_CONST is set, only return nonzero for constants.
2372    This function tests whether the operands are indistinguishable;
2373    it does not test whether they are equal using C's == operation.
2374    The distinction is important for IEEE floating point, because
2375    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2376    (2) two NaNs may be indistinguishable, but NaN!=NaN.
2377
2378    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2379    even though it may hold multiple values during a function.
2380    This is because a GCC tree node guarantees that nothing else is
2381    executed between the evaluation of its "operands" (which may often
2382    be evaluated in arbitrary order).  Hence if the operands themselves
2383    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2384    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
2385    unset means assuming isochronic (or instantaneous) tree equivalence.
2386    Unless comparing arbitrary expression trees, such as from different
2387    statements, this flag can usually be left unset.
2388
2389    If OEP_PURE_SAME is set, then pure functions with identical arguments
2390    are considered the same.  It is used when the caller has other ways
2391    to ensure that global memory is unchanged in between.  */
2392
2393 int
2394 operand_equal_p (tree arg0, tree arg1, unsigned int flags)
2395 {
2396   /* If either is ERROR_MARK, they aren't equal.  */
2397   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
2398     return 0;
2399
2400   /* If both types don't have the same signedness, then we can't consider
2401      them equal.  We must check this before the STRIP_NOPS calls
2402      because they may change the signedness of the arguments.  */
2403   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2404     return 0;
2405
2406   STRIP_NOPS (arg0);
2407   STRIP_NOPS (arg1);
2408
2409   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2410       /* This is needed for conversions and for COMPONENT_REF.
2411          Might as well play it safe and always test this.  */
2412       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2413       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2414       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2415     return 0;
2416
2417   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2418      We don't care about side effects in that case because the SAVE_EXPR
2419      takes care of that for us. In all other cases, two expressions are
2420      equal if they have no side effects.  If we have two identical
2421      expressions with side effects that should be treated the same due
2422      to the only side effects being identical SAVE_EXPR's, that will
2423      be detected in the recursive calls below.  */
2424   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2425       && (TREE_CODE (arg0) == SAVE_EXPR
2426           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2427     return 1;
2428
2429   /* Next handle constant cases, those for which we can return 1 even
2430      if ONLY_CONST is set.  */
2431   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2432     switch (TREE_CODE (arg0))
2433       {
2434       case INTEGER_CST:
2435         return (! TREE_CONSTANT_OVERFLOW (arg0)
2436                 && ! TREE_CONSTANT_OVERFLOW (arg1)
2437                 && tree_int_cst_equal (arg0, arg1));
2438
2439       case REAL_CST:
2440         return (! TREE_CONSTANT_OVERFLOW (arg0)
2441                 && ! TREE_CONSTANT_OVERFLOW (arg1)
2442                 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2443                                           TREE_REAL_CST (arg1)));
2444
2445       case VECTOR_CST:
2446         {
2447           tree v1, v2;
2448
2449           if (TREE_CONSTANT_OVERFLOW (arg0)
2450               || TREE_CONSTANT_OVERFLOW (arg1))
2451             return 0;
2452
2453           v1 = TREE_VECTOR_CST_ELTS (arg0);
2454           v2 = TREE_VECTOR_CST_ELTS (arg1);
2455           while (v1 && v2)
2456             {
2457               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2458                                     flags))
2459                 return 0;
2460               v1 = TREE_CHAIN (v1);
2461               v2 = TREE_CHAIN (v2);
2462             }
2463
2464           return 1;
2465         }
2466
2467       case COMPLEX_CST:
2468         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2469                                  flags)
2470                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2471                                     flags));
2472
2473       case STRING_CST:
2474         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2475                 && ! memcmp (TREE_STRING_POINTER (arg0),
2476                               TREE_STRING_POINTER (arg1),
2477                               TREE_STRING_LENGTH (arg0)));
2478
2479       case ADDR_EXPR:
2480         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2481                                 0);
2482       default:
2483         break;
2484       }
2485
2486   if (flags & OEP_ONLY_CONST)
2487     return 0;
2488
2489 /* Define macros to test an operand from arg0 and arg1 for equality and a
2490    variant that allows null and views null as being different from any
2491    non-null value.  In the latter case, if either is null, the both
2492    must be; otherwise, do the normal comparison.  */
2493 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
2494                                     TREE_OPERAND (arg1, N), flags)
2495
2496 #define OP_SAME_WITH_NULL(N)                            \
2497   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2498    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2499
2500   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2501     {
2502     case tcc_unary:
2503       /* Two conversions are equal only if signedness and modes match.  */
2504       switch (TREE_CODE (arg0))
2505         {
2506         case NOP_EXPR:
2507         case CONVERT_EXPR:
2508         case FIX_CEIL_EXPR:
2509         case FIX_TRUNC_EXPR:
2510         case FIX_FLOOR_EXPR:
2511         case FIX_ROUND_EXPR:
2512           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2513               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2514             return 0;
2515           break;
2516         default:
2517           break;
2518         }
2519
2520       return OP_SAME (0);
2521
2522
2523     case tcc_comparison:
2524     case tcc_binary:
2525       if (OP_SAME (0) && OP_SAME (1))
2526         return 1;
2527
2528       /* For commutative ops, allow the other order.  */
2529       return (commutative_tree_code (TREE_CODE (arg0))
2530               && operand_equal_p (TREE_OPERAND (arg0, 0),
2531                                   TREE_OPERAND (arg1, 1), flags)
2532               && operand_equal_p (TREE_OPERAND (arg0, 1),
2533                                   TREE_OPERAND (arg1, 0), flags));
2534
2535     case tcc_reference:
2536       /* If either of the pointer (or reference) expressions we are
2537          dereferencing contain a side effect, these cannot be equal.  */
2538       if (TREE_SIDE_EFFECTS (arg0)
2539           || TREE_SIDE_EFFECTS (arg1))
2540         return 0;
2541
2542       switch (TREE_CODE (arg0))
2543         {
2544         case INDIRECT_REF:
2545         case ALIGN_INDIRECT_REF:
2546         case MISALIGNED_INDIRECT_REF:
2547         case REALPART_EXPR:
2548         case IMAGPART_EXPR:
2549           return OP_SAME (0);
2550
2551         case ARRAY_REF:
2552         case ARRAY_RANGE_REF:
2553           /* Operands 2 and 3 may be null.  */
2554           return (OP_SAME (0)
2555                   && OP_SAME (1)
2556                   && OP_SAME_WITH_NULL (2)
2557                   && OP_SAME_WITH_NULL (3));
2558
2559         case COMPONENT_REF:
2560           /* Handle operand 2 the same as for ARRAY_REF.  */
2561           return OP_SAME (0) && OP_SAME (1) && OP_SAME_WITH_NULL (2);
2562
2563         case BIT_FIELD_REF:
2564           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2565
2566         default:
2567           return 0;
2568         }
2569
2570     case tcc_expression:
2571       switch (TREE_CODE (arg0))
2572         {
2573         case ADDR_EXPR:
2574         case TRUTH_NOT_EXPR:
2575           return OP_SAME (0);
2576
2577         case TRUTH_ANDIF_EXPR:
2578         case TRUTH_ORIF_EXPR:
2579           return OP_SAME (0) && OP_SAME (1);
2580
2581         case TRUTH_AND_EXPR:
2582         case TRUTH_OR_EXPR:
2583         case TRUTH_XOR_EXPR:
2584           if (OP_SAME (0) && OP_SAME (1))
2585             return 1;
2586
2587           /* Otherwise take into account this is a commutative operation.  */
2588           return (operand_equal_p (TREE_OPERAND (arg0, 0),
2589                                    TREE_OPERAND (arg1, 1), flags)
2590                   && operand_equal_p (TREE_OPERAND (arg0, 1),
2591                                       TREE_OPERAND (arg1, 0), flags));
2592
2593         case CALL_EXPR:
2594           /* If the CALL_EXPRs call different functions, then they
2595              clearly can not be equal.  */
2596           if (!OP_SAME (0))
2597             return 0;
2598
2599           {
2600             unsigned int cef = call_expr_flags (arg0);
2601             if (flags & OEP_PURE_SAME)
2602               cef &= ECF_CONST | ECF_PURE;
2603             else
2604               cef &= ECF_CONST;
2605             if (!cef)
2606               return 0;
2607           }
2608
2609           /* Now see if all the arguments are the same.  operand_equal_p
2610              does not handle TREE_LIST, so we walk the operands here
2611              feeding them to operand_equal_p.  */
2612           arg0 = TREE_OPERAND (arg0, 1);
2613           arg1 = TREE_OPERAND (arg1, 1);
2614           while (arg0 && arg1)
2615             {
2616               if (! operand_equal_p (TREE_VALUE (arg0), TREE_VALUE (arg1),
2617                                      flags))
2618                 return 0;
2619
2620               arg0 = TREE_CHAIN (arg0);
2621               arg1 = TREE_CHAIN (arg1);
2622             }
2623
2624           /* If we get here and both argument lists are exhausted
2625              then the CALL_EXPRs are equal.  */
2626           return ! (arg0 || arg1);
2627
2628         default:
2629           return 0;
2630         }
2631
2632     case tcc_declaration:
2633       /* Consider __builtin_sqrt equal to sqrt.  */
2634       return (TREE_CODE (arg0) == FUNCTION_DECL
2635               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2636               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2637               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2638
2639     default:
2640       return 0;
2641     }
2642
2643 #undef OP_SAME
2644 #undef OP_SAME_WITH_NULL
2645 }
2646 \f
2647 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2648    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2649
2650    When in doubt, return 0.  */
2651
2652 static int
2653 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2654 {
2655   int unsignedp1, unsignedpo;
2656   tree primarg0, primarg1, primother;
2657   unsigned int correct_width;
2658
2659   if (operand_equal_p (arg0, arg1, 0))
2660     return 1;
2661
2662   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2663       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2664     return 0;
2665
2666   /* Discard any conversions that don't change the modes of ARG0 and ARG1
2667      and see if the inner values are the same.  This removes any
2668      signedness comparison, which doesn't matter here.  */
2669   primarg0 = arg0, primarg1 = arg1;
2670   STRIP_NOPS (primarg0);
2671   STRIP_NOPS (primarg1);
2672   if (operand_equal_p (primarg0, primarg1, 0))
2673     return 1;
2674
2675   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2676      actual comparison operand, ARG0.
2677
2678      First throw away any conversions to wider types
2679      already present in the operands.  */
2680
2681   primarg1 = get_narrower (arg1, &unsignedp1);
2682   primother = get_narrower (other, &unsignedpo);
2683
2684   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2685   if (unsignedp1 == unsignedpo
2686       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2687       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2688     {
2689       tree type = TREE_TYPE (arg0);
2690
2691       /* Make sure shorter operand is extended the right way
2692          to match the longer operand.  */
2693       primarg1 = fold_convert (lang_hooks.types.signed_or_unsigned_type
2694                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2695
2696       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2697         return 1;
2698     }
2699
2700   return 0;
2701 }
2702 \f
2703 /* See if ARG is an expression that is either a comparison or is performing
2704    arithmetic on comparisons.  The comparisons must only be comparing
2705    two different values, which will be stored in *CVAL1 and *CVAL2; if
2706    they are nonzero it means that some operands have already been found.
2707    No variables may be used anywhere else in the expression except in the
2708    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
2709    the expression and save_expr needs to be called with CVAL1 and CVAL2.
2710
2711    If this is true, return 1.  Otherwise, return zero.  */
2712
2713 static int
2714 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2715 {
2716   enum tree_code code = TREE_CODE (arg);
2717   enum tree_code_class class = TREE_CODE_CLASS (code);
2718
2719   /* We can handle some of the tcc_expression cases here.  */
2720   if (class == tcc_expression && code == TRUTH_NOT_EXPR)
2721     class = tcc_unary;
2722   else if (class == tcc_expression
2723            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2724                || code == COMPOUND_EXPR))
2725     class = tcc_binary;
2726
2727   else if (class == tcc_expression && code == SAVE_EXPR
2728            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2729     {
2730       /* If we've already found a CVAL1 or CVAL2, this expression is
2731          two complex to handle.  */
2732       if (*cval1 || *cval2)
2733         return 0;
2734
2735       class = tcc_unary;
2736       *save_p = 1;
2737     }
2738
2739   switch (class)
2740     {
2741     case tcc_unary:
2742       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2743
2744     case tcc_binary:
2745       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2746               && twoval_comparison_p (TREE_OPERAND (arg, 1),
2747                                       cval1, cval2, save_p));
2748
2749     case tcc_constant:
2750       return 1;
2751
2752     case tcc_expression:
2753       if (code == COND_EXPR)
2754         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2755                                      cval1, cval2, save_p)
2756                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2757                                         cval1, cval2, save_p)
2758                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2759                                         cval1, cval2, save_p));
2760       return 0;
2761
2762     case tcc_comparison:
2763       /* First see if we can handle the first operand, then the second.  For
2764          the second operand, we know *CVAL1 can't be zero.  It must be that
2765          one side of the comparison is each of the values; test for the
2766          case where this isn't true by failing if the two operands
2767          are the same.  */
2768
2769       if (operand_equal_p (TREE_OPERAND (arg, 0),
2770                            TREE_OPERAND (arg, 1), 0))
2771         return 0;
2772
2773       if (*cval1 == 0)
2774         *cval1 = TREE_OPERAND (arg, 0);
2775       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2776         ;
2777       else if (*cval2 == 0)
2778         *cval2 = TREE_OPERAND (arg, 0);
2779       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2780         ;
2781       else
2782         return 0;
2783
2784       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2785         ;
2786       else if (*cval2 == 0)
2787         *cval2 = TREE_OPERAND (arg, 1);
2788       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2789         ;
2790       else
2791         return 0;
2792
2793       return 1;
2794
2795     default:
2796       return 0;
2797     }
2798 }
2799 \f
2800 /* ARG is a tree that is known to contain just arithmetic operations and
2801    comparisons.  Evaluate the operations in the tree substituting NEW0 for
2802    any occurrence of OLD0 as an operand of a comparison and likewise for
2803    NEW1 and OLD1.  */
2804
2805 static tree
2806 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
2807 {
2808   tree type = TREE_TYPE (arg);
2809   enum tree_code code = TREE_CODE (arg);
2810   enum tree_code_class class = TREE_CODE_CLASS (code);
2811
2812   /* We can handle some of the tcc_expression cases here.  */
2813   if (class == tcc_expression && code == TRUTH_NOT_EXPR)
2814     class = tcc_unary;
2815   else if (class == tcc_expression
2816            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2817     class = tcc_binary;
2818
2819   switch (class)
2820     {
2821     case tcc_unary:
2822       return fold_build1 (code, type,
2823                           eval_subst (TREE_OPERAND (arg, 0),
2824                                       old0, new0, old1, new1));
2825
2826     case tcc_binary:
2827       return fold_build2 (code, type,
2828                           eval_subst (TREE_OPERAND (arg, 0),
2829                                       old0, new0, old1, new1),
2830                           eval_subst (TREE_OPERAND (arg, 1),
2831                                       old0, new0, old1, new1));
2832
2833     case tcc_expression:
2834       switch (code)
2835         {
2836         case SAVE_EXPR:
2837           return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2838
2839         case COMPOUND_EXPR:
2840           return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2841
2842         case COND_EXPR:
2843           return fold_build3 (code, type,
2844                               eval_subst (TREE_OPERAND (arg, 0),
2845                                           old0, new0, old1, new1),
2846                               eval_subst (TREE_OPERAND (arg, 1),
2847                                           old0, new0, old1, new1),
2848                               eval_subst (TREE_OPERAND (arg, 2),
2849                                           old0, new0, old1, new1));
2850         default:
2851           break;
2852         }
2853       /* Fall through - ???  */
2854
2855     case tcc_comparison:
2856       {
2857         tree arg0 = TREE_OPERAND (arg, 0);
2858         tree arg1 = TREE_OPERAND (arg, 1);
2859
2860         /* We need to check both for exact equality and tree equality.  The
2861            former will be true if the operand has a side-effect.  In that
2862            case, we know the operand occurred exactly once.  */
2863
2864         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2865           arg0 = new0;
2866         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2867           arg0 = new1;
2868
2869         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2870           arg1 = new0;
2871         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2872           arg1 = new1;
2873
2874         return fold_build2 (code, type, arg0, arg1);
2875       }
2876
2877     default:
2878       return arg;
2879     }
2880 }
2881 \f
2882 /* Return a tree for the case when the result of an expression is RESULT
2883    converted to TYPE and OMITTED was previously an operand of the expression
2884    but is now not needed (e.g., we folded OMITTED * 0).
2885
2886    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
2887    the conversion of RESULT to TYPE.  */
2888
2889 tree
2890 omit_one_operand (tree type, tree result, tree omitted)
2891 {
2892   tree t = fold_convert (type, result);
2893
2894   if (TREE_SIDE_EFFECTS (omitted))
2895     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
2896
2897   return non_lvalue (t);
2898 }
2899
2900 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
2901
2902 static tree
2903 pedantic_omit_one_operand (tree type, tree result, tree omitted)
2904 {
2905   tree t = fold_convert (type, result);
2906
2907   if (TREE_SIDE_EFFECTS (omitted))
2908     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
2909
2910   return pedantic_non_lvalue (t);
2911 }
2912
2913 /* Return a tree for the case when the result of an expression is RESULT
2914    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
2915    of the expression but are now not needed.
2916
2917    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
2918    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
2919    evaluated before OMITTED2.  Otherwise, if neither has side effects,
2920    just do the conversion of RESULT to TYPE.  */
2921
2922 tree
2923 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
2924 {
2925   tree t = fold_convert (type, result);
2926
2927   if (TREE_SIDE_EFFECTS (omitted2))
2928     t = build2 (COMPOUND_EXPR, type, omitted2, t);
2929   if (TREE_SIDE_EFFECTS (omitted1))
2930     t = build2 (COMPOUND_EXPR, type, omitted1, t);
2931
2932   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
2933 }
2934
2935 \f
2936 /* Return a simplified tree node for the truth-negation of ARG.  This
2937    never alters ARG itself.  We assume that ARG is an operation that
2938    returns a truth value (0 or 1).
2939
2940    FIXME: one would think we would fold the result, but it causes
2941    problems with the dominator optimizer.  */
2942 tree
2943 invert_truthvalue (tree arg)
2944 {
2945   tree type = TREE_TYPE (arg);
2946   enum tree_code code = TREE_CODE (arg);
2947
2948   if (code == ERROR_MARK)
2949     return arg;
2950
2951   /* If this is a comparison, we can simply invert it, except for
2952      floating-point non-equality comparisons, in which case we just
2953      enclose a TRUTH_NOT_EXPR around what we have.  */
2954
2955   if (TREE_CODE_CLASS (code) == tcc_comparison)
2956     {
2957       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
2958       if (FLOAT_TYPE_P (op_type)
2959           && flag_trapping_math
2960           && code != ORDERED_EXPR && code != UNORDERED_EXPR
2961           && code != NE_EXPR && code != EQ_EXPR)
2962         return build1 (TRUTH_NOT_EXPR, type, arg);
2963       else
2964         {
2965           code = invert_tree_comparison (code,
2966                                          HONOR_NANS (TYPE_MODE (op_type)));
2967           if (code == ERROR_MARK)
2968             return build1 (TRUTH_NOT_EXPR, type, arg);
2969           else
2970             return build2 (code, type,
2971                            TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2972         }
2973     }
2974
2975   switch (code)
2976     {
2977     case INTEGER_CST:
2978       return constant_boolean_node (integer_zerop (arg), type);
2979
2980     case TRUTH_AND_EXPR:
2981       return build2 (TRUTH_OR_EXPR, type,
2982                      invert_truthvalue (TREE_OPERAND (arg, 0)),
2983                      invert_truthvalue (TREE_OPERAND (arg, 1)));
2984
2985     case TRUTH_OR_EXPR:
2986       return build2 (TRUTH_AND_EXPR, type,
2987                      invert_truthvalue (TREE_OPERAND (arg, 0)),
2988                      invert_truthvalue (TREE_OPERAND (arg, 1)));
2989
2990     case TRUTH_XOR_EXPR:
2991       /* Here we can invert either operand.  We invert the first operand
2992          unless the second operand is a TRUTH_NOT_EXPR in which case our
2993          result is the XOR of the first operand with the inside of the
2994          negation of the second operand.  */
2995
2996       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2997         return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2998                        TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2999       else
3000         return build2 (TRUTH_XOR_EXPR, type,
3001                        invert_truthvalue (TREE_OPERAND (arg, 0)),
3002                        TREE_OPERAND (arg, 1));
3003
3004     case TRUTH_ANDIF_EXPR:
3005       return build2 (TRUTH_ORIF_EXPR, type,
3006                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3007                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3008
3009     case TRUTH_ORIF_EXPR:
3010       return build2 (TRUTH_ANDIF_EXPR, type,
3011                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3012                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3013
3014     case TRUTH_NOT_EXPR:
3015       return TREE_OPERAND (arg, 0);
3016
3017     case COND_EXPR:
3018       return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3019                      invert_truthvalue (TREE_OPERAND (arg, 1)),
3020                      invert_truthvalue (TREE_OPERAND (arg, 2)));
3021
3022     case COMPOUND_EXPR:
3023       return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3024                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3025
3026     case NON_LVALUE_EXPR:
3027       return invert_truthvalue (TREE_OPERAND (arg, 0));
3028
3029     case NOP_EXPR:
3030       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3031         break;
3032
3033     case CONVERT_EXPR:
3034     case FLOAT_EXPR:
3035       return build1 (TREE_CODE (arg), type,
3036                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3037
3038     case BIT_AND_EXPR:
3039       if (!integer_onep (TREE_OPERAND (arg, 1)))
3040         break;
3041       return build2 (EQ_EXPR, type, arg,
3042                      fold_convert (type, integer_zero_node));
3043
3044     case SAVE_EXPR:
3045       return build1 (TRUTH_NOT_EXPR, type, arg);
3046
3047     case CLEANUP_POINT_EXPR:
3048       return build1 (CLEANUP_POINT_EXPR, type,
3049                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3050
3051     default:
3052       break;
3053     }
3054   gcc_assert (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE);
3055   return build1 (TRUTH_NOT_EXPR, type, arg);
3056 }
3057
3058 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3059    operands are another bit-wise operation with a common input.  If so,
3060    distribute the bit operations to save an operation and possibly two if
3061    constants are involved.  For example, convert
3062         (A | B) & (A | C) into A | (B & C)
3063    Further simplification will occur if B and C are constants.
3064
3065    If this optimization cannot be done, 0 will be returned.  */
3066
3067 static tree
3068 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3069 {
3070   tree common;
3071   tree left, right;
3072
3073   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3074       || TREE_CODE (arg0) == code
3075       || (TREE_CODE (arg0) != BIT_AND_EXPR
3076           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3077     return 0;
3078
3079   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3080     {
3081       common = TREE_OPERAND (arg0, 0);
3082       left = TREE_OPERAND (arg0, 1);
3083       right = TREE_OPERAND (arg1, 1);
3084     }
3085   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3086     {
3087       common = TREE_OPERAND (arg0, 0);
3088       left = TREE_OPERAND (arg0, 1);
3089       right = TREE_OPERAND (arg1, 0);
3090     }
3091   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3092     {
3093       common = TREE_OPERAND (arg0, 1);
3094       left = TREE_OPERAND (arg0, 0);
3095       right = TREE_OPERAND (arg1, 1);
3096     }
3097   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3098     {
3099       common = TREE_OPERAND (arg0, 1);
3100       left = TREE_OPERAND (arg0, 0);
3101       right = TREE_OPERAND (arg1, 0);
3102     }
3103   else
3104     return 0;
3105
3106   return fold_build2 (TREE_CODE (arg0), type, common,
3107                       fold_build2 (code, type, left, right));
3108 }
3109
3110 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3111    with code CODE.  This optimization is unsafe.  */
3112 static tree
3113 distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3114 {
3115   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3116   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3117
3118   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3119   if (mul0 == mul1
3120       && operand_equal_p (TREE_OPERAND (arg0, 1),
3121                        TREE_OPERAND (arg1, 1), 0))
3122     return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3123                         fold_build2 (code, type,
3124                                      TREE_OPERAND (arg0, 0),
3125                                      TREE_OPERAND (arg1, 0)),
3126                         TREE_OPERAND (arg0, 1));
3127
3128   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3129   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3130                        TREE_OPERAND (arg1, 0), 0)
3131       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3132       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3133     {
3134       REAL_VALUE_TYPE r0, r1;
3135       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3136       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3137       if (!mul0)
3138         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3139       if (!mul1)
3140         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3141       real_arithmetic (&r0, code, &r0, &r1);
3142       return fold_build2 (MULT_EXPR, type,
3143                           TREE_OPERAND (arg0, 0),
3144                           build_real (type, r0));
3145     }
3146
3147   return NULL_TREE;
3148 }
3149 \f
3150 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3151    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3152
3153 static tree
3154 make_bit_field_ref (tree inner, tree type, int bitsize, int bitpos,
3155                     int unsignedp)
3156 {
3157   tree result;
3158
3159   if (bitpos == 0)
3160     {
3161       tree size = TYPE_SIZE (TREE_TYPE (inner));
3162       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3163            || POINTER_TYPE_P (TREE_TYPE (inner)))
3164           && host_integerp (size, 0) 
3165           && tree_low_cst (size, 0) == bitsize)
3166         return fold_convert (type, inner);
3167     }
3168
3169   result = build3 (BIT_FIELD_REF, type, inner,
3170                    size_int (bitsize), bitsize_int (bitpos));
3171
3172   BIT_FIELD_REF_UNSIGNED (result) = unsignedp;
3173
3174   return result;
3175 }
3176
3177 /* Optimize a bit-field compare.
3178
3179    There are two cases:  First is a compare against a constant and the
3180    second is a comparison of two items where the fields are at the same
3181    bit position relative to the start of a chunk (byte, halfword, word)
3182    large enough to contain it.  In these cases we can avoid the shift
3183    implicit in bitfield extractions.
3184
3185    For constants, we emit a compare of the shifted constant with the
3186    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3187    compared.  For two fields at the same position, we do the ANDs with the
3188    similar mask and compare the result of the ANDs.
3189
3190    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3191    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3192    are the left and right operands of the comparison, respectively.
3193
3194    If the optimization described above can be done, we return the resulting
3195    tree.  Otherwise we return zero.  */
3196
3197 static tree
3198 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3199                             tree lhs, tree rhs)
3200 {
3201   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3202   tree type = TREE_TYPE (lhs);
3203   tree signed_type, unsigned_type;
3204   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3205   enum machine_mode lmode, rmode, nmode;
3206   int lunsignedp, runsignedp;
3207   int lvolatilep = 0, rvolatilep = 0;
3208   tree linner, rinner = NULL_TREE;
3209   tree mask;
3210   tree offset;
3211
3212   /* Get all the information about the extractions being done.  If the bit size
3213      if the same as the size of the underlying object, we aren't doing an
3214      extraction at all and so can do nothing.  We also don't want to
3215      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3216      then will no longer be able to replace it.  */
3217   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3218                                 &lunsignedp, &lvolatilep, false);
3219   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3220       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3221     return 0;
3222
3223  if (!const_p)
3224    {
3225      /* If this is not a constant, we can only do something if bit positions,
3226         sizes, and signedness are the same.  */
3227      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3228                                    &runsignedp, &rvolatilep, false);
3229
3230      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3231          || lunsignedp != runsignedp || offset != 0
3232          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3233        return 0;
3234    }
3235
3236   /* See if we can find a mode to refer to this field.  We should be able to,
3237      but fail if we can't.  */
3238   nmode = get_best_mode (lbitsize, lbitpos,
3239                          const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3240                          : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3241                                 TYPE_ALIGN (TREE_TYPE (rinner))),
3242                          word_mode, lvolatilep || rvolatilep);
3243   if (nmode == VOIDmode)
3244     return 0;
3245
3246   /* Set signed and unsigned types of the precision of this mode for the
3247      shifts below.  */
3248   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3249   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3250
3251   /* Compute the bit position and size for the new reference and our offset
3252      within it. If the new reference is the same size as the original, we
3253      won't optimize anything, so return zero.  */
3254   nbitsize = GET_MODE_BITSIZE (nmode);
3255   nbitpos = lbitpos & ~ (nbitsize - 1);
3256   lbitpos -= nbitpos;
3257   if (nbitsize == lbitsize)
3258     return 0;
3259
3260   if (BYTES_BIG_ENDIAN)
3261     lbitpos = nbitsize - lbitsize - lbitpos;
3262
3263   /* Make the mask to be used against the extracted field.  */
3264   mask = build_int_cst (unsigned_type, -1);
3265   mask = force_fit_type (mask, 0, false, false);
3266   mask = fold_convert (unsigned_type, mask);
3267   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3268   mask = const_binop (RSHIFT_EXPR, mask,
3269                       size_int (nbitsize - lbitsize - lbitpos), 0);
3270
3271   if (! const_p)
3272     /* If not comparing with constant, just rework the comparison
3273        and return.  */
3274     return build2 (code, compare_type,
3275                    build2 (BIT_AND_EXPR, unsigned_type,
3276                            make_bit_field_ref (linner, unsigned_type,
3277                                                nbitsize, nbitpos, 1),
3278                            mask),
3279                    build2 (BIT_AND_EXPR, unsigned_type,
3280                            make_bit_field_ref (rinner, unsigned_type,
3281                                                nbitsize, nbitpos, 1),
3282                            mask));
3283
3284   /* Otherwise, we are handling the constant case. See if the constant is too
3285      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
3286      this not only for its own sake, but to avoid having to test for this
3287      error case below.  If we didn't, we might generate wrong code.
3288
3289      For unsigned fields, the constant shifted right by the field length should
3290      be all zero.  For signed fields, the high-order bits should agree with
3291      the sign bit.  */
3292
3293   if (lunsignedp)
3294     {
3295       if (! integer_zerop (const_binop (RSHIFT_EXPR,
3296                                         fold_convert (unsigned_type, rhs),
3297                                         size_int (lbitsize), 0)))
3298         {
3299           warning (0, "comparison is always %d due to width of bit-field",
3300                    code == NE_EXPR);
3301           return constant_boolean_node (code == NE_EXPR, compare_type);
3302         }
3303     }
3304   else
3305     {
3306       tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
3307                               size_int (lbitsize - 1), 0);
3308       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3309         {
3310           warning (0, "comparison is always %d due to width of bit-field",
3311                    code == NE_EXPR);
3312           return constant_boolean_node (code == NE_EXPR, compare_type);
3313         }
3314     }
3315
3316   /* Single-bit compares should always be against zero.  */
3317   if (lbitsize == 1 && ! integer_zerop (rhs))
3318     {
3319       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3320       rhs = fold_convert (type, integer_zero_node);
3321     }
3322
3323   /* Make a new bitfield reference, shift the constant over the
3324      appropriate number of bits and mask it with the computed mask
3325      (in case this was a signed field).  If we changed it, make a new one.  */
3326   lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
3327   if (lvolatilep)
3328     {
3329       TREE_SIDE_EFFECTS (lhs) = 1;
3330       TREE_THIS_VOLATILE (lhs) = 1;
3331     }
3332
3333   rhs = fold (const_binop (BIT_AND_EXPR,
3334                            const_binop (LSHIFT_EXPR,
3335                                         fold_convert (unsigned_type, rhs),
3336                                         size_int (lbitpos), 0),
3337                            mask, 0));
3338
3339   return build2 (code, compare_type,
3340                  build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
3341                  rhs);
3342 }
3343 \f
3344 /* Subroutine for fold_truthop: decode a field reference.
3345
3346    If EXP is a comparison reference, we return the innermost reference.
3347
3348    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3349    set to the starting bit number.
3350
3351    If the innermost field can be completely contained in a mode-sized
3352    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
3353
3354    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3355    otherwise it is not changed.
3356
3357    *PUNSIGNEDP is set to the signedness of the field.
3358
3359    *PMASK is set to the mask used.  This is either contained in a
3360    BIT_AND_EXPR or derived from the width of the field.
3361
3362    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3363
3364    Return 0 if this is not a component reference or is one that we can't
3365    do anything with.  */
3366
3367 static tree
3368 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
3369                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3370                         int *punsignedp, int *pvolatilep,
3371                         tree *pmask, tree *pand_mask)
3372 {
3373   tree outer_type = 0;
3374   tree and_mask = 0;
3375   tree mask, inner, offset;
3376   tree unsigned_type;
3377   unsigned int precision;
3378
3379   /* All the optimizations using this function assume integer fields.
3380      There are problems with FP fields since the type_for_size call
3381      below can fail for, e.g., XFmode.  */
3382   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3383     return 0;
3384
3385   /* We are interested in the bare arrangement of bits, so strip everything
3386      that doesn't affect the machine mode.  However, record the type of the
3387      outermost expression if it may matter below.  */
3388   if (TREE_CODE (exp) == NOP_EXPR
3389       || TREE_CODE (exp) == CONVERT_EXPR
3390       || TREE_CODE (exp) == NON_LVALUE_EXPR)
3391     outer_type = TREE_TYPE (exp);
3392   STRIP_NOPS (exp);
3393
3394   if (TREE_CODE (exp) == BIT_AND_EXPR)
3395     {
3396       and_mask = TREE_OPERAND (exp, 1);
3397       exp = TREE_OPERAND (exp, 0);
3398       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3399       if (TREE_CODE (and_mask) != INTEGER_CST)
3400         return 0;
3401     }
3402
3403   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3404                                punsignedp, pvolatilep, false);
3405   if ((inner == exp && and_mask == 0)
3406       || *pbitsize < 0 || offset != 0
3407       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3408     return 0;
3409
3410   /* If the number of bits in the reference is the same as the bitsize of
3411      the outer type, then the outer type gives the signedness. Otherwise
3412      (in case of a small bitfield) the signedness is unchanged.  */
3413   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3414     *punsignedp = TYPE_UNSIGNED (outer_type);
3415
3416   /* Compute the mask to access the bitfield.  */
3417   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3418   precision = TYPE_PRECISION (unsigned_type);
3419
3420   mask = build_int_cst (unsigned_type, -1);
3421   mask = force_fit_type (mask, 0, false, false);
3422
3423   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3424   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3425
3426   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
3427   if (and_mask != 0)
3428     mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
3429                         fold_convert (unsigned_type, and_mask), mask);
3430
3431   *pmask = mask;
3432   *pand_mask = and_mask;
3433   return inner;
3434 }
3435
3436 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3437    bit positions.  */
3438
3439 static int
3440 all_ones_mask_p (tree mask, int size)
3441 {
3442   tree type = TREE_TYPE (mask);
3443   unsigned int precision = TYPE_PRECISION (type);
3444   tree tmask;
3445
3446   tmask = build_int_cst (lang_hooks.types.signed_type (type), -1);
3447   tmask = force_fit_type (tmask, 0, false, false);
3448
3449   return
3450     tree_int_cst_equal (mask,
3451                         const_binop (RSHIFT_EXPR,
3452                                      const_binop (LSHIFT_EXPR, tmask,
3453                                                   size_int (precision - size),
3454                                                   0),
3455                                      size_int (precision - size), 0));
3456 }
3457
3458 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3459    represents the sign bit of EXP's type.  If EXP represents a sign
3460    or zero extension, also test VAL against the unextended type.
3461    The return value is the (sub)expression whose sign bit is VAL,
3462    or NULL_TREE otherwise.  */
3463
3464 static tree
3465 sign_bit_p (tree exp, tree val)
3466 {
3467   unsigned HOST_WIDE_INT mask_lo, lo;
3468   HOST_WIDE_INT mask_hi, hi;
3469   int width;
3470   tree t;
3471
3472   /* Tree EXP must have an integral type.  */
3473   t = TREE_TYPE (exp);
3474   if (! INTEGRAL_TYPE_P (t))
3475     return NULL_TREE;
3476
3477   /* Tree VAL must be an integer constant.  */
3478   if (TREE_CODE (val) != INTEGER_CST
3479       || TREE_CONSTANT_OVERFLOW (val))
3480     return NULL_TREE;
3481
3482   width = TYPE_PRECISION (t);
3483   if (width > HOST_BITS_PER_WIDE_INT)
3484     {
3485       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3486       lo = 0;
3487
3488       mask_hi = ((unsigned HOST_WIDE_INT) -1
3489                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
3490       mask_lo = -1;
3491     }
3492   else
3493     {
3494       hi = 0;
3495       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3496
3497       mask_hi = 0;
3498       mask_lo = ((unsigned HOST_WIDE_INT) -1
3499                  >> (HOST_BITS_PER_WIDE_INT - width));
3500     }
3501
3502   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3503      treat VAL as if it were unsigned.  */
3504   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3505       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3506     return exp;
3507
3508   /* Handle extension from a narrower type.  */
3509   if (TREE_CODE (exp) == NOP_EXPR
3510       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3511     return sign_bit_p (TREE_OPERAND (exp, 0), val);
3512
3513   return NULL_TREE;
3514 }
3515
3516 /* Subroutine for fold_truthop: determine if an operand is simple enough
3517    to be evaluated unconditionally.  */
3518
3519 static int
3520 simple_operand_p (tree exp)
3521 {
3522   /* Strip any conversions that don't change the machine mode.  */
3523   STRIP_NOPS (exp);
3524
3525   return (CONSTANT_CLASS_P (exp)
3526           || TREE_CODE (exp) == SSA_NAME
3527           || (DECL_P (exp)
3528               && ! TREE_ADDRESSABLE (exp)
3529               && ! TREE_THIS_VOLATILE (exp)
3530               && ! DECL_NONLOCAL (exp)
3531               /* Don't regard global variables as simple.  They may be
3532                  allocated in ways unknown to the compiler (shared memory,
3533                  #pragma weak, etc).  */
3534               && ! TREE_PUBLIC (exp)
3535               && ! DECL_EXTERNAL (exp)
3536               /* Loading a static variable is unduly expensive, but global
3537                  registers aren't expensive.  */
3538               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3539 }
3540 \f
3541 /* The following functions are subroutines to fold_range_test and allow it to
3542    try to change a logical combination of comparisons into a range test.
3543
3544    For example, both
3545         X == 2 || X == 3 || X == 4 || X == 5
3546    and
3547         X >= 2 && X <= 5
3548    are converted to
3549         (unsigned) (X - 2) <= 3
3550
3551    We describe each set of comparisons as being either inside or outside
3552    a range, using a variable named like IN_P, and then describe the
3553    range with a lower and upper bound.  If one of the bounds is omitted,
3554    it represents either the highest or lowest value of the type.
3555
3556    In the comments below, we represent a range by two numbers in brackets
3557    preceded by a "+" to designate being inside that range, or a "-" to
3558    designate being outside that range, so the condition can be inverted by
3559    flipping the prefix.  An omitted bound is represented by a "-".  For
3560    example, "- [-, 10]" means being outside the range starting at the lowest
3561    possible value and ending at 10, in other words, being greater than 10.
3562    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3563    always false.
3564
3565    We set up things so that the missing bounds are handled in a consistent
3566    manner so neither a missing bound nor "true" and "false" need to be
3567    handled using a special case.  */
3568
3569 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3570    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3571    and UPPER1_P are nonzero if the respective argument is an upper bound
3572    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
3573    must be specified for a comparison.  ARG1 will be converted to ARG0's
3574    type if both are specified.  */
3575
3576 static tree
3577 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3578              tree arg1, int upper1_p)
3579 {
3580   tree tem;
3581   int result;
3582   int sgn0, sgn1;
3583
3584   /* If neither arg represents infinity, do the normal operation.
3585      Else, if not a comparison, return infinity.  Else handle the special
3586      comparison rules. Note that most of the cases below won't occur, but
3587      are handled for consistency.  */
3588
3589   if (arg0 != 0 && arg1 != 0)
3590     {
3591       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3592                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
3593       STRIP_NOPS (tem);
3594       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3595     }
3596
3597   if (TREE_CODE_CLASS (code) != tcc_comparison)
3598     return 0;
3599
3600   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3601      for neither.  In real maths, we cannot assume open ended ranges are
3602      the same. But, this is computer arithmetic, where numbers are finite.
3603      We can therefore make the transformation of any unbounded range with
3604      the value Z, Z being greater than any representable number. This permits
3605      us to treat unbounded ranges as equal.  */
3606   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3607   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3608   switch (code)
3609     {
3610     case EQ_EXPR:
3611       result = sgn0 == sgn1;
3612       break;
3613     case NE_EXPR:
3614       result = sgn0 != sgn1;
3615       break;
3616     case LT_EXPR:
3617       result = sgn0 < sgn1;
3618       break;
3619     case LE_EXPR:
3620       result = sgn0 <= sgn1;
3621       break;
3622     case GT_EXPR:
3623       result = sgn0 > sgn1;
3624       break;
3625     case GE_EXPR:
3626       result = sgn0 >= sgn1;
3627       break;
3628     default:
3629       gcc_unreachable ();
3630     }
3631
3632   return constant_boolean_node (result, type);
3633 }
3634 \f
3635 /* Given EXP, a logical expression, set the range it is testing into
3636    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
3637    actually being tested.  *PLOW and *PHIGH will be made of the same type
3638    as the returned expression.  If EXP is not a comparison, we will most
3639    likely not be returning a useful value and range.  */
3640
3641 static tree
3642 make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
3643 {
3644   enum tree_code code;
3645   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
3646   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
3647   int in_p, n_in_p;
3648   tree low, high, n_low, n_high;
3649
3650   /* Start with simply saying "EXP != 0" and then look at the code of EXP
3651      and see if we can refine the range.  Some of the cases below may not
3652      happen, but it doesn't seem worth worrying about this.  We "continue"
3653      the outer loop when we've changed something; otherwise we "break"
3654      the switch, which will "break" the while.  */
3655
3656   in_p = 0;
3657   low = high = fold_convert (TREE_TYPE (exp), integer_zero_node);
3658
3659   while (1)
3660     {
3661       code = TREE_CODE (exp);
3662       exp_type = TREE_TYPE (exp);
3663
3664       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3665         {
3666           if (TREE_CODE_LENGTH (code) > 0)
3667             arg0 = TREE_OPERAND (exp, 0);
3668           if (TREE_CODE_CLASS (code) == tcc_comparison
3669               || TREE_CODE_CLASS (code) == tcc_unary
3670               || TREE_CODE_CLASS (code) == tcc_binary)
3671             arg0_type = TREE_TYPE (arg0);
3672           if (TREE_CODE_CLASS (code) == tcc_binary
3673               || TREE_CODE_CLASS (code) == tcc_comparison
3674               || (TREE_CODE_CLASS (code) == tcc_expression
3675                   && TREE_CODE_LENGTH (code) > 1))
3676             arg1 = TREE_OPERAND (exp, 1);
3677         }
3678
3679       switch (code)
3680         {
3681         case TRUTH_NOT_EXPR:
3682           in_p = ! in_p, exp = arg0;
3683           continue;
3684
3685         case EQ_EXPR: case NE_EXPR:
3686         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3687           /* We can only do something if the range is testing for zero
3688              and if the second operand is an integer constant.  Note that
3689              saying something is "in" the range we make is done by
3690              complementing IN_P since it will set in the initial case of
3691              being not equal to zero; "out" is leaving it alone.  */
3692           if (low == 0 || high == 0
3693               || ! integer_zerop (low) || ! integer_zerop (high)
3694               || TREE_CODE (arg1) != INTEGER_CST)
3695             break;
3696
3697           switch (code)
3698             {
3699             case NE_EXPR:  /* - [c, c]  */
3700               low = high = arg1;
3701               break;
3702             case EQ_EXPR:  /* + [c, c]  */
3703               in_p = ! in_p, low = high = arg1;
3704               break;
3705             case GT_EXPR:  /* - [-, c] */
3706               low = 0, high = arg1;
3707               break;
3708             case GE_EXPR:  /* + [c, -] */
3709               in_p = ! in_p, low = arg1, high = 0;
3710               break;
3711             case LT_EXPR:  /* - [c, -] */
3712               low = arg1, high = 0;
3713               break;
3714             case LE_EXPR:  /* + [-, c] */
3715               in_p = ! in_p, low = 0, high = arg1;
3716               break;
3717             default:
3718               gcc_unreachable ();
3719             }
3720
3721           /* If this is an unsigned comparison, we also know that EXP is
3722              greater than or equal to zero.  We base the range tests we make
3723              on that fact, so we record it here so we can parse existing
3724              range tests.  We test arg0_type since often the return type
3725              of, e.g. EQ_EXPR, is boolean.  */
3726           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3727             {
3728               if (! merge_ranges (&n_in_p, &n_low, &n_high,
3729                                   in_p, low, high, 1,
3730                                   fold_convert (arg0_type, integer_zero_node),
3731                                   NULL_TREE))
3732                 break;
3733
3734               in_p = n_in_p, low = n_low, high = n_high;
3735
3736               /* If the high bound is missing, but we have a nonzero low
3737                  bound, reverse the range so it goes from zero to the low bound
3738                  minus 1.  */
3739               if (high == 0 && low && ! integer_zerop (low))
3740                 {
3741                   in_p = ! in_p;
3742                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3743                                       integer_one_node, 0);
3744                   low = fold_convert (arg0_type, integer_zero_node);
3745                 }
3746             }
3747
3748           exp = arg0;
3749           continue;
3750
3751         case NEGATE_EXPR:
3752           /* (-x) IN [a,b] -> x in [-b, -a]  */
3753           n_low = range_binop (MINUS_EXPR, exp_type,
3754                                fold_convert (exp_type, integer_zero_node),
3755                                0, high, 1);
3756           n_high = range_binop (MINUS_EXPR, exp_type,
3757                                 fold_convert (exp_type, integer_zero_node),
3758                                 0, low, 0);
3759           low = n_low, high = n_high;
3760           exp = arg0;
3761           continue;
3762
3763         case BIT_NOT_EXPR:
3764           /* ~ X -> -X - 1  */
3765           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
3766                         fold_convert (exp_type, integer_one_node));
3767           continue;
3768
3769         case PLUS_EXPR:  case MINUS_EXPR:
3770           if (TREE_CODE (arg1) != INTEGER_CST)
3771             break;
3772
3773           /* If EXP is signed, any overflow in the computation is undefined,
3774              so we don't worry about it so long as our computations on
3775              the bounds don't overflow.  For unsigned, overflow is defined
3776              and this is exactly the right thing.  */
3777           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3778                                arg0_type, low, 0, arg1, 0);
3779           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3780                                 arg0_type, high, 1, arg1, 0);
3781           if ((n_low != 0 && TREE_OVERFLOW (n_low))
3782               || (n_high != 0 && TREE_OVERFLOW (n_high)))
3783             break;
3784
3785           /* Check for an unsigned range which has wrapped around the maximum
3786              value thus making n_high < n_low, and normalize it.  */
3787           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3788             {
3789               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
3790                                  integer_one_node, 0);
3791               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
3792                                   integer_one_node, 0);
3793
3794               /* If the range is of the form +/- [ x+1, x ], we won't
3795                  be able to normalize it.  But then, it represents the
3796                  whole range or the empty set, so make it
3797                  +/- [ -, - ].  */
3798               if (tree_int_cst_equal (n_low, low)
3799                   && tree_int_cst_equal (n_high, high))
3800                 low = high = 0;
3801               else
3802                 in_p = ! in_p;
3803             }
3804           else
3805             low = n_low, high = n_high;
3806
3807           exp = arg0;
3808           continue;
3809
3810         case NOP_EXPR:  case NON_LVALUE_EXPR:  case CONVERT_EXPR:
3811           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
3812             break;
3813
3814           if (! INTEGRAL_TYPE_P (arg0_type)
3815               || (low != 0 && ! int_fits_type_p (low, arg0_type))
3816               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
3817             break;
3818
3819           n_low = low, n_high = high;
3820
3821           if (n_low != 0)
3822             n_low = fold_convert (arg0_type, n_low);
3823
3824           if (n_high != 0)
3825             n_high = fold_convert (arg0_type, n_high);
3826
3827
3828           /* If we're converting arg0 from an unsigned type, to exp,
3829              a signed type,  we will be doing the comparison as unsigned.
3830              The tests above have already verified that LOW and HIGH
3831              are both positive.
3832
3833              So we have to ensure that we will handle large unsigned
3834              values the same way that the current signed bounds treat
3835              negative values.  */
3836
3837           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
3838             {
3839               tree high_positive;
3840               tree equiv_type = lang_hooks.types.type_for_mode
3841                 (TYPE_MODE (arg0_type), 1);
3842
3843               /* A range without an upper bound is, naturally, unbounded.
3844                  Since convert would have cropped a very large value, use
3845                  the max value for the destination type.  */
3846               high_positive
3847                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
3848                 : TYPE_MAX_VALUE (arg0_type);
3849
3850               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
3851                 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
3852                                              fold_convert (arg0_type,
3853                                                            high_positive),
3854                                              fold_convert (arg0_type,
3855                                                            integer_one_node));
3856
3857               /* If the low bound is specified, "and" the range with the
3858                  range for which the original unsigned value will be
3859                  positive.  */
3860               if (low != 0)
3861                 {
3862                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
3863                                       1, n_low, n_high, 1,
3864                                       fold_convert (arg0_type,
3865                                                     integer_zero_node),
3866                                       high_positive))
3867                     break;
3868
3869                   in_p = (n_in_p == in_p);
3870                 }
3871               else
3872                 {
3873                   /* Otherwise, "or" the range with the range of the input
3874                      that will be interpreted as negative.  */
3875                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
3876                                       0, n_low, n_high, 1,
3877                                       fold_convert (arg0_type,
3878                                                     integer_zero_node),
3879                                       high_positive))
3880                     break;
3881
3882                   in_p = (in_p != n_in_p);
3883                 }
3884             }
3885
3886           exp = arg0;
3887           low = n_low, high = n_high;
3888           continue;
3889
3890         default:
3891           break;
3892         }
3893
3894       break;
3895     }
3896
3897   /* If EXP is a constant, we can evaluate whether this is true or false.  */
3898   if (TREE_CODE (exp) == INTEGER_CST)
3899     {
3900       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3901                                                  exp, 0, low, 0))
3902                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
3903                                                     exp, 1, high, 1)));
3904       low = high = 0;
3905       exp = 0;
3906     }
3907
3908   *pin_p = in_p, *plow = low, *phigh = high;
3909   return exp;
3910 }
3911 \f
3912 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3913    type, TYPE, return an expression to test if EXP is in (or out of, depending
3914    on IN_P) the range.  Return 0 if the test couldn't be created.  */
3915
3916 static tree
3917 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
3918 {
3919   tree etype = TREE_TYPE (exp);
3920   tree value;
3921
3922   if (! in_p)
3923     {
3924       value = build_range_check (type, exp, 1, low, high);
3925       if (value != 0)
3926         return invert_truthvalue (value);
3927
3928       return 0;
3929     }
3930
3931   if (low == 0 && high == 0)
3932     return fold_convert (type, integer_one_node);
3933
3934   if (low == 0)
3935     return fold_build2 (LE_EXPR, type, exp, high);
3936
3937   if (high == 0)
3938     return fold_build2 (GE_EXPR, type, exp, low);
3939
3940   if (operand_equal_p (low, high, 0))
3941     return fold_build2 (EQ_EXPR, type, exp, low);
3942
3943   if (integer_zerop (low))
3944     {
3945       if (! TYPE_UNSIGNED (etype))
3946         {
3947           etype = lang_hooks.types.unsigned_type (etype);
3948           high = fold_convert (etype, high);
3949           exp = fold_convert (etype, exp);
3950         }
3951       return build_range_check (type, exp, 1, 0, high);
3952     }
3953
3954   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
3955   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
3956     {
3957       unsigned HOST_WIDE_INT lo;
3958       HOST_WIDE_INT hi;
3959       int prec;
3960
3961       prec = TYPE_PRECISION (etype);
3962       if (prec <= HOST_BITS_PER_WIDE_INT)
3963         {
3964           hi = 0;
3965           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
3966         }
3967       else
3968         {
3969           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
3970           lo = (unsigned HOST_WIDE_INT) -1;
3971         }
3972
3973       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
3974         {
3975           if (TYPE_UNSIGNED (etype))
3976             {
3977               etype = lang_hooks.types.signed_type (etype);
3978               exp = fold_convert (etype, exp);
3979             }
3980           return fold_build2 (GT_EXPR, type, exp,
3981                               fold_convert (etype, integer_zero_node));
3982         }
3983     }
3984
3985   value = const_binop (MINUS_EXPR, high, low, 0);
3986   if (value != 0 && TREE_OVERFLOW (value) && ! TYPE_UNSIGNED (etype))
3987     {
3988       tree utype, minv, maxv;
3989
3990       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
3991          for the type in question, as we rely on this here.  */
3992       switch (TREE_CODE (etype))
3993         {
3994         case INTEGER_TYPE:
3995         case ENUMERAL_TYPE:
3996         case CHAR_TYPE:
3997           utype = lang_hooks.types.unsigned_type (etype);
3998           maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
3999           maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4000                               integer_one_node, 1);
4001           minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4002           if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4003                                           minv, 1, maxv, 1)))
4004             {
4005               etype = utype;
4006               high = fold_convert (etype, high);
4007               low = fold_convert (etype, low);
4008               exp = fold_convert (etype, exp);
4009               value = const_binop (MINUS_EXPR, high, low, 0);
4010             }
4011           break;
4012         default:
4013           break;
4014         }
4015     }
4016
4017   if (value != 0 && ! TREE_OVERFLOW (value))
4018     return build_range_check (type,
4019                               fold_build2 (MINUS_EXPR, etype, exp, low),
4020                               1, fold_convert (etype, integer_zero_node),
4021                               value);
4022
4023   return 0;
4024 }
4025 \f
4026 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4027    can, 0 if we can't.  Set the output range into the specified parameters.  */
4028
4029 static int
4030 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4031               tree high0, int in1_p, tree low1, tree high1)
4032 {
4033   int no_overlap;
4034   int subset;
4035   int temp;
4036   tree tem;
4037   int in_p;
4038   tree low, high;
4039   int lowequal = ((low0 == 0 && low1 == 0)
4040                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4041                                                 low0, 0, low1, 0)));
4042   int highequal = ((high0 == 0 && high1 == 0)
4043                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4044                                                  high0, 1, high1, 1)));
4045
4046   /* Make range 0 be the range that starts first, or ends last if they
4047      start at the same value.  Swap them if it isn't.  */
4048   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4049                                  low0, 0, low1, 0))
4050       || (lowequal
4051           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4052                                         high1, 1, high0, 1))))
4053     {
4054       temp = in0_p, in0_p = in1_p, in1_p = temp;
4055       tem = low0, low0 = low1, low1 = tem;
4056       tem = high0, high0 = high1, high1 = tem;
4057     }
4058
4059   /* Now flag two cases, whether the ranges are disjoint or whether the
4060      second range is totally subsumed in the first.  Note that the tests
4061      below are simplified by the ones above.  */
4062   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4063                                           high0, 1, low1, 0));
4064   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4065                                       high1, 1, high0, 1));
4066
4067   /* We now have four cases, depending on whether we are including or
4068      excluding the two ranges.  */
4069   if (in0_p && in1_p)
4070     {
4071       /* If they don't overlap, the result is false.  If the second range
4072          is a subset it is the result.  Otherwise, the range is from the start
4073          of the second to the end of the first.  */
4074       if (no_overlap)
4075         in_p = 0, low = high = 0;
4076       else if (subset)
4077         in_p = 1, low = low1, high = high1;
4078       else
4079         in_p = 1, low = low1, high = high0;
4080     }
4081
4082   else if (in0_p && ! in1_p)
4083     {
4084       /* If they don't overlap, the result is the first range.  If they are
4085          equal, the result is false.  If the second range is a subset of the
4086          first, and the ranges begin at the same place, we go from just after
4087          the end of the first range to the end of the second.  If the second
4088          range is not a subset of the first, or if it is a subset and both
4089          ranges end at the same place, the range starts at the start of the
4090          first range and ends just before the second range.
4091          Otherwise, we can't describe this as a single range.  */
4092       if (no_overlap)
4093         in_p = 1, low = low0, high = high0;
4094       else if (lowequal && highequal)
4095         in_p = 0, low = high = 0;
4096       else if (subset && lowequal)
4097         {
4098           in_p = 1, high = high0;
4099           low = range_binop (PLUS_EXPR, NULL_TREE, high1, 0,
4100                              integer_one_node, 0);
4101         }
4102       else if (! subset || highequal)
4103         {
4104           in_p = 1, low = low0;
4105           high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
4106                               integer_one_node, 0);
4107         }
4108       else
4109         return 0;
4110     }
4111
4112   else if (! in0_p && in1_p)
4113     {
4114       /* If they don't overlap, the result is the second range.  If the second
4115          is a subset of the first, the result is false.  Otherwise,
4116          the range starts just after the first range and ends at the
4117          end of the second.  */
4118       if (no_overlap)
4119         in_p = 1, low = low1, high = high1;
4120       else if (subset || highequal)
4121         in_p = 0, low = high = 0;
4122       else
4123         {
4124           in_p = 1, high = high1;
4125           low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
4126                              integer_one_node, 0);
4127         }
4128     }
4129
4130   else
4131     {
4132       /* The case where we are excluding both ranges.  Here the complex case
4133          is if they don't overlap.  In that case, the only time we have a
4134          range is if they are adjacent.  If the second is a subset of the
4135          first, the result is the first.  Otherwise, the range to exclude
4136          starts at the beginning of the first range and ends at the end of the
4137          second.  */
4138       if (no_overlap)
4139         {
4140           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4141                                          range_binop (PLUS_EXPR, NULL_TREE,
4142                                                       high0, 1,
4143                                                       integer_one_node, 1),
4144                                          1, low1, 0)))
4145             in_p = 0, low = low0, high = high1;
4146           else
4147             {
4148               /* Canonicalize - [min, x] into - [-, x].  */
4149               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4150                 switch (TREE_CODE (TREE_TYPE (low0)))
4151                   {
4152                   case ENUMERAL_TYPE:
4153                     if (TYPE_PRECISION (TREE_TYPE (low0))
4154                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4155                       break;
4156                     /* FALLTHROUGH */
4157                   case INTEGER_TYPE:
4158                   case CHAR_TYPE:
4159                     if (tree_int_cst_equal (low0,
4160                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4161                       low0 = 0;
4162                     break;
4163                   case POINTER_TYPE:
4164                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4165                         && integer_zerop (low0))
4166                       low0 = 0;
4167                     break;
4168                   default:
4169                     break;
4170                   }
4171
4172               /* Canonicalize - [x, max] into - [x, -].  */
4173               if (high1 && TREE_CODE (high1) == INTEGER_CST)
4174                 switch (TREE_CODE (TREE_TYPE (high1)))
4175                   {
4176                   case ENUMERAL_TYPE:
4177                     if (TYPE_PRECISION (TREE_TYPE (high1))
4178                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4179                       break;
4180                     /* FALLTHROUGH */
4181                   case INTEGER_TYPE:
4182                   case CHAR_TYPE:
4183                     if (tree_int_cst_equal (high1,
4184                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
4185                       high1 = 0;
4186                     break;
4187                   case POINTER_TYPE:
4188                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
4189                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4190                                                        high1, 1,
4191                                                        integer_one_node, 1)))
4192                       high1 = 0;
4193                     break;
4194                   default:
4195                     break;
4196                   }
4197
4198               /* The ranges might be also adjacent between the maximum and
4199                  minimum values of the given type.  For
4200                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4201                  return + [x + 1, y - 1].  */
4202               if (low0 == 0 && high1 == 0)
4203                 {
4204                   low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
4205                                      integer_one_node, 1);
4206                   high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
4207                                       integer_one_node, 0);
4208                   if (low == 0 || high == 0)
4209                     return 0;
4210
4211                   in_p = 1;
4212                 }
4213               else
4214                 return 0;
4215             }
4216         }
4217       else if (subset)
4218         in_p = 0, low = low0, high = high0;
4219       else
4220         in_p = 0, low = low0, high = high1;
4221     }
4222
4223   *pin_p = in_p, *plow = low, *phigh = high;
4224   return 1;
4225 }
4226 \f
4227
4228 /* Subroutine of fold, looking inside expressions of the form
4229    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4230    of the COND_EXPR.  This function is being used also to optimize
4231    A op B ? C : A, by reversing the comparison first.
4232
4233    Return a folded expression whose code is not a COND_EXPR
4234    anymore, or NULL_TREE if no folding opportunity is found.  */
4235
4236 static tree
4237 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
4238 {
4239   enum tree_code comp_code = TREE_CODE (arg0);
4240   tree arg00 = TREE_OPERAND (arg0, 0);
4241   tree arg01 = TREE_OPERAND (arg0, 1);
4242   tree arg1_type = TREE_TYPE (arg1);
4243   tree tem;
4244
4245   STRIP_NOPS (arg1);
4246   STRIP_NOPS (arg2);
4247
4248   /* If we have A op 0 ? A : -A, consider applying the following
4249      transformations:
4250
4251      A == 0? A : -A    same as -A
4252      A != 0? A : -A    same as A
4253      A >= 0? A : -A    same as abs (A)
4254      A > 0?  A : -A    same as abs (A)
4255      A <= 0? A : -A    same as -abs (A)
4256      A < 0?  A : -A    same as -abs (A)
4257
4258      None of these transformations work for modes with signed
4259      zeros.  If A is +/-0, the first two transformations will
4260      change the sign of the result (from +0 to -0, or vice
4261      versa).  The last four will fix the sign of the result,
4262      even though the original expressions could be positive or
4263      negative, depending on the sign of A.
4264
4265      Note that all these transformations are correct if A is
4266      NaN, since the two alternatives (A and -A) are also NaNs.  */
4267   if ((FLOAT_TYPE_P (TREE_TYPE (arg01))
4268        ? real_zerop (arg01)
4269        : integer_zerop (arg01))
4270       && ((TREE_CODE (arg2) == NEGATE_EXPR
4271            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4272              /* In the case that A is of the form X-Y, '-A' (arg2) may
4273                 have already been folded to Y-X, check for that. */
4274           || (TREE_CODE (arg1) == MINUS_EXPR
4275               && TREE_CODE (arg2) == MINUS_EXPR
4276               && operand_equal_p (TREE_OPERAND (arg1, 0),
4277                                   TREE_OPERAND (arg2, 1), 0)
4278               && operand_equal_p (TREE_OPERAND (arg1, 1),
4279                                   TREE_OPERAND (arg2, 0), 0))))
4280     switch (comp_code)
4281       {
4282       case EQ_EXPR:
4283       case UNEQ_EXPR:
4284         tem = fold_convert (arg1_type, arg1);
4285         return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
4286       case NE_EXPR:
4287       case LTGT_EXPR:
4288         return pedantic_non_lvalue (fold_convert (type, arg1));
4289       case UNGE_EXPR:
4290       case UNGT_EXPR:
4291         if (flag_trapping_math)
4292           break;
4293         /* Fall through.  */
4294       case GE_EXPR:
4295       case GT_EXPR:
4296         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4297           arg1 = fold_convert (lang_hooks.types.signed_type
4298                                (TREE_TYPE (arg1)), arg1);
4299         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
4300         return pedantic_non_lvalue (fold_convert (type, tem));
4301       case UNLE_EXPR:
4302       case UNLT_EXPR:
4303         if (flag_trapping_math)
4304           break;
4305       case LE_EXPR:
4306       case LT_EXPR:
4307         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4308           arg1 = fold_convert (lang_hooks.types.signed_type
4309                                (TREE_TYPE (arg1)), arg1);
4310         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
4311         return negate_expr (fold_convert (type, tem));
4312       default:
4313         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4314         break;
4315       }
4316
4317   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
4318      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
4319      both transformations are correct when A is NaN: A != 0
4320      is then true, and A == 0 is false.  */
4321
4322   if (integer_zerop (arg01) && integer_zerop (arg2))
4323     {
4324       if (comp_code == NE_EXPR)
4325         return pedantic_non_lvalue (fold_convert (type, arg1));
4326       else if (comp_code == EQ_EXPR)
4327         return fold_convert (type, integer_zero_node);
4328     }
4329
4330   /* Try some transformations of A op B ? A : B.
4331
4332      A == B? A : B    same as B
4333      A != B? A : B    same as A
4334      A >= B? A : B    same as max (A, B)
4335      A > B?  A : B    same as max (B, A)
4336      A <= B? A : B    same as min (A, B)
4337      A < B?  A : B    same as min (B, A)
4338
4339      As above, these transformations don't work in the presence
4340      of signed zeros.  For example, if A and B are zeros of
4341      opposite sign, the first two transformations will change
4342      the sign of the result.  In the last four, the original
4343      expressions give different results for (A=+0, B=-0) and
4344      (A=-0, B=+0), but the transformed expressions do not.
4345
4346      The first two transformations are correct if either A or B
4347      is a NaN.  In the first transformation, the condition will
4348      be false, and B will indeed be chosen.  In the case of the
4349      second transformation, the condition A != B will be true,
4350      and A will be chosen.
4351
4352      The conversions to max() and min() are not correct if B is
4353      a number and A is not.  The conditions in the original
4354      expressions will be false, so all four give B.  The min()
4355      and max() versions would give a NaN instead.  */
4356   if (operand_equal_for_comparison_p (arg01, arg2, arg00)
4357       /* Avoid these transformations if the COND_EXPR may be used
4358          as an lvalue in the C++ front-end.  PR c++/19199.  */
4359       && (in_gimple_form
4360           || strcmp (lang_hooks.name, "GNU C++") != 0
4361           || ! maybe_lvalue_p (arg1)
4362           || ! maybe_lvalue_p (arg2)))
4363     {
4364       tree comp_op0 = arg00;
4365       tree comp_op1 = arg01;
4366       tree comp_type = TREE_TYPE (comp_op0);
4367
4368       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
4369       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4370         {
4371           comp_type = type;
4372           comp_op0 = arg1;
4373           comp_op1 = arg2;
4374         }
4375
4376       switch (comp_code)
4377         {
4378         case EQ_EXPR:
4379           return pedantic_non_lvalue (fold_convert (type, arg2));
4380         case NE_EXPR:
4381           return pedantic_non_lvalue (fold_convert (type, arg1));
4382         case LE_EXPR:
4383         case LT_EXPR:
4384         case UNLE_EXPR:
4385         case UNLT_EXPR:
4386           /* In C++ a ?: expression can be an lvalue, so put the
4387              operand which will be used if they are equal first
4388              so that we can convert this back to the
4389              corresponding COND_EXPR.  */
4390           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4391             {
4392               comp_op0 = fold_convert (comp_type, comp_op0);
4393               comp_op1 = fold_convert (comp_type, comp_op1);
4394               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4395                     ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
4396                     : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
4397               return pedantic_non_lvalue (fold_convert (type, tem));
4398             }
4399           break;
4400         case GE_EXPR:
4401         case GT_EXPR:
4402         case UNGE_EXPR:
4403         case UNGT_EXPR:
4404           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4405             {
4406               comp_op0 = fold_convert (comp_type, comp_op0);
4407               comp_op1 = fold_convert (comp_type, comp_op1);
4408               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4409                     ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
4410                     : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
4411               return pedantic_non_lvalue (fold_convert (type, tem));
4412             }
4413           break;
4414         case UNEQ_EXPR:
4415           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4416             return pedantic_non_lvalue (fold_convert (type, arg2));
4417           break;
4418         case LTGT_EXPR:
4419           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4420             return pedantic_non_lvalue (fold_convert (type, arg1));
4421           break;
4422         default:
4423           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4424           break;
4425         }
4426     }
4427
4428   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4429      we might still be able to simplify this.  For example,
4430      if C1 is one less or one more than C2, this might have started
4431      out as a MIN or MAX and been transformed by this function.
4432      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
4433
4434   if (INTEGRAL_TYPE_P (type)
4435       && TREE_CODE (arg01) == INTEGER_CST
4436       && TREE_CODE (arg2) == INTEGER_CST)
4437     switch (comp_code)
4438       {
4439       case EQ_EXPR:
4440         /* We can replace A with C1 in this case.  */
4441         arg1 = fold_convert (type, arg01);
4442         return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
4443
4444       case LT_EXPR:
4445         /* If C1 is C2 + 1, this is min(A, C2).  */
4446         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4447                                OEP_ONLY_CONST)
4448             && operand_equal_p (arg01,
4449                                 const_binop (PLUS_EXPR, arg2,
4450                                              integer_one_node, 0),
4451                                 OEP_ONLY_CONST))
4452           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
4453                                                    type, arg1, arg2));
4454         break;
4455
4456       case LE_EXPR:
4457         /* If C1 is C2 - 1, this is min(A, C2).  */
4458         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4459                                OEP_ONLY_CONST)
4460             && operand_equal_p (arg01,
4461                                 const_binop (MINUS_EXPR, arg2,
4462                                              integer_one_node, 0),
4463                                 OEP_ONLY_CONST))
4464           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
4465                                                    type, arg1, arg2));
4466         break;
4467
4468       case GT_EXPR:
4469         /* If C1 is C2 - 1, this is max(A, C2).  */
4470         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4471                                OEP_ONLY_CONST)
4472             && operand_equal_p (arg01,
4473                                 const_binop (MINUS_EXPR, arg2,
4474                                              integer_one_node, 0),
4475                                 OEP_ONLY_CONST))
4476           return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
4477                                                    type, arg1, arg2));
4478         break;
4479
4480       case GE_EXPR:
4481         /* If C1 is C2 + 1, this is max(A, C2).  */
4482         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4483                                OEP_ONLY_CONST)
4484             && operand_equal_p (arg01,
4485                                 const_binop (PLUS_EXPR, arg2,
4486                                              integer_one_node, 0),
4487                                 OEP_ONLY_CONST))
4488           return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
4489                                                    type, arg1, arg2));
4490         break;
4491       case NE_EXPR:
4492         break;
4493       default:
4494         gcc_unreachable ();
4495       }
4496
4497   return NULL_TREE;
4498 }
4499
4500
4501 \f
4502 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4503 #define LOGICAL_OP_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
4504 #endif
4505
4506 /* EXP is some logical combination of boolean tests.  See if we can
4507    merge it into some range test.  Return the new tree if so.  */
4508
4509 static tree
4510 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
4511 {
4512   int or_op = (code == TRUTH_ORIF_EXPR
4513                || code == TRUTH_OR_EXPR);
4514   int in0_p, in1_p, in_p;
4515   tree low0, low1, low, high0, high1, high;
4516   tree lhs = make_range (op0, &in0_p, &low0, &high0);
4517   tree rhs = make_range (op1, &in1_p, &low1, &high1);
4518   tree tem;
4519
4520   /* If this is an OR operation, invert both sides; we will invert
4521      again at the end.  */
4522   if (or_op)
4523     in0_p = ! in0_p, in1_p = ! in1_p;
4524
4525   /* If both expressions are the same, if we can merge the ranges, and we
4526      can build the range test, return it or it inverted.  If one of the
4527      ranges is always true or always false, consider it to be the same
4528      expression as the other.  */
4529   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4530       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4531                        in1_p, low1, high1)
4532       && 0 != (tem = (build_range_check (type,
4533                                          lhs != 0 ? lhs
4534                                          : rhs != 0 ? rhs : integer_zero_node,
4535                                          in_p, low, high))))
4536     return or_op ? invert_truthvalue (tem) : tem;
4537
4538   /* On machines where the branch cost is expensive, if this is a
4539      short-circuited branch and the underlying object on both sides
4540      is the same, make a non-short-circuit operation.  */
4541   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4542            && lhs != 0 && rhs != 0
4543            && (code == TRUTH_ANDIF_EXPR
4544                || code == TRUTH_ORIF_EXPR)
4545            && operand_equal_p (lhs, rhs, 0))
4546     {
4547       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
4548          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4549          which cases we can't do this.  */
4550       if (simple_operand_p (lhs))
4551         return build2 (code == TRUTH_ANDIF_EXPR
4552                        ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4553                        type, op0, op1);
4554
4555       else if (lang_hooks.decls.global_bindings_p () == 0
4556                && ! CONTAINS_PLACEHOLDER_P (lhs))
4557         {
4558           tree common = save_expr (lhs);
4559
4560           if (0 != (lhs = build_range_check (type, common,
4561                                              or_op ? ! in0_p : in0_p,
4562                                              low0, high0))
4563               && (0 != (rhs = build_range_check (type, common,
4564                                                  or_op ? ! in1_p : in1_p,
4565                                                  low1, high1))))
4566             return build2 (code == TRUTH_ANDIF_EXPR
4567                            ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4568                            type, lhs, rhs);
4569         }
4570     }
4571
4572   return 0;
4573 }
4574 \f
4575 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4576    bit value.  Arrange things so the extra bits will be set to zero if and
4577    only if C is signed-extended to its full width.  If MASK is nonzero,
4578    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
4579
4580 static tree
4581 unextend (tree c, int p, int unsignedp, tree mask)
4582 {
4583   tree type = TREE_TYPE (c);
4584   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4585   tree temp;
4586
4587   if (p == modesize || unsignedp)
4588     return c;
4589
4590   /* We work by getting just the sign bit into the low-order bit, then
4591      into the high-order bit, then sign-extend.  We then XOR that value
4592      with C.  */
4593   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
4594   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
4595
4596   /* We must use a signed type in order to get an arithmetic right shift.
4597      However, we must also avoid introducing accidental overflows, so that
4598      a subsequent call to integer_zerop will work.  Hence we must
4599      do the type conversion here.  At this point, the constant is either
4600      zero or one, and the conversion to a signed type can never overflow.
4601      We could get an overflow if this conversion is done anywhere else.  */
4602   if (TYPE_UNSIGNED (type))
4603     temp = fold_convert (lang_hooks.types.signed_type (type), temp);
4604
4605   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
4606   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
4607   if (mask != 0)
4608     temp = const_binop (BIT_AND_EXPR, temp,
4609                         fold_convert (TREE_TYPE (c), mask), 0);
4610   /* If necessary, convert the type back to match the type of C.  */
4611   if (TYPE_UNSIGNED (type))
4612     temp = fold_convert (type, temp);
4613
4614   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
4615 }
4616 \f
4617 /* Find ways of folding logical expressions of LHS and RHS:
4618    Try to merge two comparisons to the same innermost item.
4619    Look for range tests like "ch >= '0' && ch <= '9'".
4620    Look for combinations of simple terms on machines with expensive branches
4621    and evaluate the RHS unconditionally.
4622
4623    For example, if we have p->a == 2 && p->b == 4 and we can make an
4624    object large enough to span both A and B, we can do this with a comparison
4625    against the object ANDed with the a mask.
4626
4627    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
4628    operations to do this with one comparison.
4629
4630    We check for both normal comparisons and the BIT_AND_EXPRs made this by
4631    function and the one above.
4632
4633    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
4634    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
4635
4636    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
4637    two operands.
4638
4639    We return the simplified tree or 0 if no optimization is possible.  */
4640
4641 static tree
4642 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
4643 {
4644   /* If this is the "or" of two comparisons, we can do something if
4645      the comparisons are NE_EXPR.  If this is the "and", we can do something
4646      if the comparisons are EQ_EXPR.  I.e.,
4647         (a->b == 2 && a->c == 4) can become (a->new == NEW).
4648
4649      WANTED_CODE is this operation code.  For single bit fields, we can
4650      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
4651      comparison for one-bit fields.  */
4652
4653   enum tree_code wanted_code;
4654   enum tree_code lcode, rcode;
4655   tree ll_arg, lr_arg, rl_arg, rr_arg;
4656   tree ll_inner, lr_inner, rl_inner, rr_inner;
4657   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
4658   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
4659   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
4660   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
4661   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
4662   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
4663   enum machine_mode lnmode, rnmode;
4664   tree ll_mask, lr_mask, rl_mask, rr_mask;
4665   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
4666   tree l_const, r_const;
4667   tree lntype, rntype, result;
4668   int first_bit, end_bit;
4669   int volatilep;
4670
4671   /* Start by getting the comparison codes.  Fail if anything is volatile.
4672      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
4673      it were surrounded with a NE_EXPR.  */
4674
4675   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
4676     return 0;
4677
4678   lcode = TREE_CODE (lhs);
4679   rcode = TREE_CODE (rhs);
4680
4681   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
4682     {
4683       lhs = build2 (NE_EXPR, truth_type, lhs,
4684                     fold_convert (TREE_TYPE (lhs), integer_zero_node));
4685       lcode = NE_EXPR;
4686     }
4687
4688   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
4689     {
4690       rhs = build2 (NE_EXPR, truth_type, rhs,
4691                     fold_convert (TREE_TYPE (rhs), integer_zero_node));
4692       rcode = NE_EXPR;
4693     }
4694
4695   if (TREE_CODE_CLASS (lcode) != tcc_comparison
4696       || TREE_CODE_CLASS (rcode) != tcc_comparison)
4697     return 0;
4698
4699   ll_arg = TREE_OPERAND (lhs, 0);
4700   lr_arg = TREE_OPERAND (lhs, 1);
4701   rl_arg = TREE_OPERAND (rhs, 0);
4702   rr_arg = TREE_OPERAND (rhs, 1);
4703
4704   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
4705   if (simple_operand_p (ll_arg)
4706       && simple_operand_p (lr_arg))
4707     {
4708       tree result;
4709       if (operand_equal_p (ll_arg, rl_arg, 0)
4710           && operand_equal_p (lr_arg, rr_arg, 0))
4711         {
4712           result = combine_comparisons (code, lcode, rcode,
4713                                         truth_type, ll_arg, lr_arg);
4714           if (result)
4715             return result;
4716         }
4717       else if (operand_equal_p (ll_arg, rr_arg, 0)
4718                && operand_equal_p (lr_arg, rl_arg, 0))
4719         {
4720           result = combine_comparisons (code, lcode,
4721                                         swap_tree_comparison (rcode),
4722                                         truth_type, ll_arg, lr_arg);
4723           if (result)
4724             return result;
4725         }
4726     }
4727
4728   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
4729           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
4730
4731   /* If the RHS can be evaluated unconditionally and its operands are
4732      simple, it wins to evaluate the RHS unconditionally on machines
4733      with expensive branches.  In this case, this isn't a comparison
4734      that can be merged.  Avoid doing this if the RHS is a floating-point
4735      comparison since those can trap.  */
4736
4737   if (BRANCH_COST >= 2
4738       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
4739       && simple_operand_p (rl_arg)
4740       && simple_operand_p (rr_arg))
4741     {
4742       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
4743       if (code == TRUTH_OR_EXPR
4744           && lcode == NE_EXPR && integer_zerop (lr_arg)
4745           && rcode == NE_EXPR && integer_zerop (rr_arg)
4746           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
4747         return build2 (NE_EXPR, truth_type,
4748                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
4749                                ll_arg, rl_arg),
4750                        fold_convert (TREE_TYPE (ll_arg), integer_zero_node));
4751
4752       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
4753       if (code == TRUTH_AND_EXPR
4754           && lcode == EQ_EXPR && integer_zerop (lr_arg)
4755           && rcode == EQ_EXPR && integer_zerop (rr_arg)
4756           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
4757         return build2 (EQ_EXPR, truth_type,
4758                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
4759                                ll_arg, rl_arg),
4760                        fold_convert (TREE_TYPE (ll_arg), integer_zero_node));
4761
4762       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
4763         return build2 (code, truth_type, lhs, rhs);
4764     }
4765
4766   /* See if the comparisons can be merged.  Then get all the parameters for
4767      each side.  */
4768
4769   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
4770       || (rcode != EQ_EXPR && rcode != NE_EXPR))
4771     return 0;
4772
4773   volatilep = 0;
4774   ll_inner = decode_field_reference (ll_arg,
4775                                      &ll_bitsize, &ll_bitpos, &ll_mode,
4776                                      &ll_unsignedp, &volatilep, &ll_mask,
4777                                      &ll_and_mask);
4778   lr_inner = decode_field_reference (lr_arg,
4779                                      &lr_bitsize, &lr_bitpos, &lr_mode,
4780                                      &lr_unsignedp, &volatilep, &lr_mask,
4781                                      &lr_and_mask);
4782   rl_inner = decode_field_reference (rl_arg,
4783                                      &rl_bitsize, &rl_bitpos, &rl_mode,
4784                                      &rl_unsignedp, &volatilep, &rl_mask,
4785                                      &rl_and_mask);
4786   rr_inner = decode_field_reference (rr_arg,
4787                                      &rr_bitsize, &rr_bitpos, &rr_mode,
4788                                      &rr_unsignedp, &volatilep, &rr_mask,
4789                                      &rr_and_mask);
4790
4791   /* It must be true that the inner operation on the lhs of each
4792      comparison must be the same if we are to be able to do anything.
4793      Then see if we have constants.  If not, the same must be true for
4794      the rhs's.  */
4795   if (volatilep || ll_inner == 0 || rl_inner == 0
4796       || ! operand_equal_p (ll_inner, rl_inner, 0))
4797     return 0;
4798
4799   if (TREE_CODE (lr_arg) == INTEGER_CST
4800       && TREE_CODE (rr_arg) == INTEGER_CST)
4801     l_const = lr_arg, r_const = rr_arg;
4802   else if (lr_inner == 0 || rr_inner == 0
4803            || ! operand_equal_p (lr_inner, rr_inner, 0))
4804     return 0;
4805   else
4806     l_const = r_const = 0;
4807
4808   /* If either comparison code is not correct for our logical operation,
4809      fail.  However, we can convert a one-bit comparison against zero into
4810      the opposite comparison against that bit being set in the field.  */
4811
4812   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
4813   if (lcode != wanted_code)
4814     {
4815       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
4816         {
4817           /* Make the left operand unsigned, since we are only interested
4818              in the value of one bit.  Otherwise we are doing the wrong
4819              thing below.  */
4820           ll_unsignedp = 1;
4821           l_const = ll_mask;
4822         }
4823       else
4824         return 0;
4825     }
4826
4827   /* This is analogous to the code for l_const above.  */
4828   if (rcode != wanted_code)
4829     {
4830       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
4831         {
4832           rl_unsignedp = 1;
4833           r_const = rl_mask;
4834         }
4835       else
4836         return 0;
4837     }
4838
4839   /* After this point all optimizations will generate bit-field
4840      references, which we might not want.  */
4841   if (! lang_hooks.can_use_bit_fields_p ())
4842     return 0;
4843
4844   /* See if we can find a mode that contains both fields being compared on
4845      the left.  If we can't, fail.  Otherwise, update all constants and masks
4846      to be relative to a field of that size.  */
4847   first_bit = MIN (ll_bitpos, rl_bitpos);
4848   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
4849   lnmode = get_best_mode (end_bit - first_bit, first_bit,
4850                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
4851                           volatilep);
4852   if (lnmode == VOIDmode)
4853     return 0;
4854
4855   lnbitsize = GET_MODE_BITSIZE (lnmode);
4856   lnbitpos = first_bit & ~ (lnbitsize - 1);
4857   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
4858   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
4859
4860   if (BYTES_BIG_ENDIAN)
4861     {
4862       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
4863       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
4864     }
4865
4866   ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
4867                          size_int (xll_bitpos), 0);
4868   rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
4869                          size_int (xrl_bitpos), 0);
4870
4871   if (l_const)
4872     {
4873       l_const = fold_convert (lntype, l_const);
4874       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
4875       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
4876       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
4877                                         fold_build1 (BIT_NOT_EXPR,
4878                                                      lntype, ll_mask),
4879                                         0)))
4880         {
4881           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
4882
4883           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
4884         }
4885     }
4886   if (r_const)
4887     {
4888       r_const = fold_convert (lntype, r_const);
4889       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
4890       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
4891       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
4892                                         fold_build1 (BIT_NOT_EXPR,
4893                                                      lntype, rl_mask),
4894                                         0)))
4895         {
4896           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
4897
4898           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
4899         }
4900     }
4901
4902   /* If the right sides are not constant, do the same for it.  Also,
4903      disallow this optimization if a size or signedness mismatch occurs
4904      between the left and right sides.  */
4905   if (l_const == 0)
4906     {
4907       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
4908           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
4909           /* Make sure the two fields on the right
4910              correspond to the left without being swapped.  */
4911           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
4912         return 0;
4913
4914       first_bit = MIN (lr_bitpos, rr_bitpos);
4915       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
4916       rnmode = get_best_mode (end_bit - first_bit, first_bit,
4917                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
4918                               volatilep);
4919       if (rnmode == VOIDmode)
4920         return 0;
4921
4922       rnbitsize = GET_MODE_BITSIZE (rnmode);
4923       rnbitpos = first_bit & ~ (rnbitsize - 1);
4924       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
4925       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
4926
4927       if (BYTES_BIG_ENDIAN)
4928         {
4929           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
4930           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
4931         }
4932
4933       lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
4934                              size_int (xlr_bitpos), 0);
4935       rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
4936                              size_int (xrr_bitpos), 0);
4937
4938       /* Make a mask that corresponds to both fields being compared.
4939          Do this for both items being compared.  If the operands are the
4940          same size and the bits being compared are in the same position
4941          then we can do this by masking both and comparing the masked
4942          results.  */
4943       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4944       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
4945       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
4946         {
4947           lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4948                                     ll_unsignedp || rl_unsignedp);
4949           if (! all_ones_mask_p (ll_mask, lnbitsize))
4950             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
4951
4952           rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
4953                                     lr_unsignedp || rr_unsignedp);
4954           if (! all_ones_mask_p (lr_mask, rnbitsize))
4955             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
4956
4957           return build2 (wanted_code, truth_type, lhs, rhs);
4958         }
4959
4960       /* There is still another way we can do something:  If both pairs of
4961          fields being compared are adjacent, we may be able to make a wider
4962          field containing them both.
4963
4964          Note that we still must mask the lhs/rhs expressions.  Furthermore,
4965          the mask must be shifted to account for the shift done by
4966          make_bit_field_ref.  */
4967       if ((ll_bitsize + ll_bitpos == rl_bitpos
4968            && lr_bitsize + lr_bitpos == rr_bitpos)
4969           || (ll_bitpos == rl_bitpos + rl_bitsize
4970               && lr_bitpos == rr_bitpos + rr_bitsize))
4971         {
4972           tree type;
4973
4974           lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
4975                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
4976           rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
4977                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
4978
4979           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
4980                                  size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
4981           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
4982                                  size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
4983
4984           /* Convert to the smaller type before masking out unwanted bits.  */
4985           type = lntype;
4986           if (lntype != rntype)
4987             {
4988               if (lnbitsize > rnbitsize)
4989                 {
4990                   lhs = fold_convert (rntype, lhs);
4991                   ll_mask = fold_convert (rntype, ll_mask);
4992                   type = rntype;
4993                 }
4994               else if (lnbitsize < rnbitsize)
4995                 {
4996                   rhs = fold_convert (lntype, rhs);
4997                   lr_mask = fold_convert (lntype, lr_mask);
4998                   type = lntype;
4999                 }
5000             }
5001
5002           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5003             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5004
5005           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5006             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5007
5008           return build2 (wanted_code, truth_type, lhs, rhs);
5009         }
5010
5011       return 0;
5012     }
5013
5014   /* Handle the case of comparisons with constants.  If there is something in
5015      common between the masks, those bits of the constants must be the same.
5016      If not, the condition is always false.  Test for this to avoid generating
5017      incorrect code below.  */
5018   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5019   if (! integer_zerop (result)
5020       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5021                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5022     {
5023       if (wanted_code == NE_EXPR)
5024         {
5025           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5026           return constant_boolean_node (true, truth_type);
5027         }
5028       else
5029         {
5030           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5031           return constant_boolean_node (false, truth_type);
5032         }
5033     }
5034
5035   /* Construct the expression we will return.  First get the component
5036      reference we will make.  Unless the mask is all ones the width of
5037      that field, perform the mask operation.  Then compare with the
5038      merged constant.  */
5039   result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5040                                ll_unsignedp || rl_unsignedp);
5041
5042   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5043   if (! all_ones_mask_p (ll_mask, lnbitsize))
5044     result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5045
5046   return build2 (wanted_code, truth_type, result,
5047                  const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
5048 }
5049 \f
5050 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5051    constant.  */
5052
5053 static tree
5054 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5055 {
5056   tree arg0 = op0;
5057   enum tree_code op_code;
5058   tree comp_const = op1;
5059   tree minmax_const;
5060   int consts_equal, consts_lt;
5061   tree inner;
5062
5063   STRIP_SIGN_NOPS (arg0);
5064
5065   op_code = TREE_CODE (arg0);
5066   minmax_const = TREE_OPERAND (arg0, 1);
5067   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5068   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5069   inner = TREE_OPERAND (arg0, 0);
5070
5071   /* If something does not permit us to optimize, return the original tree.  */
5072   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5073       || TREE_CODE (comp_const) != INTEGER_CST
5074       || TREE_CONSTANT_OVERFLOW (comp_const)
5075       || TREE_CODE (minmax_const) != INTEGER_CST
5076       || TREE_CONSTANT_OVERFLOW (minmax_const))
5077     return NULL_TREE;
5078
5079   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5080      and GT_EXPR, doing the rest with recursive calls using logical
5081      simplifications.  */
5082   switch (code)
5083     {
5084     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5085       {
5086         /* FIXME: We should be able to invert code without building a
5087            scratch tree node, but doing so would require us to
5088            duplicate a part of invert_truthvalue here.  */
5089         tree tem = invert_truthvalue (build2 (code, type, op0, op1));
5090         tem = optimize_minmax_comparison (TREE_CODE (tem),
5091                                           TREE_TYPE (tem),
5092                                           TREE_OPERAND (tem, 0),
5093                                           TREE_OPERAND (tem, 1));
5094         return invert_truthvalue (tem);
5095       }
5096
5097     case GE_EXPR:
5098       return
5099         fold_build2 (TRUTH_ORIF_EXPR, type,
5100                      optimize_minmax_comparison
5101                      (EQ_EXPR, type, arg0, comp_const),
5102                      optimize_minmax_comparison
5103                      (GT_EXPR, type, arg0, comp_const));
5104
5105     case EQ_EXPR:
5106       if (op_code == MAX_EXPR && consts_equal)
5107         /* MAX (X, 0) == 0  ->  X <= 0  */
5108         return fold_build2 (LE_EXPR, type, inner, comp_const);
5109
5110       else if (op_code == MAX_EXPR && consts_lt)
5111         /* MAX (X, 0) == 5  ->  X == 5   */
5112         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5113
5114       else if (op_code == MAX_EXPR)
5115         /* MAX (X, 0) == -1  ->  false  */
5116         return omit_one_operand (type, integer_zero_node, inner);
5117
5118       else if (consts_equal)
5119         /* MIN (X, 0) == 0  ->  X >= 0  */
5120         return fold_build2 (GE_EXPR, type, inner, comp_const);
5121
5122       else if (consts_lt)
5123         /* MIN (X, 0) == 5  ->  false  */
5124         return omit_one_operand (type, integer_zero_node, inner);
5125
5126       else
5127         /* MIN (X, 0) == -1  ->  X == -1  */
5128         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5129
5130     case GT_EXPR:
5131       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5132         /* MAX (X, 0) > 0  ->  X > 0
5133            MAX (X, 0) > 5  ->  X > 5  */
5134         return fold_build2 (GT_EXPR, type, inner, comp_const);
5135
5136       else if (op_code == MAX_EXPR)
5137         /* MAX (X, 0) > -1  ->  true  */
5138         return omit_one_operand (type, integer_one_node, inner);
5139
5140       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5141         /* MIN (X, 0) > 0  ->  false
5142            MIN (X, 0) > 5  ->  false  */
5143         return omit_one_operand (type, integer_zero_node, inner);
5144
5145       else
5146         /* MIN (X, 0) > -1  ->  X > -1  */
5147         return fold_build2 (GT_EXPR, type, inner, comp_const);
5148
5149     default:
5150       return NULL_TREE;
5151     }
5152 }
5153 \f
5154 /* T is an integer expression that is being multiplied, divided, or taken a
5155    modulus (CODE says which and what kind of divide or modulus) by a
5156    constant C.  See if we can eliminate that operation by folding it with
5157    other operations already in T.  WIDE_TYPE, if non-null, is a type that
5158    should be used for the computation if wider than our type.
5159
5160    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5161    (X * 2) + (Y * 4).  We must, however, be assured that either the original
5162    expression would not overflow or that overflow is undefined for the type
5163    in the language in question.
5164
5165    We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
5166    the machine has a multiply-accumulate insn or that this is part of an
5167    addressing calculation.
5168
5169    If we return a non-null expression, it is an equivalent form of the
5170    original computation, but need not be in the original type.  */
5171
5172 static tree
5173 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
5174 {
5175   /* To avoid exponential search depth, refuse to allow recursion past
5176      three levels.  Beyond that (1) it's highly unlikely that we'll find
5177      something interesting and (2) we've probably processed it before
5178      when we built the inner expression.  */
5179
5180   static int depth;
5181   tree ret;
5182
5183   if (depth > 3)
5184     return NULL;
5185
5186   depth++;
5187   ret = extract_muldiv_1 (t, c, code, wide_type);
5188   depth--;
5189
5190   return ret;
5191 }
5192
5193 static tree
5194 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
5195 {
5196   tree type = TREE_TYPE (t);
5197   enum tree_code tcode = TREE_CODE (t);
5198   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5199                                    > GET_MODE_SIZE (TYPE_MODE (type)))
5200                 ? wide_type : type);
5201   tree t1, t2;
5202   int same_p = tcode == code;
5203   tree op0 = NULL_TREE, op1 = NULL_TREE;
5204
5205   /* Don't deal with constants of zero here; they confuse the code below.  */
5206   if (integer_zerop (c))
5207     return NULL_TREE;
5208
5209   if (TREE_CODE_CLASS (tcode) == tcc_unary)
5210     op0 = TREE_OPERAND (t, 0);
5211
5212   if (TREE_CODE_CLASS (tcode) == tcc_binary)
5213     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5214
5215   /* Note that we need not handle conditional operations here since fold
5216      already handles those cases.  So just do arithmetic here.  */
5217   switch (tcode)
5218     {
5219     case INTEGER_CST:
5220       /* For a constant, we can always simplify if we are a multiply
5221          or (for divide and modulus) if it is a multiple of our constant.  */
5222       if (code == MULT_EXPR
5223           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
5224         return const_binop (code, fold_convert (ctype, t),
5225                             fold_convert (ctype, c), 0);
5226       break;
5227
5228     case CONVERT_EXPR:  case NON_LVALUE_EXPR:  case NOP_EXPR:
5229       /* If op0 is an expression ...  */
5230       if ((COMPARISON_CLASS_P (op0)
5231            || UNARY_CLASS_P (op0)
5232            || BINARY_CLASS_P (op0)
5233            || EXPRESSION_CLASS_P (op0))
5234           /* ... and is unsigned, and its type is smaller than ctype,
5235              then we cannot pass through as widening.  */
5236           && ((TYPE_UNSIGNED (TREE_TYPE (op0))
5237                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5238                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5239                && (GET_MODE_SIZE (TYPE_MODE (ctype))
5240                    > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
5241               /* ... or this is a truncation (t is narrower than op0),
5242                  then we cannot pass through this narrowing.  */
5243               || (GET_MODE_SIZE (TYPE_MODE (type))
5244                   < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
5245               /* ... or signedness changes for division or modulus,
5246                  then we cannot pass through this conversion.  */
5247               || (code != MULT_EXPR
5248                   && (TYPE_UNSIGNED (ctype)
5249                       != TYPE_UNSIGNED (TREE_TYPE (op0))))))
5250         break;
5251
5252       /* Pass the constant down and see if we can make a simplification.  If
5253          we can, replace this expression with the inner simplification for
5254          possible later conversion to our or some other type.  */
5255       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5256           && TREE_CODE (t2) == INTEGER_CST
5257           && ! TREE_CONSTANT_OVERFLOW (t2)
5258           && (0 != (t1 = extract_muldiv (op0, t2, code,
5259                                          code == MULT_EXPR
5260                                          ? ctype : NULL_TREE))))
5261         return t1;
5262       break;
5263
5264     case ABS_EXPR:
5265       /* If widening the type changes it from signed to unsigned, then we
5266          must avoid building ABS_EXPR itself as unsigned.  */
5267       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5268         {
5269           tree cstype = (*lang_hooks.types.signed_type) (ctype);
5270           if ((t1 = extract_muldiv (op0, c, code, cstype)) != 0)
5271             {
5272               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5273               return fold_convert (ctype, t1);
5274             }
5275           break;
5276         }
5277       /* FALLTHROUGH */
5278     case NEGATE_EXPR:
5279       if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
5280         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5281       break;
5282
5283     case MIN_EXPR:  case MAX_EXPR:
5284       /* If widening the type changes the signedness, then we can't perform
5285          this optimization as that changes the result.  */
5286       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5287         break;
5288
5289       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
5290       if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
5291           && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
5292         {
5293           if (tree_int_cst_sgn (c) < 0)
5294             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5295
5296           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5297                               fold_convert (ctype, t2));
5298         }
5299       break;
5300
5301     case LSHIFT_EXPR:  case RSHIFT_EXPR:
5302       /* If the second operand is constant, this is a multiplication
5303          or floor division, by a power of two, so we can treat it that
5304          way unless the multiplier or divisor overflows.  Signed
5305          left-shift overflow is implementation-defined rather than
5306          undefined in C90, so do not convert signed left shift into
5307          multiplication.  */
5308       if (TREE_CODE (op1) == INTEGER_CST
5309           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5310           /* const_binop may not detect overflow correctly,
5311              so check for it explicitly here.  */
5312           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5313           && TREE_INT_CST_HIGH (op1) == 0
5314           && 0 != (t1 = fold_convert (ctype,
5315                                       const_binop (LSHIFT_EXPR,
5316                                                    size_one_node,
5317                                                    op1, 0)))
5318           && ! TREE_OVERFLOW (t1))
5319         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5320                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
5321                                        ctype, fold_convert (ctype, op0), t1),
5322                                c, code, wide_type);
5323       break;
5324
5325     case PLUS_EXPR:  case MINUS_EXPR:
5326       /* See if we can eliminate the operation on both sides.  If we can, we
5327          can return a new PLUS or MINUS.  If we can't, the only remaining
5328          cases where we can do anything are if the second operand is a
5329          constant.  */
5330       t1 = extract_muldiv (op0, c, code, wide_type);
5331       t2 = extract_muldiv (op1, c, code, wide_type);
5332       if (t1 != 0 && t2 != 0
5333           && (code == MULT_EXPR
5334               /* If not multiplication, we can only do this if both operands
5335                  are divisible by c.  */
5336               || (multiple_of_p (ctype, op0, c)
5337                   && multiple_of_p (ctype, op1, c))))
5338         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5339                             fold_convert (ctype, t2));
5340
5341       /* If this was a subtraction, negate OP1 and set it to be an addition.
5342          This simplifies the logic below.  */
5343       if (tcode == MINUS_EXPR)
5344         tcode = PLUS_EXPR, op1 = negate_expr (op1);
5345
5346       if (TREE_CODE (op1) != INTEGER_CST)
5347         break;
5348
5349       /* If either OP1 or C are negative, this optimization is not safe for
5350          some of the division and remainder types while for others we need
5351          to change the code.  */
5352       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5353         {
5354           if (code == CEIL_DIV_EXPR)
5355             code = FLOOR_DIV_EXPR;
5356           else if (code == FLOOR_DIV_EXPR)
5357             code = CEIL_DIV_EXPR;
5358           else if (code != MULT_EXPR
5359                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5360             break;
5361         }
5362
5363       /* If it's a multiply or a division/modulus operation of a multiple
5364          of our constant, do the operation and verify it doesn't overflow.  */
5365       if (code == MULT_EXPR
5366           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5367         {
5368           op1 = const_binop (code, fold_convert (ctype, op1),
5369                              fold_convert (ctype, c), 0);
5370           /* We allow the constant to overflow with wrapping semantics.  */
5371           if (op1 == 0
5372               || (TREE_OVERFLOW (op1) && ! flag_wrapv))
5373             break;
5374         }
5375       else
5376         break;
5377
5378       /* If we have an unsigned type is not a sizetype, we cannot widen
5379          the operation since it will change the result if the original
5380          computation overflowed.  */
5381       if (TYPE_UNSIGNED (ctype)
5382           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5383           && ctype != type)
5384         break;
5385
5386       /* If we were able to eliminate our operation from the first side,
5387          apply our operation to the second side and reform the PLUS.  */
5388       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5389         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5390
5391       /* The last case is if we are a multiply.  In that case, we can
5392          apply the distributive law to commute the multiply and addition
5393          if the multiplication of the constants doesn't overflow.  */
5394       if (code == MULT_EXPR)
5395         return fold_build2 (tcode, ctype,
5396                             fold_build2 (code, ctype,
5397                                          fold_convert (ctype, op0),
5398                                          fold_convert (ctype, c)),
5399                             op1);
5400
5401       break;
5402
5403     case MULT_EXPR:
5404       /* We have a special case here if we are doing something like
5405          (C * 8) % 4 since we know that's zero.  */
5406       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5407            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5408           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5409           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5410         return omit_one_operand (type, integer_zero_node, op0);
5411
5412       /* ... fall through ...  */
5413
5414     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
5415     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
5416       /* If we can extract our operation from the LHS, do so and return a
5417          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
5418          do something only if the second operand is a constant.  */
5419       if (same_p
5420           && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
5421         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5422                             fold_convert (ctype, op1));
5423       else if (tcode == MULT_EXPR && code == MULT_EXPR
5424                && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
5425         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5426                             fold_convert (ctype, t1));
5427       else if (TREE_CODE (op1) != INTEGER_CST)
5428         return 0;
5429
5430       /* If these are the same operation types, we can associate them
5431          assuming no overflow.  */
5432       if (tcode == code
5433           && 0 != (t1 = const_binop (MULT_EXPR, fold_convert (ctype, op1),
5434                                      fold_convert (ctype, c), 0))
5435           && ! TREE_OVERFLOW (t1))
5436         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
5437
5438       /* If these operations "cancel" each other, we have the main
5439          optimizations of this pass, which occur when either constant is a
5440          multiple of the other, in which case we replace this with either an
5441          operation or CODE or TCODE.
5442
5443          If we have an unsigned type that is not a sizetype, we cannot do
5444          this since it will change the result if the original computation
5445          overflowed.  */
5446       if ((! TYPE_UNSIGNED (ctype)
5447            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5448           && ! flag_wrapv
5449           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5450               || (tcode == MULT_EXPR
5451                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5452                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
5453         {
5454           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5455             return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5456                                 fold_convert (ctype,
5457                                               const_binop (TRUNC_DIV_EXPR,
5458                                                            op1, c, 0)));
5459           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
5460             return fold_build2 (code, ctype, fold_convert (ctype, op0),
5461                                 fold_convert (ctype,
5462                                               const_binop (TRUNC_DIV_EXPR,
5463                                                            c, op1, 0)));
5464         }
5465       break;
5466
5467     default:
5468       break;
5469     }
5470
5471   return 0;
5472 }
5473 \f
5474 /* Return a node which has the indicated constant VALUE (either 0 or
5475    1), and is of the indicated TYPE.  */
5476
5477 tree
5478 constant_boolean_node (int value, tree type)
5479 {
5480   if (type == integer_type_node)
5481     return value ? integer_one_node : integer_zero_node;
5482   else if (type == boolean_type_node)
5483     return value ? boolean_true_node : boolean_false_node;
5484   else
5485     return build_int_cst (type, value);
5486 }
5487
5488
5489 /* Return true if expr looks like an ARRAY_REF and set base and
5490    offset to the appropriate trees.  If there is no offset,
5491    offset is set to NULL_TREE.  */
5492
5493 static bool
5494 extract_array_ref (tree expr, tree *base, tree *offset)
5495 {
5496   /* We have to be careful with stripping nops as with the
5497      base type the meaning of the offset can change.  */
5498   tree inner_expr = expr;
5499   STRIP_NOPS (inner_expr);
5500   /* One canonical form is a PLUS_EXPR with the first
5501      argument being an ADDR_EXPR with a possible NOP_EXPR
5502      attached.  */
5503   if (TREE_CODE (expr) == PLUS_EXPR)
5504     {
5505       tree op0 = TREE_OPERAND (expr, 0);
5506       STRIP_NOPS (op0);
5507       if (TREE_CODE (op0) == ADDR_EXPR)
5508         {
5509           *base = TREE_OPERAND (expr, 0);
5510           *offset = TREE_OPERAND (expr, 1);
5511           return true;
5512         }
5513     }
5514   /* Other canonical form is an ADDR_EXPR of an ARRAY_REF,
5515      which we transform into an ADDR_EXPR with appropriate
5516      offset.  For other arguments to the ADDR_EXPR we assume
5517      zero offset and as such do not care about the ADDR_EXPR
5518      type and strip possible nops from it.  */
5519   else if (TREE_CODE (inner_expr) == ADDR_EXPR)
5520     {
5521       tree op0 = TREE_OPERAND (inner_expr, 0);
5522       if (TREE_CODE (op0) == ARRAY_REF)
5523         {
5524           *base = build_fold_addr_expr (TREE_OPERAND (op0, 0));
5525           *offset = TREE_OPERAND (op0, 1);
5526         }
5527       else
5528         {
5529           *base = inner_expr;
5530           *offset = NULL_TREE;
5531         }
5532       return true;
5533     }
5534
5535   return false;
5536 }
5537
5538
5539 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
5540    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
5541    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
5542    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
5543    COND is the first argument to CODE; otherwise (as in the example
5544    given here), it is the second argument.  TYPE is the type of the
5545    original expression.  Return NULL_TREE if no simplification is
5546    possible.  */
5547
5548 static tree
5549 fold_binary_op_with_conditional_arg (enum tree_code code,
5550                                      tree type, tree op0, tree op1,
5551                                      tree cond, tree arg, int cond_first_p)
5552 {
5553   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
5554   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
5555   tree test, true_value, false_value;
5556   tree lhs = NULL_TREE;
5557   tree rhs = NULL_TREE;
5558
5559   /* This transformation is only worthwhile if we don't have to wrap
5560      arg in a SAVE_EXPR, and the operation can be simplified on at least
5561      one of the branches once its pushed inside the COND_EXPR.  */
5562   if (!TREE_CONSTANT (arg))
5563     return NULL_TREE;
5564
5565   if (TREE_CODE (cond) == COND_EXPR)
5566     {
5567       test = TREE_OPERAND (cond, 0);
5568       true_value = TREE_OPERAND (cond, 1);
5569       false_value = TREE_OPERAND (cond, 2);
5570       /* If this operand throws an expression, then it does not make
5571          sense to try to perform a logical or arithmetic operation
5572          involving it.  */
5573       if (VOID_TYPE_P (TREE_TYPE (true_value)))
5574         lhs = true_value;
5575       if (VOID_TYPE_P (TREE_TYPE (false_value)))
5576         rhs = false_value;
5577     }
5578   else
5579     {
5580       tree testtype = TREE_TYPE (cond);
5581       test = cond;
5582       true_value = constant_boolean_node (true, testtype);
5583       false_value = constant_boolean_node (false, testtype);
5584     }
5585
5586   arg = fold_convert (arg_type, arg);
5587   if (lhs == 0)
5588     {
5589       true_value = fold_convert (cond_type, true_value);
5590       if (cond_first_p)
5591         lhs = fold_build2 (code, type, true_value, arg);
5592       else
5593         lhs = fold_build2 (code, type, arg, true_value);
5594     }
5595   if (rhs == 0)
5596     {
5597       false_value = fold_convert (cond_type, false_value);
5598       if (cond_first_p)
5599         rhs = fold_build2 (code, type, false_value, arg);
5600       else
5601         rhs = fold_build2 (code, type, arg, false_value);
5602     }
5603
5604   test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
5605   return fold_convert (type, test);
5606 }
5607
5608 \f
5609 /* Subroutine of fold() that checks for the addition of +/- 0.0.
5610
5611    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
5612    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
5613    ADDEND is the same as X.
5614
5615    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
5616    and finite.  The problematic cases are when X is zero, and its mode
5617    has signed zeros.  In the case of rounding towards -infinity,
5618    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
5619    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
5620
5621 static bool
5622 fold_real_zero_addition_p (tree type, tree addend, int negate)
5623 {
5624   if (!real_zerop (addend))
5625     return false;
5626
5627   /* Don't allow the fold with -fsignaling-nans.  */
5628   if (HONOR_SNANS (TYPE_MODE (type)))
5629     return false;
5630
5631   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
5632   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
5633     return true;
5634
5635   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
5636   if (TREE_CODE (addend) == REAL_CST
5637       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
5638     negate = !negate;
5639
5640   /* The mode has signed zeros, and we have to honor their sign.
5641      In this situation, there is only one case we can return true for.
5642      X - 0 is the same as X unless rounding towards -infinity is
5643      supported.  */
5644   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
5645 }
5646
5647 /* Subroutine of fold() that checks comparisons of built-in math
5648    functions against real constants.
5649
5650    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
5651    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
5652    is the type of the result and ARG0 and ARG1 are the operands of the
5653    comparison.  ARG1 must be a TREE_REAL_CST.
5654
5655    The function returns the constant folded tree if a simplification
5656    can be made, and NULL_TREE otherwise.  */
5657
5658 static tree
5659 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
5660                      tree type, tree arg0, tree arg1)
5661 {
5662   REAL_VALUE_TYPE c;
5663
5664   if (BUILTIN_SQRT_P (fcode))
5665     {
5666       tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
5667       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
5668
5669       c = TREE_REAL_CST (arg1);
5670       if (REAL_VALUE_NEGATIVE (c))
5671         {
5672           /* sqrt(x) < y is always false, if y is negative.  */
5673           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
5674             return omit_one_operand (type, integer_zero_node, arg);
5675
5676           /* sqrt(x) > y is always true, if y is negative and we
5677              don't care about NaNs, i.e. negative values of x.  */
5678           if (code == NE_EXPR || !HONOR_NANS (mode))
5679             return omit_one_operand (type, integer_one_node, arg);
5680
5681           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
5682           return fold_build2 (GE_EXPR, type, arg,
5683                               build_real (TREE_TYPE (arg), dconst0));
5684         }
5685       else if (code == GT_EXPR || code == GE_EXPR)
5686         {
5687           REAL_VALUE_TYPE c2;
5688
5689           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5690           real_convert (&c2, mode, &c2);
5691
5692           if (REAL_VALUE_ISINF (c2))
5693             {
5694               /* sqrt(x) > y is x == +Inf, when y is very large.  */
5695               if (HONOR_INFINITIES (mode))
5696                 return fold_build2 (EQ_EXPR, type, arg,
5697                                     build_real (TREE_TYPE (arg), c2));
5698
5699               /* sqrt(x) > y is always false, when y is very large
5700                  and we don't care about infinities.  */
5701               return omit_one_operand (type, integer_zero_node, arg);
5702             }
5703
5704           /* sqrt(x) > c is the same as x > c*c.  */
5705           return fold_build2 (code, type, arg,
5706                               build_real (TREE_TYPE (arg), c2));
5707         }
5708       else if (code == LT_EXPR || code == LE_EXPR)
5709         {
5710           REAL_VALUE_TYPE c2;
5711
5712           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5713           real_convert (&c2, mode, &c2);
5714
5715           if (REAL_VALUE_ISINF (c2))
5716             {
5717               /* sqrt(x) < y is always true, when y is a very large
5718                  value and we don't care about NaNs or Infinities.  */
5719               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
5720                 return omit_one_operand (type, integer_one_node, arg);
5721
5722               /* sqrt(x) < y is x != +Inf when y is very large and we
5723                  don't care about NaNs.  */
5724               if (! HONOR_NANS (mode))
5725                 return fold_build2 (NE_EXPR, type, arg,
5726                                     build_real (TREE_TYPE (arg), c2));
5727
5728               /* sqrt(x) < y is x >= 0 when y is very large and we
5729                  don't care about Infinities.  */
5730               if (! HONOR_INFINITIES (mode))
5731                 return fold_build2 (GE_EXPR, type, arg,
5732                                     build_real (TREE_TYPE (arg), dconst0));
5733
5734               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
5735               if (lang_hooks.decls.global_bindings_p () != 0
5736                   || CONTAINS_PLACEHOLDER_P (arg))
5737                 return NULL_TREE;
5738
5739               arg = save_expr (arg);
5740               return fold_build2 (TRUTH_ANDIF_EXPR, type,
5741                                   fold_build2 (GE_EXPR, type, arg,
5742                                                build_real (TREE_TYPE (arg),
5743                                                            dconst0)),
5744                                   fold_build2 (NE_EXPR, type, arg,
5745                                                build_real (TREE_TYPE (arg),
5746                                                            c2)));
5747             }
5748
5749           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
5750           if (! HONOR_NANS (mode))
5751             return fold_build2 (code, type, arg,
5752                                 build_real (TREE_TYPE (arg), c2));
5753
5754           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
5755           if (lang_hooks.decls.global_bindings_p () == 0
5756               && ! CONTAINS_PLACEHOLDER_P (arg))
5757             {
5758               arg = save_expr (arg);
5759               return fold_build2 (TRUTH_ANDIF_EXPR, type,
5760                                   fold_build2 (GE_EXPR, type, arg,
5761                                                build_real (TREE_TYPE (arg),
5762                                                            dconst0)),
5763                                   fold_build2 (code, type, arg,
5764                                                build_real (TREE_TYPE (arg),
5765                                                            c2)));
5766             }
5767         }
5768     }
5769
5770   return NULL_TREE;
5771 }
5772
5773 /* Subroutine of fold() that optimizes comparisons against Infinities,
5774    either +Inf or -Inf.
5775
5776    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
5777    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
5778    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
5779
5780    The function returns the constant folded tree if a simplification
5781    can be made, and NULL_TREE otherwise.  */
5782
5783 static tree
5784 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
5785 {
5786   enum machine_mode mode;
5787   REAL_VALUE_TYPE max;
5788   tree temp;
5789   bool neg;
5790
5791   mode = TYPE_MODE (TREE_TYPE (arg0));
5792
5793   /* For negative infinity swap the sense of the comparison.  */
5794   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
5795   if (neg)
5796     code = swap_tree_comparison (code);
5797
5798   switch (code)
5799     {
5800     case GT_EXPR:
5801       /* x > +Inf is always false, if with ignore sNANs.  */
5802       if (HONOR_SNANS (mode))
5803         return NULL_TREE;
5804       return omit_one_operand (type, integer_zero_node, arg0);
5805
5806     case LE_EXPR:
5807       /* x <= +Inf is always true, if we don't case about NaNs.  */
5808       if (! HONOR_NANS (mode))
5809         return omit_one_operand (type, integer_one_node, arg0);
5810
5811       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
5812       if (lang_hooks.decls.global_bindings_p () == 0
5813           && ! CONTAINS_PLACEHOLDER_P (arg0))
5814         {
5815           arg0 = save_expr (arg0);
5816           return fold_build2 (EQ_EXPR, type, arg0, arg0);
5817         }
5818       break;
5819
5820     case EQ_EXPR:
5821     case GE_EXPR:
5822       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
5823       real_maxval (&max, neg, mode);
5824       return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
5825                           arg0, build_real (TREE_TYPE (arg0), max));
5826
5827     case LT_EXPR:
5828       /* x < +Inf is always equal to x <= DBL_MAX.  */
5829       real_maxval (&max, neg, mode);
5830       return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
5831                           arg0, build_real (TREE_TYPE (arg0), max));
5832
5833     case NE_EXPR:
5834       /* x != +Inf is always equal to !(x > DBL_MAX).  */
5835       real_maxval (&max, neg, mode);
5836       if (! HONOR_NANS (mode))
5837         return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
5838                             arg0, build_real (TREE_TYPE (arg0), max));
5839
5840       /* The transformation below creates non-gimple code and thus is
5841          not appropriate if we are in gimple form.  */
5842       if (in_gimple_form)
5843         return NULL_TREE;
5844
5845       temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
5846                           arg0, build_real (TREE_TYPE (arg0), max));
5847       return fold_build1 (TRUTH_NOT_EXPR, type, temp);
5848
5849     default:
5850       break;
5851     }
5852
5853   return NULL_TREE;
5854 }
5855
5856 /* Subroutine of fold() that optimizes comparisons of a division by
5857    a nonzero integer constant against an integer constant, i.e.
5858    X/C1 op C2.
5859
5860    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
5861    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
5862    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
5863
5864    The function returns the constant folded tree if a simplification
5865    can be made, and NULL_TREE otherwise.  */
5866
5867 static tree
5868 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
5869 {
5870   tree prod, tmp, hi, lo;
5871   tree arg00 = TREE_OPERAND (arg0, 0);
5872   tree arg01 = TREE_OPERAND (arg0, 1);
5873   unsigned HOST_WIDE_INT lpart;
5874   HOST_WIDE_INT hpart;
5875   int overflow;
5876
5877   /* We have to do this the hard way to detect unsigned overflow.
5878      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
5879   overflow = mul_double (TREE_INT_CST_LOW (arg01),
5880                          TREE_INT_CST_HIGH (arg01),
5881                          TREE_INT_CST_LOW (arg1),
5882                          TREE_INT_CST_HIGH (arg1), &lpart, &hpart);
5883   prod = build_int_cst_wide (TREE_TYPE (arg00), lpart, hpart);
5884   prod = force_fit_type (prod, -1, overflow, false);
5885
5886   if (TYPE_UNSIGNED (TREE_TYPE (arg0)))
5887     {
5888       tmp = int_const_binop (MINUS_EXPR, arg01, integer_one_node, 0);
5889       lo = prod;
5890
5891       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
5892       overflow = add_double (TREE_INT_CST_LOW (prod),
5893                              TREE_INT_CST_HIGH (prod),
5894                              TREE_INT_CST_LOW (tmp),
5895                              TREE_INT_CST_HIGH (tmp),
5896                              &lpart, &hpart);
5897       hi = build_int_cst_wide (TREE_TYPE (arg00), lpart, hpart);
5898       hi = force_fit_type (hi, -1, overflow | TREE_OVERFLOW (prod),
5899                            TREE_CONSTANT_OVERFLOW (prod));
5900     }
5901   else if (tree_int_cst_sgn (arg01) >= 0)
5902     {
5903       tmp = int_const_binop (MINUS_EXPR, arg01, integer_one_node, 0);
5904       switch (tree_int_cst_sgn (arg1))
5905         {
5906         case -1:
5907           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
5908           hi = prod;
5909           break;
5910
5911         case  0:
5912           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
5913           hi = tmp;
5914           break;
5915
5916         case  1:
5917           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
5918           lo = prod;
5919           break;
5920
5921         default:
5922           gcc_unreachable ();
5923         }
5924     }
5925   else
5926     {
5927       /* A negative divisor reverses the relational operators.  */
5928       code = swap_tree_comparison (code);
5929
5930       tmp = int_const_binop (PLUS_EXPR, arg01, integer_one_node, 0);
5931       switch (tree_int_cst_sgn (arg1))
5932         {
5933         case -1:
5934           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
5935           lo = prod;
5936           break;
5937
5938         case  0:
5939           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
5940           lo = tmp;
5941           break;
5942
5943         case  1:
5944           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
5945           hi = prod;
5946           break;
5947
5948         default:
5949           gcc_unreachable ();
5950         }
5951     }
5952
5953   switch (code)
5954     {
5955     case EQ_EXPR:
5956       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
5957         return omit_one_operand (type, integer_zero_node, arg00);
5958       if (TREE_OVERFLOW (hi))
5959         return fold_build2 (GE_EXPR, type, arg00, lo);
5960       if (TREE_OVERFLOW (lo))
5961         return fold_build2 (LE_EXPR, type, arg00, hi);
5962       return build_range_check (type, arg00, 1, lo, hi);
5963
5964     case NE_EXPR:
5965       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
5966         return omit_one_operand (type, integer_one_node, arg00);
5967       if (TREE_OVERFLOW (hi))
5968         return fold_build2 (LT_EXPR, type, arg00, lo);
5969       if (TREE_OVERFLOW (lo))
5970         return fold_build2 (GT_EXPR, type, arg00, hi);
5971       return build_range_check (type, arg00, 0, lo, hi);
5972
5973     case LT_EXPR:
5974       if (TREE_OVERFLOW (lo))
5975         return omit_one_operand (type, integer_zero_node, arg00);
5976       return fold_build2 (LT_EXPR, type, arg00, lo);
5977
5978     case LE_EXPR:
5979       if (TREE_OVERFLOW (hi))
5980         return omit_one_operand (type, integer_one_node, arg00);
5981       return fold_build2 (LE_EXPR, type, arg00, hi);
5982
5983     case GT_EXPR:
5984       if (TREE_OVERFLOW (hi))
5985         return omit_one_operand (type, integer_zero_node, arg00);
5986       return fold_build2 (GT_EXPR, type, arg00, hi);
5987
5988     case GE_EXPR:
5989       if (TREE_OVERFLOW (lo))
5990         return omit_one_operand (type, integer_one_node, arg00);
5991       return fold_build2 (GE_EXPR, type, arg00, lo);
5992
5993     default:
5994       break;
5995     }
5996
5997   return NULL_TREE;
5998 }
5999
6000
6001 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6002    equality/inequality test, then return a simplified form of the test
6003    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6004    result type.  */
6005
6006 static tree
6007 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6008                                      tree result_type)
6009 {
6010   /* If this is testing a single bit, we can optimize the test.  */
6011   if ((code == NE_EXPR || code == EQ_EXPR)
6012       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6013       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6014     {
6015       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6016          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6017       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6018
6019       if (arg00 != NULL_TREE
6020           /* This is only a win if casting to a signed type is cheap,
6021              i.e. when arg00's type is not a partial mode.  */
6022           && TYPE_PRECISION (TREE_TYPE (arg00))
6023              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6024         {
6025           tree stype = lang_hooks.types.signed_type (TREE_TYPE (arg00));
6026           return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6027                               result_type, fold_convert (stype, arg00),
6028                               fold_convert (stype, integer_zero_node));
6029         }
6030     }
6031
6032   return NULL_TREE;
6033 }
6034
6035 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6036    equality/inequality test, then return a simplified form of
6037    the test using shifts and logical operations.  Otherwise return
6038    NULL.  TYPE is the desired result type.  */
6039
6040 tree
6041 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6042                       tree result_type)
6043 {
6044   /* If this is testing a single bit, we can optimize the test.  */
6045   if ((code == NE_EXPR || code == EQ_EXPR)
6046       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6047       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6048     {
6049       tree inner = TREE_OPERAND (arg0, 0);
6050       tree type = TREE_TYPE (arg0);
6051       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6052       enum machine_mode operand_mode = TYPE_MODE (type);
6053       int ops_unsigned;
6054       tree signed_type, unsigned_type, intermediate_type;
6055       tree tem;
6056
6057       /* First, see if we can fold the single bit test into a sign-bit
6058          test.  */
6059       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6060                                                  result_type);
6061       if (tem)
6062         return tem;
6063
6064       /* Otherwise we have (A & C) != 0 where C is a single bit,
6065          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6066          Similarly for (A & C) == 0.  */
6067
6068       /* If INNER is a right shift of a constant and it plus BITNUM does
6069          not overflow, adjust BITNUM and INNER.  */
6070       if (TREE_CODE (inner) == RSHIFT_EXPR
6071           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6072           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6073           && bitnum < TYPE_PRECISION (type)
6074           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6075                                    bitnum - TYPE_PRECISION (type)))
6076         {
6077           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6078           inner = TREE_OPERAND (inner, 0);
6079         }
6080
6081       /* If we are going to be able to omit the AND below, we must do our
6082          operations as unsigned.  If we must use the AND, we have a choice.
6083          Normally unsigned is faster, but for some machines signed is.  */
6084 #ifdef LOAD_EXTEND_OP
6085       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND 
6086                       && !flag_syntax_only) ? 0 : 1;
6087 #else
6088       ops_unsigned = 1;
6089 #endif
6090
6091       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6092       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6093       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6094       inner = fold_convert (intermediate_type, inner);
6095
6096       if (bitnum != 0)
6097         inner = build2 (RSHIFT_EXPR, intermediate_type,
6098                         inner, size_int (bitnum));
6099
6100       if (code == EQ_EXPR)
6101         inner = fold_build2 (BIT_XOR_EXPR, intermediate_type,
6102                              inner, integer_one_node);
6103
6104       /* Put the AND last so it can combine with more things.  */
6105       inner = build2 (BIT_AND_EXPR, intermediate_type,
6106                       inner, integer_one_node);
6107
6108       /* Make sure to return the proper type.  */
6109       inner = fold_convert (result_type, inner);
6110
6111       return inner;
6112     }
6113   return NULL_TREE;
6114 }
6115
6116 /* Check whether we are allowed to reorder operands arg0 and arg1,
6117    such that the evaluation of arg1 occurs before arg0.  */
6118
6119 static bool
6120 reorder_operands_p (tree arg0, tree arg1)
6121 {
6122   if (! flag_evaluation_order)
6123       return true;
6124   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6125     return true;
6126   return ! TREE_SIDE_EFFECTS (arg0)
6127          && ! TREE_SIDE_EFFECTS (arg1);
6128 }
6129
6130 /* Test whether it is preferable two swap two operands, ARG0 and
6131    ARG1, for example because ARG0 is an integer constant and ARG1
6132    isn't.  If REORDER is true, only recommend swapping if we can
6133    evaluate the operands in reverse order.  */
6134
6135 bool
6136 tree_swap_operands_p (tree arg0, tree arg1, bool reorder)
6137 {
6138   STRIP_SIGN_NOPS (arg0);
6139   STRIP_SIGN_NOPS (arg1);
6140
6141   if (TREE_CODE (arg1) == INTEGER_CST)
6142     return 0;
6143   if (TREE_CODE (arg0) == INTEGER_CST)
6144     return 1;
6145
6146   if (TREE_CODE (arg1) == REAL_CST)
6147     return 0;
6148   if (TREE_CODE (arg0) == REAL_CST)
6149     return 1;
6150
6151   if (TREE_CODE (arg1) == COMPLEX_CST)
6152     return 0;
6153   if (TREE_CODE (arg0) == COMPLEX_CST)
6154     return 1;
6155
6156   if (TREE_CONSTANT (arg1))
6157     return 0;
6158   if (TREE_CONSTANT (arg0))
6159     return 1;
6160
6161   if (optimize_size)
6162     return 0;
6163
6164   if (reorder && flag_evaluation_order
6165       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6166     return 0;
6167
6168   if (DECL_P (arg1))
6169     return 0;
6170   if (DECL_P (arg0))
6171     return 1;
6172
6173   /* It is preferable to swap two SSA_NAME to ensure a canonical form
6174      for commutative and comparison operators.  Ensuring a canonical
6175      form allows the optimizers to find additional redundancies without
6176      having to explicitly check for both orderings.  */
6177   if (TREE_CODE (arg0) == SSA_NAME
6178       && TREE_CODE (arg1) == SSA_NAME
6179       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6180     return 1;
6181
6182   return 0;
6183 }
6184
6185 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6186    ARG0 is extended to a wider type.  */
6187
6188 static tree
6189 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
6190 {
6191   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6192   tree arg1_unw;
6193   tree shorter_type, outer_type;
6194   tree min, max;
6195   bool above, below;
6196
6197   if (arg0_unw == arg0)
6198     return NULL_TREE;
6199   shorter_type = TREE_TYPE (arg0_unw);
6200
6201 #ifdef HAVE_canonicalize_funcptr_for_compare
6202   /* Disable this optimization if we're casting a function pointer
6203      type on targets that require function pointer canonicalization.  */
6204   if (HAVE_canonicalize_funcptr_for_compare
6205       && TREE_CODE (shorter_type) == POINTER_TYPE
6206       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6207     return NULL_TREE;
6208 #endif
6209
6210   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6211     return NULL_TREE;
6212
6213   arg1_unw = get_unwidened (arg1, shorter_type);
6214   if (!arg1_unw)
6215     return NULL_TREE;
6216
6217   /* If possible, express the comparison in the shorter mode.  */
6218   if ((code == EQ_EXPR || code == NE_EXPR
6219        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6220       && (TREE_TYPE (arg1_unw) == shorter_type
6221           || (TREE_CODE (arg1_unw) == INTEGER_CST
6222               && (TREE_CODE (shorter_type) == INTEGER_TYPE
6223                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6224               && int_fits_type_p (arg1_unw, shorter_type))))
6225     return fold_build2 (code, type, arg0_unw,
6226                        fold_convert (shorter_type, arg1_unw));
6227
6228   if (TREE_CODE (arg1_unw) != INTEGER_CST)
6229     return NULL_TREE;
6230
6231   /* If we are comparing with the integer that does not fit into the range
6232      of the shorter type, the result is known.  */
6233   outer_type = TREE_TYPE (arg1_unw);
6234   min = lower_bound_in_type (outer_type, shorter_type);
6235   max = upper_bound_in_type (outer_type, shorter_type);
6236
6237   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6238                                                    max, arg1_unw));
6239   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6240                                                    arg1_unw, min));
6241
6242   switch (code)
6243     {
6244     case EQ_EXPR:
6245       if (above || below)
6246         return omit_one_operand (type, integer_zero_node, arg0);
6247       break;
6248
6249     case NE_EXPR:
6250       if (above || below)
6251         return omit_one_operand (type, integer_one_node, arg0);
6252       break;
6253
6254     case LT_EXPR:
6255     case LE_EXPR:
6256       if (above)
6257         return omit_one_operand (type, integer_one_node, arg0);
6258       else if (below)
6259         return omit_one_operand (type, integer_zero_node, arg0);
6260
6261     case GT_EXPR:
6262     case GE_EXPR:
6263       if (above)
6264         return omit_one_operand (type, integer_zero_node, arg0);
6265       else if (below)
6266         return omit_one_operand (type, integer_one_node, arg0);
6267
6268     default:
6269       break;
6270     }
6271
6272   return NULL_TREE;
6273 }
6274
6275 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6276    ARG0 just the signedness is changed.  */
6277
6278 static tree
6279 fold_sign_changed_comparison (enum tree_code code, tree type,
6280                               tree arg0, tree arg1)
6281 {
6282   tree arg0_inner, tmp;
6283   tree inner_type, outer_type;
6284
6285   if (TREE_CODE (arg0) != NOP_EXPR
6286       && TREE_CODE (arg0) != CONVERT_EXPR)
6287     return NULL_TREE;
6288
6289   outer_type = TREE_TYPE (arg0);
6290   arg0_inner = TREE_OPERAND (arg0, 0);
6291   inner_type = TREE_TYPE (arg0_inner);
6292
6293 #ifdef HAVE_canonicalize_funcptr_for_compare
6294   /* Disable this optimization if we're casting a function pointer
6295      type on targets that require function pointer canonicalization.  */
6296   if (HAVE_canonicalize_funcptr_for_compare
6297       && TREE_CODE (inner_type) == POINTER_TYPE
6298       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6299     return NULL_TREE;
6300 #endif
6301
6302   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6303     return NULL_TREE;
6304
6305   if (TREE_CODE (arg1) != INTEGER_CST
6306       && !((TREE_CODE (arg1) == NOP_EXPR
6307             || TREE_CODE (arg1) == CONVERT_EXPR)
6308            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6309     return NULL_TREE;
6310
6311   if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6312       && code != NE_EXPR
6313       && code != EQ_EXPR)
6314     return NULL_TREE;
6315
6316   if (TREE_CODE (arg1) == INTEGER_CST)
6317     {
6318       tmp = build_int_cst_wide (inner_type,
6319                                 TREE_INT_CST_LOW (arg1),
6320                                 TREE_INT_CST_HIGH (arg1));
6321       arg1 = force_fit_type (tmp, 0,
6322                              TREE_OVERFLOW (arg1),
6323                              TREE_CONSTANT_OVERFLOW (arg1));
6324     }
6325   else
6326     arg1 = fold_convert (inner_type, arg1);
6327
6328   return fold_build2 (code, type, arg0_inner, arg1);
6329 }
6330
6331 /* Tries to replace &a[idx] CODE s * delta with &a[idx CODE delta], if s is
6332    step of the array.  Reconstructs s and delta in the case of s * delta
6333    being an integer constant (and thus already folded).
6334    ADDR is the address. MULT is the multiplicative expression.
6335    If the function succeeds, the new address expression is returned.  Otherwise
6336    NULL_TREE is returned.  */
6337
6338 static tree
6339 try_move_mult_to_index (enum tree_code code, tree addr, tree op1)
6340 {
6341   tree s, delta, step;
6342   tree ref = TREE_OPERAND (addr, 0), pref;
6343   tree ret, pos;
6344   tree itype;
6345
6346   /* Canonicalize op1 into a possibly non-constant delta
6347      and an INTEGER_CST s.  */
6348   if (TREE_CODE (op1) == MULT_EXPR)
6349     {
6350       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6351
6352       STRIP_NOPS (arg0);
6353       STRIP_NOPS (arg1);
6354   
6355       if (TREE_CODE (arg0) == INTEGER_CST)
6356         {
6357           s = arg0;
6358           delta = arg1;
6359         }
6360       else if (TREE_CODE (arg1) == INTEGER_CST)
6361         {
6362           s = arg1;
6363           delta = arg0;
6364         }
6365       else
6366         return NULL_TREE;
6367     }
6368   else if (TREE_CODE (op1) == INTEGER_CST)
6369     {
6370       delta = op1;
6371       s = NULL_TREE;
6372     }
6373   else
6374     {
6375       /* Simulate we are delta * 1.  */
6376       delta = op1;
6377       s = integer_one_node;
6378     }
6379
6380   for (;; ref = TREE_OPERAND (ref, 0))
6381     {
6382       if (TREE_CODE (ref) == ARRAY_REF)
6383         {
6384           itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6385           if (! itype)
6386             continue;
6387
6388           step = array_ref_element_size (ref);
6389           if (TREE_CODE (step) != INTEGER_CST)
6390             continue;
6391
6392           if (s)
6393             {
6394               if (! tree_int_cst_equal (step, s))
6395                 continue;
6396             }
6397           else
6398             {
6399               /* Try if delta is a multiple of step.  */
6400               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, delta, step);
6401               if (! tmp)
6402                 continue;
6403               delta = tmp;
6404             }
6405
6406           break;
6407         }
6408
6409       if (!handled_component_p (ref))
6410         return NULL_TREE;
6411     }
6412
6413   /* We found the suitable array reference.  So copy everything up to it,
6414      and replace the index.  */
6415
6416   pref = TREE_OPERAND (addr, 0);
6417   ret = copy_node (pref);
6418   pos = ret;
6419
6420   while (pref != ref)
6421     {
6422       pref = TREE_OPERAND (pref, 0);
6423       TREE_OPERAND (pos, 0) = copy_node (pref);
6424       pos = TREE_OPERAND (pos, 0);
6425     }
6426
6427   TREE_OPERAND (pos, 1) = fold_build2 (code, itype,
6428                                        fold_convert (itype,
6429                                                      TREE_OPERAND (pos, 1)),
6430                                        fold_convert (itype, delta));
6431
6432   return build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
6433 }
6434
6435
6436 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
6437    means A >= Y && A != MAX, but in this case we know that
6438    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
6439
6440 static tree
6441 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
6442 {
6443   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6444
6445   if (TREE_CODE (bound) == LT_EXPR)
6446     a = TREE_OPERAND (bound, 0);
6447   else if (TREE_CODE (bound) == GT_EXPR)
6448     a = TREE_OPERAND (bound, 1);
6449   else
6450     return NULL_TREE;
6451
6452   typea = TREE_TYPE (a);
6453   if (!INTEGRAL_TYPE_P (typea)
6454       && !POINTER_TYPE_P (typea))
6455     return NULL_TREE;
6456
6457   if (TREE_CODE (ineq) == LT_EXPR)
6458     {
6459       a1 = TREE_OPERAND (ineq, 1);
6460       y = TREE_OPERAND (ineq, 0);
6461     }
6462   else if (TREE_CODE (ineq) == GT_EXPR)
6463     {
6464       a1 = TREE_OPERAND (ineq, 0);
6465       y = TREE_OPERAND (ineq, 1);
6466     }
6467   else
6468     return NULL_TREE;
6469
6470   if (TREE_TYPE (a1) != typea)
6471     return NULL_TREE;
6472
6473   diff = fold_build2 (MINUS_EXPR, typea, a1, a);
6474   if (!integer_onep (diff))
6475     return NULL_TREE;
6476
6477   return fold_build2 (GE_EXPR, type, a, y);
6478 }
6479
6480 /* Fold complex addition when both components are accessible by parts.
6481    Return non-null if successful.  CODE should be PLUS_EXPR for addition,
6482    or MINUS_EXPR for subtraction.  */
6483
6484 static tree
6485 fold_complex_add (tree type, tree ac, tree bc, enum tree_code code)
6486 {
6487   tree ar, ai, br, bi, rr, ri, inner_type;
6488
6489   if (TREE_CODE (ac) == COMPLEX_EXPR)
6490     ar = TREE_OPERAND (ac, 0), ai = TREE_OPERAND (ac, 1);
6491   else if (TREE_CODE (ac) == COMPLEX_CST)
6492     ar = TREE_REALPART (ac), ai = TREE_IMAGPART (ac);
6493   else
6494     return NULL;
6495
6496   if (TREE_CODE (bc) == COMPLEX_EXPR)
6497     br = TREE_OPERAND (bc, 0), bi = TREE_OPERAND (bc, 1);
6498   else if (TREE_CODE (bc) == COMPLEX_CST)
6499     br = TREE_REALPART (bc), bi = TREE_IMAGPART (bc);
6500   else
6501     return NULL;
6502
6503   inner_type = TREE_TYPE (type);
6504
6505   rr = fold_build2 (code, inner_type, ar, br); 
6506   ri = fold_build2 (code, inner_type, ai, bi); 
6507
6508   return fold_build2 (COMPLEX_EXPR, type, rr, ri);
6509 }
6510
6511 /* Perform some simplifications of complex multiplication when one or more
6512    of the components are constants or zeros.  Return non-null if successful.  */
6513
6514 tree
6515 fold_complex_mult_parts (tree type, tree ar, tree ai, tree br, tree bi)
6516 {
6517   tree rr, ri, inner_type, zero;
6518   bool ar0, ai0, br0, bi0, bi1;
6519
6520   inner_type = TREE_TYPE (type);
6521   zero = NULL;
6522
6523   if (SCALAR_FLOAT_TYPE_P (inner_type))
6524     {
6525       ar0 = ai0 = br0 = bi0 = bi1 = false;
6526
6527       /* We're only interested in +0.0 here, thus we don't use real_zerop.  */
6528
6529       if (TREE_CODE (ar) == REAL_CST
6530           && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ar), dconst0))
6531         ar0 = true, zero = ar;
6532
6533       if (TREE_CODE (ai) == REAL_CST
6534           && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst0))
6535         ai0 = true, zero = ai;
6536
6537       if (TREE_CODE (br) == REAL_CST
6538           && REAL_VALUES_IDENTICAL (TREE_REAL_CST (br), dconst0))
6539         br0 = true, zero = br;
6540
6541       if (TREE_CODE (bi) == REAL_CST)
6542         {
6543           if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (bi), dconst0))
6544             bi0 = true, zero = bi;
6545           else if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (bi), dconst1))
6546             bi1 = true;
6547         }
6548     }
6549   else
6550     {
6551       ar0 = integer_zerop (ar);
6552       if (ar0)
6553         zero = ar;
6554       ai0 = integer_zerop (ai);
6555       if (ai0)
6556         zero = ai;
6557       br0 = integer_zerop (br);
6558       if (br0)
6559         zero = br;
6560       bi0 = integer_zerop (bi);
6561       if (bi0)
6562         {
6563           zero = bi;
6564           bi1 = false;
6565         }
6566       else
6567         bi1 = integer_onep (bi);
6568     }
6569
6570   /* We won't optimize anything below unless something is zero.  */
6571   if (zero == NULL)
6572     return NULL;
6573
6574   if (ai0 && br0 && bi1)
6575     {
6576       rr = zero;
6577       ri = ar;
6578     }
6579   else if (ai0 && bi0)
6580     {
6581       rr = fold_build2 (MULT_EXPR, inner_type, ar, br);
6582       ri = zero;
6583     }
6584   else if (ai0 && br0)
6585     {
6586       rr = zero;
6587       ri = fold_build2 (MULT_EXPR, inner_type, ar, bi);
6588     }
6589   else if (ar0 && bi0)
6590     {
6591       rr = zero;
6592       ri = fold_build2 (MULT_EXPR, inner_type, ai, br);
6593     }
6594   else if (ar0 && br0)
6595     {
6596       rr = fold_build2 (MULT_EXPR, inner_type, ai, bi);
6597       rr = fold_build1 (NEGATE_EXPR, inner_type, rr);
6598       ri = zero;
6599     }
6600   else if (bi0)
6601     {
6602       rr = fold_build2 (MULT_EXPR, inner_type, ar, br);
6603       ri = fold_build2 (MULT_EXPR, inner_type, ai, br);
6604     }
6605   else if (ai0)
6606     {
6607       rr = fold_build2 (MULT_EXPR, inner_type, ar, br);
6608       ri = fold_build2 (MULT_EXPR, inner_type, ar, bi);
6609     }
6610   else if (br0)
6611     {
6612       rr = fold_build2 (MULT_EXPR, inner_type, ai, bi);
6613       rr = fold_build1 (NEGATE_EXPR, inner_type, rr);
6614       ri = fold_build2 (MULT_EXPR, inner_type, ar, bi);
6615     }
6616   else if (ar0)
6617     {
6618       rr = fold_build2 (MULT_EXPR, inner_type, ai, bi);
6619       rr = fold_build1 (NEGATE_EXPR, inner_type, rr);
6620       ri = fold_build2 (MULT_EXPR, inner_type, ai, br);
6621     }
6622   else
6623     return NULL;
6624
6625   return fold_build2 (COMPLEX_EXPR, type, rr, ri);
6626 }
6627
6628 static tree
6629 fold_complex_mult (tree type, tree ac, tree bc)
6630 {
6631   tree ar, ai, br, bi;
6632
6633   if (TREE_CODE (ac) == COMPLEX_EXPR)
6634     ar = TREE_OPERAND (ac, 0), ai = TREE_OPERAND (ac, 1);
6635   else if (TREE_CODE (ac) == COMPLEX_CST)
6636     ar = TREE_REALPART (ac), ai = TREE_IMAGPART (ac);
6637   else
6638     return NULL;
6639
6640   if (TREE_CODE (bc) == COMPLEX_EXPR)
6641     br = TREE_OPERAND (bc, 0), bi = TREE_OPERAND (bc, 1);
6642   else if (TREE_CODE (bc) == COMPLEX_CST)
6643     br = TREE_REALPART (bc), bi = TREE_IMAGPART (bc);
6644   else
6645     return NULL;
6646
6647   return fold_complex_mult_parts (type, ar, ai, br, bi);
6648 }
6649
6650 /* Perform some simplifications of complex division when one or more of
6651    the components are constants or zeros.  Return non-null if successful.  */
6652
6653 tree
6654 fold_complex_div_parts (tree type, tree ar, tree ai, tree br, tree bi,
6655                         enum tree_code code)
6656 {
6657   tree rr, ri, inner_type, zero;
6658   bool ar0, ai0, br0, bi0, bi1;
6659
6660   inner_type = TREE_TYPE (type);
6661   zero = NULL;
6662
6663   if (SCALAR_FLOAT_TYPE_P (inner_type))
6664     {
6665       ar0 = ai0 = br0 = bi0 = bi1 = false;
6666
6667       /* We're only interested in +0.0 here, thus we don't use real_zerop.  */
6668
6669       if (TREE_CODE (ar) == REAL_CST
6670           && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ar), dconst0))
6671         ar0 = true, zero = ar;
6672
6673       if (TREE_CODE (ai) == REAL_CST
6674           && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst0))
6675         ai0 = true, zero = ai;
6676
6677       if (TREE_CODE (br) == REAL_CST
6678           && REAL_VALUES_IDENTICAL (TREE_REAL_CST (br), dconst0))
6679         br0 = true, zero = br;
6680
6681       if (TREE_CODE (bi) == REAL_CST)
6682         {
6683           if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (bi), dconst0))
6684             bi0 = true, zero = bi;
6685           else if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (bi), dconst1))
6686             bi1 = true;
6687         }
6688     }
6689   else
6690     {
6691       ar0 = integer_zerop (ar);
6692       if (ar0)
6693         zero = ar;
6694       ai0 = integer_zerop (ai);
6695       if (ai0)
6696         zero = ai;
6697       br0 = integer_zerop (br);
6698       if (br0)
6699         zero = br;
6700       bi0 = integer_zerop (bi);
6701       if (bi0)
6702         {
6703           zero = bi;
6704           bi1 = false;
6705         }
6706       else
6707         bi1 = integer_onep (bi);
6708     }
6709
6710   /* We won't optimize anything below unless something is zero.  */
6711   if (zero == NULL)
6712     return NULL;
6713
6714   if (ai0 && bi0)
6715     {
6716       rr = fold_build2 (code, inner_type, ar, br);
6717       ri = zero;
6718     }
6719   else if (ai0 && br0)
6720     {
6721       rr = zero;
6722       ri = fold_build2 (code, inner_type, ar, bi);
6723       ri = fold_build1 (NEGATE_EXPR, inner_type, ri);
6724     }
6725   else if (ar0 && bi0)
6726     {
6727       rr = zero;
6728       ri = fold_build2 (code, inner_type, ai, br);
6729     }
6730   else if (ar0 && br0)
6731     {
6732       rr = fold_build2 (code, inner_type, ai, bi);
6733       ri = zero;
6734     }
6735   else if (bi0)
6736     {
6737       rr = fold_build2 (code, inner_type, ar, br);
6738       ri = fold_build2 (code, inner_type, ai, br);
6739     }
6740   else if (br0)
6741     {
6742       rr = fold_build2 (code, inner_type, ai, bi);
6743       ri = fold_build2 (code, inner_type, ar, bi);
6744       ri = fold_build1 (NEGATE_EXPR, inner_type, ri);
6745     }
6746   else
6747     return NULL;
6748
6749   return fold_build2 (COMPLEX_EXPR, type, rr, ri);
6750 }
6751
6752 static tree
6753 fold_complex_div (tree type, tree ac, tree bc, enum tree_code code)
6754 {
6755   tree ar, ai, br, bi;
6756
6757   if (TREE_CODE (ac) == COMPLEX_EXPR)
6758     ar = TREE_OPERAND (ac, 0), ai = TREE_OPERAND (ac, 1);
6759   else if (TREE_CODE (ac) == COMPLEX_CST)
6760     ar = TREE_REALPART (ac), ai = TREE_IMAGPART (ac);
6761   else
6762     return NULL;
6763
6764   if (TREE_CODE (bc) == COMPLEX_EXPR)
6765     br = TREE_OPERAND (bc, 0), bi = TREE_OPERAND (bc, 1);
6766   else if (TREE_CODE (bc) == COMPLEX_CST)
6767     br = TREE_REALPART (bc), bi = TREE_IMAGPART (bc);
6768   else
6769     return NULL;
6770
6771   return fold_complex_div_parts (type, ar, ai, br, bi, code);
6772 }
6773
6774 /* Fold a unary expression of code CODE and type TYPE with operand
6775    OP0.  Return the folded expression if folding is successful.
6776    Otherwise, return NULL_TREE.  */
6777
6778 tree
6779 fold_unary (enum tree_code code, tree type, tree op0)
6780 {
6781   tree tem;
6782   tree arg0;
6783   enum tree_code_class kind = TREE_CODE_CLASS (code);
6784
6785   gcc_assert (IS_EXPR_CODE_CLASS (kind)
6786               && TREE_CODE_LENGTH (code) == 1);
6787
6788   arg0 = op0;
6789   if (arg0)
6790     {
6791       if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
6792         {
6793           /* Don't use STRIP_NOPS, because signedness of argument type matters.  */
6794           STRIP_SIGN_NOPS (arg0);
6795         }
6796       else
6797         {
6798           /* Strip any conversions that don't change the mode.  This
6799              is safe for every expression, except for a comparison
6800              expression because its signedness is derived from its
6801              operands.
6802
6803              Note that this is done as an internal manipulation within
6804              the constant folder, in order to find the simplest
6805              representation of the arguments so that their form can be
6806              studied.  In any cases, the appropriate type conversions
6807              should be put back in the tree that will get out of the
6808              constant folder.  */
6809           STRIP_NOPS (arg0);
6810         }
6811     }
6812
6813   if (TREE_CODE_CLASS (code) == tcc_unary)
6814     {
6815       if (TREE_CODE (arg0) == COMPOUND_EXPR)
6816         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
6817                        fold_build1 (code, type, TREE_OPERAND (arg0, 1)));
6818       else if (TREE_CODE (arg0) == COND_EXPR)
6819         {
6820           tree arg01 = TREE_OPERAND (arg0, 1);
6821           tree arg02 = TREE_OPERAND (arg0, 2);
6822           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
6823             arg01 = fold_build1 (code, type, arg01);
6824           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
6825             arg02 = fold_build1 (code, type, arg02);
6826           tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
6827                              arg01, arg02);
6828
6829           /* If this was a conversion, and all we did was to move into
6830              inside the COND_EXPR, bring it back out.  But leave it if
6831              it is a conversion from integer to integer and the
6832              result precision is no wider than a word since such a
6833              conversion is cheap and may be optimized away by combine,
6834              while it couldn't if it were outside the COND_EXPR.  Then return
6835              so we don't get into an infinite recursion loop taking the
6836              conversion out and then back in.  */
6837
6838           if ((code == NOP_EXPR || code == CONVERT_EXPR
6839                || code == NON_LVALUE_EXPR)
6840               && TREE_CODE (tem) == COND_EXPR
6841               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
6842               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
6843               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
6844               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
6845               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
6846                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
6847               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
6848                      && (INTEGRAL_TYPE_P
6849                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
6850                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
6851                   || flag_syntax_only))
6852             tem = build1 (code, type,
6853                           build3 (COND_EXPR,
6854                                   TREE_TYPE (TREE_OPERAND
6855                                              (TREE_OPERAND (tem, 1), 0)),
6856                                   TREE_OPERAND (tem, 0),
6857                                   TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
6858                                   TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
6859           return tem;
6860         }
6861       else if (COMPARISON_CLASS_P (arg0))
6862         {
6863           if (TREE_CODE (type) == BOOLEAN_TYPE)
6864             {
6865               arg0 = copy_node (arg0);
6866               TREE_TYPE (arg0) = type;
6867               return arg0;
6868             }
6869           else if (TREE_CODE (type) != INTEGER_TYPE)
6870             return fold_build3 (COND_EXPR, type, arg0,
6871                                 fold_build1 (code, type,
6872                                              integer_one_node),
6873                                 fold_build1 (code, type,
6874                                              integer_zero_node));
6875         }
6876    }
6877
6878   switch (code)
6879     {
6880     case NOP_EXPR:
6881     case FLOAT_EXPR:
6882     case CONVERT_EXPR:
6883     case FIX_TRUNC_EXPR:
6884     case FIX_CEIL_EXPR:
6885     case FIX_FLOOR_EXPR:
6886     case FIX_ROUND_EXPR:
6887       if (TREE_TYPE (op0) == type)
6888         return op0;
6889
6890       /* Handle cases of two conversions in a row.  */
6891       if (TREE_CODE (op0) == NOP_EXPR
6892           || TREE_CODE (op0) == CONVERT_EXPR)
6893         {
6894           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
6895           tree inter_type = TREE_TYPE (op0);
6896           int inside_int = INTEGRAL_TYPE_P (inside_type);
6897           int inside_ptr = POINTER_TYPE_P (inside_type);
6898           int inside_float = FLOAT_TYPE_P (inside_type);
6899           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
6900           unsigned int inside_prec = TYPE_PRECISION (inside_type);
6901           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
6902           int inter_int = INTEGRAL_TYPE_P (inter_type);
6903           int inter_ptr = POINTER_TYPE_P (inter_type);
6904           int inter_float = FLOAT_TYPE_P (inter_type);
6905           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
6906           unsigned int inter_prec = TYPE_PRECISION (inter_type);
6907           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
6908           int final_int = INTEGRAL_TYPE_P (type);
6909           int final_ptr = POINTER_TYPE_P (type);
6910           int final_float = FLOAT_TYPE_P (type);
6911           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
6912           unsigned int final_prec = TYPE_PRECISION (type);
6913           int final_unsignedp = TYPE_UNSIGNED (type);
6914
6915           /* In addition to the cases of two conversions in a row
6916              handled below, if we are converting something to its own
6917              type via an object of identical or wider precision, neither
6918              conversion is needed.  */
6919           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
6920               && ((inter_int && final_int) || (inter_float && final_float))
6921               && inter_prec >= final_prec)
6922             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
6923
6924           /* Likewise, if the intermediate and final types are either both
6925              float or both integer, we don't need the middle conversion if
6926              it is wider than the final type and doesn't change the signedness
6927              (for integers).  Avoid this if the final type is a pointer
6928              since then we sometimes need the inner conversion.  Likewise if
6929              the outer has a precision not equal to the size of its mode.  */
6930           if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
6931                || (inter_float && inside_float)
6932                || (inter_vec && inside_vec))
6933               && inter_prec >= inside_prec
6934               && (inter_float || inter_vec
6935                   || inter_unsignedp == inside_unsignedp)
6936               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
6937                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
6938               && ! final_ptr
6939               && (! final_vec || inter_prec == inside_prec))
6940             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
6941
6942           /* If we have a sign-extension of a zero-extended value, we can
6943              replace that by a single zero-extension.  */
6944           if (inside_int && inter_int && final_int
6945               && inside_prec < inter_prec && inter_prec < final_prec
6946               && inside_unsignedp && !inter_unsignedp)
6947             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
6948
6949           /* Two conversions in a row are not needed unless:
6950              - some conversion is floating-point (overstrict for now), or
6951              - some conversion is a vector (overstrict for now), or
6952              - the intermediate type is narrower than both initial and
6953                final, or
6954              - the intermediate type and innermost type differ in signedness,
6955                and the outermost type is wider than the intermediate, or
6956              - the initial type is a pointer type and the precisions of the
6957                intermediate and final types differ, or
6958              - the final type is a pointer type and the precisions of the
6959                initial and intermediate types differ.  */
6960           if (! inside_float && ! inter_float && ! final_float
6961               && ! inside_vec && ! inter_vec && ! final_vec
6962               && (inter_prec > inside_prec || inter_prec > final_prec)
6963               && ! (inside_int && inter_int
6964                     && inter_unsignedp != inside_unsignedp
6965                     && inter_prec < final_prec)
6966               && ((inter_unsignedp && inter_prec > inside_prec)
6967                   == (final_unsignedp && final_prec > inter_prec))
6968               && ! (inside_ptr && inter_prec != final_prec)
6969               && ! (final_ptr && inside_prec != inter_prec)
6970               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
6971                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
6972               && ! final_ptr)
6973             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
6974         }
6975
6976       if (TREE_CODE (op0) == MODIFY_EXPR
6977           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
6978           /* Detect assigning a bitfield.  */
6979           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
6980                && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
6981         {
6982           /* Don't leave an assignment inside a conversion
6983              unless assigning a bitfield.  */
6984           tem = fold_build1 (code, type, TREE_OPERAND (op0, 1));
6985           /* First do the assignment, then return converted constant.  */
6986           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
6987           TREE_NO_WARNING (tem) = 1;
6988           TREE_USED (tem) = 1;
6989           return tem;
6990         }
6991
6992       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
6993          constants (if x has signed type, the sign bit cannot be set
6994          in c).  This folds extension into the BIT_AND_EXPR.  */
6995       if (INTEGRAL_TYPE_P (type)
6996           && TREE_CODE (type) != BOOLEAN_TYPE
6997           && TREE_CODE (op0) == BIT_AND_EXPR
6998           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
6999         {
7000           tree and = op0;
7001           tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
7002           int change = 0;
7003
7004           if (TYPE_UNSIGNED (TREE_TYPE (and))
7005               || (TYPE_PRECISION (type)
7006                   <= TYPE_PRECISION (TREE_TYPE (and))))
7007             change = 1;
7008           else if (TYPE_PRECISION (TREE_TYPE (and1))
7009                    <= HOST_BITS_PER_WIDE_INT
7010                    && host_integerp (and1, 1))
7011             {
7012               unsigned HOST_WIDE_INT cst;
7013
7014               cst = tree_low_cst (and1, 1);
7015               cst &= (HOST_WIDE_INT) -1
7016                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7017               change = (cst == 0);
7018 #ifdef LOAD_EXTEND_OP
7019               if (change
7020                   && !flag_syntax_only
7021                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7022                       == ZERO_EXTEND))
7023                 {
7024                   tree uns = lang_hooks.types.unsigned_type (TREE_TYPE (and0));
7025                   and0 = fold_convert (uns, and0);
7026                   and1 = fold_convert (uns, and1);
7027                 }
7028 #endif
7029             }
7030           if (change)
7031             {
7032               tem = build_int_cst_wide (type, TREE_INT_CST_LOW (and1),
7033                                         TREE_INT_CST_HIGH (and1));
7034               tem = force_fit_type (tem, 0, TREE_OVERFLOW (and1),
7035                                     TREE_CONSTANT_OVERFLOW (and1));
7036               return fold_build2 (BIT_AND_EXPR, type,
7037                                   fold_convert (type, and0), tem);
7038             }
7039         }
7040
7041       /* Convert (T1)((T2)X op Y) into (T1)X op Y, for pointer types T1 and
7042          T2 being pointers to types of the same size.  */
7043       if (POINTER_TYPE_P (type)
7044           && BINARY_CLASS_P (arg0)
7045           && TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7046           && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0))))
7047         {
7048           tree arg00 = TREE_OPERAND (arg0, 0);
7049           tree t0 = type;
7050           tree t1 = TREE_TYPE (arg00);
7051           tree tt0 = TREE_TYPE (t0);
7052           tree tt1 = TREE_TYPE (t1);
7053           tree s0 = TYPE_SIZE (tt0);
7054           tree s1 = TYPE_SIZE (tt1);
7055
7056           if (s0 && s1 && operand_equal_p (s0, s1, OEP_ONLY_CONST))
7057             return build2 (TREE_CODE (arg0), t0, fold_convert (t0, arg00),
7058                            TREE_OPERAND (arg0, 1));
7059         }
7060
7061       tem = fold_convert_const (code, type, arg0);
7062       return tem ? tem : NULL_TREE;
7063
7064     case VIEW_CONVERT_EXPR:
7065       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
7066         return build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
7067       return NULL_TREE;
7068
7069     case NEGATE_EXPR:
7070       if (negate_expr_p (arg0))
7071         return fold_convert (type, negate_expr (arg0));
7072       /* Convert - (~A) to A + 1.  */
7073       if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == BIT_NOT_EXPR)
7074         return fold_build2 (PLUS_EXPR, type, TREE_OPERAND (arg0, 0),
7075                             build_int_cst (type, 1));
7076       return NULL_TREE;
7077
7078     case ABS_EXPR:
7079       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
7080         return fold_abs_const (arg0, type);
7081       else if (TREE_CODE (arg0) == NEGATE_EXPR)
7082         return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
7083       /* Convert fabs((double)float) into (double)fabsf(float).  */
7084       else if (TREE_CODE (arg0) == NOP_EXPR
7085                && TREE_CODE (type) == REAL_TYPE)
7086         {
7087           tree targ0 = strip_float_extensions (arg0);
7088           if (targ0 != arg0)
7089             return fold_convert (type, fold_build1 (ABS_EXPR,
7090                                                     TREE_TYPE (targ0),
7091                                                     targ0));
7092         }
7093       else if (tree_expr_nonnegative_p (arg0))
7094         return arg0;
7095
7096       /* Strip sign ops from argument.  */
7097       if (TREE_CODE (type) == REAL_TYPE)
7098         {
7099           tem = fold_strip_sign_ops (arg0);
7100           if (tem)
7101             return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
7102         }
7103       return NULL_TREE;
7104
7105     case CONJ_EXPR:
7106       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7107         return fold_convert (type, arg0);
7108       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7109         return build2 (COMPLEX_EXPR, type,
7110                        TREE_OPERAND (arg0, 0),
7111                        negate_expr (TREE_OPERAND (arg0, 1)));
7112       else if (TREE_CODE (arg0) == COMPLEX_CST)
7113         return build_complex (type, TREE_REALPART (arg0),
7114                               negate_expr (TREE_IMAGPART (arg0)));
7115       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7116         return fold_build2 (TREE_CODE (arg0), type,
7117                             fold_build1 (CONJ_EXPR, type,
7118                                          TREE_OPERAND (arg0, 0)),
7119                             fold_build1 (CONJ_EXPR, type,
7120                                          TREE_OPERAND (arg0, 1)));
7121       else if (TREE_CODE (arg0) == CONJ_EXPR)
7122         return TREE_OPERAND (arg0, 0);
7123       return NULL_TREE;
7124
7125     case BIT_NOT_EXPR:
7126       if (TREE_CODE (arg0) == INTEGER_CST)
7127         return fold_not_const (arg0, type);
7128       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
7129         return TREE_OPERAND (arg0, 0);
7130       /* Convert ~ (-A) to A - 1.  */
7131       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
7132         return fold_build2 (MINUS_EXPR, type, TREE_OPERAND (arg0, 0),
7133                             build_int_cst (type, 1));
7134       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
7135       else if (INTEGRAL_TYPE_P (type)
7136                && ((TREE_CODE (arg0) == MINUS_EXPR
7137                     && integer_onep (TREE_OPERAND (arg0, 1)))
7138                    || (TREE_CODE (arg0) == PLUS_EXPR
7139                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
7140         return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
7141       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
7142       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7143                && (tem = fold_unary (BIT_NOT_EXPR, type,
7144                                      fold_convert (type,
7145                                                    TREE_OPERAND (arg0, 0)))))
7146         return fold_build2 (BIT_XOR_EXPR, type, tem,
7147                             fold_convert (type, TREE_OPERAND (arg0, 1)));
7148       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7149                && (tem = fold_unary (BIT_NOT_EXPR, type,
7150                                      fold_convert (type,
7151                                                    TREE_OPERAND (arg0, 1)))))
7152         return fold_build2 (BIT_XOR_EXPR, type,
7153                             fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
7154
7155       return NULL_TREE;
7156
7157     case TRUTH_NOT_EXPR:
7158       /* The argument to invert_truthvalue must have Boolean type.  */
7159       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
7160           arg0 = fold_convert (boolean_type_node, arg0);
7161
7162       /* Note that the operand of this must be an int
7163          and its values must be 0 or 1.
7164          ("true" is a fixed value perhaps depending on the language,
7165          but we don't handle values other than 1 correctly yet.)  */
7166       tem = invert_truthvalue (arg0);
7167       /* Avoid infinite recursion.  */
7168       if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
7169         return NULL_TREE;
7170       return fold_convert (type, tem);
7171
7172     case REALPART_EXPR:
7173       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7174         return NULL_TREE;
7175       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7176         return omit_one_operand (type, TREE_OPERAND (arg0, 0),
7177                                  TREE_OPERAND (arg0, 1));
7178       else if (TREE_CODE (arg0) == COMPLEX_CST)
7179         return TREE_REALPART (arg0);
7180       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7181         return fold_build2 (TREE_CODE (arg0), type,
7182                             fold_build1 (REALPART_EXPR, type,
7183                                          TREE_OPERAND (arg0, 0)),
7184                             fold_build1 (REALPART_EXPR, type,
7185                                          TREE_OPERAND (arg0, 1)));
7186       return NULL_TREE;
7187
7188     case IMAGPART_EXPR:
7189       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7190         return fold_convert (type, integer_zero_node);
7191       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7192         return omit_one_operand (type, TREE_OPERAND (arg0, 1),
7193                                  TREE_OPERAND (arg0, 0));
7194       else if (TREE_CODE (arg0) == COMPLEX_CST)
7195         return TREE_IMAGPART (arg0);
7196       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7197         return fold_build2 (TREE_CODE (arg0), type,
7198                             fold_build1 (IMAGPART_EXPR, type,
7199                                          TREE_OPERAND (arg0, 0)),
7200                             fold_build1 (IMAGPART_EXPR, type,
7201                                          TREE_OPERAND (arg0, 1)));
7202       return NULL_TREE;
7203
7204     default:
7205       return NULL_TREE;
7206     } /* switch (code) */
7207 }
7208
7209 /* Fold a binary expression of code CODE and type TYPE with operands
7210    OP0 and OP1.  Return the folded expression if folding is
7211    successful.  Otherwise, return NULL_TREE.  */
7212
7213 tree
7214 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
7215 {
7216   tree t1 = NULL_TREE;
7217   tree tem;
7218   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
7219   enum tree_code_class kind = TREE_CODE_CLASS (code);
7220
7221   /* WINS will be nonzero when the switch is done
7222      if all operands are constant.  */
7223   int wins = 1;
7224
7225   gcc_assert (IS_EXPR_CODE_CLASS (kind)
7226               && TREE_CODE_LENGTH (code) == 2);
7227
7228   arg0 = op0;
7229   arg1 = op1;
7230
7231   if (arg0)
7232     {
7233       tree subop;
7234
7235       /* Strip any conversions that don't change the mode.  This is
7236          safe for every expression, except for a comparison expression
7237          because its signedness is derived from its operands.  So, in
7238          the latter case, only strip conversions that don't change the
7239          signedness.
7240
7241          Note that this is done as an internal manipulation within the
7242          constant folder, in order to find the simplest representation
7243          of the arguments so that their form can be studied.  In any
7244          cases, the appropriate type conversions should be put back in
7245          the tree that will get out of the constant folder.  */
7246       if (kind == tcc_comparison)
7247         STRIP_SIGN_NOPS (arg0);
7248       else
7249         STRIP_NOPS (arg0);
7250
7251       if (TREE_CODE (arg0) == COMPLEX_CST)
7252         subop = TREE_REALPART (arg0);
7253       else
7254         subop = arg0;
7255
7256       if (TREE_CODE (subop) != INTEGER_CST
7257           && TREE_CODE (subop) != REAL_CST)
7258         /* Note that TREE_CONSTANT isn't enough:
7259            static var addresses are constant but we can't
7260            do arithmetic on them.  */
7261         wins = 0;
7262     }
7263
7264   if (arg1)
7265     {
7266       tree subop;
7267
7268       /* Strip any conversions that don't change the mode.  This is
7269          safe for every expression, except for a comparison expression
7270          because its signedness is derived from its operands.  So, in
7271          the latter case, only strip conversions that don't change the
7272          signedness.
7273
7274          Note that this is done as an internal manipulation within the
7275          constant folder, in order to find the simplest representation
7276          of the arguments so that their form can be studied.  In any
7277          cases, the appropriate type conversions should be put back in
7278          the tree that will get out of the constant folder.  */
7279       if (kind == tcc_comparison)
7280         STRIP_SIGN_NOPS (arg1);
7281       else
7282         STRIP_NOPS (arg1);
7283
7284       if (TREE_CODE (arg1) == COMPLEX_CST)
7285         subop = TREE_REALPART (arg1);
7286       else
7287         subop = arg1;
7288
7289       if (TREE_CODE (subop) != INTEGER_CST
7290           && TREE_CODE (subop) != REAL_CST)
7291         /* Note that TREE_CONSTANT isn't enough:
7292            static var addresses are constant but we can't
7293            do arithmetic on them.  */
7294         wins = 0;
7295     }
7296
7297   /* If this is a commutative operation, and ARG0 is a constant, move it
7298      to ARG1 to reduce the number of tests below.  */
7299   if (commutative_tree_code (code)
7300       && tree_swap_operands_p (arg0, arg1, true))
7301     return fold_build2 (code, type, op1, op0);
7302
7303   /* Now WINS is set as described above,
7304      ARG0 is the first operand of EXPR,
7305      and ARG1 is the second operand (if it has more than one operand).
7306
7307      First check for cases where an arithmetic operation is applied to a
7308      compound, conditional, or comparison operation.  Push the arithmetic
7309      operation inside the compound or conditional to see if any folding
7310      can then be done.  Convert comparison to conditional for this purpose.
7311      The also optimizes non-constant cases that used to be done in
7312      expand_expr.
7313
7314      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
7315      one of the operands is a comparison and the other is a comparison, a
7316      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
7317      code below would make the expression more complex.  Change it to a
7318      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
7319      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
7320
7321   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
7322        || code == EQ_EXPR || code == NE_EXPR)
7323       && ((truth_value_p (TREE_CODE (arg0))
7324            && (truth_value_p (TREE_CODE (arg1))
7325                || (TREE_CODE (arg1) == BIT_AND_EXPR
7326                    && integer_onep (TREE_OPERAND (arg1, 1)))))
7327           || (truth_value_p (TREE_CODE (arg1))
7328               && (truth_value_p (TREE_CODE (arg0))
7329                   || (TREE_CODE (arg0) == BIT_AND_EXPR
7330                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
7331     {
7332       tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
7333                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
7334                          : TRUTH_XOR_EXPR,
7335                          boolean_type_node,
7336                          fold_convert (boolean_type_node, arg0),
7337                          fold_convert (boolean_type_node, arg1));
7338
7339       if (code == EQ_EXPR)
7340         tem = invert_truthvalue (tem);
7341
7342       return fold_convert (type, tem);
7343     }
7344
7345   if (TREE_CODE_CLASS (code) == tcc_comparison
7346            && TREE_CODE (arg0) == COMPOUND_EXPR)
7347     return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7348                    fold_build2 (code, type, TREE_OPERAND (arg0, 1), arg1));
7349   else if (TREE_CODE_CLASS (code) == tcc_comparison
7350            && TREE_CODE (arg1) == COMPOUND_EXPR)
7351     return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
7352                    fold_build2 (code, type, arg0, TREE_OPERAND (arg1, 1)));
7353   else if (TREE_CODE_CLASS (code) == tcc_binary
7354            || TREE_CODE_CLASS (code) == tcc_comparison)
7355     {
7356       if (TREE_CODE (arg0) == COMPOUND_EXPR)
7357         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7358                        fold_build2 (code, type, TREE_OPERAND (arg0, 1),
7359                                     arg1));
7360       if (TREE_CODE (arg1) == COMPOUND_EXPR
7361           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
7362         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
7363                        fold_build2 (code, type,
7364                                     arg0, TREE_OPERAND (arg1, 1)));
7365
7366       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
7367         {
7368           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
7369                                                      arg0, arg1, 
7370                                                      /*cond_first_p=*/1);
7371           if (tem != NULL_TREE)
7372             return tem;
7373         }
7374
7375       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
7376         {
7377           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
7378                                                      arg1, arg0, 
7379                                                      /*cond_first_p=*/0);
7380           if (tem != NULL_TREE)
7381             return tem;
7382         }
7383     }
7384
7385   switch (code)
7386     {
7387     case PLUS_EXPR:
7388       /* A + (-B) -> A - B */
7389       if (TREE_CODE (arg1) == NEGATE_EXPR)
7390         return fold_build2 (MINUS_EXPR, type,
7391                             fold_convert (type, arg0),
7392                             fold_convert (type, TREE_OPERAND (arg1, 0)));
7393       /* (-A) + B -> B - A */
7394       if (TREE_CODE (arg0) == NEGATE_EXPR
7395           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
7396         return fold_build2 (MINUS_EXPR, type,
7397                             fold_convert (type, arg1),
7398                             fold_convert (type, TREE_OPERAND (arg0, 0)));
7399       /* Convert ~A + 1 to -A.  */
7400       if (INTEGRAL_TYPE_P (type)
7401           && TREE_CODE (arg0) == BIT_NOT_EXPR
7402           && integer_onep (arg1))
7403         return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
7404
7405       if (TREE_CODE (type) == COMPLEX_TYPE)
7406         {
7407           tem = fold_complex_add (type, arg0, arg1, PLUS_EXPR);
7408           if (tem)
7409             return tem;
7410         }
7411
7412       if (! FLOAT_TYPE_P (type))
7413         {
7414           if (integer_zerop (arg1))
7415             return non_lvalue (fold_convert (type, arg0));
7416
7417           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
7418              with a constant, and the two constants have no bits in common,
7419              we should treat this as a BIT_IOR_EXPR since this may produce more
7420              simplifications.  */
7421           if (TREE_CODE (arg0) == BIT_AND_EXPR
7422               && TREE_CODE (arg1) == BIT_AND_EXPR
7423               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7424               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
7425               && integer_zerop (const_binop (BIT_AND_EXPR,
7426                                              TREE_OPERAND (arg0, 1),
7427                                              TREE_OPERAND (arg1, 1), 0)))
7428             {
7429               code = BIT_IOR_EXPR;
7430               goto bit_ior;
7431             }
7432
7433           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
7434              (plus (plus (mult) (mult)) (foo)) so that we can
7435              take advantage of the factoring cases below.  */
7436           if (((TREE_CODE (arg0) == PLUS_EXPR
7437                 || TREE_CODE (arg0) == MINUS_EXPR)
7438                && TREE_CODE (arg1) == MULT_EXPR)
7439               || ((TREE_CODE (arg1) == PLUS_EXPR
7440                    || TREE_CODE (arg1) == MINUS_EXPR)
7441                   && TREE_CODE (arg0) == MULT_EXPR))
7442             {
7443               tree parg0, parg1, parg, marg;
7444               enum tree_code pcode;
7445
7446               if (TREE_CODE (arg1) == MULT_EXPR)
7447                 parg = arg0, marg = arg1;
7448               else
7449                 parg = arg1, marg = arg0;
7450               pcode = TREE_CODE (parg);
7451               parg0 = TREE_OPERAND (parg, 0);
7452               parg1 = TREE_OPERAND (parg, 1);
7453               STRIP_NOPS (parg0);
7454               STRIP_NOPS (parg1);
7455
7456               if (TREE_CODE (parg0) == MULT_EXPR
7457                   && TREE_CODE (parg1) != MULT_EXPR)
7458                 return fold_build2 (pcode, type,
7459                                     fold_build2 (PLUS_EXPR, type,
7460                                                  fold_convert (type, parg0),
7461                                                  fold_convert (type, marg)),
7462                                     fold_convert (type, parg1));
7463               if (TREE_CODE (parg0) != MULT_EXPR
7464                   && TREE_CODE (parg1) == MULT_EXPR)
7465                 return fold_build2 (PLUS_EXPR, type,
7466                                     fold_convert (type, parg0),
7467                                     fold_build2 (pcode, type,
7468                                                  fold_convert (type, marg),
7469                                                  fold_convert (type,
7470                                                                parg1)));
7471             }
7472
7473           if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR)
7474             {
7475               tree arg00, arg01, arg10, arg11;
7476               tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7477
7478               /* (A * C) + (B * C) -> (A+B) * C.
7479                  We are most concerned about the case where C is a constant,
7480                  but other combinations show up during loop reduction.  Since
7481                  it is not difficult, try all four possibilities.  */
7482
7483               arg00 = TREE_OPERAND (arg0, 0);
7484               arg01 = TREE_OPERAND (arg0, 1);
7485               arg10 = TREE_OPERAND (arg1, 0);
7486               arg11 = TREE_OPERAND (arg1, 1);
7487               same = NULL_TREE;
7488
7489               if (operand_equal_p (arg01, arg11, 0))
7490                 same = arg01, alt0 = arg00, alt1 = arg10;
7491               else if (operand_equal_p (arg00, arg10, 0))
7492                 same = arg00, alt0 = arg01, alt1 = arg11;
7493               else if (operand_equal_p (arg00, arg11, 0))
7494                 same = arg00, alt0 = arg01, alt1 = arg10;
7495               else if (operand_equal_p (arg01, arg10, 0))
7496                 same = arg01, alt0 = arg00, alt1 = arg11;
7497
7498               /* No identical multiplicands; see if we can find a common
7499                  power-of-two factor in non-power-of-two multiplies.  This
7500                  can help in multi-dimensional array access.  */
7501               else if (TREE_CODE (arg01) == INTEGER_CST
7502                        && TREE_CODE (arg11) == INTEGER_CST
7503                        && TREE_INT_CST_HIGH (arg01) == 0
7504                        && TREE_INT_CST_HIGH (arg11) == 0)
7505                 {
7506                   HOST_WIDE_INT int01, int11, tmp;
7507                   int01 = TREE_INT_CST_LOW (arg01);
7508                   int11 = TREE_INT_CST_LOW (arg11);
7509
7510                   /* Move min of absolute values to int11.  */
7511                   if ((int01 >= 0 ? int01 : -int01)
7512                       < (int11 >= 0 ? int11 : -int11))
7513                     {
7514                       tmp = int01, int01 = int11, int11 = tmp;
7515                       alt0 = arg00, arg00 = arg10, arg10 = alt0;
7516                       alt0 = arg01, arg01 = arg11, arg11 = alt0;
7517                     }
7518
7519                   if (exact_log2 (int11) > 0 && int01 % int11 == 0)
7520                     {
7521                       alt0 = fold_build2 (MULT_EXPR, type, arg00,
7522                                           build_int_cst (NULL_TREE,
7523                                                          int01 / int11));
7524                       alt1 = arg10;
7525                       same = arg11;
7526                     }
7527                 }
7528
7529               if (same)
7530                 return fold_build2 (MULT_EXPR, type,
7531                                     fold_build2 (PLUS_EXPR, type,
7532                                                  fold_convert (type, alt0),
7533                                                  fold_convert (type, alt1)),
7534                                     fold_convert (type, same));
7535             }
7536
7537           /* Try replacing &a[i1] + c * i2 with &a[i1 + i2], if c is step
7538              of the array.  Loop optimizer sometimes produce this type of
7539              expressions.  */
7540           if (TREE_CODE (arg0) == ADDR_EXPR)
7541             {
7542               tem = try_move_mult_to_index (PLUS_EXPR, arg0, arg1);
7543               if (tem)
7544                 return fold_convert (type, fold (tem));
7545             }
7546           else if (TREE_CODE (arg1) == ADDR_EXPR)
7547             {
7548               tem = try_move_mult_to_index (PLUS_EXPR, arg1, arg0);
7549               if (tem)
7550                 return fold_convert (type, fold (tem));
7551             }
7552         }
7553       else
7554         {
7555           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
7556           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
7557             return non_lvalue (fold_convert (type, arg0));
7558
7559           /* Likewise if the operands are reversed.  */
7560           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
7561             return non_lvalue (fold_convert (type, arg1));
7562
7563           /* Convert X + -C into X - C.  */
7564           if (TREE_CODE (arg1) == REAL_CST
7565               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
7566             {
7567               tem = fold_negate_const (arg1, type);
7568               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
7569                 return fold_build2 (MINUS_EXPR, type,
7570                                     fold_convert (type, arg0),
7571                                     fold_convert (type, tem));
7572             }
7573
7574           if (flag_unsafe_math_optimizations
7575               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
7576               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
7577               && (tem = distribute_real_division (code, type, arg0, arg1)))
7578             return tem;
7579
7580           /* Convert x+x into x*2.0.  */
7581           if (operand_equal_p (arg0, arg1, 0)
7582               && SCALAR_FLOAT_TYPE_P (type))
7583             return fold_build2 (MULT_EXPR, type, arg0,
7584                                 build_real (type, dconst2));
7585
7586           /* Convert x*c+x into x*(c+1).  */
7587           if (flag_unsafe_math_optimizations
7588               && TREE_CODE (arg0) == MULT_EXPR
7589               && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
7590               && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0, 1))
7591               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
7592             {
7593               REAL_VALUE_TYPE c;
7594
7595               c = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
7596               real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
7597               return fold_build2 (MULT_EXPR, type, arg1,
7598                                   build_real (type, c));
7599             }
7600
7601           /* Convert x+x*c into x*(c+1).  */
7602           if (flag_unsafe_math_optimizations
7603               && TREE_CODE (arg1) == MULT_EXPR
7604               && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST
7605               && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1, 1))
7606               && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
7607             {
7608               REAL_VALUE_TYPE c;
7609
7610               c = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
7611               real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
7612               return fold_build2 (MULT_EXPR, type, arg0,
7613                                   build_real (type, c));
7614             }
7615
7616           /* Convert x*c1+x*c2 into x*(c1+c2).  */
7617           if (flag_unsafe_math_optimizations
7618               && TREE_CODE (arg0) == MULT_EXPR
7619               && TREE_CODE (arg1) == MULT_EXPR
7620               && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
7621               && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0, 1))
7622               && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST
7623               && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1, 1))
7624               && operand_equal_p (TREE_OPERAND (arg0, 0),
7625                                   TREE_OPERAND (arg1, 0), 0))
7626             {
7627               REAL_VALUE_TYPE c1, c2;
7628
7629               c1 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
7630               c2 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
7631               real_arithmetic (&c1, PLUS_EXPR, &c1, &c2);
7632               return fold_build2 (MULT_EXPR, type,
7633                                   TREE_OPERAND (arg0, 0),
7634                                   build_real (type, c1));
7635             }
7636           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.  */
7637           if (flag_unsafe_math_optimizations
7638               && TREE_CODE (arg1) == PLUS_EXPR
7639               && TREE_CODE (arg0) != MULT_EXPR)
7640             {
7641               tree tree10 = TREE_OPERAND (arg1, 0);
7642               tree tree11 = TREE_OPERAND (arg1, 1);
7643               if (TREE_CODE (tree11) == MULT_EXPR
7644                   && TREE_CODE (tree10) == MULT_EXPR)
7645                 {
7646                   tree tree0;
7647                   tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
7648                   return fold_build2 (PLUS_EXPR, type, tree0, tree11);
7649                 }
7650             }
7651           /* Convert (b*c + d*e) + a into b*c + (d*e +a).  */
7652           if (flag_unsafe_math_optimizations
7653               && TREE_CODE (arg0) == PLUS_EXPR
7654               && TREE_CODE (arg1) != MULT_EXPR)
7655             {
7656               tree tree00 = TREE_OPERAND (arg0, 0);
7657               tree tree01 = TREE_OPERAND (arg0, 1);
7658               if (TREE_CODE (tree01) == MULT_EXPR
7659                   && TREE_CODE (tree00) == MULT_EXPR)
7660                 {
7661                   tree tree0;
7662                   tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
7663                   return fold_build2 (PLUS_EXPR, type, tree00, tree0);
7664                 }
7665             }
7666         }
7667
7668      bit_rotate:
7669       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
7670          is a rotate of A by C1 bits.  */
7671       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
7672          is a rotate of A by B bits.  */
7673       {
7674         enum tree_code code0, code1;
7675         code0 = TREE_CODE (arg0);
7676         code1 = TREE_CODE (arg1);
7677         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
7678              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
7679             && operand_equal_p (TREE_OPERAND (arg0, 0),
7680                                 TREE_OPERAND (arg1, 0), 0)
7681             && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
7682           {
7683             tree tree01, tree11;
7684             enum tree_code code01, code11;
7685
7686             tree01 = TREE_OPERAND (arg0, 1);
7687             tree11 = TREE_OPERAND (arg1, 1);
7688             STRIP_NOPS (tree01);
7689             STRIP_NOPS (tree11);
7690             code01 = TREE_CODE (tree01);
7691             code11 = TREE_CODE (tree11);
7692             if (code01 == INTEGER_CST
7693                 && code11 == INTEGER_CST
7694                 && TREE_INT_CST_HIGH (tree01) == 0
7695                 && TREE_INT_CST_HIGH (tree11) == 0
7696                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
7697                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
7698               return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
7699                              code0 == LSHIFT_EXPR ? tree01 : tree11);
7700             else if (code11 == MINUS_EXPR)
7701               {
7702                 tree tree110, tree111;
7703                 tree110 = TREE_OPERAND (tree11, 0);
7704                 tree111 = TREE_OPERAND (tree11, 1);
7705                 STRIP_NOPS (tree110);
7706                 STRIP_NOPS (tree111);
7707                 if (TREE_CODE (tree110) == INTEGER_CST
7708                     && 0 == compare_tree_int (tree110,
7709                                               TYPE_PRECISION
7710                                               (TREE_TYPE (TREE_OPERAND
7711                                                           (arg0, 0))))
7712                     && operand_equal_p (tree01, tree111, 0))
7713                   return build2 ((code0 == LSHIFT_EXPR
7714                                   ? LROTATE_EXPR
7715                                   : RROTATE_EXPR),
7716                                  type, TREE_OPERAND (arg0, 0), tree01);
7717               }
7718             else if (code01 == MINUS_EXPR)
7719               {
7720                 tree tree010, tree011;
7721                 tree010 = TREE_OPERAND (tree01, 0);
7722                 tree011 = TREE_OPERAND (tree01, 1);
7723                 STRIP_NOPS (tree010);
7724                 STRIP_NOPS (tree011);
7725                 if (TREE_CODE (tree010) == INTEGER_CST
7726                     && 0 == compare_tree_int (tree010,
7727                                               TYPE_PRECISION
7728                                               (TREE_TYPE (TREE_OPERAND
7729                                                           (arg0, 0))))
7730                     && operand_equal_p (tree11, tree011, 0))
7731                   return build2 ((code0 != LSHIFT_EXPR
7732                                   ? LROTATE_EXPR
7733                                   : RROTATE_EXPR),
7734                                  type, TREE_OPERAND (arg0, 0), tree11);
7735               }
7736           }
7737       }
7738
7739     associate:
7740       /* In most languages, can't associate operations on floats through
7741          parentheses.  Rather than remember where the parentheses were, we
7742          don't associate floats at all, unless the user has specified
7743          -funsafe-math-optimizations.  */
7744
7745       if (! wins
7746           && (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
7747         {
7748           tree var0, con0, lit0, minus_lit0;
7749           tree var1, con1, lit1, minus_lit1;
7750
7751           /* Split both trees into variables, constants, and literals.  Then
7752              associate each group together, the constants with literals,
7753              then the result with variables.  This increases the chances of
7754              literals being recombined later and of generating relocatable
7755              expressions for the sum of a constant and literal.  */
7756           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
7757           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
7758                              code == MINUS_EXPR);
7759
7760           /* Only do something if we found more than two objects.  Otherwise,
7761              nothing has changed and we risk infinite recursion.  */
7762           if (2 < ((var0 != 0) + (var1 != 0)
7763                    + (con0 != 0) + (con1 != 0)
7764                    + (lit0 != 0) + (lit1 != 0)
7765                    + (minus_lit0 != 0) + (minus_lit1 != 0)))
7766             {
7767               /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
7768               if (code == MINUS_EXPR)
7769                 code = PLUS_EXPR;
7770
7771               var0 = associate_trees (var0, var1, code, type);
7772               con0 = associate_trees (con0, con1, code, type);
7773               lit0 = associate_trees (lit0, lit1, code, type);
7774               minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
7775
7776               /* Preserve the MINUS_EXPR if the negative part of the literal is
7777                  greater than the positive part.  Otherwise, the multiplicative
7778                  folding code (i.e extract_muldiv) may be fooled in case
7779                  unsigned constants are subtracted, like in the following
7780                  example: ((X*2 + 4) - 8U)/2.  */
7781               if (minus_lit0 && lit0)
7782                 {
7783                   if (TREE_CODE (lit0) == INTEGER_CST
7784                       && TREE_CODE (minus_lit0) == INTEGER_CST
7785                       && tree_int_cst_lt (lit0, minus_lit0))
7786                     {
7787                       minus_lit0 = associate_trees (minus_lit0, lit0,
7788                                                     MINUS_EXPR, type);
7789                       lit0 = 0;
7790                     }
7791                   else
7792                     {
7793                       lit0 = associate_trees (lit0, minus_lit0,
7794                                               MINUS_EXPR, type);
7795                       minus_lit0 = 0;
7796                     }
7797                 }
7798               if (minus_lit0)
7799                 {
7800                   if (con0 == 0)
7801                     return fold_convert (type,
7802                                          associate_trees (var0, minus_lit0,
7803                                                           MINUS_EXPR, type));
7804                   else
7805                     {
7806                       con0 = associate_trees (con0, minus_lit0,
7807                                               MINUS_EXPR, type);
7808                       return fold_convert (type,
7809                                            associate_trees (var0, con0,
7810                                                             PLUS_EXPR, type));
7811                     }
7812                 }
7813
7814               con0 = associate_trees (con0, lit0, code, type);
7815               return fold_convert (type, associate_trees (var0, con0,
7816                                                           code, type));
7817             }
7818         }
7819
7820     binary:
7821       if (wins)
7822         t1 = const_binop (code, arg0, arg1, 0);
7823       if (t1 != NULL_TREE)
7824         {
7825           /* The return value should always have
7826              the same type as the original expression.  */
7827           if (TREE_TYPE (t1) != type)
7828             t1 = fold_convert (type, t1);
7829
7830           return t1;
7831         }
7832       return NULL_TREE;
7833
7834     case MINUS_EXPR:
7835       /* A - (-B) -> A + B */
7836       if (TREE_CODE (arg1) == NEGATE_EXPR)
7837         return fold_build2 (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0));
7838       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
7839       if (TREE_CODE (arg0) == NEGATE_EXPR
7840           && (FLOAT_TYPE_P (type)
7841               || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv))
7842           && negate_expr_p (arg1)
7843           && reorder_operands_p (arg0, arg1))
7844         return fold_build2 (MINUS_EXPR, type, negate_expr (arg1),
7845                             TREE_OPERAND (arg0, 0));
7846       /* Convert -A - 1 to ~A.  */
7847       if (INTEGRAL_TYPE_P (type)
7848           && TREE_CODE (arg0) == NEGATE_EXPR
7849           && integer_onep (arg1))
7850         return fold_build1 (BIT_NOT_EXPR, type, TREE_OPERAND (arg0, 0));
7851
7852       /* Convert -1 - A to ~A.  */
7853       if (INTEGRAL_TYPE_P (type)
7854           && integer_all_onesp (arg0))
7855         return fold_build1 (BIT_NOT_EXPR, type, arg1);
7856
7857       if (TREE_CODE (type) == COMPLEX_TYPE)
7858         {
7859           tem = fold_complex_add (type, arg0, arg1, MINUS_EXPR);
7860           if (tem)
7861             return tem;
7862         }
7863
7864       if (! FLOAT_TYPE_P (type))
7865         {
7866           if (! wins && integer_zerop (arg0))
7867             return negate_expr (fold_convert (type, arg1));
7868           if (integer_zerop (arg1))
7869             return non_lvalue (fold_convert (type, arg0));
7870
7871           /* Fold A - (A & B) into ~B & A.  */
7872           if (!TREE_SIDE_EFFECTS (arg0)
7873               && TREE_CODE (arg1) == BIT_AND_EXPR)
7874             {
7875               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
7876                 return fold_build2 (BIT_AND_EXPR, type,
7877                                     fold_build1 (BIT_NOT_EXPR, type,
7878                                                  TREE_OPERAND (arg1, 0)),
7879                                     arg0);
7880               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
7881                 return fold_build2 (BIT_AND_EXPR, type,
7882                                     fold_build1 (BIT_NOT_EXPR, type,
7883                                                  TREE_OPERAND (arg1, 1)),
7884                                     arg0);
7885             }
7886
7887           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
7888              any power of 2 minus 1.  */
7889           if (TREE_CODE (arg0) == BIT_AND_EXPR
7890               && TREE_CODE (arg1) == BIT_AND_EXPR
7891               && operand_equal_p (TREE_OPERAND (arg0, 0),
7892                                   TREE_OPERAND (arg1, 0), 0))
7893             {
7894               tree mask0 = TREE_OPERAND (arg0, 1);
7895               tree mask1 = TREE_OPERAND (arg1, 1);
7896               tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
7897
7898               if (operand_equal_p (tem, mask1, 0))
7899                 {
7900                   tem = fold_build2 (BIT_XOR_EXPR, type,
7901                                      TREE_OPERAND (arg0, 0), mask1);
7902                   return fold_build2 (MINUS_EXPR, type, tem, mask1);
7903                 }
7904             }
7905         }
7906
7907       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
7908       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
7909         return non_lvalue (fold_convert (type, arg0));
7910
7911       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
7912          ARG0 is zero and X + ARG0 reduces to X, since that would mean
7913          (-ARG1 + ARG0) reduces to -ARG1.  */
7914       else if (!wins && fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
7915         return negate_expr (fold_convert (type, arg1));
7916
7917       /* Fold &x - &x.  This can happen from &x.foo - &x.
7918          This is unsafe for certain floats even in non-IEEE formats.
7919          In IEEE, it is unsafe because it does wrong for NaNs.
7920          Also note that operand_equal_p is always false if an operand
7921          is volatile.  */
7922
7923       if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
7924           && operand_equal_p (arg0, arg1, 0))
7925         return fold_convert (type, integer_zero_node);
7926
7927       /* A - B -> A + (-B) if B is easily negatable.  */
7928       if (!wins && negate_expr_p (arg1)
7929           && ((FLOAT_TYPE_P (type)
7930                /* Avoid this transformation if B is a positive REAL_CST.  */
7931                && (TREE_CODE (arg1) != REAL_CST
7932                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
7933               || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv)))
7934         return fold_build2 (PLUS_EXPR, type, arg0, negate_expr (arg1));
7935
7936       /* Try folding difference of addresses.  */
7937       {
7938         HOST_WIDE_INT diff;
7939
7940         if ((TREE_CODE (arg0) == ADDR_EXPR
7941              || TREE_CODE (arg1) == ADDR_EXPR)
7942             && ptr_difference_const (arg0, arg1, &diff))
7943           return build_int_cst_type (type, diff);
7944       }
7945
7946       /* Fold &a[i] - &a[j] to i-j.  */
7947       if (TREE_CODE (arg0) == ADDR_EXPR
7948           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
7949           && TREE_CODE (arg1) == ADDR_EXPR
7950           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
7951         {
7952           tree aref0 = TREE_OPERAND (arg0, 0);
7953           tree aref1 = TREE_OPERAND (arg1, 0);
7954           if (operand_equal_p (TREE_OPERAND (aref0, 0),
7955                                TREE_OPERAND (aref1, 0), 0))
7956             {
7957               tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
7958               tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
7959               tree esz = array_ref_element_size (aref0);
7960               tree diff = build2 (MINUS_EXPR, type, op0, op1);
7961               return fold_build2 (MULT_EXPR, type, diff,
7962                                   fold_convert (type, esz));
7963                                   
7964             }
7965         }
7966
7967       /* Try replacing &a[i1] - c * i2 with &a[i1 - i2], if c is step
7968          of the array.  Loop optimizer sometimes produce this type of
7969          expressions.  */
7970       if (TREE_CODE (arg0) == ADDR_EXPR)
7971         {
7972           tem = try_move_mult_to_index (MINUS_EXPR, arg0, arg1);
7973           if (tem)
7974             return fold_convert (type, fold (tem));
7975         }
7976
7977       if (flag_unsafe_math_optimizations
7978           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
7979           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
7980           && (tem = distribute_real_division (code, type, arg0, arg1)))
7981         return tem;
7982
7983       if (TREE_CODE (arg0) == MULT_EXPR
7984           && TREE_CODE (arg1) == MULT_EXPR
7985           && (!FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
7986         {
7987           /* (A * C) - (B * C) -> (A-B) * C.  */
7988           if (operand_equal_p (TREE_OPERAND (arg0, 1),
7989                                TREE_OPERAND (arg1, 1), 0))
7990             return fold_build2 (MULT_EXPR, type,
7991                                 fold_build2 (MINUS_EXPR, type,
7992                                              TREE_OPERAND (arg0, 0),
7993                                              TREE_OPERAND (arg1, 0)),
7994                                 TREE_OPERAND (arg0, 1));
7995           /* (A * C1) - (A * C2) -> A * (C1-C2).  */
7996           if (operand_equal_p (TREE_OPERAND (arg0, 0),
7997                                TREE_OPERAND (arg1, 0), 0))
7998             return fold_build2 (MULT_EXPR, type,
7999                                 TREE_OPERAND (arg0, 0),
8000                                 fold_build2 (MINUS_EXPR, type,
8001                                              TREE_OPERAND (arg0, 1),
8002                                              TREE_OPERAND (arg1, 1)));
8003         }
8004
8005       goto associate;
8006
8007     case MULT_EXPR:
8008       /* (-A) * (-B) -> A * B  */
8009       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
8010         return fold_build2 (MULT_EXPR, type,
8011                             TREE_OPERAND (arg0, 0),
8012                             negate_expr (arg1));
8013       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
8014         return fold_build2 (MULT_EXPR, type,
8015                             negate_expr (arg0),
8016                             TREE_OPERAND (arg1, 0));
8017
8018       if (TREE_CODE (type) == COMPLEX_TYPE)
8019         {
8020           tem = fold_complex_mult (type, arg0, arg1);
8021           if (tem)
8022             return tem;
8023         }
8024
8025       if (! FLOAT_TYPE_P (type))
8026         {
8027           if (integer_zerop (arg1))
8028             return omit_one_operand (type, arg1, arg0);
8029           if (integer_onep (arg1))
8030             return non_lvalue (fold_convert (type, arg0));
8031           /* Transform x * -1 into -x.  */
8032           if (integer_all_onesp (arg1))
8033             return fold_convert (type, negate_expr (arg0));
8034
8035           /* (a * (1 << b)) is (a << b)  */
8036           if (TREE_CODE (arg1) == LSHIFT_EXPR
8037               && integer_onep (TREE_OPERAND (arg1, 0)))
8038             return fold_build2 (LSHIFT_EXPR, type, arg0,
8039                                 TREE_OPERAND (arg1, 1));
8040           if (TREE_CODE (arg0) == LSHIFT_EXPR
8041               && integer_onep (TREE_OPERAND (arg0, 0)))
8042             return fold_build2 (LSHIFT_EXPR, type, arg1,
8043                                 TREE_OPERAND (arg0, 1));
8044
8045           if (TREE_CODE (arg1) == INTEGER_CST
8046               && 0 != (tem = extract_muldiv (op0,
8047                                              fold_convert (type, arg1),
8048                                              code, NULL_TREE)))
8049             return fold_convert (type, tem);
8050
8051         }
8052       else
8053         {
8054           /* Maybe fold x * 0 to 0.  The expressions aren't the same
8055              when x is NaN, since x * 0 is also NaN.  Nor are they the
8056              same in modes with signed zeros, since multiplying a
8057              negative value by 0 gives -0, not +0.  */
8058           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
8059               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
8060               && real_zerop (arg1))
8061             return omit_one_operand (type, arg1, arg0);
8062           /* In IEEE floating point, x*1 is not equivalent to x for snans.  */
8063           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
8064               && real_onep (arg1))
8065             return non_lvalue (fold_convert (type, arg0));
8066
8067           /* Transform x * -1.0 into -x.  */
8068           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
8069               && real_minus_onep (arg1))
8070             return fold_convert (type, negate_expr (arg0));
8071
8072           /* Convert (C1/X)*C2 into (C1*C2)/X.  */
8073           if (flag_unsafe_math_optimizations
8074               && TREE_CODE (arg0) == RDIV_EXPR
8075               && TREE_CODE (arg1) == REAL_CST
8076               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
8077             {
8078               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
8079                                       arg1, 0);
8080               if (tem)
8081                 return fold_build2 (RDIV_EXPR, type, tem,
8082                                     TREE_OPERAND (arg0, 1));
8083             }
8084
8085           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
8086           if (operand_equal_p (arg0, arg1, 0))
8087             {
8088               tree tem = fold_strip_sign_ops (arg0);
8089               if (tem != NULL_TREE)
8090                 {
8091                   tem = fold_convert (type, tem);
8092                   return fold_build2 (MULT_EXPR, type, tem, tem);
8093                 }
8094             }
8095
8096           if (flag_unsafe_math_optimizations)
8097             {
8098               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
8099               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
8100
8101               /* Optimizations of root(...)*root(...).  */
8102               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
8103                 {
8104                   tree rootfn, arg, arglist;
8105                   tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8106                   tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
8107
8108                   /* Optimize sqrt(x)*sqrt(x) as x.  */
8109                   if (BUILTIN_SQRT_P (fcode0)
8110                       && operand_equal_p (arg00, arg10, 0)
8111                       && ! HONOR_SNANS (TYPE_MODE (type)))
8112                     return arg00;
8113
8114                   /* Optimize root(x)*root(y) as root(x*y).  */
8115                   rootfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8116                   arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
8117                   arglist = build_tree_list (NULL_TREE, arg);
8118                   return build_function_call_expr (rootfn, arglist);
8119                 }
8120
8121               /* Optimize expN(x)*expN(y) as expN(x+y).  */
8122               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
8123                 {
8124                   tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8125                   tree arg = fold_build2 (PLUS_EXPR, type,
8126                                           TREE_VALUE (TREE_OPERAND (arg0, 1)),
8127                                           TREE_VALUE (TREE_OPERAND (arg1, 1)));
8128                   tree arglist = build_tree_list (NULL_TREE, arg);
8129                   return build_function_call_expr (expfn, arglist);
8130                 }
8131
8132               /* Optimizations of pow(...)*pow(...).  */
8133               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
8134                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
8135                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
8136                 {
8137                   tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8138                   tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
8139                                                                      1)));
8140                   tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
8141                   tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
8142                                                                      1)));
8143
8144                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
8145                   if (operand_equal_p (arg01, arg11, 0))
8146                     {
8147                       tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8148                       tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
8149                       tree arglist = tree_cons (NULL_TREE, arg,
8150                                                 build_tree_list (NULL_TREE,
8151                                                                  arg01));
8152                       return build_function_call_expr (powfn, arglist);
8153                     }
8154
8155                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
8156                   if (operand_equal_p (arg00, arg10, 0))
8157                     {
8158                       tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8159                       tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
8160                       tree arglist = tree_cons (NULL_TREE, arg00,
8161                                                 build_tree_list (NULL_TREE,
8162                                                                  arg));
8163                       return build_function_call_expr (powfn, arglist);
8164                     }
8165                 }
8166
8167               /* Optimize tan(x)*cos(x) as sin(x).  */
8168               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
8169                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
8170                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
8171                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
8172                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
8173                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
8174                   && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
8175                                       TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
8176                 {
8177                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
8178
8179                   if (sinfn != NULL_TREE)
8180                     return build_function_call_expr (sinfn,
8181                                                      TREE_OPERAND (arg0, 1));
8182                 }
8183
8184               /* Optimize x*pow(x,c) as pow(x,c+1).  */
8185               if (fcode1 == BUILT_IN_POW
8186                   || fcode1 == BUILT_IN_POWF
8187                   || fcode1 == BUILT_IN_POWL)
8188                 {
8189                   tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
8190                   tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
8191                                                                      1)));
8192                   if (TREE_CODE (arg11) == REAL_CST
8193                       && ! TREE_CONSTANT_OVERFLOW (arg11)
8194                       && operand_equal_p (arg0, arg10, 0))
8195                     {
8196                       tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
8197                       REAL_VALUE_TYPE c;
8198                       tree arg, arglist;
8199
8200                       c = TREE_REAL_CST (arg11);
8201                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
8202                       arg = build_real (type, c);
8203                       arglist = build_tree_list (NULL_TREE, arg);
8204                       arglist = tree_cons (NULL_TREE, arg0, arglist);
8205                       return build_function_call_expr (powfn, arglist);
8206                     }
8207                 }
8208
8209               /* Optimize pow(x,c)*x as pow(x,c+1).  */
8210               if (fcode0 == BUILT_IN_POW
8211                   || fcode0 == BUILT_IN_POWF
8212                   || fcode0 == BUILT_IN_POWL)
8213                 {
8214                   tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8215                   tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
8216                                                                      1)));
8217                   if (TREE_CODE (arg01) == REAL_CST
8218                       && ! TREE_CONSTANT_OVERFLOW (arg01)
8219                       && operand_equal_p (arg1, arg00, 0))
8220                     {
8221                       tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8222                       REAL_VALUE_TYPE c;
8223                       tree arg, arglist;
8224
8225                       c = TREE_REAL_CST (arg01);
8226                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
8227                       arg = build_real (type, c);
8228                       arglist = build_tree_list (NULL_TREE, arg);
8229                       arglist = tree_cons (NULL_TREE, arg1, arglist);
8230                       return build_function_call_expr (powfn, arglist);
8231                     }
8232                 }
8233
8234               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
8235               if (! optimize_size
8236                   && operand_equal_p (arg0, arg1, 0))
8237                 {
8238                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
8239
8240                   if (powfn)
8241                     {
8242                       tree arg = build_real (type, dconst2);
8243                       tree arglist = build_tree_list (NULL_TREE, arg);
8244                       arglist = tree_cons (NULL_TREE, arg0, arglist);
8245                       return build_function_call_expr (powfn, arglist);
8246                     }
8247                 }
8248             }
8249         }
8250       goto associate;
8251
8252     case BIT_IOR_EXPR:
8253     bit_ior:
8254       if (integer_all_onesp (arg1))
8255         return omit_one_operand (type, arg1, arg0);
8256       if (integer_zerop (arg1))
8257         return non_lvalue (fold_convert (type, arg0));
8258       if (operand_equal_p (arg0, arg1, 0))
8259         return non_lvalue (fold_convert (type, arg0));
8260
8261       /* ~X | X is -1.  */
8262       if (TREE_CODE (arg0) == BIT_NOT_EXPR
8263           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8264         {
8265           t1 = build_int_cst (type, -1);
8266           t1 = force_fit_type (t1, 0, false, false);
8267           return omit_one_operand (type, t1, arg1);
8268         }
8269
8270       /* X | ~X is -1.  */
8271       if (TREE_CODE (arg1) == BIT_NOT_EXPR
8272           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8273         {
8274           t1 = build_int_cst (type, -1);
8275           t1 = force_fit_type (t1, 0, false, false);
8276           return omit_one_operand (type, t1, arg0);
8277         }
8278
8279       t1 = distribute_bit_expr (code, type, arg0, arg1);
8280       if (t1 != NULL_TREE)
8281         return t1;
8282
8283       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
8284
8285          This results in more efficient code for machines without a NAND
8286          instruction.  Combine will canonicalize to the first form
8287          which will allow use of NAND instructions provided by the
8288          backend if they exist.  */
8289       if (TREE_CODE (arg0) == BIT_NOT_EXPR
8290           && TREE_CODE (arg1) == BIT_NOT_EXPR)
8291         {
8292           return fold_build1 (BIT_NOT_EXPR, type,
8293                               build2 (BIT_AND_EXPR, type,
8294                                       TREE_OPERAND (arg0, 0),
8295                                       TREE_OPERAND (arg1, 0)));
8296         }
8297
8298       /* See if this can be simplified into a rotate first.  If that
8299          is unsuccessful continue in the association code.  */
8300       goto bit_rotate;
8301
8302     case BIT_XOR_EXPR:
8303       if (integer_zerop (arg1))
8304         return non_lvalue (fold_convert (type, arg0));
8305       if (integer_all_onesp (arg1))
8306         return fold_build1 (BIT_NOT_EXPR, type, arg0);
8307       if (operand_equal_p (arg0, arg1, 0))
8308         return omit_one_operand (type, integer_zero_node, arg0);
8309
8310       /* ~X ^ X is -1.  */
8311       if (TREE_CODE (arg0) == BIT_NOT_EXPR
8312           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8313         {
8314           t1 = build_int_cst (type, -1);
8315           t1 = force_fit_type (t1, 0, false, false);
8316           return omit_one_operand (type, t1, arg1);
8317         }
8318
8319       /* X ^ ~X is -1.  */
8320       if (TREE_CODE (arg1) == BIT_NOT_EXPR
8321           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8322         {
8323           t1 = build_int_cst (type, -1);
8324           t1 = force_fit_type (t1, 0, false, false);
8325           return omit_one_operand (type, t1, arg0);
8326         }
8327
8328       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
8329          with a constant, and the two constants have no bits in common,
8330          we should treat this as a BIT_IOR_EXPR since this may produce more
8331          simplifications.  */
8332       if (TREE_CODE (arg0) == BIT_AND_EXPR
8333           && TREE_CODE (arg1) == BIT_AND_EXPR
8334           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8335           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8336           && integer_zerop (const_binop (BIT_AND_EXPR,
8337                                          TREE_OPERAND (arg0, 1),
8338                                          TREE_OPERAND (arg1, 1), 0)))
8339         {
8340           code = BIT_IOR_EXPR;
8341           goto bit_ior;
8342         }
8343
8344       /* Convert ~X ^ ~Y to X ^ Y.  */
8345       if (TREE_CODE (arg0) == BIT_NOT_EXPR
8346           && TREE_CODE (arg1) == BIT_NOT_EXPR)
8347         return fold_build2 (code, type,
8348                             fold_convert (type, TREE_OPERAND (arg0, 0)),
8349                             fold_convert (type, TREE_OPERAND (arg1, 0)));
8350
8351       /* See if this can be simplified into a rotate first.  If that
8352          is unsuccessful continue in the association code.  */
8353       goto bit_rotate;
8354
8355     case BIT_AND_EXPR:
8356       if (integer_all_onesp (arg1))
8357         return non_lvalue (fold_convert (type, arg0));
8358       if (integer_zerop (arg1))
8359         return omit_one_operand (type, arg1, arg0);
8360       if (operand_equal_p (arg0, arg1, 0))
8361         return non_lvalue (fold_convert (type, arg0));
8362
8363       /* ~X & X is always zero.  */
8364       if (TREE_CODE (arg0) == BIT_NOT_EXPR
8365           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8366         return omit_one_operand (type, integer_zero_node, arg1);
8367
8368       /* X & ~X is always zero.  */
8369       if (TREE_CODE (arg1) == BIT_NOT_EXPR
8370           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8371         return omit_one_operand (type, integer_zero_node, arg0);
8372
8373       t1 = distribute_bit_expr (code, type, arg0, arg1);
8374       if (t1 != NULL_TREE)
8375         return t1;
8376       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
8377       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
8378           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
8379         {
8380           unsigned int prec
8381             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
8382
8383           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
8384               && (~TREE_INT_CST_LOW (arg1)
8385                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
8386             return fold_convert (type, TREE_OPERAND (arg0, 0));
8387         }
8388
8389       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
8390
8391          This results in more efficient code for machines without a NOR
8392          instruction.  Combine will canonicalize to the first form
8393          which will allow use of NOR instructions provided by the
8394          backend if they exist.  */
8395       if (TREE_CODE (arg0) == BIT_NOT_EXPR
8396           && TREE_CODE (arg1) == BIT_NOT_EXPR)
8397         {
8398           return fold_build1 (BIT_NOT_EXPR, type,
8399                               build2 (BIT_IOR_EXPR, type,
8400                                       TREE_OPERAND (arg0, 0),
8401                                       TREE_OPERAND (arg1, 0)));
8402         }
8403
8404       goto associate;
8405
8406     case RDIV_EXPR:
8407       /* Don't touch a floating-point divide by zero unless the mode
8408          of the constant can represent infinity.  */
8409       if (TREE_CODE (arg1) == REAL_CST
8410           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
8411           && real_zerop (arg1))
8412         return NULL_TREE;
8413
8414       /* (-A) / (-B) -> A / B  */
8415       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
8416         return fold_build2 (RDIV_EXPR, type,
8417                             TREE_OPERAND (arg0, 0),
8418                             negate_expr (arg1));
8419       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
8420         return fold_build2 (RDIV_EXPR, type,
8421                             negate_expr (arg0),
8422                             TREE_OPERAND (arg1, 0));
8423
8424       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
8425       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
8426           && real_onep (arg1))
8427         return non_lvalue (fold_convert (type, arg0));
8428
8429       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
8430       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
8431           && real_minus_onep (arg1))
8432         return non_lvalue (fold_convert (type, negate_expr (arg0)));
8433
8434       /* If ARG1 is a constant, we can convert this to a multiply by the
8435          reciprocal.  This does not have the same rounding properties,
8436          so only do this if -funsafe-math-optimizations.  We can actually
8437          always safely do it if ARG1 is a power of two, but it's hard to
8438          tell if it is or not in a portable manner.  */
8439       if (TREE_CODE (arg1) == REAL_CST)
8440         {
8441           if (flag_unsafe_math_optimizations
8442               && 0 != (tem = const_binop (code, build_real (type, dconst1),
8443                                           arg1, 0)))
8444             return fold_build2 (MULT_EXPR, type, arg0, tem);
8445           /* Find the reciprocal if optimizing and the result is exact.  */
8446           if (optimize)
8447             {
8448               REAL_VALUE_TYPE r;
8449               r = TREE_REAL_CST (arg1);
8450               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
8451                 {
8452                   tem = build_real (type, r);
8453                   return fold_build2 (MULT_EXPR, type, arg0, tem);
8454                 }
8455             }
8456         }
8457       /* Convert A/B/C to A/(B*C).  */
8458       if (flag_unsafe_math_optimizations
8459           && TREE_CODE (arg0) == RDIV_EXPR)
8460         return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
8461                             fold_build2 (MULT_EXPR, type,
8462                                          TREE_OPERAND (arg0, 1), arg1));
8463
8464       /* Convert A/(B/C) to (A/B)*C.  */
8465       if (flag_unsafe_math_optimizations
8466           && TREE_CODE (arg1) == RDIV_EXPR)
8467         return fold_build2 (MULT_EXPR, type,
8468                             fold_build2 (RDIV_EXPR, type, arg0,
8469                                          TREE_OPERAND (arg1, 0)),
8470                             TREE_OPERAND (arg1, 1));
8471
8472       /* Convert C1/(X*C2) into (C1/C2)/X.  */
8473       if (flag_unsafe_math_optimizations
8474           && TREE_CODE (arg1) == MULT_EXPR
8475           && TREE_CODE (arg0) == REAL_CST
8476           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
8477         {
8478           tree tem = const_binop (RDIV_EXPR, arg0,
8479                                   TREE_OPERAND (arg1, 1), 0);
8480           if (tem)
8481             return fold_build2 (RDIV_EXPR, type, tem,
8482                                 TREE_OPERAND (arg1, 0));
8483         }
8484
8485       if (TREE_CODE (type) == COMPLEX_TYPE)
8486         {
8487           tem = fold_complex_div (type, arg0, arg1, code);
8488           if (tem)
8489             return tem;
8490         }
8491
8492       if (flag_unsafe_math_optimizations)
8493         {
8494           enum built_in_function fcode = builtin_mathfn_code (arg1);
8495           /* Optimize x/expN(y) into x*expN(-y).  */
8496           if (BUILTIN_EXPONENT_P (fcode))
8497             {
8498               tree expfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
8499               tree arg = negate_expr (TREE_VALUE (TREE_OPERAND (arg1, 1)));
8500               tree arglist = build_tree_list (NULL_TREE,
8501                                               fold_convert (type, arg));
8502               arg1 = build_function_call_expr (expfn, arglist);
8503               return fold_build2 (MULT_EXPR, type, arg0, arg1);
8504             }
8505
8506           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
8507           if (fcode == BUILT_IN_POW
8508               || fcode == BUILT_IN_POWF
8509               || fcode == BUILT_IN_POWL)
8510             {
8511               tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
8512               tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
8513               tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1, 1)));
8514               tree neg11 = fold_convert (type, negate_expr (arg11));
8515               tree arglist = tree_cons(NULL_TREE, arg10,
8516                                        build_tree_list (NULL_TREE, neg11));
8517               arg1 = build_function_call_expr (powfn, arglist);
8518               return fold_build2 (MULT_EXPR, type, arg0, arg1);
8519             }
8520         }
8521
8522       if (flag_unsafe_math_optimizations)
8523         {
8524           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
8525           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
8526
8527           /* Optimize sin(x)/cos(x) as tan(x).  */
8528           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
8529                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
8530                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
8531               && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
8532                                   TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
8533             {
8534               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
8535
8536               if (tanfn != NULL_TREE)
8537                 return build_function_call_expr (tanfn,
8538                                                  TREE_OPERAND (arg0, 1));
8539             }
8540
8541           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
8542           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
8543                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
8544                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
8545               && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
8546                                   TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
8547             {
8548               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
8549
8550               if (tanfn != NULL_TREE)
8551                 {
8552                   tree tmp = TREE_OPERAND (arg0, 1);
8553                   tmp = build_function_call_expr (tanfn, tmp);
8554                   return fold_build2 (RDIV_EXPR, type,
8555                                       build_real (type, dconst1), tmp);
8556                 }
8557             }
8558
8559           /* Optimize pow(x,c)/x as pow(x,c-1).  */
8560           if (fcode0 == BUILT_IN_POW
8561               || fcode0 == BUILT_IN_POWF
8562               || fcode0 == BUILT_IN_POWL)
8563             {
8564               tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8565               tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
8566               if (TREE_CODE (arg01) == REAL_CST
8567                   && ! TREE_CONSTANT_OVERFLOW (arg01)
8568                   && operand_equal_p (arg1, arg00, 0))
8569                 {
8570                   tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8571                   REAL_VALUE_TYPE c;
8572                   tree arg, arglist;
8573
8574                   c = TREE_REAL_CST (arg01);
8575                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
8576                   arg = build_real (type, c);
8577                   arglist = build_tree_list (NULL_TREE, arg);
8578                   arglist = tree_cons (NULL_TREE, arg1, arglist);
8579                   return build_function_call_expr (powfn, arglist);
8580                 }
8581             }
8582         }
8583       goto binary;
8584
8585     case TRUNC_DIV_EXPR:
8586     case ROUND_DIV_EXPR:
8587     case FLOOR_DIV_EXPR:
8588     case CEIL_DIV_EXPR:
8589     case EXACT_DIV_EXPR:
8590       if (integer_onep (arg1))
8591         return non_lvalue (fold_convert (type, arg0));
8592       if (integer_zerop (arg1))
8593         return NULL_TREE;
8594       /* X / -1 is -X.  */
8595       if (!TYPE_UNSIGNED (type)
8596           && TREE_CODE (arg1) == INTEGER_CST
8597           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
8598           && TREE_INT_CST_HIGH (arg1) == -1)
8599         return fold_convert (type, negate_expr (arg0));
8600
8601       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
8602          operation, EXACT_DIV_EXPR.
8603
8604          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
8605          At one time others generated faster code, it's not clear if they do
8606          after the last round to changes to the DIV code in expmed.c.  */
8607       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
8608           && multiple_of_p (type, arg0, arg1))
8609         return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
8610
8611       if (TREE_CODE (arg1) == INTEGER_CST
8612           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE)))
8613         return fold_convert (type, tem);
8614
8615       if (TREE_CODE (type) == COMPLEX_TYPE)
8616         {
8617           tem = fold_complex_div (type, arg0, arg1, code);
8618           if (tem)
8619             return tem;
8620         }
8621       goto binary;
8622
8623     case CEIL_MOD_EXPR:
8624     case FLOOR_MOD_EXPR:
8625     case ROUND_MOD_EXPR:
8626     case TRUNC_MOD_EXPR:
8627       /* X % 1 is always zero, but be sure to preserve any side
8628          effects in X.  */
8629       if (integer_onep (arg1))
8630         return omit_one_operand (type, integer_zero_node, arg0);
8631
8632       /* X % 0, return X % 0 unchanged so that we can get the
8633          proper warnings and errors.  */
8634       if (integer_zerop (arg1))
8635         return NULL_TREE;
8636
8637       /* 0 % X is always zero, but be sure to preserve any side
8638          effects in X.  Place this after checking for X == 0.  */
8639       if (integer_zerop (arg0))
8640         return omit_one_operand (type, integer_zero_node, arg1);
8641
8642       /* X % -1 is zero.  */
8643       if (!TYPE_UNSIGNED (type)
8644           && TREE_CODE (arg1) == INTEGER_CST
8645           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
8646           && TREE_INT_CST_HIGH (arg1) == -1)
8647         return omit_one_operand (type, integer_zero_node, arg0);
8648
8649       /* Optimize unsigned TRUNC_MOD_EXPR by a power of two into a
8650          BIT_AND_EXPR, i.e. "X % C" into "X & C2".  */
8651       if (code == TRUNC_MOD_EXPR
8652           && TYPE_UNSIGNED (type)
8653           && integer_pow2p (arg1))
8654         {
8655           unsigned HOST_WIDE_INT high, low;
8656           tree mask;
8657           int l;
8658
8659           l = tree_log2 (arg1);
8660           if (l >= HOST_BITS_PER_WIDE_INT)
8661             {
8662               high = ((unsigned HOST_WIDE_INT) 1
8663                       << (l - HOST_BITS_PER_WIDE_INT)) - 1;
8664               low = -1;
8665             }
8666           else
8667             {
8668               high = 0;
8669               low = ((unsigned HOST_WIDE_INT) 1 << l) - 1;
8670             }
8671
8672           mask = build_int_cst_wide (type, low, high);
8673           return fold_build2 (BIT_AND_EXPR, type,
8674                               fold_convert (type, arg0), mask);
8675         }
8676
8677       /* X % -C is the same as X % C.  */
8678       if (code == TRUNC_MOD_EXPR
8679           && !TYPE_UNSIGNED (type)
8680           && TREE_CODE (arg1) == INTEGER_CST
8681           && !TREE_CONSTANT_OVERFLOW (arg1)
8682           && TREE_INT_CST_HIGH (arg1) < 0
8683           && !flag_trapv
8684           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
8685           && !sign_bit_p (arg1, arg1))
8686         return fold_build2 (code, type, fold_convert (type, arg0),
8687                             fold_convert (type, negate_expr (arg1)));
8688
8689       /* X % -Y is the same as X % Y.  */
8690       if (code == TRUNC_MOD_EXPR
8691           && !TYPE_UNSIGNED (type)
8692           && TREE_CODE (arg1) == NEGATE_EXPR
8693           && !flag_trapv)
8694         return fold_build2 (code, type, fold_convert (type, arg0),
8695                             fold_convert (type, TREE_OPERAND (arg1, 0)));
8696
8697       if (TREE_CODE (arg1) == INTEGER_CST
8698           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE)))
8699         return fold_convert (type, tem);
8700
8701       goto binary;
8702
8703     case LROTATE_EXPR:
8704     case RROTATE_EXPR:
8705       if (integer_all_onesp (arg0))
8706         return omit_one_operand (type, arg0, arg1);
8707       goto shift;
8708
8709     case RSHIFT_EXPR:
8710       /* Optimize -1 >> x for arithmetic right shifts.  */
8711       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
8712         return omit_one_operand (type, arg0, arg1);
8713       /* ... fall through ...  */
8714
8715     case LSHIFT_EXPR:
8716     shift:
8717       if (integer_zerop (arg1))
8718         return non_lvalue (fold_convert (type, arg0));
8719       if (integer_zerop (arg0))
8720         return omit_one_operand (type, arg0, arg1);
8721
8722       /* Since negative shift count is not well-defined,
8723          don't try to compute it in the compiler.  */
8724       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
8725         return NULL_TREE;
8726       /* Rewrite an LROTATE_EXPR by a constant into an
8727          RROTATE_EXPR by a new constant.  */
8728       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
8729         {
8730           tree tem = build_int_cst (NULL_TREE,
8731                                     GET_MODE_BITSIZE (TYPE_MODE (type)));
8732           tem = fold_convert (TREE_TYPE (arg1), tem);
8733           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
8734           return fold_build2 (RROTATE_EXPR, type, arg0, tem);
8735         }
8736
8737       /* If we have a rotate of a bit operation with the rotate count and
8738          the second operand of the bit operation both constant,
8739          permute the two operations.  */
8740       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
8741           && (TREE_CODE (arg0) == BIT_AND_EXPR
8742               || TREE_CODE (arg0) == BIT_IOR_EXPR
8743               || TREE_CODE (arg0) == BIT_XOR_EXPR)
8744           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8745         return fold_build2 (TREE_CODE (arg0), type,
8746                             fold_build2 (code, type,
8747                                          TREE_OPERAND (arg0, 0), arg1),
8748                             fold_build2 (code, type,
8749                                          TREE_OPERAND (arg0, 1), arg1));
8750
8751       /* Two consecutive rotates adding up to the width of the mode can
8752          be ignored.  */
8753       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
8754           && TREE_CODE (arg0) == RROTATE_EXPR
8755           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8756           && TREE_INT_CST_HIGH (arg1) == 0
8757           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
8758           && ((TREE_INT_CST_LOW (arg1)
8759                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
8760               == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
8761         return TREE_OPERAND (arg0, 0);
8762
8763       goto binary;
8764
8765     case MIN_EXPR:
8766       if (operand_equal_p (arg0, arg1, 0))
8767         return omit_one_operand (type, arg0, arg1);
8768       if (INTEGRAL_TYPE_P (type)
8769           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
8770         return omit_one_operand (type, arg1, arg0);
8771       goto associate;
8772
8773     case MAX_EXPR:
8774       if (operand_equal_p (arg0, arg1, 0))
8775         return omit_one_operand (type, arg0, arg1);
8776       if (INTEGRAL_TYPE_P (type)
8777           && TYPE_MAX_VALUE (type)
8778           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
8779         return omit_one_operand (type, arg1, arg0);
8780       goto associate;
8781
8782     case TRUTH_ANDIF_EXPR:
8783       /* Note that the operands of this must be ints
8784          and their values must be 0 or 1.
8785          ("true" is a fixed value perhaps depending on the language.)  */
8786       /* If first arg is constant zero, return it.  */
8787       if (integer_zerop (arg0))
8788         return fold_convert (type, arg0);
8789     case TRUTH_AND_EXPR:
8790       /* If either arg is constant true, drop it.  */
8791       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
8792         return non_lvalue (fold_convert (type, arg1));
8793       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
8794           /* Preserve sequence points.  */
8795           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
8796         return non_lvalue (fold_convert (type, arg0));
8797       /* If second arg is constant zero, result is zero, but first arg
8798          must be evaluated.  */
8799       if (integer_zerop (arg1))
8800         return omit_one_operand (type, arg1, arg0);
8801       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
8802          case will be handled here.  */
8803       if (integer_zerop (arg0))
8804         return omit_one_operand (type, arg0, arg1);
8805
8806       /* !X && X is always false.  */
8807       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
8808           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8809         return omit_one_operand (type, integer_zero_node, arg1);
8810       /* X && !X is always false.  */
8811       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
8812           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8813         return omit_one_operand (type, integer_zero_node, arg0);
8814
8815       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
8816          means A >= Y && A != MAX, but in this case we know that
8817          A < X <= MAX.  */
8818
8819       if (!TREE_SIDE_EFFECTS (arg0)
8820           && !TREE_SIDE_EFFECTS (arg1))
8821         {
8822           tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
8823           if (tem)
8824             return fold_build2 (code, type, tem, arg1);
8825
8826           tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
8827           if (tem)
8828             return fold_build2 (code, type, arg0, tem);
8829         }
8830
8831     truth_andor:
8832       /* We only do these simplifications if we are optimizing.  */
8833       if (!optimize)
8834         return NULL_TREE;
8835
8836       /* Check for things like (A || B) && (A || C).  We can convert this
8837          to A || (B && C).  Note that either operator can be any of the four
8838          truth and/or operations and the transformation will still be
8839          valid.   Also note that we only care about order for the
8840          ANDIF and ORIF operators.  If B contains side effects, this
8841          might change the truth-value of A.  */
8842       if (TREE_CODE (arg0) == TREE_CODE (arg1)
8843           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8844               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8845               || TREE_CODE (arg0) == TRUTH_AND_EXPR
8846               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8847           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8848         {
8849           tree a00 = TREE_OPERAND (arg0, 0);
8850           tree a01 = TREE_OPERAND (arg0, 1);
8851           tree a10 = TREE_OPERAND (arg1, 0);
8852           tree a11 = TREE_OPERAND (arg1, 1);
8853           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8854                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8855                              && (code == TRUTH_AND_EXPR
8856                                  || code == TRUTH_OR_EXPR));
8857
8858           if (operand_equal_p (a00, a10, 0))
8859             return fold_build2 (TREE_CODE (arg0), type, a00,
8860                                 fold_build2 (code, type, a01, a11));
8861           else if (commutative && operand_equal_p (a00, a11, 0))
8862             return fold_build2 (TREE_CODE (arg0), type, a00,
8863                                 fold_build2 (code, type, a01, a10));
8864           else if (commutative && operand_equal_p (a01, a10, 0))
8865             return fold_build2 (TREE_CODE (arg0), type, a01,
8866                                 fold_build2 (code, type, a00, a11));
8867
8868           /* This case if tricky because we must either have commutative
8869              operators or else A10 must not have side-effects.  */
8870
8871           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8872                    && operand_equal_p (a01, a11, 0))
8873             return fold_build2 (TREE_CODE (arg0), type,
8874                                 fold_build2 (code, type, a00, a10),
8875                                 a01);
8876         }
8877
8878       /* See if we can build a range comparison.  */
8879       if (0 != (tem = fold_range_test (code, type, op0, op1)))
8880         return tem;
8881
8882       /* Check for the possibility of merging component references.  If our
8883          lhs is another similar operation, try to merge its rhs with our
8884          rhs.  Then try to merge our lhs and rhs.  */
8885       if (TREE_CODE (arg0) == code
8886           && 0 != (tem = fold_truthop (code, type,
8887                                        TREE_OPERAND (arg0, 1), arg1)))
8888         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
8889
8890       if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
8891         return tem;
8892
8893       return NULL_TREE;
8894
8895     case TRUTH_ORIF_EXPR:
8896       /* Note that the operands of this must be ints
8897          and their values must be 0 or true.
8898          ("true" is a fixed value perhaps depending on the language.)  */
8899       /* If first arg is constant true, return it.  */
8900       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
8901         return fold_convert (type, arg0);
8902     case TRUTH_OR_EXPR:
8903       /* If either arg is constant zero, drop it.  */
8904       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
8905         return non_lvalue (fold_convert (type, arg1));
8906       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
8907           /* Preserve sequence points.  */
8908           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
8909         return non_lvalue (fold_convert (type, arg0));
8910       /* If second arg is constant true, result is true, but we must
8911          evaluate first arg.  */
8912       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
8913         return omit_one_operand (type, arg1, arg0);
8914       /* Likewise for first arg, but note this only occurs here for
8915          TRUTH_OR_EXPR.  */
8916       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
8917         return omit_one_operand (type, arg0, arg1);
8918
8919       /* !X || X is always true.  */
8920       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
8921           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8922         return omit_one_operand (type, integer_one_node, arg1);
8923       /* X || !X is always true.  */
8924       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
8925           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8926         return omit_one_operand (type, integer_one_node, arg0);
8927
8928       goto truth_andor;
8929
8930     case TRUTH_XOR_EXPR:
8931       /* If the second arg is constant zero, drop it.  */
8932       if (integer_zerop (arg1))
8933         return non_lvalue (fold_convert (type, arg0));
8934       /* If the second arg is constant true, this is a logical inversion.  */
8935       if (integer_onep (arg1))
8936         {
8937           /* Only call invert_truthvalue if operand is a truth value.  */
8938           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8939             tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
8940           else
8941             tem = invert_truthvalue (arg0);
8942           return non_lvalue (fold_convert (type, tem));
8943         }
8944       /* Identical arguments cancel to zero.  */
8945       if (operand_equal_p (arg0, arg1, 0))
8946         return omit_one_operand (type, integer_zero_node, arg0);
8947
8948       /* !X ^ X is always true.  */
8949       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
8950           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8951         return omit_one_operand (type, integer_one_node, arg1);
8952
8953       /* X ^ !X is always true.  */
8954       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
8955           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8956         return omit_one_operand (type, integer_one_node, arg0);
8957
8958       return NULL_TREE;
8959
8960     case EQ_EXPR:
8961     case NE_EXPR:
8962     case LT_EXPR:
8963     case GT_EXPR:
8964     case LE_EXPR:
8965     case GE_EXPR:       
8966       /* If one arg is a real or integer constant, put it last.  */
8967       if (tree_swap_operands_p (arg0, arg1, true))
8968         return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8969         
8970       /* bool_var != 0 becomes bool_var. */
8971       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
8972           && code == NE_EXPR)
8973         return non_lvalue (fold_convert (type, arg0));
8974         
8975       /* bool_var == 1 becomes bool_var. */
8976       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
8977           && code == EQ_EXPR)
8978         return non_lvalue (fold_convert (type, arg0));
8979
8980       /* If this is an equality comparison of the address of a non-weak
8981          object against zero, then we know the result.  */
8982       if ((code == EQ_EXPR || code == NE_EXPR)
8983           && TREE_CODE (arg0) == ADDR_EXPR
8984           && DECL_P (TREE_OPERAND (arg0, 0))
8985           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
8986           && integer_zerop (arg1))
8987         return constant_boolean_node (code != EQ_EXPR, type);
8988
8989       /* If this is an equality comparison of the address of two non-weak,
8990          unaliased symbols neither of which are extern (since we do not
8991          have access to attributes for externs), then we know the result.  */
8992       if ((code == EQ_EXPR || code == NE_EXPR)
8993           && TREE_CODE (arg0) == ADDR_EXPR
8994           && DECL_P (TREE_OPERAND (arg0, 0))
8995           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
8996           && ! lookup_attribute ("alias",
8997                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
8998           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
8999           && TREE_CODE (arg1) == ADDR_EXPR
9000           && DECL_P (TREE_OPERAND (arg1, 0))
9001           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
9002           && ! lookup_attribute ("alias",
9003                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
9004           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
9005         return constant_boolean_node (operand_equal_p (arg0, arg1, 0)
9006                                       ? code == EQ_EXPR : code != EQ_EXPR,
9007                                       type);
9008
9009       /* If this is a comparison of two exprs that look like an
9010          ARRAY_REF of the same object, then we can fold this to a
9011          comparison of the two offsets.  */
9012       if (TREE_CODE_CLASS (code) == tcc_comparison)
9013         {
9014           tree base0, offset0, base1, offset1;
9015
9016           if (extract_array_ref (arg0, &base0, &offset0)
9017               && extract_array_ref (arg1, &base1, &offset1)
9018               && operand_equal_p (base0, base1, 0))
9019             {
9020               if (offset0 == NULL_TREE
9021                   && offset1 == NULL_TREE)
9022                 {
9023                   offset0 = integer_zero_node;
9024                   offset1 = integer_zero_node;
9025                 }
9026               else if (offset0 == NULL_TREE)
9027                 offset0 = build_int_cst (TREE_TYPE (offset1), 0);
9028               else if (offset1 == NULL_TREE)
9029                 offset1 = build_int_cst (TREE_TYPE (offset0), 0);
9030
9031               if (TREE_TYPE (offset0) == TREE_TYPE (offset1))
9032                 return fold_build2 (code, type, offset0, offset1);
9033             }
9034         }
9035
9036       /* Transform comparisons of the form X +- C CMP X.  */
9037       if ((code != EQ_EXPR && code != NE_EXPR)
9038           && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9039           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
9040           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9041                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
9042               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9043                   && !TYPE_UNSIGNED (TREE_TYPE (arg1))
9044                   && !(flag_wrapv || flag_trapv))))
9045         {
9046           tree arg01 = TREE_OPERAND (arg0, 1);
9047           enum tree_code code0 = TREE_CODE (arg0);
9048           int is_positive;
9049
9050           if (TREE_CODE (arg01) == REAL_CST)
9051             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
9052           else
9053             is_positive = tree_int_cst_sgn (arg01);
9054
9055           /* (X - c) > X becomes false.  */
9056           if (code == GT_EXPR
9057               && ((code0 == MINUS_EXPR && is_positive >= 0)
9058                   || (code0 == PLUS_EXPR && is_positive <= 0)))
9059             return constant_boolean_node (0, type);
9060
9061           /* Likewise (X + c) < X becomes false.  */
9062           if (code == LT_EXPR
9063               && ((code0 == PLUS_EXPR && is_positive >= 0)
9064                   || (code0 == MINUS_EXPR && is_positive <= 0)))
9065             return constant_boolean_node (0, type);
9066
9067           /* Convert (X - c) <= X to true.  */
9068           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
9069               && code == LE_EXPR
9070               && ((code0 == MINUS_EXPR && is_positive >= 0)
9071                   || (code0 == PLUS_EXPR && is_positive <= 0)))
9072             return constant_boolean_node (1, type);
9073
9074           /* Convert (X + c) >= X to true.  */
9075           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
9076               && code == GE_EXPR
9077               && ((code0 == PLUS_EXPR && is_positive >= 0)
9078                   || (code0 == MINUS_EXPR && is_positive <= 0)))
9079             return constant_boolean_node (1, type);
9080
9081           if (TREE_CODE (arg01) == INTEGER_CST)
9082             {
9083               /* Convert X + c > X and X - c < X to true for integers.  */
9084               if (code == GT_EXPR
9085                   && ((code0 == PLUS_EXPR && is_positive > 0)
9086                       || (code0 == MINUS_EXPR && is_positive < 0)))
9087                 return constant_boolean_node (1, type);
9088
9089               if (code == LT_EXPR
9090                   && ((code0 == MINUS_EXPR && is_positive > 0)
9091                       || (code0 == PLUS_EXPR && is_positive < 0)))
9092                 return constant_boolean_node (1, type);
9093
9094               /* Convert X + c <= X and X - c >= X to false for integers.  */
9095               if (code == LE_EXPR
9096                   && ((code0 == PLUS_EXPR && is_positive > 0)
9097                       || (code0 == MINUS_EXPR && is_positive < 0)))
9098                 return constant_boolean_node (0, type);
9099
9100               if (code == GE_EXPR
9101                   && ((code0 == MINUS_EXPR && is_positive > 0)
9102                       || (code0 == PLUS_EXPR && is_positive < 0)))
9103                 return constant_boolean_node (0, type);
9104             }
9105         }
9106
9107       if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9108         {
9109           tree targ0 = strip_float_extensions (arg0);
9110           tree targ1 = strip_float_extensions (arg1);
9111           tree newtype = TREE_TYPE (targ0);
9112
9113           if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9114             newtype = TREE_TYPE (targ1);
9115
9116           /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
9117           if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9118             return fold_build2 (code, type, fold_convert (newtype, targ0),
9119                                 fold_convert (newtype, targ1));
9120
9121           /* (-a) CMP (-b) -> b CMP a  */
9122           if (TREE_CODE (arg0) == NEGATE_EXPR
9123               && TREE_CODE (arg1) == NEGATE_EXPR)
9124             return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
9125                                 TREE_OPERAND (arg0, 0));
9126
9127           if (TREE_CODE (arg1) == REAL_CST)
9128           {
9129             REAL_VALUE_TYPE cst;
9130             cst = TREE_REAL_CST (arg1);
9131
9132             /* (-a) CMP CST -> a swap(CMP) (-CST)  */
9133             if (TREE_CODE (arg0) == NEGATE_EXPR)
9134               return
9135                 fold_build2 (swap_tree_comparison (code), type,
9136                              TREE_OPERAND (arg0, 0),
9137                              build_real (TREE_TYPE (arg1),
9138                                          REAL_VALUE_NEGATE (cst)));
9139
9140             /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
9141             /* a CMP (-0) -> a CMP 0  */
9142             if (REAL_VALUE_MINUS_ZERO (cst))
9143               return fold_build2 (code, type, arg0,
9144                                   build_real (TREE_TYPE (arg1), dconst0));
9145
9146             /* x != NaN is always true, other ops are always false.  */
9147             if (REAL_VALUE_ISNAN (cst)
9148                 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9149               {
9150                 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9151                 return omit_one_operand (type, tem, arg0);
9152               }
9153
9154             /* Fold comparisons against infinity.  */
9155             if (REAL_VALUE_ISINF (cst))
9156               {
9157                 tem = fold_inf_compare (code, type, arg0, arg1);
9158                 if (tem != NULL_TREE)
9159                   return tem;
9160               }
9161           }
9162
9163           /* If this is a comparison of a real constant with a PLUS_EXPR
9164              or a MINUS_EXPR of a real constant, we can convert it into a
9165              comparison with a revised real constant as long as no overflow
9166              occurs when unsafe_math_optimizations are enabled.  */
9167           if (flag_unsafe_math_optimizations
9168               && TREE_CODE (arg1) == REAL_CST
9169               && (TREE_CODE (arg0) == PLUS_EXPR
9170                   || TREE_CODE (arg0) == MINUS_EXPR)
9171               && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9172               && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9173                                           ? MINUS_EXPR : PLUS_EXPR,
9174                                           arg1, TREE_OPERAND (arg0, 1), 0))
9175               && ! TREE_CONSTANT_OVERFLOW (tem))
9176             return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9177
9178           /* Likewise, we can simplify a comparison of a real constant with
9179              a MINUS_EXPR whose first operand is also a real constant, i.e.
9180              (c1 - x) < c2 becomes x > c1-c2.  */
9181           if (flag_unsafe_math_optimizations
9182               && TREE_CODE (arg1) == REAL_CST
9183               && TREE_CODE (arg0) == MINUS_EXPR
9184               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9185               && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9186                                           arg1, 0))
9187               && ! TREE_CONSTANT_OVERFLOW (tem))
9188             return fold_build2 (swap_tree_comparison (code), type,
9189                                 TREE_OPERAND (arg0, 1), tem);
9190
9191           /* Fold comparisons against built-in math functions.  */
9192           if (TREE_CODE (arg1) == REAL_CST
9193               && flag_unsafe_math_optimizations
9194               && ! flag_errno_math)
9195             {
9196               enum built_in_function fcode = builtin_mathfn_code (arg0);
9197
9198               if (fcode != END_BUILTINS)
9199                 {
9200                   tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
9201                   if (tem != NULL_TREE)
9202                     return tem;
9203                 }
9204             }
9205         }
9206
9207       /* Convert foo++ == CONST into ++foo == CONST + INCR.  */
9208       if (TREE_CONSTANT (arg1)
9209           && (TREE_CODE (arg0) == POSTINCREMENT_EXPR
9210               || TREE_CODE (arg0) == POSTDECREMENT_EXPR)
9211           /* This optimization is invalid for ordered comparisons
9212              if CONST+INCR overflows or if foo+incr might overflow.
9213              This optimization is invalid for floating point due to rounding.
9214              For pointer types we assume overflow doesn't happen.  */
9215           && (POINTER_TYPE_P (TREE_TYPE (arg0))
9216               || (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9217                   && (code == EQ_EXPR || code == NE_EXPR))))
9218         {
9219           tree varop, newconst;
9220
9221           if (TREE_CODE (arg0) == POSTINCREMENT_EXPR)
9222             {
9223               newconst = fold_build2 (PLUS_EXPR, TREE_TYPE (arg0),
9224                                       arg1, TREE_OPERAND (arg0, 1));
9225               varop = build2 (PREINCREMENT_EXPR, TREE_TYPE (arg0),
9226                               TREE_OPERAND (arg0, 0),
9227                               TREE_OPERAND (arg0, 1));
9228             }
9229           else
9230             {
9231               newconst = fold_build2 (MINUS_EXPR, TREE_TYPE (arg0),
9232                                       arg1, TREE_OPERAND (arg0, 1));
9233               varop = build2 (PREDECREMENT_EXPR, TREE_TYPE (arg0),
9234                               TREE_OPERAND (arg0, 0),
9235                               TREE_OPERAND (arg0, 1));
9236             }
9237
9238
9239           /* If VAROP is a reference to a bitfield, we must mask
9240              the constant by the width of the field.  */
9241           if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
9242               && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (varop, 0), 1))
9243               && host_integerp (DECL_SIZE (TREE_OPERAND
9244                                            (TREE_OPERAND (varop, 0), 1)), 1))
9245             {
9246               tree fielddecl = TREE_OPERAND (TREE_OPERAND (varop, 0), 1);
9247               HOST_WIDE_INT size = tree_low_cst (DECL_SIZE (fielddecl), 1);
9248               tree folded_compare, shift;
9249
9250               /* First check whether the comparison would come out
9251                  always the same.  If we don't do that we would
9252                  change the meaning with the masking.  */
9253               folded_compare = fold_build2 (code, type,
9254                                             TREE_OPERAND (varop, 0), arg1);
9255               if (integer_zerop (folded_compare)
9256                   || integer_onep (folded_compare))
9257                 return omit_one_operand (type, folded_compare, varop);
9258
9259               shift = build_int_cst (NULL_TREE,
9260                                      TYPE_PRECISION (TREE_TYPE (varop)) - size);
9261               shift = fold_convert (TREE_TYPE (varop), shift);
9262               newconst = fold_build2 (LSHIFT_EXPR, TREE_TYPE (varop),
9263                                       newconst, shift);
9264               newconst = fold_build2 (RSHIFT_EXPR, TREE_TYPE (varop),
9265                                       newconst, shift);
9266             }
9267
9268           return fold_build2 (code, type, varop, newconst);
9269         }
9270
9271       /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
9272          This transformation affects the cases which are handled in later
9273          optimizations involving comparisons with non-negative constants.  */
9274       if (TREE_CODE (arg1) == INTEGER_CST
9275           && TREE_CODE (arg0) != INTEGER_CST
9276           && tree_int_cst_sgn (arg1) > 0)
9277         {
9278           switch (code)
9279             {
9280             case GE_EXPR:
9281               arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
9282               return fold_build2 (GT_EXPR, type, arg0, arg1);
9283
9284             case LT_EXPR:
9285               arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
9286               return fold_build2 (LE_EXPR, type, arg0, arg1);
9287
9288             default:
9289               break;
9290             }
9291         }
9292
9293       /* Comparisons with the highest or lowest possible integer of
9294          the specified size will have known values.  */
9295       {
9296         int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
9297
9298         if (TREE_CODE (arg1) == INTEGER_CST
9299             && ! TREE_CONSTANT_OVERFLOW (arg1)
9300             && width <= 2 * HOST_BITS_PER_WIDE_INT
9301             && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9302                 || POINTER_TYPE_P (TREE_TYPE (arg1))))
9303           {
9304             HOST_WIDE_INT signed_max_hi;
9305             unsigned HOST_WIDE_INT signed_max_lo;
9306             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
9307
9308             if (width <= HOST_BITS_PER_WIDE_INT)
9309               {
9310                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
9311                                 - 1;
9312                 signed_max_hi = 0;
9313                 max_hi = 0;
9314
9315                 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
9316                   {
9317                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
9318                     min_lo = 0;
9319                     min_hi = 0;
9320                   }
9321                 else
9322                   {
9323                     max_lo = signed_max_lo;
9324                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
9325                     min_hi = -1;
9326                   }
9327               }
9328             else
9329               {
9330                 width -= HOST_BITS_PER_WIDE_INT;
9331                 signed_max_lo = -1;
9332                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
9333                                 - 1;
9334                 max_lo = -1;
9335                 min_lo = 0;
9336
9337                 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
9338                   {
9339                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
9340                     min_hi = 0;
9341                   }
9342                 else
9343                   {
9344                     max_hi = signed_max_hi;
9345                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
9346                   }
9347               }
9348
9349             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
9350                 && TREE_INT_CST_LOW (arg1) == max_lo)
9351               switch (code)
9352                 {
9353                 case GT_EXPR:
9354                   return omit_one_operand (type, integer_zero_node, arg0);
9355
9356                 case GE_EXPR:
9357                   return fold_build2 (EQ_EXPR, type, arg0, arg1);
9358
9359                 case LE_EXPR:
9360                   return omit_one_operand (type, integer_one_node, arg0);
9361
9362                 case LT_EXPR:
9363                   return fold_build2 (NE_EXPR, type, arg0, arg1);
9364
9365                 /* The GE_EXPR and LT_EXPR cases above are not normally
9366                    reached because of previous transformations.  */
9367
9368                 default:
9369                   break;
9370                 }
9371             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
9372                      == max_hi
9373                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
9374               switch (code)
9375                 {
9376                 case GT_EXPR:
9377                   arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
9378                   return fold_build2 (EQ_EXPR, type, arg0, arg1);
9379                 case LE_EXPR:
9380                   arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
9381                   return fold_build2 (NE_EXPR, type, arg0, arg1);
9382                 default:
9383                   break;
9384                 }
9385             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
9386                      == min_hi
9387                      && TREE_INT_CST_LOW (arg1) == min_lo)
9388               switch (code)
9389                 {
9390                 case LT_EXPR:
9391                   return omit_one_operand (type, integer_zero_node, arg0);
9392
9393                 case LE_EXPR:
9394                   return fold_build2 (EQ_EXPR, type, arg0, arg1);
9395
9396                 case GE_EXPR:
9397                   return omit_one_operand (type, integer_one_node, arg0);
9398
9399                 case GT_EXPR:
9400                   return fold_build2 (NE_EXPR, type, arg0, arg1);
9401
9402                 default:
9403                   break;
9404                 }
9405             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
9406                      == min_hi
9407                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
9408               switch (code)
9409                 {
9410                 case GE_EXPR:
9411                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
9412                   return fold_build2 (NE_EXPR, type, arg0, arg1);
9413                 case LT_EXPR:
9414                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
9415                   return fold_build2 (EQ_EXPR, type, arg0, arg1);
9416                 default:
9417                   break;
9418                 }
9419
9420             else if (!in_gimple_form
9421                      && TREE_INT_CST_HIGH (arg1) == signed_max_hi
9422                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
9423                      && TYPE_UNSIGNED (TREE_TYPE (arg1))
9424                      /* signed_type does not work on pointer types.  */
9425                      && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
9426               {
9427                 /* The following case also applies to X < signed_max+1
9428                    and X >= signed_max+1 because previous transformations.  */
9429                 if (code == LE_EXPR || code == GT_EXPR)
9430                   {
9431                     tree st0, st1;
9432                     st0 = lang_hooks.types.signed_type (TREE_TYPE (arg0));
9433                     st1 = lang_hooks.types.signed_type (TREE_TYPE (arg1));
9434                     return fold
9435                       (build2 (code == LE_EXPR ? GE_EXPR: LT_EXPR,
9436                                type, fold_convert (st0, arg0),
9437                                fold_convert (st1, integer_zero_node)));
9438                   }
9439               }
9440           }
9441       }
9442
9443       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
9444          a MINUS_EXPR of a constant, we can convert it into a comparison with
9445          a revised constant as long as no overflow occurs.  */
9446       if ((code == EQ_EXPR || code == NE_EXPR)
9447           && TREE_CODE (arg1) == INTEGER_CST
9448           && (TREE_CODE (arg0) == PLUS_EXPR
9449               || TREE_CODE (arg0) == MINUS_EXPR)
9450           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9451           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9452                                       ? MINUS_EXPR : PLUS_EXPR,
9453                                       arg1, TREE_OPERAND (arg0, 1), 0))
9454           && ! TREE_CONSTANT_OVERFLOW (tem))
9455         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9456
9457       /* Similarly for a NEGATE_EXPR.  */
9458       else if ((code == EQ_EXPR || code == NE_EXPR)
9459                && TREE_CODE (arg0) == NEGATE_EXPR
9460                && TREE_CODE (arg1) == INTEGER_CST
9461                && 0 != (tem = negate_expr (arg1))
9462                && TREE_CODE (tem) == INTEGER_CST
9463                && ! TREE_CONSTANT_OVERFLOW (tem))
9464         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9465
9466       /* If we have X - Y == 0, we can convert that to X == Y and similarly
9467          for !=.  Don't do this for ordered comparisons due to overflow.  */
9468       else if ((code == NE_EXPR || code == EQ_EXPR)
9469                && integer_zerop (arg1) && TREE_CODE (arg0) == MINUS_EXPR)
9470         return fold_build2 (code, type,
9471                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
9472
9473       else if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9474                && (TREE_CODE (arg0) == NOP_EXPR
9475                    || TREE_CODE (arg0) == CONVERT_EXPR))
9476         {
9477           /* If we are widening one operand of an integer comparison,
9478              see if the other operand is similarly being widened.  Perhaps we
9479              can do the comparison in the narrower type.  */
9480           tem = fold_widened_comparison (code, type, arg0, arg1);
9481           if (tem)
9482             return tem;
9483
9484           /* Or if we are changing signedness.  */
9485           tem = fold_sign_changed_comparison (code, type, arg0, arg1);
9486           if (tem)
9487             return tem;
9488         }
9489
9490       /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9491          constant, we can simplify it.  */
9492       else if (TREE_CODE (arg1) == INTEGER_CST
9493                && (TREE_CODE (arg0) == MIN_EXPR
9494                    || TREE_CODE (arg0) == MAX_EXPR)
9495                && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9496         {
9497           tem = optimize_minmax_comparison (code, type, op0, op1);
9498           if (tem)
9499             return tem;
9500
9501           return NULL_TREE;
9502         }
9503
9504       /* If we are comparing an ABS_EXPR with a constant, we can
9505          convert all the cases into explicit comparisons, but they may
9506          well not be faster than doing the ABS and one comparison.
9507          But ABS (X) <= C is a range comparison, which becomes a subtraction
9508          and a comparison, and is probably faster.  */
9509       else if (code == LE_EXPR && TREE_CODE (arg1) == INTEGER_CST
9510                && TREE_CODE (arg0) == ABS_EXPR
9511                && ! TREE_SIDE_EFFECTS (arg0)
9512                && (0 != (tem = negate_expr (arg1)))
9513                && TREE_CODE (tem) == INTEGER_CST
9514                && ! TREE_CONSTANT_OVERFLOW (tem))
9515         return fold_build2 (TRUTH_ANDIF_EXPR, type,
9516                             build2 (GE_EXPR, type,
9517                                     TREE_OPERAND (arg0, 0), tem),
9518                             build2 (LE_EXPR, type,
9519                                     TREE_OPERAND (arg0, 0), arg1));
9520
9521       /* Convert ABS_EXPR<x> >= 0 to true.  */
9522       else if (code == GE_EXPR
9523                && tree_expr_nonnegative_p (arg0)
9524                && (integer_zerop (arg1)
9525                    || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
9526                        && real_zerop (arg1))))
9527         return omit_one_operand (type, integer_one_node, arg0);
9528
9529       /* Convert ABS_EXPR<x> < 0 to false.  */
9530       else if (code == LT_EXPR
9531                && tree_expr_nonnegative_p (arg0)
9532                && (integer_zerop (arg1) || real_zerop (arg1)))
9533         return omit_one_operand (type, integer_zero_node, arg0);
9534
9535       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
9536       else if ((code == EQ_EXPR || code == NE_EXPR)
9537                && TREE_CODE (arg0) == ABS_EXPR
9538                && (integer_zerop (arg1) || real_zerop (arg1)))
9539         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
9540
9541       /* If this is an EQ or NE comparison with zero and ARG0 is
9542          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
9543          two operations, but the latter can be done in one less insn
9544          on machines that have only two-operand insns or on which a
9545          constant cannot be the first operand.  */
9546       if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
9547           && TREE_CODE (arg0) == BIT_AND_EXPR)
9548         {
9549           tree arg00 = TREE_OPERAND (arg0, 0);
9550           tree arg01 = TREE_OPERAND (arg0, 1);
9551           if (TREE_CODE (arg00) == LSHIFT_EXPR
9552               && integer_onep (TREE_OPERAND (arg00, 0)))
9553             return
9554               fold_build2 (code, type,
9555                            build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9556                                    build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
9557                                            arg01, TREE_OPERAND (arg00, 1)),
9558                                    fold_convert (TREE_TYPE (arg0),
9559                                                  integer_one_node)),
9560                            arg1);
9561           else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
9562                    && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
9563             return
9564               fold_build2 (code, type,
9565                            build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9566                                    build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
9567                                            arg00, TREE_OPERAND (arg01, 1)),
9568                                    fold_convert (TREE_TYPE (arg0),
9569                                                  integer_one_node)),
9570                            arg1);
9571         }
9572
9573       /* If this is an NE or EQ comparison of zero against the result of a
9574          signed MOD operation whose second operand is a power of 2, make
9575          the MOD operation unsigned since it is simpler and equivalent.  */
9576       if ((code == NE_EXPR || code == EQ_EXPR)
9577           && integer_zerop (arg1)
9578           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
9579           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
9580               || TREE_CODE (arg0) == CEIL_MOD_EXPR
9581               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
9582               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
9583           && integer_pow2p (TREE_OPERAND (arg0, 1)))
9584         {
9585           tree newtype = lang_hooks.types.unsigned_type (TREE_TYPE (arg0));
9586           tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
9587                                      fold_convert (newtype,
9588                                                    TREE_OPERAND (arg0, 0)),
9589                                      fold_convert (newtype,
9590                                                    TREE_OPERAND (arg0, 1)));
9591
9592           return fold_build2 (code, type, newmod,
9593                               fold_convert (newtype, arg1));
9594         }
9595
9596       /* If this is an NE comparison of zero with an AND of one, remove the
9597          comparison since the AND will give the correct value.  */
9598       if (code == NE_EXPR && integer_zerop (arg1)
9599           && TREE_CODE (arg0) == BIT_AND_EXPR
9600           && integer_onep (TREE_OPERAND (arg0, 1)))
9601         return fold_convert (type, arg0);
9602
9603       /* If we have (A & C) == C where C is a power of 2, convert this into
9604          (A & C) != 0.  Similarly for NE_EXPR.  */
9605       if ((code == EQ_EXPR || code == NE_EXPR)
9606           && TREE_CODE (arg0) == BIT_AND_EXPR
9607           && integer_pow2p (TREE_OPERAND (arg0, 1))
9608           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
9609         return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
9610                             arg0, fold_convert (TREE_TYPE (arg0),
9611                                                 integer_zero_node));
9612
9613       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
9614          bit, then fold the expression into A < 0 or A >= 0.  */
9615       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
9616       if (tem)
9617         return tem;
9618
9619       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
9620          Similarly for NE_EXPR.  */
9621       if ((code == EQ_EXPR || code == NE_EXPR)
9622           && TREE_CODE (arg0) == BIT_AND_EXPR
9623           && TREE_CODE (arg1) == INTEGER_CST
9624           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9625         {
9626           tree notc = fold_build1 (BIT_NOT_EXPR,
9627                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
9628                                    TREE_OPERAND (arg0, 1));
9629           tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9630                                        arg1, notc);
9631           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
9632           if (integer_nonzerop (dandnotc))
9633             return omit_one_operand (type, rslt, arg0);
9634         }
9635
9636       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
9637          Similarly for NE_EXPR.  */
9638       if ((code == EQ_EXPR || code == NE_EXPR)
9639           && TREE_CODE (arg0) == BIT_IOR_EXPR
9640           && TREE_CODE (arg1) == INTEGER_CST
9641           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9642         {
9643           tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
9644           tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9645                                        TREE_OPERAND (arg0, 1), notd);
9646           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
9647           if (integer_nonzerop (candnotd))
9648             return omit_one_operand (type, rslt, arg0);
9649         }
9650
9651       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
9652          and similarly for >= into !=.  */
9653       if ((code == LT_EXPR || code == GE_EXPR)
9654           && TYPE_UNSIGNED (TREE_TYPE (arg0))
9655           && TREE_CODE (arg1) == LSHIFT_EXPR
9656           && integer_onep (TREE_OPERAND (arg1, 0)))
9657         return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
9658                        build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
9659                                TREE_OPERAND (arg1, 1)),
9660                        fold_convert (TREE_TYPE (arg0), integer_zero_node));
9661
9662       else if ((code == LT_EXPR || code == GE_EXPR)
9663                && TYPE_UNSIGNED (TREE_TYPE (arg0))
9664                && (TREE_CODE (arg1) == NOP_EXPR
9665                    || TREE_CODE (arg1) == CONVERT_EXPR)
9666                && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
9667                && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
9668         return
9669           build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
9670                   fold_convert (TREE_TYPE (arg0),
9671                                 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
9672                                         TREE_OPERAND (TREE_OPERAND (arg1, 0),
9673                                                       1))),
9674                   fold_convert (TREE_TYPE (arg0), integer_zero_node));
9675
9676       /* Simplify comparison of something with itself.  (For IEEE
9677          floating-point, we can only do some of these simplifications.)  */
9678       if (operand_equal_p (arg0, arg1, 0))
9679         {
9680           switch (code)
9681             {
9682             case EQ_EXPR:
9683               if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9684                   || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9685                 return constant_boolean_node (1, type);
9686               break;
9687
9688             case GE_EXPR:
9689             case LE_EXPR:
9690               if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9691                   || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9692                 return constant_boolean_node (1, type);
9693               return fold_build2 (EQ_EXPR, type, arg0, arg1);
9694
9695             case NE_EXPR:
9696               /* For NE, we can only do this simplification if integer
9697                  or we don't honor IEEE floating point NaNs.  */
9698               if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9699                   && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9700                 break;
9701               /* ... fall through ...  */
9702             case GT_EXPR:
9703             case LT_EXPR:
9704               return constant_boolean_node (0, type);
9705             default:
9706               gcc_unreachable ();
9707             }
9708         }
9709
9710       /* If we are comparing an expression that just has comparisons
9711          of two integer values, arithmetic expressions of those comparisons,
9712          and constants, we can simplify it.  There are only three cases
9713          to check: the two values can either be equal, the first can be
9714          greater, or the second can be greater.  Fold the expression for
9715          those three values.  Since each value must be 0 or 1, we have
9716          eight possibilities, each of which corresponds to the constant 0
9717          or 1 or one of the six possible comparisons.
9718
9719          This handles common cases like (a > b) == 0 but also handles
9720          expressions like  ((x > y) - (y > x)) > 0, which supposedly
9721          occur in macroized code.  */
9722
9723       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9724         {
9725           tree cval1 = 0, cval2 = 0;
9726           int save_p = 0;
9727
9728           if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9729               /* Don't handle degenerate cases here; they should already
9730                  have been handled anyway.  */
9731               && cval1 != 0 && cval2 != 0
9732               && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9733               && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9734               && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9735               && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9736               && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9737               && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9738                                     TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9739             {
9740               tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9741               tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9742
9743               /* We can't just pass T to eval_subst in case cval1 or cval2
9744                  was the same as ARG1.  */
9745
9746               tree high_result
9747                 = fold_build2 (code, type,
9748                                eval_subst (arg0, cval1, maxval,
9749                                            cval2, minval),
9750                                arg1);
9751               tree equal_result
9752                 = fold_build2 (code, type,
9753                                eval_subst (arg0, cval1, maxval,
9754                                            cval2, maxval),
9755                                arg1);
9756               tree low_result
9757                 = fold_build2 (code, type,
9758                                eval_subst (arg0, cval1, minval,
9759                                            cval2, maxval),
9760                                arg1);
9761
9762               /* All three of these results should be 0 or 1.  Confirm they
9763                  are.  Then use those values to select the proper code
9764                  to use.  */
9765
9766               if ((integer_zerop (high_result)
9767                    || integer_onep (high_result))
9768                   && (integer_zerop (equal_result)
9769                       || integer_onep (equal_result))
9770                   && (integer_zerop (low_result)
9771                       || integer_onep (low_result)))
9772                 {
9773                   /* Make a 3-bit mask with the high-order bit being the
9774                      value for `>', the next for '=', and the low for '<'.  */
9775                   switch ((integer_onep (high_result) * 4)
9776                           + (integer_onep (equal_result) * 2)
9777                           + integer_onep (low_result))
9778                     {
9779                     case 0:
9780                       /* Always false.  */
9781                       return omit_one_operand (type, integer_zero_node, arg0);
9782                     case 1:
9783                       code = LT_EXPR;
9784                       break;
9785                     case 2:
9786                       code = EQ_EXPR;
9787                       break;
9788                     case 3:
9789                       code = LE_EXPR;
9790                       break;
9791                     case 4:
9792                       code = GT_EXPR;
9793                       break;
9794                     case 5:
9795                       code = NE_EXPR;
9796                       break;
9797                     case 6:
9798                       code = GE_EXPR;
9799                       break;
9800                     case 7:
9801                       /* Always true.  */
9802                       return omit_one_operand (type, integer_one_node, arg0);
9803                     }
9804
9805                   if (save_p)
9806                     return save_expr (build2 (code, type, cval1, cval2));
9807                   else
9808                     return fold_build2 (code, type, cval1, cval2);
9809                 }
9810             }
9811         }
9812
9813       /* If this is a comparison of a field, we may be able to simplify it.  */
9814       if (((TREE_CODE (arg0) == COMPONENT_REF
9815             && lang_hooks.can_use_bit_fields_p ())
9816            || TREE_CODE (arg0) == BIT_FIELD_REF)
9817           && (code == EQ_EXPR || code == NE_EXPR)
9818           /* Handle the constant case even without -O
9819              to make sure the warnings are given.  */
9820           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
9821         {
9822           t1 = optimize_bit_field_compare (code, type, arg0, arg1);
9823           if (t1)
9824             return t1;
9825         }
9826
9827       /* Fold a comparison of the address of COMPONENT_REFs with the same
9828          type and component to a comparison of the address of the base
9829          object.  In short, &x->a OP &y->a to x OP y and
9830          &x->a OP &y.a to x OP &y  */
9831       if (TREE_CODE (arg0) == ADDR_EXPR
9832           && TREE_CODE (TREE_OPERAND (arg0, 0)) == COMPONENT_REF
9833           && TREE_CODE (arg1) == ADDR_EXPR
9834           && TREE_CODE (TREE_OPERAND (arg1, 0)) == COMPONENT_REF)
9835         {
9836           tree cref0 = TREE_OPERAND (arg0, 0);
9837           tree cref1 = TREE_OPERAND (arg1, 0);
9838           if (TREE_OPERAND (cref0, 1) == TREE_OPERAND (cref1, 1))
9839             {
9840               tree op0 = TREE_OPERAND (cref0, 0);
9841               tree op1 = TREE_OPERAND (cref1, 0);
9842               return fold_build2 (code, type,
9843                                   build_fold_addr_expr (op0),
9844                                   build_fold_addr_expr (op1));
9845             }
9846         }
9847
9848       /* If this is a comparison of complex values and either or both sides
9849          are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
9850          comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
9851          This may prevent needless evaluations.  */
9852       if ((code == EQ_EXPR || code == NE_EXPR)
9853           && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
9854           && (TREE_CODE (arg0) == COMPLEX_EXPR
9855               || TREE_CODE (arg1) == COMPLEX_EXPR
9856               || TREE_CODE (arg0) == COMPLEX_CST
9857               || TREE_CODE (arg1) == COMPLEX_CST))
9858         {
9859           tree subtype = TREE_TYPE (TREE_TYPE (arg0));
9860           tree real0, imag0, real1, imag1;
9861
9862           arg0 = save_expr (arg0);
9863           arg1 = save_expr (arg1);
9864           real0 = fold_build1 (REALPART_EXPR, subtype, arg0);
9865           imag0 = fold_build1 (IMAGPART_EXPR, subtype, arg0);
9866           real1 = fold_build1 (REALPART_EXPR, subtype, arg1);
9867           imag1 = fold_build1 (IMAGPART_EXPR, subtype, arg1);
9868
9869           return fold_build2 ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
9870                                : TRUTH_ORIF_EXPR),
9871                               type,
9872                               fold_build2 (code, type, real0, real1),
9873                               fold_build2 (code, type, imag0, imag1));
9874         }
9875
9876       /* Optimize comparisons of strlen vs zero to a compare of the
9877          first character of the string vs zero.  To wit,
9878                 strlen(ptr) == 0   =>  *ptr == 0
9879                 strlen(ptr) != 0   =>  *ptr != 0
9880          Other cases should reduce to one of these two (or a constant)
9881          due to the return value of strlen being unsigned.  */
9882       if ((code == EQ_EXPR || code == NE_EXPR)
9883           && integer_zerop (arg1)
9884           && TREE_CODE (arg0) == CALL_EXPR)
9885         {
9886           tree fndecl = get_callee_fndecl (arg0);
9887           tree arglist;
9888
9889           if (fndecl
9890               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
9891               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
9892               && (arglist = TREE_OPERAND (arg0, 1))
9893               && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE
9894               && ! TREE_CHAIN (arglist))
9895             return fold_build2 (code, type,
9896                                 build1 (INDIRECT_REF, char_type_node,
9897                                         TREE_VALUE (arglist)),
9898                                 fold_convert (char_type_node,
9899                                               integer_zero_node));
9900         }
9901
9902       /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9903          into a single range test.  */
9904       if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9905            || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9906           && TREE_CODE (arg1) == INTEGER_CST
9907           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9908           && !integer_zerop (TREE_OPERAND (arg0, 1))
9909           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9910           && !TREE_OVERFLOW (arg1))
9911         {
9912           t1 = fold_div_compare (code, type, arg0, arg1);
9913           if (t1 != NULL_TREE)
9914             return t1;
9915         }
9916
9917       if ((code == EQ_EXPR || code == NE_EXPR)
9918           && !TREE_SIDE_EFFECTS (arg0)
9919           && integer_zerop (arg1)
9920           && tree_expr_nonzero_p (arg0))
9921         return constant_boolean_node (code==NE_EXPR, type);
9922
9923       t1 = fold_relational_const (code, type, arg0, arg1);
9924       return t1 == NULL_TREE ? NULL_TREE : t1;
9925
9926     case UNORDERED_EXPR:
9927     case ORDERED_EXPR:
9928     case UNLT_EXPR:
9929     case UNLE_EXPR:
9930     case UNGT_EXPR:
9931     case UNGE_EXPR:
9932     case UNEQ_EXPR:
9933     case LTGT_EXPR:
9934       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9935         {
9936           t1 = fold_relational_const (code, type, arg0, arg1);
9937           if (t1 != NULL_TREE)
9938             return t1;
9939         }
9940
9941       /* If the first operand is NaN, the result is constant.  */
9942       if (TREE_CODE (arg0) == REAL_CST
9943           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
9944           && (code != LTGT_EXPR || ! flag_trapping_math))
9945         {
9946           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
9947                ? integer_zero_node
9948                : integer_one_node;
9949           return omit_one_operand (type, t1, arg1);
9950         }
9951
9952       /* If the second operand is NaN, the result is constant.  */
9953       if (TREE_CODE (arg1) == REAL_CST
9954           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
9955           && (code != LTGT_EXPR || ! flag_trapping_math))
9956         {
9957           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
9958                ? integer_zero_node
9959                : integer_one_node;
9960           return omit_one_operand (type, t1, arg0);
9961         }
9962
9963       /* Simplify unordered comparison of something with itself.  */
9964       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
9965           && operand_equal_p (arg0, arg1, 0))
9966         return constant_boolean_node (1, type);
9967
9968       if (code == LTGT_EXPR
9969           && !flag_trapping_math
9970           && operand_equal_p (arg0, arg1, 0))
9971         return constant_boolean_node (0, type);
9972
9973       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
9974       {
9975         tree targ0 = strip_float_extensions (arg0);
9976         tree targ1 = strip_float_extensions (arg1);
9977         tree newtype = TREE_TYPE (targ0);
9978
9979         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9980           newtype = TREE_TYPE (targ1);
9981
9982         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9983           return fold_build2 (code, type, fold_convert (newtype, targ0),
9984                               fold_convert (newtype, targ1));
9985       }
9986
9987       return NULL_TREE;
9988
9989     case COMPOUND_EXPR:
9990       /* When pedantic, a compound expression can be neither an lvalue
9991          nor an integer constant expression.  */
9992       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
9993         return NULL_TREE;
9994       /* Don't let (0, 0) be null pointer constant.  */
9995       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
9996                                  : fold_convert (type, arg1);
9997       return pedantic_non_lvalue (tem);
9998
9999     case COMPLEX_EXPR:
10000       if (wins)
10001         return build_complex (type, arg0, arg1);
10002       return NULL_TREE;
10003
10004     case ASSERT_EXPR:
10005       /* An ASSERT_EXPR should never be passed to fold_binary.  */
10006       gcc_unreachable ();
10007
10008     default:
10009       return NULL_TREE;
10010     } /* switch (code) */
10011 }
10012
10013 /* Callback for walk_tree, looking for LABEL_EXPR.
10014    Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
10015    Do not check the sub-tree of GOTO_EXPR.  */
10016
10017 static tree
10018 contains_label_1 (tree *tp,
10019                   int *walk_subtrees,
10020                   void *data ATTRIBUTE_UNUSED)
10021 {
10022   switch (TREE_CODE (*tp))
10023     {
10024     case LABEL_EXPR:
10025       return *tp;
10026     case GOTO_EXPR:
10027       *walk_subtrees = 0;
10028     /* no break */
10029     default:
10030       return NULL_TREE;
10031     }
10032 }
10033
10034 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
10035    accessible from outside the sub-tree. Returns NULL_TREE if no
10036    addressable label is found.  */
10037
10038 static bool
10039 contains_label_p (tree st)
10040 {
10041   return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
10042 }
10043
10044 /* Fold a ternary expression of code CODE and type TYPE with operands
10045    OP0, OP1, and OP2.  Return the folded expression if folding is
10046    successful.  Otherwise, return NULL_TREE.  */
10047
10048 tree
10049 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
10050 {
10051   tree tem;
10052   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
10053   enum tree_code_class kind = TREE_CODE_CLASS (code);
10054
10055   gcc_assert (IS_EXPR_CODE_CLASS (kind)
10056               && TREE_CODE_LENGTH (code) == 3);
10057
10058   /* Strip any conversions that don't change the mode.  This is safe
10059      for every expression, except for a comparison expression because
10060      its signedness is derived from its operands.  So, in the latter
10061      case, only strip conversions that don't change the signedness.
10062
10063      Note that this is done as an internal manipulation within the
10064      constant folder, in order to find the simplest representation of
10065      the arguments so that their form can be studied.  In any cases,
10066      the appropriate type conversions should be put back in the tree
10067      that will get out of the constant folder.  */
10068   if (op0)
10069     {
10070       arg0 = op0;
10071       STRIP_NOPS (arg0);
10072     }
10073
10074   if (op1)
10075     {
10076       arg1 = op1;
10077       STRIP_NOPS (arg1);
10078     }
10079
10080   switch (code)
10081     {
10082     case COMPONENT_REF:
10083       if (TREE_CODE (arg0) == CONSTRUCTOR
10084           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
10085         {
10086           tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
10087           if (m)
10088             return TREE_VALUE (m);
10089         }
10090       return NULL_TREE;
10091
10092     case COND_EXPR:
10093       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
10094          so all simple results must be passed through pedantic_non_lvalue.  */
10095       if (TREE_CODE (arg0) == INTEGER_CST)
10096         {
10097           tree unused_op = integer_zerop (arg0) ? op1 : op2;
10098           tem = integer_zerop (arg0) ? op2 : op1;
10099           /* Only optimize constant conditions when the selected branch
10100              has the same type as the COND_EXPR.  This avoids optimizing
10101              away "c ? x : throw", where the throw has a void type.
10102              Avoid throwing away that operand which contains label.  */
10103           if ((!TREE_SIDE_EFFECTS (unused_op)
10104                || !contains_label_p (unused_op))
10105               && (! VOID_TYPE_P (TREE_TYPE (tem))
10106                   || VOID_TYPE_P (type)))
10107             return pedantic_non_lvalue (tem);
10108           return NULL_TREE;
10109         }
10110       if (operand_equal_p (arg1, op2, 0))
10111         return pedantic_omit_one_operand (type, arg1, arg0);
10112
10113       /* If we have A op B ? A : C, we may be able to convert this to a
10114          simpler expression, depending on the operation and the values
10115          of B and C.  Signed zeros prevent all of these transformations,
10116          for reasons given above each one.
10117
10118          Also try swapping the arguments and inverting the conditional.  */
10119       if (COMPARISON_CLASS_P (arg0)
10120           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
10121                                              arg1, TREE_OPERAND (arg0, 1))
10122           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
10123         {
10124           tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
10125           if (tem)
10126             return tem;
10127         }
10128
10129       if (COMPARISON_CLASS_P (arg0)
10130           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
10131                                              op2,
10132                                              TREE_OPERAND (arg0, 1))
10133           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
10134         {
10135           tem = invert_truthvalue (arg0);
10136           if (COMPARISON_CLASS_P (tem))
10137             {
10138               tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
10139               if (tem)
10140                 return tem;
10141             }
10142         }
10143
10144       /* If the second operand is simpler than the third, swap them
10145          since that produces better jump optimization results.  */
10146       if (tree_swap_operands_p (op1, op2, false))
10147         {
10148           /* See if this can be inverted.  If it can't, possibly because
10149              it was a floating-point inequality comparison, don't do
10150              anything.  */
10151           tem = invert_truthvalue (arg0);
10152
10153           if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
10154             return fold_build3 (code, type, tem, op2, op1);
10155         }
10156
10157       /* Convert A ? 1 : 0 to simply A.  */
10158       if (integer_onep (op1)
10159           && integer_zerop (op2)
10160           /* If we try to convert OP0 to our type, the
10161              call to fold will try to move the conversion inside
10162              a COND, which will recurse.  In that case, the COND_EXPR
10163              is probably the best choice, so leave it alone.  */
10164           && type == TREE_TYPE (arg0))
10165         return pedantic_non_lvalue (arg0);
10166
10167       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
10168          over COND_EXPR in cases such as floating point comparisons.  */
10169       if (integer_zerop (op1)
10170           && integer_onep (op2)
10171           && truth_value_p (TREE_CODE (arg0)))
10172         return pedantic_non_lvalue (fold_convert (type,
10173                                                   invert_truthvalue (arg0)));
10174
10175       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
10176       if (TREE_CODE (arg0) == LT_EXPR
10177           && integer_zerop (TREE_OPERAND (arg0, 1))
10178           && integer_zerop (op2)
10179           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
10180         return fold_convert (type, fold_build2 (BIT_AND_EXPR,
10181                                                 TREE_TYPE (tem), tem, arg1));
10182
10183       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
10184          already handled above.  */
10185       if (TREE_CODE (arg0) == BIT_AND_EXPR
10186           && integer_onep (TREE_OPERAND (arg0, 1))
10187           && integer_zerop (op2)
10188           && integer_pow2p (arg1))
10189         {
10190           tree tem = TREE_OPERAND (arg0, 0);
10191           STRIP_NOPS (tem);
10192           if (TREE_CODE (tem) == RSHIFT_EXPR
10193               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
10194               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
10195                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
10196             return fold_build2 (BIT_AND_EXPR, type,
10197                                 TREE_OPERAND (tem, 0), arg1);
10198         }
10199
10200       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
10201          is probably obsolete because the first operand should be a
10202          truth value (that's why we have the two cases above), but let's
10203          leave it in until we can confirm this for all front-ends.  */
10204       if (integer_zerop (op2)
10205           && TREE_CODE (arg0) == NE_EXPR
10206           && integer_zerop (TREE_OPERAND (arg0, 1))
10207           && integer_pow2p (arg1)
10208           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10209           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10210                               arg1, OEP_ONLY_CONST))
10211         return pedantic_non_lvalue (fold_convert (type,
10212                                                   TREE_OPERAND (arg0, 0)));
10213
10214       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
10215       if (integer_zerop (op2)
10216           && truth_value_p (TREE_CODE (arg0))
10217           && truth_value_p (TREE_CODE (arg1)))
10218         return fold_build2 (TRUTH_ANDIF_EXPR, type, arg0, arg1);
10219
10220       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
10221       if (integer_onep (op2)
10222           && truth_value_p (TREE_CODE (arg0))
10223           && truth_value_p (TREE_CODE (arg1)))
10224         {
10225           /* Only perform transformation if ARG0 is easily inverted.  */
10226           tem = invert_truthvalue (arg0);
10227           if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
10228             return fold_build2 (TRUTH_ORIF_EXPR, type, tem, arg1);
10229         }
10230
10231       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
10232       if (integer_zerop (arg1)
10233           && truth_value_p (TREE_CODE (arg0))
10234           && truth_value_p (TREE_CODE (op2)))
10235         {
10236           /* Only perform transformation if ARG0 is easily inverted.  */
10237           tem = invert_truthvalue (arg0);
10238           if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
10239             return fold_build2 (TRUTH_ANDIF_EXPR, type, tem, op2);
10240         }
10241
10242       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
10243       if (integer_onep (arg1)
10244           && truth_value_p (TREE_CODE (arg0))
10245           && truth_value_p (TREE_CODE (op2)))
10246         return fold_build2 (TRUTH_ORIF_EXPR, type, arg0, op2);
10247
10248       return NULL_TREE;
10249
10250     case CALL_EXPR:
10251       /* Check for a built-in function.  */
10252       if (TREE_CODE (op0) == ADDR_EXPR
10253           && TREE_CODE (TREE_OPERAND (op0, 0)) == FUNCTION_DECL
10254           && DECL_BUILT_IN (TREE_OPERAND (op0, 0)))
10255         {
10256           tree fndecl = TREE_OPERAND (op0, 0);
10257           tree arglist = op1;
10258           tree tmp = fold_builtin (fndecl, arglist, false);
10259           if (tmp)
10260             return tmp;
10261         }
10262       return NULL_TREE;
10263
10264     default:
10265       return NULL_TREE;
10266     } /* switch (code) */
10267 }
10268
10269 /* Perform constant folding and related simplification of EXPR.
10270    The related simplifications include x*1 => x, x*0 => 0, etc.,
10271    and application of the associative law.
10272    NOP_EXPR conversions may be removed freely (as long as we
10273    are careful not to change the type of the overall expression).
10274    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
10275    but we can constant-fold them if they have constant operands.  */
10276
10277 #ifdef ENABLE_FOLD_CHECKING
10278 # define fold(x) fold_1 (x)
10279 static tree fold_1 (tree);
10280 static
10281 #endif
10282 tree
10283 fold (tree expr)
10284 {
10285   const tree t = expr;
10286   enum tree_code code = TREE_CODE (t);
10287   enum tree_code_class kind = TREE_CODE_CLASS (code);
10288   tree tem;
10289
10290   /* Return right away if a constant.  */
10291   if (kind == tcc_constant)
10292     return t;
10293
10294   if (IS_EXPR_CODE_CLASS (kind))
10295     {
10296       tree type = TREE_TYPE (t);
10297       tree op0, op1, op2;
10298
10299       switch (TREE_CODE_LENGTH (code))
10300         {
10301         case 1:
10302           op0 = TREE_OPERAND (t, 0);
10303           tem = fold_unary (code, type, op0);
10304           return tem ? tem : expr;
10305         case 2:
10306           op0 = TREE_OPERAND (t, 0);
10307           op1 = TREE_OPERAND (t, 1);
10308           tem = fold_binary (code, type, op0, op1);
10309           return tem ? tem : expr;
10310         case 3:
10311           op0 = TREE_OPERAND (t, 0);
10312           op1 = TREE_OPERAND (t, 1);
10313           op2 = TREE_OPERAND (t, 2);
10314           tem = fold_ternary (code, type, op0, op1, op2);
10315           return tem ? tem : expr;
10316         default:
10317           break;
10318         }
10319     }
10320
10321   switch (code)
10322     {
10323     case CONST_DECL:
10324       return fold (DECL_INITIAL (t));
10325
10326     default:
10327       return t;
10328     } /* switch (code) */
10329 }
10330
10331 #ifdef ENABLE_FOLD_CHECKING
10332 #undef fold
10333
10334 static void fold_checksum_tree (tree, struct md5_ctx *, htab_t);
10335 static void fold_check_failed (tree, tree);
10336 void print_fold_checksum (tree);
10337
10338 /* When --enable-checking=fold, compute a digest of expr before
10339    and after actual fold call to see if fold did not accidentally
10340    change original expr.  */
10341
10342 tree
10343 fold (tree expr)
10344 {
10345   tree ret;
10346   struct md5_ctx ctx;
10347   unsigned char checksum_before[16], checksum_after[16];
10348   htab_t ht;
10349
10350   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
10351   md5_init_ctx (&ctx);
10352   fold_checksum_tree (expr, &ctx, ht);
10353   md5_finish_ctx (&ctx, checksum_before);
10354   htab_empty (ht);
10355
10356   ret = fold_1 (expr);
10357
10358   md5_init_ctx (&ctx);
10359   fold_checksum_tree (expr, &ctx, ht);
10360   md5_finish_ctx (&ctx, checksum_after);
10361   htab_delete (ht);
10362
10363   if (memcmp (checksum_before, checksum_after, 16))
10364     fold_check_failed (expr, ret);
10365
10366   return ret;
10367 }
10368
10369 void
10370 print_fold_checksum (tree expr)
10371 {
10372   struct md5_ctx ctx;
10373   unsigned char checksum[16], cnt;
10374   htab_t ht;
10375
10376   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
10377   md5_init_ctx (&ctx);
10378   fold_checksum_tree (expr, &ctx, ht);
10379   md5_finish_ctx (&ctx, checksum);
10380   htab_delete (ht);
10381   for (cnt = 0; cnt < 16; ++cnt)
10382     fprintf (stderr, "%02x", checksum[cnt]);
10383   putc ('\n', stderr);
10384 }
10385
10386 static void
10387 fold_check_failed (tree expr ATTRIBUTE_UNUSED, tree ret ATTRIBUTE_UNUSED)
10388 {
10389   internal_error ("fold check: original tree changed by fold");
10390 }
10391
10392 static void
10393 fold_checksum_tree (tree expr, struct md5_ctx *ctx, htab_t ht)
10394 {
10395   void **slot;
10396   enum tree_code code;
10397   char buf[sizeof (struct tree_decl)];
10398   int i, len;
10399
10400   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
10401                <= sizeof (struct tree_decl))
10402               && sizeof (struct tree_type) <= sizeof (struct tree_decl));
10403   if (expr == NULL)
10404     return;
10405   slot = htab_find_slot (ht, expr, INSERT);
10406   if (*slot != NULL)
10407     return;
10408   *slot = expr;
10409   code = TREE_CODE (expr);
10410   if (TREE_CODE_CLASS (code) == tcc_declaration
10411       && DECL_ASSEMBLER_NAME_SET_P (expr))
10412     {
10413       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
10414       memcpy (buf, expr, tree_size (expr));
10415       expr = (tree) buf;
10416       SET_DECL_ASSEMBLER_NAME (expr, NULL);
10417     }
10418   else if (TREE_CODE_CLASS (code) == tcc_type
10419            && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)
10420                || TYPE_CACHED_VALUES_P (expr)))
10421     {
10422       /* Allow these fields to be modified.  */
10423       memcpy (buf, expr, tree_size (expr));
10424       expr = (tree) buf;
10425       TYPE_POINTER_TO (expr) = NULL;
10426       TYPE_REFERENCE_TO (expr) = NULL;
10427       if (TYPE_CACHED_VALUES_P (expr))
10428         {
10429           TYPE_CACHED_VALUES_P (expr) = 0;
10430           TYPE_CACHED_VALUES (expr) = NULL;
10431         }
10432     }
10433   md5_process_bytes (expr, tree_size (expr), ctx);
10434   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
10435   if (TREE_CODE_CLASS (code) != tcc_type
10436       && TREE_CODE_CLASS (code) != tcc_declaration)
10437     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
10438   switch (TREE_CODE_CLASS (code))
10439     {
10440     case tcc_constant:
10441       switch (code)
10442         {
10443         case STRING_CST:
10444           md5_process_bytes (TREE_STRING_POINTER (expr),
10445                              TREE_STRING_LENGTH (expr), ctx);
10446           break;
10447         case COMPLEX_CST:
10448           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
10449           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
10450           break;
10451         case VECTOR_CST:
10452           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
10453           break;
10454         default:
10455           break;
10456         }
10457       break;
10458     case tcc_exceptional:
10459       switch (code)
10460         {
10461         case TREE_LIST:
10462           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
10463           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
10464           break;
10465         case TREE_VEC:
10466           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
10467             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
10468           break;
10469         default:
10470           break;
10471         }
10472       break;
10473     case tcc_expression:
10474     case tcc_reference:
10475     case tcc_comparison:
10476     case tcc_unary:
10477     case tcc_binary:
10478     case tcc_statement:
10479       len = TREE_CODE_LENGTH (code);
10480       for (i = 0; i < len; ++i)
10481         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
10482       break;
10483     case tcc_declaration:
10484       fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
10485       fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
10486       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
10487       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
10488       fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
10489       fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
10490       fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
10491       fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
10492       fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
10493       fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
10494       fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
10495       break;
10496     case tcc_type:
10497       if (TREE_CODE (expr) == ENUMERAL_TYPE)
10498         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
10499       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
10500       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
10501       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
10502       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
10503       if (INTEGRAL_TYPE_P (expr)
10504           || SCALAR_FLOAT_TYPE_P (expr))
10505         {
10506           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
10507           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
10508         }
10509       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
10510       if (TREE_CODE (expr) == RECORD_TYPE
10511           || TREE_CODE (expr) == UNION_TYPE
10512           || TREE_CODE (expr) == QUAL_UNION_TYPE)
10513         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
10514       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
10515       break;
10516     default:
10517       break;
10518     }
10519 }
10520
10521 #endif
10522
10523 /* Fold a unary tree expression with code CODE of type TYPE with an
10524    operand OP0.  Return a folded expression if successful.  Otherwise,
10525    return a tree expression with code CODE of type TYPE with an
10526    operand OP0.  */
10527
10528 tree
10529 fold_build1 (enum tree_code code, tree type, tree op0)
10530 {
10531   tree tem = fold_unary (code, type, op0);
10532   if (tem)
10533     return tem;
10534
10535   return build1 (code, type, op0);
10536 }
10537
10538 /* Fold a binary tree expression with code CODE of type TYPE with
10539    operands OP0 and OP1.  Return a folded expression if successful.
10540    Otherwise, return a tree expression with code CODE of type TYPE
10541    with operands OP0 and OP1.  */
10542
10543 tree
10544 fold_build2 (enum tree_code code, tree type, tree op0, tree op1)
10545 {
10546   tree tem = fold_binary (code, type, op0, op1);
10547   if (tem)
10548     return tem;
10549
10550   return build2 (code, type, op0, op1);
10551 }
10552
10553 /* Fold a ternary tree expression with code CODE of type TYPE with
10554    operands OP0, OP1, and OP2.  Return a folded expression if
10555    successful.  Otherwise, return a tree expression with code CODE of
10556    type TYPE with operands OP0, OP1, and OP2.  */
10557
10558 tree
10559 fold_build3 (enum tree_code code, tree type, tree op0, tree op1, tree op2)
10560 {
10561   tree tem = fold_ternary (code, type, op0, op1, op2);
10562   if (tem)
10563     return tem;
10564
10565   return build3 (code, type, op0, op1, op2);
10566 }
10567
10568 /* Perform constant folding and related simplification of initializer
10569    expression EXPR.  This behaves identically to "fold" but ignores
10570    potential run-time traps and exceptions that fold must preserve.  */
10571
10572 tree
10573 fold_initializer (tree expr)
10574 {
10575   int saved_signaling_nans = flag_signaling_nans;
10576   int saved_trapping_math = flag_trapping_math;
10577   int saved_rounding_math = flag_rounding_math;
10578   int saved_trapv = flag_trapv;
10579   tree result;
10580
10581   flag_signaling_nans = 0;
10582   flag_trapping_math = 0;
10583   flag_rounding_math = 0;
10584   flag_trapv = 0;
10585
10586   result = fold (expr);
10587
10588   flag_signaling_nans = saved_signaling_nans;
10589   flag_trapping_math = saved_trapping_math;
10590   flag_rounding_math = saved_rounding_math;
10591   flag_trapv = saved_trapv;
10592
10593   return result;
10594 }
10595
10596 /* Determine if first argument is a multiple of second argument.  Return 0 if
10597    it is not, or we cannot easily determined it to be.
10598
10599    An example of the sort of thing we care about (at this point; this routine
10600    could surely be made more general, and expanded to do what the *_DIV_EXPR's
10601    fold cases do now) is discovering that
10602
10603      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
10604
10605    is a multiple of
10606
10607      SAVE_EXPR (J * 8)
10608
10609    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
10610
10611    This code also handles discovering that
10612
10613      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
10614
10615    is a multiple of 8 so we don't have to worry about dealing with a
10616    possible remainder.
10617
10618    Note that we *look* inside a SAVE_EXPR only to determine how it was
10619    calculated; it is not safe for fold to do much of anything else with the
10620    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
10621    at run time.  For example, the latter example above *cannot* be implemented
10622    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
10623    evaluation time of the original SAVE_EXPR is not necessarily the same at
10624    the time the new expression is evaluated.  The only optimization of this
10625    sort that would be valid is changing
10626
10627      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
10628
10629    divided by 8 to
10630
10631      SAVE_EXPR (I) * SAVE_EXPR (J)
10632
10633    (where the same SAVE_EXPR (J) is used in the original and the
10634    transformed version).  */
10635
10636 static int
10637 multiple_of_p (tree type, tree top, tree bottom)
10638 {
10639   if (operand_equal_p (top, bottom, 0))
10640     return 1;
10641
10642   if (TREE_CODE (type) != INTEGER_TYPE)
10643     return 0;
10644
10645   switch (TREE_CODE (top))
10646     {
10647     case BIT_AND_EXPR:
10648       /* Bitwise and provides a power of two multiple.  If the mask is
10649          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
10650       if (!integer_pow2p (bottom))
10651         return 0;
10652       /* FALLTHRU */
10653
10654     case MULT_EXPR:
10655       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
10656               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
10657
10658     case PLUS_EXPR:
10659     case MINUS_EXPR:
10660       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
10661               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
10662
10663     case LSHIFT_EXPR:
10664       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
10665         {
10666           tree op1, t1;
10667
10668           op1 = TREE_OPERAND (top, 1);
10669           /* const_binop may not detect overflow correctly,
10670              so check for it explicitly here.  */
10671           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
10672               > TREE_INT_CST_LOW (op1)
10673               && TREE_INT_CST_HIGH (op1) == 0
10674               && 0 != (t1 = fold_convert (type,
10675                                           const_binop (LSHIFT_EXPR,
10676                                                        size_one_node,
10677                                                        op1, 0)))
10678               && ! TREE_OVERFLOW (t1))
10679             return multiple_of_p (type, t1, bottom);
10680         }
10681       return 0;
10682
10683     case NOP_EXPR:
10684       /* Can't handle conversions from non-integral or wider integral type.  */
10685       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
10686           || (TYPE_PRECISION (type)
10687               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
10688         return 0;
10689
10690       /* .. fall through ...  */
10691
10692     case SAVE_EXPR:
10693       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
10694
10695     case INTEGER_CST:
10696       if (TREE_CODE (bottom) != INTEGER_CST
10697           || (TYPE_UNSIGNED (type)
10698               && (tree_int_cst_sgn (top) < 0
10699                   || tree_int_cst_sgn (bottom) < 0)))
10700         return 0;
10701       return integer_zerop (const_binop (TRUNC_MOD_EXPR,
10702                                          top, bottom, 0));
10703
10704     default:
10705       return 0;
10706     }
10707 }
10708
10709 /* Return true if `t' is known to be non-negative.  */
10710
10711 int
10712 tree_expr_nonnegative_p (tree t)
10713 {
10714   switch (TREE_CODE (t))
10715     {
10716     case ABS_EXPR:
10717       return 1;
10718
10719     case INTEGER_CST:
10720       return tree_int_cst_sgn (t) >= 0;
10721
10722     case REAL_CST:
10723       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
10724
10725     case PLUS_EXPR:
10726       if (FLOAT_TYPE_P (TREE_TYPE (t)))
10727         return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10728                && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10729
10730       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
10731          both unsigned and at least 2 bits shorter than the result.  */
10732       if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
10733           && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
10734           && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
10735         {
10736           tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
10737           tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
10738           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
10739               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
10740             {
10741               unsigned int prec = MAX (TYPE_PRECISION (inner1),
10742                                        TYPE_PRECISION (inner2)) + 1;
10743               return prec < TYPE_PRECISION (TREE_TYPE (t));
10744             }
10745         }
10746       break;
10747
10748     case MULT_EXPR:
10749       if (FLOAT_TYPE_P (TREE_TYPE (t)))
10750         {
10751           /* x * x for floating point x is always non-negative.  */
10752           if (operand_equal_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1), 0))
10753             return 1;
10754           return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10755                  && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10756         }
10757
10758       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
10759          both unsigned and their total bits is shorter than the result.  */
10760       if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
10761           && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
10762           && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
10763         {
10764           tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
10765           tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
10766           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
10767               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
10768             return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
10769                    < TYPE_PRECISION (TREE_TYPE (t));
10770         }
10771       return 0;
10772
10773     case TRUNC_DIV_EXPR:
10774     case CEIL_DIV_EXPR:
10775     case FLOOR_DIV_EXPR:
10776     case ROUND_DIV_EXPR:
10777       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10778              && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10779
10780     case TRUNC_MOD_EXPR:
10781     case CEIL_MOD_EXPR:
10782     case FLOOR_MOD_EXPR:
10783     case ROUND_MOD_EXPR:
10784       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10785
10786     case RDIV_EXPR:
10787       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10788              && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10789
10790     case BIT_AND_EXPR:
10791       return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
10792              || tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10793     case BIT_IOR_EXPR:
10794     case BIT_XOR_EXPR:
10795       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10796              && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10797
10798     case NOP_EXPR:
10799       {
10800         tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
10801         tree outer_type = TREE_TYPE (t);
10802
10803         if (TREE_CODE (outer_type) == REAL_TYPE)
10804           {
10805             if (TREE_CODE (inner_type) == REAL_TYPE)
10806               return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10807             if (TREE_CODE (inner_type) == INTEGER_TYPE)
10808               {
10809                 if (TYPE_UNSIGNED (inner_type))
10810                   return 1;
10811                 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10812               }
10813           }
10814         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
10815           {
10816             if (TREE_CODE (inner_type) == REAL_TYPE)
10817               return tree_expr_nonnegative_p (TREE_OPERAND (t,0));
10818             if (TREE_CODE (inner_type) == INTEGER_TYPE)
10819               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
10820                       && TYPE_UNSIGNED (inner_type);
10821           }
10822       }
10823       break;
10824
10825     case COND_EXPR:
10826       return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
10827         && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
10828     case COMPOUND_EXPR:
10829       return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10830     case MIN_EXPR:
10831       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10832         && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10833     case MAX_EXPR:
10834       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10835         || tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10836     case MODIFY_EXPR:
10837       return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10838     case BIND_EXPR:
10839       return tree_expr_nonnegative_p (expr_last (TREE_OPERAND (t, 1)));
10840     case SAVE_EXPR:
10841       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10842     case NON_LVALUE_EXPR:
10843       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10844     case FLOAT_EXPR:
10845       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10846
10847     case TARGET_EXPR:
10848       {
10849         tree temp = TARGET_EXPR_SLOT (t);
10850         t = TARGET_EXPR_INITIAL (t);
10851
10852         /* If the initializer is non-void, then it's a normal expression
10853            that will be assigned to the slot.  */
10854         if (!VOID_TYPE_P (t))
10855           return tree_expr_nonnegative_p (t);
10856
10857         /* Otherwise, the initializer sets the slot in some way.  One common
10858            way is an assignment statement at the end of the initializer.  */
10859         while (1)
10860           {
10861             if (TREE_CODE (t) == BIND_EXPR)
10862               t = expr_last (BIND_EXPR_BODY (t));
10863             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
10864                      || TREE_CODE (t) == TRY_CATCH_EXPR)
10865               t = expr_last (TREE_OPERAND (t, 0));
10866             else if (TREE_CODE (t) == STATEMENT_LIST)
10867               t = expr_last (t);
10868             else
10869               break;
10870           }
10871         if (TREE_CODE (t) == MODIFY_EXPR
10872             && TREE_OPERAND (t, 0) == temp)
10873           return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10874
10875         return 0;
10876       }
10877
10878     case CALL_EXPR:
10879       {
10880         tree fndecl = get_callee_fndecl (t);
10881         tree arglist = TREE_OPERAND (t, 1);
10882         if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
10883           switch (DECL_FUNCTION_CODE (fndecl))
10884             {
10885 #define CASE_BUILTIN_F(BUILT_IN_FN) \
10886   case BUILT_IN_FN: case BUILT_IN_FN##F: case BUILT_IN_FN##L:
10887 #define CASE_BUILTIN_I(BUILT_IN_FN) \
10888   case BUILT_IN_FN: case BUILT_IN_FN##L: case BUILT_IN_FN##LL:
10889
10890             CASE_BUILTIN_F (BUILT_IN_ACOS)
10891             CASE_BUILTIN_F (BUILT_IN_ACOSH)
10892             CASE_BUILTIN_F (BUILT_IN_CABS)
10893             CASE_BUILTIN_F (BUILT_IN_COSH)
10894             CASE_BUILTIN_F (BUILT_IN_ERFC)
10895             CASE_BUILTIN_F (BUILT_IN_EXP)
10896             CASE_BUILTIN_F (BUILT_IN_EXP10)
10897             CASE_BUILTIN_F (BUILT_IN_EXP2)
10898             CASE_BUILTIN_F (BUILT_IN_FABS)
10899             CASE_BUILTIN_F (BUILT_IN_FDIM)
10900             CASE_BUILTIN_F (BUILT_IN_FREXP)
10901             CASE_BUILTIN_F (BUILT_IN_HYPOT)
10902             CASE_BUILTIN_F (BUILT_IN_POW10)
10903             CASE_BUILTIN_I (BUILT_IN_FFS)
10904             CASE_BUILTIN_I (BUILT_IN_PARITY)
10905             CASE_BUILTIN_I (BUILT_IN_POPCOUNT)
10906               /* Always true.  */
10907               return 1;
10908
10909             CASE_BUILTIN_F (BUILT_IN_SQRT)
10910               /* sqrt(-0.0) is -0.0.  */
10911               if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (t))))
10912                 return 1;
10913               return tree_expr_nonnegative_p (TREE_VALUE (arglist));
10914
10915             CASE_BUILTIN_F (BUILT_IN_ASINH)
10916             CASE_BUILTIN_F (BUILT_IN_ATAN)
10917             CASE_BUILTIN_F (BUILT_IN_ATANH)
10918             CASE_BUILTIN_F (BUILT_IN_CBRT)
10919             CASE_BUILTIN_F (BUILT_IN_CEIL)
10920             CASE_BUILTIN_F (BUILT_IN_ERF)
10921             CASE_BUILTIN_F (BUILT_IN_EXPM1)
10922             CASE_BUILTIN_F (BUILT_IN_FLOOR)
10923             CASE_BUILTIN_F (BUILT_IN_FMOD)
10924             CASE_BUILTIN_F (BUILT_IN_LCEIL)
10925             CASE_BUILTIN_F (BUILT_IN_LDEXP)
10926             CASE_BUILTIN_F (BUILT_IN_LFLOOR)
10927             CASE_BUILTIN_F (BUILT_IN_LLCEIL)
10928             CASE_BUILTIN_F (BUILT_IN_LLFLOOR)
10929             CASE_BUILTIN_F (BUILT_IN_LLRINT)
10930             CASE_BUILTIN_F (BUILT_IN_LLROUND)
10931             CASE_BUILTIN_F (BUILT_IN_LRINT)
10932             CASE_BUILTIN_F (BUILT_IN_LROUND)
10933             CASE_BUILTIN_F (BUILT_IN_MODF)
10934             CASE_BUILTIN_F (BUILT_IN_NEARBYINT)
10935             CASE_BUILTIN_F (BUILT_IN_POW)
10936             CASE_BUILTIN_F (BUILT_IN_RINT)
10937             CASE_BUILTIN_F (BUILT_IN_ROUND)
10938             CASE_BUILTIN_F (BUILT_IN_SIGNBIT)
10939             CASE_BUILTIN_F (BUILT_IN_SINH)
10940             CASE_BUILTIN_F (BUILT_IN_TANH)
10941             CASE_BUILTIN_F (BUILT_IN_TRUNC)
10942               /* True if the 1st argument is nonnegative.  */
10943               return tree_expr_nonnegative_p (TREE_VALUE (arglist));
10944
10945             CASE_BUILTIN_F (BUILT_IN_FMAX)
10946               /* True if the 1st OR 2nd arguments are nonnegative.  */
10947               return tree_expr_nonnegative_p (TREE_VALUE (arglist))
10948                 || tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
10949
10950             CASE_BUILTIN_F (BUILT_IN_FMIN)
10951               /* True if the 1st AND 2nd arguments are nonnegative.  */
10952               return tree_expr_nonnegative_p (TREE_VALUE (arglist))
10953                 && tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
10954
10955             CASE_BUILTIN_F (BUILT_IN_COPYSIGN)
10956               /* True if the 2nd argument is nonnegative.  */
10957               return tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
10958
10959             default:
10960               break;
10961 #undef CASE_BUILTIN_F
10962 #undef CASE_BUILTIN_I
10963             }
10964       }
10965
10966       /* ... fall through ...  */
10967
10968     default:
10969       if (truth_value_p (TREE_CODE (t)))
10970         /* Truth values evaluate to 0 or 1, which is nonnegative.  */
10971         return 1;
10972     }
10973
10974   /* We don't know sign of `t', so be conservative and return false.  */
10975   return 0;
10976 }
10977
10978 /* Return true when T is an address and is known to be nonzero.
10979    For floating point we further ensure that T is not denormal.
10980    Similar logic is present in nonzero_address in rtlanal.h.  */
10981
10982 static bool
10983 tree_expr_nonzero_p (tree t)
10984 {
10985   tree type = TREE_TYPE (t);
10986
10987   /* Doing something useful for floating point would need more work.  */
10988   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10989     return false;
10990
10991   switch (TREE_CODE (t))
10992     {
10993     case ABS_EXPR:
10994       if (!TYPE_UNSIGNED (type) && !flag_wrapv)
10995         return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
10996
10997     case INTEGER_CST:
10998       /* We used to test for !integer_zerop here.  This does not work correctly
10999          if TREE_CONSTANT_OVERFLOW (t).  */
11000       return (TREE_INT_CST_LOW (t) != 0
11001               || TREE_INT_CST_HIGH (t) != 0);
11002
11003     case PLUS_EXPR:
11004       if (!TYPE_UNSIGNED (type) && !flag_wrapv)
11005         {
11006           /* With the presence of negative values it is hard
11007              to say something.  */
11008           if (!tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
11009               || !tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
11010             return false;
11011           /* One of operands must be positive and the other non-negative.  */
11012           return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
11013                   || tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
11014         }
11015       break;
11016
11017     case MULT_EXPR:
11018       if (!TYPE_UNSIGNED (type) && !flag_wrapv)
11019         {
11020           return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
11021                   && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
11022         }
11023       break;
11024
11025     case NOP_EXPR:
11026       {
11027         tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
11028         tree outer_type = TREE_TYPE (t);
11029
11030         return (TYPE_PRECISION (inner_type) >= TYPE_PRECISION (outer_type)
11031                 && tree_expr_nonzero_p (TREE_OPERAND (t, 0)));
11032       }
11033       break;
11034
11035    case ADDR_EXPR:
11036       {
11037         tree base = get_base_address (TREE_OPERAND (t, 0));
11038
11039         if (!base)
11040           return false;
11041
11042         /* Weak declarations may link to NULL.  */
11043         if (DECL_P (base))
11044           return !DECL_WEAK (base);
11045
11046         /* Constants are never weak.  */
11047         if (CONSTANT_CLASS_P (base))
11048           return true;
11049
11050         return false;
11051       }
11052
11053     case COND_EXPR:
11054       return (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
11055               && tree_expr_nonzero_p (TREE_OPERAND (t, 2)));
11056
11057     case MIN_EXPR:
11058       return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
11059               && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
11060
11061     case MAX_EXPR:
11062       if (tree_expr_nonzero_p (TREE_OPERAND (t, 0)))
11063         {
11064           /* When both operands are nonzero, then MAX must be too.  */
11065           if (tree_expr_nonzero_p (TREE_OPERAND (t, 1)))
11066             return true;
11067
11068           /* MAX where operand 0 is positive is positive.  */
11069           return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
11070         }
11071       /* MAX where operand 1 is positive is positive.  */
11072       else if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
11073                && tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
11074         return true;
11075       break;
11076
11077     case COMPOUND_EXPR:
11078     case MODIFY_EXPR:
11079     case BIND_EXPR:
11080       return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
11081
11082     case SAVE_EXPR:
11083     case NON_LVALUE_EXPR:
11084       return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
11085
11086     case BIT_IOR_EXPR:
11087       return tree_expr_nonzero_p (TREE_OPERAND (t, 1))
11088              || tree_expr_nonzero_p (TREE_OPERAND (t, 0));
11089
11090     default:
11091       break;
11092     }
11093   return false;
11094 }
11095
11096 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
11097    attempt to fold the expression to a constant without modifying TYPE,
11098    OP0 or OP1.
11099
11100    If the expression could be simplified to a constant, then return
11101    the constant.  If the expression would not be simplified to a
11102    constant, then return NULL_TREE.  */
11103
11104 tree
11105 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
11106 {
11107   tree tem = fold_binary (code, type, op0, op1);
11108   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
11109 }
11110
11111 /* Given the components of a unary expression CODE, TYPE and OP0,
11112    attempt to fold the expression to a constant without modifying
11113    TYPE or OP0.
11114
11115    If the expression could be simplified to a constant, then return
11116    the constant.  If the expression would not be simplified to a
11117    constant, then return NULL_TREE.  */
11118
11119 tree
11120 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
11121 {
11122   tree tem = fold_unary (code, type, op0);
11123   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
11124 }
11125
11126 /* If EXP represents referencing an element in a constant string
11127    (either via pointer arithmetic or array indexing), return the
11128    tree representing the value accessed, otherwise return NULL.  */
11129
11130 tree
11131 fold_read_from_constant_string (tree exp)
11132 {
11133   if (TREE_CODE (exp) == INDIRECT_REF || TREE_CODE (exp) == ARRAY_REF)
11134     {
11135       tree exp1 = TREE_OPERAND (exp, 0);
11136       tree index;
11137       tree string;
11138
11139       if (TREE_CODE (exp) == INDIRECT_REF)
11140         string = string_constant (exp1, &index);
11141       else
11142         {
11143           tree low_bound = array_ref_low_bound (exp);
11144           index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
11145
11146           /* Optimize the special-case of a zero lower bound.
11147
11148              We convert the low_bound to sizetype to avoid some problems
11149              with constant folding.  (E.g. suppose the lower bound is 1,
11150              and its mode is QI.  Without the conversion,l (ARRAY
11151              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
11152              +INDEX), which becomes (ARRAY+255+INDEX).  Opps!)  */
11153           if (! integer_zerop (low_bound))
11154             index = size_diffop (index, fold_convert (sizetype, low_bound));
11155
11156           string = exp1;
11157         }
11158
11159       if (string
11160           && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (string))
11161           && TREE_CODE (string) == STRING_CST
11162           && TREE_CODE (index) == INTEGER_CST
11163           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
11164           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
11165               == MODE_INT)
11166           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
11167         return fold_convert (TREE_TYPE (exp),
11168                              build_int_cst (NULL_TREE,
11169                                             (TREE_STRING_POINTER (string)
11170                                              [TREE_INT_CST_LOW (index)])));
11171     }
11172   return NULL;
11173 }
11174
11175 /* Return the tree for neg (ARG0) when ARG0 is known to be either
11176    an integer constant or real constant.
11177
11178    TYPE is the type of the result.  */
11179
11180 static tree
11181 fold_negate_const (tree arg0, tree type)
11182 {
11183   tree t = NULL_TREE;
11184
11185   switch (TREE_CODE (arg0))
11186     {
11187     case INTEGER_CST:
11188       {
11189         unsigned HOST_WIDE_INT low;
11190         HOST_WIDE_INT high;
11191         int overflow = neg_double (TREE_INT_CST_LOW (arg0),
11192                                    TREE_INT_CST_HIGH (arg0),
11193                                    &low, &high);
11194         t = build_int_cst_wide (type, low, high);
11195         t = force_fit_type (t, 1,
11196                             (overflow | TREE_OVERFLOW (arg0))
11197                             && !TYPE_UNSIGNED (type),
11198                             TREE_CONSTANT_OVERFLOW (arg0));
11199         break;
11200       }
11201
11202     case REAL_CST:
11203       t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
11204       break;
11205
11206     default:
11207       gcc_unreachable ();
11208     }
11209
11210   return t;
11211 }
11212
11213 /* Return the tree for abs (ARG0) when ARG0 is known to be either
11214    an integer constant or real constant.
11215
11216    TYPE is the type of the result.  */
11217
11218 tree
11219 fold_abs_const (tree arg0, tree type)
11220 {
11221   tree t = NULL_TREE;
11222
11223   switch (TREE_CODE (arg0))
11224     {
11225     case INTEGER_CST:
11226       /* If the value is unsigned, then the absolute value is
11227          the same as the ordinary value.  */
11228       if (TYPE_UNSIGNED (type))
11229         t = arg0;
11230       /* Similarly, if the value is non-negative.  */
11231       else if (INT_CST_LT (integer_minus_one_node, arg0))
11232         t = arg0;
11233       /* If the value is negative, then the absolute value is
11234          its negation.  */
11235       else
11236         {
11237           unsigned HOST_WIDE_INT low;
11238           HOST_WIDE_INT high;
11239           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
11240                                      TREE_INT_CST_HIGH (arg0),
11241                                      &low, &high);
11242           t = build_int_cst_wide (type, low, high);
11243           t = force_fit_type (t, -1, overflow | TREE_OVERFLOW (arg0),
11244                               TREE_CONSTANT_OVERFLOW (arg0));
11245         }
11246       break;
11247
11248     case REAL_CST:
11249       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
11250         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
11251       else
11252         t =  arg0;
11253       break;
11254
11255     default:
11256       gcc_unreachable ();
11257     }
11258
11259   return t;
11260 }
11261
11262 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
11263    constant.  TYPE is the type of the result.  */
11264
11265 static tree
11266 fold_not_const (tree arg0, tree type)
11267 {
11268   tree t = NULL_TREE;
11269
11270   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
11271
11272   t = build_int_cst_wide (type,
11273                           ~ TREE_INT_CST_LOW (arg0),
11274                           ~ TREE_INT_CST_HIGH (arg0));
11275   t = force_fit_type (t, 0, TREE_OVERFLOW (arg0),
11276                       TREE_CONSTANT_OVERFLOW (arg0));
11277
11278   return t;
11279 }
11280
11281 /* Given CODE, a relational operator, the target type, TYPE and two
11282    constant operands OP0 and OP1, return the result of the
11283    relational operation.  If the result is not a compile time
11284    constant, then return NULL_TREE.  */
11285
11286 static tree
11287 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
11288 {
11289   int result, invert;
11290
11291   /* From here on, the only cases we handle are when the result is
11292      known to be a constant.  */
11293
11294   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
11295     {
11296       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
11297       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
11298
11299       /* Handle the cases where either operand is a NaN.  */
11300       if (real_isnan (c0) || real_isnan (c1))
11301         {
11302           switch (code)
11303             {
11304             case EQ_EXPR:
11305             case ORDERED_EXPR:
11306               result = 0;
11307               break;
11308
11309             case NE_EXPR:
11310             case UNORDERED_EXPR:
11311             case UNLT_EXPR:
11312             case UNLE_EXPR:
11313             case UNGT_EXPR:
11314             case UNGE_EXPR:
11315             case UNEQ_EXPR:
11316               result = 1;
11317               break;
11318
11319             case LT_EXPR:
11320             case LE_EXPR:
11321             case GT_EXPR:
11322             case GE_EXPR:
11323             case LTGT_EXPR:
11324               if (flag_trapping_math)
11325                 return NULL_TREE;
11326               result = 0;
11327               break;
11328
11329             default:
11330               gcc_unreachable ();
11331             }
11332
11333           return constant_boolean_node (result, type);
11334         }
11335
11336       return constant_boolean_node (real_compare (code, c0, c1), type);
11337     }
11338
11339   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
11340
11341      To compute GT, swap the arguments and do LT.
11342      To compute GE, do LT and invert the result.
11343      To compute LE, swap the arguments, do LT and invert the result.
11344      To compute NE, do EQ and invert the result.
11345
11346      Therefore, the code below must handle only EQ and LT.  */
11347
11348   if (code == LE_EXPR || code == GT_EXPR)
11349     {
11350       tree tem = op0;
11351       op0 = op1;
11352       op1 = tem;
11353       code = swap_tree_comparison (code);
11354     }
11355
11356   /* Note that it is safe to invert for real values here because we
11357      have already handled the one case that it matters.  */
11358
11359   invert = 0;
11360   if (code == NE_EXPR || code == GE_EXPR)
11361     {
11362       invert = 1;
11363       code = invert_tree_comparison (code, false);
11364     }
11365
11366   /* Compute a result for LT or EQ if args permit;
11367      Otherwise return T.  */
11368   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
11369     {
11370       if (code == EQ_EXPR)
11371         result = tree_int_cst_equal (op0, op1);
11372       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
11373         result = INT_CST_LT_UNSIGNED (op0, op1);
11374       else
11375         result = INT_CST_LT (op0, op1);
11376     }
11377   else
11378     return NULL_TREE;
11379
11380   if (invert)
11381     result ^= 1;
11382   return constant_boolean_node (result, type);
11383 }
11384
11385 /* Build an expression for the a clean point containing EXPR with type TYPE.
11386    Don't build a cleanup point expression for EXPR which don't have side
11387    effects.  */
11388
11389 tree
11390 fold_build_cleanup_point_expr (tree type, tree expr)
11391 {
11392   /* If the expression does not have side effects then we don't have to wrap
11393      it with a cleanup point expression.  */
11394   if (!TREE_SIDE_EFFECTS (expr))
11395     return expr;
11396
11397   /* If the expression is a return, check to see if the expression inside the
11398      return has no side effects or the right hand side of the modify expression
11399      inside the return. If either don't have side effects set we don't need to
11400      wrap the expression in a cleanup point expression.  Note we don't check the
11401      left hand side of the modify because it should always be a return decl.  */
11402   if (TREE_CODE (expr) == RETURN_EXPR)
11403     {
11404       tree op = TREE_OPERAND (expr, 0);
11405       if (!op || !TREE_SIDE_EFFECTS (op))
11406         return expr;
11407       op = TREE_OPERAND (op, 1);
11408       if (!TREE_SIDE_EFFECTS (op))
11409         return expr;
11410     }
11411   
11412   return build1 (CLEANUP_POINT_EXPR, type, expr);
11413 }
11414
11415 /* Build an expression for the address of T.  Folds away INDIRECT_REF to
11416    avoid confusing the gimplify process.  */
11417
11418 tree
11419 build_fold_addr_expr_with_type (tree t, tree ptrtype)
11420 {
11421   /* The size of the object is not relevant when talking about its address.  */
11422   if (TREE_CODE (t) == WITH_SIZE_EXPR)
11423     t = TREE_OPERAND (t, 0);
11424
11425   /* Note: doesn't apply to ALIGN_INDIRECT_REF */
11426   if (TREE_CODE (t) == INDIRECT_REF
11427       || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
11428     {
11429       t = TREE_OPERAND (t, 0);
11430       if (TREE_TYPE (t) != ptrtype)
11431         t = build1 (NOP_EXPR, ptrtype, t);
11432     }
11433   else
11434     {
11435       tree base = t;
11436
11437       while (handled_component_p (base))
11438         base = TREE_OPERAND (base, 0);
11439       if (DECL_P (base))
11440         TREE_ADDRESSABLE (base) = 1;
11441
11442       t = build1 (ADDR_EXPR, ptrtype, t);
11443     }
11444
11445   return t;
11446 }
11447
11448 tree
11449 build_fold_addr_expr (tree t)
11450 {
11451   return build_fold_addr_expr_with_type (t, build_pointer_type (TREE_TYPE (t)));
11452 }
11453
11454 /* Given a pointer value T, return a simplified version of an indirection
11455    through T, or NULL_TREE if no simplification is possible.  */
11456
11457 static tree
11458 fold_indirect_ref_1 (tree t)
11459 {
11460   tree type = TREE_TYPE (TREE_TYPE (t));
11461   tree sub = t;
11462   tree subtype;
11463
11464   STRIP_NOPS (sub);
11465   subtype = TREE_TYPE (sub);
11466   if (!POINTER_TYPE_P (subtype))
11467     return NULL_TREE;
11468
11469   if (TREE_CODE (sub) == ADDR_EXPR)
11470     {
11471       tree op = TREE_OPERAND (sub, 0);
11472       tree optype = TREE_TYPE (op);
11473       /* *&p => p */
11474       if (lang_hooks.types_compatible_p (type, optype))
11475         return op;
11476       /* *(foo *)&fooarray => fooarray[0] */
11477       else if (TREE_CODE (optype) == ARRAY_TYPE
11478                && lang_hooks.types_compatible_p (type, TREE_TYPE (optype)))
11479         {
11480           tree type_domain = TYPE_DOMAIN (optype);
11481           tree min_val = size_zero_node;
11482           if (type_domain && TYPE_MIN_VALUE (type_domain))
11483             min_val = TYPE_MIN_VALUE (type_domain);
11484           return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
11485         }
11486     }
11487
11488   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
11489   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
11490       && lang_hooks.types_compatible_p (type, TREE_TYPE (TREE_TYPE (subtype))))
11491     {
11492       tree type_domain;
11493       tree min_val = size_zero_node;
11494       sub = build_fold_indirect_ref (sub);
11495       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
11496       if (type_domain && TYPE_MIN_VALUE (type_domain))
11497         min_val = TYPE_MIN_VALUE (type_domain);
11498       return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
11499     }
11500
11501   return NULL_TREE;
11502 }
11503
11504 /* Builds an expression for an indirection through T, simplifying some
11505    cases.  */
11506
11507 tree
11508 build_fold_indirect_ref (tree t)
11509 {
11510   tree sub = fold_indirect_ref_1 (t);
11511
11512   if (sub)
11513     return sub;
11514   else
11515     return build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (t)), t);
11516 }
11517
11518 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
11519
11520 tree
11521 fold_indirect_ref (tree t)
11522 {
11523   tree sub = fold_indirect_ref_1 (TREE_OPERAND (t, 0));
11524
11525   if (sub)
11526     return sub;
11527   else
11528     return t;
11529 }
11530
11531 /* Strip non-trapping, non-side-effecting tree nodes from an expression
11532    whose result is ignored.  The type of the returned tree need not be
11533    the same as the original expression.  */
11534
11535 tree
11536 fold_ignored_result (tree t)
11537 {
11538   if (!TREE_SIDE_EFFECTS (t))
11539     return integer_zero_node;
11540
11541   for (;;)
11542     switch (TREE_CODE_CLASS (TREE_CODE (t)))
11543       {
11544       case tcc_unary:
11545         t = TREE_OPERAND (t, 0);
11546         break;
11547
11548       case tcc_binary:
11549       case tcc_comparison:
11550         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
11551           t = TREE_OPERAND (t, 0);
11552         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
11553           t = TREE_OPERAND (t, 1);
11554         else
11555           return t;
11556         break;
11557
11558       case tcc_expression:
11559         switch (TREE_CODE (t))
11560           {
11561           case COMPOUND_EXPR:
11562             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
11563               return t;
11564             t = TREE_OPERAND (t, 0);
11565             break;
11566
11567           case COND_EXPR:
11568             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
11569                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
11570               return t;
11571             t = TREE_OPERAND (t, 0);
11572             break;
11573
11574           default:
11575             return t;
11576           }
11577         break;
11578
11579       default:
11580         return t;
11581       }
11582 }
11583
11584 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
11585    This can only be applied to objects of a sizetype.  */
11586
11587 tree
11588 round_up (tree value, int divisor)
11589 {
11590   tree div = NULL_TREE;
11591
11592   gcc_assert (divisor > 0);
11593   if (divisor == 1)
11594     return value;
11595
11596   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
11597      have to do anything.  Only do this when we are not given a const,
11598      because in that case, this check is more expensive than just
11599      doing it.  */
11600   if (TREE_CODE (value) != INTEGER_CST)
11601     {
11602       div = build_int_cst (TREE_TYPE (value), divisor);
11603
11604       if (multiple_of_p (TREE_TYPE (value), value, div))
11605         return value;
11606     }
11607
11608   /* If divisor is a power of two, simplify this to bit manipulation.  */
11609   if (divisor == (divisor & -divisor))
11610     {
11611       tree t;
11612
11613       t = build_int_cst (TREE_TYPE (value), divisor - 1);
11614       value = size_binop (PLUS_EXPR, value, t);
11615       t = build_int_cst (TREE_TYPE (value), -divisor);
11616       value = size_binop (BIT_AND_EXPR, value, t);
11617     }
11618   else
11619     {
11620       if (!div)
11621         div = build_int_cst (TREE_TYPE (value), divisor);
11622       value = size_binop (CEIL_DIV_EXPR, value, div);
11623       value = size_binop (MULT_EXPR, value, div);
11624     }
11625
11626   return value;
11627 }
11628
11629 /* Likewise, but round down.  */
11630
11631 tree
11632 round_down (tree value, int divisor)
11633 {
11634   tree div = NULL_TREE;
11635
11636   gcc_assert (divisor > 0);
11637   if (divisor == 1)
11638     return value;
11639
11640   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
11641      have to do anything.  Only do this when we are not given a const,
11642      because in that case, this check is more expensive than just
11643      doing it.  */
11644   if (TREE_CODE (value) != INTEGER_CST)
11645     {
11646       div = build_int_cst (TREE_TYPE (value), divisor);
11647
11648       if (multiple_of_p (TREE_TYPE (value), value, div))
11649         return value;
11650     }
11651
11652   /* If divisor is a power of two, simplify this to bit manipulation.  */
11653   if (divisor == (divisor & -divisor))
11654     {
11655       tree t;
11656
11657       t = build_int_cst (TREE_TYPE (value), -divisor);
11658       value = size_binop (BIT_AND_EXPR, value, t);
11659     }
11660   else
11661     {
11662       if (!div)
11663         div = build_int_cst (TREE_TYPE (value), divisor);
11664       value = size_binop (FLOOR_DIV_EXPR, value, div);
11665       value = size_binop (MULT_EXPR, value, div);
11666     }
11667
11668   return value;
11669 }
11670
11671 /* Returns the pointer to the base of the object addressed by EXP and
11672    extracts the information about the offset of the access, storing it
11673    to PBITPOS and POFFSET.  */
11674
11675 static tree
11676 split_address_to_core_and_offset (tree exp,
11677                                   HOST_WIDE_INT *pbitpos, tree *poffset)
11678 {
11679   tree core;
11680   enum machine_mode mode;
11681   int unsignedp, volatilep;
11682   HOST_WIDE_INT bitsize;
11683
11684   if (TREE_CODE (exp) == ADDR_EXPR)
11685     {
11686       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
11687                                   poffset, &mode, &unsignedp, &volatilep,
11688                                   false);
11689
11690       if (TREE_CODE (core) == INDIRECT_REF)
11691         core = TREE_OPERAND (core, 0);
11692     }
11693   else
11694     {
11695       core = exp;
11696       *pbitpos = 0;
11697       *poffset = NULL_TREE;
11698     }
11699
11700   return core;
11701 }
11702
11703 /* Returns true if addresses of E1 and E2 differ by a constant, false
11704    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
11705
11706 bool
11707 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
11708 {
11709   tree core1, core2;
11710   HOST_WIDE_INT bitpos1, bitpos2;
11711   tree toffset1, toffset2, tdiff, type;
11712
11713   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
11714   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
11715
11716   if (bitpos1 % BITS_PER_UNIT != 0
11717       || bitpos2 % BITS_PER_UNIT != 0
11718       || !operand_equal_p (core1, core2, 0))
11719     return false;
11720
11721   if (toffset1 && toffset2)
11722     {
11723       type = TREE_TYPE (toffset1);
11724       if (type != TREE_TYPE (toffset2))
11725         toffset2 = fold_convert (type, toffset2);
11726
11727       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
11728       if (!host_integerp (tdiff, 0))
11729         return false;
11730
11731       *diff = tree_low_cst (tdiff, 0);
11732     }
11733   else if (toffset1 || toffset2)
11734     {
11735       /* If only one of the offsets is non-constant, the difference cannot
11736          be a constant.  */
11737       return false;
11738     }
11739   else
11740     *diff = 0;
11741
11742   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
11743   return true;
11744 }
11745
11746 /* Simplify the floating point expression EXP when the sign of the
11747    result is not significant.  Return NULL_TREE if no simplification
11748    is possible.  */
11749
11750 tree
11751 fold_strip_sign_ops (tree exp)
11752 {
11753   tree arg0, arg1;
11754
11755   switch (TREE_CODE (exp))
11756     {
11757     case ABS_EXPR:
11758     case NEGATE_EXPR:
11759       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
11760       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
11761
11762     case MULT_EXPR:
11763     case RDIV_EXPR:
11764       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
11765         return NULL_TREE;
11766       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
11767       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
11768       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
11769         return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
11770                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
11771                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
11772       break;
11773
11774     default:
11775       break;
11776     }
11777   return NULL_TREE;
11778 }
11779