OSDN Git Service

* fold-const.c (fold_indirect_ref_1): Handle folding
[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
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /*@@ This file should be rewritten to use an arbitrary precision
23   @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
24   @@ Perhaps the routines could also be used for bc/dc, and made a lib.
25   @@ The routines that translate from the ap rep should
26   @@ warn if precision et. al. is lost.
27   @@ This would also make life easier when this technology is used
28   @@ for cross-compilers.  */
29
30 /* The entry points in this file are fold, size_int_wide and size_binop.
31
32    fold takes a tree as argument and returns a simplified tree.
33
34    size_binop takes a tree code for an arithmetic operation
35    and two operands that are trees, and produces a tree for the
36    result, assuming the type comes from `sizetype'.
37
38    size_int takes an integer value, and creates a tree constant
39    with type from `sizetype'.
40
41    Note: Since the folders get called on non-gimple code as well as
42    gimple code, we need to handle GIMPLE tuples as well as their
43    corresponding tree equivalents.  */
44
45 #include "config.h"
46 #include "system.h"
47 #include "coretypes.h"
48 #include "tm.h"
49 #include "flags.h"
50 #include "tree.h"
51 #include "realmpfr.h"
52 #include "rtl.h"
53 #include "expr.h"
54 #include "tm_p.h"
55 #include "target.h"
56 #include "diagnostic-core.h"
57 #include "toplev.h"
58 #include "intl.h"
59 #include "ggc.h"
60 #include "hashtab.h"
61 #include "langhooks.h"
62 #include "md5.h"
63 #include "gimple.h"
64 #include "tree-flow.h"
65
66 /* Nonzero if we are folding constants inside an initializer; zero
67    otherwise.  */
68 int folding_initializer = 0;
69
70 /* The following constants represent a bit based encoding of GCC's
71    comparison operators.  This encoding simplifies transformations
72    on relational comparison operators, such as AND and OR.  */
73 enum comparison_code {
74   COMPCODE_FALSE = 0,
75   COMPCODE_LT = 1,
76   COMPCODE_EQ = 2,
77   COMPCODE_LE = 3,
78   COMPCODE_GT = 4,
79   COMPCODE_LTGT = 5,
80   COMPCODE_GE = 6,
81   COMPCODE_ORD = 7,
82   COMPCODE_UNORD = 8,
83   COMPCODE_UNLT = 9,
84   COMPCODE_UNEQ = 10,
85   COMPCODE_UNLE = 11,
86   COMPCODE_UNGT = 12,
87   COMPCODE_NE = 13,
88   COMPCODE_UNGE = 14,
89   COMPCODE_TRUE = 15
90 };
91
92 static bool negate_mathfn_p (enum built_in_function);
93 static bool negate_expr_p (tree);
94 static tree negate_expr (tree);
95 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
96 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
97 static tree const_binop (enum tree_code, tree, tree);
98 static enum comparison_code comparison_to_compcode (enum tree_code);
99 static enum tree_code compcode_to_comparison (enum comparison_code);
100 static int operand_equal_for_comparison_p (tree, tree, tree);
101 static int twoval_comparison_p (tree, tree *, tree *, int *);
102 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
103 static tree pedantic_omit_one_operand_loc (location_t, tree, tree, tree);
104 static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree);
105 static tree make_bit_field_ref (location_t, tree, tree,
106                                 HOST_WIDE_INT, HOST_WIDE_INT, int);
107 static tree optimize_bit_field_compare (location_t, enum tree_code,
108                                         tree, tree, tree);
109 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
110                                     HOST_WIDE_INT *,
111                                     enum machine_mode *, int *, int *,
112                                     tree *, tree *);
113 static int all_ones_mask_p (const_tree, int);
114 static tree sign_bit_p (tree, const_tree);
115 static int simple_operand_p (const_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 extern tree make_range (tree, int *, tree *, tree *, bool *);
120 extern bool merge_ranges (int *, tree *, tree *, int, tree, tree, int,
121                           tree, tree);
122 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
123 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
124 static tree unextend (tree, int, int, tree);
125 static tree fold_truthop (location_t, enum tree_code, tree, tree, tree);
126 static tree optimize_minmax_comparison (location_t, enum tree_code,
127                                         tree, tree, tree);
128 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
129 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
130 static tree fold_binary_op_with_conditional_arg (location_t,
131                                                  enum tree_code, tree,
132                                                  tree, tree,
133                                                  tree, tree, int);
134 static tree fold_mathfn_compare (location_t,
135                                  enum built_in_function, enum tree_code,
136                                  tree, tree, tree);
137 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
138 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
139 static bool reorder_operands_p (const_tree, const_tree);
140 static tree fold_negate_const (tree, tree);
141 static tree fold_not_const (const_tree, tree);
142 static tree fold_relational_const (enum tree_code, tree, tree, tree);
143 static tree fold_convert_const (enum tree_code, tree, tree);
144
145
146 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
147    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
148    and SUM1.  Then this yields nonzero if overflow occurred during the
149    addition.
150
151    Overflow occurs if A and B have the same sign, but A and SUM differ in
152    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
153    sign.  */
154 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
155 \f
156 /* If ARG2 divides ARG1 with zero remainder, carries out the division
157    of type CODE and returns the quotient.
158    Otherwise returns NULL_TREE.  */
159
160 tree
161 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
162 {
163   double_int quo, rem;
164   int uns;
165
166   /* The sign of the division is according to operand two, that
167      does the correct thing for POINTER_PLUS_EXPR where we want
168      a signed division.  */
169   uns = TYPE_UNSIGNED (TREE_TYPE (arg2));
170   if (TREE_CODE (TREE_TYPE (arg2)) == INTEGER_TYPE
171       && TYPE_IS_SIZETYPE (TREE_TYPE (arg2)))
172     uns = false;
173
174   quo = double_int_divmod (tree_to_double_int (arg1),
175                            tree_to_double_int (arg2),
176                            uns, code, &rem);
177
178   if (double_int_zero_p (rem))
179     return build_int_cst_wide (TREE_TYPE (arg1), quo.low, quo.high);
180
181   return NULL_TREE; 
182 }
183 \f
184 /* This is nonzero if we should defer warnings about undefined
185    overflow.  This facility exists because these warnings are a
186    special case.  The code to estimate loop iterations does not want
187    to issue any warnings, since it works with expressions which do not
188    occur in user code.  Various bits of cleanup code call fold(), but
189    only use the result if it has certain characteristics (e.g., is a
190    constant); that code only wants to issue a warning if the result is
191    used.  */
192
193 static int fold_deferring_overflow_warnings;
194
195 /* If a warning about undefined overflow is deferred, this is the
196    warning.  Note that this may cause us to turn two warnings into
197    one, but that is fine since it is sufficient to only give one
198    warning per expression.  */
199
200 static const char* fold_deferred_overflow_warning;
201
202 /* If a warning about undefined overflow is deferred, this is the
203    level at which the warning should be emitted.  */
204
205 static enum warn_strict_overflow_code fold_deferred_overflow_code;
206
207 /* Start deferring overflow warnings.  We could use a stack here to
208    permit nested calls, but at present it is not necessary.  */
209
210 void
211 fold_defer_overflow_warnings (void)
212 {
213   ++fold_deferring_overflow_warnings;
214 }
215
216 /* Stop deferring overflow warnings.  If there is a pending warning,
217    and ISSUE is true, then issue the warning if appropriate.  STMT is
218    the statement with which the warning should be associated (used for
219    location information); STMT may be NULL.  CODE is the level of the
220    warning--a warn_strict_overflow_code value.  This function will use
221    the smaller of CODE and the deferred code when deciding whether to
222    issue the warning.  CODE may be zero to mean to always use the
223    deferred code.  */
224
225 void
226 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
227 {
228   const char *warnmsg;
229   location_t locus;
230
231   gcc_assert (fold_deferring_overflow_warnings > 0);
232   --fold_deferring_overflow_warnings;
233   if (fold_deferring_overflow_warnings > 0)
234     {
235       if (fold_deferred_overflow_warning != NULL
236           && code != 0
237           && code < (int) fold_deferred_overflow_code)
238         fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
239       return;
240     }
241
242   warnmsg = fold_deferred_overflow_warning;
243   fold_deferred_overflow_warning = NULL;
244
245   if (!issue || warnmsg == NULL)
246     return;
247
248   if (gimple_no_warning_p (stmt))
249     return;
250
251   /* Use the smallest code level when deciding to issue the
252      warning.  */
253   if (code == 0 || code > (int) fold_deferred_overflow_code)
254     code = fold_deferred_overflow_code;
255
256   if (!issue_strict_overflow_warning (code))
257     return;
258
259   if (stmt == NULL)
260     locus = input_location;
261   else
262     locus = gimple_location (stmt);
263   warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
264 }
265
266 /* Stop deferring overflow warnings, ignoring any deferred
267    warnings.  */
268
269 void
270 fold_undefer_and_ignore_overflow_warnings (void)
271 {
272   fold_undefer_overflow_warnings (false, NULL, 0);
273 }
274
275 /* Whether we are deferring overflow warnings.  */
276
277 bool
278 fold_deferring_overflow_warnings_p (void)
279 {
280   return fold_deferring_overflow_warnings > 0;
281 }
282
283 /* This is called when we fold something based on the fact that signed
284    overflow is undefined.  */
285
286 static void
287 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
288 {
289   if (fold_deferring_overflow_warnings > 0)
290     {
291       if (fold_deferred_overflow_warning == NULL
292           || wc < fold_deferred_overflow_code)
293         {
294           fold_deferred_overflow_warning = gmsgid;
295           fold_deferred_overflow_code = wc;
296         }
297     }
298   else if (issue_strict_overflow_warning (wc))
299     warning (OPT_Wstrict_overflow, gmsgid);
300 }
301 \f
302 /* Return true if the built-in mathematical function specified by CODE
303    is odd, i.e. -f(x) == f(-x).  */
304
305 static bool
306 negate_mathfn_p (enum built_in_function code)
307 {
308   switch (code)
309     {
310     CASE_FLT_FN (BUILT_IN_ASIN):
311     CASE_FLT_FN (BUILT_IN_ASINH):
312     CASE_FLT_FN (BUILT_IN_ATAN):
313     CASE_FLT_FN (BUILT_IN_ATANH):
314     CASE_FLT_FN (BUILT_IN_CASIN):
315     CASE_FLT_FN (BUILT_IN_CASINH):
316     CASE_FLT_FN (BUILT_IN_CATAN):
317     CASE_FLT_FN (BUILT_IN_CATANH):
318     CASE_FLT_FN (BUILT_IN_CBRT):
319     CASE_FLT_FN (BUILT_IN_CPROJ):
320     CASE_FLT_FN (BUILT_IN_CSIN):
321     CASE_FLT_FN (BUILT_IN_CSINH):
322     CASE_FLT_FN (BUILT_IN_CTAN):
323     CASE_FLT_FN (BUILT_IN_CTANH):
324     CASE_FLT_FN (BUILT_IN_ERF):
325     CASE_FLT_FN (BUILT_IN_LLROUND):
326     CASE_FLT_FN (BUILT_IN_LROUND):
327     CASE_FLT_FN (BUILT_IN_ROUND):
328     CASE_FLT_FN (BUILT_IN_SIN):
329     CASE_FLT_FN (BUILT_IN_SINH):
330     CASE_FLT_FN (BUILT_IN_TAN):
331     CASE_FLT_FN (BUILT_IN_TANH):
332     CASE_FLT_FN (BUILT_IN_TRUNC):
333       return true;
334
335     CASE_FLT_FN (BUILT_IN_LLRINT):
336     CASE_FLT_FN (BUILT_IN_LRINT):
337     CASE_FLT_FN (BUILT_IN_NEARBYINT):
338     CASE_FLT_FN (BUILT_IN_RINT):
339       return !flag_rounding_math;
340
341     default:
342       break;
343     }
344   return false;
345 }
346
347 /* Check whether we may negate an integer constant T without causing
348    overflow.  */
349
350 bool
351 may_negate_without_overflow_p (const_tree t)
352 {
353   unsigned HOST_WIDE_INT val;
354   unsigned int prec;
355   tree type;
356
357   gcc_assert (TREE_CODE (t) == INTEGER_CST);
358
359   type = TREE_TYPE (t);
360   if (TYPE_UNSIGNED (type))
361     return false;
362
363   prec = TYPE_PRECISION (type);
364   if (prec > HOST_BITS_PER_WIDE_INT)
365     {
366       if (TREE_INT_CST_LOW (t) != 0)
367         return true;
368       prec -= HOST_BITS_PER_WIDE_INT;
369       val = TREE_INT_CST_HIGH (t);
370     }
371   else
372     val = TREE_INT_CST_LOW (t);
373   if (prec < HOST_BITS_PER_WIDE_INT)
374     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
375   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
376 }
377
378 /* Determine whether an expression T can be cheaply negated using
379    the function negate_expr without introducing undefined overflow.  */
380
381 static bool
382 negate_expr_p (tree t)
383 {
384   tree type;
385
386   if (t == 0)
387     return false;
388
389   type = TREE_TYPE (t);
390
391   STRIP_SIGN_NOPS (t);
392   switch (TREE_CODE (t))
393     {
394     case INTEGER_CST:
395       if (TYPE_OVERFLOW_WRAPS (type))
396         return true;
397
398       /* Check that -CST will not overflow type.  */
399       return may_negate_without_overflow_p (t);
400     case BIT_NOT_EXPR:
401       return (INTEGRAL_TYPE_P (type)
402               && TYPE_OVERFLOW_WRAPS (type));
403
404     case FIXED_CST:
405     case NEGATE_EXPR:
406       return true;
407
408     case REAL_CST:
409       /* We want to canonicalize to positive real constants.  Pretend
410          that only negative ones can be easily negated.  */
411       return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
412
413     case COMPLEX_CST:
414       return negate_expr_p (TREE_REALPART (t))
415              && negate_expr_p (TREE_IMAGPART (t));
416
417     case COMPLEX_EXPR:
418       return negate_expr_p (TREE_OPERAND (t, 0))
419              && negate_expr_p (TREE_OPERAND (t, 1));
420
421     case CONJ_EXPR:
422       return negate_expr_p (TREE_OPERAND (t, 0));
423
424     case PLUS_EXPR:
425       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
426           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
427         return false;
428       /* -(A + B) -> (-B) - A.  */
429       if (negate_expr_p (TREE_OPERAND (t, 1))
430           && reorder_operands_p (TREE_OPERAND (t, 0),
431                                  TREE_OPERAND (t, 1)))
432         return true;
433       /* -(A + B) -> (-A) - B.  */
434       return negate_expr_p (TREE_OPERAND (t, 0));
435
436     case MINUS_EXPR:
437       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
438       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
439              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
440              && reorder_operands_p (TREE_OPERAND (t, 0),
441                                     TREE_OPERAND (t, 1));
442
443     case MULT_EXPR:
444       if (TYPE_UNSIGNED (TREE_TYPE (t)))
445         break;
446
447       /* Fall through.  */
448
449     case RDIV_EXPR:
450       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
451         return negate_expr_p (TREE_OPERAND (t, 1))
452                || negate_expr_p (TREE_OPERAND (t, 0));
453       break;
454
455     case TRUNC_DIV_EXPR:
456     case ROUND_DIV_EXPR:
457     case FLOOR_DIV_EXPR:
458     case CEIL_DIV_EXPR:
459     case EXACT_DIV_EXPR:
460       /* In general we can't negate A / B, because if A is INT_MIN and
461          B is 1, we may turn this into INT_MIN / -1 which is undefined
462          and actually traps on some architectures.  But if overflow is
463          undefined, we can negate, because - (INT_MIN / 1) is an
464          overflow.  */
465       if (INTEGRAL_TYPE_P (TREE_TYPE (t))
466           && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
467         break;
468       return negate_expr_p (TREE_OPERAND (t, 1))
469              || negate_expr_p (TREE_OPERAND (t, 0));
470
471     case NOP_EXPR:
472       /* Negate -((double)float) as (double)(-float).  */
473       if (TREE_CODE (type) == REAL_TYPE)
474         {
475           tree tem = strip_float_extensions (t);
476           if (tem != t)
477             return negate_expr_p (tem);
478         }
479       break;
480
481     case CALL_EXPR:
482       /* Negate -f(x) as f(-x).  */
483       if (negate_mathfn_p (builtin_mathfn_code (t)))
484         return negate_expr_p (CALL_EXPR_ARG (t, 0));
485       break;
486
487     case RSHIFT_EXPR:
488       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
489       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
490         {
491           tree op1 = TREE_OPERAND (t, 1);
492           if (TREE_INT_CST_HIGH (op1) == 0
493               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
494                  == TREE_INT_CST_LOW (op1))
495             return true;
496         }
497       break;
498
499     default:
500       break;
501     }
502   return false;
503 }
504
505 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
506    simplification is possible.
507    If negate_expr_p would return true for T, NULL_TREE will never be
508    returned.  */
509
510 static tree
511 fold_negate_expr (location_t loc, tree t)
512 {
513   tree type = TREE_TYPE (t);
514   tree tem;
515
516   switch (TREE_CODE (t))
517     {
518     /* Convert - (~A) to A + 1.  */
519     case BIT_NOT_EXPR:
520       if (INTEGRAL_TYPE_P (type))
521         return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
522                             build_int_cst (type, 1));
523       break;
524
525     case INTEGER_CST:
526       tem = fold_negate_const (t, type);
527       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
528           || !TYPE_OVERFLOW_TRAPS (type))
529         return tem;
530       break;
531
532     case REAL_CST:
533       tem = fold_negate_const (t, type);
534       /* Two's complement FP formats, such as c4x, may overflow.  */
535       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
536         return tem;
537       break;
538
539     case FIXED_CST:
540       tem = fold_negate_const (t, type);
541       return tem;
542
543     case COMPLEX_CST:
544       {
545         tree rpart = negate_expr (TREE_REALPART (t));
546         tree ipart = negate_expr (TREE_IMAGPART (t));
547
548         if ((TREE_CODE (rpart) == REAL_CST
549              && TREE_CODE (ipart) == REAL_CST)
550             || (TREE_CODE (rpart) == INTEGER_CST
551                 && TREE_CODE (ipart) == INTEGER_CST))
552           return build_complex (type, rpart, ipart);
553       }
554       break;
555
556     case COMPLEX_EXPR:
557       if (negate_expr_p (t))
558         return fold_build2_loc (loc, COMPLEX_EXPR, type,
559                             fold_negate_expr (loc, TREE_OPERAND (t, 0)),
560                             fold_negate_expr (loc, TREE_OPERAND (t, 1)));
561       break;
562
563     case CONJ_EXPR:
564       if (negate_expr_p (t))
565         return fold_build1_loc (loc, CONJ_EXPR, type,
566                             fold_negate_expr (loc, TREE_OPERAND (t, 0)));
567       break;
568
569     case NEGATE_EXPR:
570       return TREE_OPERAND (t, 0);
571
572     case PLUS_EXPR:
573       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
574           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
575         {
576           /* -(A + B) -> (-B) - A.  */
577           if (negate_expr_p (TREE_OPERAND (t, 1))
578               && reorder_operands_p (TREE_OPERAND (t, 0),
579                                      TREE_OPERAND (t, 1)))
580             {
581               tem = negate_expr (TREE_OPERAND (t, 1));
582               return fold_build2_loc (loc, MINUS_EXPR, type,
583                                   tem, TREE_OPERAND (t, 0));
584             }
585
586           /* -(A + B) -> (-A) - B.  */
587           if (negate_expr_p (TREE_OPERAND (t, 0)))
588             {
589               tem = negate_expr (TREE_OPERAND (t, 0));
590               return fold_build2_loc (loc, MINUS_EXPR, type,
591                                   tem, TREE_OPERAND (t, 1));
592             }
593         }
594       break;
595
596     case MINUS_EXPR:
597       /* - (A - B) -> B - A  */
598       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
599           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
600           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
601         return fold_build2_loc (loc, MINUS_EXPR, type,
602                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
603       break;
604
605     case MULT_EXPR:
606       if (TYPE_UNSIGNED (type))
607         break;
608
609       /* Fall through.  */
610
611     case RDIV_EXPR:
612       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
613         {
614           tem = TREE_OPERAND (t, 1);
615           if (negate_expr_p (tem))
616             return fold_build2_loc (loc, TREE_CODE (t), type,
617                                 TREE_OPERAND (t, 0), negate_expr (tem));
618           tem = TREE_OPERAND (t, 0);
619           if (negate_expr_p (tem))
620             return fold_build2_loc (loc, TREE_CODE (t), type,
621                                 negate_expr (tem), TREE_OPERAND (t, 1));
622         }
623       break;
624
625     case TRUNC_DIV_EXPR:
626     case ROUND_DIV_EXPR:
627     case FLOOR_DIV_EXPR:
628     case CEIL_DIV_EXPR:
629     case EXACT_DIV_EXPR:
630       /* In general we can't negate A / B, because if A is INT_MIN and
631          B is 1, we may turn this into INT_MIN / -1 which is undefined
632          and actually traps on some architectures.  But if overflow is
633          undefined, we can negate, because - (INT_MIN / 1) is an
634          overflow.  */
635       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
636         {
637           const char * const warnmsg = G_("assuming signed overflow does not "
638                                           "occur when negating a division");
639           tem = TREE_OPERAND (t, 1);
640           if (negate_expr_p (tem))
641             {
642               if (INTEGRAL_TYPE_P (type)
643                   && (TREE_CODE (tem) != INTEGER_CST
644                       || integer_onep (tem)))
645                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
646               return fold_build2_loc (loc, TREE_CODE (t), type,
647                                   TREE_OPERAND (t, 0), negate_expr (tem));
648             }
649           tem = TREE_OPERAND (t, 0);
650           if (negate_expr_p (tem))
651             {
652               if (INTEGRAL_TYPE_P (type)
653                   && (TREE_CODE (tem) != INTEGER_CST
654                       || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
655                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
656               return fold_build2_loc (loc, TREE_CODE (t), type,
657                                   negate_expr (tem), TREE_OPERAND (t, 1));
658             }
659         }
660       break;
661
662     case NOP_EXPR:
663       /* Convert -((double)float) into (double)(-float).  */
664       if (TREE_CODE (type) == REAL_TYPE)
665         {
666           tem = strip_float_extensions (t);
667           if (tem != t && negate_expr_p (tem))
668             return fold_convert_loc (loc, type, negate_expr (tem));
669         }
670       break;
671
672     case CALL_EXPR:
673       /* Negate -f(x) as f(-x).  */
674       if (negate_mathfn_p (builtin_mathfn_code (t))
675           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
676         {
677           tree fndecl, arg;
678
679           fndecl = get_callee_fndecl (t);
680           arg = negate_expr (CALL_EXPR_ARG (t, 0));
681           return build_call_expr_loc (loc, fndecl, 1, arg);
682         }
683       break;
684
685     case RSHIFT_EXPR:
686       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
687       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
688         {
689           tree op1 = TREE_OPERAND (t, 1);
690           if (TREE_INT_CST_HIGH (op1) == 0
691               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
692                  == TREE_INT_CST_LOW (op1))
693             {
694               tree ntype = TYPE_UNSIGNED (type)
695                            ? signed_type_for (type)
696                            : unsigned_type_for (type);
697               tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
698               temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
699               return fold_convert_loc (loc, type, temp);
700             }
701         }
702       break;
703
704     default:
705       break;
706     }
707
708   return NULL_TREE;
709 }
710
711 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
712    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
713    return NULL_TREE. */
714
715 static tree
716 negate_expr (tree t)
717 {
718   tree type, tem;
719   location_t loc;
720
721   if (t == NULL_TREE)
722     return NULL_TREE;
723
724   loc = EXPR_LOCATION (t);
725   type = TREE_TYPE (t);
726   STRIP_SIGN_NOPS (t);
727
728   tem = fold_negate_expr (loc, t);
729   if (!tem)
730     {
731       tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
732       SET_EXPR_LOCATION (tem, loc);
733     }
734   return fold_convert_loc (loc, type, tem);
735 }
736 \f
737 /* Split a tree IN into a constant, literal and variable parts that could be
738    combined with CODE to make IN.  "constant" means an expression with
739    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
740    commutative arithmetic operation.  Store the constant part into *CONP,
741    the literal in *LITP and return the variable part.  If a part isn't
742    present, set it to null.  If the tree does not decompose in this way,
743    return the entire tree as the variable part and the other parts as null.
744
745    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
746    case, we negate an operand that was subtracted.  Except if it is a
747    literal for which we use *MINUS_LITP instead.
748
749    If NEGATE_P is true, we are negating all of IN, again except a literal
750    for which we use *MINUS_LITP instead.
751
752    If IN is itself a literal or constant, return it as appropriate.
753
754    Note that we do not guarantee that any of the three values will be the
755    same type as IN, but they will have the same signedness and mode.  */
756
757 static tree
758 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
759             tree *minus_litp, int negate_p)
760 {
761   tree var = 0;
762
763   *conp = 0;
764   *litp = 0;
765   *minus_litp = 0;
766
767   /* Strip any conversions that don't change the machine mode or signedness.  */
768   STRIP_SIGN_NOPS (in);
769
770   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
771       || TREE_CODE (in) == FIXED_CST)
772     *litp = in;
773   else if (TREE_CODE (in) == code
774            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
775                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
776                /* We can associate addition and subtraction together (even
777                   though the C standard doesn't say so) for integers because
778                   the value is not affected.  For reals, the value might be
779                   affected, so we can't.  */
780                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
781                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
782     {
783       tree op0 = TREE_OPERAND (in, 0);
784       tree op1 = TREE_OPERAND (in, 1);
785       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
786       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
787
788       /* First see if either of the operands is a literal, then a constant.  */
789       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
790           || TREE_CODE (op0) == FIXED_CST)
791         *litp = op0, op0 = 0;
792       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
793                || TREE_CODE (op1) == FIXED_CST)
794         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
795
796       if (op0 != 0 && TREE_CONSTANT (op0))
797         *conp = op0, op0 = 0;
798       else if (op1 != 0 && TREE_CONSTANT (op1))
799         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
800
801       /* If we haven't dealt with either operand, this is not a case we can
802          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
803       if (op0 != 0 && op1 != 0)
804         var = in;
805       else if (op0 != 0)
806         var = op0;
807       else
808         var = op1, neg_var_p = neg1_p;
809
810       /* Now do any needed negations.  */
811       if (neg_litp_p)
812         *minus_litp = *litp, *litp = 0;
813       if (neg_conp_p)
814         *conp = negate_expr (*conp);
815       if (neg_var_p)
816         var = negate_expr (var);
817     }
818   else if (TREE_CONSTANT (in))
819     *conp = in;
820   else
821     var = in;
822
823   if (negate_p)
824     {
825       if (*litp)
826         *minus_litp = *litp, *litp = 0;
827       else if (*minus_litp)
828         *litp = *minus_litp, *minus_litp = 0;
829       *conp = negate_expr (*conp);
830       var = negate_expr (var);
831     }
832
833   return var;
834 }
835
836 /* Re-associate trees split by the above function.  T1 and T2 are
837    either expressions to associate or null.  Return the new
838    expression, if any.  LOC is the location of the new expression.  If
839    we build an operation, do it in TYPE and with CODE.  */
840
841 static tree
842 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
843 {
844   tree tem;
845
846   if (t1 == 0)
847     return t2;
848   else if (t2 == 0)
849     return t1;
850
851   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
852      try to fold this since we will have infinite recursion.  But do
853      deal with any NEGATE_EXPRs.  */
854   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
855       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
856     {
857       if (code == PLUS_EXPR)
858         {
859           if (TREE_CODE (t1) == NEGATE_EXPR)
860             tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t2),
861                           fold_convert_loc (loc, type, TREE_OPERAND (t1, 0)));
862           else if (TREE_CODE (t2) == NEGATE_EXPR)
863             tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t1),
864                           fold_convert_loc (loc, type, TREE_OPERAND (t2, 0)));
865           else if (integer_zerop (t2))
866             return fold_convert_loc (loc, type, t1);
867         }
868       else if (code == MINUS_EXPR)
869         {
870           if (integer_zerop (t2))
871             return fold_convert_loc (loc, type, t1);
872         }
873
874       tem = build2 (code, type, fold_convert_loc (loc, type, t1),
875                     fold_convert_loc (loc, type, t2));
876       goto associate_trees_exit;
877     }
878
879   return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
880                       fold_convert_loc (loc, type, t2));
881  associate_trees_exit:
882   protected_set_expr_location (tem, loc);
883   return tem;
884 }
885 \f
886 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
887    for use in int_const_binop, size_binop and size_diffop.  */
888
889 static bool
890 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
891 {
892   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
893     return false;
894   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
895     return false;
896
897   switch (code)
898     {
899     case LSHIFT_EXPR:
900     case RSHIFT_EXPR:
901     case LROTATE_EXPR:
902     case RROTATE_EXPR:
903       return true;
904
905     default:
906       break;
907     }
908
909   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
910          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
911          && TYPE_MODE (type1) == TYPE_MODE (type2);
912 }
913
914
915 /* Combine two integer constants ARG1 and ARG2 under operation CODE
916    to produce a new constant.  Return NULL_TREE if we don't know how
917    to evaluate CODE at compile-time.
918
919    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
920
921 tree
922 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
923 {
924   double_int op1, op2, res, tmp;
925   tree t;
926   tree type = TREE_TYPE (arg1);
927   bool uns = TYPE_UNSIGNED (type);
928   bool is_sizetype
929     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
930   bool overflow = false;
931
932   op1 = tree_to_double_int (arg1);
933   op2 = tree_to_double_int (arg2);
934
935   switch (code)
936     {
937     case BIT_IOR_EXPR:
938       res = double_int_ior (op1, op2);
939       break;
940
941     case BIT_XOR_EXPR:
942       res = double_int_xor (op1, op2);
943       break;
944
945     case BIT_AND_EXPR:
946       res = double_int_and (op1, op2);
947       break;
948
949     case RSHIFT_EXPR:
950       res = double_int_rshift (op1, double_int_to_shwi (op2),
951                                TYPE_PRECISION (type), !uns);
952       break;
953
954     case LSHIFT_EXPR:
955       /* It's unclear from the C standard whether shifts can overflow.
956          The following code ignores overflow; perhaps a C standard
957          interpretation ruling is needed.  */
958       res = double_int_lshift (op1, double_int_to_shwi (op2),
959                                TYPE_PRECISION (type), !uns);
960       break;
961
962     case RROTATE_EXPR:
963       res = double_int_rrotate (op1, double_int_to_shwi (op2),
964                                 TYPE_PRECISION (type));
965       break;
966
967     case LROTATE_EXPR:
968       res = double_int_lrotate (op1, double_int_to_shwi (op2),
969                                 TYPE_PRECISION (type));
970       break;
971
972     case PLUS_EXPR:
973       overflow = add_double (op1.low, op1.high, op2.low, op2.high,
974                              &res.low, &res.high);
975       break;
976
977     case MINUS_EXPR:
978       neg_double (op2.low, op2.high, &res.low, &res.high);
979       add_double (op1.low, op1.high, res.low, res.high,
980                   &res.low, &res.high);
981       overflow = OVERFLOW_SUM_SIGN (res.high, op2.high, op1.high);
982       break;
983
984     case MULT_EXPR:
985       overflow = mul_double (op1.low, op1.high, op2.low, op2.high,
986                              &res.low, &res.high);
987       break;
988
989     case TRUNC_DIV_EXPR:
990     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
991     case EXACT_DIV_EXPR:
992       /* This is a shortcut for a common special case.  */
993       if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
994           && !TREE_OVERFLOW (arg1)
995           && !TREE_OVERFLOW (arg2)
996           && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
997         {
998           if (code == CEIL_DIV_EXPR)
999             op1.low += op2.low - 1;
1000
1001           res.low = op1.low / op2.low, res.high = 0;
1002           break;
1003         }
1004
1005       /* ... fall through ...  */
1006
1007     case ROUND_DIV_EXPR:
1008       if (double_int_zero_p (op2))
1009         return NULL_TREE;
1010       if (double_int_one_p (op2))
1011         {
1012           res = op1;
1013           break;
1014         }
1015       if (double_int_equal_p (op1, op2)
1016           && ! double_int_zero_p (op1))
1017         {
1018           res = double_int_one;
1019           break;
1020         }
1021       overflow = div_and_round_double (code, uns,
1022                                        op1.low, op1.high, op2.low, op2.high,
1023                                        &res.low, &res.high,
1024                                        &tmp.low, &tmp.high);
1025       break;
1026
1027     case TRUNC_MOD_EXPR:
1028     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1029       /* This is a shortcut for a common special case.  */
1030       if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1031           && !TREE_OVERFLOW (arg1)
1032           && !TREE_OVERFLOW (arg2)
1033           && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1034         {
1035           if (code == CEIL_MOD_EXPR)
1036             op1.low += op2.low - 1;
1037           res.low = op1.low % op2.low, res.high = 0;
1038           break;
1039         }
1040
1041       /* ... fall through ...  */
1042
1043     case ROUND_MOD_EXPR:
1044       if (double_int_zero_p (op2))
1045         return NULL_TREE;
1046       overflow = div_and_round_double (code, uns,
1047                                        op1.low, op1.high, op2.low, op2.high,
1048                                        &tmp.low, &tmp.high,
1049                                        &res.low, &res.high);
1050       break;
1051
1052     case MIN_EXPR:
1053       res = double_int_min (op1, op2, uns);
1054       break;
1055
1056     case MAX_EXPR:
1057       res = double_int_max (op1, op2, uns);
1058       break;
1059
1060     default:
1061       return NULL_TREE;
1062     }
1063
1064   if (notrunc)
1065     {
1066       t = build_int_cst_wide (TREE_TYPE (arg1), res.low, res.high);
1067
1068       /* Propagate overflow flags ourselves.  */
1069       if (((!uns || is_sizetype) && overflow)
1070           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1071         {
1072           t = copy_node (t);
1073           TREE_OVERFLOW (t) = 1;
1074         }
1075     }
1076   else
1077     t = force_fit_type_double (TREE_TYPE (arg1), res, 1,
1078                                ((!uns || is_sizetype) && overflow)
1079                                | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1080
1081   return t;
1082 }
1083
1084 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1085    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1086    are the same kind of constant and the same machine mode.  Return zero if
1087    combining the constants is not allowed in the current operating mode.  */
1088
1089 static tree
1090 const_binop (enum tree_code code, tree arg1, tree arg2)
1091 {
1092   /* Sanity check for the recursive cases.  */
1093   if (!arg1 || !arg2)
1094     return NULL_TREE;
1095
1096   STRIP_NOPS (arg1);
1097   STRIP_NOPS (arg2);
1098
1099   if (TREE_CODE (arg1) == INTEGER_CST)
1100     return int_const_binop (code, arg1, arg2, 0);
1101
1102   if (TREE_CODE (arg1) == REAL_CST)
1103     {
1104       enum machine_mode mode;
1105       REAL_VALUE_TYPE d1;
1106       REAL_VALUE_TYPE d2;
1107       REAL_VALUE_TYPE value;
1108       REAL_VALUE_TYPE result;
1109       bool inexact;
1110       tree t, type;
1111
1112       /* The following codes are handled by real_arithmetic.  */
1113       switch (code)
1114         {
1115         case PLUS_EXPR:
1116         case MINUS_EXPR:
1117         case MULT_EXPR:
1118         case RDIV_EXPR:
1119         case MIN_EXPR:
1120         case MAX_EXPR:
1121           break;
1122
1123         default:
1124           return NULL_TREE;
1125         }
1126
1127       d1 = TREE_REAL_CST (arg1);
1128       d2 = TREE_REAL_CST (arg2);
1129
1130       type = TREE_TYPE (arg1);
1131       mode = TYPE_MODE (type);
1132
1133       /* Don't perform operation if we honor signaling NaNs and
1134          either operand is a NaN.  */
1135       if (HONOR_SNANS (mode)
1136           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1137         return NULL_TREE;
1138
1139       /* Don't perform operation if it would raise a division
1140          by zero exception.  */
1141       if (code == RDIV_EXPR
1142           && REAL_VALUES_EQUAL (d2, dconst0)
1143           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1144         return NULL_TREE;
1145
1146       /* If either operand is a NaN, just return it.  Otherwise, set up
1147          for floating-point trap; we return an overflow.  */
1148       if (REAL_VALUE_ISNAN (d1))
1149         return arg1;
1150       else if (REAL_VALUE_ISNAN (d2))
1151         return arg2;
1152
1153       inexact = real_arithmetic (&value, code, &d1, &d2);
1154       real_convert (&result, mode, &value);
1155
1156       /* Don't constant fold this floating point operation if
1157          the result has overflowed and flag_trapping_math.  */
1158       if (flag_trapping_math
1159           && MODE_HAS_INFINITIES (mode)
1160           && REAL_VALUE_ISINF (result)
1161           && !REAL_VALUE_ISINF (d1)
1162           && !REAL_VALUE_ISINF (d2))
1163         return NULL_TREE;
1164
1165       /* Don't constant fold this floating point operation if the
1166          result may dependent upon the run-time rounding mode and
1167          flag_rounding_math is set, or if GCC's software emulation
1168          is unable to accurately represent the result.  */
1169       if ((flag_rounding_math
1170            || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1171           && (inexact || !real_identical (&result, &value)))
1172         return NULL_TREE;
1173
1174       t = build_real (type, result);
1175
1176       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1177       return t;
1178     }
1179
1180   if (TREE_CODE (arg1) == FIXED_CST)
1181     {
1182       FIXED_VALUE_TYPE f1;
1183       FIXED_VALUE_TYPE f2;
1184       FIXED_VALUE_TYPE result;
1185       tree t, type;
1186       int sat_p;
1187       bool overflow_p;
1188
1189       /* The following codes are handled by fixed_arithmetic.  */
1190       switch (code)
1191         {
1192         case PLUS_EXPR:
1193         case MINUS_EXPR:
1194         case MULT_EXPR:
1195         case TRUNC_DIV_EXPR:
1196           f2 = TREE_FIXED_CST (arg2);
1197           break;
1198
1199         case LSHIFT_EXPR:
1200         case RSHIFT_EXPR:
1201           f2.data.high = TREE_INT_CST_HIGH (arg2);
1202           f2.data.low = TREE_INT_CST_LOW (arg2);
1203           f2.mode = SImode;
1204           break;
1205
1206         default:
1207           return NULL_TREE;
1208         }
1209
1210       f1 = TREE_FIXED_CST (arg1);
1211       type = TREE_TYPE (arg1);
1212       sat_p = TYPE_SATURATING (type);
1213       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1214       t = build_fixed (type, result);
1215       /* Propagate overflow flags.  */
1216       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1217         TREE_OVERFLOW (t) = 1;
1218       return t;
1219     }
1220
1221   if (TREE_CODE (arg1) == COMPLEX_CST)
1222     {
1223       tree type = TREE_TYPE (arg1);
1224       tree r1 = TREE_REALPART (arg1);
1225       tree i1 = TREE_IMAGPART (arg1);
1226       tree r2 = TREE_REALPART (arg2);
1227       tree i2 = TREE_IMAGPART (arg2);
1228       tree real, imag;
1229
1230       switch (code)
1231         {
1232         case PLUS_EXPR:
1233         case MINUS_EXPR:
1234           real = const_binop (code, r1, r2);
1235           imag = const_binop (code, i1, i2);
1236           break;
1237
1238         case MULT_EXPR:
1239           if (COMPLEX_FLOAT_TYPE_P (type))
1240             return do_mpc_arg2 (arg1, arg2, type,
1241                                 /* do_nonfinite= */ folding_initializer,
1242                                 mpc_mul);
1243
1244           real = const_binop (MINUS_EXPR,
1245                               const_binop (MULT_EXPR, r1, r2),
1246                               const_binop (MULT_EXPR, i1, i2));
1247           imag = const_binop (PLUS_EXPR,
1248                               const_binop (MULT_EXPR, r1, i2),
1249                               const_binop (MULT_EXPR, i1, r2));
1250           break;
1251
1252         case RDIV_EXPR:
1253           if (COMPLEX_FLOAT_TYPE_P (type))
1254             return do_mpc_arg2 (arg1, arg2, type,
1255                                 /* do_nonfinite= */ folding_initializer,
1256                                 mpc_div);
1257           /* Fallthru ... */
1258         case TRUNC_DIV_EXPR:
1259         case CEIL_DIV_EXPR:
1260         case FLOOR_DIV_EXPR:
1261         case ROUND_DIV_EXPR:
1262           if (flag_complex_method == 0)
1263           {
1264             /* Keep this algorithm in sync with
1265                tree-complex.c:expand_complex_div_straight().
1266
1267                Expand complex division to scalars, straightforward algorithm.
1268                a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1269                t = br*br + bi*bi
1270             */
1271             tree magsquared
1272               = const_binop (PLUS_EXPR,
1273                              const_binop (MULT_EXPR, r2, r2),
1274                              const_binop (MULT_EXPR, i2, i2));
1275             tree t1
1276               = const_binop (PLUS_EXPR,
1277                              const_binop (MULT_EXPR, r1, r2),
1278                              const_binop (MULT_EXPR, i1, i2));
1279             tree t2
1280               = const_binop (MINUS_EXPR,
1281                              const_binop (MULT_EXPR, i1, r2),
1282                              const_binop (MULT_EXPR, r1, i2));
1283
1284             real = const_binop (code, t1, magsquared);
1285             imag = const_binop (code, t2, magsquared);
1286           }
1287           else
1288           {
1289             /* Keep this algorithm in sync with
1290                tree-complex.c:expand_complex_div_wide().
1291
1292                Expand complex division to scalars, modified algorithm to minimize
1293                overflow with wide input ranges.  */
1294             tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1295                                         fold_abs_const (r2, TREE_TYPE (type)),
1296                                         fold_abs_const (i2, TREE_TYPE (type)));
1297
1298             if (integer_nonzerop (compare))
1299               {
1300                 /* In the TRUE branch, we compute
1301                    ratio = br/bi;
1302                    div = (br * ratio) + bi;
1303                    tr = (ar * ratio) + ai;
1304                    ti = (ai * ratio) - ar;
1305                    tr = tr / div;
1306                    ti = ti / div;  */
1307                 tree ratio = const_binop (code, r2, i2);
1308                 tree div = const_binop (PLUS_EXPR, i2,
1309                                         const_binop (MULT_EXPR, r2, ratio));
1310                 real = const_binop (MULT_EXPR, r1, ratio);
1311                 real = const_binop (PLUS_EXPR, real, i1);
1312                 real = const_binop (code, real, div);
1313
1314                 imag = const_binop (MULT_EXPR, i1, ratio);
1315                 imag = const_binop (MINUS_EXPR, imag, r1);
1316                 imag = const_binop (code, imag, div);
1317               }
1318             else
1319               {
1320                 /* In the FALSE branch, we compute
1321                    ratio = d/c;
1322                    divisor = (d * ratio) + c;
1323                    tr = (b * ratio) + a;
1324                    ti = b - (a * ratio);
1325                    tr = tr / div;
1326                    ti = ti / div;  */
1327                 tree ratio = const_binop (code, i2, r2);
1328                 tree div = const_binop (PLUS_EXPR, r2,
1329                                         const_binop (MULT_EXPR, i2, ratio));
1330
1331                 real = const_binop (MULT_EXPR, i1, ratio);
1332                 real = const_binop (PLUS_EXPR, real, r1);
1333                 real = const_binop (code, real, div);
1334
1335                 imag = const_binop (MULT_EXPR, r1, ratio);
1336                 imag = const_binop (MINUS_EXPR, i1, imag);
1337                 imag = const_binop (code, imag, div);
1338               }
1339           }
1340           break;
1341
1342         default:
1343           return NULL_TREE;
1344         }
1345
1346       if (real && imag)
1347         return build_complex (type, real, imag);
1348     }
1349
1350   if (TREE_CODE (arg1) == VECTOR_CST)
1351     {
1352       tree type = TREE_TYPE(arg1);
1353       int count = TYPE_VECTOR_SUBPARTS (type), i;
1354       tree elements1, elements2, list = NULL_TREE;
1355
1356       if(TREE_CODE(arg2) != VECTOR_CST)
1357         return NULL_TREE;
1358
1359       elements1 = TREE_VECTOR_CST_ELTS (arg1);
1360       elements2 = TREE_VECTOR_CST_ELTS (arg2);
1361
1362       for (i = 0; i < count; i++)
1363         {
1364           tree elem1, elem2, elem;
1365
1366           /* The trailing elements can be empty and should be treated as 0 */
1367           if(!elements1)
1368             elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1369           else
1370             {
1371               elem1 = TREE_VALUE(elements1);
1372               elements1 = TREE_CHAIN (elements1);
1373             }
1374
1375           if(!elements2)
1376             elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1377           else
1378             {
1379               elem2 = TREE_VALUE(elements2);
1380               elements2 = TREE_CHAIN (elements2);
1381             }
1382
1383           elem = const_binop (code, elem1, elem2);
1384
1385           /* It is possible that const_binop cannot handle the given
1386             code and return NULL_TREE */
1387           if(elem == NULL_TREE)
1388             return NULL_TREE;
1389
1390           list = tree_cons (NULL_TREE, elem, list);
1391         }
1392       return build_vector(type, nreverse(list));
1393     }
1394   return NULL_TREE;
1395 }
1396
1397 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
1398    indicates which particular sizetype to create.  */
1399
1400 tree
1401 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1402 {
1403   return build_int_cst (sizetype_tab[(int) kind], number);
1404 }
1405 \f
1406 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
1407    is a tree code.  The type of the result is taken from the operands.
1408    Both must be equivalent integer types, ala int_binop_types_match_p.
1409    If the operands are constant, so is the result.  */
1410
1411 tree
1412 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1413 {
1414   tree type = TREE_TYPE (arg0);
1415
1416   if (arg0 == error_mark_node || arg1 == error_mark_node)
1417     return error_mark_node;
1418
1419   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1420                                        TREE_TYPE (arg1)));
1421
1422   /* Handle the special case of two integer constants faster.  */
1423   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1424     {
1425       /* And some specific cases even faster than that.  */
1426       if (code == PLUS_EXPR)
1427         {
1428           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1429             return arg1;
1430           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1431             return arg0;
1432         }
1433       else if (code == MINUS_EXPR)
1434         {
1435           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1436             return arg0;
1437         }
1438       else if (code == MULT_EXPR)
1439         {
1440           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1441             return arg1;
1442         }
1443
1444       /* Handle general case of two integer constants.  */
1445       return int_const_binop (code, arg0, arg1, 0);
1446     }
1447
1448   return fold_build2_loc (loc, code, type, arg0, arg1);
1449 }
1450
1451 /* Given two values, either both of sizetype or both of bitsizetype,
1452    compute the difference between the two values.  Return the value
1453    in signed type corresponding to the type of the operands.  */
1454
1455 tree
1456 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1457 {
1458   tree type = TREE_TYPE (arg0);
1459   tree ctype;
1460
1461   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1462                                        TREE_TYPE (arg1)));
1463
1464   /* If the type is already signed, just do the simple thing.  */
1465   if (!TYPE_UNSIGNED (type))
1466     return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1467
1468   if (type == sizetype)
1469     ctype = ssizetype;
1470   else if (type == bitsizetype)
1471     ctype = sbitsizetype;
1472   else
1473     ctype = signed_type_for (type);
1474
1475   /* If either operand is not a constant, do the conversions to the signed
1476      type and subtract.  The hardware will do the right thing with any
1477      overflow in the subtraction.  */
1478   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1479     return size_binop_loc (loc, MINUS_EXPR,
1480                            fold_convert_loc (loc, ctype, arg0),
1481                            fold_convert_loc (loc, ctype, arg1));
1482
1483   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1484      Otherwise, subtract the other way, convert to CTYPE (we know that can't
1485      overflow) and negate (which can't either).  Special-case a result
1486      of zero while we're here.  */
1487   if (tree_int_cst_equal (arg0, arg1))
1488     return build_int_cst (ctype, 0);
1489   else if (tree_int_cst_lt (arg1, arg0))
1490     return fold_convert_loc (loc, ctype,
1491                              size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1492   else
1493     return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1494                            fold_convert_loc (loc, ctype,
1495                                              size_binop_loc (loc,
1496                                                              MINUS_EXPR,
1497                                                              arg1, arg0)));
1498 }
1499 \f
1500 /* A subroutine of fold_convert_const handling conversions of an
1501    INTEGER_CST to another integer type.  */
1502
1503 static tree
1504 fold_convert_const_int_from_int (tree type, const_tree arg1)
1505 {
1506   tree t;
1507
1508   /* Given an integer constant, make new constant with new type,
1509      appropriately sign-extended or truncated.  */
1510   t = force_fit_type_double (type, tree_to_double_int (arg1),
1511                              !POINTER_TYPE_P (TREE_TYPE (arg1)),
1512                              (TREE_INT_CST_HIGH (arg1) < 0
1513                               && (TYPE_UNSIGNED (type)
1514                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1515                              | TREE_OVERFLOW (arg1));
1516
1517   return t;
1518 }
1519
1520 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1521    to an integer type.  */
1522
1523 static tree
1524 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1525 {
1526   int overflow = 0;
1527   tree t;
1528
1529   /* The following code implements the floating point to integer
1530      conversion rules required by the Java Language Specification,
1531      that IEEE NaNs are mapped to zero and values that overflow
1532      the target precision saturate, i.e. values greater than
1533      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1534      are mapped to INT_MIN.  These semantics are allowed by the
1535      C and C++ standards that simply state that the behavior of
1536      FP-to-integer conversion is unspecified upon overflow.  */
1537
1538   double_int val;
1539   REAL_VALUE_TYPE r;
1540   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1541
1542   switch (code)
1543     {
1544     case FIX_TRUNC_EXPR:
1545       real_trunc (&r, VOIDmode, &x);
1546       break;
1547
1548     default:
1549       gcc_unreachable ();
1550     }
1551
1552   /* If R is NaN, return zero and show we have an overflow.  */
1553   if (REAL_VALUE_ISNAN (r))
1554     {
1555       overflow = 1;
1556       val = double_int_zero;
1557     }
1558
1559   /* See if R is less than the lower bound or greater than the
1560      upper bound.  */
1561
1562   if (! overflow)
1563     {
1564       tree lt = TYPE_MIN_VALUE (type);
1565       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1566       if (REAL_VALUES_LESS (r, l))
1567         {
1568           overflow = 1;
1569           val = tree_to_double_int (lt);
1570         }
1571     }
1572
1573   if (! overflow)
1574     {
1575       tree ut = TYPE_MAX_VALUE (type);
1576       if (ut)
1577         {
1578           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1579           if (REAL_VALUES_LESS (u, r))
1580             {
1581               overflow = 1;
1582               val = tree_to_double_int (ut);
1583             }
1584         }
1585     }
1586
1587   if (! overflow)
1588     real_to_integer2 ((HOST_WIDE_INT *) &val.low, &val.high, &r);
1589
1590   t = force_fit_type_double (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1591   return t;
1592 }
1593
1594 /* A subroutine of fold_convert_const handling conversions of a
1595    FIXED_CST to an integer type.  */
1596
1597 static tree
1598 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1599 {
1600   tree t;
1601   double_int temp, temp_trunc;
1602   unsigned int mode;
1603
1604   /* Right shift FIXED_CST to temp by fbit.  */
1605   temp = TREE_FIXED_CST (arg1).data;
1606   mode = TREE_FIXED_CST (arg1).mode;
1607   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
1608     {
1609       temp = double_int_rshift (temp, GET_MODE_FBIT (mode),
1610                                 HOST_BITS_PER_DOUBLE_INT,
1611                                 SIGNED_FIXED_POINT_MODE_P (mode));
1612
1613       /* Left shift temp to temp_trunc by fbit.  */
1614       temp_trunc = double_int_lshift (temp, GET_MODE_FBIT (mode),
1615                                       HOST_BITS_PER_DOUBLE_INT,
1616                                       SIGNED_FIXED_POINT_MODE_P (mode));
1617     }
1618   else
1619     {
1620       temp = double_int_zero;
1621       temp_trunc = double_int_zero;
1622     }
1623
1624   /* If FIXED_CST is negative, we need to round the value toward 0.
1625      By checking if the fractional bits are not zero to add 1 to temp.  */
1626   if (SIGNED_FIXED_POINT_MODE_P (mode)
1627       && double_int_negative_p (temp_trunc)
1628       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
1629     temp = double_int_add (temp, double_int_one);
1630
1631   /* Given a fixed-point constant, make new constant with new type,
1632      appropriately sign-extended or truncated.  */
1633   t = force_fit_type_double (type, temp, -1,
1634                              (double_int_negative_p (temp)
1635                               && (TYPE_UNSIGNED (type)
1636                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1637                              | TREE_OVERFLOW (arg1));
1638
1639   return t;
1640 }
1641
1642 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1643    to another floating point type.  */
1644
1645 static tree
1646 fold_convert_const_real_from_real (tree type, const_tree arg1)
1647 {
1648   REAL_VALUE_TYPE value;
1649   tree t;
1650
1651   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1652   t = build_real (type, value);
1653
1654   /* If converting an infinity or NAN to a representation that doesn't
1655      have one, set the overflow bit so that we can produce some kind of
1656      error message at the appropriate point if necessary.  It's not the
1657      most user-friendly message, but it's better than nothing.  */
1658   if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1659       && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1660     TREE_OVERFLOW (t) = 1;
1661   else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1662            && !MODE_HAS_NANS (TYPE_MODE (type)))
1663     TREE_OVERFLOW (t) = 1;
1664   /* Regular overflow, conversion produced an infinity in a mode that
1665      can't represent them.  */
1666   else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1667            && REAL_VALUE_ISINF (value)
1668            && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1669     TREE_OVERFLOW (t) = 1;
1670   else
1671     TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1672   return t;
1673 }
1674
1675 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1676    to a floating point type.  */
1677
1678 static tree
1679 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
1680 {
1681   REAL_VALUE_TYPE value;
1682   tree t;
1683
1684   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
1685   t = build_real (type, value);
1686
1687   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1688   return t;
1689 }
1690
1691 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1692    to another fixed-point type.  */
1693
1694 static tree
1695 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
1696 {
1697   FIXED_VALUE_TYPE value;
1698   tree t;
1699   bool overflow_p;
1700
1701   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
1702                               TYPE_SATURATING (type));
1703   t = build_fixed (type, value);
1704
1705   /* Propagate overflow flags.  */
1706   if (overflow_p | TREE_OVERFLOW (arg1))
1707     TREE_OVERFLOW (t) = 1;
1708   return t;
1709 }
1710
1711 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
1712    to a fixed-point type.  */
1713
1714 static tree
1715 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
1716 {
1717   FIXED_VALUE_TYPE value;
1718   tree t;
1719   bool overflow_p;
1720
1721   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
1722                                        TREE_INT_CST (arg1),
1723                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
1724                                        TYPE_SATURATING (type));
1725   t = build_fixed (type, value);
1726
1727   /* Propagate overflow flags.  */
1728   if (overflow_p | TREE_OVERFLOW (arg1))
1729     TREE_OVERFLOW (t) = 1;
1730   return t;
1731 }
1732
1733 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1734    to a fixed-point type.  */
1735
1736 static tree
1737 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
1738 {
1739   FIXED_VALUE_TYPE value;
1740   tree t;
1741   bool overflow_p;
1742
1743   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
1744                                         &TREE_REAL_CST (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 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1755    type TYPE.  If no simplification can be done return NULL_TREE.  */
1756
1757 static tree
1758 fold_convert_const (enum tree_code code, tree type, tree arg1)
1759 {
1760   if (TREE_TYPE (arg1) == type)
1761     return arg1;
1762
1763   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
1764       || TREE_CODE (type) == OFFSET_TYPE)
1765     {
1766       if (TREE_CODE (arg1) == INTEGER_CST)
1767         return fold_convert_const_int_from_int (type, arg1);
1768       else if (TREE_CODE (arg1) == REAL_CST)
1769         return fold_convert_const_int_from_real (code, type, arg1);
1770       else if (TREE_CODE (arg1) == FIXED_CST)
1771         return fold_convert_const_int_from_fixed (type, arg1);
1772     }
1773   else if (TREE_CODE (type) == REAL_TYPE)
1774     {
1775       if (TREE_CODE (arg1) == INTEGER_CST)
1776         return build_real_from_int_cst (type, arg1);
1777       else if (TREE_CODE (arg1) == REAL_CST)
1778         return fold_convert_const_real_from_real (type, arg1);
1779       else if (TREE_CODE (arg1) == FIXED_CST)
1780         return fold_convert_const_real_from_fixed (type, arg1);
1781     }
1782   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
1783     {
1784       if (TREE_CODE (arg1) == FIXED_CST)
1785         return fold_convert_const_fixed_from_fixed (type, arg1);
1786       else if (TREE_CODE (arg1) == INTEGER_CST)
1787         return fold_convert_const_fixed_from_int (type, arg1);
1788       else if (TREE_CODE (arg1) == REAL_CST)
1789         return fold_convert_const_fixed_from_real (type, arg1);
1790     }
1791   return NULL_TREE;
1792 }
1793
1794 /* Construct a vector of zero elements of vector type TYPE.  */
1795
1796 static tree
1797 build_zero_vector (tree type)
1798 {
1799   tree t;
1800
1801   t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1802   return build_vector_from_val (type, t);
1803 }
1804
1805 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
1806
1807 bool
1808 fold_convertible_p (const_tree type, const_tree arg)
1809 {
1810   tree orig = TREE_TYPE (arg);
1811
1812   if (type == orig)
1813     return true;
1814
1815   if (TREE_CODE (arg) == ERROR_MARK
1816       || TREE_CODE (type) == ERROR_MARK
1817       || TREE_CODE (orig) == ERROR_MARK)
1818     return false;
1819
1820   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1821     return true;
1822
1823   switch (TREE_CODE (type))
1824     {
1825     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1826     case POINTER_TYPE: case REFERENCE_TYPE:
1827     case OFFSET_TYPE:
1828       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1829           || TREE_CODE (orig) == OFFSET_TYPE)
1830         return true;
1831       return (TREE_CODE (orig) == VECTOR_TYPE
1832               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1833
1834     case REAL_TYPE:
1835     case FIXED_POINT_TYPE:
1836     case COMPLEX_TYPE:
1837     case VECTOR_TYPE:
1838     case VOID_TYPE:
1839       return TREE_CODE (type) == TREE_CODE (orig);
1840
1841     default:
1842       return false;
1843     }
1844 }
1845
1846 /* Convert expression ARG to type TYPE.  Used by the middle-end for
1847    simple conversions in preference to calling the front-end's convert.  */
1848
1849 tree
1850 fold_convert_loc (location_t loc, tree type, tree arg)
1851 {
1852   tree orig = TREE_TYPE (arg);
1853   tree tem;
1854
1855   if (type == orig)
1856     return arg;
1857
1858   if (TREE_CODE (arg) == ERROR_MARK
1859       || TREE_CODE (type) == ERROR_MARK
1860       || TREE_CODE (orig) == ERROR_MARK)
1861     return error_mark_node;
1862
1863   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1864     return fold_build1_loc (loc, NOP_EXPR, type, arg);
1865
1866   switch (TREE_CODE (type))
1867     {
1868     case POINTER_TYPE:
1869     case REFERENCE_TYPE:
1870       /* Handle conversions between pointers to different address spaces.  */
1871       if (POINTER_TYPE_P (orig)
1872           && (TYPE_ADDR_SPACE (TREE_TYPE (type))
1873               != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
1874         return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
1875       /* fall through */
1876
1877     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1878     case OFFSET_TYPE:
1879       if (TREE_CODE (arg) == INTEGER_CST)
1880         {
1881           tem = fold_convert_const (NOP_EXPR, type, arg);
1882           if (tem != NULL_TREE)
1883             return tem;
1884         }
1885       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1886           || TREE_CODE (orig) == OFFSET_TYPE)
1887         return fold_build1_loc (loc, NOP_EXPR, type, arg);
1888       if (TREE_CODE (orig) == COMPLEX_TYPE)
1889         return fold_convert_loc (loc, type,
1890                              fold_build1_loc (loc, REALPART_EXPR,
1891                                           TREE_TYPE (orig), arg));
1892       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
1893                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1894       return fold_build1_loc (loc, NOP_EXPR, type, arg);
1895
1896     case REAL_TYPE:
1897       if (TREE_CODE (arg) == INTEGER_CST)
1898         {
1899           tem = fold_convert_const (FLOAT_EXPR, type, arg);
1900           if (tem != NULL_TREE)
1901             return tem;
1902         }
1903       else if (TREE_CODE (arg) == REAL_CST)
1904         {
1905           tem = fold_convert_const (NOP_EXPR, type, arg);
1906           if (tem != NULL_TREE)
1907             return tem;
1908         }
1909       else if (TREE_CODE (arg) == FIXED_CST)
1910         {
1911           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1912           if (tem != NULL_TREE)
1913             return tem;
1914         }
1915
1916       switch (TREE_CODE (orig))
1917         {
1918         case INTEGER_TYPE:
1919         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1920         case POINTER_TYPE: case REFERENCE_TYPE:
1921           return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
1922
1923         case REAL_TYPE:
1924           return fold_build1_loc (loc, NOP_EXPR, type, arg);
1925
1926         case FIXED_POINT_TYPE:
1927           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1928
1929         case COMPLEX_TYPE:
1930           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1931           return fold_convert_loc (loc, type, tem);
1932
1933         default:
1934           gcc_unreachable ();
1935         }
1936
1937     case FIXED_POINT_TYPE:
1938       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
1939           || TREE_CODE (arg) == REAL_CST)
1940         {
1941           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1942           if (tem != NULL_TREE)
1943             goto fold_convert_exit;
1944         }
1945
1946       switch (TREE_CODE (orig))
1947         {
1948         case FIXED_POINT_TYPE:
1949         case INTEGER_TYPE:
1950         case ENUMERAL_TYPE:
1951         case BOOLEAN_TYPE:
1952         case REAL_TYPE:
1953           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1954
1955         case COMPLEX_TYPE:
1956           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1957           return fold_convert_loc (loc, type, tem);
1958
1959         default:
1960           gcc_unreachable ();
1961         }
1962
1963     case COMPLEX_TYPE:
1964       switch (TREE_CODE (orig))
1965         {
1966         case INTEGER_TYPE:
1967         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1968         case POINTER_TYPE: case REFERENCE_TYPE:
1969         case REAL_TYPE:
1970         case FIXED_POINT_TYPE:
1971           return fold_build2_loc (loc, COMPLEX_EXPR, type,
1972                               fold_convert_loc (loc, TREE_TYPE (type), arg),
1973                               fold_convert_loc (loc, TREE_TYPE (type),
1974                                             integer_zero_node));
1975         case COMPLEX_TYPE:
1976           {
1977             tree rpart, ipart;
1978
1979             if (TREE_CODE (arg) == COMPLEX_EXPR)
1980               {
1981                 rpart = fold_convert_loc (loc, TREE_TYPE (type),
1982                                       TREE_OPERAND (arg, 0));
1983                 ipart = fold_convert_loc (loc, TREE_TYPE (type),
1984                                       TREE_OPERAND (arg, 1));
1985                 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
1986               }
1987
1988             arg = save_expr (arg);
1989             rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1990             ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
1991             rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
1992             ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
1993             return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
1994           }
1995
1996         default:
1997           gcc_unreachable ();
1998         }
1999
2000     case VECTOR_TYPE:
2001       if (integer_zerop (arg))
2002         return build_zero_vector (type);
2003       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2004       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2005                   || TREE_CODE (orig) == VECTOR_TYPE);
2006       return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2007
2008     case VOID_TYPE:
2009       tem = fold_ignored_result (arg);
2010       if (TREE_CODE (tem) == MODIFY_EXPR)
2011         goto fold_convert_exit;
2012       return fold_build1_loc (loc, NOP_EXPR, type, tem);
2013
2014     default:
2015       gcc_unreachable ();
2016     }
2017  fold_convert_exit:
2018   protected_set_expr_location (tem, loc);
2019   return tem;
2020 }
2021 \f
2022 /* Return false if expr can be assumed not to be an lvalue, true
2023    otherwise.  */
2024
2025 static bool
2026 maybe_lvalue_p (const_tree x)
2027 {
2028   /* We only need to wrap lvalue tree codes.  */
2029   switch (TREE_CODE (x))
2030   {
2031   case VAR_DECL:
2032   case PARM_DECL:
2033   case RESULT_DECL:
2034   case LABEL_DECL:
2035   case FUNCTION_DECL:
2036   case SSA_NAME:
2037
2038   case COMPONENT_REF:
2039   case MEM_REF:
2040   case INDIRECT_REF:
2041   case ARRAY_REF:
2042   case ARRAY_RANGE_REF:
2043   case BIT_FIELD_REF:
2044   case OBJ_TYPE_REF:
2045
2046   case REALPART_EXPR:
2047   case IMAGPART_EXPR:
2048   case PREINCREMENT_EXPR:
2049   case PREDECREMENT_EXPR:
2050   case SAVE_EXPR:
2051   case TRY_CATCH_EXPR:
2052   case WITH_CLEANUP_EXPR:
2053   case COMPOUND_EXPR:
2054   case MODIFY_EXPR:
2055   case TARGET_EXPR:
2056   case COND_EXPR:
2057   case BIND_EXPR:
2058     break;
2059
2060   default:
2061     /* Assume the worst for front-end tree codes.  */
2062     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2063       break;
2064     return false;
2065   }
2066
2067   return true;
2068 }
2069
2070 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2071
2072 tree
2073 non_lvalue_loc (location_t loc, tree x)
2074 {
2075   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2076      us.  */
2077   if (in_gimple_form)
2078     return x;
2079
2080   if (! maybe_lvalue_p (x))
2081     return x;
2082   x = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2083   SET_EXPR_LOCATION (x, loc);
2084   return x;
2085 }
2086
2087 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2088    Zero means allow extended lvalues.  */
2089
2090 int pedantic_lvalues;
2091
2092 /* When pedantic, return an expr equal to X but certainly not valid as a
2093    pedantic lvalue.  Otherwise, return X.  */
2094
2095 static tree
2096 pedantic_non_lvalue_loc (location_t loc, tree x)
2097 {
2098   if (pedantic_lvalues)
2099     return non_lvalue_loc (loc, x);
2100   protected_set_expr_location (x, loc);
2101   return x;
2102 }
2103 \f
2104 /* Given a tree comparison code, return the code that is the logical inverse
2105    of the given code.  It is not safe to do this for floating-point
2106    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2107    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2108
2109 enum tree_code
2110 invert_tree_comparison (enum tree_code code, bool honor_nans)
2111 {
2112   if (honor_nans && flag_trapping_math)
2113     return ERROR_MARK;
2114
2115   switch (code)
2116     {
2117     case EQ_EXPR:
2118       return NE_EXPR;
2119     case NE_EXPR:
2120       return EQ_EXPR;
2121     case GT_EXPR:
2122       return honor_nans ? UNLE_EXPR : LE_EXPR;
2123     case GE_EXPR:
2124       return honor_nans ? UNLT_EXPR : LT_EXPR;
2125     case LT_EXPR:
2126       return honor_nans ? UNGE_EXPR : GE_EXPR;
2127     case LE_EXPR:
2128       return honor_nans ? UNGT_EXPR : GT_EXPR;
2129     case LTGT_EXPR:
2130       return UNEQ_EXPR;
2131     case UNEQ_EXPR:
2132       return LTGT_EXPR;
2133     case UNGT_EXPR:
2134       return LE_EXPR;
2135     case UNGE_EXPR:
2136       return LT_EXPR;
2137     case UNLT_EXPR:
2138       return GE_EXPR;
2139     case UNLE_EXPR:
2140       return GT_EXPR;
2141     case ORDERED_EXPR:
2142       return UNORDERED_EXPR;
2143     case UNORDERED_EXPR:
2144       return ORDERED_EXPR;
2145     default:
2146       gcc_unreachable ();
2147     }
2148 }
2149
2150 /* Similar, but return the comparison that results if the operands are
2151    swapped.  This is safe for floating-point.  */
2152
2153 enum tree_code
2154 swap_tree_comparison (enum tree_code code)
2155 {
2156   switch (code)
2157     {
2158     case EQ_EXPR:
2159     case NE_EXPR:
2160     case ORDERED_EXPR:
2161     case UNORDERED_EXPR:
2162     case LTGT_EXPR:
2163     case UNEQ_EXPR:
2164       return code;
2165     case GT_EXPR:
2166       return LT_EXPR;
2167     case GE_EXPR:
2168       return LE_EXPR;
2169     case LT_EXPR:
2170       return GT_EXPR;
2171     case LE_EXPR:
2172       return GE_EXPR;
2173     case UNGT_EXPR:
2174       return UNLT_EXPR;
2175     case UNGE_EXPR:
2176       return UNLE_EXPR;
2177     case UNLT_EXPR:
2178       return UNGT_EXPR;
2179     case UNLE_EXPR:
2180       return UNGE_EXPR;
2181     default:
2182       gcc_unreachable ();
2183     }
2184 }
2185
2186
2187 /* Convert a comparison tree code from an enum tree_code representation
2188    into a compcode bit-based encoding.  This function is the inverse of
2189    compcode_to_comparison.  */
2190
2191 static enum comparison_code
2192 comparison_to_compcode (enum tree_code code)
2193 {
2194   switch (code)
2195     {
2196     case LT_EXPR:
2197       return COMPCODE_LT;
2198     case EQ_EXPR:
2199       return COMPCODE_EQ;
2200     case LE_EXPR:
2201       return COMPCODE_LE;
2202     case GT_EXPR:
2203       return COMPCODE_GT;
2204     case NE_EXPR:
2205       return COMPCODE_NE;
2206     case GE_EXPR:
2207       return COMPCODE_GE;
2208     case ORDERED_EXPR:
2209       return COMPCODE_ORD;
2210     case UNORDERED_EXPR:
2211       return COMPCODE_UNORD;
2212     case UNLT_EXPR:
2213       return COMPCODE_UNLT;
2214     case UNEQ_EXPR:
2215       return COMPCODE_UNEQ;
2216     case UNLE_EXPR:
2217       return COMPCODE_UNLE;
2218     case UNGT_EXPR:
2219       return COMPCODE_UNGT;
2220     case LTGT_EXPR:
2221       return COMPCODE_LTGT;
2222     case UNGE_EXPR:
2223       return COMPCODE_UNGE;
2224     default:
2225       gcc_unreachable ();
2226     }
2227 }
2228
2229 /* Convert a compcode bit-based encoding of a comparison operator back
2230    to GCC's enum tree_code representation.  This function is the
2231    inverse of comparison_to_compcode.  */
2232
2233 static enum tree_code
2234 compcode_to_comparison (enum comparison_code code)
2235 {
2236   switch (code)
2237     {
2238     case COMPCODE_LT:
2239       return LT_EXPR;
2240     case COMPCODE_EQ:
2241       return EQ_EXPR;
2242     case COMPCODE_LE:
2243       return LE_EXPR;
2244     case COMPCODE_GT:
2245       return GT_EXPR;
2246     case COMPCODE_NE:
2247       return NE_EXPR;
2248     case COMPCODE_GE:
2249       return GE_EXPR;
2250     case COMPCODE_ORD:
2251       return ORDERED_EXPR;
2252     case COMPCODE_UNORD:
2253       return UNORDERED_EXPR;
2254     case COMPCODE_UNLT:
2255       return UNLT_EXPR;
2256     case COMPCODE_UNEQ:
2257       return UNEQ_EXPR;
2258     case COMPCODE_UNLE:
2259       return UNLE_EXPR;
2260     case COMPCODE_UNGT:
2261       return UNGT_EXPR;
2262     case COMPCODE_LTGT:
2263       return LTGT_EXPR;
2264     case COMPCODE_UNGE:
2265       return UNGE_EXPR;
2266     default:
2267       gcc_unreachable ();
2268     }
2269 }
2270
2271 /* Return a tree for the comparison which is the combination of
2272    doing the AND or OR (depending on CODE) of the two operations LCODE
2273    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2274    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2275    if this makes the transformation invalid.  */
2276
2277 tree
2278 combine_comparisons (location_t loc,
2279                      enum tree_code code, enum tree_code lcode,
2280                      enum tree_code rcode, tree truth_type,
2281                      tree ll_arg, tree lr_arg)
2282 {
2283   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2284   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2285   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2286   int compcode;
2287
2288   switch (code)
2289     {
2290     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2291       compcode = lcompcode & rcompcode;
2292       break;
2293
2294     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2295       compcode = lcompcode | rcompcode;
2296       break;
2297
2298     default:
2299       return NULL_TREE;
2300     }
2301
2302   if (!honor_nans)
2303     {
2304       /* Eliminate unordered comparisons, as well as LTGT and ORD
2305          which are not used unless the mode has NaNs.  */
2306       compcode &= ~COMPCODE_UNORD;
2307       if (compcode == COMPCODE_LTGT)
2308         compcode = COMPCODE_NE;
2309       else if (compcode == COMPCODE_ORD)
2310         compcode = COMPCODE_TRUE;
2311     }
2312    else if (flag_trapping_math)
2313      {
2314         /* Check that the original operation and the optimized ones will trap
2315            under the same condition.  */
2316         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2317                      && (lcompcode != COMPCODE_EQ)
2318                      && (lcompcode != COMPCODE_ORD);
2319         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2320                      && (rcompcode != COMPCODE_EQ)
2321                      && (rcompcode != COMPCODE_ORD);
2322         bool trap = (compcode & COMPCODE_UNORD) == 0
2323                     && (compcode != COMPCODE_EQ)
2324                     && (compcode != COMPCODE_ORD);
2325
2326         /* In a short-circuited boolean expression the LHS might be
2327            such that the RHS, if evaluated, will never trap.  For
2328            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2329            if neither x nor y is NaN.  (This is a mixed blessing: for
2330            example, the expression above will never trap, hence
2331            optimizing it to x < y would be invalid).  */
2332         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2333             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2334           rtrap = false;
2335
2336         /* If the comparison was short-circuited, and only the RHS
2337            trapped, we may now generate a spurious trap.  */
2338         if (rtrap && !ltrap
2339             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2340           return NULL_TREE;
2341
2342         /* If we changed the conditions that cause a trap, we lose.  */
2343         if ((ltrap || rtrap) != trap)
2344           return NULL_TREE;
2345       }
2346
2347   if (compcode == COMPCODE_TRUE)
2348     return constant_boolean_node (true, truth_type);
2349   else if (compcode == COMPCODE_FALSE)
2350     return constant_boolean_node (false, truth_type);
2351   else
2352     {
2353       enum tree_code tcode;
2354
2355       tcode = compcode_to_comparison ((enum comparison_code) compcode);
2356       return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2357     }
2358 }
2359 \f
2360 /* Return nonzero if two operands (typically of the same tree node)
2361    are necessarily equal.  If either argument has side-effects this
2362    function returns zero.  FLAGS modifies behavior as follows:
2363
2364    If OEP_ONLY_CONST is set, only return nonzero for constants.
2365    This function tests whether the operands are indistinguishable;
2366    it does not test whether they are equal using C's == operation.
2367    The distinction is important for IEEE floating point, because
2368    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2369    (2) two NaNs may be indistinguishable, but NaN!=NaN.
2370
2371    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2372    even though it may hold multiple values during a function.
2373    This is because a GCC tree node guarantees that nothing else is
2374    executed between the evaluation of its "operands" (which may often
2375    be evaluated in arbitrary order).  Hence if the operands themselves
2376    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2377    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
2378    unset means assuming isochronic (or instantaneous) tree equivalence.
2379    Unless comparing arbitrary expression trees, such as from different
2380    statements, this flag can usually be left unset.
2381
2382    If OEP_PURE_SAME is set, then pure functions with identical arguments
2383    are considered the same.  It is used when the caller has other ways
2384    to ensure that global memory is unchanged in between.  */
2385
2386 int
2387 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2388 {
2389   /* If either is ERROR_MARK, they aren't equal.  */
2390   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2391       || TREE_TYPE (arg0) == error_mark_node
2392       || TREE_TYPE (arg1) == error_mark_node)
2393     return 0;
2394
2395   /* Similar, if either does not have a type (like a released SSA name), 
2396      they aren't equal.  */
2397   if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2398     return 0;
2399
2400   /* Check equality of integer constants before bailing out due to
2401      precision differences.  */
2402   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2403     return tree_int_cst_equal (arg0, arg1);
2404
2405   /* If both types don't have the same signedness, then we can't consider
2406      them equal.  We must check this before the STRIP_NOPS calls
2407      because they may change the signedness of the arguments.  As pointers
2408      strictly don't have a signedness, require either two pointers or
2409      two non-pointers as well.  */
2410   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2411       || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2412     return 0;
2413
2414   /* We cannot consider pointers to different address space equal.  */
2415   if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2416       && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2417           != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2418     return 0;
2419
2420   /* If both types don't have the same precision, then it is not safe
2421      to strip NOPs.  */
2422   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
2423     return 0;
2424
2425   STRIP_NOPS (arg0);
2426   STRIP_NOPS (arg1);
2427
2428   /* In case both args are comparisons but with different comparison
2429      code, try to swap the comparison operands of one arg to produce
2430      a match and compare that variant.  */
2431   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2432       && COMPARISON_CLASS_P (arg0)
2433       && COMPARISON_CLASS_P (arg1))
2434     {
2435       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2436
2437       if (TREE_CODE (arg0) == swap_code)
2438         return operand_equal_p (TREE_OPERAND (arg0, 0),
2439                                 TREE_OPERAND (arg1, 1), flags)
2440                && operand_equal_p (TREE_OPERAND (arg0, 1),
2441                                    TREE_OPERAND (arg1, 0), flags);
2442     }
2443
2444   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2445       /* This is needed for conversions and for COMPONENT_REF.
2446          Might as well play it safe and always test this.  */
2447       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2448       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2449       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2450     return 0;
2451
2452   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2453      We don't care about side effects in that case because the SAVE_EXPR
2454      takes care of that for us. In all other cases, two expressions are
2455      equal if they have no side effects.  If we have two identical
2456      expressions with side effects that should be treated the same due
2457      to the only side effects being identical SAVE_EXPR's, that will
2458      be detected in the recursive calls below.  */
2459   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2460       && (TREE_CODE (arg0) == SAVE_EXPR
2461           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2462     return 1;
2463
2464   /* Next handle constant cases, those for which we can return 1 even
2465      if ONLY_CONST is set.  */
2466   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2467     switch (TREE_CODE (arg0))
2468       {
2469       case INTEGER_CST:
2470         return tree_int_cst_equal (arg0, arg1);
2471
2472       case FIXED_CST:
2473         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2474                                        TREE_FIXED_CST (arg1));
2475
2476       case REAL_CST:
2477         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2478                                    TREE_REAL_CST (arg1)))
2479           return 1;
2480
2481
2482         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
2483           {
2484             /* If we do not distinguish between signed and unsigned zero,
2485                consider them equal.  */
2486             if (real_zerop (arg0) && real_zerop (arg1))
2487               return 1;
2488           }
2489         return 0;
2490
2491       case VECTOR_CST:
2492         {
2493           tree v1, v2;
2494
2495           v1 = TREE_VECTOR_CST_ELTS (arg0);
2496           v2 = TREE_VECTOR_CST_ELTS (arg1);
2497           while (v1 && v2)
2498             {
2499               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2500                                     flags))
2501                 return 0;
2502               v1 = TREE_CHAIN (v1);
2503               v2 = TREE_CHAIN (v2);
2504             }
2505
2506           return v1 == v2;
2507         }
2508
2509       case COMPLEX_CST:
2510         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2511                                  flags)
2512                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2513                                     flags));
2514
2515       case STRING_CST:
2516         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2517                 && ! memcmp (TREE_STRING_POINTER (arg0),
2518                               TREE_STRING_POINTER (arg1),
2519                               TREE_STRING_LENGTH (arg0)));
2520
2521       case ADDR_EXPR:
2522         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2523                                 0);
2524       default:
2525         break;
2526       }
2527
2528   if (flags & OEP_ONLY_CONST)
2529     return 0;
2530
2531 /* Define macros to test an operand from arg0 and arg1 for equality and a
2532    variant that allows null and views null as being different from any
2533    non-null value.  In the latter case, if either is null, the both
2534    must be; otherwise, do the normal comparison.  */
2535 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
2536                                     TREE_OPERAND (arg1, N), flags)
2537
2538 #define OP_SAME_WITH_NULL(N)                            \
2539   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2540    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2541
2542   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2543     {
2544     case tcc_unary:
2545       /* Two conversions are equal only if signedness and modes match.  */
2546       switch (TREE_CODE (arg0))
2547         {
2548         CASE_CONVERT:
2549         case FIX_TRUNC_EXPR:
2550           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2551               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2552             return 0;
2553           break;
2554         default:
2555           break;
2556         }
2557
2558       return OP_SAME (0);
2559
2560
2561     case tcc_comparison:
2562     case tcc_binary:
2563       if (OP_SAME (0) && OP_SAME (1))
2564         return 1;
2565
2566       /* For commutative ops, allow the other order.  */
2567       return (commutative_tree_code (TREE_CODE (arg0))
2568               && operand_equal_p (TREE_OPERAND (arg0, 0),
2569                                   TREE_OPERAND (arg1, 1), flags)
2570               && operand_equal_p (TREE_OPERAND (arg0, 1),
2571                                   TREE_OPERAND (arg1, 0), flags));
2572
2573     case tcc_reference:
2574       /* If either of the pointer (or reference) expressions we are
2575          dereferencing contain a side effect, these cannot be equal.  */
2576       if (TREE_SIDE_EFFECTS (arg0)
2577           || TREE_SIDE_EFFECTS (arg1))
2578         return 0;
2579
2580       switch (TREE_CODE (arg0))
2581         {
2582         case INDIRECT_REF:
2583         case REALPART_EXPR:
2584         case IMAGPART_EXPR:
2585           return OP_SAME (0);
2586
2587         case MEM_REF:
2588           /* Require equal access sizes, and similar pointer types.
2589              We can have incomplete types for array references of
2590              variable-sized arrays from the Fortran frontent
2591              though.  */
2592           return ((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1))
2593                    || (TYPE_SIZE (TREE_TYPE (arg0))
2594                        && TYPE_SIZE (TREE_TYPE (arg1))
2595                        && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
2596                                            TYPE_SIZE (TREE_TYPE (arg1)), flags)))
2597                   && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg0, 1)))
2598                       == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg1, 1))))
2599                   && OP_SAME (0) && OP_SAME (1));
2600
2601         case ARRAY_REF:
2602         case ARRAY_RANGE_REF:
2603           /* Operands 2 and 3 may be null.
2604              Compare the array index by value if it is constant first as we
2605              may have different types but same value here.  */
2606           return (OP_SAME (0)
2607                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2608                                           TREE_OPERAND (arg1, 1))
2609                       || OP_SAME (1))
2610                   && OP_SAME_WITH_NULL (2)
2611                   && OP_SAME_WITH_NULL (3));
2612
2613         case COMPONENT_REF:
2614           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
2615              may be NULL when we're called to compare MEM_EXPRs.  */
2616           return OP_SAME_WITH_NULL (0)
2617                  && OP_SAME (1)
2618                  && OP_SAME_WITH_NULL (2);
2619
2620         case BIT_FIELD_REF:
2621           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2622
2623         default:
2624           return 0;
2625         }
2626
2627     case tcc_expression:
2628       switch (TREE_CODE (arg0))
2629         {
2630         case ADDR_EXPR:
2631         case TRUTH_NOT_EXPR:
2632           return OP_SAME (0);
2633
2634         case TRUTH_ANDIF_EXPR:
2635         case TRUTH_ORIF_EXPR:
2636           return OP_SAME (0) && OP_SAME (1);
2637
2638         case TRUTH_AND_EXPR:
2639         case TRUTH_OR_EXPR:
2640         case TRUTH_XOR_EXPR:
2641           if (OP_SAME (0) && OP_SAME (1))
2642             return 1;
2643
2644           /* Otherwise take into account this is a commutative operation.  */
2645           return (operand_equal_p (TREE_OPERAND (arg0, 0),
2646                                    TREE_OPERAND (arg1, 1), flags)
2647                   && operand_equal_p (TREE_OPERAND (arg0, 1),
2648                                       TREE_OPERAND (arg1, 0), flags));
2649
2650         case COND_EXPR:
2651           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2652
2653         default:
2654           return 0;
2655         }
2656
2657     case tcc_vl_exp:
2658       switch (TREE_CODE (arg0))
2659         {
2660         case CALL_EXPR:
2661           /* If the CALL_EXPRs call different functions, then they
2662              clearly can not be equal.  */
2663           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
2664                                  flags))
2665             return 0;
2666
2667           {
2668             unsigned int cef = call_expr_flags (arg0);
2669             if (flags & OEP_PURE_SAME)
2670               cef &= ECF_CONST | ECF_PURE;
2671             else
2672               cef &= ECF_CONST;
2673             if (!cef)
2674               return 0;
2675           }
2676
2677           /* Now see if all the arguments are the same.  */
2678           {
2679             const_call_expr_arg_iterator iter0, iter1;
2680             const_tree a0, a1;
2681             for (a0 = first_const_call_expr_arg (arg0, &iter0),
2682                    a1 = first_const_call_expr_arg (arg1, &iter1);
2683                  a0 && a1;
2684                  a0 = next_const_call_expr_arg (&iter0),
2685                    a1 = next_const_call_expr_arg (&iter1))
2686               if (! operand_equal_p (a0, a1, flags))
2687                 return 0;
2688
2689             /* If we get here and both argument lists are exhausted
2690                then the CALL_EXPRs are equal.  */
2691             return ! (a0 || a1);
2692           }
2693         default:
2694           return 0;
2695         }
2696
2697     case tcc_declaration:
2698       /* Consider __builtin_sqrt equal to sqrt.  */
2699       return (TREE_CODE (arg0) == FUNCTION_DECL
2700               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2701               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2702               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2703
2704     default:
2705       return 0;
2706     }
2707
2708 #undef OP_SAME
2709 #undef OP_SAME_WITH_NULL
2710 }
2711 \f
2712 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2713    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2714
2715    When in doubt, return 0.  */
2716
2717 static int
2718 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2719 {
2720   int unsignedp1, unsignedpo;
2721   tree primarg0, primarg1, primother;
2722   unsigned int correct_width;
2723
2724   if (operand_equal_p (arg0, arg1, 0))
2725     return 1;
2726
2727   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2728       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2729     return 0;
2730
2731   /* Discard any conversions that don't change the modes of ARG0 and ARG1
2732      and see if the inner values are the same.  This removes any
2733      signedness comparison, which doesn't matter here.  */
2734   primarg0 = arg0, primarg1 = arg1;
2735   STRIP_NOPS (primarg0);
2736   STRIP_NOPS (primarg1);
2737   if (operand_equal_p (primarg0, primarg1, 0))
2738     return 1;
2739
2740   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2741      actual comparison operand, ARG0.
2742
2743      First throw away any conversions to wider types
2744      already present in the operands.  */
2745
2746   primarg1 = get_narrower (arg1, &unsignedp1);
2747   primother = get_narrower (other, &unsignedpo);
2748
2749   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2750   if (unsignedp1 == unsignedpo
2751       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2752       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2753     {
2754       tree type = TREE_TYPE (arg0);
2755
2756       /* Make sure shorter operand is extended the right way
2757          to match the longer operand.  */
2758       primarg1 = fold_convert (signed_or_unsigned_type_for
2759                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2760
2761       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2762         return 1;
2763     }
2764
2765   return 0;
2766 }
2767 \f
2768 /* See if ARG is an expression that is either a comparison or is performing
2769    arithmetic on comparisons.  The comparisons must only be comparing
2770    two different values, which will be stored in *CVAL1 and *CVAL2; if
2771    they are nonzero it means that some operands have already been found.
2772    No variables may be used anywhere else in the expression except in the
2773    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
2774    the expression and save_expr needs to be called with CVAL1 and CVAL2.
2775
2776    If this is true, return 1.  Otherwise, return zero.  */
2777
2778 static int
2779 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2780 {
2781   enum tree_code code = TREE_CODE (arg);
2782   enum tree_code_class tclass = TREE_CODE_CLASS (code);
2783
2784   /* We can handle some of the tcc_expression cases here.  */
2785   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2786     tclass = tcc_unary;
2787   else if (tclass == tcc_expression
2788            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2789                || code == COMPOUND_EXPR))
2790     tclass = tcc_binary;
2791
2792   else if (tclass == tcc_expression && code == SAVE_EXPR
2793            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2794     {
2795       /* If we've already found a CVAL1 or CVAL2, this expression is
2796          two complex to handle.  */
2797       if (*cval1 || *cval2)
2798         return 0;
2799
2800       tclass = tcc_unary;
2801       *save_p = 1;
2802     }
2803
2804   switch (tclass)
2805     {
2806     case tcc_unary:
2807       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2808
2809     case tcc_binary:
2810       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2811               && twoval_comparison_p (TREE_OPERAND (arg, 1),
2812                                       cval1, cval2, save_p));
2813
2814     case tcc_constant:
2815       return 1;
2816
2817     case tcc_expression:
2818       if (code == COND_EXPR)
2819         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2820                                      cval1, cval2, save_p)
2821                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2822                                         cval1, cval2, save_p)
2823                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2824                                         cval1, cval2, save_p));
2825       return 0;
2826
2827     case tcc_comparison:
2828       /* First see if we can handle the first operand, then the second.  For
2829          the second operand, we know *CVAL1 can't be zero.  It must be that
2830          one side of the comparison is each of the values; test for the
2831          case where this isn't true by failing if the two operands
2832          are the same.  */
2833
2834       if (operand_equal_p (TREE_OPERAND (arg, 0),
2835                            TREE_OPERAND (arg, 1), 0))
2836         return 0;
2837
2838       if (*cval1 == 0)
2839         *cval1 = TREE_OPERAND (arg, 0);
2840       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2841         ;
2842       else if (*cval2 == 0)
2843         *cval2 = TREE_OPERAND (arg, 0);
2844       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2845         ;
2846       else
2847         return 0;
2848
2849       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2850         ;
2851       else if (*cval2 == 0)
2852         *cval2 = TREE_OPERAND (arg, 1);
2853       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2854         ;
2855       else
2856         return 0;
2857
2858       return 1;
2859
2860     default:
2861       return 0;
2862     }
2863 }
2864 \f
2865 /* ARG is a tree that is known to contain just arithmetic operations and
2866    comparisons.  Evaluate the operations in the tree substituting NEW0 for
2867    any occurrence of OLD0 as an operand of a comparison and likewise for
2868    NEW1 and OLD1.  */
2869
2870 static tree
2871 eval_subst (location_t loc, tree arg, tree old0, tree new0,
2872             tree old1, tree new1)
2873 {
2874   tree type = TREE_TYPE (arg);
2875   enum tree_code code = TREE_CODE (arg);
2876   enum tree_code_class tclass = TREE_CODE_CLASS (code);
2877
2878   /* We can handle some of the tcc_expression cases here.  */
2879   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2880     tclass = tcc_unary;
2881   else if (tclass == tcc_expression
2882            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2883     tclass = tcc_binary;
2884
2885   switch (tclass)
2886     {
2887     case tcc_unary:
2888       return fold_build1_loc (loc, code, type,
2889                           eval_subst (loc, TREE_OPERAND (arg, 0),
2890                                       old0, new0, old1, new1));
2891
2892     case tcc_binary:
2893       return fold_build2_loc (loc, code, type,
2894                           eval_subst (loc, TREE_OPERAND (arg, 0),
2895                                       old0, new0, old1, new1),
2896                           eval_subst (loc, TREE_OPERAND (arg, 1),
2897                                       old0, new0, old1, new1));
2898
2899     case tcc_expression:
2900       switch (code)
2901         {
2902         case SAVE_EXPR:
2903           return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
2904                              old1, new1);
2905
2906         case COMPOUND_EXPR:
2907           return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
2908                              old1, new1);
2909
2910         case COND_EXPR:
2911           return fold_build3_loc (loc, code, type,
2912                               eval_subst (loc, TREE_OPERAND (arg, 0),
2913                                           old0, new0, old1, new1),
2914                               eval_subst (loc, TREE_OPERAND (arg, 1),
2915                                           old0, new0, old1, new1),
2916                               eval_subst (loc, TREE_OPERAND (arg, 2),
2917                                           old0, new0, old1, new1));
2918         default:
2919           break;
2920         }
2921       /* Fall through - ???  */
2922
2923     case tcc_comparison:
2924       {
2925         tree arg0 = TREE_OPERAND (arg, 0);
2926         tree arg1 = TREE_OPERAND (arg, 1);
2927
2928         /* We need to check both for exact equality and tree equality.  The
2929            former will be true if the operand has a side-effect.  In that
2930            case, we know the operand occurred exactly once.  */
2931
2932         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2933           arg0 = new0;
2934         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2935           arg0 = new1;
2936
2937         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2938           arg1 = new0;
2939         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2940           arg1 = new1;
2941
2942         return fold_build2_loc (loc, code, type, arg0, arg1);
2943       }
2944
2945     default:
2946       return arg;
2947     }
2948 }
2949 \f
2950 /* Return a tree for the case when the result of an expression is RESULT
2951    converted to TYPE and OMITTED was previously an operand of the expression
2952    but is now not needed (e.g., we folded OMITTED * 0).
2953
2954    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
2955    the conversion of RESULT to TYPE.  */
2956
2957 tree
2958 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
2959 {
2960   tree t = fold_convert_loc (loc, type, result);
2961
2962   /* If the resulting operand is an empty statement, just return the omitted
2963      statement casted to void. */
2964   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
2965     {
2966       t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
2967       goto omit_one_operand_exit;
2968     }
2969
2970   if (TREE_SIDE_EFFECTS (omitted))
2971     {
2972       t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
2973       goto omit_one_operand_exit;
2974     }
2975
2976   return non_lvalue_loc (loc, t);
2977
2978  omit_one_operand_exit:
2979   protected_set_expr_location (t, loc);
2980   return t;
2981 }
2982
2983 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
2984
2985 static tree
2986 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
2987                                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     {
2995       t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
2996       goto pedantic_omit_one_operand_exit;
2997     }
2998
2999   if (TREE_SIDE_EFFECTS (omitted))
3000     {
3001       t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3002       goto pedantic_omit_one_operand_exit;
3003     }
3004
3005   return pedantic_non_lvalue_loc (loc, t);
3006
3007  pedantic_omit_one_operand_exit:
3008   protected_set_expr_location (t, loc);
3009   return t;
3010 }
3011
3012 /* Return a tree for the case when the result of an expression is RESULT
3013    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3014    of the expression but are now not needed.
3015
3016    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3017    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3018    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3019    just do the conversion of RESULT to TYPE.  */
3020
3021 tree
3022 omit_two_operands_loc (location_t loc, tree type, tree result,
3023                    tree omitted1, tree omitted2)
3024 {
3025   tree t = fold_convert_loc (loc, type, result);
3026
3027   if (TREE_SIDE_EFFECTS (omitted2))
3028     {
3029       t = build2 (COMPOUND_EXPR, type, omitted2, t);
3030       SET_EXPR_LOCATION (t, loc);
3031     }
3032   if (TREE_SIDE_EFFECTS (omitted1))
3033     {
3034       t = build2 (COMPOUND_EXPR, type, omitted1, t);
3035       SET_EXPR_LOCATION (t, loc);
3036     }
3037
3038   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3039 }
3040
3041 \f
3042 /* Return a simplified tree node for the truth-negation of ARG.  This
3043    never alters ARG itself.  We assume that ARG is an operation that
3044    returns a truth value (0 or 1).
3045
3046    FIXME: one would think we would fold the result, but it causes
3047    problems with the dominator optimizer.  */
3048
3049 tree
3050 fold_truth_not_expr (location_t loc, tree arg)
3051 {
3052   tree t, type = TREE_TYPE (arg);
3053   enum tree_code code = TREE_CODE (arg);
3054   location_t loc1, loc2;
3055
3056   /* If this is a comparison, we can simply invert it, except for
3057      floating-point non-equality comparisons, in which case we just
3058      enclose a TRUTH_NOT_EXPR around what we have.  */
3059
3060   if (TREE_CODE_CLASS (code) == tcc_comparison)
3061     {
3062       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3063       if (FLOAT_TYPE_P (op_type)
3064           && flag_trapping_math
3065           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3066           && code != NE_EXPR && code != EQ_EXPR)
3067         return NULL_TREE;
3068
3069       code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3070       if (code == ERROR_MARK)
3071         return NULL_TREE;
3072
3073       t = build2 (code, type, TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3074       SET_EXPR_LOCATION (t, loc);
3075       return t;
3076     }
3077
3078   switch (code)
3079     {
3080     case INTEGER_CST:
3081       return constant_boolean_node (integer_zerop (arg), type);
3082
3083     case TRUTH_AND_EXPR:
3084       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3085       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3086       if (loc1 == UNKNOWN_LOCATION)
3087         loc1 = loc;
3088       if (loc2 == UNKNOWN_LOCATION)
3089         loc2 = loc;
3090       t = build2 (TRUTH_OR_EXPR, type,
3091                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3092                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3093       break;
3094
3095     case TRUTH_OR_EXPR:
3096       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3097       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3098       if (loc1 == UNKNOWN_LOCATION)
3099         loc1 = loc;
3100       if (loc2 == UNKNOWN_LOCATION)
3101         loc2 = loc;
3102       t = build2 (TRUTH_AND_EXPR, type,
3103                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3104                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3105       break;
3106
3107     case TRUTH_XOR_EXPR:
3108       /* Here we can invert either operand.  We invert the first operand
3109          unless the second operand is a TRUTH_NOT_EXPR in which case our
3110          result is the XOR of the first operand with the inside of the
3111          negation of the second operand.  */
3112
3113       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3114         t = build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3115                     TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3116       else
3117         t = build2 (TRUTH_XOR_EXPR, type,
3118                     invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3119                     TREE_OPERAND (arg, 1));
3120       break;
3121
3122     case TRUTH_ANDIF_EXPR:
3123       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3124       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3125       if (loc1 == UNKNOWN_LOCATION)
3126         loc1 = loc;
3127       if (loc2 == UNKNOWN_LOCATION)
3128         loc2 = loc;
3129       t = build2 (TRUTH_ORIF_EXPR, type,
3130                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3131                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3132       break;
3133
3134     case TRUTH_ORIF_EXPR:
3135       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3136       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3137       if (loc1 == UNKNOWN_LOCATION)
3138         loc1 = loc;
3139       if (loc2 == UNKNOWN_LOCATION)
3140         loc2 = loc;
3141       t = build2 (TRUTH_ANDIF_EXPR, type,
3142                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3143                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3144       break;
3145
3146     case TRUTH_NOT_EXPR:
3147       return TREE_OPERAND (arg, 0);
3148
3149     case COND_EXPR:
3150       {
3151         tree arg1 = TREE_OPERAND (arg, 1);
3152         tree arg2 = TREE_OPERAND (arg, 2);
3153
3154         loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3155         loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 2));
3156         if (loc1 == UNKNOWN_LOCATION)
3157           loc1 = loc;
3158         if (loc2 == UNKNOWN_LOCATION)
3159           loc2 = loc;
3160
3161         /* A COND_EXPR may have a throw as one operand, which
3162            then has void type.  Just leave void operands
3163            as they are.  */
3164         t = build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3165                     VOID_TYPE_P (TREE_TYPE (arg1))
3166                     ? arg1 : invert_truthvalue_loc (loc1, arg1),
3167                     VOID_TYPE_P (TREE_TYPE (arg2))
3168                     ? arg2 : invert_truthvalue_loc (loc2, arg2));
3169         break;
3170       }
3171
3172     case COMPOUND_EXPR:
3173       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3174       if (loc1 == UNKNOWN_LOCATION)
3175         loc1 = loc;
3176       t = build2 (COMPOUND_EXPR, type,
3177                   TREE_OPERAND (arg, 0),
3178                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3179       break;
3180
3181     case NON_LVALUE_EXPR:
3182       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3183       if (loc1 == UNKNOWN_LOCATION)
3184         loc1 = loc;
3185       return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3186
3187     CASE_CONVERT:
3188       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3189         {
3190           t = build1 (TRUTH_NOT_EXPR, type, arg);
3191           break;
3192         }
3193
3194       /* ... fall through ...  */
3195
3196     case FLOAT_EXPR:
3197       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3198       if (loc1 == UNKNOWN_LOCATION)
3199         loc1 = loc;
3200       t = build1 (TREE_CODE (arg), type,
3201                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3202       break;
3203
3204     case BIT_AND_EXPR:
3205       if (!integer_onep (TREE_OPERAND (arg, 1)))
3206         return NULL_TREE;
3207       t = build2 (EQ_EXPR, type, arg, build_int_cst (type, 0));
3208       break;
3209
3210     case SAVE_EXPR:
3211       t = build1 (TRUTH_NOT_EXPR, type, arg);
3212       break;
3213
3214     case CLEANUP_POINT_EXPR:
3215       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3216       if (loc1 == UNKNOWN_LOCATION)
3217         loc1 = loc;
3218       t = build1 (CLEANUP_POINT_EXPR, type,
3219                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3220       break;
3221
3222     default:
3223       t = NULL_TREE;
3224       break;
3225     }
3226
3227   if (t)
3228     SET_EXPR_LOCATION (t, loc);
3229
3230   return t;
3231 }
3232
3233 /* Return a simplified tree node for the truth-negation of ARG.  This
3234    never alters ARG itself.  We assume that ARG is an operation that
3235    returns a truth value (0 or 1).
3236
3237    FIXME: one would think we would fold the result, but it causes
3238    problems with the dominator optimizer.  */
3239
3240 tree
3241 invert_truthvalue_loc (location_t loc, tree arg)
3242 {
3243   tree tem;
3244
3245   if (TREE_CODE (arg) == ERROR_MARK)
3246     return arg;
3247
3248   tem = fold_truth_not_expr (loc, arg);
3249   if (!tem)
3250     {
3251       tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3252       SET_EXPR_LOCATION (tem, loc);
3253     }
3254
3255   return tem;
3256 }
3257
3258 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3259    operands are another bit-wise operation with a common input.  If so,
3260    distribute the bit operations to save an operation and possibly two if
3261    constants are involved.  For example, convert
3262         (A | B) & (A | C) into A | (B & C)
3263    Further simplification will occur if B and C are constants.
3264
3265    If this optimization cannot be done, 0 will be returned.  */
3266
3267 static tree
3268 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
3269                      tree arg0, tree arg1)
3270 {
3271   tree common;
3272   tree left, right;
3273
3274   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3275       || TREE_CODE (arg0) == code
3276       || (TREE_CODE (arg0) != BIT_AND_EXPR
3277           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3278     return 0;
3279
3280   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3281     {
3282       common = TREE_OPERAND (arg0, 0);
3283       left = TREE_OPERAND (arg0, 1);
3284       right = TREE_OPERAND (arg1, 1);
3285     }
3286   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3287     {
3288       common = TREE_OPERAND (arg0, 0);
3289       left = TREE_OPERAND (arg0, 1);
3290       right = TREE_OPERAND (arg1, 0);
3291     }
3292   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3293     {
3294       common = TREE_OPERAND (arg0, 1);
3295       left = TREE_OPERAND (arg0, 0);
3296       right = TREE_OPERAND (arg1, 1);
3297     }
3298   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3299     {
3300       common = TREE_OPERAND (arg0, 1);
3301       left = TREE_OPERAND (arg0, 0);
3302       right = TREE_OPERAND (arg1, 0);
3303     }
3304   else
3305     return 0;
3306
3307   common = fold_convert_loc (loc, type, common);
3308   left = fold_convert_loc (loc, type, left);
3309   right = fold_convert_loc (loc, type, right);
3310   return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
3311                       fold_build2_loc (loc, code, type, left, right));
3312 }
3313
3314 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3315    with code CODE.  This optimization is unsafe.  */
3316 static tree
3317 distribute_real_division (location_t loc, enum tree_code code, tree type,
3318                           tree arg0, tree arg1)
3319 {
3320   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3321   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3322
3323   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3324   if (mul0 == mul1
3325       && operand_equal_p (TREE_OPERAND (arg0, 1),
3326                        TREE_OPERAND (arg1, 1), 0))
3327     return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3328                         fold_build2_loc (loc, code, type,
3329                                      TREE_OPERAND (arg0, 0),
3330                                      TREE_OPERAND (arg1, 0)),
3331                         TREE_OPERAND (arg0, 1));
3332
3333   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3334   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3335                        TREE_OPERAND (arg1, 0), 0)
3336       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3337       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3338     {
3339       REAL_VALUE_TYPE r0, r1;
3340       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3341       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3342       if (!mul0)
3343         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3344       if (!mul1)
3345         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3346       real_arithmetic (&r0, code, &r0, &r1);
3347       return fold_build2_loc (loc, MULT_EXPR, type,
3348                           TREE_OPERAND (arg0, 0),
3349                           build_real (type, r0));
3350     }
3351
3352   return NULL_TREE;
3353 }
3354 \f
3355 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3356    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3357
3358 static tree
3359 make_bit_field_ref (location_t loc, tree inner, tree type,
3360                     HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3361 {
3362   tree result, bftype;
3363
3364   if (bitpos == 0)
3365     {
3366       tree size = TYPE_SIZE (TREE_TYPE (inner));
3367       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3368            || POINTER_TYPE_P (TREE_TYPE (inner)))
3369           && host_integerp (size, 0)
3370           && tree_low_cst (size, 0) == bitsize)
3371         return fold_convert_loc (loc, type, inner);
3372     }
3373
3374   bftype = type;
3375   if (TYPE_PRECISION (bftype) != bitsize
3376       || TYPE_UNSIGNED (bftype) == !unsignedp)
3377     bftype = build_nonstandard_integer_type (bitsize, 0);
3378
3379   result = build3 (BIT_FIELD_REF, bftype, inner,
3380                    size_int (bitsize), bitsize_int (bitpos));
3381   SET_EXPR_LOCATION (result, loc);
3382
3383   if (bftype != type)
3384     result = fold_convert_loc (loc, type, result);
3385
3386   return result;
3387 }
3388
3389 /* Optimize a bit-field compare.
3390
3391    There are two cases:  First is a compare against a constant and the
3392    second is a comparison of two items where the fields are at the same
3393    bit position relative to the start of a chunk (byte, halfword, word)
3394    large enough to contain it.  In these cases we can avoid the shift
3395    implicit in bitfield extractions.
3396
3397    For constants, we emit a compare of the shifted constant with the
3398    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3399    compared.  For two fields at the same position, we do the ANDs with the
3400    similar mask and compare the result of the ANDs.
3401
3402    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3403    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3404    are the left and right operands of the comparison, respectively.
3405
3406    If the optimization described above can be done, we return the resulting
3407    tree.  Otherwise we return zero.  */
3408
3409 static tree
3410 optimize_bit_field_compare (location_t loc, enum tree_code code,
3411                             tree compare_type, tree lhs, tree rhs)
3412 {
3413   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3414   tree type = TREE_TYPE (lhs);
3415   tree signed_type, unsigned_type;
3416   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3417   enum machine_mode lmode, rmode, nmode;
3418   int lunsignedp, runsignedp;
3419   int lvolatilep = 0, rvolatilep = 0;
3420   tree linner, rinner = NULL_TREE;
3421   tree mask;
3422   tree offset;
3423
3424   /* Get all the information about the extractions being done.  If the bit size
3425      if the same as the size of the underlying object, we aren't doing an
3426      extraction at all and so can do nothing.  We also don't want to
3427      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3428      then will no longer be able to replace it.  */
3429   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3430                                 &lunsignedp, &lvolatilep, false);
3431   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3432       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3433     return 0;
3434
3435  if (!const_p)
3436    {
3437      /* If this is not a constant, we can only do something if bit positions,
3438         sizes, and signedness are the same.  */
3439      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3440                                    &runsignedp, &rvolatilep, false);
3441
3442      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3443          || lunsignedp != runsignedp || offset != 0
3444          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3445        return 0;
3446    }
3447
3448   /* See if we can find a mode to refer to this field.  We should be able to,
3449      but fail if we can't.  */
3450   if (lvolatilep
3451       && GET_MODE_BITSIZE (lmode) > 0
3452       && flag_strict_volatile_bitfields > 0)
3453     nmode = lmode;
3454   else
3455     nmode = get_best_mode (lbitsize, lbitpos,
3456                            const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3457                            : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3458                                   TYPE_ALIGN (TREE_TYPE (rinner))),
3459                            word_mode, lvolatilep || rvolatilep);
3460   if (nmode == VOIDmode)
3461     return 0;
3462
3463   /* Set signed and unsigned types of the precision of this mode for the
3464      shifts below.  */
3465   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3466   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3467
3468   /* Compute the bit position and size for the new reference and our offset
3469      within it. If the new reference is the same size as the original, we
3470      won't optimize anything, so return zero.  */
3471   nbitsize = GET_MODE_BITSIZE (nmode);
3472   nbitpos = lbitpos & ~ (nbitsize - 1);
3473   lbitpos -= nbitpos;
3474   if (nbitsize == lbitsize)
3475     return 0;
3476
3477   if (BYTES_BIG_ENDIAN)
3478     lbitpos = nbitsize - lbitsize - lbitpos;
3479
3480   /* Make the mask to be used against the extracted field.  */
3481   mask = build_int_cst_type (unsigned_type, -1);
3482   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3483   mask = const_binop (RSHIFT_EXPR, mask,
3484                       size_int (nbitsize - lbitsize - lbitpos));
3485
3486   if (! const_p)
3487     /* If not comparing with constant, just rework the comparison
3488        and return.  */
3489     return fold_build2_loc (loc, code, compare_type,
3490                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3491                                      make_bit_field_ref (loc, linner,
3492                                                          unsigned_type,
3493                                                          nbitsize, nbitpos,
3494                                                          1),
3495                                      mask),
3496                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3497                                      make_bit_field_ref (loc, rinner,
3498                                                          unsigned_type,
3499                                                          nbitsize, nbitpos,
3500                                                          1),
3501                                      mask));
3502
3503   /* Otherwise, we are handling the constant case. See if the constant is too
3504      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
3505      this not only for its own sake, but to avoid having to test for this
3506      error case below.  If we didn't, we might generate wrong code.
3507
3508      For unsigned fields, the constant shifted right by the field length should
3509      be all zero.  For signed fields, the high-order bits should agree with
3510      the sign bit.  */
3511
3512   if (lunsignedp)
3513     {
3514       if (! integer_zerop (const_binop (RSHIFT_EXPR,
3515                                         fold_convert_loc (loc,
3516                                                           unsigned_type, rhs),
3517                                         size_int (lbitsize))))
3518         {
3519           warning (0, "comparison is always %d due to width of bit-field",
3520                    code == NE_EXPR);
3521           return constant_boolean_node (code == NE_EXPR, compare_type);
3522         }
3523     }
3524   else
3525     {
3526       tree tem = const_binop (RSHIFT_EXPR,
3527                               fold_convert_loc (loc, signed_type, rhs),
3528                               size_int (lbitsize - 1));
3529       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3530         {
3531           warning (0, "comparison is always %d due to width of bit-field",
3532                    code == NE_EXPR);
3533           return constant_boolean_node (code == NE_EXPR, compare_type);
3534         }
3535     }
3536
3537   /* Single-bit compares should always be against zero.  */
3538   if (lbitsize == 1 && ! integer_zerop (rhs))
3539     {
3540       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3541       rhs = build_int_cst (type, 0);
3542     }
3543
3544   /* Make a new bitfield reference, shift the constant over the
3545      appropriate number of bits and mask it with the computed mask
3546      (in case this was a signed field).  If we changed it, make a new one.  */
3547   lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3548   if (lvolatilep)
3549     {
3550       TREE_SIDE_EFFECTS (lhs) = 1;
3551       TREE_THIS_VOLATILE (lhs) = 1;
3552     }
3553
3554   rhs = const_binop (BIT_AND_EXPR,
3555                      const_binop (LSHIFT_EXPR,
3556                                   fold_convert_loc (loc, unsigned_type, rhs),
3557                                   size_int (lbitpos)),
3558                      mask);
3559
3560   lhs = build2 (code, compare_type,
3561                 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
3562                 rhs);
3563   SET_EXPR_LOCATION (lhs, loc);
3564   return lhs;
3565 }
3566 \f
3567 /* Subroutine for fold_truthop: decode a field reference.
3568
3569    If EXP is a comparison reference, we return the innermost reference.
3570
3571    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3572    set to the starting bit number.
3573
3574    If the innermost field can be completely contained in a mode-sized
3575    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
3576
3577    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3578    otherwise it is not changed.
3579
3580    *PUNSIGNEDP is set to the signedness of the field.
3581
3582    *PMASK is set to the mask used.  This is either contained in a
3583    BIT_AND_EXPR or derived from the width of the field.
3584
3585    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3586
3587    Return 0 if this is not a component reference or is one that we can't
3588    do anything with.  */
3589
3590 static tree
3591 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3592                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3593                         int *punsignedp, int *pvolatilep,
3594                         tree *pmask, tree *pand_mask)
3595 {
3596   tree outer_type = 0;
3597   tree and_mask = 0;
3598   tree mask, inner, offset;
3599   tree unsigned_type;
3600   unsigned int precision;
3601
3602   /* All the optimizations using this function assume integer fields.
3603      There are problems with FP fields since the type_for_size call
3604      below can fail for, e.g., XFmode.  */
3605   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3606     return 0;
3607
3608   /* We are interested in the bare arrangement of bits, so strip everything
3609      that doesn't affect the machine mode.  However, record the type of the
3610      outermost expression if it may matter below.  */
3611   if (CONVERT_EXPR_P (exp)
3612       || TREE_CODE (exp) == NON_LVALUE_EXPR)
3613     outer_type = TREE_TYPE (exp);
3614   STRIP_NOPS (exp);
3615
3616   if (TREE_CODE (exp) == BIT_AND_EXPR)
3617     {
3618       and_mask = TREE_OPERAND (exp, 1);
3619       exp = TREE_OPERAND (exp, 0);
3620       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3621       if (TREE_CODE (and_mask) != INTEGER_CST)
3622         return 0;
3623     }
3624
3625   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3626                                punsignedp, pvolatilep, false);
3627   if ((inner == exp && and_mask == 0)
3628       || *pbitsize < 0 || offset != 0
3629       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3630     return 0;
3631
3632   /* If the number of bits in the reference is the same as the bitsize of
3633      the outer type, then the outer type gives the signedness. Otherwise
3634      (in case of a small bitfield) the signedness is unchanged.  */
3635   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3636     *punsignedp = TYPE_UNSIGNED (outer_type);
3637
3638   /* Compute the mask to access the bitfield.  */
3639   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3640   precision = TYPE_PRECISION (unsigned_type);
3641
3642   mask = build_int_cst_type (unsigned_type, -1);
3643
3644   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3645   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3646
3647   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
3648   if (and_mask != 0)
3649     mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3650                         fold_convert_loc (loc, unsigned_type, and_mask), mask);
3651
3652   *pmask = mask;
3653   *pand_mask = and_mask;
3654   return inner;
3655 }
3656
3657 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3658    bit positions.  */
3659
3660 static int
3661 all_ones_mask_p (const_tree mask, int size)
3662 {
3663   tree type = TREE_TYPE (mask);
3664   unsigned int precision = TYPE_PRECISION (type);
3665   tree tmask;
3666
3667   tmask = build_int_cst_type (signed_type_for (type), -1);
3668
3669   return
3670     tree_int_cst_equal (mask,
3671                         const_binop (RSHIFT_EXPR,
3672                                      const_binop (LSHIFT_EXPR, tmask,
3673                                                   size_int (precision - size)),
3674                                      size_int (precision - size)));
3675 }
3676
3677 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3678    represents the sign bit of EXP's type.  If EXP represents a sign
3679    or zero extension, also test VAL against the unextended type.
3680    The return value is the (sub)expression whose sign bit is VAL,
3681    or NULL_TREE otherwise.  */
3682
3683 static tree
3684 sign_bit_p (tree exp, const_tree val)
3685 {
3686   unsigned HOST_WIDE_INT mask_lo, lo;
3687   HOST_WIDE_INT mask_hi, hi;
3688   int width;
3689   tree t;
3690
3691   /* Tree EXP must have an integral type.  */
3692   t = TREE_TYPE (exp);
3693   if (! INTEGRAL_TYPE_P (t))
3694     return NULL_TREE;
3695
3696   /* Tree VAL must be an integer constant.  */
3697   if (TREE_CODE (val) != INTEGER_CST
3698       || TREE_OVERFLOW (val))
3699     return NULL_TREE;
3700
3701   width = TYPE_PRECISION (t);
3702   if (width > HOST_BITS_PER_WIDE_INT)
3703     {
3704       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3705       lo = 0;
3706
3707       mask_hi = ((unsigned HOST_WIDE_INT) -1
3708                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
3709       mask_lo = -1;
3710     }
3711   else
3712     {
3713       hi = 0;
3714       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3715
3716       mask_hi = 0;
3717       mask_lo = ((unsigned HOST_WIDE_INT) -1
3718                  >> (HOST_BITS_PER_WIDE_INT - width));
3719     }
3720
3721   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3722      treat VAL as if it were unsigned.  */
3723   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3724       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3725     return exp;
3726
3727   /* Handle extension from a narrower type.  */
3728   if (TREE_CODE (exp) == NOP_EXPR
3729       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3730     return sign_bit_p (TREE_OPERAND (exp, 0), val);
3731
3732   return NULL_TREE;
3733 }
3734
3735 /* Subroutine for fold_truthop: determine if an operand is simple enough
3736    to be evaluated unconditionally.  */
3737
3738 static int
3739 simple_operand_p (const_tree exp)
3740 {
3741   /* Strip any conversions that don't change the machine mode.  */
3742   STRIP_NOPS (exp);
3743
3744   return (CONSTANT_CLASS_P (exp)
3745           || TREE_CODE (exp) == SSA_NAME
3746           || (DECL_P (exp)
3747               && ! TREE_ADDRESSABLE (exp)
3748               && ! TREE_THIS_VOLATILE (exp)
3749               && ! DECL_NONLOCAL (exp)
3750               /* Don't regard global variables as simple.  They may be
3751                  allocated in ways unknown to the compiler (shared memory,
3752                  #pragma weak, etc).  */
3753               && ! TREE_PUBLIC (exp)
3754               && ! DECL_EXTERNAL (exp)
3755               /* Loading a static variable is unduly expensive, but global
3756                  registers aren't expensive.  */
3757               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3758 }
3759 \f
3760 /* The following functions are subroutines to fold_range_test and allow it to
3761    try to change a logical combination of comparisons into a range test.
3762
3763    For example, both
3764         X == 2 || X == 3 || X == 4 || X == 5
3765    and
3766         X >= 2 && X <= 5
3767    are converted to
3768         (unsigned) (X - 2) <= 3
3769
3770    We describe each set of comparisons as being either inside or outside
3771    a range, using a variable named like IN_P, and then describe the
3772    range with a lower and upper bound.  If one of the bounds is omitted,
3773    it represents either the highest or lowest value of the type.
3774
3775    In the comments below, we represent a range by two numbers in brackets
3776    preceded by a "+" to designate being inside that range, or a "-" to
3777    designate being outside that range, so the condition can be inverted by
3778    flipping the prefix.  An omitted bound is represented by a "-".  For
3779    example, "- [-, 10]" means being outside the range starting at the lowest
3780    possible value and ending at 10, in other words, being greater than 10.
3781    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3782    always false.
3783
3784    We set up things so that the missing bounds are handled in a consistent
3785    manner so neither a missing bound nor "true" and "false" need to be
3786    handled using a special case.  */
3787
3788 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3789    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3790    and UPPER1_P are nonzero if the respective argument is an upper bound
3791    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
3792    must be specified for a comparison.  ARG1 will be converted to ARG0's
3793    type if both are specified.  */
3794
3795 static tree
3796 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3797              tree arg1, int upper1_p)
3798 {
3799   tree tem;
3800   int result;
3801   int sgn0, sgn1;
3802
3803   /* If neither arg represents infinity, do the normal operation.
3804      Else, if not a comparison, return infinity.  Else handle the special
3805      comparison rules. Note that most of the cases below won't occur, but
3806      are handled for consistency.  */
3807
3808   if (arg0 != 0 && arg1 != 0)
3809     {
3810       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3811                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
3812       STRIP_NOPS (tem);
3813       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3814     }
3815
3816   if (TREE_CODE_CLASS (code) != tcc_comparison)
3817     return 0;
3818
3819   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3820      for neither.  In real maths, we cannot assume open ended ranges are
3821      the same. But, this is computer arithmetic, where numbers are finite.
3822      We can therefore make the transformation of any unbounded range with
3823      the value Z, Z being greater than any representable number. This permits
3824      us to treat unbounded ranges as equal.  */
3825   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3826   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3827   switch (code)
3828     {
3829     case EQ_EXPR:
3830       result = sgn0 == sgn1;
3831       break;
3832     case NE_EXPR:
3833       result = sgn0 != sgn1;
3834       break;
3835     case LT_EXPR:
3836       result = sgn0 < sgn1;
3837       break;
3838     case LE_EXPR:
3839       result = sgn0 <= sgn1;
3840       break;
3841     case GT_EXPR:
3842       result = sgn0 > sgn1;
3843       break;
3844     case GE_EXPR:
3845       result = sgn0 >= sgn1;
3846       break;
3847     default:
3848       gcc_unreachable ();
3849     }
3850
3851   return constant_boolean_node (result, type);
3852 }
3853 \f
3854 /* Given EXP, a logical expression, set the range it is testing into
3855    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
3856    actually being tested.  *PLOW and *PHIGH will be made of the same
3857    type as the returned expression.  If EXP is not a comparison, we
3858    will most likely not be returning a useful value and range.  Set
3859    *STRICT_OVERFLOW_P to true if the return value is only valid
3860    because signed overflow is undefined; otherwise, do not change
3861    *STRICT_OVERFLOW_P.  */
3862
3863 tree
3864 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
3865             bool *strict_overflow_p)
3866 {
3867   enum tree_code code;
3868   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
3869   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
3870   int in_p, n_in_p;
3871   tree low, high, n_low, n_high;
3872   location_t loc = EXPR_LOCATION (exp);
3873
3874   /* Start with simply saying "EXP != 0" and then look at the code of EXP
3875      and see if we can refine the range.  Some of the cases below may not
3876      happen, but it doesn't seem worth worrying about this.  We "continue"
3877      the outer loop when we've changed something; otherwise we "break"
3878      the switch, which will "break" the while.  */
3879
3880   in_p = 0;
3881   low = high = build_int_cst (TREE_TYPE (exp), 0);
3882
3883   while (1)
3884     {
3885       code = TREE_CODE (exp);
3886       exp_type = TREE_TYPE (exp);
3887
3888       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3889         {
3890           if (TREE_OPERAND_LENGTH (exp) > 0)
3891             arg0 = TREE_OPERAND (exp, 0);
3892           if (TREE_CODE_CLASS (code) == tcc_comparison
3893               || TREE_CODE_CLASS (code) == tcc_unary
3894               || TREE_CODE_CLASS (code) == tcc_binary)
3895             arg0_type = TREE_TYPE (arg0);
3896           if (TREE_CODE_CLASS (code) == tcc_binary
3897               || TREE_CODE_CLASS (code) == tcc_comparison
3898               || (TREE_CODE_CLASS (code) == tcc_expression
3899                   && TREE_OPERAND_LENGTH (exp) > 1))
3900             arg1 = TREE_OPERAND (exp, 1);
3901         }
3902
3903       switch (code)
3904         {
3905         case TRUTH_NOT_EXPR:
3906           in_p = ! in_p, exp = arg0;
3907           continue;
3908
3909         case EQ_EXPR: case NE_EXPR:
3910         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3911           /* We can only do something if the range is testing for zero
3912              and if the second operand is an integer constant.  Note that
3913              saying something is "in" the range we make is done by
3914              complementing IN_P since it will set in the initial case of
3915              being not equal to zero; "out" is leaving it alone.  */
3916           if (low == 0 || high == 0
3917               || ! integer_zerop (low) || ! integer_zerop (high)
3918               || TREE_CODE (arg1) != INTEGER_CST)
3919             break;
3920
3921           switch (code)
3922             {
3923             case NE_EXPR:  /* - [c, c]  */
3924               low = high = arg1;
3925               break;
3926             case EQ_EXPR:  /* + [c, c]  */
3927               in_p = ! in_p, low = high = arg1;
3928               break;
3929             case GT_EXPR:  /* - [-, c] */
3930               low = 0, high = arg1;
3931               break;
3932             case GE_EXPR:  /* + [c, -] */
3933               in_p = ! in_p, low = arg1, high = 0;
3934               break;
3935             case LT_EXPR:  /* - [c, -] */
3936               low = arg1, high = 0;
3937               break;
3938             case LE_EXPR:  /* + [-, c] */
3939               in_p = ! in_p, low = 0, high = arg1;
3940               break;
3941             default:
3942               gcc_unreachable ();
3943             }
3944
3945           /* If this is an unsigned comparison, we also know that EXP is
3946              greater than or equal to zero.  We base the range tests we make
3947              on that fact, so we record it here so we can parse existing
3948              range tests.  We test arg0_type since often the return type
3949              of, e.g. EQ_EXPR, is boolean.  */
3950           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3951             {
3952               if (! merge_ranges (&n_in_p, &n_low, &n_high,
3953                                   in_p, low, high, 1,
3954                                   build_int_cst (arg0_type, 0),
3955                                   NULL_TREE))
3956                 break;
3957
3958               in_p = n_in_p, low = n_low, high = n_high;
3959
3960               /* If the high bound is missing, but we have a nonzero low
3961                  bound, reverse the range so it goes from zero to the low bound
3962                  minus 1.  */
3963               if (high == 0 && low && ! integer_zerop (low))
3964                 {
3965                   in_p = ! in_p;
3966                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3967                                       integer_one_node, 0);
3968                   low = build_int_cst (arg0_type, 0);
3969                 }
3970             }
3971
3972           exp = arg0;
3973           continue;
3974
3975         case NEGATE_EXPR:
3976           /* (-x) IN [a,b] -> x in [-b, -a]  */
3977           n_low = range_binop (MINUS_EXPR, exp_type,
3978                                build_int_cst (exp_type, 0),
3979                                0, high, 1);
3980           n_high = range_binop (MINUS_EXPR, exp_type,
3981                                 build_int_cst (exp_type, 0),
3982                                 0, low, 0);
3983           if (n_high != 0 && TREE_OVERFLOW (n_high))
3984             break;
3985           goto normalize;
3986
3987         case BIT_NOT_EXPR:
3988           /* ~ X -> -X - 1  */
3989           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
3990                         build_int_cst (exp_type, 1));
3991           SET_EXPR_LOCATION (exp, loc);
3992           continue;
3993
3994         case PLUS_EXPR:  case MINUS_EXPR:
3995           if (TREE_CODE (arg1) != INTEGER_CST)
3996             break;
3997
3998           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
3999              move a constant to the other side.  */
4000           if (!TYPE_UNSIGNED (arg0_type)
4001               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4002             break;
4003
4004           /* If EXP is signed, any overflow in the computation is undefined,
4005              so we don't worry about it so long as our computations on
4006              the bounds don't overflow.  For unsigned, overflow is defined
4007              and this is exactly the right thing.  */
4008           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4009                                arg0_type, low, 0, arg1, 0);
4010           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4011                                 arg0_type, high, 1, arg1, 0);
4012           if ((n_low != 0 && TREE_OVERFLOW (n_low))
4013               || (n_high != 0 && TREE_OVERFLOW (n_high)))
4014             break;
4015
4016           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4017             *strict_overflow_p = true;
4018
4019         normalize:
4020           /* Check for an unsigned range which has wrapped around the maximum
4021              value thus making n_high < n_low, and normalize it.  */
4022           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4023             {
4024               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4025                                  integer_one_node, 0);
4026               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4027                                   integer_one_node, 0);
4028
4029               /* If the range is of the form +/- [ x+1, x ], we won't
4030                  be able to normalize it.  But then, it represents the
4031                  whole range or the empty set, so make it
4032                  +/- [ -, - ].  */
4033               if (tree_int_cst_equal (n_low, low)
4034                   && tree_int_cst_equal (n_high, high))
4035                 low = high = 0;
4036               else
4037                 in_p = ! in_p;
4038             }
4039           else
4040             low = n_low, high = n_high;
4041
4042           exp = arg0;
4043           continue;
4044
4045         CASE_CONVERT: case NON_LVALUE_EXPR:
4046           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4047             break;
4048
4049           if (! INTEGRAL_TYPE_P (arg0_type)
4050               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4051               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4052             break;
4053
4054           n_low = low, n_high = high;
4055
4056           if (n_low != 0)
4057             n_low = fold_convert_loc (loc, arg0_type, n_low);
4058
4059           if (n_high != 0)
4060             n_high = fold_convert_loc (loc, arg0_type, n_high);
4061
4062
4063           /* If we're converting arg0 from an unsigned type, to exp,
4064              a signed type,  we will be doing the comparison as unsigned.
4065              The tests above have already verified that LOW and HIGH
4066              are both positive.
4067
4068              So we have to ensure that we will handle large unsigned
4069              values the same way that the current signed bounds treat
4070              negative values.  */
4071
4072           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4073             {
4074               tree high_positive;
4075               tree equiv_type;
4076               /* For fixed-point modes, we need to pass the saturating flag
4077                  as the 2nd parameter.  */
4078               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4079                 equiv_type = lang_hooks.types.type_for_mode
4080                              (TYPE_MODE (arg0_type),
4081                               TYPE_SATURATING (arg0_type));
4082               else
4083                 equiv_type = lang_hooks.types.type_for_mode
4084                              (TYPE_MODE (arg0_type), 1);
4085
4086               /* A range without an upper bound is, naturally, unbounded.
4087                  Since convert would have cropped a very large value, use
4088                  the max value for the destination type.  */
4089               high_positive
4090                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4091                 : TYPE_MAX_VALUE (arg0_type);
4092
4093               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4094                 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4095                                              fold_convert_loc (loc, arg0_type,
4096                                                                high_positive),
4097                                              build_int_cst (arg0_type, 1));
4098
4099               /* If the low bound is specified, "and" the range with the
4100                  range for which the original unsigned value will be
4101                  positive.  */
4102               if (low != 0)
4103                 {
4104                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4105                                       1, n_low, n_high, 1,
4106                                       fold_convert_loc (loc, arg0_type,
4107                                                         integer_zero_node),
4108                                       high_positive))
4109                     break;
4110
4111                   in_p = (n_in_p == in_p);
4112                 }
4113               else
4114                 {
4115                   /* Otherwise, "or" the range with the range of the input
4116                      that will be interpreted as negative.  */
4117                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4118                                       0, n_low, n_high, 1,
4119                                       fold_convert_loc (loc, arg0_type,
4120                                                         integer_zero_node),
4121                                       high_positive))
4122                     break;
4123
4124                   in_p = (in_p != n_in_p);
4125                 }
4126             }
4127
4128           exp = arg0;
4129           low = n_low, high = n_high;
4130           continue;
4131
4132         default:
4133           break;
4134         }
4135
4136       break;
4137     }
4138
4139   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4140   if (TREE_CODE (exp) == INTEGER_CST)
4141     {
4142       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4143                                                  exp, 0, low, 0))
4144                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4145                                                     exp, 1, high, 1)));
4146       low = high = 0;
4147       exp = 0;
4148     }
4149
4150   *pin_p = in_p, *plow = low, *phigh = high;
4151   return exp;
4152 }
4153 \f
4154 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4155    type, TYPE, return an expression to test if EXP is in (or out of, depending
4156    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4157
4158 tree
4159 build_range_check (location_t loc, tree type, tree exp, int in_p,
4160                    tree low, tree high)
4161 {
4162   tree etype = TREE_TYPE (exp), value;
4163
4164 #ifdef HAVE_canonicalize_funcptr_for_compare
4165   /* Disable this optimization for function pointer expressions
4166      on targets that require function pointer canonicalization.  */
4167   if (HAVE_canonicalize_funcptr_for_compare
4168       && TREE_CODE (etype) == POINTER_TYPE
4169       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4170     return NULL_TREE;
4171 #endif
4172
4173   if (! in_p)
4174     {
4175       value = build_range_check (loc, type, exp, 1, low, high);
4176       if (value != 0)
4177         return invert_truthvalue_loc (loc, value);
4178
4179       return 0;
4180     }
4181
4182   if (low == 0 && high == 0)
4183     return build_int_cst (type, 1);
4184
4185   if (low == 0)
4186     return fold_build2_loc (loc, LE_EXPR, type, exp,
4187                         fold_convert_loc (loc, etype, high));
4188
4189   if (high == 0)
4190     return fold_build2_loc (loc, GE_EXPR, type, exp,
4191                         fold_convert_loc (loc, etype, low));
4192
4193   if (operand_equal_p (low, high, 0))
4194     return fold_build2_loc (loc, EQ_EXPR, type, exp,
4195                         fold_convert_loc (loc, etype, low));
4196
4197   if (integer_zerop (low))
4198     {
4199       if (! TYPE_UNSIGNED (etype))
4200         {
4201           etype = unsigned_type_for (etype);
4202           high = fold_convert_loc (loc, etype, high);
4203           exp = fold_convert_loc (loc, etype, exp);
4204         }
4205       return build_range_check (loc, type, exp, 1, 0, high);
4206     }
4207
4208   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4209   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4210     {
4211       unsigned HOST_WIDE_INT lo;
4212       HOST_WIDE_INT hi;
4213       int prec;
4214
4215       prec = TYPE_PRECISION (etype);
4216       if (prec <= HOST_BITS_PER_WIDE_INT)
4217         {
4218           hi = 0;
4219           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4220         }
4221       else
4222         {
4223           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4224           lo = (unsigned HOST_WIDE_INT) -1;
4225         }
4226
4227       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4228         {
4229           if (TYPE_UNSIGNED (etype))
4230             {
4231               tree signed_etype = signed_type_for (etype);
4232               if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4233                 etype
4234                   = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4235               else
4236                 etype = signed_etype;
4237               exp = fold_convert_loc (loc, etype, exp);
4238             }
4239           return fold_build2_loc (loc, GT_EXPR, type, exp,
4240                               build_int_cst (etype, 0));
4241         }
4242     }
4243
4244   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4245      This requires wrap-around arithmetics for the type of the expression.
4246      First make sure that arithmetics in this type is valid, then make sure
4247      that it wraps around.  */
4248   if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4249     etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4250                                             TYPE_UNSIGNED (etype));
4251
4252   if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4253     {
4254       tree utype, minv, maxv;
4255
4256       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4257          for the type in question, as we rely on this here.  */
4258       utype = unsigned_type_for (etype);
4259       maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4260       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4261                           integer_one_node, 1);
4262       minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4263
4264       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4265                                       minv, 1, maxv, 1)))
4266         etype = utype;
4267       else
4268         return 0;
4269     }
4270
4271   high = fold_convert_loc (loc, etype, high);
4272   low = fold_convert_loc (loc, etype, low);
4273   exp = fold_convert_loc (loc, etype, exp);
4274
4275   value = const_binop (MINUS_EXPR, high, low);
4276
4277
4278   if (POINTER_TYPE_P (etype))
4279     {
4280       if (value != 0 && !TREE_OVERFLOW (value))
4281         {
4282           low = fold_convert_loc (loc, sizetype, low);
4283           low = fold_build1_loc (loc, NEGATE_EXPR, sizetype, low);
4284           return build_range_check (loc, type,
4285                                     fold_build2_loc (loc, POINTER_PLUS_EXPR,
4286                                                  etype, exp, low),
4287                                     1, build_int_cst (etype, 0), value);
4288         }
4289       return 0;
4290     }
4291
4292   if (value != 0 && !TREE_OVERFLOW (value))
4293     return build_range_check (loc, type,
4294                               fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4295                               1, build_int_cst (etype, 0), value);
4296
4297   return 0;
4298 }
4299 \f
4300 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4301
4302 static tree
4303 range_predecessor (tree val)
4304 {
4305   tree type = TREE_TYPE (val);
4306
4307   if (INTEGRAL_TYPE_P (type)
4308       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4309     return 0;
4310   else
4311     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4312 }
4313
4314 /* Return the successor of VAL in its type, handling the infinite case.  */
4315
4316 static tree
4317 range_successor (tree val)
4318 {
4319   tree type = TREE_TYPE (val);
4320
4321   if (INTEGRAL_TYPE_P (type)
4322       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4323     return 0;
4324   else
4325     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4326 }
4327
4328 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4329    can, 0 if we can't.  Set the output range into the specified parameters.  */
4330
4331 bool
4332 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4333               tree high0, int in1_p, tree low1, tree high1)
4334 {
4335   int no_overlap;
4336   int subset;
4337   int temp;
4338   tree tem;
4339   int in_p;
4340   tree low, high;
4341   int lowequal = ((low0 == 0 && low1 == 0)
4342                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4343                                                 low0, 0, low1, 0)));
4344   int highequal = ((high0 == 0 && high1 == 0)
4345                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4346                                                  high0, 1, high1, 1)));
4347
4348   /* Make range 0 be the range that starts first, or ends last if they
4349      start at the same value.  Swap them if it isn't.  */
4350   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4351                                  low0, 0, low1, 0))
4352       || (lowequal
4353           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4354                                         high1, 1, high0, 1))))
4355     {
4356       temp = in0_p, in0_p = in1_p, in1_p = temp;
4357       tem = low0, low0 = low1, low1 = tem;
4358       tem = high0, high0 = high1, high1 = tem;
4359     }
4360
4361   /* Now flag two cases, whether the ranges are disjoint or whether the
4362      second range is totally subsumed in the first.  Note that the tests
4363      below are simplified by the ones above.  */
4364   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4365                                           high0, 1, low1, 0));
4366   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4367                                       high1, 1, high0, 1));
4368
4369   /* We now have four cases, depending on whether we are including or
4370      excluding the two ranges.  */
4371   if (in0_p && in1_p)
4372     {
4373       /* If they don't overlap, the result is false.  If the second range
4374          is a subset it is the result.  Otherwise, the range is from the start
4375          of the second to the end of the first.  */
4376       if (no_overlap)
4377         in_p = 0, low = high = 0;
4378       else if (subset)
4379         in_p = 1, low = low1, high = high1;
4380       else
4381         in_p = 1, low = low1, high = high0;
4382     }
4383
4384   else if (in0_p && ! in1_p)
4385     {
4386       /* If they don't overlap, the result is the first range.  If they are
4387          equal, the result is false.  If the second range is a subset of the
4388          first, and the ranges begin at the same place, we go from just after
4389          the end of the second range to the end of the first.  If the second
4390          range is not a subset of the first, or if it is a subset and both
4391          ranges end at the same place, the range starts at the start of the
4392          first range and ends just before the second range.
4393          Otherwise, we can't describe this as a single range.  */
4394       if (no_overlap)
4395         in_p = 1, low = low0, high = high0;
4396       else if (lowequal && highequal)
4397         in_p = 0, low = high = 0;
4398       else if (subset && lowequal)
4399         {
4400           low = range_successor (high1);
4401           high = high0;
4402           in_p = 1;
4403           if (low == 0)
4404             {
4405               /* We are in the weird situation where high0 > high1 but
4406                  high1 has no successor.  Punt.  */
4407               return 0;
4408             }
4409         }
4410       else if (! subset || highequal)
4411         {
4412           low = low0;
4413           high = range_predecessor (low1);
4414           in_p = 1;
4415           if (high == 0)
4416             {
4417               /* low0 < low1 but low1 has no predecessor.  Punt.  */
4418               return 0;
4419             }
4420         }
4421       else
4422         return 0;
4423     }
4424
4425   else if (! in0_p && in1_p)
4426     {
4427       /* If they don't overlap, the result is the second range.  If the second
4428          is a subset of the first, the result is false.  Otherwise,
4429          the range starts just after the first range and ends at the
4430          end of the second.  */
4431       if (no_overlap)
4432         in_p = 1, low = low1, high = high1;
4433       else if (subset || highequal)
4434         in_p = 0, low = high = 0;
4435       else
4436         {
4437           low = range_successor (high0);
4438           high = high1;
4439           in_p = 1;
4440           if (low == 0)
4441             {
4442               /* high1 > high0 but high0 has no successor.  Punt.  */
4443               return 0;
4444             }
4445         }
4446     }
4447
4448   else
4449     {
4450       /* The case where we are excluding both ranges.  Here the complex case
4451          is if they don't overlap.  In that case, the only time we have a
4452          range is if they are adjacent.  If the second is a subset of the
4453          first, the result is the first.  Otherwise, the range to exclude
4454          starts at the beginning of the first range and ends at the end of the
4455          second.  */
4456       if (no_overlap)
4457         {
4458           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4459                                          range_successor (high0),
4460                                          1, low1, 0)))
4461             in_p = 0, low = low0, high = high1;
4462           else
4463             {
4464               /* Canonicalize - [min, x] into - [-, x].  */
4465               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4466                 switch (TREE_CODE (TREE_TYPE (low0)))
4467                   {
4468                   case ENUMERAL_TYPE:
4469                     if (TYPE_PRECISION (TREE_TYPE (low0))
4470                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4471                       break;
4472                     /* FALLTHROUGH */
4473                   case INTEGER_TYPE:
4474                     if (tree_int_cst_equal (low0,
4475                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4476                       low0 = 0;
4477                     break;
4478                   case POINTER_TYPE:
4479                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4480                         && integer_zerop (low0))
4481                       low0 = 0;
4482                     break;
4483                   default:
4484                     break;
4485                   }
4486
4487               /* Canonicalize - [x, max] into - [x, -].  */
4488               if (high1 && TREE_CODE (high1) == INTEGER_CST)
4489                 switch (TREE_CODE (TREE_TYPE (high1)))
4490                   {
4491                   case ENUMERAL_TYPE:
4492                     if (TYPE_PRECISION (TREE_TYPE (high1))
4493                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4494                       break;
4495                     /* FALLTHROUGH */
4496                   case INTEGER_TYPE:
4497                     if (tree_int_cst_equal (high1,
4498                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
4499                       high1 = 0;
4500                     break;
4501                   case POINTER_TYPE:
4502                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
4503                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4504                                                        high1, 1,
4505                                                        integer_one_node, 1)))
4506                       high1 = 0;
4507                     break;
4508                   default:
4509                     break;
4510                   }
4511
4512               /* The ranges might be also adjacent between the maximum and
4513                  minimum values of the given type.  For
4514                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4515                  return + [x + 1, y - 1].  */
4516               if (low0 == 0 && high1 == 0)
4517                 {
4518                   low = range_successor (high0);
4519                   high = range_predecessor (low1);
4520                   if (low == 0 || high == 0)
4521                     return 0;
4522
4523                   in_p = 1;
4524                 }
4525               else
4526                 return 0;
4527             }
4528         }
4529       else if (subset)
4530         in_p = 0, low = low0, high = high0;
4531       else
4532         in_p = 0, low = low0, high = high1;
4533     }
4534
4535   *pin_p = in_p, *plow = low, *phigh = high;
4536   return 1;
4537 }
4538 \f
4539
4540 /* Subroutine of fold, looking inside expressions of the form
4541    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4542    of the COND_EXPR.  This function is being used also to optimize
4543    A op B ? C : A, by reversing the comparison first.
4544
4545    Return a folded expression whose code is not a COND_EXPR
4546    anymore, or NULL_TREE if no folding opportunity is found.  */
4547
4548 static tree
4549 fold_cond_expr_with_comparison (location_t loc, tree type,
4550                                 tree arg0, tree arg1, tree arg2)
4551 {
4552   enum tree_code comp_code = TREE_CODE (arg0);
4553   tree arg00 = TREE_OPERAND (arg0, 0);
4554   tree arg01 = TREE_OPERAND (arg0, 1);
4555   tree arg1_type = TREE_TYPE (arg1);
4556   tree tem;
4557
4558   STRIP_NOPS (arg1);
4559   STRIP_NOPS (arg2);
4560
4561   /* If we have A op 0 ? A : -A, consider applying the following
4562      transformations:
4563
4564      A == 0? A : -A    same as -A
4565      A != 0? A : -A    same as A
4566      A >= 0? A : -A    same as abs (A)
4567      A > 0?  A : -A    same as abs (A)
4568      A <= 0? A : -A    same as -abs (A)
4569      A < 0?  A : -A    same as -abs (A)
4570
4571      None of these transformations work for modes with signed
4572      zeros.  If A is +/-0, the first two transformations will
4573      change the sign of the result (from +0 to -0, or vice
4574      versa).  The last four will fix the sign of the result,
4575      even though the original expressions could be positive or
4576      negative, depending on the sign of A.
4577
4578      Note that all these transformations are correct if A is
4579      NaN, since the two alternatives (A and -A) are also NaNs.  */
4580   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4581       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4582           ? real_zerop (arg01)
4583           : integer_zerop (arg01))
4584       && ((TREE_CODE (arg2) == NEGATE_EXPR
4585            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4586              /* In the case that A is of the form X-Y, '-A' (arg2) may
4587                 have already been folded to Y-X, check for that. */
4588           || (TREE_CODE (arg1) == MINUS_EXPR
4589               && TREE_CODE (arg2) == MINUS_EXPR
4590               && operand_equal_p (TREE_OPERAND (arg1, 0),
4591                                   TREE_OPERAND (arg2, 1), 0)
4592               && operand_equal_p (TREE_OPERAND (arg1, 1),
4593                                   TREE_OPERAND (arg2, 0), 0))))
4594     switch (comp_code)
4595       {
4596       case EQ_EXPR:
4597       case UNEQ_EXPR:
4598         tem = fold_convert_loc (loc, arg1_type, arg1);
4599         return pedantic_non_lvalue_loc (loc,
4600                                     fold_convert_loc (loc, type,
4601                                                   negate_expr (tem)));
4602       case NE_EXPR:
4603       case LTGT_EXPR:
4604         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4605       case UNGE_EXPR:
4606       case UNGT_EXPR:
4607         if (flag_trapping_math)
4608           break;
4609         /* Fall through.  */
4610       case GE_EXPR:
4611       case GT_EXPR:
4612         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4613           arg1 = fold_convert_loc (loc, signed_type_for
4614                                (TREE_TYPE (arg1)), arg1);
4615         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4616         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4617       case UNLE_EXPR:
4618       case UNLT_EXPR:
4619         if (flag_trapping_math)
4620           break;
4621       case LE_EXPR:
4622       case LT_EXPR:
4623         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4624           arg1 = fold_convert_loc (loc, signed_type_for
4625                                (TREE_TYPE (arg1)), arg1);
4626         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4627         return negate_expr (fold_convert_loc (loc, type, tem));
4628       default:
4629         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4630         break;
4631       }
4632
4633   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
4634      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
4635      both transformations are correct when A is NaN: A != 0
4636      is then true, and A == 0 is false.  */
4637
4638   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4639       && integer_zerop (arg01) && integer_zerop (arg2))
4640     {
4641       if (comp_code == NE_EXPR)
4642         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4643       else if (comp_code == EQ_EXPR)
4644         return build_int_cst (type, 0);
4645     }
4646
4647   /* Try some transformations of A op B ? A : B.
4648
4649      A == B? A : B    same as B
4650      A != B? A : B    same as A
4651      A >= B? A : B    same as max (A, B)
4652      A > B?  A : B    same as max (B, A)
4653      A <= B? A : B    same as min (A, B)
4654      A < B?  A : B    same as min (B, A)
4655
4656      As above, these transformations don't work in the presence
4657      of signed zeros.  For example, if A and B are zeros of
4658      opposite sign, the first two transformations will change
4659      the sign of the result.  In the last four, the original
4660      expressions give different results for (A=+0, B=-0) and
4661      (A=-0, B=+0), but the transformed expressions do not.
4662
4663      The first two transformations are correct if either A or B
4664      is a NaN.  In the first transformation, the condition will
4665      be false, and B will indeed be chosen.  In the case of the
4666      second transformation, the condition A != B will be true,
4667      and A will be chosen.
4668
4669      The conversions to max() and min() are not correct if B is
4670      a number and A is not.  The conditions in the original
4671      expressions will be false, so all four give B.  The min()
4672      and max() versions would give a NaN instead.  */
4673   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4674       && operand_equal_for_comparison_p (arg01, arg2, arg00)
4675       /* Avoid these transformations if the COND_EXPR may be used
4676          as an lvalue in the C++ front-end.  PR c++/19199.  */
4677       && (in_gimple_form
4678           || (strcmp (lang_hooks.name, "GNU C++") != 0
4679               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4680           || ! maybe_lvalue_p (arg1)
4681           || ! maybe_lvalue_p (arg2)))
4682     {
4683       tree comp_op0 = arg00;
4684       tree comp_op1 = arg01;
4685       tree comp_type = TREE_TYPE (comp_op0);
4686
4687       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
4688       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4689         {
4690           comp_type = type;
4691           comp_op0 = arg1;
4692           comp_op1 = arg2;
4693         }
4694
4695       switch (comp_code)
4696         {
4697         case EQ_EXPR:
4698           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
4699         case NE_EXPR:
4700           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4701         case LE_EXPR:
4702         case LT_EXPR:
4703         case UNLE_EXPR:
4704         case UNLT_EXPR:
4705           /* In C++ a ?: expression can be an lvalue, so put the
4706              operand which will be used if they are equal first
4707              so that we can convert this back to the
4708              corresponding COND_EXPR.  */
4709           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4710             {
4711               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4712               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4713               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4714                     ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
4715                     : fold_build2_loc (loc, MIN_EXPR, comp_type,
4716                                    comp_op1, comp_op0);
4717               return pedantic_non_lvalue_loc (loc,
4718                                           fold_convert_loc (loc, type, tem));
4719             }
4720           break;
4721         case GE_EXPR:
4722         case GT_EXPR:
4723         case UNGE_EXPR:
4724         case UNGT_EXPR:
4725           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4726             {
4727               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4728               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4729               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4730                     ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
4731                     : fold_build2_loc (loc, MAX_EXPR, comp_type,
4732                                    comp_op1, comp_op0);
4733               return pedantic_non_lvalue_loc (loc,
4734                                           fold_convert_loc (loc, type, tem));
4735             }
4736           break;
4737         case UNEQ_EXPR:
4738           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4739             return pedantic_non_lvalue_loc (loc,
4740                                         fold_convert_loc (loc, type, arg2));
4741           break;
4742         case LTGT_EXPR:
4743           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4744             return pedantic_non_lvalue_loc (loc,
4745                                         fold_convert_loc (loc, type, arg1));
4746           break;
4747         default:
4748           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4749           break;
4750         }
4751     }
4752
4753   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4754      we might still be able to simplify this.  For example,
4755      if C1 is one less or one more than C2, this might have started
4756      out as a MIN or MAX and been transformed by this function.
4757      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
4758
4759   if (INTEGRAL_TYPE_P (type)
4760       && TREE_CODE (arg01) == INTEGER_CST
4761       && TREE_CODE (arg2) == INTEGER_CST)
4762     switch (comp_code)
4763       {
4764       case EQ_EXPR:
4765         if (TREE_CODE (arg1) == INTEGER_CST)
4766           break;
4767         /* We can replace A with C1 in this case.  */
4768         arg1 = fold_convert_loc (loc, type, arg01);
4769         return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
4770
4771       case LT_EXPR:
4772         /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
4773            MIN_EXPR, to preserve the signedness of the comparison.  */
4774         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4775                                OEP_ONLY_CONST)
4776             && operand_equal_p (arg01,
4777                                 const_binop (PLUS_EXPR, arg2,
4778                                              build_int_cst (type, 1)),
4779                                 OEP_ONLY_CONST))
4780           {
4781             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4782                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4783                                                      arg2));
4784             return pedantic_non_lvalue_loc (loc,
4785                                             fold_convert_loc (loc, type, tem));
4786           }
4787         break;
4788
4789       case LE_EXPR:
4790         /* If C1 is C2 - 1, this is min(A, C2), with the same care
4791            as above.  */
4792         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4793                                OEP_ONLY_CONST)
4794             && operand_equal_p (arg01,
4795                                 const_binop (MINUS_EXPR, arg2,
4796                                              build_int_cst (type, 1)),
4797                                 OEP_ONLY_CONST))
4798           {
4799             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4800                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4801                                                      arg2));
4802             return pedantic_non_lvalue_loc (loc,
4803                                             fold_convert_loc (loc, type, tem));
4804           }
4805         break;
4806
4807       case GT_EXPR:
4808         /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
4809            MAX_EXPR, to preserve the signedness of the comparison.  */
4810         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4811                                OEP_ONLY_CONST)
4812             && operand_equal_p (arg01,
4813                                 const_binop (MINUS_EXPR, arg2,
4814                                              build_int_cst (type, 1)),
4815                                 OEP_ONLY_CONST))
4816           {
4817             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4818                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4819                                                      arg2));
4820             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4821           }
4822         break;
4823
4824       case GE_EXPR:
4825         /* If C1 is C2 + 1, this is max(A, C2), with the same care as above.  */
4826         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4827                                OEP_ONLY_CONST)
4828             && operand_equal_p (arg01,
4829                                 const_binop (PLUS_EXPR, arg2,
4830                                              build_int_cst (type, 1)),
4831                                 OEP_ONLY_CONST))
4832           {
4833             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4834                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4835                                                      arg2));
4836             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4837           }
4838         break;
4839       case NE_EXPR:
4840         break;
4841       default:
4842         gcc_unreachable ();
4843       }
4844
4845   return NULL_TREE;
4846 }
4847
4848
4849 \f
4850 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4851 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
4852   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
4853                 false) >= 2)
4854 #endif
4855
4856 /* EXP is some logical combination of boolean tests.  See if we can
4857    merge it into some range test.  Return the new tree if so.  */
4858
4859 static tree
4860 fold_range_test (location_t loc, enum tree_code code, tree type,
4861                  tree op0, tree op1)
4862 {
4863   int or_op = (code == TRUTH_ORIF_EXPR
4864                || code == TRUTH_OR_EXPR);
4865   int in0_p, in1_p, in_p;
4866   tree low0, low1, low, high0, high1, high;
4867   bool strict_overflow_p = false;
4868   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
4869   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
4870   tree tem;
4871   const char * const warnmsg = G_("assuming signed overflow does not occur "
4872                                   "when simplifying range test");
4873
4874   /* If this is an OR operation, invert both sides; we will invert
4875      again at the end.  */
4876   if (or_op)
4877     in0_p = ! in0_p, in1_p = ! in1_p;
4878
4879   /* If both expressions are the same, if we can merge the ranges, and we
4880      can build the range test, return it or it inverted.  If one of the
4881      ranges is always true or always false, consider it to be the same
4882      expression as the other.  */
4883   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4884       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4885                        in1_p, low1, high1)
4886       && 0 != (tem = (build_range_check (UNKNOWN_LOCATION, type,
4887                                          lhs != 0 ? lhs
4888                                          : rhs != 0 ? rhs : integer_zero_node,
4889                                          in_p, low, high))))
4890     {
4891       if (strict_overflow_p)
4892         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
4893       return or_op ? invert_truthvalue_loc (loc, tem) : tem;
4894     }
4895
4896   /* On machines where the branch cost is expensive, if this is a
4897      short-circuited branch and the underlying object on both sides
4898      is the same, make a non-short-circuit operation.  */
4899   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4900            && lhs != 0 && rhs != 0
4901            && (code == TRUTH_ANDIF_EXPR
4902                || code == TRUTH_ORIF_EXPR)
4903            && operand_equal_p (lhs, rhs, 0))
4904     {
4905       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
4906          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4907          which cases we can't do this.  */
4908       if (simple_operand_p (lhs))
4909         {
4910           tem = build2 (code == TRUTH_ANDIF_EXPR
4911                         ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4912                         type, op0, op1);
4913           SET_EXPR_LOCATION (tem, loc);
4914           return tem;
4915         }
4916
4917       else if (lang_hooks.decls.global_bindings_p () == 0
4918                && ! CONTAINS_PLACEHOLDER_P (lhs))
4919         {
4920           tree common = save_expr (lhs);
4921
4922           if (0 != (lhs = build_range_check (loc, type, common,
4923                                              or_op ? ! in0_p : in0_p,
4924                                              low0, high0))
4925               && (0 != (rhs = build_range_check (loc, type, common,
4926                                                  or_op ? ! in1_p : in1_p,
4927                                                  low1, high1))))
4928             {
4929               if (strict_overflow_p)
4930                 fold_overflow_warning (warnmsg,
4931                                        WARN_STRICT_OVERFLOW_COMPARISON);
4932               tem = build2 (code == TRUTH_ANDIF_EXPR
4933                             ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4934                             type, lhs, rhs);
4935               SET_EXPR_LOCATION (tem, loc);
4936               return tem;
4937             }
4938         }
4939     }
4940
4941   return 0;
4942 }
4943 \f
4944 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4945    bit value.  Arrange things so the extra bits will be set to zero if and
4946    only if C is signed-extended to its full width.  If MASK is nonzero,
4947    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
4948
4949 static tree
4950 unextend (tree c, int p, int unsignedp, tree mask)
4951 {
4952   tree type = TREE_TYPE (c);
4953   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4954   tree temp;
4955
4956   if (p == modesize || unsignedp)
4957     return c;
4958
4959   /* We work by getting just the sign bit into the low-order bit, then
4960      into the high-order bit, then sign-extend.  We then XOR that value
4961      with C.  */
4962   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1));
4963   temp = const_binop (BIT_AND_EXPR, temp, size_int (1));
4964
4965   /* We must use a signed type in order to get an arithmetic right shift.
4966      However, we must also avoid introducing accidental overflows, so that
4967      a subsequent call to integer_zerop will work.  Hence we must
4968      do the type conversion here.  At this point, the constant is either
4969      zero or one, and the conversion to a signed type can never overflow.
4970      We could get an overflow if this conversion is done anywhere else.  */
4971   if (TYPE_UNSIGNED (type))
4972     temp = fold_convert (signed_type_for (type), temp);
4973
4974   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
4975   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
4976   if (mask != 0)
4977     temp = const_binop (BIT_AND_EXPR, temp,
4978                         fold_convert (TREE_TYPE (c), mask));
4979   /* If necessary, convert the type back to match the type of C.  */
4980   if (TYPE_UNSIGNED (type))
4981     temp = fold_convert (type, temp);
4982
4983   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
4984 }
4985 \f
4986 /* For an expression that has the form
4987      (A && B) || ~B
4988    or
4989      (A || B) && ~B,
4990    we can drop one of the inner expressions and simplify to
4991      A || ~B
4992    or
4993      A && ~B
4994    LOC is the location of the resulting expression.  OP is the inner 
4995    logical operation; the left-hand side in the examples above, while CMPOP
4996    is the right-hand side.  RHS_ONLY is used to prevent us from accidentally
4997    removing a condition that guards another, as in
4998      (A != NULL && A->...) || A == NULL
4999    which we must not transform.  If RHS_ONLY is true, only eliminate the
5000    right-most operand of the inner logical operation.  */
5001
5002 static tree
5003 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5004                                  bool rhs_only)
5005 {
5006   tree type = TREE_TYPE (cmpop);
5007   enum tree_code code = TREE_CODE (cmpop);
5008   enum tree_code truthop_code = TREE_CODE (op);
5009   tree lhs = TREE_OPERAND (op, 0);
5010   tree rhs = TREE_OPERAND (op, 1);
5011   tree orig_lhs = lhs, orig_rhs = rhs;
5012   enum tree_code rhs_code = TREE_CODE (rhs);
5013   enum tree_code lhs_code = TREE_CODE (lhs);
5014   enum tree_code inv_code;
5015
5016   if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5017     return NULL_TREE;
5018
5019   if (TREE_CODE_CLASS (code) != tcc_comparison)
5020     return NULL_TREE;
5021
5022   if (rhs_code == truthop_code)
5023     {
5024       tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5025       if (newrhs != NULL_TREE)
5026         {
5027           rhs = newrhs;
5028           rhs_code = TREE_CODE (rhs);
5029         }
5030     }
5031   if (lhs_code == truthop_code && !rhs_only)
5032     {
5033       tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5034       if (newlhs != NULL_TREE)
5035         {
5036           lhs = newlhs;
5037           lhs_code = TREE_CODE (lhs);
5038         }
5039     }
5040
5041   inv_code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
5042   if (inv_code == rhs_code
5043       && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5044       && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5045     return lhs;
5046   if (!rhs_only && inv_code == lhs_code
5047       && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5048       && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5049     return rhs;
5050   if (rhs != orig_rhs || lhs != orig_lhs)
5051     return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5052                             lhs, rhs);
5053   return NULL_TREE;
5054 }
5055
5056 /* Find ways of folding logical expressions of LHS and RHS:
5057    Try to merge two comparisons to the same innermost item.
5058    Look for range tests like "ch >= '0' && ch <= '9'".
5059    Look for combinations of simple terms on machines with expensive branches
5060    and evaluate the RHS unconditionally.
5061
5062    For example, if we have p->a == 2 && p->b == 4 and we can make an
5063    object large enough to span both A and B, we can do this with a comparison
5064    against the object ANDed with the a mask.
5065
5066    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5067    operations to do this with one comparison.
5068
5069    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5070    function and the one above.
5071
5072    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5073    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5074
5075    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5076    two operands.
5077
5078    We return the simplified tree or 0 if no optimization is possible.  */
5079
5080 static tree
5081 fold_truthop (location_t loc, enum tree_code code, tree truth_type,
5082               tree lhs, tree rhs)
5083 {
5084   /* If this is the "or" of two comparisons, we can do something if
5085      the comparisons are NE_EXPR.  If this is the "and", we can do something
5086      if the comparisons are EQ_EXPR.  I.e.,
5087         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5088
5089      WANTED_CODE is this operation code.  For single bit fields, we can
5090      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5091      comparison for one-bit fields.  */
5092
5093   enum tree_code wanted_code;
5094   enum tree_code lcode, rcode;
5095   tree ll_arg, lr_arg, rl_arg, rr_arg;
5096   tree ll_inner, lr_inner, rl_inner, rr_inner;
5097   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5098   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5099   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5100   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5101   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5102   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5103   enum machine_mode lnmode, rnmode;
5104   tree ll_mask, lr_mask, rl_mask, rr_mask;
5105   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5106   tree l_const, r_const;
5107   tree lntype, rntype, result;
5108   HOST_WIDE_INT first_bit, end_bit;
5109   int volatilep;
5110   tree orig_lhs = lhs, orig_rhs = rhs;
5111   enum tree_code orig_code = code;
5112
5113   /* Start by getting the comparison codes.  Fail if anything is volatile.
5114      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5115      it were surrounded with a NE_EXPR.  */
5116
5117   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5118     return 0;
5119
5120   lcode = TREE_CODE (lhs);
5121   rcode = TREE_CODE (rhs);
5122
5123   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5124     {
5125       lhs = build2 (NE_EXPR, truth_type, lhs,
5126                     build_int_cst (TREE_TYPE (lhs), 0));
5127       lcode = NE_EXPR;
5128     }
5129
5130   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5131     {
5132       rhs = build2 (NE_EXPR, truth_type, rhs,
5133                     build_int_cst (TREE_TYPE (rhs), 0));
5134       rcode = NE_EXPR;
5135     }
5136
5137   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5138       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5139     return 0;
5140
5141   ll_arg = TREE_OPERAND (lhs, 0);
5142   lr_arg = TREE_OPERAND (lhs, 1);
5143   rl_arg = TREE_OPERAND (rhs, 0);
5144   rr_arg = TREE_OPERAND (rhs, 1);
5145
5146   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5147   if (simple_operand_p (ll_arg)
5148       && simple_operand_p (lr_arg))
5149     {
5150       tree result;
5151       if (operand_equal_p (ll_arg, rl_arg, 0)
5152           && operand_equal_p (lr_arg, rr_arg, 0))
5153         {
5154           result = combine_comparisons (loc, code, lcode, rcode,
5155                                         truth_type, ll_arg, lr_arg);
5156           if (result)
5157             return result;
5158         }
5159       else if (operand_equal_p (ll_arg, rr_arg, 0)
5160                && operand_equal_p (lr_arg, rl_arg, 0))
5161         {
5162           result = combine_comparisons (loc, code, lcode,
5163                                         swap_tree_comparison (rcode),
5164                                         truth_type, ll_arg, lr_arg);
5165           if (result)
5166             return result;
5167         }
5168     }
5169
5170   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5171           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5172
5173   /* If the RHS can be evaluated unconditionally and its operands are
5174      simple, it wins to evaluate the RHS unconditionally on machines
5175      with expensive branches.  In this case, this isn't a comparison
5176      that can be merged.  Avoid doing this if the RHS is a floating-point
5177      comparison since those can trap.  */
5178
5179   if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5180                    false) >= 2
5181       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5182       && simple_operand_p (rl_arg)
5183       && simple_operand_p (rr_arg))
5184     {
5185       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5186       if (code == TRUTH_OR_EXPR
5187           && lcode == NE_EXPR && integer_zerop (lr_arg)
5188           && rcode == NE_EXPR && integer_zerop (rr_arg)
5189           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5190           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5191         {
5192           result = build2 (NE_EXPR, truth_type,
5193                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5194                                    ll_arg, rl_arg),
5195                            build_int_cst (TREE_TYPE (ll_arg), 0));
5196           goto fold_truthop_exit;
5197         }
5198
5199       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5200       if (code == TRUTH_AND_EXPR
5201           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5202           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5203           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5204           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5205         {
5206           result = build2 (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           goto fold_truthop_exit;
5211         }
5212
5213       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5214         {
5215           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5216             {
5217               result = build2 (code, truth_type, lhs, rhs);
5218               goto fold_truthop_exit;
5219             }
5220           return NULL_TREE;
5221         }
5222     }
5223
5224   /* See if the comparisons can be merged.  Then get all the parameters for
5225      each side.  */
5226
5227   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5228       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5229     return 0;
5230
5231   volatilep = 0;
5232   ll_inner = decode_field_reference (loc, ll_arg,
5233                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5234                                      &ll_unsignedp, &volatilep, &ll_mask,
5235                                      &ll_and_mask);
5236   lr_inner = decode_field_reference (loc, lr_arg,
5237                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5238                                      &lr_unsignedp, &volatilep, &lr_mask,
5239                                      &lr_and_mask);
5240   rl_inner = decode_field_reference (loc, rl_arg,
5241                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5242                                      &rl_unsignedp, &volatilep, &rl_mask,
5243                                      &rl_and_mask);
5244   rr_inner = decode_field_reference (loc, rr_arg,
5245                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5246                                      &rr_unsignedp, &volatilep, &rr_mask,
5247                                      &rr_and_mask);
5248
5249   /* It must be true that the inner operation on the lhs of each
5250      comparison must be the same if we are to be able to do anything.
5251      Then see if we have constants.  If not, the same must be true for
5252      the rhs's.  */
5253   if (volatilep || ll_inner == 0 || rl_inner == 0
5254       || ! operand_equal_p (ll_inner, rl_inner, 0))
5255     return 0;
5256
5257   if (TREE_CODE (lr_arg) == INTEGER_CST
5258       && TREE_CODE (rr_arg) == INTEGER_CST)
5259     l_const = lr_arg, r_const = rr_arg;
5260   else if (lr_inner == 0 || rr_inner == 0
5261            || ! operand_equal_p (lr_inner, rr_inner, 0))
5262     return 0;
5263   else
5264     l_const = r_const = 0;
5265
5266   /* If either comparison code is not correct for our logical operation,
5267      fail.  However, we can convert a one-bit comparison against zero into
5268      the opposite comparison against that bit being set in the field.  */
5269
5270   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5271   if (lcode != wanted_code)
5272     {
5273       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5274         {
5275           /* Make the left operand unsigned, since we are only interested
5276              in the value of one bit.  Otherwise we are doing the wrong
5277              thing below.  */
5278           ll_unsignedp = 1;
5279           l_const = ll_mask;
5280         }
5281       else
5282         return 0;
5283     }
5284
5285   /* This is analogous to the code for l_const above.  */
5286   if (rcode != wanted_code)
5287     {
5288       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5289         {
5290           rl_unsignedp = 1;
5291           r_const = rl_mask;
5292         }
5293       else
5294         return 0;
5295     }
5296
5297   /* See if we can find a mode that contains both fields being compared on
5298      the left.  If we can't, fail.  Otherwise, update all constants and masks
5299      to be relative to a field of that size.  */
5300   first_bit = MIN (ll_bitpos, rl_bitpos);
5301   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5302   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5303                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5304                           volatilep);
5305   if (lnmode == VOIDmode)
5306     return 0;
5307
5308   lnbitsize = GET_MODE_BITSIZE (lnmode);
5309   lnbitpos = first_bit & ~ (lnbitsize - 1);
5310   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5311   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5312
5313   if (BYTES_BIG_ENDIAN)
5314     {
5315       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5316       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5317     }
5318
5319   ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5320                          size_int (xll_bitpos));
5321   rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5322                          size_int (xrl_bitpos));
5323
5324   if (l_const)
5325     {
5326       l_const = fold_convert_loc (loc, lntype, l_const);
5327       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5328       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5329       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5330                                         fold_build1_loc (loc, BIT_NOT_EXPR,
5331                                                      lntype, ll_mask))))
5332         {
5333           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5334
5335           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5336         }
5337     }
5338   if (r_const)
5339     {
5340       r_const = fold_convert_loc (loc, lntype, r_const);
5341       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5342       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5343       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5344                                         fold_build1_loc (loc, BIT_NOT_EXPR,
5345                                                      lntype, rl_mask))))
5346         {
5347           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5348
5349           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5350         }
5351     }
5352
5353   /* If the right sides are not constant, do the same for it.  Also,
5354      disallow this optimization if a size or signedness mismatch occurs
5355      between the left and right sides.  */
5356   if (l_const == 0)
5357     {
5358       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5359           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5360           /* Make sure the two fields on the right
5361              correspond to the left without being swapped.  */
5362           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5363         return 0;
5364
5365       first_bit = MIN (lr_bitpos, rr_bitpos);
5366       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5367       rnmode = get_best_mode (end_bit - first_bit, first_bit,
5368                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5369                               volatilep);
5370       if (rnmode == VOIDmode)
5371         return 0;
5372
5373       rnbitsize = GET_MODE_BITSIZE (rnmode);
5374       rnbitpos = first_bit & ~ (rnbitsize - 1);
5375       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5376       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5377
5378       if (BYTES_BIG_ENDIAN)
5379         {
5380           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5381           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5382         }
5383
5384       lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5385                                                             rntype, lr_mask),
5386                              size_int (xlr_bitpos));
5387       rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5388                                                             rntype, rr_mask),
5389                              size_int (xrr_bitpos));
5390
5391       /* Make a mask that corresponds to both fields being compared.
5392          Do this for both items being compared.  If the operands are the
5393          same size and the bits being compared are in the same position
5394          then we can do this by masking both and comparing the masked
5395          results.  */
5396       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5397       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5398       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5399         {
5400           lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5401                                     ll_unsignedp || rl_unsignedp);
5402           if (! all_ones_mask_p (ll_mask, lnbitsize))
5403             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5404
5405           rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5406                                     lr_unsignedp || rr_unsignedp);
5407           if (! all_ones_mask_p (lr_mask, rnbitsize))
5408             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5409
5410           result = build2 (wanted_code, truth_type, lhs, rhs);
5411           goto fold_truthop_exit;
5412         }
5413
5414       /* There is still another way we can do something:  If both pairs of
5415          fields being compared are adjacent, we may be able to make a wider
5416          field containing them both.
5417
5418          Note that we still must mask the lhs/rhs expressions.  Furthermore,
5419          the mask must be shifted to account for the shift done by
5420          make_bit_field_ref.  */
5421       if ((ll_bitsize + ll_bitpos == rl_bitpos
5422            && lr_bitsize + lr_bitpos == rr_bitpos)
5423           || (ll_bitpos == rl_bitpos + rl_bitsize
5424               && lr_bitpos == rr_bitpos + rr_bitsize))
5425         {
5426           tree type;
5427
5428           lhs = make_bit_field_ref (loc, ll_inner, lntype,
5429                                     ll_bitsize + rl_bitsize,
5430                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5431           rhs = make_bit_field_ref (loc, lr_inner, rntype,
5432                                     lr_bitsize + rr_bitsize,
5433                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5434
5435           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5436                                  size_int (MIN (xll_bitpos, xrl_bitpos)));
5437           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5438                                  size_int (MIN (xlr_bitpos, xrr_bitpos)));
5439
5440           /* Convert to the smaller type before masking out unwanted bits.  */
5441           type = lntype;
5442           if (lntype != rntype)
5443             {
5444               if (lnbitsize > rnbitsize)
5445                 {
5446                   lhs = fold_convert_loc (loc, rntype, lhs);
5447                   ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5448                   type = rntype;
5449                 }
5450               else if (lnbitsize < rnbitsize)
5451                 {
5452                   rhs = fold_convert_loc (loc, lntype, rhs);
5453                   lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5454                   type = lntype;
5455                 }
5456             }
5457
5458           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5459             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5460
5461           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5462             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5463
5464           result = build2 (wanted_code, truth_type, lhs, rhs);
5465           goto fold_truthop_exit;
5466         }
5467
5468       return 0;
5469     }
5470
5471   /* Handle the case of comparisons with constants.  If there is something in
5472      common between the masks, those bits of the constants must be the same.
5473      If not, the condition is always false.  Test for this to avoid generating
5474      incorrect code below.  */
5475   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5476   if (! integer_zerop (result)
5477       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5478                            const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5479     {
5480       if (wanted_code == NE_EXPR)
5481         {
5482           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5483           return constant_boolean_node (true, truth_type);
5484         }
5485       else
5486         {
5487           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5488           return constant_boolean_node (false, truth_type);
5489         }
5490     }
5491
5492   /* Construct the expression we will return.  First get the component
5493      reference we will make.  Unless the mask is all ones the width of
5494      that field, perform the mask operation.  Then compare with the
5495      merged constant.  */
5496   result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5497                                ll_unsignedp || rl_unsignedp);
5498
5499   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5500   if (! all_ones_mask_p (ll_mask, lnbitsize))
5501     {
5502       result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5503       SET_EXPR_LOCATION (result, loc);
5504     }
5505
5506   result = build2 (wanted_code, truth_type, result,
5507                    const_binop (BIT_IOR_EXPR, l_const, r_const));
5508
5509  fold_truthop_exit:
5510   SET_EXPR_LOCATION (result, loc);
5511   return result;
5512 }
5513 \f
5514 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5515    constant.  */
5516
5517 static tree
5518 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5519                             tree op0, tree op1)
5520 {
5521   tree arg0 = op0;
5522   enum tree_code op_code;
5523   tree comp_const;
5524   tree minmax_const;
5525   int consts_equal, consts_lt;
5526   tree inner;
5527
5528   STRIP_SIGN_NOPS (arg0);
5529
5530   op_code = TREE_CODE (arg0);
5531   minmax_const = TREE_OPERAND (arg0, 1);
5532   comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5533   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5534   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5535   inner = TREE_OPERAND (arg0, 0);
5536
5537   /* If something does not permit us to optimize, return the original tree.  */
5538   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5539       || TREE_CODE (comp_const) != INTEGER_CST
5540       || TREE_OVERFLOW (comp_const)
5541       || TREE_CODE (minmax_const) != INTEGER_CST
5542       || TREE_OVERFLOW (minmax_const))
5543     return NULL_TREE;
5544
5545   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5546      and GT_EXPR, doing the rest with recursive calls using logical
5547      simplifications.  */
5548   switch (code)
5549     {
5550     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5551       {
5552         tree tem
5553           = optimize_minmax_comparison (loc,
5554                                         invert_tree_comparison (code, false),
5555                                         type, op0, op1);
5556         if (tem)
5557           return invert_truthvalue_loc (loc, tem);
5558         return NULL_TREE;
5559       }
5560
5561     case GE_EXPR:
5562       return
5563         fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5564                      optimize_minmax_comparison
5565                      (loc, EQ_EXPR, type, arg0, comp_const),
5566                      optimize_minmax_comparison
5567                      (loc, GT_EXPR, type, arg0, comp_const));
5568
5569     case EQ_EXPR:
5570       if (op_code == MAX_EXPR && consts_equal)
5571         /* MAX (X, 0) == 0  ->  X <= 0  */
5572         return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5573
5574       else if (op_code == MAX_EXPR && consts_lt)
5575         /* MAX (X, 0) == 5  ->  X == 5   */
5576         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5577
5578       else if (op_code == MAX_EXPR)
5579         /* MAX (X, 0) == -1  ->  false  */
5580         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5581
5582       else if (consts_equal)
5583         /* MIN (X, 0) == 0  ->  X >= 0  */
5584         return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5585
5586       else if (consts_lt)
5587         /* MIN (X, 0) == 5  ->  false  */
5588         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5589
5590       else
5591         /* MIN (X, 0) == -1  ->  X == -1  */
5592         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5593
5594     case GT_EXPR:
5595       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5596         /* MAX (X, 0) > 0  ->  X > 0
5597            MAX (X, 0) > 5  ->  X > 5  */
5598         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5599
5600       else if (op_code == MAX_EXPR)
5601         /* MAX (X, 0) > -1  ->  true  */
5602         return omit_one_operand_loc (loc, type, integer_one_node, inner);
5603
5604       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5605         /* MIN (X, 0) > 0  ->  false
5606            MIN (X, 0) > 5  ->  false  */
5607         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5608
5609       else
5610         /* MIN (X, 0) > -1  ->  X > -1  */
5611         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5612
5613     default:
5614       return NULL_TREE;
5615     }
5616 }
5617 \f
5618 /* T is an integer expression that is being multiplied, divided, or taken a
5619    modulus (CODE says which and what kind of divide or modulus) by a
5620    constant C.  See if we can eliminate that operation by folding it with
5621    other operations already in T.  WIDE_TYPE, if non-null, is a type that
5622    should be used for the computation if wider than our type.
5623
5624    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5625    (X * 2) + (Y * 4).  We must, however, be assured that either the original
5626    expression would not overflow or that overflow is undefined for the type
5627    in the language in question.
5628
5629    If we return a non-null expression, it is an equivalent form of the
5630    original computation, but need not be in the original type.
5631
5632    We set *STRICT_OVERFLOW_P to true if the return values depends on
5633    signed overflow being undefined.  Otherwise we do not change
5634    *STRICT_OVERFLOW_P.  */
5635
5636 static tree
5637 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5638                 bool *strict_overflow_p)
5639 {
5640   /* To avoid exponential search depth, refuse to allow recursion past
5641      three levels.  Beyond that (1) it's highly unlikely that we'll find
5642      something interesting and (2) we've probably processed it before
5643      when we built the inner expression.  */
5644
5645   static int depth;
5646   tree ret;
5647
5648   if (depth > 3)
5649     return NULL;
5650
5651   depth++;
5652   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5653   depth--;
5654
5655   return ret;
5656 }
5657
5658 static tree
5659 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5660                   bool *strict_overflow_p)
5661 {
5662   tree type = TREE_TYPE (t);
5663   enum tree_code tcode = TREE_CODE (t);
5664   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5665                                    > GET_MODE_SIZE (TYPE_MODE (type)))
5666                 ? wide_type : type);
5667   tree t1, t2;
5668   int same_p = tcode == code;
5669   tree op0 = NULL_TREE, op1 = NULL_TREE;
5670   bool sub_strict_overflow_p;
5671
5672   /* Don't deal with constants of zero here; they confuse the code below.  */
5673   if (integer_zerop (c))
5674     return NULL_TREE;
5675
5676   if (TREE_CODE_CLASS (tcode) == tcc_unary)
5677     op0 = TREE_OPERAND (t, 0);
5678
5679   if (TREE_CODE_CLASS (tcode) == tcc_binary)
5680     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5681
5682   /* Note that we need not handle conditional operations here since fold
5683      already handles those cases.  So just do arithmetic here.  */
5684   switch (tcode)
5685     {
5686     case INTEGER_CST:
5687       /* For a constant, we can always simplify if we are a multiply
5688          or (for divide and modulus) if it is a multiple of our constant.  */
5689       if (code == MULT_EXPR
5690           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c)))
5691         return const_binop (code, fold_convert (ctype, t),
5692                             fold_convert (ctype, c));
5693       break;
5694
5695     CASE_CONVERT: case NON_LVALUE_EXPR:
5696       /* If op0 is an expression ...  */
5697       if ((COMPARISON_CLASS_P (op0)
5698            || UNARY_CLASS_P (op0)
5699            || BINARY_CLASS_P (op0)
5700            || VL_EXP_CLASS_P (op0)
5701            || EXPRESSION_CLASS_P (op0))
5702           /* ... and has wrapping overflow, and its type is smaller
5703              than ctype, then we cannot pass through as widening.  */
5704           && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
5705                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5706                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5707                && (TYPE_PRECISION (ctype)
5708                    > TYPE_PRECISION (TREE_TYPE (op0))))
5709               /* ... or this is a truncation (t is narrower than op0),
5710                  then we cannot pass through this narrowing.  */
5711               || (TYPE_PRECISION (type)
5712                   < TYPE_PRECISION (TREE_TYPE (op0)))
5713               /* ... or signedness changes for division or modulus,
5714                  then we cannot pass through this conversion.  */
5715               || (code != MULT_EXPR
5716                   && (TYPE_UNSIGNED (ctype)
5717                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
5718               /* ... or has undefined overflow while the converted to
5719                  type has not, we cannot do the operation in the inner type
5720                  as that would introduce undefined overflow.  */
5721               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5722                   && !TYPE_OVERFLOW_UNDEFINED (type))))
5723         break;
5724
5725       /* Pass the constant down and see if we can make a simplification.  If
5726          we can, replace this expression with the inner simplification for
5727          possible later conversion to our or some other type.  */
5728       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5729           && TREE_CODE (t2) == INTEGER_CST
5730           && !TREE_OVERFLOW (t2)
5731           && (0 != (t1 = extract_muldiv (op0, t2, code,
5732                                          code == MULT_EXPR
5733                                          ? ctype : NULL_TREE,
5734                                          strict_overflow_p))))
5735         return t1;
5736       break;
5737
5738     case ABS_EXPR:
5739       /* If widening the type changes it from signed to unsigned, then we
5740          must avoid building ABS_EXPR itself as unsigned.  */
5741       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5742         {
5743           tree cstype = (*signed_type_for) (ctype);
5744           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5745               != 0)
5746             {
5747               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5748               return fold_convert (ctype, t1);
5749             }
5750           break;
5751         }
5752       /* If the constant is negative, we cannot simplify this.  */
5753       if (tree_int_cst_sgn (c) == -1)
5754         break;
5755       /* FALLTHROUGH */
5756     case NEGATE_EXPR:
5757       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5758           != 0)
5759         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5760       break;
5761
5762     case MIN_EXPR:  case MAX_EXPR:
5763       /* If widening the type changes the signedness, then we can't perform
5764          this optimization as that changes the result.  */
5765       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5766         break;
5767
5768       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
5769       sub_strict_overflow_p = false;
5770       if ((t1 = extract_muldiv (op0, c, code, wide_type,
5771                                 &sub_strict_overflow_p)) != 0
5772           && (t2 = extract_muldiv (op1, c, code, wide_type,
5773                                    &sub_strict_overflow_p)) != 0)
5774         {
5775           if (tree_int_cst_sgn (c) < 0)
5776             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5777           if (sub_strict_overflow_p)
5778             *strict_overflow_p = true;
5779           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5780                               fold_convert (ctype, t2));
5781         }
5782       break;
5783
5784     case LSHIFT_EXPR:  case RSHIFT_EXPR:
5785       /* If the second operand is constant, this is a multiplication
5786          or floor division, by a power of two, so we can treat it that
5787          way unless the multiplier or divisor overflows.  Signed
5788          left-shift overflow is implementation-defined rather than
5789          undefined in C90, so do not convert signed left shift into
5790          multiplication.  */
5791       if (TREE_CODE (op1) == INTEGER_CST
5792           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5793           /* const_binop may not detect overflow correctly,
5794              so check for it explicitly here.  */
5795           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5796           && TREE_INT_CST_HIGH (op1) == 0
5797           && 0 != (t1 = fold_convert (ctype,
5798                                       const_binop (LSHIFT_EXPR,
5799                                                    size_one_node,
5800                                                    op1)))
5801           && !TREE_OVERFLOW (t1))
5802         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5803                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
5804                                        ctype,
5805                                        fold_convert (ctype, op0),
5806                                        t1),
5807                                c, code, wide_type, strict_overflow_p);
5808       break;
5809
5810     case PLUS_EXPR:  case MINUS_EXPR:
5811       /* See if we can eliminate the operation on both sides.  If we can, we
5812          can return a new PLUS or MINUS.  If we can't, the only remaining
5813          cases where we can do anything are if the second operand is a
5814          constant.  */
5815       sub_strict_overflow_p = false;
5816       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5817       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5818       if (t1 != 0 && t2 != 0
5819           && (code == MULT_EXPR
5820               /* If not multiplication, we can only do this if both operands
5821                  are divisible by c.  */
5822               || (multiple_of_p (ctype, op0, c)
5823                   && multiple_of_p (ctype, op1, c))))
5824         {
5825           if (sub_strict_overflow_p)
5826             *strict_overflow_p = true;
5827           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5828                               fold_convert (ctype, t2));
5829         }
5830
5831       /* If this was a subtraction, negate OP1 and set it to be an addition.
5832          This simplifies the logic below.  */
5833       if (tcode == MINUS_EXPR)
5834         {
5835           tcode = PLUS_EXPR, op1 = negate_expr (op1);
5836           /* If OP1 was not easily negatable, the constant may be OP0.  */
5837           if (TREE_CODE (op0) == INTEGER_CST)
5838             {
5839               tree tem = op0;
5840               op0 = op1;
5841               op1 = tem;
5842               tem = t1;
5843               t1 = t2;
5844               t2 = tem;
5845             }
5846         }
5847
5848       if (TREE_CODE (op1) != INTEGER_CST)
5849         break;
5850
5851       /* If either OP1 or C are negative, this optimization is not safe for
5852          some of the division and remainder types while for others we need
5853          to change the code.  */
5854       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5855         {
5856           if (code == CEIL_DIV_EXPR)
5857             code = FLOOR_DIV_EXPR;
5858           else if (code == FLOOR_DIV_EXPR)
5859             code = CEIL_DIV_EXPR;
5860           else if (code != MULT_EXPR
5861                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5862             break;
5863         }
5864
5865       /* If it's a multiply or a division/modulus operation of a multiple
5866          of our constant, do the operation and verify it doesn't overflow.  */
5867       if (code == MULT_EXPR
5868           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5869         {
5870           op1 = const_binop (code, fold_convert (ctype, op1),
5871                              fold_convert (ctype, c));
5872           /* We allow the constant to overflow with wrapping semantics.  */
5873           if (op1 == 0
5874               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5875             break;
5876         }
5877       else
5878         break;
5879
5880       /* If we have an unsigned type is not a sizetype, we cannot widen
5881          the operation since it will change the result if the original
5882          computation overflowed.  */
5883       if (TYPE_UNSIGNED (ctype)
5884           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5885           && ctype != type)
5886         break;
5887
5888       /* If we were able to eliminate our operation from the first side,
5889          apply our operation to the second side and reform the PLUS.  */
5890       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5891         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5892
5893       /* The last case is if we are a multiply.  In that case, we can
5894          apply the distributive law to commute the multiply and addition
5895          if the multiplication of the constants doesn't overflow.  */
5896       if (code == MULT_EXPR)
5897         return fold_build2 (tcode, ctype,
5898                             fold_build2 (code, ctype,
5899                                          fold_convert (ctype, op0),
5900                                          fold_convert (ctype, c)),
5901                             op1);
5902
5903       break;
5904
5905     case MULT_EXPR:
5906       /* We have a special case here if we are doing something like
5907          (C * 8) % 4 since we know that's zero.  */
5908       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5909            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5910           /* If the multiplication can overflow we cannot optimize this.
5911              ???  Until we can properly mark individual operations as
5912              not overflowing we need to treat sizetype special here as
5913              stor-layout relies on this opimization to make
5914              DECL_FIELD_BIT_OFFSET always a constant.  */
5915           && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
5916               || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
5917                   && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
5918           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5919           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5920         {
5921           *strict_overflow_p = true;
5922           return omit_one_operand (type, integer_zero_node, op0);
5923         }
5924
5925       /* ... fall through ...  */
5926
5927     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
5928     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
5929       /* If we can extract our operation from the LHS, do so and return a
5930          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
5931          do something only if the second operand is a constant.  */
5932       if (same_p
5933           && (t1 = extract_muldiv (op0, c, code, wide_type,
5934                                    strict_overflow_p)) != 0)
5935         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5936                             fold_convert (ctype, op1));
5937       else if (tcode == MULT_EXPR && code == MULT_EXPR
5938                && (t1 = extract_muldiv (op1, c, code, wide_type,
5939                                         strict_overflow_p)) != 0)
5940         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5941                             fold_convert (ctype, t1));
5942       else if (TREE_CODE (op1) != INTEGER_CST)
5943         return 0;
5944
5945       /* If these are the same operation types, we can associate them
5946          assuming no overflow.  */
5947       if (tcode == code
5948           && 0 != (t1 = int_const_binop (MULT_EXPR,
5949                                          fold_convert (ctype, op1),
5950                                          fold_convert (ctype, c), 1))
5951           && 0 != (t1 = force_fit_type_double (ctype, tree_to_double_int (t1),
5952                                                (TYPE_UNSIGNED (ctype)
5953                                                 && tcode != MULT_EXPR) ? -1 : 1,
5954                                                TREE_OVERFLOW (t1)))
5955           && !TREE_OVERFLOW (t1))
5956         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
5957
5958       /* If these operations "cancel" each other, we have the main
5959          optimizations of this pass, which occur when either constant is a
5960          multiple of the other, in which case we replace this with either an
5961          operation or CODE or TCODE.
5962
5963          If we have an unsigned type that is not a sizetype, we cannot do
5964          this since it will change the result if the original computation
5965          overflowed.  */
5966       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
5967            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5968           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5969               || (tcode == MULT_EXPR
5970                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5971                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
5972                   && code != MULT_EXPR)))
5973         {
5974           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5975             {
5976               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5977                 *strict_overflow_p = true;
5978               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5979                                   fold_convert (ctype,
5980                                                 const_binop (TRUNC_DIV_EXPR,
5981                                                              op1, c)));
5982             }
5983           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1)))
5984             {
5985               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5986                 *strict_overflow_p = true;
5987               return fold_build2 (code, ctype, fold_convert (ctype, op0),
5988                                   fold_convert (ctype,
5989                                                 const_binop (TRUNC_DIV_EXPR,
5990                                                              c, op1)));
5991             }
5992         }
5993       break;
5994
5995     default:
5996       break;
5997     }
5998
5999   return 0;
6000 }
6001 \f
6002 /* Return a node which has the indicated constant VALUE (either 0 or
6003    1), and is of the indicated TYPE.  */
6004
6005 tree
6006 constant_boolean_node (int value, tree type)
6007 {
6008   if (type == integer_type_node)
6009     return value ? integer_one_node : integer_zero_node;
6010   else if (type == boolean_type_node)
6011     return value ? boolean_true_node : boolean_false_node;
6012   else
6013     return build_int_cst (type, value);
6014 }
6015
6016
6017 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6018    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
6019    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6020    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
6021    COND is the first argument to CODE; otherwise (as in the example
6022    given here), it is the second argument.  TYPE is the type of the
6023    original expression.  Return NULL_TREE if no simplification is
6024    possible.  */
6025
6026 static tree
6027 fold_binary_op_with_conditional_arg (location_t loc,
6028                                      enum tree_code code,
6029                                      tree type, tree op0, tree op1,
6030                                      tree cond, tree arg, int cond_first_p)
6031 {
6032   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6033   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6034   tree test, true_value, false_value;
6035   tree lhs = NULL_TREE;
6036   tree rhs = NULL_TREE;
6037
6038   if (TREE_CODE (cond) == COND_EXPR)
6039     {
6040       test = TREE_OPERAND (cond, 0);
6041       true_value = TREE_OPERAND (cond, 1);
6042       false_value = TREE_OPERAND (cond, 2);
6043       /* If this operand throws an expression, then it does not make
6044          sense to try to perform a logical or arithmetic operation
6045          involving it.  */
6046       if (VOID_TYPE_P (TREE_TYPE (true_value)))
6047         lhs = true_value;
6048       if (VOID_TYPE_P (TREE_TYPE (false_value)))
6049         rhs = false_value;
6050     }
6051   else
6052     {
6053       tree testtype = TREE_TYPE (cond);
6054       test = cond;
6055       true_value = constant_boolean_node (true, testtype);
6056       false_value = constant_boolean_node (false, testtype);
6057     }
6058
6059   /* This transformation is only worthwhile if we don't have to wrap ARG
6060      in a SAVE_EXPR and the operation can be simplified on at least one
6061      of the branches once its pushed inside the COND_EXPR.  */
6062   if (!TREE_CONSTANT (arg)
6063       && (TREE_SIDE_EFFECTS (arg)
6064           || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6065     return NULL_TREE;
6066
6067   arg = fold_convert_loc (loc, arg_type, arg);
6068   if (lhs == 0)
6069     {
6070       true_value = fold_convert_loc (loc, cond_type, true_value);
6071       if (cond_first_p)
6072         lhs = fold_build2_loc (loc, code, type, true_value, arg);
6073       else
6074         lhs = fold_build2_loc (loc, code, type, arg, true_value);
6075     }
6076   if (rhs == 0)
6077     {
6078       false_value = fold_convert_loc (loc, cond_type, false_value);
6079       if (cond_first_p)
6080         rhs = fold_build2_loc (loc, code, type, false_value, arg);
6081       else
6082         rhs = fold_build2_loc (loc, code, type, arg, false_value);
6083     }
6084
6085   /* Check that we have simplified at least one of the branches.  */
6086   if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6087     return NULL_TREE;
6088
6089   return fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs);
6090 }
6091
6092 \f
6093 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6094
6095    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6096    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6097    ADDEND is the same as X.
6098
6099    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6100    and finite.  The problematic cases are when X is zero, and its mode
6101    has signed zeros.  In the case of rounding towards -infinity,
6102    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6103    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6104
6105 bool
6106 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6107 {
6108   if (!real_zerop (addend))
6109     return false;
6110
6111   /* Don't allow the fold with -fsignaling-nans.  */
6112   if (HONOR_SNANS (TYPE_MODE (type)))
6113     return false;
6114
6115   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6116   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6117     return true;
6118
6119   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6120   if (TREE_CODE (addend) == REAL_CST
6121       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6122     negate = !negate;
6123
6124   /* The mode has signed zeros, and we have to honor their sign.
6125      In this situation, there is only one case we can return true for.
6126      X - 0 is the same as X unless rounding towards -infinity is
6127      supported.  */
6128   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6129 }
6130
6131 /* Subroutine of fold() that checks comparisons of built-in math
6132    functions against real constants.
6133
6134    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6135    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6136    is the type of the result and ARG0 and ARG1 are the operands of the
6137    comparison.  ARG1 must be a TREE_REAL_CST.
6138
6139    The function returns the constant folded tree if a simplification
6140    can be made, and NULL_TREE otherwise.  */
6141
6142 static tree
6143 fold_mathfn_compare (location_t loc,
6144                      enum built_in_function fcode, enum tree_code code,
6145                      tree type, tree arg0, tree arg1)
6146 {
6147   REAL_VALUE_TYPE c;
6148
6149   if (BUILTIN_SQRT_P (fcode))
6150     {
6151       tree arg = CALL_EXPR_ARG (arg0, 0);
6152       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6153
6154       c = TREE_REAL_CST (arg1);
6155       if (REAL_VALUE_NEGATIVE (c))
6156         {
6157           /* sqrt(x) < y is always false, if y is negative.  */
6158           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6159             return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6160
6161           /* sqrt(x) > y is always true, if y is negative and we
6162              don't care about NaNs, i.e. negative values of x.  */
6163           if (code == NE_EXPR || !HONOR_NANS (mode))
6164             return omit_one_operand_loc (loc, type, integer_one_node, arg);
6165
6166           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6167           return fold_build2_loc (loc, GE_EXPR, type, arg,
6168                               build_real (TREE_TYPE (arg), dconst0));
6169         }
6170       else if (code == GT_EXPR || code == GE_EXPR)
6171         {
6172           REAL_VALUE_TYPE c2;
6173
6174           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6175           real_convert (&c2, mode, &c2);
6176
6177           if (REAL_VALUE_ISINF (c2))
6178             {
6179               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6180               if (HONOR_INFINITIES (mode))
6181                 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6182                                     build_real (TREE_TYPE (arg), c2));
6183
6184               /* sqrt(x) > y is always false, when y is very large
6185                  and we don't care about infinities.  */
6186               return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6187             }
6188
6189           /* sqrt(x) > c is the same as x > c*c.  */
6190           return fold_build2_loc (loc, code, type, arg,
6191                               build_real (TREE_TYPE (arg), c2));
6192         }
6193       else if (code == LT_EXPR || code == LE_EXPR)
6194         {
6195           REAL_VALUE_TYPE c2;
6196
6197           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6198           real_convert (&c2, mode, &c2);
6199
6200           if (REAL_VALUE_ISINF (c2))
6201             {
6202               /* sqrt(x) < y is always true, when y is a very large
6203                  value and we don't care about NaNs or Infinities.  */
6204               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6205                 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6206
6207               /* sqrt(x) < y is x != +Inf when y is very large and we
6208                  don't care about NaNs.  */
6209               if (! HONOR_NANS (mode))
6210                 return fold_build2_loc (loc, NE_EXPR, type, arg,
6211                                     build_real (TREE_TYPE (arg), c2));
6212
6213               /* sqrt(x) < y is x >= 0 when y is very large and we
6214                  don't care about Infinities.  */
6215               if (! HONOR_INFINITIES (mode))
6216                 return fold_build2_loc (loc, GE_EXPR, type, arg,
6217                                     build_real (TREE_TYPE (arg), dconst0));
6218
6219               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6220               if (lang_hooks.decls.global_bindings_p () != 0
6221                   || CONTAINS_PLACEHOLDER_P (arg))
6222                 return NULL_TREE;
6223
6224               arg = save_expr (arg);
6225               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6226                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6227                                                build_real (TREE_TYPE (arg),
6228                                                            dconst0)),
6229                                   fold_build2_loc (loc, NE_EXPR, type, arg,
6230                                                build_real (TREE_TYPE (arg),
6231                                                            c2)));
6232             }
6233
6234           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6235           if (! HONOR_NANS (mode))
6236             return fold_build2_loc (loc, code, type, arg,
6237                                 build_real (TREE_TYPE (arg), c2));
6238
6239           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6240           if (lang_hooks.decls.global_bindings_p () == 0
6241               && ! CONTAINS_PLACEHOLDER_P (arg))
6242             {
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
6255   return NULL_TREE;
6256 }
6257
6258 /* Subroutine of fold() that optimizes comparisons against Infinities,
6259    either +Inf or -Inf.
6260
6261    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6262    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6263    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6264
6265    The function returns the constant folded tree if a simplification
6266    can be made, and NULL_TREE otherwise.  */
6267
6268 static tree
6269 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6270                   tree arg0, tree arg1)
6271 {
6272   enum machine_mode mode;
6273   REAL_VALUE_TYPE max;
6274   tree temp;
6275   bool neg;
6276
6277   mode = TYPE_MODE (TREE_TYPE (arg0));
6278
6279   /* For negative infinity swap the sense of the comparison.  */
6280   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6281   if (neg)
6282     code = swap_tree_comparison (code);
6283
6284   switch (code)
6285     {
6286     case GT_EXPR:
6287       /* x > +Inf is always false, if with ignore sNANs.  */
6288       if (HONOR_SNANS (mode))
6289         return NULL_TREE;
6290       return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6291
6292     case LE_EXPR:
6293       /* x <= +Inf is always true, if we don't case about NaNs.  */
6294       if (! HONOR_NANS (mode))
6295         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6296
6297       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6298       if (lang_hooks.decls.global_bindings_p () == 0
6299           && ! CONTAINS_PLACEHOLDER_P (arg0))
6300         {
6301           arg0 = save_expr (arg0);
6302           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6303         }
6304       break;
6305
6306     case EQ_EXPR:
6307     case GE_EXPR:
6308       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6309       real_maxval (&max, neg, mode);
6310       return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6311                           arg0, build_real (TREE_TYPE (arg0), max));
6312
6313     case LT_EXPR:
6314       /* x < +Inf is always equal to x <= DBL_MAX.  */
6315       real_maxval (&max, neg, mode);
6316       return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6317                           arg0, build_real (TREE_TYPE (arg0), max));
6318
6319     case NE_EXPR:
6320       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6321       real_maxval (&max, neg, mode);
6322       if (! HONOR_NANS (mode))
6323         return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6324                             arg0, build_real (TREE_TYPE (arg0), max));
6325
6326       temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6327                           arg0, build_real (TREE_TYPE (arg0), max));
6328       return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
6329
6330     default:
6331       break;
6332     }
6333
6334   return NULL_TREE;
6335 }
6336
6337 /* Subroutine of fold() that optimizes comparisons of a division by
6338    a nonzero integer constant against an integer constant, i.e.
6339    X/C1 op C2.
6340
6341    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6342    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6343    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6344
6345    The function returns the constant folded tree if a simplification
6346    can be made, and NULL_TREE otherwise.  */
6347
6348 static tree
6349 fold_div_compare (location_t loc,
6350                   enum tree_code code, tree type, tree arg0, tree arg1)
6351 {
6352   tree prod, tmp, hi, lo;
6353   tree arg00 = TREE_OPERAND (arg0, 0);
6354   tree arg01 = TREE_OPERAND (arg0, 1);
6355   double_int val;
6356   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6357   bool neg_overflow;
6358   int overflow;
6359
6360   /* We have to do this the hard way to detect unsigned overflow.
6361      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
6362   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6363                                    TREE_INT_CST_HIGH (arg01),
6364                                    TREE_INT_CST_LOW (arg1),
6365                                    TREE_INT_CST_HIGH (arg1),
6366                                    &val.low, &val.high, unsigned_p);
6367   prod = force_fit_type_double (TREE_TYPE (arg00), val, -1, overflow);
6368   neg_overflow = false;
6369
6370   if (unsigned_p)
6371     {
6372       tmp = int_const_binop (MINUS_EXPR, arg01,
6373                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6374       lo = prod;
6375
6376       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
6377       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6378                                        TREE_INT_CST_HIGH (prod),
6379                                        TREE_INT_CST_LOW (tmp),
6380                                        TREE_INT_CST_HIGH (tmp),
6381                                        &val.low, &val.high, unsigned_p);
6382       hi = force_fit_type_double (TREE_TYPE (arg00), val,
6383                                   -1, overflow | TREE_OVERFLOW (prod));
6384     }
6385   else if (tree_int_cst_sgn (arg01) >= 0)
6386     {
6387       tmp = int_const_binop (MINUS_EXPR, arg01,
6388                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6389       switch (tree_int_cst_sgn (arg1))
6390         {
6391         case -1:
6392           neg_overflow = true;
6393           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6394           hi = prod;
6395           break;
6396
6397         case  0:
6398           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6399           hi = tmp;
6400           break;
6401
6402         case  1:
6403           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6404           lo = prod;
6405           break;
6406
6407         default:
6408           gcc_unreachable ();
6409         }
6410     }
6411   else
6412     {
6413       /* A negative divisor reverses the relational operators.  */
6414       code = swap_tree_comparison (code);
6415
6416       tmp = int_const_binop (PLUS_EXPR, arg01,
6417                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6418       switch (tree_int_cst_sgn (arg1))
6419         {
6420         case -1:
6421           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6422           lo = prod;
6423           break;
6424
6425         case  0:
6426           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6427           lo = tmp;
6428           break;
6429
6430         case  1:
6431           neg_overflow = true;
6432           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6433           hi = prod;
6434           break;
6435
6436         default:
6437           gcc_unreachable ();
6438         }
6439     }
6440
6441   switch (code)
6442     {
6443     case EQ_EXPR:
6444       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6445         return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6446       if (TREE_OVERFLOW (hi))
6447         return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6448       if (TREE_OVERFLOW (lo))
6449         return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6450       return build_range_check (loc, type, arg00, 1, lo, hi);
6451
6452     case NE_EXPR:
6453       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6454         return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6455       if (TREE_OVERFLOW (hi))
6456         return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6457       if (TREE_OVERFLOW (lo))
6458         return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6459       return build_range_check (loc, type, arg00, 0, lo, hi);
6460
6461     case LT_EXPR:
6462       if (TREE_OVERFLOW (lo))
6463         {
6464           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6465           return omit_one_operand_loc (loc, type, tmp, arg00);
6466         }
6467       return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6468
6469     case LE_EXPR:
6470       if (TREE_OVERFLOW (hi))
6471         {
6472           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6473           return omit_one_operand_loc (loc, type, tmp, arg00);
6474         }
6475       return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6476
6477     case GT_EXPR:
6478       if (TREE_OVERFLOW (hi))
6479         {
6480           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6481           return omit_one_operand_loc (loc, type, tmp, arg00);
6482         }
6483       return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6484
6485     case GE_EXPR:
6486       if (TREE_OVERFLOW (lo))
6487         {
6488           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6489           return omit_one_operand_loc (loc, type, tmp, arg00);
6490         }
6491       return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6492
6493     default:
6494       break;
6495     }
6496
6497   return NULL_TREE;
6498 }
6499
6500
6501 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6502    equality/inequality test, then return a simplified form of the test
6503    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6504    result type.  */
6505
6506 static tree
6507 fold_single_bit_test_into_sign_test (location_t loc,
6508                                      enum tree_code code, tree arg0, tree arg1,
6509                                      tree result_type)
6510 {
6511   /* If this is testing a single bit, we can optimize the test.  */
6512   if ((code == NE_EXPR || code == EQ_EXPR)
6513       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6514       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6515     {
6516       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6517          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6518       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6519
6520       if (arg00 != NULL_TREE
6521           /* This is only a win if casting to a signed type is cheap,
6522              i.e. when arg00's type is not a partial mode.  */
6523           && TYPE_PRECISION (TREE_TYPE (arg00))
6524              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6525         {
6526           tree stype = signed_type_for (TREE_TYPE (arg00));
6527           return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6528                               result_type,
6529                               fold_convert_loc (loc, stype, arg00),
6530                               build_int_cst (stype, 0));
6531         }
6532     }
6533
6534   return NULL_TREE;
6535 }
6536
6537 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6538    equality/inequality test, then return a simplified form of
6539    the test using shifts and logical operations.  Otherwise return
6540    NULL.  TYPE is the desired result type.  */
6541
6542 tree
6543 fold_single_bit_test (location_t loc, enum tree_code code,
6544                       tree arg0, tree arg1, tree result_type)
6545 {
6546   /* If this is testing a single bit, we can optimize the test.  */
6547   if ((code == NE_EXPR || code == EQ_EXPR)
6548       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6549       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6550     {
6551       tree inner = TREE_OPERAND (arg0, 0);
6552       tree type = TREE_TYPE (arg0);
6553       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6554       enum machine_mode operand_mode = TYPE_MODE (type);
6555       int ops_unsigned;
6556       tree signed_type, unsigned_type, intermediate_type;
6557       tree tem, one;
6558
6559       /* First, see if we can fold the single bit test into a sign-bit
6560          test.  */
6561       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6562                                                  result_type);
6563       if (tem)
6564         return tem;
6565
6566       /* Otherwise we have (A & C) != 0 where C is a single bit,
6567          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6568          Similarly for (A & C) == 0.  */
6569
6570       /* If INNER is a right shift of a constant and it plus BITNUM does
6571          not overflow, adjust BITNUM and INNER.  */
6572       if (TREE_CODE (inner) == RSHIFT_EXPR
6573           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6574           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6575           && bitnum < TYPE_PRECISION (type)
6576           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6577                                    bitnum - TYPE_PRECISION (type)))
6578         {
6579           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6580           inner = TREE_OPERAND (inner, 0);
6581         }
6582
6583       /* If we are going to be able to omit the AND below, we must do our
6584          operations as unsigned.  If we must use the AND, we have a choice.
6585          Normally unsigned is faster, but for some machines signed is.  */
6586 #ifdef LOAD_EXTEND_OP
6587       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6588                       && !flag_syntax_only) ? 0 : 1;
6589 #else
6590       ops_unsigned = 1;
6591 #endif
6592
6593       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6594       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6595       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6596       inner = fold_convert_loc (loc, intermediate_type, inner);
6597
6598       if (bitnum != 0)
6599         inner = build2 (RSHIFT_EXPR, intermediate_type,
6600                         inner, size_int (bitnum));
6601
6602       one = build_int_cst (intermediate_type, 1);
6603
6604       if (code == EQ_EXPR)
6605         inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6606
6607       /* Put the AND last so it can combine with more things.  */
6608       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6609
6610       /* Make sure to return the proper type.  */
6611       inner = fold_convert_loc (loc, result_type, inner);
6612
6613       return inner;
6614     }
6615   return NULL_TREE;
6616 }
6617
6618 /* Check whether we are allowed to reorder operands arg0 and arg1,
6619    such that the evaluation of arg1 occurs before arg0.  */
6620
6621 static bool
6622 reorder_operands_p (const_tree arg0, const_tree arg1)
6623 {
6624   if (! flag_evaluation_order)
6625       return true;
6626   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6627     return true;
6628   return ! TREE_SIDE_EFFECTS (arg0)
6629          && ! TREE_SIDE_EFFECTS (arg1);
6630 }
6631
6632 /* Test whether it is preferable two swap two operands, ARG0 and
6633    ARG1, for example because ARG0 is an integer constant and ARG1
6634    isn't.  If REORDER is true, only recommend swapping if we can
6635    evaluate the operands in reverse order.  */
6636
6637 bool
6638 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6639 {
6640   STRIP_SIGN_NOPS (arg0);
6641   STRIP_SIGN_NOPS (arg1);
6642
6643   if (TREE_CODE (arg1) == INTEGER_CST)
6644     return 0;
6645   if (TREE_CODE (arg0) == INTEGER_CST)
6646     return 1;
6647
6648   if (TREE_CODE (arg1) == REAL_CST)
6649     return 0;
6650   if (TREE_CODE (arg0) == REAL_CST)
6651     return 1;
6652
6653   if (TREE_CODE (arg1) == FIXED_CST)
6654     return 0;
6655   if (TREE_CODE (arg0) == FIXED_CST)
6656     return 1;
6657
6658   if (TREE_CODE (arg1) == COMPLEX_CST)
6659     return 0;
6660   if (TREE_CODE (arg0) == COMPLEX_CST)
6661     return 1;
6662
6663   if (TREE_CONSTANT (arg1))
6664     return 0;
6665   if (TREE_CONSTANT (arg0))
6666     return 1;
6667
6668   if (optimize_function_for_size_p (cfun))
6669     return 0;
6670
6671   if (reorder && flag_evaluation_order
6672       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6673     return 0;
6674
6675   /* It is preferable to swap two SSA_NAME to ensure a canonical form
6676      for commutative and comparison operators.  Ensuring a canonical
6677      form allows the optimizers to find additional redundancies without
6678      having to explicitly check for both orderings.  */
6679   if (TREE_CODE (arg0) == SSA_NAME
6680       && TREE_CODE (arg1) == SSA_NAME
6681       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6682     return 1;
6683
6684   /* Put SSA_NAMEs last.  */
6685   if (TREE_CODE (arg1) == SSA_NAME)
6686     return 0;
6687   if (TREE_CODE (arg0) == SSA_NAME)
6688     return 1;
6689
6690   /* Put variables last.  */
6691   if (DECL_P (arg1))
6692     return 0;
6693   if (DECL_P (arg0))
6694     return 1;
6695
6696   return 0;
6697 }
6698
6699 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6700    ARG0 is extended to a wider type.  */
6701
6702 static tree
6703 fold_widened_comparison (location_t loc, enum tree_code code,
6704                          tree type, tree arg0, tree arg1)
6705 {
6706   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6707   tree arg1_unw;
6708   tree shorter_type, outer_type;
6709   tree min, max;
6710   bool above, below;
6711
6712   if (arg0_unw == arg0)
6713     return NULL_TREE;
6714   shorter_type = TREE_TYPE (arg0_unw);
6715
6716 #ifdef HAVE_canonicalize_funcptr_for_compare
6717   /* Disable this optimization if we're casting a function pointer
6718      type on targets that require function pointer canonicalization.  */
6719   if (HAVE_canonicalize_funcptr_for_compare
6720       && TREE_CODE (shorter_type) == POINTER_TYPE
6721       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6722     return NULL_TREE;
6723 #endif
6724
6725   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6726     return NULL_TREE;
6727
6728   arg1_unw = get_unwidened (arg1, NULL_TREE);
6729
6730   /* If possible, express the comparison in the shorter mode.  */
6731   if ((code == EQ_EXPR || code == NE_EXPR
6732        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6733       && (TREE_TYPE (arg1_unw) == shorter_type
6734           || ((TYPE_PRECISION (shorter_type)
6735                >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6736               && (TYPE_UNSIGNED (shorter_type)
6737                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6738           || (TREE_CODE (arg1_unw) == INTEGER_CST
6739               && (TREE_CODE (shorter_type) == INTEGER_TYPE
6740                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6741               && int_fits_type_p (arg1_unw, shorter_type))))
6742     return fold_build2_loc (loc, code, type, arg0_unw,
6743                         fold_convert_loc (loc, shorter_type, arg1_unw));
6744
6745   if (TREE_CODE (arg1_unw) != INTEGER_CST
6746       || TREE_CODE (shorter_type) != INTEGER_TYPE
6747       || !int_fits_type_p (arg1_unw, shorter_type))
6748     return NULL_TREE;
6749
6750   /* If we are comparing with the integer that does not fit into the range
6751      of the shorter type, the result is known.  */
6752   outer_type = TREE_TYPE (arg1_unw);
6753   min = lower_bound_in_type (outer_type, shorter_type);
6754   max = upper_bound_in_type (outer_type, shorter_type);
6755
6756   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6757                                                    max, arg1_unw));
6758   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6759                                                    arg1_unw, min));
6760
6761   switch (code)
6762     {
6763     case EQ_EXPR:
6764       if (above || below)
6765         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6766       break;
6767
6768     case NE_EXPR:
6769       if (above || below)
6770         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6771       break;
6772
6773     case LT_EXPR:
6774     case LE_EXPR:
6775       if (above)
6776         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6777       else if (below)
6778         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6779
6780     case GT_EXPR:
6781     case GE_EXPR:
6782       if (above)
6783         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6784       else if (below)
6785         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6786
6787     default:
6788       break;
6789     }
6790
6791   return NULL_TREE;
6792 }
6793
6794 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6795    ARG0 just the signedness is changed.  */
6796
6797 static tree
6798 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
6799                               tree arg0, tree arg1)
6800 {
6801   tree arg0_inner;
6802   tree inner_type, outer_type;
6803
6804   if (!CONVERT_EXPR_P (arg0))
6805     return NULL_TREE;
6806
6807   outer_type = TREE_TYPE (arg0);
6808   arg0_inner = TREE_OPERAND (arg0, 0);
6809   inner_type = TREE_TYPE (arg0_inner);
6810
6811 #ifdef HAVE_canonicalize_funcptr_for_compare
6812   /* Disable this optimization if we're casting a function pointer
6813      type on targets that require function pointer canonicalization.  */
6814   if (HAVE_canonicalize_funcptr_for_compare
6815       && TREE_CODE (inner_type) == POINTER_TYPE
6816       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6817     return NULL_TREE;
6818 #endif
6819
6820   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6821     return NULL_TREE;
6822
6823   if (TREE_CODE (arg1) != INTEGER_CST
6824       && !(CONVERT_EXPR_P (arg1)
6825            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6826     return NULL_TREE;
6827
6828   if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6829        || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
6830       && code != NE_EXPR
6831       && code != EQ_EXPR)
6832     return NULL_TREE;
6833
6834   if (TREE_CODE (arg1) == INTEGER_CST)
6835     arg1 = force_fit_type_double (inner_type, tree_to_double_int (arg1),
6836                                   0, TREE_OVERFLOW (arg1));
6837   else
6838     arg1 = fold_convert_loc (loc, inner_type, arg1);
6839
6840   return fold_build2_loc (loc, code, type, arg0_inner, arg1);
6841 }
6842
6843 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6844    step of the array.  Reconstructs s and delta in the case of s *
6845    delta being an integer constant (and thus already folded).  ADDR is
6846    the address. MULT is the multiplicative expression.  If the
6847    function succeeds, the new address expression is returned.
6848    Otherwise NULL_TREE is returned.  LOC is the location of the
6849    resulting expression.  */
6850
6851 static tree
6852 try_move_mult_to_index (location_t loc, tree addr, tree op1)
6853 {
6854   tree s, delta, step;
6855   tree ref = TREE_OPERAND (addr, 0), pref;
6856   tree ret, pos;
6857   tree itype;
6858   bool mdim = false;
6859
6860   /*  Strip the nops that might be added when converting op1 to sizetype. */
6861   STRIP_NOPS (op1);
6862
6863   /* Canonicalize op1 into a possibly non-constant delta
6864      and an INTEGER_CST s.  */
6865   if (TREE_CODE (op1) == MULT_EXPR)
6866     {
6867       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6868
6869       STRIP_NOPS (arg0);
6870       STRIP_NOPS (arg1);
6871
6872       if (TREE_CODE (arg0) == INTEGER_CST)
6873         {
6874           s = arg0;
6875           delta = arg1;
6876         }
6877       else if (TREE_CODE (arg1) == INTEGER_CST)
6878         {
6879           s = arg1;
6880           delta = arg0;
6881         }
6882       else
6883         return NULL_TREE;
6884     }
6885   else if (TREE_CODE (op1) == INTEGER_CST)
6886     {
6887       delta = op1;
6888       s = NULL_TREE;
6889     }
6890   else
6891     {
6892       /* Simulate we are delta * 1.  */
6893       delta = op1;
6894       s = integer_one_node;
6895     }
6896
6897   for (;; ref = TREE_OPERAND (ref, 0))
6898     {
6899       if (TREE_CODE (ref) == ARRAY_REF)
6900         {
6901           tree domain;
6902
6903           /* Remember if this was a multi-dimensional array.  */
6904           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6905             mdim = true;
6906
6907           domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6908           if (! domain)
6909             continue;
6910           itype = TREE_TYPE (domain);
6911
6912           step = array_ref_element_size (ref);
6913           if (TREE_CODE (step) != INTEGER_CST)
6914             continue;
6915
6916           if (s)
6917             {
6918               if (! tree_int_cst_equal (step, s))
6919                 continue;
6920             }
6921           else
6922             {
6923               /* Try if delta is a multiple of step.  */
6924               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6925               if (! tmp)
6926                 continue;
6927               delta = tmp;
6928             }
6929
6930           /* Only fold here if we can verify we do not overflow one
6931              dimension of a multi-dimensional array.  */
6932           if (mdim)
6933             {
6934               tree tmp;
6935
6936               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
6937                   || !TYPE_MAX_VALUE (domain)
6938                   || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
6939                 continue;
6940
6941               tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
6942                                      fold_convert_loc (loc, itype,
6943                                                        TREE_OPERAND (ref, 1)),
6944                                      fold_convert_loc (loc, itype, delta));
6945               if (!tmp
6946                   || TREE_CODE (tmp) != INTEGER_CST
6947                   || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
6948                 continue;
6949             }
6950
6951           break;
6952         }
6953       else
6954         mdim = false;
6955
6956       if (!handled_component_p (ref))
6957         return NULL_TREE;
6958     }
6959
6960   /* We found the suitable array reference.  So copy everything up to it,
6961      and replace the index.  */
6962
6963   pref = TREE_OPERAND (addr, 0);
6964   ret = copy_node (pref);
6965   SET_EXPR_LOCATION (ret, loc);
6966   pos = ret;
6967
6968   while (pref != ref)
6969     {
6970       pref = TREE_OPERAND (pref, 0);
6971       TREE_OPERAND (pos, 0) = copy_node (pref);
6972       pos = TREE_OPERAND (pos, 0);
6973     }
6974
6975   TREE_OPERAND (pos, 1) = fold_build2_loc (loc, PLUS_EXPR, itype,
6976                                        fold_convert_loc (loc, itype,
6977                                                          TREE_OPERAND (pos, 1)),
6978                                        fold_convert_loc (loc, itype, delta));
6979
6980   return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
6981 }
6982
6983
6984 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
6985    means A >= Y && A != MAX, but in this case we know that
6986    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
6987
6988 static tree
6989 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6990 {
6991   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6992
6993   if (TREE_CODE (bound) == LT_EXPR)
6994     a = TREE_OPERAND (bound, 0);
6995   else if (TREE_CODE (bound) == GT_EXPR)
6996     a = TREE_OPERAND (bound, 1);
6997   else
6998     return NULL_TREE;
6999
7000   typea = TREE_TYPE (a);
7001   if (!INTEGRAL_TYPE_P (typea)
7002       && !POINTER_TYPE_P (typea))
7003     return NULL_TREE;
7004
7005   if (TREE_CODE (ineq) == LT_EXPR)
7006     {
7007       a1 = TREE_OPERAND (ineq, 1);
7008       y = TREE_OPERAND (ineq, 0);
7009     }
7010   else if (TREE_CODE (ineq) == GT_EXPR)
7011     {
7012       a1 = TREE_OPERAND (ineq, 0);
7013       y = TREE_OPERAND (ineq, 1);
7014     }
7015   else
7016     return NULL_TREE;
7017
7018   if (TREE_TYPE (a1) != typea)
7019     return NULL_TREE;
7020
7021   if (POINTER_TYPE_P (typea))
7022     {
7023       /* Convert the pointer types into integer before taking the difference.  */
7024       tree ta = fold_convert_loc (loc, ssizetype, a);
7025       tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7026       diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7027     }
7028   else
7029     diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7030
7031   if (!diff || !integer_onep (diff))
7032    return NULL_TREE;
7033
7034   return fold_build2_loc (loc, GE_EXPR, type, a, y);
7035 }
7036
7037 /* Fold a sum or difference of at least one multiplication.
7038    Returns the folded tree or NULL if no simplification could be made.  */
7039
7040 static tree
7041 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7042                           tree arg0, tree arg1)
7043 {
7044   tree arg00, arg01, arg10, arg11;
7045   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7046
7047   /* (A * C) +- (B * C) -> (A+-B) * C.
7048      (A * C) +- A -> A * (C+-1).
7049      We are most concerned about the case where C is a constant,
7050      but other combinations show up during loop reduction.  Since
7051      it is not difficult, try all four possibilities.  */
7052
7053   if (TREE_CODE (arg0) == MULT_EXPR)
7054     {
7055       arg00 = TREE_OPERAND (arg0, 0);
7056       arg01 = TREE_OPERAND (arg0, 1);
7057     }
7058   else if (TREE_CODE (arg0) == INTEGER_CST)
7059     {
7060       arg00 = build_one_cst (type);
7061       arg01 = arg0;
7062     }
7063   else
7064     {
7065       /* We cannot generate constant 1 for fract.  */
7066       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7067         return NULL_TREE;
7068       arg00 = arg0;
7069       arg01 = build_one_cst (type);
7070     }
7071   if (TREE_CODE (arg1) == MULT_EXPR)
7072     {
7073       arg10 = TREE_OPERAND (arg1, 0);
7074       arg11 = TREE_OPERAND (arg1, 1);
7075     }
7076   else if (TREE_CODE (arg1) == INTEGER_CST)
7077     {
7078       arg10 = build_one_cst (type);
7079       /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7080          the purpose of this canonicalization.  */
7081       if (TREE_INT_CST_HIGH (arg1) == -1
7082           && negate_expr_p (arg1)
7083           && code == PLUS_EXPR)
7084         {
7085           arg11 = negate_expr (arg1);
7086           code = MINUS_EXPR;
7087         }
7088       else
7089         arg11 = arg1;
7090     }
7091   else
7092     {
7093       /* We cannot generate constant 1 for fract.  */
7094       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7095         return NULL_TREE;
7096       arg10 = arg1;
7097       arg11 = build_one_cst (type);
7098     }
7099   same = NULL_TREE;
7100
7101   if (operand_equal_p (arg01, arg11, 0))
7102     same = arg01, alt0 = arg00, alt1 = arg10;
7103   else if (operand_equal_p (arg00, arg10, 0))
7104     same = arg00, alt0 = arg01, alt1 = arg11;
7105   else if (operand_equal_p (arg00, arg11, 0))
7106     same = arg00, alt0 = arg01, alt1 = arg10;
7107   else if (operand_equal_p (arg01, arg10, 0))
7108     same = arg01, alt0 = arg00, alt1 = arg11;
7109
7110   /* No identical multiplicands; see if we can find a common
7111      power-of-two factor in non-power-of-two multiplies.  This
7112      can help in multi-dimensional array access.  */
7113   else if (host_integerp (arg01, 0)
7114            && host_integerp (arg11, 0))
7115     {
7116       HOST_WIDE_INT int01, int11, tmp;
7117       bool swap = false;
7118       tree maybe_same;
7119       int01 = TREE_INT_CST_LOW (arg01);
7120       int11 = TREE_INT_CST_LOW (arg11);
7121
7122       /* Move min of absolute values to int11.  */
7123       if ((int01 >= 0 ? int01 : -int01)
7124           < (int11 >= 0 ? int11 : -int11))
7125         {
7126           tmp = int01, int01 = int11, int11 = tmp;
7127           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7128           maybe_same = arg01;
7129           swap = true;
7130         }
7131       else
7132         maybe_same = arg11;
7133
7134       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
7135           /* The remainder should not be a constant, otherwise we
7136              end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7137              increased the number of multiplications necessary.  */
7138           && TREE_CODE (arg10) != INTEGER_CST)
7139         {
7140           alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7141                               build_int_cst (TREE_TYPE (arg00),
7142                                              int01 / int11));
7143           alt1 = arg10;
7144           same = maybe_same;
7145           if (swap)
7146             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7147         }
7148     }
7149
7150   if (same)
7151     return fold_build2_loc (loc, MULT_EXPR, type,
7152                         fold_build2_loc (loc, code, type,
7153                                      fold_convert_loc (loc, type, alt0),
7154                                      fold_convert_loc (loc, type, alt1)),
7155                         fold_convert_loc (loc, type, same));
7156
7157   return NULL_TREE;
7158 }
7159
7160 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7161    specified by EXPR into the buffer PTR of length LEN bytes.
7162    Return the number of bytes placed in the buffer, or zero
7163    upon failure.  */
7164
7165 static int
7166 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7167 {
7168   tree type = TREE_TYPE (expr);
7169   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7170   int byte, offset, word, words;
7171   unsigned char value;
7172
7173   if (total_bytes > len)
7174     return 0;
7175   words = total_bytes / UNITS_PER_WORD;
7176
7177   for (byte = 0; byte < total_bytes; byte++)
7178     {
7179       int bitpos = byte * BITS_PER_UNIT;
7180       if (bitpos < HOST_BITS_PER_WIDE_INT)
7181         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7182       else
7183         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7184                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7185
7186       if (total_bytes > UNITS_PER_WORD)
7187         {
7188           word = byte / UNITS_PER_WORD;
7189           if (WORDS_BIG_ENDIAN)
7190             word = (words - 1) - word;
7191           offset = word * UNITS_PER_WORD;
7192           if (BYTES_BIG_ENDIAN)
7193             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7194           else
7195             offset += byte % UNITS_PER_WORD;
7196         }
7197       else
7198         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7199       ptr[offset] = value;
7200     }
7201   return total_bytes;
7202 }
7203
7204
7205 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7206    specified by EXPR into the buffer PTR of length LEN bytes.
7207    Return the number of bytes placed in the buffer, or zero
7208    upon failure.  */
7209
7210 static int
7211 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7212 {
7213   tree type = TREE_TYPE (expr);
7214   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7215   int byte, offset, word, words, bitpos;
7216   unsigned char value;
7217
7218   /* There are always 32 bits in each long, no matter the size of
7219      the hosts long.  We handle floating point representations with
7220      up to 192 bits.  */
7221   long tmp[6];
7222
7223   if (total_bytes > len)
7224     return 0;
7225   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7226
7227   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7228
7229   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7230        bitpos += BITS_PER_UNIT)
7231     {
7232       byte = (bitpos / BITS_PER_UNIT) & 3;
7233       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7234
7235       if (UNITS_PER_WORD < 4)
7236         {
7237           word = byte / UNITS_PER_WORD;
7238           if (WORDS_BIG_ENDIAN)
7239             word = (words - 1) - word;
7240           offset = word * UNITS_PER_WORD;
7241           if (BYTES_BIG_ENDIAN)
7242             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7243           else
7244             offset += byte % UNITS_PER_WORD;
7245         }
7246       else
7247         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7248       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7249     }
7250   return total_bytes;
7251 }
7252
7253 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7254    specified by EXPR into the buffer PTR of length LEN bytes.
7255    Return the number of bytes placed in the buffer, or zero
7256    upon failure.  */
7257
7258 static int
7259 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7260 {
7261   int rsize, isize;
7262   tree part;
7263
7264   part = TREE_REALPART (expr);
7265   rsize = native_encode_expr (part, ptr, len);
7266   if (rsize == 0)
7267     return 0;
7268   part = TREE_IMAGPART (expr);
7269   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7270   if (isize != rsize)
7271     return 0;
7272   return rsize + isize;
7273 }
7274
7275
7276 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7277    specified by EXPR into the buffer PTR of length LEN bytes.
7278    Return the number of bytes placed in the buffer, or zero
7279    upon failure.  */
7280
7281 static int
7282 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7283 {
7284   int i, size, offset, count;
7285   tree itype, elem, elements;
7286
7287   offset = 0;
7288   elements = TREE_VECTOR_CST_ELTS (expr);
7289   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7290   itype = TREE_TYPE (TREE_TYPE (expr));
7291   size = GET_MODE_SIZE (TYPE_MODE (itype));
7292   for (i = 0; i < count; i++)
7293     {
7294       if (elements)
7295         {
7296           elem = TREE_VALUE (elements);
7297           elements = TREE_CHAIN (elements);
7298         }
7299       else
7300         elem = NULL_TREE;
7301
7302       if (elem)
7303         {
7304           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7305             return 0;
7306         }
7307       else
7308         {
7309           if (offset + size > len)
7310             return 0;
7311           memset (ptr+offset, 0, size);
7312         }
7313       offset += size;
7314     }
7315   return offset;
7316 }
7317
7318
7319 /* Subroutine of native_encode_expr.  Encode the STRING_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_string (const_tree expr, unsigned char *ptr, int len)
7326 {
7327   tree type = TREE_TYPE (expr);
7328   HOST_WIDE_INT total_bytes;
7329
7330   if (TREE_CODE (type) != ARRAY_TYPE
7331       || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7332       || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7333       || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7334     return 0;
7335   total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7336   if (total_bytes > len)
7337     return 0;
7338   if (TREE_STRING_LENGTH (expr) < total_bytes)
7339     {
7340       memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7341       memset (ptr + TREE_STRING_LENGTH (expr), 0,
7342               total_bytes - TREE_STRING_LENGTH (expr));
7343     }
7344   else
7345     memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7346   return total_bytes;
7347 }
7348
7349
7350 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7351    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7352    buffer PTR of length LEN bytes.  Return the number of bytes
7353    placed in the buffer, or zero upon failure.  */
7354
7355 int
7356 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7357 {
7358   switch (TREE_CODE (expr))
7359     {
7360     case INTEGER_CST:
7361       return native_encode_int (expr, ptr, len);
7362
7363     case REAL_CST:
7364       return native_encode_real (expr, ptr, len);
7365
7366     case COMPLEX_CST:
7367       return native_encode_complex (expr, ptr, len);
7368
7369     case VECTOR_CST:
7370       return native_encode_vector (expr, ptr, len);
7371
7372     case STRING_CST:
7373       return native_encode_string (expr, ptr, len);
7374
7375     default:
7376       return 0;
7377     }
7378 }
7379
7380
7381 /* Subroutine of native_interpret_expr.  Interpret the contents of
7382    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7383    If the buffer cannot be interpreted, return NULL_TREE.  */
7384
7385 static tree
7386 native_interpret_int (tree type, const unsigned char *ptr, int len)
7387 {
7388   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7389   int byte, offset, word, words;
7390   unsigned char value;
7391   double_int result;
7392
7393   if (total_bytes > len)
7394     return NULL_TREE;
7395   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7396     return NULL_TREE;
7397
7398   result = double_int_zero;
7399   words = total_bytes / UNITS_PER_WORD;
7400
7401   for (byte = 0; byte < total_bytes; byte++)
7402     {
7403       int bitpos = byte * BITS_PER_UNIT;
7404       if (total_bytes > UNITS_PER_WORD)
7405         {
7406           word = byte / UNITS_PER_WORD;
7407           if (WORDS_BIG_ENDIAN)
7408             word = (words - 1) - word;
7409           offset = word * UNITS_PER_WORD;
7410           if (BYTES_BIG_ENDIAN)
7411             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7412           else
7413             offset += byte % UNITS_PER_WORD;
7414         }
7415       else
7416         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7417       value = ptr[offset];
7418
7419       if (bitpos < HOST_BITS_PER_WIDE_INT)
7420         result.low |= (unsigned HOST_WIDE_INT) value << bitpos;
7421       else
7422         result.high |= (unsigned HOST_WIDE_INT) value
7423                        << (bitpos - HOST_BITS_PER_WIDE_INT);
7424     }
7425
7426   return double_int_to_tree (type, result);
7427 }
7428
7429
7430 /* Subroutine of native_interpret_expr.  Interpret the contents of
7431    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7432    If the buffer cannot be interpreted, return NULL_TREE.  */
7433
7434 static tree
7435 native_interpret_real (tree type, const unsigned char *ptr, int len)
7436 {
7437   enum machine_mode mode = TYPE_MODE (type);
7438   int total_bytes = GET_MODE_SIZE (mode);
7439   int byte, offset, word, words, bitpos;
7440   unsigned char value;
7441   /* There are always 32 bits in each long, no matter the size of
7442      the hosts long.  We handle floating point representations with
7443      up to 192 bits.  */
7444   REAL_VALUE_TYPE r;
7445   long tmp[6];
7446
7447   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7448   if (total_bytes > len || total_bytes > 24)
7449     return NULL_TREE;
7450   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7451
7452   memset (tmp, 0, sizeof (tmp));
7453   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7454        bitpos += BITS_PER_UNIT)
7455     {
7456       byte = (bitpos / BITS_PER_UNIT) & 3;
7457       if (UNITS_PER_WORD < 4)
7458         {
7459           word = byte / UNITS_PER_WORD;
7460           if (WORDS_BIG_ENDIAN)
7461             word = (words - 1) - word;
7462           offset = word * UNITS_PER_WORD;
7463           if (BYTES_BIG_ENDIAN)
7464             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7465           else
7466             offset += byte % UNITS_PER_WORD;
7467         }
7468       else
7469         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7470       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7471
7472       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7473     }
7474
7475   real_from_target (&r, tmp, mode);
7476   return build_real (type, r);
7477 }
7478
7479
7480 /* Subroutine of native_interpret_expr.  Interpret the contents of
7481    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7482    If the buffer cannot be interpreted, return NULL_TREE.  */
7483
7484 static tree
7485 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7486 {
7487   tree etype, rpart, ipart;
7488   int size;
7489
7490   etype = TREE_TYPE (type);
7491   size = GET_MODE_SIZE (TYPE_MODE (etype));
7492   if (size * 2 > len)
7493     return NULL_TREE;
7494   rpart = native_interpret_expr (etype, ptr, size);
7495   if (!rpart)
7496     return NULL_TREE;
7497   ipart = native_interpret_expr (etype, ptr+size, size);
7498   if (!ipart)
7499     return NULL_TREE;
7500   return build_complex (type, rpart, ipart);
7501 }
7502
7503
7504 /* Subroutine of native_interpret_expr.  Interpret the contents of
7505    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7506    If the buffer cannot be interpreted, return NULL_TREE.  */
7507
7508 static tree
7509 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7510 {
7511   tree etype, elem, elements;
7512   int i, size, count;
7513
7514   etype = TREE_TYPE (type);
7515   size = GET_MODE_SIZE (TYPE_MODE (etype));
7516   count = TYPE_VECTOR_SUBPARTS (type);
7517   if (size * count > len)
7518     return NULL_TREE;
7519
7520   elements = NULL_TREE;
7521   for (i = count - 1; i >= 0; i--)
7522     {
7523       elem = native_interpret_expr (etype, ptr+(i*size), size);
7524       if (!elem)
7525         return NULL_TREE;
7526       elements = tree_cons (NULL_TREE, elem, elements);
7527     }
7528   return build_vector (type, elements);
7529 }
7530
7531
7532 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7533    the buffer PTR of length LEN as a constant of type TYPE.  For
7534    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7535    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7536    return NULL_TREE.  */
7537
7538 tree
7539 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7540 {
7541   switch (TREE_CODE (type))
7542     {
7543     case INTEGER_TYPE:
7544     case ENUMERAL_TYPE:
7545     case BOOLEAN_TYPE:
7546       return native_interpret_int (type, ptr, len);
7547
7548     case REAL_TYPE:
7549       return native_interpret_real (type, ptr, len);
7550
7551     case COMPLEX_TYPE:
7552       return native_interpret_complex (type, ptr, len);
7553
7554     case VECTOR_TYPE:
7555       return native_interpret_vector (type, ptr, len);
7556
7557     default:
7558       return NULL_TREE;
7559     }
7560 }
7561
7562
7563 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7564    TYPE at compile-time.  If we're unable to perform the conversion
7565    return NULL_TREE.  */
7566
7567 static tree
7568 fold_view_convert_expr (tree type, tree expr)
7569 {
7570   /* We support up to 512-bit values (for V8DFmode).  */
7571   unsigned char buffer[64];
7572   int len;
7573
7574   /* Check that the host and target are sane.  */
7575   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7576     return NULL_TREE;
7577
7578   len = native_encode_expr (expr, buffer, sizeof (buffer));
7579   if (len == 0)
7580     return NULL_TREE;
7581
7582   return native_interpret_expr (type, buffer, len);
7583 }
7584
7585 /* Build an expression for the address of T.  Folds away INDIRECT_REF
7586    to avoid confusing the gimplify process.  */
7587
7588 tree
7589 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7590 {
7591   /* The size of the object is not relevant when talking about its address.  */
7592   if (TREE_CODE (t) == WITH_SIZE_EXPR)
7593     t = TREE_OPERAND (t, 0);
7594
7595   if (TREE_CODE (t) == INDIRECT_REF)
7596     {
7597       t = TREE_OPERAND (t, 0);
7598
7599       if (TREE_TYPE (t) != ptrtype)
7600         {
7601           t = build1 (NOP_EXPR, ptrtype, t);
7602           SET_EXPR_LOCATION (t, loc);
7603         }
7604     }
7605   else if (TREE_CODE (t) == MEM_REF
7606       && integer_zerop (TREE_OPERAND (t, 1)))
7607     return TREE_OPERAND (t, 0);
7608   else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7609     {
7610       t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7611
7612       if (TREE_TYPE (t) != ptrtype)
7613         t = fold_convert_loc (loc, ptrtype, t);
7614     }
7615   else
7616     {
7617       t = build1 (ADDR_EXPR, ptrtype, t);
7618       SET_EXPR_LOCATION (t, loc);
7619     }
7620
7621   return t;
7622 }
7623
7624 /* Build an expression for the address of T.  */
7625
7626 tree
7627 build_fold_addr_expr_loc (location_t loc, tree t)
7628 {
7629   tree ptrtype = build_pointer_type (TREE_TYPE (t));
7630
7631   return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7632 }
7633
7634 /* Fold a unary expression of code CODE and type TYPE with operand
7635    OP0.  Return the folded expression if folding is successful.
7636    Otherwise, return NULL_TREE.  */
7637
7638 tree
7639 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7640 {
7641   tree tem;
7642   tree arg0;
7643   enum tree_code_class kind = TREE_CODE_CLASS (code);
7644
7645   gcc_assert (IS_EXPR_CODE_CLASS (kind)
7646               && TREE_CODE_LENGTH (code) == 1);
7647
7648   arg0 = op0;
7649   if (arg0)
7650     {
7651       if (CONVERT_EXPR_CODE_P (code)
7652           || code == FLOAT_EXPR || code == ABS_EXPR)
7653         {
7654           /* Don't use STRIP_NOPS, because signedness of argument type
7655              matters.  */
7656           STRIP_SIGN_NOPS (arg0);
7657         }
7658       else
7659         {
7660           /* Strip any conversions that don't change the mode.  This
7661              is safe for every expression, except for a comparison
7662              expression because its signedness is derived from its
7663              operands.
7664
7665              Note that this is done as an internal manipulation within
7666              the constant folder, in order to find the simplest
7667              representation of the arguments so that their form can be
7668              studied.  In any cases, the appropriate type conversions
7669              should be put back in the tree that will get out of the
7670              constant folder.  */
7671           STRIP_NOPS (arg0);
7672         }
7673     }
7674
7675   if (TREE_CODE_CLASS (code) == tcc_unary)
7676     {
7677       if (TREE_CODE (arg0) == COMPOUND_EXPR)
7678         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7679                        fold_build1_loc (loc, code, type,
7680                                     fold_convert_loc (loc, TREE_TYPE (op0),
7681                                                       TREE_OPERAND (arg0, 1))));
7682       else if (TREE_CODE (arg0) == COND_EXPR)
7683         {
7684           tree arg01 = TREE_OPERAND (arg0, 1);
7685           tree arg02 = TREE_OPERAND (arg0, 2);
7686           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7687             arg01 = fold_build1_loc (loc, code, type,
7688                                  fold_convert_loc (loc,
7689                                                    TREE_TYPE (op0), arg01));
7690           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7691             arg02 = fold_build1_loc (loc, code, type,
7692                                  fold_convert_loc (loc,
7693                                                    TREE_TYPE (op0), arg02));
7694           tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7695                              arg01, arg02);
7696
7697           /* If this was a conversion, and all we did was to move into
7698              inside the COND_EXPR, bring it back out.  But leave it if
7699              it is a conversion from integer to integer and the
7700              result precision is no wider than a word since such a
7701              conversion is cheap and may be optimized away by combine,
7702              while it couldn't if it were outside the COND_EXPR.  Then return
7703              so we don't get into an infinite recursion loop taking the
7704              conversion out and then back in.  */
7705
7706           if ((CONVERT_EXPR_CODE_P (code)
7707                || code == NON_LVALUE_EXPR)
7708               && TREE_CODE (tem) == COND_EXPR
7709               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7710               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7711               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7712               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7713               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7714                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7715               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7716                      && (INTEGRAL_TYPE_P
7717                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7718                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7719                   || flag_syntax_only))
7720             {
7721               tem = build1 (code, type,
7722                             build3 (COND_EXPR,
7723                                     TREE_TYPE (TREE_OPERAND
7724                                                (TREE_OPERAND (tem, 1), 0)),
7725                                     TREE_OPERAND (tem, 0),
7726                                     TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7727                                     TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
7728               SET_EXPR_LOCATION (tem, loc);
7729             }
7730           return tem;
7731         }
7732       else if (COMPARISON_CLASS_P (arg0))
7733         {
7734           if (TREE_CODE (type) == BOOLEAN_TYPE)
7735             {
7736               arg0 = copy_node (arg0);
7737               TREE_TYPE (arg0) = type;
7738               return arg0;
7739             }
7740           else if (TREE_CODE (type) != INTEGER_TYPE)
7741             return fold_build3_loc (loc, COND_EXPR, type, arg0,
7742                                 fold_build1_loc (loc, code, type,
7743                                              integer_one_node),
7744                                 fold_build1_loc (loc, code, type,
7745                                              integer_zero_node));
7746         }
7747    }
7748
7749   switch (code)
7750     {
7751     case PAREN_EXPR:
7752       /* Re-association barriers around constants and other re-association
7753          barriers can be removed.  */
7754       if (CONSTANT_CLASS_P (op0)
7755           || TREE_CODE (op0) == PAREN_EXPR)
7756         return fold_convert_loc (loc, type, op0);
7757       return NULL_TREE;
7758
7759     CASE_CONVERT:
7760     case FLOAT_EXPR:
7761     case FIX_TRUNC_EXPR:
7762       if (TREE_TYPE (op0) == type)
7763         return op0;
7764
7765       /* If we have (type) (a CMP b) and type is an integral type, return
7766          new expression involving the new type.  */
7767       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
7768         return fold_build2_loc (loc, TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
7769                             TREE_OPERAND (op0, 1));
7770
7771       /* Handle cases of two conversions in a row.  */
7772       if (CONVERT_EXPR_P (op0))
7773         {
7774           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7775           tree inter_type = TREE_TYPE (op0);
7776           int inside_int = INTEGRAL_TYPE_P (inside_type);
7777           int inside_ptr = POINTER_TYPE_P (inside_type);
7778           int inside_float = FLOAT_TYPE_P (inside_type);
7779           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7780           unsigned int inside_prec = TYPE_PRECISION (inside_type);
7781           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7782           int inter_int = INTEGRAL_TYPE_P (inter_type);
7783           int inter_ptr = POINTER_TYPE_P (inter_type);
7784           int inter_float = FLOAT_TYPE_P (inter_type);
7785           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7786           unsigned int inter_prec = TYPE_PRECISION (inter_type);
7787           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7788           int final_int = INTEGRAL_TYPE_P (type);
7789           int final_ptr = POINTER_TYPE_P (type);
7790           int final_float = FLOAT_TYPE_P (type);
7791           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7792           unsigned int final_prec = TYPE_PRECISION (type);
7793           int final_unsignedp = TYPE_UNSIGNED (type);
7794
7795           /* In addition to the cases of two conversions in a row
7796              handled below, if we are converting something to its own
7797              type via an object of identical or wider precision, neither
7798              conversion is needed.  */
7799           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7800               && (((inter_int || inter_ptr) && final_int)
7801                   || (inter_float && final_float))
7802               && inter_prec >= final_prec)
7803             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7804
7805           /* Likewise, if the intermediate and initial types are either both
7806              float or both integer, we don't need the middle conversion if the
7807              former is wider than the latter and doesn't change the signedness
7808              (for integers).  Avoid this if the final type is a pointer since
7809              then we sometimes need the middle conversion.  Likewise if the
7810              final type has a precision not equal to the size of its mode.  */
7811           if (((inter_int && inside_int)
7812                || (inter_float && inside_float)
7813                || (inter_vec && inside_vec))
7814               && inter_prec >= inside_prec
7815               && (inter_float || inter_vec
7816                   || inter_unsignedp == inside_unsignedp)
7817               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7818                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
7819               && ! final_ptr
7820               && (! final_vec || inter_prec == inside_prec))
7821             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7822
7823           /* If we have a sign-extension of a zero-extended value, we can
7824              replace that by a single zero-extension.  */
7825           if (inside_int && inter_int && final_int
7826               && inside_prec < inter_prec && inter_prec < final_prec
7827               && inside_unsignedp && !inter_unsignedp)
7828             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7829
7830           /* Two conversions in a row are not needed unless:
7831              - some conversion is floating-point (overstrict for now), or
7832              - some conversion is a vector (overstrict for now), or
7833              - the intermediate type is narrower than both initial and
7834                final, or
7835              - the intermediate type and innermost type differ in signedness,
7836                and the outermost type is wider than the intermediate, or
7837              - the initial type is a pointer type and the precisions of the
7838                intermediate and final types differ, or
7839              - the final type is a pointer type and the precisions of the
7840                initial and intermediate types differ.  */
7841           if (! inside_float && ! inter_float && ! final_float
7842               && ! inside_vec && ! inter_vec && ! final_vec
7843               && (inter_prec >= inside_prec || inter_prec >= final_prec)
7844               && ! (inside_int && inter_int
7845                     && inter_unsignedp != inside_unsignedp
7846                     && inter_prec < final_prec)
7847               && ((inter_unsignedp && inter_prec > inside_prec)
7848                   == (final_unsignedp && final_prec > inter_prec))
7849               && ! (inside_ptr && inter_prec != final_prec)
7850               && ! (final_ptr && inside_prec != inter_prec)
7851               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7852                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7853             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7854         }
7855
7856       /* Handle (T *)&A.B.C for A being of type T and B and C
7857          living at offset zero.  This occurs frequently in
7858          C++ upcasting and then accessing the base.  */
7859       if (TREE_CODE (op0) == ADDR_EXPR
7860           && POINTER_TYPE_P (type)
7861           && handled_component_p (TREE_OPERAND (op0, 0)))
7862         {
7863           HOST_WIDE_INT bitsize, bitpos;
7864           tree offset;
7865           enum machine_mode mode;
7866           int unsignedp, volatilep;
7867           tree base = TREE_OPERAND (op0, 0);
7868           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7869                                       &mode, &unsignedp, &volatilep, false);
7870           /* If the reference was to a (constant) zero offset, we can use
7871              the address of the base if it has the same base type
7872              as the result type and the pointer type is unqualified.  */
7873           if (! offset && bitpos == 0
7874               && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7875                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7876               && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7877             return fold_convert_loc (loc, type,
7878                                      build_fold_addr_expr_loc (loc, base));
7879         }
7880
7881       if (TREE_CODE (op0) == MODIFY_EXPR
7882           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7883           /* Detect assigning a bitfield.  */
7884           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7885                && DECL_BIT_FIELD
7886                (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7887         {
7888           /* Don't leave an assignment inside a conversion
7889              unless assigning a bitfield.  */
7890           tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7891           /* First do the assignment, then return converted constant.  */
7892           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7893           TREE_NO_WARNING (tem) = 1;
7894           TREE_USED (tem) = 1;
7895           SET_EXPR_LOCATION (tem, loc);
7896           return tem;
7897         }
7898
7899       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7900          constants (if x has signed type, the sign bit cannot be set
7901          in c).  This folds extension into the BIT_AND_EXPR.
7902          ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7903          very likely don't have maximal range for their precision and this
7904          transformation effectively doesn't preserve non-maximal ranges.  */
7905       if (TREE_CODE (type) == INTEGER_TYPE
7906           && TREE_CODE (op0) == BIT_AND_EXPR
7907           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7908         {
7909           tree and_expr = op0;
7910           tree and0 = TREE_OPERAND (and_expr, 0);
7911           tree and1 = TREE_OPERAND (and_expr, 1);
7912           int change = 0;
7913
7914           if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7915               || (TYPE_PRECISION (type)
7916                   <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7917             change = 1;
7918           else if (TYPE_PRECISION (TREE_TYPE (and1))
7919                    <= HOST_BITS_PER_WIDE_INT
7920                    && host_integerp (and1, 1))
7921             {
7922               unsigned HOST_WIDE_INT cst;
7923
7924               cst = tree_low_cst (and1, 1);
7925               cst &= (HOST_WIDE_INT) -1
7926                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7927               change = (cst == 0);
7928 #ifdef LOAD_EXTEND_OP
7929               if (change
7930                   && !flag_syntax_only
7931                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7932                       == ZERO_EXTEND))
7933                 {
7934                   tree uns = unsigned_type_for (TREE_TYPE (and0));
7935                   and0 = fold_convert_loc (loc, uns, and0);
7936                   and1 = fold_convert_loc (loc, uns, and1);
7937                 }
7938 #endif
7939             }
7940           if (change)
7941             {
7942               tem = force_fit_type_double (type, tree_to_double_int (and1),
7943                                            0, TREE_OVERFLOW (and1));
7944               return fold_build2_loc (loc, BIT_AND_EXPR, type,
7945                                   fold_convert_loc (loc, type, and0), tem);
7946             }
7947         }
7948
7949       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7950          when one of the new casts will fold away. Conservatively we assume
7951          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7952       if (POINTER_TYPE_P (type)
7953           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7954           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7955               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7956               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7957         {
7958           tree arg00 = TREE_OPERAND (arg0, 0);
7959           tree arg01 = TREE_OPERAND (arg0, 1);
7960
7961           return fold_build2_loc (loc,
7962                               TREE_CODE (arg0), type,
7963                               fold_convert_loc (loc, type, arg00),
7964                               fold_convert_loc (loc, sizetype, arg01));
7965         }
7966
7967       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7968          of the same precision, and X is an integer type not narrower than
7969          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
7970       if (INTEGRAL_TYPE_P (type)
7971           && TREE_CODE (op0) == BIT_NOT_EXPR
7972           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7973           && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7974           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7975         {
7976           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7977           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7978               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7979             return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7980                                 fold_convert_loc (loc, type, tem));
7981         }
7982
7983       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7984          type of X and Y (integer types only).  */
7985       if (INTEGRAL_TYPE_P (type)
7986           && TREE_CODE (op0) == MULT_EXPR
7987           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7988           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7989         {
7990           /* Be careful not to introduce new overflows.  */
7991           tree mult_type;
7992           if (TYPE_OVERFLOW_WRAPS (type))
7993             mult_type = type;
7994           else
7995             mult_type = unsigned_type_for (type);
7996
7997           if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7998             {
7999               tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8000                                  fold_convert_loc (loc, mult_type,
8001                                                    TREE_OPERAND (op0, 0)),
8002                                  fold_convert_loc (loc, mult_type,
8003                                                    TREE_OPERAND (op0, 1)));
8004               return fold_convert_loc (loc, type, tem);
8005             }
8006         }
8007
8008       tem = fold_convert_const (code, type, op0);
8009       return tem ? tem : NULL_TREE;
8010
8011     case ADDR_SPACE_CONVERT_EXPR:
8012       if (integer_zerop (arg0))
8013         return fold_convert_const (code, type, arg0);
8014       return NULL_TREE;
8015
8016     case FIXED_CONVERT_EXPR:
8017       tem = fold_convert_const (code, type, arg0);
8018       return tem ? tem : NULL_TREE;
8019
8020     case VIEW_CONVERT_EXPR:
8021       if (TREE_TYPE (op0) == type)
8022         return op0;
8023       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8024         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8025                             type, TREE_OPERAND (op0, 0));
8026       if (TREE_CODE (op0) == MEM_REF)
8027         return fold_build2_loc (loc, MEM_REF, type,
8028                                 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
8029
8030       /* For integral conversions with the same precision or pointer
8031          conversions use a NOP_EXPR instead.  */
8032       if ((INTEGRAL_TYPE_P (type)
8033            || POINTER_TYPE_P (type))
8034           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8035               || POINTER_TYPE_P (TREE_TYPE (op0)))
8036           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8037         return fold_convert_loc (loc, type, op0);
8038
8039       /* Strip inner integral conversions that do not change the precision.  */
8040       if (CONVERT_EXPR_P (op0)
8041           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8042               || POINTER_TYPE_P (TREE_TYPE (op0)))
8043           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8044               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8045           && (TYPE_PRECISION (TREE_TYPE (op0))
8046               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8047         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8048                             type, TREE_OPERAND (op0, 0));
8049
8050       return fold_view_convert_expr (type, op0);
8051
8052     case NEGATE_EXPR:
8053       tem = fold_negate_expr (loc, arg0);
8054       if (tem)
8055         return fold_convert_loc (loc, type, tem);
8056       return NULL_TREE;
8057
8058     case ABS_EXPR:
8059       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8060         return fold_abs_const (arg0, type);
8061       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8062         return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8063       /* Convert fabs((double)float) into (double)fabsf(float).  */
8064       else if (TREE_CODE (arg0) == NOP_EXPR
8065                && TREE_CODE (type) == REAL_TYPE)
8066         {
8067           tree targ0 = strip_float_extensions (arg0);
8068           if (targ0 != arg0)
8069             return fold_convert_loc (loc, type,
8070                                      fold_build1_loc (loc, ABS_EXPR,
8071                                                   TREE_TYPE (targ0),
8072                                                   targ0));
8073         }
8074       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8075       else if (TREE_CODE (arg0) == ABS_EXPR)
8076         return arg0;
8077       else if (tree_expr_nonnegative_p (arg0))
8078         return arg0;
8079
8080       /* Strip sign ops from argument.  */
8081       if (TREE_CODE (type) == REAL_TYPE)
8082         {
8083           tem = fold_strip_sign_ops (arg0);
8084           if (tem)
8085             return fold_build1_loc (loc, ABS_EXPR, type,
8086                                 fold_convert_loc (loc, type, tem));
8087         }
8088       return NULL_TREE;
8089
8090     case CONJ_EXPR:
8091       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8092         return fold_convert_loc (loc, type, arg0);
8093       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8094         {
8095           tree itype = TREE_TYPE (type);
8096           tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8097           tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8098           return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8099                               negate_expr (ipart));
8100         }
8101       if (TREE_CODE (arg0) == COMPLEX_CST)
8102         {
8103           tree itype = TREE_TYPE (type);
8104           tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8105           tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8106           return build_complex (type, rpart, negate_expr (ipart));
8107         }
8108       if (TREE_CODE (arg0) == CONJ_EXPR)
8109         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8110       return NULL_TREE;
8111
8112     case BIT_NOT_EXPR:
8113       if (TREE_CODE (arg0) == INTEGER_CST)
8114         return fold_not_const (arg0, type);
8115       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8116         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8117       /* Convert ~ (-A) to A - 1.  */
8118       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8119         return fold_build2_loc (loc, MINUS_EXPR, type,
8120                             fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8121                             build_int_cst (type, 1));
8122       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8123       else if (INTEGRAL_TYPE_P (type)
8124                && ((TREE_CODE (arg0) == MINUS_EXPR
8125                     && integer_onep (TREE_OPERAND (arg0, 1)))
8126                    || (TREE_CODE (arg0) == PLUS_EXPR
8127                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8128         return fold_build1_loc (loc, NEGATE_EXPR, type,
8129                             fold_convert_loc (loc, type,
8130                                               TREE_OPERAND (arg0, 0)));
8131       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8132       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8133                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8134                                      fold_convert_loc (loc, type,
8135                                                        TREE_OPERAND (arg0, 0)))))
8136         return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8137                             fold_convert_loc (loc, type,
8138                                               TREE_OPERAND (arg0, 1)));
8139       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8140                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8141                                      fold_convert_loc (loc, type,
8142                                                        TREE_OPERAND (arg0, 1)))))
8143         return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8144                             fold_convert_loc (loc, type,
8145                                               TREE_OPERAND (arg0, 0)), tem);
8146       /* Perform BIT_NOT_EXPR on each element individually.  */
8147       else if (TREE_CODE (arg0) == VECTOR_CST)
8148         {
8149           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8150           int count = TYPE_VECTOR_SUBPARTS (type), i;
8151
8152           for (i = 0; i < count; i++)
8153             {
8154               if (elements)
8155                 {
8156                   elem = TREE_VALUE (elements);
8157                   elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8158                   if (elem == NULL_TREE)
8159                     break;
8160                   elements = TREE_CHAIN (elements);
8161                 }
8162               else
8163                 elem = build_int_cst (TREE_TYPE (type), -1);
8164               list = tree_cons (NULL_TREE, elem, list);
8165             }
8166           if (i == count)
8167             return build_vector (type, nreverse (list));
8168         }
8169
8170       return NULL_TREE;
8171
8172     case TRUTH_NOT_EXPR:
8173       /* The argument to invert_truthvalue must have Boolean type.  */
8174       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8175           arg0 = fold_convert_loc (loc, boolean_type_node, arg0);
8176
8177       /* Note that the operand of this must be an int
8178          and its values must be 0 or 1.
8179          ("true" is a fixed value perhaps depending on the language,
8180          but we don't handle values other than 1 correctly yet.)  */
8181       tem = fold_truth_not_expr (loc, arg0);
8182       if (!tem)
8183         return NULL_TREE;
8184       return fold_convert_loc (loc, type, tem);
8185
8186     case REALPART_EXPR:
8187       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8188         return fold_convert_loc (loc, type, arg0);
8189       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8190         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8191                                  TREE_OPERAND (arg0, 1));
8192       if (TREE_CODE (arg0) == COMPLEX_CST)
8193         return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8194       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8195         {
8196           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8197           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8198                              fold_build1_loc (loc, REALPART_EXPR, itype,
8199                                           TREE_OPERAND (arg0, 0)),
8200                              fold_build1_loc (loc, REALPART_EXPR, itype,
8201                                           TREE_OPERAND (arg0, 1)));
8202           return fold_convert_loc (loc, type, tem);
8203         }
8204       if (TREE_CODE (arg0) == CONJ_EXPR)
8205         {
8206           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8207           tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8208                              TREE_OPERAND (arg0, 0));
8209           return fold_convert_loc (loc, type, tem);
8210         }
8211       if (TREE_CODE (arg0) == CALL_EXPR)
8212         {
8213           tree fn = get_callee_fndecl (arg0);
8214           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8215             switch (DECL_FUNCTION_CODE (fn))
8216               {
8217               CASE_FLT_FN (BUILT_IN_CEXPI):
8218                 fn = mathfn_built_in (type, BUILT_IN_COS);
8219                 if (fn)
8220                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8221                 break;
8222
8223               default:
8224                 break;
8225               }
8226         }
8227       return NULL_TREE;
8228
8229     case IMAGPART_EXPR:
8230       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8231         return fold_convert_loc (loc, type, integer_zero_node);
8232       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8233         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8234                                  TREE_OPERAND (arg0, 0));
8235       if (TREE_CODE (arg0) == COMPLEX_CST)
8236         return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8237       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8238         {
8239           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8240           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8241                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8242                                           TREE_OPERAND (arg0, 0)),
8243                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8244                                           TREE_OPERAND (arg0, 1)));
8245           return fold_convert_loc (loc, type, tem);
8246         }
8247       if (TREE_CODE (arg0) == CONJ_EXPR)
8248         {
8249           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8250           tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8251           return fold_convert_loc (loc, type, negate_expr (tem));
8252         }
8253       if (TREE_CODE (arg0) == CALL_EXPR)
8254         {
8255           tree fn = get_callee_fndecl (arg0);
8256           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8257             switch (DECL_FUNCTION_CODE (fn))
8258               {
8259               CASE_FLT_FN (BUILT_IN_CEXPI):
8260                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8261                 if (fn)
8262                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8263                 break;
8264
8265               default:
8266                 break;
8267               }
8268         }
8269       return NULL_TREE;
8270
8271     case INDIRECT_REF:
8272       /* Fold *&X to X if X is an lvalue.  */
8273       if (TREE_CODE (op0) == ADDR_EXPR)
8274         {
8275           tree op00 = TREE_OPERAND (op0, 0);
8276           if ((TREE_CODE (op00) == VAR_DECL
8277                || TREE_CODE (op00) == PARM_DECL
8278                || TREE_CODE (op00) == RESULT_DECL)
8279               && !TREE_READONLY (op00))
8280             return op00;
8281         }
8282       return NULL_TREE;
8283
8284     default:
8285       return NULL_TREE;
8286     } /* switch (code) */
8287 }
8288
8289
8290 /* If the operation was a conversion do _not_ mark a resulting constant
8291    with TREE_OVERFLOW if the original constant was not.  These conversions
8292    have implementation defined behavior and retaining the TREE_OVERFLOW
8293    flag here would confuse later passes such as VRP.  */
8294 tree
8295 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8296                                 tree type, tree op0)
8297 {
8298   tree res = fold_unary_loc (loc, code, type, op0);
8299   if (res
8300       && TREE_CODE (res) == INTEGER_CST
8301       && TREE_CODE (op0) == INTEGER_CST
8302       && CONVERT_EXPR_CODE_P (code))
8303     TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8304
8305   return res;
8306 }
8307
8308 /* Fold a binary expression of code CODE and type TYPE with operands
8309    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8310    Return the folded expression if folding is successful.  Otherwise,
8311    return NULL_TREE.  */
8312
8313 static tree
8314 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8315 {
8316   enum tree_code compl_code;
8317
8318   if (code == MIN_EXPR)
8319     compl_code = MAX_EXPR;
8320   else if (code == MAX_EXPR)
8321     compl_code = MIN_EXPR;
8322   else
8323     gcc_unreachable ();
8324
8325   /* MIN (MAX (a, b), b) == b.  */
8326   if (TREE_CODE (op0) == compl_code
8327       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8328     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8329
8330   /* MIN (MAX (b, a), b) == b.  */
8331   if (TREE_CODE (op0) == compl_code
8332       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8333       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8334     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8335
8336   /* MIN (a, MAX (a, b)) == a.  */
8337   if (TREE_CODE (op1) == compl_code
8338       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8339       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8340     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8341
8342   /* MIN (a, MAX (b, a)) == a.  */
8343   if (TREE_CODE (op1) == compl_code
8344       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8345       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8346     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8347
8348   return NULL_TREE;
8349 }
8350
8351 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8352    by changing CODE to reduce the magnitude of constants involved in
8353    ARG0 of the comparison.
8354    Returns a canonicalized comparison tree if a simplification was
8355    possible, otherwise returns NULL_TREE.
8356    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8357    valid if signed overflow is undefined.  */
8358
8359 static tree
8360 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8361                                  tree arg0, tree arg1,
8362                                  bool *strict_overflow_p)
8363 {
8364   enum tree_code code0 = TREE_CODE (arg0);
8365   tree t, cst0 = NULL_TREE;
8366   int sgn0;
8367   bool swap = false;
8368
8369   /* Match A +- CST code arg1 and CST code arg1.  We can change the
8370      first form only if overflow is undefined.  */
8371   if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8372          /* In principle pointers also have undefined overflow behavior,
8373             but that causes problems elsewhere.  */
8374          && !POINTER_TYPE_P (TREE_TYPE (arg0))
8375          && (code0 == MINUS_EXPR
8376              || code0 == PLUS_EXPR)
8377          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8378         || code0 == INTEGER_CST))
8379     return NULL_TREE;
8380
8381   /* Identify the constant in arg0 and its sign.  */
8382   if (code0 == INTEGER_CST)
8383     cst0 = arg0;
8384   else
8385     cst0 = TREE_OPERAND (arg0, 1);
8386   sgn0 = tree_int_cst_sgn (cst0);
8387
8388   /* Overflowed constants and zero will cause problems.  */
8389   if (integer_zerop (cst0)
8390       || TREE_OVERFLOW (cst0))
8391     return NULL_TREE;
8392
8393   /* See if we can reduce the magnitude of the constant in
8394      arg0 by changing the comparison code.  */
8395   if (code0 == INTEGER_CST)
8396     {
8397       /* CST <= arg1  ->  CST-1 < arg1.  */
8398       if (code == LE_EXPR && sgn0 == 1)
8399         code = LT_EXPR;
8400       /* -CST < arg1  ->  -CST-1 <= arg1.  */
8401       else if (code == LT_EXPR && sgn0 == -1)
8402         code = LE_EXPR;
8403       /* CST > arg1  ->  CST-1 >= arg1.  */
8404       else if (code == GT_EXPR && sgn0 == 1)
8405         code = GE_EXPR;
8406       /* -CST >= arg1  ->  -CST-1 > arg1.  */
8407       else if (code == GE_EXPR && sgn0 == -1)
8408         code = GT_EXPR;
8409       else
8410         return NULL_TREE;
8411       /* arg1 code' CST' might be more canonical.  */
8412       swap = true;
8413     }
8414   else
8415     {
8416       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
8417       if (code == LT_EXPR
8418           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8419         code = LE_EXPR;
8420       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
8421       else if (code == GT_EXPR
8422                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8423         code = GE_EXPR;
8424       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
8425       else if (code == LE_EXPR
8426                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8427         code = LT_EXPR;
8428       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
8429       else if (code == GE_EXPR
8430                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8431         code = GT_EXPR;
8432       else
8433         return NULL_TREE;
8434       *strict_overflow_p = true;
8435     }
8436
8437   /* Now build the constant reduced in magnitude.  But not if that
8438      would produce one outside of its types range.  */
8439   if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8440       && ((sgn0 == 1
8441            && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8442            && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8443           || (sgn0 == -1
8444               && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8445               && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8446     /* We cannot swap the comparison here as that would cause us to
8447        endlessly recurse.  */
8448     return NULL_TREE;
8449
8450   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8451                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8452   if (code0 != INTEGER_CST)
8453     t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8454
8455   /* If swapping might yield to a more canonical form, do so.  */
8456   if (swap)
8457     return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8458   else
8459     return fold_build2_loc (loc, code, type, t, arg1);
8460 }
8461
8462 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8463    overflow further.  Try to decrease the magnitude of constants involved
8464    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8465    and put sole constants at the second argument position.
8466    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
8467
8468 static tree
8469 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8470                                tree arg0, tree arg1)
8471 {
8472   tree t;
8473   bool strict_overflow_p;
8474   const char * const warnmsg = G_("assuming signed overflow does not occur "
8475                                   "when reducing constant in comparison");
8476
8477   /* Try canonicalization by simplifying arg0.  */
8478   strict_overflow_p = false;
8479   t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8480                                        &strict_overflow_p);
8481   if (t)
8482     {
8483       if (strict_overflow_p)
8484         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8485       return t;
8486     }
8487
8488   /* Try canonicalization by simplifying arg1 using the swapped
8489      comparison.  */
8490   code = swap_tree_comparison (code);
8491   strict_overflow_p = false;
8492   t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8493                                        &strict_overflow_p);
8494   if (t && strict_overflow_p)
8495     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8496   return t;
8497 }
8498
8499 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8500    space.  This is used to avoid issuing overflow warnings for
8501    expressions like &p->x which can not wrap.  */
8502
8503 static bool
8504 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8505 {
8506   unsigned HOST_WIDE_INT offset_low, total_low;
8507   HOST_WIDE_INT size, offset_high, total_high;
8508
8509   if (!POINTER_TYPE_P (TREE_TYPE (base)))
8510     return true;
8511
8512   if (bitpos < 0)
8513     return true;
8514
8515   if (offset == NULL_TREE)
8516     {
8517       offset_low = 0;
8518       offset_high = 0;
8519     }
8520   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8521     return true;
8522   else
8523     {
8524       offset_low = TREE_INT_CST_LOW (offset);
8525       offset_high = TREE_INT_CST_HIGH (offset);
8526     }
8527
8528   if (add_double_with_sign (offset_low, offset_high,
8529                             bitpos / BITS_PER_UNIT, 0,
8530                             &total_low, &total_high,
8531                             true))
8532     return true;
8533
8534   if (total_high != 0)
8535     return true;
8536
8537   size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8538   if (size <= 0)
8539     return true;
8540
8541   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8542      array.  */
8543   if (TREE_CODE (base) == ADDR_EXPR)
8544     {
8545       HOST_WIDE_INT base_size;
8546
8547       base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8548       if (base_size > 0 && size < base_size)
8549         size = base_size;
8550     }
8551
8552   return total_low > (unsigned HOST_WIDE_INT) size;
8553 }
8554
8555 /* Subroutine of fold_binary.  This routine performs all of the
8556    transformations that are common to the equality/inequality
8557    operators (EQ_EXPR and NE_EXPR) and the ordering operators
8558    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
8559    fold_binary should call fold_binary.  Fold a comparison with
8560    tree code CODE and type TYPE with operands OP0 and OP1.  Return
8561    the folded comparison or NULL_TREE.  */
8562
8563 static tree
8564 fold_comparison (location_t loc, enum tree_code code, tree type,
8565                  tree op0, tree op1)
8566 {
8567   tree arg0, arg1, tem;
8568
8569   arg0 = op0;
8570   arg1 = op1;
8571
8572   STRIP_SIGN_NOPS (arg0);
8573   STRIP_SIGN_NOPS (arg1);
8574
8575   tem = fold_relational_const (code, type, arg0, arg1);
8576   if (tem != NULL_TREE)
8577     return tem;
8578
8579   /* If one arg is a real or integer constant, put it last.  */
8580   if (tree_swap_operands_p (arg0, arg1, true))
8581     return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
8582
8583   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
8584   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8585       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8586           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8587           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8588       && (TREE_CODE (arg1) == INTEGER_CST
8589           && !TREE_OVERFLOW (arg1)))
8590     {
8591       tree const1 = TREE_OPERAND (arg0, 1);
8592       tree const2 = arg1;
8593       tree variable = TREE_OPERAND (arg0, 0);
8594       tree lhs;
8595       int lhs_add;
8596       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8597
8598       lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
8599                          TREE_TYPE (arg1), const2, const1);
8600
8601       /* If the constant operation overflowed this can be
8602          simplified as a comparison against INT_MAX/INT_MIN.  */
8603       if (TREE_CODE (lhs) == INTEGER_CST
8604           && TREE_OVERFLOW (lhs))
8605         {
8606           int const1_sgn = tree_int_cst_sgn (const1);
8607           enum tree_code code2 = code;
8608
8609           /* Get the sign of the constant on the lhs if the
8610              operation were VARIABLE + CONST1.  */
8611           if (TREE_CODE (arg0) == MINUS_EXPR)
8612             const1_sgn = -const1_sgn;
8613
8614           /* The sign of the constant determines if we overflowed
8615              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8616              Canonicalize to the INT_MIN overflow by swapping the comparison
8617              if necessary.  */
8618           if (const1_sgn == -1)
8619             code2 = swap_tree_comparison (code);
8620
8621           /* We now can look at the canonicalized case
8622                VARIABLE + 1  CODE2  INT_MIN
8623              and decide on the result.  */
8624           if (code2 == LT_EXPR
8625               || code2 == LE_EXPR
8626               || code2 == EQ_EXPR)
8627             return omit_one_operand_loc (loc, type, boolean_false_node, variable);
8628           else if (code2 == NE_EXPR
8629                    || code2 == GE_EXPR
8630                    || code2 == GT_EXPR)
8631             return omit_one_operand_loc (loc, type, boolean_true_node, variable);
8632         }
8633
8634       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8635           && (TREE_CODE (lhs) != INTEGER_CST
8636               || !TREE_OVERFLOW (lhs)))
8637         {
8638           fold_overflow_warning ("assuming signed overflow does not occur "
8639                                   "when changing X +- C1 cmp C2 to "
8640                                   "X cmp C1 +- C2",
8641                                  WARN_STRICT_OVERFLOW_COMPARISON);
8642           return fold_build2_loc (loc, code, type, variable, lhs);
8643         }
8644     }
8645
8646   /* For comparisons of pointers we can decompose it to a compile time
8647      comparison of the base objects and the offsets into the object.
8648      This requires at least one operand being an ADDR_EXPR or a
8649      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
8650   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8651       && (TREE_CODE (arg0) == ADDR_EXPR
8652           || TREE_CODE (arg1) == ADDR_EXPR
8653           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8654           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8655     {
8656       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8657       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8658       enum machine_mode mode;
8659       int volatilep, unsignedp;
8660       bool indirect_base0 = false, indirect_base1 = false;
8661
8662       /* Get base and offset for the access.  Strip ADDR_EXPR for
8663          get_inner_reference, but put it back by stripping INDIRECT_REF
8664          off the base object if possible.  indirect_baseN will be true
8665          if baseN is not an address but refers to the object itself.  */
8666       base0 = arg0;
8667       if (TREE_CODE (arg0) == ADDR_EXPR)
8668         {
8669           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8670                                        &bitsize, &bitpos0, &offset0, &mode,
8671                                        &unsignedp, &volatilep, false);
8672           if (TREE_CODE (base0) == INDIRECT_REF)
8673             base0 = TREE_OPERAND (base0, 0);
8674           else
8675             indirect_base0 = true;
8676         }
8677       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8678         {
8679           base0 = TREE_OPERAND (arg0, 0);
8680           STRIP_SIGN_NOPS (base0);
8681           if (TREE_CODE (base0) == ADDR_EXPR)
8682             {
8683               base0 = TREE_OPERAND (base0, 0);
8684               indirect_base0 = true;
8685             }
8686           offset0 = TREE_OPERAND (arg0, 1);
8687         }
8688
8689       base1 = arg1;
8690       if (TREE_CODE (arg1) == ADDR_EXPR)
8691         {
8692           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8693                                        &bitsize, &bitpos1, &offset1, &mode,
8694                                        &unsignedp, &volatilep, false);
8695           if (TREE_CODE (base1) == INDIRECT_REF)
8696             base1 = TREE_OPERAND (base1, 0);
8697           else
8698             indirect_base1 = true;
8699         }
8700       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8701         {
8702           base1 = TREE_OPERAND (arg1, 0);
8703           STRIP_SIGN_NOPS (base1);
8704           if (TREE_CODE (base1) == ADDR_EXPR)
8705             {
8706               base1 = TREE_OPERAND (base1, 0);
8707               indirect_base1 = true;
8708             }
8709           offset1 = TREE_OPERAND (arg1, 1);
8710         }
8711
8712       /* A local variable can never be pointed to by
8713          the default SSA name of an incoming parameter.  */
8714       if ((TREE_CODE (arg0) == ADDR_EXPR
8715            && indirect_base0
8716            && TREE_CODE (base0) == VAR_DECL
8717            && auto_var_in_fn_p (base0, current_function_decl)
8718            && !indirect_base1
8719            && TREE_CODE (base1) == SSA_NAME
8720            && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL
8721            && SSA_NAME_IS_DEFAULT_DEF (base1))
8722           || (TREE_CODE (arg1) == ADDR_EXPR
8723               && indirect_base1
8724               && TREE_CODE (base1) == VAR_DECL
8725               && auto_var_in_fn_p (base1, current_function_decl)
8726               && !indirect_base0
8727               && TREE_CODE (base0) == SSA_NAME
8728               && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL
8729               && SSA_NAME_IS_DEFAULT_DEF (base0)))
8730         {
8731           if (code == NE_EXPR)
8732             return constant_boolean_node (1, type);
8733           else if (code == EQ_EXPR)
8734             return constant_boolean_node (0, type);
8735         }
8736       /* If we have equivalent bases we might be able to simplify.  */
8737       else if (indirect_base0 == indirect_base1
8738                && operand_equal_p (base0, base1, 0))
8739         {
8740           /* We can fold this expression to a constant if the non-constant
8741              offset parts are equal.  */
8742           if ((offset0 == offset1
8743                || (offset0 && offset1
8744                    && operand_equal_p (offset0, offset1, 0)))
8745               && (code == EQ_EXPR
8746                   || code == NE_EXPR
8747                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
8748
8749             {
8750               if (code != EQ_EXPR
8751                   && code != NE_EXPR
8752                   && bitpos0 != bitpos1
8753                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
8754                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
8755                 fold_overflow_warning (("assuming pointer wraparound does not "
8756                                         "occur when comparing P +- C1 with "
8757                                         "P +- C2"),
8758                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
8759
8760               switch (code)
8761                 {
8762                 case EQ_EXPR:
8763                   return constant_boolean_node (bitpos0 == bitpos1, type);
8764                 case NE_EXPR:
8765                   return constant_boolean_node (bitpos0 != bitpos1, type);
8766                 case LT_EXPR:
8767                   return constant_boolean_node (bitpos0 < bitpos1, type);
8768                 case LE_EXPR:
8769                   return constant_boolean_node (bitpos0 <= bitpos1, type);
8770                 case GE_EXPR:
8771                   return constant_boolean_node (bitpos0 >= bitpos1, type);
8772                 case GT_EXPR:
8773                   return constant_boolean_node (bitpos0 > bitpos1, type);
8774                 default:;
8775                 }
8776             }
8777           /* We can simplify the comparison to a comparison of the variable
8778              offset parts if the constant offset parts are equal.
8779              Be careful to use signed size type here because otherwise we
8780              mess with array offsets in the wrong way.  This is possible
8781              because pointer arithmetic is restricted to retain within an
8782              object and overflow on pointer differences is undefined as of
8783              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
8784           else if (bitpos0 == bitpos1
8785                    && ((code == EQ_EXPR || code == NE_EXPR)
8786                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
8787             {
8788               /* By converting to signed size type we cover middle-end pointer
8789                  arithmetic which operates on unsigned pointer types of size
8790                  type size and ARRAY_REF offsets which are properly sign or
8791                  zero extended from their type in case it is narrower than
8792                  size type.  */
8793               if (offset0 == NULL_TREE)
8794                 offset0 = build_int_cst (ssizetype, 0);
8795               else
8796                 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8797               if (offset1 == NULL_TREE)
8798                 offset1 = build_int_cst (ssizetype, 0);
8799               else
8800                 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8801
8802               if (code != EQ_EXPR
8803                   && code != NE_EXPR
8804                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
8805                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
8806                 fold_overflow_warning (("assuming pointer wraparound does not "
8807                                         "occur when comparing P +- C1 with "
8808                                         "P +- C2"),
8809                                        WARN_STRICT_OVERFLOW_COMPARISON);
8810
8811               return fold_build2_loc (loc, code, type, offset0, offset1);
8812             }
8813         }
8814       /* For non-equal bases we can simplify if they are addresses
8815          of local binding decls or constants.  */
8816       else if (indirect_base0 && indirect_base1
8817                /* We know that !operand_equal_p (base0, base1, 0)
8818                   because the if condition was false.  But make
8819                   sure two decls are not the same.  */
8820                && base0 != base1
8821                && TREE_CODE (arg0) == ADDR_EXPR
8822                && TREE_CODE (arg1) == ADDR_EXPR
8823                && (((TREE_CODE (base0) == VAR_DECL
8824                      || TREE_CODE (base0) == PARM_DECL)
8825                     && (targetm.binds_local_p (base0)
8826                         || CONSTANT_CLASS_P (base1)))
8827                    || CONSTANT_CLASS_P (base0))
8828                && (((TREE_CODE (base1) == VAR_DECL
8829                      || TREE_CODE (base1) == PARM_DECL)
8830                     && (targetm.binds_local_p (base1)
8831                         || CONSTANT_CLASS_P (base0)))
8832                    || CONSTANT_CLASS_P (base1)))
8833         {
8834           if (code == EQ_EXPR)
8835             return omit_two_operands_loc (loc, type, boolean_false_node,
8836                                       arg0, arg1);
8837           else if (code == NE_EXPR)
8838             return omit_two_operands_loc (loc, type, boolean_true_node,
8839                                       arg0, arg1);
8840         }
8841       /* For equal offsets we can simplify to a comparison of the
8842          base addresses.  */
8843       else if (bitpos0 == bitpos1
8844                && (indirect_base0
8845                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8846                && (indirect_base1
8847                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8848                && ((offset0 == offset1)
8849                    || (offset0 && offset1
8850                        && operand_equal_p (offset0, offset1, 0))))
8851         {
8852           if (indirect_base0)
8853             base0 = build_fold_addr_expr_loc (loc, base0);
8854           if (indirect_base1)
8855             base1 = build_fold_addr_expr_loc (loc, base1);
8856           return fold_build2_loc (loc, code, type, base0, base1);
8857         }
8858     }
8859
8860   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8861      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
8862      the resulting offset is smaller in absolute value than the
8863      original one.  */
8864   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8865       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8866       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8867           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8868       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8869       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8870           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8871     {
8872       tree const1 = TREE_OPERAND (arg0, 1);
8873       tree const2 = TREE_OPERAND (arg1, 1);
8874       tree variable1 = TREE_OPERAND (arg0, 0);
8875       tree variable2 = TREE_OPERAND (arg1, 0);
8876       tree cst;
8877       const char * const warnmsg = G_("assuming signed overflow does not "
8878                                       "occur when combining constants around "
8879                                       "a comparison");
8880
8881       /* Put the constant on the side where it doesn't overflow and is
8882          of lower absolute value than before.  */
8883       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8884                              ? MINUS_EXPR : PLUS_EXPR,
8885                              const2, const1, 0);
8886       if (!TREE_OVERFLOW (cst)
8887           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8888         {
8889           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8890           return fold_build2_loc (loc, code, type,
8891                               variable1,
8892                               fold_build2_loc (loc,
8893                                            TREE_CODE (arg1), TREE_TYPE (arg1),
8894                                            variable2, cst));
8895         }
8896
8897       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8898                              ? MINUS_EXPR : PLUS_EXPR,
8899                              const1, const2, 0);
8900       if (!TREE_OVERFLOW (cst)
8901           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8902         {
8903           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8904           return fold_build2_loc (loc, code, type,
8905                               fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
8906                                            variable1, cst),
8907                               variable2);
8908         }
8909     }
8910
8911   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8912      signed arithmetic case.  That form is created by the compiler
8913      often enough for folding it to be of value.  One example is in
8914      computing loop trip counts after Operator Strength Reduction.  */
8915   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8916       && TREE_CODE (arg0) == MULT_EXPR
8917       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8918           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8919       && integer_zerop (arg1))
8920     {
8921       tree const1 = TREE_OPERAND (arg0, 1);
8922       tree const2 = arg1;                       /* zero */
8923       tree variable1 = TREE_OPERAND (arg0, 0);
8924       enum tree_code cmp_code = code;
8925
8926       /* Handle unfolded multiplication by zero.  */
8927       if (integer_zerop (const1))
8928         return fold_build2_loc (loc, cmp_code, type, const1, const2);
8929
8930       fold_overflow_warning (("assuming signed overflow does not occur when "
8931                               "eliminating multiplication in comparison "
8932                               "with zero"),
8933                              WARN_STRICT_OVERFLOW_COMPARISON);
8934
8935       /* If const1 is negative we swap the sense of the comparison.  */
8936       if (tree_int_cst_sgn (const1) < 0)
8937         cmp_code = swap_tree_comparison (cmp_code);
8938
8939       return fold_build2_loc (loc, cmp_code, type, variable1, const2);
8940     }
8941
8942   tem = maybe_canonicalize_comparison (loc, code, type, op0, op1);
8943   if (tem)
8944     return tem;
8945
8946   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8947     {
8948       tree targ0 = strip_float_extensions (arg0);
8949       tree targ1 = strip_float_extensions (arg1);
8950       tree newtype = TREE_TYPE (targ0);
8951
8952       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8953         newtype = TREE_TYPE (targ1);
8954
8955       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
8956       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8957         return fold_build2_loc (loc, code, type,
8958                             fold_convert_loc (loc, newtype, targ0),
8959                             fold_convert_loc (loc, newtype, targ1));
8960
8961       /* (-a) CMP (-b) -> b CMP a  */
8962       if (TREE_CODE (arg0) == NEGATE_EXPR
8963           && TREE_CODE (arg1) == NEGATE_EXPR)
8964         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
8965                             TREE_OPERAND (arg0, 0));
8966
8967       if (TREE_CODE (arg1) == REAL_CST)
8968         {
8969           REAL_VALUE_TYPE cst;
8970           cst = TREE_REAL_CST (arg1);
8971
8972           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
8973           if (TREE_CODE (arg0) == NEGATE_EXPR)
8974             return fold_build2_loc (loc, swap_tree_comparison (code), type,
8975                                 TREE_OPERAND (arg0, 0),
8976                                 build_real (TREE_TYPE (arg1),
8977                                             real_value_negate (&cst)));
8978
8979           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
8980           /* a CMP (-0) -> a CMP 0  */
8981           if (REAL_VALUE_MINUS_ZERO (cst))
8982             return fold_build2_loc (loc, code, type, arg0,
8983                                 build_real (TREE_TYPE (arg1), dconst0));
8984
8985           /* x != NaN is always true, other ops are always false.  */
8986           if (REAL_VALUE_ISNAN (cst)
8987               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8988             {
8989               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8990               return omit_one_operand_loc (loc, type, tem, arg0);
8991             }
8992
8993           /* Fold comparisons against infinity.  */
8994           if (REAL_VALUE_ISINF (cst)
8995               && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
8996             {
8997               tem = fold_inf_compare (loc, code, type, arg0, arg1);
8998               if (tem != NULL_TREE)
8999                 return tem;
9000             }
9001         }
9002
9003       /* If this is a comparison of a real constant with a PLUS_EXPR
9004          or a MINUS_EXPR of a real constant, we can convert it into a
9005          comparison with a revised real constant as long as no overflow
9006          occurs when unsafe_math_optimizations are enabled.  */
9007       if (flag_unsafe_math_optimizations
9008           && TREE_CODE (arg1) == REAL_CST
9009           && (TREE_CODE (arg0) == PLUS_EXPR
9010               || TREE_CODE (arg0) == MINUS_EXPR)
9011           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9012           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9013                                       ? MINUS_EXPR : PLUS_EXPR,
9014                                       arg1, TREE_OPERAND (arg0, 1)))
9015           && !TREE_OVERFLOW (tem))
9016         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9017
9018       /* Likewise, we can simplify a comparison of a real constant with
9019          a MINUS_EXPR whose first operand is also a real constant, i.e.
9020          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
9021          floating-point types only if -fassociative-math is set.  */
9022       if (flag_associative_math
9023           && TREE_CODE (arg1) == REAL_CST
9024           && TREE_CODE (arg0) == MINUS_EXPR
9025           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9026           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9027                                       arg1))
9028           && !TREE_OVERFLOW (tem))
9029         return fold_build2_loc (loc, swap_tree_comparison (code), type,
9030                             TREE_OPERAND (arg0, 1), tem);
9031
9032       /* Fold comparisons against built-in math functions.  */
9033       if (TREE_CODE (arg1) == REAL_CST
9034           && flag_unsafe_math_optimizations
9035           && ! flag_errno_math)
9036         {
9037           enum built_in_function fcode = builtin_mathfn_code (arg0);
9038
9039           if (fcode != END_BUILTINS)
9040             {
9041               tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
9042               if (tem != NULL_TREE)
9043                 return tem;
9044             }
9045         }
9046     }
9047
9048   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9049       && CONVERT_EXPR_P (arg0))
9050     {
9051       /* If we are widening one operand of an integer comparison,
9052          see if the other operand is similarly being widened.  Perhaps we
9053          can do the comparison in the narrower type.  */
9054       tem = fold_widened_comparison (loc, code, type, arg0, arg1);
9055       if (tem)
9056         return tem;
9057
9058       /* Or if we are changing signedness.  */
9059       tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
9060       if (tem)
9061         return tem;
9062     }
9063
9064   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9065      constant, we can simplify it.  */
9066   if (TREE_CODE (arg1) == INTEGER_CST
9067       && (TREE_CODE (arg0) == MIN_EXPR
9068           || TREE_CODE (arg0) == MAX_EXPR)
9069       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9070     {
9071       tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9072       if (tem)
9073         return tem;
9074     }
9075
9076   /* Simplify comparison of something with itself.  (For IEEE
9077      floating-point, we can only do some of these simplifications.)  */
9078   if (operand_equal_p (arg0, arg1, 0))
9079     {
9080       switch (code)
9081         {
9082         case EQ_EXPR:
9083           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9084               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9085             return constant_boolean_node (1, type);
9086           break;
9087
9088         case GE_EXPR:
9089         case LE_EXPR:
9090           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9091               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9092             return constant_boolean_node (1, type);
9093           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9094
9095         case NE_EXPR:
9096           /* For NE, we can only do this simplification if integer
9097              or we don't honor IEEE floating point NaNs.  */
9098           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9099               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9100             break;
9101           /* ... fall through ...  */
9102         case GT_EXPR:
9103         case LT_EXPR:
9104           return constant_boolean_node (0, type);
9105         default:
9106           gcc_unreachable ();
9107         }
9108     }
9109
9110   /* If we are comparing an expression that just has comparisons
9111      of two integer values, arithmetic expressions of those comparisons,
9112      and constants, we can simplify it.  There are only three cases
9113      to check: the two values can either be equal, the first can be
9114      greater, or the second can be greater.  Fold the expression for
9115      those three values.  Since each value must be 0 or 1, we have
9116      eight possibilities, each of which corresponds to the constant 0
9117      or 1 or one of the six possible comparisons.
9118
9119      This handles common cases like (a > b) == 0 but also handles
9120      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9121      occur in macroized code.  */
9122
9123   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9124     {
9125       tree cval1 = 0, cval2 = 0;
9126       int save_p = 0;
9127
9128       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9129           /* Don't handle degenerate cases here; they should already
9130              have been handled anyway.  */
9131           && cval1 != 0 && cval2 != 0
9132           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9133           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9134           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9135           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9136           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9137           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9138                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9139         {
9140           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9141           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9142
9143           /* We can't just pass T to eval_subst in case cval1 or cval2
9144              was the same as ARG1.  */
9145
9146           tree high_result
9147                 = fold_build2_loc (loc, code, type,
9148                                eval_subst (loc, arg0, cval1, maxval,
9149                                            cval2, minval),
9150                                arg1);
9151           tree equal_result
9152                 = fold_build2_loc (loc, code, type,
9153                                eval_subst (loc, arg0, cval1, maxval,
9154                                            cval2, maxval),
9155                                arg1);
9156           tree low_result
9157                 = fold_build2_loc (loc, code, type,
9158                                eval_subst (loc, arg0, cval1, minval,
9159                                            cval2, maxval),
9160                                arg1);
9161
9162           /* All three of these results should be 0 or 1.  Confirm they are.
9163              Then use those values to select the proper code to use.  */
9164
9165           if (TREE_CODE (high_result) == INTEGER_CST
9166               && TREE_CODE (equal_result) == INTEGER_CST
9167               && TREE_CODE (low_result) == INTEGER_CST)
9168             {
9169               /* Make a 3-bit mask with the high-order bit being the
9170                  value for `>', the next for '=', and the low for '<'.  */
9171               switch ((integer_onep (high_result) * 4)
9172                       + (integer_onep (equal_result) * 2)
9173                       + integer_onep (low_result))
9174                 {
9175                 case 0:
9176                   /* Always false.  */
9177                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9178                 case 1:
9179                   code = LT_EXPR;
9180                   break;
9181                 case 2:
9182                   code = EQ_EXPR;
9183                   break;
9184                 case 3:
9185                   code = LE_EXPR;
9186                   break;
9187                 case 4:
9188                   code = GT_EXPR;
9189                   break;
9190                 case 5:
9191                   code = NE_EXPR;
9192                   break;
9193                 case 6:
9194                   code = GE_EXPR;
9195                   break;
9196                 case 7:
9197                   /* Always true.  */
9198                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9199                 }
9200
9201               if (save_p)
9202                 {
9203                   tem = save_expr (build2 (code, type, cval1, cval2));
9204                   SET_EXPR_LOCATION (tem, loc);
9205                   return tem;
9206                 }
9207               return fold_build2_loc (loc, code, type, cval1, cval2);
9208             }
9209         }
9210     }
9211
9212   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9213      into a single range test.  */
9214   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9215        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9216       && TREE_CODE (arg1) == INTEGER_CST
9217       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9218       && !integer_zerop (TREE_OPERAND (arg0, 1))
9219       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9220       && !TREE_OVERFLOW (arg1))
9221     {
9222       tem = fold_div_compare (loc, code, type, arg0, arg1);
9223       if (tem != NULL_TREE)
9224         return tem;
9225     }
9226
9227   /* Fold ~X op ~Y as Y op X.  */
9228   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9229       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9230     {
9231       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9232       return fold_build2_loc (loc, code, type,
9233                           fold_convert_loc (loc, cmp_type,
9234                                             TREE_OPERAND (arg1, 0)),
9235                           TREE_OPERAND (arg0, 0));
9236     }
9237
9238   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9239   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9240       && TREE_CODE (arg1) == INTEGER_CST)
9241     {
9242       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9243       return fold_build2_loc (loc, swap_tree_comparison (code), type,
9244                           TREE_OPERAND (arg0, 0),
9245                           fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9246                                        fold_convert_loc (loc, cmp_type, arg1)));
9247     }
9248
9249   return NULL_TREE;
9250 }
9251
9252
9253 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9254    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9255    argument EXPR represents the expression "z" of type TYPE.  */
9256
9257 static tree
9258 fold_mult_zconjz (location_t loc, tree type, tree expr)
9259 {
9260   tree itype = TREE_TYPE (type);
9261   tree rpart, ipart, tem;
9262
9263   if (TREE_CODE (expr) == COMPLEX_EXPR)
9264     {
9265       rpart = TREE_OPERAND (expr, 0);
9266       ipart = TREE_OPERAND (expr, 1);
9267     }
9268   else if (TREE_CODE (expr) == COMPLEX_CST)
9269     {
9270       rpart = TREE_REALPART (expr);
9271       ipart = TREE_IMAGPART (expr);
9272     }
9273   else
9274     {
9275       expr = save_expr (expr);
9276       rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9277       ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9278     }
9279
9280   rpart = save_expr (rpart);
9281   ipart = save_expr (ipart);
9282   tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9283                      fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9284                      fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9285   return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9286                       fold_convert_loc (loc, itype, integer_zero_node));
9287 }
9288
9289
9290 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9291    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9292    guarantees that P and N have the same least significant log2(M) bits.
9293    N is not otherwise constrained.  In particular, N is not normalized to
9294    0 <= N < M as is common.  In general, the precise value of P is unknown.
9295    M is chosen as large as possible such that constant N can be determined.
9296
9297    Returns M and sets *RESIDUE to N.
9298
9299    If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9300    account.  This is not always possible due to PR 35705.
9301  */
9302
9303 static unsigned HOST_WIDE_INT
9304 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9305                                  bool allow_func_align)
9306 {
9307   enum tree_code code;
9308
9309   *residue = 0;
9310
9311   code = TREE_CODE (expr);
9312   if (code == ADDR_EXPR)
9313     {
9314       expr = TREE_OPERAND (expr, 0);
9315       if (handled_component_p (expr))
9316         {
9317           HOST_WIDE_INT bitsize, bitpos;
9318           tree offset;
9319           enum machine_mode mode;
9320           int unsignedp, volatilep;
9321
9322           expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9323                                       &mode, &unsignedp, &volatilep, false);
9324           *residue = bitpos / BITS_PER_UNIT;
9325           if (offset)
9326             {
9327               if (TREE_CODE (offset) == INTEGER_CST)
9328                 *residue += TREE_INT_CST_LOW (offset);
9329               else
9330                 /* We don't handle more complicated offset expressions.  */
9331                 return 1;
9332             }
9333         }
9334
9335       if (DECL_P (expr)
9336           && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL))
9337         return DECL_ALIGN_UNIT (expr);
9338     }
9339   else if (code == POINTER_PLUS_EXPR)
9340     {
9341       tree op0, op1;
9342       unsigned HOST_WIDE_INT modulus;
9343       enum tree_code inner_code;
9344
9345       op0 = TREE_OPERAND (expr, 0);
9346       STRIP_NOPS (op0);
9347       modulus = get_pointer_modulus_and_residue (op0, residue,
9348                                                  allow_func_align);
9349
9350       op1 = TREE_OPERAND (expr, 1);
9351       STRIP_NOPS (op1);
9352       inner_code = TREE_CODE (op1);
9353       if (inner_code == INTEGER_CST)
9354         {
9355           *residue += TREE_INT_CST_LOW (op1);
9356           return modulus;
9357         }
9358       else if (inner_code == MULT_EXPR)
9359         {
9360           op1 = TREE_OPERAND (op1, 1);
9361           if (TREE_CODE (op1) == INTEGER_CST)
9362             {
9363               unsigned HOST_WIDE_INT align;
9364
9365               /* Compute the greatest power-of-2 divisor of op1.  */
9366               align = TREE_INT_CST_LOW (op1);
9367               align &= -align;
9368
9369               /* If align is non-zero and less than *modulus, replace
9370                  *modulus with align., If align is 0, then either op1 is 0
9371                  or the greatest power-of-2 divisor of op1 doesn't fit in an
9372                  unsigned HOST_WIDE_INT.  In either case, no additional
9373                  constraint is imposed.  */
9374               if (align)
9375                 modulus = MIN (modulus, align);
9376
9377               return modulus;
9378             }
9379         }
9380     }
9381
9382     /* If we get here, we were unable to determine anything useful about the
9383        expression.  */
9384     return 1;
9385 }
9386
9387
9388 /* Fold a binary expression of code CODE and type TYPE with operands
9389    OP0 and OP1.  LOC is the location of the resulting expression.
9390    Return the folded expression if folding is successful.  Otherwise,
9391    return NULL_TREE.  */
9392
9393 tree
9394 fold_binary_loc (location_t loc,
9395              enum tree_code code, tree type, tree op0, tree op1)
9396 {
9397   enum tree_code_class kind = TREE_CODE_CLASS (code);
9398   tree arg0, arg1, tem;
9399   tree t1 = NULL_TREE;
9400   bool strict_overflow_p;
9401
9402   gcc_assert (IS_EXPR_CODE_CLASS (kind)
9403               && TREE_CODE_LENGTH (code) == 2
9404               && op0 != NULL_TREE
9405               && op1 != NULL_TREE);
9406
9407   arg0 = op0;
9408   arg1 = op1;
9409
9410   /* Strip any conversions that don't change the mode.  This is
9411      safe for every expression, except for a comparison expression
9412      because its signedness is derived from its operands.  So, in
9413      the latter case, only strip conversions that don't change the
9414      signedness.  MIN_EXPR/MAX_EXPR also need signedness of arguments
9415      preserved.
9416
9417      Note that this is done as an internal manipulation within the
9418      constant folder, in order to find the simplest representation
9419      of the arguments so that their form can be studied.  In any
9420      cases, the appropriate type conversions should be put back in
9421      the tree that will get out of the constant folder.  */
9422
9423   if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9424     {
9425       STRIP_SIGN_NOPS (arg0);
9426       STRIP_SIGN_NOPS (arg1);
9427     }
9428   else
9429     {
9430       STRIP_NOPS (arg0);
9431       STRIP_NOPS (arg1);
9432     }
9433
9434   /* Note that TREE_CONSTANT isn't enough: static var addresses are
9435      constant but we can't do arithmetic on them.  */
9436   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9437       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9438       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9439       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9440       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9441       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9442     {
9443       if (kind == tcc_binary)
9444         {
9445           /* Make sure type and arg0 have the same saturating flag.  */
9446           gcc_assert (TYPE_SATURATING (type)
9447                       == TYPE_SATURATING (TREE_TYPE (arg0)));
9448           tem = const_binop (code, arg0, arg1);
9449         }
9450       else if (kind == tcc_comparison)
9451         tem = fold_relational_const (code, type, arg0, arg1);
9452       else
9453         tem = NULL_TREE;
9454
9455       if (tem != NULL_TREE)
9456         {
9457           if (TREE_TYPE (tem) != type)
9458             tem = fold_convert_loc (loc, type, tem);
9459           return tem;
9460         }
9461     }
9462
9463   /* If this is a commutative operation, and ARG0 is a constant, move it
9464      to ARG1 to reduce the number of tests below.  */
9465   if (commutative_tree_code (code)
9466       && tree_swap_operands_p (arg0, arg1, true))
9467     return fold_build2_loc (loc, code, type, op1, op0);
9468
9469   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9470
9471      First check for cases where an arithmetic operation is applied to a
9472      compound, conditional, or comparison operation.  Push the arithmetic
9473      operation inside the compound or conditional to see if any folding
9474      can then be done.  Convert comparison to conditional for this purpose.
9475      The also optimizes non-constant cases that used to be done in
9476      expand_expr.
9477
9478      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9479      one of the operands is a comparison and the other is a comparison, a
9480      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
9481      code below would make the expression more complex.  Change it to a
9482      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
9483      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
9484
9485   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9486        || code == EQ_EXPR || code == NE_EXPR)
9487       && ((truth_value_p (TREE_CODE (arg0))
9488            && (truth_value_p (TREE_CODE (arg1))
9489                || (TREE_CODE (arg1) == BIT_AND_EXPR
9490                    && integer_onep (TREE_OPERAND (arg1, 1)))))
9491           || (truth_value_p (TREE_CODE (arg1))
9492               && (truth_value_p (TREE_CODE (arg0))
9493                   || (TREE_CODE (arg0) == BIT_AND_EXPR
9494                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
9495     {
9496       tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9497                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9498                          : TRUTH_XOR_EXPR,
9499                          boolean_type_node,
9500                          fold_convert_loc (loc, boolean_type_node, arg0),
9501                          fold_convert_loc (loc, boolean_type_node, arg1));
9502
9503       if (code == EQ_EXPR)
9504         tem = invert_truthvalue_loc (loc, tem);
9505
9506       return fold_convert_loc (loc, type, tem);
9507     }
9508
9509   if (TREE_CODE_CLASS (code) == tcc_binary
9510       || TREE_CODE_CLASS (code) == tcc_comparison)
9511     {
9512       if (TREE_CODE (arg0) == COMPOUND_EXPR)
9513         {
9514           tem = fold_build2_loc (loc, code, type,
9515                              fold_convert_loc (loc, TREE_TYPE (op0),
9516                                                TREE_OPERAND (arg0, 1)), op1);
9517           tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), tem);
9518           goto fold_binary_exit;
9519         }
9520       if (TREE_CODE (arg1) == COMPOUND_EXPR
9521           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9522         {
9523           tem = fold_build2_loc (loc, code, type, op0,
9524                              fold_convert_loc (loc, TREE_TYPE (op1),
9525                                                TREE_OPERAND (arg1, 1)));
9526           tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0), tem);
9527           goto fold_binary_exit;
9528         }
9529
9530       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9531         {
9532           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9533                                                      arg0, arg1,
9534                                                      /*cond_first_p=*/1);
9535           if (tem != NULL_TREE)
9536             return tem;
9537         }
9538
9539       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9540         {
9541           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9542                                                      arg1, arg0,
9543                                                      /*cond_first_p=*/0);
9544           if (tem != NULL_TREE)
9545             return tem;
9546         }
9547     }
9548
9549   switch (code)
9550     {
9551     case MEM_REF:
9552       /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2].  */
9553       if (TREE_CODE (arg0) == ADDR_EXPR
9554           && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9555         {
9556           tree iref = TREE_OPERAND (arg0, 0);
9557           return fold_build2 (MEM_REF, type,
9558                               TREE_OPERAND (iref, 0),
9559                               int_const_binop (PLUS_EXPR, arg1,
9560                                                TREE_OPERAND (iref, 1), 0));
9561         }
9562
9563       /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2].  */
9564       if (TREE_CODE (arg0) == ADDR_EXPR
9565           && handled_component_p (TREE_OPERAND (arg0, 0)))
9566         {
9567           tree base;
9568           HOST_WIDE_INT coffset;
9569           base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9570                                                 &coffset);
9571           if (!base)
9572             return NULL_TREE;
9573           return fold_build2 (MEM_REF, type,
9574                               build_fold_addr_expr (base),
9575                               int_const_binop (PLUS_EXPR, arg1,
9576                                                size_int (coffset), 0));
9577         }
9578
9579       return NULL_TREE;
9580
9581     case POINTER_PLUS_EXPR:
9582       /* 0 +p index -> (type)index */
9583       if (integer_zerop (arg0))
9584         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9585
9586       /* PTR +p 0 -> PTR */
9587       if (integer_zerop (arg1))
9588         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9589
9590       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
9591       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9592            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9593         return fold_convert_loc (loc, type,
9594                                  fold_build2_loc (loc, PLUS_EXPR, sizetype,
9595                                               fold_convert_loc (loc, sizetype,
9596                                                                 arg1),
9597                                               fold_convert_loc (loc, sizetype,
9598                                                                 arg0)));
9599
9600       /* index +p PTR -> PTR +p index */
9601       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9602           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9603         return fold_build2_loc (loc, POINTER_PLUS_EXPR, type,
9604                             fold_convert_loc (loc, type, arg1),
9605                             fold_convert_loc (loc, sizetype, arg0));
9606
9607       /* (PTR +p B) +p A -> PTR +p (B + A) */
9608       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9609         {
9610           tree inner;
9611           tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
9612           tree arg00 = TREE_OPERAND (arg0, 0);
9613           inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
9614                                arg01, fold_convert_loc (loc, sizetype, arg1));
9615           return fold_convert_loc (loc, type,
9616                                    fold_build2_loc (loc, POINTER_PLUS_EXPR,
9617                                                 TREE_TYPE (arg00),
9618                                                 arg00, inner));
9619         }
9620
9621       /* PTR_CST +p CST -> CST1 */
9622       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9623         return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
9624                             fold_convert_loc (loc, type, arg1));
9625
9626      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9627         of the array.  Loop optimizer sometimes produce this type of
9628         expressions.  */
9629       if (TREE_CODE (arg0) == ADDR_EXPR)
9630         {
9631           tem = try_move_mult_to_index (loc, arg0,
9632                                         fold_convert_loc (loc, sizetype, arg1));
9633           if (tem)
9634             return fold_convert_loc (loc, type, tem);
9635         }
9636
9637       return NULL_TREE;
9638
9639     case PLUS_EXPR:
9640       /* A + (-B) -> A - B */
9641       if (TREE_CODE (arg1) == NEGATE_EXPR)
9642         return fold_build2_loc (loc, MINUS_EXPR, type,
9643                             fold_convert_loc (loc, type, arg0),
9644                             fold_convert_loc (loc, type,
9645                                               TREE_OPERAND (arg1, 0)));
9646       /* (-A) + B -> B - A */
9647       if (TREE_CODE (arg0) == NEGATE_EXPR
9648           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9649         return fold_build2_loc (loc, MINUS_EXPR, type,
9650                             fold_convert_loc (loc, type, arg1),
9651                             fold_convert_loc (loc, type,
9652                                               TREE_OPERAND (arg0, 0)));
9653
9654       if (INTEGRAL_TYPE_P (type))
9655         {
9656           /* Convert ~A + 1 to -A.  */
9657           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9658               && integer_onep (arg1))
9659             return fold_build1_loc (loc, NEGATE_EXPR, type,
9660                                 fold_convert_loc (loc, type,
9661                                                   TREE_OPERAND (arg0, 0)));
9662
9663           /* ~X + X is -1.  */
9664           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9665               && !TYPE_OVERFLOW_TRAPS (type))
9666             {
9667               tree tem = TREE_OPERAND (arg0, 0);
9668
9669               STRIP_NOPS (tem);
9670               if (operand_equal_p (tem, arg1, 0))
9671                 {
9672                   t1 = build_int_cst_type (type, -1);
9673                   return omit_one_operand_loc (loc, type, t1, arg1);
9674                 }
9675             }
9676
9677           /* X + ~X is -1.  */
9678           if (TREE_CODE (arg1) == BIT_NOT_EXPR
9679               && !TYPE_OVERFLOW_TRAPS (type))
9680             {
9681               tree tem = TREE_OPERAND (arg1, 0);
9682
9683               STRIP_NOPS (tem);
9684               if (operand_equal_p (arg0, tem, 0))
9685                 {
9686                   t1 = build_int_cst_type (type, -1);
9687                   return omit_one_operand_loc (loc, type, t1, arg0);
9688                 }
9689             }
9690
9691           /* X + (X / CST) * -CST is X % CST.  */
9692           if (TREE_CODE (arg1) == MULT_EXPR
9693               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9694               && operand_equal_p (arg0,
9695                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9696             {
9697               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9698               tree cst1 = TREE_OPERAND (arg1, 1);
9699               tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9700                                       cst1, cst0);
9701               if (sum && integer_zerop (sum))
9702                 return fold_convert_loc (loc, type,
9703                                          fold_build2_loc (loc, TRUNC_MOD_EXPR,
9704                                                       TREE_TYPE (arg0), arg0,
9705                                                       cst0));
9706             }
9707         }
9708
9709       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9710          same or one.  Make sure type is not saturating.
9711          fold_plusminus_mult_expr will re-associate.  */
9712       if ((TREE_CODE (arg0) == MULT_EXPR
9713            || TREE_CODE (arg1) == MULT_EXPR)
9714           && !TYPE_SATURATING (type)
9715           && (!FLOAT_TYPE_P (type) || flag_associative_math))
9716         {
9717           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9718           if (tem)
9719             return tem;
9720         }
9721
9722       if (! FLOAT_TYPE_P (type))
9723         {
9724           if (integer_zerop (arg1))
9725             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9726
9727           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9728              with a constant, and the two constants have no bits in common,
9729              we should treat this as a BIT_IOR_EXPR since this may produce more
9730              simplifications.  */
9731           if (TREE_CODE (arg0) == BIT_AND_EXPR
9732               && TREE_CODE (arg1) == BIT_AND_EXPR
9733               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9734               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9735               && integer_zerop (const_binop (BIT_AND_EXPR,
9736                                              TREE_OPERAND (arg0, 1),
9737                                              TREE_OPERAND (arg1, 1))))
9738             {
9739               code = BIT_IOR_EXPR;
9740               goto bit_ior;
9741             }
9742
9743           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9744              (plus (plus (mult) (mult)) (foo)) so that we can
9745              take advantage of the factoring cases below.  */
9746           if (((TREE_CODE (arg0) == PLUS_EXPR
9747                 || TREE_CODE (arg0) == MINUS_EXPR)
9748                && TREE_CODE (arg1) == MULT_EXPR)
9749               || ((TREE_CODE (arg1) == PLUS_EXPR
9750                    || TREE_CODE (arg1) == MINUS_EXPR)
9751                   && TREE_CODE (arg0) == MULT_EXPR))
9752             {
9753               tree parg0, parg1, parg, marg;
9754               enum tree_code pcode;
9755
9756               if (TREE_CODE (arg1) == MULT_EXPR)
9757                 parg = arg0, marg = arg1;
9758               else
9759                 parg = arg1, marg = arg0;
9760               pcode = TREE_CODE (parg);
9761               parg0 = TREE_OPERAND (parg, 0);
9762               parg1 = TREE_OPERAND (parg, 1);
9763               STRIP_NOPS (parg0);
9764               STRIP_NOPS (parg1);
9765
9766               if (TREE_CODE (parg0) == MULT_EXPR
9767                   && TREE_CODE (parg1) != MULT_EXPR)
9768                 return fold_build2_loc (loc, pcode, type,
9769                                     fold_build2_loc (loc, PLUS_EXPR, type,
9770                                                  fold_convert_loc (loc, type,
9771                                                                    parg0),
9772                                                  fold_convert_loc (loc, type,
9773                                                                    marg)),
9774                                     fold_convert_loc (loc, type, parg1));
9775               if (TREE_CODE (parg0) != MULT_EXPR
9776                   && TREE_CODE (parg1) == MULT_EXPR)
9777                 return
9778                   fold_build2_loc (loc, PLUS_EXPR, type,
9779                                fold_convert_loc (loc, type, parg0),
9780                                fold_build2_loc (loc, pcode, type,
9781                                             fold_convert_loc (loc, type, marg),
9782                                             fold_convert_loc (loc, type,
9783                                                               parg1)));
9784             }
9785         }
9786       else
9787         {
9788           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
9789           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9790             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9791
9792           /* Likewise if the operands are reversed.  */
9793           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9794             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9795
9796           /* Convert X + -C into X - C.  */
9797           if (TREE_CODE (arg1) == REAL_CST
9798               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9799             {
9800               tem = fold_negate_const (arg1, type);
9801               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9802                 return fold_build2_loc (loc, MINUS_EXPR, type,
9803                                     fold_convert_loc (loc, type, arg0),
9804                                     fold_convert_loc (loc, type, tem));
9805             }
9806
9807           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9808              to __complex__ ( x, y ).  This is not the same for SNaNs or
9809              if signed zeros are involved.  */
9810           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9811               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9812               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9813             {
9814               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9815               tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9816               tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9817               bool arg0rz = false, arg0iz = false;
9818               if ((arg0r && (arg0rz = real_zerop (arg0r)))
9819                   || (arg0i && (arg0iz = real_zerop (arg0i))))
9820                 {
9821                   tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9822                   tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9823                   if (arg0rz && arg1i && real_zerop (arg1i))
9824                     {
9825                       tree rp = arg1r ? arg1r
9826                                   : build1 (REALPART_EXPR, rtype, arg1);
9827                       tree ip = arg0i ? arg0i
9828                                   : build1 (IMAGPART_EXPR, rtype, arg0);
9829                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9830                     }
9831                   else if (arg0iz && arg1r && real_zerop (arg1r))
9832                     {
9833                       tree rp = arg0r ? arg0r
9834                                   : build1 (REALPART_EXPR, rtype, arg0);
9835                       tree ip = arg1i ? arg1i
9836                                   : build1 (IMAGPART_EXPR, rtype, arg1);
9837                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9838                     }
9839                 }
9840             }
9841
9842           if (flag_unsafe_math_optimizations
9843               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9844               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9845               && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9846             return tem;
9847
9848           /* Convert x+x into x*2.0.  */
9849           if (operand_equal_p (arg0, arg1, 0)
9850               && SCALAR_FLOAT_TYPE_P (type))
9851             return fold_build2_loc (loc, MULT_EXPR, type, arg0,
9852                                 build_real (type, dconst2));
9853
9854           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9855              We associate floats only if the user has specified
9856              -fassociative-math.  */
9857           if (flag_associative_math
9858               && TREE_CODE (arg1) == PLUS_EXPR
9859               && TREE_CODE (arg0) != MULT_EXPR)
9860             {
9861               tree tree10 = TREE_OPERAND (arg1, 0);
9862               tree tree11 = TREE_OPERAND (arg1, 1);
9863               if (TREE_CODE (tree11) == MULT_EXPR
9864                   && TREE_CODE (tree10) == MULT_EXPR)
9865                 {
9866                   tree tree0;
9867                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9868                   return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9869                 }
9870             }
9871           /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9872              We associate floats only if the user has specified
9873              -fassociative-math.  */
9874           if (flag_associative_math
9875               && TREE_CODE (arg0) == PLUS_EXPR
9876               && TREE_CODE (arg1) != MULT_EXPR)
9877             {
9878               tree tree00 = TREE_OPERAND (arg0, 0);
9879               tree tree01 = TREE_OPERAND (arg0, 1);
9880               if (TREE_CODE (tree01) == MULT_EXPR
9881                   && TREE_CODE (tree00) == MULT_EXPR)
9882                 {
9883                   tree tree0;
9884                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9885                   return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9886                 }
9887             }
9888         }
9889
9890      bit_rotate:
9891       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9892          is a rotate of A by C1 bits.  */
9893       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9894          is a rotate of A by B bits.  */
9895       {
9896         enum tree_code code0, code1;
9897         tree rtype;
9898         code0 = TREE_CODE (arg0);
9899         code1 = TREE_CODE (arg1);
9900         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9901              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9902             && operand_equal_p (TREE_OPERAND (arg0, 0),
9903                                 TREE_OPERAND (arg1, 0), 0)
9904             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9905                 TYPE_UNSIGNED (rtype))
9906             /* Only create rotates in complete modes.  Other cases are not
9907                expanded properly.  */
9908             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
9909           {
9910             tree tree01, tree11;
9911             enum tree_code code01, code11;
9912
9913             tree01 = TREE_OPERAND (arg0, 1);
9914             tree11 = TREE_OPERAND (arg1, 1);
9915             STRIP_NOPS (tree01);
9916             STRIP_NOPS (tree11);
9917             code01 = TREE_CODE (tree01);
9918             code11 = TREE_CODE (tree11);
9919             if (code01 == INTEGER_CST
9920                 && code11 == INTEGER_CST
9921                 && TREE_INT_CST_HIGH (tree01) == 0
9922                 && TREE_INT_CST_HIGH (tree11) == 0
9923                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9924                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9925               {
9926                 tem = build2 (LROTATE_EXPR,
9927                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
9928                               TREE_OPERAND (arg0, 0),
9929                               code0 == LSHIFT_EXPR
9930                               ? tree01 : tree11);
9931                 SET_EXPR_LOCATION (tem, loc);
9932                 return fold_convert_loc (loc, type, tem);
9933               }
9934             else if (code11 == MINUS_EXPR)
9935               {
9936                 tree tree110, tree111;
9937                 tree110 = TREE_OPERAND (tree11, 0);
9938                 tree111 = TREE_OPERAND (tree11, 1);
9939                 STRIP_NOPS (tree110);
9940                 STRIP_NOPS (tree111);
9941                 if (TREE_CODE (tree110) == INTEGER_CST
9942                     && 0 == compare_tree_int (tree110,
9943                                               TYPE_PRECISION
9944                                               (TREE_TYPE (TREE_OPERAND
9945                                                           (arg0, 0))))
9946                     && operand_equal_p (tree01, tree111, 0))
9947                   return
9948                     fold_convert_loc (loc, type,
9949                                       build2 ((code0 == LSHIFT_EXPR
9950                                                ? LROTATE_EXPR
9951                                                : RROTATE_EXPR),
9952                                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
9953                                               TREE_OPERAND (arg0, 0), tree01));
9954               }
9955             else if (code01 == MINUS_EXPR)
9956               {
9957                 tree tree010, tree011;
9958                 tree010 = TREE_OPERAND (tree01, 0);
9959                 tree011 = TREE_OPERAND (tree01, 1);
9960                 STRIP_NOPS (tree010);
9961                 STRIP_NOPS (tree011);
9962                 if (TREE_CODE (tree010) == INTEGER_CST
9963                     && 0 == compare_tree_int (tree010,
9964                                               TYPE_PRECISION
9965                                               (TREE_TYPE (TREE_OPERAND
9966                                                           (arg0, 0))))
9967                     && operand_equal_p (tree11, tree011, 0))
9968                     return fold_convert_loc
9969                       (loc, type,
9970                        build2 ((code0 != LSHIFT_EXPR
9971                                 ? LROTATE_EXPR
9972                                 : RROTATE_EXPR),
9973                                TREE_TYPE (TREE_OPERAND (arg0, 0)),
9974                                TREE_OPERAND (arg0, 0), tree11));
9975               }
9976           }
9977       }
9978
9979     associate:
9980       /* In most languages, can't associate operations on floats through
9981          parentheses.  Rather than remember where the parentheses were, we
9982          don't associate floats at all, unless the user has specified
9983          -fassociative-math.
9984          And, we need to make sure type is not saturating.  */
9985
9986       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9987           && !TYPE_SATURATING (type))
9988         {
9989           tree var0, con0, lit0, minus_lit0;
9990           tree var1, con1, lit1, minus_lit1;
9991           bool ok = true;
9992
9993           /* Split both trees into variables, constants, and literals.  Then
9994              associate each group together, the constants with literals,
9995              then the result with variables.  This increases the chances of
9996              literals being recombined later and of generating relocatable
9997              expressions for the sum of a constant and literal.  */
9998           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9999           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10000                              code == MINUS_EXPR);
10001
10002           /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
10003           if (code == MINUS_EXPR)
10004             code = PLUS_EXPR;
10005
10006           /* With undefined overflow we can only associate constants with one
10007              variable, and constants whose association doesn't overflow.  */
10008           if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10009               || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10010             {
10011               if (var0 && var1)
10012                 {
10013                   tree tmp0 = var0;
10014                   tree tmp1 = var1;
10015
10016                   if (TREE_CODE (tmp0) == NEGATE_EXPR)
10017                     tmp0 = TREE_OPERAND (tmp0, 0);
10018                   if (TREE_CODE (tmp1) == NEGATE_EXPR)
10019                     tmp1 = TREE_OPERAND (tmp1, 0);
10020                   /* The only case we can still associate with two variables
10021                      is if they are the same, modulo negation.  */
10022                   if (!operand_equal_p (tmp0, tmp1, 0))
10023                     ok = false;
10024                 }
10025
10026               if (ok && lit0 && lit1)
10027                 {
10028                   tree tmp0 = fold_convert (type, lit0);
10029                   tree tmp1 = fold_convert (type, lit1);
10030
10031                   if (!TREE_OVERFLOW (tmp0) && !TREE_OVERFLOW (tmp1)
10032                       && TREE_OVERFLOW (fold_build2 (code, type, tmp0, tmp1)))
10033                     ok = false;
10034                 }
10035             }
10036
10037           /* Only do something if we found more than two objects.  Otherwise,
10038              nothing has changed and we risk infinite recursion.  */
10039           if (ok
10040               && (2 < ((var0 != 0) + (var1 != 0)
10041                        + (con0 != 0) + (con1 != 0)
10042                        + (lit0 != 0) + (lit1 != 0)
10043                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
10044             {
10045               var0 = associate_trees (loc, var0, var1, code, type);
10046               con0 = associate_trees (loc, con0, con1, code, type);
10047               lit0 = associate_trees (loc, lit0, lit1, code, type);
10048               minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type);
10049
10050               /* Preserve the MINUS_EXPR if the negative part of the literal is
10051                  greater than the positive part.  Otherwise, the multiplicative
10052                  folding code (i.e extract_muldiv) may be fooled in case
10053                  unsigned constants are subtracted, like in the following
10054                  example: ((X*2 + 4) - 8U)/2.  */
10055               if (minus_lit0 && lit0)
10056                 {
10057                   if (TREE_CODE (lit0) == INTEGER_CST
10058                       && TREE_CODE (minus_lit0) == INTEGER_CST
10059                       && tree_int_cst_lt (lit0, minus_lit0))
10060                     {
10061                       minus_lit0 = associate_trees (loc, minus_lit0, lit0,
10062                                                     MINUS_EXPR, type);
10063                       lit0 = 0;
10064                     }
10065                   else
10066                     {
10067                       lit0 = associate_trees (loc, lit0, minus_lit0,
10068                                               MINUS_EXPR, type);
10069                       minus_lit0 = 0;
10070                     }
10071                 }
10072               if (minus_lit0)
10073                 {
10074                   if (con0 == 0)
10075                     return
10076                       fold_convert_loc (loc, type,
10077                                         associate_trees (loc, var0, minus_lit0,
10078                                                          MINUS_EXPR, type));
10079                   else
10080                     {
10081                       con0 = associate_trees (loc, con0, minus_lit0,
10082                                               MINUS_EXPR, type);
10083                       return
10084                         fold_convert_loc (loc, type,
10085                                           associate_trees (loc, var0, con0,
10086                                                            PLUS_EXPR, type));
10087                     }
10088                 }
10089
10090               con0 = associate_trees (loc, con0, lit0, code, type);
10091               return
10092                 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10093                                                               code, type));
10094             }
10095         }
10096
10097       return NULL_TREE;
10098
10099     case MINUS_EXPR:
10100       /* Pointer simplifications for subtraction, simple reassociations. */
10101       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10102         {
10103           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10104           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10105               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10106             {
10107               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10108               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10109               tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10110               tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10111               return fold_build2_loc (loc, PLUS_EXPR, type,
10112                                   fold_build2_loc (loc, MINUS_EXPR, type,
10113                                                arg00, arg10),
10114                                   fold_build2_loc (loc, MINUS_EXPR, type,
10115                                                arg01, arg11));
10116             }
10117           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10118           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10119             {
10120               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10121               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10122               tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10123                                       fold_convert_loc (loc, type, arg1));
10124               if (tmp)
10125                 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10126             }
10127         }
10128       /* A - (-B) -> A + B */
10129       if (TREE_CODE (arg1) == NEGATE_EXPR)
10130         return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10131                             fold_convert_loc (loc, type,
10132                                               TREE_OPERAND (arg1, 0)));
10133       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10134       if (TREE_CODE (arg0) == NEGATE_EXPR
10135           && (FLOAT_TYPE_P (type)
10136               || INTEGRAL_TYPE_P (type))
10137           && negate_expr_p (arg1)
10138           && reorder_operands_p (arg0, arg1))
10139         return fold_build2_loc (loc, MINUS_EXPR, type,
10140                             fold_convert_loc (loc, type,
10141                                               negate_expr (arg1)),
10142                             fold_convert_loc (loc, type,
10143                                               TREE_OPERAND (arg0, 0)));
10144       /* Convert -A - 1 to ~A.  */
10145       if (INTEGRAL_TYPE_P (type)
10146           && TREE_CODE (arg0) == NEGATE_EXPR
10147           && integer_onep (arg1)
10148           && !TYPE_OVERFLOW_TRAPS (type))
10149         return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10150                             fold_convert_loc (loc, type,
10151                                               TREE_OPERAND (arg0, 0)));
10152
10153       /* Convert -1 - A to ~A.  */
10154       if (INTEGRAL_TYPE_P (type)
10155           && integer_all_onesp (arg0))
10156         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10157
10158
10159       /* X - (X / CST) * CST is X % CST.  */
10160       if (INTEGRAL_TYPE_P (type)
10161           && TREE_CODE (arg1) == MULT_EXPR
10162           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10163           && operand_equal_p (arg0,
10164                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10165           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10166                               TREE_OPERAND (arg1, 1), 0))
10167         return
10168           fold_convert_loc (loc, type,
10169                             fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10170                                          arg0, TREE_OPERAND (arg1, 1)));
10171
10172       if (! FLOAT_TYPE_P (type))
10173         {
10174           if (integer_zerop (arg0))
10175             return negate_expr (fold_convert_loc (loc, type, arg1));
10176           if (integer_zerop (arg1))
10177             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10178
10179           /* Fold A - (A & B) into ~B & A.  */
10180           if (!TREE_SIDE_EFFECTS (arg0)
10181               && TREE_CODE (arg1) == BIT_AND_EXPR)
10182             {
10183               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10184                 {
10185                   tree arg10 = fold_convert_loc (loc, type,
10186                                                  TREE_OPERAND (arg1, 0));
10187                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10188                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10189                                                    type, arg10),
10190                                       fold_convert_loc (loc, type, arg0));
10191                 }
10192               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10193                 {
10194                   tree arg11 = fold_convert_loc (loc,
10195                                                  type, TREE_OPERAND (arg1, 1));
10196                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10197                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10198                                                    type, arg11),
10199                                       fold_convert_loc (loc, type, arg0));
10200                 }
10201             }
10202
10203           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10204              any power of 2 minus 1.  */
10205           if (TREE_CODE (arg0) == BIT_AND_EXPR
10206               && TREE_CODE (arg1) == BIT_AND_EXPR
10207               && operand_equal_p (TREE_OPERAND (arg0, 0),
10208                                   TREE_OPERAND (arg1, 0), 0))
10209             {
10210               tree mask0 = TREE_OPERAND (arg0, 1);
10211               tree mask1 = TREE_OPERAND (arg1, 1);
10212               tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10213
10214               if (operand_equal_p (tem, mask1, 0))
10215                 {
10216                   tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10217                                      TREE_OPERAND (arg0, 0), mask1);
10218                   return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10219                 }
10220             }
10221         }
10222
10223       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10224       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10225         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10226
10227       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10228          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10229          (-ARG1 + ARG0) reduces to -ARG1.  */
10230       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10231         return negate_expr (fold_convert_loc (loc, type, arg1));
10232
10233       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10234          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10235          signed zeros are involved.  */
10236       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10237           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10238           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10239         {
10240           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10241           tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10242           tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10243           bool arg0rz = false, arg0iz = false;
10244           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10245               || (arg0i && (arg0iz = real_zerop (arg0i))))
10246             {
10247               tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10248               tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10249               if (arg0rz && arg1i && real_zerop (arg1i))
10250                 {
10251                   tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10252                                          arg1r ? arg1r
10253                                          : build1 (REALPART_EXPR, rtype, arg1));
10254                   tree ip = arg0i ? arg0i
10255                     : build1 (IMAGPART_EXPR, rtype, arg0);
10256                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10257                 }
10258               else if (arg0iz && arg1r && real_zerop (arg1r))
10259                 {
10260                   tree rp = arg0r ? arg0r
10261                     : build1 (REALPART_EXPR, rtype, arg0);
10262                   tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10263                                          arg1i ? arg1i
10264                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10265                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10266                 }
10267             }
10268         }
10269
10270       /* Fold &x - &x.  This can happen from &x.foo - &x.
10271          This is unsafe for certain floats even in non-IEEE formats.
10272          In IEEE, it is unsafe because it does wrong for NaNs.
10273          Also note that operand_equal_p is always false if an operand
10274          is volatile.  */
10275
10276       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10277           && operand_equal_p (arg0, arg1, 0))
10278         return fold_convert_loc (loc, type, integer_zero_node);
10279
10280       /* A - B -> A + (-B) if B is easily negatable.  */
10281       if (negate_expr_p (arg1)
10282           && ((FLOAT_TYPE_P (type)
10283                /* Avoid this transformation if B is a positive REAL_CST.  */
10284                && (TREE_CODE (arg1) != REAL_CST
10285                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10286               || INTEGRAL_TYPE_P (type)))
10287         return fold_build2_loc (loc, PLUS_EXPR, type,
10288                             fold_convert_loc (loc, type, arg0),
10289                             fold_convert_loc (loc, type,
10290                                               negate_expr (arg1)));
10291
10292       /* Try folding difference of addresses.  */
10293       {
10294         HOST_WIDE_INT diff;
10295
10296         if ((TREE_CODE (arg0) == ADDR_EXPR
10297              || TREE_CODE (arg1) == ADDR_EXPR)
10298             && ptr_difference_const (arg0, arg1, &diff))
10299           return build_int_cst_type (type, diff);
10300       }
10301
10302       /* Fold &a[i] - &a[j] to i-j.  */
10303       if (TREE_CODE (arg0) == ADDR_EXPR
10304           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10305           && TREE_CODE (arg1) == ADDR_EXPR
10306           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10307         {
10308           tree aref0 = TREE_OPERAND (arg0, 0);
10309           tree aref1 = TREE_OPERAND (arg1, 0);
10310           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10311                                TREE_OPERAND (aref1, 0), 0))
10312             {
10313               tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10314               tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10315               tree esz = array_ref_element_size (aref0);
10316               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10317               return fold_build2_loc (loc, MULT_EXPR, type, diff,
10318                                   fold_convert_loc (loc, type, esz));
10319
10320             }
10321         }
10322
10323       if (FLOAT_TYPE_P (type)
10324           && flag_unsafe_math_optimizations
10325           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10326           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10327           && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10328         return tem;
10329
10330       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10331          same or one.  Make sure type is not saturating.
10332          fold_plusminus_mult_expr will re-associate.  */
10333       if ((TREE_CODE (arg0) == MULT_EXPR
10334            || TREE_CODE (arg1) == MULT_EXPR)
10335           && !TYPE_SATURATING (type)
10336           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10337         {
10338           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10339           if (tem)
10340             return tem;
10341         }
10342
10343       goto associate;
10344
10345     case MULT_EXPR:
10346       /* (-A) * (-B) -> A * B  */
10347       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10348         return fold_build2_loc (loc, MULT_EXPR, type,
10349                             fold_convert_loc (loc, type,
10350                                               TREE_OPERAND (arg0, 0)),
10351                             fold_convert_loc (loc, type,
10352                                               negate_expr (arg1)));
10353       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10354         return fold_build2_loc (loc, MULT_EXPR, type,
10355                             fold_convert_loc (loc, type,
10356                                               negate_expr (arg0)),
10357                             fold_convert_loc (loc, type,
10358                                               TREE_OPERAND (arg1, 0)));
10359
10360       if (! FLOAT_TYPE_P (type))
10361         {
10362           if (integer_zerop (arg1))
10363             return omit_one_operand_loc (loc, type, arg1, arg0);
10364           if (integer_onep (arg1))
10365             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10366           /* Transform x * -1 into -x.  Make sure to do the negation
10367              on the original operand with conversions not stripped
10368              because we can only strip non-sign-changing conversions.  */
10369           if (integer_all_onesp (arg1))
10370             return fold_convert_loc (loc, type, negate_expr (op0));
10371           /* Transform x * -C into -x * C if x is easily negatable.  */
10372           if (TREE_CODE (arg1) == INTEGER_CST
10373               && tree_int_cst_sgn (arg1) == -1
10374               && negate_expr_p (arg0)
10375               && (tem = negate_expr (arg1)) != arg1
10376               && !TREE_OVERFLOW (tem))
10377             return fold_build2_loc (loc, MULT_EXPR, type,
10378                                 fold_convert_loc (loc, type,
10379                                                   negate_expr (arg0)),
10380                                 tem);
10381
10382           /* (a * (1 << b)) is (a << b)  */
10383           if (TREE_CODE (arg1) == LSHIFT_EXPR
10384               && integer_onep (TREE_OPERAND (arg1, 0)))
10385             return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10386                                 TREE_OPERAND (arg1, 1));
10387           if (TREE_CODE (arg0) == LSHIFT_EXPR
10388               && integer_onep (TREE_OPERAND (arg0, 0)))
10389             return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10390                                 TREE_OPERAND (arg0, 1));
10391
10392           /* (A + A) * C -> A * 2 * C  */
10393           if (TREE_CODE (arg0) == PLUS_EXPR
10394               && TREE_CODE (arg1) == INTEGER_CST
10395               && operand_equal_p (TREE_OPERAND (arg0, 0),
10396                                   TREE_OPERAND (arg0, 1), 0))
10397             return fold_build2_loc (loc, MULT_EXPR, type,
10398                                 omit_one_operand_loc (loc, type,
10399                                                   TREE_OPERAND (arg0, 0),
10400                                                   TREE_OPERAND (arg0, 1)),
10401                                 fold_build2_loc (loc, MULT_EXPR, type,
10402                                              build_int_cst (type, 2) , arg1));
10403
10404           strict_overflow_p = false;
10405           if (TREE_CODE (arg1) == INTEGER_CST
10406               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10407                                              &strict_overflow_p)))
10408             {
10409               if (strict_overflow_p)
10410                 fold_overflow_warning (("assuming signed overflow does not "
10411                                         "occur when simplifying "
10412                                         "multiplication"),
10413                                        WARN_STRICT_OVERFLOW_MISC);
10414               return fold_convert_loc (loc, type, tem);
10415             }
10416
10417           /* Optimize z * conj(z) for integer complex numbers.  */
10418           if (TREE_CODE (arg0) == CONJ_EXPR
10419               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10420             return fold_mult_zconjz (loc, type, arg1);
10421           if (TREE_CODE (arg1) == CONJ_EXPR
10422               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10423             return fold_mult_zconjz (loc, type, arg0);
10424         }
10425       else
10426         {
10427           /* Maybe fold x * 0 to 0.  The expressions aren't the same
10428              when x is NaN, since x * 0 is also NaN.  Nor are they the
10429              same in modes with signed zeros, since multiplying a
10430              negative value by 0 gives -0, not +0.  */
10431           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10432               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10433               && real_zerop (arg1))
10434             return omit_one_operand_loc (loc, type, arg1, arg0);
10435           /* In IEEE floating point, x*1 is not equivalent to x for snans.
10436              Likewise for complex arithmetic with signed zeros.  */
10437           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10438               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10439                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10440               && real_onep (arg1))
10441             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10442
10443           /* Transform x * -1.0 into -x.  */
10444           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10445               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10446                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10447               && real_minus_onep (arg1))
10448             return fold_convert_loc (loc, type, negate_expr (arg0));
10449
10450           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
10451              the result for floating point types due to rounding so it is applied
10452              only if -fassociative-math was specify.  */
10453           if (flag_associative_math
10454               && TREE_CODE (arg0) == RDIV_EXPR
10455               && TREE_CODE (arg1) == REAL_CST
10456               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10457             {
10458               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10459                                       arg1);
10460               if (tem)
10461                 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10462                                     TREE_OPERAND (arg0, 1));
10463             }
10464
10465           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
10466           if (operand_equal_p (arg0, arg1, 0))
10467             {
10468               tree tem = fold_strip_sign_ops (arg0);
10469               if (tem != NULL_TREE)
10470                 {
10471                   tem = fold_convert_loc (loc, type, tem);
10472                   return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
10473                 }
10474             }
10475
10476           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10477              This is not the same for NaNs or if signed zeros are
10478              involved.  */
10479           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10480               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10481               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10482               && TREE_CODE (arg1) == COMPLEX_CST
10483               && real_zerop (TREE_REALPART (arg1)))
10484             {
10485               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10486               if (real_onep (TREE_IMAGPART (arg1)))
10487                 return
10488                   fold_build2_loc (loc, COMPLEX_EXPR, type,
10489                                negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10490                                                              rtype, arg0)),
10491                                fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10492               else if (real_minus_onep (TREE_IMAGPART (arg1)))
10493                 return
10494                   fold_build2_loc (loc, COMPLEX_EXPR, type,
10495                                fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10496                                negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10497                                                              rtype, arg0)));
10498             }
10499
10500           /* Optimize z * conj(z) for floating point complex numbers.
10501              Guarded by flag_unsafe_math_optimizations as non-finite
10502              imaginary components don't produce scalar results.  */
10503           if (flag_unsafe_math_optimizations
10504               && TREE_CODE (arg0) == CONJ_EXPR
10505               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10506             return fold_mult_zconjz (loc, type, arg1);
10507           if (flag_unsafe_math_optimizations
10508               && TREE_CODE (arg1) == CONJ_EXPR
10509               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10510             return fold_mult_zconjz (loc, type, arg0);
10511
10512           if (flag_unsafe_math_optimizations)
10513             {
10514               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10515               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10516
10517               /* Optimizations of root(...)*root(...).  */
10518               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10519                 {
10520                   tree rootfn, arg;
10521                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10522                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10523
10524                   /* Optimize sqrt(x)*sqrt(x) as x.  */
10525                   if (BUILTIN_SQRT_P (fcode0)
10526                       && operand_equal_p (arg00, arg10, 0)
10527                       && ! HONOR_SNANS (TYPE_MODE (type)))
10528                     return arg00;
10529
10530                   /* Optimize root(x)*root(y) as root(x*y).  */
10531                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10532                   arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
10533                   return build_call_expr_loc (loc, rootfn, 1, arg);
10534                 }
10535
10536               /* Optimize expN(x)*expN(y) as expN(x+y).  */
10537               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10538                 {
10539                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10540                   tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10541                                           CALL_EXPR_ARG (arg0, 0),
10542                                           CALL_EXPR_ARG (arg1, 0));
10543                   return build_call_expr_loc (loc, expfn, 1, arg);
10544                 }
10545
10546               /* Optimizations of pow(...)*pow(...).  */
10547               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10548                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10549                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10550                 {
10551                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10552                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10553                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10554                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10555
10556                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
10557                   if (operand_equal_p (arg01, arg11, 0))
10558                     {
10559                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10560                       tree arg = fold_build2_loc (loc, MULT_EXPR, type,
10561                                               arg00, arg10);
10562                       return build_call_expr_loc (loc, powfn, 2, arg, arg01);
10563                     }
10564
10565                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
10566                   if (operand_equal_p (arg00, arg10, 0))
10567                     {
10568                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10569                       tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10570                                               arg01, arg11);
10571                       return build_call_expr_loc (loc, powfn, 2, arg00, arg);
10572                     }
10573                 }
10574
10575               /* Optimize tan(x)*cos(x) as sin(x).  */
10576               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10577                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10578                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10579                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10580                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10581                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10582                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10583                                       CALL_EXPR_ARG (arg1, 0), 0))
10584                 {
10585                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10586
10587                   if (sinfn != NULL_TREE)
10588                     return build_call_expr_loc (loc, sinfn, 1,
10589                                             CALL_EXPR_ARG (arg0, 0));
10590                 }
10591
10592               /* Optimize x*pow(x,c) as pow(x,c+1).  */
10593               if (fcode1 == BUILT_IN_POW
10594                   || fcode1 == BUILT_IN_POWF
10595                   || fcode1 == BUILT_IN_POWL)
10596                 {
10597                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10598                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10599                   if (TREE_CODE (arg11) == REAL_CST
10600                       && !TREE_OVERFLOW (arg11)
10601                       && operand_equal_p (arg0, arg10, 0))
10602                     {
10603                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10604                       REAL_VALUE_TYPE c;
10605                       tree arg;
10606
10607                       c = TREE_REAL_CST (arg11);
10608                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10609                       arg = build_real (type, c);
10610                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10611                     }
10612                 }
10613
10614               /* Optimize pow(x,c)*x as pow(x,c+1).  */
10615               if (fcode0 == BUILT_IN_POW
10616                   || fcode0 == BUILT_IN_POWF
10617                   || fcode0 == BUILT_IN_POWL)
10618                 {
10619                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10620                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10621                   if (TREE_CODE (arg01) == REAL_CST
10622                       && !TREE_OVERFLOW (arg01)
10623                       && operand_equal_p (arg1, arg00, 0))
10624                     {
10625                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10626                       REAL_VALUE_TYPE c;
10627                       tree arg;
10628
10629                       c = TREE_REAL_CST (arg01);
10630                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10631                       arg = build_real (type, c);
10632                       return build_call_expr_loc (loc, powfn, 2, arg1, arg);
10633                     }
10634                 }
10635
10636               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
10637               if (optimize_function_for_speed_p (cfun)
10638                   && operand_equal_p (arg0, arg1, 0))
10639                 {
10640                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10641
10642                   if (powfn)
10643                     {
10644                       tree arg = build_real (type, dconst2);
10645                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10646                     }
10647                 }
10648             }
10649         }
10650       goto associate;
10651
10652     case BIT_IOR_EXPR:
10653     bit_ior:
10654       if (integer_all_onesp (arg1))
10655         return omit_one_operand_loc (loc, type, arg1, arg0);
10656       if (integer_zerop (arg1))
10657         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10658       if (operand_equal_p (arg0, arg1, 0))
10659         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10660
10661       /* ~X | X is -1.  */
10662       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10663           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10664         {
10665           t1 = fold_convert_loc (loc, type, integer_zero_node);
10666           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10667           return omit_one_operand_loc (loc, type, t1, arg1);
10668         }
10669
10670       /* X | ~X is -1.  */
10671       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10672           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10673         {
10674           t1 = fold_convert_loc (loc, type, integer_zero_node);
10675           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10676           return omit_one_operand_loc (loc, type, t1, arg0);
10677         }
10678
10679       /* Canonicalize (X & C1) | C2.  */
10680       if (TREE_CODE (arg0) == BIT_AND_EXPR
10681           && TREE_CODE (arg1) == INTEGER_CST
10682           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10683         {
10684           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10685           int width = TYPE_PRECISION (type), w;
10686           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10687           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10688           hi2 = TREE_INT_CST_HIGH (arg1);
10689           lo2 = TREE_INT_CST_LOW (arg1);
10690
10691           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
10692           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10693             return omit_one_operand_loc (loc, type, arg1,
10694                                      TREE_OPERAND (arg0, 0));
10695
10696           if (width > HOST_BITS_PER_WIDE_INT)
10697             {
10698               mhi = (unsigned HOST_WIDE_INT) -1
10699                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
10700               mlo = -1;
10701             }
10702           else
10703             {
10704               mhi = 0;
10705               mlo = (unsigned HOST_WIDE_INT) -1
10706                     >> (HOST_BITS_PER_WIDE_INT - width);
10707             }
10708
10709           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
10710           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10711             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10712                                 TREE_OPERAND (arg0, 0), arg1);
10713
10714           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10715              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10716              mode which allows further optimizations.  */
10717           hi1 &= mhi;
10718           lo1 &= mlo;
10719           hi2 &= mhi;
10720           lo2 &= mlo;
10721           hi3 = hi1 & ~hi2;
10722           lo3 = lo1 & ~lo2;
10723           for (w = BITS_PER_UNIT;
10724                w <= width && w <= HOST_BITS_PER_WIDE_INT;
10725                w <<= 1)
10726             {
10727               unsigned HOST_WIDE_INT mask
10728                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10729               if (((lo1 | lo2) & mask) == mask
10730                   && (lo1 & ~mask) == 0 && hi1 == 0)
10731                 {
10732                   hi3 = 0;
10733                   lo3 = mask;
10734                   break;
10735                 }
10736             }
10737           if (hi3 != hi1 || lo3 != lo1)
10738             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10739                                 fold_build2_loc (loc, BIT_AND_EXPR, type,
10740                                              TREE_OPERAND (arg0, 0),
10741                                              build_int_cst_wide (type,
10742                                                                  lo3, hi3)),
10743                                 arg1);
10744         }
10745
10746       /* (X & Y) | Y is (X, Y).  */
10747       if (TREE_CODE (arg0) == BIT_AND_EXPR
10748           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10749         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10750       /* (X & Y) | X is (Y, X).  */
10751       if (TREE_CODE (arg0) == BIT_AND_EXPR
10752           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10753           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10754         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
10755       /* X | (X & Y) is (Y, X).  */
10756       if (TREE_CODE (arg1) == BIT_AND_EXPR
10757           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10758           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10759         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
10760       /* X | (Y & X) is (Y, X).  */
10761       if (TREE_CODE (arg1) == BIT_AND_EXPR
10762           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10763           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10764         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
10765
10766       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
10767       if (t1 != NULL_TREE)
10768         return t1;
10769
10770       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10771
10772          This results in more efficient code for machines without a NAND
10773          instruction.  Combine will canonicalize to the first form
10774          which will allow use of NAND instructions provided by the
10775          backend if they exist.  */
10776       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10777           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10778         {
10779           return
10780             fold_build1_loc (loc, BIT_NOT_EXPR, type,
10781                          build2 (BIT_AND_EXPR, type,
10782                                  fold_convert_loc (loc, type,
10783                                                    TREE_OPERAND (arg0, 0)),
10784                                  fold_convert_loc (loc, type,
10785                                                    TREE_OPERAND (arg1, 0))));
10786         }
10787
10788       /* See if this can be simplified into a rotate first.  If that
10789          is unsuccessful continue in the association code.  */
10790       goto bit_rotate;
10791
10792     case BIT_XOR_EXPR:
10793       if (integer_zerop (arg1))
10794         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10795       if (integer_all_onesp (arg1))
10796         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
10797       if (operand_equal_p (arg0, arg1, 0))
10798         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10799
10800       /* ~X ^ X is -1.  */
10801       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10802           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10803         {
10804           t1 = fold_convert_loc (loc, type, integer_zero_node);
10805           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10806           return omit_one_operand_loc (loc, type, t1, arg1);
10807         }
10808
10809       /* X ^ ~X is -1.  */
10810       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10811           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10812         {
10813           t1 = fold_convert_loc (loc, type, integer_zero_node);
10814           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10815           return omit_one_operand_loc (loc, type, t1, arg0);
10816         }
10817
10818       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10819          with a constant, and the two constants have no bits in common,
10820          we should treat this as a BIT_IOR_EXPR since this may produce more
10821          simplifications.  */
10822       if (TREE_CODE (arg0) == BIT_AND_EXPR
10823           && TREE_CODE (arg1) == BIT_AND_EXPR
10824           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10825           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10826           && integer_zerop (const_binop (BIT_AND_EXPR,
10827                                          TREE_OPERAND (arg0, 1),
10828                                          TREE_OPERAND (arg1, 1))))
10829         {
10830           code = BIT_IOR_EXPR;
10831           goto bit_ior;
10832         }
10833
10834       /* (X | Y) ^ X -> Y & ~ X*/
10835       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10836           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10837         {
10838           tree t2 = TREE_OPERAND (arg0, 1);
10839           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10840                             arg1);
10841           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10842                             fold_convert_loc (loc, type, t2),
10843                             fold_convert_loc (loc, type, t1));
10844           return t1;
10845         }
10846
10847       /* (Y | X) ^ X -> Y & ~ X*/
10848       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10849           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10850         {
10851           tree t2 = TREE_OPERAND (arg0, 0);
10852           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10853                             arg1);
10854           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10855                             fold_convert_loc (loc, type, t2),
10856                             fold_convert_loc (loc, type, t1));
10857           return t1;
10858         }
10859
10860       /* X ^ (X | Y) -> Y & ~ X*/
10861       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10862           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10863         {
10864           tree t2 = TREE_OPERAND (arg1, 1);
10865           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10866                             arg0);
10867           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10868                             fold_convert_loc (loc, type, t2),
10869                             fold_convert_loc (loc, type, t1));
10870           return t1;
10871         }
10872
10873       /* X ^ (Y | X) -> Y & ~ X*/
10874       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10875           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10876         {
10877           tree t2 = TREE_OPERAND (arg1, 0);
10878           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10879                             arg0);
10880           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10881                             fold_convert_loc (loc, type, t2),
10882                             fold_convert_loc (loc, type, t1));
10883           return t1;
10884         }
10885
10886       /* Convert ~X ^ ~Y to X ^ Y.  */
10887       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10888           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10889         return fold_build2_loc (loc, code, type,
10890                             fold_convert_loc (loc, type,
10891                                               TREE_OPERAND (arg0, 0)),
10892                             fold_convert_loc (loc, type,
10893                                               TREE_OPERAND (arg1, 0)));
10894
10895       /* Convert ~X ^ C to X ^ ~C.  */
10896       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10897           && TREE_CODE (arg1) == INTEGER_CST)
10898         return fold_build2_loc (loc, code, type,
10899                             fold_convert_loc (loc, type,
10900                                               TREE_OPERAND (arg0, 0)),
10901                             fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
10902
10903       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
10904       if (TREE_CODE (arg0) == BIT_AND_EXPR
10905           && integer_onep (TREE_OPERAND (arg0, 1))
10906           && integer_onep (arg1))
10907         return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10908                             build_int_cst (TREE_TYPE (arg0), 0));
10909
10910       /* Fold (X & Y) ^ Y as ~X & Y.  */
10911       if (TREE_CODE (arg0) == BIT_AND_EXPR
10912           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10913         {
10914           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10915           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10916                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10917                               fold_convert_loc (loc, type, arg1));
10918         }
10919       /* Fold (X & Y) ^ X as ~Y & X.  */
10920       if (TREE_CODE (arg0) == BIT_AND_EXPR
10921           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10922           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10923         {
10924           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10925           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10926                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10927                               fold_convert_loc (loc, type, arg1));
10928         }
10929       /* Fold X ^ (X & Y) as X & ~Y.  */
10930       if (TREE_CODE (arg1) == BIT_AND_EXPR
10931           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10932         {
10933           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10934           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10935                               fold_convert_loc (loc, type, arg0),
10936                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10937         }
10938       /* Fold X ^ (Y & X) as ~Y & X.  */
10939       if (TREE_CODE (arg1) == BIT_AND_EXPR
10940           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10941           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10942         {
10943           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10944           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10945                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10946                               fold_convert_loc (loc, type, arg0));
10947         }
10948
10949       /* See if this can be simplified into a rotate first.  If that
10950          is unsuccessful continue in the association code.  */
10951       goto bit_rotate;
10952
10953     case BIT_AND_EXPR:
10954       if (integer_all_onesp (arg1))
10955         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10956       if (integer_zerop (arg1))
10957         return omit_one_operand_loc (loc, type, arg1, arg0);
10958       if (operand_equal_p (arg0, arg1, 0))
10959         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10960
10961       /* ~X & X is always zero.  */
10962       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10963           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10964         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10965
10966       /* X & ~X is always zero.  */
10967       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10968           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10969         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10970
10971       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
10972       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10973           && TREE_CODE (arg1) == INTEGER_CST
10974           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10975         {
10976           tree tmp1 = fold_convert_loc (loc, type, arg1);
10977           tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10978           tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10979           tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
10980           tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
10981           return
10982             fold_convert_loc (loc, type,
10983                               fold_build2_loc (loc, BIT_IOR_EXPR,
10984                                            type, tmp2, tmp3));
10985         }
10986
10987       /* (X | Y) & Y is (X, Y).  */
10988       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10989           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10990         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10991       /* (X | Y) & X is (Y, X).  */
10992       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10993           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10994           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10995         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
10996       /* X & (X | Y) is (Y, X).  */
10997       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10998           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10999           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11000         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11001       /* X & (Y | X) is (Y, X).  */
11002       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11003           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11004           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11005         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11006
11007       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
11008       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11009           && integer_onep (TREE_OPERAND (arg0, 1))
11010           && integer_onep (arg1))
11011         {
11012           tem = TREE_OPERAND (arg0, 0);
11013           return fold_build2_loc (loc, EQ_EXPR, type,
11014                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11015                                            build_int_cst (TREE_TYPE (tem), 1)),
11016                               build_int_cst (TREE_TYPE (tem), 0));
11017         }
11018       /* Fold ~X & 1 as (X & 1) == 0.  */
11019       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11020           && integer_onep (arg1))
11021         {
11022           tem = TREE_OPERAND (arg0, 0);
11023           return fold_build2_loc (loc, EQ_EXPR, type,
11024                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11025                                            build_int_cst (TREE_TYPE (tem), 1)),
11026                               build_int_cst (TREE_TYPE (tem), 0));
11027         }
11028
11029       /* Fold (X ^ Y) & Y as ~X & Y.  */
11030       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11031           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11032         {
11033           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11034           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11035                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11036                               fold_convert_loc (loc, type, arg1));
11037         }
11038       /* Fold (X ^ Y) & X as ~Y & X.  */
11039       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11040           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11041           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11042         {
11043           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11044           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11045                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11046                               fold_convert_loc (loc, type, arg1));
11047         }
11048       /* Fold X & (X ^ Y) as X & ~Y.  */
11049       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11050           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11051         {
11052           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11053           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11054                               fold_convert_loc (loc, type, arg0),
11055                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11056         }
11057       /* Fold X & (Y ^ X) as ~Y & X.  */
11058       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11059           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11060           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11061         {
11062           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11063           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11064                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11065                               fold_convert_loc (loc, type, arg0));
11066         }
11067
11068       /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11069          ((A & N) + B) & M -> (A + B) & M
11070          Similarly if (N & M) == 0,
11071          ((A | N) + B) & M -> (A + B) & M
11072          and for - instead of + (or unary - instead of +)
11073          and/or ^ instead of |.
11074          If B is constant and (B & M) == 0, fold into A & M.  */
11075       if (host_integerp (arg1, 1))
11076         {
11077           unsigned HOST_WIDE_INT cst1 = tree_low_cst (arg1, 1);
11078           if (~cst1 && (cst1 & (cst1 + 1)) == 0
11079               && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11080               && (TREE_CODE (arg0) == PLUS_EXPR
11081                   || TREE_CODE (arg0) == MINUS_EXPR
11082                   || TREE_CODE (arg0) == NEGATE_EXPR)
11083               && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
11084                   || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
11085             {
11086               tree pmop[2];
11087               int which = 0;
11088               unsigned HOST_WIDE_INT cst0;
11089
11090               /* Now we know that arg0 is (C + D) or (C - D) or
11091                  -C and arg1 (M) is == (1LL << cst) - 1.
11092                  Store C into PMOP[0] and D into PMOP[1].  */
11093               pmop[0] = TREE_OPERAND (arg0, 0);
11094               pmop[1] = NULL;
11095               if (TREE_CODE (arg0) != NEGATE_EXPR)
11096                 {
11097                   pmop[1] = TREE_OPERAND (arg0, 1);
11098                   which = 1;
11099                 }
11100
11101               if (!host_integerp (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11102                   || (tree_low_cst (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11103                       & cst1) != cst1)
11104                 which = -1;
11105
11106               for (; which >= 0; which--)
11107                 switch (TREE_CODE (pmop[which]))
11108                   {
11109                   case BIT_AND_EXPR:
11110                   case BIT_IOR_EXPR:
11111                   case BIT_XOR_EXPR:
11112                     if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
11113                         != INTEGER_CST)
11114                       break;
11115                     /* tree_low_cst not used, because we don't care about
11116                        the upper bits.  */
11117                     cst0 = TREE_INT_CST_LOW (TREE_OPERAND (pmop[which], 1));
11118                     cst0 &= cst1;
11119                     if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
11120                       {
11121                         if (cst0 != cst1)
11122                           break;
11123                       }
11124                     else if (cst0 != 0)
11125                       break;
11126                     /* If C or D is of the form (A & N) where
11127                        (N & M) == M, or of the form (A | N) or
11128                        (A ^ N) where (N & M) == 0, replace it with A.  */
11129                     pmop[which] = TREE_OPERAND (pmop[which], 0);
11130                     break;
11131                   case INTEGER_CST:
11132                     /* If C or D is a N where (N & M) == 0, it can be
11133                        omitted (assumed 0).  */
11134                     if ((TREE_CODE (arg0) == PLUS_EXPR
11135                          || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
11136                         && (TREE_INT_CST_LOW (pmop[which]) & cst1) == 0)
11137                       pmop[which] = NULL;
11138                     break;
11139                   default:
11140                     break;
11141                   }
11142
11143               /* Only build anything new if we optimized one or both arguments
11144                  above.  */
11145               if (pmop[0] != TREE_OPERAND (arg0, 0)
11146                   || (TREE_CODE (arg0) != NEGATE_EXPR
11147                       && pmop[1] != TREE_OPERAND (arg0, 1)))
11148                 {
11149                   tree utype = TREE_TYPE (arg0);
11150                   if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11151                     {
11152                       /* Perform the operations in a type that has defined
11153                          overflow behavior.  */
11154                       utype = unsigned_type_for (TREE_TYPE (arg0));
11155                       if (pmop[0] != NULL)
11156                         pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
11157                       if (pmop[1] != NULL)
11158                         pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
11159                     }
11160
11161                   if (TREE_CODE (arg0) == NEGATE_EXPR)
11162                     tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
11163                   else if (TREE_CODE (arg0) == PLUS_EXPR)
11164                     {
11165                       if (pmop[0] != NULL && pmop[1] != NULL)
11166                         tem = fold_build2_loc (loc, PLUS_EXPR, utype,
11167                                                pmop[0], pmop[1]);
11168                       else if (pmop[0] != NULL)
11169                         tem = pmop[0];
11170                       else if (pmop[1] != NULL)
11171                         tem = pmop[1];
11172                       else
11173                         return build_int_cst (type, 0);
11174                     }
11175                   else if (pmop[0] == NULL)
11176                     tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
11177                   else
11178                     tem = fold_build2_loc (loc, MINUS_EXPR, utype,
11179                                            pmop[0], pmop[1]);
11180                   /* TEM is now the new binary +, - or unary - replacement.  */
11181                   tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
11182                                          fold_convert_loc (loc, utype, arg1));
11183                   return fold_convert_loc (loc, type, tem);
11184                 }
11185             }
11186         }
11187
11188       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11189       if (t1 != NULL_TREE)
11190         return t1;
11191       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
11192       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11193           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11194         {
11195           unsigned int prec
11196             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11197
11198           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11199               && (~TREE_INT_CST_LOW (arg1)
11200                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11201             return
11202               fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11203         }
11204
11205       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11206
11207          This results in more efficient code for machines without a NOR
11208          instruction.  Combine will canonicalize to the first form
11209          which will allow use of NOR instructions provided by the
11210          backend if they exist.  */
11211       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11212           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11213         {
11214           return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11215                               build2 (BIT_IOR_EXPR, type,
11216                                       fold_convert_loc (loc, type,
11217                                                         TREE_OPERAND (arg0, 0)),
11218                                       fold_convert_loc (loc, type,
11219                                                         TREE_OPERAND (arg1, 0))));
11220         }
11221
11222       /* If arg0 is derived from the address of an object or function, we may
11223          be able to fold this expression using the object or function's
11224          alignment.  */
11225       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11226         {
11227           unsigned HOST_WIDE_INT modulus, residue;
11228           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11229
11230           modulus = get_pointer_modulus_and_residue (arg0, &residue,
11231                                                      integer_onep (arg1));
11232
11233           /* This works because modulus is a power of 2.  If this weren't the
11234              case, we'd have to replace it by its greatest power-of-2
11235              divisor: modulus & -modulus.  */
11236           if (low < modulus)
11237             return build_int_cst (type, residue & low);
11238         }
11239
11240       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11241               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11242          if the new mask might be further optimized.  */
11243       if ((TREE_CODE (arg0) == LSHIFT_EXPR
11244            || TREE_CODE (arg0) == RSHIFT_EXPR)
11245           && host_integerp (TREE_OPERAND (arg0, 1), 1)
11246           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11247           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11248              < TYPE_PRECISION (TREE_TYPE (arg0))
11249           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11250           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11251         {
11252           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11253           unsigned HOST_WIDE_INT mask
11254             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11255           unsigned HOST_WIDE_INT newmask, zerobits = 0;
11256           tree shift_type = TREE_TYPE (arg0);
11257
11258           if (TREE_CODE (arg0) == LSHIFT_EXPR)
11259             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11260           else if (TREE_CODE (arg0) == RSHIFT_EXPR
11261                    && TYPE_PRECISION (TREE_TYPE (arg0))
11262                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11263             {
11264               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11265               tree arg00 = TREE_OPERAND (arg0, 0);
11266               /* See if more bits can be proven as zero because of
11267                  zero extension.  */
11268               if (TREE_CODE (arg00) == NOP_EXPR
11269                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11270                 {
11271                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11272                   if (TYPE_PRECISION (inner_type)
11273                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11274                       && TYPE_PRECISION (inner_type) < prec)
11275                     {
11276                       prec = TYPE_PRECISION (inner_type);
11277                       /* See if we can shorten the right shift.  */
11278                       if (shiftc < prec)
11279                         shift_type = inner_type;
11280                     }
11281                 }
11282               zerobits = ~(unsigned HOST_WIDE_INT) 0;
11283               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11284               zerobits <<= prec - shiftc;
11285               /* For arithmetic shift if sign bit could be set, zerobits
11286                  can contain actually sign bits, so no transformation is
11287                  possible, unless MASK masks them all away.  In that
11288                  case the shift needs to be converted into logical shift.  */
11289               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11290                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11291                 {
11292                   if ((mask & zerobits) == 0)
11293                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
11294                   else
11295                     zerobits = 0;
11296                 }
11297             }
11298
11299           /* ((X << 16) & 0xff00) is (X, 0).  */
11300           if ((mask & zerobits) == mask)
11301             return omit_one_operand_loc (loc, type,
11302                                      build_int_cst (type, 0), arg0);
11303
11304           newmask = mask | zerobits;
11305           if (newmask != mask && (newmask & (newmask + 1)) == 0)
11306             {
11307               unsigned int prec;
11308
11309               /* Only do the transformation if NEWMASK is some integer
11310                  mode's mask.  */
11311               for (prec = BITS_PER_UNIT;
11312                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11313                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11314                   break;
11315               if (prec < HOST_BITS_PER_WIDE_INT
11316                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
11317                 {
11318                   tree newmaskt;
11319
11320                   if (shift_type != TREE_TYPE (arg0))
11321                     {
11322                       tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11323                                          fold_convert_loc (loc, shift_type,
11324                                                            TREE_OPERAND (arg0, 0)),
11325                                          TREE_OPERAND (arg0, 1));
11326                       tem = fold_convert_loc (loc, type, tem);
11327                     }
11328                   else
11329                     tem = op0;
11330                   newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11331                   if (!tree_int_cst_equal (newmaskt, arg1))
11332                     return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11333                 }
11334             }
11335         }
11336
11337       goto associate;
11338
11339     case RDIV_EXPR:
11340       /* Don't touch a floating-point divide by zero unless the mode
11341          of the constant can represent infinity.  */
11342       if (TREE_CODE (arg1) == REAL_CST
11343           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11344           && real_zerop (arg1))
11345         return NULL_TREE;
11346
11347       /* Optimize A / A to 1.0 if we don't care about
11348          NaNs or Infinities.  Skip the transformation
11349          for non-real operands.  */
11350       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11351           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11352           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11353           && operand_equal_p (arg0, arg1, 0))
11354         {
11355           tree r = build_real (TREE_TYPE (arg0), dconst1);
11356
11357           return omit_two_operands_loc (loc, type, r, arg0, arg1);
11358         }
11359
11360       /* The complex version of the above A / A optimization.  */
11361       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11362           && operand_equal_p (arg0, arg1, 0))
11363         {
11364           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11365           if (! HONOR_NANS (TYPE_MODE (elem_type))
11366               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11367             {
11368               tree r = build_real (elem_type, dconst1);
11369               /* omit_two_operands will call fold_convert for us.  */
11370               return omit_two_operands_loc (loc, type, r, arg0, arg1);
11371             }
11372         }
11373
11374       /* (-A) / (-B) -> A / B  */
11375       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11376         return fold_build2_loc (loc, RDIV_EXPR, type,
11377                             TREE_OPERAND (arg0, 0),
11378                             negate_expr (arg1));
11379       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11380         return fold_build2_loc (loc, RDIV_EXPR, type,
11381                             negate_expr (arg0),
11382                             TREE_OPERAND (arg1, 0));
11383
11384       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
11385       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11386           && real_onep (arg1))
11387         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11388
11389       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
11390       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11391           && real_minus_onep (arg1))
11392         return non_lvalue_loc (loc, fold_convert_loc (loc, type,
11393                                                   negate_expr (arg0)));
11394
11395       /* If ARG1 is a constant, we can convert this to a multiply by the
11396          reciprocal.  This does not have the same rounding properties,
11397          so only do this if -freciprocal-math.  We can actually
11398          always safely do it if ARG1 is a power of two, but it's hard to
11399          tell if it is or not in a portable manner.  */
11400       if (TREE_CODE (arg1) == REAL_CST)
11401         {
11402           if (flag_reciprocal_math
11403               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11404                                           arg1)))
11405             return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
11406           /* Find the reciprocal if optimizing and the result is exact.  */
11407           if (optimize)
11408             {
11409               REAL_VALUE_TYPE r;
11410               r = TREE_REAL_CST (arg1);
11411               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11412                 {
11413                   tem = build_real (type, r);
11414                   return fold_build2_loc (loc, MULT_EXPR, type,
11415                                       fold_convert_loc (loc, type, arg0), tem);
11416                 }
11417             }
11418         }
11419       /* Convert A/B/C to A/(B*C).  */
11420       if (flag_reciprocal_math
11421           && TREE_CODE (arg0) == RDIV_EXPR)
11422         return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11423                             fold_build2_loc (loc, MULT_EXPR, type,
11424                                          TREE_OPERAND (arg0, 1), arg1));
11425
11426       /* Convert A/(B/C) to (A/B)*C.  */
11427       if (flag_reciprocal_math
11428           && TREE_CODE (arg1) == RDIV_EXPR)
11429         return fold_build2_loc (loc, MULT_EXPR, type,
11430                             fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11431                                          TREE_OPERAND (arg1, 0)),
11432                             TREE_OPERAND (arg1, 1));
11433
11434       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11435       if (flag_reciprocal_math
11436           && TREE_CODE (arg1) == MULT_EXPR
11437           && TREE_CODE (arg0) == REAL_CST
11438           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11439         {
11440           tree tem = const_binop (RDIV_EXPR, arg0,
11441                                   TREE_OPERAND (arg1, 1));
11442           if (tem)
11443             return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11444                                 TREE_OPERAND (arg1, 0));
11445         }
11446
11447       if (flag_unsafe_math_optimizations)
11448         {
11449           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11450           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11451
11452           /* Optimize sin(x)/cos(x) as tan(x).  */
11453           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11454                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11455                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11456               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11457                                   CALL_EXPR_ARG (arg1, 0), 0))
11458             {
11459               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11460
11461               if (tanfn != NULL_TREE)
11462                 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11463             }
11464
11465           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11466           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11467                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11468                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11469               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11470                                   CALL_EXPR_ARG (arg1, 0), 0))
11471             {
11472               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11473
11474               if (tanfn != NULL_TREE)
11475                 {
11476                   tree tmp = build_call_expr_loc (loc, tanfn, 1,
11477                                               CALL_EXPR_ARG (arg0, 0));
11478                   return fold_build2_loc (loc, RDIV_EXPR, type,
11479                                       build_real (type, dconst1), tmp);
11480                 }
11481             }
11482
11483           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11484              NaNs or Infinities.  */
11485           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11486                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11487                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11488             {
11489               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11490               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11491
11492               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11493                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11494                   && operand_equal_p (arg00, arg01, 0))
11495                 {
11496                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11497
11498                   if (cosfn != NULL_TREE)
11499                     return build_call_expr_loc (loc, cosfn, 1, arg00);
11500                 }
11501             }
11502
11503           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11504              NaNs or Infinities.  */
11505           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11506                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11507                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11508             {
11509               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11510               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11511
11512               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11513                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11514                   && operand_equal_p (arg00, arg01, 0))
11515                 {
11516                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11517
11518                   if (cosfn != NULL_TREE)
11519                     {
11520                       tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11521                       return fold_build2_loc (loc, RDIV_EXPR, type,
11522                                           build_real (type, dconst1),
11523                                           tmp);
11524                     }
11525                 }
11526             }
11527
11528           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11529           if (fcode0 == BUILT_IN_POW
11530               || fcode0 == BUILT_IN_POWF
11531               || fcode0 == BUILT_IN_POWL)
11532             {
11533               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11534               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11535               if (TREE_CODE (arg01) == REAL_CST
11536                   && !TREE_OVERFLOW (arg01)
11537                   && operand_equal_p (arg1, arg00, 0))
11538                 {
11539                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11540                   REAL_VALUE_TYPE c;
11541                   tree arg;
11542
11543                   c = TREE_REAL_CST (arg01);
11544                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11545                   arg = build_real (type, c);
11546                   return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11547                 }
11548             }
11549
11550           /* Optimize a/root(b/c) into a*root(c/b).  */
11551           if (BUILTIN_ROOT_P (fcode1))
11552             {
11553               tree rootarg = CALL_EXPR_ARG (arg1, 0);
11554
11555               if (TREE_CODE (rootarg) == RDIV_EXPR)
11556                 {
11557                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11558                   tree b = TREE_OPERAND (rootarg, 0);
11559                   tree c = TREE_OPERAND (rootarg, 1);
11560
11561                   tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
11562
11563                   tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
11564                   return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
11565                 }
11566             }
11567
11568           /* Optimize x/expN(y) into x*expN(-y).  */
11569           if (BUILTIN_EXPONENT_P (fcode1))
11570             {
11571               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11572               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11573               arg1 = build_call_expr_loc (loc,
11574                                       expfn, 1,
11575                                       fold_convert_loc (loc, type, arg));
11576               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11577             }
11578
11579           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
11580           if (fcode1 == BUILT_IN_POW
11581               || fcode1 == BUILT_IN_POWF
11582               || fcode1 == BUILT_IN_POWL)
11583             {
11584               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11585               tree arg10 = CALL_EXPR_ARG (arg1, 0);
11586               tree arg11 = CALL_EXPR_ARG (arg1, 1);
11587               tree neg11 = fold_convert_loc (loc, type,
11588                                              negate_expr (arg11));
11589               arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
11590               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11591             }
11592         }
11593       return NULL_TREE;
11594
11595     case TRUNC_DIV_EXPR:
11596       /* Optimize (X & (-A)) / A where A is a power of 2,
11597          to X >> log2(A) */
11598       if (TREE_CODE (arg0) == BIT_AND_EXPR
11599           && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST
11600           && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0)
11601         {
11602           tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1),
11603                                       arg1, TREE_OPERAND (arg0, 1));
11604           if (sum && integer_zerop (sum)) {
11605             unsigned long pow2;
11606
11607             if (TREE_INT_CST_LOW (arg1))
11608               pow2 = exact_log2 (TREE_INT_CST_LOW (arg1));
11609             else
11610               pow2 = exact_log2 (TREE_INT_CST_HIGH (arg1))
11611                       + HOST_BITS_PER_WIDE_INT;
11612
11613             return fold_build2_loc (loc, RSHIFT_EXPR, type,
11614                           TREE_OPERAND (arg0, 0),
11615                           build_int_cst (NULL_TREE, pow2));
11616           }
11617         }
11618
11619       /* Fall thru */
11620       
11621     case FLOOR_DIV_EXPR:
11622       /* Simplify A / (B << N) where A and B are positive and B is
11623          a power of 2, to A >> (N + log2(B)).  */
11624       strict_overflow_p = false;
11625       if (TREE_CODE (arg1) == LSHIFT_EXPR
11626           && (TYPE_UNSIGNED (type)
11627               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11628         {
11629           tree sval = TREE_OPERAND (arg1, 0);
11630           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11631             {
11632               tree sh_cnt = TREE_OPERAND (arg1, 1);
11633               unsigned long pow2;
11634
11635               if (TREE_INT_CST_LOW (sval))
11636                 pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11637               else
11638                 pow2 = exact_log2 (TREE_INT_CST_HIGH (sval))
11639                        + HOST_BITS_PER_WIDE_INT;
11640
11641               if (strict_overflow_p)
11642                 fold_overflow_warning (("assuming signed overflow does not "
11643                                         "occur when simplifying A / (B << N)"),
11644                                        WARN_STRICT_OVERFLOW_MISC);
11645
11646               sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11647                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
11648               return fold_build2_loc (loc, RSHIFT_EXPR, type,
11649                                   fold_convert_loc (loc, type, arg0), sh_cnt);
11650             }
11651         }
11652
11653       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11654          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
11655       if (INTEGRAL_TYPE_P (type)
11656           && TYPE_UNSIGNED (type)
11657           && code == FLOOR_DIV_EXPR)
11658         return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
11659
11660       /* Fall thru */
11661
11662     case ROUND_DIV_EXPR:
11663     case CEIL_DIV_EXPR:
11664     case EXACT_DIV_EXPR:
11665       if (integer_onep (arg1))
11666         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11667       if (integer_zerop (arg1))
11668         return NULL_TREE;
11669       /* X / -1 is -X.  */
11670       if (!TYPE_UNSIGNED (type)
11671           && TREE_CODE (arg1) == INTEGER_CST
11672           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11673           && TREE_INT_CST_HIGH (arg1) == -1)
11674         return fold_convert_loc (loc, type, negate_expr (arg0));
11675
11676       /* Convert -A / -B to A / B when the type is signed and overflow is
11677          undefined.  */
11678       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11679           && TREE_CODE (arg0) == NEGATE_EXPR
11680           && negate_expr_p (arg1))
11681         {
11682           if (INTEGRAL_TYPE_P (type))
11683             fold_overflow_warning (("assuming signed overflow does not occur "
11684                                     "when distributing negation across "
11685                                     "division"),
11686                                    WARN_STRICT_OVERFLOW_MISC);
11687           return fold_build2_loc (loc, code, type,
11688                               fold_convert_loc (loc, type,
11689                                                 TREE_OPERAND (arg0, 0)),
11690                               fold_convert_loc (loc, type,
11691                                                 negate_expr (arg1)));
11692         }
11693       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11694           && TREE_CODE (arg1) == NEGATE_EXPR
11695           && negate_expr_p (arg0))
11696         {
11697           if (INTEGRAL_TYPE_P (type))
11698             fold_overflow_warning (("assuming signed overflow does not occur "
11699                                     "when distributing negation across "
11700                                     "division"),
11701                                    WARN_STRICT_OVERFLOW_MISC);
11702           return fold_build2_loc (loc, code, type,
11703                               fold_convert_loc (loc, type,
11704                                                 negate_expr (arg0)),
11705                               fold_convert_loc (loc, type,
11706                                                 TREE_OPERAND (arg1, 0)));
11707         }
11708
11709       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11710          operation, EXACT_DIV_EXPR.
11711
11712          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11713          At one time others generated faster code, it's not clear if they do
11714          after the last round to changes to the DIV code in expmed.c.  */
11715       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11716           && multiple_of_p (type, arg0, arg1))
11717         return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
11718
11719       strict_overflow_p = false;
11720       if (TREE_CODE (arg1) == INTEGER_CST
11721           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11722                                          &strict_overflow_p)))
11723         {
11724           if (strict_overflow_p)
11725             fold_overflow_warning (("assuming signed overflow does not occur "
11726                                     "when simplifying division"),
11727                                    WARN_STRICT_OVERFLOW_MISC);
11728           return fold_convert_loc (loc, type, tem);
11729         }
11730
11731       return NULL_TREE;
11732
11733     case CEIL_MOD_EXPR:
11734     case FLOOR_MOD_EXPR:
11735     case ROUND_MOD_EXPR:
11736     case TRUNC_MOD_EXPR:
11737       /* X % 1 is always zero, but be sure to preserve any side
11738          effects in X.  */
11739       if (integer_onep (arg1))
11740         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11741
11742       /* X % 0, return X % 0 unchanged so that we can get the
11743          proper warnings and errors.  */
11744       if (integer_zerop (arg1))
11745         return NULL_TREE;
11746
11747       /* 0 % X is always zero, but be sure to preserve any side
11748          effects in X.  Place this after checking for X == 0.  */
11749       if (integer_zerop (arg0))
11750         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11751
11752       /* X % -1 is zero.  */
11753       if (!TYPE_UNSIGNED (type)
11754           && TREE_CODE (arg1) == INTEGER_CST
11755           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11756           && TREE_INT_CST_HIGH (arg1) == -1)
11757         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11758
11759       /* X % -C is the same as X % C.  */
11760       if (code == TRUNC_MOD_EXPR
11761           && !TYPE_UNSIGNED (type)
11762           && TREE_CODE (arg1) == INTEGER_CST
11763           && !TREE_OVERFLOW (arg1)
11764           && TREE_INT_CST_HIGH (arg1) < 0
11765           && !TYPE_OVERFLOW_TRAPS (type)
11766           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
11767           && !sign_bit_p (arg1, arg1))
11768         return fold_build2_loc (loc, code, type,
11769                             fold_convert_loc (loc, type, arg0),
11770                             fold_convert_loc (loc, type,
11771                                               negate_expr (arg1)));
11772
11773       /* X % -Y is the same as X % Y.  */
11774       if (code == TRUNC_MOD_EXPR
11775           && !TYPE_UNSIGNED (type)
11776           && TREE_CODE (arg1) == NEGATE_EXPR
11777           && !TYPE_OVERFLOW_TRAPS (type))
11778         return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
11779                             fold_convert_loc (loc, type,
11780                                               TREE_OPERAND (arg1, 0)));
11781
11782       strict_overflow_p = false;
11783       if (TREE_CODE (arg1) == INTEGER_CST
11784           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11785                                          &strict_overflow_p)))
11786         {
11787           if (strict_overflow_p)
11788             fold_overflow_warning (("assuming signed overflow does not occur "
11789                                     "when simplifying modulus"),
11790                                    WARN_STRICT_OVERFLOW_MISC);
11791           return fold_convert_loc (loc, type, tem);
11792         }
11793
11794       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11795          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
11796       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11797           && (TYPE_UNSIGNED (type)
11798               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11799         {
11800           tree c = arg1;
11801           /* Also optimize A % (C << N)  where C is a power of 2,
11802              to A & ((C << N) - 1).  */
11803           if (TREE_CODE (arg1) == LSHIFT_EXPR)
11804             c = TREE_OPERAND (arg1, 0);
11805
11806           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11807             {
11808               tree mask
11809                 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
11810                                    build_int_cst (TREE_TYPE (arg1), 1));
11811               if (strict_overflow_p)
11812                 fold_overflow_warning (("assuming signed overflow does not "
11813                                         "occur when simplifying "
11814                                         "X % (power of two)"),
11815                                        WARN_STRICT_OVERFLOW_MISC);
11816               return fold_build2_loc (loc, BIT_AND_EXPR, type,
11817                                       fold_convert_loc (loc, type, arg0),
11818                                       fold_convert_loc (loc, type, mask));
11819             }
11820         }
11821
11822       return NULL_TREE;
11823
11824     case LROTATE_EXPR:
11825     case RROTATE_EXPR:
11826       if (integer_all_onesp (arg0))
11827         return omit_one_operand_loc (loc, type, arg0, arg1);
11828       goto shift;
11829
11830     case RSHIFT_EXPR:
11831       /* Optimize -1 >> x for arithmetic right shifts.  */
11832       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11833           && tree_expr_nonnegative_p (arg1))
11834         return omit_one_operand_loc (loc, type, arg0, arg1);
11835       /* ... fall through ...  */
11836
11837     case LSHIFT_EXPR:
11838     shift:
11839       if (integer_zerop (arg1))
11840         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11841       if (integer_zerop (arg0))
11842         return omit_one_operand_loc (loc, type, arg0, arg1);
11843
11844       /* Since negative shift count is not well-defined,
11845          don't try to compute it in the compiler.  */
11846       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11847         return NULL_TREE;
11848
11849       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
11850       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11851           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11852           && host_integerp (TREE_OPERAND (arg0, 1), false)
11853           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11854         {
11855           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11856                                + TREE_INT_CST_LOW (arg1));
11857
11858           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11859              being well defined.  */
11860           if (low >= TYPE_PRECISION (type))
11861             {
11862               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11863                 low = low % TYPE_PRECISION (type);
11864               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11865                 return omit_one_operand_loc (loc, type, build_int_cst (type, 0),
11866                                          TREE_OPERAND (arg0, 0));
11867               else
11868                 low = TYPE_PRECISION (type) - 1;
11869             }
11870
11871           return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11872                               build_int_cst (type, low));
11873         }
11874
11875       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11876          into x & ((unsigned)-1 >> c) for unsigned types.  */
11877       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11878            || (TYPE_UNSIGNED (type)
11879                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11880           && host_integerp (arg1, false)
11881           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11882           && host_integerp (TREE_OPERAND (arg0, 1), false)
11883           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11884         {
11885           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11886           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11887           tree lshift;
11888           tree arg00;
11889
11890           if (low0 == low1)
11891             {
11892               arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11893
11894               lshift = build_int_cst (type, -1);
11895               lshift = int_const_binop (code, lshift, arg1, 0);
11896
11897               return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
11898             }
11899         }
11900
11901       /* Rewrite an LROTATE_EXPR by a constant into an
11902          RROTATE_EXPR by a new constant.  */
11903       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11904         {
11905           tree tem = build_int_cst (TREE_TYPE (arg1),
11906                                     TYPE_PRECISION (type));
11907           tem = const_binop (MINUS_EXPR, tem, arg1);
11908           return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
11909         }
11910
11911       /* If we have a rotate of a bit operation with the rotate count and
11912          the second operand of the bit operation both constant,
11913          permute the two operations.  */
11914       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11915           && (TREE_CODE (arg0) == BIT_AND_EXPR
11916               || TREE_CODE (arg0) == BIT_IOR_EXPR
11917               || TREE_CODE (arg0) == BIT_XOR_EXPR)
11918           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11919         return fold_build2_loc (loc, TREE_CODE (arg0), type,
11920                             fold_build2_loc (loc, code, type,
11921                                          TREE_OPERAND (arg0, 0), arg1),
11922                             fold_build2_loc (loc, code, type,
11923                                          TREE_OPERAND (arg0, 1), arg1));
11924
11925       /* Two consecutive rotates adding up to the precision of the
11926          type can be ignored.  */
11927       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11928           && TREE_CODE (arg0) == RROTATE_EXPR
11929           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11930           && TREE_INT_CST_HIGH (arg1) == 0
11931           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11932           && ((TREE_INT_CST_LOW (arg1)
11933                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11934               == (unsigned int) TYPE_PRECISION (type)))
11935         return TREE_OPERAND (arg0, 0);
11936
11937       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11938               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11939          if the latter can be further optimized.  */
11940       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11941           && TREE_CODE (arg0) == BIT_AND_EXPR
11942           && TREE_CODE (arg1) == INTEGER_CST
11943           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11944         {
11945           tree mask = fold_build2_loc (loc, code, type,
11946                                    fold_convert_loc (loc, type,
11947                                                      TREE_OPERAND (arg0, 1)),
11948                                    arg1);
11949           tree shift = fold_build2_loc (loc, code, type,
11950                                     fold_convert_loc (loc, type,
11951                                                       TREE_OPERAND (arg0, 0)),
11952                                     arg1);
11953           tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
11954           if (tem)
11955             return tem;
11956         }
11957
11958       return NULL_TREE;
11959
11960     case MIN_EXPR:
11961       if (operand_equal_p (arg0, arg1, 0))
11962         return omit_one_operand_loc (loc, type, arg0, arg1);
11963       if (INTEGRAL_TYPE_P (type)
11964           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11965         return omit_one_operand_loc (loc, type, arg1, arg0);
11966       tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
11967       if (tem)
11968         return tem;
11969       goto associate;
11970
11971     case MAX_EXPR:
11972       if (operand_equal_p (arg0, arg1, 0))
11973         return omit_one_operand_loc (loc, type, arg0, arg1);
11974       if (INTEGRAL_TYPE_P (type)
11975           && TYPE_MAX_VALUE (type)
11976           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11977         return omit_one_operand_loc (loc, type, arg1, arg0);
11978       tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
11979       if (tem)
11980         return tem;
11981       goto associate;
11982
11983     case TRUTH_ANDIF_EXPR:
11984       /* Note that the operands of this must be ints
11985          and their values must be 0 or 1.
11986          ("true" is a fixed value perhaps depending on the language.)  */
11987       /* If first arg is constant zero, return it.  */
11988       if (integer_zerop (arg0))
11989         return fold_convert_loc (loc, type, arg0);
11990     case TRUTH_AND_EXPR:
11991       /* If either arg is constant true, drop it.  */
11992       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11993         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11994       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11995           /* Preserve sequence points.  */
11996           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11997         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11998       /* If second arg is constant zero, result is zero, but first arg
11999          must be evaluated.  */
12000       if (integer_zerop (arg1))
12001         return omit_one_operand_loc (loc, type, arg1, arg0);
12002       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12003          case will be handled here.  */
12004       if (integer_zerop (arg0))
12005         return omit_one_operand_loc (loc, type, arg0, arg1);
12006
12007       /* !X && X is always false.  */
12008       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12009           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12010         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12011       /* X && !X is always false.  */
12012       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12013           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12014         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12015
12016       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
12017          means A >= Y && A != MAX, but in this case we know that
12018          A < X <= MAX.  */
12019
12020       if (!TREE_SIDE_EFFECTS (arg0)
12021           && !TREE_SIDE_EFFECTS (arg1))
12022         {
12023           tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12024           if (tem && !operand_equal_p (tem, arg0, 0))
12025             return fold_build2_loc (loc, code, type, tem, arg1);
12026
12027           tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12028           if (tem && !operand_equal_p (tem, arg1, 0))
12029             return fold_build2_loc (loc, code, type, arg0, tem);
12030         }
12031
12032     truth_andor:
12033       /* We only do these simplifications if we are optimizing.  */
12034       if (!optimize)
12035         return NULL_TREE;
12036
12037       /* Check for things like (A || B) && (A || C).  We can convert this
12038          to A || (B && C).  Note that either operator can be any of the four
12039          truth and/or operations and the transformation will still be
12040          valid.   Also note that we only care about order for the
12041          ANDIF and ORIF operators.  If B contains side effects, this
12042          might change the truth-value of A.  */
12043       if (TREE_CODE (arg0) == TREE_CODE (arg1)
12044           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
12045               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
12046               || TREE_CODE (arg0) == TRUTH_AND_EXPR
12047               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
12048           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
12049         {
12050           tree a00 = TREE_OPERAND (arg0, 0);
12051           tree a01 = TREE_OPERAND (arg0, 1);
12052           tree a10 = TREE_OPERAND (arg1, 0);
12053           tree a11 = TREE_OPERAND (arg1, 1);
12054           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
12055                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
12056                              && (code == TRUTH_AND_EXPR
12057                                  || code == TRUTH_OR_EXPR));
12058
12059           if (operand_equal_p (a00, a10, 0))
12060             return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
12061                                 fold_build2_loc (loc, code, type, a01, a11));
12062           else if (commutative && operand_equal_p (a00, a11, 0))
12063             return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
12064                                 fold_build2_loc (loc, code, type, a01, a10));
12065           else if (commutative && operand_equal_p (a01, a10, 0))
12066             return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
12067                                 fold_build2_loc (loc, code, type, a00, a11));
12068
12069           /* This case if tricky because we must either have commutative
12070              operators or else A10 must not have side-effects.  */
12071
12072           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12073                    && operand_equal_p (a01, a11, 0))
12074             return fold_build2_loc (loc, TREE_CODE (arg0), type,
12075                                 fold_build2_loc (loc, code, type, a00, a10),
12076                                 a01);
12077         }
12078
12079       /* See if we can build a range comparison.  */
12080       if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
12081         return tem;
12082
12083       if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
12084           || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
12085         {
12086           tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
12087           if (tem)
12088             return fold_build2_loc (loc, code, type, tem, arg1);
12089         }
12090
12091       if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
12092           || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
12093         {
12094           tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
12095           if (tem)
12096             return fold_build2_loc (loc, code, type, arg0, tem);
12097         }
12098
12099       /* Check for the possibility of merging component references.  If our
12100          lhs is another similar operation, try to merge its rhs with our
12101          rhs.  Then try to merge our lhs and rhs.  */
12102       if (TREE_CODE (arg0) == code
12103           && 0 != (tem = fold_truthop (loc, code, type,
12104                                        TREE_OPERAND (arg0, 1), arg1)))
12105         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12106
12107       if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0)
12108         return tem;
12109
12110       return NULL_TREE;
12111
12112     case TRUTH_ORIF_EXPR:
12113       /* Note that the operands of this must be ints
12114          and their values must be 0 or true.
12115          ("true" is a fixed value perhaps depending on the language.)  */
12116       /* If first arg is constant true, return it.  */
12117       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12118         return fold_convert_loc (loc, type, arg0);
12119     case TRUTH_OR_EXPR:
12120       /* If either arg is constant zero, drop it.  */
12121       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12122         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12123       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12124           /* Preserve sequence points.  */
12125           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12126         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12127       /* If second arg is constant true, result is true, but we must
12128          evaluate first arg.  */
12129       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12130         return omit_one_operand_loc (loc, type, arg1, arg0);
12131       /* Likewise for first arg, but note this only occurs here for
12132          TRUTH_OR_EXPR.  */
12133       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12134         return omit_one_operand_loc (loc, type, arg0, arg1);
12135
12136       /* !X || X is always true.  */
12137       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12138           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12139         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12140       /* X || !X is always true.  */
12141       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12142           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12143         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12144
12145       goto truth_andor;
12146
12147     case TRUTH_XOR_EXPR:
12148       /* If the second arg is constant zero, drop it.  */
12149       if (integer_zerop (arg1))
12150         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12151       /* If the second arg is constant true, this is a logical inversion.  */
12152       if (integer_onep (arg1))
12153         {
12154           /* Only call invert_truthvalue if operand is a truth value.  */
12155           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12156             tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12157           else
12158             tem = invert_truthvalue_loc (loc, arg0);
12159           return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12160         }
12161       /* Identical arguments cancel to zero.  */
12162       if (operand_equal_p (arg0, arg1, 0))
12163         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12164
12165       /* !X ^ X is always true.  */
12166       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12167           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12168         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12169
12170       /* X ^ !X is always true.  */
12171       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12172           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12173         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12174
12175       return NULL_TREE;
12176
12177     case EQ_EXPR:
12178     case NE_EXPR:
12179       tem = fold_comparison (loc, code, type, op0, op1);
12180       if (tem != NULL_TREE)
12181         return tem;
12182
12183       /* bool_var != 0 becomes bool_var. */
12184       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12185           && code == NE_EXPR)
12186         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12187
12188       /* bool_var == 1 becomes bool_var. */
12189       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12190           && code == EQ_EXPR)
12191         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12192
12193       /* bool_var != 1 becomes !bool_var. */
12194       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12195           && code == NE_EXPR)
12196         return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12197                             fold_convert_loc (loc, type, arg0));
12198
12199       /* bool_var == 0 becomes !bool_var. */
12200       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12201           && code == EQ_EXPR)
12202         return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12203                             fold_convert_loc (loc, type, arg0));
12204
12205       /* !exp != 0 becomes !exp */
12206       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12207           && code == NE_EXPR)
12208         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12209
12210       /* If this is an equality comparison of the address of two non-weak,
12211          unaliased symbols neither of which are extern (since we do not
12212          have access to attributes for externs), then we know the result.  */
12213       if (TREE_CODE (arg0) == ADDR_EXPR
12214           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12215           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12216           && ! lookup_attribute ("alias",
12217                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12218           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12219           && TREE_CODE (arg1) == ADDR_EXPR
12220           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12221           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12222           && ! lookup_attribute ("alias",
12223                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12224           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12225         {
12226           /* We know that we're looking at the address of two
12227              non-weak, unaliased, static _DECL nodes.
12228
12229              It is both wasteful and incorrect to call operand_equal_p
12230              to compare the two ADDR_EXPR nodes.  It is wasteful in that
12231              all we need to do is test pointer equality for the arguments
12232              to the two ADDR_EXPR nodes.  It is incorrect to use
12233              operand_equal_p as that function is NOT equivalent to a
12234              C equality test.  It can in fact return false for two
12235              objects which would test as equal using the C equality
12236              operator.  */
12237           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12238           return constant_boolean_node (equal
12239                                         ? code == EQ_EXPR : code != EQ_EXPR,
12240                                         type);
12241         }
12242
12243       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12244          a MINUS_EXPR of a constant, we can convert it into a comparison with
12245          a revised constant as long as no overflow occurs.  */
12246       if (TREE_CODE (arg1) == INTEGER_CST
12247           && (TREE_CODE (arg0) == PLUS_EXPR
12248               || TREE_CODE (arg0) == MINUS_EXPR)
12249           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12250           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12251                                       ? MINUS_EXPR : PLUS_EXPR,
12252                                       fold_convert_loc (loc, TREE_TYPE (arg0),
12253                                                         arg1),
12254                                       TREE_OPERAND (arg0, 1)))
12255           && !TREE_OVERFLOW (tem))
12256         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12257
12258       /* Similarly for a NEGATE_EXPR.  */
12259       if (TREE_CODE (arg0) == NEGATE_EXPR
12260           && TREE_CODE (arg1) == INTEGER_CST
12261           && 0 != (tem = negate_expr (arg1))
12262           && TREE_CODE (tem) == INTEGER_CST
12263           && !TREE_OVERFLOW (tem))
12264         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12265
12266       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
12267       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12268           && TREE_CODE (arg1) == INTEGER_CST
12269           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12270         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12271                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12272                                          fold_convert_loc (loc,
12273                                                            TREE_TYPE (arg0),
12274                                                            arg1),
12275                                          TREE_OPERAND (arg0, 1)));
12276
12277       /* Transform comparisons of the form X +- Y CMP X to Y CMP 0.  */
12278       if ((TREE_CODE (arg0) == PLUS_EXPR
12279            || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12280            || TREE_CODE (arg0) == MINUS_EXPR)
12281           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12282           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12283               || POINTER_TYPE_P (TREE_TYPE (arg0))))
12284         {
12285           tree val = TREE_OPERAND (arg0, 1);
12286           return omit_two_operands_loc (loc, type,
12287                                     fold_build2_loc (loc, code, type,
12288                                                  val,
12289                                                  build_int_cst (TREE_TYPE (val),
12290                                                                 0)),
12291                                     TREE_OPERAND (arg0, 0), arg1);
12292         }
12293
12294       /* Transform comparisons of the form C - X CMP X if C % 2 == 1.  */
12295       if (TREE_CODE (arg0) == MINUS_EXPR
12296           && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12297           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)
12298           && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12299         {
12300           return omit_two_operands_loc (loc, type,
12301                                     code == NE_EXPR
12302                                     ? boolean_true_node : boolean_false_node,
12303                                     TREE_OPERAND (arg0, 1), arg1);
12304         }
12305
12306       /* If we have X - Y == 0, we can convert that to X == Y and similarly
12307          for !=.  Don't do this for ordered comparisons due to overflow.  */
12308       if (TREE_CODE (arg0) == MINUS_EXPR
12309           && integer_zerop (arg1))
12310         return fold_build2_loc (loc, code, type,
12311                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12312
12313       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
12314       if (TREE_CODE (arg0) == ABS_EXPR
12315           && (integer_zerop (arg1) || real_zerop (arg1)))
12316         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12317
12318       /* If this is an EQ or NE comparison with zero and ARG0 is
12319          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
12320          two operations, but the latter can be done in one less insn
12321          on machines that have only two-operand insns or on which a
12322          constant cannot be the first operand.  */
12323       if (TREE_CODE (arg0) == BIT_AND_EXPR
12324           && integer_zerop (arg1))
12325         {
12326           tree arg00 = TREE_OPERAND (arg0, 0);
12327           tree arg01 = TREE_OPERAND (arg0, 1);
12328           if (TREE_CODE (arg00) == LSHIFT_EXPR
12329               && integer_onep (TREE_OPERAND (arg00, 0)))
12330             {
12331               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12332                                       arg01, TREE_OPERAND (arg00, 1));
12333               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12334                                  build_int_cst (TREE_TYPE (arg0), 1));
12335               return fold_build2_loc (loc, code, type,
12336                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12337                                   arg1);
12338             }
12339           else if (TREE_CODE (arg01) == LSHIFT_EXPR
12340                    && integer_onep (TREE_OPERAND (arg01, 0)))
12341             {
12342               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12343                                       arg00, TREE_OPERAND (arg01, 1));
12344               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12345                                  build_int_cst (TREE_TYPE (arg0), 1));
12346               return fold_build2_loc (loc, code, type,
12347                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12348                                   arg1);
12349             }
12350         }
12351
12352       /* If this is an NE or EQ comparison of zero against the result of a
12353          signed MOD operation whose second operand is a power of 2, make
12354          the MOD operation unsigned since it is simpler and equivalent.  */
12355       if (integer_zerop (arg1)
12356           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12357           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12358               || TREE_CODE (arg0) == CEIL_MOD_EXPR
12359               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12360               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12361           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12362         {
12363           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12364           tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12365                                      fold_convert_loc (loc, newtype,
12366                                                        TREE_OPERAND (arg0, 0)),
12367                                      fold_convert_loc (loc, newtype,
12368                                                        TREE_OPERAND (arg0, 1)));
12369
12370           return fold_build2_loc (loc, code, type, newmod,
12371                               fold_convert_loc (loc, newtype, arg1));
12372         }
12373
12374       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12375          C1 is a valid shift constant, and C2 is a power of two, i.e.
12376          a single bit.  */
12377       if (TREE_CODE (arg0) == BIT_AND_EXPR
12378           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12379           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12380              == INTEGER_CST
12381           && integer_pow2p (TREE_OPERAND (arg0, 1))
12382           && integer_zerop (arg1))
12383         {
12384           tree itype = TREE_TYPE (arg0);
12385           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12386           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12387
12388           /* Check for a valid shift count.  */
12389           if (TREE_INT_CST_HIGH (arg001) == 0
12390               && TREE_INT_CST_LOW (arg001) < prec)
12391             {
12392               tree arg01 = TREE_OPERAND (arg0, 1);
12393               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12394               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12395               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12396                  can be rewritten as (X & (C2 << C1)) != 0.  */
12397               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12398                 {
12399                   tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12400                   tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12401                   return fold_build2_loc (loc, code, type, tem, arg1);
12402                 }
12403               /* Otherwise, for signed (arithmetic) shifts,
12404                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12405                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
12406               else if (!TYPE_UNSIGNED (itype))
12407                 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12408                                     arg000, build_int_cst (itype, 0));
12409               /* Otherwise, of unsigned (logical) shifts,
12410                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12411                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
12412               else
12413                 return omit_one_operand_loc (loc, type,
12414                                          code == EQ_EXPR ? integer_one_node
12415                                                          : integer_zero_node,
12416                                          arg000);
12417             }
12418         }
12419
12420       /* If this is an NE comparison of zero with an AND of one, remove the
12421          comparison since the AND will give the correct value.  */
12422       if (code == NE_EXPR
12423           && integer_zerop (arg1)
12424           && TREE_CODE (arg0) == BIT_AND_EXPR
12425           && integer_onep (TREE_OPERAND (arg0, 1)))
12426         return fold_convert_loc (loc, type, arg0);
12427
12428       /* If we have (A & C) == C where C is a power of 2, convert this into
12429          (A & C) != 0.  Similarly for NE_EXPR.  */
12430       if (TREE_CODE (arg0) == BIT_AND_EXPR
12431           && integer_pow2p (TREE_OPERAND (arg0, 1))
12432           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12433         return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12434                             arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12435                                                     integer_zero_node));
12436
12437       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12438          bit, then fold the expression into A < 0 or A >= 0.  */
12439       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12440       if (tem)
12441         return tem;
12442
12443       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12444          Similarly for NE_EXPR.  */
12445       if (TREE_CODE (arg0) == BIT_AND_EXPR
12446           && TREE_CODE (arg1) == INTEGER_CST
12447           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12448         {
12449           tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12450                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
12451                                    TREE_OPERAND (arg0, 1));
12452           tree dandnotc = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12453                                        arg1, notc);
12454           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12455           if (integer_nonzerop (dandnotc))
12456             return omit_one_operand_loc (loc, type, rslt, arg0);
12457         }
12458
12459       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12460          Similarly for NE_EXPR.  */
12461       if (TREE_CODE (arg0) == BIT_IOR_EXPR
12462           && TREE_CODE (arg1) == INTEGER_CST
12463           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12464         {
12465           tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12466           tree candnotd = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12467                                        TREE_OPERAND (arg0, 1), notd);
12468           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12469           if (integer_nonzerop (candnotd))
12470             return omit_one_operand_loc (loc, type, rslt, arg0);
12471         }
12472
12473       /* If this is a comparison of a field, we may be able to simplify it.  */
12474       if ((TREE_CODE (arg0) == COMPONENT_REF
12475            || TREE_CODE (arg0) == BIT_FIELD_REF)
12476           /* Handle the constant case even without -O
12477              to make sure the warnings are given.  */
12478           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12479         {
12480           t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12481           if (t1)
12482             return t1;
12483         }
12484
12485       /* Optimize comparisons of strlen vs zero to a compare of the
12486          first character of the string vs zero.  To wit,
12487                 strlen(ptr) == 0   =>  *ptr == 0
12488                 strlen(ptr) != 0   =>  *ptr != 0
12489          Other cases should reduce to one of these two (or a constant)
12490          due to the return value of strlen being unsigned.  */
12491       if (TREE_CODE (arg0) == CALL_EXPR
12492           && integer_zerop (arg1))
12493         {
12494           tree fndecl = get_callee_fndecl (arg0);
12495
12496           if (fndecl
12497               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12498               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12499               && call_expr_nargs (arg0) == 1
12500               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12501             {
12502               tree iref = build_fold_indirect_ref_loc (loc,
12503                                                    CALL_EXPR_ARG (arg0, 0));
12504               return fold_build2_loc (loc, code, type, iref,
12505                                   build_int_cst (TREE_TYPE (iref), 0));
12506             }
12507         }
12508
12509       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12510          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12511       if (TREE_CODE (arg0) == RSHIFT_EXPR
12512           && integer_zerop (arg1)
12513           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12514         {
12515           tree arg00 = TREE_OPERAND (arg0, 0);
12516           tree arg01 = TREE_OPERAND (arg0, 1);
12517           tree itype = TREE_TYPE (arg00);
12518           if (TREE_INT_CST_HIGH (arg01) == 0
12519               && TREE_INT_CST_LOW (arg01)
12520                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12521             {
12522               if (TYPE_UNSIGNED (itype))
12523                 {
12524                   itype = signed_type_for (itype);
12525                   arg00 = fold_convert_loc (loc, itype, arg00);
12526                 }
12527               return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12528                                   type, arg00, build_int_cst (itype, 0));
12529             }
12530         }
12531
12532       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12533       if (integer_zerop (arg1)
12534           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12535         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12536                             TREE_OPERAND (arg0, 1));
12537
12538       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12539       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12540           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12541         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12542                             build_int_cst (TREE_TYPE (arg1), 0));
12543       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12544       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12545           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12546           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12547         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12548                             build_int_cst (TREE_TYPE (arg1), 0));
12549
12550       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12551       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12552           && TREE_CODE (arg1) == INTEGER_CST
12553           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12554         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12555                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12556                                          TREE_OPERAND (arg0, 1), arg1));
12557
12558       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12559          (X & C) == 0 when C is a single bit.  */
12560       if (TREE_CODE (arg0) == BIT_AND_EXPR
12561           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12562           && integer_zerop (arg1)
12563           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12564         {
12565           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12566                              TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12567                              TREE_OPERAND (arg0, 1));
12568           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12569                               type, tem, arg1);
12570         }
12571
12572       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12573          constant C is a power of two, i.e. a single bit.  */
12574       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12575           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12576           && integer_zerop (arg1)
12577           && integer_pow2p (TREE_OPERAND (arg0, 1))
12578           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12579                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12580         {
12581           tree arg00 = TREE_OPERAND (arg0, 0);
12582           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12583                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
12584         }
12585
12586       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12587          when is C is a power of two, i.e. a single bit.  */
12588       if (TREE_CODE (arg0) == BIT_AND_EXPR
12589           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12590           && integer_zerop (arg1)
12591           && integer_pow2p (TREE_OPERAND (arg0, 1))
12592           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12593                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12594         {
12595           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12596           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12597                              arg000, TREE_OPERAND (arg0, 1));
12598           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12599                               tem, build_int_cst (TREE_TYPE (tem), 0));
12600         }
12601
12602       if (integer_zerop (arg1)
12603           && tree_expr_nonzero_p (arg0))
12604         {
12605           tree res = constant_boolean_node (code==NE_EXPR, type);
12606           return omit_one_operand_loc (loc, type, res, arg0);
12607         }
12608
12609       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
12610       if (TREE_CODE (arg0) == NEGATE_EXPR
12611           && TREE_CODE (arg1) == NEGATE_EXPR)
12612         return fold_build2_loc (loc, code, type,
12613                             TREE_OPERAND (arg0, 0),
12614                             TREE_OPERAND (arg1, 0));
12615
12616       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
12617       if (TREE_CODE (arg0) == BIT_AND_EXPR
12618           && TREE_CODE (arg1) == BIT_AND_EXPR)
12619         {
12620           tree arg00 = TREE_OPERAND (arg0, 0);
12621           tree arg01 = TREE_OPERAND (arg0, 1);
12622           tree arg10 = TREE_OPERAND (arg1, 0);
12623           tree arg11 = TREE_OPERAND (arg1, 1);
12624           tree itype = TREE_TYPE (arg0);
12625
12626           if (operand_equal_p (arg01, arg11, 0))
12627             return fold_build2_loc (loc, code, type,
12628                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12629                                              fold_build2_loc (loc,
12630                                                           BIT_XOR_EXPR, itype,
12631                                                           arg00, arg10),
12632                                              arg01),
12633                                 build_int_cst (itype, 0));
12634
12635           if (operand_equal_p (arg01, arg10, 0))
12636             return fold_build2_loc (loc, code, type,
12637                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12638                                              fold_build2_loc (loc,
12639                                                           BIT_XOR_EXPR, itype,
12640                                                           arg00, arg11),
12641                                              arg01),
12642                                 build_int_cst (itype, 0));
12643
12644           if (operand_equal_p (arg00, arg11, 0))
12645             return fold_build2_loc (loc, code, type,
12646                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12647                                              fold_build2_loc (loc,
12648                                                           BIT_XOR_EXPR, itype,
12649                                                           arg01, arg10),
12650                                              arg00),
12651                                 build_int_cst (itype, 0));
12652
12653           if (operand_equal_p (arg00, arg10, 0))
12654             return fold_build2_loc (loc, code, type,
12655                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12656                                              fold_build2_loc (loc,
12657                                                           BIT_XOR_EXPR, itype,
12658                                                           arg01, arg11),
12659                                              arg00),
12660                                 build_int_cst (itype, 0));
12661         }
12662
12663       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12664           && TREE_CODE (arg1) == BIT_XOR_EXPR)
12665         {
12666           tree arg00 = TREE_OPERAND (arg0, 0);
12667           tree arg01 = TREE_OPERAND (arg0, 1);
12668           tree arg10 = TREE_OPERAND (arg1, 0);
12669           tree arg11 = TREE_OPERAND (arg1, 1);
12670           tree itype = TREE_TYPE (arg0);
12671
12672           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12673              operand_equal_p guarantees no side-effects so we don't need
12674              to use omit_one_operand on Z.  */
12675           if (operand_equal_p (arg01, arg11, 0))
12676             return fold_build2_loc (loc, code, type, arg00, arg10);
12677           if (operand_equal_p (arg01, arg10, 0))
12678             return fold_build2_loc (loc, code, type, arg00, arg11);
12679           if (operand_equal_p (arg00, arg11, 0))
12680             return fold_build2_loc (loc, code, type, arg01, arg10);
12681           if (operand_equal_p (arg00, arg10, 0))
12682             return fold_build2_loc (loc, code, type, arg01, arg11);
12683
12684           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
12685           if (TREE_CODE (arg01) == INTEGER_CST
12686               && TREE_CODE (arg11) == INTEGER_CST)
12687             return fold_build2_loc (loc, code, type,
12688                                 fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00,
12689                                              fold_build2_loc (loc,
12690                                                           BIT_XOR_EXPR, itype,
12691                                                           arg01, arg11)),
12692                                 arg10);
12693         }
12694
12695       /* Attempt to simplify equality/inequality comparisons of complex
12696          values.  Only lower the comparison if the result is known or
12697          can be simplified to a single scalar comparison.  */
12698       if ((TREE_CODE (arg0) == COMPLEX_EXPR
12699            || TREE_CODE (arg0) == COMPLEX_CST)
12700           && (TREE_CODE (arg1) == COMPLEX_EXPR
12701               || TREE_CODE (arg1) == COMPLEX_CST))
12702         {
12703           tree real0, imag0, real1, imag1;
12704           tree rcond, icond;
12705
12706           if (TREE_CODE (arg0) == COMPLEX_EXPR)
12707             {
12708               real0 = TREE_OPERAND (arg0, 0);
12709               imag0 = TREE_OPERAND (arg0, 1);
12710             }
12711           else
12712             {
12713               real0 = TREE_REALPART (arg0);
12714               imag0 = TREE_IMAGPART (arg0);
12715             }
12716
12717           if (TREE_CODE (arg1) == COMPLEX_EXPR)
12718             {
12719               real1 = TREE_OPERAND (arg1, 0);
12720               imag1 = TREE_OPERAND (arg1, 1);
12721             }
12722           else
12723             {
12724               real1 = TREE_REALPART (arg1);
12725               imag1 = TREE_IMAGPART (arg1);
12726             }
12727
12728           rcond = fold_binary_loc (loc, code, type, real0, real1);
12729           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12730             {
12731               if (integer_zerop (rcond))
12732                 {
12733                   if (code == EQ_EXPR)
12734                     return omit_two_operands_loc (loc, type, boolean_false_node,
12735                                               imag0, imag1);
12736                   return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12737                 }
12738               else
12739                 {
12740                   if (code == NE_EXPR)
12741                     return omit_two_operands_loc (loc, type, boolean_true_node,
12742                                               imag0, imag1);
12743                   return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12744                 }
12745             }
12746
12747           icond = fold_binary_loc (loc, code, type, imag0, imag1);
12748           if (icond && TREE_CODE (icond) == INTEGER_CST)
12749             {
12750               if (integer_zerop (icond))
12751                 {
12752                   if (code == EQ_EXPR)
12753                     return omit_two_operands_loc (loc, type, boolean_false_node,
12754                                               real0, real1);
12755                   return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12756                 }
12757               else
12758                 {
12759                   if (code == NE_EXPR)
12760                     return omit_two_operands_loc (loc, type, boolean_true_node,
12761                                               real0, real1);
12762                   return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12763                 }
12764             }
12765         }
12766
12767       return NULL_TREE;
12768
12769     case LT_EXPR:
12770     case GT_EXPR:
12771     case LE_EXPR:
12772     case GE_EXPR:
12773       tem = fold_comparison (loc, code, type, op0, op1);
12774       if (tem != NULL_TREE)
12775         return tem;
12776
12777       /* Transform comparisons of the form X +- C CMP X.  */
12778       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12779           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12780           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12781                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12782               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12783                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12784         {
12785           tree arg01 = TREE_OPERAND (arg0, 1);
12786           enum tree_code code0 = TREE_CODE (arg0);
12787           int is_positive;
12788
12789           if (TREE_CODE (arg01) == REAL_CST)
12790             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12791           else
12792             is_positive = tree_int_cst_sgn (arg01);
12793
12794           /* (X - c) > X becomes false.  */
12795           if (code == GT_EXPR
12796               && ((code0 == MINUS_EXPR && is_positive >= 0)
12797                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12798             {
12799               if (TREE_CODE (arg01) == INTEGER_CST
12800                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12801                 fold_overflow_warning (("assuming signed overflow does not "
12802                                         "occur when assuming that (X - c) > X "
12803                                         "is always false"),
12804                                        WARN_STRICT_OVERFLOW_ALL);
12805               return constant_boolean_node (0, type);
12806             }
12807
12808           /* Likewise (X + c) < X becomes false.  */
12809           if (code == LT_EXPR
12810               && ((code0 == PLUS_EXPR && is_positive >= 0)
12811                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12812             {
12813               if (TREE_CODE (arg01) == INTEGER_CST
12814                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12815                 fold_overflow_warning (("assuming signed overflow does not "
12816                                         "occur when assuming that "
12817                                         "(X + c) < X is always false"),
12818                                        WARN_STRICT_OVERFLOW_ALL);
12819               return constant_boolean_node (0, type);
12820             }
12821
12822           /* Convert (X - c) <= X to true.  */
12823           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12824               && code == LE_EXPR
12825               && ((code0 == MINUS_EXPR && is_positive >= 0)
12826                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12827             {
12828               if (TREE_CODE (arg01) == INTEGER_CST
12829                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12830                 fold_overflow_warning (("assuming signed overflow does not "
12831                                         "occur when assuming that "
12832                                         "(X - c) <= X is always true"),
12833                                        WARN_STRICT_OVERFLOW_ALL);
12834               return constant_boolean_node (1, type);
12835             }
12836
12837           /* Convert (X + c) >= X to true.  */
12838           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12839               && code == GE_EXPR
12840               && ((code0 == PLUS_EXPR && is_positive >= 0)
12841                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12842             {
12843               if (TREE_CODE (arg01) == INTEGER_CST
12844                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12845                 fold_overflow_warning (("assuming signed overflow does not "
12846                                         "occur when assuming that "
12847                                         "(X + c) >= X is always true"),
12848                                        WARN_STRICT_OVERFLOW_ALL);
12849               return constant_boolean_node (1, type);
12850             }
12851
12852           if (TREE_CODE (arg01) == INTEGER_CST)
12853             {
12854               /* Convert X + c > X and X - c < X to true for integers.  */
12855               if (code == GT_EXPR
12856                   && ((code0 == PLUS_EXPR && is_positive > 0)
12857                       || (code0 == MINUS_EXPR && is_positive < 0)))
12858                 {
12859                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12860                     fold_overflow_warning (("assuming signed overflow does "
12861                                             "not occur when assuming that "
12862                                             "(X + c) > X is always true"),
12863                                            WARN_STRICT_OVERFLOW_ALL);
12864                   return constant_boolean_node (1, type);
12865                 }
12866
12867               if (code == LT_EXPR
12868                   && ((code0 == MINUS_EXPR && is_positive > 0)
12869                       || (code0 == PLUS_EXPR && is_positive < 0)))
12870                 {
12871                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12872                     fold_overflow_warning (("assuming signed overflow does "
12873                                             "not occur when assuming that "
12874                                             "(X - c) < X is always true"),
12875                                            WARN_STRICT_OVERFLOW_ALL);
12876                   return constant_boolean_node (1, type);
12877                 }
12878
12879               /* Convert X + c <= X and X - c >= X to false for integers.  */
12880               if (code == LE_EXPR
12881                   && ((code0 == PLUS_EXPR && is_positive > 0)
12882                       || (code0 == MINUS_EXPR && is_positive < 0)))
12883                 {
12884                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12885                     fold_overflow_warning (("assuming signed overflow does "
12886                                             "not occur when assuming that "
12887                                             "(X + c) <= X is always false"),
12888                                            WARN_STRICT_OVERFLOW_ALL);
12889                   return constant_boolean_node (0, type);
12890                 }
12891
12892               if (code == GE_EXPR
12893                   && ((code0 == MINUS_EXPR && is_positive > 0)
12894                       || (code0 == PLUS_EXPR && is_positive < 0)))
12895                 {
12896                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12897                     fold_overflow_warning (("assuming signed overflow does "
12898                                             "not occur when assuming that "
12899                                             "(X - c) >= X is always false"),
12900                                            WARN_STRICT_OVERFLOW_ALL);
12901                   return constant_boolean_node (0, type);
12902                 }
12903             }
12904         }
12905
12906       /* Comparisons with the highest or lowest possible integer of
12907          the specified precision will have known values.  */
12908       {
12909         tree arg1_type = TREE_TYPE (arg1);
12910         unsigned int width = TYPE_PRECISION (arg1_type);
12911
12912         if (TREE_CODE (arg1) == INTEGER_CST
12913             && width <= 2 * HOST_BITS_PER_WIDE_INT
12914             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12915           {
12916             HOST_WIDE_INT signed_max_hi;
12917             unsigned HOST_WIDE_INT signed_max_lo;
12918             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12919
12920             if (width <= HOST_BITS_PER_WIDE_INT)
12921               {
12922                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12923                                 - 1;
12924                 signed_max_hi = 0;
12925                 max_hi = 0;
12926
12927                 if (TYPE_UNSIGNED (arg1_type))
12928                   {
12929                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12930                     min_lo = 0;
12931                     min_hi = 0;
12932                   }
12933                 else
12934                   {
12935                     max_lo = signed_max_lo;
12936                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12937                     min_hi = -1;
12938                   }
12939               }
12940             else
12941               {
12942                 width -= HOST_BITS_PER_WIDE_INT;
12943                 signed_max_lo = -1;
12944                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12945                                 - 1;
12946                 max_lo = -1;
12947                 min_lo = 0;
12948
12949                 if (TYPE_UNSIGNED (arg1_type))
12950                   {
12951                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12952                     min_hi = 0;
12953                   }
12954                 else
12955                   {
12956                     max_hi = signed_max_hi;
12957                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12958                   }
12959               }
12960
12961             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12962                 && TREE_INT_CST_LOW (arg1) == max_lo)
12963               switch (code)
12964                 {
12965                 case GT_EXPR:
12966                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12967
12968                 case GE_EXPR:
12969                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12970
12971                 case LE_EXPR:
12972                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12973
12974                 case LT_EXPR:
12975                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12976
12977                 /* The GE_EXPR and LT_EXPR cases above are not normally
12978                    reached because of previous transformations.  */
12979
12980                 default:
12981                   break;
12982                 }
12983             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12984                      == max_hi
12985                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12986               switch (code)
12987                 {
12988                 case GT_EXPR:
12989                   arg1 = const_binop (PLUS_EXPR, arg1,
12990                                       build_int_cst (TREE_TYPE (arg1), 1));
12991                   return fold_build2_loc (loc, EQ_EXPR, type,
12992                                       fold_convert_loc (loc,
12993                                                         TREE_TYPE (arg1), arg0),
12994                                       arg1);
12995                 case LE_EXPR:
12996                   arg1 = const_binop (PLUS_EXPR, arg1,
12997                                       build_int_cst (TREE_TYPE (arg1), 1));
12998                   return fold_build2_loc (loc, NE_EXPR, type,
12999                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13000                                                         arg0),
13001                                       arg1);
13002                 default:
13003                   break;
13004                 }
13005             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13006                      == min_hi
13007                      && TREE_INT_CST_LOW (arg1) == min_lo)
13008               switch (code)
13009                 {
13010                 case LT_EXPR:
13011                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13012
13013                 case LE_EXPR:
13014                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13015
13016                 case GE_EXPR:
13017                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13018
13019                 case GT_EXPR:
13020                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13021
13022                 default:
13023                   break;
13024                 }
13025             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13026                      == min_hi
13027                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
13028               switch (code)
13029                 {
13030                 case GE_EXPR:
13031                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
13032                   return fold_build2_loc (loc, NE_EXPR, type,
13033                                       fold_convert_loc (loc,
13034                                                         TREE_TYPE (arg1), arg0),
13035                                       arg1);
13036                 case LT_EXPR:
13037                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
13038                   return fold_build2_loc (loc, EQ_EXPR, type,
13039                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13040                                                         arg0),
13041                                       arg1);
13042                 default:
13043                   break;
13044                 }
13045
13046             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13047                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
13048                      && TYPE_UNSIGNED (arg1_type)
13049                      /* We will flip the signedness of the comparison operator
13050                         associated with the mode of arg1, so the sign bit is
13051                         specified by this mode.  Check that arg1 is the signed
13052                         max associated with this sign bit.  */
13053                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13054                      /* signed_type does not work on pointer types.  */
13055                      && INTEGRAL_TYPE_P (arg1_type))
13056               {
13057                 /* The following case also applies to X < signed_max+1
13058                    and X >= signed_max+1 because previous transformations.  */
13059                 if (code == LE_EXPR || code == GT_EXPR)
13060                   {
13061                     tree st;
13062                     st = signed_type_for (TREE_TYPE (arg1));
13063                     return fold_build2_loc (loc,
13064                                         code == LE_EXPR ? GE_EXPR : LT_EXPR,
13065                                         type, fold_convert_loc (loc, st, arg0),
13066                                         build_int_cst (st, 0));
13067                   }
13068               }
13069           }
13070       }
13071
13072       /* If we are comparing an ABS_EXPR with a constant, we can
13073          convert all the cases into explicit comparisons, but they may
13074          well not be faster than doing the ABS and one comparison.
13075          But ABS (X) <= C is a range comparison, which becomes a subtraction
13076          and a comparison, and is probably faster.  */
13077       if (code == LE_EXPR
13078           && TREE_CODE (arg1) == INTEGER_CST
13079           && TREE_CODE (arg0) == ABS_EXPR
13080           && ! TREE_SIDE_EFFECTS (arg0)
13081           && (0 != (tem = negate_expr (arg1)))
13082           && TREE_CODE (tem) == INTEGER_CST
13083           && !TREE_OVERFLOW (tem))
13084         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13085                             build2 (GE_EXPR, type,
13086                                     TREE_OPERAND (arg0, 0), tem),
13087                             build2 (LE_EXPR, type,
13088                                     TREE_OPERAND (arg0, 0), arg1));
13089
13090       /* Convert ABS_EXPR<x> >= 0 to true.  */
13091       strict_overflow_p = false;
13092       if (code == GE_EXPR
13093           && (integer_zerop (arg1)
13094               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13095                   && real_zerop (arg1)))
13096           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13097         {
13098           if (strict_overflow_p)
13099             fold_overflow_warning (("assuming signed overflow does not occur "
13100                                     "when simplifying comparison of "
13101                                     "absolute value and zero"),
13102                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13103           return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13104         }
13105
13106       /* Convert ABS_EXPR<x> < 0 to false.  */
13107       strict_overflow_p = false;
13108       if (code == LT_EXPR
13109           && (integer_zerop (arg1) || real_zerop (arg1))
13110           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13111         {
13112           if (strict_overflow_p)
13113             fold_overflow_warning (("assuming signed overflow does not occur "
13114                                     "when simplifying comparison of "
13115                                     "absolute value and zero"),
13116                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13117           return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13118         }
13119
13120       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13121          and similarly for >= into !=.  */
13122       if ((code == LT_EXPR || code == GE_EXPR)
13123           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13124           && TREE_CODE (arg1) == LSHIFT_EXPR
13125           && integer_onep (TREE_OPERAND (arg1, 0)))
13126         {
13127           tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13128                         build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13129                                 TREE_OPERAND (arg1, 1)),
13130                         build_int_cst (TREE_TYPE (arg0), 0));
13131           goto fold_binary_exit;
13132         }
13133
13134       if ((code == LT_EXPR || code == GE_EXPR)
13135           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13136           && CONVERT_EXPR_P (arg1)
13137           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13138           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13139         {
13140           tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13141                         fold_convert_loc (loc, TREE_TYPE (arg0),
13142                                           build2 (RSHIFT_EXPR,
13143                                                   TREE_TYPE (arg0), arg0,
13144                                                   TREE_OPERAND (TREE_OPERAND (arg1, 0),
13145                                                                 1))),
13146                         build_int_cst (TREE_TYPE (arg0), 0));
13147           goto fold_binary_exit;
13148         }
13149
13150       return NULL_TREE;
13151
13152     case UNORDERED_EXPR:
13153     case ORDERED_EXPR:
13154     case UNLT_EXPR:
13155     case UNLE_EXPR:
13156     case UNGT_EXPR:
13157     case UNGE_EXPR:
13158     case UNEQ_EXPR:
13159     case LTGT_EXPR:
13160       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13161         {
13162           t1 = fold_relational_const (code, type, arg0, arg1);
13163           if (t1 != NULL_TREE)
13164             return t1;
13165         }
13166
13167       /* If the first operand is NaN, the result is constant.  */
13168       if (TREE_CODE (arg0) == REAL_CST
13169           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13170           && (code != LTGT_EXPR || ! flag_trapping_math))
13171         {
13172           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13173                ? integer_zero_node
13174                : integer_one_node;
13175           return omit_one_operand_loc (loc, type, t1, arg1);
13176         }
13177
13178       /* If the second operand is NaN, the result is constant.  */
13179       if (TREE_CODE (arg1) == REAL_CST
13180           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13181           && (code != LTGT_EXPR || ! flag_trapping_math))
13182         {
13183           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13184                ? integer_zero_node
13185                : integer_one_node;
13186           return omit_one_operand_loc (loc, type, t1, arg0);
13187         }
13188
13189       /* Simplify unordered comparison of something with itself.  */
13190       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13191           && operand_equal_p (arg0, arg1, 0))
13192         return constant_boolean_node (1, type);
13193
13194       if (code == LTGT_EXPR
13195           && !flag_trapping_math
13196           && operand_equal_p (arg0, arg1, 0))
13197         return constant_boolean_node (0, type);
13198
13199       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
13200       {
13201         tree targ0 = strip_float_extensions (arg0);
13202         tree targ1 = strip_float_extensions (arg1);
13203         tree newtype = TREE_TYPE (targ0);
13204
13205         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13206           newtype = TREE_TYPE (targ1);
13207
13208         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13209           return fold_build2_loc (loc, code, type,
13210                               fold_convert_loc (loc, newtype, targ0),
13211                               fold_convert_loc (loc, newtype, targ1));
13212       }
13213
13214       return NULL_TREE;
13215
13216     case COMPOUND_EXPR:
13217       /* When pedantic, a compound expression can be neither an lvalue
13218          nor an integer constant expression.  */
13219       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13220         return NULL_TREE;
13221       /* Don't let (0, 0) be null pointer constant.  */
13222       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13223                                  : fold_convert_loc (loc, type, arg1);
13224       return pedantic_non_lvalue_loc (loc, tem);
13225
13226     case COMPLEX_EXPR:
13227       if ((TREE_CODE (arg0) == REAL_CST
13228            && TREE_CODE (arg1) == REAL_CST)
13229           || (TREE_CODE (arg0) == INTEGER_CST
13230               && TREE_CODE (arg1) == INTEGER_CST))
13231         return build_complex (type, arg0, arg1);
13232       return NULL_TREE;
13233
13234     case ASSERT_EXPR:
13235       /* An ASSERT_EXPR should never be passed to fold_binary.  */
13236       gcc_unreachable ();
13237
13238     default:
13239       return NULL_TREE;
13240     } /* switch (code) */
13241  fold_binary_exit:
13242   protected_set_expr_location (tem, loc);
13243   return tem;
13244 }
13245
13246 /* Callback for walk_tree, looking for LABEL_EXPR.  Return *TP if it is
13247    a LABEL_EXPR; otherwise return NULL_TREE.  Do not check the subtrees
13248    of GOTO_EXPR.  */
13249
13250 static tree
13251 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13252 {
13253   switch (TREE_CODE (*tp))
13254     {
13255     case LABEL_EXPR:
13256       return *tp;
13257
13258     case GOTO_EXPR:
13259       *walk_subtrees = 0;
13260
13261       /* ... fall through ...  */
13262
13263     default:
13264       return NULL_TREE;
13265     }
13266 }
13267
13268 /* Return whether the sub-tree ST contains a label which is accessible from
13269    outside the sub-tree.  */
13270
13271 static bool
13272 contains_label_p (tree st)
13273 {
13274   return
13275    (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13276 }
13277
13278 /* Fold a ternary expression of code CODE and type TYPE with operands
13279    OP0, OP1, and OP2.  Return the folded expression if folding is
13280    successful.  Otherwise, return NULL_TREE.  */
13281
13282 tree
13283 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13284               tree op0, tree op1, tree op2)
13285 {
13286   tree tem;
13287   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13288   enum tree_code_class kind = TREE_CODE_CLASS (code);
13289
13290   gcc_assert (IS_EXPR_CODE_CLASS (kind)
13291               && TREE_CODE_LENGTH (code) == 3);
13292
13293   /* Strip any conversions that don't change the mode.  This is safe
13294      for every expression, except for a comparison expression because
13295      its signedness is derived from its operands.  So, in the latter
13296      case, only strip conversions that don't change the signedness.
13297
13298      Note that this is done as an internal manipulation within the
13299      constant folder, in order to find the simplest representation of
13300      the arguments so that their form can be studied.  In any cases,
13301      the appropriate type conversions should be put back in the tree
13302      that will get out of the constant folder.  */
13303   if (op0)
13304     {
13305       arg0 = op0;
13306       STRIP_NOPS (arg0);
13307     }
13308
13309   if (op1)
13310     {
13311       arg1 = op1;
13312       STRIP_NOPS (arg1);
13313     }
13314
13315   switch (code)
13316     {
13317     case COMPONENT_REF:
13318       if (TREE_CODE (arg0) == CONSTRUCTOR
13319           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13320         {
13321           unsigned HOST_WIDE_INT idx;
13322           tree field, value;
13323           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13324             if (field == arg1)
13325               return value;
13326         }
13327       return NULL_TREE;
13328
13329     case COND_EXPR:
13330       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13331          so all simple results must be passed through pedantic_non_lvalue.  */
13332       if (TREE_CODE (arg0) == INTEGER_CST)
13333         {
13334           tree unused_op = integer_zerop (arg0) ? op1 : op2;
13335           tem = integer_zerop (arg0) ? op2 : op1;
13336           /* Only optimize constant conditions when the selected branch
13337              has the same type as the COND_EXPR.  This avoids optimizing
13338              away "c ? x : throw", where the throw has a void type.
13339              Avoid throwing away that operand which contains label.  */
13340           if ((!TREE_SIDE_EFFECTS (unused_op)
13341                || !contains_label_p (unused_op))
13342               && (! VOID_TYPE_P (TREE_TYPE (tem))
13343                   || VOID_TYPE_P (type)))
13344             return pedantic_non_lvalue_loc (loc, tem);
13345           return NULL_TREE;
13346         }
13347       if (operand_equal_p (arg1, op2, 0))
13348         return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13349
13350       /* If we have A op B ? A : C, we may be able to convert this to a
13351          simpler expression, depending on the operation and the values
13352          of B and C.  Signed zeros prevent all of these transformations,
13353          for reasons given above each one.
13354
13355          Also try swapping the arguments and inverting the conditional.  */
13356       if (COMPARISON_CLASS_P (arg0)
13357           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13358                                              arg1, TREE_OPERAND (arg0, 1))
13359           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13360         {
13361           tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13362           if (tem)
13363             return tem;
13364         }
13365
13366       if (COMPARISON_CLASS_P (arg0)
13367           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13368                                              op2,
13369                                              TREE_OPERAND (arg0, 1))
13370           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13371         {
13372           tem = fold_truth_not_expr (loc, arg0);
13373           if (tem && COMPARISON_CLASS_P (tem))
13374             {
13375               tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13376               if (tem)
13377                 return tem;
13378             }
13379         }
13380
13381       /* If the second operand is simpler than the third, swap them
13382          since that produces better jump optimization results.  */
13383       if (truth_value_p (TREE_CODE (arg0))
13384           && tree_swap_operands_p (op1, op2, false))
13385         {
13386           /* See if this can be inverted.  If it can't, possibly because
13387              it was a floating-point inequality comparison, don't do
13388              anything.  */
13389           tem = fold_truth_not_expr (loc, arg0);
13390           if (tem)
13391             return fold_build3_loc (loc, code, type, tem, op2, op1);
13392         }
13393
13394       /* Convert A ? 1 : 0 to simply A.  */
13395       if (integer_onep (op1)
13396           && integer_zerop (op2)
13397           /* If we try to convert OP0 to our type, the
13398              call to fold will try to move the conversion inside
13399              a COND, which will recurse.  In that case, the COND_EXPR
13400              is probably the best choice, so leave it alone.  */
13401           && type == TREE_TYPE (arg0))
13402         return pedantic_non_lvalue_loc (loc, arg0);
13403
13404       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
13405          over COND_EXPR in cases such as floating point comparisons.  */
13406       if (integer_zerop (op1)
13407           && integer_onep (op2)
13408           && truth_value_p (TREE_CODE (arg0)))
13409         return pedantic_non_lvalue_loc (loc,
13410                                     fold_convert_loc (loc, type,
13411                                               invert_truthvalue_loc (loc,
13412                                                                      arg0)));
13413
13414       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
13415       if (TREE_CODE (arg0) == LT_EXPR
13416           && integer_zerop (TREE_OPERAND (arg0, 1))
13417           && integer_zerop (op2)
13418           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13419         {
13420           /* sign_bit_p only checks ARG1 bits within A's precision.
13421              If <sign bit of A> has wider type than A, bits outside
13422              of A's precision in <sign bit of A> need to be checked.
13423              If they are all 0, this optimization needs to be done
13424              in unsigned A's type, if they are all 1 in signed A's type,
13425              otherwise this can't be done.  */
13426           if (TYPE_PRECISION (TREE_TYPE (tem))
13427               < TYPE_PRECISION (TREE_TYPE (arg1))
13428               && TYPE_PRECISION (TREE_TYPE (tem))
13429                  < TYPE_PRECISION (type))
13430             {
13431               unsigned HOST_WIDE_INT mask_lo;
13432               HOST_WIDE_INT mask_hi;
13433               int inner_width, outer_width;
13434               tree tem_type;
13435
13436               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13437               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13438               if (outer_width > TYPE_PRECISION (type))
13439                 outer_width = TYPE_PRECISION (type);
13440
13441               if (outer_width > HOST_BITS_PER_WIDE_INT)
13442                 {
13443                   mask_hi = ((unsigned HOST_WIDE_INT) -1
13444                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13445                   mask_lo = -1;
13446                 }
13447               else
13448                 {
13449                   mask_hi = 0;
13450                   mask_lo = ((unsigned HOST_WIDE_INT) -1
13451                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
13452                 }
13453               if (inner_width > HOST_BITS_PER_WIDE_INT)
13454                 {
13455                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13456                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
13457                   mask_lo = 0;
13458                 }
13459               else
13460                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13461                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
13462
13463               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13464                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13465                 {
13466                   tem_type = signed_type_for (TREE_TYPE (tem));
13467                   tem = fold_convert_loc (loc, tem_type, tem);
13468                 }
13469               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13470                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13471                 {
13472                   tem_type = unsigned_type_for (TREE_TYPE (tem));
13473                   tem = fold_convert_loc (loc, tem_type, tem);
13474                 }
13475               else
13476                 tem = NULL;
13477             }
13478
13479           if (tem)
13480             return
13481               fold_convert_loc (loc, type,
13482                                 fold_build2_loc (loc, BIT_AND_EXPR,
13483                                              TREE_TYPE (tem), tem,
13484                                              fold_convert_loc (loc,
13485                                                                TREE_TYPE (tem),
13486                                                                arg1)));
13487         }
13488
13489       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
13490          already handled above.  */
13491       if (TREE_CODE (arg0) == BIT_AND_EXPR
13492           && integer_onep (TREE_OPERAND (arg0, 1))
13493           && integer_zerop (op2)
13494           && integer_pow2p (arg1))
13495         {
13496           tree tem = TREE_OPERAND (arg0, 0);
13497           STRIP_NOPS (tem);
13498           if (TREE_CODE (tem) == RSHIFT_EXPR
13499               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13500               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13501                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13502             return fold_build2_loc (loc, BIT_AND_EXPR, type,
13503                                 TREE_OPERAND (tem, 0), arg1);
13504         }
13505
13506       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13507          is probably obsolete because the first operand should be a
13508          truth value (that's why we have the two cases above), but let's
13509          leave it in until we can confirm this for all front-ends.  */
13510       if (integer_zerop (op2)
13511           && TREE_CODE (arg0) == NE_EXPR
13512           && integer_zerop (TREE_OPERAND (arg0, 1))
13513           && integer_pow2p (arg1)
13514           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13515           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13516                               arg1, OEP_ONLY_CONST))
13517         return pedantic_non_lvalue_loc (loc,
13518                                     fold_convert_loc (loc, type,
13519                                                       TREE_OPERAND (arg0, 0)));
13520
13521       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13522       if (integer_zerop (op2)
13523           && truth_value_p (TREE_CODE (arg0))
13524           && truth_value_p (TREE_CODE (arg1)))
13525         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13526                             fold_convert_loc (loc, type, arg0),
13527                             arg1);
13528
13529       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13530       if (integer_onep (op2)
13531           && truth_value_p (TREE_CODE (arg0))
13532           && truth_value_p (TREE_CODE (arg1)))
13533         {
13534           /* Only perform transformation if ARG0 is easily inverted.  */
13535           tem = fold_truth_not_expr (loc, arg0);
13536           if (tem)
13537             return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13538                                 fold_convert_loc (loc, type, tem),
13539                                 arg1);
13540         }
13541
13542       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13543       if (integer_zerop (arg1)
13544           && truth_value_p (TREE_CODE (arg0))
13545           && truth_value_p (TREE_CODE (op2)))
13546         {
13547           /* Only perform transformation if ARG0 is easily inverted.  */
13548           tem = fold_truth_not_expr (loc, arg0);
13549           if (tem)
13550             return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13551                                 fold_convert_loc (loc, type, tem),
13552                                 op2);
13553         }
13554
13555       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13556       if (integer_onep (arg1)
13557           && truth_value_p (TREE_CODE (arg0))
13558           && truth_value_p (TREE_CODE (op2)))
13559         return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13560                             fold_convert_loc (loc, type, arg0),
13561                             op2);
13562
13563       return NULL_TREE;
13564
13565     case CALL_EXPR:
13566       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
13567          of fold_ternary on them.  */
13568       gcc_unreachable ();
13569
13570     case BIT_FIELD_REF:
13571       if ((TREE_CODE (arg0) == VECTOR_CST
13572            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13573           && type == TREE_TYPE (TREE_TYPE (arg0)))
13574         {
13575           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13576           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13577
13578           if (width != 0
13579               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13580               && (idx % width) == 0
13581               && (idx = idx / width)
13582                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13583             {
13584               tree elements = NULL_TREE;
13585
13586               if (TREE_CODE (arg0) == VECTOR_CST)
13587                 elements = TREE_VECTOR_CST_ELTS (arg0);
13588               else
13589                 {
13590                   unsigned HOST_WIDE_INT idx;
13591                   tree value;
13592
13593                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13594                     elements = tree_cons (NULL_TREE, value, elements);
13595                 }
13596               while (idx-- > 0 && elements)
13597                 elements = TREE_CHAIN (elements);
13598               if (elements)
13599                 return TREE_VALUE (elements);
13600               else
13601                 return fold_convert_loc (loc, type, integer_zero_node);
13602             }
13603         }
13604
13605       /* A bit-field-ref that referenced the full argument can be stripped.  */
13606       if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13607           && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13608           && integer_zerop (op2))
13609         return fold_convert_loc (loc, type, arg0);
13610
13611       return NULL_TREE;
13612
13613     default:
13614       return NULL_TREE;
13615     } /* switch (code) */
13616 }
13617
13618 /* Perform constant folding and related simplification of EXPR.
13619    The related simplifications include x*1 => x, x*0 => 0, etc.,
13620    and application of the associative law.
13621    NOP_EXPR conversions may be removed freely (as long as we
13622    are careful not to change the type of the overall expression).
13623    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13624    but we can constant-fold them if they have constant operands.  */
13625
13626 #ifdef ENABLE_FOLD_CHECKING
13627 # define fold(x) fold_1 (x)
13628 static tree fold_1 (tree);
13629 static
13630 #endif
13631 tree
13632 fold (tree expr)
13633 {
13634   const tree t = expr;
13635   enum tree_code code = TREE_CODE (t);
13636   enum tree_code_class kind = TREE_CODE_CLASS (code);
13637   tree tem;
13638   location_t loc = EXPR_LOCATION (expr);
13639
13640   /* Return right away if a constant.  */
13641   if (kind == tcc_constant)
13642     return t;
13643
13644   /* CALL_EXPR-like objects with variable numbers of operands are
13645      treated specially.  */
13646   if (kind == tcc_vl_exp)
13647     {
13648       if (code == CALL_EXPR)
13649         {
13650           tem = fold_call_expr (loc, expr, false);
13651           return tem ? tem : expr;
13652         }
13653       return expr;
13654     }
13655
13656   if (IS_EXPR_CODE_CLASS (kind))
13657     {
13658       tree type = TREE_TYPE (t);
13659       tree op0, op1, op2;
13660
13661       switch (TREE_CODE_LENGTH (code))
13662         {
13663         case 1:
13664           op0 = TREE_OPERAND (t, 0);
13665           tem = fold_unary_loc (loc, code, type, op0);
13666           return tem ? tem : expr;
13667         case 2:
13668           op0 = TREE_OPERAND (t, 0);
13669           op1 = TREE_OPERAND (t, 1);
13670           tem = fold_binary_loc (loc, code, type, op0, op1);
13671           return tem ? tem : expr;
13672         case 3:
13673           op0 = TREE_OPERAND (t, 0);
13674           op1 = TREE_OPERAND (t, 1);
13675           op2 = TREE_OPERAND (t, 2);
13676           tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13677           return tem ? tem : expr;
13678         default:
13679           break;
13680         }
13681     }
13682
13683   switch (code)
13684     {
13685     case ARRAY_REF:
13686       {
13687         tree op0 = TREE_OPERAND (t, 0);
13688         tree op1 = TREE_OPERAND (t, 1);
13689
13690         if (TREE_CODE (op1) == INTEGER_CST
13691             && TREE_CODE (op0) == CONSTRUCTOR
13692             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13693           {
13694             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13695             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13696             unsigned HOST_WIDE_INT begin = 0;
13697
13698             /* Find a matching index by means of a binary search.  */
13699             while (begin != end)
13700               {
13701                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13702                 tree index = VEC_index (constructor_elt, elts, middle)->index;
13703
13704                 if (TREE_CODE (index) == INTEGER_CST
13705                     && tree_int_cst_lt (index, op1))
13706                   begin = middle + 1;
13707                 else if (TREE_CODE (index) == INTEGER_CST
13708                          && tree_int_cst_lt (op1, index))
13709                   end = middle;
13710                 else if (TREE_CODE (index) == RANGE_EXPR
13711                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13712                   begin = middle + 1;
13713                 else if (TREE_CODE (index) == RANGE_EXPR
13714                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13715                   end = middle;
13716                 else
13717                   return VEC_index (constructor_elt, elts, middle)->value;
13718               }
13719           }
13720
13721         return t;
13722       }
13723
13724     case CONST_DECL:
13725       return fold (DECL_INITIAL (t));
13726
13727     default:
13728       return t;
13729     } /* switch (code) */
13730 }
13731
13732 #ifdef ENABLE_FOLD_CHECKING
13733 #undef fold
13734
13735 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13736 static void fold_check_failed (const_tree, const_tree);
13737 void print_fold_checksum (const_tree);
13738
13739 /* When --enable-checking=fold, compute a digest of expr before
13740    and after actual fold call to see if fold did not accidentally
13741    change original expr.  */
13742
13743 tree
13744 fold (tree expr)
13745 {
13746   tree ret;
13747   struct md5_ctx ctx;
13748   unsigned char checksum_before[16], checksum_after[16];
13749   htab_t ht;
13750
13751   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13752   md5_init_ctx (&ctx);
13753   fold_checksum_tree (expr, &ctx, ht);
13754   md5_finish_ctx (&ctx, checksum_before);
13755   htab_empty (ht);
13756
13757   ret = fold_1 (expr);
13758
13759   md5_init_ctx (&ctx);
13760   fold_checksum_tree (expr, &ctx, ht);
13761   md5_finish_ctx (&ctx, checksum_after);
13762   htab_delete (ht);
13763
13764   if (memcmp (checksum_before, checksum_after, 16))
13765     fold_check_failed (expr, ret);
13766
13767   return ret;
13768 }
13769
13770 void
13771 print_fold_checksum (const_tree expr)
13772 {
13773   struct md5_ctx ctx;
13774   unsigned char checksum[16], cnt;
13775   htab_t ht;
13776
13777   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13778   md5_init_ctx (&ctx);
13779   fold_checksum_tree (expr, &ctx, ht);
13780   md5_finish_ctx (&ctx, checksum);
13781   htab_delete (ht);
13782   for (cnt = 0; cnt < 16; ++cnt)
13783     fprintf (stderr, "%02x", checksum[cnt]);
13784   putc ('\n', stderr);
13785 }
13786
13787 static void
13788 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13789 {
13790   internal_error ("fold check: original tree changed by fold");
13791 }
13792
13793 static void
13794 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13795 {
13796   void **slot;
13797   enum tree_code code;
13798   union tree_node buf;
13799   int i, len;
13800
13801 recursive_label:
13802
13803   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13804                <= sizeof (struct tree_function_decl))
13805               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13806   if (expr == NULL)
13807     return;
13808   slot = (void **) htab_find_slot (ht, expr, INSERT);
13809   if (*slot != NULL)
13810     return;
13811   *slot = CONST_CAST_TREE (expr);
13812   code = TREE_CODE (expr);
13813   if (TREE_CODE_CLASS (code) == tcc_declaration
13814       && DECL_ASSEMBLER_NAME_SET_P (expr))
13815     {
13816       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
13817       memcpy ((char *) &buf, expr, tree_size (expr));
13818       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13819       expr = (tree) &buf;
13820     }
13821   else if (TREE_CODE_CLASS (code) == tcc_type
13822            && (TYPE_POINTER_TO (expr)
13823                || TYPE_REFERENCE_TO (expr)
13824                || TYPE_CACHED_VALUES_P (expr)
13825                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13826                || TYPE_NEXT_VARIANT (expr)))
13827     {
13828       /* Allow these fields to be modified.  */
13829       tree tmp;
13830       memcpy ((char *) &buf, expr, tree_size (expr));
13831       expr = tmp = (tree) &buf;
13832       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13833       TYPE_POINTER_TO (tmp) = NULL;
13834       TYPE_REFERENCE_TO (tmp) = NULL;
13835       TYPE_NEXT_VARIANT (tmp) = NULL;
13836       if (TYPE_CACHED_VALUES_P (tmp))
13837         {
13838           TYPE_CACHED_VALUES_P (tmp) = 0;
13839           TYPE_CACHED_VALUES (tmp) = NULL;
13840         }
13841     }
13842   md5_process_bytes (expr, tree_size (expr), ctx);
13843   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13844   if (TREE_CODE_CLASS (code) != tcc_type
13845       && TREE_CODE_CLASS (code) != tcc_declaration
13846       && code != TREE_LIST
13847       && code != SSA_NAME)
13848     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13849   switch (TREE_CODE_CLASS (code))
13850     {
13851     case tcc_constant:
13852       switch (code)
13853         {
13854         case STRING_CST:
13855           md5_process_bytes (TREE_STRING_POINTER (expr),
13856                              TREE_STRING_LENGTH (expr), ctx);
13857           break;
13858         case COMPLEX_CST:
13859           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13860           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13861           break;
13862         case VECTOR_CST:
13863           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13864           break;
13865         default:
13866           break;
13867         }
13868       break;
13869     case tcc_exceptional:
13870       switch (code)
13871         {
13872         case TREE_LIST:
13873           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13874           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13875           expr = TREE_CHAIN (expr);
13876           goto recursive_label;
13877           break;
13878         case TREE_VEC:
13879           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13880             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13881           break;
13882         default:
13883           break;
13884         }
13885       break;
13886     case tcc_expression:
13887     case tcc_reference:
13888     case tcc_comparison:
13889     case tcc_unary:
13890     case tcc_binary:
13891     case tcc_statement:
13892     case tcc_vl_exp:
13893       len = TREE_OPERAND_LENGTH (expr);
13894       for (i = 0; i < len; ++i)
13895         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13896       break;
13897     case tcc_declaration:
13898       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13899       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13900       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13901         {
13902           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13903           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13904           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13905           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13906           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13907         }
13908       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13909         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13910
13911       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13912         {
13913           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13914           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13915           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13916         }
13917       break;
13918     case tcc_type:
13919       if (TREE_CODE (expr) == ENUMERAL_TYPE)
13920         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13921       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13922       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13923       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13924       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13925       if (INTEGRAL_TYPE_P (expr)
13926           || SCALAR_FLOAT_TYPE_P (expr))
13927         {
13928           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13929           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13930         }
13931       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13932       if (TREE_CODE (expr) == RECORD_TYPE
13933           || TREE_CODE (expr) == UNION_TYPE
13934           || TREE_CODE (expr) == QUAL_UNION_TYPE)
13935         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13936       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13937       break;
13938     default:
13939       break;
13940     }
13941 }
13942
13943 /* Helper function for outputting the checksum of a tree T.  When
13944    debugging with gdb, you can "define mynext" to be "next" followed
13945    by "call debug_fold_checksum (op0)", then just trace down till the
13946    outputs differ.  */
13947
13948 DEBUG_FUNCTION void
13949 debug_fold_checksum (const_tree t)
13950 {
13951   int i;
13952   unsigned char checksum[16];
13953   struct md5_ctx ctx;
13954   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13955
13956   md5_init_ctx (&ctx);
13957   fold_checksum_tree (t, &ctx, ht);
13958   md5_finish_ctx (&ctx, checksum);
13959   htab_empty (ht);
13960
13961   for (i = 0; i < 16; i++)
13962     fprintf (stderr, "%d ", checksum[i]);
13963
13964   fprintf (stderr, "\n");
13965 }
13966
13967 #endif
13968
13969 /* Fold a unary tree expression with code CODE of type TYPE with an
13970    operand OP0.  LOC is the location of the resulting expression.
13971    Return a folded expression if successful.  Otherwise, return a tree
13972    expression with code CODE of type TYPE with an operand OP0.  */
13973
13974 tree
13975 fold_build1_stat_loc (location_t loc,
13976                       enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13977 {
13978   tree tem;
13979 #ifdef ENABLE_FOLD_CHECKING
13980   unsigned char checksum_before[16], checksum_after[16];
13981   struct md5_ctx ctx;
13982   htab_t ht;
13983
13984   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13985   md5_init_ctx (&ctx);
13986   fold_checksum_tree (op0, &ctx, ht);
13987   md5_finish_ctx (&ctx, checksum_before);
13988   htab_empty (ht);
13989 #endif
13990
13991   tem = fold_unary_loc (loc, code, type, op0);
13992   if (!tem)
13993     {
13994       tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13995       SET_EXPR_LOCATION (tem, loc);
13996     }
13997
13998 #ifdef ENABLE_FOLD_CHECKING
13999   md5_init_ctx (&ctx);
14000   fold_checksum_tree (op0, &ctx, ht);
14001   md5_finish_ctx (&ctx, checksum_after);
14002   htab_delete (ht);
14003
14004   if (memcmp (checksum_before, checksum_after, 16))
14005     fold_check_failed (op0, tem);
14006 #endif
14007   return tem;
14008 }
14009
14010 /* Fold a binary tree expression with code CODE of type TYPE with
14011    operands OP0 and OP1.  LOC is the location of the resulting
14012    expression.  Return a folded expression if successful.  Otherwise,
14013    return a tree expression with code CODE of type TYPE with operands
14014    OP0 and OP1.  */
14015
14016 tree
14017 fold_build2_stat_loc (location_t loc,
14018                       enum tree_code code, tree type, tree op0, tree op1
14019                       MEM_STAT_DECL)
14020 {
14021   tree tem;
14022 #ifdef ENABLE_FOLD_CHECKING
14023   unsigned char checksum_before_op0[16],
14024                 checksum_before_op1[16],
14025                 checksum_after_op0[16],
14026                 checksum_after_op1[16];
14027   struct md5_ctx ctx;
14028   htab_t ht;
14029
14030   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14031   md5_init_ctx (&ctx);
14032   fold_checksum_tree (op0, &ctx, ht);
14033   md5_finish_ctx (&ctx, checksum_before_op0);
14034   htab_empty (ht);
14035
14036   md5_init_ctx (&ctx);
14037   fold_checksum_tree (op1, &ctx, ht);
14038   md5_finish_ctx (&ctx, checksum_before_op1);
14039   htab_empty (ht);
14040 #endif
14041
14042   tem = fold_binary_loc (loc, code, type, op0, op1);
14043   if (!tem)
14044     {
14045       tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
14046       SET_EXPR_LOCATION (tem, loc);
14047     }
14048
14049 #ifdef ENABLE_FOLD_CHECKING
14050   md5_init_ctx (&ctx);
14051   fold_checksum_tree (op0, &ctx, ht);
14052   md5_finish_ctx (&ctx, checksum_after_op0);
14053   htab_empty (ht);
14054
14055   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14056     fold_check_failed (op0, tem);
14057
14058   md5_init_ctx (&ctx);
14059   fold_checksum_tree (op1, &ctx, ht);
14060   md5_finish_ctx (&ctx, checksum_after_op1);
14061   htab_delete (ht);
14062
14063   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14064     fold_check_failed (op1, tem);
14065 #endif
14066   return tem;
14067 }
14068
14069 /* Fold a ternary tree expression with code CODE of type TYPE with
14070    operands OP0, OP1, and OP2.  Return a folded expression if
14071    successful.  Otherwise, return a tree expression with code CODE of
14072    type TYPE with operands OP0, OP1, and OP2.  */
14073
14074 tree
14075 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14076                       tree op0, tree op1, tree op2 MEM_STAT_DECL)
14077 {
14078   tree tem;
14079 #ifdef ENABLE_FOLD_CHECKING
14080   unsigned char checksum_before_op0[16],
14081                 checksum_before_op1[16],
14082                 checksum_before_op2[16],
14083                 checksum_after_op0[16],
14084                 checksum_after_op1[16],
14085                 checksum_after_op2[16];
14086   struct md5_ctx ctx;
14087   htab_t ht;
14088
14089   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14090   md5_init_ctx (&ctx);
14091   fold_checksum_tree (op0, &ctx, ht);
14092   md5_finish_ctx (&ctx, checksum_before_op0);
14093   htab_empty (ht);
14094
14095   md5_init_ctx (&ctx);
14096   fold_checksum_tree (op1, &ctx, ht);
14097   md5_finish_ctx (&ctx, checksum_before_op1);
14098   htab_empty (ht);
14099
14100   md5_init_ctx (&ctx);
14101   fold_checksum_tree (op2, &ctx, ht);
14102   md5_finish_ctx (&ctx, checksum_before_op2);
14103   htab_empty (ht);
14104 #endif
14105
14106   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14107   tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14108   if (!tem)
14109     {
14110       tem =  build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
14111       SET_EXPR_LOCATION (tem, loc);
14112     }
14113
14114 #ifdef ENABLE_FOLD_CHECKING
14115   md5_init_ctx (&ctx);
14116   fold_checksum_tree (op0, &ctx, ht);
14117   md5_finish_ctx (&ctx, checksum_after_op0);
14118   htab_empty (ht);
14119
14120   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14121     fold_check_failed (op0, tem);
14122
14123   md5_init_ctx (&ctx);
14124   fold_checksum_tree (op1, &ctx, ht);
14125   md5_finish_ctx (&ctx, checksum_after_op1);
14126   htab_empty (ht);
14127
14128   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14129     fold_check_failed (op1, tem);
14130
14131   md5_init_ctx (&ctx);
14132   fold_checksum_tree (op2, &ctx, ht);
14133   md5_finish_ctx (&ctx, checksum_after_op2);
14134   htab_delete (ht);
14135
14136   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14137     fold_check_failed (op2, tem);
14138 #endif
14139   return tem;
14140 }
14141
14142 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14143    arguments in ARGARRAY, and a null static chain.
14144    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
14145    of type TYPE from the given operands as constructed by build_call_array.  */
14146
14147 tree
14148 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14149                            int nargs, tree *argarray)
14150 {
14151   tree tem;
14152 #ifdef ENABLE_FOLD_CHECKING
14153   unsigned char checksum_before_fn[16],
14154                 checksum_before_arglist[16],
14155                 checksum_after_fn[16],
14156                 checksum_after_arglist[16];
14157   struct md5_ctx ctx;
14158   htab_t ht;
14159   int i;
14160
14161   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14162   md5_init_ctx (&ctx);
14163   fold_checksum_tree (fn, &ctx, ht);
14164   md5_finish_ctx (&ctx, checksum_before_fn);
14165   htab_empty (ht);
14166
14167   md5_init_ctx (&ctx);
14168   for (i = 0; i < nargs; i++)
14169     fold_checksum_tree (argarray[i], &ctx, ht);
14170   md5_finish_ctx (&ctx, checksum_before_arglist);
14171   htab_empty (ht);
14172 #endif
14173
14174   tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14175
14176 #ifdef ENABLE_FOLD_CHECKING
14177   md5_init_ctx (&ctx);
14178   fold_checksum_tree (fn, &ctx, ht);
14179   md5_finish_ctx (&ctx, checksum_after_fn);
14180   htab_empty (ht);
14181
14182   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14183     fold_check_failed (fn, tem);
14184
14185   md5_init_ctx (&ctx);
14186   for (i = 0; i < nargs; i++)
14187     fold_checksum_tree (argarray[i], &ctx, ht);
14188   md5_finish_ctx (&ctx, checksum_after_arglist);
14189   htab_delete (ht);
14190
14191   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14192     fold_check_failed (NULL_TREE, tem);
14193 #endif
14194   return tem;
14195 }
14196
14197 /* Perform constant folding and related simplification of initializer
14198    expression EXPR.  These behave identically to "fold_buildN" but ignore
14199    potential run-time traps and exceptions that fold must preserve.  */
14200
14201 #define START_FOLD_INIT \
14202   int saved_signaling_nans = flag_signaling_nans;\
14203   int saved_trapping_math = flag_trapping_math;\
14204   int saved_rounding_math = flag_rounding_math;\
14205   int saved_trapv = flag_trapv;\
14206   int saved_folding_initializer = folding_initializer;\
14207   flag_signaling_nans = 0;\
14208   flag_trapping_math = 0;\
14209   flag_rounding_math = 0;\
14210   flag_trapv = 0;\
14211   folding_initializer = 1;
14212
14213 #define END_FOLD_INIT \
14214   flag_signaling_nans = saved_signaling_nans;\
14215   flag_trapping_math = saved_trapping_math;\
14216   flag_rounding_math = saved_rounding_math;\
14217   flag_trapv = saved_trapv;\
14218   folding_initializer = saved_folding_initializer;
14219
14220 tree
14221 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14222                              tree type, tree op)
14223 {
14224   tree result;
14225   START_FOLD_INIT;
14226
14227   result = fold_build1_loc (loc, code, type, op);
14228
14229   END_FOLD_INIT;
14230   return result;
14231 }
14232
14233 tree
14234 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14235                              tree type, tree op0, tree op1)
14236 {
14237   tree result;
14238   START_FOLD_INIT;
14239
14240   result = fold_build2_loc (loc, code, type, op0, op1);
14241
14242   END_FOLD_INIT;
14243   return result;
14244 }
14245
14246 tree
14247 fold_build3_initializer_loc (location_t loc, enum tree_code code,
14248                              tree type, tree op0, tree op1, tree op2)
14249 {
14250   tree result;
14251   START_FOLD_INIT;
14252
14253   result = fold_build3_loc (loc, code, type, op0, op1, op2);
14254
14255   END_FOLD_INIT;
14256   return result;
14257 }
14258
14259 tree
14260 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14261                                        int nargs, tree *argarray)
14262 {
14263   tree result;
14264   START_FOLD_INIT;
14265
14266   result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14267
14268   END_FOLD_INIT;
14269   return result;
14270 }
14271
14272 #undef START_FOLD_INIT
14273 #undef END_FOLD_INIT
14274
14275 /* Determine if first argument is a multiple of second argument.  Return 0 if
14276    it is not, or we cannot easily determined it to be.
14277
14278    An example of the sort of thing we care about (at this point; this routine
14279    could surely be made more general, and expanded to do what the *_DIV_EXPR's
14280    fold cases do now) is discovering that
14281
14282      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14283
14284    is a multiple of
14285
14286      SAVE_EXPR (J * 8)
14287
14288    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14289
14290    This code also handles discovering that
14291
14292      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14293
14294    is a multiple of 8 so we don't have to worry about dealing with a
14295    possible remainder.
14296
14297    Note that we *look* inside a SAVE_EXPR only to determine how it was
14298    calculated; it is not safe for fold to do much of anything else with the
14299    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14300    at run time.  For example, the latter example above *cannot* be implemented
14301    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14302    evaluation time of the original SAVE_EXPR is not necessarily the same at
14303    the time the new expression is evaluated.  The only optimization of this
14304    sort that would be valid is changing
14305
14306      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14307
14308    divided by 8 to
14309
14310      SAVE_EXPR (I) * SAVE_EXPR (J)
14311
14312    (where the same SAVE_EXPR (J) is used in the original and the
14313    transformed version).  */
14314
14315 int
14316 multiple_of_p (tree type, const_tree top, const_tree bottom)
14317 {
14318   if (operand_equal_p (top, bottom, 0))
14319     return 1;
14320
14321   if (TREE_CODE (type) != INTEGER_TYPE)
14322     return 0;
14323
14324   switch (TREE_CODE (top))
14325     {
14326     case BIT_AND_EXPR:
14327       /* Bitwise and provides a power of two multiple.  If the mask is
14328          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
14329       if (!integer_pow2p (bottom))
14330         return 0;
14331       /* FALLTHRU */
14332
14333     case MULT_EXPR:
14334       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14335               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14336
14337     case PLUS_EXPR:
14338     case MINUS_EXPR:
14339       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14340               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14341
14342     case LSHIFT_EXPR:
14343       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14344         {
14345           tree op1, t1;
14346
14347           op1 = TREE_OPERAND (top, 1);
14348           /* const_binop may not detect overflow correctly,
14349              so check for it explicitly here.  */
14350           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14351               > TREE_INT_CST_LOW (op1)
14352               && TREE_INT_CST_HIGH (op1) == 0
14353               && 0 != (t1 = fold_convert (type,
14354                                           const_binop (LSHIFT_EXPR,
14355                                                        size_one_node,
14356                                                        op1)))
14357               && !TREE_OVERFLOW (t1))
14358             return multiple_of_p (type, t1, bottom);
14359         }
14360       return 0;
14361
14362     case NOP_EXPR:
14363       /* Can't handle conversions from non-integral or wider integral type.  */
14364       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14365           || (TYPE_PRECISION (type)
14366               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14367         return 0;
14368
14369       /* .. fall through ...  */
14370
14371     case SAVE_EXPR:
14372       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14373
14374     case COND_EXPR:
14375       return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
14376               && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
14377
14378     case INTEGER_CST:
14379       if (TREE_CODE (bottom) != INTEGER_CST
14380           || integer_zerop (bottom)
14381           || (TYPE_UNSIGNED (type)
14382               && (tree_int_cst_sgn (top) < 0
14383                   || tree_int_cst_sgn (bottom) < 0)))
14384         return 0;
14385       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14386                                              top, bottom, 0));
14387
14388     default:
14389       return 0;
14390     }
14391 }
14392
14393 /* Return true if CODE or TYPE is known to be non-negative. */
14394
14395 static bool
14396 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14397 {
14398   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14399       && truth_value_p (code))
14400     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14401        have a signed:1 type (where the value is -1 and 0).  */
14402     return true;
14403   return false;
14404 }
14405
14406 /* Return true if (CODE OP0) is known to be non-negative.  If the return
14407    value is based on the assumption that signed overflow is undefined,
14408    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14409    *STRICT_OVERFLOW_P.  */
14410
14411 bool
14412 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14413                                 bool *strict_overflow_p)
14414 {
14415   if (TYPE_UNSIGNED (type))
14416     return true;
14417
14418   switch (code)
14419     {
14420     case ABS_EXPR:
14421       /* We can't return 1 if flag_wrapv is set because
14422          ABS_EXPR<INT_MIN> = INT_MIN.  */
14423       if (!INTEGRAL_TYPE_P (type))
14424         return true;
14425       if (TYPE_OVERFLOW_UNDEFINED (type))
14426         {
14427           *strict_overflow_p = true;
14428           return true;
14429         }
14430       break;
14431
14432     case NON_LVALUE_EXPR:
14433     case FLOAT_EXPR:
14434     case FIX_TRUNC_EXPR:
14435       return tree_expr_nonnegative_warnv_p (op0,
14436                                             strict_overflow_p);
14437
14438     case NOP_EXPR:
14439       {
14440         tree inner_type = TREE_TYPE (op0);
14441         tree outer_type = type;
14442
14443         if (TREE_CODE (outer_type) == REAL_TYPE)
14444           {
14445             if (TREE_CODE (inner_type) == REAL_TYPE)
14446               return tree_expr_nonnegative_warnv_p (op0,
14447                                                     strict_overflow_p);
14448             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14449               {
14450                 if (TYPE_UNSIGNED (inner_type))
14451                   return true;
14452                 return tree_expr_nonnegative_warnv_p (op0,
14453                                                       strict_overflow_p);
14454               }
14455           }
14456         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14457           {
14458             if (TREE_CODE (inner_type) == REAL_TYPE)
14459               return tree_expr_nonnegative_warnv_p (op0,
14460                                                     strict_overflow_p);
14461             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14462               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14463                       && TYPE_UNSIGNED (inner_type);
14464           }
14465       }
14466       break;
14467
14468     default:
14469       return tree_simple_nonnegative_warnv_p (code, type);
14470     }
14471
14472   /* We don't know sign of `t', so be conservative and return false.  */
14473   return false;
14474 }
14475
14476 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
14477    value is based on the assumption that signed overflow is undefined,
14478    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14479    *STRICT_OVERFLOW_P.  */
14480
14481 bool
14482 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14483                                       tree op1, bool *strict_overflow_p)
14484 {
14485   if (TYPE_UNSIGNED (type))
14486     return true;
14487
14488   switch (code)
14489     {
14490     case POINTER_PLUS_EXPR:
14491     case PLUS_EXPR:
14492       if (FLOAT_TYPE_P (type))
14493         return (tree_expr_nonnegative_warnv_p (op0,
14494                                                strict_overflow_p)
14495                 && tree_expr_nonnegative_warnv_p (op1,
14496                                                   strict_overflow_p));
14497
14498       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14499          both unsigned and at least 2 bits shorter than the result.  */
14500       if (TREE_CODE (type) == INTEGER_TYPE
14501           && TREE_CODE (op0) == NOP_EXPR
14502           && TREE_CODE (op1) == NOP_EXPR)
14503         {
14504           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14505           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14506           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14507               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14508             {
14509               unsigned int prec = MAX (TYPE_PRECISION (inner1),
14510                                        TYPE_PRECISION (inner2)) + 1;
14511               return prec < TYPE_PRECISION (type);
14512             }
14513         }
14514       break;
14515
14516     case MULT_EXPR:
14517       if (FLOAT_TYPE_P (type))
14518         {
14519           /* x * x for floating point x is always non-negative.  */
14520           if (operand_equal_p (op0, op1, 0))
14521             return true;
14522           return (tree_expr_nonnegative_warnv_p (op0,
14523                                                  strict_overflow_p)
14524                   && tree_expr_nonnegative_warnv_p (op1,
14525                                                     strict_overflow_p));
14526         }
14527
14528       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14529          both unsigned and their total bits is shorter than the result.  */
14530       if (TREE_CODE (type) == INTEGER_TYPE
14531           && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14532           && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14533         {
14534           tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14535             ? TREE_TYPE (TREE_OPERAND (op0, 0))
14536             : TREE_TYPE (op0);
14537           tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14538             ? TREE_TYPE (TREE_OPERAND (op1, 0))
14539             : TREE_TYPE (op1);
14540
14541           bool unsigned0 = TYPE_UNSIGNED (inner0);
14542           bool unsigned1 = TYPE_UNSIGNED (inner1);
14543
14544           if (TREE_CODE (op0) == INTEGER_CST)
14545             unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14546
14547           if (TREE_CODE (op1) == INTEGER_CST)
14548             unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14549
14550           if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14551               && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14552             {
14553               unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14554                 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14555                 : TYPE_PRECISION (inner0);
14556
14557               unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14558                 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14559                 : TYPE_PRECISION (inner1);
14560
14561               return precision0 + precision1 < TYPE_PRECISION (type);
14562             }
14563         }
14564       return false;
14565
14566     case BIT_AND_EXPR:
14567     case MAX_EXPR:
14568       return (tree_expr_nonnegative_warnv_p (op0,
14569                                              strict_overflow_p)
14570               || tree_expr_nonnegative_warnv_p (op1,
14571                                                 strict_overflow_p));
14572
14573     case BIT_IOR_EXPR:
14574     case BIT_XOR_EXPR:
14575     case MIN_EXPR:
14576     case RDIV_EXPR:
14577     case TRUNC_DIV_EXPR:
14578     case CEIL_DIV_EXPR:
14579     case FLOOR_DIV_EXPR:
14580     case ROUND_DIV_EXPR:
14581       return (tree_expr_nonnegative_warnv_p (op0,
14582                                              strict_overflow_p)
14583               && tree_expr_nonnegative_warnv_p (op1,
14584                                                 strict_overflow_p));
14585
14586     case TRUNC_MOD_EXPR:
14587     case CEIL_MOD_EXPR:
14588     case FLOOR_MOD_EXPR:
14589     case ROUND_MOD_EXPR:
14590       return tree_expr_nonnegative_warnv_p (op0,
14591                                             strict_overflow_p);
14592     default:
14593       return tree_simple_nonnegative_warnv_p (code, type);
14594     }
14595
14596   /* We don't know sign of `t', so be conservative and return false.  */
14597   return false;
14598 }
14599
14600 /* Return true if T is known to be non-negative.  If the return
14601    value is based on the assumption that signed overflow is undefined,
14602    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14603    *STRICT_OVERFLOW_P.  */
14604
14605 bool
14606 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14607 {
14608   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14609     return true;
14610
14611   switch (TREE_CODE (t))
14612     {
14613     case INTEGER_CST:
14614       return tree_int_cst_sgn (t) >= 0;
14615
14616     case REAL_CST:
14617       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14618
14619     case FIXED_CST:
14620       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14621
14622     case COND_EXPR:
14623       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14624                                              strict_overflow_p)
14625               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14626                                                 strict_overflow_p));
14627     default:
14628       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14629                                                    TREE_TYPE (t));
14630     }
14631   /* We don't know sign of `t', so be conservative and return false.  */
14632   return false;
14633 }
14634
14635 /* Return true if T is known to be non-negative.  If the return
14636    value is based on the assumption that signed overflow is undefined,
14637    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14638    *STRICT_OVERFLOW_P.  */
14639
14640 bool
14641 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14642                                tree arg0, tree arg1, bool *strict_overflow_p)
14643 {
14644   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14645     switch (DECL_FUNCTION_CODE (fndecl))
14646       {
14647         CASE_FLT_FN (BUILT_IN_ACOS):
14648         CASE_FLT_FN (BUILT_IN_ACOSH):
14649         CASE_FLT_FN (BUILT_IN_CABS):
14650         CASE_FLT_FN (BUILT_IN_COSH):
14651         CASE_FLT_FN (BUILT_IN_ERFC):
14652         CASE_FLT_FN (BUILT_IN_EXP):
14653         CASE_FLT_FN (BUILT_IN_EXP10):
14654         CASE_FLT_FN (BUILT_IN_EXP2):
14655         CASE_FLT_FN (BUILT_IN_FABS):
14656         CASE_FLT_FN (BUILT_IN_FDIM):
14657         CASE_FLT_FN (BUILT_IN_HYPOT):
14658         CASE_FLT_FN (BUILT_IN_POW10):
14659         CASE_INT_FN (BUILT_IN_FFS):
14660         CASE_INT_FN (BUILT_IN_PARITY):
14661         CASE_INT_FN (BUILT_IN_POPCOUNT):
14662       case BUILT_IN_BSWAP32:
14663       case BUILT_IN_BSWAP64:
14664         /* Always true.  */
14665         return true;
14666
14667         CASE_FLT_FN (BUILT_IN_SQRT):
14668         /* sqrt(-0.0) is -0.0.  */
14669         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14670           return true;
14671         return tree_expr_nonnegative_warnv_p (arg0,
14672                                               strict_overflow_p);
14673
14674         CASE_FLT_FN (BUILT_IN_ASINH):
14675         CASE_FLT_FN (BUILT_IN_ATAN):
14676         CASE_FLT_FN (BUILT_IN_ATANH):
14677         CASE_FLT_FN (BUILT_IN_CBRT):
14678         CASE_FLT_FN (BUILT_IN_CEIL):
14679         CASE_FLT_FN (BUILT_IN_ERF):
14680         CASE_FLT_FN (BUILT_IN_EXPM1):
14681         CASE_FLT_FN (BUILT_IN_FLOOR):
14682         CASE_FLT_FN (BUILT_IN_FMOD):
14683         CASE_FLT_FN (BUILT_IN_FREXP):
14684         CASE_FLT_FN (BUILT_IN_LCEIL):
14685         CASE_FLT_FN (BUILT_IN_LDEXP):
14686         CASE_FLT_FN (BUILT_IN_LFLOOR):
14687         CASE_FLT_FN (BUILT_IN_LLCEIL):
14688         CASE_FLT_FN (BUILT_IN_LLFLOOR):
14689         CASE_FLT_FN (BUILT_IN_LLRINT):
14690         CASE_FLT_FN (BUILT_IN_LLROUND):
14691         CASE_FLT_FN (BUILT_IN_LRINT):
14692         CASE_FLT_FN (BUILT_IN_LROUND):
14693         CASE_FLT_FN (BUILT_IN_MODF):
14694         CASE_FLT_FN (BUILT_IN_NEARBYINT):
14695         CASE_FLT_FN (BUILT_IN_RINT):
14696         CASE_FLT_FN (BUILT_IN_ROUND):
14697         CASE_FLT_FN (BUILT_IN_SCALB):
14698         CASE_FLT_FN (BUILT_IN_SCALBLN):
14699         CASE_FLT_FN (BUILT_IN_SCALBN):
14700         CASE_FLT_FN (BUILT_IN_SIGNBIT):
14701         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14702         CASE_FLT_FN (BUILT_IN_SINH):
14703         CASE_FLT_FN (BUILT_IN_TANH):
14704         CASE_FLT_FN (BUILT_IN_TRUNC):
14705         /* True if the 1st argument is nonnegative.  */
14706         return tree_expr_nonnegative_warnv_p (arg0,
14707                                               strict_overflow_p);
14708
14709         CASE_FLT_FN (BUILT_IN_FMAX):
14710         /* True if the 1st OR 2nd arguments are nonnegative.  */
14711         return (tree_expr_nonnegative_warnv_p (arg0,
14712                                                strict_overflow_p)
14713                 || (tree_expr_nonnegative_warnv_p (arg1,
14714                                                    strict_overflow_p)));
14715
14716         CASE_FLT_FN (BUILT_IN_FMIN):
14717         /* True if the 1st AND 2nd arguments are nonnegative.  */
14718         return (tree_expr_nonnegative_warnv_p (arg0,
14719                                                strict_overflow_p)
14720                 && (tree_expr_nonnegative_warnv_p (arg1,
14721                                                    strict_overflow_p)));
14722
14723         CASE_FLT_FN (BUILT_IN_COPYSIGN):
14724         /* True if the 2nd argument is nonnegative.  */
14725         return tree_expr_nonnegative_warnv_p (arg1,
14726                                               strict_overflow_p);
14727
14728         CASE_FLT_FN (BUILT_IN_POWI):
14729         /* True if the 1st argument is nonnegative or the second
14730            argument is an even integer.  */
14731         if (TREE_CODE (arg1) == INTEGER_CST
14732             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14733           return true;
14734         return tree_expr_nonnegative_warnv_p (arg0,
14735                                               strict_overflow_p);
14736
14737         CASE_FLT_FN (BUILT_IN_POW):
14738         /* True if the 1st argument is nonnegative or the second
14739            argument is an even integer valued real.  */
14740         if (TREE_CODE (arg1) == REAL_CST)
14741           {
14742             REAL_VALUE_TYPE c;
14743             HOST_WIDE_INT n;
14744
14745             c = TREE_REAL_CST (arg1);
14746             n = real_to_integer (&c);
14747             if ((n & 1) == 0)
14748               {
14749                 REAL_VALUE_TYPE cint;
14750                 real_from_integer (&cint, VOIDmode, n,
14751                                    n < 0 ? -1 : 0, 0);
14752                 if (real_identical (&c, &cint))
14753                   return true;
14754               }
14755           }
14756         return tree_expr_nonnegative_warnv_p (arg0,
14757                                               strict_overflow_p);
14758
14759       default:
14760         break;
14761       }
14762   return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14763                                           type);
14764 }
14765
14766 /* Return true if T is known to be non-negative.  If the return
14767    value is based on the assumption that signed overflow is undefined,
14768    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14769    *STRICT_OVERFLOW_P.  */
14770
14771 bool
14772 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14773 {
14774   enum tree_code code = TREE_CODE (t);
14775   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14776     return true;
14777
14778   switch (code)
14779     {
14780     case TARGET_EXPR:
14781       {
14782         tree temp = TARGET_EXPR_SLOT (t);
14783         t = TARGET_EXPR_INITIAL (t);
14784
14785         /* If the initializer is non-void, then it's a normal expression
14786            that will be assigned to the slot.  */
14787         if (!VOID_TYPE_P (t))
14788           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14789
14790         /* Otherwise, the initializer sets the slot in some way.  One common
14791            way is an assignment statement at the end of the initializer.  */
14792         while (1)
14793           {
14794             if (TREE_CODE (t) == BIND_EXPR)
14795               t = expr_last (BIND_EXPR_BODY (t));
14796             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14797                      || TREE_CODE (t) == TRY_CATCH_EXPR)
14798               t = expr_last (TREE_OPERAND (t, 0));
14799             else if (TREE_CODE (t) == STATEMENT_LIST)
14800               t = expr_last (t);
14801             else
14802               break;
14803           }
14804         if (TREE_CODE (t) == MODIFY_EXPR
14805             && TREE_OPERAND (t, 0) == temp)
14806           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14807                                                 strict_overflow_p);
14808
14809         return false;
14810       }
14811
14812     case CALL_EXPR:
14813       {
14814         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
14815         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
14816
14817         return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14818                                               get_callee_fndecl (t),
14819                                               arg0,
14820                                               arg1,
14821                                               strict_overflow_p);
14822       }
14823     case COMPOUND_EXPR:
14824     case MODIFY_EXPR:
14825       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14826                                             strict_overflow_p);
14827     case BIND_EXPR:
14828       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14829                                             strict_overflow_p);
14830     case SAVE_EXPR:
14831       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14832                                             strict_overflow_p);
14833
14834     default:
14835       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14836                                                    TREE_TYPE (t));
14837     }
14838
14839   /* We don't know sign of `t', so be conservative and return false.  */
14840   return false;
14841 }
14842
14843 /* Return true if T is known to be non-negative.  If the return
14844    value is based on the assumption that signed overflow is undefined,
14845    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14846    *STRICT_OVERFLOW_P.  */
14847
14848 bool
14849 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14850 {
14851   enum tree_code code;
14852   if (t == error_mark_node)
14853     return false;
14854
14855   code = TREE_CODE (t);
14856   switch (TREE_CODE_CLASS (code))
14857     {
14858     case tcc_binary:
14859     case tcc_comparison:
14860       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14861                                               TREE_TYPE (t),
14862                                               TREE_OPERAND (t, 0),
14863                                               TREE_OPERAND (t, 1),
14864                                               strict_overflow_p);
14865
14866     case tcc_unary:
14867       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14868                                              TREE_TYPE (t),
14869                                              TREE_OPERAND (t, 0),
14870                                              strict_overflow_p);
14871
14872     case tcc_constant:
14873     case tcc_declaration:
14874     case tcc_reference:
14875       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14876
14877     default:
14878       break;
14879     }
14880
14881   switch (code)
14882     {
14883     case TRUTH_AND_EXPR:
14884     case TRUTH_OR_EXPR:
14885     case TRUTH_XOR_EXPR:
14886       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14887                                               TREE_TYPE (t),
14888                                               TREE_OPERAND (t, 0),
14889                                               TREE_OPERAND (t, 1),
14890                                               strict_overflow_p);
14891     case TRUTH_NOT_EXPR:
14892       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14893                                              TREE_TYPE (t),
14894                                              TREE_OPERAND (t, 0),
14895                                              strict_overflow_p);
14896
14897     case COND_EXPR:
14898     case CONSTRUCTOR:
14899     case OBJ_TYPE_REF:
14900     case ASSERT_EXPR:
14901     case ADDR_EXPR:
14902     case WITH_SIZE_EXPR:
14903     case SSA_NAME:
14904       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14905
14906     default:
14907       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14908     }
14909 }
14910
14911 /* Return true if `t' is known to be non-negative.  Handle warnings
14912    about undefined signed overflow.  */
14913
14914 bool
14915 tree_expr_nonnegative_p (tree t)
14916 {
14917   bool ret, strict_overflow_p;
14918
14919   strict_overflow_p = false;
14920   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14921   if (strict_overflow_p)
14922     fold_overflow_warning (("assuming signed overflow does not occur when "
14923                             "determining that expression is always "
14924                             "non-negative"),
14925                            WARN_STRICT_OVERFLOW_MISC);
14926   return ret;
14927 }
14928
14929
14930 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14931    For floating point we further ensure that T is not denormal.
14932    Similar logic is present in nonzero_address in rtlanal.h.
14933
14934    If the return value is based on the assumption that signed overflow
14935    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14936    change *STRICT_OVERFLOW_P.  */
14937
14938 bool
14939 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14940                                  bool *strict_overflow_p)
14941 {
14942   switch (code)
14943     {
14944     case ABS_EXPR:
14945       return tree_expr_nonzero_warnv_p (op0,
14946                                         strict_overflow_p);
14947
14948     case NOP_EXPR:
14949       {
14950         tree inner_type = TREE_TYPE (op0);
14951         tree outer_type = type;
14952
14953         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14954                 && tree_expr_nonzero_warnv_p (op0,
14955                                               strict_overflow_p));
14956       }
14957       break;
14958
14959     case NON_LVALUE_EXPR:
14960       return tree_expr_nonzero_warnv_p (op0,
14961                                         strict_overflow_p);
14962
14963     default:
14964       break;
14965   }
14966
14967   return false;
14968 }
14969
14970 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14971    For floating point we further ensure that T is not denormal.
14972    Similar logic is present in nonzero_address in rtlanal.h.
14973
14974    If the return value is based on the assumption that signed overflow
14975    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14976    change *STRICT_OVERFLOW_P.  */
14977
14978 bool
14979 tree_binary_nonzero_warnv_p (enum tree_code code,
14980                              tree type,
14981                              tree op0,
14982                              tree op1, bool *strict_overflow_p)
14983 {
14984   bool sub_strict_overflow_p;
14985   switch (code)
14986     {
14987     case POINTER_PLUS_EXPR:
14988     case PLUS_EXPR:
14989       if (TYPE_OVERFLOW_UNDEFINED (type))
14990         {
14991           /* With the presence of negative values it is hard
14992              to say something.  */
14993           sub_strict_overflow_p = false;
14994           if (!tree_expr_nonnegative_warnv_p (op0,
14995                                               &sub_strict_overflow_p)
14996               || !tree_expr_nonnegative_warnv_p (op1,
14997                                                  &sub_strict_overflow_p))
14998             return false;
14999           /* One of operands must be positive and the other non-negative.  */
15000           /* We don't set *STRICT_OVERFLOW_P here: even if this value
15001              overflows, on a twos-complement machine the sum of two
15002              nonnegative numbers can never be zero.  */
15003           return (tree_expr_nonzero_warnv_p (op0,
15004                                              strict_overflow_p)
15005                   || tree_expr_nonzero_warnv_p (op1,
15006                                                 strict_overflow_p));
15007         }
15008       break;
15009
15010     case MULT_EXPR:
15011       if (TYPE_OVERFLOW_UNDEFINED (type))
15012         {
15013           if (tree_expr_nonzero_warnv_p (op0,
15014                                          strict_overflow_p)
15015               && tree_expr_nonzero_warnv_p (op1,
15016                                             strict_overflow_p))
15017             {
15018               *strict_overflow_p = true;
15019               return true;
15020             }
15021         }
15022       break;
15023
15024     case MIN_EXPR:
15025       sub_strict_overflow_p = false;
15026       if (tree_expr_nonzero_warnv_p (op0,
15027                                      &sub_strict_overflow_p)
15028           && tree_expr_nonzero_warnv_p (op1,
15029                                         &sub_strict_overflow_p))
15030         {
15031           if (sub_strict_overflow_p)
15032             *strict_overflow_p = true;
15033         }
15034       break;
15035
15036     case MAX_EXPR:
15037       sub_strict_overflow_p = false;
15038       if (tree_expr_nonzero_warnv_p (op0,
15039                                      &sub_strict_overflow_p))
15040         {
15041           if (sub_strict_overflow_p)
15042             *strict_overflow_p = true;
15043
15044           /* When both operands are nonzero, then MAX must be too.  */
15045           if (tree_expr_nonzero_warnv_p (op1,
15046                                          strict_overflow_p))
15047             return true;
15048
15049           /* MAX where operand 0 is positive is positive.  */
15050           return tree_expr_nonnegative_warnv_p (op0,
15051                                                strict_overflow_p);
15052         }
15053       /* MAX where operand 1 is positive is positive.  */
15054       else if (tree_expr_nonzero_warnv_p (op1,
15055                                           &sub_strict_overflow_p)
15056                && tree_expr_nonnegative_warnv_p (op1,
15057                                                  &sub_strict_overflow_p))
15058         {
15059           if (sub_strict_overflow_p)
15060             *strict_overflow_p = true;
15061           return true;
15062         }
15063       break;
15064
15065     case BIT_IOR_EXPR:
15066       return (tree_expr_nonzero_warnv_p (op1,
15067                                          strict_overflow_p)
15068               || tree_expr_nonzero_warnv_p (op0,
15069                                             strict_overflow_p));
15070
15071     default:
15072       break;
15073   }
15074
15075   return false;
15076 }
15077
15078 /* Return true when T is an address and is known to be nonzero.
15079    For floating point we further ensure that T is not denormal.
15080    Similar logic is present in nonzero_address in rtlanal.h.
15081
15082    If the return value is based on the assumption that signed overflow
15083    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15084    change *STRICT_OVERFLOW_P.  */
15085
15086 bool
15087 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15088 {
15089   bool sub_strict_overflow_p;
15090   switch (TREE_CODE (t))
15091     {
15092     case INTEGER_CST:
15093       return !integer_zerop (t);
15094
15095     case ADDR_EXPR:
15096       {
15097         tree base = TREE_OPERAND (t, 0);
15098         if (!DECL_P (base))
15099           base = get_base_address (base);
15100
15101         if (!base)
15102           return false;
15103
15104         /* Weak declarations may link to NULL.  Other things may also be NULL
15105            so protect with -fdelete-null-pointer-checks; but not variables
15106            allocated on the stack.  */
15107         if (DECL_P (base)
15108             && (flag_delete_null_pointer_checks
15109                 || (DECL_CONTEXT (base)
15110                     && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
15111                     && auto_var_in_fn_p (base, DECL_CONTEXT (base)))))
15112           return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
15113
15114         /* Constants are never weak.  */
15115         if (CONSTANT_CLASS_P (base))
15116           return true;
15117
15118         return false;
15119       }
15120
15121     case COND_EXPR:
15122       sub_strict_overflow_p = false;
15123       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15124                                      &sub_strict_overflow_p)
15125           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15126                                         &sub_strict_overflow_p))
15127         {
15128           if (sub_strict_overflow_p)
15129             *strict_overflow_p = true;
15130           return true;
15131         }
15132       break;
15133
15134     default:
15135       break;
15136     }
15137   return false;
15138 }
15139
15140 /* Return true when T is an address and is known to be nonzero.
15141    For floating point we further ensure that T is not denormal.
15142    Similar logic is present in nonzero_address in rtlanal.h.
15143
15144    If the return value is based on the assumption that signed overflow
15145    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15146    change *STRICT_OVERFLOW_P.  */
15147
15148 bool
15149 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15150 {
15151   tree type = TREE_TYPE (t);
15152   enum tree_code code;
15153
15154   /* Doing something useful for floating point would need more work.  */
15155   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15156     return false;
15157
15158   code = TREE_CODE (t);
15159   switch (TREE_CODE_CLASS (code))
15160     {
15161     case tcc_unary:
15162       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15163                                               strict_overflow_p);
15164     case tcc_binary:
15165     case tcc_comparison:
15166       return tree_binary_nonzero_warnv_p (code, type,
15167                                                TREE_OPERAND (t, 0),
15168                                                TREE_OPERAND (t, 1),
15169                                                strict_overflow_p);
15170     case tcc_constant:
15171     case tcc_declaration:
15172     case tcc_reference:
15173       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15174
15175     default:
15176       break;
15177     }
15178
15179   switch (code)
15180     {
15181     case TRUTH_NOT_EXPR:
15182       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15183                                               strict_overflow_p);
15184
15185     case TRUTH_AND_EXPR:
15186     case TRUTH_OR_EXPR:
15187     case TRUTH_XOR_EXPR:
15188       return tree_binary_nonzero_warnv_p (code, type,
15189                                                TREE_OPERAND (t, 0),
15190                                                TREE_OPERAND (t, 1),
15191                                                strict_overflow_p);
15192
15193     case COND_EXPR:
15194     case CONSTRUCTOR:
15195     case OBJ_TYPE_REF:
15196     case ASSERT_EXPR:
15197     case ADDR_EXPR:
15198     case WITH_SIZE_EXPR:
15199     case SSA_NAME:
15200       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15201
15202     case COMPOUND_EXPR:
15203     case MODIFY_EXPR:
15204     case BIND_EXPR:
15205       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15206                                         strict_overflow_p);
15207
15208     case SAVE_EXPR:
15209       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15210                                         strict_overflow_p);
15211
15212     case CALL_EXPR:
15213       return alloca_call_p (t);
15214
15215     default:
15216       break;
15217     }
15218   return false;
15219 }
15220
15221 /* Return true when T is an address and is known to be nonzero.
15222    Handle warnings about undefined signed overflow.  */
15223
15224 bool
15225 tree_expr_nonzero_p (tree t)
15226 {
15227   bool ret, strict_overflow_p;
15228
15229   strict_overflow_p = false;
15230   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15231   if (strict_overflow_p)
15232     fold_overflow_warning (("assuming signed overflow does not occur when "
15233                             "determining that expression is always "
15234                             "non-zero"),
15235                            WARN_STRICT_OVERFLOW_MISC);
15236   return ret;
15237 }
15238
15239 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15240    attempt to fold the expression to a constant without modifying TYPE,
15241    OP0 or OP1.
15242
15243    If the expression could be simplified to a constant, then return
15244    the constant.  If the expression would not be simplified to a
15245    constant, then return NULL_TREE.  */
15246
15247 tree
15248 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15249 {
15250   tree tem = fold_binary (code, type, op0, op1);
15251   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15252 }
15253
15254 /* Given the components of a unary expression CODE, TYPE and OP0,
15255    attempt to fold the expression to a constant without modifying
15256    TYPE or OP0.
15257
15258    If the expression could be simplified to a constant, then return
15259    the constant.  If the expression would not be simplified to a
15260    constant, then return NULL_TREE.  */
15261
15262 tree
15263 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15264 {
15265   tree tem = fold_unary (code, type, op0);
15266   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15267 }
15268
15269 /* If EXP represents referencing an element in a constant string
15270    (either via pointer arithmetic or array indexing), return the
15271    tree representing the value accessed, otherwise return NULL.  */
15272
15273 tree
15274 fold_read_from_constant_string (tree exp)
15275 {
15276   if ((TREE_CODE (exp) == INDIRECT_REF
15277        || TREE_CODE (exp) == ARRAY_REF)
15278       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15279     {
15280       tree exp1 = TREE_OPERAND (exp, 0);
15281       tree index;
15282       tree string;
15283       location_t loc = EXPR_LOCATION (exp);
15284
15285       if (TREE_CODE (exp) == INDIRECT_REF)
15286         string = string_constant (exp1, &index);
15287       else
15288         {
15289           tree low_bound = array_ref_low_bound (exp);
15290           index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15291
15292           /* Optimize the special-case of a zero lower bound.
15293
15294              We convert the low_bound to sizetype to avoid some problems
15295              with constant folding.  (E.g. suppose the lower bound is 1,
15296              and its mode is QI.  Without the conversion,l (ARRAY
15297              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15298              +INDEX), which becomes (ARRAY+255+INDEX).  Oops!)  */
15299           if (! integer_zerop (low_bound))
15300             index = size_diffop_loc (loc, index,
15301                                  fold_convert_loc (loc, sizetype, low_bound));
15302
15303           string = exp1;
15304         }
15305
15306       if (string
15307           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15308           && TREE_CODE (string) == STRING_CST
15309           && TREE_CODE (index) == INTEGER_CST
15310           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15311           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15312               == MODE_INT)
15313           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15314         return build_int_cst_type (TREE_TYPE (exp),
15315                                    (TREE_STRING_POINTER (string)
15316                                     [TREE_INT_CST_LOW (index)]));
15317     }
15318   return NULL;
15319 }
15320
15321 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15322    an integer constant, real, or fixed-point constant.
15323
15324    TYPE is the type of the result.  */
15325
15326 static tree
15327 fold_negate_const (tree arg0, tree type)
15328 {
15329   tree t = NULL_TREE;
15330
15331   switch (TREE_CODE (arg0))
15332     {
15333     case INTEGER_CST:
15334       {
15335         double_int val = tree_to_double_int (arg0);
15336         int overflow = neg_double (val.low, val.high, &val.low, &val.high);
15337
15338         t = force_fit_type_double (type, val, 1,
15339                                    (overflow | TREE_OVERFLOW (arg0))
15340                                    && !TYPE_UNSIGNED (type));
15341         break;
15342       }
15343
15344     case REAL_CST:
15345       t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15346       break;
15347
15348     case FIXED_CST:
15349       {
15350         FIXED_VALUE_TYPE f;
15351         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15352                                             &(TREE_FIXED_CST (arg0)), NULL,
15353                                             TYPE_SATURATING (type));
15354         t = build_fixed (type, f);
15355         /* Propagate overflow flags.  */
15356         if (overflow_p | TREE_OVERFLOW (arg0))
15357           TREE_OVERFLOW (t) = 1;
15358         break;
15359       }
15360
15361     default:
15362       gcc_unreachable ();
15363     }
15364
15365   return t;
15366 }
15367
15368 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15369    an integer constant or real constant.
15370
15371    TYPE is the type of the result.  */
15372
15373 tree
15374 fold_abs_const (tree arg0, tree type)
15375 {
15376   tree t = NULL_TREE;
15377
15378   switch (TREE_CODE (arg0))
15379     {
15380     case INTEGER_CST:
15381       {
15382         double_int val = tree_to_double_int (arg0);
15383
15384         /* If the value is unsigned or non-negative, then the absolute value
15385            is the same as the ordinary value.  */
15386         if (TYPE_UNSIGNED (type)
15387             || !double_int_negative_p (val))
15388           t = arg0;
15389
15390         /* If the value is negative, then the absolute value is
15391            its negation.  */
15392         else
15393           {
15394             int overflow;
15395
15396             overflow = neg_double (val.low, val.high, &val.low, &val.high);
15397             t = force_fit_type_double (type, val, -1,
15398                                        overflow | TREE_OVERFLOW (arg0));
15399           }
15400       }
15401       break;
15402
15403     case REAL_CST:
15404       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15405         t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15406       else
15407         t =  arg0;
15408       break;
15409
15410     default:
15411       gcc_unreachable ();
15412     }
15413
15414   return t;
15415 }
15416
15417 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15418    constant.  TYPE is the type of the result.  */
15419
15420 static tree
15421 fold_not_const (const_tree arg0, tree type)
15422 {
15423   double_int val;  
15424
15425   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15426
15427   val = double_int_not (tree_to_double_int (arg0));
15428   return force_fit_type_double (type, val, 0, TREE_OVERFLOW (arg0));
15429 }
15430
15431 /* Given CODE, a relational operator, the target type, TYPE and two
15432    constant operands OP0 and OP1, return the result of the
15433    relational operation.  If the result is not a compile time
15434    constant, then return NULL_TREE.  */
15435
15436 static tree
15437 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15438 {
15439   int result, invert;
15440
15441   /* From here on, the only cases we handle are when the result is
15442      known to be a constant.  */
15443
15444   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15445     {
15446       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15447       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15448
15449       /* Handle the cases where either operand is a NaN.  */
15450       if (real_isnan (c0) || real_isnan (c1))
15451         {
15452           switch (code)
15453             {
15454             case EQ_EXPR:
15455             case ORDERED_EXPR:
15456               result = 0;
15457               break;
15458
15459             case NE_EXPR:
15460             case UNORDERED_EXPR:
15461             case UNLT_EXPR:
15462             case UNLE_EXPR:
15463             case UNGT_EXPR:
15464             case UNGE_EXPR:
15465             case UNEQ_EXPR:
15466               result = 1;
15467               break;
15468
15469             case LT_EXPR:
15470             case LE_EXPR:
15471             case GT_EXPR:
15472             case GE_EXPR:
15473             case LTGT_EXPR:
15474               if (flag_trapping_math)
15475                 return NULL_TREE;
15476               result = 0;
15477               break;
15478
15479             default:
15480               gcc_unreachable ();
15481             }
15482
15483           return constant_boolean_node (result, type);
15484         }
15485
15486       return constant_boolean_node (real_compare (code, c0, c1), type);
15487     }
15488
15489   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15490     {
15491       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15492       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15493       return constant_boolean_node (fixed_compare (code, c0, c1), type);
15494     }
15495
15496   /* Handle equality/inequality of complex constants.  */
15497   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15498     {
15499       tree rcond = fold_relational_const (code, type,
15500                                           TREE_REALPART (op0),
15501                                           TREE_REALPART (op1));
15502       tree icond = fold_relational_const (code, type,
15503                                           TREE_IMAGPART (op0),
15504                                           TREE_IMAGPART (op1));
15505       if (code == EQ_EXPR)
15506         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15507       else if (code == NE_EXPR)
15508         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15509       else
15510         return NULL_TREE;
15511     }
15512
15513   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15514
15515      To compute GT, swap the arguments and do LT.
15516      To compute GE, do LT and invert the result.
15517      To compute LE, swap the arguments, do LT and invert the result.
15518      To compute NE, do EQ and invert the result.
15519
15520      Therefore, the code below must handle only EQ and LT.  */
15521
15522   if (code == LE_EXPR || code == GT_EXPR)
15523     {
15524       tree tem = op0;
15525       op0 = op1;
15526       op1 = tem;
15527       code = swap_tree_comparison (code);
15528     }
15529
15530   /* Note that it is safe to invert for real values here because we
15531      have already handled the one case that it matters.  */
15532
15533   invert = 0;
15534   if (code == NE_EXPR || code == GE_EXPR)
15535     {
15536       invert = 1;
15537       code = invert_tree_comparison (code, false);
15538     }
15539
15540   /* Compute a result for LT or EQ if args permit;
15541      Otherwise return T.  */
15542   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15543     {
15544       if (code == EQ_EXPR)
15545         result = tree_int_cst_equal (op0, op1);
15546       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15547         result = INT_CST_LT_UNSIGNED (op0, op1);
15548       else
15549         result = INT_CST_LT (op0, op1);
15550     }
15551   else
15552     return NULL_TREE;
15553
15554   if (invert)
15555     result ^= 1;
15556   return constant_boolean_node (result, type);
15557 }
15558
15559 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15560    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
15561    itself.  */
15562
15563 tree
15564 fold_build_cleanup_point_expr (tree type, tree expr)
15565 {
15566   /* If the expression does not have side effects then we don't have to wrap
15567      it with a cleanup point expression.  */
15568   if (!TREE_SIDE_EFFECTS (expr))
15569     return expr;
15570
15571   /* If the expression is a return, check to see if the expression inside the
15572      return has no side effects or the right hand side of the modify expression
15573      inside the return. If either don't have side effects set we don't need to
15574      wrap the expression in a cleanup point expression.  Note we don't check the
15575      left hand side of the modify because it should always be a return decl.  */
15576   if (TREE_CODE (expr) == RETURN_EXPR)
15577     {
15578       tree op = TREE_OPERAND (expr, 0);
15579       if (!op || !TREE_SIDE_EFFECTS (op))
15580         return expr;
15581       op = TREE_OPERAND (op, 1);
15582       if (!TREE_SIDE_EFFECTS (op))
15583         return expr;
15584     }
15585
15586   return build1 (CLEANUP_POINT_EXPR, type, expr);
15587 }
15588
15589 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15590    of an indirection through OP0, or NULL_TREE if no simplification is
15591    possible.  */
15592
15593 tree
15594 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15595 {
15596   tree sub = op0;
15597   tree subtype;
15598
15599   STRIP_NOPS (sub);
15600   subtype = TREE_TYPE (sub);
15601   if (!POINTER_TYPE_P (subtype))
15602     return NULL_TREE;
15603
15604   if (TREE_CODE (sub) == ADDR_EXPR)
15605     {
15606       tree op = TREE_OPERAND (sub, 0);
15607       tree optype = TREE_TYPE (op);
15608       /* *&CONST_DECL -> to the value of the const decl.  */
15609       if (TREE_CODE (op) == CONST_DECL)
15610         return DECL_INITIAL (op);
15611       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
15612       if (type == optype)
15613         {
15614           tree fop = fold_read_from_constant_string (op);
15615           if (fop)
15616             return fop;
15617           else
15618             return op;
15619         }
15620       /* *(foo *)&fooarray => fooarray[0] */
15621       else if (TREE_CODE (optype) == ARRAY_TYPE
15622                && type == TREE_TYPE (optype))
15623         {
15624           tree type_domain = TYPE_DOMAIN (optype);
15625           tree min_val = size_zero_node;
15626           if (type_domain && TYPE_MIN_VALUE (type_domain))
15627             min_val = TYPE_MIN_VALUE (type_domain);
15628           op0 = build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15629           SET_EXPR_LOCATION (op0, loc);
15630           return op0;
15631         }
15632       /* *(foo *)&complexfoo => __real__ complexfoo */
15633       else if (TREE_CODE (optype) == COMPLEX_TYPE
15634                && type == TREE_TYPE (optype))
15635         return fold_build1_loc (loc, REALPART_EXPR, type, op);
15636       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15637       else if (TREE_CODE (optype) == VECTOR_TYPE
15638                && type == TREE_TYPE (optype))
15639         {
15640           tree part_width = TYPE_SIZE (type);
15641           tree index = bitsize_int (0);
15642           return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
15643         }
15644     }
15645
15646   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15647       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15648     {
15649       tree op00 = TREE_OPERAND (sub, 0);
15650       tree op01 = TREE_OPERAND (sub, 1);
15651
15652       STRIP_NOPS (op00);
15653       if (TREE_CODE (op00) == ADDR_EXPR)
15654         {
15655           tree op00type;
15656           op00 = TREE_OPERAND (op00, 0);
15657           op00type = TREE_TYPE (op00);
15658
15659           /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15660           if (TREE_CODE (op00type) == VECTOR_TYPE
15661               && type == TREE_TYPE (op00type))
15662             {
15663               HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15664               tree part_width = TYPE_SIZE (type);
15665               unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15666               unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15667               tree index = bitsize_int (indexi);
15668
15669               if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (op00type))
15670                 return fold_build3_loc (loc,
15671                                         BIT_FIELD_REF, type, op00,
15672                                         part_width, index);
15673
15674             }
15675           /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15676           else if (TREE_CODE (op00type) == COMPLEX_TYPE
15677                    && type == TREE_TYPE (op00type))
15678             {
15679               tree size = TYPE_SIZE_UNIT (type);
15680               if (tree_int_cst_equal (size, op01))
15681                 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
15682             }
15683           /* ((foo *)&fooarray)[1] => fooarray[1] */
15684           else if (TREE_CODE (op00type) == ARRAY_TYPE
15685                    && type == TREE_TYPE (op00type))
15686             {
15687               tree type_domain = TYPE_DOMAIN (op00type);
15688               tree min_val = size_zero_node;
15689               if (type_domain && TYPE_MIN_VALUE (type_domain))
15690                 min_val = TYPE_MIN_VALUE (type_domain);
15691               op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
15692                                      TYPE_SIZE_UNIT (type));
15693               op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
15694               op0 = build4 (ARRAY_REF, type, op00, op01,
15695                             NULL_TREE, NULL_TREE);
15696               SET_EXPR_LOCATION (op0, loc);
15697               return op0;
15698             }
15699         }
15700     }
15701
15702   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15703   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15704       && type == TREE_TYPE (TREE_TYPE (subtype)))
15705     {
15706       tree type_domain;
15707       tree min_val = size_zero_node;
15708       sub = build_fold_indirect_ref_loc (loc, sub);
15709       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15710       if (type_domain && TYPE_MIN_VALUE (type_domain))
15711         min_val = TYPE_MIN_VALUE (type_domain);
15712       op0 = build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15713       SET_EXPR_LOCATION (op0, loc);
15714       return op0;
15715     }
15716
15717   return NULL_TREE;
15718 }
15719
15720 /* Builds an expression for an indirection through T, simplifying some
15721    cases.  */
15722
15723 tree
15724 build_fold_indirect_ref_loc (location_t loc, tree t)
15725 {
15726   tree type = TREE_TYPE (TREE_TYPE (t));
15727   tree sub = fold_indirect_ref_1 (loc, type, t);
15728
15729   if (sub)
15730     return sub;
15731
15732   t = build1 (INDIRECT_REF, type, t);
15733   SET_EXPR_LOCATION (t, loc);
15734   return t;
15735 }
15736
15737 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
15738
15739 tree
15740 fold_indirect_ref_loc (location_t loc, tree t)
15741 {
15742   tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15743
15744   if (sub)
15745     return sub;
15746   else
15747     return t;
15748 }
15749
15750 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15751    whose result is ignored.  The type of the returned tree need not be
15752    the same as the original expression.  */
15753
15754 tree
15755 fold_ignored_result (tree t)
15756 {
15757   if (!TREE_SIDE_EFFECTS (t))
15758     return integer_zero_node;
15759
15760   for (;;)
15761     switch (TREE_CODE_CLASS (TREE_CODE (t)))
15762       {
15763       case tcc_unary:
15764         t = TREE_OPERAND (t, 0);
15765         break;
15766
15767       case tcc_binary:
15768       case tcc_comparison:
15769         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15770           t = TREE_OPERAND (t, 0);
15771         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15772           t = TREE_OPERAND (t, 1);
15773         else
15774           return t;
15775         break;
15776
15777       case tcc_expression:
15778         switch (TREE_CODE (t))
15779           {
15780           case COMPOUND_EXPR:
15781             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15782               return t;
15783             t = TREE_OPERAND (t, 0);
15784             break;
15785
15786           case COND_EXPR:
15787             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15788                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15789               return t;
15790             t = TREE_OPERAND (t, 0);
15791             break;
15792
15793           default:
15794             return t;
15795           }
15796         break;
15797
15798       default:
15799         return t;
15800       }
15801 }
15802
15803 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15804    This can only be applied to objects of a sizetype.  */
15805
15806 tree
15807 round_up_loc (location_t loc, tree value, int divisor)
15808 {
15809   tree div = NULL_TREE;
15810
15811   gcc_assert (divisor > 0);
15812   if (divisor == 1)
15813     return value;
15814
15815   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15816      have to do anything.  Only do this when we are not given a const,
15817      because in that case, this check is more expensive than just
15818      doing it.  */
15819   if (TREE_CODE (value) != INTEGER_CST)
15820     {
15821       div = build_int_cst (TREE_TYPE (value), divisor);
15822
15823       if (multiple_of_p (TREE_TYPE (value), value, div))
15824         return value;
15825     }
15826
15827   /* If divisor is a power of two, simplify this to bit manipulation.  */
15828   if (divisor == (divisor & -divisor))
15829     {
15830       if (TREE_CODE (value) == INTEGER_CST)
15831         {
15832           double_int val = tree_to_double_int (value);
15833           bool overflow_p;
15834
15835           if ((val.low & (divisor - 1)) == 0)
15836             return value;
15837
15838           overflow_p = TREE_OVERFLOW (value);
15839           val.low &= ~(divisor - 1);
15840           val.low += divisor;
15841           if (val.low == 0)
15842             {
15843               val.high++;
15844               if (val.high == 0)
15845                 overflow_p = true;
15846             }
15847
15848           return force_fit_type_double (TREE_TYPE (value), val,
15849                                         -1, overflow_p);
15850         }
15851       else
15852         {
15853           tree t;
15854
15855           t = build_int_cst (TREE_TYPE (value), divisor - 1);
15856           value = size_binop_loc (loc, PLUS_EXPR, value, t);
15857           t = build_int_cst (TREE_TYPE (value), -divisor);
15858           value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15859         }
15860     }
15861   else
15862     {
15863       if (!div)
15864         div = build_int_cst (TREE_TYPE (value), divisor);
15865       value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
15866       value = size_binop_loc (loc, MULT_EXPR, value, div);
15867     }
15868
15869   return value;
15870 }
15871
15872 /* Likewise, but round down.  */
15873
15874 tree
15875 round_down_loc (location_t loc, tree value, int divisor)
15876 {
15877   tree div = NULL_TREE;
15878
15879   gcc_assert (divisor > 0);
15880   if (divisor == 1)
15881     return value;
15882
15883   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15884      have to do anything.  Only do this when we are not given a const,
15885      because in that case, this check is more expensive than just
15886      doing it.  */
15887   if (TREE_CODE (value) != INTEGER_CST)
15888     {
15889       div = build_int_cst (TREE_TYPE (value), divisor);
15890
15891       if (multiple_of_p (TREE_TYPE (value), value, div))
15892         return value;
15893     }
15894
15895   /* If divisor is a power of two, simplify this to bit manipulation.  */
15896   if (divisor == (divisor & -divisor))
15897     {
15898       tree t;
15899
15900       t = build_int_cst (TREE_TYPE (value), -divisor);
15901       value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15902     }
15903   else
15904     {
15905       if (!div)
15906         div = build_int_cst (TREE_TYPE (value), divisor);
15907       value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
15908       value = size_binop_loc (loc, MULT_EXPR, value, div);
15909     }
15910
15911   return value;
15912 }
15913
15914 /* Returns the pointer to the base of the object addressed by EXP and
15915    extracts the information about the offset of the access, storing it
15916    to PBITPOS and POFFSET.  */
15917
15918 static tree
15919 split_address_to_core_and_offset (tree exp,
15920                                   HOST_WIDE_INT *pbitpos, tree *poffset)
15921 {
15922   tree core;
15923   enum machine_mode mode;
15924   int unsignedp, volatilep;
15925   HOST_WIDE_INT bitsize;
15926   location_t loc = EXPR_LOCATION (exp);
15927
15928   if (TREE_CODE (exp) == ADDR_EXPR)
15929     {
15930       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15931                                   poffset, &mode, &unsignedp, &volatilep,
15932                                   false);
15933       core = build_fold_addr_expr_loc (loc, core);
15934     }
15935   else
15936     {
15937       core = exp;
15938       *pbitpos = 0;
15939       *poffset = NULL_TREE;
15940     }
15941
15942   return core;
15943 }
15944
15945 /* Returns true if addresses of E1 and E2 differ by a constant, false
15946    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
15947
15948 bool
15949 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15950 {
15951   tree core1, core2;
15952   HOST_WIDE_INT bitpos1, bitpos2;
15953   tree toffset1, toffset2, tdiff, type;
15954
15955   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15956   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15957
15958   if (bitpos1 % BITS_PER_UNIT != 0
15959       || bitpos2 % BITS_PER_UNIT != 0
15960       || !operand_equal_p (core1, core2, 0))
15961     return false;
15962
15963   if (toffset1 && toffset2)
15964     {
15965       type = TREE_TYPE (toffset1);
15966       if (type != TREE_TYPE (toffset2))
15967         toffset2 = fold_convert (type, toffset2);
15968
15969       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15970       if (!cst_and_fits_in_hwi (tdiff))
15971         return false;
15972
15973       *diff = int_cst_value (tdiff);
15974     }
15975   else if (toffset1 || toffset2)
15976     {
15977       /* If only one of the offsets is non-constant, the difference cannot
15978          be a constant.  */
15979       return false;
15980     }
15981   else
15982     *diff = 0;
15983
15984   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15985   return true;
15986 }
15987
15988 /* Simplify the floating point expression EXP when the sign of the
15989    result is not significant.  Return NULL_TREE if no simplification
15990    is possible.  */
15991
15992 tree
15993 fold_strip_sign_ops (tree exp)
15994 {
15995   tree arg0, arg1;
15996   location_t loc = EXPR_LOCATION (exp);
15997
15998   switch (TREE_CODE (exp))
15999     {
16000     case ABS_EXPR:
16001     case NEGATE_EXPR:
16002       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16003       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
16004
16005     case MULT_EXPR:
16006     case RDIV_EXPR:
16007       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
16008         return NULL_TREE;
16009       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16010       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16011       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
16012         return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
16013                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
16014                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
16015       break;
16016
16017     case COMPOUND_EXPR:
16018       arg0 = TREE_OPERAND (exp, 0);
16019       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16020       if (arg1)
16021         return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
16022       break;
16023
16024     case COND_EXPR:
16025       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16026       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
16027       if (arg0 || arg1)
16028         return fold_build3_loc (loc,
16029                             COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
16030                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
16031                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
16032       break;
16033
16034     case CALL_EXPR:
16035       {
16036         const enum built_in_function fcode = builtin_mathfn_code (exp);
16037         switch (fcode)
16038         {
16039         CASE_FLT_FN (BUILT_IN_COPYSIGN):
16040           /* Strip copysign function call, return the 1st argument. */
16041           arg0 = CALL_EXPR_ARG (exp, 0);
16042           arg1 = CALL_EXPR_ARG (exp, 1);
16043           return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
16044
16045         default:
16046           /* Strip sign ops from the argument of "odd" math functions.  */
16047           if (negate_mathfn_p (fcode))
16048             {
16049               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
16050               if (arg0)
16051                 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
16052             }
16053           break;
16054         }
16055       }
16056       break;
16057
16058     default:
16059       break;
16060     }
16061   return NULL_TREE;
16062 }