OSDN Git Service

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