OSDN Git Service

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