OSDN Git Service

2009-05-08 Tobias Burnus <burnus@net-b.de>
[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, 2006, 2007, 2008, 2009
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
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_double.
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_double takes a constant, an overflowable flag and a
43    prior overflow indicator.  It forces the value to fit the type and
44    sets TREE_OVERFLOW.
45
46    Note: Since the folders get called on non-gimple code as well as
47    gimple code, we need to handle GIMPLE tuples as well as their
48    corresponding tree equivalents.  */
49
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include "flags.h"
55 #include "tree.h"
56 #include "real.h"
57 #include "fixed-value.h"
58 #include "rtl.h"
59 #include "expr.h"
60 #include "tm_p.h"
61 #include "target.h"
62 #include "toplev.h"
63 #include "intl.h"
64 #include "ggc.h"
65 #include "hashtab.h"
66 #include "langhooks.h"
67 #include "md5.h"
68 #include "gimple.h"
69
70 /* Nonzero if we are folding constants inside an initializer; zero
71    otherwise.  */
72 int folding_initializer = 0;
73
74 /* The following constants represent a bit based encoding of GCC's
75    comparison operators.  This encoding simplifies transformations
76    on relational comparison operators, such as AND and OR.  */
77 enum comparison_code {
78   COMPCODE_FALSE = 0,
79   COMPCODE_LT = 1,
80   COMPCODE_EQ = 2,
81   COMPCODE_LE = 3,
82   COMPCODE_GT = 4,
83   COMPCODE_LTGT = 5,
84   COMPCODE_GE = 6,
85   COMPCODE_ORD = 7,
86   COMPCODE_UNORD = 8,
87   COMPCODE_UNLT = 9,
88   COMPCODE_UNEQ = 10,
89   COMPCODE_UNLE = 11,
90   COMPCODE_UNGT = 12,
91   COMPCODE_NE = 13,
92   COMPCODE_UNGE = 14,
93   COMPCODE_TRUE = 15
94 };
95
96 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
97 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
98 static bool negate_mathfn_p (enum built_in_function);
99 static bool negate_expr_p (tree);
100 static tree negate_expr (tree);
101 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
102 static tree associate_trees (tree, tree, enum tree_code, tree);
103 static tree const_binop (enum tree_code, tree, tree, int);
104 static enum comparison_code comparison_to_compcode (enum tree_code);
105 static enum tree_code compcode_to_comparison (enum comparison_code);
106 static int operand_equal_for_comparison_p (tree, tree, tree);
107 static int twoval_comparison_p (tree, tree *, tree *, int *);
108 static tree eval_subst (tree, tree, tree, tree, tree);
109 static tree pedantic_omit_one_operand (tree, tree, tree);
110 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
111 static tree make_bit_field_ref (tree, tree, HOST_WIDE_INT, HOST_WIDE_INT, int);
112 static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
113 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
114                                     enum machine_mode *, int *, int *,
115                                     tree *, tree *);
116 static int all_ones_mask_p (const_tree, int);
117 static tree sign_bit_p (tree, const_tree);
118 static int simple_operand_p (const_tree);
119 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
120 static tree range_predecessor (tree);
121 static tree range_successor (tree);
122 static tree make_range (tree, int *, tree *, tree *, bool *);
123 static tree build_range_check (tree, tree, int, tree, tree);
124 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
125                          tree);
126 static tree fold_range_test (enum tree_code, tree, tree, tree);
127 static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
128 static tree unextend (tree, int, int, tree);
129 static tree fold_truthop (enum tree_code, tree, tree, tree);
130 static tree optimize_minmax_comparison (enum tree_code, tree, tree, tree);
131 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
132 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
133 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree,
134                                                  tree, tree,
135                                                  tree, tree, int);
136 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
137                                  tree, tree, tree);
138 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
139 static tree fold_div_compare (enum tree_code, tree, tree, tree);
140 static bool reorder_operands_p (const_tree, const_tree);
141 static tree fold_negate_const (tree, tree);
142 static tree fold_not_const (tree, tree);
143 static tree fold_relational_const (enum tree_code, tree, tree, tree);
144 static tree fold_convert_const (enum tree_code, tree, tree);
145
146
147 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
148    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
149    and SUM1.  Then this yields nonzero if overflow occurred during the
150    addition.
151
152    Overflow occurs if A and B have the same sign, but A and SUM differ in
153    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
154    sign.  */
155 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
156 \f
157 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
158    We do that by representing the two-word integer in 4 words, with only
159    HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
160    number.  The value of the word is LOWPART + HIGHPART * BASE.  */
161
162 #define LOWPART(x) \
163   ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
164 #define HIGHPART(x) \
165   ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
166 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
167
168 /* Unpack a two-word integer into 4 words.
169    LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
170    WORDS points to the array of HOST_WIDE_INTs.  */
171
172 static void
173 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
174 {
175   words[0] = LOWPART (low);
176   words[1] = HIGHPART (low);
177   words[2] = LOWPART (hi);
178   words[3] = HIGHPART (hi);
179 }
180
181 /* Pack an array of 4 words into a two-word integer.
182    WORDS points to the array of words.
183    The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces.  */
184
185 static void
186 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
187         HOST_WIDE_INT *hi)
188 {
189   *low = words[0] + words[1] * BASE;
190   *hi = words[2] + words[3] * BASE;
191 }
192 \f
193 /* Force the double-word integer L1, H1 to be within the range of the
194    integer type TYPE.  Stores the properly truncated and sign-extended
195    double-word integer in *LV, *HV.  Returns true if the operation
196    overflows, that is, argument and result are different.  */
197
198 int
199 fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
200                  unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, const_tree type)
201 {
202   unsigned HOST_WIDE_INT low0 = l1;
203   HOST_WIDE_INT high0 = h1;
204   unsigned int prec;
205   int sign_extended_type;
206
207   if (POINTER_TYPE_P (type)
208       || TREE_CODE (type) == OFFSET_TYPE)
209     prec = POINTER_SIZE;
210   else
211     prec = TYPE_PRECISION (type);
212
213   /* Size types *are* sign extended.  */
214   sign_extended_type = (!TYPE_UNSIGNED (type)
215                         || (TREE_CODE (type) == INTEGER_TYPE
216                             && TYPE_IS_SIZETYPE (type)));
217
218   /* First clear all bits that are beyond the type's precision.  */
219   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
220     ;
221   else if (prec > HOST_BITS_PER_WIDE_INT)
222     h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
223   else
224     {
225       h1 = 0;
226       if (prec < HOST_BITS_PER_WIDE_INT)
227         l1 &= ~((HOST_WIDE_INT) (-1) << prec);
228     }
229
230   /* Then do sign extension if necessary.  */
231   if (!sign_extended_type)
232     /* No sign extension */;
233   else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
234     /* Correct width already.  */;
235   else if (prec > HOST_BITS_PER_WIDE_INT)
236     {
237       /* Sign extend top half? */
238       if (h1 & ((unsigned HOST_WIDE_INT)1
239                 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
240         h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
241     }
242   else if (prec == HOST_BITS_PER_WIDE_INT)
243     {
244       if ((HOST_WIDE_INT)l1 < 0)
245         h1 = -1;
246     }
247   else
248     {
249       /* Sign extend bottom half? */
250       if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
251         {
252           h1 = -1;
253           l1 |= (HOST_WIDE_INT)(-1) << prec;
254         }
255     }
256
257   *lv = l1;
258   *hv = h1;
259
260   /* If the value didn't fit, signal overflow.  */
261   return l1 != low0 || h1 != high0;
262 }
263
264 /* We force the double-int HIGH:LOW to the range of the type TYPE by
265    sign or zero extending it.
266    OVERFLOWABLE indicates if we are interested
267    in overflow of the value, when >0 we are only interested in signed
268    overflow, for <0 we are interested in any overflow.  OVERFLOWED
269    indicates whether overflow has already occurred.  CONST_OVERFLOWED
270    indicates whether constant overflow has already occurred.  We force
271    T's value to be within range of T's type (by setting to 0 or 1 all
272    the bits outside the type's range).  We set TREE_OVERFLOWED if,
273         OVERFLOWED is nonzero,
274         or OVERFLOWABLE is >0 and signed overflow occurs
275         or OVERFLOWABLE is <0 and any overflow occurs
276    We return a new tree node for the extended double-int.  The node
277    is shared if no overflow flags are set.  */
278
279 tree
280 force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
281                        HOST_WIDE_INT high, int overflowable,
282                        bool overflowed)
283 {
284   int sign_extended_type;
285   bool overflow;
286
287   /* Size types *are* sign extended.  */
288   sign_extended_type = (!TYPE_UNSIGNED (type)
289                         || (TREE_CODE (type) == INTEGER_TYPE
290                             && TYPE_IS_SIZETYPE (type)));
291
292   overflow = fit_double_type (low, high, &low, &high, type);
293
294   /* If we need to set overflow flags, return a new unshared node.  */
295   if (overflowed || overflow)
296     {
297       if (overflowed
298           || overflowable < 0
299           || (overflowable > 0 && sign_extended_type))
300         {
301           tree t = make_node (INTEGER_CST);
302           TREE_INT_CST_LOW (t) = low;
303           TREE_INT_CST_HIGH (t) = high;
304           TREE_TYPE (t) = type;
305           TREE_OVERFLOW (t) = 1;
306           return t;
307         }
308     }
309
310   /* Else build a shared node.  */
311   return build_int_cst_wide (type, low, high);
312 }
313 \f
314 /* Add two doubleword integers with doubleword result.
315    Return nonzero if the operation overflows according to UNSIGNED_P.
316    Each argument is given as two `HOST_WIDE_INT' pieces.
317    One argument is L1 and H1; the other, L2 and H2.
318    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
319
320 int
321 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
322                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
323                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
324                       bool unsigned_p)
325 {
326   unsigned HOST_WIDE_INT l;
327   HOST_WIDE_INT h;
328
329   l = l1 + l2;
330   h = h1 + h2 + (l < l1);
331
332   *lv = l;
333   *hv = h;
334
335   if (unsigned_p)
336     return (unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1;
337   else
338     return OVERFLOW_SUM_SIGN (h1, h2, h);
339 }
340
341 /* Negate a doubleword integer with doubleword result.
342    Return nonzero if the operation overflows, assuming it's signed.
343    The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
344    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
345
346 int
347 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
348             unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
349 {
350   if (l1 == 0)
351     {
352       *lv = 0;
353       *hv = - h1;
354       return (*hv & h1) < 0;
355     }
356   else
357     {
358       *lv = -l1;
359       *hv = ~h1;
360       return 0;
361     }
362 }
363 \f
364 /* Multiply two doubleword integers with doubleword result.
365    Return nonzero if the operation overflows according to UNSIGNED_P.
366    Each argument is given as two `HOST_WIDE_INT' pieces.
367    One argument is L1 and H1; the other, L2 and H2.
368    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
369
370 int
371 mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
372                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
373                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
374                       bool unsigned_p)
375 {
376   HOST_WIDE_INT arg1[4];
377   HOST_WIDE_INT arg2[4];
378   HOST_WIDE_INT prod[4 * 2];
379   unsigned HOST_WIDE_INT carry;
380   int i, j, k;
381   unsigned HOST_WIDE_INT toplow, neglow;
382   HOST_WIDE_INT tophigh, neghigh;
383
384   encode (arg1, l1, h1);
385   encode (arg2, l2, h2);
386
387   memset (prod, 0, sizeof prod);
388
389   for (i = 0; i < 4; i++)
390     {
391       carry = 0;
392       for (j = 0; j < 4; j++)
393         {
394           k = i + j;
395           /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000.  */
396           carry += arg1[i] * arg2[j];
397           /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF.  */
398           carry += prod[k];
399           prod[k] = LOWPART (carry);
400           carry = HIGHPART (carry);
401         }
402       prod[i + 4] = carry;
403     }
404
405   decode (prod, lv, hv);
406   decode (prod + 4, &toplow, &tophigh);
407
408   /* Unsigned overflow is immediate.  */
409   if (unsigned_p)
410     return (toplow | tophigh) != 0;
411
412   /* Check for signed overflow by calculating the signed representation of the
413      top half of the result; it should agree with the low half's sign bit.  */
414   if (h1 < 0)
415     {
416       neg_double (l2, h2, &neglow, &neghigh);
417       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
418     }
419   if (h2 < 0)
420     {
421       neg_double (l1, h1, &neglow, &neghigh);
422       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
423     }
424   return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
425 }
426 \f
427 /* Shift the doubleword integer in L1, H1 left by COUNT places
428    keeping only PREC bits of result.
429    Shift right if COUNT is negative.
430    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
431    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
432
433 void
434 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
435                HOST_WIDE_INT count, unsigned int prec,
436                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
437 {
438   unsigned HOST_WIDE_INT signmask;
439
440   if (count < 0)
441     {
442       rshift_double (l1, h1, -count, prec, lv, hv, arith);
443       return;
444     }
445
446   if (SHIFT_COUNT_TRUNCATED)
447     count %= prec;
448
449   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
450     {
451       /* Shifting by the host word size is undefined according to the
452          ANSI standard, so we must handle this as a special case.  */
453       *hv = 0;
454       *lv = 0;
455     }
456   else if (count >= HOST_BITS_PER_WIDE_INT)
457     {
458       *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
459       *lv = 0;
460     }
461   else
462     {
463       *hv = (((unsigned HOST_WIDE_INT) h1 << count)
464              | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
465       *lv = l1 << count;
466     }
467
468   /* Sign extend all bits that are beyond the precision.  */
469
470   signmask = -((prec > HOST_BITS_PER_WIDE_INT
471                 ? ((unsigned HOST_WIDE_INT) *hv
472                    >> (prec - HOST_BITS_PER_WIDE_INT - 1))
473                 : (*lv >> (prec - 1))) & 1);
474
475   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
476     ;
477   else if (prec >= HOST_BITS_PER_WIDE_INT)
478     {
479       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
480       *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
481     }
482   else
483     {
484       *hv = signmask;
485       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
486       *lv |= signmask << prec;
487     }
488 }
489
490 /* Shift the doubleword integer in L1, H1 right by COUNT places
491    keeping only PREC bits of result.  COUNT must be positive.
492    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
493    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
494
495 void
496 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
497                HOST_WIDE_INT count, unsigned int prec,
498                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
499                int arith)
500 {
501   unsigned HOST_WIDE_INT signmask;
502
503   signmask = (arith
504               ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
505               : 0);
506
507   if (SHIFT_COUNT_TRUNCATED)
508     count %= prec;
509
510   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
511     {
512       /* Shifting by the host word size is undefined according to the
513          ANSI standard, so we must handle this as a special case.  */
514       *hv = 0;
515       *lv = 0;
516     }
517   else if (count >= HOST_BITS_PER_WIDE_INT)
518     {
519       *hv = 0;
520       *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
521     }
522   else
523     {
524       *hv = (unsigned HOST_WIDE_INT) h1 >> count;
525       *lv = ((l1 >> count)
526              | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
527     }
528
529   /* Zero / sign extend all bits that are beyond the precision.  */
530
531   if (count >= (HOST_WIDE_INT)prec)
532     {
533       *hv = signmask;
534       *lv = signmask;
535     }
536   else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
537     ;
538   else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
539     {
540       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
541       *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
542     }
543   else
544     {
545       *hv = signmask;
546       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
547       *lv |= signmask << (prec - count);
548     }
549 }
550 \f
551 /* Rotate the doubleword integer in L1, H1 left by COUNT places
552    keeping only PREC bits of result.
553    Rotate right if COUNT is negative.
554    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
555
556 void
557 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
558                 HOST_WIDE_INT count, unsigned int prec,
559                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
560 {
561   unsigned HOST_WIDE_INT s1l, s2l;
562   HOST_WIDE_INT s1h, s2h;
563
564   count %= prec;
565   if (count < 0)
566     count += prec;
567
568   lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
569   rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
570   *lv = s1l | s2l;
571   *hv = s1h | s2h;
572 }
573
574 /* Rotate the doubleword integer in L1, H1 left by COUNT places
575    keeping only PREC bits of result.  COUNT must be positive.
576    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
577
578 void
579 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
580                 HOST_WIDE_INT count, unsigned int prec,
581                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
582 {
583   unsigned HOST_WIDE_INT s1l, s2l;
584   HOST_WIDE_INT s1h, s2h;
585
586   count %= prec;
587   if (count < 0)
588     count += prec;
589
590   rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
591   lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
592   *lv = s1l | s2l;
593   *hv = s1h | s2h;
594 }
595 \f
596 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
597    for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
598    CODE is a tree code for a kind of division, one of
599    TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
600    or EXACT_DIV_EXPR
601    It controls how the quotient is rounded to an integer.
602    Return nonzero if the operation overflows.
603    UNS nonzero says do unsigned division.  */
604
605 int
606 div_and_round_double (enum tree_code code, int uns,
607                       unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
608                       HOST_WIDE_INT hnum_orig,
609                       unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
610                       HOST_WIDE_INT hden_orig,
611                       unsigned HOST_WIDE_INT *lquo,
612                       HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
613                       HOST_WIDE_INT *hrem)
614 {
615   int quo_neg = 0;
616   HOST_WIDE_INT num[4 + 1];     /* extra element for scaling.  */
617   HOST_WIDE_INT den[4], quo[4];
618   int i, j;
619   unsigned HOST_WIDE_INT work;
620   unsigned HOST_WIDE_INT carry = 0;
621   unsigned HOST_WIDE_INT lnum = lnum_orig;
622   HOST_WIDE_INT hnum = hnum_orig;
623   unsigned HOST_WIDE_INT lden = lden_orig;
624   HOST_WIDE_INT hden = hden_orig;
625   int overflow = 0;
626
627   if (hden == 0 && lden == 0)
628     overflow = 1, lden = 1;
629
630   /* Calculate quotient sign and convert operands to unsigned.  */
631   if (!uns)
632     {
633       if (hnum < 0)
634         {
635           quo_neg = ~ quo_neg;
636           /* (minimum integer) / (-1) is the only overflow case.  */
637           if (neg_double (lnum, hnum, &lnum, &hnum)
638               && ((HOST_WIDE_INT) lden & hden) == -1)
639             overflow = 1;
640         }
641       if (hden < 0)
642         {
643           quo_neg = ~ quo_neg;
644           neg_double (lden, hden, &lden, &hden);
645         }
646     }
647
648   if (hnum == 0 && hden == 0)
649     {                           /* single precision */
650       *hquo = *hrem = 0;
651       /* This unsigned division rounds toward zero.  */
652       *lquo = lnum / lden;
653       goto finish_up;
654     }
655
656   if (hnum == 0)
657     {                           /* trivial case: dividend < divisor */
658       /* hden != 0 already checked.  */
659       *hquo = *lquo = 0;
660       *hrem = hnum;
661       *lrem = lnum;
662       goto finish_up;
663     }
664
665   memset (quo, 0, sizeof quo);
666
667   memset (num, 0, sizeof num);  /* to zero 9th element */
668   memset (den, 0, sizeof den);
669
670   encode (num, lnum, hnum);
671   encode (den, lden, hden);
672
673   /* Special code for when the divisor < BASE.  */
674   if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
675     {
676       /* hnum != 0 already checked.  */
677       for (i = 4 - 1; i >= 0; i--)
678         {
679           work = num[i] + carry * BASE;
680           quo[i] = work / lden;
681           carry = work % lden;
682         }
683     }
684   else
685     {
686       /* Full double precision division,
687          with thanks to Don Knuth's "Seminumerical Algorithms".  */
688       int num_hi_sig, den_hi_sig;
689       unsigned HOST_WIDE_INT quo_est, scale;
690
691       /* Find the highest nonzero divisor digit.  */
692       for (i = 4 - 1;; i--)
693         if (den[i] != 0)
694           {
695             den_hi_sig = i;
696             break;
697           }
698
699       /* Insure that the first digit of the divisor is at least BASE/2.
700          This is required by the quotient digit estimation algorithm.  */
701
702       scale = BASE / (den[den_hi_sig] + 1);
703       if (scale > 1)
704         {               /* scale divisor and dividend */
705           carry = 0;
706           for (i = 0; i <= 4 - 1; i++)
707             {
708               work = (num[i] * scale) + carry;
709               num[i] = LOWPART (work);
710               carry = HIGHPART (work);
711             }
712
713           num[4] = carry;
714           carry = 0;
715           for (i = 0; i <= 4 - 1; i++)
716             {
717               work = (den[i] * scale) + carry;
718               den[i] = LOWPART (work);
719               carry = HIGHPART (work);
720               if (den[i] != 0) den_hi_sig = i;
721             }
722         }
723
724       num_hi_sig = 4;
725
726       /* Main loop */
727       for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
728         {
729           /* Guess the next quotient digit, quo_est, by dividing the first
730              two remaining dividend digits by the high order quotient digit.
731              quo_est is never low and is at most 2 high.  */
732           unsigned HOST_WIDE_INT tmp;
733
734           num_hi_sig = i + den_hi_sig + 1;
735           work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
736           if (num[num_hi_sig] != den[den_hi_sig])
737             quo_est = work / den[den_hi_sig];
738           else
739             quo_est = BASE - 1;
740
741           /* Refine quo_est so it's usually correct, and at most one high.  */
742           tmp = work - quo_est * den[den_hi_sig];
743           if (tmp < BASE
744               && (den[den_hi_sig - 1] * quo_est
745                   > (tmp * BASE + num[num_hi_sig - 2])))
746             quo_est--;
747
748           /* Try QUO_EST as the quotient digit, by multiplying the
749              divisor by QUO_EST and subtracting from the remaining dividend.
750              Keep in mind that QUO_EST is the I - 1st digit.  */
751
752           carry = 0;
753           for (j = 0; j <= den_hi_sig; j++)
754             {
755               work = quo_est * den[j] + carry;
756               carry = HIGHPART (work);
757               work = num[i + j] - LOWPART (work);
758               num[i + j] = LOWPART (work);
759               carry += HIGHPART (work) != 0;
760             }
761
762           /* If quo_est was high by one, then num[i] went negative and
763              we need to correct things.  */
764           if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
765             {
766               quo_est--;
767               carry = 0;                /* add divisor back in */
768               for (j = 0; j <= den_hi_sig; j++)
769                 {
770                   work = num[i + j] + den[j] + carry;
771                   carry = HIGHPART (work);
772                   num[i + j] = LOWPART (work);
773                 }
774
775               num [num_hi_sig] += carry;
776             }
777
778           /* Store the quotient digit.  */
779           quo[i] = quo_est;
780         }
781     }
782
783   decode (quo, lquo, hquo);
784
785  finish_up:
786   /* If result is negative, make it so.  */
787   if (quo_neg)
788     neg_double (*lquo, *hquo, lquo, hquo);
789
790   /* Compute trial remainder:  rem = num - (quo * den)  */
791   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
792   neg_double (*lrem, *hrem, lrem, hrem);
793   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
794
795   switch (code)
796     {
797     case TRUNC_DIV_EXPR:
798     case TRUNC_MOD_EXPR:        /* round toward zero */
799     case EXACT_DIV_EXPR:        /* for this one, it shouldn't matter */
800       return overflow;
801
802     case FLOOR_DIV_EXPR:
803     case FLOOR_MOD_EXPR:        /* round toward negative infinity */
804       if (quo_neg && (*lrem != 0 || *hrem != 0))   /* ratio < 0 && rem != 0 */
805         {
806           /* quo = quo - 1;  */
807           add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT)  -1,
808                       lquo, hquo);
809         }
810       else
811         return overflow;
812       break;
813
814     case CEIL_DIV_EXPR:
815     case CEIL_MOD_EXPR:         /* round toward positive infinity */
816       if (!quo_neg && (*lrem != 0 || *hrem != 0))  /* ratio > 0 && rem != 0 */
817         {
818           add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
819                       lquo, hquo);
820         }
821       else
822         return overflow;
823       break;
824
825     case ROUND_DIV_EXPR:
826     case ROUND_MOD_EXPR:        /* round to closest integer */
827       {
828         unsigned HOST_WIDE_INT labs_rem = *lrem;
829         HOST_WIDE_INT habs_rem = *hrem;
830         unsigned HOST_WIDE_INT labs_den = lden, ltwice;
831         HOST_WIDE_INT habs_den = hden, htwice;
832
833         /* Get absolute values.  */
834         if (*hrem < 0)
835           neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
836         if (hden < 0)
837           neg_double (lden, hden, &labs_den, &habs_den);
838
839         /* If (2 * abs (lrem) >= abs (lden)), adjust the quotient.  */
840         mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
841                     labs_rem, habs_rem, &ltwice, &htwice);
842
843         if (((unsigned HOST_WIDE_INT) habs_den
844              < (unsigned HOST_WIDE_INT) htwice)
845             || (((unsigned HOST_WIDE_INT) habs_den
846                  == (unsigned HOST_WIDE_INT) htwice)
847                 && (labs_den <= ltwice)))
848           {
849             if (*hquo < 0)
850               /* quo = quo - 1;  */
851               add_double (*lquo, *hquo,
852                           (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
853             else
854               /* quo = quo + 1; */
855               add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
856                           lquo, hquo);
857           }
858         else
859           return overflow;
860       }
861       break;
862
863     default:
864       gcc_unreachable ();
865     }
866
867   /* Compute true remainder:  rem = num - (quo * den)  */
868   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
869   neg_double (*lrem, *hrem, lrem, hrem);
870   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
871   return overflow;
872 }
873
874 /* If ARG2 divides ARG1 with zero remainder, carries out the division
875    of type CODE and returns the quotient.
876    Otherwise returns NULL_TREE.  */
877
878 tree
879 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
880 {
881   unsigned HOST_WIDE_INT int1l, int2l;
882   HOST_WIDE_INT int1h, int2h;
883   unsigned HOST_WIDE_INT quol, reml;
884   HOST_WIDE_INT quoh, remh;
885   tree type = TREE_TYPE (arg1);
886   int uns = TYPE_UNSIGNED (type);
887
888   int1l = TREE_INT_CST_LOW (arg1);
889   int1h = TREE_INT_CST_HIGH (arg1);
890   /* &obj[0] + -128 really should be compiled as &obj[-8] rather than
891      &obj[some_exotic_number].  */
892   if (POINTER_TYPE_P (type))
893     {
894       uns = false;
895       type = signed_type_for (type);
896       fit_double_type (int1l, int1h, &int1l, &int1h,
897                        type);
898     }
899   else
900     fit_double_type (int1l, int1h, &int1l, &int1h, type);
901   int2l = TREE_INT_CST_LOW (arg2);
902   int2h = TREE_INT_CST_HIGH (arg2);
903
904   div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
905                         &quol, &quoh, &reml, &remh);
906   if (remh != 0 || reml != 0)
907     return NULL_TREE;
908
909   return build_int_cst_wide (type, quol, quoh);
910 }
911 \f
912 /* This is nonzero if we should defer warnings about undefined
913    overflow.  This facility exists because these warnings are a
914    special case.  The code to estimate loop iterations does not want
915    to issue any warnings, since it works with expressions which do not
916    occur in user code.  Various bits of cleanup code call fold(), but
917    only use the result if it has certain characteristics (e.g., is a
918    constant); that code only wants to issue a warning if the result is
919    used.  */
920
921 static int fold_deferring_overflow_warnings;
922
923 /* If a warning about undefined overflow is deferred, this is the
924    warning.  Note that this may cause us to turn two warnings into
925    one, but that is fine since it is sufficient to only give one
926    warning per expression.  */
927
928 static const char* fold_deferred_overflow_warning;
929
930 /* If a warning about undefined overflow is deferred, this is the
931    level at which the warning should be emitted.  */
932
933 static enum warn_strict_overflow_code fold_deferred_overflow_code;
934
935 /* Start deferring overflow warnings.  We could use a stack here to
936    permit nested calls, but at present it is not necessary.  */
937
938 void
939 fold_defer_overflow_warnings (void)
940 {
941   ++fold_deferring_overflow_warnings;
942 }
943
944 /* Stop deferring overflow warnings.  If there is a pending warning,
945    and ISSUE is true, then issue the warning if appropriate.  STMT is
946    the statement with which the warning should be associated (used for
947    location information); STMT may be NULL.  CODE is the level of the
948    warning--a warn_strict_overflow_code value.  This function will use
949    the smaller of CODE and the deferred code when deciding whether to
950    issue the warning.  CODE may be zero to mean to always use the
951    deferred code.  */
952
953 void
954 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
955 {
956   const char *warnmsg;
957   location_t locus;
958
959   gcc_assert (fold_deferring_overflow_warnings > 0);
960   --fold_deferring_overflow_warnings;
961   if (fold_deferring_overflow_warnings > 0)
962     {
963       if (fold_deferred_overflow_warning != NULL
964           && code != 0
965           && code < (int) fold_deferred_overflow_code)
966         fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
967       return;
968     }
969
970   warnmsg = fold_deferred_overflow_warning;
971   fold_deferred_overflow_warning = NULL;
972
973   if (!issue || warnmsg == NULL)
974     return;
975
976   if (gimple_no_warning_p (stmt))
977     return;
978
979   /* Use the smallest code level when deciding to issue the
980      warning.  */
981   if (code == 0 || code > (int) fold_deferred_overflow_code)
982     code = fold_deferred_overflow_code;
983
984   if (!issue_strict_overflow_warning (code))
985     return;
986
987   if (stmt == NULL)
988     locus = input_location;
989   else
990     locus = gimple_location (stmt);
991   warning (OPT_Wstrict_overflow, "%H%s", &locus, warnmsg);
992 }
993
994 /* Stop deferring overflow warnings, ignoring any deferred
995    warnings.  */
996
997 void
998 fold_undefer_and_ignore_overflow_warnings (void)
999 {
1000   fold_undefer_overflow_warnings (false, NULL, 0);
1001 }
1002
1003 /* Whether we are deferring overflow warnings.  */
1004
1005 bool
1006 fold_deferring_overflow_warnings_p (void)
1007 {
1008   return fold_deferring_overflow_warnings > 0;
1009 }
1010
1011 /* This is called when we fold something based on the fact that signed
1012    overflow is undefined.  */
1013
1014 static void
1015 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
1016 {
1017   if (fold_deferring_overflow_warnings > 0)
1018     {
1019       if (fold_deferred_overflow_warning == NULL
1020           || wc < fold_deferred_overflow_code)
1021         {
1022           fold_deferred_overflow_warning = gmsgid;
1023           fold_deferred_overflow_code = wc;
1024         }
1025     }
1026   else if (issue_strict_overflow_warning (wc))
1027     warning (OPT_Wstrict_overflow, gmsgid);
1028 }
1029 \f
1030 /* Return true if the built-in mathematical function specified by CODE
1031    is odd, i.e. -f(x) == f(-x).  */
1032
1033 static bool
1034 negate_mathfn_p (enum built_in_function code)
1035 {
1036   switch (code)
1037     {
1038     CASE_FLT_FN (BUILT_IN_ASIN):
1039     CASE_FLT_FN (BUILT_IN_ASINH):
1040     CASE_FLT_FN (BUILT_IN_ATAN):
1041     CASE_FLT_FN (BUILT_IN_ATANH):
1042     CASE_FLT_FN (BUILT_IN_CASIN):
1043     CASE_FLT_FN (BUILT_IN_CASINH):
1044     CASE_FLT_FN (BUILT_IN_CATAN):
1045     CASE_FLT_FN (BUILT_IN_CATANH):
1046     CASE_FLT_FN (BUILT_IN_CBRT):
1047     CASE_FLT_FN (BUILT_IN_CPROJ):
1048     CASE_FLT_FN (BUILT_IN_CSIN):
1049     CASE_FLT_FN (BUILT_IN_CSINH):
1050     CASE_FLT_FN (BUILT_IN_CTAN):
1051     CASE_FLT_FN (BUILT_IN_CTANH):
1052     CASE_FLT_FN (BUILT_IN_ERF):
1053     CASE_FLT_FN (BUILT_IN_LLROUND):
1054     CASE_FLT_FN (BUILT_IN_LROUND):
1055     CASE_FLT_FN (BUILT_IN_ROUND):
1056     CASE_FLT_FN (BUILT_IN_SIN):
1057     CASE_FLT_FN (BUILT_IN_SINH):
1058     CASE_FLT_FN (BUILT_IN_TAN):
1059     CASE_FLT_FN (BUILT_IN_TANH):
1060     CASE_FLT_FN (BUILT_IN_TRUNC):
1061       return true;
1062
1063     CASE_FLT_FN (BUILT_IN_LLRINT):
1064     CASE_FLT_FN (BUILT_IN_LRINT):
1065     CASE_FLT_FN (BUILT_IN_NEARBYINT):
1066     CASE_FLT_FN (BUILT_IN_RINT):
1067       return !flag_rounding_math;
1068     
1069     default:
1070       break;
1071     }
1072   return false;
1073 }
1074
1075 /* Check whether we may negate an integer constant T without causing
1076    overflow.  */
1077
1078 bool
1079 may_negate_without_overflow_p (const_tree t)
1080 {
1081   unsigned HOST_WIDE_INT val;
1082   unsigned int prec;
1083   tree type;
1084
1085   gcc_assert (TREE_CODE (t) == INTEGER_CST);
1086
1087   type = TREE_TYPE (t);
1088   if (TYPE_UNSIGNED (type))
1089     return false;
1090
1091   prec = TYPE_PRECISION (type);
1092   if (prec > HOST_BITS_PER_WIDE_INT)
1093     {
1094       if (TREE_INT_CST_LOW (t) != 0)
1095         return true;
1096       prec -= HOST_BITS_PER_WIDE_INT;
1097       val = TREE_INT_CST_HIGH (t);
1098     }
1099   else
1100     val = TREE_INT_CST_LOW (t);
1101   if (prec < HOST_BITS_PER_WIDE_INT)
1102     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1103   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
1104 }
1105
1106 /* Determine whether an expression T can be cheaply negated using
1107    the function negate_expr without introducing undefined overflow.  */
1108
1109 static bool
1110 negate_expr_p (tree t)
1111 {
1112   tree type;
1113
1114   if (t == 0)
1115     return false;
1116
1117   type = TREE_TYPE (t);
1118
1119   STRIP_SIGN_NOPS (t);
1120   switch (TREE_CODE (t))
1121     {
1122     case INTEGER_CST:
1123       if (TYPE_OVERFLOW_WRAPS (type))
1124         return true;
1125
1126       /* Check that -CST will not overflow type.  */
1127       return may_negate_without_overflow_p (t);
1128     case BIT_NOT_EXPR:
1129       return (INTEGRAL_TYPE_P (type)
1130               && TYPE_OVERFLOW_WRAPS (type));
1131
1132     case FIXED_CST:
1133     case REAL_CST:
1134     case NEGATE_EXPR:
1135       return true;
1136
1137     case COMPLEX_CST:
1138       return negate_expr_p (TREE_REALPART (t))
1139              && negate_expr_p (TREE_IMAGPART (t));
1140
1141     case COMPLEX_EXPR:
1142       return negate_expr_p (TREE_OPERAND (t, 0))
1143              && negate_expr_p (TREE_OPERAND (t, 1));
1144
1145     case CONJ_EXPR:
1146       return negate_expr_p (TREE_OPERAND (t, 0));
1147
1148     case PLUS_EXPR:
1149       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1150           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1151         return false;
1152       /* -(A + B) -> (-B) - A.  */
1153       if (negate_expr_p (TREE_OPERAND (t, 1))
1154           && reorder_operands_p (TREE_OPERAND (t, 0),
1155                                  TREE_OPERAND (t, 1)))
1156         return true;
1157       /* -(A + B) -> (-A) - B.  */
1158       return negate_expr_p (TREE_OPERAND (t, 0));
1159
1160     case MINUS_EXPR:
1161       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
1162       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1163              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1164              && reorder_operands_p (TREE_OPERAND (t, 0),
1165                                     TREE_OPERAND (t, 1));
1166
1167     case MULT_EXPR:
1168       if (TYPE_UNSIGNED (TREE_TYPE (t)))
1169         break;
1170
1171       /* Fall through.  */
1172
1173     case RDIV_EXPR:
1174       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1175         return negate_expr_p (TREE_OPERAND (t, 1))
1176                || negate_expr_p (TREE_OPERAND (t, 0));
1177       break;
1178
1179     case TRUNC_DIV_EXPR:
1180     case ROUND_DIV_EXPR:
1181     case FLOOR_DIV_EXPR:
1182     case CEIL_DIV_EXPR:
1183     case EXACT_DIV_EXPR:
1184       /* In general we can't negate A / B, because if A is INT_MIN and
1185          B is 1, we may turn this into INT_MIN / -1 which is undefined
1186          and actually traps on some architectures.  But if overflow is
1187          undefined, we can negate, because - (INT_MIN / 1) is an
1188          overflow.  */
1189       if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1190           && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
1191         break;
1192       return negate_expr_p (TREE_OPERAND (t, 1))
1193              || negate_expr_p (TREE_OPERAND (t, 0));
1194
1195     case NOP_EXPR:
1196       /* Negate -((double)float) as (double)(-float).  */
1197       if (TREE_CODE (type) == REAL_TYPE)
1198         {
1199           tree tem = strip_float_extensions (t);
1200           if (tem != t)
1201             return negate_expr_p (tem);
1202         }
1203       break;
1204
1205     case CALL_EXPR:
1206       /* Negate -f(x) as f(-x).  */
1207       if (negate_mathfn_p (builtin_mathfn_code (t)))
1208         return negate_expr_p (CALL_EXPR_ARG (t, 0));
1209       break;
1210
1211     case RSHIFT_EXPR:
1212       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1213       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1214         {
1215           tree op1 = TREE_OPERAND (t, 1);
1216           if (TREE_INT_CST_HIGH (op1) == 0
1217               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1218                  == TREE_INT_CST_LOW (op1))
1219             return true;
1220         }
1221       break;
1222
1223     default:
1224       break;
1225     }
1226   return false;
1227 }
1228
1229 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1230    simplification is possible.
1231    If negate_expr_p would return true for T, NULL_TREE will never be
1232    returned.  */
1233
1234 static tree
1235 fold_negate_expr (tree t)
1236 {
1237   tree type = TREE_TYPE (t);
1238   tree tem;
1239
1240   switch (TREE_CODE (t))
1241     {
1242     /* Convert - (~A) to A + 1.  */
1243     case BIT_NOT_EXPR:
1244       if (INTEGRAL_TYPE_P (type))
1245         return fold_build2 (PLUS_EXPR, type, TREE_OPERAND (t, 0),
1246                             build_int_cst (type, 1));
1247       break;
1248       
1249     case INTEGER_CST:
1250       tem = fold_negate_const (t, type);
1251       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
1252           || !TYPE_OVERFLOW_TRAPS (type))
1253         return tem;
1254       break;
1255
1256     case REAL_CST:
1257       tem = fold_negate_const (t, type);
1258       /* Two's complement FP formats, such as c4x, may overflow.  */
1259       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1260         return tem;
1261       break;
1262
1263     case FIXED_CST:
1264       tem = fold_negate_const (t, type);
1265       return tem;
1266
1267     case COMPLEX_CST:
1268       {
1269         tree rpart = negate_expr (TREE_REALPART (t));
1270         tree ipart = negate_expr (TREE_IMAGPART (t));
1271
1272         if ((TREE_CODE (rpart) == REAL_CST
1273              && TREE_CODE (ipart) == REAL_CST)
1274             || (TREE_CODE (rpart) == INTEGER_CST
1275                 && TREE_CODE (ipart) == INTEGER_CST))
1276           return build_complex (type, rpart, ipart);
1277       }
1278       break;
1279
1280     case COMPLEX_EXPR:
1281       if (negate_expr_p (t))
1282         return fold_build2 (COMPLEX_EXPR, type,
1283                             fold_negate_expr (TREE_OPERAND (t, 0)),
1284                             fold_negate_expr (TREE_OPERAND (t, 1)));
1285       break;
1286       
1287     case CONJ_EXPR:
1288       if (negate_expr_p (t))
1289         return fold_build1 (CONJ_EXPR, type,
1290                             fold_negate_expr (TREE_OPERAND (t, 0)));
1291       break;
1292
1293     case NEGATE_EXPR:
1294       return TREE_OPERAND (t, 0);
1295
1296     case PLUS_EXPR:
1297       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1298           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1299         {
1300           /* -(A + B) -> (-B) - A.  */
1301           if (negate_expr_p (TREE_OPERAND (t, 1))
1302               && reorder_operands_p (TREE_OPERAND (t, 0),
1303                                      TREE_OPERAND (t, 1)))
1304             {
1305               tem = negate_expr (TREE_OPERAND (t, 1));
1306               return fold_build2 (MINUS_EXPR, type,
1307                                   tem, TREE_OPERAND (t, 0));
1308             }
1309
1310           /* -(A + B) -> (-A) - B.  */
1311           if (negate_expr_p (TREE_OPERAND (t, 0)))
1312             {
1313               tem = negate_expr (TREE_OPERAND (t, 0));
1314               return fold_build2 (MINUS_EXPR, type,
1315                                   tem, TREE_OPERAND (t, 1));
1316             }
1317         }
1318       break;
1319
1320     case MINUS_EXPR:
1321       /* - (A - B) -> B - A  */
1322       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1323           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1324           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1325         return fold_build2 (MINUS_EXPR, type,
1326                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
1327       break;
1328
1329     case MULT_EXPR:
1330       if (TYPE_UNSIGNED (type))
1331         break;
1332
1333       /* Fall through.  */
1334
1335     case RDIV_EXPR:
1336       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
1337         {
1338           tem = TREE_OPERAND (t, 1);
1339           if (negate_expr_p (tem))
1340             return fold_build2 (TREE_CODE (t), type,
1341                                 TREE_OPERAND (t, 0), negate_expr (tem));
1342           tem = TREE_OPERAND (t, 0);
1343           if (negate_expr_p (tem))
1344             return fold_build2 (TREE_CODE (t), type,
1345                                 negate_expr (tem), TREE_OPERAND (t, 1));
1346         }
1347       break;
1348
1349     case TRUNC_DIV_EXPR:
1350     case ROUND_DIV_EXPR:
1351     case FLOOR_DIV_EXPR:
1352     case CEIL_DIV_EXPR:
1353     case EXACT_DIV_EXPR:
1354       /* In general we can't negate A / B, because if A is INT_MIN and
1355          B is 1, we may turn this into INT_MIN / -1 which is undefined
1356          and actually traps on some architectures.  But if overflow is
1357          undefined, we can negate, because - (INT_MIN / 1) is an
1358          overflow.  */
1359       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
1360         {
1361           const char * const warnmsg = G_("assuming signed overflow does not "
1362                                           "occur when negating a division");
1363           tem = TREE_OPERAND (t, 1);
1364           if (negate_expr_p (tem))
1365             {
1366               if (INTEGRAL_TYPE_P (type)
1367                   && (TREE_CODE (tem) != INTEGER_CST
1368                       || integer_onep (tem)))
1369                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1370               return fold_build2 (TREE_CODE (t), type,
1371                                   TREE_OPERAND (t, 0), negate_expr (tem));
1372             }
1373           tem = TREE_OPERAND (t, 0);
1374           if (negate_expr_p (tem))
1375             {
1376               if (INTEGRAL_TYPE_P (type)
1377                   && (TREE_CODE (tem) != INTEGER_CST
1378                       || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
1379                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1380               return fold_build2 (TREE_CODE (t), type,
1381                                   negate_expr (tem), TREE_OPERAND (t, 1));
1382             }
1383         }
1384       break;
1385
1386     case NOP_EXPR:
1387       /* Convert -((double)float) into (double)(-float).  */
1388       if (TREE_CODE (type) == REAL_TYPE)
1389         {
1390           tem = strip_float_extensions (t);
1391           if (tem != t && negate_expr_p (tem))
1392             return fold_convert (type, negate_expr (tem));
1393         }
1394       break;
1395
1396     case CALL_EXPR:
1397       /* Negate -f(x) as f(-x).  */
1398       if (negate_mathfn_p (builtin_mathfn_code (t))
1399           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
1400         {
1401           tree fndecl, arg;
1402
1403           fndecl = get_callee_fndecl (t);
1404           arg = negate_expr (CALL_EXPR_ARG (t, 0));
1405           return build_call_expr (fndecl, 1, arg);
1406         }
1407       break;
1408
1409     case RSHIFT_EXPR:
1410       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1411       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1412         {
1413           tree op1 = TREE_OPERAND (t, 1);
1414           if (TREE_INT_CST_HIGH (op1) == 0
1415               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1416                  == TREE_INT_CST_LOW (op1))
1417             {
1418               tree ntype = TYPE_UNSIGNED (type)
1419                            ? signed_type_for (type)
1420                            : unsigned_type_for (type);
1421               tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1422               temp = fold_build2 (RSHIFT_EXPR, ntype, temp, op1);
1423               return fold_convert (type, temp);
1424             }
1425         }
1426       break;
1427
1428     default:
1429       break;
1430     }
1431
1432   return NULL_TREE;
1433 }
1434
1435 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1436    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
1437    return NULL_TREE. */
1438
1439 static tree
1440 negate_expr (tree t)
1441 {
1442   tree type, tem;
1443
1444   if (t == NULL_TREE)
1445     return NULL_TREE;
1446
1447   type = TREE_TYPE (t);
1448   STRIP_SIGN_NOPS (t);
1449
1450   tem = fold_negate_expr (t);
1451   if (!tem)
1452     tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1453   return fold_convert (type, tem);
1454 }
1455 \f
1456 /* Split a tree IN into a constant, literal and variable parts that could be
1457    combined with CODE to make IN.  "constant" means an expression with
1458    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
1459    commutative arithmetic operation.  Store the constant part into *CONP,
1460    the literal in *LITP and return the variable part.  If a part isn't
1461    present, set it to null.  If the tree does not decompose in this way,
1462    return the entire tree as the variable part and the other parts as null.
1463
1464    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
1465    case, we negate an operand that was subtracted.  Except if it is a
1466    literal for which we use *MINUS_LITP instead.
1467
1468    If NEGATE_P is true, we are negating all of IN, again except a literal
1469    for which we use *MINUS_LITP instead.
1470
1471    If IN is itself a literal or constant, return it as appropriate.
1472
1473    Note that we do not guarantee that any of the three values will be the
1474    same type as IN, but they will have the same signedness and mode.  */
1475
1476 static tree
1477 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1478             tree *minus_litp, int negate_p)
1479 {
1480   tree var = 0;
1481
1482   *conp = 0;
1483   *litp = 0;
1484   *minus_litp = 0;
1485
1486   /* Strip any conversions that don't change the machine mode or signedness.  */
1487   STRIP_SIGN_NOPS (in);
1488
1489   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1490       || TREE_CODE (in) == FIXED_CST)
1491     *litp = in;
1492   else if (TREE_CODE (in) == code
1493            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
1494                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
1495                /* We can associate addition and subtraction together (even
1496                   though the C standard doesn't say so) for integers because
1497                   the value is not affected.  For reals, the value might be
1498                   affected, so we can't.  */
1499                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1500                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1501     {
1502       tree op0 = TREE_OPERAND (in, 0);
1503       tree op1 = TREE_OPERAND (in, 1);
1504       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1505       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1506
1507       /* First see if either of the operands is a literal, then a constant.  */
1508       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1509           || TREE_CODE (op0) == FIXED_CST)
1510         *litp = op0, op0 = 0;
1511       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1512                || TREE_CODE (op1) == FIXED_CST)
1513         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1514
1515       if (op0 != 0 && TREE_CONSTANT (op0))
1516         *conp = op0, op0 = 0;
1517       else if (op1 != 0 && TREE_CONSTANT (op1))
1518         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1519
1520       /* If we haven't dealt with either operand, this is not a case we can
1521          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
1522       if (op0 != 0 && op1 != 0)
1523         var = in;
1524       else if (op0 != 0)
1525         var = op0;
1526       else
1527         var = op1, neg_var_p = neg1_p;
1528
1529       /* Now do any needed negations.  */
1530       if (neg_litp_p)
1531         *minus_litp = *litp, *litp = 0;
1532       if (neg_conp_p)
1533         *conp = negate_expr (*conp);
1534       if (neg_var_p)
1535         var = negate_expr (var);
1536     }
1537   else if (TREE_CONSTANT (in))
1538     *conp = in;
1539   else
1540     var = in;
1541
1542   if (negate_p)
1543     {
1544       if (*litp)
1545         *minus_litp = *litp, *litp = 0;
1546       else if (*minus_litp)
1547         *litp = *minus_litp, *minus_litp = 0;
1548       *conp = negate_expr (*conp);
1549       var = negate_expr (var);
1550     }
1551
1552   return var;
1553 }
1554
1555 /* Re-associate trees split by the above function.  T1 and T2 are either
1556    expressions to associate or null.  Return the new expression, if any.  If
1557    we build an operation, do it in TYPE and with CODE.  */
1558
1559 static tree
1560 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1561 {
1562   if (t1 == 0)
1563     return t2;
1564   else if (t2 == 0)
1565     return t1;
1566
1567   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1568      try to fold this since we will have infinite recursion.  But do
1569      deal with any NEGATE_EXPRs.  */
1570   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1571       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1572     {
1573       if (code == PLUS_EXPR)
1574         {
1575           if (TREE_CODE (t1) == NEGATE_EXPR)
1576             return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1577                            fold_convert (type, TREE_OPERAND (t1, 0)));
1578           else if (TREE_CODE (t2) == NEGATE_EXPR)
1579             return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1580                            fold_convert (type, TREE_OPERAND (t2, 0)));
1581           else if (integer_zerop (t2))
1582             return fold_convert (type, t1);
1583         }
1584       else if (code == MINUS_EXPR)
1585         {
1586           if (integer_zerop (t2))
1587             return fold_convert (type, t1);
1588         }
1589
1590       return build2 (code, type, fold_convert (type, t1),
1591                      fold_convert (type, t2));
1592     }
1593
1594   return fold_build2 (code, type, fold_convert (type, t1),
1595                       fold_convert (type, t2));
1596 }
1597 \f
1598 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1599    for use in int_const_binop, size_binop and size_diffop.  */
1600
1601 static bool
1602 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1603 {
1604   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1605     return false;
1606   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1607     return false;
1608
1609   switch (code)
1610     {
1611     case LSHIFT_EXPR:
1612     case RSHIFT_EXPR:
1613     case LROTATE_EXPR:
1614     case RROTATE_EXPR:
1615       return true;
1616
1617     default:
1618       break;
1619     }
1620
1621   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1622          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1623          && TYPE_MODE (type1) == TYPE_MODE (type2);
1624 }
1625
1626
1627 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1628    to produce a new constant.  Return NULL_TREE if we don't know how
1629    to evaluate CODE at compile-time.
1630
1631    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1632
1633 tree
1634 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
1635 {
1636   unsigned HOST_WIDE_INT int1l, int2l;
1637   HOST_WIDE_INT int1h, int2h;
1638   unsigned HOST_WIDE_INT low;
1639   HOST_WIDE_INT hi;
1640   unsigned HOST_WIDE_INT garbagel;
1641   HOST_WIDE_INT garbageh;
1642   tree t;
1643   tree type = TREE_TYPE (arg1);
1644   int uns = TYPE_UNSIGNED (type);
1645   int is_sizetype
1646     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1647   int overflow = 0;
1648
1649   int1l = TREE_INT_CST_LOW (arg1);
1650   int1h = TREE_INT_CST_HIGH (arg1);
1651   int2l = TREE_INT_CST_LOW (arg2);
1652   int2h = TREE_INT_CST_HIGH (arg2);
1653
1654   switch (code)
1655     {
1656     case BIT_IOR_EXPR:
1657       low = int1l | int2l, hi = int1h | int2h;
1658       break;
1659
1660     case BIT_XOR_EXPR:
1661       low = int1l ^ int2l, hi = int1h ^ int2h;
1662       break;
1663
1664     case BIT_AND_EXPR:
1665       low = int1l & int2l, hi = int1h & int2h;
1666       break;
1667
1668     case RSHIFT_EXPR:
1669       int2l = -int2l;
1670     case LSHIFT_EXPR:
1671       /* It's unclear from the C standard whether shifts can overflow.
1672          The following code ignores overflow; perhaps a C standard
1673          interpretation ruling is needed.  */
1674       lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1675                      &low, &hi, !uns);
1676       break;
1677
1678     case RROTATE_EXPR:
1679       int2l = - int2l;
1680     case LROTATE_EXPR:
1681       lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1682                       &low, &hi);
1683       break;
1684
1685     case PLUS_EXPR:
1686       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1687       break;
1688
1689     case MINUS_EXPR:
1690       neg_double (int2l, int2h, &low, &hi);
1691       add_double (int1l, int1h, low, hi, &low, &hi);
1692       overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1693       break;
1694
1695     case MULT_EXPR:
1696       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1697       break;
1698
1699     case TRUNC_DIV_EXPR:
1700     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1701     case EXACT_DIV_EXPR:
1702       /* This is a shortcut for a common special case.  */
1703       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1704           && !TREE_OVERFLOW (arg1)
1705           && !TREE_OVERFLOW (arg2)
1706           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1707         {
1708           if (code == CEIL_DIV_EXPR)
1709             int1l += int2l - 1;
1710
1711           low = int1l / int2l, hi = 0;
1712           break;
1713         }
1714
1715       /* ... fall through ...  */
1716
1717     case ROUND_DIV_EXPR:
1718       if (int2h == 0 && int2l == 0)
1719         return NULL_TREE;
1720       if (int2h == 0 && int2l == 1)
1721         {
1722           low = int1l, hi = int1h;
1723           break;
1724         }
1725       if (int1l == int2l && int1h == int2h
1726           && ! (int1l == 0 && int1h == 0))
1727         {
1728           low = 1, hi = 0;
1729           break;
1730         }
1731       overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1732                                        &low, &hi, &garbagel, &garbageh);
1733       break;
1734
1735     case TRUNC_MOD_EXPR:
1736     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1737       /* This is a shortcut for a common special case.  */
1738       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1739           && !TREE_OVERFLOW (arg1)
1740           && !TREE_OVERFLOW (arg2)
1741           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1742         {
1743           if (code == CEIL_MOD_EXPR)
1744             int1l += int2l - 1;
1745           low = int1l % int2l, hi = 0;
1746           break;
1747         }
1748
1749       /* ... fall through ...  */
1750
1751     case ROUND_MOD_EXPR:
1752       if (int2h == 0 && int2l == 0)
1753         return NULL_TREE;
1754       overflow = div_and_round_double (code, uns,
1755                                        int1l, int1h, int2l, int2h,
1756                                        &garbagel, &garbageh, &low, &hi);
1757       break;
1758
1759     case MIN_EXPR:
1760     case MAX_EXPR:
1761       if (uns)
1762         low = (((unsigned HOST_WIDE_INT) int1h
1763                 < (unsigned HOST_WIDE_INT) int2h)
1764                || (((unsigned HOST_WIDE_INT) int1h
1765                     == (unsigned HOST_WIDE_INT) int2h)
1766                    && int1l < int2l));
1767       else
1768         low = (int1h < int2h
1769                || (int1h == int2h && int1l < int2l));
1770
1771       if (low == (code == MIN_EXPR))
1772         low = int1l, hi = int1h;
1773       else
1774         low = int2l, hi = int2h;
1775       break;
1776
1777     default:
1778       return NULL_TREE;
1779     }
1780
1781   if (notrunc)
1782     {
1783       t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1784
1785       /* Propagate overflow flags ourselves.  */
1786       if (((!uns || is_sizetype) && overflow)
1787           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1788         {
1789           t = copy_node (t);
1790           TREE_OVERFLOW (t) = 1;
1791         }
1792     }
1793   else
1794     t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1795                                ((!uns || is_sizetype) && overflow)
1796                                | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1797
1798   return t;
1799 }
1800
1801 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1802    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1803    are the same kind of constant and the same machine mode.  Return zero if
1804    combining the constants is not allowed in the current operating mode.
1805
1806    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1807
1808 static tree
1809 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1810 {
1811   /* Sanity check for the recursive cases.  */
1812   if (!arg1 || !arg2)
1813     return NULL_TREE;
1814
1815   STRIP_NOPS (arg1);
1816   STRIP_NOPS (arg2);
1817
1818   if (TREE_CODE (arg1) == INTEGER_CST)
1819     return int_const_binop (code, arg1, arg2, notrunc);
1820
1821   if (TREE_CODE (arg1) == REAL_CST)
1822     {
1823       enum machine_mode mode;
1824       REAL_VALUE_TYPE d1;
1825       REAL_VALUE_TYPE d2;
1826       REAL_VALUE_TYPE value;
1827       REAL_VALUE_TYPE result;
1828       bool inexact;
1829       tree t, type;
1830
1831       /* The following codes are handled by real_arithmetic.  */
1832       switch (code)
1833         {
1834         case PLUS_EXPR:
1835         case MINUS_EXPR:
1836         case MULT_EXPR:
1837         case RDIV_EXPR:
1838         case MIN_EXPR:
1839         case MAX_EXPR:
1840           break;
1841
1842         default:
1843           return NULL_TREE;
1844         }
1845
1846       d1 = TREE_REAL_CST (arg1);
1847       d2 = TREE_REAL_CST (arg2);
1848
1849       type = TREE_TYPE (arg1);
1850       mode = TYPE_MODE (type);
1851
1852       /* Don't perform operation if we honor signaling NaNs and
1853          either operand is a NaN.  */
1854       if (HONOR_SNANS (mode)
1855           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1856         return NULL_TREE;
1857
1858       /* Don't perform operation if it would raise a division
1859          by zero exception.  */
1860       if (code == RDIV_EXPR
1861           && REAL_VALUES_EQUAL (d2, dconst0)
1862           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1863         return NULL_TREE;
1864
1865       /* If either operand is a NaN, just return it.  Otherwise, set up
1866          for floating-point trap; we return an overflow.  */
1867       if (REAL_VALUE_ISNAN (d1))
1868         return arg1;
1869       else if (REAL_VALUE_ISNAN (d2))
1870         return arg2;
1871
1872       inexact = real_arithmetic (&value, code, &d1, &d2);
1873       real_convert (&result, mode, &value);
1874
1875       /* Don't constant fold this floating point operation if
1876          the result has overflowed and flag_trapping_math.  */
1877       if (flag_trapping_math
1878           && MODE_HAS_INFINITIES (mode)
1879           && REAL_VALUE_ISINF (result)
1880           && !REAL_VALUE_ISINF (d1)
1881           && !REAL_VALUE_ISINF (d2))
1882         return NULL_TREE;
1883
1884       /* Don't constant fold this floating point operation if the
1885          result may dependent upon the run-time rounding mode and
1886          flag_rounding_math is set, or if GCC's software emulation
1887          is unable to accurately represent the result.  */
1888       if ((flag_rounding_math
1889            || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1890           && (inexact || !real_identical (&result, &value)))
1891         return NULL_TREE;
1892
1893       t = build_real (type, result);
1894
1895       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1896       return t;
1897     }
1898
1899   if (TREE_CODE (arg1) == FIXED_CST)
1900     {
1901       FIXED_VALUE_TYPE f1;
1902       FIXED_VALUE_TYPE f2;
1903       FIXED_VALUE_TYPE result;
1904       tree t, type;
1905       int sat_p;
1906       bool overflow_p;
1907
1908       /* The following codes are handled by fixed_arithmetic.  */
1909       switch (code)
1910         {
1911         case PLUS_EXPR:
1912         case MINUS_EXPR:
1913         case MULT_EXPR:
1914         case TRUNC_DIV_EXPR:
1915           f2 = TREE_FIXED_CST (arg2);
1916           break;
1917
1918         case LSHIFT_EXPR:
1919         case RSHIFT_EXPR:
1920           f2.data.high = TREE_INT_CST_HIGH (arg2);
1921           f2.data.low = TREE_INT_CST_LOW (arg2);
1922           f2.mode = SImode;
1923           break;
1924
1925         default:
1926           return NULL_TREE;
1927         }
1928
1929       f1 = TREE_FIXED_CST (arg1);
1930       type = TREE_TYPE (arg1);
1931       sat_p = TYPE_SATURATING (type);
1932       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1933       t = build_fixed (type, result);
1934       /* Propagate overflow flags.  */
1935       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1936         TREE_OVERFLOW (t) = 1;
1937       return t;
1938     }
1939
1940   if (TREE_CODE (arg1) == COMPLEX_CST)
1941     {
1942       tree type = TREE_TYPE (arg1);
1943       tree r1 = TREE_REALPART (arg1);
1944       tree i1 = TREE_IMAGPART (arg1);
1945       tree r2 = TREE_REALPART (arg2);
1946       tree i2 = TREE_IMAGPART (arg2);
1947       tree real, imag;
1948
1949       switch (code)
1950         {
1951         case PLUS_EXPR:
1952         case MINUS_EXPR:
1953           real = const_binop (code, r1, r2, notrunc);
1954           imag = const_binop (code, i1, i2, notrunc);
1955           break;
1956
1957         case MULT_EXPR:
1958           real = const_binop (MINUS_EXPR,
1959                               const_binop (MULT_EXPR, r1, r2, notrunc),
1960                               const_binop (MULT_EXPR, i1, i2, notrunc),
1961                               notrunc);
1962           imag = const_binop (PLUS_EXPR,
1963                               const_binop (MULT_EXPR, r1, i2, notrunc),
1964                               const_binop (MULT_EXPR, i1, r2, notrunc),
1965                               notrunc);
1966           break;
1967
1968         case RDIV_EXPR:
1969           {
1970             tree magsquared
1971               = const_binop (PLUS_EXPR,
1972                              const_binop (MULT_EXPR, r2, r2, notrunc),
1973                              const_binop (MULT_EXPR, i2, i2, notrunc),
1974                              notrunc);
1975             tree t1
1976               = const_binop (PLUS_EXPR,
1977                              const_binop (MULT_EXPR, r1, r2, notrunc),
1978                              const_binop (MULT_EXPR, i1, i2, notrunc),
1979                              notrunc);
1980             tree t2
1981               = const_binop (MINUS_EXPR,
1982                              const_binop (MULT_EXPR, i1, r2, notrunc),
1983                              const_binop (MULT_EXPR, r1, i2, notrunc),
1984                              notrunc);
1985
1986             if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1987               code = TRUNC_DIV_EXPR;
1988
1989             real = const_binop (code, t1, magsquared, notrunc);
1990             imag = const_binop (code, t2, magsquared, notrunc);
1991           }
1992           break;
1993
1994         default:
1995           return NULL_TREE;
1996         }
1997
1998       if (real && imag)
1999         return build_complex (type, real, imag);
2000     }
2001
2002   if (TREE_CODE (arg1) == VECTOR_CST)
2003     {
2004       tree type = TREE_TYPE(arg1);
2005       int count = TYPE_VECTOR_SUBPARTS (type), i;
2006       tree elements1, elements2, list = NULL_TREE;
2007       
2008       if(TREE_CODE(arg2) != VECTOR_CST)
2009         return NULL_TREE;
2010         
2011       elements1 = TREE_VECTOR_CST_ELTS (arg1);
2012       elements2 = TREE_VECTOR_CST_ELTS (arg2);
2013
2014       for (i = 0; i < count; i++)
2015         {
2016           tree elem1, elem2, elem;
2017           
2018           /* The trailing elements can be empty and should be treated as 0 */
2019           if(!elements1)
2020             elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2021           else
2022             {
2023               elem1 = TREE_VALUE(elements1);
2024               elements1 = TREE_CHAIN (elements1);
2025             }  
2026             
2027           if(!elements2)
2028             elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2029           else
2030             {
2031               elem2 = TREE_VALUE(elements2);
2032               elements2 = TREE_CHAIN (elements2);
2033             }
2034               
2035           elem = const_binop (code, elem1, elem2, notrunc);
2036           
2037           /* It is possible that const_binop cannot handle the given
2038             code and return NULL_TREE */
2039           if(elem == NULL_TREE)
2040             return NULL_TREE;
2041           
2042           list = tree_cons (NULL_TREE, elem, list);
2043         }
2044       return build_vector(type, nreverse(list));  
2045     }
2046   return NULL_TREE;
2047 }
2048
2049 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
2050    indicates which particular sizetype to create.  */
2051
2052 tree
2053 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2054 {
2055   return build_int_cst (sizetype_tab[(int) kind], number);
2056 }
2057 \f
2058 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
2059    is a tree code.  The type of the result is taken from the operands.
2060    Both must be equivalent integer types, ala int_binop_types_match_p.
2061    If the operands are constant, so is the result.  */
2062
2063 tree
2064 size_binop (enum tree_code code, tree arg0, tree arg1)
2065 {
2066   tree type = TREE_TYPE (arg0);
2067
2068   if (arg0 == error_mark_node || arg1 == error_mark_node)
2069     return error_mark_node;
2070
2071   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2072                                        TREE_TYPE (arg1)));
2073
2074   /* Handle the special case of two integer constants faster.  */
2075   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2076     {
2077       /* And some specific cases even faster than that.  */
2078       if (code == PLUS_EXPR)
2079         {
2080           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2081             return arg1;
2082           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2083             return arg0;
2084         }
2085       else if (code == MINUS_EXPR)
2086         {
2087           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2088             return arg0;
2089         }
2090       else if (code == MULT_EXPR)
2091         {
2092           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2093             return arg1;
2094         }
2095
2096       /* Handle general case of two integer constants.  */
2097       return int_const_binop (code, arg0, arg1, 0);
2098     }
2099
2100   return fold_build2 (code, type, arg0, arg1);
2101 }
2102
2103 /* Given two values, either both of sizetype or both of bitsizetype,
2104    compute the difference between the two values.  Return the value
2105    in signed type corresponding to the type of the operands.  */
2106
2107 tree
2108 size_diffop (tree arg0, tree arg1)
2109 {
2110   tree type = TREE_TYPE (arg0);
2111   tree ctype;
2112
2113   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2114                                        TREE_TYPE (arg1)));
2115
2116   /* If the type is already signed, just do the simple thing.  */
2117   if (!TYPE_UNSIGNED (type))
2118     return size_binop (MINUS_EXPR, arg0, arg1);
2119
2120   if (type == sizetype)
2121     ctype = ssizetype;
2122   else if (type == bitsizetype)
2123     ctype = sbitsizetype;
2124   else
2125     ctype = signed_type_for (type);
2126
2127   /* If either operand is not a constant, do the conversions to the signed
2128      type and subtract.  The hardware will do the right thing with any
2129      overflow in the subtraction.  */
2130   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2131     return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
2132                        fold_convert (ctype, arg1));
2133
2134   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2135      Otherwise, subtract the other way, convert to CTYPE (we know that can't
2136      overflow) and negate (which can't either).  Special-case a result
2137      of zero while we're here.  */
2138   if (tree_int_cst_equal (arg0, arg1))
2139     return build_int_cst (ctype, 0);
2140   else if (tree_int_cst_lt (arg1, arg0))
2141     return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
2142   else
2143     return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
2144                        fold_convert (ctype, size_binop (MINUS_EXPR,
2145                                                         arg1, arg0)));
2146 }
2147 \f
2148 /* A subroutine of fold_convert_const handling conversions of an
2149    INTEGER_CST to another integer type.  */
2150
2151 static tree
2152 fold_convert_const_int_from_int (tree type, const_tree arg1)
2153 {
2154   tree t;
2155
2156   /* Given an integer constant, make new constant with new type,
2157      appropriately sign-extended or truncated.  */
2158   t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2159                              TREE_INT_CST_HIGH (arg1),
2160                              /* Don't set the overflow when
2161                                 converting from a pointer,  */
2162                              !POINTER_TYPE_P (TREE_TYPE (arg1))
2163                              /* or to a sizetype with same signedness
2164                                 and the precision is unchanged.
2165                                 ???  sizetype is always sign-extended,
2166                                 but its signedness depends on the
2167                                 frontend.  Thus we see spurious overflows
2168                                 here if we do not check this.  */
2169                              && !((TYPE_PRECISION (TREE_TYPE (arg1))
2170                                    == TYPE_PRECISION (type))
2171                                   && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2172                                       == TYPE_UNSIGNED (type))
2173                                   && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2174                                        && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2175                                       || (TREE_CODE (type) == INTEGER_TYPE
2176                                           && TYPE_IS_SIZETYPE (type)))),
2177                              (TREE_INT_CST_HIGH (arg1) < 0
2178                               && (TYPE_UNSIGNED (type)
2179                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2180                              | TREE_OVERFLOW (arg1));
2181
2182   return t;
2183 }
2184
2185 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2186    to an integer type.  */
2187
2188 static tree
2189 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2190 {
2191   int overflow = 0;
2192   tree t;
2193
2194   /* The following code implements the floating point to integer
2195      conversion rules required by the Java Language Specification,
2196      that IEEE NaNs are mapped to zero and values that overflow
2197      the target precision saturate, i.e. values greater than
2198      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2199      are mapped to INT_MIN.  These semantics are allowed by the
2200      C and C++ standards that simply state that the behavior of
2201      FP-to-integer conversion is unspecified upon overflow.  */
2202
2203   HOST_WIDE_INT high, low;
2204   REAL_VALUE_TYPE r;
2205   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2206
2207   switch (code)
2208     {
2209     case FIX_TRUNC_EXPR:
2210       real_trunc (&r, VOIDmode, &x);
2211       break;
2212
2213     default:
2214       gcc_unreachable ();
2215     }
2216
2217   /* If R is NaN, return zero and show we have an overflow.  */
2218   if (REAL_VALUE_ISNAN (r))
2219     {
2220       overflow = 1;
2221       high = 0;
2222       low = 0;
2223     }
2224
2225   /* See if R is less than the lower bound or greater than the
2226      upper bound.  */
2227
2228   if (! overflow)
2229     {
2230       tree lt = TYPE_MIN_VALUE (type);
2231       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2232       if (REAL_VALUES_LESS (r, l))
2233         {
2234           overflow = 1;
2235           high = TREE_INT_CST_HIGH (lt);
2236           low = TREE_INT_CST_LOW (lt);
2237         }
2238     }
2239
2240   if (! overflow)
2241     {
2242       tree ut = TYPE_MAX_VALUE (type);
2243       if (ut)
2244         {
2245           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2246           if (REAL_VALUES_LESS (u, r))
2247             {
2248               overflow = 1;
2249               high = TREE_INT_CST_HIGH (ut);
2250               low = TREE_INT_CST_LOW (ut);
2251             }
2252         }
2253     }
2254
2255   if (! overflow)
2256     REAL_VALUE_TO_INT (&low, &high, r);
2257
2258   t = force_fit_type_double (type, low, high, -1,
2259                              overflow | TREE_OVERFLOW (arg1));
2260   return t;
2261 }
2262
2263 /* A subroutine of fold_convert_const handling conversions of a
2264    FIXED_CST to an integer type.  */
2265
2266 static tree
2267 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2268 {
2269   tree t;
2270   double_int temp, temp_trunc;
2271   unsigned int mode;
2272
2273   /* Right shift FIXED_CST to temp by fbit.  */
2274   temp = TREE_FIXED_CST (arg1).data;
2275   mode = TREE_FIXED_CST (arg1).mode;
2276   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2277     {
2278       lshift_double (temp.low, temp.high,
2279                      - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2280                      &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2281
2282       /* Left shift temp to temp_trunc by fbit.  */
2283       lshift_double (temp.low, temp.high,
2284                      GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2285                      &temp_trunc.low, &temp_trunc.high,
2286                      SIGNED_FIXED_POINT_MODE_P (mode));
2287     }
2288   else
2289     {
2290       temp.low = 0;
2291       temp.high = 0;
2292       temp_trunc.low = 0;
2293       temp_trunc.high = 0;
2294     }
2295
2296   /* If FIXED_CST is negative, we need to round the value toward 0.
2297      By checking if the fractional bits are not zero to add 1 to temp.  */
2298   if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2299       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2300     {
2301       double_int one;
2302       one.low = 1;
2303       one.high = 0;
2304       temp = double_int_add (temp, one);
2305     }
2306
2307   /* Given a fixed-point constant, make new constant with new type,
2308      appropriately sign-extended or truncated.  */
2309   t = force_fit_type_double (type, temp.low, temp.high, -1,
2310                              (temp.high < 0
2311                               && (TYPE_UNSIGNED (type)
2312                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2313                              | TREE_OVERFLOW (arg1));
2314
2315   return t;
2316 }
2317
2318 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2319    to another floating point type.  */
2320
2321 static tree
2322 fold_convert_const_real_from_real (tree type, const_tree arg1)
2323 {
2324   REAL_VALUE_TYPE value;
2325   tree t;
2326
2327   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2328   t = build_real (type, value);
2329
2330   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2331   return t;
2332 }
2333
2334 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2335    to a floating point type.  */
2336
2337 static tree
2338 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2339 {
2340   REAL_VALUE_TYPE value;
2341   tree t;
2342
2343   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2344   t = build_real (type, value);
2345
2346   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2347   return t;
2348 }
2349
2350 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2351    to another fixed-point type.  */
2352
2353 static tree
2354 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2355 {
2356   FIXED_VALUE_TYPE value;
2357   tree t;
2358   bool overflow_p;
2359
2360   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2361                               TYPE_SATURATING (type));
2362   t = build_fixed (type, value);
2363
2364   /* Propagate overflow flags.  */
2365   if (overflow_p | TREE_OVERFLOW (arg1))
2366     TREE_OVERFLOW (t) = 1;
2367   return t;
2368 }
2369
2370 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2371    to a fixed-point type.  */
2372
2373 static tree
2374 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2375 {
2376   FIXED_VALUE_TYPE value;
2377   tree t;
2378   bool overflow_p;
2379
2380   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2381                                        TREE_INT_CST (arg1),
2382                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
2383                                        TYPE_SATURATING (type));
2384   t = build_fixed (type, value);
2385
2386   /* Propagate overflow flags.  */
2387   if (overflow_p | TREE_OVERFLOW (arg1))
2388     TREE_OVERFLOW (t) = 1;
2389   return t;
2390 }
2391
2392 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2393    to a fixed-point type.  */
2394
2395 static tree
2396 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2397 {
2398   FIXED_VALUE_TYPE value;
2399   tree t;
2400   bool overflow_p;
2401
2402   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2403                                         &TREE_REAL_CST (arg1),
2404                                         TYPE_SATURATING (type));
2405   t = build_fixed (type, value);
2406
2407   /* Propagate overflow flags.  */
2408   if (overflow_p | TREE_OVERFLOW (arg1))
2409     TREE_OVERFLOW (t) = 1;
2410   return t;
2411 }
2412
2413 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2414    type TYPE.  If no simplification can be done return NULL_TREE.  */
2415
2416 static tree
2417 fold_convert_const (enum tree_code code, tree type, tree arg1)
2418 {
2419   if (TREE_TYPE (arg1) == type)
2420     return arg1;
2421
2422   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2423       || TREE_CODE (type) == OFFSET_TYPE)
2424     {
2425       if (TREE_CODE (arg1) == INTEGER_CST)
2426         return fold_convert_const_int_from_int (type, arg1);
2427       else if (TREE_CODE (arg1) == REAL_CST)
2428         return fold_convert_const_int_from_real (code, type, arg1);
2429       else if (TREE_CODE (arg1) == FIXED_CST)
2430         return fold_convert_const_int_from_fixed (type, arg1);
2431     }
2432   else if (TREE_CODE (type) == REAL_TYPE)
2433     {
2434       if (TREE_CODE (arg1) == INTEGER_CST)
2435         return build_real_from_int_cst (type, arg1);
2436       else if (TREE_CODE (arg1) == REAL_CST)
2437         return fold_convert_const_real_from_real (type, arg1);
2438       else if (TREE_CODE (arg1) == FIXED_CST)
2439         return fold_convert_const_real_from_fixed (type, arg1);
2440     }
2441   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2442     {
2443       if (TREE_CODE (arg1) == FIXED_CST)
2444         return fold_convert_const_fixed_from_fixed (type, arg1);
2445       else if (TREE_CODE (arg1) == INTEGER_CST)
2446         return fold_convert_const_fixed_from_int (type, arg1);
2447       else if (TREE_CODE (arg1) == REAL_CST)
2448         return fold_convert_const_fixed_from_real (type, arg1);
2449     }
2450   return NULL_TREE;
2451 }
2452
2453 /* Construct a vector of zero elements of vector type TYPE.  */
2454
2455 static tree
2456 build_zero_vector (tree type)
2457 {
2458   tree elem, list;
2459   int i, units;
2460
2461   elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2462   units = TYPE_VECTOR_SUBPARTS (type);
2463   
2464   list = NULL_TREE;
2465   for (i = 0; i < units; i++)
2466     list = tree_cons (NULL_TREE, elem, list);
2467   return build_vector (type, list);
2468 }
2469
2470 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
2471
2472 bool
2473 fold_convertible_p (const_tree type, const_tree arg)
2474 {
2475   tree orig = TREE_TYPE (arg);
2476
2477   if (type == orig)
2478     return true;
2479
2480   if (TREE_CODE (arg) == ERROR_MARK
2481       || TREE_CODE (type) == ERROR_MARK
2482       || TREE_CODE (orig) == ERROR_MARK)
2483     return false;
2484
2485   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2486     return true;
2487
2488   switch (TREE_CODE (type))
2489     {
2490     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2491     case POINTER_TYPE: case REFERENCE_TYPE:
2492     case OFFSET_TYPE:
2493       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2494           || TREE_CODE (orig) == OFFSET_TYPE)
2495         return true;
2496       return (TREE_CODE (orig) == VECTOR_TYPE
2497               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2498
2499     case REAL_TYPE:
2500     case FIXED_POINT_TYPE:
2501     case COMPLEX_TYPE:
2502     case VECTOR_TYPE:
2503     case VOID_TYPE:
2504       return TREE_CODE (type) == TREE_CODE (orig);
2505
2506     default:
2507       return false;
2508     }
2509 }
2510
2511 /* Convert expression ARG to type TYPE.  Used by the middle-end for
2512    simple conversions in preference to calling the front-end's convert.  */
2513
2514 tree
2515 fold_convert (tree type, tree arg)
2516 {
2517   tree orig = TREE_TYPE (arg);
2518   tree tem;
2519
2520   if (type == orig)
2521     return arg;
2522
2523   if (TREE_CODE (arg) == ERROR_MARK
2524       || TREE_CODE (type) == ERROR_MARK
2525       || TREE_CODE (orig) == ERROR_MARK)
2526     return error_mark_node;
2527
2528   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2529     return fold_build1 (NOP_EXPR, type, arg);
2530
2531   switch (TREE_CODE (type))
2532     {
2533     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2534     case POINTER_TYPE: case REFERENCE_TYPE:
2535     case OFFSET_TYPE:
2536       if (TREE_CODE (arg) == INTEGER_CST)
2537         {
2538           tem = fold_convert_const (NOP_EXPR, type, arg);
2539           if (tem != NULL_TREE)
2540             return tem;
2541         }
2542       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2543           || TREE_CODE (orig) == OFFSET_TYPE)
2544         return fold_build1 (NOP_EXPR, type, arg);
2545       if (TREE_CODE (orig) == COMPLEX_TYPE)
2546         {
2547           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2548           return fold_convert (type, tem);
2549         }
2550       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2551                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2552       return fold_build1 (NOP_EXPR, type, arg);
2553
2554     case REAL_TYPE:
2555       if (TREE_CODE (arg) == INTEGER_CST)
2556         {
2557           tem = fold_convert_const (FLOAT_EXPR, type, arg);
2558           if (tem != NULL_TREE)
2559             return tem;
2560         }
2561       else if (TREE_CODE (arg) == REAL_CST)
2562         {
2563           tem = fold_convert_const (NOP_EXPR, type, arg);
2564           if (tem != NULL_TREE)
2565             return tem;
2566         }
2567       else if (TREE_CODE (arg) == FIXED_CST)
2568         {
2569           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2570           if (tem != NULL_TREE)
2571             return tem;
2572         }
2573
2574       switch (TREE_CODE (orig))
2575         {
2576         case INTEGER_TYPE:
2577         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2578         case POINTER_TYPE: case REFERENCE_TYPE:
2579           return fold_build1 (FLOAT_EXPR, type, arg);
2580
2581         case REAL_TYPE:
2582           return fold_build1 (NOP_EXPR, type, arg);
2583
2584         case FIXED_POINT_TYPE:
2585           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2586
2587         case COMPLEX_TYPE:
2588           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2589           return fold_convert (type, tem);
2590
2591         default:
2592           gcc_unreachable ();
2593         }
2594
2595     case FIXED_POINT_TYPE:
2596       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2597           || TREE_CODE (arg) == REAL_CST)
2598         {
2599           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2600           if (tem != NULL_TREE)
2601             return tem;
2602         }
2603
2604       switch (TREE_CODE (orig))
2605         {
2606         case FIXED_POINT_TYPE:
2607         case INTEGER_TYPE:
2608         case ENUMERAL_TYPE:
2609         case BOOLEAN_TYPE:
2610         case REAL_TYPE:
2611           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2612
2613         case COMPLEX_TYPE:
2614           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2615           return fold_convert (type, tem);
2616
2617         default:
2618           gcc_unreachable ();
2619         }
2620
2621     case COMPLEX_TYPE:
2622       switch (TREE_CODE (orig))
2623         {
2624         case INTEGER_TYPE:
2625         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2626         case POINTER_TYPE: case REFERENCE_TYPE:
2627         case REAL_TYPE:
2628         case FIXED_POINT_TYPE:
2629           return build2 (COMPLEX_EXPR, type,
2630                          fold_convert (TREE_TYPE (type), arg),
2631                          fold_convert (TREE_TYPE (type), integer_zero_node));
2632         case COMPLEX_TYPE:
2633           {
2634             tree rpart, ipart;
2635
2636             if (TREE_CODE (arg) == COMPLEX_EXPR)
2637               {
2638                 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2639                 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2640                 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2641               }
2642
2643             arg = save_expr (arg);
2644             rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2645             ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2646             rpart = fold_convert (TREE_TYPE (type), rpart);
2647             ipart = fold_convert (TREE_TYPE (type), ipart);
2648             return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2649           }
2650
2651         default:
2652           gcc_unreachable ();
2653         }
2654
2655     case VECTOR_TYPE:
2656       if (integer_zerop (arg))
2657         return build_zero_vector (type);
2658       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2659       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2660                   || TREE_CODE (orig) == VECTOR_TYPE);
2661       return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
2662
2663     case VOID_TYPE:
2664       tem = fold_ignored_result (arg);
2665       if (TREE_CODE (tem) == MODIFY_EXPR)
2666         return tem;
2667       return fold_build1 (NOP_EXPR, type, tem);
2668
2669     default:
2670       gcc_unreachable ();
2671     }
2672 }
2673 \f
2674 /* Return false if expr can be assumed not to be an lvalue, true
2675    otherwise.  */
2676
2677 static bool
2678 maybe_lvalue_p (const_tree x)
2679 {
2680   /* We only need to wrap lvalue tree codes.  */
2681   switch (TREE_CODE (x))
2682   {
2683   case VAR_DECL:
2684   case PARM_DECL:
2685   case RESULT_DECL:
2686   case LABEL_DECL:
2687   case FUNCTION_DECL:
2688   case SSA_NAME:
2689
2690   case COMPONENT_REF:
2691   case INDIRECT_REF:
2692   case ALIGN_INDIRECT_REF:
2693   case MISALIGNED_INDIRECT_REF:
2694   case ARRAY_REF:
2695   case ARRAY_RANGE_REF:
2696   case BIT_FIELD_REF:
2697   case OBJ_TYPE_REF:
2698
2699   case REALPART_EXPR:
2700   case IMAGPART_EXPR:
2701   case PREINCREMENT_EXPR:
2702   case PREDECREMENT_EXPR:
2703   case SAVE_EXPR:
2704   case TRY_CATCH_EXPR:
2705   case WITH_CLEANUP_EXPR:
2706   case COMPOUND_EXPR:
2707   case MODIFY_EXPR:
2708   case TARGET_EXPR:
2709   case COND_EXPR:
2710   case BIND_EXPR:
2711   case MIN_EXPR:
2712   case MAX_EXPR:
2713     break;
2714
2715   default:
2716     /* Assume the worst for front-end tree codes.  */
2717     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2718       break;
2719     return false;
2720   }
2721
2722   return true;
2723 }
2724
2725 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2726
2727 tree
2728 non_lvalue (tree x)
2729 {
2730   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2731      us.  */
2732   if (in_gimple_form)
2733     return x;
2734
2735   if (! maybe_lvalue_p (x))
2736     return x;
2737   return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2738 }
2739
2740 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2741    Zero means allow extended lvalues.  */
2742
2743 int pedantic_lvalues;
2744
2745 /* When pedantic, return an expr equal to X but certainly not valid as a
2746    pedantic lvalue.  Otherwise, return X.  */
2747
2748 static tree
2749 pedantic_non_lvalue (tree x)
2750 {
2751   if (pedantic_lvalues)
2752     return non_lvalue (x);
2753   else
2754     return x;
2755 }
2756 \f
2757 /* Given a tree comparison code, return the code that is the logical inverse
2758    of the given code.  It is not safe to do this for floating-point
2759    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2760    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2761
2762 enum tree_code
2763 invert_tree_comparison (enum tree_code code, bool honor_nans)
2764 {
2765   if (honor_nans && flag_trapping_math)
2766     return ERROR_MARK;
2767
2768   switch (code)
2769     {
2770     case EQ_EXPR:
2771       return NE_EXPR;
2772     case NE_EXPR:
2773       return EQ_EXPR;
2774     case GT_EXPR:
2775       return honor_nans ? UNLE_EXPR : LE_EXPR;
2776     case GE_EXPR:
2777       return honor_nans ? UNLT_EXPR : LT_EXPR;
2778     case LT_EXPR:
2779       return honor_nans ? UNGE_EXPR : GE_EXPR;
2780     case LE_EXPR:
2781       return honor_nans ? UNGT_EXPR : GT_EXPR;
2782     case LTGT_EXPR:
2783       return UNEQ_EXPR;
2784     case UNEQ_EXPR:
2785       return LTGT_EXPR;
2786     case UNGT_EXPR:
2787       return LE_EXPR;
2788     case UNGE_EXPR:
2789       return LT_EXPR;
2790     case UNLT_EXPR:
2791       return GE_EXPR;
2792     case UNLE_EXPR:
2793       return GT_EXPR;
2794     case ORDERED_EXPR:
2795       return UNORDERED_EXPR;
2796     case UNORDERED_EXPR:
2797       return ORDERED_EXPR;
2798     default:
2799       gcc_unreachable ();
2800     }
2801 }
2802
2803 /* Similar, but return the comparison that results if the operands are
2804    swapped.  This is safe for floating-point.  */
2805
2806 enum tree_code
2807 swap_tree_comparison (enum tree_code code)
2808 {
2809   switch (code)
2810     {
2811     case EQ_EXPR:
2812     case NE_EXPR:
2813     case ORDERED_EXPR:
2814     case UNORDERED_EXPR:
2815     case LTGT_EXPR:
2816     case UNEQ_EXPR:
2817       return code;
2818     case GT_EXPR:
2819       return LT_EXPR;
2820     case GE_EXPR:
2821       return LE_EXPR;
2822     case LT_EXPR:
2823       return GT_EXPR;
2824     case LE_EXPR:
2825       return GE_EXPR;
2826     case UNGT_EXPR:
2827       return UNLT_EXPR;
2828     case UNGE_EXPR:
2829       return UNLE_EXPR;
2830     case UNLT_EXPR:
2831       return UNGT_EXPR;
2832     case UNLE_EXPR:
2833       return UNGE_EXPR;
2834     default:
2835       gcc_unreachable ();
2836     }
2837 }
2838
2839
2840 /* Convert a comparison tree code from an enum tree_code representation
2841    into a compcode bit-based encoding.  This function is the inverse of
2842    compcode_to_comparison.  */
2843
2844 static enum comparison_code
2845 comparison_to_compcode (enum tree_code code)
2846 {
2847   switch (code)
2848     {
2849     case LT_EXPR:
2850       return COMPCODE_LT;
2851     case EQ_EXPR:
2852       return COMPCODE_EQ;
2853     case LE_EXPR:
2854       return COMPCODE_LE;
2855     case GT_EXPR:
2856       return COMPCODE_GT;
2857     case NE_EXPR:
2858       return COMPCODE_NE;
2859     case GE_EXPR:
2860       return COMPCODE_GE;
2861     case ORDERED_EXPR:
2862       return COMPCODE_ORD;
2863     case UNORDERED_EXPR:
2864       return COMPCODE_UNORD;
2865     case UNLT_EXPR:
2866       return COMPCODE_UNLT;
2867     case UNEQ_EXPR:
2868       return COMPCODE_UNEQ;
2869     case UNLE_EXPR:
2870       return COMPCODE_UNLE;
2871     case UNGT_EXPR:
2872       return COMPCODE_UNGT;
2873     case LTGT_EXPR:
2874       return COMPCODE_LTGT;
2875     case UNGE_EXPR:
2876       return COMPCODE_UNGE;
2877     default:
2878       gcc_unreachable ();
2879     }
2880 }
2881
2882 /* Convert a compcode bit-based encoding of a comparison operator back
2883    to GCC's enum tree_code representation.  This function is the
2884    inverse of comparison_to_compcode.  */
2885
2886 static enum tree_code
2887 compcode_to_comparison (enum comparison_code code)
2888 {
2889   switch (code)
2890     {
2891     case COMPCODE_LT:
2892       return LT_EXPR;
2893     case COMPCODE_EQ:
2894       return EQ_EXPR;
2895     case COMPCODE_LE:
2896       return LE_EXPR;
2897     case COMPCODE_GT:
2898       return GT_EXPR;
2899     case COMPCODE_NE:
2900       return NE_EXPR;
2901     case COMPCODE_GE:
2902       return GE_EXPR;
2903     case COMPCODE_ORD:
2904       return ORDERED_EXPR;
2905     case COMPCODE_UNORD:
2906       return UNORDERED_EXPR;
2907     case COMPCODE_UNLT:
2908       return UNLT_EXPR;
2909     case COMPCODE_UNEQ:
2910       return UNEQ_EXPR;
2911     case COMPCODE_UNLE:
2912       return UNLE_EXPR;
2913     case COMPCODE_UNGT:
2914       return UNGT_EXPR;
2915     case COMPCODE_LTGT:
2916       return LTGT_EXPR;
2917     case COMPCODE_UNGE:
2918       return UNGE_EXPR;
2919     default:
2920       gcc_unreachable ();
2921     }
2922 }
2923
2924 /* Return a tree for the comparison which is the combination of
2925    doing the AND or OR (depending on CODE) of the two operations LCODE
2926    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2927    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2928    if this makes the transformation invalid.  */
2929
2930 tree
2931 combine_comparisons (enum tree_code code, enum tree_code lcode,
2932                      enum tree_code rcode, tree truth_type,
2933                      tree ll_arg, tree lr_arg)
2934 {
2935   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2936   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2937   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2938   int compcode;
2939
2940   switch (code)
2941     {
2942     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2943       compcode = lcompcode & rcompcode;
2944       break;
2945
2946     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2947       compcode = lcompcode | rcompcode;
2948       break;
2949
2950     default:
2951       return NULL_TREE;
2952     }
2953
2954   if (!honor_nans)
2955     {
2956       /* Eliminate unordered comparisons, as well as LTGT and ORD
2957          which are not used unless the mode has NaNs.  */
2958       compcode &= ~COMPCODE_UNORD;
2959       if (compcode == COMPCODE_LTGT)
2960         compcode = COMPCODE_NE;
2961       else if (compcode == COMPCODE_ORD)
2962         compcode = COMPCODE_TRUE;
2963     }
2964    else if (flag_trapping_math)
2965      {
2966         /* Check that the original operation and the optimized ones will trap
2967            under the same condition.  */
2968         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2969                      && (lcompcode != COMPCODE_EQ)
2970                      && (lcompcode != COMPCODE_ORD);
2971         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2972                      && (rcompcode != COMPCODE_EQ)
2973                      && (rcompcode != COMPCODE_ORD);
2974         bool trap = (compcode & COMPCODE_UNORD) == 0
2975                     && (compcode != COMPCODE_EQ)
2976                     && (compcode != COMPCODE_ORD);
2977
2978         /* In a short-circuited boolean expression the LHS might be
2979            such that the RHS, if evaluated, will never trap.  For
2980            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2981            if neither x nor y is NaN.  (This is a mixed blessing: for
2982            example, the expression above will never trap, hence
2983            optimizing it to x < y would be invalid).  */
2984         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2985             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2986           rtrap = false;
2987
2988         /* If the comparison was short-circuited, and only the RHS
2989            trapped, we may now generate a spurious trap.  */
2990         if (rtrap && !ltrap
2991             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2992           return NULL_TREE;
2993
2994         /* If we changed the conditions that cause a trap, we lose.  */
2995         if ((ltrap || rtrap) != trap)
2996           return NULL_TREE;
2997       }
2998
2999   if (compcode == COMPCODE_TRUE)
3000     return constant_boolean_node (true, truth_type);
3001   else if (compcode == COMPCODE_FALSE)
3002     return constant_boolean_node (false, truth_type);
3003   else
3004     {
3005       enum tree_code tcode;
3006
3007       tcode = compcode_to_comparison ((enum comparison_code) compcode);
3008       return fold_build2 (tcode, truth_type, ll_arg, lr_arg);
3009     }
3010 }
3011 \f
3012 /* Return nonzero if two operands (typically of the same tree node)
3013    are necessarily equal.  If either argument has side-effects this
3014    function returns zero.  FLAGS modifies behavior as follows:
3015
3016    If OEP_ONLY_CONST is set, only return nonzero for constants.
3017    This function tests whether the operands are indistinguishable;
3018    it does not test whether they are equal using C's == operation.
3019    The distinction is important for IEEE floating point, because
3020    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3021    (2) two NaNs may be indistinguishable, but NaN!=NaN.
3022
3023    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3024    even though it may hold multiple values during a function.
3025    This is because a GCC tree node guarantees that nothing else is
3026    executed between the evaluation of its "operands" (which may often
3027    be evaluated in arbitrary order).  Hence if the operands themselves
3028    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3029    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
3030    unset means assuming isochronic (or instantaneous) tree equivalence.
3031    Unless comparing arbitrary expression trees, such as from different
3032    statements, this flag can usually be left unset.
3033
3034    If OEP_PURE_SAME is set, then pure functions with identical arguments
3035    are considered the same.  It is used when the caller has other ways
3036    to ensure that global memory is unchanged in between.  */
3037
3038 int
3039 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
3040 {
3041   /* If either is ERROR_MARK, they aren't equal.  */
3042   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
3043     return 0;
3044
3045   /* Check equality of integer constants before bailing out due to
3046      precision differences.  */
3047   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3048     return tree_int_cst_equal (arg0, arg1);
3049
3050   /* If both types don't have the same signedness, then we can't consider
3051      them equal.  We must check this before the STRIP_NOPS calls
3052      because they may change the signedness of the arguments.  As pointers
3053      strictly don't have a signedness, require either two pointers or
3054      two non-pointers as well.  */
3055   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3056       || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
3057     return 0;
3058
3059   /* If both types don't have the same precision, then it is not safe
3060      to strip NOPs.  */
3061   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3062     return 0;
3063
3064   STRIP_NOPS (arg0);
3065   STRIP_NOPS (arg1);
3066
3067   /* In case both args are comparisons but with different comparison
3068      code, try to swap the comparison operands of one arg to produce
3069      a match and compare that variant.  */
3070   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3071       && COMPARISON_CLASS_P (arg0)
3072       && COMPARISON_CLASS_P (arg1))
3073     {
3074       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3075
3076       if (TREE_CODE (arg0) == swap_code)
3077         return operand_equal_p (TREE_OPERAND (arg0, 0),
3078                                 TREE_OPERAND (arg1, 1), flags)
3079                && operand_equal_p (TREE_OPERAND (arg0, 1),
3080                                    TREE_OPERAND (arg1, 0), flags);
3081     }
3082
3083   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3084       /* This is needed for conversions and for COMPONENT_REF.
3085          Might as well play it safe and always test this.  */
3086       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3087       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3088       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3089     return 0;
3090
3091   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3092      We don't care about side effects in that case because the SAVE_EXPR
3093      takes care of that for us. In all other cases, two expressions are
3094      equal if they have no side effects.  If we have two identical
3095      expressions with side effects that should be treated the same due
3096      to the only side effects being identical SAVE_EXPR's, that will
3097      be detected in the recursive calls below.  */
3098   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3099       && (TREE_CODE (arg0) == SAVE_EXPR
3100           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3101     return 1;
3102
3103   /* Next handle constant cases, those for which we can return 1 even
3104      if ONLY_CONST is set.  */
3105   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3106     switch (TREE_CODE (arg0))
3107       {
3108       case INTEGER_CST:
3109         return tree_int_cst_equal (arg0, arg1);
3110
3111       case FIXED_CST:
3112         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3113                                        TREE_FIXED_CST (arg1));
3114
3115       case REAL_CST:
3116         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3117                                    TREE_REAL_CST (arg1)))
3118           return 1;
3119
3120         
3121         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3122           {
3123             /* If we do not distinguish between signed and unsigned zero,
3124                consider them equal.  */
3125             if (real_zerop (arg0) && real_zerop (arg1))
3126               return 1;
3127           }
3128         return 0;
3129
3130       case VECTOR_CST:
3131         {
3132           tree v1, v2;
3133
3134           v1 = TREE_VECTOR_CST_ELTS (arg0);
3135           v2 = TREE_VECTOR_CST_ELTS (arg1);
3136           while (v1 && v2)
3137             {
3138               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3139                                     flags))
3140                 return 0;
3141               v1 = TREE_CHAIN (v1);
3142               v2 = TREE_CHAIN (v2);
3143             }
3144
3145           return v1 == v2;
3146         }
3147
3148       case COMPLEX_CST:
3149         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3150                                  flags)
3151                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3152                                     flags));
3153
3154       case STRING_CST:
3155         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3156                 && ! memcmp (TREE_STRING_POINTER (arg0),
3157                               TREE_STRING_POINTER (arg1),
3158                               TREE_STRING_LENGTH (arg0)));
3159
3160       case ADDR_EXPR:
3161         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3162                                 0);
3163       default:
3164         break;
3165       }
3166
3167   if (flags & OEP_ONLY_CONST)
3168     return 0;
3169
3170 /* Define macros to test an operand from arg0 and arg1 for equality and a
3171    variant that allows null and views null as being different from any
3172    non-null value.  In the latter case, if either is null, the both
3173    must be; otherwise, do the normal comparison.  */
3174 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
3175                                     TREE_OPERAND (arg1, N), flags)
3176
3177 #define OP_SAME_WITH_NULL(N)                            \
3178   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3179    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3180
3181   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3182     {
3183     case tcc_unary:
3184       /* Two conversions are equal only if signedness and modes match.  */
3185       switch (TREE_CODE (arg0))
3186         {
3187         CASE_CONVERT:
3188         case FIX_TRUNC_EXPR:
3189           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3190               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3191             return 0;
3192           break;
3193         default:
3194           break;
3195         }
3196
3197       return OP_SAME (0);
3198
3199
3200     case tcc_comparison:
3201     case tcc_binary:
3202       if (OP_SAME (0) && OP_SAME (1))
3203         return 1;
3204
3205       /* For commutative ops, allow the other order.  */
3206       return (commutative_tree_code (TREE_CODE (arg0))
3207               && operand_equal_p (TREE_OPERAND (arg0, 0),
3208                                   TREE_OPERAND (arg1, 1), flags)
3209               && operand_equal_p (TREE_OPERAND (arg0, 1),
3210                                   TREE_OPERAND (arg1, 0), flags));
3211
3212     case tcc_reference:
3213       /* If either of the pointer (or reference) expressions we are
3214          dereferencing contain a side effect, these cannot be equal.  */
3215       if (TREE_SIDE_EFFECTS (arg0)
3216           || TREE_SIDE_EFFECTS (arg1))
3217         return 0;
3218
3219       switch (TREE_CODE (arg0))
3220         {
3221         case INDIRECT_REF:
3222         case ALIGN_INDIRECT_REF:
3223         case MISALIGNED_INDIRECT_REF:
3224         case REALPART_EXPR:
3225         case IMAGPART_EXPR:
3226           return OP_SAME (0);
3227
3228         case ARRAY_REF:
3229         case ARRAY_RANGE_REF:
3230           /* Operands 2 and 3 may be null.
3231              Compare the array index by value if it is constant first as we
3232              may have different types but same value here.  */
3233           return (OP_SAME (0)
3234                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3235                                           TREE_OPERAND (arg1, 1))
3236                       || OP_SAME (1))
3237                   && OP_SAME_WITH_NULL (2)
3238                   && OP_SAME_WITH_NULL (3));
3239
3240         case COMPONENT_REF:
3241           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
3242              may be NULL when we're called to compare MEM_EXPRs.  */
3243           return OP_SAME_WITH_NULL (0)
3244                  && OP_SAME (1)
3245                  && OP_SAME_WITH_NULL (2);
3246
3247         case BIT_FIELD_REF:
3248           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3249
3250         default:
3251           return 0;
3252         }
3253
3254     case tcc_expression:
3255       switch (TREE_CODE (arg0))
3256         {
3257         case ADDR_EXPR:
3258         case TRUTH_NOT_EXPR:
3259           return OP_SAME (0);
3260
3261         case TRUTH_ANDIF_EXPR:
3262         case TRUTH_ORIF_EXPR:
3263           return OP_SAME (0) && OP_SAME (1);
3264
3265         case TRUTH_AND_EXPR:
3266         case TRUTH_OR_EXPR:
3267         case TRUTH_XOR_EXPR:
3268           if (OP_SAME (0) && OP_SAME (1))
3269             return 1;
3270
3271           /* Otherwise take into account this is a commutative operation.  */
3272           return (operand_equal_p (TREE_OPERAND (arg0, 0),
3273                                    TREE_OPERAND (arg1, 1), flags)
3274                   && operand_equal_p (TREE_OPERAND (arg0, 1),
3275                                       TREE_OPERAND (arg1, 0), flags));
3276
3277         case COND_EXPR:
3278           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3279           
3280         default:
3281           return 0;
3282         }
3283
3284     case tcc_vl_exp:
3285       switch (TREE_CODE (arg0))
3286         {
3287         case CALL_EXPR:
3288           /* If the CALL_EXPRs call different functions, then they
3289              clearly can not be equal.  */
3290           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3291                                  flags))
3292             return 0;
3293
3294           {
3295             unsigned int cef = call_expr_flags (arg0);
3296             if (flags & OEP_PURE_SAME)
3297               cef &= ECF_CONST | ECF_PURE;
3298             else
3299               cef &= ECF_CONST;
3300             if (!cef)
3301               return 0;
3302           }
3303
3304           /* Now see if all the arguments are the same.  */
3305           {
3306             const_call_expr_arg_iterator iter0, iter1;
3307             const_tree a0, a1;
3308             for (a0 = first_const_call_expr_arg (arg0, &iter0),
3309                    a1 = first_const_call_expr_arg (arg1, &iter1);
3310                  a0 && a1;
3311                  a0 = next_const_call_expr_arg (&iter0),
3312                    a1 = next_const_call_expr_arg (&iter1))
3313               if (! operand_equal_p (a0, a1, flags))
3314                 return 0;
3315
3316             /* If we get here and both argument lists are exhausted
3317                then the CALL_EXPRs are equal.  */
3318             return ! (a0 || a1);
3319           }
3320         default:
3321           return 0;
3322         }
3323
3324     case tcc_declaration:
3325       /* Consider __builtin_sqrt equal to sqrt.  */
3326       return (TREE_CODE (arg0) == FUNCTION_DECL
3327               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3328               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3329               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3330
3331     default:
3332       return 0;
3333     }
3334
3335 #undef OP_SAME
3336 #undef OP_SAME_WITH_NULL
3337 }
3338 \f
3339 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3340    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3341
3342    When in doubt, return 0.  */
3343
3344 static int
3345 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3346 {
3347   int unsignedp1, unsignedpo;
3348   tree primarg0, primarg1, primother;
3349   unsigned int correct_width;
3350
3351   if (operand_equal_p (arg0, arg1, 0))
3352     return 1;
3353
3354   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3355       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3356     return 0;
3357
3358   /* Discard any conversions that don't change the modes of ARG0 and ARG1
3359      and see if the inner values are the same.  This removes any
3360      signedness comparison, which doesn't matter here.  */
3361   primarg0 = arg0, primarg1 = arg1;
3362   STRIP_NOPS (primarg0);
3363   STRIP_NOPS (primarg1);
3364   if (operand_equal_p (primarg0, primarg1, 0))
3365     return 1;
3366
3367   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3368      actual comparison operand, ARG0.
3369
3370      First throw away any conversions to wider types
3371      already present in the operands.  */
3372
3373   primarg1 = get_narrower (arg1, &unsignedp1);
3374   primother = get_narrower (other, &unsignedpo);
3375
3376   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3377   if (unsignedp1 == unsignedpo
3378       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3379       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3380     {
3381       tree type = TREE_TYPE (arg0);
3382
3383       /* Make sure shorter operand is extended the right way
3384          to match the longer operand.  */
3385       primarg1 = fold_convert (signed_or_unsigned_type_for
3386                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3387
3388       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3389         return 1;
3390     }
3391
3392   return 0;
3393 }
3394 \f
3395 /* See if ARG is an expression that is either a comparison or is performing
3396    arithmetic on comparisons.  The comparisons must only be comparing
3397    two different values, which will be stored in *CVAL1 and *CVAL2; if
3398    they are nonzero it means that some operands have already been found.
3399    No variables may be used anywhere else in the expression except in the
3400    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
3401    the expression and save_expr needs to be called with CVAL1 and CVAL2.
3402
3403    If this is true, return 1.  Otherwise, return zero.  */
3404
3405 static int
3406 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3407 {
3408   enum tree_code code = TREE_CODE (arg);
3409   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3410
3411   /* We can handle some of the tcc_expression cases here.  */
3412   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3413     tclass = tcc_unary;
3414   else if (tclass == tcc_expression
3415            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3416                || code == COMPOUND_EXPR))
3417     tclass = tcc_binary;
3418
3419   else if (tclass == tcc_expression && code == SAVE_EXPR
3420            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3421     {
3422       /* If we've already found a CVAL1 or CVAL2, this expression is
3423          two complex to handle.  */
3424       if (*cval1 || *cval2)
3425         return 0;
3426
3427       tclass = tcc_unary;
3428       *save_p = 1;
3429     }
3430
3431   switch (tclass)
3432     {
3433     case tcc_unary:
3434       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3435
3436     case tcc_binary:
3437       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3438               && twoval_comparison_p (TREE_OPERAND (arg, 1),
3439                                       cval1, cval2, save_p));
3440
3441     case tcc_constant:
3442       return 1;
3443
3444     case tcc_expression:
3445       if (code == COND_EXPR)
3446         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3447                                      cval1, cval2, save_p)
3448                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3449                                         cval1, cval2, save_p)
3450                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3451                                         cval1, cval2, save_p));
3452       return 0;
3453
3454     case tcc_comparison:
3455       /* First see if we can handle the first operand, then the second.  For
3456          the second operand, we know *CVAL1 can't be zero.  It must be that
3457          one side of the comparison is each of the values; test for the
3458          case where this isn't true by failing if the two operands
3459          are the same.  */
3460
3461       if (operand_equal_p (TREE_OPERAND (arg, 0),
3462                            TREE_OPERAND (arg, 1), 0))
3463         return 0;
3464
3465       if (*cval1 == 0)
3466         *cval1 = TREE_OPERAND (arg, 0);
3467       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3468         ;
3469       else if (*cval2 == 0)
3470         *cval2 = TREE_OPERAND (arg, 0);
3471       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3472         ;
3473       else
3474         return 0;
3475
3476       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3477         ;
3478       else if (*cval2 == 0)
3479         *cval2 = TREE_OPERAND (arg, 1);
3480       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3481         ;
3482       else
3483         return 0;
3484
3485       return 1;
3486
3487     default:
3488       return 0;
3489     }
3490 }
3491 \f
3492 /* ARG is a tree that is known to contain just arithmetic operations and
3493    comparisons.  Evaluate the operations in the tree substituting NEW0 for
3494    any occurrence of OLD0 as an operand of a comparison and likewise for
3495    NEW1 and OLD1.  */
3496
3497 static tree
3498 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
3499 {
3500   tree type = TREE_TYPE (arg);
3501   enum tree_code code = TREE_CODE (arg);
3502   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3503
3504   /* We can handle some of the tcc_expression cases here.  */
3505   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3506     tclass = tcc_unary;
3507   else if (tclass == tcc_expression
3508            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3509     tclass = tcc_binary;
3510
3511   switch (tclass)
3512     {
3513     case tcc_unary:
3514       return fold_build1 (code, type,
3515                           eval_subst (TREE_OPERAND (arg, 0),
3516                                       old0, new0, old1, new1));
3517
3518     case tcc_binary:
3519       return fold_build2 (code, type,
3520                           eval_subst (TREE_OPERAND (arg, 0),
3521                                       old0, new0, old1, new1),
3522                           eval_subst (TREE_OPERAND (arg, 1),
3523                                       old0, new0, old1, new1));
3524
3525     case tcc_expression:
3526       switch (code)
3527         {
3528         case SAVE_EXPR:
3529           return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3530
3531         case COMPOUND_EXPR:
3532           return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3533
3534         case COND_EXPR:
3535           return fold_build3 (code, type,
3536                               eval_subst (TREE_OPERAND (arg, 0),
3537                                           old0, new0, old1, new1),
3538                               eval_subst (TREE_OPERAND (arg, 1),
3539                                           old0, new0, old1, new1),
3540                               eval_subst (TREE_OPERAND (arg, 2),
3541                                           old0, new0, old1, new1));
3542         default:
3543           break;
3544         }
3545       /* Fall through - ???  */
3546
3547     case tcc_comparison:
3548       {
3549         tree arg0 = TREE_OPERAND (arg, 0);
3550         tree arg1 = TREE_OPERAND (arg, 1);
3551
3552         /* We need to check both for exact equality and tree equality.  The
3553            former will be true if the operand has a side-effect.  In that
3554            case, we know the operand occurred exactly once.  */
3555
3556         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3557           arg0 = new0;
3558         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3559           arg0 = new1;
3560
3561         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3562           arg1 = new0;
3563         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3564           arg1 = new1;
3565
3566         return fold_build2 (code, type, arg0, arg1);
3567       }
3568
3569     default:
3570       return arg;
3571     }
3572 }
3573 \f
3574 /* Return a tree for the case when the result of an expression is RESULT
3575    converted to TYPE and OMITTED was previously an operand of the expression
3576    but is now not needed (e.g., we folded OMITTED * 0).
3577
3578    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
3579    the conversion of RESULT to TYPE.  */
3580
3581 tree
3582 omit_one_operand (tree type, tree result, tree omitted)
3583 {
3584   tree t = fold_convert (type, result);
3585
3586   /* If the resulting operand is an empty statement, just return the omitted
3587      statement casted to void. */
3588   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3589     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3590
3591   if (TREE_SIDE_EFFECTS (omitted))
3592     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3593
3594   return non_lvalue (t);
3595 }
3596
3597 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
3598
3599 static tree
3600 pedantic_omit_one_operand (tree type, tree result, tree omitted)
3601 {
3602   tree t = fold_convert (type, result);
3603
3604   /* If the resulting operand is an empty statement, just return the omitted
3605      statement casted to void. */
3606   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3607     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3608
3609   if (TREE_SIDE_EFFECTS (omitted))
3610     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3611
3612   return pedantic_non_lvalue (t);
3613 }
3614
3615 /* Return a tree for the case when the result of an expression is RESULT
3616    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3617    of the expression but are now not needed.
3618
3619    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3620    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3621    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3622    just do the conversion of RESULT to TYPE.  */
3623
3624 tree
3625 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3626 {
3627   tree t = fold_convert (type, result);
3628
3629   if (TREE_SIDE_EFFECTS (omitted2))
3630     t = build2 (COMPOUND_EXPR, type, omitted2, t);
3631   if (TREE_SIDE_EFFECTS (omitted1))
3632     t = build2 (COMPOUND_EXPR, type, omitted1, t);
3633
3634   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3635 }
3636
3637 \f
3638 /* Return a simplified tree node for the truth-negation of ARG.  This
3639    never alters ARG itself.  We assume that ARG is an operation that
3640    returns a truth value (0 or 1).
3641
3642    FIXME: one would think we would fold the result, but it causes
3643    problems with the dominator optimizer.  */
3644
3645 tree
3646 fold_truth_not_expr (tree arg)
3647 {
3648   tree t, type = TREE_TYPE (arg);
3649   enum tree_code code = TREE_CODE (arg);
3650
3651   /* If this is a comparison, we can simply invert it, except for
3652      floating-point non-equality comparisons, in which case we just
3653      enclose a TRUTH_NOT_EXPR around what we have.  */
3654
3655   if (TREE_CODE_CLASS (code) == tcc_comparison)
3656     {
3657       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3658       if (FLOAT_TYPE_P (op_type)
3659           && flag_trapping_math
3660           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3661           && code != NE_EXPR && code != EQ_EXPR)
3662         return NULL_TREE;
3663
3664       code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3665       if (code == ERROR_MARK)
3666         return NULL_TREE;
3667
3668       t = build2 (code, type, TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3669       if (EXPR_HAS_LOCATION (arg))
3670         SET_EXPR_LOCATION (t, EXPR_LOCATION (arg));
3671       return t;
3672     }
3673
3674   switch (code)
3675     {
3676     case INTEGER_CST:
3677       return constant_boolean_node (integer_zerop (arg), type);
3678
3679     case TRUTH_AND_EXPR:
3680       t = build2 (TRUTH_OR_EXPR, type,
3681                   invert_truthvalue (TREE_OPERAND (arg, 0)),
3682                   invert_truthvalue (TREE_OPERAND (arg, 1)));
3683       break;
3684
3685     case TRUTH_OR_EXPR:
3686       t = build2 (TRUTH_AND_EXPR, type,
3687                   invert_truthvalue (TREE_OPERAND (arg, 0)),
3688                   invert_truthvalue (TREE_OPERAND (arg, 1)));
3689       break;
3690
3691     case TRUTH_XOR_EXPR:
3692       /* Here we can invert either operand.  We invert the first operand
3693          unless the second operand is a TRUTH_NOT_EXPR in which case our
3694          result is the XOR of the first operand with the inside of the
3695          negation of the second operand.  */
3696
3697       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3698         t = build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3699                     TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3700       else
3701         t = build2 (TRUTH_XOR_EXPR, type,
3702                     invert_truthvalue (TREE_OPERAND (arg, 0)),
3703                     TREE_OPERAND (arg, 1));
3704       break;
3705
3706     case TRUTH_ANDIF_EXPR:
3707       t = build2 (TRUTH_ORIF_EXPR, type,
3708                   invert_truthvalue (TREE_OPERAND (arg, 0)),
3709                   invert_truthvalue (TREE_OPERAND (arg, 1)));
3710       break;
3711
3712     case TRUTH_ORIF_EXPR:
3713       t = build2 (TRUTH_ANDIF_EXPR, type,
3714                   invert_truthvalue (TREE_OPERAND (arg, 0)),
3715                   invert_truthvalue (TREE_OPERAND (arg, 1)));
3716       break;
3717
3718     case TRUTH_NOT_EXPR:
3719       return TREE_OPERAND (arg, 0);
3720
3721     case COND_EXPR:
3722       {
3723         tree arg1 = TREE_OPERAND (arg, 1);
3724         tree arg2 = TREE_OPERAND (arg, 2);
3725         /* A COND_EXPR may have a throw as one operand, which
3726            then has void type.  Just leave void operands
3727            as they are.  */
3728         t = build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3729                     VOID_TYPE_P (TREE_TYPE (arg1))
3730                     ? arg1 : invert_truthvalue (arg1),
3731                     VOID_TYPE_P (TREE_TYPE (arg2))
3732                     ? arg2 : invert_truthvalue (arg2));
3733         break;
3734       }
3735
3736     case COMPOUND_EXPR:
3737       t = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3738                   invert_truthvalue (TREE_OPERAND (arg, 1)));
3739       break;
3740
3741     case NON_LVALUE_EXPR:
3742       return invert_truthvalue (TREE_OPERAND (arg, 0));
3743
3744     case NOP_EXPR:
3745       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3746         {
3747           t = build1 (TRUTH_NOT_EXPR, type, arg);
3748           break;
3749         }
3750
3751       /* ... fall through ...  */
3752
3753     case CONVERT_EXPR:
3754     case FLOAT_EXPR:
3755       t = build1 (TREE_CODE (arg), type,
3756                   invert_truthvalue (TREE_OPERAND (arg, 0)));
3757       break;
3758
3759     case BIT_AND_EXPR:
3760       if (!integer_onep (TREE_OPERAND (arg, 1)))
3761         return NULL_TREE;
3762       t = build2 (EQ_EXPR, type, arg, build_int_cst (type, 0));
3763       break;
3764
3765     case SAVE_EXPR:
3766       t = build1 (TRUTH_NOT_EXPR, type, arg);
3767       break;
3768
3769     case CLEANUP_POINT_EXPR:
3770       t = build1 (CLEANUP_POINT_EXPR, type,
3771                   invert_truthvalue (TREE_OPERAND (arg, 0)));
3772       break;
3773
3774     default:
3775       t = NULL_TREE;
3776       break;
3777     }
3778
3779   if (t && EXPR_HAS_LOCATION (arg))
3780     SET_EXPR_LOCATION (t, EXPR_LOCATION (arg));
3781
3782   return t;
3783 }
3784
3785 /* Return a simplified tree node for the truth-negation of ARG.  This
3786    never alters ARG itself.  We assume that ARG is an operation that
3787    returns a truth value (0 or 1).
3788
3789    FIXME: one would think we would fold the result, but it causes
3790    problems with the dominator optimizer.  */
3791
3792 tree
3793 invert_truthvalue (tree arg)
3794 {
3795   tree tem;
3796
3797   if (TREE_CODE (arg) == ERROR_MARK)
3798     return arg;
3799
3800   tem = fold_truth_not_expr (arg);
3801   if (!tem)
3802     tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3803
3804   return tem;
3805 }
3806
3807 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3808    operands are another bit-wise operation with a common input.  If so,
3809    distribute the bit operations to save an operation and possibly two if
3810    constants are involved.  For example, convert
3811         (A | B) & (A | C) into A | (B & C)
3812    Further simplification will occur if B and C are constants.
3813
3814    If this optimization cannot be done, 0 will be returned.  */
3815
3816 static tree
3817 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3818 {
3819   tree common;
3820   tree left, right;
3821
3822   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3823       || TREE_CODE (arg0) == code
3824       || (TREE_CODE (arg0) != BIT_AND_EXPR
3825           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3826     return 0;
3827
3828   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3829     {
3830       common = TREE_OPERAND (arg0, 0);
3831       left = TREE_OPERAND (arg0, 1);
3832       right = TREE_OPERAND (arg1, 1);
3833     }
3834   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3835     {
3836       common = TREE_OPERAND (arg0, 0);
3837       left = TREE_OPERAND (arg0, 1);
3838       right = TREE_OPERAND (arg1, 0);
3839     }
3840   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3841     {
3842       common = TREE_OPERAND (arg0, 1);
3843       left = TREE_OPERAND (arg0, 0);
3844       right = TREE_OPERAND (arg1, 1);
3845     }
3846   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3847     {
3848       common = TREE_OPERAND (arg0, 1);
3849       left = TREE_OPERAND (arg0, 0);
3850       right = TREE_OPERAND (arg1, 0);
3851     }
3852   else
3853     return 0;
3854
3855   common = fold_convert (type, common);
3856   left = fold_convert (type, left);
3857   right = fold_convert (type, right);
3858   return fold_build2 (TREE_CODE (arg0), type, common,
3859                       fold_build2 (code, type, left, right));
3860 }
3861
3862 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3863    with code CODE.  This optimization is unsafe.  */
3864 static tree
3865 distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3866 {
3867   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3868   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3869
3870   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3871   if (mul0 == mul1
3872       && operand_equal_p (TREE_OPERAND (arg0, 1),
3873                        TREE_OPERAND (arg1, 1), 0))
3874     return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3875                         fold_build2 (code, type,
3876                                      TREE_OPERAND (arg0, 0),
3877                                      TREE_OPERAND (arg1, 0)),
3878                         TREE_OPERAND (arg0, 1));
3879
3880   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3881   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3882                        TREE_OPERAND (arg1, 0), 0)
3883       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3884       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3885     {
3886       REAL_VALUE_TYPE r0, r1;
3887       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3888       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3889       if (!mul0)
3890         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3891       if (!mul1)
3892         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3893       real_arithmetic (&r0, code, &r0, &r1);
3894       return fold_build2 (MULT_EXPR, type,
3895                           TREE_OPERAND (arg0, 0),
3896                           build_real (type, r0));
3897     }
3898
3899   return NULL_TREE;
3900 }
3901 \f
3902 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3903    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3904
3905 static tree
3906 make_bit_field_ref (tree inner, tree type, HOST_WIDE_INT bitsize,
3907                     HOST_WIDE_INT bitpos, int unsignedp)
3908 {
3909   tree result, bftype;
3910
3911   if (bitpos == 0)
3912     {
3913       tree size = TYPE_SIZE (TREE_TYPE (inner));
3914       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3915            || POINTER_TYPE_P (TREE_TYPE (inner)))
3916           && host_integerp (size, 0) 
3917           && tree_low_cst (size, 0) == bitsize)
3918         return fold_convert (type, inner);
3919     }
3920
3921   bftype = type;
3922   if (TYPE_PRECISION (bftype) != bitsize
3923       || TYPE_UNSIGNED (bftype) == !unsignedp)
3924     bftype = build_nonstandard_integer_type (bitsize, 0);
3925
3926   result = build3 (BIT_FIELD_REF, bftype, inner,
3927                    size_int (bitsize), bitsize_int (bitpos));
3928
3929   if (bftype != type)
3930     result = fold_convert (type, result);
3931
3932   return result;
3933 }
3934
3935 /* Optimize a bit-field compare.
3936
3937    There are two cases:  First is a compare against a constant and the
3938    second is a comparison of two items where the fields are at the same
3939    bit position relative to the start of a chunk (byte, halfword, word)
3940    large enough to contain it.  In these cases we can avoid the shift
3941    implicit in bitfield extractions.
3942
3943    For constants, we emit a compare of the shifted constant with the
3944    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3945    compared.  For two fields at the same position, we do the ANDs with the
3946    similar mask and compare the result of the ANDs.
3947
3948    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3949    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3950    are the left and right operands of the comparison, respectively.
3951
3952    If the optimization described above can be done, we return the resulting
3953    tree.  Otherwise we return zero.  */
3954
3955 static tree
3956 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3957                             tree lhs, tree rhs)
3958 {
3959   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3960   tree type = TREE_TYPE (lhs);
3961   tree signed_type, unsigned_type;
3962   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3963   enum machine_mode lmode, rmode, nmode;
3964   int lunsignedp, runsignedp;
3965   int lvolatilep = 0, rvolatilep = 0;
3966   tree linner, rinner = NULL_TREE;
3967   tree mask;
3968   tree offset;
3969
3970   /* Get all the information about the extractions being done.  If the bit size
3971      if the same as the size of the underlying object, we aren't doing an
3972      extraction at all and so can do nothing.  We also don't want to
3973      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3974      then will no longer be able to replace it.  */
3975   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3976                                 &lunsignedp, &lvolatilep, false);
3977   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3978       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3979     return 0;
3980
3981  if (!const_p)
3982    {
3983      /* If this is not a constant, we can only do something if bit positions,
3984         sizes, and signedness are the same.  */
3985      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3986                                    &runsignedp, &rvolatilep, false);
3987
3988      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3989          || lunsignedp != runsignedp || offset != 0
3990          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3991        return 0;
3992    }
3993
3994   /* See if we can find a mode to refer to this field.  We should be able to,
3995      but fail if we can't.  */
3996   nmode = get_best_mode (lbitsize, lbitpos,
3997                          const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3998                          : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3999                                 TYPE_ALIGN (TREE_TYPE (rinner))),
4000                          word_mode, lvolatilep || rvolatilep);
4001   if (nmode == VOIDmode)
4002     return 0;
4003
4004   /* Set signed and unsigned types of the precision of this mode for the
4005      shifts below.  */
4006   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
4007   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4008
4009   /* Compute the bit position and size for the new reference and our offset
4010      within it. If the new reference is the same size as the original, we
4011      won't optimize anything, so return zero.  */
4012   nbitsize = GET_MODE_BITSIZE (nmode);
4013   nbitpos = lbitpos & ~ (nbitsize - 1);
4014   lbitpos -= nbitpos;
4015   if (nbitsize == lbitsize)
4016     return 0;
4017
4018   if (BYTES_BIG_ENDIAN)
4019     lbitpos = nbitsize - lbitsize - lbitpos;
4020
4021   /* Make the mask to be used against the extracted field.  */
4022   mask = build_int_cst_type (unsigned_type, -1);
4023   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
4024   mask = const_binop (RSHIFT_EXPR, mask,
4025                       size_int (nbitsize - lbitsize - lbitpos), 0);
4026
4027   if (! const_p)
4028     /* If not comparing with constant, just rework the comparison
4029        and return.  */
4030     return fold_build2 (code, compare_type,
4031                         fold_build2 (BIT_AND_EXPR, unsigned_type,
4032                                      make_bit_field_ref (linner,
4033                                                          unsigned_type,
4034                                                          nbitsize, nbitpos,
4035                                                          1),
4036                                      mask),
4037                         fold_build2 (BIT_AND_EXPR, unsigned_type,
4038                                      make_bit_field_ref (rinner,
4039                                                          unsigned_type,
4040                                                          nbitsize, nbitpos,
4041                                                          1),
4042                                      mask));
4043
4044   /* Otherwise, we are handling the constant case. See if the constant is too
4045      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
4046      this not only for its own sake, but to avoid having to test for this
4047      error case below.  If we didn't, we might generate wrong code.
4048
4049      For unsigned fields, the constant shifted right by the field length should
4050      be all zero.  For signed fields, the high-order bits should agree with
4051      the sign bit.  */
4052
4053   if (lunsignedp)
4054     {
4055       if (! integer_zerop (const_binop (RSHIFT_EXPR,
4056                                         fold_convert (unsigned_type, rhs),
4057                                         size_int (lbitsize), 0)))
4058         {
4059           warning (0, "comparison is always %d due to width of bit-field",
4060                    code == NE_EXPR);
4061           return constant_boolean_node (code == NE_EXPR, compare_type);
4062         }
4063     }
4064   else
4065     {
4066       tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
4067                               size_int (lbitsize - 1), 0);
4068       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
4069         {
4070           warning (0, "comparison is always %d due to width of bit-field",
4071                    code == NE_EXPR);
4072           return constant_boolean_node (code == NE_EXPR, compare_type);
4073         }
4074     }
4075
4076   /* Single-bit compares should always be against zero.  */
4077   if (lbitsize == 1 && ! integer_zerop (rhs))
4078     {
4079       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4080       rhs = build_int_cst (type, 0);
4081     }
4082
4083   /* Make a new bitfield reference, shift the constant over the
4084      appropriate number of bits and mask it with the computed mask
4085      (in case this was a signed field).  If we changed it, make a new one.  */
4086   lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
4087   if (lvolatilep)
4088     {
4089       TREE_SIDE_EFFECTS (lhs) = 1;
4090       TREE_THIS_VOLATILE (lhs) = 1;
4091     }
4092
4093   rhs = const_binop (BIT_AND_EXPR,
4094                      const_binop (LSHIFT_EXPR,
4095                                   fold_convert (unsigned_type, rhs),
4096                                   size_int (lbitpos), 0),
4097                      mask, 0);
4098
4099   return build2 (code, compare_type,
4100                  build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
4101                  rhs);
4102 }
4103 \f
4104 /* Subroutine for fold_truthop: decode a field reference.
4105
4106    If EXP is a comparison reference, we return the innermost reference.
4107
4108    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4109    set to the starting bit number.
4110
4111    If the innermost field can be completely contained in a mode-sized
4112    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
4113
4114    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4115    otherwise it is not changed.
4116
4117    *PUNSIGNEDP is set to the signedness of the field.
4118
4119    *PMASK is set to the mask used.  This is either contained in a
4120    BIT_AND_EXPR or derived from the width of the field.
4121
4122    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4123
4124    Return 0 if this is not a component reference or is one that we can't
4125    do anything with.  */
4126
4127 static tree
4128 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
4129                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
4130                         int *punsignedp, int *pvolatilep,
4131                         tree *pmask, tree *pand_mask)
4132 {
4133   tree outer_type = 0;
4134   tree and_mask = 0;
4135   tree mask, inner, offset;
4136   tree unsigned_type;
4137   unsigned int precision;
4138
4139   /* All the optimizations using this function assume integer fields.
4140      There are problems with FP fields since the type_for_size call
4141      below can fail for, e.g., XFmode.  */
4142   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4143     return 0;
4144
4145   /* We are interested in the bare arrangement of bits, so strip everything
4146      that doesn't affect the machine mode.  However, record the type of the
4147      outermost expression if it may matter below.  */
4148   if (CONVERT_EXPR_P (exp)
4149       || TREE_CODE (exp) == NON_LVALUE_EXPR)
4150     outer_type = TREE_TYPE (exp);
4151   STRIP_NOPS (exp);
4152
4153   if (TREE_CODE (exp) == BIT_AND_EXPR)
4154     {
4155       and_mask = TREE_OPERAND (exp, 1);
4156       exp = TREE_OPERAND (exp, 0);
4157       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4158       if (TREE_CODE (and_mask) != INTEGER_CST)
4159         return 0;
4160     }
4161
4162   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4163                                punsignedp, pvolatilep, false);
4164   if ((inner == exp && and_mask == 0)
4165       || *pbitsize < 0 || offset != 0
4166       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4167     return 0;
4168
4169   /* If the number of bits in the reference is the same as the bitsize of
4170      the outer type, then the outer type gives the signedness. Otherwise
4171      (in case of a small bitfield) the signedness is unchanged.  */
4172   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4173     *punsignedp = TYPE_UNSIGNED (outer_type);
4174
4175   /* Compute the mask to access the bitfield.  */
4176   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4177   precision = TYPE_PRECISION (unsigned_type);
4178
4179   mask = build_int_cst_type (unsigned_type, -1);
4180
4181   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4182   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4183
4184   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
4185   if (and_mask != 0)
4186     mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
4187                         fold_convert (unsigned_type, and_mask), mask);
4188
4189   *pmask = mask;
4190   *pand_mask = and_mask;
4191   return inner;
4192 }
4193
4194 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4195    bit positions.  */
4196
4197 static int
4198 all_ones_mask_p (const_tree mask, int size)
4199 {
4200   tree type = TREE_TYPE (mask);
4201   unsigned int precision = TYPE_PRECISION (type);
4202   tree tmask;
4203
4204   tmask = build_int_cst_type (signed_type_for (type), -1);
4205
4206   return
4207     tree_int_cst_equal (mask,
4208                         const_binop (RSHIFT_EXPR,
4209                                      const_binop (LSHIFT_EXPR, tmask,
4210                                                   size_int (precision - size),
4211                                                   0),
4212                                      size_int (precision - size), 0));
4213 }
4214
4215 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4216    represents the sign bit of EXP's type.  If EXP represents a sign
4217    or zero extension, also test VAL against the unextended type.
4218    The return value is the (sub)expression whose sign bit is VAL,
4219    or NULL_TREE otherwise.  */
4220
4221 static tree
4222 sign_bit_p (tree exp, const_tree val)
4223 {
4224   unsigned HOST_WIDE_INT mask_lo, lo;
4225   HOST_WIDE_INT mask_hi, hi;
4226   int width;
4227   tree t;
4228
4229   /* Tree EXP must have an integral type.  */
4230   t = TREE_TYPE (exp);
4231   if (! INTEGRAL_TYPE_P (t))
4232     return NULL_TREE;
4233
4234   /* Tree VAL must be an integer constant.  */
4235   if (TREE_CODE (val) != INTEGER_CST
4236       || TREE_OVERFLOW (val))
4237     return NULL_TREE;
4238
4239   width = TYPE_PRECISION (t);
4240   if (width > HOST_BITS_PER_WIDE_INT)
4241     {
4242       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
4243       lo = 0;
4244
4245       mask_hi = ((unsigned HOST_WIDE_INT) -1
4246                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
4247       mask_lo = -1;
4248     }
4249   else
4250     {
4251       hi = 0;
4252       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
4253
4254       mask_hi = 0;
4255       mask_lo = ((unsigned HOST_WIDE_INT) -1
4256                  >> (HOST_BITS_PER_WIDE_INT - width));
4257     }
4258
4259   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4260      treat VAL as if it were unsigned.  */
4261   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4262       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
4263     return exp;
4264
4265   /* Handle extension from a narrower type.  */
4266   if (TREE_CODE (exp) == NOP_EXPR
4267       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4268     return sign_bit_p (TREE_OPERAND (exp, 0), val);
4269
4270   return NULL_TREE;
4271 }
4272
4273 /* Subroutine for fold_truthop: determine if an operand is simple enough
4274    to be evaluated unconditionally.  */
4275
4276 static int
4277 simple_operand_p (const_tree exp)
4278 {
4279   /* Strip any conversions that don't change the machine mode.  */
4280   STRIP_NOPS (exp);
4281
4282   return (CONSTANT_CLASS_P (exp)
4283           || TREE_CODE (exp) == SSA_NAME
4284           || (DECL_P (exp)
4285               && ! TREE_ADDRESSABLE (exp)
4286               && ! TREE_THIS_VOLATILE (exp)
4287               && ! DECL_NONLOCAL (exp)
4288               /* Don't regard global variables as simple.  They may be
4289                  allocated in ways unknown to the compiler (shared memory,
4290                  #pragma weak, etc).  */
4291               && ! TREE_PUBLIC (exp)
4292               && ! DECL_EXTERNAL (exp)
4293               /* Loading a static variable is unduly expensive, but global
4294                  registers aren't expensive.  */
4295               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4296 }
4297 \f
4298 /* The following functions are subroutines to fold_range_test and allow it to
4299    try to change a logical combination of comparisons into a range test.
4300
4301    For example, both
4302         X == 2 || X == 3 || X == 4 || X == 5
4303    and
4304         X >= 2 && X <= 5
4305    are converted to
4306         (unsigned) (X - 2) <= 3
4307
4308    We describe each set of comparisons as being either inside or outside
4309    a range, using a variable named like IN_P, and then describe the
4310    range with a lower and upper bound.  If one of the bounds is omitted,
4311    it represents either the highest or lowest value of the type.
4312
4313    In the comments below, we represent a range by two numbers in brackets
4314    preceded by a "+" to designate being inside that range, or a "-" to
4315    designate being outside that range, so the condition can be inverted by
4316    flipping the prefix.  An omitted bound is represented by a "-".  For
4317    example, "- [-, 10]" means being outside the range starting at the lowest
4318    possible value and ending at 10, in other words, being greater than 10.
4319    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4320    always false.
4321
4322    We set up things so that the missing bounds are handled in a consistent
4323    manner so neither a missing bound nor "true" and "false" need to be
4324    handled using a special case.  */
4325
4326 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4327    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4328    and UPPER1_P are nonzero if the respective argument is an upper bound
4329    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
4330    must be specified for a comparison.  ARG1 will be converted to ARG0's
4331    type if both are specified.  */
4332
4333 static tree
4334 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4335              tree arg1, int upper1_p)
4336 {
4337   tree tem;
4338   int result;
4339   int sgn0, sgn1;
4340
4341   /* If neither arg represents infinity, do the normal operation.
4342      Else, if not a comparison, return infinity.  Else handle the special
4343      comparison rules. Note that most of the cases below won't occur, but
4344      are handled for consistency.  */
4345
4346   if (arg0 != 0 && arg1 != 0)
4347     {
4348       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4349                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
4350       STRIP_NOPS (tem);
4351       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4352     }
4353
4354   if (TREE_CODE_CLASS (code) != tcc_comparison)
4355     return 0;
4356
4357   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4358      for neither.  In real maths, we cannot assume open ended ranges are
4359      the same. But, this is computer arithmetic, where numbers are finite.
4360      We can therefore make the transformation of any unbounded range with
4361      the value Z, Z being greater than any representable number. This permits
4362      us to treat unbounded ranges as equal.  */
4363   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4364   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4365   switch (code)
4366     {
4367     case EQ_EXPR:
4368       result = sgn0 == sgn1;
4369       break;
4370     case NE_EXPR:
4371       result = sgn0 != sgn1;
4372       break;
4373     case LT_EXPR:
4374       result = sgn0 < sgn1;
4375       break;
4376     case LE_EXPR:
4377       result = sgn0 <= sgn1;
4378       break;
4379     case GT_EXPR:
4380       result = sgn0 > sgn1;
4381       break;
4382     case GE_EXPR:
4383       result = sgn0 >= sgn1;
4384       break;
4385     default:
4386       gcc_unreachable ();
4387     }
4388
4389   return constant_boolean_node (result, type);
4390 }
4391 \f
4392 /* Given EXP, a logical expression, set the range it is testing into
4393    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
4394    actually being tested.  *PLOW and *PHIGH will be made of the same
4395    type as the returned expression.  If EXP is not a comparison, we
4396    will most likely not be returning a useful value and range.  Set
4397    *STRICT_OVERFLOW_P to true if the return value is only valid
4398    because signed overflow is undefined; otherwise, do not change
4399    *STRICT_OVERFLOW_P.  */
4400
4401 static tree
4402 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4403             bool *strict_overflow_p)
4404 {
4405   enum tree_code code;
4406   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4407   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4408   int in_p, n_in_p;
4409   tree low, high, n_low, n_high;
4410
4411   /* Start with simply saying "EXP != 0" and then look at the code of EXP
4412      and see if we can refine the range.  Some of the cases below may not
4413      happen, but it doesn't seem worth worrying about this.  We "continue"
4414      the outer loop when we've changed something; otherwise we "break"
4415      the switch, which will "break" the while.  */
4416
4417   in_p = 0;
4418   low = high = build_int_cst (TREE_TYPE (exp), 0);
4419
4420   while (1)
4421     {
4422       code = TREE_CODE (exp);
4423       exp_type = TREE_TYPE (exp);
4424
4425       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4426         {
4427           if (TREE_OPERAND_LENGTH (exp) > 0)
4428             arg0 = TREE_OPERAND (exp, 0);
4429           if (TREE_CODE_CLASS (code) == tcc_comparison
4430               || TREE_CODE_CLASS (code) == tcc_unary
4431               || TREE_CODE_CLASS (code) == tcc_binary)
4432             arg0_type = TREE_TYPE (arg0);
4433           if (TREE_CODE_CLASS (code) == tcc_binary
4434               || TREE_CODE_CLASS (code) == tcc_comparison
4435               || (TREE_CODE_CLASS (code) == tcc_expression
4436                   && TREE_OPERAND_LENGTH (exp) > 1))
4437             arg1 = TREE_OPERAND (exp, 1);
4438         }
4439
4440       switch (code)
4441         {
4442         case TRUTH_NOT_EXPR:
4443           in_p = ! in_p, exp = arg0;
4444           continue;
4445
4446         case EQ_EXPR: case NE_EXPR:
4447         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4448           /* We can only do something if the range is testing for zero
4449              and if the second operand is an integer constant.  Note that
4450              saying something is "in" the range we make is done by
4451              complementing IN_P since it will set in the initial case of
4452              being not equal to zero; "out" is leaving it alone.  */
4453           if (low == 0 || high == 0
4454               || ! integer_zerop (low) || ! integer_zerop (high)
4455               || TREE_CODE (arg1) != INTEGER_CST)
4456             break;
4457
4458           switch (code)
4459             {
4460             case NE_EXPR:  /* - [c, c]  */
4461               low = high = arg1;
4462               break;
4463             case EQ_EXPR:  /* + [c, c]  */
4464               in_p = ! in_p, low = high = arg1;
4465               break;
4466             case GT_EXPR:  /* - [-, c] */
4467               low = 0, high = arg1;
4468               break;
4469             case GE_EXPR:  /* + [c, -] */
4470               in_p = ! in_p, low = arg1, high = 0;
4471               break;
4472             case LT_EXPR:  /* - [c, -] */
4473               low = arg1, high = 0;
4474               break;
4475             case LE_EXPR:  /* + [-, c] */
4476               in_p = ! in_p, low = 0, high = arg1;
4477               break;
4478             default:
4479               gcc_unreachable ();
4480             }
4481
4482           /* If this is an unsigned comparison, we also know that EXP is
4483              greater than or equal to zero.  We base the range tests we make
4484              on that fact, so we record it here so we can parse existing
4485              range tests.  We test arg0_type since often the return type
4486              of, e.g. EQ_EXPR, is boolean.  */
4487           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4488             {
4489               if (! merge_ranges (&n_in_p, &n_low, &n_high,
4490                                   in_p, low, high, 1,
4491                                   build_int_cst (arg0_type, 0),
4492                                   NULL_TREE))
4493                 break;
4494
4495               in_p = n_in_p, low = n_low, high = n_high;
4496
4497               /* If the high bound is missing, but we have a nonzero low
4498                  bound, reverse the range so it goes from zero to the low bound
4499                  minus 1.  */
4500               if (high == 0 && low && ! integer_zerop (low))
4501                 {
4502                   in_p = ! in_p;
4503                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4504                                       integer_one_node, 0);
4505                   low = build_int_cst (arg0_type, 0);
4506                 }
4507             }
4508
4509           exp = arg0;
4510           continue;
4511
4512         case NEGATE_EXPR:
4513           /* (-x) IN [a,b] -> x in [-b, -a]  */
4514           n_low = range_binop (MINUS_EXPR, exp_type,
4515                                build_int_cst (exp_type, 0),
4516                                0, high, 1);
4517           n_high = range_binop (MINUS_EXPR, exp_type,
4518                                 build_int_cst (exp_type, 0),
4519                                 0, low, 0);
4520           low = n_low, high = n_high;
4521           exp = arg0;
4522           continue;
4523
4524         case BIT_NOT_EXPR:
4525           /* ~ X -> -X - 1  */
4526           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4527                         build_int_cst (exp_type, 1));
4528           continue;
4529
4530         case PLUS_EXPR:  case MINUS_EXPR:
4531           if (TREE_CODE (arg1) != INTEGER_CST)
4532             break;
4533
4534           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4535              move a constant to the other side.  */
4536           if (!TYPE_UNSIGNED (arg0_type)
4537               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4538             break;
4539
4540           /* If EXP is signed, any overflow in the computation is undefined,
4541              so we don't worry about it so long as our computations on
4542              the bounds don't overflow.  For unsigned, overflow is defined
4543              and this is exactly the right thing.  */
4544           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4545                                arg0_type, low, 0, arg1, 0);
4546           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4547                                 arg0_type, high, 1, arg1, 0);
4548           if ((n_low != 0 && TREE_OVERFLOW (n_low))
4549               || (n_high != 0 && TREE_OVERFLOW (n_high)))
4550             break;
4551
4552           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4553             *strict_overflow_p = true;
4554
4555           /* Check for an unsigned range which has wrapped around the maximum
4556              value thus making n_high < n_low, and normalize it.  */
4557           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4558             {
4559               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4560                                  integer_one_node, 0);
4561               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4562                                   integer_one_node, 0);
4563
4564               /* If the range is of the form +/- [ x+1, x ], we won't
4565                  be able to normalize it.  But then, it represents the
4566                  whole range or the empty set, so make it
4567                  +/- [ -, - ].  */
4568               if (tree_int_cst_equal (n_low, low)
4569                   && tree_int_cst_equal (n_high, high))
4570                 low = high = 0;
4571               else
4572                 in_p = ! in_p;
4573             }
4574           else
4575             low = n_low, high = n_high;
4576
4577           exp = arg0;
4578           continue;
4579
4580         CASE_CONVERT: case NON_LVALUE_EXPR:
4581           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4582             break;
4583
4584           if (! INTEGRAL_TYPE_P (arg0_type)
4585               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4586               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4587             break;
4588
4589           n_low = low, n_high = high;
4590
4591           if (n_low != 0)
4592             n_low = fold_convert (arg0_type, n_low);
4593
4594           if (n_high != 0)
4595             n_high = fold_convert (arg0_type, n_high);
4596
4597
4598           /* If we're converting arg0 from an unsigned type, to exp,
4599              a signed type,  we will be doing the comparison as unsigned.
4600              The tests above have already verified that LOW and HIGH
4601              are both positive.
4602
4603              So we have to ensure that we will handle large unsigned
4604              values the same way that the current signed bounds treat
4605              negative values.  */
4606
4607           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4608             {
4609               tree high_positive;
4610               tree equiv_type;
4611               /* For fixed-point modes, we need to pass the saturating flag
4612                  as the 2nd parameter.  */
4613               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4614                 equiv_type = lang_hooks.types.type_for_mode
4615                              (TYPE_MODE (arg0_type),
4616                               TYPE_SATURATING (arg0_type));
4617               else
4618                 equiv_type = lang_hooks.types.type_for_mode
4619                              (TYPE_MODE (arg0_type), 1);
4620
4621               /* A range without an upper bound is, naturally, unbounded.
4622                  Since convert would have cropped a very large value, use
4623                  the max value for the destination type.  */
4624               high_positive
4625                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4626                 : TYPE_MAX_VALUE (arg0_type);
4627
4628               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4629                 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4630                                              fold_convert (arg0_type,
4631                                                            high_positive),
4632                                              build_int_cst (arg0_type, 1));
4633
4634               /* If the low bound is specified, "and" the range with the
4635                  range for which the original unsigned value will be
4636                  positive.  */
4637               if (low != 0)
4638                 {
4639                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4640                                       1, n_low, n_high, 1,
4641                                       fold_convert (arg0_type,
4642                                                     integer_zero_node),
4643                                       high_positive))
4644                     break;
4645
4646                   in_p = (n_in_p == in_p);
4647                 }
4648               else
4649                 {
4650                   /* Otherwise, "or" the range with the range of the input
4651                      that will be interpreted as negative.  */
4652                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4653                                       0, n_low, n_high, 1,
4654                                       fold_convert (arg0_type,
4655                                                     integer_zero_node),
4656                                       high_positive))
4657                     break;
4658
4659                   in_p = (in_p != n_in_p);
4660                 }
4661             }
4662
4663           exp = arg0;
4664           low = n_low, high = n_high;
4665           continue;
4666
4667         default:
4668           break;
4669         }
4670
4671       break;
4672     }
4673
4674   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4675   if (TREE_CODE (exp) == INTEGER_CST)
4676     {
4677       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4678                                                  exp, 0, low, 0))
4679                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4680                                                     exp, 1, high, 1)));
4681       low = high = 0;
4682       exp = 0;
4683     }
4684
4685   *pin_p = in_p, *plow = low, *phigh = high;
4686   return exp;
4687 }
4688 \f
4689 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4690    type, TYPE, return an expression to test if EXP is in (or out of, depending
4691    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4692
4693 static tree
4694 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
4695 {
4696   tree etype = TREE_TYPE (exp), value;
4697   enum tree_code code;
4698
4699 #ifdef HAVE_canonicalize_funcptr_for_compare
4700   /* Disable this optimization for function pointer expressions
4701      on targets that require function pointer canonicalization.  */
4702   if (HAVE_canonicalize_funcptr_for_compare
4703       && TREE_CODE (etype) == POINTER_TYPE
4704       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4705     return NULL_TREE;
4706 #endif
4707
4708   if (! in_p)
4709     {
4710       value = build_range_check (type, exp, 1, low, high);
4711       if (value != 0)
4712         return invert_truthvalue (value);
4713
4714       return 0;
4715     }
4716
4717   if (low == 0 && high == 0)
4718     return build_int_cst (type, 1);
4719
4720   if (low == 0)
4721     return fold_build2 (LE_EXPR, type, exp,
4722                         fold_convert (etype, high));
4723
4724   if (high == 0)
4725     return fold_build2 (GE_EXPR, type, exp,
4726                         fold_convert (etype, low));
4727
4728   if (operand_equal_p (low, high, 0))
4729     return fold_build2 (EQ_EXPR, type, exp,
4730                         fold_convert (etype, low));
4731
4732   if (integer_zerop (low))
4733     {
4734       if (! TYPE_UNSIGNED (etype))
4735         {
4736           etype = unsigned_type_for (etype);
4737           high = fold_convert (etype, high);
4738           exp = fold_convert (etype, exp);
4739         }
4740       return build_range_check (type, exp, 1, 0, high);
4741     }
4742
4743   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4744   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4745     {
4746       unsigned HOST_WIDE_INT lo;
4747       HOST_WIDE_INT hi;
4748       int prec;
4749
4750       prec = TYPE_PRECISION (etype);
4751       if (prec <= HOST_BITS_PER_WIDE_INT)
4752         {
4753           hi = 0;
4754           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4755         }
4756       else
4757         {
4758           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4759           lo = (unsigned HOST_WIDE_INT) -1;
4760         }
4761
4762       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4763         {
4764           if (TYPE_UNSIGNED (etype))
4765             {
4766               tree signed_etype = signed_type_for (etype);
4767               if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4768                 etype
4769                   = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4770               else
4771                 etype = signed_etype;
4772               exp = fold_convert (etype, exp);
4773             }
4774           return fold_build2 (GT_EXPR, type, exp,
4775                               build_int_cst (etype, 0));
4776         }
4777     }
4778
4779   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4780      This requires wrap-around arithmetics for the type of the expression.  */
4781   code = TREE_CODE (etype);
4782   switch (code)
4783     {
4784     case INTEGER_TYPE:
4785     case ENUMERAL_TYPE:
4786     case BOOLEAN_TYPE:
4787       /* There is no requirement that LOW be within the range of ETYPE
4788          if the latter is a subtype.  It must, however, be within the base
4789          type of ETYPE.  So be sure we do the subtraction in that type.  */
4790       if (code == INTEGER_TYPE && TREE_TYPE (etype))
4791         {
4792           etype = TREE_TYPE (etype);
4793           /* But not in an enumeral or boolean type though.  */
4794           code = TREE_CODE (etype);
4795         }
4796
4797       if (code != INTEGER_TYPE)
4798         etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4799                                                 TYPE_UNSIGNED (etype));
4800       break;
4801
4802     default:
4803       break;
4804     }
4805
4806   /* If we don't have wrap-around arithmetics upfront, try to force it.  */
4807   if (TREE_CODE (etype) == INTEGER_TYPE
4808       && !TYPE_OVERFLOW_WRAPS (etype))
4809     {
4810       tree utype, minv, maxv;
4811
4812       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4813          for the type in question, as we rely on this here.  */
4814       utype = unsigned_type_for (etype);
4815       maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4816       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4817                           integer_one_node, 1);
4818       minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4819
4820       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4821                                       minv, 1, maxv, 1)))
4822         etype = utype;
4823       else
4824         return 0;
4825     }
4826
4827   high = fold_convert (etype, high);
4828   low = fold_convert (etype, low);
4829   exp = fold_convert (etype, exp);
4830
4831   value = const_binop (MINUS_EXPR, high, low, 0);
4832
4833
4834   if (POINTER_TYPE_P (etype))
4835     {
4836       if (value != 0 && !TREE_OVERFLOW (value))
4837         {
4838           low = fold_convert (sizetype, low);
4839           low = fold_build1 (NEGATE_EXPR, sizetype, low);
4840           return build_range_check (type,
4841                                     fold_build2 (POINTER_PLUS_EXPR, etype, exp, low),
4842                                     1, build_int_cst (etype, 0), value);
4843         }
4844       return 0;
4845     }
4846
4847   if (value != 0 && !TREE_OVERFLOW (value))
4848     return build_range_check (type,
4849                               fold_build2 (MINUS_EXPR, etype, exp, low),
4850                               1, build_int_cst (etype, 0), value);
4851
4852   return 0;
4853 }
4854 \f
4855 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4856
4857 static tree
4858 range_predecessor (tree val)
4859 {
4860   tree type = TREE_TYPE (val);
4861
4862   if (INTEGRAL_TYPE_P (type)
4863       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4864     return 0;
4865   else
4866     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4867 }
4868
4869 /* Return the successor of VAL in its type, handling the infinite case.  */
4870
4871 static tree
4872 range_successor (tree val)
4873 {
4874   tree type = TREE_TYPE (val);
4875
4876   if (INTEGRAL_TYPE_P (type)
4877       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4878     return 0;
4879   else
4880     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4881 }
4882
4883 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4884    can, 0 if we can't.  Set the output range into the specified parameters.  */
4885
4886 static int
4887 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4888               tree high0, int in1_p, tree low1, tree high1)
4889 {
4890   int no_overlap;
4891   int subset;
4892   int temp;
4893   tree tem;
4894   int in_p;
4895   tree low, high;
4896   int lowequal = ((low0 == 0 && low1 == 0)
4897                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4898                                                 low0, 0, low1, 0)));
4899   int highequal = ((high0 == 0 && high1 == 0)
4900                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4901                                                  high0, 1, high1, 1)));
4902
4903   /* Make range 0 be the range that starts first, or ends last if they
4904      start at the same value.  Swap them if it isn't.  */
4905   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4906                                  low0, 0, low1, 0))
4907       || (lowequal
4908           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4909                                         high1, 1, high0, 1))))
4910     {
4911       temp = in0_p, in0_p = in1_p, in1_p = temp;
4912       tem = low0, low0 = low1, low1 = tem;
4913       tem = high0, high0 = high1, high1 = tem;
4914     }
4915
4916   /* Now flag two cases, whether the ranges are disjoint or whether the
4917      second range is totally subsumed in the first.  Note that the tests
4918      below are simplified by the ones above.  */
4919   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4920                                           high0, 1, low1, 0));
4921   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4922                                       high1, 1, high0, 1));
4923
4924   /* We now have four cases, depending on whether we are including or
4925      excluding the two ranges.  */
4926   if (in0_p && in1_p)
4927     {
4928       /* If they don't overlap, the result is false.  If the second range
4929          is a subset it is the result.  Otherwise, the range is from the start
4930          of the second to the end of the first.  */
4931       if (no_overlap)
4932         in_p = 0, low = high = 0;
4933       else if (subset)
4934         in_p = 1, low = low1, high = high1;
4935       else
4936         in_p = 1, low = low1, high = high0;
4937     }
4938
4939   else if (in0_p && ! in1_p)
4940     {
4941       /* If they don't overlap, the result is the first range.  If they are
4942          equal, the result is false.  If the second range is a subset of the
4943          first, and the ranges begin at the same place, we go from just after
4944          the end of the second range to the end of the first.  If the second
4945          range is not a subset of the first, or if it is a subset and both
4946          ranges end at the same place, the range starts at the start of the
4947          first range and ends just before the second range.
4948          Otherwise, we can't describe this as a single range.  */
4949       if (no_overlap)
4950         in_p = 1, low = low0, high = high0;
4951       else if (lowequal && highequal)
4952         in_p = 0, low = high = 0;
4953       else if (subset && lowequal)
4954         {
4955           low = range_successor (high1);
4956           high = high0;
4957           in_p = 1;
4958           if (low == 0)
4959             {
4960               /* We are in the weird situation where high0 > high1 but
4961                  high1 has no successor.  Punt.  */
4962               return 0;
4963             }
4964         }
4965       else if (! subset || highequal)
4966         {
4967           low = low0;
4968           high = range_predecessor (low1);
4969           in_p = 1;
4970           if (high == 0)
4971             {
4972               /* low0 < low1 but low1 has no predecessor.  Punt.  */
4973               return 0;
4974             }
4975         }
4976       else
4977         return 0;
4978     }
4979
4980   else if (! in0_p && in1_p)
4981     {
4982       /* If they don't overlap, the result is the second range.  If the second
4983          is a subset of the first, the result is false.  Otherwise,
4984          the range starts just after the first range and ends at the
4985          end of the second.  */
4986       if (no_overlap)
4987         in_p = 1, low = low1, high = high1;
4988       else if (subset || highequal)
4989         in_p = 0, low = high = 0;
4990       else
4991         {
4992           low = range_successor (high0);
4993           high = high1;
4994           in_p = 1;
4995           if (low == 0)
4996             {
4997               /* high1 > high0 but high0 has no successor.  Punt.  */
4998               return 0;
4999             }
5000         }
5001     }
5002
5003   else
5004     {
5005       /* The case where we are excluding both ranges.  Here the complex case
5006          is if they don't overlap.  In that case, the only time we have a
5007          range is if they are adjacent.  If the second is a subset of the
5008          first, the result is the first.  Otherwise, the range to exclude
5009          starts at the beginning of the first range and ends at the end of the
5010          second.  */
5011       if (no_overlap)
5012         {
5013           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5014                                          range_successor (high0),
5015                                          1, low1, 0)))
5016             in_p = 0, low = low0, high = high1;
5017           else
5018             {
5019               /* Canonicalize - [min, x] into - [-, x].  */
5020               if (low0 && TREE_CODE (low0) == INTEGER_CST)
5021                 switch (TREE_CODE (TREE_TYPE (low0)))
5022                   {
5023                   case ENUMERAL_TYPE:
5024                     if (TYPE_PRECISION (TREE_TYPE (low0))
5025                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
5026                       break;
5027                     /* FALLTHROUGH */
5028                   case INTEGER_TYPE:
5029                     if (tree_int_cst_equal (low0,
5030                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
5031                       low0 = 0;
5032                     break;
5033                   case POINTER_TYPE:
5034                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
5035                         && integer_zerop (low0))
5036                       low0 = 0;
5037                     break;
5038                   default:
5039                     break;
5040                   }
5041
5042               /* Canonicalize - [x, max] into - [x, -].  */
5043               if (high1 && TREE_CODE (high1) == INTEGER_CST)
5044                 switch (TREE_CODE (TREE_TYPE (high1)))
5045                   {
5046                   case ENUMERAL_TYPE:
5047                     if (TYPE_PRECISION (TREE_TYPE (high1))
5048                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
5049                       break;
5050                     /* FALLTHROUGH */
5051                   case INTEGER_TYPE:
5052                     if (tree_int_cst_equal (high1,
5053                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
5054                       high1 = 0;
5055                     break;
5056                   case POINTER_TYPE:
5057                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
5058                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5059                                                        high1, 1,
5060                                                        integer_one_node, 1)))
5061                       high1 = 0;
5062                     break;
5063                   default:
5064                     break;
5065                   }
5066
5067               /* The ranges might be also adjacent between the maximum and
5068                  minimum values of the given type.  For
5069                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5070                  return + [x + 1, y - 1].  */
5071               if (low0 == 0 && high1 == 0)
5072                 {
5073                   low = range_successor (high0);
5074                   high = range_predecessor (low1);
5075                   if (low == 0 || high == 0)
5076                     return 0;
5077
5078                   in_p = 1;
5079                 }
5080               else
5081                 return 0;
5082             }
5083         }
5084       else if (subset)
5085         in_p = 0, low = low0, high = high0;
5086       else
5087         in_p = 0, low = low0, high = high1;
5088     }
5089
5090   *pin_p = in_p, *plow = low, *phigh = high;
5091   return 1;
5092 }
5093 \f
5094
5095 /* Subroutine of fold, looking inside expressions of the form
5096    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5097    of the COND_EXPR.  This function is being used also to optimize
5098    A op B ? C : A, by reversing the comparison first.
5099
5100    Return a folded expression whose code is not a COND_EXPR
5101    anymore, or NULL_TREE if no folding opportunity is found.  */
5102
5103 static tree
5104 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
5105 {
5106   enum tree_code comp_code = TREE_CODE (arg0);
5107   tree arg00 = TREE_OPERAND (arg0, 0);
5108   tree arg01 = TREE_OPERAND (arg0, 1);
5109   tree arg1_type = TREE_TYPE (arg1);
5110   tree tem;
5111
5112   STRIP_NOPS (arg1);
5113   STRIP_NOPS (arg2);
5114
5115   /* If we have A op 0 ? A : -A, consider applying the following
5116      transformations:
5117
5118      A == 0? A : -A    same as -A
5119      A != 0? A : -A    same as A
5120      A >= 0? A : -A    same as abs (A)
5121      A > 0?  A : -A    same as abs (A)
5122      A <= 0? A : -A    same as -abs (A)
5123      A < 0?  A : -A    same as -abs (A)
5124
5125      None of these transformations work for modes with signed
5126      zeros.  If A is +/-0, the first two transformations will
5127      change the sign of the result (from +0 to -0, or vice
5128      versa).  The last four will fix the sign of the result,
5129      even though the original expressions could be positive or
5130      negative, depending on the sign of A.
5131
5132      Note that all these transformations are correct if A is
5133      NaN, since the two alternatives (A and -A) are also NaNs.  */
5134   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5135       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5136           ? real_zerop (arg01)
5137           : integer_zerop (arg01))
5138       && ((TREE_CODE (arg2) == NEGATE_EXPR
5139            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5140              /* In the case that A is of the form X-Y, '-A' (arg2) may
5141                 have already been folded to Y-X, check for that. */
5142           || (TREE_CODE (arg1) == MINUS_EXPR
5143               && TREE_CODE (arg2) == MINUS_EXPR
5144               && operand_equal_p (TREE_OPERAND (arg1, 0),
5145                                   TREE_OPERAND (arg2, 1), 0)
5146               && operand_equal_p (TREE_OPERAND (arg1, 1),
5147                                   TREE_OPERAND (arg2, 0), 0))))
5148     switch (comp_code)
5149       {
5150       case EQ_EXPR:
5151       case UNEQ_EXPR:
5152         tem = fold_convert (arg1_type, arg1);
5153         return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
5154       case NE_EXPR:
5155       case LTGT_EXPR:
5156         return pedantic_non_lvalue (fold_convert (type, arg1));
5157       case UNGE_EXPR:
5158       case UNGT_EXPR:
5159         if (flag_trapping_math)
5160           break;
5161         /* Fall through.  */
5162       case GE_EXPR:
5163       case GT_EXPR:
5164         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5165           arg1 = fold_convert (signed_type_for
5166                                (TREE_TYPE (arg1)), arg1);
5167         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5168         return pedantic_non_lvalue (fold_convert (type, tem));
5169       case UNLE_EXPR:
5170       case UNLT_EXPR:
5171         if (flag_trapping_math)
5172           break;
5173       case LE_EXPR:
5174       case LT_EXPR:
5175         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5176           arg1 = fold_convert (signed_type_for
5177                                (TREE_TYPE (arg1)), arg1);
5178         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5179         return negate_expr (fold_convert (type, tem));
5180       default:
5181         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5182         break;
5183       }
5184
5185   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
5186      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
5187      both transformations are correct when A is NaN: A != 0
5188      is then true, and A == 0 is false.  */
5189
5190   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5191       && integer_zerop (arg01) && integer_zerop (arg2))
5192     {
5193       if (comp_code == NE_EXPR)
5194         return pedantic_non_lvalue (fold_convert (type, arg1));
5195       else if (comp_code == EQ_EXPR)
5196         return build_int_cst (type, 0);
5197     }
5198
5199   /* Try some transformations of A op B ? A : B.
5200
5201      A == B? A : B    same as B
5202      A != B? A : B    same as A
5203      A >= B? A : B    same as max (A, B)
5204      A > B?  A : B    same as max (B, A)
5205      A <= B? A : B    same as min (A, B)
5206      A < B?  A : B    same as min (B, A)
5207
5208      As above, these transformations don't work in the presence
5209      of signed zeros.  For example, if A and B are zeros of
5210      opposite sign, the first two transformations will change
5211      the sign of the result.  In the last four, the original
5212      expressions give different results for (A=+0, B=-0) and
5213      (A=-0, B=+0), but the transformed expressions do not.
5214
5215      The first two transformations are correct if either A or B
5216      is a NaN.  In the first transformation, the condition will
5217      be false, and B will indeed be chosen.  In the case of the
5218      second transformation, the condition A != B will be true,
5219      and A will be chosen.
5220
5221      The conversions to max() and min() are not correct if B is
5222      a number and A is not.  The conditions in the original
5223      expressions will be false, so all four give B.  The min()
5224      and max() versions would give a NaN instead.  */
5225   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5226       && operand_equal_for_comparison_p (arg01, arg2, arg00)
5227       /* Avoid these transformations if the COND_EXPR may be used
5228          as an lvalue in the C++ front-end.  PR c++/19199.  */
5229       && (in_gimple_form
5230           || (strcmp (lang_hooks.name, "GNU C++") != 0
5231               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5232           || ! maybe_lvalue_p (arg1)
5233           || ! maybe_lvalue_p (arg2)))
5234     {
5235       tree comp_op0 = arg00;
5236       tree comp_op1 = arg01;
5237       tree comp_type = TREE_TYPE (comp_op0);
5238
5239       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
5240       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5241         {
5242           comp_type = type;
5243           comp_op0 = arg1;
5244           comp_op1 = arg2;
5245         }
5246
5247       switch (comp_code)
5248         {
5249         case EQ_EXPR:
5250           return pedantic_non_lvalue (fold_convert (type, arg2));
5251         case NE_EXPR:
5252           return pedantic_non_lvalue (fold_convert (type, arg1));
5253         case LE_EXPR:
5254         case LT_EXPR:
5255         case UNLE_EXPR:
5256         case UNLT_EXPR:
5257           /* In C++ a ?: expression can be an lvalue, so put the
5258              operand which will be used if they are equal first
5259              so that we can convert this back to the
5260              corresponding COND_EXPR.  */
5261           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5262             {
5263               comp_op0 = fold_convert (comp_type, comp_op0);
5264               comp_op1 = fold_convert (comp_type, comp_op1);
5265               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5266                     ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
5267                     : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
5268               return pedantic_non_lvalue (fold_convert (type, tem));
5269             }
5270           break;
5271         case GE_EXPR:
5272         case GT_EXPR:
5273         case UNGE_EXPR:
5274         case UNGT_EXPR:
5275           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5276             {
5277               comp_op0 = fold_convert (comp_type, comp_op0);
5278               comp_op1 = fold_convert (comp_type, comp_op1);
5279               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5280                     ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
5281                     : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
5282               return pedantic_non_lvalue (fold_convert (type, tem));
5283             }
5284           break;
5285         case UNEQ_EXPR:
5286           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5287             return pedantic_non_lvalue (fold_convert (type, arg2));
5288           break;
5289         case LTGT_EXPR:
5290           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5291             return pedantic_non_lvalue (fold_convert (type, arg1));
5292           break;
5293         default:
5294           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5295           break;
5296         }
5297     }
5298
5299   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5300      we might still be able to simplify this.  For example,
5301      if C1 is one less or one more than C2, this might have started
5302      out as a MIN or MAX and been transformed by this function.
5303      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
5304
5305   if (INTEGRAL_TYPE_P (type)
5306       && TREE_CODE (arg01) == INTEGER_CST
5307       && TREE_CODE (arg2) == INTEGER_CST)
5308     switch (comp_code)
5309       {
5310       case EQ_EXPR:
5311         /* We can replace A with C1 in this case.  */
5312         arg1 = fold_convert (type, arg01);
5313         return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
5314
5315       case LT_EXPR:
5316         /* If C1 is C2 + 1, this is min(A, C2).  */
5317         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5318                                OEP_ONLY_CONST)
5319             && operand_equal_p (arg01,
5320                                 const_binop (PLUS_EXPR, arg2,
5321                                              build_int_cst (type, 1), 0),
5322                                 OEP_ONLY_CONST))
5323           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5324                                                    type,
5325                                                    fold_convert (type, arg1),
5326                                                    arg2));
5327         break;
5328
5329       case LE_EXPR:
5330         /* If C1 is C2 - 1, this is min(A, C2).  */
5331         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5332                                OEP_ONLY_CONST)
5333             && operand_equal_p (arg01,
5334                                 const_binop (MINUS_EXPR, arg2,
5335                                              build_int_cst (type, 1), 0),
5336                                 OEP_ONLY_CONST))
5337           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5338                                                    type,
5339                                                    fold_convert (type, arg1),
5340                                                    arg2));
5341         break;
5342
5343       case GT_EXPR:
5344         /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5345            MAX_EXPR, to preserve the signedness of the comparison.  */
5346         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5347                                OEP_ONLY_CONST)
5348             && operand_equal_p (arg01,
5349                                 const_binop (MINUS_EXPR, arg2,
5350                                              build_int_cst (type, 1), 0),
5351                                 OEP_ONLY_CONST))
5352           return pedantic_non_lvalue (fold_convert (type,
5353                                       fold_build2 (MAX_EXPR, TREE_TYPE (arg00),
5354                                                    arg00,
5355                                                    fold_convert (TREE_TYPE (arg00),
5356                                                                  arg2))));
5357         break;
5358
5359       case GE_EXPR:
5360         /* If C1 is C2 + 1, this is max(A, C2), with the same care as above.  */
5361         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5362                                OEP_ONLY_CONST)
5363             && operand_equal_p (arg01,
5364                                 const_binop (PLUS_EXPR, arg2,
5365                                              build_int_cst (type, 1), 0),
5366                                 OEP_ONLY_CONST))
5367           return pedantic_non_lvalue (fold_convert (type,
5368                                       fold_build2 (MAX_EXPR, TREE_TYPE (arg00),
5369                                                    arg00,
5370                                                    fold_convert (TREE_TYPE (arg00),
5371                                                                  arg2))));
5372         break;
5373       case NE_EXPR:
5374         break;
5375       default:
5376         gcc_unreachable ();
5377       }
5378
5379   return NULL_TREE;
5380 }
5381
5382
5383 \f
5384 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5385 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5386   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5387                 false) >= 2)
5388 #endif
5389
5390 /* EXP is some logical combination of boolean tests.  See if we can
5391    merge it into some range test.  Return the new tree if so.  */
5392
5393 static tree
5394 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
5395 {
5396   int or_op = (code == TRUTH_ORIF_EXPR
5397                || code == TRUTH_OR_EXPR);
5398   int in0_p, in1_p, in_p;
5399   tree low0, low1, low, high0, high1, high;
5400   bool strict_overflow_p = false;
5401   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5402   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5403   tree tem;
5404   const char * const warnmsg = G_("assuming signed overflow does not occur "
5405                                   "when simplifying range test");
5406
5407   /* If this is an OR operation, invert both sides; we will invert
5408      again at the end.  */
5409   if (or_op)
5410     in0_p = ! in0_p, in1_p = ! in1_p;
5411
5412   /* If both expressions are the same, if we can merge the ranges, and we
5413      can build the range test, return it or it inverted.  If one of the
5414      ranges is always true or always false, consider it to be the same
5415      expression as the other.  */
5416   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5417       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5418                        in1_p, low1, high1)
5419       && 0 != (tem = (build_range_check (type,
5420                                          lhs != 0 ? lhs
5421                                          : rhs != 0 ? rhs : integer_zero_node,
5422                                          in_p, low, high))))
5423     {
5424       if (strict_overflow_p)
5425         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5426       return or_op ? invert_truthvalue (tem) : tem;
5427     }
5428
5429   /* On machines where the branch cost is expensive, if this is a
5430      short-circuited branch and the underlying object on both sides
5431      is the same, make a non-short-circuit operation.  */
5432   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5433            && lhs != 0 && rhs != 0
5434            && (code == TRUTH_ANDIF_EXPR
5435                || code == TRUTH_ORIF_EXPR)
5436            && operand_equal_p (lhs, rhs, 0))
5437     {
5438       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
5439          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5440          which cases we can't do this.  */
5441       if (simple_operand_p (lhs))
5442         return build2 (code == TRUTH_ANDIF_EXPR
5443                        ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5444                        type, op0, op1);
5445
5446       else if (lang_hooks.decls.global_bindings_p () == 0
5447                && ! CONTAINS_PLACEHOLDER_P (lhs))
5448         {
5449           tree common = save_expr (lhs);
5450
5451           if (0 != (lhs = build_range_check (type, common,
5452                                              or_op ? ! in0_p : in0_p,
5453                                              low0, high0))
5454               && (0 != (rhs = build_range_check (type, common,
5455                                                  or_op ? ! in1_p : in1_p,
5456                                                  low1, high1))))
5457             {
5458               if (strict_overflow_p)
5459                 fold_overflow_warning (warnmsg,
5460                                        WARN_STRICT_OVERFLOW_COMPARISON);
5461               return build2 (code == TRUTH_ANDIF_EXPR
5462                              ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5463                              type, lhs, rhs);
5464             }
5465         }
5466     }
5467
5468   return 0;
5469 }
5470 \f
5471 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5472    bit value.  Arrange things so the extra bits will be set to zero if and
5473    only if C is signed-extended to its full width.  If MASK is nonzero,
5474    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
5475
5476 static tree
5477 unextend (tree c, int p, int unsignedp, tree mask)
5478 {
5479   tree type = TREE_TYPE (c);
5480   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5481   tree temp;
5482
5483   if (p == modesize || unsignedp)
5484     return c;
5485
5486   /* We work by getting just the sign bit into the low-order bit, then
5487      into the high-order bit, then sign-extend.  We then XOR that value
5488      with C.  */
5489   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5490   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5491
5492   /* We must use a signed type in order to get an arithmetic right shift.
5493      However, we must also avoid introducing accidental overflows, so that
5494      a subsequent call to integer_zerop will work.  Hence we must
5495      do the type conversion here.  At this point, the constant is either
5496      zero or one, and the conversion to a signed type can never overflow.
5497      We could get an overflow if this conversion is done anywhere else.  */
5498   if (TYPE_UNSIGNED (type))
5499     temp = fold_convert (signed_type_for (type), temp);
5500
5501   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5502   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5503   if (mask != 0)
5504     temp = const_binop (BIT_AND_EXPR, temp,
5505                         fold_convert (TREE_TYPE (c), mask), 0);
5506   /* If necessary, convert the type back to match the type of C.  */
5507   if (TYPE_UNSIGNED (type))
5508     temp = fold_convert (type, temp);
5509
5510   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
5511 }
5512 \f
5513 /* Find ways of folding logical expressions of LHS and RHS:
5514    Try to merge two comparisons to the same innermost item.
5515    Look for range tests like "ch >= '0' && ch <= '9'".
5516    Look for combinations of simple terms on machines with expensive branches
5517    and evaluate the RHS unconditionally.
5518
5519    For example, if we have p->a == 2 && p->b == 4 and we can make an
5520    object large enough to span both A and B, we can do this with a comparison
5521    against the object ANDed with the a mask.
5522
5523    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5524    operations to do this with one comparison.
5525
5526    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5527    function and the one above.
5528
5529    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5530    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5531
5532    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5533    two operands.
5534
5535    We return the simplified tree or 0 if no optimization is possible.  */
5536
5537 static tree
5538 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
5539 {
5540   /* If this is the "or" of two comparisons, we can do something if
5541      the comparisons are NE_EXPR.  If this is the "and", we can do something
5542      if the comparisons are EQ_EXPR.  I.e.,
5543         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5544
5545      WANTED_CODE is this operation code.  For single bit fields, we can
5546      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5547      comparison for one-bit fields.  */
5548
5549   enum tree_code wanted_code;
5550   enum tree_code lcode, rcode;
5551   tree ll_arg, lr_arg, rl_arg, rr_arg;
5552   tree ll_inner, lr_inner, rl_inner, rr_inner;
5553   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5554   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5555   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5556   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5557   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5558   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5559   enum machine_mode lnmode, rnmode;
5560   tree ll_mask, lr_mask, rl_mask, rr_mask;
5561   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5562   tree l_const, r_const;
5563   tree lntype, rntype, result;
5564   HOST_WIDE_INT first_bit, end_bit;
5565   int volatilep;
5566   tree orig_lhs = lhs, orig_rhs = rhs;
5567   enum tree_code orig_code = code;
5568
5569   /* Start by getting the comparison codes.  Fail if anything is volatile.
5570      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5571      it were surrounded with a NE_EXPR.  */
5572
5573   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5574     return 0;
5575
5576   lcode = TREE_CODE (lhs);
5577   rcode = TREE_CODE (rhs);
5578
5579   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5580     {
5581       lhs = build2 (NE_EXPR, truth_type, lhs,
5582                     build_int_cst (TREE_TYPE (lhs), 0));
5583       lcode = NE_EXPR;
5584     }
5585
5586   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5587     {
5588       rhs = build2 (NE_EXPR, truth_type, rhs,
5589                     build_int_cst (TREE_TYPE (rhs), 0));
5590       rcode = NE_EXPR;
5591     }
5592
5593   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5594       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5595     return 0;
5596
5597   ll_arg = TREE_OPERAND (lhs, 0);
5598   lr_arg = TREE_OPERAND (lhs, 1);
5599   rl_arg = TREE_OPERAND (rhs, 0);
5600   rr_arg = TREE_OPERAND (rhs, 1);
5601
5602   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5603   if (simple_operand_p (ll_arg)
5604       && simple_operand_p (lr_arg))
5605     {
5606       tree result;
5607       if (operand_equal_p (ll_arg, rl_arg, 0)
5608           && operand_equal_p (lr_arg, rr_arg, 0))
5609         {
5610           result = combine_comparisons (code, lcode, rcode,
5611                                         truth_type, ll_arg, lr_arg);
5612           if (result)
5613             return result;
5614         }
5615       else if (operand_equal_p (ll_arg, rr_arg, 0)
5616                && operand_equal_p (lr_arg, rl_arg, 0))
5617         {
5618           result = combine_comparisons (code, lcode,
5619                                         swap_tree_comparison (rcode),
5620                                         truth_type, ll_arg, lr_arg);
5621           if (result)
5622             return result;
5623         }
5624     }
5625
5626   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5627           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5628
5629   /* If the RHS can be evaluated unconditionally and its operands are
5630      simple, it wins to evaluate the RHS unconditionally on machines
5631      with expensive branches.  In this case, this isn't a comparison
5632      that can be merged.  Avoid doing this if the RHS is a floating-point
5633      comparison since those can trap.  */
5634
5635   if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5636                    false) >= 2
5637       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5638       && simple_operand_p (rl_arg)
5639       && simple_operand_p (rr_arg))
5640     {
5641       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5642       if (code == TRUTH_OR_EXPR
5643           && lcode == NE_EXPR && integer_zerop (lr_arg)
5644           && rcode == NE_EXPR && integer_zerop (rr_arg)
5645           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5646           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5647         return build2 (NE_EXPR, truth_type,
5648                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5649                                ll_arg, rl_arg),
5650                        build_int_cst (TREE_TYPE (ll_arg), 0));
5651
5652       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5653       if (code == TRUTH_AND_EXPR
5654           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5655           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5656           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5657           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5658         return build2 (EQ_EXPR, truth_type,
5659                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5660                                ll_arg, rl_arg),
5661                        build_int_cst (TREE_TYPE (ll_arg), 0));
5662
5663       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5664         {
5665           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5666             return build2 (code, truth_type, lhs, rhs);
5667           return NULL_TREE;
5668         }
5669     }
5670
5671   /* See if the comparisons can be merged.  Then get all the parameters for
5672      each side.  */
5673
5674   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5675       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5676     return 0;
5677
5678   volatilep = 0;
5679   ll_inner = decode_field_reference (ll_arg,
5680                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5681                                      &ll_unsignedp, &volatilep, &ll_mask,
5682                                      &ll_and_mask);
5683   lr_inner = decode_field_reference (lr_arg,
5684                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5685                                      &lr_unsignedp, &volatilep, &lr_mask,
5686                                      &lr_and_mask);
5687   rl_inner = decode_field_reference (rl_arg,
5688                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5689                                      &rl_unsignedp, &volatilep, &rl_mask,
5690                                      &rl_and_mask);
5691   rr_inner = decode_field_reference (rr_arg,
5692                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5693                                      &rr_unsignedp, &volatilep, &rr_mask,
5694                                      &rr_and_mask);
5695
5696   /* It must be true that the inner operation on the lhs of each
5697      comparison must be the same if we are to be able to do anything.
5698      Then see if we have constants.  If not, the same must be true for
5699      the rhs's.  */
5700   if (volatilep || ll_inner == 0 || rl_inner == 0
5701       || ! operand_equal_p (ll_inner, rl_inner, 0))
5702     return 0;
5703
5704   if (TREE_CODE (lr_arg) == INTEGER_CST
5705       && TREE_CODE (rr_arg) == INTEGER_CST)
5706     l_const = lr_arg, r_const = rr_arg;
5707   else if (lr_inner == 0 || rr_inner == 0
5708            || ! operand_equal_p (lr_inner, rr_inner, 0))
5709     return 0;
5710   else
5711     l_const = r_const = 0;
5712
5713   /* If either comparison code is not correct for our logical operation,
5714      fail.  However, we can convert a one-bit comparison against zero into
5715      the opposite comparison against that bit being set in the field.  */
5716
5717   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5718   if (lcode != wanted_code)
5719     {
5720       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5721         {
5722           /* Make the left operand unsigned, since we are only interested
5723              in the value of one bit.  Otherwise we are doing the wrong
5724              thing below.  */
5725           ll_unsignedp = 1;
5726           l_const = ll_mask;
5727         }
5728       else
5729         return 0;
5730     }
5731
5732   /* This is analogous to the code for l_const above.  */
5733   if (rcode != wanted_code)
5734     {
5735       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5736         {
5737           rl_unsignedp = 1;
5738           r_const = rl_mask;
5739         }
5740       else
5741         return 0;
5742     }
5743
5744   /* See if we can find a mode that contains both fields being compared on
5745      the left.  If we can't, fail.  Otherwise, update all constants and masks
5746      to be relative to a field of that size.  */
5747   first_bit = MIN (ll_bitpos, rl_bitpos);
5748   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5749   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5750                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5751                           volatilep);
5752   if (lnmode == VOIDmode)
5753     return 0;
5754
5755   lnbitsize = GET_MODE_BITSIZE (lnmode);
5756   lnbitpos = first_bit & ~ (lnbitsize - 1);
5757   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5758   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5759
5760   if (BYTES_BIG_ENDIAN)
5761     {
5762       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5763       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5764     }
5765
5766   ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5767                          size_int (xll_bitpos), 0);
5768   rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5769                          size_int (xrl_bitpos), 0);
5770
5771   if (l_const)
5772     {
5773       l_const = fold_convert (lntype, l_const);
5774       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5775       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5776       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5777                                         fold_build1 (BIT_NOT_EXPR,
5778                                                      lntype, ll_mask),
5779                                         0)))
5780         {
5781           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5782
5783           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5784         }
5785     }
5786   if (r_const)
5787     {
5788       r_const = fold_convert (lntype, r_const);
5789       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5790       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5791       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5792                                         fold_build1 (BIT_NOT_EXPR,
5793                                                      lntype, rl_mask),
5794                                         0)))
5795         {
5796           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5797
5798           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5799         }
5800     }
5801
5802   /* If the right sides are not constant, do the same for it.  Also,
5803      disallow this optimization if a size or signedness mismatch occurs
5804      between the left and right sides.  */
5805   if (l_const == 0)
5806     {
5807       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5808           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5809           /* Make sure the two fields on the right
5810              correspond to the left without being swapped.  */
5811           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5812         return 0;
5813
5814       first_bit = MIN (lr_bitpos, rr_bitpos);
5815       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5816       rnmode = get_best_mode (end_bit - first_bit, first_bit,
5817                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5818                               volatilep);
5819       if (rnmode == VOIDmode)
5820         return 0;
5821
5822       rnbitsize = GET_MODE_BITSIZE (rnmode);
5823       rnbitpos = first_bit & ~ (rnbitsize - 1);
5824       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5825       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5826
5827       if (BYTES_BIG_ENDIAN)
5828         {
5829           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5830           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5831         }
5832
5833       lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
5834                              size_int (xlr_bitpos), 0);
5835       rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
5836                              size_int (xrr_bitpos), 0);
5837
5838       /* Make a mask that corresponds to both fields being compared.
5839          Do this for both items being compared.  If the operands are the
5840          same size and the bits being compared are in the same position
5841          then we can do this by masking both and comparing the masked
5842          results.  */
5843       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5844       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
5845       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5846         {
5847           lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5848                                     ll_unsignedp || rl_unsignedp);
5849           if (! all_ones_mask_p (ll_mask, lnbitsize))
5850             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5851
5852           rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
5853                                     lr_unsignedp || rr_unsignedp);
5854           if (! all_ones_mask_p (lr_mask, rnbitsize))
5855             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5856
5857           return build2 (wanted_code, truth_type, lhs, rhs);
5858         }
5859
5860       /* There is still another way we can do something:  If both pairs of
5861          fields being compared are adjacent, we may be able to make a wider
5862          field containing them both.
5863
5864          Note that we still must mask the lhs/rhs expressions.  Furthermore,
5865          the mask must be shifted to account for the shift done by
5866          make_bit_field_ref.  */
5867       if ((ll_bitsize + ll_bitpos == rl_bitpos
5868            && lr_bitsize + lr_bitpos == rr_bitpos)
5869           || (ll_bitpos == rl_bitpos + rl_bitsize
5870               && lr_bitpos == rr_bitpos + rr_bitsize))
5871         {
5872           tree type;
5873
5874           lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
5875                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5876           rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
5877                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5878
5879           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5880                                  size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
5881           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5882                                  size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
5883
5884           /* Convert to the smaller type before masking out unwanted bits.  */
5885           type = lntype;
5886           if (lntype != rntype)
5887             {
5888               if (lnbitsize > rnbitsize)
5889                 {
5890                   lhs = fold_convert (rntype, lhs);
5891                   ll_mask = fold_convert (rntype, ll_mask);
5892                   type = rntype;
5893                 }
5894               else if (lnbitsize < rnbitsize)
5895                 {
5896                   rhs = fold_convert (lntype, rhs);
5897                   lr_mask = fold_convert (lntype, lr_mask);
5898                   type = lntype;
5899                 }
5900             }
5901
5902           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5903             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5904
5905           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5906             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5907
5908           return build2 (wanted_code, truth_type, lhs, rhs);
5909         }
5910
5911       return 0;
5912     }
5913
5914   /* Handle the case of comparisons with constants.  If there is something in
5915      common between the masks, those bits of the constants must be the same.
5916      If not, the condition is always false.  Test for this to avoid generating
5917      incorrect code below.  */
5918   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5919   if (! integer_zerop (result)
5920       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5921                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5922     {
5923       if (wanted_code == NE_EXPR)
5924         {
5925           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5926           return constant_boolean_node (true, truth_type);
5927         }
5928       else
5929         {
5930           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5931           return constant_boolean_node (false, truth_type);
5932         }
5933     }
5934
5935   /* Construct the expression we will return.  First get the component
5936      reference we will make.  Unless the mask is all ones the width of
5937      that field, perform the mask operation.  Then compare with the
5938      merged constant.  */
5939   result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5940                                ll_unsignedp || rl_unsignedp);
5941
5942   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5943   if (! all_ones_mask_p (ll_mask, lnbitsize))
5944     result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5945
5946   return build2 (wanted_code, truth_type, result,
5947                  const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
5948 }
5949 \f
5950 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5951    constant.  */
5952
5953 static tree
5954 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5955 {
5956   tree arg0 = op0;
5957   enum tree_code op_code;
5958   tree comp_const;
5959   tree minmax_const;
5960   int consts_equal, consts_lt;
5961   tree inner;
5962
5963   STRIP_SIGN_NOPS (arg0);
5964
5965   op_code = TREE_CODE (arg0);
5966   minmax_const = TREE_OPERAND (arg0, 1);
5967   comp_const = fold_convert (TREE_TYPE (arg0), op1);
5968   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5969   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5970   inner = TREE_OPERAND (arg0, 0);
5971
5972   /* If something does not permit us to optimize, return the original tree.  */
5973   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5974       || TREE_CODE (comp_const) != INTEGER_CST
5975       || TREE_OVERFLOW (comp_const)
5976       || TREE_CODE (minmax_const) != INTEGER_CST
5977       || TREE_OVERFLOW (minmax_const))
5978     return NULL_TREE;
5979
5980   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5981      and GT_EXPR, doing the rest with recursive calls using logical
5982      simplifications.  */
5983   switch (code)
5984     {
5985     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5986       {
5987         tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5988                                           type, op0, op1);
5989         if (tem)
5990           return invert_truthvalue (tem);
5991         return NULL_TREE;
5992       }
5993
5994     case GE_EXPR:
5995       return
5996         fold_build2 (TRUTH_ORIF_EXPR, type,
5997                      optimize_minmax_comparison
5998                      (EQ_EXPR, type, arg0, comp_const),
5999                      optimize_minmax_comparison
6000                      (GT_EXPR, type, arg0, comp_const));
6001
6002     case EQ_EXPR:
6003       if (op_code == MAX_EXPR && consts_equal)
6004         /* MAX (X, 0) == 0  ->  X <= 0  */
6005         return fold_build2 (LE_EXPR, type, inner, comp_const);
6006
6007       else if (op_code == MAX_EXPR && consts_lt)
6008         /* MAX (X, 0) == 5  ->  X == 5   */
6009         return fold_build2 (EQ_EXPR, type, inner, comp_const);
6010
6011       else if (op_code == MAX_EXPR)
6012         /* MAX (X, 0) == -1  ->  false  */
6013         return omit_one_operand (type, integer_zero_node, inner);
6014
6015       else if (consts_equal)
6016         /* MIN (X, 0) == 0  ->  X >= 0  */
6017         return fold_build2 (GE_EXPR, type, inner, comp_const);
6018
6019       else if (consts_lt)
6020         /* MIN (X, 0) == 5  ->  false  */
6021         return omit_one_operand (type, integer_zero_node, inner);
6022
6023       else
6024         /* MIN (X, 0) == -1  ->  X == -1  */
6025         return fold_build2 (EQ_EXPR, type, inner, comp_const);
6026
6027     case GT_EXPR:
6028       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
6029         /* MAX (X, 0) > 0  ->  X > 0
6030            MAX (X, 0) > 5  ->  X > 5  */
6031         return fold_build2 (GT_EXPR, type, inner, comp_const);
6032
6033       else if (op_code == MAX_EXPR)
6034         /* MAX (X, 0) > -1  ->  true  */
6035         return omit_one_operand (type, integer_one_node, inner);
6036
6037       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
6038         /* MIN (X, 0) > 0  ->  false
6039            MIN (X, 0) > 5  ->  false  */
6040         return omit_one_operand (type, integer_zero_node, inner);
6041
6042       else
6043         /* MIN (X, 0) > -1  ->  X > -1  */
6044         return fold_build2 (GT_EXPR, type, inner, comp_const);
6045
6046     default:
6047       return NULL_TREE;
6048     }
6049 }
6050 \f
6051 /* T is an integer expression that is being multiplied, divided, or taken a
6052    modulus (CODE says which and what kind of divide or modulus) by a
6053    constant C.  See if we can eliminate that operation by folding it with
6054    other operations already in T.  WIDE_TYPE, if non-null, is a type that
6055    should be used for the computation if wider than our type.
6056
6057    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6058    (X * 2) + (Y * 4).  We must, however, be assured that either the original
6059    expression would not overflow or that overflow is undefined for the type
6060    in the language in question.
6061
6062    If we return a non-null expression, it is an equivalent form of the
6063    original computation, but need not be in the original type.
6064
6065    We set *STRICT_OVERFLOW_P to true if the return values depends on
6066    signed overflow being undefined.  Otherwise we do not change
6067    *STRICT_OVERFLOW_P.  */
6068
6069 static tree
6070 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6071                 bool *strict_overflow_p)
6072 {
6073   /* To avoid exponential search depth, refuse to allow recursion past
6074      three levels.  Beyond that (1) it's highly unlikely that we'll find
6075      something interesting and (2) we've probably processed it before
6076      when we built the inner expression.  */
6077
6078   static int depth;
6079   tree ret;
6080
6081   if (depth > 3)
6082     return NULL;
6083
6084   depth++;
6085   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6086   depth--;
6087
6088   return ret;
6089 }
6090
6091 static tree
6092 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6093                   bool *strict_overflow_p)
6094 {
6095   tree type = TREE_TYPE (t);
6096   enum tree_code tcode = TREE_CODE (t);
6097   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6098                                    > GET_MODE_SIZE (TYPE_MODE (type)))
6099                 ? wide_type : type);
6100   tree t1, t2;
6101   int same_p = tcode == code;
6102   tree op0 = NULL_TREE, op1 = NULL_TREE;
6103   bool sub_strict_overflow_p;
6104
6105   /* Don't deal with constants of zero here; they confuse the code below.  */
6106   if (integer_zerop (c))
6107     return NULL_TREE;
6108
6109   if (TREE_CODE_CLASS (tcode) == tcc_unary)
6110     op0 = TREE_OPERAND (t, 0);
6111
6112   if (TREE_CODE_CLASS (tcode) == tcc_binary)
6113     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6114
6115   /* Note that we need not handle conditional operations here since fold
6116      already handles those cases.  So just do arithmetic here.  */
6117   switch (tcode)
6118     {
6119     case INTEGER_CST:
6120       /* For a constant, we can always simplify if we are a multiply
6121          or (for divide and modulus) if it is a multiple of our constant.  */
6122       if (code == MULT_EXPR
6123           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
6124         return const_binop (code, fold_convert (ctype, t),
6125                             fold_convert (ctype, c), 0);
6126       break;
6127
6128     CASE_CONVERT: case NON_LVALUE_EXPR:
6129       /* If op0 is an expression ...  */
6130       if ((COMPARISON_CLASS_P (op0)
6131            || UNARY_CLASS_P (op0)
6132            || BINARY_CLASS_P (op0)
6133            || VL_EXP_CLASS_P (op0)
6134            || EXPRESSION_CLASS_P (op0))
6135           /* ... and has wrapping overflow, and its type is smaller
6136              than ctype, then we cannot pass through as widening.  */
6137           && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6138                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
6139                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
6140                && (TYPE_PRECISION (ctype)
6141                    > TYPE_PRECISION (TREE_TYPE (op0))))
6142               /* ... or this is a truncation (t is narrower than op0),
6143                  then we cannot pass through this narrowing.  */
6144               || (TYPE_PRECISION (type)
6145                   < TYPE_PRECISION (TREE_TYPE (op0)))
6146               /* ... or signedness changes for division or modulus,
6147                  then we cannot pass through this conversion.  */
6148               || (code != MULT_EXPR
6149                   && (TYPE_UNSIGNED (ctype)
6150                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
6151               /* ... or has undefined overflow while the converted to
6152                  type has not, we cannot do the operation in the inner type
6153                  as that would introduce undefined overflow.  */
6154               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6155                   && !TYPE_OVERFLOW_UNDEFINED (type))))
6156         break;
6157
6158       /* Pass the constant down and see if we can make a simplification.  If
6159          we can, replace this expression with the inner simplification for
6160          possible later conversion to our or some other type.  */
6161       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6162           && TREE_CODE (t2) == INTEGER_CST
6163           && !TREE_OVERFLOW (t2)
6164           && (0 != (t1 = extract_muldiv (op0, t2, code,
6165                                          code == MULT_EXPR
6166                                          ? ctype : NULL_TREE,
6167                                          strict_overflow_p))))
6168         return t1;
6169       break;
6170
6171     case ABS_EXPR:
6172       /* If widening the type changes it from signed to unsigned, then we
6173          must avoid building ABS_EXPR itself as unsigned.  */
6174       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6175         {
6176           tree cstype = (*signed_type_for) (ctype);
6177           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6178               != 0)
6179             {
6180               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6181               return fold_convert (ctype, t1);
6182             }
6183           break;
6184         }
6185       /* If the constant is negative, we cannot simplify this.  */
6186       if (tree_int_cst_sgn (c) == -1)
6187         break;
6188       /* FALLTHROUGH */
6189     case NEGATE_EXPR:
6190       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6191           != 0)
6192         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6193       break;
6194
6195     case MIN_EXPR:  case MAX_EXPR:
6196       /* If widening the type changes the signedness, then we can't perform
6197          this optimization as that changes the result.  */
6198       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6199         break;
6200
6201       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
6202       sub_strict_overflow_p = false;
6203       if ((t1 = extract_muldiv (op0, c, code, wide_type,
6204                                 &sub_strict_overflow_p)) != 0
6205           && (t2 = extract_muldiv (op1, c, code, wide_type,
6206                                    &sub_strict_overflow_p)) != 0)
6207         {
6208           if (tree_int_cst_sgn (c) < 0)
6209             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6210           if (sub_strict_overflow_p)
6211             *strict_overflow_p = true;
6212           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6213                               fold_convert (ctype, t2));
6214         }
6215       break;
6216
6217     case LSHIFT_EXPR:  case RSHIFT_EXPR:
6218       /* If the second operand is constant, this is a multiplication
6219          or floor division, by a power of two, so we can treat it that
6220          way unless the multiplier or divisor overflows.  Signed
6221          left-shift overflow is implementation-defined rather than
6222          undefined in C90, so do not convert signed left shift into
6223          multiplication.  */
6224       if (TREE_CODE (op1) == INTEGER_CST
6225           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6226           /* const_binop may not detect overflow correctly,
6227              so check for it explicitly here.  */
6228           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
6229           && TREE_INT_CST_HIGH (op1) == 0
6230           && 0 != (t1 = fold_convert (ctype,
6231                                       const_binop (LSHIFT_EXPR,
6232                                                    size_one_node,
6233                                                    op1, 0)))
6234           && !TREE_OVERFLOW (t1))
6235         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6236                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
6237                                        ctype, fold_convert (ctype, op0), t1),
6238                                c, code, wide_type, strict_overflow_p);
6239       break;
6240
6241     case PLUS_EXPR:  case MINUS_EXPR:
6242       /* See if we can eliminate the operation on both sides.  If we can, we
6243          can return a new PLUS or MINUS.  If we can't, the only remaining
6244          cases where we can do anything are if the second operand is a
6245          constant.  */
6246       sub_strict_overflow_p = false;
6247       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6248       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6249       if (t1 != 0 && t2 != 0
6250           && (code == MULT_EXPR
6251               /* If not multiplication, we can only do this if both operands
6252                  are divisible by c.  */
6253               || (multiple_of_p (ctype, op0, c)
6254                   && multiple_of_p (ctype, op1, c))))
6255         {
6256           if (sub_strict_overflow_p)
6257             *strict_overflow_p = true;
6258           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6259                               fold_convert (ctype, t2));
6260         }
6261
6262       /* If this was a subtraction, negate OP1 and set it to be an addition.
6263          This simplifies the logic below.  */
6264       if (tcode == MINUS_EXPR)
6265         tcode = PLUS_EXPR, op1 = negate_expr (op1);
6266
6267       if (TREE_CODE (op1) != INTEGER_CST)
6268         break;
6269
6270       /* If either OP1 or C are negative, this optimization is not safe for
6271          some of the division and remainder types while for others we need
6272          to change the code.  */
6273       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6274         {
6275           if (code == CEIL_DIV_EXPR)
6276             code = FLOOR_DIV_EXPR;
6277           else if (code == FLOOR_DIV_EXPR)
6278             code = CEIL_DIV_EXPR;
6279           else if (code != MULT_EXPR
6280                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6281             break;
6282         }
6283
6284       /* If it's a multiply or a division/modulus operation of a multiple
6285          of our constant, do the operation and verify it doesn't overflow.  */
6286       if (code == MULT_EXPR
6287           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6288         {
6289           op1 = const_binop (code, fold_convert (ctype, op1),
6290                              fold_convert (ctype, c), 0);
6291           /* We allow the constant to overflow with wrapping semantics.  */
6292           if (op1 == 0
6293               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6294             break;
6295         }
6296       else
6297         break;
6298
6299       /* If we have an unsigned type is not a sizetype, we cannot widen
6300          the operation since it will change the result if the original
6301          computation overflowed.  */
6302       if (TYPE_UNSIGNED (ctype)
6303           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
6304           && ctype != type)
6305         break;
6306
6307       /* If we were able to eliminate our operation from the first side,
6308          apply our operation to the second side and reform the PLUS.  */
6309       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6310         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6311
6312       /* The last case is if we are a multiply.  In that case, we can
6313          apply the distributive law to commute the multiply and addition
6314          if the multiplication of the constants doesn't overflow.  */
6315       if (code == MULT_EXPR)
6316         return fold_build2 (tcode, ctype,
6317                             fold_build2 (code, ctype,
6318                                          fold_convert (ctype, op0),
6319                                          fold_convert (ctype, c)),
6320                             op1);
6321
6322       break;
6323
6324     case MULT_EXPR:
6325       /* We have a special case here if we are doing something like
6326          (C * 8) % 4 since we know that's zero.  */
6327       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6328            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6329           /* If the multiplication can overflow we cannot optimize this.
6330              ???  Until we can properly mark individual operations as
6331              not overflowing we need to treat sizetype special here as
6332              stor-layout relies on this opimization to make
6333              DECL_FIELD_BIT_OFFSET always a constant.  */
6334           && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6335               || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
6336                   && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
6337           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6338           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6339         {
6340           *strict_overflow_p = true;
6341           return omit_one_operand (type, integer_zero_node, op0);
6342         }
6343
6344       /* ... fall through ...  */
6345
6346     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
6347     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
6348       /* If we can extract our operation from the LHS, do so and return a
6349          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
6350          do something only if the second operand is a constant.  */
6351       if (same_p
6352           && (t1 = extract_muldiv (op0, c, code, wide_type,
6353                                    strict_overflow_p)) != 0)
6354         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6355                             fold_convert (ctype, op1));
6356       else if (tcode == MULT_EXPR && code == MULT_EXPR
6357                && (t1 = extract_muldiv (op1, c, code, wide_type,
6358                                         strict_overflow_p)) != 0)
6359         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6360                             fold_convert (ctype, t1));
6361       else if (TREE_CODE (op1) != INTEGER_CST)
6362         return 0;
6363
6364       /* If these are the same operation types, we can associate them
6365          assuming no overflow.  */
6366       if (tcode == code
6367           && 0 != (t1 = int_const_binop (MULT_EXPR, fold_convert (ctype, op1),
6368                                          fold_convert (ctype, c), 1))
6369           && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
6370                                                TREE_INT_CST_HIGH (t1),
6371                                                (TYPE_UNSIGNED (ctype)
6372                                                 && tcode != MULT_EXPR) ? -1 : 1,
6373                                                TREE_OVERFLOW (t1)))
6374           && !TREE_OVERFLOW (t1))
6375         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
6376
6377       /* If these operations "cancel" each other, we have the main
6378          optimizations of this pass, which occur when either constant is a
6379          multiple of the other, in which case we replace this with either an
6380          operation or CODE or TCODE.
6381
6382          If we have an unsigned type that is not a sizetype, we cannot do
6383          this since it will change the result if the original computation
6384          overflowed.  */
6385       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
6386            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
6387           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6388               || (tcode == MULT_EXPR
6389                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6390                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6391                   && code != MULT_EXPR)))
6392         {
6393           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6394             {
6395               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6396                 *strict_overflow_p = true;
6397               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6398                                   fold_convert (ctype,
6399                                                 const_binop (TRUNC_DIV_EXPR,
6400                                                              op1, c, 0)));
6401             }
6402           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6403             {
6404               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6405                 *strict_overflow_p = true;
6406               return fold_build2 (code, ctype, fold_convert (ctype, op0),
6407                                   fold_convert (ctype,
6408                                                 const_binop (TRUNC_DIV_EXPR,
6409                                                              c, op1, 0)));
6410             }
6411         }
6412       break;
6413
6414     default:
6415       break;
6416     }
6417
6418   return 0;
6419 }
6420 \f
6421 /* Return a node which has the indicated constant VALUE (either 0 or
6422    1), and is of the indicated TYPE.  */
6423
6424 tree
6425 constant_boolean_node (int value, tree type)
6426 {
6427   if (type == integer_type_node)
6428     return value ? integer_one_node : integer_zero_node;
6429   else if (type == boolean_type_node)
6430     return value ? boolean_true_node : boolean_false_node;
6431   else
6432     return build_int_cst (type, value);
6433 }
6434
6435
6436 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6437    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
6438    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6439    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
6440    COND is the first argument to CODE; otherwise (as in the example
6441    given here), it is the second argument.  TYPE is the type of the
6442    original expression.  Return NULL_TREE if no simplification is
6443    possible.  */
6444
6445 static tree
6446 fold_binary_op_with_conditional_arg (enum tree_code code,
6447                                      tree type, tree op0, tree op1,
6448                                      tree cond, tree arg, int cond_first_p)
6449 {
6450   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6451   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6452   tree test, true_value, false_value;
6453   tree lhs = NULL_TREE;
6454   tree rhs = NULL_TREE;
6455
6456   /* This transformation is only worthwhile if we don't have to wrap
6457      arg in a SAVE_EXPR, and the operation can be simplified on at least
6458      one of the branches once its pushed inside the COND_EXPR.  */
6459   if (!TREE_CONSTANT (arg))
6460     return NULL_TREE;
6461
6462   if (TREE_CODE (cond) == COND_EXPR)
6463     {
6464       test = TREE_OPERAND (cond, 0);
6465       true_value = TREE_OPERAND (cond, 1);
6466       false_value = TREE_OPERAND (cond, 2);
6467       /* If this operand throws an expression, then it does not make
6468          sense to try to perform a logical or arithmetic operation
6469          involving it.  */
6470       if (VOID_TYPE_P (TREE_TYPE (true_value)))
6471         lhs = true_value;
6472       if (VOID_TYPE_P (TREE_TYPE (false_value)))
6473         rhs = false_value;
6474     }
6475   else
6476     {
6477       tree testtype = TREE_TYPE (cond);
6478       test = cond;
6479       true_value = constant_boolean_node (true, testtype);
6480       false_value = constant_boolean_node (false, testtype);
6481     }
6482
6483   arg = fold_convert (arg_type, arg);
6484   if (lhs == 0)
6485     {
6486       true_value = fold_convert (cond_type, true_value);
6487       if (cond_first_p)
6488         lhs = fold_build2 (code, type, true_value, arg);
6489       else
6490         lhs = fold_build2 (code, type, arg, true_value);
6491     }
6492   if (rhs == 0)
6493     {
6494       false_value = fold_convert (cond_type, false_value);
6495       if (cond_first_p)
6496         rhs = fold_build2 (code, type, false_value, arg);
6497       else
6498         rhs = fold_build2 (code, type, arg, false_value);
6499     }
6500
6501   test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
6502   return fold_convert (type, test);
6503 }
6504
6505 \f
6506 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6507
6508    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6509    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6510    ADDEND is the same as X.
6511
6512    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6513    and finite.  The problematic cases are when X is zero, and its mode
6514    has signed zeros.  In the case of rounding towards -infinity,
6515    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6516    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6517
6518 bool
6519 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6520 {
6521   if (!real_zerop (addend))
6522     return false;
6523
6524   /* Don't allow the fold with -fsignaling-nans.  */
6525   if (HONOR_SNANS (TYPE_MODE (type)))
6526     return false;
6527
6528   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6529   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6530     return true;
6531
6532   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6533   if (TREE_CODE (addend) == REAL_CST
6534       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6535     negate = !negate;
6536
6537   /* The mode has signed zeros, and we have to honor their sign.
6538      In this situation, there is only one case we can return true for.
6539      X - 0 is the same as X unless rounding towards -infinity is
6540      supported.  */
6541   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6542 }
6543
6544 /* Subroutine of fold() that checks comparisons of built-in math
6545    functions against real constants.
6546
6547    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6548    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6549    is the type of the result and ARG0 and ARG1 are the operands of the
6550    comparison.  ARG1 must be a TREE_REAL_CST.
6551
6552    The function returns the constant folded tree if a simplification
6553    can be made, and NULL_TREE otherwise.  */
6554
6555 static tree
6556 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
6557                      tree type, tree arg0, tree arg1)
6558 {
6559   REAL_VALUE_TYPE c;
6560
6561   if (BUILTIN_SQRT_P (fcode))
6562     {
6563       tree arg = CALL_EXPR_ARG (arg0, 0);
6564       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6565
6566       c = TREE_REAL_CST (arg1);
6567       if (REAL_VALUE_NEGATIVE (c))
6568         {
6569           /* sqrt(x) < y is always false, if y is negative.  */
6570           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6571             return omit_one_operand (type, integer_zero_node, arg);
6572
6573           /* sqrt(x) > y is always true, if y is negative and we
6574              don't care about NaNs, i.e. negative values of x.  */
6575           if (code == NE_EXPR || !HONOR_NANS (mode))
6576             return omit_one_operand (type, integer_one_node, arg);
6577
6578           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6579           return fold_build2 (GE_EXPR, type, arg,
6580                               build_real (TREE_TYPE (arg), dconst0));
6581         }
6582       else if (code == GT_EXPR || code == GE_EXPR)
6583         {
6584           REAL_VALUE_TYPE c2;
6585
6586           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6587           real_convert (&c2, mode, &c2);
6588
6589           if (REAL_VALUE_ISINF (c2))
6590             {
6591               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6592               if (HONOR_INFINITIES (mode))
6593                 return fold_build2 (EQ_EXPR, type, arg,
6594                                     build_real (TREE_TYPE (arg), c2));
6595
6596               /* sqrt(x) > y is always false, when y is very large
6597                  and we don't care about infinities.  */
6598               return omit_one_operand (type, integer_zero_node, arg);
6599             }
6600
6601           /* sqrt(x) > c is the same as x > c*c.  */
6602           return fold_build2 (code, type, arg,
6603                               build_real (TREE_TYPE (arg), c2));
6604         }
6605       else if (code == LT_EXPR || code == LE_EXPR)
6606         {
6607           REAL_VALUE_TYPE c2;
6608
6609           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6610           real_convert (&c2, mode, &c2);
6611
6612           if (REAL_VALUE_ISINF (c2))
6613             {
6614               /* sqrt(x) < y is always true, when y is a very large
6615                  value and we don't care about NaNs or Infinities.  */
6616               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6617                 return omit_one_operand (type, integer_one_node, arg);
6618
6619               /* sqrt(x) < y is x != +Inf when y is very large and we
6620                  don't care about NaNs.  */
6621               if (! HONOR_NANS (mode))
6622                 return fold_build2 (NE_EXPR, type, arg,
6623                                     build_real (TREE_TYPE (arg), c2));
6624
6625               /* sqrt(x) < y is x >= 0 when y is very large and we
6626                  don't care about Infinities.  */
6627               if (! HONOR_INFINITIES (mode))
6628                 return fold_build2 (GE_EXPR, type, arg,
6629                                     build_real (TREE_TYPE (arg), dconst0));
6630
6631               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6632               if (lang_hooks.decls.global_bindings_p () != 0
6633                   || CONTAINS_PLACEHOLDER_P (arg))
6634                 return NULL_TREE;
6635
6636               arg = save_expr (arg);
6637               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6638                                   fold_build2 (GE_EXPR, type, arg,
6639                                                build_real (TREE_TYPE (arg),
6640                                                            dconst0)),
6641                                   fold_build2 (NE_EXPR, type, arg,
6642                                                build_real (TREE_TYPE (arg),
6643                                                            c2)));
6644             }
6645
6646           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6647           if (! HONOR_NANS (mode))
6648             return fold_build2 (code, type, arg,
6649                                 build_real (TREE_TYPE (arg), c2));
6650
6651           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6652           if (lang_hooks.decls.global_bindings_p () == 0
6653               && ! CONTAINS_PLACEHOLDER_P (arg))
6654             {
6655               arg = save_expr (arg);
6656               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6657                                   fold_build2 (GE_EXPR, type, arg,
6658                                                build_real (TREE_TYPE (arg),
6659                                                            dconst0)),
6660                                   fold_build2 (code, type, arg,
6661                                                build_real (TREE_TYPE (arg),
6662                                                            c2)));
6663             }
6664         }
6665     }
6666
6667   return NULL_TREE;
6668 }
6669
6670 /* Subroutine of fold() that optimizes comparisons against Infinities,
6671    either +Inf or -Inf.
6672
6673    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6674    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6675    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6676
6677    The function returns the constant folded tree if a simplification
6678    can be made, and NULL_TREE otherwise.  */
6679
6680 static tree
6681 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6682 {
6683   enum machine_mode mode;
6684   REAL_VALUE_TYPE max;
6685   tree temp;
6686   bool neg;
6687
6688   mode = TYPE_MODE (TREE_TYPE (arg0));
6689
6690   /* For negative infinity swap the sense of the comparison.  */
6691   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6692   if (neg)
6693     code = swap_tree_comparison (code);
6694
6695   switch (code)
6696     {
6697     case GT_EXPR:
6698       /* x > +Inf is always false, if with ignore sNANs.  */
6699       if (HONOR_SNANS (mode))
6700         return NULL_TREE;
6701       return omit_one_operand (type, integer_zero_node, arg0);
6702
6703     case LE_EXPR:
6704       /* x <= +Inf is always true, if we don't case about NaNs.  */
6705       if (! HONOR_NANS (mode))
6706         return omit_one_operand (type, integer_one_node, arg0);
6707
6708       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6709       if (lang_hooks.decls.global_bindings_p () == 0
6710           && ! CONTAINS_PLACEHOLDER_P (arg0))
6711         {
6712           arg0 = save_expr (arg0);
6713           return fold_build2 (EQ_EXPR, type, arg0, arg0);
6714         }
6715       break;
6716
6717     case EQ_EXPR:
6718     case GE_EXPR:
6719       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6720       real_maxval (&max, neg, mode);
6721       return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6722                           arg0, build_real (TREE_TYPE (arg0), max));
6723
6724     case LT_EXPR:
6725       /* x < +Inf is always equal to x <= DBL_MAX.  */
6726       real_maxval (&max, neg, mode);
6727       return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6728                           arg0, build_real (TREE_TYPE (arg0), max));
6729
6730     case NE_EXPR:
6731       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6732       real_maxval (&max, neg, mode);
6733       if (! HONOR_NANS (mode))
6734         return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6735                             arg0, build_real (TREE_TYPE (arg0), max));
6736
6737       temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6738                           arg0, build_real (TREE_TYPE (arg0), max));
6739       return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6740
6741     default:
6742       break;
6743     }
6744
6745   return NULL_TREE;
6746 }
6747
6748 /* Subroutine of fold() that optimizes comparisons of a division by
6749    a nonzero integer constant against an integer constant, i.e.
6750    X/C1 op C2.
6751
6752    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6753    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6754    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6755
6756    The function returns the constant folded tree if a simplification
6757    can be made, and NULL_TREE otherwise.  */
6758
6759 static tree
6760 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6761 {
6762   tree prod, tmp, hi, lo;
6763   tree arg00 = TREE_OPERAND (arg0, 0);
6764   tree arg01 = TREE_OPERAND (arg0, 1);
6765   unsigned HOST_WIDE_INT lpart;
6766   HOST_WIDE_INT hpart;
6767   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6768   bool neg_overflow;
6769   int overflow;
6770
6771   /* We have to do this the hard way to detect unsigned overflow.
6772      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
6773   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6774                                    TREE_INT_CST_HIGH (arg01),
6775                                    TREE_INT_CST_LOW (arg1),
6776                                    TREE_INT_CST_HIGH (arg1),
6777                                    &lpart, &hpart, unsigned_p);
6778   prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6779                                 -1, overflow);
6780   neg_overflow = false;
6781
6782   if (unsigned_p)
6783     {
6784       tmp = int_const_binop (MINUS_EXPR, arg01,
6785                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6786       lo = prod;
6787
6788       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
6789       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6790                                        TREE_INT_CST_HIGH (prod),
6791                                        TREE_INT_CST_LOW (tmp),
6792                                        TREE_INT_CST_HIGH (tmp),
6793                                        &lpart, &hpart, unsigned_p);
6794       hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6795                                   -1, overflow | TREE_OVERFLOW (prod));
6796     }
6797   else if (tree_int_cst_sgn (arg01) >= 0)
6798     {
6799       tmp = int_const_binop (MINUS_EXPR, arg01,
6800                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6801       switch (tree_int_cst_sgn (arg1))
6802         {
6803         case -1:
6804           neg_overflow = true;
6805           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6806           hi = prod;
6807           break;
6808
6809         case  0:
6810           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6811           hi = tmp;
6812           break;
6813
6814         case  1:
6815           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6816           lo = prod;
6817           break;
6818
6819         default:
6820           gcc_unreachable ();
6821         }
6822     }
6823   else
6824     {
6825       /* A negative divisor reverses the relational operators.  */
6826       code = swap_tree_comparison (code);
6827
6828       tmp = int_const_binop (PLUS_EXPR, arg01,
6829                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6830       switch (tree_int_cst_sgn (arg1))
6831         {
6832         case -1:
6833           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6834           lo = prod;
6835           break;
6836
6837         case  0:
6838           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6839           lo = tmp;
6840           break;
6841
6842         case  1:
6843           neg_overflow = true;
6844           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6845           hi = prod;
6846           break;
6847
6848         default:
6849           gcc_unreachable ();
6850         }
6851     }
6852
6853   switch (code)
6854     {
6855     case EQ_EXPR:
6856       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6857         return omit_one_operand (type, integer_zero_node, arg00);
6858       if (TREE_OVERFLOW (hi))
6859         return fold_build2 (GE_EXPR, type, arg00, lo);
6860       if (TREE_OVERFLOW (lo))
6861         return fold_build2 (LE_EXPR, type, arg00, hi);
6862       return build_range_check (type, arg00, 1, lo, hi);
6863
6864     case NE_EXPR:
6865       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6866         return omit_one_operand (type, integer_one_node, arg00);
6867       if (TREE_OVERFLOW (hi))
6868         return fold_build2 (LT_EXPR, type, arg00, lo);
6869       if (TREE_OVERFLOW (lo))
6870         return fold_build2 (GT_EXPR, type, arg00, hi);
6871       return build_range_check (type, arg00, 0, lo, hi);
6872
6873     case LT_EXPR:
6874       if (TREE_OVERFLOW (lo))
6875         {
6876           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6877           return omit_one_operand (type, tmp, arg00);
6878         }
6879       return fold_build2 (LT_EXPR, type, arg00, lo);
6880
6881     case LE_EXPR:
6882       if (TREE_OVERFLOW (hi))
6883         {
6884           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6885           return omit_one_operand (type, tmp, arg00);
6886         }
6887       return fold_build2 (LE_EXPR, type, arg00, hi);
6888
6889     case GT_EXPR:
6890       if (TREE_OVERFLOW (hi))
6891         {
6892           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6893           return omit_one_operand (type, tmp, arg00);
6894         }
6895       return fold_build2 (GT_EXPR, type, arg00, hi);
6896
6897     case GE_EXPR:
6898       if (TREE_OVERFLOW (lo))
6899         {
6900           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6901           return omit_one_operand (type, tmp, arg00);
6902         }
6903       return fold_build2 (GE_EXPR, type, arg00, lo);
6904
6905     default:
6906       break;
6907     }
6908
6909   return NULL_TREE;
6910 }
6911
6912
6913 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6914    equality/inequality test, then return a simplified form of the test
6915    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6916    result type.  */
6917
6918 static tree
6919 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6920                                      tree result_type)
6921 {
6922   /* If this is testing a single bit, we can optimize the test.  */
6923   if ((code == NE_EXPR || code == EQ_EXPR)
6924       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6925       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6926     {
6927       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6928          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6929       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6930
6931       if (arg00 != NULL_TREE
6932           /* This is only a win if casting to a signed type is cheap,
6933              i.e. when arg00's type is not a partial mode.  */
6934           && TYPE_PRECISION (TREE_TYPE (arg00))
6935              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6936         {
6937           tree stype = signed_type_for (TREE_TYPE (arg00));
6938           return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6939                               result_type, fold_convert (stype, arg00),
6940                               build_int_cst (stype, 0));
6941         }
6942     }
6943
6944   return NULL_TREE;
6945 }
6946
6947 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6948    equality/inequality test, then return a simplified form of
6949    the test using shifts and logical operations.  Otherwise return
6950    NULL.  TYPE is the desired result type.  */
6951
6952 tree
6953 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6954                       tree result_type)
6955 {
6956   /* If this is testing a single bit, we can optimize the test.  */
6957   if ((code == NE_EXPR || code == EQ_EXPR)
6958       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6959       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6960     {
6961       tree inner = TREE_OPERAND (arg0, 0);
6962       tree type = TREE_TYPE (arg0);
6963       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6964       enum machine_mode operand_mode = TYPE_MODE (type);
6965       int ops_unsigned;
6966       tree signed_type, unsigned_type, intermediate_type;
6967       tree tem, one;
6968
6969       /* First, see if we can fold the single bit test into a sign-bit
6970          test.  */
6971       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6972                                                  result_type);
6973       if (tem)
6974         return tem;
6975
6976       /* Otherwise we have (A & C) != 0 where C is a single bit,
6977          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6978          Similarly for (A & C) == 0.  */
6979
6980       /* If INNER is a right shift of a constant and it plus BITNUM does
6981          not overflow, adjust BITNUM and INNER.  */
6982       if (TREE_CODE (inner) == RSHIFT_EXPR
6983           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6984           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6985           && bitnum < TYPE_PRECISION (type)
6986           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6987                                    bitnum - TYPE_PRECISION (type)))
6988         {
6989           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6990           inner = TREE_OPERAND (inner, 0);
6991         }
6992
6993       /* If we are going to be able to omit the AND below, we must do our
6994          operations as unsigned.  If we must use the AND, we have a choice.
6995          Normally unsigned is faster, but for some machines signed is.  */
6996 #ifdef LOAD_EXTEND_OP
6997       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND 
6998                       && !flag_syntax_only) ? 0 : 1;
6999 #else
7000       ops_unsigned = 1;
7001 #endif
7002
7003       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
7004       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
7005       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
7006       inner = fold_convert (intermediate_type, inner);
7007
7008       if (bitnum != 0)
7009         inner = build2 (RSHIFT_EXPR, intermediate_type,
7010                         inner, size_int (bitnum));
7011
7012       one = build_int_cst (intermediate_type, 1);
7013
7014       if (code == EQ_EXPR)
7015         inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
7016
7017       /* Put the AND last so it can combine with more things.  */
7018       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
7019
7020       /* Make sure to return the proper type.  */
7021       inner = fold_convert (result_type, inner);
7022
7023       return inner;
7024     }
7025   return NULL_TREE;
7026 }
7027
7028 /* Check whether we are allowed to reorder operands arg0 and arg1,
7029    such that the evaluation of arg1 occurs before arg0.  */
7030
7031 static bool
7032 reorder_operands_p (const_tree arg0, const_tree arg1)
7033 {
7034   if (! flag_evaluation_order)
7035       return true;
7036   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
7037     return true;
7038   return ! TREE_SIDE_EFFECTS (arg0)
7039          && ! TREE_SIDE_EFFECTS (arg1);
7040 }
7041
7042 /* Test whether it is preferable two swap two operands, ARG0 and
7043    ARG1, for example because ARG0 is an integer constant and ARG1
7044    isn't.  If REORDER is true, only recommend swapping if we can
7045    evaluate the operands in reverse order.  */
7046
7047 bool
7048 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
7049 {
7050   STRIP_SIGN_NOPS (arg0);
7051   STRIP_SIGN_NOPS (arg1);
7052
7053   if (TREE_CODE (arg1) == INTEGER_CST)
7054     return 0;
7055   if (TREE_CODE (arg0) == INTEGER_CST)
7056     return 1;
7057
7058   if (TREE_CODE (arg1) == REAL_CST)
7059     return 0;
7060   if (TREE_CODE (arg0) == REAL_CST)
7061     return 1;
7062
7063   if (TREE_CODE (arg1) == FIXED_CST)
7064     return 0;
7065   if (TREE_CODE (arg0) == FIXED_CST)
7066     return 1;
7067
7068   if (TREE_CODE (arg1) == COMPLEX_CST)
7069     return 0;
7070   if (TREE_CODE (arg0) == COMPLEX_CST)
7071     return 1;
7072
7073   if (TREE_CONSTANT (arg1))
7074     return 0;
7075   if (TREE_CONSTANT (arg0))
7076     return 1;
7077
7078   if (optimize_function_for_size_p (cfun))
7079     return 0;
7080
7081   if (reorder && flag_evaluation_order
7082       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
7083     return 0;
7084
7085   /* It is preferable to swap two SSA_NAME to ensure a canonical form
7086      for commutative and comparison operators.  Ensuring a canonical
7087      form allows the optimizers to find additional redundancies without
7088      having to explicitly check for both orderings.  */
7089   if (TREE_CODE (arg0) == SSA_NAME
7090       && TREE_CODE (arg1) == SSA_NAME
7091       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7092     return 1;
7093
7094   /* Put SSA_NAMEs last.  */
7095   if (TREE_CODE (arg1) == SSA_NAME)
7096     return 0;
7097   if (TREE_CODE (arg0) == SSA_NAME)
7098     return 1;
7099
7100   /* Put variables last.  */
7101   if (DECL_P (arg1))
7102     return 0;
7103   if (DECL_P (arg0))
7104     return 1;
7105
7106   return 0;
7107 }
7108
7109 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
7110    ARG0 is extended to a wider type.  */
7111
7112 static tree
7113 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
7114 {
7115   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
7116   tree arg1_unw;
7117   tree shorter_type, outer_type;
7118   tree min, max;
7119   bool above, below;
7120
7121   if (arg0_unw == arg0)
7122     return NULL_TREE;
7123   shorter_type = TREE_TYPE (arg0_unw);
7124
7125 #ifdef HAVE_canonicalize_funcptr_for_compare
7126   /* Disable this optimization if we're casting a function pointer
7127      type on targets that require function pointer canonicalization.  */
7128   if (HAVE_canonicalize_funcptr_for_compare
7129       && TREE_CODE (shorter_type) == POINTER_TYPE
7130       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
7131     return NULL_TREE;
7132 #endif
7133
7134   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
7135     return NULL_TREE;
7136
7137   arg1_unw = get_unwidened (arg1, NULL_TREE);
7138
7139   /* If possible, express the comparison in the shorter mode.  */
7140   if ((code == EQ_EXPR || code == NE_EXPR
7141        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
7142       && (TREE_TYPE (arg1_unw) == shorter_type
7143           || ((TYPE_PRECISION (shorter_type)
7144                >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
7145               && (TYPE_UNSIGNED (shorter_type)
7146                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
7147           || (TREE_CODE (arg1_unw) == INTEGER_CST
7148               && (TREE_CODE (shorter_type) == INTEGER_TYPE
7149                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
7150               && int_fits_type_p (arg1_unw, shorter_type))))
7151     return fold_build2 (code, type, arg0_unw,
7152                        fold_convert (shorter_type, arg1_unw));
7153
7154   if (TREE_CODE (arg1_unw) != INTEGER_CST
7155       || TREE_CODE (shorter_type) != INTEGER_TYPE
7156       || !int_fits_type_p (arg1_unw, shorter_type))
7157     return NULL_TREE;
7158
7159   /* If we are comparing with the integer that does not fit into the range
7160      of the shorter type, the result is known.  */
7161   outer_type = TREE_TYPE (arg1_unw);
7162   min = lower_bound_in_type (outer_type, shorter_type);
7163   max = upper_bound_in_type (outer_type, shorter_type);
7164
7165   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7166                                                    max, arg1_unw));
7167   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7168                                                    arg1_unw, min));
7169
7170   switch (code)
7171     {
7172     case EQ_EXPR:
7173       if (above || below)
7174         return omit_one_operand (type, integer_zero_node, arg0);
7175       break;
7176
7177     case NE_EXPR:
7178       if (above || below)
7179         return omit_one_operand (type, integer_one_node, arg0);
7180       break;
7181
7182     case LT_EXPR:
7183     case LE_EXPR:
7184       if (above)
7185         return omit_one_operand (type, integer_one_node, arg0);
7186       else if (below)
7187         return omit_one_operand (type, integer_zero_node, arg0);
7188
7189     case GT_EXPR:
7190     case GE_EXPR:
7191       if (above)
7192         return omit_one_operand (type, integer_zero_node, arg0);
7193       else if (below)
7194         return omit_one_operand (type, integer_one_node, arg0);
7195
7196     default:
7197       break;
7198     }
7199
7200   return NULL_TREE;
7201 }
7202
7203 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7204    ARG0 just the signedness is changed.  */
7205
7206 static tree
7207 fold_sign_changed_comparison (enum tree_code code, tree type,
7208                               tree arg0, tree arg1)
7209 {
7210   tree arg0_inner;
7211   tree inner_type, outer_type;
7212
7213   if (!CONVERT_EXPR_P (arg0))
7214     return NULL_TREE;
7215
7216   outer_type = TREE_TYPE (arg0);
7217   arg0_inner = TREE_OPERAND (arg0, 0);
7218   inner_type = TREE_TYPE (arg0_inner);
7219
7220 #ifdef HAVE_canonicalize_funcptr_for_compare
7221   /* Disable this optimization if we're casting a function pointer
7222      type on targets that require function pointer canonicalization.  */
7223   if (HAVE_canonicalize_funcptr_for_compare
7224       && TREE_CODE (inner_type) == POINTER_TYPE
7225       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7226     return NULL_TREE;
7227 #endif
7228
7229   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7230     return NULL_TREE;
7231
7232   /* If the conversion is from an integral subtype to its basetype
7233      leave it alone.  */
7234   if (TREE_TYPE (inner_type) == outer_type)
7235     return NULL_TREE;
7236
7237   if (TREE_CODE (arg1) != INTEGER_CST
7238       && !(CONVERT_EXPR_P (arg1)
7239            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7240     return NULL_TREE;
7241
7242   if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7243        || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
7244       && code != NE_EXPR
7245       && code != EQ_EXPR)
7246     return NULL_TREE;
7247
7248   if (TREE_CODE (arg1) == INTEGER_CST)
7249     arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
7250                                   TREE_INT_CST_HIGH (arg1), 0,
7251                                   TREE_OVERFLOW (arg1));
7252   else
7253     arg1 = fold_convert (inner_type, arg1);
7254
7255   return fold_build2 (code, type, arg0_inner, arg1);
7256 }
7257
7258 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
7259    step of the array.  Reconstructs s and delta in the case of s * delta
7260    being an integer constant (and thus already folded).
7261    ADDR is the address. MULT is the multiplicative expression.
7262    If the function succeeds, the new address expression is returned.  Otherwise
7263    NULL_TREE is returned.  */
7264
7265 static tree
7266 try_move_mult_to_index (tree addr, tree op1)
7267 {
7268   tree s, delta, step;
7269   tree ref = TREE_OPERAND (addr, 0), pref;
7270   tree ret, pos;
7271   tree itype;
7272   bool mdim = false;
7273
7274   /*  Strip the nops that might be added when converting op1 to sizetype. */
7275   STRIP_NOPS (op1);
7276
7277   /* Canonicalize op1 into a possibly non-constant delta
7278      and an INTEGER_CST s.  */
7279   if (TREE_CODE (op1) == MULT_EXPR)
7280     {
7281       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
7282
7283       STRIP_NOPS (arg0);
7284       STRIP_NOPS (arg1);
7285   
7286       if (TREE_CODE (arg0) == INTEGER_CST)
7287         {
7288           s = arg0;
7289           delta = arg1;
7290         }
7291       else if (TREE_CODE (arg1) == INTEGER_CST)
7292         {
7293           s = arg1;
7294           delta = arg0;
7295         }
7296       else
7297         return NULL_TREE;
7298     }
7299   else if (TREE_CODE (op1) == INTEGER_CST)
7300     {
7301       delta = op1;
7302       s = NULL_TREE;
7303     }
7304   else
7305     {
7306       /* Simulate we are delta * 1.  */
7307       delta = op1;
7308       s = integer_one_node;
7309     }
7310
7311   for (;; ref = TREE_OPERAND (ref, 0))
7312     {
7313       if (TREE_CODE (ref) == ARRAY_REF)
7314         {
7315           /* Remember if this was a multi-dimensional array.  */
7316           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
7317             mdim = true;
7318
7319           itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
7320           if (! itype)
7321             continue;
7322
7323           step = array_ref_element_size (ref);
7324           if (TREE_CODE (step) != INTEGER_CST)
7325             continue;
7326
7327           if (s)
7328             {
7329               if (! tree_int_cst_equal (step, s))
7330                 continue;
7331             }
7332           else
7333             {
7334               /* Try if delta is a multiple of step.  */
7335               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
7336               if (! tmp)
7337                 continue;
7338               delta = tmp;
7339             }
7340
7341           /* Only fold here if we can verify we do not overflow one
7342              dimension of a multi-dimensional array.  */
7343           if (mdim)
7344             {
7345               tree tmp;
7346
7347               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7348                   || !INTEGRAL_TYPE_P (itype)
7349                   || !TYPE_MAX_VALUE (itype)
7350                   || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
7351                 continue;
7352
7353               tmp = fold_binary (PLUS_EXPR, itype,
7354                                  fold_convert (itype,
7355                                                TREE_OPERAND (ref, 1)),
7356                                  fold_convert (itype, delta));
7357               if (!tmp
7358                   || TREE_CODE (tmp) != INTEGER_CST
7359                   || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
7360                 continue;
7361             }
7362
7363           break;
7364         }
7365       else
7366         mdim = false;
7367
7368       if (!handled_component_p (ref))
7369         return NULL_TREE;
7370     }
7371
7372   /* We found the suitable array reference.  So copy everything up to it,
7373      and replace the index.  */
7374
7375   pref = TREE_OPERAND (addr, 0);
7376   ret = copy_node (pref);
7377   pos = ret;
7378
7379   while (pref != ref)
7380     {
7381       pref = TREE_OPERAND (pref, 0);
7382       TREE_OPERAND (pos, 0) = copy_node (pref);
7383       pos = TREE_OPERAND (pos, 0);
7384     }
7385
7386   TREE_OPERAND (pos, 1) = fold_build2 (PLUS_EXPR, itype,
7387                                        fold_convert (itype,
7388                                                      TREE_OPERAND (pos, 1)),
7389                                        fold_convert (itype, delta));
7390
7391   return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
7392 }
7393
7394
7395 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
7396    means A >= Y && A != MAX, but in this case we know that
7397    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
7398
7399 static tree
7400 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
7401 {
7402   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7403
7404   if (TREE_CODE (bound) == LT_EXPR)
7405     a = TREE_OPERAND (bound, 0);
7406   else if (TREE_CODE (bound) == GT_EXPR)
7407     a = TREE_OPERAND (bound, 1);
7408   else
7409     return NULL_TREE;
7410
7411   typea = TREE_TYPE (a);
7412   if (!INTEGRAL_TYPE_P (typea)
7413       && !POINTER_TYPE_P (typea))
7414     return NULL_TREE;
7415
7416   if (TREE_CODE (ineq) == LT_EXPR)
7417     {
7418       a1 = TREE_OPERAND (ineq, 1);
7419       y = TREE_OPERAND (ineq, 0);
7420     }
7421   else if (TREE_CODE (ineq) == GT_EXPR)
7422     {
7423       a1 = TREE_OPERAND (ineq, 0);
7424       y = TREE_OPERAND (ineq, 1);
7425     }
7426   else
7427     return NULL_TREE;
7428
7429   if (TREE_TYPE (a1) != typea)
7430     return NULL_TREE;
7431
7432   if (POINTER_TYPE_P (typea))
7433     {
7434       /* Convert the pointer types into integer before taking the difference.  */
7435       tree ta = fold_convert (ssizetype, a);
7436       tree ta1 = fold_convert (ssizetype, a1);
7437       diff = fold_binary (MINUS_EXPR, ssizetype, ta1, ta);
7438     }
7439   else
7440    diff = fold_binary (MINUS_EXPR, typea, a1, a);
7441
7442   if (!diff || !integer_onep (diff))
7443    return NULL_TREE;
7444
7445   return fold_build2 (GE_EXPR, type, a, y);
7446 }
7447
7448 /* Fold a sum or difference of at least one multiplication.
7449    Returns the folded tree or NULL if no simplification could be made.  */
7450
7451 static tree
7452 fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
7453 {
7454   tree arg00, arg01, arg10, arg11;
7455   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7456
7457   /* (A * C) +- (B * C) -> (A+-B) * C.
7458      (A * C) +- A -> A * (C+-1).
7459      We are most concerned about the case where C is a constant,
7460      but other combinations show up during loop reduction.  Since
7461      it is not difficult, try all four possibilities.  */
7462
7463   if (TREE_CODE (arg0) == MULT_EXPR)
7464     {
7465       arg00 = TREE_OPERAND (arg0, 0);
7466       arg01 = TREE_OPERAND (arg0, 1);
7467     }
7468   else if (TREE_CODE (arg0) == INTEGER_CST)
7469     {
7470       arg00 = build_one_cst (type);
7471       arg01 = arg0;
7472     }
7473   else
7474     {
7475       /* We cannot generate constant 1 for fract.  */
7476       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7477         return NULL_TREE;
7478       arg00 = arg0;
7479       arg01 = build_one_cst (type);
7480     }
7481   if (TREE_CODE (arg1) == MULT_EXPR)
7482     {
7483       arg10 = TREE_OPERAND (arg1, 0);
7484       arg11 = TREE_OPERAND (arg1, 1);
7485     }
7486   else if (TREE_CODE (arg1) == INTEGER_CST)
7487     {
7488       arg10 = build_one_cst (type);
7489       /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7490          the purpose of this canonicalization.  */
7491       if (TREE_INT_CST_HIGH (arg1) == -1
7492           && negate_expr_p (arg1)
7493           && code == PLUS_EXPR)
7494         {
7495           arg11 = negate_expr (arg1);
7496           code = MINUS_EXPR;
7497         }
7498       else
7499         arg11 = arg1;
7500     }
7501   else
7502     {
7503       /* We cannot generate constant 1 for fract.  */
7504       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7505         return NULL_TREE;
7506       arg10 = arg1;
7507       arg11 = build_one_cst (type);
7508     }
7509   same = NULL_TREE;
7510
7511   if (operand_equal_p (arg01, arg11, 0))
7512     same = arg01, alt0 = arg00, alt1 = arg10;
7513   else if (operand_equal_p (arg00, arg10, 0))
7514     same = arg00, alt0 = arg01, alt1 = arg11;
7515   else if (operand_equal_p (arg00, arg11, 0))
7516     same = arg00, alt0 = arg01, alt1 = arg10;
7517   else if (operand_equal_p (arg01, arg10, 0))
7518     same = arg01, alt0 = arg00, alt1 = arg11;
7519
7520   /* No identical multiplicands; see if we can find a common
7521      power-of-two factor in non-power-of-two multiplies.  This
7522      can help in multi-dimensional array access.  */
7523   else if (host_integerp (arg01, 0)
7524            && host_integerp (arg11, 0))
7525     {
7526       HOST_WIDE_INT int01, int11, tmp;
7527       bool swap = false;
7528       tree maybe_same;
7529       int01 = TREE_INT_CST_LOW (arg01);
7530       int11 = TREE_INT_CST_LOW (arg11);
7531
7532       /* Move min of absolute values to int11.  */
7533       if ((int01 >= 0 ? int01 : -int01)
7534           < (int11 >= 0 ? int11 : -int11))
7535         {
7536           tmp = int01, int01 = int11, int11 = tmp;
7537           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7538           maybe_same = arg01;
7539           swap = true;
7540         }
7541       else
7542         maybe_same = arg11;
7543
7544       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
7545           /* The remainder should not be a constant, otherwise we
7546              end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7547              increased the number of multiplications necessary.  */
7548           && TREE_CODE (arg10) != INTEGER_CST)
7549         {
7550           alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
7551                               build_int_cst (TREE_TYPE (arg00),
7552                                              int01 / int11));
7553           alt1 = arg10;
7554           same = maybe_same;
7555           if (swap)
7556             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7557         }
7558     }
7559
7560   if (same)
7561     return fold_build2 (MULT_EXPR, type,
7562                         fold_build2 (code, type,
7563                                      fold_convert (type, alt0),
7564                                      fold_convert (type, alt1)),
7565                         fold_convert (type, same));
7566
7567   return NULL_TREE;
7568 }
7569
7570 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7571    specified by EXPR into the buffer PTR of length LEN bytes.
7572    Return the number of bytes placed in the buffer, or zero
7573    upon failure.  */
7574
7575 static int
7576 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7577 {
7578   tree type = TREE_TYPE (expr);
7579   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7580   int byte, offset, word, words;
7581   unsigned char value;
7582
7583   if (total_bytes > len)
7584     return 0;
7585   words = total_bytes / UNITS_PER_WORD;
7586
7587   for (byte = 0; byte < total_bytes; byte++)
7588     {
7589       int bitpos = byte * BITS_PER_UNIT;
7590       if (bitpos < HOST_BITS_PER_WIDE_INT)
7591         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7592       else
7593         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7594                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7595
7596       if (total_bytes > UNITS_PER_WORD)
7597         {
7598           word = byte / UNITS_PER_WORD;
7599           if (WORDS_BIG_ENDIAN)
7600             word = (words - 1) - word;
7601           offset = word * UNITS_PER_WORD;
7602           if (BYTES_BIG_ENDIAN)
7603             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7604           else
7605             offset += byte % UNITS_PER_WORD;
7606         }
7607       else
7608         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7609       ptr[offset] = value;
7610     }
7611   return total_bytes;
7612 }
7613
7614
7615 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7616    specified by EXPR into the buffer PTR of length LEN bytes.
7617    Return the number of bytes placed in the buffer, or zero
7618    upon failure.  */
7619
7620 static int
7621 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7622 {
7623   tree type = TREE_TYPE (expr);
7624   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7625   int byte, offset, word, words, bitpos;
7626   unsigned char value;
7627
7628   /* There are always 32 bits in each long, no matter the size of
7629      the hosts long.  We handle floating point representations with
7630      up to 192 bits.  */
7631   long tmp[6];
7632
7633   if (total_bytes > len)
7634     return 0;
7635   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7636
7637   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7638
7639   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7640        bitpos += BITS_PER_UNIT)
7641     {
7642       byte = (bitpos / BITS_PER_UNIT) & 3;
7643       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7644
7645       if (UNITS_PER_WORD < 4)
7646         {
7647           word = byte / UNITS_PER_WORD;
7648           if (WORDS_BIG_ENDIAN)
7649             word = (words - 1) - word;
7650           offset = word * UNITS_PER_WORD;
7651           if (BYTES_BIG_ENDIAN)
7652             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7653           else
7654             offset += byte % UNITS_PER_WORD;
7655         }
7656       else
7657         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7658       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7659     }
7660   return total_bytes;
7661 }
7662
7663 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7664    specified by EXPR into the buffer PTR of length LEN bytes.
7665    Return the number of bytes placed in the buffer, or zero
7666    upon failure.  */
7667
7668 static int
7669 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7670 {
7671   int rsize, isize;
7672   tree part;
7673
7674   part = TREE_REALPART (expr);
7675   rsize = native_encode_expr (part, ptr, len);
7676   if (rsize == 0)
7677     return 0;
7678   part = TREE_IMAGPART (expr);
7679   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7680   if (isize != rsize)
7681     return 0;
7682   return rsize + isize;
7683 }
7684
7685
7686 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7687    specified by EXPR into the buffer PTR of length LEN bytes.
7688    Return the number of bytes placed in the buffer, or zero
7689    upon failure.  */
7690
7691 static int
7692 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7693 {
7694   int i, size, offset, count;
7695   tree itype, elem, elements;
7696
7697   offset = 0;
7698   elements = TREE_VECTOR_CST_ELTS (expr);
7699   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7700   itype = TREE_TYPE (TREE_TYPE (expr));
7701   size = GET_MODE_SIZE (TYPE_MODE (itype));
7702   for (i = 0; i < count; i++)
7703     {
7704       if (elements)
7705         {
7706           elem = TREE_VALUE (elements);
7707           elements = TREE_CHAIN (elements);
7708         }
7709       else
7710         elem = NULL_TREE;
7711
7712       if (elem)
7713         {
7714           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7715             return 0;
7716         }
7717       else
7718         {
7719           if (offset + size > len)
7720             return 0;
7721           memset (ptr+offset, 0, size);
7722         }
7723       offset += size;
7724     }
7725   return offset;
7726 }
7727
7728
7729 /* Subroutine of native_encode_expr.  Encode the STRING_CST
7730    specified by EXPR into the buffer PTR of length LEN bytes.
7731    Return the number of bytes placed in the buffer, or zero
7732    upon failure.  */
7733
7734 static int
7735 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7736 {
7737   tree type = TREE_TYPE (expr);
7738   HOST_WIDE_INT total_bytes;
7739
7740   if (TREE_CODE (type) != ARRAY_TYPE
7741       || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7742       || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7743       || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7744     return 0;
7745   total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7746   if (total_bytes > len)
7747     return 0;
7748   if (TREE_STRING_LENGTH (expr) < total_bytes)
7749     {
7750       memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7751       memset (ptr + TREE_STRING_LENGTH (expr), 0,
7752               total_bytes - TREE_STRING_LENGTH (expr));
7753     }
7754   else
7755     memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7756   return total_bytes;
7757 }
7758
7759
7760 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7761    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7762    buffer PTR of length LEN bytes.  Return the number of bytes
7763    placed in the buffer, or zero upon failure.  */
7764
7765 int
7766 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7767 {
7768   switch (TREE_CODE (expr))
7769     {
7770     case INTEGER_CST:
7771       return native_encode_int (expr, ptr, len);
7772
7773     case REAL_CST:
7774       return native_encode_real (expr, ptr, len);
7775
7776     case COMPLEX_CST:
7777       return native_encode_complex (expr, ptr, len);
7778
7779     case VECTOR_CST:
7780       return native_encode_vector (expr, ptr, len);
7781
7782     case STRING_CST:
7783       return native_encode_string (expr, ptr, len);
7784
7785     default:
7786       return 0;
7787     }
7788 }
7789
7790
7791 /* Subroutine of native_interpret_expr.  Interpret the contents of
7792    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7793    If the buffer cannot be interpreted, return NULL_TREE.  */
7794
7795 static tree
7796 native_interpret_int (tree type, const unsigned char *ptr, int len)
7797 {
7798   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7799   int byte, offset, word, words;
7800   unsigned char value;
7801   unsigned int HOST_WIDE_INT lo = 0;
7802   HOST_WIDE_INT hi = 0;
7803
7804   if (total_bytes > len)
7805     return NULL_TREE;
7806   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7807     return NULL_TREE;
7808   words = total_bytes / UNITS_PER_WORD;
7809
7810   for (byte = 0; byte < total_bytes; byte++)
7811     {
7812       int bitpos = byte * BITS_PER_UNIT;
7813       if (total_bytes > UNITS_PER_WORD)
7814         {
7815           word = byte / UNITS_PER_WORD;
7816           if (WORDS_BIG_ENDIAN)
7817             word = (words - 1) - word;
7818           offset = word * UNITS_PER_WORD;
7819           if (BYTES_BIG_ENDIAN)
7820             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7821           else
7822             offset += byte % UNITS_PER_WORD;
7823         }
7824       else
7825         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7826       value = ptr[offset];
7827
7828       if (bitpos < HOST_BITS_PER_WIDE_INT)
7829         lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7830       else
7831         hi |= (unsigned HOST_WIDE_INT) value
7832               << (bitpos - HOST_BITS_PER_WIDE_INT);
7833     }
7834
7835   return build_int_cst_wide_type (type, lo, hi);
7836 }
7837
7838
7839 /* Subroutine of native_interpret_expr.  Interpret the contents of
7840    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7841    If the buffer cannot be interpreted, return NULL_TREE.  */
7842
7843 static tree
7844 native_interpret_real (tree type, const unsigned char *ptr, int len)
7845 {
7846   enum machine_mode mode = TYPE_MODE (type);
7847   int total_bytes = GET_MODE_SIZE (mode);
7848   int byte, offset, word, words, bitpos;
7849   unsigned char value;
7850   /* There are always 32 bits in each long, no matter the size of
7851      the hosts long.  We handle floating point representations with
7852      up to 192 bits.  */
7853   REAL_VALUE_TYPE r;
7854   long tmp[6];
7855
7856   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7857   if (total_bytes > len || total_bytes > 24)
7858     return NULL_TREE;
7859   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7860
7861   memset (tmp, 0, sizeof (tmp));
7862   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7863        bitpos += BITS_PER_UNIT)
7864     {
7865       byte = (bitpos / BITS_PER_UNIT) & 3;
7866       if (UNITS_PER_WORD < 4)
7867         {
7868           word = byte / UNITS_PER_WORD;
7869           if (WORDS_BIG_ENDIAN)
7870             word = (words - 1) - word;
7871           offset = word * UNITS_PER_WORD;
7872           if (BYTES_BIG_ENDIAN)
7873             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7874           else
7875             offset += byte % UNITS_PER_WORD;
7876         }
7877       else
7878         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7879       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7880
7881       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7882     }
7883
7884   real_from_target (&r, tmp, mode);
7885   return build_real (type, r);
7886 }
7887
7888
7889 /* Subroutine of native_interpret_expr.  Interpret the contents of
7890    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7891    If the buffer cannot be interpreted, return NULL_TREE.  */
7892
7893 static tree
7894 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7895 {
7896   tree etype, rpart, ipart;
7897   int size;
7898
7899   etype = TREE_TYPE (type);
7900   size = GET_MODE_SIZE (TYPE_MODE (etype));
7901   if (size * 2 > len)
7902     return NULL_TREE;
7903   rpart = native_interpret_expr (etype, ptr, size);
7904   if (!rpart)
7905     return NULL_TREE;
7906   ipart = native_interpret_expr (etype, ptr+size, size);
7907   if (!ipart)
7908     return NULL_TREE;
7909   return build_complex (type, rpart, ipart);
7910 }
7911
7912
7913 /* Subroutine of native_interpret_expr.  Interpret the contents of
7914    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7915    If the buffer cannot be interpreted, return NULL_TREE.  */
7916
7917 static tree
7918 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7919 {
7920   tree etype, elem, elements;
7921   int i, size, count;
7922
7923   etype = TREE_TYPE (type);
7924   size = GET_MODE_SIZE (TYPE_MODE (etype));
7925   count = TYPE_VECTOR_SUBPARTS (type);
7926   if (size * count > len)
7927     return NULL_TREE;
7928
7929   elements = NULL_TREE;
7930   for (i = count - 1; i >= 0; i--)
7931     {
7932       elem = native_interpret_expr (etype, ptr+(i*size), size);
7933       if (!elem)
7934         return NULL_TREE;
7935       elements = tree_cons (NULL_TREE, elem, elements);
7936     }
7937   return build_vector (type, elements);
7938 }
7939
7940
7941 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7942    the buffer PTR of length LEN as a constant of type TYPE.  For
7943    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7944    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7945    return NULL_TREE.  */
7946
7947 tree
7948 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7949 {
7950   switch (TREE_CODE (type))
7951     {
7952     case INTEGER_TYPE:
7953     case ENUMERAL_TYPE:
7954     case BOOLEAN_TYPE:
7955       return native_interpret_int (type, ptr, len);
7956
7957     case REAL_TYPE:
7958       return native_interpret_real (type, ptr, len);
7959
7960     case COMPLEX_TYPE:
7961       return native_interpret_complex (type, ptr, len);
7962
7963     case VECTOR_TYPE:
7964       return native_interpret_vector (type, ptr, len);
7965
7966     default:
7967       return NULL_TREE;
7968     }
7969 }
7970
7971
7972 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7973    TYPE at compile-time.  If we're unable to perform the conversion
7974    return NULL_TREE.  */
7975
7976 static tree
7977 fold_view_convert_expr (tree type, tree expr)
7978 {
7979   /* We support up to 512-bit values (for V8DFmode).  */
7980   unsigned char buffer[64];
7981   int len;
7982
7983   /* Check that the host and target are sane.  */
7984   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7985     return NULL_TREE;
7986
7987   len = native_encode_expr (expr, buffer, sizeof (buffer));
7988   if (len == 0)
7989     return NULL_TREE;
7990
7991   return native_interpret_expr (type, buffer, len);
7992 }
7993
7994 /* Build an expression for the address of T.  Folds away INDIRECT_REF
7995    to avoid confusing the gimplify process.  */
7996
7997 tree
7998 build_fold_addr_expr_with_type (tree t, tree ptrtype)
7999 {
8000   /* The size of the object is not relevant when talking about its address.  */
8001   if (TREE_CODE (t) == WITH_SIZE_EXPR)
8002     t = TREE_OPERAND (t, 0);
8003
8004   /* Note: doesn't apply to ALIGN_INDIRECT_REF */
8005   if (TREE_CODE (t) == INDIRECT_REF
8006       || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
8007     {
8008       t = TREE_OPERAND (t, 0);
8009
8010       if (TREE_TYPE (t) != ptrtype)
8011         t = build1 (NOP_EXPR, ptrtype, t);
8012     }
8013   else
8014     t = build1 (ADDR_EXPR, ptrtype, t);
8015
8016   return t;
8017 }
8018
8019 /* Build an expression for the address of T.  */
8020
8021 tree
8022 build_fold_addr_expr (tree t)
8023 {
8024   tree ptrtype = build_pointer_type (TREE_TYPE (t));
8025
8026   return build_fold_addr_expr_with_type (t, ptrtype);
8027 }
8028
8029 /* Fold a unary expression of code CODE and type TYPE with operand
8030    OP0.  Return the folded expression if folding is successful.
8031    Otherwise, return NULL_TREE.  */
8032
8033 tree
8034 fold_unary (enum tree_code code, tree type, tree op0)
8035 {
8036   tree tem;
8037   tree arg0;
8038   enum tree_code_class kind = TREE_CODE_CLASS (code);
8039
8040   gcc_assert (IS_EXPR_CODE_CLASS (kind)
8041               && TREE_CODE_LENGTH (code) == 1);
8042
8043   arg0 = op0;
8044   if (arg0)
8045     {
8046       if (CONVERT_EXPR_CODE_P (code)
8047           || code == FLOAT_EXPR || code == ABS_EXPR)
8048         {
8049           /* Don't use STRIP_NOPS, because signedness of argument type
8050              matters.  */
8051           STRIP_SIGN_NOPS (arg0);
8052         }
8053       else
8054         {
8055           /* Strip any conversions that don't change the mode.  This
8056              is safe for every expression, except for a comparison
8057              expression because its signedness is derived from its
8058              operands.
8059
8060              Note that this is done as an internal manipulation within
8061              the constant folder, in order to find the simplest
8062              representation of the arguments so that their form can be
8063              studied.  In any cases, the appropriate type conversions
8064              should be put back in the tree that will get out of the
8065              constant folder.  */
8066           STRIP_NOPS (arg0);
8067         }
8068     }
8069
8070   if (TREE_CODE_CLASS (code) == tcc_unary)
8071     {
8072       if (TREE_CODE (arg0) == COMPOUND_EXPR)
8073         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
8074                        fold_build1 (code, type,
8075                                     fold_convert (TREE_TYPE (op0),
8076                                                   TREE_OPERAND (arg0, 1))));
8077       else if (TREE_CODE (arg0) == COND_EXPR)
8078         {
8079           tree arg01 = TREE_OPERAND (arg0, 1);
8080           tree arg02 = TREE_OPERAND (arg0, 2);
8081           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
8082             arg01 = fold_build1 (code, type,
8083                                  fold_convert (TREE_TYPE (op0), arg01));
8084           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
8085             arg02 = fold_build1 (code, type,
8086                                  fold_convert (TREE_TYPE (op0), arg02));
8087           tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
8088                              arg01, arg02);
8089
8090           /* If this was a conversion, and all we did was to move into
8091              inside the COND_EXPR, bring it back out.  But leave it if
8092              it is a conversion from integer to integer and the
8093              result precision is no wider than a word since such a
8094              conversion is cheap and may be optimized away by combine,
8095              while it couldn't if it were outside the COND_EXPR.  Then return
8096              so we don't get into an infinite recursion loop taking the
8097              conversion out and then back in.  */
8098
8099           if ((CONVERT_EXPR_CODE_P (code)
8100                || code == NON_LVALUE_EXPR)
8101               && TREE_CODE (tem) == COND_EXPR
8102               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
8103               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
8104               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
8105               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
8106               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
8107                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
8108               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8109                      && (INTEGRAL_TYPE_P
8110                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
8111                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
8112                   || flag_syntax_only))
8113             tem = build1 (code, type,
8114                           build3 (COND_EXPR,
8115                                   TREE_TYPE (TREE_OPERAND
8116                                              (TREE_OPERAND (tem, 1), 0)),
8117                                   TREE_OPERAND (tem, 0),
8118                                   TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
8119                                   TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
8120           return tem;
8121         }
8122       else if (COMPARISON_CLASS_P (arg0))
8123         {
8124           if (TREE_CODE (type) == BOOLEAN_TYPE)
8125             {
8126               arg0 = copy_node (arg0);
8127               TREE_TYPE (arg0) = type;
8128               return arg0;
8129             }
8130           else if (TREE_CODE (type) != INTEGER_TYPE)
8131             return fold_build3 (COND_EXPR, type, arg0,
8132                                 fold_build1 (code, type,
8133                                              integer_one_node),
8134                                 fold_build1 (code, type,
8135                                              integer_zero_node));
8136         }
8137    }
8138
8139   switch (code)
8140     {
8141     case PAREN_EXPR:
8142       /* Re-association barriers around constants and other re-association
8143          barriers can be removed.  */
8144       if (CONSTANT_CLASS_P (op0)
8145           || TREE_CODE (op0) == PAREN_EXPR)
8146         return fold_convert (type, op0);
8147       return NULL_TREE;
8148
8149     CASE_CONVERT:
8150     case FLOAT_EXPR:
8151     case FIX_TRUNC_EXPR:
8152       if (TREE_TYPE (op0) == type)
8153         return op0;
8154       
8155       /* If we have (type) (a CMP b) and type is an integral type, return
8156          new expression involving the new type.  */
8157       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
8158         return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
8159                             TREE_OPERAND (op0, 1));
8160
8161       /* Handle cases of two conversions in a row.  */
8162       if (CONVERT_EXPR_P (op0))
8163         {
8164           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
8165           tree inter_type = TREE_TYPE (op0);
8166           int inside_int = INTEGRAL_TYPE_P (inside_type);
8167           int inside_ptr = POINTER_TYPE_P (inside_type);
8168           int inside_float = FLOAT_TYPE_P (inside_type);
8169           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
8170           unsigned int inside_prec = TYPE_PRECISION (inside_type);
8171           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
8172           int inter_int = INTEGRAL_TYPE_P (inter_type);
8173           int inter_ptr = POINTER_TYPE_P (inter_type);
8174           int inter_float = FLOAT_TYPE_P (inter_type);
8175           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
8176           unsigned int inter_prec = TYPE_PRECISION (inter_type);
8177           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
8178           int final_int = INTEGRAL_TYPE_P (type);
8179           int final_ptr = POINTER_TYPE_P (type);
8180           int final_float = FLOAT_TYPE_P (type);
8181           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
8182           unsigned int final_prec = TYPE_PRECISION (type);
8183           int final_unsignedp = TYPE_UNSIGNED (type);
8184
8185           /* In addition to the cases of two conversions in a row
8186              handled below, if we are converting something to its own
8187              type via an object of identical or wider precision, neither
8188              conversion is needed.  */
8189           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
8190               && (((inter_int || inter_ptr) && final_int)
8191                   || (inter_float && final_float))
8192               && inter_prec >= final_prec)
8193             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8194
8195           /* Likewise, if the intermediate and initial types are either both
8196              float or both integer, we don't need the middle conversion if the
8197              former is wider than the latter and doesn't change the signedness
8198              (for integers).  Avoid this if the final type is a pointer since
8199              then we sometimes need the middle conversion.  Likewise if the
8200              final type has a precision not equal to the size of its mode.  */
8201           if (((inter_int && inside_int)
8202                || (inter_float && inside_float)
8203                || (inter_vec && inside_vec))
8204               && inter_prec >= inside_prec
8205               && (inter_float || inter_vec
8206                   || inter_unsignedp == inside_unsignedp)
8207               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8208                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
8209               && ! final_ptr
8210               && (! final_vec || inter_prec == inside_prec))
8211             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8212
8213           /* If we have a sign-extension of a zero-extended value, we can
8214              replace that by a single zero-extension.  */
8215           if (inside_int && inter_int && final_int
8216               && inside_prec < inter_prec && inter_prec < final_prec
8217               && inside_unsignedp && !inter_unsignedp)
8218             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8219
8220           /* Two conversions in a row are not needed unless:
8221              - some conversion is floating-point (overstrict for now), or
8222              - some conversion is a vector (overstrict for now), or
8223              - the intermediate type is narrower than both initial and
8224                final, or
8225              - the intermediate type and innermost type differ in signedness,
8226                and the outermost type is wider than the intermediate, or
8227              - the initial type is a pointer type and the precisions of the
8228                intermediate and final types differ, or
8229              - the final type is a pointer type and the precisions of the
8230                initial and intermediate types differ.  */
8231           if (! inside_float && ! inter_float && ! final_float
8232               && ! inside_vec && ! inter_vec && ! final_vec
8233               && (inter_prec >= inside_prec || inter_prec >= final_prec)
8234               && ! (inside_int && inter_int
8235                     && inter_unsignedp != inside_unsignedp
8236                     && inter_prec < final_prec)
8237               && ((inter_unsignedp && inter_prec > inside_prec)
8238                   == (final_unsignedp && final_prec > inter_prec))
8239               && ! (inside_ptr && inter_prec != final_prec)
8240               && ! (final_ptr && inside_prec != inter_prec)
8241               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8242                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
8243             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8244         }
8245
8246       /* Handle (T *)&A.B.C for A being of type T and B and C
8247          living at offset zero.  This occurs frequently in
8248          C++ upcasting and then accessing the base.  */
8249       if (TREE_CODE (op0) == ADDR_EXPR
8250           && POINTER_TYPE_P (type)
8251           && handled_component_p (TREE_OPERAND (op0, 0)))
8252         {
8253           HOST_WIDE_INT bitsize, bitpos;
8254           tree offset;
8255           enum machine_mode mode;
8256           int unsignedp, volatilep;
8257           tree base = TREE_OPERAND (op0, 0);
8258           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8259                                       &mode, &unsignedp, &volatilep, false);
8260           /* If the reference was to a (constant) zero offset, we can use
8261              the address of the base if it has the same base type
8262              as the result type.  */
8263           if (! offset && bitpos == 0
8264               && TYPE_MAIN_VARIANT (TREE_TYPE (type))
8265                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
8266             return fold_convert (type, build_fold_addr_expr (base));
8267         }
8268
8269       if (TREE_CODE (op0) == MODIFY_EXPR
8270           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
8271           /* Detect assigning a bitfield.  */
8272           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
8273                && DECL_BIT_FIELD
8274                (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
8275         {
8276           /* Don't leave an assignment inside a conversion
8277              unless assigning a bitfield.  */
8278           tem = fold_build1 (code, type, TREE_OPERAND (op0, 1));
8279           /* First do the assignment, then return converted constant.  */
8280           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
8281           TREE_NO_WARNING (tem) = 1;
8282           TREE_USED (tem) = 1;
8283           return tem;
8284         }
8285
8286       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8287          constants (if x has signed type, the sign bit cannot be set
8288          in c).  This folds extension into the BIT_AND_EXPR.
8289          ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8290          very likely don't have maximal range for their precision and this
8291          transformation effectively doesn't preserve non-maximal ranges.  */
8292       if (TREE_CODE (type) == INTEGER_TYPE
8293           && TREE_CODE (op0) == BIT_AND_EXPR
8294           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST
8295           /* Not if the conversion is to the sub-type.  */
8296           && TREE_TYPE (type) != TREE_TYPE (op0))
8297         {
8298           tree and = op0;
8299           tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
8300           int change = 0;
8301
8302           if (TYPE_UNSIGNED (TREE_TYPE (and))
8303               || (TYPE_PRECISION (type)
8304                   <= TYPE_PRECISION (TREE_TYPE (and))))
8305             change = 1;
8306           else if (TYPE_PRECISION (TREE_TYPE (and1))
8307                    <= HOST_BITS_PER_WIDE_INT
8308                    && host_integerp (and1, 1))
8309             {
8310               unsigned HOST_WIDE_INT cst;
8311
8312               cst = tree_low_cst (and1, 1);
8313               cst &= (HOST_WIDE_INT) -1
8314                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8315               change = (cst == 0);
8316 #ifdef LOAD_EXTEND_OP
8317               if (change
8318                   && !flag_syntax_only
8319                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8320                       == ZERO_EXTEND))
8321                 {
8322                   tree uns = unsigned_type_for (TREE_TYPE (and0));
8323                   and0 = fold_convert (uns, and0);
8324                   and1 = fold_convert (uns, and1);
8325                 }
8326 #endif
8327             }
8328           if (change)
8329             {
8330               tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
8331                                            TREE_INT_CST_HIGH (and1), 0,
8332                                            TREE_OVERFLOW (and1));
8333               return fold_build2 (BIT_AND_EXPR, type,
8334                                   fold_convert (type, and0), tem);
8335             }
8336         }
8337
8338       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8339          when one of the new casts will fold away. Conservatively we assume
8340          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8341       if (POINTER_TYPE_P (type)
8342           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8343           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8344               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8345               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8346         {
8347           tree arg00 = TREE_OPERAND (arg0, 0);
8348           tree arg01 = TREE_OPERAND (arg0, 1);
8349
8350           return fold_build2 (TREE_CODE (arg0), type, fold_convert (type, arg00),
8351                               fold_convert (sizetype, arg01));
8352         }
8353
8354       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8355          of the same precision, and X is an integer type not narrower than
8356          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
8357       if (INTEGRAL_TYPE_P (type)
8358           && TREE_CODE (op0) == BIT_NOT_EXPR
8359           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8360           && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8361           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8362         {
8363           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8364           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8365               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8366             return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
8367         }
8368
8369       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8370          type of X and Y (integer types only).  */
8371       if (INTEGRAL_TYPE_P (type)
8372           && TREE_CODE (op0) == MULT_EXPR
8373           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8374           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8375         {
8376           /* Be careful not to introduce new overflows.  */
8377           tree mult_type;
8378           if (TYPE_OVERFLOW_WRAPS (type))
8379             mult_type = type;
8380           else
8381             mult_type = unsigned_type_for (type);
8382
8383           if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8384             {
8385               tem = fold_build2 (MULT_EXPR, mult_type,
8386                                  fold_convert (mult_type,
8387                                                TREE_OPERAND (op0, 0)),
8388                                  fold_convert (mult_type,
8389                                                TREE_OPERAND (op0, 1)));
8390               return fold_convert (type, tem);
8391             }
8392         }
8393
8394       tem = fold_convert_const (code, type, op0);
8395       return tem ? tem : NULL_TREE;
8396
8397     case FIXED_CONVERT_EXPR:
8398       tem = fold_convert_const (code, type, arg0);
8399       return tem ? tem : NULL_TREE;
8400
8401     case VIEW_CONVERT_EXPR:
8402       if (TREE_TYPE (op0) == type)
8403         return op0;
8404       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8405         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8406
8407       /* For integral conversions with the same precision or pointer
8408          conversions use a NOP_EXPR instead.  */
8409       if ((INTEGRAL_TYPE_P (type)
8410            || POINTER_TYPE_P (type))
8411           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8412               || POINTER_TYPE_P (TREE_TYPE (op0)))
8413           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0))
8414           /* Do not muck with VIEW_CONVERT_EXPRs that convert from
8415              a sub-type to its base type as generated by the Ada FE.  */
8416           && !(INTEGRAL_TYPE_P (TREE_TYPE (op0))
8417                && TREE_TYPE (TREE_TYPE (op0))))
8418         return fold_convert (type, op0);
8419
8420       /* Strip inner integral conversions that do not change the precision.  */
8421       if (CONVERT_EXPR_P (op0)
8422           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8423               || POINTER_TYPE_P (TREE_TYPE (op0)))
8424           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8425               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8426           && (TYPE_PRECISION (TREE_TYPE (op0))
8427               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8428         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8429
8430       return fold_view_convert_expr (type, op0);
8431
8432     case NEGATE_EXPR:
8433       tem = fold_negate_expr (arg0);
8434       if (tem)
8435         return fold_convert (type, tem);
8436       return NULL_TREE;
8437
8438     case ABS_EXPR:
8439       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8440         return fold_abs_const (arg0, type);
8441       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8442         return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8443       /* Convert fabs((double)float) into (double)fabsf(float).  */
8444       else if (TREE_CODE (arg0) == NOP_EXPR
8445                && TREE_CODE (type) == REAL_TYPE)
8446         {
8447           tree targ0 = strip_float_extensions (arg0);
8448           if (targ0 != arg0)
8449             return fold_convert (type, fold_build1 (ABS_EXPR,
8450                                                     TREE_TYPE (targ0),
8451                                                     targ0));
8452         }
8453       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8454       else if (TREE_CODE (arg0) == ABS_EXPR)
8455         return arg0;
8456       else if (tree_expr_nonnegative_p (arg0))
8457         return arg0;
8458
8459       /* Strip sign ops from argument.  */
8460       if (TREE_CODE (type) == REAL_TYPE)
8461         {
8462           tem = fold_strip_sign_ops (arg0);
8463           if (tem)
8464             return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
8465         }
8466       return NULL_TREE;
8467
8468     case CONJ_EXPR:
8469       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8470         return fold_convert (type, arg0);
8471       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8472         {
8473           tree itype = TREE_TYPE (type);
8474           tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
8475           tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
8476           return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
8477         }
8478       if (TREE_CODE (arg0) == COMPLEX_CST)
8479         {
8480           tree itype = TREE_TYPE (type);
8481           tree rpart = fold_convert (itype, TREE_REALPART (arg0));
8482           tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
8483           return build_complex (type, rpart, negate_expr (ipart));
8484         }
8485       if (TREE_CODE (arg0) == CONJ_EXPR)
8486         return fold_convert (type, TREE_OPERAND (arg0, 0));
8487       return NULL_TREE;
8488
8489     case BIT_NOT_EXPR:
8490       if (TREE_CODE (arg0) == INTEGER_CST)
8491         return fold_not_const (arg0, type);
8492       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8493         return fold_convert (type, TREE_OPERAND (arg0, 0));
8494       /* Convert ~ (-A) to A - 1.  */
8495       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8496         return fold_build2 (MINUS_EXPR, type,
8497                             fold_convert (type, TREE_OPERAND (arg0, 0)),
8498                             build_int_cst (type, 1));
8499       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8500       else if (INTEGRAL_TYPE_P (type)
8501                && ((TREE_CODE (arg0) == MINUS_EXPR
8502                     && integer_onep (TREE_OPERAND (arg0, 1)))
8503                    || (TREE_CODE (arg0) == PLUS_EXPR
8504                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8505         return fold_build1 (NEGATE_EXPR, type,
8506                             fold_convert (type, TREE_OPERAND (arg0, 0)));
8507       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8508       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8509                && (tem = fold_unary (BIT_NOT_EXPR, type,
8510                                      fold_convert (type,
8511                                                    TREE_OPERAND (arg0, 0)))))
8512         return fold_build2 (BIT_XOR_EXPR, type, tem,
8513                             fold_convert (type, TREE_OPERAND (arg0, 1)));
8514       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8515                && (tem = fold_unary (BIT_NOT_EXPR, type,
8516                                      fold_convert (type,
8517                                                    TREE_OPERAND (arg0, 1)))))
8518         return fold_build2 (BIT_XOR_EXPR, type,
8519                             fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
8520       /* Perform BIT_NOT_EXPR on each element individually.  */
8521       else if (TREE_CODE (arg0) == VECTOR_CST)
8522         {
8523           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8524           int count = TYPE_VECTOR_SUBPARTS (type), i;
8525
8526           for (i = 0; i < count; i++)
8527             {
8528               if (elements)
8529                 {
8530                   elem = TREE_VALUE (elements);
8531                   elem = fold_unary (BIT_NOT_EXPR, TREE_TYPE (type), elem);
8532                   if (elem == NULL_TREE)
8533                     break;
8534                   elements = TREE_CHAIN (elements);
8535                 }
8536               else
8537                 elem = build_int_cst (TREE_TYPE (type), -1);
8538               list = tree_cons (NULL_TREE, elem, list);
8539             }
8540           if (i == count)
8541             return build_vector (type, nreverse (list));
8542         }
8543
8544       return NULL_TREE;
8545
8546     case TRUTH_NOT_EXPR:
8547       /* The argument to invert_truthvalue must have Boolean type.  */
8548       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8549           arg0 = fold_convert (boolean_type_node, arg0);
8550
8551       /* Note that the operand of this must be an int
8552          and its values must be 0 or 1.
8553          ("true" is a fixed value perhaps depending on the language,
8554          but we don't handle values other than 1 correctly yet.)  */
8555       tem = fold_truth_not_expr (arg0);
8556       if (!tem)
8557         return NULL_TREE;
8558       return fold_convert (type, tem);
8559
8560     case REALPART_EXPR:
8561       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8562         return fold_convert (type, arg0);
8563       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8564         return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8565                                  TREE_OPERAND (arg0, 1));
8566       if (TREE_CODE (arg0) == COMPLEX_CST)
8567         return fold_convert (type, TREE_REALPART (arg0));
8568       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8569         {
8570           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8571           tem = fold_build2 (TREE_CODE (arg0), itype,
8572                              fold_build1 (REALPART_EXPR, itype,
8573                                           TREE_OPERAND (arg0, 0)),
8574                              fold_build1 (REALPART_EXPR, itype,
8575                                           TREE_OPERAND (arg0, 1)));
8576           return fold_convert (type, tem);
8577         }
8578       if (TREE_CODE (arg0) == CONJ_EXPR)
8579         {
8580           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8581           tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8582           return fold_convert (type, tem);
8583         }
8584       if (TREE_CODE (arg0) == CALL_EXPR)
8585         {
8586           tree fn = get_callee_fndecl (arg0);
8587           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8588             switch (DECL_FUNCTION_CODE (fn))
8589               {
8590               CASE_FLT_FN (BUILT_IN_CEXPI):
8591                 fn = mathfn_built_in (type, BUILT_IN_COS);
8592                 if (fn)
8593                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8594                 break;
8595
8596               default:
8597                 break;
8598               }
8599         }
8600       return NULL_TREE;
8601
8602     case IMAGPART_EXPR:
8603       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8604         return fold_convert (type, integer_zero_node);
8605       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8606         return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8607                                  TREE_OPERAND (arg0, 0));
8608       if (TREE_CODE (arg0) == COMPLEX_CST)
8609         return fold_convert (type, TREE_IMAGPART (arg0));
8610       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8611         {
8612           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8613           tem = fold_build2 (TREE_CODE (arg0), itype,
8614                              fold_build1 (IMAGPART_EXPR, itype,
8615                                           TREE_OPERAND (arg0, 0)),
8616                              fold_build1 (IMAGPART_EXPR, itype,
8617                                           TREE_OPERAND (arg0, 1)));
8618           return fold_convert (type, tem);
8619         }
8620       if (TREE_CODE (arg0) == CONJ_EXPR)
8621         {
8622           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8623           tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8624           return fold_convert (type, negate_expr (tem));
8625         }
8626       if (TREE_CODE (arg0) == CALL_EXPR)
8627         {
8628           tree fn = get_callee_fndecl (arg0);
8629           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8630             switch (DECL_FUNCTION_CODE (fn))
8631               {
8632               CASE_FLT_FN (BUILT_IN_CEXPI):
8633                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8634                 if (fn)
8635                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8636                 break;
8637
8638               default:
8639                 break;
8640               }
8641         }
8642       return NULL_TREE;
8643
8644     default:
8645       return NULL_TREE;
8646     } /* switch (code) */
8647 }
8648
8649
8650 /* If the operation was a conversion do _not_ mark a resulting constant
8651    with TREE_OVERFLOW if the original constant was not.  These conversions
8652    have implementation defined behavior and retaining the TREE_OVERFLOW
8653    flag here would confuse later passes such as VRP.  */
8654 tree
8655 fold_unary_ignore_overflow (enum tree_code code, tree type, tree op0)
8656 {
8657   tree res = fold_unary (code, type, op0);
8658   if (res
8659       && TREE_CODE (res) == INTEGER_CST
8660       && TREE_CODE (op0) == INTEGER_CST
8661       && CONVERT_EXPR_CODE_P (code))
8662     TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8663
8664   return res;
8665 }
8666
8667 /* Fold a binary expression of code CODE and type TYPE with operands
8668    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8669    Return the folded expression if folding is successful.  Otherwise,
8670    return NULL_TREE.  */
8671
8672 static tree
8673 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
8674 {
8675   enum tree_code compl_code;
8676
8677   if (code == MIN_EXPR)
8678     compl_code = MAX_EXPR;
8679   else if (code == MAX_EXPR)
8680     compl_code = MIN_EXPR;
8681   else
8682     gcc_unreachable ();
8683
8684   /* MIN (MAX (a, b), b) == b.  */
8685   if (TREE_CODE (op0) == compl_code
8686       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8687     return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
8688
8689   /* MIN (MAX (b, a), b) == b.  */
8690   if (TREE_CODE (op0) == compl_code
8691       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8692       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8693     return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
8694
8695   /* MIN (a, MAX (a, b)) == a.  */
8696   if (TREE_CODE (op1) == compl_code
8697       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8698       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8699     return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
8700
8701   /* MIN (a, MAX (b, a)) == a.  */
8702   if (TREE_CODE (op1) == compl_code
8703       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8704       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8705     return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
8706
8707   return NULL_TREE;
8708 }
8709
8710 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8711    by changing CODE to reduce the magnitude of constants involved in
8712    ARG0 of the comparison.
8713    Returns a canonicalized comparison tree if a simplification was
8714    possible, otherwise returns NULL_TREE.
8715    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8716    valid if signed overflow is undefined.  */
8717
8718 static tree
8719 maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
8720                                  tree arg0, tree arg1,
8721                                  bool *strict_overflow_p)
8722 {
8723   enum tree_code code0 = TREE_CODE (arg0);
8724   tree t, cst0 = NULL_TREE;
8725   int sgn0;
8726   bool swap = false;
8727
8728   /* Match A +- CST code arg1 and CST code arg1.  We can change the
8729      first form only if overflow is undefined.  */
8730   if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8731          /* In principle pointers also have undefined overflow behavior,
8732             but that causes problems elsewhere.  */
8733          && !POINTER_TYPE_P (TREE_TYPE (arg0))
8734          && (code0 == MINUS_EXPR
8735              || code0 == PLUS_EXPR)
8736          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8737         || code0 == INTEGER_CST))
8738     return NULL_TREE;
8739
8740   /* Identify the constant in arg0 and its sign.  */
8741   if (code0 == INTEGER_CST)
8742     cst0 = arg0;
8743   else
8744     cst0 = TREE_OPERAND (arg0, 1);
8745   sgn0 = tree_int_cst_sgn (cst0);
8746
8747   /* Overflowed constants and zero will cause problems.  */
8748   if (integer_zerop (cst0)
8749       || TREE_OVERFLOW (cst0))
8750     return NULL_TREE;
8751
8752   /* See if we can reduce the magnitude of the constant in
8753      arg0 by changing the comparison code.  */
8754   if (code0 == INTEGER_CST)
8755     {
8756       /* CST <= arg1  ->  CST-1 < arg1.  */
8757       if (code == LE_EXPR && sgn0 == 1)
8758         code = LT_EXPR;
8759       /* -CST < arg1  ->  -CST-1 <= arg1.  */
8760       else if (code == LT_EXPR && sgn0 == -1)
8761         code = LE_EXPR;
8762       /* CST > arg1  ->  CST-1 >= arg1.  */
8763       else if (code == GT_EXPR && sgn0 == 1)
8764         code = GE_EXPR;
8765       /* -CST >= arg1  ->  -CST-1 > arg1.  */
8766       else if (code == GE_EXPR && sgn0 == -1)
8767         code = GT_EXPR;
8768       else
8769         return NULL_TREE;
8770       /* arg1 code' CST' might be more canonical.  */
8771       swap = true;
8772     }
8773   else
8774     {
8775       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
8776       if (code == LT_EXPR
8777           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8778         code = LE_EXPR;
8779       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
8780       else if (code == GT_EXPR
8781                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8782         code = GE_EXPR;
8783       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
8784       else if (code == LE_EXPR
8785                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8786         code = LT_EXPR;
8787       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
8788       else if (code == GE_EXPR
8789                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8790         code = GT_EXPR;
8791       else
8792         return NULL_TREE;
8793       *strict_overflow_p = true;
8794     }
8795
8796   /* Now build the constant reduced in magnitude.  But not if that
8797      would produce one outside of its types range.  */
8798   if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8799       && ((sgn0 == 1
8800            && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8801            && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8802           || (sgn0 == -1
8803               && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8804               && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8805     /* We cannot swap the comparison here as that would cause us to
8806        endlessly recurse.  */
8807     return NULL_TREE;
8808
8809   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8810                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8811   if (code0 != INTEGER_CST)
8812     t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8813
8814   /* If swapping might yield to a more canonical form, do so.  */
8815   if (swap)
8816     return fold_build2 (swap_tree_comparison (code), type, arg1, t);
8817   else
8818     return fold_build2 (code, type, t, arg1);
8819 }
8820
8821 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8822    overflow further.  Try to decrease the magnitude of constants involved
8823    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8824    and put sole constants at the second argument position.
8825    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
8826
8827 static tree
8828 maybe_canonicalize_comparison (enum tree_code code, tree type,
8829                                tree arg0, tree arg1)
8830 {
8831   tree t;
8832   bool strict_overflow_p;
8833   const char * const warnmsg = G_("assuming signed overflow does not occur "
8834                                   "when reducing constant in comparison");
8835
8836   /* Try canonicalization by simplifying arg0.  */
8837   strict_overflow_p = false;
8838   t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1,
8839                                        &strict_overflow_p);
8840   if (t)
8841     {
8842       if (strict_overflow_p)
8843         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8844       return t;
8845     }
8846
8847   /* Try canonicalization by simplifying arg1 using the swapped
8848      comparison.  */
8849   code = swap_tree_comparison (code);
8850   strict_overflow_p = false;
8851   t = maybe_canonicalize_comparison_1 (code, type, arg1, arg0,
8852                                        &strict_overflow_p);
8853   if (t && strict_overflow_p)
8854     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8855   return t;
8856 }
8857
8858 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8859    space.  This is used to avoid issuing overflow warnings for
8860    expressions like &p->x which can not wrap.  */
8861
8862 static bool
8863 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8864 {
8865   unsigned HOST_WIDE_INT offset_low, total_low;
8866   HOST_WIDE_INT size, offset_high, total_high;
8867
8868   if (!POINTER_TYPE_P (TREE_TYPE (base)))
8869     return true;
8870
8871   if (bitpos < 0)
8872     return true;
8873
8874   if (offset == NULL_TREE)
8875     {
8876       offset_low = 0;
8877       offset_high = 0;
8878     }
8879   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8880     return true;
8881   else
8882     {
8883       offset_low = TREE_INT_CST_LOW (offset);
8884       offset_high = TREE_INT_CST_HIGH (offset);
8885     }
8886
8887   if (add_double_with_sign (offset_low, offset_high,
8888                             bitpos / BITS_PER_UNIT, 0,
8889                             &total_low, &total_high,
8890                             true))
8891     return true;
8892
8893   if (total_high != 0)
8894     return true;
8895
8896   size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8897   if (size <= 0)
8898     return true;
8899
8900   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8901      array.  */
8902   if (TREE_CODE (base) == ADDR_EXPR)
8903     {
8904       HOST_WIDE_INT base_size;
8905
8906       base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8907       if (base_size > 0 && size < base_size)
8908         size = base_size;
8909     }
8910
8911   return total_low > (unsigned HOST_WIDE_INT) size;
8912 }
8913
8914 /* Subroutine of fold_binary.  This routine performs all of the
8915    transformations that are common to the equality/inequality
8916    operators (EQ_EXPR and NE_EXPR) and the ordering operators
8917    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
8918    fold_binary should call fold_binary.  Fold a comparison with
8919    tree code CODE and type TYPE with operands OP0 and OP1.  Return
8920    the folded comparison or NULL_TREE.  */
8921
8922 static tree
8923 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
8924 {
8925   tree arg0, arg1, tem;
8926
8927   arg0 = op0;
8928   arg1 = op1;
8929
8930   STRIP_SIGN_NOPS (arg0);
8931   STRIP_SIGN_NOPS (arg1);
8932
8933   tem = fold_relational_const (code, type, arg0, arg1);
8934   if (tem != NULL_TREE)
8935     return tem;
8936
8937   /* If one arg is a real or integer constant, put it last.  */
8938   if (tree_swap_operands_p (arg0, arg1, true))
8939     return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8940
8941   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
8942   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8943       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8944           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8945           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8946       && (TREE_CODE (arg1) == INTEGER_CST
8947           && !TREE_OVERFLOW (arg1)))
8948     {
8949       tree const1 = TREE_OPERAND (arg0, 1);
8950       tree const2 = arg1;
8951       tree variable = TREE_OPERAND (arg0, 0);
8952       tree lhs;
8953       int lhs_add;
8954       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8955
8956       lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8957                          TREE_TYPE (arg1), const2, const1);
8958
8959       /* If the constant operation overflowed this can be
8960          simplified as a comparison against INT_MAX/INT_MIN.  */
8961       if (TREE_CODE (lhs) == INTEGER_CST
8962           && TREE_OVERFLOW (lhs))
8963         {
8964           int const1_sgn = tree_int_cst_sgn (const1);
8965           enum tree_code code2 = code;
8966
8967           /* Get the sign of the constant on the lhs if the
8968              operation were VARIABLE + CONST1.  */
8969           if (TREE_CODE (arg0) == MINUS_EXPR)
8970             const1_sgn = -const1_sgn;
8971
8972           /* The sign of the constant determines if we overflowed
8973              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8974              Canonicalize to the INT_MIN overflow by swapping the comparison
8975              if necessary.  */
8976           if (const1_sgn == -1)
8977             code2 = swap_tree_comparison (code);
8978
8979           /* We now can look at the canonicalized case
8980                VARIABLE + 1  CODE2  INT_MIN
8981              and decide on the result.  */
8982           if (code2 == LT_EXPR
8983               || code2 == LE_EXPR
8984               || code2 == EQ_EXPR)
8985             return omit_one_operand (type, boolean_false_node, variable);
8986           else if (code2 == NE_EXPR
8987                    || code2 == GE_EXPR
8988                    || code2 == GT_EXPR)
8989             return omit_one_operand (type, boolean_true_node, variable);
8990         }
8991
8992       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8993           && (TREE_CODE (lhs) != INTEGER_CST
8994               || !TREE_OVERFLOW (lhs)))
8995         {
8996           fold_overflow_warning (("assuming signed overflow does not occur "
8997                                   "when changing X +- C1 cmp C2 to "
8998                                   "X cmp C1 +- C2"),
8999                                  WARN_STRICT_OVERFLOW_COMPARISON);
9000           return fold_build2 (code, type, variable, lhs);
9001         }
9002     }
9003
9004   /* For comparisons of pointers we can decompose it to a compile time
9005      comparison of the base objects and the offsets into the object.
9006      This requires at least one operand being an ADDR_EXPR or a
9007      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
9008   if (POINTER_TYPE_P (TREE_TYPE (arg0))
9009       && (TREE_CODE (arg0) == ADDR_EXPR
9010           || TREE_CODE (arg1) == ADDR_EXPR
9011           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9012           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9013     {
9014       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9015       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
9016       enum machine_mode mode;
9017       int volatilep, unsignedp;
9018       bool indirect_base0 = false, indirect_base1 = false;
9019
9020       /* Get base and offset for the access.  Strip ADDR_EXPR for
9021          get_inner_reference, but put it back by stripping INDIRECT_REF
9022          off the base object if possible.  indirect_baseN will be true
9023          if baseN is not an address but refers to the object itself.  */
9024       base0 = arg0;
9025       if (TREE_CODE (arg0) == ADDR_EXPR)
9026         {
9027           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
9028                                        &bitsize, &bitpos0, &offset0, &mode,
9029                                        &unsignedp, &volatilep, false);
9030           if (TREE_CODE (base0) == INDIRECT_REF)
9031             base0 = TREE_OPERAND (base0, 0);
9032           else
9033             indirect_base0 = true;
9034         }
9035       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9036         {
9037           base0 = TREE_OPERAND (arg0, 0);
9038           offset0 = TREE_OPERAND (arg0, 1);
9039         }
9040
9041       base1 = arg1;
9042       if (TREE_CODE (arg1) == ADDR_EXPR)
9043         {
9044           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
9045                                        &bitsize, &bitpos1, &offset1, &mode,
9046                                        &unsignedp, &volatilep, false);
9047           if (TREE_CODE (base1) == INDIRECT_REF)
9048             base1 = TREE_OPERAND (base1, 0);
9049           else
9050             indirect_base1 = true;
9051         }
9052       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9053         {
9054           base1 = TREE_OPERAND (arg1, 0);
9055           offset1 = TREE_OPERAND (arg1, 1);
9056         }
9057
9058       /* If we have equivalent bases we might be able to simplify.  */
9059       if (indirect_base0 == indirect_base1
9060           && operand_equal_p (base0, base1, 0))
9061         {
9062           /* We can fold this expression to a constant if the non-constant
9063              offset parts are equal.  */
9064           if ((offset0 == offset1
9065                || (offset0 && offset1
9066                    && operand_equal_p (offset0, offset1, 0)))
9067               && (code == EQ_EXPR
9068                   || code == NE_EXPR
9069                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
9070                 
9071             {
9072               if (code != EQ_EXPR
9073                   && code != NE_EXPR
9074                   && bitpos0 != bitpos1
9075                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9076                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9077                 fold_overflow_warning (("assuming pointer wraparound does not "
9078                                         "occur when comparing P +- C1 with "
9079                                         "P +- C2"),
9080                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
9081
9082               switch (code)
9083                 {
9084                 case EQ_EXPR:
9085                   return constant_boolean_node (bitpos0 == bitpos1, type);
9086                 case NE_EXPR:
9087                   return constant_boolean_node (bitpos0 != bitpos1, type);
9088                 case LT_EXPR:
9089                   return constant_boolean_node (bitpos0 < bitpos1, type);
9090                 case LE_EXPR:
9091                   return constant_boolean_node (bitpos0 <= bitpos1, type);
9092                 case GE_EXPR:
9093                   return constant_boolean_node (bitpos0 >= bitpos1, type);
9094                 case GT_EXPR:
9095                   return constant_boolean_node (bitpos0 > bitpos1, type);
9096                 default:;
9097                 }
9098             }
9099           /* We can simplify the comparison to a comparison of the variable
9100              offset parts if the constant offset parts are equal.
9101              Be careful to use signed size type here because otherwise we
9102              mess with array offsets in the wrong way.  This is possible
9103              because pointer arithmetic is restricted to retain within an
9104              object and overflow on pointer differences is undefined as of
9105              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
9106           else if (bitpos0 == bitpos1
9107                    && ((code == EQ_EXPR || code == NE_EXPR)
9108                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
9109             {
9110               tree signed_size_type_node;
9111               signed_size_type_node = signed_type_for (size_type_node);
9112
9113               /* By converting to signed size type we cover middle-end pointer
9114                  arithmetic which operates on unsigned pointer types of size
9115                  type size and ARRAY_REF offsets which are properly sign or
9116                  zero extended from their type in case it is narrower than
9117                  size type.  */
9118               if (offset0 == NULL_TREE)
9119                 offset0 = build_int_cst (signed_size_type_node, 0);
9120               else
9121                 offset0 = fold_convert (signed_size_type_node, offset0);
9122               if (offset1 == NULL_TREE)
9123                 offset1 = build_int_cst (signed_size_type_node, 0);
9124               else
9125                 offset1 = fold_convert (signed_size_type_node, offset1);
9126
9127               if (code != EQ_EXPR
9128                   && code != NE_EXPR
9129                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9130                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9131                 fold_overflow_warning (("assuming pointer wraparound does not "
9132                                         "occur when comparing P +- C1 with "
9133                                         "P +- C2"),
9134                                        WARN_STRICT_OVERFLOW_COMPARISON);
9135
9136               return fold_build2 (code, type, offset0, offset1);
9137             }
9138         }
9139       /* For non-equal bases we can simplify if they are addresses
9140          of local binding decls or constants.  */
9141       else if (indirect_base0 && indirect_base1
9142                /* We know that !operand_equal_p (base0, base1, 0)
9143                   because the if condition was false.  But make
9144                   sure two decls are not the same.  */
9145                && base0 != base1
9146                && TREE_CODE (arg0) == ADDR_EXPR
9147                && TREE_CODE (arg1) == ADDR_EXPR
9148                && (((TREE_CODE (base0) == VAR_DECL
9149                      || TREE_CODE (base0) == PARM_DECL)
9150                     && (targetm.binds_local_p (base0)
9151                         || CONSTANT_CLASS_P (base1)))
9152                    || CONSTANT_CLASS_P (base0))
9153                && (((TREE_CODE (base1) == VAR_DECL
9154                      || TREE_CODE (base1) == PARM_DECL)
9155                     && (targetm.binds_local_p (base1)
9156                         || CONSTANT_CLASS_P (base0)))
9157                    || CONSTANT_CLASS_P (base1)))
9158         {
9159           if (code == EQ_EXPR)
9160             return omit_two_operands (type, boolean_false_node, arg0, arg1);
9161           else if (code == NE_EXPR)
9162             return omit_two_operands (type, boolean_true_node, arg0, arg1);
9163         }
9164       /* For equal offsets we can simplify to a comparison of the
9165          base addresses.  */
9166       else if (bitpos0 == bitpos1
9167                && (indirect_base0
9168                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9169                && (indirect_base1
9170                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9171                && ((offset0 == offset1)
9172                    || (offset0 && offset1
9173                        && operand_equal_p (offset0, offset1, 0))))
9174         {
9175           if (indirect_base0)
9176             base0 = build_fold_addr_expr (base0);
9177           if (indirect_base1)
9178             base1 = build_fold_addr_expr (base1);
9179           return fold_build2 (code, type, base0, base1);
9180         }
9181     }
9182
9183   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9184      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
9185      the resulting offset is smaller in absolute value than the
9186      original one.  */
9187   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9188       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9189       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9190           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9191       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9192       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9193           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9194     {
9195       tree const1 = TREE_OPERAND (arg0, 1);
9196       tree const2 = TREE_OPERAND (arg1, 1);
9197       tree variable1 = TREE_OPERAND (arg0, 0);
9198       tree variable2 = TREE_OPERAND (arg1, 0);
9199       tree cst;
9200       const char * const warnmsg = G_("assuming signed overflow does not "
9201                                       "occur when combining constants around "
9202                                       "a comparison");
9203
9204       /* Put the constant on the side where it doesn't overflow and is
9205          of lower absolute value than before.  */
9206       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9207                              ? MINUS_EXPR : PLUS_EXPR,
9208                              const2, const1, 0);
9209       if (!TREE_OVERFLOW (cst)
9210           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
9211         {
9212           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9213           return fold_build2 (code, type,
9214                               variable1,
9215                               fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
9216                                            variable2, cst));
9217         }
9218
9219       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9220                              ? MINUS_EXPR : PLUS_EXPR,
9221                              const1, const2, 0);
9222       if (!TREE_OVERFLOW (cst)
9223           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
9224         {
9225           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9226           return fold_build2 (code, type,
9227                               fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
9228                                            variable1, cst),
9229                               variable2);
9230         }
9231     }
9232
9233   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9234      signed arithmetic case.  That form is created by the compiler
9235      often enough for folding it to be of value.  One example is in
9236      computing loop trip counts after Operator Strength Reduction.  */
9237   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9238       && TREE_CODE (arg0) == MULT_EXPR
9239       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9240           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9241       && integer_zerop (arg1))
9242     {
9243       tree const1 = TREE_OPERAND (arg0, 1);
9244       tree const2 = arg1;                       /* zero */
9245       tree variable1 = TREE_OPERAND (arg0, 0);
9246       enum tree_code cmp_code = code;
9247
9248       gcc_assert (!integer_zerop (const1));
9249
9250       fold_overflow_warning (("assuming signed overflow does not occur when "
9251                               "eliminating multiplication in comparison "
9252                               "with zero"),
9253                              WARN_STRICT_OVERFLOW_COMPARISON);
9254
9255       /* If const1 is negative we swap the sense of the comparison.  */
9256       if (tree_int_cst_sgn (const1) < 0)
9257         cmp_code = swap_tree_comparison (cmp_code);
9258
9259       return fold_build2 (cmp_code, type, variable1, const2);
9260     }
9261
9262   tem = maybe_canonicalize_comparison (code, type, op0, op1);
9263   if (tem)
9264     return tem;
9265
9266   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9267     {
9268       tree targ0 = strip_float_extensions (arg0);
9269       tree targ1 = strip_float_extensions (arg1);
9270       tree newtype = TREE_TYPE (targ0);
9271
9272       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9273         newtype = TREE_TYPE (targ1);
9274
9275       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
9276       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9277         return fold_build2 (code, type, fold_convert (newtype, targ0),
9278                             fold_convert (newtype, targ1));
9279
9280       /* (-a) CMP (-b) -> b CMP a  */
9281       if (TREE_CODE (arg0) == NEGATE_EXPR
9282           && TREE_CODE (arg1) == NEGATE_EXPR)
9283         return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
9284                             TREE_OPERAND (arg0, 0));
9285
9286       if (TREE_CODE (arg1) == REAL_CST)
9287         {
9288           REAL_VALUE_TYPE cst;
9289           cst = TREE_REAL_CST (arg1);
9290
9291           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
9292           if (TREE_CODE (arg0) == NEGATE_EXPR)
9293             return fold_build2 (swap_tree_comparison (code), type,
9294                                 TREE_OPERAND (arg0, 0),
9295                                 build_real (TREE_TYPE (arg1),
9296                                             REAL_VALUE_NEGATE (cst)));
9297
9298           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
9299           /* a CMP (-0) -> a CMP 0  */
9300           if (REAL_VALUE_MINUS_ZERO (cst))
9301             return fold_build2 (code, type, arg0,
9302                                 build_real (TREE_TYPE (arg1), dconst0));
9303
9304           /* x != NaN is always true, other ops are always false.  */
9305           if (REAL_VALUE_ISNAN (cst)
9306               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9307             {
9308               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9309               return omit_one_operand (type, tem, arg0);
9310             }
9311
9312           /* Fold comparisons against infinity.  */
9313           if (REAL_VALUE_ISINF (cst)
9314               && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9315             {
9316               tem = fold_inf_compare (code, type, arg0, arg1);
9317               if (tem != NULL_TREE)
9318                 return tem;
9319             }
9320         }
9321
9322       /* If this is a comparison of a real constant with a PLUS_EXPR
9323          or a MINUS_EXPR of a real constant, we can convert it into a
9324          comparison with a revised real constant as long as no overflow
9325          occurs when unsafe_math_optimizations are enabled.  */
9326       if (flag_unsafe_math_optimizations
9327           && TREE_CODE (arg1) == REAL_CST
9328           && (TREE_CODE (arg0) == PLUS_EXPR
9329               || TREE_CODE (arg0) == MINUS_EXPR)
9330           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9331           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9332                                       ? MINUS_EXPR : PLUS_EXPR,
9333                                       arg1, TREE_OPERAND (arg0, 1), 0))
9334           && !TREE_OVERFLOW (tem))
9335         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9336
9337       /* Likewise, we can simplify a comparison of a real constant with
9338          a MINUS_EXPR whose first operand is also a real constant, i.e.
9339          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on 
9340          floating-point types only if -fassociative-math is set.  */
9341       if (flag_associative_math
9342           && TREE_CODE (arg1) == REAL_CST
9343           && TREE_CODE (arg0) == MINUS_EXPR
9344           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9345           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9346                                       arg1, 0))
9347           && !TREE_OVERFLOW (tem))
9348         return fold_build2 (swap_tree_comparison (code), type,
9349                             TREE_OPERAND (arg0, 1), tem);
9350
9351       /* Fold comparisons against built-in math functions.  */
9352       if (TREE_CODE (arg1) == REAL_CST
9353           && flag_unsafe_math_optimizations
9354           && ! flag_errno_math)
9355         {
9356           enum built_in_function fcode = builtin_mathfn_code (arg0);
9357
9358           if (fcode != END_BUILTINS)
9359             {
9360               tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
9361               if (tem != NULL_TREE)
9362                 return tem;
9363             }
9364         }
9365     }
9366
9367   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9368       && CONVERT_EXPR_P (arg0))
9369     {
9370       /* If we are widening one operand of an integer comparison,
9371          see if the other operand is similarly being widened.  Perhaps we
9372          can do the comparison in the narrower type.  */
9373       tem = fold_widened_comparison (code, type, arg0, arg1);
9374       if (tem)
9375         return tem;
9376
9377       /* Or if we are changing signedness.  */
9378       tem = fold_sign_changed_comparison (code, type, arg0, arg1);
9379       if (tem)
9380         return tem;
9381     }
9382
9383   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9384      constant, we can simplify it.  */
9385   if (TREE_CODE (arg1) == INTEGER_CST
9386       && (TREE_CODE (arg0) == MIN_EXPR
9387           || TREE_CODE (arg0) == MAX_EXPR)
9388       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9389     {
9390       tem = optimize_minmax_comparison (code, type, op0, op1);
9391       if (tem)
9392         return tem;
9393     }
9394
9395   /* Simplify comparison of something with itself.  (For IEEE
9396      floating-point, we can only do some of these simplifications.)  */
9397   if (operand_equal_p (arg0, arg1, 0))
9398     {
9399       switch (code)
9400         {
9401         case EQ_EXPR:
9402           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9403               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9404             return constant_boolean_node (1, type);
9405           break;
9406
9407         case GE_EXPR:
9408         case LE_EXPR:
9409           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9410               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9411             return constant_boolean_node (1, type);
9412           return fold_build2 (EQ_EXPR, type, arg0, arg1);
9413
9414         case NE_EXPR:
9415           /* For NE, we can only do this simplification if integer
9416              or we don't honor IEEE floating point NaNs.  */
9417           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9418               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9419             break;
9420           /* ... fall through ...  */
9421         case GT_EXPR:
9422         case LT_EXPR:
9423           return constant_boolean_node (0, type);
9424         default:
9425           gcc_unreachable ();
9426         }
9427     }
9428
9429   /* If we are comparing an expression that just has comparisons
9430      of two integer values, arithmetic expressions of those comparisons,
9431      and constants, we can simplify it.  There are only three cases
9432      to check: the two values can either be equal, the first can be
9433      greater, or the second can be greater.  Fold the expression for
9434      those three values.  Since each value must be 0 or 1, we have
9435      eight possibilities, each of which corresponds to the constant 0
9436      or 1 or one of the six possible comparisons.
9437
9438      This handles common cases like (a > b) == 0 but also handles
9439      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9440      occur in macroized code.  */
9441
9442   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9443     {
9444       tree cval1 = 0, cval2 = 0;
9445       int save_p = 0;
9446
9447       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9448           /* Don't handle degenerate cases here; they should already
9449              have been handled anyway.  */
9450           && cval1 != 0 && cval2 != 0
9451           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9452           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9453           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9454           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9455           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9456           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9457                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9458         {
9459           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9460           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9461
9462           /* We can't just pass T to eval_subst in case cval1 or cval2
9463              was the same as ARG1.  */
9464
9465           tree high_result
9466                 = fold_build2 (code, type,
9467                                eval_subst (arg0, cval1, maxval,
9468                                            cval2, minval),
9469                                arg1);
9470           tree equal_result
9471                 = fold_build2 (code, type,
9472                                eval_subst (arg0, cval1, maxval,
9473                                            cval2, maxval),
9474                                arg1);
9475           tree low_result
9476                 = fold_build2 (code, type,
9477                                eval_subst (arg0, cval1, minval,
9478                                            cval2, maxval),
9479                                arg1);
9480
9481           /* All three of these results should be 0 or 1.  Confirm they are.
9482              Then use those values to select the proper code to use.  */
9483
9484           if (TREE_CODE (high_result) == INTEGER_CST
9485               && TREE_CODE (equal_result) == INTEGER_CST
9486               && TREE_CODE (low_result) == INTEGER_CST)
9487             {
9488               /* Make a 3-bit mask with the high-order bit being the
9489                  value for `>', the next for '=', and the low for '<'.  */
9490               switch ((integer_onep (high_result) * 4)
9491                       + (integer_onep (equal_result) * 2)
9492                       + integer_onep (low_result))
9493                 {
9494                 case 0:
9495                   /* Always false.  */
9496                   return omit_one_operand (type, integer_zero_node, arg0);
9497                 case 1:
9498                   code = LT_EXPR;
9499                   break;
9500                 case 2:
9501                   code = EQ_EXPR;
9502                   break;
9503                 case 3:
9504                   code = LE_EXPR;
9505                   break;
9506                 case 4:
9507                   code = GT_EXPR;
9508                   break;
9509                 case 5:
9510                   code = NE_EXPR;
9511                   break;
9512                 case 6:
9513                   code = GE_EXPR;
9514                   break;
9515                 case 7:
9516                   /* Always true.  */
9517                   return omit_one_operand (type, integer_one_node, arg0);
9518                 }
9519
9520               if (save_p)
9521                 return save_expr (build2 (code, type, cval1, cval2));
9522               return fold_build2 (code, type, cval1, cval2);
9523             }
9524         }
9525     }
9526
9527   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9528      into a single range test.  */
9529   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9530        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9531       && TREE_CODE (arg1) == INTEGER_CST
9532       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9533       && !integer_zerop (TREE_OPERAND (arg0, 1))
9534       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9535       && !TREE_OVERFLOW (arg1))
9536     {
9537       tem = fold_div_compare (code, type, arg0, arg1);
9538       if (tem != NULL_TREE)
9539         return tem;
9540     }
9541
9542   /* Fold ~X op ~Y as Y op X.  */
9543   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9544       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9545     {
9546       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9547       return fold_build2 (code, type,
9548                           fold_convert (cmp_type, TREE_OPERAND (arg1, 0)),
9549                           TREE_OPERAND (arg0, 0));
9550     }
9551
9552   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9553   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9554       && TREE_CODE (arg1) == INTEGER_CST)
9555     {
9556       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9557       return fold_build2 (swap_tree_comparison (code), type,
9558                           TREE_OPERAND (arg0, 0),
9559                           fold_build1 (BIT_NOT_EXPR, cmp_type,
9560                                        fold_convert (cmp_type, arg1)));
9561     }
9562
9563   return NULL_TREE;
9564 }
9565
9566
9567 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9568    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9569    argument EXPR represents the expression "z" of type TYPE.  */
9570
9571 static tree
9572 fold_mult_zconjz (tree type, tree expr)
9573 {
9574   tree itype = TREE_TYPE (type);
9575   tree rpart, ipart, tem;
9576
9577   if (TREE_CODE (expr) == COMPLEX_EXPR)
9578     {
9579       rpart = TREE_OPERAND (expr, 0);
9580       ipart = TREE_OPERAND (expr, 1);
9581     }
9582   else if (TREE_CODE (expr) == COMPLEX_CST)
9583     {
9584       rpart = TREE_REALPART (expr);
9585       ipart = TREE_IMAGPART (expr);
9586     }
9587   else
9588     {
9589       expr = save_expr (expr);
9590       rpart = fold_build1 (REALPART_EXPR, itype, expr);
9591       ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
9592     }
9593
9594   rpart = save_expr (rpart);
9595   ipart = save_expr (ipart);
9596   tem = fold_build2 (PLUS_EXPR, itype,
9597                      fold_build2 (MULT_EXPR, itype, rpart, rpart),
9598                      fold_build2 (MULT_EXPR, itype, ipart, ipart));
9599   return fold_build2 (COMPLEX_EXPR, type, tem,
9600                       fold_convert (itype, integer_zero_node));
9601 }
9602
9603
9604 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9605    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9606    guarantees that P and N have the same least significant log2(M) bits.
9607    N is not otherwise constrained.  In particular, N is not normalized to
9608    0 <= N < M as is common.  In general, the precise value of P is unknown.
9609    M is chosen as large as possible such that constant N can be determined.
9610
9611    Returns M and sets *RESIDUE to N.
9612
9613    If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9614    account.  This is not always possible due to PR 35705.
9615  */
9616
9617 static unsigned HOST_WIDE_INT
9618 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9619                                  bool allow_func_align)
9620 {
9621   enum tree_code code;
9622
9623   *residue = 0;
9624
9625   code = TREE_CODE (expr);
9626   if (code == ADDR_EXPR)
9627     {
9628       expr = TREE_OPERAND (expr, 0);
9629       if (handled_component_p (expr))
9630         {
9631           HOST_WIDE_INT bitsize, bitpos;
9632           tree offset;
9633           enum machine_mode mode;
9634           int unsignedp, volatilep;
9635
9636           expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9637                                       &mode, &unsignedp, &volatilep, false);
9638           *residue = bitpos / BITS_PER_UNIT;
9639           if (offset)
9640             {
9641               if (TREE_CODE (offset) == INTEGER_CST)
9642                 *residue += TREE_INT_CST_LOW (offset);
9643               else
9644                 /* We don't handle more complicated offset expressions.  */
9645                 return 1;
9646             }
9647         }
9648
9649       if (DECL_P (expr)
9650           && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL))
9651         return DECL_ALIGN_UNIT (expr);
9652     }
9653   else if (code == POINTER_PLUS_EXPR)
9654     {
9655       tree op0, op1;
9656       unsigned HOST_WIDE_INT modulus;
9657       enum tree_code inner_code;
9658       
9659       op0 = TREE_OPERAND (expr, 0);
9660       STRIP_NOPS (op0);
9661       modulus = get_pointer_modulus_and_residue (op0, residue,
9662                                                  allow_func_align);
9663
9664       op1 = TREE_OPERAND (expr, 1);
9665       STRIP_NOPS (op1);
9666       inner_code = TREE_CODE (op1);
9667       if (inner_code == INTEGER_CST)
9668         {
9669           *residue += TREE_INT_CST_LOW (op1);
9670           return modulus;
9671         }
9672       else if (inner_code == MULT_EXPR)
9673         {
9674           op1 = TREE_OPERAND (op1, 1);
9675           if (TREE_CODE (op1) == INTEGER_CST)
9676             {
9677               unsigned HOST_WIDE_INT align;
9678               
9679               /* Compute the greatest power-of-2 divisor of op1.  */
9680               align = TREE_INT_CST_LOW (op1);
9681               align &= -align;
9682
9683               /* If align is non-zero and less than *modulus, replace
9684                  *modulus with align., If align is 0, then either op1 is 0
9685                  or the greatest power-of-2 divisor of op1 doesn't fit in an
9686                  unsigned HOST_WIDE_INT.  In either case, no additional
9687                  constraint is imposed.  */
9688               if (align)
9689                 modulus = MIN (modulus, align);
9690
9691               return modulus;
9692             }
9693         }
9694     }
9695
9696     /* If we get here, we were unable to determine anything useful about the
9697        expression.  */
9698     return 1;
9699 }
9700
9701
9702 /* Fold a binary expression of code CODE and type TYPE with operands
9703    OP0 and OP1.  Return the folded expression if folding is
9704    successful.  Otherwise, return NULL_TREE.  */
9705
9706 tree
9707 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
9708 {
9709   enum tree_code_class kind = TREE_CODE_CLASS (code);
9710   tree arg0, arg1, tem;
9711   tree t1 = NULL_TREE;
9712   bool strict_overflow_p;
9713
9714   gcc_assert (IS_EXPR_CODE_CLASS (kind)
9715               && TREE_CODE_LENGTH (code) == 2
9716               && op0 != NULL_TREE
9717               && op1 != NULL_TREE);
9718
9719   arg0 = op0;
9720   arg1 = op1;
9721
9722   /* Strip any conversions that don't change the mode.  This is
9723      safe for every expression, except for a comparison expression
9724      because its signedness is derived from its operands.  So, in
9725      the latter case, only strip conversions that don't change the
9726      signedness.  MIN_EXPR/MAX_EXPR also need signedness of arguments
9727      preserved.
9728
9729      Note that this is done as an internal manipulation within the
9730      constant folder, in order to find the simplest representation
9731      of the arguments so that their form can be studied.  In any
9732      cases, the appropriate type conversions should be put back in
9733      the tree that will get out of the constant folder.  */
9734
9735   if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9736     {
9737       STRIP_SIGN_NOPS (arg0);
9738       STRIP_SIGN_NOPS (arg1);
9739     }
9740   else
9741     {
9742       STRIP_NOPS (arg0);
9743       STRIP_NOPS (arg1);
9744     }
9745
9746   /* Note that TREE_CONSTANT isn't enough: static var addresses are
9747      constant but we can't do arithmetic on them.  */
9748   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9749       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9750       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9751       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9752       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9753       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9754     {
9755       if (kind == tcc_binary)
9756         {
9757           /* Make sure type and arg0 have the same saturating flag.  */
9758           gcc_assert (TYPE_SATURATING (type)
9759                       == TYPE_SATURATING (TREE_TYPE (arg0)));
9760           tem = const_binop (code, arg0, arg1, 0);
9761         }
9762       else if (kind == tcc_comparison)
9763         tem = fold_relational_const (code, type, arg0, arg1);
9764       else
9765         tem = NULL_TREE;
9766
9767       if (tem != NULL_TREE)
9768         {
9769           if (TREE_TYPE (tem) != type)
9770             tem = fold_convert (type, tem);
9771           return tem;
9772         }
9773     }
9774
9775   /* If this is a commutative operation, and ARG0 is a constant, move it
9776      to ARG1 to reduce the number of tests below.  */
9777   if (commutative_tree_code (code)
9778       && tree_swap_operands_p (arg0, arg1, true))
9779     return fold_build2 (code, type, op1, op0);
9780
9781   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9782
9783      First check for cases where an arithmetic operation is applied to a
9784      compound, conditional, or comparison operation.  Push the arithmetic
9785      operation inside the compound or conditional to see if any folding
9786      can then be done.  Convert comparison to conditional for this purpose.
9787      The also optimizes non-constant cases that used to be done in
9788      expand_expr.
9789
9790      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9791      one of the operands is a comparison and the other is a comparison, a
9792      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
9793      code below would make the expression more complex.  Change it to a
9794      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
9795      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
9796
9797   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9798        || code == EQ_EXPR || code == NE_EXPR)
9799       && ((truth_value_p (TREE_CODE (arg0))
9800            && (truth_value_p (TREE_CODE (arg1))
9801                || (TREE_CODE (arg1) == BIT_AND_EXPR
9802                    && integer_onep (TREE_OPERAND (arg1, 1)))))
9803           || (truth_value_p (TREE_CODE (arg1))
9804               && (truth_value_p (TREE_CODE (arg0))
9805                   || (TREE_CODE (arg0) == BIT_AND_EXPR
9806                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
9807     {
9808       tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9809                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9810                          : TRUTH_XOR_EXPR,
9811                          boolean_type_node,
9812                          fold_convert (boolean_type_node, arg0),
9813                          fold_convert (boolean_type_node, arg1));
9814
9815       if (code == EQ_EXPR)
9816         tem = invert_truthvalue (tem);
9817
9818       return fold_convert (type, tem);
9819     }
9820
9821   if (TREE_CODE_CLASS (code) == tcc_binary
9822       || TREE_CODE_CLASS (code) == tcc_comparison)
9823     {
9824       if (TREE_CODE (arg0) == COMPOUND_EXPR)
9825         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9826                        fold_build2 (code, type,
9827                                     fold_convert (TREE_TYPE (op0),
9828                                                   TREE_OPERAND (arg0, 1)),
9829                                     op1));
9830       if (TREE_CODE (arg1) == COMPOUND_EXPR
9831           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9832         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9833                        fold_build2 (code, type, op0,
9834                                     fold_convert (TREE_TYPE (op1),
9835                                                   TREE_OPERAND (arg1, 1))));
9836
9837       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9838         {
9839           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9840                                                      arg0, arg1, 
9841                                                      /*cond_first_p=*/1);
9842           if (tem != NULL_TREE)
9843             return tem;
9844         }
9845
9846       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9847         {
9848           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9849                                                      arg1, arg0, 
9850                                                      /*cond_first_p=*/0);
9851           if (tem != NULL_TREE)
9852             return tem;
9853         }
9854     }
9855
9856   switch (code)
9857     {
9858     case POINTER_PLUS_EXPR:
9859       /* 0 +p index -> (type)index */
9860       if (integer_zerop (arg0))
9861         return non_lvalue (fold_convert (type, arg1));
9862
9863       /* PTR +p 0 -> PTR */
9864       if (integer_zerop (arg1))
9865         return non_lvalue (fold_convert (type, arg0));
9866
9867       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
9868       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9869            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9870         return fold_convert (type, fold_build2 (PLUS_EXPR, sizetype,
9871                                                 fold_convert (sizetype, arg1),
9872                                                 fold_convert (sizetype, arg0)));
9873
9874       /* index +p PTR -> PTR +p index */
9875       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9876           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9877         return fold_build2 (POINTER_PLUS_EXPR, type,
9878                             fold_convert (type, arg1),
9879                             fold_convert (sizetype, arg0));
9880
9881       /* (PTR +p B) +p A -> PTR +p (B + A) */
9882       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9883         {
9884           tree inner;
9885           tree arg01 = fold_convert (sizetype, TREE_OPERAND (arg0, 1));
9886           tree arg00 = TREE_OPERAND (arg0, 0);
9887           inner = fold_build2 (PLUS_EXPR, sizetype,
9888                                arg01, fold_convert (sizetype, arg1));
9889           return fold_convert (type,
9890                                fold_build2 (POINTER_PLUS_EXPR,
9891                                             TREE_TYPE (arg00), arg00, inner));
9892         }
9893
9894       /* PTR_CST +p CST -> CST1 */
9895       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9896         return fold_build2 (PLUS_EXPR, type, arg0, fold_convert (type, arg1));
9897
9898      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9899         of the array.  Loop optimizer sometimes produce this type of
9900         expressions.  */
9901       if (TREE_CODE (arg0) == ADDR_EXPR)
9902         {
9903           tem = try_move_mult_to_index (arg0, fold_convert (sizetype, arg1));
9904           if (tem)
9905             return fold_convert (type, tem);
9906         }
9907
9908       return NULL_TREE;
9909
9910     case PLUS_EXPR:
9911       /* A + (-B) -> A - B */
9912       if (TREE_CODE (arg1) == NEGATE_EXPR)
9913         return fold_build2 (MINUS_EXPR, type,
9914                             fold_convert (type, arg0),
9915                             fold_convert (type, TREE_OPERAND (arg1, 0)));
9916       /* (-A) + B -> B - A */
9917       if (TREE_CODE (arg0) == NEGATE_EXPR
9918           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9919         return fold_build2 (MINUS_EXPR, type,
9920                             fold_convert (type, arg1),
9921                             fold_convert (type, TREE_OPERAND (arg0, 0)));
9922
9923       if (INTEGRAL_TYPE_P (type))
9924         {
9925           /* Convert ~A + 1 to -A.  */
9926           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9927               && integer_onep (arg1))
9928             return fold_build1 (NEGATE_EXPR, type,
9929                                 fold_convert (type, TREE_OPERAND (arg0, 0)));
9930
9931           /* ~X + X is -1.  */
9932           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9933               && !TYPE_OVERFLOW_TRAPS (type))
9934             {
9935               tree tem = TREE_OPERAND (arg0, 0);
9936
9937               STRIP_NOPS (tem);
9938               if (operand_equal_p (tem, arg1, 0))
9939                 {
9940                   t1 = build_int_cst_type (type, -1);
9941                   return omit_one_operand (type, t1, arg1);
9942                 }
9943             }
9944
9945           /* X + ~X is -1.  */
9946           if (TREE_CODE (arg1) == BIT_NOT_EXPR
9947               && !TYPE_OVERFLOW_TRAPS (type))
9948             {
9949               tree tem = TREE_OPERAND (arg1, 0);
9950
9951               STRIP_NOPS (tem);
9952               if (operand_equal_p (arg0, tem, 0))
9953                 {
9954                   t1 = build_int_cst_type (type, -1);
9955                   return omit_one_operand (type, t1, arg0);
9956                 }
9957             }
9958
9959           /* X + (X / CST) * -CST is X % CST.  */
9960           if (TREE_CODE (arg1) == MULT_EXPR
9961               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9962               && operand_equal_p (arg0,
9963                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9964             {
9965               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9966               tree cst1 = TREE_OPERAND (arg1, 1);
9967               tree sum = fold_binary (PLUS_EXPR, TREE_TYPE (cst1), cst1, cst0);
9968               if (sum && integer_zerop (sum))
9969                 return fold_convert (type,
9970                                      fold_build2 (TRUNC_MOD_EXPR,
9971                                                   TREE_TYPE (arg0), arg0, cst0));
9972             }
9973         }
9974
9975       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9976          same or one.  Make sure type is not saturating.
9977          fold_plusminus_mult_expr will re-associate.  */
9978       if ((TREE_CODE (arg0) == MULT_EXPR
9979            || TREE_CODE (arg1) == MULT_EXPR)
9980           && !TYPE_SATURATING (type)
9981           && (!FLOAT_TYPE_P (type) || flag_associative_math))
9982         {
9983           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9984           if (tem)
9985             return tem;
9986         }
9987
9988       if (! FLOAT_TYPE_P (type))
9989         {
9990           if (integer_zerop (arg1))
9991             return non_lvalue (fold_convert (type, arg0));
9992
9993           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9994              with a constant, and the two constants have no bits in common,
9995              we should treat this as a BIT_IOR_EXPR since this may produce more
9996              simplifications.  */
9997           if (TREE_CODE (arg0) == BIT_AND_EXPR
9998               && TREE_CODE (arg1) == BIT_AND_EXPR
9999               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10000               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10001               && integer_zerop (const_binop (BIT_AND_EXPR,
10002                                              TREE_OPERAND (arg0, 1),
10003                                              TREE_OPERAND (arg1, 1), 0)))
10004             {
10005               code = BIT_IOR_EXPR;
10006               goto bit_ior;
10007             }
10008
10009           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
10010              (plus (plus (mult) (mult)) (foo)) so that we can
10011              take advantage of the factoring cases below.  */
10012           if (((TREE_CODE (arg0) == PLUS_EXPR
10013                 || TREE_CODE (arg0) == MINUS_EXPR)
10014                && TREE_CODE (arg1) == MULT_EXPR)
10015               || ((TREE_CODE (arg1) == PLUS_EXPR
10016                    || TREE_CODE (arg1) == MINUS_EXPR)
10017                   && TREE_CODE (arg0) == MULT_EXPR))
10018             {
10019               tree parg0, parg1, parg, marg;
10020               enum tree_code pcode;
10021
10022               if (TREE_CODE (arg1) == MULT_EXPR)
10023                 parg = arg0, marg = arg1;
10024               else
10025                 parg = arg1, marg = arg0;
10026               pcode = TREE_CODE (parg);
10027               parg0 = TREE_OPERAND (parg, 0);
10028               parg1 = TREE_OPERAND (parg, 1);
10029               STRIP_NOPS (parg0);
10030               STRIP_NOPS (parg1);
10031
10032               if (TREE_CODE (parg0) == MULT_EXPR
10033                   && TREE_CODE (parg1) != MULT_EXPR)
10034                 return fold_build2 (pcode, type,
10035                                     fold_build2 (PLUS_EXPR, type,
10036                                                  fold_convert (type, parg0),
10037                                                  fold_convert (type, marg)),
10038                                     fold_convert (type, parg1));
10039               if (TREE_CODE (parg0) != MULT_EXPR
10040                   && TREE_CODE (parg1) == MULT_EXPR)
10041                 return fold_build2 (PLUS_EXPR, type,
10042                                     fold_convert (type, parg0),
10043                                     fold_build2 (pcode, type,
10044                                                  fold_convert (type, marg),
10045                                                  fold_convert (type,
10046                                                                parg1)));
10047             }
10048         }
10049       else
10050         {
10051           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
10052           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
10053             return non_lvalue (fold_convert (type, arg0));
10054
10055           /* Likewise if the operands are reversed.  */
10056           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10057             return non_lvalue (fold_convert (type, arg1));
10058
10059           /* Convert X + -C into X - C.  */
10060           if (TREE_CODE (arg1) == REAL_CST
10061               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
10062             {
10063               tem = fold_negate_const (arg1, type);
10064               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
10065                 return fold_build2 (MINUS_EXPR, type,
10066                                     fold_convert (type, arg0),
10067                                     fold_convert (type, tem));
10068             }
10069
10070           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10071              to __complex__ ( x, y ).  This is not the same for SNaNs or
10072              if signed zeros are involved.  */
10073           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10074               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10075               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10076             {
10077               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10078               tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10079               tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10080               bool arg0rz = false, arg0iz = false;
10081               if ((arg0r && (arg0rz = real_zerop (arg0r)))
10082                   || (arg0i && (arg0iz = real_zerop (arg0i))))
10083                 {
10084                   tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10085                   tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10086                   if (arg0rz && arg1i && real_zerop (arg1i))
10087                     {
10088                       tree rp = arg1r ? arg1r
10089                                   : build1 (REALPART_EXPR, rtype, arg1);
10090                       tree ip = arg0i ? arg0i
10091                                   : build1 (IMAGPART_EXPR, rtype, arg0);
10092                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10093                     }
10094                   else if (arg0iz && arg1r && real_zerop (arg1r))
10095                     {
10096                       tree rp = arg0r ? arg0r
10097                                   : build1 (REALPART_EXPR, rtype, arg0);
10098                       tree ip = arg1i ? arg1i
10099                                   : build1 (IMAGPART_EXPR, rtype, arg1);
10100                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10101                     }
10102                 }
10103             }
10104
10105           if (flag_unsafe_math_optimizations
10106               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10107               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10108               && (tem = distribute_real_division (code, type, arg0, arg1)))
10109             return tem;
10110
10111           /* Convert x+x into x*2.0.  */
10112           if (operand_equal_p (arg0, arg1, 0)
10113               && SCALAR_FLOAT_TYPE_P (type))
10114             return fold_build2 (MULT_EXPR, type, arg0,
10115                                 build_real (type, dconst2));
10116
10117           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.  
10118              We associate floats only if the user has specified
10119              -fassociative-math.  */
10120           if (flag_associative_math
10121               && TREE_CODE (arg1) == PLUS_EXPR
10122               && TREE_CODE (arg0) != MULT_EXPR)
10123             {
10124               tree tree10 = TREE_OPERAND (arg1, 0);
10125               tree tree11 = TREE_OPERAND (arg1, 1);
10126               if (TREE_CODE (tree11) == MULT_EXPR
10127                   && TREE_CODE (tree10) == MULT_EXPR)
10128                 {
10129                   tree tree0;
10130                   tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
10131                   return fold_build2 (PLUS_EXPR, type, tree0, tree11);
10132                 }
10133             }
10134           /* Convert (b*c + d*e) + a into b*c + (d*e +a).  
10135              We associate floats only if the user has specified
10136              -fassociative-math.  */
10137           if (flag_associative_math
10138               && TREE_CODE (arg0) == PLUS_EXPR
10139               && TREE_CODE (arg1) != MULT_EXPR)
10140             {
10141               tree tree00 = TREE_OPERAND (arg0, 0);
10142               tree tree01 = TREE_OPERAND (arg0, 1);
10143               if (TREE_CODE (tree01) == MULT_EXPR
10144                   && TREE_CODE (tree00) == MULT_EXPR)
10145                 {
10146                   tree tree0;
10147                   tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
10148                   return fold_build2 (PLUS_EXPR, type, tree00, tree0);
10149                 }
10150             }
10151         }
10152
10153      bit_rotate:
10154       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10155          is a rotate of A by C1 bits.  */
10156       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10157          is a rotate of A by B bits.  */
10158       {
10159         enum tree_code code0, code1;
10160         tree rtype;
10161         code0 = TREE_CODE (arg0);
10162         code1 = TREE_CODE (arg1);
10163         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10164              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10165             && operand_equal_p (TREE_OPERAND (arg0, 0),
10166                                 TREE_OPERAND (arg1, 0), 0)
10167             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10168                 TYPE_UNSIGNED (rtype))
10169             /* Only create rotates in complete modes.  Other cases are not
10170                expanded properly.  */
10171             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
10172           {
10173             tree tree01, tree11;
10174             enum tree_code code01, code11;
10175
10176             tree01 = TREE_OPERAND (arg0, 1);
10177             tree11 = TREE_OPERAND (arg1, 1);
10178             STRIP_NOPS (tree01);
10179             STRIP_NOPS (tree11);
10180             code01 = TREE_CODE (tree01);
10181             code11 = TREE_CODE (tree11);
10182             if (code01 == INTEGER_CST
10183                 && code11 == INTEGER_CST
10184                 && TREE_INT_CST_HIGH (tree01) == 0
10185                 && TREE_INT_CST_HIGH (tree11) == 0
10186                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
10187                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10188               return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
10189                              code0 == LSHIFT_EXPR ? tree01 : tree11);
10190             else if (code11 == MINUS_EXPR)
10191               {
10192                 tree tree110, tree111;
10193                 tree110 = TREE_OPERAND (tree11, 0);
10194                 tree111 = TREE_OPERAND (tree11, 1);
10195                 STRIP_NOPS (tree110);
10196                 STRIP_NOPS (tree111);
10197                 if (TREE_CODE (tree110) == INTEGER_CST
10198                     && 0 == compare_tree_int (tree110,
10199                                               TYPE_PRECISION
10200                                               (TREE_TYPE (TREE_OPERAND
10201                                                           (arg0, 0))))
10202                     && operand_equal_p (tree01, tree111, 0))
10203                   return build2 ((code0 == LSHIFT_EXPR
10204                                   ? LROTATE_EXPR
10205                                   : RROTATE_EXPR),
10206                                  type, TREE_OPERAND (arg0, 0), tree01);
10207               }
10208             else if (code01 == MINUS_EXPR)
10209               {
10210                 tree tree010, tree011;
10211                 tree010 = TREE_OPERAND (tree01, 0);
10212                 tree011 = TREE_OPERAND (tree01, 1);
10213                 STRIP_NOPS (tree010);
10214                 STRIP_NOPS (tree011);
10215                 if (TREE_CODE (tree010) == INTEGER_CST
10216                     && 0 == compare_tree_int (tree010,
10217                                               TYPE_PRECISION
10218                                               (TREE_TYPE (TREE_OPERAND
10219                                                           (arg0, 0))))
10220                     && operand_equal_p (tree11, tree011, 0))
10221                   return build2 ((code0 != LSHIFT_EXPR
10222                                   ? LROTATE_EXPR
10223                                   : RROTATE_EXPR),
10224                                  type, TREE_OPERAND (arg0, 0), tree11);
10225               }
10226           }
10227       }
10228
10229     associate:
10230       /* In most languages, can't associate operations on floats through
10231          parentheses.  Rather than remember where the parentheses were, we
10232          don't associate floats at all, unless the user has specified
10233          -fassociative-math.
10234          And, we need to make sure type is not saturating.  */
10235
10236       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10237           && !TYPE_SATURATING (type))
10238         {
10239           tree var0, con0, lit0, minus_lit0;
10240           tree var1, con1, lit1, minus_lit1;
10241           bool ok = true;
10242
10243           /* Split both trees into variables, constants, and literals.  Then
10244              associate each group together, the constants with literals,
10245              then the result with variables.  This increases the chances of
10246              literals being recombined later and of generating relocatable
10247              expressions for the sum of a constant and literal.  */
10248           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10249           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10250                              code == MINUS_EXPR);
10251
10252           /* With undefined overflow we can only associate constants
10253              with one variable.  */
10254           if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10255                || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10256               && var0 && var1)
10257             {
10258               tree tmp0 = var0;
10259               tree tmp1 = var1;
10260
10261               if (TREE_CODE (tmp0) == NEGATE_EXPR)
10262                 tmp0 = TREE_OPERAND (tmp0, 0);
10263               if (TREE_CODE (tmp1) == NEGATE_EXPR)
10264                 tmp1 = TREE_OPERAND (tmp1, 0);
10265               /* The only case we can still associate with two variables
10266                  is if they are the same, modulo negation.  */
10267               if (!operand_equal_p (tmp0, tmp1, 0))
10268                 ok = false;
10269             }
10270
10271           /* Only do something if we found more than two objects.  Otherwise,
10272              nothing has changed and we risk infinite recursion.  */
10273           if (ok
10274               && (2 < ((var0 != 0) + (var1 != 0)
10275                        + (con0 != 0) + (con1 != 0)
10276                        + (lit0 != 0) + (lit1 != 0)
10277                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
10278             {
10279               /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
10280               if (code == MINUS_EXPR)
10281                 code = PLUS_EXPR;
10282
10283               var0 = associate_trees (var0, var1, code, type);
10284               con0 = associate_trees (con0, con1, code, type);
10285               lit0 = associate_trees (lit0, lit1, code, type);
10286               minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
10287
10288               /* Preserve the MINUS_EXPR if the negative part of the literal is
10289                  greater than the positive part.  Otherwise, the multiplicative
10290                  folding code (i.e extract_muldiv) may be fooled in case
10291                  unsigned constants are subtracted, like in the following
10292                  example: ((X*2 + 4) - 8U)/2.  */
10293               if (minus_lit0 && lit0)
10294                 {
10295                   if (TREE_CODE (lit0) == INTEGER_CST
10296                       && TREE_CODE (minus_lit0) == INTEGER_CST
10297                       && tree_int_cst_lt (lit0, minus_lit0))
10298                     {
10299                       minus_lit0 = associate_trees (minus_lit0, lit0,
10300                                                     MINUS_EXPR, type);
10301                       lit0 = 0;
10302                     }
10303                   else
10304                     {
10305                       lit0 = associate_trees (lit0, minus_lit0,
10306                                               MINUS_EXPR, type);
10307                       minus_lit0 = 0;
10308                     }
10309                 }
10310               if (minus_lit0)
10311                 {
10312                   if (con0 == 0)
10313                     return fold_convert (type,
10314                                          associate_trees (var0, minus_lit0,
10315                                                           MINUS_EXPR, type));
10316                   else
10317                     {
10318                       con0 = associate_trees (con0, minus_lit0,
10319                                               MINUS_EXPR, type);
10320                       return fold_convert (type,
10321                                            associate_trees (var0, con0,
10322                                                             PLUS_EXPR, type));
10323                     }
10324                 }
10325
10326               con0 = associate_trees (con0, lit0, code, type);
10327               return fold_convert (type, associate_trees (var0, con0,
10328                                                           code, type));
10329             }
10330         }
10331
10332       return NULL_TREE;
10333
10334     case MINUS_EXPR:
10335       /* Pointer simplifications for subtraction, simple reassociations. */
10336       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10337         {
10338           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10339           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10340               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10341             {
10342               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10343               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10344               tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10345               tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10346               return fold_build2 (PLUS_EXPR, type,
10347                                   fold_build2 (MINUS_EXPR, type, arg00, arg10),
10348                                   fold_build2 (MINUS_EXPR, type, arg01, arg11));
10349             }
10350           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10351           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10352             {
10353               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10354               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10355               tree tmp = fold_binary (MINUS_EXPR, type, arg00, fold_convert (type, arg1));
10356               if (tmp)
10357                 return fold_build2 (PLUS_EXPR, type, tmp, arg01);
10358             }
10359         }
10360       /* A - (-B) -> A + B */
10361       if (TREE_CODE (arg1) == NEGATE_EXPR)
10362         return fold_build2 (PLUS_EXPR, type, op0,
10363                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10364       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10365       if (TREE_CODE (arg0) == NEGATE_EXPR
10366           && (FLOAT_TYPE_P (type)
10367               || INTEGRAL_TYPE_P (type))
10368           && negate_expr_p (arg1)
10369           && reorder_operands_p (arg0, arg1))
10370         return fold_build2 (MINUS_EXPR, type,
10371                             fold_convert (type, negate_expr (arg1)),
10372                             fold_convert (type, TREE_OPERAND (arg0, 0)));
10373       /* Convert -A - 1 to ~A.  */
10374       if (INTEGRAL_TYPE_P (type)
10375           && TREE_CODE (arg0) == NEGATE_EXPR
10376           && integer_onep (arg1)
10377           && !TYPE_OVERFLOW_TRAPS (type))
10378         return fold_build1 (BIT_NOT_EXPR, type,
10379                             fold_convert (type, TREE_OPERAND (arg0, 0)));
10380
10381       /* Convert -1 - A to ~A.  */
10382       if (INTEGRAL_TYPE_P (type)
10383           && integer_all_onesp (arg0))
10384         return fold_build1 (BIT_NOT_EXPR, type, op1);
10385
10386
10387       /* X - (X / CST) * CST is X % CST.  */
10388       if (INTEGRAL_TYPE_P (type)
10389           && TREE_CODE (arg1) == MULT_EXPR
10390           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10391           && operand_equal_p (arg0,
10392                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10393           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10394                               TREE_OPERAND (arg1, 1), 0))
10395         return fold_convert (type,
10396                              fold_build2 (TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10397                                           arg0, TREE_OPERAND (arg1, 1)));
10398
10399       if (! FLOAT_TYPE_P (type))
10400         {
10401           if (integer_zerop (arg0))
10402             return negate_expr (fold_convert (type, arg1));
10403           if (integer_zerop (arg1))
10404             return non_lvalue (fold_convert (type, arg0));
10405
10406           /* Fold A - (A & B) into ~B & A.  */
10407           if (!TREE_SIDE_EFFECTS (arg0)
10408               && TREE_CODE (arg1) == BIT_AND_EXPR)
10409             {
10410               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10411                 {
10412                   tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10413                   return fold_build2 (BIT_AND_EXPR, type,
10414                                       fold_build1 (BIT_NOT_EXPR, type, arg10),
10415                                       fold_convert (type, arg0));
10416                 }
10417               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10418                 {
10419                   tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10420                   return fold_build2 (BIT_AND_EXPR, type,
10421                                       fold_build1 (BIT_NOT_EXPR, type, arg11),
10422                                       fold_convert (type, arg0));
10423                 }
10424             }
10425
10426           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10427              any power of 2 minus 1.  */
10428           if (TREE_CODE (arg0) == BIT_AND_EXPR
10429               && TREE_CODE (arg1) == BIT_AND_EXPR
10430               && operand_equal_p (TREE_OPERAND (arg0, 0),
10431                                   TREE_OPERAND (arg1, 0), 0))
10432             {
10433               tree mask0 = TREE_OPERAND (arg0, 1);
10434               tree mask1 = TREE_OPERAND (arg1, 1);
10435               tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
10436
10437               if (operand_equal_p (tem, mask1, 0))
10438                 {
10439                   tem = fold_build2 (BIT_XOR_EXPR, type,
10440                                      TREE_OPERAND (arg0, 0), mask1);
10441                   return fold_build2 (MINUS_EXPR, type, tem, mask1);
10442                 }
10443             }
10444         }
10445
10446       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10447       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10448         return non_lvalue (fold_convert (type, arg0));
10449
10450       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10451          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10452          (-ARG1 + ARG0) reduces to -ARG1.  */
10453       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10454         return negate_expr (fold_convert (type, arg1));
10455
10456       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10457          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10458          signed zeros are involved.  */
10459       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10460           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10461           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10462         {
10463           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10464           tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10465           tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10466           bool arg0rz = false, arg0iz = false;
10467           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10468               || (arg0i && (arg0iz = real_zerop (arg0i))))
10469             {
10470               tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10471               tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10472               if (arg0rz && arg1i && real_zerop (arg1i))
10473                 {
10474                   tree rp = fold_build1 (NEGATE_EXPR, rtype,
10475                                          arg1r ? arg1r
10476                                          : build1 (REALPART_EXPR, rtype, arg1));
10477                   tree ip = arg0i ? arg0i
10478                     : build1 (IMAGPART_EXPR, rtype, arg0);
10479                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10480                 }
10481               else if (arg0iz && arg1r && real_zerop (arg1r))
10482                 {
10483                   tree rp = arg0r ? arg0r
10484                     : build1 (REALPART_EXPR, rtype, arg0);
10485                   tree ip = fold_build1 (NEGATE_EXPR, rtype,
10486                                          arg1i ? arg1i
10487                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10488                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10489                 }
10490             }
10491         }
10492
10493       /* Fold &x - &x.  This can happen from &x.foo - &x.
10494          This is unsafe for certain floats even in non-IEEE formats.
10495          In IEEE, it is unsafe because it does wrong for NaNs.
10496          Also note that operand_equal_p is always false if an operand
10497          is volatile.  */
10498
10499       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10500           && operand_equal_p (arg0, arg1, 0))
10501         return fold_convert (type, integer_zero_node);
10502
10503       /* A - B -> A + (-B) if B is easily negatable.  */
10504       if (negate_expr_p (arg1)
10505           && ((FLOAT_TYPE_P (type)
10506                /* Avoid this transformation if B is a positive REAL_CST.  */
10507                && (TREE_CODE (arg1) != REAL_CST
10508                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10509               || INTEGRAL_TYPE_P (type)))
10510         return fold_build2 (PLUS_EXPR, type,
10511                             fold_convert (type, arg0),
10512                             fold_convert (type, negate_expr (arg1)));
10513
10514       /* Try folding difference of addresses.  */
10515       {
10516         HOST_WIDE_INT diff;
10517
10518         if ((TREE_CODE (arg0) == ADDR_EXPR
10519              || TREE_CODE (arg1) == ADDR_EXPR)
10520             && ptr_difference_const (arg0, arg1, &diff))
10521           return build_int_cst_type (type, diff);
10522       }
10523
10524       /* Fold &a[i] - &a[j] to i-j.  */
10525       if (TREE_CODE (arg0) == ADDR_EXPR
10526           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10527           && TREE_CODE (arg1) == ADDR_EXPR
10528           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10529         {
10530           tree aref0 = TREE_OPERAND (arg0, 0);
10531           tree aref1 = TREE_OPERAND (arg1, 0);
10532           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10533                                TREE_OPERAND (aref1, 0), 0))
10534             {
10535               tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
10536               tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
10537               tree esz = array_ref_element_size (aref0);
10538               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10539               return fold_build2 (MULT_EXPR, type, diff,
10540                                   fold_convert (type, esz));
10541                                   
10542             }
10543         }
10544
10545       if (FLOAT_TYPE_P (type)
10546           && flag_unsafe_math_optimizations
10547           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10548           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10549           && (tem = distribute_real_division (code, type, arg0, arg1)))
10550         return tem;
10551
10552       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10553          same or one.  Make sure type is not saturating.
10554          fold_plusminus_mult_expr will re-associate.  */
10555       if ((TREE_CODE (arg0) == MULT_EXPR
10556            || TREE_CODE (arg1) == MULT_EXPR)
10557           && !TYPE_SATURATING (type)
10558           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10559         {
10560           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
10561           if (tem)
10562             return tem;
10563         }
10564
10565       goto associate;
10566
10567     case MULT_EXPR:
10568       /* (-A) * (-B) -> A * B  */
10569       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10570         return fold_build2 (MULT_EXPR, type,
10571                             fold_convert (type, TREE_OPERAND (arg0, 0)),
10572                             fold_convert (type, negate_expr (arg1)));
10573       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10574         return fold_build2 (MULT_EXPR, type,
10575                             fold_convert (type, negate_expr (arg0)),
10576                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10577
10578       if (! FLOAT_TYPE_P (type))
10579         {
10580           if (integer_zerop (arg1))
10581             return omit_one_operand (type, arg1, arg0);
10582           if (integer_onep (arg1))
10583             return non_lvalue (fold_convert (type, arg0));
10584           /* Transform x * -1 into -x.  Make sure to do the negation
10585              on the original operand with conversions not stripped
10586              because we can only strip non-sign-changing conversions.  */
10587           if (integer_all_onesp (arg1))
10588             return fold_convert (type, negate_expr (op0));
10589           /* Transform x * -C into -x * C if x is easily negatable.  */
10590           if (TREE_CODE (arg1) == INTEGER_CST
10591               && tree_int_cst_sgn (arg1) == -1
10592               && negate_expr_p (arg0)
10593               && (tem = negate_expr (arg1)) != arg1
10594               && !TREE_OVERFLOW (tem))
10595             return fold_build2 (MULT_EXPR, type,
10596                                 fold_convert (type, negate_expr (arg0)), tem);
10597
10598           /* (a * (1 << b)) is (a << b)  */
10599           if (TREE_CODE (arg1) == LSHIFT_EXPR
10600               && integer_onep (TREE_OPERAND (arg1, 0)))
10601             return fold_build2 (LSHIFT_EXPR, type, op0,
10602                                 TREE_OPERAND (arg1, 1));
10603           if (TREE_CODE (arg0) == LSHIFT_EXPR
10604               && integer_onep (TREE_OPERAND (arg0, 0)))
10605             return fold_build2 (LSHIFT_EXPR, type, op1,
10606                                 TREE_OPERAND (arg0, 1));
10607
10608           /* (A + A) * C -> A * 2 * C  */
10609           if (TREE_CODE (arg0) == PLUS_EXPR
10610               && TREE_CODE (arg1) == INTEGER_CST
10611               && operand_equal_p (TREE_OPERAND (arg0, 0),
10612                                   TREE_OPERAND (arg0, 1), 0))
10613             return fold_build2 (MULT_EXPR, type,
10614                                 omit_one_operand (type, TREE_OPERAND (arg0, 0),
10615                                                   TREE_OPERAND (arg0, 1)),
10616                                 fold_build2 (MULT_EXPR, type,
10617                                              build_int_cst (type, 2) , arg1));
10618
10619           strict_overflow_p = false;
10620           if (TREE_CODE (arg1) == INTEGER_CST
10621               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10622                                              &strict_overflow_p)))
10623             {
10624               if (strict_overflow_p)
10625                 fold_overflow_warning (("assuming signed overflow does not "
10626                                         "occur when simplifying "
10627                                         "multiplication"),
10628                                        WARN_STRICT_OVERFLOW_MISC);
10629               return fold_convert (type, tem);
10630             }
10631
10632           /* Optimize z * conj(z) for integer complex numbers.  */
10633           if (TREE_CODE (arg0) == CONJ_EXPR
10634               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10635             return fold_mult_zconjz (type, arg1);
10636           if (TREE_CODE (arg1) == CONJ_EXPR
10637               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10638             return fold_mult_zconjz (type, arg0);
10639         }
10640       else
10641         {
10642           /* Maybe fold x * 0 to 0.  The expressions aren't the same
10643              when x is NaN, since x * 0 is also NaN.  Nor are they the
10644              same in modes with signed zeros, since multiplying a
10645              negative value by 0 gives -0, not +0.  */
10646           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10647               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10648               && real_zerop (arg1))
10649             return omit_one_operand (type, arg1, arg0);
10650           /* In IEEE floating point, x*1 is not equivalent to x for snans.  */
10651           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10652               && real_onep (arg1))
10653             return non_lvalue (fold_convert (type, arg0));
10654
10655           /* Transform x * -1.0 into -x.  */
10656           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10657               && real_minus_onep (arg1))
10658             return fold_convert (type, negate_expr (arg0));
10659
10660           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
10661              the result for floating point types due to rounding so it is applied
10662              only if -fassociative-math was specify.  */
10663           if (flag_associative_math
10664               && TREE_CODE (arg0) == RDIV_EXPR
10665               && TREE_CODE (arg1) == REAL_CST
10666               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10667             {
10668               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10669                                       arg1, 0);
10670               if (tem)
10671                 return fold_build2 (RDIV_EXPR, type, tem,
10672                                     TREE_OPERAND (arg0, 1));
10673             }
10674
10675           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
10676           if (operand_equal_p (arg0, arg1, 0))
10677             {
10678               tree tem = fold_strip_sign_ops (arg0);
10679               if (tem != NULL_TREE)
10680                 {
10681                   tem = fold_convert (type, tem);
10682                   return fold_build2 (MULT_EXPR, type, tem, tem);
10683                 }
10684             }
10685
10686           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10687              This is not the same for NaNs or if signed zeros are
10688              involved.  */
10689           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10690               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10691               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10692               && TREE_CODE (arg1) == COMPLEX_CST
10693               && real_zerop (TREE_REALPART (arg1)))
10694             {
10695               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10696               if (real_onep (TREE_IMAGPART (arg1)))
10697                 return fold_build2 (COMPLEX_EXPR, type,
10698                                     negate_expr (fold_build1 (IMAGPART_EXPR,
10699                                                               rtype, arg0)),
10700                                     fold_build1 (REALPART_EXPR, rtype, arg0));
10701               else if (real_minus_onep (TREE_IMAGPART (arg1)))
10702                 return fold_build2 (COMPLEX_EXPR, type,
10703                                     fold_build1 (IMAGPART_EXPR, rtype, arg0),
10704                                     negate_expr (fold_build1 (REALPART_EXPR,
10705                                                               rtype, arg0)));
10706             }
10707
10708           /* Optimize z * conj(z) for floating point complex numbers.
10709              Guarded by flag_unsafe_math_optimizations as non-finite
10710              imaginary components don't produce scalar results.  */
10711           if (flag_unsafe_math_optimizations
10712               && TREE_CODE (arg0) == CONJ_EXPR
10713               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10714             return fold_mult_zconjz (type, arg1);
10715           if (flag_unsafe_math_optimizations
10716               && TREE_CODE (arg1) == CONJ_EXPR
10717               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10718             return fold_mult_zconjz (type, arg0);
10719
10720           if (flag_unsafe_math_optimizations)
10721             {
10722               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10723               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10724
10725               /* Optimizations of root(...)*root(...).  */
10726               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10727                 {
10728                   tree rootfn, arg;
10729                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10730                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10731
10732                   /* Optimize sqrt(x)*sqrt(x) as x.  */
10733                   if (BUILTIN_SQRT_P (fcode0)
10734                       && operand_equal_p (arg00, arg10, 0)
10735                       && ! HONOR_SNANS (TYPE_MODE (type)))
10736                     return arg00;
10737
10738                   /* Optimize root(x)*root(y) as root(x*y).  */
10739                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10740                   arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10741                   return build_call_expr (rootfn, 1, arg);
10742                 }
10743
10744               /* Optimize expN(x)*expN(y) as expN(x+y).  */
10745               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10746                 {
10747                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10748                   tree arg = fold_build2 (PLUS_EXPR, type,
10749                                           CALL_EXPR_ARG (arg0, 0),
10750                                           CALL_EXPR_ARG (arg1, 0));
10751                   return build_call_expr (expfn, 1, arg);
10752                 }
10753
10754               /* Optimizations of pow(...)*pow(...).  */
10755               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10756                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10757                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10758                 {
10759                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10760                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10761                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10762                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10763
10764                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
10765                   if (operand_equal_p (arg01, arg11, 0))
10766                     {
10767                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10768                       tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10769                       return build_call_expr (powfn, 2, arg, arg01);
10770                     }
10771
10772                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
10773                   if (operand_equal_p (arg00, arg10, 0))
10774                     {
10775                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10776                       tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
10777                       return build_call_expr (powfn, 2, arg00, arg);
10778                     }
10779                 }
10780
10781               /* Optimize tan(x)*cos(x) as sin(x).  */
10782               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10783                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10784                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10785                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10786                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10787                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10788                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10789                                       CALL_EXPR_ARG (arg1, 0), 0))
10790                 {
10791                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10792
10793                   if (sinfn != NULL_TREE)
10794                     return build_call_expr (sinfn, 1, CALL_EXPR_ARG (arg0, 0));
10795                 }
10796
10797               /* Optimize x*pow(x,c) as pow(x,c+1).  */
10798               if (fcode1 == BUILT_IN_POW
10799                   || fcode1 == BUILT_IN_POWF
10800                   || fcode1 == BUILT_IN_POWL)
10801                 {
10802                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10803                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10804                   if (TREE_CODE (arg11) == REAL_CST
10805                       && !TREE_OVERFLOW (arg11)
10806                       && operand_equal_p (arg0, arg10, 0))
10807                     {
10808                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10809                       REAL_VALUE_TYPE c;
10810                       tree arg;
10811
10812                       c = TREE_REAL_CST (arg11);
10813                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10814                       arg = build_real (type, c);
10815                       return build_call_expr (powfn, 2, arg0, arg);
10816                     }
10817                 }
10818
10819               /* Optimize pow(x,c)*x as pow(x,c+1).  */
10820               if (fcode0 == BUILT_IN_POW
10821                   || fcode0 == BUILT_IN_POWF
10822                   || fcode0 == BUILT_IN_POWL)
10823                 {
10824                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10825                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10826                   if (TREE_CODE (arg01) == REAL_CST
10827                       && !TREE_OVERFLOW (arg01)
10828                       && operand_equal_p (arg1, arg00, 0))
10829                     {
10830                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10831                       REAL_VALUE_TYPE c;
10832                       tree arg;
10833
10834                       c = TREE_REAL_CST (arg01);
10835                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10836                       arg = build_real (type, c);
10837                       return build_call_expr (powfn, 2, arg1, arg);
10838                     }
10839                 }
10840
10841               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
10842               if (optimize_function_for_speed_p (cfun)
10843                   && operand_equal_p (arg0, arg1, 0))
10844                 {
10845                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10846
10847                   if (powfn)
10848                     {
10849                       tree arg = build_real (type, dconst2);
10850                       return build_call_expr (powfn, 2, arg0, arg);
10851                     }
10852                 }
10853             }
10854         }
10855       goto associate;
10856
10857     case BIT_IOR_EXPR:
10858     bit_ior:
10859       if (integer_all_onesp (arg1))
10860         return omit_one_operand (type, arg1, arg0);
10861       if (integer_zerop (arg1))
10862         return non_lvalue (fold_convert (type, arg0));
10863       if (operand_equal_p (arg0, arg1, 0))
10864         return non_lvalue (fold_convert (type, arg0));
10865
10866       /* ~X | X is -1.  */
10867       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10868           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10869         {
10870           t1 = fold_convert (type, integer_zero_node);
10871           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10872           return omit_one_operand (type, t1, arg1);
10873         }
10874
10875       /* X | ~X is -1.  */
10876       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10877           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10878         {
10879           t1 = fold_convert (type, integer_zero_node);
10880           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10881           return omit_one_operand (type, t1, arg0);
10882         }
10883
10884       /* Canonicalize (X & C1) | C2.  */
10885       if (TREE_CODE (arg0) == BIT_AND_EXPR
10886           && TREE_CODE (arg1) == INTEGER_CST
10887           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10888         {
10889           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10890           int width = TYPE_PRECISION (type), w;
10891           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10892           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10893           hi2 = TREE_INT_CST_HIGH (arg1);
10894           lo2 = TREE_INT_CST_LOW (arg1);
10895
10896           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
10897           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10898             return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10899
10900           if (width > HOST_BITS_PER_WIDE_INT)
10901             {
10902               mhi = (unsigned HOST_WIDE_INT) -1 
10903                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
10904               mlo = -1;
10905             }
10906           else
10907             {
10908               mhi = 0;
10909               mlo = (unsigned HOST_WIDE_INT) -1
10910                     >> (HOST_BITS_PER_WIDE_INT - width);
10911             }
10912
10913           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
10914           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10915             return fold_build2 (BIT_IOR_EXPR, type,
10916                                 TREE_OPERAND (arg0, 0), arg1);
10917
10918           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10919              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10920              mode which allows further optimizations.  */
10921           hi1 &= mhi;
10922           lo1 &= mlo;
10923           hi2 &= mhi;
10924           lo2 &= mlo;
10925           hi3 = hi1 & ~hi2;
10926           lo3 = lo1 & ~lo2;
10927           for (w = BITS_PER_UNIT;
10928                w <= width && w <= HOST_BITS_PER_WIDE_INT;
10929                w <<= 1)
10930             {
10931               unsigned HOST_WIDE_INT mask
10932                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10933               if (((lo1 | lo2) & mask) == mask
10934                   && (lo1 & ~mask) == 0 && hi1 == 0)
10935                 {
10936                   hi3 = 0;
10937                   lo3 = mask;
10938                   break;
10939                 }
10940             }
10941           if (hi3 != hi1 || lo3 != lo1)
10942             return fold_build2 (BIT_IOR_EXPR, type,
10943                                 fold_build2 (BIT_AND_EXPR, type,
10944                                              TREE_OPERAND (arg0, 0),
10945                                              build_int_cst_wide (type,
10946                                                                  lo3, hi3)),
10947                                 arg1);
10948         }
10949
10950       /* (X & Y) | Y is (X, Y).  */
10951       if (TREE_CODE (arg0) == BIT_AND_EXPR
10952           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10953         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10954       /* (X & Y) | X is (Y, X).  */
10955       if (TREE_CODE (arg0) == BIT_AND_EXPR
10956           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10957           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10958         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10959       /* X | (X & Y) is (Y, X).  */
10960       if (TREE_CODE (arg1) == BIT_AND_EXPR
10961           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10962           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10963         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10964       /* X | (Y & X) is (Y, X).  */
10965       if (TREE_CODE (arg1) == BIT_AND_EXPR
10966           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10967           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10968         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10969
10970       t1 = distribute_bit_expr (code, type, arg0, arg1);
10971       if (t1 != NULL_TREE)
10972         return t1;
10973
10974       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10975
10976          This results in more efficient code for machines without a NAND
10977          instruction.  Combine will canonicalize to the first form
10978          which will allow use of NAND instructions provided by the
10979          backend if they exist.  */
10980       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10981           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10982         {
10983           return fold_build1 (BIT_NOT_EXPR, type,
10984                               build2 (BIT_AND_EXPR, type,
10985                                       fold_convert (type,
10986                                                     TREE_OPERAND (arg0, 0)),
10987                                       fold_convert (type,
10988                                                     TREE_OPERAND (arg1, 0))));
10989         }
10990
10991       /* See if this can be simplified into a rotate first.  If that
10992          is unsuccessful continue in the association code.  */
10993       goto bit_rotate;
10994
10995     case BIT_XOR_EXPR:
10996       if (integer_zerop (arg1))
10997         return non_lvalue (fold_convert (type, arg0));
10998       if (integer_all_onesp (arg1))
10999         return fold_build1 (BIT_NOT_EXPR, type, op0);
11000       if (operand_equal_p (arg0, arg1, 0))
11001         return omit_one_operand (type, integer_zero_node, arg0);
11002
11003       /* ~X ^ X is -1.  */
11004       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11005           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11006         {
11007           t1 = fold_convert (type, integer_zero_node);
11008           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
11009           return omit_one_operand (type, t1, arg1);
11010         }
11011
11012       /* X ^ ~X is -1.  */
11013       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11014           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11015         {
11016           t1 = fold_convert (type, integer_zero_node);
11017           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
11018           return omit_one_operand (type, t1, arg0);
11019         }
11020
11021       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
11022          with a constant, and the two constants have no bits in common,
11023          we should treat this as a BIT_IOR_EXPR since this may produce more
11024          simplifications.  */
11025       if (TREE_CODE (arg0) == BIT_AND_EXPR
11026           && TREE_CODE (arg1) == BIT_AND_EXPR
11027           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11028           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
11029           && integer_zerop (const_binop (BIT_AND_EXPR,
11030                                          TREE_OPERAND (arg0, 1),
11031                                          TREE_OPERAND (arg1, 1), 0)))
11032         {
11033           code = BIT_IOR_EXPR;
11034           goto bit_ior;
11035         }
11036
11037       /* (X | Y) ^ X -> Y & ~ X*/
11038       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11039           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11040         {
11041           tree t2 = TREE_OPERAND (arg0, 1);
11042           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
11043                             arg1);
11044           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11045                             fold_convert (type, t1));
11046           return t1;
11047         }
11048
11049       /* (Y | X) ^ X -> Y & ~ X*/
11050       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11051           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11052         {
11053           tree t2 = TREE_OPERAND (arg0, 0);
11054           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
11055                             arg1);
11056           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11057                             fold_convert (type, t1));
11058           return t1;
11059         }
11060
11061       /* X ^ (X | Y) -> Y & ~ X*/
11062       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11063           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11064         {
11065           tree t2 = TREE_OPERAND (arg1, 1);
11066           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
11067                             arg0);
11068           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11069                             fold_convert (type, t1));
11070           return t1;
11071         }
11072
11073       /* X ^ (Y | X) -> Y & ~ X*/
11074       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11075           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11076         {
11077           tree t2 = TREE_OPERAND (arg1, 0);
11078           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
11079                             arg0);
11080           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11081                             fold_convert (type, t1));
11082           return t1;
11083         }
11084         
11085       /* Convert ~X ^ ~Y to X ^ Y.  */
11086       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11087           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11088         return fold_build2 (code, type,
11089                             fold_convert (type, TREE_OPERAND (arg0, 0)),
11090                             fold_convert (type, TREE_OPERAND (arg1, 0)));
11091
11092       /* Convert ~X ^ C to X ^ ~C.  */
11093       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11094           && TREE_CODE (arg1) == INTEGER_CST)
11095         return fold_build2 (code, type,
11096                             fold_convert (type, TREE_OPERAND (arg0, 0)),
11097                             fold_build1 (BIT_NOT_EXPR, type, arg1));
11098
11099       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
11100       if (TREE_CODE (arg0) == BIT_AND_EXPR
11101           && integer_onep (TREE_OPERAND (arg0, 1))
11102           && integer_onep (arg1))
11103         return fold_build2 (EQ_EXPR, type, arg0,
11104                             build_int_cst (TREE_TYPE (arg0), 0));
11105
11106       /* Fold (X & Y) ^ Y as ~X & Y.  */
11107       if (TREE_CODE (arg0) == BIT_AND_EXPR
11108           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11109         {
11110           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11111           return fold_build2 (BIT_AND_EXPR, type, 
11112                               fold_build1 (BIT_NOT_EXPR, type, tem),
11113                               fold_convert (type, arg1));
11114         }
11115       /* Fold (X & Y) ^ X as ~Y & X.  */
11116       if (TREE_CODE (arg0) == BIT_AND_EXPR
11117           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11118           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11119         {
11120           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11121           return fold_build2 (BIT_AND_EXPR, type,
11122                               fold_build1 (BIT_NOT_EXPR, type, tem),
11123                               fold_convert (type, arg1));
11124         }
11125       /* Fold X ^ (X & Y) as X & ~Y.  */
11126       if (TREE_CODE (arg1) == BIT_AND_EXPR
11127           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11128         {
11129           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11130           return fold_build2 (BIT_AND_EXPR, type,
11131                               fold_convert (type, arg0),
11132                               fold_build1 (BIT_NOT_EXPR, type, tem));
11133         }
11134       /* Fold X ^ (Y & X) as ~Y & X.  */
11135       if (TREE_CODE (arg1) == BIT_AND_EXPR
11136           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11137           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11138         {
11139           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11140           return fold_build2 (BIT_AND_EXPR, type,
11141                               fold_build1 (BIT_NOT_EXPR, type, tem),
11142                               fold_convert (type, arg0));
11143         }
11144
11145       /* See if this can be simplified into a rotate first.  If that
11146          is unsuccessful continue in the association code.  */
11147       goto bit_rotate;
11148
11149     case BIT_AND_EXPR:
11150       if (integer_all_onesp (arg1))
11151         return non_lvalue (fold_convert (type, arg0));
11152       if (integer_zerop (arg1))
11153         return omit_one_operand (type, arg1, arg0);
11154       if (operand_equal_p (arg0, arg1, 0))
11155         return non_lvalue (fold_convert (type, arg0));
11156
11157       /* ~X & X is always zero.  */
11158       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11159           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11160         return omit_one_operand (type, integer_zero_node, arg1);
11161
11162       /* X & ~X is always zero.  */
11163       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11164           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11165         return omit_one_operand (type, integer_zero_node, arg0);
11166
11167       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
11168       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11169           && TREE_CODE (arg1) == INTEGER_CST
11170           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11171         {
11172           tree tmp1 = fold_convert (type, arg1);
11173           tree tmp2 = fold_convert (type, TREE_OPERAND (arg0, 0));
11174           tree tmp3 = fold_convert (type, TREE_OPERAND (arg0, 1));
11175           tmp2 = fold_build2 (BIT_AND_EXPR, type, tmp2, tmp1);
11176           tmp3 = fold_build2 (BIT_AND_EXPR, type, tmp3, tmp1);
11177           return fold_convert (type,
11178                                fold_build2 (BIT_IOR_EXPR, type, tmp2, tmp3));
11179         }
11180
11181       /* (X | Y) & Y is (X, Y).  */
11182       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11183           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11184         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
11185       /* (X | Y) & X is (Y, X).  */
11186       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11187           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11188           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11189         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
11190       /* X & (X | Y) is (Y, X).  */
11191       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11192           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11193           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11194         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
11195       /* X & (Y | X) is (Y, X).  */
11196       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11197           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11198           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11199         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
11200
11201       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
11202       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11203           && integer_onep (TREE_OPERAND (arg0, 1))
11204           && integer_onep (arg1))
11205         {
11206           tem = TREE_OPERAND (arg0, 0);
11207           return fold_build2 (EQ_EXPR, type,
11208                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11209                                            build_int_cst (TREE_TYPE (tem), 1)),
11210                               build_int_cst (TREE_TYPE (tem), 0));
11211         }
11212       /* Fold ~X & 1 as (X & 1) == 0.  */
11213       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11214           && integer_onep (arg1))
11215         {
11216           tem = TREE_OPERAND (arg0, 0);
11217           return fold_build2 (EQ_EXPR, type,
11218                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11219                                            build_int_cst (TREE_TYPE (tem), 1)),
11220                               build_int_cst (TREE_TYPE (tem), 0));
11221         }
11222
11223       /* Fold (X ^ Y) & Y as ~X & Y.  */
11224       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11225           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11226         {
11227           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11228           return fold_build2 (BIT_AND_EXPR, type, 
11229                               fold_build1 (BIT_NOT_EXPR, type, tem),
11230                               fold_convert (type, arg1));
11231         }
11232       /* Fold (X ^ Y) & X as ~Y & X.  */
11233       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11234           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11235           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11236         {
11237           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11238           return fold_build2 (BIT_AND_EXPR, type,
11239                               fold_build1 (BIT_NOT_EXPR, type, tem),
11240                               fold_convert (type, arg1));
11241         }
11242       /* Fold X & (X ^ Y) as X & ~Y.  */
11243       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11244           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11245         {
11246           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11247           return fold_build2 (BIT_AND_EXPR, type,
11248                               fold_convert (type, arg0),
11249                               fold_build1 (BIT_NOT_EXPR, type, tem));
11250         }
11251       /* Fold X & (Y ^ X) as ~Y & X.  */
11252       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11253           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11254           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11255         {
11256           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11257           return fold_build2 (BIT_AND_EXPR, type,
11258                               fold_build1 (BIT_NOT_EXPR, type, tem),
11259                               fold_convert (type, arg0));
11260         }
11261
11262       t1 = distribute_bit_expr (code, type, arg0, arg1);
11263       if (t1 != NULL_TREE)
11264         return t1;
11265       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
11266       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11267           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11268         {
11269           unsigned int prec
11270             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11271
11272           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11273               && (~TREE_INT_CST_LOW (arg1)
11274                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11275             return fold_convert (type, TREE_OPERAND (arg0, 0));
11276         }
11277
11278       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11279
11280          This results in more efficient code for machines without a NOR
11281          instruction.  Combine will canonicalize to the first form
11282          which will allow use of NOR instructions provided by the
11283          backend if they exist.  */
11284       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11285           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11286         {
11287           return fold_build1 (BIT_NOT_EXPR, type,
11288                               build2 (BIT_IOR_EXPR, type,
11289                                       fold_convert (type,
11290                                                     TREE_OPERAND (arg0, 0)),
11291                                       fold_convert (type,
11292                                                     TREE_OPERAND (arg1, 0))));
11293         }
11294
11295       /* If arg0 is derived from the address of an object or function, we may
11296          be able to fold this expression using the object or function's
11297          alignment.  */
11298       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11299         {
11300           unsigned HOST_WIDE_INT modulus, residue;
11301           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11302
11303           modulus = get_pointer_modulus_and_residue (arg0, &residue,
11304                                                      integer_onep (arg1));
11305
11306           /* This works because modulus is a power of 2.  If this weren't the
11307              case, we'd have to replace it by its greatest power-of-2
11308              divisor: modulus & -modulus.  */
11309           if (low < modulus)
11310             return build_int_cst (type, residue & low);
11311         }
11312
11313       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11314               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11315          if the new mask might be further optimized.  */
11316       if ((TREE_CODE (arg0) == LSHIFT_EXPR
11317            || TREE_CODE (arg0) == RSHIFT_EXPR)
11318           && host_integerp (TREE_OPERAND (arg0, 1), 1)
11319           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11320           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11321              < TYPE_PRECISION (TREE_TYPE (arg0))
11322           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11323           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11324         {
11325           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11326           unsigned HOST_WIDE_INT mask
11327             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11328           unsigned HOST_WIDE_INT newmask, zerobits = 0;
11329           tree shift_type = TREE_TYPE (arg0);
11330
11331           if (TREE_CODE (arg0) == LSHIFT_EXPR)
11332             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11333           else if (TREE_CODE (arg0) == RSHIFT_EXPR
11334                    && TYPE_PRECISION (TREE_TYPE (arg0))
11335                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11336             {
11337               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11338               tree arg00 = TREE_OPERAND (arg0, 0);
11339               /* See if more bits can be proven as zero because of
11340                  zero extension.  */
11341               if (TREE_CODE (arg00) == NOP_EXPR
11342                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11343                 {
11344                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11345                   if (TYPE_PRECISION (inner_type)
11346                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11347                       && TYPE_PRECISION (inner_type) < prec)
11348                     {
11349                       prec = TYPE_PRECISION (inner_type);
11350                       /* See if we can shorten the right shift.  */
11351                       if (shiftc < prec)
11352                         shift_type = inner_type;
11353                     }
11354                 }
11355               zerobits = ~(unsigned HOST_WIDE_INT) 0;
11356               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11357               zerobits <<= prec - shiftc;
11358               /* For arithmetic shift if sign bit could be set, zerobits
11359                  can contain actually sign bits, so no transformation is
11360                  possible, unless MASK masks them all away.  In that
11361                  case the shift needs to be converted into logical shift.  */
11362               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11363                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11364                 {
11365                   if ((mask & zerobits) == 0)
11366                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
11367                   else
11368                     zerobits = 0;
11369                 }
11370             }
11371
11372           /* ((X << 16) & 0xff00) is (X, 0).  */
11373           if ((mask & zerobits) == mask)
11374             return omit_one_operand (type, build_int_cst (type, 0), arg0);
11375
11376           newmask = mask | zerobits;
11377           if (newmask != mask && (newmask & (newmask + 1)) == 0)
11378             {
11379               unsigned int prec;
11380
11381               /* Only do the transformation if NEWMASK is some integer
11382                  mode's mask.  */
11383               for (prec = BITS_PER_UNIT;
11384                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11385                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11386                   break;
11387               if (prec < HOST_BITS_PER_WIDE_INT
11388                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
11389                 {
11390                   if (shift_type != TREE_TYPE (arg0))
11391                     {
11392                       tem = fold_build2 (TREE_CODE (arg0), shift_type,
11393                                          fold_convert (shift_type,
11394                                                        TREE_OPERAND (arg0, 0)),
11395                                          TREE_OPERAND (arg0, 1));
11396                       tem = fold_convert (type, tem);
11397                     }
11398                   else
11399                     tem = op0;
11400                   return fold_build2 (BIT_AND_EXPR, type, tem,
11401                                       build_int_cst_type (TREE_TYPE (op1),
11402                                                           newmask));
11403                 }
11404             }
11405         }
11406
11407       goto associate;
11408
11409     case RDIV_EXPR:
11410       /* Don't touch a floating-point divide by zero unless the mode
11411          of the constant can represent infinity.  */
11412       if (TREE_CODE (arg1) == REAL_CST
11413           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11414           && real_zerop (arg1))
11415         return NULL_TREE;
11416
11417       /* Optimize A / A to 1.0 if we don't care about
11418          NaNs or Infinities.  Skip the transformation
11419          for non-real operands.  */
11420       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11421           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11422           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11423           && operand_equal_p (arg0, arg1, 0))
11424         {
11425           tree r = build_real (TREE_TYPE (arg0), dconst1);
11426
11427           return omit_two_operands (type, r, arg0, arg1);
11428         }
11429
11430       /* The complex version of the above A / A optimization.  */
11431       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11432           && operand_equal_p (arg0, arg1, 0))
11433         {
11434           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11435           if (! HONOR_NANS (TYPE_MODE (elem_type))
11436               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11437             {
11438               tree r = build_real (elem_type, dconst1);
11439               /* omit_two_operands will call fold_convert for us.  */
11440               return omit_two_operands (type, r, arg0, arg1);
11441             }
11442         }
11443
11444       /* (-A) / (-B) -> A / B  */
11445       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11446         return fold_build2 (RDIV_EXPR, type,
11447                             TREE_OPERAND (arg0, 0),
11448                             negate_expr (arg1));
11449       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11450         return fold_build2 (RDIV_EXPR, type,
11451                             negate_expr (arg0),
11452                             TREE_OPERAND (arg1, 0));
11453
11454       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
11455       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11456           && real_onep (arg1))
11457         return non_lvalue (fold_convert (type, arg0));
11458
11459       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
11460       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11461           && real_minus_onep (arg1))
11462         return non_lvalue (fold_convert (type, negate_expr (arg0)));
11463
11464       /* If ARG1 is a constant, we can convert this to a multiply by the
11465          reciprocal.  This does not have the same rounding properties,
11466          so only do this if -freciprocal-math.  We can actually
11467          always safely do it if ARG1 is a power of two, but it's hard to
11468          tell if it is or not in a portable manner.  */
11469       if (TREE_CODE (arg1) == REAL_CST)
11470         {
11471           if (flag_reciprocal_math
11472               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11473                                           arg1, 0)))
11474             return fold_build2 (MULT_EXPR, type, arg0, tem);
11475           /* Find the reciprocal if optimizing and the result is exact.  */
11476           if (optimize)
11477             {
11478               REAL_VALUE_TYPE r;
11479               r = TREE_REAL_CST (arg1);
11480               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11481                 {
11482                   tem = build_real (type, r);
11483                   return fold_build2 (MULT_EXPR, type,
11484                                       fold_convert (type, arg0), tem);
11485                 }
11486             }
11487         }
11488       /* Convert A/B/C to A/(B*C).  */ 
11489       if (flag_reciprocal_math
11490           && TREE_CODE (arg0) == RDIV_EXPR)
11491         return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11492                             fold_build2 (MULT_EXPR, type,
11493                                          TREE_OPERAND (arg0, 1), arg1));
11494
11495       /* Convert A/(B/C) to (A/B)*C.  */
11496       if (flag_reciprocal_math
11497           && TREE_CODE (arg1) == RDIV_EXPR)
11498         return fold_build2 (MULT_EXPR, type,
11499                             fold_build2 (RDIV_EXPR, type, arg0,
11500                                          TREE_OPERAND (arg1, 0)),
11501                             TREE_OPERAND (arg1, 1));
11502
11503       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11504       if (flag_reciprocal_math
11505           && TREE_CODE (arg1) == MULT_EXPR
11506           && TREE_CODE (arg0) == REAL_CST
11507           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11508         {
11509           tree tem = const_binop (RDIV_EXPR, arg0,
11510                                   TREE_OPERAND (arg1, 1), 0);
11511           if (tem)
11512             return fold_build2 (RDIV_EXPR, type, tem,
11513                                 TREE_OPERAND (arg1, 0));
11514         }
11515
11516       if (flag_unsafe_math_optimizations)
11517         {
11518           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11519           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11520
11521           /* Optimize sin(x)/cos(x) as tan(x).  */
11522           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11523                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11524                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11525               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11526                                   CALL_EXPR_ARG (arg1, 0), 0))
11527             {
11528               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11529
11530               if (tanfn != NULL_TREE)
11531                 return build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11532             }
11533
11534           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11535           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11536                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11537                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11538               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11539                                   CALL_EXPR_ARG (arg1, 0), 0))
11540             {
11541               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11542
11543               if (tanfn != NULL_TREE)
11544                 {
11545                   tree tmp = build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11546                   return fold_build2 (RDIV_EXPR, type,
11547                                       build_real (type, dconst1), tmp);
11548                 }
11549             }
11550
11551           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11552              NaNs or Infinities.  */
11553           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11554                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11555                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11556             {
11557               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11558               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11559
11560               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11561                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11562                   && operand_equal_p (arg00, arg01, 0))
11563                 {
11564                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11565
11566                   if (cosfn != NULL_TREE)
11567                     return build_call_expr (cosfn, 1, arg00);
11568                 }
11569             }
11570
11571           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11572              NaNs or Infinities.  */
11573           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11574                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11575                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11576             {
11577               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11578               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11579
11580               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11581                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11582                   && operand_equal_p (arg00, arg01, 0))
11583                 {
11584                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11585
11586                   if (cosfn != NULL_TREE)
11587                     {
11588                       tree tmp = build_call_expr (cosfn, 1, arg00);
11589                       return fold_build2 (RDIV_EXPR, type,
11590                                           build_real (type, dconst1),
11591                                           tmp);
11592                     }
11593                 }
11594             }
11595
11596           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11597           if (fcode0 == BUILT_IN_POW
11598               || fcode0 == BUILT_IN_POWF
11599               || fcode0 == BUILT_IN_POWL)
11600             {
11601               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11602               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11603               if (TREE_CODE (arg01) == REAL_CST
11604                   && !TREE_OVERFLOW (arg01)
11605                   && operand_equal_p (arg1, arg00, 0))
11606                 {
11607                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11608                   REAL_VALUE_TYPE c;
11609                   tree arg;
11610
11611                   c = TREE_REAL_CST (arg01);
11612                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11613                   arg = build_real (type, c);
11614                   return build_call_expr (powfn, 2, arg1, arg);
11615                 }
11616             }
11617
11618           /* Optimize a/root(b/c) into a*root(c/b).  */
11619           if (BUILTIN_ROOT_P (fcode1))
11620             {
11621               tree rootarg = CALL_EXPR_ARG (arg1, 0);
11622
11623               if (TREE_CODE (rootarg) == RDIV_EXPR)
11624                 {
11625                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11626                   tree b = TREE_OPERAND (rootarg, 0);
11627                   tree c = TREE_OPERAND (rootarg, 1);
11628
11629                   tree tmp = fold_build2 (RDIV_EXPR, type, c, b);
11630
11631                   tmp = build_call_expr (rootfn, 1, tmp);
11632                   return fold_build2 (MULT_EXPR, type, arg0, tmp);
11633                 }
11634             }
11635
11636           /* Optimize x/expN(y) into x*expN(-y).  */
11637           if (BUILTIN_EXPONENT_P (fcode1))
11638             {
11639               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11640               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11641               arg1 = build_call_expr (expfn, 1, fold_convert (type, arg));
11642               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11643             }
11644
11645           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
11646           if (fcode1 == BUILT_IN_POW
11647               || fcode1 == BUILT_IN_POWF
11648               || fcode1 == BUILT_IN_POWL)
11649             {
11650               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11651               tree arg10 = CALL_EXPR_ARG (arg1, 0);
11652               tree arg11 = CALL_EXPR_ARG (arg1, 1);
11653               tree neg11 = fold_convert (type, negate_expr (arg11));
11654               arg1 = build_call_expr (powfn, 2, arg10, neg11);
11655               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11656             }
11657         }
11658       return NULL_TREE;
11659
11660     case TRUNC_DIV_EXPR:
11661     case FLOOR_DIV_EXPR:
11662       /* Simplify A / (B << N) where A and B are positive and B is
11663          a power of 2, to A >> (N + log2(B)).  */
11664       strict_overflow_p = false;
11665       if (TREE_CODE (arg1) == LSHIFT_EXPR
11666           && (TYPE_UNSIGNED (type)
11667               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11668         {
11669           tree sval = TREE_OPERAND (arg1, 0);
11670           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11671             {
11672               tree sh_cnt = TREE_OPERAND (arg1, 1);
11673               unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11674
11675               if (strict_overflow_p)
11676                 fold_overflow_warning (("assuming signed overflow does not "
11677                                         "occur when simplifying A / (B << N)"),
11678                                        WARN_STRICT_OVERFLOW_MISC);
11679
11680               sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
11681                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
11682               return fold_build2 (RSHIFT_EXPR, type,
11683                                   fold_convert (type, arg0), sh_cnt);
11684             }
11685         }
11686
11687       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11688          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
11689       if (INTEGRAL_TYPE_P (type)
11690           && TYPE_UNSIGNED (type)
11691           && code == FLOOR_DIV_EXPR)
11692         return fold_build2 (TRUNC_DIV_EXPR, type, op0, op1);
11693
11694       /* Fall thru */
11695
11696     case ROUND_DIV_EXPR:
11697     case CEIL_DIV_EXPR:
11698     case EXACT_DIV_EXPR:
11699       if (integer_onep (arg1))
11700         return non_lvalue (fold_convert (type, arg0));
11701       if (integer_zerop (arg1))
11702         return NULL_TREE;
11703       /* X / -1 is -X.  */
11704       if (!TYPE_UNSIGNED (type)
11705           && TREE_CODE (arg1) == INTEGER_CST
11706           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11707           && TREE_INT_CST_HIGH (arg1) == -1)
11708         return fold_convert (type, negate_expr (arg0));
11709
11710       /* Convert -A / -B to A / B when the type is signed and overflow is
11711          undefined.  */
11712       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11713           && TREE_CODE (arg0) == NEGATE_EXPR
11714           && negate_expr_p (arg1))
11715         {
11716           if (INTEGRAL_TYPE_P (type))
11717             fold_overflow_warning (("assuming signed overflow does not occur "
11718                                     "when distributing negation across "
11719                                     "division"),
11720                                    WARN_STRICT_OVERFLOW_MISC);
11721           return fold_build2 (code, type,
11722                               fold_convert (type, TREE_OPERAND (arg0, 0)),
11723                               fold_convert (type, negate_expr (arg1)));
11724         }
11725       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11726           && TREE_CODE (arg1) == NEGATE_EXPR
11727           && negate_expr_p (arg0))
11728         {
11729           if (INTEGRAL_TYPE_P (type))
11730             fold_overflow_warning (("assuming signed overflow does not occur "
11731                                     "when distributing negation across "
11732                                     "division"),
11733                                    WARN_STRICT_OVERFLOW_MISC);
11734           return fold_build2 (code, type,
11735                               fold_convert (type, negate_expr (arg0)),
11736                               fold_convert (type, TREE_OPERAND (arg1, 0)));
11737         }
11738
11739       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11740          operation, EXACT_DIV_EXPR.
11741
11742          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11743          At one time others generated faster code, it's not clear if they do
11744          after the last round to changes to the DIV code in expmed.c.  */
11745       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11746           && multiple_of_p (type, arg0, arg1))
11747         return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
11748
11749       strict_overflow_p = false;
11750       if (TREE_CODE (arg1) == INTEGER_CST
11751           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11752                                          &strict_overflow_p)))
11753         {
11754           if (strict_overflow_p)
11755             fold_overflow_warning (("assuming signed overflow does not occur "
11756                                     "when simplifying division"),
11757                                    WARN_STRICT_OVERFLOW_MISC);
11758           return fold_convert (type, tem);
11759         }
11760
11761       return NULL_TREE;
11762
11763     case CEIL_MOD_EXPR:
11764     case FLOOR_MOD_EXPR:
11765     case ROUND_MOD_EXPR:
11766     case TRUNC_MOD_EXPR:
11767       /* X % 1 is always zero, but be sure to preserve any side
11768          effects in X.  */
11769       if (integer_onep (arg1))
11770         return omit_one_operand (type, integer_zero_node, arg0);
11771
11772       /* X % 0, return X % 0 unchanged so that we can get the
11773          proper warnings and errors.  */
11774       if (integer_zerop (arg1))
11775         return NULL_TREE;
11776
11777       /* 0 % X is always zero, but be sure to preserve any side
11778          effects in X.  Place this after checking for X == 0.  */
11779       if (integer_zerop (arg0))
11780         return omit_one_operand (type, integer_zero_node, arg1);
11781
11782       /* X % -1 is zero.  */
11783       if (!TYPE_UNSIGNED (type)
11784           && TREE_CODE (arg1) == INTEGER_CST
11785           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11786           && TREE_INT_CST_HIGH (arg1) == -1)
11787         return omit_one_operand (type, integer_zero_node, arg0);
11788
11789       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11790          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
11791       strict_overflow_p = false;
11792       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11793           && (TYPE_UNSIGNED (type)
11794               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11795         {
11796           tree c = arg1;
11797           /* Also optimize A % (C << N)  where C is a power of 2,
11798              to A & ((C << N) - 1).  */
11799           if (TREE_CODE (arg1) == LSHIFT_EXPR)
11800             c = TREE_OPERAND (arg1, 0);
11801
11802           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11803             {
11804               tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
11805                                        build_int_cst (TREE_TYPE (arg1), 1));
11806               if (strict_overflow_p)
11807                 fold_overflow_warning (("assuming signed overflow does not "
11808                                         "occur when simplifying "
11809                                         "X % (power of two)"),
11810                                        WARN_STRICT_OVERFLOW_MISC);
11811               return fold_build2 (BIT_AND_EXPR, type,
11812                                   fold_convert (type, arg0),
11813                                   fold_convert (type, mask));
11814             }
11815         }
11816
11817       /* X % -C is the same as X % C.  */
11818       if (code == TRUNC_MOD_EXPR
11819           && !TYPE_UNSIGNED (type)
11820           && TREE_CODE (arg1) == INTEGER_CST
11821           && !TREE_OVERFLOW (arg1)
11822           && TREE_INT_CST_HIGH (arg1) < 0
11823           && !TYPE_OVERFLOW_TRAPS (type)
11824           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
11825           && !sign_bit_p (arg1, arg1))
11826         return fold_build2 (code, type, fold_convert (type, arg0),
11827                             fold_convert (type, negate_expr (arg1)));
11828
11829       /* X % -Y is the same as X % Y.  */
11830       if (code == TRUNC_MOD_EXPR
11831           && !TYPE_UNSIGNED (type)
11832           && TREE_CODE (arg1) == NEGATE_EXPR
11833           && !TYPE_OVERFLOW_TRAPS (type))
11834         return fold_build2 (code, type, fold_convert (type, arg0),
11835                             fold_convert (type, TREE_OPERAND (arg1, 0)));
11836
11837       if (TREE_CODE (arg1) == INTEGER_CST
11838           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11839                                          &strict_overflow_p)))
11840         {
11841           if (strict_overflow_p)
11842             fold_overflow_warning (("assuming signed overflow does not occur "
11843                                     "when simplifying modulus"),
11844                                    WARN_STRICT_OVERFLOW_MISC);
11845           return fold_convert (type, tem);
11846         }
11847
11848       return NULL_TREE;
11849
11850     case LROTATE_EXPR:
11851     case RROTATE_EXPR:
11852       if (integer_all_onesp (arg0))
11853         return omit_one_operand (type, arg0, arg1);
11854       goto shift;
11855
11856     case RSHIFT_EXPR:
11857       /* Optimize -1 >> x for arithmetic right shifts.  */
11858       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11859           && tree_expr_nonnegative_p (arg1))
11860         return omit_one_operand (type, arg0, arg1);
11861       /* ... fall through ...  */
11862
11863     case LSHIFT_EXPR:
11864     shift:
11865       if (integer_zerop (arg1))
11866         return non_lvalue (fold_convert (type, arg0));
11867       if (integer_zerop (arg0))
11868         return omit_one_operand (type, arg0, arg1);
11869
11870       /* Since negative shift count is not well-defined,
11871          don't try to compute it in the compiler.  */
11872       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11873         return NULL_TREE;
11874
11875       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
11876       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11877           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11878           && host_integerp (TREE_OPERAND (arg0, 1), false)
11879           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11880         {
11881           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11882                                + TREE_INT_CST_LOW (arg1));
11883
11884           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11885              being well defined.  */
11886           if (low >= TYPE_PRECISION (type))
11887             {
11888               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11889                 low = low % TYPE_PRECISION (type);
11890               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11891                 return omit_one_operand (type, build_int_cst (type, 0),
11892                                          TREE_OPERAND (arg0, 0));
11893               else
11894                 low = TYPE_PRECISION (type) - 1;
11895             }
11896
11897           return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11898                               build_int_cst (type, low));
11899         }
11900
11901       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11902          into x & ((unsigned)-1 >> c) for unsigned types.  */
11903       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11904            || (TYPE_UNSIGNED (type)
11905                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11906           && host_integerp (arg1, false)
11907           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11908           && host_integerp (TREE_OPERAND (arg0, 1), false)
11909           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11910         {
11911           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11912           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11913           tree lshift;
11914           tree arg00;
11915
11916           if (low0 == low1)
11917             {
11918               arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
11919
11920               lshift = build_int_cst (type, -1);
11921               lshift = int_const_binop (code, lshift, arg1, 0);
11922
11923               return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
11924             }
11925         }
11926
11927       /* Rewrite an LROTATE_EXPR by a constant into an
11928          RROTATE_EXPR by a new constant.  */
11929       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11930         {
11931           tree tem = build_int_cst (TREE_TYPE (arg1),
11932                                     TYPE_PRECISION (type));
11933           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11934           return fold_build2 (RROTATE_EXPR, type, op0, tem);
11935         }
11936
11937       /* If we have a rotate of a bit operation with the rotate count and
11938          the second operand of the bit operation both constant,
11939          permute the two operations.  */
11940       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11941           && (TREE_CODE (arg0) == BIT_AND_EXPR
11942               || TREE_CODE (arg0) == BIT_IOR_EXPR
11943               || TREE_CODE (arg0) == BIT_XOR_EXPR)
11944           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11945         return fold_build2 (TREE_CODE (arg0), type,
11946                             fold_build2 (code, type,
11947                                          TREE_OPERAND (arg0, 0), arg1),
11948                             fold_build2 (code, type,
11949                                          TREE_OPERAND (arg0, 1), arg1));
11950
11951       /* Two consecutive rotates adding up to the precision of the
11952          type can be ignored.  */
11953       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11954           && TREE_CODE (arg0) == RROTATE_EXPR
11955           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11956           && TREE_INT_CST_HIGH (arg1) == 0
11957           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11958           && ((TREE_INT_CST_LOW (arg1)
11959                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11960               == (unsigned int) TYPE_PRECISION (type)))
11961         return TREE_OPERAND (arg0, 0);
11962
11963       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11964               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11965          if the latter can be further optimized.  */
11966       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11967           && TREE_CODE (arg0) == BIT_AND_EXPR
11968           && TREE_CODE (arg1) == INTEGER_CST
11969           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11970         {
11971           tree mask = fold_build2 (code, type,
11972                                    fold_convert (type, TREE_OPERAND (arg0, 1)),
11973                                    arg1);
11974           tree shift = fold_build2 (code, type,
11975                                     fold_convert (type, TREE_OPERAND (arg0, 0)),
11976                                     arg1);
11977           tem = fold_binary (BIT_AND_EXPR, type, shift, mask);
11978           if (tem)
11979             return tem;
11980         }
11981
11982       return NULL_TREE;
11983
11984     case MIN_EXPR:
11985       if (operand_equal_p (arg0, arg1, 0))
11986         return omit_one_operand (type, arg0, arg1);
11987       if (INTEGRAL_TYPE_P (type)
11988           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11989         return omit_one_operand (type, arg1, arg0);
11990       tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
11991       if (tem)
11992         return tem;
11993       goto associate;
11994
11995     case MAX_EXPR:
11996       if (operand_equal_p (arg0, arg1, 0))
11997         return omit_one_operand (type, arg0, arg1);
11998       if (INTEGRAL_TYPE_P (type)
11999           && TYPE_MAX_VALUE (type)
12000           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
12001         return omit_one_operand (type, arg1, arg0);
12002       tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
12003       if (tem)
12004         return tem;
12005       goto associate;
12006
12007     case TRUTH_ANDIF_EXPR:
12008       /* Note that the operands of this must be ints
12009          and their values must be 0 or 1.
12010          ("true" is a fixed value perhaps depending on the language.)  */
12011       /* If first arg is constant zero, return it.  */
12012       if (integer_zerop (arg0))
12013         return fold_convert (type, arg0);
12014     case TRUTH_AND_EXPR:
12015       /* If either arg is constant true, drop it.  */
12016       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12017         return non_lvalue (fold_convert (type, arg1));
12018       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12019           /* Preserve sequence points.  */
12020           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12021         return non_lvalue (fold_convert (type, arg0));
12022       /* If second arg is constant zero, result is zero, but first arg
12023          must be evaluated.  */
12024       if (integer_zerop (arg1))
12025         return omit_one_operand (type, arg1, arg0);
12026       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12027          case will be handled here.  */
12028       if (integer_zerop (arg0))
12029         return omit_one_operand (type, arg0, arg1);
12030
12031       /* !X && X is always false.  */
12032       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12033           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12034         return omit_one_operand (type, integer_zero_node, arg1);
12035       /* X && !X is always false.  */
12036       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12037           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12038         return omit_one_operand (type, integer_zero_node, arg0);
12039
12040       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
12041          means A >= Y && A != MAX, but in this case we know that
12042          A < X <= MAX.  */
12043
12044       if (!TREE_SIDE_EFFECTS (arg0)
12045           && !TREE_SIDE_EFFECTS (arg1))
12046         {
12047           tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
12048           if (tem && !operand_equal_p (tem, arg0, 0))
12049             return fold_build2 (code, type, tem, arg1);
12050
12051           tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
12052           if (tem && !operand_equal_p (tem, arg1, 0))
12053             return fold_build2 (code, type, arg0, tem);
12054         }
12055
12056     truth_andor:
12057       /* We only do these simplifications if we are optimizing.  */
12058       if (!optimize)
12059         return NULL_TREE;
12060
12061       /* Check for things like (A || B) && (A || C).  We can convert this
12062          to A || (B && C).  Note that either operator can be any of the four
12063          truth and/or operations and the transformation will still be
12064          valid.   Also note that we only care about order for the
12065          ANDIF and ORIF operators.  If B contains side effects, this
12066          might change the truth-value of A.  */
12067       if (TREE_CODE (arg0) == TREE_CODE (arg1)
12068           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
12069               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
12070               || TREE_CODE (arg0) == TRUTH_AND_EXPR
12071               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
12072           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
12073         {
12074           tree a00 = TREE_OPERAND (arg0, 0);
12075           tree a01 = TREE_OPERAND (arg0, 1);
12076           tree a10 = TREE_OPERAND (arg1, 0);
12077           tree a11 = TREE_OPERAND (arg1, 1);
12078           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
12079                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
12080                              && (code == TRUTH_AND_EXPR
12081                                  || code == TRUTH_OR_EXPR));
12082
12083           if (operand_equal_p (a00, a10, 0))
12084             return fold_build2 (TREE_CODE (arg0), type, a00,
12085                                 fold_build2 (code, type, a01, a11));
12086           else if (commutative && operand_equal_p (a00, a11, 0))
12087             return fold_build2 (TREE_CODE (arg0), type, a00,
12088                                 fold_build2 (code, type, a01, a10));
12089           else if (commutative && operand_equal_p (a01, a10, 0))
12090             return fold_build2 (TREE_CODE (arg0), type, a01,
12091                                 fold_build2 (code, type, a00, a11));
12092
12093           /* This case if tricky because we must either have commutative
12094              operators or else A10 must not have side-effects.  */
12095
12096           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12097                    && operand_equal_p (a01, a11, 0))
12098             return fold_build2 (TREE_CODE (arg0), type,
12099                                 fold_build2 (code, type, a00, a10),
12100                                 a01);
12101         }
12102
12103       /* See if we can build a range comparison.  */
12104       if (0 != (tem = fold_range_test (code, type, op0, op1)))
12105         return tem;
12106
12107       /* Check for the possibility of merging component references.  If our
12108          lhs is another similar operation, try to merge its rhs with our
12109          rhs.  Then try to merge our lhs and rhs.  */
12110       if (TREE_CODE (arg0) == code
12111           && 0 != (tem = fold_truthop (code, type,
12112                                        TREE_OPERAND (arg0, 1), arg1)))
12113         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12114
12115       if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
12116         return tem;
12117
12118       return NULL_TREE;
12119
12120     case TRUTH_ORIF_EXPR:
12121       /* Note that the operands of this must be ints
12122          and their values must be 0 or true.
12123          ("true" is a fixed value perhaps depending on the language.)  */
12124       /* If first arg is constant true, return it.  */
12125       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12126         return fold_convert (type, arg0);
12127     case TRUTH_OR_EXPR:
12128       /* If either arg is constant zero, drop it.  */
12129       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12130         return non_lvalue (fold_convert (type, arg1));
12131       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12132           /* Preserve sequence points.  */
12133           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12134         return non_lvalue (fold_convert (type, arg0));
12135       /* If second arg is constant true, result is true, but we must
12136          evaluate first arg.  */
12137       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12138         return omit_one_operand (type, arg1, arg0);
12139       /* Likewise for first arg, but note this only occurs here for
12140          TRUTH_OR_EXPR.  */
12141       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12142         return omit_one_operand (type, arg0, arg1);
12143
12144       /* !X || X is always true.  */
12145       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12146           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12147         return omit_one_operand (type, integer_one_node, arg1);
12148       /* X || !X is always true.  */
12149       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12150           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12151         return omit_one_operand (type, integer_one_node, arg0);
12152
12153       goto truth_andor;
12154
12155     case TRUTH_XOR_EXPR:
12156       /* If the second arg is constant zero, drop it.  */
12157       if (integer_zerop (arg1))
12158         return non_lvalue (fold_convert (type, arg0));
12159       /* If the second arg is constant true, this is a logical inversion.  */
12160       if (integer_onep (arg1))
12161         {
12162           /* Only call invert_truthvalue if operand is a truth value.  */
12163           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12164             tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12165           else
12166             tem = invert_truthvalue (arg0);
12167           return non_lvalue (fold_convert (type, tem));
12168         }
12169       /* Identical arguments cancel to zero.  */
12170       if (operand_equal_p (arg0, arg1, 0))
12171         return omit_one_operand (type, integer_zero_node, arg0);
12172
12173       /* !X ^ X is always true.  */
12174       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12175           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12176         return omit_one_operand (type, integer_one_node, arg1);
12177
12178       /* X ^ !X is always true.  */
12179       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12180           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12181         return omit_one_operand (type, integer_one_node, arg0);
12182
12183       return NULL_TREE;
12184
12185     case EQ_EXPR:
12186     case NE_EXPR:
12187       tem = fold_comparison (code, type, op0, op1);
12188       if (tem != NULL_TREE)
12189         return tem;
12190
12191       /* bool_var != 0 becomes bool_var. */
12192       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12193           && code == NE_EXPR)
12194         return non_lvalue (fold_convert (type, arg0));
12195
12196       /* bool_var == 1 becomes bool_var. */
12197       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12198           && code == EQ_EXPR)
12199         return non_lvalue (fold_convert (type, arg0));
12200
12201       /* bool_var != 1 becomes !bool_var. */
12202       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12203           && code == NE_EXPR)
12204         return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
12205
12206       /* bool_var == 0 becomes !bool_var. */
12207       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12208           && code == EQ_EXPR)
12209         return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
12210
12211       /* If this is an equality comparison of the address of two non-weak,
12212          unaliased symbols neither of which are extern (since we do not
12213          have access to attributes for externs), then we know the result.  */
12214       if (TREE_CODE (arg0) == ADDR_EXPR
12215           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12216           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12217           && ! lookup_attribute ("alias",
12218                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12219           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12220           && TREE_CODE (arg1) == ADDR_EXPR
12221           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12222           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12223           && ! lookup_attribute ("alias",
12224                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12225           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12226         {
12227           /* We know that we're looking at the address of two
12228              non-weak, unaliased, static _DECL nodes.
12229
12230              It is both wasteful and incorrect to call operand_equal_p
12231              to compare the two ADDR_EXPR nodes.  It is wasteful in that
12232              all we need to do is test pointer equality for the arguments
12233              to the two ADDR_EXPR nodes.  It is incorrect to use
12234              operand_equal_p as that function is NOT equivalent to a
12235              C equality test.  It can in fact return false for two
12236              objects which would test as equal using the C equality
12237              operator.  */
12238           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12239           return constant_boolean_node (equal
12240                                         ? code == EQ_EXPR : code != EQ_EXPR,
12241                                         type);
12242         }
12243
12244       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12245          a MINUS_EXPR of a constant, we can convert it into a comparison with
12246          a revised constant as long as no overflow occurs.  */
12247       if (TREE_CODE (arg1) == INTEGER_CST
12248           && (TREE_CODE (arg0) == PLUS_EXPR
12249               || TREE_CODE (arg0) == MINUS_EXPR)
12250           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12251           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12252                                       ? MINUS_EXPR : PLUS_EXPR,
12253                                       fold_convert (TREE_TYPE (arg0), arg1),
12254                                       TREE_OPERAND (arg0, 1), 0))
12255           && !TREE_OVERFLOW (tem))
12256         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12257
12258       /* Similarly for a NEGATE_EXPR.  */
12259       if (TREE_CODE (arg0) == NEGATE_EXPR
12260           && TREE_CODE (arg1) == INTEGER_CST
12261           && 0 != (tem = negate_expr (arg1))
12262           && TREE_CODE (tem) == INTEGER_CST
12263           && !TREE_OVERFLOW (tem))
12264         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12265
12266       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
12267       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12268           && TREE_CODE (arg1) == INTEGER_CST
12269           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12270         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12271                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
12272                                          fold_convert (TREE_TYPE (arg0), arg1),
12273                                          TREE_OPERAND (arg0, 1)));
12274
12275       /* Transform comparisons of the form X +- Y CMP X to Y CMP 0.  */
12276       if ((TREE_CODE (arg0) == PLUS_EXPR
12277            || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12278            || TREE_CODE (arg0) == MINUS_EXPR)
12279           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12280           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12281               || POINTER_TYPE_P (TREE_TYPE (arg0))))
12282         {
12283           tree val = TREE_OPERAND (arg0, 1);
12284           return omit_two_operands (type,
12285                                     fold_build2 (code, type,
12286                                                  val,
12287                                                  build_int_cst (TREE_TYPE (val),
12288                                                                 0)),
12289                                     TREE_OPERAND (arg0, 0), arg1);
12290         }
12291
12292       /* Transform comparisons of the form C - X CMP X if C % 2 == 1.  */
12293       if (TREE_CODE (arg0) == MINUS_EXPR
12294           && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12295           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)
12296           && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12297         {
12298           return omit_two_operands (type,
12299                                     code == NE_EXPR
12300                                     ? boolean_true_node : boolean_false_node,
12301                                     TREE_OPERAND (arg0, 1), arg1);
12302         }
12303
12304       /* If we have X - Y == 0, we can convert that to X == Y and similarly
12305          for !=.  Don't do this for ordered comparisons due to overflow.  */
12306       if (TREE_CODE (arg0) == MINUS_EXPR
12307           && integer_zerop (arg1))
12308         return fold_build2 (code, type,
12309                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12310
12311       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
12312       if (TREE_CODE (arg0) == ABS_EXPR
12313           && (integer_zerop (arg1) || real_zerop (arg1)))
12314         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
12315
12316       /* If this is an EQ or NE comparison with zero and ARG0 is
12317          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
12318          two operations, but the latter can be done in one less insn
12319          on machines that have only two-operand insns or on which a
12320          constant cannot be the first operand.  */
12321       if (TREE_CODE (arg0) == BIT_AND_EXPR
12322           && integer_zerop (arg1))
12323         {
12324           tree arg00 = TREE_OPERAND (arg0, 0);
12325           tree arg01 = TREE_OPERAND (arg0, 1);
12326           if (TREE_CODE (arg00) == LSHIFT_EXPR
12327               && integer_onep (TREE_OPERAND (arg00, 0)))
12328             {
12329               tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
12330                                       arg01, TREE_OPERAND (arg00, 1));
12331               tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12332                                  build_int_cst (TREE_TYPE (arg0), 1));
12333               return fold_build2 (code, type,
12334                                   fold_convert (TREE_TYPE (arg1), tem), arg1);
12335             }
12336           else if (TREE_CODE (arg01) == LSHIFT_EXPR
12337                    && integer_onep (TREE_OPERAND (arg01, 0)))
12338             {
12339               tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
12340                                       arg00, TREE_OPERAND (arg01, 1));
12341               tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12342                                  build_int_cst (TREE_TYPE (arg0), 1));
12343               return fold_build2 (code, type,
12344                                   fold_convert (TREE_TYPE (arg1), tem), arg1);
12345             }
12346         }
12347
12348       /* If this is an NE or EQ comparison of zero against the result of a
12349          signed MOD operation whose second operand is a power of 2, make
12350          the MOD operation unsigned since it is simpler and equivalent.  */
12351       if (integer_zerop (arg1)
12352           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12353           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12354               || TREE_CODE (arg0) == CEIL_MOD_EXPR
12355               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12356               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12357           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12358         {
12359           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12360           tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
12361                                      fold_convert (newtype,
12362                                                    TREE_OPERAND (arg0, 0)),
12363                                      fold_convert (newtype,
12364                                                    TREE_OPERAND (arg0, 1)));
12365
12366           return fold_build2 (code, type, newmod,
12367                               fold_convert (newtype, arg1));
12368         }
12369
12370       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12371          C1 is a valid shift constant, and C2 is a power of two, i.e.
12372          a single bit.  */
12373       if (TREE_CODE (arg0) == BIT_AND_EXPR
12374           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12375           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12376              == INTEGER_CST
12377           && integer_pow2p (TREE_OPERAND (arg0, 1))
12378           && integer_zerop (arg1))
12379         {
12380           tree itype = TREE_TYPE (arg0);
12381           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12382           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12383
12384           /* Check for a valid shift count.  */
12385           if (TREE_INT_CST_HIGH (arg001) == 0
12386               && TREE_INT_CST_LOW (arg001) < prec)
12387             {
12388               tree arg01 = TREE_OPERAND (arg0, 1);
12389               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12390               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12391               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12392                  can be rewritten as (X & (C2 << C1)) != 0.  */
12393               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12394                 {
12395                   tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
12396                   tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
12397                   return fold_build2 (code, type, tem, arg1);
12398                 }
12399               /* Otherwise, for signed (arithmetic) shifts,
12400                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12401                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
12402               else if (!TYPE_UNSIGNED (itype))
12403                 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12404                                     arg000, build_int_cst (itype, 0));
12405               /* Otherwise, of unsigned (logical) shifts,
12406                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12407                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
12408               else
12409                 return omit_one_operand (type,
12410                                          code == EQ_EXPR ? integer_one_node
12411                                                          : integer_zero_node,
12412                                          arg000);
12413             }
12414         }
12415
12416       /* If this is an NE comparison of zero with an AND of one, remove the
12417          comparison since the AND will give the correct value.  */
12418       if (code == NE_EXPR
12419           && integer_zerop (arg1)
12420           && TREE_CODE (arg0) == BIT_AND_EXPR
12421           && integer_onep (TREE_OPERAND (arg0, 1)))
12422         return fold_convert (type, arg0);
12423
12424       /* If we have (A & C) == C where C is a power of 2, convert this into
12425          (A & C) != 0.  Similarly for NE_EXPR.  */
12426       if (TREE_CODE (arg0) == BIT_AND_EXPR
12427           && integer_pow2p (TREE_OPERAND (arg0, 1))
12428           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12429         return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12430                             arg0, fold_convert (TREE_TYPE (arg0),
12431                                                 integer_zero_node));
12432
12433       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12434          bit, then fold the expression into A < 0 or A >= 0.  */
12435       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
12436       if (tem)
12437         return tem;
12438
12439       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12440          Similarly for NE_EXPR.  */
12441       if (TREE_CODE (arg0) == BIT_AND_EXPR
12442           && TREE_CODE (arg1) == INTEGER_CST
12443           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12444         {
12445           tree notc = fold_build1 (BIT_NOT_EXPR,
12446                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
12447                                    TREE_OPERAND (arg0, 1));
12448           tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12449                                        arg1, notc);
12450           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12451           if (integer_nonzerop (dandnotc))
12452             return omit_one_operand (type, rslt, arg0);
12453         }
12454
12455       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12456          Similarly for NE_EXPR.  */
12457       if (TREE_CODE (arg0) == BIT_IOR_EXPR
12458           && TREE_CODE (arg1) == INTEGER_CST
12459           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12460         {
12461           tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12462           tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12463                                        TREE_OPERAND (arg0, 1), notd);
12464           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12465           if (integer_nonzerop (candnotd))
12466             return omit_one_operand (type, rslt, arg0);
12467         }
12468
12469       /* If this is a comparison of a field, we may be able to simplify it.  */
12470       if ((TREE_CODE (arg0) == COMPONENT_REF
12471            || TREE_CODE (arg0) == BIT_FIELD_REF)
12472           /* Handle the constant case even without -O
12473              to make sure the warnings are given.  */
12474           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12475         {
12476           t1 = optimize_bit_field_compare (code, type, arg0, arg1);
12477           if (t1)
12478             return t1;
12479         }
12480
12481       /* Optimize comparisons of strlen vs zero to a compare of the
12482          first character of the string vs zero.  To wit,
12483                 strlen(ptr) == 0   =>  *ptr == 0
12484                 strlen(ptr) != 0   =>  *ptr != 0
12485          Other cases should reduce to one of these two (or a constant)
12486          due to the return value of strlen being unsigned.  */
12487       if (TREE_CODE (arg0) == CALL_EXPR
12488           && integer_zerop (arg1))
12489         {
12490           tree fndecl = get_callee_fndecl (arg0);
12491
12492           if (fndecl
12493               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12494               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12495               && call_expr_nargs (arg0) == 1
12496               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12497             {
12498               tree iref = build_fold_indirect_ref (CALL_EXPR_ARG (arg0, 0));
12499               return fold_build2 (code, type, iref,
12500                                   build_int_cst (TREE_TYPE (iref), 0));
12501             }
12502         }
12503
12504       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12505          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12506       if (TREE_CODE (arg0) == RSHIFT_EXPR
12507           && integer_zerop (arg1)
12508           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12509         {
12510           tree arg00 = TREE_OPERAND (arg0, 0);
12511           tree arg01 = TREE_OPERAND (arg0, 1);
12512           tree itype = TREE_TYPE (arg00);
12513           if (TREE_INT_CST_HIGH (arg01) == 0
12514               && TREE_INT_CST_LOW (arg01)
12515                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12516             {
12517               if (TYPE_UNSIGNED (itype))
12518                 {
12519                   itype = signed_type_for (itype);
12520                   arg00 = fold_convert (itype, arg00);
12521                 }
12522               return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12523                                   type, arg00, build_int_cst (itype, 0));
12524             }
12525         }
12526
12527       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12528       if (integer_zerop (arg1)
12529           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12530         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12531                             TREE_OPERAND (arg0, 1));
12532
12533       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12534       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12535           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12536         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12537                             build_int_cst (TREE_TYPE (arg1), 0));
12538       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12539       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12540           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12541           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12542         return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
12543                             build_int_cst (TREE_TYPE (arg1), 0));
12544
12545       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12546       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12547           && TREE_CODE (arg1) == INTEGER_CST
12548           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12549         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12550                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
12551                                          TREE_OPERAND (arg0, 1), arg1));
12552
12553       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12554          (X & C) == 0 when C is a single bit.  */
12555       if (TREE_CODE (arg0) == BIT_AND_EXPR
12556           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12557           && integer_zerop (arg1)
12558           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12559         {
12560           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12561                              TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12562                              TREE_OPERAND (arg0, 1));
12563           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12564                               type, tem, arg1);
12565         }
12566
12567       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12568          constant C is a power of two, i.e. a single bit.  */
12569       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12570           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12571           && integer_zerop (arg1)
12572           && integer_pow2p (TREE_OPERAND (arg0, 1))
12573           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12574                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12575         {
12576           tree arg00 = TREE_OPERAND (arg0, 0);
12577           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12578                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
12579         }
12580
12581       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12582          when is C is a power of two, i.e. a single bit.  */
12583       if (TREE_CODE (arg0) == BIT_AND_EXPR
12584           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12585           && integer_zerop (arg1)
12586           && integer_pow2p (TREE_OPERAND (arg0, 1))
12587           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12588                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12589         {
12590           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12591           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
12592                              arg000, TREE_OPERAND (arg0, 1));
12593           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12594                               tem, build_int_cst (TREE_TYPE (tem), 0));
12595         }
12596
12597       if (integer_zerop (arg1)
12598           && tree_expr_nonzero_p (arg0))
12599         {
12600           tree res = constant_boolean_node (code==NE_EXPR, type);
12601           return omit_one_operand (type, res, arg0);
12602         }
12603
12604       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
12605       if (TREE_CODE (arg0) == NEGATE_EXPR
12606           && TREE_CODE (arg1) == NEGATE_EXPR)
12607         return fold_build2 (code, type,
12608                             TREE_OPERAND (arg0, 0),
12609                             TREE_OPERAND (arg1, 0));
12610
12611       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
12612       if (TREE_CODE (arg0) == BIT_AND_EXPR
12613           && TREE_CODE (arg1) == BIT_AND_EXPR)
12614         {
12615           tree arg00 = TREE_OPERAND (arg0, 0);
12616           tree arg01 = TREE_OPERAND (arg0, 1);
12617           tree arg10 = TREE_OPERAND (arg1, 0);
12618           tree arg11 = TREE_OPERAND (arg1, 1);
12619           tree itype = TREE_TYPE (arg0);
12620
12621           if (operand_equal_p (arg01, arg11, 0))
12622             return fold_build2 (code, type,
12623                                 fold_build2 (BIT_AND_EXPR, itype,
12624                                              fold_build2 (BIT_XOR_EXPR, itype,
12625                                                           arg00, arg10),
12626                                              arg01),
12627                                 build_int_cst (itype, 0));
12628
12629           if (operand_equal_p (arg01, arg10, 0))
12630             return fold_build2 (code, type,
12631                                 fold_build2 (BIT_AND_EXPR, itype,
12632                                              fold_build2 (BIT_XOR_EXPR, itype,
12633                                                           arg00, arg11),
12634                                              arg01),
12635                                 build_int_cst (itype, 0));
12636
12637           if (operand_equal_p (arg00, arg11, 0))
12638             return fold_build2 (code, type,
12639                                 fold_build2 (BIT_AND_EXPR, itype,
12640                                              fold_build2 (BIT_XOR_EXPR, itype,
12641                                                           arg01, arg10),
12642                                              arg00),
12643                                 build_int_cst (itype, 0));
12644
12645           if (operand_equal_p (arg00, arg10, 0))
12646             return fold_build2 (code, type,
12647                                 fold_build2 (BIT_AND_EXPR, itype,
12648                                              fold_build2 (BIT_XOR_EXPR, itype,
12649                                                           arg01, arg11),
12650                                              arg00),
12651                                 build_int_cst (itype, 0));
12652         }
12653
12654       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12655           && TREE_CODE (arg1) == BIT_XOR_EXPR)
12656         {
12657           tree arg00 = TREE_OPERAND (arg0, 0);
12658           tree arg01 = TREE_OPERAND (arg0, 1);
12659           tree arg10 = TREE_OPERAND (arg1, 0);
12660           tree arg11 = TREE_OPERAND (arg1, 1);
12661           tree itype = TREE_TYPE (arg0);
12662
12663           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12664              operand_equal_p guarantees no side-effects so we don't need
12665              to use omit_one_operand on Z.  */
12666           if (operand_equal_p (arg01, arg11, 0))
12667             return fold_build2 (code, type, arg00, arg10);
12668           if (operand_equal_p (arg01, arg10, 0))
12669             return fold_build2 (code, type, arg00, arg11);
12670           if (operand_equal_p (arg00, arg11, 0))
12671             return fold_build2 (code, type, arg01, arg10);
12672           if (operand_equal_p (arg00, arg10, 0))
12673             return fold_build2 (code, type, arg01, arg11);
12674
12675           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
12676           if (TREE_CODE (arg01) == INTEGER_CST
12677               && TREE_CODE (arg11) == INTEGER_CST)
12678             return fold_build2 (code, type,
12679                                 fold_build2 (BIT_XOR_EXPR, itype, arg00,
12680                                              fold_build2 (BIT_XOR_EXPR, itype,
12681                                                           arg01, arg11)),
12682                                 arg10);
12683         }
12684
12685       /* Attempt to simplify equality/inequality comparisons of complex
12686          values.  Only lower the comparison if the result is known or
12687          can be simplified to a single scalar comparison.  */
12688       if ((TREE_CODE (arg0) == COMPLEX_EXPR
12689            || TREE_CODE (arg0) == COMPLEX_CST)
12690           && (TREE_CODE (arg1) == COMPLEX_EXPR
12691               || TREE_CODE (arg1) == COMPLEX_CST))
12692         {
12693           tree real0, imag0, real1, imag1;
12694           tree rcond, icond;
12695
12696           if (TREE_CODE (arg0) == COMPLEX_EXPR)
12697             {
12698               real0 = TREE_OPERAND (arg0, 0);
12699               imag0 = TREE_OPERAND (arg0, 1);
12700             }
12701           else
12702             {
12703               real0 = TREE_REALPART (arg0);
12704               imag0 = TREE_IMAGPART (arg0);
12705             }
12706
12707           if (TREE_CODE (arg1) == COMPLEX_EXPR)
12708             {
12709               real1 = TREE_OPERAND (arg1, 0);
12710               imag1 = TREE_OPERAND (arg1, 1);
12711             }
12712           else
12713             {
12714               real1 = TREE_REALPART (arg1);
12715               imag1 = TREE_IMAGPART (arg1);
12716             }
12717
12718           rcond = fold_binary (code, type, real0, real1);
12719           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12720             {
12721               if (integer_zerop (rcond))
12722                 {
12723                   if (code == EQ_EXPR)
12724                     return omit_two_operands (type, boolean_false_node,
12725                                               imag0, imag1);
12726                   return fold_build2 (NE_EXPR, type, imag0, imag1);
12727                 }
12728               else
12729                 {
12730                   if (code == NE_EXPR)
12731                     return omit_two_operands (type, boolean_true_node,
12732                                               imag0, imag1);
12733                   return fold_build2 (EQ_EXPR, type, imag0, imag1);
12734                 }
12735             }
12736
12737           icond = fold_binary (code, type, imag0, imag1);
12738           if (icond && TREE_CODE (icond) == INTEGER_CST)
12739             {
12740               if (integer_zerop (icond))
12741                 {
12742                   if (code == EQ_EXPR)
12743                     return omit_two_operands (type, boolean_false_node,
12744                                               real0, real1);
12745                   return fold_build2 (NE_EXPR, type, real0, real1);
12746                 }
12747               else
12748                 {
12749                   if (code == NE_EXPR)
12750                     return omit_two_operands (type, boolean_true_node,
12751                                               real0, real1);
12752                   return fold_build2 (EQ_EXPR, type, real0, real1);
12753                 }
12754             }
12755         }
12756
12757       return NULL_TREE;
12758
12759     case LT_EXPR:
12760     case GT_EXPR:
12761     case LE_EXPR:
12762     case GE_EXPR:
12763       tem = fold_comparison (code, type, op0, op1);
12764       if (tem != NULL_TREE)
12765         return tem;
12766
12767       /* Transform comparisons of the form X +- C CMP X.  */
12768       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12769           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12770           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12771                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12772               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12773                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12774         {
12775           tree arg01 = TREE_OPERAND (arg0, 1);
12776           enum tree_code code0 = TREE_CODE (arg0);
12777           int is_positive;
12778
12779           if (TREE_CODE (arg01) == REAL_CST)
12780             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12781           else
12782             is_positive = tree_int_cst_sgn (arg01);
12783
12784           /* (X - c) > X becomes false.  */
12785           if (code == GT_EXPR
12786               && ((code0 == MINUS_EXPR && is_positive >= 0)
12787                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12788             {
12789               if (TREE_CODE (arg01) == INTEGER_CST
12790                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12791                 fold_overflow_warning (("assuming signed overflow does not "
12792                                         "occur when assuming that (X - c) > X "
12793                                         "is always false"),
12794                                        WARN_STRICT_OVERFLOW_ALL);
12795               return constant_boolean_node (0, type);
12796             }
12797
12798           /* Likewise (X + c) < X becomes false.  */
12799           if (code == LT_EXPR
12800               && ((code0 == PLUS_EXPR && is_positive >= 0)
12801                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12802             {
12803               if (TREE_CODE (arg01) == INTEGER_CST
12804                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12805                 fold_overflow_warning (("assuming signed overflow does not "
12806                                         "occur when assuming that "
12807                                         "(X + c) < X is always false"),
12808                                        WARN_STRICT_OVERFLOW_ALL);
12809               return constant_boolean_node (0, type);
12810             }
12811
12812           /* Convert (X - c) <= X to true.  */
12813           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12814               && code == LE_EXPR
12815               && ((code0 == MINUS_EXPR && is_positive >= 0)
12816                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12817             {
12818               if (TREE_CODE (arg01) == INTEGER_CST
12819                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12820                 fold_overflow_warning (("assuming signed overflow does not "
12821                                         "occur when assuming that "
12822                                         "(X - c) <= X is always true"),
12823                                        WARN_STRICT_OVERFLOW_ALL);
12824               return constant_boolean_node (1, type);
12825             }
12826
12827           /* Convert (X + c) >= X to true.  */
12828           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12829               && code == GE_EXPR
12830               && ((code0 == PLUS_EXPR && is_positive >= 0)
12831                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12832             {
12833               if (TREE_CODE (arg01) == INTEGER_CST
12834                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12835                 fold_overflow_warning (("assuming signed overflow does not "
12836                                         "occur when assuming that "
12837                                         "(X + c) >= X is always true"),
12838                                        WARN_STRICT_OVERFLOW_ALL);
12839               return constant_boolean_node (1, type);
12840             }
12841
12842           if (TREE_CODE (arg01) == INTEGER_CST)
12843             {
12844               /* Convert X + c > X and X - c < X to true for integers.  */
12845               if (code == GT_EXPR
12846                   && ((code0 == PLUS_EXPR && is_positive > 0)
12847                       || (code0 == MINUS_EXPR && is_positive < 0)))
12848                 {
12849                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12850                     fold_overflow_warning (("assuming signed overflow does "
12851                                             "not occur when assuming that "
12852                                             "(X + c) > X is always true"),
12853                                            WARN_STRICT_OVERFLOW_ALL);
12854                   return constant_boolean_node (1, type);
12855                 }
12856
12857               if (code == LT_EXPR
12858                   && ((code0 == MINUS_EXPR && is_positive > 0)
12859                       || (code0 == PLUS_EXPR && is_positive < 0)))
12860                 {
12861                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12862                     fold_overflow_warning (("assuming signed overflow does "
12863                                             "not occur when assuming that "
12864                                             "(X - c) < X is always true"),
12865                                            WARN_STRICT_OVERFLOW_ALL);
12866                   return constant_boolean_node (1, type);
12867                 }
12868
12869               /* Convert X + c <= X and X - c >= X to false for integers.  */
12870               if (code == LE_EXPR
12871                   && ((code0 == PLUS_EXPR && is_positive > 0)
12872                       || (code0 == MINUS_EXPR && is_positive < 0)))
12873                 {
12874                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12875                     fold_overflow_warning (("assuming signed overflow does "
12876                                             "not occur when assuming that "
12877                                             "(X + c) <= X is always false"),
12878                                            WARN_STRICT_OVERFLOW_ALL);
12879                   return constant_boolean_node (0, type);
12880                 }
12881
12882               if (code == GE_EXPR
12883                   && ((code0 == MINUS_EXPR && is_positive > 0)
12884                       || (code0 == PLUS_EXPR && is_positive < 0)))
12885                 {
12886                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12887                     fold_overflow_warning (("assuming signed overflow does "
12888                                             "not occur when assuming that "
12889                                             "(X - c) >= X is always false"),
12890                                            WARN_STRICT_OVERFLOW_ALL);
12891                   return constant_boolean_node (0, type);
12892                 }
12893             }
12894         }
12895
12896       /* Comparisons with the highest or lowest possible integer of
12897          the specified precision will have known values.  */
12898       {
12899         tree arg1_type = TREE_TYPE (arg1);
12900         unsigned int width = TYPE_PRECISION (arg1_type);
12901
12902         if (TREE_CODE (arg1) == INTEGER_CST
12903             && width <= 2 * HOST_BITS_PER_WIDE_INT
12904             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12905           {
12906             HOST_WIDE_INT signed_max_hi;
12907             unsigned HOST_WIDE_INT signed_max_lo;
12908             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12909
12910             if (width <= HOST_BITS_PER_WIDE_INT)
12911               {
12912                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12913                                 - 1;
12914                 signed_max_hi = 0;
12915                 max_hi = 0;
12916
12917                 if (TYPE_UNSIGNED (arg1_type))
12918                   {
12919                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12920                     min_lo = 0;
12921                     min_hi = 0;
12922                   }
12923                 else
12924                   {
12925                     max_lo = signed_max_lo;
12926                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12927                     min_hi = -1;
12928                   }
12929               }
12930             else
12931               {
12932                 width -= HOST_BITS_PER_WIDE_INT;
12933                 signed_max_lo = -1;
12934                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12935                                 - 1;
12936                 max_lo = -1;
12937                 min_lo = 0;
12938
12939                 if (TYPE_UNSIGNED (arg1_type))
12940                   {
12941                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12942                     min_hi = 0;
12943                   }
12944                 else
12945                   {
12946                     max_hi = signed_max_hi;
12947                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12948                   }
12949               }
12950
12951             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12952                 && TREE_INT_CST_LOW (arg1) == max_lo)
12953               switch (code)
12954                 {
12955                 case GT_EXPR:
12956                   return omit_one_operand (type, integer_zero_node, arg0);
12957
12958                 case GE_EXPR:
12959                   return fold_build2 (EQ_EXPR, type, op0, op1);
12960
12961                 case LE_EXPR:
12962                   return omit_one_operand (type, integer_one_node, arg0);
12963
12964                 case LT_EXPR:
12965                   return fold_build2 (NE_EXPR, type, op0, op1);
12966
12967                 /* The GE_EXPR and LT_EXPR cases above are not normally
12968                    reached because of previous transformations.  */
12969
12970                 default:
12971                   break;
12972                 }
12973             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12974                      == max_hi
12975                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12976               switch (code)
12977                 {
12978                 case GT_EXPR:
12979                   arg1 = const_binop (PLUS_EXPR, arg1,
12980                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12981                   return fold_build2 (EQ_EXPR, type,
12982                                       fold_convert (TREE_TYPE (arg1), arg0),
12983                                       arg1);
12984                 case LE_EXPR:
12985                   arg1 = const_binop (PLUS_EXPR, arg1,
12986                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12987                   return fold_build2 (NE_EXPR, type,
12988                                       fold_convert (TREE_TYPE (arg1), arg0),
12989                                       arg1);
12990                 default:
12991                   break;
12992                 }
12993             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12994                      == min_hi
12995                      && TREE_INT_CST_LOW (arg1) == min_lo)
12996               switch (code)
12997                 {
12998                 case LT_EXPR:
12999                   return omit_one_operand (type, integer_zero_node, arg0);
13000
13001                 case LE_EXPR:
13002                   return fold_build2 (EQ_EXPR, type, op0, op1);
13003
13004                 case GE_EXPR:
13005                   return omit_one_operand (type, integer_one_node, arg0);
13006
13007                 case GT_EXPR:
13008                   return fold_build2 (NE_EXPR, type, op0, op1);
13009
13010                 default:
13011                   break;
13012                 }
13013             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13014                      == min_hi
13015                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
13016               switch (code)
13017                 {
13018                 case GE_EXPR:
13019                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
13020                   return fold_build2 (NE_EXPR, type,
13021                                       fold_convert (TREE_TYPE (arg1), arg0),
13022                                       arg1);
13023                 case LT_EXPR:
13024                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
13025                   return fold_build2 (EQ_EXPR, type,
13026                                       fold_convert (TREE_TYPE (arg1), arg0),
13027                                       arg1);
13028                 default:
13029                   break;
13030                 }
13031
13032             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13033                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
13034                      && TYPE_UNSIGNED (arg1_type)
13035                      /* We will flip the signedness of the comparison operator
13036                         associated with the mode of arg1, so the sign bit is
13037                         specified by this mode.  Check that arg1 is the signed
13038                         max associated with this sign bit.  */
13039                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13040                      /* signed_type does not work on pointer types.  */
13041                      && INTEGRAL_TYPE_P (arg1_type))
13042               {
13043                 /* The following case also applies to X < signed_max+1
13044                    and X >= signed_max+1 because previous transformations.  */
13045                 if (code == LE_EXPR || code == GT_EXPR)
13046                   {
13047                     tree st;
13048                     st = signed_type_for (TREE_TYPE (arg1));
13049                     return fold_build2 (code == LE_EXPR ? GE_EXPR : LT_EXPR,
13050                                         type, fold_convert (st, arg0),
13051                                         build_int_cst (st, 0));
13052                   }
13053               }
13054           }
13055       }
13056
13057       /* If we are comparing an ABS_EXPR with a constant, we can
13058          convert all the cases into explicit comparisons, but they may
13059          well not be faster than doing the ABS and one comparison.
13060          But ABS (X) <= C is a range comparison, which becomes a subtraction
13061          and a comparison, and is probably faster.  */
13062       if (code == LE_EXPR
13063           && TREE_CODE (arg1) == INTEGER_CST
13064           && TREE_CODE (arg0) == ABS_EXPR
13065           && ! TREE_SIDE_EFFECTS (arg0)
13066           && (0 != (tem = negate_expr (arg1)))
13067           && TREE_CODE (tem) == INTEGER_CST
13068           && !TREE_OVERFLOW (tem))
13069         return fold_build2 (TRUTH_ANDIF_EXPR, type,
13070                             build2 (GE_EXPR, type,
13071                                     TREE_OPERAND (arg0, 0), tem),
13072                             build2 (LE_EXPR, type,
13073                                     TREE_OPERAND (arg0, 0), arg1));
13074
13075       /* Convert ABS_EXPR<x> >= 0 to true.  */
13076       strict_overflow_p = false;
13077       if (code == GE_EXPR
13078           && (integer_zerop (arg1)
13079               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13080                   && real_zerop (arg1)))
13081           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13082         {
13083           if (strict_overflow_p)
13084             fold_overflow_warning (("assuming signed overflow does not occur "
13085                                     "when simplifying comparison of "
13086                                     "absolute value and zero"),
13087                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13088           return omit_one_operand (type, integer_one_node, arg0);
13089         }
13090
13091       /* Convert ABS_EXPR<x> < 0 to false.  */
13092       strict_overflow_p = false;
13093       if (code == LT_EXPR
13094           && (integer_zerop (arg1) || real_zerop (arg1))
13095           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13096         {
13097           if (strict_overflow_p)
13098             fold_overflow_warning (("assuming signed overflow does not occur "
13099                                     "when simplifying comparison of "
13100                                     "absolute value and zero"),
13101                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13102           return omit_one_operand (type, integer_zero_node, arg0);
13103         }
13104
13105       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13106          and similarly for >= into !=.  */
13107       if ((code == LT_EXPR || code == GE_EXPR)
13108           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13109           && TREE_CODE (arg1) == LSHIFT_EXPR
13110           && integer_onep (TREE_OPERAND (arg1, 0)))
13111         return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13112                        build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13113                                TREE_OPERAND (arg1, 1)),
13114                        build_int_cst (TREE_TYPE (arg0), 0));
13115
13116       if ((code == LT_EXPR || code == GE_EXPR)
13117           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13118           && CONVERT_EXPR_P (arg1)
13119           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13120           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13121         return
13122           build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13123                   fold_convert (TREE_TYPE (arg0),
13124                                 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13125                                         TREE_OPERAND (TREE_OPERAND (arg1, 0),
13126                                                       1))),
13127                   build_int_cst (TREE_TYPE (arg0), 0));
13128
13129       return NULL_TREE;
13130
13131     case UNORDERED_EXPR:
13132     case ORDERED_EXPR:
13133     case UNLT_EXPR:
13134     case UNLE_EXPR:
13135     case UNGT_EXPR:
13136     case UNGE_EXPR:
13137     case UNEQ_EXPR:
13138     case LTGT_EXPR:
13139       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13140         {
13141           t1 = fold_relational_const (code, type, arg0, arg1);
13142           if (t1 != NULL_TREE)
13143             return t1;
13144         }
13145
13146       /* If the first operand is NaN, the result is constant.  */
13147       if (TREE_CODE (arg0) == REAL_CST
13148           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13149           && (code != LTGT_EXPR || ! flag_trapping_math))
13150         {
13151           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13152                ? integer_zero_node
13153                : integer_one_node;
13154           return omit_one_operand (type, t1, arg1);
13155         }
13156
13157       /* If the second operand is NaN, the result is constant.  */
13158       if (TREE_CODE (arg1) == REAL_CST
13159           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13160           && (code != LTGT_EXPR || ! flag_trapping_math))
13161         {
13162           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13163                ? integer_zero_node
13164                : integer_one_node;
13165           return omit_one_operand (type, t1, arg0);
13166         }
13167
13168       /* Simplify unordered comparison of something with itself.  */
13169       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13170           && operand_equal_p (arg0, arg1, 0))
13171         return constant_boolean_node (1, type);
13172
13173       if (code == LTGT_EXPR
13174           && !flag_trapping_math
13175           && operand_equal_p (arg0, arg1, 0))
13176         return constant_boolean_node (0, type);
13177
13178       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
13179       {
13180         tree targ0 = strip_float_extensions (arg0);
13181         tree targ1 = strip_float_extensions (arg1);
13182         tree newtype = TREE_TYPE (targ0);
13183
13184         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13185           newtype = TREE_TYPE (targ1);
13186
13187         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13188           return fold_build2 (code, type, fold_convert (newtype, targ0),
13189                               fold_convert (newtype, targ1));
13190       }
13191
13192       return NULL_TREE;
13193
13194     case COMPOUND_EXPR:
13195       /* When pedantic, a compound expression can be neither an lvalue
13196          nor an integer constant expression.  */
13197       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13198         return NULL_TREE;
13199       /* Don't let (0, 0) be null pointer constant.  */
13200       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13201                                  : fold_convert (type, arg1);
13202       return pedantic_non_lvalue (tem);
13203
13204     case COMPLEX_EXPR:
13205       if ((TREE_CODE (arg0) == REAL_CST
13206            && TREE_CODE (arg1) == REAL_CST)
13207           || (TREE_CODE (arg0) == INTEGER_CST
13208               && TREE_CODE (arg1) == INTEGER_CST))
13209         return build_complex (type, arg0, arg1);
13210       return NULL_TREE;
13211
13212     case ASSERT_EXPR:
13213       /* An ASSERT_EXPR should never be passed to fold_binary.  */
13214       gcc_unreachable ();
13215
13216     default:
13217       return NULL_TREE;
13218     } /* switch (code) */
13219 }
13220
13221 /* Callback for walk_tree, looking for LABEL_EXPR.
13222    Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
13223    Do not check the sub-tree of GOTO_EXPR.  */
13224
13225 static tree
13226 contains_label_1 (tree *tp,
13227                   int *walk_subtrees,
13228                   void *data ATTRIBUTE_UNUSED)
13229 {
13230   switch (TREE_CODE (*tp))
13231     {
13232     case LABEL_EXPR:
13233       return *tp;
13234     case GOTO_EXPR:
13235       *walk_subtrees = 0;
13236     /* no break */
13237     default:
13238       return NULL_TREE;
13239     }
13240 }
13241
13242 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
13243    accessible from outside the sub-tree. Returns NULL_TREE if no
13244    addressable label is found.  */
13245
13246 static bool
13247 contains_label_p (tree st)
13248 {
13249   return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
13250 }
13251
13252 /* Fold a ternary expression of code CODE and type TYPE with operands
13253    OP0, OP1, and OP2.  Return the folded expression if folding is
13254    successful.  Otherwise, return NULL_TREE.  */
13255
13256 tree
13257 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
13258 {
13259   tree tem;
13260   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13261   enum tree_code_class kind = TREE_CODE_CLASS (code);
13262
13263   gcc_assert (IS_EXPR_CODE_CLASS (kind)
13264               && TREE_CODE_LENGTH (code) == 3);
13265
13266   /* Strip any conversions that don't change the mode.  This is safe
13267      for every expression, except for a comparison expression because
13268      its signedness is derived from its operands.  So, in the latter
13269      case, only strip conversions that don't change the signedness.
13270
13271      Note that this is done as an internal manipulation within the
13272      constant folder, in order to find the simplest representation of
13273      the arguments so that their form can be studied.  In any cases,
13274      the appropriate type conversions should be put back in the tree
13275      that will get out of the constant folder.  */
13276   if (op0)
13277     {
13278       arg0 = op0;
13279       STRIP_NOPS (arg0);
13280     }
13281
13282   if (op1)
13283     {
13284       arg1 = op1;
13285       STRIP_NOPS (arg1);
13286     }
13287
13288   switch (code)
13289     {
13290     case COMPONENT_REF:
13291       if (TREE_CODE (arg0) == CONSTRUCTOR
13292           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13293         {
13294           unsigned HOST_WIDE_INT idx;
13295           tree field, value;
13296           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13297             if (field == arg1)
13298               return value;
13299         }
13300       return NULL_TREE;
13301
13302     case COND_EXPR:
13303       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13304          so all simple results must be passed through pedantic_non_lvalue.  */
13305       if (TREE_CODE (arg0) == INTEGER_CST)
13306         {
13307           tree unused_op = integer_zerop (arg0) ? op1 : op2;
13308           tem = integer_zerop (arg0) ? op2 : op1;
13309           /* Only optimize constant conditions when the selected branch
13310              has the same type as the COND_EXPR.  This avoids optimizing
13311              away "c ? x : throw", where the throw has a void type.
13312              Avoid throwing away that operand which contains label.  */
13313           if ((!TREE_SIDE_EFFECTS (unused_op)
13314                || !contains_label_p (unused_op))
13315               && (! VOID_TYPE_P (TREE_TYPE (tem))
13316                   || VOID_TYPE_P (type)))
13317             return pedantic_non_lvalue (tem);
13318           return NULL_TREE;
13319         }
13320       if (operand_equal_p (arg1, op2, 0))
13321         return pedantic_omit_one_operand (type, arg1, arg0);
13322
13323       /* If we have A op B ? A : C, we may be able to convert this to a
13324          simpler expression, depending on the operation and the values
13325          of B and C.  Signed zeros prevent all of these transformations,
13326          for reasons given above each one.
13327
13328          Also try swapping the arguments and inverting the conditional.  */
13329       if (COMPARISON_CLASS_P (arg0)
13330           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13331                                              arg1, TREE_OPERAND (arg0, 1))
13332           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13333         {
13334           tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
13335           if (tem)
13336             return tem;
13337         }
13338
13339       if (COMPARISON_CLASS_P (arg0)
13340           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13341                                              op2,
13342                                              TREE_OPERAND (arg0, 1))
13343           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13344         {
13345           tem = fold_truth_not_expr (arg0);
13346           if (tem && COMPARISON_CLASS_P (tem))
13347             {
13348               tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
13349               if (tem)
13350                 return tem;
13351             }
13352         }
13353
13354       /* If the second operand is simpler than the third, swap them
13355          since that produces better jump optimization results.  */
13356       if (truth_value_p (TREE_CODE (arg0))
13357           && tree_swap_operands_p (op1, op2, false))
13358         {
13359           /* See if this can be inverted.  If it can't, possibly because
13360              it was a floating-point inequality comparison, don't do
13361              anything.  */
13362           tem = fold_truth_not_expr (arg0);
13363           if (tem)
13364             return fold_build3 (code, type, tem, op2, op1);
13365         }
13366
13367       /* Convert A ? 1 : 0 to simply A.  */
13368       if (integer_onep (op1)
13369           && integer_zerop (op2)
13370           /* If we try to convert OP0 to our type, the
13371              call to fold will try to move the conversion inside
13372              a COND, which will recurse.  In that case, the COND_EXPR
13373              is probably the best choice, so leave it alone.  */
13374           && type == TREE_TYPE (arg0))
13375         return pedantic_non_lvalue (arg0);
13376
13377       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
13378          over COND_EXPR in cases such as floating point comparisons.  */
13379       if (integer_zerop (op1)
13380           && integer_onep (op2)
13381           && truth_value_p (TREE_CODE (arg0)))
13382         return pedantic_non_lvalue (fold_convert (type,
13383                                                   invert_truthvalue (arg0)));
13384
13385       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
13386       if (TREE_CODE (arg0) == LT_EXPR
13387           && integer_zerop (TREE_OPERAND (arg0, 1))
13388           && integer_zerop (op2)
13389           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13390         {
13391           /* sign_bit_p only checks ARG1 bits within A's precision.
13392              If <sign bit of A> has wider type than A, bits outside
13393              of A's precision in <sign bit of A> need to be checked.
13394              If they are all 0, this optimization needs to be done
13395              in unsigned A's type, if they are all 1 in signed A's type,
13396              otherwise this can't be done.  */
13397           if (TYPE_PRECISION (TREE_TYPE (tem))
13398               < TYPE_PRECISION (TREE_TYPE (arg1))
13399               && TYPE_PRECISION (TREE_TYPE (tem))
13400                  < TYPE_PRECISION (type))
13401             {
13402               unsigned HOST_WIDE_INT mask_lo;
13403               HOST_WIDE_INT mask_hi;
13404               int inner_width, outer_width;
13405               tree tem_type;
13406
13407               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13408               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13409               if (outer_width > TYPE_PRECISION (type))
13410                 outer_width = TYPE_PRECISION (type);
13411
13412               if (outer_width > HOST_BITS_PER_WIDE_INT)
13413                 {
13414                   mask_hi = ((unsigned HOST_WIDE_INT) -1
13415                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13416                   mask_lo = -1;
13417                 }
13418               else
13419                 {
13420                   mask_hi = 0;
13421                   mask_lo = ((unsigned HOST_WIDE_INT) -1
13422                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
13423                 }
13424               if (inner_width > HOST_BITS_PER_WIDE_INT)
13425                 {
13426                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13427                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
13428                   mask_lo = 0;
13429                 }
13430               else
13431                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13432                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
13433
13434               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13435                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13436                 {
13437                   tem_type = signed_type_for (TREE_TYPE (tem));
13438                   tem = fold_convert (tem_type, tem);
13439                 }
13440               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13441                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13442                 {
13443                   tem_type = unsigned_type_for (TREE_TYPE (tem));
13444                   tem = fold_convert (tem_type, tem);
13445                 }
13446               else
13447                 tem = NULL;
13448             }
13449
13450           if (tem)
13451             return fold_convert (type,
13452                                  fold_build2 (BIT_AND_EXPR,
13453                                               TREE_TYPE (tem), tem,
13454                                               fold_convert (TREE_TYPE (tem),
13455                                                             arg1)));
13456         }
13457
13458       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
13459          already handled above.  */
13460       if (TREE_CODE (arg0) == BIT_AND_EXPR
13461           && integer_onep (TREE_OPERAND (arg0, 1))
13462           && integer_zerop (op2)
13463           && integer_pow2p (arg1))
13464         {
13465           tree tem = TREE_OPERAND (arg0, 0);
13466           STRIP_NOPS (tem);
13467           if (TREE_CODE (tem) == RSHIFT_EXPR
13468               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13469               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13470                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13471             return fold_build2 (BIT_AND_EXPR, type,
13472                                 TREE_OPERAND (tem, 0), arg1);
13473         }
13474
13475       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13476          is probably obsolete because the first operand should be a
13477          truth value (that's why we have the two cases above), but let's
13478          leave it in until we can confirm this for all front-ends.  */
13479       if (integer_zerop (op2)
13480           && TREE_CODE (arg0) == NE_EXPR
13481           && integer_zerop (TREE_OPERAND (arg0, 1))
13482           && integer_pow2p (arg1)
13483           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13484           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13485                               arg1, OEP_ONLY_CONST))
13486         return pedantic_non_lvalue (fold_convert (type,
13487                                                   TREE_OPERAND (arg0, 0)));
13488
13489       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13490       if (integer_zerop (op2)
13491           && truth_value_p (TREE_CODE (arg0))
13492           && truth_value_p (TREE_CODE (arg1)))
13493         return fold_build2 (TRUTH_ANDIF_EXPR, type,
13494                             fold_convert (type, arg0),
13495                             arg1);
13496
13497       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13498       if (integer_onep (op2)
13499           && truth_value_p (TREE_CODE (arg0))
13500           && truth_value_p (TREE_CODE (arg1)))
13501         {
13502           /* Only perform transformation if ARG0 is easily inverted.  */
13503           tem = fold_truth_not_expr (arg0);
13504           if (tem)
13505             return fold_build2 (TRUTH_ORIF_EXPR, type,
13506                                 fold_convert (type, tem),
13507                                 arg1);
13508         }
13509
13510       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13511       if (integer_zerop (arg1)
13512           && truth_value_p (TREE_CODE (arg0))
13513           && truth_value_p (TREE_CODE (op2)))
13514         {
13515           /* Only perform transformation if ARG0 is easily inverted.  */
13516           tem = fold_truth_not_expr (arg0);
13517           if (tem)
13518             return fold_build2 (TRUTH_ANDIF_EXPR, type,
13519                                 fold_convert (type, tem),
13520                                 op2);
13521         }
13522
13523       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13524       if (integer_onep (arg1)
13525           && truth_value_p (TREE_CODE (arg0))
13526           && truth_value_p (TREE_CODE (op2)))
13527         return fold_build2 (TRUTH_ORIF_EXPR, type,
13528                             fold_convert (type, arg0),
13529                             op2);
13530
13531       return NULL_TREE;
13532
13533     case CALL_EXPR:
13534       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
13535          of fold_ternary on them.  */
13536       gcc_unreachable ();
13537
13538     case BIT_FIELD_REF:
13539       if ((TREE_CODE (arg0) == VECTOR_CST
13540            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13541           && type == TREE_TYPE (TREE_TYPE (arg0)))
13542         {
13543           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13544           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13545
13546           if (width != 0
13547               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13548               && (idx % width) == 0
13549               && (idx = idx / width)
13550                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13551             {
13552               tree elements = NULL_TREE;
13553
13554               if (TREE_CODE (arg0) == VECTOR_CST)
13555                 elements = TREE_VECTOR_CST_ELTS (arg0);
13556               else
13557                 {
13558                   unsigned HOST_WIDE_INT idx;
13559                   tree value;
13560
13561                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13562                     elements = tree_cons (NULL_TREE, value, elements);
13563                 }
13564               while (idx-- > 0 && elements)
13565                 elements = TREE_CHAIN (elements);
13566               if (elements)
13567                 return TREE_VALUE (elements);
13568               else
13569                 return fold_convert (type, integer_zero_node);
13570             }
13571         }
13572
13573       /* A bit-field-ref that referenced the full argument can be stripped.  */
13574       if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13575           && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13576           && integer_zerop (op2))
13577         return fold_convert (type, arg0);
13578
13579       return NULL_TREE;
13580
13581     default:
13582       return NULL_TREE;
13583     } /* switch (code) */
13584 }
13585
13586 /* Perform constant folding and related simplification of EXPR.
13587    The related simplifications include x*1 => x, x*0 => 0, etc.,
13588    and application of the associative law.
13589    NOP_EXPR conversions may be removed freely (as long as we
13590    are careful not to change the type of the overall expression).
13591    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13592    but we can constant-fold them if they have constant operands.  */
13593
13594 #ifdef ENABLE_FOLD_CHECKING
13595 # define fold(x) fold_1 (x)
13596 static tree fold_1 (tree);
13597 static
13598 #endif
13599 tree
13600 fold (tree expr)
13601 {
13602   const tree t = expr;
13603   enum tree_code code = TREE_CODE (t);
13604   enum tree_code_class kind = TREE_CODE_CLASS (code);
13605   tree tem;
13606
13607   /* Return right away if a constant.  */
13608   if (kind == tcc_constant)
13609     return t;
13610
13611   /* CALL_EXPR-like objects with variable numbers of operands are
13612      treated specially.  */
13613   if (kind == tcc_vl_exp)
13614     {
13615       if (code == CALL_EXPR)
13616         {
13617           tem = fold_call_expr (expr, false);
13618           return tem ? tem : expr;
13619         }
13620       return expr;
13621     }
13622
13623   if (IS_EXPR_CODE_CLASS (kind))
13624     {
13625       tree type = TREE_TYPE (t);
13626       tree op0, op1, op2;
13627
13628       switch (TREE_CODE_LENGTH (code))
13629         {
13630         case 1:
13631           op0 = TREE_OPERAND (t, 0);
13632           tem = fold_unary (code, type, op0);
13633           return tem ? tem : expr;
13634         case 2:
13635           op0 = TREE_OPERAND (t, 0);
13636           op1 = TREE_OPERAND (t, 1);
13637           tem = fold_binary (code, type, op0, op1);
13638           return tem ? tem : expr;
13639         case 3:
13640           op0 = TREE_OPERAND (t, 0);
13641           op1 = TREE_OPERAND (t, 1);
13642           op2 = TREE_OPERAND (t, 2);
13643           tem = fold_ternary (code, type, op0, op1, op2);
13644           return tem ? tem : expr;
13645         default:
13646           break;
13647         }
13648     }
13649
13650   switch (code)
13651     {
13652     case ARRAY_REF:
13653       {
13654         tree op0 = TREE_OPERAND (t, 0);
13655         tree op1 = TREE_OPERAND (t, 1);
13656
13657         if (TREE_CODE (op1) == INTEGER_CST
13658             && TREE_CODE (op0) == CONSTRUCTOR
13659             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13660           {
13661             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13662             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13663             unsigned HOST_WIDE_INT begin = 0;
13664
13665             /* Find a matching index by means of a binary search.  */
13666             while (begin != end)
13667               {
13668                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13669                 tree index = VEC_index (constructor_elt, elts, middle)->index;
13670
13671                 if (TREE_CODE (index) == INTEGER_CST
13672                     && tree_int_cst_lt (index, op1))
13673                   begin = middle + 1;
13674                 else if (TREE_CODE (index) == INTEGER_CST
13675                          && tree_int_cst_lt (op1, index))
13676                   end = middle;
13677                 else if (TREE_CODE (index) == RANGE_EXPR
13678                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13679                   begin = middle + 1;
13680                 else if (TREE_CODE (index) == RANGE_EXPR
13681                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13682                   end = middle;
13683                 else
13684                   return VEC_index (constructor_elt, elts, middle)->value;
13685               }
13686           }
13687
13688         return t;
13689       }
13690
13691     case CONST_DECL:
13692       return fold (DECL_INITIAL (t));
13693
13694     default:
13695       return t;
13696     } /* switch (code) */
13697 }
13698
13699 #ifdef ENABLE_FOLD_CHECKING
13700 #undef fold
13701
13702 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13703 static void fold_check_failed (const_tree, const_tree);
13704 void print_fold_checksum (const_tree);
13705
13706 /* When --enable-checking=fold, compute a digest of expr before
13707    and after actual fold call to see if fold did not accidentally
13708    change original expr.  */
13709
13710 tree
13711 fold (tree expr)
13712 {
13713   tree ret;
13714   struct md5_ctx ctx;
13715   unsigned char checksum_before[16], checksum_after[16];
13716   htab_t ht;
13717
13718   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13719   md5_init_ctx (&ctx);
13720   fold_checksum_tree (expr, &ctx, ht);
13721   md5_finish_ctx (&ctx, checksum_before);
13722   htab_empty (ht);
13723
13724   ret = fold_1 (expr);
13725
13726   md5_init_ctx (&ctx);
13727   fold_checksum_tree (expr, &ctx, ht);
13728   md5_finish_ctx (&ctx, checksum_after);
13729   htab_delete (ht);
13730
13731   if (memcmp (checksum_before, checksum_after, 16))
13732     fold_check_failed (expr, ret);
13733
13734   return ret;
13735 }
13736
13737 void
13738 print_fold_checksum (const_tree expr)
13739 {
13740   struct md5_ctx ctx;
13741   unsigned char checksum[16], cnt;
13742   htab_t ht;
13743
13744   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13745   md5_init_ctx (&ctx);
13746   fold_checksum_tree (expr, &ctx, ht);
13747   md5_finish_ctx (&ctx, checksum);
13748   htab_delete (ht);
13749   for (cnt = 0; cnt < 16; ++cnt)
13750     fprintf (stderr, "%02x", checksum[cnt]);
13751   putc ('\n', stderr);
13752 }
13753
13754 static void
13755 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13756 {
13757   internal_error ("fold check: original tree changed by fold");
13758 }
13759
13760 static void
13761 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13762 {
13763   const void **slot;
13764   enum tree_code code;
13765   union tree_node buf;
13766   int i, len;
13767   
13768 recursive_label:
13769
13770   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13771                <= sizeof (struct tree_function_decl))
13772               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13773   if (expr == NULL)
13774     return;
13775   slot = (const void **) htab_find_slot (ht, expr, INSERT);
13776   if (*slot != NULL)
13777     return;
13778   *slot = expr;
13779   code = TREE_CODE (expr);
13780   if (TREE_CODE_CLASS (code) == tcc_declaration
13781       && DECL_ASSEMBLER_NAME_SET_P (expr))
13782     {
13783       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
13784       memcpy ((char *) &buf, expr, tree_size (expr));
13785       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13786       expr = (tree) &buf;
13787     }
13788   else if (TREE_CODE_CLASS (code) == tcc_type
13789            && (TYPE_POINTER_TO (expr)
13790                || TYPE_REFERENCE_TO (expr)
13791                || TYPE_CACHED_VALUES_P (expr)
13792                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13793                || TYPE_NEXT_VARIANT (expr)))
13794     {
13795       /* Allow these fields to be modified.  */
13796       tree tmp;
13797       memcpy ((char *) &buf, expr, tree_size (expr));
13798       expr = tmp = (tree) &buf;
13799       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13800       TYPE_POINTER_TO (tmp) = NULL;
13801       TYPE_REFERENCE_TO (tmp) = NULL;
13802       TYPE_NEXT_VARIANT (tmp) = NULL;
13803       if (TYPE_CACHED_VALUES_P (tmp))
13804         {
13805           TYPE_CACHED_VALUES_P (tmp) = 0;
13806           TYPE_CACHED_VALUES (tmp) = NULL;
13807         }
13808     }
13809   md5_process_bytes (expr, tree_size (expr), ctx);
13810   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13811   if (TREE_CODE_CLASS (code) != tcc_type
13812       && TREE_CODE_CLASS (code) != tcc_declaration
13813       && code != TREE_LIST
13814       && code != SSA_NAME)
13815     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13816   switch (TREE_CODE_CLASS (code))
13817     {
13818     case tcc_constant:
13819       switch (code)
13820         {
13821         case STRING_CST:
13822           md5_process_bytes (TREE_STRING_POINTER (expr),
13823                              TREE_STRING_LENGTH (expr), ctx);
13824           break;
13825         case COMPLEX_CST:
13826           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13827           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13828           break;
13829         case VECTOR_CST:
13830           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13831           break;
13832         default:
13833           break;
13834         }
13835       break;
13836     case tcc_exceptional:
13837       switch (code)
13838         {
13839         case TREE_LIST:
13840           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13841           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13842           expr = TREE_CHAIN (expr);
13843           goto recursive_label;
13844           break;
13845         case TREE_VEC:
13846           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13847             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13848           break;
13849         default:
13850           break;
13851         }
13852       break;
13853     case tcc_expression:
13854     case tcc_reference:
13855     case tcc_comparison:
13856     case tcc_unary:
13857     case tcc_binary:
13858     case tcc_statement:
13859     case tcc_vl_exp:
13860       len = TREE_OPERAND_LENGTH (expr);
13861       for (i = 0; i < len; ++i)
13862         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13863       break;
13864     case tcc_declaration:
13865       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13866       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13867       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13868         {
13869           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13870           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13871           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13872           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13873           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13874         }
13875       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13876         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13877           
13878       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13879         {
13880           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13881           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13882           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13883         }
13884       break;
13885     case tcc_type:
13886       if (TREE_CODE (expr) == ENUMERAL_TYPE)
13887         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13888       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13889       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13890       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13891       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13892       if (INTEGRAL_TYPE_P (expr)
13893           || SCALAR_FLOAT_TYPE_P (expr))
13894         {
13895           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13896           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13897         }
13898       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13899       if (TREE_CODE (expr) == RECORD_TYPE
13900           || TREE_CODE (expr) == UNION_TYPE
13901           || TREE_CODE (expr) == QUAL_UNION_TYPE)
13902         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13903       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13904       break;
13905     default:
13906       break;
13907     }
13908 }
13909
13910 /* Helper function for outputting the checksum of a tree T.  When
13911    debugging with gdb, you can "define mynext" to be "next" followed
13912    by "call debug_fold_checksum (op0)", then just trace down till the
13913    outputs differ.  */
13914
13915 void
13916 debug_fold_checksum (const_tree t)
13917 {
13918   int i;
13919   unsigned char checksum[16];
13920   struct md5_ctx ctx;
13921   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13922   
13923   md5_init_ctx (&ctx);
13924   fold_checksum_tree (t, &ctx, ht);
13925   md5_finish_ctx (&ctx, checksum);
13926   htab_empty (ht);
13927
13928   for (i = 0; i < 16; i++)
13929     fprintf (stderr, "%d ", checksum[i]);
13930
13931   fprintf (stderr, "\n");
13932 }
13933
13934 #endif
13935
13936 /* Fold a unary tree expression with code CODE of type TYPE with an
13937    operand OP0.  Return a folded expression if successful.  Otherwise,
13938    return a tree expression with code CODE of type TYPE with an
13939    operand OP0.  */
13940
13941 tree
13942 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13943 {
13944   tree tem;
13945 #ifdef ENABLE_FOLD_CHECKING
13946   unsigned char checksum_before[16], checksum_after[16];
13947   struct md5_ctx ctx;
13948   htab_t ht;
13949
13950   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13951   md5_init_ctx (&ctx);
13952   fold_checksum_tree (op0, &ctx, ht);
13953   md5_finish_ctx (&ctx, checksum_before);
13954   htab_empty (ht);
13955 #endif
13956   
13957   tem = fold_unary (code, type, op0);
13958   if (!tem)
13959     tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13960   
13961 #ifdef ENABLE_FOLD_CHECKING
13962   md5_init_ctx (&ctx);
13963   fold_checksum_tree (op0, &ctx, ht);
13964   md5_finish_ctx (&ctx, checksum_after);
13965   htab_delete (ht);
13966
13967   if (memcmp (checksum_before, checksum_after, 16))
13968     fold_check_failed (op0, tem);
13969 #endif
13970   return tem;
13971 }
13972
13973 /* Fold a binary tree expression with code CODE of type TYPE with
13974    operands OP0 and OP1.  Return a folded expression if successful.
13975    Otherwise, return a tree expression with code CODE of type TYPE
13976    with operands OP0 and OP1.  */
13977
13978 tree
13979 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
13980                   MEM_STAT_DECL)
13981 {
13982   tree tem;
13983 #ifdef ENABLE_FOLD_CHECKING
13984   unsigned char checksum_before_op0[16],
13985                 checksum_before_op1[16],
13986                 checksum_after_op0[16],
13987                 checksum_after_op1[16];
13988   struct md5_ctx ctx;
13989   htab_t ht;
13990
13991   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13992   md5_init_ctx (&ctx);
13993   fold_checksum_tree (op0, &ctx, ht);
13994   md5_finish_ctx (&ctx, checksum_before_op0);
13995   htab_empty (ht);
13996
13997   md5_init_ctx (&ctx);
13998   fold_checksum_tree (op1, &ctx, ht);
13999   md5_finish_ctx (&ctx, checksum_before_op1);
14000   htab_empty (ht);
14001 #endif
14002
14003   tem = fold_binary (code, type, op0, op1);
14004   if (!tem)
14005     tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
14006   
14007 #ifdef ENABLE_FOLD_CHECKING
14008   md5_init_ctx (&ctx);
14009   fold_checksum_tree (op0, &ctx, ht);
14010   md5_finish_ctx (&ctx, checksum_after_op0);
14011   htab_empty (ht);
14012
14013   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14014     fold_check_failed (op0, tem);
14015   
14016   md5_init_ctx (&ctx);
14017   fold_checksum_tree (op1, &ctx, ht);
14018   md5_finish_ctx (&ctx, checksum_after_op1);
14019   htab_delete (ht);
14020
14021   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14022     fold_check_failed (op1, tem);
14023 #endif
14024   return tem;
14025 }
14026
14027 /* Fold a ternary tree expression with code CODE of type TYPE with
14028    operands OP0, OP1, and OP2.  Return a folded expression if
14029    successful.  Otherwise, return a tree expression with code CODE of
14030    type TYPE with operands OP0, OP1, and OP2.  */
14031
14032 tree
14033 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
14034              MEM_STAT_DECL)
14035 {
14036   tree tem;
14037 #ifdef ENABLE_FOLD_CHECKING
14038   unsigned char checksum_before_op0[16],
14039                 checksum_before_op1[16],
14040                 checksum_before_op2[16],
14041                 checksum_after_op0[16],
14042                 checksum_after_op1[16],
14043                 checksum_after_op2[16];
14044   struct md5_ctx ctx;
14045   htab_t ht;
14046
14047   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14048   md5_init_ctx (&ctx);
14049   fold_checksum_tree (op0, &ctx, ht);
14050   md5_finish_ctx (&ctx, checksum_before_op0);
14051   htab_empty (ht);
14052
14053   md5_init_ctx (&ctx);
14054   fold_checksum_tree (op1, &ctx, ht);
14055   md5_finish_ctx (&ctx, checksum_before_op1);
14056   htab_empty (ht);
14057
14058   md5_init_ctx (&ctx);
14059   fold_checksum_tree (op2, &ctx, ht);
14060   md5_finish_ctx (&ctx, checksum_before_op2);
14061   htab_empty (ht);
14062 #endif
14063
14064   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14065   tem = fold_ternary (code, type, op0, op1, op2);
14066   if (!tem)
14067     tem =  build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
14068       
14069 #ifdef ENABLE_FOLD_CHECKING
14070   md5_init_ctx (&ctx);
14071   fold_checksum_tree (op0, &ctx, ht);
14072   md5_finish_ctx (&ctx, checksum_after_op0);
14073   htab_empty (ht);
14074
14075   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14076     fold_check_failed (op0, tem);
14077   
14078   md5_init_ctx (&ctx);
14079   fold_checksum_tree (op1, &ctx, ht);
14080   md5_finish_ctx (&ctx, checksum_after_op1);
14081   htab_empty (ht);
14082
14083   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14084     fold_check_failed (op1, tem);
14085   
14086   md5_init_ctx (&ctx);
14087   fold_checksum_tree (op2, &ctx, ht);
14088   md5_finish_ctx (&ctx, checksum_after_op2);
14089   htab_delete (ht);
14090
14091   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14092     fold_check_failed (op2, tem);
14093 #endif
14094   return tem;
14095 }
14096
14097 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14098    arguments in ARGARRAY, and a null static chain.
14099    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
14100    of type TYPE from the given operands as constructed by build_call_array.  */
14101
14102 tree
14103 fold_build_call_array (tree type, tree fn, int nargs, tree *argarray)
14104 {
14105   tree tem;
14106 #ifdef ENABLE_FOLD_CHECKING
14107   unsigned char checksum_before_fn[16],
14108                 checksum_before_arglist[16],
14109                 checksum_after_fn[16],
14110                 checksum_after_arglist[16];
14111   struct md5_ctx ctx;
14112   htab_t ht;
14113   int i;
14114
14115   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14116   md5_init_ctx (&ctx);
14117   fold_checksum_tree (fn, &ctx, ht);
14118   md5_finish_ctx (&ctx, checksum_before_fn);
14119   htab_empty (ht);
14120
14121   md5_init_ctx (&ctx);
14122   for (i = 0; i < nargs; i++)
14123     fold_checksum_tree (argarray[i], &ctx, ht);
14124   md5_finish_ctx (&ctx, checksum_before_arglist);
14125   htab_empty (ht);
14126 #endif
14127
14128   tem = fold_builtin_call_array (type, fn, nargs, argarray);
14129       
14130 #ifdef ENABLE_FOLD_CHECKING
14131   md5_init_ctx (&ctx);
14132   fold_checksum_tree (fn, &ctx, ht);
14133   md5_finish_ctx (&ctx, checksum_after_fn);
14134   htab_empty (ht);
14135
14136   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14137     fold_check_failed (fn, tem);
14138   
14139   md5_init_ctx (&ctx);
14140   for (i = 0; i < nargs; i++)
14141     fold_checksum_tree (argarray[i], &ctx, ht);
14142   md5_finish_ctx (&ctx, checksum_after_arglist);
14143   htab_delete (ht);
14144
14145   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14146     fold_check_failed (NULL_TREE, tem);
14147 #endif
14148   return tem;
14149 }
14150
14151 /* Perform constant folding and related simplification of initializer
14152    expression EXPR.  These behave identically to "fold_buildN" but ignore
14153    potential run-time traps and exceptions that fold must preserve.  */
14154
14155 #define START_FOLD_INIT \
14156   int saved_signaling_nans = flag_signaling_nans;\
14157   int saved_trapping_math = flag_trapping_math;\
14158   int saved_rounding_math = flag_rounding_math;\
14159   int saved_trapv = flag_trapv;\
14160   int saved_folding_initializer = folding_initializer;\
14161   flag_signaling_nans = 0;\
14162   flag_trapping_math = 0;\
14163   flag_rounding_math = 0;\
14164   flag_trapv = 0;\
14165   folding_initializer = 1;
14166
14167 #define END_FOLD_INIT \
14168   flag_signaling_nans = saved_signaling_nans;\
14169   flag_trapping_math = saved_trapping_math;\
14170   flag_rounding_math = saved_rounding_math;\
14171   flag_trapv = saved_trapv;\
14172   folding_initializer = saved_folding_initializer;
14173
14174 tree
14175 fold_build1_initializer (enum tree_code code, tree type, tree op)
14176 {
14177   tree result;
14178   START_FOLD_INIT;
14179
14180   result = fold_build1 (code, type, op);
14181
14182   END_FOLD_INIT;
14183   return result;
14184 }
14185
14186 tree
14187 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
14188 {
14189   tree result;
14190   START_FOLD_INIT;
14191
14192   result = fold_build2 (code, type, op0, op1);
14193
14194   END_FOLD_INIT;
14195   return result;
14196 }
14197
14198 tree
14199 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
14200                          tree op2)
14201 {
14202   tree result;
14203   START_FOLD_INIT;
14204
14205   result = fold_build3 (code, type, op0, op1, op2);
14206
14207   END_FOLD_INIT;
14208   return result;
14209 }
14210
14211 tree
14212 fold_build_call_array_initializer (tree type, tree fn,
14213                                    int nargs, tree *argarray)
14214 {
14215   tree result;
14216   START_FOLD_INIT;
14217
14218   result = fold_build_call_array (type, fn, nargs, argarray);
14219
14220   END_FOLD_INIT;
14221   return result;
14222 }
14223
14224 #undef START_FOLD_INIT
14225 #undef END_FOLD_INIT
14226
14227 /* Determine if first argument is a multiple of second argument.  Return 0 if
14228    it is not, or we cannot easily determined it to be.
14229
14230    An example of the sort of thing we care about (at this point; this routine
14231    could surely be made more general, and expanded to do what the *_DIV_EXPR's
14232    fold cases do now) is discovering that
14233
14234      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14235
14236    is a multiple of
14237
14238      SAVE_EXPR (J * 8)
14239
14240    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14241
14242    This code also handles discovering that
14243
14244      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14245
14246    is a multiple of 8 so we don't have to worry about dealing with a
14247    possible remainder.
14248
14249    Note that we *look* inside a SAVE_EXPR only to determine how it was
14250    calculated; it is not safe for fold to do much of anything else with the
14251    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14252    at run time.  For example, the latter example above *cannot* be implemented
14253    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14254    evaluation time of the original SAVE_EXPR is not necessarily the same at
14255    the time the new expression is evaluated.  The only optimization of this
14256    sort that would be valid is changing
14257
14258      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14259
14260    divided by 8 to
14261
14262      SAVE_EXPR (I) * SAVE_EXPR (J)
14263
14264    (where the same SAVE_EXPR (J) is used in the original and the
14265    transformed version).  */
14266
14267 int
14268 multiple_of_p (tree type, const_tree top, const_tree bottom)
14269 {
14270   if (operand_equal_p (top, bottom, 0))
14271     return 1;
14272
14273   if (TREE_CODE (type) != INTEGER_TYPE)
14274     return 0;
14275
14276   switch (TREE_CODE (top))
14277     {
14278     case BIT_AND_EXPR:
14279       /* Bitwise and provides a power of two multiple.  If the mask is
14280          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
14281       if (!integer_pow2p (bottom))
14282         return 0;
14283       /* FALLTHRU */
14284
14285     case MULT_EXPR:
14286       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14287               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14288
14289     case PLUS_EXPR:
14290     case MINUS_EXPR:
14291       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14292               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14293
14294     case LSHIFT_EXPR:
14295       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14296         {
14297           tree op1, t1;
14298
14299           op1 = TREE_OPERAND (top, 1);
14300           /* const_binop may not detect overflow correctly,
14301              so check for it explicitly here.  */
14302           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14303               > TREE_INT_CST_LOW (op1)
14304               && TREE_INT_CST_HIGH (op1) == 0
14305               && 0 != (t1 = fold_convert (type,
14306                                           const_binop (LSHIFT_EXPR,
14307                                                        size_one_node,
14308                                                        op1, 0)))
14309               && !TREE_OVERFLOW (t1))
14310             return multiple_of_p (type, t1, bottom);
14311         }
14312       return 0;
14313
14314     case NOP_EXPR:
14315       /* Can't handle conversions from non-integral or wider integral type.  */
14316       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14317           || (TYPE_PRECISION (type)
14318               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14319         return 0;
14320
14321       /* .. fall through ...  */
14322
14323     case SAVE_EXPR:
14324       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14325
14326     case INTEGER_CST:
14327       if (TREE_CODE (bottom) != INTEGER_CST
14328           || integer_zerop (bottom)
14329           || (TYPE_UNSIGNED (type)
14330               && (tree_int_cst_sgn (top) < 0
14331                   || tree_int_cst_sgn (bottom) < 0)))
14332         return 0;
14333       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14334                                              top, bottom, 0));
14335
14336     default:
14337       return 0;
14338     }
14339 }
14340
14341 /* Return true if CODE or TYPE is known to be non-negative. */
14342
14343 static bool
14344 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14345 {
14346   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14347       && truth_value_p (code))
14348     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14349        have a signed:1 type (where the value is -1 and 0).  */
14350     return true;
14351   return false;
14352 }
14353
14354 /* Return true if (CODE OP0) is known to be non-negative.  If the return
14355    value is based on the assumption that signed overflow is undefined,
14356    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14357    *STRICT_OVERFLOW_P.  */
14358
14359 bool
14360 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14361                                 bool *strict_overflow_p)
14362 {
14363   if (TYPE_UNSIGNED (type))
14364     return true;
14365
14366   switch (code)
14367     {
14368     case ABS_EXPR:
14369       /* We can't return 1 if flag_wrapv is set because
14370          ABS_EXPR<INT_MIN> = INT_MIN.  */
14371       if (!INTEGRAL_TYPE_P (type))
14372         return true;
14373       if (TYPE_OVERFLOW_UNDEFINED (type))
14374         {
14375           *strict_overflow_p = true;
14376           return true;
14377         }
14378       break;
14379
14380     case NON_LVALUE_EXPR:
14381     case FLOAT_EXPR:
14382     case FIX_TRUNC_EXPR:
14383       return tree_expr_nonnegative_warnv_p (op0,
14384                                             strict_overflow_p);
14385
14386     case NOP_EXPR:
14387       {
14388         tree inner_type = TREE_TYPE (op0);
14389         tree outer_type = type;
14390
14391         if (TREE_CODE (outer_type) == REAL_TYPE)
14392           {
14393             if (TREE_CODE (inner_type) == REAL_TYPE)
14394               return tree_expr_nonnegative_warnv_p (op0,
14395                                                     strict_overflow_p);
14396             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14397               {
14398                 if (TYPE_UNSIGNED (inner_type))
14399                   return true;
14400                 return tree_expr_nonnegative_warnv_p (op0,
14401                                                       strict_overflow_p);
14402               }
14403           }
14404         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14405           {
14406             if (TREE_CODE (inner_type) == REAL_TYPE)
14407               return tree_expr_nonnegative_warnv_p (op0,
14408                                                     strict_overflow_p);
14409             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14410               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14411                       && TYPE_UNSIGNED (inner_type);
14412           }
14413       }
14414       break;
14415
14416     default:
14417       return tree_simple_nonnegative_warnv_p (code, type);
14418     }
14419
14420   /* We don't know sign of `t', so be conservative and return false.  */
14421   return false;
14422 }
14423
14424 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
14425    value is based on the assumption that signed overflow is undefined,
14426    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14427    *STRICT_OVERFLOW_P.  */
14428
14429 bool
14430 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14431                                       tree op1, bool *strict_overflow_p)
14432 {
14433   if (TYPE_UNSIGNED (type))
14434     return true;
14435
14436   switch (code)
14437     {
14438     case POINTER_PLUS_EXPR:
14439     case PLUS_EXPR:
14440       if (FLOAT_TYPE_P (type))
14441         return (tree_expr_nonnegative_warnv_p (op0,
14442                                                strict_overflow_p)
14443                 && tree_expr_nonnegative_warnv_p (op1,
14444                                                   strict_overflow_p));
14445
14446       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14447          both unsigned and at least 2 bits shorter than the result.  */
14448       if (TREE_CODE (type) == INTEGER_TYPE
14449           && TREE_CODE (op0) == NOP_EXPR
14450           && TREE_CODE (op1) == NOP_EXPR)
14451         {
14452           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14453           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14454           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14455               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14456             {
14457               unsigned int prec = MAX (TYPE_PRECISION (inner1),
14458                                        TYPE_PRECISION (inner2)) + 1;
14459               return prec < TYPE_PRECISION (type);
14460             }
14461         }
14462       break;
14463
14464     case MULT_EXPR:
14465       if (FLOAT_TYPE_P (type))
14466         {
14467           /* x * x for floating point x is always non-negative.  */
14468           if (operand_equal_p (op0, op1, 0))
14469             return true;
14470           return (tree_expr_nonnegative_warnv_p (op0,
14471                                                  strict_overflow_p)
14472                   && tree_expr_nonnegative_warnv_p (op1,
14473                                                     strict_overflow_p));
14474         }
14475
14476       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14477          both unsigned and their total bits is shorter than the result.  */
14478       if (TREE_CODE (type) == INTEGER_TYPE
14479           && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14480           && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14481         {
14482           tree inner0 = (TREE_CODE (op0) == NOP_EXPR) 
14483             ? TREE_TYPE (TREE_OPERAND (op0, 0))
14484             : TREE_TYPE (op0);
14485           tree inner1 = (TREE_CODE (op1) == NOP_EXPR) 
14486             ? TREE_TYPE (TREE_OPERAND (op1, 0))
14487             : TREE_TYPE (op1);
14488
14489           bool unsigned0 = TYPE_UNSIGNED (inner0);
14490           bool unsigned1 = TYPE_UNSIGNED (inner1);
14491
14492           if (TREE_CODE (op0) == INTEGER_CST)
14493             unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14494
14495           if (TREE_CODE (op1) == INTEGER_CST)
14496             unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14497
14498           if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14499               && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14500             {
14501               unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14502                 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14503                 : TYPE_PRECISION (inner0);
14504
14505               unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14506                 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14507                 : TYPE_PRECISION (inner1);
14508
14509               return precision0 + precision1 < TYPE_PRECISION (type);
14510             }
14511         }
14512       return false;
14513
14514     case BIT_AND_EXPR:
14515     case MAX_EXPR:
14516       return (tree_expr_nonnegative_warnv_p (op0,
14517                                              strict_overflow_p)
14518               || tree_expr_nonnegative_warnv_p (op1,
14519                                                 strict_overflow_p));
14520
14521     case BIT_IOR_EXPR:
14522     case BIT_XOR_EXPR:
14523     case MIN_EXPR:
14524     case RDIV_EXPR:
14525     case TRUNC_DIV_EXPR:
14526     case CEIL_DIV_EXPR:
14527     case FLOOR_DIV_EXPR:
14528     case ROUND_DIV_EXPR:
14529       return (tree_expr_nonnegative_warnv_p (op0,
14530                                              strict_overflow_p)
14531               && tree_expr_nonnegative_warnv_p (op1,
14532                                                 strict_overflow_p));
14533
14534     case TRUNC_MOD_EXPR:
14535     case CEIL_MOD_EXPR:
14536     case FLOOR_MOD_EXPR:
14537     case ROUND_MOD_EXPR:
14538       return tree_expr_nonnegative_warnv_p (op0,
14539                                             strict_overflow_p);
14540     default:
14541       return tree_simple_nonnegative_warnv_p (code, type);
14542     }
14543
14544   /* We don't know sign of `t', so be conservative and return false.  */
14545   return false;
14546 }
14547
14548 /* Return true if T is known to be non-negative.  If the return
14549    value is based on the assumption that signed overflow is undefined,
14550    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14551    *STRICT_OVERFLOW_P.  */
14552
14553 bool
14554 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14555 {
14556   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14557     return true;
14558
14559   switch (TREE_CODE (t))
14560     {
14561     case INTEGER_CST:
14562       return tree_int_cst_sgn (t) >= 0;
14563
14564     case REAL_CST:
14565       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14566
14567     case FIXED_CST:
14568       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14569
14570     case COND_EXPR:
14571       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14572                                              strict_overflow_p)
14573               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14574                                                 strict_overflow_p));
14575     default:
14576       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14577                                                    TREE_TYPE (t));
14578     }
14579   /* We don't know sign of `t', so be conservative and return false.  */
14580   return false;
14581 }
14582
14583 /* Return true if T is known to be non-negative.  If the return
14584    value is based on the assumption that signed overflow is undefined,
14585    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14586    *STRICT_OVERFLOW_P.  */
14587
14588 bool
14589 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14590                                tree arg0, tree arg1, bool *strict_overflow_p)
14591 {
14592   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14593     switch (DECL_FUNCTION_CODE (fndecl))
14594       {
14595         CASE_FLT_FN (BUILT_IN_ACOS):
14596         CASE_FLT_FN (BUILT_IN_ACOSH):
14597         CASE_FLT_FN (BUILT_IN_CABS):
14598         CASE_FLT_FN (BUILT_IN_COSH):
14599         CASE_FLT_FN (BUILT_IN_ERFC):
14600         CASE_FLT_FN (BUILT_IN_EXP):
14601         CASE_FLT_FN (BUILT_IN_EXP10):
14602         CASE_FLT_FN (BUILT_IN_EXP2):
14603         CASE_FLT_FN (BUILT_IN_FABS):
14604         CASE_FLT_FN (BUILT_IN_FDIM):
14605         CASE_FLT_FN (BUILT_IN_HYPOT):
14606         CASE_FLT_FN (BUILT_IN_POW10):
14607         CASE_INT_FN (BUILT_IN_FFS):
14608         CASE_INT_FN (BUILT_IN_PARITY):
14609         CASE_INT_FN (BUILT_IN_POPCOUNT):
14610       case BUILT_IN_BSWAP32:
14611       case BUILT_IN_BSWAP64:
14612         /* Always true.  */
14613         return true;
14614
14615         CASE_FLT_FN (BUILT_IN_SQRT):
14616         /* sqrt(-0.0) is -0.0.  */
14617         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14618           return true;
14619         return tree_expr_nonnegative_warnv_p (arg0,
14620                                               strict_overflow_p);
14621
14622         CASE_FLT_FN (BUILT_IN_ASINH):
14623         CASE_FLT_FN (BUILT_IN_ATAN):
14624         CASE_FLT_FN (BUILT_IN_ATANH):
14625         CASE_FLT_FN (BUILT_IN_CBRT):
14626         CASE_FLT_FN (BUILT_IN_CEIL):
14627         CASE_FLT_FN (BUILT_IN_ERF):
14628         CASE_FLT_FN (BUILT_IN_EXPM1):
14629         CASE_FLT_FN (BUILT_IN_FLOOR):
14630         CASE_FLT_FN (BUILT_IN_FMOD):
14631         CASE_FLT_FN (BUILT_IN_FREXP):
14632         CASE_FLT_FN (BUILT_IN_LCEIL):
14633         CASE_FLT_FN (BUILT_IN_LDEXP):
14634         CASE_FLT_FN (BUILT_IN_LFLOOR):
14635         CASE_FLT_FN (BUILT_IN_LLCEIL):
14636         CASE_FLT_FN (BUILT_IN_LLFLOOR):
14637         CASE_FLT_FN (BUILT_IN_LLRINT):
14638         CASE_FLT_FN (BUILT_IN_LLROUND):
14639         CASE_FLT_FN (BUILT_IN_LRINT):
14640         CASE_FLT_FN (BUILT_IN_LROUND):
14641         CASE_FLT_FN (BUILT_IN_MODF):
14642         CASE_FLT_FN (BUILT_IN_NEARBYINT):
14643         CASE_FLT_FN (BUILT_IN_RINT):
14644         CASE_FLT_FN (BUILT_IN_ROUND):
14645         CASE_FLT_FN (BUILT_IN_SCALB):
14646         CASE_FLT_FN (BUILT_IN_SCALBLN):
14647         CASE_FLT_FN (BUILT_IN_SCALBN):
14648         CASE_FLT_FN (BUILT_IN_SIGNBIT):
14649         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14650         CASE_FLT_FN (BUILT_IN_SINH):
14651         CASE_FLT_FN (BUILT_IN_TANH):
14652         CASE_FLT_FN (BUILT_IN_TRUNC):
14653         /* True if the 1st argument is nonnegative.  */
14654         return tree_expr_nonnegative_warnv_p (arg0,
14655                                               strict_overflow_p);
14656
14657         CASE_FLT_FN (BUILT_IN_FMAX):
14658         /* True if the 1st OR 2nd arguments are nonnegative.  */
14659         return (tree_expr_nonnegative_warnv_p (arg0,
14660                                                strict_overflow_p)
14661                 || (tree_expr_nonnegative_warnv_p (arg1,
14662                                                    strict_overflow_p)));
14663
14664         CASE_FLT_FN (BUILT_IN_FMIN):
14665         /* True if the 1st AND 2nd arguments are nonnegative.  */
14666         return (tree_expr_nonnegative_warnv_p (arg0,
14667                                                strict_overflow_p)
14668                 && (tree_expr_nonnegative_warnv_p (arg1,
14669                                                    strict_overflow_p)));
14670
14671         CASE_FLT_FN (BUILT_IN_COPYSIGN):
14672         /* True if the 2nd argument is nonnegative.  */
14673         return tree_expr_nonnegative_warnv_p (arg1,
14674                                               strict_overflow_p);
14675
14676         CASE_FLT_FN (BUILT_IN_POWI):
14677         /* True if the 1st argument is nonnegative or the second
14678            argument is an even integer.  */
14679         if (TREE_CODE (arg1) == INTEGER_CST
14680             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14681           return true;
14682         return tree_expr_nonnegative_warnv_p (arg0,
14683                                               strict_overflow_p);
14684
14685         CASE_FLT_FN (BUILT_IN_POW):
14686         /* True if the 1st argument is nonnegative or the second
14687            argument is an even integer valued real.  */
14688         if (TREE_CODE (arg1) == REAL_CST)
14689           {
14690             REAL_VALUE_TYPE c;
14691             HOST_WIDE_INT n;
14692
14693             c = TREE_REAL_CST (arg1);
14694             n = real_to_integer (&c);
14695             if ((n & 1) == 0)
14696               {
14697                 REAL_VALUE_TYPE cint;
14698                 real_from_integer (&cint, VOIDmode, n,
14699                                    n < 0 ? -1 : 0, 0);
14700                 if (real_identical (&c, &cint))
14701                   return true;
14702               }
14703           }
14704         return tree_expr_nonnegative_warnv_p (arg0,
14705                                               strict_overflow_p);
14706
14707       default:
14708         break;
14709       }
14710   return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14711                                           type);
14712 }
14713
14714 /* Return true if T is known to be non-negative.  If the return
14715    value is based on the assumption that signed overflow is undefined,
14716    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14717    *STRICT_OVERFLOW_P.  */
14718
14719 bool
14720 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14721 {
14722   enum tree_code code = TREE_CODE (t);
14723   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14724     return true;
14725
14726   switch (code)
14727     {
14728     case TARGET_EXPR:
14729       {
14730         tree temp = TARGET_EXPR_SLOT (t);
14731         t = TARGET_EXPR_INITIAL (t);
14732
14733         /* If the initializer is non-void, then it's a normal expression
14734            that will be assigned to the slot.  */
14735         if (!VOID_TYPE_P (t))
14736           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14737
14738         /* Otherwise, the initializer sets the slot in some way.  One common
14739            way is an assignment statement at the end of the initializer.  */
14740         while (1)
14741           {
14742             if (TREE_CODE (t) == BIND_EXPR)
14743               t = expr_last (BIND_EXPR_BODY (t));
14744             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14745                      || TREE_CODE (t) == TRY_CATCH_EXPR)
14746               t = expr_last (TREE_OPERAND (t, 0));
14747             else if (TREE_CODE (t) == STATEMENT_LIST)
14748               t = expr_last (t);
14749             else
14750               break;
14751           }
14752         if (TREE_CODE (t) == MODIFY_EXPR
14753             && TREE_OPERAND (t, 0) == temp)
14754           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14755                                                 strict_overflow_p);
14756
14757         return false;
14758       }
14759
14760     case CALL_EXPR:
14761       {
14762         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
14763         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
14764
14765         return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14766                                               get_callee_fndecl (t),
14767                                               arg0,
14768                                               arg1,
14769                                               strict_overflow_p);
14770       }
14771     case COMPOUND_EXPR:
14772     case MODIFY_EXPR:
14773       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14774                                             strict_overflow_p);
14775     case BIND_EXPR:
14776       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14777                                             strict_overflow_p);
14778     case SAVE_EXPR:
14779       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14780                                             strict_overflow_p);
14781
14782     default:
14783       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14784                                                    TREE_TYPE (t));
14785     }
14786
14787   /* We don't know sign of `t', so be conservative and return false.  */
14788   return false;
14789 }
14790
14791 /* Return true if T is known to be non-negative.  If the return
14792    value is based on the assumption that signed overflow is undefined,
14793    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14794    *STRICT_OVERFLOW_P.  */
14795
14796 bool
14797 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14798 {
14799   enum tree_code code;
14800   if (t == error_mark_node)
14801     return false;
14802
14803   code = TREE_CODE (t);
14804   switch (TREE_CODE_CLASS (code))
14805     {
14806     case tcc_binary:
14807     case tcc_comparison:
14808       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14809                                               TREE_TYPE (t),
14810                                               TREE_OPERAND (t, 0),
14811                                               TREE_OPERAND (t, 1),
14812                                               strict_overflow_p);
14813
14814     case tcc_unary:
14815       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14816                                              TREE_TYPE (t),
14817                                              TREE_OPERAND (t, 0),
14818                                              strict_overflow_p);
14819
14820     case tcc_constant:
14821     case tcc_declaration:
14822     case tcc_reference:
14823       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14824
14825     default:
14826       break;
14827     }
14828
14829   switch (code)
14830     {
14831     case TRUTH_AND_EXPR:
14832     case TRUTH_OR_EXPR:
14833     case TRUTH_XOR_EXPR:
14834       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14835                                               TREE_TYPE (t),
14836                                               TREE_OPERAND (t, 0),
14837                                               TREE_OPERAND (t, 1),
14838                                               strict_overflow_p);
14839     case TRUTH_NOT_EXPR:
14840       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14841                                              TREE_TYPE (t),
14842                                              TREE_OPERAND (t, 0),
14843                                              strict_overflow_p);
14844
14845     case COND_EXPR:
14846     case CONSTRUCTOR:
14847     case OBJ_TYPE_REF:
14848     case ASSERT_EXPR:
14849     case ADDR_EXPR:
14850     case WITH_SIZE_EXPR:
14851     case EXC_PTR_EXPR:
14852     case SSA_NAME:
14853     case FILTER_EXPR:
14854       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14855
14856     default:
14857       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14858     }
14859 }
14860
14861 /* Return true if `t' is known to be non-negative.  Handle warnings
14862    about undefined signed overflow.  */
14863
14864 bool
14865 tree_expr_nonnegative_p (tree t)
14866 {
14867   bool ret, strict_overflow_p;
14868
14869   strict_overflow_p = false;
14870   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14871   if (strict_overflow_p)
14872     fold_overflow_warning (("assuming signed overflow does not occur when "
14873                             "determining that expression is always "
14874                             "non-negative"),
14875                            WARN_STRICT_OVERFLOW_MISC);
14876   return ret;
14877 }
14878
14879
14880 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14881    For floating point we further ensure that T is not denormal.
14882    Similar logic is present in nonzero_address in rtlanal.h.
14883
14884    If the return value is based on the assumption that signed overflow
14885    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14886    change *STRICT_OVERFLOW_P.  */
14887
14888 bool
14889 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14890                                  bool *strict_overflow_p)
14891 {
14892   switch (code)
14893     {
14894     case ABS_EXPR:
14895       return tree_expr_nonzero_warnv_p (op0,
14896                                         strict_overflow_p);
14897
14898     case NOP_EXPR:
14899       {
14900         tree inner_type = TREE_TYPE (op0);
14901         tree outer_type = type;
14902
14903         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14904                 && tree_expr_nonzero_warnv_p (op0,
14905                                               strict_overflow_p));
14906       }
14907       break;
14908
14909     case NON_LVALUE_EXPR:
14910       return tree_expr_nonzero_warnv_p (op0,
14911                                         strict_overflow_p);
14912
14913     default:
14914       break;
14915   }
14916
14917   return false;
14918 }
14919
14920 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14921    For floating point we further ensure that T is not denormal.
14922    Similar logic is present in nonzero_address in rtlanal.h.
14923
14924    If the return value is based on the assumption that signed overflow
14925    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14926    change *STRICT_OVERFLOW_P.  */
14927
14928 bool
14929 tree_binary_nonzero_warnv_p (enum tree_code code,
14930                              tree type,
14931                              tree op0,
14932                              tree op1, bool *strict_overflow_p)
14933 {
14934   bool sub_strict_overflow_p;
14935   switch (code)
14936     {
14937     case POINTER_PLUS_EXPR:
14938     case PLUS_EXPR:
14939       if (TYPE_OVERFLOW_UNDEFINED (type))
14940         {
14941           /* With the presence of negative values it is hard
14942              to say something.  */
14943           sub_strict_overflow_p = false;
14944           if (!tree_expr_nonnegative_warnv_p (op0,
14945                                               &sub_strict_overflow_p)
14946               || !tree_expr_nonnegative_warnv_p (op1,
14947                                                  &sub_strict_overflow_p))
14948             return false;
14949           /* One of operands must be positive and the other non-negative.  */
14950           /* We don't set *STRICT_OVERFLOW_P here: even if this value
14951              overflows, on a twos-complement machine the sum of two
14952              nonnegative numbers can never be zero.  */
14953           return (tree_expr_nonzero_warnv_p (op0,
14954                                              strict_overflow_p)
14955                   || tree_expr_nonzero_warnv_p (op1,
14956                                                 strict_overflow_p));
14957         }
14958       break;
14959
14960     case MULT_EXPR:
14961       if (TYPE_OVERFLOW_UNDEFINED (type))
14962         {
14963           if (tree_expr_nonzero_warnv_p (op0,
14964                                          strict_overflow_p)
14965               && tree_expr_nonzero_warnv_p (op1,
14966                                             strict_overflow_p))
14967             {
14968               *strict_overflow_p = true;
14969               return true;
14970             }
14971         }
14972       break;
14973
14974     case MIN_EXPR:
14975       sub_strict_overflow_p = false;
14976       if (tree_expr_nonzero_warnv_p (op0,
14977                                      &sub_strict_overflow_p)
14978           && tree_expr_nonzero_warnv_p (op1,
14979                                         &sub_strict_overflow_p))
14980         {
14981           if (sub_strict_overflow_p)
14982             *strict_overflow_p = true;
14983         }
14984       break;
14985
14986     case MAX_EXPR:
14987       sub_strict_overflow_p = false;
14988       if (tree_expr_nonzero_warnv_p (op0,
14989                                      &sub_strict_overflow_p))
14990         {
14991           if (sub_strict_overflow_p)
14992             *strict_overflow_p = true;
14993
14994           /* When both operands are nonzero, then MAX must be too.  */
14995           if (tree_expr_nonzero_warnv_p (op1,
14996                                          strict_overflow_p))
14997             return true;
14998
14999           /* MAX where operand 0 is positive is positive.  */
15000           return tree_expr_nonnegative_warnv_p (op0,
15001                                                strict_overflow_p);
15002         }
15003       /* MAX where operand 1 is positive is positive.  */
15004       else if (tree_expr_nonzero_warnv_p (op1,
15005                                           &sub_strict_overflow_p)
15006                && tree_expr_nonnegative_warnv_p (op1,
15007                                                  &sub_strict_overflow_p))
15008         {
15009           if (sub_strict_overflow_p)
15010             *strict_overflow_p = true;
15011           return true;
15012         }
15013       break;
15014
15015     case BIT_IOR_EXPR:
15016       return (tree_expr_nonzero_warnv_p (op1,
15017                                          strict_overflow_p)
15018               || tree_expr_nonzero_warnv_p (op0,
15019                                             strict_overflow_p));
15020
15021     default:
15022       break;
15023   }
15024
15025   return false;
15026 }
15027
15028 /* Return true when T is an address and is known to be nonzero.
15029    For floating point we further ensure that T is not denormal.
15030    Similar logic is present in nonzero_address in rtlanal.h.
15031
15032    If the return value is based on the assumption that signed overflow
15033    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15034    change *STRICT_OVERFLOW_P.  */
15035
15036 bool
15037 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15038 {
15039   bool sub_strict_overflow_p;
15040   switch (TREE_CODE (t))
15041     {
15042     case INTEGER_CST:
15043       return !integer_zerop (t);
15044
15045     case ADDR_EXPR:
15046       {
15047         tree base = get_base_address (TREE_OPERAND (t, 0));
15048
15049         if (!base)
15050           return false;
15051
15052         /* Weak declarations may link to NULL.  Other things may also be NULL
15053            so protect with -fdelete-null-pointer-checks; but not variables
15054            allocated on the stack.  */
15055         if (DECL_P (base)
15056             && (flag_delete_null_pointer_checks
15057                 || (TREE_CODE (base) == VAR_DECL && !TREE_STATIC (base))))
15058           return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
15059
15060         /* Constants are never weak.  */
15061         if (CONSTANT_CLASS_P (base))
15062           return true;
15063
15064         return false;
15065       }
15066
15067     case COND_EXPR:
15068       sub_strict_overflow_p = false;
15069       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15070                                      &sub_strict_overflow_p)
15071           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15072                                         &sub_strict_overflow_p))
15073         {
15074           if (sub_strict_overflow_p)
15075             *strict_overflow_p = true;
15076           return true;
15077         }
15078       break;
15079
15080     default:
15081       break;
15082     }
15083   return false;
15084 }
15085
15086 /* Return true when T is an address and is known to be nonzero.
15087    For floating point we further ensure that T is not denormal.
15088    Similar logic is present in nonzero_address in rtlanal.h.
15089
15090    If the return value is based on the assumption that signed overflow
15091    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15092    change *STRICT_OVERFLOW_P.  */
15093
15094 bool
15095 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15096 {
15097   tree type = TREE_TYPE (t);
15098   enum tree_code code;
15099
15100   /* Doing something useful for floating point would need more work.  */
15101   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15102     return false;
15103
15104   code = TREE_CODE (t);
15105   switch (TREE_CODE_CLASS (code))
15106     {
15107     case tcc_unary:
15108       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15109                                               strict_overflow_p);
15110     case tcc_binary:
15111     case tcc_comparison:
15112       return tree_binary_nonzero_warnv_p (code, type,
15113                                                TREE_OPERAND (t, 0),
15114                                                TREE_OPERAND (t, 1),
15115                                                strict_overflow_p);
15116     case tcc_constant:
15117     case tcc_declaration:
15118     case tcc_reference:
15119       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15120
15121     default:
15122       break;
15123     }
15124
15125   switch (code)
15126     {
15127     case TRUTH_NOT_EXPR:
15128       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15129                                               strict_overflow_p);
15130
15131     case TRUTH_AND_EXPR:
15132     case TRUTH_OR_EXPR:
15133     case TRUTH_XOR_EXPR:
15134       return tree_binary_nonzero_warnv_p (code, type,
15135                                                TREE_OPERAND (t, 0),
15136                                                TREE_OPERAND (t, 1),
15137                                                strict_overflow_p);
15138
15139     case COND_EXPR:
15140     case CONSTRUCTOR:
15141     case OBJ_TYPE_REF:
15142     case ASSERT_EXPR:
15143     case ADDR_EXPR:
15144     case WITH_SIZE_EXPR:
15145     case EXC_PTR_EXPR:
15146     case SSA_NAME:
15147     case FILTER_EXPR:
15148       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15149
15150     case COMPOUND_EXPR:
15151     case MODIFY_EXPR:
15152     case BIND_EXPR:
15153       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15154                                         strict_overflow_p);
15155
15156     case SAVE_EXPR:
15157       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15158                                         strict_overflow_p);
15159
15160     case CALL_EXPR:
15161       return alloca_call_p (t);
15162
15163     default:
15164       break;
15165     }
15166   return false;
15167 }
15168
15169 /* Return true when T is an address and is known to be nonzero.
15170    Handle warnings about undefined signed overflow.  */
15171
15172 bool
15173 tree_expr_nonzero_p (tree t)
15174 {
15175   bool ret, strict_overflow_p;
15176
15177   strict_overflow_p = false;
15178   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15179   if (strict_overflow_p)
15180     fold_overflow_warning (("assuming signed overflow does not occur when "
15181                             "determining that expression is always "
15182                             "non-zero"),
15183                            WARN_STRICT_OVERFLOW_MISC);
15184   return ret;
15185 }
15186
15187 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15188    attempt to fold the expression to a constant without modifying TYPE,
15189    OP0 or OP1.
15190
15191    If the expression could be simplified to a constant, then return
15192    the constant.  If the expression would not be simplified to a
15193    constant, then return NULL_TREE.  */
15194
15195 tree
15196 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15197 {
15198   tree tem = fold_binary (code, type, op0, op1);
15199   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15200 }
15201
15202 /* Given the components of a unary expression CODE, TYPE and OP0,
15203    attempt to fold the expression to a constant without modifying
15204    TYPE or OP0.
15205
15206    If the expression could be simplified to a constant, then return
15207    the constant.  If the expression would not be simplified to a
15208    constant, then return NULL_TREE.  */
15209
15210 tree
15211 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15212 {
15213   tree tem = fold_unary (code, type, op0);
15214   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15215 }
15216
15217 /* If EXP represents referencing an element in a constant string
15218    (either via pointer arithmetic or array indexing), return the
15219    tree representing the value accessed, otherwise return NULL.  */
15220
15221 tree
15222 fold_read_from_constant_string (tree exp)
15223 {
15224   if ((TREE_CODE (exp) == INDIRECT_REF
15225        || TREE_CODE (exp) == ARRAY_REF)
15226       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15227     {
15228       tree exp1 = TREE_OPERAND (exp, 0);
15229       tree index;
15230       tree string;
15231
15232       if (TREE_CODE (exp) == INDIRECT_REF)
15233         string = string_constant (exp1, &index);
15234       else
15235         {
15236           tree low_bound = array_ref_low_bound (exp);
15237           index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
15238
15239           /* Optimize the special-case of a zero lower bound.
15240
15241              We convert the low_bound to sizetype to avoid some problems
15242              with constant folding.  (E.g. suppose the lower bound is 1,
15243              and its mode is QI.  Without the conversion,l (ARRAY
15244              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15245              +INDEX), which becomes (ARRAY+255+INDEX).  Oops!)  */
15246           if (! integer_zerop (low_bound))
15247             index = size_diffop (index, fold_convert (sizetype, low_bound));
15248
15249           string = exp1;
15250         }
15251
15252       if (string
15253           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15254           && TREE_CODE (string) == STRING_CST
15255           && TREE_CODE (index) == INTEGER_CST
15256           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15257           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15258               == MODE_INT)
15259           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15260         return build_int_cst_type (TREE_TYPE (exp),
15261                                    (TREE_STRING_POINTER (string)
15262                                     [TREE_INT_CST_LOW (index)]));
15263     }
15264   return NULL;
15265 }
15266
15267 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15268    an integer constant, real, or fixed-point constant.
15269
15270    TYPE is the type of the result.  */
15271
15272 static tree
15273 fold_negate_const (tree arg0, tree type)
15274 {
15275   tree t = NULL_TREE;
15276
15277   switch (TREE_CODE (arg0))
15278     {
15279     case INTEGER_CST:
15280       {
15281         unsigned HOST_WIDE_INT low;
15282         HOST_WIDE_INT high;
15283         int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15284                                    TREE_INT_CST_HIGH (arg0),
15285                                    &low, &high);
15286         t = force_fit_type_double (type, low, high, 1,
15287                                    (overflow | TREE_OVERFLOW (arg0))
15288                                    && !TYPE_UNSIGNED (type));
15289         break;
15290       }
15291
15292     case REAL_CST:
15293       t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15294       break;
15295
15296     case FIXED_CST:
15297       {
15298         FIXED_VALUE_TYPE f;
15299         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15300                                             &(TREE_FIXED_CST (arg0)), NULL,
15301                                             TYPE_SATURATING (type));
15302         t = build_fixed (type, f);
15303         /* Propagate overflow flags.  */
15304         if (overflow_p | TREE_OVERFLOW (arg0))
15305           TREE_OVERFLOW (t) = 1;
15306         break;
15307       }
15308
15309     default:
15310       gcc_unreachable ();
15311     }
15312
15313   return t;
15314 }
15315
15316 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15317    an integer constant or real constant.
15318
15319    TYPE is the type of the result.  */
15320
15321 tree
15322 fold_abs_const (tree arg0, tree type)
15323 {
15324   tree t = NULL_TREE;
15325
15326   switch (TREE_CODE (arg0))
15327     {
15328     case INTEGER_CST:
15329       /* If the value is unsigned, then the absolute value is
15330          the same as the ordinary value.  */
15331       if (TYPE_UNSIGNED (type))
15332         t = arg0;
15333       /* Similarly, if the value is non-negative.  */
15334       else if (INT_CST_LT (integer_minus_one_node, arg0))
15335         t = arg0;
15336       /* If the value is negative, then the absolute value is
15337          its negation.  */
15338       else
15339         {
15340           unsigned HOST_WIDE_INT low;
15341           HOST_WIDE_INT high;
15342           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15343                                      TREE_INT_CST_HIGH (arg0),
15344                                      &low, &high);
15345           t = force_fit_type_double (type, low, high, -1,
15346                                      overflow | TREE_OVERFLOW (arg0));
15347         }
15348       break;
15349
15350     case REAL_CST:
15351       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15352         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15353       else
15354         t =  arg0;
15355       break;
15356
15357     default:
15358       gcc_unreachable ();
15359     }
15360
15361   return t;
15362 }
15363
15364 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15365    constant.  TYPE is the type of the result.  */
15366
15367 static tree
15368 fold_not_const (tree arg0, tree type)
15369 {
15370   tree t = NULL_TREE;
15371
15372   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15373
15374   t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
15375                              ~TREE_INT_CST_HIGH (arg0), 0,
15376                              TREE_OVERFLOW (arg0));
15377
15378   return t;
15379 }
15380
15381 /* Given CODE, a relational operator, the target type, TYPE and two
15382    constant operands OP0 and OP1, return the result of the
15383    relational operation.  If the result is not a compile time
15384    constant, then return NULL_TREE.  */
15385
15386 static tree
15387 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15388 {
15389   int result, invert;
15390
15391   /* From here on, the only cases we handle are when the result is
15392      known to be a constant.  */
15393
15394   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15395     {
15396       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15397       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15398
15399       /* Handle the cases where either operand is a NaN.  */
15400       if (real_isnan (c0) || real_isnan (c1))
15401         {
15402           switch (code)
15403             {
15404             case EQ_EXPR:
15405             case ORDERED_EXPR:
15406               result = 0;
15407               break;
15408
15409             case NE_EXPR:
15410             case UNORDERED_EXPR:
15411             case UNLT_EXPR:
15412             case UNLE_EXPR:
15413             case UNGT_EXPR:
15414             case UNGE_EXPR:
15415             case UNEQ_EXPR:
15416               result = 1;
15417               break;
15418
15419             case LT_EXPR:
15420             case LE_EXPR:
15421             case GT_EXPR:
15422             case GE_EXPR:
15423             case LTGT_EXPR:
15424               if (flag_trapping_math)
15425                 return NULL_TREE;
15426               result = 0;
15427               break;
15428
15429             default:
15430               gcc_unreachable ();
15431             }
15432
15433           return constant_boolean_node (result, type);
15434         }
15435
15436       return constant_boolean_node (real_compare (code, c0, c1), type);
15437     }
15438
15439   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15440     {
15441       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15442       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15443       return constant_boolean_node (fixed_compare (code, c0, c1), type);
15444     }
15445
15446   /* Handle equality/inequality of complex constants.  */
15447   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15448     {
15449       tree rcond = fold_relational_const (code, type,
15450                                           TREE_REALPART (op0),
15451                                           TREE_REALPART (op1));
15452       tree icond = fold_relational_const (code, type,
15453                                           TREE_IMAGPART (op0),
15454                                           TREE_IMAGPART (op1));
15455       if (code == EQ_EXPR)
15456         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15457       else if (code == NE_EXPR)
15458         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15459       else
15460         return NULL_TREE;
15461     }
15462
15463   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15464
15465      To compute GT, swap the arguments and do LT.
15466      To compute GE, do LT and invert the result.
15467      To compute LE, swap the arguments, do LT and invert the result.
15468      To compute NE, do EQ and invert the result.
15469
15470      Therefore, the code below must handle only EQ and LT.  */
15471
15472   if (code == LE_EXPR || code == GT_EXPR)
15473     {
15474       tree tem = op0;
15475       op0 = op1;
15476       op1 = tem;
15477       code = swap_tree_comparison (code);
15478     }
15479
15480   /* Note that it is safe to invert for real values here because we
15481      have already handled the one case that it matters.  */
15482
15483   invert = 0;
15484   if (code == NE_EXPR || code == GE_EXPR)
15485     {
15486       invert = 1;
15487       code = invert_tree_comparison (code, false);
15488     }
15489
15490   /* Compute a result for LT or EQ if args permit;
15491      Otherwise return T.  */
15492   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15493     {
15494       if (code == EQ_EXPR)
15495         result = tree_int_cst_equal (op0, op1);
15496       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15497         result = INT_CST_LT_UNSIGNED (op0, op1);
15498       else
15499         result = INT_CST_LT (op0, op1);
15500     }
15501   else
15502     return NULL_TREE;
15503
15504   if (invert)
15505     result ^= 1;
15506   return constant_boolean_node (result, type);
15507 }
15508
15509 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15510    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
15511    itself.  */
15512
15513 tree
15514 fold_build_cleanup_point_expr (tree type, tree expr)
15515 {
15516   /* If the expression does not have side effects then we don't have to wrap
15517      it with a cleanup point expression.  */
15518   if (!TREE_SIDE_EFFECTS (expr))
15519     return expr;
15520
15521   /* If the expression is a return, check to see if the expression inside the
15522      return has no side effects or the right hand side of the modify expression
15523      inside the return. If either don't have side effects set we don't need to
15524      wrap the expression in a cleanup point expression.  Note we don't check the
15525      left hand side of the modify because it should always be a return decl.  */
15526   if (TREE_CODE (expr) == RETURN_EXPR)
15527     {
15528       tree op = TREE_OPERAND (expr, 0);
15529       if (!op || !TREE_SIDE_EFFECTS (op))
15530         return expr;
15531       op = TREE_OPERAND (op, 1);
15532       if (!TREE_SIDE_EFFECTS (op))
15533         return expr;
15534     }
15535   
15536   return build1 (CLEANUP_POINT_EXPR, type, expr);
15537 }
15538
15539 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15540    of an indirection through OP0, or NULL_TREE if no simplification is
15541    possible.  */
15542
15543 tree
15544 fold_indirect_ref_1 (tree type, tree op0)
15545 {
15546   tree sub = op0;
15547   tree subtype;
15548
15549   STRIP_NOPS (sub);
15550   subtype = TREE_TYPE (sub);
15551   if (!POINTER_TYPE_P (subtype))
15552     return NULL_TREE;
15553
15554   if (TREE_CODE (sub) == ADDR_EXPR)
15555     {
15556       tree op = TREE_OPERAND (sub, 0);
15557       tree optype = TREE_TYPE (op);
15558       /* *&CONST_DECL -> to the value of the const decl.  */
15559       if (TREE_CODE (op) == CONST_DECL)
15560         return DECL_INITIAL (op);
15561       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
15562       if (type == optype)
15563         {
15564           tree fop = fold_read_from_constant_string (op);
15565           if (fop)
15566             return fop;
15567           else
15568             return op;
15569         }
15570       /* *(foo *)&fooarray => fooarray[0] */
15571       else if (TREE_CODE (optype) == ARRAY_TYPE
15572                && type == TREE_TYPE (optype))
15573         {
15574           tree type_domain = TYPE_DOMAIN (optype);
15575           tree min_val = size_zero_node;
15576           if (type_domain && TYPE_MIN_VALUE (type_domain))
15577             min_val = TYPE_MIN_VALUE (type_domain);
15578           return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15579         }
15580       /* *(foo *)&complexfoo => __real__ complexfoo */
15581       else if (TREE_CODE (optype) == COMPLEX_TYPE
15582                && type == TREE_TYPE (optype))
15583         return fold_build1 (REALPART_EXPR, type, op);
15584       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15585       else if (TREE_CODE (optype) == VECTOR_TYPE
15586                && type == TREE_TYPE (optype))
15587         {
15588           tree part_width = TYPE_SIZE (type);
15589           tree index = bitsize_int (0);
15590           return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
15591         }
15592     }
15593
15594   /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15595   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15596       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15597     { 
15598       tree op00 = TREE_OPERAND (sub, 0);
15599       tree op01 = TREE_OPERAND (sub, 1);
15600       tree op00type;
15601       
15602       STRIP_NOPS (op00);
15603       op00type = TREE_TYPE (op00);
15604       if (TREE_CODE (op00) == ADDR_EXPR
15605           && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
15606           && type == TREE_TYPE (TREE_TYPE (op00type)))
15607         { 
15608           HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15609           tree part_width = TYPE_SIZE (type);
15610           unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15611           unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15612           tree index = bitsize_int (indexi);
15613
15614           if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
15615             return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
15616                                 part_width, index);
15617         
15618         }
15619     }
15620
15621
15622   /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15623   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15624       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15625     {
15626       tree op00 = TREE_OPERAND (sub, 0);
15627       tree op01 = TREE_OPERAND (sub, 1);
15628       tree op00type;
15629
15630       STRIP_NOPS (op00);
15631       op00type = TREE_TYPE (op00);
15632       if (TREE_CODE (op00) == ADDR_EXPR
15633           && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
15634           && type == TREE_TYPE (TREE_TYPE (op00type)))
15635         {
15636           tree size = TYPE_SIZE_UNIT (type);
15637           if (tree_int_cst_equal (size, op01))
15638             return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
15639         }
15640     }
15641   
15642   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15643   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15644       && type == TREE_TYPE (TREE_TYPE (subtype)))
15645     {
15646       tree type_domain;
15647       tree min_val = size_zero_node;
15648       sub = build_fold_indirect_ref (sub);
15649       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15650       if (type_domain && TYPE_MIN_VALUE (type_domain))
15651         min_val = TYPE_MIN_VALUE (type_domain);
15652       return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15653     }
15654
15655   return NULL_TREE;
15656 }
15657
15658 /* Builds an expression for an indirection through T, simplifying some
15659    cases.  */
15660
15661 tree
15662 build_fold_indirect_ref (tree t)
15663 {
15664   tree type = TREE_TYPE (TREE_TYPE (t));
15665   tree sub = fold_indirect_ref_1 (type, t);
15666
15667   if (sub)
15668     return sub;
15669   else
15670     return build1 (INDIRECT_REF, type, t);
15671 }
15672
15673 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
15674
15675 tree
15676 fold_indirect_ref (tree t)
15677 {
15678   tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
15679
15680   if (sub)
15681     return sub;
15682   else
15683     return t;
15684 }
15685
15686 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15687    whose result is ignored.  The type of the returned tree need not be
15688    the same as the original expression.  */
15689
15690 tree
15691 fold_ignored_result (tree t)
15692 {
15693   if (!TREE_SIDE_EFFECTS (t))
15694     return integer_zero_node;
15695
15696   for (;;)
15697     switch (TREE_CODE_CLASS (TREE_CODE (t)))
15698       {
15699       case tcc_unary:
15700         t = TREE_OPERAND (t, 0);
15701         break;
15702
15703       case tcc_binary:
15704       case tcc_comparison:
15705         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15706           t = TREE_OPERAND (t, 0);
15707         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15708           t = TREE_OPERAND (t, 1);
15709         else
15710           return t;
15711         break;
15712
15713       case tcc_expression:
15714         switch (TREE_CODE (t))
15715           {
15716           case COMPOUND_EXPR:
15717             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15718               return t;
15719             t = TREE_OPERAND (t, 0);
15720             break;
15721
15722           case COND_EXPR:
15723             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15724                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15725               return t;
15726             t = TREE_OPERAND (t, 0);
15727             break;
15728
15729           default:
15730             return t;
15731           }
15732         break;
15733
15734       default:
15735         return t;
15736       }
15737 }
15738
15739 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15740    This can only be applied to objects of a sizetype.  */
15741
15742 tree
15743 round_up (tree value, int divisor)
15744 {
15745   tree div = NULL_TREE;
15746
15747   gcc_assert (divisor > 0);
15748   if (divisor == 1)
15749     return value;
15750
15751   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15752      have to do anything.  Only do this when we are not given a const,
15753      because in that case, this check is more expensive than just
15754      doing it.  */
15755   if (TREE_CODE (value) != INTEGER_CST)
15756     {
15757       div = build_int_cst (TREE_TYPE (value), divisor);
15758
15759       if (multiple_of_p (TREE_TYPE (value), value, div))
15760         return value;
15761     }
15762
15763   /* If divisor is a power of two, simplify this to bit manipulation.  */
15764   if (divisor == (divisor & -divisor))
15765     {
15766       if (TREE_CODE (value) == INTEGER_CST)
15767         {
15768           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
15769           unsigned HOST_WIDE_INT high;
15770           bool overflow_p;
15771
15772           if ((low & (divisor - 1)) == 0)
15773             return value;
15774
15775           overflow_p = TREE_OVERFLOW (value);
15776           high = TREE_INT_CST_HIGH (value);
15777           low &= ~(divisor - 1);
15778           low += divisor;
15779           if (low == 0)
15780             {
15781               high++;
15782               if (high == 0)
15783                 overflow_p = true;
15784             }
15785
15786           return force_fit_type_double (TREE_TYPE (value), low, high,
15787                                         -1, overflow_p);
15788         }
15789       else
15790         {
15791           tree t;
15792
15793           t = build_int_cst (TREE_TYPE (value), divisor - 1);
15794           value = size_binop (PLUS_EXPR, value, t);
15795           t = build_int_cst (TREE_TYPE (value), -divisor);
15796           value = size_binop (BIT_AND_EXPR, value, t);
15797         }
15798     }
15799   else
15800     {
15801       if (!div)
15802         div = build_int_cst (TREE_TYPE (value), divisor);
15803       value = size_binop (CEIL_DIV_EXPR, value, div);
15804       value = size_binop (MULT_EXPR, value, div);
15805     }
15806
15807   return value;
15808 }
15809
15810 /* Likewise, but round down.  */
15811
15812 tree
15813 round_down (tree value, int divisor)
15814 {
15815   tree div = NULL_TREE;
15816
15817   gcc_assert (divisor > 0);
15818   if (divisor == 1)
15819     return value;
15820
15821   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15822      have to do anything.  Only do this when we are not given a const,
15823      because in that case, this check is more expensive than just
15824      doing it.  */
15825   if (TREE_CODE (value) != INTEGER_CST)
15826     {
15827       div = build_int_cst (TREE_TYPE (value), divisor);
15828
15829       if (multiple_of_p (TREE_TYPE (value), value, div))
15830         return value;
15831     }
15832
15833   /* If divisor is a power of two, simplify this to bit manipulation.  */
15834   if (divisor == (divisor & -divisor))
15835     {
15836       tree t;
15837
15838       t = build_int_cst (TREE_TYPE (value), -divisor);
15839       value = size_binop (BIT_AND_EXPR, value, t);
15840     }
15841   else
15842     {
15843       if (!div)
15844         div = build_int_cst (TREE_TYPE (value), divisor);
15845       value = size_binop (FLOOR_DIV_EXPR, value, div);
15846       value = size_binop (MULT_EXPR, value, div);
15847     }
15848
15849   return value;
15850 }
15851
15852 /* Returns the pointer to the base of the object addressed by EXP and
15853    extracts the information about the offset of the access, storing it
15854    to PBITPOS and POFFSET.  */
15855
15856 static tree
15857 split_address_to_core_and_offset (tree exp,
15858                                   HOST_WIDE_INT *pbitpos, tree *poffset)
15859 {
15860   tree core;
15861   enum machine_mode mode;
15862   int unsignedp, volatilep;
15863   HOST_WIDE_INT bitsize;
15864
15865   if (TREE_CODE (exp) == ADDR_EXPR)
15866     {
15867       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15868                                   poffset, &mode, &unsignedp, &volatilep,
15869                                   false);
15870       core = build_fold_addr_expr (core);
15871     }
15872   else
15873     {
15874       core = exp;
15875       *pbitpos = 0;
15876       *poffset = NULL_TREE;
15877     }
15878
15879   return core;
15880 }
15881
15882 /* Returns true if addresses of E1 and E2 differ by a constant, false
15883    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
15884
15885 bool
15886 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15887 {
15888   tree core1, core2;
15889   HOST_WIDE_INT bitpos1, bitpos2;
15890   tree toffset1, toffset2, tdiff, type;
15891
15892   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15893   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15894
15895   if (bitpos1 % BITS_PER_UNIT != 0
15896       || bitpos2 % BITS_PER_UNIT != 0
15897       || !operand_equal_p (core1, core2, 0))
15898     return false;
15899
15900   if (toffset1 && toffset2)
15901     {
15902       type = TREE_TYPE (toffset1);
15903       if (type != TREE_TYPE (toffset2))
15904         toffset2 = fold_convert (type, toffset2);
15905
15906       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15907       if (!cst_and_fits_in_hwi (tdiff))
15908         return false;
15909
15910       *diff = int_cst_value (tdiff);
15911     }
15912   else if (toffset1 || toffset2)
15913     {
15914       /* If only one of the offsets is non-constant, the difference cannot
15915          be a constant.  */
15916       return false;
15917     }
15918   else
15919     *diff = 0;
15920
15921   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15922   return true;
15923 }
15924
15925 /* Simplify the floating point expression EXP when the sign of the
15926    result is not significant.  Return NULL_TREE if no simplification
15927    is possible.  */
15928
15929 tree
15930 fold_strip_sign_ops (tree exp)
15931 {
15932   tree arg0, arg1;
15933
15934   switch (TREE_CODE (exp))
15935     {
15936     case ABS_EXPR:
15937     case NEGATE_EXPR:
15938       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15939       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15940
15941     case MULT_EXPR:
15942     case RDIV_EXPR:
15943       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15944         return NULL_TREE;
15945       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15946       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15947       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15948         return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
15949                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
15950                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
15951       break;
15952
15953     case COMPOUND_EXPR:
15954       arg0 = TREE_OPERAND (exp, 0);
15955       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15956       if (arg1)
15957         return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15958       break;
15959       
15960     case COND_EXPR:
15961       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15962       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15963       if (arg0 || arg1)
15964         return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15965                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
15966                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
15967       break;
15968       
15969     case CALL_EXPR:
15970       {
15971         const enum built_in_function fcode = builtin_mathfn_code (exp);
15972         switch (fcode)
15973         {
15974         CASE_FLT_FN (BUILT_IN_COPYSIGN):
15975           /* Strip copysign function call, return the 1st argument. */
15976           arg0 = CALL_EXPR_ARG (exp, 0);
15977           arg1 = CALL_EXPR_ARG (exp, 1);
15978           return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
15979
15980         default:
15981           /* Strip sign ops from the argument of "odd" math functions.  */
15982           if (negate_mathfn_p (fcode))
15983             {
15984               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15985               if (arg0)
15986                 return build_call_expr (get_callee_fndecl (exp), 1, arg0);
15987             }
15988           break;
15989         }
15990       }
15991       break;
15992
15993     default:
15994       break;
15995     }
15996   return NULL_TREE;
15997 }