OSDN Git Service

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