OSDN Git Service

2011-07-08 Kai Tietz <ktietz@redhat.com>
[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   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1871     return fold_build1_loc (loc, NOP_EXPR, type, arg);
1872
1873   switch (TREE_CODE (type))
1874     {
1875     case POINTER_TYPE:
1876     case REFERENCE_TYPE:
1877       /* Handle conversions between pointers to different address spaces.  */
1878       if (POINTER_TYPE_P (orig)
1879           && (TYPE_ADDR_SPACE (TREE_TYPE (type))
1880               != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
1881         return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
1882       /* fall through */
1883
1884     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1885     case OFFSET_TYPE:
1886       if (TREE_CODE (arg) == INTEGER_CST)
1887         {
1888           tem = fold_convert_const (NOP_EXPR, type, arg);
1889           if (tem != NULL_TREE)
1890             return tem;
1891         }
1892       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1893           || TREE_CODE (orig) == OFFSET_TYPE)
1894         return fold_build1_loc (loc, NOP_EXPR, type, arg);
1895       if (TREE_CODE (orig) == COMPLEX_TYPE)
1896         return fold_convert_loc (loc, type,
1897                              fold_build1_loc (loc, REALPART_EXPR,
1898                                           TREE_TYPE (orig), arg));
1899       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
1900                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1901       return fold_build1_loc (loc, NOP_EXPR, type, arg);
1902
1903     case REAL_TYPE:
1904       if (TREE_CODE (arg) == INTEGER_CST)
1905         {
1906           tem = fold_convert_const (FLOAT_EXPR, type, arg);
1907           if (tem != NULL_TREE)
1908             return tem;
1909         }
1910       else if (TREE_CODE (arg) == REAL_CST)
1911         {
1912           tem = fold_convert_const (NOP_EXPR, type, arg);
1913           if (tem != NULL_TREE)
1914             return tem;
1915         }
1916       else if (TREE_CODE (arg) == FIXED_CST)
1917         {
1918           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1919           if (tem != NULL_TREE)
1920             return tem;
1921         }
1922
1923       switch (TREE_CODE (orig))
1924         {
1925         case INTEGER_TYPE:
1926         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1927         case POINTER_TYPE: case REFERENCE_TYPE:
1928           return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
1929
1930         case REAL_TYPE:
1931           return fold_build1_loc (loc, NOP_EXPR, type, arg);
1932
1933         case FIXED_POINT_TYPE:
1934           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1935
1936         case COMPLEX_TYPE:
1937           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1938           return fold_convert_loc (loc, type, tem);
1939
1940         default:
1941           gcc_unreachable ();
1942         }
1943
1944     case FIXED_POINT_TYPE:
1945       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
1946           || TREE_CODE (arg) == REAL_CST)
1947         {
1948           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1949           if (tem != NULL_TREE)
1950             goto fold_convert_exit;
1951         }
1952
1953       switch (TREE_CODE (orig))
1954         {
1955         case FIXED_POINT_TYPE:
1956         case INTEGER_TYPE:
1957         case ENUMERAL_TYPE:
1958         case BOOLEAN_TYPE:
1959         case REAL_TYPE:
1960           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1961
1962         case COMPLEX_TYPE:
1963           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1964           return fold_convert_loc (loc, type, tem);
1965
1966         default:
1967           gcc_unreachable ();
1968         }
1969
1970     case COMPLEX_TYPE:
1971       switch (TREE_CODE (orig))
1972         {
1973         case INTEGER_TYPE:
1974         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1975         case POINTER_TYPE: case REFERENCE_TYPE:
1976         case REAL_TYPE:
1977         case FIXED_POINT_TYPE:
1978           return fold_build2_loc (loc, COMPLEX_EXPR, type,
1979                               fold_convert_loc (loc, TREE_TYPE (type), arg),
1980                               fold_convert_loc (loc, TREE_TYPE (type),
1981                                             integer_zero_node));
1982         case COMPLEX_TYPE:
1983           {
1984             tree rpart, ipart;
1985
1986             if (TREE_CODE (arg) == COMPLEX_EXPR)
1987               {
1988                 rpart = fold_convert_loc (loc, TREE_TYPE (type),
1989                                       TREE_OPERAND (arg, 0));
1990                 ipart = fold_convert_loc (loc, TREE_TYPE (type),
1991                                       TREE_OPERAND (arg, 1));
1992                 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
1993               }
1994
1995             arg = save_expr (arg);
1996             rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1997             ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
1998             rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
1999             ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2000             return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2001           }
2002
2003         default:
2004           gcc_unreachable ();
2005         }
2006
2007     case VECTOR_TYPE:
2008       if (integer_zerop (arg))
2009         return build_zero_vector (type);
2010       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2011       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2012                   || TREE_CODE (orig) == VECTOR_TYPE);
2013       return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2014
2015     case VOID_TYPE:
2016       tem = fold_ignored_result (arg);
2017       return fold_build1_loc (loc, NOP_EXPR, type, tem);
2018
2019     default:
2020       gcc_unreachable ();
2021     }
2022  fold_convert_exit:
2023   protected_set_expr_location_unshare (tem, loc);
2024   return tem;
2025 }
2026 \f
2027 /* Return false if expr can be assumed not to be an lvalue, true
2028    otherwise.  */
2029
2030 static bool
2031 maybe_lvalue_p (const_tree x)
2032 {
2033   /* We only need to wrap lvalue tree codes.  */
2034   switch (TREE_CODE (x))
2035   {
2036   case VAR_DECL:
2037   case PARM_DECL:
2038   case RESULT_DECL:
2039   case LABEL_DECL:
2040   case FUNCTION_DECL:
2041   case SSA_NAME:
2042
2043   case COMPONENT_REF:
2044   case MEM_REF:
2045   case INDIRECT_REF:
2046   case ARRAY_REF:
2047   case ARRAY_RANGE_REF:
2048   case BIT_FIELD_REF:
2049   case OBJ_TYPE_REF:
2050
2051   case REALPART_EXPR:
2052   case IMAGPART_EXPR:
2053   case PREINCREMENT_EXPR:
2054   case PREDECREMENT_EXPR:
2055   case SAVE_EXPR:
2056   case TRY_CATCH_EXPR:
2057   case WITH_CLEANUP_EXPR:
2058   case COMPOUND_EXPR:
2059   case MODIFY_EXPR:
2060   case TARGET_EXPR:
2061   case COND_EXPR:
2062   case BIND_EXPR:
2063     break;
2064
2065   default:
2066     /* Assume the worst for front-end tree codes.  */
2067     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2068       break;
2069     return false;
2070   }
2071
2072   return true;
2073 }
2074
2075 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2076
2077 tree
2078 non_lvalue_loc (location_t loc, tree x)
2079 {
2080   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2081      us.  */
2082   if (in_gimple_form)
2083     return x;
2084
2085   if (! maybe_lvalue_p (x))
2086     return x;
2087   return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2088 }
2089
2090 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2091    Zero means allow extended lvalues.  */
2092
2093 int pedantic_lvalues;
2094
2095 /* When pedantic, return an expr equal to X but certainly not valid as a
2096    pedantic lvalue.  Otherwise, return X.  */
2097
2098 static tree
2099 pedantic_non_lvalue_loc (location_t loc, tree x)
2100 {
2101   if (pedantic_lvalues)
2102     return non_lvalue_loc (loc, x);
2103
2104   return protected_set_expr_location_unshare (x, loc);
2105 }
2106 \f
2107 /* Given a tree comparison code, return the code that is the logical inverse
2108    of the given code.  It is not safe to do this for floating-point
2109    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2110    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2111
2112 enum tree_code
2113 invert_tree_comparison (enum tree_code code, bool honor_nans)
2114 {
2115   if (honor_nans && flag_trapping_math)
2116     return ERROR_MARK;
2117
2118   switch (code)
2119     {
2120     case EQ_EXPR:
2121       return NE_EXPR;
2122     case NE_EXPR:
2123       return EQ_EXPR;
2124     case GT_EXPR:
2125       return honor_nans ? UNLE_EXPR : LE_EXPR;
2126     case GE_EXPR:
2127       return honor_nans ? UNLT_EXPR : LT_EXPR;
2128     case LT_EXPR:
2129       return honor_nans ? UNGE_EXPR : GE_EXPR;
2130     case LE_EXPR:
2131       return honor_nans ? UNGT_EXPR : GT_EXPR;
2132     case LTGT_EXPR:
2133       return UNEQ_EXPR;
2134     case UNEQ_EXPR:
2135       return LTGT_EXPR;
2136     case UNGT_EXPR:
2137       return LE_EXPR;
2138     case UNGE_EXPR:
2139       return LT_EXPR;
2140     case UNLT_EXPR:
2141       return GE_EXPR;
2142     case UNLE_EXPR:
2143       return GT_EXPR;
2144     case ORDERED_EXPR:
2145       return UNORDERED_EXPR;
2146     case UNORDERED_EXPR:
2147       return ORDERED_EXPR;
2148     default:
2149       gcc_unreachable ();
2150     }
2151 }
2152
2153 /* Similar, but return the comparison that results if the operands are
2154    swapped.  This is safe for floating-point.  */
2155
2156 enum tree_code
2157 swap_tree_comparison (enum tree_code code)
2158 {
2159   switch (code)
2160     {
2161     case EQ_EXPR:
2162     case NE_EXPR:
2163     case ORDERED_EXPR:
2164     case UNORDERED_EXPR:
2165     case LTGT_EXPR:
2166     case UNEQ_EXPR:
2167       return code;
2168     case GT_EXPR:
2169       return LT_EXPR;
2170     case GE_EXPR:
2171       return LE_EXPR;
2172     case LT_EXPR:
2173       return GT_EXPR;
2174     case LE_EXPR:
2175       return GE_EXPR;
2176     case UNGT_EXPR:
2177       return UNLT_EXPR;
2178     case UNGE_EXPR:
2179       return UNLE_EXPR;
2180     case UNLT_EXPR:
2181       return UNGT_EXPR;
2182     case UNLE_EXPR:
2183       return UNGE_EXPR;
2184     default:
2185       gcc_unreachable ();
2186     }
2187 }
2188
2189
2190 /* Convert a comparison tree code from an enum tree_code representation
2191    into a compcode bit-based encoding.  This function is the inverse of
2192    compcode_to_comparison.  */
2193
2194 static enum comparison_code
2195 comparison_to_compcode (enum tree_code code)
2196 {
2197   switch (code)
2198     {
2199     case LT_EXPR:
2200       return COMPCODE_LT;
2201     case EQ_EXPR:
2202       return COMPCODE_EQ;
2203     case LE_EXPR:
2204       return COMPCODE_LE;
2205     case GT_EXPR:
2206       return COMPCODE_GT;
2207     case NE_EXPR:
2208       return COMPCODE_NE;
2209     case GE_EXPR:
2210       return COMPCODE_GE;
2211     case ORDERED_EXPR:
2212       return COMPCODE_ORD;
2213     case UNORDERED_EXPR:
2214       return COMPCODE_UNORD;
2215     case UNLT_EXPR:
2216       return COMPCODE_UNLT;
2217     case UNEQ_EXPR:
2218       return COMPCODE_UNEQ;
2219     case UNLE_EXPR:
2220       return COMPCODE_UNLE;
2221     case UNGT_EXPR:
2222       return COMPCODE_UNGT;
2223     case LTGT_EXPR:
2224       return COMPCODE_LTGT;
2225     case UNGE_EXPR:
2226       return COMPCODE_UNGE;
2227     default:
2228       gcc_unreachable ();
2229     }
2230 }
2231
2232 /* Convert a compcode bit-based encoding of a comparison operator back
2233    to GCC's enum tree_code representation.  This function is the
2234    inverse of comparison_to_compcode.  */
2235
2236 static enum tree_code
2237 compcode_to_comparison (enum comparison_code code)
2238 {
2239   switch (code)
2240     {
2241     case COMPCODE_LT:
2242       return LT_EXPR;
2243     case COMPCODE_EQ:
2244       return EQ_EXPR;
2245     case COMPCODE_LE:
2246       return LE_EXPR;
2247     case COMPCODE_GT:
2248       return GT_EXPR;
2249     case COMPCODE_NE:
2250       return NE_EXPR;
2251     case COMPCODE_GE:
2252       return GE_EXPR;
2253     case COMPCODE_ORD:
2254       return ORDERED_EXPR;
2255     case COMPCODE_UNORD:
2256       return UNORDERED_EXPR;
2257     case COMPCODE_UNLT:
2258       return UNLT_EXPR;
2259     case COMPCODE_UNEQ:
2260       return UNEQ_EXPR;
2261     case COMPCODE_UNLE:
2262       return UNLE_EXPR;
2263     case COMPCODE_UNGT:
2264       return UNGT_EXPR;
2265     case COMPCODE_LTGT:
2266       return LTGT_EXPR;
2267     case COMPCODE_UNGE:
2268       return UNGE_EXPR;
2269     default:
2270       gcc_unreachable ();
2271     }
2272 }
2273
2274 /* Return a tree for the comparison which is the combination of
2275    doing the AND or OR (depending on CODE) of the two operations LCODE
2276    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2277    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2278    if this makes the transformation invalid.  */
2279
2280 tree
2281 combine_comparisons (location_t loc,
2282                      enum tree_code code, enum tree_code lcode,
2283                      enum tree_code rcode, tree truth_type,
2284                      tree ll_arg, tree lr_arg)
2285 {
2286   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2287   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2288   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2289   int compcode;
2290
2291   switch (code)
2292     {
2293     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2294       compcode = lcompcode & rcompcode;
2295       break;
2296
2297     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2298       compcode = lcompcode | rcompcode;
2299       break;
2300
2301     default:
2302       return NULL_TREE;
2303     }
2304
2305   if (!honor_nans)
2306     {
2307       /* Eliminate unordered comparisons, as well as LTGT and ORD
2308          which are not used unless the mode has NaNs.  */
2309       compcode &= ~COMPCODE_UNORD;
2310       if (compcode == COMPCODE_LTGT)
2311         compcode = COMPCODE_NE;
2312       else if (compcode == COMPCODE_ORD)
2313         compcode = COMPCODE_TRUE;
2314     }
2315    else if (flag_trapping_math)
2316      {
2317         /* Check that the original operation and the optimized ones will trap
2318            under the same condition.  */
2319         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2320                      && (lcompcode != COMPCODE_EQ)
2321                      && (lcompcode != COMPCODE_ORD);
2322         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2323                      && (rcompcode != COMPCODE_EQ)
2324                      && (rcompcode != COMPCODE_ORD);
2325         bool trap = (compcode & COMPCODE_UNORD) == 0
2326                     && (compcode != COMPCODE_EQ)
2327                     && (compcode != COMPCODE_ORD);
2328
2329         /* In a short-circuited boolean expression the LHS might be
2330            such that the RHS, if evaluated, will never trap.  For
2331            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2332            if neither x nor y is NaN.  (This is a mixed blessing: for
2333            example, the expression above will never trap, hence
2334            optimizing it to x < y would be invalid).  */
2335         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2336             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2337           rtrap = false;
2338
2339         /* If the comparison was short-circuited, and only the RHS
2340            trapped, we may now generate a spurious trap.  */
2341         if (rtrap && !ltrap
2342             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2343           return NULL_TREE;
2344
2345         /* If we changed the conditions that cause a trap, we lose.  */
2346         if ((ltrap || rtrap) != trap)
2347           return NULL_TREE;
2348       }
2349
2350   if (compcode == COMPCODE_TRUE)
2351     return constant_boolean_node (true, truth_type);
2352   else if (compcode == COMPCODE_FALSE)
2353     return constant_boolean_node (false, truth_type);
2354   else
2355     {
2356       enum tree_code tcode;
2357
2358       tcode = compcode_to_comparison ((enum comparison_code) compcode);
2359       return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2360     }
2361 }
2362 \f
2363 /* Return nonzero if two operands (typically of the same tree node)
2364    are necessarily equal.  If either argument has side-effects this
2365    function returns zero.  FLAGS modifies behavior as follows:
2366
2367    If OEP_ONLY_CONST is set, only return nonzero for constants.
2368    This function tests whether the operands are indistinguishable;
2369    it does not test whether they are equal using C's == operation.
2370    The distinction is important for IEEE floating point, because
2371    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2372    (2) two NaNs may be indistinguishable, but NaN!=NaN.
2373
2374    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2375    even though it may hold multiple values during a function.
2376    This is because a GCC tree node guarantees that nothing else is
2377    executed between the evaluation of its "operands" (which may often
2378    be evaluated in arbitrary order).  Hence if the operands themselves
2379    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2380    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
2381    unset means assuming isochronic (or instantaneous) tree equivalence.
2382    Unless comparing arbitrary expression trees, such as from different
2383    statements, this flag can usually be left unset.
2384
2385    If OEP_PURE_SAME is set, then pure functions with identical arguments
2386    are considered the same.  It is used when the caller has other ways
2387    to ensure that global memory is unchanged in between.  */
2388
2389 int
2390 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2391 {
2392   /* If either is ERROR_MARK, they aren't equal.  */
2393   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2394       || TREE_TYPE (arg0) == error_mark_node
2395       || TREE_TYPE (arg1) == error_mark_node)
2396     return 0;
2397
2398   /* Similar, if either does not have a type (like a released SSA name), 
2399      they aren't equal.  */
2400   if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2401     return 0;
2402
2403   /* Check equality of integer constants before bailing out due to
2404      precision differences.  */
2405   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2406     return tree_int_cst_equal (arg0, arg1);
2407
2408   /* If both types don't have the same signedness, then we can't consider
2409      them equal.  We must check this before the STRIP_NOPS calls
2410      because they may change the signedness of the arguments.  As pointers
2411      strictly don't have a signedness, require either two pointers or
2412      two non-pointers as well.  */
2413   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2414       || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2415     return 0;
2416
2417   /* We cannot consider pointers to different address space equal.  */
2418   if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2419       && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2420           != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2421     return 0;
2422
2423   /* If both types don't have the same precision, then it is not safe
2424      to strip NOPs.  */
2425   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
2426     return 0;
2427
2428   STRIP_NOPS (arg0);
2429   STRIP_NOPS (arg1);
2430
2431   /* In case both args are comparisons but with different comparison
2432      code, try to swap the comparison operands of one arg to produce
2433      a match and compare that variant.  */
2434   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2435       && COMPARISON_CLASS_P (arg0)
2436       && COMPARISON_CLASS_P (arg1))
2437     {
2438       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2439
2440       if (TREE_CODE (arg0) == swap_code)
2441         return operand_equal_p (TREE_OPERAND (arg0, 0),
2442                                 TREE_OPERAND (arg1, 1), flags)
2443                && operand_equal_p (TREE_OPERAND (arg0, 1),
2444                                    TREE_OPERAND (arg1, 0), flags);
2445     }
2446
2447   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2448       /* This is needed for conversions and for COMPONENT_REF.
2449          Might as well play it safe and always test this.  */
2450       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2451       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2452       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2453     return 0;
2454
2455   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2456      We don't care about side effects in that case because the SAVE_EXPR
2457      takes care of that for us. In all other cases, two expressions are
2458      equal if they have no side effects.  If we have two identical
2459      expressions with side effects that should be treated the same due
2460      to the only side effects being identical SAVE_EXPR's, that will
2461      be detected in the recursive calls below.
2462      If we are taking an invariant address of two identical objects
2463      they are necessarily equal as well.  */
2464   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2465       && (TREE_CODE (arg0) == SAVE_EXPR
2466           || (flags & OEP_CONSTANT_ADDRESS_OF)
2467           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2468     return 1;
2469
2470   /* Next handle constant cases, those for which we can return 1 even
2471      if ONLY_CONST is set.  */
2472   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2473     switch (TREE_CODE (arg0))
2474       {
2475       case INTEGER_CST:
2476         return tree_int_cst_equal (arg0, arg1);
2477
2478       case FIXED_CST:
2479         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2480                                        TREE_FIXED_CST (arg1));
2481
2482       case REAL_CST:
2483         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2484                                    TREE_REAL_CST (arg1)))
2485           return 1;
2486
2487
2488         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
2489           {
2490             /* If we do not distinguish between signed and unsigned zero,
2491                consider them equal.  */
2492             if (real_zerop (arg0) && real_zerop (arg1))
2493               return 1;
2494           }
2495         return 0;
2496
2497       case VECTOR_CST:
2498         {
2499           tree v1, v2;
2500
2501           v1 = TREE_VECTOR_CST_ELTS (arg0);
2502           v2 = TREE_VECTOR_CST_ELTS (arg1);
2503           while (v1 && v2)
2504             {
2505               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2506                                     flags))
2507                 return 0;
2508               v1 = TREE_CHAIN (v1);
2509               v2 = TREE_CHAIN (v2);
2510             }
2511
2512           return v1 == v2;
2513         }
2514
2515       case COMPLEX_CST:
2516         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2517                                  flags)
2518                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2519                                     flags));
2520
2521       case STRING_CST:
2522         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2523                 && ! memcmp (TREE_STRING_POINTER (arg0),
2524                               TREE_STRING_POINTER (arg1),
2525                               TREE_STRING_LENGTH (arg0)));
2526
2527       case ADDR_EXPR:
2528         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2529                                 TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1)
2530                                 ? OEP_CONSTANT_ADDRESS_OF : 0);
2531       default:
2532         break;
2533       }
2534
2535   if (flags & OEP_ONLY_CONST)
2536     return 0;
2537
2538 /* Define macros to test an operand from arg0 and arg1 for equality and a
2539    variant that allows null and views null as being different from any
2540    non-null value.  In the latter case, if either is null, the both
2541    must be; otherwise, do the normal comparison.  */
2542 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
2543                                     TREE_OPERAND (arg1, N), flags)
2544
2545 #define OP_SAME_WITH_NULL(N)                            \
2546   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2547    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2548
2549   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2550     {
2551     case tcc_unary:
2552       /* Two conversions are equal only if signedness and modes match.  */
2553       switch (TREE_CODE (arg0))
2554         {
2555         CASE_CONVERT:
2556         case FIX_TRUNC_EXPR:
2557           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2558               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2559             return 0;
2560           break;
2561         default:
2562           break;
2563         }
2564
2565       return OP_SAME (0);
2566
2567
2568     case tcc_comparison:
2569     case tcc_binary:
2570       if (OP_SAME (0) && OP_SAME (1))
2571         return 1;
2572
2573       /* For commutative ops, allow the other order.  */
2574       return (commutative_tree_code (TREE_CODE (arg0))
2575               && operand_equal_p (TREE_OPERAND (arg0, 0),
2576                                   TREE_OPERAND (arg1, 1), flags)
2577               && operand_equal_p (TREE_OPERAND (arg0, 1),
2578                                   TREE_OPERAND (arg1, 0), flags));
2579
2580     case tcc_reference:
2581       /* If either of the pointer (or reference) expressions we are
2582          dereferencing contain a side effect, these cannot be equal.  */
2583       if (TREE_SIDE_EFFECTS (arg0)
2584           || TREE_SIDE_EFFECTS (arg1))
2585         return 0;
2586
2587       switch (TREE_CODE (arg0))
2588         {
2589         case INDIRECT_REF:
2590         case REALPART_EXPR:
2591         case IMAGPART_EXPR:
2592           return OP_SAME (0);
2593
2594         case MEM_REF:
2595           /* Require equal access sizes, and similar pointer types.
2596              We can have incomplete types for array references of
2597              variable-sized arrays from the Fortran frontent
2598              though.  */
2599           return ((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1))
2600                    || (TYPE_SIZE (TREE_TYPE (arg0))
2601                        && TYPE_SIZE (TREE_TYPE (arg1))
2602                        && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
2603                                            TYPE_SIZE (TREE_TYPE (arg1)), flags)))
2604                   && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg0, 1)))
2605                       == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg1, 1))))
2606                   && OP_SAME (0) && OP_SAME (1));
2607
2608         case ARRAY_REF:
2609         case ARRAY_RANGE_REF:
2610           /* Operands 2 and 3 may be null.
2611              Compare the array index by value if it is constant first as we
2612              may have different types but same value here.  */
2613           return (OP_SAME (0)
2614                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2615                                           TREE_OPERAND (arg1, 1))
2616                       || OP_SAME (1))
2617                   && OP_SAME_WITH_NULL (2)
2618                   && OP_SAME_WITH_NULL (3));
2619
2620         case COMPONENT_REF:
2621           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
2622              may be NULL when we're called to compare MEM_EXPRs.  */
2623           return OP_SAME_WITH_NULL (0)
2624                  && OP_SAME (1)
2625                  && OP_SAME_WITH_NULL (2);
2626
2627         case BIT_FIELD_REF:
2628           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2629
2630         default:
2631           return 0;
2632         }
2633
2634     case tcc_expression:
2635       switch (TREE_CODE (arg0))
2636         {
2637         case ADDR_EXPR:
2638         case TRUTH_NOT_EXPR:
2639           return OP_SAME (0);
2640
2641         case TRUTH_ANDIF_EXPR:
2642         case TRUTH_ORIF_EXPR:
2643           return OP_SAME (0) && OP_SAME (1);
2644
2645         case FMA_EXPR:
2646         case WIDEN_MULT_PLUS_EXPR:
2647         case WIDEN_MULT_MINUS_EXPR:
2648           if (!OP_SAME (2))
2649             return 0;
2650           /* The multiplcation operands are commutative.  */
2651           /* FALLTHRU */
2652
2653         case TRUTH_AND_EXPR:
2654         case TRUTH_OR_EXPR:
2655         case TRUTH_XOR_EXPR:
2656           if (OP_SAME (0) && OP_SAME (1))
2657             return 1;
2658
2659           /* Otherwise take into account this is a commutative operation.  */
2660           return (operand_equal_p (TREE_OPERAND (arg0, 0),
2661                                    TREE_OPERAND (arg1, 1), flags)
2662                   && operand_equal_p (TREE_OPERAND (arg0, 1),
2663                                       TREE_OPERAND (arg1, 0), flags));
2664
2665         case COND_EXPR:
2666         case VEC_COND_EXPR:
2667         case DOT_PROD_EXPR:
2668           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2669
2670         default:
2671           return 0;
2672         }
2673
2674     case tcc_vl_exp:
2675       switch (TREE_CODE (arg0))
2676         {
2677         case CALL_EXPR:
2678           /* If the CALL_EXPRs call different functions, then they
2679              clearly can not be equal.  */
2680           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
2681                                  flags))
2682             return 0;
2683
2684           {
2685             unsigned int cef = call_expr_flags (arg0);
2686             if (flags & OEP_PURE_SAME)
2687               cef &= ECF_CONST | ECF_PURE;
2688             else
2689               cef &= ECF_CONST;
2690             if (!cef)
2691               return 0;
2692           }
2693
2694           /* Now see if all the arguments are the same.  */
2695           {
2696             const_call_expr_arg_iterator iter0, iter1;
2697             const_tree a0, a1;
2698             for (a0 = first_const_call_expr_arg (arg0, &iter0),
2699                    a1 = first_const_call_expr_arg (arg1, &iter1);
2700                  a0 && a1;
2701                  a0 = next_const_call_expr_arg (&iter0),
2702                    a1 = next_const_call_expr_arg (&iter1))
2703               if (! operand_equal_p (a0, a1, flags))
2704                 return 0;
2705
2706             /* If we get here and both argument lists are exhausted
2707                then the CALL_EXPRs are equal.  */
2708             return ! (a0 || a1);
2709           }
2710         default:
2711           return 0;
2712         }
2713
2714     case tcc_declaration:
2715       /* Consider __builtin_sqrt equal to sqrt.  */
2716       return (TREE_CODE (arg0) == FUNCTION_DECL
2717               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2718               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2719               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2720
2721     default:
2722       return 0;
2723     }
2724
2725 #undef OP_SAME
2726 #undef OP_SAME_WITH_NULL
2727 }
2728 \f
2729 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2730    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2731
2732    When in doubt, return 0.  */
2733
2734 static int
2735 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2736 {
2737   int unsignedp1, unsignedpo;
2738   tree primarg0, primarg1, primother;
2739   unsigned int correct_width;
2740
2741   if (operand_equal_p (arg0, arg1, 0))
2742     return 1;
2743
2744   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2745       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2746     return 0;
2747
2748   /* Discard any conversions that don't change the modes of ARG0 and ARG1
2749      and see if the inner values are the same.  This removes any
2750      signedness comparison, which doesn't matter here.  */
2751   primarg0 = arg0, primarg1 = arg1;
2752   STRIP_NOPS (primarg0);
2753   STRIP_NOPS (primarg1);
2754   if (operand_equal_p (primarg0, primarg1, 0))
2755     return 1;
2756
2757   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2758      actual comparison operand, ARG0.
2759
2760      First throw away any conversions to wider types
2761      already present in the operands.  */
2762
2763   primarg1 = get_narrower (arg1, &unsignedp1);
2764   primother = get_narrower (other, &unsignedpo);
2765
2766   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2767   if (unsignedp1 == unsignedpo
2768       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2769       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2770     {
2771       tree type = TREE_TYPE (arg0);
2772
2773       /* Make sure shorter operand is extended the right way
2774          to match the longer operand.  */
2775       primarg1 = fold_convert (signed_or_unsigned_type_for
2776                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2777
2778       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2779         return 1;
2780     }
2781
2782   return 0;
2783 }
2784 \f
2785 /* See if ARG is an expression that is either a comparison or is performing
2786    arithmetic on comparisons.  The comparisons must only be comparing
2787    two different values, which will be stored in *CVAL1 and *CVAL2; if
2788    they are nonzero it means that some operands have already been found.
2789    No variables may be used anywhere else in the expression except in the
2790    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
2791    the expression and save_expr needs to be called with CVAL1 and CVAL2.
2792
2793    If this is true, return 1.  Otherwise, return zero.  */
2794
2795 static int
2796 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2797 {
2798   enum tree_code code = TREE_CODE (arg);
2799   enum tree_code_class tclass = TREE_CODE_CLASS (code);
2800
2801   /* We can handle some of the tcc_expression cases here.  */
2802   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2803     tclass = tcc_unary;
2804   else if (tclass == tcc_expression
2805            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2806                || code == COMPOUND_EXPR))
2807     tclass = tcc_binary;
2808
2809   else if (tclass == tcc_expression && code == SAVE_EXPR
2810            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2811     {
2812       /* If we've already found a CVAL1 or CVAL2, this expression is
2813          two complex to handle.  */
2814       if (*cval1 || *cval2)
2815         return 0;
2816
2817       tclass = tcc_unary;
2818       *save_p = 1;
2819     }
2820
2821   switch (tclass)
2822     {
2823     case tcc_unary:
2824       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2825
2826     case tcc_binary:
2827       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2828               && twoval_comparison_p (TREE_OPERAND (arg, 1),
2829                                       cval1, cval2, save_p));
2830
2831     case tcc_constant:
2832       return 1;
2833
2834     case tcc_expression:
2835       if (code == COND_EXPR)
2836         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2837                                      cval1, cval2, save_p)
2838                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2839                                         cval1, cval2, save_p)
2840                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2841                                         cval1, cval2, save_p));
2842       return 0;
2843
2844     case tcc_comparison:
2845       /* First see if we can handle the first operand, then the second.  For
2846          the second operand, we know *CVAL1 can't be zero.  It must be that
2847          one side of the comparison is each of the values; test for the
2848          case where this isn't true by failing if the two operands
2849          are the same.  */
2850
2851       if (operand_equal_p (TREE_OPERAND (arg, 0),
2852                            TREE_OPERAND (arg, 1), 0))
2853         return 0;
2854
2855       if (*cval1 == 0)
2856         *cval1 = TREE_OPERAND (arg, 0);
2857       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2858         ;
2859       else if (*cval2 == 0)
2860         *cval2 = TREE_OPERAND (arg, 0);
2861       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2862         ;
2863       else
2864         return 0;
2865
2866       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2867         ;
2868       else if (*cval2 == 0)
2869         *cval2 = TREE_OPERAND (arg, 1);
2870       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2871         ;
2872       else
2873         return 0;
2874
2875       return 1;
2876
2877     default:
2878       return 0;
2879     }
2880 }
2881 \f
2882 /* ARG is a tree that is known to contain just arithmetic operations and
2883    comparisons.  Evaluate the operations in the tree substituting NEW0 for
2884    any occurrence of OLD0 as an operand of a comparison and likewise for
2885    NEW1 and OLD1.  */
2886
2887 static tree
2888 eval_subst (location_t loc, tree arg, tree old0, tree new0,
2889             tree old1, tree new1)
2890 {
2891   tree type = TREE_TYPE (arg);
2892   enum tree_code code = TREE_CODE (arg);
2893   enum tree_code_class tclass = TREE_CODE_CLASS (code);
2894
2895   /* We can handle some of the tcc_expression cases here.  */
2896   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2897     tclass = tcc_unary;
2898   else if (tclass == tcc_expression
2899            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2900     tclass = tcc_binary;
2901
2902   switch (tclass)
2903     {
2904     case tcc_unary:
2905       return fold_build1_loc (loc, code, type,
2906                           eval_subst (loc, TREE_OPERAND (arg, 0),
2907                                       old0, new0, old1, new1));
2908
2909     case tcc_binary:
2910       return fold_build2_loc (loc, code, type,
2911                           eval_subst (loc, TREE_OPERAND (arg, 0),
2912                                       old0, new0, old1, new1),
2913                           eval_subst (loc, TREE_OPERAND (arg, 1),
2914                                       old0, new0, old1, new1));
2915
2916     case tcc_expression:
2917       switch (code)
2918         {
2919         case SAVE_EXPR:
2920           return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
2921                              old1, new1);
2922
2923         case COMPOUND_EXPR:
2924           return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
2925                              old1, new1);
2926
2927         case COND_EXPR:
2928           return fold_build3_loc (loc, code, type,
2929                               eval_subst (loc, TREE_OPERAND (arg, 0),
2930                                           old0, new0, old1, new1),
2931                               eval_subst (loc, TREE_OPERAND (arg, 1),
2932                                           old0, new0, old1, new1),
2933                               eval_subst (loc, TREE_OPERAND (arg, 2),
2934                                           old0, new0, old1, new1));
2935         default:
2936           break;
2937         }
2938       /* Fall through - ???  */
2939
2940     case tcc_comparison:
2941       {
2942         tree arg0 = TREE_OPERAND (arg, 0);
2943         tree arg1 = TREE_OPERAND (arg, 1);
2944
2945         /* We need to check both for exact equality and tree equality.  The
2946            former will be true if the operand has a side-effect.  In that
2947            case, we know the operand occurred exactly once.  */
2948
2949         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2950           arg0 = new0;
2951         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2952           arg0 = new1;
2953
2954         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2955           arg1 = new0;
2956         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2957           arg1 = new1;
2958
2959         return fold_build2_loc (loc, code, type, arg0, arg1);
2960       }
2961
2962     default:
2963       return arg;
2964     }
2965 }
2966 \f
2967 /* Return a tree for the case when the result of an expression is RESULT
2968    converted to TYPE and OMITTED was previously an operand of the expression
2969    but is now not needed (e.g., we folded OMITTED * 0).
2970
2971    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
2972    the conversion of RESULT to TYPE.  */
2973
2974 tree
2975 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
2976 {
2977   tree t = fold_convert_loc (loc, type, result);
2978
2979   /* If the resulting operand is an empty statement, just return the omitted
2980      statement casted to void. */
2981   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
2982     return build1_loc (loc, NOP_EXPR, void_type_node,
2983                        fold_ignored_result (omitted));
2984
2985   if (TREE_SIDE_EFFECTS (omitted))
2986     return build2_loc (loc, COMPOUND_EXPR, type,
2987                        fold_ignored_result (omitted), t);
2988
2989   return non_lvalue_loc (loc, t);
2990 }
2991
2992 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
2993
2994 static tree
2995 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
2996                                tree omitted)
2997 {
2998   tree t = fold_convert_loc (loc, type, result);
2999
3000   /* If the resulting operand is an empty statement, just return the omitted
3001      statement casted to void. */
3002   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3003     return build1_loc (loc, NOP_EXPR, void_type_node,
3004                        fold_ignored_result (omitted));
3005
3006   if (TREE_SIDE_EFFECTS (omitted))
3007     return build2_loc (loc, COMPOUND_EXPR, type,
3008                        fold_ignored_result (omitted), t);
3009
3010   return pedantic_non_lvalue_loc (loc, t);
3011 }
3012
3013 /* Return a tree for the case when the result of an expression is RESULT
3014    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3015    of the expression but are now not needed.
3016
3017    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3018    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3019    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3020    just do the conversion of RESULT to TYPE.  */
3021
3022 tree
3023 omit_two_operands_loc (location_t loc, tree type, tree result,
3024                        tree omitted1, tree omitted2)
3025 {
3026   tree t = fold_convert_loc (loc, type, result);
3027
3028   if (TREE_SIDE_EFFECTS (omitted2))
3029     t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3030   if (TREE_SIDE_EFFECTS (omitted1))
3031     t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3032
3033   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3034 }
3035
3036 \f
3037 /* Return a simplified tree node for the truth-negation of ARG.  This
3038    never alters ARG itself.  We assume that ARG is an operation that
3039    returns a truth value (0 or 1).
3040
3041    FIXME: one would think we would fold the result, but it causes
3042    problems with the dominator optimizer.  */
3043
3044 tree
3045 fold_truth_not_expr (location_t loc, tree arg)
3046 {
3047   tree type = TREE_TYPE (arg);
3048   enum tree_code code = TREE_CODE (arg);
3049   location_t loc1, loc2;
3050
3051   /* If this is a comparison, we can simply invert it, except for
3052      floating-point non-equality comparisons, in which case we just
3053      enclose a TRUTH_NOT_EXPR around what we have.  */
3054
3055   if (TREE_CODE_CLASS (code) == tcc_comparison)
3056     {
3057       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3058       if (FLOAT_TYPE_P (op_type)
3059           && flag_trapping_math
3060           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3061           && code != NE_EXPR && code != EQ_EXPR)
3062         return NULL_TREE;
3063
3064       code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3065       if (code == ERROR_MARK)
3066         return NULL_TREE;
3067
3068       return build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3069                          TREE_OPERAND (arg, 1));
3070     }
3071
3072   switch (code)
3073     {
3074     case INTEGER_CST:
3075       return constant_boolean_node (integer_zerop (arg), type);
3076
3077     case TRUTH_AND_EXPR:
3078       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3079       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3080       return build2_loc (loc, TRUTH_OR_EXPR, type,
3081                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3082                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3083
3084     case TRUTH_OR_EXPR:
3085       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3086       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3087       return build2_loc (loc, TRUTH_AND_EXPR, type,
3088                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3089                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3090
3091     case TRUTH_XOR_EXPR:
3092       /* Here we can invert either operand.  We invert the first operand
3093          unless the second operand is a TRUTH_NOT_EXPR in which case our
3094          result is the XOR of the first operand with the inside of the
3095          negation of the second operand.  */
3096
3097       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3098         return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3099                            TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3100       else
3101         return build2_loc (loc, TRUTH_XOR_EXPR, type,
3102                            invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3103                            TREE_OPERAND (arg, 1));
3104
3105     case TRUTH_ANDIF_EXPR:
3106       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3107       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3108       return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3109                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3110                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3111
3112     case TRUTH_ORIF_EXPR:
3113       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3114       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3115       return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3116                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3117                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3118
3119     case TRUTH_NOT_EXPR:
3120       return TREE_OPERAND (arg, 0);
3121
3122     case COND_EXPR:
3123       {
3124         tree arg1 = TREE_OPERAND (arg, 1);
3125         tree arg2 = TREE_OPERAND (arg, 2);
3126
3127         loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3128         loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3129
3130         /* A COND_EXPR may have a throw as one operand, which
3131            then has void type.  Just leave void operands
3132            as they are.  */
3133         return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3134                            VOID_TYPE_P (TREE_TYPE (arg1))
3135                            ? arg1 : invert_truthvalue_loc (loc1, arg1),
3136                            VOID_TYPE_P (TREE_TYPE (arg2))
3137                            ? arg2 : invert_truthvalue_loc (loc2, arg2));
3138       }
3139
3140     case COMPOUND_EXPR:
3141       loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3142       return build2_loc (loc, COMPOUND_EXPR, type,
3143                          TREE_OPERAND (arg, 0),
3144                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3145
3146     case NON_LVALUE_EXPR:
3147       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3148       return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3149
3150     CASE_CONVERT:
3151       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3152         return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3153
3154       /* ... fall through ...  */
3155
3156     case FLOAT_EXPR:
3157       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3158       return build1_loc (loc, TREE_CODE (arg), type,
3159                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3160
3161     case BIT_AND_EXPR:
3162       if (!integer_onep (TREE_OPERAND (arg, 1)))
3163         return NULL_TREE;
3164       return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3165
3166     case SAVE_EXPR:
3167       return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3168
3169     case CLEANUP_POINT_EXPR:
3170       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3171       return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3172                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3173
3174     default:
3175       return NULL_TREE;
3176     }
3177 }
3178
3179 /* Return a simplified tree node for the truth-negation of ARG.  This
3180    never alters ARG itself.  We assume that ARG is an operation that
3181    returns a truth value (0 or 1).
3182
3183    FIXME: one would think we would fold the result, but it causes
3184    problems with the dominator optimizer.  */
3185
3186 tree
3187 invert_truthvalue_loc (location_t loc, tree arg)
3188 {
3189   tree tem;
3190
3191   if (TREE_CODE (arg) == ERROR_MARK)
3192     return arg;
3193
3194   tem = fold_truth_not_expr (loc, arg);
3195   if (!tem)
3196     tem = build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3197
3198   return tem;
3199 }
3200
3201 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3202    operands are another bit-wise operation with a common input.  If so,
3203    distribute the bit operations to save an operation and possibly two if
3204    constants are involved.  For example, convert
3205         (A | B) & (A | C) into A | (B & C)
3206    Further simplification will occur if B and C are constants.
3207
3208    If this optimization cannot be done, 0 will be returned.  */
3209
3210 static tree
3211 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
3212                      tree arg0, tree arg1)
3213 {
3214   tree common;
3215   tree left, right;
3216
3217   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3218       || TREE_CODE (arg0) == code
3219       || (TREE_CODE (arg0) != BIT_AND_EXPR
3220           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3221     return 0;
3222
3223   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3224     {
3225       common = TREE_OPERAND (arg0, 0);
3226       left = TREE_OPERAND (arg0, 1);
3227       right = TREE_OPERAND (arg1, 1);
3228     }
3229   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3230     {
3231       common = TREE_OPERAND (arg0, 0);
3232       left = TREE_OPERAND (arg0, 1);
3233       right = TREE_OPERAND (arg1, 0);
3234     }
3235   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3236     {
3237       common = TREE_OPERAND (arg0, 1);
3238       left = TREE_OPERAND (arg0, 0);
3239       right = TREE_OPERAND (arg1, 1);
3240     }
3241   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3242     {
3243       common = TREE_OPERAND (arg0, 1);
3244       left = TREE_OPERAND (arg0, 0);
3245       right = TREE_OPERAND (arg1, 0);
3246     }
3247   else
3248     return 0;
3249
3250   common = fold_convert_loc (loc, type, common);
3251   left = fold_convert_loc (loc, type, left);
3252   right = fold_convert_loc (loc, type, right);
3253   return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
3254                       fold_build2_loc (loc, code, type, left, right));
3255 }
3256
3257 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3258    with code CODE.  This optimization is unsafe.  */
3259 static tree
3260 distribute_real_division (location_t loc, enum tree_code code, tree type,
3261                           tree arg0, tree arg1)
3262 {
3263   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3264   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3265
3266   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3267   if (mul0 == mul1
3268       && operand_equal_p (TREE_OPERAND (arg0, 1),
3269                        TREE_OPERAND (arg1, 1), 0))
3270     return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3271                         fold_build2_loc (loc, code, type,
3272                                      TREE_OPERAND (arg0, 0),
3273                                      TREE_OPERAND (arg1, 0)),
3274                         TREE_OPERAND (arg0, 1));
3275
3276   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3277   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3278                        TREE_OPERAND (arg1, 0), 0)
3279       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3280       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3281     {
3282       REAL_VALUE_TYPE r0, r1;
3283       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3284       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3285       if (!mul0)
3286         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3287       if (!mul1)
3288         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3289       real_arithmetic (&r0, code, &r0, &r1);
3290       return fold_build2_loc (loc, MULT_EXPR, type,
3291                           TREE_OPERAND (arg0, 0),
3292                           build_real (type, r0));
3293     }
3294
3295   return NULL_TREE;
3296 }
3297 \f
3298 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3299    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3300
3301 static tree
3302 make_bit_field_ref (location_t loc, tree inner, tree type,
3303                     HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3304 {
3305   tree result, bftype;
3306
3307   if (bitpos == 0)
3308     {
3309       tree size = TYPE_SIZE (TREE_TYPE (inner));
3310       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3311            || POINTER_TYPE_P (TREE_TYPE (inner)))
3312           && host_integerp (size, 0)
3313           && tree_low_cst (size, 0) == bitsize)
3314         return fold_convert_loc (loc, type, inner);
3315     }
3316
3317   bftype = type;
3318   if (TYPE_PRECISION (bftype) != bitsize
3319       || TYPE_UNSIGNED (bftype) == !unsignedp)
3320     bftype = build_nonstandard_integer_type (bitsize, 0);
3321
3322   result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3323                        size_int (bitsize), bitsize_int (bitpos));
3324
3325   if (bftype != type)
3326     result = fold_convert_loc (loc, type, result);
3327
3328   return result;
3329 }
3330
3331 /* Optimize a bit-field compare.
3332
3333    There are two cases:  First is a compare against a constant and the
3334    second is a comparison of two items where the fields are at the same
3335    bit position relative to the start of a chunk (byte, halfword, word)
3336    large enough to contain it.  In these cases we can avoid the shift
3337    implicit in bitfield extractions.
3338
3339    For constants, we emit a compare of the shifted constant with the
3340    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3341    compared.  For two fields at the same position, we do the ANDs with the
3342    similar mask and compare the result of the ANDs.
3343
3344    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3345    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3346    are the left and right operands of the comparison, respectively.
3347
3348    If the optimization described above can be done, we return the resulting
3349    tree.  Otherwise we return zero.  */
3350
3351 static tree
3352 optimize_bit_field_compare (location_t loc, enum tree_code code,
3353                             tree compare_type, tree lhs, tree rhs)
3354 {
3355   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3356   tree type = TREE_TYPE (lhs);
3357   tree signed_type, unsigned_type;
3358   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3359   enum machine_mode lmode, rmode, nmode;
3360   int lunsignedp, runsignedp;
3361   int lvolatilep = 0, rvolatilep = 0;
3362   tree linner, rinner = NULL_TREE;
3363   tree mask;
3364   tree offset;
3365
3366   /* Get all the information about the extractions being done.  If the bit size
3367      if the same as the size of the underlying object, we aren't doing an
3368      extraction at all and so can do nothing.  We also don't want to
3369      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3370      then will no longer be able to replace it.  */
3371   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3372                                 &lunsignedp, &lvolatilep, false);
3373   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3374       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3375     return 0;
3376
3377  if (!const_p)
3378    {
3379      /* If this is not a constant, we can only do something if bit positions,
3380         sizes, and signedness are the same.  */
3381      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3382                                    &runsignedp, &rvolatilep, false);
3383
3384      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3385          || lunsignedp != runsignedp || offset != 0
3386          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3387        return 0;
3388    }
3389
3390   /* See if we can find a mode to refer to this field.  We should be able to,
3391      but fail if we can't.  */
3392   if (lvolatilep
3393       && GET_MODE_BITSIZE (lmode) > 0
3394       && flag_strict_volatile_bitfields > 0)
3395     nmode = lmode;
3396   else
3397     nmode = get_best_mode (lbitsize, lbitpos,
3398                            const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3399                            : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3400                                   TYPE_ALIGN (TREE_TYPE (rinner))),
3401                            word_mode, lvolatilep || rvolatilep);
3402   if (nmode == VOIDmode)
3403     return 0;
3404
3405   /* Set signed and unsigned types of the precision of this mode for the
3406      shifts below.  */
3407   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3408   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3409
3410   /* Compute the bit position and size for the new reference and our offset
3411      within it. If the new reference is the same size as the original, we
3412      won't optimize anything, so return zero.  */
3413   nbitsize = GET_MODE_BITSIZE (nmode);
3414   nbitpos = lbitpos & ~ (nbitsize - 1);
3415   lbitpos -= nbitpos;
3416   if (nbitsize == lbitsize)
3417     return 0;
3418
3419   if (BYTES_BIG_ENDIAN)
3420     lbitpos = nbitsize - lbitsize - lbitpos;
3421
3422   /* Make the mask to be used against the extracted field.  */
3423   mask = build_int_cst_type (unsigned_type, -1);
3424   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3425   mask = const_binop (RSHIFT_EXPR, mask,
3426                       size_int (nbitsize - lbitsize - lbitpos));
3427
3428   if (! const_p)
3429     /* If not comparing with constant, just rework the comparison
3430        and return.  */
3431     return fold_build2_loc (loc, code, compare_type,
3432                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3433                                      make_bit_field_ref (loc, linner,
3434                                                          unsigned_type,
3435                                                          nbitsize, nbitpos,
3436                                                          1),
3437                                      mask),
3438                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3439                                      make_bit_field_ref (loc, rinner,
3440                                                          unsigned_type,
3441                                                          nbitsize, nbitpos,
3442                                                          1),
3443                                      mask));
3444
3445   /* Otherwise, we are handling the constant case. See if the constant is too
3446      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
3447      this not only for its own sake, but to avoid having to test for this
3448      error case below.  If we didn't, we might generate wrong code.
3449
3450      For unsigned fields, the constant shifted right by the field length should
3451      be all zero.  For signed fields, the high-order bits should agree with
3452      the sign bit.  */
3453
3454   if (lunsignedp)
3455     {
3456       if (! integer_zerop (const_binop (RSHIFT_EXPR,
3457                                         fold_convert_loc (loc,
3458                                                           unsigned_type, rhs),
3459                                         size_int (lbitsize))))
3460         {
3461           warning (0, "comparison is always %d due to width of bit-field",
3462                    code == NE_EXPR);
3463           return constant_boolean_node (code == NE_EXPR, compare_type);
3464         }
3465     }
3466   else
3467     {
3468       tree tem = const_binop (RSHIFT_EXPR,
3469                               fold_convert_loc (loc, signed_type, rhs),
3470                               size_int (lbitsize - 1));
3471       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3472         {
3473           warning (0, "comparison is always %d due to width of bit-field",
3474                    code == NE_EXPR);
3475           return constant_boolean_node (code == NE_EXPR, compare_type);
3476         }
3477     }
3478
3479   /* Single-bit compares should always be against zero.  */
3480   if (lbitsize == 1 && ! integer_zerop (rhs))
3481     {
3482       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3483       rhs = build_int_cst (type, 0);
3484     }
3485
3486   /* Make a new bitfield reference, shift the constant over the
3487      appropriate number of bits and mask it with the computed mask
3488      (in case this was a signed field).  If we changed it, make a new one.  */
3489   lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3490   if (lvolatilep)
3491     {
3492       TREE_SIDE_EFFECTS (lhs) = 1;
3493       TREE_THIS_VOLATILE (lhs) = 1;
3494     }
3495
3496   rhs = const_binop (BIT_AND_EXPR,
3497                      const_binop (LSHIFT_EXPR,
3498                                   fold_convert_loc (loc, unsigned_type, rhs),
3499                                   size_int (lbitpos)),
3500                      mask);
3501
3502   lhs = build2_loc (loc, code, compare_type,
3503                     build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
3504   return lhs;
3505 }
3506 \f
3507 /* Subroutine for fold_truthop: decode a field reference.
3508
3509    If EXP is a comparison reference, we return the innermost reference.
3510
3511    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3512    set to the starting bit number.
3513
3514    If the innermost field can be completely contained in a mode-sized
3515    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
3516
3517    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3518    otherwise it is not changed.
3519
3520    *PUNSIGNEDP is set to the signedness of the field.
3521
3522    *PMASK is set to the mask used.  This is either contained in a
3523    BIT_AND_EXPR or derived from the width of the field.
3524
3525    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3526
3527    Return 0 if this is not a component reference or is one that we can't
3528    do anything with.  */
3529
3530 static tree
3531 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3532                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3533                         int *punsignedp, int *pvolatilep,
3534                         tree *pmask, tree *pand_mask)
3535 {
3536   tree outer_type = 0;
3537   tree and_mask = 0;
3538   tree mask, inner, offset;
3539   tree unsigned_type;
3540   unsigned int precision;
3541
3542   /* All the optimizations using this function assume integer fields.
3543      There are problems with FP fields since the type_for_size call
3544      below can fail for, e.g., XFmode.  */
3545   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3546     return 0;
3547
3548   /* We are interested in the bare arrangement of bits, so strip everything
3549      that doesn't affect the machine mode.  However, record the type of the
3550      outermost expression if it may matter below.  */
3551   if (CONVERT_EXPR_P (exp)
3552       || TREE_CODE (exp) == NON_LVALUE_EXPR)
3553     outer_type = TREE_TYPE (exp);
3554   STRIP_NOPS (exp);
3555
3556   if (TREE_CODE (exp) == BIT_AND_EXPR)
3557     {
3558       and_mask = TREE_OPERAND (exp, 1);
3559       exp = TREE_OPERAND (exp, 0);
3560       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3561       if (TREE_CODE (and_mask) != INTEGER_CST)
3562         return 0;
3563     }
3564
3565   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3566                                punsignedp, pvolatilep, false);
3567   if ((inner == exp && and_mask == 0)
3568       || *pbitsize < 0 || offset != 0
3569       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3570     return 0;
3571
3572   /* If the number of bits in the reference is the same as the bitsize of
3573      the outer type, then the outer type gives the signedness. Otherwise
3574      (in case of a small bitfield) the signedness is unchanged.  */
3575   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3576     *punsignedp = TYPE_UNSIGNED (outer_type);
3577
3578   /* Compute the mask to access the bitfield.  */
3579   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3580   precision = TYPE_PRECISION (unsigned_type);
3581
3582   mask = build_int_cst_type (unsigned_type, -1);
3583
3584   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3585   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3586
3587   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
3588   if (and_mask != 0)
3589     mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3590                         fold_convert_loc (loc, unsigned_type, and_mask), mask);
3591
3592   *pmask = mask;
3593   *pand_mask = and_mask;
3594   return inner;
3595 }
3596
3597 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3598    bit positions.  */
3599
3600 static int
3601 all_ones_mask_p (const_tree mask, int size)
3602 {
3603   tree type = TREE_TYPE (mask);
3604   unsigned int precision = TYPE_PRECISION (type);
3605   tree tmask;
3606
3607   tmask = build_int_cst_type (signed_type_for (type), -1);
3608
3609   return
3610     tree_int_cst_equal (mask,
3611                         const_binop (RSHIFT_EXPR,
3612                                      const_binop (LSHIFT_EXPR, tmask,
3613                                                   size_int (precision - size)),
3614                                      size_int (precision - size)));
3615 }
3616
3617 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3618    represents the sign bit of EXP's type.  If EXP represents a sign
3619    or zero extension, also test VAL against the unextended type.
3620    The return value is the (sub)expression whose sign bit is VAL,
3621    or NULL_TREE otherwise.  */
3622
3623 static tree
3624 sign_bit_p (tree exp, const_tree val)
3625 {
3626   unsigned HOST_WIDE_INT mask_lo, lo;
3627   HOST_WIDE_INT mask_hi, hi;
3628   int width;
3629   tree t;
3630
3631   /* Tree EXP must have an integral type.  */
3632   t = TREE_TYPE (exp);
3633   if (! INTEGRAL_TYPE_P (t))
3634     return NULL_TREE;
3635
3636   /* Tree VAL must be an integer constant.  */
3637   if (TREE_CODE (val) != INTEGER_CST
3638       || TREE_OVERFLOW (val))
3639     return NULL_TREE;
3640
3641   width = TYPE_PRECISION (t);
3642   if (width > HOST_BITS_PER_WIDE_INT)
3643     {
3644       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3645       lo = 0;
3646
3647       mask_hi = ((unsigned HOST_WIDE_INT) -1
3648                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
3649       mask_lo = -1;
3650     }
3651   else
3652     {
3653       hi = 0;
3654       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3655
3656       mask_hi = 0;
3657       mask_lo = ((unsigned HOST_WIDE_INT) -1
3658                  >> (HOST_BITS_PER_WIDE_INT - width));
3659     }
3660
3661   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3662      treat VAL as if it were unsigned.  */
3663   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3664       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3665     return exp;
3666
3667   /* Handle extension from a narrower type.  */
3668   if (TREE_CODE (exp) == NOP_EXPR
3669       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3670     return sign_bit_p (TREE_OPERAND (exp, 0), val);
3671
3672   return NULL_TREE;
3673 }
3674
3675 /* Subroutine for fold_truthop: determine if an operand is simple enough
3676    to be evaluated unconditionally.  */
3677
3678 static int
3679 simple_operand_p (const_tree exp)
3680 {
3681   /* Strip any conversions that don't change the machine mode.  */
3682   STRIP_NOPS (exp);
3683
3684   return (CONSTANT_CLASS_P (exp)
3685           || TREE_CODE (exp) == SSA_NAME
3686           || (DECL_P (exp)
3687               && ! TREE_ADDRESSABLE (exp)
3688               && ! TREE_THIS_VOLATILE (exp)
3689               && ! DECL_NONLOCAL (exp)
3690               /* Don't regard global variables as simple.  They may be
3691                  allocated in ways unknown to the compiler (shared memory,
3692                  #pragma weak, etc).  */
3693               && ! TREE_PUBLIC (exp)
3694               && ! DECL_EXTERNAL (exp)
3695               /* Loading a static variable is unduly expensive, but global
3696                  registers aren't expensive.  */
3697               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3698 }
3699 \f
3700 /* The following functions are subroutines to fold_range_test and allow it to
3701    try to change a logical combination of comparisons into a range test.
3702
3703    For example, both
3704         X == 2 || X == 3 || X == 4 || X == 5
3705    and
3706         X >= 2 && X <= 5
3707    are converted to
3708         (unsigned) (X - 2) <= 3
3709
3710    We describe each set of comparisons as being either inside or outside
3711    a range, using a variable named like IN_P, and then describe the
3712    range with a lower and upper bound.  If one of the bounds is omitted,
3713    it represents either the highest or lowest value of the type.
3714
3715    In the comments below, we represent a range by two numbers in brackets
3716    preceded by a "+" to designate being inside that range, or a "-" to
3717    designate being outside that range, so the condition can be inverted by
3718    flipping the prefix.  An omitted bound is represented by a "-".  For
3719    example, "- [-, 10]" means being outside the range starting at the lowest
3720    possible value and ending at 10, in other words, being greater than 10.
3721    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3722    always false.
3723
3724    We set up things so that the missing bounds are handled in a consistent
3725    manner so neither a missing bound nor "true" and "false" need to be
3726    handled using a special case.  */
3727
3728 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3729    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3730    and UPPER1_P are nonzero if the respective argument is an upper bound
3731    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
3732    must be specified for a comparison.  ARG1 will be converted to ARG0's
3733    type if both are specified.  */
3734
3735 static tree
3736 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3737              tree arg1, int upper1_p)
3738 {
3739   tree tem;
3740   int result;
3741   int sgn0, sgn1;
3742
3743   /* If neither arg represents infinity, do the normal operation.
3744      Else, if not a comparison, return infinity.  Else handle the special
3745      comparison rules. Note that most of the cases below won't occur, but
3746      are handled for consistency.  */
3747
3748   if (arg0 != 0 && arg1 != 0)
3749     {
3750       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3751                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
3752       STRIP_NOPS (tem);
3753       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3754     }
3755
3756   if (TREE_CODE_CLASS (code) != tcc_comparison)
3757     return 0;
3758
3759   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3760      for neither.  In real maths, we cannot assume open ended ranges are
3761      the same. But, this is computer arithmetic, where numbers are finite.
3762      We can therefore make the transformation of any unbounded range with
3763      the value Z, Z being greater than any representable number. This permits
3764      us to treat unbounded ranges as equal.  */
3765   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3766   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3767   switch (code)
3768     {
3769     case EQ_EXPR:
3770       result = sgn0 == sgn1;
3771       break;
3772     case NE_EXPR:
3773       result = sgn0 != sgn1;
3774       break;
3775     case LT_EXPR:
3776       result = sgn0 < sgn1;
3777       break;
3778     case LE_EXPR:
3779       result = sgn0 <= sgn1;
3780       break;
3781     case GT_EXPR:
3782       result = sgn0 > sgn1;
3783       break;
3784     case GE_EXPR:
3785       result = sgn0 >= sgn1;
3786       break;
3787     default:
3788       gcc_unreachable ();
3789     }
3790
3791   return constant_boolean_node (result, type);
3792 }
3793 \f
3794 /* Given EXP, a logical expression, set the range it is testing into
3795    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
3796    actually being tested.  *PLOW and *PHIGH will be made of the same
3797    type as the returned expression.  If EXP is not a comparison, we
3798    will most likely not be returning a useful value and range.  Set
3799    *STRICT_OVERFLOW_P to true if the return value is only valid
3800    because signed overflow is undefined; otherwise, do not change
3801    *STRICT_OVERFLOW_P.  */
3802
3803 tree
3804 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
3805             bool *strict_overflow_p)
3806 {
3807   enum tree_code code;
3808   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
3809   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
3810   int in_p, n_in_p;
3811   tree low, high, n_low, n_high;
3812   location_t loc = EXPR_LOCATION (exp);
3813
3814   /* Start with simply saying "EXP != 0" and then look at the code of EXP
3815      and see if we can refine the range.  Some of the cases below may not
3816      happen, but it doesn't seem worth worrying about this.  We "continue"
3817      the outer loop when we've changed something; otherwise we "break"
3818      the switch, which will "break" the while.  */
3819
3820   in_p = 0;
3821   low = high = build_int_cst (TREE_TYPE (exp), 0);
3822
3823   while (1)
3824     {
3825       code = TREE_CODE (exp);
3826       exp_type = TREE_TYPE (exp);
3827
3828       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3829         {
3830           if (TREE_OPERAND_LENGTH (exp) > 0)
3831             arg0 = TREE_OPERAND (exp, 0);
3832           if (TREE_CODE_CLASS (code) == tcc_comparison
3833               || TREE_CODE_CLASS (code) == tcc_unary
3834               || TREE_CODE_CLASS (code) == tcc_binary)
3835             arg0_type = TREE_TYPE (arg0);
3836           if (TREE_CODE_CLASS (code) == tcc_binary
3837               || TREE_CODE_CLASS (code) == tcc_comparison
3838               || (TREE_CODE_CLASS (code) == tcc_expression
3839                   && TREE_OPERAND_LENGTH (exp) > 1))
3840             arg1 = TREE_OPERAND (exp, 1);
3841         }
3842
3843       switch (code)
3844         {
3845         case TRUTH_NOT_EXPR:
3846           in_p = ! in_p, exp = arg0;
3847           continue;
3848
3849         case EQ_EXPR: case NE_EXPR:
3850         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3851           /* We can only do something if the range is testing for zero
3852              and if the second operand is an integer constant.  Note that
3853              saying something is "in" the range we make is done by
3854              complementing IN_P since it will set in the initial case of
3855              being not equal to zero; "out" is leaving it alone.  */
3856           if (low == 0 || high == 0
3857               || ! integer_zerop (low) || ! integer_zerop (high)
3858               || TREE_CODE (arg1) != INTEGER_CST)
3859             break;
3860
3861           switch (code)
3862             {
3863             case NE_EXPR:  /* - [c, c]  */
3864               low = high = arg1;
3865               break;
3866             case EQ_EXPR:  /* + [c, c]  */
3867               in_p = ! in_p, low = high = arg1;
3868               break;
3869             case GT_EXPR:  /* - [-, c] */
3870               low = 0, high = arg1;
3871               break;
3872             case GE_EXPR:  /* + [c, -] */
3873               in_p = ! in_p, low = arg1, high = 0;
3874               break;
3875             case LT_EXPR:  /* - [c, -] */
3876               low = arg1, high = 0;
3877               break;
3878             case LE_EXPR:  /* + [-, c] */
3879               in_p = ! in_p, low = 0, high = arg1;
3880               break;
3881             default:
3882               gcc_unreachable ();
3883             }
3884
3885           /* If this is an unsigned comparison, we also know that EXP is
3886              greater than or equal to zero.  We base the range tests we make
3887              on that fact, so we record it here so we can parse existing
3888              range tests.  We test arg0_type since often the return type
3889              of, e.g. EQ_EXPR, is boolean.  */
3890           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3891             {
3892               if (! merge_ranges (&n_in_p, &n_low, &n_high,
3893                                   in_p, low, high, 1,
3894                                   build_int_cst (arg0_type, 0),
3895                                   NULL_TREE))
3896                 break;
3897
3898               in_p = n_in_p, low = n_low, high = n_high;
3899
3900               /* If the high bound is missing, but we have a nonzero low
3901                  bound, reverse the range so it goes from zero to the low bound
3902                  minus 1.  */
3903               if (high == 0 && low && ! integer_zerop (low))
3904                 {
3905                   in_p = ! in_p;
3906                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3907                                       integer_one_node, 0);
3908                   low = build_int_cst (arg0_type, 0);
3909                 }
3910             }
3911
3912           exp = arg0;
3913           continue;
3914
3915         case NEGATE_EXPR:
3916           /* (-x) IN [a,b] -> x in [-b, -a]  */
3917           n_low = range_binop (MINUS_EXPR, exp_type,
3918                                build_int_cst (exp_type, 0),
3919                                0, high, 1);
3920           n_high = range_binop (MINUS_EXPR, exp_type,
3921                                 build_int_cst (exp_type, 0),
3922                                 0, low, 0);
3923           if (n_high != 0 && TREE_OVERFLOW (n_high))
3924             break;
3925           goto normalize;
3926
3927         case BIT_NOT_EXPR:
3928           /* ~ X -> -X - 1  */
3929           exp = build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
3930                             build_int_cst (exp_type, 1));
3931           continue;
3932
3933         case PLUS_EXPR:  case MINUS_EXPR:
3934           if (TREE_CODE (arg1) != INTEGER_CST)
3935             break;
3936
3937           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
3938              move a constant to the other side.  */
3939           if (!TYPE_UNSIGNED (arg0_type)
3940               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
3941             break;
3942
3943           /* If EXP is signed, any overflow in the computation is undefined,
3944              so we don't worry about it so long as our computations on
3945              the bounds don't overflow.  For unsigned, overflow is defined
3946              and this is exactly the right thing.  */
3947           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3948                                arg0_type, low, 0, arg1, 0);
3949           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3950                                 arg0_type, high, 1, arg1, 0);
3951           if ((n_low != 0 && TREE_OVERFLOW (n_low))
3952               || (n_high != 0 && TREE_OVERFLOW (n_high)))
3953             break;
3954
3955           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
3956             *strict_overflow_p = true;
3957
3958         normalize:
3959           /* Check for an unsigned range which has wrapped around the maximum
3960              value thus making n_high < n_low, and normalize it.  */
3961           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3962             {
3963               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
3964                                  integer_one_node, 0);
3965               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
3966                                   integer_one_node, 0);
3967
3968               /* If the range is of the form +/- [ x+1, x ], we won't
3969                  be able to normalize it.  But then, it represents the
3970                  whole range or the empty set, so make it
3971                  +/- [ -, - ].  */
3972               if (tree_int_cst_equal (n_low, low)
3973                   && tree_int_cst_equal (n_high, high))
3974                 low = high = 0;
3975               else
3976                 in_p = ! in_p;
3977             }
3978           else
3979             low = n_low, high = n_high;
3980
3981           exp = arg0;
3982           continue;
3983
3984         CASE_CONVERT: case NON_LVALUE_EXPR:
3985           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
3986             break;
3987
3988           if (! INTEGRAL_TYPE_P (arg0_type)
3989               || (low != 0 && ! int_fits_type_p (low, arg0_type))
3990               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
3991             break;
3992
3993           n_low = low, n_high = high;
3994
3995           if (n_low != 0)
3996             n_low = fold_convert_loc (loc, arg0_type, n_low);
3997
3998           if (n_high != 0)
3999             n_high = fold_convert_loc (loc, arg0_type, n_high);
4000
4001
4002           /* If we're converting arg0 from an unsigned type, to exp,
4003              a signed type,  we will be doing the comparison as unsigned.
4004              The tests above have already verified that LOW and HIGH
4005              are both positive.
4006
4007              So we have to ensure that we will handle large unsigned
4008              values the same way that the current signed bounds treat
4009              negative values.  */
4010
4011           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4012             {
4013               tree high_positive;
4014               tree equiv_type;
4015               /* For fixed-point modes, we need to pass the saturating flag
4016                  as the 2nd parameter.  */
4017               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4018                 equiv_type = lang_hooks.types.type_for_mode
4019                              (TYPE_MODE (arg0_type),
4020                               TYPE_SATURATING (arg0_type));
4021               else
4022                 equiv_type = lang_hooks.types.type_for_mode
4023                              (TYPE_MODE (arg0_type), 1);
4024
4025               /* A range without an upper bound is, naturally, unbounded.
4026                  Since convert would have cropped a very large value, use
4027                  the max value for the destination type.  */
4028               high_positive
4029                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4030                 : TYPE_MAX_VALUE (arg0_type);
4031
4032               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4033                 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4034                                              fold_convert_loc (loc, arg0_type,
4035                                                                high_positive),
4036                                              build_int_cst (arg0_type, 1));
4037
4038               /* If the low bound is specified, "and" the range with the
4039                  range for which the original unsigned value will be
4040                  positive.  */
4041               if (low != 0)
4042                 {
4043                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4044                                       1, n_low, n_high, 1,
4045                                       fold_convert_loc (loc, arg0_type,
4046                                                         integer_zero_node),
4047                                       high_positive))
4048                     break;
4049
4050                   in_p = (n_in_p == in_p);
4051                 }
4052               else
4053                 {
4054                   /* Otherwise, "or" the range with the range of the input
4055                      that will be interpreted as negative.  */
4056                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4057                                       0, n_low, n_high, 1,
4058                                       fold_convert_loc (loc, arg0_type,
4059                                                         integer_zero_node),
4060                                       high_positive))
4061                     break;
4062
4063                   in_p = (in_p != n_in_p);
4064                 }
4065             }
4066
4067           exp = arg0;
4068           low = n_low, high = n_high;
4069           continue;
4070
4071         default:
4072           break;
4073         }
4074
4075       break;
4076     }
4077
4078   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4079   if (TREE_CODE (exp) == INTEGER_CST)
4080     {
4081       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4082                                                  exp, 0, low, 0))
4083                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4084                                                     exp, 1, high, 1)));
4085       low = high = 0;
4086       exp = 0;
4087     }
4088
4089   *pin_p = in_p, *plow = low, *phigh = high;
4090   return exp;
4091 }
4092 \f
4093 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4094    type, TYPE, return an expression to test if EXP is in (or out of, depending
4095    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4096
4097 tree
4098 build_range_check (location_t loc, tree type, tree exp, int in_p,
4099                    tree low, tree high)
4100 {
4101   tree etype = TREE_TYPE (exp), value;
4102
4103 #ifdef HAVE_canonicalize_funcptr_for_compare
4104   /* Disable this optimization for function pointer expressions
4105      on targets that require function pointer canonicalization.  */
4106   if (HAVE_canonicalize_funcptr_for_compare
4107       && TREE_CODE (etype) == POINTER_TYPE
4108       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4109     return NULL_TREE;
4110 #endif
4111
4112   if (! in_p)
4113     {
4114       value = build_range_check (loc, type, exp, 1, low, high);
4115       if (value != 0)
4116         return invert_truthvalue_loc (loc, value);
4117
4118       return 0;
4119     }
4120
4121   if (low == 0 && high == 0)
4122     return build_int_cst (type, 1);
4123
4124   if (low == 0)
4125     return fold_build2_loc (loc, LE_EXPR, type, exp,
4126                         fold_convert_loc (loc, etype, high));
4127
4128   if (high == 0)
4129     return fold_build2_loc (loc, GE_EXPR, type, exp,
4130                         fold_convert_loc (loc, etype, low));
4131
4132   if (operand_equal_p (low, high, 0))
4133     return fold_build2_loc (loc, EQ_EXPR, type, exp,
4134                         fold_convert_loc (loc, etype, low));
4135
4136   if (integer_zerop (low))
4137     {
4138       if (! TYPE_UNSIGNED (etype))
4139         {
4140           etype = unsigned_type_for (etype);
4141           high = fold_convert_loc (loc, etype, high);
4142           exp = fold_convert_loc (loc, etype, exp);
4143         }
4144       return build_range_check (loc, type, exp, 1, 0, high);
4145     }
4146
4147   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4148   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4149     {
4150       unsigned HOST_WIDE_INT lo;
4151       HOST_WIDE_INT hi;
4152       int prec;
4153
4154       prec = TYPE_PRECISION (etype);
4155       if (prec <= HOST_BITS_PER_WIDE_INT)
4156         {
4157           hi = 0;
4158           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4159         }
4160       else
4161         {
4162           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4163           lo = (unsigned HOST_WIDE_INT) -1;
4164         }
4165
4166       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4167         {
4168           if (TYPE_UNSIGNED (etype))
4169             {
4170               tree signed_etype = signed_type_for (etype);
4171               if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4172                 etype
4173                   = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4174               else
4175                 etype = signed_etype;
4176               exp = fold_convert_loc (loc, etype, exp);
4177             }
4178           return fold_build2_loc (loc, GT_EXPR, type, exp,
4179                               build_int_cst (etype, 0));
4180         }
4181     }
4182
4183   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4184      This requires wrap-around arithmetics for the type of the expression.
4185      First make sure that arithmetics in this type is valid, then make sure
4186      that it wraps around.  */
4187   if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4188     etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4189                                             TYPE_UNSIGNED (etype));
4190
4191   if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4192     {
4193       tree utype, minv, maxv;
4194
4195       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4196          for the type in question, as we rely on this here.  */
4197       utype = unsigned_type_for (etype);
4198       maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4199       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4200                           integer_one_node, 1);
4201       minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4202
4203       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4204                                       minv, 1, maxv, 1)))
4205         etype = utype;
4206       else
4207         return 0;
4208     }
4209
4210   high = fold_convert_loc (loc, etype, high);
4211   low = fold_convert_loc (loc, etype, low);
4212   exp = fold_convert_loc (loc, etype, exp);
4213
4214   value = const_binop (MINUS_EXPR, high, low);
4215
4216
4217   if (POINTER_TYPE_P (etype))
4218     {
4219       if (value != 0 && !TREE_OVERFLOW (value))
4220         {
4221           low = fold_convert_loc (loc, sizetype, low);
4222           low = fold_build1_loc (loc, NEGATE_EXPR, sizetype, low);
4223           return build_range_check (loc, type,
4224                                     fold_build2_loc (loc, POINTER_PLUS_EXPR,
4225                                                  etype, exp, low),
4226                                     1, build_int_cst (etype, 0), value);
4227         }
4228       return 0;
4229     }
4230
4231   if (value != 0 && !TREE_OVERFLOW (value))
4232     return build_range_check (loc, type,
4233                               fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4234                               1, build_int_cst (etype, 0), value);
4235
4236   return 0;
4237 }
4238 \f
4239 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4240
4241 static tree
4242 range_predecessor (tree val)
4243 {
4244   tree type = TREE_TYPE (val);
4245
4246   if (INTEGRAL_TYPE_P (type)
4247       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4248     return 0;
4249   else
4250     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4251 }
4252
4253 /* Return the successor of VAL in its type, handling the infinite case.  */
4254
4255 static tree
4256 range_successor (tree val)
4257 {
4258   tree type = TREE_TYPE (val);
4259
4260   if (INTEGRAL_TYPE_P (type)
4261       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4262     return 0;
4263   else
4264     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4265 }
4266
4267 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4268    can, 0 if we can't.  Set the output range into the specified parameters.  */
4269
4270 bool
4271 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4272               tree high0, int in1_p, tree low1, tree high1)
4273 {
4274   int no_overlap;
4275   int subset;
4276   int temp;
4277   tree tem;
4278   int in_p;
4279   tree low, high;
4280   int lowequal = ((low0 == 0 && low1 == 0)
4281                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4282                                                 low0, 0, low1, 0)));
4283   int highequal = ((high0 == 0 && high1 == 0)
4284                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4285                                                  high0, 1, high1, 1)));
4286
4287   /* Make range 0 be the range that starts first, or ends last if they
4288      start at the same value.  Swap them if it isn't.  */
4289   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4290                                  low0, 0, low1, 0))
4291       || (lowequal
4292           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4293                                         high1, 1, high0, 1))))
4294     {
4295       temp = in0_p, in0_p = in1_p, in1_p = temp;
4296       tem = low0, low0 = low1, low1 = tem;
4297       tem = high0, high0 = high1, high1 = tem;
4298     }
4299
4300   /* Now flag two cases, whether the ranges are disjoint or whether the
4301      second range is totally subsumed in the first.  Note that the tests
4302      below are simplified by the ones above.  */
4303   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4304                                           high0, 1, low1, 0));
4305   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4306                                       high1, 1, high0, 1));
4307
4308   /* We now have four cases, depending on whether we are including or
4309      excluding the two ranges.  */
4310   if (in0_p && in1_p)
4311     {
4312       /* If they don't overlap, the result is false.  If the second range
4313          is a subset it is the result.  Otherwise, the range is from the start
4314          of the second to the end of the first.  */
4315       if (no_overlap)
4316         in_p = 0, low = high = 0;
4317       else if (subset)
4318         in_p = 1, low = low1, high = high1;
4319       else
4320         in_p = 1, low = low1, high = high0;
4321     }
4322
4323   else if (in0_p && ! in1_p)
4324     {
4325       /* If they don't overlap, the result is the first range.  If they are
4326          equal, the result is false.  If the second range is a subset of the
4327          first, and the ranges begin at the same place, we go from just after
4328          the end of the second range to the end of the first.  If the second
4329          range is not a subset of the first, or if it is a subset and both
4330          ranges end at the same place, the range starts at the start of the
4331          first range and ends just before the second range.
4332          Otherwise, we can't describe this as a single range.  */
4333       if (no_overlap)
4334         in_p = 1, low = low0, high = high0;
4335       else if (lowequal && highequal)
4336         in_p = 0, low = high = 0;
4337       else if (subset && lowequal)
4338         {
4339           low = range_successor (high1);
4340           high = high0;
4341           in_p = 1;
4342           if (low == 0)
4343             {
4344               /* We are in the weird situation where high0 > high1 but
4345                  high1 has no successor.  Punt.  */
4346               return 0;
4347             }
4348         }
4349       else if (! subset || highequal)
4350         {
4351           low = low0;
4352           high = range_predecessor (low1);
4353           in_p = 1;
4354           if (high == 0)
4355             {
4356               /* low0 < low1 but low1 has no predecessor.  Punt.  */
4357               return 0;
4358             }
4359         }
4360       else
4361         return 0;
4362     }
4363
4364   else if (! in0_p && in1_p)
4365     {
4366       /* If they don't overlap, the result is the second range.  If the second
4367          is a subset of the first, the result is false.  Otherwise,
4368          the range starts just after the first range and ends at the
4369          end of the second.  */
4370       if (no_overlap)
4371         in_p = 1, low = low1, high = high1;
4372       else if (subset || highequal)
4373         in_p = 0, low = high = 0;
4374       else
4375         {
4376           low = range_successor (high0);
4377           high = high1;
4378           in_p = 1;
4379           if (low == 0)
4380             {
4381               /* high1 > high0 but high0 has no successor.  Punt.  */
4382               return 0;
4383             }
4384         }
4385     }
4386
4387   else
4388     {
4389       /* The case where we are excluding both ranges.  Here the complex case
4390          is if they don't overlap.  In that case, the only time we have a
4391          range is if they are adjacent.  If the second is a subset of the
4392          first, the result is the first.  Otherwise, the range to exclude
4393          starts at the beginning of the first range and ends at the end of the
4394          second.  */
4395       if (no_overlap)
4396         {
4397           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4398                                          range_successor (high0),
4399                                          1, low1, 0)))
4400             in_p = 0, low = low0, high = high1;
4401           else
4402             {
4403               /* Canonicalize - [min, x] into - [-, x].  */
4404               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4405                 switch (TREE_CODE (TREE_TYPE (low0)))
4406                   {
4407                   case ENUMERAL_TYPE:
4408                     if (TYPE_PRECISION (TREE_TYPE (low0))
4409                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4410                       break;
4411                     /* FALLTHROUGH */
4412                   case INTEGER_TYPE:
4413                     if (tree_int_cst_equal (low0,
4414                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4415                       low0 = 0;
4416                     break;
4417                   case POINTER_TYPE:
4418                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4419                         && integer_zerop (low0))
4420                       low0 = 0;
4421                     break;
4422                   default:
4423                     break;
4424                   }
4425
4426               /* Canonicalize - [x, max] into - [x, -].  */
4427               if (high1 && TREE_CODE (high1) == INTEGER_CST)
4428                 switch (TREE_CODE (TREE_TYPE (high1)))
4429                   {
4430                   case ENUMERAL_TYPE:
4431                     if (TYPE_PRECISION (TREE_TYPE (high1))
4432                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4433                       break;
4434                     /* FALLTHROUGH */
4435                   case INTEGER_TYPE:
4436                     if (tree_int_cst_equal (high1,
4437                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
4438                       high1 = 0;
4439                     break;
4440                   case POINTER_TYPE:
4441                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
4442                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4443                                                        high1, 1,
4444                                                        integer_one_node, 1)))
4445                       high1 = 0;
4446                     break;
4447                   default:
4448                     break;
4449                   }
4450
4451               /* The ranges might be also adjacent between the maximum and
4452                  minimum values of the given type.  For
4453                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4454                  return + [x + 1, y - 1].  */
4455               if (low0 == 0 && high1 == 0)
4456                 {
4457                   low = range_successor (high0);
4458                   high = range_predecessor (low1);
4459                   if (low == 0 || high == 0)
4460                     return 0;
4461
4462                   in_p = 1;
4463                 }
4464               else
4465                 return 0;
4466             }
4467         }
4468       else if (subset)
4469         in_p = 0, low = low0, high = high0;
4470       else
4471         in_p = 0, low = low0, high = high1;
4472     }
4473
4474   *pin_p = in_p, *plow = low, *phigh = high;
4475   return 1;
4476 }
4477 \f
4478
4479 /* Subroutine of fold, looking inside expressions of the form
4480    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4481    of the COND_EXPR.  This function is being used also to optimize
4482    A op B ? C : A, by reversing the comparison first.
4483
4484    Return a folded expression whose code is not a COND_EXPR
4485    anymore, or NULL_TREE if no folding opportunity is found.  */
4486
4487 static tree
4488 fold_cond_expr_with_comparison (location_t loc, tree type,
4489                                 tree arg0, tree arg1, tree arg2)
4490 {
4491   enum tree_code comp_code = TREE_CODE (arg0);
4492   tree arg00 = TREE_OPERAND (arg0, 0);
4493   tree arg01 = TREE_OPERAND (arg0, 1);
4494   tree arg1_type = TREE_TYPE (arg1);
4495   tree tem;
4496
4497   STRIP_NOPS (arg1);
4498   STRIP_NOPS (arg2);
4499
4500   /* If we have A op 0 ? A : -A, consider applying the following
4501      transformations:
4502
4503      A == 0? A : -A    same as -A
4504      A != 0? A : -A    same as A
4505      A >= 0? A : -A    same as abs (A)
4506      A > 0?  A : -A    same as abs (A)
4507      A <= 0? A : -A    same as -abs (A)
4508      A < 0?  A : -A    same as -abs (A)
4509
4510      None of these transformations work for modes with signed
4511      zeros.  If A is +/-0, the first two transformations will
4512      change the sign of the result (from +0 to -0, or vice
4513      versa).  The last four will fix the sign of the result,
4514      even though the original expressions could be positive or
4515      negative, depending on the sign of A.
4516
4517      Note that all these transformations are correct if A is
4518      NaN, since the two alternatives (A and -A) are also NaNs.  */
4519   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4520       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4521           ? real_zerop (arg01)
4522           : integer_zerop (arg01))
4523       && ((TREE_CODE (arg2) == NEGATE_EXPR
4524            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4525              /* In the case that A is of the form X-Y, '-A' (arg2) may
4526                 have already been folded to Y-X, check for that. */
4527           || (TREE_CODE (arg1) == MINUS_EXPR
4528               && TREE_CODE (arg2) == MINUS_EXPR
4529               && operand_equal_p (TREE_OPERAND (arg1, 0),
4530                                   TREE_OPERAND (arg2, 1), 0)
4531               && operand_equal_p (TREE_OPERAND (arg1, 1),
4532                                   TREE_OPERAND (arg2, 0), 0))))
4533     switch (comp_code)
4534       {
4535       case EQ_EXPR:
4536       case UNEQ_EXPR:
4537         tem = fold_convert_loc (loc, arg1_type, arg1);
4538         return pedantic_non_lvalue_loc (loc,
4539                                     fold_convert_loc (loc, type,
4540                                                   negate_expr (tem)));
4541       case NE_EXPR:
4542       case LTGT_EXPR:
4543         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4544       case UNGE_EXPR:
4545       case UNGT_EXPR:
4546         if (flag_trapping_math)
4547           break;
4548         /* Fall through.  */
4549       case GE_EXPR:
4550       case GT_EXPR:
4551         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4552           arg1 = fold_convert_loc (loc, signed_type_for
4553                                (TREE_TYPE (arg1)), arg1);
4554         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4555         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4556       case UNLE_EXPR:
4557       case UNLT_EXPR:
4558         if (flag_trapping_math)
4559           break;
4560       case LE_EXPR:
4561       case LT_EXPR:
4562         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4563           arg1 = fold_convert_loc (loc, signed_type_for
4564                                (TREE_TYPE (arg1)), arg1);
4565         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4566         return negate_expr (fold_convert_loc (loc, type, tem));
4567       default:
4568         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4569         break;
4570       }
4571
4572   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
4573      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
4574      both transformations are correct when A is NaN: A != 0
4575      is then true, and A == 0 is false.  */
4576
4577   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4578       && integer_zerop (arg01) && integer_zerop (arg2))
4579     {
4580       if (comp_code == NE_EXPR)
4581         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4582       else if (comp_code == EQ_EXPR)
4583         return build_int_cst (type, 0);
4584     }
4585
4586   /* Try some transformations of A op B ? A : B.
4587
4588      A == B? A : B    same as B
4589      A != B? A : B    same as A
4590      A >= B? A : B    same as max (A, B)
4591      A > B?  A : B    same as max (B, A)
4592      A <= B? A : B    same as min (A, B)
4593      A < B?  A : B    same as min (B, A)
4594
4595      As above, these transformations don't work in the presence
4596      of signed zeros.  For example, if A and B are zeros of
4597      opposite sign, the first two transformations will change
4598      the sign of the result.  In the last four, the original
4599      expressions give different results for (A=+0, B=-0) and
4600      (A=-0, B=+0), but the transformed expressions do not.
4601
4602      The first two transformations are correct if either A or B
4603      is a NaN.  In the first transformation, the condition will
4604      be false, and B will indeed be chosen.  In the case of the
4605      second transformation, the condition A != B will be true,
4606      and A will be chosen.
4607
4608      The conversions to max() and min() are not correct if B is
4609      a number and A is not.  The conditions in the original
4610      expressions will be false, so all four give B.  The min()
4611      and max() versions would give a NaN instead.  */
4612   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4613       && operand_equal_for_comparison_p (arg01, arg2, arg00)
4614       /* Avoid these transformations if the COND_EXPR may be used
4615          as an lvalue in the C++ front-end.  PR c++/19199.  */
4616       && (in_gimple_form
4617           || (strcmp (lang_hooks.name, "GNU C++") != 0
4618               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4619           || ! maybe_lvalue_p (arg1)
4620           || ! maybe_lvalue_p (arg2)))
4621     {
4622       tree comp_op0 = arg00;
4623       tree comp_op1 = arg01;
4624       tree comp_type = TREE_TYPE (comp_op0);
4625
4626       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
4627       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4628         {
4629           comp_type = type;
4630           comp_op0 = arg1;
4631           comp_op1 = arg2;
4632         }
4633
4634       switch (comp_code)
4635         {
4636         case EQ_EXPR:
4637           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
4638         case NE_EXPR:
4639           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4640         case LE_EXPR:
4641         case LT_EXPR:
4642         case UNLE_EXPR:
4643         case UNLT_EXPR:
4644           /* In C++ a ?: expression can be an lvalue, so put the
4645              operand which will be used if they are equal first
4646              so that we can convert this back to the
4647              corresponding COND_EXPR.  */
4648           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4649             {
4650               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4651               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4652               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4653                     ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
4654                     : fold_build2_loc (loc, MIN_EXPR, comp_type,
4655                                    comp_op1, comp_op0);
4656               return pedantic_non_lvalue_loc (loc,
4657                                           fold_convert_loc (loc, type, tem));
4658             }
4659           break;
4660         case GE_EXPR:
4661         case GT_EXPR:
4662         case UNGE_EXPR:
4663         case UNGT_EXPR:
4664           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4665             {
4666               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4667               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4668               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4669                     ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
4670                     : fold_build2_loc (loc, MAX_EXPR, comp_type,
4671                                    comp_op1, comp_op0);
4672               return pedantic_non_lvalue_loc (loc,
4673                                           fold_convert_loc (loc, type, tem));
4674             }
4675           break;
4676         case UNEQ_EXPR:
4677           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4678             return pedantic_non_lvalue_loc (loc,
4679                                         fold_convert_loc (loc, type, arg2));
4680           break;
4681         case LTGT_EXPR:
4682           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4683             return pedantic_non_lvalue_loc (loc,
4684                                         fold_convert_loc (loc, type, arg1));
4685           break;
4686         default:
4687           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4688           break;
4689         }
4690     }
4691
4692   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4693      we might still be able to simplify this.  For example,
4694      if C1 is one less or one more than C2, this might have started
4695      out as a MIN or MAX and been transformed by this function.
4696      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
4697
4698   if (INTEGRAL_TYPE_P (type)
4699       && TREE_CODE (arg01) == INTEGER_CST
4700       && TREE_CODE (arg2) == INTEGER_CST)
4701     switch (comp_code)
4702       {
4703       case EQ_EXPR:
4704         if (TREE_CODE (arg1) == INTEGER_CST)
4705           break;
4706         /* We can replace A with C1 in this case.  */
4707         arg1 = fold_convert_loc (loc, type, arg01);
4708         return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
4709
4710       case LT_EXPR:
4711         /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
4712            MIN_EXPR, to preserve the signedness of the comparison.  */
4713         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4714                                OEP_ONLY_CONST)
4715             && operand_equal_p (arg01,
4716                                 const_binop (PLUS_EXPR, arg2,
4717                                              build_int_cst (type, 1)),
4718                                 OEP_ONLY_CONST))
4719           {
4720             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4721                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4722                                                      arg2));
4723             return pedantic_non_lvalue_loc (loc,
4724                                             fold_convert_loc (loc, type, tem));
4725           }
4726         break;
4727
4728       case LE_EXPR:
4729         /* If C1 is C2 - 1, this is min(A, C2), with the same care
4730            as above.  */
4731         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4732                                OEP_ONLY_CONST)
4733             && operand_equal_p (arg01,
4734                                 const_binop (MINUS_EXPR, arg2,
4735                                              build_int_cst (type, 1)),
4736                                 OEP_ONLY_CONST))
4737           {
4738             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4739                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4740                                                      arg2));
4741             return pedantic_non_lvalue_loc (loc,
4742                                             fold_convert_loc (loc, type, tem));
4743           }
4744         break;
4745
4746       case GT_EXPR:
4747         /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
4748            MAX_EXPR, to preserve the signedness of the comparison.  */
4749         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4750                                OEP_ONLY_CONST)
4751             && operand_equal_p (arg01,
4752                                 const_binop (MINUS_EXPR, arg2,
4753                                              build_int_cst (type, 1)),
4754                                 OEP_ONLY_CONST))
4755           {
4756             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4757                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4758                                                      arg2));
4759             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4760           }
4761         break;
4762
4763       case GE_EXPR:
4764         /* If C1 is C2 + 1, this is max(A, C2), with the same care as above.  */
4765         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4766                                OEP_ONLY_CONST)
4767             && operand_equal_p (arg01,
4768                                 const_binop (PLUS_EXPR, arg2,
4769                                              build_int_cst (type, 1)),
4770                                 OEP_ONLY_CONST))
4771           {
4772             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4773                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4774                                                      arg2));
4775             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4776           }
4777         break;
4778       case NE_EXPR:
4779         break;
4780       default:
4781         gcc_unreachable ();
4782       }
4783
4784   return NULL_TREE;
4785 }
4786
4787
4788 \f
4789 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4790 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
4791   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
4792                 false) >= 2)
4793 #endif
4794
4795 /* EXP is some logical combination of boolean tests.  See if we can
4796    merge it into some range test.  Return the new tree if so.  */
4797
4798 static tree
4799 fold_range_test (location_t loc, enum tree_code code, tree type,
4800                  tree op0, tree op1)
4801 {
4802   int or_op = (code == TRUTH_ORIF_EXPR
4803                || code == TRUTH_OR_EXPR);
4804   int in0_p, in1_p, in_p;
4805   tree low0, low1, low, high0, high1, high;
4806   bool strict_overflow_p = false;
4807   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
4808   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
4809   tree tem;
4810   const char * const warnmsg = G_("assuming signed overflow does not occur "
4811                                   "when simplifying range test");
4812
4813   /* If this is an OR operation, invert both sides; we will invert
4814      again at the end.  */
4815   if (or_op)
4816     in0_p = ! in0_p, in1_p = ! in1_p;
4817
4818   /* If both expressions are the same, if we can merge the ranges, and we
4819      can build the range test, return it or it inverted.  If one of the
4820      ranges is always true or always false, consider it to be the same
4821      expression as the other.  */
4822   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4823       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4824                        in1_p, low1, high1)
4825       && 0 != (tem = (build_range_check (loc, type,
4826                                          lhs != 0 ? lhs
4827                                          : rhs != 0 ? rhs : integer_zero_node,
4828                                          in_p, low, high))))
4829     {
4830       if (strict_overflow_p)
4831         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
4832       return or_op ? invert_truthvalue_loc (loc, tem) : tem;
4833     }
4834
4835   /* On machines where the branch cost is expensive, if this is a
4836      short-circuited branch and the underlying object on both sides
4837      is the same, make a non-short-circuit operation.  */
4838   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4839            && lhs != 0 && rhs != 0
4840            && (code == TRUTH_ANDIF_EXPR
4841                || code == TRUTH_ORIF_EXPR)
4842            && operand_equal_p (lhs, rhs, 0))
4843     {
4844       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
4845          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4846          which cases we can't do this.  */
4847       if (simple_operand_p (lhs))
4848         return build2_loc (loc, code == TRUTH_ANDIF_EXPR
4849                            ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4850                            type, op0, op1);
4851
4852       else if (!lang_hooks.decls.global_bindings_p ()
4853                && !CONTAINS_PLACEHOLDER_P (lhs))
4854         {
4855           tree common = save_expr (lhs);
4856
4857           if (0 != (lhs = build_range_check (loc, type, common,
4858                                              or_op ? ! in0_p : in0_p,
4859                                              low0, high0))
4860               && (0 != (rhs = build_range_check (loc, type, common,
4861                                                  or_op ? ! in1_p : in1_p,
4862                                                  low1, high1))))
4863             {
4864               if (strict_overflow_p)
4865                 fold_overflow_warning (warnmsg,
4866                                        WARN_STRICT_OVERFLOW_COMPARISON);
4867               return build2_loc (loc, code == TRUTH_ANDIF_EXPR
4868                                  ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4869                                  type, lhs, rhs);
4870             }
4871         }
4872     }
4873
4874   return 0;
4875 }
4876 \f
4877 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4878    bit value.  Arrange things so the extra bits will be set to zero if and
4879    only if C is signed-extended to its full width.  If MASK is nonzero,
4880    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
4881
4882 static tree
4883 unextend (tree c, int p, int unsignedp, tree mask)
4884 {
4885   tree type = TREE_TYPE (c);
4886   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4887   tree temp;
4888
4889   if (p == modesize || unsignedp)
4890     return c;
4891
4892   /* We work by getting just the sign bit into the low-order bit, then
4893      into the high-order bit, then sign-extend.  We then XOR that value
4894      with C.  */
4895   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1));
4896   temp = const_binop (BIT_AND_EXPR, temp, size_int (1));
4897
4898   /* We must use a signed type in order to get an arithmetic right shift.
4899      However, we must also avoid introducing accidental overflows, so that
4900      a subsequent call to integer_zerop will work.  Hence we must
4901      do the type conversion here.  At this point, the constant is either
4902      zero or one, and the conversion to a signed type can never overflow.
4903      We could get an overflow if this conversion is done anywhere else.  */
4904   if (TYPE_UNSIGNED (type))
4905     temp = fold_convert (signed_type_for (type), temp);
4906
4907   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
4908   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
4909   if (mask != 0)
4910     temp = const_binop (BIT_AND_EXPR, temp,
4911                         fold_convert (TREE_TYPE (c), mask));
4912   /* If necessary, convert the type back to match the type of C.  */
4913   if (TYPE_UNSIGNED (type))
4914     temp = fold_convert (type, temp);
4915
4916   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
4917 }
4918 \f
4919 /* For an expression that has the form
4920      (A && B) || ~B
4921    or
4922      (A || B) && ~B,
4923    we can drop one of the inner expressions and simplify to
4924      A || ~B
4925    or
4926      A && ~B
4927    LOC is the location of the resulting expression.  OP is the inner 
4928    logical operation; the left-hand side in the examples above, while CMPOP
4929    is the right-hand side.  RHS_ONLY is used to prevent us from accidentally
4930    removing a condition that guards another, as in
4931      (A != NULL && A->...) || A == NULL
4932    which we must not transform.  If RHS_ONLY is true, only eliminate the
4933    right-most operand of the inner logical operation.  */
4934
4935 static tree
4936 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
4937                                  bool rhs_only)
4938 {
4939   tree type = TREE_TYPE (cmpop);
4940   enum tree_code code = TREE_CODE (cmpop);
4941   enum tree_code truthop_code = TREE_CODE (op);
4942   tree lhs = TREE_OPERAND (op, 0);
4943   tree rhs = TREE_OPERAND (op, 1);
4944   tree orig_lhs = lhs, orig_rhs = rhs;
4945   enum tree_code rhs_code = TREE_CODE (rhs);
4946   enum tree_code lhs_code = TREE_CODE (lhs);
4947   enum tree_code inv_code;
4948
4949   if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
4950     return NULL_TREE;
4951
4952   if (TREE_CODE_CLASS (code) != tcc_comparison)
4953     return NULL_TREE;
4954
4955   if (rhs_code == truthop_code)
4956     {
4957       tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
4958       if (newrhs != NULL_TREE)
4959         {
4960           rhs = newrhs;
4961           rhs_code = TREE_CODE (rhs);
4962         }
4963     }
4964   if (lhs_code == truthop_code && !rhs_only)
4965     {
4966       tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
4967       if (newlhs != NULL_TREE)
4968         {
4969           lhs = newlhs;
4970           lhs_code = TREE_CODE (lhs);
4971         }
4972     }
4973
4974   inv_code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
4975   if (inv_code == rhs_code
4976       && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
4977       && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
4978     return lhs;
4979   if (!rhs_only && inv_code == lhs_code
4980       && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
4981       && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
4982     return rhs;
4983   if (rhs != orig_rhs || lhs != orig_lhs)
4984     return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
4985                             lhs, rhs);
4986   return NULL_TREE;
4987 }
4988
4989 /* Find ways of folding logical expressions of LHS and RHS:
4990    Try to merge two comparisons to the same innermost item.
4991    Look for range tests like "ch >= '0' && ch <= '9'".
4992    Look for combinations of simple terms on machines with expensive branches
4993    and evaluate the RHS unconditionally.
4994
4995    For example, if we have p->a == 2 && p->b == 4 and we can make an
4996    object large enough to span both A and B, we can do this with a comparison
4997    against the object ANDed with the a mask.
4998
4999    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5000    operations to do this with one comparison.
5001
5002    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5003    function and the one above.
5004
5005    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5006    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5007
5008    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5009    two operands.
5010
5011    We return the simplified tree or 0 if no optimization is possible.  */
5012
5013 static tree
5014 fold_truthop (location_t loc, enum tree_code code, tree truth_type,
5015               tree lhs, tree rhs)
5016 {
5017   /* If this is the "or" of two comparisons, we can do something if
5018      the comparisons are NE_EXPR.  If this is the "and", we can do something
5019      if the comparisons are EQ_EXPR.  I.e.,
5020         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5021
5022      WANTED_CODE is this operation code.  For single bit fields, we can
5023      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5024      comparison for one-bit fields.  */
5025
5026   enum tree_code wanted_code;
5027   enum tree_code lcode, rcode;
5028   tree ll_arg, lr_arg, rl_arg, rr_arg;
5029   tree ll_inner, lr_inner, rl_inner, rr_inner;
5030   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5031   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5032   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5033   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5034   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5035   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5036   enum machine_mode lnmode, rnmode;
5037   tree ll_mask, lr_mask, rl_mask, rr_mask;
5038   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5039   tree l_const, r_const;
5040   tree lntype, rntype, result;
5041   HOST_WIDE_INT first_bit, end_bit;
5042   int volatilep;
5043   tree orig_lhs = lhs, orig_rhs = rhs;
5044   enum tree_code orig_code = code;
5045
5046   /* Start by getting the comparison codes.  Fail if anything is volatile.
5047      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5048      it were surrounded with a NE_EXPR.  */
5049
5050   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5051     return 0;
5052
5053   lcode = TREE_CODE (lhs);
5054   rcode = TREE_CODE (rhs);
5055
5056   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5057     {
5058       lhs = build2 (NE_EXPR, truth_type, lhs,
5059                     build_int_cst (TREE_TYPE (lhs), 0));
5060       lcode = NE_EXPR;
5061     }
5062
5063   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5064     {
5065       rhs = build2 (NE_EXPR, truth_type, rhs,
5066                     build_int_cst (TREE_TYPE (rhs), 0));
5067       rcode = NE_EXPR;
5068     }
5069
5070   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5071       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5072     return 0;
5073
5074   ll_arg = TREE_OPERAND (lhs, 0);
5075   lr_arg = TREE_OPERAND (lhs, 1);
5076   rl_arg = TREE_OPERAND (rhs, 0);
5077   rr_arg = TREE_OPERAND (rhs, 1);
5078
5079   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5080   if (simple_operand_p (ll_arg)
5081       && simple_operand_p (lr_arg))
5082     {
5083       if (operand_equal_p (ll_arg, rl_arg, 0)
5084           && operand_equal_p (lr_arg, rr_arg, 0))
5085         {
5086           result = combine_comparisons (loc, code, lcode, rcode,
5087                                         truth_type, ll_arg, lr_arg);
5088           if (result)
5089             return result;
5090         }
5091       else if (operand_equal_p (ll_arg, rr_arg, 0)
5092                && operand_equal_p (lr_arg, rl_arg, 0))
5093         {
5094           result = combine_comparisons (loc, code, lcode,
5095                                         swap_tree_comparison (rcode),
5096                                         truth_type, ll_arg, lr_arg);
5097           if (result)
5098             return result;
5099         }
5100     }
5101
5102   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5103           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5104
5105   /* If the RHS can be evaluated unconditionally and its operands are
5106      simple, it wins to evaluate the RHS unconditionally on machines
5107      with expensive branches.  In this case, this isn't a comparison
5108      that can be merged.  Avoid doing this if the RHS is a floating-point
5109      comparison since those can trap.  */
5110
5111   if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5112                    false) >= 2
5113       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5114       && simple_operand_p (rl_arg)
5115       && simple_operand_p (rr_arg))
5116     {
5117       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5118       if (code == TRUTH_OR_EXPR
5119           && lcode == NE_EXPR && integer_zerop (lr_arg)
5120           && rcode == NE_EXPR && integer_zerop (rr_arg)
5121           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5122           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5123         return build2_loc (loc, NE_EXPR, truth_type,
5124                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5125                                    ll_arg, rl_arg),
5126                            build_int_cst (TREE_TYPE (ll_arg), 0));
5127
5128       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5129       if (code == TRUTH_AND_EXPR
5130           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5131           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5132           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5133           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5134         return build2_loc (loc, EQ_EXPR, truth_type,
5135                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5136                                    ll_arg, rl_arg),
5137                            build_int_cst (TREE_TYPE (ll_arg), 0));
5138
5139       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5140         {
5141           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5142             return build2_loc (loc, code, truth_type, lhs, rhs);
5143           return NULL_TREE;
5144         }
5145     }
5146
5147   /* See if the comparisons can be merged.  Then get all the parameters for
5148      each side.  */
5149
5150   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5151       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5152     return 0;
5153
5154   volatilep = 0;
5155   ll_inner = decode_field_reference (loc, ll_arg,
5156                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5157                                      &ll_unsignedp, &volatilep, &ll_mask,
5158                                      &ll_and_mask);
5159   lr_inner = decode_field_reference (loc, lr_arg,
5160                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5161                                      &lr_unsignedp, &volatilep, &lr_mask,
5162                                      &lr_and_mask);
5163   rl_inner = decode_field_reference (loc, rl_arg,
5164                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5165                                      &rl_unsignedp, &volatilep, &rl_mask,
5166                                      &rl_and_mask);
5167   rr_inner = decode_field_reference (loc, rr_arg,
5168                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5169                                      &rr_unsignedp, &volatilep, &rr_mask,
5170                                      &rr_and_mask);
5171
5172   /* It must be true that the inner operation on the lhs of each
5173      comparison must be the same if we are to be able to do anything.
5174      Then see if we have constants.  If not, the same must be true for
5175      the rhs's.  */
5176   if (volatilep || ll_inner == 0 || rl_inner == 0
5177       || ! operand_equal_p (ll_inner, rl_inner, 0))
5178     return 0;
5179
5180   if (TREE_CODE (lr_arg) == INTEGER_CST
5181       && TREE_CODE (rr_arg) == INTEGER_CST)
5182     l_const = lr_arg, r_const = rr_arg;
5183   else if (lr_inner == 0 || rr_inner == 0
5184            || ! operand_equal_p (lr_inner, rr_inner, 0))
5185     return 0;
5186   else
5187     l_const = r_const = 0;
5188
5189   /* If either comparison code is not correct for our logical operation,
5190      fail.  However, we can convert a one-bit comparison against zero into
5191      the opposite comparison against that bit being set in the field.  */
5192
5193   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5194   if (lcode != wanted_code)
5195     {
5196       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5197         {
5198           /* Make the left operand unsigned, since we are only interested
5199              in the value of one bit.  Otherwise we are doing the wrong
5200              thing below.  */
5201           ll_unsignedp = 1;
5202           l_const = ll_mask;
5203         }
5204       else
5205         return 0;
5206     }
5207
5208   /* This is analogous to the code for l_const above.  */
5209   if (rcode != wanted_code)
5210     {
5211       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5212         {
5213           rl_unsignedp = 1;
5214           r_const = rl_mask;
5215         }
5216       else
5217         return 0;
5218     }
5219
5220   /* See if we can find a mode that contains both fields being compared on
5221      the left.  If we can't, fail.  Otherwise, update all constants and masks
5222      to be relative to a field of that size.  */
5223   first_bit = MIN (ll_bitpos, rl_bitpos);
5224   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5225   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5226                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5227                           volatilep);
5228   if (lnmode == VOIDmode)
5229     return 0;
5230
5231   lnbitsize = GET_MODE_BITSIZE (lnmode);
5232   lnbitpos = first_bit & ~ (lnbitsize - 1);
5233   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5234   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5235
5236   if (BYTES_BIG_ENDIAN)
5237     {
5238       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5239       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5240     }
5241
5242   ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5243                          size_int (xll_bitpos));
5244   rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5245                          size_int (xrl_bitpos));
5246
5247   if (l_const)
5248     {
5249       l_const = fold_convert_loc (loc, lntype, l_const);
5250       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5251       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5252       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5253                                         fold_build1_loc (loc, BIT_NOT_EXPR,
5254                                                      lntype, ll_mask))))
5255         {
5256           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5257
5258           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5259         }
5260     }
5261   if (r_const)
5262     {
5263       r_const = fold_convert_loc (loc, lntype, r_const);
5264       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5265       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5266       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5267                                         fold_build1_loc (loc, BIT_NOT_EXPR,
5268                                                      lntype, rl_mask))))
5269         {
5270           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5271
5272           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5273         }
5274     }
5275
5276   /* If the right sides are not constant, do the same for it.  Also,
5277      disallow this optimization if a size or signedness mismatch occurs
5278      between the left and right sides.  */
5279   if (l_const == 0)
5280     {
5281       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5282           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5283           /* Make sure the two fields on the right
5284              correspond to the left without being swapped.  */
5285           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5286         return 0;
5287
5288       first_bit = MIN (lr_bitpos, rr_bitpos);
5289       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5290       rnmode = get_best_mode (end_bit - first_bit, first_bit,
5291                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5292                               volatilep);
5293       if (rnmode == VOIDmode)
5294         return 0;
5295
5296       rnbitsize = GET_MODE_BITSIZE (rnmode);
5297       rnbitpos = first_bit & ~ (rnbitsize - 1);
5298       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5299       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5300
5301       if (BYTES_BIG_ENDIAN)
5302         {
5303           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5304           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5305         }
5306
5307       lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5308                                                             rntype, lr_mask),
5309                              size_int (xlr_bitpos));
5310       rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5311                                                             rntype, rr_mask),
5312                              size_int (xrr_bitpos));
5313
5314       /* Make a mask that corresponds to both fields being compared.
5315          Do this for both items being compared.  If the operands are the
5316          same size and the bits being compared are in the same position
5317          then we can do this by masking both and comparing the masked
5318          results.  */
5319       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5320       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5321       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5322         {
5323           lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5324                                     ll_unsignedp || rl_unsignedp);
5325           if (! all_ones_mask_p (ll_mask, lnbitsize))
5326             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5327
5328           rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5329                                     lr_unsignedp || rr_unsignedp);
5330           if (! all_ones_mask_p (lr_mask, rnbitsize))
5331             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5332
5333           return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5334         }
5335
5336       /* There is still another way we can do something:  If both pairs of
5337          fields being compared are adjacent, we may be able to make a wider
5338          field containing them both.
5339
5340          Note that we still must mask the lhs/rhs expressions.  Furthermore,
5341          the mask must be shifted to account for the shift done by
5342          make_bit_field_ref.  */
5343       if ((ll_bitsize + ll_bitpos == rl_bitpos
5344            && lr_bitsize + lr_bitpos == rr_bitpos)
5345           || (ll_bitpos == rl_bitpos + rl_bitsize
5346               && lr_bitpos == rr_bitpos + rr_bitsize))
5347         {
5348           tree type;
5349
5350           lhs = make_bit_field_ref (loc, ll_inner, lntype,
5351                                     ll_bitsize + rl_bitsize,
5352                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5353           rhs = make_bit_field_ref (loc, lr_inner, rntype,
5354                                     lr_bitsize + rr_bitsize,
5355                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5356
5357           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5358                                  size_int (MIN (xll_bitpos, xrl_bitpos)));
5359           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5360                                  size_int (MIN (xlr_bitpos, xrr_bitpos)));
5361
5362           /* Convert to the smaller type before masking out unwanted bits.  */
5363           type = lntype;
5364           if (lntype != rntype)
5365             {
5366               if (lnbitsize > rnbitsize)
5367                 {
5368                   lhs = fold_convert_loc (loc, rntype, lhs);
5369                   ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5370                   type = rntype;
5371                 }
5372               else if (lnbitsize < rnbitsize)
5373                 {
5374                   rhs = fold_convert_loc (loc, lntype, rhs);
5375                   lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5376                   type = lntype;
5377                 }
5378             }
5379
5380           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5381             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5382
5383           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5384             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5385
5386           return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5387         }
5388
5389       return 0;
5390     }
5391
5392   /* Handle the case of comparisons with constants.  If there is something in
5393      common between the masks, those bits of the constants must be the same.
5394      If not, the condition is always false.  Test for this to avoid generating
5395      incorrect code below.  */
5396   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5397   if (! integer_zerop (result)
5398       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5399                            const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5400     {
5401       if (wanted_code == NE_EXPR)
5402         {
5403           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5404           return constant_boolean_node (true, truth_type);
5405         }
5406       else
5407         {
5408           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5409           return constant_boolean_node (false, truth_type);
5410         }
5411     }
5412
5413   /* Construct the expression we will return.  First get the component
5414      reference we will make.  Unless the mask is all ones the width of
5415      that field, perform the mask operation.  Then compare with the
5416      merged constant.  */
5417   result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5418                                ll_unsignedp || rl_unsignedp);
5419
5420   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5421   if (! all_ones_mask_p (ll_mask, lnbitsize))
5422     result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5423
5424   return build2_loc (loc, wanted_code, truth_type, result,
5425                      const_binop (BIT_IOR_EXPR, l_const, r_const));
5426 }
5427 \f
5428 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5429    constant.  */
5430
5431 static tree
5432 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5433                             tree op0, tree op1)
5434 {
5435   tree arg0 = op0;
5436   enum tree_code op_code;
5437   tree comp_const;
5438   tree minmax_const;
5439   int consts_equal, consts_lt;
5440   tree inner;
5441
5442   STRIP_SIGN_NOPS (arg0);
5443
5444   op_code = TREE_CODE (arg0);
5445   minmax_const = TREE_OPERAND (arg0, 1);
5446   comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5447   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5448   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5449   inner = TREE_OPERAND (arg0, 0);
5450
5451   /* If something does not permit us to optimize, return the original tree.  */
5452   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5453       || TREE_CODE (comp_const) != INTEGER_CST
5454       || TREE_OVERFLOW (comp_const)
5455       || TREE_CODE (minmax_const) != INTEGER_CST
5456       || TREE_OVERFLOW (minmax_const))
5457     return NULL_TREE;
5458
5459   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5460      and GT_EXPR, doing the rest with recursive calls using logical
5461      simplifications.  */
5462   switch (code)
5463     {
5464     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5465       {
5466         tree tem
5467           = optimize_minmax_comparison (loc,
5468                                         invert_tree_comparison (code, false),
5469                                         type, op0, op1);
5470         if (tem)
5471           return invert_truthvalue_loc (loc, tem);
5472         return NULL_TREE;
5473       }
5474
5475     case GE_EXPR:
5476       return
5477         fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5478                      optimize_minmax_comparison
5479                      (loc, EQ_EXPR, type, arg0, comp_const),
5480                      optimize_minmax_comparison
5481                      (loc, GT_EXPR, type, arg0, comp_const));
5482
5483     case EQ_EXPR:
5484       if (op_code == MAX_EXPR && consts_equal)
5485         /* MAX (X, 0) == 0  ->  X <= 0  */
5486         return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5487
5488       else if (op_code == MAX_EXPR && consts_lt)
5489         /* MAX (X, 0) == 5  ->  X == 5   */
5490         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5491
5492       else if (op_code == MAX_EXPR)
5493         /* MAX (X, 0) == -1  ->  false  */
5494         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5495
5496       else if (consts_equal)
5497         /* MIN (X, 0) == 0  ->  X >= 0  */
5498         return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5499
5500       else if (consts_lt)
5501         /* MIN (X, 0) == 5  ->  false  */
5502         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5503
5504       else
5505         /* MIN (X, 0) == -1  ->  X == -1  */
5506         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5507
5508     case GT_EXPR:
5509       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5510         /* MAX (X, 0) > 0  ->  X > 0
5511            MAX (X, 0) > 5  ->  X > 5  */
5512         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5513
5514       else if (op_code == MAX_EXPR)
5515         /* MAX (X, 0) > -1  ->  true  */
5516         return omit_one_operand_loc (loc, type, integer_one_node, inner);
5517
5518       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5519         /* MIN (X, 0) > 0  ->  false
5520            MIN (X, 0) > 5  ->  false  */
5521         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5522
5523       else
5524         /* MIN (X, 0) > -1  ->  X > -1  */
5525         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5526
5527     default:
5528       return NULL_TREE;
5529     }
5530 }
5531 \f
5532 /* T is an integer expression that is being multiplied, divided, or taken a
5533    modulus (CODE says which and what kind of divide or modulus) by a
5534    constant C.  See if we can eliminate that operation by folding it with
5535    other operations already in T.  WIDE_TYPE, if non-null, is a type that
5536    should be used for the computation if wider than our type.
5537
5538    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5539    (X * 2) + (Y * 4).  We must, however, be assured that either the original
5540    expression would not overflow or that overflow is undefined for the type
5541    in the language in question.
5542
5543    If we return a non-null expression, it is an equivalent form of the
5544    original computation, but need not be in the original type.
5545
5546    We set *STRICT_OVERFLOW_P to true if the return values depends on
5547    signed overflow being undefined.  Otherwise we do not change
5548    *STRICT_OVERFLOW_P.  */
5549
5550 static tree
5551 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5552                 bool *strict_overflow_p)
5553 {
5554   /* To avoid exponential search depth, refuse to allow recursion past
5555      three levels.  Beyond that (1) it's highly unlikely that we'll find
5556      something interesting and (2) we've probably processed it before
5557      when we built the inner expression.  */
5558
5559   static int depth;
5560   tree ret;
5561
5562   if (depth > 3)
5563     return NULL;
5564
5565   depth++;
5566   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5567   depth--;
5568
5569   return ret;
5570 }
5571
5572 static tree
5573 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5574                   bool *strict_overflow_p)
5575 {
5576   tree type = TREE_TYPE (t);
5577   enum tree_code tcode = TREE_CODE (t);
5578   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5579                                    > GET_MODE_SIZE (TYPE_MODE (type)))
5580                 ? wide_type : type);
5581   tree t1, t2;
5582   int same_p = tcode == code;
5583   tree op0 = NULL_TREE, op1 = NULL_TREE;
5584   bool sub_strict_overflow_p;
5585
5586   /* Don't deal with constants of zero here; they confuse the code below.  */
5587   if (integer_zerop (c))
5588     return NULL_TREE;
5589
5590   if (TREE_CODE_CLASS (tcode) == tcc_unary)
5591     op0 = TREE_OPERAND (t, 0);
5592
5593   if (TREE_CODE_CLASS (tcode) == tcc_binary)
5594     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5595
5596   /* Note that we need not handle conditional operations here since fold
5597      already handles those cases.  So just do arithmetic here.  */
5598   switch (tcode)
5599     {
5600     case INTEGER_CST:
5601       /* For a constant, we can always simplify if we are a multiply
5602          or (for divide and modulus) if it is a multiple of our constant.  */
5603       if (code == MULT_EXPR
5604           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c)))
5605         return const_binop (code, fold_convert (ctype, t),
5606                             fold_convert (ctype, c));
5607       break;
5608
5609     CASE_CONVERT: case NON_LVALUE_EXPR:
5610       /* If op0 is an expression ...  */
5611       if ((COMPARISON_CLASS_P (op0)
5612            || UNARY_CLASS_P (op0)
5613            || BINARY_CLASS_P (op0)
5614            || VL_EXP_CLASS_P (op0)
5615            || EXPRESSION_CLASS_P (op0))
5616           /* ... and has wrapping overflow, and its type is smaller
5617              than ctype, then we cannot pass through as widening.  */
5618           && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
5619                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5620                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5621                && (TYPE_PRECISION (ctype)
5622                    > TYPE_PRECISION (TREE_TYPE (op0))))
5623               /* ... or this is a truncation (t is narrower than op0),
5624                  then we cannot pass through this narrowing.  */
5625               || (TYPE_PRECISION (type)
5626                   < TYPE_PRECISION (TREE_TYPE (op0)))
5627               /* ... or signedness changes for division or modulus,
5628                  then we cannot pass through this conversion.  */
5629               || (code != MULT_EXPR
5630                   && (TYPE_UNSIGNED (ctype)
5631                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
5632               /* ... or has undefined overflow while the converted to
5633                  type has not, we cannot do the operation in the inner type
5634                  as that would introduce undefined overflow.  */
5635               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5636                   && !TYPE_OVERFLOW_UNDEFINED (type))))
5637         break;
5638
5639       /* Pass the constant down and see if we can make a simplification.  If
5640          we can, replace this expression with the inner simplification for
5641          possible later conversion to our or some other type.  */
5642       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5643           && TREE_CODE (t2) == INTEGER_CST
5644           && !TREE_OVERFLOW (t2)
5645           && (0 != (t1 = extract_muldiv (op0, t2, code,
5646                                          code == MULT_EXPR
5647                                          ? ctype : NULL_TREE,
5648                                          strict_overflow_p))))
5649         return t1;
5650       break;
5651
5652     case ABS_EXPR:
5653       /* If widening the type changes it from signed to unsigned, then we
5654          must avoid building ABS_EXPR itself as unsigned.  */
5655       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5656         {
5657           tree cstype = (*signed_type_for) (ctype);
5658           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5659               != 0)
5660             {
5661               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5662               return fold_convert (ctype, t1);
5663             }
5664           break;
5665         }
5666       /* If the constant is negative, we cannot simplify this.  */
5667       if (tree_int_cst_sgn (c) == -1)
5668         break;
5669       /* FALLTHROUGH */
5670     case NEGATE_EXPR:
5671       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5672           != 0)
5673         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5674       break;
5675
5676     case MIN_EXPR:  case MAX_EXPR:
5677       /* If widening the type changes the signedness, then we can't perform
5678          this optimization as that changes the result.  */
5679       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5680         break;
5681
5682       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
5683       sub_strict_overflow_p = false;
5684       if ((t1 = extract_muldiv (op0, c, code, wide_type,
5685                                 &sub_strict_overflow_p)) != 0
5686           && (t2 = extract_muldiv (op1, c, code, wide_type,
5687                                    &sub_strict_overflow_p)) != 0)
5688         {
5689           if (tree_int_cst_sgn (c) < 0)
5690             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5691           if (sub_strict_overflow_p)
5692             *strict_overflow_p = true;
5693           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5694                               fold_convert (ctype, t2));
5695         }
5696       break;
5697
5698     case LSHIFT_EXPR:  case RSHIFT_EXPR:
5699       /* If the second operand is constant, this is a multiplication
5700          or floor division, by a power of two, so we can treat it that
5701          way unless the multiplier or divisor overflows.  Signed
5702          left-shift overflow is implementation-defined rather than
5703          undefined in C90, so do not convert signed left shift into
5704          multiplication.  */
5705       if (TREE_CODE (op1) == INTEGER_CST
5706           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5707           /* const_binop may not detect overflow correctly,
5708              so check for it explicitly here.  */
5709           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5710           && TREE_INT_CST_HIGH (op1) == 0
5711           && 0 != (t1 = fold_convert (ctype,
5712                                       const_binop (LSHIFT_EXPR,
5713                                                    size_one_node,
5714                                                    op1)))
5715           && !TREE_OVERFLOW (t1))
5716         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5717                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
5718                                        ctype,
5719                                        fold_convert (ctype, op0),
5720                                        t1),
5721                                c, code, wide_type, strict_overflow_p);
5722       break;
5723
5724     case PLUS_EXPR:  case MINUS_EXPR:
5725       /* See if we can eliminate the operation on both sides.  If we can, we
5726          can return a new PLUS or MINUS.  If we can't, the only remaining
5727          cases where we can do anything are if the second operand is a
5728          constant.  */
5729       sub_strict_overflow_p = false;
5730       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5731       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5732       if (t1 != 0 && t2 != 0
5733           && (code == MULT_EXPR
5734               /* If not multiplication, we can only do this if both operands
5735                  are divisible by c.  */
5736               || (multiple_of_p (ctype, op0, c)
5737                   && multiple_of_p (ctype, op1, c))))
5738         {
5739           if (sub_strict_overflow_p)
5740             *strict_overflow_p = true;
5741           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5742                               fold_convert (ctype, t2));
5743         }
5744
5745       /* If this was a subtraction, negate OP1 and set it to be an addition.
5746          This simplifies the logic below.  */
5747       if (tcode == MINUS_EXPR)
5748         {
5749           tcode = PLUS_EXPR, op1 = negate_expr (op1);
5750           /* If OP1 was not easily negatable, the constant may be OP0.  */
5751           if (TREE_CODE (op0) == INTEGER_CST)
5752             {
5753               tree tem = op0;
5754               op0 = op1;
5755               op1 = tem;
5756               tem = t1;
5757               t1 = t2;
5758               t2 = tem;
5759             }
5760         }
5761
5762       if (TREE_CODE (op1) != INTEGER_CST)
5763         break;
5764
5765       /* If either OP1 or C are negative, this optimization is not safe for
5766          some of the division and remainder types while for others we need
5767          to change the code.  */
5768       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5769         {
5770           if (code == CEIL_DIV_EXPR)
5771             code = FLOOR_DIV_EXPR;
5772           else if (code == FLOOR_DIV_EXPR)
5773             code = CEIL_DIV_EXPR;
5774           else if (code != MULT_EXPR
5775                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5776             break;
5777         }
5778
5779       /* If it's a multiply or a division/modulus operation of a multiple
5780          of our constant, do the operation and verify it doesn't overflow.  */
5781       if (code == MULT_EXPR
5782           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5783         {
5784           op1 = const_binop (code, fold_convert (ctype, op1),
5785                              fold_convert (ctype, c));
5786           /* We allow the constant to overflow with wrapping semantics.  */
5787           if (op1 == 0
5788               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5789             break;
5790         }
5791       else
5792         break;
5793
5794       /* If we have an unsigned type is not a sizetype, we cannot widen
5795          the operation since it will change the result if the original
5796          computation overflowed.  */
5797       if (TYPE_UNSIGNED (ctype)
5798           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5799           && ctype != type)
5800         break;
5801
5802       /* If we were able to eliminate our operation from the first side,
5803          apply our operation to the second side and reform the PLUS.  */
5804       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5805         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5806
5807       /* The last case is if we are a multiply.  In that case, we can
5808          apply the distributive law to commute the multiply and addition
5809          if the multiplication of the constants doesn't overflow.  */
5810       if (code == MULT_EXPR)
5811         return fold_build2 (tcode, ctype,
5812                             fold_build2 (code, ctype,
5813                                          fold_convert (ctype, op0),
5814                                          fold_convert (ctype, c)),
5815                             op1);
5816
5817       break;
5818
5819     case MULT_EXPR:
5820       /* We have a special case here if we are doing something like
5821          (C * 8) % 4 since we know that's zero.  */
5822       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5823            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5824           /* If the multiplication can overflow we cannot optimize this.
5825              ???  Until we can properly mark individual operations as
5826              not overflowing we need to treat sizetype special here as
5827              stor-layout relies on this opimization to make
5828              DECL_FIELD_BIT_OFFSET always a constant.  */
5829           && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
5830               || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
5831                   && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
5832           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5833           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5834         {
5835           *strict_overflow_p = true;
5836           return omit_one_operand (type, integer_zero_node, op0);
5837         }
5838
5839       /* ... fall through ...  */
5840
5841     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
5842     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
5843       /* If we can extract our operation from the LHS, do so and return a
5844          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
5845          do something only if the second operand is a constant.  */
5846       if (same_p
5847           && (t1 = extract_muldiv (op0, c, code, wide_type,
5848                                    strict_overflow_p)) != 0)
5849         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5850                             fold_convert (ctype, op1));
5851       else if (tcode == MULT_EXPR && code == MULT_EXPR
5852                && (t1 = extract_muldiv (op1, c, code, wide_type,
5853                                         strict_overflow_p)) != 0)
5854         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5855                             fold_convert (ctype, t1));
5856       else if (TREE_CODE (op1) != INTEGER_CST)
5857         return 0;
5858
5859       /* If these are the same operation types, we can associate them
5860          assuming no overflow.  */
5861       if (tcode == code)
5862         {
5863           double_int mul;
5864           int overflow_p;
5865           mul = double_int_mul_with_sign
5866                   (double_int_ext
5867                      (tree_to_double_int (op1),
5868                       TYPE_PRECISION (ctype), TYPE_UNSIGNED (ctype)),
5869                    double_int_ext
5870                      (tree_to_double_int (c),
5871                       TYPE_PRECISION (ctype), TYPE_UNSIGNED (ctype)),
5872                    false, &overflow_p);
5873           overflow_p = (((!TYPE_UNSIGNED (ctype)
5874                           || (TREE_CODE (ctype) == INTEGER_TYPE
5875                               && TYPE_IS_SIZETYPE (ctype)))
5876                          && overflow_p)
5877                         | TREE_OVERFLOW (c) | TREE_OVERFLOW (op1));
5878           if (!double_int_fits_to_tree_p (ctype, mul)
5879               && ((TYPE_UNSIGNED (ctype) && tcode != MULT_EXPR)
5880                   || !TYPE_UNSIGNED (ctype)
5881                   || (TREE_CODE (ctype) == INTEGER_TYPE
5882                       && TYPE_IS_SIZETYPE (ctype))))
5883             overflow_p = 1;
5884           if (!overflow_p)
5885             return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5886                                 double_int_to_tree (ctype, mul));
5887         }
5888
5889       /* If these operations "cancel" each other, we have the main
5890          optimizations of this pass, which occur when either constant is a
5891          multiple of the other, in which case we replace this with either an
5892          operation or CODE or TCODE.
5893
5894          If we have an unsigned type that is not a sizetype, we cannot do
5895          this since it will change the result if the original computation
5896          overflowed.  */
5897       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
5898            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5899           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5900               || (tcode == MULT_EXPR
5901                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5902                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
5903                   && code != MULT_EXPR)))
5904         {
5905           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5906             {
5907               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5908                 *strict_overflow_p = true;
5909               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5910                                   fold_convert (ctype,
5911                                                 const_binop (TRUNC_DIV_EXPR,
5912                                                              op1, c)));
5913             }
5914           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1)))
5915             {
5916               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5917                 *strict_overflow_p = true;
5918               return fold_build2 (code, ctype, fold_convert (ctype, op0),
5919                                   fold_convert (ctype,
5920                                                 const_binop (TRUNC_DIV_EXPR,
5921                                                              c, op1)));
5922             }
5923         }
5924       break;
5925
5926     default:
5927       break;
5928     }
5929
5930   return 0;
5931 }
5932 \f
5933 /* Return a node which has the indicated constant VALUE (either 0 or
5934    1), and is of the indicated TYPE.  */
5935
5936 tree
5937 constant_boolean_node (int value, tree type)
5938 {
5939   if (type == integer_type_node)
5940     return value ? integer_one_node : integer_zero_node;
5941   else if (type == boolean_type_node)
5942     return value ? boolean_true_node : boolean_false_node;
5943   else
5944     return build_int_cst (type, value);
5945 }
5946
5947
5948 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
5949    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
5950    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
5951    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
5952    COND is the first argument to CODE; otherwise (as in the example
5953    given here), it is the second argument.  TYPE is the type of the
5954    original expression.  Return NULL_TREE if no simplification is
5955    possible.  */
5956
5957 static tree
5958 fold_binary_op_with_conditional_arg (location_t loc,
5959                                      enum tree_code code,
5960                                      tree type, tree op0, tree op1,
5961                                      tree cond, tree arg, int cond_first_p)
5962 {
5963   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
5964   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
5965   tree test, true_value, false_value;
5966   tree lhs = NULL_TREE;
5967   tree rhs = NULL_TREE;
5968
5969   if (TREE_CODE (cond) == COND_EXPR)
5970     {
5971       test = TREE_OPERAND (cond, 0);
5972       true_value = TREE_OPERAND (cond, 1);
5973       false_value = TREE_OPERAND (cond, 2);
5974       /* If this operand throws an expression, then it does not make
5975          sense to try to perform a logical or arithmetic operation
5976          involving it.  */
5977       if (VOID_TYPE_P (TREE_TYPE (true_value)))
5978         lhs = true_value;
5979       if (VOID_TYPE_P (TREE_TYPE (false_value)))
5980         rhs = false_value;
5981     }
5982   else
5983     {
5984       tree testtype = TREE_TYPE (cond);
5985       test = cond;
5986       true_value = constant_boolean_node (true, testtype);
5987       false_value = constant_boolean_node (false, testtype);
5988     }
5989
5990   /* This transformation is only worthwhile if we don't have to wrap ARG
5991      in a SAVE_EXPR and the operation can be simplified on at least one
5992      of the branches once its pushed inside the COND_EXPR.  */
5993   if (!TREE_CONSTANT (arg)
5994       && (TREE_SIDE_EFFECTS (arg)
5995           || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
5996     return NULL_TREE;
5997
5998   arg = fold_convert_loc (loc, arg_type, arg);
5999   if (lhs == 0)
6000     {
6001       true_value = fold_convert_loc (loc, cond_type, true_value);
6002       if (cond_first_p)
6003         lhs = fold_build2_loc (loc, code, type, true_value, arg);
6004       else
6005         lhs = fold_build2_loc (loc, code, type, arg, true_value);
6006     }
6007   if (rhs == 0)
6008     {
6009       false_value = fold_convert_loc (loc, cond_type, false_value);
6010       if (cond_first_p)
6011         rhs = fold_build2_loc (loc, code, type, false_value, arg);
6012       else
6013         rhs = fold_build2_loc (loc, code, type, arg, false_value);
6014     }
6015
6016   /* Check that we have simplified at least one of the branches.  */
6017   if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6018     return NULL_TREE;
6019
6020   return fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs);
6021 }
6022
6023 \f
6024 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6025
6026    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6027    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6028    ADDEND is the same as X.
6029
6030    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6031    and finite.  The problematic cases are when X is zero, and its mode
6032    has signed zeros.  In the case of rounding towards -infinity,
6033    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6034    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6035
6036 bool
6037 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6038 {
6039   if (!real_zerop (addend))
6040     return false;
6041
6042   /* Don't allow the fold with -fsignaling-nans.  */
6043   if (HONOR_SNANS (TYPE_MODE (type)))
6044     return false;
6045
6046   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6047   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6048     return true;
6049
6050   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6051   if (TREE_CODE (addend) == REAL_CST
6052       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6053     negate = !negate;
6054
6055   /* The mode has signed zeros, and we have to honor their sign.
6056      In this situation, there is only one case we can return true for.
6057      X - 0 is the same as X unless rounding towards -infinity is
6058      supported.  */
6059   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6060 }
6061
6062 /* Subroutine of fold() that checks comparisons of built-in math
6063    functions against real constants.
6064
6065    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6066    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6067    is the type of the result and ARG0 and ARG1 are the operands of the
6068    comparison.  ARG1 must be a TREE_REAL_CST.
6069
6070    The function returns the constant folded tree if a simplification
6071    can be made, and NULL_TREE otherwise.  */
6072
6073 static tree
6074 fold_mathfn_compare (location_t loc,
6075                      enum built_in_function fcode, enum tree_code code,
6076                      tree type, tree arg0, tree arg1)
6077 {
6078   REAL_VALUE_TYPE c;
6079
6080   if (BUILTIN_SQRT_P (fcode))
6081     {
6082       tree arg = CALL_EXPR_ARG (arg0, 0);
6083       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6084
6085       c = TREE_REAL_CST (arg1);
6086       if (REAL_VALUE_NEGATIVE (c))
6087         {
6088           /* sqrt(x) < y is always false, if y is negative.  */
6089           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6090             return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6091
6092           /* sqrt(x) > y is always true, if y is negative and we
6093              don't care about NaNs, i.e. negative values of x.  */
6094           if (code == NE_EXPR || !HONOR_NANS (mode))
6095             return omit_one_operand_loc (loc, type, integer_one_node, arg);
6096
6097           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6098           return fold_build2_loc (loc, GE_EXPR, type, arg,
6099                               build_real (TREE_TYPE (arg), dconst0));
6100         }
6101       else if (code == GT_EXPR || code == GE_EXPR)
6102         {
6103           REAL_VALUE_TYPE c2;
6104
6105           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6106           real_convert (&c2, mode, &c2);
6107
6108           if (REAL_VALUE_ISINF (c2))
6109             {
6110               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6111               if (HONOR_INFINITIES (mode))
6112                 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6113                                     build_real (TREE_TYPE (arg), c2));
6114
6115               /* sqrt(x) > y is always false, when y is very large
6116                  and we don't care about infinities.  */
6117               return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6118             }
6119
6120           /* sqrt(x) > c is the same as x > c*c.  */
6121           return fold_build2_loc (loc, code, type, arg,
6122                               build_real (TREE_TYPE (arg), c2));
6123         }
6124       else if (code == LT_EXPR || code == LE_EXPR)
6125         {
6126           REAL_VALUE_TYPE c2;
6127
6128           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6129           real_convert (&c2, mode, &c2);
6130
6131           if (REAL_VALUE_ISINF (c2))
6132             {
6133               /* sqrt(x) < y is always true, when y is a very large
6134                  value and we don't care about NaNs or Infinities.  */
6135               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6136                 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6137
6138               /* sqrt(x) < y is x != +Inf when y is very large and we
6139                  don't care about NaNs.  */
6140               if (! HONOR_NANS (mode))
6141                 return fold_build2_loc (loc, NE_EXPR, type, arg,
6142                                     build_real (TREE_TYPE (arg), c2));
6143
6144               /* sqrt(x) < y is x >= 0 when y is very large and we
6145                  don't care about Infinities.  */
6146               if (! HONOR_INFINITIES (mode))
6147                 return fold_build2_loc (loc, GE_EXPR, type, arg,
6148                                     build_real (TREE_TYPE (arg), dconst0));
6149
6150               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6151               arg = save_expr (arg);
6152               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6153                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6154                                                build_real (TREE_TYPE (arg),
6155                                                            dconst0)),
6156                                   fold_build2_loc (loc, NE_EXPR, type, arg,
6157                                                build_real (TREE_TYPE (arg),
6158                                                            c2)));
6159             }
6160
6161           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6162           if (! HONOR_NANS (mode))
6163             return fold_build2_loc (loc, code, type, arg,
6164                                 build_real (TREE_TYPE (arg), c2));
6165
6166           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6167           arg = save_expr (arg);
6168           return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6169                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6170                                                build_real (TREE_TYPE (arg),
6171                                                            dconst0)),
6172                                   fold_build2_loc (loc, code, type, arg,
6173                                                build_real (TREE_TYPE (arg),
6174                                                            c2)));
6175         }
6176     }
6177
6178   return NULL_TREE;
6179 }
6180
6181 /* Subroutine of fold() that optimizes comparisons against Infinities,
6182    either +Inf or -Inf.
6183
6184    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6185    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6186    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6187
6188    The function returns the constant folded tree if a simplification
6189    can be made, and NULL_TREE otherwise.  */
6190
6191 static tree
6192 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6193                   tree arg0, tree arg1)
6194 {
6195   enum machine_mode mode;
6196   REAL_VALUE_TYPE max;
6197   tree temp;
6198   bool neg;
6199
6200   mode = TYPE_MODE (TREE_TYPE (arg0));
6201
6202   /* For negative infinity swap the sense of the comparison.  */
6203   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6204   if (neg)
6205     code = swap_tree_comparison (code);
6206
6207   switch (code)
6208     {
6209     case GT_EXPR:
6210       /* x > +Inf is always false, if with ignore sNANs.  */
6211       if (HONOR_SNANS (mode))
6212         return NULL_TREE;
6213       return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6214
6215     case LE_EXPR:
6216       /* x <= +Inf is always true, if we don't case about NaNs.  */
6217       if (! HONOR_NANS (mode))
6218         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6219
6220       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6221       arg0 = save_expr (arg0);
6222       return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6223
6224     case EQ_EXPR:
6225     case GE_EXPR:
6226       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6227       real_maxval (&max, neg, mode);
6228       return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6229                           arg0, build_real (TREE_TYPE (arg0), max));
6230
6231     case LT_EXPR:
6232       /* x < +Inf is always equal to x <= DBL_MAX.  */
6233       real_maxval (&max, neg, mode);
6234       return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6235                           arg0, build_real (TREE_TYPE (arg0), max));
6236
6237     case NE_EXPR:
6238       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6239       real_maxval (&max, neg, mode);
6240       if (! HONOR_NANS (mode))
6241         return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6242                             arg0, build_real (TREE_TYPE (arg0), max));
6243
6244       temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6245                           arg0, build_real (TREE_TYPE (arg0), max));
6246       return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
6247
6248     default:
6249       break;
6250     }
6251
6252   return NULL_TREE;
6253 }
6254
6255 /* Subroutine of fold() that optimizes comparisons of a division by
6256    a nonzero integer constant against an integer constant, i.e.
6257    X/C1 op C2.
6258
6259    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6260    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6261    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6262
6263    The function returns the constant folded tree if a simplification
6264    can be made, and NULL_TREE otherwise.  */
6265
6266 static tree
6267 fold_div_compare (location_t loc,
6268                   enum tree_code code, tree type, tree arg0, tree arg1)
6269 {
6270   tree prod, tmp, hi, lo;
6271   tree arg00 = TREE_OPERAND (arg0, 0);
6272   tree arg01 = TREE_OPERAND (arg0, 1);
6273   double_int val;
6274   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6275   bool neg_overflow;
6276   int overflow;
6277
6278   /* We have to do this the hard way to detect unsigned overflow.
6279      prod = int_const_binop (MULT_EXPR, arg01, arg1);  */
6280   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6281                                    TREE_INT_CST_HIGH (arg01),
6282                                    TREE_INT_CST_LOW (arg1),
6283                                    TREE_INT_CST_HIGH (arg1),
6284                                    &val.low, &val.high, unsigned_p);
6285   prod = force_fit_type_double (TREE_TYPE (arg00), val, -1, overflow);
6286   neg_overflow = false;
6287
6288   if (unsigned_p)
6289     {
6290       tmp = int_const_binop (MINUS_EXPR, arg01,
6291                              build_int_cst (TREE_TYPE (arg01), 1));
6292       lo = prod;
6293
6294       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp).  */
6295       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6296                                        TREE_INT_CST_HIGH (prod),
6297                                        TREE_INT_CST_LOW (tmp),
6298                                        TREE_INT_CST_HIGH (tmp),
6299                                        &val.low, &val.high, unsigned_p);
6300       hi = force_fit_type_double (TREE_TYPE (arg00), val,
6301                                   -1, overflow | TREE_OVERFLOW (prod));
6302     }
6303   else if (tree_int_cst_sgn (arg01) >= 0)
6304     {
6305       tmp = int_const_binop (MINUS_EXPR, arg01,
6306                              build_int_cst (TREE_TYPE (arg01), 1));
6307       switch (tree_int_cst_sgn (arg1))
6308         {
6309         case -1:
6310           neg_overflow = true;
6311           lo = int_const_binop (MINUS_EXPR, prod, tmp);
6312           hi = prod;
6313           break;
6314
6315         case  0:
6316           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6317           hi = tmp;
6318           break;
6319
6320         case  1:
6321           hi = int_const_binop (PLUS_EXPR, prod, tmp);
6322           lo = prod;
6323           break;
6324
6325         default:
6326           gcc_unreachable ();
6327         }
6328     }
6329   else
6330     {
6331       /* A negative divisor reverses the relational operators.  */
6332       code = swap_tree_comparison (code);
6333
6334       tmp = int_const_binop (PLUS_EXPR, arg01,
6335                              build_int_cst (TREE_TYPE (arg01), 1));
6336       switch (tree_int_cst_sgn (arg1))
6337         {
6338         case -1:
6339           hi = int_const_binop (MINUS_EXPR, prod, tmp);
6340           lo = prod;
6341           break;
6342
6343         case  0:
6344           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6345           lo = tmp;
6346           break;
6347
6348         case  1:
6349           neg_overflow = true;
6350           lo = int_const_binop (PLUS_EXPR, prod, tmp);
6351           hi = prod;
6352           break;
6353
6354         default:
6355           gcc_unreachable ();
6356         }
6357     }
6358
6359   switch (code)
6360     {
6361     case EQ_EXPR:
6362       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6363         return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6364       if (TREE_OVERFLOW (hi))
6365         return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6366       if (TREE_OVERFLOW (lo))
6367         return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6368       return build_range_check (loc, type, arg00, 1, lo, hi);
6369
6370     case NE_EXPR:
6371       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6372         return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6373       if (TREE_OVERFLOW (hi))
6374         return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6375       if (TREE_OVERFLOW (lo))
6376         return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6377       return build_range_check (loc, type, arg00, 0, lo, hi);
6378
6379     case LT_EXPR:
6380       if (TREE_OVERFLOW (lo))
6381         {
6382           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6383           return omit_one_operand_loc (loc, type, tmp, arg00);
6384         }
6385       return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6386
6387     case LE_EXPR:
6388       if (TREE_OVERFLOW (hi))
6389         {
6390           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6391           return omit_one_operand_loc (loc, type, tmp, arg00);
6392         }
6393       return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6394
6395     case GT_EXPR:
6396       if (TREE_OVERFLOW (hi))
6397         {
6398           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6399           return omit_one_operand_loc (loc, type, tmp, arg00);
6400         }
6401       return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6402
6403     case GE_EXPR:
6404       if (TREE_OVERFLOW (lo))
6405         {
6406           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6407           return omit_one_operand_loc (loc, type, tmp, arg00);
6408         }
6409       return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6410
6411     default:
6412       break;
6413     }
6414
6415   return NULL_TREE;
6416 }
6417
6418
6419 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6420    equality/inequality test, then return a simplified form of the test
6421    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6422    result type.  */
6423
6424 static tree
6425 fold_single_bit_test_into_sign_test (location_t loc,
6426                                      enum tree_code code, tree arg0, tree arg1,
6427                                      tree result_type)
6428 {
6429   /* If this is testing a single bit, we can optimize the test.  */
6430   if ((code == NE_EXPR || code == EQ_EXPR)
6431       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6432       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6433     {
6434       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6435          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6436       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6437
6438       if (arg00 != NULL_TREE
6439           /* This is only a win if casting to a signed type is cheap,
6440              i.e. when arg00's type is not a partial mode.  */
6441           && TYPE_PRECISION (TREE_TYPE (arg00))
6442              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6443         {
6444           tree stype = signed_type_for (TREE_TYPE (arg00));
6445           return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6446                               result_type,
6447                               fold_convert_loc (loc, stype, arg00),
6448                               build_int_cst (stype, 0));
6449         }
6450     }
6451
6452   return NULL_TREE;
6453 }
6454
6455 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6456    equality/inequality test, then return a simplified form of
6457    the test using shifts and logical operations.  Otherwise return
6458    NULL.  TYPE is the desired result type.  */
6459
6460 tree
6461 fold_single_bit_test (location_t loc, enum tree_code code,
6462                       tree arg0, tree arg1, tree result_type)
6463 {
6464   /* If this is testing a single bit, we can optimize the test.  */
6465   if ((code == NE_EXPR || code == EQ_EXPR)
6466       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6467       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6468     {
6469       tree inner = TREE_OPERAND (arg0, 0);
6470       tree type = TREE_TYPE (arg0);
6471       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6472       enum machine_mode operand_mode = TYPE_MODE (type);
6473       int ops_unsigned;
6474       tree signed_type, unsigned_type, intermediate_type;
6475       tree tem, one;
6476
6477       /* First, see if we can fold the single bit test into a sign-bit
6478          test.  */
6479       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6480                                                  result_type);
6481       if (tem)
6482         return tem;
6483
6484       /* Otherwise we have (A & C) != 0 where C is a single bit,
6485          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6486          Similarly for (A & C) == 0.  */
6487
6488       /* If INNER is a right shift of a constant and it plus BITNUM does
6489          not overflow, adjust BITNUM and INNER.  */
6490       if (TREE_CODE (inner) == RSHIFT_EXPR
6491           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6492           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6493           && bitnum < TYPE_PRECISION (type)
6494           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6495                                    bitnum - TYPE_PRECISION (type)))
6496         {
6497           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6498           inner = TREE_OPERAND (inner, 0);
6499         }
6500
6501       /* If we are going to be able to omit the AND below, we must do our
6502          operations as unsigned.  If we must use the AND, we have a choice.
6503          Normally unsigned is faster, but for some machines signed is.  */
6504 #ifdef LOAD_EXTEND_OP
6505       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6506                       && !flag_syntax_only) ? 0 : 1;
6507 #else
6508       ops_unsigned = 1;
6509 #endif
6510
6511       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6512       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6513       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6514       inner = fold_convert_loc (loc, intermediate_type, inner);
6515
6516       if (bitnum != 0)
6517         inner = build2 (RSHIFT_EXPR, intermediate_type,
6518                         inner, size_int (bitnum));
6519
6520       one = build_int_cst (intermediate_type, 1);
6521
6522       if (code == EQ_EXPR)
6523         inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6524
6525       /* Put the AND last so it can combine with more things.  */
6526       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6527
6528       /* Make sure to return the proper type.  */
6529       inner = fold_convert_loc (loc, result_type, inner);
6530
6531       return inner;
6532     }
6533   return NULL_TREE;
6534 }
6535
6536 /* Check whether we are allowed to reorder operands arg0 and arg1,
6537    such that the evaluation of arg1 occurs before arg0.  */
6538
6539 static bool
6540 reorder_operands_p (const_tree arg0, const_tree arg1)
6541 {
6542   if (! flag_evaluation_order)
6543       return true;
6544   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6545     return true;
6546   return ! TREE_SIDE_EFFECTS (arg0)
6547          && ! TREE_SIDE_EFFECTS (arg1);
6548 }
6549
6550 /* Test whether it is preferable two swap two operands, ARG0 and
6551    ARG1, for example because ARG0 is an integer constant and ARG1
6552    isn't.  If REORDER is true, only recommend swapping if we can
6553    evaluate the operands in reverse order.  */
6554
6555 bool
6556 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6557 {
6558   STRIP_SIGN_NOPS (arg0);
6559   STRIP_SIGN_NOPS (arg1);
6560
6561   if (TREE_CODE (arg1) == INTEGER_CST)
6562     return 0;
6563   if (TREE_CODE (arg0) == INTEGER_CST)
6564     return 1;
6565
6566   if (TREE_CODE (arg1) == REAL_CST)
6567     return 0;
6568   if (TREE_CODE (arg0) == REAL_CST)
6569     return 1;
6570
6571   if (TREE_CODE (arg1) == FIXED_CST)
6572     return 0;
6573   if (TREE_CODE (arg0) == FIXED_CST)
6574     return 1;
6575
6576   if (TREE_CODE (arg1) == COMPLEX_CST)
6577     return 0;
6578   if (TREE_CODE (arg0) == COMPLEX_CST)
6579     return 1;
6580
6581   if (TREE_CONSTANT (arg1))
6582     return 0;
6583   if (TREE_CONSTANT (arg0))
6584     return 1;
6585
6586   if (optimize_function_for_size_p (cfun))
6587     return 0;
6588
6589   if (reorder && flag_evaluation_order
6590       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6591     return 0;
6592
6593   /* It is preferable to swap two SSA_NAME to ensure a canonical form
6594      for commutative and comparison operators.  Ensuring a canonical
6595      form allows the optimizers to find additional redundancies without
6596      having to explicitly check for both orderings.  */
6597   if (TREE_CODE (arg0) == SSA_NAME
6598       && TREE_CODE (arg1) == SSA_NAME
6599       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6600     return 1;
6601
6602   /* Put SSA_NAMEs last.  */
6603   if (TREE_CODE (arg1) == SSA_NAME)
6604     return 0;
6605   if (TREE_CODE (arg0) == SSA_NAME)
6606     return 1;
6607
6608   /* Put variables last.  */
6609   if (DECL_P (arg1))
6610     return 0;
6611   if (DECL_P (arg0))
6612     return 1;
6613
6614   return 0;
6615 }
6616
6617 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6618    ARG0 is extended to a wider type.  */
6619
6620 static tree
6621 fold_widened_comparison (location_t loc, enum tree_code code,
6622                          tree type, tree arg0, tree arg1)
6623 {
6624   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6625   tree arg1_unw;
6626   tree shorter_type, outer_type;
6627   tree min, max;
6628   bool above, below;
6629
6630   if (arg0_unw == arg0)
6631     return NULL_TREE;
6632   shorter_type = TREE_TYPE (arg0_unw);
6633
6634 #ifdef HAVE_canonicalize_funcptr_for_compare
6635   /* Disable this optimization if we're casting a function pointer
6636      type on targets that require function pointer canonicalization.  */
6637   if (HAVE_canonicalize_funcptr_for_compare
6638       && TREE_CODE (shorter_type) == POINTER_TYPE
6639       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6640     return NULL_TREE;
6641 #endif
6642
6643   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6644     return NULL_TREE;
6645
6646   arg1_unw = get_unwidened (arg1, NULL_TREE);
6647
6648   /* If possible, express the comparison in the shorter mode.  */
6649   if ((code == EQ_EXPR || code == NE_EXPR
6650        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6651       && (TREE_TYPE (arg1_unw) == shorter_type
6652           || ((TYPE_PRECISION (shorter_type)
6653                >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6654               && (TYPE_UNSIGNED (shorter_type)
6655                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6656           || (TREE_CODE (arg1_unw) == INTEGER_CST
6657               && (TREE_CODE (shorter_type) == INTEGER_TYPE
6658                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6659               && int_fits_type_p (arg1_unw, shorter_type))))
6660     return fold_build2_loc (loc, code, type, arg0_unw,
6661                         fold_convert_loc (loc, shorter_type, arg1_unw));
6662
6663   if (TREE_CODE (arg1_unw) != INTEGER_CST
6664       || TREE_CODE (shorter_type) != INTEGER_TYPE
6665       || !int_fits_type_p (arg1_unw, shorter_type))
6666     return NULL_TREE;
6667
6668   /* If we are comparing with the integer that does not fit into the range
6669      of the shorter type, the result is known.  */
6670   outer_type = TREE_TYPE (arg1_unw);
6671   min = lower_bound_in_type (outer_type, shorter_type);
6672   max = upper_bound_in_type (outer_type, shorter_type);
6673
6674   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6675                                                    max, arg1_unw));
6676   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6677                                                    arg1_unw, min));
6678
6679   switch (code)
6680     {
6681     case EQ_EXPR:
6682       if (above || below)
6683         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6684       break;
6685
6686     case NE_EXPR:
6687       if (above || below)
6688         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6689       break;
6690
6691     case LT_EXPR:
6692     case LE_EXPR:
6693       if (above)
6694         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6695       else if (below)
6696         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6697
6698     case GT_EXPR:
6699     case GE_EXPR:
6700       if (above)
6701         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6702       else if (below)
6703         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6704
6705     default:
6706       break;
6707     }
6708
6709   return NULL_TREE;
6710 }
6711
6712 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6713    ARG0 just the signedness is changed.  */
6714
6715 static tree
6716 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
6717                               tree arg0, tree arg1)
6718 {
6719   tree arg0_inner;
6720   tree inner_type, outer_type;
6721
6722   if (!CONVERT_EXPR_P (arg0))
6723     return NULL_TREE;
6724
6725   outer_type = TREE_TYPE (arg0);
6726   arg0_inner = TREE_OPERAND (arg0, 0);
6727   inner_type = TREE_TYPE (arg0_inner);
6728
6729 #ifdef HAVE_canonicalize_funcptr_for_compare
6730   /* Disable this optimization if we're casting a function pointer
6731      type on targets that require function pointer canonicalization.  */
6732   if (HAVE_canonicalize_funcptr_for_compare
6733       && TREE_CODE (inner_type) == POINTER_TYPE
6734       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6735     return NULL_TREE;
6736 #endif
6737
6738   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6739     return NULL_TREE;
6740
6741   if (TREE_CODE (arg1) != INTEGER_CST
6742       && !(CONVERT_EXPR_P (arg1)
6743            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6744     return NULL_TREE;
6745
6746   if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6747        || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
6748       && code != NE_EXPR
6749       && code != EQ_EXPR)
6750     return NULL_TREE;
6751
6752   if (TREE_CODE (arg1) == INTEGER_CST)
6753     arg1 = force_fit_type_double (inner_type, tree_to_double_int (arg1),
6754                                   0, TREE_OVERFLOW (arg1));
6755   else
6756     arg1 = fold_convert_loc (loc, inner_type, arg1);
6757
6758   return fold_build2_loc (loc, code, type, arg0_inner, arg1);
6759 }
6760
6761 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6762    step of the array.  Reconstructs s and delta in the case of s *
6763    delta being an integer constant (and thus already folded).  ADDR is
6764    the address. MULT is the multiplicative expression.  If the
6765    function succeeds, the new address expression is returned.
6766    Otherwise NULL_TREE is returned.  LOC is the location of the
6767    resulting expression.  */
6768
6769 static tree
6770 try_move_mult_to_index (location_t loc, tree addr, tree op1)
6771 {
6772   tree s, delta, step;
6773   tree ref = TREE_OPERAND (addr, 0), pref;
6774   tree ret, pos;
6775   tree itype;
6776   bool mdim = false;
6777
6778   /*  Strip the nops that might be added when converting op1 to sizetype. */
6779   STRIP_NOPS (op1);
6780
6781   /* Canonicalize op1 into a possibly non-constant delta
6782      and an INTEGER_CST s.  */
6783   if (TREE_CODE (op1) == MULT_EXPR)
6784     {
6785       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6786
6787       STRIP_NOPS (arg0);
6788       STRIP_NOPS (arg1);
6789
6790       if (TREE_CODE (arg0) == INTEGER_CST)
6791         {
6792           s = arg0;
6793           delta = arg1;
6794         }
6795       else if (TREE_CODE (arg1) == INTEGER_CST)
6796         {
6797           s = arg1;
6798           delta = arg0;
6799         }
6800       else
6801         return NULL_TREE;
6802     }
6803   else if (TREE_CODE (op1) == INTEGER_CST)
6804     {
6805       delta = op1;
6806       s = NULL_TREE;
6807     }
6808   else
6809     {
6810       /* Simulate we are delta * 1.  */
6811       delta = op1;
6812       s = integer_one_node;
6813     }
6814
6815   for (;; ref = TREE_OPERAND (ref, 0))
6816     {
6817       if (TREE_CODE (ref) == ARRAY_REF)
6818         {
6819           tree domain;
6820
6821           /* Remember if this was a multi-dimensional array.  */
6822           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6823             mdim = true;
6824
6825           domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6826           if (! domain)
6827             continue;
6828           itype = TREE_TYPE (domain);
6829
6830           step = array_ref_element_size (ref);
6831           if (TREE_CODE (step) != INTEGER_CST)
6832             continue;
6833
6834           if (s)
6835             {
6836               if (! tree_int_cst_equal (step, s))
6837                 continue;
6838             }
6839           else
6840             {
6841               /* Try if delta is a multiple of step.  */
6842               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6843               if (! tmp)
6844                 continue;
6845               delta = tmp;
6846             }
6847
6848           /* Only fold here if we can verify we do not overflow one
6849              dimension of a multi-dimensional array.  */
6850           if (mdim)
6851             {
6852               tree tmp;
6853
6854               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
6855                   || !TYPE_MAX_VALUE (domain)
6856                   || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
6857                 continue;
6858
6859               tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
6860                                      fold_convert_loc (loc, itype,
6861                                                        TREE_OPERAND (ref, 1)),
6862                                      fold_convert_loc (loc, itype, delta));
6863               if (!tmp
6864                   || TREE_CODE (tmp) != INTEGER_CST
6865                   || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
6866                 continue;
6867             }
6868
6869           break;
6870         }
6871       else
6872         mdim = false;
6873
6874       if (!handled_component_p (ref))
6875         return NULL_TREE;
6876     }
6877
6878   /* We found the suitable array reference.  So copy everything up to it,
6879      and replace the index.  */
6880
6881   pref = TREE_OPERAND (addr, 0);
6882   ret = copy_node (pref);
6883   SET_EXPR_LOCATION (ret, loc);
6884   pos = ret;
6885
6886   while (pref != ref)
6887     {
6888       pref = TREE_OPERAND (pref, 0);
6889       TREE_OPERAND (pos, 0) = copy_node (pref);
6890       pos = TREE_OPERAND (pos, 0);
6891     }
6892
6893   TREE_OPERAND (pos, 1) = fold_build2_loc (loc, PLUS_EXPR, itype,
6894                                        fold_convert_loc (loc, itype,
6895                                                          TREE_OPERAND (pos, 1)),
6896                                        fold_convert_loc (loc, itype, delta));
6897
6898   return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
6899 }
6900
6901
6902 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
6903    means A >= Y && A != MAX, but in this case we know that
6904    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
6905
6906 static tree
6907 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6908 {
6909   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6910
6911   if (TREE_CODE (bound) == LT_EXPR)
6912     a = TREE_OPERAND (bound, 0);
6913   else if (TREE_CODE (bound) == GT_EXPR)
6914     a = TREE_OPERAND (bound, 1);
6915   else
6916     return NULL_TREE;
6917
6918   typea = TREE_TYPE (a);
6919   if (!INTEGRAL_TYPE_P (typea)
6920       && !POINTER_TYPE_P (typea))
6921     return NULL_TREE;
6922
6923   if (TREE_CODE (ineq) == LT_EXPR)
6924     {
6925       a1 = TREE_OPERAND (ineq, 1);
6926       y = TREE_OPERAND (ineq, 0);
6927     }
6928   else if (TREE_CODE (ineq) == GT_EXPR)
6929     {
6930       a1 = TREE_OPERAND (ineq, 0);
6931       y = TREE_OPERAND (ineq, 1);
6932     }
6933   else
6934     return NULL_TREE;
6935
6936   if (TREE_TYPE (a1) != typea)
6937     return NULL_TREE;
6938
6939   if (POINTER_TYPE_P (typea))
6940     {
6941       /* Convert the pointer types into integer before taking the difference.  */
6942       tree ta = fold_convert_loc (loc, ssizetype, a);
6943       tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6944       diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6945     }
6946   else
6947     diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6948
6949   if (!diff || !integer_onep (diff))
6950    return NULL_TREE;
6951
6952   return fold_build2_loc (loc, GE_EXPR, type, a, y);
6953 }
6954
6955 /* Fold a sum or difference of at least one multiplication.
6956    Returns the folded tree or NULL if no simplification could be made.  */
6957
6958 static tree
6959 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6960                           tree arg0, tree arg1)
6961 {
6962   tree arg00, arg01, arg10, arg11;
6963   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6964
6965   /* (A * C) +- (B * C) -> (A+-B) * C.
6966      (A * C) +- A -> A * (C+-1).
6967      We are most concerned about the case where C is a constant,
6968      but other combinations show up during loop reduction.  Since
6969      it is not difficult, try all four possibilities.  */
6970
6971   if (TREE_CODE (arg0) == MULT_EXPR)
6972     {
6973       arg00 = TREE_OPERAND (arg0, 0);
6974       arg01 = TREE_OPERAND (arg0, 1);
6975     }
6976   else if (TREE_CODE (arg0) == INTEGER_CST)
6977     {
6978       arg00 = build_one_cst (type);
6979       arg01 = arg0;
6980     }
6981   else
6982     {
6983       /* We cannot generate constant 1 for fract.  */
6984       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6985         return NULL_TREE;
6986       arg00 = arg0;
6987       arg01 = build_one_cst (type);
6988     }
6989   if (TREE_CODE (arg1) == MULT_EXPR)
6990     {
6991       arg10 = TREE_OPERAND (arg1, 0);
6992       arg11 = TREE_OPERAND (arg1, 1);
6993     }
6994   else if (TREE_CODE (arg1) == INTEGER_CST)
6995     {
6996       arg10 = build_one_cst (type);
6997       /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6998          the purpose of this canonicalization.  */
6999       if (TREE_INT_CST_HIGH (arg1) == -1
7000           && negate_expr_p (arg1)
7001           && code == PLUS_EXPR)
7002         {
7003           arg11 = negate_expr (arg1);
7004           code = MINUS_EXPR;
7005         }
7006       else
7007         arg11 = arg1;
7008     }
7009   else
7010     {
7011       /* We cannot generate constant 1 for fract.  */
7012       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7013         return NULL_TREE;
7014       arg10 = arg1;
7015       arg11 = build_one_cst (type);
7016     }
7017   same = NULL_TREE;
7018
7019   if (operand_equal_p (arg01, arg11, 0))
7020     same = arg01, alt0 = arg00, alt1 = arg10;
7021   else if (operand_equal_p (arg00, arg10, 0))
7022     same = arg00, alt0 = arg01, alt1 = arg11;
7023   else if (operand_equal_p (arg00, arg11, 0))
7024     same = arg00, alt0 = arg01, alt1 = arg10;
7025   else if (operand_equal_p (arg01, arg10, 0))
7026     same = arg01, alt0 = arg00, alt1 = arg11;
7027
7028   /* No identical multiplicands; see if we can find a common
7029      power-of-two factor in non-power-of-two multiplies.  This
7030      can help in multi-dimensional array access.  */
7031   else if (host_integerp (arg01, 0)
7032            && host_integerp (arg11, 0))
7033     {
7034       HOST_WIDE_INT int01, int11, tmp;
7035       bool swap = false;
7036       tree maybe_same;
7037       int01 = TREE_INT_CST_LOW (arg01);
7038       int11 = TREE_INT_CST_LOW (arg11);
7039
7040       /* Move min of absolute values to int11.  */
7041       if ((int01 >= 0 ? int01 : -int01)
7042           < (int11 >= 0 ? int11 : -int11))
7043         {
7044           tmp = int01, int01 = int11, int11 = tmp;
7045           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7046           maybe_same = arg01;
7047           swap = true;
7048         }
7049       else
7050         maybe_same = arg11;
7051
7052       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
7053           /* The remainder should not be a constant, otherwise we
7054              end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7055              increased the number of multiplications necessary.  */
7056           && TREE_CODE (arg10) != INTEGER_CST)
7057         {
7058           alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7059                               build_int_cst (TREE_TYPE (arg00),
7060                                              int01 / int11));
7061           alt1 = arg10;
7062           same = maybe_same;
7063           if (swap)
7064             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7065         }
7066     }
7067
7068   if (same)
7069     return fold_build2_loc (loc, MULT_EXPR, type,
7070                         fold_build2_loc (loc, code, type,
7071                                      fold_convert_loc (loc, type, alt0),
7072                                      fold_convert_loc (loc, type, alt1)),
7073                         fold_convert_loc (loc, type, same));
7074
7075   return NULL_TREE;
7076 }
7077
7078 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7079    specified by EXPR into the buffer PTR of length LEN bytes.
7080    Return the number of bytes placed in the buffer, or zero
7081    upon failure.  */
7082
7083 static int
7084 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7085 {
7086   tree type = TREE_TYPE (expr);
7087   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7088   int byte, offset, word, words;
7089   unsigned char value;
7090
7091   if (total_bytes > len)
7092     return 0;
7093   words = total_bytes / UNITS_PER_WORD;
7094
7095   for (byte = 0; byte < total_bytes; byte++)
7096     {
7097       int bitpos = byte * BITS_PER_UNIT;
7098       if (bitpos < HOST_BITS_PER_WIDE_INT)
7099         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7100       else
7101         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7102                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7103
7104       if (total_bytes > UNITS_PER_WORD)
7105         {
7106           word = byte / UNITS_PER_WORD;
7107           if (WORDS_BIG_ENDIAN)
7108             word = (words - 1) - word;
7109           offset = word * UNITS_PER_WORD;
7110           if (BYTES_BIG_ENDIAN)
7111             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7112           else
7113             offset += byte % UNITS_PER_WORD;
7114         }
7115       else
7116         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7117       ptr[offset] = value;
7118     }
7119   return total_bytes;
7120 }
7121
7122
7123 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7124    specified by EXPR into the buffer PTR of length LEN bytes.
7125    Return the number of bytes placed in the buffer, or zero
7126    upon failure.  */
7127
7128 static int
7129 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7130 {
7131   tree type = TREE_TYPE (expr);
7132   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7133   int byte, offset, word, words, bitpos;
7134   unsigned char value;
7135
7136   /* There are always 32 bits in each long, no matter the size of
7137      the hosts long.  We handle floating point representations with
7138      up to 192 bits.  */
7139   long tmp[6];
7140
7141   if (total_bytes > len)
7142     return 0;
7143   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7144
7145   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7146
7147   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7148        bitpos += BITS_PER_UNIT)
7149     {
7150       byte = (bitpos / BITS_PER_UNIT) & 3;
7151       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7152
7153       if (UNITS_PER_WORD < 4)
7154         {
7155           word = byte / UNITS_PER_WORD;
7156           if (WORDS_BIG_ENDIAN)
7157             word = (words - 1) - word;
7158           offset = word * UNITS_PER_WORD;
7159           if (BYTES_BIG_ENDIAN)
7160             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7161           else
7162             offset += byte % UNITS_PER_WORD;
7163         }
7164       else
7165         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7166       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7167     }
7168   return total_bytes;
7169 }
7170
7171 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7172    specified by EXPR into the buffer PTR of length LEN bytes.
7173    Return the number of bytes placed in the buffer, or zero
7174    upon failure.  */
7175
7176 static int
7177 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7178 {
7179   int rsize, isize;
7180   tree part;
7181
7182   part = TREE_REALPART (expr);
7183   rsize = native_encode_expr (part, ptr, len);
7184   if (rsize == 0)
7185     return 0;
7186   part = TREE_IMAGPART (expr);
7187   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7188   if (isize != rsize)
7189     return 0;
7190   return rsize + isize;
7191 }
7192
7193
7194 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7195    specified by EXPR into the buffer PTR of length LEN bytes.
7196    Return the number of bytes placed in the buffer, or zero
7197    upon failure.  */
7198
7199 static int
7200 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7201 {
7202   int i, size, offset, count;
7203   tree itype, elem, elements;
7204
7205   offset = 0;
7206   elements = TREE_VECTOR_CST_ELTS (expr);
7207   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7208   itype = TREE_TYPE (TREE_TYPE (expr));
7209   size = GET_MODE_SIZE (TYPE_MODE (itype));
7210   for (i = 0; i < count; i++)
7211     {
7212       if (elements)
7213         {
7214           elem = TREE_VALUE (elements);
7215           elements = TREE_CHAIN (elements);
7216         }
7217       else
7218         elem = NULL_TREE;
7219
7220       if (elem)
7221         {
7222           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7223             return 0;
7224         }
7225       else
7226         {
7227           if (offset + size > len)
7228             return 0;
7229           memset (ptr+offset, 0, size);
7230         }
7231       offset += size;
7232     }
7233   return offset;
7234 }
7235
7236
7237 /* Subroutine of native_encode_expr.  Encode the STRING_CST
7238    specified by EXPR into the buffer PTR of length LEN bytes.
7239    Return the number of bytes placed in the buffer, or zero
7240    upon failure.  */
7241
7242 static int
7243 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7244 {
7245   tree type = TREE_TYPE (expr);
7246   HOST_WIDE_INT total_bytes;
7247
7248   if (TREE_CODE (type) != ARRAY_TYPE
7249       || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7250       || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7251       || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7252     return 0;
7253   total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7254   if (total_bytes > len)
7255     return 0;
7256   if (TREE_STRING_LENGTH (expr) < total_bytes)
7257     {
7258       memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7259       memset (ptr + TREE_STRING_LENGTH (expr), 0,
7260               total_bytes - TREE_STRING_LENGTH (expr));
7261     }
7262   else
7263     memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7264   return total_bytes;
7265 }
7266
7267
7268 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7269    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7270    buffer PTR of length LEN bytes.  Return the number of bytes
7271    placed in the buffer, or zero upon failure.  */
7272
7273 int
7274 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7275 {
7276   switch (TREE_CODE (expr))
7277     {
7278     case INTEGER_CST:
7279       return native_encode_int (expr, ptr, len);
7280
7281     case REAL_CST:
7282       return native_encode_real (expr, ptr, len);
7283
7284     case COMPLEX_CST:
7285       return native_encode_complex (expr, ptr, len);
7286
7287     case VECTOR_CST:
7288       return native_encode_vector (expr, ptr, len);
7289
7290     case STRING_CST:
7291       return native_encode_string (expr, ptr, len);
7292
7293     default:
7294       return 0;
7295     }
7296 }
7297
7298
7299 /* Subroutine of native_interpret_expr.  Interpret the contents of
7300    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7301    If the buffer cannot be interpreted, return NULL_TREE.  */
7302
7303 static tree
7304 native_interpret_int (tree type, const unsigned char *ptr, int len)
7305 {
7306   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7307   int byte, offset, word, words;
7308   unsigned char value;
7309   double_int result;
7310
7311   if (total_bytes > len)
7312     return NULL_TREE;
7313   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7314     return NULL_TREE;
7315
7316   result = double_int_zero;
7317   words = total_bytes / UNITS_PER_WORD;
7318
7319   for (byte = 0; byte < total_bytes; byte++)
7320     {
7321       int bitpos = byte * BITS_PER_UNIT;
7322       if (total_bytes > UNITS_PER_WORD)
7323         {
7324           word = byte / UNITS_PER_WORD;
7325           if (WORDS_BIG_ENDIAN)
7326             word = (words - 1) - word;
7327           offset = word * UNITS_PER_WORD;
7328           if (BYTES_BIG_ENDIAN)
7329             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7330           else
7331             offset += byte % UNITS_PER_WORD;
7332         }
7333       else
7334         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7335       value = ptr[offset];
7336
7337       if (bitpos < HOST_BITS_PER_WIDE_INT)
7338         result.low |= (unsigned HOST_WIDE_INT) value << bitpos;
7339       else
7340         result.high |= (unsigned HOST_WIDE_INT) value
7341                        << (bitpos - HOST_BITS_PER_WIDE_INT);
7342     }
7343
7344   return double_int_to_tree (type, result);
7345 }
7346
7347
7348 /* Subroutine of native_interpret_expr.  Interpret the contents of
7349    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7350    If the buffer cannot be interpreted, return NULL_TREE.  */
7351
7352 static tree
7353 native_interpret_real (tree type, const unsigned char *ptr, int len)
7354 {
7355   enum machine_mode mode = TYPE_MODE (type);
7356   int total_bytes = GET_MODE_SIZE (mode);
7357   int byte, offset, word, words, bitpos;
7358   unsigned char value;
7359   /* There are always 32 bits in each long, no matter the size of
7360      the hosts long.  We handle floating point representations with
7361      up to 192 bits.  */
7362   REAL_VALUE_TYPE r;
7363   long tmp[6];
7364
7365   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7366   if (total_bytes > len || total_bytes > 24)
7367     return NULL_TREE;
7368   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7369
7370   memset (tmp, 0, sizeof (tmp));
7371   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7372        bitpos += BITS_PER_UNIT)
7373     {
7374       byte = (bitpos / BITS_PER_UNIT) & 3;
7375       if (UNITS_PER_WORD < 4)
7376         {
7377           word = byte / UNITS_PER_WORD;
7378           if (WORDS_BIG_ENDIAN)
7379             word = (words - 1) - word;
7380           offset = word * UNITS_PER_WORD;
7381           if (BYTES_BIG_ENDIAN)
7382             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7383           else
7384             offset += byte % UNITS_PER_WORD;
7385         }
7386       else
7387         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7388       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7389
7390       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7391     }
7392
7393   real_from_target (&r, tmp, mode);
7394   return build_real (type, r);
7395 }
7396
7397
7398 /* Subroutine of native_interpret_expr.  Interpret the contents of
7399    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7400    If the buffer cannot be interpreted, return NULL_TREE.  */
7401
7402 static tree
7403 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7404 {
7405   tree etype, rpart, ipart;
7406   int size;
7407
7408   etype = TREE_TYPE (type);
7409   size = GET_MODE_SIZE (TYPE_MODE (etype));
7410   if (size * 2 > len)
7411     return NULL_TREE;
7412   rpart = native_interpret_expr (etype, ptr, size);
7413   if (!rpart)
7414     return NULL_TREE;
7415   ipart = native_interpret_expr (etype, ptr+size, size);
7416   if (!ipart)
7417     return NULL_TREE;
7418   return build_complex (type, rpart, ipart);
7419 }
7420
7421
7422 /* Subroutine of native_interpret_expr.  Interpret the contents of
7423    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7424    If the buffer cannot be interpreted, return NULL_TREE.  */
7425
7426 static tree
7427 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7428 {
7429   tree etype, elem, elements;
7430   int i, size, count;
7431
7432   etype = TREE_TYPE (type);
7433   size = GET_MODE_SIZE (TYPE_MODE (etype));
7434   count = TYPE_VECTOR_SUBPARTS (type);
7435   if (size * count > len)
7436     return NULL_TREE;
7437
7438   elements = NULL_TREE;
7439   for (i = count - 1; i >= 0; i--)
7440     {
7441       elem = native_interpret_expr (etype, ptr+(i*size), size);
7442       if (!elem)
7443         return NULL_TREE;
7444       elements = tree_cons (NULL_TREE, elem, elements);
7445     }
7446   return build_vector (type, elements);
7447 }
7448
7449
7450 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7451    the buffer PTR of length LEN as a constant of type TYPE.  For
7452    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7453    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7454    return NULL_TREE.  */
7455
7456 tree
7457 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7458 {
7459   switch (TREE_CODE (type))
7460     {
7461     case INTEGER_TYPE:
7462     case ENUMERAL_TYPE:
7463     case BOOLEAN_TYPE:
7464       return native_interpret_int (type, ptr, len);
7465
7466     case REAL_TYPE:
7467       return native_interpret_real (type, ptr, len);
7468
7469     case COMPLEX_TYPE:
7470       return native_interpret_complex (type, ptr, len);
7471
7472     case VECTOR_TYPE:
7473       return native_interpret_vector (type, ptr, len);
7474
7475     default:
7476       return NULL_TREE;
7477     }
7478 }
7479
7480
7481 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7482    TYPE at compile-time.  If we're unable to perform the conversion
7483    return NULL_TREE.  */
7484
7485 static tree
7486 fold_view_convert_expr (tree type, tree expr)
7487 {
7488   /* We support up to 512-bit values (for V8DFmode).  */
7489   unsigned char buffer[64];
7490   int len;
7491
7492   /* Check that the host and target are sane.  */
7493   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7494     return NULL_TREE;
7495
7496   len = native_encode_expr (expr, buffer, sizeof (buffer));
7497   if (len == 0)
7498     return NULL_TREE;
7499
7500   return native_interpret_expr (type, buffer, len);
7501 }
7502
7503 /* Build an expression for the address of T.  Folds away INDIRECT_REF
7504    to avoid confusing the gimplify process.  */
7505
7506 tree
7507 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7508 {
7509   /* The size of the object is not relevant when talking about its address.  */
7510   if (TREE_CODE (t) == WITH_SIZE_EXPR)
7511     t = TREE_OPERAND (t, 0);
7512
7513   if (TREE_CODE (t) == INDIRECT_REF)
7514     {
7515       t = TREE_OPERAND (t, 0);
7516
7517       if (TREE_TYPE (t) != ptrtype)
7518         t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7519     }
7520   else if (TREE_CODE (t) == MEM_REF
7521       && integer_zerop (TREE_OPERAND (t, 1)))
7522     return TREE_OPERAND (t, 0);
7523   else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7524     {
7525       t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7526
7527       if (TREE_TYPE (t) != ptrtype)
7528         t = fold_convert_loc (loc, ptrtype, t);
7529     }
7530   else
7531     t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7532
7533   return t;
7534 }
7535
7536 /* Build an expression for the address of T.  */
7537
7538 tree
7539 build_fold_addr_expr_loc (location_t loc, tree t)
7540 {
7541   tree ptrtype = build_pointer_type (TREE_TYPE (t));
7542
7543   return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7544 }
7545
7546 /* Fold a unary expression of code CODE and type TYPE with operand
7547    OP0.  Return the folded expression if folding is successful.
7548    Otherwise, return NULL_TREE.  */
7549
7550 tree
7551 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7552 {
7553   tree tem;
7554   tree arg0;
7555   enum tree_code_class kind = TREE_CODE_CLASS (code);
7556
7557   gcc_assert (IS_EXPR_CODE_CLASS (kind)
7558               && TREE_CODE_LENGTH (code) == 1);
7559
7560   arg0 = op0;
7561   if (arg0)
7562     {
7563       if (CONVERT_EXPR_CODE_P (code)
7564           || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7565         {
7566           /* Don't use STRIP_NOPS, because signedness of argument type
7567              matters.  */
7568           STRIP_SIGN_NOPS (arg0);
7569         }
7570       else
7571         {
7572           /* Strip any conversions that don't change the mode.  This
7573              is safe for every expression, except for a comparison
7574              expression because its signedness is derived from its
7575              operands.
7576
7577              Note that this is done as an internal manipulation within
7578              the constant folder, in order to find the simplest
7579              representation of the arguments so that their form can be
7580              studied.  In any cases, the appropriate type conversions
7581              should be put back in the tree that will get out of the
7582              constant folder.  */
7583           STRIP_NOPS (arg0);
7584         }
7585     }
7586
7587   if (TREE_CODE_CLASS (code) == tcc_unary)
7588     {
7589       if (TREE_CODE (arg0) == COMPOUND_EXPR)
7590         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7591                        fold_build1_loc (loc, code, type,
7592                                     fold_convert_loc (loc, TREE_TYPE (op0),
7593                                                       TREE_OPERAND (arg0, 1))));
7594       else if (TREE_CODE (arg0) == COND_EXPR)
7595         {
7596           tree arg01 = TREE_OPERAND (arg0, 1);
7597           tree arg02 = TREE_OPERAND (arg0, 2);
7598           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7599             arg01 = fold_build1_loc (loc, code, type,
7600                                  fold_convert_loc (loc,
7601                                                    TREE_TYPE (op0), arg01));
7602           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7603             arg02 = fold_build1_loc (loc, code, type,
7604                                  fold_convert_loc (loc,
7605                                                    TREE_TYPE (op0), arg02));
7606           tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7607                              arg01, arg02);
7608
7609           /* If this was a conversion, and all we did was to move into
7610              inside the COND_EXPR, bring it back out.  But leave it if
7611              it is a conversion from integer to integer and the
7612              result precision is no wider than a word since such a
7613              conversion is cheap and may be optimized away by combine,
7614              while it couldn't if it were outside the COND_EXPR.  Then return
7615              so we don't get into an infinite recursion loop taking the
7616              conversion out and then back in.  */
7617
7618           if ((CONVERT_EXPR_CODE_P (code)
7619                || code == NON_LVALUE_EXPR)
7620               && TREE_CODE (tem) == COND_EXPR
7621               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7622               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7623               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7624               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7625               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7626                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7627               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7628                      && (INTEGRAL_TYPE_P
7629                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7630                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7631                   || flag_syntax_only))
7632             tem = build1_loc (loc, code, type,
7633                               build3 (COND_EXPR,
7634                                       TREE_TYPE (TREE_OPERAND
7635                                                  (TREE_OPERAND (tem, 1), 0)),
7636                                       TREE_OPERAND (tem, 0),
7637                                       TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7638                                       TREE_OPERAND (TREE_OPERAND (tem, 2),
7639                                                     0)));
7640           return tem;
7641         }
7642    }
7643
7644   switch (code)
7645     {
7646     case PAREN_EXPR:
7647       /* Re-association barriers around constants and other re-association
7648          barriers can be removed.  */
7649       if (CONSTANT_CLASS_P (op0)
7650           || TREE_CODE (op0) == PAREN_EXPR)
7651         return fold_convert_loc (loc, type, op0);
7652       return NULL_TREE;
7653
7654     CASE_CONVERT:
7655     case FLOAT_EXPR:
7656     case FIX_TRUNC_EXPR:
7657       if (TREE_TYPE (op0) == type)
7658         return op0;
7659
7660       if (COMPARISON_CLASS_P (op0))
7661         {
7662           /* If we have (type) (a CMP b) and type is an integral type, return
7663              new expression involving the new type.  Canonicalize
7664              (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7665              non-integral type.
7666              Do not fold the result as that would not simplify further, also
7667              folding again results in recursions.  */
7668           if (INTEGRAL_TYPE_P (type))
7669             return build2_loc (loc, TREE_CODE (op0), type,
7670                                TREE_OPERAND (op0, 0),
7671                                TREE_OPERAND (op0, 1));
7672           else
7673             return build3_loc (loc, COND_EXPR, type, op0,
7674                                fold_convert (type, boolean_true_node),
7675                                fold_convert (type, boolean_false_node));
7676         }
7677
7678       /* Handle cases of two conversions in a row.  */
7679       if (CONVERT_EXPR_P (op0))
7680         {
7681           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7682           tree inter_type = TREE_TYPE (op0);
7683           int inside_int = INTEGRAL_TYPE_P (inside_type);
7684           int inside_ptr = POINTER_TYPE_P (inside_type);
7685           int inside_float = FLOAT_TYPE_P (inside_type);
7686           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7687           unsigned int inside_prec = TYPE_PRECISION (inside_type);
7688           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7689           int inter_int = INTEGRAL_TYPE_P (inter_type);
7690           int inter_ptr = POINTER_TYPE_P (inter_type);
7691           int inter_float = FLOAT_TYPE_P (inter_type);
7692           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7693           unsigned int inter_prec = TYPE_PRECISION (inter_type);
7694           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7695           int final_int = INTEGRAL_TYPE_P (type);
7696           int final_ptr = POINTER_TYPE_P (type);
7697           int final_float = FLOAT_TYPE_P (type);
7698           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7699           unsigned int final_prec = TYPE_PRECISION (type);
7700           int final_unsignedp = TYPE_UNSIGNED (type);
7701
7702           /* In addition to the cases of two conversions in a row
7703              handled below, if we are converting something to its own
7704              type via an object of identical or wider precision, neither
7705              conversion is needed.  */
7706           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7707               && (((inter_int || inter_ptr) && final_int)
7708                   || (inter_float && final_float))
7709               && inter_prec >= final_prec)
7710             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7711
7712           /* Likewise, if the intermediate and initial types are either both
7713              float or both integer, we don't need the middle conversion if the
7714              former is wider than the latter and doesn't change the signedness
7715              (for integers).  Avoid this if the final type is a pointer since
7716              then we sometimes need the middle conversion.  Likewise if the
7717              final type has a precision not equal to the size of its mode.  */
7718           if (((inter_int && inside_int)
7719                || (inter_float && inside_float)
7720                || (inter_vec && inside_vec))
7721               && inter_prec >= inside_prec
7722               && (inter_float || inter_vec
7723                   || inter_unsignedp == inside_unsignedp)
7724               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7725                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
7726               && ! final_ptr
7727               && (! final_vec || inter_prec == inside_prec))
7728             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7729
7730           /* If we have a sign-extension of a zero-extended value, we can
7731              replace that by a single zero-extension.  */
7732           if (inside_int && inter_int && final_int
7733               && inside_prec < inter_prec && inter_prec < final_prec
7734               && inside_unsignedp && !inter_unsignedp)
7735             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7736
7737           /* Two conversions in a row are not needed unless:
7738              - some conversion is floating-point (overstrict for now), or
7739              - some conversion is a vector (overstrict for now), or
7740              - the intermediate type is narrower than both initial and
7741                final, or
7742              - the intermediate type and innermost type differ in signedness,
7743                and the outermost type is wider than the intermediate, or
7744              - the initial type is a pointer type and the precisions of the
7745                intermediate and final types differ, or
7746              - the final type is a pointer type and the precisions of the
7747                initial and intermediate types differ.  */
7748           if (! inside_float && ! inter_float && ! final_float
7749               && ! inside_vec && ! inter_vec && ! final_vec
7750               && (inter_prec >= inside_prec || inter_prec >= final_prec)
7751               && ! (inside_int && inter_int
7752                     && inter_unsignedp != inside_unsignedp
7753                     && inter_prec < final_prec)
7754               && ((inter_unsignedp && inter_prec > inside_prec)
7755                   == (final_unsignedp && final_prec > inter_prec))
7756               && ! (inside_ptr && inter_prec != final_prec)
7757               && ! (final_ptr && inside_prec != inter_prec)
7758               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7759                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7760             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7761         }
7762
7763       /* Handle (T *)&A.B.C for A being of type T and B and C
7764          living at offset zero.  This occurs frequently in
7765          C++ upcasting and then accessing the base.  */
7766       if (TREE_CODE (op0) == ADDR_EXPR
7767           && POINTER_TYPE_P (type)
7768           && handled_component_p (TREE_OPERAND (op0, 0)))
7769         {
7770           HOST_WIDE_INT bitsize, bitpos;
7771           tree offset;
7772           enum machine_mode mode;
7773           int unsignedp, volatilep;
7774           tree base = TREE_OPERAND (op0, 0);
7775           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7776                                       &mode, &unsignedp, &volatilep, false);
7777           /* If the reference was to a (constant) zero offset, we can use
7778              the address of the base if it has the same base type
7779              as the result type and the pointer type is unqualified.  */
7780           if (! offset && bitpos == 0
7781               && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7782                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7783               && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7784             return fold_convert_loc (loc, type,
7785                                      build_fold_addr_expr_loc (loc, base));
7786         }
7787
7788       if (TREE_CODE (op0) == MODIFY_EXPR
7789           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7790           /* Detect assigning a bitfield.  */
7791           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7792                && DECL_BIT_FIELD
7793                (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7794         {
7795           /* Don't leave an assignment inside a conversion
7796              unless assigning a bitfield.  */
7797           tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7798           /* First do the assignment, then return converted constant.  */
7799           tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7800           TREE_NO_WARNING (tem) = 1;
7801           TREE_USED (tem) = 1;
7802           return tem;
7803         }
7804
7805       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7806          constants (if x has signed type, the sign bit cannot be set
7807          in c).  This folds extension into the BIT_AND_EXPR.
7808          ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7809          very likely don't have maximal range for their precision and this
7810          transformation effectively doesn't preserve non-maximal ranges.  */
7811       if (TREE_CODE (type) == INTEGER_TYPE
7812           && TREE_CODE (op0) == BIT_AND_EXPR
7813           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7814         {
7815           tree and_expr = op0;
7816           tree and0 = TREE_OPERAND (and_expr, 0);
7817           tree and1 = TREE_OPERAND (and_expr, 1);
7818           int change = 0;
7819
7820           if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7821               || (TYPE_PRECISION (type)
7822                   <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7823             change = 1;
7824           else if (TYPE_PRECISION (TREE_TYPE (and1))
7825                    <= HOST_BITS_PER_WIDE_INT
7826                    && host_integerp (and1, 1))
7827             {
7828               unsigned HOST_WIDE_INT cst;
7829
7830               cst = tree_low_cst (and1, 1);
7831               cst &= (HOST_WIDE_INT) -1
7832                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7833               change = (cst == 0);
7834 #ifdef LOAD_EXTEND_OP
7835               if (change
7836                   && !flag_syntax_only
7837                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7838                       == ZERO_EXTEND))
7839                 {
7840                   tree uns = unsigned_type_for (TREE_TYPE (and0));
7841                   and0 = fold_convert_loc (loc, uns, and0);
7842                   and1 = fold_convert_loc (loc, uns, and1);
7843                 }
7844 #endif
7845             }
7846           if (change)
7847             {
7848               tem = force_fit_type_double (type, tree_to_double_int (and1),
7849                                            0, TREE_OVERFLOW (and1));
7850               return fold_build2_loc (loc, BIT_AND_EXPR, type,
7851                                   fold_convert_loc (loc, type, and0), tem);
7852             }
7853         }
7854
7855       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7856          when one of the new casts will fold away. Conservatively we assume
7857          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7858       if (POINTER_TYPE_P (type)
7859           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7860           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7861               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7862               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7863         {
7864           tree arg00 = TREE_OPERAND (arg0, 0);
7865           tree arg01 = TREE_OPERAND (arg0, 1);
7866
7867           return fold_build2_loc (loc,
7868                               TREE_CODE (arg0), type,
7869                               fold_convert_loc (loc, type, arg00),
7870                               fold_convert_loc (loc, sizetype, arg01));
7871         }
7872
7873       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7874          of the same precision, and X is an integer type not narrower than
7875          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
7876       if (INTEGRAL_TYPE_P (type)
7877           && TREE_CODE (op0) == BIT_NOT_EXPR
7878           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7879           && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7880           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7881         {
7882           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7883           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7884               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7885             return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7886                                 fold_convert_loc (loc, type, tem));
7887         }
7888
7889       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7890          type of X and Y (integer types only).  */
7891       if (INTEGRAL_TYPE_P (type)
7892           && TREE_CODE (op0) == MULT_EXPR
7893           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7894           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7895         {
7896           /* Be careful not to introduce new overflows.  */
7897           tree mult_type;
7898           if (TYPE_OVERFLOW_WRAPS (type))
7899             mult_type = type;
7900           else
7901             mult_type = unsigned_type_for (type);
7902
7903           if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7904             {
7905               tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7906                                  fold_convert_loc (loc, mult_type,
7907                                                    TREE_OPERAND (op0, 0)),
7908                                  fold_convert_loc (loc, mult_type,
7909                                                    TREE_OPERAND (op0, 1)));
7910               return fold_convert_loc (loc, type, tem);
7911             }
7912         }
7913
7914       tem = fold_convert_const (code, type, op0);
7915       return tem ? tem : NULL_TREE;
7916
7917     case ADDR_SPACE_CONVERT_EXPR:
7918       if (integer_zerop (arg0))
7919         return fold_convert_const (code, type, arg0);
7920       return NULL_TREE;
7921
7922     case FIXED_CONVERT_EXPR:
7923       tem = fold_convert_const (code, type, arg0);
7924       return tem ? tem : NULL_TREE;
7925
7926     case VIEW_CONVERT_EXPR:
7927       if (TREE_TYPE (op0) == type)
7928         return op0;
7929       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
7930         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
7931                             type, TREE_OPERAND (op0, 0));
7932       if (TREE_CODE (op0) == MEM_REF)
7933         return fold_build2_loc (loc, MEM_REF, type,
7934                                 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7935
7936       /* For integral conversions with the same precision or pointer
7937          conversions use a NOP_EXPR instead.  */
7938       if ((INTEGRAL_TYPE_P (type)
7939            || POINTER_TYPE_P (type))
7940           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7941               || POINTER_TYPE_P (TREE_TYPE (op0)))
7942           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7943         return fold_convert_loc (loc, type, op0);
7944
7945       /* Strip inner integral conversions that do not change the precision.  */
7946       if (CONVERT_EXPR_P (op0)
7947           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7948               || POINTER_TYPE_P (TREE_TYPE (op0)))
7949           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
7950               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
7951           && (TYPE_PRECISION (TREE_TYPE (op0))
7952               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
7953         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
7954                             type, TREE_OPERAND (op0, 0));
7955
7956       return fold_view_convert_expr (type, op0);
7957
7958     case NEGATE_EXPR:
7959       tem = fold_negate_expr (loc, arg0);
7960       if (tem)
7961         return fold_convert_loc (loc, type, tem);
7962       return NULL_TREE;
7963
7964     case ABS_EXPR:
7965       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
7966         return fold_abs_const (arg0, type);
7967       else if (TREE_CODE (arg0) == NEGATE_EXPR)
7968         return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
7969       /* Convert fabs((double)float) into (double)fabsf(float).  */
7970       else if (TREE_CODE (arg0) == NOP_EXPR
7971                && TREE_CODE (type) == REAL_TYPE)
7972         {
7973           tree targ0 = strip_float_extensions (arg0);
7974           if (targ0 != arg0)
7975             return fold_convert_loc (loc, type,
7976                                      fold_build1_loc (loc, ABS_EXPR,
7977                                                   TREE_TYPE (targ0),
7978                                                   targ0));
7979         }
7980       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
7981       else if (TREE_CODE (arg0) == ABS_EXPR)
7982         return arg0;
7983       else if (tree_expr_nonnegative_p (arg0))
7984         return arg0;
7985
7986       /* Strip sign ops from argument.  */
7987       if (TREE_CODE (type) == REAL_TYPE)
7988         {
7989           tem = fold_strip_sign_ops (arg0);
7990           if (tem)
7991             return fold_build1_loc (loc, ABS_EXPR, type,
7992                                 fold_convert_loc (loc, type, tem));
7993         }
7994       return NULL_TREE;
7995
7996     case CONJ_EXPR:
7997       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7998         return fold_convert_loc (loc, type, arg0);
7999       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8000         {
8001           tree itype = TREE_TYPE (type);
8002           tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8003           tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8004           return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8005                               negate_expr (ipart));
8006         }
8007       if (TREE_CODE (arg0) == COMPLEX_CST)
8008         {
8009           tree itype = TREE_TYPE (type);
8010           tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8011           tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8012           return build_complex (type, rpart, negate_expr (ipart));
8013         }
8014       if (TREE_CODE (arg0) == CONJ_EXPR)
8015         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8016       return NULL_TREE;
8017
8018     case BIT_NOT_EXPR:
8019       if (TREE_CODE (arg0) == INTEGER_CST)
8020         return fold_not_const (arg0, type);
8021       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8022         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8023       /* Convert ~ (-A) to A - 1.  */
8024       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8025         return fold_build2_loc (loc, MINUS_EXPR, type,
8026                             fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8027                             build_int_cst (type, 1));
8028       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8029       else if (INTEGRAL_TYPE_P (type)
8030                && ((TREE_CODE (arg0) == MINUS_EXPR
8031                     && integer_onep (TREE_OPERAND (arg0, 1)))
8032                    || (TREE_CODE (arg0) == PLUS_EXPR
8033                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8034         return fold_build1_loc (loc, NEGATE_EXPR, type,
8035                             fold_convert_loc (loc, type,
8036                                               TREE_OPERAND (arg0, 0)));
8037       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8038       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8039                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8040                                      fold_convert_loc (loc, type,
8041                                                        TREE_OPERAND (arg0, 0)))))
8042         return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8043                             fold_convert_loc (loc, type,
8044                                               TREE_OPERAND (arg0, 1)));
8045       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8046                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8047                                      fold_convert_loc (loc, type,
8048                                                        TREE_OPERAND (arg0, 1)))))
8049         return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8050                             fold_convert_loc (loc, type,
8051                                               TREE_OPERAND (arg0, 0)), tem);
8052       /* Perform BIT_NOT_EXPR on each element individually.  */
8053       else if (TREE_CODE (arg0) == VECTOR_CST)
8054         {
8055           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8056           int count = TYPE_VECTOR_SUBPARTS (type), i;
8057
8058           for (i = 0; i < count; i++)
8059             {
8060               if (elements)
8061                 {
8062                   elem = TREE_VALUE (elements);
8063                   elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8064                   if (elem == NULL_TREE)
8065                     break;
8066                   elements = TREE_CHAIN (elements);
8067                 }
8068               else
8069                 elem = build_int_cst (TREE_TYPE (type), -1);
8070               list = tree_cons (NULL_TREE, elem, list);
8071             }
8072           if (i == count)
8073             return build_vector (type, nreverse (list));
8074         }
8075
8076       return NULL_TREE;
8077
8078     case TRUTH_NOT_EXPR:
8079       /* The argument to invert_truthvalue must have Boolean type.  */
8080       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8081           arg0 = fold_convert_loc (loc, boolean_type_node, arg0);
8082
8083       /* Note that the operand of this must be an int
8084          and its values must be 0 or 1.
8085          ("true" is a fixed value perhaps depending on the language,
8086          but we don't handle values other than 1 correctly yet.)  */
8087       tem = fold_truth_not_expr (loc, arg0);
8088       if (!tem)
8089         return NULL_TREE;
8090       return fold_convert_loc (loc, type, tem);
8091
8092     case REALPART_EXPR:
8093       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8094         return fold_convert_loc (loc, type, arg0);
8095       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8096         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8097                                  TREE_OPERAND (arg0, 1));
8098       if (TREE_CODE (arg0) == COMPLEX_CST)
8099         return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8100       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8101         {
8102           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8103           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8104                              fold_build1_loc (loc, REALPART_EXPR, itype,
8105                                           TREE_OPERAND (arg0, 0)),
8106                              fold_build1_loc (loc, REALPART_EXPR, itype,
8107                                           TREE_OPERAND (arg0, 1)));
8108           return fold_convert_loc (loc, type, tem);
8109         }
8110       if (TREE_CODE (arg0) == CONJ_EXPR)
8111         {
8112           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8113           tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8114                              TREE_OPERAND (arg0, 0));
8115           return fold_convert_loc (loc, type, tem);
8116         }
8117       if (TREE_CODE (arg0) == CALL_EXPR)
8118         {
8119           tree fn = get_callee_fndecl (arg0);
8120           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8121             switch (DECL_FUNCTION_CODE (fn))
8122               {
8123               CASE_FLT_FN (BUILT_IN_CEXPI):
8124                 fn = mathfn_built_in (type, BUILT_IN_COS);
8125                 if (fn)
8126                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8127                 break;
8128
8129               default:
8130                 break;
8131               }
8132         }
8133       return NULL_TREE;
8134
8135     case IMAGPART_EXPR:
8136       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8137         return build_zero_cst (type);
8138       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8139         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8140                                  TREE_OPERAND (arg0, 0));
8141       if (TREE_CODE (arg0) == COMPLEX_CST)
8142         return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8143       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8144         {
8145           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8146           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8147                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8148                                           TREE_OPERAND (arg0, 0)),
8149                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8150                                           TREE_OPERAND (arg0, 1)));
8151           return fold_convert_loc (loc, type, tem);
8152         }
8153       if (TREE_CODE (arg0) == CONJ_EXPR)
8154         {
8155           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8156           tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8157           return fold_convert_loc (loc, type, negate_expr (tem));
8158         }
8159       if (TREE_CODE (arg0) == CALL_EXPR)
8160         {
8161           tree fn = get_callee_fndecl (arg0);
8162           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8163             switch (DECL_FUNCTION_CODE (fn))
8164               {
8165               CASE_FLT_FN (BUILT_IN_CEXPI):
8166                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8167                 if (fn)
8168                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8169                 break;
8170
8171               default:
8172                 break;
8173               }
8174         }
8175       return NULL_TREE;
8176
8177     case INDIRECT_REF:
8178       /* Fold *&X to X if X is an lvalue.  */
8179       if (TREE_CODE (op0) == ADDR_EXPR)
8180         {
8181           tree op00 = TREE_OPERAND (op0, 0);
8182           if ((TREE_CODE (op00) == VAR_DECL
8183                || TREE_CODE (op00) == PARM_DECL
8184                || TREE_CODE (op00) == RESULT_DECL)
8185               && !TREE_READONLY (op00))
8186             return op00;
8187         }
8188       return NULL_TREE;
8189
8190     default:
8191       return NULL_TREE;
8192     } /* switch (code) */
8193 }
8194
8195
8196 /* If the operation was a conversion do _not_ mark a resulting constant
8197    with TREE_OVERFLOW if the original constant was not.  These conversions
8198    have implementation defined behavior and retaining the TREE_OVERFLOW
8199    flag here would confuse later passes such as VRP.  */
8200 tree
8201 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8202                                 tree type, tree op0)
8203 {
8204   tree res = fold_unary_loc (loc, code, type, op0);
8205   if (res
8206       && TREE_CODE (res) == INTEGER_CST
8207       && TREE_CODE (op0) == INTEGER_CST
8208       && CONVERT_EXPR_CODE_P (code))
8209     TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8210
8211   return res;
8212 }
8213
8214 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8215    operands OP0 and OP1.  LOC is the location of the resulting expression.
8216    ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8217    Return the folded expression if folding is successful.  Otherwise,
8218    return NULL_TREE.  */
8219 static tree
8220 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8221                   tree arg0, tree arg1, tree op0, tree op1)
8222 {
8223   tree tem;
8224
8225   /* We only do these simplifications if we are optimizing.  */
8226   if (!optimize)
8227     return NULL_TREE;
8228
8229   /* Check for things like (A || B) && (A || C).  We can convert this
8230      to A || (B && C).  Note that either operator can be any of the four
8231      truth and/or operations and the transformation will still be
8232      valid.   Also note that we only care about order for the
8233      ANDIF and ORIF operators.  If B contains side effects, this
8234      might change the truth-value of A.  */
8235   if (TREE_CODE (arg0) == TREE_CODE (arg1)
8236       && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8237           || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8238           || TREE_CODE (arg0) == TRUTH_AND_EXPR
8239           || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8240       && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8241     {
8242       tree a00 = TREE_OPERAND (arg0, 0);
8243       tree a01 = TREE_OPERAND (arg0, 1);
8244       tree a10 = TREE_OPERAND (arg1, 0);
8245       tree a11 = TREE_OPERAND (arg1, 1);
8246       int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8247                           || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8248                          && (code == TRUTH_AND_EXPR
8249                              || code == TRUTH_OR_EXPR));
8250
8251       if (operand_equal_p (a00, a10, 0))
8252         return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8253                             fold_build2_loc (loc, code, type, a01, a11));
8254       else if (commutative && operand_equal_p (a00, a11, 0))
8255         return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8256                             fold_build2_loc (loc, code, type, a01, a10));
8257       else if (commutative && operand_equal_p (a01, a10, 0))
8258         return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8259                             fold_build2_loc (loc, code, type, a00, a11));
8260
8261       /* This case if tricky because we must either have commutative
8262          operators or else A10 must not have side-effects.  */
8263
8264       else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8265                && operand_equal_p (a01, a11, 0))
8266         return fold_build2_loc (loc, TREE_CODE (arg0), type,
8267                             fold_build2_loc (loc, code, type, a00, a10),
8268                             a01);
8269     }
8270
8271   /* See if we can build a range comparison.  */
8272   if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8273     return tem;
8274
8275   if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8276       || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8277     {
8278       tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8279       if (tem)
8280         return fold_build2_loc (loc, code, type, tem, arg1);
8281     }
8282
8283   if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8284       || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8285     {
8286       tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8287       if (tem)
8288         return fold_build2_loc (loc, code, type, arg0, tem);
8289     }
8290
8291   /* Check for the possibility of merging component references.  If our
8292      lhs is another similar operation, try to merge its rhs with our
8293      rhs.  Then try to merge our lhs and rhs.  */
8294   if (TREE_CODE (arg0) == code
8295       && 0 != (tem = fold_truthop (loc, code, type,
8296                                    TREE_OPERAND (arg0, 1), arg1)))
8297     return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8298
8299   if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0)
8300     return tem;
8301
8302   return NULL_TREE;
8303 }
8304
8305 /* Fold a binary expression of code CODE and type TYPE with operands
8306    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8307    Return the folded expression if folding is successful.  Otherwise,
8308    return NULL_TREE.  */
8309
8310 static tree
8311 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8312 {
8313   enum tree_code compl_code;
8314
8315   if (code == MIN_EXPR)
8316     compl_code = MAX_EXPR;
8317   else if (code == MAX_EXPR)
8318     compl_code = MIN_EXPR;
8319   else
8320     gcc_unreachable ();
8321
8322   /* MIN (MAX (a, b), b) == b.  */
8323   if (TREE_CODE (op0) == compl_code
8324       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8325     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8326
8327   /* MIN (MAX (b, a), b) == b.  */
8328   if (TREE_CODE (op0) == compl_code
8329       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8330       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8331     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8332
8333   /* MIN (a, MAX (a, b)) == a.  */
8334   if (TREE_CODE (op1) == compl_code
8335       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8336       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8337     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8338
8339   /* MIN (a, MAX (b, a)) == a.  */
8340   if (TREE_CODE (op1) == compl_code
8341       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8342       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8343     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8344
8345   return NULL_TREE;
8346 }
8347
8348 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8349    by changing CODE to reduce the magnitude of constants involved in
8350    ARG0 of the comparison.
8351    Returns a canonicalized comparison tree if a simplification was
8352    possible, otherwise returns NULL_TREE.
8353    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8354    valid if signed overflow is undefined.  */
8355
8356 static tree
8357 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8358                                  tree arg0, tree arg1,
8359                                  bool *strict_overflow_p)
8360 {
8361   enum tree_code code0 = TREE_CODE (arg0);
8362   tree t, cst0 = NULL_TREE;
8363   int sgn0;
8364   bool swap = false;
8365
8366   /* Match A +- CST code arg1 and CST code arg1.  We can change the
8367      first form only if overflow is undefined.  */
8368   if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8369          /* In principle pointers also have undefined overflow behavior,
8370             but that causes problems elsewhere.  */
8371          && !POINTER_TYPE_P (TREE_TYPE (arg0))
8372          && (code0 == MINUS_EXPR
8373              || code0 == PLUS_EXPR)
8374          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8375         || code0 == INTEGER_CST))
8376     return NULL_TREE;
8377
8378   /* Identify the constant in arg0 and its sign.  */
8379   if (code0 == INTEGER_CST)
8380     cst0 = arg0;
8381   else
8382     cst0 = TREE_OPERAND (arg0, 1);
8383   sgn0 = tree_int_cst_sgn (cst0);
8384
8385   /* Overflowed constants and zero will cause problems.  */
8386   if (integer_zerop (cst0)
8387       || TREE_OVERFLOW (cst0))
8388     return NULL_TREE;
8389
8390   /* See if we can reduce the magnitude of the constant in
8391      arg0 by changing the comparison code.  */
8392   if (code0 == INTEGER_CST)
8393     {
8394       /* CST <= arg1  ->  CST-1 < arg1.  */
8395       if (code == LE_EXPR && sgn0 == 1)
8396         code = LT_EXPR;
8397       /* -CST < arg1  ->  -CST-1 <= arg1.  */
8398       else if (code == LT_EXPR && sgn0 == -1)
8399         code = LE_EXPR;
8400       /* CST > arg1  ->  CST-1 >= arg1.  */
8401       else if (code == GT_EXPR && sgn0 == 1)
8402         code = GE_EXPR;
8403       /* -CST >= arg1  ->  -CST-1 > arg1.  */
8404       else if (code == GE_EXPR && sgn0 == -1)
8405         code = GT_EXPR;
8406       else
8407         return NULL_TREE;
8408       /* arg1 code' CST' might be more canonical.  */
8409       swap = true;
8410     }
8411   else
8412     {
8413       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
8414       if (code == LT_EXPR
8415           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8416         code = LE_EXPR;
8417       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
8418       else if (code == GT_EXPR
8419                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8420         code = GE_EXPR;
8421       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
8422       else if (code == LE_EXPR
8423                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8424         code = LT_EXPR;
8425       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
8426       else if (code == GE_EXPR
8427                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8428         code = GT_EXPR;
8429       else
8430         return NULL_TREE;
8431       *strict_overflow_p = true;
8432     }
8433
8434   /* Now build the constant reduced in magnitude.  But not if that
8435      would produce one outside of its types range.  */
8436   if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8437       && ((sgn0 == 1
8438            && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8439            && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8440           || (sgn0 == -1
8441               && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8442               && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8443     /* We cannot swap the comparison here as that would cause us to
8444        endlessly recurse.  */
8445     return NULL_TREE;
8446
8447   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8448                        cst0, build_int_cst (TREE_TYPE (cst0), 1));
8449   if (code0 != INTEGER_CST)
8450     t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8451
8452   /* If swapping might yield to a more canonical form, do so.  */
8453   if (swap)
8454     return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8455   else
8456     return fold_build2_loc (loc, code, type, t, arg1);
8457 }
8458
8459 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8460    overflow further.  Try to decrease the magnitude of constants involved
8461    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8462    and put sole constants at the second argument position.
8463    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
8464
8465 static tree
8466 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8467                                tree arg0, tree arg1)
8468 {
8469   tree t;
8470   bool strict_overflow_p;
8471   const char * const warnmsg = G_("assuming signed overflow does not occur "
8472                                   "when reducing constant in comparison");
8473
8474   /* Try canonicalization by simplifying arg0.  */
8475   strict_overflow_p = false;
8476   t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8477                                        &strict_overflow_p);
8478   if (t)
8479     {
8480       if (strict_overflow_p)
8481         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8482       return t;
8483     }
8484
8485   /* Try canonicalization by simplifying arg1 using the swapped
8486      comparison.  */
8487   code = swap_tree_comparison (code);
8488   strict_overflow_p = false;
8489   t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8490                                        &strict_overflow_p);
8491   if (t && strict_overflow_p)
8492     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8493   return t;
8494 }
8495
8496 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8497    space.  This is used to avoid issuing overflow warnings for
8498    expressions like &p->x which can not wrap.  */
8499
8500 static bool
8501 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8502 {
8503   unsigned HOST_WIDE_INT offset_low, total_low;
8504   HOST_WIDE_INT size, offset_high, total_high;
8505
8506   if (!POINTER_TYPE_P (TREE_TYPE (base)))
8507     return true;
8508
8509   if (bitpos < 0)
8510     return true;
8511
8512   if (offset == NULL_TREE)
8513     {
8514       offset_low = 0;
8515       offset_high = 0;
8516     }
8517   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8518     return true;
8519   else
8520     {
8521       offset_low = TREE_INT_CST_LOW (offset);
8522       offset_high = TREE_INT_CST_HIGH (offset);
8523     }
8524
8525   if (add_double_with_sign (offset_low, offset_high,
8526                             bitpos / BITS_PER_UNIT, 0,
8527                             &total_low, &total_high,
8528                             true))
8529     return true;
8530
8531   if (total_high != 0)
8532     return true;
8533
8534   size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8535   if (size <= 0)
8536     return true;
8537
8538   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8539      array.  */
8540   if (TREE_CODE (base) == ADDR_EXPR)
8541     {
8542       HOST_WIDE_INT base_size;
8543
8544       base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8545       if (base_size > 0 && size < base_size)
8546         size = base_size;
8547     }
8548
8549   return total_low > (unsigned HOST_WIDE_INT) size;
8550 }
8551
8552 /* Subroutine of fold_binary.  This routine performs all of the
8553    transformations that are common to the equality/inequality
8554    operators (EQ_EXPR and NE_EXPR) and the ordering operators
8555    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
8556    fold_binary should call fold_binary.  Fold a comparison with
8557    tree code CODE and type TYPE with operands OP0 and OP1.  Return
8558    the folded comparison or NULL_TREE.  */
8559
8560 static tree
8561 fold_comparison (location_t loc, enum tree_code code, tree type,
8562                  tree op0, tree op1)
8563 {
8564   tree arg0, arg1, tem;
8565
8566   arg0 = op0;
8567   arg1 = op1;
8568
8569   STRIP_SIGN_NOPS (arg0);
8570   STRIP_SIGN_NOPS (arg1);
8571
8572   tem = fold_relational_const (code, type, arg0, arg1);
8573   if (tem != NULL_TREE)
8574     return tem;
8575
8576   /* If one arg is a real or integer constant, put it last.  */
8577   if (tree_swap_operands_p (arg0, arg1, true))
8578     return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
8579
8580   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
8581   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8582       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8583           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8584           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8585       && (TREE_CODE (arg1) == INTEGER_CST
8586           && !TREE_OVERFLOW (arg1)))
8587     {
8588       tree const1 = TREE_OPERAND (arg0, 1);
8589       tree const2 = arg1;
8590       tree variable = TREE_OPERAND (arg0, 0);
8591       tree lhs;
8592       int lhs_add;
8593       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8594
8595       lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
8596                          TREE_TYPE (arg1), const2, const1);
8597
8598       /* If the constant operation overflowed this can be
8599          simplified as a comparison against INT_MAX/INT_MIN.  */
8600       if (TREE_CODE (lhs) == INTEGER_CST
8601           && TREE_OVERFLOW (lhs))
8602         {
8603           int const1_sgn = tree_int_cst_sgn (const1);
8604           enum tree_code code2 = code;
8605
8606           /* Get the sign of the constant on the lhs if the
8607              operation were VARIABLE + CONST1.  */
8608           if (TREE_CODE (arg0) == MINUS_EXPR)
8609             const1_sgn = -const1_sgn;
8610
8611           /* The sign of the constant determines if we overflowed
8612              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8613              Canonicalize to the INT_MIN overflow by swapping the comparison
8614              if necessary.  */
8615           if (const1_sgn == -1)
8616             code2 = swap_tree_comparison (code);
8617
8618           /* We now can look at the canonicalized case
8619                VARIABLE + 1  CODE2  INT_MIN
8620              and decide on the result.  */
8621           if (code2 == LT_EXPR
8622               || code2 == LE_EXPR
8623               || code2 == EQ_EXPR)
8624             return omit_one_operand_loc (loc, type, boolean_false_node, variable);
8625           else if (code2 == NE_EXPR
8626                    || code2 == GE_EXPR
8627                    || code2 == GT_EXPR)
8628             return omit_one_operand_loc (loc, type, boolean_true_node, variable);
8629         }
8630
8631       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8632           && (TREE_CODE (lhs) != INTEGER_CST
8633               || !TREE_OVERFLOW (lhs)))
8634         {
8635           if (code != EQ_EXPR && code != NE_EXPR)
8636             fold_overflow_warning ("assuming signed overflow does not occur "
8637                                    "when changing X +- C1 cmp C2 to "
8638                                    "X cmp C1 +- C2",
8639                                    WARN_STRICT_OVERFLOW_COMPARISON);
8640           return fold_build2_loc (loc, code, type, variable, lhs);
8641         }
8642     }
8643
8644   /* For comparisons of pointers we can decompose it to a compile time
8645      comparison of the base objects and the offsets into the object.
8646      This requires at least one operand being an ADDR_EXPR or a
8647      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
8648   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8649       && (TREE_CODE (arg0) == ADDR_EXPR
8650           || TREE_CODE (arg1) == ADDR_EXPR
8651           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8652           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8653     {
8654       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8655       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8656       enum machine_mode mode;
8657       int volatilep, unsignedp;
8658       bool indirect_base0 = false, indirect_base1 = false;
8659
8660       /* Get base and offset for the access.  Strip ADDR_EXPR for
8661          get_inner_reference, but put it back by stripping INDIRECT_REF
8662          off the base object if possible.  indirect_baseN will be true
8663          if baseN is not an address but refers to the object itself.  */
8664       base0 = arg0;
8665       if (TREE_CODE (arg0) == ADDR_EXPR)
8666         {
8667           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8668                                        &bitsize, &bitpos0, &offset0, &mode,
8669                                        &unsignedp, &volatilep, false);
8670           if (TREE_CODE (base0) == INDIRECT_REF)
8671             base0 = TREE_OPERAND (base0, 0);
8672           else
8673             indirect_base0 = true;
8674         }
8675       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8676         {
8677           base0 = TREE_OPERAND (arg0, 0);
8678           STRIP_SIGN_NOPS (base0);
8679           if (TREE_CODE (base0) == ADDR_EXPR)
8680             {
8681               base0 = TREE_OPERAND (base0, 0);
8682               indirect_base0 = true;
8683             }
8684           offset0 = TREE_OPERAND (arg0, 1);
8685         }
8686
8687       base1 = arg1;
8688       if (TREE_CODE (arg1) == ADDR_EXPR)
8689         {
8690           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8691                                        &bitsize, &bitpos1, &offset1, &mode,
8692                                        &unsignedp, &volatilep, false);
8693           if (TREE_CODE (base1) == INDIRECT_REF)
8694             base1 = TREE_OPERAND (base1, 0);
8695           else
8696             indirect_base1 = true;
8697         }
8698       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8699         {
8700           base1 = TREE_OPERAND (arg1, 0);
8701           STRIP_SIGN_NOPS (base1);
8702           if (TREE_CODE (base1) == ADDR_EXPR)
8703             {
8704               base1 = TREE_OPERAND (base1, 0);
8705               indirect_base1 = true;
8706             }
8707           offset1 = TREE_OPERAND (arg1, 1);
8708         }
8709
8710       /* A local variable can never be pointed to by
8711          the default SSA name of an incoming parameter.  */
8712       if ((TREE_CODE (arg0) == ADDR_EXPR
8713            && indirect_base0
8714            && TREE_CODE (base0) == VAR_DECL
8715            && auto_var_in_fn_p (base0, current_function_decl)
8716            && !indirect_base1
8717            && TREE_CODE (base1) == SSA_NAME
8718            && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL
8719            && SSA_NAME_IS_DEFAULT_DEF (base1))
8720           || (TREE_CODE (arg1) == ADDR_EXPR
8721               && indirect_base1
8722               && TREE_CODE (base1) == VAR_DECL
8723               && auto_var_in_fn_p (base1, current_function_decl)
8724               && !indirect_base0
8725               && TREE_CODE (base0) == SSA_NAME
8726               && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL
8727               && SSA_NAME_IS_DEFAULT_DEF (base0)))
8728         {
8729           if (code == NE_EXPR)
8730             return constant_boolean_node (1, type);
8731           else if (code == EQ_EXPR)
8732             return constant_boolean_node (0, type);
8733         }
8734       /* If we have equivalent bases we might be able to simplify.  */
8735       else if (indirect_base0 == indirect_base1
8736                && operand_equal_p (base0, base1, 0))
8737         {
8738           /* We can fold this expression to a constant if the non-constant
8739              offset parts are equal.  */
8740           if ((offset0 == offset1
8741                || (offset0 && offset1
8742                    && operand_equal_p (offset0, offset1, 0)))
8743               && (code == EQ_EXPR
8744                   || code == NE_EXPR
8745                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
8746
8747             {
8748               if (code != EQ_EXPR
8749                   && code != NE_EXPR
8750                   && bitpos0 != bitpos1
8751                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
8752                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
8753                 fold_overflow_warning (("assuming pointer wraparound does not "
8754                                         "occur when comparing P +- C1 with "
8755                                         "P +- C2"),
8756                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
8757
8758               switch (code)
8759                 {
8760                 case EQ_EXPR:
8761                   return constant_boolean_node (bitpos0 == bitpos1, type);
8762                 case NE_EXPR:
8763                   return constant_boolean_node (bitpos0 != bitpos1, type);
8764                 case LT_EXPR:
8765                   return constant_boolean_node (bitpos0 < bitpos1, type);
8766                 case LE_EXPR:
8767                   return constant_boolean_node (bitpos0 <= bitpos1, type);
8768                 case GE_EXPR:
8769                   return constant_boolean_node (bitpos0 >= bitpos1, type);
8770                 case GT_EXPR:
8771                   return constant_boolean_node (bitpos0 > bitpos1, type);
8772                 default:;
8773                 }
8774             }
8775           /* We can simplify the comparison to a comparison of the variable
8776              offset parts if the constant offset parts are equal.
8777              Be careful to use signed size type here because otherwise we
8778              mess with array offsets in the wrong way.  This is possible
8779              because pointer arithmetic is restricted to retain within an
8780              object and overflow on pointer differences is undefined as of
8781              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
8782           else if (bitpos0 == bitpos1
8783                    && ((code == EQ_EXPR || code == NE_EXPR)
8784                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
8785             {
8786               /* By converting to signed size type we cover middle-end pointer
8787                  arithmetic which operates on unsigned pointer types of size
8788                  type size and ARRAY_REF offsets which are properly sign or
8789                  zero extended from their type in case it is narrower than
8790                  size type.  */
8791               if (offset0 == NULL_TREE)
8792                 offset0 = build_int_cst (ssizetype, 0);
8793               else
8794                 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8795               if (offset1 == NULL_TREE)
8796                 offset1 = build_int_cst (ssizetype, 0);
8797               else
8798                 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8799
8800               if (code != EQ_EXPR
8801                   && code != NE_EXPR
8802                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
8803                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
8804                 fold_overflow_warning (("assuming pointer wraparound does not "
8805                                         "occur when comparing P +- C1 with "
8806                                         "P +- C2"),
8807                                        WARN_STRICT_OVERFLOW_COMPARISON);
8808
8809               return fold_build2_loc (loc, code, type, offset0, offset1);
8810             }
8811         }
8812       /* For non-equal bases we can simplify if they are addresses
8813          of local binding decls or constants.  */
8814       else if (indirect_base0 && indirect_base1
8815                /* We know that !operand_equal_p (base0, base1, 0)
8816                   because the if condition was false.  But make
8817                   sure two decls are not the same.  */
8818                && base0 != base1
8819                && TREE_CODE (arg0) == ADDR_EXPR
8820                && TREE_CODE (arg1) == ADDR_EXPR
8821                && (((TREE_CODE (base0) == VAR_DECL
8822                      || TREE_CODE (base0) == PARM_DECL)
8823                     && (targetm.binds_local_p (base0)
8824                         || CONSTANT_CLASS_P (base1)))
8825                    || CONSTANT_CLASS_P (base0))
8826                && (((TREE_CODE (base1) == VAR_DECL
8827                      || TREE_CODE (base1) == PARM_DECL)
8828                     && (targetm.binds_local_p (base1)
8829                         || CONSTANT_CLASS_P (base0)))
8830                    || CONSTANT_CLASS_P (base1)))
8831         {
8832           if (code == EQ_EXPR)
8833             return omit_two_operands_loc (loc, type, boolean_false_node,
8834                                       arg0, arg1);
8835           else if (code == NE_EXPR)
8836             return omit_two_operands_loc (loc, type, boolean_true_node,
8837                                       arg0, arg1);
8838         }
8839       /* For equal offsets we can simplify to a comparison of the
8840          base addresses.  */
8841       else if (bitpos0 == bitpos1
8842                && (indirect_base0
8843                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8844                && (indirect_base1
8845                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8846                && ((offset0 == offset1)
8847                    || (offset0 && offset1
8848                        && operand_equal_p (offset0, offset1, 0))))
8849         {
8850           if (indirect_base0)
8851             base0 = build_fold_addr_expr_loc (loc, base0);
8852           if (indirect_base1)
8853             base1 = build_fold_addr_expr_loc (loc, base1);
8854           return fold_build2_loc (loc, code, type, base0, base1);
8855         }
8856     }
8857
8858   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8859      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
8860      the resulting offset is smaller in absolute value than the
8861      original one.  */
8862   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8863       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8864       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8865           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8866       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8867       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8868           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8869     {
8870       tree const1 = TREE_OPERAND (arg0, 1);
8871       tree const2 = TREE_OPERAND (arg1, 1);
8872       tree variable1 = TREE_OPERAND (arg0, 0);
8873       tree variable2 = TREE_OPERAND (arg1, 0);
8874       tree cst;
8875       const char * const warnmsg = G_("assuming signed overflow does not "
8876                                       "occur when combining constants around "
8877                                       "a comparison");
8878
8879       /* Put the constant on the side where it doesn't overflow and is
8880          of lower absolute value than before.  */
8881       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8882                              ? MINUS_EXPR : PLUS_EXPR,
8883                              const2, const1);
8884       if (!TREE_OVERFLOW (cst)
8885           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8886         {
8887           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8888           return fold_build2_loc (loc, code, type,
8889                               variable1,
8890                               fold_build2_loc (loc,
8891                                            TREE_CODE (arg1), TREE_TYPE (arg1),
8892                                            variable2, cst));
8893         }
8894
8895       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8896                              ? MINUS_EXPR : PLUS_EXPR,
8897                              const1, const2);
8898       if (!TREE_OVERFLOW (cst)
8899           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8900         {
8901           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8902           return fold_build2_loc (loc, code, type,
8903                               fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
8904                                            variable1, cst),
8905                               variable2);
8906         }
8907     }
8908
8909   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8910      signed arithmetic case.  That form is created by the compiler
8911      often enough for folding it to be of value.  One example is in
8912      computing loop trip counts after Operator Strength Reduction.  */
8913   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8914       && TREE_CODE (arg0) == MULT_EXPR
8915       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8916           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8917       && integer_zerop (arg1))
8918     {
8919       tree const1 = TREE_OPERAND (arg0, 1);
8920       tree const2 = arg1;                       /* zero */
8921       tree variable1 = TREE_OPERAND (arg0, 0);
8922       enum tree_code cmp_code = code;
8923
8924       /* Handle unfolded multiplication by zero.  */
8925       if (integer_zerop (const1))
8926         return fold_build2_loc (loc, cmp_code, type, const1, const2);
8927
8928       fold_overflow_warning (("assuming signed overflow does not occur when "
8929                               "eliminating multiplication in comparison "
8930                               "with zero"),
8931                              WARN_STRICT_OVERFLOW_COMPARISON);
8932
8933       /* If const1 is negative we swap the sense of the comparison.  */
8934       if (tree_int_cst_sgn (const1) < 0)
8935         cmp_code = swap_tree_comparison (cmp_code);
8936
8937       return fold_build2_loc (loc, cmp_code, type, variable1, const2);
8938     }
8939
8940   tem = maybe_canonicalize_comparison (loc, code, type, op0, op1);
8941   if (tem)
8942     return tem;
8943
8944   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8945     {
8946       tree targ0 = strip_float_extensions (arg0);
8947       tree targ1 = strip_float_extensions (arg1);
8948       tree newtype = TREE_TYPE (targ0);
8949
8950       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8951         newtype = TREE_TYPE (targ1);
8952
8953       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
8954       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8955         return fold_build2_loc (loc, code, type,
8956                             fold_convert_loc (loc, newtype, targ0),
8957                             fold_convert_loc (loc, newtype, targ1));
8958
8959       /* (-a) CMP (-b) -> b CMP a  */
8960       if (TREE_CODE (arg0) == NEGATE_EXPR
8961           && TREE_CODE (arg1) == NEGATE_EXPR)
8962         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
8963                             TREE_OPERAND (arg0, 0));
8964
8965       if (TREE_CODE (arg1) == REAL_CST)
8966         {
8967           REAL_VALUE_TYPE cst;
8968           cst = TREE_REAL_CST (arg1);
8969
8970           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
8971           if (TREE_CODE (arg0) == NEGATE_EXPR)
8972             return fold_build2_loc (loc, swap_tree_comparison (code), type,
8973                                 TREE_OPERAND (arg0, 0),
8974                                 build_real (TREE_TYPE (arg1),
8975                                             real_value_negate (&cst)));
8976
8977           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
8978           /* a CMP (-0) -> a CMP 0  */
8979           if (REAL_VALUE_MINUS_ZERO (cst))
8980             return fold_build2_loc (loc, code, type, arg0,
8981                                 build_real (TREE_TYPE (arg1), dconst0));
8982
8983           /* x != NaN is always true, other ops are always false.  */
8984           if (REAL_VALUE_ISNAN (cst)
8985               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8986             {
8987               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8988               return omit_one_operand_loc (loc, type, tem, arg0);
8989             }
8990
8991           /* Fold comparisons against infinity.  */
8992           if (REAL_VALUE_ISINF (cst)
8993               && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
8994             {
8995               tem = fold_inf_compare (loc, code, type, arg0, arg1);
8996               if (tem != NULL_TREE)
8997                 return tem;
8998             }
8999         }
9000
9001       /* If this is a comparison of a real constant with a PLUS_EXPR
9002          or a MINUS_EXPR of a real constant, we can convert it into a
9003          comparison with a revised real constant as long as no overflow
9004          occurs when unsafe_math_optimizations are enabled.  */
9005       if (flag_unsafe_math_optimizations
9006           && TREE_CODE (arg1) == REAL_CST
9007           && (TREE_CODE (arg0) == PLUS_EXPR
9008               || TREE_CODE (arg0) == MINUS_EXPR)
9009           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9010           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9011                                       ? MINUS_EXPR : PLUS_EXPR,
9012                                       arg1, TREE_OPERAND (arg0, 1)))
9013           && !TREE_OVERFLOW (tem))
9014         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9015
9016       /* Likewise, we can simplify a comparison of a real constant with
9017          a MINUS_EXPR whose first operand is also a real constant, i.e.
9018          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
9019          floating-point types only if -fassociative-math is set.  */
9020       if (flag_associative_math
9021           && TREE_CODE (arg1) == REAL_CST
9022           && TREE_CODE (arg0) == MINUS_EXPR
9023           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9024           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9025                                       arg1))
9026           && !TREE_OVERFLOW (tem))
9027         return fold_build2_loc (loc, swap_tree_comparison (code), type,
9028                             TREE_OPERAND (arg0, 1), tem);
9029
9030       /* Fold comparisons against built-in math functions.  */
9031       if (TREE_CODE (arg1) == REAL_CST
9032           && flag_unsafe_math_optimizations
9033           && ! flag_errno_math)
9034         {
9035           enum built_in_function fcode = builtin_mathfn_code (arg0);
9036
9037           if (fcode != END_BUILTINS)
9038             {
9039               tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
9040               if (tem != NULL_TREE)
9041                 return tem;
9042             }
9043         }
9044     }
9045
9046   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9047       && CONVERT_EXPR_P (arg0))
9048     {
9049       /* If we are widening one operand of an integer comparison,
9050          see if the other operand is similarly being widened.  Perhaps we
9051          can do the comparison in the narrower type.  */
9052       tem = fold_widened_comparison (loc, code, type, arg0, arg1);
9053       if (tem)
9054         return tem;
9055
9056       /* Or if we are changing signedness.  */
9057       tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
9058       if (tem)
9059         return tem;
9060     }
9061
9062   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9063      constant, we can simplify it.  */
9064   if (TREE_CODE (arg1) == INTEGER_CST
9065       && (TREE_CODE (arg0) == MIN_EXPR
9066           || TREE_CODE (arg0) == MAX_EXPR)
9067       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9068     {
9069       tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9070       if (tem)
9071         return tem;
9072     }
9073
9074   /* Simplify comparison of something with itself.  (For IEEE
9075      floating-point, we can only do some of these simplifications.)  */
9076   if (operand_equal_p (arg0, arg1, 0))
9077     {
9078       switch (code)
9079         {
9080         case EQ_EXPR:
9081           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9082               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9083             return constant_boolean_node (1, type);
9084           break;
9085
9086         case GE_EXPR:
9087         case LE_EXPR:
9088           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9089               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9090             return constant_boolean_node (1, type);
9091           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9092
9093         case NE_EXPR:
9094           /* For NE, we can only do this simplification if integer
9095              or we don't honor IEEE floating point NaNs.  */
9096           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9097               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9098             break;
9099           /* ... fall through ...  */
9100         case GT_EXPR:
9101         case LT_EXPR:
9102           return constant_boolean_node (0, type);
9103         default:
9104           gcc_unreachable ();
9105         }
9106     }
9107
9108   /* If we are comparing an expression that just has comparisons
9109      of two integer values, arithmetic expressions of those comparisons,
9110      and constants, we can simplify it.  There are only three cases
9111      to check: the two values can either be equal, the first can be
9112      greater, or the second can be greater.  Fold the expression for
9113      those three values.  Since each value must be 0 or 1, we have
9114      eight possibilities, each of which corresponds to the constant 0
9115      or 1 or one of the six possible comparisons.
9116
9117      This handles common cases like (a > b) == 0 but also handles
9118      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9119      occur in macroized code.  */
9120
9121   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9122     {
9123       tree cval1 = 0, cval2 = 0;
9124       int save_p = 0;
9125
9126       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9127           /* Don't handle degenerate cases here; they should already
9128              have been handled anyway.  */
9129           && cval1 != 0 && cval2 != 0
9130           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9131           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9132           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9133           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9134           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9135           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9136                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9137         {
9138           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9139           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9140
9141           /* We can't just pass T to eval_subst in case cval1 or cval2
9142              was the same as ARG1.  */
9143
9144           tree high_result
9145                 = fold_build2_loc (loc, code, type,
9146                                eval_subst (loc, arg0, cval1, maxval,
9147                                            cval2, minval),
9148                                arg1);
9149           tree equal_result
9150                 = fold_build2_loc (loc, code, type,
9151                                eval_subst (loc, arg0, cval1, maxval,
9152                                            cval2, maxval),
9153                                arg1);
9154           tree low_result
9155                 = fold_build2_loc (loc, code, type,
9156                                eval_subst (loc, arg0, cval1, minval,
9157                                            cval2, maxval),
9158                                arg1);
9159
9160           /* All three of these results should be 0 or 1.  Confirm they are.
9161              Then use those values to select the proper code to use.  */
9162
9163           if (TREE_CODE (high_result) == INTEGER_CST
9164               && TREE_CODE (equal_result) == INTEGER_CST
9165               && TREE_CODE (low_result) == INTEGER_CST)
9166             {
9167               /* Make a 3-bit mask with the high-order bit being the
9168                  value for `>', the next for '=', and the low for '<'.  */
9169               switch ((integer_onep (high_result) * 4)
9170                       + (integer_onep (equal_result) * 2)
9171                       + integer_onep (low_result))
9172                 {
9173                 case 0:
9174                   /* Always false.  */
9175                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9176                 case 1:
9177                   code = LT_EXPR;
9178                   break;
9179                 case 2:
9180                   code = EQ_EXPR;
9181                   break;
9182                 case 3:
9183                   code = LE_EXPR;
9184                   break;
9185                 case 4:
9186                   code = GT_EXPR;
9187                   break;
9188                 case 5:
9189                   code = NE_EXPR;
9190                   break;
9191                 case 6:
9192                   code = GE_EXPR;
9193                   break;
9194                 case 7:
9195                   /* Always true.  */
9196                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9197                 }
9198
9199               if (save_p)
9200                 {
9201                   tem = save_expr (build2 (code, type, cval1, cval2));
9202                   SET_EXPR_LOCATION (tem, loc);
9203                   return tem;
9204                 }
9205               return fold_build2_loc (loc, code, type, cval1, cval2);
9206             }
9207         }
9208     }
9209
9210   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9211      into a single range test.  */
9212   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9213        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9214       && TREE_CODE (arg1) == INTEGER_CST
9215       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9216       && !integer_zerop (TREE_OPERAND (arg0, 1))
9217       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9218       && !TREE_OVERFLOW (arg1))
9219     {
9220       tem = fold_div_compare (loc, code, type, arg0, arg1);
9221       if (tem != NULL_TREE)
9222         return tem;
9223     }
9224
9225   /* Fold ~X op ~Y as Y op X.  */
9226   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9227       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9228     {
9229       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9230       return fold_build2_loc (loc, code, type,
9231                           fold_convert_loc (loc, cmp_type,
9232                                             TREE_OPERAND (arg1, 0)),
9233                           TREE_OPERAND (arg0, 0));
9234     }
9235
9236   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9237   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9238       && TREE_CODE (arg1) == INTEGER_CST)
9239     {
9240       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9241       return fold_build2_loc (loc, swap_tree_comparison (code), type,
9242                           TREE_OPERAND (arg0, 0),
9243                           fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9244                                        fold_convert_loc (loc, cmp_type, arg1)));
9245     }
9246
9247   return NULL_TREE;
9248 }
9249
9250
9251 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9252    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9253    argument EXPR represents the expression "z" of type TYPE.  */
9254
9255 static tree
9256 fold_mult_zconjz (location_t loc, tree type, tree expr)
9257 {
9258   tree itype = TREE_TYPE (type);
9259   tree rpart, ipart, tem;
9260
9261   if (TREE_CODE (expr) == COMPLEX_EXPR)
9262     {
9263       rpart = TREE_OPERAND (expr, 0);
9264       ipart = TREE_OPERAND (expr, 1);
9265     }
9266   else if (TREE_CODE (expr) == COMPLEX_CST)
9267     {
9268       rpart = TREE_REALPART (expr);
9269       ipart = TREE_IMAGPART (expr);
9270     }
9271   else
9272     {
9273       expr = save_expr (expr);
9274       rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9275       ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9276     }
9277
9278   rpart = save_expr (rpart);
9279   ipart = save_expr (ipart);
9280   tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9281                      fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9282                      fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9283   return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9284                           build_zero_cst (itype));
9285 }
9286
9287
9288 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9289    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9290    guarantees that P and N have the same least significant log2(M) bits.
9291    N is not otherwise constrained.  In particular, N is not normalized to
9292    0 <= N < M as is common.  In general, the precise value of P is unknown.
9293    M is chosen as large as possible such that constant N can be determined.
9294
9295    Returns M and sets *RESIDUE to N.
9296
9297    If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9298    account.  This is not always possible due to PR 35705.
9299  */
9300
9301 static unsigned HOST_WIDE_INT
9302 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9303                                  bool allow_func_align)
9304 {
9305   enum tree_code code;
9306
9307   *residue = 0;
9308
9309   code = TREE_CODE (expr);
9310   if (code == ADDR_EXPR)
9311     {
9312       unsigned int bitalign;
9313       bitalign = get_object_alignment_1 (TREE_OPERAND (expr, 0), residue);
9314       *residue /= BITS_PER_UNIT;
9315       return bitalign / BITS_PER_UNIT;
9316     }
9317   else if (code == POINTER_PLUS_EXPR)
9318     {
9319       tree op0, op1;
9320       unsigned HOST_WIDE_INT modulus;
9321       enum tree_code inner_code;
9322
9323       op0 = TREE_OPERAND (expr, 0);
9324       STRIP_NOPS (op0);
9325       modulus = get_pointer_modulus_and_residue (op0, residue,
9326                                                  allow_func_align);
9327
9328       op1 = TREE_OPERAND (expr, 1);
9329       STRIP_NOPS (op1);
9330       inner_code = TREE_CODE (op1);
9331       if (inner_code == INTEGER_CST)
9332         {
9333           *residue += TREE_INT_CST_LOW (op1);
9334           return modulus;
9335         }
9336       else if (inner_code == MULT_EXPR)
9337         {
9338           op1 = TREE_OPERAND (op1, 1);
9339           if (TREE_CODE (op1) == INTEGER_CST)
9340             {
9341               unsigned HOST_WIDE_INT align;
9342
9343               /* Compute the greatest power-of-2 divisor of op1.  */
9344               align = TREE_INT_CST_LOW (op1);
9345               align &= -align;
9346
9347               /* If align is non-zero and less than *modulus, replace
9348                  *modulus with align., If align is 0, then either op1 is 0
9349                  or the greatest power-of-2 divisor of op1 doesn't fit in an
9350                  unsigned HOST_WIDE_INT.  In either case, no additional
9351                  constraint is imposed.  */
9352               if (align)
9353                 modulus = MIN (modulus, align);
9354
9355               return modulus;
9356             }
9357         }
9358     }
9359
9360   /* If we get here, we were unable to determine anything useful about the
9361      expression.  */
9362   return 1;
9363 }
9364
9365
9366 /* Fold a binary expression of code CODE and type TYPE with operands
9367    OP0 and OP1.  LOC is the location of the resulting expression.
9368    Return the folded expression if folding is successful.  Otherwise,
9369    return NULL_TREE.  */
9370
9371 tree
9372 fold_binary_loc (location_t loc,
9373              enum tree_code code, tree type, tree op0, tree op1)
9374 {
9375   enum tree_code_class kind = TREE_CODE_CLASS (code);
9376   tree arg0, arg1, tem;
9377   tree t1 = NULL_TREE;
9378   bool strict_overflow_p;
9379
9380   gcc_assert (IS_EXPR_CODE_CLASS (kind)
9381               && TREE_CODE_LENGTH (code) == 2
9382               && op0 != NULL_TREE
9383               && op1 != NULL_TREE);
9384
9385   arg0 = op0;
9386   arg1 = op1;
9387
9388   /* Strip any conversions that don't change the mode.  This is
9389      safe for every expression, except for a comparison expression
9390      because its signedness is derived from its operands.  So, in
9391      the latter case, only strip conversions that don't change the
9392      signedness.  MIN_EXPR/MAX_EXPR also need signedness of arguments
9393      preserved.
9394
9395      Note that this is done as an internal manipulation within the
9396      constant folder, in order to find the simplest representation
9397      of the arguments so that their form can be studied.  In any
9398      cases, the appropriate type conversions should be put back in
9399      the tree that will get out of the constant folder.  */
9400
9401   if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9402     {
9403       STRIP_SIGN_NOPS (arg0);
9404       STRIP_SIGN_NOPS (arg1);
9405     }
9406   else
9407     {
9408       STRIP_NOPS (arg0);
9409       STRIP_NOPS (arg1);
9410     }
9411
9412   /* Note that TREE_CONSTANT isn't enough: static var addresses are
9413      constant but we can't do arithmetic on them.  */
9414   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9415       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9416       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9417       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9418       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9419       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9420     {
9421       if (kind == tcc_binary)
9422         {
9423           /* Make sure type and arg0 have the same saturating flag.  */
9424           gcc_assert (TYPE_SATURATING (type)
9425                       == TYPE_SATURATING (TREE_TYPE (arg0)));
9426           tem = const_binop (code, arg0, arg1);
9427         }
9428       else if (kind == tcc_comparison)
9429         tem = fold_relational_const (code, type, arg0, arg1);
9430       else
9431         tem = NULL_TREE;
9432
9433       if (tem != NULL_TREE)
9434         {
9435           if (TREE_TYPE (tem) != type)
9436             tem = fold_convert_loc (loc, type, tem);
9437           return tem;
9438         }
9439     }
9440
9441   /* If this is a commutative operation, and ARG0 is a constant, move it
9442      to ARG1 to reduce the number of tests below.  */
9443   if (commutative_tree_code (code)
9444       && tree_swap_operands_p (arg0, arg1, true))
9445     return fold_build2_loc (loc, code, type, op1, op0);
9446
9447   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9448
9449      First check for cases where an arithmetic operation is applied to a
9450      compound, conditional, or comparison operation.  Push the arithmetic
9451      operation inside the compound or conditional to see if any folding
9452      can then be done.  Convert comparison to conditional for this purpose.
9453      The also optimizes non-constant cases that used to be done in
9454      expand_expr.
9455
9456      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9457      one of the operands is a comparison and the other is a comparison, a
9458      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
9459      code below would make the expression more complex.  Change it to a
9460      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
9461      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
9462
9463   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9464        || code == EQ_EXPR || code == NE_EXPR)
9465       && ((truth_value_p (TREE_CODE (arg0))
9466            && (truth_value_p (TREE_CODE (arg1))
9467                || (TREE_CODE (arg1) == BIT_AND_EXPR
9468                    && integer_onep (TREE_OPERAND (arg1, 1)))))
9469           || (truth_value_p (TREE_CODE (arg1))
9470               && (truth_value_p (TREE_CODE (arg0))
9471                   || (TREE_CODE (arg0) == BIT_AND_EXPR
9472                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
9473     {
9474       tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9475                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9476                          : TRUTH_XOR_EXPR,
9477                          boolean_type_node,
9478                          fold_convert_loc (loc, boolean_type_node, arg0),
9479                          fold_convert_loc (loc, boolean_type_node, arg1));
9480
9481       if (code == EQ_EXPR)
9482         tem = invert_truthvalue_loc (loc, tem);
9483
9484       return fold_convert_loc (loc, type, tem);
9485     }
9486
9487   if (TREE_CODE_CLASS (code) == tcc_binary
9488       || TREE_CODE_CLASS (code) == tcc_comparison)
9489     {
9490       if (TREE_CODE (arg0) == COMPOUND_EXPR)
9491         {
9492           tem = fold_build2_loc (loc, code, type,
9493                              fold_convert_loc (loc, TREE_TYPE (op0),
9494                                                TREE_OPERAND (arg0, 1)), op1);
9495           return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9496                              tem);
9497         }
9498       if (TREE_CODE (arg1) == COMPOUND_EXPR
9499           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9500         {
9501           tem = fold_build2_loc (loc, code, type, op0,
9502                              fold_convert_loc (loc, TREE_TYPE (op1),
9503                                                TREE_OPERAND (arg1, 1)));
9504           return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9505                              tem);
9506         }
9507
9508       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9509         {
9510           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9511                                                      arg0, arg1,
9512                                                      /*cond_first_p=*/1);
9513           if (tem != NULL_TREE)
9514             return tem;
9515         }
9516
9517       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9518         {
9519           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9520                                                      arg1, arg0,
9521                                                      /*cond_first_p=*/0);
9522           if (tem != NULL_TREE)
9523             return tem;
9524         }
9525     }
9526
9527   switch (code)
9528     {
9529     case MEM_REF:
9530       /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2].  */
9531       if (TREE_CODE (arg0) == ADDR_EXPR
9532           && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9533         {
9534           tree iref = TREE_OPERAND (arg0, 0);
9535           return fold_build2 (MEM_REF, type,
9536                               TREE_OPERAND (iref, 0),
9537                               int_const_binop (PLUS_EXPR, arg1,
9538                                                TREE_OPERAND (iref, 1)));
9539         }
9540
9541       /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2].  */
9542       if (TREE_CODE (arg0) == ADDR_EXPR
9543           && handled_component_p (TREE_OPERAND (arg0, 0)))
9544         {
9545           tree base;
9546           HOST_WIDE_INT coffset;
9547           base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9548                                                 &coffset);
9549           if (!base)
9550             return NULL_TREE;
9551           return fold_build2 (MEM_REF, type,
9552                               build_fold_addr_expr (base),
9553                               int_const_binop (PLUS_EXPR, arg1,
9554                                                size_int (coffset)));
9555         }
9556
9557       return NULL_TREE;
9558
9559     case POINTER_PLUS_EXPR:
9560       /* 0 +p index -> (type)index */
9561       if (integer_zerop (arg0))
9562         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9563
9564       /* PTR +p 0 -> PTR */
9565       if (integer_zerop (arg1))
9566         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9567
9568       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
9569       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9570            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9571         return fold_convert_loc (loc, type,
9572                                  fold_build2_loc (loc, PLUS_EXPR, sizetype,
9573                                               fold_convert_loc (loc, sizetype,
9574                                                                 arg1),
9575                                               fold_convert_loc (loc, sizetype,
9576                                                                 arg0)));
9577
9578       /* index +p PTR -> PTR +p index */
9579       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9580           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9581         return fold_build2_loc (loc, POINTER_PLUS_EXPR, type,
9582                             fold_convert_loc (loc, type, arg1),
9583                             fold_convert_loc (loc, sizetype, arg0));
9584
9585       /* (PTR +p B) +p A -> PTR +p (B + A) */
9586       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9587         {
9588           tree inner;
9589           tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
9590           tree arg00 = TREE_OPERAND (arg0, 0);
9591           inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
9592                                arg01, fold_convert_loc (loc, sizetype, arg1));
9593           return fold_convert_loc (loc, type,
9594                                    fold_build2_loc (loc, POINTER_PLUS_EXPR,
9595                                                 TREE_TYPE (arg00),
9596                                                 arg00, inner));
9597         }
9598
9599       /* PTR_CST +p CST -> CST1 */
9600       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9601         return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
9602                             fold_convert_loc (loc, type, arg1));
9603
9604      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9605         of the array.  Loop optimizer sometimes produce this type of
9606         expressions.  */
9607       if (TREE_CODE (arg0) == ADDR_EXPR)
9608         {
9609           tem = try_move_mult_to_index (loc, arg0,
9610                                         fold_convert_loc (loc, sizetype, arg1));
9611           if (tem)
9612             return fold_convert_loc (loc, type, tem);
9613         }
9614
9615       return NULL_TREE;
9616
9617     case PLUS_EXPR:
9618       /* A + (-B) -> A - B */
9619       if (TREE_CODE (arg1) == NEGATE_EXPR)
9620         return fold_build2_loc (loc, MINUS_EXPR, type,
9621                             fold_convert_loc (loc, type, arg0),
9622                             fold_convert_loc (loc, type,
9623                                               TREE_OPERAND (arg1, 0)));
9624       /* (-A) + B -> B - A */
9625       if (TREE_CODE (arg0) == NEGATE_EXPR
9626           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9627         return fold_build2_loc (loc, MINUS_EXPR, type,
9628                             fold_convert_loc (loc, type, arg1),
9629                             fold_convert_loc (loc, type,
9630                                               TREE_OPERAND (arg0, 0)));
9631
9632       if (INTEGRAL_TYPE_P (type))
9633         {
9634           /* Convert ~A + 1 to -A.  */
9635           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9636               && integer_onep (arg1))
9637             return fold_build1_loc (loc, NEGATE_EXPR, type,
9638                                 fold_convert_loc (loc, type,
9639                                                   TREE_OPERAND (arg0, 0)));
9640
9641           /* ~X + X is -1.  */
9642           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9643               && !TYPE_OVERFLOW_TRAPS (type))
9644             {
9645               tree tem = TREE_OPERAND (arg0, 0);
9646
9647               STRIP_NOPS (tem);
9648               if (operand_equal_p (tem, arg1, 0))
9649                 {
9650                   t1 = build_int_cst_type (type, -1);
9651                   return omit_one_operand_loc (loc, type, t1, arg1);
9652                 }
9653             }
9654
9655           /* X + ~X is -1.  */
9656           if (TREE_CODE (arg1) == BIT_NOT_EXPR
9657               && !TYPE_OVERFLOW_TRAPS (type))
9658             {
9659               tree tem = TREE_OPERAND (arg1, 0);
9660
9661               STRIP_NOPS (tem);
9662               if (operand_equal_p (arg0, tem, 0))
9663                 {
9664                   t1 = build_int_cst_type (type, -1);
9665                   return omit_one_operand_loc (loc, type, t1, arg0);
9666                 }
9667             }
9668
9669           /* X + (X / CST) * -CST is X % CST.  */
9670           if (TREE_CODE (arg1) == MULT_EXPR
9671               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9672               && operand_equal_p (arg0,
9673                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9674             {
9675               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9676               tree cst1 = TREE_OPERAND (arg1, 1);
9677               tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9678                                       cst1, cst0);
9679               if (sum && integer_zerop (sum))
9680                 return fold_convert_loc (loc, type,
9681                                          fold_build2_loc (loc, TRUNC_MOD_EXPR,
9682                                                       TREE_TYPE (arg0), arg0,
9683                                                       cst0));
9684             }
9685         }
9686
9687       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9688          same or one.  Make sure type is not saturating.
9689          fold_plusminus_mult_expr will re-associate.  */
9690       if ((TREE_CODE (arg0) == MULT_EXPR
9691            || TREE_CODE (arg1) == MULT_EXPR)
9692           && !TYPE_SATURATING (type)
9693           && (!FLOAT_TYPE_P (type) || flag_associative_math))
9694         {
9695           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9696           if (tem)
9697             return tem;
9698         }
9699
9700       if (! FLOAT_TYPE_P (type))
9701         {
9702           if (integer_zerop (arg1))
9703             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9704
9705           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9706              with a constant, and the two constants have no bits in common,
9707              we should treat this as a BIT_IOR_EXPR since this may produce more
9708              simplifications.  */
9709           if (TREE_CODE (arg0) == BIT_AND_EXPR
9710               && TREE_CODE (arg1) == BIT_AND_EXPR
9711               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9712               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9713               && integer_zerop (const_binop (BIT_AND_EXPR,
9714                                              TREE_OPERAND (arg0, 1),
9715                                              TREE_OPERAND (arg1, 1))))
9716             {
9717               code = BIT_IOR_EXPR;
9718               goto bit_ior;
9719             }
9720
9721           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9722              (plus (plus (mult) (mult)) (foo)) so that we can
9723              take advantage of the factoring cases below.  */
9724           if (((TREE_CODE (arg0) == PLUS_EXPR
9725                 || TREE_CODE (arg0) == MINUS_EXPR)
9726                && TREE_CODE (arg1) == MULT_EXPR)
9727               || ((TREE_CODE (arg1) == PLUS_EXPR
9728                    || TREE_CODE (arg1) == MINUS_EXPR)
9729                   && TREE_CODE (arg0) == MULT_EXPR))
9730             {
9731               tree parg0, parg1, parg, marg;
9732               enum tree_code pcode;
9733
9734               if (TREE_CODE (arg1) == MULT_EXPR)
9735                 parg = arg0, marg = arg1;
9736               else
9737                 parg = arg1, marg = arg0;
9738               pcode = TREE_CODE (parg);
9739               parg0 = TREE_OPERAND (parg, 0);
9740               parg1 = TREE_OPERAND (parg, 1);
9741               STRIP_NOPS (parg0);
9742               STRIP_NOPS (parg1);
9743
9744               if (TREE_CODE (parg0) == MULT_EXPR
9745                   && TREE_CODE (parg1) != MULT_EXPR)
9746                 return fold_build2_loc (loc, pcode, type,
9747                                     fold_build2_loc (loc, PLUS_EXPR, type,
9748                                                  fold_convert_loc (loc, type,
9749                                                                    parg0),
9750                                                  fold_convert_loc (loc, type,
9751                                                                    marg)),
9752                                     fold_convert_loc (loc, type, parg1));
9753               if (TREE_CODE (parg0) != MULT_EXPR
9754                   && TREE_CODE (parg1) == MULT_EXPR)
9755                 return
9756                   fold_build2_loc (loc, PLUS_EXPR, type,
9757                                fold_convert_loc (loc, type, parg0),
9758                                fold_build2_loc (loc, pcode, type,
9759                                             fold_convert_loc (loc, type, marg),
9760                                             fold_convert_loc (loc, type,
9761                                                               parg1)));
9762             }
9763         }
9764       else
9765         {
9766           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
9767           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9768             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9769
9770           /* Likewise if the operands are reversed.  */
9771           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9772             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9773
9774           /* Convert X + -C into X - C.  */
9775           if (TREE_CODE (arg1) == REAL_CST
9776               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9777             {
9778               tem = fold_negate_const (arg1, type);
9779               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9780                 return fold_build2_loc (loc, MINUS_EXPR, type,
9781                                     fold_convert_loc (loc, type, arg0),
9782                                     fold_convert_loc (loc, type, tem));
9783             }
9784
9785           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9786              to __complex__ ( x, y ).  This is not the same for SNaNs or
9787              if signed zeros are involved.  */
9788           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9789               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9790               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9791             {
9792               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9793               tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9794               tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9795               bool arg0rz = false, arg0iz = false;
9796               if ((arg0r && (arg0rz = real_zerop (arg0r)))
9797                   || (arg0i && (arg0iz = real_zerop (arg0i))))
9798                 {
9799                   tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9800                   tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9801                   if (arg0rz && arg1i && real_zerop (arg1i))
9802                     {
9803                       tree rp = arg1r ? arg1r
9804                                   : build1 (REALPART_EXPR, rtype, arg1);
9805                       tree ip = arg0i ? arg0i
9806                                   : build1 (IMAGPART_EXPR, rtype, arg0);
9807                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9808                     }
9809                   else if (arg0iz && arg1r && real_zerop (arg1r))
9810                     {
9811                       tree rp = arg0r ? arg0r
9812                                   : build1 (REALPART_EXPR, rtype, arg0);
9813                       tree ip = arg1i ? arg1i
9814                                   : build1 (IMAGPART_EXPR, rtype, arg1);
9815                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9816                     }
9817                 }
9818             }
9819
9820           if (flag_unsafe_math_optimizations
9821               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9822               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9823               && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9824             return tem;
9825
9826           /* Convert x+x into x*2.0.  */
9827           if (operand_equal_p (arg0, arg1, 0)
9828               && SCALAR_FLOAT_TYPE_P (type))
9829             return fold_build2_loc (loc, MULT_EXPR, type, arg0,
9830                                 build_real (type, dconst2));
9831
9832           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9833              We associate floats only if the user has specified
9834              -fassociative-math.  */
9835           if (flag_associative_math
9836               && TREE_CODE (arg1) == PLUS_EXPR
9837               && TREE_CODE (arg0) != MULT_EXPR)
9838             {
9839               tree tree10 = TREE_OPERAND (arg1, 0);
9840               tree tree11 = TREE_OPERAND (arg1, 1);
9841               if (TREE_CODE (tree11) == MULT_EXPR
9842                   && TREE_CODE (tree10) == MULT_EXPR)
9843                 {
9844                   tree tree0;
9845                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9846                   return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9847                 }
9848             }
9849           /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9850              We associate floats only if the user has specified
9851              -fassociative-math.  */
9852           if (flag_associative_math
9853               && TREE_CODE (arg0) == PLUS_EXPR
9854               && TREE_CODE (arg1) != MULT_EXPR)
9855             {
9856               tree tree00 = TREE_OPERAND (arg0, 0);
9857               tree tree01 = TREE_OPERAND (arg0, 1);
9858               if (TREE_CODE (tree01) == MULT_EXPR
9859                   && TREE_CODE (tree00) == MULT_EXPR)
9860                 {
9861                   tree tree0;
9862                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9863                   return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9864                 }
9865             }
9866         }
9867
9868      bit_rotate:
9869       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9870          is a rotate of A by C1 bits.  */
9871       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9872          is a rotate of A by B bits.  */
9873       {
9874         enum tree_code code0, code1;
9875         tree rtype;
9876         code0 = TREE_CODE (arg0);
9877         code1 = TREE_CODE (arg1);
9878         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9879              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9880             && operand_equal_p (TREE_OPERAND (arg0, 0),
9881                                 TREE_OPERAND (arg1, 0), 0)
9882             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9883                 TYPE_UNSIGNED (rtype))
9884             /* Only create rotates in complete modes.  Other cases are not
9885                expanded properly.  */
9886             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
9887           {
9888             tree tree01, tree11;
9889             enum tree_code code01, code11;
9890
9891             tree01 = TREE_OPERAND (arg0, 1);
9892             tree11 = TREE_OPERAND (arg1, 1);
9893             STRIP_NOPS (tree01);
9894             STRIP_NOPS (tree11);
9895             code01 = TREE_CODE (tree01);
9896             code11 = TREE_CODE (tree11);
9897             if (code01 == INTEGER_CST
9898                 && code11 == INTEGER_CST
9899                 && TREE_INT_CST_HIGH (tree01) == 0
9900                 && TREE_INT_CST_HIGH (tree11) == 0
9901                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9902                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9903               {
9904                 tem = build2_loc (loc, LROTATE_EXPR,
9905                                   TREE_TYPE (TREE_OPERAND (arg0, 0)),
9906                                   TREE_OPERAND (arg0, 0),
9907                                   code0 == LSHIFT_EXPR ? tree01 : tree11);
9908                 return fold_convert_loc (loc, type, tem);
9909               }
9910             else if (code11 == MINUS_EXPR)
9911               {
9912                 tree tree110, tree111;
9913                 tree110 = TREE_OPERAND (tree11, 0);
9914                 tree111 = TREE_OPERAND (tree11, 1);
9915                 STRIP_NOPS (tree110);
9916                 STRIP_NOPS (tree111);
9917                 if (TREE_CODE (tree110) == INTEGER_CST
9918                     && 0 == compare_tree_int (tree110,
9919                                               TYPE_PRECISION
9920                                               (TREE_TYPE (TREE_OPERAND
9921                                                           (arg0, 0))))
9922                     && operand_equal_p (tree01, tree111, 0))
9923                   return
9924                     fold_convert_loc (loc, type,
9925                                       build2 ((code0 == LSHIFT_EXPR
9926                                                ? LROTATE_EXPR
9927                                                : RROTATE_EXPR),
9928                                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
9929                                               TREE_OPERAND (arg0, 0), tree01));
9930               }
9931             else if (code01 == MINUS_EXPR)
9932               {
9933                 tree tree010, tree011;
9934                 tree010 = TREE_OPERAND (tree01, 0);
9935                 tree011 = TREE_OPERAND (tree01, 1);
9936                 STRIP_NOPS (tree010);
9937                 STRIP_NOPS (tree011);
9938                 if (TREE_CODE (tree010) == INTEGER_CST
9939                     && 0 == compare_tree_int (tree010,
9940                                               TYPE_PRECISION
9941                                               (TREE_TYPE (TREE_OPERAND
9942                                                           (arg0, 0))))
9943                     && operand_equal_p (tree11, tree011, 0))
9944                     return fold_convert_loc
9945                       (loc, type,
9946                        build2 ((code0 != LSHIFT_EXPR
9947                                 ? LROTATE_EXPR
9948                                 : RROTATE_EXPR),
9949                                TREE_TYPE (TREE_OPERAND (arg0, 0)),
9950                                TREE_OPERAND (arg0, 0), tree11));
9951               }
9952           }
9953       }
9954
9955     associate:
9956       /* In most languages, can't associate operations on floats through
9957          parentheses.  Rather than remember where the parentheses were, we
9958          don't associate floats at all, unless the user has specified
9959          -fassociative-math.
9960          And, we need to make sure type is not saturating.  */
9961
9962       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9963           && !TYPE_SATURATING (type))
9964         {
9965           tree var0, con0, lit0, minus_lit0;
9966           tree var1, con1, lit1, minus_lit1;
9967           bool ok = true;
9968
9969           /* Split both trees into variables, constants, and literals.  Then
9970              associate each group together, the constants with literals,
9971              then the result with variables.  This increases the chances of
9972              literals being recombined later and of generating relocatable
9973              expressions for the sum of a constant and literal.  */
9974           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9975           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9976                              code == MINUS_EXPR);
9977
9978           /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
9979           if (code == MINUS_EXPR)
9980             code = PLUS_EXPR;
9981
9982           /* With undefined overflow we can only associate constants with one
9983              variable, and constants whose association doesn't overflow.  */
9984           if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9985               || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9986             {
9987               if (var0 && var1)
9988                 {
9989                   tree tmp0 = var0;
9990                   tree tmp1 = var1;
9991
9992                   if (TREE_CODE (tmp0) == NEGATE_EXPR)
9993                     tmp0 = TREE_OPERAND (tmp0, 0);
9994                   if (TREE_CODE (tmp1) == NEGATE_EXPR)
9995                     tmp1 = TREE_OPERAND (tmp1, 0);
9996                   /* The only case we can still associate with two variables
9997                      is if they are the same, modulo negation.  */
9998                   if (!operand_equal_p (tmp0, tmp1, 0))
9999                     ok = false;
10000                 }
10001
10002               if (ok && lit0 && lit1)
10003                 {
10004                   tree tmp0 = fold_convert (type, lit0);
10005                   tree tmp1 = fold_convert (type, lit1);
10006
10007                   if (!TREE_OVERFLOW (tmp0) && !TREE_OVERFLOW (tmp1)
10008                       && TREE_OVERFLOW (fold_build2 (code, type, tmp0, tmp1)))
10009                     ok = false;
10010                 }
10011             }
10012
10013           /* Only do something if we found more than two objects.  Otherwise,
10014              nothing has changed and we risk infinite recursion.  */
10015           if (ok
10016               && (2 < ((var0 != 0) + (var1 != 0)
10017                        + (con0 != 0) + (con1 != 0)
10018                        + (lit0 != 0) + (lit1 != 0)
10019                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
10020             {
10021               var0 = associate_trees (loc, var0, var1, code, type);
10022               con0 = associate_trees (loc, con0, con1, code, type);
10023               lit0 = associate_trees (loc, lit0, lit1, code, type);
10024               minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type);
10025
10026               /* Preserve the MINUS_EXPR if the negative part of the literal is
10027                  greater than the positive part.  Otherwise, the multiplicative
10028                  folding code (i.e extract_muldiv) may be fooled in case
10029                  unsigned constants are subtracted, like in the following
10030                  example: ((X*2 + 4) - 8U)/2.  */
10031               if (minus_lit0 && lit0)
10032                 {
10033                   if (TREE_CODE (lit0) == INTEGER_CST
10034                       && TREE_CODE (minus_lit0) == INTEGER_CST
10035                       && tree_int_cst_lt (lit0, minus_lit0))
10036                     {
10037                       minus_lit0 = associate_trees (loc, minus_lit0, lit0,
10038                                                     MINUS_EXPR, type);
10039                       lit0 = 0;
10040                     }
10041                   else
10042                     {
10043                       lit0 = associate_trees (loc, lit0, minus_lit0,
10044                                               MINUS_EXPR, type);
10045                       minus_lit0 = 0;
10046                     }
10047                 }
10048               if (minus_lit0)
10049                 {
10050                   if (con0 == 0)
10051                     return
10052                       fold_convert_loc (loc, type,
10053                                         associate_trees (loc, var0, minus_lit0,
10054                                                          MINUS_EXPR, type));
10055                   else
10056                     {
10057                       con0 = associate_trees (loc, con0, minus_lit0,
10058                                               MINUS_EXPR, type);
10059                       return
10060                         fold_convert_loc (loc, type,
10061                                           associate_trees (loc, var0, con0,
10062                                                            PLUS_EXPR, type));
10063                     }
10064                 }
10065
10066               con0 = associate_trees (loc, con0, lit0, code, type);
10067               return
10068                 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10069                                                               code, type));
10070             }
10071         }
10072
10073       return NULL_TREE;
10074
10075     case MINUS_EXPR:
10076       /* Pointer simplifications for subtraction, simple reassociations. */
10077       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10078         {
10079           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10080           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10081               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10082             {
10083               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10084               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10085               tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10086               tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10087               return fold_build2_loc (loc, PLUS_EXPR, type,
10088                                   fold_build2_loc (loc, MINUS_EXPR, type,
10089                                                arg00, arg10),
10090                                   fold_build2_loc (loc, MINUS_EXPR, type,
10091                                                arg01, arg11));
10092             }
10093           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10094           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10095             {
10096               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10097               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10098               tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10099                                       fold_convert_loc (loc, type, arg1));
10100               if (tmp)
10101                 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10102             }
10103         }
10104       /* A - (-B) -> A + B */
10105       if (TREE_CODE (arg1) == NEGATE_EXPR)
10106         return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10107                             fold_convert_loc (loc, type,
10108                                               TREE_OPERAND (arg1, 0)));
10109       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10110       if (TREE_CODE (arg0) == NEGATE_EXPR
10111           && (FLOAT_TYPE_P (type)
10112               || INTEGRAL_TYPE_P (type))
10113           && negate_expr_p (arg1)
10114           && reorder_operands_p (arg0, arg1))
10115         return fold_build2_loc (loc, MINUS_EXPR, type,
10116                             fold_convert_loc (loc, type,
10117                                               negate_expr (arg1)),
10118                             fold_convert_loc (loc, type,
10119                                               TREE_OPERAND (arg0, 0)));
10120       /* Convert -A - 1 to ~A.  */
10121       if (INTEGRAL_TYPE_P (type)
10122           && TREE_CODE (arg0) == NEGATE_EXPR
10123           && integer_onep (arg1)
10124           && !TYPE_OVERFLOW_TRAPS (type))
10125         return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10126                             fold_convert_loc (loc, type,
10127                                               TREE_OPERAND (arg0, 0)));
10128
10129       /* Convert -1 - A to ~A.  */
10130       if (INTEGRAL_TYPE_P (type)
10131           && integer_all_onesp (arg0))
10132         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10133
10134
10135       /* X - (X / CST) * CST is X % CST.  */
10136       if (INTEGRAL_TYPE_P (type)
10137           && TREE_CODE (arg1) == MULT_EXPR
10138           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10139           && operand_equal_p (arg0,
10140                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10141           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10142                               TREE_OPERAND (arg1, 1), 0))
10143         return
10144           fold_convert_loc (loc, type,
10145                             fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10146                                          arg0, TREE_OPERAND (arg1, 1)));
10147
10148       if (! FLOAT_TYPE_P (type))
10149         {
10150           if (integer_zerop (arg0))
10151             return negate_expr (fold_convert_loc (loc, type, arg1));
10152           if (integer_zerop (arg1))
10153             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10154
10155           /* Fold A - (A & B) into ~B & A.  */
10156           if (!TREE_SIDE_EFFECTS (arg0)
10157               && TREE_CODE (arg1) == BIT_AND_EXPR)
10158             {
10159               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10160                 {
10161                   tree arg10 = fold_convert_loc (loc, type,
10162                                                  TREE_OPERAND (arg1, 0));
10163                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10164                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10165                                                    type, arg10),
10166                                       fold_convert_loc (loc, type, arg0));
10167                 }
10168               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10169                 {
10170                   tree arg11 = fold_convert_loc (loc,
10171                                                  type, TREE_OPERAND (arg1, 1));
10172                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10173                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10174                                                    type, arg11),
10175                                       fold_convert_loc (loc, type, arg0));
10176                 }
10177             }
10178
10179           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10180              any power of 2 minus 1.  */
10181           if (TREE_CODE (arg0) == BIT_AND_EXPR
10182               && TREE_CODE (arg1) == BIT_AND_EXPR
10183               && operand_equal_p (TREE_OPERAND (arg0, 0),
10184                                   TREE_OPERAND (arg1, 0), 0))
10185             {
10186               tree mask0 = TREE_OPERAND (arg0, 1);
10187               tree mask1 = TREE_OPERAND (arg1, 1);
10188               tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10189
10190               if (operand_equal_p (tem, mask1, 0))
10191                 {
10192                   tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10193                                      TREE_OPERAND (arg0, 0), mask1);
10194                   return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10195                 }
10196             }
10197         }
10198
10199       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10200       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10201         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10202
10203       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10204          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10205          (-ARG1 + ARG0) reduces to -ARG1.  */
10206       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10207         return negate_expr (fold_convert_loc (loc, type, arg1));
10208
10209       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10210          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10211          signed zeros are involved.  */
10212       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10213           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10214           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10215         {
10216           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10217           tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10218           tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10219           bool arg0rz = false, arg0iz = false;
10220           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10221               || (arg0i && (arg0iz = real_zerop (arg0i))))
10222             {
10223               tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10224               tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10225               if (arg0rz && arg1i && real_zerop (arg1i))
10226                 {
10227                   tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10228                                          arg1r ? arg1r
10229                                          : build1 (REALPART_EXPR, rtype, arg1));
10230                   tree ip = arg0i ? arg0i
10231                     : build1 (IMAGPART_EXPR, rtype, arg0);
10232                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10233                 }
10234               else if (arg0iz && arg1r && real_zerop (arg1r))
10235                 {
10236                   tree rp = arg0r ? arg0r
10237                     : build1 (REALPART_EXPR, rtype, arg0);
10238                   tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10239                                          arg1i ? arg1i
10240                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10241                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10242                 }
10243             }
10244         }
10245
10246       /* Fold &x - &x.  This can happen from &x.foo - &x.
10247          This is unsafe for certain floats even in non-IEEE formats.
10248          In IEEE, it is unsafe because it does wrong for NaNs.
10249          Also note that operand_equal_p is always false if an operand
10250          is volatile.  */
10251
10252       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10253           && operand_equal_p (arg0, arg1, 0))
10254         return build_zero_cst (type);
10255
10256       /* A - B -> A + (-B) if B is easily negatable.  */
10257       if (negate_expr_p (arg1)
10258           && ((FLOAT_TYPE_P (type)
10259                /* Avoid this transformation if B is a positive REAL_CST.  */
10260                && (TREE_CODE (arg1) != REAL_CST
10261                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10262               || INTEGRAL_TYPE_P (type)))
10263         return fold_build2_loc (loc, PLUS_EXPR, type,
10264                             fold_convert_loc (loc, type, arg0),
10265                             fold_convert_loc (loc, type,
10266                                               negate_expr (arg1)));
10267
10268       /* Try folding difference of addresses.  */
10269       {
10270         HOST_WIDE_INT diff;
10271
10272         if ((TREE_CODE (arg0) == ADDR_EXPR
10273              || TREE_CODE (arg1) == ADDR_EXPR)
10274             && ptr_difference_const (arg0, arg1, &diff))
10275           return build_int_cst_type (type, diff);
10276       }
10277
10278       /* Fold &a[i] - &a[j] to i-j.  */
10279       if (TREE_CODE (arg0) == ADDR_EXPR
10280           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10281           && TREE_CODE (arg1) == ADDR_EXPR
10282           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10283         {
10284           tree aref0 = TREE_OPERAND (arg0, 0);
10285           tree aref1 = TREE_OPERAND (arg1, 0);
10286           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10287                                TREE_OPERAND (aref1, 0), 0))
10288             {
10289               tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10290               tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10291               tree esz = array_ref_element_size (aref0);
10292               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10293               return fold_build2_loc (loc, MULT_EXPR, type, diff,
10294                                   fold_convert_loc (loc, type, esz));
10295
10296             }
10297         }
10298
10299       if (FLOAT_TYPE_P (type)
10300           && flag_unsafe_math_optimizations
10301           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10302           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10303           && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10304         return tem;
10305
10306       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10307          same or one.  Make sure type is not saturating.
10308          fold_plusminus_mult_expr will re-associate.  */
10309       if ((TREE_CODE (arg0) == MULT_EXPR
10310            || TREE_CODE (arg1) == MULT_EXPR)
10311           && !TYPE_SATURATING (type)
10312           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10313         {
10314           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10315           if (tem)
10316             return tem;
10317         }
10318
10319       goto associate;
10320
10321     case MULT_EXPR:
10322       /* (-A) * (-B) -> A * B  */
10323       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10324         return fold_build2_loc (loc, MULT_EXPR, type,
10325                             fold_convert_loc (loc, type,
10326                                               TREE_OPERAND (arg0, 0)),
10327                             fold_convert_loc (loc, type,
10328                                               negate_expr (arg1)));
10329       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10330         return fold_build2_loc (loc, MULT_EXPR, type,
10331                             fold_convert_loc (loc, type,
10332                                               negate_expr (arg0)),
10333                             fold_convert_loc (loc, type,
10334                                               TREE_OPERAND (arg1, 0)));
10335
10336       if (! FLOAT_TYPE_P (type))
10337         {
10338           if (integer_zerop (arg1))
10339             return omit_one_operand_loc (loc, type, arg1, arg0);
10340           if (integer_onep (arg1))
10341             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10342           /* Transform x * -1 into -x.  Make sure to do the negation
10343              on the original operand with conversions not stripped
10344              because we can only strip non-sign-changing conversions.  */
10345           if (integer_all_onesp (arg1))
10346             return fold_convert_loc (loc, type, negate_expr (op0));
10347           /* Transform x * -C into -x * C if x is easily negatable.  */
10348           if (TREE_CODE (arg1) == INTEGER_CST
10349               && tree_int_cst_sgn (arg1) == -1
10350               && negate_expr_p (arg0)
10351               && (tem = negate_expr (arg1)) != arg1
10352               && !TREE_OVERFLOW (tem))
10353             return fold_build2_loc (loc, MULT_EXPR, type,
10354                                 fold_convert_loc (loc, type,
10355                                                   negate_expr (arg0)),
10356                                 tem);
10357
10358           /* (a * (1 << b)) is (a << b)  */
10359           if (TREE_CODE (arg1) == LSHIFT_EXPR
10360               && integer_onep (TREE_OPERAND (arg1, 0)))
10361             return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10362                                 TREE_OPERAND (arg1, 1));
10363           if (TREE_CODE (arg0) == LSHIFT_EXPR
10364               && integer_onep (TREE_OPERAND (arg0, 0)))
10365             return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10366                                 TREE_OPERAND (arg0, 1));
10367
10368           /* (A + A) * C -> A * 2 * C  */
10369           if (TREE_CODE (arg0) == PLUS_EXPR
10370               && TREE_CODE (arg1) == INTEGER_CST
10371               && operand_equal_p (TREE_OPERAND (arg0, 0),
10372                                   TREE_OPERAND (arg0, 1), 0))
10373             return fold_build2_loc (loc, MULT_EXPR, type,
10374                                 omit_one_operand_loc (loc, type,
10375                                                   TREE_OPERAND (arg0, 0),
10376                                                   TREE_OPERAND (arg0, 1)),
10377                                 fold_build2_loc (loc, MULT_EXPR, type,
10378                                              build_int_cst (type, 2) , arg1));
10379
10380           strict_overflow_p = false;
10381           if (TREE_CODE (arg1) == INTEGER_CST
10382               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10383                                              &strict_overflow_p)))
10384             {
10385               if (strict_overflow_p)
10386                 fold_overflow_warning (("assuming signed overflow does not "
10387                                         "occur when simplifying "
10388                                         "multiplication"),
10389                                        WARN_STRICT_OVERFLOW_MISC);
10390               return fold_convert_loc (loc, type, tem);
10391             }
10392
10393           /* Optimize z * conj(z) for integer complex numbers.  */
10394           if (TREE_CODE (arg0) == CONJ_EXPR
10395               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10396             return fold_mult_zconjz (loc, type, arg1);
10397           if (TREE_CODE (arg1) == CONJ_EXPR
10398               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10399             return fold_mult_zconjz (loc, type, arg0);
10400         }
10401       else
10402         {
10403           /* Maybe fold x * 0 to 0.  The expressions aren't the same
10404              when x is NaN, since x * 0 is also NaN.  Nor are they the
10405              same in modes with signed zeros, since multiplying a
10406              negative value by 0 gives -0, not +0.  */
10407           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10408               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10409               && real_zerop (arg1))
10410             return omit_one_operand_loc (loc, type, arg1, arg0);
10411           /* In IEEE floating point, x*1 is not equivalent to x for snans.
10412              Likewise for complex arithmetic with signed zeros.  */
10413           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10414               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10415                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10416               && real_onep (arg1))
10417             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10418
10419           /* Transform x * -1.0 into -x.  */
10420           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10421               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10422                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10423               && real_minus_onep (arg1))
10424             return fold_convert_loc (loc, type, negate_expr (arg0));
10425
10426           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
10427              the result for floating point types due to rounding so it is applied
10428              only if -fassociative-math was specify.  */
10429           if (flag_associative_math
10430               && TREE_CODE (arg0) == RDIV_EXPR
10431               && TREE_CODE (arg1) == REAL_CST
10432               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10433             {
10434               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10435                                       arg1);
10436               if (tem)
10437                 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10438                                     TREE_OPERAND (arg0, 1));
10439             }
10440
10441           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
10442           if (operand_equal_p (arg0, arg1, 0))
10443             {
10444               tree tem = fold_strip_sign_ops (arg0);
10445               if (tem != NULL_TREE)
10446                 {
10447                   tem = fold_convert_loc (loc, type, tem);
10448                   return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
10449                 }
10450             }
10451
10452           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10453              This is not the same for NaNs or if signed zeros are
10454              involved.  */
10455           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10456               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10457               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10458               && TREE_CODE (arg1) == COMPLEX_CST
10459               && real_zerop (TREE_REALPART (arg1)))
10460             {
10461               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10462               if (real_onep (TREE_IMAGPART (arg1)))
10463                 return
10464                   fold_build2_loc (loc, COMPLEX_EXPR, type,
10465                                negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10466                                                              rtype, arg0)),
10467                                fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10468               else if (real_minus_onep (TREE_IMAGPART (arg1)))
10469                 return
10470                   fold_build2_loc (loc, COMPLEX_EXPR, type,
10471                                fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10472                                negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10473                                                              rtype, arg0)));
10474             }
10475
10476           /* Optimize z * conj(z) for floating point complex numbers.
10477              Guarded by flag_unsafe_math_optimizations as non-finite
10478              imaginary components don't produce scalar results.  */
10479           if (flag_unsafe_math_optimizations
10480               && TREE_CODE (arg0) == CONJ_EXPR
10481               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10482             return fold_mult_zconjz (loc, type, arg1);
10483           if (flag_unsafe_math_optimizations
10484               && TREE_CODE (arg1) == CONJ_EXPR
10485               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10486             return fold_mult_zconjz (loc, type, arg0);
10487
10488           if (flag_unsafe_math_optimizations)
10489             {
10490               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10491               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10492
10493               /* Optimizations of root(...)*root(...).  */
10494               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10495                 {
10496                   tree rootfn, arg;
10497                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10498                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10499
10500                   /* Optimize sqrt(x)*sqrt(x) as x.  */
10501                   if (BUILTIN_SQRT_P (fcode0)
10502                       && operand_equal_p (arg00, arg10, 0)
10503                       && ! HONOR_SNANS (TYPE_MODE (type)))
10504                     return arg00;
10505
10506                   /* Optimize root(x)*root(y) as root(x*y).  */
10507                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10508                   arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
10509                   return build_call_expr_loc (loc, rootfn, 1, arg);
10510                 }
10511
10512               /* Optimize expN(x)*expN(y) as expN(x+y).  */
10513               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10514                 {
10515                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10516                   tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10517                                           CALL_EXPR_ARG (arg0, 0),
10518                                           CALL_EXPR_ARG (arg1, 0));
10519                   return build_call_expr_loc (loc, expfn, 1, arg);
10520                 }
10521
10522               /* Optimizations of pow(...)*pow(...).  */
10523               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10524                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10525                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10526                 {
10527                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10528                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10529                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10530                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10531
10532                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
10533                   if (operand_equal_p (arg01, arg11, 0))
10534                     {
10535                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10536                       tree arg = fold_build2_loc (loc, MULT_EXPR, type,
10537                                               arg00, arg10);
10538                       return build_call_expr_loc (loc, powfn, 2, arg, arg01);
10539                     }
10540
10541                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
10542                   if (operand_equal_p (arg00, arg10, 0))
10543                     {
10544                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10545                       tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10546                                               arg01, arg11);
10547                       return build_call_expr_loc (loc, powfn, 2, arg00, arg);
10548                     }
10549                 }
10550
10551               /* Optimize tan(x)*cos(x) as sin(x).  */
10552               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10553                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10554                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10555                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10556                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10557                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10558                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10559                                       CALL_EXPR_ARG (arg1, 0), 0))
10560                 {
10561                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10562
10563                   if (sinfn != NULL_TREE)
10564                     return build_call_expr_loc (loc, sinfn, 1,
10565                                             CALL_EXPR_ARG (arg0, 0));
10566                 }
10567
10568               /* Optimize x*pow(x,c) as pow(x,c+1).  */
10569               if (fcode1 == BUILT_IN_POW
10570                   || fcode1 == BUILT_IN_POWF
10571                   || fcode1 == BUILT_IN_POWL)
10572                 {
10573                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10574                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10575                   if (TREE_CODE (arg11) == REAL_CST
10576                       && !TREE_OVERFLOW (arg11)
10577                       && operand_equal_p (arg0, arg10, 0))
10578                     {
10579                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10580                       REAL_VALUE_TYPE c;
10581                       tree arg;
10582
10583                       c = TREE_REAL_CST (arg11);
10584                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10585                       arg = build_real (type, c);
10586                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10587                     }
10588                 }
10589
10590               /* Optimize pow(x,c)*x as pow(x,c+1).  */
10591               if (fcode0 == BUILT_IN_POW
10592                   || fcode0 == BUILT_IN_POWF
10593                   || fcode0 == BUILT_IN_POWL)
10594                 {
10595                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10596                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10597                   if (TREE_CODE (arg01) == REAL_CST
10598                       && !TREE_OVERFLOW (arg01)
10599                       && operand_equal_p (arg1, arg00, 0))
10600                     {
10601                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10602                       REAL_VALUE_TYPE c;
10603                       tree arg;
10604
10605                       c = TREE_REAL_CST (arg01);
10606                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10607                       arg = build_real (type, c);
10608                       return build_call_expr_loc (loc, powfn, 2, arg1, arg);
10609                     }
10610                 }
10611
10612               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
10613               if (!in_gimple_form
10614                   && optimize_function_for_speed_p (cfun)
10615                   && operand_equal_p (arg0, arg1, 0))
10616                 {
10617                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10618
10619                   if (powfn)
10620                     {
10621                       tree arg = build_real (type, dconst2);
10622                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10623                     }
10624                 }
10625             }
10626         }
10627       goto associate;
10628
10629     case BIT_IOR_EXPR:
10630     bit_ior:
10631       if (integer_all_onesp (arg1))
10632         return omit_one_operand_loc (loc, type, arg1, arg0);
10633       if (integer_zerop (arg1))
10634         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10635       if (operand_equal_p (arg0, arg1, 0))
10636         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10637
10638       /* ~X | X is -1.  */
10639       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10640           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10641         {
10642           t1 = build_zero_cst (type);
10643           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10644           return omit_one_operand_loc (loc, type, t1, arg1);
10645         }
10646
10647       /* X | ~X is -1.  */
10648       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10649           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10650         {
10651           t1 = build_zero_cst (type);
10652           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10653           return omit_one_operand_loc (loc, type, t1, arg0);
10654         }
10655
10656       /* Canonicalize (X & C1) | C2.  */
10657       if (TREE_CODE (arg0) == BIT_AND_EXPR
10658           && TREE_CODE (arg1) == INTEGER_CST
10659           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10660         {
10661           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10662           int width = TYPE_PRECISION (type), w;
10663           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10664           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10665           hi2 = TREE_INT_CST_HIGH (arg1);
10666           lo2 = TREE_INT_CST_LOW (arg1);
10667
10668           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
10669           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10670             return omit_one_operand_loc (loc, type, arg1,
10671                                      TREE_OPERAND (arg0, 0));
10672
10673           if (width > HOST_BITS_PER_WIDE_INT)
10674             {
10675               mhi = (unsigned HOST_WIDE_INT) -1
10676                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
10677               mlo = -1;
10678             }
10679           else
10680             {
10681               mhi = 0;
10682               mlo = (unsigned HOST_WIDE_INT) -1
10683                     >> (HOST_BITS_PER_WIDE_INT - width);
10684             }
10685
10686           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
10687           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10688             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10689                                 TREE_OPERAND (arg0, 0), arg1);
10690
10691           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10692              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10693              mode which allows further optimizations.  */
10694           hi1 &= mhi;
10695           lo1 &= mlo;
10696           hi2 &= mhi;
10697           lo2 &= mlo;
10698           hi3 = hi1 & ~hi2;
10699           lo3 = lo1 & ~lo2;
10700           for (w = BITS_PER_UNIT;
10701                w <= width && w <= HOST_BITS_PER_WIDE_INT;
10702                w <<= 1)
10703             {
10704               unsigned HOST_WIDE_INT mask
10705                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10706               if (((lo1 | lo2) & mask) == mask
10707                   && (lo1 & ~mask) == 0 && hi1 == 0)
10708                 {
10709                   hi3 = 0;
10710                   lo3 = mask;
10711                   break;
10712                 }
10713             }
10714           if (hi3 != hi1 || lo3 != lo1)
10715             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10716                                 fold_build2_loc (loc, BIT_AND_EXPR, type,
10717                                              TREE_OPERAND (arg0, 0),
10718                                              build_int_cst_wide (type,
10719                                                                  lo3, hi3)),
10720                                 arg1);
10721         }
10722
10723       /* (X & Y) | Y is (X, Y).  */
10724       if (TREE_CODE (arg0) == BIT_AND_EXPR
10725           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10726         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10727       /* (X & Y) | X is (Y, X).  */
10728       if (TREE_CODE (arg0) == BIT_AND_EXPR
10729           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10730           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10731         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
10732       /* X | (X & Y) is (Y, X).  */
10733       if (TREE_CODE (arg1) == BIT_AND_EXPR
10734           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10735           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10736         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
10737       /* X | (Y & X) is (Y, X).  */
10738       if (TREE_CODE (arg1) == BIT_AND_EXPR
10739           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10740           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10741         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
10742
10743       /* (X & ~Y) | (~X & Y) is X ^ Y */
10744       if (TREE_CODE (arg0) == BIT_AND_EXPR
10745           && TREE_CODE (arg1) == BIT_AND_EXPR)
10746         {
10747           tree a0, a1, l0, l1, n0, n1;
10748
10749           a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10750           a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10751
10752           l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10753           l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10754           
10755           n0 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l0);
10756           n1 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l1);
10757           
10758           if ((operand_equal_p (n0, a0, 0)
10759                && operand_equal_p (n1, a1, 0))
10760               || (operand_equal_p (n0, a1, 0)
10761                   && operand_equal_p (n1, a0, 0)))
10762             return fold_build2_loc (loc, BIT_XOR_EXPR, type, l0, n1);
10763         }
10764
10765       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
10766       if (t1 != NULL_TREE)
10767         return t1;
10768
10769       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10770
10771          This results in more efficient code for machines without a NAND
10772          instruction.  Combine will canonicalize to the first form
10773          which will allow use of NAND instructions provided by the
10774          backend if they exist.  */
10775       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10776           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10777         {
10778           return
10779             fold_build1_loc (loc, BIT_NOT_EXPR, type,
10780                          build2 (BIT_AND_EXPR, type,
10781                                  fold_convert_loc (loc, type,
10782                                                    TREE_OPERAND (arg0, 0)),
10783                                  fold_convert_loc (loc, type,
10784                                                    TREE_OPERAND (arg1, 0))));
10785         }
10786
10787       /* See if this can be simplified into a rotate first.  If that
10788          is unsuccessful continue in the association code.  */
10789       goto bit_rotate;
10790
10791     case BIT_XOR_EXPR:
10792       if (integer_zerop (arg1))
10793         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10794       if (integer_all_onesp (arg1))
10795         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
10796       if (operand_equal_p (arg0, arg1, 0))
10797         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10798
10799       /* ~X ^ X is -1.  */
10800       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10801           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10802         {
10803           t1 = build_zero_cst (type);
10804           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10805           return omit_one_operand_loc (loc, type, t1, arg1);
10806         }
10807
10808       /* X ^ ~X is -1.  */
10809       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10810           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10811         {
10812           t1 = build_zero_cst (type);
10813           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10814           return omit_one_operand_loc (loc, type, t1, arg0);
10815         }
10816
10817       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10818          with a constant, and the two constants have no bits in common,
10819          we should treat this as a BIT_IOR_EXPR since this may produce more
10820          simplifications.  */
10821       if (TREE_CODE (arg0) == BIT_AND_EXPR
10822           && TREE_CODE (arg1) == BIT_AND_EXPR
10823           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10824           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10825           && integer_zerop (const_binop (BIT_AND_EXPR,
10826                                          TREE_OPERAND (arg0, 1),
10827                                          TREE_OPERAND (arg1, 1))))
10828         {
10829           code = BIT_IOR_EXPR;
10830           goto bit_ior;
10831         }
10832
10833       /* (X | Y) ^ X -> Y & ~ X*/
10834       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10835           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10836         {
10837           tree t2 = TREE_OPERAND (arg0, 1);
10838           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10839                             arg1);
10840           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10841                             fold_convert_loc (loc, type, t2),
10842                             fold_convert_loc (loc, type, t1));
10843           return t1;
10844         }
10845
10846       /* (Y | X) ^ X -> Y & ~ X*/
10847       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10848           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10849         {
10850           tree t2 = TREE_OPERAND (arg0, 0);
10851           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10852                             arg1);
10853           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10854                             fold_convert_loc (loc, type, t2),
10855                             fold_convert_loc (loc, type, t1));
10856           return t1;
10857         }
10858
10859       /* X ^ (X | Y) -> Y & ~ X*/
10860       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10861           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10862         {
10863           tree t2 = TREE_OPERAND (arg1, 1);
10864           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10865                             arg0);
10866           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10867                             fold_convert_loc (loc, type, t2),
10868                             fold_convert_loc (loc, type, t1));
10869           return t1;
10870         }
10871
10872       /* X ^ (Y | X) -> Y & ~ X*/
10873       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10874           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10875         {
10876           tree t2 = TREE_OPERAND (arg1, 0);
10877           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10878                             arg0);
10879           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10880                             fold_convert_loc (loc, type, t2),
10881                             fold_convert_loc (loc, type, t1));
10882           return t1;
10883         }
10884
10885       /* Convert ~X ^ ~Y to X ^ Y.  */
10886       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10887           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10888         return fold_build2_loc (loc, code, type,
10889                             fold_convert_loc (loc, type,
10890                                               TREE_OPERAND (arg0, 0)),
10891                             fold_convert_loc (loc, type,
10892                                               TREE_OPERAND (arg1, 0)));
10893
10894       /* Convert ~X ^ C to X ^ ~C.  */
10895       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10896           && TREE_CODE (arg1) == INTEGER_CST)
10897         return fold_build2_loc (loc, code, type,
10898                             fold_convert_loc (loc, type,
10899                                               TREE_OPERAND (arg0, 0)),
10900                             fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
10901
10902       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
10903       if (TREE_CODE (arg0) == BIT_AND_EXPR
10904           && integer_onep (TREE_OPERAND (arg0, 1))
10905           && integer_onep (arg1))
10906         return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10907                             build_int_cst (TREE_TYPE (arg0), 0));
10908
10909       /* Fold (X & Y) ^ Y as ~X & Y.  */
10910       if (TREE_CODE (arg0) == BIT_AND_EXPR
10911           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10912         {
10913           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10914           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10915                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10916                               fold_convert_loc (loc, type, arg1));
10917         }
10918       /* Fold (X & Y) ^ X as ~Y & X.  */
10919       if (TREE_CODE (arg0) == BIT_AND_EXPR
10920           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10921           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10922         {
10923           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10924           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10925                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10926                               fold_convert_loc (loc, type, arg1));
10927         }
10928       /* Fold X ^ (X & Y) as X & ~Y.  */
10929       if (TREE_CODE (arg1) == BIT_AND_EXPR
10930           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10931         {
10932           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10933           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10934                               fold_convert_loc (loc, type, arg0),
10935                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10936         }
10937       /* Fold X ^ (Y & X) as ~Y & X.  */
10938       if (TREE_CODE (arg1) == BIT_AND_EXPR
10939           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10940           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10941         {
10942           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10943           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10944                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10945                               fold_convert_loc (loc, type, arg0));
10946         }
10947
10948       /* See if this can be simplified into a rotate first.  If that
10949          is unsuccessful continue in the association code.  */
10950       goto bit_rotate;
10951
10952     case BIT_AND_EXPR:
10953       if (integer_all_onesp (arg1))
10954         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10955       if (integer_zerop (arg1))
10956         return omit_one_operand_loc (loc, type, arg1, arg0);
10957       if (operand_equal_p (arg0, arg1, 0))
10958         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10959
10960       /* ~X & X, (X == 0) & X, and !X & X are always zero.  */
10961       if ((TREE_CODE (arg0) == BIT_NOT_EXPR
10962            || TREE_CODE (arg0) == TRUTH_NOT_EXPR
10963            || (TREE_CODE (arg0) == EQ_EXPR
10964                && integer_zerop (TREE_OPERAND (arg0, 1))))
10965           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10966         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10967
10968       /* X & ~X , X & (X == 0), and X & !X are always zero.  */
10969       if ((TREE_CODE (arg1) == BIT_NOT_EXPR
10970            || TREE_CODE (arg1) == TRUTH_NOT_EXPR
10971            || (TREE_CODE (arg1) == EQ_EXPR
10972                && integer_zerop (TREE_OPERAND (arg1, 1))))
10973           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10974         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10975
10976       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
10977       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10978           && TREE_CODE (arg1) == INTEGER_CST
10979           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10980         {
10981           tree tmp1 = fold_convert_loc (loc, type, arg1);
10982           tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10983           tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10984           tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
10985           tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
10986           return
10987             fold_convert_loc (loc, type,
10988                               fold_build2_loc (loc, BIT_IOR_EXPR,
10989                                            type, tmp2, tmp3));
10990         }
10991
10992       /* (X | Y) & Y is (X, Y).  */
10993       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10994           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10995         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10996       /* (X | Y) & X is (Y, X).  */
10997       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10998           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10999           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11000         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11001       /* X & (X | Y) is (Y, X).  */
11002       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11003           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11004           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11005         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11006       /* X & (Y | X) is (Y, X).  */
11007       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11008           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11009           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11010         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11011
11012       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
11013       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11014           && integer_onep (TREE_OPERAND (arg0, 1))
11015           && integer_onep (arg1))
11016         {
11017           tem = TREE_OPERAND (arg0, 0);
11018           return fold_build2_loc (loc, EQ_EXPR, type,
11019                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11020                                            build_int_cst (TREE_TYPE (tem), 1)),
11021                               build_int_cst (TREE_TYPE (tem), 0));
11022         }
11023       /* Fold ~X & 1 as (X & 1) == 0.  */
11024       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11025           && integer_onep (arg1))
11026         {
11027           tem = TREE_OPERAND (arg0, 0);
11028           return fold_build2_loc (loc, EQ_EXPR, type,
11029                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11030                                            build_int_cst (TREE_TYPE (tem), 1)),
11031                               build_int_cst (TREE_TYPE (tem), 0));
11032         }
11033       /* Fold !X & 1 as X == 0.  */
11034       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11035           && integer_onep (arg1))
11036         {
11037           tem = TREE_OPERAND (arg0, 0);
11038           return fold_build2_loc (loc, EQ_EXPR, type, tem,
11039                                   build_int_cst (TREE_TYPE (tem), 0));
11040         }
11041
11042       /* Fold (X ^ Y) & Y as ~X & Y.  */
11043       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11044           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11045         {
11046           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11047           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11048                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11049                               fold_convert_loc (loc, type, arg1));
11050         }
11051       /* Fold (X ^ Y) & X as ~Y & X.  */
11052       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11053           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11054           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11055         {
11056           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11057           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11058                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11059                               fold_convert_loc (loc, type, arg1));
11060         }
11061       /* Fold X & (X ^ Y) as X & ~Y.  */
11062       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11063           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11064         {
11065           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11066           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11067                               fold_convert_loc (loc, type, arg0),
11068                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11069         }
11070       /* Fold X & (Y ^ X) as ~Y & X.  */
11071       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11072           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11073           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11074         {
11075           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11076           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11077                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11078                               fold_convert_loc (loc, type, arg0));
11079         }
11080
11081       /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11082          ((A & N) + B) & M -> (A + B) & M
11083          Similarly if (N & M) == 0,
11084          ((A | N) + B) & M -> (A + B) & M
11085          and for - instead of + (or unary - instead of +)
11086          and/or ^ instead of |.
11087          If B is constant and (B & M) == 0, fold into A & M.  */
11088       if (host_integerp (arg1, 1))
11089         {
11090           unsigned HOST_WIDE_INT cst1 = tree_low_cst (arg1, 1);
11091           if (~cst1 && (cst1 & (cst1 + 1)) == 0
11092               && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11093               && (TREE_CODE (arg0) == PLUS_EXPR
11094                   || TREE_CODE (arg0) == MINUS_EXPR
11095                   || TREE_CODE (arg0) == NEGATE_EXPR)
11096               && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
11097                   || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
11098             {
11099               tree pmop[2];
11100               int which = 0;
11101               unsigned HOST_WIDE_INT cst0;
11102
11103               /* Now we know that arg0 is (C + D) or (C - D) or
11104                  -C and arg1 (M) is == (1LL << cst) - 1.
11105                  Store C into PMOP[0] and D into PMOP[1].  */
11106               pmop[0] = TREE_OPERAND (arg0, 0);
11107               pmop[1] = NULL;
11108               if (TREE_CODE (arg0) != NEGATE_EXPR)
11109                 {
11110                   pmop[1] = TREE_OPERAND (arg0, 1);
11111                   which = 1;
11112                 }
11113
11114               if (!host_integerp (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11115                   || (tree_low_cst (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11116                       & cst1) != cst1)
11117                 which = -1;
11118
11119               for (; which >= 0; which--)
11120                 switch (TREE_CODE (pmop[which]))
11121                   {
11122                   case BIT_AND_EXPR:
11123                   case BIT_IOR_EXPR:
11124                   case BIT_XOR_EXPR:
11125                     if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
11126                         != INTEGER_CST)
11127                       break;
11128                     /* tree_low_cst not used, because we don't care about
11129                        the upper bits.  */
11130                     cst0 = TREE_INT_CST_LOW (TREE_OPERAND (pmop[which], 1));
11131                     cst0 &= cst1;
11132                     if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
11133                       {
11134                         if (cst0 != cst1)
11135                           break;
11136                       }
11137                     else if (cst0 != 0)
11138                       break;
11139                     /* If C or D is of the form (A & N) where
11140                        (N & M) == M, or of the form (A | N) or
11141                        (A ^ N) where (N & M) == 0, replace it with A.  */
11142                     pmop[which] = TREE_OPERAND (pmop[which], 0);
11143                     break;
11144                   case INTEGER_CST:
11145                     /* If C or D is a N where (N & M) == 0, it can be
11146                        omitted (assumed 0).  */
11147                     if ((TREE_CODE (arg0) == PLUS_EXPR
11148                          || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
11149                         && (TREE_INT_CST_LOW (pmop[which]) & cst1) == 0)
11150                       pmop[which] = NULL;
11151                     break;
11152                   default:
11153                     break;
11154                   }
11155
11156               /* Only build anything new if we optimized one or both arguments
11157                  above.  */
11158               if (pmop[0] != TREE_OPERAND (arg0, 0)
11159                   || (TREE_CODE (arg0) != NEGATE_EXPR
11160                       && pmop[1] != TREE_OPERAND (arg0, 1)))
11161                 {
11162                   tree utype = TREE_TYPE (arg0);
11163                   if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11164                     {
11165                       /* Perform the operations in a type that has defined
11166                          overflow behavior.  */
11167                       utype = unsigned_type_for (TREE_TYPE (arg0));
11168                       if (pmop[0] != NULL)
11169                         pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
11170                       if (pmop[1] != NULL)
11171                         pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
11172                     }
11173
11174                   if (TREE_CODE (arg0) == NEGATE_EXPR)
11175                     tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
11176                   else if (TREE_CODE (arg0) == PLUS_EXPR)
11177                     {
11178                       if (pmop[0] != NULL && pmop[1] != NULL)
11179                         tem = fold_build2_loc (loc, PLUS_EXPR, utype,
11180                                                pmop[0], pmop[1]);
11181                       else if (pmop[0] != NULL)
11182                         tem = pmop[0];
11183                       else if (pmop[1] != NULL)
11184                         tem = pmop[1];
11185                       else
11186                         return build_int_cst (type, 0);
11187                     }
11188                   else if (pmop[0] == NULL)
11189                     tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
11190                   else
11191                     tem = fold_build2_loc (loc, MINUS_EXPR, utype,
11192                                            pmop[0], pmop[1]);
11193                   /* TEM is now the new binary +, - or unary - replacement.  */
11194                   tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
11195                                          fold_convert_loc (loc, utype, arg1));
11196                   return fold_convert_loc (loc, type, tem);
11197                 }
11198             }
11199         }
11200
11201       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11202       if (t1 != NULL_TREE)
11203         return t1;
11204       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
11205       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11206           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11207         {
11208           unsigned int prec
11209             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11210
11211           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11212               && (~TREE_INT_CST_LOW (arg1)
11213                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11214             return
11215               fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11216         }
11217
11218       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11219
11220          This results in more efficient code for machines without a NOR
11221          instruction.  Combine will canonicalize to the first form
11222          which will allow use of NOR instructions provided by the
11223          backend if they exist.  */
11224       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11225           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11226         {
11227           return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11228                               build2 (BIT_IOR_EXPR, type,
11229                                       fold_convert_loc (loc, type,
11230                                                         TREE_OPERAND (arg0, 0)),
11231                                       fold_convert_loc (loc, type,
11232                                                         TREE_OPERAND (arg1, 0))));
11233         }
11234
11235       /* If arg0 is derived from the address of an object or function, we may
11236          be able to fold this expression using the object or function's
11237          alignment.  */
11238       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11239         {
11240           unsigned HOST_WIDE_INT modulus, residue;
11241           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11242
11243           modulus = get_pointer_modulus_and_residue (arg0, &residue,
11244                                                      integer_onep (arg1));
11245
11246           /* This works because modulus is a power of 2.  If this weren't the
11247              case, we'd have to replace it by its greatest power-of-2
11248              divisor: modulus & -modulus.  */
11249           if (low < modulus)
11250             return build_int_cst (type, residue & low);
11251         }
11252
11253       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11254               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11255          if the new mask might be further optimized.  */
11256       if ((TREE_CODE (arg0) == LSHIFT_EXPR
11257            || TREE_CODE (arg0) == RSHIFT_EXPR)
11258           && host_integerp (TREE_OPERAND (arg0, 1), 1)
11259           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11260           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11261              < TYPE_PRECISION (TREE_TYPE (arg0))
11262           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11263           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11264         {
11265           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11266           unsigned HOST_WIDE_INT mask
11267             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11268           unsigned HOST_WIDE_INT newmask, zerobits = 0;
11269           tree shift_type = TREE_TYPE (arg0);
11270
11271           if (TREE_CODE (arg0) == LSHIFT_EXPR)
11272             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11273           else if (TREE_CODE (arg0) == RSHIFT_EXPR
11274                    && TYPE_PRECISION (TREE_TYPE (arg0))
11275                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11276             {
11277               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11278               tree arg00 = TREE_OPERAND (arg0, 0);
11279               /* See if more bits can be proven as zero because of
11280                  zero extension.  */
11281               if (TREE_CODE (arg00) == NOP_EXPR
11282                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11283                 {
11284                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11285                   if (TYPE_PRECISION (inner_type)
11286                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11287                       && TYPE_PRECISION (inner_type) < prec)
11288                     {
11289                       prec = TYPE_PRECISION (inner_type);
11290                       /* See if we can shorten the right shift.  */
11291                       if (shiftc < prec)
11292                         shift_type = inner_type;
11293                     }
11294                 }
11295               zerobits = ~(unsigned HOST_WIDE_INT) 0;
11296               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11297               zerobits <<= prec - shiftc;
11298               /* For arithmetic shift if sign bit could be set, zerobits
11299                  can contain actually sign bits, so no transformation is
11300                  possible, unless MASK masks them all away.  In that
11301                  case the shift needs to be converted into logical shift.  */
11302               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11303                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11304                 {
11305                   if ((mask & zerobits) == 0)
11306                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
11307                   else
11308                     zerobits = 0;
11309                 }
11310             }
11311
11312           /* ((X << 16) & 0xff00) is (X, 0).  */
11313           if ((mask & zerobits) == mask)
11314             return omit_one_operand_loc (loc, type,
11315                                      build_int_cst (type, 0), arg0);
11316
11317           newmask = mask | zerobits;
11318           if (newmask != mask && (newmask & (newmask + 1)) == 0)
11319             {
11320               unsigned int prec;
11321
11322               /* Only do the transformation if NEWMASK is some integer
11323                  mode's mask.  */
11324               for (prec = BITS_PER_UNIT;
11325                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11326                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11327                   break;
11328               if (prec < HOST_BITS_PER_WIDE_INT
11329                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
11330                 {
11331                   tree newmaskt;
11332
11333                   if (shift_type != TREE_TYPE (arg0))
11334                     {
11335                       tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11336                                          fold_convert_loc (loc, shift_type,
11337                                                            TREE_OPERAND (arg0, 0)),
11338                                          TREE_OPERAND (arg0, 1));
11339                       tem = fold_convert_loc (loc, type, tem);
11340                     }
11341                   else
11342                     tem = op0;
11343                   newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11344                   if (!tree_int_cst_equal (newmaskt, arg1))
11345                     return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11346                 }
11347             }
11348         }
11349
11350       goto associate;
11351
11352     case RDIV_EXPR:
11353       /* Don't touch a floating-point divide by zero unless the mode
11354          of the constant can represent infinity.  */
11355       if (TREE_CODE (arg1) == REAL_CST
11356           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11357           && real_zerop (arg1))
11358         return NULL_TREE;
11359
11360       /* Optimize A / A to 1.0 if we don't care about
11361          NaNs or Infinities.  Skip the transformation
11362          for non-real operands.  */
11363       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11364           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11365           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11366           && operand_equal_p (arg0, arg1, 0))
11367         {
11368           tree r = build_real (TREE_TYPE (arg0), dconst1);
11369
11370           return omit_two_operands_loc (loc, type, r, arg0, arg1);
11371         }
11372
11373       /* The complex version of the above A / A optimization.  */
11374       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11375           && operand_equal_p (arg0, arg1, 0))
11376         {
11377           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11378           if (! HONOR_NANS (TYPE_MODE (elem_type))
11379               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11380             {
11381               tree r = build_real (elem_type, dconst1);
11382               /* omit_two_operands will call fold_convert for us.  */
11383               return omit_two_operands_loc (loc, type, r, arg0, arg1);
11384             }
11385         }
11386
11387       /* (-A) / (-B) -> A / B  */
11388       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11389         return fold_build2_loc (loc, RDIV_EXPR, type,
11390                             TREE_OPERAND (arg0, 0),
11391                             negate_expr (arg1));
11392       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11393         return fold_build2_loc (loc, RDIV_EXPR, type,
11394                             negate_expr (arg0),
11395                             TREE_OPERAND (arg1, 0));
11396
11397       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
11398       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11399           && real_onep (arg1))
11400         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11401
11402       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
11403       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11404           && real_minus_onep (arg1))
11405         return non_lvalue_loc (loc, fold_convert_loc (loc, type,
11406                                                   negate_expr (arg0)));
11407
11408       /* If ARG1 is a constant, we can convert this to a multiply by the
11409          reciprocal.  This does not have the same rounding properties,
11410          so only do this if -freciprocal-math.  We can actually
11411          always safely do it if ARG1 is a power of two, but it's hard to
11412          tell if it is or not in a portable manner.  */
11413       if (TREE_CODE (arg1) == REAL_CST)
11414         {
11415           if (flag_reciprocal_math
11416               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11417                                           arg1)))
11418             return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
11419           /* Find the reciprocal if optimizing and the result is exact.  */
11420           if (optimize)
11421             {
11422               REAL_VALUE_TYPE r;
11423               r = TREE_REAL_CST (arg1);
11424               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11425                 {
11426                   tem = build_real (type, r);
11427                   return fold_build2_loc (loc, MULT_EXPR, type,
11428                                       fold_convert_loc (loc, type, arg0), tem);
11429                 }
11430             }
11431         }
11432       /* Convert A/B/C to A/(B*C).  */
11433       if (flag_reciprocal_math
11434           && TREE_CODE (arg0) == RDIV_EXPR)
11435         return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11436                             fold_build2_loc (loc, MULT_EXPR, type,
11437                                          TREE_OPERAND (arg0, 1), arg1));
11438
11439       /* Convert A/(B/C) to (A/B)*C.  */
11440       if (flag_reciprocal_math
11441           && TREE_CODE (arg1) == RDIV_EXPR)
11442         return fold_build2_loc (loc, MULT_EXPR, type,
11443                             fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11444                                          TREE_OPERAND (arg1, 0)),
11445                             TREE_OPERAND (arg1, 1));
11446
11447       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11448       if (flag_reciprocal_math
11449           && TREE_CODE (arg1) == MULT_EXPR
11450           && TREE_CODE (arg0) == REAL_CST
11451           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11452         {
11453           tree tem = const_binop (RDIV_EXPR, arg0,
11454                                   TREE_OPERAND (arg1, 1));
11455           if (tem)
11456             return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11457                                 TREE_OPERAND (arg1, 0));
11458         }
11459
11460       if (flag_unsafe_math_optimizations)
11461         {
11462           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11463           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11464
11465           /* Optimize sin(x)/cos(x) as tan(x).  */
11466           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11467                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11468                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11469               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11470                                   CALL_EXPR_ARG (arg1, 0), 0))
11471             {
11472               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11473
11474               if (tanfn != NULL_TREE)
11475                 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11476             }
11477
11478           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11479           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11480                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11481                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11482               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11483                                   CALL_EXPR_ARG (arg1, 0), 0))
11484             {
11485               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11486
11487               if (tanfn != NULL_TREE)
11488                 {
11489                   tree tmp = build_call_expr_loc (loc, tanfn, 1,
11490                                               CALL_EXPR_ARG (arg0, 0));
11491                   return fold_build2_loc (loc, RDIV_EXPR, type,
11492                                       build_real (type, dconst1), tmp);
11493                 }
11494             }
11495
11496           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11497              NaNs or Infinities.  */
11498           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11499                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11500                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11501             {
11502               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11503               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11504
11505               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11506                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11507                   && operand_equal_p (arg00, arg01, 0))
11508                 {
11509                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11510
11511                   if (cosfn != NULL_TREE)
11512                     return build_call_expr_loc (loc, cosfn, 1, arg00);
11513                 }
11514             }
11515
11516           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11517              NaNs or Infinities.  */
11518           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11519                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11520                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11521             {
11522               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11523               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11524
11525               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11526                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11527                   && operand_equal_p (arg00, arg01, 0))
11528                 {
11529                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11530
11531                   if (cosfn != NULL_TREE)
11532                     {
11533                       tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11534                       return fold_build2_loc (loc, RDIV_EXPR, type,
11535                                           build_real (type, dconst1),
11536                                           tmp);
11537                     }
11538                 }
11539             }
11540
11541           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11542           if (fcode0 == BUILT_IN_POW
11543               || fcode0 == BUILT_IN_POWF
11544               || fcode0 == BUILT_IN_POWL)
11545             {
11546               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11547               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11548               if (TREE_CODE (arg01) == REAL_CST
11549                   && !TREE_OVERFLOW (arg01)
11550                   && operand_equal_p (arg1, arg00, 0))
11551                 {
11552                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11553                   REAL_VALUE_TYPE c;
11554                   tree arg;
11555
11556                   c = TREE_REAL_CST (arg01);
11557                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11558                   arg = build_real (type, c);
11559                   return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11560                 }
11561             }
11562
11563           /* Optimize a/root(b/c) into a*root(c/b).  */
11564           if (BUILTIN_ROOT_P (fcode1))
11565             {
11566               tree rootarg = CALL_EXPR_ARG (arg1, 0);
11567
11568               if (TREE_CODE (rootarg) == RDIV_EXPR)
11569                 {
11570                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11571                   tree b = TREE_OPERAND (rootarg, 0);
11572                   tree c = TREE_OPERAND (rootarg, 1);
11573
11574                   tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
11575
11576                   tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
11577                   return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
11578                 }
11579             }
11580
11581           /* Optimize x/expN(y) into x*expN(-y).  */
11582           if (BUILTIN_EXPONENT_P (fcode1))
11583             {
11584               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11585               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11586               arg1 = build_call_expr_loc (loc,
11587                                       expfn, 1,
11588                                       fold_convert_loc (loc, type, arg));
11589               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11590             }
11591
11592           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
11593           if (fcode1 == BUILT_IN_POW
11594               || fcode1 == BUILT_IN_POWF
11595               || fcode1 == BUILT_IN_POWL)
11596             {
11597               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11598               tree arg10 = CALL_EXPR_ARG (arg1, 0);
11599               tree arg11 = CALL_EXPR_ARG (arg1, 1);
11600               tree neg11 = fold_convert_loc (loc, type,
11601                                              negate_expr (arg11));
11602               arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
11603               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11604             }
11605         }
11606       return NULL_TREE;
11607
11608     case TRUNC_DIV_EXPR:
11609       /* Optimize (X & (-A)) / A where A is a power of 2,
11610          to X >> log2(A) */
11611       if (TREE_CODE (arg0) == BIT_AND_EXPR
11612           && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST
11613           && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0)
11614         {
11615           tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1),
11616                                       arg1, TREE_OPERAND (arg0, 1));
11617           if (sum && integer_zerop (sum)) {
11618             unsigned long pow2;
11619
11620             if (TREE_INT_CST_LOW (arg1))
11621               pow2 = exact_log2 (TREE_INT_CST_LOW (arg1));
11622             else
11623               pow2 = exact_log2 (TREE_INT_CST_HIGH (arg1))
11624                       + HOST_BITS_PER_WIDE_INT;
11625
11626             return fold_build2_loc (loc, RSHIFT_EXPR, type,
11627                           TREE_OPERAND (arg0, 0),
11628                           build_int_cst (integer_type_node, pow2));
11629           }
11630         }
11631
11632       /* Fall thru */
11633       
11634     case FLOOR_DIV_EXPR:
11635       /* Simplify A / (B << N) where A and B are positive and B is
11636          a power of 2, to A >> (N + log2(B)).  */
11637       strict_overflow_p = false;
11638       if (TREE_CODE (arg1) == LSHIFT_EXPR
11639           && (TYPE_UNSIGNED (type)
11640               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11641         {
11642           tree sval = TREE_OPERAND (arg1, 0);
11643           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11644             {
11645               tree sh_cnt = TREE_OPERAND (arg1, 1);
11646               unsigned long pow2;
11647
11648               if (TREE_INT_CST_LOW (sval))
11649                 pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11650               else
11651                 pow2 = exact_log2 (TREE_INT_CST_HIGH (sval))
11652                        + HOST_BITS_PER_WIDE_INT;
11653
11654               if (strict_overflow_p)
11655                 fold_overflow_warning (("assuming signed overflow does not "
11656                                         "occur when simplifying A / (B << N)"),
11657                                        WARN_STRICT_OVERFLOW_MISC);
11658
11659               sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11660                                         sh_cnt,
11661                                         build_int_cst (TREE_TYPE (sh_cnt),
11662                                                        pow2));
11663               return fold_build2_loc (loc, RSHIFT_EXPR, type,
11664                                   fold_convert_loc (loc, type, arg0), sh_cnt);
11665             }
11666         }
11667
11668       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11669          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
11670       if (INTEGRAL_TYPE_P (type)
11671           && TYPE_UNSIGNED (type)
11672           && code == FLOOR_DIV_EXPR)
11673         return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
11674
11675       /* Fall thru */
11676
11677     case ROUND_DIV_EXPR:
11678     case CEIL_DIV_EXPR:
11679     case EXACT_DIV_EXPR:
11680       if (integer_onep (arg1))
11681         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11682       if (integer_zerop (arg1))
11683         return NULL_TREE;
11684       /* X / -1 is -X.  */
11685       if (!TYPE_UNSIGNED (type)
11686           && TREE_CODE (arg1) == INTEGER_CST
11687           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11688           && TREE_INT_CST_HIGH (arg1) == -1)
11689         return fold_convert_loc (loc, type, negate_expr (arg0));
11690
11691       /* Convert -A / -B to A / B when the type is signed and overflow is
11692          undefined.  */
11693       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11694           && TREE_CODE (arg0) == NEGATE_EXPR
11695           && negate_expr_p (arg1))
11696         {
11697           if (INTEGRAL_TYPE_P (type))
11698             fold_overflow_warning (("assuming signed overflow does not occur "
11699                                     "when distributing negation across "
11700                                     "division"),
11701                                    WARN_STRICT_OVERFLOW_MISC);
11702           return fold_build2_loc (loc, code, type,
11703                               fold_convert_loc (loc, type,
11704                                                 TREE_OPERAND (arg0, 0)),
11705                               fold_convert_loc (loc, type,
11706                                                 negate_expr (arg1)));
11707         }
11708       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11709           && TREE_CODE (arg1) == NEGATE_EXPR
11710           && negate_expr_p (arg0))
11711         {
11712           if (INTEGRAL_TYPE_P (type))
11713             fold_overflow_warning (("assuming signed overflow does not occur "
11714                                     "when distributing negation across "
11715                                     "division"),
11716                                    WARN_STRICT_OVERFLOW_MISC);
11717           return fold_build2_loc (loc, code, type,
11718                               fold_convert_loc (loc, type,
11719                                                 negate_expr (arg0)),
11720                               fold_convert_loc (loc, type,
11721                                                 TREE_OPERAND (arg1, 0)));
11722         }
11723
11724       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11725          operation, EXACT_DIV_EXPR.
11726
11727          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11728          At one time others generated faster code, it's not clear if they do
11729          after the last round to changes to the DIV code in expmed.c.  */
11730       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11731           && multiple_of_p (type, arg0, arg1))
11732         return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
11733
11734       strict_overflow_p = false;
11735       if (TREE_CODE (arg1) == INTEGER_CST
11736           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11737                                          &strict_overflow_p)))
11738         {
11739           if (strict_overflow_p)
11740             fold_overflow_warning (("assuming signed overflow does not occur "
11741                                     "when simplifying division"),
11742                                    WARN_STRICT_OVERFLOW_MISC);
11743           return fold_convert_loc (loc, type, tem);
11744         }
11745
11746       return NULL_TREE;
11747
11748     case CEIL_MOD_EXPR:
11749     case FLOOR_MOD_EXPR:
11750     case ROUND_MOD_EXPR:
11751     case TRUNC_MOD_EXPR:
11752       /* X % 1 is always zero, but be sure to preserve any side
11753          effects in X.  */
11754       if (integer_onep (arg1))
11755         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11756
11757       /* X % 0, return X % 0 unchanged so that we can get the
11758          proper warnings and errors.  */
11759       if (integer_zerop (arg1))
11760         return NULL_TREE;
11761
11762       /* 0 % X is always zero, but be sure to preserve any side
11763          effects in X.  Place this after checking for X == 0.  */
11764       if (integer_zerop (arg0))
11765         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11766
11767       /* X % -1 is zero.  */
11768       if (!TYPE_UNSIGNED (type)
11769           && TREE_CODE (arg1) == INTEGER_CST
11770           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11771           && TREE_INT_CST_HIGH (arg1) == -1)
11772         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11773
11774       /* X % -C is the same as X % C.  */
11775       if (code == TRUNC_MOD_EXPR
11776           && !TYPE_UNSIGNED (type)
11777           && TREE_CODE (arg1) == INTEGER_CST
11778           && !TREE_OVERFLOW (arg1)
11779           && TREE_INT_CST_HIGH (arg1) < 0
11780           && !TYPE_OVERFLOW_TRAPS (type)
11781           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
11782           && !sign_bit_p (arg1, arg1))
11783         return fold_build2_loc (loc, code, type,
11784                             fold_convert_loc (loc, type, arg0),
11785                             fold_convert_loc (loc, type,
11786                                               negate_expr (arg1)));
11787
11788       /* X % -Y is the same as X % Y.  */
11789       if (code == TRUNC_MOD_EXPR
11790           && !TYPE_UNSIGNED (type)
11791           && TREE_CODE (arg1) == NEGATE_EXPR
11792           && !TYPE_OVERFLOW_TRAPS (type))
11793         return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
11794                             fold_convert_loc (loc, type,
11795                                               TREE_OPERAND (arg1, 0)));
11796
11797       strict_overflow_p = false;
11798       if (TREE_CODE (arg1) == INTEGER_CST
11799           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11800                                          &strict_overflow_p)))
11801         {
11802           if (strict_overflow_p)
11803             fold_overflow_warning (("assuming signed overflow does not occur "
11804                                     "when simplifying modulus"),
11805                                    WARN_STRICT_OVERFLOW_MISC);
11806           return fold_convert_loc (loc, type, tem);
11807         }
11808
11809       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11810          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
11811       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11812           && (TYPE_UNSIGNED (type)
11813               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11814         {
11815           tree c = arg1;
11816           /* Also optimize A % (C << N)  where C is a power of 2,
11817              to A & ((C << N) - 1).  */
11818           if (TREE_CODE (arg1) == LSHIFT_EXPR)
11819             c = TREE_OPERAND (arg1, 0);
11820
11821           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11822             {
11823               tree mask
11824                 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
11825                                    build_int_cst (TREE_TYPE (arg1), 1));
11826               if (strict_overflow_p)
11827                 fold_overflow_warning (("assuming signed overflow does not "
11828                                         "occur when simplifying "
11829                                         "X % (power of two)"),
11830                                        WARN_STRICT_OVERFLOW_MISC);
11831               return fold_build2_loc (loc, BIT_AND_EXPR, type,
11832                                       fold_convert_loc (loc, type, arg0),
11833                                       fold_convert_loc (loc, type, mask));
11834             }
11835         }
11836
11837       return NULL_TREE;
11838
11839     case LROTATE_EXPR:
11840     case RROTATE_EXPR:
11841       if (integer_all_onesp (arg0))
11842         return omit_one_operand_loc (loc, type, arg0, arg1);
11843       goto shift;
11844
11845     case RSHIFT_EXPR:
11846       /* Optimize -1 >> x for arithmetic right shifts.  */
11847       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11848           && tree_expr_nonnegative_p (arg1))
11849         return omit_one_operand_loc (loc, type, arg0, arg1);
11850       /* ... fall through ...  */
11851
11852     case LSHIFT_EXPR:
11853     shift:
11854       if (integer_zerop (arg1))
11855         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11856       if (integer_zerop (arg0))
11857         return omit_one_operand_loc (loc, type, arg0, arg1);
11858
11859       /* Since negative shift count is not well-defined,
11860          don't try to compute it in the compiler.  */
11861       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11862         return NULL_TREE;
11863
11864       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
11865       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11866           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11867           && host_integerp (TREE_OPERAND (arg0, 1), false)
11868           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11869         {
11870           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11871                                + TREE_INT_CST_LOW (arg1));
11872
11873           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11874              being well defined.  */
11875           if (low >= TYPE_PRECISION (type))
11876             {
11877               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11878                 low = low % TYPE_PRECISION (type);
11879               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11880                 return omit_one_operand_loc (loc, type, build_int_cst (type, 0),
11881                                          TREE_OPERAND (arg0, 0));
11882               else
11883                 low = TYPE_PRECISION (type) - 1;
11884             }
11885
11886           return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11887                               build_int_cst (type, low));
11888         }
11889
11890       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11891          into x & ((unsigned)-1 >> c) for unsigned types.  */
11892       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11893            || (TYPE_UNSIGNED (type)
11894                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11895           && host_integerp (arg1, false)
11896           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11897           && host_integerp (TREE_OPERAND (arg0, 1), false)
11898           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11899         {
11900           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11901           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11902           tree lshift;
11903           tree arg00;
11904
11905           if (low0 == low1)
11906             {
11907               arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11908
11909               lshift = build_int_cst (type, -1);
11910               lshift = int_const_binop (code, lshift, arg1);
11911
11912               return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
11913             }
11914         }
11915
11916       /* Rewrite an LROTATE_EXPR by a constant into an
11917          RROTATE_EXPR by a new constant.  */
11918       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11919         {
11920           tree tem = build_int_cst (TREE_TYPE (arg1),
11921                                     TYPE_PRECISION (type));
11922           tem = const_binop (MINUS_EXPR, tem, arg1);
11923           return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
11924         }
11925
11926       /* If we have a rotate of a bit operation with the rotate count and
11927          the second operand of the bit operation both constant,
11928          permute the two operations.  */
11929       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11930           && (TREE_CODE (arg0) == BIT_AND_EXPR
11931               || TREE_CODE (arg0) == BIT_IOR_EXPR
11932               || TREE_CODE (arg0) == BIT_XOR_EXPR)
11933           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11934         return fold_build2_loc (loc, TREE_CODE (arg0), type,
11935                             fold_build2_loc (loc, code, type,
11936                                          TREE_OPERAND (arg0, 0), arg1),
11937                             fold_build2_loc (loc, code, type,
11938                                          TREE_OPERAND (arg0, 1), arg1));
11939
11940       /* Two consecutive rotates adding up to the precision of the
11941          type can be ignored.  */
11942       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11943           && TREE_CODE (arg0) == RROTATE_EXPR
11944           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11945           && TREE_INT_CST_HIGH (arg1) == 0
11946           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11947           && ((TREE_INT_CST_LOW (arg1)
11948                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11949               == (unsigned int) TYPE_PRECISION (type)))
11950         return TREE_OPERAND (arg0, 0);
11951
11952       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11953               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11954          if the latter can be further optimized.  */
11955       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11956           && TREE_CODE (arg0) == BIT_AND_EXPR
11957           && TREE_CODE (arg1) == INTEGER_CST
11958           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11959         {
11960           tree mask = fold_build2_loc (loc, code, type,
11961                                    fold_convert_loc (loc, type,
11962                                                      TREE_OPERAND (arg0, 1)),
11963                                    arg1);
11964           tree shift = fold_build2_loc (loc, code, type,
11965                                     fold_convert_loc (loc, type,
11966                                                       TREE_OPERAND (arg0, 0)),
11967                                     arg1);
11968           tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
11969           if (tem)
11970             return tem;
11971         }
11972
11973       return NULL_TREE;
11974
11975     case MIN_EXPR:
11976       if (operand_equal_p (arg0, arg1, 0))
11977         return omit_one_operand_loc (loc, type, arg0, arg1);
11978       if (INTEGRAL_TYPE_P (type)
11979           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11980         return omit_one_operand_loc (loc, type, arg1, arg0);
11981       tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
11982       if (tem)
11983         return tem;
11984       goto associate;
11985
11986     case MAX_EXPR:
11987       if (operand_equal_p (arg0, arg1, 0))
11988         return omit_one_operand_loc (loc, type, arg0, arg1);
11989       if (INTEGRAL_TYPE_P (type)
11990           && TYPE_MAX_VALUE (type)
11991           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11992         return omit_one_operand_loc (loc, type, arg1, arg0);
11993       tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
11994       if (tem)
11995         return tem;
11996       goto associate;
11997
11998     case TRUTH_ANDIF_EXPR:
11999       /* Note that the operands of this must be ints
12000          and their values must be 0 or 1.
12001          ("true" is a fixed value perhaps depending on the language.)  */
12002       /* If first arg is constant zero, return it.  */
12003       if (integer_zerop (arg0))
12004         return fold_convert_loc (loc, type, arg0);
12005     case TRUTH_AND_EXPR:
12006       /* If either arg is constant true, drop it.  */
12007       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12008         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12009       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12010           /* Preserve sequence points.  */
12011           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12012         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12013       /* If second arg is constant zero, result is zero, but first arg
12014          must be evaluated.  */
12015       if (integer_zerop (arg1))
12016         return omit_one_operand_loc (loc, type, arg1, arg0);
12017       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12018          case will be handled here.  */
12019       if (integer_zerop (arg0))
12020         return omit_one_operand_loc (loc, type, arg0, arg1);
12021
12022       /* !X && X is always false.  */
12023       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12024           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12025         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12026       /* X && !X is always false.  */
12027       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12028           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12029         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12030
12031       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
12032          means A >= Y && A != MAX, but in this case we know that
12033          A < X <= MAX.  */
12034
12035       if (!TREE_SIDE_EFFECTS (arg0)
12036           && !TREE_SIDE_EFFECTS (arg1))
12037         {
12038           tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12039           if (tem && !operand_equal_p (tem, arg0, 0))
12040             return fold_build2_loc (loc, code, type, tem, arg1);
12041
12042           tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12043           if (tem && !operand_equal_p (tem, arg1, 0))
12044             return fold_build2_loc (loc, code, type, arg0, tem);
12045         }
12046
12047       if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12048           != NULL_TREE)
12049         return tem;
12050
12051       return NULL_TREE;
12052
12053     case TRUTH_ORIF_EXPR:
12054       /* Note that the operands of this must be ints
12055          and their values must be 0 or true.
12056          ("true" is a fixed value perhaps depending on the language.)  */
12057       /* If first arg is constant true, return it.  */
12058       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12059         return fold_convert_loc (loc, type, arg0);
12060     case TRUTH_OR_EXPR:
12061       /* If either arg is constant zero, drop it.  */
12062       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12063         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12064       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12065           /* Preserve sequence points.  */
12066           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12067         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12068       /* If second arg is constant true, result is true, but we must
12069          evaluate first arg.  */
12070       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12071         return omit_one_operand_loc (loc, type, arg1, arg0);
12072       /* Likewise for first arg, but note this only occurs here for
12073          TRUTH_OR_EXPR.  */
12074       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12075         return omit_one_operand_loc (loc, type, arg0, arg1);
12076
12077       /* !X || X is always true.  */
12078       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12079           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12080         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12081       /* X || !X is always true.  */
12082       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12083           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12084         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12085
12086       /* (X && !Y) || (!X && Y) is X ^ Y */
12087       if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12088           && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12089         {
12090           tree a0, a1, l0, l1, n0, n1;
12091
12092           a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12093           a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12094
12095           l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12096           l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12097           
12098           n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12099           n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12100           
12101           if ((operand_equal_p (n0, a0, 0)
12102                && operand_equal_p (n1, a1, 0))
12103               || (operand_equal_p (n0, a1, 0)
12104                   && operand_equal_p (n1, a0, 0)))
12105             return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12106         }
12107
12108       if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12109           != NULL_TREE)
12110         return tem;
12111
12112       return NULL_TREE;
12113
12114     case TRUTH_XOR_EXPR:
12115       /* If the second arg is constant zero, drop it.  */
12116       if (integer_zerop (arg1))
12117         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12118       /* If the second arg is constant true, this is a logical inversion.  */
12119       if (integer_onep (arg1))
12120         {
12121           /* Only call invert_truthvalue if operand is a truth value.  */
12122           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12123             tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12124           else
12125             tem = invert_truthvalue_loc (loc, arg0);
12126           return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12127         }
12128       /* Identical arguments cancel to zero.  */
12129       if (operand_equal_p (arg0, arg1, 0))
12130         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12131
12132       /* !X ^ X is always true.  */
12133       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12134           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12135         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12136
12137       /* X ^ !X is always true.  */
12138       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12139           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12140         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12141
12142       return NULL_TREE;
12143
12144     case EQ_EXPR:
12145     case NE_EXPR:
12146       STRIP_NOPS (arg0);
12147       STRIP_NOPS (arg1);
12148
12149       tem = fold_comparison (loc, code, type, op0, op1);
12150       if (tem != NULL_TREE)
12151         return tem;
12152
12153       /* bool_var != 0 becomes bool_var. */
12154       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12155           && code == NE_EXPR)
12156         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12157
12158       /* bool_var == 1 becomes bool_var. */
12159       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12160           && code == EQ_EXPR)
12161         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12162
12163       /* bool_var != 1 becomes !bool_var. */
12164       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12165           && code == NE_EXPR)
12166         return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12167                             fold_convert_loc (loc, type, arg0));
12168
12169       /* bool_var == 0 becomes !bool_var. */
12170       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12171           && code == EQ_EXPR)
12172         return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12173                             fold_convert_loc (loc, type, arg0));
12174
12175       /* !exp != 0 becomes !exp */
12176       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12177           && code == NE_EXPR)
12178         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12179
12180       /* If this is an equality comparison of the address of two non-weak,
12181          unaliased symbols neither of which are extern (since we do not
12182          have access to attributes for externs), then we know the result.  */
12183       if (TREE_CODE (arg0) == ADDR_EXPR
12184           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12185           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12186           && ! lookup_attribute ("alias",
12187                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12188           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12189           && TREE_CODE (arg1) == ADDR_EXPR
12190           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12191           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12192           && ! lookup_attribute ("alias",
12193                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12194           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12195         {
12196           /* We know that we're looking at the address of two
12197              non-weak, unaliased, static _DECL nodes.
12198
12199              It is both wasteful and incorrect to call operand_equal_p
12200              to compare the two ADDR_EXPR nodes.  It is wasteful in that
12201              all we need to do is test pointer equality for the arguments
12202              to the two ADDR_EXPR nodes.  It is incorrect to use
12203              operand_equal_p as that function is NOT equivalent to a
12204              C equality test.  It can in fact return false for two
12205              objects which would test as equal using the C equality
12206              operator.  */
12207           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12208           return constant_boolean_node (equal
12209                                         ? code == EQ_EXPR : code != EQ_EXPR,
12210                                         type);
12211         }
12212
12213       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12214          a MINUS_EXPR of a constant, we can convert it into a comparison with
12215          a revised constant as long as no overflow occurs.  */
12216       if (TREE_CODE (arg1) == INTEGER_CST
12217           && (TREE_CODE (arg0) == PLUS_EXPR
12218               || TREE_CODE (arg0) == MINUS_EXPR)
12219           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12220           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12221                                       ? MINUS_EXPR : PLUS_EXPR,
12222                                       fold_convert_loc (loc, TREE_TYPE (arg0),
12223                                                         arg1),
12224                                       TREE_OPERAND (arg0, 1)))
12225           && !TREE_OVERFLOW (tem))
12226         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12227
12228       /* Similarly for a NEGATE_EXPR.  */
12229       if (TREE_CODE (arg0) == NEGATE_EXPR
12230           && TREE_CODE (arg1) == INTEGER_CST
12231           && 0 != (tem = negate_expr (fold_convert_loc (loc, TREE_TYPE (arg0),
12232                                                         arg1)))
12233           && TREE_CODE (tem) == INTEGER_CST
12234           && !TREE_OVERFLOW (tem))
12235         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12236
12237       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
12238       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12239           && TREE_CODE (arg1) == INTEGER_CST
12240           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12241         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12242                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12243                                          fold_convert_loc (loc,
12244                                                            TREE_TYPE (arg0),
12245                                                            arg1),
12246                                          TREE_OPERAND (arg0, 1)));
12247
12248       /* Transform comparisons of the form X +- Y CMP X to Y CMP 0.  */
12249       if ((TREE_CODE (arg0) == PLUS_EXPR
12250            || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12251            || TREE_CODE (arg0) == MINUS_EXPR)
12252           && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12253                                                                         0)),
12254                               arg1, 0)
12255           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12256               || POINTER_TYPE_P (TREE_TYPE (arg0))))
12257         {
12258           tree val = TREE_OPERAND (arg0, 1);
12259           return omit_two_operands_loc (loc, type,
12260                                     fold_build2_loc (loc, code, type,
12261                                                  val,
12262                                                  build_int_cst (TREE_TYPE (val),
12263                                                                 0)),
12264                                     TREE_OPERAND (arg0, 0), arg1);
12265         }
12266
12267       /* Transform comparisons of the form C - X CMP X if C % 2 == 1.  */
12268       if (TREE_CODE (arg0) == MINUS_EXPR
12269           && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12270           && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12271                                                                         1)),
12272                               arg1, 0)
12273           && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12274         {
12275           return omit_two_operands_loc (loc, type,
12276                                     code == NE_EXPR
12277                                     ? boolean_true_node : boolean_false_node,
12278                                     TREE_OPERAND (arg0, 1), arg1);
12279         }
12280
12281       /* If we have X - Y == 0, we can convert that to X == Y and similarly
12282          for !=.  Don't do this for ordered comparisons due to overflow.  */
12283       if (TREE_CODE (arg0) == MINUS_EXPR
12284           && integer_zerop (arg1))
12285         return fold_build2_loc (loc, code, type,
12286                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12287
12288       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
12289       if (TREE_CODE (arg0) == ABS_EXPR
12290           && (integer_zerop (arg1) || real_zerop (arg1)))
12291         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12292
12293       /* If this is an EQ or NE comparison with zero and ARG0 is
12294          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
12295          two operations, but the latter can be done in one less insn
12296          on machines that have only two-operand insns or on which a
12297          constant cannot be the first operand.  */
12298       if (TREE_CODE (arg0) == BIT_AND_EXPR
12299           && integer_zerop (arg1))
12300         {
12301           tree arg00 = TREE_OPERAND (arg0, 0);
12302           tree arg01 = TREE_OPERAND (arg0, 1);
12303           if (TREE_CODE (arg00) == LSHIFT_EXPR
12304               && integer_onep (TREE_OPERAND (arg00, 0)))
12305             {
12306               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12307                                       arg01, TREE_OPERAND (arg00, 1));
12308               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12309                                  build_int_cst (TREE_TYPE (arg0), 1));
12310               return fold_build2_loc (loc, code, type,
12311                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12312                                   arg1);
12313             }
12314           else if (TREE_CODE (arg01) == LSHIFT_EXPR
12315                    && integer_onep (TREE_OPERAND (arg01, 0)))
12316             {
12317               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12318                                       arg00, TREE_OPERAND (arg01, 1));
12319               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12320                                  build_int_cst (TREE_TYPE (arg0), 1));
12321               return fold_build2_loc (loc, code, type,
12322                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12323                                   arg1);
12324             }
12325         }
12326
12327       /* If this is an NE or EQ comparison of zero against the result of a
12328          signed MOD operation whose second operand is a power of 2, make
12329          the MOD operation unsigned since it is simpler and equivalent.  */
12330       if (integer_zerop (arg1)
12331           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12332           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12333               || TREE_CODE (arg0) == CEIL_MOD_EXPR
12334               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12335               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12336           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12337         {
12338           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12339           tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12340                                      fold_convert_loc (loc, newtype,
12341                                                        TREE_OPERAND (arg0, 0)),
12342                                      fold_convert_loc (loc, newtype,
12343                                                        TREE_OPERAND (arg0, 1)));
12344
12345           return fold_build2_loc (loc, code, type, newmod,
12346                               fold_convert_loc (loc, newtype, arg1));
12347         }
12348
12349       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12350          C1 is a valid shift constant, and C2 is a power of two, i.e.
12351          a single bit.  */
12352       if (TREE_CODE (arg0) == BIT_AND_EXPR
12353           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12354           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12355              == INTEGER_CST
12356           && integer_pow2p (TREE_OPERAND (arg0, 1))
12357           && integer_zerop (arg1))
12358         {
12359           tree itype = TREE_TYPE (arg0);
12360           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12361           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12362
12363           /* Check for a valid shift count.  */
12364           if (TREE_INT_CST_HIGH (arg001) == 0
12365               && TREE_INT_CST_LOW (arg001) < prec)
12366             {
12367               tree arg01 = TREE_OPERAND (arg0, 1);
12368               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12369               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12370               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12371                  can be rewritten as (X & (C2 << C1)) != 0.  */
12372               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12373                 {
12374                   tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12375                   tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12376                   return fold_build2_loc (loc, code, type, tem,
12377                                           fold_convert_loc (loc, itype, arg1));
12378                 }
12379               /* Otherwise, for signed (arithmetic) shifts,
12380                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12381                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
12382               else if (!TYPE_UNSIGNED (itype))
12383                 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12384                                     arg000, build_int_cst (itype, 0));
12385               /* Otherwise, of unsigned (logical) shifts,
12386                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12387                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
12388               else
12389                 return omit_one_operand_loc (loc, type,
12390                                          code == EQ_EXPR ? integer_one_node
12391                                                          : integer_zero_node,
12392                                          arg000);
12393             }
12394         }
12395
12396       /* If we have (A & C) == C where C is a power of 2, convert this into
12397          (A & C) != 0.  Similarly for NE_EXPR.  */
12398       if (TREE_CODE (arg0) == BIT_AND_EXPR
12399           && integer_pow2p (TREE_OPERAND (arg0, 1))
12400           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12401         return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12402                             arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12403                                                     integer_zero_node));
12404
12405       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12406          bit, then fold the expression into A < 0 or A >= 0.  */
12407       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12408       if (tem)
12409         return tem;
12410
12411       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12412          Similarly for NE_EXPR.  */
12413       if (TREE_CODE (arg0) == BIT_AND_EXPR
12414           && TREE_CODE (arg1) == INTEGER_CST
12415           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12416         {
12417           tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12418                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
12419                                    TREE_OPERAND (arg0, 1));
12420           tree dandnotc
12421             = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12422                                fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
12423                                notc);
12424           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12425           if (integer_nonzerop (dandnotc))
12426             return omit_one_operand_loc (loc, type, rslt, arg0);
12427         }
12428
12429       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12430          Similarly for NE_EXPR.  */
12431       if (TREE_CODE (arg0) == BIT_IOR_EXPR
12432           && TREE_CODE (arg1) == INTEGER_CST
12433           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12434         {
12435           tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12436           tree candnotd
12437             = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12438                                TREE_OPERAND (arg0, 1),
12439                                fold_convert_loc (loc, TREE_TYPE (arg0), notd));
12440           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12441           if (integer_nonzerop (candnotd))
12442             return omit_one_operand_loc (loc, type, rslt, arg0);
12443         }
12444
12445       /* If this is a comparison of a field, we may be able to simplify it.  */
12446       if ((TREE_CODE (arg0) == COMPONENT_REF
12447            || TREE_CODE (arg0) == BIT_FIELD_REF)
12448           /* Handle the constant case even without -O
12449              to make sure the warnings are given.  */
12450           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12451         {
12452           t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12453           if (t1)
12454             return t1;
12455         }
12456
12457       /* Optimize comparisons of strlen vs zero to a compare of the
12458          first character of the string vs zero.  To wit,
12459                 strlen(ptr) == 0   =>  *ptr == 0
12460                 strlen(ptr) != 0   =>  *ptr != 0
12461          Other cases should reduce to one of these two (or a constant)
12462          due to the return value of strlen being unsigned.  */
12463       if (TREE_CODE (arg0) == CALL_EXPR
12464           && integer_zerop (arg1))
12465         {
12466           tree fndecl = get_callee_fndecl (arg0);
12467
12468           if (fndecl
12469               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12470               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12471               && call_expr_nargs (arg0) == 1
12472               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12473             {
12474               tree iref = build_fold_indirect_ref_loc (loc,
12475                                                    CALL_EXPR_ARG (arg0, 0));
12476               return fold_build2_loc (loc, code, type, iref,
12477                                   build_int_cst (TREE_TYPE (iref), 0));
12478             }
12479         }
12480
12481       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12482          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12483       if (TREE_CODE (arg0) == RSHIFT_EXPR
12484           && integer_zerop (arg1)
12485           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12486         {
12487           tree arg00 = TREE_OPERAND (arg0, 0);
12488           tree arg01 = TREE_OPERAND (arg0, 1);
12489           tree itype = TREE_TYPE (arg00);
12490           if (TREE_INT_CST_HIGH (arg01) == 0
12491               && TREE_INT_CST_LOW (arg01)
12492                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12493             {
12494               if (TYPE_UNSIGNED (itype))
12495                 {
12496                   itype = signed_type_for (itype);
12497                   arg00 = fold_convert_loc (loc, itype, arg00);
12498                 }
12499               return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12500                                   type, arg00, build_int_cst (itype, 0));
12501             }
12502         }
12503
12504       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12505       if (integer_zerop (arg1)
12506           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12507         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12508                             TREE_OPERAND (arg0, 1));
12509
12510       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12511       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12512           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12513         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12514                                 build_int_cst (TREE_TYPE (arg0), 0));
12515       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12516       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12517           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12518           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12519         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12520                                 build_int_cst (TREE_TYPE (arg0), 0));
12521
12522       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12523       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12524           && TREE_CODE (arg1) == INTEGER_CST
12525           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12526         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12527                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12528                                          TREE_OPERAND (arg0, 1), arg1));
12529
12530       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12531          (X & C) == 0 when C is a single bit.  */
12532       if (TREE_CODE (arg0) == BIT_AND_EXPR
12533           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12534           && integer_zerop (arg1)
12535           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12536         {
12537           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12538                                  TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12539                                  TREE_OPERAND (arg0, 1));
12540           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12541                                   type, tem,
12542                                   fold_convert_loc (loc, TREE_TYPE (arg0),
12543                                                     arg1));
12544         }
12545
12546       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12547          constant C is a power of two, i.e. a single bit.  */
12548       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12549           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12550           && integer_zerop (arg1)
12551           && integer_pow2p (TREE_OPERAND (arg0, 1))
12552           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12553                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12554         {
12555           tree arg00 = TREE_OPERAND (arg0, 0);
12556           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12557                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
12558         }
12559
12560       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12561          when is C is a power of two, i.e. a single bit.  */
12562       if (TREE_CODE (arg0) == BIT_AND_EXPR
12563           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12564           && integer_zerop (arg1)
12565           && integer_pow2p (TREE_OPERAND (arg0, 1))
12566           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12567                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12568         {
12569           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12570           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12571                              arg000, TREE_OPERAND (arg0, 1));
12572           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12573                               tem, build_int_cst (TREE_TYPE (tem), 0));
12574         }
12575
12576       if (integer_zerop (arg1)
12577           && tree_expr_nonzero_p (arg0))
12578         {
12579           tree res = constant_boolean_node (code==NE_EXPR, type);
12580           return omit_one_operand_loc (loc, type, res, arg0);
12581         }
12582
12583       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
12584       if (TREE_CODE (arg0) == NEGATE_EXPR
12585           && TREE_CODE (arg1) == NEGATE_EXPR)
12586         return fold_build2_loc (loc, code, type,
12587                                 TREE_OPERAND (arg0, 0),
12588                                 fold_convert_loc (loc, TREE_TYPE (arg0),
12589                                                   TREE_OPERAND (arg1, 0)));
12590
12591       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
12592       if (TREE_CODE (arg0) == BIT_AND_EXPR
12593           && TREE_CODE (arg1) == BIT_AND_EXPR)
12594         {
12595           tree arg00 = TREE_OPERAND (arg0, 0);
12596           tree arg01 = TREE_OPERAND (arg0, 1);
12597           tree arg10 = TREE_OPERAND (arg1, 0);
12598           tree arg11 = TREE_OPERAND (arg1, 1);
12599           tree itype = TREE_TYPE (arg0);
12600
12601           if (operand_equal_p (arg01, arg11, 0))
12602             return fold_build2_loc (loc, code, type,
12603                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12604                                              fold_build2_loc (loc,
12605                                                           BIT_XOR_EXPR, itype,
12606                                                           arg00, arg10),
12607                                              arg01),
12608                                 build_int_cst (itype, 0));
12609
12610           if (operand_equal_p (arg01, arg10, 0))
12611             return fold_build2_loc (loc, code, type,
12612                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12613                                              fold_build2_loc (loc,
12614                                                           BIT_XOR_EXPR, itype,
12615                                                           arg00, arg11),
12616                                              arg01),
12617                                 build_int_cst (itype, 0));
12618
12619           if (operand_equal_p (arg00, arg11, 0))
12620             return fold_build2_loc (loc, code, type,
12621                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12622                                              fold_build2_loc (loc,
12623                                                           BIT_XOR_EXPR, itype,
12624                                                           arg01, arg10),
12625                                              arg00),
12626                                 build_int_cst (itype, 0));
12627
12628           if (operand_equal_p (arg00, arg10, 0))
12629             return fold_build2_loc (loc, code, type,
12630                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12631                                              fold_build2_loc (loc,
12632                                                           BIT_XOR_EXPR, itype,
12633                                                           arg01, arg11),
12634                                              arg00),
12635                                 build_int_cst (itype, 0));
12636         }
12637
12638       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12639           && TREE_CODE (arg1) == BIT_XOR_EXPR)
12640         {
12641           tree arg00 = TREE_OPERAND (arg0, 0);
12642           tree arg01 = TREE_OPERAND (arg0, 1);
12643           tree arg10 = TREE_OPERAND (arg1, 0);
12644           tree arg11 = TREE_OPERAND (arg1, 1);
12645           tree itype = TREE_TYPE (arg0);
12646
12647           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12648              operand_equal_p guarantees no side-effects so we don't need
12649              to use omit_one_operand on Z.  */
12650           if (operand_equal_p (arg01, arg11, 0))
12651             return fold_build2_loc (loc, code, type, arg00,
12652                                     fold_convert_loc (loc, TREE_TYPE (arg00),
12653                                                       arg10));
12654           if (operand_equal_p (arg01, arg10, 0))
12655             return fold_build2_loc (loc, code, type, arg00,
12656                                     fold_convert_loc (loc, TREE_TYPE (arg00),
12657                                                       arg11));
12658           if (operand_equal_p (arg00, arg11, 0))
12659             return fold_build2_loc (loc, code, type, arg01,
12660                                     fold_convert_loc (loc, TREE_TYPE (arg01),
12661                                                       arg10));
12662           if (operand_equal_p (arg00, arg10, 0))
12663             return fold_build2_loc (loc, code, type, arg01,
12664                                     fold_convert_loc (loc, TREE_TYPE (arg01),
12665                                                       arg11));
12666
12667           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
12668           if (TREE_CODE (arg01) == INTEGER_CST
12669               && TREE_CODE (arg11) == INTEGER_CST)
12670             {
12671               tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12672                                      fold_convert_loc (loc, itype, arg11));
12673               tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12674               return fold_build2_loc (loc, code, type, tem,
12675                                       fold_convert_loc (loc, itype, arg10));
12676             }
12677         }
12678
12679       /* Attempt to simplify equality/inequality comparisons of complex
12680          values.  Only lower the comparison if the result is known or
12681          can be simplified to a single scalar comparison.  */
12682       if ((TREE_CODE (arg0) == COMPLEX_EXPR
12683            || TREE_CODE (arg0) == COMPLEX_CST)
12684           && (TREE_CODE (arg1) == COMPLEX_EXPR
12685               || TREE_CODE (arg1) == COMPLEX_CST))
12686         {
12687           tree real0, imag0, real1, imag1;
12688           tree rcond, icond;
12689
12690           if (TREE_CODE (arg0) == COMPLEX_EXPR)
12691             {
12692               real0 = TREE_OPERAND (arg0, 0);
12693               imag0 = TREE_OPERAND (arg0, 1);
12694             }
12695           else
12696             {
12697               real0 = TREE_REALPART (arg0);
12698               imag0 = TREE_IMAGPART (arg0);
12699             }
12700
12701           if (TREE_CODE (arg1) == COMPLEX_EXPR)
12702             {
12703               real1 = TREE_OPERAND (arg1, 0);
12704               imag1 = TREE_OPERAND (arg1, 1);
12705             }
12706           else
12707             {
12708               real1 = TREE_REALPART (arg1);
12709               imag1 = TREE_IMAGPART (arg1);
12710             }
12711
12712           rcond = fold_binary_loc (loc, code, type, real0, real1);
12713           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12714             {
12715               if (integer_zerop (rcond))
12716                 {
12717                   if (code == EQ_EXPR)
12718                     return omit_two_operands_loc (loc, type, boolean_false_node,
12719                                               imag0, imag1);
12720                   return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12721                 }
12722               else
12723                 {
12724                   if (code == NE_EXPR)
12725                     return omit_two_operands_loc (loc, type, boolean_true_node,
12726                                               imag0, imag1);
12727                   return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12728                 }
12729             }
12730
12731           icond = fold_binary_loc (loc, code, type, imag0, imag1);
12732           if (icond && TREE_CODE (icond) == INTEGER_CST)
12733             {
12734               if (integer_zerop (icond))
12735                 {
12736                   if (code == EQ_EXPR)
12737                     return omit_two_operands_loc (loc, type, boolean_false_node,
12738                                               real0, real1);
12739                   return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12740                 }
12741               else
12742                 {
12743                   if (code == NE_EXPR)
12744                     return omit_two_operands_loc (loc, type, boolean_true_node,
12745                                               real0, real1);
12746                   return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12747                 }
12748             }
12749         }
12750
12751       return NULL_TREE;
12752
12753     case LT_EXPR:
12754     case GT_EXPR:
12755     case LE_EXPR:
12756     case GE_EXPR:
12757       tem = fold_comparison (loc, code, type, op0, op1);
12758       if (tem != NULL_TREE)
12759         return tem;
12760
12761       /* Transform comparisons of the form X +- C CMP X.  */
12762       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12763           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12764           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12765                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12766               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12767                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12768         {
12769           tree arg01 = TREE_OPERAND (arg0, 1);
12770           enum tree_code code0 = TREE_CODE (arg0);
12771           int is_positive;
12772
12773           if (TREE_CODE (arg01) == REAL_CST)
12774             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12775           else
12776             is_positive = tree_int_cst_sgn (arg01);
12777
12778           /* (X - c) > X becomes false.  */
12779           if (code == GT_EXPR
12780               && ((code0 == MINUS_EXPR && is_positive >= 0)
12781                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12782             {
12783               if (TREE_CODE (arg01) == INTEGER_CST
12784                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12785                 fold_overflow_warning (("assuming signed overflow does not "
12786                                         "occur when assuming that (X - c) > X "
12787                                         "is always false"),
12788                                        WARN_STRICT_OVERFLOW_ALL);
12789               return constant_boolean_node (0, type);
12790             }
12791
12792           /* Likewise (X + c) < X becomes false.  */
12793           if (code == LT_EXPR
12794               && ((code0 == PLUS_EXPR && is_positive >= 0)
12795                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12796             {
12797               if (TREE_CODE (arg01) == INTEGER_CST
12798                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12799                 fold_overflow_warning (("assuming signed overflow does not "
12800                                         "occur when assuming that "
12801                                         "(X + c) < X is always false"),
12802                                        WARN_STRICT_OVERFLOW_ALL);
12803               return constant_boolean_node (0, type);
12804             }
12805
12806           /* Convert (X - c) <= X to true.  */
12807           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12808               && code == LE_EXPR
12809               && ((code0 == MINUS_EXPR && is_positive >= 0)
12810                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12811             {
12812               if (TREE_CODE (arg01) == INTEGER_CST
12813                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12814                 fold_overflow_warning (("assuming signed overflow does not "
12815                                         "occur when assuming that "
12816                                         "(X - c) <= X is always true"),
12817                                        WARN_STRICT_OVERFLOW_ALL);
12818               return constant_boolean_node (1, type);
12819             }
12820
12821           /* Convert (X + c) >= X to true.  */
12822           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12823               && code == GE_EXPR
12824               && ((code0 == PLUS_EXPR && is_positive >= 0)
12825                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12826             {
12827               if (TREE_CODE (arg01) == INTEGER_CST
12828                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12829                 fold_overflow_warning (("assuming signed overflow does not "
12830                                         "occur when assuming that "
12831                                         "(X + c) >= X is always true"),
12832                                        WARN_STRICT_OVERFLOW_ALL);
12833               return constant_boolean_node (1, type);
12834             }
12835
12836           if (TREE_CODE (arg01) == INTEGER_CST)
12837             {
12838               /* Convert X + c > X and X - c < X to true for integers.  */
12839               if (code == GT_EXPR
12840                   && ((code0 == PLUS_EXPR && is_positive > 0)
12841                       || (code0 == MINUS_EXPR && is_positive < 0)))
12842                 {
12843                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12844                     fold_overflow_warning (("assuming signed overflow does "
12845                                             "not occur when assuming that "
12846                                             "(X + c) > X is always true"),
12847                                            WARN_STRICT_OVERFLOW_ALL);
12848                   return constant_boolean_node (1, type);
12849                 }
12850
12851               if (code == LT_EXPR
12852                   && ((code0 == MINUS_EXPR && is_positive > 0)
12853                       || (code0 == PLUS_EXPR && is_positive < 0)))
12854                 {
12855                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12856                     fold_overflow_warning (("assuming signed overflow does "
12857                                             "not occur when assuming that "
12858                                             "(X - c) < X is always true"),
12859                                            WARN_STRICT_OVERFLOW_ALL);
12860                   return constant_boolean_node (1, type);
12861                 }
12862
12863               /* Convert X + c <= X and X - c >= X to false for integers.  */
12864               if (code == LE_EXPR
12865                   && ((code0 == PLUS_EXPR && is_positive > 0)
12866                       || (code0 == MINUS_EXPR && is_positive < 0)))
12867                 {
12868                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12869                     fold_overflow_warning (("assuming signed overflow does "
12870                                             "not occur when assuming that "
12871                                             "(X + c) <= X is always false"),
12872                                            WARN_STRICT_OVERFLOW_ALL);
12873                   return constant_boolean_node (0, type);
12874                 }
12875
12876               if (code == GE_EXPR
12877                   && ((code0 == MINUS_EXPR && is_positive > 0)
12878                       || (code0 == PLUS_EXPR && is_positive < 0)))
12879                 {
12880                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12881                     fold_overflow_warning (("assuming signed overflow does "
12882                                             "not occur when assuming that "
12883                                             "(X - c) >= X is always false"),
12884                                            WARN_STRICT_OVERFLOW_ALL);
12885                   return constant_boolean_node (0, type);
12886                 }
12887             }
12888         }
12889
12890       /* Comparisons with the highest or lowest possible integer of
12891          the specified precision will have known values.  */
12892       {
12893         tree arg1_type = TREE_TYPE (arg1);
12894         unsigned int width = TYPE_PRECISION (arg1_type);
12895
12896         if (TREE_CODE (arg1) == INTEGER_CST
12897             && width <= 2 * HOST_BITS_PER_WIDE_INT
12898             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12899           {
12900             HOST_WIDE_INT signed_max_hi;
12901             unsigned HOST_WIDE_INT signed_max_lo;
12902             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12903
12904             if (width <= HOST_BITS_PER_WIDE_INT)
12905               {
12906                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12907                                 - 1;
12908                 signed_max_hi = 0;
12909                 max_hi = 0;
12910
12911                 if (TYPE_UNSIGNED (arg1_type))
12912                   {
12913                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12914                     min_lo = 0;
12915                     min_hi = 0;
12916                   }
12917                 else
12918                   {
12919                     max_lo = signed_max_lo;
12920                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12921                     min_hi = -1;
12922                   }
12923               }
12924             else
12925               {
12926                 width -= HOST_BITS_PER_WIDE_INT;
12927                 signed_max_lo = -1;
12928                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12929                                 - 1;
12930                 max_lo = -1;
12931                 min_lo = 0;
12932
12933                 if (TYPE_UNSIGNED (arg1_type))
12934                   {
12935                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12936                     min_hi = 0;
12937                   }
12938                 else
12939                   {
12940                     max_hi = signed_max_hi;
12941                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12942                   }
12943               }
12944
12945             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12946                 && TREE_INT_CST_LOW (arg1) == max_lo)
12947               switch (code)
12948                 {
12949                 case GT_EXPR:
12950                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12951
12952                 case GE_EXPR:
12953                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12954
12955                 case LE_EXPR:
12956                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12957
12958                 case LT_EXPR:
12959                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12960
12961                 /* The GE_EXPR and LT_EXPR cases above are not normally
12962                    reached because of previous transformations.  */
12963
12964                 default:
12965                   break;
12966                 }
12967             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12968                      == max_hi
12969                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12970               switch (code)
12971                 {
12972                 case GT_EXPR:
12973                   arg1 = const_binop (PLUS_EXPR, arg1,
12974                                       build_int_cst (TREE_TYPE (arg1), 1));
12975                   return fold_build2_loc (loc, EQ_EXPR, type,
12976                                       fold_convert_loc (loc,
12977                                                         TREE_TYPE (arg1), arg0),
12978                                       arg1);
12979                 case LE_EXPR:
12980                   arg1 = const_binop (PLUS_EXPR, arg1,
12981                                       build_int_cst (TREE_TYPE (arg1), 1));
12982                   return fold_build2_loc (loc, NE_EXPR, type,
12983                                       fold_convert_loc (loc, TREE_TYPE (arg1),
12984                                                         arg0),
12985                                       arg1);
12986                 default:
12987                   break;
12988                 }
12989             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12990                      == min_hi
12991                      && TREE_INT_CST_LOW (arg1) == min_lo)
12992               switch (code)
12993                 {
12994                 case LT_EXPR:
12995                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12996
12997                 case LE_EXPR:
12998                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12999
13000                 case GE_EXPR:
13001                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13002
13003                 case GT_EXPR:
13004                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13005
13006                 default:
13007                   break;
13008                 }
13009             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13010                      == min_hi
13011                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
13012               switch (code)
13013                 {
13014                 case GE_EXPR:
13015                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
13016                   return fold_build2_loc (loc, NE_EXPR, type,
13017                                       fold_convert_loc (loc,
13018                                                         TREE_TYPE (arg1), arg0),
13019                                       arg1);
13020                 case LT_EXPR:
13021                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
13022                   return fold_build2_loc (loc, EQ_EXPR, type,
13023                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13024                                                         arg0),
13025                                       arg1);
13026                 default:
13027                   break;
13028                 }
13029
13030             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13031                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
13032                      && TYPE_UNSIGNED (arg1_type)
13033                      /* We will flip the signedness of the comparison operator
13034                         associated with the mode of arg1, so the sign bit is
13035                         specified by this mode.  Check that arg1 is the signed
13036                         max associated with this sign bit.  */
13037                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13038                      /* signed_type does not work on pointer types.  */
13039                      && INTEGRAL_TYPE_P (arg1_type))
13040               {
13041                 /* The following case also applies to X < signed_max+1
13042                    and X >= signed_max+1 because previous transformations.  */
13043                 if (code == LE_EXPR || code == GT_EXPR)
13044                   {
13045                     tree st;
13046                     st = signed_type_for (TREE_TYPE (arg1));
13047                     return fold_build2_loc (loc,
13048                                         code == LE_EXPR ? GE_EXPR : LT_EXPR,
13049                                         type, fold_convert_loc (loc, st, arg0),
13050                                         build_int_cst (st, 0));
13051                   }
13052               }
13053           }
13054       }
13055
13056       /* If we are comparing an ABS_EXPR with a constant, we can
13057          convert all the cases into explicit comparisons, but they may
13058          well not be faster than doing the ABS and one comparison.
13059          But ABS (X) <= C is a range comparison, which becomes a subtraction
13060          and a comparison, and is probably faster.  */
13061       if (code == LE_EXPR
13062           && TREE_CODE (arg1) == INTEGER_CST
13063           && TREE_CODE (arg0) == ABS_EXPR
13064           && ! TREE_SIDE_EFFECTS (arg0)
13065           && (0 != (tem = negate_expr (arg1)))
13066           && TREE_CODE (tem) == INTEGER_CST
13067           && !TREE_OVERFLOW (tem))
13068         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13069                             build2 (GE_EXPR, type,
13070                                     TREE_OPERAND (arg0, 0), tem),
13071                             build2 (LE_EXPR, type,
13072                                     TREE_OPERAND (arg0, 0), arg1));
13073
13074       /* Convert ABS_EXPR<x> >= 0 to true.  */
13075       strict_overflow_p = false;
13076       if (code == GE_EXPR
13077           && (integer_zerop (arg1)
13078               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13079                   && real_zerop (arg1)))
13080           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13081         {
13082           if (strict_overflow_p)
13083             fold_overflow_warning (("assuming signed overflow does not occur "
13084                                     "when simplifying comparison of "
13085                                     "absolute value and zero"),
13086                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13087           return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13088         }
13089
13090       /* Convert ABS_EXPR<x> < 0 to false.  */
13091       strict_overflow_p = false;
13092       if (code == LT_EXPR
13093           && (integer_zerop (arg1) || real_zerop (arg1))
13094           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13095         {
13096           if (strict_overflow_p)
13097             fold_overflow_warning (("assuming signed overflow does not occur "
13098                                     "when simplifying comparison of "
13099                                     "absolute value and zero"),
13100                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13101           return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13102         }
13103
13104       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13105          and similarly for >= into !=.  */
13106       if ((code == LT_EXPR || code == GE_EXPR)
13107           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13108           && TREE_CODE (arg1) == LSHIFT_EXPR
13109           && integer_onep (TREE_OPERAND (arg1, 0)))
13110         return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13111                            build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13112                                    TREE_OPERAND (arg1, 1)),
13113                            build_int_cst (TREE_TYPE (arg0), 0));
13114
13115       if ((code == LT_EXPR || code == GE_EXPR)
13116           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13117           && CONVERT_EXPR_P (arg1)
13118           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13119           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13120         {
13121           tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13122                         TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
13123           return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13124                              fold_convert_loc (loc, TREE_TYPE (arg0), tem),
13125                              build_int_cst (TREE_TYPE (arg0), 0));
13126         }
13127
13128       return NULL_TREE;
13129
13130     case UNORDERED_EXPR:
13131     case ORDERED_EXPR:
13132     case UNLT_EXPR:
13133     case UNLE_EXPR:
13134     case UNGT_EXPR:
13135     case UNGE_EXPR:
13136     case UNEQ_EXPR:
13137     case LTGT_EXPR:
13138       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13139         {
13140           t1 = fold_relational_const (code, type, arg0, arg1);
13141           if (t1 != NULL_TREE)
13142             return t1;
13143         }
13144
13145       /* If the first operand is NaN, the result is constant.  */
13146       if (TREE_CODE (arg0) == REAL_CST
13147           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13148           && (code != LTGT_EXPR || ! flag_trapping_math))
13149         {
13150           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13151                ? integer_zero_node
13152                : integer_one_node;
13153           return omit_one_operand_loc (loc, type, t1, arg1);
13154         }
13155
13156       /* If the second operand is NaN, the result is constant.  */
13157       if (TREE_CODE (arg1) == REAL_CST
13158           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13159           && (code != LTGT_EXPR || ! flag_trapping_math))
13160         {
13161           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13162                ? integer_zero_node
13163                : integer_one_node;
13164           return omit_one_operand_loc (loc, type, t1, arg0);
13165         }
13166
13167       /* Simplify unordered comparison of something with itself.  */
13168       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13169           && operand_equal_p (arg0, arg1, 0))
13170         return constant_boolean_node (1, type);
13171
13172       if (code == LTGT_EXPR
13173           && !flag_trapping_math
13174           && operand_equal_p (arg0, arg1, 0))
13175         return constant_boolean_node (0, type);
13176
13177       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
13178       {
13179         tree targ0 = strip_float_extensions (arg0);
13180         tree targ1 = strip_float_extensions (arg1);
13181         tree newtype = TREE_TYPE (targ0);
13182
13183         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13184           newtype = TREE_TYPE (targ1);
13185
13186         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13187           return fold_build2_loc (loc, code, type,
13188                               fold_convert_loc (loc, newtype, targ0),
13189                               fold_convert_loc (loc, newtype, targ1));
13190       }
13191
13192       return NULL_TREE;
13193
13194     case COMPOUND_EXPR:
13195       /* When pedantic, a compound expression can be neither an lvalue
13196          nor an integer constant expression.  */
13197       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13198         return NULL_TREE;
13199       /* Don't let (0, 0) be null pointer constant.  */
13200       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13201                                  : fold_convert_loc (loc, type, arg1);
13202       return pedantic_non_lvalue_loc (loc, tem);
13203
13204     case COMPLEX_EXPR:
13205       if ((TREE_CODE (arg0) == REAL_CST
13206            && TREE_CODE (arg1) == REAL_CST)
13207           || (TREE_CODE (arg0) == INTEGER_CST
13208               && TREE_CODE (arg1) == INTEGER_CST))
13209         return build_complex (type, arg0, arg1);
13210       if (TREE_CODE (arg0) == REALPART_EXPR
13211           && TREE_CODE (arg1) == IMAGPART_EXPR
13212           && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg0, 0)))
13213               == TYPE_MAIN_VARIANT (type))
13214           && operand_equal_p (TREE_OPERAND (arg0, 0),
13215                               TREE_OPERAND (arg1, 0), 0))
13216         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
13217                                      TREE_OPERAND (arg1, 0));
13218       return NULL_TREE;
13219
13220     case ASSERT_EXPR:
13221       /* An ASSERT_EXPR should never be passed to fold_binary.  */
13222       gcc_unreachable ();
13223
13224     default:
13225       return NULL_TREE;
13226     } /* switch (code) */
13227 }
13228
13229 /* Callback for walk_tree, looking for LABEL_EXPR.  Return *TP if it is
13230    a LABEL_EXPR; otherwise return NULL_TREE.  Do not check the subtrees
13231    of GOTO_EXPR.  */
13232
13233 static tree
13234 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13235 {
13236   switch (TREE_CODE (*tp))
13237     {
13238     case LABEL_EXPR:
13239       return *tp;
13240
13241     case GOTO_EXPR:
13242       *walk_subtrees = 0;
13243
13244       /* ... fall through ...  */
13245
13246     default:
13247       return NULL_TREE;
13248     }
13249 }
13250
13251 /* Return whether the sub-tree ST contains a label which is accessible from
13252    outside the sub-tree.  */
13253
13254 static bool
13255 contains_label_p (tree st)
13256 {
13257   return
13258    (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13259 }
13260
13261 /* Fold a ternary expression of code CODE and type TYPE with operands
13262    OP0, OP1, and OP2.  Return the folded expression if folding is
13263    successful.  Otherwise, return NULL_TREE.  */
13264
13265 tree
13266 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13267                   tree op0, tree op1, tree op2)
13268 {
13269   tree tem;
13270   tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
13271   enum tree_code_class kind = TREE_CODE_CLASS (code);
13272
13273   gcc_assert (IS_EXPR_CODE_CLASS (kind)
13274               && TREE_CODE_LENGTH (code) == 3);
13275
13276   /* Strip any conversions that don't change the mode.  This is safe
13277      for every expression, except for a comparison expression because
13278      its signedness is derived from its operands.  So, in the latter
13279      case, only strip conversions that don't change the signedness.
13280
13281      Note that this is done as an internal manipulation within the
13282      constant folder, in order to find the simplest representation of
13283      the arguments so that their form can be studied.  In any cases,
13284      the appropriate type conversions should be put back in the tree
13285      that will get out of the constant folder.  */
13286   if (op0)
13287     {
13288       arg0 = op0;
13289       STRIP_NOPS (arg0);
13290     }
13291
13292   if (op1)
13293     {
13294       arg1 = op1;
13295       STRIP_NOPS (arg1);
13296     }
13297
13298   if (op2)
13299     {
13300       arg2 = op2;
13301       STRIP_NOPS (arg2);
13302     }
13303
13304   switch (code)
13305     {
13306     case COMPONENT_REF:
13307       if (TREE_CODE (arg0) == CONSTRUCTOR
13308           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13309         {
13310           unsigned HOST_WIDE_INT idx;
13311           tree field, value;
13312           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13313             if (field == arg1)
13314               return value;
13315         }
13316       return NULL_TREE;
13317
13318     case COND_EXPR:
13319       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13320          so all simple results must be passed through pedantic_non_lvalue.  */
13321       if (TREE_CODE (arg0) == INTEGER_CST)
13322         {
13323           tree unused_op = integer_zerop (arg0) ? op1 : op2;
13324           tem = integer_zerop (arg0) ? op2 : op1;
13325           /* Only optimize constant conditions when the selected branch
13326              has the same type as the COND_EXPR.  This avoids optimizing
13327              away "c ? x : throw", where the throw has a void type.
13328              Avoid throwing away that operand which contains label.  */
13329           if ((!TREE_SIDE_EFFECTS (unused_op)
13330                || !contains_label_p (unused_op))
13331               && (! VOID_TYPE_P (TREE_TYPE (tem))
13332                   || VOID_TYPE_P (type)))
13333             return pedantic_non_lvalue_loc (loc, tem);
13334           return NULL_TREE;
13335         }
13336       if (operand_equal_p (arg1, op2, 0))
13337         return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13338
13339       /* If we have A op B ? A : C, we may be able to convert this to a
13340          simpler expression, depending on the operation and the values
13341          of B and C.  Signed zeros prevent all of these transformations,
13342          for reasons given above each one.
13343
13344          Also try swapping the arguments and inverting the conditional.  */
13345       if (COMPARISON_CLASS_P (arg0)
13346           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13347                                              arg1, TREE_OPERAND (arg0, 1))
13348           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13349         {
13350           tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13351           if (tem)
13352             return tem;
13353         }
13354
13355       if (COMPARISON_CLASS_P (arg0)
13356           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13357                                              op2,
13358                                              TREE_OPERAND (arg0, 1))
13359           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13360         {
13361           location_t loc0 = expr_location_or (arg0, loc);
13362           tem = fold_truth_not_expr (loc0, arg0);
13363           if (tem && COMPARISON_CLASS_P (tem))
13364             {
13365               tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13366               if (tem)
13367                 return tem;
13368             }
13369         }
13370
13371       /* If the second operand is simpler than the third, swap them
13372          since that produces better jump optimization results.  */
13373       if (truth_value_p (TREE_CODE (arg0))
13374           && tree_swap_operands_p (op1, op2, false))
13375         {
13376           location_t loc0 = expr_location_or (arg0, loc);
13377           /* See if this can be inverted.  If it can't, possibly because
13378              it was a floating-point inequality comparison, don't do
13379              anything.  */
13380           tem = fold_truth_not_expr (loc0, arg0);
13381           if (tem)
13382             return fold_build3_loc (loc, code, type, tem, op2, op1);
13383         }
13384
13385       /* Convert A ? 1 : 0 to simply A.  */
13386       if (integer_onep (op1)
13387           && integer_zerop (op2)
13388           /* If we try to convert OP0 to our type, the
13389              call to fold will try to move the conversion inside
13390              a COND, which will recurse.  In that case, the COND_EXPR
13391              is probably the best choice, so leave it alone.  */
13392           && type == TREE_TYPE (arg0))
13393         return pedantic_non_lvalue_loc (loc, arg0);
13394
13395       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
13396          over COND_EXPR in cases such as floating point comparisons.  */
13397       if (integer_zerop (op1)
13398           && integer_onep (op2)
13399           && truth_value_p (TREE_CODE (arg0)))
13400         return pedantic_non_lvalue_loc (loc,
13401                                     fold_convert_loc (loc, type,
13402                                               invert_truthvalue_loc (loc,
13403                                                                      arg0)));
13404
13405       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
13406       if (TREE_CODE (arg0) == LT_EXPR
13407           && integer_zerop (TREE_OPERAND (arg0, 1))
13408           && integer_zerop (op2)
13409           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13410         {
13411           /* sign_bit_p only checks ARG1 bits within A's precision.
13412              If <sign bit of A> has wider type than A, bits outside
13413              of A's precision in <sign bit of A> need to be checked.
13414              If they are all 0, this optimization needs to be done
13415              in unsigned A's type, if they are all 1 in signed A's type,
13416              otherwise this can't be done.  */
13417           if (TYPE_PRECISION (TREE_TYPE (tem))
13418               < TYPE_PRECISION (TREE_TYPE (arg1))
13419               && TYPE_PRECISION (TREE_TYPE (tem))
13420                  < TYPE_PRECISION (type))
13421             {
13422               unsigned HOST_WIDE_INT mask_lo;
13423               HOST_WIDE_INT mask_hi;
13424               int inner_width, outer_width;
13425               tree tem_type;
13426
13427               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13428               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13429               if (outer_width > TYPE_PRECISION (type))
13430                 outer_width = TYPE_PRECISION (type);
13431
13432               if (outer_width > HOST_BITS_PER_WIDE_INT)
13433                 {
13434                   mask_hi = ((unsigned HOST_WIDE_INT) -1
13435                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13436                   mask_lo = -1;
13437                 }
13438               else
13439                 {
13440                   mask_hi = 0;
13441                   mask_lo = ((unsigned HOST_WIDE_INT) -1
13442                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
13443                 }
13444               if (inner_width > HOST_BITS_PER_WIDE_INT)
13445                 {
13446                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13447                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
13448                   mask_lo = 0;
13449                 }
13450               else
13451                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13452                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
13453
13454               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13455                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13456                 {
13457                   tem_type = signed_type_for (TREE_TYPE (tem));
13458                   tem = fold_convert_loc (loc, tem_type, tem);
13459                 }
13460               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13461                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13462                 {
13463                   tem_type = unsigned_type_for (TREE_TYPE (tem));
13464                   tem = fold_convert_loc (loc, tem_type, tem);
13465                 }
13466               else
13467                 tem = NULL;
13468             }
13469
13470           if (tem)
13471             return
13472               fold_convert_loc (loc, type,
13473                                 fold_build2_loc (loc, BIT_AND_EXPR,
13474                                              TREE_TYPE (tem), tem,
13475                                              fold_convert_loc (loc,
13476                                                                TREE_TYPE (tem),
13477                                                                arg1)));
13478         }
13479
13480       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
13481          already handled above.  */
13482       if (TREE_CODE (arg0) == BIT_AND_EXPR
13483           && integer_onep (TREE_OPERAND (arg0, 1))
13484           && integer_zerop (op2)
13485           && integer_pow2p (arg1))
13486         {
13487           tree tem = TREE_OPERAND (arg0, 0);
13488           STRIP_NOPS (tem);
13489           if (TREE_CODE (tem) == RSHIFT_EXPR
13490               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13491               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13492                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13493             return fold_build2_loc (loc, BIT_AND_EXPR, type,
13494                                 TREE_OPERAND (tem, 0), arg1);
13495         }
13496
13497       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13498          is probably obsolete because the first operand should be a
13499          truth value (that's why we have the two cases above), but let's
13500          leave it in until we can confirm this for all front-ends.  */
13501       if (integer_zerop (op2)
13502           && TREE_CODE (arg0) == NE_EXPR
13503           && integer_zerop (TREE_OPERAND (arg0, 1))
13504           && integer_pow2p (arg1)
13505           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13506           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13507                               arg1, OEP_ONLY_CONST))
13508         return pedantic_non_lvalue_loc (loc,
13509                                     fold_convert_loc (loc, type,
13510                                                       TREE_OPERAND (arg0, 0)));
13511
13512       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13513       if (integer_zerop (op2)
13514           && truth_value_p (TREE_CODE (arg0))
13515           && truth_value_p (TREE_CODE (arg1)))
13516         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13517                             fold_convert_loc (loc, type, arg0),
13518                             arg1);
13519
13520       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13521       if (integer_onep (op2)
13522           && truth_value_p (TREE_CODE (arg0))
13523           && truth_value_p (TREE_CODE (arg1)))
13524         {
13525           location_t loc0 = expr_location_or (arg0, loc);
13526           /* Only perform transformation if ARG0 is easily inverted.  */
13527           tem = fold_truth_not_expr (loc0, arg0);
13528           if (tem)
13529             return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13530                                 fold_convert_loc (loc, type, tem),
13531                                 arg1);
13532         }
13533
13534       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13535       if (integer_zerop (arg1)
13536           && truth_value_p (TREE_CODE (arg0))
13537           && truth_value_p (TREE_CODE (op2)))
13538         {
13539           location_t loc0 = expr_location_or (arg0, loc);
13540           /* Only perform transformation if ARG0 is easily inverted.  */
13541           tem = fold_truth_not_expr (loc0, arg0);
13542           if (tem)
13543             return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13544                                 fold_convert_loc (loc, type, tem),
13545                                 op2);
13546         }
13547
13548       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13549       if (integer_onep (arg1)
13550           && truth_value_p (TREE_CODE (arg0))
13551           && truth_value_p (TREE_CODE (op2)))
13552         return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13553                             fold_convert_loc (loc, type, arg0),
13554                             op2);
13555
13556       return NULL_TREE;
13557
13558     case CALL_EXPR:
13559       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
13560          of fold_ternary on them.  */
13561       gcc_unreachable ();
13562
13563     case BIT_FIELD_REF:
13564       if ((TREE_CODE (arg0) == VECTOR_CST
13565            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13566           && type == TREE_TYPE (TREE_TYPE (arg0)))
13567         {
13568           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13569           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13570
13571           if (width != 0
13572               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13573               && (idx % width) == 0
13574               && (idx = idx / width)
13575                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13576             {
13577               tree elements = NULL_TREE;
13578
13579               if (TREE_CODE (arg0) == VECTOR_CST)
13580                 elements = TREE_VECTOR_CST_ELTS (arg0);
13581               else
13582                 {
13583                   unsigned HOST_WIDE_INT idx;
13584                   tree value;
13585
13586                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13587                     elements = tree_cons (NULL_TREE, value, elements);
13588                 }
13589               while (idx-- > 0 && elements)
13590                 elements = TREE_CHAIN (elements);
13591               if (elements)
13592                 return TREE_VALUE (elements);
13593               else
13594                 return build_zero_cst (type);
13595             }
13596         }
13597
13598       /* A bit-field-ref that referenced the full argument can be stripped.  */
13599       if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13600           && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13601           && integer_zerop (op2))
13602         return fold_convert_loc (loc, type, arg0);
13603
13604       return NULL_TREE;
13605
13606     case FMA_EXPR:
13607       /* For integers we can decompose the FMA if possible.  */
13608       if (TREE_CODE (arg0) == INTEGER_CST
13609           && TREE_CODE (arg1) == INTEGER_CST)
13610         return fold_build2_loc (loc, PLUS_EXPR, type,
13611                                 const_binop (MULT_EXPR, arg0, arg1), arg2);
13612       if (integer_zerop (arg2))
13613         return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
13614
13615       return fold_fma (loc, type, arg0, arg1, arg2);
13616
13617     default:
13618       return NULL_TREE;
13619     } /* switch (code) */
13620 }
13621
13622 /* Perform constant folding and related simplification of EXPR.
13623    The related simplifications include x*1 => x, x*0 => 0, etc.,
13624    and application of the associative law.
13625    NOP_EXPR conversions may be removed freely (as long as we
13626    are careful not to change the type of the overall expression).
13627    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13628    but we can constant-fold them if they have constant operands.  */
13629
13630 #ifdef ENABLE_FOLD_CHECKING
13631 # define fold(x) fold_1 (x)
13632 static tree fold_1 (tree);
13633 static
13634 #endif
13635 tree
13636 fold (tree expr)
13637 {
13638   const tree t = expr;
13639   enum tree_code code = TREE_CODE (t);
13640   enum tree_code_class kind = TREE_CODE_CLASS (code);
13641   tree tem;
13642   location_t loc = EXPR_LOCATION (expr);
13643
13644   /* Return right away if a constant.  */
13645   if (kind == tcc_constant)
13646     return t;
13647
13648   /* CALL_EXPR-like objects with variable numbers of operands are
13649      treated specially.  */
13650   if (kind == tcc_vl_exp)
13651     {
13652       if (code == CALL_EXPR)
13653         {
13654           tem = fold_call_expr (loc, expr, false);
13655           return tem ? tem : expr;
13656         }
13657       return expr;
13658     }
13659
13660   if (IS_EXPR_CODE_CLASS (kind))
13661     {
13662       tree type = TREE_TYPE (t);
13663       tree op0, op1, op2;
13664
13665       switch (TREE_CODE_LENGTH (code))
13666         {
13667         case 1:
13668           op0 = TREE_OPERAND (t, 0);
13669           tem = fold_unary_loc (loc, code, type, op0);
13670           return tem ? tem : expr;
13671         case 2:
13672           op0 = TREE_OPERAND (t, 0);
13673           op1 = TREE_OPERAND (t, 1);
13674           tem = fold_binary_loc (loc, code, type, op0, op1);
13675           return tem ? tem : expr;
13676         case 3:
13677           op0 = TREE_OPERAND (t, 0);
13678           op1 = TREE_OPERAND (t, 1);
13679           op2 = TREE_OPERAND (t, 2);
13680           tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13681           return tem ? tem : expr;
13682         default:
13683           break;
13684         }
13685     }
13686
13687   switch (code)
13688     {
13689     case ARRAY_REF:
13690       {
13691         tree op0 = TREE_OPERAND (t, 0);
13692         tree op1 = TREE_OPERAND (t, 1);
13693
13694         if (TREE_CODE (op1) == INTEGER_CST
13695             && TREE_CODE (op0) == CONSTRUCTOR
13696             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13697           {
13698             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13699             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13700             unsigned HOST_WIDE_INT begin = 0;
13701
13702             /* Find a matching index by means of a binary search.  */
13703             while (begin != end)
13704               {
13705                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13706                 tree index = VEC_index (constructor_elt, elts, middle)->index;
13707
13708                 if (TREE_CODE (index) == INTEGER_CST
13709                     && tree_int_cst_lt (index, op1))
13710                   begin = middle + 1;
13711                 else if (TREE_CODE (index) == INTEGER_CST
13712                          && tree_int_cst_lt (op1, index))
13713                   end = middle;
13714                 else if (TREE_CODE (index) == RANGE_EXPR
13715                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13716                   begin = middle + 1;
13717                 else if (TREE_CODE (index) == RANGE_EXPR
13718                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13719                   end = middle;
13720                 else
13721                   return VEC_index (constructor_elt, elts, middle)->value;
13722               }
13723           }
13724
13725         return t;
13726       }
13727
13728     case CONST_DECL:
13729       return fold (DECL_INITIAL (t));
13730
13731     default:
13732       return t;
13733     } /* switch (code) */
13734 }
13735
13736 #ifdef ENABLE_FOLD_CHECKING
13737 #undef fold
13738
13739 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13740 static void fold_check_failed (const_tree, const_tree);
13741 void print_fold_checksum (const_tree);
13742
13743 /* When --enable-checking=fold, compute a digest of expr before
13744    and after actual fold call to see if fold did not accidentally
13745    change original expr.  */
13746
13747 tree
13748 fold (tree expr)
13749 {
13750   tree ret;
13751   struct md5_ctx ctx;
13752   unsigned char checksum_before[16], checksum_after[16];
13753   htab_t ht;
13754
13755   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13756   md5_init_ctx (&ctx);
13757   fold_checksum_tree (expr, &ctx, ht);
13758   md5_finish_ctx (&ctx, checksum_before);
13759   htab_empty (ht);
13760
13761   ret = fold_1 (expr);
13762
13763   md5_init_ctx (&ctx);
13764   fold_checksum_tree (expr, &ctx, ht);
13765   md5_finish_ctx (&ctx, checksum_after);
13766   htab_delete (ht);
13767
13768   if (memcmp (checksum_before, checksum_after, 16))
13769     fold_check_failed (expr, ret);
13770
13771   return ret;
13772 }
13773
13774 void
13775 print_fold_checksum (const_tree expr)
13776 {
13777   struct md5_ctx ctx;
13778   unsigned char checksum[16], cnt;
13779   htab_t ht;
13780
13781   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13782   md5_init_ctx (&ctx);
13783   fold_checksum_tree (expr, &ctx, ht);
13784   md5_finish_ctx (&ctx, checksum);
13785   htab_delete (ht);
13786   for (cnt = 0; cnt < 16; ++cnt)
13787     fprintf (stderr, "%02x", checksum[cnt]);
13788   putc ('\n', stderr);
13789 }
13790
13791 static void
13792 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13793 {
13794   internal_error ("fold check: original tree changed by fold");
13795 }
13796
13797 static void
13798 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13799 {
13800   void **slot;
13801   enum tree_code code;
13802   union tree_node buf;
13803   int i, len;
13804
13805 recursive_label:
13806
13807   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13808                <= sizeof (struct tree_function_decl))
13809               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13810   if (expr == NULL)
13811     return;
13812   slot = (void **) htab_find_slot (ht, expr, INSERT);
13813   if (*slot != NULL)
13814     return;
13815   *slot = CONST_CAST_TREE (expr);
13816   code = TREE_CODE (expr);
13817   if (TREE_CODE_CLASS (code) == tcc_declaration
13818       && DECL_ASSEMBLER_NAME_SET_P (expr))
13819     {
13820       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
13821       memcpy ((char *) &buf, expr, tree_size (expr));
13822       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13823       expr = (tree) &buf;
13824     }
13825   else if (TREE_CODE_CLASS (code) == tcc_type
13826            && (TYPE_POINTER_TO (expr)
13827                || TYPE_REFERENCE_TO (expr)
13828                || TYPE_CACHED_VALUES_P (expr)
13829                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13830                || TYPE_NEXT_VARIANT (expr)))
13831     {
13832       /* Allow these fields to be modified.  */
13833       tree tmp;
13834       memcpy ((char *) &buf, expr, tree_size (expr));
13835       expr = tmp = (tree) &buf;
13836       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13837       TYPE_POINTER_TO (tmp) = NULL;
13838       TYPE_REFERENCE_TO (tmp) = NULL;
13839       TYPE_NEXT_VARIANT (tmp) = NULL;
13840       if (TYPE_CACHED_VALUES_P (tmp))
13841         {
13842           TYPE_CACHED_VALUES_P (tmp) = 0;
13843           TYPE_CACHED_VALUES (tmp) = NULL;
13844         }
13845     }
13846   md5_process_bytes (expr, tree_size (expr), ctx);
13847   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13848   if (TREE_CODE_CLASS (code) != tcc_type
13849       && TREE_CODE_CLASS (code) != tcc_declaration
13850       && code != TREE_LIST
13851       && code != SSA_NAME
13852       && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13853     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13854   switch (TREE_CODE_CLASS (code))
13855     {
13856     case tcc_constant:
13857       switch (code)
13858         {
13859         case STRING_CST:
13860           md5_process_bytes (TREE_STRING_POINTER (expr),
13861                              TREE_STRING_LENGTH (expr), ctx);
13862           break;
13863         case COMPLEX_CST:
13864           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13865           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13866           break;
13867         case VECTOR_CST:
13868           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13869           break;
13870         default:
13871           break;
13872         }
13873       break;
13874     case tcc_exceptional:
13875       switch (code)
13876         {
13877         case TREE_LIST:
13878           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13879           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13880           expr = TREE_CHAIN (expr);
13881           goto recursive_label;
13882           break;
13883         case TREE_VEC:
13884           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13885             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13886           break;
13887         default:
13888           break;
13889         }
13890       break;
13891     case tcc_expression:
13892     case tcc_reference:
13893     case tcc_comparison:
13894     case tcc_unary:
13895     case tcc_binary:
13896     case tcc_statement:
13897     case tcc_vl_exp:
13898       len = TREE_OPERAND_LENGTH (expr);
13899       for (i = 0; i < len; ++i)
13900         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13901       break;
13902     case tcc_declaration:
13903       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13904       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13905       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13906         {
13907           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13908           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13909           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13910           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13911           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13912         }
13913       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13914         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13915
13916       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13917         {
13918           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13919           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13920           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13921         }
13922       break;
13923     case tcc_type:
13924       if (TREE_CODE (expr) == ENUMERAL_TYPE)
13925         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13926       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13927       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13928       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13929       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13930       if (INTEGRAL_TYPE_P (expr)
13931           || SCALAR_FLOAT_TYPE_P (expr))
13932         {
13933           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13934           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13935         }
13936       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13937       if (TREE_CODE (expr) == RECORD_TYPE
13938           || TREE_CODE (expr) == UNION_TYPE
13939           || TREE_CODE (expr) == QUAL_UNION_TYPE)
13940         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13941       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13942       break;
13943     default:
13944       break;
13945     }
13946 }
13947
13948 /* Helper function for outputting the checksum of a tree T.  When
13949    debugging with gdb, you can "define mynext" to be "next" followed
13950    by "call debug_fold_checksum (op0)", then just trace down till the
13951    outputs differ.  */
13952
13953 DEBUG_FUNCTION void
13954 debug_fold_checksum (const_tree t)
13955 {
13956   int i;
13957   unsigned char checksum[16];
13958   struct md5_ctx ctx;
13959   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13960
13961   md5_init_ctx (&ctx);
13962   fold_checksum_tree (t, &ctx, ht);
13963   md5_finish_ctx (&ctx, checksum);
13964   htab_empty (ht);
13965
13966   for (i = 0; i < 16; i++)
13967     fprintf (stderr, "%d ", checksum[i]);
13968
13969   fprintf (stderr, "\n");
13970 }
13971
13972 #endif
13973
13974 /* Fold a unary tree expression with code CODE of type TYPE with an
13975    operand OP0.  LOC is the location of the resulting expression.
13976    Return a folded expression if successful.  Otherwise, return a tree
13977    expression with code CODE of type TYPE with an operand OP0.  */
13978
13979 tree
13980 fold_build1_stat_loc (location_t loc,
13981                       enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13982 {
13983   tree tem;
13984 #ifdef ENABLE_FOLD_CHECKING
13985   unsigned char checksum_before[16], checksum_after[16];
13986   struct md5_ctx ctx;
13987   htab_t ht;
13988
13989   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13990   md5_init_ctx (&ctx);
13991   fold_checksum_tree (op0, &ctx, ht);
13992   md5_finish_ctx (&ctx, checksum_before);
13993   htab_empty (ht);
13994 #endif
13995
13996   tem = fold_unary_loc (loc, code, type, op0);
13997   if (!tem)
13998     tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
13999
14000 #ifdef ENABLE_FOLD_CHECKING
14001   md5_init_ctx (&ctx);
14002   fold_checksum_tree (op0, &ctx, ht);
14003   md5_finish_ctx (&ctx, checksum_after);
14004   htab_delete (ht);
14005
14006   if (memcmp (checksum_before, checksum_after, 16))
14007     fold_check_failed (op0, tem);
14008 #endif
14009   return tem;
14010 }
14011
14012 /* Fold a binary tree expression with code CODE of type TYPE with
14013    operands OP0 and OP1.  LOC is the location of the resulting
14014    expression.  Return a folded expression if successful.  Otherwise,
14015    return a tree expression with code CODE of type TYPE with operands
14016    OP0 and OP1.  */
14017
14018 tree
14019 fold_build2_stat_loc (location_t loc,
14020                       enum tree_code code, tree type, tree op0, tree op1
14021                       MEM_STAT_DECL)
14022 {
14023   tree tem;
14024 #ifdef ENABLE_FOLD_CHECKING
14025   unsigned char checksum_before_op0[16],
14026                 checksum_before_op1[16],
14027                 checksum_after_op0[16],
14028                 checksum_after_op1[16];
14029   struct md5_ctx ctx;
14030   htab_t ht;
14031
14032   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14033   md5_init_ctx (&ctx);
14034   fold_checksum_tree (op0, &ctx, ht);
14035   md5_finish_ctx (&ctx, checksum_before_op0);
14036   htab_empty (ht);
14037
14038   md5_init_ctx (&ctx);
14039   fold_checksum_tree (op1, &ctx, ht);
14040   md5_finish_ctx (&ctx, checksum_before_op1);
14041   htab_empty (ht);
14042 #endif
14043
14044   tem = fold_binary_loc (loc, code, type, op0, op1);
14045   if (!tem)
14046     tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
14047
14048 #ifdef ENABLE_FOLD_CHECKING
14049   md5_init_ctx (&ctx);
14050   fold_checksum_tree (op0, &ctx, ht);
14051   md5_finish_ctx (&ctx, checksum_after_op0);
14052   htab_empty (ht);
14053
14054   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14055     fold_check_failed (op0, tem);
14056
14057   md5_init_ctx (&ctx);
14058   fold_checksum_tree (op1, &ctx, ht);
14059   md5_finish_ctx (&ctx, checksum_after_op1);
14060   htab_delete (ht);
14061
14062   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14063     fold_check_failed (op1, tem);
14064 #endif
14065   return tem;
14066 }
14067
14068 /* Fold a ternary tree expression with code CODE of type TYPE with
14069    operands OP0, OP1, and OP2.  Return a folded expression if
14070    successful.  Otherwise, return a tree expression with code CODE of
14071    type TYPE with operands OP0, OP1, and OP2.  */
14072
14073 tree
14074 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14075                       tree op0, tree op1, tree op2 MEM_STAT_DECL)
14076 {
14077   tree tem;
14078 #ifdef ENABLE_FOLD_CHECKING
14079   unsigned char checksum_before_op0[16],
14080                 checksum_before_op1[16],
14081                 checksum_before_op2[16],
14082                 checksum_after_op0[16],
14083                 checksum_after_op1[16],
14084                 checksum_after_op2[16];
14085   struct md5_ctx ctx;
14086   htab_t ht;
14087
14088   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14089   md5_init_ctx (&ctx);
14090   fold_checksum_tree (op0, &ctx, ht);
14091   md5_finish_ctx (&ctx, checksum_before_op0);
14092   htab_empty (ht);
14093
14094   md5_init_ctx (&ctx);
14095   fold_checksum_tree (op1, &ctx, ht);
14096   md5_finish_ctx (&ctx, checksum_before_op1);
14097   htab_empty (ht);
14098
14099   md5_init_ctx (&ctx);
14100   fold_checksum_tree (op2, &ctx, ht);
14101   md5_finish_ctx (&ctx, checksum_before_op2);
14102   htab_empty (ht);
14103 #endif
14104
14105   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14106   tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14107   if (!tem)
14108     tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
14109
14110 #ifdef ENABLE_FOLD_CHECKING
14111   md5_init_ctx (&ctx);
14112   fold_checksum_tree (op0, &ctx, ht);
14113   md5_finish_ctx (&ctx, checksum_after_op0);
14114   htab_empty (ht);
14115
14116   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14117     fold_check_failed (op0, tem);
14118
14119   md5_init_ctx (&ctx);
14120   fold_checksum_tree (op1, &ctx, ht);
14121   md5_finish_ctx (&ctx, checksum_after_op1);
14122   htab_empty (ht);
14123
14124   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14125     fold_check_failed (op1, tem);
14126
14127   md5_init_ctx (&ctx);
14128   fold_checksum_tree (op2, &ctx, ht);
14129   md5_finish_ctx (&ctx, checksum_after_op2);
14130   htab_delete (ht);
14131
14132   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14133     fold_check_failed (op2, tem);
14134 #endif
14135   return tem;
14136 }
14137
14138 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14139    arguments in ARGARRAY, and a null static chain.
14140    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
14141    of type TYPE from the given operands as constructed by build_call_array.  */
14142
14143 tree
14144 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14145                            int nargs, tree *argarray)
14146 {
14147   tree tem;
14148 #ifdef ENABLE_FOLD_CHECKING
14149   unsigned char checksum_before_fn[16],
14150                 checksum_before_arglist[16],
14151                 checksum_after_fn[16],
14152                 checksum_after_arglist[16];
14153   struct md5_ctx ctx;
14154   htab_t ht;
14155   int i;
14156
14157   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14158   md5_init_ctx (&ctx);
14159   fold_checksum_tree (fn, &ctx, ht);
14160   md5_finish_ctx (&ctx, checksum_before_fn);
14161   htab_empty (ht);
14162
14163   md5_init_ctx (&ctx);
14164   for (i = 0; i < nargs; i++)
14165     fold_checksum_tree (argarray[i], &ctx, ht);
14166   md5_finish_ctx (&ctx, checksum_before_arglist);
14167   htab_empty (ht);
14168 #endif
14169
14170   tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14171
14172 #ifdef ENABLE_FOLD_CHECKING
14173   md5_init_ctx (&ctx);
14174   fold_checksum_tree (fn, &ctx, ht);
14175   md5_finish_ctx (&ctx, checksum_after_fn);
14176   htab_empty (ht);
14177
14178   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14179     fold_check_failed (fn, tem);
14180
14181   md5_init_ctx (&ctx);
14182   for (i = 0; i < nargs; i++)
14183     fold_checksum_tree (argarray[i], &ctx, ht);
14184   md5_finish_ctx (&ctx, checksum_after_arglist);
14185   htab_delete (ht);
14186
14187   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14188     fold_check_failed (NULL_TREE, tem);
14189 #endif
14190   return tem;
14191 }
14192
14193 /* Perform constant folding and related simplification of initializer
14194    expression EXPR.  These behave identically to "fold_buildN" but ignore
14195    potential run-time traps and exceptions that fold must preserve.  */
14196
14197 #define START_FOLD_INIT \
14198   int saved_signaling_nans = flag_signaling_nans;\
14199   int saved_trapping_math = flag_trapping_math;\
14200   int saved_rounding_math = flag_rounding_math;\
14201   int saved_trapv = flag_trapv;\
14202   int saved_folding_initializer = folding_initializer;\
14203   flag_signaling_nans = 0;\
14204   flag_trapping_math = 0;\
14205   flag_rounding_math = 0;\
14206   flag_trapv = 0;\
14207   folding_initializer = 1;
14208
14209 #define END_FOLD_INIT \
14210   flag_signaling_nans = saved_signaling_nans;\
14211   flag_trapping_math = saved_trapping_math;\
14212   flag_rounding_math = saved_rounding_math;\
14213   flag_trapv = saved_trapv;\
14214   folding_initializer = saved_folding_initializer;
14215
14216 tree
14217 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14218                              tree type, tree op)
14219 {
14220   tree result;
14221   START_FOLD_INIT;
14222
14223   result = fold_build1_loc (loc, code, type, op);
14224
14225   END_FOLD_INIT;
14226   return result;
14227 }
14228
14229 tree
14230 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14231                              tree type, tree op0, tree op1)
14232 {
14233   tree result;
14234   START_FOLD_INIT;
14235
14236   result = fold_build2_loc (loc, code, type, op0, op1);
14237
14238   END_FOLD_INIT;
14239   return result;
14240 }
14241
14242 tree
14243 fold_build3_initializer_loc (location_t loc, enum tree_code code,
14244                              tree type, tree op0, tree op1, tree op2)
14245 {
14246   tree result;
14247   START_FOLD_INIT;
14248
14249   result = fold_build3_loc (loc, code, type, op0, op1, op2);
14250
14251   END_FOLD_INIT;
14252   return result;
14253 }
14254
14255 tree
14256 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14257                                        int nargs, tree *argarray)
14258 {
14259   tree result;
14260   START_FOLD_INIT;
14261
14262   result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14263
14264   END_FOLD_INIT;
14265   return result;
14266 }
14267
14268 #undef START_FOLD_INIT
14269 #undef END_FOLD_INIT
14270
14271 /* Determine if first argument is a multiple of second argument.  Return 0 if
14272    it is not, or we cannot easily determined it to be.
14273
14274    An example of the sort of thing we care about (at this point; this routine
14275    could surely be made more general, and expanded to do what the *_DIV_EXPR's
14276    fold cases do now) is discovering that
14277
14278      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14279
14280    is a multiple of
14281
14282      SAVE_EXPR (J * 8)
14283
14284    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14285
14286    This code also handles discovering that
14287
14288      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14289
14290    is a multiple of 8 so we don't have to worry about dealing with a
14291    possible remainder.
14292
14293    Note that we *look* inside a SAVE_EXPR only to determine how it was
14294    calculated; it is not safe for fold to do much of anything else with the
14295    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14296    at run time.  For example, the latter example above *cannot* be implemented
14297    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14298    evaluation time of the original SAVE_EXPR is not necessarily the same at
14299    the time the new expression is evaluated.  The only optimization of this
14300    sort that would be valid is changing
14301
14302      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14303
14304    divided by 8 to
14305
14306      SAVE_EXPR (I) * SAVE_EXPR (J)
14307
14308    (where the same SAVE_EXPR (J) is used in the original and the
14309    transformed version).  */
14310
14311 int
14312 multiple_of_p (tree type, const_tree top, const_tree bottom)
14313 {
14314   if (operand_equal_p (top, bottom, 0))
14315     return 1;
14316
14317   if (TREE_CODE (type) != INTEGER_TYPE)
14318     return 0;
14319
14320   switch (TREE_CODE (top))
14321     {
14322     case BIT_AND_EXPR:
14323       /* Bitwise and provides a power of two multiple.  If the mask is
14324          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
14325       if (!integer_pow2p (bottom))
14326         return 0;
14327       /* FALLTHRU */
14328
14329     case MULT_EXPR:
14330       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14331               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14332
14333     case PLUS_EXPR:
14334     case MINUS_EXPR:
14335       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14336               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14337
14338     case LSHIFT_EXPR:
14339       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14340         {
14341           tree op1, t1;
14342
14343           op1 = TREE_OPERAND (top, 1);
14344           /* const_binop may not detect overflow correctly,
14345              so check for it explicitly here.  */
14346           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14347               > TREE_INT_CST_LOW (op1)
14348               && TREE_INT_CST_HIGH (op1) == 0
14349               && 0 != (t1 = fold_convert (type,
14350                                           const_binop (LSHIFT_EXPR,
14351                                                        size_one_node,
14352                                                        op1)))
14353               && !TREE_OVERFLOW (t1))
14354             return multiple_of_p (type, t1, bottom);
14355         }
14356       return 0;
14357
14358     case NOP_EXPR:
14359       /* Can't handle conversions from non-integral or wider integral type.  */
14360       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14361           || (TYPE_PRECISION (type)
14362               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14363         return 0;
14364
14365       /* .. fall through ...  */
14366
14367     case SAVE_EXPR:
14368       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14369
14370     case COND_EXPR:
14371       return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
14372               && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
14373
14374     case INTEGER_CST:
14375       if (TREE_CODE (bottom) != INTEGER_CST
14376           || integer_zerop (bottom)
14377           || (TYPE_UNSIGNED (type)
14378               && (tree_int_cst_sgn (top) < 0
14379                   || tree_int_cst_sgn (bottom) < 0)))
14380         return 0;
14381       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14382                                              top, bottom));
14383
14384     default:
14385       return 0;
14386     }
14387 }
14388
14389 /* Return true if CODE or TYPE is known to be non-negative. */
14390
14391 static bool
14392 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14393 {
14394   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14395       && truth_value_p (code))
14396     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14397        have a signed:1 type (where the value is -1 and 0).  */
14398     return true;
14399   return false;
14400 }
14401
14402 /* Return true if (CODE OP0) is known to be non-negative.  If the return
14403    value is based on the assumption that signed overflow is undefined,
14404    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14405    *STRICT_OVERFLOW_P.  */
14406
14407 bool
14408 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14409                                 bool *strict_overflow_p)
14410 {
14411   if (TYPE_UNSIGNED (type))
14412     return true;
14413
14414   switch (code)
14415     {
14416     case ABS_EXPR:
14417       /* We can't return 1 if flag_wrapv is set because
14418          ABS_EXPR<INT_MIN> = INT_MIN.  */
14419       if (!INTEGRAL_TYPE_P (type))
14420         return true;
14421       if (TYPE_OVERFLOW_UNDEFINED (type))
14422         {
14423           *strict_overflow_p = true;
14424           return true;
14425         }
14426       break;
14427
14428     case NON_LVALUE_EXPR:
14429     case FLOAT_EXPR:
14430     case FIX_TRUNC_EXPR:
14431       return tree_expr_nonnegative_warnv_p (op0,
14432                                             strict_overflow_p);
14433
14434     case NOP_EXPR:
14435       {
14436         tree inner_type = TREE_TYPE (op0);
14437         tree outer_type = type;
14438
14439         if (TREE_CODE (outer_type) == REAL_TYPE)
14440           {
14441             if (TREE_CODE (inner_type) == REAL_TYPE)
14442               return tree_expr_nonnegative_warnv_p (op0,
14443                                                     strict_overflow_p);
14444             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14445               {
14446                 if (TYPE_UNSIGNED (inner_type))
14447                   return true;
14448                 return tree_expr_nonnegative_warnv_p (op0,
14449                                                       strict_overflow_p);
14450               }
14451           }
14452         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14453           {
14454             if (TREE_CODE (inner_type) == REAL_TYPE)
14455               return tree_expr_nonnegative_warnv_p (op0,
14456                                                     strict_overflow_p);
14457             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14458               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14459                       && TYPE_UNSIGNED (inner_type);
14460           }
14461       }
14462       break;
14463
14464     default:
14465       return tree_simple_nonnegative_warnv_p (code, type);
14466     }
14467
14468   /* We don't know sign of `t', so be conservative and return false.  */
14469   return false;
14470 }
14471
14472 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
14473    value is based on the assumption that signed overflow is undefined,
14474    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14475    *STRICT_OVERFLOW_P.  */
14476
14477 bool
14478 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14479                                       tree op1, bool *strict_overflow_p)
14480 {
14481   if (TYPE_UNSIGNED (type))
14482     return true;
14483
14484   switch (code)
14485     {
14486     case POINTER_PLUS_EXPR:
14487     case PLUS_EXPR:
14488       if (FLOAT_TYPE_P (type))
14489         return (tree_expr_nonnegative_warnv_p (op0,
14490                                                strict_overflow_p)
14491                 && tree_expr_nonnegative_warnv_p (op1,
14492                                                   strict_overflow_p));
14493
14494       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14495          both unsigned and at least 2 bits shorter than the result.  */
14496       if (TREE_CODE (type) == INTEGER_TYPE
14497           && TREE_CODE (op0) == NOP_EXPR
14498           && TREE_CODE (op1) == NOP_EXPR)
14499         {
14500           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14501           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14502           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14503               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14504             {
14505               unsigned int prec = MAX (TYPE_PRECISION (inner1),
14506                                        TYPE_PRECISION (inner2)) + 1;
14507               return prec < TYPE_PRECISION (type);
14508             }
14509         }
14510       break;
14511
14512     case MULT_EXPR:
14513       if (FLOAT_TYPE_P (type))
14514         {
14515           /* x * x for floating point x is always non-negative.  */
14516           if (operand_equal_p (op0, op1, 0))
14517             return true;
14518           return (tree_expr_nonnegative_warnv_p (op0,
14519                                                  strict_overflow_p)
14520                   && tree_expr_nonnegative_warnv_p (op1,
14521                                                     strict_overflow_p));
14522         }
14523
14524       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14525          both unsigned and their total bits is shorter than the result.  */
14526       if (TREE_CODE (type) == INTEGER_TYPE
14527           && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14528           && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14529         {
14530           tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14531             ? TREE_TYPE (TREE_OPERAND (op0, 0))
14532             : TREE_TYPE (op0);
14533           tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14534             ? TREE_TYPE (TREE_OPERAND (op1, 0))
14535             : TREE_TYPE (op1);
14536
14537           bool unsigned0 = TYPE_UNSIGNED (inner0);
14538           bool unsigned1 = TYPE_UNSIGNED (inner1);
14539
14540           if (TREE_CODE (op0) == INTEGER_CST)
14541             unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14542
14543           if (TREE_CODE (op1) == INTEGER_CST)
14544             unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14545
14546           if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14547               && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14548             {
14549               unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14550                 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14551                 : TYPE_PRECISION (inner0);
14552
14553               unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14554                 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14555                 : TYPE_PRECISION (inner1);
14556
14557               return precision0 + precision1 < TYPE_PRECISION (type);
14558             }
14559         }
14560       return false;
14561
14562     case BIT_AND_EXPR:
14563     case MAX_EXPR:
14564       return (tree_expr_nonnegative_warnv_p (op0,
14565                                              strict_overflow_p)
14566               || tree_expr_nonnegative_warnv_p (op1,
14567                                                 strict_overflow_p));
14568
14569     case BIT_IOR_EXPR:
14570     case BIT_XOR_EXPR:
14571     case MIN_EXPR:
14572     case RDIV_EXPR:
14573     case TRUNC_DIV_EXPR:
14574     case CEIL_DIV_EXPR:
14575     case FLOOR_DIV_EXPR:
14576     case ROUND_DIV_EXPR:
14577       return (tree_expr_nonnegative_warnv_p (op0,
14578                                              strict_overflow_p)
14579               && tree_expr_nonnegative_warnv_p (op1,
14580                                                 strict_overflow_p));
14581
14582     case TRUNC_MOD_EXPR:
14583     case CEIL_MOD_EXPR:
14584     case FLOOR_MOD_EXPR:
14585     case ROUND_MOD_EXPR:
14586       return tree_expr_nonnegative_warnv_p (op0,
14587                                             strict_overflow_p);
14588     default:
14589       return tree_simple_nonnegative_warnv_p (code, type);
14590     }
14591
14592   /* We don't know sign of `t', so be conservative and return false.  */
14593   return false;
14594 }
14595
14596 /* Return true if T is known to be non-negative.  If the return
14597    value is based on the assumption that signed overflow is undefined,
14598    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14599    *STRICT_OVERFLOW_P.  */
14600
14601 bool
14602 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14603 {
14604   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14605     return true;
14606
14607   switch (TREE_CODE (t))
14608     {
14609     case INTEGER_CST:
14610       return tree_int_cst_sgn (t) >= 0;
14611
14612     case REAL_CST:
14613       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14614
14615     case FIXED_CST:
14616       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14617
14618     case COND_EXPR:
14619       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14620                                              strict_overflow_p)
14621               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14622                                                 strict_overflow_p));
14623     default:
14624       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14625                                                    TREE_TYPE (t));
14626     }
14627   /* We don't know sign of `t', so be conservative and return false.  */
14628   return false;
14629 }
14630
14631 /* Return true if T is known to be non-negative.  If the return
14632    value is based on the assumption that signed overflow is undefined,
14633    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14634    *STRICT_OVERFLOW_P.  */
14635
14636 bool
14637 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14638                                tree arg0, tree arg1, bool *strict_overflow_p)
14639 {
14640   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14641     switch (DECL_FUNCTION_CODE (fndecl))
14642       {
14643         CASE_FLT_FN (BUILT_IN_ACOS):
14644         CASE_FLT_FN (BUILT_IN_ACOSH):
14645         CASE_FLT_FN (BUILT_IN_CABS):
14646         CASE_FLT_FN (BUILT_IN_COSH):
14647         CASE_FLT_FN (BUILT_IN_ERFC):
14648         CASE_FLT_FN (BUILT_IN_EXP):
14649         CASE_FLT_FN (BUILT_IN_EXP10):
14650         CASE_FLT_FN (BUILT_IN_EXP2):
14651         CASE_FLT_FN (BUILT_IN_FABS):
14652         CASE_FLT_FN (BUILT_IN_FDIM):
14653         CASE_FLT_FN (BUILT_IN_HYPOT):
14654         CASE_FLT_FN (BUILT_IN_POW10):
14655         CASE_INT_FN (BUILT_IN_FFS):
14656         CASE_INT_FN (BUILT_IN_PARITY):
14657         CASE_INT_FN (BUILT_IN_POPCOUNT):
14658       case BUILT_IN_BSWAP32:
14659       case BUILT_IN_BSWAP64:
14660         /* Always true.  */
14661         return true;
14662
14663         CASE_FLT_FN (BUILT_IN_SQRT):
14664         /* sqrt(-0.0) is -0.0.  */
14665         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14666           return true;
14667         return tree_expr_nonnegative_warnv_p (arg0,
14668                                               strict_overflow_p);
14669
14670         CASE_FLT_FN (BUILT_IN_ASINH):
14671         CASE_FLT_FN (BUILT_IN_ATAN):
14672         CASE_FLT_FN (BUILT_IN_ATANH):
14673         CASE_FLT_FN (BUILT_IN_CBRT):
14674         CASE_FLT_FN (BUILT_IN_CEIL):
14675         CASE_FLT_FN (BUILT_IN_ERF):
14676         CASE_FLT_FN (BUILT_IN_EXPM1):
14677         CASE_FLT_FN (BUILT_IN_FLOOR):
14678         CASE_FLT_FN (BUILT_IN_FMOD):
14679         CASE_FLT_FN (BUILT_IN_FREXP):
14680         CASE_FLT_FN (BUILT_IN_LCEIL):
14681         CASE_FLT_FN (BUILT_IN_LDEXP):
14682         CASE_FLT_FN (BUILT_IN_LFLOOR):
14683         CASE_FLT_FN (BUILT_IN_LLCEIL):
14684         CASE_FLT_FN (BUILT_IN_LLFLOOR):
14685         CASE_FLT_FN (BUILT_IN_LLRINT):
14686         CASE_FLT_FN (BUILT_IN_LLROUND):
14687         CASE_FLT_FN (BUILT_IN_LRINT):
14688         CASE_FLT_FN (BUILT_IN_LROUND):
14689         CASE_FLT_FN (BUILT_IN_MODF):
14690         CASE_FLT_FN (BUILT_IN_NEARBYINT):
14691         CASE_FLT_FN (BUILT_IN_RINT):
14692         CASE_FLT_FN (BUILT_IN_ROUND):
14693         CASE_FLT_FN (BUILT_IN_SCALB):
14694         CASE_FLT_FN (BUILT_IN_SCALBLN):
14695         CASE_FLT_FN (BUILT_IN_SCALBN):
14696         CASE_FLT_FN (BUILT_IN_SIGNBIT):
14697         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14698         CASE_FLT_FN (BUILT_IN_SINH):
14699         CASE_FLT_FN (BUILT_IN_TANH):
14700         CASE_FLT_FN (BUILT_IN_TRUNC):
14701         /* True if the 1st argument is nonnegative.  */
14702         return tree_expr_nonnegative_warnv_p (arg0,
14703                                               strict_overflow_p);
14704
14705         CASE_FLT_FN (BUILT_IN_FMAX):
14706         /* True if the 1st OR 2nd arguments are nonnegative.  */
14707         return (tree_expr_nonnegative_warnv_p (arg0,
14708                                                strict_overflow_p)
14709                 || (tree_expr_nonnegative_warnv_p (arg1,
14710                                                    strict_overflow_p)));
14711
14712         CASE_FLT_FN (BUILT_IN_FMIN):
14713         /* True if the 1st AND 2nd arguments are nonnegative.  */
14714         return (tree_expr_nonnegative_warnv_p (arg0,
14715                                                strict_overflow_p)
14716                 && (tree_expr_nonnegative_warnv_p (arg1,
14717                                                    strict_overflow_p)));
14718
14719         CASE_FLT_FN (BUILT_IN_COPYSIGN):
14720         /* True if the 2nd argument is nonnegative.  */
14721         return tree_expr_nonnegative_warnv_p (arg1,
14722                                               strict_overflow_p);
14723
14724         CASE_FLT_FN (BUILT_IN_POWI):
14725         /* True if the 1st argument is nonnegative or the second
14726            argument is an even integer.  */
14727         if (TREE_CODE (arg1) == INTEGER_CST
14728             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14729           return true;
14730         return tree_expr_nonnegative_warnv_p (arg0,
14731                                               strict_overflow_p);
14732
14733         CASE_FLT_FN (BUILT_IN_POW):
14734         /* True if the 1st argument is nonnegative or the second
14735            argument is an even integer valued real.  */
14736         if (TREE_CODE (arg1) == REAL_CST)
14737           {
14738             REAL_VALUE_TYPE c;
14739             HOST_WIDE_INT n;
14740
14741             c = TREE_REAL_CST (arg1);
14742             n = real_to_integer (&c);
14743             if ((n & 1) == 0)
14744               {
14745                 REAL_VALUE_TYPE cint;
14746                 real_from_integer (&cint, VOIDmode, n,
14747                                    n < 0 ? -1 : 0, 0);
14748                 if (real_identical (&c, &cint))
14749                   return true;
14750               }
14751           }
14752         return tree_expr_nonnegative_warnv_p (arg0,
14753                                               strict_overflow_p);
14754
14755       default:
14756         break;
14757       }
14758   return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14759                                           type);
14760 }
14761
14762 /* Return true if T is known to be non-negative.  If the return
14763    value is based on the assumption that signed overflow is undefined,
14764    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14765    *STRICT_OVERFLOW_P.  */
14766
14767 bool
14768 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14769 {
14770   enum tree_code code = TREE_CODE (t);
14771   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14772     return true;
14773
14774   switch (code)
14775     {
14776     case TARGET_EXPR:
14777       {
14778         tree temp = TARGET_EXPR_SLOT (t);
14779         t = TARGET_EXPR_INITIAL (t);
14780
14781         /* If the initializer is non-void, then it's a normal expression
14782            that will be assigned to the slot.  */
14783         if (!VOID_TYPE_P (t))
14784           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14785
14786         /* Otherwise, the initializer sets the slot in some way.  One common
14787            way is an assignment statement at the end of the initializer.  */
14788         while (1)
14789           {
14790             if (TREE_CODE (t) == BIND_EXPR)
14791               t = expr_last (BIND_EXPR_BODY (t));
14792             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14793                      || TREE_CODE (t) == TRY_CATCH_EXPR)
14794               t = expr_last (TREE_OPERAND (t, 0));
14795             else if (TREE_CODE (t) == STATEMENT_LIST)
14796               t = expr_last (t);
14797             else
14798               break;
14799           }
14800         if (TREE_CODE (t) == MODIFY_EXPR
14801             && TREE_OPERAND (t, 0) == temp)
14802           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14803                                                 strict_overflow_p);
14804
14805         return false;
14806       }
14807
14808     case CALL_EXPR:
14809       {
14810         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
14811         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
14812
14813         return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14814                                               get_callee_fndecl (t),
14815                                               arg0,
14816                                               arg1,
14817                                               strict_overflow_p);
14818       }
14819     case COMPOUND_EXPR:
14820     case MODIFY_EXPR:
14821       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14822                                             strict_overflow_p);
14823     case BIND_EXPR:
14824       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14825                                             strict_overflow_p);
14826     case SAVE_EXPR:
14827       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14828                                             strict_overflow_p);
14829
14830     default:
14831       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14832                                                    TREE_TYPE (t));
14833     }
14834
14835   /* We don't know sign of `t', so be conservative and return false.  */
14836   return false;
14837 }
14838
14839 /* Return true if T is known to be non-negative.  If the return
14840    value is based on the assumption that signed overflow is undefined,
14841    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14842    *STRICT_OVERFLOW_P.  */
14843
14844 bool
14845 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14846 {
14847   enum tree_code code;
14848   if (t == error_mark_node)
14849     return false;
14850
14851   code = TREE_CODE (t);
14852   switch (TREE_CODE_CLASS (code))
14853     {
14854     case tcc_binary:
14855     case tcc_comparison:
14856       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14857                                               TREE_TYPE (t),
14858                                               TREE_OPERAND (t, 0),
14859                                               TREE_OPERAND (t, 1),
14860                                               strict_overflow_p);
14861
14862     case tcc_unary:
14863       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14864                                              TREE_TYPE (t),
14865                                              TREE_OPERAND (t, 0),
14866                                              strict_overflow_p);
14867
14868     case tcc_constant:
14869     case tcc_declaration:
14870     case tcc_reference:
14871       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14872
14873     default:
14874       break;
14875     }
14876
14877   switch (code)
14878     {
14879     case TRUTH_AND_EXPR:
14880     case TRUTH_OR_EXPR:
14881     case TRUTH_XOR_EXPR:
14882       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14883                                               TREE_TYPE (t),
14884                                               TREE_OPERAND (t, 0),
14885                                               TREE_OPERAND (t, 1),
14886                                               strict_overflow_p);
14887     case TRUTH_NOT_EXPR:
14888       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14889                                              TREE_TYPE (t),
14890                                              TREE_OPERAND (t, 0),
14891                                              strict_overflow_p);
14892
14893     case COND_EXPR:
14894     case CONSTRUCTOR:
14895     case OBJ_TYPE_REF:
14896     case ASSERT_EXPR:
14897     case ADDR_EXPR:
14898     case WITH_SIZE_EXPR:
14899     case SSA_NAME:
14900       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14901
14902     default:
14903       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14904     }
14905 }
14906
14907 /* Return true if `t' is known to be non-negative.  Handle warnings
14908    about undefined signed overflow.  */
14909
14910 bool
14911 tree_expr_nonnegative_p (tree t)
14912 {
14913   bool ret, strict_overflow_p;
14914
14915   strict_overflow_p = false;
14916   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14917   if (strict_overflow_p)
14918     fold_overflow_warning (("assuming signed overflow does not occur when "
14919                             "determining that expression is always "
14920                             "non-negative"),
14921                            WARN_STRICT_OVERFLOW_MISC);
14922   return ret;
14923 }
14924
14925
14926 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14927    For floating point we further ensure that T is not denormal.
14928    Similar logic is present in nonzero_address in rtlanal.h.
14929
14930    If the return value is based on the assumption that signed overflow
14931    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14932    change *STRICT_OVERFLOW_P.  */
14933
14934 bool
14935 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14936                                  bool *strict_overflow_p)
14937 {
14938   switch (code)
14939     {
14940     case ABS_EXPR:
14941       return tree_expr_nonzero_warnv_p (op0,
14942                                         strict_overflow_p);
14943
14944     case NOP_EXPR:
14945       {
14946         tree inner_type = TREE_TYPE (op0);
14947         tree outer_type = type;
14948
14949         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14950                 && tree_expr_nonzero_warnv_p (op0,
14951                                               strict_overflow_p));
14952       }
14953       break;
14954
14955     case NON_LVALUE_EXPR:
14956       return tree_expr_nonzero_warnv_p (op0,
14957                                         strict_overflow_p);
14958
14959     default:
14960       break;
14961   }
14962
14963   return false;
14964 }
14965
14966 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14967    For floating point we further ensure that T is not denormal.
14968    Similar logic is present in nonzero_address in rtlanal.h.
14969
14970    If the return value is based on the assumption that signed overflow
14971    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14972    change *STRICT_OVERFLOW_P.  */
14973
14974 bool
14975 tree_binary_nonzero_warnv_p (enum tree_code code,
14976                              tree type,
14977                              tree op0,
14978                              tree op1, bool *strict_overflow_p)
14979 {
14980   bool sub_strict_overflow_p;
14981   switch (code)
14982     {
14983     case POINTER_PLUS_EXPR:
14984     case PLUS_EXPR:
14985       if (TYPE_OVERFLOW_UNDEFINED (type))
14986         {
14987           /* With the presence of negative values it is hard
14988              to say something.  */
14989           sub_strict_overflow_p = false;
14990           if (!tree_expr_nonnegative_warnv_p (op0,
14991                                               &sub_strict_overflow_p)
14992               || !tree_expr_nonnegative_warnv_p (op1,
14993                                                  &sub_strict_overflow_p))
14994             return false;
14995           /* One of operands must be positive and the other non-negative.  */
14996           /* We don't set *STRICT_OVERFLOW_P here: even if this value
14997              overflows, on a twos-complement machine the sum of two
14998              nonnegative numbers can never be zero.  */
14999           return (tree_expr_nonzero_warnv_p (op0,
15000                                              strict_overflow_p)
15001                   || tree_expr_nonzero_warnv_p (op1,
15002                                                 strict_overflow_p));
15003         }
15004       break;
15005
15006     case MULT_EXPR:
15007       if (TYPE_OVERFLOW_UNDEFINED (type))
15008         {
15009           if (tree_expr_nonzero_warnv_p (op0,
15010                                          strict_overflow_p)
15011               && tree_expr_nonzero_warnv_p (op1,
15012                                             strict_overflow_p))
15013             {
15014               *strict_overflow_p = true;
15015               return true;
15016             }
15017         }
15018       break;
15019
15020     case MIN_EXPR:
15021       sub_strict_overflow_p = false;
15022       if (tree_expr_nonzero_warnv_p (op0,
15023                                      &sub_strict_overflow_p)
15024           && tree_expr_nonzero_warnv_p (op1,
15025                                         &sub_strict_overflow_p))
15026         {
15027           if (sub_strict_overflow_p)
15028             *strict_overflow_p = true;
15029         }
15030       break;
15031
15032     case MAX_EXPR:
15033       sub_strict_overflow_p = false;
15034       if (tree_expr_nonzero_warnv_p (op0,
15035                                      &sub_strict_overflow_p))
15036         {
15037           if (sub_strict_overflow_p)
15038             *strict_overflow_p = true;
15039
15040           /* When both operands are nonzero, then MAX must be too.  */
15041           if (tree_expr_nonzero_warnv_p (op1,
15042                                          strict_overflow_p))
15043             return true;
15044
15045           /* MAX where operand 0 is positive is positive.  */
15046           return tree_expr_nonnegative_warnv_p (op0,
15047                                                strict_overflow_p);
15048         }
15049       /* MAX where operand 1 is positive is positive.  */
15050       else if (tree_expr_nonzero_warnv_p (op1,
15051                                           &sub_strict_overflow_p)
15052                && tree_expr_nonnegative_warnv_p (op1,
15053                                                  &sub_strict_overflow_p))
15054         {
15055           if (sub_strict_overflow_p)
15056             *strict_overflow_p = true;
15057           return true;
15058         }
15059       break;
15060
15061     case BIT_IOR_EXPR:
15062       return (tree_expr_nonzero_warnv_p (op1,
15063                                          strict_overflow_p)
15064               || tree_expr_nonzero_warnv_p (op0,
15065                                             strict_overflow_p));
15066
15067     default:
15068       break;
15069   }
15070
15071   return false;
15072 }
15073
15074 /* Return true when T is an address and is known to be nonzero.
15075    For floating point we further ensure that T is not denormal.
15076    Similar logic is present in nonzero_address in rtlanal.h.
15077
15078    If the return value is based on the assumption that signed overflow
15079    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15080    change *STRICT_OVERFLOW_P.  */
15081
15082 bool
15083 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15084 {
15085   bool sub_strict_overflow_p;
15086   switch (TREE_CODE (t))
15087     {
15088     case INTEGER_CST:
15089       return !integer_zerop (t);
15090
15091     case ADDR_EXPR:
15092       {
15093         tree base = TREE_OPERAND (t, 0);
15094         if (!DECL_P (base))
15095           base = get_base_address (base);
15096
15097         if (!base)
15098           return false;
15099
15100         /* Weak declarations may link to NULL.  Other things may also be NULL
15101            so protect with -fdelete-null-pointer-checks; but not variables
15102            allocated on the stack.  */
15103         if (DECL_P (base)
15104             && (flag_delete_null_pointer_checks
15105                 || (DECL_CONTEXT (base)
15106                     && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
15107                     && auto_var_in_fn_p (base, DECL_CONTEXT (base)))))
15108           return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
15109
15110         /* Constants are never weak.  */
15111         if (CONSTANT_CLASS_P (base))
15112           return true;
15113
15114         return false;
15115       }
15116
15117     case COND_EXPR:
15118       sub_strict_overflow_p = false;
15119       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15120                                      &sub_strict_overflow_p)
15121           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15122                                         &sub_strict_overflow_p))
15123         {
15124           if (sub_strict_overflow_p)
15125             *strict_overflow_p = true;
15126           return true;
15127         }
15128       break;
15129
15130     default:
15131       break;
15132     }
15133   return false;
15134 }
15135
15136 /* Return true when T is an address and is known to be nonzero.
15137    For floating point we further ensure that T is not denormal.
15138    Similar logic is present in nonzero_address in rtlanal.h.
15139
15140    If the return value is based on the assumption that signed overflow
15141    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15142    change *STRICT_OVERFLOW_P.  */
15143
15144 bool
15145 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15146 {
15147   tree type = TREE_TYPE (t);
15148   enum tree_code code;
15149
15150   /* Doing something useful for floating point would need more work.  */
15151   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15152     return false;
15153
15154   code = TREE_CODE (t);
15155   switch (TREE_CODE_CLASS (code))
15156     {
15157     case tcc_unary:
15158       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15159                                               strict_overflow_p);
15160     case tcc_binary:
15161     case tcc_comparison:
15162       return tree_binary_nonzero_warnv_p (code, type,
15163                                                TREE_OPERAND (t, 0),
15164                                                TREE_OPERAND (t, 1),
15165                                                strict_overflow_p);
15166     case tcc_constant:
15167     case tcc_declaration:
15168     case tcc_reference:
15169       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15170
15171     default:
15172       break;
15173     }
15174
15175   switch (code)
15176     {
15177     case TRUTH_NOT_EXPR:
15178       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15179                                               strict_overflow_p);
15180
15181     case TRUTH_AND_EXPR:
15182     case TRUTH_OR_EXPR:
15183     case TRUTH_XOR_EXPR:
15184       return tree_binary_nonzero_warnv_p (code, type,
15185                                                TREE_OPERAND (t, 0),
15186                                                TREE_OPERAND (t, 1),
15187                                                strict_overflow_p);
15188
15189     case COND_EXPR:
15190     case CONSTRUCTOR:
15191     case OBJ_TYPE_REF:
15192     case ASSERT_EXPR:
15193     case ADDR_EXPR:
15194     case WITH_SIZE_EXPR:
15195     case SSA_NAME:
15196       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15197
15198     case COMPOUND_EXPR:
15199     case MODIFY_EXPR:
15200     case BIND_EXPR:
15201       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15202                                         strict_overflow_p);
15203
15204     case SAVE_EXPR:
15205       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15206                                         strict_overflow_p);
15207
15208     case CALL_EXPR:
15209       return alloca_call_p (t);
15210
15211     default:
15212       break;
15213     }
15214   return false;
15215 }
15216
15217 /* Return true when T is an address and is known to be nonzero.
15218    Handle warnings about undefined signed overflow.  */
15219
15220 bool
15221 tree_expr_nonzero_p (tree t)
15222 {
15223   bool ret, strict_overflow_p;
15224
15225   strict_overflow_p = false;
15226   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15227   if (strict_overflow_p)
15228     fold_overflow_warning (("assuming signed overflow does not occur when "
15229                             "determining that expression is always "
15230                             "non-zero"),
15231                            WARN_STRICT_OVERFLOW_MISC);
15232   return ret;
15233 }
15234
15235 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15236    attempt to fold the expression to a constant without modifying TYPE,
15237    OP0 or OP1.
15238
15239    If the expression could be simplified to a constant, then return
15240    the constant.  If the expression would not be simplified to a
15241    constant, then return NULL_TREE.  */
15242
15243 tree
15244 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15245 {
15246   tree tem = fold_binary (code, type, op0, op1);
15247   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15248 }
15249
15250 /* Given the components of a unary expression CODE, TYPE and OP0,
15251    attempt to fold the expression to a constant without modifying
15252    TYPE or OP0.
15253
15254    If the expression could be simplified to a constant, then return
15255    the constant.  If the expression would not be simplified to a
15256    constant, then return NULL_TREE.  */
15257
15258 tree
15259 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15260 {
15261   tree tem = fold_unary (code, type, op0);
15262   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15263 }
15264
15265 /* If EXP represents referencing an element in a constant string
15266    (either via pointer arithmetic or array indexing), return the
15267    tree representing the value accessed, otherwise return NULL.  */
15268
15269 tree
15270 fold_read_from_constant_string (tree exp)
15271 {
15272   if ((TREE_CODE (exp) == INDIRECT_REF
15273        || TREE_CODE (exp) == ARRAY_REF)
15274       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15275     {
15276       tree exp1 = TREE_OPERAND (exp, 0);
15277       tree index;
15278       tree string;
15279       location_t loc = EXPR_LOCATION (exp);
15280
15281       if (TREE_CODE (exp) == INDIRECT_REF)
15282         string = string_constant (exp1, &index);
15283       else
15284         {
15285           tree low_bound = array_ref_low_bound (exp);
15286           index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15287
15288           /* Optimize the special-case of a zero lower bound.
15289
15290              We convert the low_bound to sizetype to avoid some problems
15291              with constant folding.  (E.g. suppose the lower bound is 1,
15292              and its mode is QI.  Without the conversion,l (ARRAY
15293              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15294              +INDEX), which becomes (ARRAY+255+INDEX).  Oops!)  */
15295           if (! integer_zerop (low_bound))
15296             index = size_diffop_loc (loc, index,
15297                                  fold_convert_loc (loc, sizetype, low_bound));
15298
15299           string = exp1;
15300         }
15301
15302       if (string
15303           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15304           && TREE_CODE (string) == STRING_CST
15305           && TREE_CODE (index) == INTEGER_CST
15306           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15307           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15308               == MODE_INT)
15309           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15310         return build_int_cst_type (TREE_TYPE (exp),
15311                                    (TREE_STRING_POINTER (string)
15312                                     [TREE_INT_CST_LOW (index)]));
15313     }
15314   return NULL;
15315 }
15316
15317 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15318    an integer constant, real, or fixed-point constant.
15319
15320    TYPE is the type of the result.  */
15321
15322 static tree
15323 fold_negate_const (tree arg0, tree type)
15324 {
15325   tree t = NULL_TREE;
15326
15327   switch (TREE_CODE (arg0))
15328     {
15329     case INTEGER_CST:
15330       {
15331         double_int val = tree_to_double_int (arg0);
15332         int overflow = neg_double (val.low, val.high, &val.low, &val.high);
15333
15334         t = force_fit_type_double (type, val, 1,
15335                                    (overflow | TREE_OVERFLOW (arg0))
15336                                    && !TYPE_UNSIGNED (type));
15337         break;
15338       }
15339
15340     case REAL_CST:
15341       t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15342       break;
15343
15344     case FIXED_CST:
15345       {
15346         FIXED_VALUE_TYPE f;
15347         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15348                                             &(TREE_FIXED_CST (arg0)), NULL,
15349                                             TYPE_SATURATING (type));
15350         t = build_fixed (type, f);
15351         /* Propagate overflow flags.  */
15352         if (overflow_p | TREE_OVERFLOW (arg0))
15353           TREE_OVERFLOW (t) = 1;
15354         break;
15355       }
15356
15357     default:
15358       gcc_unreachable ();
15359     }
15360
15361   return t;
15362 }
15363
15364 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15365    an integer constant or real constant.
15366
15367    TYPE is the type of the result.  */
15368
15369 tree
15370 fold_abs_const (tree arg0, tree type)
15371 {
15372   tree t = NULL_TREE;
15373
15374   switch (TREE_CODE (arg0))
15375     {
15376     case INTEGER_CST:
15377       {
15378         double_int val = tree_to_double_int (arg0);
15379
15380         /* If the value is unsigned or non-negative, then the absolute value
15381            is the same as the ordinary value.  */
15382         if (TYPE_UNSIGNED (type)
15383             || !double_int_negative_p (val))
15384           t = arg0;
15385
15386         /* If the value is negative, then the absolute value is
15387            its negation.  */
15388         else
15389           {
15390             int overflow;
15391
15392             overflow = neg_double (val.low, val.high, &val.low, &val.high);
15393             t = force_fit_type_double (type, val, -1,
15394                                        overflow | TREE_OVERFLOW (arg0));
15395           }
15396       }
15397       break;
15398
15399     case REAL_CST:
15400       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15401         t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15402       else
15403         t =  arg0;
15404       break;
15405
15406     default:
15407       gcc_unreachable ();
15408     }
15409
15410   return t;
15411 }
15412
15413 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15414    constant.  TYPE is the type of the result.  */
15415
15416 static tree
15417 fold_not_const (const_tree arg0, tree type)
15418 {
15419   double_int val;  
15420
15421   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15422
15423   val = double_int_not (tree_to_double_int (arg0));
15424   return force_fit_type_double (type, val, 0, TREE_OVERFLOW (arg0));
15425 }
15426
15427 /* Given CODE, a relational operator, the target type, TYPE and two
15428    constant operands OP0 and OP1, return the result of the
15429    relational operation.  If the result is not a compile time
15430    constant, then return NULL_TREE.  */
15431
15432 static tree
15433 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15434 {
15435   int result, invert;
15436
15437   /* From here on, the only cases we handle are when the result is
15438      known to be a constant.  */
15439
15440   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15441     {
15442       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15443       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15444
15445       /* Handle the cases where either operand is a NaN.  */
15446       if (real_isnan (c0) || real_isnan (c1))
15447         {
15448           switch (code)
15449             {
15450             case EQ_EXPR:
15451             case ORDERED_EXPR:
15452               result = 0;
15453               break;
15454
15455             case NE_EXPR:
15456             case UNORDERED_EXPR:
15457             case UNLT_EXPR:
15458             case UNLE_EXPR:
15459             case UNGT_EXPR:
15460             case UNGE_EXPR:
15461             case UNEQ_EXPR:
15462               result = 1;
15463               break;
15464
15465             case LT_EXPR:
15466             case LE_EXPR:
15467             case GT_EXPR:
15468             case GE_EXPR:
15469             case LTGT_EXPR:
15470               if (flag_trapping_math)
15471                 return NULL_TREE;
15472               result = 0;
15473               break;
15474
15475             default:
15476               gcc_unreachable ();
15477             }
15478
15479           return constant_boolean_node (result, type);
15480         }
15481
15482       return constant_boolean_node (real_compare (code, c0, c1), type);
15483     }
15484
15485   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15486     {
15487       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15488       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15489       return constant_boolean_node (fixed_compare (code, c0, c1), type);
15490     }
15491
15492   /* Handle equality/inequality of complex constants.  */
15493   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15494     {
15495       tree rcond = fold_relational_const (code, type,
15496                                           TREE_REALPART (op0),
15497                                           TREE_REALPART (op1));
15498       tree icond = fold_relational_const (code, type,
15499                                           TREE_IMAGPART (op0),
15500                                           TREE_IMAGPART (op1));
15501       if (code == EQ_EXPR)
15502         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15503       else if (code == NE_EXPR)
15504         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15505       else
15506         return NULL_TREE;
15507     }
15508
15509   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15510
15511      To compute GT, swap the arguments and do LT.
15512      To compute GE, do LT and invert the result.
15513      To compute LE, swap the arguments, do LT and invert the result.
15514      To compute NE, do EQ and invert the result.
15515
15516      Therefore, the code below must handle only EQ and LT.  */
15517
15518   if (code == LE_EXPR || code == GT_EXPR)
15519     {
15520       tree tem = op0;
15521       op0 = op1;
15522       op1 = tem;
15523       code = swap_tree_comparison (code);
15524     }
15525
15526   /* Note that it is safe to invert for real values here because we
15527      have already handled the one case that it matters.  */
15528
15529   invert = 0;
15530   if (code == NE_EXPR || code == GE_EXPR)
15531     {
15532       invert = 1;
15533       code = invert_tree_comparison (code, false);
15534     }
15535
15536   /* Compute a result for LT or EQ if args permit;
15537      Otherwise return T.  */
15538   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15539     {
15540       if (code == EQ_EXPR)
15541         result = tree_int_cst_equal (op0, op1);
15542       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15543         result = INT_CST_LT_UNSIGNED (op0, op1);
15544       else
15545         result = INT_CST_LT (op0, op1);
15546     }
15547   else
15548     return NULL_TREE;
15549
15550   if (invert)
15551     result ^= 1;
15552   return constant_boolean_node (result, type);
15553 }
15554
15555 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15556    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
15557    itself.  */
15558
15559 tree
15560 fold_build_cleanup_point_expr (tree type, tree expr)
15561 {
15562   /* If the expression does not have side effects then we don't have to wrap
15563      it with a cleanup point expression.  */
15564   if (!TREE_SIDE_EFFECTS (expr))
15565     return expr;
15566
15567   /* If the expression is a return, check to see if the expression inside the
15568      return has no side effects or the right hand side of the modify expression
15569      inside the return. If either don't have side effects set we don't need to
15570      wrap the expression in a cleanup point expression.  Note we don't check the
15571      left hand side of the modify because it should always be a return decl.  */
15572   if (TREE_CODE (expr) == RETURN_EXPR)
15573     {
15574       tree op = TREE_OPERAND (expr, 0);
15575       if (!op || !TREE_SIDE_EFFECTS (op))
15576         return expr;
15577       op = TREE_OPERAND (op, 1);
15578       if (!TREE_SIDE_EFFECTS (op))
15579         return expr;
15580     }
15581
15582   return build1 (CLEANUP_POINT_EXPR, type, expr);
15583 }
15584
15585 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15586    of an indirection through OP0, or NULL_TREE if no simplification is
15587    possible.  */
15588
15589 tree
15590 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15591 {
15592   tree sub = op0;
15593   tree subtype;
15594
15595   STRIP_NOPS (sub);
15596   subtype = TREE_TYPE (sub);
15597   if (!POINTER_TYPE_P (subtype))
15598     return NULL_TREE;
15599
15600   if (TREE_CODE (sub) == ADDR_EXPR)
15601     {
15602       tree op = TREE_OPERAND (sub, 0);
15603       tree optype = TREE_TYPE (op);
15604       /* *&CONST_DECL -> to the value of the const decl.  */
15605       if (TREE_CODE (op) == CONST_DECL)
15606         return DECL_INITIAL (op);
15607       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
15608       if (type == optype)
15609         {
15610           tree fop = fold_read_from_constant_string (op);
15611           if (fop)
15612             return fop;
15613           else
15614             return op;
15615         }
15616       /* *(foo *)&fooarray => fooarray[0] */
15617       else if (TREE_CODE (optype) == ARRAY_TYPE
15618                && type == TREE_TYPE (optype)
15619                && (!in_gimple_form
15620                    || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15621         {
15622           tree type_domain = TYPE_DOMAIN (optype);
15623           tree min_val = size_zero_node;
15624           if (type_domain && TYPE_MIN_VALUE (type_domain))
15625             min_val = TYPE_MIN_VALUE (type_domain);
15626           if (in_gimple_form
15627               && TREE_CODE (min_val) != INTEGER_CST)
15628             return NULL_TREE;
15629           return build4_loc (loc, ARRAY_REF, type, op, min_val,
15630                              NULL_TREE, NULL_TREE);
15631         }
15632       /* *(foo *)&complexfoo => __real__ complexfoo */
15633       else if (TREE_CODE (optype) == COMPLEX_TYPE
15634                && type == TREE_TYPE (optype))
15635         return fold_build1_loc (loc, REALPART_EXPR, type, op);
15636       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15637       else if (TREE_CODE (optype) == VECTOR_TYPE
15638                && type == TREE_TYPE (optype))
15639         {
15640           tree part_width = TYPE_SIZE (type);
15641           tree index = bitsize_int (0);
15642           return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
15643         }
15644     }
15645
15646   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15647       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15648     {
15649       tree op00 = TREE_OPERAND (sub, 0);
15650       tree op01 = TREE_OPERAND (sub, 1);
15651
15652       STRIP_NOPS (op00);
15653       if (TREE_CODE (op00) == ADDR_EXPR)
15654         {
15655           tree op00type;
15656           op00 = TREE_OPERAND (op00, 0);
15657           op00type = TREE_TYPE (op00);
15658
15659           /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15660           if (TREE_CODE (op00type) == VECTOR_TYPE
15661               && type == TREE_TYPE (op00type))
15662             {
15663               HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15664               tree part_width = TYPE_SIZE (type);
15665               unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15666               unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15667               tree index = bitsize_int (indexi);
15668
15669               if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (op00type))
15670                 return fold_build3_loc (loc,
15671                                         BIT_FIELD_REF, type, op00,
15672                                         part_width, index);
15673
15674             }
15675           /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15676           else if (TREE_CODE (op00type) == COMPLEX_TYPE
15677                    && type == TREE_TYPE (op00type))
15678             {
15679               tree size = TYPE_SIZE_UNIT (type);
15680               if (tree_int_cst_equal (size, op01))
15681                 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
15682             }
15683           /* ((foo *)&fooarray)[1] => fooarray[1] */
15684           else if (TREE_CODE (op00type) == ARRAY_TYPE
15685                    && type == TREE_TYPE (op00type))
15686             {
15687               tree type_domain = TYPE_DOMAIN (op00type);
15688               tree min_val = size_zero_node;
15689               if (type_domain && TYPE_MIN_VALUE (type_domain))
15690                 min_val = TYPE_MIN_VALUE (type_domain);
15691               op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
15692                                      TYPE_SIZE_UNIT (type));
15693               op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
15694               return build4_loc (loc, ARRAY_REF, type, op00, op01,
15695                                  NULL_TREE, NULL_TREE);
15696             }
15697         }
15698     }
15699
15700   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15701   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15702       && type == TREE_TYPE (TREE_TYPE (subtype))
15703       && (!in_gimple_form
15704           || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15705     {
15706       tree type_domain;
15707       tree min_val = size_zero_node;
15708       sub = build_fold_indirect_ref_loc (loc, sub);
15709       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15710       if (type_domain && TYPE_MIN_VALUE (type_domain))
15711         min_val = TYPE_MIN_VALUE (type_domain);
15712       if (in_gimple_form
15713           && TREE_CODE (min_val) != INTEGER_CST)
15714         return NULL_TREE;
15715       return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
15716                          NULL_TREE);
15717     }
15718
15719   return NULL_TREE;
15720 }
15721
15722 /* Builds an expression for an indirection through T, simplifying some
15723    cases.  */
15724
15725 tree
15726 build_fold_indirect_ref_loc (location_t loc, tree t)
15727 {
15728   tree type = TREE_TYPE (TREE_TYPE (t));
15729   tree sub = fold_indirect_ref_1 (loc, type, t);
15730
15731   if (sub)
15732     return sub;
15733
15734   return build1_loc (loc, INDIRECT_REF, type, t);
15735 }
15736
15737 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
15738
15739 tree
15740 fold_indirect_ref_loc (location_t loc, tree t)
15741 {
15742   tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15743
15744   if (sub)
15745     return sub;
15746   else
15747     return t;
15748 }
15749
15750 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15751    whose result is ignored.  The type of the returned tree need not be
15752    the same as the original expression.  */
15753
15754 tree
15755 fold_ignored_result (tree t)
15756 {
15757   if (!TREE_SIDE_EFFECTS (t))
15758     return integer_zero_node;
15759
15760   for (;;)
15761     switch (TREE_CODE_CLASS (TREE_CODE (t)))
15762       {
15763       case tcc_unary:
15764         t = TREE_OPERAND (t, 0);
15765         break;
15766
15767       case tcc_binary:
15768       case tcc_comparison:
15769         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15770           t = TREE_OPERAND (t, 0);
15771         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15772           t = TREE_OPERAND (t, 1);
15773         else
15774           return t;
15775         break;
15776
15777       case tcc_expression:
15778         switch (TREE_CODE (t))
15779           {
15780           case COMPOUND_EXPR:
15781             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15782               return t;
15783             t = TREE_OPERAND (t, 0);
15784             break;
15785
15786           case COND_EXPR:
15787             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15788                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15789               return t;
15790             t = TREE_OPERAND (t, 0);
15791             break;
15792
15793           default:
15794             return t;
15795           }
15796         break;
15797
15798       default:
15799         return t;
15800       }
15801 }
15802
15803 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15804    This can only be applied to objects of a sizetype.  */
15805
15806 tree
15807 round_up_loc (location_t loc, tree value, int divisor)
15808 {
15809   tree div = NULL_TREE;
15810
15811   gcc_assert (divisor > 0);
15812   if (divisor == 1)
15813     return value;
15814
15815   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15816      have to do anything.  Only do this when we are not given a const,
15817      because in that case, this check is more expensive than just
15818      doing it.  */
15819   if (TREE_CODE (value) != INTEGER_CST)
15820     {
15821       div = build_int_cst (TREE_TYPE (value), divisor);
15822
15823       if (multiple_of_p (TREE_TYPE (value), value, div))
15824         return value;
15825     }
15826
15827   /* If divisor is a power of two, simplify this to bit manipulation.  */
15828   if (divisor == (divisor & -divisor))
15829     {
15830       if (TREE_CODE (value) == INTEGER_CST)
15831         {
15832           double_int val = tree_to_double_int (value);
15833           bool overflow_p;
15834
15835           if ((val.low & (divisor - 1)) == 0)
15836             return value;
15837
15838           overflow_p = TREE_OVERFLOW (value);
15839           val.low &= ~(divisor - 1);
15840           val.low += divisor;
15841           if (val.low == 0)
15842             {
15843               val.high++;
15844               if (val.high == 0)
15845                 overflow_p = true;
15846             }
15847
15848           return force_fit_type_double (TREE_TYPE (value), val,
15849                                         -1, overflow_p);
15850         }
15851       else
15852         {
15853           tree t;
15854
15855           t = build_int_cst (TREE_TYPE (value), divisor - 1);
15856           value = size_binop_loc (loc, PLUS_EXPR, value, t);
15857           t = build_int_cst (TREE_TYPE (value), -divisor);
15858           value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15859         }
15860     }
15861   else
15862     {
15863       if (!div)
15864         div = build_int_cst (TREE_TYPE (value), divisor);
15865       value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
15866       value = size_binop_loc (loc, MULT_EXPR, value, div);
15867     }
15868
15869   return value;
15870 }
15871
15872 /* Likewise, but round down.  */
15873
15874 tree
15875 round_down_loc (location_t loc, tree value, int divisor)
15876 {
15877   tree div = NULL_TREE;
15878
15879   gcc_assert (divisor > 0);
15880   if (divisor == 1)
15881     return value;
15882
15883   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15884      have to do anything.  Only do this when we are not given a const,
15885      because in that case, this check is more expensive than just
15886      doing it.  */
15887   if (TREE_CODE (value) != INTEGER_CST)
15888     {
15889       div = build_int_cst (TREE_TYPE (value), divisor);
15890
15891       if (multiple_of_p (TREE_TYPE (value), value, div))
15892         return value;
15893     }
15894
15895   /* If divisor is a power of two, simplify this to bit manipulation.  */
15896   if (divisor == (divisor & -divisor))
15897     {
15898       tree t;
15899
15900       t = build_int_cst (TREE_TYPE (value), -divisor);
15901       value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15902     }
15903   else
15904     {
15905       if (!div)
15906         div = build_int_cst (TREE_TYPE (value), divisor);
15907       value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
15908       value = size_binop_loc (loc, MULT_EXPR, value, div);
15909     }
15910
15911   return value;
15912 }
15913
15914 /* Returns the pointer to the base of the object addressed by EXP and
15915    extracts the information about the offset of the access, storing it
15916    to PBITPOS and POFFSET.  */
15917
15918 static tree
15919 split_address_to_core_and_offset (tree exp,
15920                                   HOST_WIDE_INT *pbitpos, tree *poffset)
15921 {
15922   tree core;
15923   enum machine_mode mode;
15924   int unsignedp, volatilep;
15925   HOST_WIDE_INT bitsize;
15926   location_t loc = EXPR_LOCATION (exp);
15927
15928   if (TREE_CODE (exp) == ADDR_EXPR)
15929     {
15930       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15931                                   poffset, &mode, &unsignedp, &volatilep,
15932                                   false);
15933       core = build_fold_addr_expr_loc (loc, core);
15934     }
15935   else
15936     {
15937       core = exp;
15938       *pbitpos = 0;
15939       *poffset = NULL_TREE;
15940     }
15941
15942   return core;
15943 }
15944
15945 /* Returns true if addresses of E1 and E2 differ by a constant, false
15946    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
15947
15948 bool
15949 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15950 {
15951   tree core1, core2;
15952   HOST_WIDE_INT bitpos1, bitpos2;
15953   tree toffset1, toffset2, tdiff, type;
15954
15955   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15956   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15957
15958   if (bitpos1 % BITS_PER_UNIT != 0
15959       || bitpos2 % BITS_PER_UNIT != 0
15960       || !operand_equal_p (core1, core2, 0))
15961     return false;
15962
15963   if (toffset1 && toffset2)
15964     {
15965       type = TREE_TYPE (toffset1);
15966       if (type != TREE_TYPE (toffset2))
15967         toffset2 = fold_convert (type, toffset2);
15968
15969       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15970       if (!cst_and_fits_in_hwi (tdiff))
15971         return false;
15972
15973       *diff = int_cst_value (tdiff);
15974     }
15975   else if (toffset1 || toffset2)
15976     {
15977       /* If only one of the offsets is non-constant, the difference cannot
15978          be a constant.  */
15979       return false;
15980     }
15981   else
15982     *diff = 0;
15983
15984   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15985   return true;
15986 }
15987
15988 /* Simplify the floating point expression EXP when the sign of the
15989    result is not significant.  Return NULL_TREE if no simplification
15990    is possible.  */
15991
15992 tree
15993 fold_strip_sign_ops (tree exp)
15994 {
15995   tree arg0, arg1;
15996   location_t loc = EXPR_LOCATION (exp);
15997
15998   switch (TREE_CODE (exp))
15999     {
16000     case ABS_EXPR:
16001     case NEGATE_EXPR:
16002       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16003       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
16004
16005     case MULT_EXPR:
16006     case RDIV_EXPR:
16007       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
16008         return NULL_TREE;
16009       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16010       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16011       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
16012         return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
16013                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
16014                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
16015       break;
16016
16017     case COMPOUND_EXPR:
16018       arg0 = TREE_OPERAND (exp, 0);
16019       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16020       if (arg1)
16021         return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
16022       break;
16023
16024     case COND_EXPR:
16025       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16026       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
16027       if (arg0 || arg1)
16028         return fold_build3_loc (loc,
16029                             COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
16030                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
16031                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
16032       break;
16033
16034     case CALL_EXPR:
16035       {
16036         const enum built_in_function fcode = builtin_mathfn_code (exp);
16037         switch (fcode)
16038         {
16039         CASE_FLT_FN (BUILT_IN_COPYSIGN):
16040           /* Strip copysign function call, return the 1st argument. */
16041           arg0 = CALL_EXPR_ARG (exp, 0);
16042           arg1 = CALL_EXPR_ARG (exp, 1);
16043           return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
16044
16045         default:
16046           /* Strip sign ops from the argument of "odd" math functions.  */
16047           if (negate_mathfn_p (fcode))
16048             {
16049               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
16050               if (arg0)
16051                 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
16052             }
16053           break;
16054         }
16055       }
16056       break;
16057
16058     default:
16059       break;
16060     }
16061   return NULL_TREE;
16062 }