OSDN Git Service

temporarily revert last changes
[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 fold_convert (type,
2940                            build_int_cst (NULL_TREE, integer_zerop (arg)));
2941
2942     case TRUTH_AND_EXPR:
2943       return build2 (TRUTH_OR_EXPR, type,
2944                      invert_truthvalue (TREE_OPERAND (arg, 0)),
2945                      invert_truthvalue (TREE_OPERAND (arg, 1)));
2946
2947     case TRUTH_OR_EXPR:
2948       return build2 (TRUTH_AND_EXPR, type,
2949                      invert_truthvalue (TREE_OPERAND (arg, 0)),
2950                      invert_truthvalue (TREE_OPERAND (arg, 1)));
2951
2952     case TRUTH_XOR_EXPR:
2953       /* Here we can invert either operand.  We invert the first operand
2954          unless the second operand is a TRUTH_NOT_EXPR in which case our
2955          result is the XOR of the first operand with the inside of the
2956          negation of the second operand.  */
2957
2958       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2959         return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2960                        TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2961       else
2962         return build2 (TRUTH_XOR_EXPR, type,
2963                        invert_truthvalue (TREE_OPERAND (arg, 0)),
2964                        TREE_OPERAND (arg, 1));
2965
2966     case TRUTH_ANDIF_EXPR:
2967       return build2 (TRUTH_ORIF_EXPR, type,
2968                      invert_truthvalue (TREE_OPERAND (arg, 0)),
2969                      invert_truthvalue (TREE_OPERAND (arg, 1)));
2970
2971     case TRUTH_ORIF_EXPR:
2972       return build2 (TRUTH_ANDIF_EXPR, type,
2973                      invert_truthvalue (TREE_OPERAND (arg, 0)),
2974                      invert_truthvalue (TREE_OPERAND (arg, 1)));
2975
2976     case TRUTH_NOT_EXPR:
2977       return TREE_OPERAND (arg, 0);
2978
2979     case COND_EXPR:
2980       return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
2981                      invert_truthvalue (TREE_OPERAND (arg, 1)),
2982                      invert_truthvalue (TREE_OPERAND (arg, 2)));
2983
2984     case COMPOUND_EXPR:
2985       return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2986                      invert_truthvalue (TREE_OPERAND (arg, 1)));
2987
2988     case NON_LVALUE_EXPR:
2989       return invert_truthvalue (TREE_OPERAND (arg, 0));
2990
2991     case NOP_EXPR:
2992       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2993         break;
2994
2995     case CONVERT_EXPR:
2996     case FLOAT_EXPR:
2997       return build1 (TREE_CODE (arg), type,
2998                      invert_truthvalue (TREE_OPERAND (arg, 0)));
2999
3000     case BIT_AND_EXPR:
3001       if (!integer_onep (TREE_OPERAND (arg, 1)))
3002         break;
3003       return build2 (EQ_EXPR, type, arg,
3004                      fold_convert (type, integer_zero_node));
3005
3006     case SAVE_EXPR:
3007       return build1 (TRUTH_NOT_EXPR, type, arg);
3008
3009     case CLEANUP_POINT_EXPR:
3010       return build1 (CLEANUP_POINT_EXPR, type,
3011                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3012
3013     default:
3014       break;
3015     }
3016   gcc_assert (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE);
3017   return build1 (TRUTH_NOT_EXPR, type, arg);
3018 }
3019
3020 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3021    operands are another bit-wise operation with a common input.  If so,
3022    distribute the bit operations to save an operation and possibly two if
3023    constants are involved.  For example, convert
3024         (A | B) & (A | C) into A | (B & C)
3025    Further simplification will occur if B and C are constants.
3026
3027    If this optimization cannot be done, 0 will be returned.  */
3028
3029 static tree
3030 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3031 {
3032   tree common;
3033   tree left, right;
3034
3035   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3036       || TREE_CODE (arg0) == code
3037       || (TREE_CODE (arg0) != BIT_AND_EXPR
3038           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3039     return 0;
3040
3041   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3042     {
3043       common = TREE_OPERAND (arg0, 0);
3044       left = TREE_OPERAND (arg0, 1);
3045       right = TREE_OPERAND (arg1, 1);
3046     }
3047   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3048     {
3049       common = TREE_OPERAND (arg0, 0);
3050       left = TREE_OPERAND (arg0, 1);
3051       right = TREE_OPERAND (arg1, 0);
3052     }
3053   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3054     {
3055       common = TREE_OPERAND (arg0, 1);
3056       left = TREE_OPERAND (arg0, 0);
3057       right = TREE_OPERAND (arg1, 1);
3058     }
3059   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3060     {
3061       common = TREE_OPERAND (arg0, 1);
3062       left = TREE_OPERAND (arg0, 0);
3063       right = TREE_OPERAND (arg1, 0);
3064     }
3065   else
3066     return 0;
3067
3068   return fold (build2 (TREE_CODE (arg0), type, common,
3069                        fold (build2 (code, type, left, right))));
3070 }
3071 \f
3072 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3073    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3074
3075 static tree
3076 make_bit_field_ref (tree inner, tree type, int bitsize, int bitpos,
3077                     int unsignedp)
3078 {
3079   tree result;
3080
3081   if (bitpos == 0)
3082     {
3083       tree size = TYPE_SIZE (TREE_TYPE (inner));
3084       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3085            || POINTER_TYPE_P (TREE_TYPE (inner)))
3086           && host_integerp (size, 0) 
3087           && tree_low_cst (size, 0) == bitsize)
3088         return fold_convert (type, inner);
3089     }
3090
3091   result = build3 (BIT_FIELD_REF, type, inner,
3092                    size_int (bitsize), bitsize_int (bitpos));
3093
3094   BIT_FIELD_REF_UNSIGNED (result) = unsignedp;
3095
3096   return result;
3097 }
3098
3099 /* Optimize a bit-field compare.
3100
3101    There are two cases:  First is a compare against a constant and the
3102    second is a comparison of two items where the fields are at the same
3103    bit position relative to the start of a chunk (byte, halfword, word)
3104    large enough to contain it.  In these cases we can avoid the shift
3105    implicit in bitfield extractions.
3106
3107    For constants, we emit a compare of the shifted constant with the
3108    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3109    compared.  For two fields at the same position, we do the ANDs with the
3110    similar mask and compare the result of the ANDs.
3111
3112    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3113    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3114    are the left and right operands of the comparison, respectively.
3115
3116    If the optimization described above can be done, we return the resulting
3117    tree.  Otherwise we return zero.  */
3118
3119 static tree
3120 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3121                             tree lhs, tree rhs)
3122 {
3123   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3124   tree type = TREE_TYPE (lhs);
3125   tree signed_type, unsigned_type;
3126   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3127   enum machine_mode lmode, rmode, nmode;
3128   int lunsignedp, runsignedp;
3129   int lvolatilep = 0, rvolatilep = 0;
3130   tree linner, rinner = NULL_TREE;
3131   tree mask;
3132   tree offset;
3133
3134   /* Get all the information about the extractions being done.  If the bit size
3135      if the same as the size of the underlying object, we aren't doing an
3136      extraction at all and so can do nothing.  We also don't want to
3137      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3138      then will no longer be able to replace it.  */
3139   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3140                                 &lunsignedp, &lvolatilep, false);
3141   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3142       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3143     return 0;
3144
3145  if (!const_p)
3146    {
3147      /* If this is not a constant, we can only do something if bit positions,
3148         sizes, and signedness are the same.  */
3149      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3150                                    &runsignedp, &rvolatilep, false);
3151
3152      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3153          || lunsignedp != runsignedp || offset != 0
3154          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3155        return 0;
3156    }
3157
3158   /* See if we can find a mode to refer to this field.  We should be able to,
3159      but fail if we can't.  */
3160   nmode = get_best_mode (lbitsize, lbitpos,
3161                          const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3162                          : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3163                                 TYPE_ALIGN (TREE_TYPE (rinner))),
3164                          word_mode, lvolatilep || rvolatilep);
3165   if (nmode == VOIDmode)
3166     return 0;
3167
3168   /* Set signed and unsigned types of the precision of this mode for the
3169      shifts below.  */
3170   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3171   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3172
3173   /* Compute the bit position and size for the new reference and our offset
3174      within it. If the new reference is the same size as the original, we
3175      won't optimize anything, so return zero.  */
3176   nbitsize = GET_MODE_BITSIZE (nmode);
3177   nbitpos = lbitpos & ~ (nbitsize - 1);
3178   lbitpos -= nbitpos;
3179   if (nbitsize == lbitsize)
3180     return 0;
3181
3182   if (BYTES_BIG_ENDIAN)
3183     lbitpos = nbitsize - lbitsize - lbitpos;
3184
3185   /* Make the mask to be used against the extracted field.  */
3186   mask = build_int_cst (unsigned_type, -1);
3187   mask = force_fit_type (mask, 0, false, false);
3188   mask = fold_convert (unsigned_type, mask);
3189   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3190   mask = const_binop (RSHIFT_EXPR, mask,
3191                       size_int (nbitsize - lbitsize - lbitpos), 0);
3192
3193   if (! const_p)
3194     /* If not comparing with constant, just rework the comparison
3195        and return.  */
3196     return build2 (code, compare_type,
3197                    build2 (BIT_AND_EXPR, unsigned_type,
3198                            make_bit_field_ref (linner, unsigned_type,
3199                                                nbitsize, nbitpos, 1),
3200                            mask),
3201                    build2 (BIT_AND_EXPR, unsigned_type,
3202                            make_bit_field_ref (rinner, unsigned_type,
3203                                                nbitsize, nbitpos, 1),
3204                            mask));
3205
3206   /* Otherwise, we are handling the constant case. See if the constant is too
3207      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
3208      this not only for its own sake, but to avoid having to test for this
3209      error case below.  If we didn't, we might generate wrong code.
3210
3211      For unsigned fields, the constant shifted right by the field length should
3212      be all zero.  For signed fields, the high-order bits should agree with
3213      the sign bit.  */
3214
3215   if (lunsignedp)
3216     {
3217       if (! integer_zerop (const_binop (RSHIFT_EXPR,
3218                                         fold_convert (unsigned_type, rhs),
3219                                         size_int (lbitsize), 0)))
3220         {
3221           warning ("comparison is always %d due to width of bit-field",
3222                    code == NE_EXPR);
3223           return constant_boolean_node (code == NE_EXPR, compare_type);
3224         }
3225     }
3226   else
3227     {
3228       tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
3229                               size_int (lbitsize - 1), 0);
3230       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3231         {
3232           warning ("comparison is always %d due to width of bit-field",
3233                    code == NE_EXPR);
3234           return constant_boolean_node (code == NE_EXPR, compare_type);
3235         }
3236     }
3237
3238   /* Single-bit compares should always be against zero.  */
3239   if (lbitsize == 1 && ! integer_zerop (rhs))
3240     {
3241       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3242       rhs = fold_convert (type, integer_zero_node);
3243     }
3244
3245   /* Make a new bitfield reference, shift the constant over the
3246      appropriate number of bits and mask it with the computed mask
3247      (in case this was a signed field).  If we changed it, make a new one.  */
3248   lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
3249   if (lvolatilep)
3250     {
3251       TREE_SIDE_EFFECTS (lhs) = 1;
3252       TREE_THIS_VOLATILE (lhs) = 1;
3253     }
3254
3255   rhs = fold (const_binop (BIT_AND_EXPR,
3256                            const_binop (LSHIFT_EXPR,
3257                                         fold_convert (unsigned_type, rhs),
3258                                         size_int (lbitpos), 0),
3259                            mask, 0));
3260
3261   return build2 (code, compare_type,
3262                  build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
3263                  rhs);
3264 }
3265 \f
3266 /* Subroutine for fold_truthop: decode a field reference.
3267
3268    If EXP is a comparison reference, we return the innermost reference.
3269
3270    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3271    set to the starting bit number.
3272
3273    If the innermost field can be completely contained in a mode-sized
3274    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
3275
3276    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3277    otherwise it is not changed.
3278
3279    *PUNSIGNEDP is set to the signedness of the field.
3280
3281    *PMASK is set to the mask used.  This is either contained in a
3282    BIT_AND_EXPR or derived from the width of the field.
3283
3284    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3285
3286    Return 0 if this is not a component reference or is one that we can't
3287    do anything with.  */
3288
3289 static tree
3290 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
3291                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3292                         int *punsignedp, int *pvolatilep,
3293                         tree *pmask, tree *pand_mask)
3294 {
3295   tree outer_type = 0;
3296   tree and_mask = 0;
3297   tree mask, inner, offset;
3298   tree unsigned_type;
3299   unsigned int precision;
3300
3301   /* All the optimizations using this function assume integer fields.
3302      There are problems with FP fields since the type_for_size call
3303      below can fail for, e.g., XFmode.  */
3304   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3305     return 0;
3306
3307   /* We are interested in the bare arrangement of bits, so strip everything
3308      that doesn't affect the machine mode.  However, record the type of the
3309      outermost expression if it may matter below.  */
3310   if (TREE_CODE (exp) == NOP_EXPR
3311       || TREE_CODE (exp) == CONVERT_EXPR
3312       || TREE_CODE (exp) == NON_LVALUE_EXPR)
3313     outer_type = TREE_TYPE (exp);
3314   STRIP_NOPS (exp);
3315
3316   if (TREE_CODE (exp) == BIT_AND_EXPR)
3317     {
3318       and_mask = TREE_OPERAND (exp, 1);
3319       exp = TREE_OPERAND (exp, 0);
3320       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3321       if (TREE_CODE (and_mask) != INTEGER_CST)
3322         return 0;
3323     }
3324
3325   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3326                                punsignedp, pvolatilep, false);
3327   if ((inner == exp && and_mask == 0)
3328       || *pbitsize < 0 || offset != 0
3329       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3330     return 0;
3331
3332   /* If the number of bits in the reference is the same as the bitsize of
3333      the outer type, then the outer type gives the signedness. Otherwise
3334      (in case of a small bitfield) the signedness is unchanged.  */
3335   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3336     *punsignedp = TYPE_UNSIGNED (outer_type);
3337
3338   /* Compute the mask to access the bitfield.  */
3339   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3340   precision = TYPE_PRECISION (unsigned_type);
3341
3342   mask = build_int_cst (unsigned_type, -1);
3343   mask = force_fit_type (mask, 0, false, false);
3344
3345   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3346   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3347
3348   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
3349   if (and_mask != 0)
3350     mask = fold (build2 (BIT_AND_EXPR, unsigned_type,
3351                          fold_convert (unsigned_type, and_mask), mask));
3352
3353   *pmask = mask;
3354   *pand_mask = and_mask;
3355   return inner;
3356 }
3357
3358 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3359    bit positions.  */
3360
3361 static int
3362 all_ones_mask_p (tree mask, int size)
3363 {
3364   tree type = TREE_TYPE (mask);
3365   unsigned int precision = TYPE_PRECISION (type);
3366   tree tmask;
3367
3368   tmask = build_int_cst (lang_hooks.types.signed_type (type), -1);
3369   tmask = force_fit_type (tmask, 0, false, false);
3370
3371   return
3372     tree_int_cst_equal (mask,
3373                         const_binop (RSHIFT_EXPR,
3374                                      const_binop (LSHIFT_EXPR, tmask,
3375                                                   size_int (precision - size),
3376                                                   0),
3377                                      size_int (precision - size), 0));
3378 }
3379
3380 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3381    represents the sign bit of EXP's type.  If EXP represents a sign
3382    or zero extension, also test VAL against the unextended type.
3383    The return value is the (sub)expression whose sign bit is VAL,
3384    or NULL_TREE otherwise.  */
3385
3386 static tree
3387 sign_bit_p (tree exp, tree val)
3388 {
3389   unsigned HOST_WIDE_INT mask_lo, lo;
3390   HOST_WIDE_INT mask_hi, hi;
3391   int width;
3392   tree t;
3393
3394   /* Tree EXP must have an integral type.  */
3395   t = TREE_TYPE (exp);
3396   if (! INTEGRAL_TYPE_P (t))
3397     return NULL_TREE;
3398
3399   /* Tree VAL must be an integer constant.  */
3400   if (TREE_CODE (val) != INTEGER_CST
3401       || TREE_CONSTANT_OVERFLOW (val))
3402     return NULL_TREE;
3403
3404   width = TYPE_PRECISION (t);
3405   if (width > HOST_BITS_PER_WIDE_INT)
3406     {
3407       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3408       lo = 0;
3409
3410       mask_hi = ((unsigned HOST_WIDE_INT) -1
3411                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
3412       mask_lo = -1;
3413     }
3414   else
3415     {
3416       hi = 0;
3417       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3418
3419       mask_hi = 0;
3420       mask_lo = ((unsigned HOST_WIDE_INT) -1
3421                  >> (HOST_BITS_PER_WIDE_INT - width));
3422     }
3423
3424   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3425      treat VAL as if it were unsigned.  */
3426   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3427       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3428     return exp;
3429
3430   /* Handle extension from a narrower type.  */
3431   if (TREE_CODE (exp) == NOP_EXPR
3432       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3433     return sign_bit_p (TREE_OPERAND (exp, 0), val);
3434
3435   return NULL_TREE;
3436 }
3437
3438 /* Subroutine for fold_truthop: determine if an operand is simple enough
3439    to be evaluated unconditionally.  */
3440
3441 static int
3442 simple_operand_p (tree exp)
3443 {
3444   /* Strip any conversions that don't change the machine mode.  */
3445   STRIP_NOPS (exp);
3446
3447   return (CONSTANT_CLASS_P (exp)
3448           || TREE_CODE (exp) == SSA_NAME
3449           || (DECL_P (exp)
3450               && ! TREE_ADDRESSABLE (exp)
3451               && ! TREE_THIS_VOLATILE (exp)
3452               && ! DECL_NONLOCAL (exp)
3453               /* Don't regard global variables as simple.  They may be
3454                  allocated in ways unknown to the compiler (shared memory,
3455                  #pragma weak, etc).  */
3456               && ! TREE_PUBLIC (exp)
3457               && ! DECL_EXTERNAL (exp)
3458               /* Loading a static variable is unduly expensive, but global
3459                  registers aren't expensive.  */
3460               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3461 }
3462 \f
3463 /* The following functions are subroutines to fold_range_test and allow it to
3464    try to change a logical combination of comparisons into a range test.
3465
3466    For example, both
3467         X == 2 || X == 3 || X == 4 || X == 5
3468    and
3469         X >= 2 && X <= 5
3470    are converted to
3471         (unsigned) (X - 2) <= 3
3472
3473    We describe each set of comparisons as being either inside or outside
3474    a range, using a variable named like IN_P, and then describe the
3475    range with a lower and upper bound.  If one of the bounds is omitted,
3476    it represents either the highest or lowest value of the type.
3477
3478    In the comments below, we represent a range by two numbers in brackets
3479    preceded by a "+" to designate being inside that range, or a "-" to
3480    designate being outside that range, so the condition can be inverted by
3481    flipping the prefix.  An omitted bound is represented by a "-".  For
3482    example, "- [-, 10]" means being outside the range starting at the lowest
3483    possible value and ending at 10, in other words, being greater than 10.
3484    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3485    always false.
3486
3487    We set up things so that the missing bounds are handled in a consistent
3488    manner so neither a missing bound nor "true" and "false" need to be
3489    handled using a special case.  */
3490
3491 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3492    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3493    and UPPER1_P are nonzero if the respective argument is an upper bound
3494    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
3495    must be specified for a comparison.  ARG1 will be converted to ARG0's
3496    type if both are specified.  */
3497
3498 static tree
3499 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3500              tree arg1, int upper1_p)
3501 {
3502   tree tem;
3503   int result;
3504   int sgn0, sgn1;
3505
3506   /* If neither arg represents infinity, do the normal operation.
3507      Else, if not a comparison, return infinity.  Else handle the special
3508      comparison rules. Note that most of the cases below won't occur, but
3509      are handled for consistency.  */
3510
3511   if (arg0 != 0 && arg1 != 0)
3512     {
3513       tem = fold (build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3514                           arg0, fold_convert (TREE_TYPE (arg0), arg1)));
3515       STRIP_NOPS (tem);
3516       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3517     }
3518
3519   if (TREE_CODE_CLASS (code) != tcc_comparison)
3520     return 0;
3521
3522   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3523      for neither.  In real maths, we cannot assume open ended ranges are
3524      the same. But, this is computer arithmetic, where numbers are finite.
3525      We can therefore make the transformation of any unbounded range with
3526      the value Z, Z being greater than any representable number. This permits
3527      us to treat unbounded ranges as equal.  */
3528   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3529   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3530   switch (code)
3531     {
3532     case EQ_EXPR:
3533       result = sgn0 == sgn1;
3534       break;
3535     case NE_EXPR:
3536       result = sgn0 != sgn1;
3537       break;
3538     case LT_EXPR:
3539       result = sgn0 < sgn1;
3540       break;
3541     case LE_EXPR:
3542       result = sgn0 <= sgn1;
3543       break;
3544     case GT_EXPR:
3545       result = sgn0 > sgn1;
3546       break;
3547     case GE_EXPR:
3548       result = sgn0 >= sgn1;
3549       break;
3550     default:
3551       gcc_unreachable ();
3552     }
3553
3554   return constant_boolean_node (result, type);
3555 }
3556 \f
3557 /* Given EXP, a logical expression, set the range it is testing into
3558    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
3559    actually being tested.  *PLOW and *PHIGH will be made of the same type
3560    as the returned expression.  If EXP is not a comparison, we will most
3561    likely not be returning a useful value and range.  */
3562
3563 static tree
3564 make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
3565 {
3566   enum tree_code code;
3567   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
3568   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
3569   int in_p, n_in_p;
3570   tree low, high, n_low, n_high;
3571
3572   /* Start with simply saying "EXP != 0" and then look at the code of EXP
3573      and see if we can refine the range.  Some of the cases below may not
3574      happen, but it doesn't seem worth worrying about this.  We "continue"
3575      the outer loop when we've changed something; otherwise we "break"
3576      the switch, which will "break" the while.  */
3577
3578   in_p = 0;
3579   low = high = fold_convert (TREE_TYPE (exp), integer_zero_node);
3580
3581   while (1)
3582     {
3583       code = TREE_CODE (exp);
3584       exp_type = TREE_TYPE (exp);
3585
3586       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3587         {
3588           if (TREE_CODE_LENGTH (code) > 0)
3589             arg0 = TREE_OPERAND (exp, 0);
3590           if (TREE_CODE_CLASS (code) == tcc_comparison
3591               || TREE_CODE_CLASS (code) == tcc_unary
3592               || TREE_CODE_CLASS (code) == tcc_binary)
3593             arg0_type = TREE_TYPE (arg0);
3594           if (TREE_CODE_CLASS (code) == tcc_binary
3595               || TREE_CODE_CLASS (code) == tcc_comparison
3596               || (TREE_CODE_CLASS (code) == tcc_expression
3597                   && TREE_CODE_LENGTH (code) > 1))
3598             arg1 = TREE_OPERAND (exp, 1);
3599         }
3600
3601       switch (code)
3602         {
3603         case TRUTH_NOT_EXPR:
3604           in_p = ! in_p, exp = arg0;
3605           continue;
3606
3607         case EQ_EXPR: case NE_EXPR:
3608         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3609           /* We can only do something if the range is testing for zero
3610              and if the second operand is an integer constant.  Note that
3611              saying something is "in" the range we make is done by
3612              complementing IN_P since it will set in the initial case of
3613              being not equal to zero; "out" is leaving it alone.  */
3614           if (low == 0 || high == 0
3615               || ! integer_zerop (low) || ! integer_zerop (high)
3616               || TREE_CODE (arg1) != INTEGER_CST)
3617             break;
3618
3619           switch (code)
3620             {
3621             case NE_EXPR:  /* - [c, c]  */
3622               low = high = arg1;
3623               break;
3624             case EQ_EXPR:  /* + [c, c]  */
3625               in_p = ! in_p, low = high = arg1;
3626               break;
3627             case GT_EXPR:  /* - [-, c] */
3628               low = 0, high = arg1;
3629               break;
3630             case GE_EXPR:  /* + [c, -] */
3631               in_p = ! in_p, low = arg1, high = 0;
3632               break;
3633             case LT_EXPR:  /* - [c, -] */
3634               low = arg1, high = 0;
3635               break;
3636             case LE_EXPR:  /* + [-, c] */
3637               in_p = ! in_p, low = 0, high = arg1;
3638               break;
3639             default:
3640               gcc_unreachable ();
3641             }
3642
3643           /* If this is an unsigned comparison, we also know that EXP is
3644              greater than or equal to zero.  We base the range tests we make
3645              on that fact, so we record it here so we can parse existing
3646              range tests.  We test arg0_type since often the return type
3647              of, e.g. EQ_EXPR, is boolean.  */
3648           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3649             {
3650               if (! merge_ranges (&n_in_p, &n_low, &n_high,
3651                                   in_p, low, high, 1,
3652                                   fold_convert (arg0_type, integer_zero_node),
3653                                   NULL_TREE))
3654                 break;
3655
3656               in_p = n_in_p, low = n_low, high = n_high;
3657
3658               /* If the high bound is missing, but we have a nonzero low
3659                  bound, reverse the range so it goes from zero to the low bound
3660                  minus 1.  */
3661               if (high == 0 && low && ! integer_zerop (low))
3662                 {
3663                   in_p = ! in_p;
3664                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3665                                       integer_one_node, 0);
3666                   low = fold_convert (arg0_type, integer_zero_node);
3667                 }
3668             }
3669
3670           exp = arg0;
3671           continue;
3672
3673         case NEGATE_EXPR:
3674           /* (-x) IN [a,b] -> x in [-b, -a]  */
3675           n_low = range_binop (MINUS_EXPR, exp_type,
3676                                fold_convert (exp_type, integer_zero_node),
3677                                0, high, 1);
3678           n_high = range_binop (MINUS_EXPR, exp_type,
3679                                 fold_convert (exp_type, integer_zero_node),
3680                                 0, low, 0);
3681           low = n_low, high = n_high;
3682           exp = arg0;
3683           continue;
3684
3685         case BIT_NOT_EXPR:
3686           /* ~ X -> -X - 1  */
3687           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
3688                         fold_convert (exp_type, integer_one_node));
3689           continue;
3690
3691         case PLUS_EXPR:  case MINUS_EXPR:
3692           if (TREE_CODE (arg1) != INTEGER_CST)
3693             break;
3694
3695           /* If EXP is signed, any overflow in the computation is undefined,
3696              so we don't worry about it so long as our computations on
3697              the bounds don't overflow.  For unsigned, overflow is defined
3698              and this is exactly the right thing.  */
3699           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3700                                arg0_type, low, 0, arg1, 0);
3701           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3702                                 arg0_type, high, 1, arg1, 0);
3703           if ((n_low != 0 && TREE_OVERFLOW (n_low))
3704               || (n_high != 0 && TREE_OVERFLOW (n_high)))
3705             break;
3706
3707           /* Check for an unsigned range which has wrapped around the maximum
3708              value thus making n_high < n_low, and normalize it.  */
3709           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3710             {
3711               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
3712                                  integer_one_node, 0);
3713               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
3714                                   integer_one_node, 0);
3715
3716               /* If the range is of the form +/- [ x+1, x ], we won't
3717                  be able to normalize it.  But then, it represents the
3718                  whole range or the empty set, so make it
3719                  +/- [ -, - ].  */
3720               if (tree_int_cst_equal (n_low, low)
3721                   && tree_int_cst_equal (n_high, high))
3722                 low = high = 0;
3723               else
3724                 in_p = ! in_p;
3725             }
3726           else
3727             low = n_low, high = n_high;
3728
3729           exp = arg0;
3730           continue;
3731
3732         case NOP_EXPR:  case NON_LVALUE_EXPR:  case CONVERT_EXPR:
3733           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
3734             break;
3735
3736           if (! INTEGRAL_TYPE_P (arg0_type)
3737               || (low != 0 && ! int_fits_type_p (low, arg0_type))
3738               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
3739             break;
3740
3741           n_low = low, n_high = high;
3742
3743           if (n_low != 0)
3744             n_low = fold_convert (arg0_type, n_low);
3745
3746           if (n_high != 0)
3747             n_high = fold_convert (arg0_type, n_high);
3748
3749
3750           /* If we're converting arg0 from an unsigned type, to exp,
3751              a signed type,  we will be doing the comparison as unsigned.
3752              The tests above have already verified that LOW and HIGH
3753              are both positive.
3754
3755              So we have to ensure that we will handle large unsigned
3756              values the same way that the current signed bounds treat
3757              negative values.  */
3758
3759           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
3760             {
3761               tree high_positive;
3762               tree equiv_type = lang_hooks.types.type_for_mode
3763                 (TYPE_MODE (arg0_type), 1);
3764
3765               /* A range without an upper bound is, naturally, unbounded.
3766                  Since convert would have cropped a very large value, use
3767                  the max value for the destination type.  */
3768               high_positive
3769                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
3770                 : TYPE_MAX_VALUE (arg0_type);
3771
3772               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
3773                 high_positive = fold (build2 (RSHIFT_EXPR, arg0_type,
3774                                               fold_convert (arg0_type,
3775                                                             high_positive),
3776                                               fold_convert (arg0_type,
3777                                                             integer_one_node)));
3778
3779               /* If the low bound is specified, "and" the range with the
3780                  range for which the original unsigned value will be
3781                  positive.  */
3782               if (low != 0)
3783                 {
3784                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
3785                                       1, n_low, n_high, 1,
3786                                       fold_convert (arg0_type,
3787                                                     integer_zero_node),
3788                                       high_positive))
3789                     break;
3790
3791                   in_p = (n_in_p == in_p);
3792                 }
3793               else
3794                 {
3795                   /* Otherwise, "or" the range with the range of the input
3796                      that will be interpreted as negative.  */
3797                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
3798                                       0, n_low, n_high, 1,
3799                                       fold_convert (arg0_type,
3800                                                     integer_zero_node),
3801                                       high_positive))
3802                     break;
3803
3804                   in_p = (in_p != n_in_p);
3805                 }
3806             }
3807
3808           exp = arg0;
3809           low = n_low, high = n_high;
3810           continue;
3811
3812         default:
3813           break;
3814         }
3815
3816       break;
3817     }
3818
3819   /* If EXP is a constant, we can evaluate whether this is true or false.  */
3820   if (TREE_CODE (exp) == INTEGER_CST)
3821     {
3822       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3823                                                  exp, 0, low, 0))
3824                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
3825                                                     exp, 1, high, 1)));
3826       low = high = 0;
3827       exp = 0;
3828     }
3829
3830   *pin_p = in_p, *plow = low, *phigh = high;
3831   return exp;
3832 }
3833 \f
3834 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3835    type, TYPE, return an expression to test if EXP is in (or out of, depending
3836    on IN_P) the range.  Return 0 if the test couldn't be created.  */
3837
3838 static tree
3839 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
3840 {
3841   tree etype = TREE_TYPE (exp);
3842   tree value;
3843
3844   if (! in_p)
3845     {
3846       value = build_range_check (type, exp, 1, low, high);
3847       if (value != 0)
3848         return invert_truthvalue (value);
3849
3850       return 0;
3851     }
3852
3853   if (low == 0 && high == 0)
3854     return fold_convert (type, integer_one_node);
3855
3856   if (low == 0)
3857     return fold (build2 (LE_EXPR, type, exp, high));
3858
3859   if (high == 0)
3860     return fold (build2 (GE_EXPR, type, exp, low));
3861
3862   if (operand_equal_p (low, high, 0))
3863     return fold (build2 (EQ_EXPR, type, exp, low));
3864
3865   if (integer_zerop (low))
3866     {
3867       if (! TYPE_UNSIGNED (etype))
3868         {
3869           etype = lang_hooks.types.unsigned_type (etype);
3870           high = fold_convert (etype, high);
3871           exp = fold_convert (etype, exp);
3872         }
3873       return build_range_check (type, exp, 1, 0, high);
3874     }
3875
3876   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
3877   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
3878     {
3879       unsigned HOST_WIDE_INT lo;
3880       HOST_WIDE_INT hi;
3881       int prec;
3882
3883       prec = TYPE_PRECISION (etype);
3884       if (prec <= HOST_BITS_PER_WIDE_INT)
3885         {
3886           hi = 0;
3887           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
3888         }
3889       else
3890         {
3891           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
3892           lo = (unsigned HOST_WIDE_INT) -1;
3893         }
3894
3895       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
3896         {
3897           if (TYPE_UNSIGNED (etype))
3898             {
3899               etype = lang_hooks.types.signed_type (etype);
3900               exp = fold_convert (etype, exp);
3901             }
3902           return fold (build2 (GT_EXPR, type, exp,
3903                                fold_convert (etype, integer_zero_node)));
3904         }
3905     }
3906
3907   value = const_binop (MINUS_EXPR, high, low, 0);
3908   if (value != 0 && TREE_OVERFLOW (value) && ! TYPE_UNSIGNED (etype))
3909     {
3910       tree utype, minv, maxv;
3911
3912       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
3913          for the type in question, as we rely on this here.  */
3914       switch (TREE_CODE (etype))
3915         {
3916         case INTEGER_TYPE:
3917         case ENUMERAL_TYPE:
3918         case CHAR_TYPE:
3919           utype = lang_hooks.types.unsigned_type (etype);
3920           maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
3921           maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
3922                               integer_one_node, 1);
3923           minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
3924           if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
3925                                           minv, 1, maxv, 1)))
3926             {
3927               etype = utype;
3928               high = fold_convert (etype, high);
3929               low = fold_convert (etype, low);
3930               exp = fold_convert (etype, exp);
3931               value = const_binop (MINUS_EXPR, high, low, 0);
3932             }
3933           break;
3934         default:
3935           break;
3936         }
3937     }
3938
3939   if (value != 0 && ! TREE_OVERFLOW (value))
3940     return build_range_check (type,
3941                               fold (build2 (MINUS_EXPR, etype, exp, low)),
3942                               1, fold_convert (etype, integer_zero_node),
3943                               value);
3944
3945   return 0;
3946 }
3947 \f
3948 /* Given two ranges, see if we can merge them into one.  Return 1 if we
3949    can, 0 if we can't.  Set the output range into the specified parameters.  */
3950
3951 static int
3952 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
3953               tree high0, int in1_p, tree low1, tree high1)
3954 {
3955   int no_overlap;
3956   int subset;
3957   int temp;
3958   tree tem;
3959   int in_p;
3960   tree low, high;
3961   int lowequal = ((low0 == 0 && low1 == 0)
3962                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3963                                                 low0, 0, low1, 0)));
3964   int highequal = ((high0 == 0 && high1 == 0)
3965                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3966                                                  high0, 1, high1, 1)));
3967
3968   /* Make range 0 be the range that starts first, or ends last if they
3969      start at the same value.  Swap them if it isn't.  */
3970   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
3971                                  low0, 0, low1, 0))
3972       || (lowequal
3973           && integer_onep (range_binop (GT_EXPR, integer_type_node,
3974                                         high1, 1, high0, 1))))
3975     {
3976       temp = in0_p, in0_p = in1_p, in1_p = temp;
3977       tem = low0, low0 = low1, low1 = tem;
3978       tem = high0, high0 = high1, high1 = tem;
3979     }
3980
3981   /* Now flag two cases, whether the ranges are disjoint or whether the
3982      second range is totally subsumed in the first.  Note that the tests
3983      below are simplified by the ones above.  */
3984   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
3985                                           high0, 1, low1, 0));
3986   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
3987                                       high1, 1, high0, 1));
3988
3989   /* We now have four cases, depending on whether we are including or
3990      excluding the two ranges.  */
3991   if (in0_p && in1_p)
3992     {
3993       /* If they don't overlap, the result is false.  If the second range
3994          is a subset it is the result.  Otherwise, the range is from the start
3995          of the second to the end of the first.  */
3996       if (no_overlap)
3997         in_p = 0, low = high = 0;
3998       else if (subset)
3999         in_p = 1, low = low1, high = high1;
4000       else
4001         in_p = 1, low = low1, high = high0;
4002     }
4003
4004   else if (in0_p && ! in1_p)
4005     {
4006       /* If they don't overlap, the result is the first range.  If they are
4007          equal, the result is false.  If the second range is a subset of the
4008          first, and the ranges begin at the same place, we go from just after
4009          the end of the first range to the end of the second.  If the second
4010          range is not a subset of the first, or if it is a subset and both
4011          ranges end at the same place, the range starts at the start of the
4012          first range and ends just before the second range.
4013          Otherwise, we can't describe this as a single range.  */
4014       if (no_overlap)
4015         in_p = 1, low = low0, high = high0;
4016       else if (lowequal && highequal)
4017         in_p = 0, low = high = 0;
4018       else if (subset && lowequal)
4019         {
4020           in_p = 1, high = high0;
4021           low = range_binop (PLUS_EXPR, NULL_TREE, high1, 0,
4022                              integer_one_node, 0);
4023         }
4024       else if (! subset || highequal)
4025         {
4026           in_p = 1, low = low0;
4027           high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
4028                               integer_one_node, 0);
4029         }
4030       else
4031         return 0;
4032     }
4033
4034   else if (! in0_p && in1_p)
4035     {
4036       /* If they don't overlap, the result is the second range.  If the second
4037          is a subset of the first, the result is false.  Otherwise,
4038          the range starts just after the first range and ends at the
4039          end of the second.  */
4040       if (no_overlap)
4041         in_p = 1, low = low1, high = high1;
4042       else if (subset || highequal)
4043         in_p = 0, low = high = 0;
4044       else
4045         {
4046           in_p = 1, high = high1;
4047           low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
4048                              integer_one_node, 0);
4049         }
4050     }
4051
4052   else
4053     {
4054       /* The case where we are excluding both ranges.  Here the complex case
4055          is if they don't overlap.  In that case, the only time we have a
4056          range is if they are adjacent.  If the second is a subset of the
4057          first, the result is the first.  Otherwise, the range to exclude
4058          starts at the beginning of the first range and ends at the end of the
4059          second.  */
4060       if (no_overlap)
4061         {
4062           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4063                                          range_binop (PLUS_EXPR, NULL_TREE,
4064                                                       high0, 1,
4065                                                       integer_one_node, 1),
4066                                          1, low1, 0)))
4067             in_p = 0, low = low0, high = high1;
4068           else
4069             {
4070               /* Canonicalize - [min, x] into - [-, x].  */
4071               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4072                 switch (TREE_CODE (TREE_TYPE (low0)))
4073                   {
4074                   case ENUMERAL_TYPE:
4075                     if (TYPE_PRECISION (TREE_TYPE (low0))
4076                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4077                       break;
4078                     /* FALLTHROUGH */
4079                   case INTEGER_TYPE:
4080                   case CHAR_TYPE:
4081                     if (tree_int_cst_equal (low0,
4082                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4083                       low0 = 0;
4084                     break;
4085                   case POINTER_TYPE:
4086                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4087                         && integer_zerop (low0))
4088                       low0 = 0;
4089                     break;
4090                   default:
4091                     break;
4092                   }
4093
4094               /* Canonicalize - [x, max] into - [x, -].  */
4095               if (high1 && TREE_CODE (high1) == INTEGER_CST)
4096                 switch (TREE_CODE (TREE_TYPE (high1)))
4097                   {
4098                   case ENUMERAL_TYPE:
4099                     if (TYPE_PRECISION (TREE_TYPE (high1))
4100                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4101                       break;
4102                     /* FALLTHROUGH */
4103                   case INTEGER_TYPE:
4104                   case CHAR_TYPE:
4105                     if (tree_int_cst_equal (high1,
4106                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
4107                       high1 = 0;
4108                     break;
4109                   case POINTER_TYPE:
4110                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
4111                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4112                                                        high1, 1,
4113                                                        integer_one_node, 1)))
4114                       high1 = 0;
4115                     break;
4116                   default:
4117                     break;
4118                   }
4119
4120               /* The ranges might be also adjacent between the maximum and
4121                  minimum values of the given type.  For
4122                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4123                  return + [x + 1, y - 1].  */
4124               if (low0 == 0 && high1 == 0)
4125                 {
4126                   low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
4127                                      integer_one_node, 1);
4128                   high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
4129                                       integer_one_node, 0);
4130                   if (low == 0 || high == 0)
4131                     return 0;
4132
4133                   in_p = 1;
4134                 }
4135               else
4136                 return 0;
4137             }
4138         }
4139       else if (subset)
4140         in_p = 0, low = low0, high = high0;
4141       else
4142         in_p = 0, low = low0, high = high1;
4143     }
4144
4145   *pin_p = in_p, *plow = low, *phigh = high;
4146   return 1;
4147 }
4148 \f
4149
4150 /* Subroutine of fold, looking inside expressions of the form
4151    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4152    of the COND_EXPR.  This function is being used also to optimize
4153    A op B ? C : A, by reversing the comparison first.
4154
4155    Return a folded expression whose code is not a COND_EXPR
4156    anymore, or NULL_TREE if no folding opportunity is found.  */
4157
4158 static tree
4159 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
4160 {
4161   enum tree_code comp_code = TREE_CODE (arg0);
4162   tree arg00 = TREE_OPERAND (arg0, 0);
4163   tree arg01 = TREE_OPERAND (arg0, 1);
4164   tree arg1_type = TREE_TYPE (arg1);
4165   tree tem;
4166
4167   STRIP_NOPS (arg1);
4168   STRIP_NOPS (arg2);
4169
4170   /* If we have A op 0 ? A : -A, consider applying the following
4171      transformations:
4172
4173      A == 0? A : -A    same as -A
4174      A != 0? A : -A    same as 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      A < 0?  A : -A    same as -abs (A)
4179
4180      None of these transformations work for modes with signed
4181      zeros.  If A is +/-0, the first two transformations will
4182      change the sign of the result (from +0 to -0, or vice
4183      versa).  The last four will fix the sign of the result,
4184      even though the original expressions could be positive or
4185      negative, depending on the sign of A.
4186
4187      Note that all these transformations are correct if A is
4188      NaN, since the two alternatives (A and -A) are also NaNs.  */
4189   if ((FLOAT_TYPE_P (TREE_TYPE (arg01))
4190        ? real_zerop (arg01)
4191        : integer_zerop (arg01))
4192       && TREE_CODE (arg2) == NEGATE_EXPR
4193       && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4194     switch (comp_code)
4195       {
4196       case EQ_EXPR:
4197       case UNEQ_EXPR:
4198         tem = fold_convert (arg1_type, arg1);
4199         return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
4200       case NE_EXPR:
4201       case LTGT_EXPR:
4202         return pedantic_non_lvalue (fold_convert (type, arg1));
4203       case UNGE_EXPR:
4204       case UNGT_EXPR:
4205         if (flag_trapping_math)
4206           break;
4207         /* Fall through.  */
4208       case GE_EXPR:
4209       case GT_EXPR:
4210         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4211           arg1 = fold_convert (lang_hooks.types.signed_type
4212                                (TREE_TYPE (arg1)), arg1);
4213         tem = fold (build1 (ABS_EXPR, TREE_TYPE (arg1), arg1));
4214         return pedantic_non_lvalue (fold_convert (type, tem));
4215       case UNLE_EXPR:
4216       case UNLT_EXPR:
4217         if (flag_trapping_math)
4218           break;
4219       case LE_EXPR:
4220       case LT_EXPR:
4221         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4222           arg1 = fold_convert (lang_hooks.types.signed_type
4223                                (TREE_TYPE (arg1)), arg1);
4224         tem = fold (build1 (ABS_EXPR, TREE_TYPE (arg1), arg1));
4225         return negate_expr (fold_convert (type, tem));
4226       default:
4227         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4228         break;
4229       }
4230
4231   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
4232      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
4233      both transformations are correct when A is NaN: A != 0
4234      is then true, and A == 0 is false.  */
4235
4236   if (integer_zerop (arg01) && integer_zerop (arg2))
4237     {
4238       if (comp_code == NE_EXPR)
4239         return pedantic_non_lvalue (fold_convert (type, arg1));
4240       else if (comp_code == EQ_EXPR)
4241         return fold_convert (type, integer_zero_node);
4242     }
4243
4244   /* Try some transformations of A op B ? A : B.
4245
4246      A == B? A : B    same as B
4247      A != B? A : B    same as A
4248      A >= B? A : B    same as max (A, B)
4249      A > B?  A : B    same as max (B, A)
4250      A <= B? A : B    same as min (A, B)
4251      A < B?  A : B    same as min (B, A)
4252
4253      As above, these transformations don't work in the presence
4254      of signed zeros.  For example, if A and B are zeros of
4255      opposite sign, the first two transformations will change
4256      the sign of the result.  In the last four, the original
4257      expressions give different results for (A=+0, B=-0) and
4258      (A=-0, B=+0), but the transformed expressions do not.
4259
4260      The first two transformations are correct if either A or B
4261      is a NaN.  In the first transformation, the condition will
4262      be false, and B will indeed be chosen.  In the case of the
4263      second transformation, the condition A != B will be true,
4264      and A will be chosen.
4265
4266      The conversions to max() and min() are not correct if B is
4267      a number and A is not.  The conditions in the original
4268      expressions will be false, so all four give B.  The min()
4269      and max() versions would give a NaN instead.  */
4270   if (operand_equal_for_comparison_p (arg01, arg2, arg00))
4271     {
4272       tree comp_op0 = arg00;
4273       tree comp_op1 = arg01;
4274       tree comp_type = TREE_TYPE (comp_op0);
4275
4276       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
4277       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4278         {
4279           comp_type = type;
4280           comp_op0 = arg1;
4281           comp_op1 = arg2;
4282         }
4283
4284       switch (comp_code)
4285         {
4286         case EQ_EXPR:
4287           return pedantic_non_lvalue (fold_convert (type, arg2));
4288         case NE_EXPR:
4289           return pedantic_non_lvalue (fold_convert (type, arg1));
4290         case LE_EXPR:
4291         case LT_EXPR:
4292         case UNLE_EXPR:
4293         case UNLT_EXPR:
4294           /* In C++ a ?: expression can be an lvalue, so put the
4295              operand which will be used if they are equal first
4296              so that we can convert this back to the
4297              corresponding COND_EXPR.  */
4298           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4299             {
4300               comp_op0 = fold_convert (comp_type, comp_op0);
4301               comp_op1 = fold_convert (comp_type, comp_op1);
4302               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4303                     ? fold (build2 (MIN_EXPR, comp_type, comp_op0, comp_op1))
4304                     : fold (build2 (MIN_EXPR, comp_type, comp_op1, comp_op0));
4305               return pedantic_non_lvalue (fold_convert (type, tem));
4306             }
4307           break;
4308         case GE_EXPR:
4309         case GT_EXPR:
4310         case UNGE_EXPR:
4311         case UNGT_EXPR:
4312           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4313             {
4314               comp_op0 = fold_convert (comp_type, comp_op0);
4315               comp_op1 = fold_convert (comp_type, comp_op1);
4316               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4317                     ? fold (build2 (MAX_EXPR, comp_type, comp_op0, comp_op1))
4318                     : fold (build2 (MAX_EXPR, comp_type, comp_op1, comp_op0));
4319               return pedantic_non_lvalue (fold_convert (type, tem));
4320             }
4321           break;
4322         case UNEQ_EXPR:
4323           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4324             return pedantic_non_lvalue (fold_convert (type, arg2));
4325           break;
4326         case LTGT_EXPR:
4327           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4328             return pedantic_non_lvalue (fold_convert (type, arg1));
4329           break;
4330         default:
4331           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4332           break;
4333         }
4334     }
4335
4336   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4337      we might still be able to simplify this.  For example,
4338      if C1 is one less or one more than C2, this might have started
4339      out as a MIN or MAX and been transformed by this function.
4340      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
4341
4342   if (INTEGRAL_TYPE_P (type)
4343       && TREE_CODE (arg01) == INTEGER_CST
4344       && TREE_CODE (arg2) == INTEGER_CST)
4345     switch (comp_code)
4346       {
4347       case EQ_EXPR:
4348         /* We can replace A with C1 in this case.  */
4349         arg1 = fold_convert (type, arg01);
4350         return fold (build3 (COND_EXPR, type, arg0, arg1, arg2));
4351
4352       case LT_EXPR:
4353         /* If C1 is C2 + 1, this is min(A, C2).  */
4354         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4355                                OEP_ONLY_CONST)
4356             && operand_equal_p (arg01,
4357                                 const_binop (PLUS_EXPR, arg2,
4358                                              integer_one_node, 0),
4359                                 OEP_ONLY_CONST))
4360           return pedantic_non_lvalue (fold (build2 (MIN_EXPR,
4361                                                     type, arg1, arg2)));
4362         break;
4363
4364       case LE_EXPR:
4365         /* If C1 is C2 - 1, this is min(A, C2).  */
4366         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4367                                OEP_ONLY_CONST)
4368             && operand_equal_p (arg01,
4369                                 const_binop (MINUS_EXPR, arg2,
4370                                              integer_one_node, 0),
4371                                 OEP_ONLY_CONST))
4372           return pedantic_non_lvalue (fold (build2 (MIN_EXPR,
4373                                                     type, arg1, arg2)));
4374         break;
4375
4376       case GT_EXPR:
4377         /* If C1 is C2 - 1, this is max(A, C2).  */
4378         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4379                                OEP_ONLY_CONST)
4380             && operand_equal_p (arg01,
4381                                 const_binop (MINUS_EXPR, arg2,
4382                                              integer_one_node, 0),
4383                                 OEP_ONLY_CONST))
4384           return pedantic_non_lvalue (fold (build2 (MAX_EXPR,
4385                                                     type, arg1, arg2)));
4386         break;
4387
4388       case GE_EXPR:
4389         /* If C1 is C2 + 1, this is max(A, C2).  */
4390         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4391                                OEP_ONLY_CONST)
4392             && operand_equal_p (arg01,
4393                                 const_binop (PLUS_EXPR, arg2,
4394                                              integer_one_node, 0),
4395                                 OEP_ONLY_CONST))
4396           return pedantic_non_lvalue (fold (build2 (MAX_EXPR,
4397                                                     type, arg1, arg2)));
4398         break;
4399       case NE_EXPR:
4400         break;
4401       default:
4402         gcc_unreachable ();
4403       }
4404
4405   return NULL_TREE;
4406 }
4407
4408
4409 \f
4410 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4411 #define LOGICAL_OP_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
4412 #endif
4413
4414 /* EXP is some logical combination of boolean tests.  See if we can
4415    merge it into some range test.  Return the new tree if so.  */
4416
4417 static tree
4418 fold_range_test (tree exp)
4419 {
4420   int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
4421                || TREE_CODE (exp) == TRUTH_OR_EXPR);
4422   int in0_p, in1_p, in_p;
4423   tree low0, low1, low, high0, high1, high;
4424   tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
4425   tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
4426   tree tem;
4427
4428   /* If this is an OR operation, invert both sides; we will invert
4429      again at the end.  */
4430   if (or_op)
4431     in0_p = ! in0_p, in1_p = ! in1_p;
4432
4433   /* If both expressions are the same, if we can merge the ranges, and we
4434      can build the range test, return it or it inverted.  If one of the
4435      ranges is always true or always false, consider it to be the same
4436      expression as the other.  */
4437   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4438       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4439                        in1_p, low1, high1)
4440       && 0 != (tem = (build_range_check (TREE_TYPE (exp),
4441                                          lhs != 0 ? lhs
4442                                          : rhs != 0 ? rhs : integer_zero_node,
4443                                          in_p, low, high))))
4444     return or_op ? invert_truthvalue (tem) : tem;
4445
4446   /* On machines where the branch cost is expensive, if this is a
4447      short-circuited branch and the underlying object on both sides
4448      is the same, make a non-short-circuit operation.  */
4449   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4450            && lhs != 0 && rhs != 0
4451            && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
4452                || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
4453            && operand_equal_p (lhs, rhs, 0))
4454     {
4455       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
4456          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4457          which cases we can't do this.  */
4458       if (simple_operand_p (lhs))
4459         return build2 (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
4460                        ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4461                        TREE_TYPE (exp), TREE_OPERAND (exp, 0),
4462                        TREE_OPERAND (exp, 1));
4463
4464       else if (lang_hooks.decls.global_bindings_p () == 0
4465                && ! CONTAINS_PLACEHOLDER_P (lhs))
4466         {
4467           tree common = save_expr (lhs);
4468
4469           if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
4470                                              or_op ? ! in0_p : in0_p,
4471                                              low0, high0))
4472               && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
4473                                                  or_op ? ! in1_p : in1_p,
4474                                                  low1, high1))))
4475             return build2 (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
4476                            ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4477                            TREE_TYPE (exp), lhs, rhs);
4478         }
4479     }
4480
4481   return 0;
4482 }
4483 \f
4484 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4485    bit value.  Arrange things so the extra bits will be set to zero if and
4486    only if C is signed-extended to its full width.  If MASK is nonzero,
4487    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
4488
4489 static tree
4490 unextend (tree c, int p, int unsignedp, tree mask)
4491 {
4492   tree type = TREE_TYPE (c);
4493   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4494   tree temp;
4495
4496   if (p == modesize || unsignedp)
4497     return c;
4498
4499   /* We work by getting just the sign bit into the low-order bit, then
4500      into the high-order bit, then sign-extend.  We then XOR that value
4501      with C.  */
4502   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
4503   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
4504
4505   /* We must use a signed type in order to get an arithmetic right shift.
4506      However, we must also avoid introducing accidental overflows, so that
4507      a subsequent call to integer_zerop will work.  Hence we must
4508      do the type conversion here.  At this point, the constant is either
4509      zero or one, and the conversion to a signed type can never overflow.
4510      We could get an overflow if this conversion is done anywhere else.  */
4511   if (TYPE_UNSIGNED (type))
4512     temp = fold_convert (lang_hooks.types.signed_type (type), temp);
4513
4514   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
4515   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
4516   if (mask != 0)
4517     temp = const_binop (BIT_AND_EXPR, temp,
4518                         fold_convert (TREE_TYPE (c), mask), 0);
4519   /* If necessary, convert the type back to match the type of C.  */
4520   if (TYPE_UNSIGNED (type))
4521     temp = fold_convert (type, temp);
4522
4523   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
4524 }
4525 \f
4526 /* Find ways of folding logical expressions of LHS and RHS:
4527    Try to merge two comparisons to the same innermost item.
4528    Look for range tests like "ch >= '0' && ch <= '9'".
4529    Look for combinations of simple terms on machines with expensive branches
4530    and evaluate the RHS unconditionally.
4531
4532    For example, if we have p->a == 2 && p->b == 4 and we can make an
4533    object large enough to span both A and B, we can do this with a comparison
4534    against the object ANDed with the a mask.
4535
4536    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
4537    operations to do this with one comparison.
4538
4539    We check for both normal comparisons and the BIT_AND_EXPRs made this by
4540    function and the one above.
4541
4542    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
4543    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
4544
4545    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
4546    two operands.
4547
4548    We return the simplified tree or 0 if no optimization is possible.  */
4549
4550 static tree
4551 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
4552 {
4553   /* If this is the "or" of two comparisons, we can do something if
4554      the comparisons are NE_EXPR.  If this is the "and", we can do something
4555      if the comparisons are EQ_EXPR.  I.e.,
4556         (a->b == 2 && a->c == 4) can become (a->new == NEW).
4557
4558      WANTED_CODE is this operation code.  For single bit fields, we can
4559      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
4560      comparison for one-bit fields.  */
4561
4562   enum tree_code wanted_code;
4563   enum tree_code lcode, rcode;
4564   tree ll_arg, lr_arg, rl_arg, rr_arg;
4565   tree ll_inner, lr_inner, rl_inner, rr_inner;
4566   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
4567   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
4568   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
4569   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
4570   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
4571   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
4572   enum machine_mode lnmode, rnmode;
4573   tree ll_mask, lr_mask, rl_mask, rr_mask;
4574   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
4575   tree l_const, r_const;
4576   tree lntype, rntype, result;
4577   int first_bit, end_bit;
4578   int volatilep;
4579
4580   /* Start by getting the comparison codes.  Fail if anything is volatile.
4581      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
4582      it were surrounded with a NE_EXPR.  */
4583
4584   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
4585     return 0;
4586
4587   lcode = TREE_CODE (lhs);
4588   rcode = TREE_CODE (rhs);
4589
4590   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
4591     {
4592       lhs = build2 (NE_EXPR, truth_type, lhs,
4593                     fold_convert (TREE_TYPE (lhs), integer_zero_node));
4594       lcode = NE_EXPR;
4595     }
4596
4597   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
4598     {
4599       rhs = build2 (NE_EXPR, truth_type, rhs,
4600                     fold_convert (TREE_TYPE (rhs), integer_zero_node));
4601       rcode = NE_EXPR;
4602     }
4603
4604   if (TREE_CODE_CLASS (lcode) != tcc_comparison
4605       || TREE_CODE_CLASS (rcode) != tcc_comparison)
4606     return 0;
4607
4608   ll_arg = TREE_OPERAND (lhs, 0);
4609   lr_arg = TREE_OPERAND (lhs, 1);
4610   rl_arg = TREE_OPERAND (rhs, 0);
4611   rr_arg = TREE_OPERAND (rhs, 1);
4612
4613   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
4614   if (simple_operand_p (ll_arg)
4615       && simple_operand_p (lr_arg))
4616     {
4617       tree result;
4618       if (operand_equal_p (ll_arg, rl_arg, 0)
4619           && operand_equal_p (lr_arg, rr_arg, 0))
4620         {
4621           result = combine_comparisons (code, lcode, rcode,
4622                                         truth_type, ll_arg, lr_arg);
4623           if (result)
4624             return result;
4625         }
4626       else if (operand_equal_p (ll_arg, rr_arg, 0)
4627                && operand_equal_p (lr_arg, rl_arg, 0))
4628         {
4629           result = combine_comparisons (code, lcode,
4630                                         swap_tree_comparison (rcode),
4631                                         truth_type, ll_arg, lr_arg);
4632           if (result)
4633             return result;
4634         }
4635     }
4636
4637   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
4638           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
4639
4640   /* If the RHS can be evaluated unconditionally and its operands are
4641      simple, it wins to evaluate the RHS unconditionally on machines
4642      with expensive branches.  In this case, this isn't a comparison
4643      that can be merged.  Avoid doing this if the RHS is a floating-point
4644      comparison since those can trap.  */
4645
4646   if (BRANCH_COST >= 2
4647       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
4648       && simple_operand_p (rl_arg)
4649       && simple_operand_p (rr_arg))
4650     {
4651       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
4652       if (code == TRUTH_OR_EXPR
4653           && lcode == NE_EXPR && integer_zerop (lr_arg)
4654           && rcode == NE_EXPR && integer_zerop (rr_arg)
4655           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
4656         return build2 (NE_EXPR, truth_type,
4657                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
4658                                ll_arg, rl_arg),
4659                        fold_convert (TREE_TYPE (ll_arg), integer_zero_node));
4660
4661       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
4662       if (code == TRUTH_AND_EXPR
4663           && lcode == EQ_EXPR && integer_zerop (lr_arg)
4664           && rcode == EQ_EXPR && integer_zerop (rr_arg)
4665           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
4666         return build2 (EQ_EXPR, truth_type,
4667                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
4668                                ll_arg, rl_arg),
4669                        fold_convert (TREE_TYPE (ll_arg), integer_zero_node));
4670
4671       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
4672         return build2 (code, truth_type, lhs, rhs);
4673     }
4674
4675   /* See if the comparisons can be merged.  Then get all the parameters for
4676      each side.  */
4677
4678   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
4679       || (rcode != EQ_EXPR && rcode != NE_EXPR))
4680     return 0;
4681
4682   volatilep = 0;
4683   ll_inner = decode_field_reference (ll_arg,
4684                                      &ll_bitsize, &ll_bitpos, &ll_mode,
4685                                      &ll_unsignedp, &volatilep, &ll_mask,
4686                                      &ll_and_mask);
4687   lr_inner = decode_field_reference (lr_arg,
4688                                      &lr_bitsize, &lr_bitpos, &lr_mode,
4689                                      &lr_unsignedp, &volatilep, &lr_mask,
4690                                      &lr_and_mask);
4691   rl_inner = decode_field_reference (rl_arg,
4692                                      &rl_bitsize, &rl_bitpos, &rl_mode,
4693                                      &rl_unsignedp, &volatilep, &rl_mask,
4694                                      &rl_and_mask);
4695   rr_inner = decode_field_reference (rr_arg,
4696                                      &rr_bitsize, &rr_bitpos, &rr_mode,
4697                                      &rr_unsignedp, &volatilep, &rr_mask,
4698                                      &rr_and_mask);
4699
4700   /* It must be true that the inner operation on the lhs of each
4701      comparison must be the same if we are to be able to do anything.
4702      Then see if we have constants.  If not, the same must be true for
4703      the rhs's.  */
4704   if (volatilep || ll_inner == 0 || rl_inner == 0
4705       || ! operand_equal_p (ll_inner, rl_inner, 0))
4706     return 0;
4707
4708   if (TREE_CODE (lr_arg) == INTEGER_CST
4709       && TREE_CODE (rr_arg) == INTEGER_CST)
4710     l_const = lr_arg, r_const = rr_arg;
4711   else if (lr_inner == 0 || rr_inner == 0
4712            || ! operand_equal_p (lr_inner, rr_inner, 0))
4713     return 0;
4714   else
4715     l_const = r_const = 0;
4716
4717   /* If either comparison code is not correct for our logical operation,
4718      fail.  However, we can convert a one-bit comparison against zero into
4719      the opposite comparison against that bit being set in the field.  */
4720
4721   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
4722   if (lcode != wanted_code)
4723     {
4724       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
4725         {
4726           /* Make the left operand unsigned, since we are only interested
4727              in the value of one bit.  Otherwise we are doing the wrong
4728              thing below.  */
4729           ll_unsignedp = 1;
4730           l_const = ll_mask;
4731         }
4732       else
4733         return 0;
4734     }
4735
4736   /* This is analogous to the code for l_const above.  */
4737   if (rcode != wanted_code)
4738     {
4739       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
4740         {
4741           rl_unsignedp = 1;
4742           r_const = rl_mask;
4743         }
4744       else
4745         return 0;
4746     }
4747
4748   /* After this point all optimizations will generate bit-field
4749      references, which we might not want.  */
4750   if (! lang_hooks.can_use_bit_fields_p ())
4751     return 0;
4752
4753   /* See if we can find a mode that contains both fields being compared on
4754      the left.  If we can't, fail.  Otherwise, update all constants and masks
4755      to be relative to a field of that size.  */
4756   first_bit = MIN (ll_bitpos, rl_bitpos);
4757   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
4758   lnmode = get_best_mode (end_bit - first_bit, first_bit,
4759                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
4760                           volatilep);
4761   if (lnmode == VOIDmode)
4762     return 0;
4763
4764   lnbitsize = GET_MODE_BITSIZE (lnmode);
4765   lnbitpos = first_bit & ~ (lnbitsize - 1);
4766   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
4767   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
4768
4769   if (BYTES_BIG_ENDIAN)
4770     {
4771       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
4772       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
4773     }
4774
4775   ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
4776                          size_int (xll_bitpos), 0);
4777   rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
4778                          size_int (xrl_bitpos), 0);
4779
4780   if (l_const)
4781     {
4782       l_const = fold_convert (lntype, l_const);
4783       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
4784       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
4785       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
4786                                         fold (build1 (BIT_NOT_EXPR,
4787                                                       lntype, ll_mask)),
4788                                         0)))
4789         {
4790           warning ("comparison is always %d", wanted_code == NE_EXPR);
4791
4792           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
4793         }
4794     }
4795   if (r_const)
4796     {
4797       r_const = fold_convert (lntype, r_const);
4798       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
4799       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
4800       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
4801                                         fold (build1 (BIT_NOT_EXPR,
4802                                                       lntype, rl_mask)),
4803                                         0)))
4804         {
4805           warning ("comparison is always %d", wanted_code == NE_EXPR);
4806
4807           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
4808         }
4809     }
4810
4811   /* If the right sides are not constant, do the same for it.  Also,
4812      disallow this optimization if a size or signedness mismatch occurs
4813      between the left and right sides.  */
4814   if (l_const == 0)
4815     {
4816       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
4817           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
4818           /* Make sure the two fields on the right
4819              correspond to the left without being swapped.  */
4820           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
4821         return 0;
4822
4823       first_bit = MIN (lr_bitpos, rr_bitpos);
4824       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
4825       rnmode = get_best_mode (end_bit - first_bit, first_bit,
4826                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
4827                               volatilep);
4828       if (rnmode == VOIDmode)
4829         return 0;
4830
4831       rnbitsize = GET_MODE_BITSIZE (rnmode);
4832       rnbitpos = first_bit & ~ (rnbitsize - 1);
4833       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
4834       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
4835
4836       if (BYTES_BIG_ENDIAN)
4837         {
4838           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
4839           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
4840         }
4841
4842       lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
4843                              size_int (xlr_bitpos), 0);
4844       rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
4845                              size_int (xrr_bitpos), 0);
4846
4847       /* Make a mask that corresponds to both fields being compared.
4848          Do this for both items being compared.  If the operands are the
4849          same size and the bits being compared are in the same position
4850          then we can do this by masking both and comparing the masked
4851          results.  */
4852       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4853       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
4854       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
4855         {
4856           lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4857                                     ll_unsignedp || rl_unsignedp);
4858           if (! all_ones_mask_p (ll_mask, lnbitsize))
4859             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
4860
4861           rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
4862                                     lr_unsignedp || rr_unsignedp);
4863           if (! all_ones_mask_p (lr_mask, rnbitsize))
4864             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
4865
4866           return build2 (wanted_code, truth_type, lhs, rhs);
4867         }
4868
4869       /* There is still another way we can do something:  If both pairs of
4870          fields being compared are adjacent, we may be able to make a wider
4871          field containing them both.
4872
4873          Note that we still must mask the lhs/rhs expressions.  Furthermore,
4874          the mask must be shifted to account for the shift done by
4875          make_bit_field_ref.  */
4876       if ((ll_bitsize + ll_bitpos == rl_bitpos
4877            && lr_bitsize + lr_bitpos == rr_bitpos)
4878           || (ll_bitpos == rl_bitpos + rl_bitsize
4879               && lr_bitpos == rr_bitpos + rr_bitsize))
4880         {
4881           tree type;
4882
4883           lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
4884                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
4885           rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
4886                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
4887
4888           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
4889                                  size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
4890           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
4891                                  size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
4892
4893           /* Convert to the smaller type before masking out unwanted bits.  */
4894           type = lntype;
4895           if (lntype != rntype)
4896             {
4897               if (lnbitsize > rnbitsize)
4898                 {
4899                   lhs = fold_convert (rntype, lhs);
4900                   ll_mask = fold_convert (rntype, ll_mask);
4901                   type = rntype;
4902                 }
4903               else if (lnbitsize < rnbitsize)
4904                 {
4905                   rhs = fold_convert (lntype, rhs);
4906                   lr_mask = fold_convert (lntype, lr_mask);
4907                   type = lntype;
4908                 }
4909             }
4910
4911           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
4912             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
4913
4914           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
4915             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
4916
4917           return build2 (wanted_code, truth_type, lhs, rhs);
4918         }
4919
4920       return 0;
4921     }
4922
4923   /* Handle the case of comparisons with constants.  If there is something in
4924      common between the masks, those bits of the constants must be the same.
4925      If not, the condition is always false.  Test for this to avoid generating
4926      incorrect code below.  */
4927   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
4928   if (! integer_zerop (result)
4929       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
4930                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
4931     {
4932       if (wanted_code == NE_EXPR)
4933         {
4934           warning ("%<or%> of unmatched not-equal tests is always 1");
4935           return constant_boolean_node (true, truth_type);
4936         }
4937       else
4938         {
4939           warning ("%<and%> of mutually exclusive equal-tests is always 0");
4940           return constant_boolean_node (false, truth_type);
4941         }
4942     }
4943
4944   /* Construct the expression we will return.  First get the component
4945      reference we will make.  Unless the mask is all ones the width of
4946      that field, perform the mask operation.  Then compare with the
4947      merged constant.  */
4948   result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4949                                ll_unsignedp || rl_unsignedp);
4950
4951   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4952   if (! all_ones_mask_p (ll_mask, lnbitsize))
4953     result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
4954
4955   return build2 (wanted_code, truth_type, result,
4956                  const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
4957 }
4958 \f
4959 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
4960    constant.  */
4961
4962 static tree
4963 optimize_minmax_comparison (tree t)
4964 {
4965   tree type = TREE_TYPE (t);
4966   tree arg0 = TREE_OPERAND (t, 0);
4967   enum tree_code op_code;
4968   tree comp_const = TREE_OPERAND (t, 1);
4969   tree minmax_const;
4970   int consts_equal, consts_lt;
4971   tree inner;
4972
4973   STRIP_SIGN_NOPS (arg0);
4974
4975   op_code = TREE_CODE (arg0);
4976   minmax_const = TREE_OPERAND (arg0, 1);
4977   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
4978   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
4979   inner = TREE_OPERAND (arg0, 0);
4980
4981   /* If something does not permit us to optimize, return the original tree.  */
4982   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
4983       || TREE_CODE (comp_const) != INTEGER_CST
4984       || TREE_CONSTANT_OVERFLOW (comp_const)
4985       || TREE_CODE (minmax_const) != INTEGER_CST
4986       || TREE_CONSTANT_OVERFLOW (minmax_const))
4987     return t;
4988
4989   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
4990      and GT_EXPR, doing the rest with recursive calls using logical
4991      simplifications.  */
4992   switch (TREE_CODE (t))
4993     {
4994     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
4995       return
4996         invert_truthvalue (optimize_minmax_comparison (invert_truthvalue (t)));
4997
4998     case GE_EXPR:
4999       return
5000         fold (build2 (TRUTH_ORIF_EXPR, type,
5001                       optimize_minmax_comparison
5002                       (build2 (EQ_EXPR, type, arg0, comp_const)),
5003                       optimize_minmax_comparison
5004                       (build2 (GT_EXPR, type, arg0, comp_const))));
5005
5006     case EQ_EXPR:
5007       if (op_code == MAX_EXPR && consts_equal)
5008         /* MAX (X, 0) == 0  ->  X <= 0  */
5009         return fold (build2 (LE_EXPR, type, inner, comp_const));
5010
5011       else if (op_code == MAX_EXPR && consts_lt)
5012         /* MAX (X, 0) == 5  ->  X == 5   */
5013         return fold (build2 (EQ_EXPR, type, inner, comp_const));
5014
5015       else if (op_code == MAX_EXPR)
5016         /* MAX (X, 0) == -1  ->  false  */
5017         return omit_one_operand (type, integer_zero_node, inner);
5018
5019       else if (consts_equal)
5020         /* MIN (X, 0) == 0  ->  X >= 0  */
5021         return fold (build2 (GE_EXPR, type, inner, comp_const));
5022
5023       else if (consts_lt)
5024         /* MIN (X, 0) == 5  ->  false  */
5025         return omit_one_operand (type, integer_zero_node, inner);
5026
5027       else
5028         /* MIN (X, 0) == -1  ->  X == -1  */
5029         return fold (build2 (EQ_EXPR, type, inner, comp_const));
5030
5031     case GT_EXPR:
5032       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5033         /* MAX (X, 0) > 0  ->  X > 0
5034            MAX (X, 0) > 5  ->  X > 5  */
5035         return fold (build2 (GT_EXPR, type, inner, comp_const));
5036
5037       else if (op_code == MAX_EXPR)
5038         /* MAX (X, 0) > -1  ->  true  */
5039         return omit_one_operand (type, integer_one_node, inner);
5040
5041       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5042         /* MIN (X, 0) > 0  ->  false
5043            MIN (X, 0) > 5  ->  false  */
5044         return omit_one_operand (type, integer_zero_node, inner);
5045
5046       else
5047         /* MIN (X, 0) > -1  ->  X > -1  */
5048         return fold (build2 (GT_EXPR, type, inner, comp_const));
5049
5050     default:
5051       return t;
5052     }
5053 }
5054 \f
5055 /* T is an integer expression that is being multiplied, divided, or taken a
5056    modulus (CODE says which and what kind of divide or modulus) by a
5057    constant C.  See if we can eliminate that operation by folding it with
5058    other operations already in T.  WIDE_TYPE, if non-null, is a type that
5059    should be used for the computation if wider than our type.
5060
5061    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5062    (X * 2) + (Y * 4).  We must, however, be assured that either the original
5063    expression would not overflow or that overflow is undefined for the type
5064    in the language in question.
5065
5066    We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
5067    the machine has a multiply-accumulate insn or that this is part of an
5068    addressing calculation.
5069
5070    If we return a non-null expression, it is an equivalent form of the
5071    original computation, but need not be in the original type.  */
5072
5073 static tree
5074 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
5075 {
5076   /* To avoid exponential search depth, refuse to allow recursion past
5077      three levels.  Beyond that (1) it's highly unlikely that we'll find
5078      something interesting and (2) we've probably processed it before
5079      when we built the inner expression.  */
5080
5081   static int depth;
5082   tree ret;
5083
5084   if (depth > 3)
5085     return NULL;
5086
5087   depth++;
5088   ret = extract_muldiv_1 (t, c, code, wide_type);
5089   depth--;
5090
5091   return ret;
5092 }
5093
5094 static tree
5095 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
5096 {
5097   tree type = TREE_TYPE (t);
5098   enum tree_code tcode = TREE_CODE (t);
5099   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5100                                    > GET_MODE_SIZE (TYPE_MODE (type)))
5101                 ? wide_type : type);
5102   tree t1, t2;
5103   int same_p = tcode == code;
5104   tree op0 = NULL_TREE, op1 = NULL_TREE;
5105
5106   /* Don't deal with constants of zero here; they confuse the code below.  */
5107   if (integer_zerop (c))
5108     return NULL_TREE;
5109
5110   if (TREE_CODE_CLASS (tcode) == tcc_unary)
5111     op0 = TREE_OPERAND (t, 0);
5112
5113   if (TREE_CODE_CLASS (tcode) == tcc_binary)
5114     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5115
5116   /* Note that we need not handle conditional operations here since fold
5117      already handles those cases.  So just do arithmetic here.  */
5118   switch (tcode)
5119     {
5120     case INTEGER_CST:
5121       /* For a constant, we can always simplify if we are a multiply
5122          or (for divide and modulus) if it is a multiple of our constant.  */
5123       if (code == MULT_EXPR
5124           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
5125         return const_binop (code, fold_convert (ctype, t),
5126                             fold_convert (ctype, c), 0);
5127       break;
5128
5129     case CONVERT_EXPR:  case NON_LVALUE_EXPR:  case NOP_EXPR:
5130       /* If op0 is an expression ...  */
5131       if ((COMPARISON_CLASS_P (op0)
5132            || UNARY_CLASS_P (op0)
5133            || BINARY_CLASS_P (op0)
5134            || EXPRESSION_CLASS_P (op0))
5135           /* ... and is unsigned, and its type is smaller than ctype,
5136              then we cannot pass through as widening.  */
5137           && ((TYPE_UNSIGNED (TREE_TYPE (op0))
5138                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5139                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5140                && (GET_MODE_SIZE (TYPE_MODE (ctype))
5141                    > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
5142               /* ... or this is a truncation (t is narrower than op0),
5143                  then we cannot pass through this narrowing.  */
5144               || (GET_MODE_SIZE (TYPE_MODE (type))
5145                   < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
5146               /* ... or signedness changes for division or modulus,
5147                  then we cannot pass through this conversion.  */
5148               || (code != MULT_EXPR
5149                   && (TYPE_UNSIGNED (ctype)
5150                       != TYPE_UNSIGNED (TREE_TYPE (op0))))))
5151         break;
5152
5153       /* Pass the constant down and see if we can make a simplification.  If
5154          we can, replace this expression with the inner simplification for
5155          possible later conversion to our or some other type.  */
5156       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5157           && TREE_CODE (t2) == INTEGER_CST
5158           && ! TREE_CONSTANT_OVERFLOW (t2)
5159           && (0 != (t1 = extract_muldiv (op0, t2, code,
5160                                          code == MULT_EXPR
5161                                          ? ctype : NULL_TREE))))
5162         return t1;
5163       break;
5164
5165     case ABS_EXPR:
5166       /* If widening the type changes it from signed to unsigned, then we
5167          must avoid building ABS_EXPR itself as unsigned.  */
5168       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5169         {
5170           tree cstype = (*lang_hooks.types.signed_type) (ctype);
5171           if ((t1 = extract_muldiv (op0, c, code, cstype)) != 0)
5172             {
5173               t1 = fold (build1 (tcode, cstype, fold_convert (cstype, t1)));
5174               return fold_convert (ctype, t1);
5175             }
5176           break;
5177         }
5178       /* FALLTHROUGH */
5179     case NEGATE_EXPR:
5180       if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
5181         return fold (build1 (tcode, ctype, fold_convert (ctype, t1)));
5182       break;
5183
5184     case MIN_EXPR:  case MAX_EXPR:
5185       /* If widening the type changes the signedness, then we can't perform
5186          this optimization as that changes the result.  */
5187       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5188         break;
5189
5190       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
5191       if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
5192           && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
5193         {
5194           if (tree_int_cst_sgn (c) < 0)
5195             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5196
5197           return fold (build2 (tcode, ctype, fold_convert (ctype, t1),
5198                                fold_convert (ctype, t2)));
5199         }
5200       break;
5201
5202     case LSHIFT_EXPR:  case RSHIFT_EXPR:
5203       /* If the second operand is constant, this is a multiplication
5204          or floor division, by a power of two, so we can treat it that
5205          way unless the multiplier or divisor overflows.  Signed
5206          left-shift overflow is implementation-defined rather than
5207          undefined in C90, so do not convert signed left shift into
5208          multiplication.  */
5209       if (TREE_CODE (op1) == INTEGER_CST
5210           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5211           /* const_binop may not detect overflow correctly,
5212              so check for it explicitly here.  */
5213           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5214           && TREE_INT_CST_HIGH (op1) == 0
5215           && 0 != (t1 = fold_convert (ctype,
5216                                       const_binop (LSHIFT_EXPR,
5217                                                    size_one_node,
5218                                                    op1, 0)))
5219           && ! TREE_OVERFLOW (t1))
5220         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5221                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
5222                                        ctype, fold_convert (ctype, op0), t1),
5223                                c, code, wide_type);
5224       break;
5225
5226     case PLUS_EXPR:  case MINUS_EXPR:
5227       /* See if we can eliminate the operation on both sides.  If we can, we
5228          can return a new PLUS or MINUS.  If we can't, the only remaining
5229          cases where we can do anything are if the second operand is a
5230          constant.  */
5231       t1 = extract_muldiv (op0, c, code, wide_type);
5232       t2 = extract_muldiv (op1, c, code, wide_type);
5233       if (t1 != 0 && t2 != 0
5234           && (code == MULT_EXPR
5235               /* If not multiplication, we can only do this if both operands
5236                  are divisible by c.  */
5237               || (multiple_of_p (ctype, op0, c)
5238                   && multiple_of_p (ctype, op1, c))))
5239         return fold (build2 (tcode, ctype, fold_convert (ctype, t1),
5240                              fold_convert (ctype, t2)));
5241
5242       /* If this was a subtraction, negate OP1 and set it to be an addition.
5243          This simplifies the logic below.  */
5244       if (tcode == MINUS_EXPR)
5245         tcode = PLUS_EXPR, op1 = negate_expr (op1);
5246
5247       if (TREE_CODE (op1) != INTEGER_CST)
5248         break;
5249
5250       /* If either OP1 or C are negative, this optimization is not safe for
5251          some of the division and remainder types while for others we need
5252          to change the code.  */
5253       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5254         {
5255           if (code == CEIL_DIV_EXPR)
5256             code = FLOOR_DIV_EXPR;
5257           else if (code == FLOOR_DIV_EXPR)
5258             code = CEIL_DIV_EXPR;
5259           else if (code != MULT_EXPR
5260                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5261             break;
5262         }
5263
5264       /* If it's a multiply or a division/modulus operation of a multiple
5265          of our constant, do the operation and verify it doesn't overflow.  */
5266       if (code == MULT_EXPR
5267           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5268         {
5269           op1 = const_binop (code, fold_convert (ctype, op1),
5270                              fold_convert (ctype, c), 0);
5271           /* We allow the constant to overflow with wrapping semantics.  */
5272           if (op1 == 0
5273               || (TREE_OVERFLOW (op1) && ! flag_wrapv))
5274             break;
5275         }
5276       else
5277         break;
5278
5279       /* If we have an unsigned type is not a sizetype, we cannot widen
5280          the operation since it will change the result if the original
5281          computation overflowed.  */
5282       if (TYPE_UNSIGNED (ctype)
5283           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5284           && ctype != type)
5285         break;
5286
5287       /* If we were able to eliminate our operation from the first side,
5288          apply our operation to the second side and reform the PLUS.  */
5289       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5290         return fold (build2 (tcode, ctype, fold_convert (ctype, t1), op1));
5291
5292       /* The last case is if we are a multiply.  In that case, we can
5293          apply the distributive law to commute the multiply and addition
5294          if the multiplication of the constants doesn't overflow.  */
5295       if (code == MULT_EXPR)
5296         return fold (build2 (tcode, ctype,
5297                              fold (build2 (code, ctype,
5298                                            fold_convert (ctype, op0),
5299                                            fold_convert (ctype, c))),
5300                              op1));
5301
5302       break;
5303
5304     case MULT_EXPR:
5305       /* We have a special case here if we are doing something like
5306          (C * 8) % 4 since we know that's zero.  */
5307       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5308            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5309           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5310           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5311         return omit_one_operand (type, integer_zero_node, op0);
5312
5313       /* ... fall through ...  */
5314
5315     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
5316     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
5317       /* If we can extract our operation from the LHS, do so and return a
5318          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
5319          do something only if the second operand is a constant.  */
5320       if (same_p
5321           && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
5322         return fold (build2 (tcode, ctype, fold_convert (ctype, t1),
5323                              fold_convert (ctype, op1)));
5324       else if (tcode == MULT_EXPR && code == MULT_EXPR
5325                && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
5326         return fold (build2 (tcode, ctype, fold_convert (ctype, op0),
5327                              fold_convert (ctype, t1)));
5328       else if (TREE_CODE (op1) != INTEGER_CST)
5329         return 0;
5330
5331       /* If these are the same operation types, we can associate them
5332          assuming no overflow.  */
5333       if (tcode == code
5334           && 0 != (t1 = const_binop (MULT_EXPR, fold_convert (ctype, op1),
5335                                      fold_convert (ctype, c), 0))
5336           && ! TREE_OVERFLOW (t1))
5337         return fold (build2 (tcode, ctype, fold_convert (ctype, op0), t1));
5338
5339       /* If these operations "cancel" each other, we have the main
5340          optimizations of this pass, which occur when either constant is a
5341          multiple of the other, in which case we replace this with either an
5342          operation or CODE or TCODE.
5343
5344          If we have an unsigned type that is not a sizetype, we cannot do
5345          this since it will change the result if the original computation
5346          overflowed.  */
5347       if ((! TYPE_UNSIGNED (ctype)
5348            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5349           && ! flag_wrapv
5350           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5351               || (tcode == MULT_EXPR
5352                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5353                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
5354         {
5355           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5356             return fold (build2 (tcode, ctype, fold_convert (ctype, op0),
5357                                  fold_convert (ctype,
5358                                                const_binop (TRUNC_DIV_EXPR,
5359                                                             op1, c, 0))));
5360           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
5361             return fold (build2 (code, ctype, fold_convert (ctype, op0),
5362                                  fold_convert (ctype,
5363                                                const_binop (TRUNC_DIV_EXPR,
5364                                                             c, op1, 0))));
5365         }
5366       break;
5367
5368     default:
5369       break;
5370     }
5371
5372   return 0;
5373 }
5374 \f
5375 /* Return a node which has the indicated constant VALUE (either 0 or
5376    1), and is of the indicated TYPE.  */
5377
5378 tree
5379 constant_boolean_node (int value, tree type)
5380 {
5381   if (type == integer_type_node)
5382     return value ? integer_one_node : integer_zero_node;
5383   else if (type == boolean_type_node)
5384     return value ? boolean_true_node : boolean_false_node;
5385   else if (TREE_CODE (type) == BOOLEAN_TYPE)
5386     return lang_hooks.truthvalue_conversion (value ? integer_one_node
5387                                                    : integer_zero_node);
5388   else
5389     return build_int_cst (type, value);
5390 }
5391
5392
5393 /* Return true if expr looks like an ARRAY_REF and set base and
5394    offset to the appropriate trees.  If there is no offset,
5395    offset is set to NULL_TREE.  */
5396
5397 static bool
5398 extract_array_ref (tree expr, tree *base, tree *offset)
5399 {
5400   /* We have to be careful with stripping nops as with the
5401      base type the meaning of the offset can change.  */
5402   tree inner_expr = expr;
5403   STRIP_NOPS (inner_expr);
5404   /* One canonical form is a PLUS_EXPR with the first
5405      argument being an ADDR_EXPR with a possible NOP_EXPR
5406      attached.  */
5407   if (TREE_CODE (expr) == PLUS_EXPR)
5408     {
5409       tree op0 = TREE_OPERAND (expr, 0);
5410       STRIP_NOPS (op0);
5411       if (TREE_CODE (op0) == ADDR_EXPR)
5412         {
5413           *base = TREE_OPERAND (expr, 0);
5414           *offset = TREE_OPERAND (expr, 1);
5415           return true;
5416         }
5417     }
5418   /* Other canonical form is an ADDR_EXPR of an ARRAY_REF,
5419      which we transform into an ADDR_EXPR with appropriate
5420      offset.  For other arguments to the ADDR_EXPR we assume
5421      zero offset and as such do not care about the ADDR_EXPR
5422      type and strip possible nops from it.  */
5423   else if (TREE_CODE (inner_expr) == ADDR_EXPR)
5424     {
5425       tree op0 = TREE_OPERAND (inner_expr, 0);
5426       if (TREE_CODE (op0) == ARRAY_REF)
5427         {
5428           *base = build_fold_addr_expr (TREE_OPERAND (op0, 0));
5429           *offset = TREE_OPERAND (op0, 1);
5430         }
5431       else
5432         {
5433           *base = inner_expr;
5434           *offset = NULL_TREE;
5435         }
5436       return true;
5437     }
5438
5439   return false;
5440 }
5441
5442
5443 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
5444    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
5445    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
5446    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
5447    COND is the first argument to CODE; otherwise (as in the example
5448    given here), it is the second argument.  TYPE is the type of the
5449    original expression.  Return NULL_TREE if no simplification is
5450    possible.  */
5451
5452 static tree
5453 fold_binary_op_with_conditional_arg (tree t, enum tree_code code, tree cond,
5454                                      tree arg, int cond_first_p)
5455 {
5456   const tree type = TREE_TYPE (t);
5457   tree cond_type = cond_first_p ? TREE_TYPE (TREE_OPERAND (t, 0)) 
5458                                 : TREE_TYPE (TREE_OPERAND (t, 1));
5459   tree arg_type = cond_first_p ? TREE_TYPE (TREE_OPERAND (t, 1)) 
5460                                : TREE_TYPE (TREE_OPERAND (t, 0));
5461   tree test, true_value, false_value;
5462   tree lhs = NULL_TREE;
5463   tree rhs = NULL_TREE;
5464
5465   /* This transformation is only worthwhile if we don't have to wrap
5466      arg in a SAVE_EXPR, and the operation can be simplified on at least
5467      one of the branches once its pushed inside the COND_EXPR.  */
5468   if (!TREE_CONSTANT (arg))
5469     return NULL_TREE;
5470
5471   if (TREE_CODE (cond) == COND_EXPR)
5472     {
5473       test = TREE_OPERAND (cond, 0);
5474       true_value = TREE_OPERAND (cond, 1);
5475       false_value = TREE_OPERAND (cond, 2);
5476       /* If this operand throws an expression, then it does not make
5477          sense to try to perform a logical or arithmetic operation
5478          involving it.  */
5479       if (VOID_TYPE_P (TREE_TYPE (true_value)))
5480         lhs = true_value;
5481       if (VOID_TYPE_P (TREE_TYPE (false_value)))
5482         rhs = false_value;
5483     }
5484   else
5485     {
5486       tree testtype = TREE_TYPE (cond);
5487       test = cond;
5488       true_value = constant_boolean_node (true, testtype);
5489       false_value = constant_boolean_node (false, testtype);
5490     }
5491
5492   arg = fold_convert (arg_type, arg);
5493   if (lhs == 0)
5494     {
5495       true_value = fold_convert (cond_type, true_value);
5496       lhs = fold (cond_first_p ? build2 (code, type, true_value, arg)
5497                              : build2 (code, type, arg, true_value));
5498     }
5499   if (rhs == 0)
5500     {
5501       false_value = fold_convert (cond_type, false_value);
5502       rhs = fold (cond_first_p ? build2 (code, type, false_value, arg)
5503                              : build2 (code, type, arg, false_value));
5504     }
5505
5506   test = fold (build3 (COND_EXPR, type, test, lhs, rhs));
5507   return fold_convert (type, test);
5508 }
5509
5510 \f
5511 /* Subroutine of fold() that checks for the addition of +/- 0.0.
5512
5513    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
5514    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
5515    ADDEND is the same as X.
5516
5517    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
5518    and finite.  The problematic cases are when X is zero, and its mode
5519    has signed zeros.  In the case of rounding towards -infinity,
5520    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
5521    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
5522
5523 static bool
5524 fold_real_zero_addition_p (tree type, tree addend, int negate)
5525 {
5526   if (!real_zerop (addend))
5527     return false;
5528
5529   /* Don't allow the fold with -fsignaling-nans.  */
5530   if (HONOR_SNANS (TYPE_MODE (type)))
5531     return false;
5532
5533   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
5534   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
5535     return true;
5536
5537   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
5538   if (TREE_CODE (addend) == REAL_CST
5539       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
5540     negate = !negate;
5541
5542   /* The mode has signed zeros, and we have to honor their sign.
5543      In this situation, there is only one case we can return true for.
5544      X - 0 is the same as X unless rounding towards -infinity is
5545      supported.  */
5546   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
5547 }
5548
5549 /* Subroutine of fold() that checks comparisons of built-in math
5550    functions against real constants.
5551
5552    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
5553    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
5554    is the type of the result and ARG0 and ARG1 are the operands of the
5555    comparison.  ARG1 must be a TREE_REAL_CST.
5556
5557    The function returns the constant folded tree if a simplification
5558    can be made, and NULL_TREE otherwise.  */
5559
5560 static tree
5561 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
5562                      tree type, tree arg0, tree arg1)
5563 {
5564   REAL_VALUE_TYPE c;
5565
5566   if (BUILTIN_SQRT_P (fcode))
5567     {
5568       tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
5569       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
5570
5571       c = TREE_REAL_CST (arg1);
5572       if (REAL_VALUE_NEGATIVE (c))
5573         {
5574           /* sqrt(x) < y is always false, if y is negative.  */
5575           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
5576             return omit_one_operand (type, integer_zero_node, arg);
5577
5578           /* sqrt(x) > y is always true, if y is negative and we
5579              don't care about NaNs, i.e. negative values of x.  */
5580           if (code == NE_EXPR || !HONOR_NANS (mode))
5581             return omit_one_operand (type, integer_one_node, arg);
5582
5583           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
5584           return fold (build2 (GE_EXPR, type, arg,
5585                                build_real (TREE_TYPE (arg), dconst0)));
5586         }
5587       else if (code == GT_EXPR || code == GE_EXPR)
5588         {
5589           REAL_VALUE_TYPE c2;
5590
5591           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5592           real_convert (&c2, mode, &c2);
5593
5594           if (REAL_VALUE_ISINF (c2))
5595             {
5596               /* sqrt(x) > y is x == +Inf, when y is very large.  */
5597               if (HONOR_INFINITIES (mode))
5598                 return fold (build2 (EQ_EXPR, type, arg,
5599                                      build_real (TREE_TYPE (arg), c2)));
5600
5601               /* sqrt(x) > y is always false, when y is very large
5602                  and we don't care about infinities.  */
5603               return omit_one_operand (type, integer_zero_node, arg);
5604             }
5605
5606           /* sqrt(x) > c is the same as x > c*c.  */
5607           return fold (build2 (code, type, arg,
5608                                build_real (TREE_TYPE (arg), c2)));
5609         }
5610       else if (code == LT_EXPR || code == LE_EXPR)
5611         {
5612           REAL_VALUE_TYPE c2;
5613
5614           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5615           real_convert (&c2, mode, &c2);
5616
5617           if (REAL_VALUE_ISINF (c2))
5618             {
5619               /* sqrt(x) < y is always true, when y is a very large
5620                  value and we don't care about NaNs or Infinities.  */
5621               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
5622                 return omit_one_operand (type, integer_one_node, arg);
5623
5624               /* sqrt(x) < y is x != +Inf when y is very large and we
5625                  don't care about NaNs.  */
5626               if (! HONOR_NANS (mode))
5627                 return fold (build2 (NE_EXPR, type, arg,
5628                                      build_real (TREE_TYPE (arg), c2)));
5629
5630               /* sqrt(x) < y is x >= 0 when y is very large and we
5631                  don't care about Infinities.  */
5632               if (! HONOR_INFINITIES (mode))
5633                 return fold (build2 (GE_EXPR, type, arg,
5634                                      build_real (TREE_TYPE (arg), dconst0)));
5635
5636               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
5637               if (lang_hooks.decls.global_bindings_p () != 0
5638                   || CONTAINS_PLACEHOLDER_P (arg))
5639                 return NULL_TREE;
5640
5641               arg = save_expr (arg);
5642               return fold (build2 (TRUTH_ANDIF_EXPR, type,
5643                                    fold (build2 (GE_EXPR, type, arg,
5644                                                  build_real (TREE_TYPE (arg),
5645                                                              dconst0))),
5646                                    fold (build2 (NE_EXPR, type, arg,
5647                                                  build_real (TREE_TYPE (arg),
5648                                                              c2)))));
5649             }
5650
5651           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
5652           if (! HONOR_NANS (mode))
5653             return fold (build2 (code, type, arg,
5654                                  build_real (TREE_TYPE (arg), c2)));
5655
5656           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
5657           if (lang_hooks.decls.global_bindings_p () == 0
5658               && ! CONTAINS_PLACEHOLDER_P (arg))
5659             {
5660               arg = save_expr (arg);
5661               return fold (build2 (TRUTH_ANDIF_EXPR, type,
5662                                    fold (build2 (GE_EXPR, type, arg,
5663                                                  build_real (TREE_TYPE (arg),
5664                                                              dconst0))),
5665                                    fold (build2 (code, type, arg,
5666                                                  build_real (TREE_TYPE (arg),
5667                                                              c2)))));
5668             }
5669         }
5670     }
5671
5672   return NULL_TREE;
5673 }
5674
5675 /* Subroutine of fold() that optimizes comparisons against Infinities,
5676    either +Inf or -Inf.
5677
5678    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
5679    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
5680    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
5681
5682    The function returns the constant folded tree if a simplification
5683    can be made, and NULL_TREE otherwise.  */
5684
5685 static tree
5686 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
5687 {
5688   enum machine_mode mode;
5689   REAL_VALUE_TYPE max;
5690   tree temp;
5691   bool neg;
5692
5693   mode = TYPE_MODE (TREE_TYPE (arg0));
5694
5695   /* For negative infinity swap the sense of the comparison.  */
5696   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
5697   if (neg)
5698     code = swap_tree_comparison (code);
5699
5700   switch (code)
5701     {
5702     case GT_EXPR:
5703       /* x > +Inf is always false, if with ignore sNANs.  */
5704       if (HONOR_SNANS (mode))
5705         return NULL_TREE;
5706       return omit_one_operand (type, integer_zero_node, arg0);
5707
5708     case LE_EXPR:
5709       /* x <= +Inf is always true, if we don't case about NaNs.  */
5710       if (! HONOR_NANS (mode))
5711         return omit_one_operand (type, integer_one_node, arg0);
5712
5713       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
5714       if (lang_hooks.decls.global_bindings_p () == 0
5715           && ! CONTAINS_PLACEHOLDER_P (arg0))
5716         {
5717           arg0 = save_expr (arg0);
5718           return fold (build2 (EQ_EXPR, type, arg0, arg0));
5719         }
5720       break;
5721
5722     case EQ_EXPR:
5723     case GE_EXPR:
5724       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
5725       real_maxval (&max, neg, mode);
5726       return fold (build2 (neg ? LT_EXPR : GT_EXPR, type,
5727                            arg0, build_real (TREE_TYPE (arg0), max)));
5728
5729     case LT_EXPR:
5730       /* x < +Inf is always equal to x <= DBL_MAX.  */
5731       real_maxval (&max, neg, mode);
5732       return fold (build2 (neg ? GE_EXPR : LE_EXPR, type,
5733                            arg0, build_real (TREE_TYPE (arg0), max)));
5734
5735     case NE_EXPR:
5736       /* x != +Inf is always equal to !(x > DBL_MAX).  */
5737       real_maxval (&max, neg, mode);
5738       if (! HONOR_NANS (mode))
5739         return fold (build2 (neg ? GE_EXPR : LE_EXPR, type,
5740                              arg0, build_real (TREE_TYPE (arg0), max)));
5741
5742       /* The transformation below creates non-gimple code and thus is
5743          not appropriate if we are in gimple form.  */
5744       if (in_gimple_form)
5745         return NULL_TREE;
5746
5747       temp = fold (build2 (neg ? LT_EXPR : GT_EXPR, type,
5748                            arg0, build_real (TREE_TYPE (arg0), max)));
5749       return fold (build1 (TRUTH_NOT_EXPR, type, temp));
5750
5751     default:
5752       break;
5753     }
5754
5755   return NULL_TREE;
5756 }
5757
5758 /* Subroutine of fold() that optimizes comparisons of a division by
5759    a nonzero integer constant against an integer constant, i.e.
5760    X/C1 op C2.
5761
5762    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
5763    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
5764    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
5765
5766    The function returns the constant folded tree if a simplification
5767    can be made, and NULL_TREE otherwise.  */
5768
5769 static tree
5770 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
5771 {
5772   tree prod, tmp, hi, lo;
5773   tree arg00 = TREE_OPERAND (arg0, 0);
5774   tree arg01 = TREE_OPERAND (arg0, 1);
5775   unsigned HOST_WIDE_INT lpart;
5776   HOST_WIDE_INT hpart;
5777   int overflow;
5778
5779   /* We have to do this the hard way to detect unsigned overflow.
5780      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
5781   overflow = mul_double (TREE_INT_CST_LOW (arg01),
5782                          TREE_INT_CST_HIGH (arg01),
5783                          TREE_INT_CST_LOW (arg1),
5784                          TREE_INT_CST_HIGH (arg1), &lpart, &hpart);
5785   prod = build_int_cst_wide (TREE_TYPE (arg00), lpart, hpart);
5786   prod = force_fit_type (prod, -1, overflow, false);
5787
5788   if (TYPE_UNSIGNED (TREE_TYPE (arg0)))
5789     {
5790       tmp = int_const_binop (MINUS_EXPR, arg01, integer_one_node, 0);
5791       lo = prod;
5792
5793       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
5794       overflow = add_double (TREE_INT_CST_LOW (prod),
5795                              TREE_INT_CST_HIGH (prod),
5796                              TREE_INT_CST_LOW (tmp),
5797                              TREE_INT_CST_HIGH (tmp),
5798                              &lpart, &hpart);
5799       hi = build_int_cst_wide (TREE_TYPE (arg00), lpart, hpart);
5800       hi = force_fit_type (hi, -1, overflow | TREE_OVERFLOW (prod),
5801                            TREE_CONSTANT_OVERFLOW (prod));
5802     }
5803   else if (tree_int_cst_sgn (arg01) >= 0)
5804     {
5805       tmp = int_const_binop (MINUS_EXPR, arg01, integer_one_node, 0);
5806       switch (tree_int_cst_sgn (arg1))
5807         {
5808         case -1:
5809           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
5810           hi = prod;
5811           break;
5812
5813         case  0:
5814           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
5815           hi = tmp;
5816           break;
5817
5818         case  1:
5819           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
5820           lo = prod;
5821           break;
5822
5823         default:
5824           gcc_unreachable ();
5825         }
5826     }
5827   else
5828     {
5829       /* A negative divisor reverses the relational operators.  */
5830       code = swap_tree_comparison (code);
5831
5832       tmp = int_const_binop (PLUS_EXPR, arg01, integer_one_node, 0);
5833       switch (tree_int_cst_sgn (arg1))
5834         {
5835         case -1:
5836           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
5837           lo = prod;
5838           break;
5839
5840         case  0:
5841           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
5842           lo = tmp;
5843           break;
5844
5845         case  1:
5846           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
5847           hi = prod;
5848           break;
5849
5850         default:
5851           gcc_unreachable ();
5852         }
5853     }
5854
5855   switch (code)
5856     {
5857     case EQ_EXPR:
5858       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
5859         return omit_one_operand (type, integer_zero_node, arg00);
5860       if (TREE_OVERFLOW (hi))
5861         return fold (build2 (GE_EXPR, type, arg00, lo));
5862       if (TREE_OVERFLOW (lo))
5863         return fold (build2 (LE_EXPR, type, arg00, hi));
5864       return build_range_check (type, arg00, 1, lo, hi);
5865
5866     case NE_EXPR:
5867       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
5868         return omit_one_operand (type, integer_one_node, arg00);
5869       if (TREE_OVERFLOW (hi))
5870         return fold (build2 (LT_EXPR, type, arg00, lo));
5871       if (TREE_OVERFLOW (lo))
5872         return fold (build2 (GT_EXPR, type, arg00, hi));
5873       return build_range_check (type, arg00, 0, lo, hi);
5874
5875     case LT_EXPR:
5876       if (TREE_OVERFLOW (lo))
5877         return omit_one_operand (type, integer_zero_node, arg00);
5878       return fold (build2 (LT_EXPR, type, arg00, lo));
5879
5880     case LE_EXPR:
5881       if (TREE_OVERFLOW (hi))
5882         return omit_one_operand (type, integer_one_node, arg00);
5883       return fold (build2 (LE_EXPR, type, arg00, hi));
5884
5885     case GT_EXPR:
5886       if (TREE_OVERFLOW (hi))
5887         return omit_one_operand (type, integer_zero_node, arg00);
5888       return fold (build2 (GT_EXPR, type, arg00, hi));
5889
5890     case GE_EXPR:
5891       if (TREE_OVERFLOW (lo))
5892         return omit_one_operand (type, integer_one_node, arg00);
5893       return fold (build2 (GE_EXPR, type, arg00, lo));
5894
5895     default:
5896       break;
5897     }
5898
5899   return NULL_TREE;
5900 }
5901
5902
5903 /* If CODE with arguments ARG0 and ARG1 represents a single bit
5904    equality/inequality test, then return a simplified form of
5905    the test using shifts and logical operations.  Otherwise return
5906    NULL.  TYPE is the desired result type.  */
5907
5908 tree
5909 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
5910                       tree result_type)
5911 {
5912   /* If this is testing a single bit, we can optimize the test.  */
5913   if ((code == NE_EXPR || code == EQ_EXPR)
5914       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
5915       && integer_pow2p (TREE_OPERAND (arg0, 1)))
5916     {
5917       tree inner = TREE_OPERAND (arg0, 0);
5918       tree type = TREE_TYPE (arg0);
5919       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
5920       enum machine_mode operand_mode = TYPE_MODE (type);
5921       int ops_unsigned;
5922       tree signed_type, unsigned_type, intermediate_type;
5923       tree arg00;
5924
5925       /* If we have (A & C) != 0 where C is the sign bit of A, convert
5926          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
5927       arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
5928       if (arg00 != NULL_TREE
5929           /* This is only a win if casting to a signed type is cheap,
5930              i.e. when arg00's type is not a partial mode.  */
5931           && TYPE_PRECISION (TREE_TYPE (arg00))
5932              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
5933         {
5934           tree stype = lang_hooks.types.signed_type (TREE_TYPE (arg00));
5935           return fold (build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
5936                                result_type, fold_convert (stype, arg00),
5937                                fold_convert (stype, integer_zero_node)));
5938         }
5939
5940       /* Otherwise we have (A & C) != 0 where C is a single bit,
5941          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
5942          Similarly for (A & C) == 0.  */
5943
5944       /* If INNER is a right shift of a constant and it plus BITNUM does
5945          not overflow, adjust BITNUM and INNER.  */
5946       if (TREE_CODE (inner) == RSHIFT_EXPR
5947           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
5948           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
5949           && bitnum < TYPE_PRECISION (type)
5950           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
5951                                    bitnum - TYPE_PRECISION (type)))
5952         {
5953           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
5954           inner = TREE_OPERAND (inner, 0);
5955         }
5956
5957       /* If we are going to be able to omit the AND below, we must do our
5958          operations as unsigned.  If we must use the AND, we have a choice.
5959          Normally unsigned is faster, but for some machines signed is.  */
5960 #ifdef LOAD_EXTEND_OP
5961       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND 
5962                       && !flag_syntax_only) ? 0 : 1;
5963 #else
5964       ops_unsigned = 1;
5965 #endif
5966
5967       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
5968       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
5969       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
5970       inner = fold_convert (intermediate_type, inner);
5971
5972       if (bitnum != 0)
5973         inner = build2 (RSHIFT_EXPR, intermediate_type,
5974                         inner, size_int (bitnum));
5975
5976       if (code == EQ_EXPR)
5977         inner = fold (build2 (BIT_XOR_EXPR, intermediate_type,
5978                               inner, integer_one_node));
5979
5980       /* Put the AND last so it can combine with more things.  */
5981       inner = build2 (BIT_AND_EXPR, intermediate_type,
5982                       inner, integer_one_node);
5983
5984       /* Make sure to return the proper type.  */
5985       inner = fold_convert (result_type, inner);
5986
5987       return inner;
5988     }
5989   return NULL_TREE;
5990 }
5991
5992 /* Check whether we are allowed to reorder operands arg0 and arg1,
5993    such that the evaluation of arg1 occurs before arg0.  */
5994
5995 static bool
5996 reorder_operands_p (tree arg0, tree arg1)
5997 {
5998   if (! flag_evaluation_order)
5999       return true;
6000   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6001     return true;
6002   return ! TREE_SIDE_EFFECTS (arg0)
6003          && ! TREE_SIDE_EFFECTS (arg1);
6004 }
6005
6006 /* Test whether it is preferable two swap two operands, ARG0 and
6007    ARG1, for example because ARG0 is an integer constant and ARG1
6008    isn't.  If REORDER is true, only recommend swapping if we can
6009    evaluate the operands in reverse order.  */
6010
6011 bool
6012 tree_swap_operands_p (tree arg0, tree arg1, bool reorder)
6013 {
6014   STRIP_SIGN_NOPS (arg0);
6015   STRIP_SIGN_NOPS (arg1);
6016
6017   if (TREE_CODE (arg1) == INTEGER_CST)
6018     return 0;
6019   if (TREE_CODE (arg0) == INTEGER_CST)
6020     return 1;
6021
6022   if (TREE_CODE (arg1) == REAL_CST)
6023     return 0;
6024   if (TREE_CODE (arg0) == REAL_CST)
6025     return 1;
6026
6027   if (TREE_CODE (arg1) == COMPLEX_CST)
6028     return 0;
6029   if (TREE_CODE (arg0) == COMPLEX_CST)
6030     return 1;
6031
6032   if (TREE_CONSTANT (arg1))
6033     return 0;
6034   if (TREE_CONSTANT (arg0))
6035     return 1;
6036
6037   if (optimize_size)
6038     return 0;
6039
6040   if (reorder && flag_evaluation_order
6041       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6042     return 0;
6043
6044   if (DECL_P (arg1))
6045     return 0;
6046   if (DECL_P (arg0))
6047     return 1;
6048
6049   /* It is preferable to swap two SSA_NAME to ensure a canonical form
6050      for commutative and comparison operators.  Ensuring a canonical
6051      form allows the optimizers to find additional redundancies without
6052      having to explicitly check for both orderings.  */
6053   if (TREE_CODE (arg0) == SSA_NAME
6054       && TREE_CODE (arg1) == SSA_NAME
6055       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6056     return 1;
6057
6058   return 0;
6059 }
6060
6061 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6062    ARG0 is extended to a wider type.  */
6063
6064 static tree
6065 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
6066 {
6067   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6068   tree arg1_unw;
6069   tree shorter_type, outer_type;
6070   tree min, max;
6071   bool above, below;
6072
6073   if (arg0_unw == arg0)
6074     return NULL_TREE;
6075   shorter_type = TREE_TYPE (arg0_unw);
6076
6077   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6078     return NULL_TREE;
6079
6080   arg1_unw = get_unwidened (arg1, shorter_type);
6081   if (!arg1_unw)
6082     return NULL_TREE;
6083
6084   /* If possible, express the comparison in the shorter mode.  */
6085   if ((code == EQ_EXPR || code == NE_EXPR
6086        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6087       && (TREE_TYPE (arg1_unw) == shorter_type
6088           || (TREE_CODE (arg1_unw) == INTEGER_CST
6089               && TREE_CODE (shorter_type) == INTEGER_TYPE
6090               && int_fits_type_p (arg1_unw, shorter_type))))
6091     return fold (build (code, type, arg0_unw,
6092                         fold_convert (shorter_type, arg1_unw)));
6093
6094   if (TREE_CODE (arg1_unw) != INTEGER_CST)
6095     return NULL_TREE;
6096
6097   /* If we are comparing with the integer that does not fit into the range
6098      of the shorter type, the result is known.  */
6099   outer_type = TREE_TYPE (arg1_unw);
6100   min = lower_bound_in_type (outer_type, shorter_type);
6101   max = upper_bound_in_type (outer_type, shorter_type);
6102
6103   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6104                                                    max, arg1_unw));
6105   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6106                                                    arg1_unw, min));
6107
6108   switch (code)
6109     {
6110     case EQ_EXPR:
6111       if (above || below)
6112         return omit_one_operand (type, integer_zero_node, arg0);
6113       break;
6114
6115     case NE_EXPR:
6116       if (above || below)
6117         return omit_one_operand (type, integer_one_node, arg0);
6118       break;
6119
6120     case LT_EXPR:
6121     case LE_EXPR:
6122       if (above)
6123         return omit_one_operand (type, integer_one_node, arg0);
6124       else if (below)
6125         return omit_one_operand (type, integer_zero_node, arg0);
6126
6127     case GT_EXPR:
6128     case GE_EXPR:
6129       if (above)
6130         return omit_one_operand (type, integer_zero_node, arg0);
6131       else if (below)
6132         return omit_one_operand (type, integer_one_node, arg0);
6133
6134     default:
6135       break;
6136     }
6137
6138   return NULL_TREE;
6139 }
6140
6141 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6142    ARG0 just the signedness is changed.  */
6143
6144 static tree
6145 fold_sign_changed_comparison (enum tree_code code, tree type,
6146                               tree arg0, tree arg1)
6147 {
6148   tree arg0_inner, tmp;
6149   tree inner_type, outer_type;
6150
6151   if (TREE_CODE (arg0) != NOP_EXPR)
6152     return NULL_TREE;
6153
6154   outer_type = TREE_TYPE (arg0);
6155   arg0_inner = TREE_OPERAND (arg0, 0);
6156   inner_type = TREE_TYPE (arg0_inner);
6157
6158   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6159     return NULL_TREE;
6160
6161   if (TREE_CODE (arg1) != INTEGER_CST
6162       && !(TREE_CODE (arg1) == NOP_EXPR
6163            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6164     return NULL_TREE;
6165
6166   if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6167       && code != NE_EXPR
6168       && code != EQ_EXPR)
6169     return NULL_TREE;
6170
6171   if (TREE_CODE (arg1) == INTEGER_CST)
6172     {
6173       tmp = build_int_cst_wide (inner_type,
6174                                 TREE_INT_CST_LOW (arg1),
6175                                 TREE_INT_CST_HIGH (arg1));
6176       arg1 = force_fit_type (tmp, 0,
6177                              TREE_OVERFLOW (arg1),
6178                              TREE_CONSTANT_OVERFLOW (arg1));
6179     }
6180   else
6181     arg1 = fold_convert (inner_type, arg1);
6182
6183   return fold (build (code, type, arg0_inner, arg1));
6184 }
6185
6186 /* Tries to replace &a[idx] CODE s * delta with &a[idx CODE delta], if s is
6187    step of the array.  ADDR is the address. MULT is the multiplicative expression.
6188    If the function succeeds, the new address expression is returned.  Otherwise
6189    NULL_TREE is returned.  */
6190
6191 static tree
6192 try_move_mult_to_index (enum tree_code code, tree addr, tree mult)
6193 {
6194   tree s, delta, step;
6195   tree arg0 = TREE_OPERAND (mult, 0), arg1 = TREE_OPERAND (mult, 1);
6196   tree ref = TREE_OPERAND (addr, 0), pref;
6197   tree ret, pos;
6198   tree itype;
6199
6200   STRIP_NOPS (arg0);
6201   STRIP_NOPS (arg1);
6202   
6203   if (TREE_CODE (arg0) == INTEGER_CST)
6204     {
6205       s = arg0;
6206       delta = arg1;
6207     }
6208   else if (TREE_CODE (arg1) == INTEGER_CST)
6209     {
6210       s = arg1;
6211       delta = arg0;
6212     }
6213   else
6214     return NULL_TREE;
6215
6216   for (;; ref = TREE_OPERAND (ref, 0))
6217     {
6218       if (TREE_CODE (ref) == ARRAY_REF)
6219         {
6220           step = array_ref_element_size (ref);
6221
6222           if (TREE_CODE (step) != INTEGER_CST)
6223             continue;
6224
6225           itype = TREE_TYPE (step);
6226
6227           /* If the type sizes do not match, we might run into problems
6228              when one of them would overflow.  */
6229           if (TYPE_PRECISION (itype) != TYPE_PRECISION (TREE_TYPE (s)))
6230             continue;
6231
6232           if (!operand_equal_p (step, fold_convert (itype, s), 0))
6233             continue;
6234
6235           delta = fold_convert (itype, delta);
6236           break;
6237         }
6238
6239       if (!handled_component_p (ref))
6240         return NULL_TREE;
6241     }
6242
6243   /* We found the suitable array reference.  So copy everything up to it,
6244      and replace the index.  */
6245
6246   pref = TREE_OPERAND (addr, 0);
6247   ret = copy_node (pref);
6248   pos = ret;
6249
6250   while (pref != ref)
6251     {
6252       pref = TREE_OPERAND (pref, 0);
6253       TREE_OPERAND (pos, 0) = copy_node (pref);
6254       pos = TREE_OPERAND (pos, 0);
6255     }
6256
6257   TREE_OPERAND (pos, 1) = fold (build2 (code, itype,
6258                                         TREE_OPERAND (pos, 1),
6259                                         delta));
6260
6261   return build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
6262 }
6263
6264
6265 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
6266    means A >= Y && A != MAX, but in this case we know that
6267    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
6268
6269 static tree
6270 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
6271 {
6272   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6273
6274   if (TREE_CODE (bound) == LT_EXPR)
6275     a = TREE_OPERAND (bound, 0);
6276   else if (TREE_CODE (bound) == GT_EXPR)
6277     a = TREE_OPERAND (bound, 1);
6278   else
6279     return NULL_TREE;
6280
6281   typea = TREE_TYPE (a);
6282   if (!INTEGRAL_TYPE_P (typea)
6283       && !POINTER_TYPE_P (typea))
6284     return NULL_TREE;
6285
6286   if (TREE_CODE (ineq) == LT_EXPR)
6287     {
6288       a1 = TREE_OPERAND (ineq, 1);
6289       y = TREE_OPERAND (ineq, 0);
6290     }
6291   else if (TREE_CODE (ineq) == GT_EXPR)
6292     {
6293       a1 = TREE_OPERAND (ineq, 0);
6294       y = TREE_OPERAND (ineq, 1);
6295     }
6296   else
6297     return NULL_TREE;
6298
6299   if (TREE_TYPE (a1) != typea)
6300     return NULL_TREE;
6301
6302   diff = fold (build2 (MINUS_EXPR, typea, a1, a));
6303   if (!integer_onep (diff))
6304     return NULL_TREE;
6305
6306   return fold (build2 (GE_EXPR, type, a, y));
6307 }
6308
6309 /* Fold complex addition when both components are accessible by parts.
6310    Return non-null if successful.  CODE should be PLUS_EXPR for addition,
6311    or MINUS_EXPR for subtraction.  */
6312
6313 static tree
6314 fold_complex_add (tree type, tree ac, tree bc, enum tree_code code)
6315 {
6316   tree ar, ai, br, bi, rr, ri, inner_type;
6317
6318   if (TREE_CODE (ac) == COMPLEX_EXPR)
6319     ar = TREE_OPERAND (ac, 0), ai = TREE_OPERAND (ac, 1);
6320   else if (TREE_CODE (ac) == COMPLEX_CST)
6321     ar = TREE_REALPART (ac), ai = TREE_IMAGPART (ac);
6322   else
6323     return NULL;
6324
6325   if (TREE_CODE (bc) == COMPLEX_EXPR)
6326     br = TREE_OPERAND (bc, 0), bi = TREE_OPERAND (bc, 1);
6327   else if (TREE_CODE (bc) == COMPLEX_CST)
6328     br = TREE_REALPART (bc), bi = TREE_IMAGPART (bc);
6329   else
6330     return NULL;
6331
6332   inner_type = TREE_TYPE (type);
6333
6334   rr = fold (build2 (code, inner_type, ar, br));  
6335   ri = fold (build2 (code, inner_type, ai, bi));  
6336
6337   return fold (build2 (COMPLEX_EXPR, type, rr, ri));
6338 }
6339
6340 /* Perform some simplifications of complex multiplication when one or more
6341    of the components are constants or zeros.  Return non-null if successful.  */
6342
6343 static tree
6344 fold_complex_mult (tree type, tree ac, tree bc)
6345 {
6346   tree ar, ai, br, bi, rr, ri, inner_type, zero;
6347   bool ar0, ai0, br0, bi0, bi1;
6348
6349   if (TREE_CODE (ac) == COMPLEX_EXPR)
6350     ar = TREE_OPERAND (ac, 0), ai = TREE_OPERAND (ac, 1);
6351   else if (TREE_CODE (ac) == COMPLEX_CST)
6352     ar = TREE_REALPART (ac), ai = TREE_IMAGPART (ac);
6353   else
6354     return NULL;
6355
6356   if (TREE_CODE (bc) == COMPLEX_EXPR)
6357     br = TREE_OPERAND (bc, 0), bi = TREE_OPERAND (bc, 1);
6358   else if (TREE_CODE (bc) == COMPLEX_CST)
6359     br = TREE_REALPART (bc), bi = TREE_IMAGPART (bc);
6360   else
6361     return NULL;
6362
6363   inner_type = TREE_TYPE (type);
6364   zero = NULL;
6365
6366   if (SCALAR_FLOAT_TYPE_P (inner_type))
6367     {
6368       ar0 = ai0 = br0 = bi0 = bi1 = false;
6369
6370       /* We're only interested in +0.0 here, thus we don't use real_zerop.  */
6371
6372       if (TREE_CODE (ar) == REAL_CST
6373           && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ar), dconst0))
6374         ar0 = true, zero = ar;
6375
6376       if (TREE_CODE (ai) == REAL_CST
6377           && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst0))
6378         ai0 = true, zero = ai;
6379
6380       if (TREE_CODE (br) == REAL_CST
6381           && REAL_VALUES_IDENTICAL (TREE_REAL_CST (br), dconst0))
6382         br0 = true, zero = br;
6383
6384       if (TREE_CODE (bi) == REAL_CST)
6385         {
6386           if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (bi), dconst0))
6387             bi0 = true, zero = bi;
6388           else if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (bi), dconst1))
6389             bi1 = true;
6390         }
6391     }
6392   else
6393     {
6394       ar0 = integer_zerop (ar);
6395       if (ar0)
6396         zero = ar;
6397       ai0 = integer_zerop (ai);
6398       if (ai0)
6399         zero = ai;
6400       br0 = integer_zerop (br);
6401       if (br0)
6402         zero = br;
6403       bi0 = integer_zerop (bi);
6404       if (bi0)
6405         {
6406           zero = bi;
6407           bi1 = false;
6408         }
6409       else
6410         bi1 = integer_onep (bi);
6411     }
6412
6413   /* We won't optimize anything below unless something is zero.  */
6414   if (zero == NULL)
6415     return NULL;
6416
6417   if (ai0 && br0 && bi1)
6418     {
6419       rr = zero;
6420       ri = ar;
6421     }
6422   else if (ai0 && bi0)
6423     {
6424       rr = fold (build2 (MULT_EXPR, inner_type, ar, br));
6425       ri = zero;
6426     }
6427   else if (ai0 && br0)
6428     {
6429       rr = zero;
6430       ri = fold (build2 (MULT_EXPR, inner_type, ar, bi));
6431     }
6432   else if (ar0 && bi0)
6433     {
6434       rr = zero;
6435       ri = fold (build2 (MULT_EXPR, inner_type, ai, br));
6436     }
6437   else if (ar0 && br0)
6438     {
6439       rr = fold (build2 (MULT_EXPR, inner_type, ai, br));
6440       rr = fold (build1 (NEGATE_EXPR, inner_type, rr));
6441       ri = zero;
6442     }
6443   else if (bi0)
6444     {
6445       rr = fold (build2 (MULT_EXPR, inner_type, ar, br));
6446       ri = fold (build2 (MULT_EXPR, inner_type, ai, br));
6447     }
6448   else if (ai0)
6449     {
6450       rr = fold (build2 (MULT_EXPR, inner_type, ar, br));
6451       ri = fold (build2 (MULT_EXPR, inner_type, ar, bi));
6452     }
6453   else if (br0)
6454     {
6455       rr = fold (build2 (MULT_EXPR, inner_type, ai, bi));
6456       rr = fold (build1 (NEGATE_EXPR, inner_type, rr));
6457       ri = fold (build2 (MULT_EXPR, inner_type, ar, bi));
6458     }
6459   else if (ar0)
6460     {
6461       rr = fold (build2 (MULT_EXPR, inner_type, ai, bi));
6462       rr = fold (build1 (NEGATE_EXPR, inner_type, rr));
6463       ri = fold (build2 (MULT_EXPR, inner_type, ai, br));
6464     }
6465   else
6466     return NULL;
6467
6468   return fold (build2 (COMPLEX_EXPR, type, rr, ri));
6469 }
6470
6471 /* Perform constant folding and related simplification of EXPR.
6472    The related simplifications include x*1 => x, x*0 => 0, etc.,
6473    and application of the associative law.
6474    NOP_EXPR conversions may be removed freely (as long as we
6475    are careful not to change the type of the overall expression).
6476    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
6477    but we can constant-fold them if they have constant operands.  */
6478
6479 #ifdef ENABLE_FOLD_CHECKING
6480 # define fold(x) fold_1 (x)
6481 static tree fold_1 (tree);
6482 static
6483 #endif
6484 tree
6485 fold (tree expr)
6486 {
6487   const tree t = expr;
6488   const tree type = TREE_TYPE (expr);
6489   tree t1 = NULL_TREE;
6490   tree tem;
6491   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
6492   enum tree_code code = TREE_CODE (t);
6493   enum tree_code_class kind = TREE_CODE_CLASS (code);
6494
6495   /* WINS will be nonzero when the switch is done
6496      if all operands are constant.  */
6497   int wins = 1;
6498
6499   /* Return right away if a constant.  */
6500   if (kind == tcc_constant)
6501     return t;
6502
6503   if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
6504     {
6505       tree subop;
6506
6507       /* Special case for conversion ops that can have fixed point args.  */
6508       arg0 = TREE_OPERAND (t, 0);
6509
6510       /* Don't use STRIP_NOPS, because signedness of argument type matters.  */
6511       if (arg0 != 0)
6512         STRIP_SIGN_NOPS (arg0);
6513
6514       if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
6515         subop = TREE_REALPART (arg0);
6516       else
6517         subop = arg0;
6518
6519       if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
6520           && TREE_CODE (subop) != REAL_CST)
6521         /* Note that TREE_CONSTANT isn't enough:
6522            static var addresses are constant but we can't
6523            do arithmetic on them.  */
6524         wins = 0;
6525     }
6526   else if (IS_EXPR_CODE_CLASS (kind))
6527     {
6528       int len = TREE_CODE_LENGTH (code);
6529       int i;
6530       for (i = 0; i < len; i++)
6531         {
6532           tree op = TREE_OPERAND (t, i);
6533           tree subop;
6534
6535           if (op == 0)
6536             continue;           /* Valid for CALL_EXPR, at least.  */
6537
6538           /* Strip any conversions that don't change the mode.  This is
6539              safe for every expression, except for a comparison expression
6540              because its signedness is derived from its operands.  So, in
6541              the latter case, only strip conversions that don't change the
6542              signedness.
6543
6544              Note that this is done as an internal manipulation within the
6545              constant folder, in order to find the simplest representation
6546              of the arguments so that their form can be studied.  In any
6547              cases, the appropriate type conversions should be put back in
6548              the tree that will get out of the constant folder.  */
6549           if (kind == tcc_comparison)
6550             STRIP_SIGN_NOPS (op);
6551           else
6552             STRIP_NOPS (op);
6553
6554           if (TREE_CODE (op) == COMPLEX_CST)
6555             subop = TREE_REALPART (op);
6556           else
6557             subop = op;
6558
6559           if (TREE_CODE (subop) != INTEGER_CST
6560               && TREE_CODE (subop) != REAL_CST)
6561             /* Note that TREE_CONSTANT isn't enough:
6562                static var addresses are constant but we can't
6563                do arithmetic on them.  */
6564             wins = 0;
6565
6566           if (i == 0)
6567             arg0 = op;
6568           else if (i == 1)
6569             arg1 = op;
6570         }
6571     }
6572
6573   /* If this is a commutative operation, and ARG0 is a constant, move it
6574      to ARG1 to reduce the number of tests below.  */
6575   if (commutative_tree_code (code)
6576       && tree_swap_operands_p (arg0, arg1, true))
6577     return fold (build2 (code, type, TREE_OPERAND (t, 1),
6578                          TREE_OPERAND (t, 0)));
6579
6580   /* Now WINS is set as described above,
6581      ARG0 is the first operand of EXPR,
6582      and ARG1 is the second operand (if it has more than one operand).
6583
6584      First check for cases where an arithmetic operation is applied to a
6585      compound, conditional, or comparison operation.  Push the arithmetic
6586      operation inside the compound or conditional to see if any folding
6587      can then be done.  Convert comparison to conditional for this purpose.
6588      The also optimizes non-constant cases that used to be done in
6589      expand_expr.
6590
6591      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
6592      one of the operands is a comparison and the other is a comparison, a
6593      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
6594      code below would make the expression more complex.  Change it to a
6595      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
6596      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
6597
6598   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
6599        || code == EQ_EXPR || code == NE_EXPR)
6600       && ((truth_value_p (TREE_CODE (arg0))
6601            && (truth_value_p (TREE_CODE (arg1))
6602                || (TREE_CODE (arg1) == BIT_AND_EXPR
6603                    && integer_onep (TREE_OPERAND (arg1, 1)))))
6604           || (truth_value_p (TREE_CODE (arg1))
6605               && (truth_value_p (TREE_CODE (arg0))
6606                   || (TREE_CODE (arg0) == BIT_AND_EXPR
6607                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
6608     {
6609       tem = fold (build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
6610                           : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
6611                           : TRUTH_XOR_EXPR,
6612                           type, fold_convert (boolean_type_node, arg0),
6613                           fold_convert (boolean_type_node, arg1)));
6614
6615       if (code == EQ_EXPR)
6616         tem = invert_truthvalue (tem);
6617
6618       return tem;
6619     }
6620
6621   if (TREE_CODE_CLASS (code) == tcc_unary)
6622     {
6623       if (TREE_CODE (arg0) == COMPOUND_EXPR)
6624         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
6625                        fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
6626       else if (TREE_CODE (arg0) == COND_EXPR)
6627         {
6628           tree arg01 = TREE_OPERAND (arg0, 1);
6629           tree arg02 = TREE_OPERAND (arg0, 2);
6630           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
6631             arg01 = fold (build1 (code, type, arg01));
6632           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
6633             arg02 = fold (build1 (code, type, arg02));
6634           tem = fold (build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
6635                               arg01, arg02));
6636
6637           /* If this was a conversion, and all we did was to move into
6638              inside the COND_EXPR, bring it back out.  But leave it if
6639              it is a conversion from integer to integer and the
6640              result precision is no wider than a word since such a
6641              conversion is cheap and may be optimized away by combine,
6642              while it couldn't if it were outside the COND_EXPR.  Then return
6643              so we don't get into an infinite recursion loop taking the
6644              conversion out and then back in.  */
6645
6646           if ((code == NOP_EXPR || code == CONVERT_EXPR
6647                || code == NON_LVALUE_EXPR)
6648               && TREE_CODE (tem) == COND_EXPR
6649               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
6650               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
6651               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
6652               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
6653               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
6654                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
6655               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
6656                      && (INTEGRAL_TYPE_P
6657                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
6658                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
6659                   || flag_syntax_only))
6660             tem = build1 (code, type,
6661                           build3 (COND_EXPR,
6662                                   TREE_TYPE (TREE_OPERAND
6663                                              (TREE_OPERAND (tem, 1), 0)),
6664                                   TREE_OPERAND (tem, 0),
6665                                   TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
6666                                   TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
6667           return tem;
6668         }
6669       else if (COMPARISON_CLASS_P (arg0))
6670         {
6671           if (TREE_CODE (type) == BOOLEAN_TYPE)
6672             {
6673               arg0 = copy_node (arg0);
6674               TREE_TYPE (arg0) = type;
6675               return arg0;
6676             }
6677           else if (TREE_CODE (type) != INTEGER_TYPE)
6678             return fold (build3 (COND_EXPR, type, arg0,
6679                                  fold (build1 (code, type,
6680                                                integer_one_node)),
6681                                  fold (build1 (code, type,
6682                                                integer_zero_node))));
6683         }
6684    }
6685   else if (TREE_CODE_CLASS (code) == tcc_comparison
6686            && TREE_CODE (arg0) == COMPOUND_EXPR)
6687     return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
6688                    fold (build2 (code, type, TREE_OPERAND (arg0, 1), arg1)));
6689   else if (TREE_CODE_CLASS (code) == tcc_comparison
6690            && TREE_CODE (arg1) == COMPOUND_EXPR)
6691     return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
6692                    fold (build2 (code, type, arg0, TREE_OPERAND (arg1, 1))));
6693   else if (TREE_CODE_CLASS (code) == tcc_binary
6694            || TREE_CODE_CLASS (code) == tcc_comparison)
6695     {
6696       if (TREE_CODE (arg0) == COMPOUND_EXPR)
6697         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
6698                        fold (build2 (code, type, TREE_OPERAND (arg0, 1),
6699                                      arg1)));
6700       if (TREE_CODE (arg1) == COMPOUND_EXPR
6701           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
6702         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
6703                        fold (build2 (code, type,
6704                                      arg0, TREE_OPERAND (arg1, 1))));
6705
6706       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
6707         {
6708           tem = fold_binary_op_with_conditional_arg (t, code, arg0, arg1, 
6709                                                      /*cond_first_p=*/1);
6710           if (tem != NULL_TREE)
6711             return tem;
6712         }
6713
6714       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
6715         {
6716           tem = fold_binary_op_with_conditional_arg (t, code, arg1, arg0, 
6717                                                      /*cond_first_p=*/0);
6718           if (tem != NULL_TREE)
6719             return tem;
6720         }
6721     }
6722
6723   switch (code)
6724     {
6725     case CONST_DECL:
6726       return fold (DECL_INITIAL (t));
6727
6728     case NOP_EXPR:
6729     case FLOAT_EXPR:
6730     case CONVERT_EXPR:
6731     case FIX_TRUNC_EXPR:
6732     case FIX_CEIL_EXPR:
6733     case FIX_FLOOR_EXPR:
6734     case FIX_ROUND_EXPR:
6735       if (TREE_TYPE (TREE_OPERAND (t, 0)) == type)
6736         return TREE_OPERAND (t, 0);
6737
6738       /* Handle cases of two conversions in a row.  */
6739       if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
6740           || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
6741         {
6742           tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
6743           tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
6744           int inside_int = INTEGRAL_TYPE_P (inside_type);
6745           int inside_ptr = POINTER_TYPE_P (inside_type);
6746           int inside_float = FLOAT_TYPE_P (inside_type);
6747           unsigned int inside_prec = TYPE_PRECISION (inside_type);
6748           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
6749           int inter_int = INTEGRAL_TYPE_P (inter_type);
6750           int inter_ptr = POINTER_TYPE_P (inter_type);
6751           int inter_float = FLOAT_TYPE_P (inter_type);
6752           unsigned int inter_prec = TYPE_PRECISION (inter_type);
6753           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
6754           int final_int = INTEGRAL_TYPE_P (type);
6755           int final_ptr = POINTER_TYPE_P (type);
6756           int final_float = FLOAT_TYPE_P (type);
6757           unsigned int final_prec = TYPE_PRECISION (type);
6758           int final_unsignedp = TYPE_UNSIGNED (type);
6759
6760           /* In addition to the cases of two conversions in a row
6761              handled below, if we are converting something to its own
6762              type via an object of identical or wider precision, neither
6763              conversion is needed.  */
6764           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
6765               && ((inter_int && final_int) || (inter_float && final_float))
6766               && inter_prec >= final_prec)
6767             return fold (build1 (code, type,
6768                                  TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
6769
6770           /* Likewise, if the intermediate and final types are either both
6771              float or both integer, we don't need the middle conversion if
6772              it is wider than the final type and doesn't change the signedness
6773              (for integers).  Avoid this if the final type is a pointer
6774              since then we sometimes need the inner conversion.  Likewise if
6775              the outer has a precision not equal to the size of its mode.  */
6776           if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
6777                || (inter_float && inside_float))
6778               && inter_prec >= inside_prec
6779               && (inter_float || inter_unsignedp == inside_unsignedp)
6780               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
6781                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
6782               && ! final_ptr)
6783             return fold (build1 (code, type,
6784                                  TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
6785
6786           /* If we have a sign-extension of a zero-extended value, we can
6787              replace that by a single zero-extension.  */
6788           if (inside_int && inter_int && final_int
6789               && inside_prec < inter_prec && inter_prec < final_prec
6790               && inside_unsignedp && !inter_unsignedp)
6791             return fold (build1 (code, type,
6792                                  TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
6793
6794           /* Two conversions in a row are not needed unless:
6795              - some conversion is floating-point (overstrict for now), or
6796              - the intermediate type is narrower than both initial and
6797                final, or
6798              - the intermediate type and innermost type differ in signedness,
6799                and the outermost type is wider than the intermediate, or
6800              - the initial type is a pointer type and the precisions of the
6801                intermediate and final types differ, or
6802              - the final type is a pointer type and the precisions of the
6803                initial and intermediate types differ.  */
6804           if (! inside_float && ! inter_float && ! final_float
6805               && (inter_prec > inside_prec || inter_prec > final_prec)
6806               && ! (inside_int && inter_int
6807                     && inter_unsignedp != inside_unsignedp
6808                     && inter_prec < final_prec)
6809               && ((inter_unsignedp && inter_prec > inside_prec)
6810                   == (final_unsignedp && final_prec > inter_prec))
6811               && ! (inside_ptr && inter_prec != final_prec)
6812               && ! (final_ptr && inside_prec != inter_prec)
6813               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
6814                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
6815               && ! final_ptr)
6816             return fold (build1 (code, type,
6817                                  TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
6818         }
6819
6820       if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
6821           && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
6822           /* Detect assigning a bitfield.  */
6823           && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
6824                && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
6825         {
6826           /* Don't leave an assignment inside a conversion
6827              unless assigning a bitfield.  */
6828           tree prev = TREE_OPERAND (t, 0);
6829           tem = copy_node (t);
6830           TREE_OPERAND (tem, 0) = TREE_OPERAND (prev, 1);
6831           /* First do the assignment, then return converted constant.  */
6832           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), prev, fold (tem));
6833           TREE_NO_WARNING (tem) = 1;
6834           TREE_USED (tem) = 1;
6835           return tem;
6836         }
6837
6838       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
6839          constants (if x has signed type, the sign bit cannot be set
6840          in c).  This folds extension into the BIT_AND_EXPR.  */
6841       if (INTEGRAL_TYPE_P (type)
6842           && TREE_CODE (type) != BOOLEAN_TYPE
6843           && TREE_CODE (TREE_OPERAND (t, 0)) == BIT_AND_EXPR
6844           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST)
6845         {
6846           tree and = TREE_OPERAND (t, 0);
6847           tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
6848           int change = 0;
6849
6850           if (TYPE_UNSIGNED (TREE_TYPE (and))
6851               || (TYPE_PRECISION (type)
6852                   <= TYPE_PRECISION (TREE_TYPE (and))))
6853             change = 1;
6854           else if (TYPE_PRECISION (TREE_TYPE (and1))
6855                    <= HOST_BITS_PER_WIDE_INT
6856                    && host_integerp (and1, 1))
6857             {
6858               unsigned HOST_WIDE_INT cst;
6859
6860               cst = tree_low_cst (and1, 1);
6861               cst &= (HOST_WIDE_INT) -1
6862                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
6863               change = (cst == 0);
6864 #ifdef LOAD_EXTEND_OP
6865               if (change
6866                   && !flag_syntax_only
6867                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
6868                       == ZERO_EXTEND))
6869                 {
6870                   tree uns = lang_hooks.types.unsigned_type (TREE_TYPE (and0));
6871                   and0 = fold_convert (uns, and0);
6872                   and1 = fold_convert (uns, and1);
6873                 }
6874 #endif
6875             }
6876           if (change)
6877             return fold (build2 (BIT_AND_EXPR, type,
6878                                  fold_convert (type, and0),
6879                                  fold_convert (type, and1)));
6880         }
6881
6882       /* Convert (T1)((T2)X op Y) into (T1)X op Y, for pointer types T1 and
6883          T2 being pointers to types of the same size.  */
6884       if (POINTER_TYPE_P (TREE_TYPE (t))
6885           && BINARY_CLASS_P (arg0)
6886           && TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
6887           && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0))))
6888         {
6889           tree arg00 = TREE_OPERAND (arg0, 0);
6890           tree t0 = TREE_TYPE (t);
6891           tree t1 = TREE_TYPE (arg00);
6892           tree tt0 = TREE_TYPE (t0);
6893           tree tt1 = TREE_TYPE (t1);
6894           tree s0 = TYPE_SIZE (tt0);
6895           tree s1 = TYPE_SIZE (tt1);
6896
6897           if (s0 && s1 && operand_equal_p (s0, s1, OEP_ONLY_CONST))
6898             return build2 (TREE_CODE (arg0), t0, fold_convert (t0, arg00),
6899                            TREE_OPERAND (arg0, 1));
6900         }
6901
6902       tem = fold_convert_const (code, type, arg0);
6903       return tem ? tem : t;
6904
6905     case VIEW_CONVERT_EXPR:
6906       if (TREE_CODE (TREE_OPERAND (t, 0)) == VIEW_CONVERT_EXPR)
6907         return build1 (VIEW_CONVERT_EXPR, type,
6908                        TREE_OPERAND (TREE_OPERAND (t, 0), 0));
6909       return t;
6910
6911     case COMPONENT_REF:
6912       if (TREE_CODE (arg0) == CONSTRUCTOR
6913           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
6914         {
6915           tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
6916           if (m)
6917             return TREE_VALUE (m);
6918         }
6919       return t;
6920
6921     case RANGE_EXPR:
6922       if (TREE_CONSTANT (t) != wins)
6923         {
6924           tem = copy_node (t);
6925           TREE_CONSTANT (tem) = wins;
6926           TREE_INVARIANT (tem) = wins;
6927           return tem;
6928         }
6929       return t;
6930
6931     case NEGATE_EXPR:
6932       if (negate_expr_p (arg0))
6933         return fold_convert (type, negate_expr (arg0));
6934       return t;
6935
6936     case ABS_EXPR:
6937       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
6938         return fold_abs_const (arg0, type);
6939       else if (TREE_CODE (arg0) == NEGATE_EXPR)
6940         return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0)));
6941       /* Convert fabs((double)float) into (double)fabsf(float).  */
6942       else if (TREE_CODE (arg0) == NOP_EXPR
6943                && TREE_CODE (type) == REAL_TYPE)
6944         {
6945           tree targ0 = strip_float_extensions (arg0);
6946           if (targ0 != arg0)
6947             return fold_convert (type, fold (build1 (ABS_EXPR,
6948                                                      TREE_TYPE (targ0),
6949                                                      targ0)));
6950         }
6951       else if (tree_expr_nonnegative_p (arg0))
6952         return arg0;
6953
6954       /* Strip sign ops from argument.  */
6955       if (TREE_CODE (type) == REAL_TYPE)
6956         {
6957           tem = fold_strip_sign_ops (arg0);
6958           if (tem)
6959             return fold (build1 (ABS_EXPR, type, fold_convert (type, tem)));
6960         }
6961       return t;
6962
6963     case CONJ_EXPR:
6964       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
6965         return fold_convert (type, arg0);
6966       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
6967         return build2 (COMPLEX_EXPR, type,
6968                        TREE_OPERAND (arg0, 0),
6969                        negate_expr (TREE_OPERAND (arg0, 1)));
6970       else if (TREE_CODE (arg0) == COMPLEX_CST)
6971         return build_complex (type, TREE_REALPART (arg0),
6972                               negate_expr (TREE_IMAGPART (arg0)));
6973       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
6974         return fold (build2 (TREE_CODE (arg0), type,
6975                              fold (build1 (CONJ_EXPR, type,
6976                                            TREE_OPERAND (arg0, 0))),
6977                              fold (build1 (CONJ_EXPR, type,
6978                                            TREE_OPERAND (arg0, 1)))));
6979       else if (TREE_CODE (arg0) == CONJ_EXPR)
6980         return TREE_OPERAND (arg0, 0);
6981       return t;
6982
6983     case BIT_NOT_EXPR:
6984       if (TREE_CODE (arg0) == INTEGER_CST)
6985         return fold_not_const (arg0, type);
6986       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
6987         return TREE_OPERAND (arg0, 0);
6988       return t;
6989
6990     case PLUS_EXPR:
6991       /* A + (-B) -> A - B */
6992       if (TREE_CODE (arg1) == NEGATE_EXPR)
6993         return fold (build2 (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
6994       /* (-A) + B -> B - A */
6995       if (TREE_CODE (arg0) == NEGATE_EXPR
6996           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
6997         return fold (build2 (MINUS_EXPR, type, arg1, TREE_OPERAND (arg0, 0)));
6998
6999       if (TREE_CODE (type) == COMPLEX_TYPE)
7000         {
7001           tem = fold_complex_add (type, arg0, arg1, PLUS_EXPR);
7002           if (tem)
7003             return tem;
7004         }
7005
7006       if (! FLOAT_TYPE_P (type))
7007         {
7008           if (integer_zerop (arg1))
7009             return non_lvalue (fold_convert (type, arg0));
7010
7011           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
7012              with a constant, and the two constants have no bits in common,
7013              we should treat this as a BIT_IOR_EXPR since this may produce more
7014              simplifications.  */
7015           if (TREE_CODE (arg0) == BIT_AND_EXPR
7016               && TREE_CODE (arg1) == BIT_AND_EXPR
7017               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7018               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
7019               && integer_zerop (const_binop (BIT_AND_EXPR,
7020                                              TREE_OPERAND (arg0, 1),
7021                                              TREE_OPERAND (arg1, 1), 0)))
7022             {
7023               code = BIT_IOR_EXPR;
7024               goto bit_ior;
7025             }
7026
7027           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
7028              (plus (plus (mult) (mult)) (foo)) so that we can
7029              take advantage of the factoring cases below.  */
7030           if (((TREE_CODE (arg0) == PLUS_EXPR
7031                 || TREE_CODE (arg0) == MINUS_EXPR)
7032                && TREE_CODE (arg1) == MULT_EXPR)
7033               || ((TREE_CODE (arg1) == PLUS_EXPR
7034                    || TREE_CODE (arg1) == MINUS_EXPR)
7035                   && TREE_CODE (arg0) == MULT_EXPR))
7036             {
7037               tree parg0, parg1, parg, marg;
7038               enum tree_code pcode;
7039
7040               if (TREE_CODE (arg1) == MULT_EXPR)
7041                 parg = arg0, marg = arg1;
7042               else
7043                 parg = arg1, marg = arg0;
7044               pcode = TREE_CODE (parg);
7045               parg0 = TREE_OPERAND (parg, 0);
7046               parg1 = TREE_OPERAND (parg, 1);
7047               STRIP_NOPS (parg0);
7048               STRIP_NOPS (parg1);
7049
7050               if (TREE_CODE (parg0) == MULT_EXPR
7051                   && TREE_CODE (parg1) != MULT_EXPR)
7052                 return fold (build2 (pcode, type,
7053                                      fold (build2 (PLUS_EXPR, type,
7054                                                    fold_convert (type, parg0),
7055                                                    fold_convert (type, marg))),
7056                                      fold_convert (type, parg1)));
7057               if (TREE_CODE (parg0) != MULT_EXPR
7058                   && TREE_CODE (parg1) == MULT_EXPR)
7059                 return fold (build2 (PLUS_EXPR, type,
7060                                      fold_convert (type, parg0),
7061                                      fold (build2 (pcode, type,
7062                                                    fold_convert (type, marg),
7063                                                    fold_convert (type,
7064                                                                  parg1)))));
7065             }
7066
7067           if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR)
7068             {
7069               tree arg00, arg01, arg10, arg11;
7070               tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7071
7072               /* (A * C) + (B * C) -> (A+B) * C.
7073                  We are most concerned about the case where C is a constant,
7074                  but other combinations show up during loop reduction.  Since
7075                  it is not difficult, try all four possibilities.  */
7076
7077               arg00 = TREE_OPERAND (arg0, 0);
7078               arg01 = TREE_OPERAND (arg0, 1);
7079               arg10 = TREE_OPERAND (arg1, 0);
7080               arg11 = TREE_OPERAND (arg1, 1);
7081               same = NULL_TREE;
7082
7083               if (operand_equal_p (arg01, arg11, 0))
7084                 same = arg01, alt0 = arg00, alt1 = arg10;
7085               else if (operand_equal_p (arg00, arg10, 0))
7086                 same = arg00, alt0 = arg01, alt1 = arg11;
7087               else if (operand_equal_p (arg00, arg11, 0))
7088                 same = arg00, alt0 = arg01, alt1 = arg10;
7089               else if (operand_equal_p (arg01, arg10, 0))
7090                 same = arg01, alt0 = arg00, alt1 = arg11;
7091
7092               /* No identical multiplicands; see if we can find a common
7093                  power-of-two factor in non-power-of-two multiplies.  This
7094                  can help in multi-dimensional array access.  */
7095               else if (TREE_CODE (arg01) == INTEGER_CST
7096                        && TREE_CODE (arg11) == INTEGER_CST
7097                        && TREE_INT_CST_HIGH (arg01) == 0
7098                        && TREE_INT_CST_HIGH (arg11) == 0)
7099                 {
7100                   HOST_WIDE_INT int01, int11, tmp;
7101                   int01 = TREE_INT_CST_LOW (arg01);
7102                   int11 = TREE_INT_CST_LOW (arg11);
7103
7104                   /* Move min of absolute values to int11.  */
7105                   if ((int01 >= 0 ? int01 : -int01)
7106                       < (int11 >= 0 ? int11 : -int11))
7107                     {
7108                       tmp = int01, int01 = int11, int11 = tmp;
7109                       alt0 = arg00, arg00 = arg10, arg10 = alt0;
7110                       alt0 = arg01, arg01 = arg11, arg11 = alt0;
7111                     }
7112
7113                   if (exact_log2 (int11) > 0 && int01 % int11 == 0)
7114                     {
7115                       alt0 = fold (build2 (MULT_EXPR, type, arg00,
7116                                            build_int_cst (NULL_TREE,
7117                                                           int01 / int11)));
7118                       alt1 = arg10;
7119                       same = arg11;
7120                     }
7121                 }
7122
7123               if (same)
7124                 return fold (build2 (MULT_EXPR, type,
7125                                      fold (build2 (PLUS_EXPR, type,
7126                                                    fold_convert (type, alt0),
7127                                                    fold_convert (type, alt1))),
7128                                      same));
7129             }
7130
7131           /* Try replacing &a[i1] + c * i2 with &a[i1 + i2], if c is step
7132              of the array.  Loop optimizer sometimes produce this type of
7133              expressions.  */
7134           if (TREE_CODE (arg0) == ADDR_EXPR
7135               && TREE_CODE (arg1) == MULT_EXPR)
7136             {
7137               tem = try_move_mult_to_index (PLUS_EXPR, arg0, arg1);
7138               if (tem)
7139                 return fold_convert (type, fold (tem));
7140             }
7141           else if (TREE_CODE (arg1) == ADDR_EXPR
7142                    && TREE_CODE (arg0) == MULT_EXPR)
7143             {
7144               tem = try_move_mult_to_index (PLUS_EXPR, arg1, arg0);
7145               if (tem)
7146                 return fold_convert (type, fold (tem));
7147             }
7148         }
7149       else
7150         {
7151           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
7152           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
7153             return non_lvalue (fold_convert (type, arg0));
7154
7155           /* Likewise if the operands are reversed.  */
7156           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
7157             return non_lvalue (fold_convert (type, arg1));
7158
7159           /* Convert X + -C into X - C.  */
7160           if (TREE_CODE (arg1) == REAL_CST
7161               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
7162             {
7163               tem = fold_negate_const (arg1, type);
7164               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
7165                 return fold (build2 (MINUS_EXPR, type,
7166                                      fold_convert (type, arg0),
7167                                      fold_convert (type, tem)));
7168             }
7169
7170           /* Convert x+x into x*2.0.  */
7171           if (operand_equal_p (arg0, arg1, 0)
7172               && SCALAR_FLOAT_TYPE_P (type))
7173             return fold (build2 (MULT_EXPR, type, arg0,
7174                                  build_real (type, dconst2)));
7175
7176           /* Convert x*c+x into x*(c+1).  */
7177           if (flag_unsafe_math_optimizations
7178               && TREE_CODE (arg0) == MULT_EXPR
7179               && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
7180               && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0, 1))
7181               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
7182             {
7183               REAL_VALUE_TYPE c;
7184
7185               c = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
7186               real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
7187               return fold (build2 (MULT_EXPR, type, arg1,
7188                                    build_real (type, c)));
7189             }
7190
7191           /* Convert x+x*c into x*(c+1).  */
7192           if (flag_unsafe_math_optimizations
7193               && TREE_CODE (arg1) == MULT_EXPR
7194               && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST
7195               && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1, 1))
7196               && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
7197             {
7198               REAL_VALUE_TYPE c;
7199
7200               c = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
7201               real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
7202               return fold (build2 (MULT_EXPR, type, arg0,
7203                                    build_real (type, c)));
7204             }
7205
7206           /* Convert x*c1+x*c2 into x*(c1+c2).  */
7207           if (flag_unsafe_math_optimizations
7208               && TREE_CODE (arg0) == MULT_EXPR
7209               && TREE_CODE (arg1) == MULT_EXPR
7210               && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
7211               && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0, 1))
7212               && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST
7213               && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1, 1))
7214               && operand_equal_p (TREE_OPERAND (arg0, 0),
7215                                   TREE_OPERAND (arg1, 0), 0))
7216             {
7217               REAL_VALUE_TYPE c1, c2;
7218
7219               c1 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
7220               c2 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
7221               real_arithmetic (&c1, PLUS_EXPR, &c1, &c2);
7222               return fold (build2 (MULT_EXPR, type,
7223                                    TREE_OPERAND (arg0, 0),
7224                                    build_real (type, c1)));
7225             }
7226           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.  */
7227           if (flag_unsafe_math_optimizations
7228               && TREE_CODE (arg1) == PLUS_EXPR
7229               && TREE_CODE (arg0) != MULT_EXPR)
7230             {
7231               tree tree10 = TREE_OPERAND (arg1, 0);
7232               tree tree11 = TREE_OPERAND (arg1, 1);
7233               if (TREE_CODE (tree11) == MULT_EXPR
7234                   && TREE_CODE (tree10) == MULT_EXPR)
7235                 {
7236                   tree tree0;
7237                   tree0 = fold (build2 (PLUS_EXPR, type, arg0, tree10));
7238                   return fold (build2 (PLUS_EXPR, type, tree0, tree11));
7239                 }
7240             }
7241           /* Convert (b*c + d*e) + a into b*c + (d*e +a).  */
7242           if (flag_unsafe_math_optimizations
7243               && TREE_CODE (arg0) == PLUS_EXPR
7244               && TREE_CODE (arg1) != MULT_EXPR)
7245             {
7246               tree tree00 = TREE_OPERAND (arg0, 0);
7247               tree tree01 = TREE_OPERAND (arg0, 1);
7248               if (TREE_CODE (tree01) == MULT_EXPR
7249                   && TREE_CODE (tree00) == MULT_EXPR)
7250                 {
7251                   tree tree0;
7252                   tree0 = fold (build2 (PLUS_EXPR, type, tree01, arg1));
7253                   return fold (build2 (PLUS_EXPR, type, tree00, tree0));
7254                 }
7255             }
7256         }
7257
7258      bit_rotate:
7259       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
7260          is a rotate of A by C1 bits.  */
7261       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
7262          is a rotate of A by B bits.  */
7263       {
7264         enum tree_code code0, code1;
7265         code0 = TREE_CODE (arg0);
7266         code1 = TREE_CODE (arg1);
7267         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
7268              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
7269             && operand_equal_p (TREE_OPERAND (arg0, 0),
7270                                 TREE_OPERAND (arg1, 0), 0)
7271             && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
7272           {
7273             tree tree01, tree11;
7274             enum tree_code code01, code11;
7275
7276             tree01 = TREE_OPERAND (arg0, 1);
7277             tree11 = TREE_OPERAND (arg1, 1);
7278             STRIP_NOPS (tree01);
7279             STRIP_NOPS (tree11);
7280             code01 = TREE_CODE (tree01);
7281             code11 = TREE_CODE (tree11);
7282             if (code01 == INTEGER_CST
7283                 && code11 == INTEGER_CST
7284                 && TREE_INT_CST_HIGH (tree01) == 0
7285                 && TREE_INT_CST_HIGH (tree11) == 0
7286                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
7287                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
7288               return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
7289                              code0 == LSHIFT_EXPR ? tree01 : tree11);
7290             else if (code11 == MINUS_EXPR)
7291               {
7292                 tree tree110, tree111;
7293                 tree110 = TREE_OPERAND (tree11, 0);
7294                 tree111 = TREE_OPERAND (tree11, 1);
7295                 STRIP_NOPS (tree110);
7296                 STRIP_NOPS (tree111);
7297                 if (TREE_CODE (tree110) == INTEGER_CST
7298                     && 0 == compare_tree_int (tree110,
7299                                               TYPE_PRECISION
7300                                               (TREE_TYPE (TREE_OPERAND
7301                                                           (arg0, 0))))
7302                     && operand_equal_p (tree01, tree111, 0))
7303                   return build2 ((code0 == LSHIFT_EXPR
7304                                   ? LROTATE_EXPR
7305                                   : RROTATE_EXPR),
7306                                  type, TREE_OPERAND (arg0, 0), tree01);
7307               }
7308             else if (code01 == MINUS_EXPR)
7309               {
7310                 tree tree010, tree011;
7311                 tree010 = TREE_OPERAND (tree01, 0);
7312                 tree011 = TREE_OPERAND (tree01, 1);
7313                 STRIP_NOPS (tree010);
7314                 STRIP_NOPS (tree011);
7315                 if (TREE_CODE (tree010) == INTEGER_CST
7316                     && 0 == compare_tree_int (tree010,
7317                                               TYPE_PRECISION
7318                                               (TREE_TYPE (TREE_OPERAND
7319                                                           (arg0, 0))))
7320                     && operand_equal_p (tree11, tree011, 0))
7321                   return build2 ((code0 != LSHIFT_EXPR
7322                                   ? LROTATE_EXPR
7323                                   : RROTATE_EXPR),
7324                                  type, TREE_OPERAND (arg0, 0), tree11);
7325               }
7326           }
7327       }
7328
7329     associate:
7330       /* In most languages, can't associate operations on floats through
7331          parentheses.  Rather than remember where the parentheses were, we
7332          don't associate floats at all, unless the user has specified
7333          -funsafe-math-optimizations.  */
7334
7335       if (! wins
7336           && (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
7337         {
7338           tree var0, con0, lit0, minus_lit0;
7339           tree var1, con1, lit1, minus_lit1;
7340
7341           /* Split both trees into variables, constants, and literals.  Then
7342              associate each group together, the constants with literals,
7343              then the result with variables.  This increases the chances of
7344              literals being recombined later and of generating relocatable
7345              expressions for the sum of a constant and literal.  */
7346           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
7347           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
7348                              code == MINUS_EXPR);
7349
7350           /* Only do something if we found more than two objects.  Otherwise,
7351              nothing has changed and we risk infinite recursion.  */
7352           if (2 < ((var0 != 0) + (var1 != 0)
7353                    + (con0 != 0) + (con1 != 0)
7354                    + (lit0 != 0) + (lit1 != 0)
7355                    + (minus_lit0 != 0) + (minus_lit1 != 0)))
7356             {
7357               /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
7358               if (code == MINUS_EXPR)
7359                 code = PLUS_EXPR;
7360
7361               var0 = associate_trees (var0, var1, code, type);
7362               con0 = associate_trees (con0, con1, code, type);
7363               lit0 = associate_trees (lit0, lit1, code, type);
7364               minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
7365
7366               /* Preserve the MINUS_EXPR if the negative part of the literal is
7367                  greater than the positive part.  Otherwise, the multiplicative
7368                  folding code (i.e extract_muldiv) may be fooled in case
7369                  unsigned constants are subtracted, like in the following
7370                  example: ((X*2 + 4) - 8U)/2.  */
7371               if (minus_lit0 && lit0)
7372                 {
7373                   if (TREE_CODE (lit0) == INTEGER_CST
7374                       && TREE_CODE (minus_lit0) == INTEGER_CST
7375                       && tree_int_cst_lt (lit0, minus_lit0))
7376                     {
7377                       minus_lit0 = associate_trees (minus_lit0, lit0,
7378                                                     MINUS_EXPR, type);
7379                       lit0 = 0;
7380                     }
7381                   else
7382                     {
7383                       lit0 = associate_trees (lit0, minus_lit0,
7384                                               MINUS_EXPR, type);
7385                       minus_lit0 = 0;
7386                     }
7387                 }
7388               if (minus_lit0)
7389                 {
7390                   if (con0 == 0)
7391                     return fold_convert (type,
7392                                          associate_trees (var0, minus_lit0,
7393                                                           MINUS_EXPR, type));
7394                   else
7395                     {
7396                       con0 = associate_trees (con0, minus_lit0,
7397                                               MINUS_EXPR, type);
7398                       return fold_convert (type,
7399                                            associate_trees (var0, con0,
7400                                                             PLUS_EXPR, type));
7401                     }
7402                 }
7403
7404               con0 = associate_trees (con0, lit0, code, type);
7405               return fold_convert (type, associate_trees (var0, con0,
7406                                                           code, type));
7407             }
7408         }
7409
7410     binary:
7411       if (wins)
7412         t1 = const_binop (code, arg0, arg1, 0);
7413       if (t1 != NULL_TREE)
7414         {
7415           /* The return value should always have
7416              the same type as the original expression.  */
7417           if (TREE_TYPE (t1) != type)
7418             t1 = fold_convert (type, t1);
7419
7420           return t1;
7421         }
7422       return t;
7423
7424     case MINUS_EXPR:
7425       /* A - (-B) -> A + B */
7426       if (TREE_CODE (arg1) == NEGATE_EXPR)
7427         return fold (build2 (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
7428       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
7429       if (TREE_CODE (arg0) == NEGATE_EXPR
7430           && (FLOAT_TYPE_P (type)
7431               || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv))
7432           && negate_expr_p (arg1)
7433           && reorder_operands_p (arg0, arg1))
7434         return fold (build2 (MINUS_EXPR, type, negate_expr (arg1),
7435                              TREE_OPERAND (arg0, 0)));
7436
7437       if (TREE_CODE (type) == COMPLEX_TYPE)
7438         {
7439           tem = fold_complex_add (type, arg0, arg1, MINUS_EXPR);
7440           if (tem)
7441             return tem;
7442         }
7443
7444       if (! FLOAT_TYPE_P (type))
7445         {
7446           if (! wins && integer_zerop (arg0))
7447             return negate_expr (fold_convert (type, arg1));
7448           if (integer_zerop (arg1))
7449             return non_lvalue (fold_convert (type, arg0));
7450
7451           /* Fold A - (A & B) into ~B & A.  */
7452           if (!TREE_SIDE_EFFECTS (arg0)
7453               && TREE_CODE (arg1) == BIT_AND_EXPR)
7454             {
7455               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
7456                 return fold (build2 (BIT_AND_EXPR, type,
7457                                      fold (build1 (BIT_NOT_EXPR, type,
7458                                                    TREE_OPERAND (arg1, 0))),
7459                                      arg0));
7460               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
7461                 return fold (build2 (BIT_AND_EXPR, type,
7462                                      fold (build1 (BIT_NOT_EXPR, type,
7463                                                    TREE_OPERAND (arg1, 1))),
7464                                      arg0));
7465             }
7466
7467           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
7468              any power of 2 minus 1.  */
7469           if (TREE_CODE (arg0) == BIT_AND_EXPR
7470               && TREE_CODE (arg1) == BIT_AND_EXPR
7471               && operand_equal_p (TREE_OPERAND (arg0, 0),
7472                                   TREE_OPERAND (arg1, 0), 0))
7473             {
7474               tree mask0 = TREE_OPERAND (arg0, 1);
7475               tree mask1 = TREE_OPERAND (arg1, 1);
7476               tree tem = fold (build1 (BIT_NOT_EXPR, type, mask0));
7477
7478               if (operand_equal_p (tem, mask1, 0))
7479                 {
7480                   tem = fold (build2 (BIT_XOR_EXPR, type,
7481                                       TREE_OPERAND (arg0, 0), mask1));
7482                   return fold (build2 (MINUS_EXPR, type, tem, mask1));
7483                 }
7484             }
7485         }
7486
7487       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
7488       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
7489         return non_lvalue (fold_convert (type, arg0));
7490
7491       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
7492          ARG0 is zero and X + ARG0 reduces to X, since that would mean
7493          (-ARG1 + ARG0) reduces to -ARG1.  */
7494       else if (!wins && fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
7495         return negate_expr (fold_convert (type, arg1));
7496
7497       /* Fold &x - &x.  This can happen from &x.foo - &x.
7498          This is unsafe for certain floats even in non-IEEE formats.
7499          In IEEE, it is unsafe because it does wrong for NaNs.
7500          Also note that operand_equal_p is always false if an operand
7501          is volatile.  */
7502
7503       if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
7504           && operand_equal_p (arg0, arg1, 0))
7505         return fold_convert (type, integer_zero_node);
7506
7507       /* A - B -> A + (-B) if B is easily negatable.  */
7508       if (!wins && negate_expr_p (arg1)
7509           && ((FLOAT_TYPE_P (type)
7510                /* Avoid this transformation if B is a positive REAL_CST.  */
7511                && (TREE_CODE (arg1) != REAL_CST
7512                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
7513               || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv)))
7514         return fold (build2 (PLUS_EXPR, type, arg0, negate_expr (arg1)));
7515
7516       /* Try folding difference of addresses.  */
7517       {
7518         HOST_WIDE_INT diff;
7519
7520         if ((TREE_CODE (arg0) == ADDR_EXPR
7521              || TREE_CODE (arg1) == ADDR_EXPR)
7522             && ptr_difference_const (arg0, arg1, &diff))
7523           return build_int_cst_type (type, diff);
7524       }
7525           
7526       /* Try replacing &a[i1] - c * i2 with &a[i1 - i2], if c is step
7527          of the array.  Loop optimizer sometimes produce this type of
7528          expressions.  */
7529       if (TREE_CODE (arg0) == ADDR_EXPR
7530           && TREE_CODE (arg1) == MULT_EXPR)
7531         {
7532           tem = try_move_mult_to_index (MINUS_EXPR, arg0, arg1);
7533           if (tem)
7534             return fold_convert (type, fold (tem));
7535         }
7536
7537       if (TREE_CODE (arg0) == MULT_EXPR
7538           && TREE_CODE (arg1) == MULT_EXPR
7539           && (!FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
7540         {
7541           /* (A * C) - (B * C) -> (A-B) * C.  */
7542           if (operand_equal_p (TREE_OPERAND (arg0, 1),
7543                                TREE_OPERAND (arg1, 1), 0))
7544             return fold (build2 (MULT_EXPR, type,
7545                                  fold (build2 (MINUS_EXPR, type,
7546                                                TREE_OPERAND (arg0, 0),
7547                                                TREE_OPERAND (arg1, 0))),
7548                                  TREE_OPERAND (arg0, 1)));
7549           /* (A * C1) - (A * C2) -> A * (C1-C2).  */
7550           if (operand_equal_p (TREE_OPERAND (arg0, 0),
7551                                TREE_OPERAND (arg1, 0), 0))
7552             return fold (build2 (MULT_EXPR, type,
7553                                  TREE_OPERAND (arg0, 0),
7554                                  fold (build2 (MINUS_EXPR, type,
7555                                                TREE_OPERAND (arg0, 1),
7556                                                TREE_OPERAND (arg1, 1)))));
7557         }
7558
7559       goto associate;
7560
7561     case MULT_EXPR:
7562       /* (-A) * (-B) -> A * B  */
7563       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
7564         return fold (build2 (MULT_EXPR, type,
7565                              TREE_OPERAND (arg0, 0),
7566                              negate_expr (arg1)));
7567       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
7568         return fold (build2 (MULT_EXPR, type,
7569                              negate_expr (arg0),
7570                              TREE_OPERAND (arg1, 0)));
7571
7572       if (TREE_CODE (type) == COMPLEX_TYPE)
7573         {
7574           tem = fold_complex_mult (type, arg0, arg1);
7575           if (tem)
7576             return tem;
7577         }
7578
7579       if (! FLOAT_TYPE_P (type))
7580         {
7581           if (integer_zerop (arg1))
7582             return omit_one_operand (type, arg1, arg0);
7583           if (integer_onep (arg1))
7584             return non_lvalue (fold_convert (type, arg0));
7585
7586           /* (a * (1 << b)) is (a << b)  */
7587           if (TREE_CODE (arg1) == LSHIFT_EXPR
7588               && integer_onep (TREE_OPERAND (arg1, 0)))
7589             return fold (build2 (LSHIFT_EXPR, type, arg0,
7590                                  TREE_OPERAND (arg1, 1)));
7591           if (TREE_CODE (arg0) == LSHIFT_EXPR
7592               && integer_onep (TREE_OPERAND (arg0, 0)))
7593             return fold (build2 (LSHIFT_EXPR, type, arg1,
7594                                  TREE_OPERAND (arg0, 1)));
7595
7596           if (TREE_CODE (arg1) == INTEGER_CST
7597               && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0),
7598                                              fold_convert (type, arg1),
7599                                              code, NULL_TREE)))
7600             return fold_convert (type, tem);
7601
7602         }
7603       else
7604         {
7605           /* Maybe fold x * 0 to 0.  The expressions aren't the same
7606              when x is NaN, since x * 0 is also NaN.  Nor are they the
7607              same in modes with signed zeros, since multiplying a
7608              negative value by 0 gives -0, not +0.  */
7609           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
7610               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
7611               && real_zerop (arg1))
7612             return omit_one_operand (type, arg1, arg0);
7613           /* In IEEE floating point, x*1 is not equivalent to x for snans.  */
7614           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
7615               && real_onep (arg1))
7616             return non_lvalue (fold_convert (type, arg0));
7617
7618           /* Transform x * -1.0 into -x.  */
7619           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
7620               && real_minus_onep (arg1))
7621             return fold_convert (type, negate_expr (arg0));
7622
7623           /* Convert (C1/X)*C2 into (C1*C2)/X.  */
7624           if (flag_unsafe_math_optimizations
7625               && TREE_CODE (arg0) == RDIV_EXPR
7626               && TREE_CODE (arg1) == REAL_CST
7627               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
7628             {
7629               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
7630                                       arg1, 0);
7631               if (tem)
7632                 return fold (build2 (RDIV_EXPR, type, tem,
7633                                      TREE_OPERAND (arg0, 1)));
7634             }
7635
7636           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
7637           if (operand_equal_p (arg0, arg1, 0))
7638             {
7639               tree tem = fold_strip_sign_ops (arg0);
7640               if (tem != NULL_TREE)
7641                 {
7642                   tem = fold_convert (type, tem);
7643                   return fold (build2 (MULT_EXPR, type, tem, tem));
7644                 }
7645             }
7646
7647           if (flag_unsafe_math_optimizations)
7648             {
7649               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
7650               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
7651
7652               /* Optimizations of root(...)*root(...).  */
7653               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
7654                 {
7655                   tree rootfn, arg, arglist;
7656                   tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7657                   tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
7658
7659                   /* Optimize sqrt(x)*sqrt(x) as x.  */
7660                   if (BUILTIN_SQRT_P (fcode0)
7661                       && operand_equal_p (arg00, arg10, 0)
7662                       && ! HONOR_SNANS (TYPE_MODE (type)))
7663                     return arg00;
7664
7665                   /* Optimize root(x)*root(y) as root(x*y).  */
7666                   rootfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7667                   arg = fold (build2 (MULT_EXPR, type, arg00, arg10));
7668                   arglist = build_tree_list (NULL_TREE, arg);
7669                   return build_function_call_expr (rootfn, arglist);
7670                 }
7671
7672               /* Optimize expN(x)*expN(y) as expN(x+y).  */
7673               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
7674                 {
7675                   tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7676                   tree arg = build2 (PLUS_EXPR, type,
7677                                      TREE_VALUE (TREE_OPERAND (arg0, 1)),
7678                                      TREE_VALUE (TREE_OPERAND (arg1, 1)));
7679                   tree arglist = build_tree_list (NULL_TREE, fold (arg));
7680                   return build_function_call_expr (expfn, arglist);
7681                 }
7682
7683               /* Optimizations of pow(...)*pow(...).  */
7684               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
7685                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
7686                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
7687                 {
7688                   tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7689                   tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
7690                                                                      1)));
7691                   tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
7692                   tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
7693                                                                      1)));
7694
7695                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
7696                   if (operand_equal_p (arg01, arg11, 0))
7697                     {
7698                       tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7699                       tree arg = build2 (MULT_EXPR, type, arg00, arg10);
7700                       tree arglist = tree_cons (NULL_TREE, fold (arg),
7701                                                 build_tree_list (NULL_TREE,
7702                                                                  arg01));
7703                       return build_function_call_expr (powfn, arglist);
7704                     }
7705
7706                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
7707                   if (operand_equal_p (arg00, arg10, 0))
7708                     {
7709                       tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7710                       tree arg = fold (build2 (PLUS_EXPR, type, arg01, arg11));
7711                       tree arglist = tree_cons (NULL_TREE, arg00,
7712                                                 build_tree_list (NULL_TREE,
7713                                                                  arg));
7714                       return build_function_call_expr (powfn, arglist);
7715                     }
7716                 }
7717
7718               /* Optimize tan(x)*cos(x) as sin(x).  */
7719               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
7720                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
7721                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
7722                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
7723                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
7724                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
7725                   && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
7726                                       TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
7727                 {
7728                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
7729
7730                   if (sinfn != NULL_TREE)
7731                     return build_function_call_expr (sinfn,
7732                                                      TREE_OPERAND (arg0, 1));
7733                 }
7734
7735               /* Optimize x*pow(x,c) as pow(x,c+1).  */
7736               if (fcode1 == BUILT_IN_POW
7737                   || fcode1 == BUILT_IN_POWF
7738                   || fcode1 == BUILT_IN_POWL)
7739                 {
7740                   tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
7741                   tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
7742                                                                      1)));
7743                   if (TREE_CODE (arg11) == REAL_CST
7744                       && ! TREE_CONSTANT_OVERFLOW (arg11)
7745                       && operand_equal_p (arg0, arg10, 0))
7746                     {
7747                       tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
7748                       REAL_VALUE_TYPE c;
7749                       tree arg, arglist;
7750
7751                       c = TREE_REAL_CST (arg11);
7752                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
7753                       arg = build_real (type, c);
7754                       arglist = build_tree_list (NULL_TREE, arg);
7755                       arglist = tree_cons (NULL_TREE, arg0, arglist);
7756                       return build_function_call_expr (powfn, arglist);
7757                     }
7758                 }
7759
7760               /* Optimize pow(x,c)*x as pow(x,c+1).  */
7761               if (fcode0 == BUILT_IN_POW
7762                   || fcode0 == BUILT_IN_POWF
7763                   || fcode0 == BUILT_IN_POWL)
7764                 {
7765                   tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7766                   tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
7767                                                                      1)));
7768                   if (TREE_CODE (arg01) == REAL_CST
7769                       && ! TREE_CONSTANT_OVERFLOW (arg01)
7770                       && operand_equal_p (arg1, arg00, 0))
7771                     {
7772                       tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7773                       REAL_VALUE_TYPE c;
7774                       tree arg, arglist;
7775
7776                       c = TREE_REAL_CST (arg01);
7777                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
7778                       arg = build_real (type, c);
7779                       arglist = build_tree_list (NULL_TREE, arg);
7780                       arglist = tree_cons (NULL_TREE, arg1, arglist);
7781                       return build_function_call_expr (powfn, arglist);
7782                     }
7783                 }
7784
7785               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
7786               if (! optimize_size
7787                   && operand_equal_p (arg0, arg1, 0))
7788                 {
7789                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7790
7791                   if (powfn)
7792                     {
7793                       tree arg = build_real (type, dconst2);
7794                       tree arglist = build_tree_list (NULL_TREE, arg);
7795                       arglist = tree_cons (NULL_TREE, arg0, arglist);
7796                       return build_function_call_expr (powfn, arglist);
7797                     }
7798                 }
7799             }
7800         }
7801       goto associate;
7802
7803     case BIT_IOR_EXPR:
7804     bit_ior:
7805       if (integer_all_onesp (arg1))
7806         return omit_one_operand (type, arg1, arg0);
7807       if (integer_zerop (arg1))
7808         return non_lvalue (fold_convert (type, arg0));
7809       if (operand_equal_p (arg0, arg1, 0))
7810         return non_lvalue (fold_convert (type, arg0));
7811
7812       /* ~X | X is -1.  */
7813       if (TREE_CODE (arg0) == BIT_NOT_EXPR
7814           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
7815         {
7816           t1 = build_int_cst (type, -1);
7817           t1 = force_fit_type (t1, 0, false, false);
7818           return omit_one_operand (type, t1, arg1);
7819         }
7820
7821       /* X | ~X is -1.  */
7822       if (TREE_CODE (arg1) == BIT_NOT_EXPR
7823           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
7824         {
7825           t1 = build_int_cst (type, -1);
7826           t1 = force_fit_type (t1, 0, false, false);
7827           return omit_one_operand (type, t1, arg0);
7828         }
7829
7830       t1 = distribute_bit_expr (code, type, arg0, arg1);
7831       if (t1 != NULL_TREE)
7832         return t1;
7833
7834       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
7835
7836          This results in more efficient code for machines without a NAND
7837          instruction.  Combine will canonicalize to the first form
7838          which will allow use of NAND instructions provided by the
7839          backend if they exist.  */
7840       if (TREE_CODE (arg0) == BIT_NOT_EXPR
7841           && TREE_CODE (arg1) == BIT_NOT_EXPR)
7842         {
7843           return fold (build1 (BIT_NOT_EXPR, type,
7844                                build2 (BIT_AND_EXPR, type,
7845                                        TREE_OPERAND (arg0, 0),
7846                                        TREE_OPERAND (arg1, 0))));
7847         }
7848
7849       /* See if this can be simplified into a rotate first.  If that
7850          is unsuccessful continue in the association code.  */
7851       goto bit_rotate;
7852
7853     case BIT_XOR_EXPR:
7854       if (integer_zerop (arg1))
7855         return non_lvalue (fold_convert (type, arg0));
7856       if (integer_all_onesp (arg1))
7857         return fold (build1 (BIT_NOT_EXPR, type, arg0));
7858       if (operand_equal_p (arg0, arg1, 0))
7859         return omit_one_operand (type, integer_zero_node, arg0);
7860
7861       /* ~X ^ X is -1.  */
7862       if (TREE_CODE (arg0) == BIT_NOT_EXPR
7863           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
7864         {
7865           t1 = build_int_cst (type, -1);
7866           t1 = force_fit_type (t1, 0, false, false);
7867           return omit_one_operand (type, t1, arg1);
7868         }
7869
7870       /* X ^ ~X is -1.  */
7871       if (TREE_CODE (arg1) == BIT_NOT_EXPR
7872           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
7873         {
7874           t1 = build_int_cst (type, -1);
7875           t1 = force_fit_type (t1, 0, false, false);
7876           return omit_one_operand (type, t1, arg0);
7877         }
7878
7879       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
7880          with a constant, and the two constants have no bits in common,
7881          we should treat this as a BIT_IOR_EXPR since this may produce more
7882          simplifications.  */
7883       if (TREE_CODE (arg0) == BIT_AND_EXPR
7884           && TREE_CODE (arg1) == BIT_AND_EXPR
7885           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7886           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
7887           && integer_zerop (const_binop (BIT_AND_EXPR,
7888                                          TREE_OPERAND (arg0, 1),
7889                                          TREE_OPERAND (arg1, 1), 0)))
7890         {
7891           code = BIT_IOR_EXPR;
7892           goto bit_ior;
7893         }
7894
7895       /* See if this can be simplified into a rotate first.  If that
7896          is unsuccessful continue in the association code.  */
7897       goto bit_rotate;
7898
7899     case BIT_AND_EXPR:
7900       if (integer_all_onesp (arg1))
7901         return non_lvalue (fold_convert (type, arg0));
7902       if (integer_zerop (arg1))
7903         return omit_one_operand (type, arg1, arg0);
7904       if (operand_equal_p (arg0, arg1, 0))
7905         return non_lvalue (fold_convert (type, arg0));
7906
7907       /* ~X & X is always zero.  */
7908       if (TREE_CODE (arg0) == BIT_NOT_EXPR
7909           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
7910         return omit_one_operand (type, integer_zero_node, arg1);
7911
7912       /* X & ~X is always zero.  */
7913       if (TREE_CODE (arg1) == BIT_NOT_EXPR
7914           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
7915         return omit_one_operand (type, integer_zero_node, arg0);
7916
7917       t1 = distribute_bit_expr (code, type, arg0, arg1);
7918       if (t1 != NULL_TREE)
7919         return t1;
7920       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
7921       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
7922           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
7923         {
7924           unsigned int prec
7925             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
7926
7927           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
7928               && (~TREE_INT_CST_LOW (arg1)
7929                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
7930             return fold_convert (type, TREE_OPERAND (arg0, 0));
7931         }
7932
7933       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
7934
7935          This results in more efficient code for machines without a NOR
7936          instruction.  Combine will canonicalize to the first form
7937          which will allow use of NOR instructions provided by the
7938          backend if they exist.  */
7939       if (TREE_CODE (arg0) == BIT_NOT_EXPR
7940           && TREE_CODE (arg1) == BIT_NOT_EXPR)
7941         {
7942           return fold (build1 (BIT_NOT_EXPR, type,
7943                                build2 (BIT_IOR_EXPR, type,
7944                                        TREE_OPERAND (arg0, 0),
7945                                        TREE_OPERAND (arg1, 0))));
7946         }
7947
7948       goto associate;
7949
7950     case RDIV_EXPR:
7951       /* Don't touch a floating-point divide by zero unless the mode
7952          of the constant can represent infinity.  */
7953       if (TREE_CODE (arg1) == REAL_CST
7954           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
7955           && real_zerop (arg1))
7956         return t;
7957
7958       /* (-A) / (-B) -> A / B  */
7959       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
7960         return fold (build2 (RDIV_EXPR, type,
7961                              TREE_OPERAND (arg0, 0),
7962                              negate_expr (arg1)));
7963       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
7964         return fold (build2 (RDIV_EXPR, type,
7965                              negate_expr (arg0),
7966                              TREE_OPERAND (arg1, 0)));
7967
7968       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
7969       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
7970           && real_onep (arg1))
7971         return non_lvalue (fold_convert (type, arg0));
7972
7973       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
7974       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
7975           && real_minus_onep (arg1))
7976         return non_lvalue (fold_convert (type, negate_expr (arg0)));
7977
7978       /* If ARG1 is a constant, we can convert this to a multiply by the
7979          reciprocal.  This does not have the same rounding properties,
7980          so only do this if -funsafe-math-optimizations.  We can actually
7981          always safely do it if ARG1 is a power of two, but it's hard to
7982          tell if it is or not in a portable manner.  */
7983       if (TREE_CODE (arg1) == REAL_CST)
7984         {
7985           if (flag_unsafe_math_optimizations
7986               && 0 != (tem = const_binop (code, build_real (type, dconst1),
7987                                           arg1, 0)))
7988             return fold (build2 (MULT_EXPR, type, arg0, tem));
7989           /* Find the reciprocal if optimizing and the result is exact.  */
7990           if (optimize)
7991             {
7992               REAL_VALUE_TYPE r;
7993               r = TREE_REAL_CST (arg1);
7994               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
7995                 {
7996                   tem = build_real (type, r);
7997                   return fold (build2 (MULT_EXPR, type, arg0, tem));
7998                 }
7999             }
8000         }
8001       /* Convert A/B/C to A/(B*C).  */
8002       if (flag_unsafe_math_optimizations
8003           && TREE_CODE (arg0) == RDIV_EXPR)
8004         return fold (build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
8005                              fold (build2 (MULT_EXPR, type,
8006                                            TREE_OPERAND (arg0, 1), arg1))));
8007
8008       /* Convert A/(B/C) to (A/B)*C.  */
8009       if (flag_unsafe_math_optimizations
8010           && TREE_CODE (arg1) == RDIV_EXPR)
8011         return fold (build2 (MULT_EXPR, type,
8012                              fold (build2 (RDIV_EXPR, type, arg0,
8013                                            TREE_OPERAND (arg1, 0))),
8014                              TREE_OPERAND (arg1, 1)));
8015
8016       /* Convert C1/(X*C2) into (C1/C2)/X.  */
8017       if (flag_unsafe_math_optimizations
8018           && TREE_CODE (arg1) == MULT_EXPR
8019           && TREE_CODE (arg0) == REAL_CST
8020           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
8021         {
8022           tree tem = const_binop (RDIV_EXPR, arg0,
8023                                   TREE_OPERAND (arg1, 1), 0);
8024           if (tem)
8025             return fold (build2 (RDIV_EXPR, type, tem,
8026                                  TREE_OPERAND (arg1, 0)));
8027         }
8028
8029       if (flag_unsafe_math_optimizations)
8030         {
8031           enum built_in_function fcode = builtin_mathfn_code (arg1);
8032           /* Optimize x/expN(y) into x*expN(-y).  */
8033           if (BUILTIN_EXPONENT_P (fcode))
8034             {
8035               tree expfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
8036               tree arg = negate_expr (TREE_VALUE (TREE_OPERAND (arg1, 1)));
8037               tree arglist = build_tree_list (NULL_TREE,
8038                                               fold_convert (type, arg));
8039               arg1 = build_function_call_expr (expfn, arglist);
8040               return fold (build2 (MULT_EXPR, type, arg0, arg1));
8041             }
8042
8043           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
8044           if (fcode == BUILT_IN_POW
8045               || fcode == BUILT_IN_POWF
8046               || fcode == BUILT_IN_POWL)
8047             {
8048               tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
8049               tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
8050               tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1, 1)));
8051               tree neg11 = fold_convert (type, negate_expr (arg11));
8052               tree arglist = tree_cons(NULL_TREE, arg10,
8053                                        build_tree_list (NULL_TREE, neg11));
8054               arg1 = build_function_call_expr (powfn, arglist);
8055               return fold (build2 (MULT_EXPR, type, arg0, arg1));
8056             }
8057         }
8058
8059       if (flag_unsafe_math_optimizations)
8060         {
8061           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
8062           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
8063
8064           /* Optimize sin(x)/cos(x) as tan(x).  */
8065           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
8066                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
8067                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
8068               && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
8069                                   TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
8070             {
8071               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
8072
8073               if (tanfn != NULL_TREE)
8074                 return build_function_call_expr (tanfn,
8075                                                  TREE_OPERAND (arg0, 1));
8076             }
8077
8078           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
8079           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
8080                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
8081                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
8082               && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
8083                                   TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
8084             {
8085               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
8086
8087               if (tanfn != NULL_TREE)
8088                 {
8089                   tree tmp = TREE_OPERAND (arg0, 1);
8090                   tmp = build_function_call_expr (tanfn, tmp);
8091                   return fold (build2 (RDIV_EXPR, type,
8092                                        build_real (type, dconst1), tmp));
8093                 }
8094             }
8095
8096           /* Optimize pow(x,c)/x as pow(x,c-1).  */
8097           if (fcode0 == BUILT_IN_POW
8098               || fcode0 == BUILT_IN_POWF
8099               || fcode0 == BUILT_IN_POWL)
8100             {
8101               tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8102               tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
8103               if (TREE_CODE (arg01) == REAL_CST
8104                   && ! TREE_CONSTANT_OVERFLOW (arg01)
8105                   && operand_equal_p (arg1, arg00, 0))
8106                 {
8107                   tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8108                   REAL_VALUE_TYPE c;
8109                   tree arg, arglist;
8110
8111                   c = TREE_REAL_CST (arg01);
8112                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
8113                   arg = build_real (type, c);
8114                   arglist = build_tree_list (NULL_TREE, arg);
8115                   arglist = tree_cons (NULL_TREE, arg1, arglist);
8116                   return build_function_call_expr (powfn, arglist);
8117                 }
8118             }
8119         }
8120       goto binary;
8121
8122     case TRUNC_DIV_EXPR:
8123     case ROUND_DIV_EXPR:
8124     case FLOOR_DIV_EXPR:
8125     case CEIL_DIV_EXPR:
8126     case EXACT_DIV_EXPR:
8127       if (integer_onep (arg1))
8128         return non_lvalue (fold_convert (type, arg0));
8129       if (integer_zerop (arg1))
8130         return t;
8131       /* X / -1 is -X.  */
8132       if (!TYPE_UNSIGNED (type)
8133           && TREE_CODE (arg1) == INTEGER_CST
8134           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
8135           && TREE_INT_CST_HIGH (arg1) == -1)
8136         return fold_convert (type, negate_expr (arg0));
8137
8138       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
8139          operation, EXACT_DIV_EXPR.
8140
8141          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
8142          At one time others generated faster code, it's not clear if they do
8143          after the last round to changes to the DIV code in expmed.c.  */
8144       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
8145           && multiple_of_p (type, arg0, arg1))
8146         return fold (build2 (EXACT_DIV_EXPR, type, arg0, arg1));
8147
8148       if (TREE_CODE (arg1) == INTEGER_CST
8149           && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
8150                                          code, NULL_TREE)))
8151         return fold_convert (type, tem);
8152
8153       goto binary;
8154
8155     case CEIL_MOD_EXPR:
8156     case FLOOR_MOD_EXPR:
8157     case ROUND_MOD_EXPR:
8158     case TRUNC_MOD_EXPR:
8159       /* X % 1 is always zero, but be sure to preserve any side
8160          effects in X.  */
8161       if (integer_onep (arg1))
8162         return omit_one_operand (type, integer_zero_node, arg0);
8163
8164       /* X % 0, return X % 0 unchanged so that we can get the
8165          proper warnings and errors.  */
8166       if (integer_zerop (arg1))
8167         return t;
8168
8169       /* 0 % X is always zero, but be sure to preserve any side
8170          effects in X.  Place this after checking for X == 0.  */
8171       if (integer_zerop (arg0))
8172         return omit_one_operand (type, integer_zero_node, arg1);
8173
8174       /* X % -1 is zero.  */
8175       if (!TYPE_UNSIGNED (type)
8176           && TREE_CODE (arg1) == INTEGER_CST
8177           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
8178           && TREE_INT_CST_HIGH (arg1) == -1)
8179         return omit_one_operand (type, integer_zero_node, arg0);
8180
8181       /* Optimize unsigned TRUNC_MOD_EXPR by a power of two into a
8182          BIT_AND_EXPR, i.e. "X % C" into "X & C2".  */
8183       if (code == TRUNC_MOD_EXPR
8184           && TYPE_UNSIGNED (type)
8185           && integer_pow2p (arg1))
8186         {
8187           unsigned HOST_WIDE_INT high, low;
8188           tree mask;
8189           int l;
8190
8191           l = tree_log2 (arg1);
8192           if (l >= HOST_BITS_PER_WIDE_INT)
8193             {
8194               high = ((unsigned HOST_WIDE_INT) 1
8195                       << (l - HOST_BITS_PER_WIDE_INT)) - 1;
8196               low = -1;
8197             }
8198           else
8199             {
8200               high = 0;
8201               low = ((unsigned HOST_WIDE_INT) 1 << l) - 1;
8202             }
8203
8204           mask = build_int_cst_wide (type, low, high);
8205           return fold (build2 (BIT_AND_EXPR, type,
8206                                fold_convert (type, arg0), mask));
8207         }
8208
8209       /* X % -C is the same as X % C.  */
8210       if (code == TRUNC_MOD_EXPR
8211           && !TYPE_UNSIGNED (type)
8212           && TREE_CODE (arg1) == INTEGER_CST
8213           && TREE_INT_CST_HIGH (arg1) < 0
8214           && !flag_trapv
8215           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
8216           && !sign_bit_p (arg1, arg1))
8217         return fold (build2 (code, type, fold_convert (type, arg0),
8218                              fold_convert (type, negate_expr (arg1))));
8219
8220       /* X % -Y is the same as X % Y.  */
8221       if (code == TRUNC_MOD_EXPR
8222           && !TYPE_UNSIGNED (type)
8223           && TREE_CODE (arg1) == NEGATE_EXPR
8224           && !flag_trapv)
8225         return fold (build2 (code, type, fold_convert (type, arg0),
8226                              fold_convert (type, TREE_OPERAND (arg1, 0))));
8227
8228       if (TREE_CODE (arg1) == INTEGER_CST
8229           && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
8230                                          code, NULL_TREE)))
8231         return fold_convert (type, tem);
8232
8233       goto binary;
8234
8235     case LROTATE_EXPR:
8236     case RROTATE_EXPR:
8237       if (integer_all_onesp (arg0))
8238         return omit_one_operand (type, arg0, arg1);
8239       goto shift;
8240
8241     case RSHIFT_EXPR:
8242       /* Optimize -1 >> x for arithmetic right shifts.  */
8243       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
8244         return omit_one_operand (type, arg0, arg1);
8245       /* ... fall through ...  */
8246
8247     case LSHIFT_EXPR:
8248     shift:
8249       if (integer_zerop (arg1))
8250         return non_lvalue (fold_convert (type, arg0));
8251       if (integer_zerop (arg0))
8252         return omit_one_operand (type, arg0, arg1);
8253
8254       /* Since negative shift count is not well-defined,
8255          don't try to compute it in the compiler.  */
8256       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
8257         return t;
8258       /* Rewrite an LROTATE_EXPR by a constant into an
8259          RROTATE_EXPR by a new constant.  */
8260       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
8261         {
8262           tree tem = build_int_cst (NULL_TREE,
8263                                     GET_MODE_BITSIZE (TYPE_MODE (type)));
8264           tem = fold_convert (TREE_TYPE (arg1), tem);
8265           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
8266           return fold (build2 (RROTATE_EXPR, type, arg0, tem));
8267         }
8268
8269       /* If we have a rotate of a bit operation with the rotate count and
8270          the second operand of the bit operation both constant,
8271          permute the two operations.  */
8272       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
8273           && (TREE_CODE (arg0) == BIT_AND_EXPR
8274               || TREE_CODE (arg0) == BIT_IOR_EXPR
8275               || TREE_CODE (arg0) == BIT_XOR_EXPR)
8276           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8277         return fold (build2 (TREE_CODE (arg0), type,
8278                              fold (build2 (code, type,
8279                                            TREE_OPERAND (arg0, 0), arg1)),
8280                              fold (build2 (code, type,
8281                                            TREE_OPERAND (arg0, 1), arg1))));
8282
8283       /* Two consecutive rotates adding up to the width of the mode can
8284          be ignored.  */
8285       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
8286           && TREE_CODE (arg0) == RROTATE_EXPR
8287           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8288           && TREE_INT_CST_HIGH (arg1) == 0
8289           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
8290           && ((TREE_INT_CST_LOW (arg1)
8291                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
8292               == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
8293         return TREE_OPERAND (arg0, 0);
8294
8295       goto binary;
8296
8297     case MIN_EXPR:
8298       if (operand_equal_p (arg0, arg1, 0))
8299         return omit_one_operand (type, arg0, arg1);
8300       if (INTEGRAL_TYPE_P (type)
8301           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
8302         return omit_one_operand (type, arg1, arg0);
8303       goto associate;
8304
8305     case MAX_EXPR:
8306       if (operand_equal_p (arg0, arg1, 0))
8307         return omit_one_operand (type, arg0, arg1);
8308       if (INTEGRAL_TYPE_P (type)
8309           && TYPE_MAX_VALUE (type)
8310           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
8311         return omit_one_operand (type, arg1, arg0);
8312       goto associate;
8313
8314     case TRUTH_NOT_EXPR:
8315       /* The argument to invert_truthvalue must have Boolean type.  */
8316       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8317           arg0 = fold_convert (boolean_type_node, arg0);
8318
8319       /* Note that the operand of this must be an int
8320          and its values must be 0 or 1.
8321          ("true" is a fixed value perhaps depending on the language,
8322          but we don't handle values other than 1 correctly yet.)  */
8323       tem = invert_truthvalue (arg0);
8324       /* Avoid infinite recursion.  */
8325       if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
8326         return t;
8327       return fold_convert (type, tem);
8328
8329     case TRUTH_ANDIF_EXPR:
8330       /* Note that the operands of this must be ints
8331          and their values must be 0 or 1.
8332          ("true" is a fixed value perhaps depending on the language.)  */
8333       /* If first arg is constant zero, return it.  */
8334       if (integer_zerop (arg0))
8335         return fold_convert (type, arg0);
8336     case TRUTH_AND_EXPR:
8337       /* If either arg is constant true, drop it.  */
8338       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
8339         return non_lvalue (fold_convert (type, arg1));
8340       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
8341           /* Preserve sequence points.  */
8342           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
8343         return non_lvalue (fold_convert (type, arg0));
8344       /* If second arg is constant zero, result is zero, but first arg
8345          must be evaluated.  */
8346       if (integer_zerop (arg1))
8347         return omit_one_operand (type, arg1, arg0);
8348       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
8349          case will be handled here.  */
8350       if (integer_zerop (arg0))
8351         return omit_one_operand (type, arg0, arg1);
8352
8353       /* !X && X is always false.  */
8354       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
8355           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8356         return omit_one_operand (type, integer_zero_node, arg1);
8357       /* X && !X is always false.  */
8358       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
8359           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8360         return omit_one_operand (type, integer_zero_node, arg0);
8361
8362       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
8363          means A >= Y && A != MAX, but in this case we know that
8364          A < X <= MAX.  */
8365
8366       if (!TREE_SIDE_EFFECTS (arg0)
8367           && !TREE_SIDE_EFFECTS (arg1))
8368         {
8369           tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
8370           if (tem)
8371             return fold (build2 (code, type, tem, arg1));
8372
8373           tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
8374           if (tem)
8375             return fold (build2 (code, type, arg0, tem));
8376         }
8377
8378     truth_andor:
8379       /* We only do these simplifications if we are optimizing.  */
8380       if (!optimize)
8381         return t;
8382
8383       /* Check for things like (A || B) && (A || C).  We can convert this
8384          to A || (B && C).  Note that either operator can be any of the four
8385          truth and/or operations and the transformation will still be
8386          valid.   Also note that we only care about order for the
8387          ANDIF and ORIF operators.  If B contains side effects, this
8388          might change the truth-value of A.  */
8389       if (TREE_CODE (arg0) == TREE_CODE (arg1)
8390           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8391               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8392               || TREE_CODE (arg0) == TRUTH_AND_EXPR
8393               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8394           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8395         {
8396           tree a00 = TREE_OPERAND (arg0, 0);
8397           tree a01 = TREE_OPERAND (arg0, 1);
8398           tree a10 = TREE_OPERAND (arg1, 0);
8399           tree a11 = TREE_OPERAND (arg1, 1);
8400           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8401                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8402                              && (code == TRUTH_AND_EXPR
8403                                  || code == TRUTH_OR_EXPR));
8404
8405           if (operand_equal_p (a00, a10, 0))
8406             return fold (build2 (TREE_CODE (arg0), type, a00,
8407                                  fold (build2 (code, type, a01, a11))));
8408           else if (commutative && operand_equal_p (a00, a11, 0))
8409             return fold (build2 (TREE_CODE (arg0), type, a00,
8410                                  fold (build2 (code, type, a01, a10))));
8411           else if (commutative && operand_equal_p (a01, a10, 0))
8412             return fold (build2 (TREE_CODE (arg0), type, a01,
8413                                  fold (build2 (code, type, a00, a11))));
8414
8415           /* This case if tricky because we must either have commutative
8416              operators or else A10 must not have side-effects.  */
8417
8418           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8419                    && operand_equal_p (a01, a11, 0))
8420             return fold (build2 (TREE_CODE (arg0), type,
8421                                  fold (build2 (code, type, a00, a10)),
8422                                  a01));
8423         }
8424
8425       /* See if we can build a range comparison.  */
8426       if (0 != (tem = fold_range_test (t)))
8427         return tem;
8428
8429       /* Check for the possibility of merging component references.  If our
8430          lhs is another similar operation, try to merge its rhs with our
8431          rhs.  Then try to merge our lhs and rhs.  */
8432       if (TREE_CODE (arg0) == code
8433           && 0 != (tem = fold_truthop (code, type,
8434                                        TREE_OPERAND (arg0, 1), arg1)))
8435         return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
8436
8437       if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
8438         return tem;
8439
8440       return t;
8441
8442     case TRUTH_ORIF_EXPR:
8443       /* Note that the operands of this must be ints
8444          and their values must be 0 or true.
8445          ("true" is a fixed value perhaps depending on the language.)  */
8446       /* If first arg is constant true, return it.  */
8447       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
8448         return fold_convert (type, arg0);
8449     case TRUTH_OR_EXPR:
8450       /* If either arg is constant zero, drop it.  */
8451       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
8452         return non_lvalue (fold_convert (type, arg1));
8453       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
8454           /* Preserve sequence points.  */
8455           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
8456         return non_lvalue (fold_convert (type, arg0));
8457       /* If second arg is constant true, result is true, but we must
8458          evaluate first arg.  */
8459       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
8460         return omit_one_operand (type, arg1, arg0);
8461       /* Likewise for first arg, but note this only occurs here for
8462          TRUTH_OR_EXPR.  */
8463       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
8464         return omit_one_operand (type, arg0, arg1);
8465
8466       /* !X || X is always true.  */
8467       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
8468           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8469         return omit_one_operand (type, integer_one_node, arg1);
8470       /* X || !X is always true.  */
8471       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
8472           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8473         return omit_one_operand (type, integer_one_node, arg0);
8474
8475       goto truth_andor;
8476
8477     case TRUTH_XOR_EXPR:
8478       /* If the second arg is constant zero, drop it.  */
8479       if (integer_zerop (arg1))
8480         return non_lvalue (fold_convert (type, arg0));
8481       /* If the second arg is constant true, this is a logical inversion.  */
8482       if (integer_onep (arg1))
8483         return non_lvalue (fold_convert (type, invert_truthvalue (arg0)));
8484       /* Identical arguments cancel to zero.  */
8485       if (operand_equal_p (arg0, arg1, 0))
8486         return omit_one_operand (type, integer_zero_node, arg0);
8487
8488       /* !X ^ X is always true.  */
8489       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
8490           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8491         return omit_one_operand (type, integer_one_node, arg1);
8492
8493       /* X ^ !X is always true.  */
8494       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
8495           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8496         return omit_one_operand (type, integer_one_node, arg0);
8497
8498       return t;
8499
8500     case EQ_EXPR:
8501     case NE_EXPR:
8502     case LT_EXPR:
8503     case GT_EXPR:
8504     case LE_EXPR:
8505     case GE_EXPR:
8506       /* If one arg is a real or integer constant, put it last.  */
8507       if (tree_swap_operands_p (arg0, arg1, true))
8508         return fold (build2 (swap_tree_comparison (code), type, arg1, arg0));
8509
8510       /* If this is an equality comparison of the address of a non-weak
8511          object against zero, then we know the result.  */
8512       if ((code == EQ_EXPR || code == NE_EXPR)
8513           && TREE_CODE (arg0) == ADDR_EXPR
8514           && DECL_P (TREE_OPERAND (arg0, 0))
8515           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
8516           && integer_zerop (arg1))
8517         return constant_boolean_node (code != EQ_EXPR, type);
8518
8519       /* If this is an equality comparison of the address of two non-weak,
8520          unaliased symbols neither of which are extern (since we do not
8521          have access to attributes for externs), then we know the result.  */
8522       if ((code == EQ_EXPR || code == NE_EXPR)
8523           && TREE_CODE (arg0) == ADDR_EXPR
8524           && DECL_P (TREE_OPERAND (arg0, 0))
8525           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
8526           && ! lookup_attribute ("alias",
8527                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
8528           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
8529           && TREE_CODE (arg1) == ADDR_EXPR
8530           && DECL_P (TREE_OPERAND (arg1, 0))
8531           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
8532           && ! lookup_attribute ("alias",
8533                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
8534           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
8535         return constant_boolean_node (operand_equal_p (arg0, arg1, 0)
8536                                       ? code == EQ_EXPR : code != EQ_EXPR,
8537                                       type);
8538
8539       /* If this is a comparison of two exprs that look like an
8540          ARRAY_REF of the same object, then we can fold this to a
8541          comparison of the two offsets.  */
8542       if (COMPARISON_CLASS_P (t))
8543         {
8544           tree base0, offset0, base1, offset1;
8545
8546           if (extract_array_ref (arg0, &base0, &offset0)
8547               && extract_array_ref (arg1, &base1, &offset1)
8548               && operand_equal_p (base0, base1, 0))
8549             {
8550               if (offset0 == NULL_TREE
8551                   && offset1 == NULL_TREE)
8552                 {
8553                   offset0 = integer_zero_node;
8554                   offset1 = integer_zero_node;
8555                 }
8556               else if (offset0 == NULL_TREE)
8557                 offset0 = build_int_cst (TREE_TYPE (offset1), 0);
8558               else if (offset1 == NULL_TREE)
8559                 offset1 = build_int_cst (TREE_TYPE (offset0), 0);
8560
8561               if (TREE_TYPE (offset0) == TREE_TYPE (offset1))
8562                 return fold (build2 (code, type, offset0, offset1));
8563             }
8564         }
8565
8566       if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8567         {
8568           tree targ0 = strip_float_extensions (arg0);
8569           tree targ1 = strip_float_extensions (arg1);
8570           tree newtype = TREE_TYPE (targ0);
8571
8572           if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8573             newtype = TREE_TYPE (targ1);
8574
8575           /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
8576           if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8577             return fold (build2 (code, type, fold_convert (newtype, targ0),
8578                                  fold_convert (newtype, targ1)));
8579
8580           /* (-a) CMP (-b) -> b CMP a  */
8581           if (TREE_CODE (arg0) == NEGATE_EXPR
8582               && TREE_CODE (arg1) == NEGATE_EXPR)
8583             return fold (build2 (code, type, TREE_OPERAND (arg1, 0),
8584                                  TREE_OPERAND (arg0, 0)));
8585
8586           if (TREE_CODE (arg1) == REAL_CST)
8587           {
8588             REAL_VALUE_TYPE cst;
8589             cst = TREE_REAL_CST (arg1);
8590
8591             /* (-a) CMP CST -> a swap(CMP) (-CST)  */
8592             if (TREE_CODE (arg0) == NEGATE_EXPR)
8593               return
8594                 fold (build2 (swap_tree_comparison (code), type,
8595                               TREE_OPERAND (arg0, 0),
8596                               build_real (TREE_TYPE (arg1),
8597                                           REAL_VALUE_NEGATE (cst))));
8598
8599             /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
8600             /* a CMP (-0) -> a CMP 0  */
8601             if (REAL_VALUE_MINUS_ZERO (cst))
8602               return fold (build2 (code, type, arg0,
8603                                    build_real (TREE_TYPE (arg1), dconst0)));
8604
8605             /* x != NaN is always true, other ops are always false.  */
8606             if (REAL_VALUE_ISNAN (cst)
8607                 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8608               {
8609                 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8610                 return omit_one_operand (type, tem, arg0);
8611               }
8612
8613             /* Fold comparisons against infinity.  */
8614             if (REAL_VALUE_ISINF (cst))
8615               {
8616                 tem = fold_inf_compare (code, type, arg0, arg1);
8617                 if (tem != NULL_TREE)
8618                   return tem;
8619               }
8620           }
8621
8622           /* If this is a comparison of a real constant with a PLUS_EXPR
8623              or a MINUS_EXPR of a real constant, we can convert it into a
8624              comparison with a revised real constant as long as no overflow
8625              occurs when unsafe_math_optimizations are enabled.  */
8626           if (flag_unsafe_math_optimizations
8627               && TREE_CODE (arg1) == REAL_CST
8628               && (TREE_CODE (arg0) == PLUS_EXPR
8629                   || TREE_CODE (arg0) == MINUS_EXPR)
8630               && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
8631               && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
8632                                           ? MINUS_EXPR : PLUS_EXPR,
8633                                           arg1, TREE_OPERAND (arg0, 1), 0))
8634               && ! TREE_CONSTANT_OVERFLOW (tem))
8635             return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
8636
8637           /* Likewise, we can simplify a comparison of a real constant with
8638              a MINUS_EXPR whose first operand is also a real constant, i.e.
8639              (c1 - x) < c2 becomes x > c1-c2.  */
8640           if (flag_unsafe_math_optimizations
8641               && TREE_CODE (arg1) == REAL_CST
8642               && TREE_CODE (arg0) == MINUS_EXPR
8643               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
8644               && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
8645                                           arg1, 0))
8646               && ! TREE_CONSTANT_OVERFLOW (tem))
8647             return fold (build2 (swap_tree_comparison (code), type,
8648                                  TREE_OPERAND (arg0, 1), tem));
8649
8650           /* Fold comparisons against built-in math functions.  */
8651           if (TREE_CODE (arg1) == REAL_CST
8652               && flag_unsafe_math_optimizations
8653               && ! flag_errno_math)
8654             {
8655               enum built_in_function fcode = builtin_mathfn_code (arg0);
8656
8657               if (fcode != END_BUILTINS)
8658                 {
8659                   tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
8660                   if (tem != NULL_TREE)
8661                     return tem;
8662                 }
8663             }
8664         }
8665
8666       /* Convert foo++ == CONST into ++foo == CONST + INCR.  */
8667       if (TREE_CONSTANT (arg1)
8668           && (TREE_CODE (arg0) == POSTINCREMENT_EXPR
8669               || TREE_CODE (arg0) == POSTDECREMENT_EXPR)
8670           /* This optimization is invalid for ordered comparisons
8671              if CONST+INCR overflows or if foo+incr might overflow.
8672              This optimization is invalid for floating point due to rounding.
8673              For pointer types we assume overflow doesn't happen.  */
8674           && (POINTER_TYPE_P (TREE_TYPE (arg0))
8675               || (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8676                   && (code == EQ_EXPR || code == NE_EXPR))))
8677         {
8678           tree varop, newconst;
8679
8680           if (TREE_CODE (arg0) == POSTINCREMENT_EXPR)
8681             {
8682               newconst = fold (build2 (PLUS_EXPR, TREE_TYPE (arg0),
8683                                        arg1, TREE_OPERAND (arg0, 1)));
8684               varop = build2 (PREINCREMENT_EXPR, TREE_TYPE (arg0),
8685                               TREE_OPERAND (arg0, 0),
8686                               TREE_OPERAND (arg0, 1));
8687             }
8688           else
8689             {
8690               newconst = fold (build2 (MINUS_EXPR, TREE_TYPE (arg0),
8691                                        arg1, TREE_OPERAND (arg0, 1)));
8692               varop = build2 (PREDECREMENT_EXPR, TREE_TYPE (arg0),
8693                               TREE_OPERAND (arg0, 0),
8694                               TREE_OPERAND (arg0, 1));
8695             }
8696
8697
8698           /* If VAROP is a reference to a bitfield, we must mask
8699              the constant by the width of the field.  */
8700           if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
8701               && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (varop, 0), 1))
8702               && host_integerp (DECL_SIZE (TREE_OPERAND
8703                                            (TREE_OPERAND (varop, 0), 1)), 1))
8704             {
8705               tree fielddecl = TREE_OPERAND (TREE_OPERAND (varop, 0), 1);
8706               HOST_WIDE_INT size = tree_low_cst (DECL_SIZE (fielddecl), 1);
8707               tree folded_compare, shift;
8708
8709               /* First check whether the comparison would come out
8710                  always the same.  If we don't do that we would
8711                  change the meaning with the masking.  */
8712               folded_compare = fold (build2 (code, type,
8713                                              TREE_OPERAND (varop, 0), arg1));
8714               if (integer_zerop (folded_compare)
8715                   || integer_onep (folded_compare))
8716                 return omit_one_operand (type, folded_compare, varop);
8717
8718               shift = build_int_cst (NULL_TREE,
8719                                      TYPE_PRECISION (TREE_TYPE (varop)) - size);
8720               shift = fold_convert (TREE_TYPE (varop), shift);
8721               newconst = fold (build2 (LSHIFT_EXPR, TREE_TYPE (varop),
8722                                        newconst, shift));
8723               newconst = fold (build2 (RSHIFT_EXPR, TREE_TYPE (varop),
8724                                        newconst, shift));
8725             }
8726
8727           return fold (build2 (code, type, varop, newconst));
8728         }
8729
8730       /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
8731          This transformation affects the cases which are handled in later
8732          optimizations involving comparisons with non-negative constants.  */
8733       if (TREE_CODE (arg1) == INTEGER_CST
8734           && TREE_CODE (arg0) != INTEGER_CST
8735           && tree_int_cst_sgn (arg1) > 0)
8736         {
8737           switch (code)
8738             {
8739             case GE_EXPR:
8740               arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
8741               return fold (build2 (GT_EXPR, type, arg0, arg1));
8742
8743             case LT_EXPR:
8744               arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
8745               return fold (build2 (LE_EXPR, type, arg0, arg1));
8746
8747             default:
8748               break;
8749             }
8750         }
8751
8752       /* Comparisons with the highest or lowest possible integer of
8753          the specified size will have known values.
8754
8755          This is quite similar to fold_relational_hi_lo, however,
8756          attempts to share the code have been nothing but trouble.  */
8757       {
8758         int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
8759
8760         if (TREE_CODE (arg1) == INTEGER_CST
8761             && ! TREE_CONSTANT_OVERFLOW (arg1)
8762             && width <= 2 * HOST_BITS_PER_WIDE_INT
8763             && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
8764                 || POINTER_TYPE_P (TREE_TYPE (arg1))))
8765           {
8766             HOST_WIDE_INT signed_max_hi;
8767             unsigned HOST_WIDE_INT signed_max_lo;
8768             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
8769
8770             if (width <= HOST_BITS_PER_WIDE_INT)
8771               {
8772                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
8773                                 - 1;
8774                 signed_max_hi = 0;
8775                 max_hi = 0;
8776
8777                 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
8778                   {
8779                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
8780                     min_lo = 0;
8781                     min_hi = 0;
8782                   }
8783                 else
8784                   {
8785                     max_lo = signed_max_lo;
8786                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
8787                     min_hi = -1;
8788                   }
8789               }
8790             else
8791               {
8792                 width -= HOST_BITS_PER_WIDE_INT;
8793                 signed_max_lo = -1;
8794                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
8795                                 - 1;
8796                 max_lo = -1;
8797                 min_lo = 0;
8798
8799                 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
8800                   {
8801                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
8802                     min_hi = 0;
8803                   }
8804                 else
8805                   {
8806                     max_hi = signed_max_hi;
8807                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
8808                   }
8809               }
8810
8811             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
8812                 && TREE_INT_CST_LOW (arg1) == max_lo)
8813               switch (code)
8814                 {
8815                 case GT_EXPR:
8816                   return omit_one_operand (type, integer_zero_node, arg0);
8817
8818                 case GE_EXPR:
8819                   return fold (build2 (EQ_EXPR, type, arg0, arg1));
8820
8821                 case LE_EXPR:
8822                   return omit_one_operand (type, integer_one_node, arg0);
8823
8824                 case LT_EXPR:
8825                   return fold (build2 (NE_EXPR, type, arg0, arg1));
8826
8827                 /* The GE_EXPR and LT_EXPR cases above are not normally
8828                    reached because of previous transformations.  */
8829
8830                 default:
8831                   break;
8832                 }
8833             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
8834                      == max_hi
8835                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
8836               switch (code)
8837                 {
8838                 case GT_EXPR:
8839                   arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
8840                   return fold (build2 (EQ_EXPR, type, arg0, arg1));
8841                 case LE_EXPR:
8842                   arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
8843                   return fold (build2 (NE_EXPR, type, arg0, arg1));
8844                 default:
8845                   break;
8846                 }
8847             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
8848                      == min_hi
8849                      && TREE_INT_CST_LOW (arg1) == min_lo)
8850               switch (code)
8851                 {
8852                 case LT_EXPR:
8853                   return omit_one_operand (type, integer_zero_node, arg0);
8854
8855                 case LE_EXPR:
8856                   return fold (build2 (EQ_EXPR, type, arg0, arg1));
8857
8858                 case GE_EXPR:
8859                   return omit_one_operand (type, integer_one_node, arg0);
8860
8861                 case GT_EXPR:
8862                   return fold (build2 (NE_EXPR, type, arg0, arg1));
8863
8864                 default:
8865                   break;
8866                 }
8867             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
8868                      == min_hi
8869                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
8870               switch (code)
8871                 {
8872                 case GE_EXPR:
8873                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
8874                   return fold (build2 (NE_EXPR, type, arg0, arg1));
8875                 case LT_EXPR:
8876                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
8877                   return fold (build2 (EQ_EXPR, type, arg0, arg1));
8878                 default:
8879                   break;
8880                 }
8881
8882             else if (!in_gimple_form
8883                      && TREE_INT_CST_HIGH (arg1) == signed_max_hi
8884                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
8885                      && TYPE_UNSIGNED (TREE_TYPE (arg1))
8886                      /* signed_type does not work on pointer types.  */
8887                      && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
8888               {
8889                 /* The following case also applies to X < signed_max+1
8890                    and X >= signed_max+1 because previous transformations.  */
8891                 if (code == LE_EXPR || code == GT_EXPR)
8892                   {
8893                     tree st0, st1;
8894                     st0 = lang_hooks.types.signed_type (TREE_TYPE (arg0));
8895                     st1 = lang_hooks.types.signed_type (TREE_TYPE (arg1));
8896                     return fold
8897                       (build2 (code == LE_EXPR ? GE_EXPR: LT_EXPR,
8898                                type, fold_convert (st0, arg0),
8899                                fold_convert (st1, integer_zero_node)));
8900                   }
8901               }
8902           }
8903       }
8904
8905       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
8906          a MINUS_EXPR of a constant, we can convert it into a comparison with
8907          a revised constant as long as no overflow occurs.  */
8908       if ((code == EQ_EXPR || code == NE_EXPR)
8909           && TREE_CODE (arg1) == INTEGER_CST
8910           && (TREE_CODE (arg0) == PLUS_EXPR
8911               || TREE_CODE (arg0) == MINUS_EXPR)
8912           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8913           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
8914                                       ? MINUS_EXPR : PLUS_EXPR,
8915                                       arg1, TREE_OPERAND (arg0, 1), 0))
8916           && ! TREE_CONSTANT_OVERFLOW (tem))
8917         return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
8918
8919       /* Similarly for a NEGATE_EXPR.  */
8920       else if ((code == EQ_EXPR || code == NE_EXPR)
8921                && TREE_CODE (arg0) == NEGATE_EXPR
8922                && TREE_CODE (arg1) == INTEGER_CST
8923                && 0 != (tem = negate_expr (arg1))
8924                && TREE_CODE (tem) == INTEGER_CST
8925                && ! TREE_CONSTANT_OVERFLOW (tem))
8926         return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
8927
8928       /* If we have X - Y == 0, we can convert that to X == Y and similarly
8929          for !=.  Don't do this for ordered comparisons due to overflow.  */
8930       else if ((code == NE_EXPR || code == EQ_EXPR)
8931                && integer_zerop (arg1) && TREE_CODE (arg0) == MINUS_EXPR)
8932         return fold (build2 (code, type,
8933                              TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)));
8934
8935       else if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
8936                && TREE_CODE (arg0) == NOP_EXPR)
8937         {
8938           /* If we are widening one operand of an integer comparison,
8939              see if the other operand is similarly being widened.  Perhaps we
8940              can do the comparison in the narrower type.  */
8941           tem = fold_widened_comparison (code, type, arg0, arg1);
8942           if (tem)
8943             return tem;
8944
8945           /* Or if we are changing signedness.  */
8946           tem = fold_sign_changed_comparison (code, type, arg0, arg1);
8947           if (tem)
8948             return tem;
8949         }
8950
8951       /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8952          constant, we can simplify it.  */
8953       else if (TREE_CODE (arg1) == INTEGER_CST
8954                && (TREE_CODE (arg0) == MIN_EXPR
8955                    || TREE_CODE (arg0) == MAX_EXPR)
8956                && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8957         return optimize_minmax_comparison (t);
8958
8959       /* If we are comparing an ABS_EXPR with a constant, we can
8960          convert all the cases into explicit comparisons, but they may
8961          well not be faster than doing the ABS and one comparison.
8962          But ABS (X) <= C is a range comparison, which becomes a subtraction
8963          and a comparison, and is probably faster.  */
8964       else if (code == LE_EXPR && TREE_CODE (arg1) == INTEGER_CST
8965                && TREE_CODE (arg0) == ABS_EXPR
8966                && ! TREE_SIDE_EFFECTS (arg0)
8967                && (0 != (tem = negate_expr (arg1)))
8968                && TREE_CODE (tem) == INTEGER_CST
8969                && ! TREE_CONSTANT_OVERFLOW (tem))
8970         return fold (build2 (TRUTH_ANDIF_EXPR, type,
8971                              build2 (GE_EXPR, type,
8972                                      TREE_OPERAND (arg0, 0), tem),
8973                              build2 (LE_EXPR, type,
8974                                      TREE_OPERAND (arg0, 0), arg1)));
8975
8976       /* If this is an EQ or NE comparison with zero and ARG0 is
8977          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
8978          two operations, but the latter can be done in one less insn
8979          on machines that have only two-operand insns or on which a
8980          constant cannot be the first operand.  */
8981       if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
8982           && TREE_CODE (arg0) == BIT_AND_EXPR)
8983         {
8984           tree arg00 = TREE_OPERAND (arg0, 0);
8985           tree arg01 = TREE_OPERAND (arg0, 1);
8986           if (TREE_CODE (arg00) == LSHIFT_EXPR
8987               && integer_onep (TREE_OPERAND (arg00, 0)))
8988             return
8989               fold (build2 (code, type,
8990                             build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
8991                                     build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
8992                                             arg01, TREE_OPERAND (arg00, 1)),
8993                                     fold_convert (TREE_TYPE (arg0),
8994                                                   integer_one_node)),
8995                             arg1));
8996           else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
8997                    && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
8998             return
8999               fold (build2 (code, type,
9000                             build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9001                                     build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
9002                                             arg00, TREE_OPERAND (arg01, 1)),
9003                                     fold_convert (TREE_TYPE (arg0),
9004                                                   integer_one_node)),
9005                             arg1));
9006         }
9007
9008       /* If this is an NE or EQ comparison of zero against the result of a
9009          signed MOD operation whose second operand is a power of 2, make
9010          the MOD operation unsigned since it is simpler and equivalent.  */
9011       if ((code == NE_EXPR || code == EQ_EXPR)
9012           && integer_zerop (arg1)
9013           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
9014           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
9015               || TREE_CODE (arg0) == CEIL_MOD_EXPR
9016               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
9017               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
9018           && integer_pow2p (TREE_OPERAND (arg0, 1)))
9019         {
9020           tree newtype = lang_hooks.types.unsigned_type (TREE_TYPE (arg0));
9021           tree newmod = fold (build2 (TREE_CODE (arg0), newtype,
9022                                       fold_convert (newtype,
9023                                                     TREE_OPERAND (arg0, 0)),
9024                                       fold_convert (newtype,
9025                                                     TREE_OPERAND (arg0, 1))));
9026
9027           return fold (build2 (code, type, newmod,
9028                                fold_convert (newtype, arg1)));
9029         }
9030
9031       /* If this is an NE comparison of zero with an AND of one, remove the
9032          comparison since the AND will give the correct value.  */
9033       if (code == NE_EXPR && integer_zerop (arg1)
9034           && TREE_CODE (arg0) == BIT_AND_EXPR
9035           && integer_onep (TREE_OPERAND (arg0, 1)))
9036         return fold_convert (type, arg0);
9037
9038       /* If we have (A & C) == C where C is a power of 2, convert this into
9039          (A & C) != 0.  Similarly for NE_EXPR.  */
9040       if ((code == EQ_EXPR || code == NE_EXPR)
9041           && TREE_CODE (arg0) == BIT_AND_EXPR
9042           && integer_pow2p (TREE_OPERAND (arg0, 1))
9043           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
9044         return fold (build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
9045                              arg0, fold_convert (TREE_TYPE (arg0),
9046                                                  integer_zero_node)));
9047
9048       /* If we have (A & C) != 0 or (A & C) == 0 and C is a power of
9049          2, then fold the expression into shifts and logical operations.  */
9050       tem = fold_single_bit_test (code, arg0, arg1, type);
9051       if (tem)
9052         return tem;
9053
9054       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
9055          Similarly for NE_EXPR.  */
9056       if ((code == EQ_EXPR || code == NE_EXPR)
9057           && TREE_CODE (arg0) == BIT_AND_EXPR
9058           && TREE_CODE (arg1) == INTEGER_CST
9059           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9060         {
9061           tree notc = fold (build1 (BIT_NOT_EXPR,
9062                                     TREE_TYPE (TREE_OPERAND (arg0, 1)),
9063                                     TREE_OPERAND (arg0, 1)));
9064           tree dandnotc = fold (build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9065                                         arg1, notc));
9066           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
9067           if (integer_nonzerop (dandnotc))
9068             return omit_one_operand (type, rslt, arg0);
9069         }
9070
9071       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
9072          Similarly for NE_EXPR.  */
9073       if ((code == EQ_EXPR || code == NE_EXPR)
9074           && TREE_CODE (arg0) == BIT_IOR_EXPR
9075           && TREE_CODE (arg1) == INTEGER_CST
9076           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9077         {
9078           tree notd = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1));
9079           tree candnotd = fold (build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9080                                         TREE_OPERAND (arg0, 1), notd));
9081           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
9082           if (integer_nonzerop (candnotd))
9083             return omit_one_operand (type, rslt, arg0);
9084         }
9085
9086       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
9087          and similarly for >= into !=.  */
9088       if ((code == LT_EXPR || code == GE_EXPR)
9089           && TYPE_UNSIGNED (TREE_TYPE (arg0))
9090           && TREE_CODE (arg1) == LSHIFT_EXPR
9091           && integer_onep (TREE_OPERAND (arg1, 0)))
9092         return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
9093                        build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
9094                                TREE_OPERAND (arg1, 1)),
9095                        fold_convert (TREE_TYPE (arg0), integer_zero_node));
9096
9097       else if ((code == LT_EXPR || code == GE_EXPR)
9098                && TYPE_UNSIGNED (TREE_TYPE (arg0))
9099                && (TREE_CODE (arg1) == NOP_EXPR
9100                    || TREE_CODE (arg1) == CONVERT_EXPR)
9101                && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
9102                && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
9103         return
9104           build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
9105                   fold_convert (TREE_TYPE (arg0),
9106                                 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
9107                                         TREE_OPERAND (TREE_OPERAND (arg1, 0),
9108                                                       1))),
9109                   fold_convert (TREE_TYPE (arg0), integer_zero_node));
9110
9111       /* Simplify comparison of something with itself.  (For IEEE
9112          floating-point, we can only do some of these simplifications.)  */
9113       if (operand_equal_p (arg0, arg1, 0))
9114         {
9115           switch (code)
9116             {
9117             case EQ_EXPR:
9118               if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9119                   || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9120                 return constant_boolean_node (1, type);
9121               break;
9122
9123             case GE_EXPR:
9124             case LE_EXPR:
9125               if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9126                   || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9127                 return constant_boolean_node (1, type);
9128               return fold (build2 (EQ_EXPR, type, arg0, arg1));
9129
9130             case NE_EXPR:
9131               /* For NE, we can only do this simplification if integer
9132                  or we don't honor IEEE floating point NaNs.  */
9133               if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9134                   && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9135                 break;
9136               /* ... fall through ...  */
9137             case GT_EXPR:
9138             case LT_EXPR:
9139               return constant_boolean_node (0, type);
9140             default:
9141               gcc_unreachable ();
9142             }
9143         }
9144
9145       /* If we are comparing an expression that just has comparisons
9146          of two integer values, arithmetic expressions of those comparisons,
9147          and constants, we can simplify it.  There are only three cases
9148          to check: the two values can either be equal, the first can be
9149          greater, or the second can be greater.  Fold the expression for
9150          those three values.  Since each value must be 0 or 1, we have
9151          eight possibilities, each of which corresponds to the constant 0
9152          or 1 or one of the six possible comparisons.
9153
9154          This handles common cases like (a > b) == 0 but also handles
9155          expressions like  ((x > y) - (y > x)) > 0, which supposedly
9156          occur in macroized code.  */
9157
9158       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9159         {
9160           tree cval1 = 0, cval2 = 0;
9161           int save_p = 0;
9162
9163           if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9164               /* Don't handle degenerate cases here; they should already
9165                  have been handled anyway.  */
9166               && cval1 != 0 && cval2 != 0
9167               && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9168               && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9169               && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9170               && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9171               && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9172               && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9173                                     TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9174             {
9175               tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9176               tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9177
9178               /* We can't just pass T to eval_subst in case cval1 or cval2
9179                  was the same as ARG1.  */
9180
9181               tree high_result
9182                 = fold (build2 (code, type,
9183                                 eval_subst (arg0, cval1, maxval,
9184                                             cval2, minval),
9185                                 arg1));
9186               tree equal_result
9187                 = fold (build2 (code, type,
9188                                 eval_subst (arg0, cval1, maxval,
9189                                             cval2, maxval),
9190                                 arg1));
9191               tree low_result
9192                 = fold (build2 (code, type,
9193                                 eval_subst (arg0, cval1, minval,
9194                                             cval2, maxval),
9195                                 arg1));
9196
9197               /* All three of these results should be 0 or 1.  Confirm they
9198                  are.  Then use those values to select the proper code
9199                  to use.  */
9200
9201               if ((integer_zerop (high_result)
9202                    || integer_onep (high_result))
9203                   && (integer_zerop (equal_result)
9204                       || integer_onep (equal_result))
9205                   && (integer_zerop (low_result)
9206                       || integer_onep (low_result)))
9207                 {
9208                   /* Make a 3-bit mask with the high-order bit being the
9209                      value for `>', the next for '=', and the low for '<'.  */
9210                   switch ((integer_onep (high_result) * 4)
9211                           + (integer_onep (equal_result) * 2)
9212                           + integer_onep (low_result))
9213                     {
9214                     case 0:
9215                       /* Always false.  */
9216                       return omit_one_operand (type, integer_zero_node, arg0);
9217                     case 1:
9218                       code = LT_EXPR;
9219                       break;
9220                     case 2:
9221                       code = EQ_EXPR;
9222                       break;
9223                     case 3:
9224                       code = LE_EXPR;
9225                       break;
9226                     case 4:
9227                       code = GT_EXPR;
9228                       break;
9229                     case 5:
9230                       code = NE_EXPR;
9231                       break;
9232                     case 6:
9233                       code = GE_EXPR;
9234                       break;
9235                     case 7:
9236                       /* Always true.  */
9237                       return omit_one_operand (type, integer_one_node, arg0);
9238                     }
9239
9240                   tem = build2 (code, type, cval1, cval2);
9241                   if (save_p)
9242                     return save_expr (tem);
9243                   else
9244                     return fold (tem);
9245                 }
9246             }
9247         }
9248
9249       /* If this is a comparison of a field, we may be able to simplify it.  */
9250       if (((TREE_CODE (arg0) == COMPONENT_REF
9251             && lang_hooks.can_use_bit_fields_p ())
9252            || TREE_CODE (arg0) == BIT_FIELD_REF)
9253           && (code == EQ_EXPR || code == NE_EXPR)
9254           /* Handle the constant case even without -O
9255              to make sure the warnings are given.  */
9256           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
9257         {
9258           t1 = optimize_bit_field_compare (code, type, arg0, arg1);
9259           if (t1)
9260             return t1;
9261         }
9262
9263       /* If this is a comparison of complex values and either or both sides
9264          are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
9265          comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
9266          This may prevent needless evaluations.  */
9267       if ((code == EQ_EXPR || code == NE_EXPR)
9268           && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
9269           && (TREE_CODE (arg0) == COMPLEX_EXPR
9270               || TREE_CODE (arg1) == COMPLEX_EXPR
9271               || TREE_CODE (arg0) == COMPLEX_CST
9272               || TREE_CODE (arg1) == COMPLEX_CST))
9273         {
9274           tree subtype = TREE_TYPE (TREE_TYPE (arg0));
9275           tree real0, imag0, real1, imag1;
9276
9277           arg0 = save_expr (arg0);
9278           arg1 = save_expr (arg1);
9279           real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
9280           imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
9281           real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
9282           imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
9283
9284           return fold (build2 ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
9285                                 : TRUTH_ORIF_EXPR),
9286                                type,
9287                                fold (build2 (code, type, real0, real1)),
9288                                fold (build2 (code, type, imag0, imag1))));
9289         }
9290
9291       /* Optimize comparisons of strlen vs zero to a compare of the
9292          first character of the string vs zero.  To wit,
9293                 strlen(ptr) == 0   =>  *ptr == 0
9294                 strlen(ptr) != 0   =>  *ptr != 0
9295          Other cases should reduce to one of these two (or a constant)
9296          due to the return value of strlen being unsigned.  */
9297       if ((code == EQ_EXPR || code == NE_EXPR)
9298           && integer_zerop (arg1)
9299           && TREE_CODE (arg0) == CALL_EXPR)
9300         {
9301           tree fndecl = get_callee_fndecl (arg0);
9302           tree arglist;
9303
9304           if (fndecl
9305               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
9306               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
9307               && (arglist = TREE_OPERAND (arg0, 1))
9308               && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE
9309               && ! TREE_CHAIN (arglist))
9310             return fold (build2 (code, type,
9311                                  build1 (INDIRECT_REF, char_type_node,
9312                                          TREE_VALUE (arglist)),
9313                                  fold_convert (char_type_node,
9314                                                integer_zero_node)));
9315         }
9316
9317       /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9318          into a single range test.  */
9319       if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9320            || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9321           && TREE_CODE (arg1) == INTEGER_CST
9322           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9323           && !integer_zerop (TREE_OPERAND (arg0, 1))
9324           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9325           && !TREE_OVERFLOW (arg1))
9326         {
9327           t1 = fold_div_compare (code, type, arg0, arg1);
9328           if (t1 != NULL_TREE)
9329             return t1;
9330         }
9331
9332       if ((code == EQ_EXPR || code == NE_EXPR)
9333           && !TREE_SIDE_EFFECTS (arg0)
9334           && integer_zerop (arg1)
9335           && tree_expr_nonzero_p (arg0))
9336         return constant_boolean_node (code==NE_EXPR, type);
9337
9338       t1 = fold_relational_const (code, type, arg0, arg1);
9339       return t1 == NULL_TREE ? t : t1;
9340
9341     case UNORDERED_EXPR:
9342     case ORDERED_EXPR:
9343     case UNLT_EXPR:
9344     case UNLE_EXPR:
9345     case UNGT_EXPR:
9346     case UNGE_EXPR:
9347     case UNEQ_EXPR:
9348     case LTGT_EXPR:
9349       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9350         {
9351           t1 = fold_relational_const (code, type, arg0, arg1);
9352           if (t1 != NULL_TREE)
9353             return t1;
9354         }
9355
9356       /* If the first operand is NaN, the result is constant.  */
9357       if (TREE_CODE (arg0) == REAL_CST
9358           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
9359           && (code != LTGT_EXPR || ! flag_trapping_math))
9360         {
9361           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
9362                ? integer_zero_node
9363                : integer_one_node;
9364           return omit_one_operand (type, t1, arg1);
9365         }
9366
9367       /* If the second operand is NaN, the result is constant.  */
9368       if (TREE_CODE (arg1) == REAL_CST
9369           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
9370           && (code != LTGT_EXPR || ! flag_trapping_math))
9371         {
9372           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
9373                ? integer_zero_node
9374                : integer_one_node;
9375           return omit_one_operand (type, t1, arg0);
9376         }
9377
9378       /* Simplify unordered comparison of something with itself.  */
9379       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
9380           && operand_equal_p (arg0, arg1, 0))
9381         return constant_boolean_node (1, type);
9382
9383       if (code == LTGT_EXPR
9384           && !flag_trapping_math
9385           && operand_equal_p (arg0, arg1, 0))
9386         return constant_boolean_node (0, type);
9387
9388       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
9389       {
9390         tree targ0 = strip_float_extensions (arg0);
9391         tree targ1 = strip_float_extensions (arg1);
9392         tree newtype = TREE_TYPE (targ0);
9393
9394         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9395           newtype = TREE_TYPE (targ1);
9396
9397         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9398           return fold (build2 (code, type, fold_convert (newtype, targ0),
9399                                fold_convert (newtype, targ1)));
9400       }
9401
9402       return t;
9403
9404     case COND_EXPR:
9405       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
9406          so all simple results must be passed through pedantic_non_lvalue.  */
9407       if (TREE_CODE (arg0) == INTEGER_CST)
9408         {
9409           tem = TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1));
9410           /* Only optimize constant conditions when the selected branch
9411              has the same type as the COND_EXPR.  This avoids optimizing
9412              away "c ? x : throw", where the throw has a void type.  */
9413           if (! VOID_TYPE_P (TREE_TYPE (tem))
9414               || VOID_TYPE_P (type))
9415             return pedantic_non_lvalue (tem);
9416           return t;
9417         }
9418       if (operand_equal_p (arg1, TREE_OPERAND (t, 2), 0))
9419         return pedantic_omit_one_operand (type, arg1, arg0);
9420
9421       /* If we have A op B ? A : C, we may be able to convert this to a
9422          simpler expression, depending on the operation and the values
9423          of B and C.  Signed zeros prevent all of these transformations,
9424          for reasons given above each one.
9425
9426          Also try swapping the arguments and inverting the conditional.  */
9427       if (COMPARISON_CLASS_P (arg0)
9428           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
9429                                              arg1, TREE_OPERAND (arg0, 1))
9430           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
9431         {
9432           tem = fold_cond_expr_with_comparison (type, arg0,
9433                                                 TREE_OPERAND (t, 1),
9434                                                 TREE_OPERAND (t, 2));
9435           if (tem)
9436             return tem;
9437         }
9438
9439       if (COMPARISON_CLASS_P (arg0)
9440           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
9441                                              TREE_OPERAND (t, 2),
9442                                              TREE_OPERAND (arg0, 1))
9443           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (TREE_OPERAND (t, 2)))))
9444         {
9445           tem = invert_truthvalue (arg0);
9446           if (COMPARISON_CLASS_P (tem))
9447             {
9448               tem = fold_cond_expr_with_comparison (type, tem,
9449                                                     TREE_OPERAND (t, 2),
9450                                                     TREE_OPERAND (t, 1));
9451               if (tem)
9452                 return tem;
9453             }
9454         }
9455
9456       /* If the second operand is simpler than the third, swap them
9457          since that produces better jump optimization results.  */
9458       if (tree_swap_operands_p (TREE_OPERAND (t, 1),
9459                                 TREE_OPERAND (t, 2), false))
9460         {
9461           /* See if this can be inverted.  If it can't, possibly because
9462              it was a floating-point inequality comparison, don't do
9463              anything.  */
9464           tem = invert_truthvalue (arg0);
9465
9466           if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
9467             return fold (build3 (code, type, tem,
9468                                  TREE_OPERAND (t, 2), TREE_OPERAND (t, 1)));
9469         }
9470
9471       /* Convert A ? 1 : 0 to simply A.  */
9472       if (integer_onep (TREE_OPERAND (t, 1))
9473           && integer_zerop (TREE_OPERAND (t, 2))
9474           /* If we try to convert TREE_OPERAND (t, 0) to our type, the
9475              call to fold will try to move the conversion inside
9476              a COND, which will recurse.  In that case, the COND_EXPR
9477              is probably the best choice, so leave it alone.  */
9478           && type == TREE_TYPE (arg0))
9479         return pedantic_non_lvalue (arg0);
9480
9481       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
9482          over COND_EXPR in cases such as floating point comparisons.  */
9483       if (integer_zerop (TREE_OPERAND (t, 1))
9484           && integer_onep (TREE_OPERAND (t, 2))
9485           && truth_value_p (TREE_CODE (arg0)))
9486         return pedantic_non_lvalue (fold_convert (type,
9487                                                   invert_truthvalue (arg0)));
9488
9489       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
9490       if (TREE_CODE (arg0) == LT_EXPR
9491           && integer_zerop (TREE_OPERAND (arg0, 1))
9492           && integer_zerop (TREE_OPERAND (t, 2))
9493           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
9494         return fold_convert (type, fold (build2 (BIT_AND_EXPR,
9495                                                  TREE_TYPE (tem), tem, arg1)));
9496
9497       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
9498          already handled above.  */
9499       if (TREE_CODE (arg0) == BIT_AND_EXPR
9500           && integer_onep (TREE_OPERAND (arg0, 1))
9501           && integer_zerop (TREE_OPERAND (t, 2))
9502           && integer_pow2p (arg1))
9503         {
9504           tree tem = TREE_OPERAND (arg0, 0);
9505           STRIP_NOPS (tem);
9506           if (TREE_CODE (tem) == RSHIFT_EXPR
9507               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
9508               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
9509                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
9510             return fold (build2 (BIT_AND_EXPR, type,
9511                                  TREE_OPERAND (tem, 0), arg1));
9512         }
9513
9514       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
9515          is probably obsolete because the first operand should be a
9516          truth value (that's why we have the two cases above), but let's
9517          leave it in until we can confirm this for all front-ends.  */
9518       if (integer_zerop (TREE_OPERAND (t, 2))
9519           && TREE_CODE (arg0) == NE_EXPR
9520           && integer_zerop (TREE_OPERAND (arg0, 1))
9521           && integer_pow2p (arg1)
9522           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
9523           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
9524                               arg1, OEP_ONLY_CONST))
9525         return pedantic_non_lvalue (fold_convert (type,
9526                                                   TREE_OPERAND (arg0, 0)));
9527
9528       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
9529       if (integer_zerop (TREE_OPERAND (t, 2))
9530           && truth_value_p (TREE_CODE (arg0))
9531           && truth_value_p (TREE_CODE (arg1)))
9532         return fold (build2 (TRUTH_ANDIF_EXPR, type, arg0, arg1));
9533
9534       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
9535       if (integer_onep (TREE_OPERAND (t, 2))
9536           && truth_value_p (TREE_CODE (arg0))
9537           && truth_value_p (TREE_CODE (arg1)))
9538         {
9539           /* Only perform transformation if ARG0 is easily inverted.  */
9540           tem = invert_truthvalue (arg0);
9541           if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
9542             return fold (build2 (TRUTH_ORIF_EXPR, type, tem, arg1));
9543         }
9544
9545       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
9546       if (integer_zerop (arg1)
9547           && truth_value_p (TREE_CODE (arg0))
9548           && truth_value_p (TREE_CODE (TREE_OPERAND (t, 2))))
9549         {
9550           /* Only perform transformation if ARG0 is easily inverted.  */
9551           tem = invert_truthvalue (arg0);
9552           if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
9553             return fold (build2 (TRUTH_ANDIF_EXPR, type, tem,
9554                                  TREE_OPERAND (t, 2)));
9555         }
9556
9557       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
9558       if (integer_onep (arg1)
9559           && truth_value_p (TREE_CODE (arg0))
9560           && truth_value_p (TREE_CODE (TREE_OPERAND (t, 2))))
9561         return fold (build2 (TRUTH_ORIF_EXPR, type, arg0,
9562                              TREE_OPERAND (t, 2)));
9563
9564       return t;
9565
9566     case COMPOUND_EXPR:
9567       /* When pedantic, a compound expression can be neither an lvalue
9568          nor an integer constant expression.  */
9569       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
9570         return t;
9571       /* Don't let (0, 0) be null pointer constant.  */
9572       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
9573                                  : fold_convert (type, arg1);
9574       return pedantic_non_lvalue (tem);
9575
9576     case COMPLEX_EXPR:
9577       if (wins)
9578         return build_complex (type, arg0, arg1);
9579       return t;
9580
9581     case REALPART_EXPR:
9582       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
9583         return t;
9584       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
9585         return omit_one_operand (type, TREE_OPERAND (arg0, 0),
9586                                  TREE_OPERAND (arg0, 1));
9587       else if (TREE_CODE (arg0) == COMPLEX_CST)
9588         return TREE_REALPART (arg0);
9589       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9590         return fold (build2 (TREE_CODE (arg0), type,
9591                              fold (build1 (REALPART_EXPR, type,
9592                                            TREE_OPERAND (arg0, 0))),
9593                              fold (build1 (REALPART_EXPR, type,
9594                                            TREE_OPERAND (arg0, 1)))));
9595       return t;
9596
9597     case IMAGPART_EXPR:
9598       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
9599         return fold_convert (type, integer_zero_node);
9600       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
9601         return omit_one_operand (type, TREE_OPERAND (arg0, 1),
9602                                  TREE_OPERAND (arg0, 0));
9603       else if (TREE_CODE (arg0) == COMPLEX_CST)
9604         return TREE_IMAGPART (arg0);
9605       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9606         return fold (build2 (TREE_CODE (arg0), type,
9607                              fold (build1 (IMAGPART_EXPR, type,
9608                                            TREE_OPERAND (arg0, 0))),
9609                              fold (build1 (IMAGPART_EXPR, type,
9610                                            TREE_OPERAND (arg0, 1)))));
9611       return t;
9612
9613     case CALL_EXPR:
9614       /* Check for a built-in function.  */
9615       if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
9616           && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
9617               == FUNCTION_DECL)
9618           && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
9619         {
9620           tree tmp = fold_builtin (t, false);
9621           if (tmp)
9622             return tmp;
9623         }
9624       return t;
9625
9626     default:
9627       return t;
9628     } /* switch (code) */
9629 }
9630
9631 #ifdef ENABLE_FOLD_CHECKING
9632 #undef fold
9633
9634 static void fold_checksum_tree (tree, struct md5_ctx *, htab_t);
9635 static void fold_check_failed (tree, tree);
9636 void print_fold_checksum (tree);
9637
9638 /* When --enable-checking=fold, compute a digest of expr before
9639    and after actual fold call to see if fold did not accidentally
9640    change original expr.  */
9641
9642 tree
9643 fold (tree expr)
9644 {
9645   tree ret;
9646   struct md5_ctx ctx;
9647   unsigned char checksum_before[16], checksum_after[16];
9648   htab_t ht;
9649
9650   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
9651   md5_init_ctx (&ctx);
9652   fold_checksum_tree (expr, &ctx, ht);
9653   md5_finish_ctx (&ctx, checksum_before);
9654   htab_empty (ht);
9655
9656   ret = fold_1 (expr);
9657
9658   md5_init_ctx (&ctx);
9659   fold_checksum_tree (expr, &ctx, ht);
9660   md5_finish_ctx (&ctx, checksum_after);
9661   htab_delete (ht);
9662
9663   if (memcmp (checksum_before, checksum_after, 16))
9664     fold_check_failed (expr, ret);
9665
9666   return ret;
9667 }
9668
9669 void
9670 print_fold_checksum (tree expr)
9671 {
9672   struct md5_ctx ctx;
9673   unsigned char checksum[16], cnt;
9674   htab_t ht;
9675
9676   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
9677   md5_init_ctx (&ctx);
9678   fold_checksum_tree (expr, &ctx, ht);
9679   md5_finish_ctx (&ctx, checksum);
9680   htab_delete (ht);
9681   for (cnt = 0; cnt < 16; ++cnt)
9682     fprintf (stderr, "%02x", checksum[cnt]);
9683   putc ('\n', stderr);
9684 }
9685
9686 static void
9687 fold_check_failed (tree expr ATTRIBUTE_UNUSED, tree ret ATTRIBUTE_UNUSED)
9688 {
9689   internal_error ("fold check: original tree changed by fold");
9690 }
9691
9692 static void
9693 fold_checksum_tree (tree expr, struct md5_ctx *ctx, htab_t ht)
9694 {
9695   void **slot;
9696   enum tree_code code;
9697   char buf[sizeof (struct tree_decl)];
9698   int i, len;
9699
9700   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
9701                <= sizeof (struct tree_decl))
9702               && sizeof (struct tree_type) <= sizeof (struct tree_decl));
9703   if (expr == NULL)
9704     return;
9705   slot = htab_find_slot (ht, expr, INSERT);
9706   if (*slot != NULL)
9707     return;
9708   *slot = expr;
9709   code = TREE_CODE (expr);
9710   if (TREE_CODE_CLASS (code) == tcc_declaration
9711       && DECL_ASSEMBLER_NAME_SET_P (expr))
9712     {
9713       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
9714       memcpy (buf, expr, tree_size (expr));
9715       expr = (tree) buf;
9716       SET_DECL_ASSEMBLER_NAME (expr, NULL);
9717     }
9718   else if (TREE_CODE_CLASS (code) == tcc_type
9719            && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)
9720                || TYPE_CACHED_VALUES_P (expr)))
9721     {
9722       /* Allow these fields to be modified.  */
9723       memcpy (buf, expr, tree_size (expr));
9724       expr = (tree) buf;
9725       TYPE_POINTER_TO (expr) = NULL;
9726       TYPE_REFERENCE_TO (expr) = NULL;
9727       TYPE_CACHED_VALUES_P (expr) = 0;
9728       TYPE_CACHED_VALUES (expr) = NULL;
9729     }
9730   md5_process_bytes (expr, tree_size (expr), ctx);
9731   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
9732   if (TREE_CODE_CLASS (code) != tcc_type
9733       && TREE_CODE_CLASS (code) != tcc_declaration)
9734     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
9735   switch (TREE_CODE_CLASS (code))
9736     {
9737     case tcc_constant:
9738       switch (code)
9739         {
9740         case STRING_CST:
9741           md5_process_bytes (TREE_STRING_POINTER (expr),
9742                              TREE_STRING_LENGTH (expr), ctx);
9743           break;
9744         case COMPLEX_CST:
9745           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
9746           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
9747           break;
9748         case VECTOR_CST:
9749           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
9750           break;
9751         default:
9752           break;
9753         }
9754       break;
9755     case tcc_exceptional:
9756       switch (code)
9757         {
9758         case TREE_LIST:
9759           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
9760           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
9761           break;
9762         case TREE_VEC:
9763           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
9764             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
9765           break;
9766         default:
9767           break;
9768         }
9769       break;
9770     case tcc_expression:
9771     case tcc_reference:
9772     case tcc_comparison:
9773     case tcc_unary:
9774     case tcc_binary:
9775     case tcc_statement:
9776       len = TREE_CODE_LENGTH (code);
9777       for (i = 0; i < len; ++i)
9778         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
9779       break;
9780     case tcc_declaration:
9781       fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
9782       fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
9783       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
9784       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
9785       fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
9786       fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
9787       fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
9788       fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
9789       fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
9790       fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
9791       fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
9792       break;
9793     case tcc_type:
9794       if (TREE_CODE (expr) == ENUMERAL_TYPE)
9795         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
9796       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
9797       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
9798       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
9799       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
9800       if (INTEGRAL_TYPE_P (expr)
9801           || SCALAR_FLOAT_TYPE_P (expr))
9802         {
9803           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
9804           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
9805         }
9806       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
9807       if (TREE_CODE (expr) == RECORD_TYPE
9808           || TREE_CODE (expr) == UNION_TYPE
9809           || TREE_CODE (expr) == QUAL_UNION_TYPE)
9810         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
9811       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
9812       break;
9813     default:
9814       break;
9815     }
9816 }
9817
9818 #endif
9819
9820 /* Perform constant folding and related simplification of initializer
9821    expression EXPR.  This behaves identically to "fold" but ignores
9822    potential run-time traps and exceptions that fold must preserve.  */
9823
9824 tree
9825 fold_initializer (tree expr)
9826 {
9827   int saved_signaling_nans = flag_signaling_nans;
9828   int saved_trapping_math = flag_trapping_math;
9829   int saved_rounding_math = flag_rounding_math;
9830   int saved_trapv = flag_trapv;
9831   tree result;
9832
9833   flag_signaling_nans = 0;
9834   flag_trapping_math = 0;
9835   flag_rounding_math = 0;
9836   flag_trapv = 0;
9837
9838   result = fold (expr);
9839
9840   flag_signaling_nans = saved_signaling_nans;
9841   flag_trapping_math = saved_trapping_math;
9842   flag_rounding_math = saved_rounding_math;
9843   flag_trapv = saved_trapv;
9844
9845   return result;
9846 }
9847
9848 /* Determine if first argument is a multiple of second argument.  Return 0 if
9849    it is not, or we cannot easily determined it to be.
9850
9851    An example of the sort of thing we care about (at this point; this routine
9852    could surely be made more general, and expanded to do what the *_DIV_EXPR's
9853    fold cases do now) is discovering that
9854
9855      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
9856
9857    is a multiple of
9858
9859      SAVE_EXPR (J * 8)
9860
9861    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
9862
9863    This code also handles discovering that
9864
9865      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
9866
9867    is a multiple of 8 so we don't have to worry about dealing with a
9868    possible remainder.
9869
9870    Note that we *look* inside a SAVE_EXPR only to determine how it was
9871    calculated; it is not safe for fold to do much of anything else with the
9872    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
9873    at run time.  For example, the latter example above *cannot* be implemented
9874    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
9875    evaluation time of the original SAVE_EXPR is not necessarily the same at
9876    the time the new expression is evaluated.  The only optimization of this
9877    sort that would be valid is changing
9878
9879      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
9880
9881    divided by 8 to
9882
9883      SAVE_EXPR (I) * SAVE_EXPR (J)
9884
9885    (where the same SAVE_EXPR (J) is used in the original and the
9886    transformed version).  */
9887
9888 static int
9889 multiple_of_p (tree type, tree top, tree bottom)
9890 {
9891   if (operand_equal_p (top, bottom, 0))
9892     return 1;
9893
9894   if (TREE_CODE (type) != INTEGER_TYPE)
9895     return 0;
9896
9897   switch (TREE_CODE (top))
9898     {
9899     case BIT_AND_EXPR:
9900       /* Bitwise and provides a power of two multiple.  If the mask is
9901          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
9902       if (!integer_pow2p (bottom))
9903         return 0;
9904       /* FALLTHRU */
9905
9906     case MULT_EXPR:
9907       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
9908               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
9909
9910     case PLUS_EXPR:
9911     case MINUS_EXPR:
9912       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
9913               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
9914
9915     case LSHIFT_EXPR:
9916       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
9917         {
9918           tree op1, t1;
9919
9920           op1 = TREE_OPERAND (top, 1);
9921           /* const_binop may not detect overflow correctly,
9922              so check for it explicitly here.  */
9923           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
9924               > TREE_INT_CST_LOW (op1)
9925               && TREE_INT_CST_HIGH (op1) == 0
9926               && 0 != (t1 = fold_convert (type,
9927                                           const_binop (LSHIFT_EXPR,
9928                                                        size_one_node,
9929                                                        op1, 0)))
9930               && ! TREE_OVERFLOW (t1))
9931             return multiple_of_p (type, t1, bottom);
9932         }
9933       return 0;
9934
9935     case NOP_EXPR:
9936       /* Can't handle conversions from non-integral or wider integral type.  */
9937       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
9938           || (TYPE_PRECISION (type)
9939               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
9940         return 0;
9941
9942       /* .. fall through ...  */
9943
9944     case SAVE_EXPR:
9945       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
9946
9947     case INTEGER_CST:
9948       if (TREE_CODE (bottom) != INTEGER_CST
9949           || (TYPE_UNSIGNED (type)
9950               && (tree_int_cst_sgn (top) < 0
9951                   || tree_int_cst_sgn (bottom) < 0)))
9952         return 0;
9953       return integer_zerop (const_binop (TRUNC_MOD_EXPR,
9954                                          top, bottom, 0));
9955
9956     default:
9957       return 0;
9958     }
9959 }
9960
9961 /* Return true if `t' is known to be non-negative.  */
9962
9963 int
9964 tree_expr_nonnegative_p (tree t)
9965 {
9966   switch (TREE_CODE (t))
9967     {
9968     case ABS_EXPR:
9969       return 1;
9970
9971     case INTEGER_CST:
9972       return tree_int_cst_sgn (t) >= 0;
9973
9974     case REAL_CST:
9975       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
9976
9977     case PLUS_EXPR:
9978       if (FLOAT_TYPE_P (TREE_TYPE (t)))
9979         return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
9980                && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
9981
9982       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
9983          both unsigned and at least 2 bits shorter than the result.  */
9984       if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
9985           && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
9986           && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
9987         {
9988           tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
9989           tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
9990           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
9991               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
9992             {
9993               unsigned int prec = MAX (TYPE_PRECISION (inner1),
9994                                        TYPE_PRECISION (inner2)) + 1;
9995               return prec < TYPE_PRECISION (TREE_TYPE (t));
9996             }
9997         }
9998       break;
9999
10000     case MULT_EXPR:
10001       if (FLOAT_TYPE_P (TREE_TYPE (t)))
10002         {
10003           /* x * x for floating point x is always non-negative.  */
10004           if (operand_equal_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1), 0))
10005             return 1;
10006           return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10007                  && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10008         }
10009
10010       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
10011          both unsigned and their total bits is shorter than the result.  */
10012       if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
10013           && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
10014           && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
10015         {
10016           tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
10017           tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
10018           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
10019               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
10020             return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
10021                    < TYPE_PRECISION (TREE_TYPE (t));
10022         }
10023       return 0;
10024
10025     case TRUNC_DIV_EXPR:
10026     case CEIL_DIV_EXPR:
10027     case FLOOR_DIV_EXPR:
10028     case ROUND_DIV_EXPR:
10029       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10030              && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10031
10032     case TRUNC_MOD_EXPR:
10033     case CEIL_MOD_EXPR:
10034     case FLOOR_MOD_EXPR:
10035     case ROUND_MOD_EXPR:
10036       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10037
10038     case RDIV_EXPR:
10039       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10040              && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10041
10042     case BIT_AND_EXPR:
10043       return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
10044              || tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10045     case BIT_IOR_EXPR:
10046     case BIT_XOR_EXPR:
10047       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10048              && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10049
10050     case NOP_EXPR:
10051       {
10052         tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
10053         tree outer_type = TREE_TYPE (t);
10054
10055         if (TREE_CODE (outer_type) == REAL_TYPE)
10056           {
10057             if (TREE_CODE (inner_type) == REAL_TYPE)
10058               return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10059             if (TREE_CODE (inner_type) == INTEGER_TYPE)
10060               {
10061                 if (TYPE_UNSIGNED (inner_type))
10062                   return 1;
10063                 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10064               }
10065           }
10066         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
10067           {
10068             if (TREE_CODE (inner_type) == REAL_TYPE)
10069               return tree_expr_nonnegative_p (TREE_OPERAND (t,0));
10070             if (TREE_CODE (inner_type) == INTEGER_TYPE)
10071               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
10072                       && TYPE_UNSIGNED (inner_type);
10073           }
10074       }
10075       break;
10076
10077     case COND_EXPR:
10078       return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
10079         && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
10080     case COMPOUND_EXPR:
10081       return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10082     case MIN_EXPR:
10083       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10084         && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10085     case MAX_EXPR:
10086       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10087         || tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10088     case MODIFY_EXPR:
10089       return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10090     case BIND_EXPR:
10091       return tree_expr_nonnegative_p (expr_last (TREE_OPERAND (t, 1)));
10092     case SAVE_EXPR:
10093       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10094     case NON_LVALUE_EXPR:
10095       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10096     case FLOAT_EXPR:
10097       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10098
10099     case TARGET_EXPR:
10100       {
10101         tree temp = TARGET_EXPR_SLOT (t);
10102         t = TARGET_EXPR_INITIAL (t);
10103
10104         /* If the initializer is non-void, then it's a normal expression
10105            that will be assigned to the slot.  */
10106         if (!VOID_TYPE_P (t))
10107           return tree_expr_nonnegative_p (t);
10108
10109         /* Otherwise, the initializer sets the slot in some way.  One common
10110            way is an assignment statement at the end of the initializer.  */
10111         while (1)
10112           {
10113             if (TREE_CODE (t) == BIND_EXPR)
10114               t = expr_last (BIND_EXPR_BODY (t));
10115             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
10116                      || TREE_CODE (t) == TRY_CATCH_EXPR)
10117               t = expr_last (TREE_OPERAND (t, 0));
10118             else if (TREE_CODE (t) == STATEMENT_LIST)
10119               t = expr_last (t);
10120             else
10121               break;
10122           }
10123         if (TREE_CODE (t) == MODIFY_EXPR
10124             && TREE_OPERAND (t, 0) == temp)
10125           return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10126
10127         return 0;
10128       }
10129
10130     case CALL_EXPR:
10131       {
10132         tree fndecl = get_callee_fndecl (t);
10133         tree arglist = TREE_OPERAND (t, 1);
10134         if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
10135           switch (DECL_FUNCTION_CODE (fndecl))
10136             {
10137 #define CASE_BUILTIN_F(BUILT_IN_FN) \
10138   case BUILT_IN_FN: case BUILT_IN_FN##F: case BUILT_IN_FN##L:
10139 #define CASE_BUILTIN_I(BUILT_IN_FN) \
10140   case BUILT_IN_FN: case BUILT_IN_FN##L: case BUILT_IN_FN##LL:
10141
10142             CASE_BUILTIN_F (BUILT_IN_ACOS)
10143             CASE_BUILTIN_F (BUILT_IN_ACOSH)
10144             CASE_BUILTIN_F (BUILT_IN_CABS)
10145             CASE_BUILTIN_F (BUILT_IN_COSH)
10146             CASE_BUILTIN_F (BUILT_IN_ERFC)
10147             CASE_BUILTIN_F (BUILT_IN_EXP)
10148             CASE_BUILTIN_F (BUILT_IN_EXP10)
10149             CASE_BUILTIN_F (BUILT_IN_EXP2)
10150             CASE_BUILTIN_F (BUILT_IN_FABS)
10151             CASE_BUILTIN_F (BUILT_IN_FDIM)
10152             CASE_BUILTIN_F (BUILT_IN_FREXP)
10153             CASE_BUILTIN_F (BUILT_IN_HYPOT)
10154             CASE_BUILTIN_F (BUILT_IN_POW10)
10155             CASE_BUILTIN_I (BUILT_IN_FFS)
10156             CASE_BUILTIN_I (BUILT_IN_PARITY)
10157             CASE_BUILTIN_I (BUILT_IN_POPCOUNT)
10158               /* Always true.  */
10159               return 1;
10160
10161             CASE_BUILTIN_F (BUILT_IN_SQRT)
10162               /* sqrt(-0.0) is -0.0.  */
10163               if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (t))))
10164                 return 1;
10165               return tree_expr_nonnegative_p (TREE_VALUE (arglist));
10166
10167             CASE_BUILTIN_F (BUILT_IN_ASINH)
10168             CASE_BUILTIN_F (BUILT_IN_ATAN)
10169             CASE_BUILTIN_F (BUILT_IN_ATANH)
10170             CASE_BUILTIN_F (BUILT_IN_CBRT)
10171             CASE_BUILTIN_F (BUILT_IN_CEIL)
10172             CASE_BUILTIN_F (BUILT_IN_ERF)
10173             CASE_BUILTIN_F (BUILT_IN_EXPM1)
10174             CASE_BUILTIN_F (BUILT_IN_FLOOR)
10175             CASE_BUILTIN_F (BUILT_IN_FMOD)
10176             CASE_BUILTIN_F (BUILT_IN_LDEXP)
10177             CASE_BUILTIN_F (BUILT_IN_LLRINT)
10178             CASE_BUILTIN_F (BUILT_IN_LLROUND)
10179             CASE_BUILTIN_F (BUILT_IN_LRINT)
10180             CASE_BUILTIN_F (BUILT_IN_LROUND)
10181             CASE_BUILTIN_F (BUILT_IN_MODF)
10182             CASE_BUILTIN_F (BUILT_IN_NEARBYINT)
10183             CASE_BUILTIN_F (BUILT_IN_POW)
10184             CASE_BUILTIN_F (BUILT_IN_RINT)
10185             CASE_BUILTIN_F (BUILT_IN_ROUND)
10186             CASE_BUILTIN_F (BUILT_IN_SIGNBIT)
10187             CASE_BUILTIN_F (BUILT_IN_SINH)
10188             CASE_BUILTIN_F (BUILT_IN_TANH)
10189             CASE_BUILTIN_F (BUILT_IN_TRUNC)
10190               /* True if the 1st argument is nonnegative.  */
10191               return tree_expr_nonnegative_p (TREE_VALUE (arglist));
10192
10193             CASE_BUILTIN_F (BUILT_IN_FMAX)
10194               /* True if the 1st OR 2nd arguments are nonnegative.  */
10195               return tree_expr_nonnegative_p (TREE_VALUE (arglist))
10196                 || tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
10197
10198             CASE_BUILTIN_F (BUILT_IN_FMIN)
10199               /* True if the 1st AND 2nd arguments are nonnegative.  */
10200               return tree_expr_nonnegative_p (TREE_VALUE (arglist))
10201                 && tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
10202
10203             CASE_BUILTIN_F (BUILT_IN_COPYSIGN)
10204               /* True if the 2nd argument is nonnegative.  */
10205               return tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
10206
10207             default:
10208               break;
10209 #undef CASE_BUILTIN_F
10210 #undef CASE_BUILTIN_I
10211             }
10212       }
10213
10214       /* ... fall through ...  */
10215
10216     default:
10217       if (truth_value_p (TREE_CODE (t)))
10218         /* Truth values evaluate to 0 or 1, which is nonnegative.  */
10219         return 1;
10220     }
10221
10222   /* We don't know sign of `t', so be conservative and return false.  */
10223   return 0;
10224 }
10225
10226 /* Return true when T is an address and is known to be nonzero.
10227    For floating point we further ensure that T is not denormal.
10228    Similar logic is present in nonzero_address in rtlanal.h.  */
10229
10230 static bool
10231 tree_expr_nonzero_p (tree t)
10232 {
10233   tree type = TREE_TYPE (t);
10234
10235   /* Doing something useful for floating point would need more work.  */
10236   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10237     return false;
10238
10239   switch (TREE_CODE (t))
10240     {
10241     case ABS_EXPR:
10242       if (!TYPE_UNSIGNED (type) && !flag_wrapv)
10243         return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
10244
10245     case INTEGER_CST:
10246       /* We used to test for !integer_zerop here.  This does not work correctly
10247          if TREE_CONSTANT_OVERFLOW (t).  */
10248       return (TREE_INT_CST_LOW (t) != 0
10249               || TREE_INT_CST_HIGH (t) != 0);
10250
10251     case PLUS_EXPR:
10252       if (!TYPE_UNSIGNED (type) && !flag_wrapv)
10253         {
10254           /* With the presence of negative values it is hard
10255              to say something.  */
10256           if (!tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10257               || !tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
10258             return false;
10259           /* One of operands must be positive and the other non-negative.  */
10260           return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
10261                   || tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
10262         }
10263       break;
10264
10265     case MULT_EXPR:
10266       if (!TYPE_UNSIGNED (type) && !flag_wrapv)
10267         {
10268           return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
10269                   && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
10270         }
10271       break;
10272
10273     case NOP_EXPR:
10274       {
10275         tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
10276         tree outer_type = TREE_TYPE (t);
10277
10278         return (TYPE_PRECISION (inner_type) >= TYPE_PRECISION (outer_type)
10279                 && tree_expr_nonzero_p (TREE_OPERAND (t, 0)));
10280       }
10281       break;
10282
10283    case ADDR_EXPR:
10284       {
10285         tree base = get_base_address (TREE_OPERAND (t, 0));
10286
10287         if (!base)
10288           return false;
10289
10290         /* Weak declarations may link to NULL.  */
10291         if (DECL_P (base))
10292           return !DECL_WEAK (base);
10293
10294         /* Constants are never weak.  */
10295         if (CONSTANT_CLASS_P (base))
10296           return true;
10297
10298         return false;
10299       }
10300
10301     case COND_EXPR:
10302       return (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
10303               && tree_expr_nonzero_p (TREE_OPERAND (t, 2)));
10304
10305     case MIN_EXPR:
10306       return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
10307               && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
10308
10309     case MAX_EXPR:
10310       if (tree_expr_nonzero_p (TREE_OPERAND (t, 0)))
10311         {
10312           /* When both operands are nonzero, then MAX must be too.  */
10313           if (tree_expr_nonzero_p (TREE_OPERAND (t, 1)))
10314             return true;
10315
10316           /* MAX where operand 0 is positive is positive.  */
10317           return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10318         }
10319       /* MAX where operand 1 is positive is positive.  */
10320       else if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
10321                && tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
10322         return true;
10323       break;
10324
10325     case COMPOUND_EXPR:
10326     case MODIFY_EXPR:
10327     case BIND_EXPR:
10328       return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
10329
10330     case SAVE_EXPR:
10331     case NON_LVALUE_EXPR:
10332       return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
10333
10334     case BIT_IOR_EXPR:
10335       return tree_expr_nonzero_p (TREE_OPERAND (t, 1))
10336              || tree_expr_nonzero_p (TREE_OPERAND (t, 0));
10337
10338     default:
10339       break;
10340     }
10341   return false;
10342 }
10343
10344 /* See if we are applying CODE, a relational to the highest or lowest
10345    possible integer of TYPE.  If so, then the result is a compile
10346    time constant.  */
10347
10348 static tree
10349 fold_relational_hi_lo (enum tree_code *code_p, const tree type, tree *op0_p,
10350                        tree *op1_p)
10351 {
10352   tree op0 = *op0_p;
10353   tree op1 = *op1_p;
10354   enum tree_code code = *code_p;
10355   int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (op1)));
10356
10357   if (TREE_CODE (op1) == INTEGER_CST
10358       && ! TREE_CONSTANT_OVERFLOW (op1)
10359       && width <= HOST_BITS_PER_WIDE_INT
10360       && (INTEGRAL_TYPE_P (TREE_TYPE (op1))
10361           || POINTER_TYPE_P (TREE_TYPE (op1))))
10362     {
10363       unsigned HOST_WIDE_INT signed_max;
10364       unsigned HOST_WIDE_INT max, min;
10365
10366       signed_max = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1;
10367
10368       if (TYPE_UNSIGNED (TREE_TYPE (op1)))
10369         {
10370           max = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
10371           min = 0;
10372         }
10373       else
10374         {
10375           max = signed_max;
10376           min = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
10377         }
10378
10379       if (TREE_INT_CST_HIGH (op1) == 0
10380           && TREE_INT_CST_LOW (op1) == max)
10381         switch (code)
10382           {
10383           case GT_EXPR:
10384             return omit_one_operand (type, integer_zero_node, op0);
10385
10386           case GE_EXPR:
10387             *code_p = EQ_EXPR;
10388             break;
10389           case LE_EXPR:
10390             return omit_one_operand (type, integer_one_node, op0);
10391
10392           case LT_EXPR:
10393             *code_p = NE_EXPR;
10394             break;
10395
10396           /* The GE_EXPR and LT_EXPR cases above are not normally
10397              reached because of  previous transformations.  */
10398
10399           default:
10400             break;
10401           }
10402       else if (TREE_INT_CST_HIGH (op1) == 0
10403                && TREE_INT_CST_LOW (op1) == max - 1)
10404         switch (code)
10405           {
10406           case GT_EXPR:
10407             *code_p = EQ_EXPR;
10408             *op1_p = const_binop (PLUS_EXPR, op1, integer_one_node, 0);
10409             break;
10410           case LE_EXPR:
10411             *code_p = NE_EXPR;
10412             *op1_p = const_binop (PLUS_EXPR, op1, integer_one_node, 0);
10413             break;
10414           default:
10415             break;
10416           }
10417       else if (TREE_INT_CST_HIGH (op1) == (min ? -1 : 0)
10418                && TREE_INT_CST_LOW (op1) == min)
10419        switch (code)
10420           {
10421           case LT_EXPR:
10422             return omit_one_operand (type, integer_zero_node, op0);
10423
10424           case LE_EXPR:
10425             *code_p = EQ_EXPR;
10426             break;
10427
10428           case GE_EXPR:
10429             return omit_one_operand (type, integer_one_node, op0);
10430
10431           case GT_EXPR:
10432             *code_p = NE_EXPR;
10433             break;
10434
10435           default:
10436             break;
10437           }
10438       else if (TREE_INT_CST_HIGH (op1) == (min ? -1 : 0)
10439                && TREE_INT_CST_LOW (op1) == min + 1)
10440         switch (code)
10441           {
10442           case GE_EXPR:
10443             *code_p = NE_EXPR;
10444             *op1_p = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
10445             break;
10446           case LT_EXPR:
10447             *code_p = EQ_EXPR;
10448             *op1_p = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
10449             break;
10450           default:
10451             break;
10452           }
10453
10454       else if (TREE_INT_CST_HIGH (op1) == 0
10455                && TREE_INT_CST_LOW (op1) == signed_max
10456                && TYPE_UNSIGNED (TREE_TYPE (op1))
10457                /* signed_type does not work on pointer types.  */
10458                && INTEGRAL_TYPE_P (TREE_TYPE (op1)))
10459         {
10460           /* The following case also applies to X < signed_max+1
10461              and X >= signed_max+1 because previous transformations.  */
10462           if (code == LE_EXPR || code == GT_EXPR)
10463             {
10464               tree st0, st1, exp, retval;
10465               st0 = lang_hooks.types.signed_type (TREE_TYPE (op0));
10466               st1 = lang_hooks.types.signed_type (TREE_TYPE (op1));
10467
10468               exp = build2 (code == LE_EXPR ? GE_EXPR: LT_EXPR,
10469                             type,
10470                             fold_convert (st0, op0),
10471                             fold_convert (st1, integer_zero_node));
10472
10473               retval = fold_binary_to_constant (TREE_CODE (exp),
10474                                                 TREE_TYPE (exp),
10475                                                 TREE_OPERAND (exp, 0),
10476                                                 TREE_OPERAND (exp, 1));
10477
10478               /* If we are in gimple form, then returning EXP would create
10479                  non-gimple expressions.  Clearing it is safe and insures
10480                  we do not allow a non-gimple expression to escape.  */
10481               if (in_gimple_form)
10482                 exp = NULL;
10483
10484               return (retval ? retval : exp);
10485             }
10486         }
10487     }
10488
10489   return NULL_TREE;
10490 }
10491
10492
10493 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
10494    attempt to fold the expression to a constant without modifying TYPE,
10495    OP0 or OP1.
10496
10497    If the expression could be simplified to a constant, then return
10498    the constant.  If the expression would not be simplified to a
10499    constant, then return NULL_TREE.
10500
10501    Note this is primarily designed to be called after gimplification
10502    of the tree structures and when at least one operand is a constant.
10503    As a result of those simplifying assumptions this routine is far
10504    simpler than the generic fold routine.  */
10505
10506 tree
10507 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
10508 {
10509   int wins = 1;
10510   tree subop0;
10511   tree subop1;
10512   tree tem;
10513
10514   /* If this is a commutative operation, and ARG0 is a constant, move it
10515      to ARG1 to reduce the number of tests below.  */
10516   if (commutative_tree_code (code)
10517       && (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST))
10518     {
10519       tem = op0;
10520       op0 = op1;
10521       op1 = tem;
10522     }
10523
10524   /* If either operand is a complex type, extract its real component.  */
10525   if (TREE_CODE (op0) == COMPLEX_CST)
10526     subop0 = TREE_REALPART (op0);
10527   else
10528     subop0 = op0;
10529
10530   if (TREE_CODE (op1) == COMPLEX_CST)
10531     subop1 = TREE_REALPART (op1);
10532   else
10533     subop1 = op1;
10534
10535   /* Note if either argument is not a real or integer constant.
10536      With a few exceptions, simplification is limited to cases
10537      where both arguments are constants.  */
10538   if ((TREE_CODE (subop0) != INTEGER_CST
10539        && TREE_CODE (subop0) != REAL_CST)
10540       || (TREE_CODE (subop1) != INTEGER_CST
10541           && TREE_CODE (subop1) != REAL_CST))
10542     wins = 0;
10543
10544   switch (code)
10545     {
10546     case PLUS_EXPR:
10547       /* (plus (address) (const_int)) is a constant.  */
10548       if (TREE_CODE (op0) == PLUS_EXPR
10549           && TREE_CODE (op1) == INTEGER_CST
10550           && (TREE_CODE (TREE_OPERAND (op0, 0)) == ADDR_EXPR
10551               || (TREE_CODE (TREE_OPERAND (op0, 0)) == NOP_EXPR
10552                   && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (op0, 0), 0))
10553                       == ADDR_EXPR)))
10554           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
10555         {
10556           return build2 (PLUS_EXPR, type, TREE_OPERAND (op0, 0),
10557                          const_binop (PLUS_EXPR, op1,
10558                                       TREE_OPERAND (op0, 1), 0));
10559         }
10560     case BIT_XOR_EXPR:
10561
10562     binary:
10563       if (!wins)
10564         return NULL_TREE;
10565
10566       /* Both arguments are constants.  Simplify.  */
10567       tem = const_binop (code, op0, op1, 0);
10568       if (tem != NULL_TREE)
10569         {
10570           /* The return value should always have the same type as
10571              the original expression.  */
10572           if (TREE_TYPE (tem) != type)
10573             tem = fold_convert (type, tem);
10574
10575           return tem;
10576         }
10577       return NULL_TREE;
10578
10579     case MINUS_EXPR:
10580       /* Fold &x - &x.  This can happen from &x.foo - &x.
10581          This is unsafe for certain floats even in non-IEEE formats.
10582          In IEEE, it is unsafe because it does wrong for NaNs.
10583          Also note that operand_equal_p is always false if an
10584          operand is volatile.  */
10585       if (! FLOAT_TYPE_P (type) && operand_equal_p (op0, op1, 0))
10586         return fold_convert (type, integer_zero_node);
10587
10588       goto binary;
10589
10590     case MULT_EXPR:
10591     case BIT_AND_EXPR:
10592       /* Special case multiplication or bitwise AND where one argument
10593          is zero.  */
10594       if (! FLOAT_TYPE_P (type) && integer_zerop (op1))
10595         return omit_one_operand (type, op1, op0);
10596       else
10597         if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (op0)))
10598             && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op0)))
10599             && real_zerop (op1))
10600           return omit_one_operand (type, op1, op0);
10601
10602       goto binary;
10603
10604     case BIT_IOR_EXPR:
10605       /* Special case when we know the result will be all ones.  */
10606       if (integer_all_onesp (op1))
10607         return omit_one_operand (type, op1, op0);
10608
10609       goto binary;
10610
10611     case TRUNC_DIV_EXPR:
10612     case ROUND_DIV_EXPR:
10613     case FLOOR_DIV_EXPR:
10614     case CEIL_DIV_EXPR:
10615     case EXACT_DIV_EXPR:
10616     case TRUNC_MOD_EXPR:
10617     case ROUND_MOD_EXPR:
10618     case FLOOR_MOD_EXPR:
10619     case CEIL_MOD_EXPR:
10620     case RDIV_EXPR:
10621       /* Division by zero is undefined.  */
10622       if (integer_zerop (op1))
10623         return NULL_TREE;
10624
10625       if (TREE_CODE (op1) == REAL_CST
10626           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (op1)))
10627           && real_zerop (op1))
10628         return NULL_TREE;
10629
10630       goto binary;
10631
10632     case MIN_EXPR:
10633       if (INTEGRAL_TYPE_P (type)
10634           && operand_equal_p (op1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
10635         return omit_one_operand (type, op1, op0);
10636
10637       goto binary;
10638
10639     case MAX_EXPR:
10640       if (INTEGRAL_TYPE_P (type)
10641           && TYPE_MAX_VALUE (type)
10642           && operand_equal_p (op1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
10643         return omit_one_operand (type, op1, op0);
10644
10645       goto binary;
10646
10647     case RSHIFT_EXPR:
10648       /* Optimize -1 >> x for arithmetic right shifts.  */
10649       if (integer_all_onesp (op0) && ! TYPE_UNSIGNED (type))
10650         return omit_one_operand (type, op0, op1);
10651       /* ... fall through ...  */
10652
10653     case LSHIFT_EXPR:
10654       if (integer_zerop (op0))
10655         return omit_one_operand (type, op0, op1);
10656
10657       /* Since negative shift count is not well-defined, don't
10658          try to compute it in the compiler.  */
10659       if (TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sgn (op1) < 0)
10660         return NULL_TREE;
10661
10662       goto binary;
10663
10664     case LROTATE_EXPR:
10665     case RROTATE_EXPR:
10666       /* -1 rotated either direction by any amount is still -1.  */
10667       if (integer_all_onesp (op0))
10668         return omit_one_operand (type, op0, op1);
10669
10670       /* 0 rotated either direction by any amount is still zero.  */
10671       if (integer_zerop (op0))
10672         return omit_one_operand (type, op0, op1);
10673
10674       goto binary;
10675
10676     case COMPLEX_EXPR:
10677       if (wins)
10678         return build_complex (type, op0, op1);
10679       return NULL_TREE;
10680
10681     case LT_EXPR:
10682     case LE_EXPR:
10683     case GT_EXPR:
10684     case GE_EXPR:
10685     case EQ_EXPR:
10686     case NE_EXPR:
10687       /* If one arg is a real or integer constant, put it last.  */
10688       if ((TREE_CODE (op0) == INTEGER_CST
10689            && TREE_CODE (op1) != INTEGER_CST)
10690           || (TREE_CODE (op0) == REAL_CST
10691               && TREE_CODE (op0) != REAL_CST))
10692         {
10693           tree temp;
10694
10695           temp = op0;
10696           op0 = op1;
10697           op1 = temp;
10698           code = swap_tree_comparison (code);
10699         }
10700
10701       /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
10702          This transformation affects the cases which are handled in later
10703          optimizations involving comparisons with non-negative constants.  */
10704       if (TREE_CODE (op1) == INTEGER_CST
10705           && TREE_CODE (op0) != INTEGER_CST
10706           && tree_int_cst_sgn (op1) > 0)
10707         {
10708           switch (code)
10709             {
10710             case GE_EXPR:
10711               code = GT_EXPR;
10712               op1 = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
10713               break;
10714
10715             case LT_EXPR:
10716               code = LE_EXPR;
10717               op1 = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
10718               break;
10719
10720             default:
10721               break;
10722             }
10723         }
10724
10725       tem = fold_relational_hi_lo (&code, type, &op0, &op1);
10726       if (tem)
10727         return tem;
10728
10729       /* Fall through.  */
10730
10731     case ORDERED_EXPR:
10732     case UNORDERED_EXPR:
10733     case UNLT_EXPR:
10734     case UNLE_EXPR:
10735     case UNGT_EXPR:
10736     case UNGE_EXPR:
10737     case UNEQ_EXPR:
10738     case LTGT_EXPR:
10739       if (!wins)
10740         return NULL_TREE;
10741
10742       return fold_relational_const (code, type, op0, op1);
10743
10744     case RANGE_EXPR:
10745       /* This could probably be handled.  */
10746       return NULL_TREE;
10747
10748     case TRUTH_AND_EXPR:
10749       /* If second arg is constant zero, result is zero, but first arg
10750          must be evaluated.  */
10751       if (integer_zerop (op1))
10752         return omit_one_operand (type, op1, op0);
10753       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10754          case will be handled here.  */
10755       if (integer_zerop (op0))
10756         return omit_one_operand (type, op0, op1);
10757       if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
10758         return constant_boolean_node (true, type);
10759       return NULL_TREE;
10760
10761     case TRUTH_OR_EXPR:
10762       /* If second arg is constant true, result is true, but we must
10763          evaluate first arg.  */
10764       if (TREE_CODE (op1) == INTEGER_CST && ! integer_zerop (op1))
10765         return omit_one_operand (type, op1, op0);
10766       /* Likewise for first arg, but note this only occurs here for
10767          TRUTH_OR_EXPR.  */
10768       if (TREE_CODE (op0) == INTEGER_CST && ! integer_zerop (op0))
10769         return omit_one_operand (type, op0, op1);
10770       if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
10771         return constant_boolean_node (false, type);
10772       return NULL_TREE;
10773
10774     case TRUTH_XOR_EXPR:
10775       if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
10776         {
10777           int x = ! integer_zerop (op0) ^ ! integer_zerop (op1);
10778           return constant_boolean_node (x, type);
10779         }
10780       return NULL_TREE;
10781
10782     default:
10783       return NULL_TREE;
10784     }
10785 }
10786
10787 /* Given the components of a unary expression CODE, TYPE and OP0,
10788    attempt to fold the expression to a constant without modifying
10789    TYPE or OP0.
10790
10791    If the expression could be simplified to a constant, then return
10792    the constant.  If the expression would not be simplified to a
10793    constant, then return NULL_TREE.
10794
10795    Note this is primarily designed to be called after gimplification
10796    of the tree structures and when op0 is a constant.  As a result
10797    of those simplifying assumptions this routine is far simpler than
10798    the generic fold routine.  */
10799
10800 tree
10801 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
10802 {
10803   /* Make sure we have a suitable constant argument.  */
10804   if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
10805     {
10806       tree subop;
10807
10808       if (TREE_CODE (op0) == COMPLEX_CST)
10809         subop = TREE_REALPART (op0);
10810       else
10811         subop = op0;
10812
10813       if (TREE_CODE (subop) != INTEGER_CST && TREE_CODE (subop) != REAL_CST)
10814         return NULL_TREE;
10815     }
10816
10817   switch (code)
10818     {
10819     case NOP_EXPR:
10820     case FLOAT_EXPR:
10821     case CONVERT_EXPR:
10822     case FIX_TRUNC_EXPR:
10823     case FIX_FLOOR_EXPR:
10824     case FIX_CEIL_EXPR:
10825       return fold_convert_const (code, type, op0);
10826
10827     case NEGATE_EXPR:
10828       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
10829         return fold_negate_const (op0, type);
10830       else
10831         return NULL_TREE;
10832
10833     case ABS_EXPR:
10834       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
10835         return fold_abs_const (op0, type);
10836       else
10837         return NULL_TREE;
10838
10839     case BIT_NOT_EXPR:
10840       if (TREE_CODE (op0) == INTEGER_CST)
10841         return fold_not_const (op0, type);
10842       else
10843         return NULL_TREE;
10844
10845     case REALPART_EXPR:
10846       if (TREE_CODE (op0) == COMPLEX_CST)
10847         return TREE_REALPART (op0);
10848       else
10849         return NULL_TREE;
10850
10851     case IMAGPART_EXPR:
10852       if (TREE_CODE (op0) == COMPLEX_CST)
10853         return TREE_IMAGPART (op0);
10854       else
10855         return NULL_TREE;
10856
10857     case CONJ_EXPR:
10858       if (TREE_CODE (op0) == COMPLEX_CST
10859           && TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
10860         return build_complex (type, TREE_REALPART (op0),
10861                               negate_expr (TREE_IMAGPART (op0)));
10862       return NULL_TREE;
10863
10864     default:
10865       return NULL_TREE;
10866     }
10867 }
10868
10869 /* If EXP represents referencing an element in a constant string
10870    (either via pointer arithmetic or array indexing), return the
10871    tree representing the value accessed, otherwise return NULL.  */
10872
10873 tree
10874 fold_read_from_constant_string (tree exp)
10875 {
10876   if (TREE_CODE (exp) == INDIRECT_REF || TREE_CODE (exp) == ARRAY_REF)
10877     {
10878       tree exp1 = TREE_OPERAND (exp, 0);
10879       tree index;
10880       tree string;
10881
10882       if (TREE_CODE (exp) == INDIRECT_REF)
10883         string = string_constant (exp1, &index);
10884       else
10885         {
10886           tree low_bound = array_ref_low_bound (exp);
10887           index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
10888
10889           /* Optimize the special-case of a zero lower bound.
10890
10891              We convert the low_bound to sizetype to avoid some problems
10892              with constant folding.  (E.g. suppose the lower bound is 1,
10893              and its mode is QI.  Without the conversion,l (ARRAY
10894              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
10895              +INDEX), which becomes (ARRAY+255+INDEX).  Opps!)  */
10896           if (! integer_zerop (low_bound))
10897             index = size_diffop (index, fold_convert (sizetype, low_bound));
10898
10899           string = exp1;
10900         }
10901
10902       if (string
10903           && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (string))
10904           && TREE_CODE (string) == STRING_CST
10905           && TREE_CODE (index) == INTEGER_CST
10906           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
10907           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
10908               == MODE_INT)
10909           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
10910         return fold_convert (TREE_TYPE (exp),
10911                              build_int_cst (NULL_TREE,
10912                                             (TREE_STRING_POINTER (string)
10913                                              [TREE_INT_CST_LOW (index)])));
10914     }
10915   return NULL;
10916 }
10917
10918 /* Return the tree for neg (ARG0) when ARG0 is known to be either
10919    an integer constant or real constant.
10920
10921    TYPE is the type of the result.  */
10922
10923 static tree
10924 fold_negate_const (tree arg0, tree type)
10925 {
10926   tree t = NULL_TREE;
10927
10928   switch (TREE_CODE (arg0))
10929     {
10930     case INTEGER_CST:
10931       {
10932         unsigned HOST_WIDE_INT low;
10933         HOST_WIDE_INT high;
10934         int overflow = neg_double (TREE_INT_CST_LOW (arg0),
10935                                    TREE_INT_CST_HIGH (arg0),
10936                                    &low, &high);
10937         t = build_int_cst_wide (type, low, high);
10938         t = force_fit_type (t, 1,
10939                             (overflow | TREE_OVERFLOW (arg0))
10940                             && !TYPE_UNSIGNED (type),
10941                             TREE_CONSTANT_OVERFLOW (arg0));
10942         break;
10943       }
10944
10945     case REAL_CST:
10946       t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
10947       break;
10948
10949     default:
10950       gcc_unreachable ();
10951     }
10952
10953   return t;
10954 }
10955
10956 /* Return the tree for abs (ARG0) when ARG0 is known to be either
10957    an integer constant or real constant.
10958
10959    TYPE is the type of the result.  */
10960
10961 tree
10962 fold_abs_const (tree arg0, tree type)
10963 {
10964   tree t = NULL_TREE;
10965
10966   switch (TREE_CODE (arg0))
10967     {
10968     case INTEGER_CST:
10969       /* If the value is unsigned, then the absolute value is
10970          the same as the ordinary value.  */
10971       if (TYPE_UNSIGNED (type))
10972         t = arg0;
10973       /* Similarly, if the value is non-negative.  */
10974       else if (INT_CST_LT (integer_minus_one_node, arg0))
10975         t = arg0;
10976       /* If the value is negative, then the absolute value is
10977          its negation.  */
10978       else
10979         {
10980           unsigned HOST_WIDE_INT low;
10981           HOST_WIDE_INT high;
10982           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
10983                                      TREE_INT_CST_HIGH (arg0),
10984                                      &low, &high);
10985           t = build_int_cst_wide (type, low, high);
10986           t = force_fit_type (t, -1, overflow | TREE_OVERFLOW (arg0),
10987                               TREE_CONSTANT_OVERFLOW (arg0));
10988         }
10989       break;
10990
10991     case REAL_CST:
10992       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
10993         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
10994       else
10995         t =  arg0;
10996       break;
10997
10998     default:
10999       gcc_unreachable ();
11000     }
11001
11002   return t;
11003 }
11004
11005 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
11006    constant.  TYPE is the type of the result.  */
11007
11008 static tree
11009 fold_not_const (tree arg0, tree type)
11010 {
11011   tree t = NULL_TREE;
11012
11013   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
11014
11015   t = build_int_cst_wide (type,
11016                           ~ TREE_INT_CST_LOW (arg0),
11017                           ~ TREE_INT_CST_HIGH (arg0));
11018   t = force_fit_type (t, 0, TREE_OVERFLOW (arg0),
11019                       TREE_CONSTANT_OVERFLOW (arg0));
11020
11021   return t;
11022 }
11023
11024 /* Given CODE, a relational operator, the target type, TYPE and two
11025    constant operands OP0 and OP1, return the result of the
11026    relational operation.  If the result is not a compile time
11027    constant, then return NULL_TREE.  */
11028
11029 static tree
11030 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
11031 {
11032   int result, invert;
11033
11034   /* From here on, the only cases we handle are when the result is
11035      known to be a constant.  */
11036
11037   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
11038     {
11039       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
11040       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
11041
11042       /* Handle the cases where either operand is a NaN.  */
11043       if (real_isnan (c0) || real_isnan (c1))
11044         {
11045           switch (code)
11046             {
11047             case EQ_EXPR:
11048             case ORDERED_EXPR:
11049               result = 0;
11050               break;
11051
11052             case NE_EXPR:
11053             case UNORDERED_EXPR:
11054             case UNLT_EXPR:
11055             case UNLE_EXPR:
11056             case UNGT_EXPR:
11057             case UNGE_EXPR:
11058             case UNEQ_EXPR:
11059               result = 1;
11060               break;
11061
11062             case LT_EXPR:
11063             case LE_EXPR:
11064             case GT_EXPR:
11065             case GE_EXPR:
11066             case LTGT_EXPR:
11067               if (flag_trapping_math)
11068                 return NULL_TREE;
11069               result = 0;
11070               break;
11071
11072             default:
11073               gcc_unreachable ();
11074             }
11075
11076           return constant_boolean_node (result, type);
11077         }
11078
11079       return constant_boolean_node (real_compare (code, c0, c1), type);
11080     }
11081
11082   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
11083
11084      To compute GT, swap the arguments and do LT.
11085      To compute GE, do LT and invert the result.
11086      To compute LE, swap the arguments, do LT and invert the result.
11087      To compute NE, do EQ and invert the result.
11088
11089      Therefore, the code below must handle only EQ and LT.  */
11090
11091   if (code == LE_EXPR || code == GT_EXPR)
11092     {
11093       tree tem = op0;
11094       op0 = op1;
11095       op1 = tem;
11096       code = swap_tree_comparison (code);
11097     }
11098
11099   /* Note that it is safe to invert for real values here because we
11100      have already handled the one case that it matters.  */
11101
11102   invert = 0;
11103   if (code == NE_EXPR || code == GE_EXPR)
11104     {
11105       invert = 1;
11106       code = invert_tree_comparison (code, false);
11107     }
11108
11109   /* Compute a result for LT or EQ if args permit;
11110      Otherwise return T.  */
11111   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
11112     {
11113       if (code == EQ_EXPR)
11114         result = tree_int_cst_equal (op0, op1);
11115       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
11116         result = INT_CST_LT_UNSIGNED (op0, op1);
11117       else
11118         result = INT_CST_LT (op0, op1);
11119     }
11120   else
11121     return NULL_TREE;
11122
11123   if (invert)
11124     result ^= 1;
11125   return constant_boolean_node (result, type);
11126 }
11127
11128 /* Build an expression for the a clean point containing EXPR with type TYPE.
11129    Don't build a cleanup point expression for EXPR which don't have side
11130    effects.  */
11131
11132 tree
11133 fold_build_cleanup_point_expr (tree type, tree expr)
11134 {
11135   /* If the expression does not have side effects then we don't have to wrap
11136      it with a cleanup point expression.  */
11137   if (!TREE_SIDE_EFFECTS (expr))
11138     return expr;
11139
11140   /* If the expression is a return, check to see if the expression inside the
11141      return has no side effects or the right hand side of the modify expression
11142      inside the return. If either don't have side effects set we don't need to
11143      wrap the expression in a cleanup point expression.  Note we don't check the
11144      left hand side of the modify because it should always be a return decl.  */
11145   if (TREE_CODE (expr) == RETURN_EXPR)
11146     {
11147       tree op = TREE_OPERAND (expr, 0);
11148       if (!op || !TREE_SIDE_EFFECTS (op))
11149         return expr;
11150       op = TREE_OPERAND (op, 1);
11151       if (!TREE_SIDE_EFFECTS (op))
11152         return expr;
11153     }
11154   
11155   return build1 (CLEANUP_POINT_EXPR, type, expr);
11156 }
11157
11158 /* Build an expression for the address of T.  Folds away INDIRECT_REF to
11159    avoid confusing the gimplify process.  */
11160
11161 tree
11162 build_fold_addr_expr_with_type (tree t, tree ptrtype)
11163 {
11164   /* The size of the object is not relevant when talking about its address.  */
11165   if (TREE_CODE (t) == WITH_SIZE_EXPR)
11166     t = TREE_OPERAND (t, 0);
11167
11168   /* Note: doesn't apply to ALIGN_INDIRECT_REF */
11169   if (TREE_CODE (t) == INDIRECT_REF
11170       || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
11171     {
11172       t = TREE_OPERAND (t, 0);
11173       if (TREE_TYPE (t) != ptrtype)
11174         t = build1 (NOP_EXPR, ptrtype, t);
11175     }
11176   else
11177     {
11178       tree base = t;
11179
11180       while (handled_component_p (base))
11181         base = TREE_OPERAND (base, 0);
11182       if (DECL_P (base))
11183         TREE_ADDRESSABLE (base) = 1;
11184
11185       t = build1 (ADDR_EXPR, ptrtype, t);
11186     }
11187
11188   return t;
11189 }
11190
11191 tree
11192 build_fold_addr_expr (tree t)
11193 {
11194   return build_fold_addr_expr_with_type (t, build_pointer_type (TREE_TYPE (t)));
11195 }
11196
11197 /* Builds an expression for an indirection through T, simplifying some
11198    cases.  */
11199
11200 tree
11201 build_fold_indirect_ref (tree t)
11202 {
11203   tree type = TREE_TYPE (TREE_TYPE (t));
11204   tree sub = t;
11205   tree subtype;
11206
11207   STRIP_NOPS (sub);
11208   if (TREE_CODE (sub) == ADDR_EXPR)
11209     {
11210       tree op = TREE_OPERAND (sub, 0);
11211       tree optype = TREE_TYPE (op);
11212       /* *&p => p */
11213       if (lang_hooks.types_compatible_p (type, optype))
11214         return op;
11215       /* *(foo *)&fooarray => fooarray[0] */
11216       else if (TREE_CODE (optype) == ARRAY_TYPE
11217                && lang_hooks.types_compatible_p (type, TREE_TYPE (optype)))
11218         return build4 (ARRAY_REF, type, op, size_zero_node, NULL_TREE, NULL_TREE);
11219     }
11220
11221   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
11222   subtype = TREE_TYPE (sub);
11223   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
11224       && lang_hooks.types_compatible_p (type, TREE_TYPE (TREE_TYPE (subtype))))
11225     {
11226       sub = build_fold_indirect_ref (sub);
11227       return build4 (ARRAY_REF, type, sub, size_zero_node, NULL_TREE, NULL_TREE);
11228     }
11229
11230   return build1 (INDIRECT_REF, type, t);
11231 }
11232
11233 /* Strip non-trapping, non-side-effecting tree nodes from an expression
11234    whose result is ignored.  The type of the returned tree need not be
11235    the same as the original expression.  */
11236
11237 tree
11238 fold_ignored_result (tree t)
11239 {
11240   if (!TREE_SIDE_EFFECTS (t))
11241     return integer_zero_node;
11242
11243   for (;;)
11244     switch (TREE_CODE_CLASS (TREE_CODE (t)))
11245       {
11246       case tcc_unary:
11247         t = TREE_OPERAND (t, 0);
11248         break;
11249
11250       case tcc_binary:
11251       case tcc_comparison:
11252         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
11253           t = TREE_OPERAND (t, 0);
11254         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
11255           t = TREE_OPERAND (t, 1);
11256         else
11257           return t;
11258         break;
11259
11260       case tcc_expression:
11261         switch (TREE_CODE (t))
11262           {
11263           case COMPOUND_EXPR:
11264             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
11265               return t;
11266             t = TREE_OPERAND (t, 0);
11267             break;
11268
11269           case COND_EXPR:
11270             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
11271                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
11272               return t;
11273             t = TREE_OPERAND (t, 0);
11274             break;
11275
11276           default:
11277             return t;
11278           }
11279         break;
11280
11281       default:
11282         return t;
11283       }
11284 }
11285
11286 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
11287    This can only be applied to objects of a sizetype.  */
11288
11289 tree
11290 round_up (tree value, int divisor)
11291 {
11292   tree div = NULL_TREE;
11293
11294   gcc_assert (divisor > 0);
11295   if (divisor == 1)
11296     return value;
11297
11298   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
11299      have to do anything.  Only do this when we are not given a const,
11300      because in that case, this check is more expensive than just
11301      doing it.  */
11302   if (TREE_CODE (value) != INTEGER_CST)
11303     {
11304       div = build_int_cst (TREE_TYPE (value), divisor);
11305
11306       if (multiple_of_p (TREE_TYPE (value), value, div))
11307         return value;
11308     }
11309
11310   /* If divisor is a power of two, simplify this to bit manipulation.  */
11311   if (divisor == (divisor & -divisor))
11312     {
11313       tree t;
11314
11315       t = build_int_cst (TREE_TYPE (value), divisor - 1);
11316       value = size_binop (PLUS_EXPR, value, t);
11317       t = build_int_cst (TREE_TYPE (value), -divisor);
11318       value = size_binop (BIT_AND_EXPR, value, t);
11319     }
11320   else
11321     {
11322       if (!div)
11323         div = build_int_cst (TREE_TYPE (value), divisor);
11324       value = size_binop (CEIL_DIV_EXPR, value, div);
11325       value = size_binop (MULT_EXPR, value, div);
11326     }
11327
11328   return value;
11329 }
11330
11331 /* Likewise, but round down.  */
11332
11333 tree
11334 round_down (tree value, int divisor)
11335 {
11336   tree div = NULL_TREE;
11337
11338   gcc_assert (divisor > 0);
11339   if (divisor == 1)
11340     return value;
11341
11342   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
11343      have to do anything.  Only do this when we are not given a const,
11344      because in that case, this check is more expensive than just
11345      doing it.  */
11346   if (TREE_CODE (value) != INTEGER_CST)
11347     {
11348       div = build_int_cst (TREE_TYPE (value), divisor);
11349
11350       if (multiple_of_p (TREE_TYPE (value), value, div))
11351         return value;
11352     }
11353
11354   /* If divisor is a power of two, simplify this to bit manipulation.  */
11355   if (divisor == (divisor & -divisor))
11356     {
11357       tree t;
11358
11359       t = build_int_cst (TREE_TYPE (value), -divisor);
11360       value = size_binop (BIT_AND_EXPR, value, t);
11361     }
11362   else
11363     {
11364       if (!div)
11365         div = build_int_cst (TREE_TYPE (value), divisor);
11366       value = size_binop (FLOOR_DIV_EXPR, value, div);
11367       value = size_binop (MULT_EXPR, value, div);
11368     }
11369
11370   return value;
11371 }
11372
11373 /* Returns the pointer to the base of the object addressed by EXP and
11374    extracts the information about the offset of the access, storing it
11375    to PBITPOS and POFFSET.  */
11376
11377 static tree
11378 split_address_to_core_and_offset (tree exp,
11379                                   HOST_WIDE_INT *pbitpos, tree *poffset)
11380 {
11381   tree core;
11382   enum machine_mode mode;
11383   int unsignedp, volatilep;
11384   HOST_WIDE_INT bitsize;
11385
11386   if (TREE_CODE (exp) == ADDR_EXPR)
11387     {
11388       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
11389                                   poffset, &mode, &unsignedp, &volatilep,
11390                                   false);
11391
11392       if (TREE_CODE (core) == INDIRECT_REF)
11393         core = TREE_OPERAND (core, 0);
11394     }
11395   else
11396     {
11397       core = exp;
11398       *pbitpos = 0;
11399       *poffset = NULL_TREE;
11400     }
11401
11402   return core;
11403 }
11404
11405 /* Returns true if addresses of E1 and E2 differ by a constant, false
11406    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
11407
11408 bool
11409 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
11410 {
11411   tree core1, core2;
11412   HOST_WIDE_INT bitpos1, bitpos2;
11413   tree toffset1, toffset2, tdiff, type;
11414
11415   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
11416   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
11417
11418   if (bitpos1 % BITS_PER_UNIT != 0
11419       || bitpos2 % BITS_PER_UNIT != 0
11420       || !operand_equal_p (core1, core2, 0))
11421     return false;
11422
11423   if (toffset1 && toffset2)
11424     {
11425       type = TREE_TYPE (toffset1);
11426       if (type != TREE_TYPE (toffset2))
11427         toffset2 = fold_convert (type, toffset2);
11428
11429       tdiff = fold (build2 (MINUS_EXPR, type, toffset1, toffset2));
11430       if (!host_integerp (tdiff, 0))
11431         return false;
11432
11433       *diff = tree_low_cst (tdiff, 0);
11434     }
11435   else if (toffset1 || toffset2)
11436     {
11437       /* If only one of the offsets is non-constant, the difference cannot
11438          be a constant.  */
11439       return false;
11440     }
11441   else
11442     *diff = 0;
11443
11444   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
11445   return true;
11446 }
11447
11448 /* Simplify the floating point expression EXP when the sign of the
11449    result is not significant.  Return NULL_TREE if no simplification
11450    is possible.  */
11451
11452 tree
11453 fold_strip_sign_ops (tree exp)
11454 {
11455   tree arg0, arg1;
11456
11457   switch (TREE_CODE (exp))
11458     {
11459     case ABS_EXPR:
11460     case NEGATE_EXPR:
11461       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
11462       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
11463
11464     case MULT_EXPR:
11465     case RDIV_EXPR:
11466       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
11467         return NULL_TREE;
11468       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
11469       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
11470       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
11471         return fold (build2 (TREE_CODE (exp), TREE_TYPE (exp),
11472                              arg0 ? arg0 : TREE_OPERAND (exp, 0),
11473                              arg1 ? arg1 : TREE_OPERAND (exp, 1)));
11474       break;
11475
11476     default:
11477       break;
11478     }
11479   return NULL_TREE;
11480 }
11481