OSDN Git Service

* collect2.c (main): In AIX specific computations for vector
[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, 2010, 2011
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 and size_binop.
31
32    fold takes a tree as argument and returns a simplified tree.
33
34    size_binop takes a tree code for an arithmetic operation
35    and two operands that are trees, and produces a tree for the
36    result, assuming the type comes from `sizetype'.
37
38    size_int takes an integer value, and creates a tree constant
39    with type from `sizetype'.
40
41    Note: Since the folders get called on non-gimple code as well as
42    gimple code, we need to handle GIMPLE tuples as well as their
43    corresponding tree equivalents.  */
44
45 #include "config.h"
46 #include "system.h"
47 #include "coretypes.h"
48 #include "tm.h"
49 #include "flags.h"
50 #include "tree.h"
51 #include "realmpfr.h"
52 #include "rtl.h"
53 #include "expr.h"
54 #include "tm_p.h"
55 #include "target.h"
56 #include "diagnostic-core.h"
57 #include "intl.h"
58 #include "ggc.h"
59 #include "hashtab.h"
60 #include "langhooks.h"
61 #include "md5.h"
62 #include "gimple.h"
63 #include "tree-flow.h"
64
65 /* Nonzero if we are folding constants inside an initializer; zero
66    otherwise.  */
67 int folding_initializer = 0;
68
69 /* The following constants represent a bit based encoding of GCC's
70    comparison operators.  This encoding simplifies transformations
71    on relational comparison operators, such as AND and OR.  */
72 enum comparison_code {
73   COMPCODE_FALSE = 0,
74   COMPCODE_LT = 1,
75   COMPCODE_EQ = 2,
76   COMPCODE_LE = 3,
77   COMPCODE_GT = 4,
78   COMPCODE_LTGT = 5,
79   COMPCODE_GE = 6,
80   COMPCODE_ORD = 7,
81   COMPCODE_UNORD = 8,
82   COMPCODE_UNLT = 9,
83   COMPCODE_UNEQ = 10,
84   COMPCODE_UNLE = 11,
85   COMPCODE_UNGT = 12,
86   COMPCODE_NE = 13,
87   COMPCODE_UNGE = 14,
88   COMPCODE_TRUE = 15
89 };
90
91 static bool negate_mathfn_p (enum built_in_function);
92 static bool negate_expr_p (tree);
93 static tree negate_expr (tree);
94 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
95 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
96 static tree const_binop (enum tree_code, tree, tree);
97 static enum comparison_code comparison_to_compcode (enum tree_code);
98 static enum tree_code compcode_to_comparison (enum comparison_code);
99 static int operand_equal_for_comparison_p (tree, tree, tree);
100 static int twoval_comparison_p (tree, tree *, tree *, int *);
101 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
102 static tree pedantic_omit_one_operand_loc (location_t, tree, tree, tree);
103 static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree);
104 static tree make_bit_field_ref (location_t, tree, tree,
105                                 HOST_WIDE_INT, HOST_WIDE_INT, int);
106 static tree optimize_bit_field_compare (location_t, enum tree_code,
107                                         tree, tree, tree);
108 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
109                                     HOST_WIDE_INT *,
110                                     enum machine_mode *, int *, int *,
111                                     tree *, tree *);
112 static int all_ones_mask_p (const_tree, int);
113 static tree sign_bit_p (tree, const_tree);
114 static int simple_operand_p (const_tree);
115 static bool simple_operand_p_2 (tree);
116 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
117 static tree range_predecessor (tree);
118 static tree range_successor (tree);
119 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
120 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
121 static tree unextend (tree, int, int, tree);
122 static tree optimize_minmax_comparison (location_t, enum tree_code,
123                                         tree, tree, tree);
124 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
125 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
126 static tree fold_binary_op_with_conditional_arg (location_t,
127                                                  enum tree_code, tree,
128                                                  tree, tree,
129                                                  tree, tree, int);
130 static tree fold_mathfn_compare (location_t,
131                                  enum built_in_function, enum tree_code,
132                                  tree, tree, tree);
133 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
134 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
135 static bool reorder_operands_p (const_tree, const_tree);
136 static tree fold_negate_const (tree, tree);
137 static tree fold_not_const (const_tree, tree);
138 static tree fold_relational_const (enum tree_code, tree, tree, tree);
139 static tree fold_convert_const (enum tree_code, tree, tree);
140
141 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
142    Otherwise, return LOC.  */
143
144 static location_t
145 expr_location_or (tree t, location_t loc)
146 {
147   location_t tloc = EXPR_LOCATION (t);
148   return tloc != UNKNOWN_LOCATION ? tloc : loc;
149 }
150
151 /* Similar to protected_set_expr_location, but never modify x in place,
152    if location can and needs to be set, unshare it.  */
153
154 static inline tree
155 protected_set_expr_location_unshare (tree x, location_t loc)
156 {
157   if (CAN_HAVE_LOCATION_P (x)
158       && EXPR_LOCATION (x) != loc
159       && !(TREE_CODE (x) == SAVE_EXPR
160            || TREE_CODE (x) == TARGET_EXPR
161            || TREE_CODE (x) == BIND_EXPR))
162     {
163       x = copy_node (x);
164       SET_EXPR_LOCATION (x, loc);
165     }
166   return x;
167 }
168
169
170 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
171    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
172    and SUM1.  Then this yields nonzero if overflow occurred during the
173    addition.
174
175    Overflow occurs if A and B have the same sign, but A and SUM differ in
176    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
177    sign.  */
178 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
179 \f
180 /* If ARG2 divides ARG1 with zero remainder, carries out the division
181    of type CODE and returns the quotient.
182    Otherwise returns NULL_TREE.  */
183
184 tree
185 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
186 {
187   double_int quo, rem;
188   int uns;
189
190   /* The sign of the division is according to operand two, that
191      does the correct thing for POINTER_PLUS_EXPR where we want
192      a signed division.  */
193   uns = TYPE_UNSIGNED (TREE_TYPE (arg2));
194   if (TREE_CODE (TREE_TYPE (arg2)) == INTEGER_TYPE
195       && TYPE_IS_SIZETYPE (TREE_TYPE (arg2)))
196     uns = false;
197
198   quo = double_int_divmod (tree_to_double_int (arg1),
199                            tree_to_double_int (arg2),
200                            uns, code, &rem);
201
202   if (double_int_zero_p (rem))
203     return build_int_cst_wide (TREE_TYPE (arg1), quo.low, quo.high);
204
205   return NULL_TREE; 
206 }
207 \f
208 /* This is nonzero if we should defer warnings about undefined
209    overflow.  This facility exists because these warnings are a
210    special case.  The code to estimate loop iterations does not want
211    to issue any warnings, since it works with expressions which do not
212    occur in user code.  Various bits of cleanup code call fold(), but
213    only use the result if it has certain characteristics (e.g., is a
214    constant); that code only wants to issue a warning if the result is
215    used.  */
216
217 static int fold_deferring_overflow_warnings;
218
219 /* If a warning about undefined overflow is deferred, this is the
220    warning.  Note that this may cause us to turn two warnings into
221    one, but that is fine since it is sufficient to only give one
222    warning per expression.  */
223
224 static const char* fold_deferred_overflow_warning;
225
226 /* If a warning about undefined overflow is deferred, this is the
227    level at which the warning should be emitted.  */
228
229 static enum warn_strict_overflow_code fold_deferred_overflow_code;
230
231 /* Start deferring overflow warnings.  We could use a stack here to
232    permit nested calls, but at present it is not necessary.  */
233
234 void
235 fold_defer_overflow_warnings (void)
236 {
237   ++fold_deferring_overflow_warnings;
238 }
239
240 /* Stop deferring overflow warnings.  If there is a pending warning,
241    and ISSUE is true, then issue the warning if appropriate.  STMT is
242    the statement with which the warning should be associated (used for
243    location information); STMT may be NULL.  CODE is the level of the
244    warning--a warn_strict_overflow_code value.  This function will use
245    the smaller of CODE and the deferred code when deciding whether to
246    issue the warning.  CODE may be zero to mean to always use the
247    deferred code.  */
248
249 void
250 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
251 {
252   const char *warnmsg;
253   location_t locus;
254
255   gcc_assert (fold_deferring_overflow_warnings > 0);
256   --fold_deferring_overflow_warnings;
257   if (fold_deferring_overflow_warnings > 0)
258     {
259       if (fold_deferred_overflow_warning != NULL
260           && code != 0
261           && code < (int) fold_deferred_overflow_code)
262         fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
263       return;
264     }
265
266   warnmsg = fold_deferred_overflow_warning;
267   fold_deferred_overflow_warning = NULL;
268
269   if (!issue || warnmsg == NULL)
270     return;
271
272   if (gimple_no_warning_p (stmt))
273     return;
274
275   /* Use the smallest code level when deciding to issue the
276      warning.  */
277   if (code == 0 || code > (int) fold_deferred_overflow_code)
278     code = fold_deferred_overflow_code;
279
280   if (!issue_strict_overflow_warning (code))
281     return;
282
283   if (stmt == NULL)
284     locus = input_location;
285   else
286     locus = gimple_location (stmt);
287   warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
288 }
289
290 /* Stop deferring overflow warnings, ignoring any deferred
291    warnings.  */
292
293 void
294 fold_undefer_and_ignore_overflow_warnings (void)
295 {
296   fold_undefer_overflow_warnings (false, NULL, 0);
297 }
298
299 /* Whether we are deferring overflow warnings.  */
300
301 bool
302 fold_deferring_overflow_warnings_p (void)
303 {
304   return fold_deferring_overflow_warnings > 0;
305 }
306
307 /* This is called when we fold something based on the fact that signed
308    overflow is undefined.  */
309
310 static void
311 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
312 {
313   if (fold_deferring_overflow_warnings > 0)
314     {
315       if (fold_deferred_overflow_warning == NULL
316           || wc < fold_deferred_overflow_code)
317         {
318           fold_deferred_overflow_warning = gmsgid;
319           fold_deferred_overflow_code = wc;
320         }
321     }
322   else if (issue_strict_overflow_warning (wc))
323     warning (OPT_Wstrict_overflow, gmsgid);
324 }
325 \f
326 /* Return true if the built-in mathematical function specified by CODE
327    is odd, i.e. -f(x) == f(-x).  */
328
329 static bool
330 negate_mathfn_p (enum built_in_function code)
331 {
332   switch (code)
333     {
334     CASE_FLT_FN (BUILT_IN_ASIN):
335     CASE_FLT_FN (BUILT_IN_ASINH):
336     CASE_FLT_FN (BUILT_IN_ATAN):
337     CASE_FLT_FN (BUILT_IN_ATANH):
338     CASE_FLT_FN (BUILT_IN_CASIN):
339     CASE_FLT_FN (BUILT_IN_CASINH):
340     CASE_FLT_FN (BUILT_IN_CATAN):
341     CASE_FLT_FN (BUILT_IN_CATANH):
342     CASE_FLT_FN (BUILT_IN_CBRT):
343     CASE_FLT_FN (BUILT_IN_CPROJ):
344     CASE_FLT_FN (BUILT_IN_CSIN):
345     CASE_FLT_FN (BUILT_IN_CSINH):
346     CASE_FLT_FN (BUILT_IN_CTAN):
347     CASE_FLT_FN (BUILT_IN_CTANH):
348     CASE_FLT_FN (BUILT_IN_ERF):
349     CASE_FLT_FN (BUILT_IN_LLROUND):
350     CASE_FLT_FN (BUILT_IN_LROUND):
351     CASE_FLT_FN (BUILT_IN_ROUND):
352     CASE_FLT_FN (BUILT_IN_SIN):
353     CASE_FLT_FN (BUILT_IN_SINH):
354     CASE_FLT_FN (BUILT_IN_TAN):
355     CASE_FLT_FN (BUILT_IN_TANH):
356     CASE_FLT_FN (BUILT_IN_TRUNC):
357       return true;
358
359     CASE_FLT_FN (BUILT_IN_LLRINT):
360     CASE_FLT_FN (BUILT_IN_LRINT):
361     CASE_FLT_FN (BUILT_IN_NEARBYINT):
362     CASE_FLT_FN (BUILT_IN_RINT):
363       return !flag_rounding_math;
364
365     default:
366       break;
367     }
368   return false;
369 }
370
371 /* Check whether we may negate an integer constant T without causing
372    overflow.  */
373
374 bool
375 may_negate_without_overflow_p (const_tree t)
376 {
377   unsigned HOST_WIDE_INT val;
378   unsigned int prec;
379   tree type;
380
381   gcc_assert (TREE_CODE (t) == INTEGER_CST);
382
383   type = TREE_TYPE (t);
384   if (TYPE_UNSIGNED (type))
385     return false;
386
387   prec = TYPE_PRECISION (type);
388   if (prec > HOST_BITS_PER_WIDE_INT)
389     {
390       if (TREE_INT_CST_LOW (t) != 0)
391         return true;
392       prec -= HOST_BITS_PER_WIDE_INT;
393       val = TREE_INT_CST_HIGH (t);
394     }
395   else
396     val = TREE_INT_CST_LOW (t);
397   if (prec < HOST_BITS_PER_WIDE_INT)
398     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
399   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
400 }
401
402 /* Determine whether an expression T can be cheaply negated using
403    the function negate_expr without introducing undefined overflow.  */
404
405 static bool
406 negate_expr_p (tree t)
407 {
408   tree type;
409
410   if (t == 0)
411     return false;
412
413   type = TREE_TYPE (t);
414
415   STRIP_SIGN_NOPS (t);
416   switch (TREE_CODE (t))
417     {
418     case INTEGER_CST:
419       if (TYPE_OVERFLOW_WRAPS (type))
420         return true;
421
422       /* Check that -CST will not overflow type.  */
423       return may_negate_without_overflow_p (t);
424     case BIT_NOT_EXPR:
425       return (INTEGRAL_TYPE_P (type)
426               && TYPE_OVERFLOW_WRAPS (type));
427
428     case FIXED_CST:
429     case NEGATE_EXPR:
430       return true;
431
432     case REAL_CST:
433       /* We want to canonicalize to positive real constants.  Pretend
434          that only negative ones can be easily negated.  */
435       return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
436
437     case COMPLEX_CST:
438       return negate_expr_p (TREE_REALPART (t))
439              && negate_expr_p (TREE_IMAGPART (t));
440
441     case COMPLEX_EXPR:
442       return negate_expr_p (TREE_OPERAND (t, 0))
443              && negate_expr_p (TREE_OPERAND (t, 1));
444
445     case CONJ_EXPR:
446       return negate_expr_p (TREE_OPERAND (t, 0));
447
448     case PLUS_EXPR:
449       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
450           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
451         return false;
452       /* -(A + B) -> (-B) - A.  */
453       if (negate_expr_p (TREE_OPERAND (t, 1))
454           && reorder_operands_p (TREE_OPERAND (t, 0),
455                                  TREE_OPERAND (t, 1)))
456         return true;
457       /* -(A + B) -> (-A) - B.  */
458       return negate_expr_p (TREE_OPERAND (t, 0));
459
460     case MINUS_EXPR:
461       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
462       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
463              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
464              && reorder_operands_p (TREE_OPERAND (t, 0),
465                                     TREE_OPERAND (t, 1));
466
467     case MULT_EXPR:
468       if (TYPE_UNSIGNED (TREE_TYPE (t)))
469         break;
470
471       /* Fall through.  */
472
473     case RDIV_EXPR:
474       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
475         return negate_expr_p (TREE_OPERAND (t, 1))
476                || negate_expr_p (TREE_OPERAND (t, 0));
477       break;
478
479     case TRUNC_DIV_EXPR:
480     case ROUND_DIV_EXPR:
481     case FLOOR_DIV_EXPR:
482     case CEIL_DIV_EXPR:
483     case EXACT_DIV_EXPR:
484       /* In general we can't negate A / B, because if A is INT_MIN and
485          B is 1, we may turn this into INT_MIN / -1 which is undefined
486          and actually traps on some architectures.  But if overflow is
487          undefined, we can negate, because - (INT_MIN / 1) is an
488          overflow.  */
489       if (INTEGRAL_TYPE_P (TREE_TYPE (t))
490           && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
491         break;
492       return negate_expr_p (TREE_OPERAND (t, 1))
493              || negate_expr_p (TREE_OPERAND (t, 0));
494
495     case NOP_EXPR:
496       /* Negate -((double)float) as (double)(-float).  */
497       if (TREE_CODE (type) == REAL_TYPE)
498         {
499           tree tem = strip_float_extensions (t);
500           if (tem != t)
501             return negate_expr_p (tem);
502         }
503       break;
504
505     case CALL_EXPR:
506       /* Negate -f(x) as f(-x).  */
507       if (negate_mathfn_p (builtin_mathfn_code (t)))
508         return negate_expr_p (CALL_EXPR_ARG (t, 0));
509       break;
510
511     case RSHIFT_EXPR:
512       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
513       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
514         {
515           tree op1 = TREE_OPERAND (t, 1);
516           if (TREE_INT_CST_HIGH (op1) == 0
517               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
518                  == TREE_INT_CST_LOW (op1))
519             return true;
520         }
521       break;
522
523     default:
524       break;
525     }
526   return false;
527 }
528
529 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
530    simplification is possible.
531    If negate_expr_p would return true for T, NULL_TREE will never be
532    returned.  */
533
534 static tree
535 fold_negate_expr (location_t loc, tree t)
536 {
537   tree type = TREE_TYPE (t);
538   tree tem;
539
540   switch (TREE_CODE (t))
541     {
542     /* Convert - (~A) to A + 1.  */
543     case BIT_NOT_EXPR:
544       if (INTEGRAL_TYPE_P (type))
545         return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
546                             build_int_cst (type, 1));
547       break;
548
549     case INTEGER_CST:
550       tem = fold_negate_const (t, type);
551       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
552           || !TYPE_OVERFLOW_TRAPS (type))
553         return tem;
554       break;
555
556     case REAL_CST:
557       tem = fold_negate_const (t, type);
558       /* Two's complement FP formats, such as c4x, may overflow.  */
559       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
560         return tem;
561       break;
562
563     case FIXED_CST:
564       tem = fold_negate_const (t, type);
565       return tem;
566
567     case COMPLEX_CST:
568       {
569         tree rpart = negate_expr (TREE_REALPART (t));
570         tree ipart = negate_expr (TREE_IMAGPART (t));
571
572         if ((TREE_CODE (rpart) == REAL_CST
573              && TREE_CODE (ipart) == REAL_CST)
574             || (TREE_CODE (rpart) == INTEGER_CST
575                 && TREE_CODE (ipart) == INTEGER_CST))
576           return build_complex (type, rpart, ipart);
577       }
578       break;
579
580     case COMPLEX_EXPR:
581       if (negate_expr_p (t))
582         return fold_build2_loc (loc, COMPLEX_EXPR, type,
583                             fold_negate_expr (loc, TREE_OPERAND (t, 0)),
584                             fold_negate_expr (loc, TREE_OPERAND (t, 1)));
585       break;
586
587     case CONJ_EXPR:
588       if (negate_expr_p (t))
589         return fold_build1_loc (loc, CONJ_EXPR, type,
590                             fold_negate_expr (loc, TREE_OPERAND (t, 0)));
591       break;
592
593     case NEGATE_EXPR:
594       return TREE_OPERAND (t, 0);
595
596     case PLUS_EXPR:
597       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
598           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
599         {
600           /* -(A + B) -> (-B) - A.  */
601           if (negate_expr_p (TREE_OPERAND (t, 1))
602               && reorder_operands_p (TREE_OPERAND (t, 0),
603                                      TREE_OPERAND (t, 1)))
604             {
605               tem = negate_expr (TREE_OPERAND (t, 1));
606               return fold_build2_loc (loc, MINUS_EXPR, type,
607                                   tem, TREE_OPERAND (t, 0));
608             }
609
610           /* -(A + B) -> (-A) - B.  */
611           if (negate_expr_p (TREE_OPERAND (t, 0)))
612             {
613               tem = negate_expr (TREE_OPERAND (t, 0));
614               return fold_build2_loc (loc, MINUS_EXPR, type,
615                                   tem, TREE_OPERAND (t, 1));
616             }
617         }
618       break;
619
620     case MINUS_EXPR:
621       /* - (A - B) -> B - A  */
622       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
623           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
624           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
625         return fold_build2_loc (loc, MINUS_EXPR, type,
626                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
627       break;
628
629     case MULT_EXPR:
630       if (TYPE_UNSIGNED (type))
631         break;
632
633       /* Fall through.  */
634
635     case RDIV_EXPR:
636       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
637         {
638           tem = TREE_OPERAND (t, 1);
639           if (negate_expr_p (tem))
640             return fold_build2_loc (loc, TREE_CODE (t), type,
641                                 TREE_OPERAND (t, 0), negate_expr (tem));
642           tem = TREE_OPERAND (t, 0);
643           if (negate_expr_p (tem))
644             return fold_build2_loc (loc, TREE_CODE (t), type,
645                                 negate_expr (tem), TREE_OPERAND (t, 1));
646         }
647       break;
648
649     case TRUNC_DIV_EXPR:
650     case ROUND_DIV_EXPR:
651     case FLOOR_DIV_EXPR:
652     case CEIL_DIV_EXPR:
653     case EXACT_DIV_EXPR:
654       /* In general we can't negate A / B, because if A is INT_MIN and
655          B is 1, we may turn this into INT_MIN / -1 which is undefined
656          and actually traps on some architectures.  But if overflow is
657          undefined, we can negate, because - (INT_MIN / 1) is an
658          overflow.  */
659       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
660         {
661           const char * const warnmsg = G_("assuming signed overflow does not "
662                                           "occur when negating a division");
663           tem = TREE_OPERAND (t, 1);
664           if (negate_expr_p (tem))
665             {
666               if (INTEGRAL_TYPE_P (type)
667                   && (TREE_CODE (tem) != INTEGER_CST
668                       || integer_onep (tem)))
669                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
670               return fold_build2_loc (loc, TREE_CODE (t), type,
671                                   TREE_OPERAND (t, 0), negate_expr (tem));
672             }
673           tem = TREE_OPERAND (t, 0);
674           if (negate_expr_p (tem))
675             {
676               if (INTEGRAL_TYPE_P (type)
677                   && (TREE_CODE (tem) != INTEGER_CST
678                       || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
679                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
680               return fold_build2_loc (loc, TREE_CODE (t), type,
681                                   negate_expr (tem), TREE_OPERAND (t, 1));
682             }
683         }
684       break;
685
686     case NOP_EXPR:
687       /* Convert -((double)float) into (double)(-float).  */
688       if (TREE_CODE (type) == REAL_TYPE)
689         {
690           tem = strip_float_extensions (t);
691           if (tem != t && negate_expr_p (tem))
692             return fold_convert_loc (loc, type, negate_expr (tem));
693         }
694       break;
695
696     case CALL_EXPR:
697       /* Negate -f(x) as f(-x).  */
698       if (negate_mathfn_p (builtin_mathfn_code (t))
699           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
700         {
701           tree fndecl, arg;
702
703           fndecl = get_callee_fndecl (t);
704           arg = negate_expr (CALL_EXPR_ARG (t, 0));
705           return build_call_expr_loc (loc, fndecl, 1, arg);
706         }
707       break;
708
709     case RSHIFT_EXPR:
710       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
711       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
712         {
713           tree op1 = TREE_OPERAND (t, 1);
714           if (TREE_INT_CST_HIGH (op1) == 0
715               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
716                  == TREE_INT_CST_LOW (op1))
717             {
718               tree ntype = TYPE_UNSIGNED (type)
719                            ? signed_type_for (type)
720                            : unsigned_type_for (type);
721               tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
722               temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
723               return fold_convert_loc (loc, type, temp);
724             }
725         }
726       break;
727
728     default:
729       break;
730     }
731
732   return NULL_TREE;
733 }
734
735 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
736    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
737    return NULL_TREE. */
738
739 static tree
740 negate_expr (tree t)
741 {
742   tree type, tem;
743   location_t loc;
744
745   if (t == NULL_TREE)
746     return NULL_TREE;
747
748   loc = EXPR_LOCATION (t);
749   type = TREE_TYPE (t);
750   STRIP_SIGN_NOPS (t);
751
752   tem = fold_negate_expr (loc, t);
753   if (!tem)
754     tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
755   return fold_convert_loc (loc, type, tem);
756 }
757 \f
758 /* Split a tree IN into a constant, literal and variable parts that could be
759    combined with CODE to make IN.  "constant" means an expression with
760    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
761    commutative arithmetic operation.  Store the constant part into *CONP,
762    the literal in *LITP and return the variable part.  If a part isn't
763    present, set it to null.  If the tree does not decompose in this way,
764    return the entire tree as the variable part and the other parts as null.
765
766    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
767    case, we negate an operand that was subtracted.  Except if it is a
768    literal for which we use *MINUS_LITP instead.
769
770    If NEGATE_P is true, we are negating all of IN, again except a literal
771    for which we use *MINUS_LITP instead.
772
773    If IN is itself a literal or constant, return it as appropriate.
774
775    Note that we do not guarantee that any of the three values will be the
776    same type as IN, but they will have the same signedness and mode.  */
777
778 static tree
779 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
780             tree *minus_litp, int negate_p)
781 {
782   tree var = 0;
783
784   *conp = 0;
785   *litp = 0;
786   *minus_litp = 0;
787
788   /* Strip any conversions that don't change the machine mode or signedness.  */
789   STRIP_SIGN_NOPS (in);
790
791   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
792       || TREE_CODE (in) == FIXED_CST)
793     *litp = in;
794   else if (TREE_CODE (in) == code
795            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
796                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
797                /* We can associate addition and subtraction together (even
798                   though the C standard doesn't say so) for integers because
799                   the value is not affected.  For reals, the value might be
800                   affected, so we can't.  */
801                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
802                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
803     {
804       tree op0 = TREE_OPERAND (in, 0);
805       tree op1 = TREE_OPERAND (in, 1);
806       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
807       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
808
809       /* First see if either of the operands is a literal, then a constant.  */
810       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
811           || TREE_CODE (op0) == FIXED_CST)
812         *litp = op0, op0 = 0;
813       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
814                || TREE_CODE (op1) == FIXED_CST)
815         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
816
817       if (op0 != 0 && TREE_CONSTANT (op0))
818         *conp = op0, op0 = 0;
819       else if (op1 != 0 && TREE_CONSTANT (op1))
820         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
821
822       /* If we haven't dealt with either operand, this is not a case we can
823          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
824       if (op0 != 0 && op1 != 0)
825         var = in;
826       else if (op0 != 0)
827         var = op0;
828       else
829         var = op1, neg_var_p = neg1_p;
830
831       /* Now do any needed negations.  */
832       if (neg_litp_p)
833         *minus_litp = *litp, *litp = 0;
834       if (neg_conp_p)
835         *conp = negate_expr (*conp);
836       if (neg_var_p)
837         var = negate_expr (var);
838     }
839   else if (TREE_CONSTANT (in))
840     *conp = in;
841   else
842     var = in;
843
844   if (negate_p)
845     {
846       if (*litp)
847         *minus_litp = *litp, *litp = 0;
848       else if (*minus_litp)
849         *litp = *minus_litp, *minus_litp = 0;
850       *conp = negate_expr (*conp);
851       var = negate_expr (var);
852     }
853
854   return var;
855 }
856
857 /* Re-associate trees split by the above function.  T1 and T2 are
858    either expressions to associate or null.  Return the new
859    expression, if any.  LOC is the location of the new expression.  If
860    we build an operation, do it in TYPE and with CODE.  */
861
862 static tree
863 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
864 {
865   if (t1 == 0)
866     return t2;
867   else if (t2 == 0)
868     return t1;
869
870   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
871      try to fold this since we will have infinite recursion.  But do
872      deal with any NEGATE_EXPRs.  */
873   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
874       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
875     {
876       if (code == PLUS_EXPR)
877         {
878           if (TREE_CODE (t1) == NEGATE_EXPR)
879             return build2_loc (loc, MINUS_EXPR, type,
880                                fold_convert_loc (loc, type, t2),
881                                fold_convert_loc (loc, type,
882                                                  TREE_OPERAND (t1, 0)));
883           else if (TREE_CODE (t2) == NEGATE_EXPR)
884             return build2_loc (loc, MINUS_EXPR, type,
885                                fold_convert_loc (loc, type, t1),
886                                fold_convert_loc (loc, type,
887                                                  TREE_OPERAND (t2, 0)));
888           else if (integer_zerop (t2))
889             return fold_convert_loc (loc, type, t1);
890         }
891       else if (code == MINUS_EXPR)
892         {
893           if (integer_zerop (t2))
894             return fold_convert_loc (loc, type, t1);
895         }
896
897       return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
898                          fold_convert_loc (loc, type, t2));
899     }
900
901   return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
902                           fold_convert_loc (loc, type, t2));
903 }
904 \f
905 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
906    for use in int_const_binop, size_binop and size_diffop.  */
907
908 static bool
909 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
910 {
911   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
912     return false;
913   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
914     return false;
915
916   switch (code)
917     {
918     case LSHIFT_EXPR:
919     case RSHIFT_EXPR:
920     case LROTATE_EXPR:
921     case RROTATE_EXPR:
922       return true;
923
924     default:
925       break;
926     }
927
928   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
929          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
930          && TYPE_MODE (type1) == TYPE_MODE (type2);
931 }
932
933
934 /* Combine two integer constants ARG1 and ARG2 under operation CODE
935    to produce a new constant.  Return NULL_TREE if we don't know how
936    to evaluate CODE at compile-time.  */
937
938 tree
939 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
940 {
941   double_int op1, op2, res, tmp;
942   tree t;
943   tree type = TREE_TYPE (arg1);
944   bool uns = TYPE_UNSIGNED (type);
945   bool is_sizetype
946     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
947   bool overflow = false;
948
949   op1 = tree_to_double_int (arg1);
950   op2 = tree_to_double_int (arg2);
951
952   switch (code)
953     {
954     case BIT_IOR_EXPR:
955       res = double_int_ior (op1, op2);
956       break;
957
958     case BIT_XOR_EXPR:
959       res = double_int_xor (op1, op2);
960       break;
961
962     case BIT_AND_EXPR:
963       res = double_int_and (op1, op2);
964       break;
965
966     case RSHIFT_EXPR:
967       res = double_int_rshift (op1, double_int_to_shwi (op2),
968                                TYPE_PRECISION (type), !uns);
969       break;
970
971     case LSHIFT_EXPR:
972       /* It's unclear from the C standard whether shifts can overflow.
973          The following code ignores overflow; perhaps a C standard
974          interpretation ruling is needed.  */
975       res = double_int_lshift (op1, double_int_to_shwi (op2),
976                                TYPE_PRECISION (type), !uns);
977       break;
978
979     case RROTATE_EXPR:
980       res = double_int_rrotate (op1, double_int_to_shwi (op2),
981                                 TYPE_PRECISION (type));
982       break;
983
984     case LROTATE_EXPR:
985       res = double_int_lrotate (op1, double_int_to_shwi (op2),
986                                 TYPE_PRECISION (type));
987       break;
988
989     case PLUS_EXPR:
990       overflow = add_double (op1.low, op1.high, op2.low, op2.high,
991                              &res.low, &res.high);
992       break;
993
994     case MINUS_EXPR:
995       neg_double (op2.low, op2.high, &res.low, &res.high);
996       add_double (op1.low, op1.high, res.low, res.high,
997                   &res.low, &res.high);
998       overflow = OVERFLOW_SUM_SIGN (res.high, op2.high, op1.high);
999       break;
1000
1001     case MULT_EXPR:
1002       overflow = mul_double (op1.low, op1.high, op2.low, op2.high,
1003                              &res.low, &res.high);
1004       break;
1005
1006     case TRUNC_DIV_EXPR:
1007     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1008     case EXACT_DIV_EXPR:
1009       /* This is a shortcut for a common special case.  */
1010       if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1011           && !TREE_OVERFLOW (arg1)
1012           && !TREE_OVERFLOW (arg2)
1013           && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1014         {
1015           if (code == CEIL_DIV_EXPR)
1016             op1.low += op2.low - 1;
1017
1018           res.low = op1.low / op2.low, res.high = 0;
1019           break;
1020         }
1021
1022       /* ... fall through ...  */
1023
1024     case ROUND_DIV_EXPR:
1025       if (double_int_zero_p (op2))
1026         return NULL_TREE;
1027       if (double_int_one_p (op2))
1028         {
1029           res = op1;
1030           break;
1031         }
1032       if (double_int_equal_p (op1, op2)
1033           && ! double_int_zero_p (op1))
1034         {
1035           res = double_int_one;
1036           break;
1037         }
1038       overflow = div_and_round_double (code, uns,
1039                                        op1.low, op1.high, op2.low, op2.high,
1040                                        &res.low, &res.high,
1041                                        &tmp.low, &tmp.high);
1042       break;
1043
1044     case TRUNC_MOD_EXPR:
1045     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1046       /* This is a shortcut for a common special case.  */
1047       if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1048           && !TREE_OVERFLOW (arg1)
1049           && !TREE_OVERFLOW (arg2)
1050           && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1051         {
1052           if (code == CEIL_MOD_EXPR)
1053             op1.low += op2.low - 1;
1054           res.low = op1.low % op2.low, res.high = 0;
1055           break;
1056         }
1057
1058       /* ... fall through ...  */
1059
1060     case ROUND_MOD_EXPR:
1061       if (double_int_zero_p (op2))
1062         return NULL_TREE;
1063       overflow = div_and_round_double (code, uns,
1064                                        op1.low, op1.high, op2.low, op2.high,
1065                                        &tmp.low, &tmp.high,
1066                                        &res.low, &res.high);
1067       break;
1068
1069     case MIN_EXPR:
1070       res = double_int_min (op1, op2, uns);
1071       break;
1072
1073     case MAX_EXPR:
1074       res = double_int_max (op1, op2, uns);
1075       break;
1076
1077     default:
1078       return NULL_TREE;
1079     }
1080
1081   t = force_fit_type_double (TREE_TYPE (arg1), res, 1,
1082                              ((!uns || is_sizetype) && overflow)
1083                              | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1084
1085   return t;
1086 }
1087
1088 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1089    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1090    are the same kind of constant and the same machine mode.  Return zero if
1091    combining the constants is not allowed in the current operating mode.  */
1092
1093 static tree
1094 const_binop (enum tree_code code, tree arg1, tree arg2)
1095 {
1096   /* Sanity check for the recursive cases.  */
1097   if (!arg1 || !arg2)
1098     return NULL_TREE;
1099
1100   STRIP_NOPS (arg1);
1101   STRIP_NOPS (arg2);
1102
1103   if (TREE_CODE (arg1) == INTEGER_CST)
1104     return int_const_binop (code, arg1, arg2);
1105
1106   if (TREE_CODE (arg1) == REAL_CST)
1107     {
1108       enum machine_mode mode;
1109       REAL_VALUE_TYPE d1;
1110       REAL_VALUE_TYPE d2;
1111       REAL_VALUE_TYPE value;
1112       REAL_VALUE_TYPE result;
1113       bool inexact;
1114       tree t, type;
1115
1116       /* The following codes are handled by real_arithmetic.  */
1117       switch (code)
1118         {
1119         case PLUS_EXPR:
1120         case MINUS_EXPR:
1121         case MULT_EXPR:
1122         case RDIV_EXPR:
1123         case MIN_EXPR:
1124         case MAX_EXPR:
1125           break;
1126
1127         default:
1128           return NULL_TREE;
1129         }
1130
1131       d1 = TREE_REAL_CST (arg1);
1132       d2 = TREE_REAL_CST (arg2);
1133
1134       type = TREE_TYPE (arg1);
1135       mode = TYPE_MODE (type);
1136
1137       /* Don't perform operation if we honor signaling NaNs and
1138          either operand is a NaN.  */
1139       if (HONOR_SNANS (mode)
1140           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1141         return NULL_TREE;
1142
1143       /* Don't perform operation if it would raise a division
1144          by zero exception.  */
1145       if (code == RDIV_EXPR
1146           && REAL_VALUES_EQUAL (d2, dconst0)
1147           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1148         return NULL_TREE;
1149
1150       /* If either operand is a NaN, just return it.  Otherwise, set up
1151          for floating-point trap; we return an overflow.  */
1152       if (REAL_VALUE_ISNAN (d1))
1153         return arg1;
1154       else if (REAL_VALUE_ISNAN (d2))
1155         return arg2;
1156
1157       inexact = real_arithmetic (&value, code, &d1, &d2);
1158       real_convert (&result, mode, &value);
1159
1160       /* Don't constant fold this floating point operation if
1161          the result has overflowed and flag_trapping_math.  */
1162       if (flag_trapping_math
1163           && MODE_HAS_INFINITIES (mode)
1164           && REAL_VALUE_ISINF (result)
1165           && !REAL_VALUE_ISINF (d1)
1166           && !REAL_VALUE_ISINF (d2))
1167         return NULL_TREE;
1168
1169       /* Don't constant fold this floating point operation if the
1170          result may dependent upon the run-time rounding mode and
1171          flag_rounding_math is set, or if GCC's software emulation
1172          is unable to accurately represent the result.  */
1173       if ((flag_rounding_math
1174            || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1175           && (inexact || !real_identical (&result, &value)))
1176         return NULL_TREE;
1177
1178       t = build_real (type, result);
1179
1180       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1181       return t;
1182     }
1183
1184   if (TREE_CODE (arg1) == FIXED_CST)
1185     {
1186       FIXED_VALUE_TYPE f1;
1187       FIXED_VALUE_TYPE f2;
1188       FIXED_VALUE_TYPE result;
1189       tree t, type;
1190       int sat_p;
1191       bool overflow_p;
1192
1193       /* The following codes are handled by fixed_arithmetic.  */
1194       switch (code)
1195         {
1196         case PLUS_EXPR:
1197         case MINUS_EXPR:
1198         case MULT_EXPR:
1199         case TRUNC_DIV_EXPR:
1200           f2 = TREE_FIXED_CST (arg2);
1201           break;
1202
1203         case LSHIFT_EXPR:
1204         case RSHIFT_EXPR:
1205           f2.data.high = TREE_INT_CST_HIGH (arg2);
1206           f2.data.low = TREE_INT_CST_LOW (arg2);
1207           f2.mode = SImode;
1208           break;
1209
1210         default:
1211           return NULL_TREE;
1212         }
1213
1214       f1 = TREE_FIXED_CST (arg1);
1215       type = TREE_TYPE (arg1);
1216       sat_p = TYPE_SATURATING (type);
1217       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1218       t = build_fixed (type, result);
1219       /* Propagate overflow flags.  */
1220       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1221         TREE_OVERFLOW (t) = 1;
1222       return t;
1223     }
1224
1225   if (TREE_CODE (arg1) == COMPLEX_CST)
1226     {
1227       tree type = TREE_TYPE (arg1);
1228       tree r1 = TREE_REALPART (arg1);
1229       tree i1 = TREE_IMAGPART (arg1);
1230       tree r2 = TREE_REALPART (arg2);
1231       tree i2 = TREE_IMAGPART (arg2);
1232       tree real, imag;
1233
1234       switch (code)
1235         {
1236         case PLUS_EXPR:
1237         case MINUS_EXPR:
1238           real = const_binop (code, r1, r2);
1239           imag = const_binop (code, i1, i2);
1240           break;
1241
1242         case MULT_EXPR:
1243           if (COMPLEX_FLOAT_TYPE_P (type))
1244             return do_mpc_arg2 (arg1, arg2, type,
1245                                 /* do_nonfinite= */ folding_initializer,
1246                                 mpc_mul);
1247
1248           real = const_binop (MINUS_EXPR,
1249                               const_binop (MULT_EXPR, r1, r2),
1250                               const_binop (MULT_EXPR, i1, i2));
1251           imag = const_binop (PLUS_EXPR,
1252                               const_binop (MULT_EXPR, r1, i2),
1253                               const_binop (MULT_EXPR, i1, r2));
1254           break;
1255
1256         case RDIV_EXPR:
1257           if (COMPLEX_FLOAT_TYPE_P (type))
1258             return do_mpc_arg2 (arg1, arg2, type,
1259                                 /* do_nonfinite= */ folding_initializer,
1260                                 mpc_div);
1261           /* Fallthru ... */
1262         case TRUNC_DIV_EXPR:
1263         case CEIL_DIV_EXPR:
1264         case FLOOR_DIV_EXPR:
1265         case ROUND_DIV_EXPR:
1266           if (flag_complex_method == 0)
1267           {
1268             /* Keep this algorithm in sync with
1269                tree-complex.c:expand_complex_div_straight().
1270
1271                Expand complex division to scalars, straightforward algorithm.
1272                a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1273                t = br*br + bi*bi
1274             */
1275             tree magsquared
1276               = const_binop (PLUS_EXPR,
1277                              const_binop (MULT_EXPR, r2, r2),
1278                              const_binop (MULT_EXPR, i2, i2));
1279             tree t1
1280               = const_binop (PLUS_EXPR,
1281                              const_binop (MULT_EXPR, r1, r2),
1282                              const_binop (MULT_EXPR, i1, i2));
1283             tree t2
1284               = const_binop (MINUS_EXPR,
1285                              const_binop (MULT_EXPR, i1, r2),
1286                              const_binop (MULT_EXPR, r1, i2));
1287
1288             real = const_binop (code, t1, magsquared);
1289             imag = const_binop (code, t2, magsquared);
1290           }
1291           else
1292           {
1293             /* Keep this algorithm in sync with
1294                tree-complex.c:expand_complex_div_wide().
1295
1296                Expand complex division to scalars, modified algorithm to minimize
1297                overflow with wide input ranges.  */
1298             tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1299                                         fold_abs_const (r2, TREE_TYPE (type)),
1300                                         fold_abs_const (i2, TREE_TYPE (type)));
1301
1302             if (integer_nonzerop (compare))
1303               {
1304                 /* In the TRUE branch, we compute
1305                    ratio = br/bi;
1306                    div = (br * ratio) + bi;
1307                    tr = (ar * ratio) + ai;
1308                    ti = (ai * ratio) - ar;
1309                    tr = tr / div;
1310                    ti = ti / div;  */
1311                 tree ratio = const_binop (code, r2, i2);
1312                 tree div = const_binop (PLUS_EXPR, i2,
1313                                         const_binop (MULT_EXPR, r2, ratio));
1314                 real = const_binop (MULT_EXPR, r1, ratio);
1315                 real = const_binop (PLUS_EXPR, real, i1);
1316                 real = const_binop (code, real, div);
1317
1318                 imag = const_binop (MULT_EXPR, i1, ratio);
1319                 imag = const_binop (MINUS_EXPR, imag, r1);
1320                 imag = const_binop (code, imag, div);
1321               }
1322             else
1323               {
1324                 /* In the FALSE branch, we compute
1325                    ratio = d/c;
1326                    divisor = (d * ratio) + c;
1327                    tr = (b * ratio) + a;
1328                    ti = b - (a * ratio);
1329                    tr = tr / div;
1330                    ti = ti / div;  */
1331                 tree ratio = const_binop (code, i2, r2);
1332                 tree div = const_binop (PLUS_EXPR, r2,
1333                                         const_binop (MULT_EXPR, i2, ratio));
1334
1335                 real = const_binop (MULT_EXPR, i1, ratio);
1336                 real = const_binop (PLUS_EXPR, real, r1);
1337                 real = const_binop (code, real, div);
1338
1339                 imag = const_binop (MULT_EXPR, r1, ratio);
1340                 imag = const_binop (MINUS_EXPR, i1, imag);
1341                 imag = const_binop (code, imag, div);
1342               }
1343           }
1344           break;
1345
1346         default:
1347           return NULL_TREE;
1348         }
1349
1350       if (real && imag)
1351         return build_complex (type, real, imag);
1352     }
1353
1354   if (TREE_CODE (arg1) == VECTOR_CST)
1355     {
1356       tree type = TREE_TYPE(arg1);
1357       int count = TYPE_VECTOR_SUBPARTS (type), i;
1358       tree elements1, elements2, list = NULL_TREE;
1359
1360       if(TREE_CODE(arg2) != VECTOR_CST)
1361         return NULL_TREE;
1362
1363       elements1 = TREE_VECTOR_CST_ELTS (arg1);
1364       elements2 = TREE_VECTOR_CST_ELTS (arg2);
1365
1366       for (i = 0; i < count; i++)
1367         {
1368           tree elem1, elem2, elem;
1369
1370           /* The trailing elements can be empty and should be treated as 0 */
1371           if(!elements1)
1372             elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1373           else
1374             {
1375               elem1 = TREE_VALUE(elements1);
1376               elements1 = TREE_CHAIN (elements1);
1377             }
1378
1379           if(!elements2)
1380             elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1381           else
1382             {
1383               elem2 = TREE_VALUE(elements2);
1384               elements2 = TREE_CHAIN (elements2);
1385             }
1386
1387           elem = const_binop (code, elem1, elem2);
1388
1389           /* It is possible that const_binop cannot handle the given
1390             code and return NULL_TREE */
1391           if(elem == NULL_TREE)
1392             return NULL_TREE;
1393
1394           list = tree_cons (NULL_TREE, elem, list);
1395         }
1396       return build_vector(type, nreverse(list));
1397     }
1398   return NULL_TREE;
1399 }
1400
1401 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
1402    indicates which particular sizetype to create.  */
1403
1404 tree
1405 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1406 {
1407   return build_int_cst (sizetype_tab[(int) kind], number);
1408 }
1409 \f
1410 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
1411    is a tree code.  The type of the result is taken from the operands.
1412    Both must be equivalent integer types, ala int_binop_types_match_p.
1413    If the operands are constant, so is the result.  */
1414
1415 tree
1416 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1417 {
1418   tree type = TREE_TYPE (arg0);
1419
1420   if (arg0 == error_mark_node || arg1 == error_mark_node)
1421     return error_mark_node;
1422
1423   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1424                                        TREE_TYPE (arg1)));
1425
1426   /* Handle the special case of two integer constants faster.  */
1427   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1428     {
1429       /* And some specific cases even faster than that.  */
1430       if (code == PLUS_EXPR)
1431         {
1432           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1433             return arg1;
1434           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1435             return arg0;
1436         }
1437       else if (code == MINUS_EXPR)
1438         {
1439           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1440             return arg0;
1441         }
1442       else if (code == MULT_EXPR)
1443         {
1444           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1445             return arg1;
1446         }
1447
1448       /* Handle general case of two integer constants.  */
1449       return int_const_binop (code, arg0, arg1);
1450     }
1451
1452   return fold_build2_loc (loc, code, type, arg0, arg1);
1453 }
1454
1455 /* Given two values, either both of sizetype or both of bitsizetype,
1456    compute the difference between the two values.  Return the value
1457    in signed type corresponding to the type of the operands.  */
1458
1459 tree
1460 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1461 {
1462   tree type = TREE_TYPE (arg0);
1463   tree ctype;
1464
1465   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1466                                        TREE_TYPE (arg1)));
1467
1468   /* If the type is already signed, just do the simple thing.  */
1469   if (!TYPE_UNSIGNED (type))
1470     return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1471
1472   if (type == sizetype)
1473     ctype = ssizetype;
1474   else if (type == bitsizetype)
1475     ctype = sbitsizetype;
1476   else
1477     ctype = signed_type_for (type);
1478
1479   /* If either operand is not a constant, do the conversions to the signed
1480      type and subtract.  The hardware will do the right thing with any
1481      overflow in the subtraction.  */
1482   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1483     return size_binop_loc (loc, MINUS_EXPR,
1484                            fold_convert_loc (loc, ctype, arg0),
1485                            fold_convert_loc (loc, ctype, arg1));
1486
1487   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1488      Otherwise, subtract the other way, convert to CTYPE (we know that can't
1489      overflow) and negate (which can't either).  Special-case a result
1490      of zero while we're here.  */
1491   if (tree_int_cst_equal (arg0, arg1))
1492     return build_int_cst (ctype, 0);
1493   else if (tree_int_cst_lt (arg1, arg0))
1494     return fold_convert_loc (loc, ctype,
1495                              size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1496   else
1497     return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1498                            fold_convert_loc (loc, ctype,
1499                                              size_binop_loc (loc,
1500                                                              MINUS_EXPR,
1501                                                              arg1, arg0)));
1502 }
1503 \f
1504 /* A subroutine of fold_convert_const handling conversions of an
1505    INTEGER_CST to another integer type.  */
1506
1507 static tree
1508 fold_convert_const_int_from_int (tree type, const_tree arg1)
1509 {
1510   tree t;
1511
1512   /* Given an integer constant, make new constant with new type,
1513      appropriately sign-extended or truncated.  */
1514   t = force_fit_type_double (type, tree_to_double_int (arg1),
1515                              !POINTER_TYPE_P (TREE_TYPE (arg1)),
1516                              (TREE_INT_CST_HIGH (arg1) < 0
1517                               && (TYPE_UNSIGNED (type)
1518                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1519                              | TREE_OVERFLOW (arg1));
1520
1521   return t;
1522 }
1523
1524 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1525    to an integer type.  */
1526
1527 static tree
1528 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1529 {
1530   int overflow = 0;
1531   tree t;
1532
1533   /* The following code implements the floating point to integer
1534      conversion rules required by the Java Language Specification,
1535      that IEEE NaNs are mapped to zero and values that overflow
1536      the target precision saturate, i.e. values greater than
1537      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1538      are mapped to INT_MIN.  These semantics are allowed by the
1539      C and C++ standards that simply state that the behavior of
1540      FP-to-integer conversion is unspecified upon overflow.  */
1541
1542   double_int val;
1543   REAL_VALUE_TYPE r;
1544   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1545
1546   switch (code)
1547     {
1548     case FIX_TRUNC_EXPR:
1549       real_trunc (&r, VOIDmode, &x);
1550       break;
1551
1552     default:
1553       gcc_unreachable ();
1554     }
1555
1556   /* If R is NaN, return zero and show we have an overflow.  */
1557   if (REAL_VALUE_ISNAN (r))
1558     {
1559       overflow = 1;
1560       val = double_int_zero;
1561     }
1562
1563   /* See if R is less than the lower bound or greater than the
1564      upper bound.  */
1565
1566   if (! overflow)
1567     {
1568       tree lt = TYPE_MIN_VALUE (type);
1569       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1570       if (REAL_VALUES_LESS (r, l))
1571         {
1572           overflow = 1;
1573           val = tree_to_double_int (lt);
1574         }
1575     }
1576
1577   if (! overflow)
1578     {
1579       tree ut = TYPE_MAX_VALUE (type);
1580       if (ut)
1581         {
1582           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1583           if (REAL_VALUES_LESS (u, r))
1584             {
1585               overflow = 1;
1586               val = tree_to_double_int (ut);
1587             }
1588         }
1589     }
1590
1591   if (! overflow)
1592     real_to_integer2 ((HOST_WIDE_INT *) &val.low, &val.high, &r);
1593
1594   t = force_fit_type_double (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1595   return t;
1596 }
1597
1598 /* A subroutine of fold_convert_const handling conversions of a
1599    FIXED_CST to an integer type.  */
1600
1601 static tree
1602 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1603 {
1604   tree t;
1605   double_int temp, temp_trunc;
1606   unsigned int mode;
1607
1608   /* Right shift FIXED_CST to temp by fbit.  */
1609   temp = TREE_FIXED_CST (arg1).data;
1610   mode = TREE_FIXED_CST (arg1).mode;
1611   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
1612     {
1613       temp = double_int_rshift (temp, GET_MODE_FBIT (mode),
1614                                 HOST_BITS_PER_DOUBLE_INT,
1615                                 SIGNED_FIXED_POINT_MODE_P (mode));
1616
1617       /* Left shift temp to temp_trunc by fbit.  */
1618       temp_trunc = double_int_lshift (temp, GET_MODE_FBIT (mode),
1619                                       HOST_BITS_PER_DOUBLE_INT,
1620                                       SIGNED_FIXED_POINT_MODE_P (mode));
1621     }
1622   else
1623     {
1624       temp = double_int_zero;
1625       temp_trunc = double_int_zero;
1626     }
1627
1628   /* If FIXED_CST is negative, we need to round the value toward 0.
1629      By checking if the fractional bits are not zero to add 1 to temp.  */
1630   if (SIGNED_FIXED_POINT_MODE_P (mode)
1631       && double_int_negative_p (temp_trunc)
1632       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
1633     temp = double_int_add (temp, double_int_one);
1634
1635   /* Given a fixed-point constant, make new constant with new type,
1636      appropriately sign-extended or truncated.  */
1637   t = force_fit_type_double (type, temp, -1,
1638                              (double_int_negative_p (temp)
1639                               && (TYPE_UNSIGNED (type)
1640                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1641                              | TREE_OVERFLOW (arg1));
1642
1643   return t;
1644 }
1645
1646 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1647    to another floating point type.  */
1648
1649 static tree
1650 fold_convert_const_real_from_real (tree type, const_tree arg1)
1651 {
1652   REAL_VALUE_TYPE value;
1653   tree t;
1654
1655   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1656   t = build_real (type, value);
1657
1658   /* If converting an infinity or NAN to a representation that doesn't
1659      have one, set the overflow bit so that we can produce some kind of
1660      error message at the appropriate point if necessary.  It's not the
1661      most user-friendly message, but it's better than nothing.  */
1662   if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1663       && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1664     TREE_OVERFLOW (t) = 1;
1665   else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1666            && !MODE_HAS_NANS (TYPE_MODE (type)))
1667     TREE_OVERFLOW (t) = 1;
1668   /* Regular overflow, conversion produced an infinity in a mode that
1669      can't represent them.  */
1670   else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1671            && REAL_VALUE_ISINF (value)
1672            && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1673     TREE_OVERFLOW (t) = 1;
1674   else
1675     TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1676   return t;
1677 }
1678
1679 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1680    to a floating point type.  */
1681
1682 static tree
1683 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
1684 {
1685   REAL_VALUE_TYPE value;
1686   tree t;
1687
1688   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
1689   t = build_real (type, value);
1690
1691   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1692   return t;
1693 }
1694
1695 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1696    to another fixed-point type.  */
1697
1698 static tree
1699 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
1700 {
1701   FIXED_VALUE_TYPE value;
1702   tree t;
1703   bool overflow_p;
1704
1705   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
1706                               TYPE_SATURATING (type));
1707   t = build_fixed (type, value);
1708
1709   /* Propagate overflow flags.  */
1710   if (overflow_p | TREE_OVERFLOW (arg1))
1711     TREE_OVERFLOW (t) = 1;
1712   return t;
1713 }
1714
1715 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
1716    to a fixed-point type.  */
1717
1718 static tree
1719 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
1720 {
1721   FIXED_VALUE_TYPE value;
1722   tree t;
1723   bool overflow_p;
1724
1725   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
1726                                        TREE_INT_CST (arg1),
1727                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
1728                                        TYPE_SATURATING (type));
1729   t = build_fixed (type, value);
1730
1731   /* Propagate overflow flags.  */
1732   if (overflow_p | TREE_OVERFLOW (arg1))
1733     TREE_OVERFLOW (t) = 1;
1734   return t;
1735 }
1736
1737 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1738    to a fixed-point type.  */
1739
1740 static tree
1741 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
1742 {
1743   FIXED_VALUE_TYPE value;
1744   tree t;
1745   bool overflow_p;
1746
1747   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
1748                                         &TREE_REAL_CST (arg1),
1749                                         TYPE_SATURATING (type));
1750   t = build_fixed (type, value);
1751
1752   /* Propagate overflow flags.  */
1753   if (overflow_p | TREE_OVERFLOW (arg1))
1754     TREE_OVERFLOW (t) = 1;
1755   return t;
1756 }
1757
1758 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1759    type TYPE.  If no simplification can be done return NULL_TREE.  */
1760
1761 static tree
1762 fold_convert_const (enum tree_code code, tree type, tree arg1)
1763 {
1764   if (TREE_TYPE (arg1) == type)
1765     return arg1;
1766
1767   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
1768       || TREE_CODE (type) == OFFSET_TYPE)
1769     {
1770       if (TREE_CODE (arg1) == INTEGER_CST)
1771         return fold_convert_const_int_from_int (type, arg1);
1772       else if (TREE_CODE (arg1) == REAL_CST)
1773         return fold_convert_const_int_from_real (code, type, arg1);
1774       else if (TREE_CODE (arg1) == FIXED_CST)
1775         return fold_convert_const_int_from_fixed (type, arg1);
1776     }
1777   else if (TREE_CODE (type) == REAL_TYPE)
1778     {
1779       if (TREE_CODE (arg1) == INTEGER_CST)
1780         return build_real_from_int_cst (type, arg1);
1781       else if (TREE_CODE (arg1) == REAL_CST)
1782         return fold_convert_const_real_from_real (type, arg1);
1783       else if (TREE_CODE (arg1) == FIXED_CST)
1784         return fold_convert_const_real_from_fixed (type, arg1);
1785     }
1786   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
1787     {
1788       if (TREE_CODE (arg1) == FIXED_CST)
1789         return fold_convert_const_fixed_from_fixed (type, arg1);
1790       else if (TREE_CODE (arg1) == INTEGER_CST)
1791         return fold_convert_const_fixed_from_int (type, arg1);
1792       else if (TREE_CODE (arg1) == REAL_CST)
1793         return fold_convert_const_fixed_from_real (type, arg1);
1794     }
1795   return NULL_TREE;
1796 }
1797
1798 /* Construct a vector of zero elements of vector type TYPE.  */
1799
1800 static tree
1801 build_zero_vector (tree type)
1802 {
1803   tree t;
1804
1805   t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1806   return build_vector_from_val (type, t);
1807 }
1808
1809 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
1810
1811 bool
1812 fold_convertible_p (const_tree type, const_tree arg)
1813 {
1814   tree orig = TREE_TYPE (arg);
1815
1816   if (type == orig)
1817     return true;
1818
1819   if (TREE_CODE (arg) == ERROR_MARK
1820       || TREE_CODE (type) == ERROR_MARK
1821       || TREE_CODE (orig) == ERROR_MARK)
1822     return false;
1823
1824   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1825     return true;
1826
1827   switch (TREE_CODE (type))
1828     {
1829     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1830     case POINTER_TYPE: case REFERENCE_TYPE:
1831     case OFFSET_TYPE:
1832       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1833           || TREE_CODE (orig) == OFFSET_TYPE)
1834         return true;
1835       return (TREE_CODE (orig) == VECTOR_TYPE
1836               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1837
1838     case REAL_TYPE:
1839     case FIXED_POINT_TYPE:
1840     case COMPLEX_TYPE:
1841     case VECTOR_TYPE:
1842     case VOID_TYPE:
1843       return TREE_CODE (type) == TREE_CODE (orig);
1844
1845     default:
1846       return false;
1847     }
1848 }
1849
1850 /* Convert expression ARG to type TYPE.  Used by the middle-end for
1851    simple conversions in preference to calling the front-end's convert.  */
1852
1853 tree
1854 fold_convert_loc (location_t loc, tree type, tree arg)
1855 {
1856   tree orig = TREE_TYPE (arg);
1857   tree tem;
1858
1859   if (type == orig)
1860     return arg;
1861
1862   if (TREE_CODE (arg) == ERROR_MARK
1863       || TREE_CODE (type) == ERROR_MARK
1864       || TREE_CODE (orig) == ERROR_MARK)
1865     return error_mark_node;
1866
1867   switch (TREE_CODE (type))
1868     {
1869     case POINTER_TYPE:
1870     case REFERENCE_TYPE:
1871       /* Handle conversions between pointers to different address spaces.  */
1872       if (POINTER_TYPE_P (orig)
1873           && (TYPE_ADDR_SPACE (TREE_TYPE (type))
1874               != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
1875         return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
1876       /* fall through */
1877
1878     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1879     case OFFSET_TYPE:
1880       if (TREE_CODE (arg) == INTEGER_CST)
1881         {
1882           tem = fold_convert_const (NOP_EXPR, type, arg);
1883           if (tem != NULL_TREE)
1884             return tem;
1885         }
1886       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1887           || TREE_CODE (orig) == OFFSET_TYPE)
1888         return fold_build1_loc (loc, NOP_EXPR, type, arg);
1889       if (TREE_CODE (orig) == COMPLEX_TYPE)
1890         return fold_convert_loc (loc, type,
1891                              fold_build1_loc (loc, REALPART_EXPR,
1892                                           TREE_TYPE (orig), arg));
1893       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
1894                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1895       return fold_build1_loc (loc, NOP_EXPR, type, arg);
1896
1897     case REAL_TYPE:
1898       if (TREE_CODE (arg) == INTEGER_CST)
1899         {
1900           tem = fold_convert_const (FLOAT_EXPR, type, arg);
1901           if (tem != NULL_TREE)
1902             return tem;
1903         }
1904       else if (TREE_CODE (arg) == REAL_CST)
1905         {
1906           tem = fold_convert_const (NOP_EXPR, type, arg);
1907           if (tem != NULL_TREE)
1908             return tem;
1909         }
1910       else if (TREE_CODE (arg) == FIXED_CST)
1911         {
1912           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1913           if (tem != NULL_TREE)
1914             return tem;
1915         }
1916
1917       switch (TREE_CODE (orig))
1918         {
1919         case INTEGER_TYPE:
1920         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1921         case POINTER_TYPE: case REFERENCE_TYPE:
1922           return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
1923
1924         case REAL_TYPE:
1925           return fold_build1_loc (loc, NOP_EXPR, type, arg);
1926
1927         case FIXED_POINT_TYPE:
1928           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1929
1930         case COMPLEX_TYPE:
1931           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1932           return fold_convert_loc (loc, type, tem);
1933
1934         default:
1935           gcc_unreachable ();
1936         }
1937
1938     case FIXED_POINT_TYPE:
1939       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
1940           || TREE_CODE (arg) == REAL_CST)
1941         {
1942           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1943           if (tem != NULL_TREE)
1944             goto fold_convert_exit;
1945         }
1946
1947       switch (TREE_CODE (orig))
1948         {
1949         case FIXED_POINT_TYPE:
1950         case INTEGER_TYPE:
1951         case ENUMERAL_TYPE:
1952         case BOOLEAN_TYPE:
1953         case REAL_TYPE:
1954           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1955
1956         case COMPLEX_TYPE:
1957           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1958           return fold_convert_loc (loc, type, tem);
1959
1960         default:
1961           gcc_unreachable ();
1962         }
1963
1964     case COMPLEX_TYPE:
1965       switch (TREE_CODE (orig))
1966         {
1967         case INTEGER_TYPE:
1968         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1969         case POINTER_TYPE: case REFERENCE_TYPE:
1970         case REAL_TYPE:
1971         case FIXED_POINT_TYPE:
1972           return fold_build2_loc (loc, COMPLEX_EXPR, type,
1973                               fold_convert_loc (loc, TREE_TYPE (type), arg),
1974                               fold_convert_loc (loc, TREE_TYPE (type),
1975                                             integer_zero_node));
1976         case COMPLEX_TYPE:
1977           {
1978             tree rpart, ipart;
1979
1980             if (TREE_CODE (arg) == COMPLEX_EXPR)
1981               {
1982                 rpart = fold_convert_loc (loc, TREE_TYPE (type),
1983                                       TREE_OPERAND (arg, 0));
1984                 ipart = fold_convert_loc (loc, TREE_TYPE (type),
1985                                       TREE_OPERAND (arg, 1));
1986                 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
1987               }
1988
1989             arg = save_expr (arg);
1990             rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1991             ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
1992             rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
1993             ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
1994             return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
1995           }
1996
1997         default:
1998           gcc_unreachable ();
1999         }
2000
2001     case VECTOR_TYPE:
2002       if (integer_zerop (arg))
2003         return build_zero_vector (type);
2004       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2005       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2006                   || TREE_CODE (orig) == VECTOR_TYPE);
2007       return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2008
2009     case VOID_TYPE:
2010       tem = fold_ignored_result (arg);
2011       return fold_build1_loc (loc, NOP_EXPR, type, tem);
2012
2013     default:
2014       if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2015         return fold_build1_loc (loc, NOP_EXPR, type, arg);
2016       gcc_unreachable ();
2017     }
2018  fold_convert_exit:
2019   protected_set_expr_location_unshare (tem, loc);
2020   return tem;
2021 }
2022 \f
2023 /* Return false if expr can be assumed not to be an lvalue, true
2024    otherwise.  */
2025
2026 static bool
2027 maybe_lvalue_p (const_tree x)
2028 {
2029   /* We only need to wrap lvalue tree codes.  */
2030   switch (TREE_CODE (x))
2031   {
2032   case VAR_DECL:
2033   case PARM_DECL:
2034   case RESULT_DECL:
2035   case LABEL_DECL:
2036   case FUNCTION_DECL:
2037   case SSA_NAME:
2038
2039   case COMPONENT_REF:
2040   case MEM_REF:
2041   case INDIRECT_REF:
2042   case ARRAY_REF:
2043   case ARRAY_RANGE_REF:
2044   case BIT_FIELD_REF:
2045   case OBJ_TYPE_REF:
2046
2047   case REALPART_EXPR:
2048   case IMAGPART_EXPR:
2049   case PREINCREMENT_EXPR:
2050   case PREDECREMENT_EXPR:
2051   case SAVE_EXPR:
2052   case TRY_CATCH_EXPR:
2053   case WITH_CLEANUP_EXPR:
2054   case COMPOUND_EXPR:
2055   case MODIFY_EXPR:
2056   case TARGET_EXPR:
2057   case COND_EXPR:
2058   case BIND_EXPR:
2059     break;
2060
2061   default:
2062     /* Assume the worst for front-end tree codes.  */
2063     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2064       break;
2065     return false;
2066   }
2067
2068   return true;
2069 }
2070
2071 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2072
2073 tree
2074 non_lvalue_loc (location_t loc, tree x)
2075 {
2076   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2077      us.  */
2078   if (in_gimple_form)
2079     return x;
2080
2081   if (! maybe_lvalue_p (x))
2082     return x;
2083   return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2084 }
2085
2086 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2087    Zero means allow extended lvalues.  */
2088
2089 int pedantic_lvalues;
2090
2091 /* When pedantic, return an expr equal to X but certainly not valid as a
2092    pedantic lvalue.  Otherwise, return X.  */
2093
2094 static tree
2095 pedantic_non_lvalue_loc (location_t loc, tree x)
2096 {
2097   if (pedantic_lvalues)
2098     return non_lvalue_loc (loc, x);
2099
2100   return protected_set_expr_location_unshare (x, loc);
2101 }
2102 \f
2103 /* Given a tree comparison code, return the code that is the logical inverse.
2104    It is generally not safe to do this for floating-point comparisons, except
2105    for EQ_EXPR and NE_EXPR, so we return ERROR_MARK in this case.  */
2106
2107 enum tree_code
2108 invert_tree_comparison (enum tree_code code, bool honor_nans)
2109 {
2110   if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR)
2111     return ERROR_MARK;
2112
2113   switch (code)
2114     {
2115     case EQ_EXPR:
2116       return NE_EXPR;
2117     case NE_EXPR:
2118       return EQ_EXPR;
2119     case GT_EXPR:
2120       return honor_nans ? UNLE_EXPR : LE_EXPR;
2121     case GE_EXPR:
2122       return honor_nans ? UNLT_EXPR : LT_EXPR;
2123     case LT_EXPR:
2124       return honor_nans ? UNGE_EXPR : GE_EXPR;
2125     case LE_EXPR:
2126       return honor_nans ? UNGT_EXPR : GT_EXPR;
2127     case LTGT_EXPR:
2128       return UNEQ_EXPR;
2129     case UNEQ_EXPR:
2130       return LTGT_EXPR;
2131     case UNGT_EXPR:
2132       return LE_EXPR;
2133     case UNGE_EXPR:
2134       return LT_EXPR;
2135     case UNLT_EXPR:
2136       return GE_EXPR;
2137     case UNLE_EXPR:
2138       return GT_EXPR;
2139     case ORDERED_EXPR:
2140       return UNORDERED_EXPR;
2141     case UNORDERED_EXPR:
2142       return ORDERED_EXPR;
2143     default:
2144       gcc_unreachable ();
2145     }
2146 }
2147
2148 /* Similar, but return the comparison that results if the operands are
2149    swapped.  This is safe for floating-point.  */
2150
2151 enum tree_code
2152 swap_tree_comparison (enum tree_code code)
2153 {
2154   switch (code)
2155     {
2156     case EQ_EXPR:
2157     case NE_EXPR:
2158     case ORDERED_EXPR:
2159     case UNORDERED_EXPR:
2160     case LTGT_EXPR:
2161     case UNEQ_EXPR:
2162       return code;
2163     case GT_EXPR:
2164       return LT_EXPR;
2165     case GE_EXPR:
2166       return LE_EXPR;
2167     case LT_EXPR:
2168       return GT_EXPR;
2169     case LE_EXPR:
2170       return GE_EXPR;
2171     case UNGT_EXPR:
2172       return UNLT_EXPR;
2173     case UNGE_EXPR:
2174       return UNLE_EXPR;
2175     case UNLT_EXPR:
2176       return UNGT_EXPR;
2177     case UNLE_EXPR:
2178       return UNGE_EXPR;
2179     default:
2180       gcc_unreachable ();
2181     }
2182 }
2183
2184
2185 /* Convert a comparison tree code from an enum tree_code representation
2186    into a compcode bit-based encoding.  This function is the inverse of
2187    compcode_to_comparison.  */
2188
2189 static enum comparison_code
2190 comparison_to_compcode (enum tree_code code)
2191 {
2192   switch (code)
2193     {
2194     case LT_EXPR:
2195       return COMPCODE_LT;
2196     case EQ_EXPR:
2197       return COMPCODE_EQ;
2198     case LE_EXPR:
2199       return COMPCODE_LE;
2200     case GT_EXPR:
2201       return COMPCODE_GT;
2202     case NE_EXPR:
2203       return COMPCODE_NE;
2204     case GE_EXPR:
2205       return COMPCODE_GE;
2206     case ORDERED_EXPR:
2207       return COMPCODE_ORD;
2208     case UNORDERED_EXPR:
2209       return COMPCODE_UNORD;
2210     case UNLT_EXPR:
2211       return COMPCODE_UNLT;
2212     case UNEQ_EXPR:
2213       return COMPCODE_UNEQ;
2214     case UNLE_EXPR:
2215       return COMPCODE_UNLE;
2216     case UNGT_EXPR:
2217       return COMPCODE_UNGT;
2218     case LTGT_EXPR:
2219       return COMPCODE_LTGT;
2220     case UNGE_EXPR:
2221       return COMPCODE_UNGE;
2222     default:
2223       gcc_unreachable ();
2224     }
2225 }
2226
2227 /* Convert a compcode bit-based encoding of a comparison operator back
2228    to GCC's enum tree_code representation.  This function is the
2229    inverse of comparison_to_compcode.  */
2230
2231 static enum tree_code
2232 compcode_to_comparison (enum comparison_code code)
2233 {
2234   switch (code)
2235     {
2236     case COMPCODE_LT:
2237       return LT_EXPR;
2238     case COMPCODE_EQ:
2239       return EQ_EXPR;
2240     case COMPCODE_LE:
2241       return LE_EXPR;
2242     case COMPCODE_GT:
2243       return GT_EXPR;
2244     case COMPCODE_NE:
2245       return NE_EXPR;
2246     case COMPCODE_GE:
2247       return GE_EXPR;
2248     case COMPCODE_ORD:
2249       return ORDERED_EXPR;
2250     case COMPCODE_UNORD:
2251       return UNORDERED_EXPR;
2252     case COMPCODE_UNLT:
2253       return UNLT_EXPR;
2254     case COMPCODE_UNEQ:
2255       return UNEQ_EXPR;
2256     case COMPCODE_UNLE:
2257       return UNLE_EXPR;
2258     case COMPCODE_UNGT:
2259       return UNGT_EXPR;
2260     case COMPCODE_LTGT:
2261       return LTGT_EXPR;
2262     case COMPCODE_UNGE:
2263       return UNGE_EXPR;
2264     default:
2265       gcc_unreachable ();
2266     }
2267 }
2268
2269 /* Return a tree for the comparison which is the combination of
2270    doing the AND or OR (depending on CODE) of the two operations LCODE
2271    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2272    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2273    if this makes the transformation invalid.  */
2274
2275 tree
2276 combine_comparisons (location_t loc,
2277                      enum tree_code code, enum tree_code lcode,
2278                      enum tree_code rcode, tree truth_type,
2279                      tree ll_arg, tree lr_arg)
2280 {
2281   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2282   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2283   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2284   int compcode;
2285
2286   switch (code)
2287     {
2288     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2289       compcode = lcompcode & rcompcode;
2290       break;
2291
2292     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2293       compcode = lcompcode | rcompcode;
2294       break;
2295
2296     default:
2297       return NULL_TREE;
2298     }
2299
2300   if (!honor_nans)
2301     {
2302       /* Eliminate unordered comparisons, as well as LTGT and ORD
2303          which are not used unless the mode has NaNs.  */
2304       compcode &= ~COMPCODE_UNORD;
2305       if (compcode == COMPCODE_LTGT)
2306         compcode = COMPCODE_NE;
2307       else if (compcode == COMPCODE_ORD)
2308         compcode = COMPCODE_TRUE;
2309     }
2310    else if (flag_trapping_math)
2311      {
2312         /* Check that the original operation and the optimized ones will trap
2313            under the same condition.  */
2314         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2315                      && (lcompcode != COMPCODE_EQ)
2316                      && (lcompcode != COMPCODE_ORD);
2317         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2318                      && (rcompcode != COMPCODE_EQ)
2319                      && (rcompcode != COMPCODE_ORD);
2320         bool trap = (compcode & COMPCODE_UNORD) == 0
2321                     && (compcode != COMPCODE_EQ)
2322                     && (compcode != COMPCODE_ORD);
2323
2324         /* In a short-circuited boolean expression the LHS might be
2325            such that the RHS, if evaluated, will never trap.  For
2326            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2327            if neither x nor y is NaN.  (This is a mixed blessing: for
2328            example, the expression above will never trap, hence
2329            optimizing it to x < y would be invalid).  */
2330         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2331             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2332           rtrap = false;
2333
2334         /* If the comparison was short-circuited, and only the RHS
2335            trapped, we may now generate a spurious trap.  */
2336         if (rtrap && !ltrap
2337             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2338           return NULL_TREE;
2339
2340         /* If we changed the conditions that cause a trap, we lose.  */
2341         if ((ltrap || rtrap) != trap)
2342           return NULL_TREE;
2343       }
2344
2345   if (compcode == COMPCODE_TRUE)
2346     return constant_boolean_node (true, truth_type);
2347   else if (compcode == COMPCODE_FALSE)
2348     return constant_boolean_node (false, truth_type);
2349   else
2350     {
2351       enum tree_code tcode;
2352
2353       tcode = compcode_to_comparison ((enum comparison_code) compcode);
2354       return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2355     }
2356 }
2357 \f
2358 /* Return nonzero if two operands (typically of the same tree node)
2359    are necessarily equal.  If either argument has side-effects this
2360    function returns zero.  FLAGS modifies behavior as follows:
2361
2362    If OEP_ONLY_CONST is set, only return nonzero for constants.
2363    This function tests whether the operands are indistinguishable;
2364    it does not test whether they are equal using C's == operation.
2365    The distinction is important for IEEE floating point, because
2366    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2367    (2) two NaNs may be indistinguishable, but NaN!=NaN.
2368
2369    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2370    even though it may hold multiple values during a function.
2371    This is because a GCC tree node guarantees that nothing else is
2372    executed between the evaluation of its "operands" (which may often
2373    be evaluated in arbitrary order).  Hence if the operands themselves
2374    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2375    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
2376    unset means assuming isochronic (or instantaneous) tree equivalence.
2377    Unless comparing arbitrary expression trees, such as from different
2378    statements, this flag can usually be left unset.
2379
2380    If OEP_PURE_SAME is set, then pure functions with identical arguments
2381    are considered the same.  It is used when the caller has other ways
2382    to ensure that global memory is unchanged in between.  */
2383
2384 int
2385 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2386 {
2387   /* If either is ERROR_MARK, they aren't equal.  */
2388   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2389       || TREE_TYPE (arg0) == error_mark_node
2390       || TREE_TYPE (arg1) == error_mark_node)
2391     return 0;
2392
2393   /* Similar, if either does not have a type (like a released SSA name), 
2394      they aren't equal.  */
2395   if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2396     return 0;
2397
2398   /* Check equality of integer constants before bailing out due to
2399      precision differences.  */
2400   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2401     return tree_int_cst_equal (arg0, arg1);
2402
2403   /* If both types don't have the same signedness, then we can't consider
2404      them equal.  We must check this before the STRIP_NOPS calls
2405      because they may change the signedness of the arguments.  As pointers
2406      strictly don't have a signedness, require either two pointers or
2407      two non-pointers as well.  */
2408   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2409       || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2410     return 0;
2411
2412   /* We cannot consider pointers to different address space equal.  */
2413   if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2414       && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2415           != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2416     return 0;
2417
2418   /* If both types don't have the same precision, then it is not safe
2419      to strip NOPs.  */
2420   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
2421     return 0;
2422
2423   STRIP_NOPS (arg0);
2424   STRIP_NOPS (arg1);
2425
2426   /* In case both args are comparisons but with different comparison
2427      code, try to swap the comparison operands of one arg to produce
2428      a match and compare that variant.  */
2429   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2430       && COMPARISON_CLASS_P (arg0)
2431       && COMPARISON_CLASS_P (arg1))
2432     {
2433       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2434
2435       if (TREE_CODE (arg0) == swap_code)
2436         return operand_equal_p (TREE_OPERAND (arg0, 0),
2437                                 TREE_OPERAND (arg1, 1), flags)
2438                && operand_equal_p (TREE_OPERAND (arg0, 1),
2439                                    TREE_OPERAND (arg1, 0), flags);
2440     }
2441
2442   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2443       /* This is needed for conversions and for COMPONENT_REF.
2444          Might as well play it safe and always test this.  */
2445       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2446       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2447       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2448     return 0;
2449
2450   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2451      We don't care about side effects in that case because the SAVE_EXPR
2452      takes care of that for us. In all other cases, two expressions are
2453      equal if they have no side effects.  If we have two identical
2454      expressions with side effects that should be treated the same due
2455      to the only side effects being identical SAVE_EXPR's, that will
2456      be detected in the recursive calls below.
2457      If we are taking an invariant address of two identical objects
2458      they are necessarily equal as well.  */
2459   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2460       && (TREE_CODE (arg0) == SAVE_EXPR
2461           || (flags & OEP_CONSTANT_ADDRESS_OF)
2462           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2463     return 1;
2464
2465   /* Next handle constant cases, those for which we can return 1 even
2466      if ONLY_CONST is set.  */
2467   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2468     switch (TREE_CODE (arg0))
2469       {
2470       case INTEGER_CST:
2471         return tree_int_cst_equal (arg0, arg1);
2472
2473       case FIXED_CST:
2474         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2475                                        TREE_FIXED_CST (arg1));
2476
2477       case REAL_CST:
2478         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2479                                    TREE_REAL_CST (arg1)))
2480           return 1;
2481
2482
2483         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
2484           {
2485             /* If we do not distinguish between signed and unsigned zero,
2486                consider them equal.  */
2487             if (real_zerop (arg0) && real_zerop (arg1))
2488               return 1;
2489           }
2490         return 0;
2491
2492       case VECTOR_CST:
2493         {
2494           tree v1, v2;
2495
2496           v1 = TREE_VECTOR_CST_ELTS (arg0);
2497           v2 = TREE_VECTOR_CST_ELTS (arg1);
2498           while (v1 && v2)
2499             {
2500               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2501                                     flags))
2502                 return 0;
2503               v1 = TREE_CHAIN (v1);
2504               v2 = TREE_CHAIN (v2);
2505             }
2506
2507           return v1 == v2;
2508         }
2509
2510       case COMPLEX_CST:
2511         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2512                                  flags)
2513                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2514                                     flags));
2515
2516       case STRING_CST:
2517         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2518                 && ! memcmp (TREE_STRING_POINTER (arg0),
2519                               TREE_STRING_POINTER (arg1),
2520                               TREE_STRING_LENGTH (arg0)));
2521
2522       case ADDR_EXPR:
2523         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2524                                 TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1)
2525                                 ? OEP_CONSTANT_ADDRESS_OF : 0);
2526       default:
2527         break;
2528       }
2529
2530   if (flags & OEP_ONLY_CONST)
2531     return 0;
2532
2533 /* Define macros to test an operand from arg0 and arg1 for equality and a
2534    variant that allows null and views null as being different from any
2535    non-null value.  In the latter case, if either is null, the both
2536    must be; otherwise, do the normal comparison.  */
2537 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
2538                                     TREE_OPERAND (arg1, N), flags)
2539
2540 #define OP_SAME_WITH_NULL(N)                            \
2541   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2542    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2543
2544   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2545     {
2546     case tcc_unary:
2547       /* Two conversions are equal only if signedness and modes match.  */
2548       switch (TREE_CODE (arg0))
2549         {
2550         CASE_CONVERT:
2551         case FIX_TRUNC_EXPR:
2552           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2553               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2554             return 0;
2555           break;
2556         default:
2557           break;
2558         }
2559
2560       return OP_SAME (0);
2561
2562
2563     case tcc_comparison:
2564     case tcc_binary:
2565       if (OP_SAME (0) && OP_SAME (1))
2566         return 1;
2567
2568       /* For commutative ops, allow the other order.  */
2569       return (commutative_tree_code (TREE_CODE (arg0))
2570               && operand_equal_p (TREE_OPERAND (arg0, 0),
2571                                   TREE_OPERAND (arg1, 1), flags)
2572               && operand_equal_p (TREE_OPERAND (arg0, 1),
2573                                   TREE_OPERAND (arg1, 0), flags));
2574
2575     case tcc_reference:
2576       /* If either of the pointer (or reference) expressions we are
2577          dereferencing contain a side effect, these cannot be equal.  */
2578       if (TREE_SIDE_EFFECTS (arg0)
2579           || TREE_SIDE_EFFECTS (arg1))
2580         return 0;
2581
2582       switch (TREE_CODE (arg0))
2583         {
2584         case INDIRECT_REF:
2585         case REALPART_EXPR:
2586         case IMAGPART_EXPR:
2587           return OP_SAME (0);
2588
2589         case MEM_REF:
2590           /* Require equal access sizes, and similar pointer types.
2591              We can have incomplete types for array references of
2592              variable-sized arrays from the Fortran frontent
2593              though.  */
2594           return ((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1))
2595                    || (TYPE_SIZE (TREE_TYPE (arg0))
2596                        && TYPE_SIZE (TREE_TYPE (arg1))
2597                        && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
2598                                            TYPE_SIZE (TREE_TYPE (arg1)), flags)))
2599                   && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg0, 1)))
2600                       == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg1, 1))))
2601                   && OP_SAME (0) && OP_SAME (1));
2602
2603         case ARRAY_REF:
2604         case ARRAY_RANGE_REF:
2605           /* Operands 2 and 3 may be null.
2606              Compare the array index by value if it is constant first as we
2607              may have different types but same value here.  */
2608           return (OP_SAME (0)
2609                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2610                                           TREE_OPERAND (arg1, 1))
2611                       || OP_SAME (1))
2612                   && OP_SAME_WITH_NULL (2)
2613                   && OP_SAME_WITH_NULL (3));
2614
2615         case COMPONENT_REF:
2616           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
2617              may be NULL when we're called to compare MEM_EXPRs.  */
2618           return OP_SAME_WITH_NULL (0)
2619                  && OP_SAME (1)
2620                  && OP_SAME_WITH_NULL (2);
2621
2622         case BIT_FIELD_REF:
2623           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2624
2625         default:
2626           return 0;
2627         }
2628
2629     case tcc_expression:
2630       switch (TREE_CODE (arg0))
2631         {
2632         case ADDR_EXPR:
2633         case TRUTH_NOT_EXPR:
2634           return OP_SAME (0);
2635
2636         case TRUTH_ANDIF_EXPR:
2637         case TRUTH_ORIF_EXPR:
2638           return OP_SAME (0) && OP_SAME (1);
2639
2640         case FMA_EXPR:
2641         case WIDEN_MULT_PLUS_EXPR:
2642         case WIDEN_MULT_MINUS_EXPR:
2643           if (!OP_SAME (2))
2644             return 0;
2645           /* The multiplcation operands are commutative.  */
2646           /* FALLTHRU */
2647
2648         case TRUTH_AND_EXPR:
2649         case TRUTH_OR_EXPR:
2650         case TRUTH_XOR_EXPR:
2651           if (OP_SAME (0) && OP_SAME (1))
2652             return 1;
2653
2654           /* Otherwise take into account this is a commutative operation.  */
2655           return (operand_equal_p (TREE_OPERAND (arg0, 0),
2656                                    TREE_OPERAND (arg1, 1), flags)
2657                   && operand_equal_p (TREE_OPERAND (arg0, 1),
2658                                       TREE_OPERAND (arg1, 0), flags));
2659
2660         case COND_EXPR:
2661         case VEC_COND_EXPR:
2662         case DOT_PROD_EXPR:
2663           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2664
2665         default:
2666           return 0;
2667         }
2668
2669     case tcc_vl_exp:
2670       switch (TREE_CODE (arg0))
2671         {
2672         case CALL_EXPR:
2673           /* If the CALL_EXPRs call different functions, then they
2674              clearly can not be equal.  */
2675           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
2676                                  flags))
2677             return 0;
2678
2679           {
2680             unsigned int cef = call_expr_flags (arg0);
2681             if (flags & OEP_PURE_SAME)
2682               cef &= ECF_CONST | ECF_PURE;
2683             else
2684               cef &= ECF_CONST;
2685             if (!cef)
2686               return 0;
2687           }
2688
2689           /* Now see if all the arguments are the same.  */
2690           {
2691             const_call_expr_arg_iterator iter0, iter1;
2692             const_tree a0, a1;
2693             for (a0 = first_const_call_expr_arg (arg0, &iter0),
2694                    a1 = first_const_call_expr_arg (arg1, &iter1);
2695                  a0 && a1;
2696                  a0 = next_const_call_expr_arg (&iter0),
2697                    a1 = next_const_call_expr_arg (&iter1))
2698               if (! operand_equal_p (a0, a1, flags))
2699                 return 0;
2700
2701             /* If we get here and both argument lists are exhausted
2702                then the CALL_EXPRs are equal.  */
2703             return ! (a0 || a1);
2704           }
2705         default:
2706           return 0;
2707         }
2708
2709     case tcc_declaration:
2710       /* Consider __builtin_sqrt equal to sqrt.  */
2711       return (TREE_CODE (arg0) == FUNCTION_DECL
2712               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2713               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2714               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2715
2716     default:
2717       return 0;
2718     }
2719
2720 #undef OP_SAME
2721 #undef OP_SAME_WITH_NULL
2722 }
2723 \f
2724 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2725    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2726
2727    When in doubt, return 0.  */
2728
2729 static int
2730 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2731 {
2732   int unsignedp1, unsignedpo;
2733   tree primarg0, primarg1, primother;
2734   unsigned int correct_width;
2735
2736   if (operand_equal_p (arg0, arg1, 0))
2737     return 1;
2738
2739   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2740       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2741     return 0;
2742
2743   /* Discard any conversions that don't change the modes of ARG0 and ARG1
2744      and see if the inner values are the same.  This removes any
2745      signedness comparison, which doesn't matter here.  */
2746   primarg0 = arg0, primarg1 = arg1;
2747   STRIP_NOPS (primarg0);
2748   STRIP_NOPS (primarg1);
2749   if (operand_equal_p (primarg0, primarg1, 0))
2750     return 1;
2751
2752   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2753      actual comparison operand, ARG0.
2754
2755      First throw away any conversions to wider types
2756      already present in the operands.  */
2757
2758   primarg1 = get_narrower (arg1, &unsignedp1);
2759   primother = get_narrower (other, &unsignedpo);
2760
2761   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2762   if (unsignedp1 == unsignedpo
2763       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2764       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2765     {
2766       tree type = TREE_TYPE (arg0);
2767
2768       /* Make sure shorter operand is extended the right way
2769          to match the longer operand.  */
2770       primarg1 = fold_convert (signed_or_unsigned_type_for
2771                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2772
2773       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2774         return 1;
2775     }
2776
2777   return 0;
2778 }
2779 \f
2780 /* See if ARG is an expression that is either a comparison or is performing
2781    arithmetic on comparisons.  The comparisons must only be comparing
2782    two different values, which will be stored in *CVAL1 and *CVAL2; if
2783    they are nonzero it means that some operands have already been found.
2784    No variables may be used anywhere else in the expression except in the
2785    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
2786    the expression and save_expr needs to be called with CVAL1 and CVAL2.
2787
2788    If this is true, return 1.  Otherwise, return zero.  */
2789
2790 static int
2791 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2792 {
2793   enum tree_code code = TREE_CODE (arg);
2794   enum tree_code_class tclass = TREE_CODE_CLASS (code);
2795
2796   /* We can handle some of the tcc_expression cases here.  */
2797   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2798     tclass = tcc_unary;
2799   else if (tclass == tcc_expression
2800            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2801                || code == COMPOUND_EXPR))
2802     tclass = tcc_binary;
2803
2804   else if (tclass == tcc_expression && code == SAVE_EXPR
2805            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2806     {
2807       /* If we've already found a CVAL1 or CVAL2, this expression is
2808          two complex to handle.  */
2809       if (*cval1 || *cval2)
2810         return 0;
2811
2812       tclass = tcc_unary;
2813       *save_p = 1;
2814     }
2815
2816   switch (tclass)
2817     {
2818     case tcc_unary:
2819       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2820
2821     case tcc_binary:
2822       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2823               && twoval_comparison_p (TREE_OPERAND (arg, 1),
2824                                       cval1, cval2, save_p));
2825
2826     case tcc_constant:
2827       return 1;
2828
2829     case tcc_expression:
2830       if (code == COND_EXPR)
2831         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2832                                      cval1, cval2, save_p)
2833                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2834                                         cval1, cval2, save_p)
2835                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2836                                         cval1, cval2, save_p));
2837       return 0;
2838
2839     case tcc_comparison:
2840       /* First see if we can handle the first operand, then the second.  For
2841          the second operand, we know *CVAL1 can't be zero.  It must be that
2842          one side of the comparison is each of the values; test for the
2843          case where this isn't true by failing if the two operands
2844          are the same.  */
2845
2846       if (operand_equal_p (TREE_OPERAND (arg, 0),
2847                            TREE_OPERAND (arg, 1), 0))
2848         return 0;
2849
2850       if (*cval1 == 0)
2851         *cval1 = TREE_OPERAND (arg, 0);
2852       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2853         ;
2854       else if (*cval2 == 0)
2855         *cval2 = TREE_OPERAND (arg, 0);
2856       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2857         ;
2858       else
2859         return 0;
2860
2861       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2862         ;
2863       else if (*cval2 == 0)
2864         *cval2 = TREE_OPERAND (arg, 1);
2865       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2866         ;
2867       else
2868         return 0;
2869
2870       return 1;
2871
2872     default:
2873       return 0;
2874     }
2875 }
2876 \f
2877 /* ARG is a tree that is known to contain just arithmetic operations and
2878    comparisons.  Evaluate the operations in the tree substituting NEW0 for
2879    any occurrence of OLD0 as an operand of a comparison and likewise for
2880    NEW1 and OLD1.  */
2881
2882 static tree
2883 eval_subst (location_t loc, tree arg, tree old0, tree new0,
2884             tree old1, tree new1)
2885 {
2886   tree type = TREE_TYPE (arg);
2887   enum tree_code code = TREE_CODE (arg);
2888   enum tree_code_class tclass = TREE_CODE_CLASS (code);
2889
2890   /* We can handle some of the tcc_expression cases here.  */
2891   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2892     tclass = tcc_unary;
2893   else if (tclass == tcc_expression
2894            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2895     tclass = tcc_binary;
2896
2897   switch (tclass)
2898     {
2899     case tcc_unary:
2900       return fold_build1_loc (loc, code, type,
2901                           eval_subst (loc, TREE_OPERAND (arg, 0),
2902                                       old0, new0, old1, new1));
2903
2904     case tcc_binary:
2905       return fold_build2_loc (loc, code, type,
2906                           eval_subst (loc, TREE_OPERAND (arg, 0),
2907                                       old0, new0, old1, new1),
2908                           eval_subst (loc, TREE_OPERAND (arg, 1),
2909                                       old0, new0, old1, new1));
2910
2911     case tcc_expression:
2912       switch (code)
2913         {
2914         case SAVE_EXPR:
2915           return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
2916                              old1, new1);
2917
2918         case COMPOUND_EXPR:
2919           return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
2920                              old1, new1);
2921
2922         case COND_EXPR:
2923           return fold_build3_loc (loc, code, type,
2924                               eval_subst (loc, TREE_OPERAND (arg, 0),
2925                                           old0, new0, old1, new1),
2926                               eval_subst (loc, TREE_OPERAND (arg, 1),
2927                                           old0, new0, old1, new1),
2928                               eval_subst (loc, TREE_OPERAND (arg, 2),
2929                                           old0, new0, old1, new1));
2930         default:
2931           break;
2932         }
2933       /* Fall through - ???  */
2934
2935     case tcc_comparison:
2936       {
2937         tree arg0 = TREE_OPERAND (arg, 0);
2938         tree arg1 = TREE_OPERAND (arg, 1);
2939
2940         /* We need to check both for exact equality and tree equality.  The
2941            former will be true if the operand has a side-effect.  In that
2942            case, we know the operand occurred exactly once.  */
2943
2944         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2945           arg0 = new0;
2946         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2947           arg0 = new1;
2948
2949         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2950           arg1 = new0;
2951         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2952           arg1 = new1;
2953
2954         return fold_build2_loc (loc, code, type, arg0, arg1);
2955       }
2956
2957     default:
2958       return arg;
2959     }
2960 }
2961 \f
2962 /* Return a tree for the case when the result of an expression is RESULT
2963    converted to TYPE and OMITTED was previously an operand of the expression
2964    but is now not needed (e.g., we folded OMITTED * 0).
2965
2966    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
2967    the conversion of RESULT to TYPE.  */
2968
2969 tree
2970 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
2971 {
2972   tree t = fold_convert_loc (loc, type, result);
2973
2974   /* If the resulting operand is an empty statement, just return the omitted
2975      statement casted to void. */
2976   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
2977     return build1_loc (loc, NOP_EXPR, void_type_node,
2978                        fold_ignored_result (omitted));
2979
2980   if (TREE_SIDE_EFFECTS (omitted))
2981     return build2_loc (loc, COMPOUND_EXPR, type,
2982                        fold_ignored_result (omitted), t);
2983
2984   return non_lvalue_loc (loc, t);
2985 }
2986
2987 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
2988
2989 static tree
2990 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
2991                                tree omitted)
2992 {
2993   tree t = fold_convert_loc (loc, type, result);
2994
2995   /* If the resulting operand is an empty statement, just return the omitted
2996      statement casted to void. */
2997   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
2998     return build1_loc (loc, NOP_EXPR, void_type_node,
2999                        fold_ignored_result (omitted));
3000
3001   if (TREE_SIDE_EFFECTS (omitted))
3002     return build2_loc (loc, COMPOUND_EXPR, type,
3003                        fold_ignored_result (omitted), t);
3004
3005   return pedantic_non_lvalue_loc (loc, t);
3006 }
3007
3008 /* Return a tree for the case when the result of an expression is RESULT
3009    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3010    of the expression but are now not needed.
3011
3012    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3013    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3014    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3015    just do the conversion of RESULT to TYPE.  */
3016
3017 tree
3018 omit_two_operands_loc (location_t loc, tree type, tree result,
3019                        tree omitted1, tree omitted2)
3020 {
3021   tree t = fold_convert_loc (loc, type, result);
3022
3023   if (TREE_SIDE_EFFECTS (omitted2))
3024     t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3025   if (TREE_SIDE_EFFECTS (omitted1))
3026     t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3027
3028   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3029 }
3030
3031 \f
3032 /* Return a simplified tree node for the truth-negation of ARG.  This
3033    never alters ARG itself.  We assume that ARG is an operation that
3034    returns a truth value (0 or 1).
3035
3036    FIXME: one would think we would fold the result, but it causes
3037    problems with the dominator optimizer.  */
3038
3039 tree
3040 fold_truth_not_expr (location_t loc, tree arg)
3041 {
3042   tree type = TREE_TYPE (arg);
3043   enum tree_code code = TREE_CODE (arg);
3044   location_t loc1, loc2;
3045
3046   /* If this is a comparison, we can simply invert it, except for
3047      floating-point non-equality comparisons, in which case we just
3048      enclose a TRUTH_NOT_EXPR around what we have.  */
3049
3050   if (TREE_CODE_CLASS (code) == tcc_comparison)
3051     {
3052       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3053       if (FLOAT_TYPE_P (op_type)
3054           && flag_trapping_math
3055           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3056           && code != NE_EXPR && code != EQ_EXPR)
3057         return NULL_TREE;
3058
3059       code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3060       if (code == ERROR_MARK)
3061         return NULL_TREE;
3062
3063       return build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3064                          TREE_OPERAND (arg, 1));
3065     }
3066
3067   switch (code)
3068     {
3069     case INTEGER_CST:
3070       return constant_boolean_node (integer_zerop (arg), type);
3071
3072     case TRUTH_AND_EXPR:
3073       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3074       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3075       return build2_loc (loc, TRUTH_OR_EXPR, type,
3076                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3077                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3078
3079     case TRUTH_OR_EXPR:
3080       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3081       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3082       return build2_loc (loc, TRUTH_AND_EXPR, type,
3083                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3084                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3085
3086     case TRUTH_XOR_EXPR:
3087       /* Here we can invert either operand.  We invert the first operand
3088          unless the second operand is a TRUTH_NOT_EXPR in which case our
3089          result is the XOR of the first operand with the inside of the
3090          negation of the second operand.  */
3091
3092       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3093         return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3094                            TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3095       else
3096         return build2_loc (loc, TRUTH_XOR_EXPR, type,
3097                            invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3098                            TREE_OPERAND (arg, 1));
3099
3100     case TRUTH_ANDIF_EXPR:
3101       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3102       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3103       return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3104                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3105                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3106
3107     case TRUTH_ORIF_EXPR:
3108       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3109       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3110       return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3111                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3112                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3113
3114     case TRUTH_NOT_EXPR:
3115       return TREE_OPERAND (arg, 0);
3116
3117     case COND_EXPR:
3118       {
3119         tree arg1 = TREE_OPERAND (arg, 1);
3120         tree arg2 = TREE_OPERAND (arg, 2);
3121
3122         loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3123         loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3124
3125         /* A COND_EXPR may have a throw as one operand, which
3126            then has void type.  Just leave void operands
3127            as they are.  */
3128         return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3129                            VOID_TYPE_P (TREE_TYPE (arg1))
3130                            ? arg1 : invert_truthvalue_loc (loc1, arg1),
3131                            VOID_TYPE_P (TREE_TYPE (arg2))
3132                            ? arg2 : invert_truthvalue_loc (loc2, arg2));
3133       }
3134
3135     case COMPOUND_EXPR:
3136       loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3137       return build2_loc (loc, COMPOUND_EXPR, type,
3138                          TREE_OPERAND (arg, 0),
3139                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3140
3141     case NON_LVALUE_EXPR:
3142       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3143       return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3144
3145     CASE_CONVERT:
3146       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3147         return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3148
3149       /* ... fall through ...  */
3150
3151     case FLOAT_EXPR:
3152       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3153       return build1_loc (loc, TREE_CODE (arg), type,
3154                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3155
3156     case BIT_AND_EXPR:
3157       if (!integer_onep (TREE_OPERAND (arg, 1)))
3158         return NULL_TREE;
3159       return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3160
3161     case SAVE_EXPR:
3162       return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3163
3164     case CLEANUP_POINT_EXPR:
3165       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3166       return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3167                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3168
3169     default:
3170       return NULL_TREE;
3171     }
3172 }
3173
3174 /* Return a simplified tree node for the truth-negation of ARG.  This
3175    never alters ARG itself.  We assume that ARG is an operation that
3176    returns a truth value (0 or 1).
3177
3178    FIXME: one would think we would fold the result, but it causes
3179    problems with the dominator optimizer.  */
3180
3181 tree
3182 invert_truthvalue_loc (location_t loc, tree arg)
3183 {
3184   tree tem;
3185
3186   if (TREE_CODE (arg) == ERROR_MARK)
3187     return arg;
3188
3189   tem = fold_truth_not_expr (loc, arg);
3190   if (!tem)
3191     tem = build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3192
3193   return tem;
3194 }
3195
3196 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3197    operands are another bit-wise operation with a common input.  If so,
3198    distribute the bit operations to save an operation and possibly two if
3199    constants are involved.  For example, convert
3200         (A | B) & (A | C) into A | (B & C)
3201    Further simplification will occur if B and C are constants.
3202
3203    If this optimization cannot be done, 0 will be returned.  */
3204
3205 static tree
3206 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
3207                      tree arg0, tree arg1)
3208 {
3209   tree common;
3210   tree left, right;
3211
3212   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3213       || TREE_CODE (arg0) == code
3214       || (TREE_CODE (arg0) != BIT_AND_EXPR
3215           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3216     return 0;
3217
3218   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3219     {
3220       common = TREE_OPERAND (arg0, 0);
3221       left = TREE_OPERAND (arg0, 1);
3222       right = TREE_OPERAND (arg1, 1);
3223     }
3224   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3225     {
3226       common = TREE_OPERAND (arg0, 0);
3227       left = TREE_OPERAND (arg0, 1);
3228       right = TREE_OPERAND (arg1, 0);
3229     }
3230   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3231     {
3232       common = TREE_OPERAND (arg0, 1);
3233       left = TREE_OPERAND (arg0, 0);
3234       right = TREE_OPERAND (arg1, 1);
3235     }
3236   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3237     {
3238       common = TREE_OPERAND (arg0, 1);
3239       left = TREE_OPERAND (arg0, 0);
3240       right = TREE_OPERAND (arg1, 0);
3241     }
3242   else
3243     return 0;
3244
3245   common = fold_convert_loc (loc, type, common);
3246   left = fold_convert_loc (loc, type, left);
3247   right = fold_convert_loc (loc, type, right);
3248   return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
3249                       fold_build2_loc (loc, code, type, left, right));
3250 }
3251
3252 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3253    with code CODE.  This optimization is unsafe.  */
3254 static tree
3255 distribute_real_division (location_t loc, enum tree_code code, tree type,
3256                           tree arg0, tree arg1)
3257 {
3258   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3259   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3260
3261   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3262   if (mul0 == mul1
3263       && operand_equal_p (TREE_OPERAND (arg0, 1),
3264                        TREE_OPERAND (arg1, 1), 0))
3265     return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3266                         fold_build2_loc (loc, code, type,
3267                                      TREE_OPERAND (arg0, 0),
3268                                      TREE_OPERAND (arg1, 0)),
3269                         TREE_OPERAND (arg0, 1));
3270
3271   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3272   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3273                        TREE_OPERAND (arg1, 0), 0)
3274       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3275       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3276     {
3277       REAL_VALUE_TYPE r0, r1;
3278       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3279       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3280       if (!mul0)
3281         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3282       if (!mul1)
3283         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3284       real_arithmetic (&r0, code, &r0, &r1);
3285       return fold_build2_loc (loc, MULT_EXPR, type,
3286                           TREE_OPERAND (arg0, 0),
3287                           build_real (type, r0));
3288     }
3289
3290   return NULL_TREE;
3291 }
3292 \f
3293 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3294    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3295
3296 static tree
3297 make_bit_field_ref (location_t loc, tree inner, tree type,
3298                     HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3299 {
3300   tree result, bftype;
3301
3302   if (bitpos == 0)
3303     {
3304       tree size = TYPE_SIZE (TREE_TYPE (inner));
3305       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3306            || POINTER_TYPE_P (TREE_TYPE (inner)))
3307           && host_integerp (size, 0)
3308           && tree_low_cst (size, 0) == bitsize)
3309         return fold_convert_loc (loc, type, inner);
3310     }
3311
3312   bftype = type;
3313   if (TYPE_PRECISION (bftype) != bitsize
3314       || TYPE_UNSIGNED (bftype) == !unsignedp)
3315     bftype = build_nonstandard_integer_type (bitsize, 0);
3316
3317   result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3318                        size_int (bitsize), bitsize_int (bitpos));
3319
3320   if (bftype != type)
3321     result = fold_convert_loc (loc, type, result);
3322
3323   return result;
3324 }
3325
3326 /* Optimize a bit-field compare.
3327
3328    There are two cases:  First is a compare against a constant and the
3329    second is a comparison of two items where the fields are at the same
3330    bit position relative to the start of a chunk (byte, halfword, word)
3331    large enough to contain it.  In these cases we can avoid the shift
3332    implicit in bitfield extractions.
3333
3334    For constants, we emit a compare of the shifted constant with the
3335    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3336    compared.  For two fields at the same position, we do the ANDs with the
3337    similar mask and compare the result of the ANDs.
3338
3339    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3340    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3341    are the left and right operands of the comparison, respectively.
3342
3343    If the optimization described above can be done, we return the resulting
3344    tree.  Otherwise we return zero.  */
3345
3346 static tree
3347 optimize_bit_field_compare (location_t loc, enum tree_code code,
3348                             tree compare_type, tree lhs, tree rhs)
3349 {
3350   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3351   tree type = TREE_TYPE (lhs);
3352   tree signed_type, unsigned_type;
3353   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3354   enum machine_mode lmode, rmode, nmode;
3355   int lunsignedp, runsignedp;
3356   int lvolatilep = 0, rvolatilep = 0;
3357   tree linner, rinner = NULL_TREE;
3358   tree mask;
3359   tree offset;
3360
3361   /* Get all the information about the extractions being done.  If the bit size
3362      if the same as the size of the underlying object, we aren't doing an
3363      extraction at all and so can do nothing.  We also don't want to
3364      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3365      then will no longer be able to replace it.  */
3366   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3367                                 &lunsignedp, &lvolatilep, false);
3368   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3369       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3370     return 0;
3371
3372  if (!const_p)
3373    {
3374      /* If this is not a constant, we can only do something if bit positions,
3375         sizes, and signedness are the same.  */
3376      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3377                                    &runsignedp, &rvolatilep, false);
3378
3379      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3380          || lunsignedp != runsignedp || offset != 0
3381          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3382        return 0;
3383    }
3384
3385   /* See if we can find a mode to refer to this field.  We should be able to,
3386      but fail if we can't.  */
3387   if (lvolatilep
3388       && GET_MODE_BITSIZE (lmode) > 0
3389       && flag_strict_volatile_bitfields > 0)
3390     nmode = lmode;
3391   else
3392     nmode = get_best_mode (lbitsize, lbitpos, 0, 0,
3393                            const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3394                            : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3395                                   TYPE_ALIGN (TREE_TYPE (rinner))),
3396                            word_mode, lvolatilep || rvolatilep);
3397   if (nmode == VOIDmode)
3398     return 0;
3399
3400   /* Set signed and unsigned types of the precision of this mode for the
3401      shifts below.  */
3402   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3403   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3404
3405   /* Compute the bit position and size for the new reference and our offset
3406      within it. If the new reference is the same size as the original, we
3407      won't optimize anything, so return zero.  */
3408   nbitsize = GET_MODE_BITSIZE (nmode);
3409   nbitpos = lbitpos & ~ (nbitsize - 1);
3410   lbitpos -= nbitpos;
3411   if (nbitsize == lbitsize)
3412     return 0;
3413
3414   if (BYTES_BIG_ENDIAN)
3415     lbitpos = nbitsize - lbitsize - lbitpos;
3416
3417   /* Make the mask to be used against the extracted field.  */
3418   mask = build_int_cst_type (unsigned_type, -1);
3419   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3420   mask = const_binop (RSHIFT_EXPR, mask,
3421                       size_int (nbitsize - lbitsize - lbitpos));
3422
3423   if (! const_p)
3424     /* If not comparing with constant, just rework the comparison
3425        and return.  */
3426     return fold_build2_loc (loc, code, compare_type,
3427                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3428                                      make_bit_field_ref (loc, linner,
3429                                                          unsigned_type,
3430                                                          nbitsize, nbitpos,
3431                                                          1),
3432                                      mask),
3433                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3434                                      make_bit_field_ref (loc, rinner,
3435                                                          unsigned_type,
3436                                                          nbitsize, nbitpos,
3437                                                          1),
3438                                      mask));
3439
3440   /* Otherwise, we are handling the constant case. See if the constant is too
3441      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
3442      this not only for its own sake, but to avoid having to test for this
3443      error case below.  If we didn't, we might generate wrong code.
3444
3445      For unsigned fields, the constant shifted right by the field length should
3446      be all zero.  For signed fields, the high-order bits should agree with
3447      the sign bit.  */
3448
3449   if (lunsignedp)
3450     {
3451       if (! integer_zerop (const_binop (RSHIFT_EXPR,
3452                                         fold_convert_loc (loc,
3453                                                           unsigned_type, rhs),
3454                                         size_int (lbitsize))))
3455         {
3456           warning (0, "comparison is always %d due to width of bit-field",
3457                    code == NE_EXPR);
3458           return constant_boolean_node (code == NE_EXPR, compare_type);
3459         }
3460     }
3461   else
3462     {
3463       tree tem = const_binop (RSHIFT_EXPR,
3464                               fold_convert_loc (loc, signed_type, rhs),
3465                               size_int (lbitsize - 1));
3466       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3467         {
3468           warning (0, "comparison is always %d due to width of bit-field",
3469                    code == NE_EXPR);
3470           return constant_boolean_node (code == NE_EXPR, compare_type);
3471         }
3472     }
3473
3474   /* Single-bit compares should always be against zero.  */
3475   if (lbitsize == 1 && ! integer_zerop (rhs))
3476     {
3477       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3478       rhs = build_int_cst (type, 0);
3479     }
3480
3481   /* Make a new bitfield reference, shift the constant over the
3482      appropriate number of bits and mask it with the computed mask
3483      (in case this was a signed field).  If we changed it, make a new one.  */
3484   lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3485   if (lvolatilep)
3486     {
3487       TREE_SIDE_EFFECTS (lhs) = 1;
3488       TREE_THIS_VOLATILE (lhs) = 1;
3489     }
3490
3491   rhs = const_binop (BIT_AND_EXPR,
3492                      const_binop (LSHIFT_EXPR,
3493                                   fold_convert_loc (loc, unsigned_type, rhs),
3494                                   size_int (lbitpos)),
3495                      mask);
3496
3497   lhs = build2_loc (loc, code, compare_type,
3498                     build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
3499   return lhs;
3500 }
3501 \f
3502 /* Subroutine for fold_truth_andor_1: decode a field reference.
3503
3504    If EXP is a comparison reference, we return the innermost reference.
3505
3506    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3507    set to the starting bit number.
3508
3509    If the innermost field can be completely contained in a mode-sized
3510    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
3511
3512    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3513    otherwise it is not changed.
3514
3515    *PUNSIGNEDP is set to the signedness of the field.
3516
3517    *PMASK is set to the mask used.  This is either contained in a
3518    BIT_AND_EXPR or derived from the width of the field.
3519
3520    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3521
3522    Return 0 if this is not a component reference or is one that we can't
3523    do anything with.  */
3524
3525 static tree
3526 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3527                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3528                         int *punsignedp, int *pvolatilep,
3529                         tree *pmask, tree *pand_mask)
3530 {
3531   tree outer_type = 0;
3532   tree and_mask = 0;
3533   tree mask, inner, offset;
3534   tree unsigned_type;
3535   unsigned int precision;
3536
3537   /* All the optimizations using this function assume integer fields.
3538      There are problems with FP fields since the type_for_size call
3539      below can fail for, e.g., XFmode.  */
3540   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3541     return 0;
3542
3543   /* We are interested in the bare arrangement of bits, so strip everything
3544      that doesn't affect the machine mode.  However, record the type of the
3545      outermost expression if it may matter below.  */
3546   if (CONVERT_EXPR_P (exp)
3547       || TREE_CODE (exp) == NON_LVALUE_EXPR)
3548     outer_type = TREE_TYPE (exp);
3549   STRIP_NOPS (exp);
3550
3551   if (TREE_CODE (exp) == BIT_AND_EXPR)
3552     {
3553       and_mask = TREE_OPERAND (exp, 1);
3554       exp = TREE_OPERAND (exp, 0);
3555       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3556       if (TREE_CODE (and_mask) != INTEGER_CST)
3557         return 0;
3558     }
3559
3560   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3561                                punsignedp, pvolatilep, false);
3562   if ((inner == exp && and_mask == 0)
3563       || *pbitsize < 0 || offset != 0
3564       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3565     return 0;
3566
3567   /* If the number of bits in the reference is the same as the bitsize of
3568      the outer type, then the outer type gives the signedness. Otherwise
3569      (in case of a small bitfield) the signedness is unchanged.  */
3570   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3571     *punsignedp = TYPE_UNSIGNED (outer_type);
3572
3573   /* Compute the mask to access the bitfield.  */
3574   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3575   precision = TYPE_PRECISION (unsigned_type);
3576
3577   mask = build_int_cst_type (unsigned_type, -1);
3578
3579   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3580   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3581
3582   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
3583   if (and_mask != 0)
3584     mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3585                         fold_convert_loc (loc, unsigned_type, and_mask), mask);
3586
3587   *pmask = mask;
3588   *pand_mask = and_mask;
3589   return inner;
3590 }
3591
3592 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3593    bit positions.  */
3594
3595 static int
3596 all_ones_mask_p (const_tree mask, int size)
3597 {
3598   tree type = TREE_TYPE (mask);
3599   unsigned int precision = TYPE_PRECISION (type);
3600   tree tmask;
3601
3602   tmask = build_int_cst_type (signed_type_for (type), -1);
3603
3604   return
3605     tree_int_cst_equal (mask,
3606                         const_binop (RSHIFT_EXPR,
3607                                      const_binop (LSHIFT_EXPR, tmask,
3608                                                   size_int (precision - size)),
3609                                      size_int (precision - size)));
3610 }
3611
3612 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3613    represents the sign bit of EXP's type.  If EXP represents a sign
3614    or zero extension, also test VAL against the unextended type.
3615    The return value is the (sub)expression whose sign bit is VAL,
3616    or NULL_TREE otherwise.  */
3617
3618 static tree
3619 sign_bit_p (tree exp, const_tree val)
3620 {
3621   unsigned HOST_WIDE_INT mask_lo, lo;
3622   HOST_WIDE_INT mask_hi, hi;
3623   int width;
3624   tree t;
3625
3626   /* Tree EXP must have an integral type.  */
3627   t = TREE_TYPE (exp);
3628   if (! INTEGRAL_TYPE_P (t))
3629     return NULL_TREE;
3630
3631   /* Tree VAL must be an integer constant.  */
3632   if (TREE_CODE (val) != INTEGER_CST
3633       || TREE_OVERFLOW (val))
3634     return NULL_TREE;
3635
3636   width = TYPE_PRECISION (t);
3637   if (width > HOST_BITS_PER_WIDE_INT)
3638     {
3639       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3640       lo = 0;
3641
3642       mask_hi = ((unsigned HOST_WIDE_INT) -1
3643                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
3644       mask_lo = -1;
3645     }
3646   else
3647     {
3648       hi = 0;
3649       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3650
3651       mask_hi = 0;
3652       mask_lo = ((unsigned HOST_WIDE_INT) -1
3653                  >> (HOST_BITS_PER_WIDE_INT - width));
3654     }
3655
3656   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3657      treat VAL as if it were unsigned.  */
3658   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3659       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3660     return exp;
3661
3662   /* Handle extension from a narrower type.  */
3663   if (TREE_CODE (exp) == NOP_EXPR
3664       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3665     return sign_bit_p (TREE_OPERAND (exp, 0), val);
3666
3667   return NULL_TREE;
3668 }
3669
3670 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
3671    to be evaluated unconditionally.  */
3672
3673 static int
3674 simple_operand_p (const_tree exp)
3675 {
3676   /* Strip any conversions that don't change the machine mode.  */
3677   STRIP_NOPS (exp);
3678
3679   return (CONSTANT_CLASS_P (exp)
3680           || TREE_CODE (exp) == SSA_NAME
3681           || (DECL_P (exp)
3682               && ! TREE_ADDRESSABLE (exp)
3683               && ! TREE_THIS_VOLATILE (exp)
3684               && ! DECL_NONLOCAL (exp)
3685               /* Don't regard global variables as simple.  They may be
3686                  allocated in ways unknown to the compiler (shared memory,
3687                  #pragma weak, etc).  */
3688               && ! TREE_PUBLIC (exp)
3689               && ! DECL_EXTERNAL (exp)
3690               /* Loading a static variable is unduly expensive, but global
3691                  registers aren't expensive.  */
3692               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3693 }
3694
3695 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
3696    to be evaluated unconditionally.
3697    I addition to simple_operand_p, we assume that comparisons, conversions,
3698    and logic-not operations are simple, if their operands are simple, too.  */
3699
3700 static bool
3701 simple_operand_p_2 (tree exp)
3702 {
3703   enum tree_code code;
3704
3705   if (TREE_SIDE_EFFECTS (exp)
3706       || tree_could_trap_p (exp))
3707     return false;
3708
3709   while (CONVERT_EXPR_P (exp))
3710     exp = TREE_OPERAND (exp, 0);
3711
3712   code = TREE_CODE (exp);
3713
3714   if (TREE_CODE_CLASS (code) == tcc_comparison)
3715     return (simple_operand_p (TREE_OPERAND (exp, 0))
3716             && simple_operand_p (TREE_OPERAND (exp, 1)));
3717
3718   if (code == TRUTH_NOT_EXPR)
3719       return simple_operand_p_2 (TREE_OPERAND (exp, 0));
3720
3721   return simple_operand_p (exp);
3722 }
3723
3724 \f
3725 /* The following functions are subroutines to fold_range_test and allow it to
3726    try to change a logical combination of comparisons into a range test.
3727
3728    For example, both
3729         X == 2 || X == 3 || X == 4 || X == 5
3730    and
3731         X >= 2 && X <= 5
3732    are converted to
3733         (unsigned) (X - 2) <= 3
3734
3735    We describe each set of comparisons as being either inside or outside
3736    a range, using a variable named like IN_P, and then describe the
3737    range with a lower and upper bound.  If one of the bounds is omitted,
3738    it represents either the highest or lowest value of the type.
3739
3740    In the comments below, we represent a range by two numbers in brackets
3741    preceded by a "+" to designate being inside that range, or a "-" to
3742    designate being outside that range, so the condition can be inverted by
3743    flipping the prefix.  An omitted bound is represented by a "-".  For
3744    example, "- [-, 10]" means being outside the range starting at the lowest
3745    possible value and ending at 10, in other words, being greater than 10.
3746    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3747    always false.
3748
3749    We set up things so that the missing bounds are handled in a consistent
3750    manner so neither a missing bound nor "true" and "false" need to be
3751    handled using a special case.  */
3752
3753 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3754    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3755    and UPPER1_P are nonzero if the respective argument is an upper bound
3756    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
3757    must be specified for a comparison.  ARG1 will be converted to ARG0's
3758    type if both are specified.  */
3759
3760 static tree
3761 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3762              tree arg1, int upper1_p)
3763 {
3764   tree tem;
3765   int result;
3766   int sgn0, sgn1;
3767
3768   /* If neither arg represents infinity, do the normal operation.
3769      Else, if not a comparison, return infinity.  Else handle the special
3770      comparison rules. Note that most of the cases below won't occur, but
3771      are handled for consistency.  */
3772
3773   if (arg0 != 0 && arg1 != 0)
3774     {
3775       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3776                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
3777       STRIP_NOPS (tem);
3778       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3779     }
3780
3781   if (TREE_CODE_CLASS (code) != tcc_comparison)
3782     return 0;
3783
3784   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3785      for neither.  In real maths, we cannot assume open ended ranges are
3786      the same. But, this is computer arithmetic, where numbers are finite.
3787      We can therefore make the transformation of any unbounded range with
3788      the value Z, Z being greater than any representable number. This permits
3789      us to treat unbounded ranges as equal.  */
3790   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3791   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3792   switch (code)
3793     {
3794     case EQ_EXPR:
3795       result = sgn0 == sgn1;
3796       break;
3797     case NE_EXPR:
3798       result = sgn0 != sgn1;
3799       break;
3800     case LT_EXPR:
3801       result = sgn0 < sgn1;
3802       break;
3803     case LE_EXPR:
3804       result = sgn0 <= sgn1;
3805       break;
3806     case GT_EXPR:
3807       result = sgn0 > sgn1;
3808       break;
3809     case GE_EXPR:
3810       result = sgn0 >= sgn1;
3811       break;
3812     default:
3813       gcc_unreachable ();
3814     }
3815
3816   return constant_boolean_node (result, type);
3817 }
3818 \f
3819 /* Helper routine for make_range.  Perform one step for it, return
3820    new expression if the loop should continue or NULL_TREE if it should
3821    stop.  */
3822
3823 tree
3824 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
3825                  tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
3826                  bool *strict_overflow_p)
3827 {
3828   tree arg0_type = TREE_TYPE (arg0);
3829   tree n_low, n_high, low = *p_low, high = *p_high;
3830   int in_p = *p_in_p, n_in_p;
3831
3832   switch (code)
3833     {
3834     case TRUTH_NOT_EXPR:
3835       *p_in_p = ! in_p;
3836       return arg0;
3837
3838     case EQ_EXPR: case NE_EXPR:
3839     case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3840       /* We can only do something if the range is testing for zero
3841          and if the second operand is an integer constant.  Note that
3842          saying something is "in" the range we make is done by
3843          complementing IN_P since it will set in the initial case of
3844          being not equal to zero; "out" is leaving it alone.  */
3845       if (low == NULL_TREE || high == NULL_TREE
3846           || ! integer_zerop (low) || ! integer_zerop (high)
3847           || TREE_CODE (arg1) != INTEGER_CST)
3848         return NULL_TREE;
3849
3850       switch (code)
3851         {
3852         case NE_EXPR:  /* - [c, c]  */
3853           low = high = arg1;
3854           break;
3855         case EQ_EXPR:  /* + [c, c]  */
3856           in_p = ! in_p, low = high = arg1;
3857           break;
3858         case GT_EXPR:  /* - [-, c] */
3859           low = 0, high = arg1;
3860           break;
3861         case GE_EXPR:  /* + [c, -] */
3862           in_p = ! in_p, low = arg1, high = 0;
3863           break;
3864         case LT_EXPR:  /* - [c, -] */
3865           low = arg1, high = 0;
3866           break;
3867         case LE_EXPR:  /* + [-, c] */
3868           in_p = ! in_p, low = 0, high = arg1;
3869           break;
3870         default:
3871           gcc_unreachable ();
3872         }
3873
3874       /* If this is an unsigned comparison, we also know that EXP is
3875          greater than or equal to zero.  We base the range tests we make
3876          on that fact, so we record it here so we can parse existing
3877          range tests.  We test arg0_type since often the return type
3878          of, e.g. EQ_EXPR, is boolean.  */
3879       if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3880         {
3881           if (! merge_ranges (&n_in_p, &n_low, &n_high,
3882                               in_p, low, high, 1,
3883                               build_int_cst (arg0_type, 0),
3884                               NULL_TREE))
3885             return NULL_TREE;
3886
3887           in_p = n_in_p, low = n_low, high = n_high;
3888
3889           /* If the high bound is missing, but we have a nonzero low
3890              bound, reverse the range so it goes from zero to the low bound
3891              minus 1.  */
3892           if (high == 0 && low && ! integer_zerop (low))
3893             {
3894               in_p = ! in_p;
3895               high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3896                                   integer_one_node, 0);
3897               low = build_int_cst (arg0_type, 0);
3898             }
3899         }
3900
3901       *p_low = low;
3902       *p_high = high;
3903       *p_in_p = in_p;
3904       return arg0;
3905
3906     case NEGATE_EXPR:
3907       /* (-x) IN [a,b] -> x in [-b, -a]  */
3908       n_low = range_binop (MINUS_EXPR, exp_type,
3909                            build_int_cst (exp_type, 0),
3910                            0, high, 1);
3911       n_high = range_binop (MINUS_EXPR, exp_type,
3912                             build_int_cst (exp_type, 0),
3913                             0, low, 0);
3914       if (n_high != 0 && TREE_OVERFLOW (n_high))
3915         return NULL_TREE;
3916       goto normalize;
3917
3918     case BIT_NOT_EXPR:
3919       /* ~ X -> -X - 1  */
3920       return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
3921                          build_int_cst (exp_type, 1));
3922
3923     case PLUS_EXPR:
3924     case MINUS_EXPR:
3925       if (TREE_CODE (arg1) != INTEGER_CST)
3926         return NULL_TREE;
3927
3928       /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
3929          move a constant to the other side.  */
3930       if (!TYPE_UNSIGNED (arg0_type)
3931           && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
3932         return NULL_TREE;
3933
3934       /* If EXP is signed, any overflow in the computation is undefined,
3935          so we don't worry about it so long as our computations on
3936          the bounds don't overflow.  For unsigned, overflow is defined
3937          and this is exactly the right thing.  */
3938       n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3939                            arg0_type, low, 0, arg1, 0);
3940       n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3941                             arg0_type, high, 1, arg1, 0);
3942       if ((n_low != 0 && TREE_OVERFLOW (n_low))
3943           || (n_high != 0 && TREE_OVERFLOW (n_high)))
3944         return NULL_TREE;
3945
3946       if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
3947         *strict_overflow_p = true;
3948
3949       normalize:
3950         /* Check for an unsigned range which has wrapped around the maximum
3951            value thus making n_high < n_low, and normalize it.  */
3952         if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3953           {
3954             low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
3955                                integer_one_node, 0);
3956             high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
3957                                 integer_one_node, 0);
3958
3959             /* If the range is of the form +/- [ x+1, x ], we won't
3960                be able to normalize it.  But then, it represents the
3961                whole range or the empty set, so make it
3962                +/- [ -, - ].  */
3963             if (tree_int_cst_equal (n_low, low)
3964                 && tree_int_cst_equal (n_high, high))
3965               low = high = 0;
3966             else
3967               in_p = ! in_p;
3968           }
3969         else
3970           low = n_low, high = n_high;
3971
3972         *p_low = low;
3973         *p_high = high;
3974         *p_in_p = in_p;
3975         return arg0;
3976
3977     CASE_CONVERT:
3978     case NON_LVALUE_EXPR:
3979       if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
3980         return NULL_TREE;
3981
3982       if (! INTEGRAL_TYPE_P (arg0_type)
3983           || (low != 0 && ! int_fits_type_p (low, arg0_type))
3984           || (high != 0 && ! int_fits_type_p (high, arg0_type)))
3985         return NULL_TREE;
3986
3987       n_low = low, n_high = high;
3988
3989       if (n_low != 0)
3990         n_low = fold_convert_loc (loc, arg0_type, n_low);
3991
3992       if (n_high != 0)
3993         n_high = fold_convert_loc (loc, arg0_type, n_high);
3994
3995       /* If we're converting arg0 from an unsigned type, to exp,
3996          a signed type,  we will be doing the comparison as unsigned.
3997          The tests above have already verified that LOW and HIGH
3998          are both positive.
3999
4000          So we have to ensure that we will handle large unsigned
4001          values the same way that the current signed bounds treat
4002          negative values.  */
4003
4004       if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4005         {
4006           tree high_positive;
4007           tree equiv_type;
4008           /* For fixed-point modes, we need to pass the saturating flag
4009              as the 2nd parameter.  */
4010           if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4011             equiv_type
4012               = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4013                                                 TYPE_SATURATING (arg0_type));
4014           else
4015             equiv_type
4016               = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4017
4018           /* A range without an upper bound is, naturally, unbounded.
4019              Since convert would have cropped a very large value, use
4020              the max value for the destination type.  */
4021           high_positive
4022             = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4023               : TYPE_MAX_VALUE (arg0_type);
4024
4025           if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4026             high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4027                                              fold_convert_loc (loc, arg0_type,
4028                                                                high_positive),
4029                                              build_int_cst (arg0_type, 1));
4030
4031           /* If the low bound is specified, "and" the range with the
4032              range for which the original unsigned value will be
4033              positive.  */
4034           if (low != 0)
4035             {
4036               if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4037                                   1, fold_convert_loc (loc, arg0_type,
4038                                                        integer_zero_node),
4039                                   high_positive))
4040                 return NULL_TREE;
4041
4042               in_p = (n_in_p == in_p);
4043             }
4044           else
4045             {
4046               /* Otherwise, "or" the range with the range of the input
4047                  that will be interpreted as negative.  */
4048               if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4049                                   1, fold_convert_loc (loc, arg0_type,
4050                                                        integer_zero_node),
4051                                   high_positive))
4052                 return NULL_TREE;
4053
4054               in_p = (in_p != n_in_p);
4055             }
4056         }
4057
4058       *p_low = n_low;
4059       *p_high = n_high;
4060       *p_in_p = in_p;
4061       return arg0;
4062
4063     default:
4064       return NULL_TREE;
4065     }
4066 }
4067
4068 /* Given EXP, a logical expression, set the range it is testing into
4069    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
4070    actually being tested.  *PLOW and *PHIGH will be made of the same
4071    type as the returned expression.  If EXP is not a comparison, we
4072    will most likely not be returning a useful value and range.  Set
4073    *STRICT_OVERFLOW_P to true if the return value is only valid
4074    because signed overflow is undefined; otherwise, do not change
4075    *STRICT_OVERFLOW_P.  */
4076
4077 tree
4078 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4079             bool *strict_overflow_p)
4080 {
4081   enum tree_code code;
4082   tree arg0, arg1 = NULL_TREE;
4083   tree exp_type, nexp;
4084   int in_p;
4085   tree low, high;
4086   location_t loc = EXPR_LOCATION (exp);
4087
4088   /* Start with simply saying "EXP != 0" and then look at the code of EXP
4089      and see if we can refine the range.  Some of the cases below may not
4090      happen, but it doesn't seem worth worrying about this.  We "continue"
4091      the outer loop when we've changed something; otherwise we "break"
4092      the switch, which will "break" the while.  */
4093
4094   in_p = 0;
4095   low = high = build_int_cst (TREE_TYPE (exp), 0);
4096
4097   while (1)
4098     {
4099       code = TREE_CODE (exp);
4100       exp_type = TREE_TYPE (exp);
4101       arg0 = NULL_TREE;
4102
4103       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4104         {
4105           if (TREE_OPERAND_LENGTH (exp) > 0)
4106             arg0 = TREE_OPERAND (exp, 0);
4107           if (TREE_CODE_CLASS (code) == tcc_binary
4108               || TREE_CODE_CLASS (code) == tcc_comparison
4109               || (TREE_CODE_CLASS (code) == tcc_expression
4110                   && TREE_OPERAND_LENGTH (exp) > 1))
4111             arg1 = TREE_OPERAND (exp, 1);
4112         }
4113       if (arg0 == NULL_TREE)
4114         break;
4115
4116       nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4117                               &high, &in_p, strict_overflow_p);
4118       if (nexp == NULL_TREE)
4119         break;
4120       exp = nexp;
4121     }
4122
4123   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4124   if (TREE_CODE (exp) == INTEGER_CST)
4125     {
4126       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4127                                                  exp, 0, low, 0))
4128                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4129                                                     exp, 1, high, 1)));
4130       low = high = 0;
4131       exp = 0;
4132     }
4133
4134   *pin_p = in_p, *plow = low, *phigh = high;
4135   return exp;
4136 }
4137 \f
4138 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4139    type, TYPE, return an expression to test if EXP is in (or out of, depending
4140    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4141
4142 tree
4143 build_range_check (location_t loc, tree type, tree exp, int in_p,
4144                    tree low, tree high)
4145 {
4146   tree etype = TREE_TYPE (exp), value;
4147
4148 #ifdef HAVE_canonicalize_funcptr_for_compare
4149   /* Disable this optimization for function pointer expressions
4150      on targets that require function pointer canonicalization.  */
4151   if (HAVE_canonicalize_funcptr_for_compare
4152       && TREE_CODE (etype) == POINTER_TYPE
4153       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4154     return NULL_TREE;
4155 #endif
4156
4157   if (! in_p)
4158     {
4159       value = build_range_check (loc, type, exp, 1, low, high);
4160       if (value != 0)
4161         return invert_truthvalue_loc (loc, value);
4162
4163       return 0;
4164     }
4165
4166   if (low == 0 && high == 0)
4167     return build_int_cst (type, 1);
4168
4169   if (low == 0)
4170     return fold_build2_loc (loc, LE_EXPR, type, exp,
4171                         fold_convert_loc (loc, etype, high));
4172
4173   if (high == 0)
4174     return fold_build2_loc (loc, GE_EXPR, type, exp,
4175                         fold_convert_loc (loc, etype, low));
4176
4177   if (operand_equal_p (low, high, 0))
4178     return fold_build2_loc (loc, EQ_EXPR, type, exp,
4179                         fold_convert_loc (loc, etype, low));
4180
4181   if (integer_zerop (low))
4182     {
4183       if (! TYPE_UNSIGNED (etype))
4184         {
4185           etype = unsigned_type_for (etype);
4186           high = fold_convert_loc (loc, etype, high);
4187           exp = fold_convert_loc (loc, etype, exp);
4188         }
4189       return build_range_check (loc, type, exp, 1, 0, high);
4190     }
4191
4192   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4193   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4194     {
4195       unsigned HOST_WIDE_INT lo;
4196       HOST_WIDE_INT hi;
4197       int prec;
4198
4199       prec = TYPE_PRECISION (etype);
4200       if (prec <= HOST_BITS_PER_WIDE_INT)
4201         {
4202           hi = 0;
4203           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4204         }
4205       else
4206         {
4207           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4208           lo = (unsigned HOST_WIDE_INT) -1;
4209         }
4210
4211       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4212         {
4213           if (TYPE_UNSIGNED (etype))
4214             {
4215               tree signed_etype = signed_type_for (etype);
4216               if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4217                 etype
4218                   = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4219               else
4220                 etype = signed_etype;
4221               exp = fold_convert_loc (loc, etype, exp);
4222             }
4223           return fold_build2_loc (loc, GT_EXPR, type, exp,
4224                               build_int_cst (etype, 0));
4225         }
4226     }
4227
4228   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4229      This requires wrap-around arithmetics for the type of the expression.
4230      First make sure that arithmetics in this type is valid, then make sure
4231      that it wraps around.  */
4232   if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4233     etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4234                                             TYPE_UNSIGNED (etype));
4235
4236   if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4237     {
4238       tree utype, minv, maxv;
4239
4240       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4241          for the type in question, as we rely on this here.  */
4242       utype = unsigned_type_for (etype);
4243       maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4244       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4245                           integer_one_node, 1);
4246       minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4247
4248       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4249                                       minv, 1, maxv, 1)))
4250         etype = utype;
4251       else
4252         return 0;
4253     }
4254
4255   high = fold_convert_loc (loc, etype, high);
4256   low = fold_convert_loc (loc, etype, low);
4257   exp = fold_convert_loc (loc, etype, exp);
4258
4259   value = const_binop (MINUS_EXPR, high, low);
4260
4261
4262   if (POINTER_TYPE_P (etype))
4263     {
4264       if (value != 0 && !TREE_OVERFLOW (value))
4265         {
4266           low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4267           return build_range_check (loc, type,
4268                                     fold_build_pointer_plus_loc (loc, exp, low),
4269                                     1, build_int_cst (etype, 0), value);
4270         }
4271       return 0;
4272     }
4273
4274   if (value != 0 && !TREE_OVERFLOW (value))
4275     return build_range_check (loc, type,
4276                               fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4277                               1, build_int_cst (etype, 0), value);
4278
4279   return 0;
4280 }
4281 \f
4282 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4283
4284 static tree
4285 range_predecessor (tree val)
4286 {
4287   tree type = TREE_TYPE (val);
4288
4289   if (INTEGRAL_TYPE_P (type)
4290       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4291     return 0;
4292   else
4293     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4294 }
4295
4296 /* Return the successor of VAL in its type, handling the infinite case.  */
4297
4298 static tree
4299 range_successor (tree val)
4300 {
4301   tree type = TREE_TYPE (val);
4302
4303   if (INTEGRAL_TYPE_P (type)
4304       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4305     return 0;
4306   else
4307     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4308 }
4309
4310 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4311    can, 0 if we can't.  Set the output range into the specified parameters.  */
4312
4313 bool
4314 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4315               tree high0, int in1_p, tree low1, tree high1)
4316 {
4317   int no_overlap;
4318   int subset;
4319   int temp;
4320   tree tem;
4321   int in_p;
4322   tree low, high;
4323   int lowequal = ((low0 == 0 && low1 == 0)
4324                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4325                                                 low0, 0, low1, 0)));
4326   int highequal = ((high0 == 0 && high1 == 0)
4327                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4328                                                  high0, 1, high1, 1)));
4329
4330   /* Make range 0 be the range that starts first, or ends last if they
4331      start at the same value.  Swap them if it isn't.  */
4332   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4333                                  low0, 0, low1, 0))
4334       || (lowequal
4335           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4336                                         high1, 1, high0, 1))))
4337     {
4338       temp = in0_p, in0_p = in1_p, in1_p = temp;
4339       tem = low0, low0 = low1, low1 = tem;
4340       tem = high0, high0 = high1, high1 = tem;
4341     }
4342
4343   /* Now flag two cases, whether the ranges are disjoint or whether the
4344      second range is totally subsumed in the first.  Note that the tests
4345      below are simplified by the ones above.  */
4346   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4347                                           high0, 1, low1, 0));
4348   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4349                                       high1, 1, high0, 1));
4350
4351   /* We now have four cases, depending on whether we are including or
4352      excluding the two ranges.  */
4353   if (in0_p && in1_p)
4354     {
4355       /* If they don't overlap, the result is false.  If the second range
4356          is a subset it is the result.  Otherwise, the range is from the start
4357          of the second to the end of the first.  */
4358       if (no_overlap)
4359         in_p = 0, low = high = 0;
4360       else if (subset)
4361         in_p = 1, low = low1, high = high1;
4362       else
4363         in_p = 1, low = low1, high = high0;
4364     }
4365
4366   else if (in0_p && ! in1_p)
4367     {
4368       /* If they don't overlap, the result is the first range.  If they are
4369          equal, the result is false.  If the second range is a subset of the
4370          first, and the ranges begin at the same place, we go from just after
4371          the end of the second range to the end of the first.  If the second
4372          range is not a subset of the first, or if it is a subset and both
4373          ranges end at the same place, the range starts at the start of the
4374          first range and ends just before the second range.
4375          Otherwise, we can't describe this as a single range.  */
4376       if (no_overlap)
4377         in_p = 1, low = low0, high = high0;
4378       else if (lowequal && highequal)
4379         in_p = 0, low = high = 0;
4380       else if (subset && lowequal)
4381         {
4382           low = range_successor (high1);
4383           high = high0;
4384           in_p = 1;
4385           if (low == 0)
4386             {
4387               /* We are in the weird situation where high0 > high1 but
4388                  high1 has no successor.  Punt.  */
4389               return 0;
4390             }
4391         }
4392       else if (! subset || highequal)
4393         {
4394           low = low0;
4395           high = range_predecessor (low1);
4396           in_p = 1;
4397           if (high == 0)
4398             {
4399               /* low0 < low1 but low1 has no predecessor.  Punt.  */
4400               return 0;
4401             }
4402         }
4403       else
4404         return 0;
4405     }
4406
4407   else if (! in0_p && in1_p)
4408     {
4409       /* If they don't overlap, the result is the second range.  If the second
4410          is a subset of the first, the result is false.  Otherwise,
4411          the range starts just after the first range and ends at the
4412          end of the second.  */
4413       if (no_overlap)
4414         in_p = 1, low = low1, high = high1;
4415       else if (subset || highequal)
4416         in_p = 0, low = high = 0;
4417       else
4418         {
4419           low = range_successor (high0);
4420           high = high1;
4421           in_p = 1;
4422           if (low == 0)
4423             {
4424               /* high1 > high0 but high0 has no successor.  Punt.  */
4425               return 0;
4426             }
4427         }
4428     }
4429
4430   else
4431     {
4432       /* The case where we are excluding both ranges.  Here the complex case
4433          is if they don't overlap.  In that case, the only time we have a
4434          range is if they are adjacent.  If the second is a subset of the
4435          first, the result is the first.  Otherwise, the range to exclude
4436          starts at the beginning of the first range and ends at the end of the
4437          second.  */
4438       if (no_overlap)
4439         {
4440           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4441                                          range_successor (high0),
4442                                          1, low1, 0)))
4443             in_p = 0, low = low0, high = high1;
4444           else
4445             {
4446               /* Canonicalize - [min, x] into - [-, x].  */
4447               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4448                 switch (TREE_CODE (TREE_TYPE (low0)))
4449                   {
4450                   case ENUMERAL_TYPE:
4451                     if (TYPE_PRECISION (TREE_TYPE (low0))
4452                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4453                       break;
4454                     /* FALLTHROUGH */
4455                   case INTEGER_TYPE:
4456                     if (tree_int_cst_equal (low0,
4457                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4458                       low0 = 0;
4459                     break;
4460                   case POINTER_TYPE:
4461                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4462                         && integer_zerop (low0))
4463                       low0 = 0;
4464                     break;
4465                   default:
4466                     break;
4467                   }
4468
4469               /* Canonicalize - [x, max] into - [x, -].  */
4470               if (high1 && TREE_CODE (high1) == INTEGER_CST)
4471                 switch (TREE_CODE (TREE_TYPE (high1)))
4472                   {
4473                   case ENUMERAL_TYPE:
4474                     if (TYPE_PRECISION (TREE_TYPE (high1))
4475                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4476                       break;
4477                     /* FALLTHROUGH */
4478                   case INTEGER_TYPE:
4479                     if (tree_int_cst_equal (high1,
4480                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
4481                       high1 = 0;
4482                     break;
4483                   case POINTER_TYPE:
4484                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
4485                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4486                                                        high1, 1,
4487                                                        integer_one_node, 1)))
4488                       high1 = 0;
4489                     break;
4490                   default:
4491                     break;
4492                   }
4493
4494               /* The ranges might be also adjacent between the maximum and
4495                  minimum values of the given type.  For
4496                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4497                  return + [x + 1, y - 1].  */
4498               if (low0 == 0 && high1 == 0)
4499                 {
4500                   low = range_successor (high0);
4501                   high = range_predecessor (low1);
4502                   if (low == 0 || high == 0)
4503                     return 0;
4504
4505                   in_p = 1;
4506                 }
4507               else
4508                 return 0;
4509             }
4510         }
4511       else if (subset)
4512         in_p = 0, low = low0, high = high0;
4513       else
4514         in_p = 0, low = low0, high = high1;
4515     }
4516
4517   *pin_p = in_p, *plow = low, *phigh = high;
4518   return 1;
4519 }
4520 \f
4521
4522 /* Subroutine of fold, looking inside expressions of the form
4523    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4524    of the COND_EXPR.  This function is being used also to optimize
4525    A op B ? C : A, by reversing the comparison first.
4526
4527    Return a folded expression whose code is not a COND_EXPR
4528    anymore, or NULL_TREE if no folding opportunity is found.  */
4529
4530 static tree
4531 fold_cond_expr_with_comparison (location_t loc, tree type,
4532                                 tree arg0, tree arg1, tree arg2)
4533 {
4534   enum tree_code comp_code = TREE_CODE (arg0);
4535   tree arg00 = TREE_OPERAND (arg0, 0);
4536   tree arg01 = TREE_OPERAND (arg0, 1);
4537   tree arg1_type = TREE_TYPE (arg1);
4538   tree tem;
4539
4540   STRIP_NOPS (arg1);
4541   STRIP_NOPS (arg2);
4542
4543   /* If we have A op 0 ? A : -A, consider applying the following
4544      transformations:
4545
4546      A == 0? A : -A    same as -A
4547      A != 0? A : -A    same as A
4548      A >= 0? A : -A    same as abs (A)
4549      A > 0?  A : -A    same as abs (A)
4550      A <= 0? A : -A    same as -abs (A)
4551      A < 0?  A : -A    same as -abs (A)
4552
4553      None of these transformations work for modes with signed
4554      zeros.  If A is +/-0, the first two transformations will
4555      change the sign of the result (from +0 to -0, or vice
4556      versa).  The last four will fix the sign of the result,
4557      even though the original expressions could be positive or
4558      negative, depending on the sign of A.
4559
4560      Note that all these transformations are correct if A is
4561      NaN, since the two alternatives (A and -A) are also NaNs.  */
4562   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4563       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4564           ? real_zerop (arg01)
4565           : integer_zerop (arg01))
4566       && ((TREE_CODE (arg2) == NEGATE_EXPR
4567            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4568              /* In the case that A is of the form X-Y, '-A' (arg2) may
4569                 have already been folded to Y-X, check for that. */
4570           || (TREE_CODE (arg1) == MINUS_EXPR
4571               && TREE_CODE (arg2) == MINUS_EXPR
4572               && operand_equal_p (TREE_OPERAND (arg1, 0),
4573                                   TREE_OPERAND (arg2, 1), 0)
4574               && operand_equal_p (TREE_OPERAND (arg1, 1),
4575                                   TREE_OPERAND (arg2, 0), 0))))
4576     switch (comp_code)
4577       {
4578       case EQ_EXPR:
4579       case UNEQ_EXPR:
4580         tem = fold_convert_loc (loc, arg1_type, arg1);
4581         return pedantic_non_lvalue_loc (loc,
4582                                     fold_convert_loc (loc, type,
4583                                                   negate_expr (tem)));
4584       case NE_EXPR:
4585       case LTGT_EXPR:
4586         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4587       case UNGE_EXPR:
4588       case UNGT_EXPR:
4589         if (flag_trapping_math)
4590           break;
4591         /* Fall through.  */
4592       case GE_EXPR:
4593       case GT_EXPR:
4594         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4595           arg1 = fold_convert_loc (loc, signed_type_for
4596                                (TREE_TYPE (arg1)), arg1);
4597         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4598         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4599       case UNLE_EXPR:
4600       case UNLT_EXPR:
4601         if (flag_trapping_math)
4602           break;
4603       case LE_EXPR:
4604       case LT_EXPR:
4605         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4606           arg1 = fold_convert_loc (loc, signed_type_for
4607                                (TREE_TYPE (arg1)), arg1);
4608         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4609         return negate_expr (fold_convert_loc (loc, type, tem));
4610       default:
4611         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4612         break;
4613       }
4614
4615   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
4616      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
4617      both transformations are correct when A is NaN: A != 0
4618      is then true, and A == 0 is false.  */
4619
4620   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4621       && integer_zerop (arg01) && integer_zerop (arg2))
4622     {
4623       if (comp_code == NE_EXPR)
4624         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4625       else if (comp_code == EQ_EXPR)
4626         return build_int_cst (type, 0);
4627     }
4628
4629   /* Try some transformations of A op B ? A : B.
4630
4631      A == B? A : B    same as B
4632      A != B? A : B    same as A
4633      A >= B? A : B    same as max (A, B)
4634      A > B?  A : B    same as max (B, A)
4635      A <= B? A : B    same as min (A, B)
4636      A < B?  A : B    same as min (B, A)
4637
4638      As above, these transformations don't work in the presence
4639      of signed zeros.  For example, if A and B are zeros of
4640      opposite sign, the first two transformations will change
4641      the sign of the result.  In the last four, the original
4642      expressions give different results for (A=+0, B=-0) and
4643      (A=-0, B=+0), but the transformed expressions do not.
4644
4645      The first two transformations are correct if either A or B
4646      is a NaN.  In the first transformation, the condition will
4647      be false, and B will indeed be chosen.  In the case of the
4648      second transformation, the condition A != B will be true,
4649      and A will be chosen.
4650
4651      The conversions to max() and min() are not correct if B is
4652      a number and A is not.  The conditions in the original
4653      expressions will be false, so all four give B.  The min()
4654      and max() versions would give a NaN instead.  */
4655   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4656       && operand_equal_for_comparison_p (arg01, arg2, arg00)
4657       /* Avoid these transformations if the COND_EXPR may be used
4658          as an lvalue in the C++ front-end.  PR c++/19199.  */
4659       && (in_gimple_form
4660           || (strcmp (lang_hooks.name, "GNU C++") != 0
4661               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4662           || ! maybe_lvalue_p (arg1)
4663           || ! maybe_lvalue_p (arg2)))
4664     {
4665       tree comp_op0 = arg00;
4666       tree comp_op1 = arg01;
4667       tree comp_type = TREE_TYPE (comp_op0);
4668
4669       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
4670       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4671         {
4672           comp_type = type;
4673           comp_op0 = arg1;
4674           comp_op1 = arg2;
4675         }
4676
4677       switch (comp_code)
4678         {
4679         case EQ_EXPR:
4680           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
4681         case NE_EXPR:
4682           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4683         case LE_EXPR:
4684         case LT_EXPR:
4685         case UNLE_EXPR:
4686         case UNLT_EXPR:
4687           /* In C++ a ?: expression can be an lvalue, so put the
4688              operand which will be used if they are equal first
4689              so that we can convert this back to the
4690              corresponding COND_EXPR.  */
4691           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4692             {
4693               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4694               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4695               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4696                     ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
4697                     : fold_build2_loc (loc, MIN_EXPR, comp_type,
4698                                    comp_op1, comp_op0);
4699               return pedantic_non_lvalue_loc (loc,
4700                                           fold_convert_loc (loc, type, tem));
4701             }
4702           break;
4703         case GE_EXPR:
4704         case GT_EXPR:
4705         case UNGE_EXPR:
4706         case UNGT_EXPR:
4707           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4708             {
4709               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4710               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4711               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4712                     ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
4713                     : fold_build2_loc (loc, MAX_EXPR, comp_type,
4714                                    comp_op1, comp_op0);
4715               return pedantic_non_lvalue_loc (loc,
4716                                           fold_convert_loc (loc, type, tem));
4717             }
4718           break;
4719         case UNEQ_EXPR:
4720           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4721             return pedantic_non_lvalue_loc (loc,
4722                                         fold_convert_loc (loc, type, arg2));
4723           break;
4724         case LTGT_EXPR:
4725           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4726             return pedantic_non_lvalue_loc (loc,
4727                                         fold_convert_loc (loc, type, arg1));
4728           break;
4729         default:
4730           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4731           break;
4732         }
4733     }
4734
4735   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4736      we might still be able to simplify this.  For example,
4737      if C1 is one less or one more than C2, this might have started
4738      out as a MIN or MAX and been transformed by this function.
4739      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
4740
4741   if (INTEGRAL_TYPE_P (type)
4742       && TREE_CODE (arg01) == INTEGER_CST
4743       && TREE_CODE (arg2) == INTEGER_CST)
4744     switch (comp_code)
4745       {
4746       case EQ_EXPR:
4747         if (TREE_CODE (arg1) == INTEGER_CST)
4748           break;
4749         /* We can replace A with C1 in this case.  */
4750         arg1 = fold_convert_loc (loc, type, arg01);
4751         return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
4752
4753       case LT_EXPR:
4754         /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
4755            MIN_EXPR, to preserve the signedness of the comparison.  */
4756         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4757                                OEP_ONLY_CONST)
4758             && operand_equal_p (arg01,
4759                                 const_binop (PLUS_EXPR, arg2,
4760                                              build_int_cst (type, 1)),
4761                                 OEP_ONLY_CONST))
4762           {
4763             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4764                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4765                                                      arg2));
4766             return pedantic_non_lvalue_loc (loc,
4767                                             fold_convert_loc (loc, type, tem));
4768           }
4769         break;
4770
4771       case LE_EXPR:
4772         /* If C1 is C2 - 1, this is min(A, C2), with the same care
4773            as above.  */
4774         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4775                                OEP_ONLY_CONST)
4776             && operand_equal_p (arg01,
4777                                 const_binop (MINUS_EXPR, arg2,
4778                                              build_int_cst (type, 1)),
4779                                 OEP_ONLY_CONST))
4780           {
4781             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4782                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4783                                                      arg2));
4784             return pedantic_non_lvalue_loc (loc,
4785                                             fold_convert_loc (loc, type, tem));
4786           }
4787         break;
4788
4789       case GT_EXPR:
4790         /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
4791            MAX_EXPR, to preserve the signedness of the comparison.  */
4792         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4793                                OEP_ONLY_CONST)
4794             && operand_equal_p (arg01,
4795                                 const_binop (MINUS_EXPR, arg2,
4796                                              build_int_cst (type, 1)),
4797                                 OEP_ONLY_CONST))
4798           {
4799             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4800                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4801                                                      arg2));
4802             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4803           }
4804         break;
4805
4806       case GE_EXPR:
4807         /* If C1 is C2 + 1, this is max(A, C2), with the same care as above.  */
4808         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4809                                OEP_ONLY_CONST)
4810             && operand_equal_p (arg01,
4811                                 const_binop (PLUS_EXPR, arg2,
4812                                              build_int_cst (type, 1)),
4813                                 OEP_ONLY_CONST))
4814           {
4815             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4816                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4817                                                      arg2));
4818             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4819           }
4820         break;
4821       case NE_EXPR:
4822         break;
4823       default:
4824         gcc_unreachable ();
4825       }
4826
4827   return NULL_TREE;
4828 }
4829
4830
4831 \f
4832 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4833 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
4834   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
4835                 false) >= 2)
4836 #endif
4837
4838 /* EXP is some logical combination of boolean tests.  See if we can
4839    merge it into some range test.  Return the new tree if so.  */
4840
4841 static tree
4842 fold_range_test (location_t loc, enum tree_code code, tree type,
4843                  tree op0, tree op1)
4844 {
4845   int or_op = (code == TRUTH_ORIF_EXPR
4846                || code == TRUTH_OR_EXPR);
4847   int in0_p, in1_p, in_p;
4848   tree low0, low1, low, high0, high1, high;
4849   bool strict_overflow_p = false;
4850   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
4851   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
4852   tree tem;
4853   const char * const warnmsg = G_("assuming signed overflow does not occur "
4854                                   "when simplifying range test");
4855
4856   /* If this is an OR operation, invert both sides; we will invert
4857      again at the end.  */
4858   if (or_op)
4859     in0_p = ! in0_p, in1_p = ! in1_p;
4860
4861   /* If both expressions are the same, if we can merge the ranges, and we
4862      can build the range test, return it or it inverted.  If one of the
4863      ranges is always true or always false, consider it to be the same
4864      expression as the other.  */
4865   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4866       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4867                        in1_p, low1, high1)
4868       && 0 != (tem = (build_range_check (loc, type,
4869                                          lhs != 0 ? lhs
4870                                          : rhs != 0 ? rhs : integer_zero_node,
4871                                          in_p, low, high))))
4872     {
4873       if (strict_overflow_p)
4874         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
4875       return or_op ? invert_truthvalue_loc (loc, tem) : tem;
4876     }
4877
4878   /* On machines where the branch cost is expensive, if this is a
4879      short-circuited branch and the underlying object on both sides
4880      is the same, make a non-short-circuit operation.  */
4881   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4882            && lhs != 0 && rhs != 0
4883            && (code == TRUTH_ANDIF_EXPR
4884                || code == TRUTH_ORIF_EXPR)
4885            && operand_equal_p (lhs, rhs, 0))
4886     {
4887       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
4888          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4889          which cases we can't do this.  */
4890       if (simple_operand_p (lhs))
4891         return build2_loc (loc, code == TRUTH_ANDIF_EXPR
4892                            ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4893                            type, op0, op1);
4894
4895       else if (!lang_hooks.decls.global_bindings_p ()
4896                && !CONTAINS_PLACEHOLDER_P (lhs))
4897         {
4898           tree common = save_expr (lhs);
4899
4900           if (0 != (lhs = build_range_check (loc, type, common,
4901                                              or_op ? ! in0_p : in0_p,
4902                                              low0, high0))
4903               && (0 != (rhs = build_range_check (loc, type, common,
4904                                                  or_op ? ! in1_p : in1_p,
4905                                                  low1, high1))))
4906             {
4907               if (strict_overflow_p)
4908                 fold_overflow_warning (warnmsg,
4909                                        WARN_STRICT_OVERFLOW_COMPARISON);
4910               return build2_loc (loc, code == TRUTH_ANDIF_EXPR
4911                                  ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4912                                  type, lhs, rhs);
4913             }
4914         }
4915     }
4916
4917   return 0;
4918 }
4919 \f
4920 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
4921    bit value.  Arrange things so the extra bits will be set to zero if and
4922    only if C is signed-extended to its full width.  If MASK is nonzero,
4923    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
4924
4925 static tree
4926 unextend (tree c, int p, int unsignedp, tree mask)
4927 {
4928   tree type = TREE_TYPE (c);
4929   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4930   tree temp;
4931
4932   if (p == modesize || unsignedp)
4933     return c;
4934
4935   /* We work by getting just the sign bit into the low-order bit, then
4936      into the high-order bit, then sign-extend.  We then XOR that value
4937      with C.  */
4938   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1));
4939   temp = const_binop (BIT_AND_EXPR, temp, size_int (1));
4940
4941   /* We must use a signed type in order to get an arithmetic right shift.
4942      However, we must also avoid introducing accidental overflows, so that
4943      a subsequent call to integer_zerop will work.  Hence we must
4944      do the type conversion here.  At this point, the constant is either
4945      zero or one, and the conversion to a signed type can never overflow.
4946      We could get an overflow if this conversion is done anywhere else.  */
4947   if (TYPE_UNSIGNED (type))
4948     temp = fold_convert (signed_type_for (type), temp);
4949
4950   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
4951   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
4952   if (mask != 0)
4953     temp = const_binop (BIT_AND_EXPR, temp,
4954                         fold_convert (TREE_TYPE (c), mask));
4955   /* If necessary, convert the type back to match the type of C.  */
4956   if (TYPE_UNSIGNED (type))
4957     temp = fold_convert (type, temp);
4958
4959   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
4960 }
4961 \f
4962 /* For an expression that has the form
4963      (A && B) || ~B
4964    or
4965      (A || B) && ~B,
4966    we can drop one of the inner expressions and simplify to
4967      A || ~B
4968    or
4969      A && ~B
4970    LOC is the location of the resulting expression.  OP is the inner 
4971    logical operation; the left-hand side in the examples above, while CMPOP
4972    is the right-hand side.  RHS_ONLY is used to prevent us from accidentally
4973    removing a condition that guards another, as in
4974      (A != NULL && A->...) || A == NULL
4975    which we must not transform.  If RHS_ONLY is true, only eliminate the
4976    right-most operand of the inner logical operation.  */
4977
4978 static tree
4979 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
4980                                  bool rhs_only)
4981 {
4982   tree type = TREE_TYPE (cmpop);
4983   enum tree_code code = TREE_CODE (cmpop);
4984   enum tree_code truthop_code = TREE_CODE (op);
4985   tree lhs = TREE_OPERAND (op, 0);
4986   tree rhs = TREE_OPERAND (op, 1);
4987   tree orig_lhs = lhs, orig_rhs = rhs;
4988   enum tree_code rhs_code = TREE_CODE (rhs);
4989   enum tree_code lhs_code = TREE_CODE (lhs);
4990   enum tree_code inv_code;
4991
4992   if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
4993     return NULL_TREE;
4994
4995   if (TREE_CODE_CLASS (code) != tcc_comparison)
4996     return NULL_TREE;
4997
4998   if (rhs_code == truthop_code)
4999     {
5000       tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5001       if (newrhs != NULL_TREE)
5002         {
5003           rhs = newrhs;
5004           rhs_code = TREE_CODE (rhs);
5005         }
5006     }
5007   if (lhs_code == truthop_code && !rhs_only)
5008     {
5009       tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5010       if (newlhs != NULL_TREE)
5011         {
5012           lhs = newlhs;
5013           lhs_code = TREE_CODE (lhs);
5014         }
5015     }
5016
5017   inv_code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
5018   if (inv_code == rhs_code
5019       && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5020       && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5021     return lhs;
5022   if (!rhs_only && inv_code == lhs_code
5023       && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5024       && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5025     return rhs;
5026   if (rhs != orig_rhs || lhs != orig_lhs)
5027     return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5028                             lhs, rhs);
5029   return NULL_TREE;
5030 }
5031
5032 /* Find ways of folding logical expressions of LHS and RHS:
5033    Try to merge two comparisons to the same innermost item.
5034    Look for range tests like "ch >= '0' && ch <= '9'".
5035    Look for combinations of simple terms on machines with expensive branches
5036    and evaluate the RHS unconditionally.
5037
5038    For example, if we have p->a == 2 && p->b == 4 and we can make an
5039    object large enough to span both A and B, we can do this with a comparison
5040    against the object ANDed with the a mask.
5041
5042    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5043    operations to do this with one comparison.
5044
5045    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5046    function and the one above.
5047
5048    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5049    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5050
5051    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5052    two operands.
5053
5054    We return the simplified tree or 0 if no optimization is possible.  */
5055
5056 static tree
5057 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5058                     tree lhs, tree rhs)
5059 {
5060   /* If this is the "or" of two comparisons, we can do something if
5061      the comparisons are NE_EXPR.  If this is the "and", we can do something
5062      if the comparisons are EQ_EXPR.  I.e.,
5063         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5064
5065      WANTED_CODE is this operation code.  For single bit fields, we can
5066      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5067      comparison for one-bit fields.  */
5068
5069   enum tree_code wanted_code;
5070   enum tree_code lcode, rcode;
5071   tree ll_arg, lr_arg, rl_arg, rr_arg;
5072   tree ll_inner, lr_inner, rl_inner, rr_inner;
5073   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5074   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5075   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5076   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5077   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5078   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5079   enum machine_mode lnmode, rnmode;
5080   tree ll_mask, lr_mask, rl_mask, rr_mask;
5081   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5082   tree l_const, r_const;
5083   tree lntype, rntype, result;
5084   HOST_WIDE_INT first_bit, end_bit;
5085   int volatilep;
5086
5087   /* Start by getting the comparison codes.  Fail if anything is volatile.
5088      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5089      it were surrounded with a NE_EXPR.  */
5090
5091   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5092     return 0;
5093
5094   lcode = TREE_CODE (lhs);
5095   rcode = TREE_CODE (rhs);
5096
5097   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5098     {
5099       lhs = build2 (NE_EXPR, truth_type, lhs,
5100                     build_int_cst (TREE_TYPE (lhs), 0));
5101       lcode = NE_EXPR;
5102     }
5103
5104   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5105     {
5106       rhs = build2 (NE_EXPR, truth_type, rhs,
5107                     build_int_cst (TREE_TYPE (rhs), 0));
5108       rcode = NE_EXPR;
5109     }
5110
5111   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5112       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5113     return 0;
5114
5115   ll_arg = TREE_OPERAND (lhs, 0);
5116   lr_arg = TREE_OPERAND (lhs, 1);
5117   rl_arg = TREE_OPERAND (rhs, 0);
5118   rr_arg = TREE_OPERAND (rhs, 1);
5119
5120   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5121   if (simple_operand_p (ll_arg)
5122       && simple_operand_p (lr_arg))
5123     {
5124       if (operand_equal_p (ll_arg, rl_arg, 0)
5125           && operand_equal_p (lr_arg, rr_arg, 0))
5126         {
5127           result = combine_comparisons (loc, code, lcode, rcode,
5128                                         truth_type, ll_arg, lr_arg);
5129           if (result)
5130             return result;
5131         }
5132       else if (operand_equal_p (ll_arg, rr_arg, 0)
5133                && operand_equal_p (lr_arg, rl_arg, 0))
5134         {
5135           result = combine_comparisons (loc, code, lcode,
5136                                         swap_tree_comparison (rcode),
5137                                         truth_type, ll_arg, lr_arg);
5138           if (result)
5139             return result;
5140         }
5141     }
5142
5143   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5144           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5145
5146   /* If the RHS can be evaluated unconditionally and its operands are
5147      simple, it wins to evaluate the RHS unconditionally on machines
5148      with expensive branches.  In this case, this isn't a comparison
5149      that can be merged.  */
5150
5151   if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5152                    false) >= 2
5153       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5154       && simple_operand_p (rl_arg)
5155       && simple_operand_p (rr_arg))
5156     {
5157       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5158       if (code == TRUTH_OR_EXPR
5159           && lcode == NE_EXPR && integer_zerop (lr_arg)
5160           && rcode == NE_EXPR && integer_zerop (rr_arg)
5161           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5162           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5163         return build2_loc (loc, NE_EXPR, truth_type,
5164                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5165                                    ll_arg, rl_arg),
5166                            build_int_cst (TREE_TYPE (ll_arg), 0));
5167
5168       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5169       if (code == TRUTH_AND_EXPR
5170           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5171           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5172           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5173           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5174         return build2_loc (loc, EQ_EXPR, truth_type,
5175                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5176                                    ll_arg, rl_arg),
5177                            build_int_cst (TREE_TYPE (ll_arg), 0));
5178     }
5179
5180   /* See if the comparisons can be merged.  Then get all the parameters for
5181      each side.  */
5182
5183   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5184       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5185     return 0;
5186
5187   volatilep = 0;
5188   ll_inner = decode_field_reference (loc, ll_arg,
5189                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5190                                      &ll_unsignedp, &volatilep, &ll_mask,
5191                                      &ll_and_mask);
5192   lr_inner = decode_field_reference (loc, lr_arg,
5193                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5194                                      &lr_unsignedp, &volatilep, &lr_mask,
5195                                      &lr_and_mask);
5196   rl_inner = decode_field_reference (loc, rl_arg,
5197                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5198                                      &rl_unsignedp, &volatilep, &rl_mask,
5199                                      &rl_and_mask);
5200   rr_inner = decode_field_reference (loc, rr_arg,
5201                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5202                                      &rr_unsignedp, &volatilep, &rr_mask,
5203                                      &rr_and_mask);
5204
5205   /* It must be true that the inner operation on the lhs of each
5206      comparison must be the same if we are to be able to do anything.
5207      Then see if we have constants.  If not, the same must be true for
5208      the rhs's.  */
5209   if (volatilep || ll_inner == 0 || rl_inner == 0
5210       || ! operand_equal_p (ll_inner, rl_inner, 0))
5211     return 0;
5212
5213   if (TREE_CODE (lr_arg) == INTEGER_CST
5214       && TREE_CODE (rr_arg) == INTEGER_CST)
5215     l_const = lr_arg, r_const = rr_arg;
5216   else if (lr_inner == 0 || rr_inner == 0
5217            || ! operand_equal_p (lr_inner, rr_inner, 0))
5218     return 0;
5219   else
5220     l_const = r_const = 0;
5221
5222   /* If either comparison code is not correct for our logical operation,
5223      fail.  However, we can convert a one-bit comparison against zero into
5224      the opposite comparison against that bit being set in the field.  */
5225
5226   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5227   if (lcode != wanted_code)
5228     {
5229       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5230         {
5231           /* Make the left operand unsigned, since we are only interested
5232              in the value of one bit.  Otherwise we are doing the wrong
5233              thing below.  */
5234           ll_unsignedp = 1;
5235           l_const = ll_mask;
5236         }
5237       else
5238         return 0;
5239     }
5240
5241   /* This is analogous to the code for l_const above.  */
5242   if (rcode != wanted_code)
5243     {
5244       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5245         {
5246           rl_unsignedp = 1;
5247           r_const = rl_mask;
5248         }
5249       else
5250         return 0;
5251     }
5252
5253   /* See if we can find a mode that contains both fields being compared on
5254      the left.  If we can't, fail.  Otherwise, update all constants and masks
5255      to be relative to a field of that size.  */
5256   first_bit = MIN (ll_bitpos, rl_bitpos);
5257   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5258   lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5259                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5260                           volatilep);
5261   if (lnmode == VOIDmode)
5262     return 0;
5263
5264   lnbitsize = GET_MODE_BITSIZE (lnmode);
5265   lnbitpos = first_bit & ~ (lnbitsize - 1);
5266   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5267   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5268
5269   if (BYTES_BIG_ENDIAN)
5270     {
5271       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5272       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5273     }
5274
5275   ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5276                          size_int (xll_bitpos));
5277   rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5278                          size_int (xrl_bitpos));
5279
5280   if (l_const)
5281     {
5282       l_const = fold_convert_loc (loc, lntype, l_const);
5283       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5284       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5285       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5286                                         fold_build1_loc (loc, BIT_NOT_EXPR,
5287                                                      lntype, ll_mask))))
5288         {
5289           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5290
5291           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5292         }
5293     }
5294   if (r_const)
5295     {
5296       r_const = fold_convert_loc (loc, lntype, r_const);
5297       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5298       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5299       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5300                                         fold_build1_loc (loc, BIT_NOT_EXPR,
5301                                                      lntype, rl_mask))))
5302         {
5303           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5304
5305           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5306         }
5307     }
5308
5309   /* If the right sides are not constant, do the same for it.  Also,
5310      disallow this optimization if a size or signedness mismatch occurs
5311      between the left and right sides.  */
5312   if (l_const == 0)
5313     {
5314       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5315           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5316           /* Make sure the two fields on the right
5317              correspond to the left without being swapped.  */
5318           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5319         return 0;
5320
5321       first_bit = MIN (lr_bitpos, rr_bitpos);
5322       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5323       rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5324                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5325                               volatilep);
5326       if (rnmode == VOIDmode)
5327         return 0;
5328
5329       rnbitsize = GET_MODE_BITSIZE (rnmode);
5330       rnbitpos = first_bit & ~ (rnbitsize - 1);
5331       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5332       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5333
5334       if (BYTES_BIG_ENDIAN)
5335         {
5336           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5337           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5338         }
5339
5340       lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5341                                                             rntype, lr_mask),
5342                              size_int (xlr_bitpos));
5343       rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5344                                                             rntype, rr_mask),
5345                              size_int (xrr_bitpos));
5346
5347       /* Make a mask that corresponds to both fields being compared.
5348          Do this for both items being compared.  If the operands are the
5349          same size and the bits being compared are in the same position
5350          then we can do this by masking both and comparing the masked
5351          results.  */
5352       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5353       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5354       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5355         {
5356           lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5357                                     ll_unsignedp || rl_unsignedp);
5358           if (! all_ones_mask_p (ll_mask, lnbitsize))
5359             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5360
5361           rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5362                                     lr_unsignedp || rr_unsignedp);
5363           if (! all_ones_mask_p (lr_mask, rnbitsize))
5364             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5365
5366           return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5367         }
5368
5369       /* There is still another way we can do something:  If both pairs of
5370          fields being compared are adjacent, we may be able to make a wider
5371          field containing them both.
5372
5373          Note that we still must mask the lhs/rhs expressions.  Furthermore,
5374          the mask must be shifted to account for the shift done by
5375          make_bit_field_ref.  */
5376       if ((ll_bitsize + ll_bitpos == rl_bitpos
5377            && lr_bitsize + lr_bitpos == rr_bitpos)
5378           || (ll_bitpos == rl_bitpos + rl_bitsize
5379               && lr_bitpos == rr_bitpos + rr_bitsize))
5380         {
5381           tree type;
5382
5383           lhs = make_bit_field_ref (loc, ll_inner, lntype,
5384                                     ll_bitsize + rl_bitsize,
5385                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5386           rhs = make_bit_field_ref (loc, lr_inner, rntype,
5387                                     lr_bitsize + rr_bitsize,
5388                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5389
5390           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5391                                  size_int (MIN (xll_bitpos, xrl_bitpos)));
5392           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5393                                  size_int (MIN (xlr_bitpos, xrr_bitpos)));
5394
5395           /* Convert to the smaller type before masking out unwanted bits.  */
5396           type = lntype;
5397           if (lntype != rntype)
5398             {
5399               if (lnbitsize > rnbitsize)
5400                 {
5401                   lhs = fold_convert_loc (loc, rntype, lhs);
5402                   ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5403                   type = rntype;
5404                 }
5405               else if (lnbitsize < rnbitsize)
5406                 {
5407                   rhs = fold_convert_loc (loc, lntype, rhs);
5408                   lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5409                   type = lntype;
5410                 }
5411             }
5412
5413           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5414             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5415
5416           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5417             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5418
5419           return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5420         }
5421
5422       return 0;
5423     }
5424
5425   /* Handle the case of comparisons with constants.  If there is something in
5426      common between the masks, those bits of the constants must be the same.
5427      If not, the condition is always false.  Test for this to avoid generating
5428      incorrect code below.  */
5429   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5430   if (! integer_zerop (result)
5431       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5432                            const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5433     {
5434       if (wanted_code == NE_EXPR)
5435         {
5436           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5437           return constant_boolean_node (true, truth_type);
5438         }
5439       else
5440         {
5441           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5442           return constant_boolean_node (false, truth_type);
5443         }
5444     }
5445
5446   /* Construct the expression we will return.  First get the component
5447      reference we will make.  Unless the mask is all ones the width of
5448      that field, perform the mask operation.  Then compare with the
5449      merged constant.  */
5450   result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5451                                ll_unsignedp || rl_unsignedp);
5452
5453   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5454   if (! all_ones_mask_p (ll_mask, lnbitsize))
5455     result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5456
5457   return build2_loc (loc, wanted_code, truth_type, result,
5458                      const_binop (BIT_IOR_EXPR, l_const, r_const));
5459 }
5460 \f
5461 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5462    constant.  */
5463
5464 static tree
5465 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5466                             tree op0, tree op1)
5467 {
5468   tree arg0 = op0;
5469   enum tree_code op_code;
5470   tree comp_const;
5471   tree minmax_const;
5472   int consts_equal, consts_lt;
5473   tree inner;
5474
5475   STRIP_SIGN_NOPS (arg0);
5476
5477   op_code = TREE_CODE (arg0);
5478   minmax_const = TREE_OPERAND (arg0, 1);
5479   comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5480   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5481   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5482   inner = TREE_OPERAND (arg0, 0);
5483
5484   /* If something does not permit us to optimize, return the original tree.  */
5485   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5486       || TREE_CODE (comp_const) != INTEGER_CST
5487       || TREE_OVERFLOW (comp_const)
5488       || TREE_CODE (minmax_const) != INTEGER_CST
5489       || TREE_OVERFLOW (minmax_const))
5490     return NULL_TREE;
5491
5492   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5493      and GT_EXPR, doing the rest with recursive calls using logical
5494      simplifications.  */
5495   switch (code)
5496     {
5497     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5498       {
5499         tree tem
5500           = optimize_minmax_comparison (loc,
5501                                         invert_tree_comparison (code, false),
5502                                         type, op0, op1);
5503         if (tem)
5504           return invert_truthvalue_loc (loc, tem);
5505         return NULL_TREE;
5506       }
5507
5508     case GE_EXPR:
5509       return
5510         fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5511                      optimize_minmax_comparison
5512                      (loc, EQ_EXPR, type, arg0, comp_const),
5513                      optimize_minmax_comparison
5514                      (loc, GT_EXPR, type, arg0, comp_const));
5515
5516     case EQ_EXPR:
5517       if (op_code == MAX_EXPR && consts_equal)
5518         /* MAX (X, 0) == 0  ->  X <= 0  */
5519         return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5520
5521       else if (op_code == MAX_EXPR && consts_lt)
5522         /* MAX (X, 0) == 5  ->  X == 5   */
5523         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5524
5525       else if (op_code == MAX_EXPR)
5526         /* MAX (X, 0) == -1  ->  false  */
5527         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5528
5529       else if (consts_equal)
5530         /* MIN (X, 0) == 0  ->  X >= 0  */
5531         return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5532
5533       else if (consts_lt)
5534         /* MIN (X, 0) == 5  ->  false  */
5535         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5536
5537       else
5538         /* MIN (X, 0) == -1  ->  X == -1  */
5539         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5540
5541     case GT_EXPR:
5542       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5543         /* MAX (X, 0) > 0  ->  X > 0
5544            MAX (X, 0) > 5  ->  X > 5  */
5545         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5546
5547       else if (op_code == MAX_EXPR)
5548         /* MAX (X, 0) > -1  ->  true  */
5549         return omit_one_operand_loc (loc, type, integer_one_node, inner);
5550
5551       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5552         /* MIN (X, 0) > 0  ->  false
5553            MIN (X, 0) > 5  ->  false  */
5554         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5555
5556       else
5557         /* MIN (X, 0) > -1  ->  X > -1  */
5558         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5559
5560     default:
5561       return NULL_TREE;
5562     }
5563 }
5564 \f
5565 /* T is an integer expression that is being multiplied, divided, or taken a
5566    modulus (CODE says which and what kind of divide or modulus) by a
5567    constant C.  See if we can eliminate that operation by folding it with
5568    other operations already in T.  WIDE_TYPE, if non-null, is a type that
5569    should be used for the computation if wider than our type.
5570
5571    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5572    (X * 2) + (Y * 4).  We must, however, be assured that either the original
5573    expression would not overflow or that overflow is undefined for the type
5574    in the language in question.
5575
5576    If we return a non-null expression, it is an equivalent form of the
5577    original computation, but need not be in the original type.
5578
5579    We set *STRICT_OVERFLOW_P to true if the return values depends on
5580    signed overflow being undefined.  Otherwise we do not change
5581    *STRICT_OVERFLOW_P.  */
5582
5583 static tree
5584 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5585                 bool *strict_overflow_p)
5586 {
5587   /* To avoid exponential search depth, refuse to allow recursion past
5588      three levels.  Beyond that (1) it's highly unlikely that we'll find
5589      something interesting and (2) we've probably processed it before
5590      when we built the inner expression.  */
5591
5592   static int depth;
5593   tree ret;
5594
5595   if (depth > 3)
5596     return NULL;
5597
5598   depth++;
5599   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5600   depth--;
5601
5602   return ret;
5603 }
5604
5605 static tree
5606 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5607                   bool *strict_overflow_p)
5608 {
5609   tree type = TREE_TYPE (t);
5610   enum tree_code tcode = TREE_CODE (t);
5611   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5612                                    > GET_MODE_SIZE (TYPE_MODE (type)))
5613                 ? wide_type : type);
5614   tree t1, t2;
5615   int same_p = tcode == code;
5616   tree op0 = NULL_TREE, op1 = NULL_TREE;
5617   bool sub_strict_overflow_p;
5618
5619   /* Don't deal with constants of zero here; they confuse the code below.  */
5620   if (integer_zerop (c))
5621     return NULL_TREE;
5622
5623   if (TREE_CODE_CLASS (tcode) == tcc_unary)
5624     op0 = TREE_OPERAND (t, 0);
5625
5626   if (TREE_CODE_CLASS (tcode) == tcc_binary)
5627     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5628
5629   /* Note that we need not handle conditional operations here since fold
5630      already handles those cases.  So just do arithmetic here.  */
5631   switch (tcode)
5632     {
5633     case INTEGER_CST:
5634       /* For a constant, we can always simplify if we are a multiply
5635          or (for divide and modulus) if it is a multiple of our constant.  */
5636       if (code == MULT_EXPR
5637           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c)))
5638         return const_binop (code, fold_convert (ctype, t),
5639                             fold_convert (ctype, c));
5640       break;
5641
5642     CASE_CONVERT: case NON_LVALUE_EXPR:
5643       /* If op0 is an expression ...  */
5644       if ((COMPARISON_CLASS_P (op0)
5645            || UNARY_CLASS_P (op0)
5646            || BINARY_CLASS_P (op0)
5647            || VL_EXP_CLASS_P (op0)
5648            || EXPRESSION_CLASS_P (op0))
5649           /* ... and has wrapping overflow, and its type is smaller
5650              than ctype, then we cannot pass through as widening.  */
5651           && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
5652                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5653                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5654                && (TYPE_PRECISION (ctype)
5655                    > TYPE_PRECISION (TREE_TYPE (op0))))
5656               /* ... or this is a truncation (t is narrower than op0),
5657                  then we cannot pass through this narrowing.  */
5658               || (TYPE_PRECISION (type)
5659                   < TYPE_PRECISION (TREE_TYPE (op0)))
5660               /* ... or signedness changes for division or modulus,
5661                  then we cannot pass through this conversion.  */
5662               || (code != MULT_EXPR
5663                   && (TYPE_UNSIGNED (ctype)
5664                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
5665               /* ... or has undefined overflow while the converted to
5666                  type has not, we cannot do the operation in the inner type
5667                  as that would introduce undefined overflow.  */
5668               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5669                   && !TYPE_OVERFLOW_UNDEFINED (type))))
5670         break;
5671
5672       /* Pass the constant down and see if we can make a simplification.  If
5673          we can, replace this expression with the inner simplification for
5674          possible later conversion to our or some other type.  */
5675       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5676           && TREE_CODE (t2) == INTEGER_CST
5677           && !TREE_OVERFLOW (t2)
5678           && (0 != (t1 = extract_muldiv (op0, t2, code,
5679                                          code == MULT_EXPR
5680                                          ? ctype : NULL_TREE,
5681                                          strict_overflow_p))))
5682         return t1;
5683       break;
5684
5685     case ABS_EXPR:
5686       /* If widening the type changes it from signed to unsigned, then we
5687          must avoid building ABS_EXPR itself as unsigned.  */
5688       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5689         {
5690           tree cstype = (*signed_type_for) (ctype);
5691           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5692               != 0)
5693             {
5694               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5695               return fold_convert (ctype, t1);
5696             }
5697           break;
5698         }
5699       /* If the constant is negative, we cannot simplify this.  */
5700       if (tree_int_cst_sgn (c) == -1)
5701         break;
5702       /* FALLTHROUGH */
5703     case NEGATE_EXPR:
5704       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5705           != 0)
5706         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5707       break;
5708
5709     case MIN_EXPR:  case MAX_EXPR:
5710       /* If widening the type changes the signedness, then we can't perform
5711          this optimization as that changes the result.  */
5712       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5713         break;
5714
5715       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
5716       sub_strict_overflow_p = false;
5717       if ((t1 = extract_muldiv (op0, c, code, wide_type,
5718                                 &sub_strict_overflow_p)) != 0
5719           && (t2 = extract_muldiv (op1, c, code, wide_type,
5720                                    &sub_strict_overflow_p)) != 0)
5721         {
5722           if (tree_int_cst_sgn (c) < 0)
5723             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5724           if (sub_strict_overflow_p)
5725             *strict_overflow_p = true;
5726           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5727                               fold_convert (ctype, t2));
5728         }
5729       break;
5730
5731     case LSHIFT_EXPR:  case RSHIFT_EXPR:
5732       /* If the second operand is constant, this is a multiplication
5733          or floor division, by a power of two, so we can treat it that
5734          way unless the multiplier or divisor overflows.  Signed
5735          left-shift overflow is implementation-defined rather than
5736          undefined in C90, so do not convert signed left shift into
5737          multiplication.  */
5738       if (TREE_CODE (op1) == INTEGER_CST
5739           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5740           /* const_binop may not detect overflow correctly,
5741              so check for it explicitly here.  */
5742           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5743           && TREE_INT_CST_HIGH (op1) == 0
5744           && 0 != (t1 = fold_convert (ctype,
5745                                       const_binop (LSHIFT_EXPR,
5746                                                    size_one_node,
5747                                                    op1)))
5748           && !TREE_OVERFLOW (t1))
5749         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5750                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
5751                                        ctype,
5752                                        fold_convert (ctype, op0),
5753                                        t1),
5754                                c, code, wide_type, strict_overflow_p);
5755       break;
5756
5757     case PLUS_EXPR:  case MINUS_EXPR:
5758       /* See if we can eliminate the operation on both sides.  If we can, we
5759          can return a new PLUS or MINUS.  If we can't, the only remaining
5760          cases where we can do anything are if the second operand is a
5761          constant.  */
5762       sub_strict_overflow_p = false;
5763       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5764       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5765       if (t1 != 0 && t2 != 0
5766           && (code == MULT_EXPR
5767               /* If not multiplication, we can only do this if both operands
5768                  are divisible by c.  */
5769               || (multiple_of_p (ctype, op0, c)
5770                   && multiple_of_p (ctype, op1, c))))
5771         {
5772           if (sub_strict_overflow_p)
5773             *strict_overflow_p = true;
5774           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5775                               fold_convert (ctype, t2));
5776         }
5777
5778       /* If this was a subtraction, negate OP1 and set it to be an addition.
5779          This simplifies the logic below.  */
5780       if (tcode == MINUS_EXPR)
5781         {
5782           tcode = PLUS_EXPR, op1 = negate_expr (op1);
5783           /* If OP1 was not easily negatable, the constant may be OP0.  */
5784           if (TREE_CODE (op0) == INTEGER_CST)
5785             {
5786               tree tem = op0;
5787               op0 = op1;
5788               op1 = tem;
5789               tem = t1;
5790               t1 = t2;
5791               t2 = tem;
5792             }
5793         }
5794
5795       if (TREE_CODE (op1) != INTEGER_CST)
5796         break;
5797
5798       /* If either OP1 or C are negative, this optimization is not safe for
5799          some of the division and remainder types while for others we need
5800          to change the code.  */
5801       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5802         {
5803           if (code == CEIL_DIV_EXPR)
5804             code = FLOOR_DIV_EXPR;
5805           else if (code == FLOOR_DIV_EXPR)
5806             code = CEIL_DIV_EXPR;
5807           else if (code != MULT_EXPR
5808                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5809             break;
5810         }
5811
5812       /* If it's a multiply or a division/modulus operation of a multiple
5813          of our constant, do the operation and verify it doesn't overflow.  */
5814       if (code == MULT_EXPR
5815           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5816         {
5817           op1 = const_binop (code, fold_convert (ctype, op1),
5818                              fold_convert (ctype, c));
5819           /* We allow the constant to overflow with wrapping semantics.  */
5820           if (op1 == 0
5821               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5822             break;
5823         }
5824       else
5825         break;
5826
5827       /* If we have an unsigned type is not a sizetype, we cannot widen
5828          the operation since it will change the result if the original
5829          computation overflowed.  */
5830       if (TYPE_UNSIGNED (ctype)
5831           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5832           && ctype != type)
5833         break;
5834
5835       /* If we were able to eliminate our operation from the first side,
5836          apply our operation to the second side and reform the PLUS.  */
5837       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5838         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5839
5840       /* The last case is if we are a multiply.  In that case, we can
5841          apply the distributive law to commute the multiply and addition
5842          if the multiplication of the constants doesn't overflow.  */
5843       if (code == MULT_EXPR)
5844         return fold_build2 (tcode, ctype,
5845                             fold_build2 (code, ctype,
5846                                          fold_convert (ctype, op0),
5847                                          fold_convert (ctype, c)),
5848                             op1);
5849
5850       break;
5851
5852     case MULT_EXPR:
5853       /* We have a special case here if we are doing something like
5854          (C * 8) % 4 since we know that's zero.  */
5855       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5856            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5857           /* If the multiplication can overflow we cannot optimize this.
5858              ???  Until we can properly mark individual operations as
5859              not overflowing we need to treat sizetype special here as
5860              stor-layout relies on this opimization to make
5861              DECL_FIELD_BIT_OFFSET always a constant.  */
5862           && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
5863               || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
5864                   && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
5865           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5866           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5867         {
5868           *strict_overflow_p = true;
5869           return omit_one_operand (type, integer_zero_node, op0);
5870         }
5871
5872       /* ... fall through ...  */
5873
5874     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
5875     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
5876       /* If we can extract our operation from the LHS, do so and return a
5877          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
5878          do something only if the second operand is a constant.  */
5879       if (same_p
5880           && (t1 = extract_muldiv (op0, c, code, wide_type,
5881                                    strict_overflow_p)) != 0)
5882         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5883                             fold_convert (ctype, op1));
5884       else if (tcode == MULT_EXPR && code == MULT_EXPR
5885                && (t1 = extract_muldiv (op1, c, code, wide_type,
5886                                         strict_overflow_p)) != 0)
5887         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5888                             fold_convert (ctype, t1));
5889       else if (TREE_CODE (op1) != INTEGER_CST)
5890         return 0;
5891
5892       /* If these are the same operation types, we can associate them
5893          assuming no overflow.  */
5894       if (tcode == code)
5895         {
5896           double_int mul;
5897           int overflow_p;
5898           mul = double_int_mul_with_sign
5899                   (double_int_ext
5900                      (tree_to_double_int (op1),
5901                       TYPE_PRECISION (ctype), TYPE_UNSIGNED (ctype)),
5902                    double_int_ext
5903                      (tree_to_double_int (c),
5904                       TYPE_PRECISION (ctype), TYPE_UNSIGNED (ctype)),
5905                    false, &overflow_p);
5906           overflow_p = (((!TYPE_UNSIGNED (ctype)
5907                           || (TREE_CODE (ctype) == INTEGER_TYPE
5908                               && TYPE_IS_SIZETYPE (ctype)))
5909                          && overflow_p)
5910                         | TREE_OVERFLOW (c) | TREE_OVERFLOW (op1));
5911           if (!double_int_fits_to_tree_p (ctype, mul)
5912               && ((TYPE_UNSIGNED (ctype) && tcode != MULT_EXPR)
5913                   || !TYPE_UNSIGNED (ctype)
5914                   || (TREE_CODE (ctype) == INTEGER_TYPE
5915                       && TYPE_IS_SIZETYPE (ctype))))
5916             overflow_p = 1;
5917           if (!overflow_p)
5918             return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5919                                 double_int_to_tree (ctype, mul));
5920         }
5921
5922       /* If these operations "cancel" each other, we have the main
5923          optimizations of this pass, which occur when either constant is a
5924          multiple of the other, in which case we replace this with either an
5925          operation or CODE or TCODE.
5926
5927          If we have an unsigned type that is not a sizetype, we cannot do
5928          this since it will change the result if the original computation
5929          overflowed.  */
5930       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
5931            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5932           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5933               || (tcode == MULT_EXPR
5934                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5935                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
5936                   && code != MULT_EXPR)))
5937         {
5938           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5939             {
5940               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5941                 *strict_overflow_p = true;
5942               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5943                                   fold_convert (ctype,
5944                                                 const_binop (TRUNC_DIV_EXPR,
5945                                                              op1, c)));
5946             }
5947           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1)))
5948             {
5949               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5950                 *strict_overflow_p = true;
5951               return fold_build2 (code, ctype, fold_convert (ctype, op0),
5952                                   fold_convert (ctype,
5953                                                 const_binop (TRUNC_DIV_EXPR,
5954                                                              c, op1)));
5955             }
5956         }
5957       break;
5958
5959     default:
5960       break;
5961     }
5962
5963   return 0;
5964 }
5965 \f
5966 /* Return a node which has the indicated constant VALUE (either 0 or
5967    1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
5968    and is of the indicated TYPE.  */
5969
5970 tree
5971 constant_boolean_node (bool value, tree type)
5972 {
5973   if (type == integer_type_node)
5974     return value ? integer_one_node : integer_zero_node;
5975   else if (type == boolean_type_node)
5976     return value ? boolean_true_node : boolean_false_node;
5977   else if (TREE_CODE (type) == VECTOR_TYPE)
5978     return build_vector_from_val (type,
5979                                   build_int_cst (TREE_TYPE (type),
5980                                                  value ? -1 : 0));
5981   else
5982     return fold_convert (type, value ? integer_one_node : integer_zero_node);
5983 }
5984
5985
5986 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
5987    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
5988    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
5989    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
5990    COND is the first argument to CODE; otherwise (as in the example
5991    given here), it is the second argument.  TYPE is the type of the
5992    original expression.  Return NULL_TREE if no simplification is
5993    possible.  */
5994
5995 static tree
5996 fold_binary_op_with_conditional_arg (location_t loc,
5997                                      enum tree_code code,
5998                                      tree type, tree op0, tree op1,
5999                                      tree cond, tree arg, int cond_first_p)
6000 {
6001   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6002   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6003   tree test, true_value, false_value;
6004   tree lhs = NULL_TREE;
6005   tree rhs = NULL_TREE;
6006
6007   if (TREE_CODE (cond) == COND_EXPR)
6008     {
6009       test = TREE_OPERAND (cond, 0);
6010       true_value = TREE_OPERAND (cond, 1);
6011       false_value = TREE_OPERAND (cond, 2);
6012       /* If this operand throws an expression, then it does not make
6013          sense to try to perform a logical or arithmetic operation
6014          involving it.  */
6015       if (VOID_TYPE_P (TREE_TYPE (true_value)))
6016         lhs = true_value;
6017       if (VOID_TYPE_P (TREE_TYPE (false_value)))
6018         rhs = false_value;
6019     }
6020   else
6021     {
6022       tree testtype = TREE_TYPE (cond);
6023       test = cond;
6024       true_value = constant_boolean_node (true, testtype);
6025       false_value = constant_boolean_node (false, testtype);
6026     }
6027
6028   /* This transformation is only worthwhile if we don't have to wrap ARG
6029      in a SAVE_EXPR and the operation can be simplified on at least one
6030      of the branches once its pushed inside the COND_EXPR.  */
6031   if (!TREE_CONSTANT (arg)
6032       && (TREE_SIDE_EFFECTS (arg)
6033           || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6034     return NULL_TREE;
6035
6036   arg = fold_convert_loc (loc, arg_type, arg);
6037   if (lhs == 0)
6038     {
6039       true_value = fold_convert_loc (loc, cond_type, true_value);
6040       if (cond_first_p)
6041         lhs = fold_build2_loc (loc, code, type, true_value, arg);
6042       else
6043         lhs = fold_build2_loc (loc, code, type, arg, true_value);
6044     }
6045   if (rhs == 0)
6046     {
6047       false_value = fold_convert_loc (loc, cond_type, false_value);
6048       if (cond_first_p)
6049         rhs = fold_build2_loc (loc, code, type, false_value, arg);
6050       else
6051         rhs = fold_build2_loc (loc, code, type, arg, false_value);
6052     }
6053
6054   /* Check that we have simplified at least one of the branches.  */
6055   if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6056     return NULL_TREE;
6057
6058   return fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs);
6059 }
6060
6061 \f
6062 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6063
6064    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6065    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6066    ADDEND is the same as X.
6067
6068    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6069    and finite.  The problematic cases are when X is zero, and its mode
6070    has signed zeros.  In the case of rounding towards -infinity,
6071    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6072    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6073
6074 bool
6075 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6076 {
6077   if (!real_zerop (addend))
6078     return false;
6079
6080   /* Don't allow the fold with -fsignaling-nans.  */
6081   if (HONOR_SNANS (TYPE_MODE (type)))
6082     return false;
6083
6084   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6085   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6086     return true;
6087
6088   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6089   if (TREE_CODE (addend) == REAL_CST
6090       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6091     negate = !negate;
6092
6093   /* The mode has signed zeros, and we have to honor their sign.
6094      In this situation, there is only one case we can return true for.
6095      X - 0 is the same as X unless rounding towards -infinity is
6096      supported.  */
6097   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6098 }
6099
6100 /* Subroutine of fold() that checks comparisons of built-in math
6101    functions against real constants.
6102
6103    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6104    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6105    is the type of the result and ARG0 and ARG1 are the operands of the
6106    comparison.  ARG1 must be a TREE_REAL_CST.
6107
6108    The function returns the constant folded tree if a simplification
6109    can be made, and NULL_TREE otherwise.  */
6110
6111 static tree
6112 fold_mathfn_compare (location_t loc,
6113                      enum built_in_function fcode, enum tree_code code,
6114                      tree type, tree arg0, tree arg1)
6115 {
6116   REAL_VALUE_TYPE c;
6117
6118   if (BUILTIN_SQRT_P (fcode))
6119     {
6120       tree arg = CALL_EXPR_ARG (arg0, 0);
6121       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6122
6123       c = TREE_REAL_CST (arg1);
6124       if (REAL_VALUE_NEGATIVE (c))
6125         {
6126           /* sqrt(x) < y is always false, if y is negative.  */
6127           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6128             return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6129
6130           /* sqrt(x) > y is always true, if y is negative and we
6131              don't care about NaNs, i.e. negative values of x.  */
6132           if (code == NE_EXPR || !HONOR_NANS (mode))
6133             return omit_one_operand_loc (loc, type, integer_one_node, arg);
6134
6135           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6136           return fold_build2_loc (loc, GE_EXPR, type, arg,
6137                               build_real (TREE_TYPE (arg), dconst0));
6138         }
6139       else if (code == GT_EXPR || code == GE_EXPR)
6140         {
6141           REAL_VALUE_TYPE c2;
6142
6143           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6144           real_convert (&c2, mode, &c2);
6145
6146           if (REAL_VALUE_ISINF (c2))
6147             {
6148               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6149               if (HONOR_INFINITIES (mode))
6150                 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6151                                     build_real (TREE_TYPE (arg), c2));
6152
6153               /* sqrt(x) > y is always false, when y is very large
6154                  and we don't care about infinities.  */
6155               return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6156             }
6157
6158           /* sqrt(x) > c is the same as x > c*c.  */
6159           return fold_build2_loc (loc, code, type, arg,
6160                               build_real (TREE_TYPE (arg), c2));
6161         }
6162       else if (code == LT_EXPR || code == LE_EXPR)
6163         {
6164           REAL_VALUE_TYPE c2;
6165
6166           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6167           real_convert (&c2, mode, &c2);
6168
6169           if (REAL_VALUE_ISINF (c2))
6170             {
6171               /* sqrt(x) < y is always true, when y is a very large
6172                  value and we don't care about NaNs or Infinities.  */
6173               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6174                 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6175
6176               /* sqrt(x) < y is x != +Inf when y is very large and we
6177                  don't care about NaNs.  */
6178               if (! HONOR_NANS (mode))
6179                 return fold_build2_loc (loc, NE_EXPR, type, arg,
6180                                     build_real (TREE_TYPE (arg), c2));
6181
6182               /* sqrt(x) < y is x >= 0 when y is very large and we
6183                  don't care about Infinities.  */
6184               if (! HONOR_INFINITIES (mode))
6185                 return fold_build2_loc (loc, GE_EXPR, type, arg,
6186                                     build_real (TREE_TYPE (arg), dconst0));
6187
6188               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6189               arg = save_expr (arg);
6190               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6191                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6192                                                build_real (TREE_TYPE (arg),
6193                                                            dconst0)),
6194                                   fold_build2_loc (loc, NE_EXPR, type, arg,
6195                                                build_real (TREE_TYPE (arg),
6196                                                            c2)));
6197             }
6198
6199           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6200           if (! HONOR_NANS (mode))
6201             return fold_build2_loc (loc, code, type, arg,
6202                                 build_real (TREE_TYPE (arg), c2));
6203
6204           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6205           arg = save_expr (arg);
6206           return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6207                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6208                                                build_real (TREE_TYPE (arg),
6209                                                            dconst0)),
6210                                   fold_build2_loc (loc, code, type, arg,
6211                                                build_real (TREE_TYPE (arg),
6212                                                            c2)));
6213         }
6214     }
6215
6216   return NULL_TREE;
6217 }
6218
6219 /* Subroutine of fold() that optimizes comparisons against Infinities,
6220    either +Inf or -Inf.
6221
6222    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6223    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6224    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6225
6226    The function returns the constant folded tree if a simplification
6227    can be made, and NULL_TREE otherwise.  */
6228
6229 static tree
6230 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6231                   tree arg0, tree arg1)
6232 {
6233   enum machine_mode mode;
6234   REAL_VALUE_TYPE max;
6235   tree temp;
6236   bool neg;
6237
6238   mode = TYPE_MODE (TREE_TYPE (arg0));
6239
6240   /* For negative infinity swap the sense of the comparison.  */
6241   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6242   if (neg)
6243     code = swap_tree_comparison (code);
6244
6245   switch (code)
6246     {
6247     case GT_EXPR:
6248       /* x > +Inf is always false, if with ignore sNANs.  */
6249       if (HONOR_SNANS (mode))
6250         return NULL_TREE;
6251       return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6252
6253     case LE_EXPR:
6254       /* x <= +Inf is always true, if we don't case about NaNs.  */
6255       if (! HONOR_NANS (mode))
6256         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6257
6258       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6259       arg0 = save_expr (arg0);
6260       return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6261
6262     case EQ_EXPR:
6263     case GE_EXPR:
6264       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6265       real_maxval (&max, neg, mode);
6266       return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6267                           arg0, build_real (TREE_TYPE (arg0), max));
6268
6269     case LT_EXPR:
6270       /* x < +Inf is always equal to x <= DBL_MAX.  */
6271       real_maxval (&max, neg, mode);
6272       return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6273                           arg0, build_real (TREE_TYPE (arg0), max));
6274
6275     case NE_EXPR:
6276       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6277       real_maxval (&max, neg, mode);
6278       if (! HONOR_NANS (mode))
6279         return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6280                             arg0, build_real (TREE_TYPE (arg0), max));
6281
6282       temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6283                           arg0, build_real (TREE_TYPE (arg0), max));
6284       return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
6285
6286     default:
6287       break;
6288     }
6289
6290   return NULL_TREE;
6291 }
6292
6293 /* Subroutine of fold() that optimizes comparisons of a division by
6294    a nonzero integer constant against an integer constant, i.e.
6295    X/C1 op C2.
6296
6297    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6298    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6299    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6300
6301    The function returns the constant folded tree if a simplification
6302    can be made, and NULL_TREE otherwise.  */
6303
6304 static tree
6305 fold_div_compare (location_t loc,
6306                   enum tree_code code, tree type, tree arg0, tree arg1)
6307 {
6308   tree prod, tmp, hi, lo;
6309   tree arg00 = TREE_OPERAND (arg0, 0);
6310   tree arg01 = TREE_OPERAND (arg0, 1);
6311   double_int val;
6312   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6313   bool neg_overflow;
6314   int overflow;
6315
6316   /* We have to do this the hard way to detect unsigned overflow.
6317      prod = int_const_binop (MULT_EXPR, arg01, arg1);  */
6318   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6319                                    TREE_INT_CST_HIGH (arg01),
6320                                    TREE_INT_CST_LOW (arg1),
6321                                    TREE_INT_CST_HIGH (arg1),
6322                                    &val.low, &val.high, unsigned_p);
6323   prod = force_fit_type_double (TREE_TYPE (arg00), val, -1, overflow);
6324   neg_overflow = false;
6325
6326   if (unsigned_p)
6327     {
6328       tmp = int_const_binop (MINUS_EXPR, arg01,
6329                              build_int_cst (TREE_TYPE (arg01), 1));
6330       lo = prod;
6331
6332       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp).  */
6333       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6334                                        TREE_INT_CST_HIGH (prod),
6335                                        TREE_INT_CST_LOW (tmp),
6336                                        TREE_INT_CST_HIGH (tmp),
6337                                        &val.low, &val.high, unsigned_p);
6338       hi = force_fit_type_double (TREE_TYPE (arg00), val,
6339                                   -1, overflow | TREE_OVERFLOW (prod));
6340     }
6341   else if (tree_int_cst_sgn (arg01) >= 0)
6342     {
6343       tmp = int_const_binop (MINUS_EXPR, arg01,
6344                              build_int_cst (TREE_TYPE (arg01), 1));
6345       switch (tree_int_cst_sgn (arg1))
6346         {
6347         case -1:
6348           neg_overflow = true;
6349           lo = int_const_binop (MINUS_EXPR, prod, tmp);
6350           hi = prod;
6351           break;
6352
6353         case  0:
6354           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6355           hi = tmp;
6356           break;
6357
6358         case  1:
6359           hi = int_const_binop (PLUS_EXPR, prod, tmp);
6360           lo = prod;
6361           break;
6362
6363         default:
6364           gcc_unreachable ();
6365         }
6366     }
6367   else
6368     {
6369       /* A negative divisor reverses the relational operators.  */
6370       code = swap_tree_comparison (code);
6371
6372       tmp = int_const_binop (PLUS_EXPR, arg01,
6373                              build_int_cst (TREE_TYPE (arg01), 1));
6374       switch (tree_int_cst_sgn (arg1))
6375         {
6376         case -1:
6377           hi = int_const_binop (MINUS_EXPR, prod, tmp);
6378           lo = prod;
6379           break;
6380
6381         case  0:
6382           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6383           lo = tmp;
6384           break;
6385
6386         case  1:
6387           neg_overflow = true;
6388           lo = int_const_binop (PLUS_EXPR, prod, tmp);
6389           hi = prod;
6390           break;
6391
6392         default:
6393           gcc_unreachable ();
6394         }
6395     }
6396
6397   switch (code)
6398     {
6399     case EQ_EXPR:
6400       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6401         return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6402       if (TREE_OVERFLOW (hi))
6403         return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6404       if (TREE_OVERFLOW (lo))
6405         return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6406       return build_range_check (loc, type, arg00, 1, lo, hi);
6407
6408     case NE_EXPR:
6409       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6410         return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6411       if (TREE_OVERFLOW (hi))
6412         return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6413       if (TREE_OVERFLOW (lo))
6414         return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6415       return build_range_check (loc, type, arg00, 0, lo, hi);
6416
6417     case LT_EXPR:
6418       if (TREE_OVERFLOW (lo))
6419         {
6420           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6421           return omit_one_operand_loc (loc, type, tmp, arg00);
6422         }
6423       return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6424
6425     case LE_EXPR:
6426       if (TREE_OVERFLOW (hi))
6427         {
6428           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6429           return omit_one_operand_loc (loc, type, tmp, arg00);
6430         }
6431       return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6432
6433     case GT_EXPR:
6434       if (TREE_OVERFLOW (hi))
6435         {
6436           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6437           return omit_one_operand_loc (loc, type, tmp, arg00);
6438         }
6439       return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6440
6441     case GE_EXPR:
6442       if (TREE_OVERFLOW (lo))
6443         {
6444           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6445           return omit_one_operand_loc (loc, type, tmp, arg00);
6446         }
6447       return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6448
6449     default:
6450       break;
6451     }
6452
6453   return NULL_TREE;
6454 }
6455
6456
6457 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6458    equality/inequality test, then return a simplified form of the test
6459    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6460    result type.  */
6461
6462 static tree
6463 fold_single_bit_test_into_sign_test (location_t loc,
6464                                      enum tree_code code, tree arg0, tree arg1,
6465                                      tree result_type)
6466 {
6467   /* If this is testing a single bit, we can optimize the test.  */
6468   if ((code == NE_EXPR || code == EQ_EXPR)
6469       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6470       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6471     {
6472       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6473          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6474       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6475
6476       if (arg00 != NULL_TREE
6477           /* This is only a win if casting to a signed type is cheap,
6478              i.e. when arg00's type is not a partial mode.  */
6479           && TYPE_PRECISION (TREE_TYPE (arg00))
6480              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6481         {
6482           tree stype = signed_type_for (TREE_TYPE (arg00));
6483           return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6484                               result_type,
6485                               fold_convert_loc (loc, stype, arg00),
6486                               build_int_cst (stype, 0));
6487         }
6488     }
6489
6490   return NULL_TREE;
6491 }
6492
6493 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6494    equality/inequality test, then return a simplified form of
6495    the test using shifts and logical operations.  Otherwise return
6496    NULL.  TYPE is the desired result type.  */
6497
6498 tree
6499 fold_single_bit_test (location_t loc, enum tree_code code,
6500                       tree arg0, tree arg1, tree result_type)
6501 {
6502   /* If this is testing a single bit, we can optimize the test.  */
6503   if ((code == NE_EXPR || code == EQ_EXPR)
6504       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6505       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6506     {
6507       tree inner = TREE_OPERAND (arg0, 0);
6508       tree type = TREE_TYPE (arg0);
6509       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6510       enum machine_mode operand_mode = TYPE_MODE (type);
6511       int ops_unsigned;
6512       tree signed_type, unsigned_type, intermediate_type;
6513       tree tem, one;
6514
6515       /* First, see if we can fold the single bit test into a sign-bit
6516          test.  */
6517       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6518                                                  result_type);
6519       if (tem)
6520         return tem;
6521
6522       /* Otherwise we have (A & C) != 0 where C is a single bit,
6523          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6524          Similarly for (A & C) == 0.  */
6525
6526       /* If INNER is a right shift of a constant and it plus BITNUM does
6527          not overflow, adjust BITNUM and INNER.  */
6528       if (TREE_CODE (inner) == RSHIFT_EXPR
6529           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6530           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6531           && bitnum < TYPE_PRECISION (type)
6532           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6533                                    bitnum - TYPE_PRECISION (type)))
6534         {
6535           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6536           inner = TREE_OPERAND (inner, 0);
6537         }
6538
6539       /* If we are going to be able to omit the AND below, we must do our
6540          operations as unsigned.  If we must use the AND, we have a choice.
6541          Normally unsigned is faster, but for some machines signed is.  */
6542 #ifdef LOAD_EXTEND_OP
6543       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6544                       && !flag_syntax_only) ? 0 : 1;
6545 #else
6546       ops_unsigned = 1;
6547 #endif
6548
6549       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6550       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6551       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6552       inner = fold_convert_loc (loc, intermediate_type, inner);
6553
6554       if (bitnum != 0)
6555         inner = build2 (RSHIFT_EXPR, intermediate_type,
6556                         inner, size_int (bitnum));
6557
6558       one = build_int_cst (intermediate_type, 1);
6559
6560       if (code == EQ_EXPR)
6561         inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6562
6563       /* Put the AND last so it can combine with more things.  */
6564       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6565
6566       /* Make sure to return the proper type.  */
6567       inner = fold_convert_loc (loc, result_type, inner);
6568
6569       return inner;
6570     }
6571   return NULL_TREE;
6572 }
6573
6574 /* Check whether we are allowed to reorder operands arg0 and arg1,
6575    such that the evaluation of arg1 occurs before arg0.  */
6576
6577 static bool
6578 reorder_operands_p (const_tree arg0, const_tree arg1)
6579 {
6580   if (! flag_evaluation_order)
6581       return true;
6582   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6583     return true;
6584   return ! TREE_SIDE_EFFECTS (arg0)
6585          && ! TREE_SIDE_EFFECTS (arg1);
6586 }
6587
6588 /* Test whether it is preferable two swap two operands, ARG0 and
6589    ARG1, for example because ARG0 is an integer constant and ARG1
6590    isn't.  If REORDER is true, only recommend swapping if we can
6591    evaluate the operands in reverse order.  */
6592
6593 bool
6594 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6595 {
6596   STRIP_SIGN_NOPS (arg0);
6597   STRIP_SIGN_NOPS (arg1);
6598
6599   if (TREE_CODE (arg1) == INTEGER_CST)
6600     return 0;
6601   if (TREE_CODE (arg0) == INTEGER_CST)
6602     return 1;
6603
6604   if (TREE_CODE (arg1) == REAL_CST)
6605     return 0;
6606   if (TREE_CODE (arg0) == REAL_CST)
6607     return 1;
6608
6609   if (TREE_CODE (arg1) == FIXED_CST)
6610     return 0;
6611   if (TREE_CODE (arg0) == FIXED_CST)
6612     return 1;
6613
6614   if (TREE_CODE (arg1) == COMPLEX_CST)
6615     return 0;
6616   if (TREE_CODE (arg0) == COMPLEX_CST)
6617     return 1;
6618
6619   if (TREE_CONSTANT (arg1))
6620     return 0;
6621   if (TREE_CONSTANT (arg0))
6622     return 1;
6623
6624   if (optimize_function_for_size_p (cfun))
6625     return 0;
6626
6627   if (reorder && flag_evaluation_order
6628       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6629     return 0;
6630
6631   /* It is preferable to swap two SSA_NAME to ensure a canonical form
6632      for commutative and comparison operators.  Ensuring a canonical
6633      form allows the optimizers to find additional redundancies without
6634      having to explicitly check for both orderings.  */
6635   if (TREE_CODE (arg0) == SSA_NAME
6636       && TREE_CODE (arg1) == SSA_NAME
6637       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6638     return 1;
6639
6640   /* Put SSA_NAMEs last.  */
6641   if (TREE_CODE (arg1) == SSA_NAME)
6642     return 0;
6643   if (TREE_CODE (arg0) == SSA_NAME)
6644     return 1;
6645
6646   /* Put variables last.  */
6647   if (DECL_P (arg1))
6648     return 0;
6649   if (DECL_P (arg0))
6650     return 1;
6651
6652   return 0;
6653 }
6654
6655 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6656    ARG0 is extended to a wider type.  */
6657
6658 static tree
6659 fold_widened_comparison (location_t loc, enum tree_code code,
6660                          tree type, tree arg0, tree arg1)
6661 {
6662   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6663   tree arg1_unw;
6664   tree shorter_type, outer_type;
6665   tree min, max;
6666   bool above, below;
6667
6668   if (arg0_unw == arg0)
6669     return NULL_TREE;
6670   shorter_type = TREE_TYPE (arg0_unw);
6671
6672 #ifdef HAVE_canonicalize_funcptr_for_compare
6673   /* Disable this optimization if we're casting a function pointer
6674      type on targets that require function pointer canonicalization.  */
6675   if (HAVE_canonicalize_funcptr_for_compare
6676       && TREE_CODE (shorter_type) == POINTER_TYPE
6677       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6678     return NULL_TREE;
6679 #endif
6680
6681   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6682     return NULL_TREE;
6683
6684   arg1_unw = get_unwidened (arg1, NULL_TREE);
6685
6686   /* If possible, express the comparison in the shorter mode.  */
6687   if ((code == EQ_EXPR || code == NE_EXPR
6688        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6689       && (TREE_TYPE (arg1_unw) == shorter_type
6690           || ((TYPE_PRECISION (shorter_type)
6691                >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6692               && (TYPE_UNSIGNED (shorter_type)
6693                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6694           || (TREE_CODE (arg1_unw) == INTEGER_CST
6695               && (TREE_CODE (shorter_type) == INTEGER_TYPE
6696                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6697               && int_fits_type_p (arg1_unw, shorter_type))))
6698     return fold_build2_loc (loc, code, type, arg0_unw,
6699                         fold_convert_loc (loc, shorter_type, arg1_unw));
6700
6701   if (TREE_CODE (arg1_unw) != INTEGER_CST
6702       || TREE_CODE (shorter_type) != INTEGER_TYPE
6703       || !int_fits_type_p (arg1_unw, shorter_type))
6704     return NULL_TREE;
6705
6706   /* If we are comparing with the integer that does not fit into the range
6707      of the shorter type, the result is known.  */
6708   outer_type = TREE_TYPE (arg1_unw);
6709   min = lower_bound_in_type (outer_type, shorter_type);
6710   max = upper_bound_in_type (outer_type, shorter_type);
6711
6712   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6713                                                    max, arg1_unw));
6714   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6715                                                    arg1_unw, min));
6716
6717   switch (code)
6718     {
6719     case EQ_EXPR:
6720       if (above || below)
6721         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6722       break;
6723
6724     case NE_EXPR:
6725       if (above || below)
6726         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6727       break;
6728
6729     case LT_EXPR:
6730     case LE_EXPR:
6731       if (above)
6732         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6733       else if (below)
6734         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6735
6736     case GT_EXPR:
6737     case GE_EXPR:
6738       if (above)
6739         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6740       else if (below)
6741         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6742
6743     default:
6744       break;
6745     }
6746
6747   return NULL_TREE;
6748 }
6749
6750 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6751    ARG0 just the signedness is changed.  */
6752
6753 static tree
6754 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
6755                               tree arg0, tree arg1)
6756 {
6757   tree arg0_inner;
6758   tree inner_type, outer_type;
6759
6760   if (!CONVERT_EXPR_P (arg0))
6761     return NULL_TREE;
6762
6763   outer_type = TREE_TYPE (arg0);
6764   arg0_inner = TREE_OPERAND (arg0, 0);
6765   inner_type = TREE_TYPE (arg0_inner);
6766
6767 #ifdef HAVE_canonicalize_funcptr_for_compare
6768   /* Disable this optimization if we're casting a function pointer
6769      type on targets that require function pointer canonicalization.  */
6770   if (HAVE_canonicalize_funcptr_for_compare
6771       && TREE_CODE (inner_type) == POINTER_TYPE
6772       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6773     return NULL_TREE;
6774 #endif
6775
6776   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6777     return NULL_TREE;
6778
6779   if (TREE_CODE (arg1) != INTEGER_CST
6780       && !(CONVERT_EXPR_P (arg1)
6781            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6782     return NULL_TREE;
6783
6784   if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6785        || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
6786       && code != NE_EXPR
6787       && code != EQ_EXPR)
6788     return NULL_TREE;
6789
6790   if (TREE_CODE (arg1) == INTEGER_CST)
6791     arg1 = force_fit_type_double (inner_type, tree_to_double_int (arg1),
6792                                   0, TREE_OVERFLOW (arg1));
6793   else
6794     arg1 = fold_convert_loc (loc, inner_type, arg1);
6795
6796   return fold_build2_loc (loc, code, type, arg0_inner, arg1);
6797 }
6798
6799 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6800    step of the array.  Reconstructs s and delta in the case of s *
6801    delta being an integer constant (and thus already folded).  ADDR is
6802    the address. MULT is the multiplicative expression.  If the
6803    function succeeds, the new address expression is returned.
6804    Otherwise NULL_TREE is returned.  LOC is the location of the
6805    resulting expression.  */
6806
6807 static tree
6808 try_move_mult_to_index (location_t loc, tree addr, tree op1)
6809 {
6810   tree s, delta, step;
6811   tree ref = TREE_OPERAND (addr, 0), pref;
6812   tree ret, pos;
6813   tree itype;
6814   bool mdim = false;
6815
6816   /*  Strip the nops that might be added when converting op1 to sizetype. */
6817   STRIP_NOPS (op1);
6818
6819   /* Canonicalize op1 into a possibly non-constant delta
6820      and an INTEGER_CST s.  */
6821   if (TREE_CODE (op1) == MULT_EXPR)
6822     {
6823       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6824
6825       STRIP_NOPS (arg0);
6826       STRIP_NOPS (arg1);
6827
6828       if (TREE_CODE (arg0) == INTEGER_CST)
6829         {
6830           s = arg0;
6831           delta = arg1;
6832         }
6833       else if (TREE_CODE (arg1) == INTEGER_CST)
6834         {
6835           s = arg1;
6836           delta = arg0;
6837         }
6838       else
6839         return NULL_TREE;
6840     }
6841   else if (TREE_CODE (op1) == INTEGER_CST)
6842     {
6843       delta = op1;
6844       s = NULL_TREE;
6845     }
6846   else
6847     {
6848       /* Simulate we are delta * 1.  */
6849       delta = op1;
6850       s = integer_one_node;
6851     }
6852
6853   for (;; ref = TREE_OPERAND (ref, 0))
6854     {
6855       if (TREE_CODE (ref) == ARRAY_REF)
6856         {
6857           tree domain;
6858
6859           /* Remember if this was a multi-dimensional array.  */
6860           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6861             mdim = true;
6862
6863           domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6864           if (! domain)
6865             continue;
6866           itype = TREE_TYPE (domain);
6867
6868           step = array_ref_element_size (ref);
6869           if (TREE_CODE (step) != INTEGER_CST)
6870             continue;
6871
6872           if (s)
6873             {
6874               if (! tree_int_cst_equal (step, s))
6875                 continue;
6876             }
6877           else
6878             {
6879               /* Try if delta is a multiple of step.  */
6880               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6881               if (! tmp)
6882                 continue;
6883               delta = tmp;
6884             }
6885
6886           /* Only fold here if we can verify we do not overflow one
6887              dimension of a multi-dimensional array.  */
6888           if (mdim)
6889             {
6890               tree tmp;
6891
6892               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
6893                   || !TYPE_MAX_VALUE (domain)
6894                   || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
6895                 continue;
6896
6897               tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
6898                                      fold_convert_loc (loc, itype,
6899                                                        TREE_OPERAND (ref, 1)),
6900                                      fold_convert_loc (loc, itype, delta));
6901               if (!tmp
6902                   || TREE_CODE (tmp) != INTEGER_CST
6903                   || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
6904                 continue;
6905             }
6906
6907           break;
6908         }
6909       else if (TREE_CODE (ref) == COMPONENT_REF
6910                && TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE)
6911         {
6912           tree domain;
6913
6914           /* Remember if this was a multi-dimensional array.  */
6915           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6916             mdim = true;
6917
6918           domain = TYPE_DOMAIN (TREE_TYPE (ref));
6919           if (! domain)
6920             continue;
6921           itype = TREE_TYPE (domain);
6922
6923           step = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ref)));
6924           if (TREE_CODE (step) != INTEGER_CST)
6925             continue;
6926
6927           if (s)
6928             {
6929               if (! tree_int_cst_equal (step, s))
6930                 continue;
6931             }
6932           else
6933             {
6934               /* Try if delta is a multiple of step.  */
6935               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6936               if (! tmp)
6937                 continue;
6938               delta = tmp;
6939             }
6940
6941           /* Only fold here if we can verify we do not overflow one
6942              dimension of a multi-dimensional array.  */
6943           if (mdim)
6944             {
6945               tree tmp;
6946
6947               if (!TYPE_MIN_VALUE (domain)
6948                   || !TYPE_MAX_VALUE (domain)
6949                   || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
6950                 continue;
6951
6952               tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
6953                                      fold_convert_loc (loc, itype,
6954                                                        TYPE_MIN_VALUE (domain)),
6955                                      fold_convert_loc (loc, itype, delta));
6956               if (TREE_CODE (tmp) != INTEGER_CST
6957                   || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
6958                 continue;
6959             }
6960
6961           break;
6962         }
6963       else
6964         mdim = false;
6965
6966       if (!handled_component_p (ref))
6967         return NULL_TREE;
6968     }
6969
6970   /* We found the suitable array reference.  So copy everything up to it,
6971      and replace the index.  */
6972
6973   pref = TREE_OPERAND (addr, 0);
6974   ret = copy_node (pref);
6975   SET_EXPR_LOCATION (ret, loc);
6976   pos = ret;
6977
6978   while (pref != ref)
6979     {
6980       pref = TREE_OPERAND (pref, 0);
6981       TREE_OPERAND (pos, 0) = copy_node (pref);
6982       pos = TREE_OPERAND (pos, 0);
6983     }
6984
6985   if (TREE_CODE (ref) == ARRAY_REF)
6986     {
6987       TREE_OPERAND (pos, 1)
6988         = fold_build2_loc (loc, PLUS_EXPR, itype,
6989                            fold_convert_loc (loc, itype, TREE_OPERAND (pos, 1)),
6990                            fold_convert_loc (loc, itype, delta));
6991       return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
6992     }
6993   else if (TREE_CODE (ref) == COMPONENT_REF)
6994     {
6995       gcc_assert (ret == pos);
6996       ret = build4_loc (loc, ARRAY_REF, TREE_TYPE (TREE_TYPE (ref)), ret,
6997                         fold_build2_loc
6998                           (loc, PLUS_EXPR, itype,
6999                            fold_convert_loc (loc, itype,
7000                                              TYPE_MIN_VALUE
7001                                                (TYPE_DOMAIN (TREE_TYPE (ref)))),
7002                            fold_convert_loc (loc, itype, delta)),
7003                         NULL_TREE, NULL_TREE);
7004       return build_fold_addr_expr_loc (loc, ret);
7005     }
7006   else
7007     gcc_unreachable ();
7008 }
7009
7010
7011 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
7012    means A >= Y && A != MAX, but in this case we know that
7013    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
7014
7015 static tree
7016 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7017 {
7018   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7019
7020   if (TREE_CODE (bound) == LT_EXPR)
7021     a = TREE_OPERAND (bound, 0);
7022   else if (TREE_CODE (bound) == GT_EXPR)
7023     a = TREE_OPERAND (bound, 1);
7024   else
7025     return NULL_TREE;
7026
7027   typea = TREE_TYPE (a);
7028   if (!INTEGRAL_TYPE_P (typea)
7029       && !POINTER_TYPE_P (typea))
7030     return NULL_TREE;
7031
7032   if (TREE_CODE (ineq) == LT_EXPR)
7033     {
7034       a1 = TREE_OPERAND (ineq, 1);
7035       y = TREE_OPERAND (ineq, 0);
7036     }
7037   else if (TREE_CODE (ineq) == GT_EXPR)
7038     {
7039       a1 = TREE_OPERAND (ineq, 0);
7040       y = TREE_OPERAND (ineq, 1);
7041     }
7042   else
7043     return NULL_TREE;
7044
7045   if (TREE_TYPE (a1) != typea)
7046     return NULL_TREE;
7047
7048   if (POINTER_TYPE_P (typea))
7049     {
7050       /* Convert the pointer types into integer before taking the difference.  */
7051       tree ta = fold_convert_loc (loc, ssizetype, a);
7052       tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7053       diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7054     }
7055   else
7056     diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7057
7058   if (!diff || !integer_onep (diff))
7059    return NULL_TREE;
7060
7061   return fold_build2_loc (loc, GE_EXPR, type, a, y);
7062 }
7063
7064 /* Fold a sum or difference of at least one multiplication.
7065    Returns the folded tree or NULL if no simplification could be made.  */
7066
7067 static tree
7068 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7069                           tree arg0, tree arg1)
7070 {
7071   tree arg00, arg01, arg10, arg11;
7072   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7073
7074   /* (A * C) +- (B * C) -> (A+-B) * C.
7075      (A * C) +- A -> A * (C+-1).
7076      We are most concerned about the case where C is a constant,
7077      but other combinations show up during loop reduction.  Since
7078      it is not difficult, try all four possibilities.  */
7079
7080   if (TREE_CODE (arg0) == MULT_EXPR)
7081     {
7082       arg00 = TREE_OPERAND (arg0, 0);
7083       arg01 = TREE_OPERAND (arg0, 1);
7084     }
7085   else if (TREE_CODE (arg0) == INTEGER_CST)
7086     {
7087       arg00 = build_one_cst (type);
7088       arg01 = arg0;
7089     }
7090   else
7091     {
7092       /* We cannot generate constant 1 for fract.  */
7093       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7094         return NULL_TREE;
7095       arg00 = arg0;
7096       arg01 = build_one_cst (type);
7097     }
7098   if (TREE_CODE (arg1) == MULT_EXPR)
7099     {
7100       arg10 = TREE_OPERAND (arg1, 0);
7101       arg11 = TREE_OPERAND (arg1, 1);
7102     }
7103   else if (TREE_CODE (arg1) == INTEGER_CST)
7104     {
7105       arg10 = build_one_cst (type);
7106       /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7107          the purpose of this canonicalization.  */
7108       if (TREE_INT_CST_HIGH (arg1) == -1
7109           && negate_expr_p (arg1)
7110           && code == PLUS_EXPR)
7111         {
7112           arg11 = negate_expr (arg1);
7113           code = MINUS_EXPR;
7114         }
7115       else
7116         arg11 = arg1;
7117     }
7118   else
7119     {
7120       /* We cannot generate constant 1 for fract.  */
7121       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7122         return NULL_TREE;
7123       arg10 = arg1;
7124       arg11 = build_one_cst (type);
7125     }
7126   same = NULL_TREE;
7127
7128   if (operand_equal_p (arg01, arg11, 0))
7129     same = arg01, alt0 = arg00, alt1 = arg10;
7130   else if (operand_equal_p (arg00, arg10, 0))
7131     same = arg00, alt0 = arg01, alt1 = arg11;
7132   else if (operand_equal_p (arg00, arg11, 0))
7133     same = arg00, alt0 = arg01, alt1 = arg10;
7134   else if (operand_equal_p (arg01, arg10, 0))
7135     same = arg01, alt0 = arg00, alt1 = arg11;
7136
7137   /* No identical multiplicands; see if we can find a common
7138      power-of-two factor in non-power-of-two multiplies.  This
7139      can help in multi-dimensional array access.  */
7140   else if (host_integerp (arg01, 0)
7141            && host_integerp (arg11, 0))
7142     {
7143       HOST_WIDE_INT int01, int11, tmp;
7144       bool swap = false;
7145       tree maybe_same;
7146       int01 = TREE_INT_CST_LOW (arg01);
7147       int11 = TREE_INT_CST_LOW (arg11);
7148
7149       /* Move min of absolute values to int11.  */
7150       if (absu_hwi (int01) < absu_hwi (int11))
7151         {
7152           tmp = int01, int01 = int11, int11 = tmp;
7153           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7154           maybe_same = arg01;
7155           swap = true;
7156         }
7157       else
7158         maybe_same = arg11;
7159
7160       if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7161           /* The remainder should not be a constant, otherwise we
7162              end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7163              increased the number of multiplications necessary.  */
7164           && TREE_CODE (arg10) != INTEGER_CST)
7165         {
7166           alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7167                               build_int_cst (TREE_TYPE (arg00),
7168                                              int01 / int11));
7169           alt1 = arg10;
7170           same = maybe_same;
7171           if (swap)
7172             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7173         }
7174     }
7175
7176   if (same)
7177     return fold_build2_loc (loc, MULT_EXPR, type,
7178                         fold_build2_loc (loc, code, type,
7179                                      fold_convert_loc (loc, type, alt0),
7180                                      fold_convert_loc (loc, type, alt1)),
7181                         fold_convert_loc (loc, type, same));
7182
7183   return NULL_TREE;
7184 }
7185
7186 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7187    specified by EXPR into the buffer PTR of length LEN bytes.
7188    Return the number of bytes placed in the buffer, or zero
7189    upon failure.  */
7190
7191 static int
7192 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7193 {
7194   tree type = TREE_TYPE (expr);
7195   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7196   int byte, offset, word, words;
7197   unsigned char value;
7198
7199   if (total_bytes > len)
7200     return 0;
7201   words = total_bytes / UNITS_PER_WORD;
7202
7203   for (byte = 0; byte < total_bytes; byte++)
7204     {
7205       int bitpos = byte * BITS_PER_UNIT;
7206       if (bitpos < HOST_BITS_PER_WIDE_INT)
7207         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7208       else
7209         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7210                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7211
7212       if (total_bytes > UNITS_PER_WORD)
7213         {
7214           word = byte / UNITS_PER_WORD;
7215           if (WORDS_BIG_ENDIAN)
7216             word = (words - 1) - word;
7217           offset = word * UNITS_PER_WORD;
7218           if (BYTES_BIG_ENDIAN)
7219             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7220           else
7221             offset += byte % UNITS_PER_WORD;
7222         }
7223       else
7224         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7225       ptr[offset] = value;
7226     }
7227   return total_bytes;
7228 }
7229
7230
7231 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7232    specified by EXPR into the buffer PTR of length LEN bytes.
7233    Return the number of bytes placed in the buffer, or zero
7234    upon failure.  */
7235
7236 static int
7237 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7238 {
7239   tree type = TREE_TYPE (expr);
7240   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7241   int byte, offset, word, words, bitpos;
7242   unsigned char value;
7243
7244   /* There are always 32 bits in each long, no matter the size of
7245      the hosts long.  We handle floating point representations with
7246      up to 192 bits.  */
7247   long tmp[6];
7248
7249   if (total_bytes > len)
7250     return 0;
7251   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7252
7253   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7254
7255   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7256        bitpos += BITS_PER_UNIT)
7257     {
7258       byte = (bitpos / BITS_PER_UNIT) & 3;
7259       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7260
7261       if (UNITS_PER_WORD < 4)
7262         {
7263           word = byte / UNITS_PER_WORD;
7264           if (WORDS_BIG_ENDIAN)
7265             word = (words - 1) - word;
7266           offset = word * UNITS_PER_WORD;
7267           if (BYTES_BIG_ENDIAN)
7268             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7269           else
7270             offset += byte % UNITS_PER_WORD;
7271         }
7272       else
7273         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7274       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7275     }
7276   return total_bytes;
7277 }
7278
7279 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7280    specified by EXPR into the buffer PTR of length LEN bytes.
7281    Return the number of bytes placed in the buffer, or zero
7282    upon failure.  */
7283
7284 static int
7285 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7286 {
7287   int rsize, isize;
7288   tree part;
7289
7290   part = TREE_REALPART (expr);
7291   rsize = native_encode_expr (part, ptr, len);
7292   if (rsize == 0)
7293     return 0;
7294   part = TREE_IMAGPART (expr);
7295   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7296   if (isize != rsize)
7297     return 0;
7298   return rsize + isize;
7299 }
7300
7301
7302 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7303    specified by EXPR into the buffer PTR of length LEN bytes.
7304    Return the number of bytes placed in the buffer, or zero
7305    upon failure.  */
7306
7307 static int
7308 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7309 {
7310   int i, size, offset, count;
7311   tree itype, elem, elements;
7312
7313   offset = 0;
7314   elements = TREE_VECTOR_CST_ELTS (expr);
7315   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7316   itype = TREE_TYPE (TREE_TYPE (expr));
7317   size = GET_MODE_SIZE (TYPE_MODE (itype));
7318   for (i = 0; i < count; i++)
7319     {
7320       if (elements)
7321         {
7322           elem = TREE_VALUE (elements);
7323           elements = TREE_CHAIN (elements);
7324         }
7325       else
7326         elem = NULL_TREE;
7327
7328       if (elem)
7329         {
7330           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7331             return 0;
7332         }
7333       else
7334         {
7335           if (offset + size > len)
7336             return 0;
7337           memset (ptr+offset, 0, size);
7338         }
7339       offset += size;
7340     }
7341   return offset;
7342 }
7343
7344
7345 /* Subroutine of native_encode_expr.  Encode the STRING_CST
7346    specified by EXPR into the buffer PTR of length LEN bytes.
7347    Return the number of bytes placed in the buffer, or zero
7348    upon failure.  */
7349
7350 static int
7351 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7352 {
7353   tree type = TREE_TYPE (expr);
7354   HOST_WIDE_INT total_bytes;
7355
7356   if (TREE_CODE (type) != ARRAY_TYPE
7357       || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7358       || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7359       || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7360     return 0;
7361   total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7362   if (total_bytes > len)
7363     return 0;
7364   if (TREE_STRING_LENGTH (expr) < total_bytes)
7365     {
7366       memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7367       memset (ptr + TREE_STRING_LENGTH (expr), 0,
7368               total_bytes - TREE_STRING_LENGTH (expr));
7369     }
7370   else
7371     memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7372   return total_bytes;
7373 }
7374
7375
7376 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7377    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7378    buffer PTR of length LEN bytes.  Return the number of bytes
7379    placed in the buffer, or zero upon failure.  */
7380
7381 int
7382 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7383 {
7384   switch (TREE_CODE (expr))
7385     {
7386     case INTEGER_CST:
7387       return native_encode_int (expr, ptr, len);
7388
7389     case REAL_CST:
7390       return native_encode_real (expr, ptr, len);
7391
7392     case COMPLEX_CST:
7393       return native_encode_complex (expr, ptr, len);
7394
7395     case VECTOR_CST:
7396       return native_encode_vector (expr, ptr, len);
7397
7398     case STRING_CST:
7399       return native_encode_string (expr, ptr, len);
7400
7401     default:
7402       return 0;
7403     }
7404 }
7405
7406
7407 /* Subroutine of native_interpret_expr.  Interpret the contents of
7408    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7409    If the buffer cannot be interpreted, return NULL_TREE.  */
7410
7411 static tree
7412 native_interpret_int (tree type, const unsigned char *ptr, int len)
7413 {
7414   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7415   int byte, offset, word, words;
7416   unsigned char value;
7417   double_int result;
7418
7419   if (total_bytes > len)
7420     return NULL_TREE;
7421   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7422     return NULL_TREE;
7423
7424   result = double_int_zero;
7425   words = total_bytes / UNITS_PER_WORD;
7426
7427   for (byte = 0; byte < total_bytes; byte++)
7428     {
7429       int bitpos = byte * BITS_PER_UNIT;
7430       if (total_bytes > UNITS_PER_WORD)
7431         {
7432           word = byte / UNITS_PER_WORD;
7433           if (WORDS_BIG_ENDIAN)
7434             word = (words - 1) - word;
7435           offset = word * UNITS_PER_WORD;
7436           if (BYTES_BIG_ENDIAN)
7437             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7438           else
7439             offset += byte % UNITS_PER_WORD;
7440         }
7441       else
7442         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7443       value = ptr[offset];
7444
7445       if (bitpos < HOST_BITS_PER_WIDE_INT)
7446         result.low |= (unsigned HOST_WIDE_INT) value << bitpos;
7447       else
7448         result.high |= (unsigned HOST_WIDE_INT) value
7449                        << (bitpos - HOST_BITS_PER_WIDE_INT);
7450     }
7451
7452   return double_int_to_tree (type, result);
7453 }
7454
7455
7456 /* Subroutine of native_interpret_expr.  Interpret the contents of
7457    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7458    If the buffer cannot be interpreted, return NULL_TREE.  */
7459
7460 static tree
7461 native_interpret_real (tree type, const unsigned char *ptr, int len)
7462 {
7463   enum machine_mode mode = TYPE_MODE (type);
7464   int total_bytes = GET_MODE_SIZE (mode);
7465   int byte, offset, word, words, bitpos;
7466   unsigned char value;
7467   /* There are always 32 bits in each long, no matter the size of
7468      the hosts long.  We handle floating point representations with
7469      up to 192 bits.  */
7470   REAL_VALUE_TYPE r;
7471   long tmp[6];
7472
7473   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7474   if (total_bytes > len || total_bytes > 24)
7475     return NULL_TREE;
7476   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7477
7478   memset (tmp, 0, sizeof (tmp));
7479   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7480        bitpos += BITS_PER_UNIT)
7481     {
7482       byte = (bitpos / BITS_PER_UNIT) & 3;
7483       if (UNITS_PER_WORD < 4)
7484         {
7485           word = byte / UNITS_PER_WORD;
7486           if (WORDS_BIG_ENDIAN)
7487             word = (words - 1) - word;
7488           offset = word * UNITS_PER_WORD;
7489           if (BYTES_BIG_ENDIAN)
7490             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7491           else
7492             offset += byte % UNITS_PER_WORD;
7493         }
7494       else
7495         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7496       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7497
7498       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7499     }
7500
7501   real_from_target (&r, tmp, mode);
7502   return build_real (type, r);
7503 }
7504
7505
7506 /* Subroutine of native_interpret_expr.  Interpret the contents of
7507    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7508    If the buffer cannot be interpreted, return NULL_TREE.  */
7509
7510 static tree
7511 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7512 {
7513   tree etype, rpart, ipart;
7514   int size;
7515
7516   etype = TREE_TYPE (type);
7517   size = GET_MODE_SIZE (TYPE_MODE (etype));
7518   if (size * 2 > len)
7519     return NULL_TREE;
7520   rpart = native_interpret_expr (etype, ptr, size);
7521   if (!rpart)
7522     return NULL_TREE;
7523   ipart = native_interpret_expr (etype, ptr+size, size);
7524   if (!ipart)
7525     return NULL_TREE;
7526   return build_complex (type, rpart, ipart);
7527 }
7528
7529
7530 /* Subroutine of native_interpret_expr.  Interpret the contents of
7531    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7532    If the buffer cannot be interpreted, return NULL_TREE.  */
7533
7534 static tree
7535 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7536 {
7537   tree etype, elem, elements;
7538   int i, size, count;
7539
7540   etype = TREE_TYPE (type);
7541   size = GET_MODE_SIZE (TYPE_MODE (etype));
7542   count = TYPE_VECTOR_SUBPARTS (type);
7543   if (size * count > len)
7544     return NULL_TREE;
7545
7546   elements = NULL_TREE;
7547   for (i = count - 1; i >= 0; i--)
7548     {
7549       elem = native_interpret_expr (etype, ptr+(i*size), size);
7550       if (!elem)
7551         return NULL_TREE;
7552       elements = tree_cons (NULL_TREE, elem, elements);
7553     }
7554   return build_vector (type, elements);
7555 }
7556
7557
7558 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7559    the buffer PTR of length LEN as a constant of type TYPE.  For
7560    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7561    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7562    return NULL_TREE.  */
7563
7564 tree
7565 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7566 {
7567   switch (TREE_CODE (type))
7568     {
7569     case INTEGER_TYPE:
7570     case ENUMERAL_TYPE:
7571     case BOOLEAN_TYPE:
7572       return native_interpret_int (type, ptr, len);
7573
7574     case REAL_TYPE:
7575       return native_interpret_real (type, ptr, len);
7576
7577     case COMPLEX_TYPE:
7578       return native_interpret_complex (type, ptr, len);
7579
7580     case VECTOR_TYPE:
7581       return native_interpret_vector (type, ptr, len);
7582
7583     default:
7584       return NULL_TREE;
7585     }
7586 }
7587
7588
7589 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7590    TYPE at compile-time.  If we're unable to perform the conversion
7591    return NULL_TREE.  */
7592
7593 static tree
7594 fold_view_convert_expr (tree type, tree expr)
7595 {
7596   /* We support up to 512-bit values (for V8DFmode).  */
7597   unsigned char buffer[64];
7598   int len;
7599
7600   /* Check that the host and target are sane.  */
7601   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7602     return NULL_TREE;
7603
7604   len = native_encode_expr (expr, buffer, sizeof (buffer));
7605   if (len == 0)
7606     return NULL_TREE;
7607
7608   return native_interpret_expr (type, buffer, len);
7609 }
7610
7611 /* Build an expression for the address of T.  Folds away INDIRECT_REF
7612    to avoid confusing the gimplify process.  */
7613
7614 tree
7615 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7616 {
7617   /* The size of the object is not relevant when talking about its address.  */
7618   if (TREE_CODE (t) == WITH_SIZE_EXPR)
7619     t = TREE_OPERAND (t, 0);
7620
7621   if (TREE_CODE (t) == INDIRECT_REF)
7622     {
7623       t = TREE_OPERAND (t, 0);
7624
7625       if (TREE_TYPE (t) != ptrtype)
7626         t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7627     }
7628   else if (TREE_CODE (t) == MEM_REF
7629       && integer_zerop (TREE_OPERAND (t, 1)))
7630     return TREE_OPERAND (t, 0);
7631   else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7632     {
7633       t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7634
7635       if (TREE_TYPE (t) != ptrtype)
7636         t = fold_convert_loc (loc, ptrtype, t);
7637     }
7638   else
7639     t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7640
7641   return t;
7642 }
7643
7644 /* Build an expression for the address of T.  */
7645
7646 tree
7647 build_fold_addr_expr_loc (location_t loc, tree t)
7648 {
7649   tree ptrtype = build_pointer_type (TREE_TYPE (t));
7650
7651   return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7652 }
7653
7654 static bool vec_cst_ctor_to_array (tree, tree *);
7655
7656 /* Fold a unary expression of code CODE and type TYPE with operand
7657    OP0.  Return the folded expression if folding is successful.
7658    Otherwise, return NULL_TREE.  */
7659
7660 tree
7661 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7662 {
7663   tree tem;
7664   tree arg0;
7665   enum tree_code_class kind = TREE_CODE_CLASS (code);
7666
7667   gcc_assert (IS_EXPR_CODE_CLASS (kind)
7668               && TREE_CODE_LENGTH (code) == 1);
7669
7670   arg0 = op0;
7671   if (arg0)
7672     {
7673       if (CONVERT_EXPR_CODE_P (code)
7674           || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7675         {
7676           /* Don't use STRIP_NOPS, because signedness of argument type
7677              matters.  */
7678           STRIP_SIGN_NOPS (arg0);
7679         }
7680       else
7681         {
7682           /* Strip any conversions that don't change the mode.  This
7683              is safe for every expression, except for a comparison
7684              expression because its signedness is derived from its
7685              operands.
7686
7687              Note that this is done as an internal manipulation within
7688              the constant folder, in order to find the simplest
7689              representation of the arguments so that their form can be
7690              studied.  In any cases, the appropriate type conversions
7691              should be put back in the tree that will get out of the
7692              constant folder.  */
7693           STRIP_NOPS (arg0);
7694         }
7695     }
7696
7697   if (TREE_CODE_CLASS (code) == tcc_unary)
7698     {
7699       if (TREE_CODE (arg0) == COMPOUND_EXPR)
7700         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7701                        fold_build1_loc (loc, code, type,
7702                                     fold_convert_loc (loc, TREE_TYPE (op0),
7703                                                       TREE_OPERAND (arg0, 1))));
7704       else if (TREE_CODE (arg0) == COND_EXPR)
7705         {
7706           tree arg01 = TREE_OPERAND (arg0, 1);
7707           tree arg02 = TREE_OPERAND (arg0, 2);
7708           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7709             arg01 = fold_build1_loc (loc, code, type,
7710                                  fold_convert_loc (loc,
7711                                                    TREE_TYPE (op0), arg01));
7712           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7713             arg02 = fold_build1_loc (loc, code, type,
7714                                  fold_convert_loc (loc,
7715                                                    TREE_TYPE (op0), arg02));
7716           tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7717                              arg01, arg02);
7718
7719           /* If this was a conversion, and all we did was to move into
7720              inside the COND_EXPR, bring it back out.  But leave it if
7721              it is a conversion from integer to integer and the
7722              result precision is no wider than a word since such a
7723              conversion is cheap and may be optimized away by combine,
7724              while it couldn't if it were outside the COND_EXPR.  Then return
7725              so we don't get into an infinite recursion loop taking the
7726              conversion out and then back in.  */
7727
7728           if ((CONVERT_EXPR_CODE_P (code)
7729                || code == NON_LVALUE_EXPR)
7730               && TREE_CODE (tem) == COND_EXPR
7731               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7732               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7733               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7734               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7735               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7736                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7737               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7738                      && (INTEGRAL_TYPE_P
7739                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7740                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7741                   || flag_syntax_only))
7742             tem = build1_loc (loc, code, type,
7743                               build3 (COND_EXPR,
7744                                       TREE_TYPE (TREE_OPERAND
7745                                                  (TREE_OPERAND (tem, 1), 0)),
7746                                       TREE_OPERAND (tem, 0),
7747                                       TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7748                                       TREE_OPERAND (TREE_OPERAND (tem, 2),
7749                                                     0)));
7750           return tem;
7751         }
7752    }
7753
7754   switch (code)
7755     {
7756     case PAREN_EXPR:
7757       /* Re-association barriers around constants and other re-association
7758          barriers can be removed.  */
7759       if (CONSTANT_CLASS_P (op0)
7760           || TREE_CODE (op0) == PAREN_EXPR)
7761         return fold_convert_loc (loc, type, op0);
7762       return NULL_TREE;
7763
7764     CASE_CONVERT:
7765     case FLOAT_EXPR:
7766     case FIX_TRUNC_EXPR:
7767       if (TREE_TYPE (op0) == type)
7768         return op0;
7769
7770       if (COMPARISON_CLASS_P (op0))
7771         {
7772           /* If we have (type) (a CMP b) and type is an integral type, return
7773              new expression involving the new type.  Canonicalize
7774              (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7775              non-integral type.
7776              Do not fold the result as that would not simplify further, also
7777              folding again results in recursions.  */
7778           if (TREE_CODE (type) == BOOLEAN_TYPE)
7779             return build2_loc (loc, TREE_CODE (op0), type,
7780                                TREE_OPERAND (op0, 0),
7781                                TREE_OPERAND (op0, 1));
7782           else if (!INTEGRAL_TYPE_P (type))
7783             return build3_loc (loc, COND_EXPR, type, op0,
7784                                constant_boolean_node (true, type),
7785                                constant_boolean_node (false, type));
7786         }
7787
7788       /* Handle cases of two conversions in a row.  */
7789       if (CONVERT_EXPR_P (op0))
7790         {
7791           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7792           tree inter_type = TREE_TYPE (op0);
7793           int inside_int = INTEGRAL_TYPE_P (inside_type);
7794           int inside_ptr = POINTER_TYPE_P (inside_type);
7795           int inside_float = FLOAT_TYPE_P (inside_type);
7796           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7797           unsigned int inside_prec = TYPE_PRECISION (inside_type);
7798           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7799           int inter_int = INTEGRAL_TYPE_P (inter_type);
7800           int inter_ptr = POINTER_TYPE_P (inter_type);
7801           int inter_float = FLOAT_TYPE_P (inter_type);
7802           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7803           unsigned int inter_prec = TYPE_PRECISION (inter_type);
7804           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7805           int final_int = INTEGRAL_TYPE_P (type);
7806           int final_ptr = POINTER_TYPE_P (type);
7807           int final_float = FLOAT_TYPE_P (type);
7808           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7809           unsigned int final_prec = TYPE_PRECISION (type);
7810           int final_unsignedp = TYPE_UNSIGNED (type);
7811
7812           /* In addition to the cases of two conversions in a row
7813              handled below, if we are converting something to its own
7814              type via an object of identical or wider precision, neither
7815              conversion is needed.  */
7816           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7817               && (((inter_int || inter_ptr) && final_int)
7818                   || (inter_float && final_float))
7819               && inter_prec >= final_prec)
7820             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7821
7822           /* Likewise, if the intermediate and initial types are either both
7823              float or both integer, we don't need the middle conversion if the
7824              former is wider than the latter and doesn't change the signedness
7825              (for integers).  Avoid this if the final type is a pointer since
7826              then we sometimes need the middle conversion.  Likewise if the
7827              final type has a precision not equal to the size of its mode.  */
7828           if (((inter_int && inside_int)
7829                || (inter_float && inside_float)
7830                || (inter_vec && inside_vec))
7831               && inter_prec >= inside_prec
7832               && (inter_float || inter_vec
7833                   || inter_unsignedp == inside_unsignedp)
7834               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7835                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
7836               && ! final_ptr
7837               && (! final_vec || inter_prec == inside_prec))
7838             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7839
7840           /* If we have a sign-extension of a zero-extended value, we can
7841              replace that by a single zero-extension.  */
7842           if (inside_int && inter_int && final_int
7843               && inside_prec < inter_prec && inter_prec < final_prec
7844               && inside_unsignedp && !inter_unsignedp)
7845             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7846
7847           /* Two conversions in a row are not needed unless:
7848              - some conversion is floating-point (overstrict for now), or
7849              - some conversion is a vector (overstrict for now), or
7850              - the intermediate type is narrower than both initial and
7851                final, or
7852              - the intermediate type and innermost type differ in signedness,
7853                and the outermost type is wider than the intermediate, or
7854              - the initial type is a pointer type and the precisions of the
7855                intermediate and final types differ, or
7856              - the final type is a pointer type and the precisions of the
7857                initial and intermediate types differ.  */
7858           if (! inside_float && ! inter_float && ! final_float
7859               && ! inside_vec && ! inter_vec && ! final_vec
7860               && (inter_prec >= inside_prec || inter_prec >= final_prec)
7861               && ! (inside_int && inter_int
7862                     && inter_unsignedp != inside_unsignedp
7863                     && inter_prec < final_prec)
7864               && ((inter_unsignedp && inter_prec > inside_prec)
7865                   == (final_unsignedp && final_prec > inter_prec))
7866               && ! (inside_ptr && inter_prec != final_prec)
7867               && ! (final_ptr && inside_prec != inter_prec)
7868               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7869                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7870             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7871         }
7872
7873       /* Handle (T *)&A.B.C for A being of type T and B and C
7874          living at offset zero.  This occurs frequently in
7875          C++ upcasting and then accessing the base.  */
7876       if (TREE_CODE (op0) == ADDR_EXPR
7877           && POINTER_TYPE_P (type)
7878           && handled_component_p (TREE_OPERAND (op0, 0)))
7879         {
7880           HOST_WIDE_INT bitsize, bitpos;
7881           tree offset;
7882           enum machine_mode mode;
7883           int unsignedp, volatilep;
7884           tree base = TREE_OPERAND (op0, 0);
7885           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7886                                       &mode, &unsignedp, &volatilep, false);
7887           /* If the reference was to a (constant) zero offset, we can use
7888              the address of the base if it has the same base type
7889              as the result type and the pointer type is unqualified.  */
7890           if (! offset && bitpos == 0
7891               && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7892                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7893               && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7894             return fold_convert_loc (loc, type,
7895                                      build_fold_addr_expr_loc (loc, base));
7896         }
7897
7898       if (TREE_CODE (op0) == MODIFY_EXPR
7899           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7900           /* Detect assigning a bitfield.  */
7901           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7902                && DECL_BIT_FIELD
7903                (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7904         {
7905           /* Don't leave an assignment inside a conversion
7906              unless assigning a bitfield.  */
7907           tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7908           /* First do the assignment, then return converted constant.  */
7909           tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7910           TREE_NO_WARNING (tem) = 1;
7911           TREE_USED (tem) = 1;
7912           return tem;
7913         }
7914
7915       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7916          constants (if x has signed type, the sign bit cannot be set
7917          in c).  This folds extension into the BIT_AND_EXPR.
7918          ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7919          very likely don't have maximal range for their precision and this
7920          transformation effectively doesn't preserve non-maximal ranges.  */
7921       if (TREE_CODE (type) == INTEGER_TYPE
7922           && TREE_CODE (op0) == BIT_AND_EXPR
7923           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7924         {
7925           tree and_expr = op0;
7926           tree and0 = TREE_OPERAND (and_expr, 0);
7927           tree and1 = TREE_OPERAND (and_expr, 1);
7928           int change = 0;
7929
7930           if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7931               || (TYPE_PRECISION (type)
7932                   <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7933             change = 1;
7934           else if (TYPE_PRECISION (TREE_TYPE (and1))
7935                    <= HOST_BITS_PER_WIDE_INT
7936                    && host_integerp (and1, 1))
7937             {
7938               unsigned HOST_WIDE_INT cst;
7939
7940               cst = tree_low_cst (and1, 1);
7941               cst &= (HOST_WIDE_INT) -1
7942                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7943               change = (cst == 0);
7944 #ifdef LOAD_EXTEND_OP
7945               if (change
7946                   && !flag_syntax_only
7947                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7948                       == ZERO_EXTEND))
7949                 {
7950                   tree uns = unsigned_type_for (TREE_TYPE (and0));
7951                   and0 = fold_convert_loc (loc, uns, and0);
7952                   and1 = fold_convert_loc (loc, uns, and1);
7953                 }
7954 #endif
7955             }
7956           if (change)
7957             {
7958               tem = force_fit_type_double (type, tree_to_double_int (and1),
7959                                            0, TREE_OVERFLOW (and1));
7960               return fold_build2_loc (loc, BIT_AND_EXPR, type,
7961                                   fold_convert_loc (loc, type, and0), tem);
7962             }
7963         }
7964
7965       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7966          when one of the new casts will fold away. Conservatively we assume
7967          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7968       if (POINTER_TYPE_P (type)
7969           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7970           && (!TYPE_RESTRICT (type) || TYPE_RESTRICT (TREE_TYPE (arg0)))
7971           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7972               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7973               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7974         {
7975           tree arg00 = TREE_OPERAND (arg0, 0);
7976           tree arg01 = TREE_OPERAND (arg0, 1);
7977
7978           return fold_build_pointer_plus_loc
7979                    (loc, fold_convert_loc (loc, type, arg00), arg01);
7980         }
7981
7982       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7983          of the same precision, and X is an integer type not narrower than
7984          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
7985       if (INTEGRAL_TYPE_P (type)
7986           && TREE_CODE (op0) == BIT_NOT_EXPR
7987           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7988           && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7989           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7990         {
7991           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7992           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7993               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7994             return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7995                                 fold_convert_loc (loc, type, tem));
7996         }
7997
7998       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7999          type of X and Y (integer types only).  */
8000       if (INTEGRAL_TYPE_P (type)
8001           && TREE_CODE (op0) == MULT_EXPR
8002           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8003           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8004         {
8005           /* Be careful not to introduce new overflows.  */
8006           tree mult_type;
8007           if (TYPE_OVERFLOW_WRAPS (type))
8008             mult_type = type;
8009           else
8010             mult_type = unsigned_type_for (type);
8011
8012           if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8013             {
8014               tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8015                                  fold_convert_loc (loc, mult_type,
8016                                                    TREE_OPERAND (op0, 0)),
8017                                  fold_convert_loc (loc, mult_type,
8018                                                    TREE_OPERAND (op0, 1)));
8019               return fold_convert_loc (loc, type, tem);
8020             }
8021         }
8022
8023       tem = fold_convert_const (code, type, op0);
8024       return tem ? tem : NULL_TREE;
8025
8026     case ADDR_SPACE_CONVERT_EXPR:
8027       if (integer_zerop (arg0))
8028         return fold_convert_const (code, type, arg0);
8029       return NULL_TREE;
8030
8031     case FIXED_CONVERT_EXPR:
8032       tem = fold_convert_const (code, type, arg0);
8033       return tem ? tem : NULL_TREE;
8034
8035     case VIEW_CONVERT_EXPR:
8036       if (TREE_TYPE (op0) == type)
8037         return op0;
8038       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8039         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8040                             type, TREE_OPERAND (op0, 0));
8041       if (TREE_CODE (op0) == MEM_REF)
8042         return fold_build2_loc (loc, MEM_REF, type,
8043                                 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
8044
8045       /* For integral conversions with the same precision or pointer
8046          conversions use a NOP_EXPR instead.  */
8047       if ((INTEGRAL_TYPE_P (type)
8048            || POINTER_TYPE_P (type))
8049           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8050               || POINTER_TYPE_P (TREE_TYPE (op0)))
8051           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8052         return fold_convert_loc (loc, type, op0);
8053
8054       /* Strip inner integral conversions that do not change the precision.  */
8055       if (CONVERT_EXPR_P (op0)
8056           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8057               || POINTER_TYPE_P (TREE_TYPE (op0)))
8058           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8059               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8060           && (TYPE_PRECISION (TREE_TYPE (op0))
8061               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8062         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8063                             type, TREE_OPERAND (op0, 0));
8064
8065       return fold_view_convert_expr (type, op0);
8066
8067     case NEGATE_EXPR:
8068       tem = fold_negate_expr (loc, arg0);
8069       if (tem)
8070         return fold_convert_loc (loc, type, tem);
8071       return NULL_TREE;
8072
8073     case ABS_EXPR:
8074       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8075         return fold_abs_const (arg0, type);
8076       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8077         return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8078       /* Convert fabs((double)float) into (double)fabsf(float).  */
8079       else if (TREE_CODE (arg0) == NOP_EXPR
8080                && TREE_CODE (type) == REAL_TYPE)
8081         {
8082           tree targ0 = strip_float_extensions (arg0);
8083           if (targ0 != arg0)
8084             return fold_convert_loc (loc, type,
8085                                      fold_build1_loc (loc, ABS_EXPR,
8086                                                   TREE_TYPE (targ0),
8087                                                   targ0));
8088         }
8089       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8090       else if (TREE_CODE (arg0) == ABS_EXPR)
8091         return arg0;
8092       else if (tree_expr_nonnegative_p (arg0))
8093         return arg0;
8094
8095       /* Strip sign ops from argument.  */
8096       if (TREE_CODE (type) == REAL_TYPE)
8097         {
8098           tem = fold_strip_sign_ops (arg0);
8099           if (tem)
8100             return fold_build1_loc (loc, ABS_EXPR, type,
8101                                 fold_convert_loc (loc, type, tem));
8102         }
8103       return NULL_TREE;
8104
8105     case CONJ_EXPR:
8106       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8107         return fold_convert_loc (loc, type, arg0);
8108       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8109         {
8110           tree itype = TREE_TYPE (type);
8111           tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8112           tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8113           return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8114                               negate_expr (ipart));
8115         }
8116       if (TREE_CODE (arg0) == COMPLEX_CST)
8117         {
8118           tree itype = TREE_TYPE (type);
8119           tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8120           tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8121           return build_complex (type, rpart, negate_expr (ipart));
8122         }
8123       if (TREE_CODE (arg0) == CONJ_EXPR)
8124         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8125       return NULL_TREE;
8126
8127     case BIT_NOT_EXPR:
8128       if (TREE_CODE (arg0) == INTEGER_CST)
8129         return fold_not_const (arg0, type);
8130       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8131         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8132       /* Convert ~ (-A) to A - 1.  */
8133       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8134         return fold_build2_loc (loc, MINUS_EXPR, type,
8135                             fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8136                             build_int_cst (type, 1));
8137       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8138       else if (INTEGRAL_TYPE_P (type)
8139                && ((TREE_CODE (arg0) == MINUS_EXPR
8140                     && integer_onep (TREE_OPERAND (arg0, 1)))
8141                    || (TREE_CODE (arg0) == PLUS_EXPR
8142                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8143         return fold_build1_loc (loc, NEGATE_EXPR, type,
8144                             fold_convert_loc (loc, type,
8145                                               TREE_OPERAND (arg0, 0)));
8146       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8147       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8148                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8149                                      fold_convert_loc (loc, type,
8150                                                        TREE_OPERAND (arg0, 0)))))
8151         return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8152                             fold_convert_loc (loc, type,
8153                                               TREE_OPERAND (arg0, 1)));
8154       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8155                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8156                                      fold_convert_loc (loc, type,
8157                                                        TREE_OPERAND (arg0, 1)))))
8158         return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8159                             fold_convert_loc (loc, type,
8160                                               TREE_OPERAND (arg0, 0)), tem);
8161       /* Perform BIT_NOT_EXPR on each element individually.  */
8162       else if (TREE_CODE (arg0) == VECTOR_CST)
8163         {
8164           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8165           int count = TYPE_VECTOR_SUBPARTS (type), i;
8166
8167           for (i = 0; i < count; i++)
8168             {
8169               if (elements)
8170                 {
8171                   elem = TREE_VALUE (elements);
8172                   elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8173                   if (elem == NULL_TREE)
8174                     break;
8175                   elements = TREE_CHAIN (elements);
8176                 }
8177               else
8178                 elem = build_int_cst (TREE_TYPE (type), -1);
8179               list = tree_cons (NULL_TREE, elem, list);
8180             }
8181           if (i == count)
8182             return build_vector (type, nreverse (list));
8183         }
8184
8185       return NULL_TREE;
8186
8187     case TRUTH_NOT_EXPR:
8188       /* The argument to invert_truthvalue must have Boolean type.  */
8189       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8190           arg0 = fold_convert_loc (loc, boolean_type_node, arg0);
8191
8192       /* Note that the operand of this must be an int
8193          and its values must be 0 or 1.
8194          ("true" is a fixed value perhaps depending on the language,
8195          but we don't handle values other than 1 correctly yet.)  */
8196       tem = fold_truth_not_expr (loc, arg0);
8197       if (!tem)
8198         return NULL_TREE;
8199       return fold_convert_loc (loc, type, tem);
8200
8201     case REALPART_EXPR:
8202       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8203         return fold_convert_loc (loc, type, arg0);
8204       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8205         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8206                                  TREE_OPERAND (arg0, 1));
8207       if (TREE_CODE (arg0) == COMPLEX_CST)
8208         return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8209       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8210         {
8211           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8212           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8213                              fold_build1_loc (loc, REALPART_EXPR, itype,
8214                                           TREE_OPERAND (arg0, 0)),
8215                              fold_build1_loc (loc, REALPART_EXPR, itype,
8216                                           TREE_OPERAND (arg0, 1)));
8217           return fold_convert_loc (loc, type, tem);
8218         }
8219       if (TREE_CODE (arg0) == CONJ_EXPR)
8220         {
8221           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8222           tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8223                              TREE_OPERAND (arg0, 0));
8224           return fold_convert_loc (loc, type, tem);
8225         }
8226       if (TREE_CODE (arg0) == CALL_EXPR)
8227         {
8228           tree fn = get_callee_fndecl (arg0);
8229           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8230             switch (DECL_FUNCTION_CODE (fn))
8231               {
8232               CASE_FLT_FN (BUILT_IN_CEXPI):
8233                 fn = mathfn_built_in (type, BUILT_IN_COS);
8234                 if (fn)
8235                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8236                 break;
8237
8238               default:
8239                 break;
8240               }
8241         }
8242       return NULL_TREE;
8243
8244     case IMAGPART_EXPR:
8245       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8246         return build_zero_cst (type);
8247       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8248         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8249                                  TREE_OPERAND (arg0, 0));
8250       if (TREE_CODE (arg0) == COMPLEX_CST)
8251         return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8252       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8253         {
8254           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8255           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8256                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8257                                           TREE_OPERAND (arg0, 0)),
8258                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8259                                           TREE_OPERAND (arg0, 1)));
8260           return fold_convert_loc (loc, type, tem);
8261         }
8262       if (TREE_CODE (arg0) == CONJ_EXPR)
8263         {
8264           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8265           tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8266           return fold_convert_loc (loc, type, negate_expr (tem));
8267         }
8268       if (TREE_CODE (arg0) == CALL_EXPR)
8269         {
8270           tree fn = get_callee_fndecl (arg0);
8271           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8272             switch (DECL_FUNCTION_CODE (fn))
8273               {
8274               CASE_FLT_FN (BUILT_IN_CEXPI):
8275                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8276                 if (fn)
8277                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8278                 break;
8279
8280               default:
8281                 break;
8282               }
8283         }
8284       return NULL_TREE;
8285
8286     case INDIRECT_REF:
8287       /* Fold *&X to X if X is an lvalue.  */
8288       if (TREE_CODE (op0) == ADDR_EXPR)
8289         {
8290           tree op00 = TREE_OPERAND (op0, 0);
8291           if ((TREE_CODE (op00) == VAR_DECL
8292                || TREE_CODE (op00) == PARM_DECL
8293                || TREE_CODE (op00) == RESULT_DECL)
8294               && !TREE_READONLY (op00))
8295             return op00;
8296         }
8297       return NULL_TREE;
8298
8299     case VEC_UNPACK_LO_EXPR:
8300     case VEC_UNPACK_HI_EXPR:
8301     case VEC_UNPACK_FLOAT_LO_EXPR:
8302     case VEC_UNPACK_FLOAT_HI_EXPR:
8303       {
8304         unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8305         tree *elts, vals = NULL_TREE;
8306         enum tree_code subcode;
8307
8308         gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
8309         if (TREE_CODE (arg0) != VECTOR_CST)
8310           return NULL_TREE;
8311
8312         elts = XALLOCAVEC (tree, nelts * 2);
8313         if (!vec_cst_ctor_to_array (arg0, elts))
8314           return NULL_TREE;
8315
8316         if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
8317                                    || code == VEC_UNPACK_FLOAT_LO_EXPR))
8318           elts += nelts;
8319
8320         if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
8321           subcode = NOP_EXPR;
8322         else
8323           subcode = FLOAT_EXPR;
8324
8325         for (i = 0; i < nelts; i++)
8326           {
8327             elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
8328             if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
8329               return NULL_TREE;
8330           }
8331
8332         for (i = 0; i < nelts; i++)
8333           vals = tree_cons (NULL_TREE, elts[nelts - i - 1], vals);
8334         return build_vector (type, vals);
8335       }
8336
8337     default:
8338       return NULL_TREE;
8339     } /* switch (code) */
8340 }
8341
8342
8343 /* If the operation was a conversion do _not_ mark a resulting constant
8344    with TREE_OVERFLOW if the original constant was not.  These conversions
8345    have implementation defined behavior and retaining the TREE_OVERFLOW
8346    flag here would confuse later passes such as VRP.  */
8347 tree
8348 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8349                                 tree type, tree op0)
8350 {
8351   tree res = fold_unary_loc (loc, code, type, op0);
8352   if (res
8353       && TREE_CODE (res) == INTEGER_CST
8354       && TREE_CODE (op0) == INTEGER_CST
8355       && CONVERT_EXPR_CODE_P (code))
8356     TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8357
8358   return res;
8359 }
8360
8361 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8362    operands OP0 and OP1.  LOC is the location of the resulting expression.
8363    ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8364    Return the folded expression if folding is successful.  Otherwise,
8365    return NULL_TREE.  */
8366 static tree
8367 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8368                   tree arg0, tree arg1, tree op0, tree op1)
8369 {
8370   tree tem;
8371
8372   /* We only do these simplifications if we are optimizing.  */
8373   if (!optimize)
8374     return NULL_TREE;
8375
8376   /* Check for things like (A || B) && (A || C).  We can convert this
8377      to A || (B && C).  Note that either operator can be any of the four
8378      truth and/or operations and the transformation will still be
8379      valid.   Also note that we only care about order for the
8380      ANDIF and ORIF operators.  If B contains side effects, this
8381      might change the truth-value of A.  */
8382   if (TREE_CODE (arg0) == TREE_CODE (arg1)
8383       && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8384           || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8385           || TREE_CODE (arg0) == TRUTH_AND_EXPR
8386           || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8387       && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8388     {
8389       tree a00 = TREE_OPERAND (arg0, 0);
8390       tree a01 = TREE_OPERAND (arg0, 1);
8391       tree a10 = TREE_OPERAND (arg1, 0);
8392       tree a11 = TREE_OPERAND (arg1, 1);
8393       int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8394                           || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8395                          && (code == TRUTH_AND_EXPR
8396                              || code == TRUTH_OR_EXPR));
8397
8398       if (operand_equal_p (a00, a10, 0))
8399         return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8400                             fold_build2_loc (loc, code, type, a01, a11));
8401       else if (commutative && operand_equal_p (a00, a11, 0))
8402         return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8403                             fold_build2_loc (loc, code, type, a01, a10));
8404       else if (commutative && operand_equal_p (a01, a10, 0))
8405         return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8406                             fold_build2_loc (loc, code, type, a00, a11));
8407
8408       /* This case if tricky because we must either have commutative
8409          operators or else A10 must not have side-effects.  */
8410
8411       else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8412                && operand_equal_p (a01, a11, 0))
8413         return fold_build2_loc (loc, TREE_CODE (arg0), type,
8414                             fold_build2_loc (loc, code, type, a00, a10),
8415                             a01);
8416     }
8417
8418   /* See if we can build a range comparison.  */
8419   if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8420     return tem;
8421
8422   if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8423       || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8424     {
8425       tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8426       if (tem)
8427         return fold_build2_loc (loc, code, type, tem, arg1);
8428     }
8429
8430   if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8431       || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8432     {
8433       tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8434       if (tem)
8435         return fold_build2_loc (loc, code, type, arg0, tem);
8436     }
8437
8438   /* Check for the possibility of merging component references.  If our
8439      lhs is another similar operation, try to merge its rhs with our
8440      rhs.  Then try to merge our lhs and rhs.  */
8441   if (TREE_CODE (arg0) == code
8442       && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8443                                          TREE_OPERAND (arg0, 1), arg1)))
8444     return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8445
8446   if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8447     return tem;
8448
8449   if ((BRANCH_COST (optimize_function_for_speed_p (cfun),
8450                     false) >= 2)
8451       && LOGICAL_OP_NON_SHORT_CIRCUIT
8452       && (code == TRUTH_AND_EXPR
8453           || code == TRUTH_ANDIF_EXPR
8454           || code == TRUTH_OR_EXPR
8455           || code == TRUTH_ORIF_EXPR))
8456     {
8457       enum tree_code ncode, icode;
8458
8459       ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8460               ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8461       icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8462
8463       /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8464          or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8465          We don't want to pack more than two leafs to a non-IF AND/OR
8466          expression.
8467          If tree-code of left-hand operand isn't an AND/OR-IF code and not
8468          equal to IF-CODE, then we don't want to add right-hand operand.
8469          If the inner right-hand side of left-hand operand has
8470          side-effects, or isn't simple, then we can't add to it,
8471          as otherwise we might destroy if-sequence.  */
8472       if (TREE_CODE (arg0) == icode
8473           && simple_operand_p_2 (arg1)
8474           /* Needed for sequence points to handle trappings, and
8475              side-effects.  */
8476           && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8477         {
8478           tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8479                                  arg1);
8480           return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8481                                   tem);
8482         }
8483         /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8484            or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C).  */
8485       else if (TREE_CODE (arg1) == icode
8486           && simple_operand_p_2 (arg0)
8487           /* Needed for sequence points to handle trappings, and
8488              side-effects.  */
8489           && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8490         {
8491           tem = fold_build2_loc (loc, ncode, type, 
8492                                  arg0, TREE_OPERAND (arg1, 0));
8493           return fold_build2_loc (loc, icode, type, tem,
8494                                   TREE_OPERAND (arg1, 1));
8495         }
8496       /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8497          into (A OR B).
8498          For sequence point consistancy, we need to check for trapping,
8499          and side-effects.  */
8500       else if (code == icode && simple_operand_p_2 (arg0)
8501                && simple_operand_p_2 (arg1))
8502         return fold_build2_loc (loc, ncode, type, arg0, arg1);
8503     }
8504
8505   return NULL_TREE;
8506 }
8507
8508 /* Fold a binary expression of code CODE and type TYPE with operands
8509    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8510    Return the folded expression if folding is successful.  Otherwise,
8511    return NULL_TREE.  */
8512
8513 static tree
8514 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8515 {
8516   enum tree_code compl_code;
8517
8518   if (code == MIN_EXPR)
8519     compl_code = MAX_EXPR;
8520   else if (code == MAX_EXPR)
8521     compl_code = MIN_EXPR;
8522   else
8523     gcc_unreachable ();
8524
8525   /* MIN (MAX (a, b), b) == b.  */
8526   if (TREE_CODE (op0) == compl_code
8527       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8528     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8529
8530   /* MIN (MAX (b, a), b) == b.  */
8531   if (TREE_CODE (op0) == compl_code
8532       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8533       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8534     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8535
8536   /* MIN (a, MAX (a, b)) == a.  */
8537   if (TREE_CODE (op1) == compl_code
8538       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8539       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8540     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8541
8542   /* MIN (a, MAX (b, a)) == a.  */
8543   if (TREE_CODE (op1) == compl_code
8544       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8545       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8546     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8547
8548   return NULL_TREE;
8549 }
8550
8551 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8552    by changing CODE to reduce the magnitude of constants involved in
8553    ARG0 of the comparison.
8554    Returns a canonicalized comparison tree if a simplification was
8555    possible, otherwise returns NULL_TREE.
8556    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8557    valid if signed overflow is undefined.  */
8558
8559 static tree
8560 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8561                                  tree arg0, tree arg1,
8562                                  bool *strict_overflow_p)
8563 {
8564   enum tree_code code0 = TREE_CODE (arg0);
8565   tree t, cst0 = NULL_TREE;
8566   int sgn0;
8567   bool swap = false;
8568
8569   /* Match A +- CST code arg1 and CST code arg1.  We can change the
8570      first form only if overflow is undefined.  */
8571   if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8572          /* In principle pointers also have undefined overflow behavior,
8573             but that causes problems elsewhere.  */
8574          && !POINTER_TYPE_P (TREE_TYPE (arg0))
8575          && (code0 == MINUS_EXPR
8576              || code0 == PLUS_EXPR)
8577          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8578         || code0 == INTEGER_CST))
8579     return NULL_TREE;
8580
8581   /* Identify the constant in arg0 and its sign.  */
8582   if (code0 == INTEGER_CST)
8583     cst0 = arg0;
8584   else
8585     cst0 = TREE_OPERAND (arg0, 1);
8586   sgn0 = tree_int_cst_sgn (cst0);
8587
8588   /* Overflowed constants and zero will cause problems.  */
8589   if (integer_zerop (cst0)
8590       || TREE_OVERFLOW (cst0))
8591     return NULL_TREE;
8592
8593   /* See if we can reduce the magnitude of the constant in
8594      arg0 by changing the comparison code.  */
8595   if (code0 == INTEGER_CST)
8596     {
8597       /* CST <= arg1  ->  CST-1 < arg1.  */
8598       if (code == LE_EXPR && sgn0 == 1)
8599         code = LT_EXPR;
8600       /* -CST < arg1  ->  -CST-1 <= arg1.  */
8601       else if (code == LT_EXPR && sgn0 == -1)
8602         code = LE_EXPR;
8603       /* CST > arg1  ->  CST-1 >= arg1.  */
8604       else if (code == GT_EXPR && sgn0 == 1)
8605         code = GE_EXPR;
8606       /* -CST >= arg1  ->  -CST-1 > arg1.  */
8607       else if (code == GE_EXPR && sgn0 == -1)
8608         code = GT_EXPR;
8609       else
8610         return NULL_TREE;
8611       /* arg1 code' CST' might be more canonical.  */
8612       swap = true;
8613     }
8614   else
8615     {
8616       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
8617       if (code == LT_EXPR
8618           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8619         code = LE_EXPR;
8620       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
8621       else if (code == GT_EXPR
8622                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8623         code = GE_EXPR;
8624       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
8625       else if (code == LE_EXPR
8626                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8627         code = LT_EXPR;
8628       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
8629       else if (code == GE_EXPR
8630                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8631         code = GT_EXPR;
8632       else
8633         return NULL_TREE;
8634       *strict_overflow_p = true;
8635     }
8636
8637   /* Now build the constant reduced in magnitude.  But not if that
8638      would produce one outside of its types range.  */
8639   if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8640       && ((sgn0 == 1
8641            && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8642            && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8643           || (sgn0 == -1
8644               && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8645               && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8646     /* We cannot swap the comparison here as that would cause us to
8647        endlessly recurse.  */
8648     return NULL_TREE;
8649
8650   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8651                        cst0, build_int_cst (TREE_TYPE (cst0), 1));
8652   if (code0 != INTEGER_CST)
8653     t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8654   t = fold_convert (TREE_TYPE (arg1), t);
8655
8656   /* If swapping might yield to a more canonical form, do so.  */
8657   if (swap)
8658     return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8659   else
8660     return fold_build2_loc (loc, code, type, t, arg1);
8661 }
8662
8663 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8664    overflow further.  Try to decrease the magnitude of constants involved
8665    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8666    and put sole constants at the second argument position.
8667    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
8668
8669 static tree
8670 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8671                                tree arg0, tree arg1)
8672 {
8673   tree t;
8674   bool strict_overflow_p;
8675   const char * const warnmsg = G_("assuming signed overflow does not occur "
8676                                   "when reducing constant in comparison");
8677
8678   /* Try canonicalization by simplifying arg0.  */
8679   strict_overflow_p = false;
8680   t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8681                                        &strict_overflow_p);
8682   if (t)
8683     {
8684       if (strict_overflow_p)
8685         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8686       return t;
8687     }
8688
8689   /* Try canonicalization by simplifying arg1 using the swapped
8690      comparison.  */
8691   code = swap_tree_comparison (code);
8692   strict_overflow_p = false;
8693   t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8694                                        &strict_overflow_p);
8695   if (t && strict_overflow_p)
8696     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8697   return t;
8698 }
8699
8700 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8701    space.  This is used to avoid issuing overflow warnings for
8702    expressions like &p->x which can not wrap.  */
8703
8704 static bool
8705 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8706 {
8707   unsigned HOST_WIDE_INT offset_low, total_low;
8708   HOST_WIDE_INT size, offset_high, total_high;
8709
8710   if (!POINTER_TYPE_P (TREE_TYPE (base)))
8711     return true;
8712
8713   if (bitpos < 0)
8714     return true;
8715
8716   if (offset == NULL_TREE)
8717     {
8718       offset_low = 0;
8719       offset_high = 0;
8720     }
8721   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8722     return true;
8723   else
8724     {
8725       offset_low = TREE_INT_CST_LOW (offset);
8726       offset_high = TREE_INT_CST_HIGH (offset);
8727     }
8728
8729   if (add_double_with_sign (offset_low, offset_high,
8730                             bitpos / BITS_PER_UNIT, 0,
8731                             &total_low, &total_high,
8732                             true))
8733     return true;
8734
8735   if (total_high != 0)
8736     return true;
8737
8738   size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8739   if (size <= 0)
8740     return true;
8741
8742   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8743      array.  */
8744   if (TREE_CODE (base) == ADDR_EXPR)
8745     {
8746       HOST_WIDE_INT base_size;
8747
8748       base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8749       if (base_size > 0 && size < base_size)
8750         size = base_size;
8751     }
8752
8753   return total_low > (unsigned HOST_WIDE_INT) size;
8754 }
8755
8756 /* Subroutine of fold_binary.  This routine performs all of the
8757    transformations that are common to the equality/inequality
8758    operators (EQ_EXPR and NE_EXPR) and the ordering operators
8759    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
8760    fold_binary should call fold_binary.  Fold a comparison with
8761    tree code CODE and type TYPE with operands OP0 and OP1.  Return
8762    the folded comparison or NULL_TREE.  */
8763
8764 static tree
8765 fold_comparison (location_t loc, enum tree_code code, tree type,
8766                  tree op0, tree op1)
8767 {
8768   tree arg0, arg1, tem;
8769
8770   arg0 = op0;
8771   arg1 = op1;
8772
8773   STRIP_SIGN_NOPS (arg0);
8774   STRIP_SIGN_NOPS (arg1);
8775
8776   tem = fold_relational_const (code, type, arg0, arg1);
8777   if (tem != NULL_TREE)
8778     return tem;
8779
8780   /* If one arg is a real or integer constant, put it last.  */
8781   if (tree_swap_operands_p (arg0, arg1, true))
8782     return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
8783
8784   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
8785   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8786       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8787           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8788           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8789       && (TREE_CODE (arg1) == INTEGER_CST
8790           && !TREE_OVERFLOW (arg1)))
8791     {
8792       tree const1 = TREE_OPERAND (arg0, 1);
8793       tree const2 = arg1;
8794       tree variable = TREE_OPERAND (arg0, 0);
8795       tree lhs;
8796       int lhs_add;
8797       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8798
8799       lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
8800                          TREE_TYPE (arg1), const2, const1);
8801
8802       /* If the constant operation overflowed this can be
8803          simplified as a comparison against INT_MAX/INT_MIN.  */
8804       if (TREE_CODE (lhs) == INTEGER_CST
8805           && TREE_OVERFLOW (lhs))
8806         {
8807           int const1_sgn = tree_int_cst_sgn (const1);
8808           enum tree_code code2 = code;
8809
8810           /* Get the sign of the constant on the lhs if the
8811              operation were VARIABLE + CONST1.  */
8812           if (TREE_CODE (arg0) == MINUS_EXPR)
8813             const1_sgn = -const1_sgn;
8814
8815           /* The sign of the constant determines if we overflowed
8816              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8817              Canonicalize to the INT_MIN overflow by swapping the comparison
8818              if necessary.  */
8819           if (const1_sgn == -1)
8820             code2 = swap_tree_comparison (code);
8821
8822           /* We now can look at the canonicalized case
8823                VARIABLE + 1  CODE2  INT_MIN
8824              and decide on the result.  */
8825           if (code2 == LT_EXPR
8826               || code2 == LE_EXPR
8827               || code2 == EQ_EXPR)
8828             return omit_one_operand_loc (loc, type, boolean_false_node, variable);
8829           else if (code2 == NE_EXPR
8830                    || code2 == GE_EXPR
8831                    || code2 == GT_EXPR)
8832             return omit_one_operand_loc (loc, type, boolean_true_node, variable);
8833         }
8834
8835       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8836           && (TREE_CODE (lhs) != INTEGER_CST
8837               || !TREE_OVERFLOW (lhs)))
8838         {
8839           if (code != EQ_EXPR && code != NE_EXPR)
8840             fold_overflow_warning ("assuming signed overflow does not occur "
8841                                    "when changing X +- C1 cmp C2 to "
8842                                    "X cmp C1 +- C2",
8843                                    WARN_STRICT_OVERFLOW_COMPARISON);
8844           return fold_build2_loc (loc, code, type, variable, lhs);
8845         }
8846     }
8847
8848   /* For comparisons of pointers we can decompose it to a compile time
8849      comparison of the base objects and the offsets into the object.
8850      This requires at least one operand being an ADDR_EXPR or a
8851      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
8852   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8853       && (TREE_CODE (arg0) == ADDR_EXPR
8854           || TREE_CODE (arg1) == ADDR_EXPR
8855           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8856           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8857     {
8858       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8859       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8860       enum machine_mode mode;
8861       int volatilep, unsignedp;
8862       bool indirect_base0 = false, indirect_base1 = false;
8863
8864       /* Get base and offset for the access.  Strip ADDR_EXPR for
8865          get_inner_reference, but put it back by stripping INDIRECT_REF
8866          off the base object if possible.  indirect_baseN will be true
8867          if baseN is not an address but refers to the object itself.  */
8868       base0 = arg0;
8869       if (TREE_CODE (arg0) == ADDR_EXPR)
8870         {
8871           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8872                                        &bitsize, &bitpos0, &offset0, &mode,
8873                                        &unsignedp, &volatilep, false);
8874           if (TREE_CODE (base0) == INDIRECT_REF)
8875             base0 = TREE_OPERAND (base0, 0);
8876           else
8877             indirect_base0 = true;
8878         }
8879       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8880         {
8881           base0 = TREE_OPERAND (arg0, 0);
8882           STRIP_SIGN_NOPS (base0);
8883           if (TREE_CODE (base0) == ADDR_EXPR)
8884             {
8885               base0 = TREE_OPERAND (base0, 0);
8886               indirect_base0 = true;
8887             }
8888           offset0 = TREE_OPERAND (arg0, 1);
8889           if (host_integerp (offset0, 0)
8890               && ((HOST_WIDE_INT) (TREE_INT_CST_LOW (offset0) * BITS_PER_UNIT)
8891                   / BITS_PER_UNIT
8892                   == (HOST_WIDE_INT) TREE_INT_CST_LOW (offset0)))
8893             {
8894               bitpos0 = TREE_INT_CST_LOW (offset0) * BITS_PER_UNIT;
8895               offset0 = NULL_TREE;
8896             }
8897         }
8898
8899       base1 = arg1;
8900       if (TREE_CODE (arg1) == ADDR_EXPR)
8901         {
8902           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8903                                        &bitsize, &bitpos1, &offset1, &mode,
8904                                        &unsignedp, &volatilep, false);
8905           if (TREE_CODE (base1) == INDIRECT_REF)
8906             base1 = TREE_OPERAND (base1, 0);
8907           else
8908             indirect_base1 = true;
8909         }
8910       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8911         {
8912           base1 = TREE_OPERAND (arg1, 0);
8913           STRIP_SIGN_NOPS (base1);
8914           if (TREE_CODE (base1) == ADDR_EXPR)
8915             {
8916               base1 = TREE_OPERAND (base1, 0);
8917               indirect_base1 = true;
8918             }
8919           offset1 = TREE_OPERAND (arg1, 1);
8920           if (host_integerp (offset1, 0)
8921               && ((HOST_WIDE_INT) (TREE_INT_CST_LOW (offset1) * BITS_PER_UNIT)
8922                   / BITS_PER_UNIT
8923                   == (HOST_WIDE_INT) TREE_INT_CST_LOW (offset1)))
8924             {
8925               bitpos1 = TREE_INT_CST_LOW (offset1) * BITS_PER_UNIT;
8926               offset1 = NULL_TREE;
8927             }
8928         }
8929
8930       /* A local variable can never be pointed to by
8931          the default SSA name of an incoming parameter.  */
8932       if ((TREE_CODE (arg0) == ADDR_EXPR
8933            && indirect_base0
8934            && TREE_CODE (base0) == VAR_DECL
8935            && auto_var_in_fn_p (base0, current_function_decl)
8936            && !indirect_base1
8937            && TREE_CODE (base1) == SSA_NAME
8938            && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL
8939            && SSA_NAME_IS_DEFAULT_DEF (base1))
8940           || (TREE_CODE (arg1) == ADDR_EXPR
8941               && indirect_base1
8942               && TREE_CODE (base1) == VAR_DECL
8943               && auto_var_in_fn_p (base1, current_function_decl)
8944               && !indirect_base0
8945               && TREE_CODE (base0) == SSA_NAME
8946               && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL
8947               && SSA_NAME_IS_DEFAULT_DEF (base0)))
8948         {
8949           if (code == NE_EXPR)
8950             return constant_boolean_node (1, type);
8951           else if (code == EQ_EXPR)
8952             return constant_boolean_node (0, type);
8953         }
8954       /* If we have equivalent bases we might be able to simplify.  */
8955       else if (indirect_base0 == indirect_base1
8956                && operand_equal_p (base0, base1, 0))
8957         {
8958           /* We can fold this expression to a constant if the non-constant
8959              offset parts are equal.  */
8960           if ((offset0 == offset1
8961                || (offset0 && offset1
8962                    && operand_equal_p (offset0, offset1, 0)))
8963               && (code == EQ_EXPR
8964                   || code == NE_EXPR
8965                   || (indirect_base0 && DECL_P (base0))
8966                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
8967
8968             {
8969               if (code != EQ_EXPR
8970                   && code != NE_EXPR
8971                   && bitpos0 != bitpos1
8972                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
8973                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
8974                 fold_overflow_warning (("assuming pointer wraparound does not "
8975                                         "occur when comparing P +- C1 with "
8976                                         "P +- C2"),
8977                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
8978
8979               switch (code)
8980                 {
8981                 case EQ_EXPR:
8982                   return constant_boolean_node (bitpos0 == bitpos1, type);
8983                 case NE_EXPR:
8984                   return constant_boolean_node (bitpos0 != bitpos1, type);
8985                 case LT_EXPR:
8986                   return constant_boolean_node (bitpos0 < bitpos1, type);
8987                 case LE_EXPR:
8988                   return constant_boolean_node (bitpos0 <= bitpos1, type);
8989                 case GE_EXPR:
8990                   return constant_boolean_node (bitpos0 >= bitpos1, type);
8991                 case GT_EXPR:
8992                   return constant_boolean_node (bitpos0 > bitpos1, type);
8993                 default:;
8994                 }
8995             }
8996           /* We can simplify the comparison to a comparison of the variable
8997              offset parts if the constant offset parts are equal.
8998              Be careful to use signed size type here because otherwise we
8999              mess with array offsets in the wrong way.  This is possible
9000              because pointer arithmetic is restricted to retain within an
9001              object and overflow on pointer differences is undefined as of
9002              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
9003           else if (bitpos0 == bitpos1
9004                    && ((code == EQ_EXPR || code == NE_EXPR)
9005                        || (indirect_base0 && DECL_P (base0))
9006                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
9007             {
9008               /* By converting to signed size type we cover middle-end pointer
9009                  arithmetic which operates on unsigned pointer types of size
9010                  type size and ARRAY_REF offsets which are properly sign or
9011                  zero extended from their type in case it is narrower than
9012                  size type.  */
9013               if (offset0 == NULL_TREE)
9014                 offset0 = build_int_cst (ssizetype, 0);
9015               else
9016                 offset0 = fold_convert_loc (loc, ssizetype, offset0);
9017               if (offset1 == NULL_TREE)
9018                 offset1 = build_int_cst (ssizetype, 0);
9019               else
9020                 offset1 = fold_convert_loc (loc, ssizetype, offset1);
9021
9022               if (code != EQ_EXPR
9023                   && code != NE_EXPR
9024                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9025                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9026                 fold_overflow_warning (("assuming pointer wraparound does not "
9027                                         "occur when comparing P +- C1 with "
9028                                         "P +- C2"),
9029                                        WARN_STRICT_OVERFLOW_COMPARISON);
9030
9031               return fold_build2_loc (loc, code, type, offset0, offset1);
9032             }
9033         }
9034       /* For non-equal bases we can simplify if they are addresses
9035          of local binding decls or constants.  */
9036       else if (indirect_base0 && indirect_base1
9037                /* We know that !operand_equal_p (base0, base1, 0)
9038                   because the if condition was false.  But make
9039                   sure two decls are not the same.  */
9040                && base0 != base1
9041                && TREE_CODE (arg0) == ADDR_EXPR
9042                && TREE_CODE (arg1) == ADDR_EXPR
9043                && (((TREE_CODE (base0) == VAR_DECL
9044                      || TREE_CODE (base0) == PARM_DECL)
9045                     && (targetm.binds_local_p (base0)
9046                         || CONSTANT_CLASS_P (base1)))
9047                    || CONSTANT_CLASS_P (base0))
9048                && (((TREE_CODE (base1) == VAR_DECL
9049                      || TREE_CODE (base1) == PARM_DECL)
9050                     && (targetm.binds_local_p (base1)
9051                         || CONSTANT_CLASS_P (base0)))
9052                    || CONSTANT_CLASS_P (base1)))
9053         {
9054           if (code == EQ_EXPR)
9055             return omit_two_operands_loc (loc, type, boolean_false_node,
9056                                       arg0, arg1);
9057           else if (code == NE_EXPR)
9058             return omit_two_operands_loc (loc, type, boolean_true_node,
9059                                       arg0, arg1);
9060         }
9061       /* For equal offsets we can simplify to a comparison of the
9062          base addresses.  */
9063       else if (bitpos0 == bitpos1
9064                && (indirect_base0
9065                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9066                && (indirect_base1
9067                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9068                && ((offset0 == offset1)
9069                    || (offset0 && offset1
9070                        && operand_equal_p (offset0, offset1, 0))))
9071         {
9072           if (indirect_base0)
9073             base0 = build_fold_addr_expr_loc (loc, base0);
9074           if (indirect_base1)
9075             base1 = build_fold_addr_expr_loc (loc, base1);
9076           return fold_build2_loc (loc, code, type, base0, base1);
9077         }
9078     }
9079
9080   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9081      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
9082      the resulting offset is smaller in absolute value than the
9083      original one.  */
9084   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9085       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9086       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9087           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9088       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9089       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9090           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9091     {
9092       tree const1 = TREE_OPERAND (arg0, 1);
9093       tree const2 = TREE_OPERAND (arg1, 1);
9094       tree variable1 = TREE_OPERAND (arg0, 0);
9095       tree variable2 = TREE_OPERAND (arg1, 0);
9096       tree cst;
9097       const char * const warnmsg = G_("assuming signed overflow does not "
9098                                       "occur when combining constants around "
9099                                       "a comparison");
9100
9101       /* Put the constant on the side where it doesn't overflow and is
9102          of lower absolute value than before.  */
9103       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9104                              ? MINUS_EXPR : PLUS_EXPR,
9105                              const2, const1);
9106       if (!TREE_OVERFLOW (cst)
9107           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
9108         {
9109           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9110           return fold_build2_loc (loc, code, type,
9111                               variable1,
9112                               fold_build2_loc (loc,
9113                                            TREE_CODE (arg1), TREE_TYPE (arg1),
9114                                            variable2, cst));
9115         }
9116
9117       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9118                              ? MINUS_EXPR : PLUS_EXPR,
9119                              const1, const2);
9120       if (!TREE_OVERFLOW (cst)
9121           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
9122         {
9123           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9124           return fold_build2_loc (loc, code, type,
9125                               fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
9126                                            variable1, cst),
9127                               variable2);
9128         }
9129     }
9130
9131   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9132      signed arithmetic case.  That form is created by the compiler
9133      often enough for folding it to be of value.  One example is in
9134      computing loop trip counts after Operator Strength Reduction.  */
9135   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9136       && TREE_CODE (arg0) == MULT_EXPR
9137       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9138           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9139       && integer_zerop (arg1))
9140     {
9141       tree const1 = TREE_OPERAND (arg0, 1);
9142       tree const2 = arg1;                       /* zero */
9143       tree variable1 = TREE_OPERAND (arg0, 0);
9144       enum tree_code cmp_code = code;
9145
9146       /* Handle unfolded multiplication by zero.  */
9147       if (integer_zerop (const1))
9148         return fold_build2_loc (loc, cmp_code, type, const1, const2);
9149
9150       fold_overflow_warning (("assuming signed overflow does not occur when "
9151                               "eliminating multiplication in comparison "
9152                               "with zero"),
9153                              WARN_STRICT_OVERFLOW_COMPARISON);
9154
9155       /* If const1 is negative we swap the sense of the comparison.  */
9156       if (tree_int_cst_sgn (const1) < 0)
9157         cmp_code = swap_tree_comparison (cmp_code);
9158
9159       return fold_build2_loc (loc, cmp_code, type, variable1, const2);
9160     }
9161
9162   tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
9163   if (tem)
9164     return tem;
9165
9166   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9167     {
9168       tree targ0 = strip_float_extensions (arg0);
9169       tree targ1 = strip_float_extensions (arg1);
9170       tree newtype = TREE_TYPE (targ0);
9171
9172       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9173         newtype = TREE_TYPE (targ1);
9174
9175       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
9176       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9177         return fold_build2_loc (loc, code, type,
9178                             fold_convert_loc (loc, newtype, targ0),
9179                             fold_convert_loc (loc, newtype, targ1));
9180
9181       /* (-a) CMP (-b) -> b CMP a  */
9182       if (TREE_CODE (arg0) == NEGATE_EXPR
9183           && TREE_CODE (arg1) == NEGATE_EXPR)
9184         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
9185                             TREE_OPERAND (arg0, 0));
9186
9187       if (TREE_CODE (arg1) == REAL_CST)
9188         {
9189           REAL_VALUE_TYPE cst;
9190           cst = TREE_REAL_CST (arg1);
9191
9192           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
9193           if (TREE_CODE (arg0) == NEGATE_EXPR)
9194             return fold_build2_loc (loc, swap_tree_comparison (code), type,
9195                                 TREE_OPERAND (arg0, 0),
9196                                 build_real (TREE_TYPE (arg1),
9197                                             real_value_negate (&cst)));
9198
9199           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
9200           /* a CMP (-0) -> a CMP 0  */
9201           if (REAL_VALUE_MINUS_ZERO (cst))
9202             return fold_build2_loc (loc, code, type, arg0,
9203                                 build_real (TREE_TYPE (arg1), dconst0));
9204
9205           /* x != NaN is always true, other ops are always false.  */
9206           if (REAL_VALUE_ISNAN (cst)
9207               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9208             {
9209               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9210               return omit_one_operand_loc (loc, type, tem, arg0);
9211             }
9212
9213           /* Fold comparisons against infinity.  */
9214           if (REAL_VALUE_ISINF (cst)
9215               && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9216             {
9217               tem = fold_inf_compare (loc, code, type, arg0, arg1);
9218               if (tem != NULL_TREE)
9219                 return tem;
9220             }
9221         }
9222
9223       /* If this is a comparison of a real constant with a PLUS_EXPR
9224          or a MINUS_EXPR of a real constant, we can convert it into a
9225          comparison with a revised real constant as long as no overflow
9226          occurs when unsafe_math_optimizations are enabled.  */
9227       if (flag_unsafe_math_optimizations
9228           && TREE_CODE (arg1) == REAL_CST
9229           && (TREE_CODE (arg0) == PLUS_EXPR
9230               || TREE_CODE (arg0) == MINUS_EXPR)
9231           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9232           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9233                                       ? MINUS_EXPR : PLUS_EXPR,
9234                                       arg1, TREE_OPERAND (arg0, 1)))
9235           && !TREE_OVERFLOW (tem))
9236         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9237
9238       /* Likewise, we can simplify a comparison of a real constant with
9239          a MINUS_EXPR whose first operand is also a real constant, i.e.
9240          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
9241          floating-point types only if -fassociative-math is set.  */
9242       if (flag_associative_math
9243           && TREE_CODE (arg1) == REAL_CST
9244           && TREE_CODE (arg0) == MINUS_EXPR
9245           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9246           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9247                                       arg1))
9248           && !TREE_OVERFLOW (tem))
9249         return fold_build2_loc (loc, swap_tree_comparison (code), type,
9250                             TREE_OPERAND (arg0, 1), tem);
9251
9252       /* Fold comparisons against built-in math functions.  */
9253       if (TREE_CODE (arg1) == REAL_CST
9254           && flag_unsafe_math_optimizations
9255           && ! flag_errno_math)
9256         {
9257           enum built_in_function fcode = builtin_mathfn_code (arg0);
9258
9259           if (fcode != END_BUILTINS)
9260             {
9261               tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
9262               if (tem != NULL_TREE)
9263                 return tem;
9264             }
9265         }
9266     }
9267
9268   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9269       && CONVERT_EXPR_P (arg0))
9270     {
9271       /* If we are widening one operand of an integer comparison,
9272          see if the other operand is similarly being widened.  Perhaps we
9273          can do the comparison in the narrower type.  */
9274       tem = fold_widened_comparison (loc, code, type, arg0, arg1);
9275       if (tem)
9276         return tem;
9277
9278       /* Or if we are changing signedness.  */
9279       tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
9280       if (tem)
9281         return tem;
9282     }
9283
9284   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9285      constant, we can simplify it.  */
9286   if (TREE_CODE (arg1) == INTEGER_CST
9287       && (TREE_CODE (arg0) == MIN_EXPR
9288           || TREE_CODE (arg0) == MAX_EXPR)
9289       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9290     {
9291       tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9292       if (tem)
9293         return tem;
9294     }
9295
9296   /* Simplify comparison of something with itself.  (For IEEE
9297      floating-point, we can only do some of these simplifications.)  */
9298   if (operand_equal_p (arg0, arg1, 0))
9299     {
9300       switch (code)
9301         {
9302         case EQ_EXPR:
9303           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9304               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9305             return constant_boolean_node (1, type);
9306           break;
9307
9308         case GE_EXPR:
9309         case LE_EXPR:
9310           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9311               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9312             return constant_boolean_node (1, type);
9313           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9314
9315         case NE_EXPR:
9316           /* For NE, we can only do this simplification if integer
9317              or we don't honor IEEE floating point NaNs.  */
9318           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9319               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9320             break;
9321           /* ... fall through ...  */
9322         case GT_EXPR:
9323         case LT_EXPR:
9324           return constant_boolean_node (0, type);
9325         default:
9326           gcc_unreachable ();
9327         }
9328     }
9329
9330   /* If we are comparing an expression that just has comparisons
9331      of two integer values, arithmetic expressions of those comparisons,
9332      and constants, we can simplify it.  There are only three cases
9333      to check: the two values can either be equal, the first can be
9334      greater, or the second can be greater.  Fold the expression for
9335      those three values.  Since each value must be 0 or 1, we have
9336      eight possibilities, each of which corresponds to the constant 0
9337      or 1 or one of the six possible comparisons.
9338
9339      This handles common cases like (a > b) == 0 but also handles
9340      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9341      occur in macroized code.  */
9342
9343   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9344     {
9345       tree cval1 = 0, cval2 = 0;
9346       int save_p = 0;
9347
9348       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9349           /* Don't handle degenerate cases here; they should already
9350              have been handled anyway.  */
9351           && cval1 != 0 && cval2 != 0
9352           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9353           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9354           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9355           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9356           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9357           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9358                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9359         {
9360           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9361           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9362
9363           /* We can't just pass T to eval_subst in case cval1 or cval2
9364              was the same as ARG1.  */
9365
9366           tree high_result
9367                 = fold_build2_loc (loc, code, type,
9368                                eval_subst (loc, arg0, cval1, maxval,
9369                                            cval2, minval),
9370                                arg1);
9371           tree equal_result
9372                 = fold_build2_loc (loc, code, type,
9373                                eval_subst (loc, arg0, cval1, maxval,
9374                                            cval2, maxval),
9375                                arg1);
9376           tree low_result
9377                 = fold_build2_loc (loc, code, type,
9378                                eval_subst (loc, arg0, cval1, minval,
9379                                            cval2, maxval),
9380                                arg1);
9381
9382           /* All three of these results should be 0 or 1.  Confirm they are.
9383              Then use those values to select the proper code to use.  */
9384
9385           if (TREE_CODE (high_result) == INTEGER_CST
9386               && TREE_CODE (equal_result) == INTEGER_CST
9387               && TREE_CODE (low_result) == INTEGER_CST)
9388             {
9389               /* Make a 3-bit mask with the high-order bit being the
9390                  value for `>', the next for '=', and the low for '<'.  */
9391               switch ((integer_onep (high_result) * 4)
9392                       + (integer_onep (equal_result) * 2)
9393                       + integer_onep (low_result))
9394                 {
9395                 case 0:
9396                   /* Always false.  */
9397                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9398                 case 1:
9399                   code = LT_EXPR;
9400                   break;
9401                 case 2:
9402                   code = EQ_EXPR;
9403                   break;
9404                 case 3:
9405                   code = LE_EXPR;
9406                   break;
9407                 case 4:
9408                   code = GT_EXPR;
9409                   break;
9410                 case 5:
9411                   code = NE_EXPR;
9412                   break;
9413                 case 6:
9414                   code = GE_EXPR;
9415                   break;
9416                 case 7:
9417                   /* Always true.  */
9418                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9419                 }
9420
9421               if (save_p)
9422                 {
9423                   tem = save_expr (build2 (code, type, cval1, cval2));
9424                   SET_EXPR_LOCATION (tem, loc);
9425                   return tem;
9426                 }
9427               return fold_build2_loc (loc, code, type, cval1, cval2);
9428             }
9429         }
9430     }
9431
9432   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9433      into a single range test.  */
9434   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9435        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9436       && TREE_CODE (arg1) == INTEGER_CST
9437       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9438       && !integer_zerop (TREE_OPERAND (arg0, 1))
9439       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9440       && !TREE_OVERFLOW (arg1))
9441     {
9442       tem = fold_div_compare (loc, code, type, arg0, arg1);
9443       if (tem != NULL_TREE)
9444         return tem;
9445     }
9446
9447   /* Fold ~X op ~Y as Y op X.  */
9448   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9449       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9450     {
9451       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9452       return fold_build2_loc (loc, code, type,
9453                           fold_convert_loc (loc, cmp_type,
9454                                             TREE_OPERAND (arg1, 0)),
9455                           TREE_OPERAND (arg0, 0));
9456     }
9457
9458   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9459   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9460       && TREE_CODE (arg1) == INTEGER_CST)
9461     {
9462       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9463       return fold_build2_loc (loc, swap_tree_comparison (code), type,
9464                           TREE_OPERAND (arg0, 0),
9465                           fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9466                                        fold_convert_loc (loc, cmp_type, arg1)));
9467     }
9468
9469   return NULL_TREE;
9470 }
9471
9472
9473 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9474    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9475    argument EXPR represents the expression "z" of type TYPE.  */
9476
9477 static tree
9478 fold_mult_zconjz (location_t loc, tree type, tree expr)
9479 {
9480   tree itype = TREE_TYPE (type);
9481   tree rpart, ipart, tem;
9482
9483   if (TREE_CODE (expr) == COMPLEX_EXPR)
9484     {
9485       rpart = TREE_OPERAND (expr, 0);
9486       ipart = TREE_OPERAND (expr, 1);
9487     }
9488   else if (TREE_CODE (expr) == COMPLEX_CST)
9489     {
9490       rpart = TREE_REALPART (expr);
9491       ipart = TREE_IMAGPART (expr);
9492     }
9493   else
9494     {
9495       expr = save_expr (expr);
9496       rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9497       ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9498     }
9499
9500   rpart = save_expr (rpart);
9501   ipart = save_expr (ipart);
9502   tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9503                      fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9504                      fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9505   return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9506                           build_zero_cst (itype));
9507 }
9508
9509
9510 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9511    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9512    guarantees that P and N have the same least significant log2(M) bits.
9513    N is not otherwise constrained.  In particular, N is not normalized to
9514    0 <= N < M as is common.  In general, the precise value of P is unknown.
9515    M is chosen as large as possible such that constant N can be determined.
9516
9517    Returns M and sets *RESIDUE to N.
9518
9519    If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9520    account.  This is not always possible due to PR 35705.
9521  */
9522
9523 static unsigned HOST_WIDE_INT
9524 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9525                                  bool allow_func_align)
9526 {
9527   enum tree_code code;
9528
9529   *residue = 0;
9530
9531   code = TREE_CODE (expr);
9532   if (code == ADDR_EXPR)
9533     {
9534       unsigned int bitalign;
9535       bitalign = get_object_alignment_1 (TREE_OPERAND (expr, 0), residue);
9536       *residue /= BITS_PER_UNIT;
9537       return bitalign / BITS_PER_UNIT;
9538     }
9539   else if (code == POINTER_PLUS_EXPR)
9540     {
9541       tree op0, op1;
9542       unsigned HOST_WIDE_INT modulus;
9543       enum tree_code inner_code;
9544
9545       op0 = TREE_OPERAND (expr, 0);
9546       STRIP_NOPS (op0);
9547       modulus = get_pointer_modulus_and_residue (op0, residue,
9548                                                  allow_func_align);
9549
9550       op1 = TREE_OPERAND (expr, 1);
9551       STRIP_NOPS (op1);
9552       inner_code = TREE_CODE (op1);
9553       if (inner_code == INTEGER_CST)
9554         {
9555           *residue += TREE_INT_CST_LOW (op1);
9556           return modulus;
9557         }
9558       else if (inner_code == MULT_EXPR)
9559         {
9560           op1 = TREE_OPERAND (op1, 1);
9561           if (TREE_CODE (op1) == INTEGER_CST)
9562             {
9563               unsigned HOST_WIDE_INT align;
9564
9565               /* Compute the greatest power-of-2 divisor of op1.  */
9566               align = TREE_INT_CST_LOW (op1);
9567               align &= -align;
9568
9569               /* If align is non-zero and less than *modulus, replace
9570                  *modulus with align., If align is 0, then either op1 is 0
9571                  or the greatest power-of-2 divisor of op1 doesn't fit in an
9572                  unsigned HOST_WIDE_INT.  In either case, no additional
9573                  constraint is imposed.  */
9574               if (align)
9575                 modulus = MIN (modulus, align);
9576
9577               return modulus;
9578             }
9579         }
9580     }
9581
9582   /* If we get here, we were unable to determine anything useful about the
9583      expression.  */
9584   return 1;
9585 }
9586
9587 /* Helper function for fold_vec_perm.  Store elements of VECTOR_CST or
9588    CONSTRUCTOR ARG into array ELTS and return true if successful.  */
9589
9590 static bool
9591 vec_cst_ctor_to_array (tree arg, tree *elts)
9592 {
9593   unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
9594
9595   if (TREE_CODE (arg) == VECTOR_CST)
9596     {
9597       tree t;
9598
9599       for (i = 0, t = TREE_VECTOR_CST_ELTS (arg);
9600            i < nelts && t; i++, t = TREE_CHAIN (t))
9601         elts[i] = TREE_VALUE (t);
9602       if (t)
9603         return false;
9604     }
9605   else if (TREE_CODE (arg) == CONSTRUCTOR)
9606     {
9607       constructor_elt *elt;
9608
9609       FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (arg), i, elt)
9610         if (i >= nelts)
9611           return false;
9612         else
9613           elts[i] = elt->value;
9614     }
9615   else
9616     return false;
9617   for (; i < nelts; i++)
9618     elts[i]
9619       = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
9620   return true;
9621 }
9622
9623 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
9624    selector.  Return the folded VECTOR_CST or CONSTRUCTOR if successful,
9625    NULL_TREE otherwise.  */
9626
9627 static tree
9628 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
9629 {
9630   unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
9631   tree *elts;
9632   bool need_ctor = false;
9633
9634   gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
9635               && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
9636   if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
9637       || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
9638     return NULL_TREE;
9639
9640   elts = XALLOCAVEC (tree, nelts * 3);
9641   if (!vec_cst_ctor_to_array (arg0, elts)
9642       || !vec_cst_ctor_to_array (arg1, elts + nelts))
9643     return NULL_TREE;
9644
9645   for (i = 0; i < nelts; i++)
9646     {
9647       if (!CONSTANT_CLASS_P (elts[sel[i]]))
9648         need_ctor = true;
9649       elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
9650     }
9651
9652   if (need_ctor)
9653     {
9654       VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, nelts);
9655       for (i = 0; i < nelts; i++)
9656         CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
9657       return build_constructor (type, v);
9658     }
9659   else
9660     {
9661       tree vals = NULL_TREE;
9662       for (i = 0; i < nelts; i++)
9663         vals = tree_cons (NULL_TREE, elts[3 * nelts - i - 1], vals);
9664       return build_vector (type, vals);
9665     }
9666 }
9667
9668 /* Fold a binary expression of code CODE and type TYPE with operands
9669    OP0 and OP1.  LOC is the location of the resulting expression.
9670    Return the folded expression if folding is successful.  Otherwise,
9671    return NULL_TREE.  */
9672
9673 tree
9674 fold_binary_loc (location_t loc,
9675              enum tree_code code, tree type, tree op0, tree op1)
9676 {
9677   enum tree_code_class kind = TREE_CODE_CLASS (code);
9678   tree arg0, arg1, tem;
9679   tree t1 = NULL_TREE;
9680   bool strict_overflow_p;
9681
9682   gcc_assert (IS_EXPR_CODE_CLASS (kind)
9683               && TREE_CODE_LENGTH (code) == 2
9684               && op0 != NULL_TREE
9685               && op1 != NULL_TREE);
9686
9687   arg0 = op0;
9688   arg1 = op1;
9689
9690   /* Strip any conversions that don't change the mode.  This is
9691      safe for every expression, except for a comparison expression
9692      because its signedness is derived from its operands.  So, in
9693      the latter case, only strip conversions that don't change the
9694      signedness.  MIN_EXPR/MAX_EXPR also need signedness of arguments
9695      preserved.
9696
9697      Note that this is done as an internal manipulation within the
9698      constant folder, in order to find the simplest representation
9699      of the arguments so that their form can be studied.  In any
9700      cases, the appropriate type conversions should be put back in
9701      the tree that will get out of the constant folder.  */
9702
9703   if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9704     {
9705       STRIP_SIGN_NOPS (arg0);
9706       STRIP_SIGN_NOPS (arg1);
9707     }
9708   else
9709     {
9710       STRIP_NOPS (arg0);
9711       STRIP_NOPS (arg1);
9712     }
9713
9714   /* Note that TREE_CONSTANT isn't enough: static var addresses are
9715      constant but we can't do arithmetic on them.  */
9716   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9717       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9718       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9719       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9720       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9721       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9722     {
9723       if (kind == tcc_binary)
9724         {
9725           /* Make sure type and arg0 have the same saturating flag.  */
9726           gcc_assert (TYPE_SATURATING (type)
9727                       == TYPE_SATURATING (TREE_TYPE (arg0)));
9728           tem = const_binop (code, arg0, arg1);
9729         }
9730       else if (kind == tcc_comparison)
9731         tem = fold_relational_const (code, type, arg0, arg1);
9732       else
9733         tem = NULL_TREE;
9734
9735       if (tem != NULL_TREE)
9736         {
9737           if (TREE_TYPE (tem) != type)
9738             tem = fold_convert_loc (loc, type, tem);
9739           return tem;
9740         }
9741     }
9742
9743   /* If this is a commutative operation, and ARG0 is a constant, move it
9744      to ARG1 to reduce the number of tests below.  */
9745   if (commutative_tree_code (code)
9746       && tree_swap_operands_p (arg0, arg1, true))
9747     return fold_build2_loc (loc, code, type, op1, op0);
9748
9749   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9750
9751      First check for cases where an arithmetic operation is applied to a
9752      compound, conditional, or comparison operation.  Push the arithmetic
9753      operation inside the compound or conditional to see if any folding
9754      can then be done.  Convert comparison to conditional for this purpose.
9755      The also optimizes non-constant cases that used to be done in
9756      expand_expr.
9757
9758      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9759      one of the operands is a comparison and the other is a comparison, a
9760      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
9761      code below would make the expression more complex.  Change it to a
9762      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
9763      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
9764
9765   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9766        || code == EQ_EXPR || code == NE_EXPR)
9767       && ((truth_value_p (TREE_CODE (arg0))
9768            && (truth_value_p (TREE_CODE (arg1))
9769                || (TREE_CODE (arg1) == BIT_AND_EXPR
9770                    && integer_onep (TREE_OPERAND (arg1, 1)))))
9771           || (truth_value_p (TREE_CODE (arg1))
9772               && (truth_value_p (TREE_CODE (arg0))
9773                   || (TREE_CODE (arg0) == BIT_AND_EXPR
9774                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
9775     {
9776       tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9777                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9778                          : TRUTH_XOR_EXPR,
9779                          boolean_type_node,
9780                          fold_convert_loc (loc, boolean_type_node, arg0),
9781                          fold_convert_loc (loc, boolean_type_node, arg1));
9782
9783       if (code == EQ_EXPR)
9784         tem = invert_truthvalue_loc (loc, tem);
9785
9786       return fold_convert_loc (loc, type, tem);
9787     }
9788
9789   if (TREE_CODE_CLASS (code) == tcc_binary
9790       || TREE_CODE_CLASS (code) == tcc_comparison)
9791     {
9792       if (TREE_CODE (arg0) == COMPOUND_EXPR)
9793         {
9794           tem = fold_build2_loc (loc, code, type,
9795                              fold_convert_loc (loc, TREE_TYPE (op0),
9796                                                TREE_OPERAND (arg0, 1)), op1);
9797           return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9798                              tem);
9799         }
9800       if (TREE_CODE (arg1) == COMPOUND_EXPR
9801           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9802         {
9803           tem = fold_build2_loc (loc, code, type, op0,
9804                              fold_convert_loc (loc, TREE_TYPE (op1),
9805                                                TREE_OPERAND (arg1, 1)));
9806           return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9807                              tem);
9808         }
9809
9810       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9811         {
9812           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9813                                                      arg0, arg1,
9814                                                      /*cond_first_p=*/1);
9815           if (tem != NULL_TREE)
9816             return tem;
9817         }
9818
9819       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9820         {
9821           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9822                                                      arg1, arg0,
9823                                                      /*cond_first_p=*/0);
9824           if (tem != NULL_TREE)
9825             return tem;
9826         }
9827     }
9828
9829   switch (code)
9830     {
9831     case MEM_REF:
9832       /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2].  */
9833       if (TREE_CODE (arg0) == ADDR_EXPR
9834           && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9835         {
9836           tree iref = TREE_OPERAND (arg0, 0);
9837           return fold_build2 (MEM_REF, type,
9838                               TREE_OPERAND (iref, 0),
9839                               int_const_binop (PLUS_EXPR, arg1,
9840                                                TREE_OPERAND (iref, 1)));
9841         }
9842
9843       /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2].  */
9844       if (TREE_CODE (arg0) == ADDR_EXPR
9845           && handled_component_p (TREE_OPERAND (arg0, 0)))
9846         {
9847           tree base;
9848           HOST_WIDE_INT coffset;
9849           base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9850                                                 &coffset);
9851           if (!base)
9852             return NULL_TREE;
9853           return fold_build2 (MEM_REF, type,
9854                               build_fold_addr_expr (base),
9855                               int_const_binop (PLUS_EXPR, arg1,
9856                                                size_int (coffset)));
9857         }
9858
9859       return NULL_TREE;
9860
9861     case POINTER_PLUS_EXPR:
9862       /* 0 +p index -> (type)index */
9863       if (integer_zerop (arg0))
9864         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9865
9866       /* PTR +p 0 -> PTR */
9867       if (integer_zerop (arg1))
9868         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9869
9870       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
9871       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9872            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9873         return fold_convert_loc (loc, type,
9874                                  fold_build2_loc (loc, PLUS_EXPR, sizetype,
9875                                               fold_convert_loc (loc, sizetype,
9876                                                                 arg1),
9877                                               fold_convert_loc (loc, sizetype,
9878                                                                 arg0)));
9879
9880       /* (PTR +p B) +p A -> PTR +p (B + A) */
9881       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9882         {
9883           tree inner;
9884           tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
9885           tree arg00 = TREE_OPERAND (arg0, 0);
9886           inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
9887                                arg01, fold_convert_loc (loc, sizetype, arg1));
9888           return fold_convert_loc (loc, type,
9889                                    fold_build_pointer_plus_loc (loc,
9890                                                                 arg00, inner));
9891         }
9892
9893       /* PTR_CST +p CST -> CST1 */
9894       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9895         return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
9896                             fold_convert_loc (loc, 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 (loc, arg0,
9904                                         fold_convert_loc (loc, sizetype, arg1));
9905           if (tem)
9906             return fold_convert_loc (loc, type, tem);
9907         }
9908
9909       return NULL_TREE;
9910
9911     case PLUS_EXPR:
9912       /* A + (-B) -> A - B */
9913       if (TREE_CODE (arg1) == NEGATE_EXPR)
9914         return fold_build2_loc (loc, MINUS_EXPR, type,
9915                             fold_convert_loc (loc, type, arg0),
9916                             fold_convert_loc (loc, type,
9917                                               TREE_OPERAND (arg1, 0)));
9918       /* (-A) + B -> B - A */
9919       if (TREE_CODE (arg0) == NEGATE_EXPR
9920           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9921         return fold_build2_loc (loc, MINUS_EXPR, type,
9922                             fold_convert_loc (loc, type, arg1),
9923                             fold_convert_loc (loc, type,
9924                                               TREE_OPERAND (arg0, 0)));
9925
9926       if (INTEGRAL_TYPE_P (type))
9927         {
9928           /* Convert ~A + 1 to -A.  */
9929           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9930               && integer_onep (arg1))
9931             return fold_build1_loc (loc, NEGATE_EXPR, type,
9932                                 fold_convert_loc (loc, type,
9933                                                   TREE_OPERAND (arg0, 0)));
9934
9935           /* ~X + X is -1.  */
9936           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9937               && !TYPE_OVERFLOW_TRAPS (type))
9938             {
9939               tree tem = TREE_OPERAND (arg0, 0);
9940
9941               STRIP_NOPS (tem);
9942               if (operand_equal_p (tem, arg1, 0))
9943                 {
9944                   t1 = build_int_cst_type (type, -1);
9945                   return omit_one_operand_loc (loc, type, t1, arg1);
9946                 }
9947             }
9948
9949           /* X + ~X is -1.  */
9950           if (TREE_CODE (arg1) == BIT_NOT_EXPR
9951               && !TYPE_OVERFLOW_TRAPS (type))
9952             {
9953               tree tem = TREE_OPERAND (arg1, 0);
9954
9955               STRIP_NOPS (tem);
9956               if (operand_equal_p (arg0, tem, 0))
9957                 {
9958                   t1 = build_int_cst_type (type, -1);
9959                   return omit_one_operand_loc (loc, type, t1, arg0);
9960                 }
9961             }
9962
9963           /* X + (X / CST) * -CST is X % CST.  */
9964           if (TREE_CODE (arg1) == MULT_EXPR
9965               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9966               && operand_equal_p (arg0,
9967                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9968             {
9969               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9970               tree cst1 = TREE_OPERAND (arg1, 1);
9971               tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9972                                       cst1, cst0);
9973               if (sum && integer_zerop (sum))
9974                 return fold_convert_loc (loc, type,
9975                                          fold_build2_loc (loc, TRUNC_MOD_EXPR,
9976                                                       TREE_TYPE (arg0), arg0,
9977                                                       cst0));
9978             }
9979         }
9980
9981       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9982          same or one.  Make sure type is not saturating.
9983          fold_plusminus_mult_expr will re-associate.  */
9984       if ((TREE_CODE (arg0) == MULT_EXPR
9985            || TREE_CODE (arg1) == MULT_EXPR)
9986           && !TYPE_SATURATING (type)
9987           && (!FLOAT_TYPE_P (type) || flag_associative_math))
9988         {
9989           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9990           if (tem)
9991             return tem;
9992         }
9993
9994       if (! FLOAT_TYPE_P (type))
9995         {
9996           if (integer_zerop (arg1))
9997             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9998
9999           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
10000              with a constant, and the two constants have no bits in common,
10001              we should treat this as a BIT_IOR_EXPR since this may produce more
10002              simplifications.  */
10003           if (TREE_CODE (arg0) == BIT_AND_EXPR
10004               && TREE_CODE (arg1) == BIT_AND_EXPR
10005               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10006               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10007               && integer_zerop (const_binop (BIT_AND_EXPR,
10008                                              TREE_OPERAND (arg0, 1),
10009                                              TREE_OPERAND (arg1, 1))))
10010             {
10011               code = BIT_IOR_EXPR;
10012               goto bit_ior;
10013             }
10014
10015           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
10016              (plus (plus (mult) (mult)) (foo)) so that we can
10017              take advantage of the factoring cases below.  */
10018           if (TYPE_OVERFLOW_WRAPS (type)
10019               && (((TREE_CODE (arg0) == PLUS_EXPR
10020                     || TREE_CODE (arg0) == MINUS_EXPR)
10021                    && TREE_CODE (arg1) == MULT_EXPR)
10022                   || ((TREE_CODE (arg1) == PLUS_EXPR
10023                        || TREE_CODE (arg1) == MINUS_EXPR)
10024                       && TREE_CODE (arg0) == MULT_EXPR)))
10025             {
10026               tree parg0, parg1, parg, marg;
10027               enum tree_code pcode;
10028
10029               if (TREE_CODE (arg1) == MULT_EXPR)
10030                 parg = arg0, marg = arg1;
10031               else
10032                 parg = arg1, marg = arg0;
10033               pcode = TREE_CODE (parg);
10034               parg0 = TREE_OPERAND (parg, 0);
10035               parg1 = TREE_OPERAND (parg, 1);
10036               STRIP_NOPS (parg0);
10037               STRIP_NOPS (parg1);
10038
10039               if (TREE_CODE (parg0) == MULT_EXPR
10040                   && TREE_CODE (parg1) != MULT_EXPR)
10041                 return fold_build2_loc (loc, pcode, type,
10042                                     fold_build2_loc (loc, PLUS_EXPR, type,
10043                                                  fold_convert_loc (loc, type,
10044                                                                    parg0),
10045                                                  fold_convert_loc (loc, type,
10046                                                                    marg)),
10047                                     fold_convert_loc (loc, type, parg1));
10048               if (TREE_CODE (parg0) != MULT_EXPR
10049                   && TREE_CODE (parg1) == MULT_EXPR)
10050                 return
10051                   fold_build2_loc (loc, PLUS_EXPR, type,
10052                                fold_convert_loc (loc, type, parg0),
10053                                fold_build2_loc (loc, pcode, type,
10054                                             fold_convert_loc (loc, type, marg),
10055                                             fold_convert_loc (loc, type,
10056                                                               parg1)));
10057             }
10058         }
10059       else
10060         {
10061           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
10062           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
10063             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10064
10065           /* Likewise if the operands are reversed.  */
10066           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10067             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10068
10069           /* Convert X + -C into X - C.  */
10070           if (TREE_CODE (arg1) == REAL_CST
10071               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
10072             {
10073               tem = fold_negate_const (arg1, type);
10074               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
10075                 return fold_build2_loc (loc, MINUS_EXPR, type,
10076                                     fold_convert_loc (loc, type, arg0),
10077                                     fold_convert_loc (loc, type, tem));
10078             }
10079
10080           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10081              to __complex__ ( x, y ).  This is not the same for SNaNs or
10082              if signed zeros are involved.  */
10083           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10084               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10085               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10086             {
10087               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10088               tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10089               tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10090               bool arg0rz = false, arg0iz = false;
10091               if ((arg0r && (arg0rz = real_zerop (arg0r)))
10092                   || (arg0i && (arg0iz = real_zerop (arg0i))))
10093                 {
10094                   tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10095                   tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10096                   if (arg0rz && arg1i && real_zerop (arg1i))
10097                     {
10098                       tree rp = arg1r ? arg1r
10099                                   : build1 (REALPART_EXPR, rtype, arg1);
10100                       tree ip = arg0i ? arg0i
10101                                   : build1 (IMAGPART_EXPR, rtype, arg0);
10102                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10103                     }
10104                   else if (arg0iz && arg1r && real_zerop (arg1r))
10105                     {
10106                       tree rp = arg0r ? arg0r
10107                                   : build1 (REALPART_EXPR, rtype, arg0);
10108                       tree ip = arg1i ? arg1i
10109                                   : build1 (IMAGPART_EXPR, rtype, arg1);
10110                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10111                     }
10112                 }
10113             }
10114
10115           if (flag_unsafe_math_optimizations
10116               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10117               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10118               && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10119             return tem;
10120
10121           /* Convert x+x into x*2.0.  */
10122           if (operand_equal_p (arg0, arg1, 0)
10123               && SCALAR_FLOAT_TYPE_P (type))
10124             return fold_build2_loc (loc, MULT_EXPR, type, arg0,
10125                                 build_real (type, dconst2));
10126
10127           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
10128              We associate floats only if the user has specified
10129              -fassociative-math.  */
10130           if (flag_associative_math
10131               && TREE_CODE (arg1) == PLUS_EXPR
10132               && TREE_CODE (arg0) != MULT_EXPR)
10133             {
10134               tree tree10 = TREE_OPERAND (arg1, 0);
10135               tree tree11 = TREE_OPERAND (arg1, 1);
10136               if (TREE_CODE (tree11) == MULT_EXPR
10137                   && TREE_CODE (tree10) == MULT_EXPR)
10138                 {
10139                   tree tree0;
10140                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
10141                   return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
10142                 }
10143             }
10144           /* Convert (b*c + d*e) + a into b*c + (d*e +a).
10145              We associate floats only if the user has specified
10146              -fassociative-math.  */
10147           if (flag_associative_math
10148               && TREE_CODE (arg0) == PLUS_EXPR
10149               && TREE_CODE (arg1) != MULT_EXPR)
10150             {
10151               tree tree00 = TREE_OPERAND (arg0, 0);
10152               tree tree01 = TREE_OPERAND (arg0, 1);
10153               if (TREE_CODE (tree01) == MULT_EXPR
10154                   && TREE_CODE (tree00) == MULT_EXPR)
10155                 {
10156                   tree tree0;
10157                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
10158                   return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
10159                 }
10160             }
10161         }
10162
10163      bit_rotate:
10164       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10165          is a rotate of A by C1 bits.  */
10166       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10167          is a rotate of A by B bits.  */
10168       {
10169         enum tree_code code0, code1;
10170         tree rtype;
10171         code0 = TREE_CODE (arg0);
10172         code1 = TREE_CODE (arg1);
10173         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10174              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10175             && operand_equal_p (TREE_OPERAND (arg0, 0),
10176                                 TREE_OPERAND (arg1, 0), 0)
10177             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10178                 TYPE_UNSIGNED (rtype))
10179             /* Only create rotates in complete modes.  Other cases are not
10180                expanded properly.  */
10181             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
10182           {
10183             tree tree01, tree11;
10184             enum tree_code code01, code11;
10185
10186             tree01 = TREE_OPERAND (arg0, 1);
10187             tree11 = TREE_OPERAND (arg1, 1);
10188             STRIP_NOPS (tree01);
10189             STRIP_NOPS (tree11);
10190             code01 = TREE_CODE (tree01);
10191             code11 = TREE_CODE (tree11);
10192             if (code01 == INTEGER_CST
10193                 && code11 == INTEGER_CST
10194                 && TREE_INT_CST_HIGH (tree01) == 0
10195                 && TREE_INT_CST_HIGH (tree11) == 0
10196                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
10197                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10198               {
10199                 tem = build2_loc (loc, LROTATE_EXPR,
10200                                   TREE_TYPE (TREE_OPERAND (arg0, 0)),
10201                                   TREE_OPERAND (arg0, 0),
10202                                   code0 == LSHIFT_EXPR ? tree01 : tree11);
10203                 return fold_convert_loc (loc, type, tem);
10204               }
10205             else if (code11 == MINUS_EXPR)
10206               {
10207                 tree tree110, tree111;
10208                 tree110 = TREE_OPERAND (tree11, 0);
10209                 tree111 = TREE_OPERAND (tree11, 1);
10210                 STRIP_NOPS (tree110);
10211                 STRIP_NOPS (tree111);
10212                 if (TREE_CODE (tree110) == INTEGER_CST
10213                     && 0 == compare_tree_int (tree110,
10214                                               TYPE_PRECISION
10215                                               (TREE_TYPE (TREE_OPERAND
10216                                                           (arg0, 0))))
10217                     && operand_equal_p (tree01, tree111, 0))
10218                   return
10219                     fold_convert_loc (loc, type,
10220                                       build2 ((code0 == LSHIFT_EXPR
10221                                                ? LROTATE_EXPR
10222                                                : RROTATE_EXPR),
10223                                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
10224                                               TREE_OPERAND (arg0, 0), tree01));
10225               }
10226             else if (code01 == MINUS_EXPR)
10227               {
10228                 tree tree010, tree011;
10229                 tree010 = TREE_OPERAND (tree01, 0);
10230                 tree011 = TREE_OPERAND (tree01, 1);
10231                 STRIP_NOPS (tree010);
10232                 STRIP_NOPS (tree011);
10233                 if (TREE_CODE (tree010) == INTEGER_CST
10234                     && 0 == compare_tree_int (tree010,
10235                                               TYPE_PRECISION
10236                                               (TREE_TYPE (TREE_OPERAND
10237                                                           (arg0, 0))))
10238                     && operand_equal_p (tree11, tree011, 0))
10239                     return fold_convert_loc
10240                       (loc, type,
10241                        build2 ((code0 != LSHIFT_EXPR
10242                                 ? LROTATE_EXPR
10243                                 : RROTATE_EXPR),
10244                                TREE_TYPE (TREE_OPERAND (arg0, 0)),
10245                                TREE_OPERAND (arg0, 0), tree11));
10246               }
10247           }
10248       }
10249
10250     associate:
10251       /* In most languages, can't associate operations on floats through
10252          parentheses.  Rather than remember where the parentheses were, we
10253          don't associate floats at all, unless the user has specified
10254          -fassociative-math.
10255          And, we need to make sure type is not saturating.  */
10256
10257       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10258           && !TYPE_SATURATING (type))
10259         {
10260           tree var0, con0, lit0, minus_lit0;
10261           tree var1, con1, lit1, minus_lit1;
10262           bool ok = true;
10263
10264           /* Split both trees into variables, constants, and literals.  Then
10265              associate each group together, the constants with literals,
10266              then the result with variables.  This increases the chances of
10267              literals being recombined later and of generating relocatable
10268              expressions for the sum of a constant and literal.  */
10269           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10270           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10271                              code == MINUS_EXPR);
10272
10273           /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
10274           if (code == MINUS_EXPR)
10275             code = PLUS_EXPR;
10276
10277           /* With undefined overflow we can only associate constants with one
10278              variable, and constants whose association doesn't overflow.  */
10279           if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10280               || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10281             {
10282               if (var0 && var1)
10283                 {
10284                   tree tmp0 = var0;
10285                   tree tmp1 = var1;
10286
10287                   if (TREE_CODE (tmp0) == NEGATE_EXPR)
10288                     tmp0 = TREE_OPERAND (tmp0, 0);
10289                   if (TREE_CODE (tmp1) == NEGATE_EXPR)
10290                     tmp1 = TREE_OPERAND (tmp1, 0);
10291                   /* The only case we can still associate with two variables
10292                      is if they are the same, modulo negation.  */
10293                   if (!operand_equal_p (tmp0, tmp1, 0))
10294                     ok = false;
10295                 }
10296
10297               if (ok && lit0 && lit1)
10298                 {
10299                   tree tmp0 = fold_convert (type, lit0);
10300                   tree tmp1 = fold_convert (type, lit1);
10301
10302                   if (!TREE_OVERFLOW (tmp0) && !TREE_OVERFLOW (tmp1)
10303                       && TREE_OVERFLOW (fold_build2 (code, type, tmp0, tmp1)))
10304                     ok = false;
10305                 }
10306             }
10307
10308           /* Only do something if we found more than two objects.  Otherwise,
10309              nothing has changed and we risk infinite recursion.  */
10310           if (ok
10311               && (2 < ((var0 != 0) + (var1 != 0)
10312                        + (con0 != 0) + (con1 != 0)
10313                        + (lit0 != 0) + (lit1 != 0)
10314                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
10315             {
10316               var0 = associate_trees (loc, var0, var1, code, type);
10317               con0 = associate_trees (loc, con0, con1, code, type);
10318               lit0 = associate_trees (loc, lit0, lit1, code, type);
10319               minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type);
10320
10321               /* Preserve the MINUS_EXPR if the negative part of the literal is
10322                  greater than the positive part.  Otherwise, the multiplicative
10323                  folding code (i.e extract_muldiv) may be fooled in case
10324                  unsigned constants are subtracted, like in the following
10325                  example: ((X*2 + 4) - 8U)/2.  */
10326               if (minus_lit0 && lit0)
10327                 {
10328                   if (TREE_CODE (lit0) == INTEGER_CST
10329                       && TREE_CODE (minus_lit0) == INTEGER_CST
10330                       && tree_int_cst_lt (lit0, minus_lit0))
10331                     {
10332                       minus_lit0 = associate_trees (loc, minus_lit0, lit0,
10333                                                     MINUS_EXPR, type);
10334                       lit0 = 0;
10335                     }
10336                   else
10337                     {
10338                       lit0 = associate_trees (loc, lit0, minus_lit0,
10339                                               MINUS_EXPR, type);
10340                       minus_lit0 = 0;
10341                     }
10342                 }
10343               if (minus_lit0)
10344                 {
10345                   if (con0 == 0)
10346                     return
10347                       fold_convert_loc (loc, type,
10348                                         associate_trees (loc, var0, minus_lit0,
10349                                                          MINUS_EXPR, type));
10350                   else
10351                     {
10352                       con0 = associate_trees (loc, con0, minus_lit0,
10353                                               MINUS_EXPR, type);
10354                       return
10355                         fold_convert_loc (loc, type,
10356                                           associate_trees (loc, var0, con0,
10357                                                            PLUS_EXPR, type));
10358                     }
10359                 }
10360
10361               con0 = associate_trees (loc, con0, lit0, code, type);
10362               return
10363                 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10364                                                               code, type));
10365             }
10366         }
10367
10368       return NULL_TREE;
10369
10370     case MINUS_EXPR:
10371       /* Pointer simplifications for subtraction, simple reassociations. */
10372       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10373         {
10374           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10375           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10376               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10377             {
10378               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10379               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10380               tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10381               tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10382               return fold_build2_loc (loc, PLUS_EXPR, type,
10383                                   fold_build2_loc (loc, MINUS_EXPR, type,
10384                                                arg00, arg10),
10385                                   fold_build2_loc (loc, MINUS_EXPR, type,
10386                                                arg01, arg11));
10387             }
10388           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10389           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10390             {
10391               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10392               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10393               tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10394                                       fold_convert_loc (loc, type, arg1));
10395               if (tmp)
10396                 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10397             }
10398         }
10399       /* A - (-B) -> A + B */
10400       if (TREE_CODE (arg1) == NEGATE_EXPR)
10401         return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10402                             fold_convert_loc (loc, type,
10403                                               TREE_OPERAND (arg1, 0)));
10404       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10405       if (TREE_CODE (arg0) == NEGATE_EXPR
10406           && (FLOAT_TYPE_P (type)
10407               || INTEGRAL_TYPE_P (type))
10408           && negate_expr_p (arg1)
10409           && reorder_operands_p (arg0, arg1))
10410         return fold_build2_loc (loc, MINUS_EXPR, type,
10411                             fold_convert_loc (loc, type,
10412                                               negate_expr (arg1)),
10413                             fold_convert_loc (loc, type,
10414                                               TREE_OPERAND (arg0, 0)));
10415       /* Convert -A - 1 to ~A.  */
10416       if (INTEGRAL_TYPE_P (type)
10417           && TREE_CODE (arg0) == NEGATE_EXPR
10418           && integer_onep (arg1)
10419           && !TYPE_OVERFLOW_TRAPS (type))
10420         return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10421                             fold_convert_loc (loc, type,
10422                                               TREE_OPERAND (arg0, 0)));
10423
10424       /* Convert -1 - A to ~A.  */
10425       if (INTEGRAL_TYPE_P (type)
10426           && integer_all_onesp (arg0))
10427         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10428
10429
10430       /* X - (X / CST) * CST is X % CST.  */
10431       if (INTEGRAL_TYPE_P (type)
10432           && TREE_CODE (arg1) == MULT_EXPR
10433           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10434           && operand_equal_p (arg0,
10435                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10436           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10437                               TREE_OPERAND (arg1, 1), 0))
10438         return
10439           fold_convert_loc (loc, type,
10440                             fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10441                                          arg0, TREE_OPERAND (arg1, 1)));
10442
10443       if (! FLOAT_TYPE_P (type))
10444         {
10445           if (integer_zerop (arg0))
10446             return negate_expr (fold_convert_loc (loc, type, arg1));
10447           if (integer_zerop (arg1))
10448             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10449
10450           /* Fold A - (A & B) into ~B & A.  */
10451           if (!TREE_SIDE_EFFECTS (arg0)
10452               && TREE_CODE (arg1) == BIT_AND_EXPR)
10453             {
10454               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10455                 {
10456                   tree arg10 = fold_convert_loc (loc, type,
10457                                                  TREE_OPERAND (arg1, 0));
10458                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10459                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10460                                                    type, arg10),
10461                                       fold_convert_loc (loc, type, arg0));
10462                 }
10463               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10464                 {
10465                   tree arg11 = fold_convert_loc (loc,
10466                                                  type, TREE_OPERAND (arg1, 1));
10467                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10468                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10469                                                    type, arg11),
10470                                       fold_convert_loc (loc, type, arg0));
10471                 }
10472             }
10473
10474           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10475              any power of 2 minus 1.  */
10476           if (TREE_CODE (arg0) == BIT_AND_EXPR
10477               && TREE_CODE (arg1) == BIT_AND_EXPR
10478               && operand_equal_p (TREE_OPERAND (arg0, 0),
10479                                   TREE_OPERAND (arg1, 0), 0))
10480             {
10481               tree mask0 = TREE_OPERAND (arg0, 1);
10482               tree mask1 = TREE_OPERAND (arg1, 1);
10483               tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10484
10485               if (operand_equal_p (tem, mask1, 0))
10486                 {
10487                   tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10488                                      TREE_OPERAND (arg0, 0), mask1);
10489                   return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10490                 }
10491             }
10492         }
10493
10494       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10495       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10496         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10497
10498       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10499          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10500          (-ARG1 + ARG0) reduces to -ARG1.  */
10501       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10502         return negate_expr (fold_convert_loc (loc, type, arg1));
10503
10504       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10505          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10506          signed zeros are involved.  */
10507       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10508           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10509           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10510         {
10511           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10512           tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10513           tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10514           bool arg0rz = false, arg0iz = false;
10515           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10516               || (arg0i && (arg0iz = real_zerop (arg0i))))
10517             {
10518               tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10519               tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10520               if (arg0rz && arg1i && real_zerop (arg1i))
10521                 {
10522                   tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10523                                          arg1r ? arg1r
10524                                          : build1 (REALPART_EXPR, rtype, arg1));
10525                   tree ip = arg0i ? arg0i
10526                     : build1 (IMAGPART_EXPR, rtype, arg0);
10527                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10528                 }
10529               else if (arg0iz && arg1r && real_zerop (arg1r))
10530                 {
10531                   tree rp = arg0r ? arg0r
10532                     : build1 (REALPART_EXPR, rtype, arg0);
10533                   tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10534                                          arg1i ? arg1i
10535                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10536                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10537                 }
10538             }
10539         }
10540
10541       /* Fold &x - &x.  This can happen from &x.foo - &x.
10542          This is unsafe for certain floats even in non-IEEE formats.
10543          In IEEE, it is unsafe because it does wrong for NaNs.
10544          Also note that operand_equal_p is always false if an operand
10545          is volatile.  */
10546
10547       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10548           && operand_equal_p (arg0, arg1, 0))
10549         return build_zero_cst (type);
10550
10551       /* A - B -> A + (-B) if B is easily negatable.  */
10552       if (negate_expr_p (arg1)
10553           && ((FLOAT_TYPE_P (type)
10554                /* Avoid this transformation if B is a positive REAL_CST.  */
10555                && (TREE_CODE (arg1) != REAL_CST
10556                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10557               || INTEGRAL_TYPE_P (type)))
10558         return fold_build2_loc (loc, PLUS_EXPR, type,
10559                             fold_convert_loc (loc, type, arg0),
10560                             fold_convert_loc (loc, type,
10561                                               negate_expr (arg1)));
10562
10563       /* Try folding difference of addresses.  */
10564       {
10565         HOST_WIDE_INT diff;
10566
10567         if ((TREE_CODE (arg0) == ADDR_EXPR
10568              || TREE_CODE (arg1) == ADDR_EXPR)
10569             && ptr_difference_const (arg0, arg1, &diff))
10570           return build_int_cst_type (type, diff);
10571       }
10572
10573       /* Fold &a[i] - &a[j] to i-j.  */
10574       if (TREE_CODE (arg0) == ADDR_EXPR
10575           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10576           && TREE_CODE (arg1) == ADDR_EXPR
10577           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10578         {
10579           tree aref0 = TREE_OPERAND (arg0, 0);
10580           tree aref1 = TREE_OPERAND (arg1, 0);
10581           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10582                                TREE_OPERAND (aref1, 0), 0))
10583             {
10584               tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10585               tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10586               tree esz = array_ref_element_size (aref0);
10587               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10588               return fold_build2_loc (loc, MULT_EXPR, type, diff,
10589                                   fold_convert_loc (loc, type, esz));
10590
10591             }
10592         }
10593
10594       if (FLOAT_TYPE_P (type)
10595           && flag_unsafe_math_optimizations
10596           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10597           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10598           && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10599         return tem;
10600
10601       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10602          same or one.  Make sure type is not saturating.
10603          fold_plusminus_mult_expr will re-associate.  */
10604       if ((TREE_CODE (arg0) == MULT_EXPR
10605            || TREE_CODE (arg1) == MULT_EXPR)
10606           && !TYPE_SATURATING (type)
10607           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10608         {
10609           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10610           if (tem)
10611             return tem;
10612         }
10613
10614       goto associate;
10615
10616     case MULT_EXPR:
10617       /* (-A) * (-B) -> A * B  */
10618       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10619         return fold_build2_loc (loc, MULT_EXPR, type,
10620                             fold_convert_loc (loc, type,
10621                                               TREE_OPERAND (arg0, 0)),
10622                             fold_convert_loc (loc, type,
10623                                               negate_expr (arg1)));
10624       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10625         return fold_build2_loc (loc, MULT_EXPR, type,
10626                             fold_convert_loc (loc, type,
10627                                               negate_expr (arg0)),
10628                             fold_convert_loc (loc, type,
10629                                               TREE_OPERAND (arg1, 0)));
10630
10631       if (! FLOAT_TYPE_P (type))
10632         {
10633           if (integer_zerop (arg1))
10634             return omit_one_operand_loc (loc, type, arg1, arg0);
10635           if (integer_onep (arg1))
10636             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10637           /* Transform x * -1 into -x.  Make sure to do the negation
10638              on the original operand with conversions not stripped
10639              because we can only strip non-sign-changing conversions.  */
10640           if (integer_all_onesp (arg1))
10641             return fold_convert_loc (loc, type, negate_expr (op0));
10642           /* Transform x * -C into -x * C if x is easily negatable.  */
10643           if (TREE_CODE (arg1) == INTEGER_CST
10644               && tree_int_cst_sgn (arg1) == -1
10645               && negate_expr_p (arg0)
10646               && (tem = negate_expr (arg1)) != arg1
10647               && !TREE_OVERFLOW (tem))
10648             return fold_build2_loc (loc, MULT_EXPR, type,
10649                                 fold_convert_loc (loc, type,
10650                                                   negate_expr (arg0)),
10651                                 tem);
10652
10653           /* (a * (1 << b)) is (a << b)  */
10654           if (TREE_CODE (arg1) == LSHIFT_EXPR
10655               && integer_onep (TREE_OPERAND (arg1, 0)))
10656             return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10657                                 TREE_OPERAND (arg1, 1));
10658           if (TREE_CODE (arg0) == LSHIFT_EXPR
10659               && integer_onep (TREE_OPERAND (arg0, 0)))
10660             return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10661                                 TREE_OPERAND (arg0, 1));
10662
10663           /* (A + A) * C -> A * 2 * C  */
10664           if (TREE_CODE (arg0) == PLUS_EXPR
10665               && TREE_CODE (arg1) == INTEGER_CST
10666               && operand_equal_p (TREE_OPERAND (arg0, 0),
10667                                   TREE_OPERAND (arg0, 1), 0))
10668             return fold_build2_loc (loc, MULT_EXPR, type,
10669                                 omit_one_operand_loc (loc, type,
10670                                                   TREE_OPERAND (arg0, 0),
10671                                                   TREE_OPERAND (arg0, 1)),
10672                                 fold_build2_loc (loc, MULT_EXPR, type,
10673                                              build_int_cst (type, 2) , arg1));
10674
10675           strict_overflow_p = false;
10676           if (TREE_CODE (arg1) == INTEGER_CST
10677               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10678                                              &strict_overflow_p)))
10679             {
10680               if (strict_overflow_p)
10681                 fold_overflow_warning (("assuming signed overflow does not "
10682                                         "occur when simplifying "
10683                                         "multiplication"),
10684                                        WARN_STRICT_OVERFLOW_MISC);
10685               return fold_convert_loc (loc, type, tem);
10686             }
10687
10688           /* Optimize z * conj(z) for integer complex numbers.  */
10689           if (TREE_CODE (arg0) == CONJ_EXPR
10690               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10691             return fold_mult_zconjz (loc, type, arg1);
10692           if (TREE_CODE (arg1) == CONJ_EXPR
10693               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10694             return fold_mult_zconjz (loc, type, arg0);
10695         }
10696       else
10697         {
10698           /* Maybe fold x * 0 to 0.  The expressions aren't the same
10699              when x is NaN, since x * 0 is also NaN.  Nor are they the
10700              same in modes with signed zeros, since multiplying a
10701              negative value by 0 gives -0, not +0.  */
10702           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10703               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10704               && real_zerop (arg1))
10705             return omit_one_operand_loc (loc, type, arg1, arg0);
10706           /* In IEEE floating point, x*1 is not equivalent to x for snans.
10707              Likewise for complex arithmetic with signed zeros.  */
10708           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10709               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10710                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10711               && real_onep (arg1))
10712             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10713
10714           /* Transform x * -1.0 into -x.  */
10715           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10716               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10717                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10718               && real_minus_onep (arg1))
10719             return fold_convert_loc (loc, type, negate_expr (arg0));
10720
10721           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
10722              the result for floating point types due to rounding so it is applied
10723              only if -fassociative-math was specify.  */
10724           if (flag_associative_math
10725               && TREE_CODE (arg0) == RDIV_EXPR
10726               && TREE_CODE (arg1) == REAL_CST
10727               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10728             {
10729               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10730                                       arg1);
10731               if (tem)
10732                 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10733                                     TREE_OPERAND (arg0, 1));
10734             }
10735
10736           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
10737           if (operand_equal_p (arg0, arg1, 0))
10738             {
10739               tree tem = fold_strip_sign_ops (arg0);
10740               if (tem != NULL_TREE)
10741                 {
10742                   tem = fold_convert_loc (loc, type, tem);
10743                   return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
10744                 }
10745             }
10746
10747           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10748              This is not the same for NaNs or if signed zeros are
10749              involved.  */
10750           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10751               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10752               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10753               && TREE_CODE (arg1) == COMPLEX_CST
10754               && real_zerop (TREE_REALPART (arg1)))
10755             {
10756               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10757               if (real_onep (TREE_IMAGPART (arg1)))
10758                 return
10759                   fold_build2_loc (loc, COMPLEX_EXPR, type,
10760                                negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10761                                                              rtype, arg0)),
10762                                fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10763               else if (real_minus_onep (TREE_IMAGPART (arg1)))
10764                 return
10765                   fold_build2_loc (loc, COMPLEX_EXPR, type,
10766                                fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10767                                negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10768                                                              rtype, arg0)));
10769             }
10770
10771           /* Optimize z * conj(z) for floating point complex numbers.
10772              Guarded by flag_unsafe_math_optimizations as non-finite
10773              imaginary components don't produce scalar results.  */
10774           if (flag_unsafe_math_optimizations
10775               && TREE_CODE (arg0) == CONJ_EXPR
10776               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10777             return fold_mult_zconjz (loc, type, arg1);
10778           if (flag_unsafe_math_optimizations
10779               && TREE_CODE (arg1) == CONJ_EXPR
10780               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10781             return fold_mult_zconjz (loc, type, arg0);
10782
10783           if (flag_unsafe_math_optimizations)
10784             {
10785               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10786               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10787
10788               /* Optimizations of root(...)*root(...).  */
10789               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10790                 {
10791                   tree rootfn, arg;
10792                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10793                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10794
10795                   /* Optimize sqrt(x)*sqrt(x) as x.  */
10796                   if (BUILTIN_SQRT_P (fcode0)
10797                       && operand_equal_p (arg00, arg10, 0)
10798                       && ! HONOR_SNANS (TYPE_MODE (type)))
10799                     return arg00;
10800
10801                   /* Optimize root(x)*root(y) as root(x*y).  */
10802                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10803                   arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
10804                   return build_call_expr_loc (loc, rootfn, 1, arg);
10805                 }
10806
10807               /* Optimize expN(x)*expN(y) as expN(x+y).  */
10808               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10809                 {
10810                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10811                   tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10812                                           CALL_EXPR_ARG (arg0, 0),
10813                                           CALL_EXPR_ARG (arg1, 0));
10814                   return build_call_expr_loc (loc, expfn, 1, arg);
10815                 }
10816
10817               /* Optimizations of pow(...)*pow(...).  */
10818               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10819                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10820                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10821                 {
10822                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10823                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10824                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10825                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10826
10827                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
10828                   if (operand_equal_p (arg01, arg11, 0))
10829                     {
10830                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10831                       tree arg = fold_build2_loc (loc, MULT_EXPR, type,
10832                                               arg00, arg10);
10833                       return build_call_expr_loc (loc, powfn, 2, arg, arg01);
10834                     }
10835
10836                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
10837                   if (operand_equal_p (arg00, arg10, 0))
10838                     {
10839                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10840                       tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10841                                               arg01, arg11);
10842                       return build_call_expr_loc (loc, powfn, 2, arg00, arg);
10843                     }
10844                 }
10845
10846               /* Optimize tan(x)*cos(x) as sin(x).  */
10847               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10848                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10849                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10850                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10851                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10852                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10853                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10854                                       CALL_EXPR_ARG (arg1, 0), 0))
10855                 {
10856                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10857
10858                   if (sinfn != NULL_TREE)
10859                     return build_call_expr_loc (loc, sinfn, 1,
10860                                             CALL_EXPR_ARG (arg0, 0));
10861                 }
10862
10863               /* Optimize x*pow(x,c) as pow(x,c+1).  */
10864               if (fcode1 == BUILT_IN_POW
10865                   || fcode1 == BUILT_IN_POWF
10866                   || fcode1 == BUILT_IN_POWL)
10867                 {
10868                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10869                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10870                   if (TREE_CODE (arg11) == REAL_CST
10871                       && !TREE_OVERFLOW (arg11)
10872                       && operand_equal_p (arg0, arg10, 0))
10873                     {
10874                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10875                       REAL_VALUE_TYPE c;
10876                       tree arg;
10877
10878                       c = TREE_REAL_CST (arg11);
10879                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10880                       arg = build_real (type, c);
10881                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10882                     }
10883                 }
10884
10885               /* Optimize pow(x,c)*x as pow(x,c+1).  */
10886               if (fcode0 == BUILT_IN_POW
10887                   || fcode0 == BUILT_IN_POWF
10888                   || fcode0 == BUILT_IN_POWL)
10889                 {
10890                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10891                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10892                   if (TREE_CODE (arg01) == REAL_CST
10893                       && !TREE_OVERFLOW (arg01)
10894                       && operand_equal_p (arg1, arg00, 0))
10895                     {
10896                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10897                       REAL_VALUE_TYPE c;
10898                       tree arg;
10899
10900                       c = TREE_REAL_CST (arg01);
10901                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10902                       arg = build_real (type, c);
10903                       return build_call_expr_loc (loc, powfn, 2, arg1, arg);
10904                     }
10905                 }
10906
10907               /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x.  */
10908               if (!in_gimple_form
10909                   && optimize
10910                   && operand_equal_p (arg0, arg1, 0))
10911                 {
10912                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10913
10914                   if (powfn)
10915                     {
10916                       tree arg = build_real (type, dconst2);
10917                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10918                     }
10919                 }
10920             }
10921         }
10922       goto associate;
10923
10924     case BIT_IOR_EXPR:
10925     bit_ior:
10926       if (integer_all_onesp (arg1))
10927         return omit_one_operand_loc (loc, type, arg1, arg0);
10928       if (integer_zerop (arg1))
10929         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10930       if (operand_equal_p (arg0, arg1, 0))
10931         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10932
10933       /* ~X | X is -1.  */
10934       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10935           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10936         {
10937           t1 = build_zero_cst (type);
10938           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10939           return omit_one_operand_loc (loc, type, t1, arg1);
10940         }
10941
10942       /* X | ~X is -1.  */
10943       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10944           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10945         {
10946           t1 = build_zero_cst (type);
10947           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10948           return omit_one_operand_loc (loc, type, t1, arg0);
10949         }
10950
10951       /* Canonicalize (X & C1) | C2.  */
10952       if (TREE_CODE (arg0) == BIT_AND_EXPR
10953           && TREE_CODE (arg1) == INTEGER_CST
10954           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10955         {
10956           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10957           int width = TYPE_PRECISION (type), w;
10958           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10959           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10960           hi2 = TREE_INT_CST_HIGH (arg1);
10961           lo2 = TREE_INT_CST_LOW (arg1);
10962
10963           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
10964           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10965             return omit_one_operand_loc (loc, type, arg1,
10966                                      TREE_OPERAND (arg0, 0));
10967
10968           if (width > HOST_BITS_PER_WIDE_INT)
10969             {
10970               mhi = (unsigned HOST_WIDE_INT) -1
10971                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
10972               mlo = -1;
10973             }
10974           else
10975             {
10976               mhi = 0;
10977               mlo = (unsigned HOST_WIDE_INT) -1
10978                     >> (HOST_BITS_PER_WIDE_INT - width);
10979             }
10980
10981           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
10982           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10983             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10984                                 TREE_OPERAND (arg0, 0), arg1);
10985
10986           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10987              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10988              mode which allows further optimizations.  */
10989           hi1 &= mhi;
10990           lo1 &= mlo;
10991           hi2 &= mhi;
10992           lo2 &= mlo;
10993           hi3 = hi1 & ~hi2;
10994           lo3 = lo1 & ~lo2;
10995           for (w = BITS_PER_UNIT;
10996                w <= width && w <= HOST_BITS_PER_WIDE_INT;
10997                w <<= 1)
10998             {
10999               unsigned HOST_WIDE_INT mask
11000                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
11001               if (((lo1 | lo2) & mask) == mask
11002                   && (lo1 & ~mask) == 0 && hi1 == 0)
11003                 {
11004                   hi3 = 0;
11005                   lo3 = mask;
11006                   break;
11007                 }
11008             }
11009           if (hi3 != hi1 || lo3 != lo1)
11010             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11011                                 fold_build2_loc (loc, BIT_AND_EXPR, type,
11012                                              TREE_OPERAND (arg0, 0),
11013                                              build_int_cst_wide (type,
11014                                                                  lo3, hi3)),
11015                                 arg1);
11016         }
11017
11018       /* (X & Y) | Y is (X, Y).  */
11019       if (TREE_CODE (arg0) == BIT_AND_EXPR
11020           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11021         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11022       /* (X & Y) | X is (Y, X).  */
11023       if (TREE_CODE (arg0) == BIT_AND_EXPR
11024           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11025           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11026         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11027       /* X | (X & Y) is (Y, X).  */
11028       if (TREE_CODE (arg1) == BIT_AND_EXPR
11029           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11030           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11031         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11032       /* X | (Y & X) is (Y, X).  */
11033       if (TREE_CODE (arg1) == BIT_AND_EXPR
11034           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11035           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11036         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11037
11038       /* (X & ~Y) | (~X & Y) is X ^ Y */
11039       if (TREE_CODE (arg0) == BIT_AND_EXPR
11040           && TREE_CODE (arg1) == BIT_AND_EXPR)
11041         {
11042           tree a0, a1, l0, l1, n0, n1;
11043
11044           a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11045           a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11046
11047           l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11048           l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11049           
11050           n0 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l0);
11051           n1 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l1);
11052           
11053           if ((operand_equal_p (n0, a0, 0)
11054                && operand_equal_p (n1, a1, 0))
11055               || (operand_equal_p (n0, a1, 0)
11056                   && operand_equal_p (n1, a0, 0)))
11057             return fold_build2_loc (loc, BIT_XOR_EXPR, type, l0, n1);
11058         }
11059
11060       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11061       if (t1 != NULL_TREE)
11062         return t1;
11063
11064       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
11065
11066          This results in more efficient code for machines without a NAND
11067          instruction.  Combine will canonicalize to the first form
11068          which will allow use of NAND instructions provided by the
11069          backend if they exist.  */
11070       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11071           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11072         {
11073           return
11074             fold_build1_loc (loc, BIT_NOT_EXPR, type,
11075                          build2 (BIT_AND_EXPR, type,
11076                                  fold_convert_loc (loc, type,
11077                                                    TREE_OPERAND (arg0, 0)),
11078                                  fold_convert_loc (loc, type,
11079                                                    TREE_OPERAND (arg1, 0))));
11080         }
11081
11082       /* See if this can be simplified into a rotate first.  If that
11083          is unsuccessful continue in the association code.  */
11084       goto bit_rotate;
11085
11086     case BIT_XOR_EXPR:
11087       if (integer_zerop (arg1))
11088         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11089       if (integer_all_onesp (arg1))
11090         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
11091       if (operand_equal_p (arg0, arg1, 0))
11092         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11093
11094       /* ~X ^ X is -1.  */
11095       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11096           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11097         {
11098           t1 = build_zero_cst (type);
11099           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11100           return omit_one_operand_loc (loc, type, t1, arg1);
11101         }
11102
11103       /* X ^ ~X is -1.  */
11104       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11105           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11106         {
11107           t1 = build_zero_cst (type);
11108           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11109           return omit_one_operand_loc (loc, type, t1, arg0);
11110         }
11111
11112       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
11113          with a constant, and the two constants have no bits in common,
11114          we should treat this as a BIT_IOR_EXPR since this may produce more
11115          simplifications.  */
11116       if (TREE_CODE (arg0) == BIT_AND_EXPR
11117           && TREE_CODE (arg1) == BIT_AND_EXPR
11118           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11119           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
11120           && integer_zerop (const_binop (BIT_AND_EXPR,
11121                                          TREE_OPERAND (arg0, 1),
11122                                          TREE_OPERAND (arg1, 1))))
11123         {
11124           code = BIT_IOR_EXPR;
11125           goto bit_ior;
11126         }
11127
11128       /* (X | Y) ^ X -> Y & ~ X*/
11129       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11130           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11131         {
11132           tree t2 = TREE_OPERAND (arg0, 1);
11133           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11134                             arg1);
11135           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11136                             fold_convert_loc (loc, type, t2),
11137                             fold_convert_loc (loc, type, t1));
11138           return t1;
11139         }
11140
11141       /* (Y | X) ^ X -> Y & ~ X*/
11142       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11143           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11144         {
11145           tree t2 = TREE_OPERAND (arg0, 0);
11146           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11147                             arg1);
11148           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11149                             fold_convert_loc (loc, type, t2),
11150                             fold_convert_loc (loc, type, t1));
11151           return t1;
11152         }
11153
11154       /* X ^ (X | Y) -> Y & ~ X*/
11155       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11156           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11157         {
11158           tree t2 = TREE_OPERAND (arg1, 1);
11159           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11160                             arg0);
11161           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11162                             fold_convert_loc (loc, type, t2),
11163                             fold_convert_loc (loc, type, t1));
11164           return t1;
11165         }
11166
11167       /* X ^ (Y | X) -> Y & ~ X*/
11168       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11169           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11170         {
11171           tree t2 = TREE_OPERAND (arg1, 0);
11172           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11173                             arg0);
11174           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11175                             fold_convert_loc (loc, type, t2),
11176                             fold_convert_loc (loc, type, t1));
11177           return t1;
11178         }
11179
11180       /* Convert ~X ^ ~Y to X ^ Y.  */
11181       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11182           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11183         return fold_build2_loc (loc, code, type,
11184                             fold_convert_loc (loc, type,
11185                                               TREE_OPERAND (arg0, 0)),
11186                             fold_convert_loc (loc, type,
11187                                               TREE_OPERAND (arg1, 0)));
11188
11189       /* Convert ~X ^ C to X ^ ~C.  */
11190       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11191           && TREE_CODE (arg1) == INTEGER_CST)
11192         return fold_build2_loc (loc, code, type,
11193                             fold_convert_loc (loc, type,
11194                                               TREE_OPERAND (arg0, 0)),
11195                             fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
11196
11197       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
11198       if (TREE_CODE (arg0) == BIT_AND_EXPR
11199           && integer_onep (TREE_OPERAND (arg0, 1))
11200           && integer_onep (arg1))
11201         return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11202                             build_int_cst (TREE_TYPE (arg0), 0));
11203
11204       /* Fold (X & Y) ^ Y as ~X & Y.  */
11205       if (TREE_CODE (arg0) == BIT_AND_EXPR
11206           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11207         {
11208           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11209           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11210                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11211                               fold_convert_loc (loc, type, arg1));
11212         }
11213       /* Fold (X & Y) ^ X as ~Y & X.  */
11214       if (TREE_CODE (arg0) == BIT_AND_EXPR
11215           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11216           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11217         {
11218           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11219           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11220                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11221                               fold_convert_loc (loc, type, arg1));
11222         }
11223       /* Fold X ^ (X & Y) as X & ~Y.  */
11224       if (TREE_CODE (arg1) == BIT_AND_EXPR
11225           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11226         {
11227           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11228           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11229                               fold_convert_loc (loc, type, arg0),
11230                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11231         }
11232       /* Fold X ^ (Y & X) as ~Y & X.  */
11233       if (TREE_CODE (arg1) == BIT_AND_EXPR
11234           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11235           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11236         {
11237           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11238           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11239                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11240                               fold_convert_loc (loc, type, arg0));
11241         }
11242
11243       /* See if this can be simplified into a rotate first.  If that
11244          is unsuccessful continue in the association code.  */
11245       goto bit_rotate;
11246
11247     case BIT_AND_EXPR:
11248       if (integer_all_onesp (arg1))
11249         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11250       if (integer_zerop (arg1))
11251         return omit_one_operand_loc (loc, type, arg1, arg0);
11252       if (operand_equal_p (arg0, arg1, 0))
11253         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11254
11255       /* ~X & X, (X == 0) & X, and !X & X are always zero.  */
11256       if ((TREE_CODE (arg0) == BIT_NOT_EXPR
11257            || TREE_CODE (arg0) == TRUTH_NOT_EXPR
11258            || (TREE_CODE (arg0) == EQ_EXPR
11259                && integer_zerop (TREE_OPERAND (arg0, 1))))
11260           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11261         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11262
11263       /* X & ~X , X & (X == 0), and X & !X are always zero.  */
11264       if ((TREE_CODE (arg1) == BIT_NOT_EXPR
11265            || TREE_CODE (arg1) == TRUTH_NOT_EXPR
11266            || (TREE_CODE (arg1) == EQ_EXPR
11267                && integer_zerop (TREE_OPERAND (arg1, 1))))
11268           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11269         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11270
11271       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
11272       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11273           && TREE_CODE (arg1) == INTEGER_CST
11274           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11275         {
11276           tree tmp1 = fold_convert_loc (loc, type, arg1);
11277           tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11278           tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11279           tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
11280           tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
11281           return
11282             fold_convert_loc (loc, type,
11283                               fold_build2_loc (loc, BIT_IOR_EXPR,
11284                                            type, tmp2, tmp3));
11285         }
11286
11287       /* (X | Y) & Y is (X, Y).  */
11288       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11289           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11290         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11291       /* (X | Y) & X is (Y, X).  */
11292       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11293           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11294           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11295         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11296       /* X & (X | Y) is (Y, X).  */
11297       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11298           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11299           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11300         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11301       /* X & (Y | X) is (Y, X).  */
11302       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11303           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11304           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11305         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11306
11307       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
11308       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11309           && integer_onep (TREE_OPERAND (arg0, 1))
11310           && integer_onep (arg1))
11311         {
11312           tem = TREE_OPERAND (arg0, 0);
11313           return fold_build2_loc (loc, EQ_EXPR, type,
11314                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11315                                            build_int_cst (TREE_TYPE (tem), 1)),
11316                               build_int_cst (TREE_TYPE (tem), 0));
11317         }
11318       /* Fold ~X & 1 as (X & 1) == 0.  */
11319       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11320           && integer_onep (arg1))
11321         {
11322           tem = TREE_OPERAND (arg0, 0);
11323           return fold_build2_loc (loc, EQ_EXPR, type,
11324                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11325                                            build_int_cst (TREE_TYPE (tem), 1)),
11326                               build_int_cst (TREE_TYPE (tem), 0));
11327         }
11328       /* Fold !X & 1 as X == 0.  */
11329       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11330           && integer_onep (arg1))
11331         {
11332           tem = TREE_OPERAND (arg0, 0);
11333           return fold_build2_loc (loc, EQ_EXPR, type, tem,
11334                                   build_int_cst (TREE_TYPE (tem), 0));
11335         }
11336
11337       /* Fold (X ^ Y) & Y as ~X & Y.  */
11338       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11339           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11340         {
11341           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11342           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11343                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11344                               fold_convert_loc (loc, type, arg1));
11345         }
11346       /* Fold (X ^ Y) & X as ~Y & X.  */
11347       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11348           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11349           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11350         {
11351           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11352           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11353                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11354                               fold_convert_loc (loc, type, arg1));
11355         }
11356       /* Fold X & (X ^ Y) as X & ~Y.  */
11357       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11358           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11359         {
11360           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11361           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11362                               fold_convert_loc (loc, type, arg0),
11363                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11364         }
11365       /* Fold X & (Y ^ X) as ~Y & X.  */
11366       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11367           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11368           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11369         {
11370           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11371           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11372                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11373                               fold_convert_loc (loc, type, arg0));
11374         }
11375
11376       /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11377          ((A & N) + B) & M -> (A + B) & M
11378          Similarly if (N & M) == 0,
11379          ((A | N) + B) & M -> (A + B) & M
11380          and for - instead of + (or unary - instead of +)
11381          and/or ^ instead of |.
11382          If B is constant and (B & M) == 0, fold into A & M.  */
11383       if (host_integerp (arg1, 1))
11384         {
11385           unsigned HOST_WIDE_INT cst1 = tree_low_cst (arg1, 1);
11386           if (~cst1 && (cst1 & (cst1 + 1)) == 0
11387               && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11388               && (TREE_CODE (arg0) == PLUS_EXPR
11389                   || TREE_CODE (arg0) == MINUS_EXPR
11390                   || TREE_CODE (arg0) == NEGATE_EXPR)
11391               && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
11392                   || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
11393             {
11394               tree pmop[2];
11395               int which = 0;
11396               unsigned HOST_WIDE_INT cst0;
11397
11398               /* Now we know that arg0 is (C + D) or (C - D) or
11399                  -C and arg1 (M) is == (1LL << cst) - 1.
11400                  Store C into PMOP[0] and D into PMOP[1].  */
11401               pmop[0] = TREE_OPERAND (arg0, 0);
11402               pmop[1] = NULL;
11403               if (TREE_CODE (arg0) != NEGATE_EXPR)
11404                 {
11405                   pmop[1] = TREE_OPERAND (arg0, 1);
11406                   which = 1;
11407                 }
11408
11409               if (!host_integerp (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11410                   || (tree_low_cst (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11411                       & cst1) != cst1)
11412                 which = -1;
11413
11414               for (; which >= 0; which--)
11415                 switch (TREE_CODE (pmop[which]))
11416                   {
11417                   case BIT_AND_EXPR:
11418                   case BIT_IOR_EXPR:
11419                   case BIT_XOR_EXPR:
11420                     if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
11421                         != INTEGER_CST)
11422                       break;
11423                     /* tree_low_cst not used, because we don't care about
11424                        the upper bits.  */
11425                     cst0 = TREE_INT_CST_LOW (TREE_OPERAND (pmop[which], 1));
11426                     cst0 &= cst1;
11427                     if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
11428                       {
11429                         if (cst0 != cst1)
11430                           break;
11431                       }
11432                     else if (cst0 != 0)
11433                       break;
11434                     /* If C or D is of the form (A & N) where
11435                        (N & M) == M, or of the form (A | N) or
11436                        (A ^ N) where (N & M) == 0, replace it with A.  */
11437                     pmop[which] = TREE_OPERAND (pmop[which], 0);
11438                     break;
11439                   case INTEGER_CST:
11440                     /* If C or D is a N where (N & M) == 0, it can be
11441                        omitted (assumed 0).  */
11442                     if ((TREE_CODE (arg0) == PLUS_EXPR
11443                          || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
11444                         && (TREE_INT_CST_LOW (pmop[which]) & cst1) == 0)
11445                       pmop[which] = NULL;
11446                     break;
11447                   default:
11448                     break;
11449                   }
11450
11451               /* Only build anything new if we optimized one or both arguments
11452                  above.  */
11453               if (pmop[0] != TREE_OPERAND (arg0, 0)
11454                   || (TREE_CODE (arg0) != NEGATE_EXPR
11455                       && pmop[1] != TREE_OPERAND (arg0, 1)))
11456                 {
11457                   tree utype = TREE_TYPE (arg0);
11458                   if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11459                     {
11460                       /* Perform the operations in a type that has defined
11461                          overflow behavior.  */
11462                       utype = unsigned_type_for (TREE_TYPE (arg0));
11463                       if (pmop[0] != NULL)
11464                         pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
11465                       if (pmop[1] != NULL)
11466                         pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
11467                     }
11468
11469                   if (TREE_CODE (arg0) == NEGATE_EXPR)
11470                     tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
11471                   else if (TREE_CODE (arg0) == PLUS_EXPR)
11472                     {
11473                       if (pmop[0] != NULL && pmop[1] != NULL)
11474                         tem = fold_build2_loc (loc, PLUS_EXPR, utype,
11475                                                pmop[0], pmop[1]);
11476                       else if (pmop[0] != NULL)
11477                         tem = pmop[0];
11478                       else if (pmop[1] != NULL)
11479                         tem = pmop[1];
11480                       else
11481                         return build_int_cst (type, 0);
11482                     }
11483                   else if (pmop[0] == NULL)
11484                     tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
11485                   else
11486                     tem = fold_build2_loc (loc, MINUS_EXPR, utype,
11487                                            pmop[0], pmop[1]);
11488                   /* TEM is now the new binary +, - or unary - replacement.  */
11489                   tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
11490                                          fold_convert_loc (loc, utype, arg1));
11491                   return fold_convert_loc (loc, type, tem);
11492                 }
11493             }
11494         }
11495
11496       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11497       if (t1 != NULL_TREE)
11498         return t1;
11499       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
11500       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11501           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11502         {
11503           unsigned int prec
11504             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11505
11506           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11507               && (~TREE_INT_CST_LOW (arg1)
11508                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11509             return
11510               fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11511         }
11512
11513       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11514
11515          This results in more efficient code for machines without a NOR
11516          instruction.  Combine will canonicalize to the first form
11517          which will allow use of NOR instructions provided by the
11518          backend if they exist.  */
11519       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11520           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11521         {
11522           return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11523                               build2 (BIT_IOR_EXPR, type,
11524                                       fold_convert_loc (loc, type,
11525                                                         TREE_OPERAND (arg0, 0)),
11526                                       fold_convert_loc (loc, type,
11527                                                         TREE_OPERAND (arg1, 0))));
11528         }
11529
11530       /* If arg0 is derived from the address of an object or function, we may
11531          be able to fold this expression using the object or function's
11532          alignment.  */
11533       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11534         {
11535           unsigned HOST_WIDE_INT modulus, residue;
11536           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11537
11538           modulus = get_pointer_modulus_and_residue (arg0, &residue,
11539                                                      integer_onep (arg1));
11540
11541           /* This works because modulus is a power of 2.  If this weren't the
11542              case, we'd have to replace it by its greatest power-of-2
11543              divisor: modulus & -modulus.  */
11544           if (low < modulus)
11545             return build_int_cst (type, residue & low);
11546         }
11547
11548       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11549               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11550          if the new mask might be further optimized.  */
11551       if ((TREE_CODE (arg0) == LSHIFT_EXPR
11552            || TREE_CODE (arg0) == RSHIFT_EXPR)
11553           && host_integerp (TREE_OPERAND (arg0, 1), 1)
11554           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11555           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11556              < TYPE_PRECISION (TREE_TYPE (arg0))
11557           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11558           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11559         {
11560           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11561           unsigned HOST_WIDE_INT mask
11562             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11563           unsigned HOST_WIDE_INT newmask, zerobits = 0;
11564           tree shift_type = TREE_TYPE (arg0);
11565
11566           if (TREE_CODE (arg0) == LSHIFT_EXPR)
11567             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11568           else if (TREE_CODE (arg0) == RSHIFT_EXPR
11569                    && TYPE_PRECISION (TREE_TYPE (arg0))
11570                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11571             {
11572               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11573               tree arg00 = TREE_OPERAND (arg0, 0);
11574               /* See if more bits can be proven as zero because of
11575                  zero extension.  */
11576               if (TREE_CODE (arg00) == NOP_EXPR
11577                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11578                 {
11579                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11580                   if (TYPE_PRECISION (inner_type)
11581                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11582                       && TYPE_PRECISION (inner_type) < prec)
11583                     {
11584                       prec = TYPE_PRECISION (inner_type);
11585                       /* See if we can shorten the right shift.  */
11586                       if (shiftc < prec)
11587                         shift_type = inner_type;
11588                     }
11589                 }
11590               zerobits = ~(unsigned HOST_WIDE_INT) 0;
11591               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11592               zerobits <<= prec - shiftc;
11593               /* For arithmetic shift if sign bit could be set, zerobits
11594                  can contain actually sign bits, so no transformation is
11595                  possible, unless MASK masks them all away.  In that
11596                  case the shift needs to be converted into logical shift.  */
11597               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11598                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11599                 {
11600                   if ((mask & zerobits) == 0)
11601                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
11602                   else
11603                     zerobits = 0;
11604                 }
11605             }
11606
11607           /* ((X << 16) & 0xff00) is (X, 0).  */
11608           if ((mask & zerobits) == mask)
11609             return omit_one_operand_loc (loc, type,
11610                                      build_int_cst (type, 0), arg0);
11611
11612           newmask = mask | zerobits;
11613           if (newmask != mask && (newmask & (newmask + 1)) == 0)
11614             {
11615               unsigned int prec;
11616
11617               /* Only do the transformation if NEWMASK is some integer
11618                  mode's mask.  */
11619               for (prec = BITS_PER_UNIT;
11620                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11621                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11622                   break;
11623               if (prec < HOST_BITS_PER_WIDE_INT
11624                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
11625                 {
11626                   tree newmaskt;
11627
11628                   if (shift_type != TREE_TYPE (arg0))
11629                     {
11630                       tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11631                                          fold_convert_loc (loc, shift_type,
11632                                                            TREE_OPERAND (arg0, 0)),
11633                                          TREE_OPERAND (arg0, 1));
11634                       tem = fold_convert_loc (loc, type, tem);
11635                     }
11636                   else
11637                     tem = op0;
11638                   newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11639                   if (!tree_int_cst_equal (newmaskt, arg1))
11640                     return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11641                 }
11642             }
11643         }
11644
11645       goto associate;
11646
11647     case RDIV_EXPR:
11648       /* Don't touch a floating-point divide by zero unless the mode
11649          of the constant can represent infinity.  */
11650       if (TREE_CODE (arg1) == REAL_CST
11651           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11652           && real_zerop (arg1))
11653         return NULL_TREE;
11654
11655       /* Optimize A / A to 1.0 if we don't care about
11656          NaNs or Infinities.  Skip the transformation
11657          for non-real operands.  */
11658       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11659           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11660           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11661           && operand_equal_p (arg0, arg1, 0))
11662         {
11663           tree r = build_real (TREE_TYPE (arg0), dconst1);
11664
11665           return omit_two_operands_loc (loc, type, r, arg0, arg1);
11666         }
11667
11668       /* The complex version of the above A / A optimization.  */
11669       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11670           && operand_equal_p (arg0, arg1, 0))
11671         {
11672           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11673           if (! HONOR_NANS (TYPE_MODE (elem_type))
11674               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11675             {
11676               tree r = build_real (elem_type, dconst1);
11677               /* omit_two_operands will call fold_convert for us.  */
11678               return omit_two_operands_loc (loc, type, r, arg0, arg1);
11679             }
11680         }
11681
11682       /* (-A) / (-B) -> A / B  */
11683       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11684         return fold_build2_loc (loc, RDIV_EXPR, type,
11685                             TREE_OPERAND (arg0, 0),
11686                             negate_expr (arg1));
11687       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11688         return fold_build2_loc (loc, RDIV_EXPR, type,
11689                             negate_expr (arg0),
11690                             TREE_OPERAND (arg1, 0));
11691
11692       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
11693       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11694           && real_onep (arg1))
11695         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11696
11697       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
11698       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11699           && real_minus_onep (arg1))
11700         return non_lvalue_loc (loc, fold_convert_loc (loc, type,
11701                                                   negate_expr (arg0)));
11702
11703       /* If ARG1 is a constant, we can convert this to a multiply by the
11704          reciprocal.  This does not have the same rounding properties,
11705          so only do this if -freciprocal-math.  We can actually
11706          always safely do it if ARG1 is a power of two, but it's hard to
11707          tell if it is or not in a portable manner.  */
11708       if (TREE_CODE (arg1) == REAL_CST)
11709         {
11710           if (flag_reciprocal_math
11711               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11712                                           arg1)))
11713             return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
11714           /* Find the reciprocal if optimizing and the result is exact.  */
11715           if (optimize)
11716             {
11717               REAL_VALUE_TYPE r;
11718               r = TREE_REAL_CST (arg1);
11719               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11720                 {
11721                   tem = build_real (type, r);
11722                   return fold_build2_loc (loc, MULT_EXPR, type,
11723                                       fold_convert_loc (loc, type, arg0), tem);
11724                 }
11725             }
11726         }
11727       /* Convert A/B/C to A/(B*C).  */
11728       if (flag_reciprocal_math
11729           && TREE_CODE (arg0) == RDIV_EXPR)
11730         return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11731                             fold_build2_loc (loc, MULT_EXPR, type,
11732                                          TREE_OPERAND (arg0, 1), arg1));
11733
11734       /* Convert A/(B/C) to (A/B)*C.  */
11735       if (flag_reciprocal_math
11736           && TREE_CODE (arg1) == RDIV_EXPR)
11737         return fold_build2_loc (loc, MULT_EXPR, type,
11738                             fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11739                                          TREE_OPERAND (arg1, 0)),
11740                             TREE_OPERAND (arg1, 1));
11741
11742       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11743       if (flag_reciprocal_math
11744           && TREE_CODE (arg1) == MULT_EXPR
11745           && TREE_CODE (arg0) == REAL_CST
11746           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11747         {
11748           tree tem = const_binop (RDIV_EXPR, arg0,
11749                                   TREE_OPERAND (arg1, 1));
11750           if (tem)
11751             return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11752                                 TREE_OPERAND (arg1, 0));
11753         }
11754
11755       if (flag_unsafe_math_optimizations)
11756         {
11757           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11758           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11759
11760           /* Optimize sin(x)/cos(x) as tan(x).  */
11761           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11762                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11763                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11764               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11765                                   CALL_EXPR_ARG (arg1, 0), 0))
11766             {
11767               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11768
11769               if (tanfn != NULL_TREE)
11770                 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11771             }
11772
11773           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11774           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11775                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11776                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11777               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11778                                   CALL_EXPR_ARG (arg1, 0), 0))
11779             {
11780               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11781
11782               if (tanfn != NULL_TREE)
11783                 {
11784                   tree tmp = build_call_expr_loc (loc, tanfn, 1,
11785                                               CALL_EXPR_ARG (arg0, 0));
11786                   return fold_build2_loc (loc, RDIV_EXPR, type,
11787                                       build_real (type, dconst1), tmp);
11788                 }
11789             }
11790
11791           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11792              NaNs or Infinities.  */
11793           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11794                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11795                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11796             {
11797               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11798               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11799
11800               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11801                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11802                   && operand_equal_p (arg00, arg01, 0))
11803                 {
11804                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11805
11806                   if (cosfn != NULL_TREE)
11807                     return build_call_expr_loc (loc, cosfn, 1, arg00);
11808                 }
11809             }
11810
11811           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11812              NaNs or Infinities.  */
11813           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11814                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11815                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11816             {
11817               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11818               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11819
11820               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11821                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11822                   && operand_equal_p (arg00, arg01, 0))
11823                 {
11824                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11825
11826                   if (cosfn != NULL_TREE)
11827                     {
11828                       tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11829                       return fold_build2_loc (loc, RDIV_EXPR, type,
11830                                           build_real (type, dconst1),
11831                                           tmp);
11832                     }
11833                 }
11834             }
11835
11836           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11837           if (fcode0 == BUILT_IN_POW
11838               || fcode0 == BUILT_IN_POWF
11839               || fcode0 == BUILT_IN_POWL)
11840             {
11841               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11842               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11843               if (TREE_CODE (arg01) == REAL_CST
11844                   && !TREE_OVERFLOW (arg01)
11845                   && operand_equal_p (arg1, arg00, 0))
11846                 {
11847                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11848                   REAL_VALUE_TYPE c;
11849                   tree arg;
11850
11851                   c = TREE_REAL_CST (arg01);
11852                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11853                   arg = build_real (type, c);
11854                   return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11855                 }
11856             }
11857
11858           /* Optimize a/root(b/c) into a*root(c/b).  */
11859           if (BUILTIN_ROOT_P (fcode1))
11860             {
11861               tree rootarg = CALL_EXPR_ARG (arg1, 0);
11862
11863               if (TREE_CODE (rootarg) == RDIV_EXPR)
11864                 {
11865                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11866                   tree b = TREE_OPERAND (rootarg, 0);
11867                   tree c = TREE_OPERAND (rootarg, 1);
11868
11869                   tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
11870
11871                   tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
11872                   return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
11873                 }
11874             }
11875
11876           /* Optimize x/expN(y) into x*expN(-y).  */
11877           if (BUILTIN_EXPONENT_P (fcode1))
11878             {
11879               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11880               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11881               arg1 = build_call_expr_loc (loc,
11882                                       expfn, 1,
11883                                       fold_convert_loc (loc, type, arg));
11884               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11885             }
11886
11887           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
11888           if (fcode1 == BUILT_IN_POW
11889               || fcode1 == BUILT_IN_POWF
11890               || fcode1 == BUILT_IN_POWL)
11891             {
11892               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11893               tree arg10 = CALL_EXPR_ARG (arg1, 0);
11894               tree arg11 = CALL_EXPR_ARG (arg1, 1);
11895               tree neg11 = fold_convert_loc (loc, type,
11896                                              negate_expr (arg11));
11897               arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
11898               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11899             }
11900         }
11901       return NULL_TREE;
11902
11903     case TRUNC_DIV_EXPR:
11904       /* Optimize (X & (-A)) / A where A is a power of 2,
11905          to X >> log2(A) */
11906       if (TREE_CODE (arg0) == BIT_AND_EXPR
11907           && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST
11908           && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0)
11909         {
11910           tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1),
11911                                       arg1, TREE_OPERAND (arg0, 1));
11912           if (sum && integer_zerop (sum)) {
11913             unsigned long pow2;
11914
11915             if (TREE_INT_CST_LOW (arg1))
11916               pow2 = exact_log2 (TREE_INT_CST_LOW (arg1));
11917             else
11918               pow2 = exact_log2 (TREE_INT_CST_HIGH (arg1))
11919                       + HOST_BITS_PER_WIDE_INT;
11920
11921             return fold_build2_loc (loc, RSHIFT_EXPR, type,
11922                           TREE_OPERAND (arg0, 0),
11923                           build_int_cst (integer_type_node, pow2));
11924           }
11925         }
11926
11927       /* Fall thru */
11928       
11929     case FLOOR_DIV_EXPR:
11930       /* Simplify A / (B << N) where A and B are positive and B is
11931          a power of 2, to A >> (N + log2(B)).  */
11932       strict_overflow_p = false;
11933       if (TREE_CODE (arg1) == LSHIFT_EXPR
11934           && (TYPE_UNSIGNED (type)
11935               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11936         {
11937           tree sval = TREE_OPERAND (arg1, 0);
11938           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11939             {
11940               tree sh_cnt = TREE_OPERAND (arg1, 1);
11941               unsigned long pow2;
11942
11943               if (TREE_INT_CST_LOW (sval))
11944                 pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11945               else
11946                 pow2 = exact_log2 (TREE_INT_CST_HIGH (sval))
11947                        + HOST_BITS_PER_WIDE_INT;
11948
11949               if (strict_overflow_p)
11950                 fold_overflow_warning (("assuming signed overflow does not "
11951                                         "occur when simplifying A / (B << N)"),
11952                                        WARN_STRICT_OVERFLOW_MISC);
11953
11954               sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11955                                         sh_cnt,
11956                                         build_int_cst (TREE_TYPE (sh_cnt),
11957                                                        pow2));
11958               return fold_build2_loc (loc, RSHIFT_EXPR, type,
11959                                   fold_convert_loc (loc, type, arg0), sh_cnt);
11960             }
11961         }
11962
11963       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11964          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
11965       if (INTEGRAL_TYPE_P (type)
11966           && TYPE_UNSIGNED (type)
11967           && code == FLOOR_DIV_EXPR)
11968         return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
11969
11970       /* Fall thru */
11971
11972     case ROUND_DIV_EXPR:
11973     case CEIL_DIV_EXPR:
11974     case EXACT_DIV_EXPR:
11975       if (integer_onep (arg1))
11976         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11977       if (integer_zerop (arg1))
11978         return NULL_TREE;
11979       /* X / -1 is -X.  */
11980       if (!TYPE_UNSIGNED (type)
11981           && TREE_CODE (arg1) == INTEGER_CST
11982           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11983           && TREE_INT_CST_HIGH (arg1) == -1)
11984         return fold_convert_loc (loc, type, negate_expr (arg0));
11985
11986       /* Convert -A / -B to A / B when the type is signed and overflow is
11987          undefined.  */
11988       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11989           && TREE_CODE (arg0) == NEGATE_EXPR
11990           && negate_expr_p (arg1))
11991         {
11992           if (INTEGRAL_TYPE_P (type))
11993             fold_overflow_warning (("assuming signed overflow does not occur "
11994                                     "when distributing negation across "
11995                                     "division"),
11996                                    WARN_STRICT_OVERFLOW_MISC);
11997           return fold_build2_loc (loc, code, type,
11998                               fold_convert_loc (loc, type,
11999                                                 TREE_OPERAND (arg0, 0)),
12000                               fold_convert_loc (loc, type,
12001                                                 negate_expr (arg1)));
12002         }
12003       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12004           && TREE_CODE (arg1) == NEGATE_EXPR
12005           && negate_expr_p (arg0))
12006         {
12007           if (INTEGRAL_TYPE_P (type))
12008             fold_overflow_warning (("assuming signed overflow does not occur "
12009                                     "when distributing negation across "
12010                                     "division"),
12011                                    WARN_STRICT_OVERFLOW_MISC);
12012           return fold_build2_loc (loc, code, type,
12013                               fold_convert_loc (loc, type,
12014                                                 negate_expr (arg0)),
12015                               fold_convert_loc (loc, type,
12016                                                 TREE_OPERAND (arg1, 0)));
12017         }
12018
12019       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12020          operation, EXACT_DIV_EXPR.
12021
12022          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12023          At one time others generated faster code, it's not clear if they do
12024          after the last round to changes to the DIV code in expmed.c.  */
12025       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
12026           && multiple_of_p (type, arg0, arg1))
12027         return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
12028
12029       strict_overflow_p = false;
12030       if (TREE_CODE (arg1) == INTEGER_CST
12031           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12032                                          &strict_overflow_p)))
12033         {
12034           if (strict_overflow_p)
12035             fold_overflow_warning (("assuming signed overflow does not occur "
12036                                     "when simplifying division"),
12037                                    WARN_STRICT_OVERFLOW_MISC);
12038           return fold_convert_loc (loc, type, tem);
12039         }
12040
12041       return NULL_TREE;
12042
12043     case CEIL_MOD_EXPR:
12044     case FLOOR_MOD_EXPR:
12045     case ROUND_MOD_EXPR:
12046     case TRUNC_MOD_EXPR:
12047       /* X % 1 is always zero, but be sure to preserve any side
12048          effects in X.  */
12049       if (integer_onep (arg1))
12050         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12051
12052       /* X % 0, return X % 0 unchanged so that we can get the
12053          proper warnings and errors.  */
12054       if (integer_zerop (arg1))
12055         return NULL_TREE;
12056
12057       /* 0 % X is always zero, but be sure to preserve any side
12058          effects in X.  Place this after checking for X == 0.  */
12059       if (integer_zerop (arg0))
12060         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12061
12062       /* X % -1 is zero.  */
12063       if (!TYPE_UNSIGNED (type)
12064           && TREE_CODE (arg1) == INTEGER_CST
12065           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
12066           && TREE_INT_CST_HIGH (arg1) == -1)
12067         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12068
12069       /* X % -C is the same as X % C.  */
12070       if (code == TRUNC_MOD_EXPR
12071           && !TYPE_UNSIGNED (type)
12072           && TREE_CODE (arg1) == INTEGER_CST
12073           && !TREE_OVERFLOW (arg1)
12074           && TREE_INT_CST_HIGH (arg1) < 0
12075           && !TYPE_OVERFLOW_TRAPS (type)
12076           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
12077           && !sign_bit_p (arg1, arg1))
12078         return fold_build2_loc (loc, code, type,
12079                             fold_convert_loc (loc, type, arg0),
12080                             fold_convert_loc (loc, type,
12081                                               negate_expr (arg1)));
12082
12083       /* X % -Y is the same as X % Y.  */
12084       if (code == TRUNC_MOD_EXPR
12085           && !TYPE_UNSIGNED (type)
12086           && TREE_CODE (arg1) == NEGATE_EXPR
12087           && !TYPE_OVERFLOW_TRAPS (type))
12088         return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
12089                             fold_convert_loc (loc, type,
12090                                               TREE_OPERAND (arg1, 0)));
12091
12092       strict_overflow_p = false;
12093       if (TREE_CODE (arg1) == INTEGER_CST
12094           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12095                                          &strict_overflow_p)))
12096         {
12097           if (strict_overflow_p)
12098             fold_overflow_warning (("assuming signed overflow does not occur "
12099                                     "when simplifying modulus"),
12100                                    WARN_STRICT_OVERFLOW_MISC);
12101           return fold_convert_loc (loc, type, tem);
12102         }
12103
12104       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
12105          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
12106       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
12107           && (TYPE_UNSIGNED (type)
12108               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12109         {
12110           tree c = arg1;
12111           /* Also optimize A % (C << N)  where C is a power of 2,
12112              to A & ((C << N) - 1).  */
12113           if (TREE_CODE (arg1) == LSHIFT_EXPR)
12114             c = TREE_OPERAND (arg1, 0);
12115
12116           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
12117             {
12118               tree mask
12119                 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
12120                                    build_int_cst (TREE_TYPE (arg1), 1));
12121               if (strict_overflow_p)
12122                 fold_overflow_warning (("assuming signed overflow does not "
12123                                         "occur when simplifying "
12124                                         "X % (power of two)"),
12125                                        WARN_STRICT_OVERFLOW_MISC);
12126               return fold_build2_loc (loc, BIT_AND_EXPR, type,
12127                                       fold_convert_loc (loc, type, arg0),
12128                                       fold_convert_loc (loc, type, mask));
12129             }
12130         }
12131
12132       return NULL_TREE;
12133
12134     case LROTATE_EXPR:
12135     case RROTATE_EXPR:
12136       if (integer_all_onesp (arg0))
12137         return omit_one_operand_loc (loc, type, arg0, arg1);
12138       goto shift;
12139
12140     case RSHIFT_EXPR:
12141       /* Optimize -1 >> x for arithmetic right shifts.  */
12142       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
12143           && tree_expr_nonnegative_p (arg1))
12144         return omit_one_operand_loc (loc, type, arg0, arg1);
12145       /* ... fall through ...  */
12146
12147     case LSHIFT_EXPR:
12148     shift:
12149       if (integer_zerop (arg1))
12150         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12151       if (integer_zerop (arg0))
12152         return omit_one_operand_loc (loc, type, arg0, arg1);
12153
12154       /* Since negative shift count is not well-defined,
12155          don't try to compute it in the compiler.  */
12156       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
12157         return NULL_TREE;
12158
12159       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
12160       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
12161           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
12162           && host_integerp (TREE_OPERAND (arg0, 1), false)
12163           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
12164         {
12165           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
12166                                + TREE_INT_CST_LOW (arg1));
12167
12168           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
12169              being well defined.  */
12170           if (low >= TYPE_PRECISION (type))
12171             {
12172               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
12173                 low = low % TYPE_PRECISION (type);
12174               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
12175                 return omit_one_operand_loc (loc, type, build_int_cst (type, 0),
12176                                          TREE_OPERAND (arg0, 0));
12177               else
12178                 low = TYPE_PRECISION (type) - 1;
12179             }
12180
12181           return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12182                               build_int_cst (type, low));
12183         }
12184
12185       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
12186          into x & ((unsigned)-1 >> c) for unsigned types.  */
12187       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
12188            || (TYPE_UNSIGNED (type)
12189                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
12190           && host_integerp (arg1, false)
12191           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
12192           && host_integerp (TREE_OPERAND (arg0, 1), false)
12193           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
12194         {
12195           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
12196           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
12197           tree lshift;
12198           tree arg00;
12199
12200           if (low0 == low1)
12201             {
12202               arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12203
12204               lshift = build_int_cst (type, -1);
12205               lshift = int_const_binop (code, lshift, arg1);
12206
12207               return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
12208             }
12209         }
12210
12211       /* Rewrite an LROTATE_EXPR by a constant into an
12212          RROTATE_EXPR by a new constant.  */
12213       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
12214         {
12215           tree tem = build_int_cst (TREE_TYPE (arg1),
12216                                     TYPE_PRECISION (type));
12217           tem = const_binop (MINUS_EXPR, tem, arg1);
12218           return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
12219         }
12220
12221       /* If we have a rotate of a bit operation with the rotate count and
12222          the second operand of the bit operation both constant,
12223          permute the two operations.  */
12224       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12225           && (TREE_CODE (arg0) == BIT_AND_EXPR
12226               || TREE_CODE (arg0) == BIT_IOR_EXPR
12227               || TREE_CODE (arg0) == BIT_XOR_EXPR)
12228           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12229         return fold_build2_loc (loc, TREE_CODE (arg0), type,
12230                             fold_build2_loc (loc, code, type,
12231                                          TREE_OPERAND (arg0, 0), arg1),
12232                             fold_build2_loc (loc, code, type,
12233                                          TREE_OPERAND (arg0, 1), arg1));
12234
12235       /* Two consecutive rotates adding up to the precision of the
12236          type can be ignored.  */
12237       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12238           && TREE_CODE (arg0) == RROTATE_EXPR
12239           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12240           && TREE_INT_CST_HIGH (arg1) == 0
12241           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
12242           && ((TREE_INT_CST_LOW (arg1)
12243                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
12244               == (unsigned int) TYPE_PRECISION (type)))
12245         return TREE_OPERAND (arg0, 0);
12246
12247       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
12248               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
12249          if the latter can be further optimized.  */
12250       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
12251           && TREE_CODE (arg0) == BIT_AND_EXPR
12252           && TREE_CODE (arg1) == INTEGER_CST
12253           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12254         {
12255           tree mask = fold_build2_loc (loc, code, type,
12256                                    fold_convert_loc (loc, type,
12257                                                      TREE_OPERAND (arg0, 1)),
12258                                    arg1);
12259           tree shift = fold_build2_loc (loc, code, type,
12260                                     fold_convert_loc (loc, type,
12261                                                       TREE_OPERAND (arg0, 0)),
12262                                     arg1);
12263           tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
12264           if (tem)
12265             return tem;
12266         }
12267
12268       return NULL_TREE;
12269
12270     case MIN_EXPR:
12271       if (operand_equal_p (arg0, arg1, 0))
12272         return omit_one_operand_loc (loc, type, arg0, arg1);
12273       if (INTEGRAL_TYPE_P (type)
12274           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
12275         return omit_one_operand_loc (loc, type, arg1, arg0);
12276       tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
12277       if (tem)
12278         return tem;
12279       goto associate;
12280
12281     case MAX_EXPR:
12282       if (operand_equal_p (arg0, arg1, 0))
12283         return omit_one_operand_loc (loc, type, arg0, arg1);
12284       if (INTEGRAL_TYPE_P (type)
12285           && TYPE_MAX_VALUE (type)
12286           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
12287         return omit_one_operand_loc (loc, type, arg1, arg0);
12288       tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
12289       if (tem)
12290         return tem;
12291       goto associate;
12292
12293     case TRUTH_ANDIF_EXPR:
12294       /* Note that the operands of this must be ints
12295          and their values must be 0 or 1.
12296          ("true" is a fixed value perhaps depending on the language.)  */
12297       /* If first arg is constant zero, return it.  */
12298       if (integer_zerop (arg0))
12299         return fold_convert_loc (loc, type, arg0);
12300     case TRUTH_AND_EXPR:
12301       /* If either arg is constant true, drop it.  */
12302       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12303         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12304       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12305           /* Preserve sequence points.  */
12306           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12307         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12308       /* If second arg is constant zero, result is zero, but first arg
12309          must be evaluated.  */
12310       if (integer_zerop (arg1))
12311         return omit_one_operand_loc (loc, type, arg1, arg0);
12312       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12313          case will be handled here.  */
12314       if (integer_zerop (arg0))
12315         return omit_one_operand_loc (loc, type, arg0, arg1);
12316
12317       /* !X && X is always false.  */
12318       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12319           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12320         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12321       /* X && !X is always false.  */
12322       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12323           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12324         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12325
12326       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
12327          means A >= Y && A != MAX, but in this case we know that
12328          A < X <= MAX.  */
12329
12330       if (!TREE_SIDE_EFFECTS (arg0)
12331           && !TREE_SIDE_EFFECTS (arg1))
12332         {
12333           tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12334           if (tem && !operand_equal_p (tem, arg0, 0))
12335             return fold_build2_loc (loc, code, type, tem, arg1);
12336
12337           tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12338           if (tem && !operand_equal_p (tem, arg1, 0))
12339             return fold_build2_loc (loc, code, type, arg0, tem);
12340         }
12341
12342       if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12343           != NULL_TREE)
12344         return tem;
12345
12346       return NULL_TREE;
12347
12348     case TRUTH_ORIF_EXPR:
12349       /* Note that the operands of this must be ints
12350          and their values must be 0 or true.
12351          ("true" is a fixed value perhaps depending on the language.)  */
12352       /* If first arg is constant true, return it.  */
12353       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12354         return fold_convert_loc (loc, type, arg0);
12355     case TRUTH_OR_EXPR:
12356       /* If either arg is constant zero, drop it.  */
12357       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12358         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12359       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12360           /* Preserve sequence points.  */
12361           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12362         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12363       /* If second arg is constant true, result is true, but we must
12364          evaluate first arg.  */
12365       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12366         return omit_one_operand_loc (loc, type, arg1, arg0);
12367       /* Likewise for first arg, but note this only occurs here for
12368          TRUTH_OR_EXPR.  */
12369       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12370         return omit_one_operand_loc (loc, type, arg0, arg1);
12371
12372       /* !X || X is always true.  */
12373       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12374           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12375         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12376       /* X || !X is always true.  */
12377       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12378           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12379         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12380
12381       /* (X && !Y) || (!X && Y) is X ^ Y */
12382       if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12383           && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12384         {
12385           tree a0, a1, l0, l1, n0, n1;
12386
12387           a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12388           a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12389
12390           l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12391           l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12392           
12393           n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12394           n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12395           
12396           if ((operand_equal_p (n0, a0, 0)
12397                && operand_equal_p (n1, a1, 0))
12398               || (operand_equal_p (n0, a1, 0)
12399                   && operand_equal_p (n1, a0, 0)))
12400             return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12401         }
12402
12403       if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12404           != NULL_TREE)
12405         return tem;
12406
12407       return NULL_TREE;
12408
12409     case TRUTH_XOR_EXPR:
12410       /* If the second arg is constant zero, drop it.  */
12411       if (integer_zerop (arg1))
12412         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12413       /* If the second arg is constant true, this is a logical inversion.  */
12414       if (integer_onep (arg1))
12415         {
12416           /* Only call invert_truthvalue if operand is a truth value.  */
12417           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12418             tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12419           else
12420             tem = invert_truthvalue_loc (loc, arg0);
12421           return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12422         }
12423       /* Identical arguments cancel to zero.  */
12424       if (operand_equal_p (arg0, arg1, 0))
12425         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12426
12427       /* !X ^ X is always true.  */
12428       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12429           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12430         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12431
12432       /* X ^ !X is always true.  */
12433       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12434           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12435         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12436
12437       return NULL_TREE;
12438
12439     case EQ_EXPR:
12440     case NE_EXPR:
12441       STRIP_NOPS (arg0);
12442       STRIP_NOPS (arg1);
12443
12444       tem = fold_comparison (loc, code, type, op0, op1);
12445       if (tem != NULL_TREE)
12446         return tem;
12447
12448       /* bool_var != 0 becomes bool_var. */
12449       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12450           && code == NE_EXPR)
12451         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12452
12453       /* bool_var == 1 becomes bool_var. */
12454       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12455           && code == EQ_EXPR)
12456         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12457
12458       /* bool_var != 1 becomes !bool_var. */
12459       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12460           && code == NE_EXPR)
12461         return fold_convert_loc (loc, type,
12462                                  fold_build1_loc (loc, TRUTH_NOT_EXPR,
12463                                                   TREE_TYPE (arg0), arg0));
12464
12465       /* bool_var == 0 becomes !bool_var. */
12466       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12467           && code == EQ_EXPR)
12468         return fold_convert_loc (loc, type,
12469                                  fold_build1_loc (loc, TRUTH_NOT_EXPR,
12470                                                   TREE_TYPE (arg0), arg0));
12471
12472       /* !exp != 0 becomes !exp */
12473       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12474           && code == NE_EXPR)
12475         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12476
12477       /* If this is an equality comparison of the address of two non-weak,
12478          unaliased symbols neither of which are extern (since we do not
12479          have access to attributes for externs), then we know the result.  */
12480       if (TREE_CODE (arg0) == ADDR_EXPR
12481           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12482           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12483           && ! lookup_attribute ("alias",
12484                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12485           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12486           && TREE_CODE (arg1) == ADDR_EXPR
12487           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12488           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12489           && ! lookup_attribute ("alias",
12490                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12491           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12492         {
12493           /* We know that we're looking at the address of two
12494              non-weak, unaliased, static _DECL nodes.
12495
12496              It is both wasteful and incorrect to call operand_equal_p
12497              to compare the two ADDR_EXPR nodes.  It is wasteful in that
12498              all we need to do is test pointer equality for the arguments
12499              to the two ADDR_EXPR nodes.  It is incorrect to use
12500              operand_equal_p as that function is NOT equivalent to a
12501              C equality test.  It can in fact return false for two
12502              objects which would test as equal using the C equality
12503              operator.  */
12504           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12505           return constant_boolean_node (equal
12506                                         ? code == EQ_EXPR : code != EQ_EXPR,
12507                                         type);
12508         }
12509
12510       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12511          a MINUS_EXPR of a constant, we can convert it into a comparison with
12512          a revised constant as long as no overflow occurs.  */
12513       if (TREE_CODE (arg1) == INTEGER_CST
12514           && (TREE_CODE (arg0) == PLUS_EXPR
12515               || TREE_CODE (arg0) == MINUS_EXPR)
12516           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12517           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12518                                       ? MINUS_EXPR : PLUS_EXPR,
12519                                       fold_convert_loc (loc, TREE_TYPE (arg0),
12520                                                         arg1),
12521                                       TREE_OPERAND (arg0, 1)))
12522           && !TREE_OVERFLOW (tem))
12523         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12524
12525       /* Similarly for a NEGATE_EXPR.  */
12526       if (TREE_CODE (arg0) == NEGATE_EXPR
12527           && TREE_CODE (arg1) == INTEGER_CST
12528           && 0 != (tem = negate_expr (fold_convert_loc (loc, TREE_TYPE (arg0),
12529                                                         arg1)))
12530           && TREE_CODE (tem) == INTEGER_CST
12531           && !TREE_OVERFLOW (tem))
12532         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12533
12534       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
12535       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12536           && TREE_CODE (arg1) == INTEGER_CST
12537           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12538         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12539                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12540                                          fold_convert_loc (loc,
12541                                                            TREE_TYPE (arg0),
12542                                                            arg1),
12543                                          TREE_OPERAND (arg0, 1)));
12544
12545       /* Transform comparisons of the form X +- Y CMP X to Y CMP 0.  */
12546       if ((TREE_CODE (arg0) == PLUS_EXPR
12547            || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12548            || TREE_CODE (arg0) == MINUS_EXPR)
12549           && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12550                                                                         0)),
12551                               arg1, 0)
12552           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12553               || POINTER_TYPE_P (TREE_TYPE (arg0))))
12554         {
12555           tree val = TREE_OPERAND (arg0, 1);
12556           return omit_two_operands_loc (loc, type,
12557                                     fold_build2_loc (loc, code, type,
12558                                                  val,
12559                                                  build_int_cst (TREE_TYPE (val),
12560                                                                 0)),
12561                                     TREE_OPERAND (arg0, 0), arg1);
12562         }
12563
12564       /* Transform comparisons of the form C - X CMP X if C % 2 == 1.  */
12565       if (TREE_CODE (arg0) == MINUS_EXPR
12566           && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12567           && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12568                                                                         1)),
12569                               arg1, 0)
12570           && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12571         {
12572           return omit_two_operands_loc (loc, type,
12573                                     code == NE_EXPR
12574                                     ? boolean_true_node : boolean_false_node,
12575                                     TREE_OPERAND (arg0, 1), arg1);
12576         }
12577
12578       /* If we have X - Y == 0, we can convert that to X == Y and similarly
12579          for !=.  Don't do this for ordered comparisons due to overflow.  */
12580       if (TREE_CODE (arg0) == MINUS_EXPR
12581           && integer_zerop (arg1))
12582         return fold_build2_loc (loc, code, type,
12583                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12584
12585       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
12586       if (TREE_CODE (arg0) == ABS_EXPR
12587           && (integer_zerop (arg1) || real_zerop (arg1)))
12588         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12589
12590       /* If this is an EQ or NE comparison with zero and ARG0 is
12591          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
12592          two operations, but the latter can be done in one less insn
12593          on machines that have only two-operand insns or on which a
12594          constant cannot be the first operand.  */
12595       if (TREE_CODE (arg0) == BIT_AND_EXPR
12596           && integer_zerop (arg1))
12597         {
12598           tree arg00 = TREE_OPERAND (arg0, 0);
12599           tree arg01 = TREE_OPERAND (arg0, 1);
12600           if (TREE_CODE (arg00) == LSHIFT_EXPR
12601               && integer_onep (TREE_OPERAND (arg00, 0)))
12602             {
12603               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12604                                       arg01, TREE_OPERAND (arg00, 1));
12605               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12606                                  build_int_cst (TREE_TYPE (arg0), 1));
12607               return fold_build2_loc (loc, code, type,
12608                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12609                                   arg1);
12610             }
12611           else if (TREE_CODE (arg01) == LSHIFT_EXPR
12612                    && integer_onep (TREE_OPERAND (arg01, 0)))
12613             {
12614               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12615                                       arg00, TREE_OPERAND (arg01, 1));
12616               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12617                                  build_int_cst (TREE_TYPE (arg0), 1));
12618               return fold_build2_loc (loc, code, type,
12619                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12620                                   arg1);
12621             }
12622         }
12623
12624       /* If this is an NE or EQ comparison of zero against the result of a
12625          signed MOD operation whose second operand is a power of 2, make
12626          the MOD operation unsigned since it is simpler and equivalent.  */
12627       if (integer_zerop (arg1)
12628           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12629           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12630               || TREE_CODE (arg0) == CEIL_MOD_EXPR
12631               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12632               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12633           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12634         {
12635           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12636           tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12637                                      fold_convert_loc (loc, newtype,
12638                                                        TREE_OPERAND (arg0, 0)),
12639                                      fold_convert_loc (loc, newtype,
12640                                                        TREE_OPERAND (arg0, 1)));
12641
12642           return fold_build2_loc (loc, code, type, newmod,
12643                               fold_convert_loc (loc, newtype, arg1));
12644         }
12645
12646       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12647          C1 is a valid shift constant, and C2 is a power of two, i.e.
12648          a single bit.  */
12649       if (TREE_CODE (arg0) == BIT_AND_EXPR
12650           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12651           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12652              == INTEGER_CST
12653           && integer_pow2p (TREE_OPERAND (arg0, 1))
12654           && integer_zerop (arg1))
12655         {
12656           tree itype = TREE_TYPE (arg0);
12657           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12658           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12659
12660           /* Check for a valid shift count.  */
12661           if (TREE_INT_CST_HIGH (arg001) == 0
12662               && TREE_INT_CST_LOW (arg001) < prec)
12663             {
12664               tree arg01 = TREE_OPERAND (arg0, 1);
12665               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12666               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12667               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12668                  can be rewritten as (X & (C2 << C1)) != 0.  */
12669               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12670                 {
12671                   tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12672                   tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12673                   return fold_build2_loc (loc, code, type, tem,
12674                                           fold_convert_loc (loc, itype, arg1));
12675                 }
12676               /* Otherwise, for signed (arithmetic) shifts,
12677                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12678                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
12679               else if (!TYPE_UNSIGNED (itype))
12680                 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12681                                     arg000, build_int_cst (itype, 0));
12682               /* Otherwise, of unsigned (logical) shifts,
12683                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12684                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
12685               else
12686                 return omit_one_operand_loc (loc, type,
12687                                          code == EQ_EXPR ? integer_one_node
12688                                                          : integer_zero_node,
12689                                          arg000);
12690             }
12691         }
12692
12693       /* If we have (A & C) == C where C is a power of 2, convert this into
12694          (A & C) != 0.  Similarly for NE_EXPR.  */
12695       if (TREE_CODE (arg0) == BIT_AND_EXPR
12696           && integer_pow2p (TREE_OPERAND (arg0, 1))
12697           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12698         return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12699                             arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12700                                                     integer_zero_node));
12701
12702       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12703          bit, then fold the expression into A < 0 or A >= 0.  */
12704       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12705       if (tem)
12706         return tem;
12707
12708       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12709          Similarly for NE_EXPR.  */
12710       if (TREE_CODE (arg0) == BIT_AND_EXPR
12711           && TREE_CODE (arg1) == INTEGER_CST
12712           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12713         {
12714           tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12715                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
12716                                    TREE_OPERAND (arg0, 1));
12717           tree dandnotc
12718             = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12719                                fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
12720                                notc);
12721           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12722           if (integer_nonzerop (dandnotc))
12723             return omit_one_operand_loc (loc, type, rslt, arg0);
12724         }
12725
12726       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12727          Similarly for NE_EXPR.  */
12728       if (TREE_CODE (arg0) == BIT_IOR_EXPR
12729           && TREE_CODE (arg1) == INTEGER_CST
12730           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12731         {
12732           tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12733           tree candnotd
12734             = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12735                                TREE_OPERAND (arg0, 1),
12736                                fold_convert_loc (loc, TREE_TYPE (arg0), notd));
12737           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12738           if (integer_nonzerop (candnotd))
12739             return omit_one_operand_loc (loc, type, rslt, arg0);
12740         }
12741
12742       /* If this is a comparison of a field, we may be able to simplify it.  */
12743       if ((TREE_CODE (arg0) == COMPONENT_REF
12744            || TREE_CODE (arg0) == BIT_FIELD_REF)
12745           /* Handle the constant case even without -O
12746              to make sure the warnings are given.  */
12747           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12748         {
12749           t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12750           if (t1)
12751             return t1;
12752         }
12753
12754       /* Optimize comparisons of strlen vs zero to a compare of the
12755          first character of the string vs zero.  To wit,
12756                 strlen(ptr) == 0   =>  *ptr == 0
12757                 strlen(ptr) != 0   =>  *ptr != 0
12758          Other cases should reduce to one of these two (or a constant)
12759          due to the return value of strlen being unsigned.  */
12760       if (TREE_CODE (arg0) == CALL_EXPR
12761           && integer_zerop (arg1))
12762         {
12763           tree fndecl = get_callee_fndecl (arg0);
12764
12765           if (fndecl
12766               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12767               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12768               && call_expr_nargs (arg0) == 1
12769               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12770             {
12771               tree iref = build_fold_indirect_ref_loc (loc,
12772                                                    CALL_EXPR_ARG (arg0, 0));
12773               return fold_build2_loc (loc, code, type, iref,
12774                                   build_int_cst (TREE_TYPE (iref), 0));
12775             }
12776         }
12777
12778       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12779          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12780       if (TREE_CODE (arg0) == RSHIFT_EXPR
12781           && integer_zerop (arg1)
12782           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12783         {
12784           tree arg00 = TREE_OPERAND (arg0, 0);
12785           tree arg01 = TREE_OPERAND (arg0, 1);
12786           tree itype = TREE_TYPE (arg00);
12787           if (TREE_INT_CST_HIGH (arg01) == 0
12788               && TREE_INT_CST_LOW (arg01)
12789                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12790             {
12791               if (TYPE_UNSIGNED (itype))
12792                 {
12793                   itype = signed_type_for (itype);
12794                   arg00 = fold_convert_loc (loc, itype, arg00);
12795                 }
12796               return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12797                                   type, arg00, build_int_cst (itype, 0));
12798             }
12799         }
12800
12801       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12802       if (integer_zerop (arg1)
12803           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12804         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12805                             TREE_OPERAND (arg0, 1));
12806
12807       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12808       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12809           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12810         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12811                                 build_int_cst (TREE_TYPE (arg0), 0));
12812       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12813       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12814           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12815           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12816         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12817                                 build_int_cst (TREE_TYPE (arg0), 0));
12818
12819       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12820       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12821           && TREE_CODE (arg1) == INTEGER_CST
12822           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12823         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12824                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12825                                          TREE_OPERAND (arg0, 1), arg1));
12826
12827       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12828          (X & C) == 0 when C is a single bit.  */
12829       if (TREE_CODE (arg0) == BIT_AND_EXPR
12830           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12831           && integer_zerop (arg1)
12832           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12833         {
12834           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12835                                  TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12836                                  TREE_OPERAND (arg0, 1));
12837           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12838                                   type, tem,
12839                                   fold_convert_loc (loc, TREE_TYPE (arg0),
12840                                                     arg1));
12841         }
12842
12843       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12844          constant C is a power of two, i.e. a single bit.  */
12845       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12846           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12847           && integer_zerop (arg1)
12848           && integer_pow2p (TREE_OPERAND (arg0, 1))
12849           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12850                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12851         {
12852           tree arg00 = TREE_OPERAND (arg0, 0);
12853           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12854                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
12855         }
12856
12857       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12858          when is C is a power of two, i.e. a single bit.  */
12859       if (TREE_CODE (arg0) == BIT_AND_EXPR
12860           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12861           && integer_zerop (arg1)
12862           && integer_pow2p (TREE_OPERAND (arg0, 1))
12863           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12864                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12865         {
12866           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12867           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12868                              arg000, TREE_OPERAND (arg0, 1));
12869           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12870                               tem, build_int_cst (TREE_TYPE (tem), 0));
12871         }
12872
12873       if (integer_zerop (arg1)
12874           && tree_expr_nonzero_p (arg0))
12875         {
12876           tree res = constant_boolean_node (code==NE_EXPR, type);
12877           return omit_one_operand_loc (loc, type, res, arg0);
12878         }
12879
12880       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
12881       if (TREE_CODE (arg0) == NEGATE_EXPR
12882           && TREE_CODE (arg1) == NEGATE_EXPR)
12883         return fold_build2_loc (loc, code, type,
12884                                 TREE_OPERAND (arg0, 0),
12885                                 fold_convert_loc (loc, TREE_TYPE (arg0),
12886                                                   TREE_OPERAND (arg1, 0)));
12887
12888       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
12889       if (TREE_CODE (arg0) == BIT_AND_EXPR
12890           && TREE_CODE (arg1) == BIT_AND_EXPR)
12891         {
12892           tree arg00 = TREE_OPERAND (arg0, 0);
12893           tree arg01 = TREE_OPERAND (arg0, 1);
12894           tree arg10 = TREE_OPERAND (arg1, 0);
12895           tree arg11 = TREE_OPERAND (arg1, 1);
12896           tree itype = TREE_TYPE (arg0);
12897
12898           if (operand_equal_p (arg01, arg11, 0))
12899             return fold_build2_loc (loc, code, type,
12900                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12901                                              fold_build2_loc (loc,
12902                                                           BIT_XOR_EXPR, itype,
12903                                                           arg00, arg10),
12904                                              arg01),
12905                                 build_int_cst (itype, 0));
12906
12907           if (operand_equal_p (arg01, arg10, 0))
12908             return fold_build2_loc (loc, code, type,
12909                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12910                                              fold_build2_loc (loc,
12911                                                           BIT_XOR_EXPR, itype,
12912                                                           arg00, arg11),
12913                                              arg01),
12914                                 build_int_cst (itype, 0));
12915
12916           if (operand_equal_p (arg00, arg11, 0))
12917             return fold_build2_loc (loc, code, type,
12918                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12919                                              fold_build2_loc (loc,
12920                                                           BIT_XOR_EXPR, itype,
12921                                                           arg01, arg10),
12922                                              arg00),
12923                                 build_int_cst (itype, 0));
12924
12925           if (operand_equal_p (arg00, arg10, 0))
12926             return fold_build2_loc (loc, code, type,
12927                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12928                                              fold_build2_loc (loc,
12929                                                           BIT_XOR_EXPR, itype,
12930                                                           arg01, arg11),
12931                                              arg00),
12932                                 build_int_cst (itype, 0));
12933         }
12934
12935       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12936           && TREE_CODE (arg1) == BIT_XOR_EXPR)
12937         {
12938           tree arg00 = TREE_OPERAND (arg0, 0);
12939           tree arg01 = TREE_OPERAND (arg0, 1);
12940           tree arg10 = TREE_OPERAND (arg1, 0);
12941           tree arg11 = TREE_OPERAND (arg1, 1);
12942           tree itype = TREE_TYPE (arg0);
12943
12944           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12945              operand_equal_p guarantees no side-effects so we don't need
12946              to use omit_one_operand on Z.  */
12947           if (operand_equal_p (arg01, arg11, 0))
12948             return fold_build2_loc (loc, code, type, arg00,
12949                                     fold_convert_loc (loc, TREE_TYPE (arg00),
12950                                                       arg10));
12951           if (operand_equal_p (arg01, arg10, 0))
12952             return fold_build2_loc (loc, code, type, arg00,
12953                                     fold_convert_loc (loc, TREE_TYPE (arg00),
12954                                                       arg11));
12955           if (operand_equal_p (arg00, arg11, 0))
12956             return fold_build2_loc (loc, code, type, arg01,
12957                                     fold_convert_loc (loc, TREE_TYPE (arg01),
12958                                                       arg10));
12959           if (operand_equal_p (arg00, arg10, 0))
12960             return fold_build2_loc (loc, code, type, arg01,
12961                                     fold_convert_loc (loc, TREE_TYPE (arg01),
12962                                                       arg11));
12963
12964           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
12965           if (TREE_CODE (arg01) == INTEGER_CST
12966               && TREE_CODE (arg11) == INTEGER_CST)
12967             {
12968               tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12969                                      fold_convert_loc (loc, itype, arg11));
12970               tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12971               return fold_build2_loc (loc, code, type, tem,
12972                                       fold_convert_loc (loc, itype, arg10));
12973             }
12974         }
12975
12976       /* Attempt to simplify equality/inequality comparisons of complex
12977          values.  Only lower the comparison if the result is known or
12978          can be simplified to a single scalar comparison.  */
12979       if ((TREE_CODE (arg0) == COMPLEX_EXPR
12980            || TREE_CODE (arg0) == COMPLEX_CST)
12981           && (TREE_CODE (arg1) == COMPLEX_EXPR
12982               || TREE_CODE (arg1) == COMPLEX_CST))
12983         {
12984           tree real0, imag0, real1, imag1;
12985           tree rcond, icond;
12986
12987           if (TREE_CODE (arg0) == COMPLEX_EXPR)
12988             {
12989               real0 = TREE_OPERAND (arg0, 0);
12990               imag0 = TREE_OPERAND (arg0, 1);
12991             }
12992           else
12993             {
12994               real0 = TREE_REALPART (arg0);
12995               imag0 = TREE_IMAGPART (arg0);
12996             }
12997
12998           if (TREE_CODE (arg1) == COMPLEX_EXPR)
12999             {
13000               real1 = TREE_OPERAND (arg1, 0);
13001               imag1 = TREE_OPERAND (arg1, 1);
13002             }
13003           else
13004             {
13005               real1 = TREE_REALPART (arg1);
13006               imag1 = TREE_IMAGPART (arg1);
13007             }
13008
13009           rcond = fold_binary_loc (loc, code, type, real0, real1);
13010           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
13011             {
13012               if (integer_zerop (rcond))
13013                 {
13014                   if (code == EQ_EXPR)
13015                     return omit_two_operands_loc (loc, type, boolean_false_node,
13016                                               imag0, imag1);
13017                   return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
13018                 }
13019               else
13020                 {
13021                   if (code == NE_EXPR)
13022                     return omit_two_operands_loc (loc, type, boolean_true_node,
13023                                               imag0, imag1);
13024                   return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
13025                 }
13026             }
13027
13028           icond = fold_binary_loc (loc, code, type, imag0, imag1);
13029           if (icond && TREE_CODE (icond) == INTEGER_CST)
13030             {
13031               if (integer_zerop (icond))
13032                 {
13033                   if (code == EQ_EXPR)
13034                     return omit_two_operands_loc (loc, type, boolean_false_node,
13035                                               real0, real1);
13036                   return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
13037                 }
13038               else
13039                 {
13040                   if (code == NE_EXPR)
13041                     return omit_two_operands_loc (loc, type, boolean_true_node,
13042                                               real0, real1);
13043                   return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
13044                 }
13045             }
13046         }
13047
13048       return NULL_TREE;
13049
13050     case LT_EXPR:
13051     case GT_EXPR:
13052     case LE_EXPR:
13053     case GE_EXPR:
13054       tem = fold_comparison (loc, code, type, op0, op1);
13055       if (tem != NULL_TREE)
13056         return tem;
13057
13058       /* Transform comparisons of the form X +- C CMP X.  */
13059       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
13060           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
13061           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
13062                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
13063               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
13064                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
13065         {
13066           tree arg01 = TREE_OPERAND (arg0, 1);
13067           enum tree_code code0 = TREE_CODE (arg0);
13068           int is_positive;
13069
13070           if (TREE_CODE (arg01) == REAL_CST)
13071             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
13072           else
13073             is_positive = tree_int_cst_sgn (arg01);
13074
13075           /* (X - c) > X becomes false.  */
13076           if (code == GT_EXPR
13077               && ((code0 == MINUS_EXPR && is_positive >= 0)
13078                   || (code0 == PLUS_EXPR && is_positive <= 0)))
13079             {
13080               if (TREE_CODE (arg01) == INTEGER_CST
13081                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13082                 fold_overflow_warning (("assuming signed overflow does not "
13083                                         "occur when assuming that (X - c) > X "
13084                                         "is always false"),
13085                                        WARN_STRICT_OVERFLOW_ALL);
13086               return constant_boolean_node (0, type);
13087             }
13088
13089           /* Likewise (X + c) < X becomes false.  */
13090           if (code == LT_EXPR
13091               && ((code0 == PLUS_EXPR && is_positive >= 0)
13092                   || (code0 == MINUS_EXPR && is_positive <= 0)))
13093             {
13094               if (TREE_CODE (arg01) == INTEGER_CST
13095                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13096                 fold_overflow_warning (("assuming signed overflow does not "
13097                                         "occur when assuming that "
13098                                         "(X + c) < X is always false"),
13099                                        WARN_STRICT_OVERFLOW_ALL);
13100               return constant_boolean_node (0, type);
13101             }
13102
13103           /* Convert (X - c) <= X to true.  */
13104           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
13105               && code == LE_EXPR
13106               && ((code0 == MINUS_EXPR && is_positive >= 0)
13107                   || (code0 == PLUS_EXPR && is_positive <= 0)))
13108             {
13109               if (TREE_CODE (arg01) == INTEGER_CST
13110                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13111                 fold_overflow_warning (("assuming signed overflow does not "
13112                                         "occur when assuming that "
13113                                         "(X - c) <= X is always true"),
13114                                        WARN_STRICT_OVERFLOW_ALL);
13115               return constant_boolean_node (1, type);
13116             }
13117
13118           /* Convert (X + c) >= X to true.  */
13119           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
13120               && code == GE_EXPR
13121               && ((code0 == PLUS_EXPR && is_positive >= 0)
13122                   || (code0 == MINUS_EXPR && is_positive <= 0)))
13123             {
13124               if (TREE_CODE (arg01) == INTEGER_CST
13125                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13126                 fold_overflow_warning (("assuming signed overflow does not "
13127                                         "occur when assuming that "
13128                                         "(X + c) >= X is always true"),
13129                                        WARN_STRICT_OVERFLOW_ALL);
13130               return constant_boolean_node (1, type);
13131             }
13132
13133           if (TREE_CODE (arg01) == INTEGER_CST)
13134             {
13135               /* Convert X + c > X and X - c < X to true for integers.  */
13136               if (code == GT_EXPR
13137                   && ((code0 == PLUS_EXPR && is_positive > 0)
13138                       || (code0 == MINUS_EXPR && is_positive < 0)))
13139                 {
13140                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13141                     fold_overflow_warning (("assuming signed overflow does "
13142                                             "not occur when assuming that "
13143                                             "(X + c) > X is always true"),
13144                                            WARN_STRICT_OVERFLOW_ALL);
13145                   return constant_boolean_node (1, type);
13146                 }
13147
13148               if (code == LT_EXPR
13149                   && ((code0 == MINUS_EXPR && is_positive > 0)
13150                       || (code0 == PLUS_EXPR && is_positive < 0)))
13151                 {
13152                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13153                     fold_overflow_warning (("assuming signed overflow does "
13154                                             "not occur when assuming that "
13155                                             "(X - c) < X is always true"),
13156                                            WARN_STRICT_OVERFLOW_ALL);
13157                   return constant_boolean_node (1, type);
13158                 }
13159
13160               /* Convert X + c <= X and X - c >= X to false for integers.  */
13161               if (code == LE_EXPR
13162                   && ((code0 == PLUS_EXPR && is_positive > 0)
13163                       || (code0 == MINUS_EXPR && is_positive < 0)))
13164                 {
13165                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13166                     fold_overflow_warning (("assuming signed overflow does "
13167                                             "not occur when assuming that "
13168                                             "(X + c) <= X is always false"),
13169                                            WARN_STRICT_OVERFLOW_ALL);
13170                   return constant_boolean_node (0, type);
13171                 }
13172
13173               if (code == GE_EXPR
13174                   && ((code0 == MINUS_EXPR && is_positive > 0)
13175                       || (code0 == PLUS_EXPR && is_positive < 0)))
13176                 {
13177                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13178                     fold_overflow_warning (("assuming signed overflow does "
13179                                             "not occur when assuming that "
13180                                             "(X - c) >= X is always false"),
13181                                            WARN_STRICT_OVERFLOW_ALL);
13182                   return constant_boolean_node (0, type);
13183                 }
13184             }
13185         }
13186
13187       /* Comparisons with the highest or lowest possible integer of
13188          the specified precision will have known values.  */
13189       {
13190         tree arg1_type = TREE_TYPE (arg1);
13191         unsigned int width = TYPE_PRECISION (arg1_type);
13192
13193         if (TREE_CODE (arg1) == INTEGER_CST
13194             && width <= 2 * HOST_BITS_PER_WIDE_INT
13195             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
13196           {
13197             HOST_WIDE_INT signed_max_hi;
13198             unsigned HOST_WIDE_INT signed_max_lo;
13199             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
13200
13201             if (width <= HOST_BITS_PER_WIDE_INT)
13202               {
13203                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
13204                                 - 1;
13205                 signed_max_hi = 0;
13206                 max_hi = 0;
13207
13208                 if (TYPE_UNSIGNED (arg1_type))
13209                   {
13210                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
13211                     min_lo = 0;
13212                     min_hi = 0;
13213                   }
13214                 else
13215                   {
13216                     max_lo = signed_max_lo;
13217                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
13218                     min_hi = -1;
13219                   }
13220               }
13221             else
13222               {
13223                 width -= HOST_BITS_PER_WIDE_INT;
13224                 signed_max_lo = -1;
13225                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
13226                                 - 1;
13227                 max_lo = -1;
13228                 min_lo = 0;
13229
13230                 if (TYPE_UNSIGNED (arg1_type))
13231                   {
13232                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
13233                     min_hi = 0;
13234                   }
13235                 else
13236                   {
13237                     max_hi = signed_max_hi;
13238                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
13239                   }
13240               }
13241
13242             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
13243                 && TREE_INT_CST_LOW (arg1) == max_lo)
13244               switch (code)
13245                 {
13246                 case GT_EXPR:
13247                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13248
13249                 case GE_EXPR:
13250                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13251
13252                 case LE_EXPR:
13253                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13254
13255                 case LT_EXPR:
13256                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13257
13258                 /* The GE_EXPR and LT_EXPR cases above are not normally
13259                    reached because of previous transformations.  */
13260
13261                 default:
13262                   break;
13263                 }
13264             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13265                      == max_hi
13266                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
13267               switch (code)
13268                 {
13269                 case GT_EXPR:
13270                   arg1 = const_binop (PLUS_EXPR, arg1,
13271                                       build_int_cst (TREE_TYPE (arg1), 1));
13272                   return fold_build2_loc (loc, EQ_EXPR, type,
13273                                       fold_convert_loc (loc,
13274                                                         TREE_TYPE (arg1), arg0),
13275                                       arg1);
13276                 case LE_EXPR:
13277                   arg1 = const_binop (PLUS_EXPR, arg1,
13278                                       build_int_cst (TREE_TYPE (arg1), 1));
13279                   return fold_build2_loc (loc, NE_EXPR, type,
13280                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13281                                                         arg0),
13282                                       arg1);
13283                 default:
13284                   break;
13285                 }
13286             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13287                      == min_hi
13288                      && TREE_INT_CST_LOW (arg1) == min_lo)
13289               switch (code)
13290                 {
13291                 case LT_EXPR:
13292                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13293
13294                 case LE_EXPR:
13295                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13296
13297                 case GE_EXPR:
13298                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13299
13300                 case GT_EXPR:
13301                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13302
13303                 default:
13304                   break;
13305                 }
13306             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13307                      == min_hi
13308                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
13309               switch (code)
13310                 {
13311                 case GE_EXPR:
13312                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
13313                   return fold_build2_loc (loc, NE_EXPR, type,
13314                                       fold_convert_loc (loc,
13315                                                         TREE_TYPE (arg1), arg0),
13316                                       arg1);
13317                 case LT_EXPR:
13318                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
13319                   return fold_build2_loc (loc, EQ_EXPR, type,
13320                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13321                                                         arg0),
13322                                       arg1);
13323                 default:
13324                   break;
13325                 }
13326
13327             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13328                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
13329                      && TYPE_UNSIGNED (arg1_type)
13330                      /* We will flip the signedness of the comparison operator
13331                         associated with the mode of arg1, so the sign bit is
13332                         specified by this mode.  Check that arg1 is the signed
13333                         max associated with this sign bit.  */
13334                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13335                      /* signed_type does not work on pointer types.  */
13336                      && INTEGRAL_TYPE_P (arg1_type))
13337               {
13338                 /* The following case also applies to X < signed_max+1
13339                    and X >= signed_max+1 because previous transformations.  */
13340                 if (code == LE_EXPR || code == GT_EXPR)
13341                   {
13342                     tree st;
13343                     st = signed_type_for (TREE_TYPE (arg1));
13344                     return fold_build2_loc (loc,
13345                                         code == LE_EXPR ? GE_EXPR : LT_EXPR,
13346                                         type, fold_convert_loc (loc, st, arg0),
13347                                         build_int_cst (st, 0));
13348                   }
13349               }
13350           }
13351       }
13352
13353       /* If we are comparing an ABS_EXPR with a constant, we can
13354          convert all the cases into explicit comparisons, but they may
13355          well not be faster than doing the ABS and one comparison.
13356          But ABS (X) <= C is a range comparison, which becomes a subtraction
13357          and a comparison, and is probably faster.  */
13358       if (code == LE_EXPR
13359           && TREE_CODE (arg1) == INTEGER_CST
13360           && TREE_CODE (arg0) == ABS_EXPR
13361           && ! TREE_SIDE_EFFECTS (arg0)
13362           && (0 != (tem = negate_expr (arg1)))
13363           && TREE_CODE (tem) == INTEGER_CST
13364           && !TREE_OVERFLOW (tem))
13365         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13366                             build2 (GE_EXPR, type,
13367                                     TREE_OPERAND (arg0, 0), tem),
13368                             build2 (LE_EXPR, type,
13369                                     TREE_OPERAND (arg0, 0), arg1));
13370
13371       /* Convert ABS_EXPR<x> >= 0 to true.  */
13372       strict_overflow_p = false;
13373       if (code == GE_EXPR
13374           && (integer_zerop (arg1)
13375               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13376                   && real_zerop (arg1)))
13377           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13378         {
13379           if (strict_overflow_p)
13380             fold_overflow_warning (("assuming signed overflow does not occur "
13381                                     "when simplifying comparison of "
13382                                     "absolute value and zero"),
13383                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13384           return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13385         }
13386
13387       /* Convert ABS_EXPR<x> < 0 to false.  */
13388       strict_overflow_p = false;
13389       if (code == LT_EXPR
13390           && (integer_zerop (arg1) || real_zerop (arg1))
13391           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13392         {
13393           if (strict_overflow_p)
13394             fold_overflow_warning (("assuming signed overflow does not occur "
13395                                     "when simplifying comparison of "
13396                                     "absolute value and zero"),
13397                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13398           return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13399         }
13400
13401       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13402          and similarly for >= into !=.  */
13403       if ((code == LT_EXPR || code == GE_EXPR)
13404           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13405           && TREE_CODE (arg1) == LSHIFT_EXPR
13406           && integer_onep (TREE_OPERAND (arg1, 0)))
13407         return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13408                            build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13409                                    TREE_OPERAND (arg1, 1)),
13410                            build_int_cst (TREE_TYPE (arg0), 0));
13411
13412       if ((code == LT_EXPR || code == GE_EXPR)
13413           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13414           && CONVERT_EXPR_P (arg1)
13415           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13416           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13417         {
13418           tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13419                         TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
13420           return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13421                              fold_convert_loc (loc, TREE_TYPE (arg0), tem),
13422                              build_int_cst (TREE_TYPE (arg0), 0));
13423         }
13424
13425       return NULL_TREE;
13426
13427     case UNORDERED_EXPR:
13428     case ORDERED_EXPR:
13429     case UNLT_EXPR:
13430     case UNLE_EXPR:
13431     case UNGT_EXPR:
13432     case UNGE_EXPR:
13433     case UNEQ_EXPR:
13434     case LTGT_EXPR:
13435       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13436         {
13437           t1 = fold_relational_const (code, type, arg0, arg1);
13438           if (t1 != NULL_TREE)
13439             return t1;
13440         }
13441
13442       /* If the first operand is NaN, the result is constant.  */
13443       if (TREE_CODE (arg0) == REAL_CST
13444           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13445           && (code != LTGT_EXPR || ! flag_trapping_math))
13446         {
13447           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13448                ? integer_zero_node
13449                : integer_one_node;
13450           return omit_one_operand_loc (loc, type, t1, arg1);
13451         }
13452
13453       /* If the second operand is NaN, the result is constant.  */
13454       if (TREE_CODE (arg1) == REAL_CST
13455           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13456           && (code != LTGT_EXPR || ! flag_trapping_math))
13457         {
13458           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13459                ? integer_zero_node
13460                : integer_one_node;
13461           return omit_one_operand_loc (loc, type, t1, arg0);
13462         }
13463
13464       /* Simplify unordered comparison of something with itself.  */
13465       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13466           && operand_equal_p (arg0, arg1, 0))
13467         return constant_boolean_node (1, type);
13468
13469       if (code == LTGT_EXPR
13470           && !flag_trapping_math
13471           && operand_equal_p (arg0, arg1, 0))
13472         return constant_boolean_node (0, type);
13473
13474       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
13475       {
13476         tree targ0 = strip_float_extensions (arg0);
13477         tree targ1 = strip_float_extensions (arg1);
13478         tree newtype = TREE_TYPE (targ0);
13479
13480         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13481           newtype = TREE_TYPE (targ1);
13482
13483         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13484           return fold_build2_loc (loc, code, type,
13485                               fold_convert_loc (loc, newtype, targ0),
13486                               fold_convert_loc (loc, newtype, targ1));
13487       }
13488
13489       return NULL_TREE;
13490
13491     case COMPOUND_EXPR:
13492       /* When pedantic, a compound expression can be neither an lvalue
13493          nor an integer constant expression.  */
13494       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13495         return NULL_TREE;
13496       /* Don't let (0, 0) be null pointer constant.  */
13497       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13498                                  : fold_convert_loc (loc, type, arg1);
13499       return pedantic_non_lvalue_loc (loc, tem);
13500
13501     case COMPLEX_EXPR:
13502       if ((TREE_CODE (arg0) == REAL_CST
13503            && TREE_CODE (arg1) == REAL_CST)
13504           || (TREE_CODE (arg0) == INTEGER_CST
13505               && TREE_CODE (arg1) == INTEGER_CST))
13506         return build_complex (type, arg0, arg1);
13507       if (TREE_CODE (arg0) == REALPART_EXPR
13508           && TREE_CODE (arg1) == IMAGPART_EXPR
13509           && TREE_TYPE (TREE_OPERAND (arg0, 0)) == type
13510           && operand_equal_p (TREE_OPERAND (arg0, 0),
13511                               TREE_OPERAND (arg1, 0), 0))
13512         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
13513                                      TREE_OPERAND (arg1, 0));
13514       return NULL_TREE;
13515
13516     case ASSERT_EXPR:
13517       /* An ASSERT_EXPR should never be passed to fold_binary.  */
13518       gcc_unreachable ();
13519
13520     case VEC_PACK_TRUNC_EXPR:
13521     case VEC_PACK_FIX_TRUNC_EXPR:
13522       {
13523         unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
13524         tree *elts, vals = NULL_TREE;
13525
13526         gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts / 2
13527                     && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2);
13528         if (TREE_CODE (arg0) != VECTOR_CST || TREE_CODE (arg1) != VECTOR_CST)
13529           return NULL_TREE;
13530
13531         elts = XALLOCAVEC (tree, nelts);
13532         if (!vec_cst_ctor_to_array (arg0, elts)
13533             || !vec_cst_ctor_to_array (arg1, elts + nelts / 2))
13534           return NULL_TREE;
13535
13536         for (i = 0; i < nelts; i++)
13537           {
13538             elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
13539                                           ? NOP_EXPR : FIX_TRUNC_EXPR,
13540                                           TREE_TYPE (type), elts[i]);
13541             if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
13542               return NULL_TREE;
13543           }
13544
13545         for (i = 0; i < nelts; i++)
13546           vals = tree_cons (NULL_TREE, elts[nelts - i - 1], vals);
13547         return build_vector (type, vals);
13548       }
13549
13550     case VEC_WIDEN_MULT_LO_EXPR:
13551     case VEC_WIDEN_MULT_HI_EXPR:
13552       {
13553         unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
13554         tree *elts, vals = NULL_TREE;
13555
13556         gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2
13557                     && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2);
13558         if (TREE_CODE (arg0) != VECTOR_CST || TREE_CODE (arg1) != VECTOR_CST)
13559           return NULL_TREE;
13560
13561         elts = XALLOCAVEC (tree, nelts * 4);
13562         if (!vec_cst_ctor_to_array (arg0, elts)
13563             || !vec_cst_ctor_to_array (arg1, elts + nelts * 2))
13564           return NULL_TREE;
13565
13566         if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_WIDEN_MULT_LO_EXPR))
13567           elts += nelts;
13568
13569         for (i = 0; i < nelts; i++)
13570           {
13571             elts[i] = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[i]);
13572             elts[i + nelts * 2]
13573               = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
13574                                     elts[i + nelts * 2]);
13575             if (elts[i] == NULL_TREE || elts[i + nelts * 2] == NULL_TREE)
13576               return NULL_TREE;
13577             elts[i] = const_binop (MULT_EXPR, elts[i], elts[i + nelts * 2]);
13578             if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
13579               return NULL_TREE;
13580           }
13581
13582         for (i = 0; i < nelts; i++)
13583           vals = tree_cons (NULL_TREE, elts[nelts - i - 1], vals);
13584         return build_vector (type, vals);
13585       }
13586
13587     default:
13588       return NULL_TREE;
13589     } /* switch (code) */
13590 }
13591
13592 /* Callback for walk_tree, looking for LABEL_EXPR.  Return *TP if it is
13593    a LABEL_EXPR; otherwise return NULL_TREE.  Do not check the subtrees
13594    of GOTO_EXPR.  */
13595
13596 static tree
13597 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13598 {
13599   switch (TREE_CODE (*tp))
13600     {
13601     case LABEL_EXPR:
13602       return *tp;
13603
13604     case GOTO_EXPR:
13605       *walk_subtrees = 0;
13606
13607       /* ... fall through ...  */
13608
13609     default:
13610       return NULL_TREE;
13611     }
13612 }
13613
13614 /* Return whether the sub-tree ST contains a label which is accessible from
13615    outside the sub-tree.  */
13616
13617 static bool
13618 contains_label_p (tree st)
13619 {
13620   return
13621    (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13622 }
13623
13624 /* Fold a ternary expression of code CODE and type TYPE with operands
13625    OP0, OP1, and OP2.  Return the folded expression if folding is
13626    successful.  Otherwise, return NULL_TREE.  */
13627
13628 tree
13629 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13630                   tree op0, tree op1, tree op2)
13631 {
13632   tree tem;
13633   tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
13634   enum tree_code_class kind = TREE_CODE_CLASS (code);
13635
13636   gcc_assert (IS_EXPR_CODE_CLASS (kind)
13637               && TREE_CODE_LENGTH (code) == 3);
13638
13639   /* Strip any conversions that don't change the mode.  This is safe
13640      for every expression, except for a comparison expression because
13641      its signedness is derived from its operands.  So, in the latter
13642      case, only strip conversions that don't change the signedness.
13643
13644      Note that this is done as an internal manipulation within the
13645      constant folder, in order to find the simplest representation of
13646      the arguments so that their form can be studied.  In any cases,
13647      the appropriate type conversions should be put back in the tree
13648      that will get out of the constant folder.  */
13649   if (op0)
13650     {
13651       arg0 = op0;
13652       STRIP_NOPS (arg0);
13653     }
13654
13655   if (op1)
13656     {
13657       arg1 = op1;
13658       STRIP_NOPS (arg1);
13659     }
13660
13661   if (op2)
13662     {
13663       arg2 = op2;
13664       STRIP_NOPS (arg2);
13665     }
13666
13667   switch (code)
13668     {
13669     case COMPONENT_REF:
13670       if (TREE_CODE (arg0) == CONSTRUCTOR
13671           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13672         {
13673           unsigned HOST_WIDE_INT idx;
13674           tree field, value;
13675           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13676             if (field == arg1)
13677               return value;
13678         }
13679       return NULL_TREE;
13680
13681     case COND_EXPR:
13682       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13683          so all simple results must be passed through pedantic_non_lvalue.  */
13684       if (TREE_CODE (arg0) == INTEGER_CST)
13685         {
13686           tree unused_op = integer_zerop (arg0) ? op1 : op2;
13687           tem = integer_zerop (arg0) ? op2 : op1;
13688           /* Only optimize constant conditions when the selected branch
13689              has the same type as the COND_EXPR.  This avoids optimizing
13690              away "c ? x : throw", where the throw has a void type.
13691              Avoid throwing away that operand which contains label.  */
13692           if ((!TREE_SIDE_EFFECTS (unused_op)
13693                || !contains_label_p (unused_op))
13694               && (! VOID_TYPE_P (TREE_TYPE (tem))
13695                   || VOID_TYPE_P (type)))
13696             return pedantic_non_lvalue_loc (loc, tem);
13697           return NULL_TREE;
13698         }
13699       if (operand_equal_p (arg1, op2, 0))
13700         return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13701
13702       /* If we have A op B ? A : C, we may be able to convert this to a
13703          simpler expression, depending on the operation and the values
13704          of B and C.  Signed zeros prevent all of these transformations,
13705          for reasons given above each one.
13706
13707          Also try swapping the arguments and inverting the conditional.  */
13708       if (COMPARISON_CLASS_P (arg0)
13709           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13710                                              arg1, TREE_OPERAND (arg0, 1))
13711           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13712         {
13713           tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13714           if (tem)
13715             return tem;
13716         }
13717
13718       if (COMPARISON_CLASS_P (arg0)
13719           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13720                                              op2,
13721                                              TREE_OPERAND (arg0, 1))
13722           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13723         {
13724           location_t loc0 = expr_location_or (arg0, loc);
13725           tem = fold_truth_not_expr (loc0, arg0);
13726           if (tem && COMPARISON_CLASS_P (tem))
13727             {
13728               tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13729               if (tem)
13730                 return tem;
13731             }
13732         }
13733
13734       /* If the second operand is simpler than the third, swap them
13735          since that produces better jump optimization results.  */
13736       if (truth_value_p (TREE_CODE (arg0))
13737           && tree_swap_operands_p (op1, op2, false))
13738         {
13739           location_t loc0 = expr_location_or (arg0, loc);
13740           /* See if this can be inverted.  If it can't, possibly because
13741              it was a floating-point inequality comparison, don't do
13742              anything.  */
13743           tem = fold_truth_not_expr (loc0, arg0);
13744           if (tem)
13745             return fold_build3_loc (loc, code, type, tem, op2, op1);
13746         }
13747
13748       /* Convert A ? 1 : 0 to simply A.  */
13749       if (integer_onep (op1)
13750           && integer_zerop (op2)
13751           /* If we try to convert OP0 to our type, the
13752              call to fold will try to move the conversion inside
13753              a COND, which will recurse.  In that case, the COND_EXPR
13754              is probably the best choice, so leave it alone.  */
13755           && type == TREE_TYPE (arg0))
13756         return pedantic_non_lvalue_loc (loc, arg0);
13757
13758       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
13759          over COND_EXPR in cases such as floating point comparisons.  */
13760       if (integer_zerop (op1)
13761           && integer_onep (op2)
13762           && truth_value_p (TREE_CODE (arg0)))
13763         return pedantic_non_lvalue_loc (loc,
13764                                     fold_convert_loc (loc, type,
13765                                               invert_truthvalue_loc (loc,
13766                                                                      arg0)));
13767
13768       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
13769       if (TREE_CODE (arg0) == LT_EXPR
13770           && integer_zerop (TREE_OPERAND (arg0, 1))
13771           && integer_zerop (op2)
13772           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13773         {
13774           /* sign_bit_p only checks ARG1 bits within A's precision.
13775              If <sign bit of A> has wider type than A, bits outside
13776              of A's precision in <sign bit of A> need to be checked.
13777              If they are all 0, this optimization needs to be done
13778              in unsigned A's type, if they are all 1 in signed A's type,
13779              otherwise this can't be done.  */
13780           if (TYPE_PRECISION (TREE_TYPE (tem))
13781               < TYPE_PRECISION (TREE_TYPE (arg1))
13782               && TYPE_PRECISION (TREE_TYPE (tem))
13783                  < TYPE_PRECISION (type))
13784             {
13785               unsigned HOST_WIDE_INT mask_lo;
13786               HOST_WIDE_INT mask_hi;
13787               int inner_width, outer_width;
13788               tree tem_type;
13789
13790               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13791               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13792               if (outer_width > TYPE_PRECISION (type))
13793                 outer_width = TYPE_PRECISION (type);
13794
13795               if (outer_width > HOST_BITS_PER_WIDE_INT)
13796                 {
13797                   mask_hi = ((unsigned HOST_WIDE_INT) -1
13798                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13799                   mask_lo = -1;
13800                 }
13801               else
13802                 {
13803                   mask_hi = 0;
13804                   mask_lo = ((unsigned HOST_WIDE_INT) -1
13805                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
13806                 }
13807               if (inner_width > HOST_BITS_PER_WIDE_INT)
13808                 {
13809                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13810                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
13811                   mask_lo = 0;
13812                 }
13813               else
13814                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13815                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
13816
13817               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13818                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13819                 {
13820                   tem_type = signed_type_for (TREE_TYPE (tem));
13821                   tem = fold_convert_loc (loc, tem_type, tem);
13822                 }
13823               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13824                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13825                 {
13826                   tem_type = unsigned_type_for (TREE_TYPE (tem));
13827                   tem = fold_convert_loc (loc, tem_type, tem);
13828                 }
13829               else
13830                 tem = NULL;
13831             }
13832
13833           if (tem)
13834             return
13835               fold_convert_loc (loc, type,
13836                                 fold_build2_loc (loc, BIT_AND_EXPR,
13837                                              TREE_TYPE (tem), tem,
13838                                              fold_convert_loc (loc,
13839                                                                TREE_TYPE (tem),
13840                                                                arg1)));
13841         }
13842
13843       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
13844          already handled above.  */
13845       if (TREE_CODE (arg0) == BIT_AND_EXPR
13846           && integer_onep (TREE_OPERAND (arg0, 1))
13847           && integer_zerop (op2)
13848           && integer_pow2p (arg1))
13849         {
13850           tree tem = TREE_OPERAND (arg0, 0);
13851           STRIP_NOPS (tem);
13852           if (TREE_CODE (tem) == RSHIFT_EXPR
13853               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13854               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13855                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13856             return fold_build2_loc (loc, BIT_AND_EXPR, type,
13857                                 TREE_OPERAND (tem, 0), arg1);
13858         }
13859
13860       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13861          is probably obsolete because the first operand should be a
13862          truth value (that's why we have the two cases above), but let's
13863          leave it in until we can confirm this for all front-ends.  */
13864       if (integer_zerop (op2)
13865           && TREE_CODE (arg0) == NE_EXPR
13866           && integer_zerop (TREE_OPERAND (arg0, 1))
13867           && integer_pow2p (arg1)
13868           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13869           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13870                               arg1, OEP_ONLY_CONST))
13871         return pedantic_non_lvalue_loc (loc,
13872                                     fold_convert_loc (loc, type,
13873                                                       TREE_OPERAND (arg0, 0)));
13874
13875       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13876       if (integer_zerop (op2)
13877           && truth_value_p (TREE_CODE (arg0))
13878           && truth_value_p (TREE_CODE (arg1)))
13879         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13880                             fold_convert_loc (loc, type, arg0),
13881                             arg1);
13882
13883       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13884       if (integer_onep (op2)
13885           && truth_value_p (TREE_CODE (arg0))
13886           && truth_value_p (TREE_CODE (arg1)))
13887         {
13888           location_t loc0 = expr_location_or (arg0, loc);
13889           /* Only perform transformation if ARG0 is easily inverted.  */
13890           tem = fold_truth_not_expr (loc0, arg0);
13891           if (tem)
13892             return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13893                                 fold_convert_loc (loc, type, tem),
13894                                 arg1);
13895         }
13896
13897       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13898       if (integer_zerop (arg1)
13899           && truth_value_p (TREE_CODE (arg0))
13900           && truth_value_p (TREE_CODE (op2)))
13901         {
13902           location_t loc0 = expr_location_or (arg0, loc);
13903           /* Only perform transformation if ARG0 is easily inverted.  */
13904           tem = fold_truth_not_expr (loc0, arg0);
13905           if (tem)
13906             return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13907                                 fold_convert_loc (loc, type, tem),
13908                                 op2);
13909         }
13910
13911       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13912       if (integer_onep (arg1)
13913           && truth_value_p (TREE_CODE (arg0))
13914           && truth_value_p (TREE_CODE (op2)))
13915         return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13916                             fold_convert_loc (loc, type, arg0),
13917                             op2);
13918
13919       return NULL_TREE;
13920
13921     case CALL_EXPR:
13922       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
13923          of fold_ternary on them.  */
13924       gcc_unreachable ();
13925
13926     case BIT_FIELD_REF:
13927       if ((TREE_CODE (arg0) == VECTOR_CST
13928            || TREE_CODE (arg0) == CONSTRUCTOR)
13929           && type == TREE_TYPE (TREE_TYPE (arg0)))
13930         {
13931           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13932           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13933
13934           if (width != 0
13935               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13936               && (idx % width) == 0
13937               && (idx = idx / width)
13938                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13939             {
13940               if (TREE_CODE (arg0) == VECTOR_CST)
13941                 {
13942                   tree elements = TREE_VECTOR_CST_ELTS (arg0);
13943                   while (idx-- > 0 && elements)
13944                     elements = TREE_CHAIN (elements);
13945                   if (elements)
13946                     return TREE_VALUE (elements);
13947                 }
13948               else if (idx < CONSTRUCTOR_NELTS (arg0))
13949                 return CONSTRUCTOR_ELT (arg0, idx)->value;
13950               return build_zero_cst (type);
13951             }
13952         }
13953
13954       /* A bit-field-ref that referenced the full argument can be stripped.  */
13955       if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13956           && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13957           && integer_zerop (op2))
13958         return fold_convert_loc (loc, type, arg0);
13959
13960       return NULL_TREE;
13961
13962     case FMA_EXPR:
13963       /* For integers we can decompose the FMA if possible.  */
13964       if (TREE_CODE (arg0) == INTEGER_CST
13965           && TREE_CODE (arg1) == INTEGER_CST)
13966         return fold_build2_loc (loc, PLUS_EXPR, type,
13967                                 const_binop (MULT_EXPR, arg0, arg1), arg2);
13968       if (integer_zerop (arg2))
13969         return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
13970
13971       return fold_fma (loc, type, arg0, arg1, arg2);
13972
13973     case VEC_PERM_EXPR:
13974       if (TREE_CODE (arg2) == VECTOR_CST)
13975         {
13976           unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
13977           unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
13978           tree t;
13979           bool need_mask_canon = false;
13980
13981           gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)));
13982           for (i = 0, t = TREE_VECTOR_CST_ELTS (arg2);
13983                i < nelts && t; i++, t = TREE_CHAIN (t))
13984             {
13985               if (TREE_CODE (TREE_VALUE (t)) != INTEGER_CST)
13986                 return NULL_TREE;
13987
13988               sel[i] = TREE_INT_CST_LOW (TREE_VALUE (t)) & (2 * nelts - 1);
13989               if (TREE_INT_CST_HIGH (TREE_VALUE (t))
13990                   || ((unsigned HOST_WIDE_INT)
13991                       TREE_INT_CST_LOW (TREE_VALUE (t)) != sel[i]))
13992                 need_mask_canon = true;
13993             }
13994           if (t)
13995             return NULL_TREE;
13996           for (; i < nelts; i++)
13997             sel[i] = 0;
13998
13999           if ((TREE_CODE (arg0) == VECTOR_CST
14000                || TREE_CODE (arg0) == CONSTRUCTOR)
14001               && (TREE_CODE (arg1) == VECTOR_CST
14002                   || TREE_CODE (arg1) == CONSTRUCTOR))
14003             {
14004               t = fold_vec_perm (type, arg0, arg1, sel);
14005               if (t != NULL_TREE)
14006                 return t;
14007             }
14008
14009           if (need_mask_canon && arg2 == op2)
14010             {
14011               tree list = NULL_TREE, eltype = TREE_TYPE (TREE_TYPE (arg2));
14012               for (i = 0; i < nelts; i++)
14013                 list = tree_cons (NULL_TREE,
14014                                   build_int_cst (eltype, sel[nelts - i - 1]),
14015                                   list);
14016               t = build_vector (TREE_TYPE (arg2), list);
14017               return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, t);
14018             }
14019         }
14020       return NULL_TREE;
14021
14022     default:
14023       return NULL_TREE;
14024     } /* switch (code) */
14025 }
14026
14027 /* Perform constant folding and related simplification of EXPR.
14028    The related simplifications include x*1 => x, x*0 => 0, etc.,
14029    and application of the associative law.
14030    NOP_EXPR conversions may be removed freely (as long as we
14031    are careful not to change the type of the overall expression).
14032    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
14033    but we can constant-fold them if they have constant operands.  */
14034
14035 #ifdef ENABLE_FOLD_CHECKING
14036 # define fold(x) fold_1 (x)
14037 static tree fold_1 (tree);
14038 static
14039 #endif
14040 tree
14041 fold (tree expr)
14042 {
14043   const tree t = expr;
14044   enum tree_code code = TREE_CODE (t);
14045   enum tree_code_class kind = TREE_CODE_CLASS (code);
14046   tree tem;
14047   location_t loc = EXPR_LOCATION (expr);
14048
14049   /* Return right away if a constant.  */
14050   if (kind == tcc_constant)
14051     return t;
14052
14053   /* CALL_EXPR-like objects with variable numbers of operands are
14054      treated specially.  */
14055   if (kind == tcc_vl_exp)
14056     {
14057       if (code == CALL_EXPR)
14058         {
14059           tem = fold_call_expr (loc, expr, false);
14060           return tem ? tem : expr;
14061         }
14062       return expr;
14063     }
14064
14065   if (IS_EXPR_CODE_CLASS (kind))
14066     {
14067       tree type = TREE_TYPE (t);
14068       tree op0, op1, op2;
14069
14070       switch (TREE_CODE_LENGTH (code))
14071         {
14072         case 1:
14073           op0 = TREE_OPERAND (t, 0);
14074           tem = fold_unary_loc (loc, code, type, op0);
14075           return tem ? tem : expr;
14076         case 2:
14077           op0 = TREE_OPERAND (t, 0);
14078           op1 = TREE_OPERAND (t, 1);
14079           tem = fold_binary_loc (loc, code, type, op0, op1);
14080           return tem ? tem : expr;
14081         case 3:
14082           op0 = TREE_OPERAND (t, 0);
14083           op1 = TREE_OPERAND (t, 1);
14084           op2 = TREE_OPERAND (t, 2);
14085           tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14086           return tem ? tem : expr;
14087         default:
14088           break;
14089         }
14090     }
14091
14092   switch (code)
14093     {
14094     case ARRAY_REF:
14095       {
14096         tree op0 = TREE_OPERAND (t, 0);
14097         tree op1 = TREE_OPERAND (t, 1);
14098
14099         if (TREE_CODE (op1) == INTEGER_CST
14100             && TREE_CODE (op0) == CONSTRUCTOR
14101             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
14102           {
14103             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
14104             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
14105             unsigned HOST_WIDE_INT begin = 0;
14106
14107             /* Find a matching index by means of a binary search.  */
14108             while (begin != end)
14109               {
14110                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
14111                 tree index = VEC_index (constructor_elt, elts, middle)->index;
14112
14113                 if (TREE_CODE (index) == INTEGER_CST
14114                     && tree_int_cst_lt (index, op1))
14115                   begin = middle + 1;
14116                 else if (TREE_CODE (index) == INTEGER_CST
14117                          && tree_int_cst_lt (op1, index))
14118                   end = middle;
14119                 else if (TREE_CODE (index) == RANGE_EXPR
14120                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
14121                   begin = middle + 1;
14122                 else if (TREE_CODE (index) == RANGE_EXPR
14123                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
14124                   end = middle;
14125                 else
14126                   return VEC_index (constructor_elt, elts, middle)->value;
14127               }
14128           }
14129
14130         return t;
14131       }
14132
14133     case CONST_DECL:
14134       return fold (DECL_INITIAL (t));
14135
14136     default:
14137       return t;
14138     } /* switch (code) */
14139 }
14140
14141 #ifdef ENABLE_FOLD_CHECKING
14142 #undef fold
14143
14144 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
14145 static void fold_check_failed (const_tree, const_tree);
14146 void print_fold_checksum (const_tree);
14147
14148 /* When --enable-checking=fold, compute a digest of expr before
14149    and after actual fold call to see if fold did not accidentally
14150    change original expr.  */
14151
14152 tree
14153 fold (tree expr)
14154 {
14155   tree ret;
14156   struct md5_ctx ctx;
14157   unsigned char checksum_before[16], checksum_after[16];
14158   htab_t ht;
14159
14160   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14161   md5_init_ctx (&ctx);
14162   fold_checksum_tree (expr, &ctx, ht);
14163   md5_finish_ctx (&ctx, checksum_before);
14164   htab_empty (ht);
14165
14166   ret = fold_1 (expr);
14167
14168   md5_init_ctx (&ctx);
14169   fold_checksum_tree (expr, &ctx, ht);
14170   md5_finish_ctx (&ctx, checksum_after);
14171   htab_delete (ht);
14172
14173   if (memcmp (checksum_before, checksum_after, 16))
14174     fold_check_failed (expr, ret);
14175
14176   return ret;
14177 }
14178
14179 void
14180 print_fold_checksum (const_tree expr)
14181 {
14182   struct md5_ctx ctx;
14183   unsigned char checksum[16], cnt;
14184   htab_t ht;
14185
14186   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14187   md5_init_ctx (&ctx);
14188   fold_checksum_tree (expr, &ctx, ht);
14189   md5_finish_ctx (&ctx, checksum);
14190   htab_delete (ht);
14191   for (cnt = 0; cnt < 16; ++cnt)
14192     fprintf (stderr, "%02x", checksum[cnt]);
14193   putc ('\n', stderr);
14194 }
14195
14196 static void
14197 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
14198 {
14199   internal_error ("fold check: original tree changed by fold");
14200 }
14201
14202 static void
14203 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
14204 {
14205   void **slot;
14206   enum tree_code code;
14207   union tree_node buf;
14208   int i, len;
14209
14210  recursive_label:
14211   if (expr == NULL)
14212     return;
14213   slot = (void **) htab_find_slot (ht, expr, INSERT);
14214   if (*slot != NULL)
14215     return;
14216   *slot = CONST_CAST_TREE (expr);
14217   code = TREE_CODE (expr);
14218   if (TREE_CODE_CLASS (code) == tcc_declaration
14219       && DECL_ASSEMBLER_NAME_SET_P (expr))
14220     {
14221       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
14222       memcpy ((char *) &buf, expr, tree_size (expr));
14223       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
14224       expr = (tree) &buf;
14225     }
14226   else if (TREE_CODE_CLASS (code) == tcc_type
14227            && (TYPE_POINTER_TO (expr)
14228                || TYPE_REFERENCE_TO (expr)
14229                || TYPE_CACHED_VALUES_P (expr)
14230                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
14231                || TYPE_NEXT_VARIANT (expr)))
14232     {
14233       /* Allow these fields to be modified.  */
14234       tree tmp;
14235       memcpy ((char *) &buf, expr, tree_size (expr));
14236       expr = tmp = (tree) &buf;
14237       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
14238       TYPE_POINTER_TO (tmp) = NULL;
14239       TYPE_REFERENCE_TO (tmp) = NULL;
14240       TYPE_NEXT_VARIANT (tmp) = NULL;
14241       if (TYPE_CACHED_VALUES_P (tmp))
14242         {
14243           TYPE_CACHED_VALUES_P (tmp) = 0;
14244           TYPE_CACHED_VALUES (tmp) = NULL;
14245         }
14246     }
14247   md5_process_bytes (expr, tree_size (expr), ctx);
14248   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
14249     fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
14250   if (TREE_CODE_CLASS (code) != tcc_type
14251       && TREE_CODE_CLASS (code) != tcc_declaration
14252       && code != TREE_LIST
14253       && code != SSA_NAME
14254       && CODE_CONTAINS_STRUCT (code, TS_COMMON))
14255     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
14256   switch (TREE_CODE_CLASS (code))
14257     {
14258     case tcc_constant:
14259       switch (code)
14260         {
14261         case STRING_CST:
14262           md5_process_bytes (TREE_STRING_POINTER (expr),
14263                              TREE_STRING_LENGTH (expr), ctx);
14264           break;
14265         case COMPLEX_CST:
14266           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
14267           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
14268           break;
14269         case VECTOR_CST:
14270           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
14271           break;
14272         default:
14273           break;
14274         }
14275       break;
14276     case tcc_exceptional:
14277       switch (code)
14278         {
14279         case TREE_LIST:
14280           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
14281           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
14282           expr = TREE_CHAIN (expr);
14283           goto recursive_label;
14284           break;
14285         case TREE_VEC:
14286           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
14287             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
14288           break;
14289         default:
14290           break;
14291         }
14292       break;
14293     case tcc_expression:
14294     case tcc_reference:
14295     case tcc_comparison:
14296     case tcc_unary:
14297     case tcc_binary:
14298     case tcc_statement:
14299     case tcc_vl_exp:
14300       len = TREE_OPERAND_LENGTH (expr);
14301       for (i = 0; i < len; ++i)
14302         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
14303       break;
14304     case tcc_declaration:
14305       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
14306       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
14307       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
14308         {
14309           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
14310           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
14311           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
14312           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
14313           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
14314         }
14315       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
14316         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
14317
14318       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
14319         {
14320           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
14321           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
14322           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
14323         }
14324       break;
14325     case tcc_type:
14326       if (TREE_CODE (expr) == ENUMERAL_TYPE)
14327         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
14328       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
14329       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
14330       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
14331       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
14332       if (INTEGRAL_TYPE_P (expr)
14333           || SCALAR_FLOAT_TYPE_P (expr))
14334         {
14335           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
14336           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
14337         }
14338       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
14339       if (TREE_CODE (expr) == RECORD_TYPE
14340           || TREE_CODE (expr) == UNION_TYPE
14341           || TREE_CODE (expr) == QUAL_UNION_TYPE)
14342         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
14343       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
14344       break;
14345     default:
14346       break;
14347     }
14348 }
14349
14350 /* Helper function for outputting the checksum of a tree T.  When
14351    debugging with gdb, you can "define mynext" to be "next" followed
14352    by "call debug_fold_checksum (op0)", then just trace down till the
14353    outputs differ.  */
14354
14355 DEBUG_FUNCTION void
14356 debug_fold_checksum (const_tree t)
14357 {
14358   int i;
14359   unsigned char checksum[16];
14360   struct md5_ctx ctx;
14361   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14362
14363   md5_init_ctx (&ctx);
14364   fold_checksum_tree (t, &ctx, ht);
14365   md5_finish_ctx (&ctx, checksum);
14366   htab_empty (ht);
14367
14368   for (i = 0; i < 16; i++)
14369     fprintf (stderr, "%d ", checksum[i]);
14370
14371   fprintf (stderr, "\n");
14372 }
14373
14374 #endif
14375
14376 /* Fold a unary tree expression with code CODE of type TYPE with an
14377    operand OP0.  LOC is the location of the resulting expression.
14378    Return a folded expression if successful.  Otherwise, return a tree
14379    expression with code CODE of type TYPE with an operand OP0.  */
14380
14381 tree
14382 fold_build1_stat_loc (location_t loc,
14383                       enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
14384 {
14385   tree tem;
14386 #ifdef ENABLE_FOLD_CHECKING
14387   unsigned char checksum_before[16], checksum_after[16];
14388   struct md5_ctx ctx;
14389   htab_t ht;
14390
14391   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14392   md5_init_ctx (&ctx);
14393   fold_checksum_tree (op0, &ctx, ht);
14394   md5_finish_ctx (&ctx, checksum_before);
14395   htab_empty (ht);
14396 #endif
14397
14398   tem = fold_unary_loc (loc, code, type, op0);
14399   if (!tem)
14400     tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
14401
14402 #ifdef ENABLE_FOLD_CHECKING
14403   md5_init_ctx (&ctx);
14404   fold_checksum_tree (op0, &ctx, ht);
14405   md5_finish_ctx (&ctx, checksum_after);
14406   htab_delete (ht);
14407
14408   if (memcmp (checksum_before, checksum_after, 16))
14409     fold_check_failed (op0, tem);
14410 #endif
14411   return tem;
14412 }
14413
14414 /* Fold a binary tree expression with code CODE of type TYPE with
14415    operands OP0 and OP1.  LOC is the location of the resulting
14416    expression.  Return a folded expression if successful.  Otherwise,
14417    return a tree expression with code CODE of type TYPE with operands
14418    OP0 and OP1.  */
14419
14420 tree
14421 fold_build2_stat_loc (location_t loc,
14422                       enum tree_code code, tree type, tree op0, tree op1
14423                       MEM_STAT_DECL)
14424 {
14425   tree tem;
14426 #ifdef ENABLE_FOLD_CHECKING
14427   unsigned char checksum_before_op0[16],
14428                 checksum_before_op1[16],
14429                 checksum_after_op0[16],
14430                 checksum_after_op1[16];
14431   struct md5_ctx ctx;
14432   htab_t ht;
14433
14434   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14435   md5_init_ctx (&ctx);
14436   fold_checksum_tree (op0, &ctx, ht);
14437   md5_finish_ctx (&ctx, checksum_before_op0);
14438   htab_empty (ht);
14439
14440   md5_init_ctx (&ctx);
14441   fold_checksum_tree (op1, &ctx, ht);
14442   md5_finish_ctx (&ctx, checksum_before_op1);
14443   htab_empty (ht);
14444 #endif
14445
14446   tem = fold_binary_loc (loc, code, type, op0, op1);
14447   if (!tem)
14448     tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
14449
14450 #ifdef ENABLE_FOLD_CHECKING
14451   md5_init_ctx (&ctx);
14452   fold_checksum_tree (op0, &ctx, ht);
14453   md5_finish_ctx (&ctx, checksum_after_op0);
14454   htab_empty (ht);
14455
14456   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14457     fold_check_failed (op0, tem);
14458
14459   md5_init_ctx (&ctx);
14460   fold_checksum_tree (op1, &ctx, ht);
14461   md5_finish_ctx (&ctx, checksum_after_op1);
14462   htab_delete (ht);
14463
14464   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14465     fold_check_failed (op1, tem);
14466 #endif
14467   return tem;
14468 }
14469
14470 /* Fold a ternary tree expression with code CODE of type TYPE with
14471    operands OP0, OP1, and OP2.  Return a folded expression if
14472    successful.  Otherwise, return a tree expression with code CODE of
14473    type TYPE with operands OP0, OP1, and OP2.  */
14474
14475 tree
14476 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14477                       tree op0, tree op1, tree op2 MEM_STAT_DECL)
14478 {
14479   tree tem;
14480 #ifdef ENABLE_FOLD_CHECKING
14481   unsigned char checksum_before_op0[16],
14482                 checksum_before_op1[16],
14483                 checksum_before_op2[16],
14484                 checksum_after_op0[16],
14485                 checksum_after_op1[16],
14486                 checksum_after_op2[16];
14487   struct md5_ctx ctx;
14488   htab_t ht;
14489
14490   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14491   md5_init_ctx (&ctx);
14492   fold_checksum_tree (op0, &ctx, ht);
14493   md5_finish_ctx (&ctx, checksum_before_op0);
14494   htab_empty (ht);
14495
14496   md5_init_ctx (&ctx);
14497   fold_checksum_tree (op1, &ctx, ht);
14498   md5_finish_ctx (&ctx, checksum_before_op1);
14499   htab_empty (ht);
14500
14501   md5_init_ctx (&ctx);
14502   fold_checksum_tree (op2, &ctx, ht);
14503   md5_finish_ctx (&ctx, checksum_before_op2);
14504   htab_empty (ht);
14505 #endif
14506
14507   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14508   tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14509   if (!tem)
14510     tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
14511
14512 #ifdef ENABLE_FOLD_CHECKING
14513   md5_init_ctx (&ctx);
14514   fold_checksum_tree (op0, &ctx, ht);
14515   md5_finish_ctx (&ctx, checksum_after_op0);
14516   htab_empty (ht);
14517
14518   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14519     fold_check_failed (op0, tem);
14520
14521   md5_init_ctx (&ctx);
14522   fold_checksum_tree (op1, &ctx, ht);
14523   md5_finish_ctx (&ctx, checksum_after_op1);
14524   htab_empty (ht);
14525
14526   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14527     fold_check_failed (op1, tem);
14528
14529   md5_init_ctx (&ctx);
14530   fold_checksum_tree (op2, &ctx, ht);
14531   md5_finish_ctx (&ctx, checksum_after_op2);
14532   htab_delete (ht);
14533
14534   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14535     fold_check_failed (op2, tem);
14536 #endif
14537   return tem;
14538 }
14539
14540 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14541    arguments in ARGARRAY, and a null static chain.
14542    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
14543    of type TYPE from the given operands as constructed by build_call_array.  */
14544
14545 tree
14546 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14547                            int nargs, tree *argarray)
14548 {
14549   tree tem;
14550 #ifdef ENABLE_FOLD_CHECKING
14551   unsigned char checksum_before_fn[16],
14552                 checksum_before_arglist[16],
14553                 checksum_after_fn[16],
14554                 checksum_after_arglist[16];
14555   struct md5_ctx ctx;
14556   htab_t ht;
14557   int i;
14558
14559   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14560   md5_init_ctx (&ctx);
14561   fold_checksum_tree (fn, &ctx, ht);
14562   md5_finish_ctx (&ctx, checksum_before_fn);
14563   htab_empty (ht);
14564
14565   md5_init_ctx (&ctx);
14566   for (i = 0; i < nargs; i++)
14567     fold_checksum_tree (argarray[i], &ctx, ht);
14568   md5_finish_ctx (&ctx, checksum_before_arglist);
14569   htab_empty (ht);
14570 #endif
14571
14572   tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14573
14574 #ifdef ENABLE_FOLD_CHECKING
14575   md5_init_ctx (&ctx);
14576   fold_checksum_tree (fn, &ctx, ht);
14577   md5_finish_ctx (&ctx, checksum_after_fn);
14578   htab_empty (ht);
14579
14580   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14581     fold_check_failed (fn, tem);
14582
14583   md5_init_ctx (&ctx);
14584   for (i = 0; i < nargs; i++)
14585     fold_checksum_tree (argarray[i], &ctx, ht);
14586   md5_finish_ctx (&ctx, checksum_after_arglist);
14587   htab_delete (ht);
14588
14589   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14590     fold_check_failed (NULL_TREE, tem);
14591 #endif
14592   return tem;
14593 }
14594
14595 /* Perform constant folding and related simplification of initializer
14596    expression EXPR.  These behave identically to "fold_buildN" but ignore
14597    potential run-time traps and exceptions that fold must preserve.  */
14598
14599 #define START_FOLD_INIT \
14600   int saved_signaling_nans = flag_signaling_nans;\
14601   int saved_trapping_math = flag_trapping_math;\
14602   int saved_rounding_math = flag_rounding_math;\
14603   int saved_trapv = flag_trapv;\
14604   int saved_folding_initializer = folding_initializer;\
14605   flag_signaling_nans = 0;\
14606   flag_trapping_math = 0;\
14607   flag_rounding_math = 0;\
14608   flag_trapv = 0;\
14609   folding_initializer = 1;
14610
14611 #define END_FOLD_INIT \
14612   flag_signaling_nans = saved_signaling_nans;\
14613   flag_trapping_math = saved_trapping_math;\
14614   flag_rounding_math = saved_rounding_math;\
14615   flag_trapv = saved_trapv;\
14616   folding_initializer = saved_folding_initializer;
14617
14618 tree
14619 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14620                              tree type, tree op)
14621 {
14622   tree result;
14623   START_FOLD_INIT;
14624
14625   result = fold_build1_loc (loc, code, type, op);
14626
14627   END_FOLD_INIT;
14628   return result;
14629 }
14630
14631 tree
14632 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14633                              tree type, tree op0, tree op1)
14634 {
14635   tree result;
14636   START_FOLD_INIT;
14637
14638   result = fold_build2_loc (loc, code, type, op0, op1);
14639
14640   END_FOLD_INIT;
14641   return result;
14642 }
14643
14644 tree
14645 fold_build3_initializer_loc (location_t loc, enum tree_code code,
14646                              tree type, tree op0, tree op1, tree op2)
14647 {
14648   tree result;
14649   START_FOLD_INIT;
14650
14651   result = fold_build3_loc (loc, code, type, op0, op1, op2);
14652
14653   END_FOLD_INIT;
14654   return result;
14655 }
14656
14657 tree
14658 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14659                                        int nargs, tree *argarray)
14660 {
14661   tree result;
14662   START_FOLD_INIT;
14663
14664   result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14665
14666   END_FOLD_INIT;
14667   return result;
14668 }
14669
14670 #undef START_FOLD_INIT
14671 #undef END_FOLD_INIT
14672
14673 /* Determine if first argument is a multiple of second argument.  Return 0 if
14674    it is not, or we cannot easily determined it to be.
14675
14676    An example of the sort of thing we care about (at this point; this routine
14677    could surely be made more general, and expanded to do what the *_DIV_EXPR's
14678    fold cases do now) is discovering that
14679
14680      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14681
14682    is a multiple of
14683
14684      SAVE_EXPR (J * 8)
14685
14686    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14687
14688    This code also handles discovering that
14689
14690      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14691
14692    is a multiple of 8 so we don't have to worry about dealing with a
14693    possible remainder.
14694
14695    Note that we *look* inside a SAVE_EXPR only to determine how it was
14696    calculated; it is not safe for fold to do much of anything else with the
14697    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14698    at run time.  For example, the latter example above *cannot* be implemented
14699    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14700    evaluation time of the original SAVE_EXPR is not necessarily the same at
14701    the time the new expression is evaluated.  The only optimization of this
14702    sort that would be valid is changing
14703
14704      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14705
14706    divided by 8 to
14707
14708      SAVE_EXPR (I) * SAVE_EXPR (J)
14709
14710    (where the same SAVE_EXPR (J) is used in the original and the
14711    transformed version).  */
14712
14713 int
14714 multiple_of_p (tree type, const_tree top, const_tree bottom)
14715 {
14716   if (operand_equal_p (top, bottom, 0))
14717     return 1;
14718
14719   if (TREE_CODE (type) != INTEGER_TYPE)
14720     return 0;
14721
14722   switch (TREE_CODE (top))
14723     {
14724     case BIT_AND_EXPR:
14725       /* Bitwise and provides a power of two multiple.  If the mask is
14726          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
14727       if (!integer_pow2p (bottom))
14728         return 0;
14729       /* FALLTHRU */
14730
14731     case MULT_EXPR:
14732       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14733               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14734
14735     case PLUS_EXPR:
14736     case MINUS_EXPR:
14737       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14738               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14739
14740     case LSHIFT_EXPR:
14741       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14742         {
14743           tree op1, t1;
14744
14745           op1 = TREE_OPERAND (top, 1);
14746           /* const_binop may not detect overflow correctly,
14747              so check for it explicitly here.  */
14748           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14749               > TREE_INT_CST_LOW (op1)
14750               && TREE_INT_CST_HIGH (op1) == 0
14751               && 0 != (t1 = fold_convert (type,
14752                                           const_binop (LSHIFT_EXPR,
14753                                                        size_one_node,
14754                                                        op1)))
14755               && !TREE_OVERFLOW (t1))
14756             return multiple_of_p (type, t1, bottom);
14757         }
14758       return 0;
14759
14760     case NOP_EXPR:
14761       /* Can't handle conversions from non-integral or wider integral type.  */
14762       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14763           || (TYPE_PRECISION (type)
14764               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14765         return 0;
14766
14767       /* .. fall through ...  */
14768
14769     case SAVE_EXPR:
14770       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14771
14772     case COND_EXPR:
14773       return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
14774               && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
14775
14776     case INTEGER_CST:
14777       if (TREE_CODE (bottom) != INTEGER_CST
14778           || integer_zerop (bottom)
14779           || (TYPE_UNSIGNED (type)
14780               && (tree_int_cst_sgn (top) < 0
14781                   || tree_int_cst_sgn (bottom) < 0)))
14782         return 0;
14783       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14784                                              top, bottom));
14785
14786     default:
14787       return 0;
14788     }
14789 }
14790
14791 /* Return true if CODE or TYPE is known to be non-negative. */
14792
14793 static bool
14794 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14795 {
14796   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14797       && truth_value_p (code))
14798     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14799        have a signed:1 type (where the value is -1 and 0).  */
14800     return true;
14801   return false;
14802 }
14803
14804 /* Return true if (CODE OP0) is known to be non-negative.  If the return
14805    value is based on the assumption that signed overflow is undefined,
14806    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14807    *STRICT_OVERFLOW_P.  */
14808
14809 bool
14810 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14811                                 bool *strict_overflow_p)
14812 {
14813   if (TYPE_UNSIGNED (type))
14814     return true;
14815
14816   switch (code)
14817     {
14818     case ABS_EXPR:
14819       /* We can't return 1 if flag_wrapv is set because
14820          ABS_EXPR<INT_MIN> = INT_MIN.  */
14821       if (!INTEGRAL_TYPE_P (type))
14822         return true;
14823       if (TYPE_OVERFLOW_UNDEFINED (type))
14824         {
14825           *strict_overflow_p = true;
14826           return true;
14827         }
14828       break;
14829
14830     case NON_LVALUE_EXPR:
14831     case FLOAT_EXPR:
14832     case FIX_TRUNC_EXPR:
14833       return tree_expr_nonnegative_warnv_p (op0,
14834                                             strict_overflow_p);
14835
14836     case NOP_EXPR:
14837       {
14838         tree inner_type = TREE_TYPE (op0);
14839         tree outer_type = type;
14840
14841         if (TREE_CODE (outer_type) == REAL_TYPE)
14842           {
14843             if (TREE_CODE (inner_type) == REAL_TYPE)
14844               return tree_expr_nonnegative_warnv_p (op0,
14845                                                     strict_overflow_p);
14846             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14847               {
14848                 if (TYPE_UNSIGNED (inner_type))
14849                   return true;
14850                 return tree_expr_nonnegative_warnv_p (op0,
14851                                                       strict_overflow_p);
14852               }
14853           }
14854         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14855           {
14856             if (TREE_CODE (inner_type) == REAL_TYPE)
14857               return tree_expr_nonnegative_warnv_p (op0,
14858                                                     strict_overflow_p);
14859             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14860               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14861                       && TYPE_UNSIGNED (inner_type);
14862           }
14863       }
14864       break;
14865
14866     default:
14867       return tree_simple_nonnegative_warnv_p (code, type);
14868     }
14869
14870   /* We don't know sign of `t', so be conservative and return false.  */
14871   return false;
14872 }
14873
14874 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
14875    value is based on the assumption that signed overflow is undefined,
14876    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14877    *STRICT_OVERFLOW_P.  */
14878
14879 bool
14880 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14881                                       tree op1, bool *strict_overflow_p)
14882 {
14883   if (TYPE_UNSIGNED (type))
14884     return true;
14885
14886   switch (code)
14887     {
14888     case POINTER_PLUS_EXPR:
14889     case PLUS_EXPR:
14890       if (FLOAT_TYPE_P (type))
14891         return (tree_expr_nonnegative_warnv_p (op0,
14892                                                strict_overflow_p)
14893                 && tree_expr_nonnegative_warnv_p (op1,
14894                                                   strict_overflow_p));
14895
14896       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14897          both unsigned and at least 2 bits shorter than the result.  */
14898       if (TREE_CODE (type) == INTEGER_TYPE
14899           && TREE_CODE (op0) == NOP_EXPR
14900           && TREE_CODE (op1) == NOP_EXPR)
14901         {
14902           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14903           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14904           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14905               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14906             {
14907               unsigned int prec = MAX (TYPE_PRECISION (inner1),
14908                                        TYPE_PRECISION (inner2)) + 1;
14909               return prec < TYPE_PRECISION (type);
14910             }
14911         }
14912       break;
14913
14914     case MULT_EXPR:
14915       if (FLOAT_TYPE_P (type))
14916         {
14917           /* x * x for floating point x is always non-negative.  */
14918           if (operand_equal_p (op0, op1, 0))
14919             return true;
14920           return (tree_expr_nonnegative_warnv_p (op0,
14921                                                  strict_overflow_p)
14922                   && tree_expr_nonnegative_warnv_p (op1,
14923                                                     strict_overflow_p));
14924         }
14925
14926       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14927          both unsigned and their total bits is shorter than the result.  */
14928       if (TREE_CODE (type) == INTEGER_TYPE
14929           && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14930           && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14931         {
14932           tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14933             ? TREE_TYPE (TREE_OPERAND (op0, 0))
14934             : TREE_TYPE (op0);
14935           tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14936             ? TREE_TYPE (TREE_OPERAND (op1, 0))
14937             : TREE_TYPE (op1);
14938
14939           bool unsigned0 = TYPE_UNSIGNED (inner0);
14940           bool unsigned1 = TYPE_UNSIGNED (inner1);
14941
14942           if (TREE_CODE (op0) == INTEGER_CST)
14943             unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14944
14945           if (TREE_CODE (op1) == INTEGER_CST)
14946             unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14947
14948           if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14949               && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14950             {
14951               unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14952                 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14953                 : TYPE_PRECISION (inner0);
14954
14955               unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14956                 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14957                 : TYPE_PRECISION (inner1);
14958
14959               return precision0 + precision1 < TYPE_PRECISION (type);
14960             }
14961         }
14962       return false;
14963
14964     case BIT_AND_EXPR:
14965     case MAX_EXPR:
14966       return (tree_expr_nonnegative_warnv_p (op0,
14967                                              strict_overflow_p)
14968               || tree_expr_nonnegative_warnv_p (op1,
14969                                                 strict_overflow_p));
14970
14971     case BIT_IOR_EXPR:
14972     case BIT_XOR_EXPR:
14973     case MIN_EXPR:
14974     case RDIV_EXPR:
14975     case TRUNC_DIV_EXPR:
14976     case CEIL_DIV_EXPR:
14977     case FLOOR_DIV_EXPR:
14978     case ROUND_DIV_EXPR:
14979       return (tree_expr_nonnegative_warnv_p (op0,
14980                                              strict_overflow_p)
14981               && tree_expr_nonnegative_warnv_p (op1,
14982                                                 strict_overflow_p));
14983
14984     case TRUNC_MOD_EXPR:
14985     case CEIL_MOD_EXPR:
14986     case FLOOR_MOD_EXPR:
14987     case ROUND_MOD_EXPR:
14988       return tree_expr_nonnegative_warnv_p (op0,
14989                                             strict_overflow_p);
14990     default:
14991       return tree_simple_nonnegative_warnv_p (code, type);
14992     }
14993
14994   /* We don't know sign of `t', so be conservative and return false.  */
14995   return false;
14996 }
14997
14998 /* Return true if T is known to be non-negative.  If the return
14999    value is based on the assumption that signed overflow is undefined,
15000    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15001    *STRICT_OVERFLOW_P.  */
15002
15003 bool
15004 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15005 {
15006   if (TYPE_UNSIGNED (TREE_TYPE (t)))
15007     return true;
15008
15009   switch (TREE_CODE (t))
15010     {
15011     case INTEGER_CST:
15012       return tree_int_cst_sgn (t) >= 0;
15013
15014     case REAL_CST:
15015       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
15016
15017     case FIXED_CST:
15018       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
15019
15020     case COND_EXPR:
15021       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15022                                              strict_overflow_p)
15023               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
15024                                                 strict_overflow_p));
15025     default:
15026       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15027                                                    TREE_TYPE (t));
15028     }
15029   /* We don't know sign of `t', so be conservative and return false.  */
15030   return false;
15031 }
15032
15033 /* Return true if T is known to be non-negative.  If the return
15034    value is based on the assumption that signed overflow is undefined,
15035    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15036    *STRICT_OVERFLOW_P.  */
15037
15038 bool
15039 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
15040                                tree arg0, tree arg1, bool *strict_overflow_p)
15041 {
15042   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
15043     switch (DECL_FUNCTION_CODE (fndecl))
15044       {
15045         CASE_FLT_FN (BUILT_IN_ACOS):
15046         CASE_FLT_FN (BUILT_IN_ACOSH):
15047         CASE_FLT_FN (BUILT_IN_CABS):
15048         CASE_FLT_FN (BUILT_IN_COSH):
15049         CASE_FLT_FN (BUILT_IN_ERFC):
15050         CASE_FLT_FN (BUILT_IN_EXP):
15051         CASE_FLT_FN (BUILT_IN_EXP10):
15052         CASE_FLT_FN (BUILT_IN_EXP2):
15053         CASE_FLT_FN (BUILT_IN_FABS):
15054         CASE_FLT_FN (BUILT_IN_FDIM):
15055         CASE_FLT_FN (BUILT_IN_HYPOT):
15056         CASE_FLT_FN (BUILT_IN_POW10):
15057         CASE_INT_FN (BUILT_IN_FFS):
15058         CASE_INT_FN (BUILT_IN_PARITY):
15059         CASE_INT_FN (BUILT_IN_POPCOUNT):
15060       case BUILT_IN_BSWAP32:
15061       case BUILT_IN_BSWAP64:
15062         /* Always true.  */
15063         return true;
15064
15065         CASE_FLT_FN (BUILT_IN_SQRT):
15066         /* sqrt(-0.0) is -0.0.  */
15067         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
15068           return true;
15069         return tree_expr_nonnegative_warnv_p (arg0,
15070                                               strict_overflow_p);
15071
15072         CASE_FLT_FN (BUILT_IN_ASINH):
15073         CASE_FLT_FN (BUILT_IN_ATAN):
15074         CASE_FLT_FN (BUILT_IN_ATANH):
15075         CASE_FLT_FN (BUILT_IN_CBRT):
15076         CASE_FLT_FN (BUILT_IN_CEIL):
15077         CASE_FLT_FN (BUILT_IN_ERF):
15078         CASE_FLT_FN (BUILT_IN_EXPM1):
15079         CASE_FLT_FN (BUILT_IN_FLOOR):
15080         CASE_FLT_FN (BUILT_IN_FMOD):
15081         CASE_FLT_FN (BUILT_IN_FREXP):
15082         CASE_FLT_FN (BUILT_IN_ICEIL):
15083         CASE_FLT_FN (BUILT_IN_IFLOOR):
15084         CASE_FLT_FN (BUILT_IN_IRINT):
15085         CASE_FLT_FN (BUILT_IN_IROUND):
15086         CASE_FLT_FN (BUILT_IN_LCEIL):
15087         CASE_FLT_FN (BUILT_IN_LDEXP):
15088         CASE_FLT_FN (BUILT_IN_LFLOOR):
15089         CASE_FLT_FN (BUILT_IN_LLCEIL):
15090         CASE_FLT_FN (BUILT_IN_LLFLOOR):
15091         CASE_FLT_FN (BUILT_IN_LLRINT):
15092         CASE_FLT_FN (BUILT_IN_LLROUND):
15093         CASE_FLT_FN (BUILT_IN_LRINT):
15094         CASE_FLT_FN (BUILT_IN_LROUND):
15095         CASE_FLT_FN (BUILT_IN_MODF):
15096         CASE_FLT_FN (BUILT_IN_NEARBYINT):
15097         CASE_FLT_FN (BUILT_IN_RINT):
15098         CASE_FLT_FN (BUILT_IN_ROUND):
15099         CASE_FLT_FN (BUILT_IN_SCALB):
15100         CASE_FLT_FN (BUILT_IN_SCALBLN):
15101         CASE_FLT_FN (BUILT_IN_SCALBN):
15102         CASE_FLT_FN (BUILT_IN_SIGNBIT):
15103         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
15104         CASE_FLT_FN (BUILT_IN_SINH):
15105         CASE_FLT_FN (BUILT_IN_TANH):
15106         CASE_FLT_FN (BUILT_IN_TRUNC):
15107         /* True if the 1st argument is nonnegative.  */
15108         return tree_expr_nonnegative_warnv_p (arg0,
15109                                               strict_overflow_p);
15110
15111         CASE_FLT_FN (BUILT_IN_FMAX):
15112         /* True if the 1st OR 2nd arguments are nonnegative.  */
15113         return (tree_expr_nonnegative_warnv_p (arg0,
15114                                                strict_overflow_p)
15115                 || (tree_expr_nonnegative_warnv_p (arg1,
15116                                                    strict_overflow_p)));
15117
15118         CASE_FLT_FN (BUILT_IN_FMIN):
15119         /* True if the 1st AND 2nd arguments are nonnegative.  */
15120         return (tree_expr_nonnegative_warnv_p (arg0,
15121                                                strict_overflow_p)
15122                 && (tree_expr_nonnegative_warnv_p (arg1,
15123                                                    strict_overflow_p)));
15124
15125         CASE_FLT_FN (BUILT_IN_COPYSIGN):
15126         /* True if the 2nd argument is nonnegative.  */
15127         return tree_expr_nonnegative_warnv_p (arg1,
15128                                               strict_overflow_p);
15129
15130         CASE_FLT_FN (BUILT_IN_POWI):
15131         /* True if the 1st argument is nonnegative or the second
15132            argument is an even integer.  */
15133         if (TREE_CODE (arg1) == INTEGER_CST
15134             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
15135           return true;
15136         return tree_expr_nonnegative_warnv_p (arg0,
15137                                               strict_overflow_p);
15138
15139         CASE_FLT_FN (BUILT_IN_POW):
15140         /* True if the 1st argument is nonnegative or the second
15141            argument is an even integer valued real.  */
15142         if (TREE_CODE (arg1) == REAL_CST)
15143           {
15144             REAL_VALUE_TYPE c;
15145             HOST_WIDE_INT n;
15146
15147             c = TREE_REAL_CST (arg1);
15148             n = real_to_integer (&c);
15149             if ((n & 1) == 0)
15150               {
15151                 REAL_VALUE_TYPE cint;
15152                 real_from_integer (&cint, VOIDmode, n,
15153                                    n < 0 ? -1 : 0, 0);
15154                 if (real_identical (&c, &cint))
15155                   return true;
15156               }
15157           }
15158         return tree_expr_nonnegative_warnv_p (arg0,
15159                                               strict_overflow_p);
15160
15161       default:
15162         break;
15163       }
15164   return tree_simple_nonnegative_warnv_p (CALL_EXPR,
15165                                           type);
15166 }
15167
15168 /* Return true if T is known to be non-negative.  If the return
15169    value is based on the assumption that signed overflow is undefined,
15170    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15171    *STRICT_OVERFLOW_P.  */
15172
15173 bool
15174 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15175 {
15176   enum tree_code code = TREE_CODE (t);
15177   if (TYPE_UNSIGNED (TREE_TYPE (t)))
15178     return true;
15179
15180   switch (code)
15181     {
15182     case TARGET_EXPR:
15183       {
15184         tree temp = TARGET_EXPR_SLOT (t);
15185         t = TARGET_EXPR_INITIAL (t);
15186
15187         /* If the initializer is non-void, then it's a normal expression
15188            that will be assigned to the slot.  */
15189         if (!VOID_TYPE_P (t))
15190           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
15191
15192         /* Otherwise, the initializer sets the slot in some way.  One common
15193            way is an assignment statement at the end of the initializer.  */
15194         while (1)
15195           {
15196             if (TREE_CODE (t) == BIND_EXPR)
15197               t = expr_last (BIND_EXPR_BODY (t));
15198             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15199                      || TREE_CODE (t) == TRY_CATCH_EXPR)
15200               t = expr_last (TREE_OPERAND (t, 0));
15201             else if (TREE_CODE (t) == STATEMENT_LIST)
15202               t = expr_last (t);
15203             else
15204               break;
15205           }
15206         if (TREE_CODE (t) == MODIFY_EXPR
15207             && TREE_OPERAND (t, 0) == temp)
15208           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15209                                                 strict_overflow_p);
15210
15211         return false;
15212       }
15213
15214     case CALL_EXPR:
15215       {
15216         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
15217         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
15218
15219         return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
15220                                               get_callee_fndecl (t),
15221                                               arg0,
15222                                               arg1,
15223                                               strict_overflow_p);
15224       }
15225     case COMPOUND_EXPR:
15226     case MODIFY_EXPR:
15227       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15228                                             strict_overflow_p);
15229     case BIND_EXPR:
15230       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
15231                                             strict_overflow_p);
15232     case SAVE_EXPR:
15233       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
15234                                             strict_overflow_p);
15235
15236     default:
15237       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15238                                                    TREE_TYPE (t));
15239     }
15240
15241   /* We don't know sign of `t', so be conservative and return false.  */
15242   return false;
15243 }
15244
15245 /* Return true if T is known to be non-negative.  If the return
15246    value is based on the assumption that signed overflow is undefined,
15247    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15248    *STRICT_OVERFLOW_P.  */
15249
15250 bool
15251 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15252 {
15253   enum tree_code code;
15254   if (t == error_mark_node)
15255     return false;
15256
15257   code = TREE_CODE (t);
15258   switch (TREE_CODE_CLASS (code))
15259     {
15260     case tcc_binary:
15261     case tcc_comparison:
15262       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15263                                               TREE_TYPE (t),
15264                                               TREE_OPERAND (t, 0),
15265                                               TREE_OPERAND (t, 1),
15266                                               strict_overflow_p);
15267
15268     case tcc_unary:
15269       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15270                                              TREE_TYPE (t),
15271                                              TREE_OPERAND (t, 0),
15272                                              strict_overflow_p);
15273
15274     case tcc_constant:
15275     case tcc_declaration:
15276     case tcc_reference:
15277       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15278
15279     default:
15280       break;
15281     }
15282
15283   switch (code)
15284     {
15285     case TRUTH_AND_EXPR:
15286     case TRUTH_OR_EXPR:
15287     case TRUTH_XOR_EXPR:
15288       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15289                                               TREE_TYPE (t),
15290                                               TREE_OPERAND (t, 0),
15291                                               TREE_OPERAND (t, 1),
15292                                               strict_overflow_p);
15293     case TRUTH_NOT_EXPR:
15294       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15295                                              TREE_TYPE (t),
15296                                              TREE_OPERAND (t, 0),
15297                                              strict_overflow_p);
15298
15299     case COND_EXPR:
15300     case CONSTRUCTOR:
15301     case OBJ_TYPE_REF:
15302     case ASSERT_EXPR:
15303     case ADDR_EXPR:
15304     case WITH_SIZE_EXPR:
15305     case SSA_NAME:
15306       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15307
15308     default:
15309       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
15310     }
15311 }
15312
15313 /* Return true if `t' is known to be non-negative.  Handle warnings
15314    about undefined signed overflow.  */
15315
15316 bool
15317 tree_expr_nonnegative_p (tree t)
15318 {
15319   bool ret, strict_overflow_p;
15320
15321   strict_overflow_p = false;
15322   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15323   if (strict_overflow_p)
15324     fold_overflow_warning (("assuming signed overflow does not occur when "
15325                             "determining that expression is always "
15326                             "non-negative"),
15327                            WARN_STRICT_OVERFLOW_MISC);
15328   return ret;
15329 }
15330
15331
15332 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15333    For floating point we further ensure that T is not denormal.
15334    Similar logic is present in nonzero_address in rtlanal.h.
15335
15336    If the return value is based on the assumption that signed overflow
15337    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15338    change *STRICT_OVERFLOW_P.  */
15339
15340 bool
15341 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15342                                  bool *strict_overflow_p)
15343 {
15344   switch (code)
15345     {
15346     case ABS_EXPR:
15347       return tree_expr_nonzero_warnv_p (op0,
15348                                         strict_overflow_p);
15349
15350     case NOP_EXPR:
15351       {
15352         tree inner_type = TREE_TYPE (op0);
15353         tree outer_type = type;
15354
15355         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15356                 && tree_expr_nonzero_warnv_p (op0,
15357                                               strict_overflow_p));
15358       }
15359       break;
15360
15361     case NON_LVALUE_EXPR:
15362       return tree_expr_nonzero_warnv_p (op0,
15363                                         strict_overflow_p);
15364
15365     default:
15366       break;
15367   }
15368
15369   return false;
15370 }
15371
15372 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15373    For floating point we further ensure that T is not denormal.
15374    Similar logic is present in nonzero_address in rtlanal.h.
15375
15376    If the return value is based on the assumption that signed overflow
15377    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15378    change *STRICT_OVERFLOW_P.  */
15379
15380 bool
15381 tree_binary_nonzero_warnv_p (enum tree_code code,
15382                              tree type,
15383                              tree op0,
15384                              tree op1, bool *strict_overflow_p)
15385 {
15386   bool sub_strict_overflow_p;
15387   switch (code)
15388     {
15389     case POINTER_PLUS_EXPR:
15390     case PLUS_EXPR:
15391       if (TYPE_OVERFLOW_UNDEFINED (type))
15392         {
15393           /* With the presence of negative values it is hard
15394              to say something.  */
15395           sub_strict_overflow_p = false;
15396           if (!tree_expr_nonnegative_warnv_p (op0,
15397                                               &sub_strict_overflow_p)
15398               || !tree_expr_nonnegative_warnv_p (op1,
15399                                                  &sub_strict_overflow_p))
15400             return false;
15401           /* One of operands must be positive and the other non-negative.  */
15402           /* We don't set *STRICT_OVERFLOW_P here: even if this value
15403              overflows, on a twos-complement machine the sum of two
15404              nonnegative numbers can never be zero.  */
15405           return (tree_expr_nonzero_warnv_p (op0,
15406                                              strict_overflow_p)
15407                   || tree_expr_nonzero_warnv_p (op1,
15408                                                 strict_overflow_p));
15409         }
15410       break;
15411
15412     case MULT_EXPR:
15413       if (TYPE_OVERFLOW_UNDEFINED (type))
15414         {
15415           if (tree_expr_nonzero_warnv_p (op0,
15416                                          strict_overflow_p)
15417               && tree_expr_nonzero_warnv_p (op1,
15418                                             strict_overflow_p))
15419             {
15420               *strict_overflow_p = true;
15421               return true;
15422             }
15423         }
15424       break;
15425
15426     case MIN_EXPR:
15427       sub_strict_overflow_p = false;
15428       if (tree_expr_nonzero_warnv_p (op0,
15429                                      &sub_strict_overflow_p)
15430           && tree_expr_nonzero_warnv_p (op1,
15431                                         &sub_strict_overflow_p))
15432         {
15433           if (sub_strict_overflow_p)
15434             *strict_overflow_p = true;
15435         }
15436       break;
15437
15438     case MAX_EXPR:
15439       sub_strict_overflow_p = false;
15440       if (tree_expr_nonzero_warnv_p (op0,
15441                                      &sub_strict_overflow_p))
15442         {
15443           if (sub_strict_overflow_p)
15444             *strict_overflow_p = true;
15445
15446           /* When both operands are nonzero, then MAX must be too.  */
15447           if (tree_expr_nonzero_warnv_p (op1,
15448                                          strict_overflow_p))
15449             return true;
15450
15451           /* MAX where operand 0 is positive is positive.  */
15452           return tree_expr_nonnegative_warnv_p (op0,
15453                                                strict_overflow_p);
15454         }
15455       /* MAX where operand 1 is positive is positive.  */
15456       else if (tree_expr_nonzero_warnv_p (op1,
15457                                           &sub_strict_overflow_p)
15458                && tree_expr_nonnegative_warnv_p (op1,
15459                                                  &sub_strict_overflow_p))
15460         {
15461           if (sub_strict_overflow_p)
15462             *strict_overflow_p = true;
15463           return true;
15464         }
15465       break;
15466
15467     case BIT_IOR_EXPR:
15468       return (tree_expr_nonzero_warnv_p (op1,
15469                                          strict_overflow_p)
15470               || tree_expr_nonzero_warnv_p (op0,
15471                                             strict_overflow_p));
15472
15473     default:
15474       break;
15475   }
15476
15477   return false;
15478 }
15479
15480 /* Return true when T is an address and is known to be nonzero.
15481    For floating point we further ensure that T is not denormal.
15482    Similar logic is present in nonzero_address in rtlanal.h.
15483
15484    If the return value is based on the assumption that signed overflow
15485    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15486    change *STRICT_OVERFLOW_P.  */
15487
15488 bool
15489 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15490 {
15491   bool sub_strict_overflow_p;
15492   switch (TREE_CODE (t))
15493     {
15494     case INTEGER_CST:
15495       return !integer_zerop (t);
15496
15497     case ADDR_EXPR:
15498       {
15499         tree base = TREE_OPERAND (t, 0);
15500         if (!DECL_P (base))
15501           base = get_base_address (base);
15502
15503         if (!base)
15504           return false;
15505
15506         /* Weak declarations may link to NULL.  Other things may also be NULL
15507            so protect with -fdelete-null-pointer-checks; but not variables
15508            allocated on the stack.  */
15509         if (DECL_P (base)
15510             && (flag_delete_null_pointer_checks
15511                 || (DECL_CONTEXT (base)
15512                     && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
15513                     && auto_var_in_fn_p (base, DECL_CONTEXT (base)))))
15514           return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
15515
15516         /* Constants are never weak.  */
15517         if (CONSTANT_CLASS_P (base))
15518           return true;
15519
15520         return false;
15521       }
15522
15523     case COND_EXPR:
15524       sub_strict_overflow_p = false;
15525       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15526                                      &sub_strict_overflow_p)
15527           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15528                                         &sub_strict_overflow_p))
15529         {
15530           if (sub_strict_overflow_p)
15531             *strict_overflow_p = true;
15532           return true;
15533         }
15534       break;
15535
15536     default:
15537       break;
15538     }
15539   return false;
15540 }
15541
15542 /* Return true when T is an address and is known to be nonzero.
15543    For floating point we further ensure that T is not denormal.
15544    Similar logic is present in nonzero_address in rtlanal.h.
15545
15546    If the return value is based on the assumption that signed overflow
15547    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15548    change *STRICT_OVERFLOW_P.  */
15549
15550 bool
15551 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15552 {
15553   tree type = TREE_TYPE (t);
15554   enum tree_code code;
15555
15556   /* Doing something useful for floating point would need more work.  */
15557   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15558     return false;
15559
15560   code = TREE_CODE (t);
15561   switch (TREE_CODE_CLASS (code))
15562     {
15563     case tcc_unary:
15564       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15565                                               strict_overflow_p);
15566     case tcc_binary:
15567     case tcc_comparison:
15568       return tree_binary_nonzero_warnv_p (code, type,
15569                                                TREE_OPERAND (t, 0),
15570                                                TREE_OPERAND (t, 1),
15571                                                strict_overflow_p);
15572     case tcc_constant:
15573     case tcc_declaration:
15574     case tcc_reference:
15575       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15576
15577     default:
15578       break;
15579     }
15580
15581   switch (code)
15582     {
15583     case TRUTH_NOT_EXPR:
15584       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15585                                               strict_overflow_p);
15586
15587     case TRUTH_AND_EXPR:
15588     case TRUTH_OR_EXPR:
15589     case TRUTH_XOR_EXPR:
15590       return tree_binary_nonzero_warnv_p (code, type,
15591                                                TREE_OPERAND (t, 0),
15592                                                TREE_OPERAND (t, 1),
15593                                                strict_overflow_p);
15594
15595     case COND_EXPR:
15596     case CONSTRUCTOR:
15597     case OBJ_TYPE_REF:
15598     case ASSERT_EXPR:
15599     case ADDR_EXPR:
15600     case WITH_SIZE_EXPR:
15601     case SSA_NAME:
15602       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15603
15604     case COMPOUND_EXPR:
15605     case MODIFY_EXPR:
15606     case BIND_EXPR:
15607       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15608                                         strict_overflow_p);
15609
15610     case SAVE_EXPR:
15611       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15612                                         strict_overflow_p);
15613
15614     case CALL_EXPR:
15615       return alloca_call_p (t);
15616
15617     default:
15618       break;
15619     }
15620   return false;
15621 }
15622
15623 /* Return true when T is an address and is known to be nonzero.
15624    Handle warnings about undefined signed overflow.  */
15625
15626 bool
15627 tree_expr_nonzero_p (tree t)
15628 {
15629   bool ret, strict_overflow_p;
15630
15631   strict_overflow_p = false;
15632   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15633   if (strict_overflow_p)
15634     fold_overflow_warning (("assuming signed overflow does not occur when "
15635                             "determining that expression is always "
15636                             "non-zero"),
15637                            WARN_STRICT_OVERFLOW_MISC);
15638   return ret;
15639 }
15640
15641 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15642    attempt to fold the expression to a constant without modifying TYPE,
15643    OP0 or OP1.
15644
15645    If the expression could be simplified to a constant, then return
15646    the constant.  If the expression would not be simplified to a
15647    constant, then return NULL_TREE.  */
15648
15649 tree
15650 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15651 {
15652   tree tem = fold_binary (code, type, op0, op1);
15653   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15654 }
15655
15656 /* Given the components of a unary expression CODE, TYPE and OP0,
15657    attempt to fold the expression to a constant without modifying
15658    TYPE or OP0.
15659
15660    If the expression could be simplified to a constant, then return
15661    the constant.  If the expression would not be simplified to a
15662    constant, then return NULL_TREE.  */
15663
15664 tree
15665 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15666 {
15667   tree tem = fold_unary (code, type, op0);
15668   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15669 }
15670
15671 /* If EXP represents referencing an element in a constant string
15672    (either via pointer arithmetic or array indexing), return the
15673    tree representing the value accessed, otherwise return NULL.  */
15674
15675 tree
15676 fold_read_from_constant_string (tree exp)
15677 {
15678   if ((TREE_CODE (exp) == INDIRECT_REF
15679        || TREE_CODE (exp) == ARRAY_REF)
15680       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15681     {
15682       tree exp1 = TREE_OPERAND (exp, 0);
15683       tree index;
15684       tree string;
15685       location_t loc = EXPR_LOCATION (exp);
15686
15687       if (TREE_CODE (exp) == INDIRECT_REF)
15688         string = string_constant (exp1, &index);
15689       else
15690         {
15691           tree low_bound = array_ref_low_bound (exp);
15692           index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15693
15694           /* Optimize the special-case of a zero lower bound.
15695
15696              We convert the low_bound to sizetype to avoid some problems
15697              with constant folding.  (E.g. suppose the lower bound is 1,
15698              and its mode is QI.  Without the conversion,l (ARRAY
15699              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15700              +INDEX), which becomes (ARRAY+255+INDEX).  Oops!)  */
15701           if (! integer_zerop (low_bound))
15702             index = size_diffop_loc (loc, index,
15703                                  fold_convert_loc (loc, sizetype, low_bound));
15704
15705           string = exp1;
15706         }
15707
15708       if (string
15709           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15710           && TREE_CODE (string) == STRING_CST
15711           && TREE_CODE (index) == INTEGER_CST
15712           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15713           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15714               == MODE_INT)
15715           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15716         return build_int_cst_type (TREE_TYPE (exp),
15717                                    (TREE_STRING_POINTER (string)
15718                                     [TREE_INT_CST_LOW (index)]));
15719     }
15720   return NULL;
15721 }
15722
15723 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15724    an integer constant, real, or fixed-point constant.
15725
15726    TYPE is the type of the result.  */
15727
15728 static tree
15729 fold_negate_const (tree arg0, tree type)
15730 {
15731   tree t = NULL_TREE;
15732
15733   switch (TREE_CODE (arg0))
15734     {
15735     case INTEGER_CST:
15736       {
15737         double_int val = tree_to_double_int (arg0);
15738         int overflow = neg_double (val.low, val.high, &val.low, &val.high);
15739
15740         t = force_fit_type_double (type, val, 1,
15741                                    (overflow | TREE_OVERFLOW (arg0))
15742                                    && !TYPE_UNSIGNED (type));
15743         break;
15744       }
15745
15746     case REAL_CST:
15747       t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15748       break;
15749
15750     case FIXED_CST:
15751       {
15752         FIXED_VALUE_TYPE f;
15753         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15754                                             &(TREE_FIXED_CST (arg0)), NULL,
15755                                             TYPE_SATURATING (type));
15756         t = build_fixed (type, f);
15757         /* Propagate overflow flags.  */
15758         if (overflow_p | TREE_OVERFLOW (arg0))
15759           TREE_OVERFLOW (t) = 1;
15760         break;
15761       }
15762
15763     default:
15764       gcc_unreachable ();
15765     }
15766
15767   return t;
15768 }
15769
15770 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15771    an integer constant or real constant.
15772
15773    TYPE is the type of the result.  */
15774
15775 tree
15776 fold_abs_const (tree arg0, tree type)
15777 {
15778   tree t = NULL_TREE;
15779
15780   switch (TREE_CODE (arg0))
15781     {
15782     case INTEGER_CST:
15783       {
15784         double_int val = tree_to_double_int (arg0);
15785
15786         /* If the value is unsigned or non-negative, then the absolute value
15787            is the same as the ordinary value.  */
15788         if (TYPE_UNSIGNED (type)
15789             || !double_int_negative_p (val))
15790           t = arg0;
15791
15792         /* If the value is negative, then the absolute value is
15793            its negation.  */
15794         else
15795           {
15796             int overflow;
15797
15798             overflow = neg_double (val.low, val.high, &val.low, &val.high);
15799             t = force_fit_type_double (type, val, -1,
15800                                        overflow | TREE_OVERFLOW (arg0));
15801           }
15802       }
15803       break;
15804
15805     case REAL_CST:
15806       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15807         t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15808       else
15809         t =  arg0;
15810       break;
15811
15812     default:
15813       gcc_unreachable ();
15814     }
15815
15816   return t;
15817 }
15818
15819 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15820    constant.  TYPE is the type of the result.  */
15821
15822 static tree
15823 fold_not_const (const_tree arg0, tree type)
15824 {
15825   double_int val;  
15826
15827   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15828
15829   val = double_int_not (tree_to_double_int (arg0));
15830   return force_fit_type_double (type, val, 0, TREE_OVERFLOW (arg0));
15831 }
15832
15833 /* Given CODE, a relational operator, the target type, TYPE and two
15834    constant operands OP0 and OP1, return the result of the
15835    relational operation.  If the result is not a compile time
15836    constant, then return NULL_TREE.  */
15837
15838 static tree
15839 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15840 {
15841   int result, invert;
15842
15843   /* From here on, the only cases we handle are when the result is
15844      known to be a constant.  */
15845
15846   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15847     {
15848       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15849       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15850
15851       /* Handle the cases where either operand is a NaN.  */
15852       if (real_isnan (c0) || real_isnan (c1))
15853         {
15854           switch (code)
15855             {
15856             case EQ_EXPR:
15857             case ORDERED_EXPR:
15858               result = 0;
15859               break;
15860
15861             case NE_EXPR:
15862             case UNORDERED_EXPR:
15863             case UNLT_EXPR:
15864             case UNLE_EXPR:
15865             case UNGT_EXPR:
15866             case UNGE_EXPR:
15867             case UNEQ_EXPR:
15868               result = 1;
15869               break;
15870
15871             case LT_EXPR:
15872             case LE_EXPR:
15873             case GT_EXPR:
15874             case GE_EXPR:
15875             case LTGT_EXPR:
15876               if (flag_trapping_math)
15877                 return NULL_TREE;
15878               result = 0;
15879               break;
15880
15881             default:
15882               gcc_unreachable ();
15883             }
15884
15885           return constant_boolean_node (result, type);
15886         }
15887
15888       return constant_boolean_node (real_compare (code, c0, c1), type);
15889     }
15890
15891   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15892     {
15893       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15894       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15895       return constant_boolean_node (fixed_compare (code, c0, c1), type);
15896     }
15897
15898   /* Handle equality/inequality of complex constants.  */
15899   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15900     {
15901       tree rcond = fold_relational_const (code, type,
15902                                           TREE_REALPART (op0),
15903                                           TREE_REALPART (op1));
15904       tree icond = fold_relational_const (code, type,
15905                                           TREE_IMAGPART (op0),
15906                                           TREE_IMAGPART (op1));
15907       if (code == EQ_EXPR)
15908         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15909       else if (code == NE_EXPR)
15910         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15911       else
15912         return NULL_TREE;
15913     }
15914
15915   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15916
15917      To compute GT, swap the arguments and do LT.
15918      To compute GE, do LT and invert the result.
15919      To compute LE, swap the arguments, do LT and invert the result.
15920      To compute NE, do EQ and invert the result.
15921
15922      Therefore, the code below must handle only EQ and LT.  */
15923
15924   if (code == LE_EXPR || code == GT_EXPR)
15925     {
15926       tree tem = op0;
15927       op0 = op1;
15928       op1 = tem;
15929       code = swap_tree_comparison (code);
15930     }
15931
15932   /* Note that it is safe to invert for real values here because we
15933      have already handled the one case that it matters.  */
15934
15935   invert = 0;
15936   if (code == NE_EXPR || code == GE_EXPR)
15937     {
15938       invert = 1;
15939       code = invert_tree_comparison (code, false);
15940     }
15941
15942   /* Compute a result for LT or EQ if args permit;
15943      Otherwise return T.  */
15944   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15945     {
15946       if (code == EQ_EXPR)
15947         result = tree_int_cst_equal (op0, op1);
15948       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15949         result = INT_CST_LT_UNSIGNED (op0, op1);
15950       else
15951         result = INT_CST_LT (op0, op1);
15952     }
15953   else
15954     return NULL_TREE;
15955
15956   if (invert)
15957     result ^= 1;
15958   return constant_boolean_node (result, type);
15959 }
15960
15961 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15962    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
15963    itself.  */
15964
15965 tree
15966 fold_build_cleanup_point_expr (tree type, tree expr)
15967 {
15968   /* If the expression does not have side effects then we don't have to wrap
15969      it with a cleanup point expression.  */
15970   if (!TREE_SIDE_EFFECTS (expr))
15971     return expr;
15972
15973   /* If the expression is a return, check to see if the expression inside the
15974      return has no side effects or the right hand side of the modify expression
15975      inside the return. If either don't have side effects set we don't need to
15976      wrap the expression in a cleanup point expression.  Note we don't check the
15977      left hand side of the modify because it should always be a return decl.  */
15978   if (TREE_CODE (expr) == RETURN_EXPR)
15979     {
15980       tree op = TREE_OPERAND (expr, 0);
15981       if (!op || !TREE_SIDE_EFFECTS (op))
15982         return expr;
15983       op = TREE_OPERAND (op, 1);
15984       if (!TREE_SIDE_EFFECTS (op))
15985         return expr;
15986     }
15987
15988   return build1 (CLEANUP_POINT_EXPR, type, expr);
15989 }
15990
15991 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15992    of an indirection through OP0, or NULL_TREE if no simplification is
15993    possible.  */
15994
15995 tree
15996 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15997 {
15998   tree sub = op0;
15999   tree subtype;
16000
16001   STRIP_NOPS (sub);
16002   subtype = TREE_TYPE (sub);
16003   if (!POINTER_TYPE_P (subtype))
16004     return NULL_TREE;
16005
16006   if (TREE_CODE (sub) == ADDR_EXPR)
16007     {
16008       tree op = TREE_OPERAND (sub, 0);
16009       tree optype = TREE_TYPE (op);
16010       /* *&CONST_DECL -> to the value of the const decl.  */
16011       if (TREE_CODE (op) == CONST_DECL)
16012         return DECL_INITIAL (op);
16013       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
16014       if (type == optype)
16015         {
16016           tree fop = fold_read_from_constant_string (op);
16017           if (fop)
16018             return fop;
16019           else
16020             return op;
16021         }
16022       /* *(foo *)&fooarray => fooarray[0] */
16023       else if (TREE_CODE (optype) == ARRAY_TYPE
16024                && type == TREE_TYPE (optype)
16025                && (!in_gimple_form
16026                    || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16027         {
16028           tree type_domain = TYPE_DOMAIN (optype);
16029           tree min_val = size_zero_node;
16030           if (type_domain && TYPE_MIN_VALUE (type_domain))
16031             min_val = TYPE_MIN_VALUE (type_domain);
16032           if (in_gimple_form
16033               && TREE_CODE (min_val) != INTEGER_CST)
16034             return NULL_TREE;
16035           return build4_loc (loc, ARRAY_REF, type, op, min_val,
16036                              NULL_TREE, NULL_TREE);
16037         }
16038       /* *(foo *)&complexfoo => __real__ complexfoo */
16039       else if (TREE_CODE (optype) == COMPLEX_TYPE
16040                && type == TREE_TYPE (optype))
16041         return fold_build1_loc (loc, REALPART_EXPR, type, op);
16042       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16043       else if (TREE_CODE (optype) == VECTOR_TYPE
16044                && type == TREE_TYPE (optype))
16045         {
16046           tree part_width = TYPE_SIZE (type);
16047           tree index = bitsize_int (0);
16048           return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
16049         }
16050     }
16051
16052   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16053       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
16054     {
16055       tree op00 = TREE_OPERAND (sub, 0);
16056       tree op01 = TREE_OPERAND (sub, 1);
16057
16058       STRIP_NOPS (op00);
16059       if (TREE_CODE (op00) == ADDR_EXPR)
16060         {
16061           tree op00type;
16062           op00 = TREE_OPERAND (op00, 0);
16063           op00type = TREE_TYPE (op00);
16064
16065           /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16066           if (TREE_CODE (op00type) == VECTOR_TYPE
16067               && type == TREE_TYPE (op00type))
16068             {
16069               HOST_WIDE_INT offset = tree_low_cst (op01, 0);
16070               tree part_width = TYPE_SIZE (type);
16071               unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
16072               unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
16073               tree index = bitsize_int (indexi);
16074
16075               if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (op00type))
16076                 return fold_build3_loc (loc,
16077                                         BIT_FIELD_REF, type, op00,
16078                                         part_width, index);
16079
16080             }
16081           /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16082           else if (TREE_CODE (op00type) == COMPLEX_TYPE
16083                    && type == TREE_TYPE (op00type))
16084             {
16085               tree size = TYPE_SIZE_UNIT (type);
16086               if (tree_int_cst_equal (size, op01))
16087                 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
16088             }
16089           /* ((foo *)&fooarray)[1] => fooarray[1] */
16090           else if (TREE_CODE (op00type) == ARRAY_TYPE
16091                    && type == TREE_TYPE (op00type))
16092             {
16093               tree type_domain = TYPE_DOMAIN (op00type);
16094               tree min_val = size_zero_node;
16095               if (type_domain && TYPE_MIN_VALUE (type_domain))
16096                 min_val = TYPE_MIN_VALUE (type_domain);
16097               op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
16098                                      TYPE_SIZE_UNIT (type));
16099               op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
16100               return build4_loc (loc, ARRAY_REF, type, op00, op01,
16101                                  NULL_TREE, NULL_TREE);
16102             }
16103         }
16104     }
16105
16106   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16107   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16108       && type == TREE_TYPE (TREE_TYPE (subtype))
16109       && (!in_gimple_form
16110           || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16111     {
16112       tree type_domain;
16113       tree min_val = size_zero_node;
16114       sub = build_fold_indirect_ref_loc (loc, sub);
16115       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16116       if (type_domain && TYPE_MIN_VALUE (type_domain))
16117         min_val = TYPE_MIN_VALUE (type_domain);
16118       if (in_gimple_form
16119           && TREE_CODE (min_val) != INTEGER_CST)
16120         return NULL_TREE;
16121       return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
16122                          NULL_TREE);
16123     }
16124
16125   return NULL_TREE;
16126 }
16127
16128 /* Builds an expression for an indirection through T, simplifying some
16129    cases.  */
16130
16131 tree
16132 build_fold_indirect_ref_loc (location_t loc, tree t)
16133 {
16134   tree type = TREE_TYPE (TREE_TYPE (t));
16135   tree sub = fold_indirect_ref_1 (loc, type, t);
16136
16137   if (sub)
16138     return sub;
16139
16140   return build1_loc (loc, INDIRECT_REF, type, t);
16141 }
16142
16143 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
16144
16145 tree
16146 fold_indirect_ref_loc (location_t loc, tree t)
16147 {
16148   tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16149
16150   if (sub)
16151     return sub;
16152   else
16153     return t;
16154 }
16155
16156 /* Strip non-trapping, non-side-effecting tree nodes from an expression
16157    whose result is ignored.  The type of the returned tree need not be
16158    the same as the original expression.  */
16159
16160 tree
16161 fold_ignored_result (tree t)
16162 {
16163   if (!TREE_SIDE_EFFECTS (t))
16164     return integer_zero_node;
16165
16166   for (;;)
16167     switch (TREE_CODE_CLASS (TREE_CODE (t)))
16168       {
16169       case tcc_unary:
16170         t = TREE_OPERAND (t, 0);
16171         break;
16172
16173       case tcc_binary:
16174       case tcc_comparison:
16175         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16176           t = TREE_OPERAND (t, 0);
16177         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16178           t = TREE_OPERAND (t, 1);
16179         else
16180           return t;
16181         break;
16182
16183       case tcc_expression:
16184         switch (TREE_CODE (t))
16185           {
16186           case COMPOUND_EXPR:
16187             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16188               return t;
16189             t = TREE_OPERAND (t, 0);
16190             break;
16191
16192           case COND_EXPR:
16193             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16194                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16195               return t;
16196             t = TREE_OPERAND (t, 0);
16197             break;
16198
16199           default:
16200             return t;
16201           }
16202         break;
16203
16204       default:
16205         return t;
16206       }
16207 }
16208
16209 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
16210    This can only be applied to objects of a sizetype.  */
16211
16212 tree
16213 round_up_loc (location_t loc, tree value, int divisor)
16214 {
16215   tree div = NULL_TREE;
16216
16217   gcc_assert (divisor > 0);
16218   if (divisor == 1)
16219     return value;
16220
16221   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
16222      have to do anything.  Only do this when we are not given a const,
16223      because in that case, this check is more expensive than just
16224      doing it.  */
16225   if (TREE_CODE (value) != INTEGER_CST)
16226     {
16227       div = build_int_cst (TREE_TYPE (value), divisor);
16228
16229       if (multiple_of_p (TREE_TYPE (value), value, div))
16230         return value;
16231     }
16232
16233   /* If divisor is a power of two, simplify this to bit manipulation.  */
16234   if (divisor == (divisor & -divisor))
16235     {
16236       if (TREE_CODE (value) == INTEGER_CST)
16237         {
16238           double_int val = tree_to_double_int (value);
16239           bool overflow_p;
16240
16241           if ((val.low & (divisor - 1)) == 0)
16242             return value;
16243
16244           overflow_p = TREE_OVERFLOW (value);
16245           val.low &= ~(divisor - 1);
16246           val.low += divisor;
16247           if (val.low == 0)
16248             {
16249               val.high++;
16250               if (val.high == 0)
16251                 overflow_p = true;
16252             }
16253
16254           return force_fit_type_double (TREE_TYPE (value), val,
16255                                         -1, overflow_p);
16256         }
16257       else
16258         {
16259           tree t;
16260
16261           t = build_int_cst (TREE_TYPE (value), divisor - 1);
16262           value = size_binop_loc (loc, PLUS_EXPR, value, t);
16263           t = build_int_cst (TREE_TYPE (value), -divisor);
16264           value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16265         }
16266     }
16267   else
16268     {
16269       if (!div)
16270         div = build_int_cst (TREE_TYPE (value), divisor);
16271       value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16272       value = size_binop_loc (loc, MULT_EXPR, value, div);
16273     }
16274
16275   return value;
16276 }
16277
16278 /* Likewise, but round down.  */
16279
16280 tree
16281 round_down_loc (location_t loc, tree value, int divisor)
16282 {
16283   tree div = NULL_TREE;
16284
16285   gcc_assert (divisor > 0);
16286   if (divisor == 1)
16287     return value;
16288
16289   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
16290      have to do anything.  Only do this when we are not given a const,
16291      because in that case, this check is more expensive than just
16292      doing it.  */
16293   if (TREE_CODE (value) != INTEGER_CST)
16294     {
16295       div = build_int_cst (TREE_TYPE (value), divisor);
16296
16297       if (multiple_of_p (TREE_TYPE (value), value, div))
16298         return value;
16299     }
16300
16301   /* If divisor is a power of two, simplify this to bit manipulation.  */
16302   if (divisor == (divisor & -divisor))
16303     {
16304       tree t;
16305
16306       t = build_int_cst (TREE_TYPE (value), -divisor);
16307       value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16308     }
16309   else
16310     {
16311       if (!div)
16312         div = build_int_cst (TREE_TYPE (value), divisor);
16313       value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16314       value = size_binop_loc (loc, MULT_EXPR, value, div);
16315     }
16316
16317   return value;
16318 }
16319
16320 /* Returns the pointer to the base of the object addressed by EXP and
16321    extracts the information about the offset of the access, storing it
16322    to PBITPOS and POFFSET.  */
16323
16324 static tree
16325 split_address_to_core_and_offset (tree exp,
16326                                   HOST_WIDE_INT *pbitpos, tree *poffset)
16327 {
16328   tree core;
16329   enum machine_mode mode;
16330   int unsignedp, volatilep;
16331   HOST_WIDE_INT bitsize;
16332   location_t loc = EXPR_LOCATION (exp);
16333
16334   if (TREE_CODE (exp) == ADDR_EXPR)
16335     {
16336       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16337                                   poffset, &mode, &unsignedp, &volatilep,
16338                                   false);
16339       core = build_fold_addr_expr_loc (loc, core);
16340     }
16341   else
16342     {
16343       core = exp;
16344       *pbitpos = 0;
16345       *poffset = NULL_TREE;
16346     }
16347
16348   return core;
16349 }
16350
16351 /* Returns true if addresses of E1 and E2 differ by a constant, false
16352    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
16353
16354 bool
16355 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
16356 {
16357   tree core1, core2;
16358   HOST_WIDE_INT bitpos1, bitpos2;
16359   tree toffset1, toffset2, tdiff, type;
16360
16361   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16362   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16363
16364   if (bitpos1 % BITS_PER_UNIT != 0
16365       || bitpos2 % BITS_PER_UNIT != 0
16366       || !operand_equal_p (core1, core2, 0))
16367     return false;
16368
16369   if (toffset1 && toffset2)
16370     {
16371       type = TREE_TYPE (toffset1);
16372       if (type != TREE_TYPE (toffset2))
16373         toffset2 = fold_convert (type, toffset2);
16374
16375       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16376       if (!cst_and_fits_in_hwi (tdiff))
16377         return false;
16378
16379       *diff = int_cst_value (tdiff);
16380     }
16381   else if (toffset1 || toffset2)
16382     {
16383       /* If only one of the offsets is non-constant, the difference cannot
16384          be a constant.  */
16385       return false;
16386     }
16387   else
16388     *diff = 0;
16389
16390   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
16391   return true;
16392 }
16393
16394 /* Simplify the floating point expression EXP when the sign of the
16395    result is not significant.  Return NULL_TREE if no simplification
16396    is possible.  */
16397
16398 tree
16399 fold_strip_sign_ops (tree exp)
16400 {
16401   tree arg0, arg1;
16402   location_t loc = EXPR_LOCATION (exp);
16403
16404   switch (TREE_CODE (exp))
16405     {
16406     case ABS_EXPR:
16407     case NEGATE_EXPR:
16408       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16409       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
16410
16411     case MULT_EXPR:
16412     case RDIV_EXPR:
16413       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
16414         return NULL_TREE;
16415       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16416       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16417       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
16418         return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
16419                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
16420                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
16421       break;
16422
16423     case COMPOUND_EXPR:
16424       arg0 = TREE_OPERAND (exp, 0);
16425       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16426       if (arg1)
16427         return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
16428       break;
16429
16430     case COND_EXPR:
16431       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16432       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
16433       if (arg0 || arg1)
16434         return fold_build3_loc (loc,
16435                             COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
16436                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
16437                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
16438       break;
16439
16440     case CALL_EXPR:
16441       {
16442         const enum built_in_function fcode = builtin_mathfn_code (exp);
16443         switch (fcode)
16444         {
16445         CASE_FLT_FN (BUILT_IN_COPYSIGN):
16446           /* Strip copysign function call, return the 1st argument. */
16447           arg0 = CALL_EXPR_ARG (exp, 0);
16448           arg1 = CALL_EXPR_ARG (exp, 1);
16449           return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
16450
16451         default:
16452           /* Strip sign ops from the argument of "odd" math functions.  */
16453           if (negate_mathfn_p (fcode))
16454             {
16455               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
16456               if (arg0)
16457                 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
16458             }
16459           break;
16460         }
16461       }
16462       break;
16463
16464     default:
16465       break;
16466     }
16467   return NULL_TREE;
16468 }