OSDN Git Service

2014-05-07 Richard Biener <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / fold-const.c
1 /* Fold a constant sub-tree into a single node for C-compiler
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4    2012 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /*@@ This file should be rewritten to use an arbitrary precision
23   @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
24   @@ Perhaps the routines could also be used for bc/dc, and made a lib.
25   @@ The routines that translate from the ap rep should
26   @@ warn if precision et. al. is lost.
27   @@ This would also make life easier when this technology is used
28   @@ for cross-compilers.  */
29
30 /* The entry points in this file are fold, size_int_wide and size_binop.
31
32    fold takes a tree as argument and returns a simplified tree.
33
34    size_binop takes a tree code for an arithmetic operation
35    and two operands that are trees, and produces a tree for the
36    result, assuming the type comes from `sizetype'.
37
38    size_int takes an integer value, and creates a tree constant
39    with type from `sizetype'.
40
41    Note: Since the folders get called on non-gimple code as well as
42    gimple code, we need to handle GIMPLE tuples as well as their
43    corresponding tree equivalents.  */
44
45 #include "config.h"
46 #include "system.h"
47 #include "coretypes.h"
48 #include "tm.h"
49 #include "flags.h"
50 #include "tree.h"
51 #include "realmpfr.h"
52 #include "rtl.h"
53 #include "expr.h"
54 #include "tm_p.h"
55 #include "target.h"
56 #include "diagnostic-core.h"
57 #include "intl.h"
58 #include "ggc.h"
59 #include "hashtab.h"
60 #include "langhooks.h"
61 #include "md5.h"
62 #include "gimple.h"
63 #include "tree-flow.h"
64
65 /* Nonzero if we are folding constants inside an initializer; zero
66    otherwise.  */
67 int folding_initializer = 0;
68
69 /* The following constants represent a bit based encoding of GCC's
70    comparison operators.  This encoding simplifies transformations
71    on relational comparison operators, such as AND and OR.  */
72 enum comparison_code {
73   COMPCODE_FALSE = 0,
74   COMPCODE_LT = 1,
75   COMPCODE_EQ = 2,
76   COMPCODE_LE = 3,
77   COMPCODE_GT = 4,
78   COMPCODE_LTGT = 5,
79   COMPCODE_GE = 6,
80   COMPCODE_ORD = 7,
81   COMPCODE_UNORD = 8,
82   COMPCODE_UNLT = 9,
83   COMPCODE_UNEQ = 10,
84   COMPCODE_UNLE = 11,
85   COMPCODE_UNGT = 12,
86   COMPCODE_NE = 13,
87   COMPCODE_UNGE = 14,
88   COMPCODE_TRUE = 15
89 };
90
91 static bool negate_mathfn_p (enum built_in_function);
92 static bool negate_expr_p (tree);
93 static tree negate_expr (tree);
94 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
95 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
96 static tree const_binop (enum tree_code, tree, tree);
97 static enum comparison_code comparison_to_compcode (enum tree_code);
98 static enum tree_code compcode_to_comparison (enum comparison_code);
99 static int operand_equal_for_comparison_p (tree, tree, tree);
100 static int twoval_comparison_p (tree, tree *, tree *, int *);
101 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
102 static tree pedantic_omit_one_operand_loc (location_t, tree, tree, tree);
103 static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree);
104 static tree make_bit_field_ref (location_t, tree, tree,
105                                 HOST_WIDE_INT, HOST_WIDE_INT, int);
106 static tree optimize_bit_field_compare (location_t, enum tree_code,
107                                         tree, tree, tree);
108 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
109                                     HOST_WIDE_INT *,
110                                     enum machine_mode *, int *, int *,
111                                     tree *, tree *);
112 static int all_ones_mask_p (const_tree, int);
113 static tree sign_bit_p (tree, const_tree);
114 static int simple_operand_p (const_tree);
115 static bool simple_operand_p_2 (tree);
116 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
117 static tree range_predecessor (tree);
118 static tree range_successor (tree);
119 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
120 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
121 static tree unextend (tree, int, int, tree);
122 static tree optimize_minmax_comparison (location_t, enum tree_code,
123                                         tree, tree, tree);
124 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
125 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
126 static tree fold_binary_op_with_conditional_arg (location_t,
127                                                  enum tree_code, tree,
128                                                  tree, tree,
129                                                  tree, tree, int);
130 static tree fold_mathfn_compare (location_t,
131                                  enum built_in_function, enum tree_code,
132                                  tree, tree, tree);
133 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
134 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
135 static bool reorder_operands_p (const_tree, const_tree);
136 static tree fold_negate_const (tree, tree);
137 static tree fold_not_const (const_tree, tree);
138 static tree fold_relational_const (enum tree_code, tree, tree, tree);
139 static tree fold_convert_const (enum tree_code, tree, tree);
140
141 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
142    Otherwise, return LOC.  */
143
144 static location_t
145 expr_location_or (tree t, location_t loc)
146 {
147   location_t tloc = EXPR_LOCATION (t);
148   return tloc != UNKNOWN_LOCATION ? tloc : loc;
149 }
150
151 /* Similar to protected_set_expr_location, but never modify x in place,
152    if location can and needs to be set, unshare it.  */
153
154 static inline tree
155 protected_set_expr_location_unshare (tree x, location_t loc)
156 {
157   if (CAN_HAVE_LOCATION_P (x)
158       && EXPR_LOCATION (x) != loc
159       && !(TREE_CODE (x) == SAVE_EXPR
160            || TREE_CODE (x) == TARGET_EXPR
161            || TREE_CODE (x) == BIND_EXPR))
162     {
163       x = copy_node (x);
164       SET_EXPR_LOCATION (x, loc);
165     }
166   return x;
167 }
168
169
170 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
171    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
172    and SUM1.  Then this yields nonzero if overflow occurred during the
173    addition.
174
175    Overflow occurs if A and B have the same sign, but A and SUM differ in
176    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
177    sign.  */
178 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
179 \f
180 /* If ARG2 divides ARG1 with zero remainder, carries out the division
181    of type CODE and returns the quotient.
182    Otherwise returns NULL_TREE.  */
183
184 tree
185 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
186 {
187   double_int quo, rem;
188   int uns;
189
190   /* The sign of the division is according to operand two, that
191      does the correct thing for POINTER_PLUS_EXPR where we want
192      a signed division.  */
193   uns = TYPE_UNSIGNED (TREE_TYPE (arg2));
194   if (TREE_CODE (TREE_TYPE (arg2)) == INTEGER_TYPE
195       && TYPE_IS_SIZETYPE (TREE_TYPE (arg2)))
196     uns = false;
197
198   quo = double_int_divmod (tree_to_double_int (arg1),
199                            tree_to_double_int (arg2),
200                            uns, code, &rem);
201
202   if (double_int_zero_p (rem))
203     return build_int_cst_wide (TREE_TYPE (arg1), quo.low, quo.high);
204
205   return NULL_TREE; 
206 }
207 \f
208 /* This is nonzero if we should defer warnings about undefined
209    overflow.  This facility exists because these warnings are a
210    special case.  The code to estimate loop iterations does not want
211    to issue any warnings, since it works with expressions which do not
212    occur in user code.  Various bits of cleanup code call fold(), but
213    only use the result if it has certain characteristics (e.g., is a
214    constant); that code only wants to issue a warning if the result is
215    used.  */
216
217 static int fold_deferring_overflow_warnings;
218
219 /* If a warning about undefined overflow is deferred, this is the
220    warning.  Note that this may cause us to turn two warnings into
221    one, but that is fine since it is sufficient to only give one
222    warning per expression.  */
223
224 static const char* fold_deferred_overflow_warning;
225
226 /* If a warning about undefined overflow is deferred, this is the
227    level at which the warning should be emitted.  */
228
229 static enum warn_strict_overflow_code fold_deferred_overflow_code;
230
231 /* Start deferring overflow warnings.  We could use a stack here to
232    permit nested calls, but at present it is not necessary.  */
233
234 void
235 fold_defer_overflow_warnings (void)
236 {
237   ++fold_deferring_overflow_warnings;
238 }
239
240 /* Stop deferring overflow warnings.  If there is a pending warning,
241    and ISSUE is true, then issue the warning if appropriate.  STMT is
242    the statement with which the warning should be associated (used for
243    location information); STMT may be NULL.  CODE is the level of the
244    warning--a warn_strict_overflow_code value.  This function will use
245    the smaller of CODE and the deferred code when deciding whether to
246    issue the warning.  CODE may be zero to mean to always use the
247    deferred code.  */
248
249 void
250 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
251 {
252   const char *warnmsg;
253   location_t locus;
254
255   gcc_assert (fold_deferring_overflow_warnings > 0);
256   --fold_deferring_overflow_warnings;
257   if (fold_deferring_overflow_warnings > 0)
258     {
259       if (fold_deferred_overflow_warning != NULL
260           && code != 0
261           && code < (int) fold_deferred_overflow_code)
262         fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
263       return;
264     }
265
266   warnmsg = fold_deferred_overflow_warning;
267   fold_deferred_overflow_warning = NULL;
268
269   if (!issue || warnmsg == NULL)
270     return;
271
272   if (gimple_no_warning_p (stmt))
273     return;
274
275   /* Use the smallest code level when deciding to issue the
276      warning.  */
277   if (code == 0 || code > (int) fold_deferred_overflow_code)
278     code = fold_deferred_overflow_code;
279
280   if (!issue_strict_overflow_warning (code))
281     return;
282
283   if (stmt == NULL)
284     locus = input_location;
285   else
286     locus = gimple_location (stmt);
287   warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
288 }
289
290 /* Stop deferring overflow warnings, ignoring any deferred
291    warnings.  */
292
293 void
294 fold_undefer_and_ignore_overflow_warnings (void)
295 {
296   fold_undefer_overflow_warnings (false, NULL, 0);
297 }
298
299 /* Whether we are deferring overflow warnings.  */
300
301 bool
302 fold_deferring_overflow_warnings_p (void)
303 {
304   return fold_deferring_overflow_warnings > 0;
305 }
306
307 /* This is called when we fold something based on the fact that signed
308    overflow is undefined.  */
309
310 static void
311 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
312 {
313   if (fold_deferring_overflow_warnings > 0)
314     {
315       if (fold_deferred_overflow_warning == NULL
316           || wc < fold_deferred_overflow_code)
317         {
318           fold_deferred_overflow_warning = gmsgid;
319           fold_deferred_overflow_code = wc;
320         }
321     }
322   else if (issue_strict_overflow_warning (wc))
323     warning (OPT_Wstrict_overflow, gmsgid);
324 }
325 \f
326 /* Return true if the built-in mathematical function specified by CODE
327    is odd, i.e. -f(x) == f(-x).  */
328
329 static bool
330 negate_mathfn_p (enum built_in_function code)
331 {
332   switch (code)
333     {
334     CASE_FLT_FN (BUILT_IN_ASIN):
335     CASE_FLT_FN (BUILT_IN_ASINH):
336     CASE_FLT_FN (BUILT_IN_ATAN):
337     CASE_FLT_FN (BUILT_IN_ATANH):
338     CASE_FLT_FN (BUILT_IN_CASIN):
339     CASE_FLT_FN (BUILT_IN_CASINH):
340     CASE_FLT_FN (BUILT_IN_CATAN):
341     CASE_FLT_FN (BUILT_IN_CATANH):
342     CASE_FLT_FN (BUILT_IN_CBRT):
343     CASE_FLT_FN (BUILT_IN_CPROJ):
344     CASE_FLT_FN (BUILT_IN_CSIN):
345     CASE_FLT_FN (BUILT_IN_CSINH):
346     CASE_FLT_FN (BUILT_IN_CTAN):
347     CASE_FLT_FN (BUILT_IN_CTANH):
348     CASE_FLT_FN (BUILT_IN_ERF):
349     CASE_FLT_FN (BUILT_IN_LLROUND):
350     CASE_FLT_FN (BUILT_IN_LROUND):
351     CASE_FLT_FN (BUILT_IN_ROUND):
352     CASE_FLT_FN (BUILT_IN_SIN):
353     CASE_FLT_FN (BUILT_IN_SINH):
354     CASE_FLT_FN (BUILT_IN_TAN):
355     CASE_FLT_FN (BUILT_IN_TANH):
356     CASE_FLT_FN (BUILT_IN_TRUNC):
357       return true;
358
359     CASE_FLT_FN (BUILT_IN_LLRINT):
360     CASE_FLT_FN (BUILT_IN_LRINT):
361     CASE_FLT_FN (BUILT_IN_NEARBYINT):
362     CASE_FLT_FN (BUILT_IN_RINT):
363       return !flag_rounding_math;
364
365     default:
366       break;
367     }
368   return false;
369 }
370
371 /* Check whether we may negate an integer constant T without causing
372    overflow.  */
373
374 bool
375 may_negate_without_overflow_p (const_tree t)
376 {
377   unsigned HOST_WIDE_INT val;
378   unsigned int prec;
379   tree type;
380
381   gcc_assert (TREE_CODE (t) == INTEGER_CST);
382
383   type = TREE_TYPE (t);
384   if (TYPE_UNSIGNED (type))
385     return false;
386
387   prec = TYPE_PRECISION (type);
388   if (prec > HOST_BITS_PER_WIDE_INT)
389     {
390       if (TREE_INT_CST_LOW (t) != 0)
391         return true;
392       prec -= HOST_BITS_PER_WIDE_INT;
393       val = TREE_INT_CST_HIGH (t);
394     }
395   else
396     val = TREE_INT_CST_LOW (t);
397   if (prec < HOST_BITS_PER_WIDE_INT)
398     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
399   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
400 }
401
402 /* Determine whether an expression T can be cheaply negated using
403    the function negate_expr without introducing undefined overflow.  */
404
405 static bool
406 negate_expr_p (tree t)
407 {
408   tree type;
409
410   if (t == 0)
411     return false;
412
413   type = TREE_TYPE (t);
414
415   STRIP_SIGN_NOPS (t);
416   switch (TREE_CODE (t))
417     {
418     case INTEGER_CST:
419       if (TYPE_OVERFLOW_WRAPS (type))
420         return true;
421
422       /* Check that -CST will not overflow type.  */
423       return may_negate_without_overflow_p (t);
424     case BIT_NOT_EXPR:
425       return (INTEGRAL_TYPE_P (type)
426               && TYPE_OVERFLOW_WRAPS (type));
427
428     case FIXED_CST:
429     case NEGATE_EXPR:
430       return true;
431
432     case REAL_CST:
433       /* We want to canonicalize to positive real constants.  Pretend
434          that only negative ones can be easily negated.  */
435       return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
436
437     case COMPLEX_CST:
438       return negate_expr_p (TREE_REALPART (t))
439              && negate_expr_p (TREE_IMAGPART (t));
440
441     case COMPLEX_EXPR:
442       return negate_expr_p (TREE_OPERAND (t, 0))
443              && negate_expr_p (TREE_OPERAND (t, 1));
444
445     case CONJ_EXPR:
446       return negate_expr_p (TREE_OPERAND (t, 0));
447
448     case PLUS_EXPR:
449       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
450           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
451         return false;
452       /* -(A + B) -> (-B) - A.  */
453       if (negate_expr_p (TREE_OPERAND (t, 1))
454           && reorder_operands_p (TREE_OPERAND (t, 0),
455                                  TREE_OPERAND (t, 1)))
456         return true;
457       /* -(A + B) -> (-A) - B.  */
458       return negate_expr_p (TREE_OPERAND (t, 0));
459
460     case MINUS_EXPR:
461       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
462       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
463              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
464              && reorder_operands_p (TREE_OPERAND (t, 0),
465                                     TREE_OPERAND (t, 1));
466
467     case MULT_EXPR:
468       if (TYPE_UNSIGNED (TREE_TYPE (t)))
469         break;
470
471       /* Fall through.  */
472
473     case RDIV_EXPR:
474       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
475         return negate_expr_p (TREE_OPERAND (t, 1))
476                || negate_expr_p (TREE_OPERAND (t, 0));
477       break;
478
479     case TRUNC_DIV_EXPR:
480     case ROUND_DIV_EXPR:
481     case FLOOR_DIV_EXPR:
482     case CEIL_DIV_EXPR:
483     case EXACT_DIV_EXPR:
484       /* In general we can't negate A / B, because if A is INT_MIN and
485          B is 1, we may turn this into INT_MIN / -1 which is undefined
486          and actually traps on some architectures.  But if overflow is
487          undefined, we can negate, because - (INT_MIN / 1) is an
488          overflow.  */
489       if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
490         {
491           if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
492             break;
493           /* If overflow is undefined then we have to be careful because
494              we ask whether it's ok to associate the negate with the
495              division which is not ok for example for
496              -((a - b) / c) where (-(a - b)) / c may invoke undefined
497              overflow because of negating INT_MIN.  So do not use
498              negate_expr_p here but open-code the two important cases.  */
499           if (TREE_CODE (TREE_OPERAND (t, 0)) == NEGATE_EXPR
500               || (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
501                   && may_negate_without_overflow_p (TREE_OPERAND (t, 0))))
502             return true;
503         }
504       else if (negate_expr_p (TREE_OPERAND (t, 0)))
505         return true;
506       return negate_expr_p (TREE_OPERAND (t, 1));
507
508     case NOP_EXPR:
509       /* Negate -((double)float) as (double)(-float).  */
510       if (TREE_CODE (type) == REAL_TYPE)
511         {
512           tree tem = strip_float_extensions (t);
513           if (tem != t)
514             return negate_expr_p (tem);
515         }
516       break;
517
518     case CALL_EXPR:
519       /* Negate -f(x) as f(-x).  */
520       if (negate_mathfn_p (builtin_mathfn_code (t)))
521         return negate_expr_p (CALL_EXPR_ARG (t, 0));
522       break;
523
524     case RSHIFT_EXPR:
525       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
526       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
527         {
528           tree op1 = TREE_OPERAND (t, 1);
529           if (TREE_INT_CST_HIGH (op1) == 0
530               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
531                  == TREE_INT_CST_LOW (op1))
532             return true;
533         }
534       break;
535
536     default:
537       break;
538     }
539   return false;
540 }
541
542 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
543    simplification is possible.
544    If negate_expr_p would return true for T, NULL_TREE will never be
545    returned.  */
546
547 static tree
548 fold_negate_expr (location_t loc, tree t)
549 {
550   tree type = TREE_TYPE (t);
551   tree tem;
552
553   switch (TREE_CODE (t))
554     {
555     /* Convert - (~A) to A + 1.  */
556     case BIT_NOT_EXPR:
557       if (INTEGRAL_TYPE_P (type))
558         return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
559                             build_int_cst (type, 1));
560       break;
561
562     case INTEGER_CST:
563       tem = fold_negate_const (t, type);
564       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
565           || !TYPE_OVERFLOW_TRAPS (type))
566         return tem;
567       break;
568
569     case REAL_CST:
570       tem = fold_negate_const (t, type);
571       /* Two's complement FP formats, such as c4x, may overflow.  */
572       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
573         return tem;
574       break;
575
576     case FIXED_CST:
577       tem = fold_negate_const (t, type);
578       return tem;
579
580     case COMPLEX_CST:
581       {
582         tree rpart = negate_expr (TREE_REALPART (t));
583         tree ipart = negate_expr (TREE_IMAGPART (t));
584
585         if ((TREE_CODE (rpart) == REAL_CST
586              && TREE_CODE (ipart) == REAL_CST)
587             || (TREE_CODE (rpart) == INTEGER_CST
588                 && TREE_CODE (ipart) == INTEGER_CST))
589           return build_complex (type, rpart, ipart);
590       }
591       break;
592
593     case COMPLEX_EXPR:
594       if (negate_expr_p (t))
595         return fold_build2_loc (loc, COMPLEX_EXPR, type,
596                             fold_negate_expr (loc, TREE_OPERAND (t, 0)),
597                             fold_negate_expr (loc, TREE_OPERAND (t, 1)));
598       break;
599
600     case CONJ_EXPR:
601       if (negate_expr_p (t))
602         return fold_build1_loc (loc, CONJ_EXPR, type,
603                             fold_negate_expr (loc, TREE_OPERAND (t, 0)));
604       break;
605
606     case NEGATE_EXPR:
607       return TREE_OPERAND (t, 0);
608
609     case PLUS_EXPR:
610       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
611           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
612         {
613           /* -(A + B) -> (-B) - A.  */
614           if (negate_expr_p (TREE_OPERAND (t, 1))
615               && reorder_operands_p (TREE_OPERAND (t, 0),
616                                      TREE_OPERAND (t, 1)))
617             {
618               tem = negate_expr (TREE_OPERAND (t, 1));
619               return fold_build2_loc (loc, MINUS_EXPR, type,
620                                   tem, TREE_OPERAND (t, 0));
621             }
622
623           /* -(A + B) -> (-A) - B.  */
624           if (negate_expr_p (TREE_OPERAND (t, 0)))
625             {
626               tem = negate_expr (TREE_OPERAND (t, 0));
627               return fold_build2_loc (loc, MINUS_EXPR, type,
628                                   tem, TREE_OPERAND (t, 1));
629             }
630         }
631       break;
632
633     case MINUS_EXPR:
634       /* - (A - B) -> B - A  */
635       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
636           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
637           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
638         return fold_build2_loc (loc, MINUS_EXPR, type,
639                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
640       break;
641
642     case MULT_EXPR:
643       if (TYPE_UNSIGNED (type))
644         break;
645
646       /* Fall through.  */
647
648     case RDIV_EXPR:
649       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
650         {
651           tem = TREE_OPERAND (t, 1);
652           if (negate_expr_p (tem))
653             return fold_build2_loc (loc, TREE_CODE (t), type,
654                                 TREE_OPERAND (t, 0), negate_expr (tem));
655           tem = TREE_OPERAND (t, 0);
656           if (negate_expr_p (tem))
657             return fold_build2_loc (loc, TREE_CODE (t), type,
658                                 negate_expr (tem), TREE_OPERAND (t, 1));
659         }
660       break;
661
662     case TRUNC_DIV_EXPR:
663     case ROUND_DIV_EXPR:
664     case FLOOR_DIV_EXPR:
665     case CEIL_DIV_EXPR:
666     case EXACT_DIV_EXPR:
667       /* In general we can't negate A / B, because if A is INT_MIN and
668          B is 1, we may turn this into INT_MIN / -1 which is undefined
669          and actually traps on some architectures.  But if overflow is
670          undefined, we can negate, because - (INT_MIN / 1) is an
671          overflow.  */
672       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
673         {
674           const char * const warnmsg = G_("assuming signed overflow does not "
675                                           "occur when negating a division");
676           tem = TREE_OPERAND (t, 1);
677           if (negate_expr_p (tem))
678             {
679               if (INTEGRAL_TYPE_P (type)
680                   && (TREE_CODE (tem) != INTEGER_CST
681                       || integer_onep (tem)))
682                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
683               return fold_build2_loc (loc, TREE_CODE (t), type,
684                                   TREE_OPERAND (t, 0), negate_expr (tem));
685             }
686           /* If overflow is undefined then we have to be careful because
687              we ask whether it's ok to associate the negate with the
688              division which is not ok for example for
689              -((a - b) / c) where (-(a - b)) / c may invoke undefined
690              overflow because of negating INT_MIN.  So do not use
691              negate_expr_p here but open-code the two important cases.  */
692           tem = TREE_OPERAND (t, 0);
693           if ((INTEGRAL_TYPE_P (type)
694                && (TREE_CODE (tem) == NEGATE_EXPR
695                    || (TREE_CODE (tem) == INTEGER_CST
696                        && may_negate_without_overflow_p (tem))))
697               || !INTEGRAL_TYPE_P (type))
698             return fold_build2_loc (loc, TREE_CODE (t), type,
699                                     negate_expr (tem), TREE_OPERAND (t, 1));
700         }
701       break;
702
703     case NOP_EXPR:
704       /* Convert -((double)float) into (double)(-float).  */
705       if (TREE_CODE (type) == REAL_TYPE)
706         {
707           tem = strip_float_extensions (t);
708           if (tem != t && negate_expr_p (tem))
709             return fold_convert_loc (loc, type, negate_expr (tem));
710         }
711       break;
712
713     case CALL_EXPR:
714       /* Negate -f(x) as f(-x).  */
715       if (negate_mathfn_p (builtin_mathfn_code (t))
716           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
717         {
718           tree fndecl, arg;
719
720           fndecl = get_callee_fndecl (t);
721           arg = negate_expr (CALL_EXPR_ARG (t, 0));
722           return build_call_expr_loc (loc, fndecl, 1, arg);
723         }
724       break;
725
726     case RSHIFT_EXPR:
727       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
728       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
729         {
730           tree op1 = TREE_OPERAND (t, 1);
731           if (TREE_INT_CST_HIGH (op1) == 0
732               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
733                  == TREE_INT_CST_LOW (op1))
734             {
735               tree ntype = TYPE_UNSIGNED (type)
736                            ? signed_type_for (type)
737                            : unsigned_type_for (type);
738               tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
739               temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
740               return fold_convert_loc (loc, type, temp);
741             }
742         }
743       break;
744
745     default:
746       break;
747     }
748
749   return NULL_TREE;
750 }
751
752 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
753    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
754    return NULL_TREE. */
755
756 static tree
757 negate_expr (tree t)
758 {
759   tree type, tem;
760   location_t loc;
761
762   if (t == NULL_TREE)
763     return NULL_TREE;
764
765   loc = EXPR_LOCATION (t);
766   type = TREE_TYPE (t);
767   STRIP_SIGN_NOPS (t);
768
769   tem = fold_negate_expr (loc, t);
770   if (!tem)
771     tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
772   return fold_convert_loc (loc, type, tem);
773 }
774 \f
775 /* Split a tree IN into a constant, literal and variable parts that could be
776    combined with CODE to make IN.  "constant" means an expression with
777    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
778    commutative arithmetic operation.  Store the constant part into *CONP,
779    the literal in *LITP and return the variable part.  If a part isn't
780    present, set it to null.  If the tree does not decompose in this way,
781    return the entire tree as the variable part and the other parts as null.
782
783    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
784    case, we negate an operand that was subtracted.  Except if it is a
785    literal for which we use *MINUS_LITP instead.
786
787    If NEGATE_P is true, we are negating all of IN, again except a literal
788    for which we use *MINUS_LITP instead.
789
790    If IN is itself a literal or constant, return it as appropriate.
791
792    Note that we do not guarantee that any of the three values will be the
793    same type as IN, but they will have the same signedness and mode.  */
794
795 static tree
796 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
797             tree *minus_litp, int negate_p)
798 {
799   tree var = 0;
800
801   *conp = 0;
802   *litp = 0;
803   *minus_litp = 0;
804
805   /* Strip any conversions that don't change the machine mode or signedness.  */
806   STRIP_SIGN_NOPS (in);
807
808   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
809       || TREE_CODE (in) == FIXED_CST)
810     *litp = in;
811   else if (TREE_CODE (in) == code
812            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
813                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
814                /* We can associate addition and subtraction together (even
815                   though the C standard doesn't say so) for integers because
816                   the value is not affected.  For reals, the value might be
817                   affected, so we can't.  */
818                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
819                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
820     {
821       tree op0 = TREE_OPERAND (in, 0);
822       tree op1 = TREE_OPERAND (in, 1);
823       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
824       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
825
826       /* First see if either of the operands is a literal, then a constant.  */
827       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
828           || TREE_CODE (op0) == FIXED_CST)
829         *litp = op0, op0 = 0;
830       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
831                || TREE_CODE (op1) == FIXED_CST)
832         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
833
834       if (op0 != 0 && TREE_CONSTANT (op0))
835         *conp = op0, op0 = 0;
836       else if (op1 != 0 && TREE_CONSTANT (op1))
837         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
838
839       /* If we haven't dealt with either operand, this is not a case we can
840          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
841       if (op0 != 0 && op1 != 0)
842         var = in;
843       else if (op0 != 0)
844         var = op0;
845       else
846         var = op1, neg_var_p = neg1_p;
847
848       /* Now do any needed negations.  */
849       if (neg_litp_p)
850         *minus_litp = *litp, *litp = 0;
851       if (neg_conp_p)
852         *conp = negate_expr (*conp);
853       if (neg_var_p)
854         var = negate_expr (var);
855     }
856   else if (TREE_CONSTANT (in))
857     *conp = in;
858   else
859     var = in;
860
861   if (negate_p)
862     {
863       if (*litp)
864         *minus_litp = *litp, *litp = 0;
865       else if (*minus_litp)
866         *litp = *minus_litp, *minus_litp = 0;
867       *conp = negate_expr (*conp);
868       var = negate_expr (var);
869     }
870
871   return var;
872 }
873
874 /* Re-associate trees split by the above function.  T1 and T2 are
875    either expressions to associate or null.  Return the new
876    expression, if any.  LOC is the location of the new expression.  If
877    we build an operation, do it in TYPE and with CODE.  */
878
879 static tree
880 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
881 {
882   if (t1 == 0)
883     return t2;
884   else if (t2 == 0)
885     return t1;
886
887   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
888      try to fold this since we will have infinite recursion.  But do
889      deal with any NEGATE_EXPRs.  */
890   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
891       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
892     {
893       if (code == PLUS_EXPR)
894         {
895           if (TREE_CODE (t1) == NEGATE_EXPR)
896             return build2_loc (loc, MINUS_EXPR, type,
897                                fold_convert_loc (loc, type, t2),
898                                fold_convert_loc (loc, type,
899                                                  TREE_OPERAND (t1, 0)));
900           else if (TREE_CODE (t2) == NEGATE_EXPR)
901             return build2_loc (loc, MINUS_EXPR, type,
902                                fold_convert_loc (loc, type, t1),
903                                fold_convert_loc (loc, type,
904                                                  TREE_OPERAND (t2, 0)));
905           else if (integer_zerop (t2))
906             return fold_convert_loc (loc, type, t1);
907         }
908       else if (code == MINUS_EXPR)
909         {
910           if (integer_zerop (t2))
911             return fold_convert_loc (loc, type, t1);
912         }
913
914       return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
915                          fold_convert_loc (loc, type, t2));
916     }
917
918   return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
919                           fold_convert_loc (loc, type, t2));
920 }
921 \f
922 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
923    for use in int_const_binop, size_binop and size_diffop.  */
924
925 static bool
926 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
927 {
928   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
929     return false;
930   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
931     return false;
932
933   switch (code)
934     {
935     case LSHIFT_EXPR:
936     case RSHIFT_EXPR:
937     case LROTATE_EXPR:
938     case RROTATE_EXPR:
939       return true;
940
941     default:
942       break;
943     }
944
945   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
946          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
947          && TYPE_MODE (type1) == TYPE_MODE (type2);
948 }
949
950
951 /* Combine two integer constants ARG1 and ARG2 under operation CODE
952    to produce a new constant.  Return NULL_TREE if we don't know how
953    to evaluate CODE at compile-time.  */
954
955 tree
956 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
957 {
958   double_int op1, op2, res, tmp;
959   tree t;
960   tree type = TREE_TYPE (arg1);
961   bool uns = TYPE_UNSIGNED (type);
962   bool is_sizetype
963     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
964   bool overflow = false;
965
966   op1 = tree_to_double_int (arg1);
967   op2 = tree_to_double_int (arg2);
968
969   switch (code)
970     {
971     case BIT_IOR_EXPR:
972       res = double_int_ior (op1, op2);
973       break;
974
975     case BIT_XOR_EXPR:
976       res = double_int_xor (op1, op2);
977       break;
978
979     case BIT_AND_EXPR:
980       res = double_int_and (op1, op2);
981       break;
982
983     case RSHIFT_EXPR:
984       res = double_int_rshift (op1, double_int_to_shwi (op2),
985                                TYPE_PRECISION (type), !uns);
986       break;
987
988     case LSHIFT_EXPR:
989       /* It's unclear from the C standard whether shifts can overflow.
990          The following code ignores overflow; perhaps a C standard
991          interpretation ruling is needed.  */
992       res = double_int_lshift (op1, double_int_to_shwi (op2),
993                                TYPE_PRECISION (type), !uns);
994       break;
995
996     case RROTATE_EXPR:
997       res = double_int_rrotate (op1, double_int_to_shwi (op2),
998                                 TYPE_PRECISION (type));
999       break;
1000
1001     case LROTATE_EXPR:
1002       res = double_int_lrotate (op1, double_int_to_shwi (op2),
1003                                 TYPE_PRECISION (type));
1004       break;
1005
1006     case PLUS_EXPR:
1007       overflow = add_double (op1.low, op1.high, op2.low, op2.high,
1008                              &res.low, &res.high);
1009       break;
1010
1011     case MINUS_EXPR:
1012       neg_double (op2.low, op2.high, &res.low, &res.high);
1013       add_double (op1.low, op1.high, res.low, res.high,
1014                   &res.low, &res.high);
1015       overflow = OVERFLOW_SUM_SIGN (res.high, op2.high, op1.high);
1016       break;
1017
1018     case MULT_EXPR:
1019       overflow = mul_double (op1.low, op1.high, op2.low, op2.high,
1020                              &res.low, &res.high);
1021       break;
1022
1023     case TRUNC_DIV_EXPR:
1024     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1025     case EXACT_DIV_EXPR:
1026       /* This is a shortcut for a common special case.  */
1027       if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1028           && !TREE_OVERFLOW (arg1)
1029           && !TREE_OVERFLOW (arg2)
1030           && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1031         {
1032           if (code == CEIL_DIV_EXPR)
1033             op1.low += op2.low - 1;
1034
1035           res.low = op1.low / op2.low, res.high = 0;
1036           break;
1037         }
1038
1039       /* ... fall through ...  */
1040
1041     case ROUND_DIV_EXPR:
1042       if (double_int_zero_p (op2))
1043         return NULL_TREE;
1044       if (double_int_one_p (op2))
1045         {
1046           res = op1;
1047           break;
1048         }
1049       if (double_int_equal_p (op1, op2)
1050           && ! double_int_zero_p (op1))
1051         {
1052           res = double_int_one;
1053           break;
1054         }
1055       overflow = div_and_round_double (code, uns,
1056                                        op1.low, op1.high, op2.low, op2.high,
1057                                        &res.low, &res.high,
1058                                        &tmp.low, &tmp.high);
1059       break;
1060
1061     case TRUNC_MOD_EXPR:
1062     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1063       /* This is a shortcut for a common special case.  */
1064       if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1065           && !TREE_OVERFLOW (arg1)
1066           && !TREE_OVERFLOW (arg2)
1067           && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1068         {
1069           if (code == CEIL_MOD_EXPR)
1070             op1.low += op2.low - 1;
1071           res.low = op1.low % op2.low, res.high = 0;
1072           break;
1073         }
1074
1075       /* ... fall through ...  */
1076
1077     case ROUND_MOD_EXPR:
1078       if (double_int_zero_p (op2))
1079         return NULL_TREE;
1080       overflow = div_and_round_double (code, uns,
1081                                        op1.low, op1.high, op2.low, op2.high,
1082                                        &tmp.low, &tmp.high,
1083                                        &res.low, &res.high);
1084       break;
1085
1086     case MIN_EXPR:
1087       res = double_int_min (op1, op2, uns);
1088       break;
1089
1090     case MAX_EXPR:
1091       res = double_int_max (op1, op2, uns);
1092       break;
1093
1094     default:
1095       return NULL_TREE;
1096     }
1097
1098   t = force_fit_type_double (TREE_TYPE (arg1), res, 1,
1099                              ((!uns || is_sizetype) && overflow)
1100                              | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1101
1102   return t;
1103 }
1104
1105 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1106    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1107    are the same kind of constant and the same machine mode.  Return zero if
1108    combining the constants is not allowed in the current operating mode.  */
1109
1110 static tree
1111 const_binop (enum tree_code code, tree arg1, tree arg2)
1112 {
1113   /* Sanity check for the recursive cases.  */
1114   if (!arg1 || !arg2)
1115     return NULL_TREE;
1116
1117   STRIP_NOPS (arg1);
1118   STRIP_NOPS (arg2);
1119
1120   if (TREE_CODE (arg1) == INTEGER_CST)
1121     return int_const_binop (code, arg1, arg2);
1122
1123   if (TREE_CODE (arg1) == REAL_CST)
1124     {
1125       enum machine_mode mode;
1126       REAL_VALUE_TYPE d1;
1127       REAL_VALUE_TYPE d2;
1128       REAL_VALUE_TYPE value;
1129       REAL_VALUE_TYPE result;
1130       bool inexact;
1131       tree t, type;
1132
1133       /* The following codes are handled by real_arithmetic.  */
1134       switch (code)
1135         {
1136         case PLUS_EXPR:
1137         case MINUS_EXPR:
1138         case MULT_EXPR:
1139         case RDIV_EXPR:
1140         case MIN_EXPR:
1141         case MAX_EXPR:
1142           break;
1143
1144         default:
1145           return NULL_TREE;
1146         }
1147
1148       d1 = TREE_REAL_CST (arg1);
1149       d2 = TREE_REAL_CST (arg2);
1150
1151       type = TREE_TYPE (arg1);
1152       mode = TYPE_MODE (type);
1153
1154       /* Don't perform operation if we honor signaling NaNs and
1155          either operand is a NaN.  */
1156       if (HONOR_SNANS (mode)
1157           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1158         return NULL_TREE;
1159
1160       /* Don't perform operation if it would raise a division
1161          by zero exception.  */
1162       if (code == RDIV_EXPR
1163           && REAL_VALUES_EQUAL (d2, dconst0)
1164           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1165         return NULL_TREE;
1166
1167       /* If either operand is a NaN, just return it.  Otherwise, set up
1168          for floating-point trap; we return an overflow.  */
1169       if (REAL_VALUE_ISNAN (d1))
1170         return arg1;
1171       else if (REAL_VALUE_ISNAN (d2))
1172         return arg2;
1173
1174       inexact = real_arithmetic (&value, code, &d1, &d2);
1175       real_convert (&result, mode, &value);
1176
1177       /* Don't constant fold this floating point operation if
1178          the result has overflowed and flag_trapping_math.  */
1179       if (flag_trapping_math
1180           && MODE_HAS_INFINITIES (mode)
1181           && REAL_VALUE_ISINF (result)
1182           && !REAL_VALUE_ISINF (d1)
1183           && !REAL_VALUE_ISINF (d2))
1184         return NULL_TREE;
1185
1186       /* Don't constant fold this floating point operation if the
1187          result may dependent upon the run-time rounding mode and
1188          flag_rounding_math is set, or if GCC's software emulation
1189          is unable to accurately represent the result.  */
1190       if ((flag_rounding_math
1191            || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1192           && (inexact || !real_identical (&result, &value)))
1193         return NULL_TREE;
1194
1195       t = build_real (type, result);
1196
1197       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1198       return t;
1199     }
1200
1201   if (TREE_CODE (arg1) == FIXED_CST)
1202     {
1203       FIXED_VALUE_TYPE f1;
1204       FIXED_VALUE_TYPE f2;
1205       FIXED_VALUE_TYPE result;
1206       tree t, type;
1207       int sat_p;
1208       bool overflow_p;
1209
1210       /* The following codes are handled by fixed_arithmetic.  */
1211       switch (code)
1212         {
1213         case PLUS_EXPR:
1214         case MINUS_EXPR:
1215         case MULT_EXPR:
1216         case TRUNC_DIV_EXPR:
1217           f2 = TREE_FIXED_CST (arg2);
1218           break;
1219
1220         case LSHIFT_EXPR:
1221         case RSHIFT_EXPR:
1222           f2.data.high = TREE_INT_CST_HIGH (arg2);
1223           f2.data.low = TREE_INT_CST_LOW (arg2);
1224           f2.mode = SImode;
1225           break;
1226
1227         default:
1228           return NULL_TREE;
1229         }
1230
1231       f1 = TREE_FIXED_CST (arg1);
1232       type = TREE_TYPE (arg1);
1233       sat_p = TYPE_SATURATING (type);
1234       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1235       t = build_fixed (type, result);
1236       /* Propagate overflow flags.  */
1237       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1238         TREE_OVERFLOW (t) = 1;
1239       return t;
1240     }
1241
1242   if (TREE_CODE (arg1) == COMPLEX_CST)
1243     {
1244       tree type = TREE_TYPE (arg1);
1245       tree r1 = TREE_REALPART (arg1);
1246       tree i1 = TREE_IMAGPART (arg1);
1247       tree r2 = TREE_REALPART (arg2);
1248       tree i2 = TREE_IMAGPART (arg2);
1249       tree real, imag;
1250
1251       switch (code)
1252         {
1253         case PLUS_EXPR:
1254         case MINUS_EXPR:
1255           real = const_binop (code, r1, r2);
1256           imag = const_binop (code, i1, i2);
1257           break;
1258
1259         case MULT_EXPR:
1260           if (COMPLEX_FLOAT_TYPE_P (type))
1261             return do_mpc_arg2 (arg1, arg2, type,
1262                                 /* do_nonfinite= */ folding_initializer,
1263                                 mpc_mul);
1264
1265           real = const_binop (MINUS_EXPR,
1266                               const_binop (MULT_EXPR, r1, r2),
1267                               const_binop (MULT_EXPR, i1, i2));
1268           imag = const_binop (PLUS_EXPR,
1269                               const_binop (MULT_EXPR, r1, i2),
1270                               const_binop (MULT_EXPR, i1, r2));
1271           break;
1272
1273         case RDIV_EXPR:
1274           if (COMPLEX_FLOAT_TYPE_P (type))
1275             return do_mpc_arg2 (arg1, arg2, type,
1276                                 /* do_nonfinite= */ folding_initializer,
1277                                 mpc_div);
1278           /* Fallthru ... */
1279         case TRUNC_DIV_EXPR:
1280         case CEIL_DIV_EXPR:
1281         case FLOOR_DIV_EXPR:
1282         case ROUND_DIV_EXPR:
1283           if (flag_complex_method == 0)
1284           {
1285             /* Keep this algorithm in sync with
1286                tree-complex.c:expand_complex_div_straight().
1287
1288                Expand complex division to scalars, straightforward algorithm.
1289                a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1290                t = br*br + bi*bi
1291             */
1292             tree magsquared
1293               = const_binop (PLUS_EXPR,
1294                              const_binop (MULT_EXPR, r2, r2),
1295                              const_binop (MULT_EXPR, i2, i2));
1296             tree t1
1297               = const_binop (PLUS_EXPR,
1298                              const_binop (MULT_EXPR, r1, r2),
1299                              const_binop (MULT_EXPR, i1, i2));
1300             tree t2
1301               = const_binop (MINUS_EXPR,
1302                              const_binop (MULT_EXPR, i1, r2),
1303                              const_binop (MULT_EXPR, r1, i2));
1304
1305             real = const_binop (code, t1, magsquared);
1306             imag = const_binop (code, t2, magsquared);
1307           }
1308           else
1309           {
1310             /* Keep this algorithm in sync with
1311                tree-complex.c:expand_complex_div_wide().
1312
1313                Expand complex division to scalars, modified algorithm to minimize
1314                overflow with wide input ranges.  */
1315             tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1316                                         fold_abs_const (r2, TREE_TYPE (type)),
1317                                         fold_abs_const (i2, TREE_TYPE (type)));
1318
1319             if (integer_nonzerop (compare))
1320               {
1321                 /* In the TRUE branch, we compute
1322                    ratio = br/bi;
1323                    div = (br * ratio) + bi;
1324                    tr = (ar * ratio) + ai;
1325                    ti = (ai * ratio) - ar;
1326                    tr = tr / div;
1327                    ti = ti / div;  */
1328                 tree ratio = const_binop (code, r2, i2);
1329                 tree div = const_binop (PLUS_EXPR, i2,
1330                                         const_binop (MULT_EXPR, r2, ratio));
1331                 real = const_binop (MULT_EXPR, r1, ratio);
1332                 real = const_binop (PLUS_EXPR, real, i1);
1333                 real = const_binop (code, real, div);
1334
1335                 imag = const_binop (MULT_EXPR, i1, ratio);
1336                 imag = const_binop (MINUS_EXPR, imag, r1);
1337                 imag = const_binop (code, imag, div);
1338               }
1339             else
1340               {
1341                 /* In the FALSE branch, we compute
1342                    ratio = d/c;
1343                    divisor = (d * ratio) + c;
1344                    tr = (b * ratio) + a;
1345                    ti = b - (a * ratio);
1346                    tr = tr / div;
1347                    ti = ti / div;  */
1348                 tree ratio = const_binop (code, i2, r2);
1349                 tree div = const_binop (PLUS_EXPR, r2,
1350                                         const_binop (MULT_EXPR, i2, ratio));
1351
1352                 real = const_binop (MULT_EXPR, i1, ratio);
1353                 real = const_binop (PLUS_EXPR, real, r1);
1354                 real = const_binop (code, real, div);
1355
1356                 imag = const_binop (MULT_EXPR, r1, ratio);
1357                 imag = const_binop (MINUS_EXPR, i1, imag);
1358                 imag = const_binop (code, imag, div);
1359               }
1360           }
1361           break;
1362
1363         default:
1364           return NULL_TREE;
1365         }
1366
1367       if (real && imag)
1368         return build_complex (type, real, imag);
1369     }
1370
1371   if (TREE_CODE (arg1) == VECTOR_CST)
1372     {
1373       tree type = TREE_TYPE(arg1);
1374       int count = TYPE_VECTOR_SUBPARTS (type), i;
1375       tree elements1, elements2, list = NULL_TREE;
1376
1377       if(TREE_CODE(arg2) != VECTOR_CST)
1378         return NULL_TREE;
1379
1380       elements1 = TREE_VECTOR_CST_ELTS (arg1);
1381       elements2 = TREE_VECTOR_CST_ELTS (arg2);
1382
1383       for (i = 0; i < count; i++)
1384         {
1385           tree elem1, elem2, elem;
1386
1387           /* The trailing elements can be empty and should be treated as 0 */
1388           if(!elements1)
1389             elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1390           else
1391             {
1392               elem1 = TREE_VALUE(elements1);
1393               elements1 = TREE_CHAIN (elements1);
1394             }
1395
1396           if(!elements2)
1397             elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1398           else
1399             {
1400               elem2 = TREE_VALUE(elements2);
1401               elements2 = TREE_CHAIN (elements2);
1402             }
1403
1404           elem = const_binop (code, elem1, elem2);
1405
1406           /* It is possible that const_binop cannot handle the given
1407             code and return NULL_TREE */
1408           if(elem == NULL_TREE)
1409             return NULL_TREE;
1410
1411           list = tree_cons (NULL_TREE, elem, list);
1412         }
1413       return build_vector(type, nreverse(list));
1414     }
1415   return NULL_TREE;
1416 }
1417
1418 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
1419    indicates which particular sizetype to create.  */
1420
1421 tree
1422 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1423 {
1424   return build_int_cst (sizetype_tab[(int) kind], number);
1425 }
1426 \f
1427 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
1428    is a tree code.  The type of the result is taken from the operands.
1429    Both must be equivalent integer types, ala int_binop_types_match_p.
1430    If the operands are constant, so is the result.  */
1431
1432 tree
1433 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1434 {
1435   tree type = TREE_TYPE (arg0);
1436
1437   if (arg0 == error_mark_node || arg1 == error_mark_node)
1438     return error_mark_node;
1439
1440   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1441                                        TREE_TYPE (arg1)));
1442
1443   /* Handle the special case of two integer constants faster.  */
1444   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1445     {
1446       /* And some specific cases even faster than that.  */
1447       if (code == PLUS_EXPR)
1448         {
1449           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1450             return arg1;
1451           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1452             return arg0;
1453         }
1454       else if (code == MINUS_EXPR)
1455         {
1456           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1457             return arg0;
1458         }
1459       else if (code == MULT_EXPR)
1460         {
1461           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1462             return arg1;
1463         }
1464
1465       /* Handle general case of two integer constants.  */
1466       return int_const_binop (code, arg0, arg1);
1467     }
1468
1469   return fold_build2_loc (loc, code, type, arg0, arg1);
1470 }
1471
1472 /* Given two values, either both of sizetype or both of bitsizetype,
1473    compute the difference between the two values.  Return the value
1474    in signed type corresponding to the type of the operands.  */
1475
1476 tree
1477 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1478 {
1479   tree type = TREE_TYPE (arg0);
1480   tree ctype;
1481
1482   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1483                                        TREE_TYPE (arg1)));
1484
1485   /* If the type is already signed, just do the simple thing.  */
1486   if (!TYPE_UNSIGNED (type))
1487     return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1488
1489   if (type == sizetype)
1490     ctype = ssizetype;
1491   else if (type == bitsizetype)
1492     ctype = sbitsizetype;
1493   else
1494     ctype = signed_type_for (type);
1495
1496   /* If either operand is not a constant, do the conversions to the signed
1497      type and subtract.  The hardware will do the right thing with any
1498      overflow in the subtraction.  */
1499   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1500     return size_binop_loc (loc, MINUS_EXPR,
1501                            fold_convert_loc (loc, ctype, arg0),
1502                            fold_convert_loc (loc, ctype, arg1));
1503
1504   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1505      Otherwise, subtract the other way, convert to CTYPE (we know that can't
1506      overflow) and negate (which can't either).  Special-case a result
1507      of zero while we're here.  */
1508   if (tree_int_cst_equal (arg0, arg1))
1509     return build_int_cst (ctype, 0);
1510   else if (tree_int_cst_lt (arg1, arg0))
1511     return fold_convert_loc (loc, ctype,
1512                              size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1513   else
1514     return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1515                            fold_convert_loc (loc, ctype,
1516                                              size_binop_loc (loc,
1517                                                              MINUS_EXPR,
1518                                                              arg1, arg0)));
1519 }
1520 \f
1521 /* A subroutine of fold_convert_const handling conversions of an
1522    INTEGER_CST to another integer type.  */
1523
1524 static tree
1525 fold_convert_const_int_from_int (tree type, const_tree arg1)
1526 {
1527   tree t;
1528
1529   /* Given an integer constant, make new constant with new type,
1530      appropriately sign-extended or truncated.  */
1531   t = force_fit_type_double (type, tree_to_double_int (arg1),
1532                              !POINTER_TYPE_P (TREE_TYPE (arg1)),
1533                              (TREE_INT_CST_HIGH (arg1) < 0
1534                               && (TYPE_UNSIGNED (type)
1535                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1536                              | TREE_OVERFLOW (arg1));
1537
1538   return t;
1539 }
1540
1541 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1542    to an integer type.  */
1543
1544 static tree
1545 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1546 {
1547   int overflow = 0;
1548   tree t;
1549
1550   /* The following code implements the floating point to integer
1551      conversion rules required by the Java Language Specification,
1552      that IEEE NaNs are mapped to zero and values that overflow
1553      the target precision saturate, i.e. values greater than
1554      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1555      are mapped to INT_MIN.  These semantics are allowed by the
1556      C and C++ standards that simply state that the behavior of
1557      FP-to-integer conversion is unspecified upon overflow.  */
1558
1559   double_int val;
1560   REAL_VALUE_TYPE r;
1561   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1562
1563   switch (code)
1564     {
1565     case FIX_TRUNC_EXPR:
1566       real_trunc (&r, VOIDmode, &x);
1567       break;
1568
1569     default:
1570       gcc_unreachable ();
1571     }
1572
1573   /* If R is NaN, return zero and show we have an overflow.  */
1574   if (REAL_VALUE_ISNAN (r))
1575     {
1576       overflow = 1;
1577       val = double_int_zero;
1578     }
1579
1580   /* See if R is less than the lower bound or greater than the
1581      upper bound.  */
1582
1583   if (! overflow)
1584     {
1585       tree lt = TYPE_MIN_VALUE (type);
1586       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1587       if (REAL_VALUES_LESS (r, l))
1588         {
1589           overflow = 1;
1590           val = tree_to_double_int (lt);
1591         }
1592     }
1593
1594   if (! overflow)
1595     {
1596       tree ut = TYPE_MAX_VALUE (type);
1597       if (ut)
1598         {
1599           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1600           if (REAL_VALUES_LESS (u, r))
1601             {
1602               overflow = 1;
1603               val = tree_to_double_int (ut);
1604             }
1605         }
1606     }
1607
1608   if (! overflow)
1609     real_to_integer2 ((HOST_WIDE_INT *) &val.low, &val.high, &r);
1610
1611   t = force_fit_type_double (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1612   return t;
1613 }
1614
1615 /* A subroutine of fold_convert_const handling conversions of a
1616    FIXED_CST to an integer type.  */
1617
1618 static tree
1619 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1620 {
1621   tree t;
1622   double_int temp, temp_trunc;
1623   unsigned int mode;
1624
1625   /* Right shift FIXED_CST to temp by fbit.  */
1626   temp = TREE_FIXED_CST (arg1).data;
1627   mode = TREE_FIXED_CST (arg1).mode;
1628   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
1629     {
1630       temp = double_int_rshift (temp, GET_MODE_FBIT (mode),
1631                                 HOST_BITS_PER_DOUBLE_INT,
1632                                 SIGNED_FIXED_POINT_MODE_P (mode));
1633
1634       /* Left shift temp to temp_trunc by fbit.  */
1635       temp_trunc = double_int_lshift (temp, GET_MODE_FBIT (mode),
1636                                       HOST_BITS_PER_DOUBLE_INT,
1637                                       SIGNED_FIXED_POINT_MODE_P (mode));
1638     }
1639   else
1640     {
1641       temp = double_int_zero;
1642       temp_trunc = double_int_zero;
1643     }
1644
1645   /* If FIXED_CST is negative, we need to round the value toward 0.
1646      By checking if the fractional bits are not zero to add 1 to temp.  */
1647   if (SIGNED_FIXED_POINT_MODE_P (mode)
1648       && double_int_negative_p (temp_trunc)
1649       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
1650     temp = double_int_add (temp, double_int_one);
1651
1652   /* Given a fixed-point constant, make new constant with new type,
1653      appropriately sign-extended or truncated.  */
1654   t = force_fit_type_double (type, temp, -1,
1655                              (double_int_negative_p (temp)
1656                               && (TYPE_UNSIGNED (type)
1657                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1658                              | TREE_OVERFLOW (arg1));
1659
1660   return t;
1661 }
1662
1663 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1664    to another floating point type.  */
1665
1666 static tree
1667 fold_convert_const_real_from_real (tree type, const_tree arg1)
1668 {
1669   REAL_VALUE_TYPE value;
1670   tree t;
1671
1672   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1673   t = build_real (type, value);
1674
1675   /* If converting an infinity or NAN to a representation that doesn't
1676      have one, set the overflow bit so that we can produce some kind of
1677      error message at the appropriate point if necessary.  It's not the
1678      most user-friendly message, but it's better than nothing.  */
1679   if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1680       && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1681     TREE_OVERFLOW (t) = 1;
1682   else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1683            && !MODE_HAS_NANS (TYPE_MODE (type)))
1684     TREE_OVERFLOW (t) = 1;
1685   /* Regular overflow, conversion produced an infinity in a mode that
1686      can't represent them.  */
1687   else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1688            && REAL_VALUE_ISINF (value)
1689            && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1690     TREE_OVERFLOW (t) = 1;
1691   else
1692     TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1693   return t;
1694 }
1695
1696 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1697    to a floating point type.  */
1698
1699 static tree
1700 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
1701 {
1702   REAL_VALUE_TYPE value;
1703   tree t;
1704
1705   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
1706   t = build_real (type, value);
1707
1708   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1709   return t;
1710 }
1711
1712 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1713    to another fixed-point type.  */
1714
1715 static tree
1716 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
1717 {
1718   FIXED_VALUE_TYPE value;
1719   tree t;
1720   bool overflow_p;
1721
1722   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
1723                               TYPE_SATURATING (type));
1724   t = build_fixed (type, value);
1725
1726   /* Propagate overflow flags.  */
1727   if (overflow_p | TREE_OVERFLOW (arg1))
1728     TREE_OVERFLOW (t) = 1;
1729   return t;
1730 }
1731
1732 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
1733    to a fixed-point type.  */
1734
1735 static tree
1736 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
1737 {
1738   FIXED_VALUE_TYPE value;
1739   tree t;
1740   bool overflow_p;
1741
1742   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
1743                                        TREE_INT_CST (arg1),
1744                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
1745                                        TYPE_SATURATING (type));
1746   t = build_fixed (type, value);
1747
1748   /* Propagate overflow flags.  */
1749   if (overflow_p | TREE_OVERFLOW (arg1))
1750     TREE_OVERFLOW (t) = 1;
1751   return t;
1752 }
1753
1754 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1755    to a fixed-point type.  */
1756
1757 static tree
1758 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
1759 {
1760   FIXED_VALUE_TYPE value;
1761   tree t;
1762   bool overflow_p;
1763
1764   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
1765                                         &TREE_REAL_CST (arg1),
1766                                         TYPE_SATURATING (type));
1767   t = build_fixed (type, value);
1768
1769   /* Propagate overflow flags.  */
1770   if (overflow_p | TREE_OVERFLOW (arg1))
1771     TREE_OVERFLOW (t) = 1;
1772   return t;
1773 }
1774
1775 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1776    type TYPE.  If no simplification can be done return NULL_TREE.  */
1777
1778 static tree
1779 fold_convert_const (enum tree_code code, tree type, tree arg1)
1780 {
1781   if (TREE_TYPE (arg1) == type)
1782     return arg1;
1783
1784   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
1785       || TREE_CODE (type) == OFFSET_TYPE)
1786     {
1787       if (TREE_CODE (arg1) == INTEGER_CST)
1788         return fold_convert_const_int_from_int (type, arg1);
1789       else if (TREE_CODE (arg1) == REAL_CST)
1790         return fold_convert_const_int_from_real (code, type, arg1);
1791       else if (TREE_CODE (arg1) == FIXED_CST)
1792         return fold_convert_const_int_from_fixed (type, arg1);
1793     }
1794   else if (TREE_CODE (type) == REAL_TYPE)
1795     {
1796       if (TREE_CODE (arg1) == INTEGER_CST)
1797         return build_real_from_int_cst (type, arg1);
1798       else if (TREE_CODE (arg1) == REAL_CST)
1799         return fold_convert_const_real_from_real (type, arg1);
1800       else if (TREE_CODE (arg1) == FIXED_CST)
1801         return fold_convert_const_real_from_fixed (type, arg1);
1802     }
1803   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
1804     {
1805       if (TREE_CODE (arg1) == FIXED_CST)
1806         return fold_convert_const_fixed_from_fixed (type, arg1);
1807       else if (TREE_CODE (arg1) == INTEGER_CST)
1808         return fold_convert_const_fixed_from_int (type, arg1);
1809       else if (TREE_CODE (arg1) == REAL_CST)
1810         return fold_convert_const_fixed_from_real (type, arg1);
1811     }
1812   return NULL_TREE;
1813 }
1814
1815 /* Construct a vector of zero elements of vector type TYPE.  */
1816
1817 static tree
1818 build_zero_vector (tree type)
1819 {
1820   tree t;
1821
1822   t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1823   return build_vector_from_val (type, t);
1824 }
1825
1826 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
1827
1828 bool
1829 fold_convertible_p (const_tree type, const_tree arg)
1830 {
1831   tree orig = TREE_TYPE (arg);
1832
1833   if (type == orig)
1834     return true;
1835
1836   if (TREE_CODE (arg) == ERROR_MARK
1837       || TREE_CODE (type) == ERROR_MARK
1838       || TREE_CODE (orig) == ERROR_MARK)
1839     return false;
1840
1841   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1842     return true;
1843
1844   switch (TREE_CODE (type))
1845     {
1846     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1847     case POINTER_TYPE: case REFERENCE_TYPE:
1848     case OFFSET_TYPE:
1849       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1850           || TREE_CODE (orig) == OFFSET_TYPE)
1851         return true;
1852       return (TREE_CODE (orig) == VECTOR_TYPE
1853               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1854
1855     case REAL_TYPE:
1856     case FIXED_POINT_TYPE:
1857     case COMPLEX_TYPE:
1858     case VECTOR_TYPE:
1859     case VOID_TYPE:
1860       return TREE_CODE (type) == TREE_CODE (orig);
1861
1862     default:
1863       return false;
1864     }
1865 }
1866
1867 /* Convert expression ARG to type TYPE.  Used by the middle-end for
1868    simple conversions in preference to calling the front-end's convert.  */
1869
1870 tree
1871 fold_convert_loc (location_t loc, tree type, tree arg)
1872 {
1873   tree orig = TREE_TYPE (arg);
1874   tree tem;
1875
1876   if (type == orig)
1877     return arg;
1878
1879   if (TREE_CODE (arg) == ERROR_MARK
1880       || TREE_CODE (type) == ERROR_MARK
1881       || TREE_CODE (orig) == ERROR_MARK)
1882     return error_mark_node;
1883
1884   switch (TREE_CODE (type))
1885     {
1886     case POINTER_TYPE:
1887     case REFERENCE_TYPE:
1888       /* Handle conversions between pointers to different address spaces.  */
1889       if (POINTER_TYPE_P (orig)
1890           && (TYPE_ADDR_SPACE (TREE_TYPE (type))
1891               != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
1892         return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
1893       /* fall through */
1894
1895     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1896     case OFFSET_TYPE:
1897       if (TREE_CODE (arg) == INTEGER_CST)
1898         {
1899           tem = fold_convert_const (NOP_EXPR, type, arg);
1900           if (tem != NULL_TREE)
1901             return tem;
1902         }
1903       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1904           || TREE_CODE (orig) == OFFSET_TYPE)
1905         return fold_build1_loc (loc, NOP_EXPR, type, arg);
1906       if (TREE_CODE (orig) == COMPLEX_TYPE)
1907         return fold_convert_loc (loc, type,
1908                              fold_build1_loc (loc, REALPART_EXPR,
1909                                           TREE_TYPE (orig), arg));
1910       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
1911                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1912       return fold_build1_loc (loc, NOP_EXPR, type, arg);
1913
1914     case REAL_TYPE:
1915       if (TREE_CODE (arg) == INTEGER_CST)
1916         {
1917           tem = fold_convert_const (FLOAT_EXPR, type, arg);
1918           if (tem != NULL_TREE)
1919             return tem;
1920         }
1921       else if (TREE_CODE (arg) == REAL_CST)
1922         {
1923           tem = fold_convert_const (NOP_EXPR, type, arg);
1924           if (tem != NULL_TREE)
1925             return tem;
1926         }
1927       else if (TREE_CODE (arg) == FIXED_CST)
1928         {
1929           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1930           if (tem != NULL_TREE)
1931             return tem;
1932         }
1933
1934       switch (TREE_CODE (orig))
1935         {
1936         case INTEGER_TYPE:
1937         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1938         case POINTER_TYPE: case REFERENCE_TYPE:
1939           return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
1940
1941         case REAL_TYPE:
1942           return fold_build1_loc (loc, NOP_EXPR, type, arg);
1943
1944         case FIXED_POINT_TYPE:
1945           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1946
1947         case COMPLEX_TYPE:
1948           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1949           return fold_convert_loc (loc, type, tem);
1950
1951         default:
1952           gcc_unreachable ();
1953         }
1954
1955     case FIXED_POINT_TYPE:
1956       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
1957           || TREE_CODE (arg) == REAL_CST)
1958         {
1959           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1960           if (tem != NULL_TREE)
1961             goto fold_convert_exit;
1962         }
1963
1964       switch (TREE_CODE (orig))
1965         {
1966         case FIXED_POINT_TYPE:
1967         case INTEGER_TYPE:
1968         case ENUMERAL_TYPE:
1969         case BOOLEAN_TYPE:
1970         case REAL_TYPE:
1971           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1972
1973         case COMPLEX_TYPE:
1974           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1975           return fold_convert_loc (loc, type, tem);
1976
1977         default:
1978           gcc_unreachable ();
1979         }
1980
1981     case COMPLEX_TYPE:
1982       switch (TREE_CODE (orig))
1983         {
1984         case INTEGER_TYPE:
1985         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1986         case POINTER_TYPE: case REFERENCE_TYPE:
1987         case REAL_TYPE:
1988         case FIXED_POINT_TYPE:
1989           return fold_build2_loc (loc, COMPLEX_EXPR, type,
1990                               fold_convert_loc (loc, TREE_TYPE (type), arg),
1991                               fold_convert_loc (loc, TREE_TYPE (type),
1992                                             integer_zero_node));
1993         case COMPLEX_TYPE:
1994           {
1995             tree rpart, ipart;
1996
1997             if (TREE_CODE (arg) == COMPLEX_EXPR)
1998               {
1999                 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2000                                       TREE_OPERAND (arg, 0));
2001                 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2002                                       TREE_OPERAND (arg, 1));
2003                 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2004               }
2005
2006             arg = save_expr (arg);
2007             rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2008             ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2009             rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2010             ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2011             return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2012           }
2013
2014         default:
2015           gcc_unreachable ();
2016         }
2017
2018     case VECTOR_TYPE:
2019       if (integer_zerop (arg))
2020         return build_zero_vector (type);
2021       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2022       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2023                   || TREE_CODE (orig) == VECTOR_TYPE);
2024       return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2025
2026     case VOID_TYPE:
2027       tem = fold_ignored_result (arg);
2028       return fold_build1_loc (loc, NOP_EXPR, type, tem);
2029
2030     default:
2031       if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2032         return fold_build1_loc (loc, NOP_EXPR, type, arg);
2033       gcc_unreachable ();
2034     }
2035  fold_convert_exit:
2036   protected_set_expr_location_unshare (tem, loc);
2037   return tem;
2038 }
2039 \f
2040 /* Return false if expr can be assumed not to be an lvalue, true
2041    otherwise.  */
2042
2043 static bool
2044 maybe_lvalue_p (const_tree x)
2045 {
2046   /* We only need to wrap lvalue tree codes.  */
2047   switch (TREE_CODE (x))
2048   {
2049   case VAR_DECL:
2050   case PARM_DECL:
2051   case RESULT_DECL:
2052   case LABEL_DECL:
2053   case FUNCTION_DECL:
2054   case SSA_NAME:
2055
2056   case COMPONENT_REF:
2057   case MEM_REF:
2058   case INDIRECT_REF:
2059   case ARRAY_REF:
2060   case ARRAY_RANGE_REF:
2061   case BIT_FIELD_REF:
2062   case OBJ_TYPE_REF:
2063
2064   case REALPART_EXPR:
2065   case IMAGPART_EXPR:
2066   case PREINCREMENT_EXPR:
2067   case PREDECREMENT_EXPR:
2068   case SAVE_EXPR:
2069   case TRY_CATCH_EXPR:
2070   case WITH_CLEANUP_EXPR:
2071   case COMPOUND_EXPR:
2072   case MODIFY_EXPR:
2073   case TARGET_EXPR:
2074   case COND_EXPR:
2075   case BIND_EXPR:
2076     break;
2077
2078   default:
2079     /* Assume the worst for front-end tree codes.  */
2080     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2081       break;
2082     return false;
2083   }
2084
2085   return true;
2086 }
2087
2088 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2089
2090 tree
2091 non_lvalue_loc (location_t loc, tree x)
2092 {
2093   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2094      us.  */
2095   if (in_gimple_form)
2096     return x;
2097
2098   if (! maybe_lvalue_p (x))
2099     return x;
2100   return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2101 }
2102
2103 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2104    Zero means allow extended lvalues.  */
2105
2106 int pedantic_lvalues;
2107
2108 /* When pedantic, return an expr equal to X but certainly not valid as a
2109    pedantic lvalue.  Otherwise, return X.  */
2110
2111 static tree
2112 pedantic_non_lvalue_loc (location_t loc, tree x)
2113 {
2114   if (pedantic_lvalues)
2115     return non_lvalue_loc (loc, x);
2116
2117   return protected_set_expr_location_unshare (x, loc);
2118 }
2119 \f
2120 /* Given a tree comparison code, return the code that is the logical inverse.
2121    It is generally not safe to do this for floating-point comparisons, except
2122    for EQ_EXPR and NE_EXPR, so we return ERROR_MARK in this case.  */
2123
2124 enum tree_code
2125 invert_tree_comparison (enum tree_code code, bool honor_nans)
2126 {
2127   if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR)
2128     return ERROR_MARK;
2129
2130   switch (code)
2131     {
2132     case EQ_EXPR:
2133       return NE_EXPR;
2134     case NE_EXPR:
2135       return EQ_EXPR;
2136     case GT_EXPR:
2137       return honor_nans ? UNLE_EXPR : LE_EXPR;
2138     case GE_EXPR:
2139       return honor_nans ? UNLT_EXPR : LT_EXPR;
2140     case LT_EXPR:
2141       return honor_nans ? UNGE_EXPR : GE_EXPR;
2142     case LE_EXPR:
2143       return honor_nans ? UNGT_EXPR : GT_EXPR;
2144     case LTGT_EXPR:
2145       return UNEQ_EXPR;
2146     case UNEQ_EXPR:
2147       return LTGT_EXPR;
2148     case UNGT_EXPR:
2149       return LE_EXPR;
2150     case UNGE_EXPR:
2151       return LT_EXPR;
2152     case UNLT_EXPR:
2153       return GE_EXPR;
2154     case UNLE_EXPR:
2155       return GT_EXPR;
2156     case ORDERED_EXPR:
2157       return UNORDERED_EXPR;
2158     case UNORDERED_EXPR:
2159       return ORDERED_EXPR;
2160     default:
2161       gcc_unreachable ();
2162     }
2163 }
2164
2165 /* Similar, but return the comparison that results if the operands are
2166    swapped.  This is safe for floating-point.  */
2167
2168 enum tree_code
2169 swap_tree_comparison (enum tree_code code)
2170 {
2171   switch (code)
2172     {
2173     case EQ_EXPR:
2174     case NE_EXPR:
2175     case ORDERED_EXPR:
2176     case UNORDERED_EXPR:
2177     case LTGT_EXPR:
2178     case UNEQ_EXPR:
2179       return code;
2180     case GT_EXPR:
2181       return LT_EXPR;
2182     case GE_EXPR:
2183       return LE_EXPR;
2184     case LT_EXPR:
2185       return GT_EXPR;
2186     case LE_EXPR:
2187       return GE_EXPR;
2188     case UNGT_EXPR:
2189       return UNLT_EXPR;
2190     case UNGE_EXPR:
2191       return UNLE_EXPR;
2192     case UNLT_EXPR:
2193       return UNGT_EXPR;
2194     case UNLE_EXPR:
2195       return UNGE_EXPR;
2196     default:
2197       gcc_unreachable ();
2198     }
2199 }
2200
2201
2202 /* Convert a comparison tree code from an enum tree_code representation
2203    into a compcode bit-based encoding.  This function is the inverse of
2204    compcode_to_comparison.  */
2205
2206 static enum comparison_code
2207 comparison_to_compcode (enum tree_code code)
2208 {
2209   switch (code)
2210     {
2211     case LT_EXPR:
2212       return COMPCODE_LT;
2213     case EQ_EXPR:
2214       return COMPCODE_EQ;
2215     case LE_EXPR:
2216       return COMPCODE_LE;
2217     case GT_EXPR:
2218       return COMPCODE_GT;
2219     case NE_EXPR:
2220       return COMPCODE_NE;
2221     case GE_EXPR:
2222       return COMPCODE_GE;
2223     case ORDERED_EXPR:
2224       return COMPCODE_ORD;
2225     case UNORDERED_EXPR:
2226       return COMPCODE_UNORD;
2227     case UNLT_EXPR:
2228       return COMPCODE_UNLT;
2229     case UNEQ_EXPR:
2230       return COMPCODE_UNEQ;
2231     case UNLE_EXPR:
2232       return COMPCODE_UNLE;
2233     case UNGT_EXPR:
2234       return COMPCODE_UNGT;
2235     case LTGT_EXPR:
2236       return COMPCODE_LTGT;
2237     case UNGE_EXPR:
2238       return COMPCODE_UNGE;
2239     default:
2240       gcc_unreachable ();
2241     }
2242 }
2243
2244 /* Convert a compcode bit-based encoding of a comparison operator back
2245    to GCC's enum tree_code representation.  This function is the
2246    inverse of comparison_to_compcode.  */
2247
2248 static enum tree_code
2249 compcode_to_comparison (enum comparison_code code)
2250 {
2251   switch (code)
2252     {
2253     case COMPCODE_LT:
2254       return LT_EXPR;
2255     case COMPCODE_EQ:
2256       return EQ_EXPR;
2257     case COMPCODE_LE:
2258       return LE_EXPR;
2259     case COMPCODE_GT:
2260       return GT_EXPR;
2261     case COMPCODE_NE:
2262       return NE_EXPR;
2263     case COMPCODE_GE:
2264       return GE_EXPR;
2265     case COMPCODE_ORD:
2266       return ORDERED_EXPR;
2267     case COMPCODE_UNORD:
2268       return UNORDERED_EXPR;
2269     case COMPCODE_UNLT:
2270       return UNLT_EXPR;
2271     case COMPCODE_UNEQ:
2272       return UNEQ_EXPR;
2273     case COMPCODE_UNLE:
2274       return UNLE_EXPR;
2275     case COMPCODE_UNGT:
2276       return UNGT_EXPR;
2277     case COMPCODE_LTGT:
2278       return LTGT_EXPR;
2279     case COMPCODE_UNGE:
2280       return UNGE_EXPR;
2281     default:
2282       gcc_unreachable ();
2283     }
2284 }
2285
2286 /* Return a tree for the comparison which is the combination of
2287    doing the AND or OR (depending on CODE) of the two operations LCODE
2288    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2289    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2290    if this makes the transformation invalid.  */
2291
2292 tree
2293 combine_comparisons (location_t loc,
2294                      enum tree_code code, enum tree_code lcode,
2295                      enum tree_code rcode, tree truth_type,
2296                      tree ll_arg, tree lr_arg)
2297 {
2298   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2299   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2300   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2301   int compcode;
2302
2303   switch (code)
2304     {
2305     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2306       compcode = lcompcode & rcompcode;
2307       break;
2308
2309     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2310       compcode = lcompcode | rcompcode;
2311       break;
2312
2313     default:
2314       return NULL_TREE;
2315     }
2316
2317   if (!honor_nans)
2318     {
2319       /* Eliminate unordered comparisons, as well as LTGT and ORD
2320          which are not used unless the mode has NaNs.  */
2321       compcode &= ~COMPCODE_UNORD;
2322       if (compcode == COMPCODE_LTGT)
2323         compcode = COMPCODE_NE;
2324       else if (compcode == COMPCODE_ORD)
2325         compcode = COMPCODE_TRUE;
2326     }
2327    else if (flag_trapping_math)
2328      {
2329         /* Check that the original operation and the optimized ones will trap
2330            under the same condition.  */
2331         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2332                      && (lcompcode != COMPCODE_EQ)
2333                      && (lcompcode != COMPCODE_ORD);
2334         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2335                      && (rcompcode != COMPCODE_EQ)
2336                      && (rcompcode != COMPCODE_ORD);
2337         bool trap = (compcode & COMPCODE_UNORD) == 0
2338                     && (compcode != COMPCODE_EQ)
2339                     && (compcode != COMPCODE_ORD);
2340
2341         /* In a short-circuited boolean expression the LHS might be
2342            such that the RHS, if evaluated, will never trap.  For
2343            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2344            if neither x nor y is NaN.  (This is a mixed blessing: for
2345            example, the expression above will never trap, hence
2346            optimizing it to x < y would be invalid).  */
2347         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2348             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2349           rtrap = false;
2350
2351         /* If the comparison was short-circuited, and only the RHS
2352            trapped, we may now generate a spurious trap.  */
2353         if (rtrap && !ltrap
2354             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2355           return NULL_TREE;
2356
2357         /* If we changed the conditions that cause a trap, we lose.  */
2358         if ((ltrap || rtrap) != trap)
2359           return NULL_TREE;
2360       }
2361
2362   if (compcode == COMPCODE_TRUE)
2363     return constant_boolean_node (true, truth_type);
2364   else if (compcode == COMPCODE_FALSE)
2365     return constant_boolean_node (false, truth_type);
2366   else
2367     {
2368       enum tree_code tcode;
2369
2370       tcode = compcode_to_comparison ((enum comparison_code) compcode);
2371       return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2372     }
2373 }
2374 \f
2375 /* Return nonzero if two operands (typically of the same tree node)
2376    are necessarily equal.  If either argument has side-effects this
2377    function returns zero.  FLAGS modifies behavior as follows:
2378
2379    If OEP_ONLY_CONST is set, only return nonzero for constants.
2380    This function tests whether the operands are indistinguishable;
2381    it does not test whether they are equal using C's == operation.
2382    The distinction is important for IEEE floating point, because
2383    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2384    (2) two NaNs may be indistinguishable, but NaN!=NaN.
2385
2386    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2387    even though it may hold multiple values during a function.
2388    This is because a GCC tree node guarantees that nothing else is
2389    executed between the evaluation of its "operands" (which may often
2390    be evaluated in arbitrary order).  Hence if the operands themselves
2391    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2392    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
2393    unset means assuming isochronic (or instantaneous) tree equivalence.
2394    Unless comparing arbitrary expression trees, such as from different
2395    statements, this flag can usually be left unset.
2396
2397    If OEP_PURE_SAME is set, then pure functions with identical arguments
2398    are considered the same.  It is used when the caller has other ways
2399    to ensure that global memory is unchanged in between.  */
2400
2401 int
2402 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2403 {
2404   /* If either is ERROR_MARK, they aren't equal.  */
2405   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2406       || TREE_TYPE (arg0) == error_mark_node
2407       || TREE_TYPE (arg1) == error_mark_node)
2408     return 0;
2409
2410   /* Similar, if either does not have a type (like a released SSA name), 
2411      they aren't equal.  */
2412   if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2413     return 0;
2414
2415   /* Check equality of integer constants before bailing out due to
2416      precision differences.  */
2417   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2418     return tree_int_cst_equal (arg0, arg1);
2419
2420   /* If both types don't have the same signedness, then we can't consider
2421      them equal.  We must check this before the STRIP_NOPS calls
2422      because they may change the signedness of the arguments.  As pointers
2423      strictly don't have a signedness, require either two pointers or
2424      two non-pointers as well.  */
2425   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2426       || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2427     return 0;
2428
2429   /* We cannot consider pointers to different address space equal.  */
2430   if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2431       && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2432           != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2433     return 0;
2434
2435   /* If both types don't have the same precision, then it is not safe
2436      to strip NOPs.  */
2437   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
2438     return 0;
2439
2440   STRIP_NOPS (arg0);
2441   STRIP_NOPS (arg1);
2442
2443   /* In case both args are comparisons but with different comparison
2444      code, try to swap the comparison operands of one arg to produce
2445      a match and compare that variant.  */
2446   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2447       && COMPARISON_CLASS_P (arg0)
2448       && COMPARISON_CLASS_P (arg1))
2449     {
2450       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2451
2452       if (TREE_CODE (arg0) == swap_code)
2453         return operand_equal_p (TREE_OPERAND (arg0, 0),
2454                                 TREE_OPERAND (arg1, 1), flags)
2455                && operand_equal_p (TREE_OPERAND (arg0, 1),
2456                                    TREE_OPERAND (arg1, 0), flags);
2457     }
2458
2459   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2460       /* This is needed for conversions and for COMPONENT_REF.
2461          Might as well play it safe and always test this.  */
2462       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2463       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2464       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2465     return 0;
2466
2467   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2468      We don't care about side effects in that case because the SAVE_EXPR
2469      takes care of that for us. In all other cases, two expressions are
2470      equal if they have no side effects.  If we have two identical
2471      expressions with side effects that should be treated the same due
2472      to the only side effects being identical SAVE_EXPR's, that will
2473      be detected in the recursive calls below.
2474      If we are taking an invariant address of two identical objects
2475      they are necessarily equal as well.  */
2476   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2477       && (TREE_CODE (arg0) == SAVE_EXPR
2478           || (flags & OEP_CONSTANT_ADDRESS_OF)
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                                 TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1)
2542                                 ? OEP_CONSTANT_ADDRESS_OF : 0);
2543       default:
2544         break;
2545       }
2546
2547   if (flags & OEP_ONLY_CONST)
2548     return 0;
2549
2550 /* Define macros to test an operand from arg0 and arg1 for equality and a
2551    variant that allows null and views null as being different from any
2552    non-null value.  In the latter case, if either is null, the both
2553    must be; otherwise, do the normal comparison.  */
2554 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
2555                                     TREE_OPERAND (arg1, N), flags)
2556
2557 #define OP_SAME_WITH_NULL(N)                            \
2558   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2559    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2560
2561   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2562     {
2563     case tcc_unary:
2564       /* Two conversions are equal only if signedness and modes match.  */
2565       switch (TREE_CODE (arg0))
2566         {
2567         CASE_CONVERT:
2568         case FIX_TRUNC_EXPR:
2569           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2570               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2571             return 0;
2572           break;
2573         default:
2574           break;
2575         }
2576
2577       return OP_SAME (0);
2578
2579
2580     case tcc_comparison:
2581     case tcc_binary:
2582       if (OP_SAME (0) && OP_SAME (1))
2583         return 1;
2584
2585       /* For commutative ops, allow the other order.  */
2586       return (commutative_tree_code (TREE_CODE (arg0))
2587               && operand_equal_p (TREE_OPERAND (arg0, 0),
2588                                   TREE_OPERAND (arg1, 1), flags)
2589               && operand_equal_p (TREE_OPERAND (arg0, 1),
2590                                   TREE_OPERAND (arg1, 0), flags));
2591
2592     case tcc_reference:
2593       /* If either of the pointer (or reference) expressions we are
2594          dereferencing contain a side effect, these cannot be equal.  */
2595       if (TREE_SIDE_EFFECTS (arg0)
2596           || TREE_SIDE_EFFECTS (arg1))
2597         return 0;
2598
2599       switch (TREE_CODE (arg0))
2600         {
2601         case INDIRECT_REF:
2602         case REALPART_EXPR:
2603         case IMAGPART_EXPR:
2604           return OP_SAME (0);
2605
2606         case MEM_REF:
2607           /* Require equal access sizes, and similar pointer types.
2608              We can have incomplete types for array references of
2609              variable-sized arrays from the Fortran frontent
2610              though.  */
2611           return ((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1))
2612                    || (TYPE_SIZE (TREE_TYPE (arg0))
2613                        && TYPE_SIZE (TREE_TYPE (arg1))
2614                        && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
2615                                            TYPE_SIZE (TREE_TYPE (arg1)), flags)))
2616                   && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg0, 1)))
2617                       == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg1, 1))))
2618                   && OP_SAME (0) && OP_SAME (1));
2619
2620         case ARRAY_REF:
2621         case ARRAY_RANGE_REF:
2622           /* Operands 2 and 3 may be null.
2623              Compare the array index by value if it is constant first as we
2624              may have different types but same value here.  */
2625           return (OP_SAME (0)
2626                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2627                                           TREE_OPERAND (arg1, 1))
2628                       || OP_SAME (1))
2629                   && OP_SAME_WITH_NULL (2)
2630                   && OP_SAME_WITH_NULL (3));
2631
2632         case COMPONENT_REF:
2633           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
2634              may be NULL when we're called to compare MEM_EXPRs.  */
2635           return OP_SAME_WITH_NULL (0)
2636                  && OP_SAME (1)
2637                  && OP_SAME_WITH_NULL (2);
2638
2639         case BIT_FIELD_REF:
2640           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2641
2642         default:
2643           return 0;
2644         }
2645
2646     case tcc_expression:
2647       switch (TREE_CODE (arg0))
2648         {
2649         case ADDR_EXPR:
2650         case TRUTH_NOT_EXPR:
2651           return OP_SAME (0);
2652
2653         case TRUTH_ANDIF_EXPR:
2654         case TRUTH_ORIF_EXPR:
2655           return OP_SAME (0) && OP_SAME (1);
2656
2657         case FMA_EXPR:
2658         case WIDEN_MULT_PLUS_EXPR:
2659         case WIDEN_MULT_MINUS_EXPR:
2660           if (!OP_SAME (2))
2661             return 0;
2662           /* The multiplcation operands are commutative.  */
2663           /* FALLTHRU */
2664
2665         case TRUTH_AND_EXPR:
2666         case TRUTH_OR_EXPR:
2667         case TRUTH_XOR_EXPR:
2668           if (OP_SAME (0) && OP_SAME (1))
2669             return 1;
2670
2671           /* Otherwise take into account this is a commutative operation.  */
2672           return (operand_equal_p (TREE_OPERAND (arg0, 0),
2673                                    TREE_OPERAND (arg1, 1), flags)
2674                   && operand_equal_p (TREE_OPERAND (arg0, 1),
2675                                       TREE_OPERAND (arg1, 0), flags));
2676
2677         case COND_EXPR:
2678         case VEC_COND_EXPR:
2679         case DOT_PROD_EXPR:
2680           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2681
2682         default:
2683           return 0;
2684         }
2685
2686     case tcc_vl_exp:
2687       switch (TREE_CODE (arg0))
2688         {
2689         case CALL_EXPR:
2690           /* If the CALL_EXPRs call different functions, then they
2691              clearly can not be equal.  */
2692           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
2693                                  flags))
2694             return 0;
2695
2696           {
2697             unsigned int cef = call_expr_flags (arg0);
2698             if (flags & OEP_PURE_SAME)
2699               cef &= ECF_CONST | ECF_PURE;
2700             else
2701               cef &= ECF_CONST;
2702             if (!cef)
2703               return 0;
2704           }
2705
2706           /* Now see if all the arguments are the same.  */
2707           {
2708             const_call_expr_arg_iterator iter0, iter1;
2709             const_tree a0, a1;
2710             for (a0 = first_const_call_expr_arg (arg0, &iter0),
2711                    a1 = first_const_call_expr_arg (arg1, &iter1);
2712                  a0 && a1;
2713                  a0 = next_const_call_expr_arg (&iter0),
2714                    a1 = next_const_call_expr_arg (&iter1))
2715               if (! operand_equal_p (a0, a1, flags))
2716                 return 0;
2717
2718             /* If we get here and both argument lists are exhausted
2719                then the CALL_EXPRs are equal.  */
2720             return ! (a0 || a1);
2721           }
2722         default:
2723           return 0;
2724         }
2725
2726     case tcc_declaration:
2727       /* Consider __builtin_sqrt equal to sqrt.  */
2728       return (TREE_CODE (arg0) == FUNCTION_DECL
2729               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2730               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2731               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2732
2733     default:
2734       return 0;
2735     }
2736
2737 #undef OP_SAME
2738 #undef OP_SAME_WITH_NULL
2739 }
2740 \f
2741 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2742    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2743
2744    When in doubt, return 0.  */
2745
2746 static int
2747 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2748 {
2749   int unsignedp1, unsignedpo;
2750   tree primarg0, primarg1, primother;
2751   unsigned int correct_width;
2752
2753   if (operand_equal_p (arg0, arg1, 0))
2754     return 1;
2755
2756   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2757       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2758     return 0;
2759
2760   /* Discard any conversions that don't change the modes of ARG0 and ARG1
2761      and see if the inner values are the same.  This removes any
2762      signedness comparison, which doesn't matter here.  */
2763   primarg0 = arg0, primarg1 = arg1;
2764   STRIP_NOPS (primarg0);
2765   STRIP_NOPS (primarg1);
2766   if (operand_equal_p (primarg0, primarg1, 0))
2767     return 1;
2768
2769   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2770      actual comparison operand, ARG0.
2771
2772      First throw away any conversions to wider types
2773      already present in the operands.  */
2774
2775   primarg1 = get_narrower (arg1, &unsignedp1);
2776   primother = get_narrower (other, &unsignedpo);
2777
2778   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2779   if (unsignedp1 == unsignedpo
2780       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2781       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2782     {
2783       tree type = TREE_TYPE (arg0);
2784
2785       /* Make sure shorter operand is extended the right way
2786          to match the longer operand.  */
2787       primarg1 = fold_convert (signed_or_unsigned_type_for
2788                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2789
2790       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2791         return 1;
2792     }
2793
2794   return 0;
2795 }
2796 \f
2797 /* See if ARG is an expression that is either a comparison or is performing
2798    arithmetic on comparisons.  The comparisons must only be comparing
2799    two different values, which will be stored in *CVAL1 and *CVAL2; if
2800    they are nonzero it means that some operands have already been found.
2801    No variables may be used anywhere else in the expression except in the
2802    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
2803    the expression and save_expr needs to be called with CVAL1 and CVAL2.
2804
2805    If this is true, return 1.  Otherwise, return zero.  */
2806
2807 static int
2808 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2809 {
2810   enum tree_code code = TREE_CODE (arg);
2811   enum tree_code_class tclass = TREE_CODE_CLASS (code);
2812
2813   /* We can handle some of the tcc_expression cases here.  */
2814   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2815     tclass = tcc_unary;
2816   else if (tclass == tcc_expression
2817            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2818                || code == COMPOUND_EXPR))
2819     tclass = tcc_binary;
2820
2821   else if (tclass == tcc_expression && code == SAVE_EXPR
2822            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2823     {
2824       /* If we've already found a CVAL1 or CVAL2, this expression is
2825          two complex to handle.  */
2826       if (*cval1 || *cval2)
2827         return 0;
2828
2829       tclass = tcc_unary;
2830       *save_p = 1;
2831     }
2832
2833   switch (tclass)
2834     {
2835     case tcc_unary:
2836       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2837
2838     case tcc_binary:
2839       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2840               && twoval_comparison_p (TREE_OPERAND (arg, 1),
2841                                       cval1, cval2, save_p));
2842
2843     case tcc_constant:
2844       return 1;
2845
2846     case tcc_expression:
2847       if (code == COND_EXPR)
2848         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2849                                      cval1, cval2, save_p)
2850                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2851                                         cval1, cval2, save_p)
2852                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2853                                         cval1, cval2, save_p));
2854       return 0;
2855
2856     case tcc_comparison:
2857       /* First see if we can handle the first operand, then the second.  For
2858          the second operand, we know *CVAL1 can't be zero.  It must be that
2859          one side of the comparison is each of the values; test for the
2860          case where this isn't true by failing if the two operands
2861          are the same.  */
2862
2863       if (operand_equal_p (TREE_OPERAND (arg, 0),
2864                            TREE_OPERAND (arg, 1), 0))
2865         return 0;
2866
2867       if (*cval1 == 0)
2868         *cval1 = TREE_OPERAND (arg, 0);
2869       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2870         ;
2871       else if (*cval2 == 0)
2872         *cval2 = TREE_OPERAND (arg, 0);
2873       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2874         ;
2875       else
2876         return 0;
2877
2878       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2879         ;
2880       else if (*cval2 == 0)
2881         *cval2 = TREE_OPERAND (arg, 1);
2882       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2883         ;
2884       else
2885         return 0;
2886
2887       return 1;
2888
2889     default:
2890       return 0;
2891     }
2892 }
2893 \f
2894 /* ARG is a tree that is known to contain just arithmetic operations and
2895    comparisons.  Evaluate the operations in the tree substituting NEW0 for
2896    any occurrence of OLD0 as an operand of a comparison and likewise for
2897    NEW1 and OLD1.  */
2898
2899 static tree
2900 eval_subst (location_t loc, tree arg, tree old0, tree new0,
2901             tree old1, tree new1)
2902 {
2903   tree type = TREE_TYPE (arg);
2904   enum tree_code code = TREE_CODE (arg);
2905   enum tree_code_class tclass = TREE_CODE_CLASS (code);
2906
2907   /* We can handle some of the tcc_expression cases here.  */
2908   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2909     tclass = tcc_unary;
2910   else if (tclass == tcc_expression
2911            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2912     tclass = tcc_binary;
2913
2914   switch (tclass)
2915     {
2916     case tcc_unary:
2917       return fold_build1_loc (loc, code, type,
2918                           eval_subst (loc, TREE_OPERAND (arg, 0),
2919                                       old0, new0, old1, new1));
2920
2921     case tcc_binary:
2922       return fold_build2_loc (loc, code, type,
2923                           eval_subst (loc, TREE_OPERAND (arg, 0),
2924                                       old0, new0, old1, new1),
2925                           eval_subst (loc, TREE_OPERAND (arg, 1),
2926                                       old0, new0, old1, new1));
2927
2928     case tcc_expression:
2929       switch (code)
2930         {
2931         case SAVE_EXPR:
2932           return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
2933                              old1, new1);
2934
2935         case COMPOUND_EXPR:
2936           return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
2937                              old1, new1);
2938
2939         case COND_EXPR:
2940           return fold_build3_loc (loc, code, type,
2941                               eval_subst (loc, TREE_OPERAND (arg, 0),
2942                                           old0, new0, old1, new1),
2943                               eval_subst (loc, TREE_OPERAND (arg, 1),
2944                                           old0, new0, old1, new1),
2945                               eval_subst (loc, TREE_OPERAND (arg, 2),
2946                                           old0, new0, old1, new1));
2947         default:
2948           break;
2949         }
2950       /* Fall through - ???  */
2951
2952     case tcc_comparison:
2953       {
2954         tree arg0 = TREE_OPERAND (arg, 0);
2955         tree arg1 = TREE_OPERAND (arg, 1);
2956
2957         /* We need to check both for exact equality and tree equality.  The
2958            former will be true if the operand has a side-effect.  In that
2959            case, we know the operand occurred exactly once.  */
2960
2961         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2962           arg0 = new0;
2963         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2964           arg0 = new1;
2965
2966         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2967           arg1 = new0;
2968         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2969           arg1 = new1;
2970
2971         return fold_build2_loc (loc, code, type, arg0, arg1);
2972       }
2973
2974     default:
2975       return arg;
2976     }
2977 }
2978 \f
2979 /* Return a tree for the case when the result of an expression is RESULT
2980    converted to TYPE and OMITTED was previously an operand of the expression
2981    but is now not needed (e.g., we folded OMITTED * 0).
2982
2983    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
2984    the conversion of RESULT to TYPE.  */
2985
2986 tree
2987 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
2988 {
2989   tree t = fold_convert_loc (loc, type, result);
2990
2991   /* If the resulting operand is an empty statement, just return the omitted
2992      statement casted to void. */
2993   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
2994     return build1_loc (loc, NOP_EXPR, void_type_node,
2995                        fold_ignored_result (omitted));
2996
2997   if (TREE_SIDE_EFFECTS (omitted))
2998     return build2_loc (loc, COMPOUND_EXPR, type,
2999                        fold_ignored_result (omitted), t);
3000
3001   return non_lvalue_loc (loc, t);
3002 }
3003
3004 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
3005
3006 static tree
3007 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
3008                                tree omitted)
3009 {
3010   tree t = fold_convert_loc (loc, type, result);
3011
3012   /* If the resulting operand is an empty statement, just return the omitted
3013      statement casted to void. */
3014   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3015     return build1_loc (loc, NOP_EXPR, void_type_node,
3016                        fold_ignored_result (omitted));
3017
3018   if (TREE_SIDE_EFFECTS (omitted))
3019     return build2_loc (loc, COMPOUND_EXPR, type,
3020                        fold_ignored_result (omitted), t);
3021
3022   return pedantic_non_lvalue_loc (loc, t);
3023 }
3024
3025 /* Return a tree for the case when the result of an expression is RESULT
3026    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3027    of the expression but are now not needed.
3028
3029    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3030    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3031    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3032    just do the conversion of RESULT to TYPE.  */
3033
3034 tree
3035 omit_two_operands_loc (location_t loc, tree type, tree result,
3036                        tree omitted1, tree omitted2)
3037 {
3038   tree t = fold_convert_loc (loc, type, result);
3039
3040   if (TREE_SIDE_EFFECTS (omitted2))
3041     t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3042   if (TREE_SIDE_EFFECTS (omitted1))
3043     t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3044
3045   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3046 }
3047
3048 \f
3049 /* Return a simplified tree node for the truth-negation of ARG.  This
3050    never alters ARG itself.  We assume that ARG is an operation that
3051    returns a truth value (0 or 1).
3052
3053    FIXME: one would think we would fold the result, but it causes
3054    problems with the dominator optimizer.  */
3055
3056 tree
3057 fold_truth_not_expr (location_t loc, tree arg)
3058 {
3059   tree type = TREE_TYPE (arg);
3060   enum tree_code code = TREE_CODE (arg);
3061   location_t loc1, loc2;
3062
3063   /* If this is a comparison, we can simply invert it, except for
3064      floating-point non-equality comparisons, in which case we just
3065      enclose a TRUTH_NOT_EXPR around what we have.  */
3066
3067   if (TREE_CODE_CLASS (code) == tcc_comparison)
3068     {
3069       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3070       if (FLOAT_TYPE_P (op_type)
3071           && flag_trapping_math
3072           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3073           && code != NE_EXPR && code != EQ_EXPR)
3074         return NULL_TREE;
3075
3076       code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3077       if (code == ERROR_MARK)
3078         return NULL_TREE;
3079
3080       return build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3081                          TREE_OPERAND (arg, 1));
3082     }
3083
3084   switch (code)
3085     {
3086     case INTEGER_CST:
3087       return constant_boolean_node (integer_zerop (arg), type);
3088
3089     case TRUTH_AND_EXPR:
3090       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3091       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3092       return build2_loc (loc, TRUTH_OR_EXPR, type,
3093                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3094                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3095
3096     case TRUTH_OR_EXPR:
3097       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3098       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3099       return build2_loc (loc, TRUTH_AND_EXPR, type,
3100                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3101                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3102
3103     case TRUTH_XOR_EXPR:
3104       /* Here we can invert either operand.  We invert the first operand
3105          unless the second operand is a TRUTH_NOT_EXPR in which case our
3106          result is the XOR of the first operand with the inside of the
3107          negation of the second operand.  */
3108
3109       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3110         return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3111                            TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3112       else
3113         return build2_loc (loc, TRUTH_XOR_EXPR, type,
3114                            invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3115                            TREE_OPERAND (arg, 1));
3116
3117     case TRUTH_ANDIF_EXPR:
3118       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3119       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3120       return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3121                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3122                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3123
3124     case TRUTH_ORIF_EXPR:
3125       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3126       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3127       return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3128                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3129                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3130
3131     case TRUTH_NOT_EXPR:
3132       return TREE_OPERAND (arg, 0);
3133
3134     case COND_EXPR:
3135       {
3136         tree arg1 = TREE_OPERAND (arg, 1);
3137         tree arg2 = TREE_OPERAND (arg, 2);
3138
3139         loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3140         loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3141
3142         /* A COND_EXPR may have a throw as one operand, which
3143            then has void type.  Just leave void operands
3144            as they are.  */
3145         return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3146                            VOID_TYPE_P (TREE_TYPE (arg1))
3147                            ? arg1 : invert_truthvalue_loc (loc1, arg1),
3148                            VOID_TYPE_P (TREE_TYPE (arg2))
3149                            ? arg2 : invert_truthvalue_loc (loc2, arg2));
3150       }
3151
3152     case COMPOUND_EXPR:
3153       loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3154       return build2_loc (loc, COMPOUND_EXPR, type,
3155                          TREE_OPERAND (arg, 0),
3156                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3157
3158     case NON_LVALUE_EXPR:
3159       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3160       return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3161
3162     CASE_CONVERT:
3163       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3164         return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3165
3166       /* ... fall through ...  */
3167
3168     case FLOAT_EXPR:
3169       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3170       return build1_loc (loc, TREE_CODE (arg), type,
3171                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3172
3173     case BIT_AND_EXPR:
3174       if (!integer_onep (TREE_OPERAND (arg, 1)))
3175         return NULL_TREE;
3176       return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3177
3178     case SAVE_EXPR:
3179       return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3180
3181     case CLEANUP_POINT_EXPR:
3182       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3183       return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3184                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3185
3186     default:
3187       return NULL_TREE;
3188     }
3189 }
3190
3191 /* Return a simplified tree node for the truth-negation of ARG.  This
3192    never alters ARG itself.  We assume that ARG is an operation that
3193    returns a truth value (0 or 1).
3194
3195    FIXME: one would think we would fold the result, but it causes
3196    problems with the dominator optimizer.  */
3197
3198 tree
3199 invert_truthvalue_loc (location_t loc, tree arg)
3200 {
3201   tree tem;
3202
3203   if (TREE_CODE (arg) == ERROR_MARK)
3204     return arg;
3205
3206   tem = fold_truth_not_expr (loc, arg);
3207   if (!tem)
3208     tem = build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3209
3210   return tem;
3211 }
3212
3213 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3214    operands are another bit-wise operation with a common input.  If so,
3215    distribute the bit operations to save an operation and possibly two if
3216    constants are involved.  For example, convert
3217         (A | B) & (A | C) into A | (B & C)
3218    Further simplification will occur if B and C are constants.
3219
3220    If this optimization cannot be done, 0 will be returned.  */
3221
3222 static tree
3223 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
3224                      tree arg0, tree arg1)
3225 {
3226   tree common;
3227   tree left, right;
3228
3229   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3230       || TREE_CODE (arg0) == code
3231       || (TREE_CODE (arg0) != BIT_AND_EXPR
3232           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3233     return 0;
3234
3235   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3236     {
3237       common = TREE_OPERAND (arg0, 0);
3238       left = TREE_OPERAND (arg0, 1);
3239       right = TREE_OPERAND (arg1, 1);
3240     }
3241   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3242     {
3243       common = TREE_OPERAND (arg0, 0);
3244       left = TREE_OPERAND (arg0, 1);
3245       right = TREE_OPERAND (arg1, 0);
3246     }
3247   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3248     {
3249       common = TREE_OPERAND (arg0, 1);
3250       left = TREE_OPERAND (arg0, 0);
3251       right = TREE_OPERAND (arg1, 1);
3252     }
3253   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3254     {
3255       common = TREE_OPERAND (arg0, 1);
3256       left = TREE_OPERAND (arg0, 0);
3257       right = TREE_OPERAND (arg1, 0);
3258     }
3259   else
3260     return 0;
3261
3262   common = fold_convert_loc (loc, type, common);
3263   left = fold_convert_loc (loc, type, left);
3264   right = fold_convert_loc (loc, type, right);
3265   return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
3266                       fold_build2_loc (loc, code, type, left, right));
3267 }
3268
3269 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3270    with code CODE.  This optimization is unsafe.  */
3271 static tree
3272 distribute_real_division (location_t loc, enum tree_code code, tree type,
3273                           tree arg0, tree arg1)
3274 {
3275   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3276   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3277
3278   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3279   if (mul0 == mul1
3280       && operand_equal_p (TREE_OPERAND (arg0, 1),
3281                        TREE_OPERAND (arg1, 1), 0))
3282     return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3283                         fold_build2_loc (loc, code, type,
3284                                      TREE_OPERAND (arg0, 0),
3285                                      TREE_OPERAND (arg1, 0)),
3286                         TREE_OPERAND (arg0, 1));
3287
3288   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3289   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3290                        TREE_OPERAND (arg1, 0), 0)
3291       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3292       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3293     {
3294       REAL_VALUE_TYPE r0, r1;
3295       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3296       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3297       if (!mul0)
3298         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3299       if (!mul1)
3300         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3301       real_arithmetic (&r0, code, &r0, &r1);
3302       return fold_build2_loc (loc, MULT_EXPR, type,
3303                           TREE_OPERAND (arg0, 0),
3304                           build_real (type, r0));
3305     }
3306
3307   return NULL_TREE;
3308 }
3309 \f
3310 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3311    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3312
3313 static tree
3314 make_bit_field_ref (location_t loc, tree inner, tree type,
3315                     HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3316 {
3317   tree result, bftype;
3318
3319   if (bitpos == 0)
3320     {
3321       tree size = TYPE_SIZE (TREE_TYPE (inner));
3322       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3323            || POINTER_TYPE_P (TREE_TYPE (inner)))
3324           && host_integerp (size, 0)
3325           && tree_low_cst (size, 0) == bitsize)
3326         return fold_convert_loc (loc, type, inner);
3327     }
3328
3329   bftype = type;
3330   if (TYPE_PRECISION (bftype) != bitsize
3331       || TYPE_UNSIGNED (bftype) == !unsignedp)
3332     bftype = build_nonstandard_integer_type (bitsize, 0);
3333
3334   result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3335                        size_int (bitsize), bitsize_int (bitpos));
3336
3337   if (bftype != type)
3338     result = fold_convert_loc (loc, type, result);
3339
3340   return result;
3341 }
3342
3343 /* Optimize a bit-field compare.
3344
3345    There are two cases:  First is a compare against a constant and the
3346    second is a comparison of two items where the fields are at the same
3347    bit position relative to the start of a chunk (byte, halfword, word)
3348    large enough to contain it.  In these cases we can avoid the shift
3349    implicit in bitfield extractions.
3350
3351    For constants, we emit a compare of the shifted constant with the
3352    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3353    compared.  For two fields at the same position, we do the ANDs with the
3354    similar mask and compare the result of the ANDs.
3355
3356    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3357    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3358    are the left and right operands of the comparison, respectively.
3359
3360    If the optimization described above can be done, we return the resulting
3361    tree.  Otherwise we return zero.  */
3362
3363 static tree
3364 optimize_bit_field_compare (location_t loc, enum tree_code code,
3365                             tree compare_type, tree lhs, tree rhs)
3366 {
3367   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3368   tree type = TREE_TYPE (lhs);
3369   tree signed_type, unsigned_type;
3370   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3371   enum machine_mode lmode, rmode, nmode;
3372   int lunsignedp, runsignedp;
3373   int lvolatilep = 0, rvolatilep = 0;
3374   tree linner, rinner = NULL_TREE;
3375   tree mask;
3376   tree offset;
3377
3378   /* Get all the information about the extractions being done.  If the bit size
3379      if the same as the size of the underlying object, we aren't doing an
3380      extraction at all and so can do nothing.  We also don't want to
3381      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3382      then will no longer be able to replace it.  */
3383   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3384                                 &lunsignedp, &lvolatilep, false);
3385   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3386       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3387     return 0;
3388
3389  if (!const_p)
3390    {
3391      /* If this is not a constant, we can only do something if bit positions,
3392         sizes, and signedness are the same.  */
3393      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3394                                    &runsignedp, &rvolatilep, false);
3395
3396      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3397          || lunsignedp != runsignedp || offset != 0
3398          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3399        return 0;
3400    }
3401
3402   /* See if we can find a mode to refer to this field.  We should be able to,
3403      but fail if we can't.  */
3404   if (lvolatilep
3405       && GET_MODE_BITSIZE (lmode) > 0
3406       && flag_strict_volatile_bitfields > 0)
3407     nmode = lmode;
3408   else
3409     nmode = get_best_mode (lbitsize, lbitpos, 0, 0,
3410                            const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3411                            : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3412                                   TYPE_ALIGN (TREE_TYPE (rinner))),
3413                            word_mode, lvolatilep || rvolatilep);
3414   if (nmode == VOIDmode)
3415     return 0;
3416
3417   /* Set signed and unsigned types of the precision of this mode for the
3418      shifts below.  */
3419   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3420   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3421
3422   /* Compute the bit position and size for the new reference and our offset
3423      within it. If the new reference is the same size as the original, we
3424      won't optimize anything, so return zero.  */
3425   nbitsize = GET_MODE_BITSIZE (nmode);
3426   nbitpos = lbitpos & ~ (nbitsize - 1);
3427   lbitpos -= nbitpos;
3428   if (nbitsize == lbitsize)
3429     return 0;
3430
3431   if (BYTES_BIG_ENDIAN)
3432     lbitpos = nbitsize - lbitsize - lbitpos;
3433
3434   /* Make the mask to be used against the extracted field.  */
3435   mask = build_int_cst_type (unsigned_type, -1);
3436   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3437   mask = const_binop (RSHIFT_EXPR, mask,
3438                       size_int (nbitsize - lbitsize - lbitpos));
3439
3440   if (! const_p)
3441     /* If not comparing with constant, just rework the comparison
3442        and return.  */
3443     return fold_build2_loc (loc, code, compare_type,
3444                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3445                                      make_bit_field_ref (loc, linner,
3446                                                          unsigned_type,
3447                                                          nbitsize, nbitpos,
3448                                                          1),
3449                                      mask),
3450                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3451                                      make_bit_field_ref (loc, rinner,
3452                                                          unsigned_type,
3453                                                          nbitsize, nbitpos,
3454                                                          1),
3455                                      mask));
3456
3457   /* Otherwise, we are handling the constant case. See if the constant is too
3458      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
3459      this not only for its own sake, but to avoid having to test for this
3460      error case below.  If we didn't, we might generate wrong code.
3461
3462      For unsigned fields, the constant shifted right by the field length should
3463      be all zero.  For signed fields, the high-order bits should agree with
3464      the sign bit.  */
3465
3466   if (lunsignedp)
3467     {
3468       if (! integer_zerop (const_binop (RSHIFT_EXPR,
3469                                         fold_convert_loc (loc,
3470                                                           unsigned_type, rhs),
3471                                         size_int (lbitsize))))
3472         {
3473           warning (0, "comparison is always %d due to width of bit-field",
3474                    code == NE_EXPR);
3475           return constant_boolean_node (code == NE_EXPR, compare_type);
3476         }
3477     }
3478   else
3479     {
3480       tree tem = const_binop (RSHIFT_EXPR,
3481                               fold_convert_loc (loc, signed_type, rhs),
3482                               size_int (lbitsize - 1));
3483       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3484         {
3485           warning (0, "comparison is always %d due to width of bit-field",
3486                    code == NE_EXPR);
3487           return constant_boolean_node (code == NE_EXPR, compare_type);
3488         }
3489     }
3490
3491   /* Single-bit compares should always be against zero.  */
3492   if (lbitsize == 1 && ! integer_zerop (rhs))
3493     {
3494       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3495       rhs = build_int_cst (type, 0);
3496     }
3497
3498   /* Make a new bitfield reference, shift the constant over the
3499      appropriate number of bits and mask it with the computed mask
3500      (in case this was a signed field).  If we changed it, make a new one.  */
3501   lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3502   if (lvolatilep)
3503     {
3504       TREE_SIDE_EFFECTS (lhs) = 1;
3505       TREE_THIS_VOLATILE (lhs) = 1;
3506     }
3507
3508   rhs = const_binop (BIT_AND_EXPR,
3509                      const_binop (LSHIFT_EXPR,
3510                                   fold_convert_loc (loc, unsigned_type, rhs),
3511                                   size_int (lbitpos)),
3512                      mask);
3513
3514   lhs = build2_loc (loc, code, compare_type,
3515                     build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
3516   return lhs;
3517 }
3518 \f
3519 /* Subroutine for fold_truth_andor_1: decode a field reference.
3520
3521    If EXP is a comparison reference, we return the innermost reference.
3522
3523    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3524    set to the starting bit number.
3525
3526    If the innermost field can be completely contained in a mode-sized
3527    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
3528
3529    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3530    otherwise it is not changed.
3531
3532    *PUNSIGNEDP is set to the signedness of the field.
3533
3534    *PMASK is set to the mask used.  This is either contained in a
3535    BIT_AND_EXPR or derived from the width of the field.
3536
3537    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3538
3539    Return 0 if this is not a component reference or is one that we can't
3540    do anything with.  */
3541
3542 static tree
3543 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3544                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3545                         int *punsignedp, int *pvolatilep,
3546                         tree *pmask, tree *pand_mask)
3547 {
3548   tree outer_type = 0;
3549   tree and_mask = 0;
3550   tree mask, inner, offset;
3551   tree unsigned_type;
3552   unsigned int precision;
3553
3554   /* All the optimizations using this function assume integer fields.
3555      There are problems with FP fields since the type_for_size call
3556      below can fail for, e.g., XFmode.  */
3557   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3558     return 0;
3559
3560   /* We are interested in the bare arrangement of bits, so strip everything
3561      that doesn't affect the machine mode.  However, record the type of the
3562      outermost expression if it may matter below.  */
3563   if (CONVERT_EXPR_P (exp)
3564       || TREE_CODE (exp) == NON_LVALUE_EXPR)
3565     outer_type = TREE_TYPE (exp);
3566   STRIP_NOPS (exp);
3567
3568   if (TREE_CODE (exp) == BIT_AND_EXPR)
3569     {
3570       and_mask = TREE_OPERAND (exp, 1);
3571       exp = TREE_OPERAND (exp, 0);
3572       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3573       if (TREE_CODE (and_mask) != INTEGER_CST)
3574         return 0;
3575     }
3576
3577   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3578                                punsignedp, pvolatilep, false);
3579   if ((inner == exp && and_mask == 0)
3580       || *pbitsize < 0 || offset != 0
3581       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3582     return 0;
3583
3584   /* If the number of bits in the reference is the same as the bitsize of
3585      the outer type, then the outer type gives the signedness. Otherwise
3586      (in case of a small bitfield) the signedness is unchanged.  */
3587   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3588     *punsignedp = TYPE_UNSIGNED (outer_type);
3589
3590   /* Compute the mask to access the bitfield.  */
3591   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3592   precision = TYPE_PRECISION (unsigned_type);
3593
3594   mask = build_int_cst_type (unsigned_type, -1);
3595
3596   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3597   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3598
3599   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
3600   if (and_mask != 0)
3601     mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3602                         fold_convert_loc (loc, unsigned_type, and_mask), mask);
3603
3604   *pmask = mask;
3605   *pand_mask = and_mask;
3606   return inner;
3607 }
3608
3609 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3610    bit positions.  */
3611
3612 static int
3613 all_ones_mask_p (const_tree mask, int size)
3614 {
3615   tree type = TREE_TYPE (mask);
3616   unsigned int precision = TYPE_PRECISION (type);
3617   tree tmask;
3618
3619   tmask = build_int_cst_type (signed_type_for (type), -1);
3620
3621   return
3622     tree_int_cst_equal (mask,
3623                         const_binop (RSHIFT_EXPR,
3624                                      const_binop (LSHIFT_EXPR, tmask,
3625                                                   size_int (precision - size)),
3626                                      size_int (precision - size)));
3627 }
3628
3629 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3630    represents the sign bit of EXP's type.  If EXP represents a sign
3631    or zero extension, also test VAL against the unextended type.
3632    The return value is the (sub)expression whose sign bit is VAL,
3633    or NULL_TREE otherwise.  */
3634
3635 static tree
3636 sign_bit_p (tree exp, const_tree val)
3637 {
3638   unsigned HOST_WIDE_INT mask_lo, lo;
3639   HOST_WIDE_INT mask_hi, hi;
3640   int width;
3641   tree t;
3642
3643   /* Tree EXP must have an integral type.  */
3644   t = TREE_TYPE (exp);
3645   if (! INTEGRAL_TYPE_P (t))
3646     return NULL_TREE;
3647
3648   /* Tree VAL must be an integer constant.  */
3649   if (TREE_CODE (val) != INTEGER_CST
3650       || TREE_OVERFLOW (val))
3651     return NULL_TREE;
3652
3653   width = TYPE_PRECISION (t);
3654   if (width > HOST_BITS_PER_WIDE_INT)
3655     {
3656       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3657       lo = 0;
3658
3659       mask_hi = ((unsigned HOST_WIDE_INT) -1
3660                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
3661       mask_lo = -1;
3662     }
3663   else
3664     {
3665       hi = 0;
3666       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3667
3668       mask_hi = 0;
3669       mask_lo = ((unsigned HOST_WIDE_INT) -1
3670                  >> (HOST_BITS_PER_WIDE_INT - width));
3671     }
3672
3673   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3674      treat VAL as if it were unsigned.  */
3675   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3676       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3677     return exp;
3678
3679   /* Handle extension from a narrower type.  */
3680   if (TREE_CODE (exp) == NOP_EXPR
3681       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3682     return sign_bit_p (TREE_OPERAND (exp, 0), val);
3683
3684   return NULL_TREE;
3685 }
3686
3687 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
3688    to be evaluated unconditionally.  */
3689
3690 static int
3691 simple_operand_p (const_tree exp)
3692 {
3693   /* Strip any conversions that don't change the machine mode.  */
3694   STRIP_NOPS (exp);
3695
3696   return (CONSTANT_CLASS_P (exp)
3697           || TREE_CODE (exp) == SSA_NAME
3698           || (DECL_P (exp)
3699               && ! TREE_ADDRESSABLE (exp)
3700               && ! TREE_THIS_VOLATILE (exp)
3701               && ! DECL_NONLOCAL (exp)
3702               /* Don't regard global variables as simple.  They may be
3703                  allocated in ways unknown to the compiler (shared memory,
3704                  #pragma weak, etc).  */
3705               && ! TREE_PUBLIC (exp)
3706               && ! DECL_EXTERNAL (exp)
3707               /* Loading a static variable is unduly expensive, but global
3708                  registers aren't expensive.  */
3709               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3710 }
3711
3712 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
3713    to be evaluated unconditionally.
3714    I addition to simple_operand_p, we assume that comparisons, conversions,
3715    and logic-not operations are simple, if their operands are simple, too.  */
3716
3717 static bool
3718 simple_operand_p_2 (tree exp)
3719 {
3720   enum tree_code code;
3721
3722   if (TREE_SIDE_EFFECTS (exp)
3723       || tree_could_trap_p (exp))
3724     return false;
3725
3726   while (CONVERT_EXPR_P (exp))
3727     exp = TREE_OPERAND (exp, 0);
3728
3729   code = TREE_CODE (exp);
3730
3731   if (TREE_CODE_CLASS (code) == tcc_comparison)
3732     return (simple_operand_p (TREE_OPERAND (exp, 0))
3733             && simple_operand_p (TREE_OPERAND (exp, 1)));
3734
3735   if (code == TRUTH_NOT_EXPR)
3736       return simple_operand_p_2 (TREE_OPERAND (exp, 0));
3737
3738   return simple_operand_p (exp);
3739 }
3740
3741 \f
3742 /* The following functions are subroutines to fold_range_test and allow it to
3743    try to change a logical combination of comparisons into a range test.
3744
3745    For example, both
3746         X == 2 || X == 3 || X == 4 || X == 5
3747    and
3748         X >= 2 && X <= 5
3749    are converted to
3750         (unsigned) (X - 2) <= 3
3751
3752    We describe each set of comparisons as being either inside or outside
3753    a range, using a variable named like IN_P, and then describe the
3754    range with a lower and upper bound.  If one of the bounds is omitted,
3755    it represents either the highest or lowest value of the type.
3756
3757    In the comments below, we represent a range by two numbers in brackets
3758    preceded by a "+" to designate being inside that range, or a "-" to
3759    designate being outside that range, so the condition can be inverted by
3760    flipping the prefix.  An omitted bound is represented by a "-".  For
3761    example, "- [-, 10]" means being outside the range starting at the lowest
3762    possible value and ending at 10, in other words, being greater than 10.
3763    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3764    always false.
3765
3766    We set up things so that the missing bounds are handled in a consistent
3767    manner so neither a missing bound nor "true" and "false" need to be
3768    handled using a special case.  */
3769
3770 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3771    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3772    and UPPER1_P are nonzero if the respective argument is an upper bound
3773    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
3774    must be specified for a comparison.  ARG1 will be converted to ARG0's
3775    type if both are specified.  */
3776
3777 static tree
3778 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3779              tree arg1, int upper1_p)
3780 {
3781   tree tem;
3782   int result;
3783   int sgn0, sgn1;
3784
3785   /* If neither arg represents infinity, do the normal operation.
3786      Else, if not a comparison, return infinity.  Else handle the special
3787      comparison rules. Note that most of the cases below won't occur, but
3788      are handled for consistency.  */
3789
3790   if (arg0 != 0 && arg1 != 0)
3791     {
3792       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3793                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
3794       STRIP_NOPS (tem);
3795       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3796     }
3797
3798   if (TREE_CODE_CLASS (code) != tcc_comparison)
3799     return 0;
3800
3801   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3802      for neither.  In real maths, we cannot assume open ended ranges are
3803      the same. But, this is computer arithmetic, where numbers are finite.
3804      We can therefore make the transformation of any unbounded range with
3805      the value Z, Z being greater than any representable number. This permits
3806      us to treat unbounded ranges as equal.  */
3807   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3808   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3809   switch (code)
3810     {
3811     case EQ_EXPR:
3812       result = sgn0 == sgn1;
3813       break;
3814     case NE_EXPR:
3815       result = sgn0 != sgn1;
3816       break;
3817     case LT_EXPR:
3818       result = sgn0 < sgn1;
3819       break;
3820     case LE_EXPR:
3821       result = sgn0 <= sgn1;
3822       break;
3823     case GT_EXPR:
3824       result = sgn0 > sgn1;
3825       break;
3826     case GE_EXPR:
3827       result = sgn0 >= sgn1;
3828       break;
3829     default:
3830       gcc_unreachable ();
3831     }
3832
3833   return constant_boolean_node (result, type);
3834 }
3835 \f
3836 /* Helper routine for make_range.  Perform one step for it, return
3837    new expression if the loop should continue or NULL_TREE if it should
3838    stop.  */
3839
3840 tree
3841 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
3842                  tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
3843                  bool *strict_overflow_p)
3844 {
3845   tree arg0_type = TREE_TYPE (arg0);
3846   tree n_low, n_high, low = *p_low, high = *p_high;
3847   int in_p = *p_in_p, n_in_p;
3848
3849   switch (code)
3850     {
3851     case TRUTH_NOT_EXPR:
3852       /* We can only do something if the range is testing for zero.  */
3853       if (low == NULL_TREE || high == NULL_TREE
3854           || ! integer_zerop (low) || ! integer_zerop (high))
3855         return NULL_TREE;
3856       *p_in_p = ! in_p;
3857       return arg0;
3858
3859     case EQ_EXPR: case NE_EXPR:
3860     case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3861       /* We can only do something if the range is testing for zero
3862          and if the second operand is an integer constant.  Note that
3863          saying something is "in" the range we make is done by
3864          complementing IN_P since it will set in the initial case of
3865          being not equal to zero; "out" is leaving it alone.  */
3866       if (low == NULL_TREE || high == NULL_TREE
3867           || ! integer_zerop (low) || ! integer_zerop (high)
3868           || TREE_CODE (arg1) != INTEGER_CST)
3869         return NULL_TREE;
3870
3871       switch (code)
3872         {
3873         case NE_EXPR:  /* - [c, c]  */
3874           low = high = arg1;
3875           break;
3876         case EQ_EXPR:  /* + [c, c]  */
3877           in_p = ! in_p, low = high = arg1;
3878           break;
3879         case GT_EXPR:  /* - [-, c] */
3880           low = 0, high = arg1;
3881           break;
3882         case GE_EXPR:  /* + [c, -] */
3883           in_p = ! in_p, low = arg1, high = 0;
3884           break;
3885         case LT_EXPR:  /* - [c, -] */
3886           low = arg1, high = 0;
3887           break;
3888         case LE_EXPR:  /* + [-, c] */
3889           in_p = ! in_p, low = 0, high = arg1;
3890           break;
3891         default:
3892           gcc_unreachable ();
3893         }
3894
3895       /* If this is an unsigned comparison, we also know that EXP is
3896          greater than or equal to zero.  We base the range tests we make
3897          on that fact, so we record it here so we can parse existing
3898          range tests.  We test arg0_type since often the return type
3899          of, e.g. EQ_EXPR, is boolean.  */
3900       if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3901         {
3902           if (! merge_ranges (&n_in_p, &n_low, &n_high,
3903                               in_p, low, high, 1,
3904                               build_int_cst (arg0_type, 0),
3905                               NULL_TREE))
3906             return NULL_TREE;
3907
3908           in_p = n_in_p, low = n_low, high = n_high;
3909
3910           /* If the high bound is missing, but we have a nonzero low
3911              bound, reverse the range so it goes from zero to the low bound
3912              minus 1.  */
3913           if (high == 0 && low && ! integer_zerop (low))
3914             {
3915               in_p = ! in_p;
3916               high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3917                                   integer_one_node, 0);
3918               low = build_int_cst (arg0_type, 0);
3919             }
3920         }
3921
3922       *p_low = low;
3923       *p_high = high;
3924       *p_in_p = in_p;
3925       return arg0;
3926
3927     case NEGATE_EXPR:
3928       /* If flag_wrapv and ARG0_TYPE is signed, make sure
3929          low and high are non-NULL, then normalize will DTRT.  */
3930       if (!TYPE_UNSIGNED (arg0_type)
3931           && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
3932         {
3933           if (low == NULL_TREE)
3934             low = TYPE_MIN_VALUE (arg0_type);
3935           if (high == NULL_TREE)
3936             high = TYPE_MAX_VALUE (arg0_type);
3937         }
3938
3939       /* (-x) IN [a,b] -> x in [-b, -a]  */
3940       n_low = range_binop (MINUS_EXPR, exp_type,
3941                            build_int_cst (exp_type, 0),
3942                            0, high, 1);
3943       n_high = range_binop (MINUS_EXPR, exp_type,
3944                             build_int_cst (exp_type, 0),
3945                             0, low, 0);
3946       if (n_high != 0 && TREE_OVERFLOW (n_high))
3947         return NULL_TREE;
3948       goto normalize;
3949
3950     case BIT_NOT_EXPR:
3951       /* ~ X -> -X - 1  */
3952       return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
3953                          build_int_cst (exp_type, 1));
3954
3955     case PLUS_EXPR:
3956     case MINUS_EXPR:
3957       if (TREE_CODE (arg1) != INTEGER_CST)
3958         return NULL_TREE;
3959
3960       /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
3961          move a constant to the other side.  */
3962       if (!TYPE_UNSIGNED (arg0_type)
3963           && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
3964         return NULL_TREE;
3965
3966       /* If EXP is signed, any overflow in the computation is undefined,
3967          so we don't worry about it so long as our computations on
3968          the bounds don't overflow.  For unsigned, overflow is defined
3969          and this is exactly the right thing.  */
3970       n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3971                            arg0_type, low, 0, arg1, 0);
3972       n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3973                             arg0_type, high, 1, arg1, 0);
3974       if ((n_low != 0 && TREE_OVERFLOW (n_low))
3975           || (n_high != 0 && TREE_OVERFLOW (n_high)))
3976         return NULL_TREE;
3977
3978       if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
3979         *strict_overflow_p = true;
3980
3981       normalize:
3982         /* Check for an unsigned range which has wrapped around the maximum
3983            value thus making n_high < n_low, and normalize it.  */
3984         if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3985           {
3986             low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
3987                                integer_one_node, 0);
3988             high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
3989                                 integer_one_node, 0);
3990
3991             /* If the range is of the form +/- [ x+1, x ], we won't
3992                be able to normalize it.  But then, it represents the
3993                whole range or the empty set, so make it
3994                +/- [ -, - ].  */
3995             if (tree_int_cst_equal (n_low, low)
3996                 && tree_int_cst_equal (n_high, high))
3997               low = high = 0;
3998             else
3999               in_p = ! in_p;
4000           }
4001         else
4002           low = n_low, high = n_high;
4003
4004         *p_low = low;
4005         *p_high = high;
4006         *p_in_p = in_p;
4007         return arg0;
4008
4009     CASE_CONVERT:
4010     case NON_LVALUE_EXPR:
4011       if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4012         return NULL_TREE;
4013
4014       if (! INTEGRAL_TYPE_P (arg0_type)
4015           || (low != 0 && ! int_fits_type_p (low, arg0_type))
4016           || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4017         return NULL_TREE;
4018
4019       n_low = low, n_high = high;
4020
4021       if (n_low != 0)
4022         n_low = fold_convert_loc (loc, arg0_type, n_low);
4023
4024       if (n_high != 0)
4025         n_high = fold_convert_loc (loc, arg0_type, n_high);
4026
4027       /* If we're converting arg0 from an unsigned type, to exp,
4028          a signed type,  we will be doing the comparison as unsigned.
4029          The tests above have already verified that LOW and HIGH
4030          are both positive.
4031
4032          So we have to ensure that we will handle large unsigned
4033          values the same way that the current signed bounds treat
4034          negative values.  */
4035
4036       if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4037         {
4038           tree high_positive;
4039           tree equiv_type;
4040           /* For fixed-point modes, we need to pass the saturating flag
4041              as the 2nd parameter.  */
4042           if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4043             equiv_type
4044               = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4045                                                 TYPE_SATURATING (arg0_type));
4046           else
4047             equiv_type
4048               = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4049
4050           /* A range without an upper bound is, naturally, unbounded.
4051              Since convert would have cropped a very large value, use
4052              the max value for the destination type.  */
4053           high_positive
4054             = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4055               : TYPE_MAX_VALUE (arg0_type);
4056
4057           if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4058             high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4059                                              fold_convert_loc (loc, arg0_type,
4060                                                                high_positive),
4061                                              build_int_cst (arg0_type, 1));
4062
4063           /* If the low bound is specified, "and" the range with the
4064              range for which the original unsigned value will be
4065              positive.  */
4066           if (low != 0)
4067             {
4068               if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4069                                   1, fold_convert_loc (loc, arg0_type,
4070                                                        integer_zero_node),
4071                                   high_positive))
4072                 return NULL_TREE;
4073
4074               in_p = (n_in_p == in_p);
4075             }
4076           else
4077             {
4078               /* Otherwise, "or" the range with the range of the input
4079                  that will be interpreted as negative.  */
4080               if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4081                                   1, fold_convert_loc (loc, arg0_type,
4082                                                        integer_zero_node),
4083                                   high_positive))
4084                 return NULL_TREE;
4085
4086               in_p = (in_p != n_in_p);
4087             }
4088         }
4089
4090       *p_low = n_low;
4091       *p_high = n_high;
4092       *p_in_p = in_p;
4093       return arg0;
4094
4095     default:
4096       return NULL_TREE;
4097     }
4098 }
4099
4100 /* Given EXP, a logical expression, set the range it is testing into
4101    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
4102    actually being tested.  *PLOW and *PHIGH will be made of the same
4103    type as the returned expression.  If EXP is not a comparison, we
4104    will most likely not be returning a useful value and range.  Set
4105    *STRICT_OVERFLOW_P to true if the return value is only valid
4106    because signed overflow is undefined; otherwise, do not change
4107    *STRICT_OVERFLOW_P.  */
4108
4109 tree
4110 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4111             bool *strict_overflow_p)
4112 {
4113   enum tree_code code;
4114   tree arg0, arg1 = NULL_TREE;
4115   tree exp_type, nexp;
4116   int in_p;
4117   tree low, high;
4118   location_t loc = EXPR_LOCATION (exp);
4119
4120   /* Start with simply saying "EXP != 0" and then look at the code of EXP
4121      and see if we can refine the range.  Some of the cases below may not
4122      happen, but it doesn't seem worth worrying about this.  We "continue"
4123      the outer loop when we've changed something; otherwise we "break"
4124      the switch, which will "break" the while.  */
4125
4126   in_p = 0;
4127   low = high = build_int_cst (TREE_TYPE (exp), 0);
4128
4129   while (1)
4130     {
4131       code = TREE_CODE (exp);
4132       exp_type = TREE_TYPE (exp);
4133       arg0 = NULL_TREE;
4134
4135       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4136         {
4137           if (TREE_OPERAND_LENGTH (exp) > 0)
4138             arg0 = TREE_OPERAND (exp, 0);
4139           if (TREE_CODE_CLASS (code) == tcc_binary
4140               || TREE_CODE_CLASS (code) == tcc_comparison
4141               || (TREE_CODE_CLASS (code) == tcc_expression
4142                   && TREE_OPERAND_LENGTH (exp) > 1))
4143             arg1 = TREE_OPERAND (exp, 1);
4144         }
4145       if (arg0 == NULL_TREE)
4146         break;
4147
4148       nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4149                               &high, &in_p, strict_overflow_p);
4150       if (nexp == NULL_TREE)
4151         break;
4152       exp = nexp;
4153     }
4154
4155   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4156   if (TREE_CODE (exp) == INTEGER_CST)
4157     {
4158       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4159                                                  exp, 0, low, 0))
4160                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4161                                                     exp, 1, high, 1)));
4162       low = high = 0;
4163       exp = 0;
4164     }
4165
4166   *pin_p = in_p, *plow = low, *phigh = high;
4167   return exp;
4168 }
4169 \f
4170 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4171    type, TYPE, return an expression to test if EXP is in (or out of, depending
4172    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4173
4174 tree
4175 build_range_check (location_t loc, tree type, tree exp, int in_p,
4176                    tree low, tree high)
4177 {
4178   tree etype = TREE_TYPE (exp), value;
4179
4180 #ifdef HAVE_canonicalize_funcptr_for_compare
4181   /* Disable this optimization for function pointer expressions
4182      on targets that require function pointer canonicalization.  */
4183   if (HAVE_canonicalize_funcptr_for_compare
4184       && TREE_CODE (etype) == POINTER_TYPE
4185       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4186     return NULL_TREE;
4187 #endif
4188
4189   if (! in_p)
4190     {
4191       value = build_range_check (loc, type, exp, 1, low, high);
4192       if (value != 0)
4193         return invert_truthvalue_loc (loc, value);
4194
4195       return 0;
4196     }
4197
4198   if (low == 0 && high == 0)
4199     return build_int_cst (type, 1);
4200
4201   if (low == 0)
4202     return fold_build2_loc (loc, LE_EXPR, type, exp,
4203                         fold_convert_loc (loc, etype, high));
4204
4205   if (high == 0)
4206     return fold_build2_loc (loc, GE_EXPR, type, exp,
4207                         fold_convert_loc (loc, etype, low));
4208
4209   if (operand_equal_p (low, high, 0))
4210     return fold_build2_loc (loc, EQ_EXPR, type, exp,
4211                         fold_convert_loc (loc, etype, low));
4212
4213   if (integer_zerop (low))
4214     {
4215       if (! TYPE_UNSIGNED (etype))
4216         {
4217           etype = unsigned_type_for (etype);
4218           high = fold_convert_loc (loc, etype, high);
4219           exp = fold_convert_loc (loc, etype, exp);
4220         }
4221       return build_range_check (loc, type, exp, 1, 0, high);
4222     }
4223
4224   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4225   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4226     {
4227       unsigned HOST_WIDE_INT lo;
4228       HOST_WIDE_INT hi;
4229       int prec;
4230
4231       prec = TYPE_PRECISION (etype);
4232       if (prec <= HOST_BITS_PER_WIDE_INT)
4233         {
4234           hi = 0;
4235           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4236         }
4237       else
4238         {
4239           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4240           lo = (unsigned HOST_WIDE_INT) -1;
4241         }
4242
4243       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4244         {
4245           if (TYPE_UNSIGNED (etype))
4246             {
4247               tree signed_etype = signed_type_for (etype);
4248               if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4249                 etype
4250                   = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4251               else
4252                 etype = signed_etype;
4253               exp = fold_convert_loc (loc, etype, exp);
4254             }
4255           return fold_build2_loc (loc, GT_EXPR, type, exp,
4256                               build_int_cst (etype, 0));
4257         }
4258     }
4259
4260   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4261      This requires wrap-around arithmetics for the type of the expression.
4262      First make sure that arithmetics in this type is valid, then make sure
4263      that it wraps around.  */
4264   if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4265     etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4266                                             TYPE_UNSIGNED (etype));
4267
4268   if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4269     {
4270       tree utype, minv, maxv;
4271
4272       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4273          for the type in question, as we rely on this here.  */
4274       utype = unsigned_type_for (etype);
4275       maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4276       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4277                           integer_one_node, 1);
4278       minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4279
4280       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4281                                       minv, 1, maxv, 1)))
4282         etype = utype;
4283       else
4284         return 0;
4285     }
4286
4287   high = fold_convert_loc (loc, etype, high);
4288   low = fold_convert_loc (loc, etype, low);
4289   exp = fold_convert_loc (loc, etype, exp);
4290
4291   value = const_binop (MINUS_EXPR, high, low);
4292
4293
4294   if (POINTER_TYPE_P (etype))
4295     {
4296       if (value != 0 && !TREE_OVERFLOW (value))
4297         {
4298           low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4299           return build_range_check (loc, type,
4300                                     fold_build_pointer_plus_loc (loc, exp, low),
4301                                     1, build_int_cst (etype, 0), value);
4302         }
4303       return 0;
4304     }
4305
4306   if (value != 0 && !TREE_OVERFLOW (value))
4307     return build_range_check (loc, type,
4308                               fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4309                               1, build_int_cst (etype, 0), value);
4310
4311   return 0;
4312 }
4313 \f
4314 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4315
4316 static tree
4317 range_predecessor (tree val)
4318 {
4319   tree type = TREE_TYPE (val);
4320
4321   if (INTEGRAL_TYPE_P (type)
4322       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4323     return 0;
4324   else
4325     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4326 }
4327
4328 /* Return the successor of VAL in its type, handling the infinite case.  */
4329
4330 static tree
4331 range_successor (tree val)
4332 {
4333   tree type = TREE_TYPE (val);
4334
4335   if (INTEGRAL_TYPE_P (type)
4336       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4337     return 0;
4338   else
4339     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4340 }
4341
4342 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4343    can, 0 if we can't.  Set the output range into the specified parameters.  */
4344
4345 bool
4346 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4347               tree high0, int in1_p, tree low1, tree high1)
4348 {
4349   int no_overlap;
4350   int subset;
4351   int temp;
4352   tree tem;
4353   int in_p;
4354   tree low, high;
4355   int lowequal = ((low0 == 0 && low1 == 0)
4356                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4357                                                 low0, 0, low1, 0)));
4358   int highequal = ((high0 == 0 && high1 == 0)
4359                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4360                                                  high0, 1, high1, 1)));
4361
4362   /* Make range 0 be the range that starts first, or ends last if they
4363      start at the same value.  Swap them if it isn't.  */
4364   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4365                                  low0, 0, low1, 0))
4366       || (lowequal
4367           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4368                                         high1, 1, high0, 1))))
4369     {
4370       temp = in0_p, in0_p = in1_p, in1_p = temp;
4371       tem = low0, low0 = low1, low1 = tem;
4372       tem = high0, high0 = high1, high1 = tem;
4373     }
4374
4375   /* Now flag two cases, whether the ranges are disjoint or whether the
4376      second range is totally subsumed in the first.  Note that the tests
4377      below are simplified by the ones above.  */
4378   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4379                                           high0, 1, low1, 0));
4380   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4381                                       high1, 1, high0, 1));
4382
4383   /* We now have four cases, depending on whether we are including or
4384      excluding the two ranges.  */
4385   if (in0_p && in1_p)
4386     {
4387       /* If they don't overlap, the result is false.  If the second range
4388          is a subset it is the result.  Otherwise, the range is from the start
4389          of the second to the end of the first.  */
4390       if (no_overlap)
4391         in_p = 0, low = high = 0;
4392       else if (subset)
4393         in_p = 1, low = low1, high = high1;
4394       else
4395         in_p = 1, low = low1, high = high0;
4396     }
4397
4398   else if (in0_p && ! in1_p)
4399     {
4400       /* If they don't overlap, the result is the first range.  If they are
4401          equal, the result is false.  If the second range is a subset of the
4402          first, and the ranges begin at the same place, we go from just after
4403          the end of the second range to the end of the first.  If the second
4404          range is not a subset of the first, or if it is a subset and both
4405          ranges end at the same place, the range starts at the start of the
4406          first range and ends just before the second range.
4407          Otherwise, we can't describe this as a single range.  */
4408       if (no_overlap)
4409         in_p = 1, low = low0, high = high0;
4410       else if (lowequal && highequal)
4411         in_p = 0, low = high = 0;
4412       else if (subset && lowequal)
4413         {
4414           low = range_successor (high1);
4415           high = high0;
4416           in_p = 1;
4417           if (low == 0)
4418             {
4419               /* We are in the weird situation where high0 > high1 but
4420                  high1 has no successor.  Punt.  */
4421               return 0;
4422             }
4423         }
4424       else if (! subset || highequal)
4425         {
4426           low = low0;
4427           high = range_predecessor (low1);
4428           in_p = 1;
4429           if (high == 0)
4430             {
4431               /* low0 < low1 but low1 has no predecessor.  Punt.  */
4432               return 0;
4433             }
4434         }
4435       else
4436         return 0;
4437     }
4438
4439   else if (! in0_p && in1_p)
4440     {
4441       /* If they don't overlap, the result is the second range.  If the second
4442          is a subset of the first, the result is false.  Otherwise,
4443          the range starts just after the first range and ends at the
4444          end of the second.  */
4445       if (no_overlap)
4446         in_p = 1, low = low1, high = high1;
4447       else if (subset || highequal)
4448         in_p = 0, low = high = 0;
4449       else
4450         {
4451           low = range_successor (high0);
4452           high = high1;
4453           in_p = 1;
4454           if (low == 0)
4455             {
4456               /* high1 > high0 but high0 has no successor.  Punt.  */
4457               return 0;
4458             }
4459         }
4460     }
4461
4462   else
4463     {
4464       /* The case where we are excluding both ranges.  Here the complex case
4465          is if they don't overlap.  In that case, the only time we have a
4466          range is if they are adjacent.  If the second is a subset of the
4467          first, the result is the first.  Otherwise, the range to exclude
4468          starts at the beginning of the first range and ends at the end of the
4469          second.  */
4470       if (no_overlap)
4471         {
4472           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4473                                          range_successor (high0),
4474                                          1, low1, 0)))
4475             in_p = 0, low = low0, high = high1;
4476           else
4477             {
4478               /* Canonicalize - [min, x] into - [-, x].  */
4479               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4480                 switch (TREE_CODE (TREE_TYPE (low0)))
4481                   {
4482                   case ENUMERAL_TYPE:
4483                     if (TYPE_PRECISION (TREE_TYPE (low0))
4484                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4485                       break;
4486                     /* FALLTHROUGH */
4487                   case INTEGER_TYPE:
4488                     if (tree_int_cst_equal (low0,
4489                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4490                       low0 = 0;
4491                     break;
4492                   case POINTER_TYPE:
4493                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4494                         && integer_zerop (low0))
4495                       low0 = 0;
4496                     break;
4497                   default:
4498                     break;
4499                   }
4500
4501               /* Canonicalize - [x, max] into - [x, -].  */
4502               if (high1 && TREE_CODE (high1) == INTEGER_CST)
4503                 switch (TREE_CODE (TREE_TYPE (high1)))
4504                   {
4505                   case ENUMERAL_TYPE:
4506                     if (TYPE_PRECISION (TREE_TYPE (high1))
4507                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4508                       break;
4509                     /* FALLTHROUGH */
4510                   case INTEGER_TYPE:
4511                     if (tree_int_cst_equal (high1,
4512                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
4513                       high1 = 0;
4514                     break;
4515                   case POINTER_TYPE:
4516                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
4517                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4518                                                        high1, 1,
4519                                                        integer_one_node, 1)))
4520                       high1 = 0;
4521                     break;
4522                   default:
4523                     break;
4524                   }
4525
4526               /* The ranges might be also adjacent between the maximum and
4527                  minimum values of the given type.  For
4528                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4529                  return + [x + 1, y - 1].  */
4530               if (low0 == 0 && high1 == 0)
4531                 {
4532                   low = range_successor (high0);
4533                   high = range_predecessor (low1);
4534                   if (low == 0 || high == 0)
4535                     return 0;
4536
4537                   in_p = 1;
4538                 }
4539               else
4540                 return 0;
4541             }
4542         }
4543       else if (subset)
4544         in_p = 0, low = low0, high = high0;
4545       else
4546         in_p = 0, low = low0, high = high1;
4547     }
4548
4549   *pin_p = in_p, *plow = low, *phigh = high;
4550   return 1;
4551 }
4552 \f
4553
4554 /* Subroutine of fold, looking inside expressions of the form
4555    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4556    of the COND_EXPR.  This function is being used also to optimize
4557    A op B ? C : A, by reversing the comparison first.
4558
4559    Return a folded expression whose code is not a COND_EXPR
4560    anymore, or NULL_TREE if no folding opportunity is found.  */
4561
4562 static tree
4563 fold_cond_expr_with_comparison (location_t loc, tree type,
4564                                 tree arg0, tree arg1, tree arg2)
4565 {
4566   enum tree_code comp_code = TREE_CODE (arg0);
4567   tree arg00 = TREE_OPERAND (arg0, 0);
4568   tree arg01 = TREE_OPERAND (arg0, 1);
4569   tree arg1_type = TREE_TYPE (arg1);
4570   tree tem;
4571
4572   STRIP_NOPS (arg1);
4573   STRIP_NOPS (arg2);
4574
4575   /* If we have A op 0 ? A : -A, consider applying the following
4576      transformations:
4577
4578      A == 0? A : -A    same as -A
4579      A != 0? A : -A    same as A
4580      A >= 0? A : -A    same as abs (A)
4581      A > 0?  A : -A    same as abs (A)
4582      A <= 0? A : -A    same as -abs (A)
4583      A < 0?  A : -A    same as -abs (A)
4584
4585      None of these transformations work for modes with signed
4586      zeros.  If A is +/-0, the first two transformations will
4587      change the sign of the result (from +0 to -0, or vice
4588      versa).  The last four will fix the sign of the result,
4589      even though the original expressions could be positive or
4590      negative, depending on the sign of A.
4591
4592      Note that all these transformations are correct if A is
4593      NaN, since the two alternatives (A and -A) are also NaNs.  */
4594   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4595       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4596           ? real_zerop (arg01)
4597           : integer_zerop (arg01))
4598       && ((TREE_CODE (arg2) == NEGATE_EXPR
4599            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4600              /* In the case that A is of the form X-Y, '-A' (arg2) may
4601                 have already been folded to Y-X, check for that. */
4602           || (TREE_CODE (arg1) == MINUS_EXPR
4603               && TREE_CODE (arg2) == MINUS_EXPR
4604               && operand_equal_p (TREE_OPERAND (arg1, 0),
4605                                   TREE_OPERAND (arg2, 1), 0)
4606               && operand_equal_p (TREE_OPERAND (arg1, 1),
4607                                   TREE_OPERAND (arg2, 0), 0))))
4608     switch (comp_code)
4609       {
4610       case EQ_EXPR:
4611       case UNEQ_EXPR:
4612         tem = fold_convert_loc (loc, arg1_type, arg1);
4613         return pedantic_non_lvalue_loc (loc,
4614                                     fold_convert_loc (loc, type,
4615                                                   negate_expr (tem)));
4616       case NE_EXPR:
4617       case LTGT_EXPR:
4618         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4619       case UNGE_EXPR:
4620       case UNGT_EXPR:
4621         if (flag_trapping_math)
4622           break;
4623         /* Fall through.  */
4624       case GE_EXPR:
4625       case GT_EXPR:
4626         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4627           arg1 = fold_convert_loc (loc, signed_type_for
4628                                (TREE_TYPE (arg1)), arg1);
4629         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4630         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4631       case UNLE_EXPR:
4632       case UNLT_EXPR:
4633         if (flag_trapping_math)
4634           break;
4635       case LE_EXPR:
4636       case LT_EXPR:
4637         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4638           arg1 = fold_convert_loc (loc, signed_type_for
4639                                (TREE_TYPE (arg1)), arg1);
4640         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4641         return negate_expr (fold_convert_loc (loc, type, tem));
4642       default:
4643         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4644         break;
4645       }
4646
4647   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
4648      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
4649      both transformations are correct when A is NaN: A != 0
4650      is then true, and A == 0 is false.  */
4651
4652   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4653       && integer_zerop (arg01) && integer_zerop (arg2))
4654     {
4655       if (comp_code == NE_EXPR)
4656         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4657       else if (comp_code == EQ_EXPR)
4658         return build_int_cst (type, 0);
4659     }
4660
4661   /* Try some transformations of A op B ? A : B.
4662
4663      A == B? A : B    same as B
4664      A != B? A : B    same as A
4665      A >= B? A : B    same as max (A, B)
4666      A > B?  A : B    same as max (B, A)
4667      A <= B? A : B    same as min (A, B)
4668      A < B?  A : B    same as min (B, A)
4669
4670      As above, these transformations don't work in the presence
4671      of signed zeros.  For example, if A and B are zeros of
4672      opposite sign, the first two transformations will change
4673      the sign of the result.  In the last four, the original
4674      expressions give different results for (A=+0, B=-0) and
4675      (A=-0, B=+0), but the transformed expressions do not.
4676
4677      The first two transformations are correct if either A or B
4678      is a NaN.  In the first transformation, the condition will
4679      be false, and B will indeed be chosen.  In the case of the
4680      second transformation, the condition A != B will be true,
4681      and A will be chosen.
4682
4683      The conversions to max() and min() are not correct if B is
4684      a number and A is not.  The conditions in the original
4685      expressions will be false, so all four give B.  The min()
4686      and max() versions would give a NaN instead.  */
4687   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4688       && operand_equal_for_comparison_p (arg01, arg2, arg00)
4689       /* Avoid these transformations if the COND_EXPR may be used
4690          as an lvalue in the C++ front-end.  PR c++/19199.  */
4691       && (in_gimple_form
4692           || (strcmp (lang_hooks.name, "GNU C++") != 0
4693               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4694           || ! maybe_lvalue_p (arg1)
4695           || ! maybe_lvalue_p (arg2)))
4696     {
4697       tree comp_op0 = arg00;
4698       tree comp_op1 = arg01;
4699       tree comp_type = TREE_TYPE (comp_op0);
4700
4701       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
4702       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4703         {
4704           comp_type = type;
4705           comp_op0 = arg1;
4706           comp_op1 = arg2;
4707         }
4708
4709       switch (comp_code)
4710         {
4711         case EQ_EXPR:
4712           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
4713         case NE_EXPR:
4714           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4715         case LE_EXPR:
4716         case LT_EXPR:
4717         case UNLE_EXPR:
4718         case UNLT_EXPR:
4719           /* In C++ a ?: expression can be an lvalue, so put the
4720              operand which will be used if they are equal first
4721              so that we can convert this back to the
4722              corresponding COND_EXPR.  */
4723           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4724             {
4725               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4726               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4727               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4728                     ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
4729                     : fold_build2_loc (loc, MIN_EXPR, comp_type,
4730                                    comp_op1, comp_op0);
4731               return pedantic_non_lvalue_loc (loc,
4732                                           fold_convert_loc (loc, type, tem));
4733             }
4734           break;
4735         case GE_EXPR:
4736         case GT_EXPR:
4737         case UNGE_EXPR:
4738         case UNGT_EXPR:
4739           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4740             {
4741               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4742               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4743               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4744                     ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
4745                     : fold_build2_loc (loc, MAX_EXPR, comp_type,
4746                                    comp_op1, comp_op0);
4747               return pedantic_non_lvalue_loc (loc,
4748                                           fold_convert_loc (loc, type, tem));
4749             }
4750           break;
4751         case UNEQ_EXPR:
4752           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4753             return pedantic_non_lvalue_loc (loc,
4754                                         fold_convert_loc (loc, type, arg2));
4755           break;
4756         case LTGT_EXPR:
4757           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4758             return pedantic_non_lvalue_loc (loc,
4759                                         fold_convert_loc (loc, type, arg1));
4760           break;
4761         default:
4762           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4763           break;
4764         }
4765     }
4766
4767   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4768      we might still be able to simplify this.  For example,
4769      if C1 is one less or one more than C2, this might have started
4770      out as a MIN or MAX and been transformed by this function.
4771      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
4772
4773   if (INTEGRAL_TYPE_P (type)
4774       && TREE_CODE (arg01) == INTEGER_CST
4775       && TREE_CODE (arg2) == INTEGER_CST)
4776     switch (comp_code)
4777       {
4778       case EQ_EXPR:
4779         if (TREE_CODE (arg1) == INTEGER_CST)
4780           break;
4781         /* We can replace A with C1 in this case.  */
4782         arg1 = fold_convert_loc (loc, type, arg01);
4783         return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
4784
4785       case LT_EXPR:
4786         /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
4787            MIN_EXPR, to preserve the signedness of the comparison.  */
4788         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4789                                OEP_ONLY_CONST)
4790             && operand_equal_p (arg01,
4791                                 const_binop (PLUS_EXPR, arg2,
4792                                              build_int_cst (type, 1)),
4793                                 OEP_ONLY_CONST))
4794           {
4795             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4796                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4797                                                      arg2));
4798             return pedantic_non_lvalue_loc (loc,
4799                                             fold_convert_loc (loc, type, tem));
4800           }
4801         break;
4802
4803       case LE_EXPR:
4804         /* If C1 is C2 - 1, this is min(A, C2), with the same care
4805            as above.  */
4806         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4807                                OEP_ONLY_CONST)
4808             && operand_equal_p (arg01,
4809                                 const_binop (MINUS_EXPR, arg2,
4810                                              build_int_cst (type, 1)),
4811                                 OEP_ONLY_CONST))
4812           {
4813             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4814                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4815                                                      arg2));
4816             return pedantic_non_lvalue_loc (loc,
4817                                             fold_convert_loc (loc, type, tem));
4818           }
4819         break;
4820
4821       case GT_EXPR:
4822         /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
4823            MAX_EXPR, to preserve the signedness of the comparison.  */
4824         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4825                                OEP_ONLY_CONST)
4826             && operand_equal_p (arg01,
4827                                 const_binop (MINUS_EXPR, arg2,
4828                                              build_int_cst (type, 1)),
4829                                 OEP_ONLY_CONST))
4830           {
4831             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4832                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4833                                                      arg2));
4834             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4835           }
4836         break;
4837
4838       case GE_EXPR:
4839         /* If C1 is C2 + 1, this is max(A, C2), with the same care as above.  */
4840         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4841                                OEP_ONLY_CONST)
4842             && operand_equal_p (arg01,
4843                                 const_binop (PLUS_EXPR, arg2,
4844                                              build_int_cst (type, 1)),
4845                                 OEP_ONLY_CONST))
4846           {
4847             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4848                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4849                                                      arg2));
4850             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4851           }
4852         break;
4853       case NE_EXPR:
4854         break;
4855       default:
4856         gcc_unreachable ();
4857       }
4858
4859   return NULL_TREE;
4860 }
4861
4862
4863 \f
4864 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4865 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
4866   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
4867                 false) >= 2)
4868 #endif
4869
4870 /* EXP is some logical combination of boolean tests.  See if we can
4871    merge it into some range test.  Return the new tree if so.  */
4872
4873 static tree
4874 fold_range_test (location_t loc, enum tree_code code, tree type,
4875                  tree op0, tree op1)
4876 {
4877   int or_op = (code == TRUTH_ORIF_EXPR
4878                || code == TRUTH_OR_EXPR);
4879   int in0_p, in1_p, in_p;
4880   tree low0, low1, low, high0, high1, high;
4881   bool strict_overflow_p = false;
4882   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
4883   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
4884   tree tem;
4885   const char * const warnmsg = G_("assuming signed overflow does not occur "
4886                                   "when simplifying range test");
4887
4888   /* If this is an OR operation, invert both sides; we will invert
4889      again at the end.  */
4890   if (or_op)
4891     in0_p = ! in0_p, in1_p = ! in1_p;
4892
4893   /* If both expressions are the same, if we can merge the ranges, and we
4894      can build the range test, return it or it inverted.  If one of the
4895      ranges is always true or always false, consider it to be the same
4896      expression as the other.  */
4897   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4898       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4899                        in1_p, low1, high1)
4900       && 0 != (tem = (build_range_check (loc, type,
4901                                          lhs != 0 ? lhs
4902                                          : rhs != 0 ? rhs : integer_zero_node,
4903                                          in_p, low, high))))
4904     {
4905       if (strict_overflow_p)
4906         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
4907       return or_op ? invert_truthvalue_loc (loc, tem) : tem;
4908     }
4909
4910   /* On machines where the branch cost is expensive, if this is a
4911      short-circuited branch and the underlying object on both sides
4912      is the same, make a non-short-circuit operation.  */
4913   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4914            && lhs != 0 && rhs != 0
4915            && (code == TRUTH_ANDIF_EXPR
4916                || code == TRUTH_ORIF_EXPR)
4917            && operand_equal_p (lhs, rhs, 0))
4918     {
4919       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
4920          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4921          which cases we can't do this.  */
4922       if (simple_operand_p (lhs))
4923         return build2_loc (loc, code == TRUTH_ANDIF_EXPR
4924                            ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4925                            type, op0, op1);
4926
4927       else if (!lang_hooks.decls.global_bindings_p ()
4928                && !CONTAINS_PLACEHOLDER_P (lhs))
4929         {
4930           tree common = save_expr (lhs);
4931
4932           if (0 != (lhs = build_range_check (loc, type, common,
4933                                              or_op ? ! in0_p : in0_p,
4934                                              low0, high0))
4935               && (0 != (rhs = build_range_check (loc, type, common,
4936                                                  or_op ? ! in1_p : in1_p,
4937                                                  low1, high1))))
4938             {
4939               if (strict_overflow_p)
4940                 fold_overflow_warning (warnmsg,
4941                                        WARN_STRICT_OVERFLOW_COMPARISON);
4942               return build2_loc (loc, code == TRUTH_ANDIF_EXPR
4943                                  ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4944                                  type, lhs, rhs);
4945             }
4946         }
4947     }
4948
4949   return 0;
4950 }
4951 \f
4952 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
4953    bit value.  Arrange things so the extra bits will be set to zero if and
4954    only if C is signed-extended to its full width.  If MASK is nonzero,
4955    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
4956
4957 static tree
4958 unextend (tree c, int p, int unsignedp, tree mask)
4959 {
4960   tree type = TREE_TYPE (c);
4961   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4962   tree temp;
4963
4964   if (p == modesize || unsignedp)
4965     return c;
4966
4967   /* We work by getting just the sign bit into the low-order bit, then
4968      into the high-order bit, then sign-extend.  We then XOR that value
4969      with C.  */
4970   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1));
4971   temp = const_binop (BIT_AND_EXPR, temp, size_int (1));
4972
4973   /* We must use a signed type in order to get an arithmetic right shift.
4974      However, we must also avoid introducing accidental overflows, so that
4975      a subsequent call to integer_zerop will work.  Hence we must
4976      do the type conversion here.  At this point, the constant is either
4977      zero or one, and the conversion to a signed type can never overflow.
4978      We could get an overflow if this conversion is done anywhere else.  */
4979   if (TYPE_UNSIGNED (type))
4980     temp = fold_convert (signed_type_for (type), temp);
4981
4982   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
4983   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
4984   if (mask != 0)
4985     temp = const_binop (BIT_AND_EXPR, temp,
4986                         fold_convert (TREE_TYPE (c), mask));
4987   /* If necessary, convert the type back to match the type of C.  */
4988   if (TYPE_UNSIGNED (type))
4989     temp = fold_convert (type, temp);
4990
4991   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
4992 }
4993 \f
4994 /* For an expression that has the form
4995      (A && B) || ~B
4996    or
4997      (A || B) && ~B,
4998    we can drop one of the inner expressions and simplify to
4999      A || ~B
5000    or
5001      A && ~B
5002    LOC is the location of the resulting expression.  OP is the inner 
5003    logical operation; the left-hand side in the examples above, while CMPOP
5004    is the right-hand side.  RHS_ONLY is used to prevent us from accidentally
5005    removing a condition that guards another, as in
5006      (A != NULL && A->...) || A == NULL
5007    which we must not transform.  If RHS_ONLY is true, only eliminate the
5008    right-most operand of the inner logical operation.  */
5009
5010 static tree
5011 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5012                                  bool rhs_only)
5013 {
5014   tree type = TREE_TYPE (cmpop);
5015   enum tree_code code = TREE_CODE (cmpop);
5016   enum tree_code truthop_code = TREE_CODE (op);
5017   tree lhs = TREE_OPERAND (op, 0);
5018   tree rhs = TREE_OPERAND (op, 1);
5019   tree orig_lhs = lhs, orig_rhs = rhs;
5020   enum tree_code rhs_code = TREE_CODE (rhs);
5021   enum tree_code lhs_code = TREE_CODE (lhs);
5022   enum tree_code inv_code;
5023
5024   if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5025     return NULL_TREE;
5026
5027   if (TREE_CODE_CLASS (code) != tcc_comparison)
5028     return NULL_TREE;
5029
5030   if (rhs_code == truthop_code)
5031     {
5032       tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5033       if (newrhs != NULL_TREE)
5034         {
5035           rhs = newrhs;
5036           rhs_code = TREE_CODE (rhs);
5037         }
5038     }
5039   if (lhs_code == truthop_code && !rhs_only)
5040     {
5041       tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5042       if (newlhs != NULL_TREE)
5043         {
5044           lhs = newlhs;
5045           lhs_code = TREE_CODE (lhs);
5046         }
5047     }
5048
5049   inv_code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
5050   if (inv_code == rhs_code
5051       && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5052       && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5053     return lhs;
5054   if (!rhs_only && inv_code == lhs_code
5055       && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5056       && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5057     return rhs;
5058   if (rhs != orig_rhs || lhs != orig_lhs)
5059     return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5060                             lhs, rhs);
5061   return NULL_TREE;
5062 }
5063
5064 /* Find ways of folding logical expressions of LHS and RHS:
5065    Try to merge two comparisons to the same innermost item.
5066    Look for range tests like "ch >= '0' && ch <= '9'".
5067    Look for combinations of simple terms on machines with expensive branches
5068    and evaluate the RHS unconditionally.
5069
5070    For example, if we have p->a == 2 && p->b == 4 and we can make an
5071    object large enough to span both A and B, we can do this with a comparison
5072    against the object ANDed with the a mask.
5073
5074    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5075    operations to do this with one comparison.
5076
5077    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5078    function and the one above.
5079
5080    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5081    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5082
5083    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5084    two operands.
5085
5086    We return the simplified tree or 0 if no optimization is possible.  */
5087
5088 static tree
5089 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5090                     tree lhs, tree rhs)
5091 {
5092   /* If this is the "or" of two comparisons, we can do something if
5093      the comparisons are NE_EXPR.  If this is the "and", we can do something
5094      if the comparisons are EQ_EXPR.  I.e.,
5095         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5096
5097      WANTED_CODE is this operation code.  For single bit fields, we can
5098      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5099      comparison for one-bit fields.  */
5100
5101   enum tree_code wanted_code;
5102   enum tree_code lcode, rcode;
5103   tree ll_arg, lr_arg, rl_arg, rr_arg;
5104   tree ll_inner, lr_inner, rl_inner, rr_inner;
5105   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5106   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5107   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5108   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5109   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5110   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5111   enum machine_mode lnmode, rnmode;
5112   tree ll_mask, lr_mask, rl_mask, rr_mask;
5113   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5114   tree l_const, r_const;
5115   tree lntype, rntype, result;
5116   HOST_WIDE_INT first_bit, end_bit;
5117   int volatilep;
5118
5119   /* Start by getting the comparison codes.  Fail if anything is volatile.
5120      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5121      it were surrounded with a NE_EXPR.  */
5122
5123   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5124     return 0;
5125
5126   lcode = TREE_CODE (lhs);
5127   rcode = TREE_CODE (rhs);
5128
5129   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5130     {
5131       lhs = build2 (NE_EXPR, truth_type, lhs,
5132                     build_int_cst (TREE_TYPE (lhs), 0));
5133       lcode = NE_EXPR;
5134     }
5135
5136   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5137     {
5138       rhs = build2 (NE_EXPR, truth_type, rhs,
5139                     build_int_cst (TREE_TYPE (rhs), 0));
5140       rcode = NE_EXPR;
5141     }
5142
5143   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5144       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5145     return 0;
5146
5147   ll_arg = TREE_OPERAND (lhs, 0);
5148   lr_arg = TREE_OPERAND (lhs, 1);
5149   rl_arg = TREE_OPERAND (rhs, 0);
5150   rr_arg = TREE_OPERAND (rhs, 1);
5151
5152   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5153   if (simple_operand_p (ll_arg)
5154       && simple_operand_p (lr_arg))
5155     {
5156       if (operand_equal_p (ll_arg, rl_arg, 0)
5157           && operand_equal_p (lr_arg, rr_arg, 0))
5158         {
5159           result = combine_comparisons (loc, code, lcode, rcode,
5160                                         truth_type, ll_arg, lr_arg);
5161           if (result)
5162             return result;
5163         }
5164       else if (operand_equal_p (ll_arg, rr_arg, 0)
5165                && operand_equal_p (lr_arg, rl_arg, 0))
5166         {
5167           result = combine_comparisons (loc, code, lcode,
5168                                         swap_tree_comparison (rcode),
5169                                         truth_type, ll_arg, lr_arg);
5170           if (result)
5171             return result;
5172         }
5173     }
5174
5175   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5176           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5177
5178   /* If the RHS can be evaluated unconditionally and its operands are
5179      simple, it wins to evaluate the RHS unconditionally on machines
5180      with expensive branches.  In this case, this isn't a comparison
5181      that can be merged.  */
5182
5183   if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5184                    false) >= 2
5185       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5186       && simple_operand_p (rl_arg)
5187       && simple_operand_p (rr_arg))
5188     {
5189       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5190       if (code == TRUTH_OR_EXPR
5191           && lcode == NE_EXPR && integer_zerop (lr_arg)
5192           && rcode == NE_EXPR && integer_zerop (rr_arg)
5193           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5194           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5195         return build2_loc (loc, NE_EXPR, truth_type,
5196                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5197                                    ll_arg, rl_arg),
5198                            build_int_cst (TREE_TYPE (ll_arg), 0));
5199
5200       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5201       if (code == TRUTH_AND_EXPR
5202           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5203           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5204           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5205           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5206         return build2_loc (loc, EQ_EXPR, truth_type,
5207                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5208                                    ll_arg, rl_arg),
5209                            build_int_cst (TREE_TYPE (ll_arg), 0));
5210     }
5211
5212   /* See if the comparisons can be merged.  Then get all the parameters for
5213      each side.  */
5214
5215   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5216       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5217     return 0;
5218
5219   volatilep = 0;
5220   ll_inner = decode_field_reference (loc, ll_arg,
5221                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5222                                      &ll_unsignedp, &volatilep, &ll_mask,
5223                                      &ll_and_mask);
5224   lr_inner = decode_field_reference (loc, lr_arg,
5225                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5226                                      &lr_unsignedp, &volatilep, &lr_mask,
5227                                      &lr_and_mask);
5228   rl_inner = decode_field_reference (loc, rl_arg,
5229                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5230                                      &rl_unsignedp, &volatilep, &rl_mask,
5231                                      &rl_and_mask);
5232   rr_inner = decode_field_reference (loc, rr_arg,
5233                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5234                                      &rr_unsignedp, &volatilep, &rr_mask,
5235                                      &rr_and_mask);
5236
5237   /* It must be true that the inner operation on the lhs of each
5238      comparison must be the same if we are to be able to do anything.
5239      Then see if we have constants.  If not, the same must be true for
5240      the rhs's.  */
5241   if (volatilep || ll_inner == 0 || rl_inner == 0
5242       || ! operand_equal_p (ll_inner, rl_inner, 0))
5243     return 0;
5244
5245   if (TREE_CODE (lr_arg) == INTEGER_CST
5246       && TREE_CODE (rr_arg) == INTEGER_CST)
5247     l_const = lr_arg, r_const = rr_arg;
5248   else if (lr_inner == 0 || rr_inner == 0
5249            || ! operand_equal_p (lr_inner, rr_inner, 0))
5250     return 0;
5251   else
5252     l_const = r_const = 0;
5253
5254   /* If either comparison code is not correct for our logical operation,
5255      fail.  However, we can convert a one-bit comparison against zero into
5256      the opposite comparison against that bit being set in the field.  */
5257
5258   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5259   if (lcode != wanted_code)
5260     {
5261       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5262         {
5263           /* Make the left operand unsigned, since we are only interested
5264              in the value of one bit.  Otherwise we are doing the wrong
5265              thing below.  */
5266           ll_unsignedp = 1;
5267           l_const = ll_mask;
5268         }
5269       else
5270         return 0;
5271     }
5272
5273   /* This is analogous to the code for l_const above.  */
5274   if (rcode != wanted_code)
5275     {
5276       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5277         {
5278           rl_unsignedp = 1;
5279           r_const = rl_mask;
5280         }
5281       else
5282         return 0;
5283     }
5284
5285   /* See if we can find a mode that contains both fields being compared on
5286      the left.  If we can't, fail.  Otherwise, update all constants and masks
5287      to be relative to a field of that size.  */
5288   first_bit = MIN (ll_bitpos, rl_bitpos);
5289   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5290   lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5291                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5292                           volatilep);
5293   if (lnmode == VOIDmode)
5294     return 0;
5295
5296   lnbitsize = GET_MODE_BITSIZE (lnmode);
5297   lnbitpos = first_bit & ~ (lnbitsize - 1);
5298   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5299   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5300
5301   if (BYTES_BIG_ENDIAN)
5302     {
5303       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5304       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5305     }
5306
5307   ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5308                          size_int (xll_bitpos));
5309   rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5310                          size_int (xrl_bitpos));
5311
5312   if (l_const)
5313     {
5314       l_const = fold_convert_loc (loc, lntype, l_const);
5315       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5316       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5317       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5318                                         fold_build1_loc (loc, BIT_NOT_EXPR,
5319                                                      lntype, ll_mask))))
5320         {
5321           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5322
5323           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5324         }
5325     }
5326   if (r_const)
5327     {
5328       r_const = fold_convert_loc (loc, lntype, r_const);
5329       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5330       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5331       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5332                                         fold_build1_loc (loc, BIT_NOT_EXPR,
5333                                                      lntype, rl_mask))))
5334         {
5335           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5336
5337           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5338         }
5339     }
5340
5341   /* If the right sides are not constant, do the same for it.  Also,
5342      disallow this optimization if a size or signedness mismatch occurs
5343      between the left and right sides.  */
5344   if (l_const == 0)
5345     {
5346       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5347           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5348           /* Make sure the two fields on the right
5349              correspond to the left without being swapped.  */
5350           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5351         return 0;
5352
5353       first_bit = MIN (lr_bitpos, rr_bitpos);
5354       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5355       rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5356                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5357                               volatilep);
5358       if (rnmode == VOIDmode)
5359         return 0;
5360
5361       rnbitsize = GET_MODE_BITSIZE (rnmode);
5362       rnbitpos = first_bit & ~ (rnbitsize - 1);
5363       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5364       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5365
5366       if (BYTES_BIG_ENDIAN)
5367         {
5368           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5369           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5370         }
5371
5372       lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5373                                                             rntype, lr_mask),
5374                              size_int (xlr_bitpos));
5375       rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5376                                                             rntype, rr_mask),
5377                              size_int (xrr_bitpos));
5378
5379       /* Make a mask that corresponds to both fields being compared.
5380          Do this for both items being compared.  If the operands are the
5381          same size and the bits being compared are in the same position
5382          then we can do this by masking both and comparing the masked
5383          results.  */
5384       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5385       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5386       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5387         {
5388           lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5389                                     ll_unsignedp || rl_unsignedp);
5390           if (! all_ones_mask_p (ll_mask, lnbitsize))
5391             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5392
5393           rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5394                                     lr_unsignedp || rr_unsignedp);
5395           if (! all_ones_mask_p (lr_mask, rnbitsize))
5396             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5397
5398           return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5399         }
5400
5401       /* There is still another way we can do something:  If both pairs of
5402          fields being compared are adjacent, we may be able to make a wider
5403          field containing them both.
5404
5405          Note that we still must mask the lhs/rhs expressions.  Furthermore,
5406          the mask must be shifted to account for the shift done by
5407          make_bit_field_ref.  */
5408       if ((ll_bitsize + ll_bitpos == rl_bitpos
5409            && lr_bitsize + lr_bitpos == rr_bitpos)
5410           || (ll_bitpos == rl_bitpos + rl_bitsize
5411               && lr_bitpos == rr_bitpos + rr_bitsize))
5412         {
5413           tree type;
5414
5415           lhs = make_bit_field_ref (loc, ll_inner, lntype,
5416                                     ll_bitsize + rl_bitsize,
5417                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5418           rhs = make_bit_field_ref (loc, lr_inner, rntype,
5419                                     lr_bitsize + rr_bitsize,
5420                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5421
5422           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5423                                  size_int (MIN (xll_bitpos, xrl_bitpos)));
5424           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5425                                  size_int (MIN (xlr_bitpos, xrr_bitpos)));
5426
5427           /* Convert to the smaller type before masking out unwanted bits.  */
5428           type = lntype;
5429           if (lntype != rntype)
5430             {
5431               if (lnbitsize > rnbitsize)
5432                 {
5433                   lhs = fold_convert_loc (loc, rntype, lhs);
5434                   ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5435                   type = rntype;
5436                 }
5437               else if (lnbitsize < rnbitsize)
5438                 {
5439                   rhs = fold_convert_loc (loc, lntype, rhs);
5440                   lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5441                   type = lntype;
5442                 }
5443             }
5444
5445           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5446             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5447
5448           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5449             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5450
5451           return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5452         }
5453
5454       return 0;
5455     }
5456
5457   /* Handle the case of comparisons with constants.  If there is something in
5458      common between the masks, those bits of the constants must be the same.
5459      If not, the condition is always false.  Test for this to avoid generating
5460      incorrect code below.  */
5461   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5462   if (! integer_zerop (result)
5463       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5464                            const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5465     {
5466       if (wanted_code == NE_EXPR)
5467         {
5468           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5469           return constant_boolean_node (true, truth_type);
5470         }
5471       else
5472         {
5473           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5474           return constant_boolean_node (false, truth_type);
5475         }
5476     }
5477
5478   /* Construct the expression we will return.  First get the component
5479      reference we will make.  Unless the mask is all ones the width of
5480      that field, perform the mask operation.  Then compare with the
5481      merged constant.  */
5482   result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5483                                ll_unsignedp || rl_unsignedp);
5484
5485   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5486   if (! all_ones_mask_p (ll_mask, lnbitsize))
5487     result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5488
5489   return build2_loc (loc, wanted_code, truth_type, result,
5490                      const_binop (BIT_IOR_EXPR, l_const, r_const));
5491 }
5492 \f
5493 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5494    constant.  */
5495
5496 static tree
5497 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5498                             tree op0, tree op1)
5499 {
5500   tree arg0 = op0;
5501   enum tree_code op_code;
5502   tree comp_const;
5503   tree minmax_const;
5504   int consts_equal, consts_lt;
5505   tree inner;
5506
5507   STRIP_SIGN_NOPS (arg0);
5508
5509   op_code = TREE_CODE (arg0);
5510   minmax_const = TREE_OPERAND (arg0, 1);
5511   comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5512   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5513   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5514   inner = TREE_OPERAND (arg0, 0);
5515
5516   /* If something does not permit us to optimize, return the original tree.  */
5517   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5518       || TREE_CODE (comp_const) != INTEGER_CST
5519       || TREE_OVERFLOW (comp_const)
5520       || TREE_CODE (minmax_const) != INTEGER_CST
5521       || TREE_OVERFLOW (minmax_const))
5522     return NULL_TREE;
5523
5524   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5525      and GT_EXPR, doing the rest with recursive calls using logical
5526      simplifications.  */
5527   switch (code)
5528     {
5529     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5530       {
5531         tree tem
5532           = optimize_minmax_comparison (loc,
5533                                         invert_tree_comparison (code, false),
5534                                         type, op0, op1);
5535         if (tem)
5536           return invert_truthvalue_loc (loc, tem);
5537         return NULL_TREE;
5538       }
5539
5540     case GE_EXPR:
5541       return
5542         fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5543                      optimize_minmax_comparison
5544                      (loc, EQ_EXPR, type, arg0, comp_const),
5545                      optimize_minmax_comparison
5546                      (loc, GT_EXPR, type, arg0, comp_const));
5547
5548     case EQ_EXPR:
5549       if (op_code == MAX_EXPR && consts_equal)
5550         /* MAX (X, 0) == 0  ->  X <= 0  */
5551         return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5552
5553       else if (op_code == MAX_EXPR && consts_lt)
5554         /* MAX (X, 0) == 5  ->  X == 5   */
5555         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5556
5557       else if (op_code == MAX_EXPR)
5558         /* MAX (X, 0) == -1  ->  false  */
5559         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5560
5561       else if (consts_equal)
5562         /* MIN (X, 0) == 0  ->  X >= 0  */
5563         return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5564
5565       else if (consts_lt)
5566         /* MIN (X, 0) == 5  ->  false  */
5567         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5568
5569       else
5570         /* MIN (X, 0) == -1  ->  X == -1  */
5571         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5572
5573     case GT_EXPR:
5574       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5575         /* MAX (X, 0) > 0  ->  X > 0
5576            MAX (X, 0) > 5  ->  X > 5  */
5577         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5578
5579       else if (op_code == MAX_EXPR)
5580         /* MAX (X, 0) > -1  ->  true  */
5581         return omit_one_operand_loc (loc, type, integer_one_node, inner);
5582
5583       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5584         /* MIN (X, 0) > 0  ->  false
5585            MIN (X, 0) > 5  ->  false  */
5586         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5587
5588       else
5589         /* MIN (X, 0) > -1  ->  X > -1  */
5590         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5591
5592     default:
5593       return NULL_TREE;
5594     }
5595 }
5596 \f
5597 /* T is an integer expression that is being multiplied, divided, or taken a
5598    modulus (CODE says which and what kind of divide or modulus) by a
5599    constant C.  See if we can eliminate that operation by folding it with
5600    other operations already in T.  WIDE_TYPE, if non-null, is a type that
5601    should be used for the computation if wider than our type.
5602
5603    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5604    (X * 2) + (Y * 4).  We must, however, be assured that either the original
5605    expression would not overflow or that overflow is undefined for the type
5606    in the language in question.
5607
5608    If we return a non-null expression, it is an equivalent form of the
5609    original computation, but need not be in the original type.
5610
5611    We set *STRICT_OVERFLOW_P to true if the return values depends on
5612    signed overflow being undefined.  Otherwise we do not change
5613    *STRICT_OVERFLOW_P.  */
5614
5615 static tree
5616 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5617                 bool *strict_overflow_p)
5618 {
5619   /* To avoid exponential search depth, refuse to allow recursion past
5620      three levels.  Beyond that (1) it's highly unlikely that we'll find
5621      something interesting and (2) we've probably processed it before
5622      when we built the inner expression.  */
5623
5624   static int depth;
5625   tree ret;
5626
5627   if (depth > 3)
5628     return NULL;
5629
5630   depth++;
5631   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5632   depth--;
5633
5634   return ret;
5635 }
5636
5637 static tree
5638 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5639                   bool *strict_overflow_p)
5640 {
5641   tree type = TREE_TYPE (t);
5642   enum tree_code tcode = TREE_CODE (t);
5643   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5644                                    > GET_MODE_SIZE (TYPE_MODE (type)))
5645                 ? wide_type : type);
5646   tree t1, t2;
5647   int same_p = tcode == code;
5648   tree op0 = NULL_TREE, op1 = NULL_TREE;
5649   bool sub_strict_overflow_p;
5650
5651   /* Don't deal with constants of zero here; they confuse the code below.  */
5652   if (integer_zerop (c))
5653     return NULL_TREE;
5654
5655   if (TREE_CODE_CLASS (tcode) == tcc_unary)
5656     op0 = TREE_OPERAND (t, 0);
5657
5658   if (TREE_CODE_CLASS (tcode) == tcc_binary)
5659     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5660
5661   /* Note that we need not handle conditional operations here since fold
5662      already handles those cases.  So just do arithmetic here.  */
5663   switch (tcode)
5664     {
5665     case INTEGER_CST:
5666       /* For a constant, we can always simplify if we are a multiply
5667          or (for divide and modulus) if it is a multiple of our constant.  */
5668       if (code == MULT_EXPR
5669           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c)))
5670         return const_binop (code, fold_convert (ctype, t),
5671                             fold_convert (ctype, c));
5672       break;
5673
5674     CASE_CONVERT: case NON_LVALUE_EXPR:
5675       /* If op0 is an expression ...  */
5676       if ((COMPARISON_CLASS_P (op0)
5677            || UNARY_CLASS_P (op0)
5678            || BINARY_CLASS_P (op0)
5679            || VL_EXP_CLASS_P (op0)
5680            || EXPRESSION_CLASS_P (op0))
5681           /* ... and has wrapping overflow, and its type is smaller
5682              than ctype, then we cannot pass through as widening.  */
5683           && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
5684                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5685                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5686                && (TYPE_PRECISION (ctype)
5687                    > TYPE_PRECISION (TREE_TYPE (op0))))
5688               /* ... or this is a truncation (t is narrower than op0),
5689                  then we cannot pass through this narrowing.  */
5690               || (TYPE_PRECISION (type)
5691                   < TYPE_PRECISION (TREE_TYPE (op0)))
5692               /* ... or signedness changes for division or modulus,
5693                  then we cannot pass through this conversion.  */
5694               || (code != MULT_EXPR
5695                   && (TYPE_UNSIGNED (ctype)
5696                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
5697               /* ... or has undefined overflow while the converted to
5698                  type has not, we cannot do the operation in the inner type
5699                  as that would introduce undefined overflow.  */
5700               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5701                   && !TYPE_OVERFLOW_UNDEFINED (type))))
5702         break;
5703
5704       /* Pass the constant down and see if we can make a simplification.  If
5705          we can, replace this expression with the inner simplification for
5706          possible later conversion to our or some other type.  */
5707       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5708           && TREE_CODE (t2) == INTEGER_CST
5709           && !TREE_OVERFLOW (t2)
5710           && (0 != (t1 = extract_muldiv (op0, t2, code,
5711                                          code == MULT_EXPR
5712                                          ? ctype : NULL_TREE,
5713                                          strict_overflow_p))))
5714         return t1;
5715       break;
5716
5717     case ABS_EXPR:
5718       /* If widening the type changes it from signed to unsigned, then we
5719          must avoid building ABS_EXPR itself as unsigned.  */
5720       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5721         {
5722           tree cstype = (*signed_type_for) (ctype);
5723           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5724               != 0)
5725             {
5726               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5727               return fold_convert (ctype, t1);
5728             }
5729           break;
5730         }
5731       /* If the constant is negative, we cannot simplify this.  */
5732       if (tree_int_cst_sgn (c) == -1)
5733         break;
5734       /* FALLTHROUGH */
5735     case NEGATE_EXPR:
5736       /* For division and modulus, type can't be unsigned, as e.g.
5737          (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
5738          For signed types, even with wrapping overflow, this is fine.  */
5739       if (code != MULT_EXPR && TYPE_UNSIGNED (type))
5740         break;
5741       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5742           != 0)
5743         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5744       break;
5745
5746     case MIN_EXPR:  case MAX_EXPR:
5747       /* If widening the type changes the signedness, then we can't perform
5748          this optimization as that changes the result.  */
5749       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5750         break;
5751
5752       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
5753       sub_strict_overflow_p = false;
5754       if ((t1 = extract_muldiv (op0, c, code, wide_type,
5755                                 &sub_strict_overflow_p)) != 0
5756           && (t2 = extract_muldiv (op1, c, code, wide_type,
5757                                    &sub_strict_overflow_p)) != 0)
5758         {
5759           if (tree_int_cst_sgn (c) < 0)
5760             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5761           if (sub_strict_overflow_p)
5762             *strict_overflow_p = true;
5763           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5764                               fold_convert (ctype, t2));
5765         }
5766       break;
5767
5768     case LSHIFT_EXPR:  case RSHIFT_EXPR:
5769       /* If the second operand is constant, this is a multiplication
5770          or floor division, by a power of two, so we can treat it that
5771          way unless the multiplier or divisor overflows.  Signed
5772          left-shift overflow is implementation-defined rather than
5773          undefined in C90, so do not convert signed left shift into
5774          multiplication.  */
5775       if (TREE_CODE (op1) == INTEGER_CST
5776           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5777           /* const_binop may not detect overflow correctly,
5778              so check for it explicitly here.  */
5779           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5780           && TREE_INT_CST_HIGH (op1) == 0
5781           && 0 != (t1 = fold_convert (ctype,
5782                                       const_binop (LSHIFT_EXPR,
5783                                                    size_one_node,
5784                                                    op1)))
5785           && !TREE_OVERFLOW (t1))
5786         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5787                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
5788                                        ctype,
5789                                        fold_convert (ctype, op0),
5790                                        t1),
5791                                c, code, wide_type, strict_overflow_p);
5792       break;
5793
5794     case PLUS_EXPR:  case MINUS_EXPR:
5795       /* See if we can eliminate the operation on both sides.  If we can, we
5796          can return a new PLUS or MINUS.  If we can't, the only remaining
5797          cases where we can do anything are if the second operand is a
5798          constant.  */
5799       sub_strict_overflow_p = false;
5800       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5801       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5802       if (t1 != 0 && t2 != 0
5803           && (code == MULT_EXPR
5804               /* If not multiplication, we can only do this if both operands
5805                  are divisible by c.  */
5806               || (multiple_of_p (ctype, op0, c)
5807                   && multiple_of_p (ctype, op1, c))))
5808         {
5809           if (sub_strict_overflow_p)
5810             *strict_overflow_p = true;
5811           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5812                               fold_convert (ctype, t2));
5813         }
5814
5815       /* If this was a subtraction, negate OP1 and set it to be an addition.
5816          This simplifies the logic below.  */
5817       if (tcode == MINUS_EXPR)
5818         {
5819           tcode = PLUS_EXPR, op1 = negate_expr (op1);
5820           /* If OP1 was not easily negatable, the constant may be OP0.  */
5821           if (TREE_CODE (op0) == INTEGER_CST)
5822             {
5823               tree tem = op0;
5824               op0 = op1;
5825               op1 = tem;
5826               tem = t1;
5827               t1 = t2;
5828               t2 = tem;
5829             }
5830         }
5831
5832       if (TREE_CODE (op1) != INTEGER_CST)
5833         break;
5834
5835       /* If either OP1 or C are negative, this optimization is not safe for
5836          some of the division and remainder types while for others we need
5837          to change the code.  */
5838       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5839         {
5840           if (code == CEIL_DIV_EXPR)
5841             code = FLOOR_DIV_EXPR;
5842           else if (code == FLOOR_DIV_EXPR)
5843             code = CEIL_DIV_EXPR;
5844           else if (code != MULT_EXPR
5845                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5846             break;
5847         }
5848
5849       /* If it's a multiply or a division/modulus operation of a multiple
5850          of our constant, do the operation and verify it doesn't overflow.  */
5851       if (code == MULT_EXPR
5852           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5853         {
5854           op1 = const_binop (code, fold_convert (ctype, op1),
5855                              fold_convert (ctype, c));
5856           /* We allow the constant to overflow with wrapping semantics.  */
5857           if (op1 == 0
5858               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5859             break;
5860         }
5861       else
5862         break;
5863
5864       /* If we have an unsigned type is not a sizetype, we cannot widen
5865          the operation since it will change the result if the original
5866          computation overflowed.  */
5867       if (TYPE_UNSIGNED (ctype)
5868           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5869           && ctype != type)
5870         break;
5871
5872       /* If we were able to eliminate our operation from the first side,
5873          apply our operation to the second side and reform the PLUS.  */
5874       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5875         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5876
5877       /* The last case is if we are a multiply.  In that case, we can
5878          apply the distributive law to commute the multiply and addition
5879          if the multiplication of the constants doesn't overflow.  */
5880       if (code == MULT_EXPR)
5881         return fold_build2 (tcode, ctype,
5882                             fold_build2 (code, ctype,
5883                                          fold_convert (ctype, op0),
5884                                          fold_convert (ctype, c)),
5885                             op1);
5886
5887       break;
5888
5889     case MULT_EXPR:
5890       /* We have a special case here if we are doing something like
5891          (C * 8) % 4 since we know that's zero.  */
5892       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5893            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5894           /* If the multiplication can overflow we cannot optimize this.
5895              ???  Until we can properly mark individual operations as
5896              not overflowing we need to treat sizetype special here as
5897              stor-layout relies on this opimization to make
5898              DECL_FIELD_BIT_OFFSET always a constant.  */
5899           && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
5900               || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
5901                   && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
5902           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5903           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5904         {
5905           *strict_overflow_p = true;
5906           return omit_one_operand (type, integer_zero_node, op0);
5907         }
5908
5909       /* ... fall through ...  */
5910
5911     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
5912     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
5913       /* If we can extract our operation from the LHS, do so and return a
5914          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
5915          do something only if the second operand is a constant.  */
5916       if (same_p
5917           && (t1 = extract_muldiv (op0, c, code, wide_type,
5918                                    strict_overflow_p)) != 0)
5919         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5920                             fold_convert (ctype, op1));
5921       else if (tcode == MULT_EXPR && code == MULT_EXPR
5922                && (t1 = extract_muldiv (op1, c, code, wide_type,
5923                                         strict_overflow_p)) != 0)
5924         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5925                             fold_convert (ctype, t1));
5926       else if (TREE_CODE (op1) != INTEGER_CST)
5927         return 0;
5928
5929       /* If these are the same operation types, we can associate them
5930          assuming no overflow.  */
5931       if (tcode == code)
5932         {
5933           double_int mul;
5934           int overflow_p;
5935           mul = double_int_mul_with_sign
5936                   (double_int_ext
5937                      (tree_to_double_int (op1),
5938                       TYPE_PRECISION (ctype), TYPE_UNSIGNED (ctype)),
5939                    double_int_ext
5940                      (tree_to_double_int (c),
5941                       TYPE_PRECISION (ctype), TYPE_UNSIGNED (ctype)),
5942                    false, &overflow_p);
5943           overflow_p = (((!TYPE_UNSIGNED (ctype)
5944                           || (TREE_CODE (ctype) == INTEGER_TYPE
5945                               && TYPE_IS_SIZETYPE (ctype)))
5946                          && overflow_p)
5947                         | TREE_OVERFLOW (c) | TREE_OVERFLOW (op1));
5948           if (!double_int_fits_to_tree_p (ctype, mul)
5949               && ((TYPE_UNSIGNED (ctype) && tcode != MULT_EXPR)
5950                   || !TYPE_UNSIGNED (ctype)
5951                   || (TREE_CODE (ctype) == INTEGER_TYPE
5952                       && TYPE_IS_SIZETYPE (ctype))))
5953             overflow_p = 1;
5954           if (!overflow_p)
5955             return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5956                                 double_int_to_tree (ctype, mul));
5957         }
5958
5959       /* If these operations "cancel" each other, we have the main
5960          optimizations of this pass, which occur when either constant is a
5961          multiple of the other, in which case we replace this with either an
5962          operation or CODE or TCODE.
5963
5964          If we have an unsigned type that is not a sizetype, we cannot do
5965          this since it will change the result if the original computation
5966          overflowed.  */
5967       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
5968            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5969           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5970               || (tcode == MULT_EXPR
5971                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5972                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
5973                   && code != MULT_EXPR)))
5974         {
5975           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5976             {
5977               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5978                 *strict_overflow_p = true;
5979               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5980                                   fold_convert (ctype,
5981                                                 const_binop (TRUNC_DIV_EXPR,
5982                                                              op1, c)));
5983             }
5984           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1)))
5985             {
5986               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5987                 *strict_overflow_p = true;
5988               return fold_build2 (code, ctype, fold_convert (ctype, op0),
5989                                   fold_convert (ctype,
5990                                                 const_binop (TRUNC_DIV_EXPR,
5991                                                              c, op1)));
5992             }
5993         }
5994       break;
5995
5996     default:
5997       break;
5998     }
5999
6000   return 0;
6001 }
6002 \f
6003 /* Return a node which has the indicated constant VALUE (either 0 or
6004    1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6005    and is of the indicated TYPE.  */
6006
6007 tree
6008 constant_boolean_node (bool value, tree type)
6009 {
6010   if (type == integer_type_node)
6011     return value ? integer_one_node : integer_zero_node;
6012   else if (type == boolean_type_node)
6013     return value ? boolean_true_node : boolean_false_node;
6014   else if (TREE_CODE (type) == VECTOR_TYPE)
6015     return build_vector_from_val (type,
6016                                   build_int_cst (TREE_TYPE (type),
6017                                                  value ? -1 : 0));
6018   else
6019     return fold_convert (type, value ? integer_one_node : integer_zero_node);
6020 }
6021
6022
6023 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6024    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
6025    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6026    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
6027    COND is the first argument to CODE; otherwise (as in the example
6028    given here), it is the second argument.  TYPE is the type of the
6029    original expression.  Return NULL_TREE if no simplification is
6030    possible.  */
6031
6032 static tree
6033 fold_binary_op_with_conditional_arg (location_t loc,
6034                                      enum tree_code code,
6035                                      tree type, tree op0, tree op1,
6036                                      tree cond, tree arg, int cond_first_p)
6037 {
6038   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6039   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6040   tree test, true_value, false_value;
6041   tree lhs = NULL_TREE;
6042   tree rhs = NULL_TREE;
6043
6044   if (TREE_CODE (cond) == COND_EXPR)
6045     {
6046       test = TREE_OPERAND (cond, 0);
6047       true_value = TREE_OPERAND (cond, 1);
6048       false_value = TREE_OPERAND (cond, 2);
6049       /* If this operand throws an expression, then it does not make
6050          sense to try to perform a logical or arithmetic operation
6051          involving it.  */
6052       if (VOID_TYPE_P (TREE_TYPE (true_value)))
6053         lhs = true_value;
6054       if (VOID_TYPE_P (TREE_TYPE (false_value)))
6055         rhs = false_value;
6056     }
6057   else
6058     {
6059       tree testtype = TREE_TYPE (cond);
6060       test = cond;
6061       true_value = constant_boolean_node (true, testtype);
6062       false_value = constant_boolean_node (false, testtype);
6063     }
6064
6065   /* This transformation is only worthwhile if we don't have to wrap ARG
6066      in a SAVE_EXPR and the operation can be simplified without recursing
6067      on at least one of the branches once its pushed inside the COND_EXPR.  */
6068   if (!TREE_CONSTANT (arg)
6069       && (TREE_SIDE_EFFECTS (arg)
6070           || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6071           || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6072     return NULL_TREE;
6073
6074   arg = fold_convert_loc (loc, arg_type, arg);
6075   if (lhs == 0)
6076     {
6077       true_value = fold_convert_loc (loc, cond_type, true_value);
6078       if (cond_first_p)
6079         lhs = fold_build2_loc (loc, code, type, true_value, arg);
6080       else
6081         lhs = fold_build2_loc (loc, code, type, arg, true_value);
6082     }
6083   if (rhs == 0)
6084     {
6085       false_value = fold_convert_loc (loc, cond_type, false_value);
6086       if (cond_first_p)
6087         rhs = fold_build2_loc (loc, code, type, false_value, arg);
6088       else
6089         rhs = fold_build2_loc (loc, code, type, arg, false_value);
6090     }
6091
6092   /* Check that we have simplified at least one of the branches.  */
6093   if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6094     return NULL_TREE;
6095
6096   return fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs);
6097 }
6098
6099 \f
6100 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6101
6102    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6103    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6104    ADDEND is the same as X.
6105
6106    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6107    and finite.  The problematic cases are when X is zero, and its mode
6108    has signed zeros.  In the case of rounding towards -infinity,
6109    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6110    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6111
6112 bool
6113 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6114 {
6115   if (!real_zerop (addend))
6116     return false;
6117
6118   /* Don't allow the fold with -fsignaling-nans.  */
6119   if (HONOR_SNANS (TYPE_MODE (type)))
6120     return false;
6121
6122   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6123   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6124     return true;
6125
6126   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6127   if (TREE_CODE (addend) == REAL_CST
6128       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6129     negate = !negate;
6130
6131   /* The mode has signed zeros, and we have to honor their sign.
6132      In this situation, there is only one case we can return true for.
6133      X - 0 is the same as X unless rounding towards -infinity is
6134      supported.  */
6135   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6136 }
6137
6138 /* Subroutine of fold() that checks comparisons of built-in math
6139    functions against real constants.
6140
6141    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6142    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6143    is the type of the result and ARG0 and ARG1 are the operands of the
6144    comparison.  ARG1 must be a TREE_REAL_CST.
6145
6146    The function returns the constant folded tree if a simplification
6147    can be made, and NULL_TREE otherwise.  */
6148
6149 static tree
6150 fold_mathfn_compare (location_t loc,
6151                      enum built_in_function fcode, enum tree_code code,
6152                      tree type, tree arg0, tree arg1)
6153 {
6154   REAL_VALUE_TYPE c;
6155
6156   if (BUILTIN_SQRT_P (fcode))
6157     {
6158       tree arg = CALL_EXPR_ARG (arg0, 0);
6159       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6160
6161       c = TREE_REAL_CST (arg1);
6162       if (REAL_VALUE_NEGATIVE (c))
6163         {
6164           /* sqrt(x) < y is always false, if y is negative.  */
6165           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6166             return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6167
6168           /* sqrt(x) > y is always true, if y is negative and we
6169              don't care about NaNs, i.e. negative values of x.  */
6170           if (code == NE_EXPR || !HONOR_NANS (mode))
6171             return omit_one_operand_loc (loc, type, integer_one_node, arg);
6172
6173           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6174           return fold_build2_loc (loc, GE_EXPR, type, arg,
6175                               build_real (TREE_TYPE (arg), dconst0));
6176         }
6177       else if (code == GT_EXPR || code == GE_EXPR)
6178         {
6179           REAL_VALUE_TYPE c2;
6180
6181           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6182           real_convert (&c2, mode, &c2);
6183
6184           if (REAL_VALUE_ISINF (c2))
6185             {
6186               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6187               if (HONOR_INFINITIES (mode))
6188                 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6189                                     build_real (TREE_TYPE (arg), c2));
6190
6191               /* sqrt(x) > y is always false, when y is very large
6192                  and we don't care about infinities.  */
6193               return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6194             }
6195
6196           /* sqrt(x) > c is the same as x > c*c.  */
6197           return fold_build2_loc (loc, code, type, arg,
6198                               build_real (TREE_TYPE (arg), c2));
6199         }
6200       else if (code == LT_EXPR || code == LE_EXPR)
6201         {
6202           REAL_VALUE_TYPE c2;
6203
6204           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6205           real_convert (&c2, mode, &c2);
6206
6207           if (REAL_VALUE_ISINF (c2))
6208             {
6209               /* sqrt(x) < y is always true, when y is a very large
6210                  value and we don't care about NaNs or Infinities.  */
6211               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6212                 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6213
6214               /* sqrt(x) < y is x != +Inf when y is very large and we
6215                  don't care about NaNs.  */
6216               if (! HONOR_NANS (mode))
6217                 return fold_build2_loc (loc, NE_EXPR, type, arg,
6218                                     build_real (TREE_TYPE (arg), c2));
6219
6220               /* sqrt(x) < y is x >= 0 when y is very large and we
6221                  don't care about Infinities.  */
6222               if (! HONOR_INFINITIES (mode))
6223                 return fold_build2_loc (loc, GE_EXPR, type, arg,
6224                                     build_real (TREE_TYPE (arg), dconst0));
6225
6226               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6227               arg = save_expr (arg);
6228               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6229                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6230                                                build_real (TREE_TYPE (arg),
6231                                                            dconst0)),
6232                                   fold_build2_loc (loc, NE_EXPR, type, arg,
6233                                                build_real (TREE_TYPE (arg),
6234                                                            c2)));
6235             }
6236
6237           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6238           if (! HONOR_NANS (mode))
6239             return fold_build2_loc (loc, code, type, arg,
6240                                 build_real (TREE_TYPE (arg), c2));
6241
6242           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6243           arg = save_expr (arg);
6244           return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6245                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6246                                                build_real (TREE_TYPE (arg),
6247                                                            dconst0)),
6248                                   fold_build2_loc (loc, code, type, arg,
6249                                                build_real (TREE_TYPE (arg),
6250                                                            c2)));
6251         }
6252     }
6253
6254   return NULL_TREE;
6255 }
6256
6257 /* Subroutine of fold() that optimizes comparisons against Infinities,
6258    either +Inf or -Inf.
6259
6260    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6261    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6262    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6263
6264    The function returns the constant folded tree if a simplification
6265    can be made, and NULL_TREE otherwise.  */
6266
6267 static tree
6268 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6269                   tree arg0, tree arg1)
6270 {
6271   enum machine_mode mode;
6272   REAL_VALUE_TYPE max;
6273   tree temp;
6274   bool neg;
6275
6276   mode = TYPE_MODE (TREE_TYPE (arg0));
6277
6278   /* For negative infinity swap the sense of the comparison.  */
6279   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6280   if (neg)
6281     code = swap_tree_comparison (code);
6282
6283   switch (code)
6284     {
6285     case GT_EXPR:
6286       /* x > +Inf is always false, if with ignore sNANs.  */
6287       if (HONOR_SNANS (mode))
6288         return NULL_TREE;
6289       return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6290
6291     case LE_EXPR:
6292       /* x <= +Inf is always true, if we don't case about NaNs.  */
6293       if (! HONOR_NANS (mode))
6294         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6295
6296       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6297       arg0 = save_expr (arg0);
6298       return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6299
6300     case EQ_EXPR:
6301     case GE_EXPR:
6302       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6303       real_maxval (&max, neg, mode);
6304       return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6305                           arg0, build_real (TREE_TYPE (arg0), max));
6306
6307     case LT_EXPR:
6308       /* x < +Inf is always equal to x <= DBL_MAX.  */
6309       real_maxval (&max, neg, mode);
6310       return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6311                           arg0, build_real (TREE_TYPE (arg0), max));
6312
6313     case NE_EXPR:
6314       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6315       real_maxval (&max, neg, mode);
6316       if (! HONOR_NANS (mode))
6317         return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6318                             arg0, build_real (TREE_TYPE (arg0), max));
6319
6320       temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6321                           arg0, build_real (TREE_TYPE (arg0), max));
6322       return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
6323
6324     default:
6325       break;
6326     }
6327
6328   return NULL_TREE;
6329 }
6330
6331 /* Subroutine of fold() that optimizes comparisons of a division by
6332    a nonzero integer constant against an integer constant, i.e.
6333    X/C1 op C2.
6334
6335    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6336    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6337    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6338
6339    The function returns the constant folded tree if a simplification
6340    can be made, and NULL_TREE otherwise.  */
6341
6342 static tree
6343 fold_div_compare (location_t loc,
6344                   enum tree_code code, tree type, tree arg0, tree arg1)
6345 {
6346   tree prod, tmp, hi, lo;
6347   tree arg00 = TREE_OPERAND (arg0, 0);
6348   tree arg01 = TREE_OPERAND (arg0, 1);
6349   double_int val;
6350   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6351   bool neg_overflow;
6352   int overflow;
6353
6354   /* We have to do this the hard way to detect unsigned overflow.
6355      prod = int_const_binop (MULT_EXPR, arg01, arg1);  */
6356   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6357                                    TREE_INT_CST_HIGH (arg01),
6358                                    TREE_INT_CST_LOW (arg1),
6359                                    TREE_INT_CST_HIGH (arg1),
6360                                    &val.low, &val.high, unsigned_p);
6361   prod = force_fit_type_double (TREE_TYPE (arg00), val, -1, overflow);
6362   neg_overflow = false;
6363
6364   if (unsigned_p)
6365     {
6366       tmp = int_const_binop (MINUS_EXPR, arg01,
6367                              build_int_cst (TREE_TYPE (arg01), 1));
6368       lo = prod;
6369
6370       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp).  */
6371       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6372                                        TREE_INT_CST_HIGH (prod),
6373                                        TREE_INT_CST_LOW (tmp),
6374                                        TREE_INT_CST_HIGH (tmp),
6375                                        &val.low, &val.high, unsigned_p);
6376       hi = force_fit_type_double (TREE_TYPE (arg00), val,
6377                                   -1, overflow | TREE_OVERFLOW (prod));
6378     }
6379   else if (tree_int_cst_sgn (arg01) >= 0)
6380     {
6381       tmp = int_const_binop (MINUS_EXPR, arg01,
6382                              build_int_cst (TREE_TYPE (arg01), 1));
6383       switch (tree_int_cst_sgn (arg1))
6384         {
6385         case -1:
6386           neg_overflow = true;
6387           lo = int_const_binop (MINUS_EXPR, prod, tmp);
6388           hi = prod;
6389           break;
6390
6391         case  0:
6392           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6393           hi = tmp;
6394           break;
6395
6396         case  1:
6397           hi = int_const_binop (PLUS_EXPR, prod, tmp);
6398           lo = prod;
6399           break;
6400
6401         default:
6402           gcc_unreachable ();
6403         }
6404     }
6405   else
6406     {
6407       /* A negative divisor reverses the relational operators.  */
6408       code = swap_tree_comparison (code);
6409
6410       tmp = int_const_binop (PLUS_EXPR, arg01,
6411                              build_int_cst (TREE_TYPE (arg01), 1));
6412       switch (tree_int_cst_sgn (arg1))
6413         {
6414         case -1:
6415           hi = int_const_binop (MINUS_EXPR, prod, tmp);
6416           lo = prod;
6417           break;
6418
6419         case  0:
6420           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6421           lo = tmp;
6422           break;
6423
6424         case  1:
6425           neg_overflow = true;
6426           lo = int_const_binop (PLUS_EXPR, prod, tmp);
6427           hi = prod;
6428           break;
6429
6430         default:
6431           gcc_unreachable ();
6432         }
6433     }
6434
6435   switch (code)
6436     {
6437     case EQ_EXPR:
6438       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6439         return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6440       if (TREE_OVERFLOW (hi))
6441         return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6442       if (TREE_OVERFLOW (lo))
6443         return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6444       return build_range_check (loc, type, arg00, 1, lo, hi);
6445
6446     case NE_EXPR:
6447       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6448         return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6449       if (TREE_OVERFLOW (hi))
6450         return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6451       if (TREE_OVERFLOW (lo))
6452         return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6453       return build_range_check (loc, type, arg00, 0, lo, hi);
6454
6455     case LT_EXPR:
6456       if (TREE_OVERFLOW (lo))
6457         {
6458           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6459           return omit_one_operand_loc (loc, type, tmp, arg00);
6460         }
6461       return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6462
6463     case LE_EXPR:
6464       if (TREE_OVERFLOW (hi))
6465         {
6466           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6467           return omit_one_operand_loc (loc, type, tmp, arg00);
6468         }
6469       return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6470
6471     case GT_EXPR:
6472       if (TREE_OVERFLOW (hi))
6473         {
6474           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6475           return omit_one_operand_loc (loc, type, tmp, arg00);
6476         }
6477       return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6478
6479     case GE_EXPR:
6480       if (TREE_OVERFLOW (lo))
6481         {
6482           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6483           return omit_one_operand_loc (loc, type, tmp, arg00);
6484         }
6485       return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6486
6487     default:
6488       break;
6489     }
6490
6491   return NULL_TREE;
6492 }
6493
6494
6495 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6496    equality/inequality test, then return a simplified form of the test
6497    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6498    result type.  */
6499
6500 static tree
6501 fold_single_bit_test_into_sign_test (location_t loc,
6502                                      enum tree_code code, tree arg0, tree arg1,
6503                                      tree result_type)
6504 {
6505   /* If this is testing a single bit, we can optimize the test.  */
6506   if ((code == NE_EXPR || code == EQ_EXPR)
6507       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6508       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6509     {
6510       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6511          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6512       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6513
6514       if (arg00 != NULL_TREE
6515           /* This is only a win if casting to a signed type is cheap,
6516              i.e. when arg00's type is not a partial mode.  */
6517           && TYPE_PRECISION (TREE_TYPE (arg00))
6518              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6519         {
6520           tree stype = signed_type_for (TREE_TYPE (arg00));
6521           return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6522                               result_type,
6523                               fold_convert_loc (loc, stype, arg00),
6524                               build_int_cst (stype, 0));
6525         }
6526     }
6527
6528   return NULL_TREE;
6529 }
6530
6531 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6532    equality/inequality test, then return a simplified form of
6533    the test using shifts and logical operations.  Otherwise return
6534    NULL.  TYPE is the desired result type.  */
6535
6536 tree
6537 fold_single_bit_test (location_t loc, enum tree_code code,
6538                       tree arg0, tree arg1, tree result_type)
6539 {
6540   /* If this is testing a single bit, we can optimize the test.  */
6541   if ((code == NE_EXPR || code == EQ_EXPR)
6542       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6543       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6544     {
6545       tree inner = TREE_OPERAND (arg0, 0);
6546       tree type = TREE_TYPE (arg0);
6547       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6548       enum machine_mode operand_mode = TYPE_MODE (type);
6549       int ops_unsigned;
6550       tree signed_type, unsigned_type, intermediate_type;
6551       tree tem, one;
6552
6553       /* First, see if we can fold the single bit test into a sign-bit
6554          test.  */
6555       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6556                                                  result_type);
6557       if (tem)
6558         return tem;
6559
6560       /* Otherwise we have (A & C) != 0 where C is a single bit,
6561          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6562          Similarly for (A & C) == 0.  */
6563
6564       /* If INNER is a right shift of a constant and it plus BITNUM does
6565          not overflow, adjust BITNUM and INNER.  */
6566       if (TREE_CODE (inner) == RSHIFT_EXPR
6567           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6568           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6569           && bitnum < TYPE_PRECISION (type)
6570           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6571                                    bitnum - TYPE_PRECISION (type)))
6572         {
6573           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6574           inner = TREE_OPERAND (inner, 0);
6575         }
6576
6577       /* If we are going to be able to omit the AND below, we must do our
6578          operations as unsigned.  If we must use the AND, we have a choice.
6579          Normally unsigned is faster, but for some machines signed is.  */
6580 #ifdef LOAD_EXTEND_OP
6581       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6582                       && !flag_syntax_only) ? 0 : 1;
6583 #else
6584       ops_unsigned = 1;
6585 #endif
6586
6587       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6588       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6589       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6590       inner = fold_convert_loc (loc, intermediate_type, inner);
6591
6592       if (bitnum != 0)
6593         inner = build2 (RSHIFT_EXPR, intermediate_type,
6594                         inner, size_int (bitnum));
6595
6596       one = build_int_cst (intermediate_type, 1);
6597
6598       if (code == EQ_EXPR)
6599         inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6600
6601       /* Put the AND last so it can combine with more things.  */
6602       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6603
6604       /* Make sure to return the proper type.  */
6605       inner = fold_convert_loc (loc, result_type, inner);
6606
6607       return inner;
6608     }
6609   return NULL_TREE;
6610 }
6611
6612 /* Check whether we are allowed to reorder operands arg0 and arg1,
6613    such that the evaluation of arg1 occurs before arg0.  */
6614
6615 static bool
6616 reorder_operands_p (const_tree arg0, const_tree arg1)
6617 {
6618   if (! flag_evaluation_order)
6619       return true;
6620   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6621     return true;
6622   return ! TREE_SIDE_EFFECTS (arg0)
6623          && ! TREE_SIDE_EFFECTS (arg1);
6624 }
6625
6626 /* Test whether it is preferable two swap two operands, ARG0 and
6627    ARG1, for example because ARG0 is an integer constant and ARG1
6628    isn't.  If REORDER is true, only recommend swapping if we can
6629    evaluate the operands in reverse order.  */
6630
6631 bool
6632 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6633 {
6634   STRIP_SIGN_NOPS (arg0);
6635   STRIP_SIGN_NOPS (arg1);
6636
6637   if (TREE_CODE (arg1) == INTEGER_CST)
6638     return 0;
6639   if (TREE_CODE (arg0) == INTEGER_CST)
6640     return 1;
6641
6642   if (TREE_CODE (arg1) == REAL_CST)
6643     return 0;
6644   if (TREE_CODE (arg0) == REAL_CST)
6645     return 1;
6646
6647   if (TREE_CODE (arg1) == FIXED_CST)
6648     return 0;
6649   if (TREE_CODE (arg0) == FIXED_CST)
6650     return 1;
6651
6652   if (TREE_CODE (arg1) == COMPLEX_CST)
6653     return 0;
6654   if (TREE_CODE (arg0) == COMPLEX_CST)
6655     return 1;
6656
6657   if (TREE_CONSTANT (arg1))
6658     return 0;
6659   if (TREE_CONSTANT (arg0))
6660     return 1;
6661
6662   if (optimize_function_for_size_p (cfun))
6663     return 0;
6664
6665   if (reorder && flag_evaluation_order
6666       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6667     return 0;
6668
6669   /* It is preferable to swap two SSA_NAME to ensure a canonical form
6670      for commutative and comparison operators.  Ensuring a canonical
6671      form allows the optimizers to find additional redundancies without
6672      having to explicitly check for both orderings.  */
6673   if (TREE_CODE (arg0) == SSA_NAME
6674       && TREE_CODE (arg1) == SSA_NAME
6675       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6676     return 1;
6677
6678   /* Put SSA_NAMEs last.  */
6679   if (TREE_CODE (arg1) == SSA_NAME)
6680     return 0;
6681   if (TREE_CODE (arg0) == SSA_NAME)
6682     return 1;
6683
6684   /* Put variables last.  */
6685   if (DECL_P (arg1))
6686     return 0;
6687   if (DECL_P (arg0))
6688     return 1;
6689
6690   return 0;
6691 }
6692
6693 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6694    ARG0 is extended to a wider type.  */
6695
6696 static tree
6697 fold_widened_comparison (location_t loc, enum tree_code code,
6698                          tree type, tree arg0, tree arg1)
6699 {
6700   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6701   tree arg1_unw;
6702   tree shorter_type, outer_type;
6703   tree min, max;
6704   bool above, below;
6705
6706   if (arg0_unw == arg0)
6707     return NULL_TREE;
6708   shorter_type = TREE_TYPE (arg0_unw);
6709
6710 #ifdef HAVE_canonicalize_funcptr_for_compare
6711   /* Disable this optimization if we're casting a function pointer
6712      type on targets that require function pointer canonicalization.  */
6713   if (HAVE_canonicalize_funcptr_for_compare
6714       && TREE_CODE (shorter_type) == POINTER_TYPE
6715       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6716     return NULL_TREE;
6717 #endif
6718
6719   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6720     return NULL_TREE;
6721
6722   arg1_unw = get_unwidened (arg1, NULL_TREE);
6723
6724   /* If possible, express the comparison in the shorter mode.  */
6725   if ((code == EQ_EXPR || code == NE_EXPR
6726        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6727       && (TREE_TYPE (arg1_unw) == shorter_type
6728           || ((TYPE_PRECISION (shorter_type)
6729                >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6730               && (TYPE_UNSIGNED (shorter_type)
6731                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6732           || (TREE_CODE (arg1_unw) == INTEGER_CST
6733               && (TREE_CODE (shorter_type) == INTEGER_TYPE
6734                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6735               && int_fits_type_p (arg1_unw, shorter_type))))
6736     return fold_build2_loc (loc, code, type, arg0_unw,
6737                         fold_convert_loc (loc, shorter_type, arg1_unw));
6738
6739   if (TREE_CODE (arg1_unw) != INTEGER_CST
6740       || TREE_CODE (shorter_type) != INTEGER_TYPE
6741       || !int_fits_type_p (arg1_unw, shorter_type))
6742     return NULL_TREE;
6743
6744   /* If we are comparing with the integer that does not fit into the range
6745      of the shorter type, the result is known.  */
6746   outer_type = TREE_TYPE (arg1_unw);
6747   min = lower_bound_in_type (outer_type, shorter_type);
6748   max = upper_bound_in_type (outer_type, shorter_type);
6749
6750   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6751                                                    max, arg1_unw));
6752   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6753                                                    arg1_unw, min));
6754
6755   switch (code)
6756     {
6757     case EQ_EXPR:
6758       if (above || below)
6759         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6760       break;
6761
6762     case NE_EXPR:
6763       if (above || below)
6764         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6765       break;
6766
6767     case LT_EXPR:
6768     case LE_EXPR:
6769       if (above)
6770         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6771       else if (below)
6772         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6773
6774     case GT_EXPR:
6775     case GE_EXPR:
6776       if (above)
6777         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6778       else if (below)
6779         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6780
6781     default:
6782       break;
6783     }
6784
6785   return NULL_TREE;
6786 }
6787
6788 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6789    ARG0 just the signedness is changed.  */
6790
6791 static tree
6792 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
6793                               tree arg0, tree arg1)
6794 {
6795   tree arg0_inner;
6796   tree inner_type, outer_type;
6797
6798   if (!CONVERT_EXPR_P (arg0))
6799     return NULL_TREE;
6800
6801   outer_type = TREE_TYPE (arg0);
6802   arg0_inner = TREE_OPERAND (arg0, 0);
6803   inner_type = TREE_TYPE (arg0_inner);
6804
6805 #ifdef HAVE_canonicalize_funcptr_for_compare
6806   /* Disable this optimization if we're casting a function pointer
6807      type on targets that require function pointer canonicalization.  */
6808   if (HAVE_canonicalize_funcptr_for_compare
6809       && TREE_CODE (inner_type) == POINTER_TYPE
6810       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6811     return NULL_TREE;
6812 #endif
6813
6814   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6815     return NULL_TREE;
6816
6817   if (TREE_CODE (arg1) != INTEGER_CST
6818       && !(CONVERT_EXPR_P (arg1)
6819            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6820     return NULL_TREE;
6821
6822   if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6823       && code != NE_EXPR
6824       && code != EQ_EXPR)
6825     return NULL_TREE;
6826
6827   if (POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
6828     return NULL_TREE;
6829
6830   if (TREE_CODE (arg1) == INTEGER_CST)
6831     arg1 = force_fit_type_double (inner_type, tree_to_double_int (arg1),
6832                                   0, TREE_OVERFLOW (arg1));
6833   else
6834     arg1 = fold_convert_loc (loc, inner_type, arg1);
6835
6836   return fold_build2_loc (loc, code, type, arg0_inner, arg1);
6837 }
6838
6839 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6840    step of the array.  Reconstructs s and delta in the case of s *
6841    delta being an integer constant (and thus already folded).  ADDR is
6842    the address. MULT is the multiplicative expression.  If the
6843    function succeeds, the new address expression is returned.
6844    Otherwise NULL_TREE is returned.  LOC is the location of the
6845    resulting expression.  */
6846
6847 static tree
6848 try_move_mult_to_index (location_t loc, tree addr, tree op1)
6849 {
6850   tree s, delta, step;
6851   tree ref = TREE_OPERAND (addr, 0), pref;
6852   tree ret, pos;
6853   tree itype;
6854   bool mdim = false;
6855
6856   /*  Strip the nops that might be added when converting op1 to sizetype. */
6857   STRIP_NOPS (op1);
6858
6859   /* Canonicalize op1 into a possibly non-constant delta
6860      and an INTEGER_CST s.  */
6861   if (TREE_CODE (op1) == MULT_EXPR)
6862     {
6863       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6864
6865       STRIP_NOPS (arg0);
6866       STRIP_NOPS (arg1);
6867
6868       if (TREE_CODE (arg0) == INTEGER_CST)
6869         {
6870           s = arg0;
6871           delta = arg1;
6872         }
6873       else if (TREE_CODE (arg1) == INTEGER_CST)
6874         {
6875           s = arg1;
6876           delta = arg0;
6877         }
6878       else
6879         return NULL_TREE;
6880     }
6881   else if (TREE_CODE (op1) == INTEGER_CST)
6882     {
6883       delta = op1;
6884       s = NULL_TREE;
6885     }
6886   else
6887     {
6888       /* Simulate we are delta * 1.  */
6889       delta = op1;
6890       s = integer_one_node;
6891     }
6892
6893   /* Handle &x.array the same as we would handle &x.array[0].  */
6894   if (TREE_CODE (ref) == COMPONENT_REF
6895       && TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE)
6896     {
6897       tree domain;
6898
6899       /* Remember if this was a multi-dimensional array.  */
6900       if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6901         mdim = true;
6902
6903       domain = TYPE_DOMAIN (TREE_TYPE (ref));
6904       if (! domain)
6905         goto cont;
6906       itype = TREE_TYPE (domain);
6907
6908       step = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ref)));
6909       if (TREE_CODE (step) != INTEGER_CST)
6910         goto cont;
6911
6912       if (s)
6913         {
6914           if (! tree_int_cst_equal (step, s))
6915             goto cont;
6916         }
6917       else
6918         {
6919           /* Try if delta is a multiple of step.  */
6920           tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6921           if (! tmp)
6922             goto cont;
6923           delta = tmp;
6924         }
6925
6926       /* Only fold here if we can verify we do not overflow one
6927          dimension of a multi-dimensional array.  */
6928       if (mdim)
6929         {
6930           tree tmp;
6931
6932           if (!TYPE_MIN_VALUE (domain)
6933               || !TYPE_MAX_VALUE (domain)
6934               || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
6935             goto cont;
6936
6937           tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
6938                                  fold_convert_loc (loc, itype,
6939                                                    TYPE_MIN_VALUE (domain)),
6940                                  fold_convert_loc (loc, itype, delta));
6941           if (TREE_CODE (tmp) != INTEGER_CST
6942               || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
6943             goto cont;
6944         }
6945
6946       /* We found a suitable component reference.  */
6947
6948       pref = TREE_OPERAND (addr, 0);
6949       ret = copy_node (pref);
6950       SET_EXPR_LOCATION (ret, loc);
6951
6952       ret = build4_loc (loc, ARRAY_REF, TREE_TYPE (TREE_TYPE (ref)), ret,
6953                         fold_build2_loc
6954                           (loc, PLUS_EXPR, itype,
6955                            fold_convert_loc (loc, itype,
6956                                              TYPE_MIN_VALUE
6957                                                (TYPE_DOMAIN (TREE_TYPE (ref)))),
6958                            fold_convert_loc (loc, itype, delta)),
6959                         NULL_TREE, NULL_TREE);
6960       return build_fold_addr_expr_loc (loc, ret);
6961     }
6962
6963 cont:
6964
6965   for (;; ref = TREE_OPERAND (ref, 0))
6966     {
6967       if (TREE_CODE (ref) == ARRAY_REF)
6968         {
6969           tree domain;
6970
6971           /* Remember if this was a multi-dimensional array.  */
6972           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6973             mdim = true;
6974
6975           domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6976           if (! domain)
6977             continue;
6978           itype = TREE_TYPE (domain);
6979
6980           step = array_ref_element_size (ref);
6981           if (TREE_CODE (step) != INTEGER_CST)
6982             continue;
6983
6984           if (s)
6985             {
6986               if (! tree_int_cst_equal (step, s))
6987                 continue;
6988             }
6989           else
6990             {
6991               /* Try if delta is a multiple of step.  */
6992               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6993               if (! tmp)
6994                 continue;
6995               delta = tmp;
6996             }
6997
6998           /* Only fold here if we can verify we do not overflow one
6999              dimension of a multi-dimensional array.  */
7000           if (mdim)
7001             {
7002               tree tmp;
7003
7004               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7005                   || !TYPE_MAX_VALUE (domain)
7006                   || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
7007                 continue;
7008
7009               tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
7010                                      fold_convert_loc (loc, itype,
7011                                                        TREE_OPERAND (ref, 1)),
7012                                      fold_convert_loc (loc, itype, delta));
7013               if (!tmp
7014                   || TREE_CODE (tmp) != INTEGER_CST
7015                   || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
7016                 continue;
7017             }
7018
7019           break;
7020         }
7021       else
7022         mdim = false;
7023
7024       if (!handled_component_p (ref))
7025         return NULL_TREE;
7026     }
7027
7028   /* We found the suitable array reference.  So copy everything up to it,
7029      and replace the index.  */
7030
7031   pref = TREE_OPERAND (addr, 0);
7032   ret = copy_node (pref);
7033   SET_EXPR_LOCATION (ret, loc);
7034   pos = ret;
7035
7036   while (pref != ref)
7037     {
7038       pref = TREE_OPERAND (pref, 0);
7039       TREE_OPERAND (pos, 0) = copy_node (pref);
7040       pos = TREE_OPERAND (pos, 0);
7041     }
7042
7043   TREE_OPERAND (pos, 1)
7044     = fold_build2_loc (loc, PLUS_EXPR, itype,
7045                        fold_convert_loc (loc, itype, TREE_OPERAND (pos, 1)),
7046                        fold_convert_loc (loc, itype, delta));
7047   return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
7048 }
7049
7050
7051 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
7052    means A >= Y && A != MAX, but in this case we know that
7053    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
7054
7055 static tree
7056 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7057 {
7058   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7059
7060   if (TREE_CODE (bound) == LT_EXPR)
7061     a = TREE_OPERAND (bound, 0);
7062   else if (TREE_CODE (bound) == GT_EXPR)
7063     a = TREE_OPERAND (bound, 1);
7064   else
7065     return NULL_TREE;
7066
7067   typea = TREE_TYPE (a);
7068   if (!INTEGRAL_TYPE_P (typea)
7069       && !POINTER_TYPE_P (typea))
7070     return NULL_TREE;
7071
7072   if (TREE_CODE (ineq) == LT_EXPR)
7073     {
7074       a1 = TREE_OPERAND (ineq, 1);
7075       y = TREE_OPERAND (ineq, 0);
7076     }
7077   else if (TREE_CODE (ineq) == GT_EXPR)
7078     {
7079       a1 = TREE_OPERAND (ineq, 0);
7080       y = TREE_OPERAND (ineq, 1);
7081     }
7082   else
7083     return NULL_TREE;
7084
7085   if (TREE_TYPE (a1) != typea)
7086     return NULL_TREE;
7087
7088   if (POINTER_TYPE_P (typea))
7089     {
7090       /* Convert the pointer types into integer before taking the difference.  */
7091       tree ta = fold_convert_loc (loc, ssizetype, a);
7092       tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7093       diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7094     }
7095   else
7096     diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7097
7098   if (!diff || !integer_onep (diff))
7099    return NULL_TREE;
7100
7101   return fold_build2_loc (loc, GE_EXPR, type, a, y);
7102 }
7103
7104 /* Fold a sum or difference of at least one multiplication.
7105    Returns the folded tree or NULL if no simplification could be made.  */
7106
7107 static tree
7108 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7109                           tree arg0, tree arg1)
7110 {
7111   tree arg00, arg01, arg10, arg11;
7112   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7113
7114   /* (A * C) +- (B * C) -> (A+-B) * C.
7115      (A * C) +- A -> A * (C+-1).
7116      We are most concerned about the case where C is a constant,
7117      but other combinations show up during loop reduction.  Since
7118      it is not difficult, try all four possibilities.  */
7119
7120   if (TREE_CODE (arg0) == MULT_EXPR)
7121     {
7122       arg00 = TREE_OPERAND (arg0, 0);
7123       arg01 = TREE_OPERAND (arg0, 1);
7124     }
7125   else if (TREE_CODE (arg0) == INTEGER_CST)
7126     {
7127       arg00 = build_one_cst (type);
7128       arg01 = arg0;
7129     }
7130   else
7131     {
7132       /* We cannot generate constant 1 for fract.  */
7133       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7134         return NULL_TREE;
7135       arg00 = arg0;
7136       arg01 = build_one_cst (type);
7137     }
7138   if (TREE_CODE (arg1) == MULT_EXPR)
7139     {
7140       arg10 = TREE_OPERAND (arg1, 0);
7141       arg11 = TREE_OPERAND (arg1, 1);
7142     }
7143   else if (TREE_CODE (arg1) == INTEGER_CST)
7144     {
7145       arg10 = build_one_cst (type);
7146       /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7147          the purpose of this canonicalization.  */
7148       if (TREE_INT_CST_HIGH (arg1) == -1
7149           && negate_expr_p (arg1)
7150           && code == PLUS_EXPR)
7151         {
7152           arg11 = negate_expr (arg1);
7153           code = MINUS_EXPR;
7154         }
7155       else
7156         arg11 = arg1;
7157     }
7158   else
7159     {
7160       /* We cannot generate constant 1 for fract.  */
7161       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7162         return NULL_TREE;
7163       arg10 = arg1;
7164       arg11 = build_one_cst (type);
7165     }
7166   same = NULL_TREE;
7167
7168   if (operand_equal_p (arg01, arg11, 0))
7169     same = arg01, alt0 = arg00, alt1 = arg10;
7170   else if (operand_equal_p (arg00, arg10, 0))
7171     same = arg00, alt0 = arg01, alt1 = arg11;
7172   else if (operand_equal_p (arg00, arg11, 0))
7173     same = arg00, alt0 = arg01, alt1 = arg10;
7174   else if (operand_equal_p (arg01, arg10, 0))
7175     same = arg01, alt0 = arg00, alt1 = arg11;
7176
7177   /* No identical multiplicands; see if we can find a common
7178      power-of-two factor in non-power-of-two multiplies.  This
7179      can help in multi-dimensional array access.  */
7180   else if (host_integerp (arg01, 0)
7181            && host_integerp (arg11, 0))
7182     {
7183       HOST_WIDE_INT int01, int11, tmp;
7184       bool swap = false;
7185       tree maybe_same;
7186       int01 = TREE_INT_CST_LOW (arg01);
7187       int11 = TREE_INT_CST_LOW (arg11);
7188
7189       /* Move min of absolute values to int11.  */
7190       if (absu_hwi (int01) < absu_hwi (int11))
7191         {
7192           tmp = int01, int01 = int11, int11 = tmp;
7193           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7194           maybe_same = arg01;
7195           swap = true;
7196         }
7197       else
7198         maybe_same = arg11;
7199
7200       if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7201           /* The remainder should not be a constant, otherwise we
7202              end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7203              increased the number of multiplications necessary.  */
7204           && TREE_CODE (arg10) != INTEGER_CST)
7205         {
7206           alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7207                               build_int_cst (TREE_TYPE (arg00),
7208                                              int01 / int11));
7209           alt1 = arg10;
7210           same = maybe_same;
7211           if (swap)
7212             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7213         }
7214     }
7215
7216   if (same)
7217     return fold_build2_loc (loc, MULT_EXPR, type,
7218                         fold_build2_loc (loc, code, type,
7219                                      fold_convert_loc (loc, type, alt0),
7220                                      fold_convert_loc (loc, type, alt1)),
7221                         fold_convert_loc (loc, type, same));
7222
7223   return NULL_TREE;
7224 }
7225
7226 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7227    specified by EXPR into the buffer PTR of length LEN bytes.
7228    Return the number of bytes placed in the buffer, or zero
7229    upon failure.  */
7230
7231 static int
7232 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7233 {
7234   tree type = TREE_TYPE (expr);
7235   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7236   int byte, offset, word, words;
7237   unsigned char value;
7238
7239   if (total_bytes > len)
7240     return 0;
7241   words = total_bytes / UNITS_PER_WORD;
7242
7243   for (byte = 0; byte < total_bytes; byte++)
7244     {
7245       int bitpos = byte * BITS_PER_UNIT;
7246       if (bitpos < HOST_BITS_PER_WIDE_INT)
7247         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7248       else
7249         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7250                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7251
7252       if (total_bytes > UNITS_PER_WORD)
7253         {
7254           word = byte / UNITS_PER_WORD;
7255           if (WORDS_BIG_ENDIAN)
7256             word = (words - 1) - word;
7257           offset = word * UNITS_PER_WORD;
7258           if (BYTES_BIG_ENDIAN)
7259             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7260           else
7261             offset += byte % UNITS_PER_WORD;
7262         }
7263       else
7264         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7265       ptr[offset] = value;
7266     }
7267   return total_bytes;
7268 }
7269
7270
7271 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7272    specified by EXPR into the buffer PTR of length LEN bytes.
7273    Return the number of bytes placed in the buffer, or zero
7274    upon failure.  */
7275
7276 static int
7277 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7278 {
7279   tree type = TREE_TYPE (expr);
7280   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7281   int byte, offset, word, words, bitpos;
7282   unsigned char value;
7283
7284   /* There are always 32 bits in each long, no matter the size of
7285      the hosts long.  We handle floating point representations with
7286      up to 192 bits.  */
7287   long tmp[6];
7288
7289   if (total_bytes > len)
7290     return 0;
7291   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7292
7293   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7294
7295   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7296        bitpos += BITS_PER_UNIT)
7297     {
7298       byte = (bitpos / BITS_PER_UNIT) & 3;
7299       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7300
7301       if (UNITS_PER_WORD < 4)
7302         {
7303           word = byte / UNITS_PER_WORD;
7304           if (WORDS_BIG_ENDIAN)
7305             word = (words - 1) - word;
7306           offset = word * UNITS_PER_WORD;
7307           if (BYTES_BIG_ENDIAN)
7308             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7309           else
7310             offset += byte % UNITS_PER_WORD;
7311         }
7312       else
7313         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7314       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7315     }
7316   return total_bytes;
7317 }
7318
7319 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7320    specified by EXPR into the buffer PTR of length LEN bytes.
7321    Return the number of bytes placed in the buffer, or zero
7322    upon failure.  */
7323
7324 static int
7325 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7326 {
7327   int rsize, isize;
7328   tree part;
7329
7330   part = TREE_REALPART (expr);
7331   rsize = native_encode_expr (part, ptr, len);
7332   if (rsize == 0)
7333     return 0;
7334   part = TREE_IMAGPART (expr);
7335   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7336   if (isize != rsize)
7337     return 0;
7338   return rsize + isize;
7339 }
7340
7341
7342 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7343    specified by EXPR into the buffer PTR of length LEN bytes.
7344    Return the number of bytes placed in the buffer, or zero
7345    upon failure.  */
7346
7347 static int
7348 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7349 {
7350   int i, size, offset, count;
7351   tree itype, elem, elements;
7352
7353   offset = 0;
7354   elements = TREE_VECTOR_CST_ELTS (expr);
7355   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7356   itype = TREE_TYPE (TREE_TYPE (expr));
7357   size = GET_MODE_SIZE (TYPE_MODE (itype));
7358   for (i = 0; i < count; i++)
7359     {
7360       if (elements)
7361         {
7362           elem = TREE_VALUE (elements);
7363           elements = TREE_CHAIN (elements);
7364         }
7365       else
7366         elem = NULL_TREE;
7367
7368       if (elem)
7369         {
7370           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7371             return 0;
7372         }
7373       else
7374         {
7375           if (offset + size > len)
7376             return 0;
7377           memset (ptr+offset, 0, size);
7378         }
7379       offset += size;
7380     }
7381   return offset;
7382 }
7383
7384
7385 /* Subroutine of native_encode_expr.  Encode the STRING_CST
7386    specified by EXPR into the buffer PTR of length LEN bytes.
7387    Return the number of bytes placed in the buffer, or zero
7388    upon failure.  */
7389
7390 static int
7391 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7392 {
7393   tree type = TREE_TYPE (expr);
7394   HOST_WIDE_INT total_bytes;
7395
7396   if (TREE_CODE (type) != ARRAY_TYPE
7397       || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7398       || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7399       || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7400     return 0;
7401   total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7402   if (total_bytes > len)
7403     return 0;
7404   if (TREE_STRING_LENGTH (expr) < total_bytes)
7405     {
7406       memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7407       memset (ptr + TREE_STRING_LENGTH (expr), 0,
7408               total_bytes - TREE_STRING_LENGTH (expr));
7409     }
7410   else
7411     memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7412   return total_bytes;
7413 }
7414
7415
7416 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7417    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7418    buffer PTR of length LEN bytes.  Return the number of bytes
7419    placed in the buffer, or zero upon failure.  */
7420
7421 int
7422 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7423 {
7424   switch (TREE_CODE (expr))
7425     {
7426     case INTEGER_CST:
7427       return native_encode_int (expr, ptr, len);
7428
7429     case REAL_CST:
7430       return native_encode_real (expr, ptr, len);
7431
7432     case COMPLEX_CST:
7433       return native_encode_complex (expr, ptr, len);
7434
7435     case VECTOR_CST:
7436       return native_encode_vector (expr, ptr, len);
7437
7438     case STRING_CST:
7439       return native_encode_string (expr, ptr, len);
7440
7441     default:
7442       return 0;
7443     }
7444 }
7445
7446
7447 /* Subroutine of native_interpret_expr.  Interpret the contents of
7448    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7449    If the buffer cannot be interpreted, return NULL_TREE.  */
7450
7451 static tree
7452 native_interpret_int (tree type, const unsigned char *ptr, int len)
7453 {
7454   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7455   int byte, offset, word, words;
7456   unsigned char value;
7457   double_int result;
7458
7459   if (total_bytes > len)
7460     return NULL_TREE;
7461   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7462     return NULL_TREE;
7463
7464   result = double_int_zero;
7465   words = total_bytes / UNITS_PER_WORD;
7466
7467   for (byte = 0; byte < total_bytes; byte++)
7468     {
7469       int bitpos = byte * BITS_PER_UNIT;
7470       if (total_bytes > UNITS_PER_WORD)
7471         {
7472           word = byte / UNITS_PER_WORD;
7473           if (WORDS_BIG_ENDIAN)
7474             word = (words - 1) - word;
7475           offset = word * UNITS_PER_WORD;
7476           if (BYTES_BIG_ENDIAN)
7477             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7478           else
7479             offset += byte % UNITS_PER_WORD;
7480         }
7481       else
7482         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7483       value = ptr[offset];
7484
7485       if (bitpos < HOST_BITS_PER_WIDE_INT)
7486         result.low |= (unsigned HOST_WIDE_INT) value << bitpos;
7487       else
7488         result.high |= (unsigned HOST_WIDE_INT) value
7489                        << (bitpos - HOST_BITS_PER_WIDE_INT);
7490     }
7491
7492   return double_int_to_tree (type, result);
7493 }
7494
7495
7496 /* Subroutine of native_interpret_expr.  Interpret the contents of
7497    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7498    If the buffer cannot be interpreted, return NULL_TREE.  */
7499
7500 static tree
7501 native_interpret_real (tree type, const unsigned char *ptr, int len)
7502 {
7503   enum machine_mode mode = TYPE_MODE (type);
7504   int total_bytes = GET_MODE_SIZE (mode);
7505   int byte, offset, word, words, bitpos;
7506   unsigned char value;
7507   /* There are always 32 bits in each long, no matter the size of
7508      the hosts long.  We handle floating point representations with
7509      up to 192 bits.  */
7510   REAL_VALUE_TYPE r;
7511   long tmp[6];
7512
7513   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7514   if (total_bytes > len || total_bytes > 24)
7515     return NULL_TREE;
7516   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7517
7518   memset (tmp, 0, sizeof (tmp));
7519   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7520        bitpos += BITS_PER_UNIT)
7521     {
7522       byte = (bitpos / BITS_PER_UNIT) & 3;
7523       if (UNITS_PER_WORD < 4)
7524         {
7525           word = byte / UNITS_PER_WORD;
7526           if (WORDS_BIG_ENDIAN)
7527             word = (words - 1) - word;
7528           offset = word * UNITS_PER_WORD;
7529           if (BYTES_BIG_ENDIAN)
7530             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7531           else
7532             offset += byte % UNITS_PER_WORD;
7533         }
7534       else
7535         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7536       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7537
7538       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7539     }
7540
7541   real_from_target (&r, tmp, mode);
7542   return build_real (type, r);
7543 }
7544
7545
7546 /* Subroutine of native_interpret_expr.  Interpret the contents of
7547    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7548    If the buffer cannot be interpreted, return NULL_TREE.  */
7549
7550 static tree
7551 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7552 {
7553   tree etype, rpart, ipart;
7554   int size;
7555
7556   etype = TREE_TYPE (type);
7557   size = GET_MODE_SIZE (TYPE_MODE (etype));
7558   if (size * 2 > len)
7559     return NULL_TREE;
7560   rpart = native_interpret_expr (etype, ptr, size);
7561   if (!rpart)
7562     return NULL_TREE;
7563   ipart = native_interpret_expr (etype, ptr+size, size);
7564   if (!ipart)
7565     return NULL_TREE;
7566   return build_complex (type, rpart, ipart);
7567 }
7568
7569
7570 /* Subroutine of native_interpret_expr.  Interpret the contents of
7571    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7572    If the buffer cannot be interpreted, return NULL_TREE.  */
7573
7574 static tree
7575 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7576 {
7577   tree etype, elem, elements;
7578   int i, size, count;
7579
7580   etype = TREE_TYPE (type);
7581   size = GET_MODE_SIZE (TYPE_MODE (etype));
7582   count = TYPE_VECTOR_SUBPARTS (type);
7583   if (size * count > len)
7584     return NULL_TREE;
7585
7586   elements = NULL_TREE;
7587   for (i = count - 1; i >= 0; i--)
7588     {
7589       elem = native_interpret_expr (etype, ptr+(i*size), size);
7590       if (!elem)
7591         return NULL_TREE;
7592       elements = tree_cons (NULL_TREE, elem, elements);
7593     }
7594   return build_vector (type, elements);
7595 }
7596
7597
7598 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7599    the buffer PTR of length LEN as a constant of type TYPE.  For
7600    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7601    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7602    return NULL_TREE.  */
7603
7604 tree
7605 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7606 {
7607   switch (TREE_CODE (type))
7608     {
7609     case INTEGER_TYPE:
7610     case ENUMERAL_TYPE:
7611     case BOOLEAN_TYPE:
7612       return native_interpret_int (type, ptr, len);
7613
7614     case REAL_TYPE:
7615       return native_interpret_real (type, ptr, len);
7616
7617     case COMPLEX_TYPE:
7618       return native_interpret_complex (type, ptr, len);
7619
7620     case VECTOR_TYPE:
7621       return native_interpret_vector (type, ptr, len);
7622
7623     default:
7624       return NULL_TREE;
7625     }
7626 }
7627
7628
7629 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7630    TYPE at compile-time.  If we're unable to perform the conversion
7631    return NULL_TREE.  */
7632
7633 static tree
7634 fold_view_convert_expr (tree type, tree expr)
7635 {
7636   /* We support up to 512-bit values (for V8DFmode).  */
7637   unsigned char buffer[64];
7638   int len;
7639
7640   /* Check that the host and target are sane.  */
7641   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7642     return NULL_TREE;
7643
7644   len = native_encode_expr (expr, buffer, sizeof (buffer));
7645   if (len == 0)
7646     return NULL_TREE;
7647
7648   return native_interpret_expr (type, buffer, len);
7649 }
7650
7651 /* Build an expression for the address of T.  Folds away INDIRECT_REF
7652    to avoid confusing the gimplify process.  */
7653
7654 tree
7655 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7656 {
7657   /* The size of the object is not relevant when talking about its address.  */
7658   if (TREE_CODE (t) == WITH_SIZE_EXPR)
7659     t = TREE_OPERAND (t, 0);
7660
7661   if (TREE_CODE (t) == INDIRECT_REF)
7662     {
7663       t = TREE_OPERAND (t, 0);
7664
7665       if (TREE_TYPE (t) != ptrtype)
7666         t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7667     }
7668   else if (TREE_CODE (t) == MEM_REF
7669       && integer_zerop (TREE_OPERAND (t, 1)))
7670     return TREE_OPERAND (t, 0);
7671   else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7672     {
7673       t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7674
7675       if (TREE_TYPE (t) != ptrtype)
7676         t = fold_convert_loc (loc, ptrtype, t);
7677     }
7678   else
7679     t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7680
7681   return t;
7682 }
7683
7684 /* Build an expression for the address of T.  */
7685
7686 tree
7687 build_fold_addr_expr_loc (location_t loc, tree t)
7688 {
7689   tree ptrtype = build_pointer_type (TREE_TYPE (t));
7690
7691   return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7692 }
7693
7694 static bool vec_cst_ctor_to_array (tree, tree *);
7695
7696 /* Fold a unary expression of code CODE and type TYPE with operand
7697    OP0.  Return the folded expression if folding is successful.
7698    Otherwise, return NULL_TREE.  */
7699
7700 tree
7701 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7702 {
7703   tree tem;
7704   tree arg0;
7705   enum tree_code_class kind = TREE_CODE_CLASS (code);
7706
7707   gcc_assert (IS_EXPR_CODE_CLASS (kind)
7708               && TREE_CODE_LENGTH (code) == 1);
7709
7710   arg0 = op0;
7711   if (arg0)
7712     {
7713       if (CONVERT_EXPR_CODE_P (code)
7714           || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7715         {
7716           /* Don't use STRIP_NOPS, because signedness of argument type
7717              matters.  */
7718           STRIP_SIGN_NOPS (arg0);
7719         }
7720       else
7721         {
7722           /* Strip any conversions that don't change the mode.  This
7723              is safe for every expression, except for a comparison
7724              expression because its signedness is derived from its
7725              operands.
7726
7727              Note that this is done as an internal manipulation within
7728              the constant folder, in order to find the simplest
7729              representation of the arguments so that their form can be
7730              studied.  In any cases, the appropriate type conversions
7731              should be put back in the tree that will get out of the
7732              constant folder.  */
7733           STRIP_NOPS (arg0);
7734         }
7735     }
7736
7737   if (TREE_CODE_CLASS (code) == tcc_unary)
7738     {
7739       if (TREE_CODE (arg0) == COMPOUND_EXPR)
7740         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7741                        fold_build1_loc (loc, code, type,
7742                                     fold_convert_loc (loc, TREE_TYPE (op0),
7743                                                       TREE_OPERAND (arg0, 1))));
7744       else if (TREE_CODE (arg0) == COND_EXPR)
7745         {
7746           tree arg01 = TREE_OPERAND (arg0, 1);
7747           tree arg02 = TREE_OPERAND (arg0, 2);
7748           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7749             arg01 = fold_build1_loc (loc, code, type,
7750                                  fold_convert_loc (loc,
7751                                                    TREE_TYPE (op0), arg01));
7752           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7753             arg02 = fold_build1_loc (loc, code, type,
7754                                  fold_convert_loc (loc,
7755                                                    TREE_TYPE (op0), arg02));
7756           tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7757                              arg01, arg02);
7758
7759           /* If this was a conversion, and all we did was to move into
7760              inside the COND_EXPR, bring it back out.  But leave it if
7761              it is a conversion from integer to integer and the
7762              result precision is no wider than a word since such a
7763              conversion is cheap and may be optimized away by combine,
7764              while it couldn't if it were outside the COND_EXPR.  Then return
7765              so we don't get into an infinite recursion loop taking the
7766              conversion out and then back in.  */
7767
7768           if ((CONVERT_EXPR_CODE_P (code)
7769                || code == NON_LVALUE_EXPR)
7770               && TREE_CODE (tem) == COND_EXPR
7771               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7772               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7773               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7774               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7775               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7776                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7777               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7778                      && (INTEGRAL_TYPE_P
7779                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7780                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7781                   || flag_syntax_only))
7782             tem = build1_loc (loc, code, type,
7783                               build3 (COND_EXPR,
7784                                       TREE_TYPE (TREE_OPERAND
7785                                                  (TREE_OPERAND (tem, 1), 0)),
7786                                       TREE_OPERAND (tem, 0),
7787                                       TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7788                                       TREE_OPERAND (TREE_OPERAND (tem, 2),
7789                                                     0)));
7790           return tem;
7791         }
7792    }
7793
7794   switch (code)
7795     {
7796     case PAREN_EXPR:
7797       /* Re-association barriers around constants and other re-association
7798          barriers can be removed.  */
7799       if (CONSTANT_CLASS_P (op0)
7800           || TREE_CODE (op0) == PAREN_EXPR)
7801         return fold_convert_loc (loc, type, op0);
7802       return NULL_TREE;
7803
7804     CASE_CONVERT:
7805     case FLOAT_EXPR:
7806     case FIX_TRUNC_EXPR:
7807       if (TREE_TYPE (op0) == type)
7808         return op0;
7809
7810       if (COMPARISON_CLASS_P (op0))
7811         {
7812           /* If we have (type) (a CMP b) and type is an integral type, return
7813              new expression involving the new type.  Canonicalize
7814              (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7815              non-integral type.
7816              Do not fold the result as that would not simplify further, also
7817              folding again results in recursions.  */
7818           if (TREE_CODE (type) == BOOLEAN_TYPE)
7819             return build2_loc (loc, TREE_CODE (op0), type,
7820                                TREE_OPERAND (op0, 0),
7821                                TREE_OPERAND (op0, 1));
7822           else if (!INTEGRAL_TYPE_P (type))
7823             return build3_loc (loc, COND_EXPR, type, op0,
7824                                constant_boolean_node (true, type),
7825                                constant_boolean_node (false, type));
7826         }
7827
7828       /* Handle cases of two conversions in a row.  */
7829       if (CONVERT_EXPR_P (op0))
7830         {
7831           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7832           tree inter_type = TREE_TYPE (op0);
7833           int inside_int = INTEGRAL_TYPE_P (inside_type);
7834           int inside_ptr = POINTER_TYPE_P (inside_type);
7835           int inside_float = FLOAT_TYPE_P (inside_type);
7836           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7837           unsigned int inside_prec = TYPE_PRECISION (inside_type);
7838           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7839           int inter_int = INTEGRAL_TYPE_P (inter_type);
7840           int inter_ptr = POINTER_TYPE_P (inter_type);
7841           int inter_float = FLOAT_TYPE_P (inter_type);
7842           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7843           unsigned int inter_prec = TYPE_PRECISION (inter_type);
7844           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7845           int final_int = INTEGRAL_TYPE_P (type);
7846           int final_ptr = POINTER_TYPE_P (type);
7847           int final_float = FLOAT_TYPE_P (type);
7848           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7849           unsigned int final_prec = TYPE_PRECISION (type);
7850           int final_unsignedp = TYPE_UNSIGNED (type);
7851
7852           /* In addition to the cases of two conversions in a row
7853              handled below, if we are converting something to its own
7854              type via an object of identical or wider precision, neither
7855              conversion is needed.  */
7856           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7857               && (((inter_int || inter_ptr) && final_int)
7858                   || (inter_float && final_float))
7859               && inter_prec >= final_prec)
7860             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7861
7862           /* Likewise, if the intermediate and initial types are either both
7863              float or both integer, we don't need the middle conversion if the
7864              former is wider than the latter and doesn't change the signedness
7865              (for integers).  Avoid this if the final type is a pointer since
7866              then we sometimes need the middle conversion.  Likewise if the
7867              final type has a precision not equal to the size of its mode.  */
7868           if (((inter_int && inside_int)
7869                || (inter_float && inside_float)
7870                || (inter_vec && inside_vec))
7871               && inter_prec >= inside_prec
7872               && (inter_float || inter_vec
7873                   || inter_unsignedp == inside_unsignedp)
7874               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7875                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
7876               && ! final_ptr
7877               && (! final_vec || inter_prec == inside_prec))
7878             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7879
7880           /* If we have a sign-extension of a zero-extended value, we can
7881              replace that by a single zero-extension.  */
7882           if (inside_int && inter_int && final_int
7883               && inside_prec < inter_prec && inter_prec < final_prec
7884               && inside_unsignedp && !inter_unsignedp)
7885             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7886
7887           /* Two conversions in a row are not needed unless:
7888              - some conversion is floating-point (overstrict for now), or
7889              - some conversion is a vector (overstrict for now), or
7890              - the intermediate type is narrower than both initial and
7891                final, or
7892              - the intermediate type and innermost type differ in signedness,
7893                and the outermost type is wider than the intermediate, or
7894              - the initial type is a pointer type and the precisions of the
7895                intermediate and final types differ, or
7896              - the final type is a pointer type and the precisions of the
7897                initial and intermediate types differ.  */
7898           if (! inside_float && ! inter_float && ! final_float
7899               && ! inside_vec && ! inter_vec && ! final_vec
7900               && (inter_prec >= inside_prec || inter_prec >= final_prec)
7901               && ! (inside_int && inter_int
7902                     && inter_unsignedp != inside_unsignedp
7903                     && inter_prec < final_prec)
7904               && ((inter_unsignedp && inter_prec > inside_prec)
7905                   == (final_unsignedp && final_prec > inter_prec))
7906               && ! (inside_ptr && inter_prec != final_prec)
7907               && ! (final_ptr && inside_prec != inter_prec)
7908               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7909                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7910             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7911         }
7912
7913       /* Handle (T *)&A.B.C for A being of type T and B and C
7914          living at offset zero.  This occurs frequently in
7915          C++ upcasting and then accessing the base.  */
7916       if (TREE_CODE (op0) == ADDR_EXPR
7917           && POINTER_TYPE_P (type)
7918           && handled_component_p (TREE_OPERAND (op0, 0)))
7919         {
7920           HOST_WIDE_INT bitsize, bitpos;
7921           tree offset;
7922           enum machine_mode mode;
7923           int unsignedp, volatilep;
7924           tree base = TREE_OPERAND (op0, 0);
7925           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7926                                       &mode, &unsignedp, &volatilep, false);
7927           /* If the reference was to a (constant) zero offset, we can use
7928              the address of the base if it has the same base type
7929              as the result type and the pointer type is unqualified.  */
7930           if (! offset && bitpos == 0
7931               && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7932                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7933               && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7934             return fold_convert_loc (loc, type,
7935                                      build_fold_addr_expr_loc (loc, base));
7936         }
7937
7938       if (TREE_CODE (op0) == MODIFY_EXPR
7939           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7940           /* Detect assigning a bitfield.  */
7941           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7942                && DECL_BIT_FIELD
7943                (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7944         {
7945           /* Don't leave an assignment inside a conversion
7946              unless assigning a bitfield.  */
7947           tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7948           /* First do the assignment, then return converted constant.  */
7949           tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7950           TREE_NO_WARNING (tem) = 1;
7951           TREE_USED (tem) = 1;
7952           return tem;
7953         }
7954
7955       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7956          constants (if x has signed type, the sign bit cannot be set
7957          in c).  This folds extension into the BIT_AND_EXPR.
7958          ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7959          very likely don't have maximal range for their precision and this
7960          transformation effectively doesn't preserve non-maximal ranges.  */
7961       if (TREE_CODE (type) == INTEGER_TYPE
7962           && TREE_CODE (op0) == BIT_AND_EXPR
7963           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7964         {
7965           tree and_expr = op0;
7966           tree and0 = TREE_OPERAND (and_expr, 0);
7967           tree and1 = TREE_OPERAND (and_expr, 1);
7968           int change = 0;
7969
7970           if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7971               || (TYPE_PRECISION (type)
7972                   <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7973             change = 1;
7974           else if (TYPE_PRECISION (TREE_TYPE (and1))
7975                    <= HOST_BITS_PER_WIDE_INT
7976                    && host_integerp (and1, 1))
7977             {
7978               unsigned HOST_WIDE_INT cst;
7979
7980               cst = tree_low_cst (and1, 1);
7981               cst &= (HOST_WIDE_INT) -1
7982                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7983               change = (cst == 0);
7984 #ifdef LOAD_EXTEND_OP
7985               if (change
7986                   && !flag_syntax_only
7987                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7988                       == ZERO_EXTEND))
7989                 {
7990                   tree uns = unsigned_type_for (TREE_TYPE (and0));
7991                   and0 = fold_convert_loc (loc, uns, and0);
7992                   and1 = fold_convert_loc (loc, uns, and1);
7993                 }
7994 #endif
7995             }
7996           if (change)
7997             {
7998               tem = force_fit_type_double (type, tree_to_double_int (and1),
7999                                            0, TREE_OVERFLOW (and1));
8000               return fold_build2_loc (loc, BIT_AND_EXPR, type,
8001                                   fold_convert_loc (loc, type, and0), tem);
8002             }
8003         }
8004
8005       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8006          when one of the new casts will fold away. Conservatively we assume
8007          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8008       if (POINTER_TYPE_P (type)
8009           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8010           && (!TYPE_RESTRICT (type) || TYPE_RESTRICT (TREE_TYPE (arg0)))
8011           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8012               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8013               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8014         {
8015           tree arg00 = TREE_OPERAND (arg0, 0);
8016           tree arg01 = TREE_OPERAND (arg0, 1);
8017
8018           return fold_build_pointer_plus_loc
8019                    (loc, fold_convert_loc (loc, type, arg00), arg01);
8020         }
8021
8022       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8023          of the same precision, and X is an integer type not narrower than
8024          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
8025       if (INTEGRAL_TYPE_P (type)
8026           && TREE_CODE (op0) == BIT_NOT_EXPR
8027           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8028           && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8029           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8030         {
8031           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8032           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8033               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8034             return fold_build1_loc (loc, BIT_NOT_EXPR, type,
8035                                 fold_convert_loc (loc, type, tem));
8036         }
8037
8038       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8039          type of X and Y (integer types only).  */
8040       if (INTEGRAL_TYPE_P (type)
8041           && TREE_CODE (op0) == MULT_EXPR
8042           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8043           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8044         {
8045           /* Be careful not to introduce new overflows.  */
8046           tree mult_type;
8047           if (TYPE_OVERFLOW_WRAPS (type))
8048             mult_type = type;
8049           else
8050             mult_type = unsigned_type_for (type);
8051
8052           if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8053             {
8054               tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8055                                  fold_convert_loc (loc, mult_type,
8056                                                    TREE_OPERAND (op0, 0)),
8057                                  fold_convert_loc (loc, mult_type,
8058                                                    TREE_OPERAND (op0, 1)));
8059               return fold_convert_loc (loc, type, tem);
8060             }
8061         }
8062
8063       tem = fold_convert_const (code, type, op0);
8064       return tem ? tem : NULL_TREE;
8065
8066     case ADDR_SPACE_CONVERT_EXPR:
8067       if (integer_zerop (arg0))
8068         return fold_convert_const (code, type, arg0);
8069       return NULL_TREE;
8070
8071     case FIXED_CONVERT_EXPR:
8072       tem = fold_convert_const (code, type, arg0);
8073       return tem ? tem : NULL_TREE;
8074
8075     case VIEW_CONVERT_EXPR:
8076       if (TREE_TYPE (op0) == type)
8077         return op0;
8078       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8079         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8080                             type, TREE_OPERAND (op0, 0));
8081       if (TREE_CODE (op0) == MEM_REF)
8082         return fold_build2_loc (loc, MEM_REF, type,
8083                                 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
8084
8085       /* For integral conversions with the same precision or pointer
8086          conversions use a NOP_EXPR instead.  */
8087       if ((INTEGRAL_TYPE_P (type)
8088            || POINTER_TYPE_P (type))
8089           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8090               || POINTER_TYPE_P (TREE_TYPE (op0)))
8091           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8092         return fold_convert_loc (loc, type, op0);
8093
8094       /* Strip inner integral conversions that do not change the precision.  */
8095       if (CONVERT_EXPR_P (op0)
8096           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8097               || POINTER_TYPE_P (TREE_TYPE (op0)))
8098           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8099               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8100           && (TYPE_PRECISION (TREE_TYPE (op0))
8101               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8102         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8103                             type, TREE_OPERAND (op0, 0));
8104
8105       return fold_view_convert_expr (type, op0);
8106
8107     case NEGATE_EXPR:
8108       tem = fold_negate_expr (loc, arg0);
8109       if (tem)
8110         return fold_convert_loc (loc, type, tem);
8111       return NULL_TREE;
8112
8113     case ABS_EXPR:
8114       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8115         return fold_abs_const (arg0, type);
8116       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8117         return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8118       /* Convert fabs((double)float) into (double)fabsf(float).  */
8119       else if (TREE_CODE (arg0) == NOP_EXPR
8120                && TREE_CODE (type) == REAL_TYPE)
8121         {
8122           tree targ0 = strip_float_extensions (arg0);
8123           if (targ0 != arg0)
8124             return fold_convert_loc (loc, type,
8125                                      fold_build1_loc (loc, ABS_EXPR,
8126                                                   TREE_TYPE (targ0),
8127                                                   targ0));
8128         }
8129       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8130       else if (TREE_CODE (arg0) == ABS_EXPR)
8131         return arg0;
8132       else if (tree_expr_nonnegative_p (arg0))
8133         return arg0;
8134
8135       /* Strip sign ops from argument.  */
8136       if (TREE_CODE (type) == REAL_TYPE)
8137         {
8138           tem = fold_strip_sign_ops (arg0);
8139           if (tem)
8140             return fold_build1_loc (loc, ABS_EXPR, type,
8141                                 fold_convert_loc (loc, type, tem));
8142         }
8143       return NULL_TREE;
8144
8145     case CONJ_EXPR:
8146       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8147         return fold_convert_loc (loc, type, arg0);
8148       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8149         {
8150           tree itype = TREE_TYPE (type);
8151           tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8152           tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8153           return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8154                               negate_expr (ipart));
8155         }
8156       if (TREE_CODE (arg0) == COMPLEX_CST)
8157         {
8158           tree itype = TREE_TYPE (type);
8159           tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8160           tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8161           return build_complex (type, rpart, negate_expr (ipart));
8162         }
8163       if (TREE_CODE (arg0) == CONJ_EXPR)
8164         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8165       return NULL_TREE;
8166
8167     case BIT_NOT_EXPR:
8168       if (TREE_CODE (arg0) == INTEGER_CST)
8169         return fold_not_const (arg0, type);
8170       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8171         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8172       /* Convert ~ (-A) to A - 1.  */
8173       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8174         return fold_build2_loc (loc, MINUS_EXPR, type,
8175                             fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8176                             build_int_cst (type, 1));
8177       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8178       else if (INTEGRAL_TYPE_P (type)
8179                && ((TREE_CODE (arg0) == MINUS_EXPR
8180                     && integer_onep (TREE_OPERAND (arg0, 1)))
8181                    || (TREE_CODE (arg0) == PLUS_EXPR
8182                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8183         return fold_build1_loc (loc, NEGATE_EXPR, type,
8184                             fold_convert_loc (loc, type,
8185                                               TREE_OPERAND (arg0, 0)));
8186       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8187       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8188                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8189                                      fold_convert_loc (loc, type,
8190                                                        TREE_OPERAND (arg0, 0)))))
8191         return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8192                             fold_convert_loc (loc, type,
8193                                               TREE_OPERAND (arg0, 1)));
8194       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8195                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8196                                      fold_convert_loc (loc, type,
8197                                                        TREE_OPERAND (arg0, 1)))))
8198         return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8199                             fold_convert_loc (loc, type,
8200                                               TREE_OPERAND (arg0, 0)), tem);
8201       /* Perform BIT_NOT_EXPR on each element individually.  */
8202       else if (TREE_CODE (arg0) == VECTOR_CST)
8203         {
8204           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8205           int count = TYPE_VECTOR_SUBPARTS (type), i;
8206
8207           for (i = 0; i < count; i++)
8208             {
8209               if (elements)
8210                 {
8211                   elem = TREE_VALUE (elements);
8212                   elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8213                   if (elem == NULL_TREE)
8214                     break;
8215                   elements = TREE_CHAIN (elements);
8216                 }
8217               else
8218                 elem = build_int_cst (TREE_TYPE (type), -1);
8219               list = tree_cons (NULL_TREE, elem, list);
8220             }
8221           if (i == count)
8222             return build_vector (type, nreverse (list));
8223         }
8224
8225       return NULL_TREE;
8226
8227     case TRUTH_NOT_EXPR:
8228       /* The argument to invert_truthvalue must have Boolean type.  */
8229       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8230           arg0 = fold_convert_loc (loc, boolean_type_node, arg0);
8231
8232       /* Note that the operand of this must be an int
8233          and its values must be 0 or 1.
8234          ("true" is a fixed value perhaps depending on the language,
8235          but we don't handle values other than 1 correctly yet.)  */
8236       tem = fold_truth_not_expr (loc, arg0);
8237       if (!tem)
8238         return NULL_TREE;
8239       return fold_convert_loc (loc, type, tem);
8240
8241     case REALPART_EXPR:
8242       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8243         return fold_convert_loc (loc, type, arg0);
8244       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8245         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8246                                  TREE_OPERAND (arg0, 1));
8247       if (TREE_CODE (arg0) == COMPLEX_CST)
8248         return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8249       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8250         {
8251           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8252           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8253                              fold_build1_loc (loc, REALPART_EXPR, itype,
8254                                           TREE_OPERAND (arg0, 0)),
8255                              fold_build1_loc (loc, REALPART_EXPR, itype,
8256                                           TREE_OPERAND (arg0, 1)));
8257           return fold_convert_loc (loc, type, tem);
8258         }
8259       if (TREE_CODE (arg0) == CONJ_EXPR)
8260         {
8261           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8262           tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8263                              TREE_OPERAND (arg0, 0));
8264           return fold_convert_loc (loc, type, tem);
8265         }
8266       if (TREE_CODE (arg0) == CALL_EXPR)
8267         {
8268           tree fn = get_callee_fndecl (arg0);
8269           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8270             switch (DECL_FUNCTION_CODE (fn))
8271               {
8272               CASE_FLT_FN (BUILT_IN_CEXPI):
8273                 fn = mathfn_built_in (type, BUILT_IN_COS);
8274                 if (fn)
8275                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8276                 break;
8277
8278               default:
8279                 break;
8280               }
8281         }
8282       return NULL_TREE;
8283
8284     case IMAGPART_EXPR:
8285       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8286         return build_zero_cst (type);
8287       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8288         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8289                                  TREE_OPERAND (arg0, 0));
8290       if (TREE_CODE (arg0) == COMPLEX_CST)
8291         return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8292       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8293         {
8294           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8295           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8296                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8297                                           TREE_OPERAND (arg0, 0)),
8298                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8299                                           TREE_OPERAND (arg0, 1)));
8300           return fold_convert_loc (loc, type, tem);
8301         }
8302       if (TREE_CODE (arg0) == CONJ_EXPR)
8303         {
8304           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8305           tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8306           return fold_convert_loc (loc, type, negate_expr (tem));
8307         }
8308       if (TREE_CODE (arg0) == CALL_EXPR)
8309         {
8310           tree fn = get_callee_fndecl (arg0);
8311           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8312             switch (DECL_FUNCTION_CODE (fn))
8313               {
8314               CASE_FLT_FN (BUILT_IN_CEXPI):
8315                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8316                 if (fn)
8317                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8318                 break;
8319
8320               default:
8321                 break;
8322               }
8323         }
8324       return NULL_TREE;
8325
8326     case INDIRECT_REF:
8327       /* Fold *&X to X if X is an lvalue.  */
8328       if (TREE_CODE (op0) == ADDR_EXPR)
8329         {
8330           tree op00 = TREE_OPERAND (op0, 0);
8331           if ((TREE_CODE (op00) == VAR_DECL
8332                || TREE_CODE (op00) == PARM_DECL
8333                || TREE_CODE (op00) == RESULT_DECL)
8334               && !TREE_READONLY (op00))
8335             return op00;
8336         }
8337       return NULL_TREE;
8338
8339     case VEC_UNPACK_LO_EXPR:
8340     case VEC_UNPACK_HI_EXPR:
8341     case VEC_UNPACK_FLOAT_LO_EXPR:
8342     case VEC_UNPACK_FLOAT_HI_EXPR:
8343       {
8344         unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8345         tree *elts, vals = NULL_TREE;
8346         enum tree_code subcode;
8347
8348         gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
8349         if (TREE_CODE (arg0) != VECTOR_CST)
8350           return NULL_TREE;
8351
8352         elts = XALLOCAVEC (tree, nelts * 2);
8353         if (!vec_cst_ctor_to_array (arg0, elts))
8354           return NULL_TREE;
8355
8356         if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
8357                                    || code == VEC_UNPACK_FLOAT_LO_EXPR))
8358           elts += nelts;
8359
8360         if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
8361           subcode = NOP_EXPR;
8362         else
8363           subcode = FLOAT_EXPR;
8364
8365         for (i = 0; i < nelts; i++)
8366           {
8367             elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
8368             if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
8369               return NULL_TREE;
8370           }
8371
8372         for (i = 0; i < nelts; i++)
8373           vals = tree_cons (NULL_TREE, elts[nelts - i - 1], vals);
8374         return build_vector (type, vals);
8375       }
8376
8377     default:
8378       return NULL_TREE;
8379     } /* switch (code) */
8380 }
8381
8382
8383 /* If the operation was a conversion do _not_ mark a resulting constant
8384    with TREE_OVERFLOW if the original constant was not.  These conversions
8385    have implementation defined behavior and retaining the TREE_OVERFLOW
8386    flag here would confuse later passes such as VRP.  */
8387 tree
8388 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8389                                 tree type, tree op0)
8390 {
8391   tree res = fold_unary_loc (loc, code, type, op0);
8392   if (res
8393       && TREE_CODE (res) == INTEGER_CST
8394       && TREE_CODE (op0) == INTEGER_CST
8395       && CONVERT_EXPR_CODE_P (code))
8396     TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8397
8398   return res;
8399 }
8400
8401 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8402    operands OP0 and OP1.  LOC is the location of the resulting expression.
8403    ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8404    Return the folded expression if folding is successful.  Otherwise,
8405    return NULL_TREE.  */
8406 static tree
8407 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8408                   tree arg0, tree arg1, tree op0, tree op1)
8409 {
8410   tree tem;
8411
8412   /* We only do these simplifications if we are optimizing.  */
8413   if (!optimize)
8414     return NULL_TREE;
8415
8416   /* Check for things like (A || B) && (A || C).  We can convert this
8417      to A || (B && C).  Note that either operator can be any of the four
8418      truth and/or operations and the transformation will still be
8419      valid.   Also note that we only care about order for the
8420      ANDIF and ORIF operators.  If B contains side effects, this
8421      might change the truth-value of A.  */
8422   if (TREE_CODE (arg0) == TREE_CODE (arg1)
8423       && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8424           || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8425           || TREE_CODE (arg0) == TRUTH_AND_EXPR
8426           || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8427       && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8428     {
8429       tree a00 = TREE_OPERAND (arg0, 0);
8430       tree a01 = TREE_OPERAND (arg0, 1);
8431       tree a10 = TREE_OPERAND (arg1, 0);
8432       tree a11 = TREE_OPERAND (arg1, 1);
8433       int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8434                           || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8435                          && (code == TRUTH_AND_EXPR
8436                              || code == TRUTH_OR_EXPR));
8437
8438       if (operand_equal_p (a00, a10, 0))
8439         return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8440                             fold_build2_loc (loc, code, type, a01, a11));
8441       else if (commutative && operand_equal_p (a00, a11, 0))
8442         return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8443                             fold_build2_loc (loc, code, type, a01, a10));
8444       else if (commutative && operand_equal_p (a01, a10, 0))
8445         return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8446                             fold_build2_loc (loc, code, type, a00, a11));
8447
8448       /* This case if tricky because we must either have commutative
8449          operators or else A10 must not have side-effects.  */
8450
8451       else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8452                && operand_equal_p (a01, a11, 0))
8453         return fold_build2_loc (loc, TREE_CODE (arg0), type,
8454                             fold_build2_loc (loc, code, type, a00, a10),
8455                             a01);
8456     }
8457
8458   /* See if we can build a range comparison.  */
8459   if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8460     return tem;
8461
8462   if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8463       || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8464     {
8465       tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8466       if (tem)
8467         return fold_build2_loc (loc, code, type, tem, arg1);
8468     }
8469
8470   if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8471       || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8472     {
8473       tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8474       if (tem)
8475         return fold_build2_loc (loc, code, type, arg0, tem);
8476     }
8477
8478   /* Check for the possibility of merging component references.  If our
8479      lhs is another similar operation, try to merge its rhs with our
8480      rhs.  Then try to merge our lhs and rhs.  */
8481   if (TREE_CODE (arg0) == code
8482       && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8483                                          TREE_OPERAND (arg0, 1), arg1)))
8484     return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8485
8486   if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8487     return tem;
8488
8489   if ((BRANCH_COST (optimize_function_for_speed_p (cfun),
8490                     false) >= 2)
8491       && LOGICAL_OP_NON_SHORT_CIRCUIT
8492       && (code == TRUTH_AND_EXPR
8493           || code == TRUTH_ANDIF_EXPR
8494           || code == TRUTH_OR_EXPR
8495           || code == TRUTH_ORIF_EXPR))
8496     {
8497       enum tree_code ncode, icode;
8498
8499       ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8500               ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8501       icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8502
8503       /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8504          or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8505          We don't want to pack more than two leafs to a non-IF AND/OR
8506          expression.
8507          If tree-code of left-hand operand isn't an AND/OR-IF code and not
8508          equal to IF-CODE, then we don't want to add right-hand operand.
8509          If the inner right-hand side of left-hand operand has
8510          side-effects, or isn't simple, then we can't add to it,
8511          as otherwise we might destroy if-sequence.  */
8512       if (TREE_CODE (arg0) == icode
8513           && simple_operand_p_2 (arg1)
8514           /* Needed for sequence points to handle trappings, and
8515              side-effects.  */
8516           && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8517         {
8518           tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8519                                  arg1);
8520           return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8521                                   tem);
8522         }
8523         /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8524            or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C).  */
8525       else if (TREE_CODE (arg1) == icode
8526           && simple_operand_p_2 (arg0)
8527           /* Needed for sequence points to handle trappings, and
8528              side-effects.  */
8529           && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8530         {
8531           tem = fold_build2_loc (loc, ncode, type, 
8532                                  arg0, TREE_OPERAND (arg1, 0));
8533           return fold_build2_loc (loc, icode, type, tem,
8534                                   TREE_OPERAND (arg1, 1));
8535         }
8536       /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8537          into (A OR B).
8538          For sequence point consistancy, we need to check for trapping,
8539          and side-effects.  */
8540       else if (code == icode && simple_operand_p_2 (arg0)
8541                && simple_operand_p_2 (arg1))
8542         return fold_build2_loc (loc, ncode, type, arg0, arg1);
8543     }
8544
8545   return NULL_TREE;
8546 }
8547
8548 /* Fold a binary expression of code CODE and type TYPE with operands
8549    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8550    Return the folded expression if folding is successful.  Otherwise,
8551    return NULL_TREE.  */
8552
8553 static tree
8554 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8555 {
8556   enum tree_code compl_code;
8557
8558   if (code == MIN_EXPR)
8559     compl_code = MAX_EXPR;
8560   else if (code == MAX_EXPR)
8561     compl_code = MIN_EXPR;
8562   else
8563     gcc_unreachable ();
8564
8565   /* MIN (MAX (a, b), b) == b.  */
8566   if (TREE_CODE (op0) == compl_code
8567       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8568     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8569
8570   /* MIN (MAX (b, a), b) == b.  */
8571   if (TREE_CODE (op0) == compl_code
8572       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8573       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8574     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8575
8576   /* MIN (a, MAX (a, b)) == a.  */
8577   if (TREE_CODE (op1) == compl_code
8578       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8579       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8580     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8581
8582   /* MIN (a, MAX (b, a)) == a.  */
8583   if (TREE_CODE (op1) == compl_code
8584       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8585       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8586     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8587
8588   return NULL_TREE;
8589 }
8590
8591 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8592    by changing CODE to reduce the magnitude of constants involved in
8593    ARG0 of the comparison.
8594    Returns a canonicalized comparison tree if a simplification was
8595    possible, otherwise returns NULL_TREE.
8596    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8597    valid if signed overflow is undefined.  */
8598
8599 static tree
8600 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8601                                  tree arg0, tree arg1,
8602                                  bool *strict_overflow_p)
8603 {
8604   enum tree_code code0 = TREE_CODE (arg0);
8605   tree t, cst0 = NULL_TREE;
8606   int sgn0;
8607   bool swap = false;
8608
8609   /* Match A +- CST code arg1 and CST code arg1.  We can change the
8610      first form only if overflow is undefined.  */
8611   if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8612          /* In principle pointers also have undefined overflow behavior,
8613             but that causes problems elsewhere.  */
8614          && !POINTER_TYPE_P (TREE_TYPE (arg0))
8615          && (code0 == MINUS_EXPR
8616              || code0 == PLUS_EXPR)
8617          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8618         || code0 == INTEGER_CST))
8619     return NULL_TREE;
8620
8621   /* Identify the constant in arg0 and its sign.  */
8622   if (code0 == INTEGER_CST)
8623     cst0 = arg0;
8624   else
8625     cst0 = TREE_OPERAND (arg0, 1);
8626   sgn0 = tree_int_cst_sgn (cst0);
8627
8628   /* Overflowed constants and zero will cause problems.  */
8629   if (integer_zerop (cst0)
8630       || TREE_OVERFLOW (cst0))
8631     return NULL_TREE;
8632
8633   /* See if we can reduce the magnitude of the constant in
8634      arg0 by changing the comparison code.  */
8635   if (code0 == INTEGER_CST)
8636     {
8637       /* CST <= arg1  ->  CST-1 < arg1.  */
8638       if (code == LE_EXPR && sgn0 == 1)
8639         code = LT_EXPR;
8640       /* -CST < arg1  ->  -CST-1 <= arg1.  */
8641       else if (code == LT_EXPR && sgn0 == -1)
8642         code = LE_EXPR;
8643       /* CST > arg1  ->  CST-1 >= arg1.  */
8644       else if (code == GT_EXPR && sgn0 == 1)
8645         code = GE_EXPR;
8646       /* -CST >= arg1  ->  -CST-1 > arg1.  */
8647       else if (code == GE_EXPR && sgn0 == -1)
8648         code = GT_EXPR;
8649       else
8650         return NULL_TREE;
8651       /* arg1 code' CST' might be more canonical.  */
8652       swap = true;
8653     }
8654   else
8655     {
8656       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
8657       if (code == LT_EXPR
8658           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8659         code = LE_EXPR;
8660       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
8661       else if (code == GT_EXPR
8662                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8663         code = GE_EXPR;
8664       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
8665       else if (code == LE_EXPR
8666                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8667         code = LT_EXPR;
8668       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
8669       else if (code == GE_EXPR
8670                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8671         code = GT_EXPR;
8672       else
8673         return NULL_TREE;
8674       *strict_overflow_p = true;
8675     }
8676
8677   /* Now build the constant reduced in magnitude.  But not if that
8678      would produce one outside of its types range.  */
8679   if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8680       && ((sgn0 == 1
8681            && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8682            && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8683           || (sgn0 == -1
8684               && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8685               && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8686     /* We cannot swap the comparison here as that would cause us to
8687        endlessly recurse.  */
8688     return NULL_TREE;
8689
8690   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8691                        cst0, build_int_cst (TREE_TYPE (cst0), 1));
8692   if (code0 != INTEGER_CST)
8693     t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8694   t = fold_convert (TREE_TYPE (arg1), t);
8695
8696   /* If swapping might yield to a more canonical form, do so.  */
8697   if (swap)
8698     return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8699   else
8700     return fold_build2_loc (loc, code, type, t, arg1);
8701 }
8702
8703 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8704    overflow further.  Try to decrease the magnitude of constants involved
8705    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8706    and put sole constants at the second argument position.
8707    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
8708
8709 static tree
8710 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8711                                tree arg0, tree arg1)
8712 {
8713   tree t;
8714   bool strict_overflow_p;
8715   const char * const warnmsg = G_("assuming signed overflow does not occur "
8716                                   "when reducing constant in comparison");
8717
8718   /* Try canonicalization by simplifying arg0.  */
8719   strict_overflow_p = false;
8720   t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8721                                        &strict_overflow_p);
8722   if (t)
8723     {
8724       if (strict_overflow_p)
8725         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8726       return t;
8727     }
8728
8729   /* Try canonicalization by simplifying arg1 using the swapped
8730      comparison.  */
8731   code = swap_tree_comparison (code);
8732   strict_overflow_p = false;
8733   t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8734                                        &strict_overflow_p);
8735   if (t && strict_overflow_p)
8736     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8737   return t;
8738 }
8739
8740 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8741    space.  This is used to avoid issuing overflow warnings for
8742    expressions like &p->x which can not wrap.  */
8743
8744 static bool
8745 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8746 {
8747   unsigned HOST_WIDE_INT offset_low, total_low;
8748   HOST_WIDE_INT size, offset_high, total_high;
8749
8750   if (!POINTER_TYPE_P (TREE_TYPE (base)))
8751     return true;
8752
8753   if (bitpos < 0)
8754     return true;
8755
8756   if (offset == NULL_TREE)
8757     {
8758       offset_low = 0;
8759       offset_high = 0;
8760     }
8761   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8762     return true;
8763   else
8764     {
8765       offset_low = TREE_INT_CST_LOW (offset);
8766       offset_high = TREE_INT_CST_HIGH (offset);
8767     }
8768
8769   if (add_double_with_sign (offset_low, offset_high,
8770                             bitpos / BITS_PER_UNIT, 0,
8771                             &total_low, &total_high,
8772                             true))
8773     return true;
8774
8775   if (total_high != 0)
8776     return true;
8777
8778   size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8779   if (size <= 0)
8780     return true;
8781
8782   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8783      array.  */
8784   if (TREE_CODE (base) == ADDR_EXPR)
8785     {
8786       HOST_WIDE_INT base_size;
8787
8788       base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8789       if (base_size > 0 && size < base_size)
8790         size = base_size;
8791     }
8792
8793   return total_low > (unsigned HOST_WIDE_INT) size;
8794 }
8795
8796 /* Subroutine of fold_binary.  This routine performs all of the
8797    transformations that are common to the equality/inequality
8798    operators (EQ_EXPR and NE_EXPR) and the ordering operators
8799    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
8800    fold_binary should call fold_binary.  Fold a comparison with
8801    tree code CODE and type TYPE with operands OP0 and OP1.  Return
8802    the folded comparison or NULL_TREE.  */
8803
8804 static tree
8805 fold_comparison (location_t loc, enum tree_code code, tree type,
8806                  tree op0, tree op1)
8807 {
8808   tree arg0, arg1, tem;
8809
8810   arg0 = op0;
8811   arg1 = op1;
8812
8813   STRIP_SIGN_NOPS (arg0);
8814   STRIP_SIGN_NOPS (arg1);
8815
8816   tem = fold_relational_const (code, type, arg0, arg1);
8817   if (tem != NULL_TREE)
8818     return tem;
8819
8820   /* If one arg is a real or integer constant, put it last.  */
8821   if (tree_swap_operands_p (arg0, arg1, true))
8822     return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
8823
8824   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
8825   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8826       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8827           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8828           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8829       && (TREE_CODE (arg1) == INTEGER_CST
8830           && !TREE_OVERFLOW (arg1)))
8831     {
8832       tree const1 = TREE_OPERAND (arg0, 1);
8833       tree const2 = arg1;
8834       tree variable = TREE_OPERAND (arg0, 0);
8835       tree lhs;
8836       int lhs_add;
8837       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8838
8839       lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
8840                          TREE_TYPE (arg1), const2, const1);
8841
8842       /* If the constant operation overflowed this can be
8843          simplified as a comparison against INT_MAX/INT_MIN.  */
8844       if (TREE_CODE (lhs) == INTEGER_CST
8845           && TREE_OVERFLOW (lhs))
8846         {
8847           int const1_sgn = tree_int_cst_sgn (const1);
8848           enum tree_code code2 = code;
8849
8850           /* Get the sign of the constant on the lhs if the
8851              operation were VARIABLE + CONST1.  */
8852           if (TREE_CODE (arg0) == MINUS_EXPR)
8853             const1_sgn = -const1_sgn;
8854
8855           /* The sign of the constant determines if we overflowed
8856              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8857              Canonicalize to the INT_MIN overflow by swapping the comparison
8858              if necessary.  */
8859           if (const1_sgn == -1)
8860             code2 = swap_tree_comparison (code);
8861
8862           /* We now can look at the canonicalized case
8863                VARIABLE + 1  CODE2  INT_MIN
8864              and decide on the result.  */
8865           if (code2 == LT_EXPR
8866               || code2 == LE_EXPR
8867               || code2 == EQ_EXPR)
8868             return omit_one_operand_loc (loc, type, boolean_false_node, variable);
8869           else if (code2 == NE_EXPR
8870                    || code2 == GE_EXPR
8871                    || code2 == GT_EXPR)
8872             return omit_one_operand_loc (loc, type, boolean_true_node, variable);
8873         }
8874
8875       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8876           && (TREE_CODE (lhs) != INTEGER_CST
8877               || !TREE_OVERFLOW (lhs)))
8878         {
8879           if (code != EQ_EXPR && code != NE_EXPR)
8880             fold_overflow_warning ("assuming signed overflow does not occur "
8881                                    "when changing X +- C1 cmp C2 to "
8882                                    "X cmp C1 +- C2",
8883                                    WARN_STRICT_OVERFLOW_COMPARISON);
8884           return fold_build2_loc (loc, code, type, variable, lhs);
8885         }
8886     }
8887
8888   /* For comparisons of pointers we can decompose it to a compile time
8889      comparison of the base objects and the offsets into the object.
8890      This requires at least one operand being an ADDR_EXPR or a
8891      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
8892   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8893       && (TREE_CODE (arg0) == ADDR_EXPR
8894           || TREE_CODE (arg1) == ADDR_EXPR
8895           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8896           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8897     {
8898       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8899       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8900       enum machine_mode mode;
8901       int volatilep, unsignedp;
8902       bool indirect_base0 = false, indirect_base1 = false;
8903
8904       /* Get base and offset for the access.  Strip ADDR_EXPR for
8905          get_inner_reference, but put it back by stripping INDIRECT_REF
8906          off the base object if possible.  indirect_baseN will be true
8907          if baseN is not an address but refers to the object itself.  */
8908       base0 = arg0;
8909       if (TREE_CODE (arg0) == ADDR_EXPR)
8910         {
8911           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8912                                        &bitsize, &bitpos0, &offset0, &mode,
8913                                        &unsignedp, &volatilep, false);
8914           if (TREE_CODE (base0) == INDIRECT_REF)
8915             base0 = TREE_OPERAND (base0, 0);
8916           else
8917             indirect_base0 = true;
8918         }
8919       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8920         {
8921           base0 = TREE_OPERAND (arg0, 0);
8922           STRIP_SIGN_NOPS (base0);
8923           if (TREE_CODE (base0) == ADDR_EXPR)
8924             {
8925               base0 = TREE_OPERAND (base0, 0);
8926               indirect_base0 = true;
8927             }
8928           offset0 = TREE_OPERAND (arg0, 1);
8929           if (host_integerp (offset0, 0))
8930             {
8931               HOST_WIDE_INT off = size_low_cst (offset0);
8932               if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off)
8933                                    * BITS_PER_UNIT)
8934                   / BITS_PER_UNIT == (HOST_WIDE_INT) off)
8935                 {
8936                   bitpos0 = off * BITS_PER_UNIT;
8937                   offset0 = NULL_TREE;
8938                 }
8939             }
8940         }
8941
8942       base1 = arg1;
8943       if (TREE_CODE (arg1) == ADDR_EXPR)
8944         {
8945           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8946                                        &bitsize, &bitpos1, &offset1, &mode,
8947                                        &unsignedp, &volatilep, false);
8948           if (TREE_CODE (base1) == INDIRECT_REF)
8949             base1 = TREE_OPERAND (base1, 0);
8950           else
8951             indirect_base1 = true;
8952         }
8953       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8954         {
8955           base1 = TREE_OPERAND (arg1, 0);
8956           STRIP_SIGN_NOPS (base1);
8957           if (TREE_CODE (base1) == ADDR_EXPR)
8958             {
8959               base1 = TREE_OPERAND (base1, 0);
8960               indirect_base1 = true;
8961             }
8962           offset1 = TREE_OPERAND (arg1, 1);
8963           if (host_integerp (offset1, 0))
8964             {
8965               HOST_WIDE_INT off = size_low_cst (offset1);
8966               if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off)
8967                                    * BITS_PER_UNIT)
8968                   / BITS_PER_UNIT == (HOST_WIDE_INT) off)
8969                 {
8970                   bitpos1 = off * BITS_PER_UNIT;
8971                   offset1 = NULL_TREE;
8972                 }
8973             }
8974         }
8975
8976       /* A local variable can never be pointed to by
8977          the default SSA name of an incoming parameter.  */
8978       if ((TREE_CODE (arg0) == ADDR_EXPR
8979            && indirect_base0
8980            && TREE_CODE (base0) == VAR_DECL
8981            && auto_var_in_fn_p (base0, current_function_decl)
8982            && !indirect_base1
8983            && TREE_CODE (base1) == SSA_NAME
8984            && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL
8985            && SSA_NAME_IS_DEFAULT_DEF (base1))
8986           || (TREE_CODE (arg1) == ADDR_EXPR
8987               && indirect_base1
8988               && TREE_CODE (base1) == VAR_DECL
8989               && auto_var_in_fn_p (base1, current_function_decl)
8990               && !indirect_base0
8991               && TREE_CODE (base0) == SSA_NAME
8992               && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL
8993               && SSA_NAME_IS_DEFAULT_DEF (base0)))
8994         {
8995           if (code == NE_EXPR)
8996             return constant_boolean_node (1, type);
8997           else if (code == EQ_EXPR)
8998             return constant_boolean_node (0, type);
8999         }
9000       /* If we have equivalent bases we might be able to simplify.  */
9001       else if (indirect_base0 == indirect_base1
9002                && operand_equal_p (base0, base1, 0))
9003         {
9004           /* We can fold this expression to a constant if the non-constant
9005              offset parts are equal.  */
9006           if ((offset0 == offset1
9007                || (offset0 && offset1
9008                    && operand_equal_p (offset0, offset1, 0)))
9009               && (code == EQ_EXPR
9010                   || code == NE_EXPR
9011                   || (indirect_base0 && DECL_P (base0))
9012                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
9013
9014             {
9015               if (code != EQ_EXPR
9016                   && code != NE_EXPR
9017                   && bitpos0 != bitpos1
9018                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9019                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9020                 fold_overflow_warning (("assuming pointer wraparound does not "
9021                                         "occur when comparing P +- C1 with "
9022                                         "P +- C2"),
9023                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
9024
9025               switch (code)
9026                 {
9027                 case EQ_EXPR:
9028                   return constant_boolean_node (bitpos0 == bitpos1, type);
9029                 case NE_EXPR:
9030                   return constant_boolean_node (bitpos0 != bitpos1, type);
9031                 case LT_EXPR:
9032                   return constant_boolean_node (bitpos0 < bitpos1, type);
9033                 case LE_EXPR:
9034                   return constant_boolean_node (bitpos0 <= bitpos1, type);
9035                 case GE_EXPR:
9036                   return constant_boolean_node (bitpos0 >= bitpos1, type);
9037                 case GT_EXPR:
9038                   return constant_boolean_node (bitpos0 > bitpos1, type);
9039                 default:;
9040                 }
9041             }
9042           /* We can simplify the comparison to a comparison of the variable
9043              offset parts if the constant offset parts are equal.
9044              Be careful to use signed size type here because otherwise we
9045              mess with array offsets in the wrong way.  This is possible
9046              because pointer arithmetic is restricted to retain within an
9047              object and overflow on pointer differences is undefined as of
9048              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
9049           else if (bitpos0 == bitpos1
9050                    && ((code == EQ_EXPR || code == NE_EXPR)
9051                        || (indirect_base0 && DECL_P (base0))
9052                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
9053             {
9054               /* By converting to signed size type we cover middle-end pointer
9055                  arithmetic which operates on unsigned pointer types of size
9056                  type size and ARRAY_REF offsets which are properly sign or
9057                  zero extended from their type in case it is narrower than
9058                  size type.  */
9059               if (offset0 == NULL_TREE)
9060                 offset0 = build_int_cst (ssizetype, 0);
9061               else
9062                 offset0 = fold_convert_loc (loc, ssizetype, offset0);
9063               if (offset1 == NULL_TREE)
9064                 offset1 = build_int_cst (ssizetype, 0);
9065               else
9066                 offset1 = fold_convert_loc (loc, ssizetype, offset1);
9067
9068               if (code != EQ_EXPR
9069                   && code != NE_EXPR
9070                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9071                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9072                 fold_overflow_warning (("assuming pointer wraparound does not "
9073                                         "occur when comparing P +- C1 with "
9074                                         "P +- C2"),
9075                                        WARN_STRICT_OVERFLOW_COMPARISON);
9076
9077               return fold_build2_loc (loc, code, type, offset0, offset1);
9078             }
9079         }
9080       /* For non-equal bases we can simplify if they are addresses
9081          of local binding decls or constants.  */
9082       else if (indirect_base0 && indirect_base1
9083                /* We know that !operand_equal_p (base0, base1, 0)
9084                   because the if condition was false.  But make
9085                   sure two decls are not the same.  */
9086                && base0 != base1
9087                && TREE_CODE (arg0) == ADDR_EXPR
9088                && TREE_CODE (arg1) == ADDR_EXPR
9089                && (((TREE_CODE (base0) == VAR_DECL
9090                      || TREE_CODE (base0) == PARM_DECL)
9091                     && (targetm.binds_local_p (base0)
9092                         || CONSTANT_CLASS_P (base1)))
9093                    || CONSTANT_CLASS_P (base0))
9094                && (((TREE_CODE (base1) == VAR_DECL
9095                      || TREE_CODE (base1) == PARM_DECL)
9096                     && (targetm.binds_local_p (base1)
9097                         || CONSTANT_CLASS_P (base0)))
9098                    || CONSTANT_CLASS_P (base1)))
9099         {
9100           if (code == EQ_EXPR)
9101             return omit_two_operands_loc (loc, type, boolean_false_node,
9102                                       arg0, arg1);
9103           else if (code == NE_EXPR)
9104             return omit_two_operands_loc (loc, type, boolean_true_node,
9105                                       arg0, arg1);
9106         }
9107       /* For equal offsets we can simplify to a comparison of the
9108          base addresses.  */
9109       else if (bitpos0 == bitpos1
9110                && (indirect_base0
9111                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9112                && (indirect_base1
9113                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9114                && ((offset0 == offset1)
9115                    || (offset0 && offset1
9116                        && operand_equal_p (offset0, offset1, 0))))
9117         {
9118           if (indirect_base0)
9119             base0 = build_fold_addr_expr_loc (loc, base0);
9120           if (indirect_base1)
9121             base1 = build_fold_addr_expr_loc (loc, base1);
9122           return fold_build2_loc (loc, code, type, base0, base1);
9123         }
9124     }
9125
9126   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9127      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
9128      the resulting offset is smaller in absolute value than the
9129      original one.  */
9130   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9131       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9132       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9133           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9134       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9135       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9136           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9137     {
9138       tree const1 = TREE_OPERAND (arg0, 1);
9139       tree const2 = TREE_OPERAND (arg1, 1);
9140       tree variable1 = TREE_OPERAND (arg0, 0);
9141       tree variable2 = TREE_OPERAND (arg1, 0);
9142       tree cst;
9143       const char * const warnmsg = G_("assuming signed overflow does not "
9144                                       "occur when combining constants around "
9145                                       "a comparison");
9146
9147       /* Put the constant on the side where it doesn't overflow and is
9148          of lower absolute value than before.  */
9149       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9150                              ? MINUS_EXPR : PLUS_EXPR,
9151                              const2, const1);
9152       if (!TREE_OVERFLOW (cst)
9153           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
9154         {
9155           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9156           return fold_build2_loc (loc, code, type,
9157                               variable1,
9158                               fold_build2_loc (loc,
9159                                            TREE_CODE (arg1), TREE_TYPE (arg1),
9160                                            variable2, cst));
9161         }
9162
9163       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9164                              ? MINUS_EXPR : PLUS_EXPR,
9165                              const1, const2);
9166       if (!TREE_OVERFLOW (cst)
9167           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
9168         {
9169           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9170           return fold_build2_loc (loc, code, type,
9171                               fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
9172                                            variable1, cst),
9173                               variable2);
9174         }
9175     }
9176
9177   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9178      signed arithmetic case.  That form is created by the compiler
9179      often enough for folding it to be of value.  One example is in
9180      computing loop trip counts after Operator Strength Reduction.  */
9181   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9182       && TREE_CODE (arg0) == MULT_EXPR
9183       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9184           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9185       && integer_zerop (arg1))
9186     {
9187       tree const1 = TREE_OPERAND (arg0, 1);
9188       tree const2 = arg1;                       /* zero */
9189       tree variable1 = TREE_OPERAND (arg0, 0);
9190       enum tree_code cmp_code = code;
9191
9192       /* Handle unfolded multiplication by zero.  */
9193       if (integer_zerop (const1))
9194         return fold_build2_loc (loc, cmp_code, type, const1, const2);
9195
9196       fold_overflow_warning (("assuming signed overflow does not occur when "
9197                               "eliminating multiplication in comparison "
9198                               "with zero"),
9199                              WARN_STRICT_OVERFLOW_COMPARISON);
9200
9201       /* If const1 is negative we swap the sense of the comparison.  */
9202       if (tree_int_cst_sgn (const1) < 0)
9203         cmp_code = swap_tree_comparison (cmp_code);
9204
9205       return fold_build2_loc (loc, cmp_code, type, variable1, const2);
9206     }
9207
9208   tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
9209   if (tem)
9210     return tem;
9211
9212   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9213     {
9214       tree targ0 = strip_float_extensions (arg0);
9215       tree targ1 = strip_float_extensions (arg1);
9216       tree newtype = TREE_TYPE (targ0);
9217
9218       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9219         newtype = TREE_TYPE (targ1);
9220
9221       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
9222       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9223         return fold_build2_loc (loc, code, type,
9224                             fold_convert_loc (loc, newtype, targ0),
9225                             fold_convert_loc (loc, newtype, targ1));
9226
9227       /* (-a) CMP (-b) -> b CMP a  */
9228       if (TREE_CODE (arg0) == NEGATE_EXPR
9229           && TREE_CODE (arg1) == NEGATE_EXPR)
9230         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
9231                             TREE_OPERAND (arg0, 0));
9232
9233       if (TREE_CODE (arg1) == REAL_CST)
9234         {
9235           REAL_VALUE_TYPE cst;
9236           cst = TREE_REAL_CST (arg1);
9237
9238           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
9239           if (TREE_CODE (arg0) == NEGATE_EXPR)
9240             return fold_build2_loc (loc, swap_tree_comparison (code), type,
9241                                 TREE_OPERAND (arg0, 0),
9242                                 build_real (TREE_TYPE (arg1),
9243                                             real_value_negate (&cst)));
9244
9245           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
9246           /* a CMP (-0) -> a CMP 0  */
9247           if (REAL_VALUE_MINUS_ZERO (cst))
9248             return fold_build2_loc (loc, code, type, arg0,
9249                                 build_real (TREE_TYPE (arg1), dconst0));
9250
9251           /* x != NaN is always true, other ops are always false.  */
9252           if (REAL_VALUE_ISNAN (cst)
9253               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9254             {
9255               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9256               return omit_one_operand_loc (loc, type, tem, arg0);
9257             }
9258
9259           /* Fold comparisons against infinity.  */
9260           if (REAL_VALUE_ISINF (cst)
9261               && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9262             {
9263               tem = fold_inf_compare (loc, code, type, arg0, arg1);
9264               if (tem != NULL_TREE)
9265                 return tem;
9266             }
9267         }
9268
9269       /* If this is a comparison of a real constant with a PLUS_EXPR
9270          or a MINUS_EXPR of a real constant, we can convert it into a
9271          comparison with a revised real constant as long as no overflow
9272          occurs when unsafe_math_optimizations are enabled.  */
9273       if (flag_unsafe_math_optimizations
9274           && TREE_CODE (arg1) == REAL_CST
9275           && (TREE_CODE (arg0) == PLUS_EXPR
9276               || TREE_CODE (arg0) == MINUS_EXPR)
9277           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9278           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9279                                       ? MINUS_EXPR : PLUS_EXPR,
9280                                       arg1, TREE_OPERAND (arg0, 1)))
9281           && !TREE_OVERFLOW (tem))
9282         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9283
9284       /* Likewise, we can simplify a comparison of a real constant with
9285          a MINUS_EXPR whose first operand is also a real constant, i.e.
9286          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
9287          floating-point types only if -fassociative-math is set.  */
9288       if (flag_associative_math
9289           && TREE_CODE (arg1) == REAL_CST
9290           && TREE_CODE (arg0) == MINUS_EXPR
9291           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9292           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9293                                       arg1))
9294           && !TREE_OVERFLOW (tem))
9295         return fold_build2_loc (loc, swap_tree_comparison (code), type,
9296                             TREE_OPERAND (arg0, 1), tem);
9297
9298       /* Fold comparisons against built-in math functions.  */
9299       if (TREE_CODE (arg1) == REAL_CST
9300           && flag_unsafe_math_optimizations
9301           && ! flag_errno_math)
9302         {
9303           enum built_in_function fcode = builtin_mathfn_code (arg0);
9304
9305           if (fcode != END_BUILTINS)
9306             {
9307               tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
9308               if (tem != NULL_TREE)
9309                 return tem;
9310             }
9311         }
9312     }
9313
9314   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9315       && CONVERT_EXPR_P (arg0))
9316     {
9317       /* If we are widening one operand of an integer comparison,
9318          see if the other operand is similarly being widened.  Perhaps we
9319          can do the comparison in the narrower type.  */
9320       tem = fold_widened_comparison (loc, code, type, arg0, arg1);
9321       if (tem)
9322         return tem;
9323
9324       /* Or if we are changing signedness.  */
9325       tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
9326       if (tem)
9327         return tem;
9328     }
9329
9330   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9331      constant, we can simplify it.  */
9332   if (TREE_CODE (arg1) == INTEGER_CST
9333       && (TREE_CODE (arg0) == MIN_EXPR
9334           || TREE_CODE (arg0) == MAX_EXPR)
9335       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9336     {
9337       tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9338       if (tem)
9339         return tem;
9340     }
9341
9342   /* Simplify comparison of something with itself.  (For IEEE
9343      floating-point, we can only do some of these simplifications.)  */
9344   if (operand_equal_p (arg0, arg1, 0))
9345     {
9346       switch (code)
9347         {
9348         case EQ_EXPR:
9349           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9350               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9351             return constant_boolean_node (1, type);
9352           break;
9353
9354         case GE_EXPR:
9355         case LE_EXPR:
9356           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9357               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9358             return constant_boolean_node (1, type);
9359           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9360
9361         case NE_EXPR:
9362           /* For NE, we can only do this simplification if integer
9363              or we don't honor IEEE floating point NaNs.  */
9364           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9365               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9366             break;
9367           /* ... fall through ...  */
9368         case GT_EXPR:
9369         case LT_EXPR:
9370           return constant_boolean_node (0, type);
9371         default:
9372           gcc_unreachable ();
9373         }
9374     }
9375
9376   /* If we are comparing an expression that just has comparisons
9377      of two integer values, arithmetic expressions of those comparisons,
9378      and constants, we can simplify it.  There are only three cases
9379      to check: the two values can either be equal, the first can be
9380      greater, or the second can be greater.  Fold the expression for
9381      those three values.  Since each value must be 0 or 1, we have
9382      eight possibilities, each of which corresponds to the constant 0
9383      or 1 or one of the six possible comparisons.
9384
9385      This handles common cases like (a > b) == 0 but also handles
9386      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9387      occur in macroized code.  */
9388
9389   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9390     {
9391       tree cval1 = 0, cval2 = 0;
9392       int save_p = 0;
9393
9394       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9395           /* Don't handle degenerate cases here; they should already
9396              have been handled anyway.  */
9397           && cval1 != 0 && cval2 != 0
9398           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9399           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9400           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9401           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9402           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9403           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9404                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9405         {
9406           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9407           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9408
9409           /* We can't just pass T to eval_subst in case cval1 or cval2
9410              was the same as ARG1.  */
9411
9412           tree high_result
9413                 = fold_build2_loc (loc, code, type,
9414                                eval_subst (loc, arg0, cval1, maxval,
9415                                            cval2, minval),
9416                                arg1);
9417           tree equal_result
9418                 = fold_build2_loc (loc, code, type,
9419                                eval_subst (loc, arg0, cval1, maxval,
9420                                            cval2, maxval),
9421                                arg1);
9422           tree low_result
9423                 = fold_build2_loc (loc, code, type,
9424                                eval_subst (loc, arg0, cval1, minval,
9425                                            cval2, maxval),
9426                                arg1);
9427
9428           /* All three of these results should be 0 or 1.  Confirm they are.
9429              Then use those values to select the proper code to use.  */
9430
9431           if (TREE_CODE (high_result) == INTEGER_CST
9432               && TREE_CODE (equal_result) == INTEGER_CST
9433               && TREE_CODE (low_result) == INTEGER_CST)
9434             {
9435               /* Make a 3-bit mask with the high-order bit being the
9436                  value for `>', the next for '=', and the low for '<'.  */
9437               switch ((integer_onep (high_result) * 4)
9438                       + (integer_onep (equal_result) * 2)
9439                       + integer_onep (low_result))
9440                 {
9441                 case 0:
9442                   /* Always false.  */
9443                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9444                 case 1:
9445                   code = LT_EXPR;
9446                   break;
9447                 case 2:
9448                   code = EQ_EXPR;
9449                   break;
9450                 case 3:
9451                   code = LE_EXPR;
9452                   break;
9453                 case 4:
9454                   code = GT_EXPR;
9455                   break;
9456                 case 5:
9457                   code = NE_EXPR;
9458                   break;
9459                 case 6:
9460                   code = GE_EXPR;
9461                   break;
9462                 case 7:
9463                   /* Always true.  */
9464                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9465                 }
9466
9467               if (save_p)
9468                 {
9469                   tem = save_expr (build2 (code, type, cval1, cval2));
9470                   SET_EXPR_LOCATION (tem, loc);
9471                   return tem;
9472                 }
9473               return fold_build2_loc (loc, code, type, cval1, cval2);
9474             }
9475         }
9476     }
9477
9478   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9479      into a single range test.  */
9480   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9481        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9482       && TREE_CODE (arg1) == INTEGER_CST
9483       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9484       && !integer_zerop (TREE_OPERAND (arg0, 1))
9485       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9486       && !TREE_OVERFLOW (arg1))
9487     {
9488       tem = fold_div_compare (loc, code, type, arg0, arg1);
9489       if (tem != NULL_TREE)
9490         return tem;
9491     }
9492
9493   /* Fold ~X op ~Y as Y op X.  */
9494   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9495       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9496     {
9497       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9498       return fold_build2_loc (loc, code, type,
9499                           fold_convert_loc (loc, cmp_type,
9500                                             TREE_OPERAND (arg1, 0)),
9501                           TREE_OPERAND (arg0, 0));
9502     }
9503
9504   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9505   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9506       && TREE_CODE (arg1) == INTEGER_CST)
9507     {
9508       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9509       return fold_build2_loc (loc, swap_tree_comparison (code), type,
9510                           TREE_OPERAND (arg0, 0),
9511                           fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9512                                        fold_convert_loc (loc, cmp_type, arg1)));
9513     }
9514
9515   return NULL_TREE;
9516 }
9517
9518
9519 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9520    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9521    argument EXPR represents the expression "z" of type TYPE.  */
9522
9523 static tree
9524 fold_mult_zconjz (location_t loc, tree type, tree expr)
9525 {
9526   tree itype = TREE_TYPE (type);
9527   tree rpart, ipart, tem;
9528
9529   if (TREE_CODE (expr) == COMPLEX_EXPR)
9530     {
9531       rpart = TREE_OPERAND (expr, 0);
9532       ipart = TREE_OPERAND (expr, 1);
9533     }
9534   else if (TREE_CODE (expr) == COMPLEX_CST)
9535     {
9536       rpart = TREE_REALPART (expr);
9537       ipart = TREE_IMAGPART (expr);
9538     }
9539   else
9540     {
9541       expr = save_expr (expr);
9542       rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9543       ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9544     }
9545
9546   rpart = save_expr (rpart);
9547   ipart = save_expr (ipart);
9548   tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9549                      fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9550                      fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9551   return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9552                           build_zero_cst (itype));
9553 }
9554
9555
9556 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9557    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9558    guarantees that P and N have the same least significant log2(M) bits.
9559    N is not otherwise constrained.  In particular, N is not normalized to
9560    0 <= N < M as is common.  In general, the precise value of P is unknown.
9561    M is chosen as large as possible such that constant N can be determined.
9562
9563    Returns M and sets *RESIDUE to N.
9564
9565    If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9566    account.  This is not always possible due to PR 35705.
9567  */
9568
9569 static unsigned HOST_WIDE_INT
9570 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9571                                  bool allow_func_align)
9572 {
9573   enum tree_code code;
9574
9575   *residue = 0;
9576
9577   code = TREE_CODE (expr);
9578   if (code == ADDR_EXPR)
9579     {
9580       unsigned int bitalign;
9581       bitalign = get_object_alignment_1 (TREE_OPERAND (expr, 0), residue);
9582       *residue /= BITS_PER_UNIT;
9583       return bitalign / BITS_PER_UNIT;
9584     }
9585   else if (code == POINTER_PLUS_EXPR)
9586     {
9587       tree op0, op1;
9588       unsigned HOST_WIDE_INT modulus;
9589       enum tree_code inner_code;
9590
9591       op0 = TREE_OPERAND (expr, 0);
9592       STRIP_NOPS (op0);
9593       modulus = get_pointer_modulus_and_residue (op0, residue,
9594                                                  allow_func_align);
9595
9596       op1 = TREE_OPERAND (expr, 1);
9597       STRIP_NOPS (op1);
9598       inner_code = TREE_CODE (op1);
9599       if (inner_code == INTEGER_CST)
9600         {
9601           *residue += TREE_INT_CST_LOW (op1);
9602           return modulus;
9603         }
9604       else if (inner_code == MULT_EXPR)
9605         {
9606           op1 = TREE_OPERAND (op1, 1);
9607           if (TREE_CODE (op1) == INTEGER_CST)
9608             {
9609               unsigned HOST_WIDE_INT align;
9610
9611               /* Compute the greatest power-of-2 divisor of op1.  */
9612               align = TREE_INT_CST_LOW (op1);
9613               align &= -align;
9614
9615               /* If align is non-zero and less than *modulus, replace
9616                  *modulus with align., If align is 0, then either op1 is 0
9617                  or the greatest power-of-2 divisor of op1 doesn't fit in an
9618                  unsigned HOST_WIDE_INT.  In either case, no additional
9619                  constraint is imposed.  */
9620               if (align)
9621                 modulus = MIN (modulus, align);
9622
9623               return modulus;
9624             }
9625         }
9626     }
9627
9628   /* If we get here, we were unable to determine anything useful about the
9629      expression.  */
9630   return 1;
9631 }
9632
9633 /* Helper function for fold_vec_perm.  Store elements of VECTOR_CST or
9634    CONSTRUCTOR ARG into array ELTS and return true if successful.  */
9635
9636 static bool
9637 vec_cst_ctor_to_array (tree arg, tree *elts)
9638 {
9639   unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
9640
9641   if (TREE_CODE (arg) == VECTOR_CST)
9642     {
9643       tree t;
9644
9645       for (i = 0, t = TREE_VECTOR_CST_ELTS (arg);
9646            i < nelts && t; i++, t = TREE_CHAIN (t))
9647         elts[i] = TREE_VALUE (t);
9648       if (t)
9649         return false;
9650     }
9651   else if (TREE_CODE (arg) == CONSTRUCTOR)
9652     {
9653       constructor_elt *elt;
9654
9655       FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (arg), i, elt)
9656         if (i >= nelts)
9657           return false;
9658         else
9659           elts[i] = elt->value;
9660     }
9661   else
9662     return false;
9663   for (; i < nelts; i++)
9664     elts[i]
9665       = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
9666   return true;
9667 }
9668
9669 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
9670    selector.  Return the folded VECTOR_CST or CONSTRUCTOR if successful,
9671    NULL_TREE otherwise.  */
9672
9673 static tree
9674 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
9675 {
9676   unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
9677   tree *elts;
9678   bool need_ctor = false;
9679
9680   gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
9681               && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
9682   if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
9683       || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
9684     return NULL_TREE;
9685
9686   elts = XALLOCAVEC (tree, nelts * 3);
9687   if (!vec_cst_ctor_to_array (arg0, elts)
9688       || !vec_cst_ctor_to_array (arg1, elts + nelts))
9689     return NULL_TREE;
9690
9691   for (i = 0; i < nelts; i++)
9692     {
9693       if (!CONSTANT_CLASS_P (elts[sel[i]]))
9694         need_ctor = true;
9695       elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
9696     }
9697
9698   if (need_ctor)
9699     {
9700       VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, nelts);
9701       for (i = 0; i < nelts; i++)
9702         CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
9703       return build_constructor (type, v);
9704     }
9705   else
9706     {
9707       tree vals = NULL_TREE;
9708       for (i = 0; i < nelts; i++)
9709         vals = tree_cons (NULL_TREE, elts[3 * nelts - i - 1], vals);
9710       return build_vector (type, vals);
9711     }
9712 }
9713
9714 /* Try to fold a pointer difference of type TYPE two address expressions of
9715    array references AREF0 and AREF1 using location LOC.  Return a
9716    simplified expression for the difference or NULL_TREE.  */
9717
9718 static tree
9719 fold_addr_of_array_ref_difference (location_t loc, tree type,
9720                                    tree aref0, tree aref1)
9721 {
9722   tree base0 = TREE_OPERAND (aref0, 0);
9723   tree base1 = TREE_OPERAND (aref1, 0);
9724   tree base_offset = build_int_cst (type, 0);
9725
9726   /* If the bases are array references as well, recurse.  If the bases
9727      are pointer indirections compute the difference of the pointers.
9728      If the bases are equal, we are set.  */
9729   if ((TREE_CODE (base0) == ARRAY_REF
9730        && TREE_CODE (base1) == ARRAY_REF
9731        && (base_offset
9732            = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
9733       || (INDIRECT_REF_P (base0)
9734           && INDIRECT_REF_P (base1)
9735           && (base_offset = fold_binary_loc (loc, MINUS_EXPR, type,
9736                                              TREE_OPERAND (base0, 0),
9737                                              TREE_OPERAND (base1, 0))))
9738       || operand_equal_p (base0, base1, 0))
9739     {
9740       tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
9741       tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
9742       tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
9743       tree diff = build2 (MINUS_EXPR, type, op0, op1);
9744       return fold_build2_loc (loc, PLUS_EXPR, type,
9745                               base_offset,
9746                               fold_build2_loc (loc, MULT_EXPR, type,
9747                                                diff, esz));
9748     }
9749   return NULL_TREE;
9750 }
9751
9752 /* Fold a binary expression of code CODE and type TYPE with operands
9753    OP0 and OP1.  LOC is the location of the resulting expression.
9754    Return the folded expression if folding is successful.  Otherwise,
9755    return NULL_TREE.  */
9756
9757 tree
9758 fold_binary_loc (location_t loc,
9759              enum tree_code code, tree type, tree op0, tree op1)
9760 {
9761   enum tree_code_class kind = TREE_CODE_CLASS (code);
9762   tree arg0, arg1, tem;
9763   tree t1 = NULL_TREE;
9764   bool strict_overflow_p;
9765
9766   gcc_assert (IS_EXPR_CODE_CLASS (kind)
9767               && TREE_CODE_LENGTH (code) == 2
9768               && op0 != NULL_TREE
9769               && op1 != NULL_TREE);
9770
9771   arg0 = op0;
9772   arg1 = op1;
9773
9774   /* Strip any conversions that don't change the mode.  This is
9775      safe for every expression, except for a comparison expression
9776      because its signedness is derived from its operands.  So, in
9777      the latter case, only strip conversions that don't change the
9778      signedness.  MIN_EXPR/MAX_EXPR also need signedness of arguments
9779      preserved.
9780
9781      Note that this is done as an internal manipulation within the
9782      constant folder, in order to find the simplest representation
9783      of the arguments so that their form can be studied.  In any
9784      cases, the appropriate type conversions should be put back in
9785      the tree that will get out of the constant folder.  */
9786
9787   if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9788     {
9789       STRIP_SIGN_NOPS (arg0);
9790       STRIP_SIGN_NOPS (arg1);
9791     }
9792   else
9793     {
9794       STRIP_NOPS (arg0);
9795       STRIP_NOPS (arg1);
9796     }
9797
9798   /* Note that TREE_CONSTANT isn't enough: static var addresses are
9799      constant but we can't do arithmetic on them.  */
9800   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9801       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9802       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9803       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9804       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9805       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9806     {
9807       if (kind == tcc_binary)
9808         {
9809           /* Make sure type and arg0 have the same saturating flag.  */
9810           gcc_assert (TYPE_SATURATING (type)
9811                       == TYPE_SATURATING (TREE_TYPE (arg0)));
9812           tem = const_binop (code, arg0, arg1);
9813         }
9814       else if (kind == tcc_comparison)
9815         tem = fold_relational_const (code, type, arg0, arg1);
9816       else
9817         tem = NULL_TREE;
9818
9819       if (tem != NULL_TREE)
9820         {
9821           if (TREE_TYPE (tem) != type)
9822             tem = fold_convert_loc (loc, type, tem);
9823           return tem;
9824         }
9825     }
9826
9827   /* If this is a commutative operation, and ARG0 is a constant, move it
9828      to ARG1 to reduce the number of tests below.  */
9829   if (commutative_tree_code (code)
9830       && tree_swap_operands_p (arg0, arg1, true))
9831     return fold_build2_loc (loc, code, type, op1, op0);
9832
9833   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9834
9835      First check for cases where an arithmetic operation is applied to a
9836      compound, conditional, or comparison operation.  Push the arithmetic
9837      operation inside the compound or conditional to see if any folding
9838      can then be done.  Convert comparison to conditional for this purpose.
9839      The also optimizes non-constant cases that used to be done in
9840      expand_expr.
9841
9842      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9843      one of the operands is a comparison and the other is a comparison, a
9844      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
9845      code below would make the expression more complex.  Change it to a
9846      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
9847      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
9848
9849   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9850        || code == EQ_EXPR || code == NE_EXPR)
9851       && ((truth_value_p (TREE_CODE (arg0))
9852            && (truth_value_p (TREE_CODE (arg1))
9853                || (TREE_CODE (arg1) == BIT_AND_EXPR
9854                    && integer_onep (TREE_OPERAND (arg1, 1)))))
9855           || (truth_value_p (TREE_CODE (arg1))
9856               && (truth_value_p (TREE_CODE (arg0))
9857                   || (TREE_CODE (arg0) == BIT_AND_EXPR
9858                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
9859     {
9860       tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9861                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9862                          : TRUTH_XOR_EXPR,
9863                          boolean_type_node,
9864                          fold_convert_loc (loc, boolean_type_node, arg0),
9865                          fold_convert_loc (loc, boolean_type_node, arg1));
9866
9867       if (code == EQ_EXPR)
9868         tem = invert_truthvalue_loc (loc, tem);
9869
9870       return fold_convert_loc (loc, type, tem);
9871     }
9872
9873   if (TREE_CODE_CLASS (code) == tcc_binary
9874       || TREE_CODE_CLASS (code) == tcc_comparison)
9875     {
9876       if (TREE_CODE (arg0) == COMPOUND_EXPR)
9877         {
9878           tem = fold_build2_loc (loc, code, type,
9879                              fold_convert_loc (loc, TREE_TYPE (op0),
9880                                                TREE_OPERAND (arg0, 1)), op1);
9881           return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9882                              tem);
9883         }
9884       if (TREE_CODE (arg1) == COMPOUND_EXPR
9885           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9886         {
9887           tem = fold_build2_loc (loc, code, type, op0,
9888                              fold_convert_loc (loc, TREE_TYPE (op1),
9889                                                TREE_OPERAND (arg1, 1)));
9890           return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9891                              tem);
9892         }
9893
9894       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9895         {
9896           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9897                                                      arg0, arg1,
9898                                                      /*cond_first_p=*/1);
9899           if (tem != NULL_TREE)
9900             return tem;
9901         }
9902
9903       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9904         {
9905           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9906                                                      arg1, arg0,
9907                                                      /*cond_first_p=*/0);
9908           if (tem != NULL_TREE)
9909             return tem;
9910         }
9911     }
9912
9913   switch (code)
9914     {
9915     case MEM_REF:
9916       /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2].  */
9917       if (TREE_CODE (arg0) == ADDR_EXPR
9918           && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9919         {
9920           tree iref = TREE_OPERAND (arg0, 0);
9921           return fold_build2 (MEM_REF, type,
9922                               TREE_OPERAND (iref, 0),
9923                               int_const_binop (PLUS_EXPR, arg1,
9924                                                TREE_OPERAND (iref, 1)));
9925         }
9926
9927       /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2].  */
9928       if (TREE_CODE (arg0) == ADDR_EXPR
9929           && handled_component_p (TREE_OPERAND (arg0, 0)))
9930         {
9931           tree base;
9932           HOST_WIDE_INT coffset;
9933           base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9934                                                 &coffset);
9935           if (!base)
9936             return NULL_TREE;
9937           return fold_build2 (MEM_REF, type,
9938                               build_fold_addr_expr (base),
9939                               int_const_binop (PLUS_EXPR, arg1,
9940                                                size_int (coffset)));
9941         }
9942
9943       return NULL_TREE;
9944
9945     case POINTER_PLUS_EXPR:
9946       /* 0 +p index -> (type)index */
9947       if (integer_zerop (arg0))
9948         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9949
9950       /* PTR +p 0 -> PTR */
9951       if (integer_zerop (arg1))
9952         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9953
9954       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
9955       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9956            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9957         return fold_convert_loc (loc, type,
9958                                  fold_build2_loc (loc, PLUS_EXPR, sizetype,
9959                                               fold_convert_loc (loc, sizetype,
9960                                                                 arg1),
9961                                               fold_convert_loc (loc, sizetype,
9962                                                                 arg0)));
9963
9964       /* (PTR +p B) +p A -> PTR +p (B + A) */
9965       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9966         {
9967           tree inner;
9968           tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
9969           tree arg00 = TREE_OPERAND (arg0, 0);
9970           inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
9971                                arg01, fold_convert_loc (loc, sizetype, arg1));
9972           return fold_convert_loc (loc, type,
9973                                    fold_build_pointer_plus_loc (loc,
9974                                                                 arg00, inner));
9975         }
9976
9977       /* PTR_CST +p CST -> CST1 */
9978       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9979         return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
9980                             fold_convert_loc (loc, type, arg1));
9981
9982      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9983         of the array.  Loop optimizer sometimes produce this type of
9984         expressions.  */
9985       if (TREE_CODE (arg0) == ADDR_EXPR)
9986         {
9987           tem = try_move_mult_to_index (loc, arg0,
9988                                         fold_convert_loc (loc, sizetype, arg1));
9989           if (tem)
9990             return fold_convert_loc (loc, type, tem);
9991         }
9992
9993       return NULL_TREE;
9994
9995     case PLUS_EXPR:
9996       /* A + (-B) -> A - B */
9997       if (TREE_CODE (arg1) == NEGATE_EXPR)
9998         return fold_build2_loc (loc, MINUS_EXPR, type,
9999                             fold_convert_loc (loc, type, arg0),
10000                             fold_convert_loc (loc, type,
10001                                               TREE_OPERAND (arg1, 0)));
10002       /* (-A) + B -> B - A */
10003       if (TREE_CODE (arg0) == NEGATE_EXPR
10004           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
10005         return fold_build2_loc (loc, MINUS_EXPR, type,
10006                             fold_convert_loc (loc, type, arg1),
10007                             fold_convert_loc (loc, type,
10008                                               TREE_OPERAND (arg0, 0)));
10009
10010       if (INTEGRAL_TYPE_P (type))
10011         {
10012           /* Convert ~A + 1 to -A.  */
10013           if (TREE_CODE (arg0) == BIT_NOT_EXPR
10014               && integer_onep (arg1))
10015             return fold_build1_loc (loc, NEGATE_EXPR, type,
10016                                 fold_convert_loc (loc, type,
10017                                                   TREE_OPERAND (arg0, 0)));
10018
10019           /* ~X + X is -1.  */
10020           if (TREE_CODE (arg0) == BIT_NOT_EXPR
10021               && !TYPE_OVERFLOW_TRAPS (type))
10022             {
10023               tree tem = TREE_OPERAND (arg0, 0);
10024
10025               STRIP_NOPS (tem);
10026               if (operand_equal_p (tem, arg1, 0))
10027                 {
10028                   t1 = build_int_cst_type (type, -1);
10029                   return omit_one_operand_loc (loc, type, t1, arg1);
10030                 }
10031             }
10032
10033           /* X + ~X is -1.  */
10034           if (TREE_CODE (arg1) == BIT_NOT_EXPR
10035               && !TYPE_OVERFLOW_TRAPS (type))
10036             {
10037               tree tem = TREE_OPERAND (arg1, 0);
10038
10039               STRIP_NOPS (tem);
10040               if (operand_equal_p (arg0, tem, 0))
10041                 {
10042                   t1 = build_int_cst_type (type, -1);
10043                   return omit_one_operand_loc (loc, type, t1, arg0);
10044                 }
10045             }
10046
10047           /* X + (X / CST) * -CST is X % CST.  */
10048           if (TREE_CODE (arg1) == MULT_EXPR
10049               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10050               && operand_equal_p (arg0,
10051                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
10052             {
10053               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
10054               tree cst1 = TREE_OPERAND (arg1, 1);
10055               tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
10056                                       cst1, cst0);
10057               if (sum && integer_zerop (sum))
10058                 return fold_convert_loc (loc, type,
10059                                          fold_build2_loc (loc, TRUNC_MOD_EXPR,
10060                                                       TREE_TYPE (arg0), arg0,
10061                                                       cst0));
10062             }
10063         }
10064
10065       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
10066          one.  Make sure the type is not saturating and has the signedness of
10067          the stripped operands, as fold_plusminus_mult_expr will re-associate.
10068          ??? The latter condition should use TYPE_OVERFLOW_* flags instead.  */
10069       if ((TREE_CODE (arg0) == MULT_EXPR
10070            || TREE_CODE (arg1) == MULT_EXPR)
10071           && !TYPE_SATURATING (type)
10072           && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
10073           && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
10074           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10075         {
10076           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10077           if (tem)
10078             return tem;
10079         }
10080
10081       if (! FLOAT_TYPE_P (type))
10082         {
10083           if (integer_zerop (arg1))
10084             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10085
10086           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
10087              with a constant, and the two constants have no bits in common,
10088              we should treat this as a BIT_IOR_EXPR since this may produce more
10089              simplifications.  */
10090           if (TREE_CODE (arg0) == BIT_AND_EXPR
10091               && TREE_CODE (arg1) == BIT_AND_EXPR
10092               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10093               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10094               && integer_zerop (const_binop (BIT_AND_EXPR,
10095                                              TREE_OPERAND (arg0, 1),
10096                                              TREE_OPERAND (arg1, 1))))
10097             {
10098               code = BIT_IOR_EXPR;
10099               goto bit_ior;
10100             }
10101
10102           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
10103              (plus (plus (mult) (mult)) (foo)) so that we can
10104              take advantage of the factoring cases below.  */
10105           if (TYPE_OVERFLOW_WRAPS (type)
10106               && (((TREE_CODE (arg0) == PLUS_EXPR
10107                     || TREE_CODE (arg0) == MINUS_EXPR)
10108                    && TREE_CODE (arg1) == MULT_EXPR)
10109                   || ((TREE_CODE (arg1) == PLUS_EXPR
10110                        || TREE_CODE (arg1) == MINUS_EXPR)
10111                       && TREE_CODE (arg0) == MULT_EXPR)))
10112             {
10113               tree parg0, parg1, parg, marg;
10114               enum tree_code pcode;
10115
10116               if (TREE_CODE (arg1) == MULT_EXPR)
10117                 parg = arg0, marg = arg1;
10118               else
10119                 parg = arg1, marg = arg0;
10120               pcode = TREE_CODE (parg);
10121               parg0 = TREE_OPERAND (parg, 0);
10122               parg1 = TREE_OPERAND (parg, 1);
10123               STRIP_NOPS (parg0);
10124               STRIP_NOPS (parg1);
10125
10126               if (TREE_CODE (parg0) == MULT_EXPR
10127                   && TREE_CODE (parg1) != MULT_EXPR)
10128                 return fold_build2_loc (loc, pcode, type,
10129                                     fold_build2_loc (loc, PLUS_EXPR, type,
10130                                                  fold_convert_loc (loc, type,
10131                                                                    parg0),
10132                                                  fold_convert_loc (loc, type,
10133                                                                    marg)),
10134                                     fold_convert_loc (loc, type, parg1));
10135               if (TREE_CODE (parg0) != MULT_EXPR
10136                   && TREE_CODE (parg1) == MULT_EXPR)
10137                 return
10138                   fold_build2_loc (loc, PLUS_EXPR, type,
10139                                fold_convert_loc (loc, type, parg0),
10140                                fold_build2_loc (loc, pcode, type,
10141                                             fold_convert_loc (loc, type, marg),
10142                                             fold_convert_loc (loc, type,
10143                                                               parg1)));
10144             }
10145         }
10146       else
10147         {
10148           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
10149           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
10150             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10151
10152           /* Likewise if the operands are reversed.  */
10153           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10154             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10155
10156           /* Convert X + -C into X - C.  */
10157           if (TREE_CODE (arg1) == REAL_CST
10158               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
10159             {
10160               tem = fold_negate_const (arg1, type);
10161               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
10162                 return fold_build2_loc (loc, MINUS_EXPR, type,
10163                                     fold_convert_loc (loc, type, arg0),
10164                                     fold_convert_loc (loc, type, tem));
10165             }
10166
10167           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10168              to __complex__ ( x, y ).  This is not the same for SNaNs or
10169              if signed zeros are involved.  */
10170           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10171               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10172               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10173             {
10174               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10175               tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10176               tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10177               bool arg0rz = false, arg0iz = false;
10178               if ((arg0r && (arg0rz = real_zerop (arg0r)))
10179                   || (arg0i && (arg0iz = real_zerop (arg0i))))
10180                 {
10181                   tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10182                   tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10183                   if (arg0rz && arg1i && real_zerop (arg1i))
10184                     {
10185                       tree rp = arg1r ? arg1r
10186                                   : build1 (REALPART_EXPR, rtype, arg1);
10187                       tree ip = arg0i ? arg0i
10188                                   : build1 (IMAGPART_EXPR, rtype, arg0);
10189                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10190                     }
10191                   else if (arg0iz && arg1r && real_zerop (arg1r))
10192                     {
10193                       tree rp = arg0r ? arg0r
10194                                   : build1 (REALPART_EXPR, rtype, arg0);
10195                       tree ip = arg1i ? arg1i
10196                                   : build1 (IMAGPART_EXPR, rtype, arg1);
10197                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10198                     }
10199                 }
10200             }
10201
10202           if (flag_unsafe_math_optimizations
10203               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10204               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10205               && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10206             return tem;
10207
10208           /* Convert x+x into x*2.0.  */
10209           if (operand_equal_p (arg0, arg1, 0)
10210               && SCALAR_FLOAT_TYPE_P (type))
10211             return fold_build2_loc (loc, MULT_EXPR, type, arg0,
10212                                 build_real (type, dconst2));
10213
10214           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
10215              We associate floats only if the user has specified
10216              -fassociative-math.  */
10217           if (flag_associative_math
10218               && TREE_CODE (arg1) == PLUS_EXPR
10219               && TREE_CODE (arg0) != MULT_EXPR)
10220             {
10221               tree tree10 = TREE_OPERAND (arg1, 0);
10222               tree tree11 = TREE_OPERAND (arg1, 1);
10223               if (TREE_CODE (tree11) == MULT_EXPR
10224                   && TREE_CODE (tree10) == MULT_EXPR)
10225                 {
10226                   tree tree0;
10227                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
10228                   return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
10229                 }
10230             }
10231           /* Convert (b*c + d*e) + a into b*c + (d*e +a).
10232              We associate floats only if the user has specified
10233              -fassociative-math.  */
10234           if (flag_associative_math
10235               && TREE_CODE (arg0) == PLUS_EXPR
10236               && TREE_CODE (arg1) != MULT_EXPR)
10237             {
10238               tree tree00 = TREE_OPERAND (arg0, 0);
10239               tree tree01 = TREE_OPERAND (arg0, 1);
10240               if (TREE_CODE (tree01) == MULT_EXPR
10241                   && TREE_CODE (tree00) == MULT_EXPR)
10242                 {
10243                   tree tree0;
10244                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
10245                   return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
10246                 }
10247             }
10248         }
10249
10250      bit_rotate:
10251       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10252          is a rotate of A by C1 bits.  */
10253       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10254          is a rotate of A by B bits.  */
10255       {
10256         enum tree_code code0, code1;
10257         tree rtype;
10258         code0 = TREE_CODE (arg0);
10259         code1 = TREE_CODE (arg1);
10260         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10261              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10262             && operand_equal_p (TREE_OPERAND (arg0, 0),
10263                                 TREE_OPERAND (arg1, 0), 0)
10264             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10265                 TYPE_UNSIGNED (rtype))
10266             /* Only create rotates in complete modes.  Other cases are not
10267                expanded properly.  */
10268             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
10269           {
10270             tree tree01, tree11;
10271             enum tree_code code01, code11;
10272
10273             tree01 = TREE_OPERAND (arg0, 1);
10274             tree11 = TREE_OPERAND (arg1, 1);
10275             STRIP_NOPS (tree01);
10276             STRIP_NOPS (tree11);
10277             code01 = TREE_CODE (tree01);
10278             code11 = TREE_CODE (tree11);
10279             if (code01 == INTEGER_CST
10280                 && code11 == INTEGER_CST
10281                 && TREE_INT_CST_HIGH (tree01) == 0
10282                 && TREE_INT_CST_HIGH (tree11) == 0
10283                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
10284                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10285               {
10286                 tem = build2_loc (loc, LROTATE_EXPR,
10287                                   TREE_TYPE (TREE_OPERAND (arg0, 0)),
10288                                   TREE_OPERAND (arg0, 0),
10289                                   code0 == LSHIFT_EXPR ? tree01 : tree11);
10290                 return fold_convert_loc (loc, type, tem);
10291               }
10292             else if (code11 == MINUS_EXPR)
10293               {
10294                 tree tree110, tree111;
10295                 tree110 = TREE_OPERAND (tree11, 0);
10296                 tree111 = TREE_OPERAND (tree11, 1);
10297                 STRIP_NOPS (tree110);
10298                 STRIP_NOPS (tree111);
10299                 if (TREE_CODE (tree110) == INTEGER_CST
10300                     && 0 == compare_tree_int (tree110,
10301                                               TYPE_PRECISION
10302                                               (TREE_TYPE (TREE_OPERAND
10303                                                           (arg0, 0))))
10304                     && operand_equal_p (tree01, tree111, 0))
10305                   return
10306                     fold_convert_loc (loc, type,
10307                                       build2 ((code0 == LSHIFT_EXPR
10308                                                ? LROTATE_EXPR
10309                                                : RROTATE_EXPR),
10310                                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
10311                                               TREE_OPERAND (arg0, 0), tree01));
10312               }
10313             else if (code01 == MINUS_EXPR)
10314               {
10315                 tree tree010, tree011;
10316                 tree010 = TREE_OPERAND (tree01, 0);
10317                 tree011 = TREE_OPERAND (tree01, 1);
10318                 STRIP_NOPS (tree010);
10319                 STRIP_NOPS (tree011);
10320                 if (TREE_CODE (tree010) == INTEGER_CST
10321                     && 0 == compare_tree_int (tree010,
10322                                               TYPE_PRECISION
10323                                               (TREE_TYPE (TREE_OPERAND
10324                                                           (arg0, 0))))
10325                     && operand_equal_p (tree11, tree011, 0))
10326                     return fold_convert_loc
10327                       (loc, type,
10328                        build2 ((code0 != LSHIFT_EXPR
10329                                 ? LROTATE_EXPR
10330                                 : RROTATE_EXPR),
10331                                TREE_TYPE (TREE_OPERAND (arg0, 0)),
10332                                TREE_OPERAND (arg0, 0), tree11));
10333               }
10334           }
10335       }
10336
10337     associate:
10338       /* In most languages, can't associate operations on floats through
10339          parentheses.  Rather than remember where the parentheses were, we
10340          don't associate floats at all, unless the user has specified
10341          -fassociative-math.
10342          And, we need to make sure type is not saturating.  */
10343
10344       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10345           && !TYPE_SATURATING (type))
10346         {
10347           tree var0, con0, lit0, minus_lit0;
10348           tree var1, con1, lit1, minus_lit1;
10349           bool ok = true;
10350
10351           /* Split both trees into variables, constants, and literals.  Then
10352              associate each group together, the constants with literals,
10353              then the result with variables.  This increases the chances of
10354              literals being recombined later and of generating relocatable
10355              expressions for the sum of a constant and literal.  */
10356           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10357           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10358                              code == MINUS_EXPR);
10359
10360           /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
10361           if (code == MINUS_EXPR)
10362             code = PLUS_EXPR;
10363
10364           /* With undefined overflow we can only associate constants with one
10365              variable, and constants whose association doesn't overflow.  */
10366           if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10367               || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10368             {
10369               if (var0 && var1)
10370                 {
10371                   tree tmp0 = var0;
10372                   tree tmp1 = var1;
10373
10374                   if (TREE_CODE (tmp0) == NEGATE_EXPR)
10375                     tmp0 = TREE_OPERAND (tmp0, 0);
10376                   if (TREE_CODE (tmp1) == NEGATE_EXPR)
10377                     tmp1 = TREE_OPERAND (tmp1, 0);
10378                   /* The only case we can still associate with two variables
10379                      is if they are the same, modulo negation.  */
10380                   if (!operand_equal_p (tmp0, tmp1, 0))
10381                     ok = false;
10382                 }
10383
10384               if (ok && lit0 && lit1)
10385                 {
10386                   tree tmp0 = fold_convert (type, lit0);
10387                   tree tmp1 = fold_convert (type, lit1);
10388
10389                   if (!TREE_OVERFLOW (tmp0) && !TREE_OVERFLOW (tmp1)
10390                       && TREE_OVERFLOW (fold_build2 (code, type, tmp0, tmp1)))
10391                     ok = false;
10392                 }
10393             }
10394
10395           /* Only do something if we found more than two objects.  Otherwise,
10396              nothing has changed and we risk infinite recursion.  */
10397           if (ok
10398               && (2 < ((var0 != 0) + (var1 != 0)
10399                        + (con0 != 0) + (con1 != 0)
10400                        + (lit0 != 0) + (lit1 != 0)
10401                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
10402             {
10403               var0 = associate_trees (loc, var0, var1, code, type);
10404               con0 = associate_trees (loc, con0, con1, code, type);
10405               lit0 = associate_trees (loc, lit0, lit1, code, type);
10406               minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type);
10407
10408               /* Preserve the MINUS_EXPR if the negative part of the literal is
10409                  greater than the positive part.  Otherwise, the multiplicative
10410                  folding code (i.e extract_muldiv) may be fooled in case
10411                  unsigned constants are subtracted, like in the following
10412                  example: ((X*2 + 4) - 8U)/2.  */
10413               if (minus_lit0 && lit0)
10414                 {
10415                   if (TREE_CODE (lit0) == INTEGER_CST
10416                       && TREE_CODE (minus_lit0) == INTEGER_CST
10417                       && tree_int_cst_lt (lit0, minus_lit0))
10418                     {
10419                       minus_lit0 = associate_trees (loc, minus_lit0, lit0,
10420                                                     MINUS_EXPR, type);
10421                       lit0 = 0;
10422                     }
10423                   else
10424                     {
10425                       lit0 = associate_trees (loc, lit0, minus_lit0,
10426                                               MINUS_EXPR, type);
10427                       minus_lit0 = 0;
10428                     }
10429                 }
10430               if (minus_lit0)
10431                 {
10432                   if (con0 == 0)
10433                     return
10434                       fold_convert_loc (loc, type,
10435                                         associate_trees (loc, var0, minus_lit0,
10436                                                          MINUS_EXPR, type));
10437                   else
10438                     {
10439                       con0 = associate_trees (loc, con0, minus_lit0,
10440                                               MINUS_EXPR, type);
10441                       return
10442                         fold_convert_loc (loc, type,
10443                                           associate_trees (loc, var0, con0,
10444                                                            PLUS_EXPR, type));
10445                     }
10446                 }
10447
10448               con0 = associate_trees (loc, con0, lit0, code, type);
10449               return
10450                 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10451                                                               code, type));
10452             }
10453         }
10454
10455       return NULL_TREE;
10456
10457     case MINUS_EXPR:
10458       /* Pointer simplifications for subtraction, simple reassociations. */
10459       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10460         {
10461           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10462           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10463               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10464             {
10465               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10466               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10467               tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10468               tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10469               return fold_build2_loc (loc, PLUS_EXPR, type,
10470                                   fold_build2_loc (loc, MINUS_EXPR, type,
10471                                                arg00, arg10),
10472                                   fold_build2_loc (loc, MINUS_EXPR, type,
10473                                                arg01, arg11));
10474             }
10475           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10476           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10477             {
10478               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10479               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10480               tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10481                                       fold_convert_loc (loc, type, arg1));
10482               if (tmp)
10483                 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10484             }
10485         }
10486       /* A - (-B) -> A + B */
10487       if (TREE_CODE (arg1) == NEGATE_EXPR)
10488         return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10489                             fold_convert_loc (loc, type,
10490                                               TREE_OPERAND (arg1, 0)));
10491       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10492       if (TREE_CODE (arg0) == NEGATE_EXPR
10493           && (FLOAT_TYPE_P (type)
10494               || INTEGRAL_TYPE_P (type))
10495           && negate_expr_p (arg1)
10496           && reorder_operands_p (arg0, arg1))
10497         return fold_build2_loc (loc, MINUS_EXPR, type,
10498                             fold_convert_loc (loc, type,
10499                                               negate_expr (arg1)),
10500                             fold_convert_loc (loc, type,
10501                                               TREE_OPERAND (arg0, 0)));
10502       /* Convert -A - 1 to ~A.  */
10503       if (INTEGRAL_TYPE_P (type)
10504           && TREE_CODE (arg0) == NEGATE_EXPR
10505           && integer_onep (arg1)
10506           && !TYPE_OVERFLOW_TRAPS (type))
10507         return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10508                             fold_convert_loc (loc, type,
10509                                               TREE_OPERAND (arg0, 0)));
10510
10511       /* Convert -1 - A to ~A.  */
10512       if (INTEGRAL_TYPE_P (type)
10513           && integer_all_onesp (arg0))
10514         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10515
10516
10517       /* X - (X / CST) * CST is X % CST.  */
10518       if (INTEGRAL_TYPE_P (type)
10519           && TREE_CODE (arg1) == MULT_EXPR
10520           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10521           && operand_equal_p (arg0,
10522                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10523           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10524                               TREE_OPERAND (arg1, 1), 0))
10525         return
10526           fold_convert_loc (loc, type,
10527                             fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10528                                          arg0, TREE_OPERAND (arg1, 1)));
10529
10530       if (! FLOAT_TYPE_P (type))
10531         {
10532           if (integer_zerop (arg0))
10533             return negate_expr (fold_convert_loc (loc, type, arg1));
10534           if (integer_zerop (arg1))
10535             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10536
10537           /* Fold A - (A & B) into ~B & A.  */
10538           if (!TREE_SIDE_EFFECTS (arg0)
10539               && TREE_CODE (arg1) == BIT_AND_EXPR)
10540             {
10541               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10542                 {
10543                   tree arg10 = fold_convert_loc (loc, type,
10544                                                  TREE_OPERAND (arg1, 0));
10545                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10546                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10547                                                    type, arg10),
10548                                       fold_convert_loc (loc, type, arg0));
10549                 }
10550               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10551                 {
10552                   tree arg11 = fold_convert_loc (loc,
10553                                                  type, TREE_OPERAND (arg1, 1));
10554                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10555                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10556                                                    type, arg11),
10557                                       fold_convert_loc (loc, type, arg0));
10558                 }
10559             }
10560
10561           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10562              any power of 2 minus 1.  */
10563           if (TREE_CODE (arg0) == BIT_AND_EXPR
10564               && TREE_CODE (arg1) == BIT_AND_EXPR
10565               && operand_equal_p (TREE_OPERAND (arg0, 0),
10566                                   TREE_OPERAND (arg1, 0), 0))
10567             {
10568               tree mask0 = TREE_OPERAND (arg0, 1);
10569               tree mask1 = TREE_OPERAND (arg1, 1);
10570               tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10571
10572               if (operand_equal_p (tem, mask1, 0))
10573                 {
10574                   tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10575                                      TREE_OPERAND (arg0, 0), mask1);
10576                   return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10577                 }
10578             }
10579         }
10580
10581       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10582       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10583         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10584
10585       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10586          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10587          (-ARG1 + ARG0) reduces to -ARG1.  */
10588       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10589         return negate_expr (fold_convert_loc (loc, type, arg1));
10590
10591       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10592          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10593          signed zeros are involved.  */
10594       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10595           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10596           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10597         {
10598           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10599           tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10600           tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10601           bool arg0rz = false, arg0iz = false;
10602           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10603               || (arg0i && (arg0iz = real_zerop (arg0i))))
10604             {
10605               tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10606               tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10607               if (arg0rz && arg1i && real_zerop (arg1i))
10608                 {
10609                   tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10610                                          arg1r ? arg1r
10611                                          : build1 (REALPART_EXPR, rtype, arg1));
10612                   tree ip = arg0i ? arg0i
10613                     : build1 (IMAGPART_EXPR, rtype, arg0);
10614                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10615                 }
10616               else if (arg0iz && arg1r && real_zerop (arg1r))
10617                 {
10618                   tree rp = arg0r ? arg0r
10619                     : build1 (REALPART_EXPR, rtype, arg0);
10620                   tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10621                                          arg1i ? arg1i
10622                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10623                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10624                 }
10625             }
10626         }
10627
10628       /* Fold &x - &x.  This can happen from &x.foo - &x.
10629          This is unsafe for certain floats even in non-IEEE formats.
10630          In IEEE, it is unsafe because it does wrong for NaNs.
10631          Also note that operand_equal_p is always false if an operand
10632          is volatile.  */
10633
10634       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10635           && operand_equal_p (arg0, arg1, 0))
10636         return build_zero_cst (type);
10637
10638       /* A - B -> A + (-B) if B is easily negatable.  */
10639       if (negate_expr_p (arg1)
10640           && ((FLOAT_TYPE_P (type)
10641                /* Avoid this transformation if B is a positive REAL_CST.  */
10642                && (TREE_CODE (arg1) != REAL_CST
10643                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10644               || INTEGRAL_TYPE_P (type)))
10645         return fold_build2_loc (loc, PLUS_EXPR, type,
10646                             fold_convert_loc (loc, type, arg0),
10647                             fold_convert_loc (loc, type,
10648                                               negate_expr (arg1)));
10649
10650       /* Try folding difference of addresses.  */
10651       {
10652         HOST_WIDE_INT diff;
10653
10654         if ((TREE_CODE (arg0) == ADDR_EXPR
10655              || TREE_CODE (arg1) == ADDR_EXPR)
10656             && ptr_difference_const (arg0, arg1, &diff))
10657           return build_int_cst_type (type, diff);
10658       }
10659
10660       /* Fold &a[i] - &a[j] to i-j.  */
10661       if (TREE_CODE (arg0) == ADDR_EXPR
10662           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10663           && TREE_CODE (arg1) == ADDR_EXPR
10664           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10665         {
10666           tree tem = fold_addr_of_array_ref_difference (loc, type,
10667                                                         TREE_OPERAND (arg0, 0),
10668                                                         TREE_OPERAND (arg1, 0));
10669           if (tem)
10670             return tem;
10671         }
10672
10673       if (FLOAT_TYPE_P (type)
10674           && flag_unsafe_math_optimizations
10675           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10676           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10677           && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10678         return tem;
10679
10680       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
10681          one.  Make sure the type is not saturating and has the signedness of
10682          the stripped operands, as fold_plusminus_mult_expr will re-associate.
10683          ??? The latter condition should use TYPE_OVERFLOW_* flags instead.  */
10684       if ((TREE_CODE (arg0) == MULT_EXPR
10685            || TREE_CODE (arg1) == MULT_EXPR)
10686           && !TYPE_SATURATING (type)
10687           && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
10688           && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
10689           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10690         {
10691           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10692           if (tem)
10693             return tem;
10694         }
10695
10696       goto associate;
10697
10698     case MULT_EXPR:
10699       /* (-A) * (-B) -> A * B  */
10700       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10701         return fold_build2_loc (loc, MULT_EXPR, type,
10702                             fold_convert_loc (loc, type,
10703                                               TREE_OPERAND (arg0, 0)),
10704                             fold_convert_loc (loc, type,
10705                                               negate_expr (arg1)));
10706       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10707         return fold_build2_loc (loc, MULT_EXPR, type,
10708                             fold_convert_loc (loc, type,
10709                                               negate_expr (arg0)),
10710                             fold_convert_loc (loc, type,
10711                                               TREE_OPERAND (arg1, 0)));
10712
10713       if (! FLOAT_TYPE_P (type))
10714         {
10715           if (integer_zerop (arg1))
10716             return omit_one_operand_loc (loc, type, arg1, arg0);
10717           if (integer_onep (arg1))
10718             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10719           /* Transform x * -1 into -x.  Make sure to do the negation
10720              on the original operand with conversions not stripped
10721              because we can only strip non-sign-changing conversions.  */
10722           if (integer_all_onesp (arg1))
10723             return fold_convert_loc (loc, type, negate_expr (op0));
10724           /* Transform x * -C into -x * C if x is easily negatable.  */
10725           if (TREE_CODE (arg1) == INTEGER_CST
10726               && tree_int_cst_sgn (arg1) == -1
10727               && negate_expr_p (arg0)
10728               && (tem = negate_expr (arg1)) != arg1
10729               && !TREE_OVERFLOW (tem))
10730             return fold_build2_loc (loc, MULT_EXPR, type,
10731                                 fold_convert_loc (loc, type,
10732                                                   negate_expr (arg0)),
10733                                 tem);
10734
10735           /* (a * (1 << b)) is (a << b)  */
10736           if (TREE_CODE (arg1) == LSHIFT_EXPR
10737               && integer_onep (TREE_OPERAND (arg1, 0)))
10738             return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10739                                 TREE_OPERAND (arg1, 1));
10740           if (TREE_CODE (arg0) == LSHIFT_EXPR
10741               && integer_onep (TREE_OPERAND (arg0, 0)))
10742             return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10743                                 TREE_OPERAND (arg0, 1));
10744
10745           /* (A + A) * C -> A * 2 * C  */
10746           if (TREE_CODE (arg0) == PLUS_EXPR
10747               && TREE_CODE (arg1) == INTEGER_CST
10748               && operand_equal_p (TREE_OPERAND (arg0, 0),
10749                                   TREE_OPERAND (arg0, 1), 0))
10750             return fold_build2_loc (loc, MULT_EXPR, type,
10751                                 omit_one_operand_loc (loc, type,
10752                                                   TREE_OPERAND (arg0, 0),
10753                                                   TREE_OPERAND (arg0, 1)),
10754                                 fold_build2_loc (loc, MULT_EXPR, type,
10755                                              build_int_cst (type, 2) , arg1));
10756
10757           strict_overflow_p = false;
10758           if (TREE_CODE (arg1) == INTEGER_CST
10759               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10760                                              &strict_overflow_p)))
10761             {
10762               if (strict_overflow_p)
10763                 fold_overflow_warning (("assuming signed overflow does not "
10764                                         "occur when simplifying "
10765                                         "multiplication"),
10766                                        WARN_STRICT_OVERFLOW_MISC);
10767               return fold_convert_loc (loc, type, tem);
10768             }
10769
10770           /* Optimize z * conj(z) for integer complex numbers.  */
10771           if (TREE_CODE (arg0) == CONJ_EXPR
10772               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10773             return fold_mult_zconjz (loc, type, arg1);
10774           if (TREE_CODE (arg1) == CONJ_EXPR
10775               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10776             return fold_mult_zconjz (loc, type, arg0);
10777         }
10778       else
10779         {
10780           /* Maybe fold x * 0 to 0.  The expressions aren't the same
10781              when x is NaN, since x * 0 is also NaN.  Nor are they the
10782              same in modes with signed zeros, since multiplying a
10783              negative value by 0 gives -0, not +0.  */
10784           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10785               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10786               && real_zerop (arg1))
10787             return omit_one_operand_loc (loc, type, arg1, arg0);
10788           /* In IEEE floating point, x*1 is not equivalent to x for snans.
10789              Likewise for complex arithmetic with signed zeros.  */
10790           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10791               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10792                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10793               && real_onep (arg1))
10794             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10795
10796           /* Transform x * -1.0 into -x.  */
10797           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10798               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10799                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10800               && real_minus_onep (arg1))
10801             return fold_convert_loc (loc, type, negate_expr (arg0));
10802
10803           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
10804              the result for floating point types due to rounding so it is applied
10805              only if -fassociative-math was specify.  */
10806           if (flag_associative_math
10807               && TREE_CODE (arg0) == RDIV_EXPR
10808               && TREE_CODE (arg1) == REAL_CST
10809               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10810             {
10811               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10812                                       arg1);
10813               if (tem)
10814                 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10815                                     TREE_OPERAND (arg0, 1));
10816             }
10817
10818           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
10819           if (operand_equal_p (arg0, arg1, 0))
10820             {
10821               tree tem = fold_strip_sign_ops (arg0);
10822               if (tem != NULL_TREE)
10823                 {
10824                   tem = fold_convert_loc (loc, type, tem);
10825                   return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
10826                 }
10827             }
10828
10829           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10830              This is not the same for NaNs or if signed zeros are
10831              involved.  */
10832           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10833               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10834               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10835               && TREE_CODE (arg1) == COMPLEX_CST
10836               && real_zerop (TREE_REALPART (arg1)))
10837             {
10838               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10839               if (real_onep (TREE_IMAGPART (arg1)))
10840                 return
10841                   fold_build2_loc (loc, COMPLEX_EXPR, type,
10842                                negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10843                                                              rtype, arg0)),
10844                                fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10845               else if (real_minus_onep (TREE_IMAGPART (arg1)))
10846                 return
10847                   fold_build2_loc (loc, COMPLEX_EXPR, type,
10848                                fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10849                                negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10850                                                              rtype, arg0)));
10851             }
10852
10853           /* Optimize z * conj(z) for floating point complex numbers.
10854              Guarded by flag_unsafe_math_optimizations as non-finite
10855              imaginary components don't produce scalar results.  */
10856           if (flag_unsafe_math_optimizations
10857               && TREE_CODE (arg0) == CONJ_EXPR
10858               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10859             return fold_mult_zconjz (loc, type, arg1);
10860           if (flag_unsafe_math_optimizations
10861               && TREE_CODE (arg1) == CONJ_EXPR
10862               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10863             return fold_mult_zconjz (loc, type, arg0);
10864
10865           if (flag_unsafe_math_optimizations)
10866             {
10867               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10868               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10869
10870               /* Optimizations of root(...)*root(...).  */
10871               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10872                 {
10873                   tree rootfn, arg;
10874                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10875                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10876
10877                   /* Optimize sqrt(x)*sqrt(x) as x.  */
10878                   if (BUILTIN_SQRT_P (fcode0)
10879                       && operand_equal_p (arg00, arg10, 0)
10880                       && ! HONOR_SNANS (TYPE_MODE (type)))
10881                     return arg00;
10882
10883                   /* Optimize root(x)*root(y) as root(x*y).  */
10884                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10885                   arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
10886                   return build_call_expr_loc (loc, rootfn, 1, arg);
10887                 }
10888
10889               /* Optimize expN(x)*expN(y) as expN(x+y).  */
10890               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10891                 {
10892                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10893                   tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10894                                           CALL_EXPR_ARG (arg0, 0),
10895                                           CALL_EXPR_ARG (arg1, 0));
10896                   return build_call_expr_loc (loc, expfn, 1, arg);
10897                 }
10898
10899               /* Optimizations of pow(...)*pow(...).  */
10900               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10901                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10902                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10903                 {
10904                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10905                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10906                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10907                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10908
10909                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
10910                   if (operand_equal_p (arg01, arg11, 0))
10911                     {
10912                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10913                       tree arg = fold_build2_loc (loc, MULT_EXPR, type,
10914                                               arg00, arg10);
10915                       return build_call_expr_loc (loc, powfn, 2, arg, arg01);
10916                     }
10917
10918                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
10919                   if (operand_equal_p (arg00, arg10, 0))
10920                     {
10921                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10922                       tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10923                                               arg01, arg11);
10924                       return build_call_expr_loc (loc, powfn, 2, arg00, arg);
10925                     }
10926                 }
10927
10928               /* Optimize tan(x)*cos(x) as sin(x).  */
10929               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10930                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10931                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10932                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10933                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10934                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10935                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10936                                       CALL_EXPR_ARG (arg1, 0), 0))
10937                 {
10938                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10939
10940                   if (sinfn != NULL_TREE)
10941                     return build_call_expr_loc (loc, sinfn, 1,
10942                                             CALL_EXPR_ARG (arg0, 0));
10943                 }
10944
10945               /* Optimize x*pow(x,c) as pow(x,c+1).  */
10946               if (fcode1 == BUILT_IN_POW
10947                   || fcode1 == BUILT_IN_POWF
10948                   || fcode1 == BUILT_IN_POWL)
10949                 {
10950                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10951                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10952                   if (TREE_CODE (arg11) == REAL_CST
10953                       && !TREE_OVERFLOW (arg11)
10954                       && operand_equal_p (arg0, arg10, 0))
10955                     {
10956                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10957                       REAL_VALUE_TYPE c;
10958                       tree arg;
10959
10960                       c = TREE_REAL_CST (arg11);
10961                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10962                       arg = build_real (type, c);
10963                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10964                     }
10965                 }
10966
10967               /* Optimize pow(x,c)*x as pow(x,c+1).  */
10968               if (fcode0 == BUILT_IN_POW
10969                   || fcode0 == BUILT_IN_POWF
10970                   || fcode0 == BUILT_IN_POWL)
10971                 {
10972                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10973                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10974                   if (TREE_CODE (arg01) == REAL_CST
10975                       && !TREE_OVERFLOW (arg01)
10976                       && operand_equal_p (arg1, arg00, 0))
10977                     {
10978                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10979                       REAL_VALUE_TYPE c;
10980                       tree arg;
10981
10982                       c = TREE_REAL_CST (arg01);
10983                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10984                       arg = build_real (type, c);
10985                       return build_call_expr_loc (loc, powfn, 2, arg1, arg);
10986                     }
10987                 }
10988
10989               /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x.  */
10990               if (!in_gimple_form
10991                   && optimize
10992                   && operand_equal_p (arg0, arg1, 0))
10993                 {
10994                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10995
10996                   if (powfn)
10997                     {
10998                       tree arg = build_real (type, dconst2);
10999                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
11000                     }
11001                 }
11002             }
11003         }
11004       goto associate;
11005
11006     case BIT_IOR_EXPR:
11007     bit_ior:
11008       if (integer_all_onesp (arg1))
11009         return omit_one_operand_loc (loc, type, arg1, arg0);
11010       if (integer_zerop (arg1))
11011         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11012       if (operand_equal_p (arg0, arg1, 0))
11013         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11014
11015       /* ~X | X is -1.  */
11016       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11017           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11018         {
11019           t1 = build_zero_cst (type);
11020           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11021           return omit_one_operand_loc (loc, type, t1, arg1);
11022         }
11023
11024       /* X | ~X is -1.  */
11025       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11026           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11027         {
11028           t1 = build_zero_cst (type);
11029           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11030           return omit_one_operand_loc (loc, type, t1, arg0);
11031         }
11032
11033       /* Canonicalize (X & C1) | C2.  */
11034       if (TREE_CODE (arg0) == BIT_AND_EXPR
11035           && TREE_CODE (arg1) == INTEGER_CST
11036           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11037         {
11038           double_int c1, c2, c3, msk;
11039           int width = TYPE_PRECISION (type), w;
11040           c1 = tree_to_double_int (TREE_OPERAND (arg0, 1));
11041           c2 = tree_to_double_int (arg1);
11042
11043           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
11044           if (double_int_equal_p (double_int_and (c1, c2), c1))
11045             return omit_one_operand_loc (loc, type, arg1,
11046                                          TREE_OPERAND (arg0, 0));
11047
11048           msk = double_int_mask (width);
11049
11050           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
11051           if (double_int_zero_p (double_int_and_not (msk,
11052                                                      double_int_ior (c1, c2))))
11053             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11054                                     TREE_OPERAND (arg0, 0), arg1);
11055
11056           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11057              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11058              mode which allows further optimizations.  */
11059           c1 = double_int_and (c1, msk);
11060           c2 = double_int_and (c2, msk);
11061           c3 = double_int_and_not (c1, c2);
11062           for (w = BITS_PER_UNIT;
11063                w <= width && w <= HOST_BITS_PER_WIDE_INT;
11064                w <<= 1)
11065             {
11066               unsigned HOST_WIDE_INT mask
11067                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
11068               if (((c1.low | c2.low) & mask) == mask
11069                   && (c1.low & ~mask) == 0 && c1.high == 0)
11070                 {
11071                   c3 = uhwi_to_double_int (mask);
11072                   break;
11073                 }
11074             }
11075           if (!double_int_equal_p (c3, c1))
11076             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11077                                     fold_build2_loc (loc, BIT_AND_EXPR, type,
11078                                                      TREE_OPERAND (arg0, 0),
11079                                                      double_int_to_tree (type,
11080                                                                          c3)),
11081                                     arg1);
11082         }
11083
11084       /* (X & Y) | Y is (X, Y).  */
11085       if (TREE_CODE (arg0) == BIT_AND_EXPR
11086           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11087         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11088       /* (X & Y) | X is (Y, X).  */
11089       if (TREE_CODE (arg0) == BIT_AND_EXPR
11090           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11091           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11092         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11093       /* X | (X & Y) is (Y, X).  */
11094       if (TREE_CODE (arg1) == BIT_AND_EXPR
11095           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11096           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11097         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11098       /* X | (Y & X) is (Y, X).  */
11099       if (TREE_CODE (arg1) == BIT_AND_EXPR
11100           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11101           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11102         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11103
11104       /* (X & ~Y) | (~X & Y) is X ^ Y */
11105       if (TREE_CODE (arg0) == BIT_AND_EXPR
11106           && TREE_CODE (arg1) == BIT_AND_EXPR)
11107         {
11108           tree a0, a1, l0, l1, n0, n1;
11109
11110           a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11111           a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11112
11113           l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11114           l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11115           
11116           n0 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l0);
11117           n1 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l1);
11118           
11119           if ((operand_equal_p (n0, a0, 0)
11120                && operand_equal_p (n1, a1, 0))
11121               || (operand_equal_p (n0, a1, 0)
11122                   && operand_equal_p (n1, a0, 0)))
11123             return fold_build2_loc (loc, BIT_XOR_EXPR, type, l0, n1);
11124         }
11125
11126       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11127       if (t1 != NULL_TREE)
11128         return t1;
11129
11130       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
11131
11132          This results in more efficient code for machines without a NAND
11133          instruction.  Combine will canonicalize to the first form
11134          which will allow use of NAND instructions provided by the
11135          backend if they exist.  */
11136       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11137           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11138         {
11139           return
11140             fold_build1_loc (loc, BIT_NOT_EXPR, type,
11141                          build2 (BIT_AND_EXPR, type,
11142                                  fold_convert_loc (loc, type,
11143                                                    TREE_OPERAND (arg0, 0)),
11144                                  fold_convert_loc (loc, type,
11145                                                    TREE_OPERAND (arg1, 0))));
11146         }
11147
11148       /* See if this can be simplified into a rotate first.  If that
11149          is unsuccessful continue in the association code.  */
11150       goto bit_rotate;
11151
11152     case BIT_XOR_EXPR:
11153       if (integer_zerop (arg1))
11154         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11155       if (integer_all_onesp (arg1))
11156         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
11157       if (operand_equal_p (arg0, arg1, 0))
11158         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11159
11160       /* ~X ^ X is -1.  */
11161       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11162           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11163         {
11164           t1 = build_zero_cst (type);
11165           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11166           return omit_one_operand_loc (loc, type, t1, arg1);
11167         }
11168
11169       /* X ^ ~X is -1.  */
11170       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11171           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11172         {
11173           t1 = build_zero_cst (type);
11174           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11175           return omit_one_operand_loc (loc, type, t1, arg0);
11176         }
11177
11178       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
11179          with a constant, and the two constants have no bits in common,
11180          we should treat this as a BIT_IOR_EXPR since this may produce more
11181          simplifications.  */
11182       if (TREE_CODE (arg0) == BIT_AND_EXPR
11183           && TREE_CODE (arg1) == BIT_AND_EXPR
11184           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11185           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
11186           && integer_zerop (const_binop (BIT_AND_EXPR,
11187                                          TREE_OPERAND (arg0, 1),
11188                                          TREE_OPERAND (arg1, 1))))
11189         {
11190           code = BIT_IOR_EXPR;
11191           goto bit_ior;
11192         }
11193
11194       /* (X | Y) ^ X -> Y & ~ X*/
11195       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11196           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11197         {
11198           tree t2 = TREE_OPERAND (arg0, 1);
11199           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11200                             arg1);
11201           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11202                             fold_convert_loc (loc, type, t2),
11203                             fold_convert_loc (loc, type, t1));
11204           return t1;
11205         }
11206
11207       /* (Y | X) ^ X -> Y & ~ X*/
11208       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11209           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11210         {
11211           tree t2 = TREE_OPERAND (arg0, 0);
11212           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11213                             arg1);
11214           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11215                             fold_convert_loc (loc, type, t2),
11216                             fold_convert_loc (loc, type, t1));
11217           return t1;
11218         }
11219
11220       /* X ^ (X | Y) -> Y & ~ X*/
11221       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11222           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11223         {
11224           tree t2 = TREE_OPERAND (arg1, 1);
11225           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11226                             arg0);
11227           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11228                             fold_convert_loc (loc, type, t2),
11229                             fold_convert_loc (loc, type, t1));
11230           return t1;
11231         }
11232
11233       /* X ^ (Y | X) -> Y & ~ X*/
11234       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11235           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11236         {
11237           tree t2 = TREE_OPERAND (arg1, 0);
11238           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11239                             arg0);
11240           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11241                             fold_convert_loc (loc, type, t2),
11242                             fold_convert_loc (loc, type, t1));
11243           return t1;
11244         }
11245
11246       /* Convert ~X ^ ~Y to X ^ Y.  */
11247       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11248           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11249         return fold_build2_loc (loc, code, type,
11250                             fold_convert_loc (loc, type,
11251                                               TREE_OPERAND (arg0, 0)),
11252                             fold_convert_loc (loc, type,
11253                                               TREE_OPERAND (arg1, 0)));
11254
11255       /* Convert ~X ^ C to X ^ ~C.  */
11256       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11257           && TREE_CODE (arg1) == INTEGER_CST)
11258         return fold_build2_loc (loc, code, type,
11259                             fold_convert_loc (loc, type,
11260                                               TREE_OPERAND (arg0, 0)),
11261                             fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
11262
11263       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
11264       if (TREE_CODE (arg0) == BIT_AND_EXPR
11265           && integer_onep (TREE_OPERAND (arg0, 1))
11266           && integer_onep (arg1))
11267         return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11268                             build_int_cst (TREE_TYPE (arg0), 0));
11269
11270       /* Fold (X & Y) ^ Y as ~X & Y.  */
11271       if (TREE_CODE (arg0) == BIT_AND_EXPR
11272           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11273         {
11274           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11275           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11276                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11277                               fold_convert_loc (loc, type, arg1));
11278         }
11279       /* Fold (X & Y) ^ X as ~Y & X.  */
11280       if (TREE_CODE (arg0) == BIT_AND_EXPR
11281           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11282           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11283         {
11284           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11285           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11286                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11287                               fold_convert_loc (loc, type, arg1));
11288         }
11289       /* Fold X ^ (X & Y) as X & ~Y.  */
11290       if (TREE_CODE (arg1) == BIT_AND_EXPR
11291           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11292         {
11293           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11294           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11295                               fold_convert_loc (loc, type, arg0),
11296                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11297         }
11298       /* Fold X ^ (Y & X) as ~Y & X.  */
11299       if (TREE_CODE (arg1) == BIT_AND_EXPR
11300           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11301           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11302         {
11303           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11304           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11305                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11306                               fold_convert_loc (loc, type, arg0));
11307         }
11308
11309       /* See if this can be simplified into a rotate first.  If that
11310          is unsuccessful continue in the association code.  */
11311       goto bit_rotate;
11312
11313     case BIT_AND_EXPR:
11314       if (integer_all_onesp (arg1))
11315         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11316       if (integer_zerop (arg1))
11317         return omit_one_operand_loc (loc, type, arg1, arg0);
11318       if (operand_equal_p (arg0, arg1, 0))
11319         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11320
11321       /* ~X & X, (X == 0) & X, and !X & X are always zero.  */
11322       if ((TREE_CODE (arg0) == BIT_NOT_EXPR
11323            || TREE_CODE (arg0) == TRUTH_NOT_EXPR
11324            || (TREE_CODE (arg0) == EQ_EXPR
11325                && integer_zerop (TREE_OPERAND (arg0, 1))))
11326           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11327         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11328
11329       /* X & ~X , X & (X == 0), and X & !X are always zero.  */
11330       if ((TREE_CODE (arg1) == BIT_NOT_EXPR
11331            || TREE_CODE (arg1) == TRUTH_NOT_EXPR
11332            || (TREE_CODE (arg1) == EQ_EXPR
11333                && integer_zerop (TREE_OPERAND (arg1, 1))))
11334           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11335         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11336
11337       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
11338       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11339           && TREE_CODE (arg1) == INTEGER_CST
11340           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11341         {
11342           tree tmp1 = fold_convert_loc (loc, type, arg1);
11343           tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11344           tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11345           tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
11346           tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
11347           return
11348             fold_convert_loc (loc, type,
11349                               fold_build2_loc (loc, BIT_IOR_EXPR,
11350                                            type, tmp2, tmp3));
11351         }
11352
11353       /* (X | Y) & Y is (X, Y).  */
11354       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11355           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11356         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11357       /* (X | Y) & X is (Y, X).  */
11358       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11359           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11360           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11361         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11362       /* X & (X | Y) is (Y, X).  */
11363       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11364           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11365           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11366         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11367       /* X & (Y | X) is (Y, X).  */
11368       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11369           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11370           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11371         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11372
11373       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
11374       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11375           && integer_onep (TREE_OPERAND (arg0, 1))
11376           && integer_onep (arg1))
11377         {
11378           tem = TREE_OPERAND (arg0, 0);
11379           return fold_build2_loc (loc, EQ_EXPR, type,
11380                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11381                                            build_int_cst (TREE_TYPE (tem), 1)),
11382                               build_int_cst (TREE_TYPE (tem), 0));
11383         }
11384       /* Fold ~X & 1 as (X & 1) == 0.  */
11385       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11386           && integer_onep (arg1))
11387         {
11388           tem = TREE_OPERAND (arg0, 0);
11389           return fold_build2_loc (loc, EQ_EXPR, type,
11390                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11391                                            build_int_cst (TREE_TYPE (tem), 1)),
11392                               build_int_cst (TREE_TYPE (tem), 0));
11393         }
11394       /* Fold !X & 1 as X == 0.  */
11395       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11396           && integer_onep (arg1))
11397         {
11398           tem = TREE_OPERAND (arg0, 0);
11399           return fold_build2_loc (loc, EQ_EXPR, type, tem,
11400                                   build_int_cst (TREE_TYPE (tem), 0));
11401         }
11402
11403       /* Fold (X ^ Y) & Y as ~X & Y.  */
11404       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11405           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11406         {
11407           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11408           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11409                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11410                               fold_convert_loc (loc, type, arg1));
11411         }
11412       /* Fold (X ^ Y) & X as ~Y & X.  */
11413       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11414           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11415           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11416         {
11417           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11418           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11419                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11420                               fold_convert_loc (loc, type, arg1));
11421         }
11422       /* Fold X & (X ^ Y) as X & ~Y.  */
11423       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11424           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11425         {
11426           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11427           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11428                               fold_convert_loc (loc, type, arg0),
11429                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11430         }
11431       /* Fold X & (Y ^ X) as ~Y & X.  */
11432       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11433           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11434           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11435         {
11436           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11437           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11438                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11439                               fold_convert_loc (loc, type, arg0));
11440         }
11441
11442       /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11443          ((A & N) + B) & M -> (A + B) & M
11444          Similarly if (N & M) == 0,
11445          ((A | N) + B) & M -> (A + B) & M
11446          and for - instead of + (or unary - instead of +)
11447          and/or ^ instead of |.
11448          If B is constant and (B & M) == 0, fold into A & M.  */
11449       if (host_integerp (arg1, 1))
11450         {
11451           unsigned HOST_WIDE_INT cst1 = tree_low_cst (arg1, 1);
11452           if (~cst1 && (cst1 & (cst1 + 1)) == 0
11453               && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11454               && (TREE_CODE (arg0) == PLUS_EXPR
11455                   || TREE_CODE (arg0) == MINUS_EXPR
11456                   || TREE_CODE (arg0) == NEGATE_EXPR)
11457               && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
11458                   || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
11459             {
11460               tree pmop[2];
11461               int which = 0;
11462               unsigned HOST_WIDE_INT cst0;
11463
11464               /* Now we know that arg0 is (C + D) or (C - D) or
11465                  -C and arg1 (M) is == (1LL << cst) - 1.
11466                  Store C into PMOP[0] and D into PMOP[1].  */
11467               pmop[0] = TREE_OPERAND (arg0, 0);
11468               pmop[1] = NULL;
11469               if (TREE_CODE (arg0) != NEGATE_EXPR)
11470                 {
11471                   pmop[1] = TREE_OPERAND (arg0, 1);
11472                   which = 1;
11473                 }
11474
11475               if (!host_integerp (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11476                   || (tree_low_cst (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11477                       & cst1) != cst1)
11478                 which = -1;
11479
11480               for (; which >= 0; which--)
11481                 switch (TREE_CODE (pmop[which]))
11482                   {
11483                   case BIT_AND_EXPR:
11484                   case BIT_IOR_EXPR:
11485                   case BIT_XOR_EXPR:
11486                     if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
11487                         != INTEGER_CST)
11488                       break;
11489                     /* tree_low_cst not used, because we don't care about
11490                        the upper bits.  */
11491                     cst0 = TREE_INT_CST_LOW (TREE_OPERAND (pmop[which], 1));
11492                     cst0 &= cst1;
11493                     if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
11494                       {
11495                         if (cst0 != cst1)
11496                           break;
11497                       }
11498                     else if (cst0 != 0)
11499                       break;
11500                     /* If C or D is of the form (A & N) where
11501                        (N & M) == M, or of the form (A | N) or
11502                        (A ^ N) where (N & M) == 0, replace it with A.  */
11503                     pmop[which] = TREE_OPERAND (pmop[which], 0);
11504                     break;
11505                   case INTEGER_CST:
11506                     /* If C or D is a N where (N & M) == 0, it can be
11507                        omitted (assumed 0).  */
11508                     if ((TREE_CODE (arg0) == PLUS_EXPR
11509                          || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
11510                         && (TREE_INT_CST_LOW (pmop[which]) & cst1) == 0)
11511                       pmop[which] = NULL;
11512                     break;
11513                   default:
11514                     break;
11515                   }
11516
11517               /* Only build anything new if we optimized one or both arguments
11518                  above.  */
11519               if (pmop[0] != TREE_OPERAND (arg0, 0)
11520                   || (TREE_CODE (arg0) != NEGATE_EXPR
11521                       && pmop[1] != TREE_OPERAND (arg0, 1)))
11522                 {
11523                   tree utype = TREE_TYPE (arg0);
11524                   if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11525                     {
11526                       /* Perform the operations in a type that has defined
11527                          overflow behavior.  */
11528                       utype = unsigned_type_for (TREE_TYPE (arg0));
11529                       if (pmop[0] != NULL)
11530                         pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
11531                       if (pmop[1] != NULL)
11532                         pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
11533                     }
11534
11535                   if (TREE_CODE (arg0) == NEGATE_EXPR)
11536                     tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
11537                   else if (TREE_CODE (arg0) == PLUS_EXPR)
11538                     {
11539                       if (pmop[0] != NULL && pmop[1] != NULL)
11540                         tem = fold_build2_loc (loc, PLUS_EXPR, utype,
11541                                                pmop[0], pmop[1]);
11542                       else if (pmop[0] != NULL)
11543                         tem = pmop[0];
11544                       else if (pmop[1] != NULL)
11545                         tem = pmop[1];
11546                       else
11547                         return build_int_cst (type, 0);
11548                     }
11549                   else if (pmop[0] == NULL)
11550                     tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
11551                   else
11552                     tem = fold_build2_loc (loc, MINUS_EXPR, utype,
11553                                            pmop[0], pmop[1]);
11554                   /* TEM is now the new binary +, - or unary - replacement.  */
11555                   tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
11556                                          fold_convert_loc (loc, utype, arg1));
11557                   return fold_convert_loc (loc, type, tem);
11558                 }
11559             }
11560         }
11561
11562       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11563       if (t1 != NULL_TREE)
11564         return t1;
11565       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
11566       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11567           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11568         {
11569           unsigned int prec
11570             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11571
11572           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11573               && (~TREE_INT_CST_LOW (arg1)
11574                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11575             return
11576               fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11577         }
11578
11579       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11580
11581          This results in more efficient code for machines without a NOR
11582          instruction.  Combine will canonicalize to the first form
11583          which will allow use of NOR instructions provided by the
11584          backend if they exist.  */
11585       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11586           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11587         {
11588           return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11589                               build2 (BIT_IOR_EXPR, type,
11590                                       fold_convert_loc (loc, type,
11591                                                         TREE_OPERAND (arg0, 0)),
11592                                       fold_convert_loc (loc, type,
11593                                                         TREE_OPERAND (arg1, 0))));
11594         }
11595
11596       /* If arg0 is derived from the address of an object or function, we may
11597          be able to fold this expression using the object or function's
11598          alignment.  */
11599       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11600         {
11601           unsigned HOST_WIDE_INT modulus, residue;
11602           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11603
11604           modulus = get_pointer_modulus_and_residue (arg0, &residue,
11605                                                      integer_onep (arg1));
11606
11607           /* This works because modulus is a power of 2.  If this weren't the
11608              case, we'd have to replace it by its greatest power-of-2
11609              divisor: modulus & -modulus.  */
11610           if (low < modulus)
11611             return build_int_cst (type, residue & low);
11612         }
11613
11614       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11615               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11616          if the new mask might be further optimized.  */
11617       if ((TREE_CODE (arg0) == LSHIFT_EXPR
11618            || TREE_CODE (arg0) == RSHIFT_EXPR)
11619           && host_integerp (TREE_OPERAND (arg0, 1), 1)
11620           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11621           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11622              < TYPE_PRECISION (TREE_TYPE (arg0))
11623           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11624           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11625         {
11626           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11627           unsigned HOST_WIDE_INT mask
11628             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11629           unsigned HOST_WIDE_INT newmask, zerobits = 0;
11630           tree shift_type = TREE_TYPE (arg0);
11631
11632           if (TREE_CODE (arg0) == LSHIFT_EXPR)
11633             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11634           else if (TREE_CODE (arg0) == RSHIFT_EXPR
11635                    && TYPE_PRECISION (TREE_TYPE (arg0))
11636                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11637             {
11638               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11639               tree arg00 = TREE_OPERAND (arg0, 0);
11640               /* See if more bits can be proven as zero because of
11641                  zero extension.  */
11642               if (TREE_CODE (arg00) == NOP_EXPR
11643                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11644                 {
11645                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11646                   if (TYPE_PRECISION (inner_type)
11647                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11648                       && TYPE_PRECISION (inner_type) < prec)
11649                     {
11650                       prec = TYPE_PRECISION (inner_type);
11651                       /* See if we can shorten the right shift.  */
11652                       if (shiftc < prec)
11653                         shift_type = inner_type;
11654                     }
11655                 }
11656               zerobits = ~(unsigned HOST_WIDE_INT) 0;
11657               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11658               zerobits <<= prec - shiftc;
11659               /* For arithmetic shift if sign bit could be set, zerobits
11660                  can contain actually sign bits, so no transformation is
11661                  possible, unless MASK masks them all away.  In that
11662                  case the shift needs to be converted into logical shift.  */
11663               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11664                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11665                 {
11666                   if ((mask & zerobits) == 0)
11667                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
11668                   else
11669                     zerobits = 0;
11670                 }
11671             }
11672
11673           /* ((X << 16) & 0xff00) is (X, 0).  */
11674           if ((mask & zerobits) == mask)
11675             return omit_one_operand_loc (loc, type,
11676                                      build_int_cst (type, 0), arg0);
11677
11678           newmask = mask | zerobits;
11679           if (newmask != mask && (newmask & (newmask + 1)) == 0)
11680             {
11681               unsigned int prec;
11682
11683               /* Only do the transformation if NEWMASK is some integer
11684                  mode's mask.  */
11685               for (prec = BITS_PER_UNIT;
11686                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11687                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11688                   break;
11689               if (prec < HOST_BITS_PER_WIDE_INT
11690                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
11691                 {
11692                   tree newmaskt;
11693
11694                   if (shift_type != TREE_TYPE (arg0))
11695                     {
11696                       tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11697                                          fold_convert_loc (loc, shift_type,
11698                                                            TREE_OPERAND (arg0, 0)),
11699                                          TREE_OPERAND (arg0, 1));
11700                       tem = fold_convert_loc (loc, type, tem);
11701                     }
11702                   else
11703                     tem = op0;
11704                   newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11705                   if (!tree_int_cst_equal (newmaskt, arg1))
11706                     return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11707                 }
11708             }
11709         }
11710
11711       goto associate;
11712
11713     case RDIV_EXPR:
11714       /* Don't touch a floating-point divide by zero unless the mode
11715          of the constant can represent infinity.  */
11716       if (TREE_CODE (arg1) == REAL_CST
11717           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11718           && real_zerop (arg1))
11719         return NULL_TREE;
11720
11721       /* Optimize A / A to 1.0 if we don't care about
11722          NaNs or Infinities.  Skip the transformation
11723          for non-real operands.  */
11724       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11725           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11726           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11727           && operand_equal_p (arg0, arg1, 0))
11728         {
11729           tree r = build_real (TREE_TYPE (arg0), dconst1);
11730
11731           return omit_two_operands_loc (loc, type, r, arg0, arg1);
11732         }
11733
11734       /* The complex version of the above A / A optimization.  */
11735       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11736           && operand_equal_p (arg0, arg1, 0))
11737         {
11738           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11739           if (! HONOR_NANS (TYPE_MODE (elem_type))
11740               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11741             {
11742               tree r = build_real (elem_type, dconst1);
11743               /* omit_two_operands will call fold_convert for us.  */
11744               return omit_two_operands_loc (loc, type, r, arg0, arg1);
11745             }
11746         }
11747
11748       /* (-A) / (-B) -> A / B  */
11749       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11750         return fold_build2_loc (loc, RDIV_EXPR, type,
11751                             TREE_OPERAND (arg0, 0),
11752                             negate_expr (arg1));
11753       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11754         return fold_build2_loc (loc, RDIV_EXPR, type,
11755                             negate_expr (arg0),
11756                             TREE_OPERAND (arg1, 0));
11757
11758       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
11759       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11760           && real_onep (arg1))
11761         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11762
11763       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
11764       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11765           && real_minus_onep (arg1))
11766         return non_lvalue_loc (loc, fold_convert_loc (loc, type,
11767                                                   negate_expr (arg0)));
11768
11769       /* If ARG1 is a constant, we can convert this to a multiply by the
11770          reciprocal.  This does not have the same rounding properties,
11771          so only do this if -freciprocal-math.  We can actually
11772          always safely do it if ARG1 is a power of two, but it's hard to
11773          tell if it is or not in a portable manner.  */
11774       if (TREE_CODE (arg1) == REAL_CST)
11775         {
11776           if (flag_reciprocal_math
11777               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11778                                           arg1)))
11779             return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
11780           /* Find the reciprocal if optimizing and the result is exact.  */
11781           if (optimize)
11782             {
11783               REAL_VALUE_TYPE r;
11784               r = TREE_REAL_CST (arg1);
11785               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11786                 {
11787                   tem = build_real (type, r);
11788                   return fold_build2_loc (loc, MULT_EXPR, type,
11789                                       fold_convert_loc (loc, type, arg0), tem);
11790                 }
11791             }
11792         }
11793       /* Convert A/B/C to A/(B*C).  */
11794       if (flag_reciprocal_math
11795           && TREE_CODE (arg0) == RDIV_EXPR)
11796         return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11797                             fold_build2_loc (loc, MULT_EXPR, type,
11798                                          TREE_OPERAND (arg0, 1), arg1));
11799
11800       /* Convert A/(B/C) to (A/B)*C.  */
11801       if (flag_reciprocal_math
11802           && TREE_CODE (arg1) == RDIV_EXPR)
11803         return fold_build2_loc (loc, MULT_EXPR, type,
11804                             fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11805                                          TREE_OPERAND (arg1, 0)),
11806                             TREE_OPERAND (arg1, 1));
11807
11808       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11809       if (flag_reciprocal_math
11810           && TREE_CODE (arg1) == MULT_EXPR
11811           && TREE_CODE (arg0) == REAL_CST
11812           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11813         {
11814           tree tem = const_binop (RDIV_EXPR, arg0,
11815                                   TREE_OPERAND (arg1, 1));
11816           if (tem)
11817             return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11818                                 TREE_OPERAND (arg1, 0));
11819         }
11820
11821       if (flag_unsafe_math_optimizations)
11822         {
11823           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11824           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11825
11826           /* Optimize sin(x)/cos(x) as tan(x).  */
11827           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11828                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11829                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11830               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11831                                   CALL_EXPR_ARG (arg1, 0), 0))
11832             {
11833               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11834
11835               if (tanfn != NULL_TREE)
11836                 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11837             }
11838
11839           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11840           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11841                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11842                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11843               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11844                                   CALL_EXPR_ARG (arg1, 0), 0))
11845             {
11846               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11847
11848               if (tanfn != NULL_TREE)
11849                 {
11850                   tree tmp = build_call_expr_loc (loc, tanfn, 1,
11851                                               CALL_EXPR_ARG (arg0, 0));
11852                   return fold_build2_loc (loc, RDIV_EXPR, type,
11853                                       build_real (type, dconst1), tmp);
11854                 }
11855             }
11856
11857           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11858              NaNs or Infinities.  */
11859           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11860                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11861                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11862             {
11863               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11864               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11865
11866               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11867                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11868                   && operand_equal_p (arg00, arg01, 0))
11869                 {
11870                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11871
11872                   if (cosfn != NULL_TREE)
11873                     return build_call_expr_loc (loc, cosfn, 1, arg00);
11874                 }
11875             }
11876
11877           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11878              NaNs or Infinities.  */
11879           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11880                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11881                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11882             {
11883               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11884               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11885
11886               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11887                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11888                   && operand_equal_p (arg00, arg01, 0))
11889                 {
11890                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11891
11892                   if (cosfn != NULL_TREE)
11893                     {
11894                       tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11895                       return fold_build2_loc (loc, RDIV_EXPR, type,
11896                                           build_real (type, dconst1),
11897                                           tmp);
11898                     }
11899                 }
11900             }
11901
11902           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11903           if (fcode0 == BUILT_IN_POW
11904               || fcode0 == BUILT_IN_POWF
11905               || fcode0 == BUILT_IN_POWL)
11906             {
11907               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11908               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11909               if (TREE_CODE (arg01) == REAL_CST
11910                   && !TREE_OVERFLOW (arg01)
11911                   && operand_equal_p (arg1, arg00, 0))
11912                 {
11913                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11914                   REAL_VALUE_TYPE c;
11915                   tree arg;
11916
11917                   c = TREE_REAL_CST (arg01);
11918                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11919                   arg = build_real (type, c);
11920                   return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11921                 }
11922             }
11923
11924           /* Optimize a/root(b/c) into a*root(c/b).  */
11925           if (BUILTIN_ROOT_P (fcode1))
11926             {
11927               tree rootarg = CALL_EXPR_ARG (arg1, 0);
11928
11929               if (TREE_CODE (rootarg) == RDIV_EXPR)
11930                 {
11931                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11932                   tree b = TREE_OPERAND (rootarg, 0);
11933                   tree c = TREE_OPERAND (rootarg, 1);
11934
11935                   tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
11936
11937                   tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
11938                   return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
11939                 }
11940             }
11941
11942           /* Optimize x/expN(y) into x*expN(-y).  */
11943           if (BUILTIN_EXPONENT_P (fcode1))
11944             {
11945               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11946               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11947               arg1 = build_call_expr_loc (loc,
11948                                       expfn, 1,
11949                                       fold_convert_loc (loc, type, arg));
11950               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11951             }
11952
11953           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
11954           if (fcode1 == BUILT_IN_POW
11955               || fcode1 == BUILT_IN_POWF
11956               || fcode1 == BUILT_IN_POWL)
11957             {
11958               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11959               tree arg10 = CALL_EXPR_ARG (arg1, 0);
11960               tree arg11 = CALL_EXPR_ARG (arg1, 1);
11961               tree neg11 = fold_convert_loc (loc, type,
11962                                              negate_expr (arg11));
11963               arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
11964               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11965             }
11966         }
11967       return NULL_TREE;
11968
11969     case TRUNC_DIV_EXPR:
11970       /* Optimize (X & (-A)) / A where A is a power of 2,
11971          to X >> log2(A) */
11972       if (TREE_CODE (arg0) == BIT_AND_EXPR
11973           && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST
11974           && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0)
11975         {
11976           tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1),
11977                                       arg1, TREE_OPERAND (arg0, 1));
11978           if (sum && integer_zerop (sum)) {
11979             unsigned long pow2;
11980
11981             if (TREE_INT_CST_LOW (arg1))
11982               pow2 = exact_log2 (TREE_INT_CST_LOW (arg1));
11983             else
11984               pow2 = exact_log2 (TREE_INT_CST_HIGH (arg1))
11985                       + HOST_BITS_PER_WIDE_INT;
11986
11987             return fold_build2_loc (loc, RSHIFT_EXPR, type,
11988                           TREE_OPERAND (arg0, 0),
11989                           build_int_cst (integer_type_node, pow2));
11990           }
11991         }
11992
11993       /* Fall thru */
11994       
11995     case FLOOR_DIV_EXPR:
11996       /* Simplify A / (B << N) where A and B are positive and B is
11997          a power of 2, to A >> (N + log2(B)).  */
11998       strict_overflow_p = false;
11999       if (TREE_CODE (arg1) == LSHIFT_EXPR
12000           && (TYPE_UNSIGNED (type)
12001               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12002         {
12003           tree sval = TREE_OPERAND (arg1, 0);
12004           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
12005             {
12006               tree sh_cnt = TREE_OPERAND (arg1, 1);
12007               unsigned long pow2;
12008
12009               if (TREE_INT_CST_LOW (sval))
12010                 pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
12011               else
12012                 pow2 = exact_log2 (TREE_INT_CST_HIGH (sval))
12013                        + HOST_BITS_PER_WIDE_INT;
12014
12015               if (strict_overflow_p)
12016                 fold_overflow_warning (("assuming signed overflow does not "
12017                                         "occur when simplifying A / (B << N)"),
12018                                        WARN_STRICT_OVERFLOW_MISC);
12019
12020               sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
12021                                         sh_cnt,
12022                                         build_int_cst (TREE_TYPE (sh_cnt),
12023                                                        pow2));
12024               return fold_build2_loc (loc, RSHIFT_EXPR, type,
12025                                   fold_convert_loc (loc, type, arg0), sh_cnt);
12026             }
12027         }
12028
12029       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
12030          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
12031       if (INTEGRAL_TYPE_P (type)
12032           && TYPE_UNSIGNED (type)
12033           && code == FLOOR_DIV_EXPR)
12034         return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
12035
12036       /* Fall thru */
12037
12038     case ROUND_DIV_EXPR:
12039     case CEIL_DIV_EXPR:
12040     case EXACT_DIV_EXPR:
12041       if (integer_onep (arg1))
12042         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12043       if (integer_zerop (arg1))
12044         return NULL_TREE;
12045       /* X / -1 is -X.  */
12046       if (!TYPE_UNSIGNED (type)
12047           && TREE_CODE (arg1) == INTEGER_CST
12048           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
12049           && TREE_INT_CST_HIGH (arg1) == -1)
12050         return fold_convert_loc (loc, type, negate_expr (arg0));
12051
12052       /* Convert -A / -B to A / B when the type is signed and overflow is
12053          undefined.  */
12054       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12055           && TREE_CODE (arg0) == NEGATE_EXPR
12056           && negate_expr_p (arg1))
12057         {
12058           if (INTEGRAL_TYPE_P (type))
12059             fold_overflow_warning (("assuming signed overflow does not occur "
12060                                     "when distributing negation across "
12061                                     "division"),
12062                                    WARN_STRICT_OVERFLOW_MISC);
12063           return fold_build2_loc (loc, code, type,
12064                               fold_convert_loc (loc, type,
12065                                                 TREE_OPERAND (arg0, 0)),
12066                               fold_convert_loc (loc, type,
12067                                                 negate_expr (arg1)));
12068         }
12069       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12070           && TREE_CODE (arg1) == NEGATE_EXPR
12071           && negate_expr_p (arg0))
12072         {
12073           if (INTEGRAL_TYPE_P (type))
12074             fold_overflow_warning (("assuming signed overflow does not occur "
12075                                     "when distributing negation across "
12076                                     "division"),
12077                                    WARN_STRICT_OVERFLOW_MISC);
12078           return fold_build2_loc (loc, code, type,
12079                               fold_convert_loc (loc, type,
12080                                                 negate_expr (arg0)),
12081                               fold_convert_loc (loc, type,
12082                                                 TREE_OPERAND (arg1, 0)));
12083         }
12084
12085       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12086          operation, EXACT_DIV_EXPR.
12087
12088          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12089          At one time others generated faster code, it's not clear if they do
12090          after the last round to changes to the DIV code in expmed.c.  */
12091       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
12092           && multiple_of_p (type, arg0, arg1))
12093         return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
12094
12095       strict_overflow_p = false;
12096       if (TREE_CODE (arg1) == INTEGER_CST
12097           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12098                                          &strict_overflow_p)))
12099         {
12100           if (strict_overflow_p)
12101             fold_overflow_warning (("assuming signed overflow does not occur "
12102                                     "when simplifying division"),
12103                                    WARN_STRICT_OVERFLOW_MISC);
12104           return fold_convert_loc (loc, type, tem);
12105         }
12106
12107       return NULL_TREE;
12108
12109     case CEIL_MOD_EXPR:
12110     case FLOOR_MOD_EXPR:
12111     case ROUND_MOD_EXPR:
12112     case TRUNC_MOD_EXPR:
12113       /* X % 1 is always zero, but be sure to preserve any side
12114          effects in X.  */
12115       if (integer_onep (arg1))
12116         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12117
12118       /* X % 0, return X % 0 unchanged so that we can get the
12119          proper warnings and errors.  */
12120       if (integer_zerop (arg1))
12121         return NULL_TREE;
12122
12123       /* 0 % X is always zero, but be sure to preserve any side
12124          effects in X.  Place this after checking for X == 0.  */
12125       if (integer_zerop (arg0))
12126         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12127
12128       /* X % -1 is zero.  */
12129       if (!TYPE_UNSIGNED (type)
12130           && TREE_CODE (arg1) == INTEGER_CST
12131           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
12132           && TREE_INT_CST_HIGH (arg1) == -1)
12133         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12134
12135       /* X % -C is the same as X % C.  */
12136       if (code == TRUNC_MOD_EXPR
12137           && !TYPE_UNSIGNED (type)
12138           && TREE_CODE (arg1) == INTEGER_CST
12139           && !TREE_OVERFLOW (arg1)
12140           && TREE_INT_CST_HIGH (arg1) < 0
12141           && !TYPE_OVERFLOW_TRAPS (type)
12142           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
12143           && !sign_bit_p (arg1, arg1))
12144         return fold_build2_loc (loc, code, type,
12145                             fold_convert_loc (loc, type, arg0),
12146                             fold_convert_loc (loc, type,
12147                                               negate_expr (arg1)));
12148
12149       /* X % -Y is the same as X % Y.  */
12150       if (code == TRUNC_MOD_EXPR
12151           && !TYPE_UNSIGNED (type)
12152           && TREE_CODE (arg1) == NEGATE_EXPR
12153           && !TYPE_OVERFLOW_TRAPS (type))
12154         return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
12155                             fold_convert_loc (loc, type,
12156                                               TREE_OPERAND (arg1, 0)));
12157
12158       strict_overflow_p = false;
12159       if (TREE_CODE (arg1) == INTEGER_CST
12160           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12161                                          &strict_overflow_p)))
12162         {
12163           if (strict_overflow_p)
12164             fold_overflow_warning (("assuming signed overflow does not occur "
12165                                     "when simplifying modulus"),
12166                                    WARN_STRICT_OVERFLOW_MISC);
12167           return fold_convert_loc (loc, type, tem);
12168         }
12169
12170       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
12171          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
12172       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
12173           && (TYPE_UNSIGNED (type)
12174               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12175         {
12176           tree c = arg1;
12177           /* Also optimize A % (C << N)  where C is a power of 2,
12178              to A & ((C << N) - 1).  */
12179           if (TREE_CODE (arg1) == LSHIFT_EXPR)
12180             c = TREE_OPERAND (arg1, 0);
12181
12182           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
12183             {
12184               tree mask
12185                 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
12186                                    build_int_cst (TREE_TYPE (arg1), 1));
12187               if (strict_overflow_p)
12188                 fold_overflow_warning (("assuming signed overflow does not "
12189                                         "occur when simplifying "
12190                                         "X % (power of two)"),
12191                                        WARN_STRICT_OVERFLOW_MISC);
12192               return fold_build2_loc (loc, BIT_AND_EXPR, type,
12193                                       fold_convert_loc (loc, type, arg0),
12194                                       fold_convert_loc (loc, type, mask));
12195             }
12196         }
12197
12198       return NULL_TREE;
12199
12200     case LROTATE_EXPR:
12201     case RROTATE_EXPR:
12202       if (integer_all_onesp (arg0))
12203         return omit_one_operand_loc (loc, type, arg0, arg1);
12204       goto shift;
12205
12206     case RSHIFT_EXPR:
12207       /* Optimize -1 >> x for arithmetic right shifts.  */
12208       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
12209           && tree_expr_nonnegative_p (arg1))
12210         return omit_one_operand_loc (loc, type, arg0, arg1);
12211       /* ... fall through ...  */
12212
12213     case LSHIFT_EXPR:
12214     shift:
12215       if (integer_zerop (arg1))
12216         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12217       if (integer_zerop (arg0))
12218         return omit_one_operand_loc (loc, type, arg0, arg1);
12219
12220       /* Since negative shift count is not well-defined,
12221          don't try to compute it in the compiler.  */
12222       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
12223         return NULL_TREE;
12224
12225       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
12226       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
12227           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
12228           && host_integerp (TREE_OPERAND (arg0, 1), false)
12229           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
12230         {
12231           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
12232                                + TREE_INT_CST_LOW (arg1));
12233
12234           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
12235              being well defined.  */
12236           if (low >= TYPE_PRECISION (type))
12237             {
12238               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
12239                 low = low % TYPE_PRECISION (type);
12240               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
12241                 return omit_one_operand_loc (loc, type, build_int_cst (type, 0),
12242                                          TREE_OPERAND (arg0, 0));
12243               else
12244                 low = TYPE_PRECISION (type) - 1;
12245             }
12246
12247           return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12248                               build_int_cst (type, low));
12249         }
12250
12251       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
12252          into x & ((unsigned)-1 >> c) for unsigned types.  */
12253       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
12254            || (TYPE_UNSIGNED (type)
12255                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
12256           && host_integerp (arg1, false)
12257           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
12258           && host_integerp (TREE_OPERAND (arg0, 1), false)
12259           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
12260         {
12261           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
12262           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
12263           tree lshift;
12264           tree arg00;
12265
12266           if (low0 == low1)
12267             {
12268               arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12269
12270               lshift = build_int_cst (type, -1);
12271               lshift = int_const_binop (code, lshift, arg1);
12272
12273               return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
12274             }
12275         }
12276
12277       /* Rewrite an LROTATE_EXPR by a constant into an
12278          RROTATE_EXPR by a new constant.  */
12279       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
12280         {
12281           tree tem = build_int_cst (TREE_TYPE (arg1),
12282                                     TYPE_PRECISION (type));
12283           tem = const_binop (MINUS_EXPR, tem, arg1);
12284           return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
12285         }
12286
12287       /* If we have a rotate of a bit operation with the rotate count and
12288          the second operand of the bit operation both constant,
12289          permute the two operations.  */
12290       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12291           && (TREE_CODE (arg0) == BIT_AND_EXPR
12292               || TREE_CODE (arg0) == BIT_IOR_EXPR
12293               || TREE_CODE (arg0) == BIT_XOR_EXPR)
12294           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12295         return fold_build2_loc (loc, TREE_CODE (arg0), type,
12296                             fold_build2_loc (loc, code, type,
12297                                          TREE_OPERAND (arg0, 0), arg1),
12298                             fold_build2_loc (loc, code, type,
12299                                          TREE_OPERAND (arg0, 1), arg1));
12300
12301       /* Two consecutive rotates adding up to the precision of the
12302          type can be ignored.  */
12303       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12304           && TREE_CODE (arg0) == RROTATE_EXPR
12305           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12306           && TREE_INT_CST_HIGH (arg1) == 0
12307           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
12308           && ((TREE_INT_CST_LOW (arg1)
12309                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
12310               == (unsigned int) TYPE_PRECISION (type)))
12311         return TREE_OPERAND (arg0, 0);
12312
12313       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
12314               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
12315          if the latter can be further optimized.  */
12316       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
12317           && TREE_CODE (arg0) == BIT_AND_EXPR
12318           && TREE_CODE (arg1) == INTEGER_CST
12319           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12320         {
12321           tree mask = fold_build2_loc (loc, code, type,
12322                                    fold_convert_loc (loc, type,
12323                                                      TREE_OPERAND (arg0, 1)),
12324                                    arg1);
12325           tree shift = fold_build2_loc (loc, code, type,
12326                                     fold_convert_loc (loc, type,
12327                                                       TREE_OPERAND (arg0, 0)),
12328                                     arg1);
12329           tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
12330           if (tem)
12331             return tem;
12332         }
12333
12334       return NULL_TREE;
12335
12336     case MIN_EXPR:
12337       if (operand_equal_p (arg0, arg1, 0))
12338         return omit_one_operand_loc (loc, type, arg0, arg1);
12339       if (INTEGRAL_TYPE_P (type)
12340           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
12341         return omit_one_operand_loc (loc, type, arg1, arg0);
12342       tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
12343       if (tem)
12344         return tem;
12345       goto associate;
12346
12347     case MAX_EXPR:
12348       if (operand_equal_p (arg0, arg1, 0))
12349         return omit_one_operand_loc (loc, type, arg0, arg1);
12350       if (INTEGRAL_TYPE_P (type)
12351           && TYPE_MAX_VALUE (type)
12352           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
12353         return omit_one_operand_loc (loc, type, arg1, arg0);
12354       tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
12355       if (tem)
12356         return tem;
12357       goto associate;
12358
12359     case TRUTH_ANDIF_EXPR:
12360       /* Note that the operands of this must be ints
12361          and their values must be 0 or 1.
12362          ("true" is a fixed value perhaps depending on the language.)  */
12363       /* If first arg is constant zero, return it.  */
12364       if (integer_zerop (arg0))
12365         return fold_convert_loc (loc, type, arg0);
12366     case TRUTH_AND_EXPR:
12367       /* If either arg is constant true, drop it.  */
12368       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12369         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12370       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12371           /* Preserve sequence points.  */
12372           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12373         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12374       /* If second arg is constant zero, result is zero, but first arg
12375          must be evaluated.  */
12376       if (integer_zerop (arg1))
12377         return omit_one_operand_loc (loc, type, arg1, arg0);
12378       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12379          case will be handled here.  */
12380       if (integer_zerop (arg0))
12381         return omit_one_operand_loc (loc, type, arg0, arg1);
12382
12383       /* !X && X is always false.  */
12384       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12385           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12386         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12387       /* X && !X is always false.  */
12388       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12389           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12390         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12391
12392       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
12393          means A >= Y && A != MAX, but in this case we know that
12394          A < X <= MAX.  */
12395
12396       if (!TREE_SIDE_EFFECTS (arg0)
12397           && !TREE_SIDE_EFFECTS (arg1))
12398         {
12399           tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12400           if (tem && !operand_equal_p (tem, arg0, 0))
12401             return fold_build2_loc (loc, code, type, tem, arg1);
12402
12403           tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12404           if (tem && !operand_equal_p (tem, arg1, 0))
12405             return fold_build2_loc (loc, code, type, arg0, tem);
12406         }
12407
12408       if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12409           != NULL_TREE)
12410         return tem;
12411
12412       return NULL_TREE;
12413
12414     case TRUTH_ORIF_EXPR:
12415       /* Note that the operands of this must be ints
12416          and their values must be 0 or true.
12417          ("true" is a fixed value perhaps depending on the language.)  */
12418       /* If first arg is constant true, return it.  */
12419       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12420         return fold_convert_loc (loc, type, arg0);
12421     case TRUTH_OR_EXPR:
12422       /* If either arg is constant zero, drop it.  */
12423       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12424         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12425       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12426           /* Preserve sequence points.  */
12427           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12428         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12429       /* If second arg is constant true, result is true, but we must
12430          evaluate first arg.  */
12431       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12432         return omit_one_operand_loc (loc, type, arg1, arg0);
12433       /* Likewise for first arg, but note this only occurs here for
12434          TRUTH_OR_EXPR.  */
12435       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12436         return omit_one_operand_loc (loc, type, arg0, arg1);
12437
12438       /* !X || X is always true.  */
12439       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12440           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12441         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12442       /* X || !X is always true.  */
12443       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12444           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12445         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12446
12447       /* (X && !Y) || (!X && Y) is X ^ Y */
12448       if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12449           && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12450         {
12451           tree a0, a1, l0, l1, n0, n1;
12452
12453           a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12454           a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12455
12456           l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12457           l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12458           
12459           n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12460           n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12461           
12462           if ((operand_equal_p (n0, a0, 0)
12463                && operand_equal_p (n1, a1, 0))
12464               || (operand_equal_p (n0, a1, 0)
12465                   && operand_equal_p (n1, a0, 0)))
12466             return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12467         }
12468
12469       if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12470           != NULL_TREE)
12471         return tem;
12472
12473       return NULL_TREE;
12474
12475     case TRUTH_XOR_EXPR:
12476       /* If the second arg is constant zero, drop it.  */
12477       if (integer_zerop (arg1))
12478         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12479       /* If the second arg is constant true, this is a logical inversion.  */
12480       if (integer_onep (arg1))
12481         {
12482           /* Only call invert_truthvalue if operand is a truth value.  */
12483           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12484             tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12485           else
12486             tem = invert_truthvalue_loc (loc, arg0);
12487           return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12488         }
12489       /* Identical arguments cancel to zero.  */
12490       if (operand_equal_p (arg0, arg1, 0))
12491         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12492
12493       /* !X ^ X is always true.  */
12494       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12495           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12496         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12497
12498       /* X ^ !X is always true.  */
12499       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12500           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12501         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12502
12503       return NULL_TREE;
12504
12505     case EQ_EXPR:
12506     case NE_EXPR:
12507       STRIP_NOPS (arg0);
12508       STRIP_NOPS (arg1);
12509
12510       tem = fold_comparison (loc, code, type, op0, op1);
12511       if (tem != NULL_TREE)
12512         return tem;
12513
12514       /* bool_var != 0 becomes bool_var. */
12515       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12516           && code == NE_EXPR)
12517         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12518
12519       /* bool_var == 1 becomes bool_var. */
12520       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12521           && code == EQ_EXPR)
12522         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12523
12524       /* bool_var != 1 becomes !bool_var. */
12525       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12526           && code == NE_EXPR)
12527         return fold_convert_loc (loc, type,
12528                                  fold_build1_loc (loc, TRUTH_NOT_EXPR,
12529                                                   TREE_TYPE (arg0), arg0));
12530
12531       /* bool_var == 0 becomes !bool_var. */
12532       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12533           && code == EQ_EXPR)
12534         return fold_convert_loc (loc, type,
12535                                  fold_build1_loc (loc, TRUTH_NOT_EXPR,
12536                                                   TREE_TYPE (arg0), arg0));
12537
12538       /* !exp != 0 becomes !exp */
12539       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12540           && code == NE_EXPR)
12541         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12542
12543       /* If this is an equality comparison of the address of two non-weak,
12544          unaliased symbols neither of which are extern (since we do not
12545          have access to attributes for externs), then we know the result.  */
12546       if (TREE_CODE (arg0) == ADDR_EXPR
12547           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12548           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12549           && ! lookup_attribute ("alias",
12550                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12551           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12552           && TREE_CODE (arg1) == ADDR_EXPR
12553           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12554           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12555           && ! lookup_attribute ("alias",
12556                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12557           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12558         {
12559           /* We know that we're looking at the address of two
12560              non-weak, unaliased, static _DECL nodes.
12561
12562              It is both wasteful and incorrect to call operand_equal_p
12563              to compare the two ADDR_EXPR nodes.  It is wasteful in that
12564              all we need to do is test pointer equality for the arguments
12565              to the two ADDR_EXPR nodes.  It is incorrect to use
12566              operand_equal_p as that function is NOT equivalent to a
12567              C equality test.  It can in fact return false for two
12568              objects which would test as equal using the C equality
12569              operator.  */
12570           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12571           return constant_boolean_node (equal
12572                                         ? code == EQ_EXPR : code != EQ_EXPR,
12573                                         type);
12574         }
12575
12576       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12577          a MINUS_EXPR of a constant, we can convert it into a comparison with
12578          a revised constant as long as no overflow occurs.  */
12579       if (TREE_CODE (arg1) == INTEGER_CST
12580           && (TREE_CODE (arg0) == PLUS_EXPR
12581               || TREE_CODE (arg0) == MINUS_EXPR)
12582           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12583           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12584                                       ? MINUS_EXPR : PLUS_EXPR,
12585                                       fold_convert_loc (loc, TREE_TYPE (arg0),
12586                                                         arg1),
12587                                       TREE_OPERAND (arg0, 1)))
12588           && !TREE_OVERFLOW (tem))
12589         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12590
12591       /* Similarly for a NEGATE_EXPR.  */
12592       if (TREE_CODE (arg0) == NEGATE_EXPR
12593           && TREE_CODE (arg1) == INTEGER_CST
12594           && 0 != (tem = negate_expr (fold_convert_loc (loc, TREE_TYPE (arg0),
12595                                                         arg1)))
12596           && TREE_CODE (tem) == INTEGER_CST
12597           && !TREE_OVERFLOW (tem))
12598         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12599
12600       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
12601       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12602           && TREE_CODE (arg1) == INTEGER_CST
12603           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12604         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12605                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12606                                          fold_convert_loc (loc,
12607                                                            TREE_TYPE (arg0),
12608                                                            arg1),
12609                                          TREE_OPERAND (arg0, 1)));
12610
12611       /* Transform comparisons of the form X +- Y CMP X to Y CMP 0.  */
12612       if ((TREE_CODE (arg0) == PLUS_EXPR
12613            || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12614            || TREE_CODE (arg0) == MINUS_EXPR)
12615           && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12616                                                                         0)),
12617                               arg1, 0)
12618           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12619               || POINTER_TYPE_P (TREE_TYPE (arg0))))
12620         {
12621           tree val = TREE_OPERAND (arg0, 1);
12622           return omit_two_operands_loc (loc, type,
12623                                     fold_build2_loc (loc, code, type,
12624                                                  val,
12625                                                  build_int_cst (TREE_TYPE (val),
12626                                                                 0)),
12627                                     TREE_OPERAND (arg0, 0), arg1);
12628         }
12629
12630       /* Transform comparisons of the form C - X CMP X if C % 2 == 1.  */
12631       if (TREE_CODE (arg0) == MINUS_EXPR
12632           && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12633           && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12634                                                                         1)),
12635                               arg1, 0)
12636           && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12637         {
12638           return omit_two_operands_loc (loc, type,
12639                                     code == NE_EXPR
12640                                     ? boolean_true_node : boolean_false_node,
12641                                     TREE_OPERAND (arg0, 1), arg1);
12642         }
12643
12644       /* If we have X - Y == 0, we can convert that to X == Y and similarly
12645          for !=.  Don't do this for ordered comparisons due to overflow.  */
12646       if (TREE_CODE (arg0) == MINUS_EXPR
12647           && integer_zerop (arg1))
12648         return fold_build2_loc (loc, code, type,
12649                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12650
12651       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
12652       if (TREE_CODE (arg0) == ABS_EXPR
12653           && (integer_zerop (arg1) || real_zerop (arg1)))
12654         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12655
12656       /* If this is an EQ or NE comparison with zero and ARG0 is
12657          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
12658          two operations, but the latter can be done in one less insn
12659          on machines that have only two-operand insns or on which a
12660          constant cannot be the first operand.  */
12661       if (TREE_CODE (arg0) == BIT_AND_EXPR
12662           && integer_zerop (arg1))
12663         {
12664           tree arg00 = TREE_OPERAND (arg0, 0);
12665           tree arg01 = TREE_OPERAND (arg0, 1);
12666           if (TREE_CODE (arg00) == LSHIFT_EXPR
12667               && integer_onep (TREE_OPERAND (arg00, 0)))
12668             {
12669               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12670                                       arg01, TREE_OPERAND (arg00, 1));
12671               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12672                                  build_int_cst (TREE_TYPE (arg0), 1));
12673               return fold_build2_loc (loc, code, type,
12674                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12675                                   arg1);
12676             }
12677           else if (TREE_CODE (arg01) == LSHIFT_EXPR
12678                    && integer_onep (TREE_OPERAND (arg01, 0)))
12679             {
12680               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12681                                       arg00, TREE_OPERAND (arg01, 1));
12682               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12683                                  build_int_cst (TREE_TYPE (arg0), 1));
12684               return fold_build2_loc (loc, code, type,
12685                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12686                                   arg1);
12687             }
12688         }
12689
12690       /* If this is an NE or EQ comparison of zero against the result of a
12691          signed MOD operation whose second operand is a power of 2, make
12692          the MOD operation unsigned since it is simpler and equivalent.  */
12693       if (integer_zerop (arg1)
12694           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12695           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12696               || TREE_CODE (arg0) == CEIL_MOD_EXPR
12697               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12698               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12699           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12700         {
12701           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12702           tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12703                                      fold_convert_loc (loc, newtype,
12704                                                        TREE_OPERAND (arg0, 0)),
12705                                      fold_convert_loc (loc, newtype,
12706                                                        TREE_OPERAND (arg0, 1)));
12707
12708           return fold_build2_loc (loc, code, type, newmod,
12709                               fold_convert_loc (loc, newtype, arg1));
12710         }
12711
12712       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12713          C1 is a valid shift constant, and C2 is a power of two, i.e.
12714          a single bit.  */
12715       if (TREE_CODE (arg0) == BIT_AND_EXPR
12716           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12717           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12718              == INTEGER_CST
12719           && integer_pow2p (TREE_OPERAND (arg0, 1))
12720           && integer_zerop (arg1))
12721         {
12722           tree itype = TREE_TYPE (arg0);
12723           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12724           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12725
12726           /* Check for a valid shift count.  */
12727           if (TREE_INT_CST_HIGH (arg001) == 0
12728               && TREE_INT_CST_LOW (arg001) < prec)
12729             {
12730               tree arg01 = TREE_OPERAND (arg0, 1);
12731               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12732               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12733               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12734                  can be rewritten as (X & (C2 << C1)) != 0.  */
12735               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12736                 {
12737                   tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12738                   tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12739                   return fold_build2_loc (loc, code, type, tem,
12740                                           fold_convert_loc (loc, itype, arg1));
12741                 }
12742               /* Otherwise, for signed (arithmetic) shifts,
12743                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12744                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
12745               else if (!TYPE_UNSIGNED (itype))
12746                 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12747                                     arg000, build_int_cst (itype, 0));
12748               /* Otherwise, of unsigned (logical) shifts,
12749                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12750                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
12751               else
12752                 return omit_one_operand_loc (loc, type,
12753                                          code == EQ_EXPR ? integer_one_node
12754                                                          : integer_zero_node,
12755                                          arg000);
12756             }
12757         }
12758
12759       /* If we have (A & C) == C where C is a power of 2, convert this into
12760          (A & C) != 0.  Similarly for NE_EXPR.  */
12761       if (TREE_CODE (arg0) == BIT_AND_EXPR
12762           && integer_pow2p (TREE_OPERAND (arg0, 1))
12763           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12764         return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12765                             arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12766                                                     integer_zero_node));
12767
12768       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12769          bit, then fold the expression into A < 0 or A >= 0.  */
12770       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12771       if (tem)
12772         return tem;
12773
12774       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12775          Similarly for NE_EXPR.  */
12776       if (TREE_CODE (arg0) == BIT_AND_EXPR
12777           && TREE_CODE (arg1) == INTEGER_CST
12778           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12779         {
12780           tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12781                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
12782                                    TREE_OPERAND (arg0, 1));
12783           tree dandnotc
12784             = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12785                                fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
12786                                notc);
12787           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12788           if (integer_nonzerop (dandnotc))
12789             return omit_one_operand_loc (loc, type, rslt, arg0);
12790         }
12791
12792       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12793          Similarly for NE_EXPR.  */
12794       if (TREE_CODE (arg0) == BIT_IOR_EXPR
12795           && TREE_CODE (arg1) == INTEGER_CST
12796           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12797         {
12798           tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12799           tree candnotd
12800             = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12801                                TREE_OPERAND (arg0, 1),
12802                                fold_convert_loc (loc, TREE_TYPE (arg0), notd));
12803           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12804           if (integer_nonzerop (candnotd))
12805             return omit_one_operand_loc (loc, type, rslt, arg0);
12806         }
12807
12808       /* If this is a comparison of a field, we may be able to simplify it.  */
12809       if ((TREE_CODE (arg0) == COMPONENT_REF
12810            || TREE_CODE (arg0) == BIT_FIELD_REF)
12811           /* Handle the constant case even without -O
12812              to make sure the warnings are given.  */
12813           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12814         {
12815           t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12816           if (t1)
12817             return t1;
12818         }
12819
12820       /* Optimize comparisons of strlen vs zero to a compare of the
12821          first character of the string vs zero.  To wit,
12822                 strlen(ptr) == 0   =>  *ptr == 0
12823                 strlen(ptr) != 0   =>  *ptr != 0
12824          Other cases should reduce to one of these two (or a constant)
12825          due to the return value of strlen being unsigned.  */
12826       if (TREE_CODE (arg0) == CALL_EXPR
12827           && integer_zerop (arg1))
12828         {
12829           tree fndecl = get_callee_fndecl (arg0);
12830
12831           if (fndecl
12832               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12833               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12834               && call_expr_nargs (arg0) == 1
12835               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12836             {
12837               tree iref = build_fold_indirect_ref_loc (loc,
12838                                                    CALL_EXPR_ARG (arg0, 0));
12839               return fold_build2_loc (loc, code, type, iref,
12840                                   build_int_cst (TREE_TYPE (iref), 0));
12841             }
12842         }
12843
12844       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12845          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12846       if (TREE_CODE (arg0) == RSHIFT_EXPR
12847           && integer_zerop (arg1)
12848           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12849         {
12850           tree arg00 = TREE_OPERAND (arg0, 0);
12851           tree arg01 = TREE_OPERAND (arg0, 1);
12852           tree itype = TREE_TYPE (arg00);
12853           if (TREE_INT_CST_HIGH (arg01) == 0
12854               && TREE_INT_CST_LOW (arg01)
12855                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12856             {
12857               if (TYPE_UNSIGNED (itype))
12858                 {
12859                   itype = signed_type_for (itype);
12860                   arg00 = fold_convert_loc (loc, itype, arg00);
12861                 }
12862               return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12863                                   type, arg00, build_int_cst (itype, 0));
12864             }
12865         }
12866
12867       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12868       if (integer_zerop (arg1)
12869           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12870         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12871                             TREE_OPERAND (arg0, 1));
12872
12873       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12874       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12875           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12876         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12877                                 build_zero_cst (TREE_TYPE (arg0)));
12878       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12879       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12880           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12881           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12882         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12883                                 build_zero_cst (TREE_TYPE (arg0)));
12884
12885       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12886       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12887           && TREE_CODE (arg1) == INTEGER_CST
12888           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12889         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12890                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12891                                          TREE_OPERAND (arg0, 1), arg1));
12892
12893       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12894          (X & C) == 0 when C is a single bit.  */
12895       if (TREE_CODE (arg0) == BIT_AND_EXPR
12896           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12897           && integer_zerop (arg1)
12898           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12899         {
12900           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12901                                  TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12902                                  TREE_OPERAND (arg0, 1));
12903           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12904                                   type, tem,
12905                                   fold_convert_loc (loc, TREE_TYPE (arg0),
12906                                                     arg1));
12907         }
12908
12909       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12910          constant C is a power of two, i.e. a single bit.  */
12911       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12912           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12913           && integer_zerop (arg1)
12914           && integer_pow2p (TREE_OPERAND (arg0, 1))
12915           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12916                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12917         {
12918           tree arg00 = TREE_OPERAND (arg0, 0);
12919           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12920                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
12921         }
12922
12923       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12924          when is C is a power of two, i.e. a single bit.  */
12925       if (TREE_CODE (arg0) == BIT_AND_EXPR
12926           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12927           && integer_zerop (arg1)
12928           && integer_pow2p (TREE_OPERAND (arg0, 1))
12929           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12930                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12931         {
12932           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12933           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12934                              arg000, TREE_OPERAND (arg0, 1));
12935           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12936                               tem, build_int_cst (TREE_TYPE (tem), 0));
12937         }
12938
12939       if (integer_zerop (arg1)
12940           && tree_expr_nonzero_p (arg0))
12941         {
12942           tree res = constant_boolean_node (code==NE_EXPR, type);
12943           return omit_one_operand_loc (loc, type, res, arg0);
12944         }
12945
12946       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
12947       if (TREE_CODE (arg0) == NEGATE_EXPR
12948           && TREE_CODE (arg1) == NEGATE_EXPR)
12949         return fold_build2_loc (loc, code, type,
12950                                 TREE_OPERAND (arg0, 0),
12951                                 fold_convert_loc (loc, TREE_TYPE (arg0),
12952                                                   TREE_OPERAND (arg1, 0)));
12953
12954       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
12955       if (TREE_CODE (arg0) == BIT_AND_EXPR
12956           && TREE_CODE (arg1) == BIT_AND_EXPR)
12957         {
12958           tree arg00 = TREE_OPERAND (arg0, 0);
12959           tree arg01 = TREE_OPERAND (arg0, 1);
12960           tree arg10 = TREE_OPERAND (arg1, 0);
12961           tree arg11 = TREE_OPERAND (arg1, 1);
12962           tree itype = TREE_TYPE (arg0);
12963
12964           if (operand_equal_p (arg01, arg11, 0))
12965             return fold_build2_loc (loc, code, type,
12966                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12967                                              fold_build2_loc (loc,
12968                                                           BIT_XOR_EXPR, itype,
12969                                                           arg00, arg10),
12970                                              arg01),
12971                                 build_zero_cst (itype));
12972
12973           if (operand_equal_p (arg01, arg10, 0))
12974             return fold_build2_loc (loc, code, type,
12975                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12976                                              fold_build2_loc (loc,
12977                                                           BIT_XOR_EXPR, itype,
12978                                                           arg00, arg11),
12979                                              arg01),
12980                                 build_zero_cst (itype));
12981
12982           if (operand_equal_p (arg00, arg11, 0))
12983             return fold_build2_loc (loc, code, type,
12984                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12985                                              fold_build2_loc (loc,
12986                                                           BIT_XOR_EXPR, itype,
12987                                                           arg01, arg10),
12988                                              arg00),
12989                                 build_zero_cst (itype));
12990
12991           if (operand_equal_p (arg00, arg10, 0))
12992             return fold_build2_loc (loc, code, type,
12993                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12994                                              fold_build2_loc (loc,
12995                                                           BIT_XOR_EXPR, itype,
12996                                                           arg01, arg11),
12997                                              arg00),
12998                                 build_zero_cst (itype));
12999         }
13000
13001       if (TREE_CODE (arg0) == BIT_XOR_EXPR
13002           && TREE_CODE (arg1) == BIT_XOR_EXPR)
13003         {
13004           tree arg00 = TREE_OPERAND (arg0, 0);
13005           tree arg01 = TREE_OPERAND (arg0, 1);
13006           tree arg10 = TREE_OPERAND (arg1, 0);
13007           tree arg11 = TREE_OPERAND (arg1, 1);
13008           tree itype = TREE_TYPE (arg0);
13009
13010           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
13011              operand_equal_p guarantees no side-effects so we don't need
13012              to use omit_one_operand on Z.  */
13013           if (operand_equal_p (arg01, arg11, 0))
13014             return fold_build2_loc (loc, code, type, arg00,
13015                                     fold_convert_loc (loc, TREE_TYPE (arg00),
13016                                                       arg10));
13017           if (operand_equal_p (arg01, arg10, 0))
13018             return fold_build2_loc (loc, code, type, arg00,
13019                                     fold_convert_loc (loc, TREE_TYPE (arg00),
13020                                                       arg11));
13021           if (operand_equal_p (arg00, arg11, 0))
13022             return fold_build2_loc (loc, code, type, arg01,
13023                                     fold_convert_loc (loc, TREE_TYPE (arg01),
13024                                                       arg10));
13025           if (operand_equal_p (arg00, arg10, 0))
13026             return fold_build2_loc (loc, code, type, arg01,
13027                                     fold_convert_loc (loc, TREE_TYPE (arg01),
13028                                                       arg11));
13029
13030           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
13031           if (TREE_CODE (arg01) == INTEGER_CST
13032               && TREE_CODE (arg11) == INTEGER_CST)
13033             {
13034               tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
13035                                      fold_convert_loc (loc, itype, arg11));
13036               tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
13037               return fold_build2_loc (loc, code, type, tem,
13038                                       fold_convert_loc (loc, itype, arg10));
13039             }
13040         }
13041
13042       /* Attempt to simplify equality/inequality comparisons of complex
13043          values.  Only lower the comparison if the result is known or
13044          can be simplified to a single scalar comparison.  */
13045       if ((TREE_CODE (arg0) == COMPLEX_EXPR
13046            || TREE_CODE (arg0) == COMPLEX_CST)
13047           && (TREE_CODE (arg1) == COMPLEX_EXPR
13048               || TREE_CODE (arg1) == COMPLEX_CST))
13049         {
13050           tree real0, imag0, real1, imag1;
13051           tree rcond, icond;
13052
13053           if (TREE_CODE (arg0) == COMPLEX_EXPR)
13054             {
13055               real0 = TREE_OPERAND (arg0, 0);
13056               imag0 = TREE_OPERAND (arg0, 1);
13057             }
13058           else
13059             {
13060               real0 = TREE_REALPART (arg0);
13061               imag0 = TREE_IMAGPART (arg0);
13062             }
13063
13064           if (TREE_CODE (arg1) == COMPLEX_EXPR)
13065             {
13066               real1 = TREE_OPERAND (arg1, 0);
13067               imag1 = TREE_OPERAND (arg1, 1);
13068             }
13069           else
13070             {
13071               real1 = TREE_REALPART (arg1);
13072               imag1 = TREE_IMAGPART (arg1);
13073             }
13074
13075           rcond = fold_binary_loc (loc, code, type, real0, real1);
13076           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
13077             {
13078               if (integer_zerop (rcond))
13079                 {
13080                   if (code == EQ_EXPR)
13081                     return omit_two_operands_loc (loc, type, boolean_false_node,
13082                                               imag0, imag1);
13083                   return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
13084                 }
13085               else
13086                 {
13087                   if (code == NE_EXPR)
13088                     return omit_two_operands_loc (loc, type, boolean_true_node,
13089                                               imag0, imag1);
13090                   return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
13091                 }
13092             }
13093
13094           icond = fold_binary_loc (loc, code, type, imag0, imag1);
13095           if (icond && TREE_CODE (icond) == INTEGER_CST)
13096             {
13097               if (integer_zerop (icond))
13098                 {
13099                   if (code == EQ_EXPR)
13100                     return omit_two_operands_loc (loc, type, boolean_false_node,
13101                                               real0, real1);
13102                   return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
13103                 }
13104               else
13105                 {
13106                   if (code == NE_EXPR)
13107                     return omit_two_operands_loc (loc, type, boolean_true_node,
13108                                               real0, real1);
13109                   return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
13110                 }
13111             }
13112         }
13113
13114       return NULL_TREE;
13115
13116     case LT_EXPR:
13117     case GT_EXPR:
13118     case LE_EXPR:
13119     case GE_EXPR:
13120       tem = fold_comparison (loc, code, type, op0, op1);
13121       if (tem != NULL_TREE)
13122         return tem;
13123
13124       /* Transform comparisons of the form X +- C CMP X.  */
13125       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
13126           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
13127           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
13128                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
13129               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
13130                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
13131         {
13132           tree arg01 = TREE_OPERAND (arg0, 1);
13133           enum tree_code code0 = TREE_CODE (arg0);
13134           int is_positive;
13135
13136           if (TREE_CODE (arg01) == REAL_CST)
13137             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
13138           else
13139             is_positive = tree_int_cst_sgn (arg01);
13140
13141           /* (X - c) > X becomes false.  */
13142           if (code == GT_EXPR
13143               && ((code0 == MINUS_EXPR && is_positive >= 0)
13144                   || (code0 == PLUS_EXPR && is_positive <= 0)))
13145             {
13146               if (TREE_CODE (arg01) == INTEGER_CST
13147                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13148                 fold_overflow_warning (("assuming signed overflow does not "
13149                                         "occur when assuming that (X - c) > X "
13150                                         "is always false"),
13151                                        WARN_STRICT_OVERFLOW_ALL);
13152               return constant_boolean_node (0, type);
13153             }
13154
13155           /* Likewise (X + c) < X becomes false.  */
13156           if (code == LT_EXPR
13157               && ((code0 == PLUS_EXPR && is_positive >= 0)
13158                   || (code0 == MINUS_EXPR && is_positive <= 0)))
13159             {
13160               if (TREE_CODE (arg01) == INTEGER_CST
13161                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13162                 fold_overflow_warning (("assuming signed overflow does not "
13163                                         "occur when assuming that "
13164                                         "(X + c) < X is always false"),
13165                                        WARN_STRICT_OVERFLOW_ALL);
13166               return constant_boolean_node (0, type);
13167             }
13168
13169           /* Convert (X - c) <= X to true.  */
13170           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
13171               && code == LE_EXPR
13172               && ((code0 == MINUS_EXPR && is_positive >= 0)
13173                   || (code0 == PLUS_EXPR && is_positive <= 0)))
13174             {
13175               if (TREE_CODE (arg01) == INTEGER_CST
13176                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13177                 fold_overflow_warning (("assuming signed overflow does not "
13178                                         "occur when assuming that "
13179                                         "(X - c) <= X is always true"),
13180                                        WARN_STRICT_OVERFLOW_ALL);
13181               return constant_boolean_node (1, type);
13182             }
13183
13184           /* Convert (X + c) >= X to true.  */
13185           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
13186               && code == GE_EXPR
13187               && ((code0 == PLUS_EXPR && is_positive >= 0)
13188                   || (code0 == MINUS_EXPR && is_positive <= 0)))
13189             {
13190               if (TREE_CODE (arg01) == INTEGER_CST
13191                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13192                 fold_overflow_warning (("assuming signed overflow does not "
13193                                         "occur when assuming that "
13194                                         "(X + c) >= X is always true"),
13195                                        WARN_STRICT_OVERFLOW_ALL);
13196               return constant_boolean_node (1, type);
13197             }
13198
13199           if (TREE_CODE (arg01) == INTEGER_CST)
13200             {
13201               /* Convert X + c > X and X - c < X to true for integers.  */
13202               if (code == GT_EXPR
13203                   && ((code0 == PLUS_EXPR && is_positive > 0)
13204                       || (code0 == MINUS_EXPR && is_positive < 0)))
13205                 {
13206                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13207                     fold_overflow_warning (("assuming signed overflow does "
13208                                             "not occur when assuming that "
13209                                             "(X + c) > X is always true"),
13210                                            WARN_STRICT_OVERFLOW_ALL);
13211                   return constant_boolean_node (1, type);
13212                 }
13213
13214               if (code == LT_EXPR
13215                   && ((code0 == MINUS_EXPR && is_positive > 0)
13216                       || (code0 == PLUS_EXPR && is_positive < 0)))
13217                 {
13218                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13219                     fold_overflow_warning (("assuming signed overflow does "
13220                                             "not occur when assuming that "
13221                                             "(X - c) < X is always true"),
13222                                            WARN_STRICT_OVERFLOW_ALL);
13223                   return constant_boolean_node (1, type);
13224                 }
13225
13226               /* Convert X + c <= X and X - c >= X to false for integers.  */
13227               if (code == LE_EXPR
13228                   && ((code0 == PLUS_EXPR && is_positive > 0)
13229                       || (code0 == MINUS_EXPR && is_positive < 0)))
13230                 {
13231                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13232                     fold_overflow_warning (("assuming signed overflow does "
13233                                             "not occur when assuming that "
13234                                             "(X + c) <= X is always false"),
13235                                            WARN_STRICT_OVERFLOW_ALL);
13236                   return constant_boolean_node (0, type);
13237                 }
13238
13239               if (code == GE_EXPR
13240                   && ((code0 == MINUS_EXPR && is_positive > 0)
13241                       || (code0 == PLUS_EXPR && is_positive < 0)))
13242                 {
13243                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13244                     fold_overflow_warning (("assuming signed overflow does "
13245                                             "not occur when assuming that "
13246                                             "(X - c) >= X is always false"),
13247                                            WARN_STRICT_OVERFLOW_ALL);
13248                   return constant_boolean_node (0, type);
13249                 }
13250             }
13251         }
13252
13253       /* Comparisons with the highest or lowest possible integer of
13254          the specified precision will have known values.  */
13255       {
13256         tree arg1_type = TREE_TYPE (arg1);
13257         unsigned int width = TYPE_PRECISION (arg1_type);
13258
13259         if (TREE_CODE (arg1) == INTEGER_CST
13260             && width <= 2 * HOST_BITS_PER_WIDE_INT
13261             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
13262           {
13263             HOST_WIDE_INT signed_max_hi;
13264             unsigned HOST_WIDE_INT signed_max_lo;
13265             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
13266
13267             if (width <= HOST_BITS_PER_WIDE_INT)
13268               {
13269                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
13270                                 - 1;
13271                 signed_max_hi = 0;
13272                 max_hi = 0;
13273
13274                 if (TYPE_UNSIGNED (arg1_type))
13275                   {
13276                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
13277                     min_lo = 0;
13278                     min_hi = 0;
13279                   }
13280                 else
13281                   {
13282                     max_lo = signed_max_lo;
13283                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
13284                     min_hi = -1;
13285                   }
13286               }
13287             else
13288               {
13289                 width -= HOST_BITS_PER_WIDE_INT;
13290                 signed_max_lo = -1;
13291                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
13292                                 - 1;
13293                 max_lo = -1;
13294                 min_lo = 0;
13295
13296                 if (TYPE_UNSIGNED (arg1_type))
13297                   {
13298                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
13299                     min_hi = 0;
13300                   }
13301                 else
13302                   {
13303                     max_hi = signed_max_hi;
13304                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
13305                   }
13306               }
13307
13308             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
13309                 && TREE_INT_CST_LOW (arg1) == max_lo)
13310               switch (code)
13311                 {
13312                 case GT_EXPR:
13313                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13314
13315                 case GE_EXPR:
13316                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13317
13318                 case LE_EXPR:
13319                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13320
13321                 case LT_EXPR:
13322                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13323
13324                 /* The GE_EXPR and LT_EXPR cases above are not normally
13325                    reached because of previous transformations.  */
13326
13327                 default:
13328                   break;
13329                 }
13330             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13331                      == max_hi
13332                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
13333               switch (code)
13334                 {
13335                 case GT_EXPR:
13336                   arg1 = const_binop (PLUS_EXPR, arg1,
13337                                       build_int_cst (TREE_TYPE (arg1), 1));
13338                   return fold_build2_loc (loc, EQ_EXPR, type,
13339                                       fold_convert_loc (loc,
13340                                                         TREE_TYPE (arg1), arg0),
13341                                       arg1);
13342                 case LE_EXPR:
13343                   arg1 = const_binop (PLUS_EXPR, arg1,
13344                                       build_int_cst (TREE_TYPE (arg1), 1));
13345                   return fold_build2_loc (loc, NE_EXPR, type,
13346                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13347                                                         arg0),
13348                                       arg1);
13349                 default:
13350                   break;
13351                 }
13352             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13353                      == min_hi
13354                      && TREE_INT_CST_LOW (arg1) == min_lo)
13355               switch (code)
13356                 {
13357                 case LT_EXPR:
13358                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13359
13360                 case LE_EXPR:
13361                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13362
13363                 case GE_EXPR:
13364                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13365
13366                 case GT_EXPR:
13367                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13368
13369                 default:
13370                   break;
13371                 }
13372             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13373                      == min_hi
13374                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
13375               switch (code)
13376                 {
13377                 case GE_EXPR:
13378                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
13379                   return fold_build2_loc (loc, NE_EXPR, type,
13380                                       fold_convert_loc (loc,
13381                                                         TREE_TYPE (arg1), arg0),
13382                                       arg1);
13383                 case LT_EXPR:
13384                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
13385                   return fold_build2_loc (loc, EQ_EXPR, type,
13386                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13387                                                         arg0),
13388                                       arg1);
13389                 default:
13390                   break;
13391                 }
13392
13393             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13394                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
13395                      && TYPE_UNSIGNED (arg1_type)
13396                      /* We will flip the signedness of the comparison operator
13397                         associated with the mode of arg1, so the sign bit is
13398                         specified by this mode.  Check that arg1 is the signed
13399                         max associated with this sign bit.  */
13400                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13401                      /* signed_type does not work on pointer types.  */
13402                      && INTEGRAL_TYPE_P (arg1_type))
13403               {
13404                 /* The following case also applies to X < signed_max+1
13405                    and X >= signed_max+1 because previous transformations.  */
13406                 if (code == LE_EXPR || code == GT_EXPR)
13407                   {
13408                     tree st;
13409                     st = signed_type_for (TREE_TYPE (arg1));
13410                     return fold_build2_loc (loc,
13411                                         code == LE_EXPR ? GE_EXPR : LT_EXPR,
13412                                         type, fold_convert_loc (loc, st, arg0),
13413                                         build_int_cst (st, 0));
13414                   }
13415               }
13416           }
13417       }
13418
13419       /* If we are comparing an ABS_EXPR with a constant, we can
13420          convert all the cases into explicit comparisons, but they may
13421          well not be faster than doing the ABS and one comparison.
13422          But ABS (X) <= C is a range comparison, which becomes a subtraction
13423          and a comparison, and is probably faster.  */
13424       if (code == LE_EXPR
13425           && TREE_CODE (arg1) == INTEGER_CST
13426           && TREE_CODE (arg0) == ABS_EXPR
13427           && ! TREE_SIDE_EFFECTS (arg0)
13428           && (0 != (tem = negate_expr (arg1)))
13429           && TREE_CODE (tem) == INTEGER_CST
13430           && !TREE_OVERFLOW (tem))
13431         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13432                             build2 (GE_EXPR, type,
13433                                     TREE_OPERAND (arg0, 0), tem),
13434                             build2 (LE_EXPR, type,
13435                                     TREE_OPERAND (arg0, 0), arg1));
13436
13437       /* Convert ABS_EXPR<x> >= 0 to true.  */
13438       strict_overflow_p = false;
13439       if (code == GE_EXPR
13440           && (integer_zerop (arg1)
13441               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13442                   && real_zerop (arg1)))
13443           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13444         {
13445           if (strict_overflow_p)
13446             fold_overflow_warning (("assuming signed overflow does not occur "
13447                                     "when simplifying comparison of "
13448                                     "absolute value and zero"),
13449                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13450           return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13451         }
13452
13453       /* Convert ABS_EXPR<x> < 0 to false.  */
13454       strict_overflow_p = false;
13455       if (code == LT_EXPR
13456           && (integer_zerop (arg1) || real_zerop (arg1))
13457           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13458         {
13459           if (strict_overflow_p)
13460             fold_overflow_warning (("assuming signed overflow does not occur "
13461                                     "when simplifying comparison of "
13462                                     "absolute value and zero"),
13463                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13464           return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13465         }
13466
13467       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13468          and similarly for >= into !=.  */
13469       if ((code == LT_EXPR || code == GE_EXPR)
13470           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13471           && TREE_CODE (arg1) == LSHIFT_EXPR
13472           && integer_onep (TREE_OPERAND (arg1, 0)))
13473         return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13474                            build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13475                                    TREE_OPERAND (arg1, 1)),
13476                            build_int_cst (TREE_TYPE (arg0), 0));
13477
13478       /* Similarly for X < (cast) (1 << Y).  But cast can't be narrowing,
13479          otherwise Y might be >= # of bits in X's type and thus e.g.
13480          (unsigned char) (1 << Y) for Y 15 might be 0.
13481          If the cast is widening, then 1 << Y should have unsigned type,
13482          otherwise if Y is number of bits in the signed shift type minus 1,
13483          we can't optimize this.  E.g. (unsigned long long) (1 << Y) for Y
13484          31 might be 0xffffffff80000000.  */
13485       if ((code == LT_EXPR || code == GE_EXPR)
13486           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13487           && CONVERT_EXPR_P (arg1)
13488           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13489           && (TYPE_PRECISION (TREE_TYPE (arg1))
13490               >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0))))
13491           && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
13492               || (TYPE_PRECISION (TREE_TYPE (arg1))
13493                   == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
13494           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13495         {
13496           tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13497                         TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
13498           return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13499                              fold_convert_loc (loc, TREE_TYPE (arg0), tem),
13500                              build_int_cst (TREE_TYPE (arg0), 0));
13501         }
13502
13503       return NULL_TREE;
13504
13505     case UNORDERED_EXPR:
13506     case ORDERED_EXPR:
13507     case UNLT_EXPR:
13508     case UNLE_EXPR:
13509     case UNGT_EXPR:
13510     case UNGE_EXPR:
13511     case UNEQ_EXPR:
13512     case LTGT_EXPR:
13513       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13514         {
13515           t1 = fold_relational_const (code, type, arg0, arg1);
13516           if (t1 != NULL_TREE)
13517             return t1;
13518         }
13519
13520       /* If the first operand is NaN, the result is constant.  */
13521       if (TREE_CODE (arg0) == REAL_CST
13522           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13523           && (code != LTGT_EXPR || ! flag_trapping_math))
13524         {
13525           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13526                ? integer_zero_node
13527                : integer_one_node;
13528           return omit_one_operand_loc (loc, type, t1, arg1);
13529         }
13530
13531       /* If the second operand is NaN, the result is constant.  */
13532       if (TREE_CODE (arg1) == REAL_CST
13533           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13534           && (code != LTGT_EXPR || ! flag_trapping_math))
13535         {
13536           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13537                ? integer_zero_node
13538                : integer_one_node;
13539           return omit_one_operand_loc (loc, type, t1, arg0);
13540         }
13541
13542       /* Simplify unordered comparison of something with itself.  */
13543       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13544           && operand_equal_p (arg0, arg1, 0))
13545         return constant_boolean_node (1, type);
13546
13547       if (code == LTGT_EXPR
13548           && !flag_trapping_math
13549           && operand_equal_p (arg0, arg1, 0))
13550         return constant_boolean_node (0, type);
13551
13552       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
13553       {
13554         tree targ0 = strip_float_extensions (arg0);
13555         tree targ1 = strip_float_extensions (arg1);
13556         tree newtype = TREE_TYPE (targ0);
13557
13558         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13559           newtype = TREE_TYPE (targ1);
13560
13561         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13562           return fold_build2_loc (loc, code, type,
13563                               fold_convert_loc (loc, newtype, targ0),
13564                               fold_convert_loc (loc, newtype, targ1));
13565       }
13566
13567       return NULL_TREE;
13568
13569     case COMPOUND_EXPR:
13570       /* When pedantic, a compound expression can be neither an lvalue
13571          nor an integer constant expression.  */
13572       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13573         return NULL_TREE;
13574       /* Don't let (0, 0) be null pointer constant.  */
13575       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13576                                  : fold_convert_loc (loc, type, arg1);
13577       return pedantic_non_lvalue_loc (loc, tem);
13578
13579     case COMPLEX_EXPR:
13580       if ((TREE_CODE (arg0) == REAL_CST
13581            && TREE_CODE (arg1) == REAL_CST)
13582           || (TREE_CODE (arg0) == INTEGER_CST
13583               && TREE_CODE (arg1) == INTEGER_CST))
13584         return build_complex (type, arg0, arg1);
13585       if (TREE_CODE (arg0) == REALPART_EXPR
13586           && TREE_CODE (arg1) == IMAGPART_EXPR
13587           && TREE_TYPE (TREE_OPERAND (arg0, 0)) == type
13588           && operand_equal_p (TREE_OPERAND (arg0, 0),
13589                               TREE_OPERAND (arg1, 0), 0))
13590         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
13591                                      TREE_OPERAND (arg1, 0));
13592       return NULL_TREE;
13593
13594     case ASSERT_EXPR:
13595       /* An ASSERT_EXPR should never be passed to fold_binary.  */
13596       gcc_unreachable ();
13597
13598     case VEC_PACK_TRUNC_EXPR:
13599     case VEC_PACK_FIX_TRUNC_EXPR:
13600       {
13601         unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
13602         tree *elts, vals = NULL_TREE;
13603
13604         gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts / 2
13605                     && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2);
13606         if (TREE_CODE (arg0) != VECTOR_CST || TREE_CODE (arg1) != VECTOR_CST)
13607           return NULL_TREE;
13608
13609         elts = XALLOCAVEC (tree, nelts);
13610         if (!vec_cst_ctor_to_array (arg0, elts)
13611             || !vec_cst_ctor_to_array (arg1, elts + nelts / 2))
13612           return NULL_TREE;
13613
13614         for (i = 0; i < nelts; i++)
13615           {
13616             elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
13617                                           ? NOP_EXPR : FIX_TRUNC_EXPR,
13618                                           TREE_TYPE (type), elts[i]);
13619             if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
13620               return NULL_TREE;
13621           }
13622
13623         for (i = 0; i < nelts; i++)
13624           vals = tree_cons (NULL_TREE, elts[nelts - i - 1], vals);
13625         return build_vector (type, vals);
13626       }
13627
13628     case VEC_WIDEN_MULT_LO_EXPR:
13629     case VEC_WIDEN_MULT_HI_EXPR:
13630       {
13631         unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
13632         tree *elts, vals = NULL_TREE;
13633
13634         gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2
13635                     && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2);
13636         if (TREE_CODE (arg0) != VECTOR_CST || TREE_CODE (arg1) != VECTOR_CST)
13637           return NULL_TREE;
13638
13639         elts = XALLOCAVEC (tree, nelts * 4);
13640         if (!vec_cst_ctor_to_array (arg0, elts)
13641             || !vec_cst_ctor_to_array (arg1, elts + nelts * 2))
13642           return NULL_TREE;
13643
13644         if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_WIDEN_MULT_LO_EXPR))
13645           elts += nelts;
13646
13647         for (i = 0; i < nelts; i++)
13648           {
13649             elts[i] = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[i]);
13650             elts[i + nelts * 2]
13651               = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
13652                                     elts[i + nelts * 2]);
13653             if (elts[i] == NULL_TREE || elts[i + nelts * 2] == NULL_TREE)
13654               return NULL_TREE;
13655             elts[i] = const_binop (MULT_EXPR, elts[i], elts[i + nelts * 2]);
13656             if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
13657               return NULL_TREE;
13658           }
13659
13660         for (i = 0; i < nelts; i++)
13661           vals = tree_cons (NULL_TREE, elts[nelts - i - 1], vals);
13662         return build_vector (type, vals);
13663       }
13664
13665     default:
13666       return NULL_TREE;
13667     } /* switch (code) */
13668 }
13669
13670 /* Callback for walk_tree, looking for LABEL_EXPR.  Return *TP if it is
13671    a LABEL_EXPR; otherwise return NULL_TREE.  Do not check the subtrees
13672    of GOTO_EXPR.  */
13673
13674 static tree
13675 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13676 {
13677   switch (TREE_CODE (*tp))
13678     {
13679     case LABEL_EXPR:
13680       return *tp;
13681
13682     case GOTO_EXPR:
13683       *walk_subtrees = 0;
13684
13685       /* ... fall through ...  */
13686
13687     default:
13688       return NULL_TREE;
13689     }
13690 }
13691
13692 /* Return whether the sub-tree ST contains a label which is accessible from
13693    outside the sub-tree.  */
13694
13695 static bool
13696 contains_label_p (tree st)
13697 {
13698   return
13699    (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13700 }
13701
13702 /* Fold a ternary expression of code CODE and type TYPE with operands
13703    OP0, OP1, and OP2.  Return the folded expression if folding is
13704    successful.  Otherwise, return NULL_TREE.  */
13705
13706 tree
13707 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13708                   tree op0, tree op1, tree op2)
13709 {
13710   tree tem;
13711   tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
13712   enum tree_code_class kind = TREE_CODE_CLASS (code);
13713
13714   gcc_assert (IS_EXPR_CODE_CLASS (kind)
13715               && TREE_CODE_LENGTH (code) == 3);
13716
13717   /* Strip any conversions that don't change the mode.  This is safe
13718      for every expression, except for a comparison expression because
13719      its signedness is derived from its operands.  So, in the latter
13720      case, only strip conversions that don't change the signedness.
13721
13722      Note that this is done as an internal manipulation within the
13723      constant folder, in order to find the simplest representation of
13724      the arguments so that their form can be studied.  In any cases,
13725      the appropriate type conversions should be put back in the tree
13726      that will get out of the constant folder.  */
13727   if (op0)
13728     {
13729       arg0 = op0;
13730       STRIP_NOPS (arg0);
13731     }
13732
13733   if (op1)
13734     {
13735       arg1 = op1;
13736       STRIP_NOPS (arg1);
13737     }
13738
13739   if (op2)
13740     {
13741       arg2 = op2;
13742       STRIP_NOPS (arg2);
13743     }
13744
13745   switch (code)
13746     {
13747     case COMPONENT_REF:
13748       if (TREE_CODE (arg0) == CONSTRUCTOR
13749           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13750         {
13751           unsigned HOST_WIDE_INT idx;
13752           tree field, value;
13753           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13754             if (field == arg1)
13755               return value;
13756         }
13757       return NULL_TREE;
13758
13759     case COND_EXPR:
13760       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13761          so all simple results must be passed through pedantic_non_lvalue.  */
13762       if (TREE_CODE (arg0) == INTEGER_CST)
13763         {
13764           tree unused_op = integer_zerop (arg0) ? op1 : op2;
13765           tem = integer_zerop (arg0) ? op2 : op1;
13766           /* Only optimize constant conditions when the selected branch
13767              has the same type as the COND_EXPR.  This avoids optimizing
13768              away "c ? x : throw", where the throw has a void type.
13769              Avoid throwing away that operand which contains label.  */
13770           if ((!TREE_SIDE_EFFECTS (unused_op)
13771                || !contains_label_p (unused_op))
13772               && (! VOID_TYPE_P (TREE_TYPE (tem))
13773                   || VOID_TYPE_P (type)))
13774             return pedantic_non_lvalue_loc (loc, tem);
13775           return NULL_TREE;
13776         }
13777       if (operand_equal_p (arg1, op2, 0))
13778         return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13779
13780       /* If we have A op B ? A : C, we may be able to convert this to a
13781          simpler expression, depending on the operation and the values
13782          of B and C.  Signed zeros prevent all of these transformations,
13783          for reasons given above each one.
13784
13785          Also try swapping the arguments and inverting the conditional.  */
13786       if (COMPARISON_CLASS_P (arg0)
13787           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13788                                              arg1, TREE_OPERAND (arg0, 1))
13789           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13790         {
13791           tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13792           if (tem)
13793             return tem;
13794         }
13795
13796       if (COMPARISON_CLASS_P (arg0)
13797           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13798                                              op2,
13799                                              TREE_OPERAND (arg0, 1))
13800           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13801         {
13802           location_t loc0 = expr_location_or (arg0, loc);
13803           tem = fold_truth_not_expr (loc0, arg0);
13804           if (tem && COMPARISON_CLASS_P (tem))
13805             {
13806               tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13807               if (tem)
13808                 return tem;
13809             }
13810         }
13811
13812       /* If the second operand is simpler than the third, swap them
13813          since that produces better jump optimization results.  */
13814       if (truth_value_p (TREE_CODE (arg0))
13815           && tree_swap_operands_p (op1, op2, false))
13816         {
13817           location_t loc0 = expr_location_or (arg0, loc);
13818           /* See if this can be inverted.  If it can't, possibly because
13819              it was a floating-point inequality comparison, don't do
13820              anything.  */
13821           tem = fold_truth_not_expr (loc0, arg0);
13822           if (tem)
13823             return fold_build3_loc (loc, code, type, tem, op2, op1);
13824         }
13825
13826       /* Convert A ? 1 : 0 to simply A.  */
13827       if (integer_onep (op1)
13828           && integer_zerop (op2)
13829           /* If we try to convert OP0 to our type, the
13830              call to fold will try to move the conversion inside
13831              a COND, which will recurse.  In that case, the COND_EXPR
13832              is probably the best choice, so leave it alone.  */
13833           && type == TREE_TYPE (arg0))
13834         return pedantic_non_lvalue_loc (loc, arg0);
13835
13836       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
13837          over COND_EXPR in cases such as floating point comparisons.  */
13838       if (integer_zerop (op1)
13839           && integer_onep (op2)
13840           && truth_value_p (TREE_CODE (arg0)))
13841         return pedantic_non_lvalue_loc (loc,
13842                                     fold_convert_loc (loc, type,
13843                                               invert_truthvalue_loc (loc,
13844                                                                      arg0)));
13845
13846       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
13847       if (TREE_CODE (arg0) == LT_EXPR
13848           && integer_zerop (TREE_OPERAND (arg0, 1))
13849           && integer_zerop (op2)
13850           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13851         {
13852           /* sign_bit_p only checks ARG1 bits within A's precision.
13853              If <sign bit of A> has wider type than A, bits outside
13854              of A's precision in <sign bit of A> need to be checked.
13855              If they are all 0, this optimization needs to be done
13856              in unsigned A's type, if they are all 1 in signed A's type,
13857              otherwise this can't be done.  */
13858           if (TYPE_PRECISION (TREE_TYPE (tem))
13859               < TYPE_PRECISION (TREE_TYPE (arg1))
13860               && TYPE_PRECISION (TREE_TYPE (tem))
13861                  < TYPE_PRECISION (type))
13862             {
13863               unsigned HOST_WIDE_INT mask_lo;
13864               HOST_WIDE_INT mask_hi;
13865               int inner_width, outer_width;
13866               tree tem_type;
13867
13868               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13869               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13870               if (outer_width > TYPE_PRECISION (type))
13871                 outer_width = TYPE_PRECISION (type);
13872
13873               if (outer_width > HOST_BITS_PER_WIDE_INT)
13874                 {
13875                   mask_hi = ((unsigned HOST_WIDE_INT) -1
13876                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13877                   mask_lo = -1;
13878                 }
13879               else
13880                 {
13881                   mask_hi = 0;
13882                   mask_lo = ((unsigned HOST_WIDE_INT) -1
13883                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
13884                 }
13885               if (inner_width > HOST_BITS_PER_WIDE_INT)
13886                 {
13887                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13888                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
13889                   mask_lo = 0;
13890                 }
13891               else
13892                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13893                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
13894
13895               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13896                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13897                 {
13898                   tem_type = signed_type_for (TREE_TYPE (tem));
13899                   tem = fold_convert_loc (loc, tem_type, tem);
13900                 }
13901               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13902                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13903                 {
13904                   tem_type = unsigned_type_for (TREE_TYPE (tem));
13905                   tem = fold_convert_loc (loc, tem_type, tem);
13906                 }
13907               else
13908                 tem = NULL;
13909             }
13910
13911           if (tem)
13912             return
13913               fold_convert_loc (loc, type,
13914                                 fold_build2_loc (loc, BIT_AND_EXPR,
13915                                              TREE_TYPE (tem), tem,
13916                                              fold_convert_loc (loc,
13917                                                                TREE_TYPE (tem),
13918                                                                arg1)));
13919         }
13920
13921       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
13922          already handled above.  */
13923       if (TREE_CODE (arg0) == BIT_AND_EXPR
13924           && integer_onep (TREE_OPERAND (arg0, 1))
13925           && integer_zerop (op2)
13926           && integer_pow2p (arg1))
13927         {
13928           tree tem = TREE_OPERAND (arg0, 0);
13929           STRIP_NOPS (tem);
13930           if (TREE_CODE (tem) == RSHIFT_EXPR
13931               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13932               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13933                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13934             return fold_build2_loc (loc, BIT_AND_EXPR, type,
13935                                 TREE_OPERAND (tem, 0), arg1);
13936         }
13937
13938       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13939          is probably obsolete because the first operand should be a
13940          truth value (that's why we have the two cases above), but let's
13941          leave it in until we can confirm this for all front-ends.  */
13942       if (integer_zerop (op2)
13943           && TREE_CODE (arg0) == NE_EXPR
13944           && integer_zerop (TREE_OPERAND (arg0, 1))
13945           && integer_pow2p (arg1)
13946           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13947           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13948                               arg1, OEP_ONLY_CONST))
13949         return pedantic_non_lvalue_loc (loc,
13950                                     fold_convert_loc (loc, type,
13951                                                       TREE_OPERAND (arg0, 0)));
13952
13953       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13954       if (integer_zerop (op2)
13955           && truth_value_p (TREE_CODE (arg0))
13956           && truth_value_p (TREE_CODE (arg1)))
13957         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13958                             fold_convert_loc (loc, type, arg0),
13959                             arg1);
13960
13961       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13962       if (integer_onep (op2)
13963           && truth_value_p (TREE_CODE (arg0))
13964           && truth_value_p (TREE_CODE (arg1)))
13965         {
13966           location_t loc0 = expr_location_or (arg0, loc);
13967           /* Only perform transformation if ARG0 is easily inverted.  */
13968           tem = fold_truth_not_expr (loc0, arg0);
13969           if (tem)
13970             return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13971                                 fold_convert_loc (loc, type, tem),
13972                                 arg1);
13973         }
13974
13975       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13976       if (integer_zerop (arg1)
13977           && truth_value_p (TREE_CODE (arg0))
13978           && truth_value_p (TREE_CODE (op2)))
13979         {
13980           location_t loc0 = expr_location_or (arg0, loc);
13981           /* Only perform transformation if ARG0 is easily inverted.  */
13982           tem = fold_truth_not_expr (loc0, arg0);
13983           if (tem)
13984             return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13985                                 fold_convert_loc (loc, type, tem),
13986                                 op2);
13987         }
13988
13989       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13990       if (integer_onep (arg1)
13991           && truth_value_p (TREE_CODE (arg0))
13992           && truth_value_p (TREE_CODE (op2)))
13993         return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13994                             fold_convert_loc (loc, type, arg0),
13995                             op2);
13996
13997       return NULL_TREE;
13998
13999     case CALL_EXPR:
14000       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
14001          of fold_ternary on them.  */
14002       gcc_unreachable ();
14003
14004     case BIT_FIELD_REF:
14005       if ((TREE_CODE (arg0) == VECTOR_CST
14006            || TREE_CODE (arg0) == CONSTRUCTOR)
14007           && type == TREE_TYPE (TREE_TYPE (arg0)))
14008         {
14009           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
14010           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
14011
14012           if (width != 0
14013               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
14014               && (idx % width) == 0
14015               && (idx = idx / width)
14016                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
14017             {
14018               if (TREE_CODE (arg0) == VECTOR_CST)
14019                 {
14020                   tree elements = TREE_VECTOR_CST_ELTS (arg0);
14021                   while (idx-- > 0 && elements)
14022                     elements = TREE_CHAIN (elements);
14023                   if (elements)
14024                     return TREE_VALUE (elements);
14025                 }
14026               else if (idx < CONSTRUCTOR_NELTS (arg0))
14027                 return CONSTRUCTOR_ELT (arg0, idx)->value;
14028               return build_zero_cst (type);
14029             }
14030         }
14031
14032       /* A bit-field-ref that referenced the full argument can be stripped.  */
14033       if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
14034           && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
14035           && integer_zerop (op2))
14036         return fold_convert_loc (loc, type, arg0);
14037
14038       return NULL_TREE;
14039
14040     case FMA_EXPR:
14041       /* For integers we can decompose the FMA if possible.  */
14042       if (TREE_CODE (arg0) == INTEGER_CST
14043           && TREE_CODE (arg1) == INTEGER_CST)
14044         return fold_build2_loc (loc, PLUS_EXPR, type,
14045                                 const_binop (MULT_EXPR, arg0, arg1), arg2);
14046       if (integer_zerop (arg2))
14047         return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
14048
14049       return fold_fma (loc, type, arg0, arg1, arg2);
14050
14051     case VEC_PERM_EXPR:
14052       if (TREE_CODE (arg2) == VECTOR_CST)
14053         {
14054           unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
14055           unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
14056           tree t;
14057           bool need_mask_canon = false;
14058
14059           gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)));
14060           for (i = 0, t = TREE_VECTOR_CST_ELTS (arg2);
14061                i < nelts && t; i++, t = TREE_CHAIN (t))
14062             {
14063               if (TREE_CODE (TREE_VALUE (t)) != INTEGER_CST)
14064                 return NULL_TREE;
14065
14066               sel[i] = TREE_INT_CST_LOW (TREE_VALUE (t)) & (2 * nelts - 1);
14067               if (TREE_INT_CST_HIGH (TREE_VALUE (t))
14068                   || ((unsigned HOST_WIDE_INT)
14069                       TREE_INT_CST_LOW (TREE_VALUE (t)) != sel[i]))
14070                 need_mask_canon = true;
14071             }
14072           if (t)
14073             return NULL_TREE;
14074           for (; i < nelts; i++)
14075             sel[i] = 0;
14076
14077           if ((TREE_CODE (arg0) == VECTOR_CST
14078                || TREE_CODE (arg0) == CONSTRUCTOR)
14079               && (TREE_CODE (arg1) == VECTOR_CST
14080                   || TREE_CODE (arg1) == CONSTRUCTOR))
14081             {
14082               t = fold_vec_perm (type, arg0, arg1, sel);
14083               if (t != NULL_TREE)
14084                 return t;
14085             }
14086
14087           if (need_mask_canon && arg2 == op2)
14088             {
14089               tree list = NULL_TREE, eltype = TREE_TYPE (TREE_TYPE (arg2));
14090               for (i = 0; i < nelts; i++)
14091                 list = tree_cons (NULL_TREE,
14092                                   build_int_cst (eltype, sel[nelts - i - 1]),
14093                                   list);
14094               t = build_vector (TREE_TYPE (arg2), list);
14095               return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, t);
14096             }
14097         }
14098       return NULL_TREE;
14099
14100     default:
14101       return NULL_TREE;
14102     } /* switch (code) */
14103 }
14104
14105 /* Perform constant folding and related simplification of EXPR.
14106    The related simplifications include x*1 => x, x*0 => 0, etc.,
14107    and application of the associative law.
14108    NOP_EXPR conversions may be removed freely (as long as we
14109    are careful not to change the type of the overall expression).
14110    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
14111    but we can constant-fold them if they have constant operands.  */
14112
14113 #ifdef ENABLE_FOLD_CHECKING
14114 # define fold(x) fold_1 (x)
14115 static tree fold_1 (tree);
14116 static
14117 #endif
14118 tree
14119 fold (tree expr)
14120 {
14121   const tree t = expr;
14122   enum tree_code code = TREE_CODE (t);
14123   enum tree_code_class kind = TREE_CODE_CLASS (code);
14124   tree tem;
14125   location_t loc = EXPR_LOCATION (expr);
14126
14127   /* Return right away if a constant.  */
14128   if (kind == tcc_constant)
14129     return t;
14130
14131   /* CALL_EXPR-like objects with variable numbers of operands are
14132      treated specially.  */
14133   if (kind == tcc_vl_exp)
14134     {
14135       if (code == CALL_EXPR)
14136         {
14137           tem = fold_call_expr (loc, expr, false);
14138           return tem ? tem : expr;
14139         }
14140       return expr;
14141     }
14142
14143   if (IS_EXPR_CODE_CLASS (kind))
14144     {
14145       tree type = TREE_TYPE (t);
14146       tree op0, op1, op2;
14147
14148       switch (TREE_CODE_LENGTH (code))
14149         {
14150         case 1:
14151           op0 = TREE_OPERAND (t, 0);
14152           tem = fold_unary_loc (loc, code, type, op0);
14153           return tem ? tem : expr;
14154         case 2:
14155           op0 = TREE_OPERAND (t, 0);
14156           op1 = TREE_OPERAND (t, 1);
14157           tem = fold_binary_loc (loc, code, type, op0, op1);
14158           return tem ? tem : expr;
14159         case 3:
14160           op0 = TREE_OPERAND (t, 0);
14161           op1 = TREE_OPERAND (t, 1);
14162           op2 = TREE_OPERAND (t, 2);
14163           tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14164           return tem ? tem : expr;
14165         default:
14166           break;
14167         }
14168     }
14169
14170   switch (code)
14171     {
14172     case ARRAY_REF:
14173       {
14174         tree op0 = TREE_OPERAND (t, 0);
14175         tree op1 = TREE_OPERAND (t, 1);
14176
14177         if (TREE_CODE (op1) == INTEGER_CST
14178             && TREE_CODE (op0) == CONSTRUCTOR
14179             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
14180           {
14181             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
14182             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
14183             unsigned HOST_WIDE_INT begin = 0;
14184
14185             /* Find a matching index by means of a binary search.  */
14186             while (begin != end)
14187               {
14188                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
14189                 tree index = VEC_index (constructor_elt, elts, middle)->index;
14190
14191                 if (TREE_CODE (index) == INTEGER_CST
14192                     && tree_int_cst_lt (index, op1))
14193                   begin = middle + 1;
14194                 else if (TREE_CODE (index) == INTEGER_CST
14195                          && tree_int_cst_lt (op1, index))
14196                   end = middle;
14197                 else if (TREE_CODE (index) == RANGE_EXPR
14198                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
14199                   begin = middle + 1;
14200                 else if (TREE_CODE (index) == RANGE_EXPR
14201                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
14202                   end = middle;
14203                 else
14204                   return VEC_index (constructor_elt, elts, middle)->value;
14205               }
14206           }
14207
14208         return t;
14209       }
14210
14211     case CONST_DECL:
14212       return fold (DECL_INITIAL (t));
14213
14214     default:
14215       return t;
14216     } /* switch (code) */
14217 }
14218
14219 #ifdef ENABLE_FOLD_CHECKING
14220 #undef fold
14221
14222 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
14223 static void fold_check_failed (const_tree, const_tree);
14224 void print_fold_checksum (const_tree);
14225
14226 /* When --enable-checking=fold, compute a digest of expr before
14227    and after actual fold call to see if fold did not accidentally
14228    change original expr.  */
14229
14230 tree
14231 fold (tree expr)
14232 {
14233   tree ret;
14234   struct md5_ctx ctx;
14235   unsigned char checksum_before[16], checksum_after[16];
14236   htab_t ht;
14237
14238   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14239   md5_init_ctx (&ctx);
14240   fold_checksum_tree (expr, &ctx, ht);
14241   md5_finish_ctx (&ctx, checksum_before);
14242   htab_empty (ht);
14243
14244   ret = fold_1 (expr);
14245
14246   md5_init_ctx (&ctx);
14247   fold_checksum_tree (expr, &ctx, ht);
14248   md5_finish_ctx (&ctx, checksum_after);
14249   htab_delete (ht);
14250
14251   if (memcmp (checksum_before, checksum_after, 16))
14252     fold_check_failed (expr, ret);
14253
14254   return ret;
14255 }
14256
14257 void
14258 print_fold_checksum (const_tree expr)
14259 {
14260   struct md5_ctx ctx;
14261   unsigned char checksum[16], cnt;
14262   htab_t ht;
14263
14264   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14265   md5_init_ctx (&ctx);
14266   fold_checksum_tree (expr, &ctx, ht);
14267   md5_finish_ctx (&ctx, checksum);
14268   htab_delete (ht);
14269   for (cnt = 0; cnt < 16; ++cnt)
14270     fprintf (stderr, "%02x", checksum[cnt]);
14271   putc ('\n', stderr);
14272 }
14273
14274 static void
14275 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
14276 {
14277   internal_error ("fold check: original tree changed by fold");
14278 }
14279
14280 static void
14281 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
14282 {
14283   void **slot;
14284   enum tree_code code;
14285   union tree_node buf;
14286   int i, len;
14287
14288  recursive_label:
14289   if (expr == NULL)
14290     return;
14291   slot = (void **) htab_find_slot (ht, expr, INSERT);
14292   if (*slot != NULL)
14293     return;
14294   *slot = CONST_CAST_TREE (expr);
14295   code = TREE_CODE (expr);
14296   if (TREE_CODE_CLASS (code) == tcc_declaration
14297       && DECL_ASSEMBLER_NAME_SET_P (expr))
14298     {
14299       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
14300       memcpy ((char *) &buf, expr, tree_size (expr));
14301       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
14302       expr = (tree) &buf;
14303     }
14304   else if (TREE_CODE_CLASS (code) == tcc_type
14305            && (TYPE_POINTER_TO (expr)
14306                || TYPE_REFERENCE_TO (expr)
14307                || TYPE_CACHED_VALUES_P (expr)
14308                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
14309                || TYPE_NEXT_VARIANT (expr)))
14310     {
14311       /* Allow these fields to be modified.  */
14312       tree tmp;
14313       memcpy ((char *) &buf, expr, tree_size (expr));
14314       expr = tmp = (tree) &buf;
14315       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
14316       TYPE_POINTER_TO (tmp) = NULL;
14317       TYPE_REFERENCE_TO (tmp) = NULL;
14318       TYPE_NEXT_VARIANT (tmp) = NULL;
14319       if (TYPE_CACHED_VALUES_P (tmp))
14320         {
14321           TYPE_CACHED_VALUES_P (tmp) = 0;
14322           TYPE_CACHED_VALUES (tmp) = NULL;
14323         }
14324     }
14325   md5_process_bytes (expr, tree_size (expr), ctx);
14326   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
14327     fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
14328   if (TREE_CODE_CLASS (code) != tcc_type
14329       && TREE_CODE_CLASS (code) != tcc_declaration
14330       && code != TREE_LIST
14331       && code != SSA_NAME
14332       && CODE_CONTAINS_STRUCT (code, TS_COMMON))
14333     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
14334   switch (TREE_CODE_CLASS (code))
14335     {
14336     case tcc_constant:
14337       switch (code)
14338         {
14339         case STRING_CST:
14340           md5_process_bytes (TREE_STRING_POINTER (expr),
14341                              TREE_STRING_LENGTH (expr), ctx);
14342           break;
14343         case COMPLEX_CST:
14344           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
14345           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
14346           break;
14347         case VECTOR_CST:
14348           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
14349           break;
14350         default:
14351           break;
14352         }
14353       break;
14354     case tcc_exceptional:
14355       switch (code)
14356         {
14357         case TREE_LIST:
14358           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
14359           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
14360           expr = TREE_CHAIN (expr);
14361           goto recursive_label;
14362           break;
14363         case TREE_VEC:
14364           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
14365             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
14366           break;
14367         default:
14368           break;
14369         }
14370       break;
14371     case tcc_expression:
14372     case tcc_reference:
14373     case tcc_comparison:
14374     case tcc_unary:
14375     case tcc_binary:
14376     case tcc_statement:
14377     case tcc_vl_exp:
14378       len = TREE_OPERAND_LENGTH (expr);
14379       for (i = 0; i < len; ++i)
14380         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
14381       break;
14382     case tcc_declaration:
14383       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
14384       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
14385       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
14386         {
14387           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
14388           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
14389           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
14390           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
14391           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
14392         }
14393       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
14394         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
14395
14396       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
14397         {
14398           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
14399           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
14400           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
14401         }
14402       break;
14403     case tcc_type:
14404       if (TREE_CODE (expr) == ENUMERAL_TYPE)
14405         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
14406       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
14407       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
14408       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
14409       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
14410       if (INTEGRAL_TYPE_P (expr)
14411           || SCALAR_FLOAT_TYPE_P (expr))
14412         {
14413           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
14414           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
14415         }
14416       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
14417       if (TREE_CODE (expr) == RECORD_TYPE
14418           || TREE_CODE (expr) == UNION_TYPE
14419           || TREE_CODE (expr) == QUAL_UNION_TYPE)
14420         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
14421       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
14422       break;
14423     default:
14424       break;
14425     }
14426 }
14427
14428 /* Helper function for outputting the checksum of a tree T.  When
14429    debugging with gdb, you can "define mynext" to be "next" followed
14430    by "call debug_fold_checksum (op0)", then just trace down till the
14431    outputs differ.  */
14432
14433 DEBUG_FUNCTION void
14434 debug_fold_checksum (const_tree t)
14435 {
14436   int i;
14437   unsigned char checksum[16];
14438   struct md5_ctx ctx;
14439   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14440
14441   md5_init_ctx (&ctx);
14442   fold_checksum_tree (t, &ctx, ht);
14443   md5_finish_ctx (&ctx, checksum);
14444   htab_empty (ht);
14445
14446   for (i = 0; i < 16; i++)
14447     fprintf (stderr, "%d ", checksum[i]);
14448
14449   fprintf (stderr, "\n");
14450 }
14451
14452 #endif
14453
14454 /* Fold a unary tree expression with code CODE of type TYPE with an
14455    operand OP0.  LOC is the location of the resulting expression.
14456    Return a folded expression if successful.  Otherwise, return a tree
14457    expression with code CODE of type TYPE with an operand OP0.  */
14458
14459 tree
14460 fold_build1_stat_loc (location_t loc,
14461                       enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
14462 {
14463   tree tem;
14464 #ifdef ENABLE_FOLD_CHECKING
14465   unsigned char checksum_before[16], checksum_after[16];
14466   struct md5_ctx ctx;
14467   htab_t ht;
14468
14469   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14470   md5_init_ctx (&ctx);
14471   fold_checksum_tree (op0, &ctx, ht);
14472   md5_finish_ctx (&ctx, checksum_before);
14473   htab_empty (ht);
14474 #endif
14475
14476   tem = fold_unary_loc (loc, code, type, op0);
14477   if (!tem)
14478     tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
14479
14480 #ifdef ENABLE_FOLD_CHECKING
14481   md5_init_ctx (&ctx);
14482   fold_checksum_tree (op0, &ctx, ht);
14483   md5_finish_ctx (&ctx, checksum_after);
14484   htab_delete (ht);
14485
14486   if (memcmp (checksum_before, checksum_after, 16))
14487     fold_check_failed (op0, tem);
14488 #endif
14489   return tem;
14490 }
14491
14492 /* Fold a binary tree expression with code CODE of type TYPE with
14493    operands OP0 and OP1.  LOC is the location of the resulting
14494    expression.  Return a folded expression if successful.  Otherwise,
14495    return a tree expression with code CODE of type TYPE with operands
14496    OP0 and OP1.  */
14497
14498 tree
14499 fold_build2_stat_loc (location_t loc,
14500                       enum tree_code code, tree type, tree op0, tree op1
14501                       MEM_STAT_DECL)
14502 {
14503   tree tem;
14504 #ifdef ENABLE_FOLD_CHECKING
14505   unsigned char checksum_before_op0[16],
14506                 checksum_before_op1[16],
14507                 checksum_after_op0[16],
14508                 checksum_after_op1[16];
14509   struct md5_ctx ctx;
14510   htab_t ht;
14511
14512   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14513   md5_init_ctx (&ctx);
14514   fold_checksum_tree (op0, &ctx, ht);
14515   md5_finish_ctx (&ctx, checksum_before_op0);
14516   htab_empty (ht);
14517
14518   md5_init_ctx (&ctx);
14519   fold_checksum_tree (op1, &ctx, ht);
14520   md5_finish_ctx (&ctx, checksum_before_op1);
14521   htab_empty (ht);
14522 #endif
14523
14524   tem = fold_binary_loc (loc, code, type, op0, op1);
14525   if (!tem)
14526     tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
14527
14528 #ifdef ENABLE_FOLD_CHECKING
14529   md5_init_ctx (&ctx);
14530   fold_checksum_tree (op0, &ctx, ht);
14531   md5_finish_ctx (&ctx, checksum_after_op0);
14532   htab_empty (ht);
14533
14534   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14535     fold_check_failed (op0, tem);
14536
14537   md5_init_ctx (&ctx);
14538   fold_checksum_tree (op1, &ctx, ht);
14539   md5_finish_ctx (&ctx, checksum_after_op1);
14540   htab_delete (ht);
14541
14542   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14543     fold_check_failed (op1, tem);
14544 #endif
14545   return tem;
14546 }
14547
14548 /* Fold a ternary tree expression with code CODE of type TYPE with
14549    operands OP0, OP1, and OP2.  Return a folded expression if
14550    successful.  Otherwise, return a tree expression with code CODE of
14551    type TYPE with operands OP0, OP1, and OP2.  */
14552
14553 tree
14554 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14555                       tree op0, tree op1, tree op2 MEM_STAT_DECL)
14556 {
14557   tree tem;
14558 #ifdef ENABLE_FOLD_CHECKING
14559   unsigned char checksum_before_op0[16],
14560                 checksum_before_op1[16],
14561                 checksum_before_op2[16],
14562                 checksum_after_op0[16],
14563                 checksum_after_op1[16],
14564                 checksum_after_op2[16];
14565   struct md5_ctx ctx;
14566   htab_t ht;
14567
14568   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14569   md5_init_ctx (&ctx);
14570   fold_checksum_tree (op0, &ctx, ht);
14571   md5_finish_ctx (&ctx, checksum_before_op0);
14572   htab_empty (ht);
14573
14574   md5_init_ctx (&ctx);
14575   fold_checksum_tree (op1, &ctx, ht);
14576   md5_finish_ctx (&ctx, checksum_before_op1);
14577   htab_empty (ht);
14578
14579   md5_init_ctx (&ctx);
14580   fold_checksum_tree (op2, &ctx, ht);
14581   md5_finish_ctx (&ctx, checksum_before_op2);
14582   htab_empty (ht);
14583 #endif
14584
14585   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14586   tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14587   if (!tem)
14588     tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
14589
14590 #ifdef ENABLE_FOLD_CHECKING
14591   md5_init_ctx (&ctx);
14592   fold_checksum_tree (op0, &ctx, ht);
14593   md5_finish_ctx (&ctx, checksum_after_op0);
14594   htab_empty (ht);
14595
14596   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14597     fold_check_failed (op0, tem);
14598
14599   md5_init_ctx (&ctx);
14600   fold_checksum_tree (op1, &ctx, ht);
14601   md5_finish_ctx (&ctx, checksum_after_op1);
14602   htab_empty (ht);
14603
14604   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14605     fold_check_failed (op1, tem);
14606
14607   md5_init_ctx (&ctx);
14608   fold_checksum_tree (op2, &ctx, ht);
14609   md5_finish_ctx (&ctx, checksum_after_op2);
14610   htab_delete (ht);
14611
14612   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14613     fold_check_failed (op2, tem);
14614 #endif
14615   return tem;
14616 }
14617
14618 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14619    arguments in ARGARRAY, and a null static chain.
14620    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
14621    of type TYPE from the given operands as constructed by build_call_array.  */
14622
14623 tree
14624 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14625                            int nargs, tree *argarray)
14626 {
14627   tree tem;
14628 #ifdef ENABLE_FOLD_CHECKING
14629   unsigned char checksum_before_fn[16],
14630                 checksum_before_arglist[16],
14631                 checksum_after_fn[16],
14632                 checksum_after_arglist[16];
14633   struct md5_ctx ctx;
14634   htab_t ht;
14635   int i;
14636
14637   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14638   md5_init_ctx (&ctx);
14639   fold_checksum_tree (fn, &ctx, ht);
14640   md5_finish_ctx (&ctx, checksum_before_fn);
14641   htab_empty (ht);
14642
14643   md5_init_ctx (&ctx);
14644   for (i = 0; i < nargs; i++)
14645     fold_checksum_tree (argarray[i], &ctx, ht);
14646   md5_finish_ctx (&ctx, checksum_before_arglist);
14647   htab_empty (ht);
14648 #endif
14649
14650   tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14651
14652 #ifdef ENABLE_FOLD_CHECKING
14653   md5_init_ctx (&ctx);
14654   fold_checksum_tree (fn, &ctx, ht);
14655   md5_finish_ctx (&ctx, checksum_after_fn);
14656   htab_empty (ht);
14657
14658   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14659     fold_check_failed (fn, tem);
14660
14661   md5_init_ctx (&ctx);
14662   for (i = 0; i < nargs; i++)
14663     fold_checksum_tree (argarray[i], &ctx, ht);
14664   md5_finish_ctx (&ctx, checksum_after_arglist);
14665   htab_delete (ht);
14666
14667   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14668     fold_check_failed (NULL_TREE, tem);
14669 #endif
14670   return tem;
14671 }
14672
14673 /* Perform constant folding and related simplification of initializer
14674    expression EXPR.  These behave identically to "fold_buildN" but ignore
14675    potential run-time traps and exceptions that fold must preserve.  */
14676
14677 #define START_FOLD_INIT \
14678   int saved_signaling_nans = flag_signaling_nans;\
14679   int saved_trapping_math = flag_trapping_math;\
14680   int saved_rounding_math = flag_rounding_math;\
14681   int saved_trapv = flag_trapv;\
14682   int saved_folding_initializer = folding_initializer;\
14683   flag_signaling_nans = 0;\
14684   flag_trapping_math = 0;\
14685   flag_rounding_math = 0;\
14686   flag_trapv = 0;\
14687   folding_initializer = 1;
14688
14689 #define END_FOLD_INIT \
14690   flag_signaling_nans = saved_signaling_nans;\
14691   flag_trapping_math = saved_trapping_math;\
14692   flag_rounding_math = saved_rounding_math;\
14693   flag_trapv = saved_trapv;\
14694   folding_initializer = saved_folding_initializer;
14695
14696 tree
14697 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14698                              tree type, tree op)
14699 {
14700   tree result;
14701   START_FOLD_INIT;
14702
14703   result = fold_build1_loc (loc, code, type, op);
14704
14705   END_FOLD_INIT;
14706   return result;
14707 }
14708
14709 tree
14710 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14711                              tree type, tree op0, tree op1)
14712 {
14713   tree result;
14714   START_FOLD_INIT;
14715
14716   result = fold_build2_loc (loc, code, type, op0, op1);
14717
14718   END_FOLD_INIT;
14719   return result;
14720 }
14721
14722 tree
14723 fold_build3_initializer_loc (location_t loc, enum tree_code code,
14724                              tree type, tree op0, tree op1, tree op2)
14725 {
14726   tree result;
14727   START_FOLD_INIT;
14728
14729   result = fold_build3_loc (loc, code, type, op0, op1, op2);
14730
14731   END_FOLD_INIT;
14732   return result;
14733 }
14734
14735 tree
14736 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14737                                        int nargs, tree *argarray)
14738 {
14739   tree result;
14740   START_FOLD_INIT;
14741
14742   result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14743
14744   END_FOLD_INIT;
14745   return result;
14746 }
14747
14748 #undef START_FOLD_INIT
14749 #undef END_FOLD_INIT
14750
14751 /* Determine if first argument is a multiple of second argument.  Return 0 if
14752    it is not, or we cannot easily determined it to be.
14753
14754    An example of the sort of thing we care about (at this point; this routine
14755    could surely be made more general, and expanded to do what the *_DIV_EXPR's
14756    fold cases do now) is discovering that
14757
14758      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14759
14760    is a multiple of
14761
14762      SAVE_EXPR (J * 8)
14763
14764    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14765
14766    This code also handles discovering that
14767
14768      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14769
14770    is a multiple of 8 so we don't have to worry about dealing with a
14771    possible remainder.
14772
14773    Note that we *look* inside a SAVE_EXPR only to determine how it was
14774    calculated; it is not safe for fold to do much of anything else with the
14775    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14776    at run time.  For example, the latter example above *cannot* be implemented
14777    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14778    evaluation time of the original SAVE_EXPR is not necessarily the same at
14779    the time the new expression is evaluated.  The only optimization of this
14780    sort that would be valid is changing
14781
14782      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14783
14784    divided by 8 to
14785
14786      SAVE_EXPR (I) * SAVE_EXPR (J)
14787
14788    (where the same SAVE_EXPR (J) is used in the original and the
14789    transformed version).  */
14790
14791 int
14792 multiple_of_p (tree type, const_tree top, const_tree bottom)
14793 {
14794   if (operand_equal_p (top, bottom, 0))
14795     return 1;
14796
14797   if (TREE_CODE (type) != INTEGER_TYPE)
14798     return 0;
14799
14800   switch (TREE_CODE (top))
14801     {
14802     case BIT_AND_EXPR:
14803       /* Bitwise and provides a power of two multiple.  If the mask is
14804          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
14805       if (!integer_pow2p (bottom))
14806         return 0;
14807       /* FALLTHRU */
14808
14809     case MULT_EXPR:
14810       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14811               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14812
14813     case PLUS_EXPR:
14814     case MINUS_EXPR:
14815       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14816               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14817
14818     case LSHIFT_EXPR:
14819       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14820         {
14821           tree op1, t1;
14822
14823           op1 = TREE_OPERAND (top, 1);
14824           /* const_binop may not detect overflow correctly,
14825              so check for it explicitly here.  */
14826           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14827               > TREE_INT_CST_LOW (op1)
14828               && TREE_INT_CST_HIGH (op1) == 0
14829               && 0 != (t1 = fold_convert (type,
14830                                           const_binop (LSHIFT_EXPR,
14831                                                        size_one_node,
14832                                                        op1)))
14833               && !TREE_OVERFLOW (t1))
14834             return multiple_of_p (type, t1, bottom);
14835         }
14836       return 0;
14837
14838     case NOP_EXPR:
14839       /* Can't handle conversions from non-integral or wider integral type.  */
14840       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14841           || (TYPE_PRECISION (type)
14842               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14843         return 0;
14844
14845       /* .. fall through ...  */
14846
14847     case SAVE_EXPR:
14848       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14849
14850     case COND_EXPR:
14851       return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
14852               && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
14853
14854     case INTEGER_CST:
14855       if (TREE_CODE (bottom) != INTEGER_CST
14856           || integer_zerop (bottom)
14857           || (TYPE_UNSIGNED (type)
14858               && (tree_int_cst_sgn (top) < 0
14859                   || tree_int_cst_sgn (bottom) < 0)))
14860         return 0;
14861       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14862                                              top, bottom));
14863
14864     default:
14865       return 0;
14866     }
14867 }
14868
14869 /* Return true if CODE or TYPE is known to be non-negative. */
14870
14871 static bool
14872 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14873 {
14874   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14875       && truth_value_p (code))
14876     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14877        have a signed:1 type (where the value is -1 and 0).  */
14878     return true;
14879   return false;
14880 }
14881
14882 /* Return true if (CODE OP0) is known to be non-negative.  If the return
14883    value is based on the assumption that signed overflow is undefined,
14884    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14885    *STRICT_OVERFLOW_P.  */
14886
14887 bool
14888 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14889                                 bool *strict_overflow_p)
14890 {
14891   if (TYPE_UNSIGNED (type))
14892     return true;
14893
14894   switch (code)
14895     {
14896     case ABS_EXPR:
14897       /* We can't return 1 if flag_wrapv is set because
14898          ABS_EXPR<INT_MIN> = INT_MIN.  */
14899       if (!INTEGRAL_TYPE_P (type))
14900         return true;
14901       if (TYPE_OVERFLOW_UNDEFINED (type))
14902         {
14903           *strict_overflow_p = true;
14904           return true;
14905         }
14906       break;
14907
14908     case NON_LVALUE_EXPR:
14909     case FLOAT_EXPR:
14910     case FIX_TRUNC_EXPR:
14911       return tree_expr_nonnegative_warnv_p (op0,
14912                                             strict_overflow_p);
14913
14914     case NOP_EXPR:
14915       {
14916         tree inner_type = TREE_TYPE (op0);
14917         tree outer_type = type;
14918
14919         if (TREE_CODE (outer_type) == REAL_TYPE)
14920           {
14921             if (TREE_CODE (inner_type) == REAL_TYPE)
14922               return tree_expr_nonnegative_warnv_p (op0,
14923                                                     strict_overflow_p);
14924             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14925               {
14926                 if (TYPE_UNSIGNED (inner_type))
14927                   return true;
14928                 return tree_expr_nonnegative_warnv_p (op0,
14929                                                       strict_overflow_p);
14930               }
14931           }
14932         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14933           {
14934             if (TREE_CODE (inner_type) == REAL_TYPE)
14935               return tree_expr_nonnegative_warnv_p (op0,
14936                                                     strict_overflow_p);
14937             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14938               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14939                       && TYPE_UNSIGNED (inner_type);
14940           }
14941       }
14942       break;
14943
14944     default:
14945       return tree_simple_nonnegative_warnv_p (code, type);
14946     }
14947
14948   /* We don't know sign of `t', so be conservative and return false.  */
14949   return false;
14950 }
14951
14952 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
14953    value is based on the assumption that signed overflow is undefined,
14954    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14955    *STRICT_OVERFLOW_P.  */
14956
14957 bool
14958 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14959                                       tree op1, bool *strict_overflow_p)
14960 {
14961   if (TYPE_UNSIGNED (type))
14962     return true;
14963
14964   switch (code)
14965     {
14966     case POINTER_PLUS_EXPR:
14967     case PLUS_EXPR:
14968       if (FLOAT_TYPE_P (type))
14969         return (tree_expr_nonnegative_warnv_p (op0,
14970                                                strict_overflow_p)
14971                 && tree_expr_nonnegative_warnv_p (op1,
14972                                                   strict_overflow_p));
14973
14974       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14975          both unsigned and at least 2 bits shorter than the result.  */
14976       if (TREE_CODE (type) == INTEGER_TYPE
14977           && TREE_CODE (op0) == NOP_EXPR
14978           && TREE_CODE (op1) == NOP_EXPR)
14979         {
14980           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14981           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14982           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14983               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14984             {
14985               unsigned int prec = MAX (TYPE_PRECISION (inner1),
14986                                        TYPE_PRECISION (inner2)) + 1;
14987               return prec < TYPE_PRECISION (type);
14988             }
14989         }
14990       break;
14991
14992     case MULT_EXPR:
14993       if (FLOAT_TYPE_P (type))
14994         {
14995           /* x * x for floating point x is always non-negative.  */
14996           if (operand_equal_p (op0, op1, 0))
14997             return true;
14998           return (tree_expr_nonnegative_warnv_p (op0,
14999                                                  strict_overflow_p)
15000                   && tree_expr_nonnegative_warnv_p (op1,
15001                                                     strict_overflow_p));
15002         }
15003
15004       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
15005          both unsigned and their total bits is shorter than the result.  */
15006       if (TREE_CODE (type) == INTEGER_TYPE
15007           && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
15008           && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
15009         {
15010           tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
15011             ? TREE_TYPE (TREE_OPERAND (op0, 0))
15012             : TREE_TYPE (op0);
15013           tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
15014             ? TREE_TYPE (TREE_OPERAND (op1, 0))
15015             : TREE_TYPE (op1);
15016
15017           bool unsigned0 = TYPE_UNSIGNED (inner0);
15018           bool unsigned1 = TYPE_UNSIGNED (inner1);
15019
15020           if (TREE_CODE (op0) == INTEGER_CST)
15021             unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
15022
15023           if (TREE_CODE (op1) == INTEGER_CST)
15024             unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
15025
15026           if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
15027               && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
15028             {
15029               unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
15030                 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
15031                 : TYPE_PRECISION (inner0);
15032
15033               unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
15034                 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
15035                 : TYPE_PRECISION (inner1);
15036
15037               return precision0 + precision1 < TYPE_PRECISION (type);
15038             }
15039         }
15040       return false;
15041
15042     case BIT_AND_EXPR:
15043     case MAX_EXPR:
15044       return (tree_expr_nonnegative_warnv_p (op0,
15045                                              strict_overflow_p)
15046               || tree_expr_nonnegative_warnv_p (op1,
15047                                                 strict_overflow_p));
15048
15049     case BIT_IOR_EXPR:
15050     case BIT_XOR_EXPR:
15051     case MIN_EXPR:
15052     case RDIV_EXPR:
15053     case TRUNC_DIV_EXPR:
15054     case CEIL_DIV_EXPR:
15055     case FLOOR_DIV_EXPR:
15056     case ROUND_DIV_EXPR:
15057       return (tree_expr_nonnegative_warnv_p (op0,
15058                                              strict_overflow_p)
15059               && tree_expr_nonnegative_warnv_p (op1,
15060                                                 strict_overflow_p));
15061
15062     case TRUNC_MOD_EXPR:
15063     case CEIL_MOD_EXPR:
15064     case FLOOR_MOD_EXPR:
15065     case ROUND_MOD_EXPR:
15066       return tree_expr_nonnegative_warnv_p (op0,
15067                                             strict_overflow_p);
15068     default:
15069       return tree_simple_nonnegative_warnv_p (code, type);
15070     }
15071
15072   /* We don't know sign of `t', so be conservative and return false.  */
15073   return false;
15074 }
15075
15076 /* Return true if T is known to be non-negative.  If the return
15077    value is based on the assumption that signed overflow is undefined,
15078    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15079    *STRICT_OVERFLOW_P.  */
15080
15081 bool
15082 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15083 {
15084   if (TYPE_UNSIGNED (TREE_TYPE (t)))
15085     return true;
15086
15087   switch (TREE_CODE (t))
15088     {
15089     case INTEGER_CST:
15090       return tree_int_cst_sgn (t) >= 0;
15091
15092     case REAL_CST:
15093       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
15094
15095     case FIXED_CST:
15096       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
15097
15098     case COND_EXPR:
15099       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15100                                              strict_overflow_p)
15101               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
15102                                                 strict_overflow_p));
15103     default:
15104       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15105                                                    TREE_TYPE (t));
15106     }
15107   /* We don't know sign of `t', so be conservative and return false.  */
15108   return false;
15109 }
15110
15111 /* Return true if T is known to be non-negative.  If the return
15112    value is based on the assumption that signed overflow is undefined,
15113    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15114    *STRICT_OVERFLOW_P.  */
15115
15116 bool
15117 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
15118                                tree arg0, tree arg1, bool *strict_overflow_p)
15119 {
15120   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
15121     switch (DECL_FUNCTION_CODE (fndecl))
15122       {
15123         CASE_FLT_FN (BUILT_IN_ACOS):
15124         CASE_FLT_FN (BUILT_IN_ACOSH):
15125         CASE_FLT_FN (BUILT_IN_CABS):
15126         CASE_FLT_FN (BUILT_IN_COSH):
15127         CASE_FLT_FN (BUILT_IN_ERFC):
15128         CASE_FLT_FN (BUILT_IN_EXP):
15129         CASE_FLT_FN (BUILT_IN_EXP10):
15130         CASE_FLT_FN (BUILT_IN_EXP2):
15131         CASE_FLT_FN (BUILT_IN_FABS):
15132         CASE_FLT_FN (BUILT_IN_FDIM):
15133         CASE_FLT_FN (BUILT_IN_HYPOT):
15134         CASE_FLT_FN (BUILT_IN_POW10):
15135         CASE_INT_FN (BUILT_IN_FFS):
15136         CASE_INT_FN (BUILT_IN_PARITY):
15137         CASE_INT_FN (BUILT_IN_POPCOUNT):
15138       case BUILT_IN_BSWAP32:
15139       case BUILT_IN_BSWAP64:
15140         /* Always true.  */
15141         return true;
15142
15143         CASE_FLT_FN (BUILT_IN_SQRT):
15144         /* sqrt(-0.0) is -0.0.  */
15145         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
15146           return true;
15147         return tree_expr_nonnegative_warnv_p (arg0,
15148                                               strict_overflow_p);
15149
15150         CASE_FLT_FN (BUILT_IN_ASINH):
15151         CASE_FLT_FN (BUILT_IN_ATAN):
15152         CASE_FLT_FN (BUILT_IN_ATANH):
15153         CASE_FLT_FN (BUILT_IN_CBRT):
15154         CASE_FLT_FN (BUILT_IN_CEIL):
15155         CASE_FLT_FN (BUILT_IN_ERF):
15156         CASE_FLT_FN (BUILT_IN_EXPM1):
15157         CASE_FLT_FN (BUILT_IN_FLOOR):
15158         CASE_FLT_FN (BUILT_IN_FMOD):
15159         CASE_FLT_FN (BUILT_IN_FREXP):
15160         CASE_FLT_FN (BUILT_IN_ICEIL):
15161         CASE_FLT_FN (BUILT_IN_IFLOOR):
15162         CASE_FLT_FN (BUILT_IN_IRINT):
15163         CASE_FLT_FN (BUILT_IN_IROUND):
15164         CASE_FLT_FN (BUILT_IN_LCEIL):
15165         CASE_FLT_FN (BUILT_IN_LDEXP):
15166         CASE_FLT_FN (BUILT_IN_LFLOOR):
15167         CASE_FLT_FN (BUILT_IN_LLCEIL):
15168         CASE_FLT_FN (BUILT_IN_LLFLOOR):
15169         CASE_FLT_FN (BUILT_IN_LLRINT):
15170         CASE_FLT_FN (BUILT_IN_LLROUND):
15171         CASE_FLT_FN (BUILT_IN_LRINT):
15172         CASE_FLT_FN (BUILT_IN_LROUND):
15173         CASE_FLT_FN (BUILT_IN_MODF):
15174         CASE_FLT_FN (BUILT_IN_NEARBYINT):
15175         CASE_FLT_FN (BUILT_IN_RINT):
15176         CASE_FLT_FN (BUILT_IN_ROUND):
15177         CASE_FLT_FN (BUILT_IN_SCALB):
15178         CASE_FLT_FN (BUILT_IN_SCALBLN):
15179         CASE_FLT_FN (BUILT_IN_SCALBN):
15180         CASE_FLT_FN (BUILT_IN_SIGNBIT):
15181         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
15182         CASE_FLT_FN (BUILT_IN_SINH):
15183         CASE_FLT_FN (BUILT_IN_TANH):
15184         CASE_FLT_FN (BUILT_IN_TRUNC):
15185         /* True if the 1st argument is nonnegative.  */
15186         return tree_expr_nonnegative_warnv_p (arg0,
15187                                               strict_overflow_p);
15188
15189         CASE_FLT_FN (BUILT_IN_FMAX):
15190         /* True if the 1st OR 2nd arguments are nonnegative.  */
15191         return (tree_expr_nonnegative_warnv_p (arg0,
15192                                                strict_overflow_p)
15193                 || (tree_expr_nonnegative_warnv_p (arg1,
15194                                                    strict_overflow_p)));
15195
15196         CASE_FLT_FN (BUILT_IN_FMIN):
15197         /* True if the 1st AND 2nd arguments are nonnegative.  */
15198         return (tree_expr_nonnegative_warnv_p (arg0,
15199                                                strict_overflow_p)
15200                 && (tree_expr_nonnegative_warnv_p (arg1,
15201                                                    strict_overflow_p)));
15202
15203         CASE_FLT_FN (BUILT_IN_COPYSIGN):
15204         /* True if the 2nd argument is nonnegative.  */
15205         return tree_expr_nonnegative_warnv_p (arg1,
15206                                               strict_overflow_p);
15207
15208         CASE_FLT_FN (BUILT_IN_POWI):
15209         /* True if the 1st argument is nonnegative or the second
15210            argument is an even integer.  */
15211         if (TREE_CODE (arg1) == INTEGER_CST
15212             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
15213           return true;
15214         return tree_expr_nonnegative_warnv_p (arg0,
15215                                               strict_overflow_p);
15216
15217         CASE_FLT_FN (BUILT_IN_POW):
15218         /* True if the 1st argument is nonnegative or the second
15219            argument is an even integer valued real.  */
15220         if (TREE_CODE (arg1) == REAL_CST)
15221           {
15222             REAL_VALUE_TYPE c;
15223             HOST_WIDE_INT n;
15224
15225             c = TREE_REAL_CST (arg1);
15226             n = real_to_integer (&c);
15227             if ((n & 1) == 0)
15228               {
15229                 REAL_VALUE_TYPE cint;
15230                 real_from_integer (&cint, VOIDmode, n,
15231                                    n < 0 ? -1 : 0, 0);
15232                 if (real_identical (&c, &cint))
15233                   return true;
15234               }
15235           }
15236         return tree_expr_nonnegative_warnv_p (arg0,
15237                                               strict_overflow_p);
15238
15239       default:
15240         break;
15241       }
15242   return tree_simple_nonnegative_warnv_p (CALL_EXPR,
15243                                           type);
15244 }
15245
15246 /* Return true if T is known to be non-negative.  If the return
15247    value is based on the assumption that signed overflow is undefined,
15248    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15249    *STRICT_OVERFLOW_P.  */
15250
15251 bool
15252 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15253 {
15254   enum tree_code code = TREE_CODE (t);
15255   if (TYPE_UNSIGNED (TREE_TYPE (t)))
15256     return true;
15257
15258   switch (code)
15259     {
15260     case TARGET_EXPR:
15261       {
15262         tree temp = TARGET_EXPR_SLOT (t);
15263         t = TARGET_EXPR_INITIAL (t);
15264
15265         /* If the initializer is non-void, then it's a normal expression
15266            that will be assigned to the slot.  */
15267         if (!VOID_TYPE_P (t))
15268           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
15269
15270         /* Otherwise, the initializer sets the slot in some way.  One common
15271            way is an assignment statement at the end of the initializer.  */
15272         while (1)
15273           {
15274             if (TREE_CODE (t) == BIND_EXPR)
15275               t = expr_last (BIND_EXPR_BODY (t));
15276             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15277                      || TREE_CODE (t) == TRY_CATCH_EXPR)
15278               t = expr_last (TREE_OPERAND (t, 0));
15279             else if (TREE_CODE (t) == STATEMENT_LIST)
15280               t = expr_last (t);
15281             else
15282               break;
15283           }
15284         if (TREE_CODE (t) == MODIFY_EXPR
15285             && TREE_OPERAND (t, 0) == temp)
15286           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15287                                                 strict_overflow_p);
15288
15289         return false;
15290       }
15291
15292     case CALL_EXPR:
15293       {
15294         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
15295         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
15296
15297         return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
15298                                               get_callee_fndecl (t),
15299                                               arg0,
15300                                               arg1,
15301                                               strict_overflow_p);
15302       }
15303     case COMPOUND_EXPR:
15304     case MODIFY_EXPR:
15305       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15306                                             strict_overflow_p);
15307     case BIND_EXPR:
15308       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
15309                                             strict_overflow_p);
15310     case SAVE_EXPR:
15311       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
15312                                             strict_overflow_p);
15313
15314     default:
15315       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15316                                                    TREE_TYPE (t));
15317     }
15318
15319   /* We don't know sign of `t', so be conservative and return false.  */
15320   return false;
15321 }
15322
15323 /* Return true if T is known to be non-negative.  If the return
15324    value is based on the assumption that signed overflow is undefined,
15325    set *STRICT_OVERFLOW_P to true; otherwise, don't change
15326    *STRICT_OVERFLOW_P.  */
15327
15328 bool
15329 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15330 {
15331   enum tree_code code;
15332   if (t == error_mark_node)
15333     return false;
15334
15335   code = TREE_CODE (t);
15336   switch (TREE_CODE_CLASS (code))
15337     {
15338     case tcc_binary:
15339     case tcc_comparison:
15340       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15341                                               TREE_TYPE (t),
15342                                               TREE_OPERAND (t, 0),
15343                                               TREE_OPERAND (t, 1),
15344                                               strict_overflow_p);
15345
15346     case tcc_unary:
15347       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15348                                              TREE_TYPE (t),
15349                                              TREE_OPERAND (t, 0),
15350                                              strict_overflow_p);
15351
15352     case tcc_constant:
15353     case tcc_declaration:
15354     case tcc_reference:
15355       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15356
15357     default:
15358       break;
15359     }
15360
15361   switch (code)
15362     {
15363     case TRUTH_AND_EXPR:
15364     case TRUTH_OR_EXPR:
15365     case TRUTH_XOR_EXPR:
15366       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15367                                               TREE_TYPE (t),
15368                                               TREE_OPERAND (t, 0),
15369                                               TREE_OPERAND (t, 1),
15370                                               strict_overflow_p);
15371     case TRUTH_NOT_EXPR:
15372       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15373                                              TREE_TYPE (t),
15374                                              TREE_OPERAND (t, 0),
15375                                              strict_overflow_p);
15376
15377     case COND_EXPR:
15378     case CONSTRUCTOR:
15379     case OBJ_TYPE_REF:
15380     case ASSERT_EXPR:
15381     case ADDR_EXPR:
15382     case WITH_SIZE_EXPR:
15383     case SSA_NAME:
15384       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15385
15386     default:
15387       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
15388     }
15389 }
15390
15391 /* Return true if `t' is known to be non-negative.  Handle warnings
15392    about undefined signed overflow.  */
15393
15394 bool
15395 tree_expr_nonnegative_p (tree t)
15396 {
15397   bool ret, strict_overflow_p;
15398
15399   strict_overflow_p = false;
15400   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15401   if (strict_overflow_p)
15402     fold_overflow_warning (("assuming signed overflow does not occur when "
15403                             "determining that expression is always "
15404                             "non-negative"),
15405                            WARN_STRICT_OVERFLOW_MISC);
15406   return ret;
15407 }
15408
15409
15410 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15411    For floating point we further ensure that T is not denormal.
15412    Similar logic is present in nonzero_address in rtlanal.h.
15413
15414    If the return value is based on the assumption that signed overflow
15415    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15416    change *STRICT_OVERFLOW_P.  */
15417
15418 bool
15419 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15420                                  bool *strict_overflow_p)
15421 {
15422   switch (code)
15423     {
15424     case ABS_EXPR:
15425       return tree_expr_nonzero_warnv_p (op0,
15426                                         strict_overflow_p);
15427
15428     case NOP_EXPR:
15429       {
15430         tree inner_type = TREE_TYPE (op0);
15431         tree outer_type = type;
15432
15433         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15434                 && tree_expr_nonzero_warnv_p (op0,
15435                                               strict_overflow_p));
15436       }
15437       break;
15438
15439     case NON_LVALUE_EXPR:
15440       return tree_expr_nonzero_warnv_p (op0,
15441                                         strict_overflow_p);
15442
15443     default:
15444       break;
15445   }
15446
15447   return false;
15448 }
15449
15450 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15451    For floating point we further ensure that T is not denormal.
15452    Similar logic is present in nonzero_address in rtlanal.h.
15453
15454    If the return value is based on the assumption that signed overflow
15455    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15456    change *STRICT_OVERFLOW_P.  */
15457
15458 bool
15459 tree_binary_nonzero_warnv_p (enum tree_code code,
15460                              tree type,
15461                              tree op0,
15462                              tree op1, bool *strict_overflow_p)
15463 {
15464   bool sub_strict_overflow_p;
15465   switch (code)
15466     {
15467     case POINTER_PLUS_EXPR:
15468     case PLUS_EXPR:
15469       if (TYPE_OVERFLOW_UNDEFINED (type))
15470         {
15471           /* With the presence of negative values it is hard
15472              to say something.  */
15473           sub_strict_overflow_p = false;
15474           if (!tree_expr_nonnegative_warnv_p (op0,
15475                                               &sub_strict_overflow_p)
15476               || !tree_expr_nonnegative_warnv_p (op1,
15477                                                  &sub_strict_overflow_p))
15478             return false;
15479           /* One of operands must be positive and the other non-negative.  */
15480           /* We don't set *STRICT_OVERFLOW_P here: even if this value
15481              overflows, on a twos-complement machine the sum of two
15482              nonnegative numbers can never be zero.  */
15483           return (tree_expr_nonzero_warnv_p (op0,
15484                                              strict_overflow_p)
15485                   || tree_expr_nonzero_warnv_p (op1,
15486                                                 strict_overflow_p));
15487         }
15488       break;
15489
15490     case MULT_EXPR:
15491       if (TYPE_OVERFLOW_UNDEFINED (type))
15492         {
15493           if (tree_expr_nonzero_warnv_p (op0,
15494                                          strict_overflow_p)
15495               && tree_expr_nonzero_warnv_p (op1,
15496                                             strict_overflow_p))
15497             {
15498               *strict_overflow_p = true;
15499               return true;
15500             }
15501         }
15502       break;
15503
15504     case MIN_EXPR:
15505       sub_strict_overflow_p = false;
15506       if (tree_expr_nonzero_warnv_p (op0,
15507                                      &sub_strict_overflow_p)
15508           && tree_expr_nonzero_warnv_p (op1,
15509                                         &sub_strict_overflow_p))
15510         {
15511           if (sub_strict_overflow_p)
15512             *strict_overflow_p = true;
15513         }
15514       break;
15515
15516     case MAX_EXPR:
15517       sub_strict_overflow_p = false;
15518       if (tree_expr_nonzero_warnv_p (op0,
15519                                      &sub_strict_overflow_p))
15520         {
15521           if (sub_strict_overflow_p)
15522             *strict_overflow_p = true;
15523
15524           /* When both operands are nonzero, then MAX must be too.  */
15525           if (tree_expr_nonzero_warnv_p (op1,
15526                                          strict_overflow_p))
15527             return true;
15528
15529           /* MAX where operand 0 is positive is positive.  */
15530           return tree_expr_nonnegative_warnv_p (op0,
15531                                                strict_overflow_p);
15532         }
15533       /* MAX where operand 1 is positive is positive.  */
15534       else if (tree_expr_nonzero_warnv_p (op1,
15535                                           &sub_strict_overflow_p)
15536                && tree_expr_nonnegative_warnv_p (op1,
15537                                                  &sub_strict_overflow_p))
15538         {
15539           if (sub_strict_overflow_p)
15540             *strict_overflow_p = true;
15541           return true;
15542         }
15543       break;
15544
15545     case BIT_IOR_EXPR:
15546       return (tree_expr_nonzero_warnv_p (op1,
15547                                          strict_overflow_p)
15548               || tree_expr_nonzero_warnv_p (op0,
15549                                             strict_overflow_p));
15550
15551     default:
15552       break;
15553   }
15554
15555   return false;
15556 }
15557
15558 /* Return true when T is an address and is known to be nonzero.
15559    For floating point we further ensure that T is not denormal.
15560    Similar logic is present in nonzero_address in rtlanal.h.
15561
15562    If the return value is based on the assumption that signed overflow
15563    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15564    change *STRICT_OVERFLOW_P.  */
15565
15566 bool
15567 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15568 {
15569   bool sub_strict_overflow_p;
15570   switch (TREE_CODE (t))
15571     {
15572     case INTEGER_CST:
15573       return !integer_zerop (t);
15574
15575     case ADDR_EXPR:
15576       {
15577         tree base = TREE_OPERAND (t, 0);
15578         if (!DECL_P (base))
15579           base = get_base_address (base);
15580
15581         if (!base)
15582           return false;
15583
15584         /* Weak declarations may link to NULL.  Other things may also be NULL
15585            so protect with -fdelete-null-pointer-checks; but not variables
15586            allocated on the stack.  */
15587         if (DECL_P (base)
15588             && (flag_delete_null_pointer_checks
15589                 || (DECL_CONTEXT (base)
15590                     && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
15591                     && auto_var_in_fn_p (base, DECL_CONTEXT (base)))))
15592           return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
15593
15594         /* Constants are never weak.  */
15595         if (CONSTANT_CLASS_P (base))
15596           return true;
15597
15598         return false;
15599       }
15600
15601     case COND_EXPR:
15602       sub_strict_overflow_p = false;
15603       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15604                                      &sub_strict_overflow_p)
15605           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15606                                         &sub_strict_overflow_p))
15607         {
15608           if (sub_strict_overflow_p)
15609             *strict_overflow_p = true;
15610           return true;
15611         }
15612       break;
15613
15614     default:
15615       break;
15616     }
15617   return false;
15618 }
15619
15620 /* Return true when T is an address and is known to be nonzero.
15621    For floating point we further ensure that T is not denormal.
15622    Similar logic is present in nonzero_address in rtlanal.h.
15623
15624    If the return value is based on the assumption that signed overflow
15625    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15626    change *STRICT_OVERFLOW_P.  */
15627
15628 bool
15629 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15630 {
15631   tree type = TREE_TYPE (t);
15632   enum tree_code code;
15633
15634   /* Doing something useful for floating point would need more work.  */
15635   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15636     return false;
15637
15638   code = TREE_CODE (t);
15639   switch (TREE_CODE_CLASS (code))
15640     {
15641     case tcc_unary:
15642       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15643                                               strict_overflow_p);
15644     case tcc_binary:
15645     case tcc_comparison:
15646       return tree_binary_nonzero_warnv_p (code, type,
15647                                                TREE_OPERAND (t, 0),
15648                                                TREE_OPERAND (t, 1),
15649                                                strict_overflow_p);
15650     case tcc_constant:
15651     case tcc_declaration:
15652     case tcc_reference:
15653       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15654
15655     default:
15656       break;
15657     }
15658
15659   switch (code)
15660     {
15661     case TRUTH_NOT_EXPR:
15662       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15663                                               strict_overflow_p);
15664
15665     case TRUTH_AND_EXPR:
15666     case TRUTH_OR_EXPR:
15667     case TRUTH_XOR_EXPR:
15668       return tree_binary_nonzero_warnv_p (code, type,
15669                                                TREE_OPERAND (t, 0),
15670                                                TREE_OPERAND (t, 1),
15671                                                strict_overflow_p);
15672
15673     case COND_EXPR:
15674     case CONSTRUCTOR:
15675     case OBJ_TYPE_REF:
15676     case ASSERT_EXPR:
15677     case ADDR_EXPR:
15678     case WITH_SIZE_EXPR:
15679     case SSA_NAME:
15680       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15681
15682     case COMPOUND_EXPR:
15683     case MODIFY_EXPR:
15684     case BIND_EXPR:
15685       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15686                                         strict_overflow_p);
15687
15688     case SAVE_EXPR:
15689       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15690                                         strict_overflow_p);
15691
15692     case CALL_EXPR:
15693       return alloca_call_p (t);
15694
15695     default:
15696       break;
15697     }
15698   return false;
15699 }
15700
15701 /* Return true when T is an address and is known to be nonzero.
15702    Handle warnings about undefined signed overflow.  */
15703
15704 bool
15705 tree_expr_nonzero_p (tree t)
15706 {
15707   bool ret, strict_overflow_p;
15708
15709   strict_overflow_p = false;
15710   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15711   if (strict_overflow_p)
15712     fold_overflow_warning (("assuming signed overflow does not occur when "
15713                             "determining that expression is always "
15714                             "non-zero"),
15715                            WARN_STRICT_OVERFLOW_MISC);
15716   return ret;
15717 }
15718
15719 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15720    attempt to fold the expression to a constant without modifying TYPE,
15721    OP0 or OP1.
15722
15723    If the expression could be simplified to a constant, then return
15724    the constant.  If the expression would not be simplified to a
15725    constant, then return NULL_TREE.  */
15726
15727 tree
15728 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15729 {
15730   tree tem = fold_binary (code, type, op0, op1);
15731   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15732 }
15733
15734 /* Given the components of a unary expression CODE, TYPE and OP0,
15735    attempt to fold the expression to a constant without modifying
15736    TYPE or OP0.
15737
15738    If the expression could be simplified to a constant, then return
15739    the constant.  If the expression would not be simplified to a
15740    constant, then return NULL_TREE.  */
15741
15742 tree
15743 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15744 {
15745   tree tem = fold_unary (code, type, op0);
15746   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15747 }
15748
15749 /* If EXP represents referencing an element in a constant string
15750    (either via pointer arithmetic or array indexing), return the
15751    tree representing the value accessed, otherwise return NULL.  */
15752
15753 tree
15754 fold_read_from_constant_string (tree exp)
15755 {
15756   if ((TREE_CODE (exp) == INDIRECT_REF
15757        || TREE_CODE (exp) == ARRAY_REF)
15758       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15759     {
15760       tree exp1 = TREE_OPERAND (exp, 0);
15761       tree index;
15762       tree string;
15763       location_t loc = EXPR_LOCATION (exp);
15764
15765       if (TREE_CODE (exp) == INDIRECT_REF)
15766         string = string_constant (exp1, &index);
15767       else
15768         {
15769           tree low_bound = array_ref_low_bound (exp);
15770           index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15771
15772           /* Optimize the special-case of a zero lower bound.
15773
15774              We convert the low_bound to sizetype to avoid some problems
15775              with constant folding.  (E.g. suppose the lower bound is 1,
15776              and its mode is QI.  Without the conversion,l (ARRAY
15777              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15778              +INDEX), which becomes (ARRAY+255+INDEX).  Oops!)  */
15779           if (! integer_zerop (low_bound))
15780             index = size_diffop_loc (loc, index,
15781                                  fold_convert_loc (loc, sizetype, low_bound));
15782
15783           string = exp1;
15784         }
15785
15786       if (string
15787           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15788           && TREE_CODE (string) == STRING_CST
15789           && TREE_CODE (index) == INTEGER_CST
15790           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15791           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15792               == MODE_INT)
15793           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15794         return build_int_cst_type (TREE_TYPE (exp),
15795                                    (TREE_STRING_POINTER (string)
15796                                     [TREE_INT_CST_LOW (index)]));
15797     }
15798   return NULL;
15799 }
15800
15801 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15802    an integer constant, real, or fixed-point constant.
15803
15804    TYPE is the type of the result.  */
15805
15806 static tree
15807 fold_negate_const (tree arg0, tree type)
15808 {
15809   tree t = NULL_TREE;
15810
15811   switch (TREE_CODE (arg0))
15812     {
15813     case INTEGER_CST:
15814       {
15815         double_int val = tree_to_double_int (arg0);
15816         int overflow = neg_double (val.low, val.high, &val.low, &val.high);
15817
15818         t = force_fit_type_double (type, val, 1,
15819                                    (overflow | TREE_OVERFLOW (arg0))
15820                                    && !TYPE_UNSIGNED (type));
15821         break;
15822       }
15823
15824     case REAL_CST:
15825       t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15826       break;
15827
15828     case FIXED_CST:
15829       {
15830         FIXED_VALUE_TYPE f;
15831         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15832                                             &(TREE_FIXED_CST (arg0)), NULL,
15833                                             TYPE_SATURATING (type));
15834         t = build_fixed (type, f);
15835         /* Propagate overflow flags.  */
15836         if (overflow_p | TREE_OVERFLOW (arg0))
15837           TREE_OVERFLOW (t) = 1;
15838         break;
15839       }
15840
15841     default:
15842       gcc_unreachable ();
15843     }
15844
15845   return t;
15846 }
15847
15848 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15849    an integer constant or real constant.
15850
15851    TYPE is the type of the result.  */
15852
15853 tree
15854 fold_abs_const (tree arg0, tree type)
15855 {
15856   tree t = NULL_TREE;
15857
15858   switch (TREE_CODE (arg0))
15859     {
15860     case INTEGER_CST:
15861       {
15862         double_int val = tree_to_double_int (arg0);
15863
15864         /* If the value is unsigned or non-negative, then the absolute value
15865            is the same as the ordinary value.  */
15866         if (TYPE_UNSIGNED (type)
15867             || !double_int_negative_p (val))
15868           t = arg0;
15869
15870         /* If the value is negative, then the absolute value is
15871            its negation.  */
15872         else
15873           {
15874             int overflow;
15875
15876             overflow = neg_double (val.low, val.high, &val.low, &val.high);
15877             t = force_fit_type_double (type, val, -1,
15878                                        overflow | TREE_OVERFLOW (arg0));
15879           }
15880       }
15881       break;
15882
15883     case REAL_CST:
15884       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15885         t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15886       else
15887         t =  arg0;
15888       break;
15889
15890     default:
15891       gcc_unreachable ();
15892     }
15893
15894   return t;
15895 }
15896
15897 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15898    constant.  TYPE is the type of the result.  */
15899
15900 static tree
15901 fold_not_const (const_tree arg0, tree type)
15902 {
15903   double_int val;  
15904
15905   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15906
15907   val = double_int_not (tree_to_double_int (arg0));
15908   return force_fit_type_double (type, val, 0, TREE_OVERFLOW (arg0));
15909 }
15910
15911 /* Given CODE, a relational operator, the target type, TYPE and two
15912    constant operands OP0 and OP1, return the result of the
15913    relational operation.  If the result is not a compile time
15914    constant, then return NULL_TREE.  */
15915
15916 static tree
15917 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15918 {
15919   int result, invert;
15920
15921   /* From here on, the only cases we handle are when the result is
15922      known to be a constant.  */
15923
15924   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15925     {
15926       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15927       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15928
15929       /* Handle the cases where either operand is a NaN.  */
15930       if (real_isnan (c0) || real_isnan (c1))
15931         {
15932           switch (code)
15933             {
15934             case EQ_EXPR:
15935             case ORDERED_EXPR:
15936               result = 0;
15937               break;
15938
15939             case NE_EXPR:
15940             case UNORDERED_EXPR:
15941             case UNLT_EXPR:
15942             case UNLE_EXPR:
15943             case UNGT_EXPR:
15944             case UNGE_EXPR:
15945             case UNEQ_EXPR:
15946               result = 1;
15947               break;
15948
15949             case LT_EXPR:
15950             case LE_EXPR:
15951             case GT_EXPR:
15952             case GE_EXPR:
15953             case LTGT_EXPR:
15954               if (flag_trapping_math)
15955                 return NULL_TREE;
15956               result = 0;
15957               break;
15958
15959             default:
15960               gcc_unreachable ();
15961             }
15962
15963           return constant_boolean_node (result, type);
15964         }
15965
15966       return constant_boolean_node (real_compare (code, c0, c1), type);
15967     }
15968
15969   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15970     {
15971       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15972       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15973       return constant_boolean_node (fixed_compare (code, c0, c1), type);
15974     }
15975
15976   /* Handle equality/inequality of complex constants.  */
15977   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15978     {
15979       tree rcond = fold_relational_const (code, type,
15980                                           TREE_REALPART (op0),
15981                                           TREE_REALPART (op1));
15982       tree icond = fold_relational_const (code, type,
15983                                           TREE_IMAGPART (op0),
15984                                           TREE_IMAGPART (op1));
15985       if (code == EQ_EXPR)
15986         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15987       else if (code == NE_EXPR)
15988         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15989       else
15990         return NULL_TREE;
15991     }
15992
15993   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15994
15995      To compute GT, swap the arguments and do LT.
15996      To compute GE, do LT and invert the result.
15997      To compute LE, swap the arguments, do LT and invert the result.
15998      To compute NE, do EQ and invert the result.
15999
16000      Therefore, the code below must handle only EQ and LT.  */
16001
16002   if (code == LE_EXPR || code == GT_EXPR)
16003     {
16004       tree tem = op0;
16005       op0 = op1;
16006       op1 = tem;
16007       code = swap_tree_comparison (code);
16008     }
16009
16010   /* Note that it is safe to invert for real values here because we
16011      have already handled the one case that it matters.  */
16012
16013   invert = 0;
16014   if (code == NE_EXPR || code == GE_EXPR)
16015     {
16016       invert = 1;
16017       code = invert_tree_comparison (code, false);
16018     }
16019
16020   /* Compute a result for LT or EQ if args permit;
16021      Otherwise return T.  */
16022   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
16023     {
16024       if (code == EQ_EXPR)
16025         result = tree_int_cst_equal (op0, op1);
16026       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
16027         result = INT_CST_LT_UNSIGNED (op0, op1);
16028       else
16029         result = INT_CST_LT (op0, op1);
16030     }
16031   else
16032     return NULL_TREE;
16033
16034   if (invert)
16035     result ^= 1;
16036   return constant_boolean_node (result, type);
16037 }
16038
16039 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
16040    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
16041    itself.  */
16042
16043 tree
16044 fold_build_cleanup_point_expr (tree type, tree expr)
16045 {
16046   /* If the expression does not have side effects then we don't have to wrap
16047      it with a cleanup point expression.  */
16048   if (!TREE_SIDE_EFFECTS (expr))
16049     return expr;
16050
16051   /* If the expression is a return, check to see if the expression inside the
16052      return has no side effects or the right hand side of the modify expression
16053      inside the return. If either don't have side effects set we don't need to
16054      wrap the expression in a cleanup point expression.  Note we don't check the
16055      left hand side of the modify because it should always be a return decl.  */
16056   if (TREE_CODE (expr) == RETURN_EXPR)
16057     {
16058       tree op = TREE_OPERAND (expr, 0);
16059       if (!op || !TREE_SIDE_EFFECTS (op))
16060         return expr;
16061       op = TREE_OPERAND (op, 1);
16062       if (!TREE_SIDE_EFFECTS (op))
16063         return expr;
16064     }
16065
16066   return build1 (CLEANUP_POINT_EXPR, type, expr);
16067 }
16068
16069 /* Given a pointer value OP0 and a type TYPE, return a simplified version
16070    of an indirection through OP0, or NULL_TREE if no simplification is
16071    possible.  */
16072
16073 tree
16074 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
16075 {
16076   tree sub = op0;
16077   tree subtype;
16078
16079   STRIP_NOPS (sub);
16080   subtype = TREE_TYPE (sub);
16081   if (!POINTER_TYPE_P (subtype))
16082     return NULL_TREE;
16083
16084   if (TREE_CODE (sub) == ADDR_EXPR)
16085     {
16086       tree op = TREE_OPERAND (sub, 0);
16087       tree optype = TREE_TYPE (op);
16088       /* *&CONST_DECL -> to the value of the const decl.  */
16089       if (TREE_CODE (op) == CONST_DECL)
16090         return DECL_INITIAL (op);
16091       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
16092       if (type == optype)
16093         {
16094           tree fop = fold_read_from_constant_string (op);
16095           if (fop)
16096             return fop;
16097           else
16098             return op;
16099         }
16100       /* *(foo *)&fooarray => fooarray[0] */
16101       else if (TREE_CODE (optype) == ARRAY_TYPE
16102                && type == TREE_TYPE (optype)
16103                && (!in_gimple_form
16104                    || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16105         {
16106           tree type_domain = TYPE_DOMAIN (optype);
16107           tree min_val = size_zero_node;
16108           if (type_domain && TYPE_MIN_VALUE (type_domain))
16109             min_val = TYPE_MIN_VALUE (type_domain);
16110           if (in_gimple_form
16111               && TREE_CODE (min_val) != INTEGER_CST)
16112             return NULL_TREE;
16113           return build4_loc (loc, ARRAY_REF, type, op, min_val,
16114                              NULL_TREE, NULL_TREE);
16115         }
16116       /* *(foo *)&complexfoo => __real__ complexfoo */
16117       else if (TREE_CODE (optype) == COMPLEX_TYPE
16118                && type == TREE_TYPE (optype))
16119         return fold_build1_loc (loc, REALPART_EXPR, type, op);
16120       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16121       else if (TREE_CODE (optype) == VECTOR_TYPE
16122                && type == TREE_TYPE (optype))
16123         {
16124           tree part_width = TYPE_SIZE (type);
16125           tree index = bitsize_int (0);
16126           return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
16127         }
16128     }
16129
16130   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16131       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
16132     {
16133       tree op00 = TREE_OPERAND (sub, 0);
16134       tree op01 = TREE_OPERAND (sub, 1);
16135
16136       STRIP_NOPS (op00);
16137       if (TREE_CODE (op00) == ADDR_EXPR)
16138         {
16139           tree op00type;
16140           op00 = TREE_OPERAND (op00, 0);
16141           op00type = TREE_TYPE (op00);
16142
16143           /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16144           if (TREE_CODE (op00type) == VECTOR_TYPE
16145               && type == TREE_TYPE (op00type))
16146             {
16147               HOST_WIDE_INT offset = tree_low_cst (op01, 0);
16148               tree part_width = TYPE_SIZE (type);
16149               unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
16150               unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
16151               tree index = bitsize_int (indexi);
16152
16153               if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (op00type))
16154                 return fold_build3_loc (loc,
16155                                         BIT_FIELD_REF, type, op00,
16156                                         part_width, index);
16157
16158             }
16159           /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16160           else if (TREE_CODE (op00type) == COMPLEX_TYPE
16161                    && type == TREE_TYPE (op00type))
16162             {
16163               tree size = TYPE_SIZE_UNIT (type);
16164               if (tree_int_cst_equal (size, op01))
16165                 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
16166             }
16167           /* ((foo *)&fooarray)[1] => fooarray[1] */
16168           else if (TREE_CODE (op00type) == ARRAY_TYPE
16169                    && type == TREE_TYPE (op00type))
16170             {
16171               tree type_domain = TYPE_DOMAIN (op00type);
16172               tree min_val = size_zero_node;
16173               if (type_domain && TYPE_MIN_VALUE (type_domain))
16174                 min_val = TYPE_MIN_VALUE (type_domain);
16175               op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
16176                                      TYPE_SIZE_UNIT (type));
16177               op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
16178               return build4_loc (loc, ARRAY_REF, type, op00, op01,
16179                                  NULL_TREE, NULL_TREE);
16180             }
16181         }
16182     }
16183
16184   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16185   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16186       && type == TREE_TYPE (TREE_TYPE (subtype))
16187       && (!in_gimple_form
16188           || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16189     {
16190       tree type_domain;
16191       tree min_val = size_zero_node;
16192       sub = build_fold_indirect_ref_loc (loc, sub);
16193       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16194       if (type_domain && TYPE_MIN_VALUE (type_domain))
16195         min_val = TYPE_MIN_VALUE (type_domain);
16196       if (in_gimple_form
16197           && TREE_CODE (min_val) != INTEGER_CST)
16198         return NULL_TREE;
16199       return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
16200                          NULL_TREE);
16201     }
16202
16203   return NULL_TREE;
16204 }
16205
16206 /* Builds an expression for an indirection through T, simplifying some
16207    cases.  */
16208
16209 tree
16210 build_fold_indirect_ref_loc (location_t loc, tree t)
16211 {
16212   tree type = TREE_TYPE (TREE_TYPE (t));
16213   tree sub = fold_indirect_ref_1 (loc, type, t);
16214
16215   if (sub)
16216     return sub;
16217
16218   return build1_loc (loc, INDIRECT_REF, type, t);
16219 }
16220
16221 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
16222
16223 tree
16224 fold_indirect_ref_loc (location_t loc, tree t)
16225 {
16226   tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16227
16228   if (sub)
16229     return sub;
16230   else
16231     return t;
16232 }
16233
16234 /* Strip non-trapping, non-side-effecting tree nodes from an expression
16235    whose result is ignored.  The type of the returned tree need not be
16236    the same as the original expression.  */
16237
16238 tree
16239 fold_ignored_result (tree t)
16240 {
16241   if (!TREE_SIDE_EFFECTS (t))
16242     return integer_zero_node;
16243
16244   for (;;)
16245     switch (TREE_CODE_CLASS (TREE_CODE (t)))
16246       {
16247       case tcc_unary:
16248         t = TREE_OPERAND (t, 0);
16249         break;
16250
16251       case tcc_binary:
16252       case tcc_comparison:
16253         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16254           t = TREE_OPERAND (t, 0);
16255         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16256           t = TREE_OPERAND (t, 1);
16257         else
16258           return t;
16259         break;
16260
16261       case tcc_expression:
16262         switch (TREE_CODE (t))
16263           {
16264           case COMPOUND_EXPR:
16265             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16266               return t;
16267             t = TREE_OPERAND (t, 0);
16268             break;
16269
16270           case COND_EXPR:
16271             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16272                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16273               return t;
16274             t = TREE_OPERAND (t, 0);
16275             break;
16276
16277           default:
16278             return t;
16279           }
16280         break;
16281
16282       default:
16283         return t;
16284       }
16285 }
16286
16287 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
16288    This can only be applied to objects of a sizetype.  */
16289
16290 tree
16291 round_up_loc (location_t loc, tree value, int divisor)
16292 {
16293   tree div = NULL_TREE;
16294
16295   gcc_assert (divisor > 0);
16296   if (divisor == 1)
16297     return value;
16298
16299   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
16300      have to do anything.  Only do this when we are not given a const,
16301      because in that case, this check is more expensive than just
16302      doing it.  */
16303   if (TREE_CODE (value) != INTEGER_CST)
16304     {
16305       div = build_int_cst (TREE_TYPE (value), divisor);
16306
16307       if (multiple_of_p (TREE_TYPE (value), value, div))
16308         return value;
16309     }
16310
16311   /* If divisor is a power of two, simplify this to bit manipulation.  */
16312   if (divisor == (divisor & -divisor))
16313     {
16314       if (TREE_CODE (value) == INTEGER_CST)
16315         {
16316           double_int val = tree_to_double_int (value);
16317           bool overflow_p;
16318
16319           if ((val.low & (divisor - 1)) == 0)
16320             return value;
16321
16322           overflow_p = TREE_OVERFLOW (value);
16323           val.low &= ~(divisor - 1);
16324           val.low += divisor;
16325           if (val.low == 0)
16326             {
16327               val.high++;
16328               if (val.high == 0)
16329                 overflow_p = true;
16330             }
16331
16332           return force_fit_type_double (TREE_TYPE (value), val,
16333                                         -1, overflow_p);
16334         }
16335       else
16336         {
16337           tree t;
16338
16339           t = build_int_cst (TREE_TYPE (value), divisor - 1);
16340           value = size_binop_loc (loc, PLUS_EXPR, value, t);
16341           t = build_int_cst (TREE_TYPE (value), -divisor);
16342           value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16343         }
16344     }
16345   else
16346     {
16347       if (!div)
16348         div = build_int_cst (TREE_TYPE (value), divisor);
16349       value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16350       value = size_binop_loc (loc, MULT_EXPR, value, div);
16351     }
16352
16353   return value;
16354 }
16355
16356 /* Likewise, but round down.  */
16357
16358 tree
16359 round_down_loc (location_t loc, tree value, int divisor)
16360 {
16361   tree div = NULL_TREE;
16362
16363   gcc_assert (divisor > 0);
16364   if (divisor == 1)
16365     return value;
16366
16367   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
16368      have to do anything.  Only do this when we are not given a const,
16369      because in that case, this check is more expensive than just
16370      doing it.  */
16371   if (TREE_CODE (value) != INTEGER_CST)
16372     {
16373       div = build_int_cst (TREE_TYPE (value), divisor);
16374
16375       if (multiple_of_p (TREE_TYPE (value), value, div))
16376         return value;
16377     }
16378
16379   /* If divisor is a power of two, simplify this to bit manipulation.  */
16380   if (divisor == (divisor & -divisor))
16381     {
16382       tree t;
16383
16384       t = build_int_cst (TREE_TYPE (value), -divisor);
16385       value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16386     }
16387   else
16388     {
16389       if (!div)
16390         div = build_int_cst (TREE_TYPE (value), divisor);
16391       value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16392       value = size_binop_loc (loc, MULT_EXPR, value, div);
16393     }
16394
16395   return value;
16396 }
16397
16398 /* Returns the pointer to the base of the object addressed by EXP and
16399    extracts the information about the offset of the access, storing it
16400    to PBITPOS and POFFSET.  */
16401
16402 static tree
16403 split_address_to_core_and_offset (tree exp,
16404                                   HOST_WIDE_INT *pbitpos, tree *poffset)
16405 {
16406   tree core;
16407   enum machine_mode mode;
16408   int unsignedp, volatilep;
16409   HOST_WIDE_INT bitsize;
16410   location_t loc = EXPR_LOCATION (exp);
16411
16412   if (TREE_CODE (exp) == ADDR_EXPR)
16413     {
16414       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16415                                   poffset, &mode, &unsignedp, &volatilep,
16416                                   false);
16417       core = build_fold_addr_expr_loc (loc, core);
16418     }
16419   else
16420     {
16421       core = exp;
16422       *pbitpos = 0;
16423       *poffset = NULL_TREE;
16424     }
16425
16426   return core;
16427 }
16428
16429 /* Returns true if addresses of E1 and E2 differ by a constant, false
16430    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
16431
16432 bool
16433 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
16434 {
16435   tree core1, core2;
16436   HOST_WIDE_INT bitpos1, bitpos2;
16437   tree toffset1, toffset2, tdiff, type;
16438
16439   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16440   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16441
16442   if (bitpos1 % BITS_PER_UNIT != 0
16443       || bitpos2 % BITS_PER_UNIT != 0
16444       || !operand_equal_p (core1, core2, 0))
16445     return false;
16446
16447   if (toffset1 && toffset2)
16448     {
16449       type = TREE_TYPE (toffset1);
16450       if (type != TREE_TYPE (toffset2))
16451         toffset2 = fold_convert (type, toffset2);
16452
16453       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16454       if (!cst_and_fits_in_hwi (tdiff))
16455         return false;
16456
16457       *diff = int_cst_value (tdiff);
16458     }
16459   else if (toffset1 || toffset2)
16460     {
16461       /* If only one of the offsets is non-constant, the difference cannot
16462          be a constant.  */
16463       return false;
16464     }
16465   else
16466     *diff = 0;
16467
16468   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
16469   return true;
16470 }
16471
16472 /* Simplify the floating point expression EXP when the sign of the
16473    result is not significant.  Return NULL_TREE if no simplification
16474    is possible.  */
16475
16476 tree
16477 fold_strip_sign_ops (tree exp)
16478 {
16479   tree arg0, arg1;
16480   location_t loc = EXPR_LOCATION (exp);
16481
16482   switch (TREE_CODE (exp))
16483     {
16484     case ABS_EXPR:
16485     case NEGATE_EXPR:
16486       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16487       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
16488
16489     case MULT_EXPR:
16490     case RDIV_EXPR:
16491       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
16492         return NULL_TREE;
16493       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16494       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16495       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
16496         return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
16497                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
16498                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
16499       break;
16500
16501     case COMPOUND_EXPR:
16502       arg0 = TREE_OPERAND (exp, 0);
16503       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16504       if (arg1)
16505         return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
16506       break;
16507
16508     case COND_EXPR:
16509       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16510       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
16511       if (arg0 || arg1)
16512         return fold_build3_loc (loc,
16513                             COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
16514                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
16515                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
16516       break;
16517
16518     case CALL_EXPR:
16519       {
16520         const enum built_in_function fcode = builtin_mathfn_code (exp);
16521         switch (fcode)
16522         {
16523         CASE_FLT_FN (BUILT_IN_COPYSIGN):
16524           /* Strip copysign function call, return the 1st argument. */
16525           arg0 = CALL_EXPR_ARG (exp, 0);
16526           arg1 = CALL_EXPR_ARG (exp, 1);
16527           return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
16528
16529         default:
16530           /* Strip sign ops from the argument of "odd" math functions.  */
16531           if (negate_mathfn_p (fcode))
16532             {
16533               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
16534               if (arg0)
16535                 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
16536             }
16537           break;
16538         }
16539       }
16540       break;
16541
16542     default:
16543       break;
16544     }
16545   return NULL_TREE;
16546 }