OSDN Git Service

2726e018665594fe456f6928da4ac39d18c65f0d
[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 tree range_binop (enum tree_code, tree, tree, int, tree, int);
116 static tree range_predecessor (tree);
117 static tree range_successor (tree);
118 extern tree make_range (tree, int *, tree *, tree *, bool *);
119 extern bool merge_ranges (int *, tree *, tree *, int, tree, tree, int,
120                           tree, tree);
121 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
122 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
123 static tree unextend (tree, int, int, tree);
124 static tree fold_truthop (location_t, enum tree_code, tree, tree, tree);
125 static tree optimize_minmax_comparison (location_t, enum tree_code,
126                                         tree, tree, tree);
127 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
128 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
129 static tree fold_binary_op_with_conditional_arg (location_t,
130                                                  enum tree_code, tree,
131                                                  tree, tree,
132                                                  tree, tree, int);
133 static tree fold_mathfn_compare (location_t,
134                                  enum built_in_function, enum tree_code,
135                                  tree, tree, tree);
136 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
137 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
138 static bool reorder_operands_p (const_tree, const_tree);
139 static tree fold_negate_const (tree, tree);
140 static tree fold_not_const (const_tree, tree);
141 static tree fold_relational_const (enum tree_code, tree, tree, tree);
142 static tree fold_convert_const (enum tree_code, tree, tree);
143
144 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
145    Otherwise, return LOC.  */
146
147 static location_t
148 expr_location_or (tree t, location_t loc)
149 {
150   location_t tloc = EXPR_LOCATION (t);
151   return tloc != UNKNOWN_LOCATION ? tloc : loc;
152 }
153
154 /* Similar to protected_set_expr_location, but never modify x in place,
155    if location can and needs to be set, unshare it.  */
156
157 static inline tree
158 protected_set_expr_location_unshare (tree x, location_t loc)
159 {
160   if (CAN_HAVE_LOCATION_P (x)
161       && EXPR_LOCATION (x) != loc
162       && !(TREE_CODE (x) == SAVE_EXPR
163            || TREE_CODE (x) == TARGET_EXPR
164            || TREE_CODE (x) == BIND_EXPR))
165     {
166       x = copy_node (x);
167       SET_EXPR_LOCATION (x, loc);
168     }
169   return x;
170 }
171
172
173 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
174    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
175    and SUM1.  Then this yields nonzero if overflow occurred during the
176    addition.
177
178    Overflow occurs if A and B have the same sign, but A and SUM differ in
179    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
180    sign.  */
181 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
182 \f
183 /* If ARG2 divides ARG1 with zero remainder, carries out the division
184    of type CODE and returns the quotient.
185    Otherwise returns NULL_TREE.  */
186
187 tree
188 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
189 {
190   double_int quo, rem;
191   int uns;
192
193   /* The sign of the division is according to operand two, that
194      does the correct thing for POINTER_PLUS_EXPR where we want
195      a signed division.  */
196   uns = TYPE_UNSIGNED (TREE_TYPE (arg2));
197   if (TREE_CODE (TREE_TYPE (arg2)) == INTEGER_TYPE
198       && TYPE_IS_SIZETYPE (TREE_TYPE (arg2)))
199     uns = false;
200
201   quo = double_int_divmod (tree_to_double_int (arg1),
202                            tree_to_double_int (arg2),
203                            uns, code, &rem);
204
205   if (double_int_zero_p (rem))
206     return build_int_cst_wide (TREE_TYPE (arg1), quo.low, quo.high);
207
208   return NULL_TREE; 
209 }
210 \f
211 /* This is nonzero if we should defer warnings about undefined
212    overflow.  This facility exists because these warnings are a
213    special case.  The code to estimate loop iterations does not want
214    to issue any warnings, since it works with expressions which do not
215    occur in user code.  Various bits of cleanup code call fold(), but
216    only use the result if it has certain characteristics (e.g., is a
217    constant); that code only wants to issue a warning if the result is
218    used.  */
219
220 static int fold_deferring_overflow_warnings;
221
222 /* If a warning about undefined overflow is deferred, this is the
223    warning.  Note that this may cause us to turn two warnings into
224    one, but that is fine since it is sufficient to only give one
225    warning per expression.  */
226
227 static const char* fold_deferred_overflow_warning;
228
229 /* If a warning about undefined overflow is deferred, this is the
230    level at which the warning should be emitted.  */
231
232 static enum warn_strict_overflow_code fold_deferred_overflow_code;
233
234 /* Start deferring overflow warnings.  We could use a stack here to
235    permit nested calls, but at present it is not necessary.  */
236
237 void
238 fold_defer_overflow_warnings (void)
239 {
240   ++fold_deferring_overflow_warnings;
241 }
242
243 /* Stop deferring overflow warnings.  If there is a pending warning,
244    and ISSUE is true, then issue the warning if appropriate.  STMT is
245    the statement with which the warning should be associated (used for
246    location information); STMT may be NULL.  CODE is the level of the
247    warning--a warn_strict_overflow_code value.  This function will use
248    the smaller of CODE and the deferred code when deciding whether to
249    issue the warning.  CODE may be zero to mean to always use the
250    deferred code.  */
251
252 void
253 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
254 {
255   const char *warnmsg;
256   location_t locus;
257
258   gcc_assert (fold_deferring_overflow_warnings > 0);
259   --fold_deferring_overflow_warnings;
260   if (fold_deferring_overflow_warnings > 0)
261     {
262       if (fold_deferred_overflow_warning != NULL
263           && code != 0
264           && code < (int) fold_deferred_overflow_code)
265         fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
266       return;
267     }
268
269   warnmsg = fold_deferred_overflow_warning;
270   fold_deferred_overflow_warning = NULL;
271
272   if (!issue || warnmsg == NULL)
273     return;
274
275   if (gimple_no_warning_p (stmt))
276     return;
277
278   /* Use the smallest code level when deciding to issue the
279      warning.  */
280   if (code == 0 || code > (int) fold_deferred_overflow_code)
281     code = fold_deferred_overflow_code;
282
283   if (!issue_strict_overflow_warning (code))
284     return;
285
286   if (stmt == NULL)
287     locus = input_location;
288   else
289     locus = gimple_location (stmt);
290   warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
291 }
292
293 /* Stop deferring overflow warnings, ignoring any deferred
294    warnings.  */
295
296 void
297 fold_undefer_and_ignore_overflow_warnings (void)
298 {
299   fold_undefer_overflow_warnings (false, NULL, 0);
300 }
301
302 /* Whether we are deferring overflow warnings.  */
303
304 bool
305 fold_deferring_overflow_warnings_p (void)
306 {
307   return fold_deferring_overflow_warnings > 0;
308 }
309
310 /* This is called when we fold something based on the fact that signed
311    overflow is undefined.  */
312
313 static void
314 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
315 {
316   if (fold_deferring_overflow_warnings > 0)
317     {
318       if (fold_deferred_overflow_warning == NULL
319           || wc < fold_deferred_overflow_code)
320         {
321           fold_deferred_overflow_warning = gmsgid;
322           fold_deferred_overflow_code = wc;
323         }
324     }
325   else if (issue_strict_overflow_warning (wc))
326     warning (OPT_Wstrict_overflow, gmsgid);
327 }
328 \f
329 /* Return true if the built-in mathematical function specified by CODE
330    is odd, i.e. -f(x) == f(-x).  */
331
332 static bool
333 negate_mathfn_p (enum built_in_function code)
334 {
335   switch (code)
336     {
337     CASE_FLT_FN (BUILT_IN_ASIN):
338     CASE_FLT_FN (BUILT_IN_ASINH):
339     CASE_FLT_FN (BUILT_IN_ATAN):
340     CASE_FLT_FN (BUILT_IN_ATANH):
341     CASE_FLT_FN (BUILT_IN_CASIN):
342     CASE_FLT_FN (BUILT_IN_CASINH):
343     CASE_FLT_FN (BUILT_IN_CATAN):
344     CASE_FLT_FN (BUILT_IN_CATANH):
345     CASE_FLT_FN (BUILT_IN_CBRT):
346     CASE_FLT_FN (BUILT_IN_CPROJ):
347     CASE_FLT_FN (BUILT_IN_CSIN):
348     CASE_FLT_FN (BUILT_IN_CSINH):
349     CASE_FLT_FN (BUILT_IN_CTAN):
350     CASE_FLT_FN (BUILT_IN_CTANH):
351     CASE_FLT_FN (BUILT_IN_ERF):
352     CASE_FLT_FN (BUILT_IN_LLROUND):
353     CASE_FLT_FN (BUILT_IN_LROUND):
354     CASE_FLT_FN (BUILT_IN_ROUND):
355     CASE_FLT_FN (BUILT_IN_SIN):
356     CASE_FLT_FN (BUILT_IN_SINH):
357     CASE_FLT_FN (BUILT_IN_TAN):
358     CASE_FLT_FN (BUILT_IN_TANH):
359     CASE_FLT_FN (BUILT_IN_TRUNC):
360       return true;
361
362     CASE_FLT_FN (BUILT_IN_LLRINT):
363     CASE_FLT_FN (BUILT_IN_LRINT):
364     CASE_FLT_FN (BUILT_IN_NEARBYINT):
365     CASE_FLT_FN (BUILT_IN_RINT):
366       return !flag_rounding_math;
367
368     default:
369       break;
370     }
371   return false;
372 }
373
374 /* Check whether we may negate an integer constant T without causing
375    overflow.  */
376
377 bool
378 may_negate_without_overflow_p (const_tree t)
379 {
380   unsigned HOST_WIDE_INT val;
381   unsigned int prec;
382   tree type;
383
384   gcc_assert (TREE_CODE (t) == INTEGER_CST);
385
386   type = TREE_TYPE (t);
387   if (TYPE_UNSIGNED (type))
388     return false;
389
390   prec = TYPE_PRECISION (type);
391   if (prec > HOST_BITS_PER_WIDE_INT)
392     {
393       if (TREE_INT_CST_LOW (t) != 0)
394         return true;
395       prec -= HOST_BITS_PER_WIDE_INT;
396       val = TREE_INT_CST_HIGH (t);
397     }
398   else
399     val = TREE_INT_CST_LOW (t);
400   if (prec < HOST_BITS_PER_WIDE_INT)
401     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
402   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
403 }
404
405 /* Determine whether an expression T can be cheaply negated using
406    the function negate_expr without introducing undefined overflow.  */
407
408 static bool
409 negate_expr_p (tree t)
410 {
411   tree type;
412
413   if (t == 0)
414     return false;
415
416   type = TREE_TYPE (t);
417
418   STRIP_SIGN_NOPS (t);
419   switch (TREE_CODE (t))
420     {
421     case INTEGER_CST:
422       if (TYPE_OVERFLOW_WRAPS (type))
423         return true;
424
425       /* Check that -CST will not overflow type.  */
426       return may_negate_without_overflow_p (t);
427     case BIT_NOT_EXPR:
428       return (INTEGRAL_TYPE_P (type)
429               && TYPE_OVERFLOW_WRAPS (type));
430
431     case FIXED_CST:
432     case NEGATE_EXPR:
433       return true;
434
435     case REAL_CST:
436       /* We want to canonicalize to positive real constants.  Pretend
437          that only negative ones can be easily negated.  */
438       return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
439
440     case COMPLEX_CST:
441       return negate_expr_p (TREE_REALPART (t))
442              && negate_expr_p (TREE_IMAGPART (t));
443
444     case COMPLEX_EXPR:
445       return negate_expr_p (TREE_OPERAND (t, 0))
446              && negate_expr_p (TREE_OPERAND (t, 1));
447
448     case CONJ_EXPR:
449       return negate_expr_p (TREE_OPERAND (t, 0));
450
451     case PLUS_EXPR:
452       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
453           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
454         return false;
455       /* -(A + B) -> (-B) - A.  */
456       if (negate_expr_p (TREE_OPERAND (t, 1))
457           && reorder_operands_p (TREE_OPERAND (t, 0),
458                                  TREE_OPERAND (t, 1)))
459         return true;
460       /* -(A + B) -> (-A) - B.  */
461       return negate_expr_p (TREE_OPERAND (t, 0));
462
463     case MINUS_EXPR:
464       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
465       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
466              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
467              && reorder_operands_p (TREE_OPERAND (t, 0),
468                                     TREE_OPERAND (t, 1));
469
470     case MULT_EXPR:
471       if (TYPE_UNSIGNED (TREE_TYPE (t)))
472         break;
473
474       /* Fall through.  */
475
476     case RDIV_EXPR:
477       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
478         return negate_expr_p (TREE_OPERAND (t, 1))
479                || negate_expr_p (TREE_OPERAND (t, 0));
480       break;
481
482     case TRUNC_DIV_EXPR:
483     case ROUND_DIV_EXPR:
484     case FLOOR_DIV_EXPR:
485     case CEIL_DIV_EXPR:
486     case EXACT_DIV_EXPR:
487       /* In general we can't negate A / B, because if A is INT_MIN and
488          B is 1, we may turn this into INT_MIN / -1 which is undefined
489          and actually traps on some architectures.  But if overflow is
490          undefined, we can negate, because - (INT_MIN / 1) is an
491          overflow.  */
492       if (INTEGRAL_TYPE_P (TREE_TYPE (t))
493           && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
494         break;
495       return negate_expr_p (TREE_OPERAND (t, 1))
496              || negate_expr_p (TREE_OPERAND (t, 0));
497
498     case NOP_EXPR:
499       /* Negate -((double)float) as (double)(-float).  */
500       if (TREE_CODE (type) == REAL_TYPE)
501         {
502           tree tem = strip_float_extensions (t);
503           if (tem != t)
504             return negate_expr_p (tem);
505         }
506       break;
507
508     case CALL_EXPR:
509       /* Negate -f(x) as f(-x).  */
510       if (negate_mathfn_p (builtin_mathfn_code (t)))
511         return negate_expr_p (CALL_EXPR_ARG (t, 0));
512       break;
513
514     case RSHIFT_EXPR:
515       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
516       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
517         {
518           tree op1 = TREE_OPERAND (t, 1);
519           if (TREE_INT_CST_HIGH (op1) == 0
520               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
521                  == TREE_INT_CST_LOW (op1))
522             return true;
523         }
524       break;
525
526     default:
527       break;
528     }
529   return false;
530 }
531
532 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
533    simplification is possible.
534    If negate_expr_p would return true for T, NULL_TREE will never be
535    returned.  */
536
537 static tree
538 fold_negate_expr (location_t loc, tree t)
539 {
540   tree type = TREE_TYPE (t);
541   tree tem;
542
543   switch (TREE_CODE (t))
544     {
545     /* Convert - (~A) to A + 1.  */
546     case BIT_NOT_EXPR:
547       if (INTEGRAL_TYPE_P (type))
548         return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
549                             build_int_cst (type, 1));
550       break;
551
552     case INTEGER_CST:
553       tem = fold_negate_const (t, type);
554       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
555           || !TYPE_OVERFLOW_TRAPS (type))
556         return tem;
557       break;
558
559     case REAL_CST:
560       tem = fold_negate_const (t, type);
561       /* Two's complement FP formats, such as c4x, may overflow.  */
562       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
563         return tem;
564       break;
565
566     case FIXED_CST:
567       tem = fold_negate_const (t, type);
568       return tem;
569
570     case COMPLEX_CST:
571       {
572         tree rpart = negate_expr (TREE_REALPART (t));
573         tree ipart = negate_expr (TREE_IMAGPART (t));
574
575         if ((TREE_CODE (rpart) == REAL_CST
576              && TREE_CODE (ipart) == REAL_CST)
577             || (TREE_CODE (rpart) == INTEGER_CST
578                 && TREE_CODE (ipart) == INTEGER_CST))
579           return build_complex (type, rpart, ipart);
580       }
581       break;
582
583     case COMPLEX_EXPR:
584       if (negate_expr_p (t))
585         return fold_build2_loc (loc, COMPLEX_EXPR, type,
586                             fold_negate_expr (loc, TREE_OPERAND (t, 0)),
587                             fold_negate_expr (loc, TREE_OPERAND (t, 1)));
588       break;
589
590     case CONJ_EXPR:
591       if (negate_expr_p (t))
592         return fold_build1_loc (loc, CONJ_EXPR, type,
593                             fold_negate_expr (loc, TREE_OPERAND (t, 0)));
594       break;
595
596     case NEGATE_EXPR:
597       return TREE_OPERAND (t, 0);
598
599     case PLUS_EXPR:
600       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
601           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
602         {
603           /* -(A + B) -> (-B) - A.  */
604           if (negate_expr_p (TREE_OPERAND (t, 1))
605               && reorder_operands_p (TREE_OPERAND (t, 0),
606                                      TREE_OPERAND (t, 1)))
607             {
608               tem = negate_expr (TREE_OPERAND (t, 1));
609               return fold_build2_loc (loc, MINUS_EXPR, type,
610                                   tem, TREE_OPERAND (t, 0));
611             }
612
613           /* -(A + B) -> (-A) - B.  */
614           if (negate_expr_p (TREE_OPERAND (t, 0)))
615             {
616               tem = negate_expr (TREE_OPERAND (t, 0));
617               return fold_build2_loc (loc, MINUS_EXPR, type,
618                                   tem, TREE_OPERAND (t, 1));
619             }
620         }
621       break;
622
623     case MINUS_EXPR:
624       /* - (A - B) -> B - A  */
625       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
626           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
627           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
628         return fold_build2_loc (loc, MINUS_EXPR, type,
629                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
630       break;
631
632     case MULT_EXPR:
633       if (TYPE_UNSIGNED (type))
634         break;
635
636       /* Fall through.  */
637
638     case RDIV_EXPR:
639       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
640         {
641           tem = TREE_OPERAND (t, 1);
642           if (negate_expr_p (tem))
643             return fold_build2_loc (loc, TREE_CODE (t), type,
644                                 TREE_OPERAND (t, 0), negate_expr (tem));
645           tem = TREE_OPERAND (t, 0);
646           if (negate_expr_p (tem))
647             return fold_build2_loc (loc, TREE_CODE (t), type,
648                                 negate_expr (tem), TREE_OPERAND (t, 1));
649         }
650       break;
651
652     case TRUNC_DIV_EXPR:
653     case ROUND_DIV_EXPR:
654     case FLOOR_DIV_EXPR:
655     case CEIL_DIV_EXPR:
656     case EXACT_DIV_EXPR:
657       /* In general we can't negate A / B, because if A is INT_MIN and
658          B is 1, we may turn this into INT_MIN / -1 which is undefined
659          and actually traps on some architectures.  But if overflow is
660          undefined, we can negate, because - (INT_MIN / 1) is an
661          overflow.  */
662       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
663         {
664           const char * const warnmsg = G_("assuming signed overflow does not "
665                                           "occur when negating a division");
666           tem = TREE_OPERAND (t, 1);
667           if (negate_expr_p (tem))
668             {
669               if (INTEGRAL_TYPE_P (type)
670                   && (TREE_CODE (tem) != INTEGER_CST
671                       || integer_onep (tem)))
672                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
673               return fold_build2_loc (loc, TREE_CODE (t), type,
674                                   TREE_OPERAND (t, 0), negate_expr (tem));
675             }
676           tem = TREE_OPERAND (t, 0);
677           if (negate_expr_p (tem))
678             {
679               if (INTEGRAL_TYPE_P (type)
680                   && (TREE_CODE (tem) != INTEGER_CST
681                       || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
682                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
683               return fold_build2_loc (loc, TREE_CODE (t), type,
684                                   negate_expr (tem), TREE_OPERAND (t, 1));
685             }
686         }
687       break;
688
689     case NOP_EXPR:
690       /* Convert -((double)float) into (double)(-float).  */
691       if (TREE_CODE (type) == REAL_TYPE)
692         {
693           tem = strip_float_extensions (t);
694           if (tem != t && negate_expr_p (tem))
695             return fold_convert_loc (loc, type, negate_expr (tem));
696         }
697       break;
698
699     case CALL_EXPR:
700       /* Negate -f(x) as f(-x).  */
701       if (negate_mathfn_p (builtin_mathfn_code (t))
702           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
703         {
704           tree fndecl, arg;
705
706           fndecl = get_callee_fndecl (t);
707           arg = negate_expr (CALL_EXPR_ARG (t, 0));
708           return build_call_expr_loc (loc, fndecl, 1, arg);
709         }
710       break;
711
712     case RSHIFT_EXPR:
713       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
714       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
715         {
716           tree op1 = TREE_OPERAND (t, 1);
717           if (TREE_INT_CST_HIGH (op1) == 0
718               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
719                  == TREE_INT_CST_LOW (op1))
720             {
721               tree ntype = TYPE_UNSIGNED (type)
722                            ? signed_type_for (type)
723                            : unsigned_type_for (type);
724               tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
725               temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
726               return fold_convert_loc (loc, type, temp);
727             }
728         }
729       break;
730
731     default:
732       break;
733     }
734
735   return NULL_TREE;
736 }
737
738 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
739    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
740    return NULL_TREE. */
741
742 static tree
743 negate_expr (tree t)
744 {
745   tree type, tem;
746   location_t loc;
747
748   if (t == NULL_TREE)
749     return NULL_TREE;
750
751   loc = EXPR_LOCATION (t);
752   type = TREE_TYPE (t);
753   STRIP_SIGN_NOPS (t);
754
755   tem = fold_negate_expr (loc, t);
756   if (!tem)
757     tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
758   return fold_convert_loc (loc, type, tem);
759 }
760 \f
761 /* Split a tree IN into a constant, literal and variable parts that could be
762    combined with CODE to make IN.  "constant" means an expression with
763    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
764    commutative arithmetic operation.  Store the constant part into *CONP,
765    the literal in *LITP and return the variable part.  If a part isn't
766    present, set it to null.  If the tree does not decompose in this way,
767    return the entire tree as the variable part and the other parts as null.
768
769    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
770    case, we negate an operand that was subtracted.  Except if it is a
771    literal for which we use *MINUS_LITP instead.
772
773    If NEGATE_P is true, we are negating all of IN, again except a literal
774    for which we use *MINUS_LITP instead.
775
776    If IN is itself a literal or constant, return it as appropriate.
777
778    Note that we do not guarantee that any of the three values will be the
779    same type as IN, but they will have the same signedness and mode.  */
780
781 static tree
782 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
783             tree *minus_litp, int negate_p)
784 {
785   tree var = 0;
786
787   *conp = 0;
788   *litp = 0;
789   *minus_litp = 0;
790
791   /* Strip any conversions that don't change the machine mode or signedness.  */
792   STRIP_SIGN_NOPS (in);
793
794   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
795       || TREE_CODE (in) == FIXED_CST)
796     *litp = in;
797   else if (TREE_CODE (in) == code
798            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
799                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
800                /* We can associate addition and subtraction together (even
801                   though the C standard doesn't say so) for integers because
802                   the value is not affected.  For reals, the value might be
803                   affected, so we can't.  */
804                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
805                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
806     {
807       tree op0 = TREE_OPERAND (in, 0);
808       tree op1 = TREE_OPERAND (in, 1);
809       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
810       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
811
812       /* First see if either of the operands is a literal, then a constant.  */
813       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
814           || TREE_CODE (op0) == FIXED_CST)
815         *litp = op0, op0 = 0;
816       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
817                || TREE_CODE (op1) == FIXED_CST)
818         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
819
820       if (op0 != 0 && TREE_CONSTANT (op0))
821         *conp = op0, op0 = 0;
822       else if (op1 != 0 && TREE_CONSTANT (op1))
823         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
824
825       /* If we haven't dealt with either operand, this is not a case we can
826          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
827       if (op0 != 0 && op1 != 0)
828         var = in;
829       else if (op0 != 0)
830         var = op0;
831       else
832         var = op1, neg_var_p = neg1_p;
833
834       /* Now do any needed negations.  */
835       if (neg_litp_p)
836         *minus_litp = *litp, *litp = 0;
837       if (neg_conp_p)
838         *conp = negate_expr (*conp);
839       if (neg_var_p)
840         var = negate_expr (var);
841     }
842   else if (TREE_CONSTANT (in))
843     *conp = in;
844   else
845     var = in;
846
847   if (negate_p)
848     {
849       if (*litp)
850         *minus_litp = *litp, *litp = 0;
851       else if (*minus_litp)
852         *litp = *minus_litp, *minus_litp = 0;
853       *conp = negate_expr (*conp);
854       var = negate_expr (var);
855     }
856
857   return var;
858 }
859
860 /* Re-associate trees split by the above function.  T1 and T2 are
861    either expressions to associate or null.  Return the new
862    expression, if any.  LOC is the location of the new expression.  If
863    we build an operation, do it in TYPE and with CODE.  */
864
865 static tree
866 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
867 {
868   if (t1 == 0)
869     return t2;
870   else if (t2 == 0)
871     return t1;
872
873   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
874      try to fold this since we will have infinite recursion.  But do
875      deal with any NEGATE_EXPRs.  */
876   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
877       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
878     {
879       if (code == PLUS_EXPR)
880         {
881           if (TREE_CODE (t1) == NEGATE_EXPR)
882             return build2_loc (loc, MINUS_EXPR, type,
883                                fold_convert_loc (loc, type, t2),
884                                fold_convert_loc (loc, type,
885                                                  TREE_OPERAND (t1, 0)));
886           else if (TREE_CODE (t2) == NEGATE_EXPR)
887             return build2_loc (loc, MINUS_EXPR, type,
888                                fold_convert_loc (loc, type, t1),
889                                fold_convert_loc (loc, type,
890                                                  TREE_OPERAND (t2, 0)));
891           else if (integer_zerop (t2))
892             return fold_convert_loc (loc, type, t1);
893         }
894       else if (code == MINUS_EXPR)
895         {
896           if (integer_zerop (t2))
897             return fold_convert_loc (loc, type, t1);
898         }
899
900       return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
901                          fold_convert_loc (loc, type, t2));
902     }
903
904   return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
905                           fold_convert_loc (loc, type, t2));
906 }
907 \f
908 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
909    for use in int_const_binop, size_binop and size_diffop.  */
910
911 static bool
912 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
913 {
914   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
915     return false;
916   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
917     return false;
918
919   switch (code)
920     {
921     case LSHIFT_EXPR:
922     case RSHIFT_EXPR:
923     case LROTATE_EXPR:
924     case RROTATE_EXPR:
925       return true;
926
927     default:
928       break;
929     }
930
931   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
932          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
933          && TYPE_MODE (type1) == TYPE_MODE (type2);
934 }
935
936
937 /* Combine two integer constants ARG1 and ARG2 under operation CODE
938    to produce a new constant.  Return NULL_TREE if we don't know how
939    to evaluate CODE at compile-time.  */
940
941 tree
942 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
943 {
944   double_int op1, op2, res, tmp;
945   tree t;
946   tree type = TREE_TYPE (arg1);
947   bool uns = TYPE_UNSIGNED (type);
948   bool is_sizetype
949     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
950   bool overflow = false;
951
952   op1 = tree_to_double_int (arg1);
953   op2 = tree_to_double_int (arg2);
954
955   switch (code)
956     {
957     case BIT_IOR_EXPR:
958       res = double_int_ior (op1, op2);
959       break;
960
961     case BIT_XOR_EXPR:
962       res = double_int_xor (op1, op2);
963       break;
964
965     case BIT_AND_EXPR:
966       res = double_int_and (op1, op2);
967       break;
968
969     case RSHIFT_EXPR:
970       res = double_int_rshift (op1, double_int_to_shwi (op2),
971                                TYPE_PRECISION (type), !uns);
972       break;
973
974     case LSHIFT_EXPR:
975       /* It's unclear from the C standard whether shifts can overflow.
976          The following code ignores overflow; perhaps a C standard
977          interpretation ruling is needed.  */
978       res = double_int_lshift (op1, double_int_to_shwi (op2),
979                                TYPE_PRECISION (type), !uns);
980       break;
981
982     case RROTATE_EXPR:
983       res = double_int_rrotate (op1, double_int_to_shwi (op2),
984                                 TYPE_PRECISION (type));
985       break;
986
987     case LROTATE_EXPR:
988       res = double_int_lrotate (op1, double_int_to_shwi (op2),
989                                 TYPE_PRECISION (type));
990       break;
991
992     case PLUS_EXPR:
993       overflow = add_double (op1.low, op1.high, op2.low, op2.high,
994                              &res.low, &res.high);
995       break;
996
997     case MINUS_EXPR:
998       neg_double (op2.low, op2.high, &res.low, &res.high);
999       add_double (op1.low, op1.high, res.low, res.high,
1000                   &res.low, &res.high);
1001       overflow = OVERFLOW_SUM_SIGN (res.high, op2.high, op1.high);
1002       break;
1003
1004     case MULT_EXPR:
1005       overflow = mul_double (op1.low, op1.high, op2.low, op2.high,
1006                              &res.low, &res.high);
1007       break;
1008
1009     case TRUNC_DIV_EXPR:
1010     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1011     case EXACT_DIV_EXPR:
1012       /* This is a shortcut for a common special case.  */
1013       if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1014           && !TREE_OVERFLOW (arg1)
1015           && !TREE_OVERFLOW (arg2)
1016           && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1017         {
1018           if (code == CEIL_DIV_EXPR)
1019             op1.low += op2.low - 1;
1020
1021           res.low = op1.low / op2.low, res.high = 0;
1022           break;
1023         }
1024
1025       /* ... fall through ...  */
1026
1027     case ROUND_DIV_EXPR:
1028       if (double_int_zero_p (op2))
1029         return NULL_TREE;
1030       if (double_int_one_p (op2))
1031         {
1032           res = op1;
1033           break;
1034         }
1035       if (double_int_equal_p (op1, op2)
1036           && ! double_int_zero_p (op1))
1037         {
1038           res = double_int_one;
1039           break;
1040         }
1041       overflow = div_and_round_double (code, uns,
1042                                        op1.low, op1.high, op2.low, op2.high,
1043                                        &res.low, &res.high,
1044                                        &tmp.low, &tmp.high);
1045       break;
1046
1047     case TRUNC_MOD_EXPR:
1048     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1049       /* This is a shortcut for a common special case.  */
1050       if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1051           && !TREE_OVERFLOW (arg1)
1052           && !TREE_OVERFLOW (arg2)
1053           && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1054         {
1055           if (code == CEIL_MOD_EXPR)
1056             op1.low += op2.low - 1;
1057           res.low = op1.low % op2.low, res.high = 0;
1058           break;
1059         }
1060
1061       /* ... fall through ...  */
1062
1063     case ROUND_MOD_EXPR:
1064       if (double_int_zero_p (op2))
1065         return NULL_TREE;
1066       overflow = div_and_round_double (code, uns,
1067                                        op1.low, op1.high, op2.low, op2.high,
1068                                        &tmp.low, &tmp.high,
1069                                        &res.low, &res.high);
1070       break;
1071
1072     case MIN_EXPR:
1073       res = double_int_min (op1, op2, uns);
1074       break;
1075
1076     case MAX_EXPR:
1077       res = double_int_max (op1, op2, uns);
1078       break;
1079
1080     default:
1081       return NULL_TREE;
1082     }
1083
1084   t = force_fit_type_double (TREE_TYPE (arg1), res, 1,
1085                              ((!uns || is_sizetype) && overflow)
1086                              | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1087
1088   return t;
1089 }
1090
1091 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1092    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1093    are the same kind of constant and the same machine mode.  Return zero if
1094    combining the constants is not allowed in the current operating mode.  */
1095
1096 static tree
1097 const_binop (enum tree_code code, tree arg1, tree arg2)
1098 {
1099   /* Sanity check for the recursive cases.  */
1100   if (!arg1 || !arg2)
1101     return NULL_TREE;
1102
1103   STRIP_NOPS (arg1);
1104   STRIP_NOPS (arg2);
1105
1106   if (TREE_CODE (arg1) == INTEGER_CST)
1107     return int_const_binop (code, arg1, arg2);
1108
1109   if (TREE_CODE (arg1) == REAL_CST)
1110     {
1111       enum machine_mode mode;
1112       REAL_VALUE_TYPE d1;
1113       REAL_VALUE_TYPE d2;
1114       REAL_VALUE_TYPE value;
1115       REAL_VALUE_TYPE result;
1116       bool inexact;
1117       tree t, type;
1118
1119       /* The following codes are handled by real_arithmetic.  */
1120       switch (code)
1121         {
1122         case PLUS_EXPR:
1123         case MINUS_EXPR:
1124         case MULT_EXPR:
1125         case RDIV_EXPR:
1126         case MIN_EXPR:
1127         case MAX_EXPR:
1128           break;
1129
1130         default:
1131           return NULL_TREE;
1132         }
1133
1134       d1 = TREE_REAL_CST (arg1);
1135       d2 = TREE_REAL_CST (arg2);
1136
1137       type = TREE_TYPE (arg1);
1138       mode = TYPE_MODE (type);
1139
1140       /* Don't perform operation if we honor signaling NaNs and
1141          either operand is a NaN.  */
1142       if (HONOR_SNANS (mode)
1143           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1144         return NULL_TREE;
1145
1146       /* Don't perform operation if it would raise a division
1147          by zero exception.  */
1148       if (code == RDIV_EXPR
1149           && REAL_VALUES_EQUAL (d2, dconst0)
1150           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1151         return NULL_TREE;
1152
1153       /* If either operand is a NaN, just return it.  Otherwise, set up
1154          for floating-point trap; we return an overflow.  */
1155       if (REAL_VALUE_ISNAN (d1))
1156         return arg1;
1157       else if (REAL_VALUE_ISNAN (d2))
1158         return arg2;
1159
1160       inexact = real_arithmetic (&value, code, &d1, &d2);
1161       real_convert (&result, mode, &value);
1162
1163       /* Don't constant fold this floating point operation if
1164          the result has overflowed and flag_trapping_math.  */
1165       if (flag_trapping_math
1166           && MODE_HAS_INFINITIES (mode)
1167           && REAL_VALUE_ISINF (result)
1168           && !REAL_VALUE_ISINF (d1)
1169           && !REAL_VALUE_ISINF (d2))
1170         return NULL_TREE;
1171
1172       /* Don't constant fold this floating point operation if the
1173          result may dependent upon the run-time rounding mode and
1174          flag_rounding_math is set, or if GCC's software emulation
1175          is unable to accurately represent the result.  */
1176       if ((flag_rounding_math
1177            || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1178           && (inexact || !real_identical (&result, &value)))
1179         return NULL_TREE;
1180
1181       t = build_real (type, result);
1182
1183       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1184       return t;
1185     }
1186
1187   if (TREE_CODE (arg1) == FIXED_CST)
1188     {
1189       FIXED_VALUE_TYPE f1;
1190       FIXED_VALUE_TYPE f2;
1191       FIXED_VALUE_TYPE result;
1192       tree t, type;
1193       int sat_p;
1194       bool overflow_p;
1195
1196       /* The following codes are handled by fixed_arithmetic.  */
1197       switch (code)
1198         {
1199         case PLUS_EXPR:
1200         case MINUS_EXPR:
1201         case MULT_EXPR:
1202         case TRUNC_DIV_EXPR:
1203           f2 = TREE_FIXED_CST (arg2);
1204           break;
1205
1206         case LSHIFT_EXPR:
1207         case RSHIFT_EXPR:
1208           f2.data.high = TREE_INT_CST_HIGH (arg2);
1209           f2.data.low = TREE_INT_CST_LOW (arg2);
1210           f2.mode = SImode;
1211           break;
1212
1213         default:
1214           return NULL_TREE;
1215         }
1216
1217       f1 = TREE_FIXED_CST (arg1);
1218       type = TREE_TYPE (arg1);
1219       sat_p = TYPE_SATURATING (type);
1220       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1221       t = build_fixed (type, result);
1222       /* Propagate overflow flags.  */
1223       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1224         TREE_OVERFLOW (t) = 1;
1225       return t;
1226     }
1227
1228   if (TREE_CODE (arg1) == COMPLEX_CST)
1229     {
1230       tree type = TREE_TYPE (arg1);
1231       tree r1 = TREE_REALPART (arg1);
1232       tree i1 = TREE_IMAGPART (arg1);
1233       tree r2 = TREE_REALPART (arg2);
1234       tree i2 = TREE_IMAGPART (arg2);
1235       tree real, imag;
1236
1237       switch (code)
1238         {
1239         case PLUS_EXPR:
1240         case MINUS_EXPR:
1241           real = const_binop (code, r1, r2);
1242           imag = const_binop (code, i1, i2);
1243           break;
1244
1245         case MULT_EXPR:
1246           if (COMPLEX_FLOAT_TYPE_P (type))
1247             return do_mpc_arg2 (arg1, arg2, type,
1248                                 /* do_nonfinite= */ folding_initializer,
1249                                 mpc_mul);
1250
1251           real = const_binop (MINUS_EXPR,
1252                               const_binop (MULT_EXPR, r1, r2),
1253                               const_binop (MULT_EXPR, i1, i2));
1254           imag = const_binop (PLUS_EXPR,
1255                               const_binop (MULT_EXPR, r1, i2),
1256                               const_binop (MULT_EXPR, i1, r2));
1257           break;
1258
1259         case RDIV_EXPR:
1260           if (COMPLEX_FLOAT_TYPE_P (type))
1261             return do_mpc_arg2 (arg1, arg2, type,
1262                                 /* do_nonfinite= */ folding_initializer,
1263                                 mpc_div);
1264           /* Fallthru ... */
1265         case TRUNC_DIV_EXPR:
1266         case CEIL_DIV_EXPR:
1267         case FLOOR_DIV_EXPR:
1268         case ROUND_DIV_EXPR:
1269           if (flag_complex_method == 0)
1270           {
1271             /* Keep this algorithm in sync with
1272                tree-complex.c:expand_complex_div_straight().
1273
1274                Expand complex division to scalars, straightforward algorithm.
1275                a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1276                t = br*br + bi*bi
1277             */
1278             tree magsquared
1279               = const_binop (PLUS_EXPR,
1280                              const_binop (MULT_EXPR, r2, r2),
1281                              const_binop (MULT_EXPR, i2, i2));
1282             tree t1
1283               = const_binop (PLUS_EXPR,
1284                              const_binop (MULT_EXPR, r1, r2),
1285                              const_binop (MULT_EXPR, i1, i2));
1286             tree t2
1287               = const_binop (MINUS_EXPR,
1288                              const_binop (MULT_EXPR, i1, r2),
1289                              const_binop (MULT_EXPR, r1, i2));
1290
1291             real = const_binop (code, t1, magsquared);
1292             imag = const_binop (code, t2, magsquared);
1293           }
1294           else
1295           {
1296             /* Keep this algorithm in sync with
1297                tree-complex.c:expand_complex_div_wide().
1298
1299                Expand complex division to scalars, modified algorithm to minimize
1300                overflow with wide input ranges.  */
1301             tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1302                                         fold_abs_const (r2, TREE_TYPE (type)),
1303                                         fold_abs_const (i2, TREE_TYPE (type)));
1304
1305             if (integer_nonzerop (compare))
1306               {
1307                 /* In the TRUE branch, we compute
1308                    ratio = br/bi;
1309                    div = (br * ratio) + bi;
1310                    tr = (ar * ratio) + ai;
1311                    ti = (ai * ratio) - ar;
1312                    tr = tr / div;
1313                    ti = ti / div;  */
1314                 tree ratio = const_binop (code, r2, i2);
1315                 tree div = const_binop (PLUS_EXPR, i2,
1316                                         const_binop (MULT_EXPR, r2, ratio));
1317                 real = const_binop (MULT_EXPR, r1, ratio);
1318                 real = const_binop (PLUS_EXPR, real, i1);
1319                 real = const_binop (code, real, div);
1320
1321                 imag = const_binop (MULT_EXPR, i1, ratio);
1322                 imag = const_binop (MINUS_EXPR, imag, r1);
1323                 imag = const_binop (code, imag, div);
1324               }
1325             else
1326               {
1327                 /* In the FALSE branch, we compute
1328                    ratio = d/c;
1329                    divisor = (d * ratio) + c;
1330                    tr = (b * ratio) + a;
1331                    ti = b - (a * ratio);
1332                    tr = tr / div;
1333                    ti = ti / div;  */
1334                 tree ratio = const_binop (code, i2, r2);
1335                 tree div = const_binop (PLUS_EXPR, r2,
1336                                         const_binop (MULT_EXPR, i2, ratio));
1337
1338                 real = const_binop (MULT_EXPR, i1, ratio);
1339                 real = const_binop (PLUS_EXPR, real, r1);
1340                 real = const_binop (code, real, div);
1341
1342                 imag = const_binop (MULT_EXPR, r1, ratio);
1343                 imag = const_binop (MINUS_EXPR, i1, imag);
1344                 imag = const_binop (code, imag, div);
1345               }
1346           }
1347           break;
1348
1349         default:
1350           return NULL_TREE;
1351         }
1352
1353       if (real && imag)
1354         return build_complex (type, real, imag);
1355     }
1356
1357   if (TREE_CODE (arg1) == VECTOR_CST)
1358     {
1359       tree type = TREE_TYPE(arg1);
1360       int count = TYPE_VECTOR_SUBPARTS (type), i;
1361       tree elements1, elements2, list = NULL_TREE;
1362
1363       if(TREE_CODE(arg2) != VECTOR_CST)
1364         return NULL_TREE;
1365
1366       elements1 = TREE_VECTOR_CST_ELTS (arg1);
1367       elements2 = TREE_VECTOR_CST_ELTS (arg2);
1368
1369       for (i = 0; i < count; i++)
1370         {
1371           tree elem1, elem2, elem;
1372
1373           /* The trailing elements can be empty and should be treated as 0 */
1374           if(!elements1)
1375             elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1376           else
1377             {
1378               elem1 = TREE_VALUE(elements1);
1379               elements1 = TREE_CHAIN (elements1);
1380             }
1381
1382           if(!elements2)
1383             elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1384           else
1385             {
1386               elem2 = TREE_VALUE(elements2);
1387               elements2 = TREE_CHAIN (elements2);
1388             }
1389
1390           elem = const_binop (code, elem1, elem2);
1391
1392           /* It is possible that const_binop cannot handle the given
1393             code and return NULL_TREE */
1394           if(elem == NULL_TREE)
1395             return NULL_TREE;
1396
1397           list = tree_cons (NULL_TREE, elem, list);
1398         }
1399       return build_vector(type, nreverse(list));
1400     }
1401   return NULL_TREE;
1402 }
1403
1404 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
1405    indicates which particular sizetype to create.  */
1406
1407 tree
1408 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1409 {
1410   return build_int_cst (sizetype_tab[(int) kind], number);
1411 }
1412 \f
1413 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
1414    is a tree code.  The type of the result is taken from the operands.
1415    Both must be equivalent integer types, ala int_binop_types_match_p.
1416    If the operands are constant, so is the result.  */
1417
1418 tree
1419 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1420 {
1421   tree type = TREE_TYPE (arg0);
1422
1423   if (arg0 == error_mark_node || arg1 == error_mark_node)
1424     return error_mark_node;
1425
1426   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1427                                        TREE_TYPE (arg1)));
1428
1429   /* Handle the special case of two integer constants faster.  */
1430   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1431     {
1432       /* And some specific cases even faster than that.  */
1433       if (code == PLUS_EXPR)
1434         {
1435           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1436             return arg1;
1437           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1438             return arg0;
1439         }
1440       else if (code == MINUS_EXPR)
1441         {
1442           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1443             return arg0;
1444         }
1445       else if (code == MULT_EXPR)
1446         {
1447           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1448             return arg1;
1449         }
1450
1451       /* Handle general case of two integer constants.  */
1452       return int_const_binop (code, arg0, arg1);
1453     }
1454
1455   return fold_build2_loc (loc, code, type, arg0, arg1);
1456 }
1457
1458 /* Given two values, either both of sizetype or both of bitsizetype,
1459    compute the difference between the two values.  Return the value
1460    in signed type corresponding to the type of the operands.  */
1461
1462 tree
1463 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1464 {
1465   tree type = TREE_TYPE (arg0);
1466   tree ctype;
1467
1468   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1469                                        TREE_TYPE (arg1)));
1470
1471   /* If the type is already signed, just do the simple thing.  */
1472   if (!TYPE_UNSIGNED (type))
1473     return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1474
1475   if (type == sizetype)
1476     ctype = ssizetype;
1477   else if (type == bitsizetype)
1478     ctype = sbitsizetype;
1479   else
1480     ctype = signed_type_for (type);
1481
1482   /* If either operand is not a constant, do the conversions to the signed
1483      type and subtract.  The hardware will do the right thing with any
1484      overflow in the subtraction.  */
1485   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1486     return size_binop_loc (loc, MINUS_EXPR,
1487                            fold_convert_loc (loc, ctype, arg0),
1488                            fold_convert_loc (loc, ctype, arg1));
1489
1490   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1491      Otherwise, subtract the other way, convert to CTYPE (we know that can't
1492      overflow) and negate (which can't either).  Special-case a result
1493      of zero while we're here.  */
1494   if (tree_int_cst_equal (arg0, arg1))
1495     return build_int_cst (ctype, 0);
1496   else if (tree_int_cst_lt (arg1, arg0))
1497     return fold_convert_loc (loc, ctype,
1498                              size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1499   else
1500     return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1501                            fold_convert_loc (loc, ctype,
1502                                              size_binop_loc (loc,
1503                                                              MINUS_EXPR,
1504                                                              arg1, arg0)));
1505 }
1506 \f
1507 /* A subroutine of fold_convert_const handling conversions of an
1508    INTEGER_CST to another integer type.  */
1509
1510 static tree
1511 fold_convert_const_int_from_int (tree type, const_tree arg1)
1512 {
1513   tree t;
1514
1515   /* Given an integer constant, make new constant with new type,
1516      appropriately sign-extended or truncated.  */
1517   t = force_fit_type_double (type, tree_to_double_int (arg1),
1518                              !POINTER_TYPE_P (TREE_TYPE (arg1)),
1519                              (TREE_INT_CST_HIGH (arg1) < 0
1520                               && (TYPE_UNSIGNED (type)
1521                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1522                              | TREE_OVERFLOW (arg1));
1523
1524   return t;
1525 }
1526
1527 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1528    to an integer type.  */
1529
1530 static tree
1531 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1532 {
1533   int overflow = 0;
1534   tree t;
1535
1536   /* The following code implements the floating point to integer
1537      conversion rules required by the Java Language Specification,
1538      that IEEE NaNs are mapped to zero and values that overflow
1539      the target precision saturate, i.e. values greater than
1540      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1541      are mapped to INT_MIN.  These semantics are allowed by the
1542      C and C++ standards that simply state that the behavior of
1543      FP-to-integer conversion is unspecified upon overflow.  */
1544
1545   double_int val;
1546   REAL_VALUE_TYPE r;
1547   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1548
1549   switch (code)
1550     {
1551     case FIX_TRUNC_EXPR:
1552       real_trunc (&r, VOIDmode, &x);
1553       break;
1554
1555     default:
1556       gcc_unreachable ();
1557     }
1558
1559   /* If R is NaN, return zero and show we have an overflow.  */
1560   if (REAL_VALUE_ISNAN (r))
1561     {
1562       overflow = 1;
1563       val = double_int_zero;
1564     }
1565
1566   /* See if R is less than the lower bound or greater than the
1567      upper bound.  */
1568
1569   if (! overflow)
1570     {
1571       tree lt = TYPE_MIN_VALUE (type);
1572       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1573       if (REAL_VALUES_LESS (r, l))
1574         {
1575           overflow = 1;
1576           val = tree_to_double_int (lt);
1577         }
1578     }
1579
1580   if (! overflow)
1581     {
1582       tree ut = TYPE_MAX_VALUE (type);
1583       if (ut)
1584         {
1585           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1586           if (REAL_VALUES_LESS (u, r))
1587             {
1588               overflow = 1;
1589               val = tree_to_double_int (ut);
1590             }
1591         }
1592     }
1593
1594   if (! overflow)
1595     real_to_integer2 ((HOST_WIDE_INT *) &val.low, &val.high, &r);
1596
1597   t = force_fit_type_double (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1598   return t;
1599 }
1600
1601 /* A subroutine of fold_convert_const handling conversions of a
1602    FIXED_CST to an integer type.  */
1603
1604 static tree
1605 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1606 {
1607   tree t;
1608   double_int temp, temp_trunc;
1609   unsigned int mode;
1610
1611   /* Right shift FIXED_CST to temp by fbit.  */
1612   temp = TREE_FIXED_CST (arg1).data;
1613   mode = TREE_FIXED_CST (arg1).mode;
1614   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
1615     {
1616       temp = double_int_rshift (temp, GET_MODE_FBIT (mode),
1617                                 HOST_BITS_PER_DOUBLE_INT,
1618                                 SIGNED_FIXED_POINT_MODE_P (mode));
1619
1620       /* Left shift temp to temp_trunc by fbit.  */
1621       temp_trunc = double_int_lshift (temp, GET_MODE_FBIT (mode),
1622                                       HOST_BITS_PER_DOUBLE_INT,
1623                                       SIGNED_FIXED_POINT_MODE_P (mode));
1624     }
1625   else
1626     {
1627       temp = double_int_zero;
1628       temp_trunc = double_int_zero;
1629     }
1630
1631   /* If FIXED_CST is negative, we need to round the value toward 0.
1632      By checking if the fractional bits are not zero to add 1 to temp.  */
1633   if (SIGNED_FIXED_POINT_MODE_P (mode)
1634       && double_int_negative_p (temp_trunc)
1635       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
1636     temp = double_int_add (temp, double_int_one);
1637
1638   /* Given a fixed-point constant, make new constant with new type,
1639      appropriately sign-extended or truncated.  */
1640   t = force_fit_type_double (type, temp, -1,
1641                              (double_int_negative_p (temp)
1642                               && (TYPE_UNSIGNED (type)
1643                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1644                              | TREE_OVERFLOW (arg1));
1645
1646   return t;
1647 }
1648
1649 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1650    to another floating point type.  */
1651
1652 static tree
1653 fold_convert_const_real_from_real (tree type, const_tree arg1)
1654 {
1655   REAL_VALUE_TYPE value;
1656   tree t;
1657
1658   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1659   t = build_real (type, value);
1660
1661   /* If converting an infinity or NAN to a representation that doesn't
1662      have one, set the overflow bit so that we can produce some kind of
1663      error message at the appropriate point if necessary.  It's not the
1664      most user-friendly message, but it's better than nothing.  */
1665   if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1666       && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1667     TREE_OVERFLOW (t) = 1;
1668   else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1669            && !MODE_HAS_NANS (TYPE_MODE (type)))
1670     TREE_OVERFLOW (t) = 1;
1671   /* Regular overflow, conversion produced an infinity in a mode that
1672      can't represent them.  */
1673   else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1674            && REAL_VALUE_ISINF (value)
1675            && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1676     TREE_OVERFLOW (t) = 1;
1677   else
1678     TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1679   return t;
1680 }
1681
1682 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1683    to a floating point type.  */
1684
1685 static tree
1686 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
1687 {
1688   REAL_VALUE_TYPE value;
1689   tree t;
1690
1691   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
1692   t = build_real (type, value);
1693
1694   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1695   return t;
1696 }
1697
1698 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1699    to another fixed-point type.  */
1700
1701 static tree
1702 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
1703 {
1704   FIXED_VALUE_TYPE value;
1705   tree t;
1706   bool overflow_p;
1707
1708   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
1709                               TYPE_SATURATING (type));
1710   t = build_fixed (type, value);
1711
1712   /* Propagate overflow flags.  */
1713   if (overflow_p | TREE_OVERFLOW (arg1))
1714     TREE_OVERFLOW (t) = 1;
1715   return t;
1716 }
1717
1718 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
1719    to a fixed-point type.  */
1720
1721 static tree
1722 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
1723 {
1724   FIXED_VALUE_TYPE value;
1725   tree t;
1726   bool overflow_p;
1727
1728   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
1729                                        TREE_INT_CST (arg1),
1730                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
1731                                        TYPE_SATURATING (type));
1732   t = build_fixed (type, value);
1733
1734   /* Propagate overflow flags.  */
1735   if (overflow_p | TREE_OVERFLOW (arg1))
1736     TREE_OVERFLOW (t) = 1;
1737   return t;
1738 }
1739
1740 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1741    to a fixed-point type.  */
1742
1743 static tree
1744 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
1745 {
1746   FIXED_VALUE_TYPE value;
1747   tree t;
1748   bool overflow_p;
1749
1750   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
1751                                         &TREE_REAL_CST (arg1),
1752                                         TYPE_SATURATING (type));
1753   t = build_fixed (type, value);
1754
1755   /* Propagate overflow flags.  */
1756   if (overflow_p | TREE_OVERFLOW (arg1))
1757     TREE_OVERFLOW (t) = 1;
1758   return t;
1759 }
1760
1761 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1762    type TYPE.  If no simplification can be done return NULL_TREE.  */
1763
1764 static tree
1765 fold_convert_const (enum tree_code code, tree type, tree arg1)
1766 {
1767   if (TREE_TYPE (arg1) == type)
1768     return arg1;
1769
1770   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
1771       || TREE_CODE (type) == OFFSET_TYPE)
1772     {
1773       if (TREE_CODE (arg1) == INTEGER_CST)
1774         return fold_convert_const_int_from_int (type, arg1);
1775       else if (TREE_CODE (arg1) == REAL_CST)
1776         return fold_convert_const_int_from_real (code, type, arg1);
1777       else if (TREE_CODE (arg1) == FIXED_CST)
1778         return fold_convert_const_int_from_fixed (type, arg1);
1779     }
1780   else if (TREE_CODE (type) == REAL_TYPE)
1781     {
1782       if (TREE_CODE (arg1) == INTEGER_CST)
1783         return build_real_from_int_cst (type, arg1);
1784       else if (TREE_CODE (arg1) == REAL_CST)
1785         return fold_convert_const_real_from_real (type, arg1);
1786       else if (TREE_CODE (arg1) == FIXED_CST)
1787         return fold_convert_const_real_from_fixed (type, arg1);
1788     }
1789   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
1790     {
1791       if (TREE_CODE (arg1) == FIXED_CST)
1792         return fold_convert_const_fixed_from_fixed (type, arg1);
1793       else if (TREE_CODE (arg1) == INTEGER_CST)
1794         return fold_convert_const_fixed_from_int (type, arg1);
1795       else if (TREE_CODE (arg1) == REAL_CST)
1796         return fold_convert_const_fixed_from_real (type, arg1);
1797     }
1798   return NULL_TREE;
1799 }
1800
1801 /* Construct a vector of zero elements of vector type TYPE.  */
1802
1803 static tree
1804 build_zero_vector (tree type)
1805 {
1806   tree t;
1807
1808   t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1809   return build_vector_from_val (type, t);
1810 }
1811
1812 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
1813
1814 bool
1815 fold_convertible_p (const_tree type, const_tree arg)
1816 {
1817   tree orig = TREE_TYPE (arg);
1818
1819   if (type == orig)
1820     return true;
1821
1822   if (TREE_CODE (arg) == ERROR_MARK
1823       || TREE_CODE (type) == ERROR_MARK
1824       || TREE_CODE (orig) == ERROR_MARK)
1825     return false;
1826
1827   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1828     return true;
1829
1830   switch (TREE_CODE (type))
1831     {
1832     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1833     case POINTER_TYPE: case REFERENCE_TYPE:
1834     case OFFSET_TYPE:
1835       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1836           || TREE_CODE (orig) == OFFSET_TYPE)
1837         return true;
1838       return (TREE_CODE (orig) == VECTOR_TYPE
1839               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1840
1841     case REAL_TYPE:
1842     case FIXED_POINT_TYPE:
1843     case COMPLEX_TYPE:
1844     case VECTOR_TYPE:
1845     case VOID_TYPE:
1846       return TREE_CODE (type) == TREE_CODE (orig);
1847
1848     default:
1849       return false;
1850     }
1851 }
1852
1853 /* Convert expression ARG to type TYPE.  Used by the middle-end for
1854    simple conversions in preference to calling the front-end's convert.  */
1855
1856 tree
1857 fold_convert_loc (location_t loc, tree type, tree arg)
1858 {
1859   tree orig = TREE_TYPE (arg);
1860   tree tem;
1861
1862   if (type == orig)
1863     return arg;
1864
1865   if (TREE_CODE (arg) == ERROR_MARK
1866       || TREE_CODE (type) == ERROR_MARK
1867       || TREE_CODE (orig) == ERROR_MARK)
1868     return error_mark_node;
1869
1870   switch (TREE_CODE (type))
1871     {
1872     case POINTER_TYPE:
1873     case REFERENCE_TYPE:
1874       /* Handle conversions between pointers to different address spaces.  */
1875       if (POINTER_TYPE_P (orig)
1876           && (TYPE_ADDR_SPACE (TREE_TYPE (type))
1877               != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
1878         return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
1879       /* fall through */
1880
1881     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1882     case OFFSET_TYPE:
1883       if (TREE_CODE (arg) == INTEGER_CST)
1884         {
1885           tem = fold_convert_const (NOP_EXPR, type, arg);
1886           if (tem != NULL_TREE)
1887             return tem;
1888         }
1889       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1890           || TREE_CODE (orig) == OFFSET_TYPE)
1891         return fold_build1_loc (loc, NOP_EXPR, type, arg);
1892       if (TREE_CODE (orig) == COMPLEX_TYPE)
1893         return fold_convert_loc (loc, type,
1894                              fold_build1_loc (loc, REALPART_EXPR,
1895                                           TREE_TYPE (orig), arg));
1896       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
1897                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1898       return fold_build1_loc (loc, NOP_EXPR, type, arg);
1899
1900     case REAL_TYPE:
1901       if (TREE_CODE (arg) == INTEGER_CST)
1902         {
1903           tem = fold_convert_const (FLOAT_EXPR, type, arg);
1904           if (tem != NULL_TREE)
1905             return tem;
1906         }
1907       else if (TREE_CODE (arg) == REAL_CST)
1908         {
1909           tem = fold_convert_const (NOP_EXPR, type, arg);
1910           if (tem != NULL_TREE)
1911             return tem;
1912         }
1913       else if (TREE_CODE (arg) == FIXED_CST)
1914         {
1915           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1916           if (tem != NULL_TREE)
1917             return tem;
1918         }
1919
1920       switch (TREE_CODE (orig))
1921         {
1922         case INTEGER_TYPE:
1923         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1924         case POINTER_TYPE: case REFERENCE_TYPE:
1925           return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
1926
1927         case REAL_TYPE:
1928           return fold_build1_loc (loc, NOP_EXPR, type, arg);
1929
1930         case FIXED_POINT_TYPE:
1931           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1932
1933         case COMPLEX_TYPE:
1934           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1935           return fold_convert_loc (loc, type, tem);
1936
1937         default:
1938           gcc_unreachable ();
1939         }
1940
1941     case FIXED_POINT_TYPE:
1942       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
1943           || TREE_CODE (arg) == REAL_CST)
1944         {
1945           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1946           if (tem != NULL_TREE)
1947             goto fold_convert_exit;
1948         }
1949
1950       switch (TREE_CODE (orig))
1951         {
1952         case FIXED_POINT_TYPE:
1953         case INTEGER_TYPE:
1954         case ENUMERAL_TYPE:
1955         case BOOLEAN_TYPE:
1956         case REAL_TYPE:
1957           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1958
1959         case COMPLEX_TYPE:
1960           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1961           return fold_convert_loc (loc, type, tem);
1962
1963         default:
1964           gcc_unreachable ();
1965         }
1966
1967     case COMPLEX_TYPE:
1968       switch (TREE_CODE (orig))
1969         {
1970         case INTEGER_TYPE:
1971         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1972         case POINTER_TYPE: case REFERENCE_TYPE:
1973         case REAL_TYPE:
1974         case FIXED_POINT_TYPE:
1975           return fold_build2_loc (loc, COMPLEX_EXPR, type,
1976                               fold_convert_loc (loc, TREE_TYPE (type), arg),
1977                               fold_convert_loc (loc, TREE_TYPE (type),
1978                                             integer_zero_node));
1979         case COMPLEX_TYPE:
1980           {
1981             tree rpart, ipart;
1982
1983             if (TREE_CODE (arg) == COMPLEX_EXPR)
1984               {
1985                 rpart = fold_convert_loc (loc, TREE_TYPE (type),
1986                                       TREE_OPERAND (arg, 0));
1987                 ipart = fold_convert_loc (loc, TREE_TYPE (type),
1988                                       TREE_OPERAND (arg, 1));
1989                 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
1990               }
1991
1992             arg = save_expr (arg);
1993             rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1994             ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
1995             rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
1996             ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
1997             return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
1998           }
1999
2000         default:
2001           gcc_unreachable ();
2002         }
2003
2004     case VECTOR_TYPE:
2005       if (integer_zerop (arg))
2006         return build_zero_vector (type);
2007       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2008       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2009                   || TREE_CODE (orig) == VECTOR_TYPE);
2010       return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2011
2012     case VOID_TYPE:
2013       tem = fold_ignored_result (arg);
2014       return fold_build1_loc (loc, NOP_EXPR, type, tem);
2015
2016     default:
2017       if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2018         return fold_build1_loc (loc, NOP_EXPR, type, arg);
2019       gcc_unreachable ();
2020     }
2021  fold_convert_exit:
2022   protected_set_expr_location_unshare (tem, loc);
2023   return tem;
2024 }
2025 \f
2026 /* Return false if expr can be assumed not to be an lvalue, true
2027    otherwise.  */
2028
2029 static bool
2030 maybe_lvalue_p (const_tree x)
2031 {
2032   /* We only need to wrap lvalue tree codes.  */
2033   switch (TREE_CODE (x))
2034   {
2035   case VAR_DECL:
2036   case PARM_DECL:
2037   case RESULT_DECL:
2038   case LABEL_DECL:
2039   case FUNCTION_DECL:
2040   case SSA_NAME:
2041
2042   case COMPONENT_REF:
2043   case MEM_REF:
2044   case INDIRECT_REF:
2045   case ARRAY_REF:
2046   case ARRAY_RANGE_REF:
2047   case BIT_FIELD_REF:
2048   case OBJ_TYPE_REF:
2049
2050   case REALPART_EXPR:
2051   case IMAGPART_EXPR:
2052   case PREINCREMENT_EXPR:
2053   case PREDECREMENT_EXPR:
2054   case SAVE_EXPR:
2055   case TRY_CATCH_EXPR:
2056   case WITH_CLEANUP_EXPR:
2057   case COMPOUND_EXPR:
2058   case MODIFY_EXPR:
2059   case TARGET_EXPR:
2060   case COND_EXPR:
2061   case BIND_EXPR:
2062     break;
2063
2064   default:
2065     /* Assume the worst for front-end tree codes.  */
2066     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2067       break;
2068     return false;
2069   }
2070
2071   return true;
2072 }
2073
2074 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2075
2076 tree
2077 non_lvalue_loc (location_t loc, tree x)
2078 {
2079   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2080      us.  */
2081   if (in_gimple_form)
2082     return x;
2083
2084   if (! maybe_lvalue_p (x))
2085     return x;
2086   return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2087 }
2088
2089 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2090    Zero means allow extended lvalues.  */
2091
2092 int pedantic_lvalues;
2093
2094 /* When pedantic, return an expr equal to X but certainly not valid as a
2095    pedantic lvalue.  Otherwise, return X.  */
2096
2097 static tree
2098 pedantic_non_lvalue_loc (location_t loc, tree x)
2099 {
2100   if (pedantic_lvalues)
2101     return non_lvalue_loc (loc, x);
2102
2103   return protected_set_expr_location_unshare (x, loc);
2104 }
2105 \f
2106 /* Given a tree comparison code, return the code that is the logical inverse
2107    of the given code.  It is not safe to do this for floating-point
2108    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2109    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2110
2111 enum tree_code
2112 invert_tree_comparison (enum tree_code code, bool honor_nans)
2113 {
2114   if (honor_nans && flag_trapping_math)
2115     return ERROR_MARK;
2116
2117   switch (code)
2118     {
2119     case EQ_EXPR:
2120       return NE_EXPR;
2121     case NE_EXPR:
2122       return EQ_EXPR;
2123     case GT_EXPR:
2124       return honor_nans ? UNLE_EXPR : LE_EXPR;
2125     case GE_EXPR:
2126       return honor_nans ? UNLT_EXPR : LT_EXPR;
2127     case LT_EXPR:
2128       return honor_nans ? UNGE_EXPR : GE_EXPR;
2129     case LE_EXPR:
2130       return honor_nans ? UNGT_EXPR : GT_EXPR;
2131     case LTGT_EXPR:
2132       return UNEQ_EXPR;
2133     case UNEQ_EXPR:
2134       return LTGT_EXPR;
2135     case UNGT_EXPR:
2136       return LE_EXPR;
2137     case UNGE_EXPR:
2138       return LT_EXPR;
2139     case UNLT_EXPR:
2140       return GE_EXPR;
2141     case UNLE_EXPR:
2142       return GT_EXPR;
2143     case ORDERED_EXPR:
2144       return UNORDERED_EXPR;
2145     case UNORDERED_EXPR:
2146       return ORDERED_EXPR;
2147     default:
2148       gcc_unreachable ();
2149     }
2150 }
2151
2152 /* Similar, but return the comparison that results if the operands are
2153    swapped.  This is safe for floating-point.  */
2154
2155 enum tree_code
2156 swap_tree_comparison (enum tree_code code)
2157 {
2158   switch (code)
2159     {
2160     case EQ_EXPR:
2161     case NE_EXPR:
2162     case ORDERED_EXPR:
2163     case UNORDERED_EXPR:
2164     case LTGT_EXPR:
2165     case UNEQ_EXPR:
2166       return code;
2167     case GT_EXPR:
2168       return LT_EXPR;
2169     case GE_EXPR:
2170       return LE_EXPR;
2171     case LT_EXPR:
2172       return GT_EXPR;
2173     case LE_EXPR:
2174       return GE_EXPR;
2175     case UNGT_EXPR:
2176       return UNLT_EXPR;
2177     case UNGE_EXPR:
2178       return UNLE_EXPR;
2179     case UNLT_EXPR:
2180       return UNGT_EXPR;
2181     case UNLE_EXPR:
2182       return UNGE_EXPR;
2183     default:
2184       gcc_unreachable ();
2185     }
2186 }
2187
2188
2189 /* Convert a comparison tree code from an enum tree_code representation
2190    into a compcode bit-based encoding.  This function is the inverse of
2191    compcode_to_comparison.  */
2192
2193 static enum comparison_code
2194 comparison_to_compcode (enum tree_code code)
2195 {
2196   switch (code)
2197     {
2198     case LT_EXPR:
2199       return COMPCODE_LT;
2200     case EQ_EXPR:
2201       return COMPCODE_EQ;
2202     case LE_EXPR:
2203       return COMPCODE_LE;
2204     case GT_EXPR:
2205       return COMPCODE_GT;
2206     case NE_EXPR:
2207       return COMPCODE_NE;
2208     case GE_EXPR:
2209       return COMPCODE_GE;
2210     case ORDERED_EXPR:
2211       return COMPCODE_ORD;
2212     case UNORDERED_EXPR:
2213       return COMPCODE_UNORD;
2214     case UNLT_EXPR:
2215       return COMPCODE_UNLT;
2216     case UNEQ_EXPR:
2217       return COMPCODE_UNEQ;
2218     case UNLE_EXPR:
2219       return COMPCODE_UNLE;
2220     case UNGT_EXPR:
2221       return COMPCODE_UNGT;
2222     case LTGT_EXPR:
2223       return COMPCODE_LTGT;
2224     case UNGE_EXPR:
2225       return COMPCODE_UNGE;
2226     default:
2227       gcc_unreachable ();
2228     }
2229 }
2230
2231 /* Convert a compcode bit-based encoding of a comparison operator back
2232    to GCC's enum tree_code representation.  This function is the
2233    inverse of comparison_to_compcode.  */
2234
2235 static enum tree_code
2236 compcode_to_comparison (enum comparison_code code)
2237 {
2238   switch (code)
2239     {
2240     case COMPCODE_LT:
2241       return LT_EXPR;
2242     case COMPCODE_EQ:
2243       return EQ_EXPR;
2244     case COMPCODE_LE:
2245       return LE_EXPR;
2246     case COMPCODE_GT:
2247       return GT_EXPR;
2248     case COMPCODE_NE:
2249       return NE_EXPR;
2250     case COMPCODE_GE:
2251       return GE_EXPR;
2252     case COMPCODE_ORD:
2253       return ORDERED_EXPR;
2254     case COMPCODE_UNORD:
2255       return UNORDERED_EXPR;
2256     case COMPCODE_UNLT:
2257       return UNLT_EXPR;
2258     case COMPCODE_UNEQ:
2259       return UNEQ_EXPR;
2260     case COMPCODE_UNLE:
2261       return UNLE_EXPR;
2262     case COMPCODE_UNGT:
2263       return UNGT_EXPR;
2264     case COMPCODE_LTGT:
2265       return LTGT_EXPR;
2266     case COMPCODE_UNGE:
2267       return UNGE_EXPR;
2268     default:
2269       gcc_unreachable ();
2270     }
2271 }
2272
2273 /* Return a tree for the comparison which is the combination of
2274    doing the AND or OR (depending on CODE) of the two operations LCODE
2275    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2276    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2277    if this makes the transformation invalid.  */
2278
2279 tree
2280 combine_comparisons (location_t loc,
2281                      enum tree_code code, enum tree_code lcode,
2282                      enum tree_code rcode, tree truth_type,
2283                      tree ll_arg, tree lr_arg)
2284 {
2285   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2286   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2287   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2288   int compcode;
2289
2290   switch (code)
2291     {
2292     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2293       compcode = lcompcode & rcompcode;
2294       break;
2295
2296     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2297       compcode = lcompcode | rcompcode;
2298       break;
2299
2300     default:
2301       return NULL_TREE;
2302     }
2303
2304   if (!honor_nans)
2305     {
2306       /* Eliminate unordered comparisons, as well as LTGT and ORD
2307          which are not used unless the mode has NaNs.  */
2308       compcode &= ~COMPCODE_UNORD;
2309       if (compcode == COMPCODE_LTGT)
2310         compcode = COMPCODE_NE;
2311       else if (compcode == COMPCODE_ORD)
2312         compcode = COMPCODE_TRUE;
2313     }
2314    else if (flag_trapping_math)
2315      {
2316         /* Check that the original operation and the optimized ones will trap
2317            under the same condition.  */
2318         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2319                      && (lcompcode != COMPCODE_EQ)
2320                      && (lcompcode != COMPCODE_ORD);
2321         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2322                      && (rcompcode != COMPCODE_EQ)
2323                      && (rcompcode != COMPCODE_ORD);
2324         bool trap = (compcode & COMPCODE_UNORD) == 0
2325                     && (compcode != COMPCODE_EQ)
2326                     && (compcode != COMPCODE_ORD);
2327
2328         /* In a short-circuited boolean expression the LHS might be
2329            such that the RHS, if evaluated, will never trap.  For
2330            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2331            if neither x nor y is NaN.  (This is a mixed blessing: for
2332            example, the expression above will never trap, hence
2333            optimizing it to x < y would be invalid).  */
2334         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2335             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2336           rtrap = false;
2337
2338         /* If the comparison was short-circuited, and only the RHS
2339            trapped, we may now generate a spurious trap.  */
2340         if (rtrap && !ltrap
2341             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2342           return NULL_TREE;
2343
2344         /* If we changed the conditions that cause a trap, we lose.  */
2345         if ((ltrap || rtrap) != trap)
2346           return NULL_TREE;
2347       }
2348
2349   if (compcode == COMPCODE_TRUE)
2350     return constant_boolean_node (true, truth_type);
2351   else if (compcode == COMPCODE_FALSE)
2352     return constant_boolean_node (false, truth_type);
2353   else
2354     {
2355       enum tree_code tcode;
2356
2357       tcode = compcode_to_comparison ((enum comparison_code) compcode);
2358       return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2359     }
2360 }
2361 \f
2362 /* Return nonzero if two operands (typically of the same tree node)
2363    are necessarily equal.  If either argument has side-effects this
2364    function returns zero.  FLAGS modifies behavior as follows:
2365
2366    If OEP_ONLY_CONST is set, only return nonzero for constants.
2367    This function tests whether the operands are indistinguishable;
2368    it does not test whether they are equal using C's == operation.
2369    The distinction is important for IEEE floating point, because
2370    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2371    (2) two NaNs may be indistinguishable, but NaN!=NaN.
2372
2373    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2374    even though it may hold multiple values during a function.
2375    This is because a GCC tree node guarantees that nothing else is
2376    executed between the evaluation of its "operands" (which may often
2377    be evaluated in arbitrary order).  Hence if the operands themselves
2378    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2379    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
2380    unset means assuming isochronic (or instantaneous) tree equivalence.
2381    Unless comparing arbitrary expression trees, such as from different
2382    statements, this flag can usually be left unset.
2383
2384    If OEP_PURE_SAME is set, then pure functions with identical arguments
2385    are considered the same.  It is used when the caller has other ways
2386    to ensure that global memory is unchanged in between.  */
2387
2388 int
2389 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2390 {
2391   /* If either is ERROR_MARK, they aren't equal.  */
2392   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2393       || TREE_TYPE (arg0) == error_mark_node
2394       || TREE_TYPE (arg1) == error_mark_node)
2395     return 0;
2396
2397   /* Similar, if either does not have a type (like a released SSA name), 
2398      they aren't equal.  */
2399   if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2400     return 0;
2401
2402   /* Check equality of integer constants before bailing out due to
2403      precision differences.  */
2404   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2405     return tree_int_cst_equal (arg0, arg1);
2406
2407   /* If both types don't have the same signedness, then we can't consider
2408      them equal.  We must check this before the STRIP_NOPS calls
2409      because they may change the signedness of the arguments.  As pointers
2410      strictly don't have a signedness, require either two pointers or
2411      two non-pointers as well.  */
2412   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2413       || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2414     return 0;
2415
2416   /* We cannot consider pointers to different address space equal.  */
2417   if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2418       && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2419           != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2420     return 0;
2421
2422   /* If both types don't have the same precision, then it is not safe
2423      to strip NOPs.  */
2424   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
2425     return 0;
2426
2427   STRIP_NOPS (arg0);
2428   STRIP_NOPS (arg1);
2429
2430   /* In case both args are comparisons but with different comparison
2431      code, try to swap the comparison operands of one arg to produce
2432      a match and compare that variant.  */
2433   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2434       && COMPARISON_CLASS_P (arg0)
2435       && COMPARISON_CLASS_P (arg1))
2436     {
2437       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2438
2439       if (TREE_CODE (arg0) == swap_code)
2440         return operand_equal_p (TREE_OPERAND (arg0, 0),
2441                                 TREE_OPERAND (arg1, 1), flags)
2442                && operand_equal_p (TREE_OPERAND (arg0, 1),
2443                                    TREE_OPERAND (arg1, 0), flags);
2444     }
2445
2446   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2447       /* This is needed for conversions and for COMPONENT_REF.
2448          Might as well play it safe and always test this.  */
2449       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2450       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2451       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2452     return 0;
2453
2454   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2455      We don't care about side effects in that case because the SAVE_EXPR
2456      takes care of that for us. In all other cases, two expressions are
2457      equal if they have no side effects.  If we have two identical
2458      expressions with side effects that should be treated the same due
2459      to the only side effects being identical SAVE_EXPR's, that will
2460      be detected in the recursive calls below.
2461      If we are taking an invariant address of two identical objects
2462      they are necessarily equal as well.  */
2463   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2464       && (TREE_CODE (arg0) == SAVE_EXPR
2465           || (flags & OEP_CONSTANT_ADDRESS_OF)
2466           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2467     return 1;
2468
2469   /* Next handle constant cases, those for which we can return 1 even
2470      if ONLY_CONST is set.  */
2471   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2472     switch (TREE_CODE (arg0))
2473       {
2474       case INTEGER_CST:
2475         return tree_int_cst_equal (arg0, arg1);
2476
2477       case FIXED_CST:
2478         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2479                                        TREE_FIXED_CST (arg1));
2480
2481       case REAL_CST:
2482         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2483                                    TREE_REAL_CST (arg1)))
2484           return 1;
2485
2486
2487         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
2488           {
2489             /* If we do not distinguish between signed and unsigned zero,
2490                consider them equal.  */
2491             if (real_zerop (arg0) && real_zerop (arg1))
2492               return 1;
2493           }
2494         return 0;
2495
2496       case VECTOR_CST:
2497         {
2498           tree v1, v2;
2499
2500           v1 = TREE_VECTOR_CST_ELTS (arg0);
2501           v2 = TREE_VECTOR_CST_ELTS (arg1);
2502           while (v1 && v2)
2503             {
2504               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2505                                     flags))
2506                 return 0;
2507               v1 = TREE_CHAIN (v1);
2508               v2 = TREE_CHAIN (v2);
2509             }
2510
2511           return v1 == v2;
2512         }
2513
2514       case COMPLEX_CST:
2515         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2516                                  flags)
2517                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2518                                     flags));
2519
2520       case STRING_CST:
2521         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2522                 && ! memcmp (TREE_STRING_POINTER (arg0),
2523                               TREE_STRING_POINTER (arg1),
2524                               TREE_STRING_LENGTH (arg0)));
2525
2526       case ADDR_EXPR:
2527         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2528                                 TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1)
2529                                 ? OEP_CONSTANT_ADDRESS_OF : 0);
2530       default:
2531         break;
2532       }
2533
2534   if (flags & OEP_ONLY_CONST)
2535     return 0;
2536
2537 /* Define macros to test an operand from arg0 and arg1 for equality and a
2538    variant that allows null and views null as being different from any
2539    non-null value.  In the latter case, if either is null, the both
2540    must be; otherwise, do the normal comparison.  */
2541 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
2542                                     TREE_OPERAND (arg1, N), flags)
2543
2544 #define OP_SAME_WITH_NULL(N)                            \
2545   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2546    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2547
2548   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2549     {
2550     case tcc_unary:
2551       /* Two conversions are equal only if signedness and modes match.  */
2552       switch (TREE_CODE (arg0))
2553         {
2554         CASE_CONVERT:
2555         case FIX_TRUNC_EXPR:
2556           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2557               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2558             return 0;
2559           break;
2560         default:
2561           break;
2562         }
2563
2564       return OP_SAME (0);
2565
2566
2567     case tcc_comparison:
2568     case tcc_binary:
2569       if (OP_SAME (0) && OP_SAME (1))
2570         return 1;
2571
2572       /* For commutative ops, allow the other order.  */
2573       return (commutative_tree_code (TREE_CODE (arg0))
2574               && operand_equal_p (TREE_OPERAND (arg0, 0),
2575                                   TREE_OPERAND (arg1, 1), flags)
2576               && operand_equal_p (TREE_OPERAND (arg0, 1),
2577                                   TREE_OPERAND (arg1, 0), flags));
2578
2579     case tcc_reference:
2580       /* If either of the pointer (or reference) expressions we are
2581          dereferencing contain a side effect, these cannot be equal.  */
2582       if (TREE_SIDE_EFFECTS (arg0)
2583           || TREE_SIDE_EFFECTS (arg1))
2584         return 0;
2585
2586       switch (TREE_CODE (arg0))
2587         {
2588         case INDIRECT_REF:
2589         case REALPART_EXPR:
2590         case IMAGPART_EXPR:
2591           return OP_SAME (0);
2592
2593         case MEM_REF:
2594           /* Require equal access sizes, and similar pointer types.
2595              We can have incomplete types for array references of
2596              variable-sized arrays from the Fortran frontent
2597              though.  */
2598           return ((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1))
2599                    || (TYPE_SIZE (TREE_TYPE (arg0))
2600                        && TYPE_SIZE (TREE_TYPE (arg1))
2601                        && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
2602                                            TYPE_SIZE (TREE_TYPE (arg1)), flags)))
2603                   && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg0, 1)))
2604                       == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg1, 1))))
2605                   && OP_SAME (0) && OP_SAME (1));
2606
2607         case ARRAY_REF:
2608         case ARRAY_RANGE_REF:
2609           /* Operands 2 and 3 may be null.
2610              Compare the array index by value if it is constant first as we
2611              may have different types but same value here.  */
2612           return (OP_SAME (0)
2613                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2614                                           TREE_OPERAND (arg1, 1))
2615                       || OP_SAME (1))
2616                   && OP_SAME_WITH_NULL (2)
2617                   && OP_SAME_WITH_NULL (3));
2618
2619         case COMPONENT_REF:
2620           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
2621              may be NULL when we're called to compare MEM_EXPRs.  */
2622           return OP_SAME_WITH_NULL (0)
2623                  && OP_SAME (1)
2624                  && OP_SAME_WITH_NULL (2);
2625
2626         case BIT_FIELD_REF:
2627           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2628
2629         default:
2630           return 0;
2631         }
2632
2633     case tcc_expression:
2634       switch (TREE_CODE (arg0))
2635         {
2636         case ADDR_EXPR:
2637         case TRUTH_NOT_EXPR:
2638           return OP_SAME (0);
2639
2640         case TRUTH_ANDIF_EXPR:
2641         case TRUTH_ORIF_EXPR:
2642           return OP_SAME (0) && OP_SAME (1);
2643
2644         case FMA_EXPR:
2645         case WIDEN_MULT_PLUS_EXPR:
2646         case WIDEN_MULT_MINUS_EXPR:
2647           if (!OP_SAME (2))
2648             return 0;
2649           /* The multiplcation operands are commutative.  */
2650           /* FALLTHRU */
2651
2652         case TRUTH_AND_EXPR:
2653         case TRUTH_OR_EXPR:
2654         case TRUTH_XOR_EXPR:
2655           if (OP_SAME (0) && OP_SAME (1))
2656             return 1;
2657
2658           /* Otherwise take into account this is a commutative operation.  */
2659           return (operand_equal_p (TREE_OPERAND (arg0, 0),
2660                                    TREE_OPERAND (arg1, 1), flags)
2661                   && operand_equal_p (TREE_OPERAND (arg0, 1),
2662                                       TREE_OPERAND (arg1, 0), flags));
2663
2664         case COND_EXPR:
2665         case VEC_COND_EXPR:
2666         case DOT_PROD_EXPR:
2667           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2668
2669         default:
2670           return 0;
2671         }
2672
2673     case tcc_vl_exp:
2674       switch (TREE_CODE (arg0))
2675         {
2676         case CALL_EXPR:
2677           /* If the CALL_EXPRs call different functions, then they
2678              clearly can not be equal.  */
2679           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
2680                                  flags))
2681             return 0;
2682
2683           {
2684             unsigned int cef = call_expr_flags (arg0);
2685             if (flags & OEP_PURE_SAME)
2686               cef &= ECF_CONST | ECF_PURE;
2687             else
2688               cef &= ECF_CONST;
2689             if (!cef)
2690               return 0;
2691           }
2692
2693           /* Now see if all the arguments are the same.  */
2694           {
2695             const_call_expr_arg_iterator iter0, iter1;
2696             const_tree a0, a1;
2697             for (a0 = first_const_call_expr_arg (arg0, &iter0),
2698                    a1 = first_const_call_expr_arg (arg1, &iter1);
2699                  a0 && a1;
2700                  a0 = next_const_call_expr_arg (&iter0),
2701                    a1 = next_const_call_expr_arg (&iter1))
2702               if (! operand_equal_p (a0, a1, flags))
2703                 return 0;
2704
2705             /* If we get here and both argument lists are exhausted
2706                then the CALL_EXPRs are equal.  */
2707             return ! (a0 || a1);
2708           }
2709         default:
2710           return 0;
2711         }
2712
2713     case tcc_declaration:
2714       /* Consider __builtin_sqrt equal to sqrt.  */
2715       return (TREE_CODE (arg0) == FUNCTION_DECL
2716               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2717               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2718               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2719
2720     default:
2721       return 0;
2722     }
2723
2724 #undef OP_SAME
2725 #undef OP_SAME_WITH_NULL
2726 }
2727 \f
2728 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2729    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2730
2731    When in doubt, return 0.  */
2732
2733 static int
2734 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2735 {
2736   int unsignedp1, unsignedpo;
2737   tree primarg0, primarg1, primother;
2738   unsigned int correct_width;
2739
2740   if (operand_equal_p (arg0, arg1, 0))
2741     return 1;
2742
2743   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2744       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2745     return 0;
2746
2747   /* Discard any conversions that don't change the modes of ARG0 and ARG1
2748      and see if the inner values are the same.  This removes any
2749      signedness comparison, which doesn't matter here.  */
2750   primarg0 = arg0, primarg1 = arg1;
2751   STRIP_NOPS (primarg0);
2752   STRIP_NOPS (primarg1);
2753   if (operand_equal_p (primarg0, primarg1, 0))
2754     return 1;
2755
2756   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2757      actual comparison operand, ARG0.
2758
2759      First throw away any conversions to wider types
2760      already present in the operands.  */
2761
2762   primarg1 = get_narrower (arg1, &unsignedp1);
2763   primother = get_narrower (other, &unsignedpo);
2764
2765   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2766   if (unsignedp1 == unsignedpo
2767       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2768       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2769     {
2770       tree type = TREE_TYPE (arg0);
2771
2772       /* Make sure shorter operand is extended the right way
2773          to match the longer operand.  */
2774       primarg1 = fold_convert (signed_or_unsigned_type_for
2775                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2776
2777       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2778         return 1;
2779     }
2780
2781   return 0;
2782 }
2783 \f
2784 /* See if ARG is an expression that is either a comparison or is performing
2785    arithmetic on comparisons.  The comparisons must only be comparing
2786    two different values, which will be stored in *CVAL1 and *CVAL2; if
2787    they are nonzero it means that some operands have already been found.
2788    No variables may be used anywhere else in the expression except in the
2789    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
2790    the expression and save_expr needs to be called with CVAL1 and CVAL2.
2791
2792    If this is true, return 1.  Otherwise, return zero.  */
2793
2794 static int
2795 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2796 {
2797   enum tree_code code = TREE_CODE (arg);
2798   enum tree_code_class tclass = TREE_CODE_CLASS (code);
2799
2800   /* We can handle some of the tcc_expression cases here.  */
2801   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2802     tclass = tcc_unary;
2803   else if (tclass == tcc_expression
2804            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2805                || code == COMPOUND_EXPR))
2806     tclass = tcc_binary;
2807
2808   else if (tclass == tcc_expression && code == SAVE_EXPR
2809            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2810     {
2811       /* If we've already found a CVAL1 or CVAL2, this expression is
2812          two complex to handle.  */
2813       if (*cval1 || *cval2)
2814         return 0;
2815
2816       tclass = tcc_unary;
2817       *save_p = 1;
2818     }
2819
2820   switch (tclass)
2821     {
2822     case tcc_unary:
2823       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2824
2825     case tcc_binary:
2826       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2827               && twoval_comparison_p (TREE_OPERAND (arg, 1),
2828                                       cval1, cval2, save_p));
2829
2830     case tcc_constant:
2831       return 1;
2832
2833     case tcc_expression:
2834       if (code == COND_EXPR)
2835         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2836                                      cval1, cval2, save_p)
2837                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2838                                         cval1, cval2, save_p)
2839                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2840                                         cval1, cval2, save_p));
2841       return 0;
2842
2843     case tcc_comparison:
2844       /* First see if we can handle the first operand, then the second.  For
2845          the second operand, we know *CVAL1 can't be zero.  It must be that
2846          one side of the comparison is each of the values; test for the
2847          case where this isn't true by failing if the two operands
2848          are the same.  */
2849
2850       if (operand_equal_p (TREE_OPERAND (arg, 0),
2851                            TREE_OPERAND (arg, 1), 0))
2852         return 0;
2853
2854       if (*cval1 == 0)
2855         *cval1 = TREE_OPERAND (arg, 0);
2856       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2857         ;
2858       else if (*cval2 == 0)
2859         *cval2 = TREE_OPERAND (arg, 0);
2860       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2861         ;
2862       else
2863         return 0;
2864
2865       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2866         ;
2867       else if (*cval2 == 0)
2868         *cval2 = TREE_OPERAND (arg, 1);
2869       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2870         ;
2871       else
2872         return 0;
2873
2874       return 1;
2875
2876     default:
2877       return 0;
2878     }
2879 }
2880 \f
2881 /* ARG is a tree that is known to contain just arithmetic operations and
2882    comparisons.  Evaluate the operations in the tree substituting NEW0 for
2883    any occurrence of OLD0 as an operand of a comparison and likewise for
2884    NEW1 and OLD1.  */
2885
2886 static tree
2887 eval_subst (location_t loc, tree arg, tree old0, tree new0,
2888             tree old1, tree new1)
2889 {
2890   tree type = TREE_TYPE (arg);
2891   enum tree_code code = TREE_CODE (arg);
2892   enum tree_code_class tclass = TREE_CODE_CLASS (code);
2893
2894   /* We can handle some of the tcc_expression cases here.  */
2895   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2896     tclass = tcc_unary;
2897   else if (tclass == tcc_expression
2898            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2899     tclass = tcc_binary;
2900
2901   switch (tclass)
2902     {
2903     case tcc_unary:
2904       return fold_build1_loc (loc, code, type,
2905                           eval_subst (loc, TREE_OPERAND (arg, 0),
2906                                       old0, new0, old1, new1));
2907
2908     case tcc_binary:
2909       return fold_build2_loc (loc, code, type,
2910                           eval_subst (loc, TREE_OPERAND (arg, 0),
2911                                       old0, new0, old1, new1),
2912                           eval_subst (loc, TREE_OPERAND (arg, 1),
2913                                       old0, new0, old1, new1));
2914
2915     case tcc_expression:
2916       switch (code)
2917         {
2918         case SAVE_EXPR:
2919           return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
2920                              old1, new1);
2921
2922         case COMPOUND_EXPR:
2923           return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
2924                              old1, new1);
2925
2926         case COND_EXPR:
2927           return fold_build3_loc (loc, code, type,
2928                               eval_subst (loc, TREE_OPERAND (arg, 0),
2929                                           old0, new0, old1, new1),
2930                               eval_subst (loc, TREE_OPERAND (arg, 1),
2931                                           old0, new0, old1, new1),
2932                               eval_subst (loc, TREE_OPERAND (arg, 2),
2933                                           old0, new0, old1, new1));
2934         default:
2935           break;
2936         }
2937       /* Fall through - ???  */
2938
2939     case tcc_comparison:
2940       {
2941         tree arg0 = TREE_OPERAND (arg, 0);
2942         tree arg1 = TREE_OPERAND (arg, 1);
2943
2944         /* We need to check both for exact equality and tree equality.  The
2945            former will be true if the operand has a side-effect.  In that
2946            case, we know the operand occurred exactly once.  */
2947
2948         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2949           arg0 = new0;
2950         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2951           arg0 = new1;
2952
2953         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2954           arg1 = new0;
2955         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2956           arg1 = new1;
2957
2958         return fold_build2_loc (loc, code, type, arg0, arg1);
2959       }
2960
2961     default:
2962       return arg;
2963     }
2964 }
2965 \f
2966 /* Return a tree for the case when the result of an expression is RESULT
2967    converted to TYPE and OMITTED was previously an operand of the expression
2968    but is now not needed (e.g., we folded OMITTED * 0).
2969
2970    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
2971    the conversion of RESULT to TYPE.  */
2972
2973 tree
2974 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
2975 {
2976   tree t = fold_convert_loc (loc, type, result);
2977
2978   /* If the resulting operand is an empty statement, just return the omitted
2979      statement casted to void. */
2980   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
2981     return build1_loc (loc, NOP_EXPR, void_type_node,
2982                        fold_ignored_result (omitted));
2983
2984   if (TREE_SIDE_EFFECTS (omitted))
2985     return build2_loc (loc, COMPOUND_EXPR, type,
2986                        fold_ignored_result (omitted), t);
2987
2988   return non_lvalue_loc (loc, t);
2989 }
2990
2991 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
2992
2993 static tree
2994 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
2995                                tree omitted)
2996 {
2997   tree t = fold_convert_loc (loc, type, result);
2998
2999   /* If the resulting operand is an empty statement, just return the omitted
3000      statement casted to void. */
3001   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3002     return build1_loc (loc, NOP_EXPR, void_type_node,
3003                        fold_ignored_result (omitted));
3004
3005   if (TREE_SIDE_EFFECTS (omitted))
3006     return build2_loc (loc, COMPOUND_EXPR, type,
3007                        fold_ignored_result (omitted), t);
3008
3009   return pedantic_non_lvalue_loc (loc, t);
3010 }
3011
3012 /* Return a tree for the case when the result of an expression is RESULT
3013    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3014    of the expression but are now not needed.
3015
3016    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3017    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3018    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3019    just do the conversion of RESULT to TYPE.  */
3020
3021 tree
3022 omit_two_operands_loc (location_t loc, tree type, tree result,
3023                        tree omitted1, tree omitted2)
3024 {
3025   tree t = fold_convert_loc (loc, type, result);
3026
3027   if (TREE_SIDE_EFFECTS (omitted2))
3028     t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3029   if (TREE_SIDE_EFFECTS (omitted1))
3030     t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3031
3032   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3033 }
3034
3035 \f
3036 /* Return a simplified tree node for the truth-negation of ARG.  This
3037    never alters ARG itself.  We assume that ARG is an operation that
3038    returns a truth value (0 or 1).
3039
3040    FIXME: one would think we would fold the result, but it causes
3041    problems with the dominator optimizer.  */
3042
3043 tree
3044 fold_truth_not_expr (location_t loc, tree arg)
3045 {
3046   tree type = TREE_TYPE (arg);
3047   enum tree_code code = TREE_CODE (arg);
3048   location_t loc1, loc2;
3049
3050   /* If this is a comparison, we can simply invert it, except for
3051      floating-point non-equality comparisons, in which case we just
3052      enclose a TRUTH_NOT_EXPR around what we have.  */
3053
3054   if (TREE_CODE_CLASS (code) == tcc_comparison)
3055     {
3056       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3057       if (FLOAT_TYPE_P (op_type)
3058           && flag_trapping_math
3059           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3060           && code != NE_EXPR && code != EQ_EXPR)
3061         return NULL_TREE;
3062
3063       code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3064       if (code == ERROR_MARK)
3065         return NULL_TREE;
3066
3067       return build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3068                          TREE_OPERAND (arg, 1));
3069     }
3070
3071   switch (code)
3072     {
3073     case INTEGER_CST:
3074       return constant_boolean_node (integer_zerop (arg), type);
3075
3076     case TRUTH_AND_EXPR:
3077       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3078       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3079       return build2_loc (loc, TRUTH_OR_EXPR, type,
3080                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3081                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3082
3083     case TRUTH_OR_EXPR:
3084       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3085       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3086       return build2_loc (loc, TRUTH_AND_EXPR, type,
3087                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3088                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3089
3090     case TRUTH_XOR_EXPR:
3091       /* Here we can invert either operand.  We invert the first operand
3092          unless the second operand is a TRUTH_NOT_EXPR in which case our
3093          result is the XOR of the first operand with the inside of the
3094          negation of the second operand.  */
3095
3096       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3097         return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3098                            TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3099       else
3100         return build2_loc (loc, TRUTH_XOR_EXPR, type,
3101                            invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3102                            TREE_OPERAND (arg, 1));
3103
3104     case TRUTH_ANDIF_EXPR:
3105       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3106       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3107       return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3108                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3109                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3110
3111     case TRUTH_ORIF_EXPR:
3112       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3113       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3114       return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3115                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3116                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3117
3118     case TRUTH_NOT_EXPR:
3119       return TREE_OPERAND (arg, 0);
3120
3121     case COND_EXPR:
3122       {
3123         tree arg1 = TREE_OPERAND (arg, 1);
3124         tree arg2 = TREE_OPERAND (arg, 2);
3125
3126         loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3127         loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3128
3129         /* A COND_EXPR may have a throw as one operand, which
3130            then has void type.  Just leave void operands
3131            as they are.  */
3132         return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3133                            VOID_TYPE_P (TREE_TYPE (arg1))
3134                            ? arg1 : invert_truthvalue_loc (loc1, arg1),
3135                            VOID_TYPE_P (TREE_TYPE (arg2))
3136                            ? arg2 : invert_truthvalue_loc (loc2, arg2));
3137       }
3138
3139     case COMPOUND_EXPR:
3140       loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3141       return build2_loc (loc, COMPOUND_EXPR, type,
3142                          TREE_OPERAND (arg, 0),
3143                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3144
3145     case NON_LVALUE_EXPR:
3146       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3147       return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3148
3149     CASE_CONVERT:
3150       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3151         return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3152
3153       /* ... fall through ...  */
3154
3155     case FLOAT_EXPR:
3156       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3157       return build1_loc (loc, TREE_CODE (arg), type,
3158                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3159
3160     case BIT_AND_EXPR:
3161       if (!integer_onep (TREE_OPERAND (arg, 1)))
3162         return NULL_TREE;
3163       return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3164
3165     case SAVE_EXPR:
3166       return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3167
3168     case CLEANUP_POINT_EXPR:
3169       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3170       return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3171                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3172
3173     default:
3174       return NULL_TREE;
3175     }
3176 }
3177
3178 /* Return a simplified tree node for the truth-negation of ARG.  This
3179    never alters ARG itself.  We assume that ARG is an operation that
3180    returns a truth value (0 or 1).
3181
3182    FIXME: one would think we would fold the result, but it causes
3183    problems with the dominator optimizer.  */
3184
3185 tree
3186 invert_truthvalue_loc (location_t loc, tree arg)
3187 {
3188   tree tem;
3189
3190   if (TREE_CODE (arg) == ERROR_MARK)
3191     return arg;
3192
3193   tem = fold_truth_not_expr (loc, arg);
3194   if (!tem)
3195     tem = build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3196
3197   return tem;
3198 }
3199
3200 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3201    operands are another bit-wise operation with a common input.  If so,
3202    distribute the bit operations to save an operation and possibly two if
3203    constants are involved.  For example, convert
3204         (A | B) & (A | C) into A | (B & C)
3205    Further simplification will occur if B and C are constants.
3206
3207    If this optimization cannot be done, 0 will be returned.  */
3208
3209 static tree
3210 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
3211                      tree arg0, tree arg1)
3212 {
3213   tree common;
3214   tree left, right;
3215
3216   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3217       || TREE_CODE (arg0) == code
3218       || (TREE_CODE (arg0) != BIT_AND_EXPR
3219           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3220     return 0;
3221
3222   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3223     {
3224       common = TREE_OPERAND (arg0, 0);
3225       left = TREE_OPERAND (arg0, 1);
3226       right = TREE_OPERAND (arg1, 1);
3227     }
3228   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3229     {
3230       common = TREE_OPERAND (arg0, 0);
3231       left = TREE_OPERAND (arg0, 1);
3232       right = TREE_OPERAND (arg1, 0);
3233     }
3234   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3235     {
3236       common = TREE_OPERAND (arg0, 1);
3237       left = TREE_OPERAND (arg0, 0);
3238       right = TREE_OPERAND (arg1, 1);
3239     }
3240   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3241     {
3242       common = TREE_OPERAND (arg0, 1);
3243       left = TREE_OPERAND (arg0, 0);
3244       right = TREE_OPERAND (arg1, 0);
3245     }
3246   else
3247     return 0;
3248
3249   common = fold_convert_loc (loc, type, common);
3250   left = fold_convert_loc (loc, type, left);
3251   right = fold_convert_loc (loc, type, right);
3252   return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
3253                       fold_build2_loc (loc, code, type, left, right));
3254 }
3255
3256 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3257    with code CODE.  This optimization is unsafe.  */
3258 static tree
3259 distribute_real_division (location_t loc, enum tree_code code, tree type,
3260                           tree arg0, tree arg1)
3261 {
3262   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3263   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3264
3265   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3266   if (mul0 == mul1
3267       && operand_equal_p (TREE_OPERAND (arg0, 1),
3268                        TREE_OPERAND (arg1, 1), 0))
3269     return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3270                         fold_build2_loc (loc, code, type,
3271                                      TREE_OPERAND (arg0, 0),
3272                                      TREE_OPERAND (arg1, 0)),
3273                         TREE_OPERAND (arg0, 1));
3274
3275   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3276   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3277                        TREE_OPERAND (arg1, 0), 0)
3278       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3279       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3280     {
3281       REAL_VALUE_TYPE r0, r1;
3282       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3283       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3284       if (!mul0)
3285         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3286       if (!mul1)
3287         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3288       real_arithmetic (&r0, code, &r0, &r1);
3289       return fold_build2_loc (loc, MULT_EXPR, type,
3290                           TREE_OPERAND (arg0, 0),
3291                           build_real (type, r0));
3292     }
3293
3294   return NULL_TREE;
3295 }
3296 \f
3297 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3298    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3299
3300 static tree
3301 make_bit_field_ref (location_t loc, tree inner, tree type,
3302                     HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3303 {
3304   tree result, bftype;
3305
3306   if (bitpos == 0)
3307     {
3308       tree size = TYPE_SIZE (TREE_TYPE (inner));
3309       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3310            || POINTER_TYPE_P (TREE_TYPE (inner)))
3311           && host_integerp (size, 0)
3312           && tree_low_cst (size, 0) == bitsize)
3313         return fold_convert_loc (loc, type, inner);
3314     }
3315
3316   bftype = type;
3317   if (TYPE_PRECISION (bftype) != bitsize
3318       || TYPE_UNSIGNED (bftype) == !unsignedp)
3319     bftype = build_nonstandard_integer_type (bitsize, 0);
3320
3321   result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3322                        size_int (bitsize), bitsize_int (bitpos));
3323
3324   if (bftype != type)
3325     result = fold_convert_loc (loc, type, result);
3326
3327   return result;
3328 }
3329
3330 /* Optimize a bit-field compare.
3331
3332    There are two cases:  First is a compare against a constant and the
3333    second is a comparison of two items where the fields are at the same
3334    bit position relative to the start of a chunk (byte, halfword, word)
3335    large enough to contain it.  In these cases we can avoid the shift
3336    implicit in bitfield extractions.
3337
3338    For constants, we emit a compare of the shifted constant with the
3339    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3340    compared.  For two fields at the same position, we do the ANDs with the
3341    similar mask and compare the result of the ANDs.
3342
3343    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3344    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3345    are the left and right operands of the comparison, respectively.
3346
3347    If the optimization described above can be done, we return the resulting
3348    tree.  Otherwise we return zero.  */
3349
3350 static tree
3351 optimize_bit_field_compare (location_t loc, enum tree_code code,
3352                             tree compare_type, tree lhs, tree rhs)
3353 {
3354   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3355   tree type = TREE_TYPE (lhs);
3356   tree signed_type, unsigned_type;
3357   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3358   enum machine_mode lmode, rmode, nmode;
3359   int lunsignedp, runsignedp;
3360   int lvolatilep = 0, rvolatilep = 0;
3361   tree linner, rinner = NULL_TREE;
3362   tree mask;
3363   tree offset;
3364
3365   /* Get all the information about the extractions being done.  If the bit size
3366      if the same as the size of the underlying object, we aren't doing an
3367      extraction at all and so can do nothing.  We also don't want to
3368      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3369      then will no longer be able to replace it.  */
3370   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3371                                 &lunsignedp, &lvolatilep, false);
3372   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3373       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3374     return 0;
3375
3376  if (!const_p)
3377    {
3378      /* If this is not a constant, we can only do something if bit positions,
3379         sizes, and signedness are the same.  */
3380      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3381                                    &runsignedp, &rvolatilep, false);
3382
3383      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3384          || lunsignedp != runsignedp || offset != 0
3385          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3386        return 0;
3387    }
3388
3389   /* See if we can find a mode to refer to this field.  We should be able to,
3390      but fail if we can't.  */
3391   if (lvolatilep
3392       && GET_MODE_BITSIZE (lmode) > 0
3393       && flag_strict_volatile_bitfields > 0)
3394     nmode = lmode;
3395   else
3396     nmode = get_best_mode (lbitsize, lbitpos, 0, 0,
3397                            const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3398                            : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3399                                   TYPE_ALIGN (TREE_TYPE (rinner))),
3400                            word_mode, lvolatilep || rvolatilep);
3401   if (nmode == VOIDmode)
3402     return 0;
3403
3404   /* Set signed and unsigned types of the precision of this mode for the
3405      shifts below.  */
3406   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3407   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3408
3409   /* Compute the bit position and size for the new reference and our offset
3410      within it. If the new reference is the same size as the original, we
3411      won't optimize anything, so return zero.  */
3412   nbitsize = GET_MODE_BITSIZE (nmode);
3413   nbitpos = lbitpos & ~ (nbitsize - 1);
3414   lbitpos -= nbitpos;
3415   if (nbitsize == lbitsize)
3416     return 0;
3417
3418   if (BYTES_BIG_ENDIAN)
3419     lbitpos = nbitsize - lbitsize - lbitpos;
3420
3421   /* Make the mask to be used against the extracted field.  */
3422   mask = build_int_cst_type (unsigned_type, -1);
3423   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3424   mask = const_binop (RSHIFT_EXPR, mask,
3425                       size_int (nbitsize - lbitsize - lbitpos));
3426
3427   if (! const_p)
3428     /* If not comparing with constant, just rework the comparison
3429        and return.  */
3430     return fold_build2_loc (loc, code, compare_type,
3431                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3432                                      make_bit_field_ref (loc, linner,
3433                                                          unsigned_type,
3434                                                          nbitsize, nbitpos,
3435                                                          1),
3436                                      mask),
3437                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3438                                      make_bit_field_ref (loc, rinner,
3439                                                          unsigned_type,
3440                                                          nbitsize, nbitpos,
3441                                                          1),
3442                                      mask));
3443
3444   /* Otherwise, we are handling the constant case. See if the constant is too
3445      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
3446      this not only for its own sake, but to avoid having to test for this
3447      error case below.  If we didn't, we might generate wrong code.
3448
3449      For unsigned fields, the constant shifted right by the field length should
3450      be all zero.  For signed fields, the high-order bits should agree with
3451      the sign bit.  */
3452
3453   if (lunsignedp)
3454     {
3455       if (! integer_zerop (const_binop (RSHIFT_EXPR,
3456                                         fold_convert_loc (loc,
3457                                                           unsigned_type, rhs),
3458                                         size_int (lbitsize))))
3459         {
3460           warning (0, "comparison is always %d due to width of bit-field",
3461                    code == NE_EXPR);
3462           return constant_boolean_node (code == NE_EXPR, compare_type);
3463         }
3464     }
3465   else
3466     {
3467       tree tem = const_binop (RSHIFT_EXPR,
3468                               fold_convert_loc (loc, signed_type, rhs),
3469                               size_int (lbitsize - 1));
3470       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3471         {
3472           warning (0, "comparison is always %d due to width of bit-field",
3473                    code == NE_EXPR);
3474           return constant_boolean_node (code == NE_EXPR, compare_type);
3475         }
3476     }
3477
3478   /* Single-bit compares should always be against zero.  */
3479   if (lbitsize == 1 && ! integer_zerop (rhs))
3480     {
3481       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3482       rhs = build_int_cst (type, 0);
3483     }
3484
3485   /* Make a new bitfield reference, shift the constant over the
3486      appropriate number of bits and mask it with the computed mask
3487      (in case this was a signed field).  If we changed it, make a new one.  */
3488   lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3489   if (lvolatilep)
3490     {
3491       TREE_SIDE_EFFECTS (lhs) = 1;
3492       TREE_THIS_VOLATILE (lhs) = 1;
3493     }
3494
3495   rhs = const_binop (BIT_AND_EXPR,
3496                      const_binop (LSHIFT_EXPR,
3497                                   fold_convert_loc (loc, unsigned_type, rhs),
3498                                   size_int (lbitpos)),
3499                      mask);
3500
3501   lhs = build2_loc (loc, code, compare_type,
3502                     build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
3503   return lhs;
3504 }
3505 \f
3506 /* Subroutine for fold_truthop: decode a field reference.
3507
3508    If EXP is a comparison reference, we return the innermost reference.
3509
3510    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3511    set to the starting bit number.
3512
3513    If the innermost field can be completely contained in a mode-sized
3514    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
3515
3516    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3517    otherwise it is not changed.
3518
3519    *PUNSIGNEDP is set to the signedness of the field.
3520
3521    *PMASK is set to the mask used.  This is either contained in a
3522    BIT_AND_EXPR or derived from the width of the field.
3523
3524    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3525
3526    Return 0 if this is not a component reference or is one that we can't
3527    do anything with.  */
3528
3529 static tree
3530 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3531                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3532                         int *punsignedp, int *pvolatilep,
3533                         tree *pmask, tree *pand_mask)
3534 {
3535   tree outer_type = 0;
3536   tree and_mask = 0;
3537   tree mask, inner, offset;
3538   tree unsigned_type;
3539   unsigned int precision;
3540
3541   /* All the optimizations using this function assume integer fields.
3542      There are problems with FP fields since the type_for_size call
3543      below can fail for, e.g., XFmode.  */
3544   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3545     return 0;
3546
3547   /* We are interested in the bare arrangement of bits, so strip everything
3548      that doesn't affect the machine mode.  However, record the type of the
3549      outermost expression if it may matter below.  */
3550   if (CONVERT_EXPR_P (exp)
3551       || TREE_CODE (exp) == NON_LVALUE_EXPR)
3552     outer_type = TREE_TYPE (exp);
3553   STRIP_NOPS (exp);
3554
3555   if (TREE_CODE (exp) == BIT_AND_EXPR)
3556     {
3557       and_mask = TREE_OPERAND (exp, 1);
3558       exp = TREE_OPERAND (exp, 0);
3559       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3560       if (TREE_CODE (and_mask) != INTEGER_CST)
3561         return 0;
3562     }
3563
3564   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3565                                punsignedp, pvolatilep, false);
3566   if ((inner == exp && and_mask == 0)
3567       || *pbitsize < 0 || offset != 0
3568       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3569     return 0;
3570
3571   /* If the number of bits in the reference is the same as the bitsize of
3572      the outer type, then the outer type gives the signedness. Otherwise
3573      (in case of a small bitfield) the signedness is unchanged.  */
3574   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3575     *punsignedp = TYPE_UNSIGNED (outer_type);
3576
3577   /* Compute the mask to access the bitfield.  */
3578   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3579   precision = TYPE_PRECISION (unsigned_type);
3580
3581   mask = build_int_cst_type (unsigned_type, -1);
3582
3583   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3584   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3585
3586   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
3587   if (and_mask != 0)
3588     mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3589                         fold_convert_loc (loc, unsigned_type, and_mask), mask);
3590
3591   *pmask = mask;
3592   *pand_mask = and_mask;
3593   return inner;
3594 }
3595
3596 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3597    bit positions.  */
3598
3599 static int
3600 all_ones_mask_p (const_tree mask, int size)
3601 {
3602   tree type = TREE_TYPE (mask);
3603   unsigned int precision = TYPE_PRECISION (type);
3604   tree tmask;
3605
3606   tmask = build_int_cst_type (signed_type_for (type), -1);
3607
3608   return
3609     tree_int_cst_equal (mask,
3610                         const_binop (RSHIFT_EXPR,
3611                                      const_binop (LSHIFT_EXPR, tmask,
3612                                                   size_int (precision - size)),
3613                                      size_int (precision - size)));
3614 }
3615
3616 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3617    represents the sign bit of EXP's type.  If EXP represents a sign
3618    or zero extension, also test VAL against the unextended type.
3619    The return value is the (sub)expression whose sign bit is VAL,
3620    or NULL_TREE otherwise.  */
3621
3622 static tree
3623 sign_bit_p (tree exp, const_tree val)
3624 {
3625   unsigned HOST_WIDE_INT mask_lo, lo;
3626   HOST_WIDE_INT mask_hi, hi;
3627   int width;
3628   tree t;
3629
3630   /* Tree EXP must have an integral type.  */
3631   t = TREE_TYPE (exp);
3632   if (! INTEGRAL_TYPE_P (t))
3633     return NULL_TREE;
3634
3635   /* Tree VAL must be an integer constant.  */
3636   if (TREE_CODE (val) != INTEGER_CST
3637       || TREE_OVERFLOW (val))
3638     return NULL_TREE;
3639
3640   width = TYPE_PRECISION (t);
3641   if (width > HOST_BITS_PER_WIDE_INT)
3642     {
3643       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3644       lo = 0;
3645
3646       mask_hi = ((unsigned HOST_WIDE_INT) -1
3647                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
3648       mask_lo = -1;
3649     }
3650   else
3651     {
3652       hi = 0;
3653       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3654
3655       mask_hi = 0;
3656       mask_lo = ((unsigned HOST_WIDE_INT) -1
3657                  >> (HOST_BITS_PER_WIDE_INT - width));
3658     }
3659
3660   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3661      treat VAL as if it were unsigned.  */
3662   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3663       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3664     return exp;
3665
3666   /* Handle extension from a narrower type.  */
3667   if (TREE_CODE (exp) == NOP_EXPR
3668       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3669     return sign_bit_p (TREE_OPERAND (exp, 0), val);
3670
3671   return NULL_TREE;
3672 }
3673
3674 /* Subroutine for fold_truthop: determine if an operand is simple enough
3675    to be evaluated unconditionally.  */
3676
3677 static int
3678 simple_operand_p (const_tree exp)
3679 {
3680   /* Strip any conversions that don't change the machine mode.  */
3681   STRIP_NOPS (exp);
3682
3683   return (CONSTANT_CLASS_P (exp)
3684           || TREE_CODE (exp) == SSA_NAME
3685           || (DECL_P (exp)
3686               && ! TREE_ADDRESSABLE (exp)
3687               && ! TREE_THIS_VOLATILE (exp)
3688               && ! DECL_NONLOCAL (exp)
3689               /* Don't regard global variables as simple.  They may be
3690                  allocated in ways unknown to the compiler (shared memory,
3691                  #pragma weak, etc).  */
3692               && ! TREE_PUBLIC (exp)
3693               && ! DECL_EXTERNAL (exp)
3694               /* Loading a static variable is unduly expensive, but global
3695                  registers aren't expensive.  */
3696               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3697 }
3698 \f
3699 /* The following functions are subroutines to fold_range_test and allow it to
3700    try to change a logical combination of comparisons into a range test.
3701
3702    For example, both
3703         X == 2 || X == 3 || X == 4 || X == 5
3704    and
3705         X >= 2 && X <= 5
3706    are converted to
3707         (unsigned) (X - 2) <= 3
3708
3709    We describe each set of comparisons as being either inside or outside
3710    a range, using a variable named like IN_P, and then describe the
3711    range with a lower and upper bound.  If one of the bounds is omitted,
3712    it represents either the highest or lowest value of the type.
3713
3714    In the comments below, we represent a range by two numbers in brackets
3715    preceded by a "+" to designate being inside that range, or a "-" to
3716    designate being outside that range, so the condition can be inverted by
3717    flipping the prefix.  An omitted bound is represented by a "-".  For
3718    example, "- [-, 10]" means being outside the range starting at the lowest
3719    possible value and ending at 10, in other words, being greater than 10.
3720    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3721    always false.
3722
3723    We set up things so that the missing bounds are handled in a consistent
3724    manner so neither a missing bound nor "true" and "false" need to be
3725    handled using a special case.  */
3726
3727 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3728    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3729    and UPPER1_P are nonzero if the respective argument is an upper bound
3730    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
3731    must be specified for a comparison.  ARG1 will be converted to ARG0's
3732    type if both are specified.  */
3733
3734 static tree
3735 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3736              tree arg1, int upper1_p)
3737 {
3738   tree tem;
3739   int result;
3740   int sgn0, sgn1;
3741
3742   /* If neither arg represents infinity, do the normal operation.
3743      Else, if not a comparison, return infinity.  Else handle the special
3744      comparison rules. Note that most of the cases below won't occur, but
3745      are handled for consistency.  */
3746
3747   if (arg0 != 0 && arg1 != 0)
3748     {
3749       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3750                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
3751       STRIP_NOPS (tem);
3752       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3753     }
3754
3755   if (TREE_CODE_CLASS (code) != tcc_comparison)
3756     return 0;
3757
3758   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3759      for neither.  In real maths, we cannot assume open ended ranges are
3760      the same. But, this is computer arithmetic, where numbers are finite.
3761      We can therefore make the transformation of any unbounded range with
3762      the value Z, Z being greater than any representable number. This permits
3763      us to treat unbounded ranges as equal.  */
3764   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3765   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3766   switch (code)
3767     {
3768     case EQ_EXPR:
3769       result = sgn0 == sgn1;
3770       break;
3771     case NE_EXPR:
3772       result = sgn0 != sgn1;
3773       break;
3774     case LT_EXPR:
3775       result = sgn0 < sgn1;
3776       break;
3777     case LE_EXPR:
3778       result = sgn0 <= sgn1;
3779       break;
3780     case GT_EXPR:
3781       result = sgn0 > sgn1;
3782       break;
3783     case GE_EXPR:
3784       result = sgn0 >= sgn1;
3785       break;
3786     default:
3787       gcc_unreachable ();
3788     }
3789
3790   return constant_boolean_node (result, type);
3791 }
3792 \f
3793 /* Given EXP, a logical expression, set the range it is testing into
3794    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
3795    actually being tested.  *PLOW and *PHIGH will be made of the same
3796    type as the returned expression.  If EXP is not a comparison, we
3797    will most likely not be returning a useful value and range.  Set
3798    *STRICT_OVERFLOW_P to true if the return value is only valid
3799    because signed overflow is undefined; otherwise, do not change
3800    *STRICT_OVERFLOW_P.  */
3801
3802 tree
3803 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
3804             bool *strict_overflow_p)
3805 {
3806   enum tree_code code;
3807   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
3808   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
3809   int in_p, n_in_p;
3810   tree low, high, n_low, n_high;
3811   location_t loc = EXPR_LOCATION (exp);
3812
3813   /* Start with simply saying "EXP != 0" and then look at the code of EXP
3814      and see if we can refine the range.  Some of the cases below may not
3815      happen, but it doesn't seem worth worrying about this.  We "continue"
3816      the outer loop when we've changed something; otherwise we "break"
3817      the switch, which will "break" the while.  */
3818
3819   in_p = 0;
3820   low = high = build_int_cst (TREE_TYPE (exp), 0);
3821
3822   while (1)
3823     {
3824       code = TREE_CODE (exp);
3825       exp_type = TREE_TYPE (exp);
3826
3827       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3828         {
3829           if (TREE_OPERAND_LENGTH (exp) > 0)
3830             arg0 = TREE_OPERAND (exp, 0);
3831           if (TREE_CODE_CLASS (code) == tcc_comparison
3832               || TREE_CODE_CLASS (code) == tcc_unary
3833               || TREE_CODE_CLASS (code) == tcc_binary)
3834             arg0_type = TREE_TYPE (arg0);
3835           if (TREE_CODE_CLASS (code) == tcc_binary
3836               || TREE_CODE_CLASS (code) == tcc_comparison
3837               || (TREE_CODE_CLASS (code) == tcc_expression
3838                   && TREE_OPERAND_LENGTH (exp) > 1))
3839             arg1 = TREE_OPERAND (exp, 1);
3840         }
3841
3842       switch (code)
3843         {
3844         case TRUTH_NOT_EXPR:
3845           in_p = ! in_p, exp = arg0;
3846           continue;
3847
3848         case EQ_EXPR: case NE_EXPR:
3849         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3850           /* We can only do something if the range is testing for zero
3851              and if the second operand is an integer constant.  Note that
3852              saying something is "in" the range we make is done by
3853              complementing IN_P since it will set in the initial case of
3854              being not equal to zero; "out" is leaving it alone.  */
3855           if (low == 0 || high == 0
3856               || ! integer_zerop (low) || ! integer_zerop (high)
3857               || TREE_CODE (arg1) != INTEGER_CST)
3858             break;
3859
3860           switch (code)
3861             {
3862             case NE_EXPR:  /* - [c, c]  */
3863               low = high = arg1;
3864               break;
3865             case EQ_EXPR:  /* + [c, c]  */
3866               in_p = ! in_p, low = high = arg1;
3867               break;
3868             case GT_EXPR:  /* - [-, c] */
3869               low = 0, high = arg1;
3870               break;
3871             case GE_EXPR:  /* + [c, -] */
3872               in_p = ! in_p, low = arg1, high = 0;
3873               break;
3874             case LT_EXPR:  /* - [c, -] */
3875               low = arg1, high = 0;
3876               break;
3877             case LE_EXPR:  /* + [-, c] */
3878               in_p = ! in_p, low = 0, high = arg1;
3879               break;
3880             default:
3881               gcc_unreachable ();
3882             }
3883
3884           /* If this is an unsigned comparison, we also know that EXP is
3885              greater than or equal to zero.  We base the range tests we make
3886              on that fact, so we record it here so we can parse existing
3887              range tests.  We test arg0_type since often the return type
3888              of, e.g. EQ_EXPR, is boolean.  */
3889           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3890             {
3891               if (! merge_ranges (&n_in_p, &n_low, &n_high,
3892                                   in_p, low, high, 1,
3893                                   build_int_cst (arg0_type, 0),
3894                                   NULL_TREE))
3895                 break;
3896
3897               in_p = n_in_p, low = n_low, high = n_high;
3898
3899               /* If the high bound is missing, but we have a nonzero low
3900                  bound, reverse the range so it goes from zero to the low bound
3901                  minus 1.  */
3902               if (high == 0 && low && ! integer_zerop (low))
3903                 {
3904                   in_p = ! in_p;
3905                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3906                                       integer_one_node, 0);
3907                   low = build_int_cst (arg0_type, 0);
3908                 }
3909             }
3910
3911           exp = arg0;
3912           continue;
3913
3914         case NEGATE_EXPR:
3915           /* (-x) IN [a,b] -> x in [-b, -a]  */
3916           n_low = range_binop (MINUS_EXPR, exp_type,
3917                                build_int_cst (exp_type, 0),
3918                                0, high, 1);
3919           n_high = range_binop (MINUS_EXPR, exp_type,
3920                                 build_int_cst (exp_type, 0),
3921                                 0, low, 0);
3922           if (n_high != 0 && TREE_OVERFLOW (n_high))
3923             break;
3924           goto normalize;
3925
3926         case BIT_NOT_EXPR:
3927           /* ~ X -> -X - 1  */
3928           exp = build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
3929                             build_int_cst (exp_type, 1));
3930           continue;
3931
3932         case PLUS_EXPR:  case MINUS_EXPR:
3933           if (TREE_CODE (arg1) != INTEGER_CST)
3934             break;
3935
3936           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
3937              move a constant to the other side.  */
3938           if (!TYPE_UNSIGNED (arg0_type)
3939               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
3940             break;
3941
3942           /* If EXP is signed, any overflow in the computation is undefined,
3943              so we don't worry about it so long as our computations on
3944              the bounds don't overflow.  For unsigned, overflow is defined
3945              and this is exactly the right thing.  */
3946           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3947                                arg0_type, low, 0, arg1, 0);
3948           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3949                                 arg0_type, high, 1, arg1, 0);
3950           if ((n_low != 0 && TREE_OVERFLOW (n_low))
3951               || (n_high != 0 && TREE_OVERFLOW (n_high)))
3952             break;
3953
3954           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
3955             *strict_overflow_p = true;
3956
3957         normalize:
3958           /* Check for an unsigned range which has wrapped around the maximum
3959              value thus making n_high < n_low, and normalize it.  */
3960           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3961             {
3962               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
3963                                  integer_one_node, 0);
3964               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
3965                                   integer_one_node, 0);
3966
3967               /* If the range is of the form +/- [ x+1, x ], we won't
3968                  be able to normalize it.  But then, it represents the
3969                  whole range or the empty set, so make it
3970                  +/- [ -, - ].  */
3971               if (tree_int_cst_equal (n_low, low)
3972                   && tree_int_cst_equal (n_high, high))
3973                 low = high = 0;
3974               else
3975                 in_p = ! in_p;
3976             }
3977           else
3978             low = n_low, high = n_high;
3979
3980           exp = arg0;
3981           continue;
3982
3983         CASE_CONVERT: case NON_LVALUE_EXPR:
3984           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
3985             break;
3986
3987           if (! INTEGRAL_TYPE_P (arg0_type)
3988               || (low != 0 && ! int_fits_type_p (low, arg0_type))
3989               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
3990             break;
3991
3992           n_low = low, n_high = high;
3993
3994           if (n_low != 0)
3995             n_low = fold_convert_loc (loc, arg0_type, n_low);
3996
3997           if (n_high != 0)
3998             n_high = fold_convert_loc (loc, arg0_type, n_high);
3999
4000
4001           /* If we're converting arg0 from an unsigned type, to exp,
4002              a signed type,  we will be doing the comparison as unsigned.
4003              The tests above have already verified that LOW and HIGH
4004              are both positive.
4005
4006              So we have to ensure that we will handle large unsigned
4007              values the same way that the current signed bounds treat
4008              negative values.  */
4009
4010           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4011             {
4012               tree high_positive;
4013               tree equiv_type;
4014               /* For fixed-point modes, we need to pass the saturating flag
4015                  as the 2nd parameter.  */
4016               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4017                 equiv_type = lang_hooks.types.type_for_mode
4018                              (TYPE_MODE (arg0_type),
4019                               TYPE_SATURATING (arg0_type));
4020               else
4021                 equiv_type = lang_hooks.types.type_for_mode
4022                              (TYPE_MODE (arg0_type), 1);
4023
4024               /* A range without an upper bound is, naturally, unbounded.
4025                  Since convert would have cropped a very large value, use
4026                  the max value for the destination type.  */
4027               high_positive
4028                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4029                 : TYPE_MAX_VALUE (arg0_type);
4030
4031               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4032                 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4033                                              fold_convert_loc (loc, arg0_type,
4034                                                                high_positive),
4035                                              build_int_cst (arg0_type, 1));
4036
4037               /* If the low bound is specified, "and" the range with the
4038                  range for which the original unsigned value will be
4039                  positive.  */
4040               if (low != 0)
4041                 {
4042                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4043                                       1, n_low, n_high, 1,
4044                                       fold_convert_loc (loc, arg0_type,
4045                                                         integer_zero_node),
4046                                       high_positive))
4047                     break;
4048
4049                   in_p = (n_in_p == in_p);
4050                 }
4051               else
4052                 {
4053                   /* Otherwise, "or" the range with the range of the input
4054                      that will be interpreted as negative.  */
4055                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4056                                       0, n_low, n_high, 1,
4057                                       fold_convert_loc (loc, arg0_type,
4058                                                         integer_zero_node),
4059                                       high_positive))
4060                     break;
4061
4062                   in_p = (in_p != n_in_p);
4063                 }
4064             }
4065
4066           exp = arg0;
4067           low = n_low, high = n_high;
4068           continue;
4069
4070         default:
4071           break;
4072         }
4073
4074       break;
4075     }
4076
4077   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4078   if (TREE_CODE (exp) == INTEGER_CST)
4079     {
4080       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4081                                                  exp, 0, low, 0))
4082                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4083                                                     exp, 1, high, 1)));
4084       low = high = 0;
4085       exp = 0;
4086     }
4087
4088   *pin_p = in_p, *plow = low, *phigh = high;
4089   return exp;
4090 }
4091 \f
4092 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4093    type, TYPE, return an expression to test if EXP is in (or out of, depending
4094    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4095
4096 tree
4097 build_range_check (location_t loc, tree type, tree exp, int in_p,
4098                    tree low, tree high)
4099 {
4100   tree etype = TREE_TYPE (exp), value;
4101
4102 #ifdef HAVE_canonicalize_funcptr_for_compare
4103   /* Disable this optimization for function pointer expressions
4104      on targets that require function pointer canonicalization.  */
4105   if (HAVE_canonicalize_funcptr_for_compare
4106       && TREE_CODE (etype) == POINTER_TYPE
4107       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4108     return NULL_TREE;
4109 #endif
4110
4111   if (! in_p)
4112     {
4113       value = build_range_check (loc, type, exp, 1, low, high);
4114       if (value != 0)
4115         return invert_truthvalue_loc (loc, value);
4116
4117       return 0;
4118     }
4119
4120   if (low == 0 && high == 0)
4121     return build_int_cst (type, 1);
4122
4123   if (low == 0)
4124     return fold_build2_loc (loc, LE_EXPR, type, exp,
4125                         fold_convert_loc (loc, etype, high));
4126
4127   if (high == 0)
4128     return fold_build2_loc (loc, GE_EXPR, type, exp,
4129                         fold_convert_loc (loc, etype, low));
4130
4131   if (operand_equal_p (low, high, 0))
4132     return fold_build2_loc (loc, EQ_EXPR, type, exp,
4133                         fold_convert_loc (loc, etype, low));
4134
4135   if (integer_zerop (low))
4136     {
4137       if (! TYPE_UNSIGNED (etype))
4138         {
4139           etype = unsigned_type_for (etype);
4140           high = fold_convert_loc (loc, etype, high);
4141           exp = fold_convert_loc (loc, etype, exp);
4142         }
4143       return build_range_check (loc, type, exp, 1, 0, high);
4144     }
4145
4146   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4147   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4148     {
4149       unsigned HOST_WIDE_INT lo;
4150       HOST_WIDE_INT hi;
4151       int prec;
4152
4153       prec = TYPE_PRECISION (etype);
4154       if (prec <= HOST_BITS_PER_WIDE_INT)
4155         {
4156           hi = 0;
4157           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4158         }
4159       else
4160         {
4161           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4162           lo = (unsigned HOST_WIDE_INT) -1;
4163         }
4164
4165       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4166         {
4167           if (TYPE_UNSIGNED (etype))
4168             {
4169               tree signed_etype = signed_type_for (etype);
4170               if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4171                 etype
4172                   = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4173               else
4174                 etype = signed_etype;
4175               exp = fold_convert_loc (loc, etype, exp);
4176             }
4177           return fold_build2_loc (loc, GT_EXPR, type, exp,
4178                               build_int_cst (etype, 0));
4179         }
4180     }
4181
4182   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4183      This requires wrap-around arithmetics for the type of the expression.
4184      First make sure that arithmetics in this type is valid, then make sure
4185      that it wraps around.  */
4186   if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4187     etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4188                                             TYPE_UNSIGNED (etype));
4189
4190   if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4191     {
4192       tree utype, minv, maxv;
4193
4194       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4195          for the type in question, as we rely on this here.  */
4196       utype = unsigned_type_for (etype);
4197       maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4198       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4199                           integer_one_node, 1);
4200       minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4201
4202       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4203                                       minv, 1, maxv, 1)))
4204         etype = utype;
4205       else
4206         return 0;
4207     }
4208
4209   high = fold_convert_loc (loc, etype, high);
4210   low = fold_convert_loc (loc, etype, low);
4211   exp = fold_convert_loc (loc, etype, exp);
4212
4213   value = const_binop (MINUS_EXPR, high, low);
4214
4215
4216   if (POINTER_TYPE_P (etype))
4217     {
4218       if (value != 0 && !TREE_OVERFLOW (value))
4219         {
4220           low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4221           return build_range_check (loc, type,
4222                                     fold_build_pointer_plus_loc (loc, exp, low),
4223                                     1, build_int_cst (etype, 0), value);
4224         }
4225       return 0;
4226     }
4227
4228   if (value != 0 && !TREE_OVERFLOW (value))
4229     return build_range_check (loc, type,
4230                               fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4231                               1, build_int_cst (etype, 0), value);
4232
4233   return 0;
4234 }
4235 \f
4236 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4237
4238 static tree
4239 range_predecessor (tree val)
4240 {
4241   tree type = TREE_TYPE (val);
4242
4243   if (INTEGRAL_TYPE_P (type)
4244       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4245     return 0;
4246   else
4247     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4248 }
4249
4250 /* Return the successor of VAL in its type, handling the infinite case.  */
4251
4252 static tree
4253 range_successor (tree val)
4254 {
4255   tree type = TREE_TYPE (val);
4256
4257   if (INTEGRAL_TYPE_P (type)
4258       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4259     return 0;
4260   else
4261     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4262 }
4263
4264 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4265    can, 0 if we can't.  Set the output range into the specified parameters.  */
4266
4267 bool
4268 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4269               tree high0, int in1_p, tree low1, tree high1)
4270 {
4271   int no_overlap;
4272   int subset;
4273   int temp;
4274   tree tem;
4275   int in_p;
4276   tree low, high;
4277   int lowequal = ((low0 == 0 && low1 == 0)
4278                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4279                                                 low0, 0, low1, 0)));
4280   int highequal = ((high0 == 0 && high1 == 0)
4281                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4282                                                  high0, 1, high1, 1)));
4283
4284   /* Make range 0 be the range that starts first, or ends last if they
4285      start at the same value.  Swap them if it isn't.  */
4286   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4287                                  low0, 0, low1, 0))
4288       || (lowequal
4289           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4290                                         high1, 1, high0, 1))))
4291     {
4292       temp = in0_p, in0_p = in1_p, in1_p = temp;
4293       tem = low0, low0 = low1, low1 = tem;
4294       tem = high0, high0 = high1, high1 = tem;
4295     }
4296
4297   /* Now flag two cases, whether the ranges are disjoint or whether the
4298      second range is totally subsumed in the first.  Note that the tests
4299      below are simplified by the ones above.  */
4300   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4301                                           high0, 1, low1, 0));
4302   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4303                                       high1, 1, high0, 1));
4304
4305   /* We now have four cases, depending on whether we are including or
4306      excluding the two ranges.  */
4307   if (in0_p && in1_p)
4308     {
4309       /* If they don't overlap, the result is false.  If the second range
4310          is a subset it is the result.  Otherwise, the range is from the start
4311          of the second to the end of the first.  */
4312       if (no_overlap)
4313         in_p = 0, low = high = 0;
4314       else if (subset)
4315         in_p = 1, low = low1, high = high1;
4316       else
4317         in_p = 1, low = low1, high = high0;
4318     }
4319
4320   else if (in0_p && ! in1_p)
4321     {
4322       /* If they don't overlap, the result is the first range.  If they are
4323          equal, the result is false.  If the second range is a subset of the
4324          first, and the ranges begin at the same place, we go from just after
4325          the end of the second range to the end of the first.  If the second
4326          range is not a subset of the first, or if it is a subset and both
4327          ranges end at the same place, the range starts at the start of the
4328          first range and ends just before the second range.
4329          Otherwise, we can't describe this as a single range.  */
4330       if (no_overlap)
4331         in_p = 1, low = low0, high = high0;
4332       else if (lowequal && highequal)
4333         in_p = 0, low = high = 0;
4334       else if (subset && lowequal)
4335         {
4336           low = range_successor (high1);
4337           high = high0;
4338           in_p = 1;
4339           if (low == 0)
4340             {
4341               /* We are in the weird situation where high0 > high1 but
4342                  high1 has no successor.  Punt.  */
4343               return 0;
4344             }
4345         }
4346       else if (! subset || highequal)
4347         {
4348           low = low0;
4349           high = range_predecessor (low1);
4350           in_p = 1;
4351           if (high == 0)
4352             {
4353               /* low0 < low1 but low1 has no predecessor.  Punt.  */
4354               return 0;
4355             }
4356         }
4357       else
4358         return 0;
4359     }
4360
4361   else if (! in0_p && in1_p)
4362     {
4363       /* If they don't overlap, the result is the second range.  If the second
4364          is a subset of the first, the result is false.  Otherwise,
4365          the range starts just after the first range and ends at the
4366          end of the second.  */
4367       if (no_overlap)
4368         in_p = 1, low = low1, high = high1;
4369       else if (subset || highequal)
4370         in_p = 0, low = high = 0;
4371       else
4372         {
4373           low = range_successor (high0);
4374           high = high1;
4375           in_p = 1;
4376           if (low == 0)
4377             {
4378               /* high1 > high0 but high0 has no successor.  Punt.  */
4379               return 0;
4380             }
4381         }
4382     }
4383
4384   else
4385     {
4386       /* The case where we are excluding both ranges.  Here the complex case
4387          is if they don't overlap.  In that case, the only time we have a
4388          range is if they are adjacent.  If the second is a subset of the
4389          first, the result is the first.  Otherwise, the range to exclude
4390          starts at the beginning of the first range and ends at the end of the
4391          second.  */
4392       if (no_overlap)
4393         {
4394           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4395                                          range_successor (high0),
4396                                          1, low1, 0)))
4397             in_p = 0, low = low0, high = high1;
4398           else
4399             {
4400               /* Canonicalize - [min, x] into - [-, x].  */
4401               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4402                 switch (TREE_CODE (TREE_TYPE (low0)))
4403                   {
4404                   case ENUMERAL_TYPE:
4405                     if (TYPE_PRECISION (TREE_TYPE (low0))
4406                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4407                       break;
4408                     /* FALLTHROUGH */
4409                   case INTEGER_TYPE:
4410                     if (tree_int_cst_equal (low0,
4411                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4412                       low0 = 0;
4413                     break;
4414                   case POINTER_TYPE:
4415                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4416                         && integer_zerop (low0))
4417                       low0 = 0;
4418                     break;
4419                   default:
4420                     break;
4421                   }
4422
4423               /* Canonicalize - [x, max] into - [x, -].  */
4424               if (high1 && TREE_CODE (high1) == INTEGER_CST)
4425                 switch (TREE_CODE (TREE_TYPE (high1)))
4426                   {
4427                   case ENUMERAL_TYPE:
4428                     if (TYPE_PRECISION (TREE_TYPE (high1))
4429                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4430                       break;
4431                     /* FALLTHROUGH */
4432                   case INTEGER_TYPE:
4433                     if (tree_int_cst_equal (high1,
4434                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
4435                       high1 = 0;
4436                     break;
4437                   case POINTER_TYPE:
4438                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
4439                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4440                                                        high1, 1,
4441                                                        integer_one_node, 1)))
4442                       high1 = 0;
4443                     break;
4444                   default:
4445                     break;
4446                   }
4447
4448               /* The ranges might be also adjacent between the maximum and
4449                  minimum values of the given type.  For
4450                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4451                  return + [x + 1, y - 1].  */
4452               if (low0 == 0 && high1 == 0)
4453                 {
4454                   low = range_successor (high0);
4455                   high = range_predecessor (low1);
4456                   if (low == 0 || high == 0)
4457                     return 0;
4458
4459                   in_p = 1;
4460                 }
4461               else
4462                 return 0;
4463             }
4464         }
4465       else if (subset)
4466         in_p = 0, low = low0, high = high0;
4467       else
4468         in_p = 0, low = low0, high = high1;
4469     }
4470
4471   *pin_p = in_p, *plow = low, *phigh = high;
4472   return 1;
4473 }
4474 \f
4475
4476 /* Subroutine of fold, looking inside expressions of the form
4477    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4478    of the COND_EXPR.  This function is being used also to optimize
4479    A op B ? C : A, by reversing the comparison first.
4480
4481    Return a folded expression whose code is not a COND_EXPR
4482    anymore, or NULL_TREE if no folding opportunity is found.  */
4483
4484 static tree
4485 fold_cond_expr_with_comparison (location_t loc, tree type,
4486                                 tree arg0, tree arg1, tree arg2)
4487 {
4488   enum tree_code comp_code = TREE_CODE (arg0);
4489   tree arg00 = TREE_OPERAND (arg0, 0);
4490   tree arg01 = TREE_OPERAND (arg0, 1);
4491   tree arg1_type = TREE_TYPE (arg1);
4492   tree tem;
4493
4494   STRIP_NOPS (arg1);
4495   STRIP_NOPS (arg2);
4496
4497   /* If we have A op 0 ? A : -A, consider applying the following
4498      transformations:
4499
4500      A == 0? A : -A    same as -A
4501      A != 0? A : -A    same as A
4502      A >= 0? A : -A    same as abs (A)
4503      A > 0?  A : -A    same as abs (A)
4504      A <= 0? A : -A    same as -abs (A)
4505      A < 0?  A : -A    same as -abs (A)
4506
4507      None of these transformations work for modes with signed
4508      zeros.  If A is +/-0, the first two transformations will
4509      change the sign of the result (from +0 to -0, or vice
4510      versa).  The last four will fix the sign of the result,
4511      even though the original expressions could be positive or
4512      negative, depending on the sign of A.
4513
4514      Note that all these transformations are correct if A is
4515      NaN, since the two alternatives (A and -A) are also NaNs.  */
4516   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4517       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4518           ? real_zerop (arg01)
4519           : integer_zerop (arg01))
4520       && ((TREE_CODE (arg2) == NEGATE_EXPR
4521            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4522              /* In the case that A is of the form X-Y, '-A' (arg2) may
4523                 have already been folded to Y-X, check for that. */
4524           || (TREE_CODE (arg1) == MINUS_EXPR
4525               && TREE_CODE (arg2) == MINUS_EXPR
4526               && operand_equal_p (TREE_OPERAND (arg1, 0),
4527                                   TREE_OPERAND (arg2, 1), 0)
4528               && operand_equal_p (TREE_OPERAND (arg1, 1),
4529                                   TREE_OPERAND (arg2, 0), 0))))
4530     switch (comp_code)
4531       {
4532       case EQ_EXPR:
4533       case UNEQ_EXPR:
4534         tem = fold_convert_loc (loc, arg1_type, arg1);
4535         return pedantic_non_lvalue_loc (loc,
4536                                     fold_convert_loc (loc, type,
4537                                                   negate_expr (tem)));
4538       case NE_EXPR:
4539       case LTGT_EXPR:
4540         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4541       case UNGE_EXPR:
4542       case UNGT_EXPR:
4543         if (flag_trapping_math)
4544           break;
4545         /* Fall through.  */
4546       case GE_EXPR:
4547       case GT_EXPR:
4548         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4549           arg1 = fold_convert_loc (loc, signed_type_for
4550                                (TREE_TYPE (arg1)), arg1);
4551         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4552         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4553       case UNLE_EXPR:
4554       case UNLT_EXPR:
4555         if (flag_trapping_math)
4556           break;
4557       case LE_EXPR:
4558       case LT_EXPR:
4559         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4560           arg1 = fold_convert_loc (loc, signed_type_for
4561                                (TREE_TYPE (arg1)), arg1);
4562         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4563         return negate_expr (fold_convert_loc (loc, type, tem));
4564       default:
4565         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4566         break;
4567       }
4568
4569   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
4570      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
4571      both transformations are correct when A is NaN: A != 0
4572      is then true, and A == 0 is false.  */
4573
4574   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4575       && integer_zerop (arg01) && integer_zerop (arg2))
4576     {
4577       if (comp_code == NE_EXPR)
4578         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4579       else if (comp_code == EQ_EXPR)
4580         return build_int_cst (type, 0);
4581     }
4582
4583   /* Try some transformations of A op B ? A : B.
4584
4585      A == B? A : B    same as B
4586      A != B? A : B    same as A
4587      A >= B? A : B    same as max (A, B)
4588      A > B?  A : B    same as max (B, A)
4589      A <= B? A : B    same as min (A, B)
4590      A < B?  A : B    same as min (B, A)
4591
4592      As above, these transformations don't work in the presence
4593      of signed zeros.  For example, if A and B are zeros of
4594      opposite sign, the first two transformations will change
4595      the sign of the result.  In the last four, the original
4596      expressions give different results for (A=+0, B=-0) and
4597      (A=-0, B=+0), but the transformed expressions do not.
4598
4599      The first two transformations are correct if either A or B
4600      is a NaN.  In the first transformation, the condition will
4601      be false, and B will indeed be chosen.  In the case of the
4602      second transformation, the condition A != B will be true,
4603      and A will be chosen.
4604
4605      The conversions to max() and min() are not correct if B is
4606      a number and A is not.  The conditions in the original
4607      expressions will be false, so all four give B.  The min()
4608      and max() versions would give a NaN instead.  */
4609   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4610       && operand_equal_for_comparison_p (arg01, arg2, arg00)
4611       /* Avoid these transformations if the COND_EXPR may be used
4612          as an lvalue in the C++ front-end.  PR c++/19199.  */
4613       && (in_gimple_form
4614           || (strcmp (lang_hooks.name, "GNU C++") != 0
4615               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4616           || ! maybe_lvalue_p (arg1)
4617           || ! maybe_lvalue_p (arg2)))
4618     {
4619       tree comp_op0 = arg00;
4620       tree comp_op1 = arg01;
4621       tree comp_type = TREE_TYPE (comp_op0);
4622
4623       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
4624       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4625         {
4626           comp_type = type;
4627           comp_op0 = arg1;
4628           comp_op1 = arg2;
4629         }
4630
4631       switch (comp_code)
4632         {
4633         case EQ_EXPR:
4634           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
4635         case NE_EXPR:
4636           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4637         case LE_EXPR:
4638         case LT_EXPR:
4639         case UNLE_EXPR:
4640         case UNLT_EXPR:
4641           /* In C++ a ?: expression can be an lvalue, so put the
4642              operand which will be used if they are equal first
4643              so that we can convert this back to the
4644              corresponding COND_EXPR.  */
4645           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4646             {
4647               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4648               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4649               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4650                     ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
4651                     : fold_build2_loc (loc, MIN_EXPR, comp_type,
4652                                    comp_op1, comp_op0);
4653               return pedantic_non_lvalue_loc (loc,
4654                                           fold_convert_loc (loc, type, tem));
4655             }
4656           break;
4657         case GE_EXPR:
4658         case GT_EXPR:
4659         case UNGE_EXPR:
4660         case UNGT_EXPR:
4661           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4662             {
4663               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4664               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4665               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4666                     ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
4667                     : fold_build2_loc (loc, MAX_EXPR, comp_type,
4668                                    comp_op1, comp_op0);
4669               return pedantic_non_lvalue_loc (loc,
4670                                           fold_convert_loc (loc, type, tem));
4671             }
4672           break;
4673         case UNEQ_EXPR:
4674           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4675             return pedantic_non_lvalue_loc (loc,
4676                                         fold_convert_loc (loc, type, arg2));
4677           break;
4678         case LTGT_EXPR:
4679           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4680             return pedantic_non_lvalue_loc (loc,
4681                                         fold_convert_loc (loc, type, arg1));
4682           break;
4683         default:
4684           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4685           break;
4686         }
4687     }
4688
4689   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4690      we might still be able to simplify this.  For example,
4691      if C1 is one less or one more than C2, this might have started
4692      out as a MIN or MAX and been transformed by this function.
4693      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
4694
4695   if (INTEGRAL_TYPE_P (type)
4696       && TREE_CODE (arg01) == INTEGER_CST
4697       && TREE_CODE (arg2) == INTEGER_CST)
4698     switch (comp_code)
4699       {
4700       case EQ_EXPR:
4701         if (TREE_CODE (arg1) == INTEGER_CST)
4702           break;
4703         /* We can replace A with C1 in this case.  */
4704         arg1 = fold_convert_loc (loc, type, arg01);
4705         return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
4706
4707       case LT_EXPR:
4708         /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
4709            MIN_EXPR, to preserve the signedness of the comparison.  */
4710         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4711                                OEP_ONLY_CONST)
4712             && operand_equal_p (arg01,
4713                                 const_binop (PLUS_EXPR, arg2,
4714                                              build_int_cst (type, 1)),
4715                                 OEP_ONLY_CONST))
4716           {
4717             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4718                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4719                                                      arg2));
4720             return pedantic_non_lvalue_loc (loc,
4721                                             fold_convert_loc (loc, type, tem));
4722           }
4723         break;
4724
4725       case LE_EXPR:
4726         /* If C1 is C2 - 1, this is min(A, C2), with the same care
4727            as above.  */
4728         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4729                                OEP_ONLY_CONST)
4730             && operand_equal_p (arg01,
4731                                 const_binop (MINUS_EXPR, arg2,
4732                                              build_int_cst (type, 1)),
4733                                 OEP_ONLY_CONST))
4734           {
4735             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4736                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4737                                                      arg2));
4738             return pedantic_non_lvalue_loc (loc,
4739                                             fold_convert_loc (loc, type, tem));
4740           }
4741         break;
4742
4743       case GT_EXPR:
4744         /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
4745            MAX_EXPR, to preserve the signedness of the comparison.  */
4746         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4747                                OEP_ONLY_CONST)
4748             && operand_equal_p (arg01,
4749                                 const_binop (MINUS_EXPR, arg2,
4750                                              build_int_cst (type, 1)),
4751                                 OEP_ONLY_CONST))
4752           {
4753             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4754                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4755                                                      arg2));
4756             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4757           }
4758         break;
4759
4760       case GE_EXPR:
4761         /* If C1 is C2 + 1, this is max(A, C2), with the same care as above.  */
4762         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4763                                OEP_ONLY_CONST)
4764             && operand_equal_p (arg01,
4765                                 const_binop (PLUS_EXPR, arg2,
4766                                              build_int_cst (type, 1)),
4767                                 OEP_ONLY_CONST))
4768           {
4769             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4770                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4771                                                      arg2));
4772             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4773           }
4774         break;
4775       case NE_EXPR:
4776         break;
4777       default:
4778         gcc_unreachable ();
4779       }
4780
4781   return NULL_TREE;
4782 }
4783
4784
4785 \f
4786 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4787 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
4788   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
4789                 false) >= 2)
4790 #endif
4791
4792 /* EXP is some logical combination of boolean tests.  See if we can
4793    merge it into some range test.  Return the new tree if so.  */
4794
4795 static tree
4796 fold_range_test (location_t loc, enum tree_code code, tree type,
4797                  tree op0, tree op1)
4798 {
4799   int or_op = (code == TRUTH_ORIF_EXPR
4800                || code == TRUTH_OR_EXPR);
4801   int in0_p, in1_p, in_p;
4802   tree low0, low1, low, high0, high1, high;
4803   bool strict_overflow_p = false;
4804   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
4805   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
4806   tree tem;
4807   const char * const warnmsg = G_("assuming signed overflow does not occur "
4808                                   "when simplifying range test");
4809
4810   /* If this is an OR operation, invert both sides; we will invert
4811      again at the end.  */
4812   if (or_op)
4813     in0_p = ! in0_p, in1_p = ! in1_p;
4814
4815   /* If both expressions are the same, if we can merge the ranges, and we
4816      can build the range test, return it or it inverted.  If one of the
4817      ranges is always true or always false, consider it to be the same
4818      expression as the other.  */
4819   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4820       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4821                        in1_p, low1, high1)
4822       && 0 != (tem = (build_range_check (loc, type,
4823                                          lhs != 0 ? lhs
4824                                          : rhs != 0 ? rhs : integer_zero_node,
4825                                          in_p, low, high))))
4826     {
4827       if (strict_overflow_p)
4828         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
4829       return or_op ? invert_truthvalue_loc (loc, tem) : tem;
4830     }
4831
4832   /* On machines where the branch cost is expensive, if this is a
4833      short-circuited branch and the underlying object on both sides
4834      is the same, make a non-short-circuit operation.  */
4835   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4836            && lhs != 0 && rhs != 0
4837            && (code == TRUTH_ANDIF_EXPR
4838                || code == TRUTH_ORIF_EXPR)
4839            && operand_equal_p (lhs, rhs, 0))
4840     {
4841       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
4842          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4843          which cases we can't do this.  */
4844       if (simple_operand_p (lhs))
4845         return build2_loc (loc, code == TRUTH_ANDIF_EXPR
4846                            ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4847                            type, op0, op1);
4848
4849       else if (!lang_hooks.decls.global_bindings_p ()
4850                && !CONTAINS_PLACEHOLDER_P (lhs))
4851         {
4852           tree common = save_expr (lhs);
4853
4854           if (0 != (lhs = build_range_check (loc, type, common,
4855                                              or_op ? ! in0_p : in0_p,
4856                                              low0, high0))
4857               && (0 != (rhs = build_range_check (loc, type, common,
4858                                                  or_op ? ! in1_p : in1_p,
4859                                                  low1, high1))))
4860             {
4861               if (strict_overflow_p)
4862                 fold_overflow_warning (warnmsg,
4863                                        WARN_STRICT_OVERFLOW_COMPARISON);
4864               return build2_loc (loc, code == TRUTH_ANDIF_EXPR
4865                                  ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4866                                  type, lhs, rhs);
4867             }
4868         }
4869     }
4870
4871   return 0;
4872 }
4873 \f
4874 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4875    bit value.  Arrange things so the extra bits will be set to zero if and
4876    only if C is signed-extended to its full width.  If MASK is nonzero,
4877    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
4878
4879 static tree
4880 unextend (tree c, int p, int unsignedp, tree mask)
4881 {
4882   tree type = TREE_TYPE (c);
4883   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4884   tree temp;
4885
4886   if (p == modesize || unsignedp)
4887     return c;
4888
4889   /* We work by getting just the sign bit into the low-order bit, then
4890      into the high-order bit, then sign-extend.  We then XOR that value
4891      with C.  */
4892   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1));
4893   temp = const_binop (BIT_AND_EXPR, temp, size_int (1));
4894
4895   /* We must use a signed type in order to get an arithmetic right shift.
4896      However, we must also avoid introducing accidental overflows, so that
4897      a subsequent call to integer_zerop will work.  Hence we must
4898      do the type conversion here.  At this point, the constant is either
4899      zero or one, and the conversion to a signed type can never overflow.
4900      We could get an overflow if this conversion is done anywhere else.  */
4901   if (TYPE_UNSIGNED (type))
4902     temp = fold_convert (signed_type_for (type), temp);
4903
4904   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
4905   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
4906   if (mask != 0)
4907     temp = const_binop (BIT_AND_EXPR, temp,
4908                         fold_convert (TREE_TYPE (c), mask));
4909   /* If necessary, convert the type back to match the type of C.  */
4910   if (TYPE_UNSIGNED (type))
4911     temp = fold_convert (type, temp);
4912
4913   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
4914 }
4915 \f
4916 /* For an expression that has the form
4917      (A && B) || ~B
4918    or
4919      (A || B) && ~B,
4920    we can drop one of the inner expressions and simplify to
4921      A || ~B
4922    or
4923      A && ~B
4924    LOC is the location of the resulting expression.  OP is the inner 
4925    logical operation; the left-hand side in the examples above, while CMPOP
4926    is the right-hand side.  RHS_ONLY is used to prevent us from accidentally
4927    removing a condition that guards another, as in
4928      (A != NULL && A->...) || A == NULL
4929    which we must not transform.  If RHS_ONLY is true, only eliminate the
4930    right-most operand of the inner logical operation.  */
4931
4932 static tree
4933 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
4934                                  bool rhs_only)
4935 {
4936   tree type = TREE_TYPE (cmpop);
4937   enum tree_code code = TREE_CODE (cmpop);
4938   enum tree_code truthop_code = TREE_CODE (op);
4939   tree lhs = TREE_OPERAND (op, 0);
4940   tree rhs = TREE_OPERAND (op, 1);
4941   tree orig_lhs = lhs, orig_rhs = rhs;
4942   enum tree_code rhs_code = TREE_CODE (rhs);
4943   enum tree_code lhs_code = TREE_CODE (lhs);
4944   enum tree_code inv_code;
4945
4946   if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
4947     return NULL_TREE;
4948
4949   if (TREE_CODE_CLASS (code) != tcc_comparison)
4950     return NULL_TREE;
4951
4952   if (rhs_code == truthop_code)
4953     {
4954       tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
4955       if (newrhs != NULL_TREE)
4956         {
4957           rhs = newrhs;
4958           rhs_code = TREE_CODE (rhs);
4959         }
4960     }
4961   if (lhs_code == truthop_code && !rhs_only)
4962     {
4963       tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
4964       if (newlhs != NULL_TREE)
4965         {
4966           lhs = newlhs;
4967           lhs_code = TREE_CODE (lhs);
4968         }
4969     }
4970
4971   inv_code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
4972   if (inv_code == rhs_code
4973       && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
4974       && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
4975     return lhs;
4976   if (!rhs_only && inv_code == lhs_code
4977       && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
4978       && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
4979     return rhs;
4980   if (rhs != orig_rhs || lhs != orig_lhs)
4981     return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
4982                             lhs, rhs);
4983   return NULL_TREE;
4984 }
4985
4986 /* Find ways of folding logical expressions of LHS and RHS:
4987    Try to merge two comparisons to the same innermost item.
4988    Look for range tests like "ch >= '0' && ch <= '9'".
4989    Look for combinations of simple terms on machines with expensive branches
4990    and evaluate the RHS unconditionally.
4991
4992    For example, if we have p->a == 2 && p->b == 4 and we can make an
4993    object large enough to span both A and B, we can do this with a comparison
4994    against the object ANDed with the a mask.
4995
4996    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
4997    operations to do this with one comparison.
4998
4999    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5000    function and the one above.
5001
5002    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5003    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5004
5005    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5006    two operands.
5007
5008    We return the simplified tree or 0 if no optimization is possible.  */
5009
5010 static tree
5011 fold_truthop (location_t loc, enum tree_code code, tree truth_type,
5012               tree lhs, tree rhs)
5013 {
5014   /* If this is the "or" of two comparisons, we can do something if
5015      the comparisons are NE_EXPR.  If this is the "and", we can do something
5016      if the comparisons are EQ_EXPR.  I.e.,
5017         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5018
5019      WANTED_CODE is this operation code.  For single bit fields, we can
5020      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5021      comparison for one-bit fields.  */
5022
5023   enum tree_code wanted_code;
5024   enum tree_code lcode, rcode;
5025   tree ll_arg, lr_arg, rl_arg, rr_arg;
5026   tree ll_inner, lr_inner, rl_inner, rr_inner;
5027   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5028   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5029   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5030   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5031   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5032   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5033   enum machine_mode lnmode, rnmode;
5034   tree ll_mask, lr_mask, rl_mask, rr_mask;
5035   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5036   tree l_const, r_const;
5037   tree lntype, rntype, result;
5038   HOST_WIDE_INT first_bit, end_bit;
5039   int volatilep;
5040   tree orig_lhs = lhs, orig_rhs = rhs;
5041   enum tree_code orig_code = code;
5042
5043   /* Start by getting the comparison codes.  Fail if anything is volatile.
5044      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5045      it were surrounded with a NE_EXPR.  */
5046
5047   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5048     return 0;
5049
5050   lcode = TREE_CODE (lhs);
5051   rcode = TREE_CODE (rhs);
5052
5053   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5054     {
5055       lhs = build2 (NE_EXPR, truth_type, lhs,
5056                     build_int_cst (TREE_TYPE (lhs), 0));
5057       lcode = NE_EXPR;
5058     }
5059
5060   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5061     {
5062       rhs = build2 (NE_EXPR, truth_type, rhs,
5063                     build_int_cst (TREE_TYPE (rhs), 0));
5064       rcode = NE_EXPR;
5065     }
5066
5067   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5068       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5069     return 0;
5070
5071   ll_arg = TREE_OPERAND (lhs, 0);
5072   lr_arg = TREE_OPERAND (lhs, 1);
5073   rl_arg = TREE_OPERAND (rhs, 0);
5074   rr_arg = TREE_OPERAND (rhs, 1);
5075
5076   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5077   if (simple_operand_p (ll_arg)
5078       && simple_operand_p (lr_arg))
5079     {
5080       if (operand_equal_p (ll_arg, rl_arg, 0)
5081           && operand_equal_p (lr_arg, rr_arg, 0))
5082         {
5083           result = combine_comparisons (loc, code, lcode, rcode,
5084                                         truth_type, ll_arg, lr_arg);
5085           if (result)
5086             return result;
5087         }
5088       else if (operand_equal_p (ll_arg, rr_arg, 0)
5089                && operand_equal_p (lr_arg, rl_arg, 0))
5090         {
5091           result = combine_comparisons (loc, code, lcode,
5092                                         swap_tree_comparison (rcode),
5093                                         truth_type, ll_arg, lr_arg);
5094           if (result)
5095             return result;
5096         }
5097     }
5098
5099   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5100           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5101
5102   /* If the RHS can be evaluated unconditionally and its operands are
5103      simple, it wins to evaluate the RHS unconditionally on machines
5104      with expensive branches.  In this case, this isn't a comparison
5105      that can be merged.  Avoid doing this if the RHS is a floating-point
5106      comparison since those can trap.  */
5107
5108   if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5109                    false) >= 2
5110       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5111       && simple_operand_p (rl_arg)
5112       && simple_operand_p (rr_arg))
5113     {
5114       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5115       if (code == TRUTH_OR_EXPR
5116           && lcode == NE_EXPR && integer_zerop (lr_arg)
5117           && rcode == NE_EXPR && integer_zerop (rr_arg)
5118           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5119           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5120         return build2_loc (loc, NE_EXPR, truth_type,
5121                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5122                                    ll_arg, rl_arg),
5123                            build_int_cst (TREE_TYPE (ll_arg), 0));
5124
5125       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5126       if (code == TRUTH_AND_EXPR
5127           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5128           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5129           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5130           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5131         return build2_loc (loc, EQ_EXPR, truth_type,
5132                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5133                                    ll_arg, rl_arg),
5134                            build_int_cst (TREE_TYPE (ll_arg), 0));
5135
5136       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5137         {
5138           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5139             return build2_loc (loc, code, truth_type, lhs, rhs);
5140           return NULL_TREE;
5141         }
5142     }
5143
5144   /* See if the comparisons can be merged.  Then get all the parameters for
5145      each side.  */
5146
5147   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5148       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5149     return 0;
5150
5151   volatilep = 0;
5152   ll_inner = decode_field_reference (loc, ll_arg,
5153                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5154                                      &ll_unsignedp, &volatilep, &ll_mask,
5155                                      &ll_and_mask);
5156   lr_inner = decode_field_reference (loc, lr_arg,
5157                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5158                                      &lr_unsignedp, &volatilep, &lr_mask,
5159                                      &lr_and_mask);
5160   rl_inner = decode_field_reference (loc, rl_arg,
5161                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5162                                      &rl_unsignedp, &volatilep, &rl_mask,
5163                                      &rl_and_mask);
5164   rr_inner = decode_field_reference (loc, rr_arg,
5165                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5166                                      &rr_unsignedp, &volatilep, &rr_mask,
5167                                      &rr_and_mask);
5168
5169   /* It must be true that the inner operation on the lhs of each
5170      comparison must be the same if we are to be able to do anything.
5171      Then see if we have constants.  If not, the same must be true for
5172      the rhs's.  */
5173   if (volatilep || ll_inner == 0 || rl_inner == 0
5174       || ! operand_equal_p (ll_inner, rl_inner, 0))
5175     return 0;
5176
5177   if (TREE_CODE (lr_arg) == INTEGER_CST
5178       && TREE_CODE (rr_arg) == INTEGER_CST)
5179     l_const = lr_arg, r_const = rr_arg;
5180   else if (lr_inner == 0 || rr_inner == 0
5181            || ! operand_equal_p (lr_inner, rr_inner, 0))
5182     return 0;
5183   else
5184     l_const = r_const = 0;
5185
5186   /* If either comparison code is not correct for our logical operation,
5187      fail.  However, we can convert a one-bit comparison against zero into
5188      the opposite comparison against that bit being set in the field.  */
5189
5190   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5191   if (lcode != wanted_code)
5192     {
5193       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5194         {
5195           /* Make the left operand unsigned, since we are only interested
5196              in the value of one bit.  Otherwise we are doing the wrong
5197              thing below.  */
5198           ll_unsignedp = 1;
5199           l_const = ll_mask;
5200         }
5201       else
5202         return 0;
5203     }
5204
5205   /* This is analogous to the code for l_const above.  */
5206   if (rcode != wanted_code)
5207     {
5208       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5209         {
5210           rl_unsignedp = 1;
5211           r_const = rl_mask;
5212         }
5213       else
5214         return 0;
5215     }
5216
5217   /* See if we can find a mode that contains both fields being compared on
5218      the left.  If we can't, fail.  Otherwise, update all constants and masks
5219      to be relative to a field of that size.  */
5220   first_bit = MIN (ll_bitpos, rl_bitpos);
5221   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5222   lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5223                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5224                           volatilep);
5225   if (lnmode == VOIDmode)
5226     return 0;
5227
5228   lnbitsize = GET_MODE_BITSIZE (lnmode);
5229   lnbitpos = first_bit & ~ (lnbitsize - 1);
5230   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5231   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5232
5233   if (BYTES_BIG_ENDIAN)
5234     {
5235       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5236       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5237     }
5238
5239   ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5240                          size_int (xll_bitpos));
5241   rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5242                          size_int (xrl_bitpos));
5243
5244   if (l_const)
5245     {
5246       l_const = fold_convert_loc (loc, lntype, l_const);
5247       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5248       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5249       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5250                                         fold_build1_loc (loc, BIT_NOT_EXPR,
5251                                                      lntype, ll_mask))))
5252         {
5253           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5254
5255           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5256         }
5257     }
5258   if (r_const)
5259     {
5260       r_const = fold_convert_loc (loc, lntype, r_const);
5261       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5262       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5263       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5264                                         fold_build1_loc (loc, BIT_NOT_EXPR,
5265                                                      lntype, rl_mask))))
5266         {
5267           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5268
5269           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5270         }
5271     }
5272
5273   /* If the right sides are not constant, do the same for it.  Also,
5274      disallow this optimization if a size or signedness mismatch occurs
5275      between the left and right sides.  */
5276   if (l_const == 0)
5277     {
5278       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5279           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5280           /* Make sure the two fields on the right
5281              correspond to the left without being swapped.  */
5282           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5283         return 0;
5284
5285       first_bit = MIN (lr_bitpos, rr_bitpos);
5286       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5287       rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5288                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5289                               volatilep);
5290       if (rnmode == VOIDmode)
5291         return 0;
5292
5293       rnbitsize = GET_MODE_BITSIZE (rnmode);
5294       rnbitpos = first_bit & ~ (rnbitsize - 1);
5295       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5296       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5297
5298       if (BYTES_BIG_ENDIAN)
5299         {
5300           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5301           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5302         }
5303
5304       lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5305                                                             rntype, lr_mask),
5306                              size_int (xlr_bitpos));
5307       rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5308                                                             rntype, rr_mask),
5309                              size_int (xrr_bitpos));
5310
5311       /* Make a mask that corresponds to both fields being compared.
5312          Do this for both items being compared.  If the operands are the
5313          same size and the bits being compared are in the same position
5314          then we can do this by masking both and comparing the masked
5315          results.  */
5316       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5317       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5318       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5319         {
5320           lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5321                                     ll_unsignedp || rl_unsignedp);
5322           if (! all_ones_mask_p (ll_mask, lnbitsize))
5323             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5324
5325           rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5326                                     lr_unsignedp || rr_unsignedp);
5327           if (! all_ones_mask_p (lr_mask, rnbitsize))
5328             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5329
5330           return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5331         }
5332
5333       /* There is still another way we can do something:  If both pairs of
5334          fields being compared are adjacent, we may be able to make a wider
5335          field containing them both.
5336
5337          Note that we still must mask the lhs/rhs expressions.  Furthermore,
5338          the mask must be shifted to account for the shift done by
5339          make_bit_field_ref.  */
5340       if ((ll_bitsize + ll_bitpos == rl_bitpos
5341            && lr_bitsize + lr_bitpos == rr_bitpos)
5342           || (ll_bitpos == rl_bitpos + rl_bitsize
5343               && lr_bitpos == rr_bitpos + rr_bitsize))
5344         {
5345           tree type;
5346
5347           lhs = make_bit_field_ref (loc, ll_inner, lntype,
5348                                     ll_bitsize + rl_bitsize,
5349                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5350           rhs = make_bit_field_ref (loc, lr_inner, rntype,
5351                                     lr_bitsize + rr_bitsize,
5352                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5353
5354           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5355                                  size_int (MIN (xll_bitpos, xrl_bitpos)));
5356           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5357                                  size_int (MIN (xlr_bitpos, xrr_bitpos)));
5358
5359           /* Convert to the smaller type before masking out unwanted bits.  */
5360           type = lntype;
5361           if (lntype != rntype)
5362             {
5363               if (lnbitsize > rnbitsize)
5364                 {
5365                   lhs = fold_convert_loc (loc, rntype, lhs);
5366                   ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5367                   type = rntype;
5368                 }
5369               else if (lnbitsize < rnbitsize)
5370                 {
5371                   rhs = fold_convert_loc (loc, lntype, rhs);
5372                   lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5373                   type = lntype;
5374                 }
5375             }
5376
5377           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5378             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5379
5380           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5381             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5382
5383           return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5384         }
5385
5386       return 0;
5387     }
5388
5389   /* Handle the case of comparisons with constants.  If there is something in
5390      common between the masks, those bits of the constants must be the same.
5391      If not, the condition is always false.  Test for this to avoid generating
5392      incorrect code below.  */
5393   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5394   if (! integer_zerop (result)
5395       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5396                            const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5397     {
5398       if (wanted_code == NE_EXPR)
5399         {
5400           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5401           return constant_boolean_node (true, truth_type);
5402         }
5403       else
5404         {
5405           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5406           return constant_boolean_node (false, truth_type);
5407         }
5408     }
5409
5410   /* Construct the expression we will return.  First get the component
5411      reference we will make.  Unless the mask is all ones the width of
5412      that field, perform the mask operation.  Then compare with the
5413      merged constant.  */
5414   result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5415                                ll_unsignedp || rl_unsignedp);
5416
5417   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5418   if (! all_ones_mask_p (ll_mask, lnbitsize))
5419     result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5420
5421   return build2_loc (loc, wanted_code, truth_type, result,
5422                      const_binop (BIT_IOR_EXPR, l_const, r_const));
5423 }
5424 \f
5425 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5426    constant.  */
5427
5428 static tree
5429 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5430                             tree op0, tree op1)
5431 {
5432   tree arg0 = op0;
5433   enum tree_code op_code;
5434   tree comp_const;
5435   tree minmax_const;
5436   int consts_equal, consts_lt;
5437   tree inner;
5438
5439   STRIP_SIGN_NOPS (arg0);
5440
5441   op_code = TREE_CODE (arg0);
5442   minmax_const = TREE_OPERAND (arg0, 1);
5443   comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5444   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5445   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5446   inner = TREE_OPERAND (arg0, 0);
5447
5448   /* If something does not permit us to optimize, return the original tree.  */
5449   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5450       || TREE_CODE (comp_const) != INTEGER_CST
5451       || TREE_OVERFLOW (comp_const)
5452       || TREE_CODE (minmax_const) != INTEGER_CST
5453       || TREE_OVERFLOW (minmax_const))
5454     return NULL_TREE;
5455
5456   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5457      and GT_EXPR, doing the rest with recursive calls using logical
5458      simplifications.  */
5459   switch (code)
5460     {
5461     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5462       {
5463         tree tem
5464           = optimize_minmax_comparison (loc,
5465                                         invert_tree_comparison (code, false),
5466                                         type, op0, op1);
5467         if (tem)
5468           return invert_truthvalue_loc (loc, tem);
5469         return NULL_TREE;
5470       }
5471
5472     case GE_EXPR:
5473       return
5474         fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5475                      optimize_minmax_comparison
5476                      (loc, EQ_EXPR, type, arg0, comp_const),
5477                      optimize_minmax_comparison
5478                      (loc, GT_EXPR, type, arg0, comp_const));
5479
5480     case EQ_EXPR:
5481       if (op_code == MAX_EXPR && consts_equal)
5482         /* MAX (X, 0) == 0  ->  X <= 0  */
5483         return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5484
5485       else if (op_code == MAX_EXPR && consts_lt)
5486         /* MAX (X, 0) == 5  ->  X == 5   */
5487         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5488
5489       else if (op_code == MAX_EXPR)
5490         /* MAX (X, 0) == -1  ->  false  */
5491         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5492
5493       else if (consts_equal)
5494         /* MIN (X, 0) == 0  ->  X >= 0  */
5495         return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5496
5497       else if (consts_lt)
5498         /* MIN (X, 0) == 5  ->  false  */
5499         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5500
5501       else
5502         /* MIN (X, 0) == -1  ->  X == -1  */
5503         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5504
5505     case GT_EXPR:
5506       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5507         /* MAX (X, 0) > 0  ->  X > 0
5508            MAX (X, 0) > 5  ->  X > 5  */
5509         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5510
5511       else if (op_code == MAX_EXPR)
5512         /* MAX (X, 0) > -1  ->  true  */
5513         return omit_one_operand_loc (loc, type, integer_one_node, inner);
5514
5515       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5516         /* MIN (X, 0) > 0  ->  false
5517            MIN (X, 0) > 5  ->  false  */
5518         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5519
5520       else
5521         /* MIN (X, 0) > -1  ->  X > -1  */
5522         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5523
5524     default:
5525       return NULL_TREE;
5526     }
5527 }
5528 \f
5529 /* T is an integer expression that is being multiplied, divided, or taken a
5530    modulus (CODE says which and what kind of divide or modulus) by a
5531    constant C.  See if we can eliminate that operation by folding it with
5532    other operations already in T.  WIDE_TYPE, if non-null, is a type that
5533    should be used for the computation if wider than our type.
5534
5535    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5536    (X * 2) + (Y * 4).  We must, however, be assured that either the original
5537    expression would not overflow or that overflow is undefined for the type
5538    in the language in question.
5539
5540    If we return a non-null expression, it is an equivalent form of the
5541    original computation, but need not be in the original type.
5542
5543    We set *STRICT_OVERFLOW_P to true if the return values depends on
5544    signed overflow being undefined.  Otherwise we do not change
5545    *STRICT_OVERFLOW_P.  */
5546
5547 static tree
5548 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5549                 bool *strict_overflow_p)
5550 {
5551   /* To avoid exponential search depth, refuse to allow recursion past
5552      three levels.  Beyond that (1) it's highly unlikely that we'll find
5553      something interesting and (2) we've probably processed it before
5554      when we built the inner expression.  */
5555
5556   static int depth;
5557   tree ret;
5558
5559   if (depth > 3)
5560     return NULL;
5561
5562   depth++;
5563   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5564   depth--;
5565
5566   return ret;
5567 }
5568
5569 static tree
5570 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5571                   bool *strict_overflow_p)
5572 {
5573   tree type = TREE_TYPE (t);
5574   enum tree_code tcode = TREE_CODE (t);
5575   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5576                                    > GET_MODE_SIZE (TYPE_MODE (type)))
5577                 ? wide_type : type);
5578   tree t1, t2;
5579   int same_p = tcode == code;
5580   tree op0 = NULL_TREE, op1 = NULL_TREE;
5581   bool sub_strict_overflow_p;
5582
5583   /* Don't deal with constants of zero here; they confuse the code below.  */
5584   if (integer_zerop (c))
5585     return NULL_TREE;
5586
5587   if (TREE_CODE_CLASS (tcode) == tcc_unary)
5588     op0 = TREE_OPERAND (t, 0);
5589
5590   if (TREE_CODE_CLASS (tcode) == tcc_binary)
5591     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5592
5593   /* Note that we need not handle conditional operations here since fold
5594      already handles those cases.  So just do arithmetic here.  */
5595   switch (tcode)
5596     {
5597     case INTEGER_CST:
5598       /* For a constant, we can always simplify if we are a multiply
5599          or (for divide and modulus) if it is a multiple of our constant.  */
5600       if (code == MULT_EXPR
5601           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c)))
5602         return const_binop (code, fold_convert (ctype, t),
5603                             fold_convert (ctype, c));
5604       break;
5605
5606     CASE_CONVERT: case NON_LVALUE_EXPR:
5607       /* If op0 is an expression ...  */
5608       if ((COMPARISON_CLASS_P (op0)
5609            || UNARY_CLASS_P (op0)
5610            || BINARY_CLASS_P (op0)
5611            || VL_EXP_CLASS_P (op0)
5612            || EXPRESSION_CLASS_P (op0))
5613           /* ... and has wrapping overflow, and its type is smaller
5614              than ctype, then we cannot pass through as widening.  */
5615           && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
5616                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5617                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5618                && (TYPE_PRECISION (ctype)
5619                    > TYPE_PRECISION (TREE_TYPE (op0))))
5620               /* ... or this is a truncation (t is narrower than op0),
5621                  then we cannot pass through this narrowing.  */
5622               || (TYPE_PRECISION (type)
5623                   < TYPE_PRECISION (TREE_TYPE (op0)))
5624               /* ... or signedness changes for division or modulus,
5625                  then we cannot pass through this conversion.  */
5626               || (code != MULT_EXPR
5627                   && (TYPE_UNSIGNED (ctype)
5628                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
5629               /* ... or has undefined overflow while the converted to
5630                  type has not, we cannot do the operation in the inner type
5631                  as that would introduce undefined overflow.  */
5632               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5633                   && !TYPE_OVERFLOW_UNDEFINED (type))))
5634         break;
5635
5636       /* Pass the constant down and see if we can make a simplification.  If
5637          we can, replace this expression with the inner simplification for
5638          possible later conversion to our or some other type.  */
5639       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5640           && TREE_CODE (t2) == INTEGER_CST
5641           && !TREE_OVERFLOW (t2)
5642           && (0 != (t1 = extract_muldiv (op0, t2, code,
5643                                          code == MULT_EXPR
5644                                          ? ctype : NULL_TREE,
5645                                          strict_overflow_p))))
5646         return t1;
5647       break;
5648
5649     case ABS_EXPR:
5650       /* If widening the type changes it from signed to unsigned, then we
5651          must avoid building ABS_EXPR itself as unsigned.  */
5652       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5653         {
5654           tree cstype = (*signed_type_for) (ctype);
5655           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5656               != 0)
5657             {
5658               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5659               return fold_convert (ctype, t1);
5660             }
5661           break;
5662         }
5663       /* If the constant is negative, we cannot simplify this.  */
5664       if (tree_int_cst_sgn (c) == -1)
5665         break;
5666       /* FALLTHROUGH */
5667     case NEGATE_EXPR:
5668       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5669           != 0)
5670         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5671       break;
5672
5673     case MIN_EXPR:  case MAX_EXPR:
5674       /* If widening the type changes the signedness, then we can't perform
5675          this optimization as that changes the result.  */
5676       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5677         break;
5678
5679       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
5680       sub_strict_overflow_p = false;
5681       if ((t1 = extract_muldiv (op0, c, code, wide_type,
5682                                 &sub_strict_overflow_p)) != 0
5683           && (t2 = extract_muldiv (op1, c, code, wide_type,
5684                                    &sub_strict_overflow_p)) != 0)
5685         {
5686           if (tree_int_cst_sgn (c) < 0)
5687             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5688           if (sub_strict_overflow_p)
5689             *strict_overflow_p = true;
5690           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5691                               fold_convert (ctype, t2));
5692         }
5693       break;
5694
5695     case LSHIFT_EXPR:  case RSHIFT_EXPR:
5696       /* If the second operand is constant, this is a multiplication
5697          or floor division, by a power of two, so we can treat it that
5698          way unless the multiplier or divisor overflows.  Signed
5699          left-shift overflow is implementation-defined rather than
5700          undefined in C90, so do not convert signed left shift into
5701          multiplication.  */
5702       if (TREE_CODE (op1) == INTEGER_CST
5703           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5704           /* const_binop may not detect overflow correctly,
5705              so check for it explicitly here.  */
5706           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5707           && TREE_INT_CST_HIGH (op1) == 0
5708           && 0 != (t1 = fold_convert (ctype,
5709                                       const_binop (LSHIFT_EXPR,
5710                                                    size_one_node,
5711                                                    op1)))
5712           && !TREE_OVERFLOW (t1))
5713         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5714                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
5715                                        ctype,
5716                                        fold_convert (ctype, op0),
5717                                        t1),
5718                                c, code, wide_type, strict_overflow_p);
5719       break;
5720
5721     case PLUS_EXPR:  case MINUS_EXPR:
5722       /* See if we can eliminate the operation on both sides.  If we can, we
5723          can return a new PLUS or MINUS.  If we can't, the only remaining
5724          cases where we can do anything are if the second operand is a
5725          constant.  */
5726       sub_strict_overflow_p = false;
5727       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5728       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5729       if (t1 != 0 && t2 != 0
5730           && (code == MULT_EXPR
5731               /* If not multiplication, we can only do this if both operands
5732                  are divisible by c.  */
5733               || (multiple_of_p (ctype, op0, c)
5734                   && multiple_of_p (ctype, op1, c))))
5735         {
5736           if (sub_strict_overflow_p)
5737             *strict_overflow_p = true;
5738           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5739                               fold_convert (ctype, t2));
5740         }
5741
5742       /* If this was a subtraction, negate OP1 and set it to be an addition.
5743          This simplifies the logic below.  */
5744       if (tcode == MINUS_EXPR)
5745         {
5746           tcode = PLUS_EXPR, op1 = negate_expr (op1);
5747           /* If OP1 was not easily negatable, the constant may be OP0.  */
5748           if (TREE_CODE (op0) == INTEGER_CST)
5749             {
5750               tree tem = op0;
5751               op0 = op1;
5752               op1 = tem;
5753               tem = t1;
5754               t1 = t2;
5755               t2 = tem;
5756             }
5757         }
5758
5759       if (TREE_CODE (op1) != INTEGER_CST)
5760         break;
5761
5762       /* If either OP1 or C are negative, this optimization is not safe for
5763          some of the division and remainder types while for others we need
5764          to change the code.  */
5765       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5766         {
5767           if (code == CEIL_DIV_EXPR)
5768             code = FLOOR_DIV_EXPR;
5769           else if (code == FLOOR_DIV_EXPR)
5770             code = CEIL_DIV_EXPR;
5771           else if (code != MULT_EXPR
5772                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5773             break;
5774         }
5775
5776       /* If it's a multiply or a division/modulus operation of a multiple
5777          of our constant, do the operation and verify it doesn't overflow.  */
5778       if (code == MULT_EXPR
5779           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5780         {
5781           op1 = const_binop (code, fold_convert (ctype, op1),
5782                              fold_convert (ctype, c));
5783           /* We allow the constant to overflow with wrapping semantics.  */
5784           if (op1 == 0
5785               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5786             break;
5787         }
5788       else
5789         break;
5790
5791       /* If we have an unsigned type is not a sizetype, we cannot widen
5792          the operation since it will change the result if the original
5793          computation overflowed.  */
5794       if (TYPE_UNSIGNED (ctype)
5795           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5796           && ctype != type)
5797         break;
5798
5799       /* If we were able to eliminate our operation from the first side,
5800          apply our operation to the second side and reform the PLUS.  */
5801       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5802         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5803
5804       /* The last case is if we are a multiply.  In that case, we can
5805          apply the distributive law to commute the multiply and addition
5806          if the multiplication of the constants doesn't overflow.  */
5807       if (code == MULT_EXPR)
5808         return fold_build2 (tcode, ctype,
5809                             fold_build2 (code, ctype,
5810                                          fold_convert (ctype, op0),
5811                                          fold_convert (ctype, c)),
5812                             op1);
5813
5814       break;
5815
5816     case MULT_EXPR:
5817       /* We have a special case here if we are doing something like
5818          (C * 8) % 4 since we know that's zero.  */
5819       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5820            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5821           /* If the multiplication can overflow we cannot optimize this.
5822              ???  Until we can properly mark individual operations as
5823              not overflowing we need to treat sizetype special here as
5824              stor-layout relies on this opimization to make
5825              DECL_FIELD_BIT_OFFSET always a constant.  */
5826           && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
5827               || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
5828                   && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
5829           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5830           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5831         {
5832           *strict_overflow_p = true;
5833           return omit_one_operand (type, integer_zero_node, op0);
5834         }
5835
5836       /* ... fall through ...  */
5837
5838     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
5839     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
5840       /* If we can extract our operation from the LHS, do so and return a
5841          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
5842          do something only if the second operand is a constant.  */
5843       if (same_p
5844           && (t1 = extract_muldiv (op0, c, code, wide_type,
5845                                    strict_overflow_p)) != 0)
5846         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5847                             fold_convert (ctype, op1));
5848       else if (tcode == MULT_EXPR && code == MULT_EXPR
5849                && (t1 = extract_muldiv (op1, c, code, wide_type,
5850                                         strict_overflow_p)) != 0)
5851         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5852                             fold_convert (ctype, t1));
5853       else if (TREE_CODE (op1) != INTEGER_CST)
5854         return 0;
5855
5856       /* If these are the same operation types, we can associate them
5857          assuming no overflow.  */
5858       if (tcode == code)
5859         {
5860           double_int mul;
5861           int overflow_p;
5862           mul = double_int_mul_with_sign
5863                   (double_int_ext
5864                      (tree_to_double_int (op1),
5865                       TYPE_PRECISION (ctype), TYPE_UNSIGNED (ctype)),
5866                    double_int_ext
5867                      (tree_to_double_int (c),
5868                       TYPE_PRECISION (ctype), TYPE_UNSIGNED (ctype)),
5869                    false, &overflow_p);
5870           overflow_p = (((!TYPE_UNSIGNED (ctype)
5871                           || (TREE_CODE (ctype) == INTEGER_TYPE
5872                               && TYPE_IS_SIZETYPE (ctype)))
5873                          && overflow_p)
5874                         | TREE_OVERFLOW (c) | TREE_OVERFLOW (op1));
5875           if (!double_int_fits_to_tree_p (ctype, mul)
5876               && ((TYPE_UNSIGNED (ctype) && tcode != MULT_EXPR)
5877                   || !TYPE_UNSIGNED (ctype)
5878                   || (TREE_CODE (ctype) == INTEGER_TYPE
5879                       && TYPE_IS_SIZETYPE (ctype))))
5880             overflow_p = 1;
5881           if (!overflow_p)
5882             return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5883                                 double_int_to_tree (ctype, mul));
5884         }
5885
5886       /* If these operations "cancel" each other, we have the main
5887          optimizations of this pass, which occur when either constant is a
5888          multiple of the other, in which case we replace this with either an
5889          operation or CODE or TCODE.
5890
5891          If we have an unsigned type that is not a sizetype, we cannot do
5892          this since it will change the result if the original computation
5893          overflowed.  */
5894       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
5895            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5896           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5897               || (tcode == MULT_EXPR
5898                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5899                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
5900                   && code != MULT_EXPR)))
5901         {
5902           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5903             {
5904               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5905                 *strict_overflow_p = true;
5906               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5907                                   fold_convert (ctype,
5908                                                 const_binop (TRUNC_DIV_EXPR,
5909                                                              op1, c)));
5910             }
5911           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1)))
5912             {
5913               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5914                 *strict_overflow_p = true;
5915               return fold_build2 (code, ctype, fold_convert (ctype, op0),
5916                                   fold_convert (ctype,
5917                                                 const_binop (TRUNC_DIV_EXPR,
5918                                                              c, op1)));
5919             }
5920         }
5921       break;
5922
5923     default:
5924       break;
5925     }
5926
5927   return 0;
5928 }
5929 \f
5930 /* Return a node which has the indicated constant VALUE (either 0 or
5931    1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
5932    and is of the indicated TYPE.  */
5933
5934 tree
5935 constant_boolean_node (bool value, tree type)
5936 {
5937   if (type == integer_type_node)
5938     return value ? integer_one_node : integer_zero_node;
5939   else if (type == boolean_type_node)
5940     return value ? boolean_true_node : boolean_false_node;
5941   else if (TREE_CODE (type) == VECTOR_TYPE)
5942     return build_vector_from_val (type,
5943                                   build_int_cst (TREE_TYPE (type),
5944                                                  value ? -1 : 0));
5945   else
5946     return fold_convert (type, value ? integer_one_node : integer_zero_node);
5947 }
5948
5949
5950 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
5951    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
5952    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
5953    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
5954    COND is the first argument to CODE; otherwise (as in the example
5955    given here), it is the second argument.  TYPE is the type of the
5956    original expression.  Return NULL_TREE if no simplification is
5957    possible.  */
5958
5959 static tree
5960 fold_binary_op_with_conditional_arg (location_t loc,
5961                                      enum tree_code code,
5962                                      tree type, tree op0, tree op1,
5963                                      tree cond, tree arg, int cond_first_p)
5964 {
5965   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
5966   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
5967   tree test, true_value, false_value;
5968   tree lhs = NULL_TREE;
5969   tree rhs = NULL_TREE;
5970
5971   if (TREE_CODE (cond) == COND_EXPR)
5972     {
5973       test = TREE_OPERAND (cond, 0);
5974       true_value = TREE_OPERAND (cond, 1);
5975       false_value = TREE_OPERAND (cond, 2);
5976       /* If this operand throws an expression, then it does not make
5977          sense to try to perform a logical or arithmetic operation
5978          involving it.  */
5979       if (VOID_TYPE_P (TREE_TYPE (true_value)))
5980         lhs = true_value;
5981       if (VOID_TYPE_P (TREE_TYPE (false_value)))
5982         rhs = false_value;
5983     }
5984   else
5985     {
5986       tree testtype = TREE_TYPE (cond);
5987       test = cond;
5988       true_value = constant_boolean_node (true, testtype);
5989       false_value = constant_boolean_node (false, testtype);
5990     }
5991
5992   /* This transformation is only worthwhile if we don't have to wrap ARG
5993      in a SAVE_EXPR and the operation can be simplified on at least one
5994      of the branches once its pushed inside the COND_EXPR.  */
5995   if (!TREE_CONSTANT (arg)
5996       && (TREE_SIDE_EFFECTS (arg)
5997           || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
5998     return NULL_TREE;
5999
6000   arg = fold_convert_loc (loc, arg_type, arg);
6001   if (lhs == 0)
6002     {
6003       true_value = fold_convert_loc (loc, cond_type, true_value);
6004       if (cond_first_p)
6005         lhs = fold_build2_loc (loc, code, type, true_value, arg);
6006       else
6007         lhs = fold_build2_loc (loc, code, type, arg, true_value);
6008     }
6009   if (rhs == 0)
6010     {
6011       false_value = fold_convert_loc (loc, cond_type, false_value);
6012       if (cond_first_p)
6013         rhs = fold_build2_loc (loc, code, type, false_value, arg);
6014       else
6015         rhs = fold_build2_loc (loc, code, type, arg, false_value);
6016     }
6017
6018   /* Check that we have simplified at least one of the branches.  */
6019   if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6020     return NULL_TREE;
6021
6022   return fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs);
6023 }
6024
6025 \f
6026 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6027
6028    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6029    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6030    ADDEND is the same as X.
6031
6032    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6033    and finite.  The problematic cases are when X is zero, and its mode
6034    has signed zeros.  In the case of rounding towards -infinity,
6035    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6036    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6037
6038 bool
6039 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6040 {
6041   if (!real_zerop (addend))
6042     return false;
6043
6044   /* Don't allow the fold with -fsignaling-nans.  */
6045   if (HONOR_SNANS (TYPE_MODE (type)))
6046     return false;
6047
6048   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6049   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6050     return true;
6051
6052   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6053   if (TREE_CODE (addend) == REAL_CST
6054       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6055     negate = !negate;
6056
6057   /* The mode has signed zeros, and we have to honor their sign.
6058      In this situation, there is only one case we can return true for.
6059      X - 0 is the same as X unless rounding towards -infinity is
6060      supported.  */
6061   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6062 }
6063
6064 /* Subroutine of fold() that checks comparisons of built-in math
6065    functions against real constants.
6066
6067    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6068    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6069    is the type of the result and ARG0 and ARG1 are the operands of the
6070    comparison.  ARG1 must be a TREE_REAL_CST.
6071
6072    The function returns the constant folded tree if a simplification
6073    can be made, and NULL_TREE otherwise.  */
6074
6075 static tree
6076 fold_mathfn_compare (location_t loc,
6077                      enum built_in_function fcode, enum tree_code code,
6078                      tree type, tree arg0, tree arg1)
6079 {
6080   REAL_VALUE_TYPE c;
6081
6082   if (BUILTIN_SQRT_P (fcode))
6083     {
6084       tree arg = CALL_EXPR_ARG (arg0, 0);
6085       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6086
6087       c = TREE_REAL_CST (arg1);
6088       if (REAL_VALUE_NEGATIVE (c))
6089         {
6090           /* sqrt(x) < y is always false, if y is negative.  */
6091           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6092             return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6093
6094           /* sqrt(x) > y is always true, if y is negative and we
6095              don't care about NaNs, i.e. negative values of x.  */
6096           if (code == NE_EXPR || !HONOR_NANS (mode))
6097             return omit_one_operand_loc (loc, type, integer_one_node, arg);
6098
6099           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6100           return fold_build2_loc (loc, GE_EXPR, type, arg,
6101                               build_real (TREE_TYPE (arg), dconst0));
6102         }
6103       else if (code == GT_EXPR || code == GE_EXPR)
6104         {
6105           REAL_VALUE_TYPE c2;
6106
6107           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6108           real_convert (&c2, mode, &c2);
6109
6110           if (REAL_VALUE_ISINF (c2))
6111             {
6112               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6113               if (HONOR_INFINITIES (mode))
6114                 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6115                                     build_real (TREE_TYPE (arg), c2));
6116
6117               /* sqrt(x) > y is always false, when y is very large
6118                  and we don't care about infinities.  */
6119               return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6120             }
6121
6122           /* sqrt(x) > c is the same as x > c*c.  */
6123           return fold_build2_loc (loc, code, type, arg,
6124                               build_real (TREE_TYPE (arg), c2));
6125         }
6126       else if (code == LT_EXPR || code == LE_EXPR)
6127         {
6128           REAL_VALUE_TYPE c2;
6129
6130           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6131           real_convert (&c2, mode, &c2);
6132
6133           if (REAL_VALUE_ISINF (c2))
6134             {
6135               /* sqrt(x) < y is always true, when y is a very large
6136                  value and we don't care about NaNs or Infinities.  */
6137               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6138                 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6139
6140               /* sqrt(x) < y is x != +Inf when y is very large and we
6141                  don't care about NaNs.  */
6142               if (! HONOR_NANS (mode))
6143                 return fold_build2_loc (loc, NE_EXPR, type, arg,
6144                                     build_real (TREE_TYPE (arg), c2));
6145
6146               /* sqrt(x) < y is x >= 0 when y is very large and we
6147                  don't care about Infinities.  */
6148               if (! HONOR_INFINITIES (mode))
6149                 return fold_build2_loc (loc, GE_EXPR, type, arg,
6150                                     build_real (TREE_TYPE (arg), dconst0));
6151
6152               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6153               arg = save_expr (arg);
6154               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6155                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6156                                                build_real (TREE_TYPE (arg),
6157                                                            dconst0)),
6158                                   fold_build2_loc (loc, NE_EXPR, type, arg,
6159                                                build_real (TREE_TYPE (arg),
6160                                                            c2)));
6161             }
6162
6163           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6164           if (! HONOR_NANS (mode))
6165             return fold_build2_loc (loc, code, type, arg,
6166                                 build_real (TREE_TYPE (arg), c2));
6167
6168           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6169           arg = save_expr (arg);
6170           return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6171                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6172                                                build_real (TREE_TYPE (arg),
6173                                                            dconst0)),
6174                                   fold_build2_loc (loc, code, type, arg,
6175                                                build_real (TREE_TYPE (arg),
6176                                                            c2)));
6177         }
6178     }
6179
6180   return NULL_TREE;
6181 }
6182
6183 /* Subroutine of fold() that optimizes comparisons against Infinities,
6184    either +Inf or -Inf.
6185
6186    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6187    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6188    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6189
6190    The function returns the constant folded tree if a simplification
6191    can be made, and NULL_TREE otherwise.  */
6192
6193 static tree
6194 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6195                   tree arg0, tree arg1)
6196 {
6197   enum machine_mode mode;
6198   REAL_VALUE_TYPE max;
6199   tree temp;
6200   bool neg;
6201
6202   mode = TYPE_MODE (TREE_TYPE (arg0));
6203
6204   /* For negative infinity swap the sense of the comparison.  */
6205   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6206   if (neg)
6207     code = swap_tree_comparison (code);
6208
6209   switch (code)
6210     {
6211     case GT_EXPR:
6212       /* x > +Inf is always false, if with ignore sNANs.  */
6213       if (HONOR_SNANS (mode))
6214         return NULL_TREE;
6215       return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6216
6217     case LE_EXPR:
6218       /* x <= +Inf is always true, if we don't case about NaNs.  */
6219       if (! HONOR_NANS (mode))
6220         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6221
6222       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6223       arg0 = save_expr (arg0);
6224       return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6225
6226     case EQ_EXPR:
6227     case GE_EXPR:
6228       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6229       real_maxval (&max, neg, mode);
6230       return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6231                           arg0, build_real (TREE_TYPE (arg0), max));
6232
6233     case LT_EXPR:
6234       /* x < +Inf is always equal to x <= DBL_MAX.  */
6235       real_maxval (&max, neg, mode);
6236       return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6237                           arg0, build_real (TREE_TYPE (arg0), max));
6238
6239     case NE_EXPR:
6240       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6241       real_maxval (&max, neg, mode);
6242       if (! HONOR_NANS (mode))
6243         return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6244                             arg0, build_real (TREE_TYPE (arg0), max));
6245
6246       temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6247                           arg0, build_real (TREE_TYPE (arg0), max));
6248       return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
6249
6250     default:
6251       break;
6252     }
6253
6254   return NULL_TREE;
6255 }
6256
6257 /* Subroutine of fold() that optimizes comparisons of a division by
6258    a nonzero integer constant against an integer constant, i.e.
6259    X/C1 op C2.
6260
6261    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6262    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6263    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6264
6265    The function returns the constant folded tree if a simplification
6266    can be made, and NULL_TREE otherwise.  */
6267
6268 static tree
6269 fold_div_compare (location_t loc,
6270                   enum tree_code code, tree type, tree arg0, tree arg1)
6271 {
6272   tree prod, tmp, hi, lo;
6273   tree arg00 = TREE_OPERAND (arg0, 0);
6274   tree arg01 = TREE_OPERAND (arg0, 1);
6275   double_int val;
6276   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6277   bool neg_overflow;
6278   int overflow;
6279
6280   /* We have to do this the hard way to detect unsigned overflow.
6281      prod = int_const_binop (MULT_EXPR, arg01, arg1);  */
6282   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6283                                    TREE_INT_CST_HIGH (arg01),
6284                                    TREE_INT_CST_LOW (arg1),
6285                                    TREE_INT_CST_HIGH (arg1),
6286                                    &val.low, &val.high, unsigned_p);
6287   prod = force_fit_type_double (TREE_TYPE (arg00), val, -1, overflow);
6288   neg_overflow = false;
6289
6290   if (unsigned_p)
6291     {
6292       tmp = int_const_binop (MINUS_EXPR, arg01,
6293                              build_int_cst (TREE_TYPE (arg01), 1));
6294       lo = prod;
6295
6296       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp).  */
6297       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6298                                        TREE_INT_CST_HIGH (prod),
6299                                        TREE_INT_CST_LOW (tmp),
6300                                        TREE_INT_CST_HIGH (tmp),
6301                                        &val.low, &val.high, unsigned_p);
6302       hi = force_fit_type_double (TREE_TYPE (arg00), val,
6303                                   -1, overflow | TREE_OVERFLOW (prod));
6304     }
6305   else if (tree_int_cst_sgn (arg01) >= 0)
6306     {
6307       tmp = int_const_binop (MINUS_EXPR, arg01,
6308                              build_int_cst (TREE_TYPE (arg01), 1));
6309       switch (tree_int_cst_sgn (arg1))
6310         {
6311         case -1:
6312           neg_overflow = true;
6313           lo = int_const_binop (MINUS_EXPR, prod, tmp);
6314           hi = prod;
6315           break;
6316
6317         case  0:
6318           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6319           hi = tmp;
6320           break;
6321
6322         case  1:
6323           hi = int_const_binop (PLUS_EXPR, prod, tmp);
6324           lo = prod;
6325           break;
6326
6327         default:
6328           gcc_unreachable ();
6329         }
6330     }
6331   else
6332     {
6333       /* A negative divisor reverses the relational operators.  */
6334       code = swap_tree_comparison (code);
6335
6336       tmp = int_const_binop (PLUS_EXPR, arg01,
6337                              build_int_cst (TREE_TYPE (arg01), 1));
6338       switch (tree_int_cst_sgn (arg1))
6339         {
6340         case -1:
6341           hi = int_const_binop (MINUS_EXPR, prod, tmp);
6342           lo = prod;
6343           break;
6344
6345         case  0:
6346           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6347           lo = tmp;
6348           break;
6349
6350         case  1:
6351           neg_overflow = true;
6352           lo = int_const_binop (PLUS_EXPR, prod, tmp);
6353           hi = prod;
6354           break;
6355
6356         default:
6357           gcc_unreachable ();
6358         }
6359     }
6360
6361   switch (code)
6362     {
6363     case EQ_EXPR:
6364       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6365         return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6366       if (TREE_OVERFLOW (hi))
6367         return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6368       if (TREE_OVERFLOW (lo))
6369         return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6370       return build_range_check (loc, type, arg00, 1, lo, hi);
6371
6372     case NE_EXPR:
6373       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6374         return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6375       if (TREE_OVERFLOW (hi))
6376         return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6377       if (TREE_OVERFLOW (lo))
6378         return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6379       return build_range_check (loc, type, arg00, 0, lo, hi);
6380
6381     case LT_EXPR:
6382       if (TREE_OVERFLOW (lo))
6383         {
6384           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6385           return omit_one_operand_loc (loc, type, tmp, arg00);
6386         }
6387       return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6388
6389     case LE_EXPR:
6390       if (TREE_OVERFLOW (hi))
6391         {
6392           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6393           return omit_one_operand_loc (loc, type, tmp, arg00);
6394         }
6395       return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6396
6397     case GT_EXPR:
6398       if (TREE_OVERFLOW (hi))
6399         {
6400           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6401           return omit_one_operand_loc (loc, type, tmp, arg00);
6402         }
6403       return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6404
6405     case GE_EXPR:
6406       if (TREE_OVERFLOW (lo))
6407         {
6408           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6409           return omit_one_operand_loc (loc, type, tmp, arg00);
6410         }
6411       return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6412
6413     default:
6414       break;
6415     }
6416
6417   return NULL_TREE;
6418 }
6419
6420
6421 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6422    equality/inequality test, then return a simplified form of the test
6423    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6424    result type.  */
6425
6426 static tree
6427 fold_single_bit_test_into_sign_test (location_t loc,
6428                                      enum tree_code code, tree arg0, tree arg1,
6429                                      tree result_type)
6430 {
6431   /* If this is testing a single bit, we can optimize the test.  */
6432   if ((code == NE_EXPR || code == EQ_EXPR)
6433       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6434       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6435     {
6436       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6437          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6438       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6439
6440       if (arg00 != NULL_TREE
6441           /* This is only a win if casting to a signed type is cheap,
6442              i.e. when arg00's type is not a partial mode.  */
6443           && TYPE_PRECISION (TREE_TYPE (arg00))
6444              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6445         {
6446           tree stype = signed_type_for (TREE_TYPE (arg00));
6447           return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6448                               result_type,
6449                               fold_convert_loc (loc, stype, arg00),
6450                               build_int_cst (stype, 0));
6451         }
6452     }
6453
6454   return NULL_TREE;
6455 }
6456
6457 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6458    equality/inequality test, then return a simplified form of
6459    the test using shifts and logical operations.  Otherwise return
6460    NULL.  TYPE is the desired result type.  */
6461
6462 tree
6463 fold_single_bit_test (location_t loc, enum tree_code code,
6464                       tree arg0, tree arg1, tree result_type)
6465 {
6466   /* If this is testing a single bit, we can optimize the test.  */
6467   if ((code == NE_EXPR || code == EQ_EXPR)
6468       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6469       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6470     {
6471       tree inner = TREE_OPERAND (arg0, 0);
6472       tree type = TREE_TYPE (arg0);
6473       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6474       enum machine_mode operand_mode = TYPE_MODE (type);
6475       int ops_unsigned;
6476       tree signed_type, unsigned_type, intermediate_type;
6477       tree tem, one;
6478
6479       /* First, see if we can fold the single bit test into a sign-bit
6480          test.  */
6481       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6482                                                  result_type);
6483       if (tem)
6484         return tem;
6485
6486       /* Otherwise we have (A & C) != 0 where C is a single bit,
6487          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6488          Similarly for (A & C) == 0.  */
6489
6490       /* If INNER is a right shift of a constant and it plus BITNUM does
6491          not overflow, adjust BITNUM and INNER.  */
6492       if (TREE_CODE (inner) == RSHIFT_EXPR
6493           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6494           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6495           && bitnum < TYPE_PRECISION (type)
6496           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6497                                    bitnum - TYPE_PRECISION (type)))
6498         {
6499           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6500           inner = TREE_OPERAND (inner, 0);
6501         }
6502
6503       /* If we are going to be able to omit the AND below, we must do our
6504          operations as unsigned.  If we must use the AND, we have a choice.
6505          Normally unsigned is faster, but for some machines signed is.  */
6506 #ifdef LOAD_EXTEND_OP
6507       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6508                       && !flag_syntax_only) ? 0 : 1;
6509 #else
6510       ops_unsigned = 1;
6511 #endif
6512
6513       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6514       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6515       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6516       inner = fold_convert_loc (loc, intermediate_type, inner);
6517
6518       if (bitnum != 0)
6519         inner = build2 (RSHIFT_EXPR, intermediate_type,
6520                         inner, size_int (bitnum));
6521
6522       one = build_int_cst (intermediate_type, 1);
6523
6524       if (code == EQ_EXPR)
6525         inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6526
6527       /* Put the AND last so it can combine with more things.  */
6528       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6529
6530       /* Make sure to return the proper type.  */
6531       inner = fold_convert_loc (loc, result_type, inner);
6532
6533       return inner;
6534     }
6535   return NULL_TREE;
6536 }
6537
6538 /* Check whether we are allowed to reorder operands arg0 and arg1,
6539    such that the evaluation of arg1 occurs before arg0.  */
6540
6541 static bool
6542 reorder_operands_p (const_tree arg0, const_tree arg1)
6543 {
6544   if (! flag_evaluation_order)
6545       return true;
6546   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6547     return true;
6548   return ! TREE_SIDE_EFFECTS (arg0)
6549          && ! TREE_SIDE_EFFECTS (arg1);
6550 }
6551
6552 /* Test whether it is preferable two swap two operands, ARG0 and
6553    ARG1, for example because ARG0 is an integer constant and ARG1
6554    isn't.  If REORDER is true, only recommend swapping if we can
6555    evaluate the operands in reverse order.  */
6556
6557 bool
6558 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6559 {
6560   STRIP_SIGN_NOPS (arg0);
6561   STRIP_SIGN_NOPS (arg1);
6562
6563   if (TREE_CODE (arg1) == INTEGER_CST)
6564     return 0;
6565   if (TREE_CODE (arg0) == INTEGER_CST)
6566     return 1;
6567
6568   if (TREE_CODE (arg1) == REAL_CST)
6569     return 0;
6570   if (TREE_CODE (arg0) == REAL_CST)
6571     return 1;
6572
6573   if (TREE_CODE (arg1) == FIXED_CST)
6574     return 0;
6575   if (TREE_CODE (arg0) == FIXED_CST)
6576     return 1;
6577
6578   if (TREE_CODE (arg1) == COMPLEX_CST)
6579     return 0;
6580   if (TREE_CODE (arg0) == COMPLEX_CST)
6581     return 1;
6582
6583   if (TREE_CONSTANT (arg1))
6584     return 0;
6585   if (TREE_CONSTANT (arg0))
6586     return 1;
6587
6588   if (optimize_function_for_size_p (cfun))
6589     return 0;
6590
6591   if (reorder && flag_evaluation_order
6592       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6593     return 0;
6594
6595   /* It is preferable to swap two SSA_NAME to ensure a canonical form
6596      for commutative and comparison operators.  Ensuring a canonical
6597      form allows the optimizers to find additional redundancies without
6598      having to explicitly check for both orderings.  */
6599   if (TREE_CODE (arg0) == SSA_NAME
6600       && TREE_CODE (arg1) == SSA_NAME
6601       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6602     return 1;
6603
6604   /* Put SSA_NAMEs last.  */
6605   if (TREE_CODE (arg1) == SSA_NAME)
6606     return 0;
6607   if (TREE_CODE (arg0) == SSA_NAME)
6608     return 1;
6609
6610   /* Put variables last.  */
6611   if (DECL_P (arg1))
6612     return 0;
6613   if (DECL_P (arg0))
6614     return 1;
6615
6616   return 0;
6617 }
6618
6619 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6620    ARG0 is extended to a wider type.  */
6621
6622 static tree
6623 fold_widened_comparison (location_t loc, enum tree_code code,
6624                          tree type, tree arg0, tree arg1)
6625 {
6626   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6627   tree arg1_unw;
6628   tree shorter_type, outer_type;
6629   tree min, max;
6630   bool above, below;
6631
6632   if (arg0_unw == arg0)
6633     return NULL_TREE;
6634   shorter_type = TREE_TYPE (arg0_unw);
6635
6636 #ifdef HAVE_canonicalize_funcptr_for_compare
6637   /* Disable this optimization if we're casting a function pointer
6638      type on targets that require function pointer canonicalization.  */
6639   if (HAVE_canonicalize_funcptr_for_compare
6640       && TREE_CODE (shorter_type) == POINTER_TYPE
6641       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6642     return NULL_TREE;
6643 #endif
6644
6645   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6646     return NULL_TREE;
6647
6648   arg1_unw = get_unwidened (arg1, NULL_TREE);
6649
6650   /* If possible, express the comparison in the shorter mode.  */
6651   if ((code == EQ_EXPR || code == NE_EXPR
6652        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6653       && (TREE_TYPE (arg1_unw) == shorter_type
6654           || ((TYPE_PRECISION (shorter_type)
6655                >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6656               && (TYPE_UNSIGNED (shorter_type)
6657                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6658           || (TREE_CODE (arg1_unw) == INTEGER_CST
6659               && (TREE_CODE (shorter_type) == INTEGER_TYPE
6660                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6661               && int_fits_type_p (arg1_unw, shorter_type))))
6662     return fold_build2_loc (loc, code, type, arg0_unw,
6663                         fold_convert_loc (loc, shorter_type, arg1_unw));
6664
6665   if (TREE_CODE (arg1_unw) != INTEGER_CST
6666       || TREE_CODE (shorter_type) != INTEGER_TYPE
6667       || !int_fits_type_p (arg1_unw, shorter_type))
6668     return NULL_TREE;
6669
6670   /* If we are comparing with the integer that does not fit into the range
6671      of the shorter type, the result is known.  */
6672   outer_type = TREE_TYPE (arg1_unw);
6673   min = lower_bound_in_type (outer_type, shorter_type);
6674   max = upper_bound_in_type (outer_type, shorter_type);
6675
6676   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6677                                                    max, arg1_unw));
6678   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6679                                                    arg1_unw, min));
6680
6681   switch (code)
6682     {
6683     case EQ_EXPR:
6684       if (above || below)
6685         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6686       break;
6687
6688     case NE_EXPR:
6689       if (above || below)
6690         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6691       break;
6692
6693     case LT_EXPR:
6694     case LE_EXPR:
6695       if (above)
6696         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6697       else if (below)
6698         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6699
6700     case GT_EXPR:
6701     case GE_EXPR:
6702       if (above)
6703         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6704       else if (below)
6705         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6706
6707     default:
6708       break;
6709     }
6710
6711   return NULL_TREE;
6712 }
6713
6714 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6715    ARG0 just the signedness is changed.  */
6716
6717 static tree
6718 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
6719                               tree arg0, tree arg1)
6720 {
6721   tree arg0_inner;
6722   tree inner_type, outer_type;
6723
6724   if (!CONVERT_EXPR_P (arg0))
6725     return NULL_TREE;
6726
6727   outer_type = TREE_TYPE (arg0);
6728   arg0_inner = TREE_OPERAND (arg0, 0);
6729   inner_type = TREE_TYPE (arg0_inner);
6730
6731 #ifdef HAVE_canonicalize_funcptr_for_compare
6732   /* Disable this optimization if we're casting a function pointer
6733      type on targets that require function pointer canonicalization.  */
6734   if (HAVE_canonicalize_funcptr_for_compare
6735       && TREE_CODE (inner_type) == POINTER_TYPE
6736       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6737     return NULL_TREE;
6738 #endif
6739
6740   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6741     return NULL_TREE;
6742
6743   if (TREE_CODE (arg1) != INTEGER_CST
6744       && !(CONVERT_EXPR_P (arg1)
6745            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6746     return NULL_TREE;
6747
6748   if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6749        || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
6750       && code != NE_EXPR
6751       && code != EQ_EXPR)
6752     return NULL_TREE;
6753
6754   if (TREE_CODE (arg1) == INTEGER_CST)
6755     arg1 = force_fit_type_double (inner_type, tree_to_double_int (arg1),
6756                                   0, TREE_OVERFLOW (arg1));
6757   else
6758     arg1 = fold_convert_loc (loc, inner_type, arg1);
6759
6760   return fold_build2_loc (loc, code, type, arg0_inner, arg1);
6761 }
6762
6763 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6764    step of the array.  Reconstructs s and delta in the case of s *
6765    delta being an integer constant (and thus already folded).  ADDR is
6766    the address. MULT is the multiplicative expression.  If the
6767    function succeeds, the new address expression is returned.
6768    Otherwise NULL_TREE is returned.  LOC is the location of the
6769    resulting expression.  */
6770
6771 static tree
6772 try_move_mult_to_index (location_t loc, tree addr, tree op1)
6773 {
6774   tree s, delta, step;
6775   tree ref = TREE_OPERAND (addr, 0), pref;
6776   tree ret, pos;
6777   tree itype;
6778   bool mdim = false;
6779
6780   /*  Strip the nops that might be added when converting op1 to sizetype. */
6781   STRIP_NOPS (op1);
6782
6783   /* Canonicalize op1 into a possibly non-constant delta
6784      and an INTEGER_CST s.  */
6785   if (TREE_CODE (op1) == MULT_EXPR)
6786     {
6787       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6788
6789       STRIP_NOPS (arg0);
6790       STRIP_NOPS (arg1);
6791
6792       if (TREE_CODE (arg0) == INTEGER_CST)
6793         {
6794           s = arg0;
6795           delta = arg1;
6796         }
6797       else if (TREE_CODE (arg1) == INTEGER_CST)
6798         {
6799           s = arg1;
6800           delta = arg0;
6801         }
6802       else
6803         return NULL_TREE;
6804     }
6805   else if (TREE_CODE (op1) == INTEGER_CST)
6806     {
6807       delta = op1;
6808       s = NULL_TREE;
6809     }
6810   else
6811     {
6812       /* Simulate we are delta * 1.  */
6813       delta = op1;
6814       s = integer_one_node;
6815     }
6816
6817   for (;; ref = TREE_OPERAND (ref, 0))
6818     {
6819       if (TREE_CODE (ref) == ARRAY_REF)
6820         {
6821           tree domain;
6822
6823           /* Remember if this was a multi-dimensional array.  */
6824           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6825             mdim = true;
6826
6827           domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6828           if (! domain)
6829             continue;
6830           itype = TREE_TYPE (domain);
6831
6832           step = array_ref_element_size (ref);
6833           if (TREE_CODE (step) != INTEGER_CST)
6834             continue;
6835
6836           if (s)
6837             {
6838               if (! tree_int_cst_equal (step, s))
6839                 continue;
6840             }
6841           else
6842             {
6843               /* Try if delta is a multiple of step.  */
6844               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6845               if (! tmp)
6846                 continue;
6847               delta = tmp;
6848             }
6849
6850           /* Only fold here if we can verify we do not overflow one
6851              dimension of a multi-dimensional array.  */
6852           if (mdim)
6853             {
6854               tree tmp;
6855
6856               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
6857                   || !TYPE_MAX_VALUE (domain)
6858                   || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
6859                 continue;
6860
6861               tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
6862                                      fold_convert_loc (loc, itype,
6863                                                        TREE_OPERAND (ref, 1)),
6864                                      fold_convert_loc (loc, itype, delta));
6865               if (!tmp
6866                   || TREE_CODE (tmp) != INTEGER_CST
6867                   || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
6868                 continue;
6869             }
6870
6871           break;
6872         }
6873       else if (TREE_CODE (ref) == COMPONENT_REF
6874                && TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE)
6875         {
6876           tree domain;
6877
6878           /* Remember if this was a multi-dimensional array.  */
6879           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6880             mdim = true;
6881
6882           domain = TYPE_DOMAIN (TREE_TYPE (ref));
6883           if (! domain)
6884             continue;
6885           itype = TREE_TYPE (domain);
6886
6887           step = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ref)));
6888           if (TREE_CODE (step) != INTEGER_CST)
6889             continue;
6890
6891           if (s)
6892             {
6893               if (! tree_int_cst_equal (step, s))
6894                 continue;
6895             }
6896           else
6897             {
6898               /* Try if delta is a multiple of step.  */
6899               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6900               if (! tmp)
6901                 continue;
6902               delta = tmp;
6903             }
6904
6905           /* Only fold here if we can verify we do not overflow one
6906              dimension of a multi-dimensional array.  */
6907           if (mdim)
6908             {
6909               tree tmp;
6910
6911               if (!TYPE_MIN_VALUE (domain)
6912                   || !TYPE_MAX_VALUE (domain)
6913                   || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
6914                 continue;
6915
6916               tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
6917                                      fold_convert_loc (loc, itype,
6918                                                        TYPE_MIN_VALUE (domain)),
6919                                      fold_convert_loc (loc, itype, delta));
6920               if (TREE_CODE (tmp) != INTEGER_CST
6921                   || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
6922                 continue;
6923             }
6924
6925           break;
6926         }
6927       else
6928         mdim = false;
6929
6930       if (!handled_component_p (ref))
6931         return NULL_TREE;
6932     }
6933
6934   /* We found the suitable array reference.  So copy everything up to it,
6935      and replace the index.  */
6936
6937   pref = TREE_OPERAND (addr, 0);
6938   ret = copy_node (pref);
6939   SET_EXPR_LOCATION (ret, loc);
6940   pos = ret;
6941
6942   while (pref != ref)
6943     {
6944       pref = TREE_OPERAND (pref, 0);
6945       TREE_OPERAND (pos, 0) = copy_node (pref);
6946       pos = TREE_OPERAND (pos, 0);
6947     }
6948
6949   if (TREE_CODE (ref) == ARRAY_REF)
6950     {
6951       TREE_OPERAND (pos, 1)
6952         = fold_build2_loc (loc, PLUS_EXPR, itype,
6953                            fold_convert_loc (loc, itype, TREE_OPERAND (pos, 1)),
6954                            fold_convert_loc (loc, itype, delta));
6955       return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
6956     }
6957   else if (TREE_CODE (ref) == COMPONENT_REF)
6958     {
6959       gcc_assert (ret == pos);
6960       ret = build4_loc (loc, ARRAY_REF, TREE_TYPE (TREE_TYPE (ref)), ret,
6961                         fold_build2_loc
6962                           (loc, PLUS_EXPR, itype,
6963                            fold_convert_loc (loc, itype,
6964                                              TYPE_MIN_VALUE
6965                                                (TYPE_DOMAIN (TREE_TYPE (ref)))),
6966                            fold_convert_loc (loc, itype, delta)),
6967                         NULL_TREE, NULL_TREE);
6968       return build_fold_addr_expr_loc (loc, ret);
6969     }
6970   else
6971     gcc_unreachable ();
6972 }
6973
6974
6975 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
6976    means A >= Y && A != MAX, but in this case we know that
6977    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
6978
6979 static tree
6980 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6981 {
6982   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6983
6984   if (TREE_CODE (bound) == LT_EXPR)
6985     a = TREE_OPERAND (bound, 0);
6986   else if (TREE_CODE (bound) == GT_EXPR)
6987     a = TREE_OPERAND (bound, 1);
6988   else
6989     return NULL_TREE;
6990
6991   typea = TREE_TYPE (a);
6992   if (!INTEGRAL_TYPE_P (typea)
6993       && !POINTER_TYPE_P (typea))
6994     return NULL_TREE;
6995
6996   if (TREE_CODE (ineq) == LT_EXPR)
6997     {
6998       a1 = TREE_OPERAND (ineq, 1);
6999       y = TREE_OPERAND (ineq, 0);
7000     }
7001   else if (TREE_CODE (ineq) == GT_EXPR)
7002     {
7003       a1 = TREE_OPERAND (ineq, 0);
7004       y = TREE_OPERAND (ineq, 1);
7005     }
7006   else
7007     return NULL_TREE;
7008
7009   if (TREE_TYPE (a1) != typea)
7010     return NULL_TREE;
7011
7012   if (POINTER_TYPE_P (typea))
7013     {
7014       /* Convert the pointer types into integer before taking the difference.  */
7015       tree ta = fold_convert_loc (loc, ssizetype, a);
7016       tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7017       diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7018     }
7019   else
7020     diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7021
7022   if (!diff || !integer_onep (diff))
7023    return NULL_TREE;
7024
7025   return fold_build2_loc (loc, GE_EXPR, type, a, y);
7026 }
7027
7028 /* Fold a sum or difference of at least one multiplication.
7029    Returns the folded tree or NULL if no simplification could be made.  */
7030
7031 static tree
7032 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7033                           tree arg0, tree arg1)
7034 {
7035   tree arg00, arg01, arg10, arg11;
7036   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7037
7038   /* (A * C) +- (B * C) -> (A+-B) * C.
7039      (A * C) +- A -> A * (C+-1).
7040      We are most concerned about the case where C is a constant,
7041      but other combinations show up during loop reduction.  Since
7042      it is not difficult, try all four possibilities.  */
7043
7044   if (TREE_CODE (arg0) == MULT_EXPR)
7045     {
7046       arg00 = TREE_OPERAND (arg0, 0);
7047       arg01 = TREE_OPERAND (arg0, 1);
7048     }
7049   else if (TREE_CODE (arg0) == INTEGER_CST)
7050     {
7051       arg00 = build_one_cst (type);
7052       arg01 = arg0;
7053     }
7054   else
7055     {
7056       /* We cannot generate constant 1 for fract.  */
7057       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7058         return NULL_TREE;
7059       arg00 = arg0;
7060       arg01 = build_one_cst (type);
7061     }
7062   if (TREE_CODE (arg1) == MULT_EXPR)
7063     {
7064       arg10 = TREE_OPERAND (arg1, 0);
7065       arg11 = TREE_OPERAND (arg1, 1);
7066     }
7067   else if (TREE_CODE (arg1) == INTEGER_CST)
7068     {
7069       arg10 = build_one_cst (type);
7070       /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7071          the purpose of this canonicalization.  */
7072       if (TREE_INT_CST_HIGH (arg1) == -1
7073           && negate_expr_p (arg1)
7074           && code == PLUS_EXPR)
7075         {
7076           arg11 = negate_expr (arg1);
7077           code = MINUS_EXPR;
7078         }
7079       else
7080         arg11 = arg1;
7081     }
7082   else
7083     {
7084       /* We cannot generate constant 1 for fract.  */
7085       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7086         return NULL_TREE;
7087       arg10 = arg1;
7088       arg11 = build_one_cst (type);
7089     }
7090   same = NULL_TREE;
7091
7092   if (operand_equal_p (arg01, arg11, 0))
7093     same = arg01, alt0 = arg00, alt1 = arg10;
7094   else if (operand_equal_p (arg00, arg10, 0))
7095     same = arg00, alt0 = arg01, alt1 = arg11;
7096   else if (operand_equal_p (arg00, arg11, 0))
7097     same = arg00, alt0 = arg01, alt1 = arg10;
7098   else if (operand_equal_p (arg01, arg10, 0))
7099     same = arg01, alt0 = arg00, alt1 = arg11;
7100
7101   /* No identical multiplicands; see if we can find a common
7102      power-of-two factor in non-power-of-two multiplies.  This
7103      can help in multi-dimensional array access.  */
7104   else if (host_integerp (arg01, 0)
7105            && host_integerp (arg11, 0))
7106     {
7107       HOST_WIDE_INT int01, int11, tmp;
7108       bool swap = false;
7109       tree maybe_same;
7110       int01 = TREE_INT_CST_LOW (arg01);
7111       int11 = TREE_INT_CST_LOW (arg11);
7112
7113       /* Move min of absolute values to int11.  */
7114       if (absu_hwi (int01) < absu_hwi (int11))
7115         {
7116           tmp = int01, int01 = int11, int11 = tmp;
7117           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7118           maybe_same = arg01;
7119           swap = true;
7120         }
7121       else
7122         maybe_same = arg11;
7123
7124       if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7125           /* The remainder should not be a constant, otherwise we
7126              end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7127              increased the number of multiplications necessary.  */
7128           && TREE_CODE (arg10) != INTEGER_CST)
7129         {
7130           alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7131                               build_int_cst (TREE_TYPE (arg00),
7132                                              int01 / int11));
7133           alt1 = arg10;
7134           same = maybe_same;
7135           if (swap)
7136             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7137         }
7138     }
7139
7140   if (same)
7141     return fold_build2_loc (loc, MULT_EXPR, type,
7142                         fold_build2_loc (loc, code, type,
7143                                      fold_convert_loc (loc, type, alt0),
7144                                      fold_convert_loc (loc, type, alt1)),
7145                         fold_convert_loc (loc, type, same));
7146
7147   return NULL_TREE;
7148 }
7149
7150 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7151    specified by EXPR into the buffer PTR of length LEN bytes.
7152    Return the number of bytes placed in the buffer, or zero
7153    upon failure.  */
7154
7155 static int
7156 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7157 {
7158   tree type = TREE_TYPE (expr);
7159   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7160   int byte, offset, word, words;
7161   unsigned char value;
7162
7163   if (total_bytes > len)
7164     return 0;
7165   words = total_bytes / UNITS_PER_WORD;
7166
7167   for (byte = 0; byte < total_bytes; byte++)
7168     {
7169       int bitpos = byte * BITS_PER_UNIT;
7170       if (bitpos < HOST_BITS_PER_WIDE_INT)
7171         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7172       else
7173         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7174                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7175
7176       if (total_bytes > UNITS_PER_WORD)
7177         {
7178           word = byte / UNITS_PER_WORD;
7179           if (WORDS_BIG_ENDIAN)
7180             word = (words - 1) - word;
7181           offset = word * UNITS_PER_WORD;
7182           if (BYTES_BIG_ENDIAN)
7183             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7184           else
7185             offset += byte % UNITS_PER_WORD;
7186         }
7187       else
7188         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7189       ptr[offset] = value;
7190     }
7191   return total_bytes;
7192 }
7193
7194
7195 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7196    specified by EXPR into the buffer PTR of length LEN bytes.
7197    Return the number of bytes placed in the buffer, or zero
7198    upon failure.  */
7199
7200 static int
7201 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7202 {
7203   tree type = TREE_TYPE (expr);
7204   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7205   int byte, offset, word, words, bitpos;
7206   unsigned char value;
7207
7208   /* There are always 32 bits in each long, no matter the size of
7209      the hosts long.  We handle floating point representations with
7210      up to 192 bits.  */
7211   long tmp[6];
7212
7213   if (total_bytes > len)
7214     return 0;
7215   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7216
7217   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7218
7219   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7220        bitpos += BITS_PER_UNIT)
7221     {
7222       byte = (bitpos / BITS_PER_UNIT) & 3;
7223       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7224
7225       if (UNITS_PER_WORD < 4)
7226         {
7227           word = byte / UNITS_PER_WORD;
7228           if (WORDS_BIG_ENDIAN)
7229             word = (words - 1) - word;
7230           offset = word * UNITS_PER_WORD;
7231           if (BYTES_BIG_ENDIAN)
7232             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7233           else
7234             offset += byte % UNITS_PER_WORD;
7235         }
7236       else
7237         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7238       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7239     }
7240   return total_bytes;
7241 }
7242
7243 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7244    specified by EXPR into the buffer PTR of length LEN bytes.
7245    Return the number of bytes placed in the buffer, or zero
7246    upon failure.  */
7247
7248 static int
7249 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7250 {
7251   int rsize, isize;
7252   tree part;
7253
7254   part = TREE_REALPART (expr);
7255   rsize = native_encode_expr (part, ptr, len);
7256   if (rsize == 0)
7257     return 0;
7258   part = TREE_IMAGPART (expr);
7259   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7260   if (isize != rsize)
7261     return 0;
7262   return rsize + isize;
7263 }
7264
7265
7266 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7267    specified by EXPR into the buffer PTR of length LEN bytes.
7268    Return the number of bytes placed in the buffer, or zero
7269    upon failure.  */
7270
7271 static int
7272 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7273 {
7274   int i, size, offset, count;
7275   tree itype, elem, elements;
7276
7277   offset = 0;
7278   elements = TREE_VECTOR_CST_ELTS (expr);
7279   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7280   itype = TREE_TYPE (TREE_TYPE (expr));
7281   size = GET_MODE_SIZE (TYPE_MODE (itype));
7282   for (i = 0; i < count; i++)
7283     {
7284       if (elements)
7285         {
7286           elem = TREE_VALUE (elements);
7287           elements = TREE_CHAIN (elements);
7288         }
7289       else
7290         elem = NULL_TREE;
7291
7292       if (elem)
7293         {
7294           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7295             return 0;
7296         }
7297       else
7298         {
7299           if (offset + size > len)
7300             return 0;
7301           memset (ptr+offset, 0, size);
7302         }
7303       offset += size;
7304     }
7305   return offset;
7306 }
7307
7308
7309 /* Subroutine of native_encode_expr.  Encode the STRING_CST
7310    specified by EXPR into the buffer PTR of length LEN bytes.
7311    Return the number of bytes placed in the buffer, or zero
7312    upon failure.  */
7313
7314 static int
7315 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7316 {
7317   tree type = TREE_TYPE (expr);
7318   HOST_WIDE_INT total_bytes;
7319
7320   if (TREE_CODE (type) != ARRAY_TYPE
7321       || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7322       || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7323       || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7324     return 0;
7325   total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7326   if (total_bytes > len)
7327     return 0;
7328   if (TREE_STRING_LENGTH (expr) < total_bytes)
7329     {
7330       memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7331       memset (ptr + TREE_STRING_LENGTH (expr), 0,
7332               total_bytes - TREE_STRING_LENGTH (expr));
7333     }
7334   else
7335     memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7336   return total_bytes;
7337 }
7338
7339
7340 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7341    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7342    buffer PTR of length LEN bytes.  Return the number of bytes
7343    placed in the buffer, or zero upon failure.  */
7344
7345 int
7346 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7347 {
7348   switch (TREE_CODE (expr))
7349     {
7350     case INTEGER_CST:
7351       return native_encode_int (expr, ptr, len);
7352
7353     case REAL_CST:
7354       return native_encode_real (expr, ptr, len);
7355
7356     case COMPLEX_CST:
7357       return native_encode_complex (expr, ptr, len);
7358
7359     case VECTOR_CST:
7360       return native_encode_vector (expr, ptr, len);
7361
7362     case STRING_CST:
7363       return native_encode_string (expr, ptr, len);
7364
7365     default:
7366       return 0;
7367     }
7368 }
7369
7370
7371 /* Subroutine of native_interpret_expr.  Interpret the contents of
7372    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7373    If the buffer cannot be interpreted, return NULL_TREE.  */
7374
7375 static tree
7376 native_interpret_int (tree type, const unsigned char *ptr, int len)
7377 {
7378   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7379   int byte, offset, word, words;
7380   unsigned char value;
7381   double_int result;
7382
7383   if (total_bytes > len)
7384     return NULL_TREE;
7385   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7386     return NULL_TREE;
7387
7388   result = double_int_zero;
7389   words = total_bytes / UNITS_PER_WORD;
7390
7391   for (byte = 0; byte < total_bytes; byte++)
7392     {
7393       int bitpos = byte * BITS_PER_UNIT;
7394       if (total_bytes > UNITS_PER_WORD)
7395         {
7396           word = byte / UNITS_PER_WORD;
7397           if (WORDS_BIG_ENDIAN)
7398             word = (words - 1) - word;
7399           offset = word * UNITS_PER_WORD;
7400           if (BYTES_BIG_ENDIAN)
7401             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7402           else
7403             offset += byte % UNITS_PER_WORD;
7404         }
7405       else
7406         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7407       value = ptr[offset];
7408
7409       if (bitpos < HOST_BITS_PER_WIDE_INT)
7410         result.low |= (unsigned HOST_WIDE_INT) value << bitpos;
7411       else
7412         result.high |= (unsigned HOST_WIDE_INT) value
7413                        << (bitpos - HOST_BITS_PER_WIDE_INT);
7414     }
7415
7416   return double_int_to_tree (type, result);
7417 }
7418
7419
7420 /* Subroutine of native_interpret_expr.  Interpret the contents of
7421    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7422    If the buffer cannot be interpreted, return NULL_TREE.  */
7423
7424 static tree
7425 native_interpret_real (tree type, const unsigned char *ptr, int len)
7426 {
7427   enum machine_mode mode = TYPE_MODE (type);
7428   int total_bytes = GET_MODE_SIZE (mode);
7429   int byte, offset, word, words, bitpos;
7430   unsigned char value;
7431   /* There are always 32 bits in each long, no matter the size of
7432      the hosts long.  We handle floating point representations with
7433      up to 192 bits.  */
7434   REAL_VALUE_TYPE r;
7435   long tmp[6];
7436
7437   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7438   if (total_bytes > len || total_bytes > 24)
7439     return NULL_TREE;
7440   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7441
7442   memset (tmp, 0, sizeof (tmp));
7443   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7444        bitpos += BITS_PER_UNIT)
7445     {
7446       byte = (bitpos / BITS_PER_UNIT) & 3;
7447       if (UNITS_PER_WORD < 4)
7448         {
7449           word = byte / UNITS_PER_WORD;
7450           if (WORDS_BIG_ENDIAN)
7451             word = (words - 1) - word;
7452           offset = word * UNITS_PER_WORD;
7453           if (BYTES_BIG_ENDIAN)
7454             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7455           else
7456             offset += byte % UNITS_PER_WORD;
7457         }
7458       else
7459         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7460       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7461
7462       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7463     }
7464
7465   real_from_target (&r, tmp, mode);
7466   return build_real (type, r);
7467 }
7468
7469
7470 /* Subroutine of native_interpret_expr.  Interpret the contents of
7471    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7472    If the buffer cannot be interpreted, return NULL_TREE.  */
7473
7474 static tree
7475 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7476 {
7477   tree etype, rpart, ipart;
7478   int size;
7479
7480   etype = TREE_TYPE (type);
7481   size = GET_MODE_SIZE (TYPE_MODE (etype));
7482   if (size * 2 > len)
7483     return NULL_TREE;
7484   rpart = native_interpret_expr (etype, ptr, size);
7485   if (!rpart)
7486     return NULL_TREE;
7487   ipart = native_interpret_expr (etype, ptr+size, size);
7488   if (!ipart)
7489     return NULL_TREE;
7490   return build_complex (type, rpart, ipart);
7491 }
7492
7493
7494 /* Subroutine of native_interpret_expr.  Interpret the contents of
7495    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7496    If the buffer cannot be interpreted, return NULL_TREE.  */
7497
7498 static tree
7499 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7500 {
7501   tree etype, elem, elements;
7502   int i, size, count;
7503
7504   etype = TREE_TYPE (type);
7505   size = GET_MODE_SIZE (TYPE_MODE (etype));
7506   count = TYPE_VECTOR_SUBPARTS (type);
7507   if (size * count > len)
7508     return NULL_TREE;
7509
7510   elements = NULL_TREE;
7511   for (i = count - 1; i >= 0; i--)
7512     {
7513       elem = native_interpret_expr (etype, ptr+(i*size), size);
7514       if (!elem)
7515         return NULL_TREE;
7516       elements = tree_cons (NULL_TREE, elem, elements);
7517     }
7518   return build_vector (type, elements);
7519 }
7520
7521
7522 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7523    the buffer PTR of length LEN as a constant of type TYPE.  For
7524    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7525    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7526    return NULL_TREE.  */
7527
7528 tree
7529 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7530 {
7531   switch (TREE_CODE (type))
7532     {
7533     case INTEGER_TYPE:
7534     case ENUMERAL_TYPE:
7535     case BOOLEAN_TYPE:
7536       return native_interpret_int (type, ptr, len);
7537
7538     case REAL_TYPE:
7539       return native_interpret_real (type, ptr, len);
7540
7541     case COMPLEX_TYPE:
7542       return native_interpret_complex (type, ptr, len);
7543
7544     case VECTOR_TYPE:
7545       return native_interpret_vector (type, ptr, len);
7546
7547     default:
7548       return NULL_TREE;
7549     }
7550 }
7551
7552
7553 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7554    TYPE at compile-time.  If we're unable to perform the conversion
7555    return NULL_TREE.  */
7556
7557 static tree
7558 fold_view_convert_expr (tree type, tree expr)
7559 {
7560   /* We support up to 512-bit values (for V8DFmode).  */
7561   unsigned char buffer[64];
7562   int len;
7563
7564   /* Check that the host and target are sane.  */
7565   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7566     return NULL_TREE;
7567
7568   len = native_encode_expr (expr, buffer, sizeof (buffer));
7569   if (len == 0)
7570     return NULL_TREE;
7571
7572   return native_interpret_expr (type, buffer, len);
7573 }
7574
7575 /* Build an expression for the address of T.  Folds away INDIRECT_REF
7576    to avoid confusing the gimplify process.  */
7577
7578 tree
7579 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7580 {
7581   /* The size of the object is not relevant when talking about its address.  */
7582   if (TREE_CODE (t) == WITH_SIZE_EXPR)
7583     t = TREE_OPERAND (t, 0);
7584
7585   if (TREE_CODE (t) == INDIRECT_REF)
7586     {
7587       t = TREE_OPERAND (t, 0);
7588
7589       if (TREE_TYPE (t) != ptrtype)
7590         t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7591     }
7592   else if (TREE_CODE (t) == MEM_REF
7593       && integer_zerop (TREE_OPERAND (t, 1)))
7594     return TREE_OPERAND (t, 0);
7595   else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7596     {
7597       t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7598
7599       if (TREE_TYPE (t) != ptrtype)
7600         t = fold_convert_loc (loc, ptrtype, t);
7601     }
7602   else
7603     t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7604
7605   return t;
7606 }
7607
7608 /* Build an expression for the address of T.  */
7609
7610 tree
7611 build_fold_addr_expr_loc (location_t loc, tree t)
7612 {
7613   tree ptrtype = build_pointer_type (TREE_TYPE (t));
7614
7615   return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7616 }
7617
7618 /* Fold a unary expression of code CODE and type TYPE with operand
7619    OP0.  Return the folded expression if folding is successful.
7620    Otherwise, return NULL_TREE.  */
7621
7622 tree
7623 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7624 {
7625   tree tem;
7626   tree arg0;
7627   enum tree_code_class kind = TREE_CODE_CLASS (code);
7628
7629   gcc_assert (IS_EXPR_CODE_CLASS (kind)
7630               && TREE_CODE_LENGTH (code) == 1);
7631
7632   arg0 = op0;
7633   if (arg0)
7634     {
7635       if (CONVERT_EXPR_CODE_P (code)
7636           || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7637         {
7638           /* Don't use STRIP_NOPS, because signedness of argument type
7639              matters.  */
7640           STRIP_SIGN_NOPS (arg0);
7641         }
7642       else
7643         {
7644           /* Strip any conversions that don't change the mode.  This
7645              is safe for every expression, except for a comparison
7646              expression because its signedness is derived from its
7647              operands.
7648
7649              Note that this is done as an internal manipulation within
7650              the constant folder, in order to find the simplest
7651              representation of the arguments so that their form can be
7652              studied.  In any cases, the appropriate type conversions
7653              should be put back in the tree that will get out of the
7654              constant folder.  */
7655           STRIP_NOPS (arg0);
7656         }
7657     }
7658
7659   if (TREE_CODE_CLASS (code) == tcc_unary)
7660     {
7661       if (TREE_CODE (arg0) == COMPOUND_EXPR)
7662         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7663                        fold_build1_loc (loc, code, type,
7664                                     fold_convert_loc (loc, TREE_TYPE (op0),
7665                                                       TREE_OPERAND (arg0, 1))));
7666       else if (TREE_CODE (arg0) == COND_EXPR)
7667         {
7668           tree arg01 = TREE_OPERAND (arg0, 1);
7669           tree arg02 = TREE_OPERAND (arg0, 2);
7670           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7671             arg01 = fold_build1_loc (loc, code, type,
7672                                  fold_convert_loc (loc,
7673                                                    TREE_TYPE (op0), arg01));
7674           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7675             arg02 = fold_build1_loc (loc, code, type,
7676                                  fold_convert_loc (loc,
7677                                                    TREE_TYPE (op0), arg02));
7678           tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7679                              arg01, arg02);
7680
7681           /* If this was a conversion, and all we did was to move into
7682              inside the COND_EXPR, bring it back out.  But leave it if
7683              it is a conversion from integer to integer and the
7684              result precision is no wider than a word since such a
7685              conversion is cheap and may be optimized away by combine,
7686              while it couldn't if it were outside the COND_EXPR.  Then return
7687              so we don't get into an infinite recursion loop taking the
7688              conversion out and then back in.  */
7689
7690           if ((CONVERT_EXPR_CODE_P (code)
7691                || code == NON_LVALUE_EXPR)
7692               && TREE_CODE (tem) == COND_EXPR
7693               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7694               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7695               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7696               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7697               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7698                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7699               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7700                      && (INTEGRAL_TYPE_P
7701                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7702                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7703                   || flag_syntax_only))
7704             tem = build1_loc (loc, code, type,
7705                               build3 (COND_EXPR,
7706                                       TREE_TYPE (TREE_OPERAND
7707                                                  (TREE_OPERAND (tem, 1), 0)),
7708                                       TREE_OPERAND (tem, 0),
7709                                       TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7710                                       TREE_OPERAND (TREE_OPERAND (tem, 2),
7711                                                     0)));
7712           return tem;
7713         }
7714    }
7715
7716   switch (code)
7717     {
7718     case PAREN_EXPR:
7719       /* Re-association barriers around constants and other re-association
7720          barriers can be removed.  */
7721       if (CONSTANT_CLASS_P (op0)
7722           || TREE_CODE (op0) == PAREN_EXPR)
7723         return fold_convert_loc (loc, type, op0);
7724       return NULL_TREE;
7725
7726     CASE_CONVERT:
7727     case FLOAT_EXPR:
7728     case FIX_TRUNC_EXPR:
7729       if (TREE_TYPE (op0) == type)
7730         return op0;
7731
7732       if (COMPARISON_CLASS_P (op0))
7733         {
7734           /* If we have (type) (a CMP b) and type is an integral type, return
7735              new expression involving the new type.  Canonicalize
7736              (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7737              non-integral type.
7738              Do not fold the result as that would not simplify further, also
7739              folding again results in recursions.  */
7740           if (TREE_CODE (type) == BOOLEAN_TYPE)
7741             return build2_loc (loc, TREE_CODE (op0), type,
7742                                TREE_OPERAND (op0, 0),
7743                                TREE_OPERAND (op0, 1));
7744           else if (!INTEGRAL_TYPE_P (type))
7745             return build3_loc (loc, COND_EXPR, type, op0,
7746                                constant_boolean_node (true, type),
7747                                constant_boolean_node (false, type));
7748         }
7749
7750       /* Handle cases of two conversions in a row.  */
7751       if (CONVERT_EXPR_P (op0))
7752         {
7753           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7754           tree inter_type = TREE_TYPE (op0);
7755           int inside_int = INTEGRAL_TYPE_P (inside_type);
7756           int inside_ptr = POINTER_TYPE_P (inside_type);
7757           int inside_float = FLOAT_TYPE_P (inside_type);
7758           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7759           unsigned int inside_prec = TYPE_PRECISION (inside_type);
7760           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7761           int inter_int = INTEGRAL_TYPE_P (inter_type);
7762           int inter_ptr = POINTER_TYPE_P (inter_type);
7763           int inter_float = FLOAT_TYPE_P (inter_type);
7764           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7765           unsigned int inter_prec = TYPE_PRECISION (inter_type);
7766           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7767           int final_int = INTEGRAL_TYPE_P (type);
7768           int final_ptr = POINTER_TYPE_P (type);
7769           int final_float = FLOAT_TYPE_P (type);
7770           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7771           unsigned int final_prec = TYPE_PRECISION (type);
7772           int final_unsignedp = TYPE_UNSIGNED (type);
7773
7774           /* In addition to the cases of two conversions in a row
7775              handled below, if we are converting something to its own
7776              type via an object of identical or wider precision, neither
7777              conversion is needed.  */
7778           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7779               && (((inter_int || inter_ptr) && final_int)
7780                   || (inter_float && final_float))
7781               && inter_prec >= final_prec)
7782             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7783
7784           /* Likewise, if the intermediate and initial types are either both
7785              float or both integer, we don't need the middle conversion if the
7786              former is wider than the latter and doesn't change the signedness
7787              (for integers).  Avoid this if the final type is a pointer since
7788              then we sometimes need the middle conversion.  Likewise if the
7789              final type has a precision not equal to the size of its mode.  */
7790           if (((inter_int && inside_int)
7791                || (inter_float && inside_float)
7792                || (inter_vec && inside_vec))
7793               && inter_prec >= inside_prec
7794               && (inter_float || inter_vec
7795                   || inter_unsignedp == inside_unsignedp)
7796               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7797                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
7798               && ! final_ptr
7799               && (! final_vec || inter_prec == inside_prec))
7800             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7801
7802           /* If we have a sign-extension of a zero-extended value, we can
7803              replace that by a single zero-extension.  */
7804           if (inside_int && inter_int && final_int
7805               && inside_prec < inter_prec && inter_prec < final_prec
7806               && inside_unsignedp && !inter_unsignedp)
7807             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7808
7809           /* Two conversions in a row are not needed unless:
7810              - some conversion is floating-point (overstrict for now), or
7811              - some conversion is a vector (overstrict for now), or
7812              - the intermediate type is narrower than both initial and
7813                final, or
7814              - the intermediate type and innermost type differ in signedness,
7815                and the outermost type is wider than the intermediate, or
7816              - the initial type is a pointer type and the precisions of the
7817                intermediate and final types differ, or
7818              - the final type is a pointer type and the precisions of the
7819                initial and intermediate types differ.  */
7820           if (! inside_float && ! inter_float && ! final_float
7821               && ! inside_vec && ! inter_vec && ! final_vec
7822               && (inter_prec >= inside_prec || inter_prec >= final_prec)
7823               && ! (inside_int && inter_int
7824                     && inter_unsignedp != inside_unsignedp
7825                     && inter_prec < final_prec)
7826               && ((inter_unsignedp && inter_prec > inside_prec)
7827                   == (final_unsignedp && final_prec > inter_prec))
7828               && ! (inside_ptr && inter_prec != final_prec)
7829               && ! (final_ptr && inside_prec != inter_prec)
7830               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7831                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7832             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7833         }
7834
7835       /* Handle (T *)&A.B.C for A being of type T and B and C
7836          living at offset zero.  This occurs frequently in
7837          C++ upcasting and then accessing the base.  */
7838       if (TREE_CODE (op0) == ADDR_EXPR
7839           && POINTER_TYPE_P (type)
7840           && handled_component_p (TREE_OPERAND (op0, 0)))
7841         {
7842           HOST_WIDE_INT bitsize, bitpos;
7843           tree offset;
7844           enum machine_mode mode;
7845           int unsignedp, volatilep;
7846           tree base = TREE_OPERAND (op0, 0);
7847           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7848                                       &mode, &unsignedp, &volatilep, false);
7849           /* If the reference was to a (constant) zero offset, we can use
7850              the address of the base if it has the same base type
7851              as the result type and the pointer type is unqualified.  */
7852           if (! offset && bitpos == 0
7853               && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7854                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7855               && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7856             return fold_convert_loc (loc, type,
7857                                      build_fold_addr_expr_loc (loc, base));
7858         }
7859
7860       if (TREE_CODE (op0) == MODIFY_EXPR
7861           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7862           /* Detect assigning a bitfield.  */
7863           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7864                && DECL_BIT_FIELD
7865                (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7866         {
7867           /* Don't leave an assignment inside a conversion
7868              unless assigning a bitfield.  */
7869           tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7870           /* First do the assignment, then return converted constant.  */
7871           tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7872           TREE_NO_WARNING (tem) = 1;
7873           TREE_USED (tem) = 1;
7874           return tem;
7875         }
7876
7877       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7878          constants (if x has signed type, the sign bit cannot be set
7879          in c).  This folds extension into the BIT_AND_EXPR.
7880          ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7881          very likely don't have maximal range for their precision and this
7882          transformation effectively doesn't preserve non-maximal ranges.  */
7883       if (TREE_CODE (type) == INTEGER_TYPE
7884           && TREE_CODE (op0) == BIT_AND_EXPR
7885           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7886         {
7887           tree and_expr = op0;
7888           tree and0 = TREE_OPERAND (and_expr, 0);
7889           tree and1 = TREE_OPERAND (and_expr, 1);
7890           int change = 0;
7891
7892           if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7893               || (TYPE_PRECISION (type)
7894                   <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7895             change = 1;
7896           else if (TYPE_PRECISION (TREE_TYPE (and1))
7897                    <= HOST_BITS_PER_WIDE_INT
7898                    && host_integerp (and1, 1))
7899             {
7900               unsigned HOST_WIDE_INT cst;
7901
7902               cst = tree_low_cst (and1, 1);
7903               cst &= (HOST_WIDE_INT) -1
7904                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7905               change = (cst == 0);
7906 #ifdef LOAD_EXTEND_OP
7907               if (change
7908                   && !flag_syntax_only
7909                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7910                       == ZERO_EXTEND))
7911                 {
7912                   tree uns = unsigned_type_for (TREE_TYPE (and0));
7913                   and0 = fold_convert_loc (loc, uns, and0);
7914                   and1 = fold_convert_loc (loc, uns, and1);
7915                 }
7916 #endif
7917             }
7918           if (change)
7919             {
7920               tem = force_fit_type_double (type, tree_to_double_int (and1),
7921                                            0, TREE_OVERFLOW (and1));
7922               return fold_build2_loc (loc, BIT_AND_EXPR, type,
7923                                   fold_convert_loc (loc, type, and0), tem);
7924             }
7925         }
7926
7927       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7928          when one of the new casts will fold away. Conservatively we assume
7929          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7930       if (POINTER_TYPE_P (type)
7931           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7932           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7933               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7934               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7935         {
7936           tree arg00 = TREE_OPERAND (arg0, 0);
7937           tree arg01 = TREE_OPERAND (arg0, 1);
7938
7939           return fold_build_pointer_plus_loc
7940                    (loc, fold_convert_loc (loc, type, arg00), arg01);
7941         }
7942
7943       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7944          of the same precision, and X is an integer type not narrower than
7945          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
7946       if (INTEGRAL_TYPE_P (type)
7947           && TREE_CODE (op0) == BIT_NOT_EXPR
7948           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7949           && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7950           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7951         {
7952           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7953           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7954               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7955             return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7956                                 fold_convert_loc (loc, type, tem));
7957         }
7958
7959       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7960          type of X and Y (integer types only).  */
7961       if (INTEGRAL_TYPE_P (type)
7962           && TREE_CODE (op0) == MULT_EXPR
7963           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7964           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7965         {
7966           /* Be careful not to introduce new overflows.  */
7967           tree mult_type;
7968           if (TYPE_OVERFLOW_WRAPS (type))
7969             mult_type = type;
7970           else
7971             mult_type = unsigned_type_for (type);
7972
7973           if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7974             {
7975               tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7976                                  fold_convert_loc (loc, mult_type,
7977                                                    TREE_OPERAND (op0, 0)),
7978                                  fold_convert_loc (loc, mult_type,
7979                                                    TREE_OPERAND (op0, 1)));
7980               return fold_convert_loc (loc, type, tem);
7981             }
7982         }
7983
7984       tem = fold_convert_const (code, type, op0);
7985       return tem ? tem : NULL_TREE;
7986
7987     case ADDR_SPACE_CONVERT_EXPR:
7988       if (integer_zerop (arg0))
7989         return fold_convert_const (code, type, arg0);
7990       return NULL_TREE;
7991
7992     case FIXED_CONVERT_EXPR:
7993       tem = fold_convert_const (code, type, arg0);
7994       return tem ? tem : NULL_TREE;
7995
7996     case VIEW_CONVERT_EXPR:
7997       if (TREE_TYPE (op0) == type)
7998         return op0;
7999       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8000         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8001                             type, TREE_OPERAND (op0, 0));
8002       if (TREE_CODE (op0) == MEM_REF)
8003         return fold_build2_loc (loc, MEM_REF, type,
8004                                 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
8005
8006       /* For integral conversions with the same precision or pointer
8007          conversions use a NOP_EXPR instead.  */
8008       if ((INTEGRAL_TYPE_P (type)
8009            || POINTER_TYPE_P (type))
8010           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8011               || POINTER_TYPE_P (TREE_TYPE (op0)))
8012           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8013         return fold_convert_loc (loc, type, op0);
8014
8015       /* Strip inner integral conversions that do not change the precision.  */
8016       if (CONVERT_EXPR_P (op0)
8017           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8018               || POINTER_TYPE_P (TREE_TYPE (op0)))
8019           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8020               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8021           && (TYPE_PRECISION (TREE_TYPE (op0))
8022               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8023         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8024                             type, TREE_OPERAND (op0, 0));
8025
8026       return fold_view_convert_expr (type, op0);
8027
8028     case NEGATE_EXPR:
8029       tem = fold_negate_expr (loc, arg0);
8030       if (tem)
8031         return fold_convert_loc (loc, type, tem);
8032       return NULL_TREE;
8033
8034     case ABS_EXPR:
8035       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8036         return fold_abs_const (arg0, type);
8037       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8038         return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8039       /* Convert fabs((double)float) into (double)fabsf(float).  */
8040       else if (TREE_CODE (arg0) == NOP_EXPR
8041                && TREE_CODE (type) == REAL_TYPE)
8042         {
8043           tree targ0 = strip_float_extensions (arg0);
8044           if (targ0 != arg0)
8045             return fold_convert_loc (loc, type,
8046                                      fold_build1_loc (loc, ABS_EXPR,
8047                                                   TREE_TYPE (targ0),
8048                                                   targ0));
8049         }
8050       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8051       else if (TREE_CODE (arg0) == ABS_EXPR)
8052         return arg0;
8053       else if (tree_expr_nonnegative_p (arg0))
8054         return arg0;
8055
8056       /* Strip sign ops from argument.  */
8057       if (TREE_CODE (type) == REAL_TYPE)
8058         {
8059           tem = fold_strip_sign_ops (arg0);
8060           if (tem)
8061             return fold_build1_loc (loc, ABS_EXPR, type,
8062                                 fold_convert_loc (loc, type, tem));
8063         }
8064       return NULL_TREE;
8065
8066     case CONJ_EXPR:
8067       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8068         return fold_convert_loc (loc, type, arg0);
8069       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8070         {
8071           tree itype = TREE_TYPE (type);
8072           tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8073           tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8074           return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8075                               negate_expr (ipart));
8076         }
8077       if (TREE_CODE (arg0) == COMPLEX_CST)
8078         {
8079           tree itype = TREE_TYPE (type);
8080           tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8081           tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8082           return build_complex (type, rpart, negate_expr (ipart));
8083         }
8084       if (TREE_CODE (arg0) == CONJ_EXPR)
8085         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8086       return NULL_TREE;
8087
8088     case BIT_NOT_EXPR:
8089       if (TREE_CODE (arg0) == INTEGER_CST)
8090         return fold_not_const (arg0, type);
8091       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8092         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8093       /* Convert ~ (-A) to A - 1.  */
8094       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8095         return fold_build2_loc (loc, MINUS_EXPR, type,
8096                             fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8097                             build_int_cst (type, 1));
8098       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8099       else if (INTEGRAL_TYPE_P (type)
8100                && ((TREE_CODE (arg0) == MINUS_EXPR
8101                     && integer_onep (TREE_OPERAND (arg0, 1)))
8102                    || (TREE_CODE (arg0) == PLUS_EXPR
8103                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8104         return fold_build1_loc (loc, NEGATE_EXPR, type,
8105                             fold_convert_loc (loc, type,
8106                                               TREE_OPERAND (arg0, 0)));
8107       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8108       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8109                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8110                                      fold_convert_loc (loc, type,
8111                                                        TREE_OPERAND (arg0, 0)))))
8112         return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8113                             fold_convert_loc (loc, type,
8114                                               TREE_OPERAND (arg0, 1)));
8115       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8116                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8117                                      fold_convert_loc (loc, type,
8118                                                        TREE_OPERAND (arg0, 1)))))
8119         return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8120                             fold_convert_loc (loc, type,
8121                                               TREE_OPERAND (arg0, 0)), tem);
8122       /* Perform BIT_NOT_EXPR on each element individually.  */
8123       else if (TREE_CODE (arg0) == VECTOR_CST)
8124         {
8125           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8126           int count = TYPE_VECTOR_SUBPARTS (type), i;
8127
8128           for (i = 0; i < count; i++)
8129             {
8130               if (elements)
8131                 {
8132                   elem = TREE_VALUE (elements);
8133                   elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8134                   if (elem == NULL_TREE)
8135                     break;
8136                   elements = TREE_CHAIN (elements);
8137                 }
8138               else
8139                 elem = build_int_cst (TREE_TYPE (type), -1);
8140               list = tree_cons (NULL_TREE, elem, list);
8141             }
8142           if (i == count)
8143             return build_vector (type, nreverse (list));
8144         }
8145
8146       return NULL_TREE;
8147
8148     case TRUTH_NOT_EXPR:
8149       /* The argument to invert_truthvalue must have Boolean type.  */
8150       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8151           arg0 = fold_convert_loc (loc, boolean_type_node, arg0);
8152
8153       /* Note that the operand of this must be an int
8154          and its values must be 0 or 1.
8155          ("true" is a fixed value perhaps depending on the language,
8156          but we don't handle values other than 1 correctly yet.)  */
8157       tem = fold_truth_not_expr (loc, arg0);
8158       if (!tem)
8159         return NULL_TREE;
8160       return fold_convert_loc (loc, type, tem);
8161
8162     case REALPART_EXPR:
8163       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8164         return fold_convert_loc (loc, type, arg0);
8165       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8166         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8167                                  TREE_OPERAND (arg0, 1));
8168       if (TREE_CODE (arg0) == COMPLEX_CST)
8169         return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8170       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8171         {
8172           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8173           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8174                              fold_build1_loc (loc, REALPART_EXPR, itype,
8175                                           TREE_OPERAND (arg0, 0)),
8176                              fold_build1_loc (loc, REALPART_EXPR, itype,
8177                                           TREE_OPERAND (arg0, 1)));
8178           return fold_convert_loc (loc, type, tem);
8179         }
8180       if (TREE_CODE (arg0) == CONJ_EXPR)
8181         {
8182           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8183           tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8184                              TREE_OPERAND (arg0, 0));
8185           return fold_convert_loc (loc, type, tem);
8186         }
8187       if (TREE_CODE (arg0) == CALL_EXPR)
8188         {
8189           tree fn = get_callee_fndecl (arg0);
8190           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8191             switch (DECL_FUNCTION_CODE (fn))
8192               {
8193               CASE_FLT_FN (BUILT_IN_CEXPI):
8194                 fn = mathfn_built_in (type, BUILT_IN_COS);
8195                 if (fn)
8196                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8197                 break;
8198
8199               default:
8200                 break;
8201               }
8202         }
8203       return NULL_TREE;
8204
8205     case IMAGPART_EXPR:
8206       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8207         return build_zero_cst (type);
8208       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8209         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8210                                  TREE_OPERAND (arg0, 0));
8211       if (TREE_CODE (arg0) == COMPLEX_CST)
8212         return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8213       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8214         {
8215           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8216           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8217                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8218                                           TREE_OPERAND (arg0, 0)),
8219                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8220                                           TREE_OPERAND (arg0, 1)));
8221           return fold_convert_loc (loc, type, tem);
8222         }
8223       if (TREE_CODE (arg0) == CONJ_EXPR)
8224         {
8225           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8226           tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8227           return fold_convert_loc (loc, type, negate_expr (tem));
8228         }
8229       if (TREE_CODE (arg0) == CALL_EXPR)
8230         {
8231           tree fn = get_callee_fndecl (arg0);
8232           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8233             switch (DECL_FUNCTION_CODE (fn))
8234               {
8235               CASE_FLT_FN (BUILT_IN_CEXPI):
8236                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8237                 if (fn)
8238                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8239                 break;
8240
8241               default:
8242                 break;
8243               }
8244         }
8245       return NULL_TREE;
8246
8247     case INDIRECT_REF:
8248       /* Fold *&X to X if X is an lvalue.  */
8249       if (TREE_CODE (op0) == ADDR_EXPR)
8250         {
8251           tree op00 = TREE_OPERAND (op0, 0);
8252           if ((TREE_CODE (op00) == VAR_DECL
8253                || TREE_CODE (op00) == PARM_DECL
8254                || TREE_CODE (op00) == RESULT_DECL)
8255               && !TREE_READONLY (op00))
8256             return op00;
8257         }
8258       return NULL_TREE;
8259
8260     default:
8261       return NULL_TREE;
8262     } /* switch (code) */
8263 }
8264
8265
8266 /* If the operation was a conversion do _not_ mark a resulting constant
8267    with TREE_OVERFLOW if the original constant was not.  These conversions
8268    have implementation defined behavior and retaining the TREE_OVERFLOW
8269    flag here would confuse later passes such as VRP.  */
8270 tree
8271 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8272                                 tree type, tree op0)
8273 {
8274   tree res = fold_unary_loc (loc, code, type, op0);
8275   if (res
8276       && TREE_CODE (res) == INTEGER_CST
8277       && TREE_CODE (op0) == INTEGER_CST
8278       && CONVERT_EXPR_CODE_P (code))
8279     TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8280
8281   return res;
8282 }
8283
8284 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8285    operands OP0 and OP1.  LOC is the location of the resulting expression.
8286    ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8287    Return the folded expression if folding is successful.  Otherwise,
8288    return NULL_TREE.  */
8289 static tree
8290 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8291                   tree arg0, tree arg1, tree op0, tree op1)
8292 {
8293   tree tem;
8294
8295   /* We only do these simplifications if we are optimizing.  */
8296   if (!optimize)
8297     return NULL_TREE;
8298
8299   /* Check for things like (A || B) && (A || C).  We can convert this
8300      to A || (B && C).  Note that either operator can be any of the four
8301      truth and/or operations and the transformation will still be
8302      valid.   Also note that we only care about order for the
8303      ANDIF and ORIF operators.  If B contains side effects, this
8304      might change the truth-value of A.  */
8305   if (TREE_CODE (arg0) == TREE_CODE (arg1)
8306       && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8307           || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8308           || TREE_CODE (arg0) == TRUTH_AND_EXPR
8309           || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8310       && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8311     {
8312       tree a00 = TREE_OPERAND (arg0, 0);
8313       tree a01 = TREE_OPERAND (arg0, 1);
8314       tree a10 = TREE_OPERAND (arg1, 0);
8315       tree a11 = TREE_OPERAND (arg1, 1);
8316       int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8317                           || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8318                          && (code == TRUTH_AND_EXPR
8319                              || code == TRUTH_OR_EXPR));
8320
8321       if (operand_equal_p (a00, a10, 0))
8322         return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8323                             fold_build2_loc (loc, code, type, a01, a11));
8324       else if (commutative && operand_equal_p (a00, a11, 0))
8325         return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8326                             fold_build2_loc (loc, code, type, a01, a10));
8327       else if (commutative && operand_equal_p (a01, a10, 0))
8328         return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8329                             fold_build2_loc (loc, code, type, a00, a11));
8330
8331       /* This case if tricky because we must either have commutative
8332          operators or else A10 must not have side-effects.  */
8333
8334       else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8335                && operand_equal_p (a01, a11, 0))
8336         return fold_build2_loc (loc, TREE_CODE (arg0), type,
8337                             fold_build2_loc (loc, code, type, a00, a10),
8338                             a01);
8339     }
8340
8341   /* See if we can build a range comparison.  */
8342   if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8343     return tem;
8344
8345   if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8346       || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8347     {
8348       tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8349       if (tem)
8350         return fold_build2_loc (loc, code, type, tem, arg1);
8351     }
8352
8353   if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8354       || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8355     {
8356       tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8357       if (tem)
8358         return fold_build2_loc (loc, code, type, arg0, tem);
8359     }
8360
8361   /* Check for the possibility of merging component references.  If our
8362      lhs is another similar operation, try to merge its rhs with our
8363      rhs.  Then try to merge our lhs and rhs.  */
8364   if (TREE_CODE (arg0) == code
8365       && 0 != (tem = fold_truthop (loc, code, type,
8366                                    TREE_OPERAND (arg0, 1), arg1)))
8367     return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8368
8369   if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0)
8370     return tem;
8371
8372   return NULL_TREE;
8373 }
8374
8375 /* Fold a binary expression of code CODE and type TYPE with operands
8376    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8377    Return the folded expression if folding is successful.  Otherwise,
8378    return NULL_TREE.  */
8379
8380 static tree
8381 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8382 {
8383   enum tree_code compl_code;
8384
8385   if (code == MIN_EXPR)
8386     compl_code = MAX_EXPR;
8387   else if (code == MAX_EXPR)
8388     compl_code = MIN_EXPR;
8389   else
8390     gcc_unreachable ();
8391
8392   /* MIN (MAX (a, b), b) == b.  */
8393   if (TREE_CODE (op0) == compl_code
8394       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8395     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8396
8397   /* MIN (MAX (b, a), b) == b.  */
8398   if (TREE_CODE (op0) == compl_code
8399       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8400       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8401     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8402
8403   /* MIN (a, MAX (a, b)) == a.  */
8404   if (TREE_CODE (op1) == compl_code
8405       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8406       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8407     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8408
8409   /* MIN (a, MAX (b, a)) == a.  */
8410   if (TREE_CODE (op1) == compl_code
8411       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8412       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8413     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8414
8415   return NULL_TREE;
8416 }
8417
8418 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8419    by changing CODE to reduce the magnitude of constants involved in
8420    ARG0 of the comparison.
8421    Returns a canonicalized comparison tree if a simplification was
8422    possible, otherwise returns NULL_TREE.
8423    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8424    valid if signed overflow is undefined.  */
8425
8426 static tree
8427 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8428                                  tree arg0, tree arg1,
8429                                  bool *strict_overflow_p)
8430 {
8431   enum tree_code code0 = TREE_CODE (arg0);
8432   tree t, cst0 = NULL_TREE;
8433   int sgn0;
8434   bool swap = false;
8435
8436   /* Match A +- CST code arg1 and CST code arg1.  We can change the
8437      first form only if overflow is undefined.  */
8438   if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8439          /* In principle pointers also have undefined overflow behavior,
8440             but that causes problems elsewhere.  */
8441          && !POINTER_TYPE_P (TREE_TYPE (arg0))
8442          && (code0 == MINUS_EXPR
8443              || code0 == PLUS_EXPR)
8444          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8445         || code0 == INTEGER_CST))
8446     return NULL_TREE;
8447
8448   /* Identify the constant in arg0 and its sign.  */
8449   if (code0 == INTEGER_CST)
8450     cst0 = arg0;
8451   else
8452     cst0 = TREE_OPERAND (arg0, 1);
8453   sgn0 = tree_int_cst_sgn (cst0);
8454
8455   /* Overflowed constants and zero will cause problems.  */
8456   if (integer_zerop (cst0)
8457       || TREE_OVERFLOW (cst0))
8458     return NULL_TREE;
8459
8460   /* See if we can reduce the magnitude of the constant in
8461      arg0 by changing the comparison code.  */
8462   if (code0 == INTEGER_CST)
8463     {
8464       /* CST <= arg1  ->  CST-1 < arg1.  */
8465       if (code == LE_EXPR && sgn0 == 1)
8466         code = LT_EXPR;
8467       /* -CST < arg1  ->  -CST-1 <= arg1.  */
8468       else if (code == LT_EXPR && sgn0 == -1)
8469         code = LE_EXPR;
8470       /* CST > arg1  ->  CST-1 >= arg1.  */
8471       else if (code == GT_EXPR && sgn0 == 1)
8472         code = GE_EXPR;
8473       /* -CST >= arg1  ->  -CST-1 > arg1.  */
8474       else if (code == GE_EXPR && sgn0 == -1)
8475         code = GT_EXPR;
8476       else
8477         return NULL_TREE;
8478       /* arg1 code' CST' might be more canonical.  */
8479       swap = true;
8480     }
8481   else
8482     {
8483       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
8484       if (code == LT_EXPR
8485           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8486         code = LE_EXPR;
8487       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
8488       else if (code == GT_EXPR
8489                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8490         code = GE_EXPR;
8491       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
8492       else if (code == LE_EXPR
8493                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8494         code = LT_EXPR;
8495       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
8496       else if (code == GE_EXPR
8497                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8498         code = GT_EXPR;
8499       else
8500         return NULL_TREE;
8501       *strict_overflow_p = true;
8502     }
8503
8504   /* Now build the constant reduced in magnitude.  But not if that
8505      would produce one outside of its types range.  */
8506   if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8507       && ((sgn0 == 1
8508            && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8509            && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8510           || (sgn0 == -1
8511               && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8512               && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8513     /* We cannot swap the comparison here as that would cause us to
8514        endlessly recurse.  */
8515     return NULL_TREE;
8516
8517   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8518                        cst0, build_int_cst (TREE_TYPE (cst0), 1));
8519   if (code0 != INTEGER_CST)
8520     t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8521   t = fold_convert (TREE_TYPE (arg1), t);
8522
8523   /* If swapping might yield to a more canonical form, do so.  */
8524   if (swap)
8525     return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8526   else
8527     return fold_build2_loc (loc, code, type, t, arg1);
8528 }
8529
8530 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8531    overflow further.  Try to decrease the magnitude of constants involved
8532    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8533    and put sole constants at the second argument position.
8534    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
8535
8536 static tree
8537 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8538                                tree arg0, tree arg1)
8539 {
8540   tree t;
8541   bool strict_overflow_p;
8542   const char * const warnmsg = G_("assuming signed overflow does not occur "
8543                                   "when reducing constant in comparison");
8544
8545   /* Try canonicalization by simplifying arg0.  */
8546   strict_overflow_p = false;
8547   t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8548                                        &strict_overflow_p);
8549   if (t)
8550     {
8551       if (strict_overflow_p)
8552         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8553       return t;
8554     }
8555
8556   /* Try canonicalization by simplifying arg1 using the swapped
8557      comparison.  */
8558   code = swap_tree_comparison (code);
8559   strict_overflow_p = false;
8560   t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8561                                        &strict_overflow_p);
8562   if (t && strict_overflow_p)
8563     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8564   return t;
8565 }
8566
8567 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8568    space.  This is used to avoid issuing overflow warnings for
8569    expressions like &p->x which can not wrap.  */
8570
8571 static bool
8572 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8573 {
8574   unsigned HOST_WIDE_INT offset_low, total_low;
8575   HOST_WIDE_INT size, offset_high, total_high;
8576
8577   if (!POINTER_TYPE_P (TREE_TYPE (base)))
8578     return true;
8579
8580   if (bitpos < 0)
8581     return true;
8582
8583   if (offset == NULL_TREE)
8584     {
8585       offset_low = 0;
8586       offset_high = 0;
8587     }
8588   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8589     return true;
8590   else
8591     {
8592       offset_low = TREE_INT_CST_LOW (offset);
8593       offset_high = TREE_INT_CST_HIGH (offset);
8594     }
8595
8596   if (add_double_with_sign (offset_low, offset_high,
8597                             bitpos / BITS_PER_UNIT, 0,
8598                             &total_low, &total_high,
8599                             true))
8600     return true;
8601
8602   if (total_high != 0)
8603     return true;
8604
8605   size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8606   if (size <= 0)
8607     return true;
8608
8609   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8610      array.  */
8611   if (TREE_CODE (base) == ADDR_EXPR)
8612     {
8613       HOST_WIDE_INT base_size;
8614
8615       base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8616       if (base_size > 0 && size < base_size)
8617         size = base_size;
8618     }
8619
8620   return total_low > (unsigned HOST_WIDE_INT) size;
8621 }
8622
8623 /* Subroutine of fold_binary.  This routine performs all of the
8624    transformations that are common to the equality/inequality
8625    operators (EQ_EXPR and NE_EXPR) and the ordering operators
8626    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
8627    fold_binary should call fold_binary.  Fold a comparison with
8628    tree code CODE and type TYPE with operands OP0 and OP1.  Return
8629    the folded comparison or NULL_TREE.  */
8630
8631 static tree
8632 fold_comparison (location_t loc, enum tree_code code, tree type,
8633                  tree op0, tree op1)
8634 {
8635   tree arg0, arg1, tem;
8636
8637   arg0 = op0;
8638   arg1 = op1;
8639
8640   STRIP_SIGN_NOPS (arg0);
8641   STRIP_SIGN_NOPS (arg1);
8642
8643   tem = fold_relational_const (code, type, arg0, arg1);
8644   if (tem != NULL_TREE)
8645     return tem;
8646
8647   /* If one arg is a real or integer constant, put it last.  */
8648   if (tree_swap_operands_p (arg0, arg1, true))
8649     return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
8650
8651   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
8652   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8653       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8654           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8655           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8656       && (TREE_CODE (arg1) == INTEGER_CST
8657           && !TREE_OVERFLOW (arg1)))
8658     {
8659       tree const1 = TREE_OPERAND (arg0, 1);
8660       tree const2 = arg1;
8661       tree variable = TREE_OPERAND (arg0, 0);
8662       tree lhs;
8663       int lhs_add;
8664       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8665
8666       lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
8667                          TREE_TYPE (arg1), const2, const1);
8668
8669       /* If the constant operation overflowed this can be
8670          simplified as a comparison against INT_MAX/INT_MIN.  */
8671       if (TREE_CODE (lhs) == INTEGER_CST
8672           && TREE_OVERFLOW (lhs))
8673         {
8674           int const1_sgn = tree_int_cst_sgn (const1);
8675           enum tree_code code2 = code;
8676
8677           /* Get the sign of the constant on the lhs if the
8678              operation were VARIABLE + CONST1.  */
8679           if (TREE_CODE (arg0) == MINUS_EXPR)
8680             const1_sgn = -const1_sgn;
8681
8682           /* The sign of the constant determines if we overflowed
8683              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8684              Canonicalize to the INT_MIN overflow by swapping the comparison
8685              if necessary.  */
8686           if (const1_sgn == -1)
8687             code2 = swap_tree_comparison (code);
8688
8689           /* We now can look at the canonicalized case
8690                VARIABLE + 1  CODE2  INT_MIN
8691              and decide on the result.  */
8692           if (code2 == LT_EXPR
8693               || code2 == LE_EXPR
8694               || code2 == EQ_EXPR)
8695             return omit_one_operand_loc (loc, type, boolean_false_node, variable);
8696           else if (code2 == NE_EXPR
8697                    || code2 == GE_EXPR
8698                    || code2 == GT_EXPR)
8699             return omit_one_operand_loc (loc, type, boolean_true_node, variable);
8700         }
8701
8702       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8703           && (TREE_CODE (lhs) != INTEGER_CST
8704               || !TREE_OVERFLOW (lhs)))
8705         {
8706           if (code != EQ_EXPR && code != NE_EXPR)
8707             fold_overflow_warning ("assuming signed overflow does not occur "
8708                                    "when changing X +- C1 cmp C2 to "
8709                                    "X cmp C1 +- C2",
8710                                    WARN_STRICT_OVERFLOW_COMPARISON);
8711           return fold_build2_loc (loc, code, type, variable, lhs);
8712         }
8713     }
8714
8715   /* For comparisons of pointers we can decompose it to a compile time
8716      comparison of the base objects and the offsets into the object.
8717      This requires at least one operand being an ADDR_EXPR or a
8718      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
8719   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8720       && (TREE_CODE (arg0) == ADDR_EXPR
8721           || TREE_CODE (arg1) == ADDR_EXPR
8722           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8723           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8724     {
8725       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8726       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8727       enum machine_mode mode;
8728       int volatilep, unsignedp;
8729       bool indirect_base0 = false, indirect_base1 = false;
8730
8731       /* Get base and offset for the access.  Strip ADDR_EXPR for
8732          get_inner_reference, but put it back by stripping INDIRECT_REF
8733          off the base object if possible.  indirect_baseN will be true
8734          if baseN is not an address but refers to the object itself.  */
8735       base0 = arg0;
8736       if (TREE_CODE (arg0) == ADDR_EXPR)
8737         {
8738           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8739                                        &bitsize, &bitpos0, &offset0, &mode,
8740                                        &unsignedp, &volatilep, false);
8741           if (TREE_CODE (base0) == INDIRECT_REF)
8742             base0 = TREE_OPERAND (base0, 0);
8743           else
8744             indirect_base0 = true;
8745         }
8746       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8747         {
8748           base0 = TREE_OPERAND (arg0, 0);
8749           STRIP_SIGN_NOPS (base0);
8750           if (TREE_CODE (base0) == ADDR_EXPR)
8751             {
8752               base0 = TREE_OPERAND (base0, 0);
8753               indirect_base0 = true;
8754             }
8755           offset0 = TREE_OPERAND (arg0, 1);
8756         }
8757
8758       base1 = arg1;
8759       if (TREE_CODE (arg1) == ADDR_EXPR)
8760         {
8761           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8762                                        &bitsize, &bitpos1, &offset1, &mode,
8763                                        &unsignedp, &volatilep, false);
8764           if (TREE_CODE (base1) == INDIRECT_REF)
8765             base1 = TREE_OPERAND (base1, 0);
8766           else
8767             indirect_base1 = true;
8768         }
8769       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8770         {
8771           base1 = TREE_OPERAND (arg1, 0);
8772           STRIP_SIGN_NOPS (base1);
8773           if (TREE_CODE (base1) == ADDR_EXPR)
8774             {
8775               base1 = TREE_OPERAND (base1, 0);
8776               indirect_base1 = true;
8777             }
8778           offset1 = TREE_OPERAND (arg1, 1);
8779         }
8780
8781       /* A local variable can never be pointed to by
8782          the default SSA name of an incoming parameter.  */
8783       if ((TREE_CODE (arg0) == ADDR_EXPR
8784            && indirect_base0
8785            && TREE_CODE (base0) == VAR_DECL
8786            && auto_var_in_fn_p (base0, current_function_decl)
8787            && !indirect_base1
8788            && TREE_CODE (base1) == SSA_NAME
8789            && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL
8790            && SSA_NAME_IS_DEFAULT_DEF (base1))
8791           || (TREE_CODE (arg1) == ADDR_EXPR
8792               && indirect_base1
8793               && TREE_CODE (base1) == VAR_DECL
8794               && auto_var_in_fn_p (base1, current_function_decl)
8795               && !indirect_base0
8796               && TREE_CODE (base0) == SSA_NAME
8797               && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL
8798               && SSA_NAME_IS_DEFAULT_DEF (base0)))
8799         {
8800           if (code == NE_EXPR)
8801             return constant_boolean_node (1, type);
8802           else if (code == EQ_EXPR)
8803             return constant_boolean_node (0, type);
8804         }
8805       /* If we have equivalent bases we might be able to simplify.  */
8806       else if (indirect_base0 == indirect_base1
8807                && operand_equal_p (base0, base1, 0))
8808         {
8809           /* We can fold this expression to a constant if the non-constant
8810              offset parts are equal.  */
8811           if ((offset0 == offset1
8812                || (offset0 && offset1
8813                    && operand_equal_p (offset0, offset1, 0)))
8814               && (code == EQ_EXPR
8815                   || code == NE_EXPR
8816                   || (indirect_base0 && DECL_P (base0))
8817                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
8818
8819             {
8820               if (code != EQ_EXPR
8821                   && code != NE_EXPR
8822                   && bitpos0 != bitpos1
8823                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
8824                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
8825                 fold_overflow_warning (("assuming pointer wraparound does not "
8826                                         "occur when comparing P +- C1 with "
8827                                         "P +- C2"),
8828                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
8829
8830               switch (code)
8831                 {
8832                 case EQ_EXPR:
8833                   return constant_boolean_node (bitpos0 == bitpos1, type);
8834                 case NE_EXPR:
8835                   return constant_boolean_node (bitpos0 != bitpos1, type);
8836                 case LT_EXPR:
8837                   return constant_boolean_node (bitpos0 < bitpos1, type);
8838                 case LE_EXPR:
8839                   return constant_boolean_node (bitpos0 <= bitpos1, type);
8840                 case GE_EXPR:
8841                   return constant_boolean_node (bitpos0 >= bitpos1, type);
8842                 case GT_EXPR:
8843                   return constant_boolean_node (bitpos0 > bitpos1, type);
8844                 default:;
8845                 }
8846             }
8847           /* We can simplify the comparison to a comparison of the variable
8848              offset parts if the constant offset parts are equal.
8849              Be careful to use signed size type here because otherwise we
8850              mess with array offsets in the wrong way.  This is possible
8851              because pointer arithmetic is restricted to retain within an
8852              object and overflow on pointer differences is undefined as of
8853              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
8854           else if (bitpos0 == bitpos1
8855                    && ((code == EQ_EXPR || code == NE_EXPR)
8856                        || (indirect_base0 && DECL_P (base0))
8857                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
8858             {
8859               /* By converting to signed size type we cover middle-end pointer
8860                  arithmetic which operates on unsigned pointer types of size
8861                  type size and ARRAY_REF offsets which are properly sign or
8862                  zero extended from their type in case it is narrower than
8863                  size type.  */
8864               if (offset0 == NULL_TREE)
8865                 offset0 = build_int_cst (ssizetype, 0);
8866               else
8867                 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8868               if (offset1 == NULL_TREE)
8869                 offset1 = build_int_cst (ssizetype, 0);
8870               else
8871                 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8872
8873               if (code != EQ_EXPR
8874                   && code != NE_EXPR
8875                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
8876                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
8877                 fold_overflow_warning (("assuming pointer wraparound does not "
8878                                         "occur when comparing P +- C1 with "
8879                                         "P +- C2"),
8880                                        WARN_STRICT_OVERFLOW_COMPARISON);
8881
8882               return fold_build2_loc (loc, code, type, offset0, offset1);
8883             }
8884         }
8885       /* For non-equal bases we can simplify if they are addresses
8886          of local binding decls or constants.  */
8887       else if (indirect_base0 && indirect_base1
8888                /* We know that !operand_equal_p (base0, base1, 0)
8889                   because the if condition was false.  But make
8890                   sure two decls are not the same.  */
8891                && base0 != base1
8892                && TREE_CODE (arg0) == ADDR_EXPR
8893                && TREE_CODE (arg1) == ADDR_EXPR
8894                && (((TREE_CODE (base0) == VAR_DECL
8895                      || TREE_CODE (base0) == PARM_DECL)
8896                     && (targetm.binds_local_p (base0)
8897                         || CONSTANT_CLASS_P (base1)))
8898                    || CONSTANT_CLASS_P (base0))
8899                && (((TREE_CODE (base1) == VAR_DECL
8900                      || TREE_CODE (base1) == PARM_DECL)
8901                     && (targetm.binds_local_p (base1)
8902                         || CONSTANT_CLASS_P (base0)))
8903                    || CONSTANT_CLASS_P (base1)))
8904         {
8905           if (code == EQ_EXPR)
8906             return omit_two_operands_loc (loc, type, boolean_false_node,
8907                                       arg0, arg1);
8908           else if (code == NE_EXPR)
8909             return omit_two_operands_loc (loc, type, boolean_true_node,
8910                                       arg0, arg1);
8911         }
8912       /* For equal offsets we can simplify to a comparison of the
8913          base addresses.  */
8914       else if (bitpos0 == bitpos1
8915                && (indirect_base0
8916                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8917                && (indirect_base1
8918                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8919                && ((offset0 == offset1)
8920                    || (offset0 && offset1
8921                        && operand_equal_p (offset0, offset1, 0))))
8922         {
8923           if (indirect_base0)
8924             base0 = build_fold_addr_expr_loc (loc, base0);
8925           if (indirect_base1)
8926             base1 = build_fold_addr_expr_loc (loc, base1);
8927           return fold_build2_loc (loc, code, type, base0, base1);
8928         }
8929     }
8930
8931   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8932      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
8933      the resulting offset is smaller in absolute value than the
8934      original one.  */
8935   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8936       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8937       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8938           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8939       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8940       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8941           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8942     {
8943       tree const1 = TREE_OPERAND (arg0, 1);
8944       tree const2 = TREE_OPERAND (arg1, 1);
8945       tree variable1 = TREE_OPERAND (arg0, 0);
8946       tree variable2 = TREE_OPERAND (arg1, 0);
8947       tree cst;
8948       const char * const warnmsg = G_("assuming signed overflow does not "
8949                                       "occur when combining constants around "
8950                                       "a comparison");
8951
8952       /* Put the constant on the side where it doesn't overflow and is
8953          of lower absolute value than before.  */
8954       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8955                              ? MINUS_EXPR : PLUS_EXPR,
8956                              const2, const1);
8957       if (!TREE_OVERFLOW (cst)
8958           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8959         {
8960           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8961           return fold_build2_loc (loc, code, type,
8962                               variable1,
8963                               fold_build2_loc (loc,
8964                                            TREE_CODE (arg1), TREE_TYPE (arg1),
8965                                            variable2, cst));
8966         }
8967
8968       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8969                              ? MINUS_EXPR : PLUS_EXPR,
8970                              const1, const2);
8971       if (!TREE_OVERFLOW (cst)
8972           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8973         {
8974           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8975           return fold_build2_loc (loc, code, type,
8976                               fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
8977                                            variable1, cst),
8978                               variable2);
8979         }
8980     }
8981
8982   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8983      signed arithmetic case.  That form is created by the compiler
8984      often enough for folding it to be of value.  One example is in
8985      computing loop trip counts after Operator Strength Reduction.  */
8986   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8987       && TREE_CODE (arg0) == MULT_EXPR
8988       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8989           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8990       && integer_zerop (arg1))
8991     {
8992       tree const1 = TREE_OPERAND (arg0, 1);
8993       tree const2 = arg1;                       /* zero */
8994       tree variable1 = TREE_OPERAND (arg0, 0);
8995       enum tree_code cmp_code = code;
8996
8997       /* Handle unfolded multiplication by zero.  */
8998       if (integer_zerop (const1))
8999         return fold_build2_loc (loc, cmp_code, type, const1, const2);
9000
9001       fold_overflow_warning (("assuming signed overflow does not occur when "
9002                               "eliminating multiplication in comparison "
9003                               "with zero"),
9004                              WARN_STRICT_OVERFLOW_COMPARISON);
9005
9006       /* If const1 is negative we swap the sense of the comparison.  */
9007       if (tree_int_cst_sgn (const1) < 0)
9008         cmp_code = swap_tree_comparison (cmp_code);
9009
9010       return fold_build2_loc (loc, cmp_code, type, variable1, const2);
9011     }
9012
9013   tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
9014   if (tem)
9015     return tem;
9016
9017   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9018     {
9019       tree targ0 = strip_float_extensions (arg0);
9020       tree targ1 = strip_float_extensions (arg1);
9021       tree newtype = TREE_TYPE (targ0);
9022
9023       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9024         newtype = TREE_TYPE (targ1);
9025
9026       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
9027       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9028         return fold_build2_loc (loc, code, type,
9029                             fold_convert_loc (loc, newtype, targ0),
9030                             fold_convert_loc (loc, newtype, targ1));
9031
9032       /* (-a) CMP (-b) -> b CMP a  */
9033       if (TREE_CODE (arg0) == NEGATE_EXPR
9034           && TREE_CODE (arg1) == NEGATE_EXPR)
9035         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
9036                             TREE_OPERAND (arg0, 0));
9037
9038       if (TREE_CODE (arg1) == REAL_CST)
9039         {
9040           REAL_VALUE_TYPE cst;
9041           cst = TREE_REAL_CST (arg1);
9042
9043           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
9044           if (TREE_CODE (arg0) == NEGATE_EXPR)
9045             return fold_build2_loc (loc, swap_tree_comparison (code), type,
9046                                 TREE_OPERAND (arg0, 0),
9047                                 build_real (TREE_TYPE (arg1),
9048                                             real_value_negate (&cst)));
9049
9050           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
9051           /* a CMP (-0) -> a CMP 0  */
9052           if (REAL_VALUE_MINUS_ZERO (cst))
9053             return fold_build2_loc (loc, code, type, arg0,
9054                                 build_real (TREE_TYPE (arg1), dconst0));
9055
9056           /* x != NaN is always true, other ops are always false.  */
9057           if (REAL_VALUE_ISNAN (cst)
9058               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9059             {
9060               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9061               return omit_one_operand_loc (loc, type, tem, arg0);
9062             }
9063
9064           /* Fold comparisons against infinity.  */
9065           if (REAL_VALUE_ISINF (cst)
9066               && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9067             {
9068               tem = fold_inf_compare (loc, code, type, arg0, arg1);
9069               if (tem != NULL_TREE)
9070                 return tem;
9071             }
9072         }
9073
9074       /* If this is a comparison of a real constant with a PLUS_EXPR
9075          or a MINUS_EXPR of a real constant, we can convert it into a
9076          comparison with a revised real constant as long as no overflow
9077          occurs when unsafe_math_optimizations are enabled.  */
9078       if (flag_unsafe_math_optimizations
9079           && TREE_CODE (arg1) == REAL_CST
9080           && (TREE_CODE (arg0) == PLUS_EXPR
9081               || TREE_CODE (arg0) == MINUS_EXPR)
9082           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9083           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9084                                       ? MINUS_EXPR : PLUS_EXPR,
9085                                       arg1, TREE_OPERAND (arg0, 1)))
9086           && !TREE_OVERFLOW (tem))
9087         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9088
9089       /* Likewise, we can simplify a comparison of a real constant with
9090          a MINUS_EXPR whose first operand is also a real constant, i.e.
9091          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
9092          floating-point types only if -fassociative-math is set.  */
9093       if (flag_associative_math
9094           && TREE_CODE (arg1) == REAL_CST
9095           && TREE_CODE (arg0) == MINUS_EXPR
9096           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9097           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9098                                       arg1))
9099           && !TREE_OVERFLOW (tem))
9100         return fold_build2_loc (loc, swap_tree_comparison (code), type,
9101                             TREE_OPERAND (arg0, 1), tem);
9102
9103       /* Fold comparisons against built-in math functions.  */
9104       if (TREE_CODE (arg1) == REAL_CST
9105           && flag_unsafe_math_optimizations
9106           && ! flag_errno_math)
9107         {
9108           enum built_in_function fcode = builtin_mathfn_code (arg0);
9109
9110           if (fcode != END_BUILTINS)
9111             {
9112               tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
9113               if (tem != NULL_TREE)
9114                 return tem;
9115             }
9116         }
9117     }
9118
9119   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9120       && CONVERT_EXPR_P (arg0))
9121     {
9122       /* If we are widening one operand of an integer comparison,
9123          see if the other operand is similarly being widened.  Perhaps we
9124          can do the comparison in the narrower type.  */
9125       tem = fold_widened_comparison (loc, code, type, arg0, arg1);
9126       if (tem)
9127         return tem;
9128
9129       /* Or if we are changing signedness.  */
9130       tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
9131       if (tem)
9132         return tem;
9133     }
9134
9135   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9136      constant, we can simplify it.  */
9137   if (TREE_CODE (arg1) == INTEGER_CST
9138       && (TREE_CODE (arg0) == MIN_EXPR
9139           || TREE_CODE (arg0) == MAX_EXPR)
9140       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9141     {
9142       tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9143       if (tem)
9144         return tem;
9145     }
9146
9147   /* Simplify comparison of something with itself.  (For IEEE
9148      floating-point, we can only do some of these simplifications.)  */
9149   if (operand_equal_p (arg0, arg1, 0))
9150     {
9151       switch (code)
9152         {
9153         case EQ_EXPR:
9154           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9155               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9156             return constant_boolean_node (1, type);
9157           break;
9158
9159         case GE_EXPR:
9160         case LE_EXPR:
9161           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9162               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9163             return constant_boolean_node (1, type);
9164           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9165
9166         case NE_EXPR:
9167           /* For NE, we can only do this simplification if integer
9168              or we don't honor IEEE floating point NaNs.  */
9169           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9170               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9171             break;
9172           /* ... fall through ...  */
9173         case GT_EXPR:
9174         case LT_EXPR:
9175           return constant_boolean_node (0, type);
9176         default:
9177           gcc_unreachable ();
9178         }
9179     }
9180
9181   /* If we are comparing an expression that just has comparisons
9182      of two integer values, arithmetic expressions of those comparisons,
9183      and constants, we can simplify it.  There are only three cases
9184      to check: the two values can either be equal, the first can be
9185      greater, or the second can be greater.  Fold the expression for
9186      those three values.  Since each value must be 0 or 1, we have
9187      eight possibilities, each of which corresponds to the constant 0
9188      or 1 or one of the six possible comparisons.
9189
9190      This handles common cases like (a > b) == 0 but also handles
9191      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9192      occur in macroized code.  */
9193
9194   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9195     {
9196       tree cval1 = 0, cval2 = 0;
9197       int save_p = 0;
9198
9199       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9200           /* Don't handle degenerate cases here; they should already
9201              have been handled anyway.  */
9202           && cval1 != 0 && cval2 != 0
9203           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9204           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9205           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9206           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9207           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9208           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9209                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9210         {
9211           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9212           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9213
9214           /* We can't just pass T to eval_subst in case cval1 or cval2
9215              was the same as ARG1.  */
9216
9217           tree high_result
9218                 = fold_build2_loc (loc, code, type,
9219                                eval_subst (loc, arg0, cval1, maxval,
9220                                            cval2, minval),
9221                                arg1);
9222           tree equal_result
9223                 = fold_build2_loc (loc, code, type,
9224                                eval_subst (loc, arg0, cval1, maxval,
9225                                            cval2, maxval),
9226                                arg1);
9227           tree low_result
9228                 = fold_build2_loc (loc, code, type,
9229                                eval_subst (loc, arg0, cval1, minval,
9230                                            cval2, maxval),
9231                                arg1);
9232
9233           /* All three of these results should be 0 or 1.  Confirm they are.
9234              Then use those values to select the proper code to use.  */
9235
9236           if (TREE_CODE (high_result) == INTEGER_CST
9237               && TREE_CODE (equal_result) == INTEGER_CST
9238               && TREE_CODE (low_result) == INTEGER_CST)
9239             {
9240               /* Make a 3-bit mask with the high-order bit being the
9241                  value for `>', the next for '=', and the low for '<'.  */
9242               switch ((integer_onep (high_result) * 4)
9243                       + (integer_onep (equal_result) * 2)
9244                       + integer_onep (low_result))
9245                 {
9246                 case 0:
9247                   /* Always false.  */
9248                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9249                 case 1:
9250                   code = LT_EXPR;
9251                   break;
9252                 case 2:
9253                   code = EQ_EXPR;
9254                   break;
9255                 case 3:
9256                   code = LE_EXPR;
9257                   break;
9258                 case 4:
9259                   code = GT_EXPR;
9260                   break;
9261                 case 5:
9262                   code = NE_EXPR;
9263                   break;
9264                 case 6:
9265                   code = GE_EXPR;
9266                   break;
9267                 case 7:
9268                   /* Always true.  */
9269                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9270                 }
9271
9272               if (save_p)
9273                 {
9274                   tem = save_expr (build2 (code, type, cval1, cval2));
9275                   SET_EXPR_LOCATION (tem, loc);
9276                   return tem;
9277                 }
9278               return fold_build2_loc (loc, code, type, cval1, cval2);
9279             }
9280         }
9281     }
9282
9283   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9284      into a single range test.  */
9285   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9286        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9287       && TREE_CODE (arg1) == INTEGER_CST
9288       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9289       && !integer_zerop (TREE_OPERAND (arg0, 1))
9290       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9291       && !TREE_OVERFLOW (arg1))
9292     {
9293       tem = fold_div_compare (loc, code, type, arg0, arg1);
9294       if (tem != NULL_TREE)
9295         return tem;
9296     }
9297
9298   /* Fold ~X op ~Y as Y op X.  */
9299   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9300       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9301     {
9302       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9303       return fold_build2_loc (loc, code, type,
9304                           fold_convert_loc (loc, cmp_type,
9305                                             TREE_OPERAND (arg1, 0)),
9306                           TREE_OPERAND (arg0, 0));
9307     }
9308
9309   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9310   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9311       && TREE_CODE (arg1) == INTEGER_CST)
9312     {
9313       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9314       return fold_build2_loc (loc, swap_tree_comparison (code), type,
9315                           TREE_OPERAND (arg0, 0),
9316                           fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9317                                        fold_convert_loc (loc, cmp_type, arg1)));
9318     }
9319
9320   return NULL_TREE;
9321 }
9322
9323
9324 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9325    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9326    argument EXPR represents the expression "z" of type TYPE.  */
9327
9328 static tree
9329 fold_mult_zconjz (location_t loc, tree type, tree expr)
9330 {
9331   tree itype = TREE_TYPE (type);
9332   tree rpart, ipart, tem;
9333
9334   if (TREE_CODE (expr) == COMPLEX_EXPR)
9335     {
9336       rpart = TREE_OPERAND (expr, 0);
9337       ipart = TREE_OPERAND (expr, 1);
9338     }
9339   else if (TREE_CODE (expr) == COMPLEX_CST)
9340     {
9341       rpart = TREE_REALPART (expr);
9342       ipart = TREE_IMAGPART (expr);
9343     }
9344   else
9345     {
9346       expr = save_expr (expr);
9347       rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9348       ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9349     }
9350
9351   rpart = save_expr (rpart);
9352   ipart = save_expr (ipart);
9353   tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9354                      fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9355                      fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9356   return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9357                           build_zero_cst (itype));
9358 }
9359
9360
9361 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9362    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9363    guarantees that P and N have the same least significant log2(M) bits.
9364    N is not otherwise constrained.  In particular, N is not normalized to
9365    0 <= N < M as is common.  In general, the precise value of P is unknown.
9366    M is chosen as large as possible such that constant N can be determined.
9367
9368    Returns M and sets *RESIDUE to N.
9369
9370    If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9371    account.  This is not always possible due to PR 35705.
9372  */
9373
9374 static unsigned HOST_WIDE_INT
9375 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9376                                  bool allow_func_align)
9377 {
9378   enum tree_code code;
9379
9380   *residue = 0;
9381
9382   code = TREE_CODE (expr);
9383   if (code == ADDR_EXPR)
9384     {
9385       unsigned int bitalign;
9386       bitalign = get_object_alignment_1 (TREE_OPERAND (expr, 0), residue);
9387       *residue /= BITS_PER_UNIT;
9388       return bitalign / BITS_PER_UNIT;
9389     }
9390   else if (code == POINTER_PLUS_EXPR)
9391     {
9392       tree op0, op1;
9393       unsigned HOST_WIDE_INT modulus;
9394       enum tree_code inner_code;
9395
9396       op0 = TREE_OPERAND (expr, 0);
9397       STRIP_NOPS (op0);
9398       modulus = get_pointer_modulus_and_residue (op0, residue,
9399                                                  allow_func_align);
9400
9401       op1 = TREE_OPERAND (expr, 1);
9402       STRIP_NOPS (op1);
9403       inner_code = TREE_CODE (op1);
9404       if (inner_code == INTEGER_CST)
9405         {
9406           *residue += TREE_INT_CST_LOW (op1);
9407           return modulus;
9408         }
9409       else if (inner_code == MULT_EXPR)
9410         {
9411           op1 = TREE_OPERAND (op1, 1);
9412           if (TREE_CODE (op1) == INTEGER_CST)
9413             {
9414               unsigned HOST_WIDE_INT align;
9415
9416               /* Compute the greatest power-of-2 divisor of op1.  */
9417               align = TREE_INT_CST_LOW (op1);
9418               align &= -align;
9419
9420               /* If align is non-zero and less than *modulus, replace
9421                  *modulus with align., If align is 0, then either op1 is 0
9422                  or the greatest power-of-2 divisor of op1 doesn't fit in an
9423                  unsigned HOST_WIDE_INT.  In either case, no additional
9424                  constraint is imposed.  */
9425               if (align)
9426                 modulus = MIN (modulus, align);
9427
9428               return modulus;
9429             }
9430         }
9431     }
9432
9433   /* If we get here, we were unable to determine anything useful about the
9434      expression.  */
9435   return 1;
9436 }
9437
9438
9439 /* Fold a binary expression of code CODE and type TYPE with operands
9440    OP0 and OP1.  LOC is the location of the resulting expression.
9441    Return the folded expression if folding is successful.  Otherwise,
9442    return NULL_TREE.  */
9443
9444 tree
9445 fold_binary_loc (location_t loc,
9446              enum tree_code code, tree type, tree op0, tree op1)
9447 {
9448   enum tree_code_class kind = TREE_CODE_CLASS (code);
9449   tree arg0, arg1, tem;
9450   tree t1 = NULL_TREE;
9451   bool strict_overflow_p;
9452
9453   gcc_assert (IS_EXPR_CODE_CLASS (kind)
9454               && TREE_CODE_LENGTH (code) == 2
9455               && op0 != NULL_TREE
9456               && op1 != NULL_TREE);
9457
9458   arg0 = op0;
9459   arg1 = op1;
9460
9461   /* Strip any conversions that don't change the mode.  This is
9462      safe for every expression, except for a comparison expression
9463      because its signedness is derived from its operands.  So, in
9464      the latter case, only strip conversions that don't change the
9465      signedness.  MIN_EXPR/MAX_EXPR also need signedness of arguments
9466      preserved.
9467
9468      Note that this is done as an internal manipulation within the
9469      constant folder, in order to find the simplest representation
9470      of the arguments so that their form can be studied.  In any
9471      cases, the appropriate type conversions should be put back in
9472      the tree that will get out of the constant folder.  */
9473
9474   if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9475     {
9476       STRIP_SIGN_NOPS (arg0);
9477       STRIP_SIGN_NOPS (arg1);
9478     }
9479   else
9480     {
9481       STRIP_NOPS (arg0);
9482       STRIP_NOPS (arg1);
9483     }
9484
9485   /* Note that TREE_CONSTANT isn't enough: static var addresses are
9486      constant but we can't do arithmetic on them.  */
9487   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9488       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9489       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9490       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9491       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9492       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9493     {
9494       if (kind == tcc_binary)
9495         {
9496           /* Make sure type and arg0 have the same saturating flag.  */
9497           gcc_assert (TYPE_SATURATING (type)
9498                       == TYPE_SATURATING (TREE_TYPE (arg0)));
9499           tem = const_binop (code, arg0, arg1);
9500         }
9501       else if (kind == tcc_comparison)
9502         tem = fold_relational_const (code, type, arg0, arg1);
9503       else
9504         tem = NULL_TREE;
9505
9506       if (tem != NULL_TREE)
9507         {
9508           if (TREE_TYPE (tem) != type)
9509             tem = fold_convert_loc (loc, type, tem);
9510           return tem;
9511         }
9512     }
9513
9514   /* If this is a commutative operation, and ARG0 is a constant, move it
9515      to ARG1 to reduce the number of tests below.  */
9516   if (commutative_tree_code (code)
9517       && tree_swap_operands_p (arg0, arg1, true))
9518     return fold_build2_loc (loc, code, type, op1, op0);
9519
9520   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9521
9522      First check for cases where an arithmetic operation is applied to a
9523      compound, conditional, or comparison operation.  Push the arithmetic
9524      operation inside the compound or conditional to see if any folding
9525      can then be done.  Convert comparison to conditional for this purpose.
9526      The also optimizes non-constant cases that used to be done in
9527      expand_expr.
9528
9529      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9530      one of the operands is a comparison and the other is a comparison, a
9531      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
9532      code below would make the expression more complex.  Change it to a
9533      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
9534      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
9535
9536   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9537        || code == EQ_EXPR || code == NE_EXPR)
9538       && ((truth_value_p (TREE_CODE (arg0))
9539            && (truth_value_p (TREE_CODE (arg1))
9540                || (TREE_CODE (arg1) == BIT_AND_EXPR
9541                    && integer_onep (TREE_OPERAND (arg1, 1)))))
9542           || (truth_value_p (TREE_CODE (arg1))
9543               && (truth_value_p (TREE_CODE (arg0))
9544                   || (TREE_CODE (arg0) == BIT_AND_EXPR
9545                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
9546     {
9547       tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9548                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9549                          : TRUTH_XOR_EXPR,
9550                          boolean_type_node,
9551                          fold_convert_loc (loc, boolean_type_node, arg0),
9552                          fold_convert_loc (loc, boolean_type_node, arg1));
9553
9554       if (code == EQ_EXPR)
9555         tem = invert_truthvalue_loc (loc, tem);
9556
9557       return fold_convert_loc (loc, type, tem);
9558     }
9559
9560   if (TREE_CODE_CLASS (code) == tcc_binary
9561       || TREE_CODE_CLASS (code) == tcc_comparison)
9562     {
9563       if (TREE_CODE (arg0) == COMPOUND_EXPR)
9564         {
9565           tem = fold_build2_loc (loc, code, type,
9566                              fold_convert_loc (loc, TREE_TYPE (op0),
9567                                                TREE_OPERAND (arg0, 1)), op1);
9568           return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9569                              tem);
9570         }
9571       if (TREE_CODE (arg1) == COMPOUND_EXPR
9572           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9573         {
9574           tem = fold_build2_loc (loc, code, type, op0,
9575                              fold_convert_loc (loc, TREE_TYPE (op1),
9576                                                TREE_OPERAND (arg1, 1)));
9577           return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9578                              tem);
9579         }
9580
9581       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9582         {
9583           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9584                                                      arg0, arg1,
9585                                                      /*cond_first_p=*/1);
9586           if (tem != NULL_TREE)
9587             return tem;
9588         }
9589
9590       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9591         {
9592           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9593                                                      arg1, arg0,
9594                                                      /*cond_first_p=*/0);
9595           if (tem != NULL_TREE)
9596             return tem;
9597         }
9598     }
9599
9600   switch (code)
9601     {
9602     case MEM_REF:
9603       /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2].  */
9604       if (TREE_CODE (arg0) == ADDR_EXPR
9605           && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9606         {
9607           tree iref = TREE_OPERAND (arg0, 0);
9608           return fold_build2 (MEM_REF, type,
9609                               TREE_OPERAND (iref, 0),
9610                               int_const_binop (PLUS_EXPR, arg1,
9611                                                TREE_OPERAND (iref, 1)));
9612         }
9613
9614       /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2].  */
9615       if (TREE_CODE (arg0) == ADDR_EXPR
9616           && handled_component_p (TREE_OPERAND (arg0, 0)))
9617         {
9618           tree base;
9619           HOST_WIDE_INT coffset;
9620           base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9621                                                 &coffset);
9622           if (!base)
9623             return NULL_TREE;
9624           return fold_build2 (MEM_REF, type,
9625                               build_fold_addr_expr (base),
9626                               int_const_binop (PLUS_EXPR, arg1,
9627                                                size_int (coffset)));
9628         }
9629
9630       return NULL_TREE;
9631
9632     case POINTER_PLUS_EXPR:
9633       /* 0 +p index -> (type)index */
9634       if (integer_zerop (arg0))
9635         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9636
9637       /* PTR +p 0 -> PTR */
9638       if (integer_zerop (arg1))
9639         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9640
9641       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
9642       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9643            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9644         return fold_convert_loc (loc, type,
9645                                  fold_build2_loc (loc, PLUS_EXPR, sizetype,
9646                                               fold_convert_loc (loc, sizetype,
9647                                                                 arg1),
9648                                               fold_convert_loc (loc, sizetype,
9649                                                                 arg0)));
9650
9651       /* (PTR +p B) +p A -> PTR +p (B + A) */
9652       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9653         {
9654           tree inner;
9655           tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
9656           tree arg00 = TREE_OPERAND (arg0, 0);
9657           inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
9658                                arg01, fold_convert_loc (loc, sizetype, arg1));
9659           return fold_convert_loc (loc, type,
9660                                    fold_build_pointer_plus_loc (loc,
9661                                                                 arg00, inner));
9662         }
9663
9664       /* PTR_CST +p CST -> CST1 */
9665       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9666         return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
9667                             fold_convert_loc (loc, type, arg1));
9668
9669      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9670         of the array.  Loop optimizer sometimes produce this type of
9671         expressions.  */
9672       if (TREE_CODE (arg0) == ADDR_EXPR)
9673         {
9674           tem = try_move_mult_to_index (loc, arg0,
9675                                         fold_convert_loc (loc, sizetype, arg1));
9676           if (tem)
9677             return fold_convert_loc (loc, type, tem);
9678         }
9679
9680       return NULL_TREE;
9681
9682     case PLUS_EXPR:
9683       /* A + (-B) -> A - B */
9684       if (TREE_CODE (arg1) == NEGATE_EXPR)
9685         return fold_build2_loc (loc, MINUS_EXPR, type,
9686                             fold_convert_loc (loc, type, arg0),
9687                             fold_convert_loc (loc, type,
9688                                               TREE_OPERAND (arg1, 0)));
9689       /* (-A) + B -> B - A */
9690       if (TREE_CODE (arg0) == NEGATE_EXPR
9691           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9692         return fold_build2_loc (loc, MINUS_EXPR, type,
9693                             fold_convert_loc (loc, type, arg1),
9694                             fold_convert_loc (loc, type,
9695                                               TREE_OPERAND (arg0, 0)));
9696
9697       if (INTEGRAL_TYPE_P (type))
9698         {
9699           /* Convert ~A + 1 to -A.  */
9700           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9701               && integer_onep (arg1))
9702             return fold_build1_loc (loc, NEGATE_EXPR, type,
9703                                 fold_convert_loc (loc, type,
9704                                                   TREE_OPERAND (arg0, 0)));
9705
9706           /* ~X + X is -1.  */
9707           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9708               && !TYPE_OVERFLOW_TRAPS (type))
9709             {
9710               tree tem = TREE_OPERAND (arg0, 0);
9711
9712               STRIP_NOPS (tem);
9713               if (operand_equal_p (tem, arg1, 0))
9714                 {
9715                   t1 = build_int_cst_type (type, -1);
9716                   return omit_one_operand_loc (loc, type, t1, arg1);
9717                 }
9718             }
9719
9720           /* X + ~X is -1.  */
9721           if (TREE_CODE (arg1) == BIT_NOT_EXPR
9722               && !TYPE_OVERFLOW_TRAPS (type))
9723             {
9724               tree tem = TREE_OPERAND (arg1, 0);
9725
9726               STRIP_NOPS (tem);
9727               if (operand_equal_p (arg0, tem, 0))
9728                 {
9729                   t1 = build_int_cst_type (type, -1);
9730                   return omit_one_operand_loc (loc, type, t1, arg0);
9731                 }
9732             }
9733
9734           /* X + (X / CST) * -CST is X % CST.  */
9735           if (TREE_CODE (arg1) == MULT_EXPR
9736               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9737               && operand_equal_p (arg0,
9738                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9739             {
9740               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9741               tree cst1 = TREE_OPERAND (arg1, 1);
9742               tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9743                                       cst1, cst0);
9744               if (sum && integer_zerop (sum))
9745                 return fold_convert_loc (loc, type,
9746                                          fold_build2_loc (loc, TRUNC_MOD_EXPR,
9747                                                       TREE_TYPE (arg0), arg0,
9748                                                       cst0));
9749             }
9750         }
9751
9752       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9753          same or one.  Make sure type is not saturating.
9754          fold_plusminus_mult_expr will re-associate.  */
9755       if ((TREE_CODE (arg0) == MULT_EXPR
9756            || TREE_CODE (arg1) == MULT_EXPR)
9757           && !TYPE_SATURATING (type)
9758           && (!FLOAT_TYPE_P (type) || flag_associative_math))
9759         {
9760           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9761           if (tem)
9762             return tem;
9763         }
9764
9765       if (! FLOAT_TYPE_P (type))
9766         {
9767           if (integer_zerop (arg1))
9768             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9769
9770           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9771              with a constant, and the two constants have no bits in common,
9772              we should treat this as a BIT_IOR_EXPR since this may produce more
9773              simplifications.  */
9774           if (TREE_CODE (arg0) == BIT_AND_EXPR
9775               && TREE_CODE (arg1) == BIT_AND_EXPR
9776               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9777               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9778               && integer_zerop (const_binop (BIT_AND_EXPR,
9779                                              TREE_OPERAND (arg0, 1),
9780                                              TREE_OPERAND (arg1, 1))))
9781             {
9782               code = BIT_IOR_EXPR;
9783               goto bit_ior;
9784             }
9785
9786           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9787              (plus (plus (mult) (mult)) (foo)) so that we can
9788              take advantage of the factoring cases below.  */
9789           if (TYPE_OVERFLOW_WRAPS (type)
9790               && (((TREE_CODE (arg0) == PLUS_EXPR
9791                     || TREE_CODE (arg0) == MINUS_EXPR)
9792                    && TREE_CODE (arg1) == MULT_EXPR)
9793                   || ((TREE_CODE (arg1) == PLUS_EXPR
9794                        || TREE_CODE (arg1) == MINUS_EXPR)
9795                       && TREE_CODE (arg0) == MULT_EXPR)))
9796             {
9797               tree parg0, parg1, parg, marg;
9798               enum tree_code pcode;
9799
9800               if (TREE_CODE (arg1) == MULT_EXPR)
9801                 parg = arg0, marg = arg1;
9802               else
9803                 parg = arg1, marg = arg0;
9804               pcode = TREE_CODE (parg);
9805               parg0 = TREE_OPERAND (parg, 0);
9806               parg1 = TREE_OPERAND (parg, 1);
9807               STRIP_NOPS (parg0);
9808               STRIP_NOPS (parg1);
9809
9810               if (TREE_CODE (parg0) == MULT_EXPR
9811                   && TREE_CODE (parg1) != MULT_EXPR)
9812                 return fold_build2_loc (loc, pcode, type,
9813                                     fold_build2_loc (loc, PLUS_EXPR, type,
9814                                                  fold_convert_loc (loc, type,
9815                                                                    parg0),
9816                                                  fold_convert_loc (loc, type,
9817                                                                    marg)),
9818                                     fold_convert_loc (loc, type, parg1));
9819               if (TREE_CODE (parg0) != MULT_EXPR
9820                   && TREE_CODE (parg1) == MULT_EXPR)
9821                 return
9822                   fold_build2_loc (loc, PLUS_EXPR, type,
9823                                fold_convert_loc (loc, type, parg0),
9824                                fold_build2_loc (loc, pcode, type,
9825                                             fold_convert_loc (loc, type, marg),
9826                                             fold_convert_loc (loc, type,
9827                                                               parg1)));
9828             }
9829         }
9830       else
9831         {
9832           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
9833           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9834             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9835
9836           /* Likewise if the operands are reversed.  */
9837           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9838             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9839
9840           /* Convert X + -C into X - C.  */
9841           if (TREE_CODE (arg1) == REAL_CST
9842               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9843             {
9844               tem = fold_negate_const (arg1, type);
9845               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9846                 return fold_build2_loc (loc, MINUS_EXPR, type,
9847                                     fold_convert_loc (loc, type, arg0),
9848                                     fold_convert_loc (loc, type, tem));
9849             }
9850
9851           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9852              to __complex__ ( x, y ).  This is not the same for SNaNs or
9853              if signed zeros are involved.  */
9854           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9855               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9856               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9857             {
9858               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9859               tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9860               tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9861               bool arg0rz = false, arg0iz = false;
9862               if ((arg0r && (arg0rz = real_zerop (arg0r)))
9863                   || (arg0i && (arg0iz = real_zerop (arg0i))))
9864                 {
9865                   tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9866                   tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9867                   if (arg0rz && arg1i && real_zerop (arg1i))
9868                     {
9869                       tree rp = arg1r ? arg1r
9870                                   : build1 (REALPART_EXPR, rtype, arg1);
9871                       tree ip = arg0i ? arg0i
9872                                   : build1 (IMAGPART_EXPR, rtype, arg0);
9873                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9874                     }
9875                   else if (arg0iz && arg1r && real_zerop (arg1r))
9876                     {
9877                       tree rp = arg0r ? arg0r
9878                                   : build1 (REALPART_EXPR, rtype, arg0);
9879                       tree ip = arg1i ? arg1i
9880                                   : build1 (IMAGPART_EXPR, rtype, arg1);
9881                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9882                     }
9883                 }
9884             }
9885
9886           if (flag_unsafe_math_optimizations
9887               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9888               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9889               && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9890             return tem;
9891
9892           /* Convert x+x into x*2.0.  */
9893           if (operand_equal_p (arg0, arg1, 0)
9894               && SCALAR_FLOAT_TYPE_P (type))
9895             return fold_build2_loc (loc, MULT_EXPR, type, arg0,
9896                                 build_real (type, dconst2));
9897
9898           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9899              We associate floats only if the user has specified
9900              -fassociative-math.  */
9901           if (flag_associative_math
9902               && TREE_CODE (arg1) == PLUS_EXPR
9903               && TREE_CODE (arg0) != MULT_EXPR)
9904             {
9905               tree tree10 = TREE_OPERAND (arg1, 0);
9906               tree tree11 = TREE_OPERAND (arg1, 1);
9907               if (TREE_CODE (tree11) == MULT_EXPR
9908                   && TREE_CODE (tree10) == MULT_EXPR)
9909                 {
9910                   tree tree0;
9911                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9912                   return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9913                 }
9914             }
9915           /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9916              We associate floats only if the user has specified
9917              -fassociative-math.  */
9918           if (flag_associative_math
9919               && TREE_CODE (arg0) == PLUS_EXPR
9920               && TREE_CODE (arg1) != MULT_EXPR)
9921             {
9922               tree tree00 = TREE_OPERAND (arg0, 0);
9923               tree tree01 = TREE_OPERAND (arg0, 1);
9924               if (TREE_CODE (tree01) == MULT_EXPR
9925                   && TREE_CODE (tree00) == MULT_EXPR)
9926                 {
9927                   tree tree0;
9928                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9929                   return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9930                 }
9931             }
9932         }
9933
9934      bit_rotate:
9935       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9936          is a rotate of A by C1 bits.  */
9937       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9938          is a rotate of A by B bits.  */
9939       {
9940         enum tree_code code0, code1;
9941         tree rtype;
9942         code0 = TREE_CODE (arg0);
9943         code1 = TREE_CODE (arg1);
9944         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9945              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9946             && operand_equal_p (TREE_OPERAND (arg0, 0),
9947                                 TREE_OPERAND (arg1, 0), 0)
9948             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9949                 TYPE_UNSIGNED (rtype))
9950             /* Only create rotates in complete modes.  Other cases are not
9951                expanded properly.  */
9952             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
9953           {
9954             tree tree01, tree11;
9955             enum tree_code code01, code11;
9956
9957             tree01 = TREE_OPERAND (arg0, 1);
9958             tree11 = TREE_OPERAND (arg1, 1);
9959             STRIP_NOPS (tree01);
9960             STRIP_NOPS (tree11);
9961             code01 = TREE_CODE (tree01);
9962             code11 = TREE_CODE (tree11);
9963             if (code01 == INTEGER_CST
9964                 && code11 == INTEGER_CST
9965                 && TREE_INT_CST_HIGH (tree01) == 0
9966                 && TREE_INT_CST_HIGH (tree11) == 0
9967                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9968                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9969               {
9970                 tem = build2_loc (loc, LROTATE_EXPR,
9971                                   TREE_TYPE (TREE_OPERAND (arg0, 0)),
9972                                   TREE_OPERAND (arg0, 0),
9973                                   code0 == LSHIFT_EXPR ? tree01 : tree11);
9974                 return fold_convert_loc (loc, type, tem);
9975               }
9976             else if (code11 == MINUS_EXPR)
9977               {
9978                 tree tree110, tree111;
9979                 tree110 = TREE_OPERAND (tree11, 0);
9980                 tree111 = TREE_OPERAND (tree11, 1);
9981                 STRIP_NOPS (tree110);
9982                 STRIP_NOPS (tree111);
9983                 if (TREE_CODE (tree110) == INTEGER_CST
9984                     && 0 == compare_tree_int (tree110,
9985                                               TYPE_PRECISION
9986                                               (TREE_TYPE (TREE_OPERAND
9987                                                           (arg0, 0))))
9988                     && operand_equal_p (tree01, tree111, 0))
9989                   return
9990                     fold_convert_loc (loc, type,
9991                                       build2 ((code0 == LSHIFT_EXPR
9992                                                ? LROTATE_EXPR
9993                                                : RROTATE_EXPR),
9994                                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
9995                                               TREE_OPERAND (arg0, 0), tree01));
9996               }
9997             else if (code01 == MINUS_EXPR)
9998               {
9999                 tree tree010, tree011;
10000                 tree010 = TREE_OPERAND (tree01, 0);
10001                 tree011 = TREE_OPERAND (tree01, 1);
10002                 STRIP_NOPS (tree010);
10003                 STRIP_NOPS (tree011);
10004                 if (TREE_CODE (tree010) == INTEGER_CST
10005                     && 0 == compare_tree_int (tree010,
10006                                               TYPE_PRECISION
10007                                               (TREE_TYPE (TREE_OPERAND
10008                                                           (arg0, 0))))
10009                     && operand_equal_p (tree11, tree011, 0))
10010                     return fold_convert_loc
10011                       (loc, type,
10012                        build2 ((code0 != LSHIFT_EXPR
10013                                 ? LROTATE_EXPR
10014                                 : RROTATE_EXPR),
10015                                TREE_TYPE (TREE_OPERAND (arg0, 0)),
10016                                TREE_OPERAND (arg0, 0), tree11));
10017               }
10018           }
10019       }
10020
10021     associate:
10022       /* In most languages, can't associate operations on floats through
10023          parentheses.  Rather than remember where the parentheses were, we
10024          don't associate floats at all, unless the user has specified
10025          -fassociative-math.
10026          And, we need to make sure type is not saturating.  */
10027
10028       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10029           && !TYPE_SATURATING (type))
10030         {
10031           tree var0, con0, lit0, minus_lit0;
10032           tree var1, con1, lit1, minus_lit1;
10033           bool ok = true;
10034
10035           /* Split both trees into variables, constants, and literals.  Then
10036              associate each group together, the constants with literals,
10037              then the result with variables.  This increases the chances of
10038              literals being recombined later and of generating relocatable
10039              expressions for the sum of a constant and literal.  */
10040           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10041           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10042                              code == MINUS_EXPR);
10043
10044           /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
10045           if (code == MINUS_EXPR)
10046             code = PLUS_EXPR;
10047
10048           /* With undefined overflow we can only associate constants with one
10049              variable, and constants whose association doesn't overflow.  */
10050           if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10051               || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10052             {
10053               if (var0 && var1)
10054                 {
10055                   tree tmp0 = var0;
10056                   tree tmp1 = var1;
10057
10058                   if (TREE_CODE (tmp0) == NEGATE_EXPR)
10059                     tmp0 = TREE_OPERAND (tmp0, 0);
10060                   if (TREE_CODE (tmp1) == NEGATE_EXPR)
10061                     tmp1 = TREE_OPERAND (tmp1, 0);
10062                   /* The only case we can still associate with two variables
10063                      is if they are the same, modulo negation.  */
10064                   if (!operand_equal_p (tmp0, tmp1, 0))
10065                     ok = false;
10066                 }
10067
10068               if (ok && lit0 && lit1)
10069                 {
10070                   tree tmp0 = fold_convert (type, lit0);
10071                   tree tmp1 = fold_convert (type, lit1);
10072
10073                   if (!TREE_OVERFLOW (tmp0) && !TREE_OVERFLOW (tmp1)
10074                       && TREE_OVERFLOW (fold_build2 (code, type, tmp0, tmp1)))
10075                     ok = false;
10076                 }
10077             }
10078
10079           /* Only do something if we found more than two objects.  Otherwise,
10080              nothing has changed and we risk infinite recursion.  */
10081           if (ok
10082               && (2 < ((var0 != 0) + (var1 != 0)
10083                        + (con0 != 0) + (con1 != 0)
10084                        + (lit0 != 0) + (lit1 != 0)
10085                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
10086             {
10087               var0 = associate_trees (loc, var0, var1, code, type);
10088               con0 = associate_trees (loc, con0, con1, code, type);
10089               lit0 = associate_trees (loc, lit0, lit1, code, type);
10090               minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type);
10091
10092               /* Preserve the MINUS_EXPR if the negative part of the literal is
10093                  greater than the positive part.  Otherwise, the multiplicative
10094                  folding code (i.e extract_muldiv) may be fooled in case
10095                  unsigned constants are subtracted, like in the following
10096                  example: ((X*2 + 4) - 8U)/2.  */
10097               if (minus_lit0 && lit0)
10098                 {
10099                   if (TREE_CODE (lit0) == INTEGER_CST
10100                       && TREE_CODE (minus_lit0) == INTEGER_CST
10101                       && tree_int_cst_lt (lit0, minus_lit0))
10102                     {
10103                       minus_lit0 = associate_trees (loc, minus_lit0, lit0,
10104                                                     MINUS_EXPR, type);
10105                       lit0 = 0;
10106                     }
10107                   else
10108                     {
10109                       lit0 = associate_trees (loc, lit0, minus_lit0,
10110                                               MINUS_EXPR, type);
10111                       minus_lit0 = 0;
10112                     }
10113                 }
10114               if (minus_lit0)
10115                 {
10116                   if (con0 == 0)
10117                     return
10118                       fold_convert_loc (loc, type,
10119                                         associate_trees (loc, var0, minus_lit0,
10120                                                          MINUS_EXPR, type));
10121                   else
10122                     {
10123                       con0 = associate_trees (loc, con0, minus_lit0,
10124                                               MINUS_EXPR, type);
10125                       return
10126                         fold_convert_loc (loc, type,
10127                                           associate_trees (loc, var0, con0,
10128                                                            PLUS_EXPR, type));
10129                     }
10130                 }
10131
10132               con0 = associate_trees (loc, con0, lit0, code, type);
10133               return
10134                 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10135                                                               code, type));
10136             }
10137         }
10138
10139       return NULL_TREE;
10140
10141     case MINUS_EXPR:
10142       /* Pointer simplifications for subtraction, simple reassociations. */
10143       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10144         {
10145           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10146           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10147               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10148             {
10149               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10150               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10151               tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10152               tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10153               return fold_build2_loc (loc, PLUS_EXPR, type,
10154                                   fold_build2_loc (loc, MINUS_EXPR, type,
10155                                                arg00, arg10),
10156                                   fold_build2_loc (loc, MINUS_EXPR, type,
10157                                                arg01, arg11));
10158             }
10159           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10160           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10161             {
10162               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10163               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10164               tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10165                                       fold_convert_loc (loc, type, arg1));
10166               if (tmp)
10167                 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10168             }
10169         }
10170       /* A - (-B) -> A + B */
10171       if (TREE_CODE (arg1) == NEGATE_EXPR)
10172         return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10173                             fold_convert_loc (loc, type,
10174                                               TREE_OPERAND (arg1, 0)));
10175       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10176       if (TREE_CODE (arg0) == NEGATE_EXPR
10177           && (FLOAT_TYPE_P (type)
10178               || INTEGRAL_TYPE_P (type))
10179           && negate_expr_p (arg1)
10180           && reorder_operands_p (arg0, arg1))
10181         return fold_build2_loc (loc, MINUS_EXPR, type,
10182                             fold_convert_loc (loc, type,
10183                                               negate_expr (arg1)),
10184                             fold_convert_loc (loc, type,
10185                                               TREE_OPERAND (arg0, 0)));
10186       /* Convert -A - 1 to ~A.  */
10187       if (INTEGRAL_TYPE_P (type)
10188           && TREE_CODE (arg0) == NEGATE_EXPR
10189           && integer_onep (arg1)
10190           && !TYPE_OVERFLOW_TRAPS (type))
10191         return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10192                             fold_convert_loc (loc, type,
10193                                               TREE_OPERAND (arg0, 0)));
10194
10195       /* Convert -1 - A to ~A.  */
10196       if (INTEGRAL_TYPE_P (type)
10197           && integer_all_onesp (arg0))
10198         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10199
10200
10201       /* X - (X / CST) * CST is X % CST.  */
10202       if (INTEGRAL_TYPE_P (type)
10203           && TREE_CODE (arg1) == MULT_EXPR
10204           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10205           && operand_equal_p (arg0,
10206                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10207           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10208                               TREE_OPERAND (arg1, 1), 0))
10209         return
10210           fold_convert_loc (loc, type,
10211                             fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10212                                          arg0, TREE_OPERAND (arg1, 1)));
10213
10214       if (! FLOAT_TYPE_P (type))
10215         {
10216           if (integer_zerop (arg0))
10217             return negate_expr (fold_convert_loc (loc, type, arg1));
10218           if (integer_zerop (arg1))
10219             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10220
10221           /* Fold A - (A & B) into ~B & A.  */
10222           if (!TREE_SIDE_EFFECTS (arg0)
10223               && TREE_CODE (arg1) == BIT_AND_EXPR)
10224             {
10225               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10226                 {
10227                   tree arg10 = fold_convert_loc (loc, type,
10228                                                  TREE_OPERAND (arg1, 0));
10229                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10230                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10231                                                    type, arg10),
10232                                       fold_convert_loc (loc, type, arg0));
10233                 }
10234               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10235                 {
10236                   tree arg11 = fold_convert_loc (loc,
10237                                                  type, TREE_OPERAND (arg1, 1));
10238                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10239                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10240                                                    type, arg11),
10241                                       fold_convert_loc (loc, type, arg0));
10242                 }
10243             }
10244
10245           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10246              any power of 2 minus 1.  */
10247           if (TREE_CODE (arg0) == BIT_AND_EXPR
10248               && TREE_CODE (arg1) == BIT_AND_EXPR
10249               && operand_equal_p (TREE_OPERAND (arg0, 0),
10250                                   TREE_OPERAND (arg1, 0), 0))
10251             {
10252               tree mask0 = TREE_OPERAND (arg0, 1);
10253               tree mask1 = TREE_OPERAND (arg1, 1);
10254               tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10255
10256               if (operand_equal_p (tem, mask1, 0))
10257                 {
10258                   tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10259                                      TREE_OPERAND (arg0, 0), mask1);
10260                   return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10261                 }
10262             }
10263         }
10264
10265       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10266       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10267         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10268
10269       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10270          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10271          (-ARG1 + ARG0) reduces to -ARG1.  */
10272       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10273         return negate_expr (fold_convert_loc (loc, type, arg1));
10274
10275       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10276          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10277          signed zeros are involved.  */
10278       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10279           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10280           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10281         {
10282           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10283           tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10284           tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10285           bool arg0rz = false, arg0iz = false;
10286           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10287               || (arg0i && (arg0iz = real_zerop (arg0i))))
10288             {
10289               tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10290               tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10291               if (arg0rz && arg1i && real_zerop (arg1i))
10292                 {
10293                   tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10294                                          arg1r ? arg1r
10295                                          : build1 (REALPART_EXPR, rtype, arg1));
10296                   tree ip = arg0i ? arg0i
10297                     : build1 (IMAGPART_EXPR, rtype, arg0);
10298                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10299                 }
10300               else if (arg0iz && arg1r && real_zerop (arg1r))
10301                 {
10302                   tree rp = arg0r ? arg0r
10303                     : build1 (REALPART_EXPR, rtype, arg0);
10304                   tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10305                                          arg1i ? arg1i
10306                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10307                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10308                 }
10309             }
10310         }
10311
10312       /* Fold &x - &x.  This can happen from &x.foo - &x.
10313          This is unsafe for certain floats even in non-IEEE formats.
10314          In IEEE, it is unsafe because it does wrong for NaNs.
10315          Also note that operand_equal_p is always false if an operand
10316          is volatile.  */
10317
10318       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10319           && operand_equal_p (arg0, arg1, 0))
10320         return build_zero_cst (type);
10321
10322       /* A - B -> A + (-B) if B is easily negatable.  */
10323       if (negate_expr_p (arg1)
10324           && ((FLOAT_TYPE_P (type)
10325                /* Avoid this transformation if B is a positive REAL_CST.  */
10326                && (TREE_CODE (arg1) != REAL_CST
10327                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10328               || INTEGRAL_TYPE_P (type)))
10329         return fold_build2_loc (loc, PLUS_EXPR, type,
10330                             fold_convert_loc (loc, type, arg0),
10331                             fold_convert_loc (loc, type,
10332                                               negate_expr (arg1)));
10333
10334       /* Try folding difference of addresses.  */
10335       {
10336         HOST_WIDE_INT diff;
10337
10338         if ((TREE_CODE (arg0) == ADDR_EXPR
10339              || TREE_CODE (arg1) == ADDR_EXPR)
10340             && ptr_difference_const (arg0, arg1, &diff))
10341           return build_int_cst_type (type, diff);
10342       }
10343
10344       /* Fold &a[i] - &a[j] to i-j.  */
10345       if (TREE_CODE (arg0) == ADDR_EXPR
10346           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10347           && TREE_CODE (arg1) == ADDR_EXPR
10348           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10349         {
10350           tree aref0 = TREE_OPERAND (arg0, 0);
10351           tree aref1 = TREE_OPERAND (arg1, 0);
10352           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10353                                TREE_OPERAND (aref1, 0), 0))
10354             {
10355               tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10356               tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10357               tree esz = array_ref_element_size (aref0);
10358               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10359               return fold_build2_loc (loc, MULT_EXPR, type, diff,
10360                                   fold_convert_loc (loc, type, esz));
10361
10362             }
10363         }
10364
10365       if (FLOAT_TYPE_P (type)
10366           && flag_unsafe_math_optimizations
10367           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10368           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10369           && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10370         return tem;
10371
10372       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10373          same or one.  Make sure type is not saturating.
10374          fold_plusminus_mult_expr will re-associate.  */
10375       if ((TREE_CODE (arg0) == MULT_EXPR
10376            || TREE_CODE (arg1) == MULT_EXPR)
10377           && !TYPE_SATURATING (type)
10378           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10379         {
10380           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10381           if (tem)
10382             return tem;
10383         }
10384
10385       goto associate;
10386
10387     case MULT_EXPR:
10388       /* (-A) * (-B) -> A * B  */
10389       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10390         return fold_build2_loc (loc, MULT_EXPR, type,
10391                             fold_convert_loc (loc, type,
10392                                               TREE_OPERAND (arg0, 0)),
10393                             fold_convert_loc (loc, type,
10394                                               negate_expr (arg1)));
10395       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10396         return fold_build2_loc (loc, MULT_EXPR, type,
10397                             fold_convert_loc (loc, type,
10398                                               negate_expr (arg0)),
10399                             fold_convert_loc (loc, type,
10400                                               TREE_OPERAND (arg1, 0)));
10401
10402       if (! FLOAT_TYPE_P (type))
10403         {
10404           if (integer_zerop (arg1))
10405             return omit_one_operand_loc (loc, type, arg1, arg0);
10406           if (integer_onep (arg1))
10407             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10408           /* Transform x * -1 into -x.  Make sure to do the negation
10409              on the original operand with conversions not stripped
10410              because we can only strip non-sign-changing conversions.  */
10411           if (integer_all_onesp (arg1))
10412             return fold_convert_loc (loc, type, negate_expr (op0));
10413           /* Transform x * -C into -x * C if x is easily negatable.  */
10414           if (TREE_CODE (arg1) == INTEGER_CST
10415               && tree_int_cst_sgn (arg1) == -1
10416               && negate_expr_p (arg0)
10417               && (tem = negate_expr (arg1)) != arg1
10418               && !TREE_OVERFLOW (tem))
10419             return fold_build2_loc (loc, MULT_EXPR, type,
10420                                 fold_convert_loc (loc, type,
10421                                                   negate_expr (arg0)),
10422                                 tem);
10423
10424           /* (a * (1 << b)) is (a << b)  */
10425           if (TREE_CODE (arg1) == LSHIFT_EXPR
10426               && integer_onep (TREE_OPERAND (arg1, 0)))
10427             return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10428                                 TREE_OPERAND (arg1, 1));
10429           if (TREE_CODE (arg0) == LSHIFT_EXPR
10430               && integer_onep (TREE_OPERAND (arg0, 0)))
10431             return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10432                                 TREE_OPERAND (arg0, 1));
10433
10434           /* (A + A) * C -> A * 2 * C  */
10435           if (TREE_CODE (arg0) == PLUS_EXPR
10436               && TREE_CODE (arg1) == INTEGER_CST
10437               && operand_equal_p (TREE_OPERAND (arg0, 0),
10438                                   TREE_OPERAND (arg0, 1), 0))
10439             return fold_build2_loc (loc, MULT_EXPR, type,
10440                                 omit_one_operand_loc (loc, type,
10441                                                   TREE_OPERAND (arg0, 0),
10442                                                   TREE_OPERAND (arg0, 1)),
10443                                 fold_build2_loc (loc, MULT_EXPR, type,
10444                                              build_int_cst (type, 2) , arg1));
10445
10446           strict_overflow_p = false;
10447           if (TREE_CODE (arg1) == INTEGER_CST
10448               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10449                                              &strict_overflow_p)))
10450             {
10451               if (strict_overflow_p)
10452                 fold_overflow_warning (("assuming signed overflow does not "
10453                                         "occur when simplifying "
10454                                         "multiplication"),
10455                                        WARN_STRICT_OVERFLOW_MISC);
10456               return fold_convert_loc (loc, type, tem);
10457             }
10458
10459           /* Optimize z * conj(z) for integer complex numbers.  */
10460           if (TREE_CODE (arg0) == CONJ_EXPR
10461               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10462             return fold_mult_zconjz (loc, type, arg1);
10463           if (TREE_CODE (arg1) == CONJ_EXPR
10464               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10465             return fold_mult_zconjz (loc, type, arg0);
10466         }
10467       else
10468         {
10469           /* Maybe fold x * 0 to 0.  The expressions aren't the same
10470              when x is NaN, since x * 0 is also NaN.  Nor are they the
10471              same in modes with signed zeros, since multiplying a
10472              negative value by 0 gives -0, not +0.  */
10473           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10474               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10475               && real_zerop (arg1))
10476             return omit_one_operand_loc (loc, type, arg1, arg0);
10477           /* In IEEE floating point, x*1 is not equivalent to x for snans.
10478              Likewise for complex arithmetic with signed zeros.  */
10479           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10480               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10481                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10482               && real_onep (arg1))
10483             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10484
10485           /* Transform x * -1.0 into -x.  */
10486           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10487               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10488                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10489               && real_minus_onep (arg1))
10490             return fold_convert_loc (loc, type, negate_expr (arg0));
10491
10492           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
10493              the result for floating point types due to rounding so it is applied
10494              only if -fassociative-math was specify.  */
10495           if (flag_associative_math
10496               && TREE_CODE (arg0) == RDIV_EXPR
10497               && TREE_CODE (arg1) == REAL_CST
10498               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10499             {
10500               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10501                                       arg1);
10502               if (tem)
10503                 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10504                                     TREE_OPERAND (arg0, 1));
10505             }
10506
10507           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
10508           if (operand_equal_p (arg0, arg1, 0))
10509             {
10510               tree tem = fold_strip_sign_ops (arg0);
10511               if (tem != NULL_TREE)
10512                 {
10513                   tem = fold_convert_loc (loc, type, tem);
10514                   return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
10515                 }
10516             }
10517
10518           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10519              This is not the same for NaNs or if signed zeros are
10520              involved.  */
10521           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10522               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10523               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10524               && TREE_CODE (arg1) == COMPLEX_CST
10525               && real_zerop (TREE_REALPART (arg1)))
10526             {
10527               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10528               if (real_onep (TREE_IMAGPART (arg1)))
10529                 return
10530                   fold_build2_loc (loc, COMPLEX_EXPR, type,
10531                                negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10532                                                              rtype, arg0)),
10533                                fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10534               else if (real_minus_onep (TREE_IMAGPART (arg1)))
10535                 return
10536                   fold_build2_loc (loc, COMPLEX_EXPR, type,
10537                                fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10538                                negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10539                                                              rtype, arg0)));
10540             }
10541
10542           /* Optimize z * conj(z) for floating point complex numbers.
10543              Guarded by flag_unsafe_math_optimizations as non-finite
10544              imaginary components don't produce scalar results.  */
10545           if (flag_unsafe_math_optimizations
10546               && TREE_CODE (arg0) == CONJ_EXPR
10547               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10548             return fold_mult_zconjz (loc, type, arg1);
10549           if (flag_unsafe_math_optimizations
10550               && TREE_CODE (arg1) == CONJ_EXPR
10551               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10552             return fold_mult_zconjz (loc, type, arg0);
10553
10554           if (flag_unsafe_math_optimizations)
10555             {
10556               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10557               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10558
10559               /* Optimizations of root(...)*root(...).  */
10560               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10561                 {
10562                   tree rootfn, arg;
10563                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10564                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10565
10566                   /* Optimize sqrt(x)*sqrt(x) as x.  */
10567                   if (BUILTIN_SQRT_P (fcode0)
10568                       && operand_equal_p (arg00, arg10, 0)
10569                       && ! HONOR_SNANS (TYPE_MODE (type)))
10570                     return arg00;
10571
10572                   /* Optimize root(x)*root(y) as root(x*y).  */
10573                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10574                   arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
10575                   return build_call_expr_loc (loc, rootfn, 1, arg);
10576                 }
10577
10578               /* Optimize expN(x)*expN(y) as expN(x+y).  */
10579               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10580                 {
10581                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10582                   tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10583                                           CALL_EXPR_ARG (arg0, 0),
10584                                           CALL_EXPR_ARG (arg1, 0));
10585                   return build_call_expr_loc (loc, expfn, 1, arg);
10586                 }
10587
10588               /* Optimizations of pow(...)*pow(...).  */
10589               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10590                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10591                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10592                 {
10593                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10594                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10595                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10596                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10597
10598                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
10599                   if (operand_equal_p (arg01, arg11, 0))
10600                     {
10601                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10602                       tree arg = fold_build2_loc (loc, MULT_EXPR, type,
10603                                               arg00, arg10);
10604                       return build_call_expr_loc (loc, powfn, 2, arg, arg01);
10605                     }
10606
10607                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
10608                   if (operand_equal_p (arg00, arg10, 0))
10609                     {
10610                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10611                       tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10612                                               arg01, arg11);
10613                       return build_call_expr_loc (loc, powfn, 2, arg00, arg);
10614                     }
10615                 }
10616
10617               /* Optimize tan(x)*cos(x) as sin(x).  */
10618               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10619                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10620                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10621                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10622                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10623                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10624                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10625                                       CALL_EXPR_ARG (arg1, 0), 0))
10626                 {
10627                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10628
10629                   if (sinfn != NULL_TREE)
10630                     return build_call_expr_loc (loc, sinfn, 1,
10631                                             CALL_EXPR_ARG (arg0, 0));
10632                 }
10633
10634               /* Optimize x*pow(x,c) as pow(x,c+1).  */
10635               if (fcode1 == BUILT_IN_POW
10636                   || fcode1 == BUILT_IN_POWF
10637                   || fcode1 == BUILT_IN_POWL)
10638                 {
10639                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10640                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10641                   if (TREE_CODE (arg11) == REAL_CST
10642                       && !TREE_OVERFLOW (arg11)
10643                       && operand_equal_p (arg0, arg10, 0))
10644                     {
10645                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10646                       REAL_VALUE_TYPE c;
10647                       tree arg;
10648
10649                       c = TREE_REAL_CST (arg11);
10650                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10651                       arg = build_real (type, c);
10652                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10653                     }
10654                 }
10655
10656               /* Optimize pow(x,c)*x as pow(x,c+1).  */
10657               if (fcode0 == BUILT_IN_POW
10658                   || fcode0 == BUILT_IN_POWF
10659                   || fcode0 == BUILT_IN_POWL)
10660                 {
10661                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10662                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10663                   if (TREE_CODE (arg01) == REAL_CST
10664                       && !TREE_OVERFLOW (arg01)
10665                       && operand_equal_p (arg1, arg00, 0))
10666                     {
10667                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10668                       REAL_VALUE_TYPE c;
10669                       tree arg;
10670
10671                       c = TREE_REAL_CST (arg01);
10672                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10673                       arg = build_real (type, c);
10674                       return build_call_expr_loc (loc, powfn, 2, arg1, arg);
10675                     }
10676                 }
10677
10678               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
10679               if (!in_gimple_form
10680                   && optimize_function_for_speed_p (cfun)
10681                   && operand_equal_p (arg0, arg1, 0))
10682                 {
10683                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10684
10685                   if (powfn)
10686                     {
10687                       tree arg = build_real (type, dconst2);
10688                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10689                     }
10690                 }
10691             }
10692         }
10693       goto associate;
10694
10695     case BIT_IOR_EXPR:
10696     bit_ior:
10697       if (integer_all_onesp (arg1))
10698         return omit_one_operand_loc (loc, type, arg1, arg0);
10699       if (integer_zerop (arg1))
10700         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10701       if (operand_equal_p (arg0, arg1, 0))
10702         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10703
10704       /* ~X | X is -1.  */
10705       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10706           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10707         {
10708           t1 = build_zero_cst (type);
10709           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10710           return omit_one_operand_loc (loc, type, t1, arg1);
10711         }
10712
10713       /* X | ~X is -1.  */
10714       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10715           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10716         {
10717           t1 = build_zero_cst (type);
10718           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10719           return omit_one_operand_loc (loc, type, t1, arg0);
10720         }
10721
10722       /* Canonicalize (X & C1) | C2.  */
10723       if (TREE_CODE (arg0) == BIT_AND_EXPR
10724           && TREE_CODE (arg1) == INTEGER_CST
10725           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10726         {
10727           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10728           int width = TYPE_PRECISION (type), w;
10729           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10730           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10731           hi2 = TREE_INT_CST_HIGH (arg1);
10732           lo2 = TREE_INT_CST_LOW (arg1);
10733
10734           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
10735           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10736             return omit_one_operand_loc (loc, type, arg1,
10737                                      TREE_OPERAND (arg0, 0));
10738
10739           if (width > HOST_BITS_PER_WIDE_INT)
10740             {
10741               mhi = (unsigned HOST_WIDE_INT) -1
10742                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
10743               mlo = -1;
10744             }
10745           else
10746             {
10747               mhi = 0;
10748               mlo = (unsigned HOST_WIDE_INT) -1
10749                     >> (HOST_BITS_PER_WIDE_INT - width);
10750             }
10751
10752           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
10753           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10754             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10755                                 TREE_OPERAND (arg0, 0), arg1);
10756
10757           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10758              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10759              mode which allows further optimizations.  */
10760           hi1 &= mhi;
10761           lo1 &= mlo;
10762           hi2 &= mhi;
10763           lo2 &= mlo;
10764           hi3 = hi1 & ~hi2;
10765           lo3 = lo1 & ~lo2;
10766           for (w = BITS_PER_UNIT;
10767                w <= width && w <= HOST_BITS_PER_WIDE_INT;
10768                w <<= 1)
10769             {
10770               unsigned HOST_WIDE_INT mask
10771                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10772               if (((lo1 | lo2) & mask) == mask
10773                   && (lo1 & ~mask) == 0 && hi1 == 0)
10774                 {
10775                   hi3 = 0;
10776                   lo3 = mask;
10777                   break;
10778                 }
10779             }
10780           if (hi3 != hi1 || lo3 != lo1)
10781             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10782                                 fold_build2_loc (loc, BIT_AND_EXPR, type,
10783                                              TREE_OPERAND (arg0, 0),
10784                                              build_int_cst_wide (type,
10785                                                                  lo3, hi3)),
10786                                 arg1);
10787         }
10788
10789       /* (X & Y) | Y is (X, Y).  */
10790       if (TREE_CODE (arg0) == BIT_AND_EXPR
10791           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10792         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10793       /* (X & Y) | X is (Y, X).  */
10794       if (TREE_CODE (arg0) == BIT_AND_EXPR
10795           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10796           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10797         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
10798       /* X | (X & Y) is (Y, X).  */
10799       if (TREE_CODE (arg1) == BIT_AND_EXPR
10800           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10801           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10802         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
10803       /* X | (Y & X) is (Y, X).  */
10804       if (TREE_CODE (arg1) == BIT_AND_EXPR
10805           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10806           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10807         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
10808
10809       /* (X & ~Y) | (~X & Y) is X ^ Y */
10810       if (TREE_CODE (arg0) == BIT_AND_EXPR
10811           && TREE_CODE (arg1) == BIT_AND_EXPR)
10812         {
10813           tree a0, a1, l0, l1, n0, n1;
10814
10815           a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10816           a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10817
10818           l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10819           l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10820           
10821           n0 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l0);
10822           n1 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l1);
10823           
10824           if ((operand_equal_p (n0, a0, 0)
10825                && operand_equal_p (n1, a1, 0))
10826               || (operand_equal_p (n0, a1, 0)
10827                   && operand_equal_p (n1, a0, 0)))
10828             return fold_build2_loc (loc, BIT_XOR_EXPR, type, l0, n1);
10829         }
10830
10831       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
10832       if (t1 != NULL_TREE)
10833         return t1;
10834
10835       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10836
10837          This results in more efficient code for machines without a NAND
10838          instruction.  Combine will canonicalize to the first form
10839          which will allow use of NAND instructions provided by the
10840          backend if they exist.  */
10841       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10842           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10843         {
10844           return
10845             fold_build1_loc (loc, BIT_NOT_EXPR, type,
10846                          build2 (BIT_AND_EXPR, type,
10847                                  fold_convert_loc (loc, type,
10848                                                    TREE_OPERAND (arg0, 0)),
10849                                  fold_convert_loc (loc, type,
10850                                                    TREE_OPERAND (arg1, 0))));
10851         }
10852
10853       /* See if this can be simplified into a rotate first.  If that
10854          is unsuccessful continue in the association code.  */
10855       goto bit_rotate;
10856
10857     case BIT_XOR_EXPR:
10858       if (integer_zerop (arg1))
10859         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10860       if (integer_all_onesp (arg1))
10861         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
10862       if (operand_equal_p (arg0, arg1, 0))
10863         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10864
10865       /* ~X ^ X is -1.  */
10866       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10867           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10868         {
10869           t1 = build_zero_cst (type);
10870           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10871           return omit_one_operand_loc (loc, type, t1, arg1);
10872         }
10873
10874       /* X ^ ~X is -1.  */
10875       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10876           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10877         {
10878           t1 = build_zero_cst (type);
10879           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10880           return omit_one_operand_loc (loc, type, t1, arg0);
10881         }
10882
10883       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10884          with a constant, and the two constants have no bits in common,
10885          we should treat this as a BIT_IOR_EXPR since this may produce more
10886          simplifications.  */
10887       if (TREE_CODE (arg0) == BIT_AND_EXPR
10888           && TREE_CODE (arg1) == BIT_AND_EXPR
10889           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10890           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10891           && integer_zerop (const_binop (BIT_AND_EXPR,
10892                                          TREE_OPERAND (arg0, 1),
10893                                          TREE_OPERAND (arg1, 1))))
10894         {
10895           code = BIT_IOR_EXPR;
10896           goto bit_ior;
10897         }
10898
10899       /* (X | Y) ^ X -> Y & ~ X*/
10900       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10901           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10902         {
10903           tree t2 = TREE_OPERAND (arg0, 1);
10904           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10905                             arg1);
10906           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10907                             fold_convert_loc (loc, type, t2),
10908                             fold_convert_loc (loc, type, t1));
10909           return t1;
10910         }
10911
10912       /* (Y | X) ^ X -> Y & ~ X*/
10913       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10914           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10915         {
10916           tree t2 = TREE_OPERAND (arg0, 0);
10917           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10918                             arg1);
10919           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10920                             fold_convert_loc (loc, type, t2),
10921                             fold_convert_loc (loc, type, t1));
10922           return t1;
10923         }
10924
10925       /* X ^ (X | Y) -> Y & ~ X*/
10926       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10927           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10928         {
10929           tree t2 = TREE_OPERAND (arg1, 1);
10930           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10931                             arg0);
10932           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10933                             fold_convert_loc (loc, type, t2),
10934                             fold_convert_loc (loc, type, t1));
10935           return t1;
10936         }
10937
10938       /* X ^ (Y | X) -> Y & ~ X*/
10939       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10940           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10941         {
10942           tree t2 = TREE_OPERAND (arg1, 0);
10943           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10944                             arg0);
10945           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10946                             fold_convert_loc (loc, type, t2),
10947                             fold_convert_loc (loc, type, t1));
10948           return t1;
10949         }
10950
10951       /* Convert ~X ^ ~Y to X ^ Y.  */
10952       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10953           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10954         return fold_build2_loc (loc, code, type,
10955                             fold_convert_loc (loc, type,
10956                                               TREE_OPERAND (arg0, 0)),
10957                             fold_convert_loc (loc, type,
10958                                               TREE_OPERAND (arg1, 0)));
10959
10960       /* Convert ~X ^ C to X ^ ~C.  */
10961       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10962           && TREE_CODE (arg1) == INTEGER_CST)
10963         return fold_build2_loc (loc, code, type,
10964                             fold_convert_loc (loc, type,
10965                                               TREE_OPERAND (arg0, 0)),
10966                             fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
10967
10968       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
10969       if (TREE_CODE (arg0) == BIT_AND_EXPR
10970           && integer_onep (TREE_OPERAND (arg0, 1))
10971           && integer_onep (arg1))
10972         return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10973                             build_int_cst (TREE_TYPE (arg0), 0));
10974
10975       /* Fold (X & Y) ^ Y as ~X & Y.  */
10976       if (TREE_CODE (arg0) == BIT_AND_EXPR
10977           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10978         {
10979           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10980           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10981                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10982                               fold_convert_loc (loc, type, arg1));
10983         }
10984       /* Fold (X & Y) ^ X as ~Y & X.  */
10985       if (TREE_CODE (arg0) == BIT_AND_EXPR
10986           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10987           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10988         {
10989           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10990           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10991                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10992                               fold_convert_loc (loc, type, arg1));
10993         }
10994       /* Fold X ^ (X & Y) as X & ~Y.  */
10995       if (TREE_CODE (arg1) == BIT_AND_EXPR
10996           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10997         {
10998           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10999           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11000                               fold_convert_loc (loc, type, arg0),
11001                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11002         }
11003       /* Fold X ^ (Y & X) as ~Y & X.  */
11004       if (TREE_CODE (arg1) == BIT_AND_EXPR
11005           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11006           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11007         {
11008           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11009           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11010                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11011                               fold_convert_loc (loc, type, arg0));
11012         }
11013
11014       /* See if this can be simplified into a rotate first.  If that
11015          is unsuccessful continue in the association code.  */
11016       goto bit_rotate;
11017
11018     case BIT_AND_EXPR:
11019       if (integer_all_onesp (arg1))
11020         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11021       if (integer_zerop (arg1))
11022         return omit_one_operand_loc (loc, type, arg1, arg0);
11023       if (operand_equal_p (arg0, arg1, 0))
11024         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11025
11026       /* ~X & X, (X == 0) & X, and !X & X are always zero.  */
11027       if ((TREE_CODE (arg0) == BIT_NOT_EXPR
11028            || TREE_CODE (arg0) == TRUTH_NOT_EXPR
11029            || (TREE_CODE (arg0) == EQ_EXPR
11030                && integer_zerop (TREE_OPERAND (arg0, 1))))
11031           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11032         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11033
11034       /* X & ~X , X & (X == 0), and X & !X are always zero.  */
11035       if ((TREE_CODE (arg1) == BIT_NOT_EXPR
11036            || TREE_CODE (arg1) == TRUTH_NOT_EXPR
11037            || (TREE_CODE (arg1) == EQ_EXPR
11038                && integer_zerop (TREE_OPERAND (arg1, 1))))
11039           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11040         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11041
11042       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
11043       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11044           && TREE_CODE (arg1) == INTEGER_CST
11045           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11046         {
11047           tree tmp1 = fold_convert_loc (loc, type, arg1);
11048           tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11049           tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11050           tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
11051           tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
11052           return
11053             fold_convert_loc (loc, type,
11054                               fold_build2_loc (loc, BIT_IOR_EXPR,
11055                                            type, tmp2, tmp3));
11056         }
11057
11058       /* (X | Y) & Y is (X, Y).  */
11059       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11060           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11061         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11062       /* (X | Y) & X is (Y, X).  */
11063       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11064           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11065           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11066         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11067       /* X & (X | Y) is (Y, X).  */
11068       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11069           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11070           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11071         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11072       /* X & (Y | X) is (Y, X).  */
11073       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11074           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11075           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11076         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11077
11078       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
11079       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11080           && integer_onep (TREE_OPERAND (arg0, 1))
11081           && integer_onep (arg1))
11082         {
11083           tem = TREE_OPERAND (arg0, 0);
11084           return fold_build2_loc (loc, EQ_EXPR, type,
11085                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11086                                            build_int_cst (TREE_TYPE (tem), 1)),
11087                               build_int_cst (TREE_TYPE (tem), 0));
11088         }
11089       /* Fold ~X & 1 as (X & 1) == 0.  */
11090       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11091           && integer_onep (arg1))
11092         {
11093           tem = TREE_OPERAND (arg0, 0);
11094           return fold_build2_loc (loc, EQ_EXPR, type,
11095                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11096                                            build_int_cst (TREE_TYPE (tem), 1)),
11097                               build_int_cst (TREE_TYPE (tem), 0));
11098         }
11099       /* Fold !X & 1 as X == 0.  */
11100       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11101           && integer_onep (arg1))
11102         {
11103           tem = TREE_OPERAND (arg0, 0);
11104           return fold_build2_loc (loc, EQ_EXPR, type, tem,
11105                                   build_int_cst (TREE_TYPE (tem), 0));
11106         }
11107
11108       /* Fold (X ^ Y) & Y as ~X & Y.  */
11109       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11110           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11111         {
11112           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11113           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11114                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11115                               fold_convert_loc (loc, type, arg1));
11116         }
11117       /* Fold (X ^ Y) & X as ~Y & X.  */
11118       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11119           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11120           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11121         {
11122           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11123           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11124                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11125                               fold_convert_loc (loc, type, arg1));
11126         }
11127       /* Fold X & (X ^ Y) as X & ~Y.  */
11128       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11129           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11130         {
11131           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11132           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11133                               fold_convert_loc (loc, type, arg0),
11134                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11135         }
11136       /* Fold X & (Y ^ X) as ~Y & X.  */
11137       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11138           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11139           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11140         {
11141           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11142           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11143                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11144                               fold_convert_loc (loc, type, arg0));
11145         }
11146
11147       /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11148          ((A & N) + B) & M -> (A + B) & M
11149          Similarly if (N & M) == 0,
11150          ((A | N) + B) & M -> (A + B) & M
11151          and for - instead of + (or unary - instead of +)
11152          and/or ^ instead of |.
11153          If B is constant and (B & M) == 0, fold into A & M.  */
11154       if (host_integerp (arg1, 1))
11155         {
11156           unsigned HOST_WIDE_INT cst1 = tree_low_cst (arg1, 1);
11157           if (~cst1 && (cst1 & (cst1 + 1)) == 0
11158               && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11159               && (TREE_CODE (arg0) == PLUS_EXPR
11160                   || TREE_CODE (arg0) == MINUS_EXPR
11161                   || TREE_CODE (arg0) == NEGATE_EXPR)
11162               && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
11163                   || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
11164             {
11165               tree pmop[2];
11166               int which = 0;
11167               unsigned HOST_WIDE_INT cst0;
11168
11169               /* Now we know that arg0 is (C + D) or (C - D) or
11170                  -C and arg1 (M) is == (1LL << cst) - 1.
11171                  Store C into PMOP[0] and D into PMOP[1].  */
11172               pmop[0] = TREE_OPERAND (arg0, 0);
11173               pmop[1] = NULL;
11174               if (TREE_CODE (arg0) != NEGATE_EXPR)
11175                 {
11176                   pmop[1] = TREE_OPERAND (arg0, 1);
11177                   which = 1;
11178                 }
11179
11180               if (!host_integerp (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11181                   || (tree_low_cst (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11182                       & cst1) != cst1)
11183                 which = -1;
11184
11185               for (; which >= 0; which--)
11186                 switch (TREE_CODE (pmop[which]))
11187                   {
11188                   case BIT_AND_EXPR:
11189                   case BIT_IOR_EXPR:
11190                   case BIT_XOR_EXPR:
11191                     if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
11192                         != INTEGER_CST)
11193                       break;
11194                     /* tree_low_cst not used, because we don't care about
11195                        the upper bits.  */
11196                     cst0 = TREE_INT_CST_LOW (TREE_OPERAND (pmop[which], 1));
11197                     cst0 &= cst1;
11198                     if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
11199                       {
11200                         if (cst0 != cst1)
11201                           break;
11202                       }
11203                     else if (cst0 != 0)
11204                       break;
11205                     /* If C or D is of the form (A & N) where
11206                        (N & M) == M, or of the form (A | N) or
11207                        (A ^ N) where (N & M) == 0, replace it with A.  */
11208                     pmop[which] = TREE_OPERAND (pmop[which], 0);
11209                     break;
11210                   case INTEGER_CST:
11211                     /* If C or D is a N where (N & M) == 0, it can be
11212                        omitted (assumed 0).  */
11213                     if ((TREE_CODE (arg0) == PLUS_EXPR
11214                          || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
11215                         && (TREE_INT_CST_LOW (pmop[which]) & cst1) == 0)
11216                       pmop[which] = NULL;
11217                     break;
11218                   default:
11219                     break;
11220                   }
11221
11222               /* Only build anything new if we optimized one or both arguments
11223                  above.  */
11224               if (pmop[0] != TREE_OPERAND (arg0, 0)
11225                   || (TREE_CODE (arg0) != NEGATE_EXPR
11226                       && pmop[1] != TREE_OPERAND (arg0, 1)))
11227                 {
11228                   tree utype = TREE_TYPE (arg0);
11229                   if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11230                     {
11231                       /* Perform the operations in a type that has defined
11232                          overflow behavior.  */
11233                       utype = unsigned_type_for (TREE_TYPE (arg0));
11234                       if (pmop[0] != NULL)
11235                         pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
11236                       if (pmop[1] != NULL)
11237                         pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
11238                     }
11239
11240                   if (TREE_CODE (arg0) == NEGATE_EXPR)
11241                     tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
11242                   else if (TREE_CODE (arg0) == PLUS_EXPR)
11243                     {
11244                       if (pmop[0] != NULL && pmop[1] != NULL)
11245                         tem = fold_build2_loc (loc, PLUS_EXPR, utype,
11246                                                pmop[0], pmop[1]);
11247                       else if (pmop[0] != NULL)
11248                         tem = pmop[0];
11249                       else if (pmop[1] != NULL)
11250                         tem = pmop[1];
11251                       else
11252                         return build_int_cst (type, 0);
11253                     }
11254                   else if (pmop[0] == NULL)
11255                     tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
11256                   else
11257                     tem = fold_build2_loc (loc, MINUS_EXPR, utype,
11258                                            pmop[0], pmop[1]);
11259                   /* TEM is now the new binary +, - or unary - replacement.  */
11260                   tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
11261                                          fold_convert_loc (loc, utype, arg1));
11262                   return fold_convert_loc (loc, type, tem);
11263                 }
11264             }
11265         }
11266
11267       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11268       if (t1 != NULL_TREE)
11269         return t1;
11270       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
11271       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11272           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11273         {
11274           unsigned int prec
11275             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11276
11277           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11278               && (~TREE_INT_CST_LOW (arg1)
11279                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11280             return
11281               fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11282         }
11283
11284       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11285
11286          This results in more efficient code for machines without a NOR
11287          instruction.  Combine will canonicalize to the first form
11288          which will allow use of NOR instructions provided by the
11289          backend if they exist.  */
11290       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11291           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11292         {
11293           return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11294                               build2 (BIT_IOR_EXPR, type,
11295                                       fold_convert_loc (loc, type,
11296                                                         TREE_OPERAND (arg0, 0)),
11297                                       fold_convert_loc (loc, type,
11298                                                         TREE_OPERAND (arg1, 0))));
11299         }
11300
11301       /* If arg0 is derived from the address of an object or function, we may
11302          be able to fold this expression using the object or function's
11303          alignment.  */
11304       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11305         {
11306           unsigned HOST_WIDE_INT modulus, residue;
11307           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11308
11309           modulus = get_pointer_modulus_and_residue (arg0, &residue,
11310                                                      integer_onep (arg1));
11311
11312           /* This works because modulus is a power of 2.  If this weren't the
11313              case, we'd have to replace it by its greatest power-of-2
11314              divisor: modulus & -modulus.  */
11315           if (low < modulus)
11316             return build_int_cst (type, residue & low);
11317         }
11318
11319       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11320               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11321          if the new mask might be further optimized.  */
11322       if ((TREE_CODE (arg0) == LSHIFT_EXPR
11323            || TREE_CODE (arg0) == RSHIFT_EXPR)
11324           && host_integerp (TREE_OPERAND (arg0, 1), 1)
11325           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11326           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11327              < TYPE_PRECISION (TREE_TYPE (arg0))
11328           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11329           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11330         {
11331           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11332           unsigned HOST_WIDE_INT mask
11333             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11334           unsigned HOST_WIDE_INT newmask, zerobits = 0;
11335           tree shift_type = TREE_TYPE (arg0);
11336
11337           if (TREE_CODE (arg0) == LSHIFT_EXPR)
11338             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11339           else if (TREE_CODE (arg0) == RSHIFT_EXPR
11340                    && TYPE_PRECISION (TREE_TYPE (arg0))
11341                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11342             {
11343               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11344               tree arg00 = TREE_OPERAND (arg0, 0);
11345               /* See if more bits can be proven as zero because of
11346                  zero extension.  */
11347               if (TREE_CODE (arg00) == NOP_EXPR
11348                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11349                 {
11350                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11351                   if (TYPE_PRECISION (inner_type)
11352                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11353                       && TYPE_PRECISION (inner_type) < prec)
11354                     {
11355                       prec = TYPE_PRECISION (inner_type);
11356                       /* See if we can shorten the right shift.  */
11357                       if (shiftc < prec)
11358                         shift_type = inner_type;
11359                     }
11360                 }
11361               zerobits = ~(unsigned HOST_WIDE_INT) 0;
11362               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11363               zerobits <<= prec - shiftc;
11364               /* For arithmetic shift if sign bit could be set, zerobits
11365                  can contain actually sign bits, so no transformation is
11366                  possible, unless MASK masks them all away.  In that
11367                  case the shift needs to be converted into logical shift.  */
11368               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11369                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11370                 {
11371                   if ((mask & zerobits) == 0)
11372                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
11373                   else
11374                     zerobits = 0;
11375                 }
11376             }
11377
11378           /* ((X << 16) & 0xff00) is (X, 0).  */
11379           if ((mask & zerobits) == mask)
11380             return omit_one_operand_loc (loc, type,
11381                                      build_int_cst (type, 0), arg0);
11382
11383           newmask = mask | zerobits;
11384           if (newmask != mask && (newmask & (newmask + 1)) == 0)
11385             {
11386               unsigned int prec;
11387
11388               /* Only do the transformation if NEWMASK is some integer
11389                  mode's mask.  */
11390               for (prec = BITS_PER_UNIT;
11391                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11392                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11393                   break;
11394               if (prec < HOST_BITS_PER_WIDE_INT
11395                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
11396                 {
11397                   tree newmaskt;
11398
11399                   if (shift_type != TREE_TYPE (arg0))
11400                     {
11401                       tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11402                                          fold_convert_loc (loc, shift_type,
11403                                                            TREE_OPERAND (arg0, 0)),
11404                                          TREE_OPERAND (arg0, 1));
11405                       tem = fold_convert_loc (loc, type, tem);
11406                     }
11407                   else
11408                     tem = op0;
11409                   newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11410                   if (!tree_int_cst_equal (newmaskt, arg1))
11411                     return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11412                 }
11413             }
11414         }
11415
11416       goto associate;
11417
11418     case RDIV_EXPR:
11419       /* Don't touch a floating-point divide by zero unless the mode
11420          of the constant can represent infinity.  */
11421       if (TREE_CODE (arg1) == REAL_CST
11422           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11423           && real_zerop (arg1))
11424         return NULL_TREE;
11425
11426       /* Optimize A / A to 1.0 if we don't care about
11427          NaNs or Infinities.  Skip the transformation
11428          for non-real operands.  */
11429       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11430           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11431           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11432           && operand_equal_p (arg0, arg1, 0))
11433         {
11434           tree r = build_real (TREE_TYPE (arg0), dconst1);
11435
11436           return omit_two_operands_loc (loc, type, r, arg0, arg1);
11437         }
11438
11439       /* The complex version of the above A / A optimization.  */
11440       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11441           && operand_equal_p (arg0, arg1, 0))
11442         {
11443           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11444           if (! HONOR_NANS (TYPE_MODE (elem_type))
11445               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11446             {
11447               tree r = build_real (elem_type, dconst1);
11448               /* omit_two_operands will call fold_convert for us.  */
11449               return omit_two_operands_loc (loc, type, r, arg0, arg1);
11450             }
11451         }
11452
11453       /* (-A) / (-B) -> A / B  */
11454       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11455         return fold_build2_loc (loc, RDIV_EXPR, type,
11456                             TREE_OPERAND (arg0, 0),
11457                             negate_expr (arg1));
11458       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11459         return fold_build2_loc (loc, RDIV_EXPR, type,
11460                             negate_expr (arg0),
11461                             TREE_OPERAND (arg1, 0));
11462
11463       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
11464       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11465           && real_onep (arg1))
11466         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11467
11468       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
11469       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11470           && real_minus_onep (arg1))
11471         return non_lvalue_loc (loc, fold_convert_loc (loc, type,
11472                                                   negate_expr (arg0)));
11473
11474       /* If ARG1 is a constant, we can convert this to a multiply by the
11475          reciprocal.  This does not have the same rounding properties,
11476          so only do this if -freciprocal-math.  We can actually
11477          always safely do it if ARG1 is a power of two, but it's hard to
11478          tell if it is or not in a portable manner.  */
11479       if (TREE_CODE (arg1) == REAL_CST)
11480         {
11481           if (flag_reciprocal_math
11482               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11483                                           arg1)))
11484             return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
11485           /* Find the reciprocal if optimizing and the result is exact.  */
11486           if (optimize)
11487             {
11488               REAL_VALUE_TYPE r;
11489               r = TREE_REAL_CST (arg1);
11490               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11491                 {
11492                   tem = build_real (type, r);
11493                   return fold_build2_loc (loc, MULT_EXPR, type,
11494                                       fold_convert_loc (loc, type, arg0), tem);
11495                 }
11496             }
11497         }
11498       /* Convert A/B/C to A/(B*C).  */
11499       if (flag_reciprocal_math
11500           && TREE_CODE (arg0) == RDIV_EXPR)
11501         return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11502                             fold_build2_loc (loc, MULT_EXPR, type,
11503                                          TREE_OPERAND (arg0, 1), arg1));
11504
11505       /* Convert A/(B/C) to (A/B)*C.  */
11506       if (flag_reciprocal_math
11507           && TREE_CODE (arg1) == RDIV_EXPR)
11508         return fold_build2_loc (loc, MULT_EXPR, type,
11509                             fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11510                                          TREE_OPERAND (arg1, 0)),
11511                             TREE_OPERAND (arg1, 1));
11512
11513       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11514       if (flag_reciprocal_math
11515           && TREE_CODE (arg1) == MULT_EXPR
11516           && TREE_CODE (arg0) == REAL_CST
11517           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11518         {
11519           tree tem = const_binop (RDIV_EXPR, arg0,
11520                                   TREE_OPERAND (arg1, 1));
11521           if (tem)
11522             return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11523                                 TREE_OPERAND (arg1, 0));
11524         }
11525
11526       if (flag_unsafe_math_optimizations)
11527         {
11528           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11529           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11530
11531           /* Optimize sin(x)/cos(x) as tan(x).  */
11532           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11533                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11534                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11535               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11536                                   CALL_EXPR_ARG (arg1, 0), 0))
11537             {
11538               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11539
11540               if (tanfn != NULL_TREE)
11541                 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11542             }
11543
11544           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11545           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11546                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11547                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11548               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11549                                   CALL_EXPR_ARG (arg1, 0), 0))
11550             {
11551               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11552
11553               if (tanfn != NULL_TREE)
11554                 {
11555                   tree tmp = build_call_expr_loc (loc, tanfn, 1,
11556                                               CALL_EXPR_ARG (arg0, 0));
11557                   return fold_build2_loc (loc, RDIV_EXPR, type,
11558                                       build_real (type, dconst1), tmp);
11559                 }
11560             }
11561
11562           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11563              NaNs or Infinities.  */
11564           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11565                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11566                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11567             {
11568               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11569               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11570
11571               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11572                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11573                   && operand_equal_p (arg00, arg01, 0))
11574                 {
11575                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11576
11577                   if (cosfn != NULL_TREE)
11578                     return build_call_expr_loc (loc, cosfn, 1, arg00);
11579                 }
11580             }
11581
11582           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11583              NaNs or Infinities.  */
11584           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11585                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11586                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11587             {
11588               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11589               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11590
11591               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11592                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11593                   && operand_equal_p (arg00, arg01, 0))
11594                 {
11595                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11596
11597                   if (cosfn != NULL_TREE)
11598                     {
11599                       tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11600                       return fold_build2_loc (loc, RDIV_EXPR, type,
11601                                           build_real (type, dconst1),
11602                                           tmp);
11603                     }
11604                 }
11605             }
11606
11607           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11608           if (fcode0 == BUILT_IN_POW
11609               || fcode0 == BUILT_IN_POWF
11610               || fcode0 == BUILT_IN_POWL)
11611             {
11612               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11613               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11614               if (TREE_CODE (arg01) == REAL_CST
11615                   && !TREE_OVERFLOW (arg01)
11616                   && operand_equal_p (arg1, arg00, 0))
11617                 {
11618                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11619                   REAL_VALUE_TYPE c;
11620                   tree arg;
11621
11622                   c = TREE_REAL_CST (arg01);
11623                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11624                   arg = build_real (type, c);
11625                   return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11626                 }
11627             }
11628
11629           /* Optimize a/root(b/c) into a*root(c/b).  */
11630           if (BUILTIN_ROOT_P (fcode1))
11631             {
11632               tree rootarg = CALL_EXPR_ARG (arg1, 0);
11633
11634               if (TREE_CODE (rootarg) == RDIV_EXPR)
11635                 {
11636                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11637                   tree b = TREE_OPERAND (rootarg, 0);
11638                   tree c = TREE_OPERAND (rootarg, 1);
11639
11640                   tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
11641
11642                   tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
11643                   return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
11644                 }
11645             }
11646
11647           /* Optimize x/expN(y) into x*expN(-y).  */
11648           if (BUILTIN_EXPONENT_P (fcode1))
11649             {
11650               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11651               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11652               arg1 = build_call_expr_loc (loc,
11653                                       expfn, 1,
11654                                       fold_convert_loc (loc, type, arg));
11655               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11656             }
11657
11658           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
11659           if (fcode1 == BUILT_IN_POW
11660               || fcode1 == BUILT_IN_POWF
11661               || fcode1 == BUILT_IN_POWL)
11662             {
11663               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11664               tree arg10 = CALL_EXPR_ARG (arg1, 0);
11665               tree arg11 = CALL_EXPR_ARG (arg1, 1);
11666               tree neg11 = fold_convert_loc (loc, type,
11667                                              negate_expr (arg11));
11668               arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
11669               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11670             }
11671         }
11672       return NULL_TREE;
11673
11674     case TRUNC_DIV_EXPR:
11675       /* Optimize (X & (-A)) / A where A is a power of 2,
11676          to X >> log2(A) */
11677       if (TREE_CODE (arg0) == BIT_AND_EXPR
11678           && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST
11679           && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0)
11680         {
11681           tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1),
11682                                       arg1, TREE_OPERAND (arg0, 1));
11683           if (sum && integer_zerop (sum)) {
11684             unsigned long pow2;
11685
11686             if (TREE_INT_CST_LOW (arg1))
11687               pow2 = exact_log2 (TREE_INT_CST_LOW (arg1));
11688             else
11689               pow2 = exact_log2 (TREE_INT_CST_HIGH (arg1))
11690                       + HOST_BITS_PER_WIDE_INT;
11691
11692             return fold_build2_loc (loc, RSHIFT_EXPR, type,
11693                           TREE_OPERAND (arg0, 0),
11694                           build_int_cst (integer_type_node, pow2));
11695           }
11696         }
11697
11698       /* Fall thru */
11699       
11700     case FLOOR_DIV_EXPR:
11701       /* Simplify A / (B << N) where A and B are positive and B is
11702          a power of 2, to A >> (N + log2(B)).  */
11703       strict_overflow_p = false;
11704       if (TREE_CODE (arg1) == LSHIFT_EXPR
11705           && (TYPE_UNSIGNED (type)
11706               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11707         {
11708           tree sval = TREE_OPERAND (arg1, 0);
11709           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11710             {
11711               tree sh_cnt = TREE_OPERAND (arg1, 1);
11712               unsigned long pow2;
11713
11714               if (TREE_INT_CST_LOW (sval))
11715                 pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11716               else
11717                 pow2 = exact_log2 (TREE_INT_CST_HIGH (sval))
11718                        + HOST_BITS_PER_WIDE_INT;
11719
11720               if (strict_overflow_p)
11721                 fold_overflow_warning (("assuming signed overflow does not "
11722                                         "occur when simplifying A / (B << N)"),
11723                                        WARN_STRICT_OVERFLOW_MISC);
11724
11725               sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11726                                         sh_cnt,
11727                                         build_int_cst (TREE_TYPE (sh_cnt),
11728                                                        pow2));
11729               return fold_build2_loc (loc, RSHIFT_EXPR, type,
11730                                   fold_convert_loc (loc, type, arg0), sh_cnt);
11731             }
11732         }
11733
11734       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11735          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
11736       if (INTEGRAL_TYPE_P (type)
11737           && TYPE_UNSIGNED (type)
11738           && code == FLOOR_DIV_EXPR)
11739         return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
11740
11741       /* Fall thru */
11742
11743     case ROUND_DIV_EXPR:
11744     case CEIL_DIV_EXPR:
11745     case EXACT_DIV_EXPR:
11746       if (integer_onep (arg1))
11747         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11748       if (integer_zerop (arg1))
11749         return NULL_TREE;
11750       /* X / -1 is -X.  */
11751       if (!TYPE_UNSIGNED (type)
11752           && TREE_CODE (arg1) == INTEGER_CST
11753           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11754           && TREE_INT_CST_HIGH (arg1) == -1)
11755         return fold_convert_loc (loc, type, negate_expr (arg0));
11756
11757       /* Convert -A / -B to A / B when the type is signed and overflow is
11758          undefined.  */
11759       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11760           && TREE_CODE (arg0) == NEGATE_EXPR
11761           && negate_expr_p (arg1))
11762         {
11763           if (INTEGRAL_TYPE_P (type))
11764             fold_overflow_warning (("assuming signed overflow does not occur "
11765                                     "when distributing negation across "
11766                                     "division"),
11767                                    WARN_STRICT_OVERFLOW_MISC);
11768           return fold_build2_loc (loc, code, type,
11769                               fold_convert_loc (loc, type,
11770                                                 TREE_OPERAND (arg0, 0)),
11771                               fold_convert_loc (loc, type,
11772                                                 negate_expr (arg1)));
11773         }
11774       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11775           && TREE_CODE (arg1) == NEGATE_EXPR
11776           && negate_expr_p (arg0))
11777         {
11778           if (INTEGRAL_TYPE_P (type))
11779             fold_overflow_warning (("assuming signed overflow does not occur "
11780                                     "when distributing negation across "
11781                                     "division"),
11782                                    WARN_STRICT_OVERFLOW_MISC);
11783           return fold_build2_loc (loc, code, type,
11784                               fold_convert_loc (loc, type,
11785                                                 negate_expr (arg0)),
11786                               fold_convert_loc (loc, type,
11787                                                 TREE_OPERAND (arg1, 0)));
11788         }
11789
11790       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11791          operation, EXACT_DIV_EXPR.
11792
11793          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11794          At one time others generated faster code, it's not clear if they do
11795          after the last round to changes to the DIV code in expmed.c.  */
11796       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11797           && multiple_of_p (type, arg0, arg1))
11798         return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
11799
11800       strict_overflow_p = false;
11801       if (TREE_CODE (arg1) == INTEGER_CST
11802           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11803                                          &strict_overflow_p)))
11804         {
11805           if (strict_overflow_p)
11806             fold_overflow_warning (("assuming signed overflow does not occur "
11807                                     "when simplifying division"),
11808                                    WARN_STRICT_OVERFLOW_MISC);
11809           return fold_convert_loc (loc, type, tem);
11810         }
11811
11812       return NULL_TREE;
11813
11814     case CEIL_MOD_EXPR:
11815     case FLOOR_MOD_EXPR:
11816     case ROUND_MOD_EXPR:
11817     case TRUNC_MOD_EXPR:
11818       /* X % 1 is always zero, but be sure to preserve any side
11819          effects in X.  */
11820       if (integer_onep (arg1))
11821         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11822
11823       /* X % 0, return X % 0 unchanged so that we can get the
11824          proper warnings and errors.  */
11825       if (integer_zerop (arg1))
11826         return NULL_TREE;
11827
11828       /* 0 % X is always zero, but be sure to preserve any side
11829          effects in X.  Place this after checking for X == 0.  */
11830       if (integer_zerop (arg0))
11831         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11832
11833       /* X % -1 is zero.  */
11834       if (!TYPE_UNSIGNED (type)
11835           && TREE_CODE (arg1) == INTEGER_CST
11836           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11837           && TREE_INT_CST_HIGH (arg1) == -1)
11838         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11839
11840       /* X % -C is the same as X % C.  */
11841       if (code == TRUNC_MOD_EXPR
11842           && !TYPE_UNSIGNED (type)
11843           && TREE_CODE (arg1) == INTEGER_CST
11844           && !TREE_OVERFLOW (arg1)
11845           && TREE_INT_CST_HIGH (arg1) < 0
11846           && !TYPE_OVERFLOW_TRAPS (type)
11847           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
11848           && !sign_bit_p (arg1, arg1))
11849         return fold_build2_loc (loc, code, type,
11850                             fold_convert_loc (loc, type, arg0),
11851                             fold_convert_loc (loc, type,
11852                                               negate_expr (arg1)));
11853
11854       /* X % -Y is the same as X % Y.  */
11855       if (code == TRUNC_MOD_EXPR
11856           && !TYPE_UNSIGNED (type)
11857           && TREE_CODE (arg1) == NEGATE_EXPR
11858           && !TYPE_OVERFLOW_TRAPS (type))
11859         return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
11860                             fold_convert_loc (loc, type,
11861                                               TREE_OPERAND (arg1, 0)));
11862
11863       strict_overflow_p = false;
11864       if (TREE_CODE (arg1) == INTEGER_CST
11865           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11866                                          &strict_overflow_p)))
11867         {
11868           if (strict_overflow_p)
11869             fold_overflow_warning (("assuming signed overflow does not occur "
11870                                     "when simplifying modulus"),
11871                                    WARN_STRICT_OVERFLOW_MISC);
11872           return fold_convert_loc (loc, type, tem);
11873         }
11874
11875       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11876          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
11877       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11878           && (TYPE_UNSIGNED (type)
11879               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11880         {
11881           tree c = arg1;
11882           /* Also optimize A % (C << N)  where C is a power of 2,
11883              to A & ((C << N) - 1).  */
11884           if (TREE_CODE (arg1) == LSHIFT_EXPR)
11885             c = TREE_OPERAND (arg1, 0);
11886
11887           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11888             {
11889               tree mask
11890                 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
11891                                    build_int_cst (TREE_TYPE (arg1), 1));
11892               if (strict_overflow_p)
11893                 fold_overflow_warning (("assuming signed overflow does not "
11894                                         "occur when simplifying "
11895                                         "X % (power of two)"),
11896                                        WARN_STRICT_OVERFLOW_MISC);
11897               return fold_build2_loc (loc, BIT_AND_EXPR, type,
11898                                       fold_convert_loc (loc, type, arg0),
11899                                       fold_convert_loc (loc, type, mask));
11900             }
11901         }
11902
11903       return NULL_TREE;
11904
11905     case LROTATE_EXPR:
11906     case RROTATE_EXPR:
11907       if (integer_all_onesp (arg0))
11908         return omit_one_operand_loc (loc, type, arg0, arg1);
11909       goto shift;
11910
11911     case RSHIFT_EXPR:
11912       /* Optimize -1 >> x for arithmetic right shifts.  */
11913       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11914           && tree_expr_nonnegative_p (arg1))
11915         return omit_one_operand_loc (loc, type, arg0, arg1);
11916       /* ... fall through ...  */
11917
11918     case LSHIFT_EXPR:
11919     shift:
11920       if (integer_zerop (arg1))
11921         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11922       if (integer_zerop (arg0))
11923         return omit_one_operand_loc (loc, type, arg0, arg1);
11924
11925       /* Since negative shift count is not well-defined,
11926          don't try to compute it in the compiler.  */
11927       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11928         return NULL_TREE;
11929
11930       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
11931       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11932           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11933           && host_integerp (TREE_OPERAND (arg0, 1), false)
11934           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11935         {
11936           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11937                                + TREE_INT_CST_LOW (arg1));
11938
11939           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11940              being well defined.  */
11941           if (low >= TYPE_PRECISION (type))
11942             {
11943               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11944                 low = low % TYPE_PRECISION (type);
11945               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11946                 return omit_one_operand_loc (loc, type, build_int_cst (type, 0),
11947                                          TREE_OPERAND (arg0, 0));
11948               else
11949                 low = TYPE_PRECISION (type) - 1;
11950             }
11951
11952           return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11953                               build_int_cst (type, low));
11954         }
11955
11956       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11957          into x & ((unsigned)-1 >> c) for unsigned types.  */
11958       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11959            || (TYPE_UNSIGNED (type)
11960                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11961           && host_integerp (arg1, false)
11962           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11963           && host_integerp (TREE_OPERAND (arg0, 1), false)
11964           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11965         {
11966           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11967           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11968           tree lshift;
11969           tree arg00;
11970
11971           if (low0 == low1)
11972             {
11973               arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11974
11975               lshift = build_int_cst (type, -1);
11976               lshift = int_const_binop (code, lshift, arg1);
11977
11978               return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
11979             }
11980         }
11981
11982       /* Rewrite an LROTATE_EXPR by a constant into an
11983          RROTATE_EXPR by a new constant.  */
11984       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11985         {
11986           tree tem = build_int_cst (TREE_TYPE (arg1),
11987                                     TYPE_PRECISION (type));
11988           tem = const_binop (MINUS_EXPR, tem, arg1);
11989           return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
11990         }
11991
11992       /* If we have a rotate of a bit operation with the rotate count and
11993          the second operand of the bit operation both constant,
11994          permute the two operations.  */
11995       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11996           && (TREE_CODE (arg0) == BIT_AND_EXPR
11997               || TREE_CODE (arg0) == BIT_IOR_EXPR
11998               || TREE_CODE (arg0) == BIT_XOR_EXPR)
11999           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12000         return fold_build2_loc (loc, TREE_CODE (arg0), type,
12001                             fold_build2_loc (loc, code, type,
12002                                          TREE_OPERAND (arg0, 0), arg1),
12003                             fold_build2_loc (loc, code, type,
12004                                          TREE_OPERAND (arg0, 1), arg1));
12005
12006       /* Two consecutive rotates adding up to the precision of the
12007          type can be ignored.  */
12008       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12009           && TREE_CODE (arg0) == RROTATE_EXPR
12010           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12011           && TREE_INT_CST_HIGH (arg1) == 0
12012           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
12013           && ((TREE_INT_CST_LOW (arg1)
12014                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
12015               == (unsigned int) TYPE_PRECISION (type)))
12016         return TREE_OPERAND (arg0, 0);
12017
12018       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
12019               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
12020          if the latter can be further optimized.  */
12021       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
12022           && TREE_CODE (arg0) == BIT_AND_EXPR
12023           && TREE_CODE (arg1) == INTEGER_CST
12024           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12025         {
12026           tree mask = fold_build2_loc (loc, code, type,
12027                                    fold_convert_loc (loc, type,
12028                                                      TREE_OPERAND (arg0, 1)),
12029                                    arg1);
12030           tree shift = fold_build2_loc (loc, code, type,
12031                                     fold_convert_loc (loc, type,
12032                                                       TREE_OPERAND (arg0, 0)),
12033                                     arg1);
12034           tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
12035           if (tem)
12036             return tem;
12037         }
12038
12039       return NULL_TREE;
12040
12041     case MIN_EXPR:
12042       if (operand_equal_p (arg0, arg1, 0))
12043         return omit_one_operand_loc (loc, type, arg0, arg1);
12044       if (INTEGRAL_TYPE_P (type)
12045           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
12046         return omit_one_operand_loc (loc, type, arg1, arg0);
12047       tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
12048       if (tem)
12049         return tem;
12050       goto associate;
12051
12052     case MAX_EXPR:
12053       if (operand_equal_p (arg0, arg1, 0))
12054         return omit_one_operand_loc (loc, type, arg0, arg1);
12055       if (INTEGRAL_TYPE_P (type)
12056           && TYPE_MAX_VALUE (type)
12057           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
12058         return omit_one_operand_loc (loc, type, arg1, arg0);
12059       tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
12060       if (tem)
12061         return tem;
12062       goto associate;
12063
12064     case TRUTH_ANDIF_EXPR:
12065       /* Note that the operands of this must be ints
12066          and their values must be 0 or 1.
12067          ("true" is a fixed value perhaps depending on the language.)  */
12068       /* If first arg is constant zero, return it.  */
12069       if (integer_zerop (arg0))
12070         return fold_convert_loc (loc, type, arg0);
12071     case TRUTH_AND_EXPR:
12072       /* If either arg is constant true, drop it.  */
12073       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12074         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12075       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12076           /* Preserve sequence points.  */
12077           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12078         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12079       /* If second arg is constant zero, result is zero, but first arg
12080          must be evaluated.  */
12081       if (integer_zerop (arg1))
12082         return omit_one_operand_loc (loc, type, arg1, arg0);
12083       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12084          case will be handled here.  */
12085       if (integer_zerop (arg0))
12086         return omit_one_operand_loc (loc, type, arg0, arg1);
12087
12088       /* !X && X is always false.  */
12089       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12090           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12091         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12092       /* X && !X is always false.  */
12093       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12094           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12095         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12096
12097       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
12098          means A >= Y && A != MAX, but in this case we know that
12099          A < X <= MAX.  */
12100
12101       if (!TREE_SIDE_EFFECTS (arg0)
12102           && !TREE_SIDE_EFFECTS (arg1))
12103         {
12104           tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12105           if (tem && !operand_equal_p (tem, arg0, 0))
12106             return fold_build2_loc (loc, code, type, tem, arg1);
12107
12108           tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12109           if (tem && !operand_equal_p (tem, arg1, 0))
12110             return fold_build2_loc (loc, code, type, arg0, tem);
12111         }
12112
12113       if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12114           != NULL_TREE)
12115         return tem;
12116
12117       return NULL_TREE;
12118
12119     case TRUTH_ORIF_EXPR:
12120       /* Note that the operands of this must be ints
12121          and their values must be 0 or true.
12122          ("true" is a fixed value perhaps depending on the language.)  */
12123       /* If first arg is constant true, return it.  */
12124       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12125         return fold_convert_loc (loc, type, arg0);
12126     case TRUTH_OR_EXPR:
12127       /* If either arg is constant zero, drop it.  */
12128       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12129         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12130       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12131           /* Preserve sequence points.  */
12132           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12133         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12134       /* If second arg is constant true, result is true, but we must
12135          evaluate first arg.  */
12136       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12137         return omit_one_operand_loc (loc, type, arg1, arg0);
12138       /* Likewise for first arg, but note this only occurs here for
12139          TRUTH_OR_EXPR.  */
12140       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12141         return omit_one_operand_loc (loc, type, arg0, arg1);
12142
12143       /* !X || X is always true.  */
12144       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12145           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12146         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12147       /* X || !X is always true.  */
12148       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12149           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12150         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12151
12152       /* (X && !Y) || (!X && Y) is X ^ Y */
12153       if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12154           && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12155         {
12156           tree a0, a1, l0, l1, n0, n1;
12157
12158           a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12159           a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12160
12161           l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12162           l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12163           
12164           n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12165           n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12166           
12167           if ((operand_equal_p (n0, a0, 0)
12168                && operand_equal_p (n1, a1, 0))
12169               || (operand_equal_p (n0, a1, 0)
12170                   && operand_equal_p (n1, a0, 0)))
12171             return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12172         }
12173
12174       if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12175           != NULL_TREE)
12176         return tem;
12177
12178       return NULL_TREE;
12179
12180     case TRUTH_XOR_EXPR:
12181       /* If the second arg is constant zero, drop it.  */
12182       if (integer_zerop (arg1))
12183         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12184       /* If the second arg is constant true, this is a logical inversion.  */
12185       if (integer_onep (arg1))
12186         {
12187           /* Only call invert_truthvalue if operand is a truth value.  */
12188           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12189             tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12190           else
12191             tem = invert_truthvalue_loc (loc, arg0);
12192           return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12193         }
12194       /* Identical arguments cancel to zero.  */
12195       if (operand_equal_p (arg0, arg1, 0))
12196         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12197
12198       /* !X ^ X is always true.  */
12199       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12200           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12201         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12202
12203       /* X ^ !X is always true.  */
12204       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12205           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12206         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12207
12208       return NULL_TREE;
12209
12210     case EQ_EXPR:
12211     case NE_EXPR:
12212       STRIP_NOPS (arg0);
12213       STRIP_NOPS (arg1);
12214
12215       tem = fold_comparison (loc, code, type, op0, op1);
12216       if (tem != NULL_TREE)
12217         return tem;
12218
12219       /* bool_var != 0 becomes bool_var. */
12220       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12221           && code == NE_EXPR)
12222         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12223
12224       /* bool_var == 1 becomes bool_var. */
12225       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12226           && code == EQ_EXPR)
12227         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12228
12229       /* bool_var != 1 becomes !bool_var. */
12230       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12231           && code == NE_EXPR)
12232         return fold_convert_loc (loc, type,
12233                                  fold_build1_loc (loc, TRUTH_NOT_EXPR,
12234                                                   TREE_TYPE (arg0), arg0));
12235
12236       /* bool_var == 0 becomes !bool_var. */
12237       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12238           && code == EQ_EXPR)
12239         return fold_convert_loc (loc, type,
12240                                  fold_build1_loc (loc, TRUTH_NOT_EXPR,
12241                                                   TREE_TYPE (arg0), arg0));
12242
12243       /* !exp != 0 becomes !exp */
12244       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12245           && code == NE_EXPR)
12246         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12247
12248       /* If this is an equality comparison of the address of two non-weak,
12249          unaliased symbols neither of which are extern (since we do not
12250          have access to attributes for externs), then we know the result.  */
12251       if (TREE_CODE (arg0) == ADDR_EXPR
12252           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12253           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12254           && ! lookup_attribute ("alias",
12255                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12256           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12257           && TREE_CODE (arg1) == ADDR_EXPR
12258           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12259           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12260           && ! lookup_attribute ("alias",
12261                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12262           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12263         {
12264           /* We know that we're looking at the address of two
12265              non-weak, unaliased, static _DECL nodes.
12266
12267              It is both wasteful and incorrect to call operand_equal_p
12268              to compare the two ADDR_EXPR nodes.  It is wasteful in that
12269              all we need to do is test pointer equality for the arguments
12270              to the two ADDR_EXPR nodes.  It is incorrect to use
12271              operand_equal_p as that function is NOT equivalent to a
12272              C equality test.  It can in fact return false for two
12273              objects which would test as equal using the C equality
12274              operator.  */
12275           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12276           return constant_boolean_node (equal
12277                                         ? code == EQ_EXPR : code != EQ_EXPR,
12278                                         type);
12279         }
12280
12281       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12282          a MINUS_EXPR of a constant, we can convert it into a comparison with
12283          a revised constant as long as no overflow occurs.  */
12284       if (TREE_CODE (arg1) == INTEGER_CST
12285           && (TREE_CODE (arg0) == PLUS_EXPR
12286               || TREE_CODE (arg0) == MINUS_EXPR)
12287           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12288           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12289                                       ? MINUS_EXPR : PLUS_EXPR,
12290                                       fold_convert_loc (loc, TREE_TYPE (arg0),
12291                                                         arg1),
12292                                       TREE_OPERAND (arg0, 1)))
12293           && !TREE_OVERFLOW (tem))
12294         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12295
12296       /* Similarly for a NEGATE_EXPR.  */
12297       if (TREE_CODE (arg0) == NEGATE_EXPR
12298           && TREE_CODE (arg1) == INTEGER_CST
12299           && 0 != (tem = negate_expr (fold_convert_loc (loc, TREE_TYPE (arg0),
12300                                                         arg1)))
12301           && TREE_CODE (tem) == INTEGER_CST
12302           && !TREE_OVERFLOW (tem))
12303         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12304
12305       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
12306       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12307           && TREE_CODE (arg1) == INTEGER_CST
12308           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12309         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12310                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12311                                          fold_convert_loc (loc,
12312                                                            TREE_TYPE (arg0),
12313                                                            arg1),
12314                                          TREE_OPERAND (arg0, 1)));
12315
12316       /* Transform comparisons of the form X +- Y CMP X to Y CMP 0.  */
12317       if ((TREE_CODE (arg0) == PLUS_EXPR
12318            || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12319            || TREE_CODE (arg0) == MINUS_EXPR)
12320           && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12321                                                                         0)),
12322                               arg1, 0)
12323           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12324               || POINTER_TYPE_P (TREE_TYPE (arg0))))
12325         {
12326           tree val = TREE_OPERAND (arg0, 1);
12327           return omit_two_operands_loc (loc, type,
12328                                     fold_build2_loc (loc, code, type,
12329                                                  val,
12330                                                  build_int_cst (TREE_TYPE (val),
12331                                                                 0)),
12332                                     TREE_OPERAND (arg0, 0), arg1);
12333         }
12334
12335       /* Transform comparisons of the form C - X CMP X if C % 2 == 1.  */
12336       if (TREE_CODE (arg0) == MINUS_EXPR
12337           && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12338           && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12339                                                                         1)),
12340                               arg1, 0)
12341           && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12342         {
12343           return omit_two_operands_loc (loc, type,
12344                                     code == NE_EXPR
12345                                     ? boolean_true_node : boolean_false_node,
12346                                     TREE_OPERAND (arg0, 1), arg1);
12347         }
12348
12349       /* If we have X - Y == 0, we can convert that to X == Y and similarly
12350          for !=.  Don't do this for ordered comparisons due to overflow.  */
12351       if (TREE_CODE (arg0) == MINUS_EXPR
12352           && integer_zerop (arg1))
12353         return fold_build2_loc (loc, code, type,
12354                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12355
12356       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
12357       if (TREE_CODE (arg0) == ABS_EXPR
12358           && (integer_zerop (arg1) || real_zerop (arg1)))
12359         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12360
12361       /* If this is an EQ or NE comparison with zero and ARG0 is
12362          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
12363          two operations, but the latter can be done in one less insn
12364          on machines that have only two-operand insns or on which a
12365          constant cannot be the first operand.  */
12366       if (TREE_CODE (arg0) == BIT_AND_EXPR
12367           && integer_zerop (arg1))
12368         {
12369           tree arg00 = TREE_OPERAND (arg0, 0);
12370           tree arg01 = TREE_OPERAND (arg0, 1);
12371           if (TREE_CODE (arg00) == LSHIFT_EXPR
12372               && integer_onep (TREE_OPERAND (arg00, 0)))
12373             {
12374               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12375                                       arg01, TREE_OPERAND (arg00, 1));
12376               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12377                                  build_int_cst (TREE_TYPE (arg0), 1));
12378               return fold_build2_loc (loc, code, type,
12379                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12380                                   arg1);
12381             }
12382           else if (TREE_CODE (arg01) == LSHIFT_EXPR
12383                    && integer_onep (TREE_OPERAND (arg01, 0)))
12384             {
12385               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12386                                       arg00, TREE_OPERAND (arg01, 1));
12387               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12388                                  build_int_cst (TREE_TYPE (arg0), 1));
12389               return fold_build2_loc (loc, code, type,
12390                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12391                                   arg1);
12392             }
12393         }
12394
12395       /* If this is an NE or EQ comparison of zero against the result of a
12396          signed MOD operation whose second operand is a power of 2, make
12397          the MOD operation unsigned since it is simpler and equivalent.  */
12398       if (integer_zerop (arg1)
12399           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12400           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12401               || TREE_CODE (arg0) == CEIL_MOD_EXPR
12402               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12403               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12404           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12405         {
12406           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12407           tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12408                                      fold_convert_loc (loc, newtype,
12409                                                        TREE_OPERAND (arg0, 0)),
12410                                      fold_convert_loc (loc, newtype,
12411                                                        TREE_OPERAND (arg0, 1)));
12412
12413           return fold_build2_loc (loc, code, type, newmod,
12414                               fold_convert_loc (loc, newtype, arg1));
12415         }
12416
12417       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12418          C1 is a valid shift constant, and C2 is a power of two, i.e.
12419          a single bit.  */
12420       if (TREE_CODE (arg0) == BIT_AND_EXPR
12421           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12422           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12423              == INTEGER_CST
12424           && integer_pow2p (TREE_OPERAND (arg0, 1))
12425           && integer_zerop (arg1))
12426         {
12427           tree itype = TREE_TYPE (arg0);
12428           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12429           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12430
12431           /* Check for a valid shift count.  */
12432           if (TREE_INT_CST_HIGH (arg001) == 0
12433               && TREE_INT_CST_LOW (arg001) < prec)
12434             {
12435               tree arg01 = TREE_OPERAND (arg0, 1);
12436               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12437               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12438               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12439                  can be rewritten as (X & (C2 << C1)) != 0.  */
12440               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12441                 {
12442                   tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12443                   tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12444                   return fold_build2_loc (loc, code, type, tem,
12445                                           fold_convert_loc (loc, itype, arg1));
12446                 }
12447               /* Otherwise, for signed (arithmetic) shifts,
12448                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12449                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
12450               else if (!TYPE_UNSIGNED (itype))
12451                 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12452                                     arg000, build_int_cst (itype, 0));
12453               /* Otherwise, of unsigned (logical) shifts,
12454                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12455                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
12456               else
12457                 return omit_one_operand_loc (loc, type,
12458                                          code == EQ_EXPR ? integer_one_node
12459                                                          : integer_zero_node,
12460                                          arg000);
12461             }
12462         }
12463
12464       /* If we have (A & C) == C where C is a power of 2, convert this into
12465          (A & C) != 0.  Similarly for NE_EXPR.  */
12466       if (TREE_CODE (arg0) == BIT_AND_EXPR
12467           && integer_pow2p (TREE_OPERAND (arg0, 1))
12468           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12469         return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12470                             arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12471                                                     integer_zero_node));
12472
12473       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12474          bit, then fold the expression into A < 0 or A >= 0.  */
12475       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12476       if (tem)
12477         return tem;
12478
12479       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12480          Similarly for NE_EXPR.  */
12481       if (TREE_CODE (arg0) == BIT_AND_EXPR
12482           && TREE_CODE (arg1) == INTEGER_CST
12483           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12484         {
12485           tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12486                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
12487                                    TREE_OPERAND (arg0, 1));
12488           tree dandnotc
12489             = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12490                                fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
12491                                notc);
12492           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12493           if (integer_nonzerop (dandnotc))
12494             return omit_one_operand_loc (loc, type, rslt, arg0);
12495         }
12496
12497       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12498          Similarly for NE_EXPR.  */
12499       if (TREE_CODE (arg0) == BIT_IOR_EXPR
12500           && TREE_CODE (arg1) == INTEGER_CST
12501           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12502         {
12503           tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12504           tree candnotd
12505             = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12506                                TREE_OPERAND (arg0, 1),
12507                                fold_convert_loc (loc, TREE_TYPE (arg0), notd));
12508           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12509           if (integer_nonzerop (candnotd))
12510             return omit_one_operand_loc (loc, type, rslt, arg0);
12511         }
12512
12513       /* If this is a comparison of a field, we may be able to simplify it.  */
12514       if ((TREE_CODE (arg0) == COMPONENT_REF
12515            || TREE_CODE (arg0) == BIT_FIELD_REF)
12516           /* Handle the constant case even without -O
12517              to make sure the warnings are given.  */
12518           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12519         {
12520           t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12521           if (t1)
12522             return t1;
12523         }
12524
12525       /* Optimize comparisons of strlen vs zero to a compare of the
12526          first character of the string vs zero.  To wit,
12527                 strlen(ptr) == 0   =>  *ptr == 0
12528                 strlen(ptr) != 0   =>  *ptr != 0
12529          Other cases should reduce to one of these two (or a constant)
12530          due to the return value of strlen being unsigned.  */
12531       if (TREE_CODE (arg0) == CALL_EXPR
12532           && integer_zerop (arg1))
12533         {
12534           tree fndecl = get_callee_fndecl (arg0);
12535
12536           if (fndecl
12537               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12538               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12539               && call_expr_nargs (arg0) == 1
12540               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12541             {
12542               tree iref = build_fold_indirect_ref_loc (loc,
12543                                                    CALL_EXPR_ARG (arg0, 0));
12544               return fold_build2_loc (loc, code, type, iref,
12545                                   build_int_cst (TREE_TYPE (iref), 0));
12546             }
12547         }
12548
12549       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12550          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12551       if (TREE_CODE (arg0) == RSHIFT_EXPR
12552           && integer_zerop (arg1)
12553           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12554         {
12555           tree arg00 = TREE_OPERAND (arg0, 0);
12556           tree arg01 = TREE_OPERAND (arg0, 1);
12557           tree itype = TREE_TYPE (arg00);
12558           if (TREE_INT_CST_HIGH (arg01) == 0
12559               && TREE_INT_CST_LOW (arg01)
12560                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12561             {
12562               if (TYPE_UNSIGNED (itype))
12563                 {
12564                   itype = signed_type_for (itype);
12565                   arg00 = fold_convert_loc (loc, itype, arg00);
12566                 }
12567               return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12568                                   type, arg00, build_int_cst (itype, 0));
12569             }
12570         }
12571
12572       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12573       if (integer_zerop (arg1)
12574           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12575         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12576                             TREE_OPERAND (arg0, 1));
12577
12578       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12579       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12580           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12581         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12582                                 build_int_cst (TREE_TYPE (arg0), 0));
12583       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12584       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12585           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12586           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12587         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12588                                 build_int_cst (TREE_TYPE (arg0), 0));
12589
12590       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12591       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12592           && TREE_CODE (arg1) == INTEGER_CST
12593           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12594         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12595                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12596                                          TREE_OPERAND (arg0, 1), arg1));
12597
12598       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12599          (X & C) == 0 when C is a single bit.  */
12600       if (TREE_CODE (arg0) == BIT_AND_EXPR
12601           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12602           && integer_zerop (arg1)
12603           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12604         {
12605           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12606                                  TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12607                                  TREE_OPERAND (arg0, 1));
12608           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12609                                   type, tem,
12610                                   fold_convert_loc (loc, TREE_TYPE (arg0),
12611                                                     arg1));
12612         }
12613
12614       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12615          constant C is a power of two, i.e. a single bit.  */
12616       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12617           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12618           && integer_zerop (arg1)
12619           && integer_pow2p (TREE_OPERAND (arg0, 1))
12620           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12621                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12622         {
12623           tree arg00 = TREE_OPERAND (arg0, 0);
12624           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12625                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
12626         }
12627
12628       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12629          when is C is a power of two, i.e. a single bit.  */
12630       if (TREE_CODE (arg0) == BIT_AND_EXPR
12631           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12632           && integer_zerop (arg1)
12633           && integer_pow2p (TREE_OPERAND (arg0, 1))
12634           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12635                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12636         {
12637           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12638           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12639                              arg000, TREE_OPERAND (arg0, 1));
12640           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12641                               tem, build_int_cst (TREE_TYPE (tem), 0));
12642         }
12643
12644       if (integer_zerop (arg1)
12645           && tree_expr_nonzero_p (arg0))
12646         {
12647           tree res = constant_boolean_node (code==NE_EXPR, type);
12648           return omit_one_operand_loc (loc, type, res, arg0);
12649         }
12650
12651       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
12652       if (TREE_CODE (arg0) == NEGATE_EXPR
12653           && TREE_CODE (arg1) == NEGATE_EXPR)
12654         return fold_build2_loc (loc, code, type,
12655                                 TREE_OPERAND (arg0, 0),
12656                                 fold_convert_loc (loc, TREE_TYPE (arg0),
12657                                                   TREE_OPERAND (arg1, 0)));
12658
12659       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
12660       if (TREE_CODE (arg0) == BIT_AND_EXPR
12661           && TREE_CODE (arg1) == BIT_AND_EXPR)
12662         {
12663           tree arg00 = TREE_OPERAND (arg0, 0);
12664           tree arg01 = TREE_OPERAND (arg0, 1);
12665           tree arg10 = TREE_OPERAND (arg1, 0);
12666           tree arg11 = TREE_OPERAND (arg1, 1);
12667           tree itype = TREE_TYPE (arg0);
12668
12669           if (operand_equal_p (arg01, arg11, 0))
12670             return fold_build2_loc (loc, code, type,
12671                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12672                                              fold_build2_loc (loc,
12673                                                           BIT_XOR_EXPR, itype,
12674                                                           arg00, arg10),
12675                                              arg01),
12676                                 build_int_cst (itype, 0));
12677
12678           if (operand_equal_p (arg01, arg10, 0))
12679             return fold_build2_loc (loc, code, type,
12680                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12681                                              fold_build2_loc (loc,
12682                                                           BIT_XOR_EXPR, itype,
12683                                                           arg00, arg11),
12684                                              arg01),
12685                                 build_int_cst (itype, 0));
12686
12687           if (operand_equal_p (arg00, arg11, 0))
12688             return fold_build2_loc (loc, code, type,
12689                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12690                                              fold_build2_loc (loc,
12691                                                           BIT_XOR_EXPR, itype,
12692                                                           arg01, arg10),
12693                                              arg00),
12694                                 build_int_cst (itype, 0));
12695
12696           if (operand_equal_p (arg00, arg10, 0))
12697             return fold_build2_loc (loc, code, type,
12698                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12699                                              fold_build2_loc (loc,
12700                                                           BIT_XOR_EXPR, itype,
12701                                                           arg01, arg11),
12702                                              arg00),
12703                                 build_int_cst (itype, 0));
12704         }
12705
12706       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12707           && TREE_CODE (arg1) == BIT_XOR_EXPR)
12708         {
12709           tree arg00 = TREE_OPERAND (arg0, 0);
12710           tree arg01 = TREE_OPERAND (arg0, 1);
12711           tree arg10 = TREE_OPERAND (arg1, 0);
12712           tree arg11 = TREE_OPERAND (arg1, 1);
12713           tree itype = TREE_TYPE (arg0);
12714
12715           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12716              operand_equal_p guarantees no side-effects so we don't need
12717              to use omit_one_operand on Z.  */
12718           if (operand_equal_p (arg01, arg11, 0))
12719             return fold_build2_loc (loc, code, type, arg00,
12720                                     fold_convert_loc (loc, TREE_TYPE (arg00),
12721                                                       arg10));
12722           if (operand_equal_p (arg01, arg10, 0))
12723             return fold_build2_loc (loc, code, type, arg00,
12724                                     fold_convert_loc (loc, TREE_TYPE (arg00),
12725                                                       arg11));
12726           if (operand_equal_p (arg00, arg11, 0))
12727             return fold_build2_loc (loc, code, type, arg01,
12728                                     fold_convert_loc (loc, TREE_TYPE (arg01),
12729                                                       arg10));
12730           if (operand_equal_p (arg00, arg10, 0))
12731             return fold_build2_loc (loc, code, type, arg01,
12732                                     fold_convert_loc (loc, TREE_TYPE (arg01),
12733                                                       arg11));
12734
12735           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
12736           if (TREE_CODE (arg01) == INTEGER_CST
12737               && TREE_CODE (arg11) == INTEGER_CST)
12738             {
12739               tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12740                                      fold_convert_loc (loc, itype, arg11));
12741               tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12742               return fold_build2_loc (loc, code, type, tem,
12743                                       fold_convert_loc (loc, itype, arg10));
12744             }
12745         }
12746
12747       /* Attempt to simplify equality/inequality comparisons of complex
12748          values.  Only lower the comparison if the result is known or
12749          can be simplified to a single scalar comparison.  */
12750       if ((TREE_CODE (arg0) == COMPLEX_EXPR
12751            || TREE_CODE (arg0) == COMPLEX_CST)
12752           && (TREE_CODE (arg1) == COMPLEX_EXPR
12753               || TREE_CODE (arg1) == COMPLEX_CST))
12754         {
12755           tree real0, imag0, real1, imag1;
12756           tree rcond, icond;
12757
12758           if (TREE_CODE (arg0) == COMPLEX_EXPR)
12759             {
12760               real0 = TREE_OPERAND (arg0, 0);
12761               imag0 = TREE_OPERAND (arg0, 1);
12762             }
12763           else
12764             {
12765               real0 = TREE_REALPART (arg0);
12766               imag0 = TREE_IMAGPART (arg0);
12767             }
12768
12769           if (TREE_CODE (arg1) == COMPLEX_EXPR)
12770             {
12771               real1 = TREE_OPERAND (arg1, 0);
12772               imag1 = TREE_OPERAND (arg1, 1);
12773             }
12774           else
12775             {
12776               real1 = TREE_REALPART (arg1);
12777               imag1 = TREE_IMAGPART (arg1);
12778             }
12779
12780           rcond = fold_binary_loc (loc, code, type, real0, real1);
12781           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12782             {
12783               if (integer_zerop (rcond))
12784                 {
12785                   if (code == EQ_EXPR)
12786                     return omit_two_operands_loc (loc, type, boolean_false_node,
12787                                               imag0, imag1);
12788                   return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12789                 }
12790               else
12791                 {
12792                   if (code == NE_EXPR)
12793                     return omit_two_operands_loc (loc, type, boolean_true_node,
12794                                               imag0, imag1);
12795                   return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12796                 }
12797             }
12798
12799           icond = fold_binary_loc (loc, code, type, imag0, imag1);
12800           if (icond && TREE_CODE (icond) == INTEGER_CST)
12801             {
12802               if (integer_zerop (icond))
12803                 {
12804                   if (code == EQ_EXPR)
12805                     return omit_two_operands_loc (loc, type, boolean_false_node,
12806                                               real0, real1);
12807                   return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12808                 }
12809               else
12810                 {
12811                   if (code == NE_EXPR)
12812                     return omit_two_operands_loc (loc, type, boolean_true_node,
12813                                               real0, real1);
12814                   return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12815                 }
12816             }
12817         }
12818
12819       return NULL_TREE;
12820
12821     case LT_EXPR:
12822     case GT_EXPR:
12823     case LE_EXPR:
12824     case GE_EXPR:
12825       tem = fold_comparison (loc, code, type, op0, op1);
12826       if (tem != NULL_TREE)
12827         return tem;
12828
12829       /* Transform comparisons of the form X +- C CMP X.  */
12830       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12831           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12832           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12833                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12834               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12835                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12836         {
12837           tree arg01 = TREE_OPERAND (arg0, 1);
12838           enum tree_code code0 = TREE_CODE (arg0);
12839           int is_positive;
12840
12841           if (TREE_CODE (arg01) == REAL_CST)
12842             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12843           else
12844             is_positive = tree_int_cst_sgn (arg01);
12845
12846           /* (X - c) > X becomes false.  */
12847           if (code == GT_EXPR
12848               && ((code0 == MINUS_EXPR && is_positive >= 0)
12849                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12850             {
12851               if (TREE_CODE (arg01) == INTEGER_CST
12852                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12853                 fold_overflow_warning (("assuming signed overflow does not "
12854                                         "occur when assuming that (X - c) > X "
12855                                         "is always false"),
12856                                        WARN_STRICT_OVERFLOW_ALL);
12857               return constant_boolean_node (0, type);
12858             }
12859
12860           /* Likewise (X + c) < X becomes false.  */
12861           if (code == LT_EXPR
12862               && ((code0 == PLUS_EXPR && is_positive >= 0)
12863                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12864             {
12865               if (TREE_CODE (arg01) == INTEGER_CST
12866                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12867                 fold_overflow_warning (("assuming signed overflow does not "
12868                                         "occur when assuming that "
12869                                         "(X + c) < X is always false"),
12870                                        WARN_STRICT_OVERFLOW_ALL);
12871               return constant_boolean_node (0, type);
12872             }
12873
12874           /* Convert (X - c) <= X to true.  */
12875           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12876               && code == LE_EXPR
12877               && ((code0 == MINUS_EXPR && is_positive >= 0)
12878                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12879             {
12880               if (TREE_CODE (arg01) == INTEGER_CST
12881                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12882                 fold_overflow_warning (("assuming signed overflow does not "
12883                                         "occur when assuming that "
12884                                         "(X - c) <= X is always true"),
12885                                        WARN_STRICT_OVERFLOW_ALL);
12886               return constant_boolean_node (1, type);
12887             }
12888
12889           /* Convert (X + c) >= X to true.  */
12890           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12891               && code == GE_EXPR
12892               && ((code0 == PLUS_EXPR && is_positive >= 0)
12893                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12894             {
12895               if (TREE_CODE (arg01) == INTEGER_CST
12896                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12897                 fold_overflow_warning (("assuming signed overflow does not "
12898                                         "occur when assuming that "
12899                                         "(X + c) >= X is always true"),
12900                                        WARN_STRICT_OVERFLOW_ALL);
12901               return constant_boolean_node (1, type);
12902             }
12903
12904           if (TREE_CODE (arg01) == INTEGER_CST)
12905             {
12906               /* Convert X + c > X and X - c < X to true for integers.  */
12907               if (code == GT_EXPR
12908                   && ((code0 == PLUS_EXPR && is_positive > 0)
12909                       || (code0 == MINUS_EXPR && is_positive < 0)))
12910                 {
12911                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12912                     fold_overflow_warning (("assuming signed overflow does "
12913                                             "not occur when assuming that "
12914                                             "(X + c) > X is always true"),
12915                                            WARN_STRICT_OVERFLOW_ALL);
12916                   return constant_boolean_node (1, type);
12917                 }
12918
12919               if (code == LT_EXPR
12920                   && ((code0 == MINUS_EXPR && is_positive > 0)
12921                       || (code0 == PLUS_EXPR && is_positive < 0)))
12922                 {
12923                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12924                     fold_overflow_warning (("assuming signed overflow does "
12925                                             "not occur when assuming that "
12926                                             "(X - c) < X is always true"),
12927                                            WARN_STRICT_OVERFLOW_ALL);
12928                   return constant_boolean_node (1, type);
12929                 }
12930
12931               /* Convert X + c <= X and X - c >= X to false for integers.  */
12932               if (code == LE_EXPR
12933                   && ((code0 == PLUS_EXPR && is_positive > 0)
12934                       || (code0 == MINUS_EXPR && is_positive < 0)))
12935                 {
12936                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12937                     fold_overflow_warning (("assuming signed overflow does "
12938                                             "not occur when assuming that "
12939                                             "(X + c) <= X is always false"),
12940                                            WARN_STRICT_OVERFLOW_ALL);
12941                   return constant_boolean_node (0, type);
12942                 }
12943
12944               if (code == GE_EXPR
12945                   && ((code0 == MINUS_EXPR && is_positive > 0)
12946                       || (code0 == PLUS_EXPR && is_positive < 0)))
12947                 {
12948                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12949                     fold_overflow_warning (("assuming signed overflow does "
12950                                             "not occur when assuming that "
12951                                             "(X - c) >= X is always false"),
12952                                            WARN_STRICT_OVERFLOW_ALL);
12953                   return constant_boolean_node (0, type);
12954                 }
12955             }
12956         }
12957
12958       /* Comparisons with the highest or lowest possible integer of
12959          the specified precision will have known values.  */
12960       {
12961         tree arg1_type = TREE_TYPE (arg1);
12962         unsigned int width = TYPE_PRECISION (arg1_type);
12963
12964         if (TREE_CODE (arg1) == INTEGER_CST
12965             && width <= 2 * HOST_BITS_PER_WIDE_INT
12966             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12967           {
12968             HOST_WIDE_INT signed_max_hi;
12969             unsigned HOST_WIDE_INT signed_max_lo;
12970             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12971
12972             if (width <= HOST_BITS_PER_WIDE_INT)
12973               {
12974                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12975                                 - 1;
12976                 signed_max_hi = 0;
12977                 max_hi = 0;
12978
12979                 if (TYPE_UNSIGNED (arg1_type))
12980                   {
12981                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12982                     min_lo = 0;
12983                     min_hi = 0;
12984                   }
12985                 else
12986                   {
12987                     max_lo = signed_max_lo;
12988                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12989                     min_hi = -1;
12990                   }
12991               }
12992             else
12993               {
12994                 width -= HOST_BITS_PER_WIDE_INT;
12995                 signed_max_lo = -1;
12996                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12997                                 - 1;
12998                 max_lo = -1;
12999                 min_lo = 0;
13000
13001                 if (TYPE_UNSIGNED (arg1_type))
13002                   {
13003                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
13004                     min_hi = 0;
13005                   }
13006                 else
13007                   {
13008                     max_hi = signed_max_hi;
13009                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
13010                   }
13011               }
13012
13013             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
13014                 && TREE_INT_CST_LOW (arg1) == max_lo)
13015               switch (code)
13016                 {
13017                 case GT_EXPR:
13018                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13019
13020                 case GE_EXPR:
13021                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13022
13023                 case LE_EXPR:
13024                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13025
13026                 case LT_EXPR:
13027                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13028
13029                 /* The GE_EXPR and LT_EXPR cases above are not normally
13030                    reached because of previous transformations.  */
13031
13032                 default:
13033                   break;
13034                 }
13035             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13036                      == max_hi
13037                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
13038               switch (code)
13039                 {
13040                 case GT_EXPR:
13041                   arg1 = const_binop (PLUS_EXPR, arg1,
13042                                       build_int_cst (TREE_TYPE (arg1), 1));
13043                   return fold_build2_loc (loc, EQ_EXPR, type,
13044                                       fold_convert_loc (loc,
13045                                                         TREE_TYPE (arg1), arg0),
13046                                       arg1);
13047                 case LE_EXPR:
13048                   arg1 = const_binop (PLUS_EXPR, arg1,
13049                                       build_int_cst (TREE_TYPE (arg1), 1));
13050                   return fold_build2_loc (loc, NE_EXPR, type,
13051                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13052                                                         arg0),
13053                                       arg1);
13054                 default:
13055                   break;
13056                 }
13057             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13058                      == min_hi
13059                      && TREE_INT_CST_LOW (arg1) == min_lo)
13060               switch (code)
13061                 {
13062                 case LT_EXPR:
13063                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13064
13065                 case LE_EXPR:
13066                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13067
13068                 case GE_EXPR:
13069                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13070
13071                 case GT_EXPR:
13072                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13073
13074                 default:
13075                   break;
13076                 }
13077             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13078                      == min_hi
13079                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
13080               switch (code)
13081                 {
13082                 case GE_EXPR:
13083                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
13084                   return fold_build2_loc (loc, NE_EXPR, type,
13085                                       fold_convert_loc (loc,
13086                                                         TREE_TYPE (arg1), arg0),
13087                                       arg1);
13088                 case LT_EXPR:
13089                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
13090                   return fold_build2_loc (loc, EQ_EXPR, type,
13091                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13092                                                         arg0),
13093                                       arg1);
13094                 default:
13095                   break;
13096                 }
13097
13098             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13099                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
13100                      && TYPE_UNSIGNED (arg1_type)
13101                      /* We will flip the signedness of the comparison operator
13102                         associated with the mode of arg1, so the sign bit is
13103                         specified by this mode.  Check that arg1 is the signed
13104                         max associated with this sign bit.  */
13105                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13106                      /* signed_type does not work on pointer types.  */
13107                      && INTEGRAL_TYPE_P (arg1_type))
13108               {
13109                 /* The following case also applies to X < signed_max+1
13110                    and X >= signed_max+1 because previous transformations.  */
13111                 if (code == LE_EXPR || code == GT_EXPR)
13112                   {
13113                     tree st;
13114                     st = signed_type_for (TREE_TYPE (arg1));
13115                     return fold_build2_loc (loc,
13116                                         code == LE_EXPR ? GE_EXPR : LT_EXPR,
13117                                         type, fold_convert_loc (loc, st, arg0),
13118                                         build_int_cst (st, 0));
13119                   }
13120               }
13121           }
13122       }
13123
13124       /* If we are comparing an ABS_EXPR with a constant, we can
13125          convert all the cases into explicit comparisons, but they may
13126          well not be faster than doing the ABS and one comparison.
13127          But ABS (X) <= C is a range comparison, which becomes a subtraction
13128          and a comparison, and is probably faster.  */
13129       if (code == LE_EXPR
13130           && TREE_CODE (arg1) == INTEGER_CST
13131           && TREE_CODE (arg0) == ABS_EXPR
13132           && ! TREE_SIDE_EFFECTS (arg0)
13133           && (0 != (tem = negate_expr (arg1)))
13134           && TREE_CODE (tem) == INTEGER_CST
13135           && !TREE_OVERFLOW (tem))
13136         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13137                             build2 (GE_EXPR, type,
13138                                     TREE_OPERAND (arg0, 0), tem),
13139                             build2 (LE_EXPR, type,
13140                                     TREE_OPERAND (arg0, 0), arg1));
13141
13142       /* Convert ABS_EXPR<x> >= 0 to true.  */
13143       strict_overflow_p = false;
13144       if (code == GE_EXPR
13145           && (integer_zerop (arg1)
13146               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13147                   && real_zerop (arg1)))
13148           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13149         {
13150           if (strict_overflow_p)
13151             fold_overflow_warning (("assuming signed overflow does not occur "
13152                                     "when simplifying comparison of "
13153                                     "absolute value and zero"),
13154                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13155           return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13156         }
13157
13158       /* Convert ABS_EXPR<x> < 0 to false.  */
13159       strict_overflow_p = false;
13160       if (code == LT_EXPR
13161           && (integer_zerop (arg1) || real_zerop (arg1))
13162           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13163         {
13164           if (strict_overflow_p)
13165             fold_overflow_warning (("assuming signed overflow does not occur "
13166                                     "when simplifying comparison of "
13167                                     "absolute value and zero"),
13168                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13169           return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13170         }
13171
13172       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13173          and similarly for >= into !=.  */
13174       if ((code == LT_EXPR || code == GE_EXPR)
13175           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13176           && TREE_CODE (arg1) == LSHIFT_EXPR
13177           && integer_onep (TREE_OPERAND (arg1, 0)))
13178         return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13179                            build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13180                                    TREE_OPERAND (arg1, 1)),
13181                            build_int_cst (TREE_TYPE (arg0), 0));
13182
13183       if ((code == LT_EXPR || code == GE_EXPR)
13184           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13185           && CONVERT_EXPR_P (arg1)
13186           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13187           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13188         {
13189           tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13190                         TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
13191           return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13192                              fold_convert_loc (loc, TREE_TYPE (arg0), tem),
13193                              build_int_cst (TREE_TYPE (arg0), 0));
13194         }
13195
13196       return NULL_TREE;
13197
13198     case UNORDERED_EXPR:
13199     case ORDERED_EXPR:
13200     case UNLT_EXPR:
13201     case UNLE_EXPR:
13202     case UNGT_EXPR:
13203     case UNGE_EXPR:
13204     case UNEQ_EXPR:
13205     case LTGT_EXPR:
13206       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13207         {
13208           t1 = fold_relational_const (code, type, arg0, arg1);
13209           if (t1 != NULL_TREE)
13210             return t1;
13211         }
13212
13213       /* If the first operand is NaN, the result is constant.  */
13214       if (TREE_CODE (arg0) == REAL_CST
13215           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13216           && (code != LTGT_EXPR || ! flag_trapping_math))
13217         {
13218           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13219                ? integer_zero_node
13220                : integer_one_node;
13221           return omit_one_operand_loc (loc, type, t1, arg1);
13222         }
13223
13224       /* If the second operand is NaN, the result is constant.  */
13225       if (TREE_CODE (arg1) == REAL_CST
13226           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13227           && (code != LTGT_EXPR || ! flag_trapping_math))
13228         {
13229           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13230                ? integer_zero_node
13231                : integer_one_node;
13232           return omit_one_operand_loc (loc, type, t1, arg0);
13233         }
13234
13235       /* Simplify unordered comparison of something with itself.  */
13236       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13237           && operand_equal_p (arg0, arg1, 0))
13238         return constant_boolean_node (1, type);
13239
13240       if (code == LTGT_EXPR
13241           && !flag_trapping_math
13242           && operand_equal_p (arg0, arg1, 0))
13243         return constant_boolean_node (0, type);
13244
13245       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
13246       {
13247         tree targ0 = strip_float_extensions (arg0);
13248         tree targ1 = strip_float_extensions (arg1);
13249         tree newtype = TREE_TYPE (targ0);
13250
13251         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13252           newtype = TREE_TYPE (targ1);
13253
13254         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13255           return fold_build2_loc (loc, code, type,
13256                               fold_convert_loc (loc, newtype, targ0),
13257                               fold_convert_loc (loc, newtype, targ1));
13258       }
13259
13260       return NULL_TREE;
13261
13262     case COMPOUND_EXPR:
13263       /* When pedantic, a compound expression can be neither an lvalue
13264          nor an integer constant expression.  */
13265       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13266         return NULL_TREE;
13267       /* Don't let (0, 0) be null pointer constant.  */
13268       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13269                                  : fold_convert_loc (loc, type, arg1);
13270       return pedantic_non_lvalue_loc (loc, tem);
13271
13272     case COMPLEX_EXPR:
13273       if ((TREE_CODE (arg0) == REAL_CST
13274            && TREE_CODE (arg1) == REAL_CST)
13275           || (TREE_CODE (arg0) == INTEGER_CST
13276               && TREE_CODE (arg1) == INTEGER_CST))
13277         return build_complex (type, arg0, arg1);
13278       if (TREE_CODE (arg0) == REALPART_EXPR
13279           && TREE_CODE (arg1) == IMAGPART_EXPR
13280           && TREE_TYPE (TREE_OPERAND (arg0, 0)) == type
13281           && operand_equal_p (TREE_OPERAND (arg0, 0),
13282                               TREE_OPERAND (arg1, 0), 0))
13283         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
13284                                      TREE_OPERAND (arg1, 0));
13285       return NULL_TREE;
13286
13287     case ASSERT_EXPR:
13288       /* An ASSERT_EXPR should never be passed to fold_binary.  */
13289       gcc_unreachable ();
13290
13291     default:
13292       return NULL_TREE;
13293     } /* switch (code) */
13294 }
13295
13296 /* Callback for walk_tree, looking for LABEL_EXPR.  Return *TP if it is
13297    a LABEL_EXPR; otherwise return NULL_TREE.  Do not check the subtrees
13298    of GOTO_EXPR.  */
13299
13300 static tree
13301 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13302 {
13303   switch (TREE_CODE (*tp))
13304     {
13305     case LABEL_EXPR:
13306       return *tp;
13307
13308     case GOTO_EXPR:
13309       *walk_subtrees = 0;
13310
13311       /* ... fall through ...  */
13312
13313     default:
13314       return NULL_TREE;
13315     }
13316 }
13317
13318 /* Return whether the sub-tree ST contains a label which is accessible from
13319    outside the sub-tree.  */
13320
13321 static bool
13322 contains_label_p (tree st)
13323 {
13324   return
13325    (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13326 }
13327
13328 /* Fold a ternary expression of code CODE and type TYPE with operands
13329    OP0, OP1, and OP2.  Return the folded expression if folding is
13330    successful.  Otherwise, return NULL_TREE.  */
13331
13332 tree
13333 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13334                   tree op0, tree op1, tree op2)
13335 {
13336   tree tem;
13337   tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
13338   enum tree_code_class kind = TREE_CODE_CLASS (code);
13339
13340   gcc_assert (IS_EXPR_CODE_CLASS (kind)
13341               && TREE_CODE_LENGTH (code) == 3);
13342
13343   /* Strip any conversions that don't change the mode.  This is safe
13344      for every expression, except for a comparison expression because
13345      its signedness is derived from its operands.  So, in the latter
13346      case, only strip conversions that don't change the signedness.
13347
13348      Note that this is done as an internal manipulation within the
13349      constant folder, in order to find the simplest representation of
13350      the arguments so that their form can be studied.  In any cases,
13351      the appropriate type conversions should be put back in the tree
13352      that will get out of the constant folder.  */
13353   if (op0)
13354     {
13355       arg0 = op0;
13356       STRIP_NOPS (arg0);
13357     }
13358
13359   if (op1)
13360     {
13361       arg1 = op1;
13362       STRIP_NOPS (arg1);
13363     }
13364
13365   if (op2)
13366     {
13367       arg2 = op2;
13368       STRIP_NOPS (arg2);
13369     }
13370
13371   switch (code)
13372     {
13373     case COMPONENT_REF:
13374       if (TREE_CODE (arg0) == CONSTRUCTOR
13375           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13376         {
13377           unsigned HOST_WIDE_INT idx;
13378           tree field, value;
13379           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13380             if (field == arg1)
13381               return value;
13382         }
13383       return NULL_TREE;
13384
13385     case COND_EXPR:
13386       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13387          so all simple results must be passed through pedantic_non_lvalue.  */
13388       if (TREE_CODE (arg0) == INTEGER_CST)
13389         {
13390           tree unused_op = integer_zerop (arg0) ? op1 : op2;
13391           tem = integer_zerop (arg0) ? op2 : op1;
13392           /* Only optimize constant conditions when the selected branch
13393              has the same type as the COND_EXPR.  This avoids optimizing
13394              away "c ? x : throw", where the throw has a void type.
13395              Avoid throwing away that operand which contains label.  */
13396           if ((!TREE_SIDE_EFFECTS (unused_op)
13397                || !contains_label_p (unused_op))
13398               && (! VOID_TYPE_P (TREE_TYPE (tem))
13399                   || VOID_TYPE_P (type)))
13400             return pedantic_non_lvalue_loc (loc, tem);
13401           return NULL_TREE;
13402         }
13403       if (operand_equal_p (arg1, op2, 0))
13404         return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13405
13406       /* If we have A op B ? A : C, we may be able to convert this to a
13407          simpler expression, depending on the operation and the values
13408          of B and C.  Signed zeros prevent all of these transformations,
13409          for reasons given above each one.
13410
13411          Also try swapping the arguments and inverting the conditional.  */
13412       if (COMPARISON_CLASS_P (arg0)
13413           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13414                                              arg1, TREE_OPERAND (arg0, 1))
13415           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13416         {
13417           tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13418           if (tem)
13419             return tem;
13420         }
13421
13422       if (COMPARISON_CLASS_P (arg0)
13423           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13424                                              op2,
13425                                              TREE_OPERAND (arg0, 1))
13426           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13427         {
13428           location_t loc0 = expr_location_or (arg0, loc);
13429           tem = fold_truth_not_expr (loc0, arg0);
13430           if (tem && COMPARISON_CLASS_P (tem))
13431             {
13432               tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13433               if (tem)
13434                 return tem;
13435             }
13436         }
13437
13438       /* If the second operand is simpler than the third, swap them
13439          since that produces better jump optimization results.  */
13440       if (truth_value_p (TREE_CODE (arg0))
13441           && tree_swap_operands_p (op1, op2, false))
13442         {
13443           location_t loc0 = expr_location_or (arg0, loc);
13444           /* See if this can be inverted.  If it can't, possibly because
13445              it was a floating-point inequality comparison, don't do
13446              anything.  */
13447           tem = fold_truth_not_expr (loc0, arg0);
13448           if (tem)
13449             return fold_build3_loc (loc, code, type, tem, op2, op1);
13450         }
13451
13452       /* Convert A ? 1 : 0 to simply A.  */
13453       if (integer_onep (op1)
13454           && integer_zerop (op2)
13455           /* If we try to convert OP0 to our type, the
13456              call to fold will try to move the conversion inside
13457              a COND, which will recurse.  In that case, the COND_EXPR
13458              is probably the best choice, so leave it alone.  */
13459           && type == TREE_TYPE (arg0))
13460         return pedantic_non_lvalue_loc (loc, arg0);
13461
13462       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
13463          over COND_EXPR in cases such as floating point comparisons.  */
13464       if (integer_zerop (op1)
13465           && integer_onep (op2)
13466           && truth_value_p (TREE_CODE (arg0)))
13467         return pedantic_non_lvalue_loc (loc,
13468                                     fold_convert_loc (loc, type,
13469                                               invert_truthvalue_loc (loc,
13470                                                                      arg0)));
13471
13472       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
13473       if (TREE_CODE (arg0) == LT_EXPR
13474           && integer_zerop (TREE_OPERAND (arg0, 1))
13475           && integer_zerop (op2)
13476           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13477         {
13478           /* sign_bit_p only checks ARG1 bits within A's precision.
13479              If <sign bit of A> has wider type than A, bits outside
13480              of A's precision in <sign bit of A> need to be checked.
13481              If they are all 0, this optimization needs to be done
13482              in unsigned A's type, if they are all 1 in signed A's type,
13483              otherwise this can't be done.  */
13484           if (TYPE_PRECISION (TREE_TYPE (tem))
13485               < TYPE_PRECISION (TREE_TYPE (arg1))
13486               && TYPE_PRECISION (TREE_TYPE (tem))
13487                  < TYPE_PRECISION (type))
13488             {
13489               unsigned HOST_WIDE_INT mask_lo;
13490               HOST_WIDE_INT mask_hi;
13491               int inner_width, outer_width;
13492               tree tem_type;
13493
13494               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13495               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13496               if (outer_width > TYPE_PRECISION (type))
13497                 outer_width = TYPE_PRECISION (type);
13498
13499               if (outer_width > HOST_BITS_PER_WIDE_INT)
13500                 {
13501                   mask_hi = ((unsigned HOST_WIDE_INT) -1
13502                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13503                   mask_lo = -1;
13504                 }
13505               else
13506                 {
13507                   mask_hi = 0;
13508                   mask_lo = ((unsigned HOST_WIDE_INT) -1
13509                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
13510                 }
13511               if (inner_width > HOST_BITS_PER_WIDE_INT)
13512                 {
13513                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13514                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
13515                   mask_lo = 0;
13516                 }
13517               else
13518                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13519                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
13520
13521               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13522                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13523                 {
13524                   tem_type = signed_type_for (TREE_TYPE (tem));
13525                   tem = fold_convert_loc (loc, tem_type, tem);
13526                 }
13527               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13528                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13529                 {
13530                   tem_type = unsigned_type_for (TREE_TYPE (tem));
13531                   tem = fold_convert_loc (loc, tem_type, tem);
13532                 }
13533               else
13534                 tem = NULL;
13535             }
13536
13537           if (tem)
13538             return
13539               fold_convert_loc (loc, type,
13540                                 fold_build2_loc (loc, BIT_AND_EXPR,
13541                                              TREE_TYPE (tem), tem,
13542                                              fold_convert_loc (loc,
13543                                                                TREE_TYPE (tem),
13544                                                                arg1)));
13545         }
13546
13547       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
13548          already handled above.  */
13549       if (TREE_CODE (arg0) == BIT_AND_EXPR
13550           && integer_onep (TREE_OPERAND (arg0, 1))
13551           && integer_zerop (op2)
13552           && integer_pow2p (arg1))
13553         {
13554           tree tem = TREE_OPERAND (arg0, 0);
13555           STRIP_NOPS (tem);
13556           if (TREE_CODE (tem) == RSHIFT_EXPR
13557               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13558               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13559                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13560             return fold_build2_loc (loc, BIT_AND_EXPR, type,
13561                                 TREE_OPERAND (tem, 0), arg1);
13562         }
13563
13564       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13565          is probably obsolete because the first operand should be a
13566          truth value (that's why we have the two cases above), but let's
13567          leave it in until we can confirm this for all front-ends.  */
13568       if (integer_zerop (op2)
13569           && TREE_CODE (arg0) == NE_EXPR
13570           && integer_zerop (TREE_OPERAND (arg0, 1))
13571           && integer_pow2p (arg1)
13572           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13573           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13574                               arg1, OEP_ONLY_CONST))
13575         return pedantic_non_lvalue_loc (loc,
13576                                     fold_convert_loc (loc, type,
13577                                                       TREE_OPERAND (arg0, 0)));
13578
13579       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13580       if (integer_zerop (op2)
13581           && truth_value_p (TREE_CODE (arg0))
13582           && truth_value_p (TREE_CODE (arg1)))
13583         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13584                             fold_convert_loc (loc, type, arg0),
13585                             arg1);
13586
13587       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13588       if (integer_onep (op2)
13589           && truth_value_p (TREE_CODE (arg0))
13590           && truth_value_p (TREE_CODE (arg1)))
13591         {
13592           location_t loc0 = expr_location_or (arg0, loc);
13593           /* Only perform transformation if ARG0 is easily inverted.  */
13594           tem = fold_truth_not_expr (loc0, arg0);
13595           if (tem)
13596             return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13597                                 fold_convert_loc (loc, type, tem),
13598                                 arg1);
13599         }
13600
13601       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13602       if (integer_zerop (arg1)
13603           && truth_value_p (TREE_CODE (arg0))
13604           && truth_value_p (TREE_CODE (op2)))
13605         {
13606           location_t loc0 = expr_location_or (arg0, loc);
13607           /* Only perform transformation if ARG0 is easily inverted.  */
13608           tem = fold_truth_not_expr (loc0, arg0);
13609           if (tem)
13610             return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13611                                 fold_convert_loc (loc, type, tem),
13612                                 op2);
13613         }
13614
13615       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13616       if (integer_onep (arg1)
13617           && truth_value_p (TREE_CODE (arg0))
13618           && truth_value_p (TREE_CODE (op2)))
13619         return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13620                             fold_convert_loc (loc, type, arg0),
13621                             op2);
13622
13623       return NULL_TREE;
13624
13625     case CALL_EXPR:
13626       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
13627          of fold_ternary on them.  */
13628       gcc_unreachable ();
13629
13630     case BIT_FIELD_REF:
13631       if ((TREE_CODE (arg0) == VECTOR_CST
13632            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13633           && type == TREE_TYPE (TREE_TYPE (arg0)))
13634         {
13635           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13636           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13637
13638           if (width != 0
13639               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13640               && (idx % width) == 0
13641               && (idx = idx / width)
13642                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13643             {
13644               tree elements = NULL_TREE;
13645
13646               if (TREE_CODE (arg0) == VECTOR_CST)
13647                 elements = TREE_VECTOR_CST_ELTS (arg0);
13648               else
13649                 {
13650                   unsigned HOST_WIDE_INT idx;
13651                   tree value;
13652
13653                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13654                     elements = tree_cons (NULL_TREE, value, elements);
13655                 }
13656               while (idx-- > 0 && elements)
13657                 elements = TREE_CHAIN (elements);
13658               if (elements)
13659                 return TREE_VALUE (elements);
13660               else
13661                 return build_zero_cst (type);
13662             }
13663         }
13664
13665       /* A bit-field-ref that referenced the full argument can be stripped.  */
13666       if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13667           && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13668           && integer_zerop (op2))
13669         return fold_convert_loc (loc, type, arg0);
13670
13671       return NULL_TREE;
13672
13673     case FMA_EXPR:
13674       /* For integers we can decompose the FMA if possible.  */
13675       if (TREE_CODE (arg0) == INTEGER_CST
13676           && TREE_CODE (arg1) == INTEGER_CST)
13677         return fold_build2_loc (loc, PLUS_EXPR, type,
13678                                 const_binop (MULT_EXPR, arg0, arg1), arg2);
13679       if (integer_zerop (arg2))
13680         return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
13681
13682       return fold_fma (loc, type, arg0, arg1, arg2);
13683
13684     default:
13685       return NULL_TREE;
13686     } /* switch (code) */
13687 }
13688
13689 /* Perform constant folding and related simplification of EXPR.
13690    The related simplifications include x*1 => x, x*0 => 0, etc.,
13691    and application of the associative law.
13692    NOP_EXPR conversions may be removed freely (as long as we
13693    are careful not to change the type of the overall expression).
13694    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13695    but we can constant-fold them if they have constant operands.  */
13696
13697 #ifdef ENABLE_FOLD_CHECKING
13698 # define fold(x) fold_1 (x)
13699 static tree fold_1 (tree);
13700 static
13701 #endif
13702 tree
13703 fold (tree expr)
13704 {
13705   const tree t = expr;
13706   enum tree_code code = TREE_CODE (t);
13707   enum tree_code_class kind = TREE_CODE_CLASS (code);
13708   tree tem;
13709   location_t loc = EXPR_LOCATION (expr);
13710
13711   /* Return right away if a constant.  */
13712   if (kind == tcc_constant)
13713     return t;
13714
13715   /* CALL_EXPR-like objects with variable numbers of operands are
13716      treated specially.  */
13717   if (kind == tcc_vl_exp)
13718     {
13719       if (code == CALL_EXPR)
13720         {
13721           tem = fold_call_expr (loc, expr, false);
13722           return tem ? tem : expr;
13723         }
13724       return expr;
13725     }
13726
13727   if (IS_EXPR_CODE_CLASS (kind))
13728     {
13729       tree type = TREE_TYPE (t);
13730       tree op0, op1, op2;
13731
13732       switch (TREE_CODE_LENGTH (code))
13733         {
13734         case 1:
13735           op0 = TREE_OPERAND (t, 0);
13736           tem = fold_unary_loc (loc, code, type, op0);
13737           return tem ? tem : expr;
13738         case 2:
13739           op0 = TREE_OPERAND (t, 0);
13740           op1 = TREE_OPERAND (t, 1);
13741           tem = fold_binary_loc (loc, code, type, op0, op1);
13742           return tem ? tem : expr;
13743         case 3:
13744           op0 = TREE_OPERAND (t, 0);
13745           op1 = TREE_OPERAND (t, 1);
13746           op2 = TREE_OPERAND (t, 2);
13747           tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13748           return tem ? tem : expr;
13749         default:
13750           break;
13751         }
13752     }
13753
13754   switch (code)
13755     {
13756     case ARRAY_REF:
13757       {
13758         tree op0 = TREE_OPERAND (t, 0);
13759         tree op1 = TREE_OPERAND (t, 1);
13760
13761         if (TREE_CODE (op1) == INTEGER_CST
13762             && TREE_CODE (op0) == CONSTRUCTOR
13763             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13764           {
13765             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13766             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13767             unsigned HOST_WIDE_INT begin = 0;
13768
13769             /* Find a matching index by means of a binary search.  */
13770             while (begin != end)
13771               {
13772                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13773                 tree index = VEC_index (constructor_elt, elts, middle)->index;
13774
13775                 if (TREE_CODE (index) == INTEGER_CST
13776                     && tree_int_cst_lt (index, op1))
13777                   begin = middle + 1;
13778                 else if (TREE_CODE (index) == INTEGER_CST
13779                          && tree_int_cst_lt (op1, index))
13780                   end = middle;
13781                 else if (TREE_CODE (index) == RANGE_EXPR
13782                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13783                   begin = middle + 1;
13784                 else if (TREE_CODE (index) == RANGE_EXPR
13785                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13786                   end = middle;
13787                 else
13788                   return VEC_index (constructor_elt, elts, middle)->value;
13789               }
13790           }
13791
13792         return t;
13793       }
13794
13795     case CONST_DECL:
13796       return fold (DECL_INITIAL (t));
13797
13798     default:
13799       return t;
13800     } /* switch (code) */
13801 }
13802
13803 #ifdef ENABLE_FOLD_CHECKING
13804 #undef fold
13805
13806 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13807 static void fold_check_failed (const_tree, const_tree);
13808 void print_fold_checksum (const_tree);
13809
13810 /* When --enable-checking=fold, compute a digest of expr before
13811    and after actual fold call to see if fold did not accidentally
13812    change original expr.  */
13813
13814 tree
13815 fold (tree expr)
13816 {
13817   tree ret;
13818   struct md5_ctx ctx;
13819   unsigned char checksum_before[16], checksum_after[16];
13820   htab_t ht;
13821
13822   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13823   md5_init_ctx (&ctx);
13824   fold_checksum_tree (expr, &ctx, ht);
13825   md5_finish_ctx (&ctx, checksum_before);
13826   htab_empty (ht);
13827
13828   ret = fold_1 (expr);
13829
13830   md5_init_ctx (&ctx);
13831   fold_checksum_tree (expr, &ctx, ht);
13832   md5_finish_ctx (&ctx, checksum_after);
13833   htab_delete (ht);
13834
13835   if (memcmp (checksum_before, checksum_after, 16))
13836     fold_check_failed (expr, ret);
13837
13838   return ret;
13839 }
13840
13841 void
13842 print_fold_checksum (const_tree expr)
13843 {
13844   struct md5_ctx ctx;
13845   unsigned char checksum[16], cnt;
13846   htab_t ht;
13847
13848   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13849   md5_init_ctx (&ctx);
13850   fold_checksum_tree (expr, &ctx, ht);
13851   md5_finish_ctx (&ctx, checksum);
13852   htab_delete (ht);
13853   for (cnt = 0; cnt < 16; ++cnt)
13854     fprintf (stderr, "%02x", checksum[cnt]);
13855   putc ('\n', stderr);
13856 }
13857
13858 static void
13859 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13860 {
13861   internal_error ("fold check: original tree changed by fold");
13862 }
13863
13864 static void
13865 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13866 {
13867   void **slot;
13868   enum tree_code code;
13869   union tree_node buf;
13870   int i, len;
13871
13872 recursive_label:
13873
13874   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13875                <= sizeof (struct tree_function_decl))
13876               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13877   if (expr == NULL)
13878     return;
13879   slot = (void **) htab_find_slot (ht, expr, INSERT);
13880   if (*slot != NULL)
13881     return;
13882   *slot = CONST_CAST_TREE (expr);
13883   code = TREE_CODE (expr);
13884   if (TREE_CODE_CLASS (code) == tcc_declaration
13885       && DECL_ASSEMBLER_NAME_SET_P (expr))
13886     {
13887       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
13888       memcpy ((char *) &buf, expr, tree_size (expr));
13889       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13890       expr = (tree) &buf;
13891     }
13892   else if (TREE_CODE_CLASS (code) == tcc_type
13893            && (TYPE_POINTER_TO (expr)
13894                || TYPE_REFERENCE_TO (expr)
13895                || TYPE_CACHED_VALUES_P (expr)
13896                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13897                || TYPE_NEXT_VARIANT (expr)))
13898     {
13899       /* Allow these fields to be modified.  */
13900       tree tmp;
13901       memcpy ((char *) &buf, expr, tree_size (expr));
13902       expr = tmp = (tree) &buf;
13903       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13904       TYPE_POINTER_TO (tmp) = NULL;
13905       TYPE_REFERENCE_TO (tmp) = NULL;
13906       TYPE_NEXT_VARIANT (tmp) = NULL;
13907       if (TYPE_CACHED_VALUES_P (tmp))
13908         {
13909           TYPE_CACHED_VALUES_P (tmp) = 0;
13910           TYPE_CACHED_VALUES (tmp) = NULL;
13911         }
13912     }
13913   md5_process_bytes (expr, tree_size (expr), ctx);
13914   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13915   if (TREE_CODE_CLASS (code) != tcc_type
13916       && TREE_CODE_CLASS (code) != tcc_declaration
13917       && code != TREE_LIST
13918       && code != SSA_NAME
13919       && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13920     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13921   switch (TREE_CODE_CLASS (code))
13922     {
13923     case tcc_constant:
13924       switch (code)
13925         {
13926         case STRING_CST:
13927           md5_process_bytes (TREE_STRING_POINTER (expr),
13928                              TREE_STRING_LENGTH (expr), ctx);
13929           break;
13930         case COMPLEX_CST:
13931           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13932           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13933           break;
13934         case VECTOR_CST:
13935           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13936           break;
13937         default:
13938           break;
13939         }
13940       break;
13941     case tcc_exceptional:
13942       switch (code)
13943         {
13944         case TREE_LIST:
13945           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13946           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13947           expr = TREE_CHAIN (expr);
13948           goto recursive_label;
13949           break;
13950         case TREE_VEC:
13951           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13952             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13953           break;
13954         default:
13955           break;
13956         }
13957       break;
13958     case tcc_expression:
13959     case tcc_reference:
13960     case tcc_comparison:
13961     case tcc_unary:
13962     case tcc_binary:
13963     case tcc_statement:
13964     case tcc_vl_exp:
13965       len = TREE_OPERAND_LENGTH (expr);
13966       for (i = 0; i < len; ++i)
13967         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13968       break;
13969     case tcc_declaration:
13970       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13971       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13972       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13973         {
13974           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13975           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13976           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13977           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13978           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13979         }
13980       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13981         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13982
13983       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13984         {
13985           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13986           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13987           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13988         }
13989       break;
13990     case tcc_type:
13991       if (TREE_CODE (expr) == ENUMERAL_TYPE)
13992         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13993       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13994       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13995       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13996       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13997       if (INTEGRAL_TYPE_P (expr)
13998           || SCALAR_FLOAT_TYPE_P (expr))
13999         {
14000           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
14001           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
14002         }
14003       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
14004       if (TREE_CODE (expr) == RECORD_TYPE
14005           || TREE_CODE (expr) == UNION_TYPE
14006           || TREE_CODE (expr) == QUAL_UNION_TYPE)
14007         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
14008       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
14009       break;
14010     default:
14011       break;
14012     }
14013 }
14014
14015 /* Helper function for outputting the checksum of a tree T.  When
14016    debugging with gdb, you can "define mynext" to be "next" followed
14017    by "call debug_fold_checksum (op0)", then just trace down till the
14018    outputs differ.  */
14019
14020 DEBUG_FUNCTION void
14021 debug_fold_checksum (const_tree t)
14022 {
14023   int i;
14024   unsigned char checksum[16];
14025   struct md5_ctx ctx;
14026   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14027
14028   md5_init_ctx (&ctx);
14029   fold_checksum_tree (t, &ctx, ht);
14030   md5_finish_ctx (&ctx, checksum);
14031   htab_empty (ht);
14032
14033   for (i = 0; i < 16; i++)
14034     fprintf (stderr, "%d ", checksum[i]);
14035
14036   fprintf (stderr, "\n");
14037 }
14038
14039 #endif
14040
14041 /* Fold a unary tree expression with code CODE of type TYPE with an
14042    operand OP0.  LOC is the location of the resulting expression.
14043    Return a folded expression if successful.  Otherwise, return a tree
14044    expression with code CODE of type TYPE with an operand OP0.  */
14045
14046 tree
14047 fold_build1_stat_loc (location_t loc,
14048                       enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
14049 {
14050   tree tem;
14051 #ifdef ENABLE_FOLD_CHECKING
14052   unsigned char checksum_before[16], checksum_after[16];
14053   struct md5_ctx ctx;
14054   htab_t ht;
14055
14056   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14057   md5_init_ctx (&ctx);
14058   fold_checksum_tree (op0, &ctx, ht);
14059   md5_finish_ctx (&ctx, checksum_before);
14060   htab_empty (ht);
14061 #endif
14062
14063   tem = fold_unary_loc (loc, code, type, op0);
14064   if (!tem)
14065     tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
14066
14067 #ifdef ENABLE_FOLD_CHECKING
14068   md5_init_ctx (&ctx);
14069   fold_checksum_tree (op0, &ctx, ht);
14070   md5_finish_ctx (&ctx, checksum_after);
14071   htab_delete (ht);
14072
14073   if (memcmp (checksum_before, checksum_after, 16))
14074     fold_check_failed (op0, tem);
14075 #endif
14076   return tem;
14077 }
14078
14079 /* Fold a binary tree expression with code CODE of type TYPE with
14080    operands OP0 and OP1.  LOC is the location of the resulting
14081    expression.  Return a folded expression if successful.  Otherwise,
14082    return a tree expression with code CODE of type TYPE with operands
14083    OP0 and OP1.  */
14084
14085 tree
14086 fold_build2_stat_loc (location_t loc,
14087                       enum tree_code code, tree type, tree op0, tree op1
14088                       MEM_STAT_DECL)
14089 {
14090   tree tem;
14091 #ifdef ENABLE_FOLD_CHECKING
14092   unsigned char checksum_before_op0[16],
14093                 checksum_before_op1[16],
14094                 checksum_after_op0[16],
14095                 checksum_after_op1[16];
14096   struct md5_ctx ctx;
14097   htab_t ht;
14098
14099   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14100   md5_init_ctx (&ctx);
14101   fold_checksum_tree (op0, &ctx, ht);
14102   md5_finish_ctx (&ctx, checksum_before_op0);
14103   htab_empty (ht);
14104
14105   md5_init_ctx (&ctx);
14106   fold_checksum_tree (op1, &ctx, ht);
14107   md5_finish_ctx (&ctx, checksum_before_op1);
14108   htab_empty (ht);
14109 #endif
14110
14111   tem = fold_binary_loc (loc, code, type, op0, op1);
14112   if (!tem)
14113     tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
14114
14115 #ifdef ENABLE_FOLD_CHECKING
14116   md5_init_ctx (&ctx);
14117   fold_checksum_tree (op0, &ctx, ht);
14118   md5_finish_ctx (&ctx, checksum_after_op0);
14119   htab_empty (ht);
14120
14121   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14122     fold_check_failed (op0, tem);
14123
14124   md5_init_ctx (&ctx);
14125   fold_checksum_tree (op1, &ctx, ht);
14126   md5_finish_ctx (&ctx, checksum_after_op1);
14127   htab_delete (ht);
14128
14129   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14130     fold_check_failed (op1, tem);
14131 #endif
14132   return tem;
14133 }
14134
14135 /* Fold a ternary tree expression with code CODE of type TYPE with
14136    operands OP0, OP1, and OP2.  Return a folded expression if
14137    successful.  Otherwise, return a tree expression with code CODE of
14138    type TYPE with operands OP0, OP1, and OP2.  */
14139
14140 tree
14141 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14142                       tree op0, tree op1, tree op2 MEM_STAT_DECL)
14143 {
14144   tree tem;
14145 #ifdef ENABLE_FOLD_CHECKING
14146   unsigned char checksum_before_op0[16],
14147                 checksum_before_op1[16],
14148                 checksum_before_op2[16],
14149                 checksum_after_op0[16],
14150                 checksum_after_op1[16],
14151                 checksum_after_op2[16];
14152   struct md5_ctx ctx;
14153   htab_t ht;
14154
14155   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14156   md5_init_ctx (&ctx);
14157   fold_checksum_tree (op0, &ctx, ht);
14158   md5_finish_ctx (&ctx, checksum_before_op0);
14159   htab_empty (ht);
14160
14161   md5_init_ctx (&ctx);
14162   fold_checksum_tree (op1, &ctx, ht);
14163   md5_finish_ctx (&ctx, checksum_before_op1);
14164   htab_empty (ht);
14165
14166   md5_init_ctx (&ctx);
14167   fold_checksum_tree (op2, &ctx, ht);
14168   md5_finish_ctx (&ctx, checksum_before_op2);
14169   htab_empty (ht);
14170 #endif
14171
14172   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14173   tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14174   if (!tem)
14175     tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
14176
14177 #ifdef ENABLE_FOLD_CHECKING
14178   md5_init_ctx (&ctx);
14179   fold_checksum_tree (op0, &ctx, ht);
14180   md5_finish_ctx (&ctx, checksum_after_op0);
14181   htab_empty (ht);
14182
14183   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14184     fold_check_failed (op0, tem);
14185
14186   md5_init_ctx (&ctx);
14187   fold_checksum_tree (op1, &ctx, ht);
14188   md5_finish_ctx (&ctx, checksum_after_op1);
14189   htab_empty (ht);
14190
14191   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14192     fold_check_failed (op1, tem);
14193
14194   md5_init_ctx (&ctx);
14195   fold_checksum_tree (op2, &ctx, ht);
14196   md5_finish_ctx (&ctx, checksum_after_op2);
14197   htab_delete (ht);
14198
14199   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14200     fold_check_failed (op2, tem);
14201 #endif
14202   return tem;
14203 }
14204
14205 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14206    arguments in ARGARRAY, and a null static chain.
14207    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
14208    of type TYPE from the given operands as constructed by build_call_array.  */
14209
14210 tree
14211 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14212                            int nargs, tree *argarray)
14213 {
14214   tree tem;
14215 #ifdef ENABLE_FOLD_CHECKING
14216   unsigned char checksum_before_fn[16],
14217                 checksum_before_arglist[16],
14218                 checksum_after_fn[16],
14219                 checksum_after_arglist[16];
14220   struct md5_ctx ctx;
14221   htab_t ht;
14222   int i;
14223
14224   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14225   md5_init_ctx (&ctx);
14226   fold_checksum_tree (fn, &ctx, ht);
14227   md5_finish_ctx (&ctx, checksum_before_fn);
14228   htab_empty (ht);
14229
14230   md5_init_ctx (&ctx);
14231   for (i = 0; i < nargs; i++)
14232     fold_checksum_tree (argarray[i], &ctx, ht);
14233   md5_finish_ctx (&ctx, checksum_before_arglist);
14234   htab_empty (ht);
14235 #endif
14236
14237   tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14238
14239 #ifdef ENABLE_FOLD_CHECKING
14240   md5_init_ctx (&ctx);
14241   fold_checksum_tree (fn, &ctx, ht);
14242   md5_finish_ctx (&ctx, checksum_after_fn);
14243   htab_empty (ht);
14244
14245   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14246     fold_check_failed (fn, tem);
14247
14248   md5_init_ctx (&ctx);
14249   for (i = 0; i < nargs; i++)
14250     fold_checksum_tree (argarray[i], &ctx, ht);
14251   md5_finish_ctx (&ctx, checksum_after_arglist);
14252   htab_delete (ht);
14253
14254   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14255     fold_check_failed (NULL_TREE, tem);
14256 #endif
14257   return tem;
14258 }
14259
14260 /* Perform constant folding and related simplification of initializer
14261    expression EXPR.  These behave identically to "fold_buildN" but ignore
14262    potential run-time traps and exceptions that fold must preserve.  */
14263
14264 #define START_FOLD_INIT \
14265   int saved_signaling_nans = flag_signaling_nans;\
14266   int saved_trapping_math = flag_trapping_math;\
14267   int saved_rounding_math = flag_rounding_math;\
14268   int saved_trapv = flag_trapv;\
14269   int saved_folding_initializer = folding_initializer;\
14270   flag_signaling_nans = 0;\
14271   flag_trapping_math = 0;\
14272   flag_rounding_math = 0;\
14273   flag_trapv = 0;\
14274   folding_initializer = 1;
14275
14276 #define END_FOLD_INIT \
14277   flag_signaling_nans = saved_signaling_nans;\
14278   flag_trapping_math = saved_trapping_math;\
14279   flag_rounding_math = saved_rounding_math;\
14280   flag_trapv = saved_trapv;\
14281   folding_initializer = saved_folding_initializer;
14282
14283 tree
14284 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14285                              tree type, tree op)
14286 {
14287   tree result;
14288   START_FOLD_INIT;
14289
14290   result = fold_build1_loc (loc, code, type, op);
14291
14292   END_FOLD_INIT;
14293   return result;
14294 }
14295
14296 tree
14297 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14298                              tree type, tree op0, tree op1)
14299 {
14300   tree result;
14301   START_FOLD_INIT;
14302
14303   result = fold_build2_loc (loc, code, type, op0, op1);
14304
14305   END_FOLD_INIT;
14306   return result;
14307 }
14308
14309 tree
14310 fold_build3_initializer_loc (location_t loc, enum tree_code code,
14311                              tree type, tree op0, tree op1, tree op2)
14312 {
14313   tree result;
14314   START_FOLD_INIT;
14315
14316   result = fold_build3_loc (loc, code, type, op0, op1, op2);
14317
14318   END_FOLD_INIT;
14319   return result;
14320 }
14321
14322 tree
14323 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14324                                        int nargs, tree *argarray)
14325 {
14326   tree result;
14327   START_FOLD_INIT;
14328
14329   result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14330
14331   END_FOLD_INIT;
14332   return result;
14333 }
14334
14335 #undef START_FOLD_INIT
14336 #undef END_FOLD_INIT
14337
14338 /* Determine if first argument is a multiple of second argument.  Return 0 if
14339    it is not, or we cannot easily determined it to be.
14340
14341    An example of the sort of thing we care about (at this point; this routine
14342    could surely be made more general, and expanded to do what the *_DIV_EXPR's
14343    fold cases do now) is discovering that
14344
14345      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14346
14347    is a multiple of
14348
14349      SAVE_EXPR (J * 8)
14350
14351    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14352
14353    This code also handles discovering that
14354
14355      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14356
14357    is a multiple of 8 so we don't have to worry about dealing with a
14358    possible remainder.
14359
14360    Note that we *look* inside a SAVE_EXPR only to determine how it was
14361    calculated; it is not safe for fold to do much of anything else with the
14362    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14363    at run time.  For example, the latter example above *cannot* be implemented
14364    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14365    evaluation time of the original SAVE_EXPR is not necessarily the same at
14366    the time the new expression is evaluated.  The only optimization of this
14367    sort that would be valid is changing
14368
14369      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14370
14371    divided by 8 to
14372
14373      SAVE_EXPR (I) * SAVE_EXPR (J)
14374
14375    (where the same SAVE_EXPR (J) is used in the original and the
14376    transformed version).  */
14377
14378 int
14379 multiple_of_p (tree type, const_tree top, const_tree bottom)
14380 {
14381   if (operand_equal_p (top, bottom, 0))
14382     return 1;
14383
14384   if (TREE_CODE (type) != INTEGER_TYPE)
14385     return 0;
14386
14387   switch (TREE_CODE (top))
14388     {
14389     case BIT_AND_EXPR:
14390       /* Bitwise and provides a power of two multiple.  If the mask is
14391          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
14392       if (!integer_pow2p (bottom))
14393         return 0;
14394       /* FALLTHRU */
14395
14396     case MULT_EXPR:
14397       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14398               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14399
14400     case PLUS_EXPR:
14401     case MINUS_EXPR:
14402       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14403               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14404
14405     case LSHIFT_EXPR:
14406       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14407         {
14408           tree op1, t1;
14409
14410           op1 = TREE_OPERAND (top, 1);
14411           /* const_binop may not detect overflow correctly,
14412              so check for it explicitly here.  */
14413           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14414               > TREE_INT_CST_LOW (op1)
14415               && TREE_INT_CST_HIGH (op1) == 0
14416               && 0 != (t1 = fold_convert (type,
14417                                           const_binop (LSHIFT_EXPR,
14418                                                        size_one_node,
14419                                                        op1)))
14420               && !TREE_OVERFLOW (t1))
14421             return multiple_of_p (type, t1, bottom);
14422         }
14423       return 0;
14424
14425     case NOP_EXPR:
14426       /* Can't handle conversions from non-integral or wider integral type.  */
14427       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14428           || (TYPE_PRECISION (type)
14429               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14430         return 0;
14431
14432       /* .. fall through ...  */
14433
14434     case SAVE_EXPR:
14435       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14436
14437     case COND_EXPR:
14438       return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
14439               && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
14440
14441     case INTEGER_CST:
14442       if (TREE_CODE (bottom) != INTEGER_CST
14443           || integer_zerop (bottom)
14444           || (TYPE_UNSIGNED (type)
14445               && (tree_int_cst_sgn (top) < 0
14446                   || tree_int_cst_sgn (bottom) < 0)))
14447         return 0;
14448       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14449                                              top, bottom));
14450
14451     default:
14452       return 0;
14453     }
14454 }
14455
14456 /* Return true if CODE or TYPE is known to be non-negative. */
14457
14458 static bool
14459 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14460 {
14461   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14462       && truth_value_p (code))
14463     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14464        have a signed:1 type (where the value is -1 and 0).  */
14465     return true;
14466   return false;
14467 }
14468
14469 /* Return true if (CODE OP0) is known to be non-negative.  If the return
14470    value is based on the assumption that signed overflow is undefined,
14471    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14472    *STRICT_OVERFLOW_P.  */
14473
14474 bool
14475 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14476                                 bool *strict_overflow_p)
14477 {
14478   if (TYPE_UNSIGNED (type))
14479     return true;
14480
14481   switch (code)
14482     {
14483     case ABS_EXPR:
14484       /* We can't return 1 if flag_wrapv is set because
14485          ABS_EXPR<INT_MIN> = INT_MIN.  */
14486       if (!INTEGRAL_TYPE_P (type))
14487         return true;
14488       if (TYPE_OVERFLOW_UNDEFINED (type))
14489         {
14490           *strict_overflow_p = true;
14491           return true;
14492         }
14493       break;
14494
14495     case NON_LVALUE_EXPR:
14496     case FLOAT_EXPR:
14497     case FIX_TRUNC_EXPR:
14498       return tree_expr_nonnegative_warnv_p (op0,
14499                                             strict_overflow_p);
14500
14501     case NOP_EXPR:
14502       {
14503         tree inner_type = TREE_TYPE (op0);
14504         tree outer_type = type;
14505
14506         if (TREE_CODE (outer_type) == REAL_TYPE)
14507           {
14508             if (TREE_CODE (inner_type) == REAL_TYPE)
14509               return tree_expr_nonnegative_warnv_p (op0,
14510                                                     strict_overflow_p);
14511             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14512               {
14513                 if (TYPE_UNSIGNED (inner_type))
14514                   return true;
14515                 return tree_expr_nonnegative_warnv_p (op0,
14516                                                       strict_overflow_p);
14517               }
14518           }
14519         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14520           {
14521             if (TREE_CODE (inner_type) == REAL_TYPE)
14522               return tree_expr_nonnegative_warnv_p (op0,
14523                                                     strict_overflow_p);
14524             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14525               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14526                       && TYPE_UNSIGNED (inner_type);
14527           }
14528       }
14529       break;
14530
14531     default:
14532       return tree_simple_nonnegative_warnv_p (code, type);
14533     }
14534
14535   /* We don't know sign of `t', so be conservative and return false.  */
14536   return false;
14537 }
14538
14539 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
14540    value is based on the assumption that signed overflow is undefined,
14541    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14542    *STRICT_OVERFLOW_P.  */
14543
14544 bool
14545 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14546                                       tree op1, bool *strict_overflow_p)
14547 {
14548   if (TYPE_UNSIGNED (type))
14549     return true;
14550
14551   switch (code)
14552     {
14553     case POINTER_PLUS_EXPR:
14554     case PLUS_EXPR:
14555       if (FLOAT_TYPE_P (type))
14556         return (tree_expr_nonnegative_warnv_p (op0,
14557                                                strict_overflow_p)
14558                 && tree_expr_nonnegative_warnv_p (op1,
14559                                                   strict_overflow_p));
14560
14561       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14562          both unsigned and at least 2 bits shorter than the result.  */
14563       if (TREE_CODE (type) == INTEGER_TYPE
14564           && TREE_CODE (op0) == NOP_EXPR
14565           && TREE_CODE (op1) == NOP_EXPR)
14566         {
14567           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14568           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14569           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14570               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14571             {
14572               unsigned int prec = MAX (TYPE_PRECISION (inner1),
14573                                        TYPE_PRECISION (inner2)) + 1;
14574               return prec < TYPE_PRECISION (type);
14575             }
14576         }
14577       break;
14578
14579     case MULT_EXPR:
14580       if (FLOAT_TYPE_P (type))
14581         {
14582           /* x * x for floating point x is always non-negative.  */
14583           if (operand_equal_p (op0, op1, 0))
14584             return true;
14585           return (tree_expr_nonnegative_warnv_p (op0,
14586                                                  strict_overflow_p)
14587                   && tree_expr_nonnegative_warnv_p (op1,
14588                                                     strict_overflow_p));
14589         }
14590
14591       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14592          both unsigned and their total bits is shorter than the result.  */
14593       if (TREE_CODE (type) == INTEGER_TYPE
14594           && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14595           && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14596         {
14597           tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14598             ? TREE_TYPE (TREE_OPERAND (op0, 0))
14599             : TREE_TYPE (op0);
14600           tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14601             ? TREE_TYPE (TREE_OPERAND (op1, 0))
14602             : TREE_TYPE (op1);
14603
14604           bool unsigned0 = TYPE_UNSIGNED (inner0);
14605           bool unsigned1 = TYPE_UNSIGNED (inner1);
14606
14607           if (TREE_CODE (op0) == INTEGER_CST)
14608             unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14609
14610           if (TREE_CODE (op1) == INTEGER_CST)
14611             unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14612
14613           if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14614               && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14615             {
14616               unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14617                 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14618                 : TYPE_PRECISION (inner0);
14619
14620               unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14621                 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14622                 : TYPE_PRECISION (inner1);
14623
14624               return precision0 + precision1 < TYPE_PRECISION (type);
14625             }
14626         }
14627       return false;
14628
14629     case BIT_AND_EXPR:
14630     case MAX_EXPR:
14631       return (tree_expr_nonnegative_warnv_p (op0,
14632                                              strict_overflow_p)
14633               || tree_expr_nonnegative_warnv_p (op1,
14634                                                 strict_overflow_p));
14635
14636     case BIT_IOR_EXPR:
14637     case BIT_XOR_EXPR:
14638     case MIN_EXPR:
14639     case RDIV_EXPR:
14640     case TRUNC_DIV_EXPR:
14641     case CEIL_DIV_EXPR:
14642     case FLOOR_DIV_EXPR:
14643     case ROUND_DIV_EXPR:
14644       return (tree_expr_nonnegative_warnv_p (op0,
14645                                              strict_overflow_p)
14646               && tree_expr_nonnegative_warnv_p (op1,
14647                                                 strict_overflow_p));
14648
14649     case TRUNC_MOD_EXPR:
14650     case CEIL_MOD_EXPR:
14651     case FLOOR_MOD_EXPR:
14652     case ROUND_MOD_EXPR:
14653       return tree_expr_nonnegative_warnv_p (op0,
14654                                             strict_overflow_p);
14655     default:
14656       return tree_simple_nonnegative_warnv_p (code, type);
14657     }
14658
14659   /* We don't know sign of `t', so be conservative and return false.  */
14660   return false;
14661 }
14662
14663 /* Return true if T is known to be non-negative.  If the return
14664    value is based on the assumption that signed overflow is undefined,
14665    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14666    *STRICT_OVERFLOW_P.  */
14667
14668 bool
14669 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14670 {
14671   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14672     return true;
14673
14674   switch (TREE_CODE (t))
14675     {
14676     case INTEGER_CST:
14677       return tree_int_cst_sgn (t) >= 0;
14678
14679     case REAL_CST:
14680       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14681
14682     case FIXED_CST:
14683       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14684
14685     case COND_EXPR:
14686       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14687                                              strict_overflow_p)
14688               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14689                                                 strict_overflow_p));
14690     default:
14691       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14692                                                    TREE_TYPE (t));
14693     }
14694   /* We don't know sign of `t', so be conservative and return false.  */
14695   return false;
14696 }
14697
14698 /* Return true if T is known to be non-negative.  If the return
14699    value is based on the assumption that signed overflow is undefined,
14700    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14701    *STRICT_OVERFLOW_P.  */
14702
14703 bool
14704 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14705                                tree arg0, tree arg1, bool *strict_overflow_p)
14706 {
14707   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14708     switch (DECL_FUNCTION_CODE (fndecl))
14709       {
14710         CASE_FLT_FN (BUILT_IN_ACOS):
14711         CASE_FLT_FN (BUILT_IN_ACOSH):
14712         CASE_FLT_FN (BUILT_IN_CABS):
14713         CASE_FLT_FN (BUILT_IN_COSH):
14714         CASE_FLT_FN (BUILT_IN_ERFC):
14715         CASE_FLT_FN (BUILT_IN_EXP):
14716         CASE_FLT_FN (BUILT_IN_EXP10):
14717         CASE_FLT_FN (BUILT_IN_EXP2):
14718         CASE_FLT_FN (BUILT_IN_FABS):
14719         CASE_FLT_FN (BUILT_IN_FDIM):
14720         CASE_FLT_FN (BUILT_IN_HYPOT):
14721         CASE_FLT_FN (BUILT_IN_POW10):
14722         CASE_INT_FN (BUILT_IN_FFS):
14723         CASE_INT_FN (BUILT_IN_PARITY):
14724         CASE_INT_FN (BUILT_IN_POPCOUNT):
14725       case BUILT_IN_BSWAP32:
14726       case BUILT_IN_BSWAP64:
14727         /* Always true.  */
14728         return true;
14729
14730         CASE_FLT_FN (BUILT_IN_SQRT):
14731         /* sqrt(-0.0) is -0.0.  */
14732         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14733           return true;
14734         return tree_expr_nonnegative_warnv_p (arg0,
14735                                               strict_overflow_p);
14736
14737         CASE_FLT_FN (BUILT_IN_ASINH):
14738         CASE_FLT_FN (BUILT_IN_ATAN):
14739         CASE_FLT_FN (BUILT_IN_ATANH):
14740         CASE_FLT_FN (BUILT_IN_CBRT):
14741         CASE_FLT_FN (BUILT_IN_CEIL):
14742         CASE_FLT_FN (BUILT_IN_ERF):
14743         CASE_FLT_FN (BUILT_IN_EXPM1):
14744         CASE_FLT_FN (BUILT_IN_FLOOR):
14745         CASE_FLT_FN (BUILT_IN_FMOD):
14746         CASE_FLT_FN (BUILT_IN_FREXP):
14747         CASE_FLT_FN (BUILT_IN_ICEIL):
14748         CASE_FLT_FN (BUILT_IN_IFLOOR):
14749         CASE_FLT_FN (BUILT_IN_IRINT):
14750         CASE_FLT_FN (BUILT_IN_IROUND):
14751         CASE_FLT_FN (BUILT_IN_LCEIL):
14752         CASE_FLT_FN (BUILT_IN_LDEXP):
14753         CASE_FLT_FN (BUILT_IN_LFLOOR):
14754         CASE_FLT_FN (BUILT_IN_LLCEIL):
14755         CASE_FLT_FN (BUILT_IN_LLFLOOR):
14756         CASE_FLT_FN (BUILT_IN_LLRINT):
14757         CASE_FLT_FN (BUILT_IN_LLROUND):
14758         CASE_FLT_FN (BUILT_IN_LRINT):
14759         CASE_FLT_FN (BUILT_IN_LROUND):
14760         CASE_FLT_FN (BUILT_IN_MODF):
14761         CASE_FLT_FN (BUILT_IN_NEARBYINT):
14762         CASE_FLT_FN (BUILT_IN_RINT):
14763         CASE_FLT_FN (BUILT_IN_ROUND):
14764         CASE_FLT_FN (BUILT_IN_SCALB):
14765         CASE_FLT_FN (BUILT_IN_SCALBLN):
14766         CASE_FLT_FN (BUILT_IN_SCALBN):
14767         CASE_FLT_FN (BUILT_IN_SIGNBIT):
14768         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14769         CASE_FLT_FN (BUILT_IN_SINH):
14770         CASE_FLT_FN (BUILT_IN_TANH):
14771         CASE_FLT_FN (BUILT_IN_TRUNC):
14772         /* True if the 1st argument is nonnegative.  */
14773         return tree_expr_nonnegative_warnv_p (arg0,
14774                                               strict_overflow_p);
14775
14776         CASE_FLT_FN (BUILT_IN_FMAX):
14777         /* True if the 1st OR 2nd arguments are nonnegative.  */
14778         return (tree_expr_nonnegative_warnv_p (arg0,
14779                                                strict_overflow_p)
14780                 || (tree_expr_nonnegative_warnv_p (arg1,
14781                                                    strict_overflow_p)));
14782
14783         CASE_FLT_FN (BUILT_IN_FMIN):
14784         /* True if the 1st AND 2nd arguments are nonnegative.  */
14785         return (tree_expr_nonnegative_warnv_p (arg0,
14786                                                strict_overflow_p)
14787                 && (tree_expr_nonnegative_warnv_p (arg1,
14788                                                    strict_overflow_p)));
14789
14790         CASE_FLT_FN (BUILT_IN_COPYSIGN):
14791         /* True if the 2nd argument is nonnegative.  */
14792         return tree_expr_nonnegative_warnv_p (arg1,
14793                                               strict_overflow_p);
14794
14795         CASE_FLT_FN (BUILT_IN_POWI):
14796         /* True if the 1st argument is nonnegative or the second
14797            argument is an even integer.  */
14798         if (TREE_CODE (arg1) == INTEGER_CST
14799             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14800           return true;
14801         return tree_expr_nonnegative_warnv_p (arg0,
14802                                               strict_overflow_p);
14803
14804         CASE_FLT_FN (BUILT_IN_POW):
14805         /* True if the 1st argument is nonnegative or the second
14806            argument is an even integer valued real.  */
14807         if (TREE_CODE (arg1) == REAL_CST)
14808           {
14809             REAL_VALUE_TYPE c;
14810             HOST_WIDE_INT n;
14811
14812             c = TREE_REAL_CST (arg1);
14813             n = real_to_integer (&c);
14814             if ((n & 1) == 0)
14815               {
14816                 REAL_VALUE_TYPE cint;
14817                 real_from_integer (&cint, VOIDmode, n,
14818                                    n < 0 ? -1 : 0, 0);
14819                 if (real_identical (&c, &cint))
14820                   return true;
14821               }
14822           }
14823         return tree_expr_nonnegative_warnv_p (arg0,
14824                                               strict_overflow_p);
14825
14826       default:
14827         break;
14828       }
14829   return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14830                                           type);
14831 }
14832
14833 /* Return true if T is known to be non-negative.  If the return
14834    value is based on the assumption that signed overflow is undefined,
14835    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14836    *STRICT_OVERFLOW_P.  */
14837
14838 bool
14839 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14840 {
14841   enum tree_code code = TREE_CODE (t);
14842   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14843     return true;
14844
14845   switch (code)
14846     {
14847     case TARGET_EXPR:
14848       {
14849         tree temp = TARGET_EXPR_SLOT (t);
14850         t = TARGET_EXPR_INITIAL (t);
14851
14852         /* If the initializer is non-void, then it's a normal expression
14853            that will be assigned to the slot.  */
14854         if (!VOID_TYPE_P (t))
14855           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14856
14857         /* Otherwise, the initializer sets the slot in some way.  One common
14858            way is an assignment statement at the end of the initializer.  */
14859         while (1)
14860           {
14861             if (TREE_CODE (t) == BIND_EXPR)
14862               t = expr_last (BIND_EXPR_BODY (t));
14863             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14864                      || TREE_CODE (t) == TRY_CATCH_EXPR)
14865               t = expr_last (TREE_OPERAND (t, 0));
14866             else if (TREE_CODE (t) == STATEMENT_LIST)
14867               t = expr_last (t);
14868             else
14869               break;
14870           }
14871         if (TREE_CODE (t) == MODIFY_EXPR
14872             && TREE_OPERAND (t, 0) == temp)
14873           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14874                                                 strict_overflow_p);
14875
14876         return false;
14877       }
14878
14879     case CALL_EXPR:
14880       {
14881         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
14882         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
14883
14884         return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14885                                               get_callee_fndecl (t),
14886                                               arg0,
14887                                               arg1,
14888                                               strict_overflow_p);
14889       }
14890     case COMPOUND_EXPR:
14891     case MODIFY_EXPR:
14892       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14893                                             strict_overflow_p);
14894     case BIND_EXPR:
14895       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14896                                             strict_overflow_p);
14897     case SAVE_EXPR:
14898       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14899                                             strict_overflow_p);
14900
14901     default:
14902       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14903                                                    TREE_TYPE (t));
14904     }
14905
14906   /* We don't know sign of `t', so be conservative and return false.  */
14907   return false;
14908 }
14909
14910 /* Return true if T is known to be non-negative.  If the return
14911    value is based on the assumption that signed overflow is undefined,
14912    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14913    *STRICT_OVERFLOW_P.  */
14914
14915 bool
14916 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14917 {
14918   enum tree_code code;
14919   if (t == error_mark_node)
14920     return false;
14921
14922   code = TREE_CODE (t);
14923   switch (TREE_CODE_CLASS (code))
14924     {
14925     case tcc_binary:
14926     case tcc_comparison:
14927       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14928                                               TREE_TYPE (t),
14929                                               TREE_OPERAND (t, 0),
14930                                               TREE_OPERAND (t, 1),
14931                                               strict_overflow_p);
14932
14933     case tcc_unary:
14934       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14935                                              TREE_TYPE (t),
14936                                              TREE_OPERAND (t, 0),
14937                                              strict_overflow_p);
14938
14939     case tcc_constant:
14940     case tcc_declaration:
14941     case tcc_reference:
14942       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14943
14944     default:
14945       break;
14946     }
14947
14948   switch (code)
14949     {
14950     case TRUTH_AND_EXPR:
14951     case TRUTH_OR_EXPR:
14952     case TRUTH_XOR_EXPR:
14953       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14954                                               TREE_TYPE (t),
14955                                               TREE_OPERAND (t, 0),
14956                                               TREE_OPERAND (t, 1),
14957                                               strict_overflow_p);
14958     case TRUTH_NOT_EXPR:
14959       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14960                                              TREE_TYPE (t),
14961                                              TREE_OPERAND (t, 0),
14962                                              strict_overflow_p);
14963
14964     case COND_EXPR:
14965     case CONSTRUCTOR:
14966     case OBJ_TYPE_REF:
14967     case ASSERT_EXPR:
14968     case ADDR_EXPR:
14969     case WITH_SIZE_EXPR:
14970     case SSA_NAME:
14971       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14972
14973     default:
14974       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14975     }
14976 }
14977
14978 /* Return true if `t' is known to be non-negative.  Handle warnings
14979    about undefined signed overflow.  */
14980
14981 bool
14982 tree_expr_nonnegative_p (tree t)
14983 {
14984   bool ret, strict_overflow_p;
14985
14986   strict_overflow_p = false;
14987   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14988   if (strict_overflow_p)
14989     fold_overflow_warning (("assuming signed overflow does not occur when "
14990                             "determining that expression is always "
14991                             "non-negative"),
14992                            WARN_STRICT_OVERFLOW_MISC);
14993   return ret;
14994 }
14995
14996
14997 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14998    For floating point we further ensure that T is not denormal.
14999    Similar logic is present in nonzero_address in rtlanal.h.
15000
15001    If the return value is based on the assumption that signed overflow
15002    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15003    change *STRICT_OVERFLOW_P.  */
15004
15005 bool
15006 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15007                                  bool *strict_overflow_p)
15008 {
15009   switch (code)
15010     {
15011     case ABS_EXPR:
15012       return tree_expr_nonzero_warnv_p (op0,
15013                                         strict_overflow_p);
15014
15015     case NOP_EXPR:
15016       {
15017         tree inner_type = TREE_TYPE (op0);
15018         tree outer_type = type;
15019
15020         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15021                 && tree_expr_nonzero_warnv_p (op0,
15022                                               strict_overflow_p));
15023       }
15024       break;
15025
15026     case NON_LVALUE_EXPR:
15027       return tree_expr_nonzero_warnv_p (op0,
15028                                         strict_overflow_p);
15029
15030     default:
15031       break;
15032   }
15033
15034   return false;
15035 }
15036
15037 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15038    For floating point we further ensure that T is not denormal.
15039    Similar logic is present in nonzero_address in rtlanal.h.
15040
15041    If the return value is based on the assumption that signed overflow
15042    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15043    change *STRICT_OVERFLOW_P.  */
15044
15045 bool
15046 tree_binary_nonzero_warnv_p (enum tree_code code,
15047                              tree type,
15048                              tree op0,
15049                              tree op1, bool *strict_overflow_p)
15050 {
15051   bool sub_strict_overflow_p;
15052   switch (code)
15053     {
15054     case POINTER_PLUS_EXPR:
15055     case PLUS_EXPR:
15056       if (TYPE_OVERFLOW_UNDEFINED (type))
15057         {
15058           /* With the presence of negative values it is hard
15059              to say something.  */
15060           sub_strict_overflow_p = false;
15061           if (!tree_expr_nonnegative_warnv_p (op0,
15062                                               &sub_strict_overflow_p)
15063               || !tree_expr_nonnegative_warnv_p (op1,
15064                                                  &sub_strict_overflow_p))
15065             return false;
15066           /* One of operands must be positive and the other non-negative.  */
15067           /* We don't set *STRICT_OVERFLOW_P here: even if this value
15068              overflows, on a twos-complement machine the sum of two
15069              nonnegative numbers can never be zero.  */
15070           return (tree_expr_nonzero_warnv_p (op0,
15071                                              strict_overflow_p)
15072                   || tree_expr_nonzero_warnv_p (op1,
15073                                                 strict_overflow_p));
15074         }
15075       break;
15076
15077     case MULT_EXPR:
15078       if (TYPE_OVERFLOW_UNDEFINED (type))
15079         {
15080           if (tree_expr_nonzero_warnv_p (op0,
15081                                          strict_overflow_p)
15082               && tree_expr_nonzero_warnv_p (op1,
15083                                             strict_overflow_p))
15084             {
15085               *strict_overflow_p = true;
15086               return true;
15087             }
15088         }
15089       break;
15090
15091     case MIN_EXPR:
15092       sub_strict_overflow_p = false;
15093       if (tree_expr_nonzero_warnv_p (op0,
15094                                      &sub_strict_overflow_p)
15095           && tree_expr_nonzero_warnv_p (op1,
15096                                         &sub_strict_overflow_p))
15097         {
15098           if (sub_strict_overflow_p)
15099             *strict_overflow_p = true;
15100         }
15101       break;
15102
15103     case MAX_EXPR:
15104       sub_strict_overflow_p = false;
15105       if (tree_expr_nonzero_warnv_p (op0,
15106                                      &sub_strict_overflow_p))
15107         {
15108           if (sub_strict_overflow_p)
15109             *strict_overflow_p = true;
15110
15111           /* When both operands are nonzero, then MAX must be too.  */
15112           if (tree_expr_nonzero_warnv_p (op1,
15113                                          strict_overflow_p))
15114             return true;
15115
15116           /* MAX where operand 0 is positive is positive.  */
15117           return tree_expr_nonnegative_warnv_p (op0,
15118                                                strict_overflow_p);
15119         }
15120       /* MAX where operand 1 is positive is positive.  */
15121       else if (tree_expr_nonzero_warnv_p (op1,
15122                                           &sub_strict_overflow_p)
15123                && tree_expr_nonnegative_warnv_p (op1,
15124                                                  &sub_strict_overflow_p))
15125         {
15126           if (sub_strict_overflow_p)
15127             *strict_overflow_p = true;
15128           return true;
15129         }
15130       break;
15131
15132     case BIT_IOR_EXPR:
15133       return (tree_expr_nonzero_warnv_p (op1,
15134                                          strict_overflow_p)
15135               || tree_expr_nonzero_warnv_p (op0,
15136                                             strict_overflow_p));
15137
15138     default:
15139       break;
15140   }
15141
15142   return false;
15143 }
15144
15145 /* Return true when T is an address and is known to be nonzero.
15146    For floating point we further ensure that T is not denormal.
15147    Similar logic is present in nonzero_address in rtlanal.h.
15148
15149    If the return value is based on the assumption that signed overflow
15150    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15151    change *STRICT_OVERFLOW_P.  */
15152
15153 bool
15154 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15155 {
15156   bool sub_strict_overflow_p;
15157   switch (TREE_CODE (t))
15158     {
15159     case INTEGER_CST:
15160       return !integer_zerop (t);
15161
15162     case ADDR_EXPR:
15163       {
15164         tree base = TREE_OPERAND (t, 0);
15165         if (!DECL_P (base))
15166           base = get_base_address (base);
15167
15168         if (!base)
15169           return false;
15170
15171         /* Weak declarations may link to NULL.  Other things may also be NULL
15172            so protect with -fdelete-null-pointer-checks; but not variables
15173            allocated on the stack.  */
15174         if (DECL_P (base)
15175             && (flag_delete_null_pointer_checks
15176                 || (DECL_CONTEXT (base)
15177                     && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
15178                     && auto_var_in_fn_p (base, DECL_CONTEXT (base)))))
15179           return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
15180
15181         /* Constants are never weak.  */
15182         if (CONSTANT_CLASS_P (base))
15183           return true;
15184
15185         return false;
15186       }
15187
15188     case COND_EXPR:
15189       sub_strict_overflow_p = false;
15190       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15191                                      &sub_strict_overflow_p)
15192           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15193                                         &sub_strict_overflow_p))
15194         {
15195           if (sub_strict_overflow_p)
15196             *strict_overflow_p = true;
15197           return true;
15198         }
15199       break;
15200
15201     default:
15202       break;
15203     }
15204   return false;
15205 }
15206
15207 /* Return true when T is an address and is known to be nonzero.
15208    For floating point we further ensure that T is not denormal.
15209    Similar logic is present in nonzero_address in rtlanal.h.
15210
15211    If the return value is based on the assumption that signed overflow
15212    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15213    change *STRICT_OVERFLOW_P.  */
15214
15215 bool
15216 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15217 {
15218   tree type = TREE_TYPE (t);
15219   enum tree_code code;
15220
15221   /* Doing something useful for floating point would need more work.  */
15222   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15223     return false;
15224
15225   code = TREE_CODE (t);
15226   switch (TREE_CODE_CLASS (code))
15227     {
15228     case tcc_unary:
15229       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15230                                               strict_overflow_p);
15231     case tcc_binary:
15232     case tcc_comparison:
15233       return tree_binary_nonzero_warnv_p (code, type,
15234                                                TREE_OPERAND (t, 0),
15235                                                TREE_OPERAND (t, 1),
15236                                                strict_overflow_p);
15237     case tcc_constant:
15238     case tcc_declaration:
15239     case tcc_reference:
15240       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15241
15242     default:
15243       break;
15244     }
15245
15246   switch (code)
15247     {
15248     case TRUTH_NOT_EXPR:
15249       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15250                                               strict_overflow_p);
15251
15252     case TRUTH_AND_EXPR:
15253     case TRUTH_OR_EXPR:
15254     case TRUTH_XOR_EXPR:
15255       return tree_binary_nonzero_warnv_p (code, type,
15256                                                TREE_OPERAND (t, 0),
15257                                                TREE_OPERAND (t, 1),
15258                                                strict_overflow_p);
15259
15260     case COND_EXPR:
15261     case CONSTRUCTOR:
15262     case OBJ_TYPE_REF:
15263     case ASSERT_EXPR:
15264     case ADDR_EXPR:
15265     case WITH_SIZE_EXPR:
15266     case SSA_NAME:
15267       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15268
15269     case COMPOUND_EXPR:
15270     case MODIFY_EXPR:
15271     case BIND_EXPR:
15272       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15273                                         strict_overflow_p);
15274
15275     case SAVE_EXPR:
15276       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15277                                         strict_overflow_p);
15278
15279     case CALL_EXPR:
15280       return alloca_call_p (t);
15281
15282     default:
15283       break;
15284     }
15285   return false;
15286 }
15287
15288 /* Return true when T is an address and is known to be nonzero.
15289    Handle warnings about undefined signed overflow.  */
15290
15291 bool
15292 tree_expr_nonzero_p (tree t)
15293 {
15294   bool ret, strict_overflow_p;
15295
15296   strict_overflow_p = false;
15297   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15298   if (strict_overflow_p)
15299     fold_overflow_warning (("assuming signed overflow does not occur when "
15300                             "determining that expression is always "
15301                             "non-zero"),
15302                            WARN_STRICT_OVERFLOW_MISC);
15303   return ret;
15304 }
15305
15306 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15307    attempt to fold the expression to a constant without modifying TYPE,
15308    OP0 or OP1.
15309
15310    If the expression could be simplified to a constant, then return
15311    the constant.  If the expression would not be simplified to a
15312    constant, then return NULL_TREE.  */
15313
15314 tree
15315 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15316 {
15317   tree tem = fold_binary (code, type, op0, op1);
15318   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15319 }
15320
15321 /* Given the components of a unary expression CODE, TYPE and OP0,
15322    attempt to fold the expression to a constant without modifying
15323    TYPE or OP0.
15324
15325    If the expression could be simplified to a constant, then return
15326    the constant.  If the expression would not be simplified to a
15327    constant, then return NULL_TREE.  */
15328
15329 tree
15330 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15331 {
15332   tree tem = fold_unary (code, type, op0);
15333   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15334 }
15335
15336 /* If EXP represents referencing an element in a constant string
15337    (either via pointer arithmetic or array indexing), return the
15338    tree representing the value accessed, otherwise return NULL.  */
15339
15340 tree
15341 fold_read_from_constant_string (tree exp)
15342 {
15343   if ((TREE_CODE (exp) == INDIRECT_REF
15344        || TREE_CODE (exp) == ARRAY_REF)
15345       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15346     {
15347       tree exp1 = TREE_OPERAND (exp, 0);
15348       tree index;
15349       tree string;
15350       location_t loc = EXPR_LOCATION (exp);
15351
15352       if (TREE_CODE (exp) == INDIRECT_REF)
15353         string = string_constant (exp1, &index);
15354       else
15355         {
15356           tree low_bound = array_ref_low_bound (exp);
15357           index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15358
15359           /* Optimize the special-case of a zero lower bound.
15360
15361              We convert the low_bound to sizetype to avoid some problems
15362              with constant folding.  (E.g. suppose the lower bound is 1,
15363              and its mode is QI.  Without the conversion,l (ARRAY
15364              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15365              +INDEX), which becomes (ARRAY+255+INDEX).  Oops!)  */
15366           if (! integer_zerop (low_bound))
15367             index = size_diffop_loc (loc, index,
15368                                  fold_convert_loc (loc, sizetype, low_bound));
15369
15370           string = exp1;
15371         }
15372
15373       if (string
15374           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15375           && TREE_CODE (string) == STRING_CST
15376           && TREE_CODE (index) == INTEGER_CST
15377           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15378           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15379               == MODE_INT)
15380           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15381         return build_int_cst_type (TREE_TYPE (exp),
15382                                    (TREE_STRING_POINTER (string)
15383                                     [TREE_INT_CST_LOW (index)]));
15384     }
15385   return NULL;
15386 }
15387
15388 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15389    an integer constant, real, or fixed-point constant.
15390
15391    TYPE is the type of the result.  */
15392
15393 static tree
15394 fold_negate_const (tree arg0, tree type)
15395 {
15396   tree t = NULL_TREE;
15397
15398   switch (TREE_CODE (arg0))
15399     {
15400     case INTEGER_CST:
15401       {
15402         double_int val = tree_to_double_int (arg0);
15403         int overflow = neg_double (val.low, val.high, &val.low, &val.high);
15404
15405         t = force_fit_type_double (type, val, 1,
15406                                    (overflow | TREE_OVERFLOW (arg0))
15407                                    && !TYPE_UNSIGNED (type));
15408         break;
15409       }
15410
15411     case REAL_CST:
15412       t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15413       break;
15414
15415     case FIXED_CST:
15416       {
15417         FIXED_VALUE_TYPE f;
15418         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15419                                             &(TREE_FIXED_CST (arg0)), NULL,
15420                                             TYPE_SATURATING (type));
15421         t = build_fixed (type, f);
15422         /* Propagate overflow flags.  */
15423         if (overflow_p | TREE_OVERFLOW (arg0))
15424           TREE_OVERFLOW (t) = 1;
15425         break;
15426       }
15427
15428     default:
15429       gcc_unreachable ();
15430     }
15431
15432   return t;
15433 }
15434
15435 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15436    an integer constant or real constant.
15437
15438    TYPE is the type of the result.  */
15439
15440 tree
15441 fold_abs_const (tree arg0, tree type)
15442 {
15443   tree t = NULL_TREE;
15444
15445   switch (TREE_CODE (arg0))
15446     {
15447     case INTEGER_CST:
15448       {
15449         double_int val = tree_to_double_int (arg0);
15450
15451         /* If the value is unsigned or non-negative, then the absolute value
15452            is the same as the ordinary value.  */
15453         if (TYPE_UNSIGNED (type)
15454             || !double_int_negative_p (val))
15455           t = arg0;
15456
15457         /* If the value is negative, then the absolute value is
15458            its negation.  */
15459         else
15460           {
15461             int overflow;
15462
15463             overflow = neg_double (val.low, val.high, &val.low, &val.high);
15464             t = force_fit_type_double (type, val, -1,
15465                                        overflow | TREE_OVERFLOW (arg0));
15466           }
15467       }
15468       break;
15469
15470     case REAL_CST:
15471       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15472         t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15473       else
15474         t =  arg0;
15475       break;
15476
15477     default:
15478       gcc_unreachable ();
15479     }
15480
15481   return t;
15482 }
15483
15484 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15485    constant.  TYPE is the type of the result.  */
15486
15487 static tree
15488 fold_not_const (const_tree arg0, tree type)
15489 {
15490   double_int val;  
15491
15492   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15493
15494   val = double_int_not (tree_to_double_int (arg0));
15495   return force_fit_type_double (type, val, 0, TREE_OVERFLOW (arg0));
15496 }
15497
15498 /* Given CODE, a relational operator, the target type, TYPE and two
15499    constant operands OP0 and OP1, return the result of the
15500    relational operation.  If the result is not a compile time
15501    constant, then return NULL_TREE.  */
15502
15503 static tree
15504 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15505 {
15506   int result, invert;
15507
15508   /* From here on, the only cases we handle are when the result is
15509      known to be a constant.  */
15510
15511   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15512     {
15513       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15514       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15515
15516       /* Handle the cases where either operand is a NaN.  */
15517       if (real_isnan (c0) || real_isnan (c1))
15518         {
15519           switch (code)
15520             {
15521             case EQ_EXPR:
15522             case ORDERED_EXPR:
15523               result = 0;
15524               break;
15525
15526             case NE_EXPR:
15527             case UNORDERED_EXPR:
15528             case UNLT_EXPR:
15529             case UNLE_EXPR:
15530             case UNGT_EXPR:
15531             case UNGE_EXPR:
15532             case UNEQ_EXPR:
15533               result = 1;
15534               break;
15535
15536             case LT_EXPR:
15537             case LE_EXPR:
15538             case GT_EXPR:
15539             case GE_EXPR:
15540             case LTGT_EXPR:
15541               if (flag_trapping_math)
15542                 return NULL_TREE;
15543               result = 0;
15544               break;
15545
15546             default:
15547               gcc_unreachable ();
15548             }
15549
15550           return constant_boolean_node (result, type);
15551         }
15552
15553       return constant_boolean_node (real_compare (code, c0, c1), type);
15554     }
15555
15556   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15557     {
15558       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15559       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15560       return constant_boolean_node (fixed_compare (code, c0, c1), type);
15561     }
15562
15563   /* Handle equality/inequality of complex constants.  */
15564   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15565     {
15566       tree rcond = fold_relational_const (code, type,
15567                                           TREE_REALPART (op0),
15568                                           TREE_REALPART (op1));
15569       tree icond = fold_relational_const (code, type,
15570                                           TREE_IMAGPART (op0),
15571                                           TREE_IMAGPART (op1));
15572       if (code == EQ_EXPR)
15573         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15574       else if (code == NE_EXPR)
15575         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15576       else
15577         return NULL_TREE;
15578     }
15579
15580   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15581
15582      To compute GT, swap the arguments and do LT.
15583      To compute GE, do LT and invert the result.
15584      To compute LE, swap the arguments, do LT and invert the result.
15585      To compute NE, do EQ and invert the result.
15586
15587      Therefore, the code below must handle only EQ and LT.  */
15588
15589   if (code == LE_EXPR || code == GT_EXPR)
15590     {
15591       tree tem = op0;
15592       op0 = op1;
15593       op1 = tem;
15594       code = swap_tree_comparison (code);
15595     }
15596
15597   /* Note that it is safe to invert for real values here because we
15598      have already handled the one case that it matters.  */
15599
15600   invert = 0;
15601   if (code == NE_EXPR || code == GE_EXPR)
15602     {
15603       invert = 1;
15604       code = invert_tree_comparison (code, false);
15605     }
15606
15607   /* Compute a result for LT or EQ if args permit;
15608      Otherwise return T.  */
15609   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15610     {
15611       if (code == EQ_EXPR)
15612         result = tree_int_cst_equal (op0, op1);
15613       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15614         result = INT_CST_LT_UNSIGNED (op0, op1);
15615       else
15616         result = INT_CST_LT (op0, op1);
15617     }
15618   else
15619     return NULL_TREE;
15620
15621   if (invert)
15622     result ^= 1;
15623   return constant_boolean_node (result, type);
15624 }
15625
15626 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15627    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
15628    itself.  */
15629
15630 tree
15631 fold_build_cleanup_point_expr (tree type, tree expr)
15632 {
15633   /* If the expression does not have side effects then we don't have to wrap
15634      it with a cleanup point expression.  */
15635   if (!TREE_SIDE_EFFECTS (expr))
15636     return expr;
15637
15638   /* If the expression is a return, check to see if the expression inside the
15639      return has no side effects or the right hand side of the modify expression
15640      inside the return. If either don't have side effects set we don't need to
15641      wrap the expression in a cleanup point expression.  Note we don't check the
15642      left hand side of the modify because it should always be a return decl.  */
15643   if (TREE_CODE (expr) == RETURN_EXPR)
15644     {
15645       tree op = TREE_OPERAND (expr, 0);
15646       if (!op || !TREE_SIDE_EFFECTS (op))
15647         return expr;
15648       op = TREE_OPERAND (op, 1);
15649       if (!TREE_SIDE_EFFECTS (op))
15650         return expr;
15651     }
15652
15653   return build1 (CLEANUP_POINT_EXPR, type, expr);
15654 }
15655
15656 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15657    of an indirection through OP0, or NULL_TREE if no simplification is
15658    possible.  */
15659
15660 tree
15661 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15662 {
15663   tree sub = op0;
15664   tree subtype;
15665
15666   STRIP_NOPS (sub);
15667   subtype = TREE_TYPE (sub);
15668   if (!POINTER_TYPE_P (subtype))
15669     return NULL_TREE;
15670
15671   if (TREE_CODE (sub) == ADDR_EXPR)
15672     {
15673       tree op = TREE_OPERAND (sub, 0);
15674       tree optype = TREE_TYPE (op);
15675       /* *&CONST_DECL -> to the value of the const decl.  */
15676       if (TREE_CODE (op) == CONST_DECL)
15677         return DECL_INITIAL (op);
15678       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
15679       if (type == optype)
15680         {
15681           tree fop = fold_read_from_constant_string (op);
15682           if (fop)
15683             return fop;
15684           else
15685             return op;
15686         }
15687       /* *(foo *)&fooarray => fooarray[0] */
15688       else if (TREE_CODE (optype) == ARRAY_TYPE
15689                && type == TREE_TYPE (optype)
15690                && (!in_gimple_form
15691                    || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15692         {
15693           tree type_domain = TYPE_DOMAIN (optype);
15694           tree min_val = size_zero_node;
15695           if (type_domain && TYPE_MIN_VALUE (type_domain))
15696             min_val = TYPE_MIN_VALUE (type_domain);
15697           if (in_gimple_form
15698               && TREE_CODE (min_val) != INTEGER_CST)
15699             return NULL_TREE;
15700           return build4_loc (loc, ARRAY_REF, type, op, min_val,
15701                              NULL_TREE, NULL_TREE);
15702         }
15703       /* *(foo *)&complexfoo => __real__ complexfoo */
15704       else if (TREE_CODE (optype) == COMPLEX_TYPE
15705                && type == TREE_TYPE (optype))
15706         return fold_build1_loc (loc, REALPART_EXPR, type, op);
15707       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15708       else if (TREE_CODE (optype) == VECTOR_TYPE
15709                && type == TREE_TYPE (optype))
15710         {
15711           tree part_width = TYPE_SIZE (type);
15712           tree index = bitsize_int (0);
15713           return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
15714         }
15715     }
15716
15717   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15718       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15719     {
15720       tree op00 = TREE_OPERAND (sub, 0);
15721       tree op01 = TREE_OPERAND (sub, 1);
15722
15723       STRIP_NOPS (op00);
15724       if (TREE_CODE (op00) == ADDR_EXPR)
15725         {
15726           tree op00type;
15727           op00 = TREE_OPERAND (op00, 0);
15728           op00type = TREE_TYPE (op00);
15729
15730           /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15731           if (TREE_CODE (op00type) == VECTOR_TYPE
15732               && type == TREE_TYPE (op00type))
15733             {
15734               HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15735               tree part_width = TYPE_SIZE (type);
15736               unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15737               unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15738               tree index = bitsize_int (indexi);
15739
15740               if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (op00type))
15741                 return fold_build3_loc (loc,
15742                                         BIT_FIELD_REF, type, op00,
15743                                         part_width, index);
15744
15745             }
15746           /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15747           else if (TREE_CODE (op00type) == COMPLEX_TYPE
15748                    && type == TREE_TYPE (op00type))
15749             {
15750               tree size = TYPE_SIZE_UNIT (type);
15751               if (tree_int_cst_equal (size, op01))
15752                 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
15753             }
15754           /* ((foo *)&fooarray)[1] => fooarray[1] */
15755           else if (TREE_CODE (op00type) == ARRAY_TYPE
15756                    && type == TREE_TYPE (op00type))
15757             {
15758               tree type_domain = TYPE_DOMAIN (op00type);
15759               tree min_val = size_zero_node;
15760               if (type_domain && TYPE_MIN_VALUE (type_domain))
15761                 min_val = TYPE_MIN_VALUE (type_domain);
15762               op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
15763                                      TYPE_SIZE_UNIT (type));
15764               op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
15765               return build4_loc (loc, ARRAY_REF, type, op00, op01,
15766                                  NULL_TREE, NULL_TREE);
15767             }
15768         }
15769     }
15770
15771   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15772   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15773       && type == TREE_TYPE (TREE_TYPE (subtype))
15774       && (!in_gimple_form
15775           || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15776     {
15777       tree type_domain;
15778       tree min_val = size_zero_node;
15779       sub = build_fold_indirect_ref_loc (loc, sub);
15780       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15781       if (type_domain && TYPE_MIN_VALUE (type_domain))
15782         min_val = TYPE_MIN_VALUE (type_domain);
15783       if (in_gimple_form
15784           && TREE_CODE (min_val) != INTEGER_CST)
15785         return NULL_TREE;
15786       return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
15787                          NULL_TREE);
15788     }
15789
15790   return NULL_TREE;
15791 }
15792
15793 /* Builds an expression for an indirection through T, simplifying some
15794    cases.  */
15795
15796 tree
15797 build_fold_indirect_ref_loc (location_t loc, tree t)
15798 {
15799   tree type = TREE_TYPE (TREE_TYPE (t));
15800   tree sub = fold_indirect_ref_1 (loc, type, t);
15801
15802   if (sub)
15803     return sub;
15804
15805   return build1_loc (loc, INDIRECT_REF, type, t);
15806 }
15807
15808 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
15809
15810 tree
15811 fold_indirect_ref_loc (location_t loc, tree t)
15812 {
15813   tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15814
15815   if (sub)
15816     return sub;
15817   else
15818     return t;
15819 }
15820
15821 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15822    whose result is ignored.  The type of the returned tree need not be
15823    the same as the original expression.  */
15824
15825 tree
15826 fold_ignored_result (tree t)
15827 {
15828   if (!TREE_SIDE_EFFECTS (t))
15829     return integer_zero_node;
15830
15831   for (;;)
15832     switch (TREE_CODE_CLASS (TREE_CODE (t)))
15833       {
15834       case tcc_unary:
15835         t = TREE_OPERAND (t, 0);
15836         break;
15837
15838       case tcc_binary:
15839       case tcc_comparison:
15840         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15841           t = TREE_OPERAND (t, 0);
15842         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15843           t = TREE_OPERAND (t, 1);
15844         else
15845           return t;
15846         break;
15847
15848       case tcc_expression:
15849         switch (TREE_CODE (t))
15850           {
15851           case COMPOUND_EXPR:
15852             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15853               return t;
15854             t = TREE_OPERAND (t, 0);
15855             break;
15856
15857           case COND_EXPR:
15858             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15859                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15860               return t;
15861             t = TREE_OPERAND (t, 0);
15862             break;
15863
15864           default:
15865             return t;
15866           }
15867         break;
15868
15869       default:
15870         return t;
15871       }
15872 }
15873
15874 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15875    This can only be applied to objects of a sizetype.  */
15876
15877 tree
15878 round_up_loc (location_t loc, tree value, int divisor)
15879 {
15880   tree div = NULL_TREE;
15881
15882   gcc_assert (divisor > 0);
15883   if (divisor == 1)
15884     return value;
15885
15886   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15887      have to do anything.  Only do this when we are not given a const,
15888      because in that case, this check is more expensive than just
15889      doing it.  */
15890   if (TREE_CODE (value) != INTEGER_CST)
15891     {
15892       div = build_int_cst (TREE_TYPE (value), divisor);
15893
15894       if (multiple_of_p (TREE_TYPE (value), value, div))
15895         return value;
15896     }
15897
15898   /* If divisor is a power of two, simplify this to bit manipulation.  */
15899   if (divisor == (divisor & -divisor))
15900     {
15901       if (TREE_CODE (value) == INTEGER_CST)
15902         {
15903           double_int val = tree_to_double_int (value);
15904           bool overflow_p;
15905
15906           if ((val.low & (divisor - 1)) == 0)
15907             return value;
15908
15909           overflow_p = TREE_OVERFLOW (value);
15910           val.low &= ~(divisor - 1);
15911           val.low += divisor;
15912           if (val.low == 0)
15913             {
15914               val.high++;
15915               if (val.high == 0)
15916                 overflow_p = true;
15917             }
15918
15919           return force_fit_type_double (TREE_TYPE (value), val,
15920                                         -1, overflow_p);
15921         }
15922       else
15923         {
15924           tree t;
15925
15926           t = build_int_cst (TREE_TYPE (value), divisor - 1);
15927           value = size_binop_loc (loc, PLUS_EXPR, value, t);
15928           t = build_int_cst (TREE_TYPE (value), -divisor);
15929           value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15930         }
15931     }
15932   else
15933     {
15934       if (!div)
15935         div = build_int_cst (TREE_TYPE (value), divisor);
15936       value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
15937       value = size_binop_loc (loc, MULT_EXPR, value, div);
15938     }
15939
15940   return value;
15941 }
15942
15943 /* Likewise, but round down.  */
15944
15945 tree
15946 round_down_loc (location_t loc, tree value, int divisor)
15947 {
15948   tree div = NULL_TREE;
15949
15950   gcc_assert (divisor > 0);
15951   if (divisor == 1)
15952     return value;
15953
15954   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15955      have to do anything.  Only do this when we are not given a const,
15956      because in that case, this check is more expensive than just
15957      doing it.  */
15958   if (TREE_CODE (value) != INTEGER_CST)
15959     {
15960       div = build_int_cst (TREE_TYPE (value), divisor);
15961
15962       if (multiple_of_p (TREE_TYPE (value), value, div))
15963         return value;
15964     }
15965
15966   /* If divisor is a power of two, simplify this to bit manipulation.  */
15967   if (divisor == (divisor & -divisor))
15968     {
15969       tree t;
15970
15971       t = build_int_cst (TREE_TYPE (value), -divisor);
15972       value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15973     }
15974   else
15975     {
15976       if (!div)
15977         div = build_int_cst (TREE_TYPE (value), divisor);
15978       value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
15979       value = size_binop_loc (loc, MULT_EXPR, value, div);
15980     }
15981
15982   return value;
15983 }
15984
15985 /* Returns the pointer to the base of the object addressed by EXP and
15986    extracts the information about the offset of the access, storing it
15987    to PBITPOS and POFFSET.  */
15988
15989 static tree
15990 split_address_to_core_and_offset (tree exp,
15991                                   HOST_WIDE_INT *pbitpos, tree *poffset)
15992 {
15993   tree core;
15994   enum machine_mode mode;
15995   int unsignedp, volatilep;
15996   HOST_WIDE_INT bitsize;
15997   location_t loc = EXPR_LOCATION (exp);
15998
15999   if (TREE_CODE (exp) == ADDR_EXPR)
16000     {
16001       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16002                                   poffset, &mode, &unsignedp, &volatilep,
16003                                   false);
16004       core = build_fold_addr_expr_loc (loc, core);
16005     }
16006   else
16007     {
16008       core = exp;
16009       *pbitpos = 0;
16010       *poffset = NULL_TREE;
16011     }
16012
16013   return core;
16014 }
16015
16016 /* Returns true if addresses of E1 and E2 differ by a constant, false
16017    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
16018
16019 bool
16020 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
16021 {
16022   tree core1, core2;
16023   HOST_WIDE_INT bitpos1, bitpos2;
16024   tree toffset1, toffset2, tdiff, type;
16025
16026   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16027   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16028
16029   if (bitpos1 % BITS_PER_UNIT != 0
16030       || bitpos2 % BITS_PER_UNIT != 0
16031       || !operand_equal_p (core1, core2, 0))
16032     return false;
16033
16034   if (toffset1 && toffset2)
16035     {
16036       type = TREE_TYPE (toffset1);
16037       if (type != TREE_TYPE (toffset2))
16038         toffset2 = fold_convert (type, toffset2);
16039
16040       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16041       if (!cst_and_fits_in_hwi (tdiff))
16042         return false;
16043
16044       *diff = int_cst_value (tdiff);
16045     }
16046   else if (toffset1 || toffset2)
16047     {
16048       /* If only one of the offsets is non-constant, the difference cannot
16049          be a constant.  */
16050       return false;
16051     }
16052   else
16053     *diff = 0;
16054
16055   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
16056   return true;
16057 }
16058
16059 /* Simplify the floating point expression EXP when the sign of the
16060    result is not significant.  Return NULL_TREE if no simplification
16061    is possible.  */
16062
16063 tree
16064 fold_strip_sign_ops (tree exp)
16065 {
16066   tree arg0, arg1;
16067   location_t loc = EXPR_LOCATION (exp);
16068
16069   switch (TREE_CODE (exp))
16070     {
16071     case ABS_EXPR:
16072     case NEGATE_EXPR:
16073       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16074       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
16075
16076     case MULT_EXPR:
16077     case RDIV_EXPR:
16078       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
16079         return NULL_TREE;
16080       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16081       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16082       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
16083         return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
16084                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
16085                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
16086       break;
16087
16088     case COMPOUND_EXPR:
16089       arg0 = TREE_OPERAND (exp, 0);
16090       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16091       if (arg1)
16092         return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
16093       break;
16094
16095     case COND_EXPR:
16096       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16097       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
16098       if (arg0 || arg1)
16099         return fold_build3_loc (loc,
16100                             COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
16101                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
16102                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
16103       break;
16104
16105     case CALL_EXPR:
16106       {
16107         const enum built_in_function fcode = builtin_mathfn_code (exp);
16108         switch (fcode)
16109         {
16110         CASE_FLT_FN (BUILT_IN_COPYSIGN):
16111           /* Strip copysign function call, return the 1st argument. */
16112           arg0 = CALL_EXPR_ARG (exp, 0);
16113           arg1 = CALL_EXPR_ARG (exp, 1);
16114           return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
16115
16116         default:
16117           /* Strip sign ops from the argument of "odd" math functions.  */
16118           if (negate_mathfn_p (fcode))
16119             {
16120               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
16121               if (arg0)
16122                 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
16123             }
16124           break;
16125         }
16126       }
16127       break;
16128
16129     default:
16130       break;
16131     }
16132   return NULL_TREE;
16133 }