OSDN Git Service

Typo in test
[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    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
942
943 tree
944 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
945 {
946   double_int op1, op2, res, tmp;
947   tree t;
948   tree type = TREE_TYPE (arg1);
949   bool uns = TYPE_UNSIGNED (type);
950   bool is_sizetype
951     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
952   bool overflow = false;
953
954   op1 = tree_to_double_int (arg1);
955   op2 = tree_to_double_int (arg2);
956
957   switch (code)
958     {
959     case BIT_IOR_EXPR:
960       res = double_int_ior (op1, op2);
961       break;
962
963     case BIT_XOR_EXPR:
964       res = double_int_xor (op1, op2);
965       break;
966
967     case BIT_AND_EXPR:
968       res = double_int_and (op1, op2);
969       break;
970
971     case RSHIFT_EXPR:
972       res = double_int_rshift (op1, double_int_to_shwi (op2),
973                                TYPE_PRECISION (type), !uns);
974       break;
975
976     case LSHIFT_EXPR:
977       /* It's unclear from the C standard whether shifts can overflow.
978          The following code ignores overflow; perhaps a C standard
979          interpretation ruling is needed.  */
980       res = double_int_lshift (op1, double_int_to_shwi (op2),
981                                TYPE_PRECISION (type), !uns);
982       break;
983
984     case RROTATE_EXPR:
985       res = double_int_rrotate (op1, double_int_to_shwi (op2),
986                                 TYPE_PRECISION (type));
987       break;
988
989     case LROTATE_EXPR:
990       res = double_int_lrotate (op1, double_int_to_shwi (op2),
991                                 TYPE_PRECISION (type));
992       break;
993
994     case PLUS_EXPR:
995       overflow = add_double (op1.low, op1.high, op2.low, op2.high,
996                              &res.low, &res.high);
997       break;
998
999     case MINUS_EXPR:
1000       neg_double (op2.low, op2.high, &res.low, &res.high);
1001       add_double (op1.low, op1.high, res.low, res.high,
1002                   &res.low, &res.high);
1003       overflow = OVERFLOW_SUM_SIGN (res.high, op2.high, op1.high);
1004       break;
1005
1006     case MULT_EXPR:
1007       overflow = mul_double (op1.low, op1.high, op2.low, op2.high,
1008                              &res.low, &res.high);
1009       break;
1010
1011     case TRUNC_DIV_EXPR:
1012     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1013     case EXACT_DIV_EXPR:
1014       /* This is a shortcut for a common special case.  */
1015       if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1016           && !TREE_OVERFLOW (arg1)
1017           && !TREE_OVERFLOW (arg2)
1018           && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1019         {
1020           if (code == CEIL_DIV_EXPR)
1021             op1.low += op2.low - 1;
1022
1023           res.low = op1.low / op2.low, res.high = 0;
1024           break;
1025         }
1026
1027       /* ... fall through ...  */
1028
1029     case ROUND_DIV_EXPR:
1030       if (double_int_zero_p (op2))
1031         return NULL_TREE;
1032       if (double_int_one_p (op2))
1033         {
1034           res = op1;
1035           break;
1036         }
1037       if (double_int_equal_p (op1, op2)
1038           && ! double_int_zero_p (op1))
1039         {
1040           res = double_int_one;
1041           break;
1042         }
1043       overflow = div_and_round_double (code, uns,
1044                                        op1.low, op1.high, op2.low, op2.high,
1045                                        &res.low, &res.high,
1046                                        &tmp.low, &tmp.high);
1047       break;
1048
1049     case TRUNC_MOD_EXPR:
1050     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1051       /* This is a shortcut for a common special case.  */
1052       if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1053           && !TREE_OVERFLOW (arg1)
1054           && !TREE_OVERFLOW (arg2)
1055           && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1056         {
1057           if (code == CEIL_MOD_EXPR)
1058             op1.low += op2.low - 1;
1059           res.low = op1.low % op2.low, res.high = 0;
1060           break;
1061         }
1062
1063       /* ... fall through ...  */
1064
1065     case ROUND_MOD_EXPR:
1066       if (double_int_zero_p (op2))
1067         return NULL_TREE;
1068       overflow = div_and_round_double (code, uns,
1069                                        op1.low, op1.high, op2.low, op2.high,
1070                                        &tmp.low, &tmp.high,
1071                                        &res.low, &res.high);
1072       break;
1073
1074     case MIN_EXPR:
1075       res = double_int_min (op1, op2, uns);
1076       break;
1077
1078     case MAX_EXPR:
1079       res = double_int_max (op1, op2, uns);
1080       break;
1081
1082     default:
1083       return NULL_TREE;
1084     }
1085
1086   if (notrunc)
1087     {
1088       t = build_int_cst_wide (TREE_TYPE (arg1), res.low, res.high);
1089
1090       /* Propagate overflow flags ourselves.  */
1091       if (((!uns || is_sizetype) && overflow)
1092           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1093         {
1094           t = copy_node (t);
1095           TREE_OVERFLOW (t) = 1;
1096         }
1097     }
1098   else
1099     t = force_fit_type_double (TREE_TYPE (arg1), res, 1,
1100                                ((!uns || is_sizetype) && overflow)
1101                                | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1102
1103   return t;
1104 }
1105
1106 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1107    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1108    are the same kind of constant and the same machine mode.  Return zero if
1109    combining the constants is not allowed in the current operating mode.  */
1110
1111 static tree
1112 const_binop (enum tree_code code, tree arg1, tree arg2)
1113 {
1114   /* Sanity check for the recursive cases.  */
1115   if (!arg1 || !arg2)
1116     return NULL_TREE;
1117
1118   STRIP_NOPS (arg1);
1119   STRIP_NOPS (arg2);
1120
1121   if (TREE_CODE (arg1) == INTEGER_CST)
1122     return int_const_binop (code, arg1, arg2, 0);
1123
1124   if (TREE_CODE (arg1) == REAL_CST)
1125     {
1126       enum machine_mode mode;
1127       REAL_VALUE_TYPE d1;
1128       REAL_VALUE_TYPE d2;
1129       REAL_VALUE_TYPE value;
1130       REAL_VALUE_TYPE result;
1131       bool inexact;
1132       tree t, type;
1133
1134       /* The following codes are handled by real_arithmetic.  */
1135       switch (code)
1136         {
1137         case PLUS_EXPR:
1138         case MINUS_EXPR:
1139         case MULT_EXPR:
1140         case RDIV_EXPR:
1141         case MIN_EXPR:
1142         case MAX_EXPR:
1143           break;
1144
1145         default:
1146           return NULL_TREE;
1147         }
1148
1149       d1 = TREE_REAL_CST (arg1);
1150       d2 = TREE_REAL_CST (arg2);
1151
1152       type = TREE_TYPE (arg1);
1153       mode = TYPE_MODE (type);
1154
1155       /* Don't perform operation if we honor signaling NaNs and
1156          either operand is a NaN.  */
1157       if (HONOR_SNANS (mode)
1158           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1159         return NULL_TREE;
1160
1161       /* Don't perform operation if it would raise a division
1162          by zero exception.  */
1163       if (code == RDIV_EXPR
1164           && REAL_VALUES_EQUAL (d2, dconst0)
1165           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1166         return NULL_TREE;
1167
1168       /* If either operand is a NaN, just return it.  Otherwise, set up
1169          for floating-point trap; we return an overflow.  */
1170       if (REAL_VALUE_ISNAN (d1))
1171         return arg1;
1172       else if (REAL_VALUE_ISNAN (d2))
1173         return arg2;
1174
1175       inexact = real_arithmetic (&value, code, &d1, &d2);
1176       real_convert (&result, mode, &value);
1177
1178       /* Don't constant fold this floating point operation if
1179          the result has overflowed and flag_trapping_math.  */
1180       if (flag_trapping_math
1181           && MODE_HAS_INFINITIES (mode)
1182           && REAL_VALUE_ISINF (result)
1183           && !REAL_VALUE_ISINF (d1)
1184           && !REAL_VALUE_ISINF (d2))
1185         return NULL_TREE;
1186
1187       /* Don't constant fold this floating point operation if the
1188          result may dependent upon the run-time rounding mode and
1189          flag_rounding_math is set, or if GCC's software emulation
1190          is unable to accurately represent the result.  */
1191       if ((flag_rounding_math
1192            || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1193           && (inexact || !real_identical (&result, &value)))
1194         return NULL_TREE;
1195
1196       t = build_real (type, result);
1197
1198       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1199       return t;
1200     }
1201
1202   if (TREE_CODE (arg1) == FIXED_CST)
1203     {
1204       FIXED_VALUE_TYPE f1;
1205       FIXED_VALUE_TYPE f2;
1206       FIXED_VALUE_TYPE result;
1207       tree t, type;
1208       int sat_p;
1209       bool overflow_p;
1210
1211       /* The following codes are handled by fixed_arithmetic.  */
1212       switch (code)
1213         {
1214         case PLUS_EXPR:
1215         case MINUS_EXPR:
1216         case MULT_EXPR:
1217         case TRUNC_DIV_EXPR:
1218           f2 = TREE_FIXED_CST (arg2);
1219           break;
1220
1221         case LSHIFT_EXPR:
1222         case RSHIFT_EXPR:
1223           f2.data.high = TREE_INT_CST_HIGH (arg2);
1224           f2.data.low = TREE_INT_CST_LOW (arg2);
1225           f2.mode = SImode;
1226           break;
1227
1228         default:
1229           return NULL_TREE;
1230         }
1231
1232       f1 = TREE_FIXED_CST (arg1);
1233       type = TREE_TYPE (arg1);
1234       sat_p = TYPE_SATURATING (type);
1235       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1236       t = build_fixed (type, result);
1237       /* Propagate overflow flags.  */
1238       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1239         TREE_OVERFLOW (t) = 1;
1240       return t;
1241     }
1242
1243   if (TREE_CODE (arg1) == COMPLEX_CST)
1244     {
1245       tree type = TREE_TYPE (arg1);
1246       tree r1 = TREE_REALPART (arg1);
1247       tree i1 = TREE_IMAGPART (arg1);
1248       tree r2 = TREE_REALPART (arg2);
1249       tree i2 = TREE_IMAGPART (arg2);
1250       tree real, imag;
1251
1252       switch (code)
1253         {
1254         case PLUS_EXPR:
1255         case MINUS_EXPR:
1256           real = const_binop (code, r1, r2);
1257           imag = const_binop (code, i1, i2);
1258           break;
1259
1260         case MULT_EXPR:
1261           if (COMPLEX_FLOAT_TYPE_P (type))
1262             return do_mpc_arg2 (arg1, arg2, type,
1263                                 /* do_nonfinite= */ folding_initializer,
1264                                 mpc_mul);
1265
1266           real = const_binop (MINUS_EXPR,
1267                               const_binop (MULT_EXPR, r1, r2),
1268                               const_binop (MULT_EXPR, i1, i2));
1269           imag = const_binop (PLUS_EXPR,
1270                               const_binop (MULT_EXPR, r1, i2),
1271                               const_binop (MULT_EXPR, i1, r2));
1272           break;
1273
1274         case RDIV_EXPR:
1275           if (COMPLEX_FLOAT_TYPE_P (type))
1276             return do_mpc_arg2 (arg1, arg2, type,
1277                                 /* do_nonfinite= */ folding_initializer,
1278                                 mpc_div);
1279           /* Fallthru ... */
1280         case TRUNC_DIV_EXPR:
1281         case CEIL_DIV_EXPR:
1282         case FLOOR_DIV_EXPR:
1283         case ROUND_DIV_EXPR:
1284           if (flag_complex_method == 0)
1285           {
1286             /* Keep this algorithm in sync with
1287                tree-complex.c:expand_complex_div_straight().
1288
1289                Expand complex division to scalars, straightforward algorithm.
1290                a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1291                t = br*br + bi*bi
1292             */
1293             tree magsquared
1294               = const_binop (PLUS_EXPR,
1295                              const_binop (MULT_EXPR, r2, r2),
1296                              const_binop (MULT_EXPR, i2, i2));
1297             tree t1
1298               = const_binop (PLUS_EXPR,
1299                              const_binop (MULT_EXPR, r1, r2),
1300                              const_binop (MULT_EXPR, i1, i2));
1301             tree t2
1302               = const_binop (MINUS_EXPR,
1303                              const_binop (MULT_EXPR, i1, r2),
1304                              const_binop (MULT_EXPR, r1, i2));
1305
1306             real = const_binop (code, t1, magsquared);
1307             imag = const_binop (code, t2, magsquared);
1308           }
1309           else
1310           {
1311             /* Keep this algorithm in sync with
1312                tree-complex.c:expand_complex_div_wide().
1313
1314                Expand complex division to scalars, modified algorithm to minimize
1315                overflow with wide input ranges.  */
1316             tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1317                                         fold_abs_const (r2, TREE_TYPE (type)),
1318                                         fold_abs_const (i2, TREE_TYPE (type)));
1319
1320             if (integer_nonzerop (compare))
1321               {
1322                 /* In the TRUE branch, we compute
1323                    ratio = br/bi;
1324                    div = (br * ratio) + bi;
1325                    tr = (ar * ratio) + ai;
1326                    ti = (ai * ratio) - ar;
1327                    tr = tr / div;
1328                    ti = ti / div;  */
1329                 tree ratio = const_binop (code, r2, i2);
1330                 tree div = const_binop (PLUS_EXPR, i2,
1331                                         const_binop (MULT_EXPR, r2, ratio));
1332                 real = const_binop (MULT_EXPR, r1, ratio);
1333                 real = const_binop (PLUS_EXPR, real, i1);
1334                 real = const_binop (code, real, div);
1335
1336                 imag = const_binop (MULT_EXPR, i1, ratio);
1337                 imag = const_binop (MINUS_EXPR, imag, r1);
1338                 imag = const_binop (code, imag, div);
1339               }
1340             else
1341               {
1342                 /* In the FALSE branch, we compute
1343                    ratio = d/c;
1344                    divisor = (d * ratio) + c;
1345                    tr = (b * ratio) + a;
1346                    ti = b - (a * ratio);
1347                    tr = tr / div;
1348                    ti = ti / div;  */
1349                 tree ratio = const_binop (code, i2, r2);
1350                 tree div = const_binop (PLUS_EXPR, r2,
1351                                         const_binop (MULT_EXPR, i2, ratio));
1352
1353                 real = const_binop (MULT_EXPR, i1, ratio);
1354                 real = const_binop (PLUS_EXPR, real, r1);
1355                 real = const_binop (code, real, div);
1356
1357                 imag = const_binop (MULT_EXPR, r1, ratio);
1358                 imag = const_binop (MINUS_EXPR, i1, imag);
1359                 imag = const_binop (code, imag, div);
1360               }
1361           }
1362           break;
1363
1364         default:
1365           return NULL_TREE;
1366         }
1367
1368       if (real && imag)
1369         return build_complex (type, real, imag);
1370     }
1371
1372   if (TREE_CODE (arg1) == VECTOR_CST)
1373     {
1374       tree type = TREE_TYPE(arg1);
1375       int count = TYPE_VECTOR_SUBPARTS (type), i;
1376       tree elements1, elements2, list = NULL_TREE;
1377
1378       if(TREE_CODE(arg2) != VECTOR_CST)
1379         return NULL_TREE;
1380
1381       elements1 = TREE_VECTOR_CST_ELTS (arg1);
1382       elements2 = TREE_VECTOR_CST_ELTS (arg2);
1383
1384       for (i = 0; i < count; i++)
1385         {
1386           tree elem1, elem2, elem;
1387
1388           /* The trailing elements can be empty and should be treated as 0 */
1389           if(!elements1)
1390             elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1391           else
1392             {
1393               elem1 = TREE_VALUE(elements1);
1394               elements1 = TREE_CHAIN (elements1);
1395             }
1396
1397           if(!elements2)
1398             elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1399           else
1400             {
1401               elem2 = TREE_VALUE(elements2);
1402               elements2 = TREE_CHAIN (elements2);
1403             }
1404
1405           elem = const_binop (code, elem1, elem2);
1406
1407           /* It is possible that const_binop cannot handle the given
1408             code and return NULL_TREE */
1409           if(elem == NULL_TREE)
1410             return NULL_TREE;
1411
1412           list = tree_cons (NULL_TREE, elem, list);
1413         }
1414       return build_vector(type, nreverse(list));
1415     }
1416   return NULL_TREE;
1417 }
1418
1419 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
1420    indicates which particular sizetype to create.  */
1421
1422 tree
1423 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1424 {
1425   return build_int_cst (sizetype_tab[(int) kind], number);
1426 }
1427 \f
1428 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
1429    is a tree code.  The type of the result is taken from the operands.
1430    Both must be equivalent integer types, ala int_binop_types_match_p.
1431    If the operands are constant, so is the result.  */
1432
1433 tree
1434 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1435 {
1436   tree type = TREE_TYPE (arg0);
1437
1438   if (arg0 == error_mark_node || arg1 == error_mark_node)
1439     return error_mark_node;
1440
1441   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1442                                        TREE_TYPE (arg1)));
1443
1444   /* Handle the special case of two integer constants faster.  */
1445   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1446     {
1447       /* And some specific cases even faster than that.  */
1448       if (code == PLUS_EXPR)
1449         {
1450           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1451             return arg1;
1452           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1453             return arg0;
1454         }
1455       else if (code == MINUS_EXPR)
1456         {
1457           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1458             return arg0;
1459         }
1460       else if (code == MULT_EXPR)
1461         {
1462           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1463             return arg1;
1464         }
1465
1466       /* Handle general case of two integer constants.  */
1467       return int_const_binop (code, arg0, arg1, 0);
1468     }
1469
1470   return fold_build2_loc (loc, code, type, arg0, arg1);
1471 }
1472
1473 /* Given two values, either both of sizetype or both of bitsizetype,
1474    compute the difference between the two values.  Return the value
1475    in signed type corresponding to the type of the operands.  */
1476
1477 tree
1478 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1479 {
1480   tree type = TREE_TYPE (arg0);
1481   tree ctype;
1482
1483   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1484                                        TREE_TYPE (arg1)));
1485
1486   /* If the type is already signed, just do the simple thing.  */
1487   if (!TYPE_UNSIGNED (type))
1488     return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1489
1490   if (type == sizetype)
1491     ctype = ssizetype;
1492   else if (type == bitsizetype)
1493     ctype = sbitsizetype;
1494   else
1495     ctype = signed_type_for (type);
1496
1497   /* If either operand is not a constant, do the conversions to the signed
1498      type and subtract.  The hardware will do the right thing with any
1499      overflow in the subtraction.  */
1500   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1501     return size_binop_loc (loc, MINUS_EXPR,
1502                            fold_convert_loc (loc, ctype, arg0),
1503                            fold_convert_loc (loc, ctype, arg1));
1504
1505   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1506      Otherwise, subtract the other way, convert to CTYPE (we know that can't
1507      overflow) and negate (which can't either).  Special-case a result
1508      of zero while we're here.  */
1509   if (tree_int_cst_equal (arg0, arg1))
1510     return build_int_cst (ctype, 0);
1511   else if (tree_int_cst_lt (arg1, arg0))
1512     return fold_convert_loc (loc, ctype,
1513                              size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1514   else
1515     return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1516                            fold_convert_loc (loc, ctype,
1517                                              size_binop_loc (loc,
1518                                                              MINUS_EXPR,
1519                                                              arg1, arg0)));
1520 }
1521 \f
1522 /* A subroutine of fold_convert_const handling conversions of an
1523    INTEGER_CST to another integer type.  */
1524
1525 static tree
1526 fold_convert_const_int_from_int (tree type, const_tree arg1)
1527 {
1528   tree t;
1529
1530   /* Given an integer constant, make new constant with new type,
1531      appropriately sign-extended or truncated.  */
1532   t = force_fit_type_double (type, tree_to_double_int (arg1),
1533                              !POINTER_TYPE_P (TREE_TYPE (arg1)),
1534                              (TREE_INT_CST_HIGH (arg1) < 0
1535                               && (TYPE_UNSIGNED (type)
1536                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1537                              | TREE_OVERFLOW (arg1));
1538
1539   return t;
1540 }
1541
1542 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1543    to an integer type.  */
1544
1545 static tree
1546 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1547 {
1548   int overflow = 0;
1549   tree t;
1550
1551   /* The following code implements the floating point to integer
1552      conversion rules required by the Java Language Specification,
1553      that IEEE NaNs are mapped to zero and values that overflow
1554      the target precision saturate, i.e. values greater than
1555      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1556      are mapped to INT_MIN.  These semantics are allowed by the
1557      C and C++ standards that simply state that the behavior of
1558      FP-to-integer conversion is unspecified upon overflow.  */
1559
1560   double_int val;
1561   REAL_VALUE_TYPE r;
1562   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1563
1564   switch (code)
1565     {
1566     case FIX_TRUNC_EXPR:
1567       real_trunc (&r, VOIDmode, &x);
1568       break;
1569
1570     default:
1571       gcc_unreachable ();
1572     }
1573
1574   /* If R is NaN, return zero and show we have an overflow.  */
1575   if (REAL_VALUE_ISNAN (r))
1576     {
1577       overflow = 1;
1578       val = double_int_zero;
1579     }
1580
1581   /* See if R is less than the lower bound or greater than the
1582      upper bound.  */
1583
1584   if (! overflow)
1585     {
1586       tree lt = TYPE_MIN_VALUE (type);
1587       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1588       if (REAL_VALUES_LESS (r, l))
1589         {
1590           overflow = 1;
1591           val = tree_to_double_int (lt);
1592         }
1593     }
1594
1595   if (! overflow)
1596     {
1597       tree ut = TYPE_MAX_VALUE (type);
1598       if (ut)
1599         {
1600           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1601           if (REAL_VALUES_LESS (u, r))
1602             {
1603               overflow = 1;
1604               val = tree_to_double_int (ut);
1605             }
1606         }
1607     }
1608
1609   if (! overflow)
1610     real_to_integer2 ((HOST_WIDE_INT *) &val.low, &val.high, &r);
1611
1612   t = force_fit_type_double (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1613   return t;
1614 }
1615
1616 /* A subroutine of fold_convert_const handling conversions of a
1617    FIXED_CST to an integer type.  */
1618
1619 static tree
1620 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1621 {
1622   tree t;
1623   double_int temp, temp_trunc;
1624   unsigned int mode;
1625
1626   /* Right shift FIXED_CST to temp by fbit.  */
1627   temp = TREE_FIXED_CST (arg1).data;
1628   mode = TREE_FIXED_CST (arg1).mode;
1629   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
1630     {
1631       temp = double_int_rshift (temp, GET_MODE_FBIT (mode),
1632                                 HOST_BITS_PER_DOUBLE_INT,
1633                                 SIGNED_FIXED_POINT_MODE_P (mode));
1634
1635       /* Left shift temp to temp_trunc by fbit.  */
1636       temp_trunc = double_int_lshift (temp, GET_MODE_FBIT (mode),
1637                                       HOST_BITS_PER_DOUBLE_INT,
1638                                       SIGNED_FIXED_POINT_MODE_P (mode));
1639     }
1640   else
1641     {
1642       temp = double_int_zero;
1643       temp_trunc = double_int_zero;
1644     }
1645
1646   /* If FIXED_CST is negative, we need to round the value toward 0.
1647      By checking if the fractional bits are not zero to add 1 to temp.  */
1648   if (SIGNED_FIXED_POINT_MODE_P (mode)
1649       && double_int_negative_p (temp_trunc)
1650       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
1651     temp = double_int_add (temp, double_int_one);
1652
1653   /* Given a fixed-point constant, make new constant with new type,
1654      appropriately sign-extended or truncated.  */
1655   t = force_fit_type_double (type, temp, -1,
1656                              (double_int_negative_p (temp)
1657                               && (TYPE_UNSIGNED (type)
1658                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1659                              | TREE_OVERFLOW (arg1));
1660
1661   return t;
1662 }
1663
1664 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1665    to another floating point type.  */
1666
1667 static tree
1668 fold_convert_const_real_from_real (tree type, const_tree arg1)
1669 {
1670   REAL_VALUE_TYPE value;
1671   tree t;
1672
1673   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1674   t = build_real (type, value);
1675
1676   /* If converting an infinity or NAN to a representation that doesn't
1677      have one, set the overflow bit so that we can produce some kind of
1678      error message at the appropriate point if necessary.  It's not the
1679      most user-friendly message, but it's better than nothing.  */
1680   if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1681       && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1682     TREE_OVERFLOW (t) = 1;
1683   else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1684            && !MODE_HAS_NANS (TYPE_MODE (type)))
1685     TREE_OVERFLOW (t) = 1;
1686   /* Regular overflow, conversion produced an infinity in a mode that
1687      can't represent them.  */
1688   else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1689            && REAL_VALUE_ISINF (value)
1690            && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1691     TREE_OVERFLOW (t) = 1;
1692   else
1693     TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1694   return t;
1695 }
1696
1697 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1698    to a floating point type.  */
1699
1700 static tree
1701 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
1702 {
1703   REAL_VALUE_TYPE value;
1704   tree t;
1705
1706   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
1707   t = build_real (type, value);
1708
1709   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1710   return t;
1711 }
1712
1713 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1714    to another fixed-point type.  */
1715
1716 static tree
1717 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
1718 {
1719   FIXED_VALUE_TYPE value;
1720   tree t;
1721   bool overflow_p;
1722
1723   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
1724                               TYPE_SATURATING (type));
1725   t = build_fixed (type, value);
1726
1727   /* Propagate overflow flags.  */
1728   if (overflow_p | TREE_OVERFLOW (arg1))
1729     TREE_OVERFLOW (t) = 1;
1730   return t;
1731 }
1732
1733 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
1734    to a fixed-point type.  */
1735
1736 static tree
1737 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
1738 {
1739   FIXED_VALUE_TYPE value;
1740   tree t;
1741   bool overflow_p;
1742
1743   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
1744                                        TREE_INT_CST (arg1),
1745                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
1746                                        TYPE_SATURATING (type));
1747   t = build_fixed (type, value);
1748
1749   /* Propagate overflow flags.  */
1750   if (overflow_p | TREE_OVERFLOW (arg1))
1751     TREE_OVERFLOW (t) = 1;
1752   return t;
1753 }
1754
1755 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1756    to a fixed-point type.  */
1757
1758 static tree
1759 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
1760 {
1761   FIXED_VALUE_TYPE value;
1762   tree t;
1763   bool overflow_p;
1764
1765   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
1766                                         &TREE_REAL_CST (arg1),
1767                                         TYPE_SATURATING (type));
1768   t = build_fixed (type, value);
1769
1770   /* Propagate overflow flags.  */
1771   if (overflow_p | TREE_OVERFLOW (arg1))
1772     TREE_OVERFLOW (t) = 1;
1773   return t;
1774 }
1775
1776 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1777    type TYPE.  If no simplification can be done return NULL_TREE.  */
1778
1779 static tree
1780 fold_convert_const (enum tree_code code, tree type, tree arg1)
1781 {
1782   if (TREE_TYPE (arg1) == type)
1783     return arg1;
1784
1785   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
1786       || TREE_CODE (type) == OFFSET_TYPE)
1787     {
1788       if (TREE_CODE (arg1) == INTEGER_CST)
1789         return fold_convert_const_int_from_int (type, arg1);
1790       else if (TREE_CODE (arg1) == REAL_CST)
1791         return fold_convert_const_int_from_real (code, type, arg1);
1792       else if (TREE_CODE (arg1) == FIXED_CST)
1793         return fold_convert_const_int_from_fixed (type, arg1);
1794     }
1795   else if (TREE_CODE (type) == REAL_TYPE)
1796     {
1797       if (TREE_CODE (arg1) == INTEGER_CST)
1798         return build_real_from_int_cst (type, arg1);
1799       else if (TREE_CODE (arg1) == REAL_CST)
1800         return fold_convert_const_real_from_real (type, arg1);
1801       else if (TREE_CODE (arg1) == FIXED_CST)
1802         return fold_convert_const_real_from_fixed (type, arg1);
1803     }
1804   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
1805     {
1806       if (TREE_CODE (arg1) == FIXED_CST)
1807         return fold_convert_const_fixed_from_fixed (type, arg1);
1808       else if (TREE_CODE (arg1) == INTEGER_CST)
1809         return fold_convert_const_fixed_from_int (type, arg1);
1810       else if (TREE_CODE (arg1) == REAL_CST)
1811         return fold_convert_const_fixed_from_real (type, arg1);
1812     }
1813   return NULL_TREE;
1814 }
1815
1816 /* Construct a vector of zero elements of vector type TYPE.  */
1817
1818 static tree
1819 build_zero_vector (tree type)
1820 {
1821   tree t;
1822
1823   t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1824   return build_vector_from_val (type, t);
1825 }
1826
1827 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
1828
1829 bool
1830 fold_convertible_p (const_tree type, const_tree arg)
1831 {
1832   tree orig = TREE_TYPE (arg);
1833
1834   if (type == orig)
1835     return true;
1836
1837   if (TREE_CODE (arg) == ERROR_MARK
1838       || TREE_CODE (type) == ERROR_MARK
1839       || TREE_CODE (orig) == ERROR_MARK)
1840     return false;
1841
1842   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1843     return true;
1844
1845   switch (TREE_CODE (type))
1846     {
1847     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1848     case POINTER_TYPE: case REFERENCE_TYPE:
1849     case OFFSET_TYPE:
1850       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1851           || TREE_CODE (orig) == OFFSET_TYPE)
1852         return true;
1853       return (TREE_CODE (orig) == VECTOR_TYPE
1854               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1855
1856     case REAL_TYPE:
1857     case FIXED_POINT_TYPE:
1858     case COMPLEX_TYPE:
1859     case VECTOR_TYPE:
1860     case VOID_TYPE:
1861       return TREE_CODE (type) == TREE_CODE (orig);
1862
1863     default:
1864       return false;
1865     }
1866 }
1867
1868 /* Convert expression ARG to type TYPE.  Used by the middle-end for
1869    simple conversions in preference to calling the front-end's convert.  */
1870
1871 tree
1872 fold_convert_loc (location_t loc, tree type, tree arg)
1873 {
1874   tree orig = TREE_TYPE (arg);
1875   tree tem;
1876
1877   if (type == orig)
1878     return arg;
1879
1880   if (TREE_CODE (arg) == ERROR_MARK
1881       || TREE_CODE (type) == ERROR_MARK
1882       || TREE_CODE (orig) == ERROR_MARK)
1883     return error_mark_node;
1884
1885   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1886     return fold_build1_loc (loc, NOP_EXPR, type, arg);
1887
1888   switch (TREE_CODE (type))
1889     {
1890     case POINTER_TYPE:
1891     case REFERENCE_TYPE:
1892       /* Handle conversions between pointers to different address spaces.  */
1893       if (POINTER_TYPE_P (orig)
1894           && (TYPE_ADDR_SPACE (TREE_TYPE (type))
1895               != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
1896         return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
1897       /* fall through */
1898
1899     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1900     case OFFSET_TYPE:
1901       if (TREE_CODE (arg) == INTEGER_CST)
1902         {
1903           tem = fold_convert_const (NOP_EXPR, type, arg);
1904           if (tem != NULL_TREE)
1905             return tem;
1906         }
1907       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1908           || TREE_CODE (orig) == OFFSET_TYPE)
1909         return fold_build1_loc (loc, NOP_EXPR, type, arg);
1910       if (TREE_CODE (orig) == COMPLEX_TYPE)
1911         return fold_convert_loc (loc, type,
1912                              fold_build1_loc (loc, REALPART_EXPR,
1913                                           TREE_TYPE (orig), arg));
1914       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
1915                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1916       return fold_build1_loc (loc, NOP_EXPR, type, arg);
1917
1918     case REAL_TYPE:
1919       if (TREE_CODE (arg) == INTEGER_CST)
1920         {
1921           tem = fold_convert_const (FLOAT_EXPR, type, arg);
1922           if (tem != NULL_TREE)
1923             return tem;
1924         }
1925       else if (TREE_CODE (arg) == REAL_CST)
1926         {
1927           tem = fold_convert_const (NOP_EXPR, type, arg);
1928           if (tem != NULL_TREE)
1929             return tem;
1930         }
1931       else if (TREE_CODE (arg) == FIXED_CST)
1932         {
1933           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1934           if (tem != NULL_TREE)
1935             return tem;
1936         }
1937
1938       switch (TREE_CODE (orig))
1939         {
1940         case INTEGER_TYPE:
1941         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1942         case POINTER_TYPE: case REFERENCE_TYPE:
1943           return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
1944
1945         case REAL_TYPE:
1946           return fold_build1_loc (loc, NOP_EXPR, type, arg);
1947
1948         case FIXED_POINT_TYPE:
1949           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1950
1951         case COMPLEX_TYPE:
1952           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1953           return fold_convert_loc (loc, type, tem);
1954
1955         default:
1956           gcc_unreachable ();
1957         }
1958
1959     case FIXED_POINT_TYPE:
1960       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
1961           || TREE_CODE (arg) == REAL_CST)
1962         {
1963           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1964           if (tem != NULL_TREE)
1965             goto fold_convert_exit;
1966         }
1967
1968       switch (TREE_CODE (orig))
1969         {
1970         case FIXED_POINT_TYPE:
1971         case INTEGER_TYPE:
1972         case ENUMERAL_TYPE:
1973         case BOOLEAN_TYPE:
1974         case REAL_TYPE:
1975           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1976
1977         case COMPLEX_TYPE:
1978           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1979           return fold_convert_loc (loc, type, tem);
1980
1981         default:
1982           gcc_unreachable ();
1983         }
1984
1985     case COMPLEX_TYPE:
1986       switch (TREE_CODE (orig))
1987         {
1988         case INTEGER_TYPE:
1989         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1990         case POINTER_TYPE: case REFERENCE_TYPE:
1991         case REAL_TYPE:
1992         case FIXED_POINT_TYPE:
1993           return fold_build2_loc (loc, COMPLEX_EXPR, type,
1994                               fold_convert_loc (loc, TREE_TYPE (type), arg),
1995                               fold_convert_loc (loc, TREE_TYPE (type),
1996                                             integer_zero_node));
1997         case COMPLEX_TYPE:
1998           {
1999             tree rpart, ipart;
2000
2001             if (TREE_CODE (arg) == COMPLEX_EXPR)
2002               {
2003                 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2004                                       TREE_OPERAND (arg, 0));
2005                 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2006                                       TREE_OPERAND (arg, 1));
2007                 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2008               }
2009
2010             arg = save_expr (arg);
2011             rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2012             ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2013             rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2014             ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2015             return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2016           }
2017
2018         default:
2019           gcc_unreachable ();
2020         }
2021
2022     case VECTOR_TYPE:
2023       if (integer_zerop (arg))
2024         return build_zero_vector (type);
2025       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2026       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2027                   || TREE_CODE (orig) == VECTOR_TYPE);
2028       return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2029
2030     case VOID_TYPE:
2031       tem = fold_ignored_result (arg);
2032       return fold_build1_loc (loc, NOP_EXPR, type, tem);
2033
2034     default:
2035       gcc_unreachable ();
2036     }
2037  fold_convert_exit:
2038   protected_set_expr_location_unshare (tem, loc);
2039   return tem;
2040 }
2041 \f
2042 /* Return false if expr can be assumed not to be an lvalue, true
2043    otherwise.  */
2044
2045 static bool
2046 maybe_lvalue_p (const_tree x)
2047 {
2048   /* We only need to wrap lvalue tree codes.  */
2049   switch (TREE_CODE (x))
2050   {
2051   case VAR_DECL:
2052   case PARM_DECL:
2053   case RESULT_DECL:
2054   case LABEL_DECL:
2055   case FUNCTION_DECL:
2056   case SSA_NAME:
2057
2058   case COMPONENT_REF:
2059   case MEM_REF:
2060   case INDIRECT_REF:
2061   case ARRAY_REF:
2062   case ARRAY_RANGE_REF:
2063   case BIT_FIELD_REF:
2064   case OBJ_TYPE_REF:
2065
2066   case REALPART_EXPR:
2067   case IMAGPART_EXPR:
2068   case PREINCREMENT_EXPR:
2069   case PREDECREMENT_EXPR:
2070   case SAVE_EXPR:
2071   case TRY_CATCH_EXPR:
2072   case WITH_CLEANUP_EXPR:
2073   case COMPOUND_EXPR:
2074   case MODIFY_EXPR:
2075   case TARGET_EXPR:
2076   case COND_EXPR:
2077   case BIND_EXPR:
2078     break;
2079
2080   default:
2081     /* Assume the worst for front-end tree codes.  */
2082     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2083       break;
2084     return false;
2085   }
2086
2087   return true;
2088 }
2089
2090 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2091
2092 tree
2093 non_lvalue_loc (location_t loc, tree x)
2094 {
2095   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2096      us.  */
2097   if (in_gimple_form)
2098     return x;
2099
2100   if (! maybe_lvalue_p (x))
2101     return x;
2102   return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2103 }
2104
2105 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2106    Zero means allow extended lvalues.  */
2107
2108 int pedantic_lvalues;
2109
2110 /* When pedantic, return an expr equal to X but certainly not valid as a
2111    pedantic lvalue.  Otherwise, return X.  */
2112
2113 static tree
2114 pedantic_non_lvalue_loc (location_t loc, tree x)
2115 {
2116   if (pedantic_lvalues)
2117     return non_lvalue_loc (loc, x);
2118
2119   return protected_set_expr_location_unshare (x, loc);
2120 }
2121 \f
2122 /* Given a tree comparison code, return the code that is the logical inverse
2123    of the given code.  It is not safe to do this for floating-point
2124    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2125    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2126
2127 enum tree_code
2128 invert_tree_comparison (enum tree_code code, bool honor_nans)
2129 {
2130   if (honor_nans && flag_trapping_math)
2131     return ERROR_MARK;
2132
2133   switch (code)
2134     {
2135     case EQ_EXPR:
2136       return NE_EXPR;
2137     case NE_EXPR:
2138       return EQ_EXPR;
2139     case GT_EXPR:
2140       return honor_nans ? UNLE_EXPR : LE_EXPR;
2141     case GE_EXPR:
2142       return honor_nans ? UNLT_EXPR : LT_EXPR;
2143     case LT_EXPR:
2144       return honor_nans ? UNGE_EXPR : GE_EXPR;
2145     case LE_EXPR:
2146       return honor_nans ? UNGT_EXPR : GT_EXPR;
2147     case LTGT_EXPR:
2148       return UNEQ_EXPR;
2149     case UNEQ_EXPR:
2150       return LTGT_EXPR;
2151     case UNGT_EXPR:
2152       return LE_EXPR;
2153     case UNGE_EXPR:
2154       return LT_EXPR;
2155     case UNLT_EXPR:
2156       return GE_EXPR;
2157     case UNLE_EXPR:
2158       return GT_EXPR;
2159     case ORDERED_EXPR:
2160       return UNORDERED_EXPR;
2161     case UNORDERED_EXPR:
2162       return ORDERED_EXPR;
2163     default:
2164       gcc_unreachable ();
2165     }
2166 }
2167
2168 /* Similar, but return the comparison that results if the operands are
2169    swapped.  This is safe for floating-point.  */
2170
2171 enum tree_code
2172 swap_tree_comparison (enum tree_code code)
2173 {
2174   switch (code)
2175     {
2176     case EQ_EXPR:
2177     case NE_EXPR:
2178     case ORDERED_EXPR:
2179     case UNORDERED_EXPR:
2180     case LTGT_EXPR:
2181     case UNEQ_EXPR:
2182       return code;
2183     case GT_EXPR:
2184       return LT_EXPR;
2185     case GE_EXPR:
2186       return LE_EXPR;
2187     case LT_EXPR:
2188       return GT_EXPR;
2189     case LE_EXPR:
2190       return GE_EXPR;
2191     case UNGT_EXPR:
2192       return UNLT_EXPR;
2193     case UNGE_EXPR:
2194       return UNLE_EXPR;
2195     case UNLT_EXPR:
2196       return UNGT_EXPR;
2197     case UNLE_EXPR:
2198       return UNGE_EXPR;
2199     default:
2200       gcc_unreachable ();
2201     }
2202 }
2203
2204
2205 /* Convert a comparison tree code from an enum tree_code representation
2206    into a compcode bit-based encoding.  This function is the inverse of
2207    compcode_to_comparison.  */
2208
2209 static enum comparison_code
2210 comparison_to_compcode (enum tree_code code)
2211 {
2212   switch (code)
2213     {
2214     case LT_EXPR:
2215       return COMPCODE_LT;
2216     case EQ_EXPR:
2217       return COMPCODE_EQ;
2218     case LE_EXPR:
2219       return COMPCODE_LE;
2220     case GT_EXPR:
2221       return COMPCODE_GT;
2222     case NE_EXPR:
2223       return COMPCODE_NE;
2224     case GE_EXPR:
2225       return COMPCODE_GE;
2226     case ORDERED_EXPR:
2227       return COMPCODE_ORD;
2228     case UNORDERED_EXPR:
2229       return COMPCODE_UNORD;
2230     case UNLT_EXPR:
2231       return COMPCODE_UNLT;
2232     case UNEQ_EXPR:
2233       return COMPCODE_UNEQ;
2234     case UNLE_EXPR:
2235       return COMPCODE_UNLE;
2236     case UNGT_EXPR:
2237       return COMPCODE_UNGT;
2238     case LTGT_EXPR:
2239       return COMPCODE_LTGT;
2240     case UNGE_EXPR:
2241       return COMPCODE_UNGE;
2242     default:
2243       gcc_unreachable ();
2244     }
2245 }
2246
2247 /* Convert a compcode bit-based encoding of a comparison operator back
2248    to GCC's enum tree_code representation.  This function is the
2249    inverse of comparison_to_compcode.  */
2250
2251 static enum tree_code
2252 compcode_to_comparison (enum comparison_code code)
2253 {
2254   switch (code)
2255     {
2256     case COMPCODE_LT:
2257       return LT_EXPR;
2258     case COMPCODE_EQ:
2259       return EQ_EXPR;
2260     case COMPCODE_LE:
2261       return LE_EXPR;
2262     case COMPCODE_GT:
2263       return GT_EXPR;
2264     case COMPCODE_NE:
2265       return NE_EXPR;
2266     case COMPCODE_GE:
2267       return GE_EXPR;
2268     case COMPCODE_ORD:
2269       return ORDERED_EXPR;
2270     case COMPCODE_UNORD:
2271       return UNORDERED_EXPR;
2272     case COMPCODE_UNLT:
2273       return UNLT_EXPR;
2274     case COMPCODE_UNEQ:
2275       return UNEQ_EXPR;
2276     case COMPCODE_UNLE:
2277       return UNLE_EXPR;
2278     case COMPCODE_UNGT:
2279       return UNGT_EXPR;
2280     case COMPCODE_LTGT:
2281       return LTGT_EXPR;
2282     case COMPCODE_UNGE:
2283       return UNGE_EXPR;
2284     default:
2285       gcc_unreachable ();
2286     }
2287 }
2288
2289 /* Return a tree for the comparison which is the combination of
2290    doing the AND or OR (depending on CODE) of the two operations LCODE
2291    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2292    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2293    if this makes the transformation invalid.  */
2294
2295 tree
2296 combine_comparisons (location_t loc,
2297                      enum tree_code code, enum tree_code lcode,
2298                      enum tree_code rcode, tree truth_type,
2299                      tree ll_arg, tree lr_arg)
2300 {
2301   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2302   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2303   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2304   int compcode;
2305
2306   switch (code)
2307     {
2308     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2309       compcode = lcompcode & rcompcode;
2310       break;
2311
2312     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2313       compcode = lcompcode | rcompcode;
2314       break;
2315
2316     default:
2317       return NULL_TREE;
2318     }
2319
2320   if (!honor_nans)
2321     {
2322       /* Eliminate unordered comparisons, as well as LTGT and ORD
2323          which are not used unless the mode has NaNs.  */
2324       compcode &= ~COMPCODE_UNORD;
2325       if (compcode == COMPCODE_LTGT)
2326         compcode = COMPCODE_NE;
2327       else if (compcode == COMPCODE_ORD)
2328         compcode = COMPCODE_TRUE;
2329     }
2330    else if (flag_trapping_math)
2331      {
2332         /* Check that the original operation and the optimized ones will trap
2333            under the same condition.  */
2334         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2335                      && (lcompcode != COMPCODE_EQ)
2336                      && (lcompcode != COMPCODE_ORD);
2337         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2338                      && (rcompcode != COMPCODE_EQ)
2339                      && (rcompcode != COMPCODE_ORD);
2340         bool trap = (compcode & COMPCODE_UNORD) == 0
2341                     && (compcode != COMPCODE_EQ)
2342                     && (compcode != COMPCODE_ORD);
2343
2344         /* In a short-circuited boolean expression the LHS might be
2345            such that the RHS, if evaluated, will never trap.  For
2346            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2347            if neither x nor y is NaN.  (This is a mixed blessing: for
2348            example, the expression above will never trap, hence
2349            optimizing it to x < y would be invalid).  */
2350         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2351             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2352           rtrap = false;
2353
2354         /* If the comparison was short-circuited, and only the RHS
2355            trapped, we may now generate a spurious trap.  */
2356         if (rtrap && !ltrap
2357             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2358           return NULL_TREE;
2359
2360         /* If we changed the conditions that cause a trap, we lose.  */
2361         if ((ltrap || rtrap) != trap)
2362           return NULL_TREE;
2363       }
2364
2365   if (compcode == COMPCODE_TRUE)
2366     return constant_boolean_node (true, truth_type);
2367   else if (compcode == COMPCODE_FALSE)
2368     return constant_boolean_node (false, truth_type);
2369   else
2370     {
2371       enum tree_code tcode;
2372
2373       tcode = compcode_to_comparison ((enum comparison_code) compcode);
2374       return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2375     }
2376 }
2377 \f
2378 /* Return nonzero if two operands (typically of the same tree node)
2379    are necessarily equal.  If either argument has side-effects this
2380    function returns zero.  FLAGS modifies behavior as follows:
2381
2382    If OEP_ONLY_CONST is set, only return nonzero for constants.
2383    This function tests whether the operands are indistinguishable;
2384    it does not test whether they are equal using C's == operation.
2385    The distinction is important for IEEE floating point, because
2386    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2387    (2) two NaNs may be indistinguishable, but NaN!=NaN.
2388
2389    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2390    even though it may hold multiple values during a function.
2391    This is because a GCC tree node guarantees that nothing else is
2392    executed between the evaluation of its "operands" (which may often
2393    be evaluated in arbitrary order).  Hence if the operands themselves
2394    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2395    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
2396    unset means assuming isochronic (or instantaneous) tree equivalence.
2397    Unless comparing arbitrary expression trees, such as from different
2398    statements, this flag can usually be left unset.
2399
2400    If OEP_PURE_SAME is set, then pure functions with identical arguments
2401    are considered the same.  It is used when the caller has other ways
2402    to ensure that global memory is unchanged in between.  */
2403
2404 int
2405 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2406 {
2407   /* If either is ERROR_MARK, they aren't equal.  */
2408   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2409       || TREE_TYPE (arg0) == error_mark_node
2410       || TREE_TYPE (arg1) == error_mark_node)
2411     return 0;
2412
2413   /* Similar, if either does not have a type (like a released SSA name), 
2414      they aren't equal.  */
2415   if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2416     return 0;
2417
2418   /* Check equality of integer constants before bailing out due to
2419      precision differences.  */
2420   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2421     return tree_int_cst_equal (arg0, arg1);
2422
2423   /* If both types don't have the same signedness, then we can't consider
2424      them equal.  We must check this before the STRIP_NOPS calls
2425      because they may change the signedness of the arguments.  As pointers
2426      strictly don't have a signedness, require either two pointers or
2427      two non-pointers as well.  */
2428   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2429       || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2430     return 0;
2431
2432   /* We cannot consider pointers to different address space equal.  */
2433   if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2434       && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2435           != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2436     return 0;
2437
2438   /* If both types don't have the same precision, then it is not safe
2439      to strip NOPs.  */
2440   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
2441     return 0;
2442
2443   STRIP_NOPS (arg0);
2444   STRIP_NOPS (arg1);
2445
2446   /* In case both args are comparisons but with different comparison
2447      code, try to swap the comparison operands of one arg to produce
2448      a match and compare that variant.  */
2449   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2450       && COMPARISON_CLASS_P (arg0)
2451       && COMPARISON_CLASS_P (arg1))
2452     {
2453       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2454
2455       if (TREE_CODE (arg0) == swap_code)
2456         return operand_equal_p (TREE_OPERAND (arg0, 0),
2457                                 TREE_OPERAND (arg1, 1), flags)
2458                && operand_equal_p (TREE_OPERAND (arg0, 1),
2459                                    TREE_OPERAND (arg1, 0), flags);
2460     }
2461
2462   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2463       /* This is needed for conversions and for COMPONENT_REF.
2464          Might as well play it safe and always test this.  */
2465       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2466       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2467       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2468     return 0;
2469
2470   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2471      We don't care about side effects in that case because the SAVE_EXPR
2472      takes care of that for us. In all other cases, two expressions are
2473      equal if they have no side effects.  If we have two identical
2474      expressions with side effects that should be treated the same due
2475      to the only side effects being identical SAVE_EXPR's, that will
2476      be detected in the recursive calls below.  */
2477   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2478       && (TREE_CODE (arg0) == SAVE_EXPR
2479           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2480     return 1;
2481
2482   /* Next handle constant cases, those for which we can return 1 even
2483      if ONLY_CONST is set.  */
2484   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2485     switch (TREE_CODE (arg0))
2486       {
2487       case INTEGER_CST:
2488         return tree_int_cst_equal (arg0, arg1);
2489
2490       case FIXED_CST:
2491         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2492                                        TREE_FIXED_CST (arg1));
2493
2494       case REAL_CST:
2495         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2496                                    TREE_REAL_CST (arg1)))
2497           return 1;
2498
2499
2500         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
2501           {
2502             /* If we do not distinguish between signed and unsigned zero,
2503                consider them equal.  */
2504             if (real_zerop (arg0) && real_zerop (arg1))
2505               return 1;
2506           }
2507         return 0;
2508
2509       case VECTOR_CST:
2510         {
2511           tree v1, v2;
2512
2513           v1 = TREE_VECTOR_CST_ELTS (arg0);
2514           v2 = TREE_VECTOR_CST_ELTS (arg1);
2515           while (v1 && v2)
2516             {
2517               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2518                                     flags))
2519                 return 0;
2520               v1 = TREE_CHAIN (v1);
2521               v2 = TREE_CHAIN (v2);
2522             }
2523
2524           return v1 == v2;
2525         }
2526
2527       case COMPLEX_CST:
2528         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2529                                  flags)
2530                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2531                                     flags));
2532
2533       case STRING_CST:
2534         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2535                 && ! memcmp (TREE_STRING_POINTER (arg0),
2536                               TREE_STRING_POINTER (arg1),
2537                               TREE_STRING_LENGTH (arg0)));
2538
2539       case ADDR_EXPR:
2540         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2541                                 0);
2542       default:
2543         break;
2544       }
2545
2546   if (flags & OEP_ONLY_CONST)
2547     return 0;
2548
2549 /* Define macros to test an operand from arg0 and arg1 for equality and a
2550    variant that allows null and views null as being different from any
2551    non-null value.  In the latter case, if either is null, the both
2552    must be; otherwise, do the normal comparison.  */
2553 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
2554                                     TREE_OPERAND (arg1, N), flags)
2555
2556 #define OP_SAME_WITH_NULL(N)                            \
2557   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2558    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2559
2560   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2561     {
2562     case tcc_unary:
2563       /* Two conversions are equal only if signedness and modes match.  */
2564       switch (TREE_CODE (arg0))
2565         {
2566         CASE_CONVERT:
2567         case FIX_TRUNC_EXPR:
2568           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2569               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2570             return 0;
2571           break;
2572         default:
2573           break;
2574         }
2575
2576       return OP_SAME (0);
2577
2578
2579     case tcc_comparison:
2580     case tcc_binary:
2581       if (OP_SAME (0) && OP_SAME (1))
2582         return 1;
2583
2584       /* For commutative ops, allow the other order.  */
2585       return (commutative_tree_code (TREE_CODE (arg0))
2586               && operand_equal_p (TREE_OPERAND (arg0, 0),
2587                                   TREE_OPERAND (arg1, 1), flags)
2588               && operand_equal_p (TREE_OPERAND (arg0, 1),
2589                                   TREE_OPERAND (arg1, 0), flags));
2590
2591     case tcc_reference:
2592       /* If either of the pointer (or reference) expressions we are
2593          dereferencing contain a side effect, these cannot be equal.  */
2594       if (TREE_SIDE_EFFECTS (arg0)
2595           || TREE_SIDE_EFFECTS (arg1))
2596         return 0;
2597
2598       switch (TREE_CODE (arg0))
2599         {
2600         case INDIRECT_REF:
2601         case REALPART_EXPR:
2602         case IMAGPART_EXPR:
2603           return OP_SAME (0);
2604
2605         case MEM_REF:
2606           /* Require equal access sizes, and similar pointer types.
2607              We can have incomplete types for array references of
2608              variable-sized arrays from the Fortran frontent
2609              though.  */
2610           return ((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1))
2611                    || (TYPE_SIZE (TREE_TYPE (arg0))
2612                        && TYPE_SIZE (TREE_TYPE (arg1))
2613                        && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
2614                                            TYPE_SIZE (TREE_TYPE (arg1)), flags)))
2615                   && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg0, 1)))
2616                       == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg1, 1))))
2617                   && OP_SAME (0) && OP_SAME (1));
2618
2619         case ARRAY_REF:
2620         case ARRAY_RANGE_REF:
2621           /* Operands 2 and 3 may be null.
2622              Compare the array index by value if it is constant first as we
2623              may have different types but same value here.  */
2624           return (OP_SAME (0)
2625                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2626                                           TREE_OPERAND (arg1, 1))
2627                       || OP_SAME (1))
2628                   && OP_SAME_WITH_NULL (2)
2629                   && OP_SAME_WITH_NULL (3));
2630
2631         case COMPONENT_REF:
2632           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
2633              may be NULL when we're called to compare MEM_EXPRs.  */
2634           return OP_SAME_WITH_NULL (0)
2635                  && OP_SAME (1)
2636                  && OP_SAME_WITH_NULL (2);
2637
2638         case BIT_FIELD_REF:
2639           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2640
2641         default:
2642           return 0;
2643         }
2644
2645     case tcc_expression:
2646       switch (TREE_CODE (arg0))
2647         {
2648         case ADDR_EXPR:
2649         case TRUTH_NOT_EXPR:
2650           return OP_SAME (0);
2651
2652         case TRUTH_ANDIF_EXPR:
2653         case TRUTH_ORIF_EXPR:
2654           return OP_SAME (0) && OP_SAME (1);
2655
2656         case FMA_EXPR:
2657         case WIDEN_MULT_PLUS_EXPR:
2658         case WIDEN_MULT_MINUS_EXPR:
2659           if (!OP_SAME (2))
2660             return 0;
2661           /* The multiplcation operands are commutative.  */
2662           /* FALLTHRU */
2663
2664         case TRUTH_AND_EXPR:
2665         case TRUTH_OR_EXPR:
2666         case TRUTH_XOR_EXPR:
2667           if (OP_SAME (0) && OP_SAME (1))
2668             return 1;
2669
2670           /* Otherwise take into account this is a commutative operation.  */
2671           return (operand_equal_p (TREE_OPERAND (arg0, 0),
2672                                    TREE_OPERAND (arg1, 1), flags)
2673                   && operand_equal_p (TREE_OPERAND (arg0, 1),
2674                                       TREE_OPERAND (arg1, 0), flags));
2675
2676         case COND_EXPR:
2677         case VEC_COND_EXPR:
2678         case DOT_PROD_EXPR:
2679           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2680
2681         default:
2682           return 0;
2683         }
2684
2685     case tcc_vl_exp:
2686       switch (TREE_CODE (arg0))
2687         {
2688         case CALL_EXPR:
2689           /* If the CALL_EXPRs call different functions, then they
2690              clearly can not be equal.  */
2691           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
2692                                  flags))
2693             return 0;
2694
2695           {
2696             unsigned int cef = call_expr_flags (arg0);
2697             if (flags & OEP_PURE_SAME)
2698               cef &= ECF_CONST | ECF_PURE;
2699             else
2700               cef &= ECF_CONST;
2701             if (!cef)
2702               return 0;
2703           }
2704
2705           /* Now see if all the arguments are the same.  */
2706           {
2707             const_call_expr_arg_iterator iter0, iter1;
2708             const_tree a0, a1;
2709             for (a0 = first_const_call_expr_arg (arg0, &iter0),
2710                    a1 = first_const_call_expr_arg (arg1, &iter1);
2711                  a0 && a1;
2712                  a0 = next_const_call_expr_arg (&iter0),
2713                    a1 = next_const_call_expr_arg (&iter1))
2714               if (! operand_equal_p (a0, a1, flags))
2715                 return 0;
2716
2717             /* If we get here and both argument lists are exhausted
2718                then the CALL_EXPRs are equal.  */
2719             return ! (a0 || a1);
2720           }
2721         default:
2722           return 0;
2723         }
2724
2725     case tcc_declaration:
2726       /* Consider __builtin_sqrt equal to sqrt.  */
2727       return (TREE_CODE (arg0) == FUNCTION_DECL
2728               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2729               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2730               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2731
2732     default:
2733       return 0;
2734     }
2735
2736 #undef OP_SAME
2737 #undef OP_SAME_WITH_NULL
2738 }
2739 \f
2740 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2741    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2742
2743    When in doubt, return 0.  */
2744
2745 static int
2746 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2747 {
2748   int unsignedp1, unsignedpo;
2749   tree primarg0, primarg1, primother;
2750   unsigned int correct_width;
2751
2752   if (operand_equal_p (arg0, arg1, 0))
2753     return 1;
2754
2755   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2756       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2757     return 0;
2758
2759   /* Discard any conversions that don't change the modes of ARG0 and ARG1
2760      and see if the inner values are the same.  This removes any
2761      signedness comparison, which doesn't matter here.  */
2762   primarg0 = arg0, primarg1 = arg1;
2763   STRIP_NOPS (primarg0);
2764   STRIP_NOPS (primarg1);
2765   if (operand_equal_p (primarg0, primarg1, 0))
2766     return 1;
2767
2768   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2769      actual comparison operand, ARG0.
2770
2771      First throw away any conversions to wider types
2772      already present in the operands.  */
2773
2774   primarg1 = get_narrower (arg1, &unsignedp1);
2775   primother = get_narrower (other, &unsignedpo);
2776
2777   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2778   if (unsignedp1 == unsignedpo
2779       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2780       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2781     {
2782       tree type = TREE_TYPE (arg0);
2783
2784       /* Make sure shorter operand is extended the right way
2785          to match the longer operand.  */
2786       primarg1 = fold_convert (signed_or_unsigned_type_for
2787                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2788
2789       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2790         return 1;
2791     }
2792
2793   return 0;
2794 }
2795 \f
2796 /* See if ARG is an expression that is either a comparison or is performing
2797    arithmetic on comparisons.  The comparisons must only be comparing
2798    two different values, which will be stored in *CVAL1 and *CVAL2; if
2799    they are nonzero it means that some operands have already been found.
2800    No variables may be used anywhere else in the expression except in the
2801    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
2802    the expression and save_expr needs to be called with CVAL1 and CVAL2.
2803
2804    If this is true, return 1.  Otherwise, return zero.  */
2805
2806 static int
2807 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2808 {
2809   enum tree_code code = TREE_CODE (arg);
2810   enum tree_code_class tclass = TREE_CODE_CLASS (code);
2811
2812   /* We can handle some of the tcc_expression cases here.  */
2813   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2814     tclass = tcc_unary;
2815   else if (tclass == tcc_expression
2816            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2817                || code == COMPOUND_EXPR))
2818     tclass = tcc_binary;
2819
2820   else if (tclass == tcc_expression && code == SAVE_EXPR
2821            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2822     {
2823       /* If we've already found a CVAL1 or CVAL2, this expression is
2824          two complex to handle.  */
2825       if (*cval1 || *cval2)
2826         return 0;
2827
2828       tclass = tcc_unary;
2829       *save_p = 1;
2830     }
2831
2832   switch (tclass)
2833     {
2834     case tcc_unary:
2835       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2836
2837     case tcc_binary:
2838       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2839               && twoval_comparison_p (TREE_OPERAND (arg, 1),
2840                                       cval1, cval2, save_p));
2841
2842     case tcc_constant:
2843       return 1;
2844
2845     case tcc_expression:
2846       if (code == COND_EXPR)
2847         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2848                                      cval1, cval2, save_p)
2849                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2850                                         cval1, cval2, save_p)
2851                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2852                                         cval1, cval2, save_p));
2853       return 0;
2854
2855     case tcc_comparison:
2856       /* First see if we can handle the first operand, then the second.  For
2857          the second operand, we know *CVAL1 can't be zero.  It must be that
2858          one side of the comparison is each of the values; test for the
2859          case where this isn't true by failing if the two operands
2860          are the same.  */
2861
2862       if (operand_equal_p (TREE_OPERAND (arg, 0),
2863                            TREE_OPERAND (arg, 1), 0))
2864         return 0;
2865
2866       if (*cval1 == 0)
2867         *cval1 = TREE_OPERAND (arg, 0);
2868       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2869         ;
2870       else if (*cval2 == 0)
2871         *cval2 = TREE_OPERAND (arg, 0);
2872       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2873         ;
2874       else
2875         return 0;
2876
2877       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2878         ;
2879       else if (*cval2 == 0)
2880         *cval2 = TREE_OPERAND (arg, 1);
2881       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2882         ;
2883       else
2884         return 0;
2885
2886       return 1;
2887
2888     default:
2889       return 0;
2890     }
2891 }
2892 \f
2893 /* ARG is a tree that is known to contain just arithmetic operations and
2894    comparisons.  Evaluate the operations in the tree substituting NEW0 for
2895    any occurrence of OLD0 as an operand of a comparison and likewise for
2896    NEW1 and OLD1.  */
2897
2898 static tree
2899 eval_subst (location_t loc, tree arg, tree old0, tree new0,
2900             tree old1, tree new1)
2901 {
2902   tree type = TREE_TYPE (arg);
2903   enum tree_code code = TREE_CODE (arg);
2904   enum tree_code_class tclass = TREE_CODE_CLASS (code);
2905
2906   /* We can handle some of the tcc_expression cases here.  */
2907   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2908     tclass = tcc_unary;
2909   else if (tclass == tcc_expression
2910            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2911     tclass = tcc_binary;
2912
2913   switch (tclass)
2914     {
2915     case tcc_unary:
2916       return fold_build1_loc (loc, code, type,
2917                           eval_subst (loc, TREE_OPERAND (arg, 0),
2918                                       old0, new0, old1, new1));
2919
2920     case tcc_binary:
2921       return fold_build2_loc (loc, code, type,
2922                           eval_subst (loc, TREE_OPERAND (arg, 0),
2923                                       old0, new0, old1, new1),
2924                           eval_subst (loc, TREE_OPERAND (arg, 1),
2925                                       old0, new0, old1, new1));
2926
2927     case tcc_expression:
2928       switch (code)
2929         {
2930         case SAVE_EXPR:
2931           return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
2932                              old1, new1);
2933
2934         case COMPOUND_EXPR:
2935           return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
2936                              old1, new1);
2937
2938         case COND_EXPR:
2939           return fold_build3_loc (loc, code, type,
2940                               eval_subst (loc, TREE_OPERAND (arg, 0),
2941                                           old0, new0, old1, new1),
2942                               eval_subst (loc, TREE_OPERAND (arg, 1),
2943                                           old0, new0, old1, new1),
2944                               eval_subst (loc, TREE_OPERAND (arg, 2),
2945                                           old0, new0, old1, new1));
2946         default:
2947           break;
2948         }
2949       /* Fall through - ???  */
2950
2951     case tcc_comparison:
2952       {
2953         tree arg0 = TREE_OPERAND (arg, 0);
2954         tree arg1 = TREE_OPERAND (arg, 1);
2955
2956         /* We need to check both for exact equality and tree equality.  The
2957            former will be true if the operand has a side-effect.  In that
2958            case, we know the operand occurred exactly once.  */
2959
2960         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2961           arg0 = new0;
2962         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2963           arg0 = new1;
2964
2965         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2966           arg1 = new0;
2967         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2968           arg1 = new1;
2969
2970         return fold_build2_loc (loc, code, type, arg0, arg1);
2971       }
2972
2973     default:
2974       return arg;
2975     }
2976 }
2977 \f
2978 /* Return a tree for the case when the result of an expression is RESULT
2979    converted to TYPE and OMITTED was previously an operand of the expression
2980    but is now not needed (e.g., we folded OMITTED * 0).
2981
2982    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
2983    the conversion of RESULT to TYPE.  */
2984
2985 tree
2986 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
2987 {
2988   tree t = fold_convert_loc (loc, type, result);
2989
2990   /* If the resulting operand is an empty statement, just return the omitted
2991      statement casted to void. */
2992   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
2993     return build1_loc (loc, NOP_EXPR, void_type_node,
2994                        fold_ignored_result (omitted));
2995
2996   if (TREE_SIDE_EFFECTS (omitted))
2997     return build2_loc (loc, COMPOUND_EXPR, type,
2998                        fold_ignored_result (omitted), t);
2999
3000   return non_lvalue_loc (loc, t);
3001 }
3002
3003 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
3004
3005 static tree
3006 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
3007                                tree omitted)
3008 {
3009   tree t = fold_convert_loc (loc, type, result);
3010
3011   /* If the resulting operand is an empty statement, just return the omitted
3012      statement casted to void. */
3013   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3014     return build1_loc (loc, NOP_EXPR, void_type_node,
3015                        fold_ignored_result (omitted));
3016
3017   if (TREE_SIDE_EFFECTS (omitted))
3018     return build2_loc (loc, COMPOUND_EXPR, type,
3019                        fold_ignored_result (omitted), t);
3020
3021   return pedantic_non_lvalue_loc (loc, t);
3022 }
3023
3024 /* Return a tree for the case when the result of an expression is RESULT
3025    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3026    of the expression but are now not needed.
3027
3028    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3029    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3030    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3031    just do the conversion of RESULT to TYPE.  */
3032
3033 tree
3034 omit_two_operands_loc (location_t loc, tree type, tree result,
3035                        tree omitted1, tree omitted2)
3036 {
3037   tree t = fold_convert_loc (loc, type, result);
3038
3039   if (TREE_SIDE_EFFECTS (omitted2))
3040     t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3041   if (TREE_SIDE_EFFECTS (omitted1))
3042     t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3043
3044   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3045 }
3046
3047 \f
3048 /* Return a simplified tree node for the truth-negation of ARG.  This
3049    never alters ARG itself.  We assume that ARG is an operation that
3050    returns a truth value (0 or 1).
3051
3052    FIXME: one would think we would fold the result, but it causes
3053    problems with the dominator optimizer.  */
3054
3055 tree
3056 fold_truth_not_expr (location_t loc, tree arg)
3057 {
3058   tree type = TREE_TYPE (arg);
3059   enum tree_code code = TREE_CODE (arg);
3060   location_t loc1, loc2;
3061
3062   /* If this is a comparison, we can simply invert it, except for
3063      floating-point non-equality comparisons, in which case we just
3064      enclose a TRUTH_NOT_EXPR around what we have.  */
3065
3066   if (TREE_CODE_CLASS (code) == tcc_comparison)
3067     {
3068       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3069       if (FLOAT_TYPE_P (op_type)
3070           && flag_trapping_math
3071           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3072           && code != NE_EXPR && code != EQ_EXPR)
3073         return NULL_TREE;
3074
3075       code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3076       if (code == ERROR_MARK)
3077         return NULL_TREE;
3078
3079       return build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3080                          TREE_OPERAND (arg, 1));
3081     }
3082
3083   switch (code)
3084     {
3085     case INTEGER_CST:
3086       return constant_boolean_node (integer_zerop (arg), type);
3087
3088     case TRUTH_AND_EXPR:
3089       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3090       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3091       return build2_loc (loc, TRUTH_OR_EXPR, type,
3092                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3093                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3094
3095     case TRUTH_OR_EXPR:
3096       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3097       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3098       return build2_loc (loc, TRUTH_AND_EXPR, type,
3099                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3100                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3101
3102     case TRUTH_XOR_EXPR:
3103       /* Here we can invert either operand.  We invert the first operand
3104          unless the second operand is a TRUTH_NOT_EXPR in which case our
3105          result is the XOR of the first operand with the inside of the
3106          negation of the second operand.  */
3107
3108       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3109         return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3110                            TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3111       else
3112         return build2_loc (loc, TRUTH_XOR_EXPR, type,
3113                            invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3114                            TREE_OPERAND (arg, 1));
3115
3116     case TRUTH_ANDIF_EXPR:
3117       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3118       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3119       return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3120                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3121                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3122
3123     case TRUTH_ORIF_EXPR:
3124       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3125       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3126       return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3127                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3128                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3129
3130     case TRUTH_NOT_EXPR:
3131       return TREE_OPERAND (arg, 0);
3132
3133     case COND_EXPR:
3134       {
3135         tree arg1 = TREE_OPERAND (arg, 1);
3136         tree arg2 = TREE_OPERAND (arg, 2);
3137
3138         loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3139         loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3140
3141         /* A COND_EXPR may have a throw as one operand, which
3142            then has void type.  Just leave void operands
3143            as they are.  */
3144         return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3145                            VOID_TYPE_P (TREE_TYPE (arg1))
3146                            ? arg1 : invert_truthvalue_loc (loc1, arg1),
3147                            VOID_TYPE_P (TREE_TYPE (arg2))
3148                            ? arg2 : invert_truthvalue_loc (loc2, arg2));
3149       }
3150
3151     case COMPOUND_EXPR:
3152       loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3153       return build2_loc (loc, COMPOUND_EXPR, type,
3154                          TREE_OPERAND (arg, 0),
3155                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3156
3157     case NON_LVALUE_EXPR:
3158       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3159       return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3160
3161     CASE_CONVERT:
3162       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3163         return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3164
3165       /* ... fall through ...  */
3166
3167     case FLOAT_EXPR:
3168       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3169       return build1_loc (loc, TREE_CODE (arg), type,
3170                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3171
3172     case BIT_AND_EXPR:
3173       if (!integer_onep (TREE_OPERAND (arg, 1)))
3174         return NULL_TREE;
3175       return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3176
3177     case SAVE_EXPR:
3178       return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3179
3180     case CLEANUP_POINT_EXPR:
3181       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3182       return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3183                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3184
3185     default:
3186       return NULL_TREE;
3187     }
3188 }
3189
3190 /* Return a simplified tree node for the truth-negation of ARG.  This
3191    never alters ARG itself.  We assume that ARG is an operation that
3192    returns a truth value (0 or 1).
3193
3194    FIXME: one would think we would fold the result, but it causes
3195    problems with the dominator optimizer.  */
3196
3197 tree
3198 invert_truthvalue_loc (location_t loc, tree arg)
3199 {
3200   tree tem;
3201
3202   if (TREE_CODE (arg) == ERROR_MARK)
3203     return arg;
3204
3205   tem = fold_truth_not_expr (loc, arg);
3206   if (!tem)
3207     tem = build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3208
3209   return tem;
3210 }
3211
3212 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3213    operands are another bit-wise operation with a common input.  If so,
3214    distribute the bit operations to save an operation and possibly two if
3215    constants are involved.  For example, convert
3216         (A | B) & (A | C) into A | (B & C)
3217    Further simplification will occur if B and C are constants.
3218
3219    If this optimization cannot be done, 0 will be returned.  */
3220
3221 static tree
3222 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
3223                      tree arg0, tree arg1)
3224 {
3225   tree common;
3226   tree left, right;
3227
3228   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3229       || TREE_CODE (arg0) == code
3230       || (TREE_CODE (arg0) != BIT_AND_EXPR
3231           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3232     return 0;
3233
3234   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3235     {
3236       common = TREE_OPERAND (arg0, 0);
3237       left = TREE_OPERAND (arg0, 1);
3238       right = TREE_OPERAND (arg1, 1);
3239     }
3240   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3241     {
3242       common = TREE_OPERAND (arg0, 0);
3243       left = TREE_OPERAND (arg0, 1);
3244       right = TREE_OPERAND (arg1, 0);
3245     }
3246   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3247     {
3248       common = TREE_OPERAND (arg0, 1);
3249       left = TREE_OPERAND (arg0, 0);
3250       right = TREE_OPERAND (arg1, 1);
3251     }
3252   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3253     {
3254       common = TREE_OPERAND (arg0, 1);
3255       left = TREE_OPERAND (arg0, 0);
3256       right = TREE_OPERAND (arg1, 0);
3257     }
3258   else
3259     return 0;
3260
3261   common = fold_convert_loc (loc, type, common);
3262   left = fold_convert_loc (loc, type, left);
3263   right = fold_convert_loc (loc, type, right);
3264   return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
3265                       fold_build2_loc (loc, code, type, left, right));
3266 }
3267
3268 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3269    with code CODE.  This optimization is unsafe.  */
3270 static tree
3271 distribute_real_division (location_t loc, enum tree_code code, tree type,
3272                           tree arg0, tree arg1)
3273 {
3274   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3275   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3276
3277   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3278   if (mul0 == mul1
3279       && operand_equal_p (TREE_OPERAND (arg0, 1),
3280                        TREE_OPERAND (arg1, 1), 0))
3281     return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3282                         fold_build2_loc (loc, code, type,
3283                                      TREE_OPERAND (arg0, 0),
3284                                      TREE_OPERAND (arg1, 0)),
3285                         TREE_OPERAND (arg0, 1));
3286
3287   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3288   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3289                        TREE_OPERAND (arg1, 0), 0)
3290       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3291       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3292     {
3293       REAL_VALUE_TYPE r0, r1;
3294       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3295       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3296       if (!mul0)
3297         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3298       if (!mul1)
3299         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3300       real_arithmetic (&r0, code, &r0, &r1);
3301       return fold_build2_loc (loc, MULT_EXPR, type,
3302                           TREE_OPERAND (arg0, 0),
3303                           build_real (type, r0));
3304     }
3305
3306   return NULL_TREE;
3307 }
3308 \f
3309 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3310    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3311
3312 static tree
3313 make_bit_field_ref (location_t loc, tree inner, tree type,
3314                     HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3315 {
3316   tree result, bftype;
3317
3318   if (bitpos == 0)
3319     {
3320       tree size = TYPE_SIZE (TREE_TYPE (inner));
3321       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3322            || POINTER_TYPE_P (TREE_TYPE (inner)))
3323           && host_integerp (size, 0)
3324           && tree_low_cst (size, 0) == bitsize)
3325         return fold_convert_loc (loc, type, inner);
3326     }
3327
3328   bftype = type;
3329   if (TYPE_PRECISION (bftype) != bitsize
3330       || TYPE_UNSIGNED (bftype) == !unsignedp)
3331     bftype = build_nonstandard_integer_type (bitsize, 0);
3332
3333   result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3334                        size_int (bitsize), bitsize_int (bitpos));
3335
3336   if (bftype != type)
3337     result = fold_convert_loc (loc, type, result);
3338
3339   return result;
3340 }
3341
3342 /* Optimize a bit-field compare.
3343
3344    There are two cases:  First is a compare against a constant and the
3345    second is a comparison of two items where the fields are at the same
3346    bit position relative to the start of a chunk (byte, halfword, word)
3347    large enough to contain it.  In these cases we can avoid the shift
3348    implicit in bitfield extractions.
3349
3350    For constants, we emit a compare of the shifted constant with the
3351    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3352    compared.  For two fields at the same position, we do the ANDs with the
3353    similar mask and compare the result of the ANDs.
3354
3355    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3356    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3357    are the left and right operands of the comparison, respectively.
3358
3359    If the optimization described above can be done, we return the resulting
3360    tree.  Otherwise we return zero.  */
3361
3362 static tree
3363 optimize_bit_field_compare (location_t loc, enum tree_code code,
3364                             tree compare_type, tree lhs, tree rhs)
3365 {
3366   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3367   tree type = TREE_TYPE (lhs);
3368   tree signed_type, unsigned_type;
3369   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3370   enum machine_mode lmode, rmode, nmode;
3371   int lunsignedp, runsignedp;
3372   int lvolatilep = 0, rvolatilep = 0;
3373   tree linner, rinner = NULL_TREE;
3374   tree mask;
3375   tree offset;
3376
3377   /* Get all the information about the extractions being done.  If the bit size
3378      if the same as the size of the underlying object, we aren't doing an
3379      extraction at all and so can do nothing.  We also don't want to
3380      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3381      then will no longer be able to replace it.  */
3382   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3383                                 &lunsignedp, &lvolatilep, false);
3384   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3385       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3386     return 0;
3387
3388  if (!const_p)
3389    {
3390      /* If this is not a constant, we can only do something if bit positions,
3391         sizes, and signedness are the same.  */
3392      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3393                                    &runsignedp, &rvolatilep, false);
3394
3395      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3396          || lunsignedp != runsignedp || offset != 0
3397          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3398        return 0;
3399    }
3400
3401   /* See if we can find a mode to refer to this field.  We should be able to,
3402      but fail if we can't.  */
3403   if (lvolatilep
3404       && GET_MODE_BITSIZE (lmode) > 0
3405       && flag_strict_volatile_bitfields > 0)
3406     nmode = lmode;
3407   else
3408     nmode = get_best_mode (lbitsize, lbitpos,
3409                            const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3410                            : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3411                                   TYPE_ALIGN (TREE_TYPE (rinner))),
3412                            word_mode, lvolatilep || rvolatilep);
3413   if (nmode == VOIDmode)
3414     return 0;
3415
3416   /* Set signed and unsigned types of the precision of this mode for the
3417      shifts below.  */
3418   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3419   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3420
3421   /* Compute the bit position and size for the new reference and our offset
3422      within it. If the new reference is the same size as the original, we
3423      won't optimize anything, so return zero.  */
3424   nbitsize = GET_MODE_BITSIZE (nmode);
3425   nbitpos = lbitpos & ~ (nbitsize - 1);
3426   lbitpos -= nbitpos;
3427   if (nbitsize == lbitsize)
3428     return 0;
3429
3430   if (BYTES_BIG_ENDIAN)
3431     lbitpos = nbitsize - lbitsize - lbitpos;
3432
3433   /* Make the mask to be used against the extracted field.  */
3434   mask = build_int_cst_type (unsigned_type, -1);
3435   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3436   mask = const_binop (RSHIFT_EXPR, mask,
3437                       size_int (nbitsize - lbitsize - lbitpos));
3438
3439   if (! const_p)
3440     /* If not comparing with constant, just rework the comparison
3441        and return.  */
3442     return fold_build2_loc (loc, code, compare_type,
3443                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3444                                      make_bit_field_ref (loc, linner,
3445                                                          unsigned_type,
3446                                                          nbitsize, nbitpos,
3447                                                          1),
3448                                      mask),
3449                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3450                                      make_bit_field_ref (loc, rinner,
3451                                                          unsigned_type,
3452                                                          nbitsize, nbitpos,
3453                                                          1),
3454                                      mask));
3455
3456   /* Otherwise, we are handling the constant case. See if the constant is too
3457      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
3458      this not only for its own sake, but to avoid having to test for this
3459      error case below.  If we didn't, we might generate wrong code.
3460
3461      For unsigned fields, the constant shifted right by the field length should
3462      be all zero.  For signed fields, the high-order bits should agree with
3463      the sign bit.  */
3464
3465   if (lunsignedp)
3466     {
3467       if (! integer_zerop (const_binop (RSHIFT_EXPR,
3468                                         fold_convert_loc (loc,
3469                                                           unsigned_type, rhs),
3470                                         size_int (lbitsize))))
3471         {
3472           warning (0, "comparison is always %d due to width of bit-field",
3473                    code == NE_EXPR);
3474           return constant_boolean_node (code == NE_EXPR, compare_type);
3475         }
3476     }
3477   else
3478     {
3479       tree tem = const_binop (RSHIFT_EXPR,
3480                               fold_convert_loc (loc, signed_type, rhs),
3481                               size_int (lbitsize - 1));
3482       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3483         {
3484           warning (0, "comparison is always %d due to width of bit-field",
3485                    code == NE_EXPR);
3486           return constant_boolean_node (code == NE_EXPR, compare_type);
3487         }
3488     }
3489
3490   /* Single-bit compares should always be against zero.  */
3491   if (lbitsize == 1 && ! integer_zerop (rhs))
3492     {
3493       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3494       rhs = build_int_cst (type, 0);
3495     }
3496
3497   /* Make a new bitfield reference, shift the constant over the
3498      appropriate number of bits and mask it with the computed mask
3499      (in case this was a signed field).  If we changed it, make a new one.  */
3500   lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3501   if (lvolatilep)
3502     {
3503       TREE_SIDE_EFFECTS (lhs) = 1;
3504       TREE_THIS_VOLATILE (lhs) = 1;
3505     }
3506
3507   rhs = const_binop (BIT_AND_EXPR,
3508                      const_binop (LSHIFT_EXPR,
3509                                   fold_convert_loc (loc, unsigned_type, rhs),
3510                                   size_int (lbitpos)),
3511                      mask);
3512
3513   lhs = build2_loc (loc, code, compare_type,
3514                     build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
3515   return lhs;
3516 }
3517 \f
3518 /* Subroutine for fold_truthop: decode a field reference.
3519
3520    If EXP is a comparison reference, we return the innermost reference.
3521
3522    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3523    set to the starting bit number.
3524
3525    If the innermost field can be completely contained in a mode-sized
3526    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
3527
3528    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3529    otherwise it is not changed.
3530
3531    *PUNSIGNEDP is set to the signedness of the field.
3532
3533    *PMASK is set to the mask used.  This is either contained in a
3534    BIT_AND_EXPR or derived from the width of the field.
3535
3536    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3537
3538    Return 0 if this is not a component reference or is one that we can't
3539    do anything with.  */
3540
3541 static tree
3542 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3543                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3544                         int *punsignedp, int *pvolatilep,
3545                         tree *pmask, tree *pand_mask)
3546 {
3547   tree outer_type = 0;
3548   tree and_mask = 0;
3549   tree mask, inner, offset;
3550   tree unsigned_type;
3551   unsigned int precision;
3552
3553   /* All the optimizations using this function assume integer fields.
3554      There are problems with FP fields since the type_for_size call
3555      below can fail for, e.g., XFmode.  */
3556   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3557     return 0;
3558
3559   /* We are interested in the bare arrangement of bits, so strip everything
3560      that doesn't affect the machine mode.  However, record the type of the
3561      outermost expression if it may matter below.  */
3562   if (CONVERT_EXPR_P (exp)
3563       || TREE_CODE (exp) == NON_LVALUE_EXPR)
3564     outer_type = TREE_TYPE (exp);
3565   STRIP_NOPS (exp);
3566
3567   if (TREE_CODE (exp) == BIT_AND_EXPR)
3568     {
3569       and_mask = TREE_OPERAND (exp, 1);
3570       exp = TREE_OPERAND (exp, 0);
3571       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3572       if (TREE_CODE (and_mask) != INTEGER_CST)
3573         return 0;
3574     }
3575
3576   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3577                                punsignedp, pvolatilep, false);
3578   if ((inner == exp && and_mask == 0)
3579       || *pbitsize < 0 || offset != 0
3580       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3581     return 0;
3582
3583   /* If the number of bits in the reference is the same as the bitsize of
3584      the outer type, then the outer type gives the signedness. Otherwise
3585      (in case of a small bitfield) the signedness is unchanged.  */
3586   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3587     *punsignedp = TYPE_UNSIGNED (outer_type);
3588
3589   /* Compute the mask to access the bitfield.  */
3590   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3591   precision = TYPE_PRECISION (unsigned_type);
3592
3593   mask = build_int_cst_type (unsigned_type, -1);
3594
3595   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3596   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3597
3598   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
3599   if (and_mask != 0)
3600     mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3601                         fold_convert_loc (loc, unsigned_type, and_mask), mask);
3602
3603   *pmask = mask;
3604   *pand_mask = and_mask;
3605   return inner;
3606 }
3607
3608 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3609    bit positions.  */
3610
3611 static int
3612 all_ones_mask_p (const_tree mask, int size)
3613 {
3614   tree type = TREE_TYPE (mask);
3615   unsigned int precision = TYPE_PRECISION (type);
3616   tree tmask;
3617
3618   tmask = build_int_cst_type (signed_type_for (type), -1);
3619
3620   return
3621     tree_int_cst_equal (mask,
3622                         const_binop (RSHIFT_EXPR,
3623                                      const_binop (LSHIFT_EXPR, tmask,
3624                                                   size_int (precision - size)),
3625                                      size_int (precision - size)));
3626 }
3627
3628 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3629    represents the sign bit of EXP's type.  If EXP represents a sign
3630    or zero extension, also test VAL against the unextended type.
3631    The return value is the (sub)expression whose sign bit is VAL,
3632    or NULL_TREE otherwise.  */
3633
3634 static tree
3635 sign_bit_p (tree exp, const_tree val)
3636 {
3637   unsigned HOST_WIDE_INT mask_lo, lo;
3638   HOST_WIDE_INT mask_hi, hi;
3639   int width;
3640   tree t;
3641
3642   /* Tree EXP must have an integral type.  */
3643   t = TREE_TYPE (exp);
3644   if (! INTEGRAL_TYPE_P (t))
3645     return NULL_TREE;
3646
3647   /* Tree VAL must be an integer constant.  */
3648   if (TREE_CODE (val) != INTEGER_CST
3649       || TREE_OVERFLOW (val))
3650     return NULL_TREE;
3651
3652   width = TYPE_PRECISION (t);
3653   if (width > HOST_BITS_PER_WIDE_INT)
3654     {
3655       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3656       lo = 0;
3657
3658       mask_hi = ((unsigned HOST_WIDE_INT) -1
3659                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
3660       mask_lo = -1;
3661     }
3662   else
3663     {
3664       hi = 0;
3665       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3666
3667       mask_hi = 0;
3668       mask_lo = ((unsigned HOST_WIDE_INT) -1
3669                  >> (HOST_BITS_PER_WIDE_INT - width));
3670     }
3671
3672   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3673      treat VAL as if it were unsigned.  */
3674   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3675       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3676     return exp;
3677
3678   /* Handle extension from a narrower type.  */
3679   if (TREE_CODE (exp) == NOP_EXPR
3680       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3681     return sign_bit_p (TREE_OPERAND (exp, 0), val);
3682
3683   return NULL_TREE;
3684 }
3685
3686 /* Subroutine for fold_truthop: determine if an operand is simple enough
3687    to be evaluated unconditionally.  */
3688
3689 static int
3690 simple_operand_p (const_tree exp)
3691 {
3692   /* Strip any conversions that don't change the machine mode.  */
3693   STRIP_NOPS (exp);
3694
3695   return (CONSTANT_CLASS_P (exp)
3696           || TREE_CODE (exp) == SSA_NAME
3697           || (DECL_P (exp)
3698               && ! TREE_ADDRESSABLE (exp)
3699               && ! TREE_THIS_VOLATILE (exp)
3700               && ! DECL_NONLOCAL (exp)
3701               /* Don't regard global variables as simple.  They may be
3702                  allocated in ways unknown to the compiler (shared memory,
3703                  #pragma weak, etc).  */
3704               && ! TREE_PUBLIC (exp)
3705               && ! DECL_EXTERNAL (exp)
3706               /* Loading a static variable is unduly expensive, but global
3707                  registers aren't expensive.  */
3708               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3709 }
3710 \f
3711 /* The following functions are subroutines to fold_range_test and allow it to
3712    try to change a logical combination of comparisons into a range test.
3713
3714    For example, both
3715         X == 2 || X == 3 || X == 4 || X == 5
3716    and
3717         X >= 2 && X <= 5
3718    are converted to
3719         (unsigned) (X - 2) <= 3
3720
3721    We describe each set of comparisons as being either inside or outside
3722    a range, using a variable named like IN_P, and then describe the
3723    range with a lower and upper bound.  If one of the bounds is omitted,
3724    it represents either the highest or lowest value of the type.
3725
3726    In the comments below, we represent a range by two numbers in brackets
3727    preceded by a "+" to designate being inside that range, or a "-" to
3728    designate being outside that range, so the condition can be inverted by
3729    flipping the prefix.  An omitted bound is represented by a "-".  For
3730    example, "- [-, 10]" means being outside the range starting at the lowest
3731    possible value and ending at 10, in other words, being greater than 10.
3732    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3733    always false.
3734
3735    We set up things so that the missing bounds are handled in a consistent
3736    manner so neither a missing bound nor "true" and "false" need to be
3737    handled using a special case.  */
3738
3739 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3740    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3741    and UPPER1_P are nonzero if the respective argument is an upper bound
3742    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
3743    must be specified for a comparison.  ARG1 will be converted to ARG0's
3744    type if both are specified.  */
3745
3746 static tree
3747 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3748              tree arg1, int upper1_p)
3749 {
3750   tree tem;
3751   int result;
3752   int sgn0, sgn1;
3753
3754   /* If neither arg represents infinity, do the normal operation.
3755      Else, if not a comparison, return infinity.  Else handle the special
3756      comparison rules. Note that most of the cases below won't occur, but
3757      are handled for consistency.  */
3758
3759   if (arg0 != 0 && arg1 != 0)
3760     {
3761       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3762                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
3763       STRIP_NOPS (tem);
3764       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3765     }
3766
3767   if (TREE_CODE_CLASS (code) != tcc_comparison)
3768     return 0;
3769
3770   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3771      for neither.  In real maths, we cannot assume open ended ranges are
3772      the same. But, this is computer arithmetic, where numbers are finite.
3773      We can therefore make the transformation of any unbounded range with
3774      the value Z, Z being greater than any representable number. This permits
3775      us to treat unbounded ranges as equal.  */
3776   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3777   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3778   switch (code)
3779     {
3780     case EQ_EXPR:
3781       result = sgn0 == sgn1;
3782       break;
3783     case NE_EXPR:
3784       result = sgn0 != sgn1;
3785       break;
3786     case LT_EXPR:
3787       result = sgn0 < sgn1;
3788       break;
3789     case LE_EXPR:
3790       result = sgn0 <= sgn1;
3791       break;
3792     case GT_EXPR:
3793       result = sgn0 > sgn1;
3794       break;
3795     case GE_EXPR:
3796       result = sgn0 >= sgn1;
3797       break;
3798     default:
3799       gcc_unreachable ();
3800     }
3801
3802   return constant_boolean_node (result, type);
3803 }
3804 \f
3805 /* Given EXP, a logical expression, set the range it is testing into
3806    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
3807    actually being tested.  *PLOW and *PHIGH will be made of the same
3808    type as the returned expression.  If EXP is not a comparison, we
3809    will most likely not be returning a useful value and range.  Set
3810    *STRICT_OVERFLOW_P to true if the return value is only valid
3811    because signed overflow is undefined; otherwise, do not change
3812    *STRICT_OVERFLOW_P.  */
3813
3814 tree
3815 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
3816             bool *strict_overflow_p)
3817 {
3818   enum tree_code code;
3819   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
3820   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
3821   int in_p, n_in_p;
3822   tree low, high, n_low, n_high;
3823   location_t loc = EXPR_LOCATION (exp);
3824
3825   /* Start with simply saying "EXP != 0" and then look at the code of EXP
3826      and see if we can refine the range.  Some of the cases below may not
3827      happen, but it doesn't seem worth worrying about this.  We "continue"
3828      the outer loop when we've changed something; otherwise we "break"
3829      the switch, which will "break" the while.  */
3830
3831   in_p = 0;
3832   low = high = build_int_cst (TREE_TYPE (exp), 0);
3833
3834   while (1)
3835     {
3836       code = TREE_CODE (exp);
3837       exp_type = TREE_TYPE (exp);
3838
3839       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3840         {
3841           if (TREE_OPERAND_LENGTH (exp) > 0)
3842             arg0 = TREE_OPERAND (exp, 0);
3843           if (TREE_CODE_CLASS (code) == tcc_comparison
3844               || TREE_CODE_CLASS (code) == tcc_unary
3845               || TREE_CODE_CLASS (code) == tcc_binary)
3846             arg0_type = TREE_TYPE (arg0);
3847           if (TREE_CODE_CLASS (code) == tcc_binary
3848               || TREE_CODE_CLASS (code) == tcc_comparison
3849               || (TREE_CODE_CLASS (code) == tcc_expression
3850                   && TREE_OPERAND_LENGTH (exp) > 1))
3851             arg1 = TREE_OPERAND (exp, 1);
3852         }
3853
3854       switch (code)
3855         {
3856         case TRUTH_NOT_EXPR:
3857           in_p = ! in_p, exp = arg0;
3858           continue;
3859
3860         case EQ_EXPR: case NE_EXPR:
3861         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3862           /* We can only do something if the range is testing for zero
3863              and if the second operand is an integer constant.  Note that
3864              saying something is "in" the range we make is done by
3865              complementing IN_P since it will set in the initial case of
3866              being not equal to zero; "out" is leaving it alone.  */
3867           if (low == 0 || high == 0
3868               || ! integer_zerop (low) || ! integer_zerop (high)
3869               || TREE_CODE (arg1) != INTEGER_CST)
3870             break;
3871
3872           switch (code)
3873             {
3874             case NE_EXPR:  /* - [c, c]  */
3875               low = high = arg1;
3876               break;
3877             case EQ_EXPR:  /* + [c, c]  */
3878               in_p = ! in_p, low = high = arg1;
3879               break;
3880             case GT_EXPR:  /* - [-, c] */
3881               low = 0, high = arg1;
3882               break;
3883             case GE_EXPR:  /* + [c, -] */
3884               in_p = ! in_p, low = arg1, high = 0;
3885               break;
3886             case LT_EXPR:  /* - [c, -] */
3887               low = arg1, high = 0;
3888               break;
3889             case LE_EXPR:  /* + [-, c] */
3890               in_p = ! in_p, low = 0, high = arg1;
3891               break;
3892             default:
3893               gcc_unreachable ();
3894             }
3895
3896           /* If this is an unsigned comparison, we also know that EXP is
3897              greater than or equal to zero.  We base the range tests we make
3898              on that fact, so we record it here so we can parse existing
3899              range tests.  We test arg0_type since often the return type
3900              of, e.g. EQ_EXPR, is boolean.  */
3901           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3902             {
3903               if (! merge_ranges (&n_in_p, &n_low, &n_high,
3904                                   in_p, low, high, 1,
3905                                   build_int_cst (arg0_type, 0),
3906                                   NULL_TREE))
3907                 break;
3908
3909               in_p = n_in_p, low = n_low, high = n_high;
3910
3911               /* If the high bound is missing, but we have a nonzero low
3912                  bound, reverse the range so it goes from zero to the low bound
3913                  minus 1.  */
3914               if (high == 0 && low && ! integer_zerop (low))
3915                 {
3916                   in_p = ! in_p;
3917                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3918                                       integer_one_node, 0);
3919                   low = build_int_cst (arg0_type, 0);
3920                 }
3921             }
3922
3923           exp = arg0;
3924           continue;
3925
3926         case NEGATE_EXPR:
3927           /* (-x) IN [a,b] -> x in [-b, -a]  */
3928           n_low = range_binop (MINUS_EXPR, exp_type,
3929                                build_int_cst (exp_type, 0),
3930                                0, high, 1);
3931           n_high = range_binop (MINUS_EXPR, exp_type,
3932                                 build_int_cst (exp_type, 0),
3933                                 0, low, 0);
3934           if (n_high != 0 && TREE_OVERFLOW (n_high))
3935             break;
3936           goto normalize;
3937
3938         case BIT_NOT_EXPR:
3939           /* ~ X -> -X - 1  */
3940           exp = build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
3941                             build_int_cst (exp_type, 1));
3942           continue;
3943
3944         case PLUS_EXPR:  case MINUS_EXPR:
3945           if (TREE_CODE (arg1) != INTEGER_CST)
3946             break;
3947
3948           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
3949              move a constant to the other side.  */
3950           if (!TYPE_UNSIGNED (arg0_type)
3951               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
3952             break;
3953
3954           /* If EXP is signed, any overflow in the computation is undefined,
3955              so we don't worry about it so long as our computations on
3956              the bounds don't overflow.  For unsigned, overflow is defined
3957              and this is exactly the right thing.  */
3958           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3959                                arg0_type, low, 0, arg1, 0);
3960           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3961                                 arg0_type, high, 1, arg1, 0);
3962           if ((n_low != 0 && TREE_OVERFLOW (n_low))
3963               || (n_high != 0 && TREE_OVERFLOW (n_high)))
3964             break;
3965
3966           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
3967             *strict_overflow_p = true;
3968
3969         normalize:
3970           /* Check for an unsigned range which has wrapped around the maximum
3971              value thus making n_high < n_low, and normalize it.  */
3972           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3973             {
3974               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
3975                                  integer_one_node, 0);
3976               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
3977                                   integer_one_node, 0);
3978
3979               /* If the range is of the form +/- [ x+1, x ], we won't
3980                  be able to normalize it.  But then, it represents the
3981                  whole range or the empty set, so make it
3982                  +/- [ -, - ].  */
3983               if (tree_int_cst_equal (n_low, low)
3984                   && tree_int_cst_equal (n_high, high))
3985                 low = high = 0;
3986               else
3987                 in_p = ! in_p;
3988             }
3989           else
3990             low = n_low, high = n_high;
3991
3992           exp = arg0;
3993           continue;
3994
3995         CASE_CONVERT: case NON_LVALUE_EXPR:
3996           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
3997             break;
3998
3999           if (! INTEGRAL_TYPE_P (arg0_type)
4000               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4001               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4002             break;
4003
4004           n_low = low, n_high = high;
4005
4006           if (n_low != 0)
4007             n_low = fold_convert_loc (loc, arg0_type, n_low);
4008
4009           if (n_high != 0)
4010             n_high = fold_convert_loc (loc, arg0_type, n_high);
4011
4012
4013           /* If we're converting arg0 from an unsigned type, to exp,
4014              a signed type,  we will be doing the comparison as unsigned.
4015              The tests above have already verified that LOW and HIGH
4016              are both positive.
4017
4018              So we have to ensure that we will handle large unsigned
4019              values the same way that the current signed bounds treat
4020              negative values.  */
4021
4022           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4023             {
4024               tree high_positive;
4025               tree equiv_type;
4026               /* For fixed-point modes, we need to pass the saturating flag
4027                  as the 2nd parameter.  */
4028               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4029                 equiv_type = lang_hooks.types.type_for_mode
4030                              (TYPE_MODE (arg0_type),
4031                               TYPE_SATURATING (arg0_type));
4032               else
4033                 equiv_type = lang_hooks.types.type_for_mode
4034                              (TYPE_MODE (arg0_type), 1);
4035
4036               /* A range without an upper bound is, naturally, unbounded.
4037                  Since convert would have cropped a very large value, use
4038                  the max value for the destination type.  */
4039               high_positive
4040                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4041                 : TYPE_MAX_VALUE (arg0_type);
4042
4043               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4044                 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4045                                              fold_convert_loc (loc, arg0_type,
4046                                                                high_positive),
4047                                              build_int_cst (arg0_type, 1));
4048
4049               /* If the low bound is specified, "and" the range with the
4050                  range for which the original unsigned value will be
4051                  positive.  */
4052               if (low != 0)
4053                 {
4054                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4055                                       1, n_low, n_high, 1,
4056                                       fold_convert_loc (loc, arg0_type,
4057                                                         integer_zero_node),
4058                                       high_positive))
4059                     break;
4060
4061                   in_p = (n_in_p == in_p);
4062                 }
4063               else
4064                 {
4065                   /* Otherwise, "or" the range with the range of the input
4066                      that will be interpreted as negative.  */
4067                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4068                                       0, n_low, n_high, 1,
4069                                       fold_convert_loc (loc, arg0_type,
4070                                                         integer_zero_node),
4071                                       high_positive))
4072                     break;
4073
4074                   in_p = (in_p != n_in_p);
4075                 }
4076             }
4077
4078           exp = arg0;
4079           low = n_low, high = n_high;
4080           continue;
4081
4082         default:
4083           break;
4084         }
4085
4086       break;
4087     }
4088
4089   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4090   if (TREE_CODE (exp) == INTEGER_CST)
4091     {
4092       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4093                                                  exp, 0, low, 0))
4094                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4095                                                     exp, 1, high, 1)));
4096       low = high = 0;
4097       exp = 0;
4098     }
4099
4100   *pin_p = in_p, *plow = low, *phigh = high;
4101   return exp;
4102 }
4103 \f
4104 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4105    type, TYPE, return an expression to test if EXP is in (or out of, depending
4106    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4107
4108 tree
4109 build_range_check (location_t loc, tree type, tree exp, int in_p,
4110                    tree low, tree high)
4111 {
4112   tree etype = TREE_TYPE (exp), value;
4113
4114 #ifdef HAVE_canonicalize_funcptr_for_compare
4115   /* Disable this optimization for function pointer expressions
4116      on targets that require function pointer canonicalization.  */
4117   if (HAVE_canonicalize_funcptr_for_compare
4118       && TREE_CODE (etype) == POINTER_TYPE
4119       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4120     return NULL_TREE;
4121 #endif
4122
4123   if (! in_p)
4124     {
4125       value = build_range_check (loc, type, exp, 1, low, high);
4126       if (value != 0)
4127         return invert_truthvalue_loc (loc, value);
4128
4129       return 0;
4130     }
4131
4132   if (low == 0 && high == 0)
4133     return build_int_cst (type, 1);
4134
4135   if (low == 0)
4136     return fold_build2_loc (loc, LE_EXPR, type, exp,
4137                         fold_convert_loc (loc, etype, high));
4138
4139   if (high == 0)
4140     return fold_build2_loc (loc, GE_EXPR, type, exp,
4141                         fold_convert_loc (loc, etype, low));
4142
4143   if (operand_equal_p (low, high, 0))
4144     return fold_build2_loc (loc, EQ_EXPR, type, exp,
4145                         fold_convert_loc (loc, etype, low));
4146
4147   if (integer_zerop (low))
4148     {
4149       if (! TYPE_UNSIGNED (etype))
4150         {
4151           etype = unsigned_type_for (etype);
4152           high = fold_convert_loc (loc, etype, high);
4153           exp = fold_convert_loc (loc, etype, exp);
4154         }
4155       return build_range_check (loc, type, exp, 1, 0, high);
4156     }
4157
4158   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4159   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4160     {
4161       unsigned HOST_WIDE_INT lo;
4162       HOST_WIDE_INT hi;
4163       int prec;
4164
4165       prec = TYPE_PRECISION (etype);
4166       if (prec <= HOST_BITS_PER_WIDE_INT)
4167         {
4168           hi = 0;
4169           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4170         }
4171       else
4172         {
4173           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4174           lo = (unsigned HOST_WIDE_INT) -1;
4175         }
4176
4177       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4178         {
4179           if (TYPE_UNSIGNED (etype))
4180             {
4181               tree signed_etype = signed_type_for (etype);
4182               if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4183                 etype
4184                   = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4185               else
4186                 etype = signed_etype;
4187               exp = fold_convert_loc (loc, etype, exp);
4188             }
4189           return fold_build2_loc (loc, GT_EXPR, type, exp,
4190                               build_int_cst (etype, 0));
4191         }
4192     }
4193
4194   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4195      This requires wrap-around arithmetics for the type of the expression.
4196      First make sure that arithmetics in this type is valid, then make sure
4197      that it wraps around.  */
4198   if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4199     etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4200                                             TYPE_UNSIGNED (etype));
4201
4202   if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4203     {
4204       tree utype, minv, maxv;
4205
4206       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4207          for the type in question, as we rely on this here.  */
4208       utype = unsigned_type_for (etype);
4209       maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4210       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4211                           integer_one_node, 1);
4212       minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4213
4214       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4215                                       minv, 1, maxv, 1)))
4216         etype = utype;
4217       else
4218         return 0;
4219     }
4220
4221   high = fold_convert_loc (loc, etype, high);
4222   low = fold_convert_loc (loc, etype, low);
4223   exp = fold_convert_loc (loc, etype, exp);
4224
4225   value = const_binop (MINUS_EXPR, high, low);
4226
4227
4228   if (POINTER_TYPE_P (etype))
4229     {
4230       if (value != 0 && !TREE_OVERFLOW (value))
4231         {
4232           low = fold_convert_loc (loc, sizetype, low);
4233           low = fold_build1_loc (loc, NEGATE_EXPR, sizetype, low);
4234           return build_range_check (loc, type,
4235                                     fold_build2_loc (loc, POINTER_PLUS_EXPR,
4236                                                  etype, exp, low),
4237                                     1, build_int_cst (etype, 0), value);
4238         }
4239       return 0;
4240     }
4241
4242   if (value != 0 && !TREE_OVERFLOW (value))
4243     return build_range_check (loc, type,
4244                               fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4245                               1, build_int_cst (etype, 0), value);
4246
4247   return 0;
4248 }
4249 \f
4250 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4251
4252 static tree
4253 range_predecessor (tree val)
4254 {
4255   tree type = TREE_TYPE (val);
4256
4257   if (INTEGRAL_TYPE_P (type)
4258       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4259     return 0;
4260   else
4261     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4262 }
4263
4264 /* Return the successor of VAL in its type, handling the infinite case.  */
4265
4266 static tree
4267 range_successor (tree val)
4268 {
4269   tree type = TREE_TYPE (val);
4270
4271   if (INTEGRAL_TYPE_P (type)
4272       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4273     return 0;
4274   else
4275     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4276 }
4277
4278 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4279    can, 0 if we can't.  Set the output range into the specified parameters.  */
4280
4281 bool
4282 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4283               tree high0, int in1_p, tree low1, tree high1)
4284 {
4285   int no_overlap;
4286   int subset;
4287   int temp;
4288   tree tem;
4289   int in_p;
4290   tree low, high;
4291   int lowequal = ((low0 == 0 && low1 == 0)
4292                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4293                                                 low0, 0, low1, 0)));
4294   int highequal = ((high0 == 0 && high1 == 0)
4295                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4296                                                  high0, 1, high1, 1)));
4297
4298   /* Make range 0 be the range that starts first, or ends last if they
4299      start at the same value.  Swap them if it isn't.  */
4300   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4301                                  low0, 0, low1, 0))
4302       || (lowequal
4303           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4304                                         high1, 1, high0, 1))))
4305     {
4306       temp = in0_p, in0_p = in1_p, in1_p = temp;
4307       tem = low0, low0 = low1, low1 = tem;
4308       tem = high0, high0 = high1, high1 = tem;
4309     }
4310
4311   /* Now flag two cases, whether the ranges are disjoint or whether the
4312      second range is totally subsumed in the first.  Note that the tests
4313      below are simplified by the ones above.  */
4314   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4315                                           high0, 1, low1, 0));
4316   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4317                                       high1, 1, high0, 1));
4318
4319   /* We now have four cases, depending on whether we are including or
4320      excluding the two ranges.  */
4321   if (in0_p && in1_p)
4322     {
4323       /* If they don't overlap, the result is false.  If the second range
4324          is a subset it is the result.  Otherwise, the range is from the start
4325          of the second to the end of the first.  */
4326       if (no_overlap)
4327         in_p = 0, low = high = 0;
4328       else if (subset)
4329         in_p = 1, low = low1, high = high1;
4330       else
4331         in_p = 1, low = low1, high = high0;
4332     }
4333
4334   else if (in0_p && ! in1_p)
4335     {
4336       /* If they don't overlap, the result is the first range.  If they are
4337          equal, the result is false.  If the second range is a subset of the
4338          first, and the ranges begin at the same place, we go from just after
4339          the end of the second range to the end of the first.  If the second
4340          range is not a subset of the first, or if it is a subset and both
4341          ranges end at the same place, the range starts at the start of the
4342          first range and ends just before the second range.
4343          Otherwise, we can't describe this as a single range.  */
4344       if (no_overlap)
4345         in_p = 1, low = low0, high = high0;
4346       else if (lowequal && highequal)
4347         in_p = 0, low = high = 0;
4348       else if (subset && lowequal)
4349         {
4350           low = range_successor (high1);
4351           high = high0;
4352           in_p = 1;
4353           if (low == 0)
4354             {
4355               /* We are in the weird situation where high0 > high1 but
4356                  high1 has no successor.  Punt.  */
4357               return 0;
4358             }
4359         }
4360       else if (! subset || highequal)
4361         {
4362           low = low0;
4363           high = range_predecessor (low1);
4364           in_p = 1;
4365           if (high == 0)
4366             {
4367               /* low0 < low1 but low1 has no predecessor.  Punt.  */
4368               return 0;
4369             }
4370         }
4371       else
4372         return 0;
4373     }
4374
4375   else if (! in0_p && in1_p)
4376     {
4377       /* If they don't overlap, the result is the second range.  If the second
4378          is a subset of the first, the result is false.  Otherwise,
4379          the range starts just after the first range and ends at the
4380          end of the second.  */
4381       if (no_overlap)
4382         in_p = 1, low = low1, high = high1;
4383       else if (subset || highequal)
4384         in_p = 0, low = high = 0;
4385       else
4386         {
4387           low = range_successor (high0);
4388           high = high1;
4389           in_p = 1;
4390           if (low == 0)
4391             {
4392               /* high1 > high0 but high0 has no successor.  Punt.  */
4393               return 0;
4394             }
4395         }
4396     }
4397
4398   else
4399     {
4400       /* The case where we are excluding both ranges.  Here the complex case
4401          is if they don't overlap.  In that case, the only time we have a
4402          range is if they are adjacent.  If the second is a subset of the
4403          first, the result is the first.  Otherwise, the range to exclude
4404          starts at the beginning of the first range and ends at the end of the
4405          second.  */
4406       if (no_overlap)
4407         {
4408           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4409                                          range_successor (high0),
4410                                          1, low1, 0)))
4411             in_p = 0, low = low0, high = high1;
4412           else
4413             {
4414               /* Canonicalize - [min, x] into - [-, x].  */
4415               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4416                 switch (TREE_CODE (TREE_TYPE (low0)))
4417                   {
4418                   case ENUMERAL_TYPE:
4419                     if (TYPE_PRECISION (TREE_TYPE (low0))
4420                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4421                       break;
4422                     /* FALLTHROUGH */
4423                   case INTEGER_TYPE:
4424                     if (tree_int_cst_equal (low0,
4425                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4426                       low0 = 0;
4427                     break;
4428                   case POINTER_TYPE:
4429                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4430                         && integer_zerop (low0))
4431                       low0 = 0;
4432                     break;
4433                   default:
4434                     break;
4435                   }
4436
4437               /* Canonicalize - [x, max] into - [x, -].  */
4438               if (high1 && TREE_CODE (high1) == INTEGER_CST)
4439                 switch (TREE_CODE (TREE_TYPE (high1)))
4440                   {
4441                   case ENUMERAL_TYPE:
4442                     if (TYPE_PRECISION (TREE_TYPE (high1))
4443                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4444                       break;
4445                     /* FALLTHROUGH */
4446                   case INTEGER_TYPE:
4447                     if (tree_int_cst_equal (high1,
4448                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
4449                       high1 = 0;
4450                     break;
4451                   case POINTER_TYPE:
4452                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
4453                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4454                                                        high1, 1,
4455                                                        integer_one_node, 1)))
4456                       high1 = 0;
4457                     break;
4458                   default:
4459                     break;
4460                   }
4461
4462               /* The ranges might be also adjacent between the maximum and
4463                  minimum values of the given type.  For
4464                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4465                  return + [x + 1, y - 1].  */
4466               if (low0 == 0 && high1 == 0)
4467                 {
4468                   low = range_successor (high0);
4469                   high = range_predecessor (low1);
4470                   if (low == 0 || high == 0)
4471                     return 0;
4472
4473                   in_p = 1;
4474                 }
4475               else
4476                 return 0;
4477             }
4478         }
4479       else if (subset)
4480         in_p = 0, low = low0, high = high0;
4481       else
4482         in_p = 0, low = low0, high = high1;
4483     }
4484
4485   *pin_p = in_p, *plow = low, *phigh = high;
4486   return 1;
4487 }
4488 \f
4489
4490 /* Subroutine of fold, looking inside expressions of the form
4491    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4492    of the COND_EXPR.  This function is being used also to optimize
4493    A op B ? C : A, by reversing the comparison first.
4494
4495    Return a folded expression whose code is not a COND_EXPR
4496    anymore, or NULL_TREE if no folding opportunity is found.  */
4497
4498 static tree
4499 fold_cond_expr_with_comparison (location_t loc, tree type,
4500                                 tree arg0, tree arg1, tree arg2)
4501 {
4502   enum tree_code comp_code = TREE_CODE (arg0);
4503   tree arg00 = TREE_OPERAND (arg0, 0);
4504   tree arg01 = TREE_OPERAND (arg0, 1);
4505   tree arg1_type = TREE_TYPE (arg1);
4506   tree tem;
4507
4508   STRIP_NOPS (arg1);
4509   STRIP_NOPS (arg2);
4510
4511   /* If we have A op 0 ? A : -A, consider applying the following
4512      transformations:
4513
4514      A == 0? A : -A    same as -A
4515      A != 0? A : -A    same as A
4516      A >= 0? A : -A    same as abs (A)
4517      A > 0?  A : -A    same as abs (A)
4518      A <= 0? A : -A    same as -abs (A)
4519      A < 0?  A : -A    same as -abs (A)
4520
4521      None of these transformations work for modes with signed
4522      zeros.  If A is +/-0, the first two transformations will
4523      change the sign of the result (from +0 to -0, or vice
4524      versa).  The last four will fix the sign of the result,
4525      even though the original expressions could be positive or
4526      negative, depending on the sign of A.
4527
4528      Note that all these transformations are correct if A is
4529      NaN, since the two alternatives (A and -A) are also NaNs.  */
4530   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4531       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4532           ? real_zerop (arg01)
4533           : integer_zerop (arg01))
4534       && ((TREE_CODE (arg2) == NEGATE_EXPR
4535            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4536              /* In the case that A is of the form X-Y, '-A' (arg2) may
4537                 have already been folded to Y-X, check for that. */
4538           || (TREE_CODE (arg1) == MINUS_EXPR
4539               && TREE_CODE (arg2) == MINUS_EXPR
4540               && operand_equal_p (TREE_OPERAND (arg1, 0),
4541                                   TREE_OPERAND (arg2, 1), 0)
4542               && operand_equal_p (TREE_OPERAND (arg1, 1),
4543                                   TREE_OPERAND (arg2, 0), 0))))
4544     switch (comp_code)
4545       {
4546       case EQ_EXPR:
4547       case UNEQ_EXPR:
4548         tem = fold_convert_loc (loc, arg1_type, arg1);
4549         return pedantic_non_lvalue_loc (loc,
4550                                     fold_convert_loc (loc, type,
4551                                                   negate_expr (tem)));
4552       case NE_EXPR:
4553       case LTGT_EXPR:
4554         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4555       case UNGE_EXPR:
4556       case UNGT_EXPR:
4557         if (flag_trapping_math)
4558           break;
4559         /* Fall through.  */
4560       case GE_EXPR:
4561       case GT_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 pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4567       case UNLE_EXPR:
4568       case UNLT_EXPR:
4569         if (flag_trapping_math)
4570           break;
4571       case LE_EXPR:
4572       case LT_EXPR:
4573         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4574           arg1 = fold_convert_loc (loc, signed_type_for
4575                                (TREE_TYPE (arg1)), arg1);
4576         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4577         return negate_expr (fold_convert_loc (loc, type, tem));
4578       default:
4579         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4580         break;
4581       }
4582
4583   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
4584      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
4585      both transformations are correct when A is NaN: A != 0
4586      is then true, and A == 0 is false.  */
4587
4588   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4589       && integer_zerop (arg01) && integer_zerop (arg2))
4590     {
4591       if (comp_code == NE_EXPR)
4592         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4593       else if (comp_code == EQ_EXPR)
4594         return build_int_cst (type, 0);
4595     }
4596
4597   /* Try some transformations of A op B ? A : B.
4598
4599      A == B? A : B    same as B
4600      A != B? A : B    same as A
4601      A >= B? A : B    same as max (A, B)
4602      A > B?  A : B    same as max (B, A)
4603      A <= B? A : B    same as min (A, B)
4604      A < B?  A : B    same as min (B, A)
4605
4606      As above, these transformations don't work in the presence
4607      of signed zeros.  For example, if A and B are zeros of
4608      opposite sign, the first two transformations will change
4609      the sign of the result.  In the last four, the original
4610      expressions give different results for (A=+0, B=-0) and
4611      (A=-0, B=+0), but the transformed expressions do not.
4612
4613      The first two transformations are correct if either A or B
4614      is a NaN.  In the first transformation, the condition will
4615      be false, and B will indeed be chosen.  In the case of the
4616      second transformation, the condition A != B will be true,
4617      and A will be chosen.
4618
4619      The conversions to max() and min() are not correct if B is
4620      a number and A is not.  The conditions in the original
4621      expressions will be false, so all four give B.  The min()
4622      and max() versions would give a NaN instead.  */
4623   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4624       && operand_equal_for_comparison_p (arg01, arg2, arg00)
4625       /* Avoid these transformations if the COND_EXPR may be used
4626          as an lvalue in the C++ front-end.  PR c++/19199.  */
4627       && (in_gimple_form
4628           || (strcmp (lang_hooks.name, "GNU C++") != 0
4629               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4630           || ! maybe_lvalue_p (arg1)
4631           || ! maybe_lvalue_p (arg2)))
4632     {
4633       tree comp_op0 = arg00;
4634       tree comp_op1 = arg01;
4635       tree comp_type = TREE_TYPE (comp_op0);
4636
4637       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
4638       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4639         {
4640           comp_type = type;
4641           comp_op0 = arg1;
4642           comp_op1 = arg2;
4643         }
4644
4645       switch (comp_code)
4646         {
4647         case EQ_EXPR:
4648           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
4649         case NE_EXPR:
4650           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4651         case LE_EXPR:
4652         case LT_EXPR:
4653         case UNLE_EXPR:
4654         case UNLT_EXPR:
4655           /* In C++ a ?: expression can be an lvalue, so put the
4656              operand which will be used if they are equal first
4657              so that we can convert this back to the
4658              corresponding COND_EXPR.  */
4659           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4660             {
4661               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4662               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4663               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4664                     ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
4665                     : fold_build2_loc (loc, MIN_EXPR, comp_type,
4666                                    comp_op1, comp_op0);
4667               return pedantic_non_lvalue_loc (loc,
4668                                           fold_convert_loc (loc, type, tem));
4669             }
4670           break;
4671         case GE_EXPR:
4672         case GT_EXPR:
4673         case UNGE_EXPR:
4674         case UNGT_EXPR:
4675           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4676             {
4677               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4678               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4679               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4680                     ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
4681                     : fold_build2_loc (loc, MAX_EXPR, comp_type,
4682                                    comp_op1, comp_op0);
4683               return pedantic_non_lvalue_loc (loc,
4684                                           fold_convert_loc (loc, type, tem));
4685             }
4686           break;
4687         case UNEQ_EXPR:
4688           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4689             return pedantic_non_lvalue_loc (loc,
4690                                         fold_convert_loc (loc, type, arg2));
4691           break;
4692         case LTGT_EXPR:
4693           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4694             return pedantic_non_lvalue_loc (loc,
4695                                         fold_convert_loc (loc, type, arg1));
4696           break;
4697         default:
4698           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4699           break;
4700         }
4701     }
4702
4703   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4704      we might still be able to simplify this.  For example,
4705      if C1 is one less or one more than C2, this might have started
4706      out as a MIN or MAX and been transformed by this function.
4707      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
4708
4709   if (INTEGRAL_TYPE_P (type)
4710       && TREE_CODE (arg01) == INTEGER_CST
4711       && TREE_CODE (arg2) == INTEGER_CST)
4712     switch (comp_code)
4713       {
4714       case EQ_EXPR:
4715         if (TREE_CODE (arg1) == INTEGER_CST)
4716           break;
4717         /* We can replace A with C1 in this case.  */
4718         arg1 = fold_convert_loc (loc, type, arg01);
4719         return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
4720
4721       case LT_EXPR:
4722         /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
4723            MIN_EXPR, to preserve the signedness of the comparison.  */
4724         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4725                                OEP_ONLY_CONST)
4726             && operand_equal_p (arg01,
4727                                 const_binop (PLUS_EXPR, arg2,
4728                                              build_int_cst (type, 1)),
4729                                 OEP_ONLY_CONST))
4730           {
4731             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4732                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4733                                                      arg2));
4734             return pedantic_non_lvalue_loc (loc,
4735                                             fold_convert_loc (loc, type, tem));
4736           }
4737         break;
4738
4739       case LE_EXPR:
4740         /* If C1 is C2 - 1, this is min(A, C2), with the same care
4741            as above.  */
4742         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4743                                OEP_ONLY_CONST)
4744             && operand_equal_p (arg01,
4745                                 const_binop (MINUS_EXPR, arg2,
4746                                              build_int_cst (type, 1)),
4747                                 OEP_ONLY_CONST))
4748           {
4749             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4750                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4751                                                      arg2));
4752             return pedantic_non_lvalue_loc (loc,
4753                                             fold_convert_loc (loc, type, tem));
4754           }
4755         break;
4756
4757       case GT_EXPR:
4758         /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
4759            MAX_EXPR, to preserve the signedness of the comparison.  */
4760         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4761                                OEP_ONLY_CONST)
4762             && operand_equal_p (arg01,
4763                                 const_binop (MINUS_EXPR, arg2,
4764                                              build_int_cst (type, 1)),
4765                                 OEP_ONLY_CONST))
4766           {
4767             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4768                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4769                                                      arg2));
4770             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4771           }
4772         break;
4773
4774       case GE_EXPR:
4775         /* If C1 is C2 + 1, this is max(A, C2), with the same care as above.  */
4776         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4777                                OEP_ONLY_CONST)
4778             && operand_equal_p (arg01,
4779                                 const_binop (PLUS_EXPR, arg2,
4780                                              build_int_cst (type, 1)),
4781                                 OEP_ONLY_CONST))
4782           {
4783             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4784                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4785                                                      arg2));
4786             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4787           }
4788         break;
4789       case NE_EXPR:
4790         break;
4791       default:
4792         gcc_unreachable ();
4793       }
4794
4795   return NULL_TREE;
4796 }
4797
4798
4799 \f
4800 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4801 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
4802   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
4803                 false) >= 2)
4804 #endif
4805
4806 /* EXP is some logical combination of boolean tests.  See if we can
4807    merge it into some range test.  Return the new tree if so.  */
4808
4809 static tree
4810 fold_range_test (location_t loc, enum tree_code code, tree type,
4811                  tree op0, tree op1)
4812 {
4813   int or_op = (code == TRUTH_ORIF_EXPR
4814                || code == TRUTH_OR_EXPR);
4815   int in0_p, in1_p, in_p;
4816   tree low0, low1, low, high0, high1, high;
4817   bool strict_overflow_p = false;
4818   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
4819   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
4820   tree tem;
4821   const char * const warnmsg = G_("assuming signed overflow does not occur "
4822                                   "when simplifying range test");
4823
4824   /* If this is an OR operation, invert both sides; we will invert
4825      again at the end.  */
4826   if (or_op)
4827     in0_p = ! in0_p, in1_p = ! in1_p;
4828
4829   /* If both expressions are the same, if we can merge the ranges, and we
4830      can build the range test, return it or it inverted.  If one of the
4831      ranges is always true or always false, consider it to be the same
4832      expression as the other.  */
4833   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4834       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4835                        in1_p, low1, high1)
4836       && 0 != (tem = (build_range_check (UNKNOWN_LOCATION, type,
4837                                          lhs != 0 ? lhs
4838                                          : rhs != 0 ? rhs : integer_zero_node,
4839                                          in_p, low, high))))
4840     {
4841       if (strict_overflow_p)
4842         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
4843       return or_op ? invert_truthvalue_loc (loc, tem) : tem;
4844     }
4845
4846   /* On machines where the branch cost is expensive, if this is a
4847      short-circuited branch and the underlying object on both sides
4848      is the same, make a non-short-circuit operation.  */
4849   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4850            && lhs != 0 && rhs != 0
4851            && (code == TRUTH_ANDIF_EXPR
4852                || code == TRUTH_ORIF_EXPR)
4853            && operand_equal_p (lhs, rhs, 0))
4854     {
4855       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
4856          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4857          which cases we can't do this.  */
4858       if (simple_operand_p (lhs))
4859         return build2_loc (loc, code == TRUTH_ANDIF_EXPR
4860                            ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4861                            type, op0, op1);
4862
4863       else if (lang_hooks.decls.global_bindings_p () == 0
4864                && ! CONTAINS_PLACEHOLDER_P (lhs))
4865         {
4866           tree common = save_expr (lhs);
4867
4868           if (0 != (lhs = build_range_check (loc, type, common,
4869                                              or_op ? ! in0_p : in0_p,
4870                                              low0, high0))
4871               && (0 != (rhs = build_range_check (loc, type, common,
4872                                                  or_op ? ! in1_p : in1_p,
4873                                                  low1, high1))))
4874             {
4875               if (strict_overflow_p)
4876                 fold_overflow_warning (warnmsg,
4877                                        WARN_STRICT_OVERFLOW_COMPARISON);
4878               return build2_loc (loc, code == TRUTH_ANDIF_EXPR
4879                                  ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4880                                  type, lhs, rhs);
4881             }
4882         }
4883     }
4884
4885   return 0;
4886 }
4887 \f
4888 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4889    bit value.  Arrange things so the extra bits will be set to zero if and
4890    only if C is signed-extended to its full width.  If MASK is nonzero,
4891    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
4892
4893 static tree
4894 unextend (tree c, int p, int unsignedp, tree mask)
4895 {
4896   tree type = TREE_TYPE (c);
4897   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4898   tree temp;
4899
4900   if (p == modesize || unsignedp)
4901     return c;
4902
4903   /* We work by getting just the sign bit into the low-order bit, then
4904      into the high-order bit, then sign-extend.  We then XOR that value
4905      with C.  */
4906   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1));
4907   temp = const_binop (BIT_AND_EXPR, temp, size_int (1));
4908
4909   /* We must use a signed type in order to get an arithmetic right shift.
4910      However, we must also avoid introducing accidental overflows, so that
4911      a subsequent call to integer_zerop will work.  Hence we must
4912      do the type conversion here.  At this point, the constant is either
4913      zero or one, and the conversion to a signed type can never overflow.
4914      We could get an overflow if this conversion is done anywhere else.  */
4915   if (TYPE_UNSIGNED (type))
4916     temp = fold_convert (signed_type_for (type), temp);
4917
4918   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
4919   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
4920   if (mask != 0)
4921     temp = const_binop (BIT_AND_EXPR, temp,
4922                         fold_convert (TREE_TYPE (c), mask));
4923   /* If necessary, convert the type back to match the type of C.  */
4924   if (TYPE_UNSIGNED (type))
4925     temp = fold_convert (type, temp);
4926
4927   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
4928 }
4929 \f
4930 /* For an expression that has the form
4931      (A && B) || ~B
4932    or
4933      (A || B) && ~B,
4934    we can drop one of the inner expressions and simplify to
4935      A || ~B
4936    or
4937      A && ~B
4938    LOC is the location of the resulting expression.  OP is the inner 
4939    logical operation; the left-hand side in the examples above, while CMPOP
4940    is the right-hand side.  RHS_ONLY is used to prevent us from accidentally
4941    removing a condition that guards another, as in
4942      (A != NULL && A->...) || A == NULL
4943    which we must not transform.  If RHS_ONLY is true, only eliminate the
4944    right-most operand of the inner logical operation.  */
4945
4946 static tree
4947 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
4948                                  bool rhs_only)
4949 {
4950   tree type = TREE_TYPE (cmpop);
4951   enum tree_code code = TREE_CODE (cmpop);
4952   enum tree_code truthop_code = TREE_CODE (op);
4953   tree lhs = TREE_OPERAND (op, 0);
4954   tree rhs = TREE_OPERAND (op, 1);
4955   tree orig_lhs = lhs, orig_rhs = rhs;
4956   enum tree_code rhs_code = TREE_CODE (rhs);
4957   enum tree_code lhs_code = TREE_CODE (lhs);
4958   enum tree_code inv_code;
4959
4960   if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
4961     return NULL_TREE;
4962
4963   if (TREE_CODE_CLASS (code) != tcc_comparison)
4964     return NULL_TREE;
4965
4966   if (rhs_code == truthop_code)
4967     {
4968       tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
4969       if (newrhs != NULL_TREE)
4970         {
4971           rhs = newrhs;
4972           rhs_code = TREE_CODE (rhs);
4973         }
4974     }
4975   if (lhs_code == truthop_code && !rhs_only)
4976     {
4977       tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
4978       if (newlhs != NULL_TREE)
4979         {
4980           lhs = newlhs;
4981           lhs_code = TREE_CODE (lhs);
4982         }
4983     }
4984
4985   inv_code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
4986   if (inv_code == rhs_code
4987       && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
4988       && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
4989     return lhs;
4990   if (!rhs_only && inv_code == lhs_code
4991       && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
4992       && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
4993     return rhs;
4994   if (rhs != orig_rhs || lhs != orig_lhs)
4995     return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
4996                             lhs, rhs);
4997   return NULL_TREE;
4998 }
4999
5000 /* Find ways of folding logical expressions of LHS and RHS:
5001    Try to merge two comparisons to the same innermost item.
5002    Look for range tests like "ch >= '0' && ch <= '9'".
5003    Look for combinations of simple terms on machines with expensive branches
5004    and evaluate the RHS unconditionally.
5005
5006    For example, if we have p->a == 2 && p->b == 4 and we can make an
5007    object large enough to span both A and B, we can do this with a comparison
5008    against the object ANDed with the a mask.
5009
5010    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5011    operations to do this with one comparison.
5012
5013    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5014    function and the one above.
5015
5016    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5017    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5018
5019    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5020    two operands.
5021
5022    We return the simplified tree or 0 if no optimization is possible.  */
5023
5024 static tree
5025 fold_truthop (location_t loc, enum tree_code code, tree truth_type,
5026               tree lhs, tree rhs)
5027 {
5028   /* If this is the "or" of two comparisons, we can do something if
5029      the comparisons are NE_EXPR.  If this is the "and", we can do something
5030      if the comparisons are EQ_EXPR.  I.e.,
5031         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5032
5033      WANTED_CODE is this operation code.  For single bit fields, we can
5034      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5035      comparison for one-bit fields.  */
5036
5037   enum tree_code wanted_code;
5038   enum tree_code lcode, rcode;
5039   tree ll_arg, lr_arg, rl_arg, rr_arg;
5040   tree ll_inner, lr_inner, rl_inner, rr_inner;
5041   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5042   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5043   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5044   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5045   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5046   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5047   enum machine_mode lnmode, rnmode;
5048   tree ll_mask, lr_mask, rl_mask, rr_mask;
5049   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5050   tree l_const, r_const;
5051   tree lntype, rntype, result;
5052   HOST_WIDE_INT first_bit, end_bit;
5053   int volatilep;
5054   tree orig_lhs = lhs, orig_rhs = rhs;
5055   enum tree_code orig_code = code;
5056
5057   /* Start by getting the comparison codes.  Fail if anything is volatile.
5058      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5059      it were surrounded with a NE_EXPR.  */
5060
5061   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5062     return 0;
5063
5064   lcode = TREE_CODE (lhs);
5065   rcode = TREE_CODE (rhs);
5066
5067   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5068     {
5069       lhs = build2 (NE_EXPR, truth_type, lhs,
5070                     build_int_cst (TREE_TYPE (lhs), 0));
5071       lcode = NE_EXPR;
5072     }
5073
5074   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5075     {
5076       rhs = build2 (NE_EXPR, truth_type, rhs,
5077                     build_int_cst (TREE_TYPE (rhs), 0));
5078       rcode = NE_EXPR;
5079     }
5080
5081   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5082       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5083     return 0;
5084
5085   ll_arg = TREE_OPERAND (lhs, 0);
5086   lr_arg = TREE_OPERAND (lhs, 1);
5087   rl_arg = TREE_OPERAND (rhs, 0);
5088   rr_arg = TREE_OPERAND (rhs, 1);
5089
5090   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5091   if (simple_operand_p (ll_arg)
5092       && simple_operand_p (lr_arg))
5093     {
5094       if (operand_equal_p (ll_arg, rl_arg, 0)
5095           && operand_equal_p (lr_arg, rr_arg, 0))
5096         {
5097           result = combine_comparisons (loc, code, lcode, rcode,
5098                                         truth_type, ll_arg, lr_arg);
5099           if (result)
5100             return result;
5101         }
5102       else if (operand_equal_p (ll_arg, rr_arg, 0)
5103                && operand_equal_p (lr_arg, rl_arg, 0))
5104         {
5105           result = combine_comparisons (loc, code, lcode,
5106                                         swap_tree_comparison (rcode),
5107                                         truth_type, ll_arg, lr_arg);
5108           if (result)
5109             return result;
5110         }
5111     }
5112
5113   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5114           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5115
5116   /* If the RHS can be evaluated unconditionally and its operands are
5117      simple, it wins to evaluate the RHS unconditionally on machines
5118      with expensive branches.  In this case, this isn't a comparison
5119      that can be merged.  Avoid doing this if the RHS is a floating-point
5120      comparison since those can trap.  */
5121
5122   if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5123                    false) >= 2
5124       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5125       && simple_operand_p (rl_arg)
5126       && simple_operand_p (rr_arg))
5127     {
5128       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5129       if (code == TRUTH_OR_EXPR
5130           && lcode == NE_EXPR && integer_zerop (lr_arg)
5131           && rcode == NE_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, NE_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       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5140       if (code == TRUTH_AND_EXPR
5141           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5142           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5143           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5144           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5145         return build2_loc (loc, EQ_EXPR, truth_type,
5146                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5147                                    ll_arg, rl_arg),
5148                            build_int_cst (TREE_TYPE (ll_arg), 0));
5149
5150       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5151         {
5152           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5153             return build2_loc (loc, code, truth_type, lhs, rhs);
5154           return NULL_TREE;
5155         }
5156     }
5157
5158   /* See if the comparisons can be merged.  Then get all the parameters for
5159      each side.  */
5160
5161   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5162       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5163     return 0;
5164
5165   volatilep = 0;
5166   ll_inner = decode_field_reference (loc, ll_arg,
5167                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5168                                      &ll_unsignedp, &volatilep, &ll_mask,
5169                                      &ll_and_mask);
5170   lr_inner = decode_field_reference (loc, lr_arg,
5171                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5172                                      &lr_unsignedp, &volatilep, &lr_mask,
5173                                      &lr_and_mask);
5174   rl_inner = decode_field_reference (loc, rl_arg,
5175                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5176                                      &rl_unsignedp, &volatilep, &rl_mask,
5177                                      &rl_and_mask);
5178   rr_inner = decode_field_reference (loc, rr_arg,
5179                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5180                                      &rr_unsignedp, &volatilep, &rr_mask,
5181                                      &rr_and_mask);
5182
5183   /* It must be true that the inner operation on the lhs of each
5184      comparison must be the same if we are to be able to do anything.
5185      Then see if we have constants.  If not, the same must be true for
5186      the rhs's.  */
5187   if (volatilep || ll_inner == 0 || rl_inner == 0
5188       || ! operand_equal_p (ll_inner, rl_inner, 0))
5189     return 0;
5190
5191   if (TREE_CODE (lr_arg) == INTEGER_CST
5192       && TREE_CODE (rr_arg) == INTEGER_CST)
5193     l_const = lr_arg, r_const = rr_arg;
5194   else if (lr_inner == 0 || rr_inner == 0
5195            || ! operand_equal_p (lr_inner, rr_inner, 0))
5196     return 0;
5197   else
5198     l_const = r_const = 0;
5199
5200   /* If either comparison code is not correct for our logical operation,
5201      fail.  However, we can convert a one-bit comparison against zero into
5202      the opposite comparison against that bit being set in the field.  */
5203
5204   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5205   if (lcode != wanted_code)
5206     {
5207       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5208         {
5209           /* Make the left operand unsigned, since we are only interested
5210              in the value of one bit.  Otherwise we are doing the wrong
5211              thing below.  */
5212           ll_unsignedp = 1;
5213           l_const = ll_mask;
5214         }
5215       else
5216         return 0;
5217     }
5218
5219   /* This is analogous to the code for l_const above.  */
5220   if (rcode != wanted_code)
5221     {
5222       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5223         {
5224           rl_unsignedp = 1;
5225           r_const = rl_mask;
5226         }
5227       else
5228         return 0;
5229     }
5230
5231   /* See if we can find a mode that contains both fields being compared on
5232      the left.  If we can't, fail.  Otherwise, update all constants and masks
5233      to be relative to a field of that size.  */
5234   first_bit = MIN (ll_bitpos, rl_bitpos);
5235   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5236   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5237                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5238                           volatilep);
5239   if (lnmode == VOIDmode)
5240     return 0;
5241
5242   lnbitsize = GET_MODE_BITSIZE (lnmode);
5243   lnbitpos = first_bit & ~ (lnbitsize - 1);
5244   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5245   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5246
5247   if (BYTES_BIG_ENDIAN)
5248     {
5249       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5250       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5251     }
5252
5253   ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5254                          size_int (xll_bitpos));
5255   rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5256                          size_int (xrl_bitpos));
5257
5258   if (l_const)
5259     {
5260       l_const = fold_convert_loc (loc, lntype, l_const);
5261       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5262       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5263       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5264                                         fold_build1_loc (loc, BIT_NOT_EXPR,
5265                                                      lntype, ll_mask))))
5266         {
5267           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5268
5269           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5270         }
5271     }
5272   if (r_const)
5273     {
5274       r_const = fold_convert_loc (loc, lntype, r_const);
5275       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5276       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5277       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5278                                         fold_build1_loc (loc, BIT_NOT_EXPR,
5279                                                      lntype, rl_mask))))
5280         {
5281           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5282
5283           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5284         }
5285     }
5286
5287   /* If the right sides are not constant, do the same for it.  Also,
5288      disallow this optimization if a size or signedness mismatch occurs
5289      between the left and right sides.  */
5290   if (l_const == 0)
5291     {
5292       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5293           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5294           /* Make sure the two fields on the right
5295              correspond to the left without being swapped.  */
5296           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5297         return 0;
5298
5299       first_bit = MIN (lr_bitpos, rr_bitpos);
5300       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5301       rnmode = get_best_mode (end_bit - first_bit, first_bit,
5302                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5303                               volatilep);
5304       if (rnmode == VOIDmode)
5305         return 0;
5306
5307       rnbitsize = GET_MODE_BITSIZE (rnmode);
5308       rnbitpos = first_bit & ~ (rnbitsize - 1);
5309       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5310       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5311
5312       if (BYTES_BIG_ENDIAN)
5313         {
5314           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5315           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5316         }
5317
5318       lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5319                                                             rntype, lr_mask),
5320                              size_int (xlr_bitpos));
5321       rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5322                                                             rntype, rr_mask),
5323                              size_int (xrr_bitpos));
5324
5325       /* Make a mask that corresponds to both fields being compared.
5326          Do this for both items being compared.  If the operands are the
5327          same size and the bits being compared are in the same position
5328          then we can do this by masking both and comparing the masked
5329          results.  */
5330       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5331       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5332       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5333         {
5334           lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5335                                     ll_unsignedp || rl_unsignedp);
5336           if (! all_ones_mask_p (ll_mask, lnbitsize))
5337             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5338
5339           rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5340                                     lr_unsignedp || rr_unsignedp);
5341           if (! all_ones_mask_p (lr_mask, rnbitsize))
5342             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5343
5344           return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5345         }
5346
5347       /* There is still another way we can do something:  If both pairs of
5348          fields being compared are adjacent, we may be able to make a wider
5349          field containing them both.
5350
5351          Note that we still must mask the lhs/rhs expressions.  Furthermore,
5352          the mask must be shifted to account for the shift done by
5353          make_bit_field_ref.  */
5354       if ((ll_bitsize + ll_bitpos == rl_bitpos
5355            && lr_bitsize + lr_bitpos == rr_bitpos)
5356           || (ll_bitpos == rl_bitpos + rl_bitsize
5357               && lr_bitpos == rr_bitpos + rr_bitsize))
5358         {
5359           tree type;
5360
5361           lhs = make_bit_field_ref (loc, ll_inner, lntype,
5362                                     ll_bitsize + rl_bitsize,
5363                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5364           rhs = make_bit_field_ref (loc, lr_inner, rntype,
5365                                     lr_bitsize + rr_bitsize,
5366                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5367
5368           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5369                                  size_int (MIN (xll_bitpos, xrl_bitpos)));
5370           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5371                                  size_int (MIN (xlr_bitpos, xrr_bitpos)));
5372
5373           /* Convert to the smaller type before masking out unwanted bits.  */
5374           type = lntype;
5375           if (lntype != rntype)
5376             {
5377               if (lnbitsize > rnbitsize)
5378                 {
5379                   lhs = fold_convert_loc (loc, rntype, lhs);
5380                   ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5381                   type = rntype;
5382                 }
5383               else if (lnbitsize < rnbitsize)
5384                 {
5385                   rhs = fold_convert_loc (loc, lntype, rhs);
5386                   lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5387                   type = lntype;
5388                 }
5389             }
5390
5391           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5392             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5393
5394           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5395             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5396
5397           return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5398         }
5399
5400       return 0;
5401     }
5402
5403   /* Handle the case of comparisons with constants.  If there is something in
5404      common between the masks, those bits of the constants must be the same.
5405      If not, the condition is always false.  Test for this to avoid generating
5406      incorrect code below.  */
5407   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5408   if (! integer_zerop (result)
5409       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5410                            const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5411     {
5412       if (wanted_code == NE_EXPR)
5413         {
5414           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5415           return constant_boolean_node (true, truth_type);
5416         }
5417       else
5418         {
5419           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5420           return constant_boolean_node (false, truth_type);
5421         }
5422     }
5423
5424   /* Construct the expression we will return.  First get the component
5425      reference we will make.  Unless the mask is all ones the width of
5426      that field, perform the mask operation.  Then compare with the
5427      merged constant.  */
5428   result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5429                                ll_unsignedp || rl_unsignedp);
5430
5431   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5432   if (! all_ones_mask_p (ll_mask, lnbitsize))
5433     result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5434
5435   return build2_loc (loc, wanted_code, truth_type, result,
5436                      const_binop (BIT_IOR_EXPR, l_const, r_const));
5437 }
5438 \f
5439 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5440    constant.  */
5441
5442 static tree
5443 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5444                             tree op0, tree op1)
5445 {
5446   tree arg0 = op0;
5447   enum tree_code op_code;
5448   tree comp_const;
5449   tree minmax_const;
5450   int consts_equal, consts_lt;
5451   tree inner;
5452
5453   STRIP_SIGN_NOPS (arg0);
5454
5455   op_code = TREE_CODE (arg0);
5456   minmax_const = TREE_OPERAND (arg0, 1);
5457   comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5458   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5459   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5460   inner = TREE_OPERAND (arg0, 0);
5461
5462   /* If something does not permit us to optimize, return the original tree.  */
5463   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5464       || TREE_CODE (comp_const) != INTEGER_CST
5465       || TREE_OVERFLOW (comp_const)
5466       || TREE_CODE (minmax_const) != INTEGER_CST
5467       || TREE_OVERFLOW (minmax_const))
5468     return NULL_TREE;
5469
5470   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5471      and GT_EXPR, doing the rest with recursive calls using logical
5472      simplifications.  */
5473   switch (code)
5474     {
5475     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5476       {
5477         tree tem
5478           = optimize_minmax_comparison (loc,
5479                                         invert_tree_comparison (code, false),
5480                                         type, op0, op1);
5481         if (tem)
5482           return invert_truthvalue_loc (loc, tem);
5483         return NULL_TREE;
5484       }
5485
5486     case GE_EXPR:
5487       return
5488         fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5489                      optimize_minmax_comparison
5490                      (loc, EQ_EXPR, type, arg0, comp_const),
5491                      optimize_minmax_comparison
5492                      (loc, GT_EXPR, type, arg0, comp_const));
5493
5494     case EQ_EXPR:
5495       if (op_code == MAX_EXPR && consts_equal)
5496         /* MAX (X, 0) == 0  ->  X <= 0  */
5497         return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5498
5499       else if (op_code == MAX_EXPR && consts_lt)
5500         /* MAX (X, 0) == 5  ->  X == 5   */
5501         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5502
5503       else if (op_code == MAX_EXPR)
5504         /* MAX (X, 0) == -1  ->  false  */
5505         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5506
5507       else if (consts_equal)
5508         /* MIN (X, 0) == 0  ->  X >= 0  */
5509         return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5510
5511       else if (consts_lt)
5512         /* MIN (X, 0) == 5  ->  false  */
5513         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5514
5515       else
5516         /* MIN (X, 0) == -1  ->  X == -1  */
5517         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5518
5519     case GT_EXPR:
5520       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5521         /* MAX (X, 0) > 0  ->  X > 0
5522            MAX (X, 0) > 5  ->  X > 5  */
5523         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5524
5525       else if (op_code == MAX_EXPR)
5526         /* MAX (X, 0) > -1  ->  true  */
5527         return omit_one_operand_loc (loc, type, integer_one_node, inner);
5528
5529       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5530         /* MIN (X, 0) > 0  ->  false
5531            MIN (X, 0) > 5  ->  false  */
5532         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5533
5534       else
5535         /* MIN (X, 0) > -1  ->  X > -1  */
5536         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5537
5538     default:
5539       return NULL_TREE;
5540     }
5541 }
5542 \f
5543 /* T is an integer expression that is being multiplied, divided, or taken a
5544    modulus (CODE says which and what kind of divide or modulus) by a
5545    constant C.  See if we can eliminate that operation by folding it with
5546    other operations already in T.  WIDE_TYPE, if non-null, is a type that
5547    should be used for the computation if wider than our type.
5548
5549    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5550    (X * 2) + (Y * 4).  We must, however, be assured that either the original
5551    expression would not overflow or that overflow is undefined for the type
5552    in the language in question.
5553
5554    If we return a non-null expression, it is an equivalent form of the
5555    original computation, but need not be in the original type.
5556
5557    We set *STRICT_OVERFLOW_P to true if the return values depends on
5558    signed overflow being undefined.  Otherwise we do not change
5559    *STRICT_OVERFLOW_P.  */
5560
5561 static tree
5562 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5563                 bool *strict_overflow_p)
5564 {
5565   /* To avoid exponential search depth, refuse to allow recursion past
5566      three levels.  Beyond that (1) it's highly unlikely that we'll find
5567      something interesting and (2) we've probably processed it before
5568      when we built the inner expression.  */
5569
5570   static int depth;
5571   tree ret;
5572
5573   if (depth > 3)
5574     return NULL;
5575
5576   depth++;
5577   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5578   depth--;
5579
5580   return ret;
5581 }
5582
5583 static tree
5584 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5585                   bool *strict_overflow_p)
5586 {
5587   tree type = TREE_TYPE (t);
5588   enum tree_code tcode = TREE_CODE (t);
5589   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5590                                    > GET_MODE_SIZE (TYPE_MODE (type)))
5591                 ? wide_type : type);
5592   tree t1, t2;
5593   int same_p = tcode == code;
5594   tree op0 = NULL_TREE, op1 = NULL_TREE;
5595   bool sub_strict_overflow_p;
5596
5597   /* Don't deal with constants of zero here; they confuse the code below.  */
5598   if (integer_zerop (c))
5599     return NULL_TREE;
5600
5601   if (TREE_CODE_CLASS (tcode) == tcc_unary)
5602     op0 = TREE_OPERAND (t, 0);
5603
5604   if (TREE_CODE_CLASS (tcode) == tcc_binary)
5605     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5606
5607   /* Note that we need not handle conditional operations here since fold
5608      already handles those cases.  So just do arithmetic here.  */
5609   switch (tcode)
5610     {
5611     case INTEGER_CST:
5612       /* For a constant, we can always simplify if we are a multiply
5613          or (for divide and modulus) if it is a multiple of our constant.  */
5614       if (code == MULT_EXPR
5615           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c)))
5616         return const_binop (code, fold_convert (ctype, t),
5617                             fold_convert (ctype, c));
5618       break;
5619
5620     CASE_CONVERT: case NON_LVALUE_EXPR:
5621       /* If op0 is an expression ...  */
5622       if ((COMPARISON_CLASS_P (op0)
5623            || UNARY_CLASS_P (op0)
5624            || BINARY_CLASS_P (op0)
5625            || VL_EXP_CLASS_P (op0)
5626            || EXPRESSION_CLASS_P (op0))
5627           /* ... and has wrapping overflow, and its type is smaller
5628              than ctype, then we cannot pass through as widening.  */
5629           && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
5630                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5631                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5632                && (TYPE_PRECISION (ctype)
5633                    > TYPE_PRECISION (TREE_TYPE (op0))))
5634               /* ... or this is a truncation (t is narrower than op0),
5635                  then we cannot pass through this narrowing.  */
5636               || (TYPE_PRECISION (type)
5637                   < TYPE_PRECISION (TREE_TYPE (op0)))
5638               /* ... or signedness changes for division or modulus,
5639                  then we cannot pass through this conversion.  */
5640               || (code != MULT_EXPR
5641                   && (TYPE_UNSIGNED (ctype)
5642                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
5643               /* ... or has undefined overflow while the converted to
5644                  type has not, we cannot do the operation in the inner type
5645                  as that would introduce undefined overflow.  */
5646               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5647                   && !TYPE_OVERFLOW_UNDEFINED (type))))
5648         break;
5649
5650       /* Pass the constant down and see if we can make a simplification.  If
5651          we can, replace this expression with the inner simplification for
5652          possible later conversion to our or some other type.  */
5653       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5654           && TREE_CODE (t2) == INTEGER_CST
5655           && !TREE_OVERFLOW (t2)
5656           && (0 != (t1 = extract_muldiv (op0, t2, code,
5657                                          code == MULT_EXPR
5658                                          ? ctype : NULL_TREE,
5659                                          strict_overflow_p))))
5660         return t1;
5661       break;
5662
5663     case ABS_EXPR:
5664       /* If widening the type changes it from signed to unsigned, then we
5665          must avoid building ABS_EXPR itself as unsigned.  */
5666       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5667         {
5668           tree cstype = (*signed_type_for) (ctype);
5669           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5670               != 0)
5671             {
5672               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5673               return fold_convert (ctype, t1);
5674             }
5675           break;
5676         }
5677       /* If the constant is negative, we cannot simplify this.  */
5678       if (tree_int_cst_sgn (c) == -1)
5679         break;
5680       /* FALLTHROUGH */
5681     case NEGATE_EXPR:
5682       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5683           != 0)
5684         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5685       break;
5686
5687     case MIN_EXPR:  case MAX_EXPR:
5688       /* If widening the type changes the signedness, then we can't perform
5689          this optimization as that changes the result.  */
5690       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5691         break;
5692
5693       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
5694       sub_strict_overflow_p = false;
5695       if ((t1 = extract_muldiv (op0, c, code, wide_type,
5696                                 &sub_strict_overflow_p)) != 0
5697           && (t2 = extract_muldiv (op1, c, code, wide_type,
5698                                    &sub_strict_overflow_p)) != 0)
5699         {
5700           if (tree_int_cst_sgn (c) < 0)
5701             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5702           if (sub_strict_overflow_p)
5703             *strict_overflow_p = true;
5704           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5705                               fold_convert (ctype, t2));
5706         }
5707       break;
5708
5709     case LSHIFT_EXPR:  case RSHIFT_EXPR:
5710       /* If the second operand is constant, this is a multiplication
5711          or floor division, by a power of two, so we can treat it that
5712          way unless the multiplier or divisor overflows.  Signed
5713          left-shift overflow is implementation-defined rather than
5714          undefined in C90, so do not convert signed left shift into
5715          multiplication.  */
5716       if (TREE_CODE (op1) == INTEGER_CST
5717           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5718           /* const_binop may not detect overflow correctly,
5719              so check for it explicitly here.  */
5720           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5721           && TREE_INT_CST_HIGH (op1) == 0
5722           && 0 != (t1 = fold_convert (ctype,
5723                                       const_binop (LSHIFT_EXPR,
5724                                                    size_one_node,
5725                                                    op1)))
5726           && !TREE_OVERFLOW (t1))
5727         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5728                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
5729                                        ctype,
5730                                        fold_convert (ctype, op0),
5731                                        t1),
5732                                c, code, wide_type, strict_overflow_p);
5733       break;
5734
5735     case PLUS_EXPR:  case MINUS_EXPR:
5736       /* See if we can eliminate the operation on both sides.  If we can, we
5737          can return a new PLUS or MINUS.  If we can't, the only remaining
5738          cases where we can do anything are if the second operand is a
5739          constant.  */
5740       sub_strict_overflow_p = false;
5741       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5742       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5743       if (t1 != 0 && t2 != 0
5744           && (code == MULT_EXPR
5745               /* If not multiplication, we can only do this if both operands
5746                  are divisible by c.  */
5747               || (multiple_of_p (ctype, op0, c)
5748                   && multiple_of_p (ctype, op1, c))))
5749         {
5750           if (sub_strict_overflow_p)
5751             *strict_overflow_p = true;
5752           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5753                               fold_convert (ctype, t2));
5754         }
5755
5756       /* If this was a subtraction, negate OP1 and set it to be an addition.
5757          This simplifies the logic below.  */
5758       if (tcode == MINUS_EXPR)
5759         {
5760           tcode = PLUS_EXPR, op1 = negate_expr (op1);
5761           /* If OP1 was not easily negatable, the constant may be OP0.  */
5762           if (TREE_CODE (op0) == INTEGER_CST)
5763             {
5764               tree tem = op0;
5765               op0 = op1;
5766               op1 = tem;
5767               tem = t1;
5768               t1 = t2;
5769               t2 = tem;
5770             }
5771         }
5772
5773       if (TREE_CODE (op1) != INTEGER_CST)
5774         break;
5775
5776       /* If either OP1 or C are negative, this optimization is not safe for
5777          some of the division and remainder types while for others we need
5778          to change the code.  */
5779       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5780         {
5781           if (code == CEIL_DIV_EXPR)
5782             code = FLOOR_DIV_EXPR;
5783           else if (code == FLOOR_DIV_EXPR)
5784             code = CEIL_DIV_EXPR;
5785           else if (code != MULT_EXPR
5786                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5787             break;
5788         }
5789
5790       /* If it's a multiply or a division/modulus operation of a multiple
5791          of our constant, do the operation and verify it doesn't overflow.  */
5792       if (code == MULT_EXPR
5793           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5794         {
5795           op1 = const_binop (code, fold_convert (ctype, op1),
5796                              fold_convert (ctype, c));
5797           /* We allow the constant to overflow with wrapping semantics.  */
5798           if (op1 == 0
5799               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5800             break;
5801         }
5802       else
5803         break;
5804
5805       /* If we have an unsigned type is not a sizetype, we cannot widen
5806          the operation since it will change the result if the original
5807          computation overflowed.  */
5808       if (TYPE_UNSIGNED (ctype)
5809           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5810           && ctype != type)
5811         break;
5812
5813       /* If we were able to eliminate our operation from the first side,
5814          apply our operation to the second side and reform the PLUS.  */
5815       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5816         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5817
5818       /* The last case is if we are a multiply.  In that case, we can
5819          apply the distributive law to commute the multiply and addition
5820          if the multiplication of the constants doesn't overflow.  */
5821       if (code == MULT_EXPR)
5822         return fold_build2 (tcode, ctype,
5823                             fold_build2 (code, ctype,
5824                                          fold_convert (ctype, op0),
5825                                          fold_convert (ctype, c)),
5826                             op1);
5827
5828       break;
5829
5830     case MULT_EXPR:
5831       /* We have a special case here if we are doing something like
5832          (C * 8) % 4 since we know that's zero.  */
5833       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5834            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5835           /* If the multiplication can overflow we cannot optimize this.
5836              ???  Until we can properly mark individual operations as
5837              not overflowing we need to treat sizetype special here as
5838              stor-layout relies on this opimization to make
5839              DECL_FIELD_BIT_OFFSET always a constant.  */
5840           && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
5841               || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
5842                   && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
5843           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5844           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5845         {
5846           *strict_overflow_p = true;
5847           return omit_one_operand (type, integer_zero_node, op0);
5848         }
5849
5850       /* ... fall through ...  */
5851
5852     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
5853     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
5854       /* If we can extract our operation from the LHS, do so and return a
5855          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
5856          do something only if the second operand is a constant.  */
5857       if (same_p
5858           && (t1 = extract_muldiv (op0, c, code, wide_type,
5859                                    strict_overflow_p)) != 0)
5860         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5861                             fold_convert (ctype, op1));
5862       else if (tcode == MULT_EXPR && code == MULT_EXPR
5863                && (t1 = extract_muldiv (op1, c, code, wide_type,
5864                                         strict_overflow_p)) != 0)
5865         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5866                             fold_convert (ctype, t1));
5867       else if (TREE_CODE (op1) != INTEGER_CST)
5868         return 0;
5869
5870       /* If these are the same operation types, we can associate them
5871          assuming no overflow.  */
5872       if (tcode == code
5873           && 0 != (t1 = int_const_binop (MULT_EXPR,
5874                                          fold_convert (ctype, op1),
5875                                          fold_convert (ctype, c), 1))
5876           && 0 != (t1 = force_fit_type_double (ctype, tree_to_double_int (t1),
5877                                                (TYPE_UNSIGNED (ctype)
5878                                                 && tcode != MULT_EXPR) ? -1 : 1,
5879                                                TREE_OVERFLOW (t1)))
5880           && !TREE_OVERFLOW (t1))
5881         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
5882
5883       /* If these operations "cancel" each other, we have the main
5884          optimizations of this pass, which occur when either constant is a
5885          multiple of the other, in which case we replace this with either an
5886          operation or CODE or TCODE.
5887
5888          If we have an unsigned type that is not a sizetype, we cannot do
5889          this since it will change the result if the original computation
5890          overflowed.  */
5891       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
5892            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5893           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5894               || (tcode == MULT_EXPR
5895                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5896                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
5897                   && code != MULT_EXPR)))
5898         {
5899           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5900             {
5901               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5902                 *strict_overflow_p = true;
5903               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5904                                   fold_convert (ctype,
5905                                                 const_binop (TRUNC_DIV_EXPR,
5906                                                              op1, c)));
5907             }
5908           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1)))
5909             {
5910               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5911                 *strict_overflow_p = true;
5912               return fold_build2 (code, ctype, fold_convert (ctype, op0),
5913                                   fold_convert (ctype,
5914                                                 const_binop (TRUNC_DIV_EXPR,
5915                                                              c, op1)));
5916             }
5917         }
5918       break;
5919
5920     default:
5921       break;
5922     }
5923
5924   return 0;
5925 }
5926 \f
5927 /* Return a node which has the indicated constant VALUE (either 0 or
5928    1), and is of the indicated TYPE.  */
5929
5930 tree
5931 constant_boolean_node (int value, tree type)
5932 {
5933   if (type == integer_type_node)
5934     return value ? integer_one_node : integer_zero_node;
5935   else if (type == boolean_type_node)
5936     return value ? boolean_true_node : boolean_false_node;
5937   else
5938     return build_int_cst (type, value);
5939 }
5940
5941
5942 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
5943    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
5944    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
5945    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
5946    COND is the first argument to CODE; otherwise (as in the example
5947    given here), it is the second argument.  TYPE is the type of the
5948    original expression.  Return NULL_TREE if no simplification is
5949    possible.  */
5950
5951 static tree
5952 fold_binary_op_with_conditional_arg (location_t loc,
5953                                      enum tree_code code,
5954                                      tree type, tree op0, tree op1,
5955                                      tree cond, tree arg, int cond_first_p)
5956 {
5957   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
5958   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
5959   tree test, true_value, false_value;
5960   tree lhs = NULL_TREE;
5961   tree rhs = NULL_TREE;
5962
5963   if (TREE_CODE (cond) == COND_EXPR)
5964     {
5965       test = TREE_OPERAND (cond, 0);
5966       true_value = TREE_OPERAND (cond, 1);
5967       false_value = TREE_OPERAND (cond, 2);
5968       /* If this operand throws an expression, then it does not make
5969          sense to try to perform a logical or arithmetic operation
5970          involving it.  */
5971       if (VOID_TYPE_P (TREE_TYPE (true_value)))
5972         lhs = true_value;
5973       if (VOID_TYPE_P (TREE_TYPE (false_value)))
5974         rhs = false_value;
5975     }
5976   else
5977     {
5978       tree testtype = TREE_TYPE (cond);
5979       test = cond;
5980       true_value = constant_boolean_node (true, testtype);
5981       false_value = constant_boolean_node (false, testtype);
5982     }
5983
5984   /* This transformation is only worthwhile if we don't have to wrap ARG
5985      in a SAVE_EXPR and the operation can be simplified on at least one
5986      of the branches once its pushed inside the COND_EXPR.  */
5987   if (!TREE_CONSTANT (arg)
5988       && (TREE_SIDE_EFFECTS (arg)
5989           || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
5990     return NULL_TREE;
5991
5992   arg = fold_convert_loc (loc, arg_type, arg);
5993   if (lhs == 0)
5994     {
5995       true_value = fold_convert_loc (loc, cond_type, true_value);
5996       if (cond_first_p)
5997         lhs = fold_build2_loc (loc, code, type, true_value, arg);
5998       else
5999         lhs = fold_build2_loc (loc, code, type, arg, true_value);
6000     }
6001   if (rhs == 0)
6002     {
6003       false_value = fold_convert_loc (loc, cond_type, false_value);
6004       if (cond_first_p)
6005         rhs = fold_build2_loc (loc, code, type, false_value, arg);
6006       else
6007         rhs = fold_build2_loc (loc, code, type, arg, false_value);
6008     }
6009
6010   /* Check that we have simplified at least one of the branches.  */
6011   if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6012     return NULL_TREE;
6013
6014   return fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs);
6015 }
6016
6017 \f
6018 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6019
6020    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6021    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6022    ADDEND is the same as X.
6023
6024    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6025    and finite.  The problematic cases are when X is zero, and its mode
6026    has signed zeros.  In the case of rounding towards -infinity,
6027    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6028    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6029
6030 bool
6031 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6032 {
6033   if (!real_zerop (addend))
6034     return false;
6035
6036   /* Don't allow the fold with -fsignaling-nans.  */
6037   if (HONOR_SNANS (TYPE_MODE (type)))
6038     return false;
6039
6040   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6041   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6042     return true;
6043
6044   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6045   if (TREE_CODE (addend) == REAL_CST
6046       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6047     negate = !negate;
6048
6049   /* The mode has signed zeros, and we have to honor their sign.
6050      In this situation, there is only one case we can return true for.
6051      X - 0 is the same as X unless rounding towards -infinity is
6052      supported.  */
6053   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6054 }
6055
6056 /* Subroutine of fold() that checks comparisons of built-in math
6057    functions against real constants.
6058
6059    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6060    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6061    is the type of the result and ARG0 and ARG1 are the operands of the
6062    comparison.  ARG1 must be a TREE_REAL_CST.
6063
6064    The function returns the constant folded tree if a simplification
6065    can be made, and NULL_TREE otherwise.  */
6066
6067 static tree
6068 fold_mathfn_compare (location_t loc,
6069                      enum built_in_function fcode, enum tree_code code,
6070                      tree type, tree arg0, tree arg1)
6071 {
6072   REAL_VALUE_TYPE c;
6073
6074   if (BUILTIN_SQRT_P (fcode))
6075     {
6076       tree arg = CALL_EXPR_ARG (arg0, 0);
6077       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6078
6079       c = TREE_REAL_CST (arg1);
6080       if (REAL_VALUE_NEGATIVE (c))
6081         {
6082           /* sqrt(x) < y is always false, if y is negative.  */
6083           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6084             return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6085
6086           /* sqrt(x) > y is always true, if y is negative and we
6087              don't care about NaNs, i.e. negative values of x.  */
6088           if (code == NE_EXPR || !HONOR_NANS (mode))
6089             return omit_one_operand_loc (loc, type, integer_one_node, arg);
6090
6091           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6092           return fold_build2_loc (loc, GE_EXPR, type, arg,
6093                               build_real (TREE_TYPE (arg), dconst0));
6094         }
6095       else if (code == GT_EXPR || code == GE_EXPR)
6096         {
6097           REAL_VALUE_TYPE c2;
6098
6099           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6100           real_convert (&c2, mode, &c2);
6101
6102           if (REAL_VALUE_ISINF (c2))
6103             {
6104               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6105               if (HONOR_INFINITIES (mode))
6106                 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6107                                     build_real (TREE_TYPE (arg), c2));
6108
6109               /* sqrt(x) > y is always false, when y is very large
6110                  and we don't care about infinities.  */
6111               return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6112             }
6113
6114           /* sqrt(x) > c is the same as x > c*c.  */
6115           return fold_build2_loc (loc, code, type, arg,
6116                               build_real (TREE_TYPE (arg), c2));
6117         }
6118       else if (code == LT_EXPR || code == LE_EXPR)
6119         {
6120           REAL_VALUE_TYPE c2;
6121
6122           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6123           real_convert (&c2, mode, &c2);
6124
6125           if (REAL_VALUE_ISINF (c2))
6126             {
6127               /* sqrt(x) < y is always true, when y is a very large
6128                  value and we don't care about NaNs or Infinities.  */
6129               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6130                 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6131
6132               /* sqrt(x) < y is x != +Inf when y is very large and we
6133                  don't care about NaNs.  */
6134               if (! HONOR_NANS (mode))
6135                 return fold_build2_loc (loc, NE_EXPR, type, arg,
6136                                     build_real (TREE_TYPE (arg), c2));
6137
6138               /* sqrt(x) < y is x >= 0 when y is very large and we
6139                  don't care about Infinities.  */
6140               if (! HONOR_INFINITIES (mode))
6141                 return fold_build2_loc (loc, GE_EXPR, type, arg,
6142                                     build_real (TREE_TYPE (arg), dconst0));
6143
6144               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6145               if (lang_hooks.decls.global_bindings_p () != 0
6146                   || CONTAINS_PLACEHOLDER_P (arg))
6147                 return NULL_TREE;
6148
6149               arg = save_expr (arg);
6150               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6151                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6152                                                build_real (TREE_TYPE (arg),
6153                                                            dconst0)),
6154                                   fold_build2_loc (loc, NE_EXPR, type, arg,
6155                                                build_real (TREE_TYPE (arg),
6156                                                            c2)));
6157             }
6158
6159           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6160           if (! HONOR_NANS (mode))
6161             return fold_build2_loc (loc, code, type, arg,
6162                                 build_real (TREE_TYPE (arg), c2));
6163
6164           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6165           if (lang_hooks.decls.global_bindings_p () == 0
6166               && ! CONTAINS_PLACEHOLDER_P (arg))
6167             {
6168               arg = save_expr (arg);
6169               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6170                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6171                                                build_real (TREE_TYPE (arg),
6172                                                            dconst0)),
6173                                   fold_build2_loc (loc, code, type, arg,
6174                                                build_real (TREE_TYPE (arg),
6175                                                            c2)));
6176             }
6177         }
6178     }
6179
6180   return NULL_TREE;
6181 }
6182
6183 /* Subroutine of fold() that optimizes comparisons against Infinities,
6184    either +Inf or -Inf.
6185
6186    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6187    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6188    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6189
6190    The function returns the constant folded tree if a simplification
6191    can be made, and NULL_TREE otherwise.  */
6192
6193 static tree
6194 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6195                   tree arg0, tree arg1)
6196 {
6197   enum machine_mode mode;
6198   REAL_VALUE_TYPE max;
6199   tree temp;
6200   bool neg;
6201
6202   mode = TYPE_MODE (TREE_TYPE (arg0));
6203
6204   /* For negative infinity swap the sense of the comparison.  */
6205   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6206   if (neg)
6207     code = swap_tree_comparison (code);
6208
6209   switch (code)
6210     {
6211     case GT_EXPR:
6212       /* x > +Inf is always false, if with ignore sNANs.  */
6213       if (HONOR_SNANS (mode))
6214         return NULL_TREE;
6215       return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6216
6217     case LE_EXPR:
6218       /* x <= +Inf is always true, if we don't case about NaNs.  */
6219       if (! HONOR_NANS (mode))
6220         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6221
6222       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6223       if (lang_hooks.decls.global_bindings_p () == 0
6224           && ! CONTAINS_PLACEHOLDER_P (arg0))
6225         {
6226           arg0 = save_expr (arg0);
6227           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6228         }
6229       break;
6230
6231     case EQ_EXPR:
6232     case GE_EXPR:
6233       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6234       real_maxval (&max, neg, mode);
6235       return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6236                           arg0, build_real (TREE_TYPE (arg0), max));
6237
6238     case LT_EXPR:
6239       /* x < +Inf is always equal to x <= DBL_MAX.  */
6240       real_maxval (&max, neg, mode);
6241       return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6242                           arg0, build_real (TREE_TYPE (arg0), max));
6243
6244     case NE_EXPR:
6245       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6246       real_maxval (&max, neg, mode);
6247       if (! HONOR_NANS (mode))
6248         return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6249                             arg0, build_real (TREE_TYPE (arg0), max));
6250
6251       temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6252                           arg0, build_real (TREE_TYPE (arg0), max));
6253       return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
6254
6255     default:
6256       break;
6257     }
6258
6259   return NULL_TREE;
6260 }
6261
6262 /* Subroutine of fold() that optimizes comparisons of a division by
6263    a nonzero integer constant against an integer constant, i.e.
6264    X/C1 op C2.
6265
6266    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6267    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6268    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6269
6270    The function returns the constant folded tree if a simplification
6271    can be made, and NULL_TREE otherwise.  */
6272
6273 static tree
6274 fold_div_compare (location_t loc,
6275                   enum tree_code code, tree type, tree arg0, tree arg1)
6276 {
6277   tree prod, tmp, hi, lo;
6278   tree arg00 = TREE_OPERAND (arg0, 0);
6279   tree arg01 = TREE_OPERAND (arg0, 1);
6280   double_int val;
6281   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6282   bool neg_overflow;
6283   int overflow;
6284
6285   /* We have to do this the hard way to detect unsigned overflow.
6286      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
6287   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6288                                    TREE_INT_CST_HIGH (arg01),
6289                                    TREE_INT_CST_LOW (arg1),
6290                                    TREE_INT_CST_HIGH (arg1),
6291                                    &val.low, &val.high, unsigned_p);
6292   prod = force_fit_type_double (TREE_TYPE (arg00), val, -1, overflow);
6293   neg_overflow = false;
6294
6295   if (unsigned_p)
6296     {
6297       tmp = int_const_binop (MINUS_EXPR, arg01,
6298                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6299       lo = prod;
6300
6301       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
6302       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6303                                        TREE_INT_CST_HIGH (prod),
6304                                        TREE_INT_CST_LOW (tmp),
6305                                        TREE_INT_CST_HIGH (tmp),
6306                                        &val.low, &val.high, unsigned_p);
6307       hi = force_fit_type_double (TREE_TYPE (arg00), val,
6308                                   -1, overflow | TREE_OVERFLOW (prod));
6309     }
6310   else if (tree_int_cst_sgn (arg01) >= 0)
6311     {
6312       tmp = int_const_binop (MINUS_EXPR, arg01,
6313                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6314       switch (tree_int_cst_sgn (arg1))
6315         {
6316         case -1:
6317           neg_overflow = true;
6318           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6319           hi = prod;
6320           break;
6321
6322         case  0:
6323           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6324           hi = tmp;
6325           break;
6326
6327         case  1:
6328           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6329           lo = prod;
6330           break;
6331
6332         default:
6333           gcc_unreachable ();
6334         }
6335     }
6336   else
6337     {
6338       /* A negative divisor reverses the relational operators.  */
6339       code = swap_tree_comparison (code);
6340
6341       tmp = int_const_binop (PLUS_EXPR, arg01,
6342                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6343       switch (tree_int_cst_sgn (arg1))
6344         {
6345         case -1:
6346           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6347           lo = prod;
6348           break;
6349
6350         case  0:
6351           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6352           lo = tmp;
6353           break;
6354
6355         case  1:
6356           neg_overflow = true;
6357           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6358           hi = prod;
6359           break;
6360
6361         default:
6362           gcc_unreachable ();
6363         }
6364     }
6365
6366   switch (code)
6367     {
6368     case EQ_EXPR:
6369       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6370         return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6371       if (TREE_OVERFLOW (hi))
6372         return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6373       if (TREE_OVERFLOW (lo))
6374         return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6375       return build_range_check (loc, type, arg00, 1, lo, hi);
6376
6377     case NE_EXPR:
6378       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6379         return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6380       if (TREE_OVERFLOW (hi))
6381         return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6382       if (TREE_OVERFLOW (lo))
6383         return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6384       return build_range_check (loc, type, arg00, 0, lo, hi);
6385
6386     case LT_EXPR:
6387       if (TREE_OVERFLOW (lo))
6388         {
6389           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6390           return omit_one_operand_loc (loc, type, tmp, arg00);
6391         }
6392       return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6393
6394     case LE_EXPR:
6395       if (TREE_OVERFLOW (hi))
6396         {
6397           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6398           return omit_one_operand_loc (loc, type, tmp, arg00);
6399         }
6400       return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6401
6402     case GT_EXPR:
6403       if (TREE_OVERFLOW (hi))
6404         {
6405           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6406           return omit_one_operand_loc (loc, type, tmp, arg00);
6407         }
6408       return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6409
6410     case GE_EXPR:
6411       if (TREE_OVERFLOW (lo))
6412         {
6413           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6414           return omit_one_operand_loc (loc, type, tmp, arg00);
6415         }
6416       return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6417
6418     default:
6419       break;
6420     }
6421
6422   return NULL_TREE;
6423 }
6424
6425
6426 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6427    equality/inequality test, then return a simplified form of the test
6428    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6429    result type.  */
6430
6431 static tree
6432 fold_single_bit_test_into_sign_test (location_t loc,
6433                                      enum tree_code code, tree arg0, tree arg1,
6434                                      tree result_type)
6435 {
6436   /* If this is testing a single bit, we can optimize the test.  */
6437   if ((code == NE_EXPR || code == EQ_EXPR)
6438       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6439       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6440     {
6441       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6442          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6443       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6444
6445       if (arg00 != NULL_TREE
6446           /* This is only a win if casting to a signed type is cheap,
6447              i.e. when arg00's type is not a partial mode.  */
6448           && TYPE_PRECISION (TREE_TYPE (arg00))
6449              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6450         {
6451           tree stype = signed_type_for (TREE_TYPE (arg00));
6452           return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6453                               result_type,
6454                               fold_convert_loc (loc, stype, arg00),
6455                               build_int_cst (stype, 0));
6456         }
6457     }
6458
6459   return NULL_TREE;
6460 }
6461
6462 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6463    equality/inequality test, then return a simplified form of
6464    the test using shifts and logical operations.  Otherwise return
6465    NULL.  TYPE is the desired result type.  */
6466
6467 tree
6468 fold_single_bit_test (location_t loc, enum tree_code code,
6469                       tree arg0, tree arg1, tree result_type)
6470 {
6471   /* If this is testing a single bit, we can optimize the test.  */
6472   if ((code == NE_EXPR || code == EQ_EXPR)
6473       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6474       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6475     {
6476       tree inner = TREE_OPERAND (arg0, 0);
6477       tree type = TREE_TYPE (arg0);
6478       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6479       enum machine_mode operand_mode = TYPE_MODE (type);
6480       int ops_unsigned;
6481       tree signed_type, unsigned_type, intermediate_type;
6482       tree tem, one;
6483
6484       /* First, see if we can fold the single bit test into a sign-bit
6485          test.  */
6486       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6487                                                  result_type);
6488       if (tem)
6489         return tem;
6490
6491       /* Otherwise we have (A & C) != 0 where C is a single bit,
6492          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6493          Similarly for (A & C) == 0.  */
6494
6495       /* If INNER is a right shift of a constant and it plus BITNUM does
6496          not overflow, adjust BITNUM and INNER.  */
6497       if (TREE_CODE (inner) == RSHIFT_EXPR
6498           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6499           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6500           && bitnum < TYPE_PRECISION (type)
6501           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6502                                    bitnum - TYPE_PRECISION (type)))
6503         {
6504           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6505           inner = TREE_OPERAND (inner, 0);
6506         }
6507
6508       /* If we are going to be able to omit the AND below, we must do our
6509          operations as unsigned.  If we must use the AND, we have a choice.
6510          Normally unsigned is faster, but for some machines signed is.  */
6511 #ifdef LOAD_EXTEND_OP
6512       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6513                       && !flag_syntax_only) ? 0 : 1;
6514 #else
6515       ops_unsigned = 1;
6516 #endif
6517
6518       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6519       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6520       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6521       inner = fold_convert_loc (loc, intermediate_type, inner);
6522
6523       if (bitnum != 0)
6524         inner = build2 (RSHIFT_EXPR, intermediate_type,
6525                         inner, size_int (bitnum));
6526
6527       one = build_int_cst (intermediate_type, 1);
6528
6529       if (code == EQ_EXPR)
6530         inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6531
6532       /* Put the AND last so it can combine with more things.  */
6533       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6534
6535       /* Make sure to return the proper type.  */
6536       inner = fold_convert_loc (loc, result_type, inner);
6537
6538       return inner;
6539     }
6540   return NULL_TREE;
6541 }
6542
6543 /* Check whether we are allowed to reorder operands arg0 and arg1,
6544    such that the evaluation of arg1 occurs before arg0.  */
6545
6546 static bool
6547 reorder_operands_p (const_tree arg0, const_tree arg1)
6548 {
6549   if (! flag_evaluation_order)
6550       return true;
6551   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6552     return true;
6553   return ! TREE_SIDE_EFFECTS (arg0)
6554          && ! TREE_SIDE_EFFECTS (arg1);
6555 }
6556
6557 /* Test whether it is preferable two swap two operands, ARG0 and
6558    ARG1, for example because ARG0 is an integer constant and ARG1
6559    isn't.  If REORDER is true, only recommend swapping if we can
6560    evaluate the operands in reverse order.  */
6561
6562 bool
6563 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6564 {
6565   STRIP_SIGN_NOPS (arg0);
6566   STRIP_SIGN_NOPS (arg1);
6567
6568   if (TREE_CODE (arg1) == INTEGER_CST)
6569     return 0;
6570   if (TREE_CODE (arg0) == INTEGER_CST)
6571     return 1;
6572
6573   if (TREE_CODE (arg1) == REAL_CST)
6574     return 0;
6575   if (TREE_CODE (arg0) == REAL_CST)
6576     return 1;
6577
6578   if (TREE_CODE (arg1) == FIXED_CST)
6579     return 0;
6580   if (TREE_CODE (arg0) == FIXED_CST)
6581     return 1;
6582
6583   if (TREE_CODE (arg1) == COMPLEX_CST)
6584     return 0;
6585   if (TREE_CODE (arg0) == COMPLEX_CST)
6586     return 1;
6587
6588   if (TREE_CONSTANT (arg1))
6589     return 0;
6590   if (TREE_CONSTANT (arg0))
6591     return 1;
6592
6593   if (optimize_function_for_size_p (cfun))
6594     return 0;
6595
6596   if (reorder && flag_evaluation_order
6597       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6598     return 0;
6599
6600   /* It is preferable to swap two SSA_NAME to ensure a canonical form
6601      for commutative and comparison operators.  Ensuring a canonical
6602      form allows the optimizers to find additional redundancies without
6603      having to explicitly check for both orderings.  */
6604   if (TREE_CODE (arg0) == SSA_NAME
6605       && TREE_CODE (arg1) == SSA_NAME
6606       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6607     return 1;
6608
6609   /* Put SSA_NAMEs last.  */
6610   if (TREE_CODE (arg1) == SSA_NAME)
6611     return 0;
6612   if (TREE_CODE (arg0) == SSA_NAME)
6613     return 1;
6614
6615   /* Put variables last.  */
6616   if (DECL_P (arg1))
6617     return 0;
6618   if (DECL_P (arg0))
6619     return 1;
6620
6621   return 0;
6622 }
6623
6624 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6625    ARG0 is extended to a wider type.  */
6626
6627 static tree
6628 fold_widened_comparison (location_t loc, enum tree_code code,
6629                          tree type, tree arg0, tree arg1)
6630 {
6631   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6632   tree arg1_unw;
6633   tree shorter_type, outer_type;
6634   tree min, max;
6635   bool above, below;
6636
6637   if (arg0_unw == arg0)
6638     return NULL_TREE;
6639   shorter_type = TREE_TYPE (arg0_unw);
6640
6641 #ifdef HAVE_canonicalize_funcptr_for_compare
6642   /* Disable this optimization if we're casting a function pointer
6643      type on targets that require function pointer canonicalization.  */
6644   if (HAVE_canonicalize_funcptr_for_compare
6645       && TREE_CODE (shorter_type) == POINTER_TYPE
6646       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6647     return NULL_TREE;
6648 #endif
6649
6650   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6651     return NULL_TREE;
6652
6653   arg1_unw = get_unwidened (arg1, NULL_TREE);
6654
6655   /* If possible, express the comparison in the shorter mode.  */
6656   if ((code == EQ_EXPR || code == NE_EXPR
6657        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6658       && (TREE_TYPE (arg1_unw) == shorter_type
6659           || ((TYPE_PRECISION (shorter_type)
6660                >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6661               && (TYPE_UNSIGNED (shorter_type)
6662                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6663           || (TREE_CODE (arg1_unw) == INTEGER_CST
6664               && (TREE_CODE (shorter_type) == INTEGER_TYPE
6665                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6666               && int_fits_type_p (arg1_unw, shorter_type))))
6667     return fold_build2_loc (loc, code, type, arg0_unw,
6668                         fold_convert_loc (loc, shorter_type, arg1_unw));
6669
6670   if (TREE_CODE (arg1_unw) != INTEGER_CST
6671       || TREE_CODE (shorter_type) != INTEGER_TYPE
6672       || !int_fits_type_p (arg1_unw, shorter_type))
6673     return NULL_TREE;
6674
6675   /* If we are comparing with the integer that does not fit into the range
6676      of the shorter type, the result is known.  */
6677   outer_type = TREE_TYPE (arg1_unw);
6678   min = lower_bound_in_type (outer_type, shorter_type);
6679   max = upper_bound_in_type (outer_type, shorter_type);
6680
6681   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6682                                                    max, arg1_unw));
6683   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6684                                                    arg1_unw, min));
6685
6686   switch (code)
6687     {
6688     case EQ_EXPR:
6689       if (above || below)
6690         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6691       break;
6692
6693     case NE_EXPR:
6694       if (above || below)
6695         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6696       break;
6697
6698     case LT_EXPR:
6699     case LE_EXPR:
6700       if (above)
6701         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6702       else if (below)
6703         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6704
6705     case GT_EXPR:
6706     case GE_EXPR:
6707       if (above)
6708         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6709       else if (below)
6710         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6711
6712     default:
6713       break;
6714     }
6715
6716   return NULL_TREE;
6717 }
6718
6719 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6720    ARG0 just the signedness is changed.  */
6721
6722 static tree
6723 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
6724                               tree arg0, tree arg1)
6725 {
6726   tree arg0_inner;
6727   tree inner_type, outer_type;
6728
6729   if (!CONVERT_EXPR_P (arg0))
6730     return NULL_TREE;
6731
6732   outer_type = TREE_TYPE (arg0);
6733   arg0_inner = TREE_OPERAND (arg0, 0);
6734   inner_type = TREE_TYPE (arg0_inner);
6735
6736 #ifdef HAVE_canonicalize_funcptr_for_compare
6737   /* Disable this optimization if we're casting a function pointer
6738      type on targets that require function pointer canonicalization.  */
6739   if (HAVE_canonicalize_funcptr_for_compare
6740       && TREE_CODE (inner_type) == POINTER_TYPE
6741       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6742     return NULL_TREE;
6743 #endif
6744
6745   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6746     return NULL_TREE;
6747
6748   if (TREE_CODE (arg1) != INTEGER_CST
6749       && !(CONVERT_EXPR_P (arg1)
6750            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6751     return NULL_TREE;
6752
6753   if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6754        || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
6755       && code != NE_EXPR
6756       && code != EQ_EXPR)
6757     return NULL_TREE;
6758
6759   if (TREE_CODE (arg1) == INTEGER_CST)
6760     arg1 = force_fit_type_double (inner_type, tree_to_double_int (arg1),
6761                                   0, TREE_OVERFLOW (arg1));
6762   else
6763     arg1 = fold_convert_loc (loc, inner_type, arg1);
6764
6765   return fold_build2_loc (loc, code, type, arg0_inner, arg1);
6766 }
6767
6768 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6769    step of the array.  Reconstructs s and delta in the case of s *
6770    delta being an integer constant (and thus already folded).  ADDR is
6771    the address. MULT is the multiplicative expression.  If the
6772    function succeeds, the new address expression is returned.
6773    Otherwise NULL_TREE is returned.  LOC is the location of the
6774    resulting expression.  */
6775
6776 static tree
6777 try_move_mult_to_index (location_t loc, tree addr, tree op1)
6778 {
6779   tree s, delta, step;
6780   tree ref = TREE_OPERAND (addr, 0), pref;
6781   tree ret, pos;
6782   tree itype;
6783   bool mdim = false;
6784
6785   /*  Strip the nops that might be added when converting op1 to sizetype. */
6786   STRIP_NOPS (op1);
6787
6788   /* Canonicalize op1 into a possibly non-constant delta
6789      and an INTEGER_CST s.  */
6790   if (TREE_CODE (op1) == MULT_EXPR)
6791     {
6792       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6793
6794       STRIP_NOPS (arg0);
6795       STRIP_NOPS (arg1);
6796
6797       if (TREE_CODE (arg0) == INTEGER_CST)
6798         {
6799           s = arg0;
6800           delta = arg1;
6801         }
6802       else if (TREE_CODE (arg1) == INTEGER_CST)
6803         {
6804           s = arg1;
6805           delta = arg0;
6806         }
6807       else
6808         return NULL_TREE;
6809     }
6810   else if (TREE_CODE (op1) == INTEGER_CST)
6811     {
6812       delta = op1;
6813       s = NULL_TREE;
6814     }
6815   else
6816     {
6817       /* Simulate we are delta * 1.  */
6818       delta = op1;
6819       s = integer_one_node;
6820     }
6821
6822   for (;; ref = TREE_OPERAND (ref, 0))
6823     {
6824       if (TREE_CODE (ref) == ARRAY_REF)
6825         {
6826           tree domain;
6827
6828           /* Remember if this was a multi-dimensional array.  */
6829           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6830             mdim = true;
6831
6832           domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6833           if (! domain)
6834             continue;
6835           itype = TREE_TYPE (domain);
6836
6837           step = array_ref_element_size (ref);
6838           if (TREE_CODE (step) != INTEGER_CST)
6839             continue;
6840
6841           if (s)
6842             {
6843               if (! tree_int_cst_equal (step, s))
6844                 continue;
6845             }
6846           else
6847             {
6848               /* Try if delta is a multiple of step.  */
6849               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6850               if (! tmp)
6851                 continue;
6852               delta = tmp;
6853             }
6854
6855           /* Only fold here if we can verify we do not overflow one
6856              dimension of a multi-dimensional array.  */
6857           if (mdim)
6858             {
6859               tree tmp;
6860
6861               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
6862                   || !TYPE_MAX_VALUE (domain)
6863                   || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
6864                 continue;
6865
6866               tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
6867                                      fold_convert_loc (loc, itype,
6868                                                        TREE_OPERAND (ref, 1)),
6869                                      fold_convert_loc (loc, itype, delta));
6870               if (!tmp
6871                   || TREE_CODE (tmp) != INTEGER_CST
6872                   || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
6873                 continue;
6874             }
6875
6876           break;
6877         }
6878       else
6879         mdim = false;
6880
6881       if (!handled_component_p (ref))
6882         return NULL_TREE;
6883     }
6884
6885   /* We found the suitable array reference.  So copy everything up to it,
6886      and replace the index.  */
6887
6888   pref = TREE_OPERAND (addr, 0);
6889   ret = copy_node (pref);
6890   SET_EXPR_LOCATION (ret, loc);
6891   pos = ret;
6892
6893   while (pref != ref)
6894     {
6895       pref = TREE_OPERAND (pref, 0);
6896       TREE_OPERAND (pos, 0) = copy_node (pref);
6897       pos = TREE_OPERAND (pos, 0);
6898     }
6899
6900   TREE_OPERAND (pos, 1) = fold_build2_loc (loc, PLUS_EXPR, itype,
6901                                        fold_convert_loc (loc, itype,
6902                                                          TREE_OPERAND (pos, 1)),
6903                                        fold_convert_loc (loc, itype, delta));
6904
6905   return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
6906 }
6907
6908
6909 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
6910    means A >= Y && A != MAX, but in this case we know that
6911    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
6912
6913 static tree
6914 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6915 {
6916   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6917
6918   if (TREE_CODE (bound) == LT_EXPR)
6919     a = TREE_OPERAND (bound, 0);
6920   else if (TREE_CODE (bound) == GT_EXPR)
6921     a = TREE_OPERAND (bound, 1);
6922   else
6923     return NULL_TREE;
6924
6925   typea = TREE_TYPE (a);
6926   if (!INTEGRAL_TYPE_P (typea)
6927       && !POINTER_TYPE_P (typea))
6928     return NULL_TREE;
6929
6930   if (TREE_CODE (ineq) == LT_EXPR)
6931     {
6932       a1 = TREE_OPERAND (ineq, 1);
6933       y = TREE_OPERAND (ineq, 0);
6934     }
6935   else if (TREE_CODE (ineq) == GT_EXPR)
6936     {
6937       a1 = TREE_OPERAND (ineq, 0);
6938       y = TREE_OPERAND (ineq, 1);
6939     }
6940   else
6941     return NULL_TREE;
6942
6943   if (TREE_TYPE (a1) != typea)
6944     return NULL_TREE;
6945
6946   if (POINTER_TYPE_P (typea))
6947     {
6948       /* Convert the pointer types into integer before taking the difference.  */
6949       tree ta = fold_convert_loc (loc, ssizetype, a);
6950       tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6951       diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6952     }
6953   else
6954     diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6955
6956   if (!diff || !integer_onep (diff))
6957    return NULL_TREE;
6958
6959   return fold_build2_loc (loc, GE_EXPR, type, a, y);
6960 }
6961
6962 /* Fold a sum or difference of at least one multiplication.
6963    Returns the folded tree or NULL if no simplification could be made.  */
6964
6965 static tree
6966 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6967                           tree arg0, tree arg1)
6968 {
6969   tree arg00, arg01, arg10, arg11;
6970   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6971
6972   /* (A * C) +- (B * C) -> (A+-B) * C.
6973      (A * C) +- A -> A * (C+-1).
6974      We are most concerned about the case where C is a constant,
6975      but other combinations show up during loop reduction.  Since
6976      it is not difficult, try all four possibilities.  */
6977
6978   if (TREE_CODE (arg0) == MULT_EXPR)
6979     {
6980       arg00 = TREE_OPERAND (arg0, 0);
6981       arg01 = TREE_OPERAND (arg0, 1);
6982     }
6983   else if (TREE_CODE (arg0) == INTEGER_CST)
6984     {
6985       arg00 = build_one_cst (type);
6986       arg01 = arg0;
6987     }
6988   else
6989     {
6990       /* We cannot generate constant 1 for fract.  */
6991       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6992         return NULL_TREE;
6993       arg00 = arg0;
6994       arg01 = build_one_cst (type);
6995     }
6996   if (TREE_CODE (arg1) == MULT_EXPR)
6997     {
6998       arg10 = TREE_OPERAND (arg1, 0);
6999       arg11 = TREE_OPERAND (arg1, 1);
7000     }
7001   else if (TREE_CODE (arg1) == INTEGER_CST)
7002     {
7003       arg10 = build_one_cst (type);
7004       /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7005          the purpose of this canonicalization.  */
7006       if (TREE_INT_CST_HIGH (arg1) == -1
7007           && negate_expr_p (arg1)
7008           && code == PLUS_EXPR)
7009         {
7010           arg11 = negate_expr (arg1);
7011           code = MINUS_EXPR;
7012         }
7013       else
7014         arg11 = arg1;
7015     }
7016   else
7017     {
7018       /* We cannot generate constant 1 for fract.  */
7019       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7020         return NULL_TREE;
7021       arg10 = arg1;
7022       arg11 = build_one_cst (type);
7023     }
7024   same = NULL_TREE;
7025
7026   if (operand_equal_p (arg01, arg11, 0))
7027     same = arg01, alt0 = arg00, alt1 = arg10;
7028   else if (operand_equal_p (arg00, arg10, 0))
7029     same = arg00, alt0 = arg01, alt1 = arg11;
7030   else if (operand_equal_p (arg00, arg11, 0))
7031     same = arg00, alt0 = arg01, alt1 = arg10;
7032   else if (operand_equal_p (arg01, arg10, 0))
7033     same = arg01, alt0 = arg00, alt1 = arg11;
7034
7035   /* No identical multiplicands; see if we can find a common
7036      power-of-two factor in non-power-of-two multiplies.  This
7037      can help in multi-dimensional array access.  */
7038   else if (host_integerp (arg01, 0)
7039            && host_integerp (arg11, 0))
7040     {
7041       HOST_WIDE_INT int01, int11, tmp;
7042       bool swap = false;
7043       tree maybe_same;
7044       int01 = TREE_INT_CST_LOW (arg01);
7045       int11 = TREE_INT_CST_LOW (arg11);
7046
7047       /* Move min of absolute values to int11.  */
7048       if ((int01 >= 0 ? int01 : -int01)
7049           < (int11 >= 0 ? int11 : -int11))
7050         {
7051           tmp = int01, int01 = int11, int11 = tmp;
7052           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7053           maybe_same = arg01;
7054           swap = true;
7055         }
7056       else
7057         maybe_same = arg11;
7058
7059       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
7060           /* The remainder should not be a constant, otherwise we
7061              end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7062              increased the number of multiplications necessary.  */
7063           && TREE_CODE (arg10) != INTEGER_CST)
7064         {
7065           alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7066                               build_int_cst (TREE_TYPE (arg00),
7067                                              int01 / int11));
7068           alt1 = arg10;
7069           same = maybe_same;
7070           if (swap)
7071             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7072         }
7073     }
7074
7075   if (same)
7076     return fold_build2_loc (loc, MULT_EXPR, type,
7077                         fold_build2_loc (loc, code, type,
7078                                      fold_convert_loc (loc, type, alt0),
7079                                      fold_convert_loc (loc, type, alt1)),
7080                         fold_convert_loc (loc, type, same));
7081
7082   return NULL_TREE;
7083 }
7084
7085 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7086    specified by EXPR into the buffer PTR of length LEN bytes.
7087    Return the number of bytes placed in the buffer, or zero
7088    upon failure.  */
7089
7090 static int
7091 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7092 {
7093   tree type = TREE_TYPE (expr);
7094   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7095   int byte, offset, word, words;
7096   unsigned char value;
7097
7098   if (total_bytes > len)
7099     return 0;
7100   words = total_bytes / UNITS_PER_WORD;
7101
7102   for (byte = 0; byte < total_bytes; byte++)
7103     {
7104       int bitpos = byte * BITS_PER_UNIT;
7105       if (bitpos < HOST_BITS_PER_WIDE_INT)
7106         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7107       else
7108         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7109                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7110
7111       if (total_bytes > UNITS_PER_WORD)
7112         {
7113           word = byte / UNITS_PER_WORD;
7114           if (WORDS_BIG_ENDIAN)
7115             word = (words - 1) - word;
7116           offset = word * UNITS_PER_WORD;
7117           if (BYTES_BIG_ENDIAN)
7118             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7119           else
7120             offset += byte % UNITS_PER_WORD;
7121         }
7122       else
7123         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7124       ptr[offset] = value;
7125     }
7126   return total_bytes;
7127 }
7128
7129
7130 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7131    specified by EXPR into the buffer PTR of length LEN bytes.
7132    Return the number of bytes placed in the buffer, or zero
7133    upon failure.  */
7134
7135 static int
7136 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7137 {
7138   tree type = TREE_TYPE (expr);
7139   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7140   int byte, offset, word, words, bitpos;
7141   unsigned char value;
7142
7143   /* There are always 32 bits in each long, no matter the size of
7144      the hosts long.  We handle floating point representations with
7145      up to 192 bits.  */
7146   long tmp[6];
7147
7148   if (total_bytes > len)
7149     return 0;
7150   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7151
7152   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7153
7154   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7155        bitpos += BITS_PER_UNIT)
7156     {
7157       byte = (bitpos / BITS_PER_UNIT) & 3;
7158       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7159
7160       if (UNITS_PER_WORD < 4)
7161         {
7162           word = byte / UNITS_PER_WORD;
7163           if (WORDS_BIG_ENDIAN)
7164             word = (words - 1) - word;
7165           offset = word * UNITS_PER_WORD;
7166           if (BYTES_BIG_ENDIAN)
7167             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7168           else
7169             offset += byte % UNITS_PER_WORD;
7170         }
7171       else
7172         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7173       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7174     }
7175   return total_bytes;
7176 }
7177
7178 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7179    specified by EXPR into the buffer PTR of length LEN bytes.
7180    Return the number of bytes placed in the buffer, or zero
7181    upon failure.  */
7182
7183 static int
7184 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7185 {
7186   int rsize, isize;
7187   tree part;
7188
7189   part = TREE_REALPART (expr);
7190   rsize = native_encode_expr (part, ptr, len);
7191   if (rsize == 0)
7192     return 0;
7193   part = TREE_IMAGPART (expr);
7194   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7195   if (isize != rsize)
7196     return 0;
7197   return rsize + isize;
7198 }
7199
7200
7201 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7202    specified by EXPR into the buffer PTR of length LEN bytes.
7203    Return the number of bytes placed in the buffer, or zero
7204    upon failure.  */
7205
7206 static int
7207 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7208 {
7209   int i, size, offset, count;
7210   tree itype, elem, elements;
7211
7212   offset = 0;
7213   elements = TREE_VECTOR_CST_ELTS (expr);
7214   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7215   itype = TREE_TYPE (TREE_TYPE (expr));
7216   size = GET_MODE_SIZE (TYPE_MODE (itype));
7217   for (i = 0; i < count; i++)
7218     {
7219       if (elements)
7220         {
7221           elem = TREE_VALUE (elements);
7222           elements = TREE_CHAIN (elements);
7223         }
7224       else
7225         elem = NULL_TREE;
7226
7227       if (elem)
7228         {
7229           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7230             return 0;
7231         }
7232       else
7233         {
7234           if (offset + size > len)
7235             return 0;
7236           memset (ptr+offset, 0, size);
7237         }
7238       offset += size;
7239     }
7240   return offset;
7241 }
7242
7243
7244 /* Subroutine of native_encode_expr.  Encode the STRING_CST
7245    specified by EXPR into the buffer PTR of length LEN bytes.
7246    Return the number of bytes placed in the buffer, or zero
7247    upon failure.  */
7248
7249 static int
7250 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7251 {
7252   tree type = TREE_TYPE (expr);
7253   HOST_WIDE_INT total_bytes;
7254
7255   if (TREE_CODE (type) != ARRAY_TYPE
7256       || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7257       || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7258       || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7259     return 0;
7260   total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7261   if (total_bytes > len)
7262     return 0;
7263   if (TREE_STRING_LENGTH (expr) < total_bytes)
7264     {
7265       memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7266       memset (ptr + TREE_STRING_LENGTH (expr), 0,
7267               total_bytes - TREE_STRING_LENGTH (expr));
7268     }
7269   else
7270     memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7271   return total_bytes;
7272 }
7273
7274
7275 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7276    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7277    buffer PTR of length LEN bytes.  Return the number of bytes
7278    placed in the buffer, or zero upon failure.  */
7279
7280 int
7281 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7282 {
7283   switch (TREE_CODE (expr))
7284     {
7285     case INTEGER_CST:
7286       return native_encode_int (expr, ptr, len);
7287
7288     case REAL_CST:
7289       return native_encode_real (expr, ptr, len);
7290
7291     case COMPLEX_CST:
7292       return native_encode_complex (expr, ptr, len);
7293
7294     case VECTOR_CST:
7295       return native_encode_vector (expr, ptr, len);
7296
7297     case STRING_CST:
7298       return native_encode_string (expr, ptr, len);
7299
7300     default:
7301       return 0;
7302     }
7303 }
7304
7305
7306 /* Subroutine of native_interpret_expr.  Interpret the contents of
7307    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7308    If the buffer cannot be interpreted, return NULL_TREE.  */
7309
7310 static tree
7311 native_interpret_int (tree type, const unsigned char *ptr, int len)
7312 {
7313   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7314   int byte, offset, word, words;
7315   unsigned char value;
7316   double_int result;
7317
7318   if (total_bytes > len)
7319     return NULL_TREE;
7320   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7321     return NULL_TREE;
7322
7323   result = double_int_zero;
7324   words = total_bytes / UNITS_PER_WORD;
7325
7326   for (byte = 0; byte < total_bytes; byte++)
7327     {
7328       int bitpos = byte * BITS_PER_UNIT;
7329       if (total_bytes > UNITS_PER_WORD)
7330         {
7331           word = byte / UNITS_PER_WORD;
7332           if (WORDS_BIG_ENDIAN)
7333             word = (words - 1) - word;
7334           offset = word * UNITS_PER_WORD;
7335           if (BYTES_BIG_ENDIAN)
7336             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7337           else
7338             offset += byte % UNITS_PER_WORD;
7339         }
7340       else
7341         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7342       value = ptr[offset];
7343
7344       if (bitpos < HOST_BITS_PER_WIDE_INT)
7345         result.low |= (unsigned HOST_WIDE_INT) value << bitpos;
7346       else
7347         result.high |= (unsigned HOST_WIDE_INT) value
7348                        << (bitpos - HOST_BITS_PER_WIDE_INT);
7349     }
7350
7351   return double_int_to_tree (type, result);
7352 }
7353
7354
7355 /* Subroutine of native_interpret_expr.  Interpret the contents of
7356    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7357    If the buffer cannot be interpreted, return NULL_TREE.  */
7358
7359 static tree
7360 native_interpret_real (tree type, const unsigned char *ptr, int len)
7361 {
7362   enum machine_mode mode = TYPE_MODE (type);
7363   int total_bytes = GET_MODE_SIZE (mode);
7364   int byte, offset, word, words, bitpos;
7365   unsigned char value;
7366   /* There are always 32 bits in each long, no matter the size of
7367      the hosts long.  We handle floating point representations with
7368      up to 192 bits.  */
7369   REAL_VALUE_TYPE r;
7370   long tmp[6];
7371
7372   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7373   if (total_bytes > len || total_bytes > 24)
7374     return NULL_TREE;
7375   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7376
7377   memset (tmp, 0, sizeof (tmp));
7378   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7379        bitpos += BITS_PER_UNIT)
7380     {
7381       byte = (bitpos / BITS_PER_UNIT) & 3;
7382       if (UNITS_PER_WORD < 4)
7383         {
7384           word = byte / UNITS_PER_WORD;
7385           if (WORDS_BIG_ENDIAN)
7386             word = (words - 1) - word;
7387           offset = word * UNITS_PER_WORD;
7388           if (BYTES_BIG_ENDIAN)
7389             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7390           else
7391             offset += byte % UNITS_PER_WORD;
7392         }
7393       else
7394         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7395       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7396
7397       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7398     }
7399
7400   real_from_target (&r, tmp, mode);
7401   return build_real (type, r);
7402 }
7403
7404
7405 /* Subroutine of native_interpret_expr.  Interpret the contents of
7406    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7407    If the buffer cannot be interpreted, return NULL_TREE.  */
7408
7409 static tree
7410 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7411 {
7412   tree etype, rpart, ipart;
7413   int size;
7414
7415   etype = TREE_TYPE (type);
7416   size = GET_MODE_SIZE (TYPE_MODE (etype));
7417   if (size * 2 > len)
7418     return NULL_TREE;
7419   rpart = native_interpret_expr (etype, ptr, size);
7420   if (!rpart)
7421     return NULL_TREE;
7422   ipart = native_interpret_expr (etype, ptr+size, size);
7423   if (!ipart)
7424     return NULL_TREE;
7425   return build_complex (type, rpart, ipart);
7426 }
7427
7428
7429 /* Subroutine of native_interpret_expr.  Interpret the contents of
7430    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7431    If the buffer cannot be interpreted, return NULL_TREE.  */
7432
7433 static tree
7434 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7435 {
7436   tree etype, elem, elements;
7437   int i, size, count;
7438
7439   etype = TREE_TYPE (type);
7440   size = GET_MODE_SIZE (TYPE_MODE (etype));
7441   count = TYPE_VECTOR_SUBPARTS (type);
7442   if (size * count > len)
7443     return NULL_TREE;
7444
7445   elements = NULL_TREE;
7446   for (i = count - 1; i >= 0; i--)
7447     {
7448       elem = native_interpret_expr (etype, ptr+(i*size), size);
7449       if (!elem)
7450         return NULL_TREE;
7451       elements = tree_cons (NULL_TREE, elem, elements);
7452     }
7453   return build_vector (type, elements);
7454 }
7455
7456
7457 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7458    the buffer PTR of length LEN as a constant of type TYPE.  For
7459    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7460    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7461    return NULL_TREE.  */
7462
7463 tree
7464 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7465 {
7466   switch (TREE_CODE (type))
7467     {
7468     case INTEGER_TYPE:
7469     case ENUMERAL_TYPE:
7470     case BOOLEAN_TYPE:
7471       return native_interpret_int (type, ptr, len);
7472
7473     case REAL_TYPE:
7474       return native_interpret_real (type, ptr, len);
7475
7476     case COMPLEX_TYPE:
7477       return native_interpret_complex (type, ptr, len);
7478
7479     case VECTOR_TYPE:
7480       return native_interpret_vector (type, ptr, len);
7481
7482     default:
7483       return NULL_TREE;
7484     }
7485 }
7486
7487
7488 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7489    TYPE at compile-time.  If we're unable to perform the conversion
7490    return NULL_TREE.  */
7491
7492 static tree
7493 fold_view_convert_expr (tree type, tree expr)
7494 {
7495   /* We support up to 512-bit values (for V8DFmode).  */
7496   unsigned char buffer[64];
7497   int len;
7498
7499   /* Check that the host and target are sane.  */
7500   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7501     return NULL_TREE;
7502
7503   len = native_encode_expr (expr, buffer, sizeof (buffer));
7504   if (len == 0)
7505     return NULL_TREE;
7506
7507   return native_interpret_expr (type, buffer, len);
7508 }
7509
7510 /* Build an expression for the address of T.  Folds away INDIRECT_REF
7511    to avoid confusing the gimplify process.  */
7512
7513 tree
7514 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7515 {
7516   /* The size of the object is not relevant when talking about its address.  */
7517   if (TREE_CODE (t) == WITH_SIZE_EXPR)
7518     t = TREE_OPERAND (t, 0);
7519
7520   if (TREE_CODE (t) == INDIRECT_REF)
7521     {
7522       t = TREE_OPERAND (t, 0);
7523
7524       if (TREE_TYPE (t) != ptrtype)
7525         t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7526     }
7527   else if (TREE_CODE (t) == MEM_REF
7528       && integer_zerop (TREE_OPERAND (t, 1)))
7529     return TREE_OPERAND (t, 0);
7530   else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7531     {
7532       t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7533
7534       if (TREE_TYPE (t) != ptrtype)
7535         t = fold_convert_loc (loc, ptrtype, t);
7536     }
7537   else
7538     t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7539
7540   return t;
7541 }
7542
7543 /* Build an expression for the address of T.  */
7544
7545 tree
7546 build_fold_addr_expr_loc (location_t loc, tree t)
7547 {
7548   tree ptrtype = build_pointer_type (TREE_TYPE (t));
7549
7550   return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7551 }
7552
7553 /* Fold a unary expression of code CODE and type TYPE with operand
7554    OP0.  Return the folded expression if folding is successful.
7555    Otherwise, return NULL_TREE.  */
7556
7557 tree
7558 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7559 {
7560   tree tem;
7561   tree arg0;
7562   enum tree_code_class kind = TREE_CODE_CLASS (code);
7563
7564   gcc_assert (IS_EXPR_CODE_CLASS (kind)
7565               && TREE_CODE_LENGTH (code) == 1);
7566
7567   arg0 = op0;
7568   if (arg0)
7569     {
7570       if (CONVERT_EXPR_CODE_P (code)
7571           || code == FLOAT_EXPR || code == ABS_EXPR)
7572         {
7573           /* Don't use STRIP_NOPS, because signedness of argument type
7574              matters.  */
7575           STRIP_SIGN_NOPS (arg0);
7576         }
7577       else
7578         {
7579           /* Strip any conversions that don't change the mode.  This
7580              is safe for every expression, except for a comparison
7581              expression because its signedness is derived from its
7582              operands.
7583
7584              Note that this is done as an internal manipulation within
7585              the constant folder, in order to find the simplest
7586              representation of the arguments so that their form can be
7587              studied.  In any cases, the appropriate type conversions
7588              should be put back in the tree that will get out of the
7589              constant folder.  */
7590           STRIP_NOPS (arg0);
7591         }
7592     }
7593
7594   if (TREE_CODE_CLASS (code) == tcc_unary)
7595     {
7596       if (TREE_CODE (arg0) == COMPOUND_EXPR)
7597         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7598                        fold_build1_loc (loc, code, type,
7599                                     fold_convert_loc (loc, TREE_TYPE (op0),
7600                                                       TREE_OPERAND (arg0, 1))));
7601       else if (TREE_CODE (arg0) == COND_EXPR)
7602         {
7603           tree arg01 = TREE_OPERAND (arg0, 1);
7604           tree arg02 = TREE_OPERAND (arg0, 2);
7605           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7606             arg01 = fold_build1_loc (loc, code, type,
7607                                  fold_convert_loc (loc,
7608                                                    TREE_TYPE (op0), arg01));
7609           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7610             arg02 = fold_build1_loc (loc, code, type,
7611                                  fold_convert_loc (loc,
7612                                                    TREE_TYPE (op0), arg02));
7613           tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7614                              arg01, arg02);
7615
7616           /* If this was a conversion, and all we did was to move into
7617              inside the COND_EXPR, bring it back out.  But leave it if
7618              it is a conversion from integer to integer and the
7619              result precision is no wider than a word since such a
7620              conversion is cheap and may be optimized away by combine,
7621              while it couldn't if it were outside the COND_EXPR.  Then return
7622              so we don't get into an infinite recursion loop taking the
7623              conversion out and then back in.  */
7624
7625           if ((CONVERT_EXPR_CODE_P (code)
7626                || code == NON_LVALUE_EXPR)
7627               && TREE_CODE (tem) == COND_EXPR
7628               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7629               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7630               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7631               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7632               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7633                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7634               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7635                      && (INTEGRAL_TYPE_P
7636                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7637                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7638                   || flag_syntax_only))
7639             tem = build1_loc (loc, code, type,
7640                               build3 (COND_EXPR,
7641                                       TREE_TYPE (TREE_OPERAND
7642                                                  (TREE_OPERAND (tem, 1), 0)),
7643                                       TREE_OPERAND (tem, 0),
7644                                       TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7645                                       TREE_OPERAND (TREE_OPERAND (tem, 2),
7646                                                     0)));
7647           return tem;
7648         }
7649       else if (COMPARISON_CLASS_P (arg0))
7650         {
7651           if (TREE_CODE (type) == BOOLEAN_TYPE)
7652             {
7653               arg0 = copy_node (arg0);
7654               TREE_TYPE (arg0) = type;
7655               return arg0;
7656             }
7657           else if (TREE_CODE (type) != INTEGER_TYPE)
7658             return fold_build3_loc (loc, COND_EXPR, type, arg0,
7659                                 fold_build1_loc (loc, code, type,
7660                                              integer_one_node),
7661                                 fold_build1_loc (loc, code, type,
7662                                              integer_zero_node));
7663         }
7664    }
7665
7666   switch (code)
7667     {
7668     case PAREN_EXPR:
7669       /* Re-association barriers around constants and other re-association
7670          barriers can be removed.  */
7671       if (CONSTANT_CLASS_P (op0)
7672           || TREE_CODE (op0) == PAREN_EXPR)
7673         return fold_convert_loc (loc, type, op0);
7674       return NULL_TREE;
7675
7676     CASE_CONVERT:
7677     case FLOAT_EXPR:
7678     case FIX_TRUNC_EXPR:
7679       if (TREE_TYPE (op0) == type)
7680         return op0;
7681
7682       /* If we have (type) (a CMP b) and type is an integral type, return
7683          new expression involving the new type.  */
7684       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
7685         return fold_build2_loc (loc, TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
7686                             TREE_OPERAND (op0, 1));
7687
7688       /* Handle cases of two conversions in a row.  */
7689       if (CONVERT_EXPR_P (op0))
7690         {
7691           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7692           tree inter_type = TREE_TYPE (op0);
7693           int inside_int = INTEGRAL_TYPE_P (inside_type);
7694           int inside_ptr = POINTER_TYPE_P (inside_type);
7695           int inside_float = FLOAT_TYPE_P (inside_type);
7696           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7697           unsigned int inside_prec = TYPE_PRECISION (inside_type);
7698           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7699           int inter_int = INTEGRAL_TYPE_P (inter_type);
7700           int inter_ptr = POINTER_TYPE_P (inter_type);
7701           int inter_float = FLOAT_TYPE_P (inter_type);
7702           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7703           unsigned int inter_prec = TYPE_PRECISION (inter_type);
7704           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7705           int final_int = INTEGRAL_TYPE_P (type);
7706           int final_ptr = POINTER_TYPE_P (type);
7707           int final_float = FLOAT_TYPE_P (type);
7708           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7709           unsigned int final_prec = TYPE_PRECISION (type);
7710           int final_unsignedp = TYPE_UNSIGNED (type);
7711
7712           /* In addition to the cases of two conversions in a row
7713              handled below, if we are converting something to its own
7714              type via an object of identical or wider precision, neither
7715              conversion is needed.  */
7716           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7717               && (((inter_int || inter_ptr) && final_int)
7718                   || (inter_float && final_float))
7719               && inter_prec >= final_prec)
7720             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7721
7722           /* Likewise, if the intermediate and initial types are either both
7723              float or both integer, we don't need the middle conversion if the
7724              former is wider than the latter and doesn't change the signedness
7725              (for integers).  Avoid this if the final type is a pointer since
7726              then we sometimes need the middle conversion.  Likewise if the
7727              final type has a precision not equal to the size of its mode.  */
7728           if (((inter_int && inside_int)
7729                || (inter_float && inside_float)
7730                || (inter_vec && inside_vec))
7731               && inter_prec >= inside_prec
7732               && (inter_float || inter_vec
7733                   || inter_unsignedp == inside_unsignedp)
7734               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7735                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
7736               && ! final_ptr
7737               && (! final_vec || inter_prec == inside_prec))
7738             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7739
7740           /* If we have a sign-extension of a zero-extended value, we can
7741              replace that by a single zero-extension.  */
7742           if (inside_int && inter_int && final_int
7743               && inside_prec < inter_prec && inter_prec < final_prec
7744               && inside_unsignedp && !inter_unsignedp)
7745             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7746
7747           /* Two conversions in a row are not needed unless:
7748              - some conversion is floating-point (overstrict for now), or
7749              - some conversion is a vector (overstrict for now), or
7750              - the intermediate type is narrower than both initial and
7751                final, or
7752              - the intermediate type and innermost type differ in signedness,
7753                and the outermost type is wider than the intermediate, or
7754              - the initial type is a pointer type and the precisions of the
7755                intermediate and final types differ, or
7756              - the final type is a pointer type and the precisions of the
7757                initial and intermediate types differ.  */
7758           if (! inside_float && ! inter_float && ! final_float
7759               && ! inside_vec && ! inter_vec && ! final_vec
7760               && (inter_prec >= inside_prec || inter_prec >= final_prec)
7761               && ! (inside_int && inter_int
7762                     && inter_unsignedp != inside_unsignedp
7763                     && inter_prec < final_prec)
7764               && ((inter_unsignedp && inter_prec > inside_prec)
7765                   == (final_unsignedp && final_prec > inter_prec))
7766               && ! (inside_ptr && inter_prec != final_prec)
7767               && ! (final_ptr && inside_prec != inter_prec)
7768               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7769                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7770             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7771         }
7772
7773       /* Handle (T *)&A.B.C for A being of type T and B and C
7774          living at offset zero.  This occurs frequently in
7775          C++ upcasting and then accessing the base.  */
7776       if (TREE_CODE (op0) == ADDR_EXPR
7777           && POINTER_TYPE_P (type)
7778           && handled_component_p (TREE_OPERAND (op0, 0)))
7779         {
7780           HOST_WIDE_INT bitsize, bitpos;
7781           tree offset;
7782           enum machine_mode mode;
7783           int unsignedp, volatilep;
7784           tree base = TREE_OPERAND (op0, 0);
7785           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7786                                       &mode, &unsignedp, &volatilep, false);
7787           /* If the reference was to a (constant) zero offset, we can use
7788              the address of the base if it has the same base type
7789              as the result type and the pointer type is unqualified.  */
7790           if (! offset && bitpos == 0
7791               && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7792                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7793               && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7794             return fold_convert_loc (loc, type,
7795                                      build_fold_addr_expr_loc (loc, base));
7796         }
7797
7798       if (TREE_CODE (op0) == MODIFY_EXPR
7799           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7800           /* Detect assigning a bitfield.  */
7801           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7802                && DECL_BIT_FIELD
7803                (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7804         {
7805           /* Don't leave an assignment inside a conversion
7806              unless assigning a bitfield.  */
7807           tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7808           /* First do the assignment, then return converted constant.  */
7809           tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7810           TREE_NO_WARNING (tem) = 1;
7811           TREE_USED (tem) = 1;
7812           return tem;
7813         }
7814
7815       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7816          constants (if x has signed type, the sign bit cannot be set
7817          in c).  This folds extension into the BIT_AND_EXPR.
7818          ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7819          very likely don't have maximal range for their precision and this
7820          transformation effectively doesn't preserve non-maximal ranges.  */
7821       if (TREE_CODE (type) == INTEGER_TYPE
7822           && TREE_CODE (op0) == BIT_AND_EXPR
7823           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7824         {
7825           tree and_expr = op0;
7826           tree and0 = TREE_OPERAND (and_expr, 0);
7827           tree and1 = TREE_OPERAND (and_expr, 1);
7828           int change = 0;
7829
7830           if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7831               || (TYPE_PRECISION (type)
7832                   <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7833             change = 1;
7834           else if (TYPE_PRECISION (TREE_TYPE (and1))
7835                    <= HOST_BITS_PER_WIDE_INT
7836                    && host_integerp (and1, 1))
7837             {
7838               unsigned HOST_WIDE_INT cst;
7839
7840               cst = tree_low_cst (and1, 1);
7841               cst &= (HOST_WIDE_INT) -1
7842                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7843               change = (cst == 0);
7844 #ifdef LOAD_EXTEND_OP
7845               if (change
7846                   && !flag_syntax_only
7847                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7848                       == ZERO_EXTEND))
7849                 {
7850                   tree uns = unsigned_type_for (TREE_TYPE (and0));
7851                   and0 = fold_convert_loc (loc, uns, and0);
7852                   and1 = fold_convert_loc (loc, uns, and1);
7853                 }
7854 #endif
7855             }
7856           if (change)
7857             {
7858               tem = force_fit_type_double (type, tree_to_double_int (and1),
7859                                            0, TREE_OVERFLOW (and1));
7860               return fold_build2_loc (loc, BIT_AND_EXPR, type,
7861                                   fold_convert_loc (loc, type, and0), tem);
7862             }
7863         }
7864
7865       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7866          when one of the new casts will fold away. Conservatively we assume
7867          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7868       if (POINTER_TYPE_P (type)
7869           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7870           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7871               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7872               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7873         {
7874           tree arg00 = TREE_OPERAND (arg0, 0);
7875           tree arg01 = TREE_OPERAND (arg0, 1);
7876
7877           return fold_build2_loc (loc,
7878                               TREE_CODE (arg0), type,
7879                               fold_convert_loc (loc, type, arg00),
7880                               fold_convert_loc (loc, sizetype, arg01));
7881         }
7882
7883       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7884          of the same precision, and X is an integer type not narrower than
7885          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
7886       if (INTEGRAL_TYPE_P (type)
7887           && TREE_CODE (op0) == BIT_NOT_EXPR
7888           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7889           && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7890           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7891         {
7892           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7893           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7894               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7895             return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7896                                 fold_convert_loc (loc, type, tem));
7897         }
7898
7899       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7900          type of X and Y (integer types only).  */
7901       if (INTEGRAL_TYPE_P (type)
7902           && TREE_CODE (op0) == MULT_EXPR
7903           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7904           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7905         {
7906           /* Be careful not to introduce new overflows.  */
7907           tree mult_type;
7908           if (TYPE_OVERFLOW_WRAPS (type))
7909             mult_type = type;
7910           else
7911             mult_type = unsigned_type_for (type);
7912
7913           if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7914             {
7915               tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7916                                  fold_convert_loc (loc, mult_type,
7917                                                    TREE_OPERAND (op0, 0)),
7918                                  fold_convert_loc (loc, mult_type,
7919                                                    TREE_OPERAND (op0, 1)));
7920               return fold_convert_loc (loc, type, tem);
7921             }
7922         }
7923
7924       tem = fold_convert_const (code, type, op0);
7925       return tem ? tem : NULL_TREE;
7926
7927     case ADDR_SPACE_CONVERT_EXPR:
7928       if (integer_zerop (arg0))
7929         return fold_convert_const (code, type, arg0);
7930       return NULL_TREE;
7931
7932     case FIXED_CONVERT_EXPR:
7933       tem = fold_convert_const (code, type, arg0);
7934       return tem ? tem : NULL_TREE;
7935
7936     case VIEW_CONVERT_EXPR:
7937       if (TREE_TYPE (op0) == type)
7938         return op0;
7939       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
7940         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
7941                             type, TREE_OPERAND (op0, 0));
7942       if (TREE_CODE (op0) == MEM_REF)
7943         return fold_build2_loc (loc, MEM_REF, type,
7944                                 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7945
7946       /* For integral conversions with the same precision or pointer
7947          conversions use a NOP_EXPR instead.  */
7948       if ((INTEGRAL_TYPE_P (type)
7949            || POINTER_TYPE_P (type))
7950           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7951               || POINTER_TYPE_P (TREE_TYPE (op0)))
7952           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7953         return fold_convert_loc (loc, type, op0);
7954
7955       /* Strip inner integral conversions that do not change the precision.  */
7956       if (CONVERT_EXPR_P (op0)
7957           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7958               || POINTER_TYPE_P (TREE_TYPE (op0)))
7959           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
7960               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
7961           && (TYPE_PRECISION (TREE_TYPE (op0))
7962               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
7963         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
7964                             type, TREE_OPERAND (op0, 0));
7965
7966       return fold_view_convert_expr (type, op0);
7967
7968     case NEGATE_EXPR:
7969       tem = fold_negate_expr (loc, arg0);
7970       if (tem)
7971         return fold_convert_loc (loc, type, tem);
7972       return NULL_TREE;
7973
7974     case ABS_EXPR:
7975       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
7976         return fold_abs_const (arg0, type);
7977       else if (TREE_CODE (arg0) == NEGATE_EXPR)
7978         return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
7979       /* Convert fabs((double)float) into (double)fabsf(float).  */
7980       else if (TREE_CODE (arg0) == NOP_EXPR
7981                && TREE_CODE (type) == REAL_TYPE)
7982         {
7983           tree targ0 = strip_float_extensions (arg0);
7984           if (targ0 != arg0)
7985             return fold_convert_loc (loc, type,
7986                                      fold_build1_loc (loc, ABS_EXPR,
7987                                                   TREE_TYPE (targ0),
7988                                                   targ0));
7989         }
7990       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
7991       else if (TREE_CODE (arg0) == ABS_EXPR)
7992         return arg0;
7993       else if (tree_expr_nonnegative_p (arg0))
7994         return arg0;
7995
7996       /* Strip sign ops from argument.  */
7997       if (TREE_CODE (type) == REAL_TYPE)
7998         {
7999           tem = fold_strip_sign_ops (arg0);
8000           if (tem)
8001             return fold_build1_loc (loc, ABS_EXPR, type,
8002                                 fold_convert_loc (loc, type, tem));
8003         }
8004       return NULL_TREE;
8005
8006     case CONJ_EXPR:
8007       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8008         return fold_convert_loc (loc, type, arg0);
8009       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8010         {
8011           tree itype = TREE_TYPE (type);
8012           tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8013           tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8014           return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8015                               negate_expr (ipart));
8016         }
8017       if (TREE_CODE (arg0) == COMPLEX_CST)
8018         {
8019           tree itype = TREE_TYPE (type);
8020           tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8021           tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8022           return build_complex (type, rpart, negate_expr (ipart));
8023         }
8024       if (TREE_CODE (arg0) == CONJ_EXPR)
8025         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8026       return NULL_TREE;
8027
8028     case BIT_NOT_EXPR:
8029       if (TREE_CODE (arg0) == INTEGER_CST)
8030         return fold_not_const (arg0, type);
8031       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8032         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8033       /* Convert ~ (-A) to A - 1.  */
8034       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8035         return fold_build2_loc (loc, MINUS_EXPR, type,
8036                             fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8037                             build_int_cst (type, 1));
8038       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8039       else if (INTEGRAL_TYPE_P (type)
8040                && ((TREE_CODE (arg0) == MINUS_EXPR
8041                     && integer_onep (TREE_OPERAND (arg0, 1)))
8042                    || (TREE_CODE (arg0) == PLUS_EXPR
8043                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8044         return fold_build1_loc (loc, NEGATE_EXPR, type,
8045                             fold_convert_loc (loc, type,
8046                                               TREE_OPERAND (arg0, 0)));
8047       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8048       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8049                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8050                                      fold_convert_loc (loc, type,
8051                                                        TREE_OPERAND (arg0, 0)))))
8052         return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8053                             fold_convert_loc (loc, type,
8054                                               TREE_OPERAND (arg0, 1)));
8055       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8056                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8057                                      fold_convert_loc (loc, type,
8058                                                        TREE_OPERAND (arg0, 1)))))
8059         return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8060                             fold_convert_loc (loc, type,
8061                                               TREE_OPERAND (arg0, 0)), tem);
8062       /* Perform BIT_NOT_EXPR on each element individually.  */
8063       else if (TREE_CODE (arg0) == VECTOR_CST)
8064         {
8065           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8066           int count = TYPE_VECTOR_SUBPARTS (type), i;
8067
8068           for (i = 0; i < count; i++)
8069             {
8070               if (elements)
8071                 {
8072                   elem = TREE_VALUE (elements);
8073                   elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8074                   if (elem == NULL_TREE)
8075                     break;
8076                   elements = TREE_CHAIN (elements);
8077                 }
8078               else
8079                 elem = build_int_cst (TREE_TYPE (type), -1);
8080               list = tree_cons (NULL_TREE, elem, list);
8081             }
8082           if (i == count)
8083             return build_vector (type, nreverse (list));
8084         }
8085
8086       return NULL_TREE;
8087
8088     case TRUTH_NOT_EXPR:
8089       /* The argument to invert_truthvalue must have Boolean type.  */
8090       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8091           arg0 = fold_convert_loc (loc, boolean_type_node, arg0);
8092
8093       /* Note that the operand of this must be an int
8094          and its values must be 0 or 1.
8095          ("true" is a fixed value perhaps depending on the language,
8096          but we don't handle values other than 1 correctly yet.)  */
8097       tem = fold_truth_not_expr (loc, arg0);
8098       if (!tem)
8099         return NULL_TREE;
8100       return fold_convert_loc (loc, type, tem);
8101
8102     case REALPART_EXPR:
8103       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8104         return fold_convert_loc (loc, type, arg0);
8105       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8106         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8107                                  TREE_OPERAND (arg0, 1));
8108       if (TREE_CODE (arg0) == COMPLEX_CST)
8109         return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8110       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8111         {
8112           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8113           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8114                              fold_build1_loc (loc, REALPART_EXPR, itype,
8115                                           TREE_OPERAND (arg0, 0)),
8116                              fold_build1_loc (loc, REALPART_EXPR, itype,
8117                                           TREE_OPERAND (arg0, 1)));
8118           return fold_convert_loc (loc, type, tem);
8119         }
8120       if (TREE_CODE (arg0) == CONJ_EXPR)
8121         {
8122           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8123           tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8124                              TREE_OPERAND (arg0, 0));
8125           return fold_convert_loc (loc, type, tem);
8126         }
8127       if (TREE_CODE (arg0) == CALL_EXPR)
8128         {
8129           tree fn = get_callee_fndecl (arg0);
8130           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8131             switch (DECL_FUNCTION_CODE (fn))
8132               {
8133               CASE_FLT_FN (BUILT_IN_CEXPI):
8134                 fn = mathfn_built_in (type, BUILT_IN_COS);
8135                 if (fn)
8136                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8137                 break;
8138
8139               default:
8140                 break;
8141               }
8142         }
8143       return NULL_TREE;
8144
8145     case IMAGPART_EXPR:
8146       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8147         return build_zero_cst (type);
8148       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8149         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8150                                  TREE_OPERAND (arg0, 0));
8151       if (TREE_CODE (arg0) == COMPLEX_CST)
8152         return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8153       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8154         {
8155           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8156           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8157                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8158                                           TREE_OPERAND (arg0, 0)),
8159                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8160                                           TREE_OPERAND (arg0, 1)));
8161           return fold_convert_loc (loc, type, tem);
8162         }
8163       if (TREE_CODE (arg0) == CONJ_EXPR)
8164         {
8165           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8166           tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8167           return fold_convert_loc (loc, type, negate_expr (tem));
8168         }
8169       if (TREE_CODE (arg0) == CALL_EXPR)
8170         {
8171           tree fn = get_callee_fndecl (arg0);
8172           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8173             switch (DECL_FUNCTION_CODE (fn))
8174               {
8175               CASE_FLT_FN (BUILT_IN_CEXPI):
8176                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8177                 if (fn)
8178                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8179                 break;
8180
8181               default:
8182                 break;
8183               }
8184         }
8185       return NULL_TREE;
8186
8187     case INDIRECT_REF:
8188       /* Fold *&X to X if X is an lvalue.  */
8189       if (TREE_CODE (op0) == ADDR_EXPR)
8190         {
8191           tree op00 = TREE_OPERAND (op0, 0);
8192           if ((TREE_CODE (op00) == VAR_DECL
8193                || TREE_CODE (op00) == PARM_DECL
8194                || TREE_CODE (op00) == RESULT_DECL)
8195               && !TREE_READONLY (op00))
8196             return op00;
8197         }
8198       return NULL_TREE;
8199
8200     default:
8201       return NULL_TREE;
8202     } /* switch (code) */
8203 }
8204
8205
8206 /* If the operation was a conversion do _not_ mark a resulting constant
8207    with TREE_OVERFLOW if the original constant was not.  These conversions
8208    have implementation defined behavior and retaining the TREE_OVERFLOW
8209    flag here would confuse later passes such as VRP.  */
8210 tree
8211 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8212                                 tree type, tree op0)
8213 {
8214   tree res = fold_unary_loc (loc, code, type, op0);
8215   if (res
8216       && TREE_CODE (res) == INTEGER_CST
8217       && TREE_CODE (op0) == INTEGER_CST
8218       && CONVERT_EXPR_CODE_P (code))
8219     TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8220
8221   return res;
8222 }
8223
8224 /* Fold a binary expression of code CODE and type TYPE with operands
8225    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8226    Return the folded expression if folding is successful.  Otherwise,
8227    return NULL_TREE.  */
8228
8229 static tree
8230 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8231 {
8232   enum tree_code compl_code;
8233
8234   if (code == MIN_EXPR)
8235     compl_code = MAX_EXPR;
8236   else if (code == MAX_EXPR)
8237     compl_code = MIN_EXPR;
8238   else
8239     gcc_unreachable ();
8240
8241   /* MIN (MAX (a, b), b) == b.  */
8242   if (TREE_CODE (op0) == compl_code
8243       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8244     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8245
8246   /* MIN (MAX (b, a), b) == b.  */
8247   if (TREE_CODE (op0) == compl_code
8248       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8249       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8250     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8251
8252   /* MIN (a, MAX (a, b)) == a.  */
8253   if (TREE_CODE (op1) == compl_code
8254       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8255       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8256     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8257
8258   /* MIN (a, MAX (b, a)) == a.  */
8259   if (TREE_CODE (op1) == compl_code
8260       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8261       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8262     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8263
8264   return NULL_TREE;
8265 }
8266
8267 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8268    by changing CODE to reduce the magnitude of constants involved in
8269    ARG0 of the comparison.
8270    Returns a canonicalized comparison tree if a simplification was
8271    possible, otherwise returns NULL_TREE.
8272    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8273    valid if signed overflow is undefined.  */
8274
8275 static tree
8276 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8277                                  tree arg0, tree arg1,
8278                                  bool *strict_overflow_p)
8279 {
8280   enum tree_code code0 = TREE_CODE (arg0);
8281   tree t, cst0 = NULL_TREE;
8282   int sgn0;
8283   bool swap = false;
8284
8285   /* Match A +- CST code arg1 and CST code arg1.  We can change the
8286      first form only if overflow is undefined.  */
8287   if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8288          /* In principle pointers also have undefined overflow behavior,
8289             but that causes problems elsewhere.  */
8290          && !POINTER_TYPE_P (TREE_TYPE (arg0))
8291          && (code0 == MINUS_EXPR
8292              || code0 == PLUS_EXPR)
8293          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8294         || code0 == INTEGER_CST))
8295     return NULL_TREE;
8296
8297   /* Identify the constant in arg0 and its sign.  */
8298   if (code0 == INTEGER_CST)
8299     cst0 = arg0;
8300   else
8301     cst0 = TREE_OPERAND (arg0, 1);
8302   sgn0 = tree_int_cst_sgn (cst0);
8303
8304   /* Overflowed constants and zero will cause problems.  */
8305   if (integer_zerop (cst0)
8306       || TREE_OVERFLOW (cst0))
8307     return NULL_TREE;
8308
8309   /* See if we can reduce the magnitude of the constant in
8310      arg0 by changing the comparison code.  */
8311   if (code0 == INTEGER_CST)
8312     {
8313       /* CST <= arg1  ->  CST-1 < arg1.  */
8314       if (code == LE_EXPR && sgn0 == 1)
8315         code = LT_EXPR;
8316       /* -CST < arg1  ->  -CST-1 <= arg1.  */
8317       else if (code == LT_EXPR && sgn0 == -1)
8318         code = LE_EXPR;
8319       /* CST > arg1  ->  CST-1 >= arg1.  */
8320       else if (code == GT_EXPR && sgn0 == 1)
8321         code = GE_EXPR;
8322       /* -CST >= arg1  ->  -CST-1 > arg1.  */
8323       else if (code == GE_EXPR && sgn0 == -1)
8324         code = GT_EXPR;
8325       else
8326         return NULL_TREE;
8327       /* arg1 code' CST' might be more canonical.  */
8328       swap = true;
8329     }
8330   else
8331     {
8332       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
8333       if (code == LT_EXPR
8334           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8335         code = LE_EXPR;
8336       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
8337       else if (code == GT_EXPR
8338                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8339         code = GE_EXPR;
8340       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
8341       else if (code == LE_EXPR
8342                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8343         code = LT_EXPR;
8344       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
8345       else if (code == GE_EXPR
8346                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8347         code = GT_EXPR;
8348       else
8349         return NULL_TREE;
8350       *strict_overflow_p = true;
8351     }
8352
8353   /* Now build the constant reduced in magnitude.  But not if that
8354      would produce one outside of its types range.  */
8355   if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8356       && ((sgn0 == 1
8357            && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8358            && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8359           || (sgn0 == -1
8360               && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8361               && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8362     /* We cannot swap the comparison here as that would cause us to
8363        endlessly recurse.  */
8364     return NULL_TREE;
8365
8366   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8367                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8368   if (code0 != INTEGER_CST)
8369     t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8370
8371   /* If swapping might yield to a more canonical form, do so.  */
8372   if (swap)
8373     return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8374   else
8375     return fold_build2_loc (loc, code, type, t, arg1);
8376 }
8377
8378 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8379    overflow further.  Try to decrease the magnitude of constants involved
8380    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8381    and put sole constants at the second argument position.
8382    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
8383
8384 static tree
8385 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8386                                tree arg0, tree arg1)
8387 {
8388   tree t;
8389   bool strict_overflow_p;
8390   const char * const warnmsg = G_("assuming signed overflow does not occur "
8391                                   "when reducing constant in comparison");
8392
8393   /* Try canonicalization by simplifying arg0.  */
8394   strict_overflow_p = false;
8395   t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8396                                        &strict_overflow_p);
8397   if (t)
8398     {
8399       if (strict_overflow_p)
8400         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8401       return t;
8402     }
8403
8404   /* Try canonicalization by simplifying arg1 using the swapped
8405      comparison.  */
8406   code = swap_tree_comparison (code);
8407   strict_overflow_p = false;
8408   t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8409                                        &strict_overflow_p);
8410   if (t && strict_overflow_p)
8411     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8412   return t;
8413 }
8414
8415 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8416    space.  This is used to avoid issuing overflow warnings for
8417    expressions like &p->x which can not wrap.  */
8418
8419 static bool
8420 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8421 {
8422   unsigned HOST_WIDE_INT offset_low, total_low;
8423   HOST_WIDE_INT size, offset_high, total_high;
8424
8425   if (!POINTER_TYPE_P (TREE_TYPE (base)))
8426     return true;
8427
8428   if (bitpos < 0)
8429     return true;
8430
8431   if (offset == NULL_TREE)
8432     {
8433       offset_low = 0;
8434       offset_high = 0;
8435     }
8436   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8437     return true;
8438   else
8439     {
8440       offset_low = TREE_INT_CST_LOW (offset);
8441       offset_high = TREE_INT_CST_HIGH (offset);
8442     }
8443
8444   if (add_double_with_sign (offset_low, offset_high,
8445                             bitpos / BITS_PER_UNIT, 0,
8446                             &total_low, &total_high,
8447                             true))
8448     return true;
8449
8450   if (total_high != 0)
8451     return true;
8452
8453   size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8454   if (size <= 0)
8455     return true;
8456
8457   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8458      array.  */
8459   if (TREE_CODE (base) == ADDR_EXPR)
8460     {
8461       HOST_WIDE_INT base_size;
8462
8463       base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8464       if (base_size > 0 && size < base_size)
8465         size = base_size;
8466     }
8467
8468   return total_low > (unsigned HOST_WIDE_INT) size;
8469 }
8470
8471 /* Subroutine of fold_binary.  This routine performs all of the
8472    transformations that are common to the equality/inequality
8473    operators (EQ_EXPR and NE_EXPR) and the ordering operators
8474    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
8475    fold_binary should call fold_binary.  Fold a comparison with
8476    tree code CODE and type TYPE with operands OP0 and OP1.  Return
8477    the folded comparison or NULL_TREE.  */
8478
8479 static tree
8480 fold_comparison (location_t loc, enum tree_code code, tree type,
8481                  tree op0, tree op1)
8482 {
8483   tree arg0, arg1, tem;
8484
8485   arg0 = op0;
8486   arg1 = op1;
8487
8488   STRIP_SIGN_NOPS (arg0);
8489   STRIP_SIGN_NOPS (arg1);
8490
8491   tem = fold_relational_const (code, type, arg0, arg1);
8492   if (tem != NULL_TREE)
8493     return tem;
8494
8495   /* If one arg is a real or integer constant, put it last.  */
8496   if (tree_swap_operands_p (arg0, arg1, true))
8497     return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
8498
8499   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
8500   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8501       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8502           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8503           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8504       && (TREE_CODE (arg1) == INTEGER_CST
8505           && !TREE_OVERFLOW (arg1)))
8506     {
8507       tree const1 = TREE_OPERAND (arg0, 1);
8508       tree const2 = arg1;
8509       tree variable = TREE_OPERAND (arg0, 0);
8510       tree lhs;
8511       int lhs_add;
8512       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8513
8514       lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
8515                          TREE_TYPE (arg1), const2, const1);
8516
8517       /* If the constant operation overflowed this can be
8518          simplified as a comparison against INT_MAX/INT_MIN.  */
8519       if (TREE_CODE (lhs) == INTEGER_CST
8520           && TREE_OVERFLOW (lhs))
8521         {
8522           int const1_sgn = tree_int_cst_sgn (const1);
8523           enum tree_code code2 = code;
8524
8525           /* Get the sign of the constant on the lhs if the
8526              operation were VARIABLE + CONST1.  */
8527           if (TREE_CODE (arg0) == MINUS_EXPR)
8528             const1_sgn = -const1_sgn;
8529
8530           /* The sign of the constant determines if we overflowed
8531              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8532              Canonicalize to the INT_MIN overflow by swapping the comparison
8533              if necessary.  */
8534           if (const1_sgn == -1)
8535             code2 = swap_tree_comparison (code);
8536
8537           /* We now can look at the canonicalized case
8538                VARIABLE + 1  CODE2  INT_MIN
8539              and decide on the result.  */
8540           if (code2 == LT_EXPR
8541               || code2 == LE_EXPR
8542               || code2 == EQ_EXPR)
8543             return omit_one_operand_loc (loc, type, boolean_false_node, variable);
8544           else if (code2 == NE_EXPR
8545                    || code2 == GE_EXPR
8546                    || code2 == GT_EXPR)
8547             return omit_one_operand_loc (loc, type, boolean_true_node, variable);
8548         }
8549
8550       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8551           && (TREE_CODE (lhs) != INTEGER_CST
8552               || !TREE_OVERFLOW (lhs)))
8553         {
8554           if (code != EQ_EXPR && code != NE_EXPR)
8555             fold_overflow_warning ("assuming signed overflow does not occur "
8556                                    "when changing X +- C1 cmp C2 to "
8557                                    "X cmp C1 +- C2",
8558                                    WARN_STRICT_OVERFLOW_COMPARISON);
8559           return fold_build2_loc (loc, code, type, variable, lhs);
8560         }
8561     }
8562
8563   /* For comparisons of pointers we can decompose it to a compile time
8564      comparison of the base objects and the offsets into the object.
8565      This requires at least one operand being an ADDR_EXPR or a
8566      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
8567   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8568       && (TREE_CODE (arg0) == ADDR_EXPR
8569           || TREE_CODE (arg1) == ADDR_EXPR
8570           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8571           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8572     {
8573       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8574       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8575       enum machine_mode mode;
8576       int volatilep, unsignedp;
8577       bool indirect_base0 = false, indirect_base1 = false;
8578
8579       /* Get base and offset for the access.  Strip ADDR_EXPR for
8580          get_inner_reference, but put it back by stripping INDIRECT_REF
8581          off the base object if possible.  indirect_baseN will be true
8582          if baseN is not an address but refers to the object itself.  */
8583       base0 = arg0;
8584       if (TREE_CODE (arg0) == ADDR_EXPR)
8585         {
8586           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8587                                        &bitsize, &bitpos0, &offset0, &mode,
8588                                        &unsignedp, &volatilep, false);
8589           if (TREE_CODE (base0) == INDIRECT_REF)
8590             base0 = TREE_OPERAND (base0, 0);
8591           else
8592             indirect_base0 = true;
8593         }
8594       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8595         {
8596           base0 = TREE_OPERAND (arg0, 0);
8597           STRIP_SIGN_NOPS (base0);
8598           if (TREE_CODE (base0) == ADDR_EXPR)
8599             {
8600               base0 = TREE_OPERAND (base0, 0);
8601               indirect_base0 = true;
8602             }
8603           offset0 = TREE_OPERAND (arg0, 1);
8604         }
8605
8606       base1 = arg1;
8607       if (TREE_CODE (arg1) == ADDR_EXPR)
8608         {
8609           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8610                                        &bitsize, &bitpos1, &offset1, &mode,
8611                                        &unsignedp, &volatilep, false);
8612           if (TREE_CODE (base1) == INDIRECT_REF)
8613             base1 = TREE_OPERAND (base1, 0);
8614           else
8615             indirect_base1 = true;
8616         }
8617       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8618         {
8619           base1 = TREE_OPERAND (arg1, 0);
8620           STRIP_SIGN_NOPS (base1);
8621           if (TREE_CODE (base1) == ADDR_EXPR)
8622             {
8623               base1 = TREE_OPERAND (base1, 0);
8624               indirect_base1 = true;
8625             }
8626           offset1 = TREE_OPERAND (arg1, 1);
8627         }
8628
8629       /* A local variable can never be pointed to by
8630          the default SSA name of an incoming parameter.  */
8631       if ((TREE_CODE (arg0) == ADDR_EXPR
8632            && indirect_base0
8633            && TREE_CODE (base0) == VAR_DECL
8634            && auto_var_in_fn_p (base0, current_function_decl)
8635            && !indirect_base1
8636            && TREE_CODE (base1) == SSA_NAME
8637            && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL
8638            && SSA_NAME_IS_DEFAULT_DEF (base1))
8639           || (TREE_CODE (arg1) == ADDR_EXPR
8640               && indirect_base1
8641               && TREE_CODE (base1) == VAR_DECL
8642               && auto_var_in_fn_p (base1, current_function_decl)
8643               && !indirect_base0
8644               && TREE_CODE (base0) == SSA_NAME
8645               && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL
8646               && SSA_NAME_IS_DEFAULT_DEF (base0)))
8647         {
8648           if (code == NE_EXPR)
8649             return constant_boolean_node (1, type);
8650           else if (code == EQ_EXPR)
8651             return constant_boolean_node (0, type);
8652         }
8653       /* If we have equivalent bases we might be able to simplify.  */
8654       else if (indirect_base0 == indirect_base1
8655                && operand_equal_p (base0, base1, 0))
8656         {
8657           /* We can fold this expression to a constant if the non-constant
8658              offset parts are equal.  */
8659           if ((offset0 == offset1
8660                || (offset0 && offset1
8661                    && operand_equal_p (offset0, offset1, 0)))
8662               && (code == EQ_EXPR
8663                   || code == NE_EXPR
8664                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
8665
8666             {
8667               if (code != EQ_EXPR
8668                   && code != NE_EXPR
8669                   && bitpos0 != bitpos1
8670                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
8671                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
8672                 fold_overflow_warning (("assuming pointer wraparound does not "
8673                                         "occur when comparing P +- C1 with "
8674                                         "P +- C2"),
8675                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
8676
8677               switch (code)
8678                 {
8679                 case EQ_EXPR:
8680                   return constant_boolean_node (bitpos0 == bitpos1, type);
8681                 case NE_EXPR:
8682                   return constant_boolean_node (bitpos0 != bitpos1, type);
8683                 case LT_EXPR:
8684                   return constant_boolean_node (bitpos0 < bitpos1, type);
8685                 case LE_EXPR:
8686                   return constant_boolean_node (bitpos0 <= bitpos1, type);
8687                 case GE_EXPR:
8688                   return constant_boolean_node (bitpos0 >= bitpos1, type);
8689                 case GT_EXPR:
8690                   return constant_boolean_node (bitpos0 > bitpos1, type);
8691                 default:;
8692                 }
8693             }
8694           /* We can simplify the comparison to a comparison of the variable
8695              offset parts if the constant offset parts are equal.
8696              Be careful to use signed size type here because otherwise we
8697              mess with array offsets in the wrong way.  This is possible
8698              because pointer arithmetic is restricted to retain within an
8699              object and overflow on pointer differences is undefined as of
8700              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
8701           else if (bitpos0 == bitpos1
8702                    && ((code == EQ_EXPR || code == NE_EXPR)
8703                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
8704             {
8705               /* By converting to signed size type we cover middle-end pointer
8706                  arithmetic which operates on unsigned pointer types of size
8707                  type size and ARRAY_REF offsets which are properly sign or
8708                  zero extended from their type in case it is narrower than
8709                  size type.  */
8710               if (offset0 == NULL_TREE)
8711                 offset0 = build_int_cst (ssizetype, 0);
8712               else
8713                 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8714               if (offset1 == NULL_TREE)
8715                 offset1 = build_int_cst (ssizetype, 0);
8716               else
8717                 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8718
8719               if (code != EQ_EXPR
8720                   && code != NE_EXPR
8721                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
8722                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
8723                 fold_overflow_warning (("assuming pointer wraparound does not "
8724                                         "occur when comparing P +- C1 with "
8725                                         "P +- C2"),
8726                                        WARN_STRICT_OVERFLOW_COMPARISON);
8727
8728               return fold_build2_loc (loc, code, type, offset0, offset1);
8729             }
8730         }
8731       /* For non-equal bases we can simplify if they are addresses
8732          of local binding decls or constants.  */
8733       else if (indirect_base0 && indirect_base1
8734                /* We know that !operand_equal_p (base0, base1, 0)
8735                   because the if condition was false.  But make
8736                   sure two decls are not the same.  */
8737                && base0 != base1
8738                && TREE_CODE (arg0) == ADDR_EXPR
8739                && TREE_CODE (arg1) == ADDR_EXPR
8740                && (((TREE_CODE (base0) == VAR_DECL
8741                      || TREE_CODE (base0) == PARM_DECL)
8742                     && (targetm.binds_local_p (base0)
8743                         || CONSTANT_CLASS_P (base1)))
8744                    || CONSTANT_CLASS_P (base0))
8745                && (((TREE_CODE (base1) == VAR_DECL
8746                      || TREE_CODE (base1) == PARM_DECL)
8747                     && (targetm.binds_local_p (base1)
8748                         || CONSTANT_CLASS_P (base0)))
8749                    || CONSTANT_CLASS_P (base1)))
8750         {
8751           if (code == EQ_EXPR)
8752             return omit_two_operands_loc (loc, type, boolean_false_node,
8753                                       arg0, arg1);
8754           else if (code == NE_EXPR)
8755             return omit_two_operands_loc (loc, type, boolean_true_node,
8756                                       arg0, arg1);
8757         }
8758       /* For equal offsets we can simplify to a comparison of the
8759          base addresses.  */
8760       else if (bitpos0 == bitpos1
8761                && (indirect_base0
8762                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8763                && (indirect_base1
8764                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8765                && ((offset0 == offset1)
8766                    || (offset0 && offset1
8767                        && operand_equal_p (offset0, offset1, 0))))
8768         {
8769           if (indirect_base0)
8770             base0 = build_fold_addr_expr_loc (loc, base0);
8771           if (indirect_base1)
8772             base1 = build_fold_addr_expr_loc (loc, base1);
8773           return fold_build2_loc (loc, code, type, base0, base1);
8774         }
8775     }
8776
8777   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8778      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
8779      the resulting offset is smaller in absolute value than the
8780      original one.  */
8781   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8782       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8783       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8784           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8785       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8786       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8787           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8788     {
8789       tree const1 = TREE_OPERAND (arg0, 1);
8790       tree const2 = TREE_OPERAND (arg1, 1);
8791       tree variable1 = TREE_OPERAND (arg0, 0);
8792       tree variable2 = TREE_OPERAND (arg1, 0);
8793       tree cst;
8794       const char * const warnmsg = G_("assuming signed overflow does not "
8795                                       "occur when combining constants around "
8796                                       "a comparison");
8797
8798       /* Put the constant on the side where it doesn't overflow and is
8799          of lower absolute value than before.  */
8800       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8801                              ? MINUS_EXPR : PLUS_EXPR,
8802                              const2, const1, 0);
8803       if (!TREE_OVERFLOW (cst)
8804           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8805         {
8806           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8807           return fold_build2_loc (loc, code, type,
8808                               variable1,
8809                               fold_build2_loc (loc,
8810                                            TREE_CODE (arg1), TREE_TYPE (arg1),
8811                                            variable2, cst));
8812         }
8813
8814       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8815                              ? MINUS_EXPR : PLUS_EXPR,
8816                              const1, const2, 0);
8817       if (!TREE_OVERFLOW (cst)
8818           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8819         {
8820           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8821           return fold_build2_loc (loc, code, type,
8822                               fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
8823                                            variable1, cst),
8824                               variable2);
8825         }
8826     }
8827
8828   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8829      signed arithmetic case.  That form is created by the compiler
8830      often enough for folding it to be of value.  One example is in
8831      computing loop trip counts after Operator Strength Reduction.  */
8832   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8833       && TREE_CODE (arg0) == MULT_EXPR
8834       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8835           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8836       && integer_zerop (arg1))
8837     {
8838       tree const1 = TREE_OPERAND (arg0, 1);
8839       tree const2 = arg1;                       /* zero */
8840       tree variable1 = TREE_OPERAND (arg0, 0);
8841       enum tree_code cmp_code = code;
8842
8843       /* Handle unfolded multiplication by zero.  */
8844       if (integer_zerop (const1))
8845         return fold_build2_loc (loc, cmp_code, type, const1, const2);
8846
8847       fold_overflow_warning (("assuming signed overflow does not occur when "
8848                               "eliminating multiplication in comparison "
8849                               "with zero"),
8850                              WARN_STRICT_OVERFLOW_COMPARISON);
8851
8852       /* If const1 is negative we swap the sense of the comparison.  */
8853       if (tree_int_cst_sgn (const1) < 0)
8854         cmp_code = swap_tree_comparison (cmp_code);
8855
8856       return fold_build2_loc (loc, cmp_code, type, variable1, const2);
8857     }
8858
8859   tem = maybe_canonicalize_comparison (loc, code, type, op0, op1);
8860   if (tem)
8861     return tem;
8862
8863   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8864     {
8865       tree targ0 = strip_float_extensions (arg0);
8866       tree targ1 = strip_float_extensions (arg1);
8867       tree newtype = TREE_TYPE (targ0);
8868
8869       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8870         newtype = TREE_TYPE (targ1);
8871
8872       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
8873       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8874         return fold_build2_loc (loc, code, type,
8875                             fold_convert_loc (loc, newtype, targ0),
8876                             fold_convert_loc (loc, newtype, targ1));
8877
8878       /* (-a) CMP (-b) -> b CMP a  */
8879       if (TREE_CODE (arg0) == NEGATE_EXPR
8880           && TREE_CODE (arg1) == NEGATE_EXPR)
8881         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
8882                             TREE_OPERAND (arg0, 0));
8883
8884       if (TREE_CODE (arg1) == REAL_CST)
8885         {
8886           REAL_VALUE_TYPE cst;
8887           cst = TREE_REAL_CST (arg1);
8888
8889           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
8890           if (TREE_CODE (arg0) == NEGATE_EXPR)
8891             return fold_build2_loc (loc, swap_tree_comparison (code), type,
8892                                 TREE_OPERAND (arg0, 0),
8893                                 build_real (TREE_TYPE (arg1),
8894                                             real_value_negate (&cst)));
8895
8896           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
8897           /* a CMP (-0) -> a CMP 0  */
8898           if (REAL_VALUE_MINUS_ZERO (cst))
8899             return fold_build2_loc (loc, code, type, arg0,
8900                                 build_real (TREE_TYPE (arg1), dconst0));
8901
8902           /* x != NaN is always true, other ops are always false.  */
8903           if (REAL_VALUE_ISNAN (cst)
8904               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8905             {
8906               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8907               return omit_one_operand_loc (loc, type, tem, arg0);
8908             }
8909
8910           /* Fold comparisons against infinity.  */
8911           if (REAL_VALUE_ISINF (cst)
8912               && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
8913             {
8914               tem = fold_inf_compare (loc, code, type, arg0, arg1);
8915               if (tem != NULL_TREE)
8916                 return tem;
8917             }
8918         }
8919
8920       /* If this is a comparison of a real constant with a PLUS_EXPR
8921          or a MINUS_EXPR of a real constant, we can convert it into a
8922          comparison with a revised real constant as long as no overflow
8923          occurs when unsafe_math_optimizations are enabled.  */
8924       if (flag_unsafe_math_optimizations
8925           && TREE_CODE (arg1) == REAL_CST
8926           && (TREE_CODE (arg0) == PLUS_EXPR
8927               || TREE_CODE (arg0) == MINUS_EXPR)
8928           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
8929           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
8930                                       ? MINUS_EXPR : PLUS_EXPR,
8931                                       arg1, TREE_OPERAND (arg0, 1)))
8932           && !TREE_OVERFLOW (tem))
8933         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8934
8935       /* Likewise, we can simplify a comparison of a real constant with
8936          a MINUS_EXPR whose first operand is also a real constant, i.e.
8937          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
8938          floating-point types only if -fassociative-math is set.  */
8939       if (flag_associative_math
8940           && TREE_CODE (arg1) == REAL_CST
8941           && TREE_CODE (arg0) == MINUS_EXPR
8942           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
8943           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
8944                                       arg1))
8945           && !TREE_OVERFLOW (tem))
8946         return fold_build2_loc (loc, swap_tree_comparison (code), type,
8947                             TREE_OPERAND (arg0, 1), tem);
8948
8949       /* Fold comparisons against built-in math functions.  */
8950       if (TREE_CODE (arg1) == REAL_CST
8951           && flag_unsafe_math_optimizations
8952           && ! flag_errno_math)
8953         {
8954           enum built_in_function fcode = builtin_mathfn_code (arg0);
8955
8956           if (fcode != END_BUILTINS)
8957             {
8958               tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
8959               if (tem != NULL_TREE)
8960                 return tem;
8961             }
8962         }
8963     }
8964
8965   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
8966       && CONVERT_EXPR_P (arg0))
8967     {
8968       /* If we are widening one operand of an integer comparison,
8969          see if the other operand is similarly being widened.  Perhaps we
8970          can do the comparison in the narrower type.  */
8971       tem = fold_widened_comparison (loc, code, type, arg0, arg1);
8972       if (tem)
8973         return tem;
8974
8975       /* Or if we are changing signedness.  */
8976       tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
8977       if (tem)
8978         return tem;
8979     }
8980
8981   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8982      constant, we can simplify it.  */
8983   if (TREE_CODE (arg1) == INTEGER_CST
8984       && (TREE_CODE (arg0) == MIN_EXPR
8985           || TREE_CODE (arg0) == MAX_EXPR)
8986       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8987     {
8988       tem = optimize_minmax_comparison (loc, code, type, op0, op1);
8989       if (tem)
8990         return tem;
8991     }
8992
8993   /* Simplify comparison of something with itself.  (For IEEE
8994      floating-point, we can only do some of these simplifications.)  */
8995   if (operand_equal_p (arg0, arg1, 0))
8996     {
8997       switch (code)
8998         {
8999         case EQ_EXPR:
9000           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9001               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9002             return constant_boolean_node (1, type);
9003           break;
9004
9005         case GE_EXPR:
9006         case LE_EXPR:
9007           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9008               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9009             return constant_boolean_node (1, type);
9010           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9011
9012         case NE_EXPR:
9013           /* For NE, we can only do this simplification if integer
9014              or we don't honor IEEE floating point NaNs.  */
9015           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9016               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9017             break;
9018           /* ... fall through ...  */
9019         case GT_EXPR:
9020         case LT_EXPR:
9021           return constant_boolean_node (0, type);
9022         default:
9023           gcc_unreachable ();
9024         }
9025     }
9026
9027   /* If we are comparing an expression that just has comparisons
9028      of two integer values, arithmetic expressions of those comparisons,
9029      and constants, we can simplify it.  There are only three cases
9030      to check: the two values can either be equal, the first can be
9031      greater, or the second can be greater.  Fold the expression for
9032      those three values.  Since each value must be 0 or 1, we have
9033      eight possibilities, each of which corresponds to the constant 0
9034      or 1 or one of the six possible comparisons.
9035
9036      This handles common cases like (a > b) == 0 but also handles
9037      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9038      occur in macroized code.  */
9039
9040   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9041     {
9042       tree cval1 = 0, cval2 = 0;
9043       int save_p = 0;
9044
9045       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9046           /* Don't handle degenerate cases here; they should already
9047              have been handled anyway.  */
9048           && cval1 != 0 && cval2 != 0
9049           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9050           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9051           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9052           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9053           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9054           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9055                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9056         {
9057           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9058           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9059
9060           /* We can't just pass T to eval_subst in case cval1 or cval2
9061              was the same as ARG1.  */
9062
9063           tree high_result
9064                 = fold_build2_loc (loc, code, type,
9065                                eval_subst (loc, arg0, cval1, maxval,
9066                                            cval2, minval),
9067                                arg1);
9068           tree equal_result
9069                 = fold_build2_loc (loc, code, type,
9070                                eval_subst (loc, arg0, cval1, maxval,
9071                                            cval2, maxval),
9072                                arg1);
9073           tree low_result
9074                 = fold_build2_loc (loc, code, type,
9075                                eval_subst (loc, arg0, cval1, minval,
9076                                            cval2, maxval),
9077                                arg1);
9078
9079           /* All three of these results should be 0 or 1.  Confirm they are.
9080              Then use those values to select the proper code to use.  */
9081
9082           if (TREE_CODE (high_result) == INTEGER_CST
9083               && TREE_CODE (equal_result) == INTEGER_CST
9084               && TREE_CODE (low_result) == INTEGER_CST)
9085             {
9086               /* Make a 3-bit mask with the high-order bit being the
9087                  value for `>', the next for '=', and the low for '<'.  */
9088               switch ((integer_onep (high_result) * 4)
9089                       + (integer_onep (equal_result) * 2)
9090                       + integer_onep (low_result))
9091                 {
9092                 case 0:
9093                   /* Always false.  */
9094                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9095                 case 1:
9096                   code = LT_EXPR;
9097                   break;
9098                 case 2:
9099                   code = EQ_EXPR;
9100                   break;
9101                 case 3:
9102                   code = LE_EXPR;
9103                   break;
9104                 case 4:
9105                   code = GT_EXPR;
9106                   break;
9107                 case 5:
9108                   code = NE_EXPR;
9109                   break;
9110                 case 6:
9111                   code = GE_EXPR;
9112                   break;
9113                 case 7:
9114                   /* Always true.  */
9115                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9116                 }
9117
9118               if (save_p)
9119                 {
9120                   tem = save_expr (build2 (code, type, cval1, cval2));
9121                   SET_EXPR_LOCATION (tem, loc);
9122                   return tem;
9123                 }
9124               return fold_build2_loc (loc, code, type, cval1, cval2);
9125             }
9126         }
9127     }
9128
9129   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9130      into a single range test.  */
9131   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9132        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9133       && TREE_CODE (arg1) == INTEGER_CST
9134       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9135       && !integer_zerop (TREE_OPERAND (arg0, 1))
9136       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9137       && !TREE_OVERFLOW (arg1))
9138     {
9139       tem = fold_div_compare (loc, code, type, arg0, arg1);
9140       if (tem != NULL_TREE)
9141         return tem;
9142     }
9143
9144   /* Fold ~X op ~Y as Y op X.  */
9145   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9146       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9147     {
9148       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9149       return fold_build2_loc (loc, code, type,
9150                           fold_convert_loc (loc, cmp_type,
9151                                             TREE_OPERAND (arg1, 0)),
9152                           TREE_OPERAND (arg0, 0));
9153     }
9154
9155   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9156   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9157       && TREE_CODE (arg1) == INTEGER_CST)
9158     {
9159       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9160       return fold_build2_loc (loc, swap_tree_comparison (code), type,
9161                           TREE_OPERAND (arg0, 0),
9162                           fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9163                                        fold_convert_loc (loc, cmp_type, arg1)));
9164     }
9165
9166   return NULL_TREE;
9167 }
9168
9169
9170 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9171    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9172    argument EXPR represents the expression "z" of type TYPE.  */
9173
9174 static tree
9175 fold_mult_zconjz (location_t loc, tree type, tree expr)
9176 {
9177   tree itype = TREE_TYPE (type);
9178   tree rpart, ipart, tem;
9179
9180   if (TREE_CODE (expr) == COMPLEX_EXPR)
9181     {
9182       rpart = TREE_OPERAND (expr, 0);
9183       ipart = TREE_OPERAND (expr, 1);
9184     }
9185   else if (TREE_CODE (expr) == COMPLEX_CST)
9186     {
9187       rpart = TREE_REALPART (expr);
9188       ipart = TREE_IMAGPART (expr);
9189     }
9190   else
9191     {
9192       expr = save_expr (expr);
9193       rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9194       ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9195     }
9196
9197   rpart = save_expr (rpart);
9198   ipart = save_expr (ipart);
9199   tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9200                      fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9201                      fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9202   return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9203                           build_zero_cst (itype));
9204 }
9205
9206
9207 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9208    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9209    guarantees that P and N have the same least significant log2(M) bits.
9210    N is not otherwise constrained.  In particular, N is not normalized to
9211    0 <= N < M as is common.  In general, the precise value of P is unknown.
9212    M is chosen as large as possible such that constant N can be determined.
9213
9214    Returns M and sets *RESIDUE to N.
9215
9216    If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9217    account.  This is not always possible due to PR 35705.
9218  */
9219
9220 static unsigned HOST_WIDE_INT
9221 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9222                                  bool allow_func_align)
9223 {
9224   enum tree_code code;
9225
9226   *residue = 0;
9227
9228   code = TREE_CODE (expr);
9229   if (code == ADDR_EXPR)
9230     {
9231       unsigned int bitalign;
9232       bitalign = get_object_alignment_1 (TREE_OPERAND (expr, 0), residue);
9233       *residue /= BITS_PER_UNIT;
9234       return bitalign / BITS_PER_UNIT;
9235     }
9236   else if (code == POINTER_PLUS_EXPR)
9237     {
9238       tree op0, op1;
9239       unsigned HOST_WIDE_INT modulus;
9240       enum tree_code inner_code;
9241
9242       op0 = TREE_OPERAND (expr, 0);
9243       STRIP_NOPS (op0);
9244       modulus = get_pointer_modulus_and_residue (op0, residue,
9245                                                  allow_func_align);
9246
9247       op1 = TREE_OPERAND (expr, 1);
9248       STRIP_NOPS (op1);
9249       inner_code = TREE_CODE (op1);
9250       if (inner_code == INTEGER_CST)
9251         {
9252           *residue += TREE_INT_CST_LOW (op1);
9253           return modulus;
9254         }
9255       else if (inner_code == MULT_EXPR)
9256         {
9257           op1 = TREE_OPERAND (op1, 1);
9258           if (TREE_CODE (op1) == INTEGER_CST)
9259             {
9260               unsigned HOST_WIDE_INT align;
9261
9262               /* Compute the greatest power-of-2 divisor of op1.  */
9263               align = TREE_INT_CST_LOW (op1);
9264               align &= -align;
9265
9266               /* If align is non-zero and less than *modulus, replace
9267                  *modulus with align., If align is 0, then either op1 is 0
9268                  or the greatest power-of-2 divisor of op1 doesn't fit in an
9269                  unsigned HOST_WIDE_INT.  In either case, no additional
9270                  constraint is imposed.  */
9271               if (align)
9272                 modulus = MIN (modulus, align);
9273
9274               return modulus;
9275             }
9276         }
9277     }
9278
9279   /* If we get here, we were unable to determine anything useful about the
9280      expression.  */
9281   return 1;
9282 }
9283
9284
9285 /* Fold a binary expression of code CODE and type TYPE with operands
9286    OP0 and OP1.  LOC is the location of the resulting expression.
9287    Return the folded expression if folding is successful.  Otherwise,
9288    return NULL_TREE.  */
9289
9290 tree
9291 fold_binary_loc (location_t loc,
9292              enum tree_code code, tree type, tree op0, tree op1)
9293 {
9294   enum tree_code_class kind = TREE_CODE_CLASS (code);
9295   tree arg0, arg1, tem;
9296   tree t1 = NULL_TREE;
9297   bool strict_overflow_p;
9298
9299   gcc_assert (IS_EXPR_CODE_CLASS (kind)
9300               && TREE_CODE_LENGTH (code) == 2
9301               && op0 != NULL_TREE
9302               && op1 != NULL_TREE);
9303
9304   arg0 = op0;
9305   arg1 = op1;
9306
9307   /* Strip any conversions that don't change the mode.  This is
9308      safe for every expression, except for a comparison expression
9309      because its signedness is derived from its operands.  So, in
9310      the latter case, only strip conversions that don't change the
9311      signedness.  MIN_EXPR/MAX_EXPR also need signedness of arguments
9312      preserved.
9313
9314      Note that this is done as an internal manipulation within the
9315      constant folder, in order to find the simplest representation
9316      of the arguments so that their form can be studied.  In any
9317      cases, the appropriate type conversions should be put back in
9318      the tree that will get out of the constant folder.  */
9319
9320   if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9321     {
9322       STRIP_SIGN_NOPS (arg0);
9323       STRIP_SIGN_NOPS (arg1);
9324     }
9325   else
9326     {
9327       STRIP_NOPS (arg0);
9328       STRIP_NOPS (arg1);
9329     }
9330
9331   /* Note that TREE_CONSTANT isn't enough: static var addresses are
9332      constant but we can't do arithmetic on them.  */
9333   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9334       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9335       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9336       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9337       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9338       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9339     {
9340       if (kind == tcc_binary)
9341         {
9342           /* Make sure type and arg0 have the same saturating flag.  */
9343           gcc_assert (TYPE_SATURATING (type)
9344                       == TYPE_SATURATING (TREE_TYPE (arg0)));
9345           tem = const_binop (code, arg0, arg1);
9346         }
9347       else if (kind == tcc_comparison)
9348         tem = fold_relational_const (code, type, arg0, arg1);
9349       else
9350         tem = NULL_TREE;
9351
9352       if (tem != NULL_TREE)
9353         {
9354           if (TREE_TYPE (tem) != type)
9355             tem = fold_convert_loc (loc, type, tem);
9356           return tem;
9357         }
9358     }
9359
9360   /* If this is a commutative operation, and ARG0 is a constant, move it
9361      to ARG1 to reduce the number of tests below.  */
9362   if (commutative_tree_code (code)
9363       && tree_swap_operands_p (arg0, arg1, true))
9364     return fold_build2_loc (loc, code, type, op1, op0);
9365
9366   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9367
9368      First check for cases where an arithmetic operation is applied to a
9369      compound, conditional, or comparison operation.  Push the arithmetic
9370      operation inside the compound or conditional to see if any folding
9371      can then be done.  Convert comparison to conditional for this purpose.
9372      The also optimizes non-constant cases that used to be done in
9373      expand_expr.
9374
9375      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9376      one of the operands is a comparison and the other is a comparison, a
9377      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
9378      code below would make the expression more complex.  Change it to a
9379      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
9380      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
9381
9382   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9383        || code == EQ_EXPR || code == NE_EXPR)
9384       && ((truth_value_p (TREE_CODE (arg0))
9385            && (truth_value_p (TREE_CODE (arg1))
9386                || (TREE_CODE (arg1) == BIT_AND_EXPR
9387                    && integer_onep (TREE_OPERAND (arg1, 1)))))
9388           || (truth_value_p (TREE_CODE (arg1))
9389               && (truth_value_p (TREE_CODE (arg0))
9390                   || (TREE_CODE (arg0) == BIT_AND_EXPR
9391                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
9392     {
9393       tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9394                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9395                          : TRUTH_XOR_EXPR,
9396                          boolean_type_node,
9397                          fold_convert_loc (loc, boolean_type_node, arg0),
9398                          fold_convert_loc (loc, boolean_type_node, arg1));
9399
9400       if (code == EQ_EXPR)
9401         tem = invert_truthvalue_loc (loc, tem);
9402
9403       return fold_convert_loc (loc, type, tem);
9404     }
9405
9406   if (TREE_CODE_CLASS (code) == tcc_binary
9407       || TREE_CODE_CLASS (code) == tcc_comparison)
9408     {
9409       if (TREE_CODE (arg0) == COMPOUND_EXPR)
9410         {
9411           tem = fold_build2_loc (loc, code, type,
9412                              fold_convert_loc (loc, TREE_TYPE (op0),
9413                                                TREE_OPERAND (arg0, 1)), op1);
9414           return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9415                              tem);
9416         }
9417       if (TREE_CODE (arg1) == COMPOUND_EXPR
9418           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9419         {
9420           tem = fold_build2_loc (loc, code, type, op0,
9421                              fold_convert_loc (loc, TREE_TYPE (op1),
9422                                                TREE_OPERAND (arg1, 1)));
9423           return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9424                              tem);
9425         }
9426
9427       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9428         {
9429           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9430                                                      arg0, arg1,
9431                                                      /*cond_first_p=*/1);
9432           if (tem != NULL_TREE)
9433             return tem;
9434         }
9435
9436       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9437         {
9438           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9439                                                      arg1, arg0,
9440                                                      /*cond_first_p=*/0);
9441           if (tem != NULL_TREE)
9442             return tem;
9443         }
9444     }
9445
9446   switch (code)
9447     {
9448     case MEM_REF:
9449       /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2].  */
9450       if (TREE_CODE (arg0) == ADDR_EXPR
9451           && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9452         {
9453           tree iref = TREE_OPERAND (arg0, 0);
9454           return fold_build2 (MEM_REF, type,
9455                               TREE_OPERAND (iref, 0),
9456                               int_const_binop (PLUS_EXPR, arg1,
9457                                                TREE_OPERAND (iref, 1), 0));
9458         }
9459
9460       /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2].  */
9461       if (TREE_CODE (arg0) == ADDR_EXPR
9462           && handled_component_p (TREE_OPERAND (arg0, 0)))
9463         {
9464           tree base;
9465           HOST_WIDE_INT coffset;
9466           base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9467                                                 &coffset);
9468           if (!base)
9469             return NULL_TREE;
9470           return fold_build2 (MEM_REF, type,
9471                               build_fold_addr_expr (base),
9472                               int_const_binop (PLUS_EXPR, arg1,
9473                                                size_int (coffset), 0));
9474         }
9475
9476       return NULL_TREE;
9477
9478     case POINTER_PLUS_EXPR:
9479       /* 0 +p index -> (type)index */
9480       if (integer_zerop (arg0))
9481         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9482
9483       /* PTR +p 0 -> PTR */
9484       if (integer_zerop (arg1))
9485         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9486
9487       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
9488       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9489            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9490         return fold_convert_loc (loc, type,
9491                                  fold_build2_loc (loc, PLUS_EXPR, sizetype,
9492                                               fold_convert_loc (loc, sizetype,
9493                                                                 arg1),
9494                                               fold_convert_loc (loc, sizetype,
9495                                                                 arg0)));
9496
9497       /* index +p PTR -> PTR +p index */
9498       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9499           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9500         return fold_build2_loc (loc, POINTER_PLUS_EXPR, type,
9501                             fold_convert_loc (loc, type, arg1),
9502                             fold_convert_loc (loc, sizetype, arg0));
9503
9504       /* (PTR +p B) +p A -> PTR +p (B + A) */
9505       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9506         {
9507           tree inner;
9508           tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
9509           tree arg00 = TREE_OPERAND (arg0, 0);
9510           inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
9511                                arg01, fold_convert_loc (loc, sizetype, arg1));
9512           return fold_convert_loc (loc, type,
9513                                    fold_build2_loc (loc, POINTER_PLUS_EXPR,
9514                                                 TREE_TYPE (arg00),
9515                                                 arg00, inner));
9516         }
9517
9518       /* PTR_CST +p CST -> CST1 */
9519       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9520         return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
9521                             fold_convert_loc (loc, type, arg1));
9522
9523      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9524         of the array.  Loop optimizer sometimes produce this type of
9525         expressions.  */
9526       if (TREE_CODE (arg0) == ADDR_EXPR)
9527         {
9528           tem = try_move_mult_to_index (loc, arg0,
9529                                         fold_convert_loc (loc, sizetype, arg1));
9530           if (tem)
9531             return fold_convert_loc (loc, type, tem);
9532         }
9533
9534       return NULL_TREE;
9535
9536     case PLUS_EXPR:
9537       /* A + (-B) -> A - B */
9538       if (TREE_CODE (arg1) == NEGATE_EXPR)
9539         return fold_build2_loc (loc, MINUS_EXPR, type,
9540                             fold_convert_loc (loc, type, arg0),
9541                             fold_convert_loc (loc, type,
9542                                               TREE_OPERAND (arg1, 0)));
9543       /* (-A) + B -> B - A */
9544       if (TREE_CODE (arg0) == NEGATE_EXPR
9545           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9546         return fold_build2_loc (loc, MINUS_EXPR, type,
9547                             fold_convert_loc (loc, type, arg1),
9548                             fold_convert_loc (loc, type,
9549                                               TREE_OPERAND (arg0, 0)));
9550
9551       if (INTEGRAL_TYPE_P (type))
9552         {
9553           /* Convert ~A + 1 to -A.  */
9554           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9555               && integer_onep (arg1))
9556             return fold_build1_loc (loc, NEGATE_EXPR, type,
9557                                 fold_convert_loc (loc, type,
9558                                                   TREE_OPERAND (arg0, 0)));
9559
9560           /* ~X + X is -1.  */
9561           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9562               && !TYPE_OVERFLOW_TRAPS (type))
9563             {
9564               tree tem = TREE_OPERAND (arg0, 0);
9565
9566               STRIP_NOPS (tem);
9567               if (operand_equal_p (tem, arg1, 0))
9568                 {
9569                   t1 = build_int_cst_type (type, -1);
9570                   return omit_one_operand_loc (loc, type, t1, arg1);
9571                 }
9572             }
9573
9574           /* X + ~X is -1.  */
9575           if (TREE_CODE (arg1) == BIT_NOT_EXPR
9576               && !TYPE_OVERFLOW_TRAPS (type))
9577             {
9578               tree tem = TREE_OPERAND (arg1, 0);
9579
9580               STRIP_NOPS (tem);
9581               if (operand_equal_p (arg0, tem, 0))
9582                 {
9583                   t1 = build_int_cst_type (type, -1);
9584                   return omit_one_operand_loc (loc, type, t1, arg0);
9585                 }
9586             }
9587
9588           /* X + (X / CST) * -CST is X % CST.  */
9589           if (TREE_CODE (arg1) == MULT_EXPR
9590               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9591               && operand_equal_p (arg0,
9592                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9593             {
9594               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9595               tree cst1 = TREE_OPERAND (arg1, 1);
9596               tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9597                                       cst1, cst0);
9598               if (sum && integer_zerop (sum))
9599                 return fold_convert_loc (loc, type,
9600                                          fold_build2_loc (loc, TRUNC_MOD_EXPR,
9601                                                       TREE_TYPE (arg0), arg0,
9602                                                       cst0));
9603             }
9604         }
9605
9606       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9607          same or one.  Make sure type is not saturating.
9608          fold_plusminus_mult_expr will re-associate.  */
9609       if ((TREE_CODE (arg0) == MULT_EXPR
9610            || TREE_CODE (arg1) == MULT_EXPR)
9611           && !TYPE_SATURATING (type)
9612           && (!FLOAT_TYPE_P (type) || flag_associative_math))
9613         {
9614           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9615           if (tem)
9616             return tem;
9617         }
9618
9619       if (! FLOAT_TYPE_P (type))
9620         {
9621           if (integer_zerop (arg1))
9622             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9623
9624           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9625              with a constant, and the two constants have no bits in common,
9626              we should treat this as a BIT_IOR_EXPR since this may produce more
9627              simplifications.  */
9628           if (TREE_CODE (arg0) == BIT_AND_EXPR
9629               && TREE_CODE (arg1) == BIT_AND_EXPR
9630               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9631               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9632               && integer_zerop (const_binop (BIT_AND_EXPR,
9633                                              TREE_OPERAND (arg0, 1),
9634                                              TREE_OPERAND (arg1, 1))))
9635             {
9636               code = BIT_IOR_EXPR;
9637               goto bit_ior;
9638             }
9639
9640           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9641              (plus (plus (mult) (mult)) (foo)) so that we can
9642              take advantage of the factoring cases below.  */
9643           if (((TREE_CODE (arg0) == PLUS_EXPR
9644                 || TREE_CODE (arg0) == MINUS_EXPR)
9645                && TREE_CODE (arg1) == MULT_EXPR)
9646               || ((TREE_CODE (arg1) == PLUS_EXPR
9647                    || TREE_CODE (arg1) == MINUS_EXPR)
9648                   && TREE_CODE (arg0) == MULT_EXPR))
9649             {
9650               tree parg0, parg1, parg, marg;
9651               enum tree_code pcode;
9652
9653               if (TREE_CODE (arg1) == MULT_EXPR)
9654                 parg = arg0, marg = arg1;
9655               else
9656                 parg = arg1, marg = arg0;
9657               pcode = TREE_CODE (parg);
9658               parg0 = TREE_OPERAND (parg, 0);
9659               parg1 = TREE_OPERAND (parg, 1);
9660               STRIP_NOPS (parg0);
9661               STRIP_NOPS (parg1);
9662
9663               if (TREE_CODE (parg0) == MULT_EXPR
9664                   && TREE_CODE (parg1) != MULT_EXPR)
9665                 return fold_build2_loc (loc, pcode, type,
9666                                     fold_build2_loc (loc, PLUS_EXPR, type,
9667                                                  fold_convert_loc (loc, type,
9668                                                                    parg0),
9669                                                  fold_convert_loc (loc, type,
9670                                                                    marg)),
9671                                     fold_convert_loc (loc, type, parg1));
9672               if (TREE_CODE (parg0) != MULT_EXPR
9673                   && TREE_CODE (parg1) == MULT_EXPR)
9674                 return
9675                   fold_build2_loc (loc, PLUS_EXPR, type,
9676                                fold_convert_loc (loc, type, parg0),
9677                                fold_build2_loc (loc, pcode, type,
9678                                             fold_convert_loc (loc, type, marg),
9679                                             fold_convert_loc (loc, type,
9680                                                               parg1)));
9681             }
9682         }
9683       else
9684         {
9685           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
9686           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9687             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9688
9689           /* Likewise if the operands are reversed.  */
9690           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9691             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9692
9693           /* Convert X + -C into X - C.  */
9694           if (TREE_CODE (arg1) == REAL_CST
9695               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9696             {
9697               tem = fold_negate_const (arg1, type);
9698               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9699                 return fold_build2_loc (loc, MINUS_EXPR, type,
9700                                     fold_convert_loc (loc, type, arg0),
9701                                     fold_convert_loc (loc, type, tem));
9702             }
9703
9704           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9705              to __complex__ ( x, y ).  This is not the same for SNaNs or
9706              if signed zeros are involved.  */
9707           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9708               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9709               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9710             {
9711               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9712               tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9713               tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9714               bool arg0rz = false, arg0iz = false;
9715               if ((arg0r && (arg0rz = real_zerop (arg0r)))
9716                   || (arg0i && (arg0iz = real_zerop (arg0i))))
9717                 {
9718                   tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9719                   tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9720                   if (arg0rz && arg1i && real_zerop (arg1i))
9721                     {
9722                       tree rp = arg1r ? arg1r
9723                                   : build1 (REALPART_EXPR, rtype, arg1);
9724                       tree ip = arg0i ? arg0i
9725                                   : build1 (IMAGPART_EXPR, rtype, arg0);
9726                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9727                     }
9728                   else if (arg0iz && arg1r && real_zerop (arg1r))
9729                     {
9730                       tree rp = arg0r ? arg0r
9731                                   : build1 (REALPART_EXPR, rtype, arg0);
9732                       tree ip = arg1i ? arg1i
9733                                   : build1 (IMAGPART_EXPR, rtype, arg1);
9734                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9735                     }
9736                 }
9737             }
9738
9739           if (flag_unsafe_math_optimizations
9740               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9741               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9742               && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9743             return tem;
9744
9745           /* Convert x+x into x*2.0.  */
9746           if (operand_equal_p (arg0, arg1, 0)
9747               && SCALAR_FLOAT_TYPE_P (type))
9748             return fold_build2_loc (loc, MULT_EXPR, type, arg0,
9749                                 build_real (type, dconst2));
9750
9751           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9752              We associate floats only if the user has specified
9753              -fassociative-math.  */
9754           if (flag_associative_math
9755               && TREE_CODE (arg1) == PLUS_EXPR
9756               && TREE_CODE (arg0) != MULT_EXPR)
9757             {
9758               tree tree10 = TREE_OPERAND (arg1, 0);
9759               tree tree11 = TREE_OPERAND (arg1, 1);
9760               if (TREE_CODE (tree11) == MULT_EXPR
9761                   && TREE_CODE (tree10) == MULT_EXPR)
9762                 {
9763                   tree tree0;
9764                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9765                   return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9766                 }
9767             }
9768           /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9769              We associate floats only if the user has specified
9770              -fassociative-math.  */
9771           if (flag_associative_math
9772               && TREE_CODE (arg0) == PLUS_EXPR
9773               && TREE_CODE (arg1) != MULT_EXPR)
9774             {
9775               tree tree00 = TREE_OPERAND (arg0, 0);
9776               tree tree01 = TREE_OPERAND (arg0, 1);
9777               if (TREE_CODE (tree01) == MULT_EXPR
9778                   && TREE_CODE (tree00) == MULT_EXPR)
9779                 {
9780                   tree tree0;
9781                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9782                   return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9783                 }
9784             }
9785         }
9786
9787      bit_rotate:
9788       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9789          is a rotate of A by C1 bits.  */
9790       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9791          is a rotate of A by B bits.  */
9792       {
9793         enum tree_code code0, code1;
9794         tree rtype;
9795         code0 = TREE_CODE (arg0);
9796         code1 = TREE_CODE (arg1);
9797         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9798              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9799             && operand_equal_p (TREE_OPERAND (arg0, 0),
9800                                 TREE_OPERAND (arg1, 0), 0)
9801             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9802                 TYPE_UNSIGNED (rtype))
9803             /* Only create rotates in complete modes.  Other cases are not
9804                expanded properly.  */
9805             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
9806           {
9807             tree tree01, tree11;
9808             enum tree_code code01, code11;
9809
9810             tree01 = TREE_OPERAND (arg0, 1);
9811             tree11 = TREE_OPERAND (arg1, 1);
9812             STRIP_NOPS (tree01);
9813             STRIP_NOPS (tree11);
9814             code01 = TREE_CODE (tree01);
9815             code11 = TREE_CODE (tree11);
9816             if (code01 == INTEGER_CST
9817                 && code11 == INTEGER_CST
9818                 && TREE_INT_CST_HIGH (tree01) == 0
9819                 && TREE_INT_CST_HIGH (tree11) == 0
9820                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9821                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9822               {
9823                 tem = build2_loc (loc, LROTATE_EXPR,
9824                                   TREE_TYPE (TREE_OPERAND (arg0, 0)),
9825                                   TREE_OPERAND (arg0, 0),
9826                                   code0 == LSHIFT_EXPR ? tree01 : tree11);
9827                 return fold_convert_loc (loc, type, tem);
9828               }
9829             else if (code11 == MINUS_EXPR)
9830               {
9831                 tree tree110, tree111;
9832                 tree110 = TREE_OPERAND (tree11, 0);
9833                 tree111 = TREE_OPERAND (tree11, 1);
9834                 STRIP_NOPS (tree110);
9835                 STRIP_NOPS (tree111);
9836                 if (TREE_CODE (tree110) == INTEGER_CST
9837                     && 0 == compare_tree_int (tree110,
9838                                               TYPE_PRECISION
9839                                               (TREE_TYPE (TREE_OPERAND
9840                                                           (arg0, 0))))
9841                     && operand_equal_p (tree01, tree111, 0))
9842                   return
9843                     fold_convert_loc (loc, type,
9844                                       build2 ((code0 == LSHIFT_EXPR
9845                                                ? LROTATE_EXPR
9846                                                : RROTATE_EXPR),
9847                                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
9848                                               TREE_OPERAND (arg0, 0), tree01));
9849               }
9850             else if (code01 == MINUS_EXPR)
9851               {
9852                 tree tree010, tree011;
9853                 tree010 = TREE_OPERAND (tree01, 0);
9854                 tree011 = TREE_OPERAND (tree01, 1);
9855                 STRIP_NOPS (tree010);
9856                 STRIP_NOPS (tree011);
9857                 if (TREE_CODE (tree010) == INTEGER_CST
9858                     && 0 == compare_tree_int (tree010,
9859                                               TYPE_PRECISION
9860                                               (TREE_TYPE (TREE_OPERAND
9861                                                           (arg0, 0))))
9862                     && operand_equal_p (tree11, tree011, 0))
9863                     return fold_convert_loc
9864                       (loc, type,
9865                        build2 ((code0 != LSHIFT_EXPR
9866                                 ? LROTATE_EXPR
9867                                 : RROTATE_EXPR),
9868                                TREE_TYPE (TREE_OPERAND (arg0, 0)),
9869                                TREE_OPERAND (arg0, 0), tree11));
9870               }
9871           }
9872       }
9873
9874     associate:
9875       /* In most languages, can't associate operations on floats through
9876          parentheses.  Rather than remember where the parentheses were, we
9877          don't associate floats at all, unless the user has specified
9878          -fassociative-math.
9879          And, we need to make sure type is not saturating.  */
9880
9881       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9882           && !TYPE_SATURATING (type))
9883         {
9884           tree var0, con0, lit0, minus_lit0;
9885           tree var1, con1, lit1, minus_lit1;
9886           bool ok = true;
9887
9888           /* Split both trees into variables, constants, and literals.  Then
9889              associate each group together, the constants with literals,
9890              then the result with variables.  This increases the chances of
9891              literals being recombined later and of generating relocatable
9892              expressions for the sum of a constant and literal.  */
9893           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9894           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9895                              code == MINUS_EXPR);
9896
9897           /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
9898           if (code == MINUS_EXPR)
9899             code = PLUS_EXPR;
9900
9901           /* With undefined overflow we can only associate constants with one
9902              variable, and constants whose association doesn't overflow.  */
9903           if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9904               || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9905             {
9906               if (var0 && var1)
9907                 {
9908                   tree tmp0 = var0;
9909                   tree tmp1 = var1;
9910
9911                   if (TREE_CODE (tmp0) == NEGATE_EXPR)
9912                     tmp0 = TREE_OPERAND (tmp0, 0);
9913                   if (TREE_CODE (tmp1) == NEGATE_EXPR)
9914                     tmp1 = TREE_OPERAND (tmp1, 0);
9915                   /* The only case we can still associate with two variables
9916                      is if they are the same, modulo negation.  */
9917                   if (!operand_equal_p (tmp0, tmp1, 0))
9918                     ok = false;
9919                 }
9920
9921               if (ok && lit0 && lit1)
9922                 {
9923                   tree tmp0 = fold_convert (type, lit0);
9924                   tree tmp1 = fold_convert (type, lit1);
9925
9926                   if (!TREE_OVERFLOW (tmp0) && !TREE_OVERFLOW (tmp1)
9927                       && TREE_OVERFLOW (fold_build2 (code, type, tmp0, tmp1)))
9928                     ok = false;
9929                 }
9930             }
9931
9932           /* Only do something if we found more than two objects.  Otherwise,
9933              nothing has changed and we risk infinite recursion.  */
9934           if (ok
9935               && (2 < ((var0 != 0) + (var1 != 0)
9936                        + (con0 != 0) + (con1 != 0)
9937                        + (lit0 != 0) + (lit1 != 0)
9938                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
9939             {
9940               var0 = associate_trees (loc, var0, var1, code, type);
9941               con0 = associate_trees (loc, con0, con1, code, type);
9942               lit0 = associate_trees (loc, lit0, lit1, code, type);
9943               minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type);
9944
9945               /* Preserve the MINUS_EXPR if the negative part of the literal is
9946                  greater than the positive part.  Otherwise, the multiplicative
9947                  folding code (i.e extract_muldiv) may be fooled in case
9948                  unsigned constants are subtracted, like in the following
9949                  example: ((X*2 + 4) - 8U)/2.  */
9950               if (minus_lit0 && lit0)
9951                 {
9952                   if (TREE_CODE (lit0) == INTEGER_CST
9953                       && TREE_CODE (minus_lit0) == INTEGER_CST
9954                       && tree_int_cst_lt (lit0, minus_lit0))
9955                     {
9956                       minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9957                                                     MINUS_EXPR, type);
9958                       lit0 = 0;
9959                     }
9960                   else
9961                     {
9962                       lit0 = associate_trees (loc, lit0, minus_lit0,
9963                                               MINUS_EXPR, type);
9964                       minus_lit0 = 0;
9965                     }
9966                 }
9967               if (minus_lit0)
9968                 {
9969                   if (con0 == 0)
9970                     return
9971                       fold_convert_loc (loc, type,
9972                                         associate_trees (loc, var0, minus_lit0,
9973                                                          MINUS_EXPR, type));
9974                   else
9975                     {
9976                       con0 = associate_trees (loc, con0, minus_lit0,
9977                                               MINUS_EXPR, type);
9978                       return
9979                         fold_convert_loc (loc, type,
9980                                           associate_trees (loc, var0, con0,
9981                                                            PLUS_EXPR, type));
9982                     }
9983                 }
9984
9985               con0 = associate_trees (loc, con0, lit0, code, type);
9986               return
9987                 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9988                                                               code, type));
9989             }
9990         }
9991
9992       return NULL_TREE;
9993
9994     case MINUS_EXPR:
9995       /* Pointer simplifications for subtraction, simple reassociations. */
9996       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
9997         {
9998           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
9999           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10000               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10001             {
10002               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10003               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10004               tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10005               tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10006               return fold_build2_loc (loc, PLUS_EXPR, type,
10007                                   fold_build2_loc (loc, MINUS_EXPR, type,
10008                                                arg00, arg10),
10009                                   fold_build2_loc (loc, MINUS_EXPR, type,
10010                                                arg01, arg11));
10011             }
10012           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10013           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10014             {
10015               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10016               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10017               tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10018                                       fold_convert_loc (loc, type, arg1));
10019               if (tmp)
10020                 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10021             }
10022         }
10023       /* A - (-B) -> A + B */
10024       if (TREE_CODE (arg1) == NEGATE_EXPR)
10025         return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10026                             fold_convert_loc (loc, type,
10027                                               TREE_OPERAND (arg1, 0)));
10028       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10029       if (TREE_CODE (arg0) == NEGATE_EXPR
10030           && (FLOAT_TYPE_P (type)
10031               || INTEGRAL_TYPE_P (type))
10032           && negate_expr_p (arg1)
10033           && reorder_operands_p (arg0, arg1))
10034         return fold_build2_loc (loc, MINUS_EXPR, type,
10035                             fold_convert_loc (loc, type,
10036                                               negate_expr (arg1)),
10037                             fold_convert_loc (loc, type,
10038                                               TREE_OPERAND (arg0, 0)));
10039       /* Convert -A - 1 to ~A.  */
10040       if (INTEGRAL_TYPE_P (type)
10041           && TREE_CODE (arg0) == NEGATE_EXPR
10042           && integer_onep (arg1)
10043           && !TYPE_OVERFLOW_TRAPS (type))
10044         return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10045                             fold_convert_loc (loc, type,
10046                                               TREE_OPERAND (arg0, 0)));
10047
10048       /* Convert -1 - A to ~A.  */
10049       if (INTEGRAL_TYPE_P (type)
10050           && integer_all_onesp (arg0))
10051         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10052
10053
10054       /* X - (X / CST) * CST is X % CST.  */
10055       if (INTEGRAL_TYPE_P (type)
10056           && TREE_CODE (arg1) == MULT_EXPR
10057           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10058           && operand_equal_p (arg0,
10059                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10060           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10061                               TREE_OPERAND (arg1, 1), 0))
10062         return
10063           fold_convert_loc (loc, type,
10064                             fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10065                                          arg0, TREE_OPERAND (arg1, 1)));
10066
10067       if (! FLOAT_TYPE_P (type))
10068         {
10069           if (integer_zerop (arg0))
10070             return negate_expr (fold_convert_loc (loc, type, arg1));
10071           if (integer_zerop (arg1))
10072             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10073
10074           /* Fold A - (A & B) into ~B & A.  */
10075           if (!TREE_SIDE_EFFECTS (arg0)
10076               && TREE_CODE (arg1) == BIT_AND_EXPR)
10077             {
10078               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10079                 {
10080                   tree arg10 = fold_convert_loc (loc, type,
10081                                                  TREE_OPERAND (arg1, 0));
10082                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10083                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10084                                                    type, arg10),
10085                                       fold_convert_loc (loc, type, arg0));
10086                 }
10087               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10088                 {
10089                   tree arg11 = fold_convert_loc (loc,
10090                                                  type, TREE_OPERAND (arg1, 1));
10091                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10092                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10093                                                    type, arg11),
10094                                       fold_convert_loc (loc, type, arg0));
10095                 }
10096             }
10097
10098           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10099              any power of 2 minus 1.  */
10100           if (TREE_CODE (arg0) == BIT_AND_EXPR
10101               && TREE_CODE (arg1) == BIT_AND_EXPR
10102               && operand_equal_p (TREE_OPERAND (arg0, 0),
10103                                   TREE_OPERAND (arg1, 0), 0))
10104             {
10105               tree mask0 = TREE_OPERAND (arg0, 1);
10106               tree mask1 = TREE_OPERAND (arg1, 1);
10107               tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10108
10109               if (operand_equal_p (tem, mask1, 0))
10110                 {
10111                   tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10112                                      TREE_OPERAND (arg0, 0), mask1);
10113                   return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10114                 }
10115             }
10116         }
10117
10118       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10119       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10120         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10121
10122       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10123          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10124          (-ARG1 + ARG0) reduces to -ARG1.  */
10125       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10126         return negate_expr (fold_convert_loc (loc, type, arg1));
10127
10128       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10129          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10130          signed zeros are involved.  */
10131       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10132           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10133           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10134         {
10135           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10136           tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10137           tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10138           bool arg0rz = false, arg0iz = false;
10139           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10140               || (arg0i && (arg0iz = real_zerop (arg0i))))
10141             {
10142               tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10143               tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10144               if (arg0rz && arg1i && real_zerop (arg1i))
10145                 {
10146                   tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10147                                          arg1r ? arg1r
10148                                          : build1 (REALPART_EXPR, rtype, arg1));
10149                   tree ip = arg0i ? arg0i
10150                     : build1 (IMAGPART_EXPR, rtype, arg0);
10151                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10152                 }
10153               else if (arg0iz && arg1r && real_zerop (arg1r))
10154                 {
10155                   tree rp = arg0r ? arg0r
10156                     : build1 (REALPART_EXPR, rtype, arg0);
10157                   tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10158                                          arg1i ? arg1i
10159                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10160                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10161                 }
10162             }
10163         }
10164
10165       /* Fold &x - &x.  This can happen from &x.foo - &x.
10166          This is unsafe for certain floats even in non-IEEE formats.
10167          In IEEE, it is unsafe because it does wrong for NaNs.
10168          Also note that operand_equal_p is always false if an operand
10169          is volatile.  */
10170
10171       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10172           && operand_equal_p (arg0, arg1, 0))
10173         return build_zero_cst (type);
10174
10175       /* A - B -> A + (-B) if B is easily negatable.  */
10176       if (negate_expr_p (arg1)
10177           && ((FLOAT_TYPE_P (type)
10178                /* Avoid this transformation if B is a positive REAL_CST.  */
10179                && (TREE_CODE (arg1) != REAL_CST
10180                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10181               || INTEGRAL_TYPE_P (type)))
10182         return fold_build2_loc (loc, PLUS_EXPR, type,
10183                             fold_convert_loc (loc, type, arg0),
10184                             fold_convert_loc (loc, type,
10185                                               negate_expr (arg1)));
10186
10187       /* Try folding difference of addresses.  */
10188       {
10189         HOST_WIDE_INT diff;
10190
10191         if ((TREE_CODE (arg0) == ADDR_EXPR
10192              || TREE_CODE (arg1) == ADDR_EXPR)
10193             && ptr_difference_const (arg0, arg1, &diff))
10194           return build_int_cst_type (type, diff);
10195       }
10196
10197       /* Fold &a[i] - &a[j] to i-j.  */
10198       if (TREE_CODE (arg0) == ADDR_EXPR
10199           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10200           && TREE_CODE (arg1) == ADDR_EXPR
10201           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10202         {
10203           tree aref0 = TREE_OPERAND (arg0, 0);
10204           tree aref1 = TREE_OPERAND (arg1, 0);
10205           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10206                                TREE_OPERAND (aref1, 0), 0))
10207             {
10208               tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10209               tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10210               tree esz = array_ref_element_size (aref0);
10211               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10212               return fold_build2_loc (loc, MULT_EXPR, type, diff,
10213                                   fold_convert_loc (loc, type, esz));
10214
10215             }
10216         }
10217
10218       if (FLOAT_TYPE_P (type)
10219           && flag_unsafe_math_optimizations
10220           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10221           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10222           && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10223         return tem;
10224
10225       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10226          same or one.  Make sure type is not saturating.
10227          fold_plusminus_mult_expr will re-associate.  */
10228       if ((TREE_CODE (arg0) == MULT_EXPR
10229            || TREE_CODE (arg1) == MULT_EXPR)
10230           && !TYPE_SATURATING (type)
10231           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10232         {
10233           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10234           if (tem)
10235             return tem;
10236         }
10237
10238       goto associate;
10239
10240     case MULT_EXPR:
10241       /* (-A) * (-B) -> A * B  */
10242       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10243         return fold_build2_loc (loc, MULT_EXPR, type,
10244                             fold_convert_loc (loc, type,
10245                                               TREE_OPERAND (arg0, 0)),
10246                             fold_convert_loc (loc, type,
10247                                               negate_expr (arg1)));
10248       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10249         return fold_build2_loc (loc, MULT_EXPR, type,
10250                             fold_convert_loc (loc, type,
10251                                               negate_expr (arg0)),
10252                             fold_convert_loc (loc, type,
10253                                               TREE_OPERAND (arg1, 0)));
10254
10255       if (! FLOAT_TYPE_P (type))
10256         {
10257           if (integer_zerop (arg1))
10258             return omit_one_operand_loc (loc, type, arg1, arg0);
10259           if (integer_onep (arg1))
10260             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10261           /* Transform x * -1 into -x.  Make sure to do the negation
10262              on the original operand with conversions not stripped
10263              because we can only strip non-sign-changing conversions.  */
10264           if (integer_all_onesp (arg1))
10265             return fold_convert_loc (loc, type, negate_expr (op0));
10266           /* Transform x * -C into -x * C if x is easily negatable.  */
10267           if (TREE_CODE (arg1) == INTEGER_CST
10268               && tree_int_cst_sgn (arg1) == -1
10269               && negate_expr_p (arg0)
10270               && (tem = negate_expr (arg1)) != arg1
10271               && !TREE_OVERFLOW (tem))
10272             return fold_build2_loc (loc, MULT_EXPR, type,
10273                                 fold_convert_loc (loc, type,
10274                                                   negate_expr (arg0)),
10275                                 tem);
10276
10277           /* (a * (1 << b)) is (a << b)  */
10278           if (TREE_CODE (arg1) == LSHIFT_EXPR
10279               && integer_onep (TREE_OPERAND (arg1, 0)))
10280             return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10281                                 TREE_OPERAND (arg1, 1));
10282           if (TREE_CODE (arg0) == LSHIFT_EXPR
10283               && integer_onep (TREE_OPERAND (arg0, 0)))
10284             return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10285                                 TREE_OPERAND (arg0, 1));
10286
10287           /* (A + A) * C -> A * 2 * C  */
10288           if (TREE_CODE (arg0) == PLUS_EXPR
10289               && TREE_CODE (arg1) == INTEGER_CST
10290               && operand_equal_p (TREE_OPERAND (arg0, 0),
10291                                   TREE_OPERAND (arg0, 1), 0))
10292             return fold_build2_loc (loc, MULT_EXPR, type,
10293                                 omit_one_operand_loc (loc, type,
10294                                                   TREE_OPERAND (arg0, 0),
10295                                                   TREE_OPERAND (arg0, 1)),
10296                                 fold_build2_loc (loc, MULT_EXPR, type,
10297                                              build_int_cst (type, 2) , arg1));
10298
10299           strict_overflow_p = false;
10300           if (TREE_CODE (arg1) == INTEGER_CST
10301               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10302                                              &strict_overflow_p)))
10303             {
10304               if (strict_overflow_p)
10305                 fold_overflow_warning (("assuming signed overflow does not "
10306                                         "occur when simplifying "
10307                                         "multiplication"),
10308                                        WARN_STRICT_OVERFLOW_MISC);
10309               return fold_convert_loc (loc, type, tem);
10310             }
10311
10312           /* Optimize z * conj(z) for integer complex numbers.  */
10313           if (TREE_CODE (arg0) == CONJ_EXPR
10314               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10315             return fold_mult_zconjz (loc, type, arg1);
10316           if (TREE_CODE (arg1) == CONJ_EXPR
10317               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10318             return fold_mult_zconjz (loc, type, arg0);
10319         }
10320       else
10321         {
10322           /* Maybe fold x * 0 to 0.  The expressions aren't the same
10323              when x is NaN, since x * 0 is also NaN.  Nor are they the
10324              same in modes with signed zeros, since multiplying a
10325              negative value by 0 gives -0, not +0.  */
10326           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10327               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10328               && real_zerop (arg1))
10329             return omit_one_operand_loc (loc, type, arg1, arg0);
10330           /* In IEEE floating point, x*1 is not equivalent to x for snans.
10331              Likewise for complex arithmetic with signed zeros.  */
10332           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10333               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10334                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10335               && real_onep (arg1))
10336             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10337
10338           /* Transform x * -1.0 into -x.  */
10339           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10340               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10341                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10342               && real_minus_onep (arg1))
10343             return fold_convert_loc (loc, type, negate_expr (arg0));
10344
10345           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
10346              the result for floating point types due to rounding so it is applied
10347              only if -fassociative-math was specify.  */
10348           if (flag_associative_math
10349               && TREE_CODE (arg0) == RDIV_EXPR
10350               && TREE_CODE (arg1) == REAL_CST
10351               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10352             {
10353               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10354                                       arg1);
10355               if (tem)
10356                 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10357                                     TREE_OPERAND (arg0, 1));
10358             }
10359
10360           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
10361           if (operand_equal_p (arg0, arg1, 0))
10362             {
10363               tree tem = fold_strip_sign_ops (arg0);
10364               if (tem != NULL_TREE)
10365                 {
10366                   tem = fold_convert_loc (loc, type, tem);
10367                   return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
10368                 }
10369             }
10370
10371           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10372              This is not the same for NaNs or if signed zeros are
10373              involved.  */
10374           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10375               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10376               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10377               && TREE_CODE (arg1) == COMPLEX_CST
10378               && real_zerop (TREE_REALPART (arg1)))
10379             {
10380               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10381               if (real_onep (TREE_IMAGPART (arg1)))
10382                 return
10383                   fold_build2_loc (loc, COMPLEX_EXPR, type,
10384                                negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10385                                                              rtype, arg0)),
10386                                fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10387               else if (real_minus_onep (TREE_IMAGPART (arg1)))
10388                 return
10389                   fold_build2_loc (loc, COMPLEX_EXPR, type,
10390                                fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10391                                negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10392                                                              rtype, arg0)));
10393             }
10394
10395           /* Optimize z * conj(z) for floating point complex numbers.
10396              Guarded by flag_unsafe_math_optimizations as non-finite
10397              imaginary components don't produce scalar results.  */
10398           if (flag_unsafe_math_optimizations
10399               && TREE_CODE (arg0) == CONJ_EXPR
10400               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10401             return fold_mult_zconjz (loc, type, arg1);
10402           if (flag_unsafe_math_optimizations
10403               && TREE_CODE (arg1) == CONJ_EXPR
10404               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10405             return fold_mult_zconjz (loc, type, arg0);
10406
10407           if (flag_unsafe_math_optimizations)
10408             {
10409               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10410               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10411
10412               /* Optimizations of root(...)*root(...).  */
10413               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10414                 {
10415                   tree rootfn, arg;
10416                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10417                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10418
10419                   /* Optimize sqrt(x)*sqrt(x) as x.  */
10420                   if (BUILTIN_SQRT_P (fcode0)
10421                       && operand_equal_p (arg00, arg10, 0)
10422                       && ! HONOR_SNANS (TYPE_MODE (type)))
10423                     return arg00;
10424
10425                   /* Optimize root(x)*root(y) as root(x*y).  */
10426                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10427                   arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
10428                   return build_call_expr_loc (loc, rootfn, 1, arg);
10429                 }
10430
10431               /* Optimize expN(x)*expN(y) as expN(x+y).  */
10432               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10433                 {
10434                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10435                   tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10436                                           CALL_EXPR_ARG (arg0, 0),
10437                                           CALL_EXPR_ARG (arg1, 0));
10438                   return build_call_expr_loc (loc, expfn, 1, arg);
10439                 }
10440
10441               /* Optimizations of pow(...)*pow(...).  */
10442               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10443                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10444                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10445                 {
10446                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10447                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10448                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10449                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10450
10451                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
10452                   if (operand_equal_p (arg01, arg11, 0))
10453                     {
10454                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10455                       tree arg = fold_build2_loc (loc, MULT_EXPR, type,
10456                                               arg00, arg10);
10457                       return build_call_expr_loc (loc, powfn, 2, arg, arg01);
10458                     }
10459
10460                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
10461                   if (operand_equal_p (arg00, arg10, 0))
10462                     {
10463                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10464                       tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10465                                               arg01, arg11);
10466                       return build_call_expr_loc (loc, powfn, 2, arg00, arg);
10467                     }
10468                 }
10469
10470               /* Optimize tan(x)*cos(x) as sin(x).  */
10471               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10472                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10473                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10474                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10475                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10476                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10477                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10478                                       CALL_EXPR_ARG (arg1, 0), 0))
10479                 {
10480                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10481
10482                   if (sinfn != NULL_TREE)
10483                     return build_call_expr_loc (loc, sinfn, 1,
10484                                             CALL_EXPR_ARG (arg0, 0));
10485                 }
10486
10487               /* Optimize x*pow(x,c) as pow(x,c+1).  */
10488               if (fcode1 == BUILT_IN_POW
10489                   || fcode1 == BUILT_IN_POWF
10490                   || fcode1 == BUILT_IN_POWL)
10491                 {
10492                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10493                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10494                   if (TREE_CODE (arg11) == REAL_CST
10495                       && !TREE_OVERFLOW (arg11)
10496                       && operand_equal_p (arg0, arg10, 0))
10497                     {
10498                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10499                       REAL_VALUE_TYPE c;
10500                       tree arg;
10501
10502                       c = TREE_REAL_CST (arg11);
10503                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10504                       arg = build_real (type, c);
10505                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10506                     }
10507                 }
10508
10509               /* Optimize pow(x,c)*x as pow(x,c+1).  */
10510               if (fcode0 == BUILT_IN_POW
10511                   || fcode0 == BUILT_IN_POWF
10512                   || fcode0 == BUILT_IN_POWL)
10513                 {
10514                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10515                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10516                   if (TREE_CODE (arg01) == REAL_CST
10517                       && !TREE_OVERFLOW (arg01)
10518                       && operand_equal_p (arg1, arg00, 0))
10519                     {
10520                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10521                       REAL_VALUE_TYPE c;
10522                       tree arg;
10523
10524                       c = TREE_REAL_CST (arg01);
10525                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10526                       arg = build_real (type, c);
10527                       return build_call_expr_loc (loc, powfn, 2, arg1, arg);
10528                     }
10529                 }
10530
10531               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
10532               if (optimize_function_for_speed_p (cfun)
10533                   && operand_equal_p (arg0, arg1, 0))
10534                 {
10535                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10536
10537                   if (powfn)
10538                     {
10539                       tree arg = build_real (type, dconst2);
10540                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10541                     }
10542                 }
10543             }
10544         }
10545       goto associate;
10546
10547     case BIT_IOR_EXPR:
10548     bit_ior:
10549       if (integer_all_onesp (arg1))
10550         return omit_one_operand_loc (loc, type, arg1, arg0);
10551       if (integer_zerop (arg1))
10552         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10553       if (operand_equal_p (arg0, arg1, 0))
10554         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10555
10556       /* ~X | X is -1.  */
10557       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10558           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10559         {
10560           t1 = build_zero_cst (type);
10561           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10562           return omit_one_operand_loc (loc, type, t1, arg1);
10563         }
10564
10565       /* X | ~X is -1.  */
10566       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10567           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10568         {
10569           t1 = build_zero_cst (type);
10570           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10571           return omit_one_operand_loc (loc, type, t1, arg0);
10572         }
10573
10574       /* Canonicalize (X & C1) | C2.  */
10575       if (TREE_CODE (arg0) == BIT_AND_EXPR
10576           && TREE_CODE (arg1) == INTEGER_CST
10577           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10578         {
10579           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10580           int width = TYPE_PRECISION (type), w;
10581           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10582           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10583           hi2 = TREE_INT_CST_HIGH (arg1);
10584           lo2 = TREE_INT_CST_LOW (arg1);
10585
10586           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
10587           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10588             return omit_one_operand_loc (loc, type, arg1,
10589                                      TREE_OPERAND (arg0, 0));
10590
10591           if (width > HOST_BITS_PER_WIDE_INT)
10592             {
10593               mhi = (unsigned HOST_WIDE_INT) -1
10594                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
10595               mlo = -1;
10596             }
10597           else
10598             {
10599               mhi = 0;
10600               mlo = (unsigned HOST_WIDE_INT) -1
10601                     >> (HOST_BITS_PER_WIDE_INT - width);
10602             }
10603
10604           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
10605           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10606             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10607                                 TREE_OPERAND (arg0, 0), arg1);
10608
10609           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10610              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10611              mode which allows further optimizations.  */
10612           hi1 &= mhi;
10613           lo1 &= mlo;
10614           hi2 &= mhi;
10615           lo2 &= mlo;
10616           hi3 = hi1 & ~hi2;
10617           lo3 = lo1 & ~lo2;
10618           for (w = BITS_PER_UNIT;
10619                w <= width && w <= HOST_BITS_PER_WIDE_INT;
10620                w <<= 1)
10621             {
10622               unsigned HOST_WIDE_INT mask
10623                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10624               if (((lo1 | lo2) & mask) == mask
10625                   && (lo1 & ~mask) == 0 && hi1 == 0)
10626                 {
10627                   hi3 = 0;
10628                   lo3 = mask;
10629                   break;
10630                 }
10631             }
10632           if (hi3 != hi1 || lo3 != lo1)
10633             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10634                                 fold_build2_loc (loc, BIT_AND_EXPR, type,
10635                                              TREE_OPERAND (arg0, 0),
10636                                              build_int_cst_wide (type,
10637                                                                  lo3, hi3)),
10638                                 arg1);
10639         }
10640
10641       /* (X & Y) | Y is (X, Y).  */
10642       if (TREE_CODE (arg0) == BIT_AND_EXPR
10643           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10644         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10645       /* (X & Y) | X is (Y, X).  */
10646       if (TREE_CODE (arg0) == BIT_AND_EXPR
10647           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10648           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10649         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
10650       /* X | (X & Y) is (Y, X).  */
10651       if (TREE_CODE (arg1) == BIT_AND_EXPR
10652           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10653           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10654         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
10655       /* X | (Y & X) is (Y, X).  */
10656       if (TREE_CODE (arg1) == BIT_AND_EXPR
10657           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10658           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10659         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
10660
10661       /* (X & ~Y) | (~X & Y) is X ^ Y */
10662       if (TREE_CODE (arg0) == BIT_AND_EXPR
10663           && TREE_CODE (arg1) == BIT_AND_EXPR)
10664         {
10665           tree a0, a1, l0, l1, n0, n1;
10666
10667           a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10668           a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10669
10670           l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10671           l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10672           
10673           n0 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l0);
10674           n1 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l1);
10675           
10676           if ((operand_equal_p (n0, a0, 0)
10677                && operand_equal_p (n1, a1, 0))
10678               || (operand_equal_p (n0, a1, 0)
10679                   && operand_equal_p (n1, a0, 0)))
10680             return fold_build2_loc (loc, BIT_XOR_EXPR, type, l0, n1);
10681         }
10682
10683       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
10684       if (t1 != NULL_TREE)
10685         return t1;
10686
10687       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10688
10689          This results in more efficient code for machines without a NAND
10690          instruction.  Combine will canonicalize to the first form
10691          which will allow use of NAND instructions provided by the
10692          backend if they exist.  */
10693       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10694           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10695         {
10696           return
10697             fold_build1_loc (loc, BIT_NOT_EXPR, type,
10698                          build2 (BIT_AND_EXPR, type,
10699                                  fold_convert_loc (loc, type,
10700                                                    TREE_OPERAND (arg0, 0)),
10701                                  fold_convert_loc (loc, type,
10702                                                    TREE_OPERAND (arg1, 0))));
10703         }
10704
10705       /* See if this can be simplified into a rotate first.  If that
10706          is unsuccessful continue in the association code.  */
10707       goto bit_rotate;
10708
10709     case BIT_XOR_EXPR:
10710       if (integer_zerop (arg1))
10711         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10712       if (integer_all_onesp (arg1))
10713         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
10714       if (operand_equal_p (arg0, arg1, 0))
10715         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10716
10717       /* ~X ^ X is -1.  */
10718       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10719           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10720         {
10721           t1 = build_zero_cst (type);
10722           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10723           return omit_one_operand_loc (loc, type, t1, arg1);
10724         }
10725
10726       /* X ^ ~X is -1.  */
10727       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10728           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10729         {
10730           t1 = build_zero_cst (type);
10731           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10732           return omit_one_operand_loc (loc, type, t1, arg0);
10733         }
10734
10735       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10736          with a constant, and the two constants have no bits in common,
10737          we should treat this as a BIT_IOR_EXPR since this may produce more
10738          simplifications.  */
10739       if (TREE_CODE (arg0) == BIT_AND_EXPR
10740           && TREE_CODE (arg1) == BIT_AND_EXPR
10741           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10742           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10743           && integer_zerop (const_binop (BIT_AND_EXPR,
10744                                          TREE_OPERAND (arg0, 1),
10745                                          TREE_OPERAND (arg1, 1))))
10746         {
10747           code = BIT_IOR_EXPR;
10748           goto bit_ior;
10749         }
10750
10751       /* (X | Y) ^ X -> Y & ~ X*/
10752       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10753           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10754         {
10755           tree t2 = TREE_OPERAND (arg0, 1);
10756           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10757                             arg1);
10758           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10759                             fold_convert_loc (loc, type, t2),
10760                             fold_convert_loc (loc, type, t1));
10761           return t1;
10762         }
10763
10764       /* (Y | X) ^ X -> Y & ~ X*/
10765       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10766           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10767         {
10768           tree t2 = TREE_OPERAND (arg0, 0);
10769           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10770                             arg1);
10771           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10772                             fold_convert_loc (loc, type, t2),
10773                             fold_convert_loc (loc, type, t1));
10774           return t1;
10775         }
10776
10777       /* X ^ (X | Y) -> Y & ~ X*/
10778       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10779           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10780         {
10781           tree t2 = TREE_OPERAND (arg1, 1);
10782           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10783                             arg0);
10784           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10785                             fold_convert_loc (loc, type, t2),
10786                             fold_convert_loc (loc, type, t1));
10787           return t1;
10788         }
10789
10790       /* X ^ (Y | X) -> Y & ~ X*/
10791       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10792           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10793         {
10794           tree t2 = TREE_OPERAND (arg1, 0);
10795           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10796                             arg0);
10797           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10798                             fold_convert_loc (loc, type, t2),
10799                             fold_convert_loc (loc, type, t1));
10800           return t1;
10801         }
10802
10803       /* Convert ~X ^ ~Y to X ^ Y.  */
10804       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10805           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10806         return fold_build2_loc (loc, code, type,
10807                             fold_convert_loc (loc, type,
10808                                               TREE_OPERAND (arg0, 0)),
10809                             fold_convert_loc (loc, type,
10810                                               TREE_OPERAND (arg1, 0)));
10811
10812       /* Convert ~X ^ C to X ^ ~C.  */
10813       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10814           && TREE_CODE (arg1) == INTEGER_CST)
10815         return fold_build2_loc (loc, code, type,
10816                             fold_convert_loc (loc, type,
10817                                               TREE_OPERAND (arg0, 0)),
10818                             fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
10819
10820       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
10821       if (TREE_CODE (arg0) == BIT_AND_EXPR
10822           && integer_onep (TREE_OPERAND (arg0, 1))
10823           && integer_onep (arg1))
10824         return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10825                             build_int_cst (TREE_TYPE (arg0), 0));
10826
10827       /* Fold (X & Y) ^ Y as ~X & Y.  */
10828       if (TREE_CODE (arg0) == BIT_AND_EXPR
10829           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10830         {
10831           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10832           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10833                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10834                               fold_convert_loc (loc, type, arg1));
10835         }
10836       /* Fold (X & Y) ^ X as ~Y & X.  */
10837       if (TREE_CODE (arg0) == BIT_AND_EXPR
10838           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10839           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10840         {
10841           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10842           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10843                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10844                               fold_convert_loc (loc, type, arg1));
10845         }
10846       /* Fold X ^ (X & Y) as X & ~Y.  */
10847       if (TREE_CODE (arg1) == BIT_AND_EXPR
10848           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10849         {
10850           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10851           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10852                               fold_convert_loc (loc, type, arg0),
10853                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10854         }
10855       /* Fold X ^ (Y & X) as ~Y & X.  */
10856       if (TREE_CODE (arg1) == BIT_AND_EXPR
10857           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10858           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10859         {
10860           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10861           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10862                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10863                               fold_convert_loc (loc, type, arg0));
10864         }
10865
10866       /* See if this can be simplified into a rotate first.  If that
10867          is unsuccessful continue in the association code.  */
10868       goto bit_rotate;
10869
10870     case BIT_AND_EXPR:
10871       if (integer_all_onesp (arg1))
10872         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10873       if (integer_zerop (arg1))
10874         return omit_one_operand_loc (loc, type, arg1, arg0);
10875       if (operand_equal_p (arg0, arg1, 0))
10876         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10877
10878       /* ~X & X is always zero.  */
10879       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10880           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10881         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10882
10883       /* X & ~X is always zero.  */
10884       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10885           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10886         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10887
10888       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
10889       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10890           && TREE_CODE (arg1) == INTEGER_CST
10891           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10892         {
10893           tree tmp1 = fold_convert_loc (loc, type, arg1);
10894           tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10895           tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10896           tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
10897           tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
10898           return
10899             fold_convert_loc (loc, type,
10900                               fold_build2_loc (loc, BIT_IOR_EXPR,
10901                                            type, tmp2, tmp3));
10902         }
10903
10904       /* (X | Y) & Y is (X, Y).  */
10905       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10906           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10907         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10908       /* (X | Y) & X is (Y, X).  */
10909       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10910           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10911           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10912         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
10913       /* X & (X | Y) is (Y, X).  */
10914       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10915           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10916           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10917         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
10918       /* X & (Y | X) is (Y, X).  */
10919       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10920           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10921           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10922         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
10923
10924       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
10925       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10926           && integer_onep (TREE_OPERAND (arg0, 1))
10927           && integer_onep (arg1))
10928         {
10929           tem = TREE_OPERAND (arg0, 0);
10930           return fold_build2_loc (loc, EQ_EXPR, type,
10931                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
10932                                            build_int_cst (TREE_TYPE (tem), 1)),
10933                               build_int_cst (TREE_TYPE (tem), 0));
10934         }
10935       /* Fold ~X & 1 as (X & 1) == 0.  */
10936       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10937           && integer_onep (arg1))
10938         {
10939           tem = TREE_OPERAND (arg0, 0);
10940           return fold_build2_loc (loc, EQ_EXPR, type,
10941                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
10942                                            build_int_cst (TREE_TYPE (tem), 1)),
10943                               build_int_cst (TREE_TYPE (tem), 0));
10944         }
10945
10946       /* Fold (X ^ Y) & Y as ~X & Y.  */
10947       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10948           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10949         {
10950           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10951           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10952                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10953                               fold_convert_loc (loc, type, arg1));
10954         }
10955       /* Fold (X ^ Y) & X as ~Y & X.  */
10956       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10957           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10958           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10959         {
10960           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10961           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10962                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10963                               fold_convert_loc (loc, type, arg1));
10964         }
10965       /* Fold X & (X ^ Y) as X & ~Y.  */
10966       if (TREE_CODE (arg1) == BIT_XOR_EXPR
10967           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10968         {
10969           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10970           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10971                               fold_convert_loc (loc, type, arg0),
10972                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10973         }
10974       /* Fold X & (Y ^ X) as ~Y & X.  */
10975       if (TREE_CODE (arg1) == BIT_XOR_EXPR
10976           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10977           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10978         {
10979           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10980           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10981                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10982                               fold_convert_loc (loc, type, arg0));
10983         }
10984
10985       /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10986          ((A & N) + B) & M -> (A + B) & M
10987          Similarly if (N & M) == 0,
10988          ((A | N) + B) & M -> (A + B) & M
10989          and for - instead of + (or unary - instead of +)
10990          and/or ^ instead of |.
10991          If B is constant and (B & M) == 0, fold into A & M.  */
10992       if (host_integerp (arg1, 1))
10993         {
10994           unsigned HOST_WIDE_INT cst1 = tree_low_cst (arg1, 1);
10995           if (~cst1 && (cst1 & (cst1 + 1)) == 0
10996               && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10997               && (TREE_CODE (arg0) == PLUS_EXPR
10998                   || TREE_CODE (arg0) == MINUS_EXPR
10999                   || TREE_CODE (arg0) == NEGATE_EXPR)
11000               && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
11001                   || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
11002             {
11003               tree pmop[2];
11004               int which = 0;
11005               unsigned HOST_WIDE_INT cst0;
11006
11007               /* Now we know that arg0 is (C + D) or (C - D) or
11008                  -C and arg1 (M) is == (1LL << cst) - 1.
11009                  Store C into PMOP[0] and D into PMOP[1].  */
11010               pmop[0] = TREE_OPERAND (arg0, 0);
11011               pmop[1] = NULL;
11012               if (TREE_CODE (arg0) != NEGATE_EXPR)
11013                 {
11014                   pmop[1] = TREE_OPERAND (arg0, 1);
11015                   which = 1;
11016                 }
11017
11018               if (!host_integerp (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11019                   || (tree_low_cst (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11020                       & cst1) != cst1)
11021                 which = -1;
11022
11023               for (; which >= 0; which--)
11024                 switch (TREE_CODE (pmop[which]))
11025                   {
11026                   case BIT_AND_EXPR:
11027                   case BIT_IOR_EXPR:
11028                   case BIT_XOR_EXPR:
11029                     if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
11030                         != INTEGER_CST)
11031                       break;
11032                     /* tree_low_cst not used, because we don't care about
11033                        the upper bits.  */
11034                     cst0 = TREE_INT_CST_LOW (TREE_OPERAND (pmop[which], 1));
11035                     cst0 &= cst1;
11036                     if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
11037                       {
11038                         if (cst0 != cst1)
11039                           break;
11040                       }
11041                     else if (cst0 != 0)
11042                       break;
11043                     /* If C or D is of the form (A & N) where
11044                        (N & M) == M, or of the form (A | N) or
11045                        (A ^ N) where (N & M) == 0, replace it with A.  */
11046                     pmop[which] = TREE_OPERAND (pmop[which], 0);
11047                     break;
11048                   case INTEGER_CST:
11049                     /* If C or D is a N where (N & M) == 0, it can be
11050                        omitted (assumed 0).  */
11051                     if ((TREE_CODE (arg0) == PLUS_EXPR
11052                          || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
11053                         && (TREE_INT_CST_LOW (pmop[which]) & cst1) == 0)
11054                       pmop[which] = NULL;
11055                     break;
11056                   default:
11057                     break;
11058                   }
11059
11060               /* Only build anything new if we optimized one or both arguments
11061                  above.  */
11062               if (pmop[0] != TREE_OPERAND (arg0, 0)
11063                   || (TREE_CODE (arg0) != NEGATE_EXPR
11064                       && pmop[1] != TREE_OPERAND (arg0, 1)))
11065                 {
11066                   tree utype = TREE_TYPE (arg0);
11067                   if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11068                     {
11069                       /* Perform the operations in a type that has defined
11070                          overflow behavior.  */
11071                       utype = unsigned_type_for (TREE_TYPE (arg0));
11072                       if (pmop[0] != NULL)
11073                         pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
11074                       if (pmop[1] != NULL)
11075                         pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
11076                     }
11077
11078                   if (TREE_CODE (arg0) == NEGATE_EXPR)
11079                     tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
11080                   else if (TREE_CODE (arg0) == PLUS_EXPR)
11081                     {
11082                       if (pmop[0] != NULL && pmop[1] != NULL)
11083                         tem = fold_build2_loc (loc, PLUS_EXPR, utype,
11084                                                pmop[0], pmop[1]);
11085                       else if (pmop[0] != NULL)
11086                         tem = pmop[0];
11087                       else if (pmop[1] != NULL)
11088                         tem = pmop[1];
11089                       else
11090                         return build_int_cst (type, 0);
11091                     }
11092                   else if (pmop[0] == NULL)
11093                     tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
11094                   else
11095                     tem = fold_build2_loc (loc, MINUS_EXPR, utype,
11096                                            pmop[0], pmop[1]);
11097                   /* TEM is now the new binary +, - or unary - replacement.  */
11098                   tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
11099                                          fold_convert_loc (loc, utype, arg1));
11100                   return fold_convert_loc (loc, type, tem);
11101                 }
11102             }
11103         }
11104
11105       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11106       if (t1 != NULL_TREE)
11107         return t1;
11108       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
11109       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11110           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11111         {
11112           unsigned int prec
11113             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11114
11115           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11116               && (~TREE_INT_CST_LOW (arg1)
11117                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11118             return
11119               fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11120         }
11121
11122       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11123
11124          This results in more efficient code for machines without a NOR
11125          instruction.  Combine will canonicalize to the first form
11126          which will allow use of NOR instructions provided by the
11127          backend if they exist.  */
11128       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11129           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11130         {
11131           return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11132                               build2 (BIT_IOR_EXPR, type,
11133                                       fold_convert_loc (loc, type,
11134                                                         TREE_OPERAND (arg0, 0)),
11135                                       fold_convert_loc (loc, type,
11136                                                         TREE_OPERAND (arg1, 0))));
11137         }
11138
11139       /* If arg0 is derived from the address of an object or function, we may
11140          be able to fold this expression using the object or function's
11141          alignment.  */
11142       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11143         {
11144           unsigned HOST_WIDE_INT modulus, residue;
11145           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11146
11147           modulus = get_pointer_modulus_and_residue (arg0, &residue,
11148                                                      integer_onep (arg1));
11149
11150           /* This works because modulus is a power of 2.  If this weren't the
11151              case, we'd have to replace it by its greatest power-of-2
11152              divisor: modulus & -modulus.  */
11153           if (low < modulus)
11154             return build_int_cst (type, residue & low);
11155         }
11156
11157       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11158               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11159          if the new mask might be further optimized.  */
11160       if ((TREE_CODE (arg0) == LSHIFT_EXPR
11161            || TREE_CODE (arg0) == RSHIFT_EXPR)
11162           && host_integerp (TREE_OPERAND (arg0, 1), 1)
11163           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11164           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11165              < TYPE_PRECISION (TREE_TYPE (arg0))
11166           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11167           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11168         {
11169           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11170           unsigned HOST_WIDE_INT mask
11171             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11172           unsigned HOST_WIDE_INT newmask, zerobits = 0;
11173           tree shift_type = TREE_TYPE (arg0);
11174
11175           if (TREE_CODE (arg0) == LSHIFT_EXPR)
11176             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11177           else if (TREE_CODE (arg0) == RSHIFT_EXPR
11178                    && TYPE_PRECISION (TREE_TYPE (arg0))
11179                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11180             {
11181               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11182               tree arg00 = TREE_OPERAND (arg0, 0);
11183               /* See if more bits can be proven as zero because of
11184                  zero extension.  */
11185               if (TREE_CODE (arg00) == NOP_EXPR
11186                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11187                 {
11188                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11189                   if (TYPE_PRECISION (inner_type)
11190                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11191                       && TYPE_PRECISION (inner_type) < prec)
11192                     {
11193                       prec = TYPE_PRECISION (inner_type);
11194                       /* See if we can shorten the right shift.  */
11195                       if (shiftc < prec)
11196                         shift_type = inner_type;
11197                     }
11198                 }
11199               zerobits = ~(unsigned HOST_WIDE_INT) 0;
11200               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11201               zerobits <<= prec - shiftc;
11202               /* For arithmetic shift if sign bit could be set, zerobits
11203                  can contain actually sign bits, so no transformation is
11204                  possible, unless MASK masks them all away.  In that
11205                  case the shift needs to be converted into logical shift.  */
11206               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11207                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11208                 {
11209                   if ((mask & zerobits) == 0)
11210                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
11211                   else
11212                     zerobits = 0;
11213                 }
11214             }
11215
11216           /* ((X << 16) & 0xff00) is (X, 0).  */
11217           if ((mask & zerobits) == mask)
11218             return omit_one_operand_loc (loc, type,
11219                                      build_int_cst (type, 0), arg0);
11220
11221           newmask = mask | zerobits;
11222           if (newmask != mask && (newmask & (newmask + 1)) == 0)
11223             {
11224               unsigned int prec;
11225
11226               /* Only do the transformation if NEWMASK is some integer
11227                  mode's mask.  */
11228               for (prec = BITS_PER_UNIT;
11229                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11230                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11231                   break;
11232               if (prec < HOST_BITS_PER_WIDE_INT
11233                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
11234                 {
11235                   tree newmaskt;
11236
11237                   if (shift_type != TREE_TYPE (arg0))
11238                     {
11239                       tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11240                                          fold_convert_loc (loc, shift_type,
11241                                                            TREE_OPERAND (arg0, 0)),
11242                                          TREE_OPERAND (arg0, 1));
11243                       tem = fold_convert_loc (loc, type, tem);
11244                     }
11245                   else
11246                     tem = op0;
11247                   newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11248                   if (!tree_int_cst_equal (newmaskt, arg1))
11249                     return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11250                 }
11251             }
11252         }
11253
11254       goto associate;
11255
11256     case RDIV_EXPR:
11257       /* Don't touch a floating-point divide by zero unless the mode
11258          of the constant can represent infinity.  */
11259       if (TREE_CODE (arg1) == REAL_CST
11260           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11261           && real_zerop (arg1))
11262         return NULL_TREE;
11263
11264       /* Optimize A / A to 1.0 if we don't care about
11265          NaNs or Infinities.  Skip the transformation
11266          for non-real operands.  */
11267       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11268           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11269           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11270           && operand_equal_p (arg0, arg1, 0))
11271         {
11272           tree r = build_real (TREE_TYPE (arg0), dconst1);
11273
11274           return omit_two_operands_loc (loc, type, r, arg0, arg1);
11275         }
11276
11277       /* The complex version of the above A / A optimization.  */
11278       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11279           && operand_equal_p (arg0, arg1, 0))
11280         {
11281           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11282           if (! HONOR_NANS (TYPE_MODE (elem_type))
11283               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11284             {
11285               tree r = build_real (elem_type, dconst1);
11286               /* omit_two_operands will call fold_convert for us.  */
11287               return omit_two_operands_loc (loc, type, r, arg0, arg1);
11288             }
11289         }
11290
11291       /* (-A) / (-B) -> A / B  */
11292       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11293         return fold_build2_loc (loc, RDIV_EXPR, type,
11294                             TREE_OPERAND (arg0, 0),
11295                             negate_expr (arg1));
11296       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11297         return fold_build2_loc (loc, RDIV_EXPR, type,
11298                             negate_expr (arg0),
11299                             TREE_OPERAND (arg1, 0));
11300
11301       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
11302       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11303           && real_onep (arg1))
11304         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11305
11306       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
11307       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11308           && real_minus_onep (arg1))
11309         return non_lvalue_loc (loc, fold_convert_loc (loc, type,
11310                                                   negate_expr (arg0)));
11311
11312       /* If ARG1 is a constant, we can convert this to a multiply by the
11313          reciprocal.  This does not have the same rounding properties,
11314          so only do this if -freciprocal-math.  We can actually
11315          always safely do it if ARG1 is a power of two, but it's hard to
11316          tell if it is or not in a portable manner.  */
11317       if (TREE_CODE (arg1) == REAL_CST)
11318         {
11319           if (flag_reciprocal_math
11320               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11321                                           arg1)))
11322             return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
11323           /* Find the reciprocal if optimizing and the result is exact.  */
11324           if (optimize)
11325             {
11326               REAL_VALUE_TYPE r;
11327               r = TREE_REAL_CST (arg1);
11328               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11329                 {
11330                   tem = build_real (type, r);
11331                   return fold_build2_loc (loc, MULT_EXPR, type,
11332                                       fold_convert_loc (loc, type, arg0), tem);
11333                 }
11334             }
11335         }
11336       /* Convert A/B/C to A/(B*C).  */
11337       if (flag_reciprocal_math
11338           && TREE_CODE (arg0) == RDIV_EXPR)
11339         return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11340                             fold_build2_loc (loc, MULT_EXPR, type,
11341                                          TREE_OPERAND (arg0, 1), arg1));
11342
11343       /* Convert A/(B/C) to (A/B)*C.  */
11344       if (flag_reciprocal_math
11345           && TREE_CODE (arg1) == RDIV_EXPR)
11346         return fold_build2_loc (loc, MULT_EXPR, type,
11347                             fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11348                                          TREE_OPERAND (arg1, 0)),
11349                             TREE_OPERAND (arg1, 1));
11350
11351       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11352       if (flag_reciprocal_math
11353           && TREE_CODE (arg1) == MULT_EXPR
11354           && TREE_CODE (arg0) == REAL_CST
11355           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11356         {
11357           tree tem = const_binop (RDIV_EXPR, arg0,
11358                                   TREE_OPERAND (arg1, 1));
11359           if (tem)
11360             return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11361                                 TREE_OPERAND (arg1, 0));
11362         }
11363
11364       if (flag_unsafe_math_optimizations)
11365         {
11366           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11367           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11368
11369           /* Optimize sin(x)/cos(x) as tan(x).  */
11370           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11371                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11372                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11373               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11374                                   CALL_EXPR_ARG (arg1, 0), 0))
11375             {
11376               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11377
11378               if (tanfn != NULL_TREE)
11379                 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11380             }
11381
11382           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11383           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11384                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11385                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11386               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11387                                   CALL_EXPR_ARG (arg1, 0), 0))
11388             {
11389               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11390
11391               if (tanfn != NULL_TREE)
11392                 {
11393                   tree tmp = build_call_expr_loc (loc, tanfn, 1,
11394                                               CALL_EXPR_ARG (arg0, 0));
11395                   return fold_build2_loc (loc, RDIV_EXPR, type,
11396                                       build_real (type, dconst1), tmp);
11397                 }
11398             }
11399
11400           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11401              NaNs or Infinities.  */
11402           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11403                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11404                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11405             {
11406               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11407               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11408
11409               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11410                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11411                   && operand_equal_p (arg00, arg01, 0))
11412                 {
11413                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11414
11415                   if (cosfn != NULL_TREE)
11416                     return build_call_expr_loc (loc, cosfn, 1, arg00);
11417                 }
11418             }
11419
11420           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11421              NaNs or Infinities.  */
11422           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11423                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11424                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11425             {
11426               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11427               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11428
11429               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11430                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11431                   && operand_equal_p (arg00, arg01, 0))
11432                 {
11433                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11434
11435                   if (cosfn != NULL_TREE)
11436                     {
11437                       tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11438                       return fold_build2_loc (loc, RDIV_EXPR, type,
11439                                           build_real (type, dconst1),
11440                                           tmp);
11441                     }
11442                 }
11443             }
11444
11445           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11446           if (fcode0 == BUILT_IN_POW
11447               || fcode0 == BUILT_IN_POWF
11448               || fcode0 == BUILT_IN_POWL)
11449             {
11450               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11451               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11452               if (TREE_CODE (arg01) == REAL_CST
11453                   && !TREE_OVERFLOW (arg01)
11454                   && operand_equal_p (arg1, arg00, 0))
11455                 {
11456                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11457                   REAL_VALUE_TYPE c;
11458                   tree arg;
11459
11460                   c = TREE_REAL_CST (arg01);
11461                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11462                   arg = build_real (type, c);
11463                   return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11464                 }
11465             }
11466
11467           /* Optimize a/root(b/c) into a*root(c/b).  */
11468           if (BUILTIN_ROOT_P (fcode1))
11469             {
11470               tree rootarg = CALL_EXPR_ARG (arg1, 0);
11471
11472               if (TREE_CODE (rootarg) == RDIV_EXPR)
11473                 {
11474                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11475                   tree b = TREE_OPERAND (rootarg, 0);
11476                   tree c = TREE_OPERAND (rootarg, 1);
11477
11478                   tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
11479
11480                   tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
11481                   return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
11482                 }
11483             }
11484
11485           /* Optimize x/expN(y) into x*expN(-y).  */
11486           if (BUILTIN_EXPONENT_P (fcode1))
11487             {
11488               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11489               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11490               arg1 = build_call_expr_loc (loc,
11491                                       expfn, 1,
11492                                       fold_convert_loc (loc, type, arg));
11493               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11494             }
11495
11496           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
11497           if (fcode1 == BUILT_IN_POW
11498               || fcode1 == BUILT_IN_POWF
11499               || fcode1 == BUILT_IN_POWL)
11500             {
11501               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11502               tree arg10 = CALL_EXPR_ARG (arg1, 0);
11503               tree arg11 = CALL_EXPR_ARG (arg1, 1);
11504               tree neg11 = fold_convert_loc (loc, type,
11505                                              negate_expr (arg11));
11506               arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
11507               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11508             }
11509         }
11510       return NULL_TREE;
11511
11512     case TRUNC_DIV_EXPR:
11513       /* Optimize (X & (-A)) / A where A is a power of 2,
11514          to X >> log2(A) */
11515       if (TREE_CODE (arg0) == BIT_AND_EXPR
11516           && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST
11517           && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0)
11518         {
11519           tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1),
11520                                       arg1, TREE_OPERAND (arg0, 1));
11521           if (sum && integer_zerop (sum)) {
11522             unsigned long pow2;
11523
11524             if (TREE_INT_CST_LOW (arg1))
11525               pow2 = exact_log2 (TREE_INT_CST_LOW (arg1));
11526             else
11527               pow2 = exact_log2 (TREE_INT_CST_HIGH (arg1))
11528                       + HOST_BITS_PER_WIDE_INT;
11529
11530             return fold_build2_loc (loc, RSHIFT_EXPR, type,
11531                           TREE_OPERAND (arg0, 0),
11532                           build_int_cst (NULL_TREE, pow2));
11533           }
11534         }
11535
11536       /* Fall thru */
11537       
11538     case FLOOR_DIV_EXPR:
11539       /* Simplify A / (B << N) where A and B are positive and B is
11540          a power of 2, to A >> (N + log2(B)).  */
11541       strict_overflow_p = false;
11542       if (TREE_CODE (arg1) == LSHIFT_EXPR
11543           && (TYPE_UNSIGNED (type)
11544               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11545         {
11546           tree sval = TREE_OPERAND (arg1, 0);
11547           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11548             {
11549               tree sh_cnt = TREE_OPERAND (arg1, 1);
11550               unsigned long pow2;
11551
11552               if (TREE_INT_CST_LOW (sval))
11553                 pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11554               else
11555                 pow2 = exact_log2 (TREE_INT_CST_HIGH (sval))
11556                        + HOST_BITS_PER_WIDE_INT;
11557
11558               if (strict_overflow_p)
11559                 fold_overflow_warning (("assuming signed overflow does not "
11560                                         "occur when simplifying A / (B << N)"),
11561                                        WARN_STRICT_OVERFLOW_MISC);
11562
11563               sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11564                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
11565               return fold_build2_loc (loc, RSHIFT_EXPR, type,
11566                                   fold_convert_loc (loc, type, arg0), sh_cnt);
11567             }
11568         }
11569
11570       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11571          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
11572       if (INTEGRAL_TYPE_P (type)
11573           && TYPE_UNSIGNED (type)
11574           && code == FLOOR_DIV_EXPR)
11575         return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
11576
11577       /* Fall thru */
11578
11579     case ROUND_DIV_EXPR:
11580     case CEIL_DIV_EXPR:
11581     case EXACT_DIV_EXPR:
11582       if (integer_onep (arg1))
11583         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11584       if (integer_zerop (arg1))
11585         return NULL_TREE;
11586       /* X / -1 is -X.  */
11587       if (!TYPE_UNSIGNED (type)
11588           && TREE_CODE (arg1) == INTEGER_CST
11589           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11590           && TREE_INT_CST_HIGH (arg1) == -1)
11591         return fold_convert_loc (loc, type, negate_expr (arg0));
11592
11593       /* Convert -A / -B to A / B when the type is signed and overflow is
11594          undefined.  */
11595       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11596           && TREE_CODE (arg0) == NEGATE_EXPR
11597           && negate_expr_p (arg1))
11598         {
11599           if (INTEGRAL_TYPE_P (type))
11600             fold_overflow_warning (("assuming signed overflow does not occur "
11601                                     "when distributing negation across "
11602                                     "division"),
11603                                    WARN_STRICT_OVERFLOW_MISC);
11604           return fold_build2_loc (loc, code, type,
11605                               fold_convert_loc (loc, type,
11606                                                 TREE_OPERAND (arg0, 0)),
11607                               fold_convert_loc (loc, type,
11608                                                 negate_expr (arg1)));
11609         }
11610       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11611           && TREE_CODE (arg1) == NEGATE_EXPR
11612           && negate_expr_p (arg0))
11613         {
11614           if (INTEGRAL_TYPE_P (type))
11615             fold_overflow_warning (("assuming signed overflow does not occur "
11616                                     "when distributing negation across "
11617                                     "division"),
11618                                    WARN_STRICT_OVERFLOW_MISC);
11619           return fold_build2_loc (loc, code, type,
11620                               fold_convert_loc (loc, type,
11621                                                 negate_expr (arg0)),
11622                               fold_convert_loc (loc, type,
11623                                                 TREE_OPERAND (arg1, 0)));
11624         }
11625
11626       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11627          operation, EXACT_DIV_EXPR.
11628
11629          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11630          At one time others generated faster code, it's not clear if they do
11631          after the last round to changes to the DIV code in expmed.c.  */
11632       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11633           && multiple_of_p (type, arg0, arg1))
11634         return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
11635
11636       strict_overflow_p = false;
11637       if (TREE_CODE (arg1) == INTEGER_CST
11638           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11639                                          &strict_overflow_p)))
11640         {
11641           if (strict_overflow_p)
11642             fold_overflow_warning (("assuming signed overflow does not occur "
11643                                     "when simplifying division"),
11644                                    WARN_STRICT_OVERFLOW_MISC);
11645           return fold_convert_loc (loc, type, tem);
11646         }
11647
11648       return NULL_TREE;
11649
11650     case CEIL_MOD_EXPR:
11651     case FLOOR_MOD_EXPR:
11652     case ROUND_MOD_EXPR:
11653     case TRUNC_MOD_EXPR:
11654       /* X % 1 is always zero, but be sure to preserve any side
11655          effects in X.  */
11656       if (integer_onep (arg1))
11657         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11658
11659       /* X % 0, return X % 0 unchanged so that we can get the
11660          proper warnings and errors.  */
11661       if (integer_zerop (arg1))
11662         return NULL_TREE;
11663
11664       /* 0 % X is always zero, but be sure to preserve any side
11665          effects in X.  Place this after checking for X == 0.  */
11666       if (integer_zerop (arg0))
11667         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11668
11669       /* X % -1 is zero.  */
11670       if (!TYPE_UNSIGNED (type)
11671           && TREE_CODE (arg1) == INTEGER_CST
11672           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11673           && TREE_INT_CST_HIGH (arg1) == -1)
11674         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11675
11676       /* X % -C is the same as X % C.  */
11677       if (code == TRUNC_MOD_EXPR
11678           && !TYPE_UNSIGNED (type)
11679           && TREE_CODE (arg1) == INTEGER_CST
11680           && !TREE_OVERFLOW (arg1)
11681           && TREE_INT_CST_HIGH (arg1) < 0
11682           && !TYPE_OVERFLOW_TRAPS (type)
11683           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
11684           && !sign_bit_p (arg1, arg1))
11685         return fold_build2_loc (loc, code, type,
11686                             fold_convert_loc (loc, type, arg0),
11687                             fold_convert_loc (loc, type,
11688                                               negate_expr (arg1)));
11689
11690       /* X % -Y is the same as X % Y.  */
11691       if (code == TRUNC_MOD_EXPR
11692           && !TYPE_UNSIGNED (type)
11693           && TREE_CODE (arg1) == NEGATE_EXPR
11694           && !TYPE_OVERFLOW_TRAPS (type))
11695         return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
11696                             fold_convert_loc (loc, type,
11697                                               TREE_OPERAND (arg1, 0)));
11698
11699       strict_overflow_p = false;
11700       if (TREE_CODE (arg1) == INTEGER_CST
11701           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11702                                          &strict_overflow_p)))
11703         {
11704           if (strict_overflow_p)
11705             fold_overflow_warning (("assuming signed overflow does not occur "
11706                                     "when simplifying modulus"),
11707                                    WARN_STRICT_OVERFLOW_MISC);
11708           return fold_convert_loc (loc, type, tem);
11709         }
11710
11711       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11712          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
11713       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11714           && (TYPE_UNSIGNED (type)
11715               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11716         {
11717           tree c = arg1;
11718           /* Also optimize A % (C << N)  where C is a power of 2,
11719              to A & ((C << N) - 1).  */
11720           if (TREE_CODE (arg1) == LSHIFT_EXPR)
11721             c = TREE_OPERAND (arg1, 0);
11722
11723           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11724             {
11725               tree mask
11726                 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
11727                                    build_int_cst (TREE_TYPE (arg1), 1));
11728               if (strict_overflow_p)
11729                 fold_overflow_warning (("assuming signed overflow does not "
11730                                         "occur when simplifying "
11731                                         "X % (power of two)"),
11732                                        WARN_STRICT_OVERFLOW_MISC);
11733               return fold_build2_loc (loc, BIT_AND_EXPR, type,
11734                                       fold_convert_loc (loc, type, arg0),
11735                                       fold_convert_loc (loc, type, mask));
11736             }
11737         }
11738
11739       return NULL_TREE;
11740
11741     case LROTATE_EXPR:
11742     case RROTATE_EXPR:
11743       if (integer_all_onesp (arg0))
11744         return omit_one_operand_loc (loc, type, arg0, arg1);
11745       goto shift;
11746
11747     case RSHIFT_EXPR:
11748       /* Optimize -1 >> x for arithmetic right shifts.  */
11749       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11750           && tree_expr_nonnegative_p (arg1))
11751         return omit_one_operand_loc (loc, type, arg0, arg1);
11752       /* ... fall through ...  */
11753
11754     case LSHIFT_EXPR:
11755     shift:
11756       if (integer_zerop (arg1))
11757         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11758       if (integer_zerop (arg0))
11759         return omit_one_operand_loc (loc, type, arg0, arg1);
11760
11761       /* Since negative shift count is not well-defined,
11762          don't try to compute it in the compiler.  */
11763       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11764         return NULL_TREE;
11765
11766       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
11767       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11768           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11769           && host_integerp (TREE_OPERAND (arg0, 1), false)
11770           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11771         {
11772           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11773                                + TREE_INT_CST_LOW (arg1));
11774
11775           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11776              being well defined.  */
11777           if (low >= TYPE_PRECISION (type))
11778             {
11779               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11780                 low = low % TYPE_PRECISION (type);
11781               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11782                 return omit_one_operand_loc (loc, type, build_int_cst (type, 0),
11783                                          TREE_OPERAND (arg0, 0));
11784               else
11785                 low = TYPE_PRECISION (type) - 1;
11786             }
11787
11788           return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11789                               build_int_cst (type, low));
11790         }
11791
11792       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11793          into x & ((unsigned)-1 >> c) for unsigned types.  */
11794       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11795            || (TYPE_UNSIGNED (type)
11796                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11797           && host_integerp (arg1, false)
11798           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11799           && host_integerp (TREE_OPERAND (arg0, 1), false)
11800           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11801         {
11802           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11803           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11804           tree lshift;
11805           tree arg00;
11806
11807           if (low0 == low1)
11808             {
11809               arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11810
11811               lshift = build_int_cst (type, -1);
11812               lshift = int_const_binop (code, lshift, arg1, 0);
11813
11814               return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
11815             }
11816         }
11817
11818       /* Rewrite an LROTATE_EXPR by a constant into an
11819          RROTATE_EXPR by a new constant.  */
11820       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11821         {
11822           tree tem = build_int_cst (TREE_TYPE (arg1),
11823                                     TYPE_PRECISION (type));
11824           tem = const_binop (MINUS_EXPR, tem, arg1);
11825           return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
11826         }
11827
11828       /* If we have a rotate of a bit operation with the rotate count and
11829          the second operand of the bit operation both constant,
11830          permute the two operations.  */
11831       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11832           && (TREE_CODE (arg0) == BIT_AND_EXPR
11833               || TREE_CODE (arg0) == BIT_IOR_EXPR
11834               || TREE_CODE (arg0) == BIT_XOR_EXPR)
11835           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11836         return fold_build2_loc (loc, TREE_CODE (arg0), type,
11837                             fold_build2_loc (loc, code, type,
11838                                          TREE_OPERAND (arg0, 0), arg1),
11839                             fold_build2_loc (loc, code, type,
11840                                          TREE_OPERAND (arg0, 1), arg1));
11841
11842       /* Two consecutive rotates adding up to the precision of the
11843          type can be ignored.  */
11844       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11845           && TREE_CODE (arg0) == RROTATE_EXPR
11846           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11847           && TREE_INT_CST_HIGH (arg1) == 0
11848           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11849           && ((TREE_INT_CST_LOW (arg1)
11850                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11851               == (unsigned int) TYPE_PRECISION (type)))
11852         return TREE_OPERAND (arg0, 0);
11853
11854       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11855               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11856          if the latter can be further optimized.  */
11857       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11858           && TREE_CODE (arg0) == BIT_AND_EXPR
11859           && TREE_CODE (arg1) == INTEGER_CST
11860           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11861         {
11862           tree mask = fold_build2_loc (loc, code, type,
11863                                    fold_convert_loc (loc, type,
11864                                                      TREE_OPERAND (arg0, 1)),
11865                                    arg1);
11866           tree shift = fold_build2_loc (loc, code, type,
11867                                     fold_convert_loc (loc, type,
11868                                                       TREE_OPERAND (arg0, 0)),
11869                                     arg1);
11870           tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
11871           if (tem)
11872             return tem;
11873         }
11874
11875       return NULL_TREE;
11876
11877     case MIN_EXPR:
11878       if (operand_equal_p (arg0, arg1, 0))
11879         return omit_one_operand_loc (loc, type, arg0, arg1);
11880       if (INTEGRAL_TYPE_P (type)
11881           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11882         return omit_one_operand_loc (loc, type, arg1, arg0);
11883       tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
11884       if (tem)
11885         return tem;
11886       goto associate;
11887
11888     case MAX_EXPR:
11889       if (operand_equal_p (arg0, arg1, 0))
11890         return omit_one_operand_loc (loc, type, arg0, arg1);
11891       if (INTEGRAL_TYPE_P (type)
11892           && TYPE_MAX_VALUE (type)
11893           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11894         return omit_one_operand_loc (loc, type, arg1, arg0);
11895       tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
11896       if (tem)
11897         return tem;
11898       goto associate;
11899
11900     case TRUTH_ANDIF_EXPR:
11901       /* Note that the operands of this must be ints
11902          and their values must be 0 or 1.
11903          ("true" is a fixed value perhaps depending on the language.)  */
11904       /* If first arg is constant zero, return it.  */
11905       if (integer_zerop (arg0))
11906         return fold_convert_loc (loc, type, arg0);
11907     case TRUTH_AND_EXPR:
11908       /* If either arg is constant true, drop it.  */
11909       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11910         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11911       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11912           /* Preserve sequence points.  */
11913           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11914         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11915       /* If second arg is constant zero, result is zero, but first arg
11916          must be evaluated.  */
11917       if (integer_zerop (arg1))
11918         return omit_one_operand_loc (loc, type, arg1, arg0);
11919       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11920          case will be handled here.  */
11921       if (integer_zerop (arg0))
11922         return omit_one_operand_loc (loc, type, arg0, arg1);
11923
11924       /* !X && X is always false.  */
11925       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11926           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11927         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11928       /* X && !X is always false.  */
11929       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11930           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11931         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11932
11933       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
11934          means A >= Y && A != MAX, but in this case we know that
11935          A < X <= MAX.  */
11936
11937       if (!TREE_SIDE_EFFECTS (arg0)
11938           && !TREE_SIDE_EFFECTS (arg1))
11939         {
11940           tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
11941           if (tem && !operand_equal_p (tem, arg0, 0))
11942             return fold_build2_loc (loc, code, type, tem, arg1);
11943
11944           tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
11945           if (tem && !operand_equal_p (tem, arg1, 0))
11946             return fold_build2_loc (loc, code, type, arg0, tem);
11947         }
11948
11949     truth_andor:
11950       /* We only do these simplifications if we are optimizing.  */
11951       if (!optimize)
11952         return NULL_TREE;
11953
11954       /* Check for things like (A || B) && (A || C).  We can convert this
11955          to A || (B && C).  Note that either operator can be any of the four
11956          truth and/or operations and the transformation will still be
11957          valid.   Also note that we only care about order for the
11958          ANDIF and ORIF operators.  If B contains side effects, this
11959          might change the truth-value of A.  */
11960       if (TREE_CODE (arg0) == TREE_CODE (arg1)
11961           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
11962               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
11963               || TREE_CODE (arg0) == TRUTH_AND_EXPR
11964               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
11965           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
11966         {
11967           tree a00 = TREE_OPERAND (arg0, 0);
11968           tree a01 = TREE_OPERAND (arg0, 1);
11969           tree a10 = TREE_OPERAND (arg1, 0);
11970           tree a11 = TREE_OPERAND (arg1, 1);
11971           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
11972                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
11973                              && (code == TRUTH_AND_EXPR
11974                                  || code == TRUTH_OR_EXPR));
11975
11976           if (operand_equal_p (a00, a10, 0))
11977             return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
11978                                 fold_build2_loc (loc, code, type, a01, a11));
11979           else if (commutative && operand_equal_p (a00, a11, 0))
11980             return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
11981                                 fold_build2_loc (loc, code, type, a01, a10));
11982           else if (commutative && operand_equal_p (a01, a10, 0))
11983             return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
11984                                 fold_build2_loc (loc, code, type, a00, a11));
11985
11986           /* This case if tricky because we must either have commutative
11987              operators or else A10 must not have side-effects.  */
11988
11989           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
11990                    && operand_equal_p (a01, a11, 0))
11991             return fold_build2_loc (loc, TREE_CODE (arg0), type,
11992                                 fold_build2_loc (loc, code, type, a00, a10),
11993                                 a01);
11994         }
11995
11996       /* See if we can build a range comparison.  */
11997       if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
11998         return tem;
11999
12000       if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
12001           || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
12002         {
12003           tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
12004           if (tem)
12005             return fold_build2_loc (loc, code, type, tem, arg1);
12006         }
12007
12008       if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
12009           || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
12010         {
12011           tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
12012           if (tem)
12013             return fold_build2_loc (loc, code, type, arg0, tem);
12014         }
12015
12016       /* Check for the possibility of merging component references.  If our
12017          lhs is another similar operation, try to merge its rhs with our
12018          rhs.  Then try to merge our lhs and rhs.  */
12019       if (TREE_CODE (arg0) == code
12020           && 0 != (tem = fold_truthop (loc, code, type,
12021                                        TREE_OPERAND (arg0, 1), arg1)))
12022         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12023
12024       if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0)
12025         return tem;
12026
12027       return NULL_TREE;
12028
12029     case TRUTH_ORIF_EXPR:
12030       /* Note that the operands of this must be ints
12031          and their values must be 0 or true.
12032          ("true" is a fixed value perhaps depending on the language.)  */
12033       /* If first arg is constant true, return it.  */
12034       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12035         return fold_convert_loc (loc, type, arg0);
12036     case TRUTH_OR_EXPR:
12037       /* If either arg is constant zero, drop it.  */
12038       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12039         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12040       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12041           /* Preserve sequence points.  */
12042           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12043         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12044       /* If second arg is constant true, result is true, but we must
12045          evaluate first arg.  */
12046       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12047         return omit_one_operand_loc (loc, type, arg1, arg0);
12048       /* Likewise for first arg, but note this only occurs here for
12049          TRUTH_OR_EXPR.  */
12050       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12051         return omit_one_operand_loc (loc, type, arg0, arg1);
12052
12053       /* !X || X is always true.  */
12054       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12055           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12056         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12057       /* X || !X is always true.  */
12058       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12059           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12060         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12061
12062       /* (X && !Y) || (!X && Y) is X ^ Y */
12063       if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12064           && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12065         {
12066           tree a0, a1, l0, l1, n0, n1;
12067
12068           a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12069           a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12070
12071           l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12072           l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12073           
12074           n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12075           n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12076           
12077           if ((operand_equal_p (n0, a0, 0)
12078                && operand_equal_p (n1, a1, 0))
12079               || (operand_equal_p (n0, a1, 0)
12080                   && operand_equal_p (n1, a0, 0)))
12081             return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12082         }
12083       goto truth_andor;
12084
12085     case TRUTH_XOR_EXPR:
12086       /* If the second arg is constant zero, drop it.  */
12087       if (integer_zerop (arg1))
12088         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12089       /* If the second arg is constant true, this is a logical inversion.  */
12090       if (integer_onep (arg1))
12091         {
12092           /* Only call invert_truthvalue if operand is a truth value.  */
12093           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12094             tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12095           else
12096             tem = invert_truthvalue_loc (loc, arg0);
12097           return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12098         }
12099       /* Identical arguments cancel to zero.  */
12100       if (operand_equal_p (arg0, arg1, 0))
12101         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12102
12103       /* !X ^ X is always true.  */
12104       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12105           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12106         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12107
12108       /* X ^ !X is always true.  */
12109       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12110           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12111         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12112
12113       return NULL_TREE;
12114
12115     case EQ_EXPR:
12116     case NE_EXPR:
12117       STRIP_NOPS (arg0);
12118       STRIP_NOPS (arg1);
12119
12120       tem = fold_comparison (loc, code, type, op0, op1);
12121       if (tem != NULL_TREE)
12122         return tem;
12123
12124       /* bool_var != 0 becomes bool_var. */
12125       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12126           && code == NE_EXPR)
12127         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12128
12129       /* bool_var == 1 becomes bool_var. */
12130       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12131           && code == EQ_EXPR)
12132         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12133
12134       /* bool_var != 1 becomes !bool_var. */
12135       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12136           && code == NE_EXPR)
12137         return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12138                             fold_convert_loc (loc, type, arg0));
12139
12140       /* bool_var == 0 becomes !bool_var. */
12141       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12142           && code == EQ_EXPR)
12143         return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12144                             fold_convert_loc (loc, type, arg0));
12145
12146       /* !exp != 0 becomes !exp */
12147       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12148           && code == NE_EXPR)
12149         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12150
12151       /* If this is an equality comparison of the address of two non-weak,
12152          unaliased symbols neither of which are extern (since we do not
12153          have access to attributes for externs), then we know the result.  */
12154       if (TREE_CODE (arg0) == ADDR_EXPR
12155           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12156           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12157           && ! lookup_attribute ("alias",
12158                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12159           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12160           && TREE_CODE (arg1) == ADDR_EXPR
12161           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12162           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12163           && ! lookup_attribute ("alias",
12164                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12165           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12166         {
12167           /* We know that we're looking at the address of two
12168              non-weak, unaliased, static _DECL nodes.
12169
12170              It is both wasteful and incorrect to call operand_equal_p
12171              to compare the two ADDR_EXPR nodes.  It is wasteful in that
12172              all we need to do is test pointer equality for the arguments
12173              to the two ADDR_EXPR nodes.  It is incorrect to use
12174              operand_equal_p as that function is NOT equivalent to a
12175              C equality test.  It can in fact return false for two
12176              objects which would test as equal using the C equality
12177              operator.  */
12178           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12179           return constant_boolean_node (equal
12180                                         ? code == EQ_EXPR : code != EQ_EXPR,
12181                                         type);
12182         }
12183
12184       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12185          a MINUS_EXPR of a constant, we can convert it into a comparison with
12186          a revised constant as long as no overflow occurs.  */
12187       if (TREE_CODE (arg1) == INTEGER_CST
12188           && (TREE_CODE (arg0) == PLUS_EXPR
12189               || TREE_CODE (arg0) == MINUS_EXPR)
12190           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12191           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12192                                       ? MINUS_EXPR : PLUS_EXPR,
12193                                       fold_convert_loc (loc, TREE_TYPE (arg0),
12194                                                         arg1),
12195                                       TREE_OPERAND (arg0, 1)))
12196           && !TREE_OVERFLOW (tem))
12197         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12198
12199       /* Similarly for a NEGATE_EXPR.  */
12200       if (TREE_CODE (arg0) == NEGATE_EXPR
12201           && TREE_CODE (arg1) == INTEGER_CST
12202           && 0 != (tem = negate_expr (fold_convert_loc (loc, TREE_TYPE (arg0),
12203                                                         arg1)))
12204           && TREE_CODE (tem) == INTEGER_CST
12205           && !TREE_OVERFLOW (tem))
12206         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12207
12208       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
12209       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12210           && TREE_CODE (arg1) == INTEGER_CST
12211           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12212         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12213                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12214                                          fold_convert_loc (loc,
12215                                                            TREE_TYPE (arg0),
12216                                                            arg1),
12217                                          TREE_OPERAND (arg0, 1)));
12218
12219       /* Transform comparisons of the form X +- Y CMP X to Y CMP 0.  */
12220       if ((TREE_CODE (arg0) == PLUS_EXPR
12221            || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12222            || TREE_CODE (arg0) == MINUS_EXPR)
12223           && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12224                                                                         0)),
12225                               arg1, 0)
12226           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12227               || POINTER_TYPE_P (TREE_TYPE (arg0))))
12228         {
12229           tree val = TREE_OPERAND (arg0, 1);
12230           return omit_two_operands_loc (loc, type,
12231                                     fold_build2_loc (loc, code, type,
12232                                                  val,
12233                                                  build_int_cst (TREE_TYPE (val),
12234                                                                 0)),
12235                                     TREE_OPERAND (arg0, 0), arg1);
12236         }
12237
12238       /* Transform comparisons of the form C - X CMP X if C % 2 == 1.  */
12239       if (TREE_CODE (arg0) == MINUS_EXPR
12240           && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12241           && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12242                                                                         1)),
12243                               arg1, 0)
12244           && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12245         {
12246           return omit_two_operands_loc (loc, type,
12247                                     code == NE_EXPR
12248                                     ? boolean_true_node : boolean_false_node,
12249                                     TREE_OPERAND (arg0, 1), arg1);
12250         }
12251
12252       /* If we have X - Y == 0, we can convert that to X == Y and similarly
12253          for !=.  Don't do this for ordered comparisons due to overflow.  */
12254       if (TREE_CODE (arg0) == MINUS_EXPR
12255           && integer_zerop (arg1))
12256         return fold_build2_loc (loc, code, type,
12257                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12258
12259       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
12260       if (TREE_CODE (arg0) == ABS_EXPR
12261           && (integer_zerop (arg1) || real_zerop (arg1)))
12262         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12263
12264       /* If this is an EQ or NE comparison with zero and ARG0 is
12265          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
12266          two operations, but the latter can be done in one less insn
12267          on machines that have only two-operand insns or on which a
12268          constant cannot be the first operand.  */
12269       if (TREE_CODE (arg0) == BIT_AND_EXPR
12270           && integer_zerop (arg1))
12271         {
12272           tree arg00 = TREE_OPERAND (arg0, 0);
12273           tree arg01 = TREE_OPERAND (arg0, 1);
12274           if (TREE_CODE (arg00) == LSHIFT_EXPR
12275               && integer_onep (TREE_OPERAND (arg00, 0)))
12276             {
12277               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12278                                       arg01, TREE_OPERAND (arg00, 1));
12279               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12280                                  build_int_cst (TREE_TYPE (arg0), 1));
12281               return fold_build2_loc (loc, code, type,
12282                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12283                                   arg1);
12284             }
12285           else if (TREE_CODE (arg01) == LSHIFT_EXPR
12286                    && integer_onep (TREE_OPERAND (arg01, 0)))
12287             {
12288               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12289                                       arg00, TREE_OPERAND (arg01, 1));
12290               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12291                                  build_int_cst (TREE_TYPE (arg0), 1));
12292               return fold_build2_loc (loc, code, type,
12293                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12294                                   arg1);
12295             }
12296         }
12297
12298       /* If this is an NE or EQ comparison of zero against the result of a
12299          signed MOD operation whose second operand is a power of 2, make
12300          the MOD operation unsigned since it is simpler and equivalent.  */
12301       if (integer_zerop (arg1)
12302           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12303           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12304               || TREE_CODE (arg0) == CEIL_MOD_EXPR
12305               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12306               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12307           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12308         {
12309           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12310           tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12311                                      fold_convert_loc (loc, newtype,
12312                                                        TREE_OPERAND (arg0, 0)),
12313                                      fold_convert_loc (loc, newtype,
12314                                                        TREE_OPERAND (arg0, 1)));
12315
12316           return fold_build2_loc (loc, code, type, newmod,
12317                               fold_convert_loc (loc, newtype, arg1));
12318         }
12319
12320       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12321          C1 is a valid shift constant, and C2 is a power of two, i.e.
12322          a single bit.  */
12323       if (TREE_CODE (arg0) == BIT_AND_EXPR
12324           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12325           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12326              == INTEGER_CST
12327           && integer_pow2p (TREE_OPERAND (arg0, 1))
12328           && integer_zerop (arg1))
12329         {
12330           tree itype = TREE_TYPE (arg0);
12331           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12332           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12333
12334           /* Check for a valid shift count.  */
12335           if (TREE_INT_CST_HIGH (arg001) == 0
12336               && TREE_INT_CST_LOW (arg001) < prec)
12337             {
12338               tree arg01 = TREE_OPERAND (arg0, 1);
12339               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12340               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12341               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12342                  can be rewritten as (X & (C2 << C1)) != 0.  */
12343               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12344                 {
12345                   tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12346                   tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12347                   return fold_build2_loc (loc, code, type, tem,
12348                                           fold_convert_loc (loc, itype, arg1));
12349                 }
12350               /* Otherwise, for signed (arithmetic) shifts,
12351                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12352                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
12353               else if (!TYPE_UNSIGNED (itype))
12354                 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12355                                     arg000, build_int_cst (itype, 0));
12356               /* Otherwise, of unsigned (logical) shifts,
12357                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12358                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
12359               else
12360                 return omit_one_operand_loc (loc, type,
12361                                          code == EQ_EXPR ? integer_one_node
12362                                                          : integer_zero_node,
12363                                          arg000);
12364             }
12365         }
12366
12367       /* If this is an NE comparison of zero with an AND of one, remove the
12368          comparison since the AND will give the correct value.  */
12369       if (code == NE_EXPR
12370           && integer_zerop (arg1)
12371           && TREE_CODE (arg0) == BIT_AND_EXPR
12372           && integer_onep (TREE_OPERAND (arg0, 1)))
12373         return fold_convert_loc (loc, type, arg0);
12374
12375       /* If we have (A & C) == C where C is a power of 2, convert this into
12376          (A & C) != 0.  Similarly for NE_EXPR.  */
12377       if (TREE_CODE (arg0) == BIT_AND_EXPR
12378           && integer_pow2p (TREE_OPERAND (arg0, 1))
12379           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12380         return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12381                             arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12382                                                     integer_zero_node));
12383
12384       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12385          bit, then fold the expression into A < 0 or A >= 0.  */
12386       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12387       if (tem)
12388         return tem;
12389
12390       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12391          Similarly for NE_EXPR.  */
12392       if (TREE_CODE (arg0) == BIT_AND_EXPR
12393           && TREE_CODE (arg1) == INTEGER_CST
12394           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12395         {
12396           tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12397                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
12398                                    TREE_OPERAND (arg0, 1));
12399           tree dandnotc
12400             = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12401                                fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
12402                                notc);
12403           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12404           if (integer_nonzerop (dandnotc))
12405             return omit_one_operand_loc (loc, type, rslt, arg0);
12406         }
12407
12408       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12409          Similarly for NE_EXPR.  */
12410       if (TREE_CODE (arg0) == BIT_IOR_EXPR
12411           && TREE_CODE (arg1) == INTEGER_CST
12412           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12413         {
12414           tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12415           tree candnotd
12416             = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12417                                TREE_OPERAND (arg0, 1),
12418                                fold_convert_loc (loc, TREE_TYPE (arg0), notd));
12419           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12420           if (integer_nonzerop (candnotd))
12421             return omit_one_operand_loc (loc, type, rslt, arg0);
12422         }
12423
12424       /* If this is a comparison of a field, we may be able to simplify it.  */
12425       if ((TREE_CODE (arg0) == COMPONENT_REF
12426            || TREE_CODE (arg0) == BIT_FIELD_REF)
12427           /* Handle the constant case even without -O
12428              to make sure the warnings are given.  */
12429           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12430         {
12431           t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12432           if (t1)
12433             return t1;
12434         }
12435
12436       /* Optimize comparisons of strlen vs zero to a compare of the
12437          first character of the string vs zero.  To wit,
12438                 strlen(ptr) == 0   =>  *ptr == 0
12439                 strlen(ptr) != 0   =>  *ptr != 0
12440          Other cases should reduce to one of these two (or a constant)
12441          due to the return value of strlen being unsigned.  */
12442       if (TREE_CODE (arg0) == CALL_EXPR
12443           && integer_zerop (arg1))
12444         {
12445           tree fndecl = get_callee_fndecl (arg0);
12446
12447           if (fndecl
12448               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12449               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12450               && call_expr_nargs (arg0) == 1
12451               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12452             {
12453               tree iref = build_fold_indirect_ref_loc (loc,
12454                                                    CALL_EXPR_ARG (arg0, 0));
12455               return fold_build2_loc (loc, code, type, iref,
12456                                   build_int_cst (TREE_TYPE (iref), 0));
12457             }
12458         }
12459
12460       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12461          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12462       if (TREE_CODE (arg0) == RSHIFT_EXPR
12463           && integer_zerop (arg1)
12464           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12465         {
12466           tree arg00 = TREE_OPERAND (arg0, 0);
12467           tree arg01 = TREE_OPERAND (arg0, 1);
12468           tree itype = TREE_TYPE (arg00);
12469           if (TREE_INT_CST_HIGH (arg01) == 0
12470               && TREE_INT_CST_LOW (arg01)
12471                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12472             {
12473               if (TYPE_UNSIGNED (itype))
12474                 {
12475                   itype = signed_type_for (itype);
12476                   arg00 = fold_convert_loc (loc, itype, arg00);
12477                 }
12478               return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12479                                   type, arg00, build_int_cst (itype, 0));
12480             }
12481         }
12482
12483       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12484       if (integer_zerop (arg1)
12485           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12486         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12487                             TREE_OPERAND (arg0, 1));
12488
12489       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12490       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12491           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12492         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12493                                 build_int_cst (TREE_TYPE (arg0), 0));
12494       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12495       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12496           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12497           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12498         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12499                                 build_int_cst (TREE_TYPE (arg0), 0));
12500
12501       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12502       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12503           && TREE_CODE (arg1) == INTEGER_CST
12504           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12505         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12506                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12507                                          TREE_OPERAND (arg0, 1), arg1));
12508
12509       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12510          (X & C) == 0 when C is a single bit.  */
12511       if (TREE_CODE (arg0) == BIT_AND_EXPR
12512           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12513           && integer_zerop (arg1)
12514           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12515         {
12516           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12517                                  TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12518                                  TREE_OPERAND (arg0, 1));
12519           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12520                                   type, tem,
12521                                   fold_convert_loc (loc, TREE_TYPE (arg0),
12522                                                     arg1));
12523         }
12524
12525       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12526          constant C is a power of two, i.e. a single bit.  */
12527       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12528           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12529           && integer_zerop (arg1)
12530           && integer_pow2p (TREE_OPERAND (arg0, 1))
12531           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12532                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12533         {
12534           tree arg00 = TREE_OPERAND (arg0, 0);
12535           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12536                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
12537         }
12538
12539       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12540          when is C is a power of two, i.e. a single bit.  */
12541       if (TREE_CODE (arg0) == BIT_AND_EXPR
12542           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12543           && integer_zerop (arg1)
12544           && integer_pow2p (TREE_OPERAND (arg0, 1))
12545           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12546                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12547         {
12548           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12549           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12550                              arg000, TREE_OPERAND (arg0, 1));
12551           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12552                               tem, build_int_cst (TREE_TYPE (tem), 0));
12553         }
12554
12555       if (integer_zerop (arg1)
12556           && tree_expr_nonzero_p (arg0))
12557         {
12558           tree res = constant_boolean_node (code==NE_EXPR, type);
12559           return omit_one_operand_loc (loc, type, res, arg0);
12560         }
12561
12562       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
12563       if (TREE_CODE (arg0) == NEGATE_EXPR
12564           && TREE_CODE (arg1) == NEGATE_EXPR)
12565         return fold_build2_loc (loc, code, type,
12566                                 TREE_OPERAND (arg0, 0),
12567                                 fold_convert_loc (loc, TREE_TYPE (arg0),
12568                                                   TREE_OPERAND (arg1, 0)));
12569
12570       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
12571       if (TREE_CODE (arg0) == BIT_AND_EXPR
12572           && TREE_CODE (arg1) == BIT_AND_EXPR)
12573         {
12574           tree arg00 = TREE_OPERAND (arg0, 0);
12575           tree arg01 = TREE_OPERAND (arg0, 1);
12576           tree arg10 = TREE_OPERAND (arg1, 0);
12577           tree arg11 = TREE_OPERAND (arg1, 1);
12578           tree itype = TREE_TYPE (arg0);
12579
12580           if (operand_equal_p (arg01, arg11, 0))
12581             return fold_build2_loc (loc, code, type,
12582                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12583                                              fold_build2_loc (loc,
12584                                                           BIT_XOR_EXPR, itype,
12585                                                           arg00, arg10),
12586                                              arg01),
12587                                 build_int_cst (itype, 0));
12588
12589           if (operand_equal_p (arg01, arg10, 0))
12590             return fold_build2_loc (loc, code, type,
12591                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12592                                              fold_build2_loc (loc,
12593                                                           BIT_XOR_EXPR, itype,
12594                                                           arg00, arg11),
12595                                              arg01),
12596                                 build_int_cst (itype, 0));
12597
12598           if (operand_equal_p (arg00, arg11, 0))
12599             return fold_build2_loc (loc, code, type,
12600                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12601                                              fold_build2_loc (loc,
12602                                                           BIT_XOR_EXPR, itype,
12603                                                           arg01, arg10),
12604                                              arg00),
12605                                 build_int_cst (itype, 0));
12606
12607           if (operand_equal_p (arg00, arg10, 0))
12608             return fold_build2_loc (loc, code, type,
12609                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12610                                              fold_build2_loc (loc,
12611                                                           BIT_XOR_EXPR, itype,
12612                                                           arg01, arg11),
12613                                              arg00),
12614                                 build_int_cst (itype, 0));
12615         }
12616
12617       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12618           && TREE_CODE (arg1) == BIT_XOR_EXPR)
12619         {
12620           tree arg00 = TREE_OPERAND (arg0, 0);
12621           tree arg01 = TREE_OPERAND (arg0, 1);
12622           tree arg10 = TREE_OPERAND (arg1, 0);
12623           tree arg11 = TREE_OPERAND (arg1, 1);
12624           tree itype = TREE_TYPE (arg0);
12625
12626           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12627              operand_equal_p guarantees no side-effects so we don't need
12628              to use omit_one_operand on Z.  */
12629           if (operand_equal_p (arg01, arg11, 0))
12630             return fold_build2_loc (loc, code, type, arg00,
12631                                     fold_convert_loc (loc, TREE_TYPE (arg00),
12632                                                       arg10));
12633           if (operand_equal_p (arg01, arg10, 0))
12634             return fold_build2_loc (loc, code, type, arg00,
12635                                     fold_convert_loc (loc, TREE_TYPE (arg00),
12636                                                       arg11));
12637           if (operand_equal_p (arg00, arg11, 0))
12638             return fold_build2_loc (loc, code, type, arg01,
12639                                     fold_convert_loc (loc, TREE_TYPE (arg01),
12640                                                       arg10));
12641           if (operand_equal_p (arg00, arg10, 0))
12642             return fold_build2_loc (loc, code, type, arg01,
12643                                     fold_convert_loc (loc, TREE_TYPE (arg01),
12644                                                       arg11));
12645
12646           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
12647           if (TREE_CODE (arg01) == INTEGER_CST
12648               && TREE_CODE (arg11) == INTEGER_CST)
12649             {
12650               tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12651                                      fold_convert_loc (loc, itype, arg11));
12652               tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12653               return fold_build2_loc (loc, code, type, tem,
12654                                       fold_convert_loc (loc, itype, arg10));
12655             }
12656         }
12657
12658       /* Attempt to simplify equality/inequality comparisons of complex
12659          values.  Only lower the comparison if the result is known or
12660          can be simplified to a single scalar comparison.  */
12661       if ((TREE_CODE (arg0) == COMPLEX_EXPR
12662            || TREE_CODE (arg0) == COMPLEX_CST)
12663           && (TREE_CODE (arg1) == COMPLEX_EXPR
12664               || TREE_CODE (arg1) == COMPLEX_CST))
12665         {
12666           tree real0, imag0, real1, imag1;
12667           tree rcond, icond;
12668
12669           if (TREE_CODE (arg0) == COMPLEX_EXPR)
12670             {
12671               real0 = TREE_OPERAND (arg0, 0);
12672               imag0 = TREE_OPERAND (arg0, 1);
12673             }
12674           else
12675             {
12676               real0 = TREE_REALPART (arg0);
12677               imag0 = TREE_IMAGPART (arg0);
12678             }
12679
12680           if (TREE_CODE (arg1) == COMPLEX_EXPR)
12681             {
12682               real1 = TREE_OPERAND (arg1, 0);
12683               imag1 = TREE_OPERAND (arg1, 1);
12684             }
12685           else
12686             {
12687               real1 = TREE_REALPART (arg1);
12688               imag1 = TREE_IMAGPART (arg1);
12689             }
12690
12691           rcond = fold_binary_loc (loc, code, type, real0, real1);
12692           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12693             {
12694               if (integer_zerop (rcond))
12695                 {
12696                   if (code == EQ_EXPR)
12697                     return omit_two_operands_loc (loc, type, boolean_false_node,
12698                                               imag0, imag1);
12699                   return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12700                 }
12701               else
12702                 {
12703                   if (code == NE_EXPR)
12704                     return omit_two_operands_loc (loc, type, boolean_true_node,
12705                                               imag0, imag1);
12706                   return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12707                 }
12708             }
12709
12710           icond = fold_binary_loc (loc, code, type, imag0, imag1);
12711           if (icond && TREE_CODE (icond) == INTEGER_CST)
12712             {
12713               if (integer_zerop (icond))
12714                 {
12715                   if (code == EQ_EXPR)
12716                     return omit_two_operands_loc (loc, type, boolean_false_node,
12717                                               real0, real1);
12718                   return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12719                 }
12720               else
12721                 {
12722                   if (code == NE_EXPR)
12723                     return omit_two_operands_loc (loc, type, boolean_true_node,
12724                                               real0, real1);
12725                   return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12726                 }
12727             }
12728         }
12729
12730       return NULL_TREE;
12731
12732     case LT_EXPR:
12733     case GT_EXPR:
12734     case LE_EXPR:
12735     case GE_EXPR:
12736       tem = fold_comparison (loc, code, type, op0, op1);
12737       if (tem != NULL_TREE)
12738         return tem;
12739
12740       /* Transform comparisons of the form X +- C CMP X.  */
12741       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12742           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12743           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12744                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12745               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12746                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12747         {
12748           tree arg01 = TREE_OPERAND (arg0, 1);
12749           enum tree_code code0 = TREE_CODE (arg0);
12750           int is_positive;
12751
12752           if (TREE_CODE (arg01) == REAL_CST)
12753             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12754           else
12755             is_positive = tree_int_cst_sgn (arg01);
12756
12757           /* (X - c) > X becomes false.  */
12758           if (code == GT_EXPR
12759               && ((code0 == MINUS_EXPR && is_positive >= 0)
12760                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12761             {
12762               if (TREE_CODE (arg01) == INTEGER_CST
12763                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12764                 fold_overflow_warning (("assuming signed overflow does not "
12765                                         "occur when assuming that (X - c) > X "
12766                                         "is always false"),
12767                                        WARN_STRICT_OVERFLOW_ALL);
12768               return constant_boolean_node (0, type);
12769             }
12770
12771           /* Likewise (X + c) < X becomes false.  */
12772           if (code == LT_EXPR
12773               && ((code0 == PLUS_EXPR && is_positive >= 0)
12774                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12775             {
12776               if (TREE_CODE (arg01) == INTEGER_CST
12777                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12778                 fold_overflow_warning (("assuming signed overflow does not "
12779                                         "occur when assuming that "
12780                                         "(X + c) < X is always false"),
12781                                        WARN_STRICT_OVERFLOW_ALL);
12782               return constant_boolean_node (0, type);
12783             }
12784
12785           /* Convert (X - c) <= X to true.  */
12786           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12787               && code == LE_EXPR
12788               && ((code0 == MINUS_EXPR && is_positive >= 0)
12789                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12790             {
12791               if (TREE_CODE (arg01) == INTEGER_CST
12792                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12793                 fold_overflow_warning (("assuming signed overflow does not "
12794                                         "occur when assuming that "
12795                                         "(X - c) <= X is always true"),
12796                                        WARN_STRICT_OVERFLOW_ALL);
12797               return constant_boolean_node (1, type);
12798             }
12799
12800           /* Convert (X + c) >= X to true.  */
12801           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12802               && code == GE_EXPR
12803               && ((code0 == PLUS_EXPR && is_positive >= 0)
12804                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12805             {
12806               if (TREE_CODE (arg01) == INTEGER_CST
12807                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12808                 fold_overflow_warning (("assuming signed overflow does not "
12809                                         "occur when assuming that "
12810                                         "(X + c) >= X is always true"),
12811                                        WARN_STRICT_OVERFLOW_ALL);
12812               return constant_boolean_node (1, type);
12813             }
12814
12815           if (TREE_CODE (arg01) == INTEGER_CST)
12816             {
12817               /* Convert X + c > X and X - c < X to true for integers.  */
12818               if (code == GT_EXPR
12819                   && ((code0 == PLUS_EXPR && is_positive > 0)
12820                       || (code0 == MINUS_EXPR && is_positive < 0)))
12821                 {
12822                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12823                     fold_overflow_warning (("assuming signed overflow does "
12824                                             "not occur when assuming that "
12825                                             "(X + c) > X is always true"),
12826                                            WARN_STRICT_OVERFLOW_ALL);
12827                   return constant_boolean_node (1, type);
12828                 }
12829
12830               if (code == LT_EXPR
12831                   && ((code0 == MINUS_EXPR && is_positive > 0)
12832                       || (code0 == PLUS_EXPR && is_positive < 0)))
12833                 {
12834                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12835                     fold_overflow_warning (("assuming signed overflow does "
12836                                             "not occur when assuming that "
12837                                             "(X - c) < X is always true"),
12838                                            WARN_STRICT_OVERFLOW_ALL);
12839                   return constant_boolean_node (1, type);
12840                 }
12841
12842               /* Convert X + c <= X and X - c >= X to false for integers.  */
12843               if (code == LE_EXPR
12844                   && ((code0 == PLUS_EXPR && is_positive > 0)
12845                       || (code0 == MINUS_EXPR && is_positive < 0)))
12846                 {
12847                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12848                     fold_overflow_warning (("assuming signed overflow does "
12849                                             "not occur when assuming that "
12850                                             "(X + c) <= X is always false"),
12851                                            WARN_STRICT_OVERFLOW_ALL);
12852                   return constant_boolean_node (0, type);
12853                 }
12854
12855               if (code == GE_EXPR
12856                   && ((code0 == MINUS_EXPR && is_positive > 0)
12857                       || (code0 == PLUS_EXPR && is_positive < 0)))
12858                 {
12859                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12860                     fold_overflow_warning (("assuming signed overflow does "
12861                                             "not occur when assuming that "
12862                                             "(X - c) >= X is always false"),
12863                                            WARN_STRICT_OVERFLOW_ALL);
12864                   return constant_boolean_node (0, type);
12865                 }
12866             }
12867         }
12868
12869       /* Comparisons with the highest or lowest possible integer of
12870          the specified precision will have known values.  */
12871       {
12872         tree arg1_type = TREE_TYPE (arg1);
12873         unsigned int width = TYPE_PRECISION (arg1_type);
12874
12875         if (TREE_CODE (arg1) == INTEGER_CST
12876             && width <= 2 * HOST_BITS_PER_WIDE_INT
12877             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12878           {
12879             HOST_WIDE_INT signed_max_hi;
12880             unsigned HOST_WIDE_INT signed_max_lo;
12881             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12882
12883             if (width <= HOST_BITS_PER_WIDE_INT)
12884               {
12885                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12886                                 - 1;
12887                 signed_max_hi = 0;
12888                 max_hi = 0;
12889
12890                 if (TYPE_UNSIGNED (arg1_type))
12891                   {
12892                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12893                     min_lo = 0;
12894                     min_hi = 0;
12895                   }
12896                 else
12897                   {
12898                     max_lo = signed_max_lo;
12899                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12900                     min_hi = -1;
12901                   }
12902               }
12903             else
12904               {
12905                 width -= HOST_BITS_PER_WIDE_INT;
12906                 signed_max_lo = -1;
12907                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12908                                 - 1;
12909                 max_lo = -1;
12910                 min_lo = 0;
12911
12912                 if (TYPE_UNSIGNED (arg1_type))
12913                   {
12914                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12915                     min_hi = 0;
12916                   }
12917                 else
12918                   {
12919                     max_hi = signed_max_hi;
12920                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12921                   }
12922               }
12923
12924             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12925                 && TREE_INT_CST_LOW (arg1) == max_lo)
12926               switch (code)
12927                 {
12928                 case GT_EXPR:
12929                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12930
12931                 case GE_EXPR:
12932                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12933
12934                 case LE_EXPR:
12935                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12936
12937                 case LT_EXPR:
12938                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12939
12940                 /* The GE_EXPR and LT_EXPR cases above are not normally
12941                    reached because of previous transformations.  */
12942
12943                 default:
12944                   break;
12945                 }
12946             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12947                      == max_hi
12948                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12949               switch (code)
12950                 {
12951                 case GT_EXPR:
12952                   arg1 = const_binop (PLUS_EXPR, arg1,
12953                                       build_int_cst (TREE_TYPE (arg1), 1));
12954                   return fold_build2_loc (loc, EQ_EXPR, type,
12955                                       fold_convert_loc (loc,
12956                                                         TREE_TYPE (arg1), arg0),
12957                                       arg1);
12958                 case LE_EXPR:
12959                   arg1 = const_binop (PLUS_EXPR, arg1,
12960                                       build_int_cst (TREE_TYPE (arg1), 1));
12961                   return fold_build2_loc (loc, NE_EXPR, type,
12962                                       fold_convert_loc (loc, TREE_TYPE (arg1),
12963                                                         arg0),
12964                                       arg1);
12965                 default:
12966                   break;
12967                 }
12968             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12969                      == min_hi
12970                      && TREE_INT_CST_LOW (arg1) == min_lo)
12971               switch (code)
12972                 {
12973                 case LT_EXPR:
12974                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12975
12976                 case LE_EXPR:
12977                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12978
12979                 case GE_EXPR:
12980                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12981
12982                 case GT_EXPR:
12983                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12984
12985                 default:
12986                   break;
12987                 }
12988             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12989                      == min_hi
12990                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12991               switch (code)
12992                 {
12993                 case GE_EXPR:
12994                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
12995                   return fold_build2_loc (loc, NE_EXPR, type,
12996                                       fold_convert_loc (loc,
12997                                                         TREE_TYPE (arg1), arg0),
12998                                       arg1);
12999                 case LT_EXPR:
13000                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
13001                   return fold_build2_loc (loc, EQ_EXPR, type,
13002                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13003                                                         arg0),
13004                                       arg1);
13005                 default:
13006                   break;
13007                 }
13008
13009             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13010                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
13011                      && TYPE_UNSIGNED (arg1_type)
13012                      /* We will flip the signedness of the comparison operator
13013                         associated with the mode of arg1, so the sign bit is
13014                         specified by this mode.  Check that arg1 is the signed
13015                         max associated with this sign bit.  */
13016                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13017                      /* signed_type does not work on pointer types.  */
13018                      && INTEGRAL_TYPE_P (arg1_type))
13019               {
13020                 /* The following case also applies to X < signed_max+1
13021                    and X >= signed_max+1 because previous transformations.  */
13022                 if (code == LE_EXPR || code == GT_EXPR)
13023                   {
13024                     tree st;
13025                     st = signed_type_for (TREE_TYPE (arg1));
13026                     return fold_build2_loc (loc,
13027                                         code == LE_EXPR ? GE_EXPR : LT_EXPR,
13028                                         type, fold_convert_loc (loc, st, arg0),
13029                                         build_int_cst (st, 0));
13030                   }
13031               }
13032           }
13033       }
13034
13035       /* If we are comparing an ABS_EXPR with a constant, we can
13036          convert all the cases into explicit comparisons, but they may
13037          well not be faster than doing the ABS and one comparison.
13038          But ABS (X) <= C is a range comparison, which becomes a subtraction
13039          and a comparison, and is probably faster.  */
13040       if (code == LE_EXPR
13041           && TREE_CODE (arg1) == INTEGER_CST
13042           && TREE_CODE (arg0) == ABS_EXPR
13043           && ! TREE_SIDE_EFFECTS (arg0)
13044           && (0 != (tem = negate_expr (arg1)))
13045           && TREE_CODE (tem) == INTEGER_CST
13046           && !TREE_OVERFLOW (tem))
13047         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13048                             build2 (GE_EXPR, type,
13049                                     TREE_OPERAND (arg0, 0), tem),
13050                             build2 (LE_EXPR, type,
13051                                     TREE_OPERAND (arg0, 0), arg1));
13052
13053       /* Convert ABS_EXPR<x> >= 0 to true.  */
13054       strict_overflow_p = false;
13055       if (code == GE_EXPR
13056           && (integer_zerop (arg1)
13057               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13058                   && real_zerop (arg1)))
13059           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13060         {
13061           if (strict_overflow_p)
13062             fold_overflow_warning (("assuming signed overflow does not occur "
13063                                     "when simplifying comparison of "
13064                                     "absolute value and zero"),
13065                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13066           return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13067         }
13068
13069       /* Convert ABS_EXPR<x> < 0 to false.  */
13070       strict_overflow_p = false;
13071       if (code == LT_EXPR
13072           && (integer_zerop (arg1) || real_zerop (arg1))
13073           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13074         {
13075           if (strict_overflow_p)
13076             fold_overflow_warning (("assuming signed overflow does not occur "
13077                                     "when simplifying comparison of "
13078                                     "absolute value and zero"),
13079                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13080           return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13081         }
13082
13083       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13084          and similarly for >= into !=.  */
13085       if ((code == LT_EXPR || code == GE_EXPR)
13086           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13087           && TREE_CODE (arg1) == LSHIFT_EXPR
13088           && integer_onep (TREE_OPERAND (arg1, 0)))
13089         return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13090                            build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13091                                    TREE_OPERAND (arg1, 1)),
13092                            build_int_cst (TREE_TYPE (arg0), 0));
13093
13094       if ((code == LT_EXPR || code == GE_EXPR)
13095           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13096           && CONVERT_EXPR_P (arg1)
13097           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13098           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13099         {
13100           tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13101                         TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
13102           return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13103                              fold_convert_loc (loc, TREE_TYPE (arg0), tem),
13104                              build_int_cst (TREE_TYPE (arg0), 0));
13105         }
13106
13107       return NULL_TREE;
13108
13109     case UNORDERED_EXPR:
13110     case ORDERED_EXPR:
13111     case UNLT_EXPR:
13112     case UNLE_EXPR:
13113     case UNGT_EXPR:
13114     case UNGE_EXPR:
13115     case UNEQ_EXPR:
13116     case LTGT_EXPR:
13117       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13118         {
13119           t1 = fold_relational_const (code, type, arg0, arg1);
13120           if (t1 != NULL_TREE)
13121             return t1;
13122         }
13123
13124       /* If the first operand is NaN, the result is constant.  */
13125       if (TREE_CODE (arg0) == REAL_CST
13126           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13127           && (code != LTGT_EXPR || ! flag_trapping_math))
13128         {
13129           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13130                ? integer_zero_node
13131                : integer_one_node;
13132           return omit_one_operand_loc (loc, type, t1, arg1);
13133         }
13134
13135       /* If the second operand is NaN, the result is constant.  */
13136       if (TREE_CODE (arg1) == REAL_CST
13137           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13138           && (code != LTGT_EXPR || ! flag_trapping_math))
13139         {
13140           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13141                ? integer_zero_node
13142                : integer_one_node;
13143           return omit_one_operand_loc (loc, type, t1, arg0);
13144         }
13145
13146       /* Simplify unordered comparison of something with itself.  */
13147       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13148           && operand_equal_p (arg0, arg1, 0))
13149         return constant_boolean_node (1, type);
13150
13151       if (code == LTGT_EXPR
13152           && !flag_trapping_math
13153           && operand_equal_p (arg0, arg1, 0))
13154         return constant_boolean_node (0, type);
13155
13156       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
13157       {
13158         tree targ0 = strip_float_extensions (arg0);
13159         tree targ1 = strip_float_extensions (arg1);
13160         tree newtype = TREE_TYPE (targ0);
13161
13162         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13163           newtype = TREE_TYPE (targ1);
13164
13165         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13166           return fold_build2_loc (loc, code, type,
13167                               fold_convert_loc (loc, newtype, targ0),
13168                               fold_convert_loc (loc, newtype, targ1));
13169       }
13170
13171       return NULL_TREE;
13172
13173     case COMPOUND_EXPR:
13174       /* When pedantic, a compound expression can be neither an lvalue
13175          nor an integer constant expression.  */
13176       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13177         return NULL_TREE;
13178       /* Don't let (0, 0) be null pointer constant.  */
13179       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13180                                  : fold_convert_loc (loc, type, arg1);
13181       return pedantic_non_lvalue_loc (loc, tem);
13182
13183     case COMPLEX_EXPR:
13184       if ((TREE_CODE (arg0) == REAL_CST
13185            && TREE_CODE (arg1) == REAL_CST)
13186           || (TREE_CODE (arg0) == INTEGER_CST
13187               && TREE_CODE (arg1) == INTEGER_CST))
13188         return build_complex (type, arg0, arg1);
13189       if (TREE_CODE (arg0) == REALPART_EXPR
13190           && TREE_CODE (arg1) == IMAGPART_EXPR
13191           && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg0, 0)))
13192               == TYPE_MAIN_VARIANT (type))
13193           && operand_equal_p (TREE_OPERAND (arg0, 0),
13194                               TREE_OPERAND (arg1, 0), 0))
13195         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
13196                                      TREE_OPERAND (arg1, 0));
13197       return NULL_TREE;
13198
13199     case ASSERT_EXPR:
13200       /* An ASSERT_EXPR should never be passed to fold_binary.  */
13201       gcc_unreachable ();
13202
13203     default:
13204       return NULL_TREE;
13205     } /* switch (code) */
13206 }
13207
13208 /* Callback for walk_tree, looking for LABEL_EXPR.  Return *TP if it is
13209    a LABEL_EXPR; otherwise return NULL_TREE.  Do not check the subtrees
13210    of GOTO_EXPR.  */
13211
13212 static tree
13213 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13214 {
13215   switch (TREE_CODE (*tp))
13216     {
13217     case LABEL_EXPR:
13218       return *tp;
13219
13220     case GOTO_EXPR:
13221       *walk_subtrees = 0;
13222
13223       /* ... fall through ...  */
13224
13225     default:
13226       return NULL_TREE;
13227     }
13228 }
13229
13230 /* Return whether the sub-tree ST contains a label which is accessible from
13231    outside the sub-tree.  */
13232
13233 static bool
13234 contains_label_p (tree st)
13235 {
13236   return
13237    (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13238 }
13239
13240 /* Fold a ternary expression of code CODE and type TYPE with operands
13241    OP0, OP1, and OP2.  Return the folded expression if folding is
13242    successful.  Otherwise, return NULL_TREE.  */
13243
13244 tree
13245 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13246                   tree op0, tree op1, tree op2)
13247 {
13248   tree tem;
13249   tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
13250   enum tree_code_class kind = TREE_CODE_CLASS (code);
13251
13252   gcc_assert (IS_EXPR_CODE_CLASS (kind)
13253               && TREE_CODE_LENGTH (code) == 3);
13254
13255   /* Strip any conversions that don't change the mode.  This is safe
13256      for every expression, except for a comparison expression because
13257      its signedness is derived from its operands.  So, in the latter
13258      case, only strip conversions that don't change the signedness.
13259
13260      Note that this is done as an internal manipulation within the
13261      constant folder, in order to find the simplest representation of
13262      the arguments so that their form can be studied.  In any cases,
13263      the appropriate type conversions should be put back in the tree
13264      that will get out of the constant folder.  */
13265   if (op0)
13266     {
13267       arg0 = op0;
13268       STRIP_NOPS (arg0);
13269     }
13270
13271   if (op1)
13272     {
13273       arg1 = op1;
13274       STRIP_NOPS (arg1);
13275     }
13276
13277   if (op2)
13278     {
13279       arg2 = op2;
13280       STRIP_NOPS (arg2);
13281     }
13282
13283   switch (code)
13284     {
13285     case COMPONENT_REF:
13286       if (TREE_CODE (arg0) == CONSTRUCTOR
13287           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13288         {
13289           unsigned HOST_WIDE_INT idx;
13290           tree field, value;
13291           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13292             if (field == arg1)
13293               return value;
13294         }
13295       return NULL_TREE;
13296
13297     case COND_EXPR:
13298       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13299          so all simple results must be passed through pedantic_non_lvalue.  */
13300       if (TREE_CODE (arg0) == INTEGER_CST)
13301         {
13302           tree unused_op = integer_zerop (arg0) ? op1 : op2;
13303           tem = integer_zerop (arg0) ? op2 : op1;
13304           /* Only optimize constant conditions when the selected branch
13305              has the same type as the COND_EXPR.  This avoids optimizing
13306              away "c ? x : throw", where the throw has a void type.
13307              Avoid throwing away that operand which contains label.  */
13308           if ((!TREE_SIDE_EFFECTS (unused_op)
13309                || !contains_label_p (unused_op))
13310               && (! VOID_TYPE_P (TREE_TYPE (tem))
13311                   || VOID_TYPE_P (type)))
13312             return pedantic_non_lvalue_loc (loc, tem);
13313           return NULL_TREE;
13314         }
13315       if (operand_equal_p (arg1, op2, 0))
13316         return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13317
13318       /* If we have A op B ? A : C, we may be able to convert this to a
13319          simpler expression, depending on the operation and the values
13320          of B and C.  Signed zeros prevent all of these transformations,
13321          for reasons given above each one.
13322
13323          Also try swapping the arguments and inverting the conditional.  */
13324       if (COMPARISON_CLASS_P (arg0)
13325           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13326                                              arg1, TREE_OPERAND (arg0, 1))
13327           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13328         {
13329           tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13330           if (tem)
13331             return tem;
13332         }
13333
13334       if (COMPARISON_CLASS_P (arg0)
13335           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13336                                              op2,
13337                                              TREE_OPERAND (arg0, 1))
13338           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13339         {
13340           location_t loc0 = EXPR_LOCATION (arg0);
13341           if (loc0 == UNKNOWN_LOCATION)
13342             loc0 = loc;
13343           tem = fold_truth_not_expr (loc0, arg0);
13344           if (tem && COMPARISON_CLASS_P (tem))
13345             {
13346               tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13347               if (tem)
13348                 return tem;
13349             }
13350         }
13351
13352       /* If the second operand is simpler than the third, swap them
13353          since that produces better jump optimization results.  */
13354       if (truth_value_p (TREE_CODE (arg0))
13355           && tree_swap_operands_p (op1, op2, false))
13356         {
13357           location_t loc0 = EXPR_LOCATION (arg0);
13358           if (loc0 == UNKNOWN_LOCATION)
13359             loc0 = loc;
13360           /* See if this can be inverted.  If it can't, possibly because
13361              it was a floating-point inequality comparison, don't do
13362              anything.  */
13363           tem = fold_truth_not_expr (loc0, arg0);
13364           if (tem)
13365             return fold_build3_loc (loc, code, type, tem, op2, op1);
13366         }
13367
13368       /* Convert A ? 1 : 0 to simply A.  */
13369       if (integer_onep (op1)
13370           && integer_zerop (op2)
13371           /* If we try to convert OP0 to our type, the
13372              call to fold will try to move the conversion inside
13373              a COND, which will recurse.  In that case, the COND_EXPR
13374              is probably the best choice, so leave it alone.  */
13375           && type == TREE_TYPE (arg0))
13376         return pedantic_non_lvalue_loc (loc, arg0);
13377
13378       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
13379          over COND_EXPR in cases such as floating point comparisons.  */
13380       if (integer_zerop (op1)
13381           && integer_onep (op2)
13382           && truth_value_p (TREE_CODE (arg0)))
13383         return pedantic_non_lvalue_loc (loc,
13384                                     fold_convert_loc (loc, type,
13385                                               invert_truthvalue_loc (loc,
13386                                                                      arg0)));
13387
13388       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
13389       if (TREE_CODE (arg0) == LT_EXPR
13390           && integer_zerop (TREE_OPERAND (arg0, 1))
13391           && integer_zerop (op2)
13392           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13393         {
13394           /* sign_bit_p only checks ARG1 bits within A's precision.
13395              If <sign bit of A> has wider type than A, bits outside
13396              of A's precision in <sign bit of A> need to be checked.
13397              If they are all 0, this optimization needs to be done
13398              in unsigned A's type, if they are all 1 in signed A's type,
13399              otherwise this can't be done.  */
13400           if (TYPE_PRECISION (TREE_TYPE (tem))
13401               < TYPE_PRECISION (TREE_TYPE (arg1))
13402               && TYPE_PRECISION (TREE_TYPE (tem))
13403                  < TYPE_PRECISION (type))
13404             {
13405               unsigned HOST_WIDE_INT mask_lo;
13406               HOST_WIDE_INT mask_hi;
13407               int inner_width, outer_width;
13408               tree tem_type;
13409
13410               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13411               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13412               if (outer_width > TYPE_PRECISION (type))
13413                 outer_width = TYPE_PRECISION (type);
13414
13415               if (outer_width > HOST_BITS_PER_WIDE_INT)
13416                 {
13417                   mask_hi = ((unsigned HOST_WIDE_INT) -1
13418                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13419                   mask_lo = -1;
13420                 }
13421               else
13422                 {
13423                   mask_hi = 0;
13424                   mask_lo = ((unsigned HOST_WIDE_INT) -1
13425                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
13426                 }
13427               if (inner_width > HOST_BITS_PER_WIDE_INT)
13428                 {
13429                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13430                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
13431                   mask_lo = 0;
13432                 }
13433               else
13434                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13435                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
13436
13437               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13438                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13439                 {
13440                   tem_type = signed_type_for (TREE_TYPE (tem));
13441                   tem = fold_convert_loc (loc, tem_type, tem);
13442                 }
13443               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13444                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13445                 {
13446                   tem_type = unsigned_type_for (TREE_TYPE (tem));
13447                   tem = fold_convert_loc (loc, tem_type, tem);
13448                 }
13449               else
13450                 tem = NULL;
13451             }
13452
13453           if (tem)
13454             return
13455               fold_convert_loc (loc, type,
13456                                 fold_build2_loc (loc, BIT_AND_EXPR,
13457                                              TREE_TYPE (tem), tem,
13458                                              fold_convert_loc (loc,
13459                                                                TREE_TYPE (tem),
13460                                                                arg1)));
13461         }
13462
13463       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
13464          already handled above.  */
13465       if (TREE_CODE (arg0) == BIT_AND_EXPR
13466           && integer_onep (TREE_OPERAND (arg0, 1))
13467           && integer_zerop (op2)
13468           && integer_pow2p (arg1))
13469         {
13470           tree tem = TREE_OPERAND (arg0, 0);
13471           STRIP_NOPS (tem);
13472           if (TREE_CODE (tem) == RSHIFT_EXPR
13473               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13474               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13475                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13476             return fold_build2_loc (loc, BIT_AND_EXPR, type,
13477                                 TREE_OPERAND (tem, 0), arg1);
13478         }
13479
13480       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13481          is probably obsolete because the first operand should be a
13482          truth value (that's why we have the two cases above), but let's
13483          leave it in until we can confirm this for all front-ends.  */
13484       if (integer_zerop (op2)
13485           && TREE_CODE (arg0) == NE_EXPR
13486           && integer_zerop (TREE_OPERAND (arg0, 1))
13487           && integer_pow2p (arg1)
13488           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13489           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13490                               arg1, OEP_ONLY_CONST))
13491         return pedantic_non_lvalue_loc (loc,
13492                                     fold_convert_loc (loc, type,
13493                                                       TREE_OPERAND (arg0, 0)));
13494
13495       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13496       if (integer_zerop (op2)
13497           && truth_value_p (TREE_CODE (arg0))
13498           && truth_value_p (TREE_CODE (arg1)))
13499         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13500                             fold_convert_loc (loc, type, arg0),
13501                             arg1);
13502
13503       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13504       if (integer_onep (op2)
13505           && truth_value_p (TREE_CODE (arg0))
13506           && truth_value_p (TREE_CODE (arg1)))
13507         {
13508           location_t loc0 = EXPR_LOCATION (arg0);
13509           if (loc0 == UNKNOWN_LOCATION)
13510             loc0 = loc;
13511           /* Only perform transformation if ARG0 is easily inverted.  */
13512           tem = fold_truth_not_expr (loc0, arg0);
13513           if (tem)
13514             return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13515                                 fold_convert_loc (loc, type, tem),
13516                                 arg1);
13517         }
13518
13519       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13520       if (integer_zerop (arg1)
13521           && truth_value_p (TREE_CODE (arg0))
13522           && truth_value_p (TREE_CODE (op2)))
13523         {
13524           location_t loc0 = EXPR_LOCATION (arg0);
13525           if (loc0 == UNKNOWN_LOCATION)
13526             loc0 = loc;
13527           /* Only perform transformation if ARG0 is easily inverted.  */
13528           tem = fold_truth_not_expr (loc0, arg0);
13529           if (tem)
13530             return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13531                                 fold_convert_loc (loc, type, tem),
13532                                 op2);
13533         }
13534
13535       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13536       if (integer_onep (arg1)
13537           && truth_value_p (TREE_CODE (arg0))
13538           && truth_value_p (TREE_CODE (op2)))
13539         return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13540                             fold_convert_loc (loc, type, arg0),
13541                             op2);
13542
13543       return NULL_TREE;
13544
13545     case CALL_EXPR:
13546       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
13547          of fold_ternary on them.  */
13548       gcc_unreachable ();
13549
13550     case BIT_FIELD_REF:
13551       if ((TREE_CODE (arg0) == VECTOR_CST
13552            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13553           && type == TREE_TYPE (TREE_TYPE (arg0)))
13554         {
13555           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13556           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13557
13558           if (width != 0
13559               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13560               && (idx % width) == 0
13561               && (idx = idx / width)
13562                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13563             {
13564               tree elements = NULL_TREE;
13565
13566               if (TREE_CODE (arg0) == VECTOR_CST)
13567                 elements = TREE_VECTOR_CST_ELTS (arg0);
13568               else
13569                 {
13570                   unsigned HOST_WIDE_INT idx;
13571                   tree value;
13572
13573                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13574                     elements = tree_cons (NULL_TREE, value, elements);
13575                 }
13576               while (idx-- > 0 && elements)
13577                 elements = TREE_CHAIN (elements);
13578               if (elements)
13579                 return TREE_VALUE (elements);
13580               else
13581                 return build_zero_cst (type);
13582             }
13583         }
13584
13585       /* A bit-field-ref that referenced the full argument can be stripped.  */
13586       if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13587           && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13588           && integer_zerop (op2))
13589         return fold_convert_loc (loc, type, arg0);
13590
13591       return NULL_TREE;
13592
13593     case FMA_EXPR:
13594       /* For integers we can decompose the FMA if possible.  */
13595       if (TREE_CODE (arg0) == INTEGER_CST
13596           && TREE_CODE (arg1) == INTEGER_CST)
13597         return fold_build2_loc (loc, PLUS_EXPR, type,
13598                                 const_binop (MULT_EXPR, arg0, arg1), arg2);
13599       if (integer_zerop (arg2))
13600         return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
13601
13602       return fold_fma (loc, type, arg0, arg1, arg2);
13603
13604     default:
13605       return NULL_TREE;
13606     } /* switch (code) */
13607 }
13608
13609 /* Perform constant folding and related simplification of EXPR.
13610    The related simplifications include x*1 => x, x*0 => 0, etc.,
13611    and application of the associative law.
13612    NOP_EXPR conversions may be removed freely (as long as we
13613    are careful not to change the type of the overall expression).
13614    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13615    but we can constant-fold them if they have constant operands.  */
13616
13617 #ifdef ENABLE_FOLD_CHECKING
13618 # define fold(x) fold_1 (x)
13619 static tree fold_1 (tree);
13620 static
13621 #endif
13622 tree
13623 fold (tree expr)
13624 {
13625   const tree t = expr;
13626   enum tree_code code = TREE_CODE (t);
13627   enum tree_code_class kind = TREE_CODE_CLASS (code);
13628   tree tem;
13629   location_t loc = EXPR_LOCATION (expr);
13630
13631   /* Return right away if a constant.  */
13632   if (kind == tcc_constant)
13633     return t;
13634
13635   /* CALL_EXPR-like objects with variable numbers of operands are
13636      treated specially.  */
13637   if (kind == tcc_vl_exp)
13638     {
13639       if (code == CALL_EXPR)
13640         {
13641           tem = fold_call_expr (loc, expr, false);
13642           return tem ? tem : expr;
13643         }
13644       return expr;
13645     }
13646
13647   if (IS_EXPR_CODE_CLASS (kind))
13648     {
13649       tree type = TREE_TYPE (t);
13650       tree op0, op1, op2;
13651
13652       switch (TREE_CODE_LENGTH (code))
13653         {
13654         case 1:
13655           op0 = TREE_OPERAND (t, 0);
13656           tem = fold_unary_loc (loc, code, type, op0);
13657           return tem ? tem : expr;
13658         case 2:
13659           op0 = TREE_OPERAND (t, 0);
13660           op1 = TREE_OPERAND (t, 1);
13661           tem = fold_binary_loc (loc, code, type, op0, op1);
13662           return tem ? tem : expr;
13663         case 3:
13664           op0 = TREE_OPERAND (t, 0);
13665           op1 = TREE_OPERAND (t, 1);
13666           op2 = TREE_OPERAND (t, 2);
13667           tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13668           return tem ? tem : expr;
13669         default:
13670           break;
13671         }
13672     }
13673
13674   switch (code)
13675     {
13676     case ARRAY_REF:
13677       {
13678         tree op0 = TREE_OPERAND (t, 0);
13679         tree op1 = TREE_OPERAND (t, 1);
13680
13681         if (TREE_CODE (op1) == INTEGER_CST
13682             && TREE_CODE (op0) == CONSTRUCTOR
13683             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13684           {
13685             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13686             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13687             unsigned HOST_WIDE_INT begin = 0;
13688
13689             /* Find a matching index by means of a binary search.  */
13690             while (begin != end)
13691               {
13692                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13693                 tree index = VEC_index (constructor_elt, elts, middle)->index;
13694
13695                 if (TREE_CODE (index) == INTEGER_CST
13696                     && tree_int_cst_lt (index, op1))
13697                   begin = middle + 1;
13698                 else if (TREE_CODE (index) == INTEGER_CST
13699                          && tree_int_cst_lt (op1, index))
13700                   end = middle;
13701                 else if (TREE_CODE (index) == RANGE_EXPR
13702                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13703                   begin = middle + 1;
13704                 else if (TREE_CODE (index) == RANGE_EXPR
13705                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13706                   end = middle;
13707                 else
13708                   return VEC_index (constructor_elt, elts, middle)->value;
13709               }
13710           }
13711
13712         return t;
13713       }
13714
13715     case CONST_DECL:
13716       return fold (DECL_INITIAL (t));
13717
13718     default:
13719       return t;
13720     } /* switch (code) */
13721 }
13722
13723 #ifdef ENABLE_FOLD_CHECKING
13724 #undef fold
13725
13726 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13727 static void fold_check_failed (const_tree, const_tree);
13728 void print_fold_checksum (const_tree);
13729
13730 /* When --enable-checking=fold, compute a digest of expr before
13731    and after actual fold call to see if fold did not accidentally
13732    change original expr.  */
13733
13734 tree
13735 fold (tree expr)
13736 {
13737   tree ret;
13738   struct md5_ctx ctx;
13739   unsigned char checksum_before[16], checksum_after[16];
13740   htab_t ht;
13741
13742   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13743   md5_init_ctx (&ctx);
13744   fold_checksum_tree (expr, &ctx, ht);
13745   md5_finish_ctx (&ctx, checksum_before);
13746   htab_empty (ht);
13747
13748   ret = fold_1 (expr);
13749
13750   md5_init_ctx (&ctx);
13751   fold_checksum_tree (expr, &ctx, ht);
13752   md5_finish_ctx (&ctx, checksum_after);
13753   htab_delete (ht);
13754
13755   if (memcmp (checksum_before, checksum_after, 16))
13756     fold_check_failed (expr, ret);
13757
13758   return ret;
13759 }
13760
13761 void
13762 print_fold_checksum (const_tree expr)
13763 {
13764   struct md5_ctx ctx;
13765   unsigned char checksum[16], cnt;
13766   htab_t ht;
13767
13768   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13769   md5_init_ctx (&ctx);
13770   fold_checksum_tree (expr, &ctx, ht);
13771   md5_finish_ctx (&ctx, checksum);
13772   htab_delete (ht);
13773   for (cnt = 0; cnt < 16; ++cnt)
13774     fprintf (stderr, "%02x", checksum[cnt]);
13775   putc ('\n', stderr);
13776 }
13777
13778 static void
13779 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13780 {
13781   internal_error ("fold check: original tree changed by fold");
13782 }
13783
13784 static void
13785 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13786 {
13787   void **slot;
13788   enum tree_code code;
13789   union tree_node buf;
13790   int i, len;
13791
13792 recursive_label:
13793
13794   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13795                <= sizeof (struct tree_function_decl))
13796               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13797   if (expr == NULL)
13798     return;
13799   slot = (void **) htab_find_slot (ht, expr, INSERT);
13800   if (*slot != NULL)
13801     return;
13802   *slot = CONST_CAST_TREE (expr);
13803   code = TREE_CODE (expr);
13804   if (TREE_CODE_CLASS (code) == tcc_declaration
13805       && DECL_ASSEMBLER_NAME_SET_P (expr))
13806     {
13807       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
13808       memcpy ((char *) &buf, expr, tree_size (expr));
13809       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13810       expr = (tree) &buf;
13811     }
13812   else if (TREE_CODE_CLASS (code) == tcc_type
13813            && (TYPE_POINTER_TO (expr)
13814                || TYPE_REFERENCE_TO (expr)
13815                || TYPE_CACHED_VALUES_P (expr)
13816                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13817                || TYPE_NEXT_VARIANT (expr)))
13818     {
13819       /* Allow these fields to be modified.  */
13820       tree tmp;
13821       memcpy ((char *) &buf, expr, tree_size (expr));
13822       expr = tmp = (tree) &buf;
13823       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13824       TYPE_POINTER_TO (tmp) = NULL;
13825       TYPE_REFERENCE_TO (tmp) = NULL;
13826       TYPE_NEXT_VARIANT (tmp) = NULL;
13827       if (TYPE_CACHED_VALUES_P (tmp))
13828         {
13829           TYPE_CACHED_VALUES_P (tmp) = 0;
13830           TYPE_CACHED_VALUES (tmp) = NULL;
13831         }
13832     }
13833   md5_process_bytes (expr, tree_size (expr), ctx);
13834   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13835   if (TREE_CODE_CLASS (code) != tcc_type
13836       && TREE_CODE_CLASS (code) != tcc_declaration
13837       && code != TREE_LIST
13838       && code != SSA_NAME)
13839     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13840   switch (TREE_CODE_CLASS (code))
13841     {
13842     case tcc_constant:
13843       switch (code)
13844         {
13845         case STRING_CST:
13846           md5_process_bytes (TREE_STRING_POINTER (expr),
13847                              TREE_STRING_LENGTH (expr), ctx);
13848           break;
13849         case COMPLEX_CST:
13850           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13851           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13852           break;
13853         case VECTOR_CST:
13854           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13855           break;
13856         default:
13857           break;
13858         }
13859       break;
13860     case tcc_exceptional:
13861       switch (code)
13862         {
13863         case TREE_LIST:
13864           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13865           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13866           expr = TREE_CHAIN (expr);
13867           goto recursive_label;
13868           break;
13869         case TREE_VEC:
13870           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13871             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13872           break;
13873         default:
13874           break;
13875         }
13876       break;
13877     case tcc_expression:
13878     case tcc_reference:
13879     case tcc_comparison:
13880     case tcc_unary:
13881     case tcc_binary:
13882     case tcc_statement:
13883     case tcc_vl_exp:
13884       len = TREE_OPERAND_LENGTH (expr);
13885       for (i = 0; i < len; ++i)
13886         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13887       break;
13888     case tcc_declaration:
13889       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13890       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13891       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13892         {
13893           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13894           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13895           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13896           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13897           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13898         }
13899       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13900         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13901
13902       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13903         {
13904           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13905           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13906           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13907         }
13908       break;
13909     case tcc_type:
13910       if (TREE_CODE (expr) == ENUMERAL_TYPE)
13911         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13912       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13913       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13914       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13915       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13916       if (INTEGRAL_TYPE_P (expr)
13917           || SCALAR_FLOAT_TYPE_P (expr))
13918         {
13919           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13920           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13921         }
13922       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13923       if (TREE_CODE (expr) == RECORD_TYPE
13924           || TREE_CODE (expr) == UNION_TYPE
13925           || TREE_CODE (expr) == QUAL_UNION_TYPE)
13926         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13927       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13928       break;
13929     default:
13930       break;
13931     }
13932 }
13933
13934 /* Helper function for outputting the checksum of a tree T.  When
13935    debugging with gdb, you can "define mynext" to be "next" followed
13936    by "call debug_fold_checksum (op0)", then just trace down till the
13937    outputs differ.  */
13938
13939 DEBUG_FUNCTION void
13940 debug_fold_checksum (const_tree t)
13941 {
13942   int i;
13943   unsigned char checksum[16];
13944   struct md5_ctx ctx;
13945   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13946
13947   md5_init_ctx (&ctx);
13948   fold_checksum_tree (t, &ctx, ht);
13949   md5_finish_ctx (&ctx, checksum);
13950   htab_empty (ht);
13951
13952   for (i = 0; i < 16; i++)
13953     fprintf (stderr, "%d ", checksum[i]);
13954
13955   fprintf (stderr, "\n");
13956 }
13957
13958 #endif
13959
13960 /* Fold a unary tree expression with code CODE of type TYPE with an
13961    operand OP0.  LOC is the location of the resulting expression.
13962    Return a folded expression if successful.  Otherwise, return a tree
13963    expression with code CODE of type TYPE with an operand OP0.  */
13964
13965 tree
13966 fold_build1_stat_loc (location_t loc,
13967                       enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13968 {
13969   tree tem;
13970 #ifdef ENABLE_FOLD_CHECKING
13971   unsigned char checksum_before[16], checksum_after[16];
13972   struct md5_ctx ctx;
13973   htab_t ht;
13974
13975   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13976   md5_init_ctx (&ctx);
13977   fold_checksum_tree (op0, &ctx, ht);
13978   md5_finish_ctx (&ctx, checksum_before);
13979   htab_empty (ht);
13980 #endif
13981
13982   tem = fold_unary_loc (loc, code, type, op0);
13983   if (!tem)
13984     tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
13985
13986 #ifdef ENABLE_FOLD_CHECKING
13987   md5_init_ctx (&ctx);
13988   fold_checksum_tree (op0, &ctx, ht);
13989   md5_finish_ctx (&ctx, checksum_after);
13990   htab_delete (ht);
13991
13992   if (memcmp (checksum_before, checksum_after, 16))
13993     fold_check_failed (op0, tem);
13994 #endif
13995   return tem;
13996 }
13997
13998 /* Fold a binary tree expression with code CODE of type TYPE with
13999    operands OP0 and OP1.  LOC is the location of the resulting
14000    expression.  Return a folded expression if successful.  Otherwise,
14001    return a tree expression with code CODE of type TYPE with operands
14002    OP0 and OP1.  */
14003
14004 tree
14005 fold_build2_stat_loc (location_t loc,
14006                       enum tree_code code, tree type, tree op0, tree op1
14007                       MEM_STAT_DECL)
14008 {
14009   tree tem;
14010 #ifdef ENABLE_FOLD_CHECKING
14011   unsigned char checksum_before_op0[16],
14012                 checksum_before_op1[16],
14013                 checksum_after_op0[16],
14014                 checksum_after_op1[16];
14015   struct md5_ctx ctx;
14016   htab_t ht;
14017
14018   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14019   md5_init_ctx (&ctx);
14020   fold_checksum_tree (op0, &ctx, ht);
14021   md5_finish_ctx (&ctx, checksum_before_op0);
14022   htab_empty (ht);
14023
14024   md5_init_ctx (&ctx);
14025   fold_checksum_tree (op1, &ctx, ht);
14026   md5_finish_ctx (&ctx, checksum_before_op1);
14027   htab_empty (ht);
14028 #endif
14029
14030   tem = fold_binary_loc (loc, code, type, op0, op1);
14031   if (!tem)
14032     tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
14033
14034 #ifdef ENABLE_FOLD_CHECKING
14035   md5_init_ctx (&ctx);
14036   fold_checksum_tree (op0, &ctx, ht);
14037   md5_finish_ctx (&ctx, checksum_after_op0);
14038   htab_empty (ht);
14039
14040   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14041     fold_check_failed (op0, tem);
14042
14043   md5_init_ctx (&ctx);
14044   fold_checksum_tree (op1, &ctx, ht);
14045   md5_finish_ctx (&ctx, checksum_after_op1);
14046   htab_delete (ht);
14047
14048   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14049     fold_check_failed (op1, tem);
14050 #endif
14051   return tem;
14052 }
14053
14054 /* Fold a ternary tree expression with code CODE of type TYPE with
14055    operands OP0, OP1, and OP2.  Return a folded expression if
14056    successful.  Otherwise, return a tree expression with code CODE of
14057    type TYPE with operands OP0, OP1, and OP2.  */
14058
14059 tree
14060 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14061                       tree op0, tree op1, tree op2 MEM_STAT_DECL)
14062 {
14063   tree tem;
14064 #ifdef ENABLE_FOLD_CHECKING
14065   unsigned char checksum_before_op0[16],
14066                 checksum_before_op1[16],
14067                 checksum_before_op2[16],
14068                 checksum_after_op0[16],
14069                 checksum_after_op1[16],
14070                 checksum_after_op2[16];
14071   struct md5_ctx ctx;
14072   htab_t ht;
14073
14074   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14075   md5_init_ctx (&ctx);
14076   fold_checksum_tree (op0, &ctx, ht);
14077   md5_finish_ctx (&ctx, checksum_before_op0);
14078   htab_empty (ht);
14079
14080   md5_init_ctx (&ctx);
14081   fold_checksum_tree (op1, &ctx, ht);
14082   md5_finish_ctx (&ctx, checksum_before_op1);
14083   htab_empty (ht);
14084
14085   md5_init_ctx (&ctx);
14086   fold_checksum_tree (op2, &ctx, ht);
14087   md5_finish_ctx (&ctx, checksum_before_op2);
14088   htab_empty (ht);
14089 #endif
14090
14091   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14092   tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14093   if (!tem)
14094     tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
14095
14096 #ifdef ENABLE_FOLD_CHECKING
14097   md5_init_ctx (&ctx);
14098   fold_checksum_tree (op0, &ctx, ht);
14099   md5_finish_ctx (&ctx, checksum_after_op0);
14100   htab_empty (ht);
14101
14102   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14103     fold_check_failed (op0, tem);
14104
14105   md5_init_ctx (&ctx);
14106   fold_checksum_tree (op1, &ctx, ht);
14107   md5_finish_ctx (&ctx, checksum_after_op1);
14108   htab_empty (ht);
14109
14110   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14111     fold_check_failed (op1, tem);
14112
14113   md5_init_ctx (&ctx);
14114   fold_checksum_tree (op2, &ctx, ht);
14115   md5_finish_ctx (&ctx, checksum_after_op2);
14116   htab_delete (ht);
14117
14118   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14119     fold_check_failed (op2, tem);
14120 #endif
14121   return tem;
14122 }
14123
14124 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14125    arguments in ARGARRAY, and a null static chain.
14126    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
14127    of type TYPE from the given operands as constructed by build_call_array.  */
14128
14129 tree
14130 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14131                            int nargs, tree *argarray)
14132 {
14133   tree tem;
14134 #ifdef ENABLE_FOLD_CHECKING
14135   unsigned char checksum_before_fn[16],
14136                 checksum_before_arglist[16],
14137                 checksum_after_fn[16],
14138                 checksum_after_arglist[16];
14139   struct md5_ctx ctx;
14140   htab_t ht;
14141   int i;
14142
14143   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14144   md5_init_ctx (&ctx);
14145   fold_checksum_tree (fn, &ctx, ht);
14146   md5_finish_ctx (&ctx, checksum_before_fn);
14147   htab_empty (ht);
14148
14149   md5_init_ctx (&ctx);
14150   for (i = 0; i < nargs; i++)
14151     fold_checksum_tree (argarray[i], &ctx, ht);
14152   md5_finish_ctx (&ctx, checksum_before_arglist);
14153   htab_empty (ht);
14154 #endif
14155
14156   tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14157
14158 #ifdef ENABLE_FOLD_CHECKING
14159   md5_init_ctx (&ctx);
14160   fold_checksum_tree (fn, &ctx, ht);
14161   md5_finish_ctx (&ctx, checksum_after_fn);
14162   htab_empty (ht);
14163
14164   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14165     fold_check_failed (fn, tem);
14166
14167   md5_init_ctx (&ctx);
14168   for (i = 0; i < nargs; i++)
14169     fold_checksum_tree (argarray[i], &ctx, ht);
14170   md5_finish_ctx (&ctx, checksum_after_arglist);
14171   htab_delete (ht);
14172
14173   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14174     fold_check_failed (NULL_TREE, tem);
14175 #endif
14176   return tem;
14177 }
14178
14179 /* Perform constant folding and related simplification of initializer
14180    expression EXPR.  These behave identically to "fold_buildN" but ignore
14181    potential run-time traps and exceptions that fold must preserve.  */
14182
14183 #define START_FOLD_INIT \
14184   int saved_signaling_nans = flag_signaling_nans;\
14185   int saved_trapping_math = flag_trapping_math;\
14186   int saved_rounding_math = flag_rounding_math;\
14187   int saved_trapv = flag_trapv;\
14188   int saved_folding_initializer = folding_initializer;\
14189   flag_signaling_nans = 0;\
14190   flag_trapping_math = 0;\
14191   flag_rounding_math = 0;\
14192   flag_trapv = 0;\
14193   folding_initializer = 1;
14194
14195 #define END_FOLD_INIT \
14196   flag_signaling_nans = saved_signaling_nans;\
14197   flag_trapping_math = saved_trapping_math;\
14198   flag_rounding_math = saved_rounding_math;\
14199   flag_trapv = saved_trapv;\
14200   folding_initializer = saved_folding_initializer;
14201
14202 tree
14203 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14204                              tree type, tree op)
14205 {
14206   tree result;
14207   START_FOLD_INIT;
14208
14209   result = fold_build1_loc (loc, code, type, op);
14210
14211   END_FOLD_INIT;
14212   return result;
14213 }
14214
14215 tree
14216 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14217                              tree type, tree op0, tree op1)
14218 {
14219   tree result;
14220   START_FOLD_INIT;
14221
14222   result = fold_build2_loc (loc, code, type, op0, op1);
14223
14224   END_FOLD_INIT;
14225   return result;
14226 }
14227
14228 tree
14229 fold_build3_initializer_loc (location_t loc, enum tree_code code,
14230                              tree type, tree op0, tree op1, tree op2)
14231 {
14232   tree result;
14233   START_FOLD_INIT;
14234
14235   result = fold_build3_loc (loc, code, type, op0, op1, op2);
14236
14237   END_FOLD_INIT;
14238   return result;
14239 }
14240
14241 tree
14242 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14243                                        int nargs, tree *argarray)
14244 {
14245   tree result;
14246   START_FOLD_INIT;
14247
14248   result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14249
14250   END_FOLD_INIT;
14251   return result;
14252 }
14253
14254 #undef START_FOLD_INIT
14255 #undef END_FOLD_INIT
14256
14257 /* Determine if first argument is a multiple of second argument.  Return 0 if
14258    it is not, or we cannot easily determined it to be.
14259
14260    An example of the sort of thing we care about (at this point; this routine
14261    could surely be made more general, and expanded to do what the *_DIV_EXPR's
14262    fold cases do now) is discovering that
14263
14264      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14265
14266    is a multiple of
14267
14268      SAVE_EXPR (J * 8)
14269
14270    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14271
14272    This code also handles discovering that
14273
14274      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14275
14276    is a multiple of 8 so we don't have to worry about dealing with a
14277    possible remainder.
14278
14279    Note that we *look* inside a SAVE_EXPR only to determine how it was
14280    calculated; it is not safe for fold to do much of anything else with the
14281    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14282    at run time.  For example, the latter example above *cannot* be implemented
14283    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14284    evaluation time of the original SAVE_EXPR is not necessarily the same at
14285    the time the new expression is evaluated.  The only optimization of this
14286    sort that would be valid is changing
14287
14288      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14289
14290    divided by 8 to
14291
14292      SAVE_EXPR (I) * SAVE_EXPR (J)
14293
14294    (where the same SAVE_EXPR (J) is used in the original and the
14295    transformed version).  */
14296
14297 int
14298 multiple_of_p (tree type, const_tree top, const_tree bottom)
14299 {
14300   if (operand_equal_p (top, bottom, 0))
14301     return 1;
14302
14303   if (TREE_CODE (type) != INTEGER_TYPE)
14304     return 0;
14305
14306   switch (TREE_CODE (top))
14307     {
14308     case BIT_AND_EXPR:
14309       /* Bitwise and provides a power of two multiple.  If the mask is
14310          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
14311       if (!integer_pow2p (bottom))
14312         return 0;
14313       /* FALLTHRU */
14314
14315     case MULT_EXPR:
14316       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14317               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14318
14319     case PLUS_EXPR:
14320     case MINUS_EXPR:
14321       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14322               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14323
14324     case LSHIFT_EXPR:
14325       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14326         {
14327           tree op1, t1;
14328
14329           op1 = TREE_OPERAND (top, 1);
14330           /* const_binop may not detect overflow correctly,
14331              so check for it explicitly here.  */
14332           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14333               > TREE_INT_CST_LOW (op1)
14334               && TREE_INT_CST_HIGH (op1) == 0
14335               && 0 != (t1 = fold_convert (type,
14336                                           const_binop (LSHIFT_EXPR,
14337                                                        size_one_node,
14338                                                        op1)))
14339               && !TREE_OVERFLOW (t1))
14340             return multiple_of_p (type, t1, bottom);
14341         }
14342       return 0;
14343
14344     case NOP_EXPR:
14345       /* Can't handle conversions from non-integral or wider integral type.  */
14346       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14347           || (TYPE_PRECISION (type)
14348               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14349         return 0;
14350
14351       /* .. fall through ...  */
14352
14353     case SAVE_EXPR:
14354       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14355
14356     case COND_EXPR:
14357       return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
14358               && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
14359
14360     case INTEGER_CST:
14361       if (TREE_CODE (bottom) != INTEGER_CST
14362           || integer_zerop (bottom)
14363           || (TYPE_UNSIGNED (type)
14364               && (tree_int_cst_sgn (top) < 0
14365                   || tree_int_cst_sgn (bottom) < 0)))
14366         return 0;
14367       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14368                                              top, bottom, 0));
14369
14370     default:
14371       return 0;
14372     }
14373 }
14374
14375 /* Return true if CODE or TYPE is known to be non-negative. */
14376
14377 static bool
14378 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14379 {
14380   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14381       && truth_value_p (code))
14382     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14383        have a signed:1 type (where the value is -1 and 0).  */
14384     return true;
14385   return false;
14386 }
14387
14388 /* Return true if (CODE OP0) is known to be non-negative.  If the return
14389    value is based on the assumption that signed overflow is undefined,
14390    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14391    *STRICT_OVERFLOW_P.  */
14392
14393 bool
14394 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14395                                 bool *strict_overflow_p)
14396 {
14397   if (TYPE_UNSIGNED (type))
14398     return true;
14399
14400   switch (code)
14401     {
14402     case ABS_EXPR:
14403       /* We can't return 1 if flag_wrapv is set because
14404          ABS_EXPR<INT_MIN> = INT_MIN.  */
14405       if (!INTEGRAL_TYPE_P (type))
14406         return true;
14407       if (TYPE_OVERFLOW_UNDEFINED (type))
14408         {
14409           *strict_overflow_p = true;
14410           return true;
14411         }
14412       break;
14413
14414     case NON_LVALUE_EXPR:
14415     case FLOAT_EXPR:
14416     case FIX_TRUNC_EXPR:
14417       return tree_expr_nonnegative_warnv_p (op0,
14418                                             strict_overflow_p);
14419
14420     case NOP_EXPR:
14421       {
14422         tree inner_type = TREE_TYPE (op0);
14423         tree outer_type = type;
14424
14425         if (TREE_CODE (outer_type) == REAL_TYPE)
14426           {
14427             if (TREE_CODE (inner_type) == REAL_TYPE)
14428               return tree_expr_nonnegative_warnv_p (op0,
14429                                                     strict_overflow_p);
14430             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14431               {
14432                 if (TYPE_UNSIGNED (inner_type))
14433                   return true;
14434                 return tree_expr_nonnegative_warnv_p (op0,
14435                                                       strict_overflow_p);
14436               }
14437           }
14438         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14439           {
14440             if (TREE_CODE (inner_type) == REAL_TYPE)
14441               return tree_expr_nonnegative_warnv_p (op0,
14442                                                     strict_overflow_p);
14443             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14444               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14445                       && TYPE_UNSIGNED (inner_type);
14446           }
14447       }
14448       break;
14449
14450     default:
14451       return tree_simple_nonnegative_warnv_p (code, type);
14452     }
14453
14454   /* We don't know sign of `t', so be conservative and return false.  */
14455   return false;
14456 }
14457
14458 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
14459    value is based on the assumption that signed overflow is undefined,
14460    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14461    *STRICT_OVERFLOW_P.  */
14462
14463 bool
14464 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14465                                       tree op1, bool *strict_overflow_p)
14466 {
14467   if (TYPE_UNSIGNED (type))
14468     return true;
14469
14470   switch (code)
14471     {
14472     case POINTER_PLUS_EXPR:
14473     case PLUS_EXPR:
14474       if (FLOAT_TYPE_P (type))
14475         return (tree_expr_nonnegative_warnv_p (op0,
14476                                                strict_overflow_p)
14477                 && tree_expr_nonnegative_warnv_p (op1,
14478                                                   strict_overflow_p));
14479
14480       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14481          both unsigned and at least 2 bits shorter than the result.  */
14482       if (TREE_CODE (type) == INTEGER_TYPE
14483           && TREE_CODE (op0) == NOP_EXPR
14484           && TREE_CODE (op1) == NOP_EXPR)
14485         {
14486           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14487           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14488           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14489               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14490             {
14491               unsigned int prec = MAX (TYPE_PRECISION (inner1),
14492                                        TYPE_PRECISION (inner2)) + 1;
14493               return prec < TYPE_PRECISION (type);
14494             }
14495         }
14496       break;
14497
14498     case MULT_EXPR:
14499       if (FLOAT_TYPE_P (type))
14500         {
14501           /* x * x for floating point x is always non-negative.  */
14502           if (operand_equal_p (op0, op1, 0))
14503             return true;
14504           return (tree_expr_nonnegative_warnv_p (op0,
14505                                                  strict_overflow_p)
14506                   && tree_expr_nonnegative_warnv_p (op1,
14507                                                     strict_overflow_p));
14508         }
14509
14510       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14511          both unsigned and their total bits is shorter than the result.  */
14512       if (TREE_CODE (type) == INTEGER_TYPE
14513           && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14514           && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14515         {
14516           tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14517             ? TREE_TYPE (TREE_OPERAND (op0, 0))
14518             : TREE_TYPE (op0);
14519           tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14520             ? TREE_TYPE (TREE_OPERAND (op1, 0))
14521             : TREE_TYPE (op1);
14522
14523           bool unsigned0 = TYPE_UNSIGNED (inner0);
14524           bool unsigned1 = TYPE_UNSIGNED (inner1);
14525
14526           if (TREE_CODE (op0) == INTEGER_CST)
14527             unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14528
14529           if (TREE_CODE (op1) == INTEGER_CST)
14530             unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14531
14532           if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14533               && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14534             {
14535               unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14536                 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14537                 : TYPE_PRECISION (inner0);
14538
14539               unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14540                 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14541                 : TYPE_PRECISION (inner1);
14542
14543               return precision0 + precision1 < TYPE_PRECISION (type);
14544             }
14545         }
14546       return false;
14547
14548     case BIT_AND_EXPR:
14549     case MAX_EXPR:
14550       return (tree_expr_nonnegative_warnv_p (op0,
14551                                              strict_overflow_p)
14552               || tree_expr_nonnegative_warnv_p (op1,
14553                                                 strict_overflow_p));
14554
14555     case BIT_IOR_EXPR:
14556     case BIT_XOR_EXPR:
14557     case MIN_EXPR:
14558     case RDIV_EXPR:
14559     case TRUNC_DIV_EXPR:
14560     case CEIL_DIV_EXPR:
14561     case FLOOR_DIV_EXPR:
14562     case ROUND_DIV_EXPR:
14563       return (tree_expr_nonnegative_warnv_p (op0,
14564                                              strict_overflow_p)
14565               && tree_expr_nonnegative_warnv_p (op1,
14566                                                 strict_overflow_p));
14567
14568     case TRUNC_MOD_EXPR:
14569     case CEIL_MOD_EXPR:
14570     case FLOOR_MOD_EXPR:
14571     case ROUND_MOD_EXPR:
14572       return tree_expr_nonnegative_warnv_p (op0,
14573                                             strict_overflow_p);
14574     default:
14575       return tree_simple_nonnegative_warnv_p (code, type);
14576     }
14577
14578   /* We don't know sign of `t', so be conservative and return false.  */
14579   return false;
14580 }
14581
14582 /* Return true if T is known to be non-negative.  If the return
14583    value is based on the assumption that signed overflow is undefined,
14584    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14585    *STRICT_OVERFLOW_P.  */
14586
14587 bool
14588 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14589 {
14590   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14591     return true;
14592
14593   switch (TREE_CODE (t))
14594     {
14595     case INTEGER_CST:
14596       return tree_int_cst_sgn (t) >= 0;
14597
14598     case REAL_CST:
14599       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14600
14601     case FIXED_CST:
14602       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14603
14604     case COND_EXPR:
14605       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14606                                              strict_overflow_p)
14607               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14608                                                 strict_overflow_p));
14609     default:
14610       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14611                                                    TREE_TYPE (t));
14612     }
14613   /* We don't know sign of `t', so be conservative and return false.  */
14614   return false;
14615 }
14616
14617 /* Return true if T is known to be non-negative.  If the return
14618    value is based on the assumption that signed overflow is undefined,
14619    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14620    *STRICT_OVERFLOW_P.  */
14621
14622 bool
14623 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14624                                tree arg0, tree arg1, bool *strict_overflow_p)
14625 {
14626   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14627     switch (DECL_FUNCTION_CODE (fndecl))
14628       {
14629         CASE_FLT_FN (BUILT_IN_ACOS):
14630         CASE_FLT_FN (BUILT_IN_ACOSH):
14631         CASE_FLT_FN (BUILT_IN_CABS):
14632         CASE_FLT_FN (BUILT_IN_COSH):
14633         CASE_FLT_FN (BUILT_IN_ERFC):
14634         CASE_FLT_FN (BUILT_IN_EXP):
14635         CASE_FLT_FN (BUILT_IN_EXP10):
14636         CASE_FLT_FN (BUILT_IN_EXP2):
14637         CASE_FLT_FN (BUILT_IN_FABS):
14638         CASE_FLT_FN (BUILT_IN_FDIM):
14639         CASE_FLT_FN (BUILT_IN_HYPOT):
14640         CASE_FLT_FN (BUILT_IN_POW10):
14641         CASE_INT_FN (BUILT_IN_FFS):
14642         CASE_INT_FN (BUILT_IN_PARITY):
14643         CASE_INT_FN (BUILT_IN_POPCOUNT):
14644       case BUILT_IN_BSWAP32:
14645       case BUILT_IN_BSWAP64:
14646         /* Always true.  */
14647         return true;
14648
14649         CASE_FLT_FN (BUILT_IN_SQRT):
14650         /* sqrt(-0.0) is -0.0.  */
14651         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14652           return true;
14653         return tree_expr_nonnegative_warnv_p (arg0,
14654                                               strict_overflow_p);
14655
14656         CASE_FLT_FN (BUILT_IN_ASINH):
14657         CASE_FLT_FN (BUILT_IN_ATAN):
14658         CASE_FLT_FN (BUILT_IN_ATANH):
14659         CASE_FLT_FN (BUILT_IN_CBRT):
14660         CASE_FLT_FN (BUILT_IN_CEIL):
14661         CASE_FLT_FN (BUILT_IN_ERF):
14662         CASE_FLT_FN (BUILT_IN_EXPM1):
14663         CASE_FLT_FN (BUILT_IN_FLOOR):
14664         CASE_FLT_FN (BUILT_IN_FMOD):
14665         CASE_FLT_FN (BUILT_IN_FREXP):
14666         CASE_FLT_FN (BUILT_IN_LCEIL):
14667         CASE_FLT_FN (BUILT_IN_LDEXP):
14668         CASE_FLT_FN (BUILT_IN_LFLOOR):
14669         CASE_FLT_FN (BUILT_IN_LLCEIL):
14670         CASE_FLT_FN (BUILT_IN_LLFLOOR):
14671         CASE_FLT_FN (BUILT_IN_LLRINT):
14672         CASE_FLT_FN (BUILT_IN_LLROUND):
14673         CASE_FLT_FN (BUILT_IN_LRINT):
14674         CASE_FLT_FN (BUILT_IN_LROUND):
14675         CASE_FLT_FN (BUILT_IN_MODF):
14676         CASE_FLT_FN (BUILT_IN_NEARBYINT):
14677         CASE_FLT_FN (BUILT_IN_RINT):
14678         CASE_FLT_FN (BUILT_IN_ROUND):
14679         CASE_FLT_FN (BUILT_IN_SCALB):
14680         CASE_FLT_FN (BUILT_IN_SCALBLN):
14681         CASE_FLT_FN (BUILT_IN_SCALBN):
14682         CASE_FLT_FN (BUILT_IN_SIGNBIT):
14683         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14684         CASE_FLT_FN (BUILT_IN_SINH):
14685         CASE_FLT_FN (BUILT_IN_TANH):
14686         CASE_FLT_FN (BUILT_IN_TRUNC):
14687         /* True if the 1st argument is nonnegative.  */
14688         return tree_expr_nonnegative_warnv_p (arg0,
14689                                               strict_overflow_p);
14690
14691         CASE_FLT_FN (BUILT_IN_FMAX):
14692         /* True if the 1st OR 2nd arguments are nonnegative.  */
14693         return (tree_expr_nonnegative_warnv_p (arg0,
14694                                                strict_overflow_p)
14695                 || (tree_expr_nonnegative_warnv_p (arg1,
14696                                                    strict_overflow_p)));
14697
14698         CASE_FLT_FN (BUILT_IN_FMIN):
14699         /* True if the 1st AND 2nd arguments are nonnegative.  */
14700         return (tree_expr_nonnegative_warnv_p (arg0,
14701                                                strict_overflow_p)
14702                 && (tree_expr_nonnegative_warnv_p (arg1,
14703                                                    strict_overflow_p)));
14704
14705         CASE_FLT_FN (BUILT_IN_COPYSIGN):
14706         /* True if the 2nd argument is nonnegative.  */
14707         return tree_expr_nonnegative_warnv_p (arg1,
14708                                               strict_overflow_p);
14709
14710         CASE_FLT_FN (BUILT_IN_POWI):
14711         /* True if the 1st argument is nonnegative or the second
14712            argument is an even integer.  */
14713         if (TREE_CODE (arg1) == INTEGER_CST
14714             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14715           return true;
14716         return tree_expr_nonnegative_warnv_p (arg0,
14717                                               strict_overflow_p);
14718
14719         CASE_FLT_FN (BUILT_IN_POW):
14720         /* True if the 1st argument is nonnegative or the second
14721            argument is an even integer valued real.  */
14722         if (TREE_CODE (arg1) == REAL_CST)
14723           {
14724             REAL_VALUE_TYPE c;
14725             HOST_WIDE_INT n;
14726
14727             c = TREE_REAL_CST (arg1);
14728             n = real_to_integer (&c);
14729             if ((n & 1) == 0)
14730               {
14731                 REAL_VALUE_TYPE cint;
14732                 real_from_integer (&cint, VOIDmode, n,
14733                                    n < 0 ? -1 : 0, 0);
14734                 if (real_identical (&c, &cint))
14735                   return true;
14736               }
14737           }
14738         return tree_expr_nonnegative_warnv_p (arg0,
14739                                               strict_overflow_p);
14740
14741       default:
14742         break;
14743       }
14744   return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14745                                           type);
14746 }
14747
14748 /* Return true if T is known to be non-negative.  If the return
14749    value is based on the assumption that signed overflow is undefined,
14750    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14751    *STRICT_OVERFLOW_P.  */
14752
14753 bool
14754 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14755 {
14756   enum tree_code code = TREE_CODE (t);
14757   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14758     return true;
14759
14760   switch (code)
14761     {
14762     case TARGET_EXPR:
14763       {
14764         tree temp = TARGET_EXPR_SLOT (t);
14765         t = TARGET_EXPR_INITIAL (t);
14766
14767         /* If the initializer is non-void, then it's a normal expression
14768            that will be assigned to the slot.  */
14769         if (!VOID_TYPE_P (t))
14770           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14771
14772         /* Otherwise, the initializer sets the slot in some way.  One common
14773            way is an assignment statement at the end of the initializer.  */
14774         while (1)
14775           {
14776             if (TREE_CODE (t) == BIND_EXPR)
14777               t = expr_last (BIND_EXPR_BODY (t));
14778             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14779                      || TREE_CODE (t) == TRY_CATCH_EXPR)
14780               t = expr_last (TREE_OPERAND (t, 0));
14781             else if (TREE_CODE (t) == STATEMENT_LIST)
14782               t = expr_last (t);
14783             else
14784               break;
14785           }
14786         if (TREE_CODE (t) == MODIFY_EXPR
14787             && TREE_OPERAND (t, 0) == temp)
14788           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14789                                                 strict_overflow_p);
14790
14791         return false;
14792       }
14793
14794     case CALL_EXPR:
14795       {
14796         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
14797         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
14798
14799         return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14800                                               get_callee_fndecl (t),
14801                                               arg0,
14802                                               arg1,
14803                                               strict_overflow_p);
14804       }
14805     case COMPOUND_EXPR:
14806     case MODIFY_EXPR:
14807       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14808                                             strict_overflow_p);
14809     case BIND_EXPR:
14810       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14811                                             strict_overflow_p);
14812     case SAVE_EXPR:
14813       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14814                                             strict_overflow_p);
14815
14816     default:
14817       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14818                                                    TREE_TYPE (t));
14819     }
14820
14821   /* We don't know sign of `t', so be conservative and return false.  */
14822   return false;
14823 }
14824
14825 /* Return true if T is known to be non-negative.  If the return
14826    value is based on the assumption that signed overflow is undefined,
14827    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14828    *STRICT_OVERFLOW_P.  */
14829
14830 bool
14831 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14832 {
14833   enum tree_code code;
14834   if (t == error_mark_node)
14835     return false;
14836
14837   code = TREE_CODE (t);
14838   switch (TREE_CODE_CLASS (code))
14839     {
14840     case tcc_binary:
14841     case tcc_comparison:
14842       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14843                                               TREE_TYPE (t),
14844                                               TREE_OPERAND (t, 0),
14845                                               TREE_OPERAND (t, 1),
14846                                               strict_overflow_p);
14847
14848     case tcc_unary:
14849       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14850                                              TREE_TYPE (t),
14851                                              TREE_OPERAND (t, 0),
14852                                              strict_overflow_p);
14853
14854     case tcc_constant:
14855     case tcc_declaration:
14856     case tcc_reference:
14857       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14858
14859     default:
14860       break;
14861     }
14862
14863   switch (code)
14864     {
14865     case TRUTH_AND_EXPR:
14866     case TRUTH_OR_EXPR:
14867     case TRUTH_XOR_EXPR:
14868       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14869                                               TREE_TYPE (t),
14870                                               TREE_OPERAND (t, 0),
14871                                               TREE_OPERAND (t, 1),
14872                                               strict_overflow_p);
14873     case TRUTH_NOT_EXPR:
14874       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14875                                              TREE_TYPE (t),
14876                                              TREE_OPERAND (t, 0),
14877                                              strict_overflow_p);
14878
14879     case COND_EXPR:
14880     case CONSTRUCTOR:
14881     case OBJ_TYPE_REF:
14882     case ASSERT_EXPR:
14883     case ADDR_EXPR:
14884     case WITH_SIZE_EXPR:
14885     case SSA_NAME:
14886       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14887
14888     default:
14889       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14890     }
14891 }
14892
14893 /* Return true if `t' is known to be non-negative.  Handle warnings
14894    about undefined signed overflow.  */
14895
14896 bool
14897 tree_expr_nonnegative_p (tree t)
14898 {
14899   bool ret, strict_overflow_p;
14900
14901   strict_overflow_p = false;
14902   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14903   if (strict_overflow_p)
14904     fold_overflow_warning (("assuming signed overflow does not occur when "
14905                             "determining that expression is always "
14906                             "non-negative"),
14907                            WARN_STRICT_OVERFLOW_MISC);
14908   return ret;
14909 }
14910
14911
14912 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14913    For floating point we further ensure that T is not denormal.
14914    Similar logic is present in nonzero_address in rtlanal.h.
14915
14916    If the return value is based on the assumption that signed overflow
14917    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14918    change *STRICT_OVERFLOW_P.  */
14919
14920 bool
14921 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14922                                  bool *strict_overflow_p)
14923 {
14924   switch (code)
14925     {
14926     case ABS_EXPR:
14927       return tree_expr_nonzero_warnv_p (op0,
14928                                         strict_overflow_p);
14929
14930     case NOP_EXPR:
14931       {
14932         tree inner_type = TREE_TYPE (op0);
14933         tree outer_type = type;
14934
14935         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14936                 && tree_expr_nonzero_warnv_p (op0,
14937                                               strict_overflow_p));
14938       }
14939       break;
14940
14941     case NON_LVALUE_EXPR:
14942       return tree_expr_nonzero_warnv_p (op0,
14943                                         strict_overflow_p);
14944
14945     default:
14946       break;
14947   }
14948
14949   return false;
14950 }
14951
14952 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14953    For floating point we further ensure that T is not denormal.
14954    Similar logic is present in nonzero_address in rtlanal.h.
14955
14956    If the return value is based on the assumption that signed overflow
14957    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14958    change *STRICT_OVERFLOW_P.  */
14959
14960 bool
14961 tree_binary_nonzero_warnv_p (enum tree_code code,
14962                              tree type,
14963                              tree op0,
14964                              tree op1, bool *strict_overflow_p)
14965 {
14966   bool sub_strict_overflow_p;
14967   switch (code)
14968     {
14969     case POINTER_PLUS_EXPR:
14970     case PLUS_EXPR:
14971       if (TYPE_OVERFLOW_UNDEFINED (type))
14972         {
14973           /* With the presence of negative values it is hard
14974              to say something.  */
14975           sub_strict_overflow_p = false;
14976           if (!tree_expr_nonnegative_warnv_p (op0,
14977                                               &sub_strict_overflow_p)
14978               || !tree_expr_nonnegative_warnv_p (op1,
14979                                                  &sub_strict_overflow_p))
14980             return false;
14981           /* One of operands must be positive and the other non-negative.  */
14982           /* We don't set *STRICT_OVERFLOW_P here: even if this value
14983              overflows, on a twos-complement machine the sum of two
14984              nonnegative numbers can never be zero.  */
14985           return (tree_expr_nonzero_warnv_p (op0,
14986                                              strict_overflow_p)
14987                   || tree_expr_nonzero_warnv_p (op1,
14988                                                 strict_overflow_p));
14989         }
14990       break;
14991
14992     case MULT_EXPR:
14993       if (TYPE_OVERFLOW_UNDEFINED (type))
14994         {
14995           if (tree_expr_nonzero_warnv_p (op0,
14996                                          strict_overflow_p)
14997               && tree_expr_nonzero_warnv_p (op1,
14998                                             strict_overflow_p))
14999             {
15000               *strict_overflow_p = true;
15001               return true;
15002             }
15003         }
15004       break;
15005
15006     case MIN_EXPR:
15007       sub_strict_overflow_p = false;
15008       if (tree_expr_nonzero_warnv_p (op0,
15009                                      &sub_strict_overflow_p)
15010           && tree_expr_nonzero_warnv_p (op1,
15011                                         &sub_strict_overflow_p))
15012         {
15013           if (sub_strict_overflow_p)
15014             *strict_overflow_p = true;
15015         }
15016       break;
15017
15018     case MAX_EXPR:
15019       sub_strict_overflow_p = false;
15020       if (tree_expr_nonzero_warnv_p (op0,
15021                                      &sub_strict_overflow_p))
15022         {
15023           if (sub_strict_overflow_p)
15024             *strict_overflow_p = true;
15025
15026           /* When both operands are nonzero, then MAX must be too.  */
15027           if (tree_expr_nonzero_warnv_p (op1,
15028                                          strict_overflow_p))
15029             return true;
15030
15031           /* MAX where operand 0 is positive is positive.  */
15032           return tree_expr_nonnegative_warnv_p (op0,
15033                                                strict_overflow_p);
15034         }
15035       /* MAX where operand 1 is positive is positive.  */
15036       else if (tree_expr_nonzero_warnv_p (op1,
15037                                           &sub_strict_overflow_p)
15038                && tree_expr_nonnegative_warnv_p (op1,
15039                                                  &sub_strict_overflow_p))
15040         {
15041           if (sub_strict_overflow_p)
15042             *strict_overflow_p = true;
15043           return true;
15044         }
15045       break;
15046
15047     case BIT_IOR_EXPR:
15048       return (tree_expr_nonzero_warnv_p (op1,
15049                                          strict_overflow_p)
15050               || tree_expr_nonzero_warnv_p (op0,
15051                                             strict_overflow_p));
15052
15053     default:
15054       break;
15055   }
15056
15057   return false;
15058 }
15059
15060 /* Return true when T is an address and is known to be nonzero.
15061    For floating point we further ensure that T is not denormal.
15062    Similar logic is present in nonzero_address in rtlanal.h.
15063
15064    If the return value is based on the assumption that signed overflow
15065    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15066    change *STRICT_OVERFLOW_P.  */
15067
15068 bool
15069 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15070 {
15071   bool sub_strict_overflow_p;
15072   switch (TREE_CODE (t))
15073     {
15074     case INTEGER_CST:
15075       return !integer_zerop (t);
15076
15077     case ADDR_EXPR:
15078       {
15079         tree base = TREE_OPERAND (t, 0);
15080         if (!DECL_P (base))
15081           base = get_base_address (base);
15082
15083         if (!base)
15084           return false;
15085
15086         /* Weak declarations may link to NULL.  Other things may also be NULL
15087            so protect with -fdelete-null-pointer-checks; but not variables
15088            allocated on the stack.  */
15089         if (DECL_P (base)
15090             && (flag_delete_null_pointer_checks
15091                 || (DECL_CONTEXT (base)
15092                     && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
15093                     && auto_var_in_fn_p (base, DECL_CONTEXT (base)))))
15094           return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
15095
15096         /* Constants are never weak.  */
15097         if (CONSTANT_CLASS_P (base))
15098           return true;
15099
15100         return false;
15101       }
15102
15103     case COND_EXPR:
15104       sub_strict_overflow_p = false;
15105       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15106                                      &sub_strict_overflow_p)
15107           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15108                                         &sub_strict_overflow_p))
15109         {
15110           if (sub_strict_overflow_p)
15111             *strict_overflow_p = true;
15112           return true;
15113         }
15114       break;
15115
15116     default:
15117       break;
15118     }
15119   return false;
15120 }
15121
15122 /* Return true when T is an address and is known to be nonzero.
15123    For floating point we further ensure that T is not denormal.
15124    Similar logic is present in nonzero_address in rtlanal.h.
15125
15126    If the return value is based on the assumption that signed overflow
15127    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15128    change *STRICT_OVERFLOW_P.  */
15129
15130 bool
15131 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15132 {
15133   tree type = TREE_TYPE (t);
15134   enum tree_code code;
15135
15136   /* Doing something useful for floating point would need more work.  */
15137   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15138     return false;
15139
15140   code = TREE_CODE (t);
15141   switch (TREE_CODE_CLASS (code))
15142     {
15143     case tcc_unary:
15144       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15145                                               strict_overflow_p);
15146     case tcc_binary:
15147     case tcc_comparison:
15148       return tree_binary_nonzero_warnv_p (code, type,
15149                                                TREE_OPERAND (t, 0),
15150                                                TREE_OPERAND (t, 1),
15151                                                strict_overflow_p);
15152     case tcc_constant:
15153     case tcc_declaration:
15154     case tcc_reference:
15155       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15156
15157     default:
15158       break;
15159     }
15160
15161   switch (code)
15162     {
15163     case TRUTH_NOT_EXPR:
15164       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15165                                               strict_overflow_p);
15166
15167     case TRUTH_AND_EXPR:
15168     case TRUTH_OR_EXPR:
15169     case TRUTH_XOR_EXPR:
15170       return tree_binary_nonzero_warnv_p (code, type,
15171                                                TREE_OPERAND (t, 0),
15172                                                TREE_OPERAND (t, 1),
15173                                                strict_overflow_p);
15174
15175     case COND_EXPR:
15176     case CONSTRUCTOR:
15177     case OBJ_TYPE_REF:
15178     case ASSERT_EXPR:
15179     case ADDR_EXPR:
15180     case WITH_SIZE_EXPR:
15181     case SSA_NAME:
15182       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15183
15184     case COMPOUND_EXPR:
15185     case MODIFY_EXPR:
15186     case BIND_EXPR:
15187       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15188                                         strict_overflow_p);
15189
15190     case SAVE_EXPR:
15191       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15192                                         strict_overflow_p);
15193
15194     case CALL_EXPR:
15195       return alloca_call_p (t);
15196
15197     default:
15198       break;
15199     }
15200   return false;
15201 }
15202
15203 /* Return true when T is an address and is known to be nonzero.
15204    Handle warnings about undefined signed overflow.  */
15205
15206 bool
15207 tree_expr_nonzero_p (tree t)
15208 {
15209   bool ret, strict_overflow_p;
15210
15211   strict_overflow_p = false;
15212   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15213   if (strict_overflow_p)
15214     fold_overflow_warning (("assuming signed overflow does not occur when "
15215                             "determining that expression is always "
15216                             "non-zero"),
15217                            WARN_STRICT_OVERFLOW_MISC);
15218   return ret;
15219 }
15220
15221 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15222    attempt to fold the expression to a constant without modifying TYPE,
15223    OP0 or OP1.
15224
15225    If the expression could be simplified to a constant, then return
15226    the constant.  If the expression would not be simplified to a
15227    constant, then return NULL_TREE.  */
15228
15229 tree
15230 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15231 {
15232   tree tem = fold_binary (code, type, op0, op1);
15233   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15234 }
15235
15236 /* Given the components of a unary expression CODE, TYPE and OP0,
15237    attempt to fold the expression to a constant without modifying
15238    TYPE or OP0.
15239
15240    If the expression could be simplified to a constant, then return
15241    the constant.  If the expression would not be simplified to a
15242    constant, then return NULL_TREE.  */
15243
15244 tree
15245 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15246 {
15247   tree tem = fold_unary (code, type, op0);
15248   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15249 }
15250
15251 /* If EXP represents referencing an element in a constant string
15252    (either via pointer arithmetic or array indexing), return the
15253    tree representing the value accessed, otherwise return NULL.  */
15254
15255 tree
15256 fold_read_from_constant_string (tree exp)
15257 {
15258   if ((TREE_CODE (exp) == INDIRECT_REF
15259        || TREE_CODE (exp) == ARRAY_REF)
15260       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15261     {
15262       tree exp1 = TREE_OPERAND (exp, 0);
15263       tree index;
15264       tree string;
15265       location_t loc = EXPR_LOCATION (exp);
15266
15267       if (TREE_CODE (exp) == INDIRECT_REF)
15268         string = string_constant (exp1, &index);
15269       else
15270         {
15271           tree low_bound = array_ref_low_bound (exp);
15272           index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15273
15274           /* Optimize the special-case of a zero lower bound.
15275
15276              We convert the low_bound to sizetype to avoid some problems
15277              with constant folding.  (E.g. suppose the lower bound is 1,
15278              and its mode is QI.  Without the conversion,l (ARRAY
15279              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15280              +INDEX), which becomes (ARRAY+255+INDEX).  Oops!)  */
15281           if (! integer_zerop (low_bound))
15282             index = size_diffop_loc (loc, index,
15283                                  fold_convert_loc (loc, sizetype, low_bound));
15284
15285           string = exp1;
15286         }
15287
15288       if (string
15289           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15290           && TREE_CODE (string) == STRING_CST
15291           && TREE_CODE (index) == INTEGER_CST
15292           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15293           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15294               == MODE_INT)
15295           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15296         return build_int_cst_type (TREE_TYPE (exp),
15297                                    (TREE_STRING_POINTER (string)
15298                                     [TREE_INT_CST_LOW (index)]));
15299     }
15300   return NULL;
15301 }
15302
15303 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15304    an integer constant, real, or fixed-point constant.
15305
15306    TYPE is the type of the result.  */
15307
15308 static tree
15309 fold_negate_const (tree arg0, tree type)
15310 {
15311   tree t = NULL_TREE;
15312
15313   switch (TREE_CODE (arg0))
15314     {
15315     case INTEGER_CST:
15316       {
15317         double_int val = tree_to_double_int (arg0);
15318         int overflow = neg_double (val.low, val.high, &val.low, &val.high);
15319
15320         t = force_fit_type_double (type, val, 1,
15321                                    (overflow | TREE_OVERFLOW (arg0))
15322                                    && !TYPE_UNSIGNED (type));
15323         break;
15324       }
15325
15326     case REAL_CST:
15327       t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15328       break;
15329
15330     case FIXED_CST:
15331       {
15332         FIXED_VALUE_TYPE f;
15333         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15334                                             &(TREE_FIXED_CST (arg0)), NULL,
15335                                             TYPE_SATURATING (type));
15336         t = build_fixed (type, f);
15337         /* Propagate overflow flags.  */
15338         if (overflow_p | TREE_OVERFLOW (arg0))
15339           TREE_OVERFLOW (t) = 1;
15340         break;
15341       }
15342
15343     default:
15344       gcc_unreachable ();
15345     }
15346
15347   return t;
15348 }
15349
15350 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15351    an integer constant or real constant.
15352
15353    TYPE is the type of the result.  */
15354
15355 tree
15356 fold_abs_const (tree arg0, tree type)
15357 {
15358   tree t = NULL_TREE;
15359
15360   switch (TREE_CODE (arg0))
15361     {
15362     case INTEGER_CST:
15363       {
15364         double_int val = tree_to_double_int (arg0);
15365
15366         /* If the value is unsigned or non-negative, then the absolute value
15367            is the same as the ordinary value.  */
15368         if (TYPE_UNSIGNED (type)
15369             || !double_int_negative_p (val))
15370           t = arg0;
15371
15372         /* If the value is negative, then the absolute value is
15373            its negation.  */
15374         else
15375           {
15376             int overflow;
15377
15378             overflow = neg_double (val.low, val.high, &val.low, &val.high);
15379             t = force_fit_type_double (type, val, -1,
15380                                        overflow | TREE_OVERFLOW (arg0));
15381           }
15382       }
15383       break;
15384
15385     case REAL_CST:
15386       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15387         t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15388       else
15389         t =  arg0;
15390       break;
15391
15392     default:
15393       gcc_unreachable ();
15394     }
15395
15396   return t;
15397 }
15398
15399 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15400    constant.  TYPE is the type of the result.  */
15401
15402 static tree
15403 fold_not_const (const_tree arg0, tree type)
15404 {
15405   double_int val;  
15406
15407   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15408
15409   val = double_int_not (tree_to_double_int (arg0));
15410   return force_fit_type_double (type, val, 0, TREE_OVERFLOW (arg0));
15411 }
15412
15413 /* Given CODE, a relational operator, the target type, TYPE and two
15414    constant operands OP0 and OP1, return the result of the
15415    relational operation.  If the result is not a compile time
15416    constant, then return NULL_TREE.  */
15417
15418 static tree
15419 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15420 {
15421   int result, invert;
15422
15423   /* From here on, the only cases we handle are when the result is
15424      known to be a constant.  */
15425
15426   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15427     {
15428       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15429       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15430
15431       /* Handle the cases where either operand is a NaN.  */
15432       if (real_isnan (c0) || real_isnan (c1))
15433         {
15434           switch (code)
15435             {
15436             case EQ_EXPR:
15437             case ORDERED_EXPR:
15438               result = 0;
15439               break;
15440
15441             case NE_EXPR:
15442             case UNORDERED_EXPR:
15443             case UNLT_EXPR:
15444             case UNLE_EXPR:
15445             case UNGT_EXPR:
15446             case UNGE_EXPR:
15447             case UNEQ_EXPR:
15448               result = 1;
15449               break;
15450
15451             case LT_EXPR:
15452             case LE_EXPR:
15453             case GT_EXPR:
15454             case GE_EXPR:
15455             case LTGT_EXPR:
15456               if (flag_trapping_math)
15457                 return NULL_TREE;
15458               result = 0;
15459               break;
15460
15461             default:
15462               gcc_unreachable ();
15463             }
15464
15465           return constant_boolean_node (result, type);
15466         }
15467
15468       return constant_boolean_node (real_compare (code, c0, c1), type);
15469     }
15470
15471   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15472     {
15473       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15474       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15475       return constant_boolean_node (fixed_compare (code, c0, c1), type);
15476     }
15477
15478   /* Handle equality/inequality of complex constants.  */
15479   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15480     {
15481       tree rcond = fold_relational_const (code, type,
15482                                           TREE_REALPART (op0),
15483                                           TREE_REALPART (op1));
15484       tree icond = fold_relational_const (code, type,
15485                                           TREE_IMAGPART (op0),
15486                                           TREE_IMAGPART (op1));
15487       if (code == EQ_EXPR)
15488         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15489       else if (code == NE_EXPR)
15490         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15491       else
15492         return NULL_TREE;
15493     }
15494
15495   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15496
15497      To compute GT, swap the arguments and do LT.
15498      To compute GE, do LT and invert the result.
15499      To compute LE, swap the arguments, do LT and invert the result.
15500      To compute NE, do EQ and invert the result.
15501
15502      Therefore, the code below must handle only EQ and LT.  */
15503
15504   if (code == LE_EXPR || code == GT_EXPR)
15505     {
15506       tree tem = op0;
15507       op0 = op1;
15508       op1 = tem;
15509       code = swap_tree_comparison (code);
15510     }
15511
15512   /* Note that it is safe to invert for real values here because we
15513      have already handled the one case that it matters.  */
15514
15515   invert = 0;
15516   if (code == NE_EXPR || code == GE_EXPR)
15517     {
15518       invert = 1;
15519       code = invert_tree_comparison (code, false);
15520     }
15521
15522   /* Compute a result for LT or EQ if args permit;
15523      Otherwise return T.  */
15524   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15525     {
15526       if (code == EQ_EXPR)
15527         result = tree_int_cst_equal (op0, op1);
15528       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15529         result = INT_CST_LT_UNSIGNED (op0, op1);
15530       else
15531         result = INT_CST_LT (op0, op1);
15532     }
15533   else
15534     return NULL_TREE;
15535
15536   if (invert)
15537     result ^= 1;
15538   return constant_boolean_node (result, type);
15539 }
15540
15541 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15542    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
15543    itself.  */
15544
15545 tree
15546 fold_build_cleanup_point_expr (tree type, tree expr)
15547 {
15548   /* If the expression does not have side effects then we don't have to wrap
15549      it with a cleanup point expression.  */
15550   if (!TREE_SIDE_EFFECTS (expr))
15551     return expr;
15552
15553   /* If the expression is a return, check to see if the expression inside the
15554      return has no side effects or the right hand side of the modify expression
15555      inside the return. If either don't have side effects set we don't need to
15556      wrap the expression in a cleanup point expression.  Note we don't check the
15557      left hand side of the modify because it should always be a return decl.  */
15558   if (TREE_CODE (expr) == RETURN_EXPR)
15559     {
15560       tree op = TREE_OPERAND (expr, 0);
15561       if (!op || !TREE_SIDE_EFFECTS (op))
15562         return expr;
15563       op = TREE_OPERAND (op, 1);
15564       if (!TREE_SIDE_EFFECTS (op))
15565         return expr;
15566     }
15567
15568   return build1 (CLEANUP_POINT_EXPR, type, expr);
15569 }
15570
15571 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15572    of an indirection through OP0, or NULL_TREE if no simplification is
15573    possible.  */
15574
15575 tree
15576 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15577 {
15578   tree sub = op0;
15579   tree subtype;
15580
15581   STRIP_NOPS (sub);
15582   subtype = TREE_TYPE (sub);
15583   if (!POINTER_TYPE_P (subtype))
15584     return NULL_TREE;
15585
15586   if (TREE_CODE (sub) == ADDR_EXPR)
15587     {
15588       tree op = TREE_OPERAND (sub, 0);
15589       tree optype = TREE_TYPE (op);
15590       /* *&CONST_DECL -> to the value of the const decl.  */
15591       if (TREE_CODE (op) == CONST_DECL)
15592         return DECL_INITIAL (op);
15593       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
15594       if (type == optype)
15595         {
15596           tree fop = fold_read_from_constant_string (op);
15597           if (fop)
15598             return fop;
15599           else
15600             return op;
15601         }
15602       /* *(foo *)&fooarray => fooarray[0] */
15603       else if (TREE_CODE (optype) == ARRAY_TYPE
15604                && type == TREE_TYPE (optype)
15605                && (!in_gimple_form
15606                    || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15607         {
15608           tree type_domain = TYPE_DOMAIN (optype);
15609           tree min_val = size_zero_node;
15610           if (type_domain && TYPE_MIN_VALUE (type_domain))
15611             min_val = TYPE_MIN_VALUE (type_domain);
15612           if (in_gimple_form
15613               && TREE_CODE (min_val) != INTEGER_CST)
15614             return NULL_TREE;
15615           return build4_loc (loc, ARRAY_REF, type, op, min_val,
15616                              NULL_TREE, NULL_TREE);
15617         }
15618       /* *(foo *)&complexfoo => __real__ complexfoo */
15619       else if (TREE_CODE (optype) == COMPLEX_TYPE
15620                && type == TREE_TYPE (optype))
15621         return fold_build1_loc (loc, REALPART_EXPR, type, op);
15622       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15623       else if (TREE_CODE (optype) == VECTOR_TYPE
15624                && type == TREE_TYPE (optype))
15625         {
15626           tree part_width = TYPE_SIZE (type);
15627           tree index = bitsize_int (0);
15628           return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
15629         }
15630     }
15631
15632   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15633       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15634     {
15635       tree op00 = TREE_OPERAND (sub, 0);
15636       tree op01 = TREE_OPERAND (sub, 1);
15637
15638       STRIP_NOPS (op00);
15639       if (TREE_CODE (op00) == ADDR_EXPR)
15640         {
15641           tree op00type;
15642           op00 = TREE_OPERAND (op00, 0);
15643           op00type = TREE_TYPE (op00);
15644
15645           /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15646           if (TREE_CODE (op00type) == VECTOR_TYPE
15647               && type == TREE_TYPE (op00type))
15648             {
15649               HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15650               tree part_width = TYPE_SIZE (type);
15651               unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15652               unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15653               tree index = bitsize_int (indexi);
15654
15655               if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (op00type))
15656                 return fold_build3_loc (loc,
15657                                         BIT_FIELD_REF, type, op00,
15658                                         part_width, index);
15659
15660             }
15661           /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15662           else if (TREE_CODE (op00type) == COMPLEX_TYPE
15663                    && type == TREE_TYPE (op00type))
15664             {
15665               tree size = TYPE_SIZE_UNIT (type);
15666               if (tree_int_cst_equal (size, op01))
15667                 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
15668             }
15669           /* ((foo *)&fooarray)[1] => fooarray[1] */
15670           else if (TREE_CODE (op00type) == ARRAY_TYPE
15671                    && type == TREE_TYPE (op00type))
15672             {
15673               tree type_domain = TYPE_DOMAIN (op00type);
15674               tree min_val = size_zero_node;
15675               if (type_domain && TYPE_MIN_VALUE (type_domain))
15676                 min_val = TYPE_MIN_VALUE (type_domain);
15677               op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
15678                                      TYPE_SIZE_UNIT (type));
15679               op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
15680               return build4_loc (loc, ARRAY_REF, type, op00, op01,
15681                                  NULL_TREE, NULL_TREE);
15682             }
15683         }
15684     }
15685
15686   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15687   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15688       && type == TREE_TYPE (TREE_TYPE (subtype))
15689       && (!in_gimple_form
15690           || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15691     {
15692       tree type_domain;
15693       tree min_val = size_zero_node;
15694       sub = build_fold_indirect_ref_loc (loc, sub);
15695       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15696       if (type_domain && TYPE_MIN_VALUE (type_domain))
15697         min_val = TYPE_MIN_VALUE (type_domain);
15698       if (in_gimple_form
15699           && TREE_CODE (min_val) != INTEGER_CST)
15700         return NULL_TREE;
15701       return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
15702                          NULL_TREE);
15703     }
15704
15705   return NULL_TREE;
15706 }
15707
15708 /* Builds an expression for an indirection through T, simplifying some
15709    cases.  */
15710
15711 tree
15712 build_fold_indirect_ref_loc (location_t loc, tree t)
15713 {
15714   tree type = TREE_TYPE (TREE_TYPE (t));
15715   tree sub = fold_indirect_ref_1 (loc, type, t);
15716
15717   if (sub)
15718     return sub;
15719
15720   return build1_loc (loc, INDIRECT_REF, type, t);
15721 }
15722
15723 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
15724
15725 tree
15726 fold_indirect_ref_loc (location_t loc, tree t)
15727 {
15728   tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15729
15730   if (sub)
15731     return sub;
15732   else
15733     return t;
15734 }
15735
15736 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15737    whose result is ignored.  The type of the returned tree need not be
15738    the same as the original expression.  */
15739
15740 tree
15741 fold_ignored_result (tree t)
15742 {
15743   if (!TREE_SIDE_EFFECTS (t))
15744     return integer_zero_node;
15745
15746   for (;;)
15747     switch (TREE_CODE_CLASS (TREE_CODE (t)))
15748       {
15749       case tcc_unary:
15750         t = TREE_OPERAND (t, 0);
15751         break;
15752
15753       case tcc_binary:
15754       case tcc_comparison:
15755         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15756           t = TREE_OPERAND (t, 0);
15757         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15758           t = TREE_OPERAND (t, 1);
15759         else
15760           return t;
15761         break;
15762
15763       case tcc_expression:
15764         switch (TREE_CODE (t))
15765           {
15766           case COMPOUND_EXPR:
15767             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15768               return t;
15769             t = TREE_OPERAND (t, 0);
15770             break;
15771
15772           case COND_EXPR:
15773             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15774                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15775               return t;
15776             t = TREE_OPERAND (t, 0);
15777             break;
15778
15779           default:
15780             return t;
15781           }
15782         break;
15783
15784       default:
15785         return t;
15786       }
15787 }
15788
15789 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15790    This can only be applied to objects of a sizetype.  */
15791
15792 tree
15793 round_up_loc (location_t loc, tree value, int divisor)
15794 {
15795   tree div = NULL_TREE;
15796
15797   gcc_assert (divisor > 0);
15798   if (divisor == 1)
15799     return value;
15800
15801   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15802      have to do anything.  Only do this when we are not given a const,
15803      because in that case, this check is more expensive than just
15804      doing it.  */
15805   if (TREE_CODE (value) != INTEGER_CST)
15806     {
15807       div = build_int_cst (TREE_TYPE (value), divisor);
15808
15809       if (multiple_of_p (TREE_TYPE (value), value, div))
15810         return value;
15811     }
15812
15813   /* If divisor is a power of two, simplify this to bit manipulation.  */
15814   if (divisor == (divisor & -divisor))
15815     {
15816       if (TREE_CODE (value) == INTEGER_CST)
15817         {
15818           double_int val = tree_to_double_int (value);
15819           bool overflow_p;
15820
15821           if ((val.low & (divisor - 1)) == 0)
15822             return value;
15823
15824           overflow_p = TREE_OVERFLOW (value);
15825           val.low &= ~(divisor - 1);
15826           val.low += divisor;
15827           if (val.low == 0)
15828             {
15829               val.high++;
15830               if (val.high == 0)
15831                 overflow_p = true;
15832             }
15833
15834           return force_fit_type_double (TREE_TYPE (value), val,
15835                                         -1, overflow_p);
15836         }
15837       else
15838         {
15839           tree t;
15840
15841           t = build_int_cst (TREE_TYPE (value), divisor - 1);
15842           value = size_binop_loc (loc, PLUS_EXPR, value, t);
15843           t = build_int_cst (TREE_TYPE (value), -divisor);
15844           value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15845         }
15846     }
15847   else
15848     {
15849       if (!div)
15850         div = build_int_cst (TREE_TYPE (value), divisor);
15851       value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
15852       value = size_binop_loc (loc, MULT_EXPR, value, div);
15853     }
15854
15855   return value;
15856 }
15857
15858 /* Likewise, but round down.  */
15859
15860 tree
15861 round_down_loc (location_t loc, tree value, int divisor)
15862 {
15863   tree div = NULL_TREE;
15864
15865   gcc_assert (divisor > 0);
15866   if (divisor == 1)
15867     return value;
15868
15869   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15870      have to do anything.  Only do this when we are not given a const,
15871      because in that case, this check is more expensive than just
15872      doing it.  */
15873   if (TREE_CODE (value) != INTEGER_CST)
15874     {
15875       div = build_int_cst (TREE_TYPE (value), divisor);
15876
15877       if (multiple_of_p (TREE_TYPE (value), value, div))
15878         return value;
15879     }
15880
15881   /* If divisor is a power of two, simplify this to bit manipulation.  */
15882   if (divisor == (divisor & -divisor))
15883     {
15884       tree t;
15885
15886       t = build_int_cst (TREE_TYPE (value), -divisor);
15887       value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15888     }
15889   else
15890     {
15891       if (!div)
15892         div = build_int_cst (TREE_TYPE (value), divisor);
15893       value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
15894       value = size_binop_loc (loc, MULT_EXPR, value, div);
15895     }
15896
15897   return value;
15898 }
15899
15900 /* Returns the pointer to the base of the object addressed by EXP and
15901    extracts the information about the offset of the access, storing it
15902    to PBITPOS and POFFSET.  */
15903
15904 static tree
15905 split_address_to_core_and_offset (tree exp,
15906                                   HOST_WIDE_INT *pbitpos, tree *poffset)
15907 {
15908   tree core;
15909   enum machine_mode mode;
15910   int unsignedp, volatilep;
15911   HOST_WIDE_INT bitsize;
15912   location_t loc = EXPR_LOCATION (exp);
15913
15914   if (TREE_CODE (exp) == ADDR_EXPR)
15915     {
15916       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15917                                   poffset, &mode, &unsignedp, &volatilep,
15918                                   false);
15919       core = build_fold_addr_expr_loc (loc, core);
15920     }
15921   else
15922     {
15923       core = exp;
15924       *pbitpos = 0;
15925       *poffset = NULL_TREE;
15926     }
15927
15928   return core;
15929 }
15930
15931 /* Returns true if addresses of E1 and E2 differ by a constant, false
15932    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
15933
15934 bool
15935 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15936 {
15937   tree core1, core2;
15938   HOST_WIDE_INT bitpos1, bitpos2;
15939   tree toffset1, toffset2, tdiff, type;
15940
15941   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15942   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15943
15944   if (bitpos1 % BITS_PER_UNIT != 0
15945       || bitpos2 % BITS_PER_UNIT != 0
15946       || !operand_equal_p (core1, core2, 0))
15947     return false;
15948
15949   if (toffset1 && toffset2)
15950     {
15951       type = TREE_TYPE (toffset1);
15952       if (type != TREE_TYPE (toffset2))
15953         toffset2 = fold_convert (type, toffset2);
15954
15955       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15956       if (!cst_and_fits_in_hwi (tdiff))
15957         return false;
15958
15959       *diff = int_cst_value (tdiff);
15960     }
15961   else if (toffset1 || toffset2)
15962     {
15963       /* If only one of the offsets is non-constant, the difference cannot
15964          be a constant.  */
15965       return false;
15966     }
15967   else
15968     *diff = 0;
15969
15970   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15971   return true;
15972 }
15973
15974 /* Simplify the floating point expression EXP when the sign of the
15975    result is not significant.  Return NULL_TREE if no simplification
15976    is possible.  */
15977
15978 tree
15979 fold_strip_sign_ops (tree exp)
15980 {
15981   tree arg0, arg1;
15982   location_t loc = EXPR_LOCATION (exp);
15983
15984   switch (TREE_CODE (exp))
15985     {
15986     case ABS_EXPR:
15987     case NEGATE_EXPR:
15988       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15989       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15990
15991     case MULT_EXPR:
15992     case RDIV_EXPR:
15993       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15994         return NULL_TREE;
15995       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15996       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15997       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15998         return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
15999                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
16000                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
16001       break;
16002
16003     case COMPOUND_EXPR:
16004       arg0 = TREE_OPERAND (exp, 0);
16005       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16006       if (arg1)
16007         return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
16008       break;
16009
16010     case COND_EXPR:
16011       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16012       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
16013       if (arg0 || arg1)
16014         return fold_build3_loc (loc,
16015                             COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
16016                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
16017                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
16018       break;
16019
16020     case CALL_EXPR:
16021       {
16022         const enum built_in_function fcode = builtin_mathfn_code (exp);
16023         switch (fcode)
16024         {
16025         CASE_FLT_FN (BUILT_IN_COPYSIGN):
16026           /* Strip copysign function call, return the 1st argument. */
16027           arg0 = CALL_EXPR_ARG (exp, 0);
16028           arg1 = CALL_EXPR_ARG (exp, 1);
16029           return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
16030
16031         default:
16032           /* Strip sign ops from the argument of "odd" math functions.  */
16033           if (negate_mathfn_p (fcode))
16034             {
16035               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
16036               if (arg0)
16037                 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
16038             }
16039           break;
16040         }
16041       }
16042       break;
16043
16044     default:
16045       break;
16046     }
16047   return NULL_TREE;
16048 }