OSDN Git Service

85f9cd1fd91611f6ff79f552bef4c87aa76ee5bc
[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, size_binop
31    and force_fit_type_double.
32
33    fold takes a tree as argument and returns a simplified tree.
34
35    size_binop takes a tree code for an arithmetic operation
36    and two operands that are trees, and produces a tree for the
37    result, assuming the type comes from `sizetype'.
38
39    size_int takes an integer value, and creates a tree constant
40    with type from `sizetype'.
41
42    force_fit_type_double takes a constant, an overflowable flag and a
43    prior overflow indicator.  It forces the value to fit the type and
44    sets TREE_OVERFLOW.
45
46    Note: Since the folders get called on non-gimple code as well as
47    gimple code, we need to handle GIMPLE tuples as well as their
48    corresponding tree equivalents.  */
49
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include "flags.h"
55 #include "tree.h"
56 #include "real.h"
57 #include "fixed-value.h"
58 #include "rtl.h"
59 #include "expr.h"
60 #include "tm_p.h"
61 #include "target.h"
62 #include "toplev.h"
63 #include "intl.h"
64 #include "ggc.h"
65 #include "hashtab.h"
66 #include "langhooks.h"
67 #include "md5.h"
68 #include "gimple.h"
69
70 /* Nonzero if we are folding constants inside an initializer; zero
71    otherwise.  */
72 int folding_initializer = 0;
73
74 /* The following constants represent a bit based encoding of GCC's
75    comparison operators.  This encoding simplifies transformations
76    on relational comparison operators, such as AND and OR.  */
77 enum comparison_code {
78   COMPCODE_FALSE = 0,
79   COMPCODE_LT = 1,
80   COMPCODE_EQ = 2,
81   COMPCODE_LE = 3,
82   COMPCODE_GT = 4,
83   COMPCODE_LTGT = 5,
84   COMPCODE_GE = 6,
85   COMPCODE_ORD = 7,
86   COMPCODE_UNORD = 8,
87   COMPCODE_UNLT = 9,
88   COMPCODE_UNEQ = 10,
89   COMPCODE_UNLE = 11,
90   COMPCODE_UNGT = 12,
91   COMPCODE_NE = 13,
92   COMPCODE_UNGE = 14,
93   COMPCODE_TRUE = 15
94 };
95
96 static bool negate_mathfn_p (enum built_in_function);
97 static bool negate_expr_p (tree);
98 static tree negate_expr (tree);
99 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
100 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
101 static tree const_binop (enum tree_code, tree, tree, int);
102 static enum comparison_code comparison_to_compcode (enum tree_code);
103 static enum tree_code compcode_to_comparison (enum comparison_code);
104 static int operand_equal_for_comparison_p (tree, tree, tree);
105 static int twoval_comparison_p (tree, tree *, tree *, int *);
106 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
107 static tree pedantic_omit_one_operand_loc (location_t, tree, tree, tree);
108 static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree);
109 static tree make_bit_field_ref (location_t, tree, tree,
110                                 HOST_WIDE_INT, HOST_WIDE_INT, int);
111 static tree optimize_bit_field_compare (location_t, enum tree_code,
112                                         tree, tree, tree);
113 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
114                                     HOST_WIDE_INT *,
115                                     enum machine_mode *, int *, int *,
116                                     tree *, tree *);
117 static int all_ones_mask_p (const_tree, int);
118 static tree sign_bit_p (tree, const_tree);
119 static int simple_operand_p (const_tree);
120 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
121 static tree range_predecessor (tree);
122 static tree range_successor (tree);
123 extern tree make_range (tree, int *, tree *, tree *, bool *);
124 extern bool merge_ranges (int *, tree *, tree *, int, tree, tree, int,
125                           tree, tree);
126 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
127 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
128 static tree unextend (tree, int, int, tree);
129 static tree fold_truthop (location_t, enum tree_code, tree, tree, tree);
130 static tree optimize_minmax_comparison (location_t, enum tree_code,
131                                         tree, tree, tree);
132 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
133 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
134 static tree fold_binary_op_with_conditional_arg (location_t,
135                                                  enum tree_code, tree,
136                                                  tree, tree,
137                                                  tree, tree, int);
138 static tree fold_mathfn_compare (location_t,
139                                  enum built_in_function, enum tree_code,
140                                  tree, tree, tree);
141 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
142 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
143 static bool reorder_operands_p (const_tree, const_tree);
144 static tree fold_negate_const (tree, tree);
145 static tree fold_not_const (tree, tree);
146 static tree fold_relational_const (enum tree_code, tree, tree, tree);
147 static tree fold_convert_const (enum tree_code, tree, tree);
148
149
150 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
151    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
152    and SUM1.  Then this yields nonzero if overflow occurred during the
153    addition.
154
155    Overflow occurs if A and B have the same sign, but A and SUM differ in
156    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
157    sign.  */
158 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
159 \f
160 /* If ARG2 divides ARG1 with zero remainder, carries out the division
161    of type CODE and returns the quotient.
162    Otherwise returns NULL_TREE.  */
163
164 tree
165 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
166 {
167   double_int quo, rem;
168   int uns;
169
170   /* The sign of the division is according to operand two, that
171      does the correct thing for POINTER_PLUS_EXPR where we want
172      a signed division.  */
173   uns = TYPE_UNSIGNED (TREE_TYPE (arg2));
174   if (TREE_CODE (TREE_TYPE (arg2)) == INTEGER_TYPE
175       && TYPE_IS_SIZETYPE (TREE_TYPE (arg2)))
176     uns = false;
177
178   quo = double_int_divmod (tree_to_double_int (arg1),
179                            tree_to_double_int (arg2),
180                            uns, code, &rem);
181
182   if (double_int_zero_p (rem))
183     return build_int_cst_wide (TREE_TYPE (arg1), quo.low, quo.high);
184
185   return NULL_TREE; 
186 }
187 \f
188 /* This is nonzero if we should defer warnings about undefined
189    overflow.  This facility exists because these warnings are a
190    special case.  The code to estimate loop iterations does not want
191    to issue any warnings, since it works with expressions which do not
192    occur in user code.  Various bits of cleanup code call fold(), but
193    only use the result if it has certain characteristics (e.g., is a
194    constant); that code only wants to issue a warning if the result is
195    used.  */
196
197 static int fold_deferring_overflow_warnings;
198
199 /* If a warning about undefined overflow is deferred, this is the
200    warning.  Note that this may cause us to turn two warnings into
201    one, but that is fine since it is sufficient to only give one
202    warning per expression.  */
203
204 static const char* fold_deferred_overflow_warning;
205
206 /* If a warning about undefined overflow is deferred, this is the
207    level at which the warning should be emitted.  */
208
209 static enum warn_strict_overflow_code fold_deferred_overflow_code;
210
211 /* Start deferring overflow warnings.  We could use a stack here to
212    permit nested calls, but at present it is not necessary.  */
213
214 void
215 fold_defer_overflow_warnings (void)
216 {
217   ++fold_deferring_overflow_warnings;
218 }
219
220 /* Stop deferring overflow warnings.  If there is a pending warning,
221    and ISSUE is true, then issue the warning if appropriate.  STMT is
222    the statement with which the warning should be associated (used for
223    location information); STMT may be NULL.  CODE is the level of the
224    warning--a warn_strict_overflow_code value.  This function will use
225    the smaller of CODE and the deferred code when deciding whether to
226    issue the warning.  CODE may be zero to mean to always use the
227    deferred code.  */
228
229 void
230 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
231 {
232   const char *warnmsg;
233   location_t locus;
234
235   gcc_assert (fold_deferring_overflow_warnings > 0);
236   --fold_deferring_overflow_warnings;
237   if (fold_deferring_overflow_warnings > 0)
238     {
239       if (fold_deferred_overflow_warning != NULL
240           && code != 0
241           && code < (int) fold_deferred_overflow_code)
242         fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
243       return;
244     }
245
246   warnmsg = fold_deferred_overflow_warning;
247   fold_deferred_overflow_warning = NULL;
248
249   if (!issue || warnmsg == NULL)
250     return;
251
252   if (gimple_no_warning_p (stmt))
253     return;
254
255   /* Use the smallest code level when deciding to issue the
256      warning.  */
257   if (code == 0 || code > (int) fold_deferred_overflow_code)
258     code = fold_deferred_overflow_code;
259
260   if (!issue_strict_overflow_warning (code))
261     return;
262
263   if (stmt == NULL)
264     locus = input_location;
265   else
266     locus = gimple_location (stmt);
267   warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
268 }
269
270 /* Stop deferring overflow warnings, ignoring any deferred
271    warnings.  */
272
273 void
274 fold_undefer_and_ignore_overflow_warnings (void)
275 {
276   fold_undefer_overflow_warnings (false, NULL, 0);
277 }
278
279 /* Whether we are deferring overflow warnings.  */
280
281 bool
282 fold_deferring_overflow_warnings_p (void)
283 {
284   return fold_deferring_overflow_warnings > 0;
285 }
286
287 /* This is called when we fold something based on the fact that signed
288    overflow is undefined.  */
289
290 static void
291 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
292 {
293   if (fold_deferring_overflow_warnings > 0)
294     {
295       if (fold_deferred_overflow_warning == NULL
296           || wc < fold_deferred_overflow_code)
297         {
298           fold_deferred_overflow_warning = gmsgid;
299           fold_deferred_overflow_code = wc;
300         }
301     }
302   else if (issue_strict_overflow_warning (wc))
303     warning (OPT_Wstrict_overflow, gmsgid);
304 }
305 \f
306 /* Return true if the built-in mathematical function specified by CODE
307    is odd, i.e. -f(x) == f(-x).  */
308
309 static bool
310 negate_mathfn_p (enum built_in_function code)
311 {
312   switch (code)
313     {
314     CASE_FLT_FN (BUILT_IN_ASIN):
315     CASE_FLT_FN (BUILT_IN_ASINH):
316     CASE_FLT_FN (BUILT_IN_ATAN):
317     CASE_FLT_FN (BUILT_IN_ATANH):
318     CASE_FLT_FN (BUILT_IN_CASIN):
319     CASE_FLT_FN (BUILT_IN_CASINH):
320     CASE_FLT_FN (BUILT_IN_CATAN):
321     CASE_FLT_FN (BUILT_IN_CATANH):
322     CASE_FLT_FN (BUILT_IN_CBRT):
323     CASE_FLT_FN (BUILT_IN_CPROJ):
324     CASE_FLT_FN (BUILT_IN_CSIN):
325     CASE_FLT_FN (BUILT_IN_CSINH):
326     CASE_FLT_FN (BUILT_IN_CTAN):
327     CASE_FLT_FN (BUILT_IN_CTANH):
328     CASE_FLT_FN (BUILT_IN_ERF):
329     CASE_FLT_FN (BUILT_IN_LLROUND):
330     CASE_FLT_FN (BUILT_IN_LROUND):
331     CASE_FLT_FN (BUILT_IN_ROUND):
332     CASE_FLT_FN (BUILT_IN_SIN):
333     CASE_FLT_FN (BUILT_IN_SINH):
334     CASE_FLT_FN (BUILT_IN_TAN):
335     CASE_FLT_FN (BUILT_IN_TANH):
336     CASE_FLT_FN (BUILT_IN_TRUNC):
337       return true;
338
339     CASE_FLT_FN (BUILT_IN_LLRINT):
340     CASE_FLT_FN (BUILT_IN_LRINT):
341     CASE_FLT_FN (BUILT_IN_NEARBYINT):
342     CASE_FLT_FN (BUILT_IN_RINT):
343       return !flag_rounding_math;
344
345     default:
346       break;
347     }
348   return false;
349 }
350
351 /* Check whether we may negate an integer constant T without causing
352    overflow.  */
353
354 bool
355 may_negate_without_overflow_p (const_tree t)
356 {
357   unsigned HOST_WIDE_INT val;
358   unsigned int prec;
359   tree type;
360
361   gcc_assert (TREE_CODE (t) == INTEGER_CST);
362
363   type = TREE_TYPE (t);
364   if (TYPE_UNSIGNED (type))
365     return false;
366
367   prec = TYPE_PRECISION (type);
368   if (prec > HOST_BITS_PER_WIDE_INT)
369     {
370       if (TREE_INT_CST_LOW (t) != 0)
371         return true;
372       prec -= HOST_BITS_PER_WIDE_INT;
373       val = TREE_INT_CST_HIGH (t);
374     }
375   else
376     val = TREE_INT_CST_LOW (t);
377   if (prec < HOST_BITS_PER_WIDE_INT)
378     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
379   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
380 }
381
382 /* Determine whether an expression T can be cheaply negated using
383    the function negate_expr without introducing undefined overflow.  */
384
385 static bool
386 negate_expr_p (tree t)
387 {
388   tree type;
389
390   if (t == 0)
391     return false;
392
393   type = TREE_TYPE (t);
394
395   STRIP_SIGN_NOPS (t);
396   switch (TREE_CODE (t))
397     {
398     case INTEGER_CST:
399       if (TYPE_OVERFLOW_WRAPS (type))
400         return true;
401
402       /* Check that -CST will not overflow type.  */
403       return may_negate_without_overflow_p (t);
404     case BIT_NOT_EXPR:
405       return (INTEGRAL_TYPE_P (type)
406               && TYPE_OVERFLOW_WRAPS (type));
407
408     case FIXED_CST:
409     case NEGATE_EXPR:
410       return true;
411
412     case REAL_CST:
413       /* We want to canonicalize to positive real constants.  Pretend
414          that only negative ones can be easily negated.  */
415       return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
416
417     case COMPLEX_CST:
418       return negate_expr_p (TREE_REALPART (t))
419              && negate_expr_p (TREE_IMAGPART (t));
420
421     case COMPLEX_EXPR:
422       return negate_expr_p (TREE_OPERAND (t, 0))
423              && negate_expr_p (TREE_OPERAND (t, 1));
424
425     case CONJ_EXPR:
426       return negate_expr_p (TREE_OPERAND (t, 0));
427
428     case PLUS_EXPR:
429       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
430           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
431         return false;
432       /* -(A + B) -> (-B) - A.  */
433       if (negate_expr_p (TREE_OPERAND (t, 1))
434           && reorder_operands_p (TREE_OPERAND (t, 0),
435                                  TREE_OPERAND (t, 1)))
436         return true;
437       /* -(A + B) -> (-A) - B.  */
438       return negate_expr_p (TREE_OPERAND (t, 0));
439
440     case MINUS_EXPR:
441       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
442       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
443              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
444              && reorder_operands_p (TREE_OPERAND (t, 0),
445                                     TREE_OPERAND (t, 1));
446
447     case MULT_EXPR:
448       if (TYPE_UNSIGNED (TREE_TYPE (t)))
449         break;
450
451       /* Fall through.  */
452
453     case RDIV_EXPR:
454       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
455         return negate_expr_p (TREE_OPERAND (t, 1))
456                || negate_expr_p (TREE_OPERAND (t, 0));
457       break;
458
459     case TRUNC_DIV_EXPR:
460     case ROUND_DIV_EXPR:
461     case FLOOR_DIV_EXPR:
462     case CEIL_DIV_EXPR:
463     case EXACT_DIV_EXPR:
464       /* In general we can't negate A / B, because if A is INT_MIN and
465          B is 1, we may turn this into INT_MIN / -1 which is undefined
466          and actually traps on some architectures.  But if overflow is
467          undefined, we can negate, because - (INT_MIN / 1) is an
468          overflow.  */
469       if (INTEGRAL_TYPE_P (TREE_TYPE (t))
470           && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
471         break;
472       return negate_expr_p (TREE_OPERAND (t, 1))
473              || negate_expr_p (TREE_OPERAND (t, 0));
474
475     case NOP_EXPR:
476       /* Negate -((double)float) as (double)(-float).  */
477       if (TREE_CODE (type) == REAL_TYPE)
478         {
479           tree tem = strip_float_extensions (t);
480           if (tem != t)
481             return negate_expr_p (tem);
482         }
483       break;
484
485     case CALL_EXPR:
486       /* Negate -f(x) as f(-x).  */
487       if (negate_mathfn_p (builtin_mathfn_code (t)))
488         return negate_expr_p (CALL_EXPR_ARG (t, 0));
489       break;
490
491     case RSHIFT_EXPR:
492       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
493       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
494         {
495           tree op1 = TREE_OPERAND (t, 1);
496           if (TREE_INT_CST_HIGH (op1) == 0
497               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
498                  == TREE_INT_CST_LOW (op1))
499             return true;
500         }
501       break;
502
503     default:
504       break;
505     }
506   return false;
507 }
508
509 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
510    simplification is possible.
511    If negate_expr_p would return true for T, NULL_TREE will never be
512    returned.  */
513
514 static tree
515 fold_negate_expr (location_t loc, tree t)
516 {
517   tree type = TREE_TYPE (t);
518   tree tem;
519
520   switch (TREE_CODE (t))
521     {
522     /* Convert - (~A) to A + 1.  */
523     case BIT_NOT_EXPR:
524       if (INTEGRAL_TYPE_P (type))
525         return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
526                             build_int_cst (type, 1));
527       break;
528
529     case INTEGER_CST:
530       tem = fold_negate_const (t, type);
531       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
532           || !TYPE_OVERFLOW_TRAPS (type))
533         return tem;
534       break;
535
536     case REAL_CST:
537       tem = fold_negate_const (t, type);
538       /* Two's complement FP formats, such as c4x, may overflow.  */
539       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
540         return tem;
541       break;
542
543     case FIXED_CST:
544       tem = fold_negate_const (t, type);
545       return tem;
546
547     case COMPLEX_CST:
548       {
549         tree rpart = negate_expr (TREE_REALPART (t));
550         tree ipart = negate_expr (TREE_IMAGPART (t));
551
552         if ((TREE_CODE (rpart) == REAL_CST
553              && TREE_CODE (ipart) == REAL_CST)
554             || (TREE_CODE (rpart) == INTEGER_CST
555                 && TREE_CODE (ipart) == INTEGER_CST))
556           return build_complex (type, rpart, ipart);
557       }
558       break;
559
560     case COMPLEX_EXPR:
561       if (negate_expr_p (t))
562         return fold_build2_loc (loc, COMPLEX_EXPR, type,
563                             fold_negate_expr (loc, TREE_OPERAND (t, 0)),
564                             fold_negate_expr (loc, TREE_OPERAND (t, 1)));
565       break;
566
567     case CONJ_EXPR:
568       if (negate_expr_p (t))
569         return fold_build1_loc (loc, CONJ_EXPR, type,
570                             fold_negate_expr (loc, TREE_OPERAND (t, 0)));
571       break;
572
573     case NEGATE_EXPR:
574       return TREE_OPERAND (t, 0);
575
576     case PLUS_EXPR:
577       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
578           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
579         {
580           /* -(A + B) -> (-B) - A.  */
581           if (negate_expr_p (TREE_OPERAND (t, 1))
582               && reorder_operands_p (TREE_OPERAND (t, 0),
583                                      TREE_OPERAND (t, 1)))
584             {
585               tem = negate_expr (TREE_OPERAND (t, 1));
586               return fold_build2_loc (loc, MINUS_EXPR, type,
587                                   tem, TREE_OPERAND (t, 0));
588             }
589
590           /* -(A + B) -> (-A) - B.  */
591           if (negate_expr_p (TREE_OPERAND (t, 0)))
592             {
593               tem = negate_expr (TREE_OPERAND (t, 0));
594               return fold_build2_loc (loc, MINUS_EXPR, type,
595                                   tem, TREE_OPERAND (t, 1));
596             }
597         }
598       break;
599
600     case MINUS_EXPR:
601       /* - (A - B) -> B - A  */
602       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
603           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
604           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
605         return fold_build2_loc (loc, MINUS_EXPR, type,
606                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
607       break;
608
609     case MULT_EXPR:
610       if (TYPE_UNSIGNED (type))
611         break;
612
613       /* Fall through.  */
614
615     case RDIV_EXPR:
616       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
617         {
618           tem = TREE_OPERAND (t, 1);
619           if (negate_expr_p (tem))
620             return fold_build2_loc (loc, TREE_CODE (t), type,
621                                 TREE_OPERAND (t, 0), negate_expr (tem));
622           tem = TREE_OPERAND (t, 0);
623           if (negate_expr_p (tem))
624             return fold_build2_loc (loc, TREE_CODE (t), type,
625                                 negate_expr (tem), TREE_OPERAND (t, 1));
626         }
627       break;
628
629     case TRUNC_DIV_EXPR:
630     case ROUND_DIV_EXPR:
631     case FLOOR_DIV_EXPR:
632     case CEIL_DIV_EXPR:
633     case EXACT_DIV_EXPR:
634       /* In general we can't negate A / B, because if A is INT_MIN and
635          B is 1, we may turn this into INT_MIN / -1 which is undefined
636          and actually traps on some architectures.  But if overflow is
637          undefined, we can negate, because - (INT_MIN / 1) is an
638          overflow.  */
639       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
640         {
641           const char * const warnmsg = G_("assuming signed overflow does not "
642                                           "occur when negating a division");
643           tem = TREE_OPERAND (t, 1);
644           if (negate_expr_p (tem))
645             {
646               if (INTEGRAL_TYPE_P (type)
647                   && (TREE_CODE (tem) != INTEGER_CST
648                       || integer_onep (tem)))
649                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
650               return fold_build2_loc (loc, TREE_CODE (t), type,
651                                   TREE_OPERAND (t, 0), negate_expr (tem));
652             }
653           tem = TREE_OPERAND (t, 0);
654           if (negate_expr_p (tem))
655             {
656               if (INTEGRAL_TYPE_P (type)
657                   && (TREE_CODE (tem) != INTEGER_CST
658                       || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
659                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
660               return fold_build2_loc (loc, TREE_CODE (t), type,
661                                   negate_expr (tem), TREE_OPERAND (t, 1));
662             }
663         }
664       break;
665
666     case NOP_EXPR:
667       /* Convert -((double)float) into (double)(-float).  */
668       if (TREE_CODE (type) == REAL_TYPE)
669         {
670           tem = strip_float_extensions (t);
671           if (tem != t && negate_expr_p (tem))
672             return fold_convert_loc (loc, type, negate_expr (tem));
673         }
674       break;
675
676     case CALL_EXPR:
677       /* Negate -f(x) as f(-x).  */
678       if (negate_mathfn_p (builtin_mathfn_code (t))
679           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
680         {
681           tree fndecl, arg;
682
683           fndecl = get_callee_fndecl (t);
684           arg = negate_expr (CALL_EXPR_ARG (t, 0));
685           return build_call_expr_loc (loc, fndecl, 1, arg);
686         }
687       break;
688
689     case RSHIFT_EXPR:
690       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
691       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
692         {
693           tree op1 = TREE_OPERAND (t, 1);
694           if (TREE_INT_CST_HIGH (op1) == 0
695               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
696                  == TREE_INT_CST_LOW (op1))
697             {
698               tree ntype = TYPE_UNSIGNED (type)
699                            ? signed_type_for (type)
700                            : unsigned_type_for (type);
701               tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
702               temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
703               return fold_convert_loc (loc, type, temp);
704             }
705         }
706       break;
707
708     default:
709       break;
710     }
711
712   return NULL_TREE;
713 }
714
715 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
716    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
717    return NULL_TREE. */
718
719 static tree
720 negate_expr (tree t)
721 {
722   tree type, tem;
723   location_t loc;
724
725   if (t == NULL_TREE)
726     return NULL_TREE;
727
728   loc = EXPR_LOCATION (t);
729   type = TREE_TYPE (t);
730   STRIP_SIGN_NOPS (t);
731
732   tem = fold_negate_expr (loc, t);
733   if (!tem)
734     {
735       tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
736       SET_EXPR_LOCATION (tem, loc);
737     }
738   return fold_convert_loc (loc, type, tem);
739 }
740 \f
741 /* Split a tree IN into a constant, literal and variable parts that could be
742    combined with CODE to make IN.  "constant" means an expression with
743    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
744    commutative arithmetic operation.  Store the constant part into *CONP,
745    the literal in *LITP and return the variable part.  If a part isn't
746    present, set it to null.  If the tree does not decompose in this way,
747    return the entire tree as the variable part and the other parts as null.
748
749    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
750    case, we negate an operand that was subtracted.  Except if it is a
751    literal for which we use *MINUS_LITP instead.
752
753    If NEGATE_P is true, we are negating all of IN, again except a literal
754    for which we use *MINUS_LITP instead.
755
756    If IN is itself a literal or constant, return it as appropriate.
757
758    Note that we do not guarantee that any of the three values will be the
759    same type as IN, but they will have the same signedness and mode.  */
760
761 static tree
762 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
763             tree *minus_litp, int negate_p)
764 {
765   tree var = 0;
766
767   *conp = 0;
768   *litp = 0;
769   *minus_litp = 0;
770
771   /* Strip any conversions that don't change the machine mode or signedness.  */
772   STRIP_SIGN_NOPS (in);
773
774   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
775       || TREE_CODE (in) == FIXED_CST)
776     *litp = in;
777   else if (TREE_CODE (in) == code
778            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
779                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
780                /* We can associate addition and subtraction together (even
781                   though the C standard doesn't say so) for integers because
782                   the value is not affected.  For reals, the value might be
783                   affected, so we can't.  */
784                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
785                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
786     {
787       tree op0 = TREE_OPERAND (in, 0);
788       tree op1 = TREE_OPERAND (in, 1);
789       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
790       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
791
792       /* First see if either of the operands is a literal, then a constant.  */
793       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
794           || TREE_CODE (op0) == FIXED_CST)
795         *litp = op0, op0 = 0;
796       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
797                || TREE_CODE (op1) == FIXED_CST)
798         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
799
800       if (op0 != 0 && TREE_CONSTANT (op0))
801         *conp = op0, op0 = 0;
802       else if (op1 != 0 && TREE_CONSTANT (op1))
803         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
804
805       /* If we haven't dealt with either operand, this is not a case we can
806          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
807       if (op0 != 0 && op1 != 0)
808         var = in;
809       else if (op0 != 0)
810         var = op0;
811       else
812         var = op1, neg_var_p = neg1_p;
813
814       /* Now do any needed negations.  */
815       if (neg_litp_p)
816         *minus_litp = *litp, *litp = 0;
817       if (neg_conp_p)
818         *conp = negate_expr (*conp);
819       if (neg_var_p)
820         var = negate_expr (var);
821     }
822   else if (TREE_CONSTANT (in))
823     *conp = in;
824   else
825     var = in;
826
827   if (negate_p)
828     {
829       if (*litp)
830         *minus_litp = *litp, *litp = 0;
831       else if (*minus_litp)
832         *litp = *minus_litp, *minus_litp = 0;
833       *conp = negate_expr (*conp);
834       var = negate_expr (var);
835     }
836
837   return var;
838 }
839
840 /* Re-associate trees split by the above function.  T1 and T2 are
841    either expressions to associate or null.  Return the new
842    expression, if any.  LOC is the location of the new expression.  If
843    we build an operation, do it in TYPE and with CODE.  */
844
845 static tree
846 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
847 {
848   tree tem;
849
850   if (t1 == 0)
851     return t2;
852   else if (t2 == 0)
853     return t1;
854
855   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
856      try to fold this since we will have infinite recursion.  But do
857      deal with any NEGATE_EXPRs.  */
858   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
859       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
860     {
861       if (code == PLUS_EXPR)
862         {
863           if (TREE_CODE (t1) == NEGATE_EXPR)
864             tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t2),
865                           fold_convert_loc (loc, type, TREE_OPERAND (t1, 0)));
866           else if (TREE_CODE (t2) == NEGATE_EXPR)
867             tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t1),
868                           fold_convert_loc (loc, type, TREE_OPERAND (t2, 0)));
869           else if (integer_zerop (t2))
870             return fold_convert_loc (loc, type, t1);
871         }
872       else if (code == MINUS_EXPR)
873         {
874           if (integer_zerop (t2))
875             return fold_convert_loc (loc, type, t1);
876         }
877
878       tem = build2 (code, type, fold_convert_loc (loc, type, t1),
879                     fold_convert_loc (loc, type, t2));
880       goto associate_trees_exit;
881     }
882
883   return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
884                       fold_convert_loc (loc, type, t2));
885  associate_trees_exit:
886   protected_set_expr_location (tem, loc);
887   return tem;
888 }
889 \f
890 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
891    for use in int_const_binop, size_binop and size_diffop.  */
892
893 static bool
894 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
895 {
896   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
897     return false;
898   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
899     return false;
900
901   switch (code)
902     {
903     case LSHIFT_EXPR:
904     case RSHIFT_EXPR:
905     case LROTATE_EXPR:
906     case RROTATE_EXPR:
907       return true;
908
909     default:
910       break;
911     }
912
913   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
914          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
915          && TYPE_MODE (type1) == TYPE_MODE (type2);
916 }
917
918
919 /* Combine two integer constants ARG1 and ARG2 under operation CODE
920    to produce a new constant.  Return NULL_TREE if we don't know how
921    to evaluate CODE at compile-time.
922
923    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
924
925 tree
926 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
927 {
928   unsigned HOST_WIDE_INT int1l, int2l;
929   HOST_WIDE_INT int1h, int2h;
930   unsigned HOST_WIDE_INT low;
931   HOST_WIDE_INT hi;
932   unsigned HOST_WIDE_INT garbagel;
933   HOST_WIDE_INT garbageh;
934   tree t;
935   tree type = TREE_TYPE (arg1);
936   int uns = TYPE_UNSIGNED (type);
937   int is_sizetype
938     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
939   int overflow = 0;
940
941   int1l = TREE_INT_CST_LOW (arg1);
942   int1h = TREE_INT_CST_HIGH (arg1);
943   int2l = TREE_INT_CST_LOW (arg2);
944   int2h = TREE_INT_CST_HIGH (arg2);
945
946   switch (code)
947     {
948     case BIT_IOR_EXPR:
949       low = int1l | int2l, hi = int1h | int2h;
950       break;
951
952     case BIT_XOR_EXPR:
953       low = int1l ^ int2l, hi = int1h ^ int2h;
954       break;
955
956     case BIT_AND_EXPR:
957       low = int1l & int2l, hi = int1h & int2h;
958       break;
959
960     case RSHIFT_EXPR:
961       int2l = -int2l;
962     case LSHIFT_EXPR:
963       /* It's unclear from the C standard whether shifts can overflow.
964          The following code ignores overflow; perhaps a C standard
965          interpretation ruling is needed.  */
966       lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
967                      &low, &hi, !uns);
968       break;
969
970     case RROTATE_EXPR:
971       int2l = - int2l;
972     case LROTATE_EXPR:
973       lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
974                       &low, &hi);
975       break;
976
977     case PLUS_EXPR:
978       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
979       break;
980
981     case MINUS_EXPR:
982       neg_double (int2l, int2h, &low, &hi);
983       add_double (int1l, int1h, low, hi, &low, &hi);
984       overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
985       break;
986
987     case MULT_EXPR:
988       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
989       break;
990
991     case TRUNC_DIV_EXPR:
992     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
993     case EXACT_DIV_EXPR:
994       /* This is a shortcut for a common special case.  */
995       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
996           && !TREE_OVERFLOW (arg1)
997           && !TREE_OVERFLOW (arg2)
998           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
999         {
1000           if (code == CEIL_DIV_EXPR)
1001             int1l += int2l - 1;
1002
1003           low = int1l / int2l, hi = 0;
1004           break;
1005         }
1006
1007       /* ... fall through ...  */
1008
1009     case ROUND_DIV_EXPR:
1010       if (int2h == 0 && int2l == 0)
1011         return NULL_TREE;
1012       if (int2h == 0 && int2l == 1)
1013         {
1014           low = int1l, hi = int1h;
1015           break;
1016         }
1017       if (int1l == int2l && int1h == int2h
1018           && ! (int1l == 0 && int1h == 0))
1019         {
1020           low = 1, hi = 0;
1021           break;
1022         }
1023       overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1024                                        &low, &hi, &garbagel, &garbageh);
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 (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1031           && !TREE_OVERFLOW (arg1)
1032           && !TREE_OVERFLOW (arg2)
1033           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1034         {
1035           if (code == CEIL_MOD_EXPR)
1036             int1l += int2l - 1;
1037           low = int1l % int2l, hi = 0;
1038           break;
1039         }
1040
1041       /* ... fall through ...  */
1042
1043     case ROUND_MOD_EXPR:
1044       if (int2h == 0 && int2l == 0)
1045         return NULL_TREE;
1046       overflow = div_and_round_double (code, uns,
1047                                        int1l, int1h, int2l, int2h,
1048                                        &garbagel, &garbageh, &low, &hi);
1049       break;
1050
1051     case MIN_EXPR:
1052     case MAX_EXPR:
1053       if (uns)
1054         low = (((unsigned HOST_WIDE_INT) int1h
1055                 < (unsigned HOST_WIDE_INT) int2h)
1056                || (((unsigned HOST_WIDE_INT) int1h
1057                     == (unsigned HOST_WIDE_INT) int2h)
1058                    && int1l < int2l));
1059       else
1060         low = (int1h < int2h
1061                || (int1h == int2h && int1l < int2l));
1062
1063       if (low == (code == MIN_EXPR))
1064         low = int1l, hi = int1h;
1065       else
1066         low = int2l, hi = int2h;
1067       break;
1068
1069     default:
1070       return NULL_TREE;
1071     }
1072
1073   if (notrunc)
1074     {
1075       t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1076
1077       /* Propagate overflow flags ourselves.  */
1078       if (((!uns || is_sizetype) && overflow)
1079           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1080         {
1081           t = copy_node (t);
1082           TREE_OVERFLOW (t) = 1;
1083         }
1084     }
1085   else
1086     t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1087                                ((!uns || is_sizetype) && overflow)
1088                                | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1089
1090   return t;
1091 }
1092
1093 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1094    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1095    are the same kind of constant and the same machine mode.  Return zero if
1096    combining the constants is not allowed in the current operating mode.
1097
1098    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1099
1100 static tree
1101 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1102 {
1103   /* Sanity check for the recursive cases.  */
1104   if (!arg1 || !arg2)
1105     return NULL_TREE;
1106
1107   STRIP_NOPS (arg1);
1108   STRIP_NOPS (arg2);
1109
1110   if (TREE_CODE (arg1) == INTEGER_CST)
1111     return int_const_binop (code, arg1, arg2, notrunc);
1112
1113   if (TREE_CODE (arg1) == REAL_CST)
1114     {
1115       enum machine_mode mode;
1116       REAL_VALUE_TYPE d1;
1117       REAL_VALUE_TYPE d2;
1118       REAL_VALUE_TYPE value;
1119       REAL_VALUE_TYPE result;
1120       bool inexact;
1121       tree t, type;
1122
1123       /* The following codes are handled by real_arithmetic.  */
1124       switch (code)
1125         {
1126         case PLUS_EXPR:
1127         case MINUS_EXPR:
1128         case MULT_EXPR:
1129         case RDIV_EXPR:
1130         case MIN_EXPR:
1131         case MAX_EXPR:
1132           break;
1133
1134         default:
1135           return NULL_TREE;
1136         }
1137
1138       d1 = TREE_REAL_CST (arg1);
1139       d2 = TREE_REAL_CST (arg2);
1140
1141       type = TREE_TYPE (arg1);
1142       mode = TYPE_MODE (type);
1143
1144       /* Don't perform operation if we honor signaling NaNs and
1145          either operand is a NaN.  */
1146       if (HONOR_SNANS (mode)
1147           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1148         return NULL_TREE;
1149
1150       /* Don't perform operation if it would raise a division
1151          by zero exception.  */
1152       if (code == RDIV_EXPR
1153           && REAL_VALUES_EQUAL (d2, dconst0)
1154           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1155         return NULL_TREE;
1156
1157       /* If either operand is a NaN, just return it.  Otherwise, set up
1158          for floating-point trap; we return an overflow.  */
1159       if (REAL_VALUE_ISNAN (d1))
1160         return arg1;
1161       else if (REAL_VALUE_ISNAN (d2))
1162         return arg2;
1163
1164       inexact = real_arithmetic (&value, code, &d1, &d2);
1165       real_convert (&result, mode, &value);
1166
1167       /* Don't constant fold this floating point operation if
1168          the result has overflowed and flag_trapping_math.  */
1169       if (flag_trapping_math
1170           && MODE_HAS_INFINITIES (mode)
1171           && REAL_VALUE_ISINF (result)
1172           && !REAL_VALUE_ISINF (d1)
1173           && !REAL_VALUE_ISINF (d2))
1174         return NULL_TREE;
1175
1176       /* Don't constant fold this floating point operation if the
1177          result may dependent upon the run-time rounding mode and
1178          flag_rounding_math is set, or if GCC's software emulation
1179          is unable to accurately represent the result.  */
1180       if ((flag_rounding_math
1181            || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1182           && (inexact || !real_identical (&result, &value)))
1183         return NULL_TREE;
1184
1185       t = build_real (type, result);
1186
1187       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1188       return t;
1189     }
1190
1191   if (TREE_CODE (arg1) == FIXED_CST)
1192     {
1193       FIXED_VALUE_TYPE f1;
1194       FIXED_VALUE_TYPE f2;
1195       FIXED_VALUE_TYPE result;
1196       tree t, type;
1197       int sat_p;
1198       bool overflow_p;
1199
1200       /* The following codes are handled by fixed_arithmetic.  */
1201       switch (code)
1202         {
1203         case PLUS_EXPR:
1204         case MINUS_EXPR:
1205         case MULT_EXPR:
1206         case TRUNC_DIV_EXPR:
1207           f2 = TREE_FIXED_CST (arg2);
1208           break;
1209
1210         case LSHIFT_EXPR:
1211         case RSHIFT_EXPR:
1212           f2.data.high = TREE_INT_CST_HIGH (arg2);
1213           f2.data.low = TREE_INT_CST_LOW (arg2);
1214           f2.mode = SImode;
1215           break;
1216
1217         default:
1218           return NULL_TREE;
1219         }
1220
1221       f1 = TREE_FIXED_CST (arg1);
1222       type = TREE_TYPE (arg1);
1223       sat_p = TYPE_SATURATING (type);
1224       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1225       t = build_fixed (type, result);
1226       /* Propagate overflow flags.  */
1227       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1228         TREE_OVERFLOW (t) = 1;
1229       return t;
1230     }
1231
1232   if (TREE_CODE (arg1) == COMPLEX_CST)
1233     {
1234       tree type = TREE_TYPE (arg1);
1235       tree r1 = TREE_REALPART (arg1);
1236       tree i1 = TREE_IMAGPART (arg1);
1237       tree r2 = TREE_REALPART (arg2);
1238       tree i2 = TREE_IMAGPART (arg2);
1239       tree real, imag;
1240
1241       switch (code)
1242         {
1243         case PLUS_EXPR:
1244         case MINUS_EXPR:
1245           real = const_binop (code, r1, r2, notrunc);
1246           imag = const_binop (code, i1, i2, notrunc);
1247           break;
1248
1249         case MULT_EXPR:
1250           if (COMPLEX_FLOAT_TYPE_P (type))
1251             return do_mpc_arg2 (arg1, arg2, type,
1252                                 /* do_nonfinite= */ folding_initializer,
1253                                 mpc_mul);
1254
1255           real = const_binop (MINUS_EXPR,
1256                               const_binop (MULT_EXPR, r1, r2, notrunc),
1257                               const_binop (MULT_EXPR, i1, i2, notrunc),
1258                               notrunc);
1259           imag = const_binop (PLUS_EXPR,
1260                               const_binop (MULT_EXPR, r1, i2, notrunc),
1261                               const_binop (MULT_EXPR, i1, r2, notrunc),
1262                               notrunc);
1263           break;
1264
1265         case RDIV_EXPR:
1266           if (COMPLEX_FLOAT_TYPE_P (type))
1267             return do_mpc_arg2 (arg1, arg2, type,
1268                                 /* do_nonfinite= */ folding_initializer,
1269                                 mpc_div);
1270           /* Fallthru ... */
1271         case TRUNC_DIV_EXPR:
1272         case CEIL_DIV_EXPR:
1273         case FLOOR_DIV_EXPR:
1274         case ROUND_DIV_EXPR:
1275           if (flag_complex_method == 0)
1276           {
1277             /* Keep this algorithm in sync with
1278                tree-complex.c:expand_complex_div_straight().
1279
1280                Expand complex division to scalars, straightforward algorithm.
1281                a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1282                t = br*br + bi*bi
1283             */
1284             tree magsquared
1285               = const_binop (PLUS_EXPR,
1286                              const_binop (MULT_EXPR, r2, r2, notrunc),
1287                              const_binop (MULT_EXPR, i2, i2, notrunc),
1288                              notrunc);
1289             tree t1
1290               = const_binop (PLUS_EXPR,
1291                              const_binop (MULT_EXPR, r1, r2, notrunc),
1292                              const_binop (MULT_EXPR, i1, i2, notrunc),
1293                              notrunc);
1294             tree t2
1295               = const_binop (MINUS_EXPR,
1296                              const_binop (MULT_EXPR, i1, r2, notrunc),
1297                              const_binop (MULT_EXPR, r1, i2, notrunc),
1298                              notrunc);
1299
1300             real = const_binop (code, t1, magsquared, notrunc);
1301             imag = const_binop (code, t2, magsquared, notrunc);
1302           }
1303           else
1304           {
1305             /* Keep this algorithm in sync with
1306                tree-complex.c:expand_complex_div_wide().
1307
1308                Expand complex division to scalars, modified algorithm to minimize
1309                overflow with wide input ranges.  */
1310             tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1311                                         fold_abs_const (r2, TREE_TYPE (type)),
1312                                         fold_abs_const (i2, TREE_TYPE (type)));
1313
1314             if (integer_nonzerop (compare))
1315               {
1316                 /* In the TRUE branch, we compute
1317                    ratio = br/bi;
1318                    div = (br * ratio) + bi;
1319                    tr = (ar * ratio) + ai;
1320                    ti = (ai * ratio) - ar;
1321                    tr = tr / div;
1322                    ti = ti / div;  */
1323                 tree ratio = const_binop (code, r2, i2, notrunc);
1324                 tree div = const_binop (PLUS_EXPR, i2,
1325                                         const_binop (MULT_EXPR, r2, ratio,
1326                                                      notrunc),
1327                                         notrunc);
1328                 real = const_binop (MULT_EXPR, r1, ratio, notrunc);
1329                 real = const_binop (PLUS_EXPR, real, i1, notrunc);
1330                 real = const_binop (code, real, div, notrunc);
1331
1332                 imag = const_binop (MULT_EXPR, i1, ratio, notrunc);
1333                 imag = const_binop (MINUS_EXPR, imag, r1, notrunc);
1334                 imag = const_binop (code, imag, div, notrunc);
1335               }
1336             else
1337               {
1338                 /* In the FALSE branch, we compute
1339                    ratio = d/c;
1340                    divisor = (d * ratio) + c;
1341                    tr = (b * ratio) + a;
1342                    ti = b - (a * ratio);
1343                    tr = tr / div;
1344                    ti = ti / div;  */
1345                 tree ratio = const_binop (code, i2, r2, notrunc);
1346                 tree div = const_binop (PLUS_EXPR, r2,
1347                                         const_binop (MULT_EXPR, i2, ratio,
1348                                                      notrunc),
1349                                         notrunc);
1350
1351                 real = const_binop (MULT_EXPR, i1, ratio, notrunc);
1352                 real = const_binop (PLUS_EXPR, real, r1, notrunc);
1353                 real = const_binop (code, real, div, notrunc);
1354
1355                 imag = const_binop (MULT_EXPR, r1, ratio, notrunc);
1356                 imag = const_binop (MINUS_EXPR, i1, imag, notrunc);
1357                 imag = const_binop (code, imag, div, notrunc);
1358               }
1359           }
1360           break;
1361
1362         default:
1363           return NULL_TREE;
1364         }
1365
1366       if (real && imag)
1367         return build_complex (type, real, imag);
1368     }
1369
1370   if (TREE_CODE (arg1) == VECTOR_CST)
1371     {
1372       tree type = TREE_TYPE(arg1);
1373       int count = TYPE_VECTOR_SUBPARTS (type), i;
1374       tree elements1, elements2, list = NULL_TREE;
1375
1376       if(TREE_CODE(arg2) != VECTOR_CST)
1377         return NULL_TREE;
1378
1379       elements1 = TREE_VECTOR_CST_ELTS (arg1);
1380       elements2 = TREE_VECTOR_CST_ELTS (arg2);
1381
1382       for (i = 0; i < count; i++)
1383         {
1384           tree elem1, elem2, elem;
1385
1386           /* The trailing elements can be empty and should be treated as 0 */
1387           if(!elements1)
1388             elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1389           else
1390             {
1391               elem1 = TREE_VALUE(elements1);
1392               elements1 = TREE_CHAIN (elements1);
1393             }
1394
1395           if(!elements2)
1396             elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1397           else
1398             {
1399               elem2 = TREE_VALUE(elements2);
1400               elements2 = TREE_CHAIN (elements2);
1401             }
1402
1403           elem = const_binop (code, elem1, elem2, notrunc);
1404
1405           /* It is possible that const_binop cannot handle the given
1406             code and return NULL_TREE */
1407           if(elem == NULL_TREE)
1408             return NULL_TREE;
1409
1410           list = tree_cons (NULL_TREE, elem, list);
1411         }
1412       return build_vector(type, nreverse(list));
1413     }
1414   return NULL_TREE;
1415 }
1416
1417 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
1418    indicates which particular sizetype to create.  */
1419
1420 tree
1421 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1422 {
1423   return build_int_cst (sizetype_tab[(int) kind], number);
1424 }
1425 \f
1426 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
1427    is a tree code.  The type of the result is taken from the operands.
1428    Both must be equivalent integer types, ala int_binop_types_match_p.
1429    If the operands are constant, so is the result.  */
1430
1431 tree
1432 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1433 {
1434   tree type = TREE_TYPE (arg0);
1435
1436   if (arg0 == error_mark_node || arg1 == error_mark_node)
1437     return error_mark_node;
1438
1439   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1440                                        TREE_TYPE (arg1)));
1441
1442   /* Handle the special case of two integer constants faster.  */
1443   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1444     {
1445       /* And some specific cases even faster than that.  */
1446       if (code == PLUS_EXPR)
1447         {
1448           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1449             return arg1;
1450           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1451             return arg0;
1452         }
1453       else if (code == MINUS_EXPR)
1454         {
1455           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1456             return arg0;
1457         }
1458       else if (code == MULT_EXPR)
1459         {
1460           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1461             return arg1;
1462         }
1463
1464       /* Handle general case of two integer constants.  */
1465       return int_const_binop (code, arg0, arg1, 0);
1466     }
1467
1468   return fold_build2_loc (loc, code, type, arg0, arg1);
1469 }
1470
1471 /* Given two values, either both of sizetype or both of bitsizetype,
1472    compute the difference between the two values.  Return the value
1473    in signed type corresponding to the type of the operands.  */
1474
1475 tree
1476 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1477 {
1478   tree type = TREE_TYPE (arg0);
1479   tree ctype;
1480
1481   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1482                                        TREE_TYPE (arg1)));
1483
1484   /* If the type is already signed, just do the simple thing.  */
1485   if (!TYPE_UNSIGNED (type))
1486     return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1487
1488   if (type == sizetype)
1489     ctype = ssizetype;
1490   else if (type == bitsizetype)
1491     ctype = sbitsizetype;
1492   else
1493     ctype = signed_type_for (type);
1494
1495   /* If either operand is not a constant, do the conversions to the signed
1496      type and subtract.  The hardware will do the right thing with any
1497      overflow in the subtraction.  */
1498   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1499     return size_binop_loc (loc, MINUS_EXPR,
1500                            fold_convert_loc (loc, ctype, arg0),
1501                            fold_convert_loc (loc, ctype, arg1));
1502
1503   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1504      Otherwise, subtract the other way, convert to CTYPE (we know that can't
1505      overflow) and negate (which can't either).  Special-case a result
1506      of zero while we're here.  */
1507   if (tree_int_cst_equal (arg0, arg1))
1508     return build_int_cst (ctype, 0);
1509   else if (tree_int_cst_lt (arg1, arg0))
1510     return fold_convert_loc (loc, ctype,
1511                              size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1512   else
1513     return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1514                            fold_convert_loc (loc, ctype,
1515                                              size_binop_loc (loc,
1516                                                              MINUS_EXPR,
1517                                                              arg1, arg0)));
1518 }
1519 \f
1520 /* A subroutine of fold_convert_const handling conversions of an
1521    INTEGER_CST to another integer type.  */
1522
1523 static tree
1524 fold_convert_const_int_from_int (tree type, const_tree arg1)
1525 {
1526   tree t;
1527
1528   /* Given an integer constant, make new constant with new type,
1529      appropriately sign-extended or truncated.  */
1530   t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
1531                              TREE_INT_CST_HIGH (arg1),
1532                              !POINTER_TYPE_P (TREE_TYPE (arg1)),
1533                              (TREE_INT_CST_HIGH (arg1) < 0
1534                               && (TYPE_UNSIGNED (type)
1535                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1536                              | TREE_OVERFLOW (arg1));
1537
1538   return t;
1539 }
1540
1541 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1542    to an integer type.  */
1543
1544 static tree
1545 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1546 {
1547   int overflow = 0;
1548   tree t;
1549
1550   /* The following code implements the floating point to integer
1551      conversion rules required by the Java Language Specification,
1552      that IEEE NaNs are mapped to zero and values that overflow
1553      the target precision saturate, i.e. values greater than
1554      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1555      are mapped to INT_MIN.  These semantics are allowed by the
1556      C and C++ standards that simply state that the behavior of
1557      FP-to-integer conversion is unspecified upon overflow.  */
1558
1559   double_int val;
1560   REAL_VALUE_TYPE r;
1561   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1562
1563   switch (code)
1564     {
1565     case FIX_TRUNC_EXPR:
1566       real_trunc (&r, VOIDmode, &x);
1567       break;
1568
1569     default:
1570       gcc_unreachable ();
1571     }
1572
1573   /* If R is NaN, return zero and show we have an overflow.  */
1574   if (REAL_VALUE_ISNAN (r))
1575     {
1576       overflow = 1;
1577       val = double_int_zero;
1578     }
1579
1580   /* See if R is less than the lower bound or greater than the
1581      upper bound.  */
1582
1583   if (! overflow)
1584     {
1585       tree lt = TYPE_MIN_VALUE (type);
1586       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1587       if (REAL_VALUES_LESS (r, l))
1588         {
1589           overflow = 1;
1590           val = tree_to_double_int (lt);
1591         }
1592     }
1593
1594   if (! overflow)
1595     {
1596       tree ut = TYPE_MAX_VALUE (type);
1597       if (ut)
1598         {
1599           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1600           if (REAL_VALUES_LESS (u, r))
1601             {
1602               overflow = 1;
1603               val = tree_to_double_int (ut);
1604             }
1605         }
1606     }
1607
1608   if (! overflow)
1609     real_to_integer2 ((HOST_WIDE_INT *) &val.low, &val.high, &r);
1610
1611   t = force_fit_type_double (type, val.low, val.high, -1,
1612                              overflow | TREE_OVERFLOW (arg1));
1613   return t;
1614 }
1615
1616 /* A subroutine of fold_convert_const handling conversions of a
1617    FIXED_CST to an integer type.  */
1618
1619 static tree
1620 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1621 {
1622   tree t;
1623   double_int temp, temp_trunc;
1624   unsigned int mode;
1625
1626   /* Right shift FIXED_CST to temp by fbit.  */
1627   temp = TREE_FIXED_CST (arg1).data;
1628   mode = TREE_FIXED_CST (arg1).mode;
1629   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
1630     {
1631       temp = double_int_rshift (temp, GET_MODE_FBIT (mode),
1632                                 HOST_BITS_PER_DOUBLE_INT,
1633                                 SIGNED_FIXED_POINT_MODE_P (mode));
1634
1635       /* Left shift temp to temp_trunc by fbit.  */
1636       temp_trunc = double_int_lshift (temp, GET_MODE_FBIT (mode),
1637                                       HOST_BITS_PER_DOUBLE_INT,
1638                                       SIGNED_FIXED_POINT_MODE_P (mode));
1639     }
1640   else
1641     {
1642       temp = double_int_zero;
1643       temp_trunc = double_int_zero;
1644     }
1645
1646   /* If FIXED_CST is negative, we need to round the value toward 0.
1647      By checking if the fractional bits are not zero to add 1 to temp.  */
1648   if (SIGNED_FIXED_POINT_MODE_P (mode)
1649       && double_int_negative_p (temp_trunc)
1650       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
1651     temp = double_int_add (temp, double_int_one);
1652
1653   /* Given a fixed-point constant, make new constant with new type,
1654      appropriately sign-extended or truncated.  */
1655   t = force_fit_type_double (type, temp.low, temp.high, -1,
1656                              (double_int_negative_p (temp)
1657                               && (TYPE_UNSIGNED (type)
1658                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1659                              | TREE_OVERFLOW (arg1));
1660
1661   return t;
1662 }
1663
1664 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1665    to another floating point type.  */
1666
1667 static tree
1668 fold_convert_const_real_from_real (tree type, const_tree arg1)
1669 {
1670   REAL_VALUE_TYPE value;
1671   tree t;
1672
1673   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1674   t = build_real (type, value);
1675
1676   /* If converting an infinity or NAN to a representation that doesn't
1677      have one, set the overflow bit so that we can produce some kind of
1678      error message at the appropriate point if necessary.  It's not the
1679      most user-friendly message, but it's better than nothing.  */
1680   if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1681       && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1682     TREE_OVERFLOW (t) = 1;
1683   else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1684            && !MODE_HAS_NANS (TYPE_MODE (type)))
1685     TREE_OVERFLOW (t) = 1;
1686   /* Regular overflow, conversion produced an infinity in a mode that
1687      can't represent them.  */
1688   else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1689            && REAL_VALUE_ISINF (value)
1690            && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1691     TREE_OVERFLOW (t) = 1;
1692   else
1693     TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1694   return t;
1695 }
1696
1697 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1698    to a floating point type.  */
1699
1700 static tree
1701 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
1702 {
1703   REAL_VALUE_TYPE value;
1704   tree t;
1705
1706   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
1707   t = build_real (type, value);
1708
1709   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1710   return t;
1711 }
1712
1713 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1714    to another fixed-point type.  */
1715
1716 static tree
1717 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
1718 {
1719   FIXED_VALUE_TYPE value;
1720   tree t;
1721   bool overflow_p;
1722
1723   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
1724                               TYPE_SATURATING (type));
1725   t = build_fixed (type, value);
1726
1727   /* Propagate overflow flags.  */
1728   if (overflow_p | TREE_OVERFLOW (arg1))
1729     TREE_OVERFLOW (t) = 1;
1730   return t;
1731 }
1732
1733 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
1734    to a fixed-point type.  */
1735
1736 static tree
1737 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
1738 {
1739   FIXED_VALUE_TYPE value;
1740   tree t;
1741   bool overflow_p;
1742
1743   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
1744                                        TREE_INT_CST (arg1),
1745                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
1746                                        TYPE_SATURATING (type));
1747   t = build_fixed (type, value);
1748
1749   /* Propagate overflow flags.  */
1750   if (overflow_p | TREE_OVERFLOW (arg1))
1751     TREE_OVERFLOW (t) = 1;
1752   return t;
1753 }
1754
1755 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1756    to a fixed-point type.  */
1757
1758 static tree
1759 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
1760 {
1761   FIXED_VALUE_TYPE value;
1762   tree t;
1763   bool overflow_p;
1764
1765   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
1766                                         &TREE_REAL_CST (arg1),
1767                                         TYPE_SATURATING (type));
1768   t = build_fixed (type, value);
1769
1770   /* Propagate overflow flags.  */
1771   if (overflow_p | TREE_OVERFLOW (arg1))
1772     TREE_OVERFLOW (t) = 1;
1773   return t;
1774 }
1775
1776 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1777    type TYPE.  If no simplification can be done return NULL_TREE.  */
1778
1779 static tree
1780 fold_convert_const (enum tree_code code, tree type, tree arg1)
1781 {
1782   if (TREE_TYPE (arg1) == type)
1783     return arg1;
1784
1785   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
1786       || TREE_CODE (type) == OFFSET_TYPE)
1787     {
1788       if (TREE_CODE (arg1) == INTEGER_CST)
1789         return fold_convert_const_int_from_int (type, arg1);
1790       else if (TREE_CODE (arg1) == REAL_CST)
1791         return fold_convert_const_int_from_real (code, type, arg1);
1792       else if (TREE_CODE (arg1) == FIXED_CST)
1793         return fold_convert_const_int_from_fixed (type, arg1);
1794     }
1795   else if (TREE_CODE (type) == REAL_TYPE)
1796     {
1797       if (TREE_CODE (arg1) == INTEGER_CST)
1798         return build_real_from_int_cst (type, arg1);
1799       else if (TREE_CODE (arg1) == REAL_CST)
1800         return fold_convert_const_real_from_real (type, arg1);
1801       else if (TREE_CODE (arg1) == FIXED_CST)
1802         return fold_convert_const_real_from_fixed (type, arg1);
1803     }
1804   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
1805     {
1806       if (TREE_CODE (arg1) == FIXED_CST)
1807         return fold_convert_const_fixed_from_fixed (type, arg1);
1808       else if (TREE_CODE (arg1) == INTEGER_CST)
1809         return fold_convert_const_fixed_from_int (type, arg1);
1810       else if (TREE_CODE (arg1) == REAL_CST)
1811         return fold_convert_const_fixed_from_real (type, arg1);
1812     }
1813   return NULL_TREE;
1814 }
1815
1816 /* Construct a vector of zero elements of vector type TYPE.  */
1817
1818 static tree
1819 build_zero_vector (tree type)
1820 {
1821   tree elem, list;
1822   int i, units;
1823
1824   elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1825   units = TYPE_VECTOR_SUBPARTS (type);
1826
1827   list = NULL_TREE;
1828   for (i = 0; i < units; i++)
1829     list = tree_cons (NULL_TREE, elem, list);
1830   return build_vector (type, list);
1831 }
1832
1833 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
1834
1835 bool
1836 fold_convertible_p (const_tree type, const_tree arg)
1837 {
1838   tree orig = TREE_TYPE (arg);
1839
1840   if (type == orig)
1841     return true;
1842
1843   if (TREE_CODE (arg) == ERROR_MARK
1844       || TREE_CODE (type) == ERROR_MARK
1845       || TREE_CODE (orig) == ERROR_MARK)
1846     return false;
1847
1848   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1849     return true;
1850
1851   switch (TREE_CODE (type))
1852     {
1853     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1854     case POINTER_TYPE: case REFERENCE_TYPE:
1855     case OFFSET_TYPE:
1856       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1857           || TREE_CODE (orig) == OFFSET_TYPE)
1858         return true;
1859       return (TREE_CODE (orig) == VECTOR_TYPE
1860               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1861
1862     case REAL_TYPE:
1863     case FIXED_POINT_TYPE:
1864     case COMPLEX_TYPE:
1865     case VECTOR_TYPE:
1866     case VOID_TYPE:
1867       return TREE_CODE (type) == TREE_CODE (orig);
1868
1869     default:
1870       return false;
1871     }
1872 }
1873
1874 /* Convert expression ARG to type TYPE.  Used by the middle-end for
1875    simple conversions in preference to calling the front-end's convert.  */
1876
1877 tree
1878 fold_convert_loc (location_t loc, tree type, tree arg)
1879 {
1880   tree orig = TREE_TYPE (arg);
1881   tree tem;
1882
1883   if (type == orig)
1884     return arg;
1885
1886   if (TREE_CODE (arg) == ERROR_MARK
1887       || TREE_CODE (type) == ERROR_MARK
1888       || TREE_CODE (orig) == ERROR_MARK)
1889     return error_mark_node;
1890
1891   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1892     return fold_build1_loc (loc, NOP_EXPR, type, arg);
1893
1894   switch (TREE_CODE (type))
1895     {
1896     case POINTER_TYPE:
1897     case REFERENCE_TYPE:
1898       /* Handle conversions between pointers to different address spaces.  */
1899       if (POINTER_TYPE_P (orig)
1900           && (TYPE_ADDR_SPACE (TREE_TYPE (type))
1901               != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
1902         return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
1903       /* fall through */
1904
1905     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1906     case OFFSET_TYPE:
1907       if (TREE_CODE (arg) == INTEGER_CST)
1908         {
1909           tem = fold_convert_const (NOP_EXPR, type, arg);
1910           if (tem != NULL_TREE)
1911             return tem;
1912         }
1913       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1914           || TREE_CODE (orig) == OFFSET_TYPE)
1915         return fold_build1_loc (loc, NOP_EXPR, type, arg);
1916       if (TREE_CODE (orig) == COMPLEX_TYPE)
1917         return fold_convert_loc (loc, type,
1918                              fold_build1_loc (loc, REALPART_EXPR,
1919                                           TREE_TYPE (orig), arg));
1920       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
1921                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1922       return fold_build1_loc (loc, NOP_EXPR, type, arg);
1923
1924     case REAL_TYPE:
1925       if (TREE_CODE (arg) == INTEGER_CST)
1926         {
1927           tem = fold_convert_const (FLOAT_EXPR, type, arg);
1928           if (tem != NULL_TREE)
1929             return tem;
1930         }
1931       else if (TREE_CODE (arg) == REAL_CST)
1932         {
1933           tem = fold_convert_const (NOP_EXPR, type, arg);
1934           if (tem != NULL_TREE)
1935             return tem;
1936         }
1937       else if (TREE_CODE (arg) == FIXED_CST)
1938         {
1939           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1940           if (tem != NULL_TREE)
1941             return tem;
1942         }
1943
1944       switch (TREE_CODE (orig))
1945         {
1946         case INTEGER_TYPE:
1947         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1948         case POINTER_TYPE: case REFERENCE_TYPE:
1949           return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
1950
1951         case REAL_TYPE:
1952           return fold_build1_loc (loc, NOP_EXPR, type, arg);
1953
1954         case FIXED_POINT_TYPE:
1955           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1956
1957         case COMPLEX_TYPE:
1958           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1959           return fold_convert_loc (loc, type, tem);
1960
1961         default:
1962           gcc_unreachable ();
1963         }
1964
1965     case FIXED_POINT_TYPE:
1966       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
1967           || TREE_CODE (arg) == REAL_CST)
1968         {
1969           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1970           if (tem != NULL_TREE)
1971             goto fold_convert_exit;
1972         }
1973
1974       switch (TREE_CODE (orig))
1975         {
1976         case FIXED_POINT_TYPE:
1977         case INTEGER_TYPE:
1978         case ENUMERAL_TYPE:
1979         case BOOLEAN_TYPE:
1980         case REAL_TYPE:
1981           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1982
1983         case COMPLEX_TYPE:
1984           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1985           return fold_convert_loc (loc, type, tem);
1986
1987         default:
1988           gcc_unreachable ();
1989         }
1990
1991     case COMPLEX_TYPE:
1992       switch (TREE_CODE (orig))
1993         {
1994         case INTEGER_TYPE:
1995         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1996         case POINTER_TYPE: case REFERENCE_TYPE:
1997         case REAL_TYPE:
1998         case FIXED_POINT_TYPE:
1999           return fold_build2_loc (loc, COMPLEX_EXPR, type,
2000                               fold_convert_loc (loc, TREE_TYPE (type), arg),
2001                               fold_convert_loc (loc, TREE_TYPE (type),
2002                                             integer_zero_node));
2003         case COMPLEX_TYPE:
2004           {
2005             tree rpart, ipart;
2006
2007             if (TREE_CODE (arg) == COMPLEX_EXPR)
2008               {
2009                 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2010                                       TREE_OPERAND (arg, 0));
2011                 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2012                                       TREE_OPERAND (arg, 1));
2013                 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2014               }
2015
2016             arg = save_expr (arg);
2017             rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2018             ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2019             rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2020             ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2021             return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2022           }
2023
2024         default:
2025           gcc_unreachable ();
2026         }
2027
2028     case VECTOR_TYPE:
2029       if (integer_zerop (arg))
2030         return build_zero_vector (type);
2031       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2032       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2033                   || TREE_CODE (orig) == VECTOR_TYPE);
2034       return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2035
2036     case VOID_TYPE:
2037       tem = fold_ignored_result (arg);
2038       if (TREE_CODE (tem) == MODIFY_EXPR)
2039         goto fold_convert_exit;
2040       return fold_build1_loc (loc, NOP_EXPR, type, tem);
2041
2042     default:
2043       gcc_unreachable ();
2044     }
2045  fold_convert_exit:
2046   protected_set_expr_location (tem, loc);
2047   return tem;
2048 }
2049 \f
2050 /* Return false if expr can be assumed not to be an lvalue, true
2051    otherwise.  */
2052
2053 static bool
2054 maybe_lvalue_p (const_tree x)
2055 {
2056   /* We only need to wrap lvalue tree codes.  */
2057   switch (TREE_CODE (x))
2058   {
2059   case VAR_DECL:
2060   case PARM_DECL:
2061   case RESULT_DECL:
2062   case LABEL_DECL:
2063   case FUNCTION_DECL:
2064   case SSA_NAME:
2065
2066   case COMPONENT_REF:
2067   case INDIRECT_REF:
2068   case ALIGN_INDIRECT_REF:
2069   case MISALIGNED_INDIRECT_REF:
2070   case ARRAY_REF:
2071   case ARRAY_RANGE_REF:
2072   case BIT_FIELD_REF:
2073   case OBJ_TYPE_REF:
2074
2075   case REALPART_EXPR:
2076   case IMAGPART_EXPR:
2077   case PREINCREMENT_EXPR:
2078   case PREDECREMENT_EXPR:
2079   case SAVE_EXPR:
2080   case TRY_CATCH_EXPR:
2081   case WITH_CLEANUP_EXPR:
2082   case COMPOUND_EXPR:
2083   case MODIFY_EXPR:
2084   case TARGET_EXPR:
2085   case COND_EXPR:
2086   case BIND_EXPR:
2087     break;
2088
2089   default:
2090     /* Assume the worst for front-end tree codes.  */
2091     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2092       break;
2093     return false;
2094   }
2095
2096   return true;
2097 }
2098
2099 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2100
2101 tree
2102 non_lvalue_loc (location_t loc, tree x)
2103 {
2104   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2105      us.  */
2106   if (in_gimple_form)
2107     return x;
2108
2109   if (! maybe_lvalue_p (x))
2110     return x;
2111   x = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2112   SET_EXPR_LOCATION (x, loc);
2113   return x;
2114 }
2115
2116 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2117    Zero means allow extended lvalues.  */
2118
2119 int pedantic_lvalues;
2120
2121 /* When pedantic, return an expr equal to X but certainly not valid as a
2122    pedantic lvalue.  Otherwise, return X.  */
2123
2124 static tree
2125 pedantic_non_lvalue_loc (location_t loc, tree x)
2126 {
2127   if (pedantic_lvalues)
2128     return non_lvalue_loc (loc, x);
2129   protected_set_expr_location (x, loc);
2130   return x;
2131 }
2132 \f
2133 /* Given a tree comparison code, return the code that is the logical inverse
2134    of the given code.  It is not safe to do this for floating-point
2135    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2136    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2137
2138 enum tree_code
2139 invert_tree_comparison (enum tree_code code, bool honor_nans)
2140 {
2141   if (honor_nans && flag_trapping_math)
2142     return ERROR_MARK;
2143
2144   switch (code)
2145     {
2146     case EQ_EXPR:
2147       return NE_EXPR;
2148     case NE_EXPR:
2149       return EQ_EXPR;
2150     case GT_EXPR:
2151       return honor_nans ? UNLE_EXPR : LE_EXPR;
2152     case GE_EXPR:
2153       return honor_nans ? UNLT_EXPR : LT_EXPR;
2154     case LT_EXPR:
2155       return honor_nans ? UNGE_EXPR : GE_EXPR;
2156     case LE_EXPR:
2157       return honor_nans ? UNGT_EXPR : GT_EXPR;
2158     case LTGT_EXPR:
2159       return UNEQ_EXPR;
2160     case UNEQ_EXPR:
2161       return LTGT_EXPR;
2162     case UNGT_EXPR:
2163       return LE_EXPR;
2164     case UNGE_EXPR:
2165       return LT_EXPR;
2166     case UNLT_EXPR:
2167       return GE_EXPR;
2168     case UNLE_EXPR:
2169       return GT_EXPR;
2170     case ORDERED_EXPR:
2171       return UNORDERED_EXPR;
2172     case UNORDERED_EXPR:
2173       return ORDERED_EXPR;
2174     default:
2175       gcc_unreachable ();
2176     }
2177 }
2178
2179 /* Similar, but return the comparison that results if the operands are
2180    swapped.  This is safe for floating-point.  */
2181
2182 enum tree_code
2183 swap_tree_comparison (enum tree_code code)
2184 {
2185   switch (code)
2186     {
2187     case EQ_EXPR:
2188     case NE_EXPR:
2189     case ORDERED_EXPR:
2190     case UNORDERED_EXPR:
2191     case LTGT_EXPR:
2192     case UNEQ_EXPR:
2193       return code;
2194     case GT_EXPR:
2195       return LT_EXPR;
2196     case GE_EXPR:
2197       return LE_EXPR;
2198     case LT_EXPR:
2199       return GT_EXPR;
2200     case LE_EXPR:
2201       return GE_EXPR;
2202     case UNGT_EXPR:
2203       return UNLT_EXPR;
2204     case UNGE_EXPR:
2205       return UNLE_EXPR;
2206     case UNLT_EXPR:
2207       return UNGT_EXPR;
2208     case UNLE_EXPR:
2209       return UNGE_EXPR;
2210     default:
2211       gcc_unreachable ();
2212     }
2213 }
2214
2215
2216 /* Convert a comparison tree code from an enum tree_code representation
2217    into a compcode bit-based encoding.  This function is the inverse of
2218    compcode_to_comparison.  */
2219
2220 static enum comparison_code
2221 comparison_to_compcode (enum tree_code code)
2222 {
2223   switch (code)
2224     {
2225     case LT_EXPR:
2226       return COMPCODE_LT;
2227     case EQ_EXPR:
2228       return COMPCODE_EQ;
2229     case LE_EXPR:
2230       return COMPCODE_LE;
2231     case GT_EXPR:
2232       return COMPCODE_GT;
2233     case NE_EXPR:
2234       return COMPCODE_NE;
2235     case GE_EXPR:
2236       return COMPCODE_GE;
2237     case ORDERED_EXPR:
2238       return COMPCODE_ORD;
2239     case UNORDERED_EXPR:
2240       return COMPCODE_UNORD;
2241     case UNLT_EXPR:
2242       return COMPCODE_UNLT;
2243     case UNEQ_EXPR:
2244       return COMPCODE_UNEQ;
2245     case UNLE_EXPR:
2246       return COMPCODE_UNLE;
2247     case UNGT_EXPR:
2248       return COMPCODE_UNGT;
2249     case LTGT_EXPR:
2250       return COMPCODE_LTGT;
2251     case UNGE_EXPR:
2252       return COMPCODE_UNGE;
2253     default:
2254       gcc_unreachable ();
2255     }
2256 }
2257
2258 /* Convert a compcode bit-based encoding of a comparison operator back
2259    to GCC's enum tree_code representation.  This function is the
2260    inverse of comparison_to_compcode.  */
2261
2262 static enum tree_code
2263 compcode_to_comparison (enum comparison_code code)
2264 {
2265   switch (code)
2266     {
2267     case COMPCODE_LT:
2268       return LT_EXPR;
2269     case COMPCODE_EQ:
2270       return EQ_EXPR;
2271     case COMPCODE_LE:
2272       return LE_EXPR;
2273     case COMPCODE_GT:
2274       return GT_EXPR;
2275     case COMPCODE_NE:
2276       return NE_EXPR;
2277     case COMPCODE_GE:
2278       return GE_EXPR;
2279     case COMPCODE_ORD:
2280       return ORDERED_EXPR;
2281     case COMPCODE_UNORD:
2282       return UNORDERED_EXPR;
2283     case COMPCODE_UNLT:
2284       return UNLT_EXPR;
2285     case COMPCODE_UNEQ:
2286       return UNEQ_EXPR;
2287     case COMPCODE_UNLE:
2288       return UNLE_EXPR;
2289     case COMPCODE_UNGT:
2290       return UNGT_EXPR;
2291     case COMPCODE_LTGT:
2292       return LTGT_EXPR;
2293     case COMPCODE_UNGE:
2294       return UNGE_EXPR;
2295     default:
2296       gcc_unreachable ();
2297     }
2298 }
2299
2300 /* Return a tree for the comparison which is the combination of
2301    doing the AND or OR (depending on CODE) of the two operations LCODE
2302    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2303    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2304    if this makes the transformation invalid.  */
2305
2306 tree
2307 combine_comparisons (location_t loc,
2308                      enum tree_code code, enum tree_code lcode,
2309                      enum tree_code rcode, tree truth_type,
2310                      tree ll_arg, tree lr_arg)
2311 {
2312   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2313   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2314   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2315   int compcode;
2316
2317   switch (code)
2318     {
2319     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2320       compcode = lcompcode & rcompcode;
2321       break;
2322
2323     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2324       compcode = lcompcode | rcompcode;
2325       break;
2326
2327     default:
2328       return NULL_TREE;
2329     }
2330
2331   if (!honor_nans)
2332     {
2333       /* Eliminate unordered comparisons, as well as LTGT and ORD
2334          which are not used unless the mode has NaNs.  */
2335       compcode &= ~COMPCODE_UNORD;
2336       if (compcode == COMPCODE_LTGT)
2337         compcode = COMPCODE_NE;
2338       else if (compcode == COMPCODE_ORD)
2339         compcode = COMPCODE_TRUE;
2340     }
2341    else if (flag_trapping_math)
2342      {
2343         /* Check that the original operation and the optimized ones will trap
2344            under the same condition.  */
2345         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2346                      && (lcompcode != COMPCODE_EQ)
2347                      && (lcompcode != COMPCODE_ORD);
2348         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2349                      && (rcompcode != COMPCODE_EQ)
2350                      && (rcompcode != COMPCODE_ORD);
2351         bool trap = (compcode & COMPCODE_UNORD) == 0
2352                     && (compcode != COMPCODE_EQ)
2353                     && (compcode != COMPCODE_ORD);
2354
2355         /* In a short-circuited boolean expression the LHS might be
2356            such that the RHS, if evaluated, will never trap.  For
2357            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2358            if neither x nor y is NaN.  (This is a mixed blessing: for
2359            example, the expression above will never trap, hence
2360            optimizing it to x < y would be invalid).  */
2361         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2362             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2363           rtrap = false;
2364
2365         /* If the comparison was short-circuited, and only the RHS
2366            trapped, we may now generate a spurious trap.  */
2367         if (rtrap && !ltrap
2368             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2369           return NULL_TREE;
2370
2371         /* If we changed the conditions that cause a trap, we lose.  */
2372         if ((ltrap || rtrap) != trap)
2373           return NULL_TREE;
2374       }
2375
2376   if (compcode == COMPCODE_TRUE)
2377     return constant_boolean_node (true, truth_type);
2378   else if (compcode == COMPCODE_FALSE)
2379     return constant_boolean_node (false, truth_type);
2380   else
2381     {
2382       enum tree_code tcode;
2383
2384       tcode = compcode_to_comparison ((enum comparison_code) compcode);
2385       return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2386     }
2387 }
2388 \f
2389 /* Return nonzero if two operands (typically of the same tree node)
2390    are necessarily equal.  If either argument has side-effects this
2391    function returns zero.  FLAGS modifies behavior as follows:
2392
2393    If OEP_ONLY_CONST is set, only return nonzero for constants.
2394    This function tests whether the operands are indistinguishable;
2395    it does not test whether they are equal using C's == operation.
2396    The distinction is important for IEEE floating point, because
2397    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2398    (2) two NaNs may be indistinguishable, but NaN!=NaN.
2399
2400    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2401    even though it may hold multiple values during a function.
2402    This is because a GCC tree node guarantees that nothing else is
2403    executed between the evaluation of its "operands" (which may often
2404    be evaluated in arbitrary order).  Hence if the operands themselves
2405    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2406    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
2407    unset means assuming isochronic (or instantaneous) tree equivalence.
2408    Unless comparing arbitrary expression trees, such as from different
2409    statements, this flag can usually be left unset.
2410
2411    If OEP_PURE_SAME is set, then pure functions with identical arguments
2412    are considered the same.  It is used when the caller has other ways
2413    to ensure that global memory is unchanged in between.  */
2414
2415 int
2416 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2417 {
2418   /* If either is ERROR_MARK, they aren't equal.  */
2419   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2420       || TREE_TYPE (arg0) == error_mark_node
2421       || TREE_TYPE (arg1) == error_mark_node)
2422     return 0;
2423
2424   /* Similar, if either does not have a type (like a released SSA name), 
2425      they aren't equal.  */
2426   if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2427     return 0;
2428
2429   /* Check equality of integer constants before bailing out due to
2430      precision differences.  */
2431   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2432     return tree_int_cst_equal (arg0, arg1);
2433
2434   /* If both types don't have the same signedness, then we can't consider
2435      them equal.  We must check this before the STRIP_NOPS calls
2436      because they may change the signedness of the arguments.  As pointers
2437      strictly don't have a signedness, require either two pointers or
2438      two non-pointers as well.  */
2439   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2440       || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2441     return 0;
2442
2443   /* We cannot consider pointers to different address space equal.  */
2444   if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2445       && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2446           != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2447     return 0;
2448
2449   /* If both types don't have the same precision, then it is not safe
2450      to strip NOPs.  */
2451   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
2452     return 0;
2453
2454   STRIP_NOPS (arg0);
2455   STRIP_NOPS (arg1);
2456
2457   /* In case both args are comparisons but with different comparison
2458      code, try to swap the comparison operands of one arg to produce
2459      a match and compare that variant.  */
2460   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2461       && COMPARISON_CLASS_P (arg0)
2462       && COMPARISON_CLASS_P (arg1))
2463     {
2464       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2465
2466       if (TREE_CODE (arg0) == swap_code)
2467         return operand_equal_p (TREE_OPERAND (arg0, 0),
2468                                 TREE_OPERAND (arg1, 1), flags)
2469                && operand_equal_p (TREE_OPERAND (arg0, 1),
2470                                    TREE_OPERAND (arg1, 0), flags);
2471     }
2472
2473   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2474       /* This is needed for conversions and for COMPONENT_REF.
2475          Might as well play it safe and always test this.  */
2476       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2477       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2478       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2479     return 0;
2480
2481   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2482      We don't care about side effects in that case because the SAVE_EXPR
2483      takes care of that for us. In all other cases, two expressions are
2484      equal if they have no side effects.  If we have two identical
2485      expressions with side effects that should be treated the same due
2486      to the only side effects being identical SAVE_EXPR's, that will
2487      be detected in the recursive calls below.  */
2488   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2489       && (TREE_CODE (arg0) == SAVE_EXPR
2490           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2491     return 1;
2492
2493   /* Next handle constant cases, those for which we can return 1 even
2494      if ONLY_CONST is set.  */
2495   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2496     switch (TREE_CODE (arg0))
2497       {
2498       case INTEGER_CST:
2499         return tree_int_cst_equal (arg0, arg1);
2500
2501       case FIXED_CST:
2502         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2503                                        TREE_FIXED_CST (arg1));
2504
2505       case REAL_CST:
2506         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2507                                    TREE_REAL_CST (arg1)))
2508           return 1;
2509
2510
2511         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
2512           {
2513             /* If we do not distinguish between signed and unsigned zero,
2514                consider them equal.  */
2515             if (real_zerop (arg0) && real_zerop (arg1))
2516               return 1;
2517           }
2518         return 0;
2519
2520       case VECTOR_CST:
2521         {
2522           tree v1, v2;
2523
2524           v1 = TREE_VECTOR_CST_ELTS (arg0);
2525           v2 = TREE_VECTOR_CST_ELTS (arg1);
2526           while (v1 && v2)
2527             {
2528               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2529                                     flags))
2530                 return 0;
2531               v1 = TREE_CHAIN (v1);
2532               v2 = TREE_CHAIN (v2);
2533             }
2534
2535           return v1 == v2;
2536         }
2537
2538       case COMPLEX_CST:
2539         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2540                                  flags)
2541                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2542                                     flags));
2543
2544       case STRING_CST:
2545         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2546                 && ! memcmp (TREE_STRING_POINTER (arg0),
2547                               TREE_STRING_POINTER (arg1),
2548                               TREE_STRING_LENGTH (arg0)));
2549
2550       case ADDR_EXPR:
2551         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2552                                 0);
2553       default:
2554         break;
2555       }
2556
2557   if (flags & OEP_ONLY_CONST)
2558     return 0;
2559
2560 /* Define macros to test an operand from arg0 and arg1 for equality and a
2561    variant that allows null and views null as being different from any
2562    non-null value.  In the latter case, if either is null, the both
2563    must be; otherwise, do the normal comparison.  */
2564 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
2565                                     TREE_OPERAND (arg1, N), flags)
2566
2567 #define OP_SAME_WITH_NULL(N)                            \
2568   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2569    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2570
2571   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2572     {
2573     case tcc_unary:
2574       /* Two conversions are equal only if signedness and modes match.  */
2575       switch (TREE_CODE (arg0))
2576         {
2577         CASE_CONVERT:
2578         case FIX_TRUNC_EXPR:
2579           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2580               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2581             return 0;
2582           break;
2583         default:
2584           break;
2585         }
2586
2587       return OP_SAME (0);
2588
2589
2590     case tcc_comparison:
2591     case tcc_binary:
2592       if (OP_SAME (0) && OP_SAME (1))
2593         return 1;
2594
2595       /* For commutative ops, allow the other order.  */
2596       return (commutative_tree_code (TREE_CODE (arg0))
2597               && operand_equal_p (TREE_OPERAND (arg0, 0),
2598                                   TREE_OPERAND (arg1, 1), flags)
2599               && operand_equal_p (TREE_OPERAND (arg0, 1),
2600                                   TREE_OPERAND (arg1, 0), flags));
2601
2602     case tcc_reference:
2603       /* If either of the pointer (or reference) expressions we are
2604          dereferencing contain a side effect, these cannot be equal.  */
2605       if (TREE_SIDE_EFFECTS (arg0)
2606           || TREE_SIDE_EFFECTS (arg1))
2607         return 0;
2608
2609       switch (TREE_CODE (arg0))
2610         {
2611         case INDIRECT_REF:
2612         case ALIGN_INDIRECT_REF:
2613         case MISALIGNED_INDIRECT_REF:
2614         case REALPART_EXPR:
2615         case IMAGPART_EXPR:
2616           return OP_SAME (0);
2617
2618         case ARRAY_REF:
2619         case ARRAY_RANGE_REF:
2620           /* Operands 2 and 3 may be null.
2621              Compare the array index by value if it is constant first as we
2622              may have different types but same value here.  */
2623           return (OP_SAME (0)
2624                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2625                                           TREE_OPERAND (arg1, 1))
2626                       || OP_SAME (1))
2627                   && OP_SAME_WITH_NULL (2)
2628                   && OP_SAME_WITH_NULL (3));
2629
2630         case COMPONENT_REF:
2631           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
2632              may be NULL when we're called to compare MEM_EXPRs.  */
2633           return OP_SAME_WITH_NULL (0)
2634                  && OP_SAME (1)
2635                  && OP_SAME_WITH_NULL (2);
2636
2637         case BIT_FIELD_REF:
2638           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2639
2640         default:
2641           return 0;
2642         }
2643
2644     case tcc_expression:
2645       switch (TREE_CODE (arg0))
2646         {
2647         case ADDR_EXPR:
2648         case TRUTH_NOT_EXPR:
2649           return OP_SAME (0);
2650
2651         case TRUTH_ANDIF_EXPR:
2652         case TRUTH_ORIF_EXPR:
2653           return OP_SAME (0) && OP_SAME (1);
2654
2655         case TRUTH_AND_EXPR:
2656         case TRUTH_OR_EXPR:
2657         case TRUTH_XOR_EXPR:
2658           if (OP_SAME (0) && OP_SAME (1))
2659             return 1;
2660
2661           /* Otherwise take into account this is a commutative operation.  */
2662           return (operand_equal_p (TREE_OPERAND (arg0, 0),
2663                                    TREE_OPERAND (arg1, 1), flags)
2664                   && operand_equal_p (TREE_OPERAND (arg0, 1),
2665                                       TREE_OPERAND (arg1, 0), flags));
2666
2667         case COND_EXPR:
2668           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2669
2670         default:
2671           return 0;
2672         }
2673
2674     case tcc_vl_exp:
2675       switch (TREE_CODE (arg0))
2676         {
2677         case CALL_EXPR:
2678           /* If the CALL_EXPRs call different functions, then they
2679              clearly can not be equal.  */
2680           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
2681                                  flags))
2682             return 0;
2683
2684           {
2685             unsigned int cef = call_expr_flags (arg0);
2686             if (flags & OEP_PURE_SAME)
2687               cef &= ECF_CONST | ECF_PURE;
2688             else
2689               cef &= ECF_CONST;
2690             if (!cef)
2691               return 0;
2692           }
2693
2694           /* Now see if all the arguments are the same.  */
2695           {
2696             const_call_expr_arg_iterator iter0, iter1;
2697             const_tree a0, a1;
2698             for (a0 = first_const_call_expr_arg (arg0, &iter0),
2699                    a1 = first_const_call_expr_arg (arg1, &iter1);
2700                  a0 && a1;
2701                  a0 = next_const_call_expr_arg (&iter0),
2702                    a1 = next_const_call_expr_arg (&iter1))
2703               if (! operand_equal_p (a0, a1, flags))
2704                 return 0;
2705
2706             /* If we get here and both argument lists are exhausted
2707                then the CALL_EXPRs are equal.  */
2708             return ! (a0 || a1);
2709           }
2710         default:
2711           return 0;
2712         }
2713
2714     case tcc_declaration:
2715       /* Consider __builtin_sqrt equal to sqrt.  */
2716       return (TREE_CODE (arg0) == FUNCTION_DECL
2717               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2718               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2719               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2720
2721     default:
2722       return 0;
2723     }
2724
2725 #undef OP_SAME
2726 #undef OP_SAME_WITH_NULL
2727 }
2728 \f
2729 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2730    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2731
2732    When in doubt, return 0.  */
2733
2734 static int
2735 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2736 {
2737   int unsignedp1, unsignedpo;
2738   tree primarg0, primarg1, primother;
2739   unsigned int correct_width;
2740
2741   if (operand_equal_p (arg0, arg1, 0))
2742     return 1;
2743
2744   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2745       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2746     return 0;
2747
2748   /* Discard any conversions that don't change the modes of ARG0 and ARG1
2749      and see if the inner values are the same.  This removes any
2750      signedness comparison, which doesn't matter here.  */
2751   primarg0 = arg0, primarg1 = arg1;
2752   STRIP_NOPS (primarg0);
2753   STRIP_NOPS (primarg1);
2754   if (operand_equal_p (primarg0, primarg1, 0))
2755     return 1;
2756
2757   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2758      actual comparison operand, ARG0.
2759
2760      First throw away any conversions to wider types
2761      already present in the operands.  */
2762
2763   primarg1 = get_narrower (arg1, &unsignedp1);
2764   primother = get_narrower (other, &unsignedpo);
2765
2766   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2767   if (unsignedp1 == unsignedpo
2768       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2769       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2770     {
2771       tree type = TREE_TYPE (arg0);
2772
2773       /* Make sure shorter operand is extended the right way
2774          to match the longer operand.  */
2775       primarg1 = fold_convert (signed_or_unsigned_type_for
2776                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2777
2778       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2779         return 1;
2780     }
2781
2782   return 0;
2783 }
2784 \f
2785 /* See if ARG is an expression that is either a comparison or is performing
2786    arithmetic on comparisons.  The comparisons must only be comparing
2787    two different values, which will be stored in *CVAL1 and *CVAL2; if
2788    they are nonzero it means that some operands have already been found.
2789    No variables may be used anywhere else in the expression except in the
2790    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
2791    the expression and save_expr needs to be called with CVAL1 and CVAL2.
2792
2793    If this is true, return 1.  Otherwise, return zero.  */
2794
2795 static int
2796 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2797 {
2798   enum tree_code code = TREE_CODE (arg);
2799   enum tree_code_class tclass = TREE_CODE_CLASS (code);
2800
2801   /* We can handle some of the tcc_expression cases here.  */
2802   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2803     tclass = tcc_unary;
2804   else if (tclass == tcc_expression
2805            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2806                || code == COMPOUND_EXPR))
2807     tclass = tcc_binary;
2808
2809   else if (tclass == tcc_expression && code == SAVE_EXPR
2810            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2811     {
2812       /* If we've already found a CVAL1 or CVAL2, this expression is
2813          two complex to handle.  */
2814       if (*cval1 || *cval2)
2815         return 0;
2816
2817       tclass = tcc_unary;
2818       *save_p = 1;
2819     }
2820
2821   switch (tclass)
2822     {
2823     case tcc_unary:
2824       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2825
2826     case tcc_binary:
2827       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2828               && twoval_comparison_p (TREE_OPERAND (arg, 1),
2829                                       cval1, cval2, save_p));
2830
2831     case tcc_constant:
2832       return 1;
2833
2834     case tcc_expression:
2835       if (code == COND_EXPR)
2836         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2837                                      cval1, cval2, save_p)
2838                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2839                                         cval1, cval2, save_p)
2840                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2841                                         cval1, cval2, save_p));
2842       return 0;
2843
2844     case tcc_comparison:
2845       /* First see if we can handle the first operand, then the second.  For
2846          the second operand, we know *CVAL1 can't be zero.  It must be that
2847          one side of the comparison is each of the values; test for the
2848          case where this isn't true by failing if the two operands
2849          are the same.  */
2850
2851       if (operand_equal_p (TREE_OPERAND (arg, 0),
2852                            TREE_OPERAND (arg, 1), 0))
2853         return 0;
2854
2855       if (*cval1 == 0)
2856         *cval1 = TREE_OPERAND (arg, 0);
2857       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2858         ;
2859       else if (*cval2 == 0)
2860         *cval2 = TREE_OPERAND (arg, 0);
2861       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2862         ;
2863       else
2864         return 0;
2865
2866       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2867         ;
2868       else if (*cval2 == 0)
2869         *cval2 = TREE_OPERAND (arg, 1);
2870       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2871         ;
2872       else
2873         return 0;
2874
2875       return 1;
2876
2877     default:
2878       return 0;
2879     }
2880 }
2881 \f
2882 /* ARG is a tree that is known to contain just arithmetic operations and
2883    comparisons.  Evaluate the operations in the tree substituting NEW0 for
2884    any occurrence of OLD0 as an operand of a comparison and likewise for
2885    NEW1 and OLD1.  */
2886
2887 static tree
2888 eval_subst (location_t loc, tree arg, tree old0, tree new0,
2889             tree old1, tree new1)
2890 {
2891   tree type = TREE_TYPE (arg);
2892   enum tree_code code = TREE_CODE (arg);
2893   enum tree_code_class tclass = TREE_CODE_CLASS (code);
2894
2895   /* We can handle some of the tcc_expression cases here.  */
2896   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2897     tclass = tcc_unary;
2898   else if (tclass == tcc_expression
2899            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2900     tclass = tcc_binary;
2901
2902   switch (tclass)
2903     {
2904     case tcc_unary:
2905       return fold_build1_loc (loc, code, type,
2906                           eval_subst (loc, TREE_OPERAND (arg, 0),
2907                                       old0, new0, old1, new1));
2908
2909     case tcc_binary:
2910       return fold_build2_loc (loc, code, type,
2911                           eval_subst (loc, TREE_OPERAND (arg, 0),
2912                                       old0, new0, old1, new1),
2913                           eval_subst (loc, TREE_OPERAND (arg, 1),
2914                                       old0, new0, old1, new1));
2915
2916     case tcc_expression:
2917       switch (code)
2918         {
2919         case SAVE_EXPR:
2920           return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
2921                              old1, new1);
2922
2923         case COMPOUND_EXPR:
2924           return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
2925                              old1, new1);
2926
2927         case COND_EXPR:
2928           return fold_build3_loc (loc, code, type,
2929                               eval_subst (loc, TREE_OPERAND (arg, 0),
2930                                           old0, new0, old1, new1),
2931                               eval_subst (loc, TREE_OPERAND (arg, 1),
2932                                           old0, new0, old1, new1),
2933                               eval_subst (loc, TREE_OPERAND (arg, 2),
2934                                           old0, new0, old1, new1));
2935         default:
2936           break;
2937         }
2938       /* Fall through - ???  */
2939
2940     case tcc_comparison:
2941       {
2942         tree arg0 = TREE_OPERAND (arg, 0);
2943         tree arg1 = TREE_OPERAND (arg, 1);
2944
2945         /* We need to check both for exact equality and tree equality.  The
2946            former will be true if the operand has a side-effect.  In that
2947            case, we know the operand occurred exactly once.  */
2948
2949         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2950           arg0 = new0;
2951         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2952           arg0 = new1;
2953
2954         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2955           arg1 = new0;
2956         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2957           arg1 = new1;
2958
2959         return fold_build2_loc (loc, code, type, arg0, arg1);
2960       }
2961
2962     default:
2963       return arg;
2964     }
2965 }
2966 \f
2967 /* Return a tree for the case when the result of an expression is RESULT
2968    converted to TYPE and OMITTED was previously an operand of the expression
2969    but is now not needed (e.g., we folded OMITTED * 0).
2970
2971    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
2972    the conversion of RESULT to TYPE.  */
2973
2974 tree
2975 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
2976 {
2977   tree t = fold_convert_loc (loc, type, result);
2978
2979   /* If the resulting operand is an empty statement, just return the omitted
2980      statement casted to void. */
2981   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
2982     {
2983       t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
2984       goto omit_one_operand_exit;
2985     }
2986
2987   if (TREE_SIDE_EFFECTS (omitted))
2988     {
2989       t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
2990       goto omit_one_operand_exit;
2991     }
2992
2993   return non_lvalue_loc (loc, t);
2994
2995  omit_one_operand_exit:
2996   protected_set_expr_location (t, loc);
2997   return t;
2998 }
2999
3000 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
3001
3002 static tree
3003 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
3004                                tree omitted)
3005 {
3006   tree t = fold_convert_loc (loc, type, result);
3007
3008   /* If the resulting operand is an empty statement, just return the omitted
3009      statement casted to void. */
3010   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3011     {
3012       t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3013       goto pedantic_omit_one_operand_exit;
3014     }
3015
3016   if (TREE_SIDE_EFFECTS (omitted))
3017     {
3018       t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3019       goto pedantic_omit_one_operand_exit;
3020     }
3021
3022   return pedantic_non_lvalue_loc (loc, t);
3023
3024  pedantic_omit_one_operand_exit:
3025   protected_set_expr_location (t, loc);
3026   return t;
3027 }
3028
3029 /* Return a tree for the case when the result of an expression is RESULT
3030    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3031    of the expression but are now not needed.
3032
3033    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3034    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3035    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3036    just do the conversion of RESULT to TYPE.  */
3037
3038 tree
3039 omit_two_operands_loc (location_t loc, tree type, tree result,
3040                    tree omitted1, tree omitted2)
3041 {
3042   tree t = fold_convert_loc (loc, type, result);
3043
3044   if (TREE_SIDE_EFFECTS (omitted2))
3045     {
3046       t = build2 (COMPOUND_EXPR, type, omitted2, t);
3047       SET_EXPR_LOCATION (t, loc);
3048     }
3049   if (TREE_SIDE_EFFECTS (omitted1))
3050     {
3051       t = build2 (COMPOUND_EXPR, type, omitted1, t);
3052       SET_EXPR_LOCATION (t, loc);
3053     }
3054
3055   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3056 }
3057
3058 \f
3059 /* Return a simplified tree node for the truth-negation of ARG.  This
3060    never alters ARG itself.  We assume that ARG is an operation that
3061    returns a truth value (0 or 1).
3062
3063    FIXME: one would think we would fold the result, but it causes
3064    problems with the dominator optimizer.  */
3065
3066 tree
3067 fold_truth_not_expr (location_t loc, tree arg)
3068 {
3069   tree t, type = TREE_TYPE (arg);
3070   enum tree_code code = TREE_CODE (arg);
3071   location_t loc1, loc2;
3072
3073   /* If this is a comparison, we can simply invert it, except for
3074      floating-point non-equality comparisons, in which case we just
3075      enclose a TRUTH_NOT_EXPR around what we have.  */
3076
3077   if (TREE_CODE_CLASS (code) == tcc_comparison)
3078     {
3079       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3080       if (FLOAT_TYPE_P (op_type)
3081           && flag_trapping_math
3082           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3083           && code != NE_EXPR && code != EQ_EXPR)
3084         return NULL_TREE;
3085
3086       code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3087       if (code == ERROR_MARK)
3088         return NULL_TREE;
3089
3090       t = build2 (code, type, TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3091       SET_EXPR_LOCATION (t, loc);
3092       return t;
3093     }
3094
3095   switch (code)
3096     {
3097     case INTEGER_CST:
3098       return constant_boolean_node (integer_zerop (arg), type);
3099
3100     case TRUTH_AND_EXPR:
3101       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3102       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3103       if (loc1 == UNKNOWN_LOCATION)
3104         loc1 = loc;
3105       if (loc2 == UNKNOWN_LOCATION)
3106         loc2 = loc;
3107       t = build2 (TRUTH_OR_EXPR, type,
3108                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3109                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3110       break;
3111
3112     case TRUTH_OR_EXPR:
3113       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3114       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3115       if (loc1 == UNKNOWN_LOCATION)
3116         loc1 = loc;
3117       if (loc2 == UNKNOWN_LOCATION)
3118         loc2 = loc;
3119       t = build2 (TRUTH_AND_EXPR, type,
3120                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3121                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3122       break;
3123
3124     case TRUTH_XOR_EXPR:
3125       /* Here we can invert either operand.  We invert the first operand
3126          unless the second operand is a TRUTH_NOT_EXPR in which case our
3127          result is the XOR of the first operand with the inside of the
3128          negation of the second operand.  */
3129
3130       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3131         t = build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3132                     TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3133       else
3134         t = build2 (TRUTH_XOR_EXPR, type,
3135                     invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3136                     TREE_OPERAND (arg, 1));
3137       break;
3138
3139     case TRUTH_ANDIF_EXPR:
3140       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3141       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3142       if (loc1 == UNKNOWN_LOCATION)
3143         loc1 = loc;
3144       if (loc2 == UNKNOWN_LOCATION)
3145         loc2 = loc;
3146       t = build2 (TRUTH_ORIF_EXPR, type,
3147                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3148                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3149       break;
3150
3151     case TRUTH_ORIF_EXPR:
3152       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3153       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3154       if (loc1 == UNKNOWN_LOCATION)
3155         loc1 = loc;
3156       if (loc2 == UNKNOWN_LOCATION)
3157         loc2 = loc;
3158       t = build2 (TRUTH_ANDIF_EXPR, type,
3159                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3160                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3161       break;
3162
3163     case TRUTH_NOT_EXPR:
3164       return TREE_OPERAND (arg, 0);
3165
3166     case COND_EXPR:
3167       {
3168         tree arg1 = TREE_OPERAND (arg, 1);
3169         tree arg2 = TREE_OPERAND (arg, 2);
3170
3171         loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3172         loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 2));
3173         if (loc1 == UNKNOWN_LOCATION)
3174           loc1 = loc;
3175         if (loc2 == UNKNOWN_LOCATION)
3176           loc2 = loc;
3177
3178         /* A COND_EXPR may have a throw as one operand, which
3179            then has void type.  Just leave void operands
3180            as they are.  */
3181         t = build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3182                     VOID_TYPE_P (TREE_TYPE (arg1))
3183                     ? arg1 : invert_truthvalue_loc (loc1, arg1),
3184                     VOID_TYPE_P (TREE_TYPE (arg2))
3185                     ? arg2 : invert_truthvalue_loc (loc2, arg2));
3186         break;
3187       }
3188
3189     case COMPOUND_EXPR:
3190       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3191       if (loc1 == UNKNOWN_LOCATION)
3192         loc1 = loc;
3193       t = build2 (COMPOUND_EXPR, type,
3194                   TREE_OPERAND (arg, 0),
3195                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3196       break;
3197
3198     case NON_LVALUE_EXPR:
3199       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3200       if (loc1 == UNKNOWN_LOCATION)
3201         loc1 = loc;
3202       return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3203
3204     CASE_CONVERT:
3205       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3206         {
3207           t = build1 (TRUTH_NOT_EXPR, type, arg);
3208           break;
3209         }
3210
3211       /* ... fall through ...  */
3212
3213     case FLOAT_EXPR:
3214       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3215       if (loc1 == UNKNOWN_LOCATION)
3216         loc1 = loc;
3217       t = build1 (TREE_CODE (arg), type,
3218                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3219       break;
3220
3221     case BIT_AND_EXPR:
3222       if (!integer_onep (TREE_OPERAND (arg, 1)))
3223         return NULL_TREE;
3224       t = build2 (EQ_EXPR, type, arg, build_int_cst (type, 0));
3225       break;
3226
3227     case SAVE_EXPR:
3228       t = build1 (TRUTH_NOT_EXPR, type, arg);
3229       break;
3230
3231     case CLEANUP_POINT_EXPR:
3232       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3233       if (loc1 == UNKNOWN_LOCATION)
3234         loc1 = loc;
3235       t = build1 (CLEANUP_POINT_EXPR, type,
3236                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3237       break;
3238
3239     default:
3240       t = NULL_TREE;
3241       break;
3242     }
3243
3244   if (t)
3245     SET_EXPR_LOCATION (t, loc);
3246
3247   return t;
3248 }
3249
3250 /* Return a simplified tree node for the truth-negation of ARG.  This
3251    never alters ARG itself.  We assume that ARG is an operation that
3252    returns a truth value (0 or 1).
3253
3254    FIXME: one would think we would fold the result, but it causes
3255    problems with the dominator optimizer.  */
3256
3257 tree
3258 invert_truthvalue_loc (location_t loc, tree arg)
3259 {
3260   tree tem;
3261
3262   if (TREE_CODE (arg) == ERROR_MARK)
3263     return arg;
3264
3265   tem = fold_truth_not_expr (loc, arg);
3266   if (!tem)
3267     {
3268       tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3269       SET_EXPR_LOCATION (tem, loc);
3270     }
3271
3272   return tem;
3273 }
3274
3275 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3276    operands are another bit-wise operation with a common input.  If so,
3277    distribute the bit operations to save an operation and possibly two if
3278    constants are involved.  For example, convert
3279         (A | B) & (A | C) into A | (B & C)
3280    Further simplification will occur if B and C are constants.
3281
3282    If this optimization cannot be done, 0 will be returned.  */
3283
3284 static tree
3285 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
3286                      tree arg0, tree arg1)
3287 {
3288   tree common;
3289   tree left, right;
3290
3291   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3292       || TREE_CODE (arg0) == code
3293       || (TREE_CODE (arg0) != BIT_AND_EXPR
3294           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3295     return 0;
3296
3297   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3298     {
3299       common = TREE_OPERAND (arg0, 0);
3300       left = TREE_OPERAND (arg0, 1);
3301       right = TREE_OPERAND (arg1, 1);
3302     }
3303   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3304     {
3305       common = TREE_OPERAND (arg0, 0);
3306       left = TREE_OPERAND (arg0, 1);
3307       right = TREE_OPERAND (arg1, 0);
3308     }
3309   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3310     {
3311       common = TREE_OPERAND (arg0, 1);
3312       left = TREE_OPERAND (arg0, 0);
3313       right = TREE_OPERAND (arg1, 1);
3314     }
3315   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3316     {
3317       common = TREE_OPERAND (arg0, 1);
3318       left = TREE_OPERAND (arg0, 0);
3319       right = TREE_OPERAND (arg1, 0);
3320     }
3321   else
3322     return 0;
3323
3324   common = fold_convert_loc (loc, type, common);
3325   left = fold_convert_loc (loc, type, left);
3326   right = fold_convert_loc (loc, type, right);
3327   return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
3328                       fold_build2_loc (loc, code, type, left, right));
3329 }
3330
3331 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3332    with code CODE.  This optimization is unsafe.  */
3333 static tree
3334 distribute_real_division (location_t loc, enum tree_code code, tree type,
3335                           tree arg0, tree arg1)
3336 {
3337   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3338   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3339
3340   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3341   if (mul0 == mul1
3342       && operand_equal_p (TREE_OPERAND (arg0, 1),
3343                        TREE_OPERAND (arg1, 1), 0))
3344     return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3345                         fold_build2_loc (loc, code, type,
3346                                      TREE_OPERAND (arg0, 0),
3347                                      TREE_OPERAND (arg1, 0)),
3348                         TREE_OPERAND (arg0, 1));
3349
3350   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3351   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3352                        TREE_OPERAND (arg1, 0), 0)
3353       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3354       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3355     {
3356       REAL_VALUE_TYPE r0, r1;
3357       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3358       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3359       if (!mul0)
3360         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3361       if (!mul1)
3362         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3363       real_arithmetic (&r0, code, &r0, &r1);
3364       return fold_build2_loc (loc, MULT_EXPR, type,
3365                           TREE_OPERAND (arg0, 0),
3366                           build_real (type, r0));
3367     }
3368
3369   return NULL_TREE;
3370 }
3371 \f
3372 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3373    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3374
3375 static tree
3376 make_bit_field_ref (location_t loc, tree inner, tree type,
3377                     HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3378 {
3379   tree result, bftype;
3380
3381   if (bitpos == 0)
3382     {
3383       tree size = TYPE_SIZE (TREE_TYPE (inner));
3384       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3385            || POINTER_TYPE_P (TREE_TYPE (inner)))
3386           && host_integerp (size, 0)
3387           && tree_low_cst (size, 0) == bitsize)
3388         return fold_convert_loc (loc, type, inner);
3389     }
3390
3391   bftype = type;
3392   if (TYPE_PRECISION (bftype) != bitsize
3393       || TYPE_UNSIGNED (bftype) == !unsignedp)
3394     bftype = build_nonstandard_integer_type (bitsize, 0);
3395
3396   result = build3 (BIT_FIELD_REF, bftype, inner,
3397                    size_int (bitsize), bitsize_int (bitpos));
3398   SET_EXPR_LOCATION (result, loc);
3399
3400   if (bftype != type)
3401     result = fold_convert_loc (loc, type, result);
3402
3403   return result;
3404 }
3405
3406 /* Optimize a bit-field compare.
3407
3408    There are two cases:  First is a compare against a constant and the
3409    second is a comparison of two items where the fields are at the same
3410    bit position relative to the start of a chunk (byte, halfword, word)
3411    large enough to contain it.  In these cases we can avoid the shift
3412    implicit in bitfield extractions.
3413
3414    For constants, we emit a compare of the shifted constant with the
3415    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3416    compared.  For two fields at the same position, we do the ANDs with the
3417    similar mask and compare the result of the ANDs.
3418
3419    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3420    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3421    are the left and right operands of the comparison, respectively.
3422
3423    If the optimization described above can be done, we return the resulting
3424    tree.  Otherwise we return zero.  */
3425
3426 static tree
3427 optimize_bit_field_compare (location_t loc, enum tree_code code,
3428                             tree compare_type, tree lhs, tree rhs)
3429 {
3430   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3431   tree type = TREE_TYPE (lhs);
3432   tree signed_type, unsigned_type;
3433   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3434   enum machine_mode lmode, rmode, nmode;
3435   int lunsignedp, runsignedp;
3436   int lvolatilep = 0, rvolatilep = 0;
3437   tree linner, rinner = NULL_TREE;
3438   tree mask;
3439   tree offset;
3440
3441   /* Get all the information about the extractions being done.  If the bit size
3442      if the same as the size of the underlying object, we aren't doing an
3443      extraction at all and so can do nothing.  We also don't want to
3444      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3445      then will no longer be able to replace it.  */
3446   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3447                                 &lunsignedp, &lvolatilep, false);
3448   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3449       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3450     return 0;
3451
3452  if (!const_p)
3453    {
3454      /* If this is not a constant, we can only do something if bit positions,
3455         sizes, and signedness are the same.  */
3456      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3457                                    &runsignedp, &rvolatilep, false);
3458
3459      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3460          || lunsignedp != runsignedp || offset != 0
3461          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3462        return 0;
3463    }
3464
3465   /* See if we can find a mode to refer to this field.  We should be able to,
3466      but fail if we can't.  */
3467   nmode = get_best_mode (lbitsize, lbitpos,
3468                          const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3469                          : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3470                                 TYPE_ALIGN (TREE_TYPE (rinner))),
3471                          word_mode, lvolatilep || rvolatilep);
3472   if (nmode == VOIDmode)
3473     return 0;
3474
3475   /* Set signed and unsigned types of the precision of this mode for the
3476      shifts below.  */
3477   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3478   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3479
3480   /* Compute the bit position and size for the new reference and our offset
3481      within it. If the new reference is the same size as the original, we
3482      won't optimize anything, so return zero.  */
3483   nbitsize = GET_MODE_BITSIZE (nmode);
3484   nbitpos = lbitpos & ~ (nbitsize - 1);
3485   lbitpos -= nbitpos;
3486   if (nbitsize == lbitsize)
3487     return 0;
3488
3489   if (BYTES_BIG_ENDIAN)
3490     lbitpos = nbitsize - lbitsize - lbitpos;
3491
3492   /* Make the mask to be used against the extracted field.  */
3493   mask = build_int_cst_type (unsigned_type, -1);
3494   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3495   mask = const_binop (RSHIFT_EXPR, mask,
3496                       size_int (nbitsize - lbitsize - lbitpos), 0);
3497
3498   if (! const_p)
3499     /* If not comparing with constant, just rework the comparison
3500        and return.  */
3501     return fold_build2_loc (loc, code, compare_type,
3502                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3503                                      make_bit_field_ref (loc, linner,
3504                                                          unsigned_type,
3505                                                          nbitsize, nbitpos,
3506                                                          1),
3507                                      mask),
3508                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3509                                      make_bit_field_ref (loc, rinner,
3510                                                          unsigned_type,
3511                                                          nbitsize, nbitpos,
3512                                                          1),
3513                                      mask));
3514
3515   /* Otherwise, we are handling the constant case. See if the constant is too
3516      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
3517      this not only for its own sake, but to avoid having to test for this
3518      error case below.  If we didn't, we might generate wrong code.
3519
3520      For unsigned fields, the constant shifted right by the field length should
3521      be all zero.  For signed fields, the high-order bits should agree with
3522      the sign bit.  */
3523
3524   if (lunsignedp)
3525     {
3526       if (! integer_zerop (const_binop (RSHIFT_EXPR,
3527                                         fold_convert_loc (loc,
3528                                                           unsigned_type, rhs),
3529                                         size_int (lbitsize), 0)))
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   else
3537     {
3538       tree tem = const_binop (RSHIFT_EXPR,
3539                               fold_convert_loc (loc, signed_type, rhs),
3540                               size_int (lbitsize - 1), 0);
3541       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3542         {
3543           warning (0, "comparison is always %d due to width of bit-field",
3544                    code == NE_EXPR);
3545           return constant_boolean_node (code == NE_EXPR, compare_type);
3546         }
3547     }
3548
3549   /* Single-bit compares should always be against zero.  */
3550   if (lbitsize == 1 && ! integer_zerop (rhs))
3551     {
3552       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3553       rhs = build_int_cst (type, 0);
3554     }
3555
3556   /* Make a new bitfield reference, shift the constant over the
3557      appropriate number of bits and mask it with the computed mask
3558      (in case this was a signed field).  If we changed it, make a new one.  */
3559   lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3560   if (lvolatilep)
3561     {
3562       TREE_SIDE_EFFECTS (lhs) = 1;
3563       TREE_THIS_VOLATILE (lhs) = 1;
3564     }
3565
3566   rhs = const_binop (BIT_AND_EXPR,
3567                      const_binop (LSHIFT_EXPR,
3568                                   fold_convert_loc (loc, unsigned_type, rhs),
3569                                   size_int (lbitpos), 0),
3570                      mask, 0);
3571
3572   lhs = build2 (code, compare_type,
3573                 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
3574                 rhs);
3575   SET_EXPR_LOCATION (lhs, loc);
3576   return lhs;
3577 }
3578 \f
3579 /* Subroutine for fold_truthop: decode a field reference.
3580
3581    If EXP is a comparison reference, we return the innermost reference.
3582
3583    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3584    set to the starting bit number.
3585
3586    If the innermost field can be completely contained in a mode-sized
3587    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
3588
3589    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3590    otherwise it is not changed.
3591
3592    *PUNSIGNEDP is set to the signedness of the field.
3593
3594    *PMASK is set to the mask used.  This is either contained in a
3595    BIT_AND_EXPR or derived from the width of the field.
3596
3597    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3598
3599    Return 0 if this is not a component reference or is one that we can't
3600    do anything with.  */
3601
3602 static tree
3603 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3604                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3605                         int *punsignedp, int *pvolatilep,
3606                         tree *pmask, tree *pand_mask)
3607 {
3608   tree outer_type = 0;
3609   tree and_mask = 0;
3610   tree mask, inner, offset;
3611   tree unsigned_type;
3612   unsigned int precision;
3613
3614   /* All the optimizations using this function assume integer fields.
3615      There are problems with FP fields since the type_for_size call
3616      below can fail for, e.g., XFmode.  */
3617   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3618     return 0;
3619
3620   /* We are interested in the bare arrangement of bits, so strip everything
3621      that doesn't affect the machine mode.  However, record the type of the
3622      outermost expression if it may matter below.  */
3623   if (CONVERT_EXPR_P (exp)
3624       || TREE_CODE (exp) == NON_LVALUE_EXPR)
3625     outer_type = TREE_TYPE (exp);
3626   STRIP_NOPS (exp);
3627
3628   if (TREE_CODE (exp) == BIT_AND_EXPR)
3629     {
3630       and_mask = TREE_OPERAND (exp, 1);
3631       exp = TREE_OPERAND (exp, 0);
3632       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3633       if (TREE_CODE (and_mask) != INTEGER_CST)
3634         return 0;
3635     }
3636
3637   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3638                                punsignedp, pvolatilep, false);
3639   if ((inner == exp && and_mask == 0)
3640       || *pbitsize < 0 || offset != 0
3641       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3642     return 0;
3643
3644   /* If the number of bits in the reference is the same as the bitsize of
3645      the outer type, then the outer type gives the signedness. Otherwise
3646      (in case of a small bitfield) the signedness is unchanged.  */
3647   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3648     *punsignedp = TYPE_UNSIGNED (outer_type);
3649
3650   /* Compute the mask to access the bitfield.  */
3651   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3652   precision = TYPE_PRECISION (unsigned_type);
3653
3654   mask = build_int_cst_type (unsigned_type, -1);
3655
3656   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3657   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3658
3659   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
3660   if (and_mask != 0)
3661     mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3662                         fold_convert_loc (loc, unsigned_type, and_mask), mask);
3663
3664   *pmask = mask;
3665   *pand_mask = and_mask;
3666   return inner;
3667 }
3668
3669 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3670    bit positions.  */
3671
3672 static int
3673 all_ones_mask_p (const_tree mask, int size)
3674 {
3675   tree type = TREE_TYPE (mask);
3676   unsigned int precision = TYPE_PRECISION (type);
3677   tree tmask;
3678
3679   tmask = build_int_cst_type (signed_type_for (type), -1);
3680
3681   return
3682     tree_int_cst_equal (mask,
3683                         const_binop (RSHIFT_EXPR,
3684                                      const_binop (LSHIFT_EXPR, tmask,
3685                                                   size_int (precision - size),
3686                                                   0),
3687                                      size_int (precision - size), 0));
3688 }
3689
3690 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3691    represents the sign bit of EXP's type.  If EXP represents a sign
3692    or zero extension, also test VAL against the unextended type.
3693    The return value is the (sub)expression whose sign bit is VAL,
3694    or NULL_TREE otherwise.  */
3695
3696 static tree
3697 sign_bit_p (tree exp, const_tree val)
3698 {
3699   unsigned HOST_WIDE_INT mask_lo, lo;
3700   HOST_WIDE_INT mask_hi, hi;
3701   int width;
3702   tree t;
3703
3704   /* Tree EXP must have an integral type.  */
3705   t = TREE_TYPE (exp);
3706   if (! INTEGRAL_TYPE_P (t))
3707     return NULL_TREE;
3708
3709   /* Tree VAL must be an integer constant.  */
3710   if (TREE_CODE (val) != INTEGER_CST
3711       || TREE_OVERFLOW (val))
3712     return NULL_TREE;
3713
3714   width = TYPE_PRECISION (t);
3715   if (width > HOST_BITS_PER_WIDE_INT)
3716     {
3717       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3718       lo = 0;
3719
3720       mask_hi = ((unsigned HOST_WIDE_INT) -1
3721                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
3722       mask_lo = -1;
3723     }
3724   else
3725     {
3726       hi = 0;
3727       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3728
3729       mask_hi = 0;
3730       mask_lo = ((unsigned HOST_WIDE_INT) -1
3731                  >> (HOST_BITS_PER_WIDE_INT - width));
3732     }
3733
3734   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3735      treat VAL as if it were unsigned.  */
3736   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3737       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3738     return exp;
3739
3740   /* Handle extension from a narrower type.  */
3741   if (TREE_CODE (exp) == NOP_EXPR
3742       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3743     return sign_bit_p (TREE_OPERAND (exp, 0), val);
3744
3745   return NULL_TREE;
3746 }
3747
3748 /* Subroutine for fold_truthop: determine if an operand is simple enough
3749    to be evaluated unconditionally.  */
3750
3751 static int
3752 simple_operand_p (const_tree exp)
3753 {
3754   /* Strip any conversions that don't change the machine mode.  */
3755   STRIP_NOPS (exp);
3756
3757   return (CONSTANT_CLASS_P (exp)
3758           || TREE_CODE (exp) == SSA_NAME
3759           || (DECL_P (exp)
3760               && ! TREE_ADDRESSABLE (exp)
3761               && ! TREE_THIS_VOLATILE (exp)
3762               && ! DECL_NONLOCAL (exp)
3763               /* Don't regard global variables as simple.  They may be
3764                  allocated in ways unknown to the compiler (shared memory,
3765                  #pragma weak, etc).  */
3766               && ! TREE_PUBLIC (exp)
3767               && ! DECL_EXTERNAL (exp)
3768               /* Loading a static variable is unduly expensive, but global
3769                  registers aren't expensive.  */
3770               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3771 }
3772 \f
3773 /* The following functions are subroutines to fold_range_test and allow it to
3774    try to change a logical combination of comparisons into a range test.
3775
3776    For example, both
3777         X == 2 || X == 3 || X == 4 || X == 5
3778    and
3779         X >= 2 && X <= 5
3780    are converted to
3781         (unsigned) (X - 2) <= 3
3782
3783    We describe each set of comparisons as being either inside or outside
3784    a range, using a variable named like IN_P, and then describe the
3785    range with a lower and upper bound.  If one of the bounds is omitted,
3786    it represents either the highest or lowest value of the type.
3787
3788    In the comments below, we represent a range by two numbers in brackets
3789    preceded by a "+" to designate being inside that range, or a "-" to
3790    designate being outside that range, so the condition can be inverted by
3791    flipping the prefix.  An omitted bound is represented by a "-".  For
3792    example, "- [-, 10]" means being outside the range starting at the lowest
3793    possible value and ending at 10, in other words, being greater than 10.
3794    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3795    always false.
3796
3797    We set up things so that the missing bounds are handled in a consistent
3798    manner so neither a missing bound nor "true" and "false" need to be
3799    handled using a special case.  */
3800
3801 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3802    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3803    and UPPER1_P are nonzero if the respective argument is an upper bound
3804    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
3805    must be specified for a comparison.  ARG1 will be converted to ARG0's
3806    type if both are specified.  */
3807
3808 static tree
3809 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3810              tree arg1, int upper1_p)
3811 {
3812   tree tem;
3813   int result;
3814   int sgn0, sgn1;
3815
3816   /* If neither arg represents infinity, do the normal operation.
3817      Else, if not a comparison, return infinity.  Else handle the special
3818      comparison rules. Note that most of the cases below won't occur, but
3819      are handled for consistency.  */
3820
3821   if (arg0 != 0 && arg1 != 0)
3822     {
3823       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3824                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
3825       STRIP_NOPS (tem);
3826       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3827     }
3828
3829   if (TREE_CODE_CLASS (code) != tcc_comparison)
3830     return 0;
3831
3832   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3833      for neither.  In real maths, we cannot assume open ended ranges are
3834      the same. But, this is computer arithmetic, where numbers are finite.
3835      We can therefore make the transformation of any unbounded range with
3836      the value Z, Z being greater than any representable number. This permits
3837      us to treat unbounded ranges as equal.  */
3838   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3839   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3840   switch (code)
3841     {
3842     case EQ_EXPR:
3843       result = sgn0 == sgn1;
3844       break;
3845     case NE_EXPR:
3846       result = sgn0 != sgn1;
3847       break;
3848     case LT_EXPR:
3849       result = sgn0 < sgn1;
3850       break;
3851     case LE_EXPR:
3852       result = sgn0 <= sgn1;
3853       break;
3854     case GT_EXPR:
3855       result = sgn0 > sgn1;
3856       break;
3857     case GE_EXPR:
3858       result = sgn0 >= sgn1;
3859       break;
3860     default:
3861       gcc_unreachable ();
3862     }
3863
3864   return constant_boolean_node (result, type);
3865 }
3866 \f
3867 /* Given EXP, a logical expression, set the range it is testing into
3868    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
3869    actually being tested.  *PLOW and *PHIGH will be made of the same
3870    type as the returned expression.  If EXP is not a comparison, we
3871    will most likely not be returning a useful value and range.  Set
3872    *STRICT_OVERFLOW_P to true if the return value is only valid
3873    because signed overflow is undefined; otherwise, do not change
3874    *STRICT_OVERFLOW_P.  */
3875
3876 tree
3877 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
3878             bool *strict_overflow_p)
3879 {
3880   enum tree_code code;
3881   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
3882   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
3883   int in_p, n_in_p;
3884   tree low, high, n_low, n_high;
3885   location_t loc = EXPR_LOCATION (exp);
3886
3887   /* Start with simply saying "EXP != 0" and then look at the code of EXP
3888      and see if we can refine the range.  Some of the cases below may not
3889      happen, but it doesn't seem worth worrying about this.  We "continue"
3890      the outer loop when we've changed something; otherwise we "break"
3891      the switch, which will "break" the while.  */
3892
3893   in_p = 0;
3894   low = high = build_int_cst (TREE_TYPE (exp), 0);
3895
3896   while (1)
3897     {
3898       code = TREE_CODE (exp);
3899       exp_type = TREE_TYPE (exp);
3900
3901       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3902         {
3903           if (TREE_OPERAND_LENGTH (exp) > 0)
3904             arg0 = TREE_OPERAND (exp, 0);
3905           if (TREE_CODE_CLASS (code) == tcc_comparison
3906               || TREE_CODE_CLASS (code) == tcc_unary
3907               || TREE_CODE_CLASS (code) == tcc_binary)
3908             arg0_type = TREE_TYPE (arg0);
3909           if (TREE_CODE_CLASS (code) == tcc_binary
3910               || TREE_CODE_CLASS (code) == tcc_comparison
3911               || (TREE_CODE_CLASS (code) == tcc_expression
3912                   && TREE_OPERAND_LENGTH (exp) > 1))
3913             arg1 = TREE_OPERAND (exp, 1);
3914         }
3915
3916       switch (code)
3917         {
3918         case TRUTH_NOT_EXPR:
3919           in_p = ! in_p, exp = arg0;
3920           continue;
3921
3922         case EQ_EXPR: case NE_EXPR:
3923         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3924           /* We can only do something if the range is testing for zero
3925              and if the second operand is an integer constant.  Note that
3926              saying something is "in" the range we make is done by
3927              complementing IN_P since it will set in the initial case of
3928              being not equal to zero; "out" is leaving it alone.  */
3929           if (low == 0 || high == 0
3930               || ! integer_zerop (low) || ! integer_zerop (high)
3931               || TREE_CODE (arg1) != INTEGER_CST)
3932             break;
3933
3934           switch (code)
3935             {
3936             case NE_EXPR:  /* - [c, c]  */
3937               low = high = arg1;
3938               break;
3939             case EQ_EXPR:  /* + [c, c]  */
3940               in_p = ! in_p, low = high = arg1;
3941               break;
3942             case GT_EXPR:  /* - [-, c] */
3943               low = 0, high = arg1;
3944               break;
3945             case GE_EXPR:  /* + [c, -] */
3946               in_p = ! in_p, low = arg1, high = 0;
3947               break;
3948             case LT_EXPR:  /* - [c, -] */
3949               low = arg1, high = 0;
3950               break;
3951             case LE_EXPR:  /* + [-, c] */
3952               in_p = ! in_p, low = 0, high = arg1;
3953               break;
3954             default:
3955               gcc_unreachable ();
3956             }
3957
3958           /* If this is an unsigned comparison, we also know that EXP is
3959              greater than or equal to zero.  We base the range tests we make
3960              on that fact, so we record it here so we can parse existing
3961              range tests.  We test arg0_type since often the return type
3962              of, e.g. EQ_EXPR, is boolean.  */
3963           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3964             {
3965               if (! merge_ranges (&n_in_p, &n_low, &n_high,
3966                                   in_p, low, high, 1,
3967                                   build_int_cst (arg0_type, 0),
3968                                   NULL_TREE))
3969                 break;
3970
3971               in_p = n_in_p, low = n_low, high = n_high;
3972
3973               /* If the high bound is missing, but we have a nonzero low
3974                  bound, reverse the range so it goes from zero to the low bound
3975                  minus 1.  */
3976               if (high == 0 && low && ! integer_zerop (low))
3977                 {
3978                   in_p = ! in_p;
3979                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3980                                       integer_one_node, 0);
3981                   low = build_int_cst (arg0_type, 0);
3982                 }
3983             }
3984
3985           exp = arg0;
3986           continue;
3987
3988         case NEGATE_EXPR:
3989           /* (-x) IN [a,b] -> x in [-b, -a]  */
3990           n_low = range_binop (MINUS_EXPR, exp_type,
3991                                build_int_cst (exp_type, 0),
3992                                0, high, 1);
3993           n_high = range_binop (MINUS_EXPR, exp_type,
3994                                 build_int_cst (exp_type, 0),
3995                                 0, low, 0);
3996           low = n_low, high = n_high;
3997           exp = arg0;
3998           continue;
3999
4000         case BIT_NOT_EXPR:
4001           /* ~ X -> -X - 1  */
4002           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4003                         build_int_cst (exp_type, 1));
4004           SET_EXPR_LOCATION (exp, loc);
4005           continue;
4006
4007         case PLUS_EXPR:  case MINUS_EXPR:
4008           if (TREE_CODE (arg1) != INTEGER_CST)
4009             break;
4010
4011           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4012              move a constant to the other side.  */
4013           if (!TYPE_UNSIGNED (arg0_type)
4014               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4015             break;
4016
4017           /* If EXP is signed, any overflow in the computation is undefined,
4018              so we don't worry about it so long as our computations on
4019              the bounds don't overflow.  For unsigned, overflow is defined
4020              and this is exactly the right thing.  */
4021           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4022                                arg0_type, low, 0, arg1, 0);
4023           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4024                                 arg0_type, high, 1, arg1, 0);
4025           if ((n_low != 0 && TREE_OVERFLOW (n_low))
4026               || (n_high != 0 && TREE_OVERFLOW (n_high)))
4027             break;
4028
4029           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4030             *strict_overflow_p = true;
4031
4032           /* Check for an unsigned range which has wrapped around the maximum
4033              value thus making n_high < n_low, and normalize it.  */
4034           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4035             {
4036               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4037                                  integer_one_node, 0);
4038               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4039                                   integer_one_node, 0);
4040
4041               /* If the range is of the form +/- [ x+1, x ], we won't
4042                  be able to normalize it.  But then, it represents the
4043                  whole range or the empty set, so make it
4044                  +/- [ -, - ].  */
4045               if (tree_int_cst_equal (n_low, low)
4046                   && tree_int_cst_equal (n_high, high))
4047                 low = high = 0;
4048               else
4049                 in_p = ! in_p;
4050             }
4051           else
4052             low = n_low, high = n_high;
4053
4054           exp = arg0;
4055           continue;
4056
4057         CASE_CONVERT: case NON_LVALUE_EXPR:
4058           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4059             break;
4060
4061           if (! INTEGRAL_TYPE_P (arg0_type)
4062               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4063               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4064             break;
4065
4066           n_low = low, n_high = high;
4067
4068           if (n_low != 0)
4069             n_low = fold_convert_loc (loc, arg0_type, n_low);
4070
4071           if (n_high != 0)
4072             n_high = fold_convert_loc (loc, arg0_type, n_high);
4073
4074
4075           /* If we're converting arg0 from an unsigned type, to exp,
4076              a signed type,  we will be doing the comparison as unsigned.
4077              The tests above have already verified that LOW and HIGH
4078              are both positive.
4079
4080              So we have to ensure that we will handle large unsigned
4081              values the same way that the current signed bounds treat
4082              negative values.  */
4083
4084           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4085             {
4086               tree high_positive;
4087               tree equiv_type;
4088               /* For fixed-point modes, we need to pass the saturating flag
4089                  as the 2nd parameter.  */
4090               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4091                 equiv_type = lang_hooks.types.type_for_mode
4092                              (TYPE_MODE (arg0_type),
4093                               TYPE_SATURATING (arg0_type));
4094               else
4095                 equiv_type = lang_hooks.types.type_for_mode
4096                              (TYPE_MODE (arg0_type), 1);
4097
4098               /* A range without an upper bound is, naturally, unbounded.
4099                  Since convert would have cropped a very large value, use
4100                  the max value for the destination type.  */
4101               high_positive
4102                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4103                 : TYPE_MAX_VALUE (arg0_type);
4104
4105               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4106                 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4107                                              fold_convert_loc (loc, arg0_type,
4108                                                                high_positive),
4109                                              build_int_cst (arg0_type, 1));
4110
4111               /* If the low bound is specified, "and" the range with the
4112                  range for which the original unsigned value will be
4113                  positive.  */
4114               if (low != 0)
4115                 {
4116                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4117                                       1, n_low, n_high, 1,
4118                                       fold_convert_loc (loc, arg0_type,
4119                                                         integer_zero_node),
4120                                       high_positive))
4121                     break;
4122
4123                   in_p = (n_in_p == in_p);
4124                 }
4125               else
4126                 {
4127                   /* Otherwise, "or" the range with the range of the input
4128                      that will be interpreted as negative.  */
4129                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4130                                       0, n_low, n_high, 1,
4131                                       fold_convert_loc (loc, arg0_type,
4132                                                         integer_zero_node),
4133                                       high_positive))
4134                     break;
4135
4136                   in_p = (in_p != n_in_p);
4137                 }
4138             }
4139
4140           exp = arg0;
4141           low = n_low, high = n_high;
4142           continue;
4143
4144         default:
4145           break;
4146         }
4147
4148       break;
4149     }
4150
4151   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4152   if (TREE_CODE (exp) == INTEGER_CST)
4153     {
4154       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4155                                                  exp, 0, low, 0))
4156                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4157                                                     exp, 1, high, 1)));
4158       low = high = 0;
4159       exp = 0;
4160     }
4161
4162   *pin_p = in_p, *plow = low, *phigh = high;
4163   return exp;
4164 }
4165 \f
4166 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4167    type, TYPE, return an expression to test if EXP is in (or out of, depending
4168    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4169
4170 tree
4171 build_range_check (location_t loc, tree type, tree exp, int in_p,
4172                    tree low, tree high)
4173 {
4174   tree etype = TREE_TYPE (exp), value;
4175
4176 #ifdef HAVE_canonicalize_funcptr_for_compare
4177   /* Disable this optimization for function pointer expressions
4178      on targets that require function pointer canonicalization.  */
4179   if (HAVE_canonicalize_funcptr_for_compare
4180       && TREE_CODE (etype) == POINTER_TYPE
4181       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4182     return NULL_TREE;
4183 #endif
4184
4185   if (! in_p)
4186     {
4187       value = build_range_check (loc, type, exp, 1, low, high);
4188       if (value != 0)
4189         return invert_truthvalue_loc (loc, value);
4190
4191       return 0;
4192     }
4193
4194   if (low == 0 && high == 0)
4195     return build_int_cst (type, 1);
4196
4197   if (low == 0)
4198     return fold_build2_loc (loc, LE_EXPR, type, exp,
4199                         fold_convert_loc (loc, etype, high));
4200
4201   if (high == 0)
4202     return fold_build2_loc (loc, GE_EXPR, type, exp,
4203                         fold_convert_loc (loc, etype, low));
4204
4205   if (operand_equal_p (low, high, 0))
4206     return fold_build2_loc (loc, EQ_EXPR, type, exp,
4207                         fold_convert_loc (loc, etype, low));
4208
4209   if (integer_zerop (low))
4210     {
4211       if (! TYPE_UNSIGNED (etype))
4212         {
4213           etype = unsigned_type_for (etype);
4214           high = fold_convert_loc (loc, etype, high);
4215           exp = fold_convert_loc (loc, etype, exp);
4216         }
4217       return build_range_check (loc, type, exp, 1, 0, high);
4218     }
4219
4220   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4221   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4222     {
4223       unsigned HOST_WIDE_INT lo;
4224       HOST_WIDE_INT hi;
4225       int prec;
4226
4227       prec = TYPE_PRECISION (etype);
4228       if (prec <= HOST_BITS_PER_WIDE_INT)
4229         {
4230           hi = 0;
4231           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4232         }
4233       else
4234         {
4235           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4236           lo = (unsigned HOST_WIDE_INT) -1;
4237         }
4238
4239       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4240         {
4241           if (TYPE_UNSIGNED (etype))
4242             {
4243               tree signed_etype = signed_type_for (etype);
4244               if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4245                 etype
4246                   = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4247               else
4248                 etype = signed_etype;
4249               exp = fold_convert_loc (loc, etype, exp);
4250             }
4251           return fold_build2_loc (loc, GT_EXPR, type, exp,
4252                               build_int_cst (etype, 0));
4253         }
4254     }
4255
4256   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4257      This requires wrap-around arithmetics for the type of the expression.
4258      First make sure that arithmetics in this type is valid, then make sure
4259      that it wraps around.  */
4260   if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4261     etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4262                                             TYPE_UNSIGNED (etype));
4263
4264   if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4265     {
4266       tree utype, minv, maxv;
4267
4268       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4269          for the type in question, as we rely on this here.  */
4270       utype = unsigned_type_for (etype);
4271       maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4272       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4273                           integer_one_node, 1);
4274       minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4275
4276       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4277                                       minv, 1, maxv, 1)))
4278         etype = utype;
4279       else
4280         return 0;
4281     }
4282
4283   high = fold_convert_loc (loc, etype, high);
4284   low = fold_convert_loc (loc, etype, low);
4285   exp = fold_convert_loc (loc, etype, exp);
4286
4287   value = const_binop (MINUS_EXPR, high, low, 0);
4288
4289
4290   if (POINTER_TYPE_P (etype))
4291     {
4292       if (value != 0 && !TREE_OVERFLOW (value))
4293         {
4294           low = fold_convert_loc (loc, sizetype, low);
4295           low = fold_build1_loc (loc, NEGATE_EXPR, sizetype, low);
4296           return build_range_check (loc, type,
4297                                     fold_build2_loc (loc, POINTER_PLUS_EXPR,
4298                                                  etype, exp, low),
4299                                     1, build_int_cst (etype, 0), value);
4300         }
4301       return 0;
4302     }
4303
4304   if (value != 0 && !TREE_OVERFLOW (value))
4305     return build_range_check (loc, type,
4306                               fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4307                               1, build_int_cst (etype, 0), value);
4308
4309   return 0;
4310 }
4311 \f
4312 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4313
4314 static tree
4315 range_predecessor (tree val)
4316 {
4317   tree type = TREE_TYPE (val);
4318
4319   if (INTEGRAL_TYPE_P (type)
4320       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4321     return 0;
4322   else
4323     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4324 }
4325
4326 /* Return the successor of VAL in its type, handling the infinite case.  */
4327
4328 static tree
4329 range_successor (tree val)
4330 {
4331   tree type = TREE_TYPE (val);
4332
4333   if (INTEGRAL_TYPE_P (type)
4334       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4335     return 0;
4336   else
4337     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4338 }
4339
4340 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4341    can, 0 if we can't.  Set the output range into the specified parameters.  */
4342
4343 bool
4344 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4345               tree high0, int in1_p, tree low1, tree high1)
4346 {
4347   int no_overlap;
4348   int subset;
4349   int temp;
4350   tree tem;
4351   int in_p;
4352   tree low, high;
4353   int lowequal = ((low0 == 0 && low1 == 0)
4354                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4355                                                 low0, 0, low1, 0)));
4356   int highequal = ((high0 == 0 && high1 == 0)
4357                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4358                                                  high0, 1, high1, 1)));
4359
4360   /* Make range 0 be the range that starts first, or ends last if they
4361      start at the same value.  Swap them if it isn't.  */
4362   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4363                                  low0, 0, low1, 0))
4364       || (lowequal
4365           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4366                                         high1, 1, high0, 1))))
4367     {
4368       temp = in0_p, in0_p = in1_p, in1_p = temp;
4369       tem = low0, low0 = low1, low1 = tem;
4370       tem = high0, high0 = high1, high1 = tem;
4371     }
4372
4373   /* Now flag two cases, whether the ranges are disjoint or whether the
4374      second range is totally subsumed in the first.  Note that the tests
4375      below are simplified by the ones above.  */
4376   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4377                                           high0, 1, low1, 0));
4378   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4379                                       high1, 1, high0, 1));
4380
4381   /* We now have four cases, depending on whether we are including or
4382      excluding the two ranges.  */
4383   if (in0_p && in1_p)
4384     {
4385       /* If they don't overlap, the result is false.  If the second range
4386          is a subset it is the result.  Otherwise, the range is from the start
4387          of the second to the end of the first.  */
4388       if (no_overlap)
4389         in_p = 0, low = high = 0;
4390       else if (subset)
4391         in_p = 1, low = low1, high = high1;
4392       else
4393         in_p = 1, low = low1, high = high0;
4394     }
4395
4396   else if (in0_p && ! in1_p)
4397     {
4398       /* If they don't overlap, the result is the first range.  If they are
4399          equal, the result is false.  If the second range is a subset of the
4400          first, and the ranges begin at the same place, we go from just after
4401          the end of the second range to the end of the first.  If the second
4402          range is not a subset of the first, or if it is a subset and both
4403          ranges end at the same place, the range starts at the start of the
4404          first range and ends just before the second range.
4405          Otherwise, we can't describe this as a single range.  */
4406       if (no_overlap)
4407         in_p = 1, low = low0, high = high0;
4408       else if (lowequal && highequal)
4409         in_p = 0, low = high = 0;
4410       else if (subset && lowequal)
4411         {
4412           low = range_successor (high1);
4413           high = high0;
4414           in_p = 1;
4415           if (low == 0)
4416             {
4417               /* We are in the weird situation where high0 > high1 but
4418                  high1 has no successor.  Punt.  */
4419               return 0;
4420             }
4421         }
4422       else if (! subset || highequal)
4423         {
4424           low = low0;
4425           high = range_predecessor (low1);
4426           in_p = 1;
4427           if (high == 0)
4428             {
4429               /* low0 < low1 but low1 has no predecessor.  Punt.  */
4430               return 0;
4431             }
4432         }
4433       else
4434         return 0;
4435     }
4436
4437   else if (! in0_p && in1_p)
4438     {
4439       /* If they don't overlap, the result is the second range.  If the second
4440          is a subset of the first, the result is false.  Otherwise,
4441          the range starts just after the first range and ends at the
4442          end of the second.  */
4443       if (no_overlap)
4444         in_p = 1, low = low1, high = high1;
4445       else if (subset || highequal)
4446         in_p = 0, low = high = 0;
4447       else
4448         {
4449           low = range_successor (high0);
4450           high = high1;
4451           in_p = 1;
4452           if (low == 0)
4453             {
4454               /* high1 > high0 but high0 has no successor.  Punt.  */
4455               return 0;
4456             }
4457         }
4458     }
4459
4460   else
4461     {
4462       /* The case where we are excluding both ranges.  Here the complex case
4463          is if they don't overlap.  In that case, the only time we have a
4464          range is if they are adjacent.  If the second is a subset of the
4465          first, the result is the first.  Otherwise, the range to exclude
4466          starts at the beginning of the first range and ends at the end of the
4467          second.  */
4468       if (no_overlap)
4469         {
4470           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4471                                          range_successor (high0),
4472                                          1, low1, 0)))
4473             in_p = 0, low = low0, high = high1;
4474           else
4475             {
4476               /* Canonicalize - [min, x] into - [-, x].  */
4477               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4478                 switch (TREE_CODE (TREE_TYPE (low0)))
4479                   {
4480                   case ENUMERAL_TYPE:
4481                     if (TYPE_PRECISION (TREE_TYPE (low0))
4482                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4483                       break;
4484                     /* FALLTHROUGH */
4485                   case INTEGER_TYPE:
4486                     if (tree_int_cst_equal (low0,
4487                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4488                       low0 = 0;
4489                     break;
4490                   case POINTER_TYPE:
4491                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4492                         && integer_zerop (low0))
4493                       low0 = 0;
4494                     break;
4495                   default:
4496                     break;
4497                   }
4498
4499               /* Canonicalize - [x, max] into - [x, -].  */
4500               if (high1 && TREE_CODE (high1) == INTEGER_CST)
4501                 switch (TREE_CODE (TREE_TYPE (high1)))
4502                   {
4503                   case ENUMERAL_TYPE:
4504                     if (TYPE_PRECISION (TREE_TYPE (high1))
4505                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4506                       break;
4507                     /* FALLTHROUGH */
4508                   case INTEGER_TYPE:
4509                     if (tree_int_cst_equal (high1,
4510                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
4511                       high1 = 0;
4512                     break;
4513                   case POINTER_TYPE:
4514                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
4515                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4516                                                        high1, 1,
4517                                                        integer_one_node, 1)))
4518                       high1 = 0;
4519                     break;
4520                   default:
4521                     break;
4522                   }
4523
4524               /* The ranges might be also adjacent between the maximum and
4525                  minimum values of the given type.  For
4526                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4527                  return + [x + 1, y - 1].  */
4528               if (low0 == 0 && high1 == 0)
4529                 {
4530                   low = range_successor (high0);
4531                   high = range_predecessor (low1);
4532                   if (low == 0 || high == 0)
4533                     return 0;
4534
4535                   in_p = 1;
4536                 }
4537               else
4538                 return 0;
4539             }
4540         }
4541       else if (subset)
4542         in_p = 0, low = low0, high = high0;
4543       else
4544         in_p = 0, low = low0, high = high1;
4545     }
4546
4547   *pin_p = in_p, *plow = low, *phigh = high;
4548   return 1;
4549 }
4550 \f
4551
4552 /* Subroutine of fold, looking inside expressions of the form
4553    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4554    of the COND_EXPR.  This function is being used also to optimize
4555    A op B ? C : A, by reversing the comparison first.
4556
4557    Return a folded expression whose code is not a COND_EXPR
4558    anymore, or NULL_TREE if no folding opportunity is found.  */
4559
4560 static tree
4561 fold_cond_expr_with_comparison (location_t loc, tree type,
4562                                 tree arg0, tree arg1, tree arg2)
4563 {
4564   enum tree_code comp_code = TREE_CODE (arg0);
4565   tree arg00 = TREE_OPERAND (arg0, 0);
4566   tree arg01 = TREE_OPERAND (arg0, 1);
4567   tree arg1_type = TREE_TYPE (arg1);
4568   tree tem;
4569
4570   STRIP_NOPS (arg1);
4571   STRIP_NOPS (arg2);
4572
4573   /* If we have A op 0 ? A : -A, consider applying the following
4574      transformations:
4575
4576      A == 0? A : -A    same as -A
4577      A != 0? A : -A    same as A
4578      A >= 0? A : -A    same as abs (A)
4579      A > 0?  A : -A    same as abs (A)
4580      A <= 0? A : -A    same as -abs (A)
4581      A < 0?  A : -A    same as -abs (A)
4582
4583      None of these transformations work for modes with signed
4584      zeros.  If A is +/-0, the first two transformations will
4585      change the sign of the result (from +0 to -0, or vice
4586      versa).  The last four will fix the sign of the result,
4587      even though the original expressions could be positive or
4588      negative, depending on the sign of A.
4589
4590      Note that all these transformations are correct if A is
4591      NaN, since the two alternatives (A and -A) are also NaNs.  */
4592   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4593       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4594           ? real_zerop (arg01)
4595           : integer_zerop (arg01))
4596       && ((TREE_CODE (arg2) == NEGATE_EXPR
4597            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4598              /* In the case that A is of the form X-Y, '-A' (arg2) may
4599                 have already been folded to Y-X, check for that. */
4600           || (TREE_CODE (arg1) == MINUS_EXPR
4601               && TREE_CODE (arg2) == MINUS_EXPR
4602               && operand_equal_p (TREE_OPERAND (arg1, 0),
4603                                   TREE_OPERAND (arg2, 1), 0)
4604               && operand_equal_p (TREE_OPERAND (arg1, 1),
4605                                   TREE_OPERAND (arg2, 0), 0))))
4606     switch (comp_code)
4607       {
4608       case EQ_EXPR:
4609       case UNEQ_EXPR:
4610         tem = fold_convert_loc (loc, arg1_type, arg1);
4611         return pedantic_non_lvalue_loc (loc,
4612                                     fold_convert_loc (loc, type,
4613                                                   negate_expr (tem)));
4614       case NE_EXPR:
4615       case LTGT_EXPR:
4616         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4617       case UNGE_EXPR:
4618       case UNGT_EXPR:
4619         if (flag_trapping_math)
4620           break;
4621         /* Fall through.  */
4622       case GE_EXPR:
4623       case GT_EXPR:
4624         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4625           arg1 = fold_convert_loc (loc, signed_type_for
4626                                (TREE_TYPE (arg1)), arg1);
4627         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4628         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4629       case UNLE_EXPR:
4630       case UNLT_EXPR:
4631         if (flag_trapping_math)
4632           break;
4633       case LE_EXPR:
4634       case LT_EXPR:
4635         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4636           arg1 = fold_convert_loc (loc, signed_type_for
4637                                (TREE_TYPE (arg1)), arg1);
4638         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4639         return negate_expr (fold_convert_loc (loc, type, tem));
4640       default:
4641         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4642         break;
4643       }
4644
4645   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
4646      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
4647      both transformations are correct when A is NaN: A != 0
4648      is then true, and A == 0 is false.  */
4649
4650   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4651       && integer_zerop (arg01) && integer_zerop (arg2))
4652     {
4653       if (comp_code == NE_EXPR)
4654         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4655       else if (comp_code == EQ_EXPR)
4656         return build_int_cst (type, 0);
4657     }
4658
4659   /* Try some transformations of A op B ? A : B.
4660
4661      A == B? A : B    same as B
4662      A != B? A : B    same as A
4663      A >= B? A : B    same as max (A, B)
4664      A > B?  A : B    same as max (B, A)
4665      A <= B? A : B    same as min (A, B)
4666      A < B?  A : B    same as min (B, A)
4667
4668      As above, these transformations don't work in the presence
4669      of signed zeros.  For example, if A and B are zeros of
4670      opposite sign, the first two transformations will change
4671      the sign of the result.  In the last four, the original
4672      expressions give different results for (A=+0, B=-0) and
4673      (A=-0, B=+0), but the transformed expressions do not.
4674
4675      The first two transformations are correct if either A or B
4676      is a NaN.  In the first transformation, the condition will
4677      be false, and B will indeed be chosen.  In the case of the
4678      second transformation, the condition A != B will be true,
4679      and A will be chosen.
4680
4681      The conversions to max() and min() are not correct if B is
4682      a number and A is not.  The conditions in the original
4683      expressions will be false, so all four give B.  The min()
4684      and max() versions would give a NaN instead.  */
4685   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4686       && operand_equal_for_comparison_p (arg01, arg2, arg00)
4687       /* Avoid these transformations if the COND_EXPR may be used
4688          as an lvalue in the C++ front-end.  PR c++/19199.  */
4689       && (in_gimple_form
4690           || (strcmp (lang_hooks.name, "GNU C++") != 0
4691               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4692           || ! maybe_lvalue_p (arg1)
4693           || ! maybe_lvalue_p (arg2)))
4694     {
4695       tree comp_op0 = arg00;
4696       tree comp_op1 = arg01;
4697       tree comp_type = TREE_TYPE (comp_op0);
4698
4699       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
4700       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4701         {
4702           comp_type = type;
4703           comp_op0 = arg1;
4704           comp_op1 = arg2;
4705         }
4706
4707       switch (comp_code)
4708         {
4709         case EQ_EXPR:
4710           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
4711         case NE_EXPR:
4712           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4713         case LE_EXPR:
4714         case LT_EXPR:
4715         case UNLE_EXPR:
4716         case UNLT_EXPR:
4717           /* In C++ a ?: expression can be an lvalue, so put the
4718              operand which will be used if they are equal first
4719              so that we can convert this back to the
4720              corresponding COND_EXPR.  */
4721           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4722             {
4723               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4724               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4725               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4726                     ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
4727                     : fold_build2_loc (loc, MIN_EXPR, comp_type,
4728                                    comp_op1, comp_op0);
4729               return pedantic_non_lvalue_loc (loc,
4730                                           fold_convert_loc (loc, type, tem));
4731             }
4732           break;
4733         case GE_EXPR:
4734         case GT_EXPR:
4735         case UNGE_EXPR:
4736         case UNGT_EXPR:
4737           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4738             {
4739               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4740               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4741               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4742                     ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
4743                     : fold_build2_loc (loc, MAX_EXPR, comp_type,
4744                                    comp_op1, comp_op0);
4745               return pedantic_non_lvalue_loc (loc,
4746                                           fold_convert_loc (loc, type, tem));
4747             }
4748           break;
4749         case UNEQ_EXPR:
4750           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4751             return pedantic_non_lvalue_loc (loc,
4752                                         fold_convert_loc (loc, type, arg2));
4753           break;
4754         case LTGT_EXPR:
4755           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4756             return pedantic_non_lvalue_loc (loc,
4757                                         fold_convert_loc (loc, type, arg1));
4758           break;
4759         default:
4760           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4761           break;
4762         }
4763     }
4764
4765   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4766      we might still be able to simplify this.  For example,
4767      if C1 is one less or one more than C2, this might have started
4768      out as a MIN or MAX and been transformed by this function.
4769      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
4770
4771   if (INTEGRAL_TYPE_P (type)
4772       && TREE_CODE (arg01) == INTEGER_CST
4773       && TREE_CODE (arg2) == INTEGER_CST)
4774     switch (comp_code)
4775       {
4776       case EQ_EXPR:
4777         if (TREE_CODE (arg1) == INTEGER_CST)
4778           break;
4779         /* We can replace A with C1 in this case.  */
4780         arg1 = fold_convert_loc (loc, type, arg01);
4781         return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
4782
4783       case LT_EXPR:
4784         /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
4785            MIN_EXPR, to preserve the signedness of the comparison.  */
4786         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4787                                OEP_ONLY_CONST)
4788             && operand_equal_p (arg01,
4789                                 const_binop (PLUS_EXPR, arg2,
4790                                              build_int_cst (type, 1), 0),
4791                                 OEP_ONLY_CONST))
4792           {
4793             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4794                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4795                                                      arg2));
4796             return pedantic_non_lvalue_loc (loc,
4797                                             fold_convert_loc (loc, type, tem));
4798           }
4799         break;
4800
4801       case LE_EXPR:
4802         /* If C1 is C2 - 1, this is min(A, C2), with the same care
4803            as above.  */
4804         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4805                                OEP_ONLY_CONST)
4806             && operand_equal_p (arg01,
4807                                 const_binop (MINUS_EXPR, arg2,
4808                                              build_int_cst (type, 1), 0),
4809                                 OEP_ONLY_CONST))
4810           {
4811             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4812                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4813                                                      arg2));
4814             return pedantic_non_lvalue_loc (loc,
4815                                             fold_convert_loc (loc, type, tem));
4816           }
4817         break;
4818
4819       case GT_EXPR:
4820         /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
4821            MAX_EXPR, to preserve the signedness of the comparison.  */
4822         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4823                                OEP_ONLY_CONST)
4824             && operand_equal_p (arg01,
4825                                 const_binop (MINUS_EXPR, arg2,
4826                                              build_int_cst (type, 1), 0),
4827                                 OEP_ONLY_CONST))
4828           {
4829             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4830                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4831                                                      arg2));
4832             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4833           }
4834         break;
4835
4836       case GE_EXPR:
4837         /* If C1 is C2 + 1, this is max(A, C2), with the same care as above.  */
4838         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4839                                OEP_ONLY_CONST)
4840             && operand_equal_p (arg01,
4841                                 const_binop (PLUS_EXPR, arg2,
4842                                              build_int_cst (type, 1), 0),
4843                                 OEP_ONLY_CONST))
4844           {
4845             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4846                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4847                                                      arg2));
4848             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4849           }
4850         break;
4851       case NE_EXPR:
4852         break;
4853       default:
4854         gcc_unreachable ();
4855       }
4856
4857   return NULL_TREE;
4858 }
4859
4860
4861 \f
4862 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4863 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
4864   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
4865                 false) >= 2)
4866 #endif
4867
4868 /* EXP is some logical combination of boolean tests.  See if we can
4869    merge it into some range test.  Return the new tree if so.  */
4870
4871 static tree
4872 fold_range_test (location_t loc, enum tree_code code, tree type,
4873                  tree op0, tree op1)
4874 {
4875   int or_op = (code == TRUTH_ORIF_EXPR
4876                || code == TRUTH_OR_EXPR);
4877   int in0_p, in1_p, in_p;
4878   tree low0, low1, low, high0, high1, high;
4879   bool strict_overflow_p = false;
4880   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
4881   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
4882   tree tem;
4883   const char * const warnmsg = G_("assuming signed overflow does not occur "
4884                                   "when simplifying range test");
4885
4886   /* If this is an OR operation, invert both sides; we will invert
4887      again at the end.  */
4888   if (or_op)
4889     in0_p = ! in0_p, in1_p = ! in1_p;
4890
4891   /* If both expressions are the same, if we can merge the ranges, and we
4892      can build the range test, return it or it inverted.  If one of the
4893      ranges is always true or always false, consider it to be the same
4894      expression as the other.  */
4895   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4896       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4897                        in1_p, low1, high1)
4898       && 0 != (tem = (build_range_check (UNKNOWN_LOCATION, type,
4899                                          lhs != 0 ? lhs
4900                                          : rhs != 0 ? rhs : integer_zero_node,
4901                                          in_p, low, high))))
4902     {
4903       if (strict_overflow_p)
4904         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
4905       return or_op ? invert_truthvalue_loc (loc, tem) : tem;
4906     }
4907
4908   /* On machines where the branch cost is expensive, if this is a
4909      short-circuited branch and the underlying object on both sides
4910      is the same, make a non-short-circuit operation.  */
4911   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4912            && lhs != 0 && rhs != 0
4913            && (code == TRUTH_ANDIF_EXPR
4914                || code == TRUTH_ORIF_EXPR)
4915            && operand_equal_p (lhs, rhs, 0))
4916     {
4917       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
4918          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4919          which cases we can't do this.  */
4920       if (simple_operand_p (lhs))
4921         {
4922           tem = build2 (code == TRUTH_ANDIF_EXPR
4923                         ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4924                         type, op0, op1);
4925           SET_EXPR_LOCATION (tem, loc);
4926           return tem;
4927         }
4928
4929       else if (lang_hooks.decls.global_bindings_p () == 0
4930                && ! CONTAINS_PLACEHOLDER_P (lhs))
4931         {
4932           tree common = save_expr (lhs);
4933
4934           if (0 != (lhs = build_range_check (loc, type, common,
4935                                              or_op ? ! in0_p : in0_p,
4936                                              low0, high0))
4937               && (0 != (rhs = build_range_check (loc, type, common,
4938                                                  or_op ? ! in1_p : in1_p,
4939                                                  low1, high1))))
4940             {
4941               if (strict_overflow_p)
4942                 fold_overflow_warning (warnmsg,
4943                                        WARN_STRICT_OVERFLOW_COMPARISON);
4944               tem = build2 (code == TRUTH_ANDIF_EXPR
4945                             ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4946                             type, lhs, rhs);
4947               SET_EXPR_LOCATION (tem, loc);
4948               return tem;
4949             }
4950         }
4951     }
4952
4953   return 0;
4954 }
4955 \f
4956 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4957    bit value.  Arrange things so the extra bits will be set to zero if and
4958    only if C is signed-extended to its full width.  If MASK is nonzero,
4959    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
4960
4961 static tree
4962 unextend (tree c, int p, int unsignedp, tree mask)
4963 {
4964   tree type = TREE_TYPE (c);
4965   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4966   tree temp;
4967
4968   if (p == modesize || unsignedp)
4969     return c;
4970
4971   /* We work by getting just the sign bit into the low-order bit, then
4972      into the high-order bit, then sign-extend.  We then XOR that value
4973      with C.  */
4974   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
4975   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
4976
4977   /* We must use a signed type in order to get an arithmetic right shift.
4978      However, we must also avoid introducing accidental overflows, so that
4979      a subsequent call to integer_zerop will work.  Hence we must
4980      do the type conversion here.  At this point, the constant is either
4981      zero or one, and the conversion to a signed type can never overflow.
4982      We could get an overflow if this conversion is done anywhere else.  */
4983   if (TYPE_UNSIGNED (type))
4984     temp = fold_convert (signed_type_for (type), temp);
4985
4986   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
4987   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
4988   if (mask != 0)
4989     temp = const_binop (BIT_AND_EXPR, temp,
4990                         fold_convert (TREE_TYPE (c), mask),
4991                         0);
4992   /* If necessary, convert the type back to match the type of C.  */
4993   if (TYPE_UNSIGNED (type))
4994     temp = fold_convert (type, temp);
4995
4996   return fold_convert (type,
4997                        const_binop (BIT_XOR_EXPR, c, temp, 0));
4998 }
4999 \f
5000 /* Find ways of folding logical expressions of LHS and RHS:
5001    Try to merge two comparisons to the same innermost item.
5002    Look for range tests like "ch >= '0' && ch <= '9'".
5003    Look for combinations of simple terms on machines with expensive branches
5004    and evaluate the RHS unconditionally.
5005
5006    For example, if we have p->a == 2 && p->b == 4 and we can make an
5007    object large enough to span both A and B, we can do this with a comparison
5008    against the object ANDed with the a mask.
5009
5010    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5011    operations to do this with one comparison.
5012
5013    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5014    function and the one above.
5015
5016    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5017    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5018
5019    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5020    two operands.
5021
5022    We return the simplified tree or 0 if no optimization is possible.  */
5023
5024 static tree
5025 fold_truthop (location_t loc, enum tree_code code, tree truth_type,
5026               tree lhs, tree rhs)
5027 {
5028   /* If this is the "or" of two comparisons, we can do something if
5029      the comparisons are NE_EXPR.  If this is the "and", we can do something
5030      if the comparisons are EQ_EXPR.  I.e.,
5031         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5032
5033      WANTED_CODE is this operation code.  For single bit fields, we can
5034      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5035      comparison for one-bit fields.  */
5036
5037   enum tree_code wanted_code;
5038   enum tree_code lcode, rcode;
5039   tree ll_arg, lr_arg, rl_arg, rr_arg;
5040   tree ll_inner, lr_inner, rl_inner, rr_inner;
5041   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5042   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5043   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5044   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5045   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5046   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5047   enum machine_mode lnmode, rnmode;
5048   tree ll_mask, lr_mask, rl_mask, rr_mask;
5049   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5050   tree l_const, r_const;
5051   tree lntype, rntype, result;
5052   HOST_WIDE_INT first_bit, end_bit;
5053   int volatilep;
5054   tree orig_lhs = lhs, orig_rhs = rhs;
5055   enum tree_code orig_code = code;
5056
5057   /* Start by getting the comparison codes.  Fail if anything is volatile.
5058      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5059      it were surrounded with a NE_EXPR.  */
5060
5061   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5062     return 0;
5063
5064   lcode = TREE_CODE (lhs);
5065   rcode = TREE_CODE (rhs);
5066
5067   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5068     {
5069       lhs = build2 (NE_EXPR, truth_type, lhs,
5070                     build_int_cst (TREE_TYPE (lhs), 0));
5071       lcode = NE_EXPR;
5072     }
5073
5074   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5075     {
5076       rhs = build2 (NE_EXPR, truth_type, rhs,
5077                     build_int_cst (TREE_TYPE (rhs), 0));
5078       rcode = NE_EXPR;
5079     }
5080
5081   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5082       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5083     return 0;
5084
5085   ll_arg = TREE_OPERAND (lhs, 0);
5086   lr_arg = TREE_OPERAND (lhs, 1);
5087   rl_arg = TREE_OPERAND (rhs, 0);
5088   rr_arg = TREE_OPERAND (rhs, 1);
5089
5090   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5091   if (simple_operand_p (ll_arg)
5092       && simple_operand_p (lr_arg))
5093     {
5094       tree result;
5095       if (operand_equal_p (ll_arg, rl_arg, 0)
5096           && operand_equal_p (lr_arg, rr_arg, 0))
5097         {
5098           result = combine_comparisons (loc, code, lcode, rcode,
5099                                         truth_type, ll_arg, lr_arg);
5100           if (result)
5101             return result;
5102         }
5103       else if (operand_equal_p (ll_arg, rr_arg, 0)
5104                && operand_equal_p (lr_arg, rl_arg, 0))
5105         {
5106           result = combine_comparisons (loc, code, lcode,
5107                                         swap_tree_comparison (rcode),
5108                                         truth_type, ll_arg, lr_arg);
5109           if (result)
5110             return result;
5111         }
5112     }
5113
5114   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5115           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5116
5117   /* If the RHS can be evaluated unconditionally and its operands are
5118      simple, it wins to evaluate the RHS unconditionally on machines
5119      with expensive branches.  In this case, this isn't a comparison
5120      that can be merged.  Avoid doing this if the RHS is a floating-point
5121      comparison since those can trap.  */
5122
5123   if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5124                    false) >= 2
5125       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5126       && simple_operand_p (rl_arg)
5127       && simple_operand_p (rr_arg))
5128     {
5129       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5130       if (code == TRUTH_OR_EXPR
5131           && lcode == NE_EXPR && integer_zerop (lr_arg)
5132           && rcode == NE_EXPR && integer_zerop (rr_arg)
5133           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5134           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5135         {
5136           result = build2 (NE_EXPR, truth_type,
5137                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5138                                    ll_arg, rl_arg),
5139                            build_int_cst (TREE_TYPE (ll_arg), 0));
5140           goto fold_truthop_exit;
5141         }
5142
5143       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5144       if (code == TRUTH_AND_EXPR
5145           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5146           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5147           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5148           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5149         {
5150           result = build2 (EQ_EXPR, truth_type,
5151                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5152                                    ll_arg, rl_arg),
5153                            build_int_cst (TREE_TYPE (ll_arg), 0));
5154           goto fold_truthop_exit;
5155         }
5156
5157       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5158         {
5159           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5160             {
5161               result = build2 (code, truth_type, lhs, rhs);
5162               goto fold_truthop_exit;
5163             }
5164           return NULL_TREE;
5165         }
5166     }
5167
5168   /* See if the comparisons can be merged.  Then get all the parameters for
5169      each side.  */
5170
5171   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5172       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5173     return 0;
5174
5175   volatilep = 0;
5176   ll_inner = decode_field_reference (loc, ll_arg,
5177                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5178                                      &ll_unsignedp, &volatilep, &ll_mask,
5179                                      &ll_and_mask);
5180   lr_inner = decode_field_reference (loc, lr_arg,
5181                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5182                                      &lr_unsignedp, &volatilep, &lr_mask,
5183                                      &lr_and_mask);
5184   rl_inner = decode_field_reference (loc, rl_arg,
5185                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5186                                      &rl_unsignedp, &volatilep, &rl_mask,
5187                                      &rl_and_mask);
5188   rr_inner = decode_field_reference (loc, rr_arg,
5189                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5190                                      &rr_unsignedp, &volatilep, &rr_mask,
5191                                      &rr_and_mask);
5192
5193   /* It must be true that the inner operation on the lhs of each
5194      comparison must be the same if we are to be able to do anything.
5195      Then see if we have constants.  If not, the same must be true for
5196      the rhs's.  */
5197   if (volatilep || ll_inner == 0 || rl_inner == 0
5198       || ! operand_equal_p (ll_inner, rl_inner, 0))
5199     return 0;
5200
5201   if (TREE_CODE (lr_arg) == INTEGER_CST
5202       && TREE_CODE (rr_arg) == INTEGER_CST)
5203     l_const = lr_arg, r_const = rr_arg;
5204   else if (lr_inner == 0 || rr_inner == 0
5205            || ! operand_equal_p (lr_inner, rr_inner, 0))
5206     return 0;
5207   else
5208     l_const = r_const = 0;
5209
5210   /* If either comparison code is not correct for our logical operation,
5211      fail.  However, we can convert a one-bit comparison against zero into
5212      the opposite comparison against that bit being set in the field.  */
5213
5214   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5215   if (lcode != wanted_code)
5216     {
5217       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5218         {
5219           /* Make the left operand unsigned, since we are only interested
5220              in the value of one bit.  Otherwise we are doing the wrong
5221              thing below.  */
5222           ll_unsignedp = 1;
5223           l_const = ll_mask;
5224         }
5225       else
5226         return 0;
5227     }
5228
5229   /* This is analogous to the code for l_const above.  */
5230   if (rcode != wanted_code)
5231     {
5232       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5233         {
5234           rl_unsignedp = 1;
5235           r_const = rl_mask;
5236         }
5237       else
5238         return 0;
5239     }
5240
5241   /* See if we can find a mode that contains both fields being compared on
5242      the left.  If we can't, fail.  Otherwise, update all constants and masks
5243      to be relative to a field of that size.  */
5244   first_bit = MIN (ll_bitpos, rl_bitpos);
5245   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5246   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5247                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5248                           volatilep);
5249   if (lnmode == VOIDmode)
5250     return 0;
5251
5252   lnbitsize = GET_MODE_BITSIZE (lnmode);
5253   lnbitpos = first_bit & ~ (lnbitsize - 1);
5254   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5255   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5256
5257   if (BYTES_BIG_ENDIAN)
5258     {
5259       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5260       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5261     }
5262
5263   ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5264                          size_int (xll_bitpos), 0);
5265   rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5266                          size_int (xrl_bitpos), 0);
5267
5268   if (l_const)
5269     {
5270       l_const = fold_convert_loc (loc, lntype, l_const);
5271       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5272       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5273       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5274                                         fold_build1_loc (loc, BIT_NOT_EXPR,
5275                                                      lntype, ll_mask),
5276                                         0)))
5277         {
5278           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5279
5280           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5281         }
5282     }
5283   if (r_const)
5284     {
5285       r_const = fold_convert_loc (loc, lntype, r_const);
5286       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5287       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5288       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5289                                         fold_build1_loc (loc, BIT_NOT_EXPR,
5290                                                      lntype, rl_mask),
5291                                         0)))
5292         {
5293           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5294
5295           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5296         }
5297     }
5298
5299   /* If the right sides are not constant, do the same for it.  Also,
5300      disallow this optimization if a size or signedness mismatch occurs
5301      between the left and right sides.  */
5302   if (l_const == 0)
5303     {
5304       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5305           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5306           /* Make sure the two fields on the right
5307              correspond to the left without being swapped.  */
5308           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5309         return 0;
5310
5311       first_bit = MIN (lr_bitpos, rr_bitpos);
5312       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5313       rnmode = get_best_mode (end_bit - first_bit, first_bit,
5314                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5315                               volatilep);
5316       if (rnmode == VOIDmode)
5317         return 0;
5318
5319       rnbitsize = GET_MODE_BITSIZE (rnmode);
5320       rnbitpos = first_bit & ~ (rnbitsize - 1);
5321       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5322       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5323
5324       if (BYTES_BIG_ENDIAN)
5325         {
5326           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5327           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5328         }
5329
5330       lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5331                                                             rntype, lr_mask),
5332                              size_int (xlr_bitpos), 0);
5333       rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5334                                                             rntype, rr_mask),
5335                              size_int (xrr_bitpos), 0);
5336
5337       /* Make a mask that corresponds to both fields being compared.
5338          Do this for both items being compared.  If the operands are the
5339          same size and the bits being compared are in the same position
5340          then we can do this by masking both and comparing the masked
5341          results.  */
5342       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5343       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
5344       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5345         {
5346           lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5347                                     ll_unsignedp || rl_unsignedp);
5348           if (! all_ones_mask_p (ll_mask, lnbitsize))
5349             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5350
5351           rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5352                                     lr_unsignedp || rr_unsignedp);
5353           if (! all_ones_mask_p (lr_mask, rnbitsize))
5354             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5355
5356           result = build2 (wanted_code, truth_type, lhs, rhs);
5357           goto fold_truthop_exit;
5358         }
5359
5360       /* There is still another way we can do something:  If both pairs of
5361          fields being compared are adjacent, we may be able to make a wider
5362          field containing them both.
5363
5364          Note that we still must mask the lhs/rhs expressions.  Furthermore,
5365          the mask must be shifted to account for the shift done by
5366          make_bit_field_ref.  */
5367       if ((ll_bitsize + ll_bitpos == rl_bitpos
5368            && lr_bitsize + lr_bitpos == rr_bitpos)
5369           || (ll_bitpos == rl_bitpos + rl_bitsize
5370               && lr_bitpos == rr_bitpos + rr_bitsize))
5371         {
5372           tree type;
5373
5374           lhs = make_bit_field_ref (loc, ll_inner, lntype,
5375                                     ll_bitsize + rl_bitsize,
5376                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5377           rhs = make_bit_field_ref (loc, lr_inner, rntype,
5378                                     lr_bitsize + rr_bitsize,
5379                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5380
5381           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5382                                  size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
5383           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5384                                  size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
5385
5386           /* Convert to the smaller type before masking out unwanted bits.  */
5387           type = lntype;
5388           if (lntype != rntype)
5389             {
5390               if (lnbitsize > rnbitsize)
5391                 {
5392                   lhs = fold_convert_loc (loc, rntype, lhs);
5393                   ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5394                   type = rntype;
5395                 }
5396               else if (lnbitsize < rnbitsize)
5397                 {
5398                   rhs = fold_convert_loc (loc, lntype, rhs);
5399                   lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5400                   type = lntype;
5401                 }
5402             }
5403
5404           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5405             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5406
5407           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5408             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5409
5410           result = build2 (wanted_code, truth_type, lhs, rhs);
5411           goto fold_truthop_exit;
5412         }
5413
5414       return 0;
5415     }
5416
5417   /* Handle the case of comparisons with constants.  If there is something in
5418      common between the masks, those bits of the constants must be the same.
5419      If not, the condition is always false.  Test for this to avoid generating
5420      incorrect code below.  */
5421   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5422   if (! integer_zerop (result)
5423       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5424                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5425     {
5426       if (wanted_code == NE_EXPR)
5427         {
5428           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5429           return constant_boolean_node (true, truth_type);
5430         }
5431       else
5432         {
5433           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5434           return constant_boolean_node (false, truth_type);
5435         }
5436     }
5437
5438   /* Construct the expression we will return.  First get the component
5439      reference we will make.  Unless the mask is all ones the width of
5440      that field, perform the mask operation.  Then compare with the
5441      merged constant.  */
5442   result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5443                                ll_unsignedp || rl_unsignedp);
5444
5445   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5446   if (! all_ones_mask_p (ll_mask, lnbitsize))
5447     {
5448       result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5449       SET_EXPR_LOCATION (result, loc);
5450     }
5451
5452   result = build2 (wanted_code, truth_type, result,
5453                    const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
5454
5455  fold_truthop_exit:
5456   SET_EXPR_LOCATION (result, loc);
5457   return result;
5458 }
5459 \f
5460 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5461    constant.  */
5462
5463 static tree
5464 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5465                             tree op0, tree op1)
5466 {
5467   tree arg0 = op0;
5468   enum tree_code op_code;
5469   tree comp_const;
5470   tree minmax_const;
5471   int consts_equal, consts_lt;
5472   tree inner;
5473
5474   STRIP_SIGN_NOPS (arg0);
5475
5476   op_code = TREE_CODE (arg0);
5477   minmax_const = TREE_OPERAND (arg0, 1);
5478   comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5479   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5480   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5481   inner = TREE_OPERAND (arg0, 0);
5482
5483   /* If something does not permit us to optimize, return the original tree.  */
5484   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5485       || TREE_CODE (comp_const) != INTEGER_CST
5486       || TREE_OVERFLOW (comp_const)
5487       || TREE_CODE (minmax_const) != INTEGER_CST
5488       || TREE_OVERFLOW (minmax_const))
5489     return NULL_TREE;
5490
5491   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5492      and GT_EXPR, doing the rest with recursive calls using logical
5493      simplifications.  */
5494   switch (code)
5495     {
5496     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5497       {
5498         tree tem
5499           = optimize_minmax_comparison (loc,
5500                                         invert_tree_comparison (code, false),
5501                                         type, op0, op1);
5502         if (tem)
5503           return invert_truthvalue_loc (loc, tem);
5504         return NULL_TREE;
5505       }
5506
5507     case GE_EXPR:
5508       return
5509         fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5510                      optimize_minmax_comparison
5511                      (loc, EQ_EXPR, type, arg0, comp_const),
5512                      optimize_minmax_comparison
5513                      (loc, GT_EXPR, type, arg0, comp_const));
5514
5515     case EQ_EXPR:
5516       if (op_code == MAX_EXPR && consts_equal)
5517         /* MAX (X, 0) == 0  ->  X <= 0  */
5518         return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5519
5520       else if (op_code == MAX_EXPR && consts_lt)
5521         /* MAX (X, 0) == 5  ->  X == 5   */
5522         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5523
5524       else if (op_code == MAX_EXPR)
5525         /* MAX (X, 0) == -1  ->  false  */
5526         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5527
5528       else if (consts_equal)
5529         /* MIN (X, 0) == 0  ->  X >= 0  */
5530         return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5531
5532       else if (consts_lt)
5533         /* MIN (X, 0) == 5  ->  false  */
5534         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5535
5536       else
5537         /* MIN (X, 0) == -1  ->  X == -1  */
5538         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5539
5540     case GT_EXPR:
5541       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5542         /* MAX (X, 0) > 0  ->  X > 0
5543            MAX (X, 0) > 5  ->  X > 5  */
5544         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5545
5546       else if (op_code == MAX_EXPR)
5547         /* MAX (X, 0) > -1  ->  true  */
5548         return omit_one_operand_loc (loc, type, integer_one_node, inner);
5549
5550       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5551         /* MIN (X, 0) > 0  ->  false
5552            MIN (X, 0) > 5  ->  false  */
5553         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5554
5555       else
5556         /* MIN (X, 0) > -1  ->  X > -1  */
5557         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5558
5559     default:
5560       return NULL_TREE;
5561     }
5562 }
5563 \f
5564 /* T is an integer expression that is being multiplied, divided, or taken a
5565    modulus (CODE says which and what kind of divide or modulus) by a
5566    constant C.  See if we can eliminate that operation by folding it with
5567    other operations already in T.  WIDE_TYPE, if non-null, is a type that
5568    should be used for the computation if wider than our type.
5569
5570    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5571    (X * 2) + (Y * 4).  We must, however, be assured that either the original
5572    expression would not overflow or that overflow is undefined for the type
5573    in the language in question.
5574
5575    If we return a non-null expression, it is an equivalent form of the
5576    original computation, but need not be in the original type.
5577
5578    We set *STRICT_OVERFLOW_P to true if the return values depends on
5579    signed overflow being undefined.  Otherwise we do not change
5580    *STRICT_OVERFLOW_P.  */
5581
5582 static tree
5583 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5584                 bool *strict_overflow_p)
5585 {
5586   /* To avoid exponential search depth, refuse to allow recursion past
5587      three levels.  Beyond that (1) it's highly unlikely that we'll find
5588      something interesting and (2) we've probably processed it before
5589      when we built the inner expression.  */
5590
5591   static int depth;
5592   tree ret;
5593
5594   if (depth > 3)
5595     return NULL;
5596
5597   depth++;
5598   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5599   depth--;
5600
5601   return ret;
5602 }
5603
5604 static tree
5605 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5606                   bool *strict_overflow_p)
5607 {
5608   tree type = TREE_TYPE (t);
5609   enum tree_code tcode = TREE_CODE (t);
5610   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5611                                    > GET_MODE_SIZE (TYPE_MODE (type)))
5612                 ? wide_type : type);
5613   tree t1, t2;
5614   int same_p = tcode == code;
5615   tree op0 = NULL_TREE, op1 = NULL_TREE;
5616   bool sub_strict_overflow_p;
5617
5618   /* Don't deal with constants of zero here; they confuse the code below.  */
5619   if (integer_zerop (c))
5620     return NULL_TREE;
5621
5622   if (TREE_CODE_CLASS (tcode) == tcc_unary)
5623     op0 = TREE_OPERAND (t, 0);
5624
5625   if (TREE_CODE_CLASS (tcode) == tcc_binary)
5626     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5627
5628   /* Note that we need not handle conditional operations here since fold
5629      already handles those cases.  So just do arithmetic here.  */
5630   switch (tcode)
5631     {
5632     case INTEGER_CST:
5633       /* For a constant, we can always simplify if we are a multiply
5634          or (for divide and modulus) if it is a multiple of our constant.  */
5635       if (code == MULT_EXPR
5636           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
5637         return const_binop (code, fold_convert (ctype, t),
5638                             fold_convert (ctype, c), 0);
5639       break;
5640
5641     CASE_CONVERT: case NON_LVALUE_EXPR:
5642       /* If op0 is an expression ...  */
5643       if ((COMPARISON_CLASS_P (op0)
5644            || UNARY_CLASS_P (op0)
5645            || BINARY_CLASS_P (op0)
5646            || VL_EXP_CLASS_P (op0)
5647            || EXPRESSION_CLASS_P (op0))
5648           /* ... and has wrapping overflow, and its type is smaller
5649              than ctype, then we cannot pass through as widening.  */
5650           && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
5651                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5652                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5653                && (TYPE_PRECISION (ctype)
5654                    > TYPE_PRECISION (TREE_TYPE (op0))))
5655               /* ... or this is a truncation (t is narrower than op0),
5656                  then we cannot pass through this narrowing.  */
5657               || (TYPE_PRECISION (type)
5658                   < TYPE_PRECISION (TREE_TYPE (op0)))
5659               /* ... or signedness changes for division or modulus,
5660                  then we cannot pass through this conversion.  */
5661               || (code != MULT_EXPR
5662                   && (TYPE_UNSIGNED (ctype)
5663                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
5664               /* ... or has undefined overflow while the converted to
5665                  type has not, we cannot do the operation in the inner type
5666                  as that would introduce undefined overflow.  */
5667               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5668                   && !TYPE_OVERFLOW_UNDEFINED (type))))
5669         break;
5670
5671       /* Pass the constant down and see if we can make a simplification.  If
5672          we can, replace this expression with the inner simplification for
5673          possible later conversion to our or some other type.  */
5674       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5675           && TREE_CODE (t2) == INTEGER_CST
5676           && !TREE_OVERFLOW (t2)
5677           && (0 != (t1 = extract_muldiv (op0, t2, code,
5678                                          code == MULT_EXPR
5679                                          ? ctype : NULL_TREE,
5680                                          strict_overflow_p))))
5681         return t1;
5682       break;
5683
5684     case ABS_EXPR:
5685       /* If widening the type changes it from signed to unsigned, then we
5686          must avoid building ABS_EXPR itself as unsigned.  */
5687       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5688         {
5689           tree cstype = (*signed_type_for) (ctype);
5690           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5691               != 0)
5692             {
5693               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5694               return fold_convert (ctype, t1);
5695             }
5696           break;
5697         }
5698       /* If the constant is negative, we cannot simplify this.  */
5699       if (tree_int_cst_sgn (c) == -1)
5700         break;
5701       /* FALLTHROUGH */
5702     case NEGATE_EXPR:
5703       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5704           != 0)
5705         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5706       break;
5707
5708     case MIN_EXPR:  case MAX_EXPR:
5709       /* If widening the type changes the signedness, then we can't perform
5710          this optimization as that changes the result.  */
5711       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5712         break;
5713
5714       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
5715       sub_strict_overflow_p = false;
5716       if ((t1 = extract_muldiv (op0, c, code, wide_type,
5717                                 &sub_strict_overflow_p)) != 0
5718           && (t2 = extract_muldiv (op1, c, code, wide_type,
5719                                    &sub_strict_overflow_p)) != 0)
5720         {
5721           if (tree_int_cst_sgn (c) < 0)
5722             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5723           if (sub_strict_overflow_p)
5724             *strict_overflow_p = true;
5725           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5726                               fold_convert (ctype, t2));
5727         }
5728       break;
5729
5730     case LSHIFT_EXPR:  case RSHIFT_EXPR:
5731       /* If the second operand is constant, this is a multiplication
5732          or floor division, by a power of two, so we can treat it that
5733          way unless the multiplier or divisor overflows.  Signed
5734          left-shift overflow is implementation-defined rather than
5735          undefined in C90, so do not convert signed left shift into
5736          multiplication.  */
5737       if (TREE_CODE (op1) == INTEGER_CST
5738           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5739           /* const_binop may not detect overflow correctly,
5740              so check for it explicitly here.  */
5741           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5742           && TREE_INT_CST_HIGH (op1) == 0
5743           && 0 != (t1 = fold_convert (ctype,
5744                                       const_binop (LSHIFT_EXPR,
5745                                                    size_one_node,
5746                                                    op1, 0)))
5747           && !TREE_OVERFLOW (t1))
5748         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5749                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
5750                                        ctype,
5751                                        fold_convert (ctype, op0),
5752                                        t1),
5753                                c, code, wide_type, strict_overflow_p);
5754       break;
5755
5756     case PLUS_EXPR:  case MINUS_EXPR:
5757       /* See if we can eliminate the operation on both sides.  If we can, we
5758          can return a new PLUS or MINUS.  If we can't, the only remaining
5759          cases where we can do anything are if the second operand is a
5760          constant.  */
5761       sub_strict_overflow_p = false;
5762       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5763       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5764       if (t1 != 0 && t2 != 0
5765           && (code == MULT_EXPR
5766               /* If not multiplication, we can only do this if both operands
5767                  are divisible by c.  */
5768               || (multiple_of_p (ctype, op0, c)
5769                   && multiple_of_p (ctype, op1, c))))
5770         {
5771           if (sub_strict_overflow_p)
5772             *strict_overflow_p = true;
5773           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5774                               fold_convert (ctype, t2));
5775         }
5776
5777       /* If this was a subtraction, negate OP1 and set it to be an addition.
5778          This simplifies the logic below.  */
5779       if (tcode == MINUS_EXPR)
5780         {
5781           tcode = PLUS_EXPR, op1 = negate_expr (op1);
5782           /* If OP1 was not easily negatable, the constant may be OP0.  */
5783           if (TREE_CODE (op0) == INTEGER_CST)
5784             {
5785               tree tem = op0;
5786               op0 = op1;
5787               op1 = tem;
5788               tem = t1;
5789               t1 = t2;
5790               t2 = tem;
5791             }
5792         }
5793
5794       if (TREE_CODE (op1) != INTEGER_CST)
5795         break;
5796
5797       /* If either OP1 or C are negative, this optimization is not safe for
5798          some of the division and remainder types while for others we need
5799          to change the code.  */
5800       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5801         {
5802           if (code == CEIL_DIV_EXPR)
5803             code = FLOOR_DIV_EXPR;
5804           else if (code == FLOOR_DIV_EXPR)
5805             code = CEIL_DIV_EXPR;
5806           else if (code != MULT_EXPR
5807                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5808             break;
5809         }
5810
5811       /* If it's a multiply or a division/modulus operation of a multiple
5812          of our constant, do the operation and verify it doesn't overflow.  */
5813       if (code == MULT_EXPR
5814           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5815         {
5816           op1 = const_binop (code, fold_convert (ctype, op1),
5817                              fold_convert (ctype, c), 0);
5818           /* We allow the constant to overflow with wrapping semantics.  */
5819           if (op1 == 0
5820               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5821             break;
5822         }
5823       else
5824         break;
5825
5826       /* If we have an unsigned type is not a sizetype, we cannot widen
5827          the operation since it will change the result if the original
5828          computation overflowed.  */
5829       if (TYPE_UNSIGNED (ctype)
5830           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5831           && ctype != type)
5832         break;
5833
5834       /* If we were able to eliminate our operation from the first side,
5835          apply our operation to the second side and reform the PLUS.  */
5836       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5837         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5838
5839       /* The last case is if we are a multiply.  In that case, we can
5840          apply the distributive law to commute the multiply and addition
5841          if the multiplication of the constants doesn't overflow.  */
5842       if (code == MULT_EXPR)
5843         return fold_build2 (tcode, ctype,
5844                             fold_build2 (code, ctype,
5845                                          fold_convert (ctype, op0),
5846                                          fold_convert (ctype, c)),
5847                             op1);
5848
5849       break;
5850
5851     case MULT_EXPR:
5852       /* We have a special case here if we are doing something like
5853          (C * 8) % 4 since we know that's zero.  */
5854       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5855            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5856           /* If the multiplication can overflow we cannot optimize this.
5857              ???  Until we can properly mark individual operations as
5858              not overflowing we need to treat sizetype special here as
5859              stor-layout relies on this opimization to make
5860              DECL_FIELD_BIT_OFFSET always a constant.  */
5861           && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
5862               || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
5863                   && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
5864           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5865           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5866         {
5867           *strict_overflow_p = true;
5868           return omit_one_operand (type, integer_zero_node, op0);
5869         }
5870
5871       /* ... fall through ...  */
5872
5873     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
5874     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
5875       /* If we can extract our operation from the LHS, do so and return a
5876          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
5877          do something only if the second operand is a constant.  */
5878       if (same_p
5879           && (t1 = extract_muldiv (op0, c, code, wide_type,
5880                                    strict_overflow_p)) != 0)
5881         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5882                             fold_convert (ctype, op1));
5883       else if (tcode == MULT_EXPR && code == MULT_EXPR
5884                && (t1 = extract_muldiv (op1, c, code, wide_type,
5885                                         strict_overflow_p)) != 0)
5886         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5887                             fold_convert (ctype, t1));
5888       else if (TREE_CODE (op1) != INTEGER_CST)
5889         return 0;
5890
5891       /* If these are the same operation types, we can associate them
5892          assuming no overflow.  */
5893       if (tcode == code
5894           && 0 != (t1 = int_const_binop (MULT_EXPR,
5895                                          fold_convert (ctype, op1),
5896                                          fold_convert (ctype, c), 1))
5897           && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
5898                                                TREE_INT_CST_HIGH (t1),
5899                                                (TYPE_UNSIGNED (ctype)
5900                                                 && tcode != MULT_EXPR) ? -1 : 1,
5901                                                TREE_OVERFLOW (t1)))
5902           && !TREE_OVERFLOW (t1))
5903         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
5904
5905       /* If these operations "cancel" each other, we have the main
5906          optimizations of this pass, which occur when either constant is a
5907          multiple of the other, in which case we replace this with either an
5908          operation or CODE or TCODE.
5909
5910          If we have an unsigned type that is not a sizetype, we cannot do
5911          this since it will change the result if the original computation
5912          overflowed.  */
5913       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
5914            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5915           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5916               || (tcode == MULT_EXPR
5917                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5918                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
5919                   && code != MULT_EXPR)))
5920         {
5921           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5922             {
5923               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5924                 *strict_overflow_p = true;
5925               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5926                                   fold_convert (ctype,
5927                                                 const_binop (TRUNC_DIV_EXPR,
5928                                                              op1, c, 0)));
5929             }
5930           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
5931             {
5932               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5933                 *strict_overflow_p = true;
5934               return fold_build2 (code, ctype, fold_convert (ctype, op0),
5935                                   fold_convert (ctype,
5936                                                 const_binop (TRUNC_DIV_EXPR,
5937                                                              c, op1, 0)));
5938             }
5939         }
5940       break;
5941
5942     default:
5943       break;
5944     }
5945
5946   return 0;
5947 }
5948 \f
5949 /* Return a node which has the indicated constant VALUE (either 0 or
5950    1), and is of the indicated TYPE.  */
5951
5952 tree
5953 constant_boolean_node (int value, tree type)
5954 {
5955   if (type == integer_type_node)
5956     return value ? integer_one_node : integer_zero_node;
5957   else if (type == boolean_type_node)
5958     return value ? boolean_true_node : boolean_false_node;
5959   else
5960     return build_int_cst (type, value);
5961 }
5962
5963
5964 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
5965    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
5966    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
5967    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
5968    COND is the first argument to CODE; otherwise (as in the example
5969    given here), it is the second argument.  TYPE is the type of the
5970    original expression.  Return NULL_TREE if no simplification is
5971    possible.  */
5972
5973 static tree
5974 fold_binary_op_with_conditional_arg (location_t loc,
5975                                      enum tree_code code,
5976                                      tree type, tree op0, tree op1,
5977                                      tree cond, tree arg, int cond_first_p)
5978 {
5979   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
5980   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
5981   tree test, true_value, false_value;
5982   tree lhs = NULL_TREE;
5983   tree rhs = NULL_TREE;
5984
5985   if (TREE_CODE (cond) == COND_EXPR)
5986     {
5987       test = TREE_OPERAND (cond, 0);
5988       true_value = TREE_OPERAND (cond, 1);
5989       false_value = TREE_OPERAND (cond, 2);
5990       /* If this operand throws an expression, then it does not make
5991          sense to try to perform a logical or arithmetic operation
5992          involving it.  */
5993       if (VOID_TYPE_P (TREE_TYPE (true_value)))
5994         lhs = true_value;
5995       if (VOID_TYPE_P (TREE_TYPE (false_value)))
5996         rhs = false_value;
5997     }
5998   else
5999     {
6000       tree testtype = TREE_TYPE (cond);
6001       test = cond;
6002       true_value = constant_boolean_node (true, testtype);
6003       false_value = constant_boolean_node (false, testtype);
6004     }
6005
6006   /* This transformation is only worthwhile if we don't have to wrap ARG
6007      in a SAVE_EXPR and the operation can be simplified on at least one
6008      of the branches once its pushed inside the COND_EXPR.  */
6009   if (!TREE_CONSTANT (arg)
6010       && (TREE_SIDE_EFFECTS (arg)
6011           || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6012     return NULL_TREE;
6013
6014   arg = fold_convert_loc (loc, arg_type, arg);
6015   if (lhs == 0)
6016     {
6017       true_value = fold_convert_loc (loc, cond_type, true_value);
6018       if (cond_first_p)
6019         lhs = fold_build2_loc (loc, code, type, true_value, arg);
6020       else
6021         lhs = fold_build2_loc (loc, code, type, arg, true_value);
6022     }
6023   if (rhs == 0)
6024     {
6025       false_value = fold_convert_loc (loc, cond_type, false_value);
6026       if (cond_first_p)
6027         rhs = fold_build2_loc (loc, code, type, false_value, arg);
6028       else
6029         rhs = fold_build2_loc (loc, code, type, arg, false_value);
6030     }
6031
6032   /* Check that we have simplified at least one of the branches.  */
6033   if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6034     return NULL_TREE;
6035
6036   return fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs);
6037 }
6038
6039 \f
6040 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6041
6042    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6043    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6044    ADDEND is the same as X.
6045
6046    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6047    and finite.  The problematic cases are when X is zero, and its mode
6048    has signed zeros.  In the case of rounding towards -infinity,
6049    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6050    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6051
6052 bool
6053 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6054 {
6055   if (!real_zerop (addend))
6056     return false;
6057
6058   /* Don't allow the fold with -fsignaling-nans.  */
6059   if (HONOR_SNANS (TYPE_MODE (type)))
6060     return false;
6061
6062   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6063   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6064     return true;
6065
6066   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6067   if (TREE_CODE (addend) == REAL_CST
6068       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6069     negate = !negate;
6070
6071   /* The mode has signed zeros, and we have to honor their sign.
6072      In this situation, there is only one case we can return true for.
6073      X - 0 is the same as X unless rounding towards -infinity is
6074      supported.  */
6075   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6076 }
6077
6078 /* Subroutine of fold() that checks comparisons of built-in math
6079    functions against real constants.
6080
6081    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6082    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6083    is the type of the result and ARG0 and ARG1 are the operands of the
6084    comparison.  ARG1 must be a TREE_REAL_CST.
6085
6086    The function returns the constant folded tree if a simplification
6087    can be made, and NULL_TREE otherwise.  */
6088
6089 static tree
6090 fold_mathfn_compare (location_t loc,
6091                      enum built_in_function fcode, enum tree_code code,
6092                      tree type, tree arg0, tree arg1)
6093 {
6094   REAL_VALUE_TYPE c;
6095
6096   if (BUILTIN_SQRT_P (fcode))
6097     {
6098       tree arg = CALL_EXPR_ARG (arg0, 0);
6099       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6100
6101       c = TREE_REAL_CST (arg1);
6102       if (REAL_VALUE_NEGATIVE (c))
6103         {
6104           /* sqrt(x) < y is always false, if y is negative.  */
6105           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6106             return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6107
6108           /* sqrt(x) > y is always true, if y is negative and we
6109              don't care about NaNs, i.e. negative values of x.  */
6110           if (code == NE_EXPR || !HONOR_NANS (mode))
6111             return omit_one_operand_loc (loc, type, integer_one_node, arg);
6112
6113           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6114           return fold_build2_loc (loc, GE_EXPR, type, arg,
6115                               build_real (TREE_TYPE (arg), dconst0));
6116         }
6117       else if (code == GT_EXPR || code == GE_EXPR)
6118         {
6119           REAL_VALUE_TYPE c2;
6120
6121           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6122           real_convert (&c2, mode, &c2);
6123
6124           if (REAL_VALUE_ISINF (c2))
6125             {
6126               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6127               if (HONOR_INFINITIES (mode))
6128                 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6129                                     build_real (TREE_TYPE (arg), c2));
6130
6131               /* sqrt(x) > y is always false, when y is very large
6132                  and we don't care about infinities.  */
6133               return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6134             }
6135
6136           /* sqrt(x) > c is the same as x > c*c.  */
6137           return fold_build2_loc (loc, code, type, arg,
6138                               build_real (TREE_TYPE (arg), c2));
6139         }
6140       else if (code == LT_EXPR || code == LE_EXPR)
6141         {
6142           REAL_VALUE_TYPE c2;
6143
6144           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6145           real_convert (&c2, mode, &c2);
6146
6147           if (REAL_VALUE_ISINF (c2))
6148             {
6149               /* sqrt(x) < y is always true, when y is a very large
6150                  value and we don't care about NaNs or Infinities.  */
6151               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6152                 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6153
6154               /* sqrt(x) < y is x != +Inf when y is very large and we
6155                  don't care about NaNs.  */
6156               if (! HONOR_NANS (mode))
6157                 return fold_build2_loc (loc, NE_EXPR, type, arg,
6158                                     build_real (TREE_TYPE (arg), c2));
6159
6160               /* sqrt(x) < y is x >= 0 when y is very large and we
6161                  don't care about Infinities.  */
6162               if (! HONOR_INFINITIES (mode))
6163                 return fold_build2_loc (loc, GE_EXPR, type, arg,
6164                                     build_real (TREE_TYPE (arg), dconst0));
6165
6166               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6167               if (lang_hooks.decls.global_bindings_p () != 0
6168                   || CONTAINS_PLACEHOLDER_P (arg))
6169                 return NULL_TREE;
6170
6171               arg = save_expr (arg);
6172               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6173                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6174                                                build_real (TREE_TYPE (arg),
6175                                                            dconst0)),
6176                                   fold_build2_loc (loc, NE_EXPR, type, arg,
6177                                                build_real (TREE_TYPE (arg),
6178                                                            c2)));
6179             }
6180
6181           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6182           if (! HONOR_NANS (mode))
6183             return fold_build2_loc (loc, code, type, arg,
6184                                 build_real (TREE_TYPE (arg), c2));
6185
6186           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6187           if (lang_hooks.decls.global_bindings_p () == 0
6188               && ! CONTAINS_PLACEHOLDER_P (arg))
6189             {
6190               arg = save_expr (arg);
6191               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6192                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6193                                                build_real (TREE_TYPE (arg),
6194                                                            dconst0)),
6195                                   fold_build2_loc (loc, code, type, arg,
6196                                                build_real (TREE_TYPE (arg),
6197                                                            c2)));
6198             }
6199         }
6200     }
6201
6202   return NULL_TREE;
6203 }
6204
6205 /* Subroutine of fold() that optimizes comparisons against Infinities,
6206    either +Inf or -Inf.
6207
6208    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6209    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6210    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6211
6212    The function returns the constant folded tree if a simplification
6213    can be made, and NULL_TREE otherwise.  */
6214
6215 static tree
6216 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6217                   tree arg0, tree arg1)
6218 {
6219   enum machine_mode mode;
6220   REAL_VALUE_TYPE max;
6221   tree temp;
6222   bool neg;
6223
6224   mode = TYPE_MODE (TREE_TYPE (arg0));
6225
6226   /* For negative infinity swap the sense of the comparison.  */
6227   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6228   if (neg)
6229     code = swap_tree_comparison (code);
6230
6231   switch (code)
6232     {
6233     case GT_EXPR:
6234       /* x > +Inf is always false, if with ignore sNANs.  */
6235       if (HONOR_SNANS (mode))
6236         return NULL_TREE;
6237       return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6238
6239     case LE_EXPR:
6240       /* x <= +Inf is always true, if we don't case about NaNs.  */
6241       if (! HONOR_NANS (mode))
6242         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6243
6244       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6245       if (lang_hooks.decls.global_bindings_p () == 0
6246           && ! CONTAINS_PLACEHOLDER_P (arg0))
6247         {
6248           arg0 = save_expr (arg0);
6249           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6250         }
6251       break;
6252
6253     case EQ_EXPR:
6254     case GE_EXPR:
6255       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6256       real_maxval (&max, neg, mode);
6257       return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6258                           arg0, build_real (TREE_TYPE (arg0), max));
6259
6260     case LT_EXPR:
6261       /* x < +Inf is always equal to x <= DBL_MAX.  */
6262       real_maxval (&max, neg, mode);
6263       return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6264                           arg0, build_real (TREE_TYPE (arg0), max));
6265
6266     case NE_EXPR:
6267       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6268       real_maxval (&max, neg, mode);
6269       if (! HONOR_NANS (mode))
6270         return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6271                             arg0, build_real (TREE_TYPE (arg0), max));
6272
6273       temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6274                           arg0, build_real (TREE_TYPE (arg0), max));
6275       return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
6276
6277     default:
6278       break;
6279     }
6280
6281   return NULL_TREE;
6282 }
6283
6284 /* Subroutine of fold() that optimizes comparisons of a division by
6285    a nonzero integer constant against an integer constant, i.e.
6286    X/C1 op C2.
6287
6288    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6289    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6290    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6291
6292    The function returns the constant folded tree if a simplification
6293    can be made, and NULL_TREE otherwise.  */
6294
6295 static tree
6296 fold_div_compare (location_t loc,
6297                   enum tree_code code, tree type, tree arg0, tree arg1)
6298 {
6299   tree prod, tmp, hi, lo;
6300   tree arg00 = TREE_OPERAND (arg0, 0);
6301   tree arg01 = TREE_OPERAND (arg0, 1);
6302   unsigned HOST_WIDE_INT lpart;
6303   HOST_WIDE_INT hpart;
6304   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6305   bool neg_overflow;
6306   int overflow;
6307
6308   /* We have to do this the hard way to detect unsigned overflow.
6309      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
6310   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6311                                    TREE_INT_CST_HIGH (arg01),
6312                                    TREE_INT_CST_LOW (arg1),
6313                                    TREE_INT_CST_HIGH (arg1),
6314                                    &lpart, &hpart, unsigned_p);
6315   prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6316                                 -1, overflow);
6317   neg_overflow = false;
6318
6319   if (unsigned_p)
6320     {
6321       tmp = int_const_binop (MINUS_EXPR, arg01,
6322                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6323       lo = prod;
6324
6325       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
6326       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6327                                        TREE_INT_CST_HIGH (prod),
6328                                        TREE_INT_CST_LOW (tmp),
6329                                        TREE_INT_CST_HIGH (tmp),
6330                                        &lpart, &hpart, unsigned_p);
6331       hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6332                                   -1, overflow | TREE_OVERFLOW (prod));
6333     }
6334   else if (tree_int_cst_sgn (arg01) >= 0)
6335     {
6336       tmp = int_const_binop (MINUS_EXPR, arg01,
6337                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6338       switch (tree_int_cst_sgn (arg1))
6339         {
6340         case -1:
6341           neg_overflow = true;
6342           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6343           hi = prod;
6344           break;
6345
6346         case  0:
6347           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6348           hi = tmp;
6349           break;
6350
6351         case  1:
6352           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6353           lo = prod;
6354           break;
6355
6356         default:
6357           gcc_unreachable ();
6358         }
6359     }
6360   else
6361     {
6362       /* A negative divisor reverses the relational operators.  */
6363       code = swap_tree_comparison (code);
6364
6365       tmp = int_const_binop (PLUS_EXPR, arg01,
6366                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6367       switch (tree_int_cst_sgn (arg1))
6368         {
6369         case -1:
6370           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6371           lo = prod;
6372           break;
6373
6374         case  0:
6375           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6376           lo = tmp;
6377           break;
6378
6379         case  1:
6380           neg_overflow = true;
6381           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6382           hi = prod;
6383           break;
6384
6385         default:
6386           gcc_unreachable ();
6387         }
6388     }
6389
6390   switch (code)
6391     {
6392     case EQ_EXPR:
6393       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6394         return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6395       if (TREE_OVERFLOW (hi))
6396         return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6397       if (TREE_OVERFLOW (lo))
6398         return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6399       return build_range_check (loc, type, arg00, 1, lo, hi);
6400
6401     case NE_EXPR:
6402       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6403         return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6404       if (TREE_OVERFLOW (hi))
6405         return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6406       if (TREE_OVERFLOW (lo))
6407         return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6408       return build_range_check (loc, type, arg00, 0, lo, hi);
6409
6410     case LT_EXPR:
6411       if (TREE_OVERFLOW (lo))
6412         {
6413           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6414           return omit_one_operand_loc (loc, type, tmp, arg00);
6415         }
6416       return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6417
6418     case LE_EXPR:
6419       if (TREE_OVERFLOW (hi))
6420         {
6421           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6422           return omit_one_operand_loc (loc, type, tmp, arg00);
6423         }
6424       return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6425
6426     case GT_EXPR:
6427       if (TREE_OVERFLOW (hi))
6428         {
6429           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6430           return omit_one_operand_loc (loc, type, tmp, arg00);
6431         }
6432       return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6433
6434     case GE_EXPR:
6435       if (TREE_OVERFLOW (lo))
6436         {
6437           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6438           return omit_one_operand_loc (loc, type, tmp, arg00);
6439         }
6440       return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6441
6442     default:
6443       break;
6444     }
6445
6446   return NULL_TREE;
6447 }
6448
6449
6450 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6451    equality/inequality test, then return a simplified form of the test
6452    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6453    result type.  */
6454
6455 static tree
6456 fold_single_bit_test_into_sign_test (location_t loc,
6457                                      enum tree_code code, tree arg0, tree arg1,
6458                                      tree result_type)
6459 {
6460   /* If this is testing a single bit, we can optimize the test.  */
6461   if ((code == NE_EXPR || code == EQ_EXPR)
6462       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6463       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6464     {
6465       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6466          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6467       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6468
6469       if (arg00 != NULL_TREE
6470           /* This is only a win if casting to a signed type is cheap,
6471              i.e. when arg00's type is not a partial mode.  */
6472           && TYPE_PRECISION (TREE_TYPE (arg00))
6473              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6474         {
6475           tree stype = signed_type_for (TREE_TYPE (arg00));
6476           return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6477                               result_type,
6478                               fold_convert_loc (loc, stype, arg00),
6479                               build_int_cst (stype, 0));
6480         }
6481     }
6482
6483   return NULL_TREE;
6484 }
6485
6486 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6487    equality/inequality test, then return a simplified form of
6488    the test using shifts and logical operations.  Otherwise return
6489    NULL.  TYPE is the desired result type.  */
6490
6491 tree
6492 fold_single_bit_test (location_t loc, enum tree_code code,
6493                       tree arg0, tree arg1, tree result_type)
6494 {
6495   /* If this is testing a single bit, we can optimize the test.  */
6496   if ((code == NE_EXPR || code == EQ_EXPR)
6497       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6498       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6499     {
6500       tree inner = TREE_OPERAND (arg0, 0);
6501       tree type = TREE_TYPE (arg0);
6502       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6503       enum machine_mode operand_mode = TYPE_MODE (type);
6504       int ops_unsigned;
6505       tree signed_type, unsigned_type, intermediate_type;
6506       tree tem, one;
6507
6508       /* First, see if we can fold the single bit test into a sign-bit
6509          test.  */
6510       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6511                                                  result_type);
6512       if (tem)
6513         return tem;
6514
6515       /* Otherwise we have (A & C) != 0 where C is a single bit,
6516          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6517          Similarly for (A & C) == 0.  */
6518
6519       /* If INNER is a right shift of a constant and it plus BITNUM does
6520          not overflow, adjust BITNUM and INNER.  */
6521       if (TREE_CODE (inner) == RSHIFT_EXPR
6522           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6523           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6524           && bitnum < TYPE_PRECISION (type)
6525           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6526                                    bitnum - TYPE_PRECISION (type)))
6527         {
6528           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6529           inner = TREE_OPERAND (inner, 0);
6530         }
6531
6532       /* If we are going to be able to omit the AND below, we must do our
6533          operations as unsigned.  If we must use the AND, we have a choice.
6534          Normally unsigned is faster, but for some machines signed is.  */
6535 #ifdef LOAD_EXTEND_OP
6536       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6537                       && !flag_syntax_only) ? 0 : 1;
6538 #else
6539       ops_unsigned = 1;
6540 #endif
6541
6542       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6543       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6544       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6545       inner = fold_convert_loc (loc, intermediate_type, inner);
6546
6547       if (bitnum != 0)
6548         inner = build2 (RSHIFT_EXPR, intermediate_type,
6549                         inner, size_int (bitnum));
6550
6551       one = build_int_cst (intermediate_type, 1);
6552
6553       if (code == EQ_EXPR)
6554         inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6555
6556       /* Put the AND last so it can combine with more things.  */
6557       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6558
6559       /* Make sure to return the proper type.  */
6560       inner = fold_convert_loc (loc, result_type, inner);
6561
6562       return inner;
6563     }
6564   return NULL_TREE;
6565 }
6566
6567 /* Check whether we are allowed to reorder operands arg0 and arg1,
6568    such that the evaluation of arg1 occurs before arg0.  */
6569
6570 static bool
6571 reorder_operands_p (const_tree arg0, const_tree arg1)
6572 {
6573   if (! flag_evaluation_order)
6574       return true;
6575   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6576     return true;
6577   return ! TREE_SIDE_EFFECTS (arg0)
6578          && ! TREE_SIDE_EFFECTS (arg1);
6579 }
6580
6581 /* Test whether it is preferable two swap two operands, ARG0 and
6582    ARG1, for example because ARG0 is an integer constant and ARG1
6583    isn't.  If REORDER is true, only recommend swapping if we can
6584    evaluate the operands in reverse order.  */
6585
6586 bool
6587 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6588 {
6589   STRIP_SIGN_NOPS (arg0);
6590   STRIP_SIGN_NOPS (arg1);
6591
6592   if (TREE_CODE (arg1) == INTEGER_CST)
6593     return 0;
6594   if (TREE_CODE (arg0) == INTEGER_CST)
6595     return 1;
6596
6597   if (TREE_CODE (arg1) == REAL_CST)
6598     return 0;
6599   if (TREE_CODE (arg0) == REAL_CST)
6600     return 1;
6601
6602   if (TREE_CODE (arg1) == FIXED_CST)
6603     return 0;
6604   if (TREE_CODE (arg0) == FIXED_CST)
6605     return 1;
6606
6607   if (TREE_CODE (arg1) == COMPLEX_CST)
6608     return 0;
6609   if (TREE_CODE (arg0) == COMPLEX_CST)
6610     return 1;
6611
6612   if (TREE_CONSTANT (arg1))
6613     return 0;
6614   if (TREE_CONSTANT (arg0))
6615     return 1;
6616
6617   if (optimize_function_for_size_p (cfun))
6618     return 0;
6619
6620   if (reorder && flag_evaluation_order
6621       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6622     return 0;
6623
6624   /* It is preferable to swap two SSA_NAME to ensure a canonical form
6625      for commutative and comparison operators.  Ensuring a canonical
6626      form allows the optimizers to find additional redundancies without
6627      having to explicitly check for both orderings.  */
6628   if (TREE_CODE (arg0) == SSA_NAME
6629       && TREE_CODE (arg1) == SSA_NAME
6630       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6631     return 1;
6632
6633   /* Put SSA_NAMEs last.  */
6634   if (TREE_CODE (arg1) == SSA_NAME)
6635     return 0;
6636   if (TREE_CODE (arg0) == SSA_NAME)
6637     return 1;
6638
6639   /* Put variables last.  */
6640   if (DECL_P (arg1))
6641     return 0;
6642   if (DECL_P (arg0))
6643     return 1;
6644
6645   return 0;
6646 }
6647
6648 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6649    ARG0 is extended to a wider type.  */
6650
6651 static tree
6652 fold_widened_comparison (location_t loc, enum tree_code code,
6653                          tree type, tree arg0, tree arg1)
6654 {
6655   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6656   tree arg1_unw;
6657   tree shorter_type, outer_type;
6658   tree min, max;
6659   bool above, below;
6660
6661   if (arg0_unw == arg0)
6662     return NULL_TREE;
6663   shorter_type = TREE_TYPE (arg0_unw);
6664
6665 #ifdef HAVE_canonicalize_funcptr_for_compare
6666   /* Disable this optimization if we're casting a function pointer
6667      type on targets that require function pointer canonicalization.  */
6668   if (HAVE_canonicalize_funcptr_for_compare
6669       && TREE_CODE (shorter_type) == POINTER_TYPE
6670       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6671     return NULL_TREE;
6672 #endif
6673
6674   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6675     return NULL_TREE;
6676
6677   arg1_unw = get_unwidened (arg1, NULL_TREE);
6678
6679   /* If possible, express the comparison in the shorter mode.  */
6680   if ((code == EQ_EXPR || code == NE_EXPR
6681        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6682       && (TREE_TYPE (arg1_unw) == shorter_type
6683           || ((TYPE_PRECISION (shorter_type)
6684                >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6685               && (TYPE_UNSIGNED (shorter_type)
6686                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6687           || (TREE_CODE (arg1_unw) == INTEGER_CST
6688               && (TREE_CODE (shorter_type) == INTEGER_TYPE
6689                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6690               && int_fits_type_p (arg1_unw, shorter_type))))
6691     return fold_build2_loc (loc, code, type, arg0_unw,
6692                         fold_convert_loc (loc, shorter_type, arg1_unw));
6693
6694   if (TREE_CODE (arg1_unw) != INTEGER_CST
6695       || TREE_CODE (shorter_type) != INTEGER_TYPE
6696       || !int_fits_type_p (arg1_unw, shorter_type))
6697     return NULL_TREE;
6698
6699   /* If we are comparing with the integer that does not fit into the range
6700      of the shorter type, the result is known.  */
6701   outer_type = TREE_TYPE (arg1_unw);
6702   min = lower_bound_in_type (outer_type, shorter_type);
6703   max = upper_bound_in_type (outer_type, shorter_type);
6704
6705   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6706                                                    max, arg1_unw));
6707   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6708                                                    arg1_unw, min));
6709
6710   switch (code)
6711     {
6712     case EQ_EXPR:
6713       if (above || below)
6714         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6715       break;
6716
6717     case NE_EXPR:
6718       if (above || below)
6719         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6720       break;
6721
6722     case LT_EXPR:
6723     case LE_EXPR:
6724       if (above)
6725         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6726       else if (below)
6727         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6728
6729     case GT_EXPR:
6730     case GE_EXPR:
6731       if (above)
6732         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6733       else if (below)
6734         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6735
6736     default:
6737       break;
6738     }
6739
6740   return NULL_TREE;
6741 }
6742
6743 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6744    ARG0 just the signedness is changed.  */
6745
6746 static tree
6747 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
6748                               tree arg0, tree arg1)
6749 {
6750   tree arg0_inner;
6751   tree inner_type, outer_type;
6752
6753   if (!CONVERT_EXPR_P (arg0))
6754     return NULL_TREE;
6755
6756   outer_type = TREE_TYPE (arg0);
6757   arg0_inner = TREE_OPERAND (arg0, 0);
6758   inner_type = TREE_TYPE (arg0_inner);
6759
6760 #ifdef HAVE_canonicalize_funcptr_for_compare
6761   /* Disable this optimization if we're casting a function pointer
6762      type on targets that require function pointer canonicalization.  */
6763   if (HAVE_canonicalize_funcptr_for_compare
6764       && TREE_CODE (inner_type) == POINTER_TYPE
6765       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6766     return NULL_TREE;
6767 #endif
6768
6769   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6770     return NULL_TREE;
6771
6772   if (TREE_CODE (arg1) != INTEGER_CST
6773       && !(CONVERT_EXPR_P (arg1)
6774            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6775     return NULL_TREE;
6776
6777   if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6778        || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
6779       && code != NE_EXPR
6780       && code != EQ_EXPR)
6781     return NULL_TREE;
6782
6783   if (TREE_CODE (arg1) == INTEGER_CST)
6784     arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
6785                                   TREE_INT_CST_HIGH (arg1), 0,
6786                                   TREE_OVERFLOW (arg1));
6787   else
6788     arg1 = fold_convert_loc (loc, inner_type, arg1);
6789
6790   return fold_build2_loc (loc, code, type, arg0_inner, arg1);
6791 }
6792
6793 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6794    step of the array.  Reconstructs s and delta in the case of s *
6795    delta being an integer constant (and thus already folded).  ADDR is
6796    the address. MULT is the multiplicative expression.  If the
6797    function succeeds, the new address expression is returned.
6798    Otherwise NULL_TREE is returned.  LOC is the location of the
6799    resulting expression.  */
6800
6801 static tree
6802 try_move_mult_to_index (location_t loc, tree addr, tree op1)
6803 {
6804   tree s, delta, step;
6805   tree ref = TREE_OPERAND (addr, 0), pref;
6806   tree ret, pos;
6807   tree itype;
6808   bool mdim = false;
6809
6810   /*  Strip the nops that might be added when converting op1 to sizetype. */
6811   STRIP_NOPS (op1);
6812
6813   /* Canonicalize op1 into a possibly non-constant delta
6814      and an INTEGER_CST s.  */
6815   if (TREE_CODE (op1) == MULT_EXPR)
6816     {
6817       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6818
6819       STRIP_NOPS (arg0);
6820       STRIP_NOPS (arg1);
6821
6822       if (TREE_CODE (arg0) == INTEGER_CST)
6823         {
6824           s = arg0;
6825           delta = arg1;
6826         }
6827       else if (TREE_CODE (arg1) == INTEGER_CST)
6828         {
6829           s = arg1;
6830           delta = arg0;
6831         }
6832       else
6833         return NULL_TREE;
6834     }
6835   else if (TREE_CODE (op1) == INTEGER_CST)
6836     {
6837       delta = op1;
6838       s = NULL_TREE;
6839     }
6840   else
6841     {
6842       /* Simulate we are delta * 1.  */
6843       delta = op1;
6844       s = integer_one_node;
6845     }
6846
6847   for (;; ref = TREE_OPERAND (ref, 0))
6848     {
6849       if (TREE_CODE (ref) == ARRAY_REF)
6850         {
6851           tree domain;
6852
6853           /* Remember if this was a multi-dimensional array.  */
6854           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6855             mdim = true;
6856
6857           domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6858           if (! domain)
6859             continue;
6860           itype = TREE_TYPE (domain);
6861
6862           step = array_ref_element_size (ref);
6863           if (TREE_CODE (step) != INTEGER_CST)
6864             continue;
6865
6866           if (s)
6867             {
6868               if (! tree_int_cst_equal (step, s))
6869                 continue;
6870             }
6871           else
6872             {
6873               /* Try if delta is a multiple of step.  */
6874               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6875               if (! tmp)
6876                 continue;
6877               delta = tmp;
6878             }
6879
6880           /* Only fold here if we can verify we do not overflow one
6881              dimension of a multi-dimensional array.  */
6882           if (mdim)
6883             {
6884               tree tmp;
6885
6886               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
6887                   || !TYPE_MAX_VALUE (domain)
6888                   || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
6889                 continue;
6890
6891               tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
6892                                      fold_convert_loc (loc, itype,
6893                                                        TREE_OPERAND (ref, 1)),
6894                                      fold_convert_loc (loc, itype, delta));
6895               if (!tmp
6896                   || TREE_CODE (tmp) != INTEGER_CST
6897                   || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
6898                 continue;
6899             }
6900
6901           break;
6902         }
6903       else
6904         mdim = false;
6905
6906       if (!handled_component_p (ref))
6907         return NULL_TREE;
6908     }
6909
6910   /* We found the suitable array reference.  So copy everything up to it,
6911      and replace the index.  */
6912
6913   pref = TREE_OPERAND (addr, 0);
6914   ret = copy_node (pref);
6915   SET_EXPR_LOCATION (ret, loc);
6916   pos = ret;
6917
6918   while (pref != ref)
6919     {
6920       pref = TREE_OPERAND (pref, 0);
6921       TREE_OPERAND (pos, 0) = copy_node (pref);
6922       pos = TREE_OPERAND (pos, 0);
6923     }
6924
6925   TREE_OPERAND (pos, 1) = fold_build2_loc (loc, PLUS_EXPR, itype,
6926                                        fold_convert_loc (loc, itype,
6927                                                          TREE_OPERAND (pos, 1)),
6928                                        fold_convert_loc (loc, itype, delta));
6929
6930   return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
6931 }
6932
6933
6934 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
6935    means A >= Y && A != MAX, but in this case we know that
6936    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
6937
6938 static tree
6939 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6940 {
6941   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6942
6943   if (TREE_CODE (bound) == LT_EXPR)
6944     a = TREE_OPERAND (bound, 0);
6945   else if (TREE_CODE (bound) == GT_EXPR)
6946     a = TREE_OPERAND (bound, 1);
6947   else
6948     return NULL_TREE;
6949
6950   typea = TREE_TYPE (a);
6951   if (!INTEGRAL_TYPE_P (typea)
6952       && !POINTER_TYPE_P (typea))
6953     return NULL_TREE;
6954
6955   if (TREE_CODE (ineq) == LT_EXPR)
6956     {
6957       a1 = TREE_OPERAND (ineq, 1);
6958       y = TREE_OPERAND (ineq, 0);
6959     }
6960   else if (TREE_CODE (ineq) == GT_EXPR)
6961     {
6962       a1 = TREE_OPERAND (ineq, 0);
6963       y = TREE_OPERAND (ineq, 1);
6964     }
6965   else
6966     return NULL_TREE;
6967
6968   if (TREE_TYPE (a1) != typea)
6969     return NULL_TREE;
6970
6971   if (POINTER_TYPE_P (typea))
6972     {
6973       /* Convert the pointer types into integer before taking the difference.  */
6974       tree ta = fold_convert_loc (loc, ssizetype, a);
6975       tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6976       diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6977     }
6978   else
6979     diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6980
6981   if (!diff || !integer_onep (diff))
6982    return NULL_TREE;
6983
6984   return fold_build2_loc (loc, GE_EXPR, type, a, y);
6985 }
6986
6987 /* Fold a sum or difference of at least one multiplication.
6988    Returns the folded tree or NULL if no simplification could be made.  */
6989
6990 static tree
6991 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6992                           tree arg0, tree arg1)
6993 {
6994   tree arg00, arg01, arg10, arg11;
6995   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6996
6997   /* (A * C) +- (B * C) -> (A+-B) * C.
6998      (A * C) +- A -> A * (C+-1).
6999      We are most concerned about the case where C is a constant,
7000      but other combinations show up during loop reduction.  Since
7001      it is not difficult, try all four possibilities.  */
7002
7003   if (TREE_CODE (arg0) == MULT_EXPR)
7004     {
7005       arg00 = TREE_OPERAND (arg0, 0);
7006       arg01 = TREE_OPERAND (arg0, 1);
7007     }
7008   else if (TREE_CODE (arg0) == INTEGER_CST)
7009     {
7010       arg00 = build_one_cst (type);
7011       arg01 = arg0;
7012     }
7013   else
7014     {
7015       /* We cannot generate constant 1 for fract.  */
7016       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7017         return NULL_TREE;
7018       arg00 = arg0;
7019       arg01 = build_one_cst (type);
7020     }
7021   if (TREE_CODE (arg1) == MULT_EXPR)
7022     {
7023       arg10 = TREE_OPERAND (arg1, 0);
7024       arg11 = TREE_OPERAND (arg1, 1);
7025     }
7026   else if (TREE_CODE (arg1) == INTEGER_CST)
7027     {
7028       arg10 = build_one_cst (type);
7029       /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7030          the purpose of this canonicalization.  */
7031       if (TREE_INT_CST_HIGH (arg1) == -1
7032           && negate_expr_p (arg1)
7033           && code == PLUS_EXPR)
7034         {
7035           arg11 = negate_expr (arg1);
7036           code = MINUS_EXPR;
7037         }
7038       else
7039         arg11 = arg1;
7040     }
7041   else
7042     {
7043       /* We cannot generate constant 1 for fract.  */
7044       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7045         return NULL_TREE;
7046       arg10 = arg1;
7047       arg11 = build_one_cst (type);
7048     }
7049   same = NULL_TREE;
7050
7051   if (operand_equal_p (arg01, arg11, 0))
7052     same = arg01, alt0 = arg00, alt1 = arg10;
7053   else if (operand_equal_p (arg00, arg10, 0))
7054     same = arg00, alt0 = arg01, alt1 = arg11;
7055   else if (operand_equal_p (arg00, arg11, 0))
7056     same = arg00, alt0 = arg01, alt1 = arg10;
7057   else if (operand_equal_p (arg01, arg10, 0))
7058     same = arg01, alt0 = arg00, alt1 = arg11;
7059
7060   /* No identical multiplicands; see if we can find a common
7061      power-of-two factor in non-power-of-two multiplies.  This
7062      can help in multi-dimensional array access.  */
7063   else if (host_integerp (arg01, 0)
7064            && host_integerp (arg11, 0))
7065     {
7066       HOST_WIDE_INT int01, int11, tmp;
7067       bool swap = false;
7068       tree maybe_same;
7069       int01 = TREE_INT_CST_LOW (arg01);
7070       int11 = TREE_INT_CST_LOW (arg11);
7071
7072       /* Move min of absolute values to int11.  */
7073       if ((int01 >= 0 ? int01 : -int01)
7074           < (int11 >= 0 ? int11 : -int11))
7075         {
7076           tmp = int01, int01 = int11, int11 = tmp;
7077           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7078           maybe_same = arg01;
7079           swap = true;
7080         }
7081       else
7082         maybe_same = arg11;
7083
7084       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
7085           /* The remainder should not be a constant, otherwise we
7086              end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7087              increased the number of multiplications necessary.  */
7088           && TREE_CODE (arg10) != INTEGER_CST)
7089         {
7090           alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7091                               build_int_cst (TREE_TYPE (arg00),
7092                                              int01 / int11));
7093           alt1 = arg10;
7094           same = maybe_same;
7095           if (swap)
7096             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7097         }
7098     }
7099
7100   if (same)
7101     return fold_build2_loc (loc, MULT_EXPR, type,
7102                         fold_build2_loc (loc, code, type,
7103                                      fold_convert_loc (loc, type, alt0),
7104                                      fold_convert_loc (loc, type, alt1)),
7105                         fold_convert_loc (loc, type, same));
7106
7107   return NULL_TREE;
7108 }
7109
7110 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7111    specified by EXPR into the buffer PTR of length LEN bytes.
7112    Return the number of bytes placed in the buffer, or zero
7113    upon failure.  */
7114
7115 static int
7116 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7117 {
7118   tree type = TREE_TYPE (expr);
7119   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7120   int byte, offset, word, words;
7121   unsigned char value;
7122
7123   if (total_bytes > len)
7124     return 0;
7125   words = total_bytes / UNITS_PER_WORD;
7126
7127   for (byte = 0; byte < total_bytes; byte++)
7128     {
7129       int bitpos = byte * BITS_PER_UNIT;
7130       if (bitpos < HOST_BITS_PER_WIDE_INT)
7131         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7132       else
7133         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7134                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7135
7136       if (total_bytes > UNITS_PER_WORD)
7137         {
7138           word = byte / UNITS_PER_WORD;
7139           if (WORDS_BIG_ENDIAN)
7140             word = (words - 1) - word;
7141           offset = word * UNITS_PER_WORD;
7142           if (BYTES_BIG_ENDIAN)
7143             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7144           else
7145             offset += byte % UNITS_PER_WORD;
7146         }
7147       else
7148         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7149       ptr[offset] = value;
7150     }
7151   return total_bytes;
7152 }
7153
7154
7155 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7156    specified by EXPR into the buffer PTR of length LEN bytes.
7157    Return the number of bytes placed in the buffer, or zero
7158    upon failure.  */
7159
7160 static int
7161 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7162 {
7163   tree type = TREE_TYPE (expr);
7164   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7165   int byte, offset, word, words, bitpos;
7166   unsigned char value;
7167
7168   /* There are always 32 bits in each long, no matter the size of
7169      the hosts long.  We handle floating point representations with
7170      up to 192 bits.  */
7171   long tmp[6];
7172
7173   if (total_bytes > len)
7174     return 0;
7175   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7176
7177   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7178
7179   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7180        bitpos += BITS_PER_UNIT)
7181     {
7182       byte = (bitpos / BITS_PER_UNIT) & 3;
7183       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7184
7185       if (UNITS_PER_WORD < 4)
7186         {
7187           word = byte / UNITS_PER_WORD;
7188           if (WORDS_BIG_ENDIAN)
7189             word = (words - 1) - word;
7190           offset = word * UNITS_PER_WORD;
7191           if (BYTES_BIG_ENDIAN)
7192             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7193           else
7194             offset += byte % UNITS_PER_WORD;
7195         }
7196       else
7197         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7198       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7199     }
7200   return total_bytes;
7201 }
7202
7203 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7204    specified by EXPR into the buffer PTR of length LEN bytes.
7205    Return the number of bytes placed in the buffer, or zero
7206    upon failure.  */
7207
7208 static int
7209 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7210 {
7211   int rsize, isize;
7212   tree part;
7213
7214   part = TREE_REALPART (expr);
7215   rsize = native_encode_expr (part, ptr, len);
7216   if (rsize == 0)
7217     return 0;
7218   part = TREE_IMAGPART (expr);
7219   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7220   if (isize != rsize)
7221     return 0;
7222   return rsize + isize;
7223 }
7224
7225
7226 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7227    specified by EXPR into the buffer PTR of length LEN bytes.
7228    Return the number of bytes placed in the buffer, or zero
7229    upon failure.  */
7230
7231 static int
7232 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7233 {
7234   int i, size, offset, count;
7235   tree itype, elem, elements;
7236
7237   offset = 0;
7238   elements = TREE_VECTOR_CST_ELTS (expr);
7239   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7240   itype = TREE_TYPE (TREE_TYPE (expr));
7241   size = GET_MODE_SIZE (TYPE_MODE (itype));
7242   for (i = 0; i < count; i++)
7243     {
7244       if (elements)
7245         {
7246           elem = TREE_VALUE (elements);
7247           elements = TREE_CHAIN (elements);
7248         }
7249       else
7250         elem = NULL_TREE;
7251
7252       if (elem)
7253         {
7254           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7255             return 0;
7256         }
7257       else
7258         {
7259           if (offset + size > len)
7260             return 0;
7261           memset (ptr+offset, 0, size);
7262         }
7263       offset += size;
7264     }
7265   return offset;
7266 }
7267
7268
7269 /* Subroutine of native_encode_expr.  Encode the STRING_CST
7270    specified by EXPR into the buffer PTR of length LEN bytes.
7271    Return the number of bytes placed in the buffer, or zero
7272    upon failure.  */
7273
7274 static int
7275 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7276 {
7277   tree type = TREE_TYPE (expr);
7278   HOST_WIDE_INT total_bytes;
7279
7280   if (TREE_CODE (type) != ARRAY_TYPE
7281       || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7282       || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7283       || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7284     return 0;
7285   total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7286   if (total_bytes > len)
7287     return 0;
7288   if (TREE_STRING_LENGTH (expr) < total_bytes)
7289     {
7290       memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7291       memset (ptr + TREE_STRING_LENGTH (expr), 0,
7292               total_bytes - TREE_STRING_LENGTH (expr));
7293     }
7294   else
7295     memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7296   return total_bytes;
7297 }
7298
7299
7300 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7301    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7302    buffer PTR of length LEN bytes.  Return the number of bytes
7303    placed in the buffer, or zero upon failure.  */
7304
7305 int
7306 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7307 {
7308   switch (TREE_CODE (expr))
7309     {
7310     case INTEGER_CST:
7311       return native_encode_int (expr, ptr, len);
7312
7313     case REAL_CST:
7314       return native_encode_real (expr, ptr, len);
7315
7316     case COMPLEX_CST:
7317       return native_encode_complex (expr, ptr, len);
7318
7319     case VECTOR_CST:
7320       return native_encode_vector (expr, ptr, len);
7321
7322     case STRING_CST:
7323       return native_encode_string (expr, ptr, len);
7324
7325     default:
7326       return 0;
7327     }
7328 }
7329
7330
7331 /* Subroutine of native_interpret_expr.  Interpret the contents of
7332    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7333    If the buffer cannot be interpreted, return NULL_TREE.  */
7334
7335 static tree
7336 native_interpret_int (tree type, const unsigned char *ptr, int len)
7337 {
7338   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7339   int byte, offset, word, words;
7340   unsigned char value;
7341   unsigned int HOST_WIDE_INT lo = 0;
7342   HOST_WIDE_INT hi = 0;
7343
7344   if (total_bytes > len)
7345     return NULL_TREE;
7346   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7347     return NULL_TREE;
7348   words = total_bytes / UNITS_PER_WORD;
7349
7350   for (byte = 0; byte < total_bytes; byte++)
7351     {
7352       int bitpos = byte * BITS_PER_UNIT;
7353       if (total_bytes > UNITS_PER_WORD)
7354         {
7355           word = byte / UNITS_PER_WORD;
7356           if (WORDS_BIG_ENDIAN)
7357             word = (words - 1) - word;
7358           offset = word * UNITS_PER_WORD;
7359           if (BYTES_BIG_ENDIAN)
7360             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7361           else
7362             offset += byte % UNITS_PER_WORD;
7363         }
7364       else
7365         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7366       value = ptr[offset];
7367
7368       if (bitpos < HOST_BITS_PER_WIDE_INT)
7369         lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7370       else
7371         hi |= (unsigned HOST_WIDE_INT) value
7372               << (bitpos - HOST_BITS_PER_WIDE_INT);
7373     }
7374
7375   return build_int_cst_wide_type (type, lo, hi);
7376 }
7377
7378
7379 /* Subroutine of native_interpret_expr.  Interpret the contents of
7380    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7381    If the buffer cannot be interpreted, return NULL_TREE.  */
7382
7383 static tree
7384 native_interpret_real (tree type, const unsigned char *ptr, int len)
7385 {
7386   enum machine_mode mode = TYPE_MODE (type);
7387   int total_bytes = GET_MODE_SIZE (mode);
7388   int byte, offset, word, words, bitpos;
7389   unsigned char value;
7390   /* There are always 32 bits in each long, no matter the size of
7391      the hosts long.  We handle floating point representations with
7392      up to 192 bits.  */
7393   REAL_VALUE_TYPE r;
7394   long tmp[6];
7395
7396   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7397   if (total_bytes > len || total_bytes > 24)
7398     return NULL_TREE;
7399   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7400
7401   memset (tmp, 0, sizeof (tmp));
7402   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7403        bitpos += BITS_PER_UNIT)
7404     {
7405       byte = (bitpos / BITS_PER_UNIT) & 3;
7406       if (UNITS_PER_WORD < 4)
7407         {
7408           word = byte / UNITS_PER_WORD;
7409           if (WORDS_BIG_ENDIAN)
7410             word = (words - 1) - word;
7411           offset = word * UNITS_PER_WORD;
7412           if (BYTES_BIG_ENDIAN)
7413             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7414           else
7415             offset += byte % UNITS_PER_WORD;
7416         }
7417       else
7418         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7419       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7420
7421       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7422     }
7423
7424   real_from_target (&r, tmp, mode);
7425   return build_real (type, r);
7426 }
7427
7428
7429 /* Subroutine of native_interpret_expr.  Interpret the contents of
7430    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7431    If the buffer cannot be interpreted, return NULL_TREE.  */
7432
7433 static tree
7434 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7435 {
7436   tree etype, rpart, ipart;
7437   int size;
7438
7439   etype = TREE_TYPE (type);
7440   size = GET_MODE_SIZE (TYPE_MODE (etype));
7441   if (size * 2 > len)
7442     return NULL_TREE;
7443   rpart = native_interpret_expr (etype, ptr, size);
7444   if (!rpart)
7445     return NULL_TREE;
7446   ipart = native_interpret_expr (etype, ptr+size, size);
7447   if (!ipart)
7448     return NULL_TREE;
7449   return build_complex (type, rpart, ipart);
7450 }
7451
7452
7453 /* Subroutine of native_interpret_expr.  Interpret the contents of
7454    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7455    If the buffer cannot be interpreted, return NULL_TREE.  */
7456
7457 static tree
7458 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7459 {
7460   tree etype, elem, elements;
7461   int i, size, count;
7462
7463   etype = TREE_TYPE (type);
7464   size = GET_MODE_SIZE (TYPE_MODE (etype));
7465   count = TYPE_VECTOR_SUBPARTS (type);
7466   if (size * count > len)
7467     return NULL_TREE;
7468
7469   elements = NULL_TREE;
7470   for (i = count - 1; i >= 0; i--)
7471     {
7472       elem = native_interpret_expr (etype, ptr+(i*size), size);
7473       if (!elem)
7474         return NULL_TREE;
7475       elements = tree_cons (NULL_TREE, elem, elements);
7476     }
7477   return build_vector (type, elements);
7478 }
7479
7480
7481 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7482    the buffer PTR of length LEN as a constant of type TYPE.  For
7483    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7484    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7485    return NULL_TREE.  */
7486
7487 tree
7488 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7489 {
7490   switch (TREE_CODE (type))
7491     {
7492     case INTEGER_TYPE:
7493     case ENUMERAL_TYPE:
7494     case BOOLEAN_TYPE:
7495       return native_interpret_int (type, ptr, len);
7496
7497     case REAL_TYPE:
7498       return native_interpret_real (type, ptr, len);
7499
7500     case COMPLEX_TYPE:
7501       return native_interpret_complex (type, ptr, len);
7502
7503     case VECTOR_TYPE:
7504       return native_interpret_vector (type, ptr, len);
7505
7506     default:
7507       return NULL_TREE;
7508     }
7509 }
7510
7511
7512 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7513    TYPE at compile-time.  If we're unable to perform the conversion
7514    return NULL_TREE.  */
7515
7516 static tree
7517 fold_view_convert_expr (tree type, tree expr)
7518 {
7519   /* We support up to 512-bit values (for V8DFmode).  */
7520   unsigned char buffer[64];
7521   int len;
7522
7523   /* Check that the host and target are sane.  */
7524   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7525     return NULL_TREE;
7526
7527   len = native_encode_expr (expr, buffer, sizeof (buffer));
7528   if (len == 0)
7529     return NULL_TREE;
7530
7531   return native_interpret_expr (type, buffer, len);
7532 }
7533
7534 /* Build an expression for the address of T.  Folds away INDIRECT_REF
7535    to avoid confusing the gimplify process.  */
7536
7537 tree
7538 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7539 {
7540   /* The size of the object is not relevant when talking about its address.  */
7541   if (TREE_CODE (t) == WITH_SIZE_EXPR)
7542     t = TREE_OPERAND (t, 0);
7543
7544   /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7545   if (TREE_CODE (t) == INDIRECT_REF
7546       || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7547     {
7548       t = TREE_OPERAND (t, 0);
7549
7550       if (TREE_TYPE (t) != ptrtype)
7551         {
7552           t = build1 (NOP_EXPR, ptrtype, t);
7553           SET_EXPR_LOCATION (t, loc);
7554         }
7555     }
7556   else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7557     {
7558       t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7559
7560       if (TREE_TYPE (t) != ptrtype)
7561         t = fold_convert_loc (loc, ptrtype, t);
7562     }
7563   else
7564     {
7565       t = build1 (ADDR_EXPR, ptrtype, t);
7566       SET_EXPR_LOCATION (t, loc);
7567     }
7568
7569   return t;
7570 }
7571
7572 /* Build an expression for the address of T.  */
7573
7574 tree
7575 build_fold_addr_expr_loc (location_t loc, tree t)
7576 {
7577   tree ptrtype = build_pointer_type (TREE_TYPE (t));
7578
7579   return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7580 }
7581
7582 /* Fold a unary expression of code CODE and type TYPE with operand
7583    OP0.  Return the folded expression if folding is successful.
7584    Otherwise, return NULL_TREE.  */
7585
7586 tree
7587 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7588 {
7589   tree tem;
7590   tree arg0;
7591   enum tree_code_class kind = TREE_CODE_CLASS (code);
7592
7593   gcc_assert (IS_EXPR_CODE_CLASS (kind)
7594               && TREE_CODE_LENGTH (code) == 1);
7595
7596   arg0 = op0;
7597   if (arg0)
7598     {
7599       if (CONVERT_EXPR_CODE_P (code)
7600           || code == FLOAT_EXPR || code == ABS_EXPR)
7601         {
7602           /* Don't use STRIP_NOPS, because signedness of argument type
7603              matters.  */
7604           STRIP_SIGN_NOPS (arg0);
7605         }
7606       else
7607         {
7608           /* Strip any conversions that don't change the mode.  This
7609              is safe for every expression, except for a comparison
7610              expression because its signedness is derived from its
7611              operands.
7612
7613              Note that this is done as an internal manipulation within
7614              the constant folder, in order to find the simplest
7615              representation of the arguments so that their form can be
7616              studied.  In any cases, the appropriate type conversions
7617              should be put back in the tree that will get out of the
7618              constant folder.  */
7619           STRIP_NOPS (arg0);
7620         }
7621     }
7622
7623   if (TREE_CODE_CLASS (code) == tcc_unary)
7624     {
7625       if (TREE_CODE (arg0) == COMPOUND_EXPR)
7626         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7627                        fold_build1_loc (loc, code, type,
7628                                     fold_convert_loc (loc, TREE_TYPE (op0),
7629                                                       TREE_OPERAND (arg0, 1))));
7630       else if (TREE_CODE (arg0) == COND_EXPR)
7631         {
7632           tree arg01 = TREE_OPERAND (arg0, 1);
7633           tree arg02 = TREE_OPERAND (arg0, 2);
7634           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7635             arg01 = fold_build1_loc (loc, code, type,
7636                                  fold_convert_loc (loc,
7637                                                    TREE_TYPE (op0), arg01));
7638           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7639             arg02 = fold_build1_loc (loc, code, type,
7640                                  fold_convert_loc (loc,
7641                                                    TREE_TYPE (op0), arg02));
7642           tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7643                              arg01, arg02);
7644
7645           /* If this was a conversion, and all we did was to move into
7646              inside the COND_EXPR, bring it back out.  But leave it if
7647              it is a conversion from integer to integer and the
7648              result precision is no wider than a word since such a
7649              conversion is cheap and may be optimized away by combine,
7650              while it couldn't if it were outside the COND_EXPR.  Then return
7651              so we don't get into an infinite recursion loop taking the
7652              conversion out and then back in.  */
7653
7654           if ((CONVERT_EXPR_CODE_P (code)
7655                || code == NON_LVALUE_EXPR)
7656               && TREE_CODE (tem) == COND_EXPR
7657               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7658               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7659               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7660               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7661               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7662                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7663               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7664                      && (INTEGRAL_TYPE_P
7665                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7666                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7667                   || flag_syntax_only))
7668             {
7669               tem = build1 (code, type,
7670                             build3 (COND_EXPR,
7671                                     TREE_TYPE (TREE_OPERAND
7672                                                (TREE_OPERAND (tem, 1), 0)),
7673                                     TREE_OPERAND (tem, 0),
7674                                     TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7675                                     TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
7676               SET_EXPR_LOCATION (tem, loc);
7677             }
7678           return tem;
7679         }
7680       else if (COMPARISON_CLASS_P (arg0))
7681         {
7682           if (TREE_CODE (type) == BOOLEAN_TYPE)
7683             {
7684               arg0 = copy_node (arg0);
7685               TREE_TYPE (arg0) = type;
7686               return arg0;
7687             }
7688           else if (TREE_CODE (type) != INTEGER_TYPE)
7689             return fold_build3_loc (loc, COND_EXPR, type, arg0,
7690                                 fold_build1_loc (loc, code, type,
7691                                              integer_one_node),
7692                                 fold_build1_loc (loc, code, type,
7693                                              integer_zero_node));
7694         }
7695    }
7696
7697   switch (code)
7698     {
7699     case PAREN_EXPR:
7700       /* Re-association barriers around constants and other re-association
7701          barriers can be removed.  */
7702       if (CONSTANT_CLASS_P (op0)
7703           || TREE_CODE (op0) == PAREN_EXPR)
7704         return fold_convert_loc (loc, type, op0);
7705       return NULL_TREE;
7706
7707     CASE_CONVERT:
7708     case FLOAT_EXPR:
7709     case FIX_TRUNC_EXPR:
7710       if (TREE_TYPE (op0) == type)
7711         return op0;
7712
7713       /* If we have (type) (a CMP b) and type is an integral type, return
7714          new expression involving the new type.  */
7715       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
7716         return fold_build2_loc (loc, TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
7717                             TREE_OPERAND (op0, 1));
7718
7719       /* Handle cases of two conversions in a row.  */
7720       if (CONVERT_EXPR_P (op0))
7721         {
7722           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7723           tree inter_type = TREE_TYPE (op0);
7724           int inside_int = INTEGRAL_TYPE_P (inside_type);
7725           int inside_ptr = POINTER_TYPE_P (inside_type);
7726           int inside_float = FLOAT_TYPE_P (inside_type);
7727           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7728           unsigned int inside_prec = TYPE_PRECISION (inside_type);
7729           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7730           int inter_int = INTEGRAL_TYPE_P (inter_type);
7731           int inter_ptr = POINTER_TYPE_P (inter_type);
7732           int inter_float = FLOAT_TYPE_P (inter_type);
7733           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7734           unsigned int inter_prec = TYPE_PRECISION (inter_type);
7735           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7736           int final_int = INTEGRAL_TYPE_P (type);
7737           int final_ptr = POINTER_TYPE_P (type);
7738           int final_float = FLOAT_TYPE_P (type);
7739           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7740           unsigned int final_prec = TYPE_PRECISION (type);
7741           int final_unsignedp = TYPE_UNSIGNED (type);
7742
7743           /* In addition to the cases of two conversions in a row
7744              handled below, if we are converting something to its own
7745              type via an object of identical or wider precision, neither
7746              conversion is needed.  */
7747           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7748               && (((inter_int || inter_ptr) && final_int)
7749                   || (inter_float && final_float))
7750               && inter_prec >= final_prec)
7751             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7752
7753           /* Likewise, if the intermediate and initial types are either both
7754              float or both integer, we don't need the middle conversion if the
7755              former is wider than the latter and doesn't change the signedness
7756              (for integers).  Avoid this if the final type is a pointer since
7757              then we sometimes need the middle conversion.  Likewise if the
7758              final type has a precision not equal to the size of its mode.  */
7759           if (((inter_int && inside_int)
7760                || (inter_float && inside_float)
7761                || (inter_vec && inside_vec))
7762               && inter_prec >= inside_prec
7763               && (inter_float || inter_vec
7764                   || inter_unsignedp == inside_unsignedp)
7765               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7766                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
7767               && ! final_ptr
7768               && (! final_vec || inter_prec == inside_prec))
7769             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7770
7771           /* If we have a sign-extension of a zero-extended value, we can
7772              replace that by a single zero-extension.  */
7773           if (inside_int && inter_int && final_int
7774               && inside_prec < inter_prec && inter_prec < final_prec
7775               && inside_unsignedp && !inter_unsignedp)
7776             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7777
7778           /* Two conversions in a row are not needed unless:
7779              - some conversion is floating-point (overstrict for now), or
7780              - some conversion is a vector (overstrict for now), or
7781              - the intermediate type is narrower than both initial and
7782                final, or
7783              - the intermediate type and innermost type differ in signedness,
7784                and the outermost type is wider than the intermediate, or
7785              - the initial type is a pointer type and the precisions of the
7786                intermediate and final types differ, or
7787              - the final type is a pointer type and the precisions of the
7788                initial and intermediate types differ.  */
7789           if (! inside_float && ! inter_float && ! final_float
7790               && ! inside_vec && ! inter_vec && ! final_vec
7791               && (inter_prec >= inside_prec || inter_prec >= final_prec)
7792               && ! (inside_int && inter_int
7793                     && inter_unsignedp != inside_unsignedp
7794                     && inter_prec < final_prec)
7795               && ((inter_unsignedp && inter_prec > inside_prec)
7796                   == (final_unsignedp && final_prec > inter_prec))
7797               && ! (inside_ptr && inter_prec != final_prec)
7798               && ! (final_ptr && inside_prec != inter_prec)
7799               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7800                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7801             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7802         }
7803
7804       /* Handle (T *)&A.B.C for A being of type T and B and C
7805          living at offset zero.  This occurs frequently in
7806          C++ upcasting and then accessing the base.  */
7807       if (TREE_CODE (op0) == ADDR_EXPR
7808           && POINTER_TYPE_P (type)
7809           && handled_component_p (TREE_OPERAND (op0, 0)))
7810         {
7811           HOST_WIDE_INT bitsize, bitpos;
7812           tree offset;
7813           enum machine_mode mode;
7814           int unsignedp, volatilep;
7815           tree base = TREE_OPERAND (op0, 0);
7816           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7817                                       &mode, &unsignedp, &volatilep, false);
7818           /* If the reference was to a (constant) zero offset, we can use
7819              the address of the base if it has the same base type
7820              as the result type and the pointer type is unqualified.  */
7821           if (! offset && bitpos == 0
7822               && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7823                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7824               && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7825             return fold_convert_loc (loc, type,
7826                                      build_fold_addr_expr_loc (loc, base));
7827         }
7828
7829       if (TREE_CODE (op0) == MODIFY_EXPR
7830           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7831           /* Detect assigning a bitfield.  */
7832           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7833                && DECL_BIT_FIELD
7834                (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7835         {
7836           /* Don't leave an assignment inside a conversion
7837              unless assigning a bitfield.  */
7838           tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7839           /* First do the assignment, then return converted constant.  */
7840           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7841           TREE_NO_WARNING (tem) = 1;
7842           TREE_USED (tem) = 1;
7843           SET_EXPR_LOCATION (tem, loc);
7844           return tem;
7845         }
7846
7847       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7848          constants (if x has signed type, the sign bit cannot be set
7849          in c).  This folds extension into the BIT_AND_EXPR.
7850          ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7851          very likely don't have maximal range for their precision and this
7852          transformation effectively doesn't preserve non-maximal ranges.  */
7853       if (TREE_CODE (type) == INTEGER_TYPE
7854           && TREE_CODE (op0) == BIT_AND_EXPR
7855           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7856         {
7857           tree and_expr = op0;
7858           tree and0 = TREE_OPERAND (and_expr, 0);
7859           tree and1 = TREE_OPERAND (and_expr, 1);
7860           int change = 0;
7861
7862           if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7863               || (TYPE_PRECISION (type)
7864                   <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7865             change = 1;
7866           else if (TYPE_PRECISION (TREE_TYPE (and1))
7867                    <= HOST_BITS_PER_WIDE_INT
7868                    && host_integerp (and1, 1))
7869             {
7870               unsigned HOST_WIDE_INT cst;
7871
7872               cst = tree_low_cst (and1, 1);
7873               cst &= (HOST_WIDE_INT) -1
7874                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7875               change = (cst == 0);
7876 #ifdef LOAD_EXTEND_OP
7877               if (change
7878                   && !flag_syntax_only
7879                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7880                       == ZERO_EXTEND))
7881                 {
7882                   tree uns = unsigned_type_for (TREE_TYPE (and0));
7883                   and0 = fold_convert_loc (loc, uns, and0);
7884                   and1 = fold_convert_loc (loc, uns, and1);
7885                 }
7886 #endif
7887             }
7888           if (change)
7889             {
7890               tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
7891                                            TREE_INT_CST_HIGH (and1), 0,
7892                                            TREE_OVERFLOW (and1));
7893               return fold_build2_loc (loc, BIT_AND_EXPR, type,
7894                                   fold_convert_loc (loc, type, and0), tem);
7895             }
7896         }
7897
7898       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7899          when one of the new casts will fold away. Conservatively we assume
7900          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7901       if (POINTER_TYPE_P (type)
7902           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7903           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7904               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7905               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7906         {
7907           tree arg00 = TREE_OPERAND (arg0, 0);
7908           tree arg01 = TREE_OPERAND (arg0, 1);
7909
7910           return fold_build2_loc (loc,
7911                               TREE_CODE (arg0), type,
7912                               fold_convert_loc (loc, type, arg00),
7913                               fold_convert_loc (loc, sizetype, arg01));
7914         }
7915
7916       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7917          of the same precision, and X is an integer type not narrower than
7918          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
7919       if (INTEGRAL_TYPE_P (type)
7920           && TREE_CODE (op0) == BIT_NOT_EXPR
7921           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7922           && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7923           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7924         {
7925           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7926           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7927               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7928             return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7929                                 fold_convert_loc (loc, type, tem));
7930         }
7931
7932       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7933          type of X and Y (integer types only).  */
7934       if (INTEGRAL_TYPE_P (type)
7935           && TREE_CODE (op0) == MULT_EXPR
7936           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7937           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7938         {
7939           /* Be careful not to introduce new overflows.  */
7940           tree mult_type;
7941           if (TYPE_OVERFLOW_WRAPS (type))
7942             mult_type = type;
7943           else
7944             mult_type = unsigned_type_for (type);
7945
7946           if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7947             {
7948               tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7949                                  fold_convert_loc (loc, mult_type,
7950                                                    TREE_OPERAND (op0, 0)),
7951                                  fold_convert_loc (loc, mult_type,
7952                                                    TREE_OPERAND (op0, 1)));
7953               return fold_convert_loc (loc, type, tem);
7954             }
7955         }
7956
7957       tem = fold_convert_const (code, type, op0);
7958       return tem ? tem : NULL_TREE;
7959
7960     case ADDR_SPACE_CONVERT_EXPR:
7961       if (integer_zerop (arg0))
7962         return fold_convert_const (code, type, arg0);
7963       return NULL_TREE;
7964
7965     case FIXED_CONVERT_EXPR:
7966       tem = fold_convert_const (code, type, arg0);
7967       return tem ? tem : NULL_TREE;
7968
7969     case VIEW_CONVERT_EXPR:
7970       if (TREE_TYPE (op0) == type)
7971         return op0;
7972       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
7973         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
7974                             type, TREE_OPERAND (op0, 0));
7975
7976       /* For integral conversions with the same precision or pointer
7977          conversions use a NOP_EXPR instead.  */
7978       if ((INTEGRAL_TYPE_P (type)
7979            || POINTER_TYPE_P (type))
7980           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7981               || POINTER_TYPE_P (TREE_TYPE (op0)))
7982           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7983         return fold_convert_loc (loc, type, op0);
7984
7985       /* Strip inner integral conversions that do not change the precision.  */
7986       if (CONVERT_EXPR_P (op0)
7987           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7988               || POINTER_TYPE_P (TREE_TYPE (op0)))
7989           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
7990               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
7991           && (TYPE_PRECISION (TREE_TYPE (op0))
7992               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
7993         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
7994                             type, TREE_OPERAND (op0, 0));
7995
7996       return fold_view_convert_expr (type, op0);
7997
7998     case NEGATE_EXPR:
7999       tem = fold_negate_expr (loc, arg0);
8000       if (tem)
8001         return fold_convert_loc (loc, type, tem);
8002       return NULL_TREE;
8003
8004     case ABS_EXPR:
8005       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8006         return fold_abs_const (arg0, type);
8007       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8008         return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8009       /* Convert fabs((double)float) into (double)fabsf(float).  */
8010       else if (TREE_CODE (arg0) == NOP_EXPR
8011                && TREE_CODE (type) == REAL_TYPE)
8012         {
8013           tree targ0 = strip_float_extensions (arg0);
8014           if (targ0 != arg0)
8015             return fold_convert_loc (loc, type,
8016                                      fold_build1_loc (loc, ABS_EXPR,
8017                                                   TREE_TYPE (targ0),
8018                                                   targ0));
8019         }
8020       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8021       else if (TREE_CODE (arg0) == ABS_EXPR)
8022         return arg0;
8023       else if (tree_expr_nonnegative_p (arg0))
8024         return arg0;
8025
8026       /* Strip sign ops from argument.  */
8027       if (TREE_CODE (type) == REAL_TYPE)
8028         {
8029           tem = fold_strip_sign_ops (arg0);
8030           if (tem)
8031             return fold_build1_loc (loc, ABS_EXPR, type,
8032                                 fold_convert_loc (loc, type, tem));
8033         }
8034       return NULL_TREE;
8035
8036     case CONJ_EXPR:
8037       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8038         return fold_convert_loc (loc, type, arg0);
8039       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8040         {
8041           tree itype = TREE_TYPE (type);
8042           tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8043           tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8044           return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8045                               negate_expr (ipart));
8046         }
8047       if (TREE_CODE (arg0) == COMPLEX_CST)
8048         {
8049           tree itype = TREE_TYPE (type);
8050           tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8051           tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8052           return build_complex (type, rpart, negate_expr (ipart));
8053         }
8054       if (TREE_CODE (arg0) == CONJ_EXPR)
8055         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8056       return NULL_TREE;
8057
8058     case BIT_NOT_EXPR:
8059       if (TREE_CODE (arg0) == INTEGER_CST)
8060         return fold_not_const (arg0, type);
8061       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8062         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8063       /* Convert ~ (-A) to A - 1.  */
8064       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8065         return fold_build2_loc (loc, MINUS_EXPR, type,
8066                             fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8067                             build_int_cst (type, 1));
8068       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8069       else if (INTEGRAL_TYPE_P (type)
8070                && ((TREE_CODE (arg0) == MINUS_EXPR
8071                     && integer_onep (TREE_OPERAND (arg0, 1)))
8072                    || (TREE_CODE (arg0) == PLUS_EXPR
8073                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8074         return fold_build1_loc (loc, NEGATE_EXPR, type,
8075                             fold_convert_loc (loc, type,
8076                                               TREE_OPERAND (arg0, 0)));
8077       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8078       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8079                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8080                                      fold_convert_loc (loc, type,
8081                                                        TREE_OPERAND (arg0, 0)))))
8082         return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8083                             fold_convert_loc (loc, type,
8084                                               TREE_OPERAND (arg0, 1)));
8085       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8086                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8087                                      fold_convert_loc (loc, type,
8088                                                        TREE_OPERAND (arg0, 1)))))
8089         return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8090                             fold_convert_loc (loc, type,
8091                                               TREE_OPERAND (arg0, 0)), tem);
8092       /* Perform BIT_NOT_EXPR on each element individually.  */
8093       else if (TREE_CODE (arg0) == VECTOR_CST)
8094         {
8095           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8096           int count = TYPE_VECTOR_SUBPARTS (type), i;
8097
8098           for (i = 0; i < count; i++)
8099             {
8100               if (elements)
8101                 {
8102                   elem = TREE_VALUE (elements);
8103                   elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8104                   if (elem == NULL_TREE)
8105                     break;
8106                   elements = TREE_CHAIN (elements);
8107                 }
8108               else
8109                 elem = build_int_cst (TREE_TYPE (type), -1);
8110               list = tree_cons (NULL_TREE, elem, list);
8111             }
8112           if (i == count)
8113             return build_vector (type, nreverse (list));
8114         }
8115
8116       return NULL_TREE;
8117
8118     case TRUTH_NOT_EXPR:
8119       /* The argument to invert_truthvalue must have Boolean type.  */
8120       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8121           arg0 = fold_convert_loc (loc, boolean_type_node, arg0);
8122
8123       /* Note that the operand of this must be an int
8124          and its values must be 0 or 1.
8125          ("true" is a fixed value perhaps depending on the language,
8126          but we don't handle values other than 1 correctly yet.)  */
8127       tem = fold_truth_not_expr (loc, arg0);
8128       if (!tem)
8129         return NULL_TREE;
8130       return fold_convert_loc (loc, type, tem);
8131
8132     case REALPART_EXPR:
8133       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8134         return fold_convert_loc (loc, type, arg0);
8135       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8136         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8137                                  TREE_OPERAND (arg0, 1));
8138       if (TREE_CODE (arg0) == COMPLEX_CST)
8139         return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8140       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8141         {
8142           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8143           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8144                              fold_build1_loc (loc, REALPART_EXPR, itype,
8145                                           TREE_OPERAND (arg0, 0)),
8146                              fold_build1_loc (loc, REALPART_EXPR, itype,
8147                                           TREE_OPERAND (arg0, 1)));
8148           return fold_convert_loc (loc, type, tem);
8149         }
8150       if (TREE_CODE (arg0) == CONJ_EXPR)
8151         {
8152           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8153           tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8154                              TREE_OPERAND (arg0, 0));
8155           return fold_convert_loc (loc, type, tem);
8156         }
8157       if (TREE_CODE (arg0) == CALL_EXPR)
8158         {
8159           tree fn = get_callee_fndecl (arg0);
8160           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8161             switch (DECL_FUNCTION_CODE (fn))
8162               {
8163               CASE_FLT_FN (BUILT_IN_CEXPI):
8164                 fn = mathfn_built_in (type, BUILT_IN_COS);
8165                 if (fn)
8166                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8167                 break;
8168
8169               default:
8170                 break;
8171               }
8172         }
8173       return NULL_TREE;
8174
8175     case IMAGPART_EXPR:
8176       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8177         return fold_convert_loc (loc, type, integer_zero_node);
8178       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8179         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8180                                  TREE_OPERAND (arg0, 0));
8181       if (TREE_CODE (arg0) == COMPLEX_CST)
8182         return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8183       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8184         {
8185           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8186           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8187                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8188                                           TREE_OPERAND (arg0, 0)),
8189                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8190                                           TREE_OPERAND (arg0, 1)));
8191           return fold_convert_loc (loc, type, tem);
8192         }
8193       if (TREE_CODE (arg0) == CONJ_EXPR)
8194         {
8195           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8196           tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8197           return fold_convert_loc (loc, type, negate_expr (tem));
8198         }
8199       if (TREE_CODE (arg0) == CALL_EXPR)
8200         {
8201           tree fn = get_callee_fndecl (arg0);
8202           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8203             switch (DECL_FUNCTION_CODE (fn))
8204               {
8205               CASE_FLT_FN (BUILT_IN_CEXPI):
8206                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8207                 if (fn)
8208                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8209                 break;
8210
8211               default:
8212                 break;
8213               }
8214         }
8215       return NULL_TREE;
8216
8217     case INDIRECT_REF:
8218       /* Fold *&X to X if X is an lvalue.  */
8219       if (TREE_CODE (op0) == ADDR_EXPR)
8220         {
8221           tree op00 = TREE_OPERAND (op0, 0);
8222           if ((TREE_CODE (op00) == VAR_DECL
8223                || TREE_CODE (op00) == PARM_DECL
8224                || TREE_CODE (op00) == RESULT_DECL)
8225               && !TREE_READONLY (op00))
8226             return op00;
8227         }
8228       return NULL_TREE;
8229
8230     default:
8231       return NULL_TREE;
8232     } /* switch (code) */
8233 }
8234
8235
8236 /* If the operation was a conversion do _not_ mark a resulting constant
8237    with TREE_OVERFLOW if the original constant was not.  These conversions
8238    have implementation defined behavior and retaining the TREE_OVERFLOW
8239    flag here would confuse later passes such as VRP.  */
8240 tree
8241 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8242                                 tree type, tree op0)
8243 {
8244   tree res = fold_unary_loc (loc, code, type, op0);
8245   if (res
8246       && TREE_CODE (res) == INTEGER_CST
8247       && TREE_CODE (op0) == INTEGER_CST
8248       && CONVERT_EXPR_CODE_P (code))
8249     TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8250
8251   return res;
8252 }
8253
8254 /* Fold a binary expression of code CODE and type TYPE with operands
8255    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8256    Return the folded expression if folding is successful.  Otherwise,
8257    return NULL_TREE.  */
8258
8259 static tree
8260 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8261 {
8262   enum tree_code compl_code;
8263
8264   if (code == MIN_EXPR)
8265     compl_code = MAX_EXPR;
8266   else if (code == MAX_EXPR)
8267     compl_code = MIN_EXPR;
8268   else
8269     gcc_unreachable ();
8270
8271   /* MIN (MAX (a, b), b) == b.  */
8272   if (TREE_CODE (op0) == compl_code
8273       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8274     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8275
8276   /* MIN (MAX (b, a), b) == b.  */
8277   if (TREE_CODE (op0) == compl_code
8278       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8279       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8280     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8281
8282   /* MIN (a, MAX (a, b)) == a.  */
8283   if (TREE_CODE (op1) == compl_code
8284       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8285       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8286     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8287
8288   /* MIN (a, MAX (b, a)) == a.  */
8289   if (TREE_CODE (op1) == compl_code
8290       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8291       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8292     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8293
8294   return NULL_TREE;
8295 }
8296
8297 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8298    by changing CODE to reduce the magnitude of constants involved in
8299    ARG0 of the comparison.
8300    Returns a canonicalized comparison tree if a simplification was
8301    possible, otherwise returns NULL_TREE.
8302    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8303    valid if signed overflow is undefined.  */
8304
8305 static tree
8306 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8307                                  tree arg0, tree arg1,
8308                                  bool *strict_overflow_p)
8309 {
8310   enum tree_code code0 = TREE_CODE (arg0);
8311   tree t, cst0 = NULL_TREE;
8312   int sgn0;
8313   bool swap = false;
8314
8315   /* Match A +- CST code arg1 and CST code arg1.  We can change the
8316      first form only if overflow is undefined.  */
8317   if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8318          /* In principle pointers also have undefined overflow behavior,
8319             but that causes problems elsewhere.  */
8320          && !POINTER_TYPE_P (TREE_TYPE (arg0))
8321          && (code0 == MINUS_EXPR
8322              || code0 == PLUS_EXPR)
8323          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8324         || code0 == INTEGER_CST))
8325     return NULL_TREE;
8326
8327   /* Identify the constant in arg0 and its sign.  */
8328   if (code0 == INTEGER_CST)
8329     cst0 = arg0;
8330   else
8331     cst0 = TREE_OPERAND (arg0, 1);
8332   sgn0 = tree_int_cst_sgn (cst0);
8333
8334   /* Overflowed constants and zero will cause problems.  */
8335   if (integer_zerop (cst0)
8336       || TREE_OVERFLOW (cst0))
8337     return NULL_TREE;
8338
8339   /* See if we can reduce the magnitude of the constant in
8340      arg0 by changing the comparison code.  */
8341   if (code0 == INTEGER_CST)
8342     {
8343       /* CST <= arg1  ->  CST-1 < arg1.  */
8344       if (code == LE_EXPR && sgn0 == 1)
8345         code = LT_EXPR;
8346       /* -CST < arg1  ->  -CST-1 <= arg1.  */
8347       else if (code == LT_EXPR && sgn0 == -1)
8348         code = LE_EXPR;
8349       /* CST > arg1  ->  CST-1 >= arg1.  */
8350       else if (code == GT_EXPR && sgn0 == 1)
8351         code = GE_EXPR;
8352       /* -CST >= arg1  ->  -CST-1 > arg1.  */
8353       else if (code == GE_EXPR && sgn0 == -1)
8354         code = GT_EXPR;
8355       else
8356         return NULL_TREE;
8357       /* arg1 code' CST' might be more canonical.  */
8358       swap = true;
8359     }
8360   else
8361     {
8362       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
8363       if (code == LT_EXPR
8364           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8365         code = LE_EXPR;
8366       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
8367       else if (code == GT_EXPR
8368                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8369         code = GE_EXPR;
8370       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
8371       else if (code == LE_EXPR
8372                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8373         code = LT_EXPR;
8374       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
8375       else if (code == GE_EXPR
8376                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8377         code = GT_EXPR;
8378       else
8379         return NULL_TREE;
8380       *strict_overflow_p = true;
8381     }
8382
8383   /* Now build the constant reduced in magnitude.  But not if that
8384      would produce one outside of its types range.  */
8385   if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8386       && ((sgn0 == 1
8387            && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8388            && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8389           || (sgn0 == -1
8390               && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8391               && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8392     /* We cannot swap the comparison here as that would cause us to
8393        endlessly recurse.  */
8394     return NULL_TREE;
8395
8396   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8397                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8398   if (code0 != INTEGER_CST)
8399     t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8400
8401   /* If swapping might yield to a more canonical form, do so.  */
8402   if (swap)
8403     return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8404   else
8405     return fold_build2_loc (loc, code, type, t, arg1);
8406 }
8407
8408 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8409    overflow further.  Try to decrease the magnitude of constants involved
8410    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8411    and put sole constants at the second argument position.
8412    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
8413
8414 static tree
8415 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8416                                tree arg0, tree arg1)
8417 {
8418   tree t;
8419   bool strict_overflow_p;
8420   const char * const warnmsg = G_("assuming signed overflow does not occur "
8421                                   "when reducing constant in comparison");
8422
8423   /* Try canonicalization by simplifying arg0.  */
8424   strict_overflow_p = false;
8425   t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8426                                        &strict_overflow_p);
8427   if (t)
8428     {
8429       if (strict_overflow_p)
8430         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8431       return t;
8432     }
8433
8434   /* Try canonicalization by simplifying arg1 using the swapped
8435      comparison.  */
8436   code = swap_tree_comparison (code);
8437   strict_overflow_p = false;
8438   t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8439                                        &strict_overflow_p);
8440   if (t && strict_overflow_p)
8441     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8442   return t;
8443 }
8444
8445 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8446    space.  This is used to avoid issuing overflow warnings for
8447    expressions like &p->x which can not wrap.  */
8448
8449 static bool
8450 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8451 {
8452   unsigned HOST_WIDE_INT offset_low, total_low;
8453   HOST_WIDE_INT size, offset_high, total_high;
8454
8455   if (!POINTER_TYPE_P (TREE_TYPE (base)))
8456     return true;
8457
8458   if (bitpos < 0)
8459     return true;
8460
8461   if (offset == NULL_TREE)
8462     {
8463       offset_low = 0;
8464       offset_high = 0;
8465     }
8466   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8467     return true;
8468   else
8469     {
8470       offset_low = TREE_INT_CST_LOW (offset);
8471       offset_high = TREE_INT_CST_HIGH (offset);
8472     }
8473
8474   if (add_double_with_sign (offset_low, offset_high,
8475                             bitpos / BITS_PER_UNIT, 0,
8476                             &total_low, &total_high,
8477                             true))
8478     return true;
8479
8480   if (total_high != 0)
8481     return true;
8482
8483   size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8484   if (size <= 0)
8485     return true;
8486
8487   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8488      array.  */
8489   if (TREE_CODE (base) == ADDR_EXPR)
8490     {
8491       HOST_WIDE_INT base_size;
8492
8493       base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8494       if (base_size > 0 && size < base_size)
8495         size = base_size;
8496     }
8497
8498   return total_low > (unsigned HOST_WIDE_INT) size;
8499 }
8500
8501 /* Subroutine of fold_binary.  This routine performs all of the
8502    transformations that are common to the equality/inequality
8503    operators (EQ_EXPR and NE_EXPR) and the ordering operators
8504    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
8505    fold_binary should call fold_binary.  Fold a comparison with
8506    tree code CODE and type TYPE with operands OP0 and OP1.  Return
8507    the folded comparison or NULL_TREE.  */
8508
8509 static tree
8510 fold_comparison (location_t loc, enum tree_code code, tree type,
8511                  tree op0, tree op1)
8512 {
8513   tree arg0, arg1, tem;
8514
8515   arg0 = op0;
8516   arg1 = op1;
8517
8518   STRIP_SIGN_NOPS (arg0);
8519   STRIP_SIGN_NOPS (arg1);
8520
8521   tem = fold_relational_const (code, type, arg0, arg1);
8522   if (tem != NULL_TREE)
8523     return tem;
8524
8525   /* If one arg is a real or integer constant, put it last.  */
8526   if (tree_swap_operands_p (arg0, arg1, true))
8527     return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
8528
8529   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
8530   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8531       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8532           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8533           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8534       && (TREE_CODE (arg1) == INTEGER_CST
8535           && !TREE_OVERFLOW (arg1)))
8536     {
8537       tree const1 = TREE_OPERAND (arg0, 1);
8538       tree const2 = arg1;
8539       tree variable = TREE_OPERAND (arg0, 0);
8540       tree lhs;
8541       int lhs_add;
8542       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8543
8544       lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
8545                          TREE_TYPE (arg1), const2, const1);
8546
8547       /* If the constant operation overflowed this can be
8548          simplified as a comparison against INT_MAX/INT_MIN.  */
8549       if (TREE_CODE (lhs) == INTEGER_CST
8550           && TREE_OVERFLOW (lhs))
8551         {
8552           int const1_sgn = tree_int_cst_sgn (const1);
8553           enum tree_code code2 = code;
8554
8555           /* Get the sign of the constant on the lhs if the
8556              operation were VARIABLE + CONST1.  */
8557           if (TREE_CODE (arg0) == MINUS_EXPR)
8558             const1_sgn = -const1_sgn;
8559
8560           /* The sign of the constant determines if we overflowed
8561              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8562              Canonicalize to the INT_MIN overflow by swapping the comparison
8563              if necessary.  */
8564           if (const1_sgn == -1)
8565             code2 = swap_tree_comparison (code);
8566
8567           /* We now can look at the canonicalized case
8568                VARIABLE + 1  CODE2  INT_MIN
8569              and decide on the result.  */
8570           if (code2 == LT_EXPR
8571               || code2 == LE_EXPR
8572               || code2 == EQ_EXPR)
8573             return omit_one_operand_loc (loc, type, boolean_false_node, variable);
8574           else if (code2 == NE_EXPR
8575                    || code2 == GE_EXPR
8576                    || code2 == GT_EXPR)
8577             return omit_one_operand_loc (loc, type, boolean_true_node, variable);
8578         }
8579
8580       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8581           && (TREE_CODE (lhs) != INTEGER_CST
8582               || !TREE_OVERFLOW (lhs)))
8583         {
8584           fold_overflow_warning (("assuming signed overflow does not occur "
8585                                   "when changing X +- C1 cmp C2 to "
8586                                   "X cmp C1 +- C2"),
8587                                  WARN_STRICT_OVERFLOW_COMPARISON);
8588           return fold_build2_loc (loc, code, type, variable, lhs);
8589         }
8590     }
8591
8592   /* For comparisons of pointers we can decompose it to a compile time
8593      comparison of the base objects and the offsets into the object.
8594      This requires at least one operand being an ADDR_EXPR or a
8595      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
8596   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8597       && (TREE_CODE (arg0) == ADDR_EXPR
8598           || TREE_CODE (arg1) == ADDR_EXPR
8599           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8600           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8601     {
8602       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8603       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8604       enum machine_mode mode;
8605       int volatilep, unsignedp;
8606       bool indirect_base0 = false, indirect_base1 = false;
8607
8608       /* Get base and offset for the access.  Strip ADDR_EXPR for
8609          get_inner_reference, but put it back by stripping INDIRECT_REF
8610          off the base object if possible.  indirect_baseN will be true
8611          if baseN is not an address but refers to the object itself.  */
8612       base0 = arg0;
8613       if (TREE_CODE (arg0) == ADDR_EXPR)
8614         {
8615           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8616                                        &bitsize, &bitpos0, &offset0, &mode,
8617                                        &unsignedp, &volatilep, false);
8618           if (TREE_CODE (base0) == INDIRECT_REF)
8619             base0 = TREE_OPERAND (base0, 0);
8620           else
8621             indirect_base0 = true;
8622         }
8623       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8624         {
8625           base0 = TREE_OPERAND (arg0, 0);
8626           offset0 = TREE_OPERAND (arg0, 1);
8627         }
8628
8629       base1 = arg1;
8630       if (TREE_CODE (arg1) == ADDR_EXPR)
8631         {
8632           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8633                                        &bitsize, &bitpos1, &offset1, &mode,
8634                                        &unsignedp, &volatilep, false);
8635           if (TREE_CODE (base1) == INDIRECT_REF)
8636             base1 = TREE_OPERAND (base1, 0);
8637           else
8638             indirect_base1 = true;
8639         }
8640       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8641         {
8642           base1 = TREE_OPERAND (arg1, 0);
8643           offset1 = TREE_OPERAND (arg1, 1);
8644         }
8645
8646       /* If we have equivalent bases we might be able to simplify.  */
8647       if (indirect_base0 == indirect_base1
8648           && operand_equal_p (base0, base1, 0))
8649         {
8650           /* We can fold this expression to a constant if the non-constant
8651              offset parts are equal.  */
8652           if ((offset0 == offset1
8653                || (offset0 && offset1
8654                    && operand_equal_p (offset0, offset1, 0)))
8655               && (code == EQ_EXPR
8656                   || code == NE_EXPR
8657                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
8658
8659             {
8660               if (code != EQ_EXPR
8661                   && code != NE_EXPR
8662                   && bitpos0 != bitpos1
8663                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
8664                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
8665                 fold_overflow_warning (("assuming pointer wraparound does not "
8666                                         "occur when comparing P +- C1 with "
8667                                         "P +- C2"),
8668                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
8669
8670               switch (code)
8671                 {
8672                 case EQ_EXPR:
8673                   return constant_boolean_node (bitpos0 == bitpos1, type);
8674                 case NE_EXPR:
8675                   return constant_boolean_node (bitpos0 != bitpos1, type);
8676                 case LT_EXPR:
8677                   return constant_boolean_node (bitpos0 < bitpos1, type);
8678                 case LE_EXPR:
8679                   return constant_boolean_node (bitpos0 <= bitpos1, type);
8680                 case GE_EXPR:
8681                   return constant_boolean_node (bitpos0 >= bitpos1, type);
8682                 case GT_EXPR:
8683                   return constant_boolean_node (bitpos0 > bitpos1, type);
8684                 default:;
8685                 }
8686             }
8687           /* We can simplify the comparison to a comparison of the variable
8688              offset parts if the constant offset parts are equal.
8689              Be careful to use signed size type here because otherwise we
8690              mess with array offsets in the wrong way.  This is possible
8691              because pointer arithmetic is restricted to retain within an
8692              object and overflow on pointer differences is undefined as of
8693              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
8694           else if (bitpos0 == bitpos1
8695                    && ((code == EQ_EXPR || code == NE_EXPR)
8696                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
8697             {
8698               /* By converting to signed size type we cover middle-end pointer
8699                  arithmetic which operates on unsigned pointer types of size
8700                  type size and ARRAY_REF offsets which are properly sign or
8701                  zero extended from their type in case it is narrower than
8702                  size type.  */
8703               if (offset0 == NULL_TREE)
8704                 offset0 = build_int_cst (ssizetype, 0);
8705               else
8706                 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8707               if (offset1 == NULL_TREE)
8708                 offset1 = build_int_cst (ssizetype, 0);
8709               else
8710                 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8711
8712               if (code != EQ_EXPR
8713                   && code != NE_EXPR
8714                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
8715                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
8716                 fold_overflow_warning (("assuming pointer wraparound does not "
8717                                         "occur when comparing P +- C1 with "
8718                                         "P +- C2"),
8719                                        WARN_STRICT_OVERFLOW_COMPARISON);
8720
8721               return fold_build2_loc (loc, code, type, offset0, offset1);
8722             }
8723         }
8724       /* For non-equal bases we can simplify if they are addresses
8725          of local binding decls or constants.  */
8726       else if (indirect_base0 && indirect_base1
8727                /* We know that !operand_equal_p (base0, base1, 0)
8728                   because the if condition was false.  But make
8729                   sure two decls are not the same.  */
8730                && base0 != base1
8731                && TREE_CODE (arg0) == ADDR_EXPR
8732                && TREE_CODE (arg1) == ADDR_EXPR
8733                && (((TREE_CODE (base0) == VAR_DECL
8734                      || TREE_CODE (base0) == PARM_DECL)
8735                     && (targetm.binds_local_p (base0)
8736                         || CONSTANT_CLASS_P (base1)))
8737                    || CONSTANT_CLASS_P (base0))
8738                && (((TREE_CODE (base1) == VAR_DECL
8739                      || TREE_CODE (base1) == PARM_DECL)
8740                     && (targetm.binds_local_p (base1)
8741                         || CONSTANT_CLASS_P (base0)))
8742                    || CONSTANT_CLASS_P (base1)))
8743         {
8744           if (code == EQ_EXPR)
8745             return omit_two_operands_loc (loc, type, boolean_false_node,
8746                                       arg0, arg1);
8747           else if (code == NE_EXPR)
8748             return omit_two_operands_loc (loc, type, boolean_true_node,
8749                                       arg0, arg1);
8750         }
8751       /* For equal offsets we can simplify to a comparison of the
8752          base addresses.  */
8753       else if (bitpos0 == bitpos1
8754                && (indirect_base0
8755                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8756                && (indirect_base1
8757                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8758                && ((offset0 == offset1)
8759                    || (offset0 && offset1
8760                        && operand_equal_p (offset0, offset1, 0))))
8761         {
8762           if (indirect_base0)
8763             base0 = build_fold_addr_expr_loc (loc, base0);
8764           if (indirect_base1)
8765             base1 = build_fold_addr_expr_loc (loc, base1);
8766           return fold_build2_loc (loc, code, type, base0, base1);
8767         }
8768     }
8769
8770   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8771      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
8772      the resulting offset is smaller in absolute value than the
8773      original one.  */
8774   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8775       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8776       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8777           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8778       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8779       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8780           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8781     {
8782       tree const1 = TREE_OPERAND (arg0, 1);
8783       tree const2 = TREE_OPERAND (arg1, 1);
8784       tree variable1 = TREE_OPERAND (arg0, 0);
8785       tree variable2 = TREE_OPERAND (arg1, 0);
8786       tree cst;
8787       const char * const warnmsg = G_("assuming signed overflow does not "
8788                                       "occur when combining constants around "
8789                                       "a comparison");
8790
8791       /* Put the constant on the side where it doesn't overflow and is
8792          of lower absolute value than before.  */
8793       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8794                              ? MINUS_EXPR : PLUS_EXPR,
8795                              const2, const1, 0);
8796       if (!TREE_OVERFLOW (cst)
8797           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8798         {
8799           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8800           return fold_build2_loc (loc, code, type,
8801                               variable1,
8802                               fold_build2_loc (loc,
8803                                            TREE_CODE (arg1), TREE_TYPE (arg1),
8804                                            variable2, cst));
8805         }
8806
8807       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8808                              ? MINUS_EXPR : PLUS_EXPR,
8809                              const1, const2, 0);
8810       if (!TREE_OVERFLOW (cst)
8811           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8812         {
8813           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8814           return fold_build2_loc (loc, code, type,
8815                               fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
8816                                            variable1, cst),
8817                               variable2);
8818         }
8819     }
8820
8821   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8822      signed arithmetic case.  That form is created by the compiler
8823      often enough for folding it to be of value.  One example is in
8824      computing loop trip counts after Operator Strength Reduction.  */
8825   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8826       && TREE_CODE (arg0) == MULT_EXPR
8827       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8828           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8829       && integer_zerop (arg1))
8830     {
8831       tree const1 = TREE_OPERAND (arg0, 1);
8832       tree const2 = arg1;                       /* zero */
8833       tree variable1 = TREE_OPERAND (arg0, 0);
8834       enum tree_code cmp_code = code;
8835
8836       /* Handle unfolded multiplication by zero.  */
8837       if (integer_zerop (const1))
8838         return fold_build2_loc (loc, cmp_code, type, const1, const2);
8839
8840       fold_overflow_warning (("assuming signed overflow does not occur when "
8841                               "eliminating multiplication in comparison "
8842                               "with zero"),
8843                              WARN_STRICT_OVERFLOW_COMPARISON);
8844
8845       /* If const1 is negative we swap the sense of the comparison.  */
8846       if (tree_int_cst_sgn (const1) < 0)
8847         cmp_code = swap_tree_comparison (cmp_code);
8848
8849       return fold_build2_loc (loc, cmp_code, type, variable1, const2);
8850     }
8851
8852   tem = maybe_canonicalize_comparison (loc, code, type, op0, op1);
8853   if (tem)
8854     return tem;
8855
8856   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8857     {
8858       tree targ0 = strip_float_extensions (arg0);
8859       tree targ1 = strip_float_extensions (arg1);
8860       tree newtype = TREE_TYPE (targ0);
8861
8862       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8863         newtype = TREE_TYPE (targ1);
8864
8865       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
8866       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8867         return fold_build2_loc (loc, code, type,
8868                             fold_convert_loc (loc, newtype, targ0),
8869                             fold_convert_loc (loc, newtype, targ1));
8870
8871       /* (-a) CMP (-b) -> b CMP a  */
8872       if (TREE_CODE (arg0) == NEGATE_EXPR
8873           && TREE_CODE (arg1) == NEGATE_EXPR)
8874         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
8875                             TREE_OPERAND (arg0, 0));
8876
8877       if (TREE_CODE (arg1) == REAL_CST)
8878         {
8879           REAL_VALUE_TYPE cst;
8880           cst = TREE_REAL_CST (arg1);
8881
8882           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
8883           if (TREE_CODE (arg0) == NEGATE_EXPR)
8884             return fold_build2_loc (loc, swap_tree_comparison (code), type,
8885                                 TREE_OPERAND (arg0, 0),
8886                                 build_real (TREE_TYPE (arg1),
8887                                             REAL_VALUE_NEGATE (cst)));
8888
8889           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
8890           /* a CMP (-0) -> a CMP 0  */
8891           if (REAL_VALUE_MINUS_ZERO (cst))
8892             return fold_build2_loc (loc, code, type, arg0,
8893                                 build_real (TREE_TYPE (arg1), dconst0));
8894
8895           /* x != NaN is always true, other ops are always false.  */
8896           if (REAL_VALUE_ISNAN (cst)
8897               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8898             {
8899               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8900               return omit_one_operand_loc (loc, type, tem, arg0);
8901             }
8902
8903           /* Fold comparisons against infinity.  */
8904           if (REAL_VALUE_ISINF (cst)
8905               && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
8906             {
8907               tem = fold_inf_compare (loc, code, type, arg0, arg1);
8908               if (tem != NULL_TREE)
8909                 return tem;
8910             }
8911         }
8912
8913       /* If this is a comparison of a real constant with a PLUS_EXPR
8914          or a MINUS_EXPR of a real constant, we can convert it into a
8915          comparison with a revised real constant as long as no overflow
8916          occurs when unsafe_math_optimizations are enabled.  */
8917       if (flag_unsafe_math_optimizations
8918           && TREE_CODE (arg1) == REAL_CST
8919           && (TREE_CODE (arg0) == PLUS_EXPR
8920               || TREE_CODE (arg0) == MINUS_EXPR)
8921           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
8922           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
8923                                       ? MINUS_EXPR : PLUS_EXPR,
8924                                       arg1, TREE_OPERAND (arg0, 1), 0))
8925           && !TREE_OVERFLOW (tem))
8926         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8927
8928       /* Likewise, we can simplify a comparison of a real constant with
8929          a MINUS_EXPR whose first operand is also a real constant, i.e.
8930          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
8931          floating-point types only if -fassociative-math is set.  */
8932       if (flag_associative_math
8933           && TREE_CODE (arg1) == REAL_CST
8934           && TREE_CODE (arg0) == MINUS_EXPR
8935           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
8936           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
8937                                       arg1, 0))
8938           && !TREE_OVERFLOW (tem))
8939         return fold_build2_loc (loc, swap_tree_comparison (code), type,
8940                             TREE_OPERAND (arg0, 1), tem);
8941
8942       /* Fold comparisons against built-in math functions.  */
8943       if (TREE_CODE (arg1) == REAL_CST
8944           && flag_unsafe_math_optimizations
8945           && ! flag_errno_math)
8946         {
8947           enum built_in_function fcode = builtin_mathfn_code (arg0);
8948
8949           if (fcode != END_BUILTINS)
8950             {
8951               tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
8952               if (tem != NULL_TREE)
8953                 return tem;
8954             }
8955         }
8956     }
8957
8958   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
8959       && CONVERT_EXPR_P (arg0))
8960     {
8961       /* If we are widening one operand of an integer comparison,
8962          see if the other operand is similarly being widened.  Perhaps we
8963          can do the comparison in the narrower type.  */
8964       tem = fold_widened_comparison (loc, code, type, arg0, arg1);
8965       if (tem)
8966         return tem;
8967
8968       /* Or if we are changing signedness.  */
8969       tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
8970       if (tem)
8971         return tem;
8972     }
8973
8974   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8975      constant, we can simplify it.  */
8976   if (TREE_CODE (arg1) == INTEGER_CST
8977       && (TREE_CODE (arg0) == MIN_EXPR
8978           || TREE_CODE (arg0) == MAX_EXPR)
8979       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8980     {
8981       tem = optimize_minmax_comparison (loc, code, type, op0, op1);
8982       if (tem)
8983         return tem;
8984     }
8985
8986   /* Simplify comparison of something with itself.  (For IEEE
8987      floating-point, we can only do some of these simplifications.)  */
8988   if (operand_equal_p (arg0, arg1, 0))
8989     {
8990       switch (code)
8991         {
8992         case EQ_EXPR:
8993           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
8994               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8995             return constant_boolean_node (1, type);
8996           break;
8997
8998         case GE_EXPR:
8999         case LE_EXPR:
9000           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9001               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9002             return constant_boolean_node (1, type);
9003           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9004
9005         case NE_EXPR:
9006           /* For NE, we can only do this simplification if integer
9007              or we don't honor IEEE floating point NaNs.  */
9008           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9009               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9010             break;
9011           /* ... fall through ...  */
9012         case GT_EXPR:
9013         case LT_EXPR:
9014           return constant_boolean_node (0, type);
9015         default:
9016           gcc_unreachable ();
9017         }
9018     }
9019
9020   /* If we are comparing an expression that just has comparisons
9021      of two integer values, arithmetic expressions of those comparisons,
9022      and constants, we can simplify it.  There are only three cases
9023      to check: the two values can either be equal, the first can be
9024      greater, or the second can be greater.  Fold the expression for
9025      those three values.  Since each value must be 0 or 1, we have
9026      eight possibilities, each of which corresponds to the constant 0
9027      or 1 or one of the six possible comparisons.
9028
9029      This handles common cases like (a > b) == 0 but also handles
9030      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9031      occur in macroized code.  */
9032
9033   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9034     {
9035       tree cval1 = 0, cval2 = 0;
9036       int save_p = 0;
9037
9038       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9039           /* Don't handle degenerate cases here; they should already
9040              have been handled anyway.  */
9041           && cval1 != 0 && cval2 != 0
9042           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9043           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9044           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9045           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9046           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9047           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9048                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9049         {
9050           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9051           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9052
9053           /* We can't just pass T to eval_subst in case cval1 or cval2
9054              was the same as ARG1.  */
9055
9056           tree high_result
9057                 = fold_build2_loc (loc, code, type,
9058                                eval_subst (loc, arg0, cval1, maxval,
9059                                            cval2, minval),
9060                                arg1);
9061           tree equal_result
9062                 = fold_build2_loc (loc, code, type,
9063                                eval_subst (loc, arg0, cval1, maxval,
9064                                            cval2, maxval),
9065                                arg1);
9066           tree low_result
9067                 = fold_build2_loc (loc, code, type,
9068                                eval_subst (loc, arg0, cval1, minval,
9069                                            cval2, maxval),
9070                                arg1);
9071
9072           /* All three of these results should be 0 or 1.  Confirm they are.
9073              Then use those values to select the proper code to use.  */
9074
9075           if (TREE_CODE (high_result) == INTEGER_CST
9076               && TREE_CODE (equal_result) == INTEGER_CST
9077               && TREE_CODE (low_result) == INTEGER_CST)
9078             {
9079               /* Make a 3-bit mask with the high-order bit being the
9080                  value for `>', the next for '=', and the low for '<'.  */
9081               switch ((integer_onep (high_result) * 4)
9082                       + (integer_onep (equal_result) * 2)
9083                       + integer_onep (low_result))
9084                 {
9085                 case 0:
9086                   /* Always false.  */
9087                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9088                 case 1:
9089                   code = LT_EXPR;
9090                   break;
9091                 case 2:
9092                   code = EQ_EXPR;
9093                   break;
9094                 case 3:
9095                   code = LE_EXPR;
9096                   break;
9097                 case 4:
9098                   code = GT_EXPR;
9099                   break;
9100                 case 5:
9101                   code = NE_EXPR;
9102                   break;
9103                 case 6:
9104                   code = GE_EXPR;
9105                   break;
9106                 case 7:
9107                   /* Always true.  */
9108                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9109                 }
9110
9111               if (save_p)
9112                 {
9113                   tem = save_expr (build2 (code, type, cval1, cval2));
9114                   SET_EXPR_LOCATION (tem, loc);
9115                   return tem;
9116                 }
9117               return fold_build2_loc (loc, code, type, cval1, cval2);
9118             }
9119         }
9120     }
9121
9122   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9123      into a single range test.  */
9124   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9125        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9126       && TREE_CODE (arg1) == INTEGER_CST
9127       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9128       && !integer_zerop (TREE_OPERAND (arg0, 1))
9129       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9130       && !TREE_OVERFLOW (arg1))
9131     {
9132       tem = fold_div_compare (loc, code, type, arg0, arg1);
9133       if (tem != NULL_TREE)
9134         return tem;
9135     }
9136
9137   /* Fold ~X op ~Y as Y op X.  */
9138   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9139       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9140     {
9141       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9142       return fold_build2_loc (loc, code, type,
9143                           fold_convert_loc (loc, cmp_type,
9144                                             TREE_OPERAND (arg1, 0)),
9145                           TREE_OPERAND (arg0, 0));
9146     }
9147
9148   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9149   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9150       && TREE_CODE (arg1) == INTEGER_CST)
9151     {
9152       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9153       return fold_build2_loc (loc, swap_tree_comparison (code), type,
9154                           TREE_OPERAND (arg0, 0),
9155                           fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9156                                        fold_convert_loc (loc, cmp_type, arg1)));
9157     }
9158
9159   return NULL_TREE;
9160 }
9161
9162
9163 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9164    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9165    argument EXPR represents the expression "z" of type TYPE.  */
9166
9167 static tree
9168 fold_mult_zconjz (location_t loc, tree type, tree expr)
9169 {
9170   tree itype = TREE_TYPE (type);
9171   tree rpart, ipart, tem;
9172
9173   if (TREE_CODE (expr) == COMPLEX_EXPR)
9174     {
9175       rpart = TREE_OPERAND (expr, 0);
9176       ipart = TREE_OPERAND (expr, 1);
9177     }
9178   else if (TREE_CODE (expr) == COMPLEX_CST)
9179     {
9180       rpart = TREE_REALPART (expr);
9181       ipart = TREE_IMAGPART (expr);
9182     }
9183   else
9184     {
9185       expr = save_expr (expr);
9186       rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9187       ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9188     }
9189
9190   rpart = save_expr (rpart);
9191   ipart = save_expr (ipart);
9192   tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9193                      fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9194                      fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9195   return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9196                       fold_convert_loc (loc, itype, integer_zero_node));
9197 }
9198
9199
9200 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9201    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9202    guarantees that P and N have the same least significant log2(M) bits.
9203    N is not otherwise constrained.  In particular, N is not normalized to
9204    0 <= N < M as is common.  In general, the precise value of P is unknown.
9205    M is chosen as large as possible such that constant N can be determined.
9206
9207    Returns M and sets *RESIDUE to N.
9208
9209    If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9210    account.  This is not always possible due to PR 35705.
9211  */
9212
9213 static unsigned HOST_WIDE_INT
9214 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9215                                  bool allow_func_align)
9216 {
9217   enum tree_code code;
9218
9219   *residue = 0;
9220
9221   code = TREE_CODE (expr);
9222   if (code == ADDR_EXPR)
9223     {
9224       expr = TREE_OPERAND (expr, 0);
9225       if (handled_component_p (expr))
9226         {
9227           HOST_WIDE_INT bitsize, bitpos;
9228           tree offset;
9229           enum machine_mode mode;
9230           int unsignedp, volatilep;
9231
9232           expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9233                                       &mode, &unsignedp, &volatilep, false);
9234           *residue = bitpos / BITS_PER_UNIT;
9235           if (offset)
9236             {
9237               if (TREE_CODE (offset) == INTEGER_CST)
9238                 *residue += TREE_INT_CST_LOW (offset);
9239               else
9240                 /* We don't handle more complicated offset expressions.  */
9241                 return 1;
9242             }
9243         }
9244
9245       if (DECL_P (expr)
9246           && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL))
9247         return DECL_ALIGN_UNIT (expr);
9248     }
9249   else if (code == POINTER_PLUS_EXPR)
9250     {
9251       tree op0, op1;
9252       unsigned HOST_WIDE_INT modulus;
9253       enum tree_code inner_code;
9254
9255       op0 = TREE_OPERAND (expr, 0);
9256       STRIP_NOPS (op0);
9257       modulus = get_pointer_modulus_and_residue (op0, residue,
9258                                                  allow_func_align);
9259
9260       op1 = TREE_OPERAND (expr, 1);
9261       STRIP_NOPS (op1);
9262       inner_code = TREE_CODE (op1);
9263       if (inner_code == INTEGER_CST)
9264         {
9265           *residue += TREE_INT_CST_LOW (op1);
9266           return modulus;
9267         }
9268       else if (inner_code == MULT_EXPR)
9269         {
9270           op1 = TREE_OPERAND (op1, 1);
9271           if (TREE_CODE (op1) == INTEGER_CST)
9272             {
9273               unsigned HOST_WIDE_INT align;
9274
9275               /* Compute the greatest power-of-2 divisor of op1.  */
9276               align = TREE_INT_CST_LOW (op1);
9277               align &= -align;
9278
9279               /* If align is non-zero and less than *modulus, replace
9280                  *modulus with align., If align is 0, then either op1 is 0
9281                  or the greatest power-of-2 divisor of op1 doesn't fit in an
9282                  unsigned HOST_WIDE_INT.  In either case, no additional
9283                  constraint is imposed.  */
9284               if (align)
9285                 modulus = MIN (modulus, align);
9286
9287               return modulus;
9288             }
9289         }
9290     }
9291
9292     /* If we get here, we were unable to determine anything useful about the
9293        expression.  */
9294     return 1;
9295 }
9296
9297
9298 /* Fold a binary expression of code CODE and type TYPE with operands
9299    OP0 and OP1.  LOC is the location of the resulting expression.
9300    Return the folded expression if folding is successful.  Otherwise,
9301    return NULL_TREE.  */
9302
9303 tree
9304 fold_binary_loc (location_t loc,
9305              enum tree_code code, tree type, tree op0, tree op1)
9306 {
9307   enum tree_code_class kind = TREE_CODE_CLASS (code);
9308   tree arg0, arg1, tem;
9309   tree t1 = NULL_TREE;
9310   bool strict_overflow_p;
9311
9312   gcc_assert (IS_EXPR_CODE_CLASS (kind)
9313               && TREE_CODE_LENGTH (code) == 2
9314               && op0 != NULL_TREE
9315               && op1 != NULL_TREE);
9316
9317   arg0 = op0;
9318   arg1 = op1;
9319
9320   /* Strip any conversions that don't change the mode.  This is
9321      safe for every expression, except for a comparison expression
9322      because its signedness is derived from its operands.  So, in
9323      the latter case, only strip conversions that don't change the
9324      signedness.  MIN_EXPR/MAX_EXPR also need signedness of arguments
9325      preserved.
9326
9327      Note that this is done as an internal manipulation within the
9328      constant folder, in order to find the simplest representation
9329      of the arguments so that their form can be studied.  In any
9330      cases, the appropriate type conversions should be put back in
9331      the tree that will get out of the constant folder.  */
9332
9333   if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9334     {
9335       STRIP_SIGN_NOPS (arg0);
9336       STRIP_SIGN_NOPS (arg1);
9337     }
9338   else
9339     {
9340       STRIP_NOPS (arg0);
9341       STRIP_NOPS (arg1);
9342     }
9343
9344   /* Note that TREE_CONSTANT isn't enough: static var addresses are
9345      constant but we can't do arithmetic on them.  */
9346   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9347       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9348       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9349       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9350       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9351       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9352     {
9353       if (kind == tcc_binary)
9354         {
9355           /* Make sure type and arg0 have the same saturating flag.  */
9356           gcc_assert (TYPE_SATURATING (type)
9357                       == TYPE_SATURATING (TREE_TYPE (arg0)));
9358           tem = const_binop (code, arg0, arg1, 0);
9359         }
9360       else if (kind == tcc_comparison)
9361         tem = fold_relational_const (code, type, arg0, arg1);
9362       else
9363         tem = NULL_TREE;
9364
9365       if (tem != NULL_TREE)
9366         {
9367           if (TREE_TYPE (tem) != type)
9368             tem = fold_convert_loc (loc, type, tem);
9369           return tem;
9370         }
9371     }
9372
9373   /* If this is a commutative operation, and ARG0 is a constant, move it
9374      to ARG1 to reduce the number of tests below.  */
9375   if (commutative_tree_code (code)
9376       && tree_swap_operands_p (arg0, arg1, true))
9377     return fold_build2_loc (loc, code, type, op1, op0);
9378
9379   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9380
9381      First check for cases where an arithmetic operation is applied to a
9382      compound, conditional, or comparison operation.  Push the arithmetic
9383      operation inside the compound or conditional to see if any folding
9384      can then be done.  Convert comparison to conditional for this purpose.
9385      The also optimizes non-constant cases that used to be done in
9386      expand_expr.
9387
9388      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9389      one of the operands is a comparison and the other is a comparison, a
9390      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
9391      code below would make the expression more complex.  Change it to a
9392      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
9393      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
9394
9395   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9396        || code == EQ_EXPR || code == NE_EXPR)
9397       && ((truth_value_p (TREE_CODE (arg0))
9398            && (truth_value_p (TREE_CODE (arg1))
9399                || (TREE_CODE (arg1) == BIT_AND_EXPR
9400                    && integer_onep (TREE_OPERAND (arg1, 1)))))
9401           || (truth_value_p (TREE_CODE (arg1))
9402               && (truth_value_p (TREE_CODE (arg0))
9403                   || (TREE_CODE (arg0) == BIT_AND_EXPR
9404                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
9405     {
9406       tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9407                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9408                          : TRUTH_XOR_EXPR,
9409                          boolean_type_node,
9410                          fold_convert_loc (loc, boolean_type_node, arg0),
9411                          fold_convert_loc (loc, boolean_type_node, arg1));
9412
9413       if (code == EQ_EXPR)
9414         tem = invert_truthvalue_loc (loc, tem);
9415
9416       return fold_convert_loc (loc, type, tem);
9417     }
9418
9419   if (TREE_CODE_CLASS (code) == tcc_binary
9420       || TREE_CODE_CLASS (code) == tcc_comparison)
9421     {
9422       if (TREE_CODE (arg0) == COMPOUND_EXPR)
9423         {
9424           tem = fold_build2_loc (loc, code, type,
9425                              fold_convert_loc (loc, TREE_TYPE (op0),
9426                                                TREE_OPERAND (arg0, 1)), op1);
9427           tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), tem);
9428           goto fold_binary_exit;
9429         }
9430       if (TREE_CODE (arg1) == COMPOUND_EXPR
9431           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9432         {
9433           tem = fold_build2_loc (loc, code, type, op0,
9434                              fold_convert_loc (loc, TREE_TYPE (op1),
9435                                                TREE_OPERAND (arg1, 1)));
9436           tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0), tem);
9437           goto fold_binary_exit;
9438         }
9439
9440       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9441         {
9442           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9443                                                      arg0, arg1,
9444                                                      /*cond_first_p=*/1);
9445           if (tem != NULL_TREE)
9446             return tem;
9447         }
9448
9449       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9450         {
9451           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9452                                                      arg1, arg0,
9453                                                      /*cond_first_p=*/0);
9454           if (tem != NULL_TREE)
9455             return tem;
9456         }
9457     }
9458
9459   switch (code)
9460     {
9461     case POINTER_PLUS_EXPR:
9462       /* 0 +p index -> (type)index */
9463       if (integer_zerop (arg0))
9464         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9465
9466       /* PTR +p 0 -> PTR */
9467       if (integer_zerop (arg1))
9468         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9469
9470       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
9471       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9472            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9473         return fold_convert_loc (loc, type,
9474                                  fold_build2_loc (loc, PLUS_EXPR, sizetype,
9475                                               fold_convert_loc (loc, sizetype,
9476                                                                 arg1),
9477                                               fold_convert_loc (loc, sizetype,
9478                                                                 arg0)));
9479
9480       /* index +p PTR -> PTR +p index */
9481       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9482           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9483         return fold_build2_loc (loc, POINTER_PLUS_EXPR, type,
9484                             fold_convert_loc (loc, type, arg1),
9485                             fold_convert_loc (loc, sizetype, arg0));
9486
9487       /* (PTR +p B) +p A -> PTR +p (B + A) */
9488       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9489         {
9490           tree inner;
9491           tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
9492           tree arg00 = TREE_OPERAND (arg0, 0);
9493           inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
9494                                arg01, fold_convert_loc (loc, sizetype, arg1));
9495           return fold_convert_loc (loc, type,
9496                                    fold_build2_loc (loc, POINTER_PLUS_EXPR,
9497                                                 TREE_TYPE (arg00),
9498                                                 arg00, inner));
9499         }
9500
9501       /* PTR_CST +p CST -> CST1 */
9502       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9503         return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
9504                             fold_convert_loc (loc, type, arg1));
9505
9506      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9507         of the array.  Loop optimizer sometimes produce this type of
9508         expressions.  */
9509       if (TREE_CODE (arg0) == ADDR_EXPR)
9510         {
9511           tem = try_move_mult_to_index (loc, arg0,
9512                                         fold_convert_loc (loc, sizetype, arg1));
9513           if (tem)
9514             return fold_convert_loc (loc, type, tem);
9515         }
9516
9517       return NULL_TREE;
9518
9519     case PLUS_EXPR:
9520       /* A + (-B) -> A - B */
9521       if (TREE_CODE (arg1) == NEGATE_EXPR)
9522         return fold_build2_loc (loc, MINUS_EXPR, type,
9523                             fold_convert_loc (loc, type, arg0),
9524                             fold_convert_loc (loc, type,
9525                                               TREE_OPERAND (arg1, 0)));
9526       /* (-A) + B -> B - A */
9527       if (TREE_CODE (arg0) == NEGATE_EXPR
9528           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9529         return fold_build2_loc (loc, MINUS_EXPR, type,
9530                             fold_convert_loc (loc, type, arg1),
9531                             fold_convert_loc (loc, type,
9532                                               TREE_OPERAND (arg0, 0)));
9533
9534       if (INTEGRAL_TYPE_P (type))
9535         {
9536           /* Convert ~A + 1 to -A.  */
9537           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9538               && integer_onep (arg1))
9539             return fold_build1_loc (loc, NEGATE_EXPR, type,
9540                                 fold_convert_loc (loc, type,
9541                                                   TREE_OPERAND (arg0, 0)));
9542
9543           /* ~X + X is -1.  */
9544           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9545               && !TYPE_OVERFLOW_TRAPS (type))
9546             {
9547               tree tem = TREE_OPERAND (arg0, 0);
9548
9549               STRIP_NOPS (tem);
9550               if (operand_equal_p (tem, arg1, 0))
9551                 {
9552                   t1 = build_int_cst_type (type, -1);
9553                   return omit_one_operand_loc (loc, type, t1, arg1);
9554                 }
9555             }
9556
9557           /* X + ~X is -1.  */
9558           if (TREE_CODE (arg1) == BIT_NOT_EXPR
9559               && !TYPE_OVERFLOW_TRAPS (type))
9560             {
9561               tree tem = TREE_OPERAND (arg1, 0);
9562
9563               STRIP_NOPS (tem);
9564               if (operand_equal_p (arg0, tem, 0))
9565                 {
9566                   t1 = build_int_cst_type (type, -1);
9567                   return omit_one_operand_loc (loc, type, t1, arg0);
9568                 }
9569             }
9570
9571           /* X + (X / CST) * -CST is X % CST.  */
9572           if (TREE_CODE (arg1) == MULT_EXPR
9573               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9574               && operand_equal_p (arg0,
9575                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9576             {
9577               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9578               tree cst1 = TREE_OPERAND (arg1, 1);
9579               tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9580                                       cst1, cst0);
9581               if (sum && integer_zerop (sum))
9582                 return fold_convert_loc (loc, type,
9583                                          fold_build2_loc (loc, TRUNC_MOD_EXPR,
9584                                                       TREE_TYPE (arg0), arg0,
9585                                                       cst0));
9586             }
9587         }
9588
9589       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9590          same or one.  Make sure type is not saturating.
9591          fold_plusminus_mult_expr will re-associate.  */
9592       if ((TREE_CODE (arg0) == MULT_EXPR
9593            || TREE_CODE (arg1) == MULT_EXPR)
9594           && !TYPE_SATURATING (type)
9595           && (!FLOAT_TYPE_P (type) || flag_associative_math))
9596         {
9597           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9598           if (tem)
9599             return tem;
9600         }
9601
9602       if (! FLOAT_TYPE_P (type))
9603         {
9604           if (integer_zerop (arg1))
9605             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9606
9607           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9608              with a constant, and the two constants have no bits in common,
9609              we should treat this as a BIT_IOR_EXPR since this may produce more
9610              simplifications.  */
9611           if (TREE_CODE (arg0) == BIT_AND_EXPR
9612               && TREE_CODE (arg1) == BIT_AND_EXPR
9613               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9614               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9615               && integer_zerop (const_binop (BIT_AND_EXPR,
9616                                              TREE_OPERAND (arg0, 1),
9617                                              TREE_OPERAND (arg1, 1), 0)))
9618             {
9619               code = BIT_IOR_EXPR;
9620               goto bit_ior;
9621             }
9622
9623           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9624              (plus (plus (mult) (mult)) (foo)) so that we can
9625              take advantage of the factoring cases below.  */
9626           if (((TREE_CODE (arg0) == PLUS_EXPR
9627                 || TREE_CODE (arg0) == MINUS_EXPR)
9628                && TREE_CODE (arg1) == MULT_EXPR)
9629               || ((TREE_CODE (arg1) == PLUS_EXPR
9630                    || TREE_CODE (arg1) == MINUS_EXPR)
9631                   && TREE_CODE (arg0) == MULT_EXPR))
9632             {
9633               tree parg0, parg1, parg, marg;
9634               enum tree_code pcode;
9635
9636               if (TREE_CODE (arg1) == MULT_EXPR)
9637                 parg = arg0, marg = arg1;
9638               else
9639                 parg = arg1, marg = arg0;
9640               pcode = TREE_CODE (parg);
9641               parg0 = TREE_OPERAND (parg, 0);
9642               parg1 = TREE_OPERAND (parg, 1);
9643               STRIP_NOPS (parg0);
9644               STRIP_NOPS (parg1);
9645
9646               if (TREE_CODE (parg0) == MULT_EXPR
9647                   && TREE_CODE (parg1) != MULT_EXPR)
9648                 return fold_build2_loc (loc, pcode, type,
9649                                     fold_build2_loc (loc, PLUS_EXPR, type,
9650                                                  fold_convert_loc (loc, type,
9651                                                                    parg0),
9652                                                  fold_convert_loc (loc, type,
9653                                                                    marg)),
9654                                     fold_convert_loc (loc, type, parg1));
9655               if (TREE_CODE (parg0) != MULT_EXPR
9656                   && TREE_CODE (parg1) == MULT_EXPR)
9657                 return
9658                   fold_build2_loc (loc, PLUS_EXPR, type,
9659                                fold_convert_loc (loc, type, parg0),
9660                                fold_build2_loc (loc, pcode, type,
9661                                             fold_convert_loc (loc, type, marg),
9662                                             fold_convert_loc (loc, type,
9663                                                               parg1)));
9664             }
9665         }
9666       else
9667         {
9668           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
9669           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9670             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9671
9672           /* Likewise if the operands are reversed.  */
9673           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9674             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9675
9676           /* Convert X + -C into X - C.  */
9677           if (TREE_CODE (arg1) == REAL_CST
9678               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9679             {
9680               tem = fold_negate_const (arg1, type);
9681               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9682                 return fold_build2_loc (loc, MINUS_EXPR, type,
9683                                     fold_convert_loc (loc, type, arg0),
9684                                     fold_convert_loc (loc, type, tem));
9685             }
9686
9687           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9688              to __complex__ ( x, y ).  This is not the same for SNaNs or
9689              if signed zeros are involved.  */
9690           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9691               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9692               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9693             {
9694               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9695               tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9696               tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9697               bool arg0rz = false, arg0iz = false;
9698               if ((arg0r && (arg0rz = real_zerop (arg0r)))
9699                   || (arg0i && (arg0iz = real_zerop (arg0i))))
9700                 {
9701                   tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9702                   tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9703                   if (arg0rz && arg1i && real_zerop (arg1i))
9704                     {
9705                       tree rp = arg1r ? arg1r
9706                                   : build1 (REALPART_EXPR, rtype, arg1);
9707                       tree ip = arg0i ? arg0i
9708                                   : build1 (IMAGPART_EXPR, rtype, arg0);
9709                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9710                     }
9711                   else if (arg0iz && arg1r && real_zerop (arg1r))
9712                     {
9713                       tree rp = arg0r ? arg0r
9714                                   : build1 (REALPART_EXPR, rtype, arg0);
9715                       tree ip = arg1i ? arg1i
9716                                   : build1 (IMAGPART_EXPR, rtype, arg1);
9717                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9718                     }
9719                 }
9720             }
9721
9722           if (flag_unsafe_math_optimizations
9723               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9724               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9725               && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9726             return tem;
9727
9728           /* Convert x+x into x*2.0.  */
9729           if (operand_equal_p (arg0, arg1, 0)
9730               && SCALAR_FLOAT_TYPE_P (type))
9731             return fold_build2_loc (loc, MULT_EXPR, type, arg0,
9732                                 build_real (type, dconst2));
9733
9734           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9735              We associate floats only if the user has specified
9736              -fassociative-math.  */
9737           if (flag_associative_math
9738               && TREE_CODE (arg1) == PLUS_EXPR
9739               && TREE_CODE (arg0) != MULT_EXPR)
9740             {
9741               tree tree10 = TREE_OPERAND (arg1, 0);
9742               tree tree11 = TREE_OPERAND (arg1, 1);
9743               if (TREE_CODE (tree11) == MULT_EXPR
9744                   && TREE_CODE (tree10) == MULT_EXPR)
9745                 {
9746                   tree tree0;
9747                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9748                   return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9749                 }
9750             }
9751           /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9752              We associate floats only if the user has specified
9753              -fassociative-math.  */
9754           if (flag_associative_math
9755               && TREE_CODE (arg0) == PLUS_EXPR
9756               && TREE_CODE (arg1) != MULT_EXPR)
9757             {
9758               tree tree00 = TREE_OPERAND (arg0, 0);
9759               tree tree01 = TREE_OPERAND (arg0, 1);
9760               if (TREE_CODE (tree01) == MULT_EXPR
9761                   && TREE_CODE (tree00) == MULT_EXPR)
9762                 {
9763                   tree tree0;
9764                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9765                   return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9766                 }
9767             }
9768         }
9769
9770      bit_rotate:
9771       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9772          is a rotate of A by C1 bits.  */
9773       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9774          is a rotate of A by B bits.  */
9775       {
9776         enum tree_code code0, code1;
9777         tree rtype;
9778         code0 = TREE_CODE (arg0);
9779         code1 = TREE_CODE (arg1);
9780         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9781              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9782             && operand_equal_p (TREE_OPERAND (arg0, 0),
9783                                 TREE_OPERAND (arg1, 0), 0)
9784             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9785                 TYPE_UNSIGNED (rtype))
9786             /* Only create rotates in complete modes.  Other cases are not
9787                expanded properly.  */
9788             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
9789           {
9790             tree tree01, tree11;
9791             enum tree_code code01, code11;
9792
9793             tree01 = TREE_OPERAND (arg0, 1);
9794             tree11 = TREE_OPERAND (arg1, 1);
9795             STRIP_NOPS (tree01);
9796             STRIP_NOPS (tree11);
9797             code01 = TREE_CODE (tree01);
9798             code11 = TREE_CODE (tree11);
9799             if (code01 == INTEGER_CST
9800                 && code11 == INTEGER_CST
9801                 && TREE_INT_CST_HIGH (tree01) == 0
9802                 && TREE_INT_CST_HIGH (tree11) == 0
9803                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9804                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9805               {
9806                 tem = build2 (LROTATE_EXPR,
9807                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
9808                               TREE_OPERAND (arg0, 0),
9809                               code0 == LSHIFT_EXPR
9810                               ? tree01 : tree11);
9811                 SET_EXPR_LOCATION (tem, loc);
9812                 return fold_convert_loc (loc, type, tem);
9813               }
9814             else if (code11 == MINUS_EXPR)
9815               {
9816                 tree tree110, tree111;
9817                 tree110 = TREE_OPERAND (tree11, 0);
9818                 tree111 = TREE_OPERAND (tree11, 1);
9819                 STRIP_NOPS (tree110);
9820                 STRIP_NOPS (tree111);
9821                 if (TREE_CODE (tree110) == INTEGER_CST
9822                     && 0 == compare_tree_int (tree110,
9823                                               TYPE_PRECISION
9824                                               (TREE_TYPE (TREE_OPERAND
9825                                                           (arg0, 0))))
9826                     && operand_equal_p (tree01, tree111, 0))
9827                   return
9828                     fold_convert_loc (loc, type,
9829                                       build2 ((code0 == LSHIFT_EXPR
9830                                                ? LROTATE_EXPR
9831                                                : RROTATE_EXPR),
9832                                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
9833                                               TREE_OPERAND (arg0, 0), tree01));
9834               }
9835             else if (code01 == MINUS_EXPR)
9836               {
9837                 tree tree010, tree011;
9838                 tree010 = TREE_OPERAND (tree01, 0);
9839                 tree011 = TREE_OPERAND (tree01, 1);
9840                 STRIP_NOPS (tree010);
9841                 STRIP_NOPS (tree011);
9842                 if (TREE_CODE (tree010) == INTEGER_CST
9843                     && 0 == compare_tree_int (tree010,
9844                                               TYPE_PRECISION
9845                                               (TREE_TYPE (TREE_OPERAND
9846                                                           (arg0, 0))))
9847                     && operand_equal_p (tree11, tree011, 0))
9848                     return fold_convert_loc
9849                       (loc, type,
9850                        build2 ((code0 != LSHIFT_EXPR
9851                                 ? LROTATE_EXPR
9852                                 : RROTATE_EXPR),
9853                                TREE_TYPE (TREE_OPERAND (arg0, 0)),
9854                                TREE_OPERAND (arg0, 0), tree11));
9855               }
9856           }
9857       }
9858
9859     associate:
9860       /* In most languages, can't associate operations on floats through
9861          parentheses.  Rather than remember where the parentheses were, we
9862          don't associate floats at all, unless the user has specified
9863          -fassociative-math.
9864          And, we need to make sure type is not saturating.  */
9865
9866       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9867           && !TYPE_SATURATING (type))
9868         {
9869           tree var0, con0, lit0, minus_lit0;
9870           tree var1, con1, lit1, minus_lit1;
9871           bool ok = true;
9872
9873           /* Split both trees into variables, constants, and literals.  Then
9874              associate each group together, the constants with literals,
9875              then the result with variables.  This increases the chances of
9876              literals being recombined later and of generating relocatable
9877              expressions for the sum of a constant and literal.  */
9878           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9879           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9880                              code == MINUS_EXPR);
9881
9882           /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
9883           if (code == MINUS_EXPR)
9884             code = PLUS_EXPR;
9885
9886           /* With undefined overflow we can only associate constants with one
9887              variable, and constants whose association doesn't overflow.  */
9888           if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9889               || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9890             {
9891               if (var0 && var1)
9892                 {
9893                   tree tmp0 = var0;
9894                   tree tmp1 = var1;
9895
9896                   if (TREE_CODE (tmp0) == NEGATE_EXPR)
9897                     tmp0 = TREE_OPERAND (tmp0, 0);
9898                   if (TREE_CODE (tmp1) == NEGATE_EXPR)
9899                     tmp1 = TREE_OPERAND (tmp1, 0);
9900                   /* The only case we can still associate with two variables
9901                      is if they are the same, modulo negation.  */
9902                   if (!operand_equal_p (tmp0, tmp1, 0))
9903                     ok = false;
9904                 }
9905
9906               if (ok && lit0 && lit1)
9907                 {
9908                   tree tmp0 = fold_convert (type, lit0);
9909                   tree tmp1 = fold_convert (type, lit1);
9910
9911                   if (!TREE_OVERFLOW (tmp0) && !TREE_OVERFLOW (tmp1)
9912                       && TREE_OVERFLOW (fold_build2 (code, type, tmp0, tmp1)))
9913                     ok = false;
9914                 }
9915             }
9916
9917           /* Only do something if we found more than two objects.  Otherwise,
9918              nothing has changed and we risk infinite recursion.  */
9919           if (ok
9920               && (2 < ((var0 != 0) + (var1 != 0)
9921                        + (con0 != 0) + (con1 != 0)
9922                        + (lit0 != 0) + (lit1 != 0)
9923                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
9924             {
9925               var0 = associate_trees (loc, var0, var1, code, type);
9926               con0 = associate_trees (loc, con0, con1, code, type);
9927               lit0 = associate_trees (loc, lit0, lit1, code, type);
9928               minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type);
9929
9930               /* Preserve the MINUS_EXPR if the negative part of the literal is
9931                  greater than the positive part.  Otherwise, the multiplicative
9932                  folding code (i.e extract_muldiv) may be fooled in case
9933                  unsigned constants are subtracted, like in the following
9934                  example: ((X*2 + 4) - 8U)/2.  */
9935               if (minus_lit0 && lit0)
9936                 {
9937                   if (TREE_CODE (lit0) == INTEGER_CST
9938                       && TREE_CODE (minus_lit0) == INTEGER_CST
9939                       && tree_int_cst_lt (lit0, minus_lit0))
9940                     {
9941                       minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9942                                                     MINUS_EXPR, type);
9943                       lit0 = 0;
9944                     }
9945                   else
9946                     {
9947                       lit0 = associate_trees (loc, lit0, minus_lit0,
9948                                               MINUS_EXPR, type);
9949                       minus_lit0 = 0;
9950                     }
9951                 }
9952               if (minus_lit0)
9953                 {
9954                   if (con0 == 0)
9955                     return
9956                       fold_convert_loc (loc, type,
9957                                         associate_trees (loc, var0, minus_lit0,
9958                                                          MINUS_EXPR, type));
9959                   else
9960                     {
9961                       con0 = associate_trees (loc, con0, minus_lit0,
9962                                               MINUS_EXPR, type);
9963                       return
9964                         fold_convert_loc (loc, type,
9965                                           associate_trees (loc, var0, con0,
9966                                                            PLUS_EXPR, type));
9967                     }
9968                 }
9969
9970               con0 = associate_trees (loc, con0, lit0, code, type);
9971               return
9972                 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9973                                                               code, type));
9974             }
9975         }
9976
9977       return NULL_TREE;
9978
9979     case MINUS_EXPR:
9980       /* Pointer simplifications for subtraction, simple reassociations. */
9981       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
9982         {
9983           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
9984           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
9985               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9986             {
9987               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9988               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
9989               tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
9990               tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
9991               return fold_build2_loc (loc, PLUS_EXPR, type,
9992                                   fold_build2_loc (loc, MINUS_EXPR, type,
9993                                                arg00, arg10),
9994                                   fold_build2_loc (loc, MINUS_EXPR, type,
9995                                                arg01, arg11));
9996             }
9997           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
9998           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9999             {
10000               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10001               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10002               tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10003                                       fold_convert_loc (loc, type, arg1));
10004               if (tmp)
10005                 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10006             }
10007         }
10008       /* A - (-B) -> A + B */
10009       if (TREE_CODE (arg1) == NEGATE_EXPR)
10010         return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10011                             fold_convert_loc (loc, type,
10012                                               TREE_OPERAND (arg1, 0)));
10013       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10014       if (TREE_CODE (arg0) == NEGATE_EXPR
10015           && (FLOAT_TYPE_P (type)
10016               || INTEGRAL_TYPE_P (type))
10017           && negate_expr_p (arg1)
10018           && reorder_operands_p (arg0, arg1))
10019         return fold_build2_loc (loc, MINUS_EXPR, type,
10020                             fold_convert_loc (loc, type,
10021                                               negate_expr (arg1)),
10022                             fold_convert_loc (loc, type,
10023                                               TREE_OPERAND (arg0, 0)));
10024       /* Convert -A - 1 to ~A.  */
10025       if (INTEGRAL_TYPE_P (type)
10026           && TREE_CODE (arg0) == NEGATE_EXPR
10027           && integer_onep (arg1)
10028           && !TYPE_OVERFLOW_TRAPS (type))
10029         return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10030                             fold_convert_loc (loc, type,
10031                                               TREE_OPERAND (arg0, 0)));
10032
10033       /* Convert -1 - A to ~A.  */
10034       if (INTEGRAL_TYPE_P (type)
10035           && integer_all_onesp (arg0))
10036         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10037
10038
10039       /* X - (X / CST) * CST is X % CST.  */
10040       if (INTEGRAL_TYPE_P (type)
10041           && TREE_CODE (arg1) == MULT_EXPR
10042           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10043           && operand_equal_p (arg0,
10044                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10045           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10046                               TREE_OPERAND (arg1, 1), 0))
10047         return
10048           fold_convert_loc (loc, type,
10049                             fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10050                                          arg0, TREE_OPERAND (arg1, 1)));
10051
10052       if (! FLOAT_TYPE_P (type))
10053         {
10054           if (integer_zerop (arg0))
10055             return negate_expr (fold_convert_loc (loc, type, arg1));
10056           if (integer_zerop (arg1))
10057             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10058
10059           /* Fold A - (A & B) into ~B & A.  */
10060           if (!TREE_SIDE_EFFECTS (arg0)
10061               && TREE_CODE (arg1) == BIT_AND_EXPR)
10062             {
10063               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10064                 {
10065                   tree arg10 = fold_convert_loc (loc, type,
10066                                                  TREE_OPERAND (arg1, 0));
10067                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10068                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10069                                                    type, arg10),
10070                                       fold_convert_loc (loc, type, arg0));
10071                 }
10072               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10073                 {
10074                   tree arg11 = fold_convert_loc (loc,
10075                                                  type, TREE_OPERAND (arg1, 1));
10076                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10077                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10078                                                    type, arg11),
10079                                       fold_convert_loc (loc, type, arg0));
10080                 }
10081             }
10082
10083           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10084              any power of 2 minus 1.  */
10085           if (TREE_CODE (arg0) == BIT_AND_EXPR
10086               && TREE_CODE (arg1) == BIT_AND_EXPR
10087               && operand_equal_p (TREE_OPERAND (arg0, 0),
10088                                   TREE_OPERAND (arg1, 0), 0))
10089             {
10090               tree mask0 = TREE_OPERAND (arg0, 1);
10091               tree mask1 = TREE_OPERAND (arg1, 1);
10092               tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10093
10094               if (operand_equal_p (tem, mask1, 0))
10095                 {
10096                   tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10097                                      TREE_OPERAND (arg0, 0), mask1);
10098                   return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10099                 }
10100             }
10101         }
10102
10103       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10104       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10105         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10106
10107       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10108          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10109          (-ARG1 + ARG0) reduces to -ARG1.  */
10110       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10111         return negate_expr (fold_convert_loc (loc, type, arg1));
10112
10113       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10114          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10115          signed zeros are involved.  */
10116       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10117           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10118           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10119         {
10120           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10121           tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10122           tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10123           bool arg0rz = false, arg0iz = false;
10124           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10125               || (arg0i && (arg0iz = real_zerop (arg0i))))
10126             {
10127               tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10128               tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10129               if (arg0rz && arg1i && real_zerop (arg1i))
10130                 {
10131                   tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10132                                          arg1r ? arg1r
10133                                          : build1 (REALPART_EXPR, rtype, arg1));
10134                   tree ip = arg0i ? arg0i
10135                     : build1 (IMAGPART_EXPR, rtype, arg0);
10136                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10137                 }
10138               else if (arg0iz && arg1r && real_zerop (arg1r))
10139                 {
10140                   tree rp = arg0r ? arg0r
10141                     : build1 (REALPART_EXPR, rtype, arg0);
10142                   tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10143                                          arg1i ? arg1i
10144                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10145                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10146                 }
10147             }
10148         }
10149
10150       /* Fold &x - &x.  This can happen from &x.foo - &x.
10151          This is unsafe for certain floats even in non-IEEE formats.
10152          In IEEE, it is unsafe because it does wrong for NaNs.
10153          Also note that operand_equal_p is always false if an operand
10154          is volatile.  */
10155
10156       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10157           && operand_equal_p (arg0, arg1, 0))
10158         return fold_convert_loc (loc, type, integer_zero_node);
10159
10160       /* A - B -> A + (-B) if B is easily negatable.  */
10161       if (negate_expr_p (arg1)
10162           && ((FLOAT_TYPE_P (type)
10163                /* Avoid this transformation if B is a positive REAL_CST.  */
10164                && (TREE_CODE (arg1) != REAL_CST
10165                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10166               || INTEGRAL_TYPE_P (type)))
10167         return fold_build2_loc (loc, PLUS_EXPR, type,
10168                             fold_convert_loc (loc, type, arg0),
10169                             fold_convert_loc (loc, type,
10170                                               negate_expr (arg1)));
10171
10172       /* Try folding difference of addresses.  */
10173       {
10174         HOST_WIDE_INT diff;
10175
10176         if ((TREE_CODE (arg0) == ADDR_EXPR
10177              || TREE_CODE (arg1) == ADDR_EXPR)
10178             && ptr_difference_const (arg0, arg1, &diff))
10179           return build_int_cst_type (type, diff);
10180       }
10181
10182       /* Fold &a[i] - &a[j] to i-j.  */
10183       if (TREE_CODE (arg0) == ADDR_EXPR
10184           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10185           && TREE_CODE (arg1) == ADDR_EXPR
10186           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10187         {
10188           tree aref0 = TREE_OPERAND (arg0, 0);
10189           tree aref1 = TREE_OPERAND (arg1, 0);
10190           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10191                                TREE_OPERAND (aref1, 0), 0))
10192             {
10193               tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10194               tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10195               tree esz = array_ref_element_size (aref0);
10196               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10197               return fold_build2_loc (loc, MULT_EXPR, type, diff,
10198                                   fold_convert_loc (loc, type, esz));
10199
10200             }
10201         }
10202
10203       if (FLOAT_TYPE_P (type)
10204           && flag_unsafe_math_optimizations
10205           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10206           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10207           && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10208         return tem;
10209
10210       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10211          same or one.  Make sure type is not saturating.
10212          fold_plusminus_mult_expr will re-associate.  */
10213       if ((TREE_CODE (arg0) == MULT_EXPR
10214            || TREE_CODE (arg1) == MULT_EXPR)
10215           && !TYPE_SATURATING (type)
10216           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10217         {
10218           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10219           if (tem)
10220             return tem;
10221         }
10222
10223       goto associate;
10224
10225     case MULT_EXPR:
10226       /* (-A) * (-B) -> A * B  */
10227       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10228         return fold_build2_loc (loc, MULT_EXPR, type,
10229                             fold_convert_loc (loc, type,
10230                                               TREE_OPERAND (arg0, 0)),
10231                             fold_convert_loc (loc, type,
10232                                               negate_expr (arg1)));
10233       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10234         return fold_build2_loc (loc, MULT_EXPR, type,
10235                             fold_convert_loc (loc, type,
10236                                               negate_expr (arg0)),
10237                             fold_convert_loc (loc, type,
10238                                               TREE_OPERAND (arg1, 0)));
10239
10240       if (! FLOAT_TYPE_P (type))
10241         {
10242           if (integer_zerop (arg1))
10243             return omit_one_operand_loc (loc, type, arg1, arg0);
10244           if (integer_onep (arg1))
10245             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10246           /* Transform x * -1 into -x.  Make sure to do the negation
10247              on the original operand with conversions not stripped
10248              because we can only strip non-sign-changing conversions.  */
10249           if (integer_all_onesp (arg1))
10250             return fold_convert_loc (loc, type, negate_expr (op0));
10251           /* Transform x * -C into -x * C if x is easily negatable.  */
10252           if (TREE_CODE (arg1) == INTEGER_CST
10253               && tree_int_cst_sgn (arg1) == -1
10254               && negate_expr_p (arg0)
10255               && (tem = negate_expr (arg1)) != arg1
10256               && !TREE_OVERFLOW (tem))
10257             return fold_build2_loc (loc, MULT_EXPR, type,
10258                                 fold_convert_loc (loc, type,
10259                                                   negate_expr (arg0)),
10260                                 tem);
10261
10262           /* (a * (1 << b)) is (a << b)  */
10263           if (TREE_CODE (arg1) == LSHIFT_EXPR
10264               && integer_onep (TREE_OPERAND (arg1, 0)))
10265             return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10266                                 TREE_OPERAND (arg1, 1));
10267           if (TREE_CODE (arg0) == LSHIFT_EXPR
10268               && integer_onep (TREE_OPERAND (arg0, 0)))
10269             return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10270                                 TREE_OPERAND (arg0, 1));
10271
10272           /* (A + A) * C -> A * 2 * C  */
10273           if (TREE_CODE (arg0) == PLUS_EXPR
10274               && TREE_CODE (arg1) == INTEGER_CST
10275               && operand_equal_p (TREE_OPERAND (arg0, 0),
10276                                   TREE_OPERAND (arg0, 1), 0))
10277             return fold_build2_loc (loc, MULT_EXPR, type,
10278                                 omit_one_operand_loc (loc, type,
10279                                                   TREE_OPERAND (arg0, 0),
10280                                                   TREE_OPERAND (arg0, 1)),
10281                                 fold_build2_loc (loc, MULT_EXPR, type,
10282                                              build_int_cst (type, 2) , arg1));
10283
10284           strict_overflow_p = false;
10285           if (TREE_CODE (arg1) == INTEGER_CST
10286               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10287                                              &strict_overflow_p)))
10288             {
10289               if (strict_overflow_p)
10290                 fold_overflow_warning (("assuming signed overflow does not "
10291                                         "occur when simplifying "
10292                                         "multiplication"),
10293                                        WARN_STRICT_OVERFLOW_MISC);
10294               return fold_convert_loc (loc, type, tem);
10295             }
10296
10297           /* Optimize z * conj(z) for integer complex numbers.  */
10298           if (TREE_CODE (arg0) == CONJ_EXPR
10299               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10300             return fold_mult_zconjz (loc, type, arg1);
10301           if (TREE_CODE (arg1) == CONJ_EXPR
10302               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10303             return fold_mult_zconjz (loc, type, arg0);
10304         }
10305       else
10306         {
10307           /* Maybe fold x * 0 to 0.  The expressions aren't the same
10308              when x is NaN, since x * 0 is also NaN.  Nor are they the
10309              same in modes with signed zeros, since multiplying a
10310              negative value by 0 gives -0, not +0.  */
10311           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10312               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10313               && real_zerop (arg1))
10314             return omit_one_operand_loc (loc, type, arg1, arg0);
10315           /* In IEEE floating point, x*1 is not equivalent to x for snans.
10316              Likewise for complex arithmetic with signed zeros.  */
10317           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10318               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10319                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10320               && real_onep (arg1))
10321             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10322
10323           /* Transform x * -1.0 into -x.  */
10324           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10325               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10326                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10327               && real_minus_onep (arg1))
10328             return fold_convert_loc (loc, type, negate_expr (arg0));
10329
10330           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
10331              the result for floating point types due to rounding so it is applied
10332              only if -fassociative-math was specify.  */
10333           if (flag_associative_math
10334               && TREE_CODE (arg0) == RDIV_EXPR
10335               && TREE_CODE (arg1) == REAL_CST
10336               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10337             {
10338               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10339                                       arg1, 0);
10340               if (tem)
10341                 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10342                                     TREE_OPERAND (arg0, 1));
10343             }
10344
10345           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
10346           if (operand_equal_p (arg0, arg1, 0))
10347             {
10348               tree tem = fold_strip_sign_ops (arg0);
10349               if (tem != NULL_TREE)
10350                 {
10351                   tem = fold_convert_loc (loc, type, tem);
10352                   return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
10353                 }
10354             }
10355
10356           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10357              This is not the same for NaNs or if signed zeros are
10358              involved.  */
10359           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10360               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10361               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10362               && TREE_CODE (arg1) == COMPLEX_CST
10363               && real_zerop (TREE_REALPART (arg1)))
10364             {
10365               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10366               if (real_onep (TREE_IMAGPART (arg1)))
10367                 return
10368                   fold_build2_loc (loc, COMPLEX_EXPR, type,
10369                                negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10370                                                              rtype, arg0)),
10371                                fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10372               else if (real_minus_onep (TREE_IMAGPART (arg1)))
10373                 return
10374                   fold_build2_loc (loc, COMPLEX_EXPR, type,
10375                                fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10376                                negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10377                                                              rtype, arg0)));
10378             }
10379
10380           /* Optimize z * conj(z) for floating point complex numbers.
10381              Guarded by flag_unsafe_math_optimizations as non-finite
10382              imaginary components don't produce scalar results.  */
10383           if (flag_unsafe_math_optimizations
10384               && TREE_CODE (arg0) == CONJ_EXPR
10385               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10386             return fold_mult_zconjz (loc, type, arg1);
10387           if (flag_unsafe_math_optimizations
10388               && TREE_CODE (arg1) == CONJ_EXPR
10389               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10390             return fold_mult_zconjz (loc, type, arg0);
10391
10392           if (flag_unsafe_math_optimizations)
10393             {
10394               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10395               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10396
10397               /* Optimizations of root(...)*root(...).  */
10398               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10399                 {
10400                   tree rootfn, arg;
10401                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10402                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10403
10404                   /* Optimize sqrt(x)*sqrt(x) as x.  */
10405                   if (BUILTIN_SQRT_P (fcode0)
10406                       && operand_equal_p (arg00, arg10, 0)
10407                       && ! HONOR_SNANS (TYPE_MODE (type)))
10408                     return arg00;
10409
10410                   /* Optimize root(x)*root(y) as root(x*y).  */
10411                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10412                   arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
10413                   return build_call_expr_loc (loc, rootfn, 1, arg);
10414                 }
10415
10416               /* Optimize expN(x)*expN(y) as expN(x+y).  */
10417               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10418                 {
10419                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10420                   tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10421                                           CALL_EXPR_ARG (arg0, 0),
10422                                           CALL_EXPR_ARG (arg1, 0));
10423                   return build_call_expr_loc (loc, expfn, 1, arg);
10424                 }
10425
10426               /* Optimizations of pow(...)*pow(...).  */
10427               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10428                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10429                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10430                 {
10431                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10432                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10433                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10434                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10435
10436                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
10437                   if (operand_equal_p (arg01, arg11, 0))
10438                     {
10439                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10440                       tree arg = fold_build2_loc (loc, MULT_EXPR, type,
10441                                               arg00, arg10);
10442                       return build_call_expr_loc (loc, powfn, 2, arg, arg01);
10443                     }
10444
10445                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
10446                   if (operand_equal_p (arg00, arg10, 0))
10447                     {
10448                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10449                       tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10450                                               arg01, arg11);
10451                       return build_call_expr_loc (loc, powfn, 2, arg00, arg);
10452                     }
10453                 }
10454
10455               /* Optimize tan(x)*cos(x) as sin(x).  */
10456               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10457                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10458                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10459                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10460                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10461                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10462                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10463                                       CALL_EXPR_ARG (arg1, 0), 0))
10464                 {
10465                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10466
10467                   if (sinfn != NULL_TREE)
10468                     return build_call_expr_loc (loc, sinfn, 1,
10469                                             CALL_EXPR_ARG (arg0, 0));
10470                 }
10471
10472               /* Optimize x*pow(x,c) as pow(x,c+1).  */
10473               if (fcode1 == BUILT_IN_POW
10474                   || fcode1 == BUILT_IN_POWF
10475                   || fcode1 == BUILT_IN_POWL)
10476                 {
10477                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10478                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10479                   if (TREE_CODE (arg11) == REAL_CST
10480                       && !TREE_OVERFLOW (arg11)
10481                       && operand_equal_p (arg0, arg10, 0))
10482                     {
10483                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10484                       REAL_VALUE_TYPE c;
10485                       tree arg;
10486
10487                       c = TREE_REAL_CST (arg11);
10488                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10489                       arg = build_real (type, c);
10490                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10491                     }
10492                 }
10493
10494               /* Optimize pow(x,c)*x as pow(x,c+1).  */
10495               if (fcode0 == BUILT_IN_POW
10496                   || fcode0 == BUILT_IN_POWF
10497                   || fcode0 == BUILT_IN_POWL)
10498                 {
10499                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10500                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10501                   if (TREE_CODE (arg01) == REAL_CST
10502                       && !TREE_OVERFLOW (arg01)
10503                       && operand_equal_p (arg1, arg00, 0))
10504                     {
10505                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10506                       REAL_VALUE_TYPE c;
10507                       tree arg;
10508
10509                       c = TREE_REAL_CST (arg01);
10510                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10511                       arg = build_real (type, c);
10512                       return build_call_expr_loc (loc, powfn, 2, arg1, arg);
10513                     }
10514                 }
10515
10516               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
10517               if (optimize_function_for_speed_p (cfun)
10518                   && operand_equal_p (arg0, arg1, 0))
10519                 {
10520                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10521
10522                   if (powfn)
10523                     {
10524                       tree arg = build_real (type, dconst2);
10525                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10526                     }
10527                 }
10528             }
10529         }
10530       goto associate;
10531
10532     case BIT_IOR_EXPR:
10533     bit_ior:
10534       if (integer_all_onesp (arg1))
10535         return omit_one_operand_loc (loc, type, arg1, arg0);
10536       if (integer_zerop (arg1))
10537         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10538       if (operand_equal_p (arg0, arg1, 0))
10539         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10540
10541       /* ~X | X is -1.  */
10542       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10543           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10544         {
10545           t1 = fold_convert_loc (loc, type, integer_zero_node);
10546           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10547           return omit_one_operand_loc (loc, type, t1, arg1);
10548         }
10549
10550       /* X | ~X is -1.  */
10551       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10552           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10553         {
10554           t1 = fold_convert_loc (loc, type, integer_zero_node);
10555           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10556           return omit_one_operand_loc (loc, type, t1, arg0);
10557         }
10558
10559       /* Canonicalize (X & C1) | C2.  */
10560       if (TREE_CODE (arg0) == BIT_AND_EXPR
10561           && TREE_CODE (arg1) == INTEGER_CST
10562           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10563         {
10564           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10565           int width = TYPE_PRECISION (type), w;
10566           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10567           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10568           hi2 = TREE_INT_CST_HIGH (arg1);
10569           lo2 = TREE_INT_CST_LOW (arg1);
10570
10571           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
10572           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10573             return omit_one_operand_loc (loc, type, arg1,
10574                                      TREE_OPERAND (arg0, 0));
10575
10576           if (width > HOST_BITS_PER_WIDE_INT)
10577             {
10578               mhi = (unsigned HOST_WIDE_INT) -1
10579                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
10580               mlo = -1;
10581             }
10582           else
10583             {
10584               mhi = 0;
10585               mlo = (unsigned HOST_WIDE_INT) -1
10586                     >> (HOST_BITS_PER_WIDE_INT - width);
10587             }
10588
10589           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
10590           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10591             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10592                                 TREE_OPERAND (arg0, 0), arg1);
10593
10594           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10595              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10596              mode which allows further optimizations.  */
10597           hi1 &= mhi;
10598           lo1 &= mlo;
10599           hi2 &= mhi;
10600           lo2 &= mlo;
10601           hi3 = hi1 & ~hi2;
10602           lo3 = lo1 & ~lo2;
10603           for (w = BITS_PER_UNIT;
10604                w <= width && w <= HOST_BITS_PER_WIDE_INT;
10605                w <<= 1)
10606             {
10607               unsigned HOST_WIDE_INT mask
10608                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10609               if (((lo1 | lo2) & mask) == mask
10610                   && (lo1 & ~mask) == 0 && hi1 == 0)
10611                 {
10612                   hi3 = 0;
10613                   lo3 = mask;
10614                   break;
10615                 }
10616             }
10617           if (hi3 != hi1 || lo3 != lo1)
10618             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10619                                 fold_build2_loc (loc, BIT_AND_EXPR, type,
10620                                              TREE_OPERAND (arg0, 0),
10621                                              build_int_cst_wide (type,
10622                                                                  lo3, hi3)),
10623                                 arg1);
10624         }
10625
10626       /* (X & Y) | Y is (X, Y).  */
10627       if (TREE_CODE (arg0) == BIT_AND_EXPR
10628           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10629         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10630       /* (X & Y) | X is (Y, X).  */
10631       if (TREE_CODE (arg0) == BIT_AND_EXPR
10632           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10633           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10634         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
10635       /* X | (X & Y) is (Y, X).  */
10636       if (TREE_CODE (arg1) == BIT_AND_EXPR
10637           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10638           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10639         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
10640       /* X | (Y & X) is (Y, X).  */
10641       if (TREE_CODE (arg1) == BIT_AND_EXPR
10642           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10643           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10644         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
10645
10646       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
10647       if (t1 != NULL_TREE)
10648         return t1;
10649
10650       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10651
10652          This results in more efficient code for machines without a NAND
10653          instruction.  Combine will canonicalize to the first form
10654          which will allow use of NAND instructions provided by the
10655          backend if they exist.  */
10656       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10657           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10658         {
10659           return
10660             fold_build1_loc (loc, BIT_NOT_EXPR, type,
10661                          build2 (BIT_AND_EXPR, type,
10662                                  fold_convert_loc (loc, type,
10663                                                    TREE_OPERAND (arg0, 0)),
10664                                  fold_convert_loc (loc, type,
10665                                                    TREE_OPERAND (arg1, 0))));
10666         }
10667
10668       /* See if this can be simplified into a rotate first.  If that
10669          is unsuccessful continue in the association code.  */
10670       goto bit_rotate;
10671
10672     case BIT_XOR_EXPR:
10673       if (integer_zerop (arg1))
10674         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10675       if (integer_all_onesp (arg1))
10676         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
10677       if (operand_equal_p (arg0, arg1, 0))
10678         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10679
10680       /* ~X ^ X is -1.  */
10681       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10682           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10683         {
10684           t1 = fold_convert_loc (loc, type, integer_zero_node);
10685           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10686           return omit_one_operand_loc (loc, type, t1, arg1);
10687         }
10688
10689       /* X ^ ~X is -1.  */
10690       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10691           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10692         {
10693           t1 = fold_convert_loc (loc, type, integer_zero_node);
10694           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10695           return omit_one_operand_loc (loc, type, t1, arg0);
10696         }
10697
10698       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10699          with a constant, and the two constants have no bits in common,
10700          we should treat this as a BIT_IOR_EXPR since this may produce more
10701          simplifications.  */
10702       if (TREE_CODE (arg0) == BIT_AND_EXPR
10703           && TREE_CODE (arg1) == BIT_AND_EXPR
10704           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10705           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10706           && integer_zerop (const_binop (BIT_AND_EXPR,
10707                                          TREE_OPERAND (arg0, 1),
10708                                          TREE_OPERAND (arg1, 1), 0)))
10709         {
10710           code = BIT_IOR_EXPR;
10711           goto bit_ior;
10712         }
10713
10714       /* (X | Y) ^ X -> Y & ~ X*/
10715       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10716           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10717         {
10718           tree t2 = TREE_OPERAND (arg0, 1);
10719           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10720                             arg1);
10721           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10722                             fold_convert_loc (loc, type, t2),
10723                             fold_convert_loc (loc, type, t1));
10724           return t1;
10725         }
10726
10727       /* (Y | X) ^ X -> Y & ~ X*/
10728       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10729           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10730         {
10731           tree t2 = TREE_OPERAND (arg0, 0);
10732           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10733                             arg1);
10734           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10735                             fold_convert_loc (loc, type, t2),
10736                             fold_convert_loc (loc, type, t1));
10737           return t1;
10738         }
10739
10740       /* X ^ (X | Y) -> Y & ~ X*/
10741       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10742           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10743         {
10744           tree t2 = TREE_OPERAND (arg1, 1);
10745           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10746                             arg0);
10747           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10748                             fold_convert_loc (loc, type, t2),
10749                             fold_convert_loc (loc, type, t1));
10750           return t1;
10751         }
10752
10753       /* X ^ (Y | X) -> Y & ~ X*/
10754       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10755           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10756         {
10757           tree t2 = TREE_OPERAND (arg1, 0);
10758           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10759                             arg0);
10760           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10761                             fold_convert_loc (loc, type, t2),
10762                             fold_convert_loc (loc, type, t1));
10763           return t1;
10764         }
10765
10766       /* Convert ~X ^ ~Y to X ^ Y.  */
10767       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10768           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10769         return fold_build2_loc (loc, code, type,
10770                             fold_convert_loc (loc, type,
10771                                               TREE_OPERAND (arg0, 0)),
10772                             fold_convert_loc (loc, type,
10773                                               TREE_OPERAND (arg1, 0)));
10774
10775       /* Convert ~X ^ C to X ^ ~C.  */
10776       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10777           && TREE_CODE (arg1) == INTEGER_CST)
10778         return fold_build2_loc (loc, code, type,
10779                             fold_convert_loc (loc, type,
10780                                               TREE_OPERAND (arg0, 0)),
10781                             fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
10782
10783       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
10784       if (TREE_CODE (arg0) == BIT_AND_EXPR
10785           && integer_onep (TREE_OPERAND (arg0, 1))
10786           && integer_onep (arg1))
10787         return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10788                             build_int_cst (TREE_TYPE (arg0), 0));
10789
10790       /* Fold (X & Y) ^ Y as ~X & Y.  */
10791       if (TREE_CODE (arg0) == BIT_AND_EXPR
10792           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10793         {
10794           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10795           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10796                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10797                               fold_convert_loc (loc, type, arg1));
10798         }
10799       /* Fold (X & Y) ^ X as ~Y & X.  */
10800       if (TREE_CODE (arg0) == BIT_AND_EXPR
10801           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10802           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10803         {
10804           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10805           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10806                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10807                               fold_convert_loc (loc, type, arg1));
10808         }
10809       /* Fold X ^ (X & Y) as X & ~Y.  */
10810       if (TREE_CODE (arg1) == BIT_AND_EXPR
10811           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10812         {
10813           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10814           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10815                               fold_convert_loc (loc, type, arg0),
10816                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10817         }
10818       /* Fold X ^ (Y & X) as ~Y & X.  */
10819       if (TREE_CODE (arg1) == BIT_AND_EXPR
10820           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10821           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10822         {
10823           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10824           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10825                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10826                               fold_convert_loc (loc, type, arg0));
10827         }
10828
10829       /* See if this can be simplified into a rotate first.  If that
10830          is unsuccessful continue in the association code.  */
10831       goto bit_rotate;
10832
10833     case BIT_AND_EXPR:
10834       if (integer_all_onesp (arg1))
10835         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10836       if (integer_zerop (arg1))
10837         return omit_one_operand_loc (loc, type, arg1, arg0);
10838       if (operand_equal_p (arg0, arg1, 0))
10839         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10840
10841       /* ~X & X is always zero.  */
10842       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10843           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10844         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10845
10846       /* X & ~X is always zero.  */
10847       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10848           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10849         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10850
10851       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
10852       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10853           && TREE_CODE (arg1) == INTEGER_CST
10854           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10855         {
10856           tree tmp1 = fold_convert_loc (loc, type, arg1);
10857           tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10858           tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10859           tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
10860           tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
10861           return
10862             fold_convert_loc (loc, type,
10863                               fold_build2_loc (loc, BIT_IOR_EXPR,
10864                                            type, tmp2, tmp3));
10865         }
10866
10867       /* (X | Y) & Y is (X, Y).  */
10868       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10869           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10870         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10871       /* (X | Y) & X is (Y, X).  */
10872       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10873           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10874           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10875         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
10876       /* X & (X | Y) is (Y, X).  */
10877       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10878           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10879           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10880         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
10881       /* X & (Y | X) is (Y, X).  */
10882       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10883           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10884           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10885         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
10886
10887       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
10888       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10889           && integer_onep (TREE_OPERAND (arg0, 1))
10890           && integer_onep (arg1))
10891         {
10892           tem = TREE_OPERAND (arg0, 0);
10893           return fold_build2_loc (loc, EQ_EXPR, type,
10894                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
10895                                            build_int_cst (TREE_TYPE (tem), 1)),
10896                               build_int_cst (TREE_TYPE (tem), 0));
10897         }
10898       /* Fold ~X & 1 as (X & 1) == 0.  */
10899       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10900           && integer_onep (arg1))
10901         {
10902           tem = TREE_OPERAND (arg0, 0);
10903           return fold_build2_loc (loc, EQ_EXPR, type,
10904                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
10905                                            build_int_cst (TREE_TYPE (tem), 1)),
10906                               build_int_cst (TREE_TYPE (tem), 0));
10907         }
10908
10909       /* Fold (X ^ Y) & Y as ~X & Y.  */
10910       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10911           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10912         {
10913           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10914           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10915                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10916                               fold_convert_loc (loc, type, arg1));
10917         }
10918       /* Fold (X ^ Y) & X as ~Y & X.  */
10919       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10920           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10921           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10922         {
10923           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10924           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10925                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10926                               fold_convert_loc (loc, type, arg1));
10927         }
10928       /* Fold X & (X ^ Y) as X & ~Y.  */
10929       if (TREE_CODE (arg1) == BIT_XOR_EXPR
10930           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10931         {
10932           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10933           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10934                               fold_convert_loc (loc, type, arg0),
10935                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10936         }
10937       /* Fold X & (Y ^ X) as ~Y & X.  */
10938       if (TREE_CODE (arg1) == BIT_XOR_EXPR
10939           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10940           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10941         {
10942           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10943           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10944                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10945                               fold_convert_loc (loc, type, arg0));
10946         }
10947
10948       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
10949       if (t1 != NULL_TREE)
10950         return t1;
10951       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
10952       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10953           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10954         {
10955           unsigned int prec
10956             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10957
10958           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
10959               && (~TREE_INT_CST_LOW (arg1)
10960                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
10961             return
10962               fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10963         }
10964
10965       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
10966
10967          This results in more efficient code for machines without a NOR
10968          instruction.  Combine will canonicalize to the first form
10969          which will allow use of NOR instructions provided by the
10970          backend if they exist.  */
10971       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10972           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10973         {
10974           return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10975                               build2 (BIT_IOR_EXPR, type,
10976                                       fold_convert_loc (loc, type,
10977                                                         TREE_OPERAND (arg0, 0)),
10978                                       fold_convert_loc (loc, type,
10979                                                         TREE_OPERAND (arg1, 0))));
10980         }
10981
10982       /* If arg0 is derived from the address of an object or function, we may
10983          be able to fold this expression using the object or function's
10984          alignment.  */
10985       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
10986         {
10987           unsigned HOST_WIDE_INT modulus, residue;
10988           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
10989
10990           modulus = get_pointer_modulus_and_residue (arg0, &residue,
10991                                                      integer_onep (arg1));
10992
10993           /* This works because modulus is a power of 2.  If this weren't the
10994              case, we'd have to replace it by its greatest power-of-2
10995              divisor: modulus & -modulus.  */
10996           if (low < modulus)
10997             return build_int_cst (type, residue & low);
10998         }
10999
11000       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11001               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11002          if the new mask might be further optimized.  */
11003       if ((TREE_CODE (arg0) == LSHIFT_EXPR
11004            || TREE_CODE (arg0) == RSHIFT_EXPR)
11005           && host_integerp (TREE_OPERAND (arg0, 1), 1)
11006           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11007           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11008              < TYPE_PRECISION (TREE_TYPE (arg0))
11009           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11010           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11011         {
11012           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11013           unsigned HOST_WIDE_INT mask
11014             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11015           unsigned HOST_WIDE_INT newmask, zerobits = 0;
11016           tree shift_type = TREE_TYPE (arg0);
11017
11018           if (TREE_CODE (arg0) == LSHIFT_EXPR)
11019             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11020           else if (TREE_CODE (arg0) == RSHIFT_EXPR
11021                    && TYPE_PRECISION (TREE_TYPE (arg0))
11022                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11023             {
11024               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11025               tree arg00 = TREE_OPERAND (arg0, 0);
11026               /* See if more bits can be proven as zero because of
11027                  zero extension.  */
11028               if (TREE_CODE (arg00) == NOP_EXPR
11029                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11030                 {
11031                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11032                   if (TYPE_PRECISION (inner_type)
11033                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11034                       && TYPE_PRECISION (inner_type) < prec)
11035                     {
11036                       prec = TYPE_PRECISION (inner_type);
11037                       /* See if we can shorten the right shift.  */
11038                       if (shiftc < prec)
11039                         shift_type = inner_type;
11040                     }
11041                 }
11042               zerobits = ~(unsigned HOST_WIDE_INT) 0;
11043               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11044               zerobits <<= prec - shiftc;
11045               /* For arithmetic shift if sign bit could be set, zerobits
11046                  can contain actually sign bits, so no transformation is
11047                  possible, unless MASK masks them all away.  In that
11048                  case the shift needs to be converted into logical shift.  */
11049               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11050                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11051                 {
11052                   if ((mask & zerobits) == 0)
11053                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
11054                   else
11055                     zerobits = 0;
11056                 }
11057             }
11058
11059           /* ((X << 16) & 0xff00) is (X, 0).  */
11060           if ((mask & zerobits) == mask)
11061             return omit_one_operand_loc (loc, type,
11062                                      build_int_cst (type, 0), arg0);
11063
11064           newmask = mask | zerobits;
11065           if (newmask != mask && (newmask & (newmask + 1)) == 0)
11066             {
11067               unsigned int prec;
11068
11069               /* Only do the transformation if NEWMASK is some integer
11070                  mode's mask.  */
11071               for (prec = BITS_PER_UNIT;
11072                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11073                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11074                   break;
11075               if (prec < HOST_BITS_PER_WIDE_INT
11076                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
11077                 {
11078                   tree newmaskt;
11079
11080                   if (shift_type != TREE_TYPE (arg0))
11081                     {
11082                       tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11083                                          fold_convert_loc (loc, shift_type,
11084                                                            TREE_OPERAND (arg0, 0)),
11085                                          TREE_OPERAND (arg0, 1));
11086                       tem = fold_convert_loc (loc, type, tem);
11087                     }
11088                   else
11089                     tem = op0;
11090                   newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11091                   if (!tree_int_cst_equal (newmaskt, arg1))
11092                     return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11093                 }
11094             }
11095         }
11096
11097       goto associate;
11098
11099     case RDIV_EXPR:
11100       /* Don't touch a floating-point divide by zero unless the mode
11101          of the constant can represent infinity.  */
11102       if (TREE_CODE (arg1) == REAL_CST
11103           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11104           && real_zerop (arg1))
11105         return NULL_TREE;
11106
11107       /* Optimize A / A to 1.0 if we don't care about
11108          NaNs or Infinities.  Skip the transformation
11109          for non-real operands.  */
11110       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11111           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11112           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11113           && operand_equal_p (arg0, arg1, 0))
11114         {
11115           tree r = build_real (TREE_TYPE (arg0), dconst1);
11116
11117           return omit_two_operands_loc (loc, type, r, arg0, arg1);
11118         }
11119
11120       /* The complex version of the above A / A optimization.  */
11121       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11122           && operand_equal_p (arg0, arg1, 0))
11123         {
11124           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11125           if (! HONOR_NANS (TYPE_MODE (elem_type))
11126               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11127             {
11128               tree r = build_real (elem_type, dconst1);
11129               /* omit_two_operands will call fold_convert for us.  */
11130               return omit_two_operands_loc (loc, type, r, arg0, arg1);
11131             }
11132         }
11133
11134       /* (-A) / (-B) -> A / B  */
11135       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11136         return fold_build2_loc (loc, RDIV_EXPR, type,
11137                             TREE_OPERAND (arg0, 0),
11138                             negate_expr (arg1));
11139       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11140         return fold_build2_loc (loc, RDIV_EXPR, type,
11141                             negate_expr (arg0),
11142                             TREE_OPERAND (arg1, 0));
11143
11144       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
11145       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11146           && real_onep (arg1))
11147         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11148
11149       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
11150       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11151           && real_minus_onep (arg1))
11152         return non_lvalue_loc (loc, fold_convert_loc (loc, type,
11153                                                   negate_expr (arg0)));
11154
11155       /* If ARG1 is a constant, we can convert this to a multiply by the
11156          reciprocal.  This does not have the same rounding properties,
11157          so only do this if -freciprocal-math.  We can actually
11158          always safely do it if ARG1 is a power of two, but it's hard to
11159          tell if it is or not in a portable manner.  */
11160       if (TREE_CODE (arg1) == REAL_CST)
11161         {
11162           if (flag_reciprocal_math
11163               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11164                                           arg1, 0)))
11165             return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
11166           /* Find the reciprocal if optimizing and the result is exact.  */
11167           if (optimize)
11168             {
11169               REAL_VALUE_TYPE r;
11170               r = TREE_REAL_CST (arg1);
11171               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11172                 {
11173                   tem = build_real (type, r);
11174                   return fold_build2_loc (loc, MULT_EXPR, type,
11175                                       fold_convert_loc (loc, type, arg0), tem);
11176                 }
11177             }
11178         }
11179       /* Convert A/B/C to A/(B*C).  */
11180       if (flag_reciprocal_math
11181           && TREE_CODE (arg0) == RDIV_EXPR)
11182         return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11183                             fold_build2_loc (loc, MULT_EXPR, type,
11184                                          TREE_OPERAND (arg0, 1), arg1));
11185
11186       /* Convert A/(B/C) to (A/B)*C.  */
11187       if (flag_reciprocal_math
11188           && TREE_CODE (arg1) == RDIV_EXPR)
11189         return fold_build2_loc (loc, MULT_EXPR, type,
11190                             fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11191                                          TREE_OPERAND (arg1, 0)),
11192                             TREE_OPERAND (arg1, 1));
11193
11194       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11195       if (flag_reciprocal_math
11196           && TREE_CODE (arg1) == MULT_EXPR
11197           && TREE_CODE (arg0) == REAL_CST
11198           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11199         {
11200           tree tem = const_binop (RDIV_EXPR, arg0,
11201                                   TREE_OPERAND (arg1, 1), 0);
11202           if (tem)
11203             return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11204                                 TREE_OPERAND (arg1, 0));
11205         }
11206
11207       if (flag_unsafe_math_optimizations)
11208         {
11209           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11210           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11211
11212           /* Optimize sin(x)/cos(x) as tan(x).  */
11213           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11214                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11215                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11216               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11217                                   CALL_EXPR_ARG (arg1, 0), 0))
11218             {
11219               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11220
11221               if (tanfn != NULL_TREE)
11222                 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11223             }
11224
11225           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11226           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11227                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11228                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11229               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11230                                   CALL_EXPR_ARG (arg1, 0), 0))
11231             {
11232               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11233
11234               if (tanfn != NULL_TREE)
11235                 {
11236                   tree tmp = build_call_expr_loc (loc, tanfn, 1,
11237                                               CALL_EXPR_ARG (arg0, 0));
11238                   return fold_build2_loc (loc, RDIV_EXPR, type,
11239                                       build_real (type, dconst1), tmp);
11240                 }
11241             }
11242
11243           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11244              NaNs or Infinities.  */
11245           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11246                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11247                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11248             {
11249               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11250               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11251
11252               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11253                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11254                   && operand_equal_p (arg00, arg01, 0))
11255                 {
11256                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11257
11258                   if (cosfn != NULL_TREE)
11259                     return build_call_expr_loc (loc, cosfn, 1, arg00);
11260                 }
11261             }
11262
11263           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11264              NaNs or Infinities.  */
11265           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11266                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11267                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11268             {
11269               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11270               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11271
11272               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11273                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11274                   && operand_equal_p (arg00, arg01, 0))
11275                 {
11276                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11277
11278                   if (cosfn != NULL_TREE)
11279                     {
11280                       tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11281                       return fold_build2_loc (loc, RDIV_EXPR, type,
11282                                           build_real (type, dconst1),
11283                                           tmp);
11284                     }
11285                 }
11286             }
11287
11288           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11289           if (fcode0 == BUILT_IN_POW
11290               || fcode0 == BUILT_IN_POWF
11291               || fcode0 == BUILT_IN_POWL)
11292             {
11293               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11294               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11295               if (TREE_CODE (arg01) == REAL_CST
11296                   && !TREE_OVERFLOW (arg01)
11297                   && operand_equal_p (arg1, arg00, 0))
11298                 {
11299                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11300                   REAL_VALUE_TYPE c;
11301                   tree arg;
11302
11303                   c = TREE_REAL_CST (arg01);
11304                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11305                   arg = build_real (type, c);
11306                   return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11307                 }
11308             }
11309
11310           /* Optimize a/root(b/c) into a*root(c/b).  */
11311           if (BUILTIN_ROOT_P (fcode1))
11312             {
11313               tree rootarg = CALL_EXPR_ARG (arg1, 0);
11314
11315               if (TREE_CODE (rootarg) == RDIV_EXPR)
11316                 {
11317                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11318                   tree b = TREE_OPERAND (rootarg, 0);
11319                   tree c = TREE_OPERAND (rootarg, 1);
11320
11321                   tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
11322
11323                   tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
11324                   return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
11325                 }
11326             }
11327
11328           /* Optimize x/expN(y) into x*expN(-y).  */
11329           if (BUILTIN_EXPONENT_P (fcode1))
11330             {
11331               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11332               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11333               arg1 = build_call_expr_loc (loc,
11334                                       expfn, 1,
11335                                       fold_convert_loc (loc, type, arg));
11336               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11337             }
11338
11339           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
11340           if (fcode1 == BUILT_IN_POW
11341               || fcode1 == BUILT_IN_POWF
11342               || fcode1 == BUILT_IN_POWL)
11343             {
11344               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11345               tree arg10 = CALL_EXPR_ARG (arg1, 0);
11346               tree arg11 = CALL_EXPR_ARG (arg1, 1);
11347               tree neg11 = fold_convert_loc (loc, type,
11348                                              negate_expr (arg11));
11349               arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
11350               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11351             }
11352         }
11353       return NULL_TREE;
11354
11355     case TRUNC_DIV_EXPR:
11356     case FLOOR_DIV_EXPR:
11357       /* Simplify A / (B << N) where A and B are positive and B is
11358          a power of 2, to A >> (N + log2(B)).  */
11359       strict_overflow_p = false;
11360       if (TREE_CODE (arg1) == LSHIFT_EXPR
11361           && (TYPE_UNSIGNED (type)
11362               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11363         {
11364           tree sval = TREE_OPERAND (arg1, 0);
11365           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11366             {
11367               tree sh_cnt = TREE_OPERAND (arg1, 1);
11368               unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11369
11370               if (strict_overflow_p)
11371                 fold_overflow_warning (("assuming signed overflow does not "
11372                                         "occur when simplifying A / (B << N)"),
11373                                        WARN_STRICT_OVERFLOW_MISC);
11374
11375               sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11376                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
11377               return fold_build2_loc (loc, RSHIFT_EXPR, type,
11378                                   fold_convert_loc (loc, type, arg0), sh_cnt);
11379             }
11380         }
11381
11382       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11383          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
11384       if (INTEGRAL_TYPE_P (type)
11385           && TYPE_UNSIGNED (type)
11386           && code == FLOOR_DIV_EXPR)
11387         return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
11388
11389       /* Fall thru */
11390
11391     case ROUND_DIV_EXPR:
11392     case CEIL_DIV_EXPR:
11393     case EXACT_DIV_EXPR:
11394       if (integer_onep (arg1))
11395         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11396       if (integer_zerop (arg1))
11397         return NULL_TREE;
11398       /* X / -1 is -X.  */
11399       if (!TYPE_UNSIGNED (type)
11400           && TREE_CODE (arg1) == INTEGER_CST
11401           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11402           && TREE_INT_CST_HIGH (arg1) == -1)
11403         return fold_convert_loc (loc, type, negate_expr (arg0));
11404
11405       /* Convert -A / -B to A / B when the type is signed and overflow is
11406          undefined.  */
11407       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11408           && TREE_CODE (arg0) == NEGATE_EXPR
11409           && negate_expr_p (arg1))
11410         {
11411           if (INTEGRAL_TYPE_P (type))
11412             fold_overflow_warning (("assuming signed overflow does not occur "
11413                                     "when distributing negation across "
11414                                     "division"),
11415                                    WARN_STRICT_OVERFLOW_MISC);
11416           return fold_build2_loc (loc, code, type,
11417                               fold_convert_loc (loc, type,
11418                                                 TREE_OPERAND (arg0, 0)),
11419                               fold_convert_loc (loc, type,
11420                                                 negate_expr (arg1)));
11421         }
11422       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11423           && TREE_CODE (arg1) == NEGATE_EXPR
11424           && negate_expr_p (arg0))
11425         {
11426           if (INTEGRAL_TYPE_P (type))
11427             fold_overflow_warning (("assuming signed overflow does not occur "
11428                                     "when distributing negation across "
11429                                     "division"),
11430                                    WARN_STRICT_OVERFLOW_MISC);
11431           return fold_build2_loc (loc, code, type,
11432                               fold_convert_loc (loc, type,
11433                                                 negate_expr (arg0)),
11434                               fold_convert_loc (loc, type,
11435                                                 TREE_OPERAND (arg1, 0)));
11436         }
11437
11438       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11439          operation, EXACT_DIV_EXPR.
11440
11441          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11442          At one time others generated faster code, it's not clear if they do
11443          after the last round to changes to the DIV code in expmed.c.  */
11444       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11445           && multiple_of_p (type, arg0, arg1))
11446         return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
11447
11448       strict_overflow_p = false;
11449       if (TREE_CODE (arg1) == INTEGER_CST
11450           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11451                                          &strict_overflow_p)))
11452         {
11453           if (strict_overflow_p)
11454             fold_overflow_warning (("assuming signed overflow does not occur "
11455                                     "when simplifying division"),
11456                                    WARN_STRICT_OVERFLOW_MISC);
11457           return fold_convert_loc (loc, type, tem);
11458         }
11459
11460       return NULL_TREE;
11461
11462     case CEIL_MOD_EXPR:
11463     case FLOOR_MOD_EXPR:
11464     case ROUND_MOD_EXPR:
11465     case TRUNC_MOD_EXPR:
11466       /* X % 1 is always zero, but be sure to preserve any side
11467          effects in X.  */
11468       if (integer_onep (arg1))
11469         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11470
11471       /* X % 0, return X % 0 unchanged so that we can get the
11472          proper warnings and errors.  */
11473       if (integer_zerop (arg1))
11474         return NULL_TREE;
11475
11476       /* 0 % X is always zero, but be sure to preserve any side
11477          effects in X.  Place this after checking for X == 0.  */
11478       if (integer_zerop (arg0))
11479         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11480
11481       /* X % -1 is zero.  */
11482       if (!TYPE_UNSIGNED (type)
11483           && TREE_CODE (arg1) == INTEGER_CST
11484           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11485           && TREE_INT_CST_HIGH (arg1) == -1)
11486         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11487
11488       /* X % -C is the same as X % C.  */
11489       if (code == TRUNC_MOD_EXPR
11490           && !TYPE_UNSIGNED (type)
11491           && TREE_CODE (arg1) == INTEGER_CST
11492           && !TREE_OVERFLOW (arg1)
11493           && TREE_INT_CST_HIGH (arg1) < 0
11494           && !TYPE_OVERFLOW_TRAPS (type)
11495           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
11496           && !sign_bit_p (arg1, arg1))
11497         return fold_build2_loc (loc, code, type,
11498                             fold_convert_loc (loc, type, arg0),
11499                             fold_convert_loc (loc, type,
11500                                               negate_expr (arg1)));
11501
11502       /* X % -Y is the same as X % Y.  */
11503       if (code == TRUNC_MOD_EXPR
11504           && !TYPE_UNSIGNED (type)
11505           && TREE_CODE (arg1) == NEGATE_EXPR
11506           && !TYPE_OVERFLOW_TRAPS (type))
11507         return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
11508                             fold_convert_loc (loc, type,
11509                                               TREE_OPERAND (arg1, 0)));
11510
11511       strict_overflow_p = false;
11512       if (TREE_CODE (arg1) == INTEGER_CST
11513           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11514                                          &strict_overflow_p)))
11515         {
11516           if (strict_overflow_p)
11517             fold_overflow_warning (("assuming signed overflow does not occur "
11518                                     "when simplifying modulus"),
11519                                    WARN_STRICT_OVERFLOW_MISC);
11520           return fold_convert_loc (loc, type, tem);
11521         }
11522
11523       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11524          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
11525       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11526           && (TYPE_UNSIGNED (type)
11527               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11528         {
11529           tree c = arg1;
11530           /* Also optimize A % (C << N)  where C is a power of 2,
11531              to A & ((C << N) - 1).  */
11532           if (TREE_CODE (arg1) == LSHIFT_EXPR)
11533             c = TREE_OPERAND (arg1, 0);
11534
11535           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11536             {
11537               tree mask
11538                 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
11539                                    build_int_cst (TREE_TYPE (arg1), 1));
11540               if (strict_overflow_p)
11541                 fold_overflow_warning (("assuming signed overflow does not "
11542                                         "occur when simplifying "
11543                                         "X % (power of two)"),
11544                                        WARN_STRICT_OVERFLOW_MISC);
11545               return fold_build2_loc (loc, BIT_AND_EXPR, type,
11546                                       fold_convert_loc (loc, type, arg0),
11547                                       fold_convert_loc (loc, type, mask));
11548             }
11549         }
11550
11551       return NULL_TREE;
11552
11553     case LROTATE_EXPR:
11554     case RROTATE_EXPR:
11555       if (integer_all_onesp (arg0))
11556         return omit_one_operand_loc (loc, type, arg0, arg1);
11557       goto shift;
11558
11559     case RSHIFT_EXPR:
11560       /* Optimize -1 >> x for arithmetic right shifts.  */
11561       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11562           && tree_expr_nonnegative_p (arg1))
11563         return omit_one_operand_loc (loc, type, arg0, arg1);
11564       /* ... fall through ...  */
11565
11566     case LSHIFT_EXPR:
11567     shift:
11568       if (integer_zerop (arg1))
11569         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11570       if (integer_zerop (arg0))
11571         return omit_one_operand_loc (loc, type, arg0, arg1);
11572
11573       /* Since negative shift count is not well-defined,
11574          don't try to compute it in the compiler.  */
11575       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11576         return NULL_TREE;
11577
11578       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
11579       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11580           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11581           && host_integerp (TREE_OPERAND (arg0, 1), false)
11582           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11583         {
11584           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11585                                + TREE_INT_CST_LOW (arg1));
11586
11587           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11588              being well defined.  */
11589           if (low >= TYPE_PRECISION (type))
11590             {
11591               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11592                 low = low % TYPE_PRECISION (type);
11593               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11594                 return omit_one_operand_loc (loc, type, build_int_cst (type, 0),
11595                                          TREE_OPERAND (arg0, 0));
11596               else
11597                 low = TYPE_PRECISION (type) - 1;
11598             }
11599
11600           return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11601                               build_int_cst (type, low));
11602         }
11603
11604       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11605          into x & ((unsigned)-1 >> c) for unsigned types.  */
11606       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11607            || (TYPE_UNSIGNED (type)
11608                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11609           && host_integerp (arg1, false)
11610           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11611           && host_integerp (TREE_OPERAND (arg0, 1), false)
11612           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11613         {
11614           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11615           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11616           tree lshift;
11617           tree arg00;
11618
11619           if (low0 == low1)
11620             {
11621               arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11622
11623               lshift = build_int_cst (type, -1);
11624               lshift = int_const_binop (code, lshift, arg1, 0);
11625
11626               return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
11627             }
11628         }
11629
11630       /* Rewrite an LROTATE_EXPR by a constant into an
11631          RROTATE_EXPR by a new constant.  */
11632       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11633         {
11634           tree tem = build_int_cst (TREE_TYPE (arg1),
11635                                     TYPE_PRECISION (type));
11636           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11637           return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
11638         }
11639
11640       /* If we have a rotate of a bit operation with the rotate count and
11641          the second operand of the bit operation both constant,
11642          permute the two operations.  */
11643       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11644           && (TREE_CODE (arg0) == BIT_AND_EXPR
11645               || TREE_CODE (arg0) == BIT_IOR_EXPR
11646               || TREE_CODE (arg0) == BIT_XOR_EXPR)
11647           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11648         return fold_build2_loc (loc, TREE_CODE (arg0), type,
11649                             fold_build2_loc (loc, code, type,
11650                                          TREE_OPERAND (arg0, 0), arg1),
11651                             fold_build2_loc (loc, code, type,
11652                                          TREE_OPERAND (arg0, 1), arg1));
11653
11654       /* Two consecutive rotates adding up to the precision of the
11655          type can be ignored.  */
11656       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11657           && TREE_CODE (arg0) == RROTATE_EXPR
11658           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11659           && TREE_INT_CST_HIGH (arg1) == 0
11660           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11661           && ((TREE_INT_CST_LOW (arg1)
11662                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11663               == (unsigned int) TYPE_PRECISION (type)))
11664         return TREE_OPERAND (arg0, 0);
11665
11666       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11667               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11668          if the latter can be further optimized.  */
11669       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11670           && TREE_CODE (arg0) == BIT_AND_EXPR
11671           && TREE_CODE (arg1) == INTEGER_CST
11672           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11673         {
11674           tree mask = fold_build2_loc (loc, code, type,
11675                                    fold_convert_loc (loc, type,
11676                                                      TREE_OPERAND (arg0, 1)),
11677                                    arg1);
11678           tree shift = fold_build2_loc (loc, code, type,
11679                                     fold_convert_loc (loc, type,
11680                                                       TREE_OPERAND (arg0, 0)),
11681                                     arg1);
11682           tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
11683           if (tem)
11684             return tem;
11685         }
11686
11687       return NULL_TREE;
11688
11689     case MIN_EXPR:
11690       if (operand_equal_p (arg0, arg1, 0))
11691         return omit_one_operand_loc (loc, type, arg0, arg1);
11692       if (INTEGRAL_TYPE_P (type)
11693           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11694         return omit_one_operand_loc (loc, type, arg1, arg0);
11695       tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
11696       if (tem)
11697         return tem;
11698       goto associate;
11699
11700     case MAX_EXPR:
11701       if (operand_equal_p (arg0, arg1, 0))
11702         return omit_one_operand_loc (loc, type, arg0, arg1);
11703       if (INTEGRAL_TYPE_P (type)
11704           && TYPE_MAX_VALUE (type)
11705           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11706         return omit_one_operand_loc (loc, type, arg1, arg0);
11707       tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
11708       if (tem)
11709         return tem;
11710       goto associate;
11711
11712     case TRUTH_ANDIF_EXPR:
11713       /* Note that the operands of this must be ints
11714          and their values must be 0 or 1.
11715          ("true" is a fixed value perhaps depending on the language.)  */
11716       /* If first arg is constant zero, return it.  */
11717       if (integer_zerop (arg0))
11718         return fold_convert_loc (loc, type, arg0);
11719     case TRUTH_AND_EXPR:
11720       /* If either arg is constant true, drop it.  */
11721       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11722         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11723       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11724           /* Preserve sequence points.  */
11725           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11726         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11727       /* If second arg is constant zero, result is zero, but first arg
11728          must be evaluated.  */
11729       if (integer_zerop (arg1))
11730         return omit_one_operand_loc (loc, type, arg1, arg0);
11731       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11732          case will be handled here.  */
11733       if (integer_zerop (arg0))
11734         return omit_one_operand_loc (loc, type, arg0, arg1);
11735
11736       /* !X && X is always false.  */
11737       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11738           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11739         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11740       /* X && !X is always false.  */
11741       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11742           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11743         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11744
11745       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
11746          means A >= Y && A != MAX, but in this case we know that
11747          A < X <= MAX.  */
11748
11749       if (!TREE_SIDE_EFFECTS (arg0)
11750           && !TREE_SIDE_EFFECTS (arg1))
11751         {
11752           tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
11753           if (tem && !operand_equal_p (tem, arg0, 0))
11754             return fold_build2_loc (loc, code, type, tem, arg1);
11755
11756           tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
11757           if (tem && !operand_equal_p (tem, arg1, 0))
11758             return fold_build2_loc (loc, code, type, arg0, tem);
11759         }
11760
11761     truth_andor:
11762       /* We only do these simplifications if we are optimizing.  */
11763       if (!optimize)
11764         return NULL_TREE;
11765
11766       /* Check for things like (A || B) && (A || C).  We can convert this
11767          to A || (B && C).  Note that either operator can be any of the four
11768          truth and/or operations and the transformation will still be
11769          valid.   Also note that we only care about order for the
11770          ANDIF and ORIF operators.  If B contains side effects, this
11771          might change the truth-value of A.  */
11772       if (TREE_CODE (arg0) == TREE_CODE (arg1)
11773           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
11774               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
11775               || TREE_CODE (arg0) == TRUTH_AND_EXPR
11776               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
11777           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
11778         {
11779           tree a00 = TREE_OPERAND (arg0, 0);
11780           tree a01 = TREE_OPERAND (arg0, 1);
11781           tree a10 = TREE_OPERAND (arg1, 0);
11782           tree a11 = TREE_OPERAND (arg1, 1);
11783           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
11784                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
11785                              && (code == TRUTH_AND_EXPR
11786                                  || code == TRUTH_OR_EXPR));
11787
11788           if (operand_equal_p (a00, a10, 0))
11789             return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
11790                                 fold_build2_loc (loc, code, type, a01, a11));
11791           else if (commutative && operand_equal_p (a00, a11, 0))
11792             return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
11793                                 fold_build2_loc (loc, code, type, a01, a10));
11794           else if (commutative && operand_equal_p (a01, a10, 0))
11795             return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
11796                                 fold_build2_loc (loc, code, type, a00, a11));
11797
11798           /* This case if tricky because we must either have commutative
11799              operators or else A10 must not have side-effects.  */
11800
11801           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
11802                    && operand_equal_p (a01, a11, 0))
11803             return fold_build2_loc (loc, TREE_CODE (arg0), type,
11804                                 fold_build2_loc (loc, code, type, a00, a10),
11805                                 a01);
11806         }
11807
11808       /* See if we can build a range comparison.  */
11809       if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
11810         return tem;
11811
11812       /* Check for the possibility of merging component references.  If our
11813          lhs is another similar operation, try to merge its rhs with our
11814          rhs.  Then try to merge our lhs and rhs.  */
11815       if (TREE_CODE (arg0) == code
11816           && 0 != (tem = fold_truthop (loc, code, type,
11817                                        TREE_OPERAND (arg0, 1), arg1)))
11818         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
11819
11820       if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0)
11821         return tem;
11822
11823       return NULL_TREE;
11824
11825     case TRUTH_ORIF_EXPR:
11826       /* Note that the operands of this must be ints
11827          and their values must be 0 or true.
11828          ("true" is a fixed value perhaps depending on the language.)  */
11829       /* If first arg is constant true, return it.  */
11830       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11831         return fold_convert_loc (loc, type, arg0);
11832     case TRUTH_OR_EXPR:
11833       /* If either arg is constant zero, drop it.  */
11834       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
11835         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11836       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
11837           /* Preserve sequence points.  */
11838           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11839         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11840       /* If second arg is constant true, result is true, but we must
11841          evaluate first arg.  */
11842       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
11843         return omit_one_operand_loc (loc, type, arg1, arg0);
11844       /* Likewise for first arg, but note this only occurs here for
11845          TRUTH_OR_EXPR.  */
11846       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11847         return omit_one_operand_loc (loc, type, arg0, arg1);
11848
11849       /* !X || X is always true.  */
11850       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11851           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11852         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
11853       /* X || !X is always true.  */
11854       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11855           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11856         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
11857
11858       goto truth_andor;
11859
11860     case TRUTH_XOR_EXPR:
11861       /* If the second arg is constant zero, drop it.  */
11862       if (integer_zerop (arg1))
11863         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11864       /* If the second arg is constant true, this is a logical inversion.  */
11865       if (integer_onep (arg1))
11866         {
11867           /* Only call invert_truthvalue if operand is a truth value.  */
11868           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
11869             tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
11870           else
11871             tem = invert_truthvalue_loc (loc, arg0);
11872           return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
11873         }
11874       /* Identical arguments cancel to zero.  */
11875       if (operand_equal_p (arg0, arg1, 0))
11876         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11877
11878       /* !X ^ X is always true.  */
11879       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11880           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11881         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
11882
11883       /* X ^ !X is always true.  */
11884       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11885           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11886         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
11887
11888       return NULL_TREE;
11889
11890     case EQ_EXPR:
11891     case NE_EXPR:
11892       tem = fold_comparison (loc, code, type, op0, op1);
11893       if (tem != NULL_TREE)
11894         return tem;
11895
11896       /* bool_var != 0 becomes bool_var. */
11897       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11898           && code == NE_EXPR)
11899         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11900
11901       /* bool_var == 1 becomes bool_var. */
11902       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11903           && code == EQ_EXPR)
11904         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11905
11906       /* bool_var != 1 becomes !bool_var. */
11907       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11908           && code == NE_EXPR)
11909         return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
11910                             fold_convert_loc (loc, type, arg0));
11911
11912       /* bool_var == 0 becomes !bool_var. */
11913       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11914           && code == EQ_EXPR)
11915         return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
11916                             fold_convert_loc (loc, type, arg0));
11917
11918       /* !exp != 0 becomes !exp */
11919       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
11920           && code == NE_EXPR)
11921         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11922
11923       /* If this is an equality comparison of the address of two non-weak,
11924          unaliased symbols neither of which are extern (since we do not
11925          have access to attributes for externs), then we know the result.  */
11926       if (TREE_CODE (arg0) == ADDR_EXPR
11927           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
11928           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
11929           && ! lookup_attribute ("alias",
11930                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
11931           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
11932           && TREE_CODE (arg1) == ADDR_EXPR
11933           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
11934           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
11935           && ! lookup_attribute ("alias",
11936                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
11937           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
11938         {
11939           /* We know that we're looking at the address of two
11940              non-weak, unaliased, static _DECL nodes.
11941
11942              It is both wasteful and incorrect to call operand_equal_p
11943              to compare the two ADDR_EXPR nodes.  It is wasteful in that
11944              all we need to do is test pointer equality for the arguments
11945              to the two ADDR_EXPR nodes.  It is incorrect to use
11946              operand_equal_p as that function is NOT equivalent to a
11947              C equality test.  It can in fact return false for two
11948              objects which would test as equal using the C equality
11949              operator.  */
11950           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
11951           return constant_boolean_node (equal
11952                                         ? code == EQ_EXPR : code != EQ_EXPR,
11953                                         type);
11954         }
11955
11956       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
11957          a MINUS_EXPR of a constant, we can convert it into a comparison with
11958          a revised constant as long as no overflow occurs.  */
11959       if (TREE_CODE (arg1) == INTEGER_CST
11960           && (TREE_CODE (arg0) == PLUS_EXPR
11961               || TREE_CODE (arg0) == MINUS_EXPR)
11962           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11963           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
11964                                       ? MINUS_EXPR : PLUS_EXPR,
11965                                       fold_convert_loc (loc, TREE_TYPE (arg0),
11966                                                         arg1),
11967                                       TREE_OPERAND (arg0, 1), 0))
11968           && !TREE_OVERFLOW (tem))
11969         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
11970
11971       /* Similarly for a NEGATE_EXPR.  */
11972       if (TREE_CODE (arg0) == NEGATE_EXPR
11973           && TREE_CODE (arg1) == INTEGER_CST
11974           && 0 != (tem = negate_expr (arg1))
11975           && TREE_CODE (tem) == INTEGER_CST
11976           && !TREE_OVERFLOW (tem))
11977         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
11978
11979       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
11980       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11981           && TREE_CODE (arg1) == INTEGER_CST
11982           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11983         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11984                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
11985                                          fold_convert_loc (loc,
11986                                                            TREE_TYPE (arg0),
11987                                                            arg1),
11988                                          TREE_OPERAND (arg0, 1)));
11989
11990       /* Transform comparisons of the form X +- Y CMP X to Y CMP 0.  */
11991       if ((TREE_CODE (arg0) == PLUS_EXPR
11992            || TREE_CODE (arg0) == POINTER_PLUS_EXPR
11993            || TREE_CODE (arg0) == MINUS_EXPR)
11994           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11995           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11996               || POINTER_TYPE_P (TREE_TYPE (arg0))))
11997         {
11998           tree val = TREE_OPERAND (arg0, 1);
11999           return omit_two_operands_loc (loc, type,
12000                                     fold_build2_loc (loc, code, type,
12001                                                  val,
12002                                                  build_int_cst (TREE_TYPE (val),
12003                                                                 0)),
12004                                     TREE_OPERAND (arg0, 0), arg1);
12005         }
12006
12007       /* Transform comparisons of the form C - X CMP X if C % 2 == 1.  */
12008       if (TREE_CODE (arg0) == MINUS_EXPR
12009           && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12010           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)
12011           && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12012         {
12013           return omit_two_operands_loc (loc, type,
12014                                     code == NE_EXPR
12015                                     ? boolean_true_node : boolean_false_node,
12016                                     TREE_OPERAND (arg0, 1), arg1);
12017         }
12018
12019       /* If we have X - Y == 0, we can convert that to X == Y and similarly
12020          for !=.  Don't do this for ordered comparisons due to overflow.  */
12021       if (TREE_CODE (arg0) == MINUS_EXPR
12022           && integer_zerop (arg1))
12023         return fold_build2_loc (loc, code, type,
12024                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12025
12026       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
12027       if (TREE_CODE (arg0) == ABS_EXPR
12028           && (integer_zerop (arg1) || real_zerop (arg1)))
12029         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12030
12031       /* If this is an EQ or NE comparison with zero and ARG0 is
12032          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
12033          two operations, but the latter can be done in one less insn
12034          on machines that have only two-operand insns or on which a
12035          constant cannot be the first operand.  */
12036       if (TREE_CODE (arg0) == BIT_AND_EXPR
12037           && integer_zerop (arg1))
12038         {
12039           tree arg00 = TREE_OPERAND (arg0, 0);
12040           tree arg01 = TREE_OPERAND (arg0, 1);
12041           if (TREE_CODE (arg00) == LSHIFT_EXPR
12042               && integer_onep (TREE_OPERAND (arg00, 0)))
12043             {
12044               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12045                                       arg01, TREE_OPERAND (arg00, 1));
12046               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12047                                  build_int_cst (TREE_TYPE (arg0), 1));
12048               return fold_build2_loc (loc, code, type,
12049                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12050                                   arg1);
12051             }
12052           else if (TREE_CODE (arg01) == LSHIFT_EXPR
12053                    && integer_onep (TREE_OPERAND (arg01, 0)))
12054             {
12055               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12056                                       arg00, TREE_OPERAND (arg01, 1));
12057               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12058                                  build_int_cst (TREE_TYPE (arg0), 1));
12059               return fold_build2_loc (loc, code, type,
12060                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12061                                   arg1);
12062             }
12063         }
12064
12065       /* If this is an NE or EQ comparison of zero against the result of a
12066          signed MOD operation whose second operand is a power of 2, make
12067          the MOD operation unsigned since it is simpler and equivalent.  */
12068       if (integer_zerop (arg1)
12069           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12070           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12071               || TREE_CODE (arg0) == CEIL_MOD_EXPR
12072               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12073               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12074           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12075         {
12076           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12077           tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12078                                      fold_convert_loc (loc, newtype,
12079                                                        TREE_OPERAND (arg0, 0)),
12080                                      fold_convert_loc (loc, newtype,
12081                                                        TREE_OPERAND (arg0, 1)));
12082
12083           return fold_build2_loc (loc, code, type, newmod,
12084                               fold_convert_loc (loc, newtype, arg1));
12085         }
12086
12087       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12088          C1 is a valid shift constant, and C2 is a power of two, i.e.
12089          a single bit.  */
12090       if (TREE_CODE (arg0) == BIT_AND_EXPR
12091           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12092           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12093              == INTEGER_CST
12094           && integer_pow2p (TREE_OPERAND (arg0, 1))
12095           && integer_zerop (arg1))
12096         {
12097           tree itype = TREE_TYPE (arg0);
12098           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12099           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12100
12101           /* Check for a valid shift count.  */
12102           if (TREE_INT_CST_HIGH (arg001) == 0
12103               && TREE_INT_CST_LOW (arg001) < prec)
12104             {
12105               tree arg01 = TREE_OPERAND (arg0, 1);
12106               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12107               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12108               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12109                  can be rewritten as (X & (C2 << C1)) != 0.  */
12110               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12111                 {
12112                   tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12113                   tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12114                   return fold_build2_loc (loc, code, type, tem, arg1);
12115                 }
12116               /* Otherwise, for signed (arithmetic) shifts,
12117                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12118                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
12119               else if (!TYPE_UNSIGNED (itype))
12120                 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12121                                     arg000, build_int_cst (itype, 0));
12122               /* Otherwise, of unsigned (logical) shifts,
12123                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12124                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
12125               else
12126                 return omit_one_operand_loc (loc, type,
12127                                          code == EQ_EXPR ? integer_one_node
12128                                                          : integer_zero_node,
12129                                          arg000);
12130             }
12131         }
12132
12133       /* If this is an NE comparison of zero with an AND of one, remove the
12134          comparison since the AND will give the correct value.  */
12135       if (code == NE_EXPR
12136           && integer_zerop (arg1)
12137           && TREE_CODE (arg0) == BIT_AND_EXPR
12138           && integer_onep (TREE_OPERAND (arg0, 1)))
12139         return fold_convert_loc (loc, type, arg0);
12140
12141       /* If we have (A & C) == C where C is a power of 2, convert this into
12142          (A & C) != 0.  Similarly for NE_EXPR.  */
12143       if (TREE_CODE (arg0) == BIT_AND_EXPR
12144           && integer_pow2p (TREE_OPERAND (arg0, 1))
12145           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12146         return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12147                             arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12148                                                     integer_zero_node));
12149
12150       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12151          bit, then fold the expression into A < 0 or A >= 0.  */
12152       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12153       if (tem)
12154         return tem;
12155
12156       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12157          Similarly for NE_EXPR.  */
12158       if (TREE_CODE (arg0) == BIT_AND_EXPR
12159           && TREE_CODE (arg1) == INTEGER_CST
12160           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12161         {
12162           tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12163                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
12164                                    TREE_OPERAND (arg0, 1));
12165           tree dandnotc = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12166                                        arg1, notc);
12167           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12168           if (integer_nonzerop (dandnotc))
12169             return omit_one_operand_loc (loc, type, rslt, arg0);
12170         }
12171
12172       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12173          Similarly for NE_EXPR.  */
12174       if (TREE_CODE (arg0) == BIT_IOR_EXPR
12175           && TREE_CODE (arg1) == INTEGER_CST
12176           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12177         {
12178           tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12179           tree candnotd = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12180                                        TREE_OPERAND (arg0, 1), notd);
12181           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12182           if (integer_nonzerop (candnotd))
12183             return omit_one_operand_loc (loc, type, rslt, arg0);
12184         }
12185
12186       /* If this is a comparison of a field, we may be able to simplify it.  */
12187       if ((TREE_CODE (arg0) == COMPONENT_REF
12188            || TREE_CODE (arg0) == BIT_FIELD_REF)
12189           /* Handle the constant case even without -O
12190              to make sure the warnings are given.  */
12191           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12192         {
12193           t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12194           if (t1)
12195             return t1;
12196         }
12197
12198       /* Optimize comparisons of strlen vs zero to a compare of the
12199          first character of the string vs zero.  To wit,
12200                 strlen(ptr) == 0   =>  *ptr == 0
12201                 strlen(ptr) != 0   =>  *ptr != 0
12202          Other cases should reduce to one of these two (or a constant)
12203          due to the return value of strlen being unsigned.  */
12204       if (TREE_CODE (arg0) == CALL_EXPR
12205           && integer_zerop (arg1))
12206         {
12207           tree fndecl = get_callee_fndecl (arg0);
12208
12209           if (fndecl
12210               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12211               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12212               && call_expr_nargs (arg0) == 1
12213               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12214             {
12215               tree iref = build_fold_indirect_ref_loc (loc,
12216                                                    CALL_EXPR_ARG (arg0, 0));
12217               return fold_build2_loc (loc, code, type, iref,
12218                                   build_int_cst (TREE_TYPE (iref), 0));
12219             }
12220         }
12221
12222       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12223          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12224       if (TREE_CODE (arg0) == RSHIFT_EXPR
12225           && integer_zerop (arg1)
12226           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12227         {
12228           tree arg00 = TREE_OPERAND (arg0, 0);
12229           tree arg01 = TREE_OPERAND (arg0, 1);
12230           tree itype = TREE_TYPE (arg00);
12231           if (TREE_INT_CST_HIGH (arg01) == 0
12232               && TREE_INT_CST_LOW (arg01)
12233                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12234             {
12235               if (TYPE_UNSIGNED (itype))
12236                 {
12237                   itype = signed_type_for (itype);
12238                   arg00 = fold_convert_loc (loc, itype, arg00);
12239                 }
12240               return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12241                                   type, arg00, build_int_cst (itype, 0));
12242             }
12243         }
12244
12245       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12246       if (integer_zerop (arg1)
12247           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12248         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12249                             TREE_OPERAND (arg0, 1));
12250
12251       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12252       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12253           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12254         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12255                             build_int_cst (TREE_TYPE (arg1), 0));
12256       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12257       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12258           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12259           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12260         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12261                             build_int_cst (TREE_TYPE (arg1), 0));
12262
12263       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12264       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12265           && TREE_CODE (arg1) == INTEGER_CST
12266           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12267         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12268                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12269                                          TREE_OPERAND (arg0, 1), arg1));
12270
12271       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12272          (X & C) == 0 when C is a single bit.  */
12273       if (TREE_CODE (arg0) == BIT_AND_EXPR
12274           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12275           && integer_zerop (arg1)
12276           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12277         {
12278           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12279                              TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12280                              TREE_OPERAND (arg0, 1));
12281           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12282                               type, tem, arg1);
12283         }
12284
12285       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12286          constant C is a power of two, i.e. a single bit.  */
12287       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12288           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12289           && integer_zerop (arg1)
12290           && integer_pow2p (TREE_OPERAND (arg0, 1))
12291           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12292                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12293         {
12294           tree arg00 = TREE_OPERAND (arg0, 0);
12295           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12296                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
12297         }
12298
12299       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12300          when is C is a power of two, i.e. a single bit.  */
12301       if (TREE_CODE (arg0) == BIT_AND_EXPR
12302           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12303           && integer_zerop (arg1)
12304           && integer_pow2p (TREE_OPERAND (arg0, 1))
12305           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12306                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12307         {
12308           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12309           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12310                              arg000, TREE_OPERAND (arg0, 1));
12311           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12312                               tem, build_int_cst (TREE_TYPE (tem), 0));
12313         }
12314
12315       if (integer_zerop (arg1)
12316           && tree_expr_nonzero_p (arg0))
12317         {
12318           tree res = constant_boolean_node (code==NE_EXPR, type);
12319           return omit_one_operand_loc (loc, type, res, arg0);
12320         }
12321
12322       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
12323       if (TREE_CODE (arg0) == NEGATE_EXPR
12324           && TREE_CODE (arg1) == NEGATE_EXPR)
12325         return fold_build2_loc (loc, code, type,
12326                             TREE_OPERAND (arg0, 0),
12327                             TREE_OPERAND (arg1, 0));
12328
12329       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
12330       if (TREE_CODE (arg0) == BIT_AND_EXPR
12331           && TREE_CODE (arg1) == BIT_AND_EXPR)
12332         {
12333           tree arg00 = TREE_OPERAND (arg0, 0);
12334           tree arg01 = TREE_OPERAND (arg0, 1);
12335           tree arg10 = TREE_OPERAND (arg1, 0);
12336           tree arg11 = TREE_OPERAND (arg1, 1);
12337           tree itype = TREE_TYPE (arg0);
12338
12339           if (operand_equal_p (arg01, arg11, 0))
12340             return fold_build2_loc (loc, code, type,
12341                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12342                                              fold_build2_loc (loc,
12343                                                           BIT_XOR_EXPR, itype,
12344                                                           arg00, arg10),
12345                                              arg01),
12346                                 build_int_cst (itype, 0));
12347
12348           if (operand_equal_p (arg01, arg10, 0))
12349             return fold_build2_loc (loc, code, type,
12350                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12351                                              fold_build2_loc (loc,
12352                                                           BIT_XOR_EXPR, itype,
12353                                                           arg00, arg11),
12354                                              arg01),
12355                                 build_int_cst (itype, 0));
12356
12357           if (operand_equal_p (arg00, arg11, 0))
12358             return fold_build2_loc (loc, code, type,
12359                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12360                                              fold_build2_loc (loc,
12361                                                           BIT_XOR_EXPR, itype,
12362                                                           arg01, arg10),
12363                                              arg00),
12364                                 build_int_cst (itype, 0));
12365
12366           if (operand_equal_p (arg00, arg10, 0))
12367             return fold_build2_loc (loc, code, type,
12368                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12369                                              fold_build2_loc (loc,
12370                                                           BIT_XOR_EXPR, itype,
12371                                                           arg01, arg11),
12372                                              arg00),
12373                                 build_int_cst (itype, 0));
12374         }
12375
12376       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12377           && TREE_CODE (arg1) == BIT_XOR_EXPR)
12378         {
12379           tree arg00 = TREE_OPERAND (arg0, 0);
12380           tree arg01 = TREE_OPERAND (arg0, 1);
12381           tree arg10 = TREE_OPERAND (arg1, 0);
12382           tree arg11 = TREE_OPERAND (arg1, 1);
12383           tree itype = TREE_TYPE (arg0);
12384
12385           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12386              operand_equal_p guarantees no side-effects so we don't need
12387              to use omit_one_operand on Z.  */
12388           if (operand_equal_p (arg01, arg11, 0))
12389             return fold_build2_loc (loc, code, type, arg00, arg10);
12390           if (operand_equal_p (arg01, arg10, 0))
12391             return fold_build2_loc (loc, code, type, arg00, arg11);
12392           if (operand_equal_p (arg00, arg11, 0))
12393             return fold_build2_loc (loc, code, type, arg01, arg10);
12394           if (operand_equal_p (arg00, arg10, 0))
12395             return fold_build2_loc (loc, code, type, arg01, arg11);
12396
12397           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
12398           if (TREE_CODE (arg01) == INTEGER_CST
12399               && TREE_CODE (arg11) == INTEGER_CST)
12400             return fold_build2_loc (loc, code, type,
12401                                 fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00,
12402                                              fold_build2_loc (loc,
12403                                                           BIT_XOR_EXPR, itype,
12404                                                           arg01, arg11)),
12405                                 arg10);
12406         }
12407
12408       /* Attempt to simplify equality/inequality comparisons of complex
12409          values.  Only lower the comparison if the result is known or
12410          can be simplified to a single scalar comparison.  */
12411       if ((TREE_CODE (arg0) == COMPLEX_EXPR
12412            || TREE_CODE (arg0) == COMPLEX_CST)
12413           && (TREE_CODE (arg1) == COMPLEX_EXPR
12414               || TREE_CODE (arg1) == COMPLEX_CST))
12415         {
12416           tree real0, imag0, real1, imag1;
12417           tree rcond, icond;
12418
12419           if (TREE_CODE (arg0) == COMPLEX_EXPR)
12420             {
12421               real0 = TREE_OPERAND (arg0, 0);
12422               imag0 = TREE_OPERAND (arg0, 1);
12423             }
12424           else
12425             {
12426               real0 = TREE_REALPART (arg0);
12427               imag0 = TREE_IMAGPART (arg0);
12428             }
12429
12430           if (TREE_CODE (arg1) == COMPLEX_EXPR)
12431             {
12432               real1 = TREE_OPERAND (arg1, 0);
12433               imag1 = TREE_OPERAND (arg1, 1);
12434             }
12435           else
12436             {
12437               real1 = TREE_REALPART (arg1);
12438               imag1 = TREE_IMAGPART (arg1);
12439             }
12440
12441           rcond = fold_binary_loc (loc, code, type, real0, real1);
12442           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12443             {
12444               if (integer_zerop (rcond))
12445                 {
12446                   if (code == EQ_EXPR)
12447                     return omit_two_operands_loc (loc, type, boolean_false_node,
12448                                               imag0, imag1);
12449                   return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12450                 }
12451               else
12452                 {
12453                   if (code == NE_EXPR)
12454                     return omit_two_operands_loc (loc, type, boolean_true_node,
12455                                               imag0, imag1);
12456                   return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12457                 }
12458             }
12459
12460           icond = fold_binary_loc (loc, code, type, imag0, imag1);
12461           if (icond && TREE_CODE (icond) == INTEGER_CST)
12462             {
12463               if (integer_zerop (icond))
12464                 {
12465                   if (code == EQ_EXPR)
12466                     return omit_two_operands_loc (loc, type, boolean_false_node,
12467                                               real0, real1);
12468                   return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12469                 }
12470               else
12471                 {
12472                   if (code == NE_EXPR)
12473                     return omit_two_operands_loc (loc, type, boolean_true_node,
12474                                               real0, real1);
12475                   return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12476                 }
12477             }
12478         }
12479
12480       return NULL_TREE;
12481
12482     case LT_EXPR:
12483     case GT_EXPR:
12484     case LE_EXPR:
12485     case GE_EXPR:
12486       tem = fold_comparison (loc, code, type, op0, op1);
12487       if (tem != NULL_TREE)
12488         return tem;
12489
12490       /* Transform comparisons of the form X +- C CMP X.  */
12491       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12492           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12493           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12494                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12495               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12496                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12497         {
12498           tree arg01 = TREE_OPERAND (arg0, 1);
12499           enum tree_code code0 = TREE_CODE (arg0);
12500           int is_positive;
12501
12502           if (TREE_CODE (arg01) == REAL_CST)
12503             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12504           else
12505             is_positive = tree_int_cst_sgn (arg01);
12506
12507           /* (X - c) > X becomes false.  */
12508           if (code == GT_EXPR
12509               && ((code0 == MINUS_EXPR && is_positive >= 0)
12510                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12511             {
12512               if (TREE_CODE (arg01) == INTEGER_CST
12513                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12514                 fold_overflow_warning (("assuming signed overflow does not "
12515                                         "occur when assuming that (X - c) > X "
12516                                         "is always false"),
12517                                        WARN_STRICT_OVERFLOW_ALL);
12518               return constant_boolean_node (0, type);
12519             }
12520
12521           /* Likewise (X + c) < X becomes false.  */
12522           if (code == LT_EXPR
12523               && ((code0 == PLUS_EXPR && is_positive >= 0)
12524                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12525             {
12526               if (TREE_CODE (arg01) == INTEGER_CST
12527                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12528                 fold_overflow_warning (("assuming signed overflow does not "
12529                                         "occur when assuming that "
12530                                         "(X + c) < X is always false"),
12531                                        WARN_STRICT_OVERFLOW_ALL);
12532               return constant_boolean_node (0, type);
12533             }
12534
12535           /* Convert (X - c) <= X to true.  */
12536           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12537               && code == LE_EXPR
12538               && ((code0 == MINUS_EXPR && is_positive >= 0)
12539                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12540             {
12541               if (TREE_CODE (arg01) == INTEGER_CST
12542                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12543                 fold_overflow_warning (("assuming signed overflow does not "
12544                                         "occur when assuming that "
12545                                         "(X - c) <= X is always true"),
12546                                        WARN_STRICT_OVERFLOW_ALL);
12547               return constant_boolean_node (1, type);
12548             }
12549
12550           /* Convert (X + c) >= X to true.  */
12551           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12552               && code == GE_EXPR
12553               && ((code0 == PLUS_EXPR && is_positive >= 0)
12554                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12555             {
12556               if (TREE_CODE (arg01) == INTEGER_CST
12557                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12558                 fold_overflow_warning (("assuming signed overflow does not "
12559                                         "occur when assuming that "
12560                                         "(X + c) >= X is always true"),
12561                                        WARN_STRICT_OVERFLOW_ALL);
12562               return constant_boolean_node (1, type);
12563             }
12564
12565           if (TREE_CODE (arg01) == INTEGER_CST)
12566             {
12567               /* Convert X + c > X and X - c < X to true for integers.  */
12568               if (code == GT_EXPR
12569                   && ((code0 == PLUS_EXPR && is_positive > 0)
12570                       || (code0 == MINUS_EXPR && is_positive < 0)))
12571                 {
12572                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12573                     fold_overflow_warning (("assuming signed overflow does "
12574                                             "not occur when assuming that "
12575                                             "(X + c) > X is always true"),
12576                                            WARN_STRICT_OVERFLOW_ALL);
12577                   return constant_boolean_node (1, type);
12578                 }
12579
12580               if (code == LT_EXPR
12581                   && ((code0 == MINUS_EXPR && is_positive > 0)
12582                       || (code0 == PLUS_EXPR && is_positive < 0)))
12583                 {
12584                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12585                     fold_overflow_warning (("assuming signed overflow does "
12586                                             "not occur when assuming that "
12587                                             "(X - c) < X is always true"),
12588                                            WARN_STRICT_OVERFLOW_ALL);
12589                   return constant_boolean_node (1, type);
12590                 }
12591
12592               /* Convert X + c <= X and X - c >= X to false for integers.  */
12593               if (code == LE_EXPR
12594                   && ((code0 == PLUS_EXPR && is_positive > 0)
12595                       || (code0 == MINUS_EXPR && is_positive < 0)))
12596                 {
12597                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12598                     fold_overflow_warning (("assuming signed overflow does "
12599                                             "not occur when assuming that "
12600                                             "(X + c) <= X is always false"),
12601                                            WARN_STRICT_OVERFLOW_ALL);
12602                   return constant_boolean_node (0, type);
12603                 }
12604
12605               if (code == GE_EXPR
12606                   && ((code0 == MINUS_EXPR && is_positive > 0)
12607                       || (code0 == PLUS_EXPR && is_positive < 0)))
12608                 {
12609                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12610                     fold_overflow_warning (("assuming signed overflow does "
12611                                             "not occur when assuming that "
12612                                             "(X - c) >= X is always false"),
12613                                            WARN_STRICT_OVERFLOW_ALL);
12614                   return constant_boolean_node (0, type);
12615                 }
12616             }
12617         }
12618
12619       /* Comparisons with the highest or lowest possible integer of
12620          the specified precision will have known values.  */
12621       {
12622         tree arg1_type = TREE_TYPE (arg1);
12623         unsigned int width = TYPE_PRECISION (arg1_type);
12624
12625         if (TREE_CODE (arg1) == INTEGER_CST
12626             && width <= 2 * HOST_BITS_PER_WIDE_INT
12627             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12628           {
12629             HOST_WIDE_INT signed_max_hi;
12630             unsigned HOST_WIDE_INT signed_max_lo;
12631             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12632
12633             if (width <= HOST_BITS_PER_WIDE_INT)
12634               {
12635                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12636                                 - 1;
12637                 signed_max_hi = 0;
12638                 max_hi = 0;
12639
12640                 if (TYPE_UNSIGNED (arg1_type))
12641                   {
12642                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12643                     min_lo = 0;
12644                     min_hi = 0;
12645                   }
12646                 else
12647                   {
12648                     max_lo = signed_max_lo;
12649                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12650                     min_hi = -1;
12651                   }
12652               }
12653             else
12654               {
12655                 width -= HOST_BITS_PER_WIDE_INT;
12656                 signed_max_lo = -1;
12657                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12658                                 - 1;
12659                 max_lo = -1;
12660                 min_lo = 0;
12661
12662                 if (TYPE_UNSIGNED (arg1_type))
12663                   {
12664                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12665                     min_hi = 0;
12666                   }
12667                 else
12668                   {
12669                     max_hi = signed_max_hi;
12670                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12671                   }
12672               }
12673
12674             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12675                 && TREE_INT_CST_LOW (arg1) == max_lo)
12676               switch (code)
12677                 {
12678                 case GT_EXPR:
12679                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12680
12681                 case GE_EXPR:
12682                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12683
12684                 case LE_EXPR:
12685                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12686
12687                 case LT_EXPR:
12688                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12689
12690                 /* The GE_EXPR and LT_EXPR cases above are not normally
12691                    reached because of previous transformations.  */
12692
12693                 default:
12694                   break;
12695                 }
12696             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12697                      == max_hi
12698                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12699               switch (code)
12700                 {
12701                 case GT_EXPR:
12702                   arg1 = const_binop (PLUS_EXPR, arg1,
12703                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12704                   return fold_build2_loc (loc, EQ_EXPR, type,
12705                                       fold_convert_loc (loc,
12706                                                         TREE_TYPE (arg1), arg0),
12707                                       arg1);
12708                 case LE_EXPR:
12709                   arg1 = const_binop (PLUS_EXPR, arg1,
12710                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12711                   return fold_build2_loc (loc, NE_EXPR, type,
12712                                       fold_convert_loc (loc, TREE_TYPE (arg1),
12713                                                         arg0),
12714                                       arg1);
12715                 default:
12716                   break;
12717                 }
12718             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12719                      == min_hi
12720                      && TREE_INT_CST_LOW (arg1) == min_lo)
12721               switch (code)
12722                 {
12723                 case LT_EXPR:
12724                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12725
12726                 case LE_EXPR:
12727                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12728
12729                 case GE_EXPR:
12730                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12731
12732                 case GT_EXPR:
12733                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12734
12735                 default:
12736                   break;
12737                 }
12738             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12739                      == min_hi
12740                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12741               switch (code)
12742                 {
12743                 case GE_EXPR:
12744                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12745                   return fold_build2_loc (loc, NE_EXPR, type,
12746                                       fold_convert_loc (loc,
12747                                                         TREE_TYPE (arg1), arg0),
12748                                       arg1);
12749                 case LT_EXPR:
12750                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12751                   return fold_build2_loc (loc, EQ_EXPR, type,
12752                                       fold_convert_loc (loc, TREE_TYPE (arg1),
12753                                                         arg0),
12754                                       arg1);
12755                 default:
12756                   break;
12757                 }
12758
12759             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
12760                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
12761                      && TYPE_UNSIGNED (arg1_type)
12762                      /* We will flip the signedness of the comparison operator
12763                         associated with the mode of arg1, so the sign bit is
12764                         specified by this mode.  Check that arg1 is the signed
12765                         max associated with this sign bit.  */
12766                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
12767                      /* signed_type does not work on pointer types.  */
12768                      && INTEGRAL_TYPE_P (arg1_type))
12769               {
12770                 /* The following case also applies to X < signed_max+1
12771                    and X >= signed_max+1 because previous transformations.  */
12772                 if (code == LE_EXPR || code == GT_EXPR)
12773                   {
12774                     tree st;
12775                     st = signed_type_for (TREE_TYPE (arg1));
12776                     return fold_build2_loc (loc,
12777                                         code == LE_EXPR ? GE_EXPR : LT_EXPR,
12778                                         type, fold_convert_loc (loc, st, arg0),
12779                                         build_int_cst (st, 0));
12780                   }
12781               }
12782           }
12783       }
12784
12785       /* If we are comparing an ABS_EXPR with a constant, we can
12786          convert all the cases into explicit comparisons, but they may
12787          well not be faster than doing the ABS and one comparison.
12788          But ABS (X) <= C is a range comparison, which becomes a subtraction
12789          and a comparison, and is probably faster.  */
12790       if (code == LE_EXPR
12791           && TREE_CODE (arg1) == INTEGER_CST
12792           && TREE_CODE (arg0) == ABS_EXPR
12793           && ! TREE_SIDE_EFFECTS (arg0)
12794           && (0 != (tem = negate_expr (arg1)))
12795           && TREE_CODE (tem) == INTEGER_CST
12796           && !TREE_OVERFLOW (tem))
12797         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12798                             build2 (GE_EXPR, type,
12799                                     TREE_OPERAND (arg0, 0), tem),
12800                             build2 (LE_EXPR, type,
12801                                     TREE_OPERAND (arg0, 0), arg1));
12802
12803       /* Convert ABS_EXPR<x> >= 0 to true.  */
12804       strict_overflow_p = false;
12805       if (code == GE_EXPR
12806           && (integer_zerop (arg1)
12807               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
12808                   && real_zerop (arg1)))
12809           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12810         {
12811           if (strict_overflow_p)
12812             fold_overflow_warning (("assuming signed overflow does not occur "
12813                                     "when simplifying comparison of "
12814                                     "absolute value and zero"),
12815                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
12816           return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12817         }
12818
12819       /* Convert ABS_EXPR<x> < 0 to false.  */
12820       strict_overflow_p = false;
12821       if (code == LT_EXPR
12822           && (integer_zerop (arg1) || real_zerop (arg1))
12823           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12824         {
12825           if (strict_overflow_p)
12826             fold_overflow_warning (("assuming signed overflow does not occur "
12827                                     "when simplifying comparison of "
12828                                     "absolute value and zero"),
12829                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
12830           return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12831         }
12832
12833       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12834          and similarly for >= into !=.  */
12835       if ((code == LT_EXPR || code == GE_EXPR)
12836           && TYPE_UNSIGNED (TREE_TYPE (arg0))
12837           && TREE_CODE (arg1) == LSHIFT_EXPR
12838           && integer_onep (TREE_OPERAND (arg1, 0)))
12839         {
12840           tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12841                         build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12842                                 TREE_OPERAND (arg1, 1)),
12843                         build_int_cst (TREE_TYPE (arg0), 0));
12844           goto fold_binary_exit;
12845         }
12846
12847       if ((code == LT_EXPR || code == GE_EXPR)
12848           && TYPE_UNSIGNED (TREE_TYPE (arg0))
12849           && CONVERT_EXPR_P (arg1)
12850           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12851           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12852         {
12853           tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12854                         fold_convert_loc (loc, TREE_TYPE (arg0),
12855                                           build2 (RSHIFT_EXPR,
12856                                                   TREE_TYPE (arg0), arg0,
12857                                                   TREE_OPERAND (TREE_OPERAND (arg1, 0),
12858                                                                 1))),
12859                         build_int_cst (TREE_TYPE (arg0), 0));
12860           goto fold_binary_exit;
12861         }
12862
12863       return NULL_TREE;
12864
12865     case UNORDERED_EXPR:
12866     case ORDERED_EXPR:
12867     case UNLT_EXPR:
12868     case UNLE_EXPR:
12869     case UNGT_EXPR:
12870     case UNGE_EXPR:
12871     case UNEQ_EXPR:
12872     case LTGT_EXPR:
12873       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
12874         {
12875           t1 = fold_relational_const (code, type, arg0, arg1);
12876           if (t1 != NULL_TREE)
12877             return t1;
12878         }
12879
12880       /* If the first operand is NaN, the result is constant.  */
12881       if (TREE_CODE (arg0) == REAL_CST
12882           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
12883           && (code != LTGT_EXPR || ! flag_trapping_math))
12884         {
12885           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12886                ? integer_zero_node
12887                : integer_one_node;
12888           return omit_one_operand_loc (loc, type, t1, arg1);
12889         }
12890
12891       /* If the second operand is NaN, the result is constant.  */
12892       if (TREE_CODE (arg1) == REAL_CST
12893           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
12894           && (code != LTGT_EXPR || ! flag_trapping_math))
12895         {
12896           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12897                ? integer_zero_node
12898                : integer_one_node;
12899           return omit_one_operand_loc (loc, type, t1, arg0);
12900         }
12901
12902       /* Simplify unordered comparison of something with itself.  */
12903       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
12904           && operand_equal_p (arg0, arg1, 0))
12905         return constant_boolean_node (1, type);
12906
12907       if (code == LTGT_EXPR
12908           && !flag_trapping_math
12909           && operand_equal_p (arg0, arg1, 0))
12910         return constant_boolean_node (0, type);
12911
12912       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
12913       {
12914         tree targ0 = strip_float_extensions (arg0);
12915         tree targ1 = strip_float_extensions (arg1);
12916         tree newtype = TREE_TYPE (targ0);
12917
12918         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
12919           newtype = TREE_TYPE (targ1);
12920
12921         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
12922           return fold_build2_loc (loc, code, type,
12923                               fold_convert_loc (loc, newtype, targ0),
12924                               fold_convert_loc (loc, newtype, targ1));
12925       }
12926
12927       return NULL_TREE;
12928
12929     case COMPOUND_EXPR:
12930       /* When pedantic, a compound expression can be neither an lvalue
12931          nor an integer constant expression.  */
12932       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12933         return NULL_TREE;
12934       /* Don't let (0, 0) be null pointer constant.  */
12935       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
12936                                  : fold_convert_loc (loc, type, arg1);
12937       return pedantic_non_lvalue_loc (loc, tem);
12938
12939     case COMPLEX_EXPR:
12940       if ((TREE_CODE (arg0) == REAL_CST
12941            && TREE_CODE (arg1) == REAL_CST)
12942           || (TREE_CODE (arg0) == INTEGER_CST
12943               && TREE_CODE (arg1) == INTEGER_CST))
12944         return build_complex (type, arg0, arg1);
12945       return NULL_TREE;
12946
12947     case ASSERT_EXPR:
12948       /* An ASSERT_EXPR should never be passed to fold_binary.  */
12949       gcc_unreachable ();
12950
12951     default:
12952       return NULL_TREE;
12953     } /* switch (code) */
12954  fold_binary_exit:
12955   protected_set_expr_location (tem, loc);
12956   return tem;
12957 }
12958
12959 /* Callback for walk_tree, looking for LABEL_EXPR.  Return *TP if it is
12960    a LABEL_EXPR; otherwise return NULL_TREE.  Do not check the subtrees
12961    of GOTO_EXPR.  */
12962
12963 static tree
12964 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
12965 {
12966   switch (TREE_CODE (*tp))
12967     {
12968     case LABEL_EXPR:
12969       return *tp;
12970
12971     case GOTO_EXPR:
12972       *walk_subtrees = 0;
12973
12974       /* ... fall through ...  */
12975
12976     default:
12977       return NULL_TREE;
12978     }
12979 }
12980
12981 /* Return whether the sub-tree ST contains a label which is accessible from
12982    outside the sub-tree.  */
12983
12984 static bool
12985 contains_label_p (tree st)
12986 {
12987   return
12988    (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
12989 }
12990
12991 /* Fold a ternary expression of code CODE and type TYPE with operands
12992    OP0, OP1, and OP2.  Return the folded expression if folding is
12993    successful.  Otherwise, return NULL_TREE.  */
12994
12995 tree
12996 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12997               tree op0, tree op1, tree op2)
12998 {
12999   tree tem;
13000   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13001   enum tree_code_class kind = TREE_CODE_CLASS (code);
13002
13003   gcc_assert (IS_EXPR_CODE_CLASS (kind)
13004               && TREE_CODE_LENGTH (code) == 3);
13005
13006   /* Strip any conversions that don't change the mode.  This is safe
13007      for every expression, except for a comparison expression because
13008      its signedness is derived from its operands.  So, in the latter
13009      case, only strip conversions that don't change the signedness.
13010
13011      Note that this is done as an internal manipulation within the
13012      constant folder, in order to find the simplest representation of
13013      the arguments so that their form can be studied.  In any cases,
13014      the appropriate type conversions should be put back in the tree
13015      that will get out of the constant folder.  */
13016   if (op0)
13017     {
13018       arg0 = op0;
13019       STRIP_NOPS (arg0);
13020     }
13021
13022   if (op1)
13023     {
13024       arg1 = op1;
13025       STRIP_NOPS (arg1);
13026     }
13027
13028   switch (code)
13029     {
13030     case COMPONENT_REF:
13031       if (TREE_CODE (arg0) == CONSTRUCTOR
13032           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13033         {
13034           unsigned HOST_WIDE_INT idx;
13035           tree field, value;
13036           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13037             if (field == arg1)
13038               return value;
13039         }
13040       return NULL_TREE;
13041
13042     case COND_EXPR:
13043       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13044          so all simple results must be passed through pedantic_non_lvalue.  */
13045       if (TREE_CODE (arg0) == INTEGER_CST)
13046         {
13047           tree unused_op = integer_zerop (arg0) ? op1 : op2;
13048           tem = integer_zerop (arg0) ? op2 : op1;
13049           /* Only optimize constant conditions when the selected branch
13050              has the same type as the COND_EXPR.  This avoids optimizing
13051              away "c ? x : throw", where the throw has a void type.
13052              Avoid throwing away that operand which contains label.  */
13053           if ((!TREE_SIDE_EFFECTS (unused_op)
13054                || !contains_label_p (unused_op))
13055               && (! VOID_TYPE_P (TREE_TYPE (tem))
13056                   || VOID_TYPE_P (type)))
13057             return pedantic_non_lvalue_loc (loc, tem);
13058           return NULL_TREE;
13059         }
13060       if (operand_equal_p (arg1, op2, 0))
13061         return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13062
13063       /* If we have A op B ? A : C, we may be able to convert this to a
13064          simpler expression, depending on the operation and the values
13065          of B and C.  Signed zeros prevent all of these transformations,
13066          for reasons given above each one.
13067
13068          Also try swapping the arguments and inverting the conditional.  */
13069       if (COMPARISON_CLASS_P (arg0)
13070           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13071                                              arg1, TREE_OPERAND (arg0, 1))
13072           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13073         {
13074           tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13075           if (tem)
13076             return tem;
13077         }
13078
13079       if (COMPARISON_CLASS_P (arg0)
13080           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13081                                              op2,
13082                                              TREE_OPERAND (arg0, 1))
13083           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13084         {
13085           tem = fold_truth_not_expr (loc, arg0);
13086           if (tem && COMPARISON_CLASS_P (tem))
13087             {
13088               tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13089               if (tem)
13090                 return tem;
13091             }
13092         }
13093
13094       /* If the second operand is simpler than the third, swap them
13095          since that produces better jump optimization results.  */
13096       if (truth_value_p (TREE_CODE (arg0))
13097           && tree_swap_operands_p (op1, op2, false))
13098         {
13099           /* See if this can be inverted.  If it can't, possibly because
13100              it was a floating-point inequality comparison, don't do
13101              anything.  */
13102           tem = fold_truth_not_expr (loc, arg0);
13103           if (tem)
13104             return fold_build3_loc (loc, code, type, tem, op2, op1);
13105         }
13106
13107       /* Convert A ? 1 : 0 to simply A.  */
13108       if (integer_onep (op1)
13109           && integer_zerop (op2)
13110           /* If we try to convert OP0 to our type, the
13111              call to fold will try to move the conversion inside
13112              a COND, which will recurse.  In that case, the COND_EXPR
13113              is probably the best choice, so leave it alone.  */
13114           && type == TREE_TYPE (arg0))
13115         return pedantic_non_lvalue_loc (loc, arg0);
13116
13117       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
13118          over COND_EXPR in cases such as floating point comparisons.  */
13119       if (integer_zerop (op1)
13120           && integer_onep (op2)
13121           && truth_value_p (TREE_CODE (arg0)))
13122         return pedantic_non_lvalue_loc (loc,
13123                                     fold_convert_loc (loc, type,
13124                                               invert_truthvalue_loc (loc,
13125                                                                      arg0)));
13126
13127       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
13128       if (TREE_CODE (arg0) == LT_EXPR
13129           && integer_zerop (TREE_OPERAND (arg0, 1))
13130           && integer_zerop (op2)
13131           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13132         {
13133           /* sign_bit_p only checks ARG1 bits within A's precision.
13134              If <sign bit of A> has wider type than A, bits outside
13135              of A's precision in <sign bit of A> need to be checked.
13136              If they are all 0, this optimization needs to be done
13137              in unsigned A's type, if they are all 1 in signed A's type,
13138              otherwise this can't be done.  */
13139           if (TYPE_PRECISION (TREE_TYPE (tem))
13140               < TYPE_PRECISION (TREE_TYPE (arg1))
13141               && TYPE_PRECISION (TREE_TYPE (tem))
13142                  < TYPE_PRECISION (type))
13143             {
13144               unsigned HOST_WIDE_INT mask_lo;
13145               HOST_WIDE_INT mask_hi;
13146               int inner_width, outer_width;
13147               tree tem_type;
13148
13149               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13150               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13151               if (outer_width > TYPE_PRECISION (type))
13152                 outer_width = TYPE_PRECISION (type);
13153
13154               if (outer_width > HOST_BITS_PER_WIDE_INT)
13155                 {
13156                   mask_hi = ((unsigned HOST_WIDE_INT) -1
13157                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13158                   mask_lo = -1;
13159                 }
13160               else
13161                 {
13162                   mask_hi = 0;
13163                   mask_lo = ((unsigned HOST_WIDE_INT) -1
13164                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
13165                 }
13166               if (inner_width > HOST_BITS_PER_WIDE_INT)
13167                 {
13168                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13169                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
13170                   mask_lo = 0;
13171                 }
13172               else
13173                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13174                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
13175
13176               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13177                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13178                 {
13179                   tem_type = signed_type_for (TREE_TYPE (tem));
13180                   tem = fold_convert_loc (loc, tem_type, tem);
13181                 }
13182               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13183                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13184                 {
13185                   tem_type = unsigned_type_for (TREE_TYPE (tem));
13186                   tem = fold_convert_loc (loc, tem_type, tem);
13187                 }
13188               else
13189                 tem = NULL;
13190             }
13191
13192           if (tem)
13193             return
13194               fold_convert_loc (loc, type,
13195                                 fold_build2_loc (loc, BIT_AND_EXPR,
13196                                              TREE_TYPE (tem), tem,
13197                                              fold_convert_loc (loc,
13198                                                                TREE_TYPE (tem),
13199                                                                arg1)));
13200         }
13201
13202       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
13203          already handled above.  */
13204       if (TREE_CODE (arg0) == BIT_AND_EXPR
13205           && integer_onep (TREE_OPERAND (arg0, 1))
13206           && integer_zerop (op2)
13207           && integer_pow2p (arg1))
13208         {
13209           tree tem = TREE_OPERAND (arg0, 0);
13210           STRIP_NOPS (tem);
13211           if (TREE_CODE (tem) == RSHIFT_EXPR
13212               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13213               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13214                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13215             return fold_build2_loc (loc, BIT_AND_EXPR, type,
13216                                 TREE_OPERAND (tem, 0), arg1);
13217         }
13218
13219       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13220          is probably obsolete because the first operand should be a
13221          truth value (that's why we have the two cases above), but let's
13222          leave it in until we can confirm this for all front-ends.  */
13223       if (integer_zerop (op2)
13224           && TREE_CODE (arg0) == NE_EXPR
13225           && integer_zerop (TREE_OPERAND (arg0, 1))
13226           && integer_pow2p (arg1)
13227           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13228           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13229                               arg1, OEP_ONLY_CONST))
13230         return pedantic_non_lvalue_loc (loc,
13231                                     fold_convert_loc (loc, type,
13232                                                       TREE_OPERAND (arg0, 0)));
13233
13234       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13235       if (integer_zerop (op2)
13236           && truth_value_p (TREE_CODE (arg0))
13237           && truth_value_p (TREE_CODE (arg1)))
13238         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13239                             fold_convert_loc (loc, type, arg0),
13240                             arg1);
13241
13242       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13243       if (integer_onep (op2)
13244           && truth_value_p (TREE_CODE (arg0))
13245           && truth_value_p (TREE_CODE (arg1)))
13246         {
13247           /* Only perform transformation if ARG0 is easily inverted.  */
13248           tem = fold_truth_not_expr (loc, arg0);
13249           if (tem)
13250             return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13251                                 fold_convert_loc (loc, type, tem),
13252                                 arg1);
13253         }
13254
13255       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13256       if (integer_zerop (arg1)
13257           && truth_value_p (TREE_CODE (arg0))
13258           && truth_value_p (TREE_CODE (op2)))
13259         {
13260           /* Only perform transformation if ARG0 is easily inverted.  */
13261           tem = fold_truth_not_expr (loc, arg0);
13262           if (tem)
13263             return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13264                                 fold_convert_loc (loc, type, tem),
13265                                 op2);
13266         }
13267
13268       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13269       if (integer_onep (arg1)
13270           && truth_value_p (TREE_CODE (arg0))
13271           && truth_value_p (TREE_CODE (op2)))
13272         return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13273                             fold_convert_loc (loc, type, arg0),
13274                             op2);
13275
13276       return NULL_TREE;
13277
13278     case CALL_EXPR:
13279       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
13280          of fold_ternary on them.  */
13281       gcc_unreachable ();
13282
13283     case BIT_FIELD_REF:
13284       if ((TREE_CODE (arg0) == VECTOR_CST
13285            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13286           && type == TREE_TYPE (TREE_TYPE (arg0)))
13287         {
13288           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13289           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13290
13291           if (width != 0
13292               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13293               && (idx % width) == 0
13294               && (idx = idx / width)
13295                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13296             {
13297               tree elements = NULL_TREE;
13298
13299               if (TREE_CODE (arg0) == VECTOR_CST)
13300                 elements = TREE_VECTOR_CST_ELTS (arg0);
13301               else
13302                 {
13303                   unsigned HOST_WIDE_INT idx;
13304                   tree value;
13305
13306                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13307                     elements = tree_cons (NULL_TREE, value, elements);
13308                 }
13309               while (idx-- > 0 && elements)
13310                 elements = TREE_CHAIN (elements);
13311               if (elements)
13312                 return TREE_VALUE (elements);
13313               else
13314                 return fold_convert_loc (loc, type, integer_zero_node);
13315             }
13316         }
13317
13318       /* A bit-field-ref that referenced the full argument can be stripped.  */
13319       if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13320           && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13321           && integer_zerop (op2))
13322         return fold_convert_loc (loc, type, arg0);
13323
13324       return NULL_TREE;
13325
13326     default:
13327       return NULL_TREE;
13328     } /* switch (code) */
13329 }
13330
13331 /* Perform constant folding and related simplification of EXPR.
13332    The related simplifications include x*1 => x, x*0 => 0, etc.,
13333    and application of the associative law.
13334    NOP_EXPR conversions may be removed freely (as long as we
13335    are careful not to change the type of the overall expression).
13336    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13337    but we can constant-fold them if they have constant operands.  */
13338
13339 #ifdef ENABLE_FOLD_CHECKING
13340 # define fold(x) fold_1 (x)
13341 static tree fold_1 (tree);
13342 static
13343 #endif
13344 tree
13345 fold (tree expr)
13346 {
13347   const tree t = expr;
13348   enum tree_code code = TREE_CODE (t);
13349   enum tree_code_class kind = TREE_CODE_CLASS (code);
13350   tree tem;
13351   location_t loc = EXPR_LOCATION (expr);
13352
13353   /* Return right away if a constant.  */
13354   if (kind == tcc_constant)
13355     return t;
13356
13357   /* CALL_EXPR-like objects with variable numbers of operands are
13358      treated specially.  */
13359   if (kind == tcc_vl_exp)
13360     {
13361       if (code == CALL_EXPR)
13362         {
13363           tem = fold_call_expr (loc, expr, false);
13364           return tem ? tem : expr;
13365         }
13366       return expr;
13367     }
13368
13369   if (IS_EXPR_CODE_CLASS (kind))
13370     {
13371       tree type = TREE_TYPE (t);
13372       tree op0, op1, op2;
13373
13374       switch (TREE_CODE_LENGTH (code))
13375         {
13376         case 1:
13377           op0 = TREE_OPERAND (t, 0);
13378           tem = fold_unary_loc (loc, code, type, op0);
13379           return tem ? tem : expr;
13380         case 2:
13381           op0 = TREE_OPERAND (t, 0);
13382           op1 = TREE_OPERAND (t, 1);
13383           tem = fold_binary_loc (loc, code, type, op0, op1);
13384           return tem ? tem : expr;
13385         case 3:
13386           op0 = TREE_OPERAND (t, 0);
13387           op1 = TREE_OPERAND (t, 1);
13388           op2 = TREE_OPERAND (t, 2);
13389           tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13390           return tem ? tem : expr;
13391         default:
13392           break;
13393         }
13394     }
13395
13396   switch (code)
13397     {
13398     case ARRAY_REF:
13399       {
13400         tree op0 = TREE_OPERAND (t, 0);
13401         tree op1 = TREE_OPERAND (t, 1);
13402
13403         if (TREE_CODE (op1) == INTEGER_CST
13404             && TREE_CODE (op0) == CONSTRUCTOR
13405             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13406           {
13407             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13408             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13409             unsigned HOST_WIDE_INT begin = 0;
13410
13411             /* Find a matching index by means of a binary search.  */
13412             while (begin != end)
13413               {
13414                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13415                 tree index = VEC_index (constructor_elt, elts, middle)->index;
13416
13417                 if (TREE_CODE (index) == INTEGER_CST
13418                     && tree_int_cst_lt (index, op1))
13419                   begin = middle + 1;
13420                 else if (TREE_CODE (index) == INTEGER_CST
13421                          && tree_int_cst_lt (op1, index))
13422                   end = middle;
13423                 else if (TREE_CODE (index) == RANGE_EXPR
13424                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13425                   begin = middle + 1;
13426                 else if (TREE_CODE (index) == RANGE_EXPR
13427                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13428                   end = middle;
13429                 else
13430                   return VEC_index (constructor_elt, elts, middle)->value;
13431               }
13432           }
13433
13434         return t;
13435       }
13436
13437     case CONST_DECL:
13438       return fold (DECL_INITIAL (t));
13439
13440     default:
13441       return t;
13442     } /* switch (code) */
13443 }
13444
13445 #ifdef ENABLE_FOLD_CHECKING
13446 #undef fold
13447
13448 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13449 static void fold_check_failed (const_tree, const_tree);
13450 void print_fold_checksum (const_tree);
13451
13452 /* When --enable-checking=fold, compute a digest of expr before
13453    and after actual fold call to see if fold did not accidentally
13454    change original expr.  */
13455
13456 tree
13457 fold (tree expr)
13458 {
13459   tree ret;
13460   struct md5_ctx ctx;
13461   unsigned char checksum_before[16], checksum_after[16];
13462   htab_t ht;
13463
13464   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13465   md5_init_ctx (&ctx);
13466   fold_checksum_tree (expr, &ctx, ht);
13467   md5_finish_ctx (&ctx, checksum_before);
13468   htab_empty (ht);
13469
13470   ret = fold_1 (expr);
13471
13472   md5_init_ctx (&ctx);
13473   fold_checksum_tree (expr, &ctx, ht);
13474   md5_finish_ctx (&ctx, checksum_after);
13475   htab_delete (ht);
13476
13477   if (memcmp (checksum_before, checksum_after, 16))
13478     fold_check_failed (expr, ret);
13479
13480   return ret;
13481 }
13482
13483 void
13484 print_fold_checksum (const_tree expr)
13485 {
13486   struct md5_ctx ctx;
13487   unsigned char checksum[16], cnt;
13488   htab_t ht;
13489
13490   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13491   md5_init_ctx (&ctx);
13492   fold_checksum_tree (expr, &ctx, ht);
13493   md5_finish_ctx (&ctx, checksum);
13494   htab_delete (ht);
13495   for (cnt = 0; cnt < 16; ++cnt)
13496     fprintf (stderr, "%02x", checksum[cnt]);
13497   putc ('\n', stderr);
13498 }
13499
13500 static void
13501 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13502 {
13503   internal_error ("fold check: original tree changed by fold");
13504 }
13505
13506 static void
13507 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13508 {
13509   const void **slot;
13510   enum tree_code code;
13511   union tree_node buf;
13512   int i, len;
13513
13514 recursive_label:
13515
13516   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13517                <= sizeof (struct tree_function_decl))
13518               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13519   if (expr == NULL)
13520     return;
13521   slot = (const void **) htab_find_slot (ht, expr, INSERT);
13522   if (*slot != NULL)
13523     return;
13524   *slot = expr;
13525   code = TREE_CODE (expr);
13526   if (TREE_CODE_CLASS (code) == tcc_declaration
13527       && DECL_ASSEMBLER_NAME_SET_P (expr))
13528     {
13529       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
13530       memcpy ((char *) &buf, expr, tree_size (expr));
13531       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13532       expr = (tree) &buf;
13533     }
13534   else if (TREE_CODE_CLASS (code) == tcc_type
13535            && (TYPE_POINTER_TO (expr)
13536                || TYPE_REFERENCE_TO (expr)
13537                || TYPE_CACHED_VALUES_P (expr)
13538                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13539                || TYPE_NEXT_VARIANT (expr)))
13540     {
13541       /* Allow these fields to be modified.  */
13542       tree tmp;
13543       memcpy ((char *) &buf, expr, tree_size (expr));
13544       expr = tmp = (tree) &buf;
13545       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13546       TYPE_POINTER_TO (tmp) = NULL;
13547       TYPE_REFERENCE_TO (tmp) = NULL;
13548       TYPE_NEXT_VARIANT (tmp) = NULL;
13549       if (TYPE_CACHED_VALUES_P (tmp))
13550         {
13551           TYPE_CACHED_VALUES_P (tmp) = 0;
13552           TYPE_CACHED_VALUES (tmp) = NULL;
13553         }
13554     }
13555   md5_process_bytes (expr, tree_size (expr), ctx);
13556   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13557   if (TREE_CODE_CLASS (code) != tcc_type
13558       && TREE_CODE_CLASS (code) != tcc_declaration
13559       && code != TREE_LIST
13560       && code != SSA_NAME)
13561     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13562   switch (TREE_CODE_CLASS (code))
13563     {
13564     case tcc_constant:
13565       switch (code)
13566         {
13567         case STRING_CST:
13568           md5_process_bytes (TREE_STRING_POINTER (expr),
13569                              TREE_STRING_LENGTH (expr), ctx);
13570           break;
13571         case COMPLEX_CST:
13572           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13573           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13574           break;
13575         case VECTOR_CST:
13576           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13577           break;
13578         default:
13579           break;
13580         }
13581       break;
13582     case tcc_exceptional:
13583       switch (code)
13584         {
13585         case TREE_LIST:
13586           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13587           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13588           expr = TREE_CHAIN (expr);
13589           goto recursive_label;
13590           break;
13591         case TREE_VEC:
13592           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13593             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13594           break;
13595         default:
13596           break;
13597         }
13598       break;
13599     case tcc_expression:
13600     case tcc_reference:
13601     case tcc_comparison:
13602     case tcc_unary:
13603     case tcc_binary:
13604     case tcc_statement:
13605     case tcc_vl_exp:
13606       len = TREE_OPERAND_LENGTH (expr);
13607       for (i = 0; i < len; ++i)
13608         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13609       break;
13610     case tcc_declaration:
13611       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13612       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13613       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13614         {
13615           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13616           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13617           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13618           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13619           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13620         }
13621       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13622         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13623
13624       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13625         {
13626           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13627           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13628           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13629         }
13630       break;
13631     case tcc_type:
13632       if (TREE_CODE (expr) == ENUMERAL_TYPE)
13633         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13634       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13635       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13636       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13637       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13638       if (INTEGRAL_TYPE_P (expr)
13639           || SCALAR_FLOAT_TYPE_P (expr))
13640         {
13641           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13642           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13643         }
13644       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13645       if (TREE_CODE (expr) == RECORD_TYPE
13646           || TREE_CODE (expr) == UNION_TYPE
13647           || TREE_CODE (expr) == QUAL_UNION_TYPE)
13648         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13649       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13650       break;
13651     default:
13652       break;
13653     }
13654 }
13655
13656 /* Helper function for outputting the checksum of a tree T.  When
13657    debugging with gdb, you can "define mynext" to be "next" followed
13658    by "call debug_fold_checksum (op0)", then just trace down till the
13659    outputs differ.  */
13660
13661 void
13662 debug_fold_checksum (const_tree t)
13663 {
13664   int i;
13665   unsigned char checksum[16];
13666   struct md5_ctx ctx;
13667   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13668
13669   md5_init_ctx (&ctx);
13670   fold_checksum_tree (t, &ctx, ht);
13671   md5_finish_ctx (&ctx, checksum);
13672   htab_empty (ht);
13673
13674   for (i = 0; i < 16; i++)
13675     fprintf (stderr, "%d ", checksum[i]);
13676
13677   fprintf (stderr, "\n");
13678 }
13679
13680 #endif
13681
13682 /* Fold a unary tree expression with code CODE of type TYPE with an
13683    operand OP0.  LOC is the location of the resulting expression.
13684    Return a folded expression if successful.  Otherwise, return a tree
13685    expression with code CODE of type TYPE with an operand OP0.  */
13686
13687 tree
13688 fold_build1_stat_loc (location_t loc,
13689                       enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13690 {
13691   tree tem;
13692 #ifdef ENABLE_FOLD_CHECKING
13693   unsigned char checksum_before[16], checksum_after[16];
13694   struct md5_ctx ctx;
13695   htab_t ht;
13696
13697   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13698   md5_init_ctx (&ctx);
13699   fold_checksum_tree (op0, &ctx, ht);
13700   md5_finish_ctx (&ctx, checksum_before);
13701   htab_empty (ht);
13702 #endif
13703
13704   tem = fold_unary_loc (loc, code, type, op0);
13705   if (!tem)
13706     {
13707       tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13708       SET_EXPR_LOCATION (tem, loc);
13709     }
13710
13711 #ifdef ENABLE_FOLD_CHECKING
13712   md5_init_ctx (&ctx);
13713   fold_checksum_tree (op0, &ctx, ht);
13714   md5_finish_ctx (&ctx, checksum_after);
13715   htab_delete (ht);
13716
13717   if (memcmp (checksum_before, checksum_after, 16))
13718     fold_check_failed (op0, tem);
13719 #endif
13720   return tem;
13721 }
13722
13723 /* Fold a binary tree expression with code CODE of type TYPE with
13724    operands OP0 and OP1.  LOC is the location of the resulting
13725    expression.  Return a folded expression if successful.  Otherwise,
13726    return a tree expression with code CODE of type TYPE with operands
13727    OP0 and OP1.  */
13728
13729 tree
13730 fold_build2_stat_loc (location_t loc,
13731                       enum tree_code code, tree type, tree op0, tree op1
13732                       MEM_STAT_DECL)
13733 {
13734   tree tem;
13735 #ifdef ENABLE_FOLD_CHECKING
13736   unsigned char checksum_before_op0[16],
13737                 checksum_before_op1[16],
13738                 checksum_after_op0[16],
13739                 checksum_after_op1[16];
13740   struct md5_ctx ctx;
13741   htab_t ht;
13742
13743   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13744   md5_init_ctx (&ctx);
13745   fold_checksum_tree (op0, &ctx, ht);
13746   md5_finish_ctx (&ctx, checksum_before_op0);
13747   htab_empty (ht);
13748
13749   md5_init_ctx (&ctx);
13750   fold_checksum_tree (op1, &ctx, ht);
13751   md5_finish_ctx (&ctx, checksum_before_op1);
13752   htab_empty (ht);
13753 #endif
13754
13755   tem = fold_binary_loc (loc, code, type, op0, op1);
13756   if (!tem)
13757     {
13758       tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
13759       SET_EXPR_LOCATION (tem, loc);
13760     }
13761
13762 #ifdef ENABLE_FOLD_CHECKING
13763   md5_init_ctx (&ctx);
13764   fold_checksum_tree (op0, &ctx, ht);
13765   md5_finish_ctx (&ctx, checksum_after_op0);
13766   htab_empty (ht);
13767
13768   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13769     fold_check_failed (op0, tem);
13770
13771   md5_init_ctx (&ctx);
13772   fold_checksum_tree (op1, &ctx, ht);
13773   md5_finish_ctx (&ctx, checksum_after_op1);
13774   htab_delete (ht);
13775
13776   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13777     fold_check_failed (op1, tem);
13778 #endif
13779   return tem;
13780 }
13781
13782 /* Fold a ternary tree expression with code CODE of type TYPE with
13783    operands OP0, OP1, and OP2.  Return a folded expression if
13784    successful.  Otherwise, return a tree expression with code CODE of
13785    type TYPE with operands OP0, OP1, and OP2.  */
13786
13787 tree
13788 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
13789                       tree op0, tree op1, tree op2 MEM_STAT_DECL)
13790 {
13791   tree tem;
13792 #ifdef ENABLE_FOLD_CHECKING
13793   unsigned char checksum_before_op0[16],
13794                 checksum_before_op1[16],
13795                 checksum_before_op2[16],
13796                 checksum_after_op0[16],
13797                 checksum_after_op1[16],
13798                 checksum_after_op2[16];
13799   struct md5_ctx ctx;
13800   htab_t ht;
13801
13802   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13803   md5_init_ctx (&ctx);
13804   fold_checksum_tree (op0, &ctx, ht);
13805   md5_finish_ctx (&ctx, checksum_before_op0);
13806   htab_empty (ht);
13807
13808   md5_init_ctx (&ctx);
13809   fold_checksum_tree (op1, &ctx, ht);
13810   md5_finish_ctx (&ctx, checksum_before_op1);
13811   htab_empty (ht);
13812
13813   md5_init_ctx (&ctx);
13814   fold_checksum_tree (op2, &ctx, ht);
13815   md5_finish_ctx (&ctx, checksum_before_op2);
13816   htab_empty (ht);
13817 #endif
13818
13819   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13820   tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13821   if (!tem)
13822     {
13823       tem =  build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
13824       SET_EXPR_LOCATION (tem, loc);
13825     }
13826
13827 #ifdef ENABLE_FOLD_CHECKING
13828   md5_init_ctx (&ctx);
13829   fold_checksum_tree (op0, &ctx, ht);
13830   md5_finish_ctx (&ctx, checksum_after_op0);
13831   htab_empty (ht);
13832
13833   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13834     fold_check_failed (op0, tem);
13835
13836   md5_init_ctx (&ctx);
13837   fold_checksum_tree (op1, &ctx, ht);
13838   md5_finish_ctx (&ctx, checksum_after_op1);
13839   htab_empty (ht);
13840
13841   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13842     fold_check_failed (op1, tem);
13843
13844   md5_init_ctx (&ctx);
13845   fold_checksum_tree (op2, &ctx, ht);
13846   md5_finish_ctx (&ctx, checksum_after_op2);
13847   htab_delete (ht);
13848
13849   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13850     fold_check_failed (op2, tem);
13851 #endif
13852   return tem;
13853 }
13854
13855 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13856    arguments in ARGARRAY, and a null static chain.
13857    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
13858    of type TYPE from the given operands as constructed by build_call_array.  */
13859
13860 tree
13861 fold_build_call_array_loc (location_t loc, tree type, tree fn,
13862                            int nargs, tree *argarray)
13863 {
13864   tree tem;
13865 #ifdef ENABLE_FOLD_CHECKING
13866   unsigned char checksum_before_fn[16],
13867                 checksum_before_arglist[16],
13868                 checksum_after_fn[16],
13869                 checksum_after_arglist[16];
13870   struct md5_ctx ctx;
13871   htab_t ht;
13872   int i;
13873
13874   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13875   md5_init_ctx (&ctx);
13876   fold_checksum_tree (fn, &ctx, ht);
13877   md5_finish_ctx (&ctx, checksum_before_fn);
13878   htab_empty (ht);
13879
13880   md5_init_ctx (&ctx);
13881   for (i = 0; i < nargs; i++)
13882     fold_checksum_tree (argarray[i], &ctx, ht);
13883   md5_finish_ctx (&ctx, checksum_before_arglist);
13884   htab_empty (ht);
13885 #endif
13886
13887   tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
13888
13889 #ifdef ENABLE_FOLD_CHECKING
13890   md5_init_ctx (&ctx);
13891   fold_checksum_tree (fn, &ctx, ht);
13892   md5_finish_ctx (&ctx, checksum_after_fn);
13893   htab_empty (ht);
13894
13895   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13896     fold_check_failed (fn, tem);
13897
13898   md5_init_ctx (&ctx);
13899   for (i = 0; i < nargs; i++)
13900     fold_checksum_tree (argarray[i], &ctx, ht);
13901   md5_finish_ctx (&ctx, checksum_after_arglist);
13902   htab_delete (ht);
13903
13904   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13905     fold_check_failed (NULL_TREE, tem);
13906 #endif
13907   return tem;
13908 }
13909
13910 /* Perform constant folding and related simplification of initializer
13911    expression EXPR.  These behave identically to "fold_buildN" but ignore
13912    potential run-time traps and exceptions that fold must preserve.  */
13913
13914 #define START_FOLD_INIT \
13915   int saved_signaling_nans = flag_signaling_nans;\
13916   int saved_trapping_math = flag_trapping_math;\
13917   int saved_rounding_math = flag_rounding_math;\
13918   int saved_trapv = flag_trapv;\
13919   int saved_folding_initializer = folding_initializer;\
13920   flag_signaling_nans = 0;\
13921   flag_trapping_math = 0;\
13922   flag_rounding_math = 0;\
13923   flag_trapv = 0;\
13924   folding_initializer = 1;
13925
13926 #define END_FOLD_INIT \
13927   flag_signaling_nans = saved_signaling_nans;\
13928   flag_trapping_math = saved_trapping_math;\
13929   flag_rounding_math = saved_rounding_math;\
13930   flag_trapv = saved_trapv;\
13931   folding_initializer = saved_folding_initializer;
13932
13933 tree
13934 fold_build1_initializer_loc (location_t loc, enum tree_code code,
13935                              tree type, tree op)
13936 {
13937   tree result;
13938   START_FOLD_INIT;
13939
13940   result = fold_build1_loc (loc, code, type, op);
13941
13942   END_FOLD_INIT;
13943   return result;
13944 }
13945
13946 tree
13947 fold_build2_initializer_loc (location_t loc, enum tree_code code,
13948                              tree type, tree op0, tree op1)
13949 {
13950   tree result;
13951   START_FOLD_INIT;
13952
13953   result = fold_build2_loc (loc, code, type, op0, op1);
13954
13955   END_FOLD_INIT;
13956   return result;
13957 }
13958
13959 tree
13960 fold_build3_initializer_loc (location_t loc, enum tree_code code,
13961                              tree type, tree op0, tree op1, tree op2)
13962 {
13963   tree result;
13964   START_FOLD_INIT;
13965
13966   result = fold_build3_loc (loc, code, type, op0, op1, op2);
13967
13968   END_FOLD_INIT;
13969   return result;
13970 }
13971
13972 tree
13973 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
13974                                        int nargs, tree *argarray)
13975 {
13976   tree result;
13977   START_FOLD_INIT;
13978
13979   result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
13980
13981   END_FOLD_INIT;
13982   return result;
13983 }
13984
13985 #undef START_FOLD_INIT
13986 #undef END_FOLD_INIT
13987
13988 /* Determine if first argument is a multiple of second argument.  Return 0 if
13989    it is not, or we cannot easily determined it to be.
13990
13991    An example of the sort of thing we care about (at this point; this routine
13992    could surely be made more general, and expanded to do what the *_DIV_EXPR's
13993    fold cases do now) is discovering that
13994
13995      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13996
13997    is a multiple of
13998
13999      SAVE_EXPR (J * 8)
14000
14001    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14002
14003    This code also handles discovering that
14004
14005      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14006
14007    is a multiple of 8 so we don't have to worry about dealing with a
14008    possible remainder.
14009
14010    Note that we *look* inside a SAVE_EXPR only to determine how it was
14011    calculated; it is not safe for fold to do much of anything else with the
14012    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14013    at run time.  For example, the latter example above *cannot* be implemented
14014    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14015    evaluation time of the original SAVE_EXPR is not necessarily the same at
14016    the time the new expression is evaluated.  The only optimization of this
14017    sort that would be valid is changing
14018
14019      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14020
14021    divided by 8 to
14022
14023      SAVE_EXPR (I) * SAVE_EXPR (J)
14024
14025    (where the same SAVE_EXPR (J) is used in the original and the
14026    transformed version).  */
14027
14028 int
14029 multiple_of_p (tree type, const_tree top, const_tree bottom)
14030 {
14031   if (operand_equal_p (top, bottom, 0))
14032     return 1;
14033
14034   if (TREE_CODE (type) != INTEGER_TYPE)
14035     return 0;
14036
14037   switch (TREE_CODE (top))
14038     {
14039     case BIT_AND_EXPR:
14040       /* Bitwise and provides a power of two multiple.  If the mask is
14041          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
14042       if (!integer_pow2p (bottom))
14043         return 0;
14044       /* FALLTHRU */
14045
14046     case MULT_EXPR:
14047       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14048               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14049
14050     case PLUS_EXPR:
14051     case MINUS_EXPR:
14052       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14053               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14054
14055     case LSHIFT_EXPR:
14056       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14057         {
14058           tree op1, t1;
14059
14060           op1 = TREE_OPERAND (top, 1);
14061           /* const_binop may not detect overflow correctly,
14062              so check for it explicitly here.  */
14063           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14064               > TREE_INT_CST_LOW (op1)
14065               && TREE_INT_CST_HIGH (op1) == 0
14066               && 0 != (t1 = fold_convert (type,
14067                                           const_binop (LSHIFT_EXPR,
14068                                                        size_one_node,
14069                                                        op1, 0)))
14070               && !TREE_OVERFLOW (t1))
14071             return multiple_of_p (type, t1, bottom);
14072         }
14073       return 0;
14074
14075     case NOP_EXPR:
14076       /* Can't handle conversions from non-integral or wider integral type.  */
14077       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14078           || (TYPE_PRECISION (type)
14079               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14080         return 0;
14081
14082       /* .. fall through ...  */
14083
14084     case SAVE_EXPR:
14085       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14086
14087     case COND_EXPR:
14088       return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
14089               && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
14090
14091     case INTEGER_CST:
14092       if (TREE_CODE (bottom) != INTEGER_CST
14093           || integer_zerop (bottom)
14094           || (TYPE_UNSIGNED (type)
14095               && (tree_int_cst_sgn (top) < 0
14096                   || tree_int_cst_sgn (bottom) < 0)))
14097         return 0;
14098       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14099                                              top, bottom, 0));
14100
14101     default:
14102       return 0;
14103     }
14104 }
14105
14106 /* Return true if CODE or TYPE is known to be non-negative. */
14107
14108 static bool
14109 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14110 {
14111   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14112       && truth_value_p (code))
14113     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14114        have a signed:1 type (where the value is -1 and 0).  */
14115     return true;
14116   return false;
14117 }
14118
14119 /* Return true if (CODE OP0) is known to be non-negative.  If the return
14120    value is based on the assumption that signed overflow is undefined,
14121    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14122    *STRICT_OVERFLOW_P.  */
14123
14124 bool
14125 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14126                                 bool *strict_overflow_p)
14127 {
14128   if (TYPE_UNSIGNED (type))
14129     return true;
14130
14131   switch (code)
14132     {
14133     case ABS_EXPR:
14134       /* We can't return 1 if flag_wrapv is set because
14135          ABS_EXPR<INT_MIN> = INT_MIN.  */
14136       if (!INTEGRAL_TYPE_P (type))
14137         return true;
14138       if (TYPE_OVERFLOW_UNDEFINED (type))
14139         {
14140           *strict_overflow_p = true;
14141           return true;
14142         }
14143       break;
14144
14145     case NON_LVALUE_EXPR:
14146     case FLOAT_EXPR:
14147     case FIX_TRUNC_EXPR:
14148       return tree_expr_nonnegative_warnv_p (op0,
14149                                             strict_overflow_p);
14150
14151     case NOP_EXPR:
14152       {
14153         tree inner_type = TREE_TYPE (op0);
14154         tree outer_type = type;
14155
14156         if (TREE_CODE (outer_type) == REAL_TYPE)
14157           {
14158             if (TREE_CODE (inner_type) == REAL_TYPE)
14159               return tree_expr_nonnegative_warnv_p (op0,
14160                                                     strict_overflow_p);
14161             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14162               {
14163                 if (TYPE_UNSIGNED (inner_type))
14164                   return true;
14165                 return tree_expr_nonnegative_warnv_p (op0,
14166                                                       strict_overflow_p);
14167               }
14168           }
14169         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14170           {
14171             if (TREE_CODE (inner_type) == REAL_TYPE)
14172               return tree_expr_nonnegative_warnv_p (op0,
14173                                                     strict_overflow_p);
14174             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14175               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14176                       && TYPE_UNSIGNED (inner_type);
14177           }
14178       }
14179       break;
14180
14181     default:
14182       return tree_simple_nonnegative_warnv_p (code, type);
14183     }
14184
14185   /* We don't know sign of `t', so be conservative and return false.  */
14186   return false;
14187 }
14188
14189 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
14190    value is based on the assumption that signed overflow is undefined,
14191    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14192    *STRICT_OVERFLOW_P.  */
14193
14194 bool
14195 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14196                                       tree op1, bool *strict_overflow_p)
14197 {
14198   if (TYPE_UNSIGNED (type))
14199     return true;
14200
14201   switch (code)
14202     {
14203     case POINTER_PLUS_EXPR:
14204     case PLUS_EXPR:
14205       if (FLOAT_TYPE_P (type))
14206         return (tree_expr_nonnegative_warnv_p (op0,
14207                                                strict_overflow_p)
14208                 && tree_expr_nonnegative_warnv_p (op1,
14209                                                   strict_overflow_p));
14210
14211       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14212          both unsigned and at least 2 bits shorter than the result.  */
14213       if (TREE_CODE (type) == INTEGER_TYPE
14214           && TREE_CODE (op0) == NOP_EXPR
14215           && TREE_CODE (op1) == NOP_EXPR)
14216         {
14217           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14218           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14219           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14220               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14221             {
14222               unsigned int prec = MAX (TYPE_PRECISION (inner1),
14223                                        TYPE_PRECISION (inner2)) + 1;
14224               return prec < TYPE_PRECISION (type);
14225             }
14226         }
14227       break;
14228
14229     case MULT_EXPR:
14230       if (FLOAT_TYPE_P (type))
14231         {
14232           /* x * x for floating point x is always non-negative.  */
14233           if (operand_equal_p (op0, op1, 0))
14234             return true;
14235           return (tree_expr_nonnegative_warnv_p (op0,
14236                                                  strict_overflow_p)
14237                   && tree_expr_nonnegative_warnv_p (op1,
14238                                                     strict_overflow_p));
14239         }
14240
14241       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14242          both unsigned and their total bits is shorter than the result.  */
14243       if (TREE_CODE (type) == INTEGER_TYPE
14244           && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14245           && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14246         {
14247           tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14248             ? TREE_TYPE (TREE_OPERAND (op0, 0))
14249             : TREE_TYPE (op0);
14250           tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14251             ? TREE_TYPE (TREE_OPERAND (op1, 0))
14252             : TREE_TYPE (op1);
14253
14254           bool unsigned0 = TYPE_UNSIGNED (inner0);
14255           bool unsigned1 = TYPE_UNSIGNED (inner1);
14256
14257           if (TREE_CODE (op0) == INTEGER_CST)
14258             unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14259
14260           if (TREE_CODE (op1) == INTEGER_CST)
14261             unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14262
14263           if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14264               && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14265             {
14266               unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14267                 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14268                 : TYPE_PRECISION (inner0);
14269
14270               unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14271                 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14272                 : TYPE_PRECISION (inner1);
14273
14274               return precision0 + precision1 < TYPE_PRECISION (type);
14275             }
14276         }
14277       return false;
14278
14279     case BIT_AND_EXPR:
14280     case MAX_EXPR:
14281       return (tree_expr_nonnegative_warnv_p (op0,
14282                                              strict_overflow_p)
14283               || tree_expr_nonnegative_warnv_p (op1,
14284                                                 strict_overflow_p));
14285
14286     case BIT_IOR_EXPR:
14287     case BIT_XOR_EXPR:
14288     case MIN_EXPR:
14289     case RDIV_EXPR:
14290     case TRUNC_DIV_EXPR:
14291     case CEIL_DIV_EXPR:
14292     case FLOOR_DIV_EXPR:
14293     case ROUND_DIV_EXPR:
14294       return (tree_expr_nonnegative_warnv_p (op0,
14295                                              strict_overflow_p)
14296               && tree_expr_nonnegative_warnv_p (op1,
14297                                                 strict_overflow_p));
14298
14299     case TRUNC_MOD_EXPR:
14300     case CEIL_MOD_EXPR:
14301     case FLOOR_MOD_EXPR:
14302     case ROUND_MOD_EXPR:
14303       return tree_expr_nonnegative_warnv_p (op0,
14304                                             strict_overflow_p);
14305     default:
14306       return tree_simple_nonnegative_warnv_p (code, type);
14307     }
14308
14309   /* We don't know sign of `t', so be conservative and return false.  */
14310   return false;
14311 }
14312
14313 /* Return true if T is known to be non-negative.  If the return
14314    value is based on the assumption that signed overflow is undefined,
14315    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14316    *STRICT_OVERFLOW_P.  */
14317
14318 bool
14319 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14320 {
14321   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14322     return true;
14323
14324   switch (TREE_CODE (t))
14325     {
14326     case INTEGER_CST:
14327       return tree_int_cst_sgn (t) >= 0;
14328
14329     case REAL_CST:
14330       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14331
14332     case FIXED_CST:
14333       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14334
14335     case COND_EXPR:
14336       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14337                                              strict_overflow_p)
14338               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14339                                                 strict_overflow_p));
14340     default:
14341       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14342                                                    TREE_TYPE (t));
14343     }
14344   /* We don't know sign of `t', so be conservative and return false.  */
14345   return false;
14346 }
14347
14348 /* Return true if T is known to be non-negative.  If the return
14349    value is based on the assumption that signed overflow is undefined,
14350    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14351    *STRICT_OVERFLOW_P.  */
14352
14353 bool
14354 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14355                                tree arg0, tree arg1, bool *strict_overflow_p)
14356 {
14357   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14358     switch (DECL_FUNCTION_CODE (fndecl))
14359       {
14360         CASE_FLT_FN (BUILT_IN_ACOS):
14361         CASE_FLT_FN (BUILT_IN_ACOSH):
14362         CASE_FLT_FN (BUILT_IN_CABS):
14363         CASE_FLT_FN (BUILT_IN_COSH):
14364         CASE_FLT_FN (BUILT_IN_ERFC):
14365         CASE_FLT_FN (BUILT_IN_EXP):
14366         CASE_FLT_FN (BUILT_IN_EXP10):
14367         CASE_FLT_FN (BUILT_IN_EXP2):
14368         CASE_FLT_FN (BUILT_IN_FABS):
14369         CASE_FLT_FN (BUILT_IN_FDIM):
14370         CASE_FLT_FN (BUILT_IN_HYPOT):
14371         CASE_FLT_FN (BUILT_IN_POW10):
14372         CASE_INT_FN (BUILT_IN_FFS):
14373         CASE_INT_FN (BUILT_IN_PARITY):
14374         CASE_INT_FN (BUILT_IN_POPCOUNT):
14375       case BUILT_IN_BSWAP32:
14376       case BUILT_IN_BSWAP64:
14377         /* Always true.  */
14378         return true;
14379
14380         CASE_FLT_FN (BUILT_IN_SQRT):
14381         /* sqrt(-0.0) is -0.0.  */
14382         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14383           return true;
14384         return tree_expr_nonnegative_warnv_p (arg0,
14385                                               strict_overflow_p);
14386
14387         CASE_FLT_FN (BUILT_IN_ASINH):
14388         CASE_FLT_FN (BUILT_IN_ATAN):
14389         CASE_FLT_FN (BUILT_IN_ATANH):
14390         CASE_FLT_FN (BUILT_IN_CBRT):
14391         CASE_FLT_FN (BUILT_IN_CEIL):
14392         CASE_FLT_FN (BUILT_IN_ERF):
14393         CASE_FLT_FN (BUILT_IN_EXPM1):
14394         CASE_FLT_FN (BUILT_IN_FLOOR):
14395         CASE_FLT_FN (BUILT_IN_FMOD):
14396         CASE_FLT_FN (BUILT_IN_FREXP):
14397         CASE_FLT_FN (BUILT_IN_LCEIL):
14398         CASE_FLT_FN (BUILT_IN_LDEXP):
14399         CASE_FLT_FN (BUILT_IN_LFLOOR):
14400         CASE_FLT_FN (BUILT_IN_LLCEIL):
14401         CASE_FLT_FN (BUILT_IN_LLFLOOR):
14402         CASE_FLT_FN (BUILT_IN_LLRINT):
14403         CASE_FLT_FN (BUILT_IN_LLROUND):
14404         CASE_FLT_FN (BUILT_IN_LRINT):
14405         CASE_FLT_FN (BUILT_IN_LROUND):
14406         CASE_FLT_FN (BUILT_IN_MODF):
14407         CASE_FLT_FN (BUILT_IN_NEARBYINT):
14408         CASE_FLT_FN (BUILT_IN_RINT):
14409         CASE_FLT_FN (BUILT_IN_ROUND):
14410         CASE_FLT_FN (BUILT_IN_SCALB):
14411         CASE_FLT_FN (BUILT_IN_SCALBLN):
14412         CASE_FLT_FN (BUILT_IN_SCALBN):
14413         CASE_FLT_FN (BUILT_IN_SIGNBIT):
14414         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14415         CASE_FLT_FN (BUILT_IN_SINH):
14416         CASE_FLT_FN (BUILT_IN_TANH):
14417         CASE_FLT_FN (BUILT_IN_TRUNC):
14418         /* True if the 1st argument is nonnegative.  */
14419         return tree_expr_nonnegative_warnv_p (arg0,
14420                                               strict_overflow_p);
14421
14422         CASE_FLT_FN (BUILT_IN_FMAX):
14423         /* True if the 1st OR 2nd arguments are nonnegative.  */
14424         return (tree_expr_nonnegative_warnv_p (arg0,
14425                                                strict_overflow_p)
14426                 || (tree_expr_nonnegative_warnv_p (arg1,
14427                                                    strict_overflow_p)));
14428
14429         CASE_FLT_FN (BUILT_IN_FMIN):
14430         /* True if the 1st AND 2nd arguments are nonnegative.  */
14431         return (tree_expr_nonnegative_warnv_p (arg0,
14432                                                strict_overflow_p)
14433                 && (tree_expr_nonnegative_warnv_p (arg1,
14434                                                    strict_overflow_p)));
14435
14436         CASE_FLT_FN (BUILT_IN_COPYSIGN):
14437         /* True if the 2nd argument is nonnegative.  */
14438         return tree_expr_nonnegative_warnv_p (arg1,
14439                                               strict_overflow_p);
14440
14441         CASE_FLT_FN (BUILT_IN_POWI):
14442         /* True if the 1st argument is nonnegative or the second
14443            argument is an even integer.  */
14444         if (TREE_CODE (arg1) == INTEGER_CST
14445             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14446           return true;
14447         return tree_expr_nonnegative_warnv_p (arg0,
14448                                               strict_overflow_p);
14449
14450         CASE_FLT_FN (BUILT_IN_POW):
14451         /* True if the 1st argument is nonnegative or the second
14452            argument is an even integer valued real.  */
14453         if (TREE_CODE (arg1) == REAL_CST)
14454           {
14455             REAL_VALUE_TYPE c;
14456             HOST_WIDE_INT n;
14457
14458             c = TREE_REAL_CST (arg1);
14459             n = real_to_integer (&c);
14460             if ((n & 1) == 0)
14461               {
14462                 REAL_VALUE_TYPE cint;
14463                 real_from_integer (&cint, VOIDmode, n,
14464                                    n < 0 ? -1 : 0, 0);
14465                 if (real_identical (&c, &cint))
14466                   return true;
14467               }
14468           }
14469         return tree_expr_nonnegative_warnv_p (arg0,
14470                                               strict_overflow_p);
14471
14472       default:
14473         break;
14474       }
14475   return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14476                                           type);
14477 }
14478
14479 /* Return true if T is known to be non-negative.  If the return
14480    value is based on the assumption that signed overflow is undefined,
14481    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14482    *STRICT_OVERFLOW_P.  */
14483
14484 bool
14485 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14486 {
14487   enum tree_code code = TREE_CODE (t);
14488   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14489     return true;
14490
14491   switch (code)
14492     {
14493     case TARGET_EXPR:
14494       {
14495         tree temp = TARGET_EXPR_SLOT (t);
14496         t = TARGET_EXPR_INITIAL (t);
14497
14498         /* If the initializer is non-void, then it's a normal expression
14499            that will be assigned to the slot.  */
14500         if (!VOID_TYPE_P (t))
14501           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14502
14503         /* Otherwise, the initializer sets the slot in some way.  One common
14504            way is an assignment statement at the end of the initializer.  */
14505         while (1)
14506           {
14507             if (TREE_CODE (t) == BIND_EXPR)
14508               t = expr_last (BIND_EXPR_BODY (t));
14509             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14510                      || TREE_CODE (t) == TRY_CATCH_EXPR)
14511               t = expr_last (TREE_OPERAND (t, 0));
14512             else if (TREE_CODE (t) == STATEMENT_LIST)
14513               t = expr_last (t);
14514             else
14515               break;
14516           }
14517         if (TREE_CODE (t) == MODIFY_EXPR
14518             && TREE_OPERAND (t, 0) == temp)
14519           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14520                                                 strict_overflow_p);
14521
14522         return false;
14523       }
14524
14525     case CALL_EXPR:
14526       {
14527         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
14528         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
14529
14530         return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14531                                               get_callee_fndecl (t),
14532                                               arg0,
14533                                               arg1,
14534                                               strict_overflow_p);
14535       }
14536     case COMPOUND_EXPR:
14537     case MODIFY_EXPR:
14538       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14539                                             strict_overflow_p);
14540     case BIND_EXPR:
14541       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14542                                             strict_overflow_p);
14543     case SAVE_EXPR:
14544       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14545                                             strict_overflow_p);
14546
14547     default:
14548       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14549                                                    TREE_TYPE (t));
14550     }
14551
14552   /* We don't know sign of `t', so be conservative and return false.  */
14553   return false;
14554 }
14555
14556 /* Return true if T is known to be non-negative.  If the return
14557    value is based on the assumption that signed overflow is undefined,
14558    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14559    *STRICT_OVERFLOW_P.  */
14560
14561 bool
14562 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14563 {
14564   enum tree_code code;
14565   if (t == error_mark_node)
14566     return false;
14567
14568   code = TREE_CODE (t);
14569   switch (TREE_CODE_CLASS (code))
14570     {
14571     case tcc_binary:
14572     case tcc_comparison:
14573       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14574                                               TREE_TYPE (t),
14575                                               TREE_OPERAND (t, 0),
14576                                               TREE_OPERAND (t, 1),
14577                                               strict_overflow_p);
14578
14579     case tcc_unary:
14580       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14581                                              TREE_TYPE (t),
14582                                              TREE_OPERAND (t, 0),
14583                                              strict_overflow_p);
14584
14585     case tcc_constant:
14586     case tcc_declaration:
14587     case tcc_reference:
14588       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14589
14590     default:
14591       break;
14592     }
14593
14594   switch (code)
14595     {
14596     case TRUTH_AND_EXPR:
14597     case TRUTH_OR_EXPR:
14598     case TRUTH_XOR_EXPR:
14599       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14600                                               TREE_TYPE (t),
14601                                               TREE_OPERAND (t, 0),
14602                                               TREE_OPERAND (t, 1),
14603                                               strict_overflow_p);
14604     case TRUTH_NOT_EXPR:
14605       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14606                                              TREE_TYPE (t),
14607                                              TREE_OPERAND (t, 0),
14608                                              strict_overflow_p);
14609
14610     case COND_EXPR:
14611     case CONSTRUCTOR:
14612     case OBJ_TYPE_REF:
14613     case ASSERT_EXPR:
14614     case ADDR_EXPR:
14615     case WITH_SIZE_EXPR:
14616     case SSA_NAME:
14617       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14618
14619     default:
14620       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14621     }
14622 }
14623
14624 /* Return true if `t' is known to be non-negative.  Handle warnings
14625    about undefined signed overflow.  */
14626
14627 bool
14628 tree_expr_nonnegative_p (tree t)
14629 {
14630   bool ret, strict_overflow_p;
14631
14632   strict_overflow_p = false;
14633   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14634   if (strict_overflow_p)
14635     fold_overflow_warning (("assuming signed overflow does not occur when "
14636                             "determining that expression is always "
14637                             "non-negative"),
14638                            WARN_STRICT_OVERFLOW_MISC);
14639   return ret;
14640 }
14641
14642
14643 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14644    For floating point we further ensure that T is not denormal.
14645    Similar logic is present in nonzero_address in rtlanal.h.
14646
14647    If the return value is based on the assumption that signed overflow
14648    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14649    change *STRICT_OVERFLOW_P.  */
14650
14651 bool
14652 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14653                                  bool *strict_overflow_p)
14654 {
14655   switch (code)
14656     {
14657     case ABS_EXPR:
14658       return tree_expr_nonzero_warnv_p (op0,
14659                                         strict_overflow_p);
14660
14661     case NOP_EXPR:
14662       {
14663         tree inner_type = TREE_TYPE (op0);
14664         tree outer_type = type;
14665
14666         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14667                 && tree_expr_nonzero_warnv_p (op0,
14668                                               strict_overflow_p));
14669       }
14670       break;
14671
14672     case NON_LVALUE_EXPR:
14673       return tree_expr_nonzero_warnv_p (op0,
14674                                         strict_overflow_p);
14675
14676     default:
14677       break;
14678   }
14679
14680   return false;
14681 }
14682
14683 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14684    For floating point we further ensure that T is not denormal.
14685    Similar logic is present in nonzero_address in rtlanal.h.
14686
14687    If the return value is based on the assumption that signed overflow
14688    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14689    change *STRICT_OVERFLOW_P.  */
14690
14691 bool
14692 tree_binary_nonzero_warnv_p (enum tree_code code,
14693                              tree type,
14694                              tree op0,
14695                              tree op1, bool *strict_overflow_p)
14696 {
14697   bool sub_strict_overflow_p;
14698   switch (code)
14699     {
14700     case POINTER_PLUS_EXPR:
14701     case PLUS_EXPR:
14702       if (TYPE_OVERFLOW_UNDEFINED (type))
14703         {
14704           /* With the presence of negative values it is hard
14705              to say something.  */
14706           sub_strict_overflow_p = false;
14707           if (!tree_expr_nonnegative_warnv_p (op0,
14708                                               &sub_strict_overflow_p)
14709               || !tree_expr_nonnegative_warnv_p (op1,
14710                                                  &sub_strict_overflow_p))
14711             return false;
14712           /* One of operands must be positive and the other non-negative.  */
14713           /* We don't set *STRICT_OVERFLOW_P here: even if this value
14714              overflows, on a twos-complement machine the sum of two
14715              nonnegative numbers can never be zero.  */
14716           return (tree_expr_nonzero_warnv_p (op0,
14717                                              strict_overflow_p)
14718                   || tree_expr_nonzero_warnv_p (op1,
14719                                                 strict_overflow_p));
14720         }
14721       break;
14722
14723     case MULT_EXPR:
14724       if (TYPE_OVERFLOW_UNDEFINED (type))
14725         {
14726           if (tree_expr_nonzero_warnv_p (op0,
14727                                          strict_overflow_p)
14728               && tree_expr_nonzero_warnv_p (op1,
14729                                             strict_overflow_p))
14730             {
14731               *strict_overflow_p = true;
14732               return true;
14733             }
14734         }
14735       break;
14736
14737     case MIN_EXPR:
14738       sub_strict_overflow_p = false;
14739       if (tree_expr_nonzero_warnv_p (op0,
14740                                      &sub_strict_overflow_p)
14741           && tree_expr_nonzero_warnv_p (op1,
14742                                         &sub_strict_overflow_p))
14743         {
14744           if (sub_strict_overflow_p)
14745             *strict_overflow_p = true;
14746         }
14747       break;
14748
14749     case MAX_EXPR:
14750       sub_strict_overflow_p = false;
14751       if (tree_expr_nonzero_warnv_p (op0,
14752                                      &sub_strict_overflow_p))
14753         {
14754           if (sub_strict_overflow_p)
14755             *strict_overflow_p = true;
14756
14757           /* When both operands are nonzero, then MAX must be too.  */
14758           if (tree_expr_nonzero_warnv_p (op1,
14759                                          strict_overflow_p))
14760             return true;
14761
14762           /* MAX where operand 0 is positive is positive.  */
14763           return tree_expr_nonnegative_warnv_p (op0,
14764                                                strict_overflow_p);
14765         }
14766       /* MAX where operand 1 is positive is positive.  */
14767       else if (tree_expr_nonzero_warnv_p (op1,
14768                                           &sub_strict_overflow_p)
14769                && tree_expr_nonnegative_warnv_p (op1,
14770                                                  &sub_strict_overflow_p))
14771         {
14772           if (sub_strict_overflow_p)
14773             *strict_overflow_p = true;
14774           return true;
14775         }
14776       break;
14777
14778     case BIT_IOR_EXPR:
14779       return (tree_expr_nonzero_warnv_p (op1,
14780                                          strict_overflow_p)
14781               || tree_expr_nonzero_warnv_p (op0,
14782                                             strict_overflow_p));
14783
14784     default:
14785       break;
14786   }
14787
14788   return false;
14789 }
14790
14791 /* Return true when T is an address and is known to be nonzero.
14792    For floating point we further ensure that T is not denormal.
14793    Similar logic is present in nonzero_address in rtlanal.h.
14794
14795    If the return value is based on the assumption that signed overflow
14796    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14797    change *STRICT_OVERFLOW_P.  */
14798
14799 bool
14800 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14801 {
14802   bool sub_strict_overflow_p;
14803   switch (TREE_CODE (t))
14804     {
14805     case INTEGER_CST:
14806       return !integer_zerop (t);
14807
14808     case ADDR_EXPR:
14809       {
14810         tree base = get_base_address (TREE_OPERAND (t, 0));
14811
14812         if (!base)
14813           return false;
14814
14815         /* Weak declarations may link to NULL.  Other things may also be NULL
14816            so protect with -fdelete-null-pointer-checks; but not variables
14817            allocated on the stack.  */
14818         if (DECL_P (base)
14819             && (flag_delete_null_pointer_checks
14820                 || (TREE_CODE (base) == VAR_DECL && !TREE_STATIC (base))))
14821           return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
14822
14823         /* Constants are never weak.  */
14824         if (CONSTANT_CLASS_P (base))
14825           return true;
14826
14827         return false;
14828       }
14829
14830     case COND_EXPR:
14831       sub_strict_overflow_p = false;
14832       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14833                                      &sub_strict_overflow_p)
14834           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
14835                                         &sub_strict_overflow_p))
14836         {
14837           if (sub_strict_overflow_p)
14838             *strict_overflow_p = true;
14839           return true;
14840         }
14841       break;
14842
14843     default:
14844       break;
14845     }
14846   return false;
14847 }
14848
14849 /* Return true when T is an address and is known to be nonzero.
14850    For floating point we further ensure that T is not denormal.
14851    Similar logic is present in nonzero_address in rtlanal.h.
14852
14853    If the return value is based on the assumption that signed overflow
14854    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14855    change *STRICT_OVERFLOW_P.  */
14856
14857 bool
14858 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14859 {
14860   tree type = TREE_TYPE (t);
14861   enum tree_code code;
14862
14863   /* Doing something useful for floating point would need more work.  */
14864   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14865     return false;
14866
14867   code = TREE_CODE (t);
14868   switch (TREE_CODE_CLASS (code))
14869     {
14870     case tcc_unary:
14871       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
14872                                               strict_overflow_p);
14873     case tcc_binary:
14874     case tcc_comparison:
14875       return tree_binary_nonzero_warnv_p (code, type,
14876                                                TREE_OPERAND (t, 0),
14877                                                TREE_OPERAND (t, 1),
14878                                                strict_overflow_p);
14879     case tcc_constant:
14880     case tcc_declaration:
14881     case tcc_reference:
14882       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
14883
14884     default:
14885       break;
14886     }
14887
14888   switch (code)
14889     {
14890     case TRUTH_NOT_EXPR:
14891       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
14892                                               strict_overflow_p);
14893
14894     case TRUTH_AND_EXPR:
14895     case TRUTH_OR_EXPR:
14896     case TRUTH_XOR_EXPR:
14897       return tree_binary_nonzero_warnv_p (code, type,
14898                                                TREE_OPERAND (t, 0),
14899                                                TREE_OPERAND (t, 1),
14900                                                strict_overflow_p);
14901
14902     case COND_EXPR:
14903     case CONSTRUCTOR:
14904     case OBJ_TYPE_REF:
14905     case ASSERT_EXPR:
14906     case ADDR_EXPR:
14907     case WITH_SIZE_EXPR:
14908     case SSA_NAME:
14909       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
14910
14911     case COMPOUND_EXPR:
14912     case MODIFY_EXPR:
14913     case BIND_EXPR:
14914       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14915                                         strict_overflow_p);
14916
14917     case SAVE_EXPR:
14918       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14919                                         strict_overflow_p);
14920
14921     case CALL_EXPR:
14922       return alloca_call_p (t);
14923
14924     default:
14925       break;
14926     }
14927   return false;
14928 }
14929
14930 /* Return true when T is an address and is known to be nonzero.
14931    Handle warnings about undefined signed overflow.  */
14932
14933 bool
14934 tree_expr_nonzero_p (tree t)
14935 {
14936   bool ret, strict_overflow_p;
14937
14938   strict_overflow_p = false;
14939   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
14940   if (strict_overflow_p)
14941     fold_overflow_warning (("assuming signed overflow does not occur when "
14942                             "determining that expression is always "
14943                             "non-zero"),
14944                            WARN_STRICT_OVERFLOW_MISC);
14945   return ret;
14946 }
14947
14948 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
14949    attempt to fold the expression to a constant without modifying TYPE,
14950    OP0 or OP1.
14951
14952    If the expression could be simplified to a constant, then return
14953    the constant.  If the expression would not be simplified to a
14954    constant, then return NULL_TREE.  */
14955
14956 tree
14957 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
14958 {
14959   tree tem = fold_binary (code, type, op0, op1);
14960   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14961 }
14962
14963 /* Given the components of a unary expression CODE, TYPE and OP0,
14964    attempt to fold the expression to a constant without modifying
14965    TYPE or OP0.
14966
14967    If the expression could be simplified to a constant, then return
14968    the constant.  If the expression would not be simplified to a
14969    constant, then return NULL_TREE.  */
14970
14971 tree
14972 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
14973 {
14974   tree tem = fold_unary (code, type, op0);
14975   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14976 }
14977
14978 /* If EXP represents referencing an element in a constant string
14979    (either via pointer arithmetic or array indexing), return the
14980    tree representing the value accessed, otherwise return NULL.  */
14981
14982 tree
14983 fold_read_from_constant_string (tree exp)
14984 {
14985   if ((TREE_CODE (exp) == INDIRECT_REF
14986        || TREE_CODE (exp) == ARRAY_REF)
14987       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
14988     {
14989       tree exp1 = TREE_OPERAND (exp, 0);
14990       tree index;
14991       tree string;
14992       location_t loc = EXPR_LOCATION (exp);
14993
14994       if (TREE_CODE (exp) == INDIRECT_REF)
14995         string = string_constant (exp1, &index);
14996       else
14997         {
14998           tree low_bound = array_ref_low_bound (exp);
14999           index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15000
15001           /* Optimize the special-case of a zero lower bound.
15002
15003              We convert the low_bound to sizetype to avoid some problems
15004              with constant folding.  (E.g. suppose the lower bound is 1,
15005              and its mode is QI.  Without the conversion,l (ARRAY
15006              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15007              +INDEX), which becomes (ARRAY+255+INDEX).  Oops!)  */
15008           if (! integer_zerop (low_bound))
15009             index = size_diffop_loc (loc, index,
15010                                  fold_convert_loc (loc, sizetype, low_bound));
15011
15012           string = exp1;
15013         }
15014
15015       if (string
15016           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15017           && TREE_CODE (string) == STRING_CST
15018           && TREE_CODE (index) == INTEGER_CST
15019           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15020           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15021               == MODE_INT)
15022           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15023         return build_int_cst_type (TREE_TYPE (exp),
15024                                    (TREE_STRING_POINTER (string)
15025                                     [TREE_INT_CST_LOW (index)]));
15026     }
15027   return NULL;
15028 }
15029
15030 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15031    an integer constant, real, or fixed-point constant.
15032
15033    TYPE is the type of the result.  */
15034
15035 static tree
15036 fold_negate_const (tree arg0, tree type)
15037 {
15038   tree t = NULL_TREE;
15039
15040   switch (TREE_CODE (arg0))
15041     {
15042     case INTEGER_CST:
15043       {
15044         unsigned HOST_WIDE_INT low;
15045         HOST_WIDE_INT high;
15046         int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15047                                    TREE_INT_CST_HIGH (arg0),
15048                                    &low, &high);
15049         t = force_fit_type_double (type, low, high, 1,
15050                                    (overflow | TREE_OVERFLOW (arg0))
15051                                    && !TYPE_UNSIGNED (type));
15052         break;
15053       }
15054
15055     case REAL_CST:
15056       t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15057       break;
15058
15059     case FIXED_CST:
15060       {
15061         FIXED_VALUE_TYPE f;
15062         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15063                                             &(TREE_FIXED_CST (arg0)), NULL,
15064                                             TYPE_SATURATING (type));
15065         t = build_fixed (type, f);
15066         /* Propagate overflow flags.  */
15067         if (overflow_p | TREE_OVERFLOW (arg0))
15068           TREE_OVERFLOW (t) = 1;
15069         break;
15070       }
15071
15072     default:
15073       gcc_unreachable ();
15074     }
15075
15076   return t;
15077 }
15078
15079 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15080    an integer constant or real constant.
15081
15082    TYPE is the type of the result.  */
15083
15084 tree
15085 fold_abs_const (tree arg0, tree type)
15086 {
15087   tree t = NULL_TREE;
15088
15089   switch (TREE_CODE (arg0))
15090     {
15091     case INTEGER_CST:
15092       /* If the value is unsigned, then the absolute value is
15093          the same as the ordinary value.  */
15094       if (TYPE_UNSIGNED (type))
15095         t = arg0;
15096       /* Similarly, if the value is non-negative.  */
15097       else if (INT_CST_LT (integer_minus_one_node, arg0))
15098         t = arg0;
15099       /* If the value is negative, then the absolute value is
15100          its negation.  */
15101       else
15102         {
15103           unsigned HOST_WIDE_INT low;
15104           HOST_WIDE_INT high;
15105           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15106                                      TREE_INT_CST_HIGH (arg0),
15107                                      &low, &high);
15108           t = force_fit_type_double (type, low, high, -1,
15109                                      overflow | TREE_OVERFLOW (arg0));
15110         }
15111       break;
15112
15113     case REAL_CST:
15114       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15115         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15116       else
15117         t =  arg0;
15118       break;
15119
15120     default:
15121       gcc_unreachable ();
15122     }
15123
15124   return t;
15125 }
15126
15127 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15128    constant.  TYPE is the type of the result.  */
15129
15130 static tree
15131 fold_not_const (tree arg0, tree type)
15132 {
15133   tree t = NULL_TREE;
15134
15135   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15136
15137   t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
15138                              ~TREE_INT_CST_HIGH (arg0), 0,
15139                              TREE_OVERFLOW (arg0));
15140
15141   return t;
15142 }
15143
15144 /* Given CODE, a relational operator, the target type, TYPE and two
15145    constant operands OP0 and OP1, return the result of the
15146    relational operation.  If the result is not a compile time
15147    constant, then return NULL_TREE.  */
15148
15149 static tree
15150 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15151 {
15152   int result, invert;
15153
15154   /* From here on, the only cases we handle are when the result is
15155      known to be a constant.  */
15156
15157   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15158     {
15159       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15160       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15161
15162       /* Handle the cases where either operand is a NaN.  */
15163       if (real_isnan (c0) || real_isnan (c1))
15164         {
15165           switch (code)
15166             {
15167             case EQ_EXPR:
15168             case ORDERED_EXPR:
15169               result = 0;
15170               break;
15171
15172             case NE_EXPR:
15173             case UNORDERED_EXPR:
15174             case UNLT_EXPR:
15175             case UNLE_EXPR:
15176             case UNGT_EXPR:
15177             case UNGE_EXPR:
15178             case UNEQ_EXPR:
15179               result = 1;
15180               break;
15181
15182             case LT_EXPR:
15183             case LE_EXPR:
15184             case GT_EXPR:
15185             case GE_EXPR:
15186             case LTGT_EXPR:
15187               if (flag_trapping_math)
15188                 return NULL_TREE;
15189               result = 0;
15190               break;
15191
15192             default:
15193               gcc_unreachable ();
15194             }
15195
15196           return constant_boolean_node (result, type);
15197         }
15198
15199       return constant_boolean_node (real_compare (code, c0, c1), type);
15200     }
15201
15202   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15203     {
15204       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15205       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15206       return constant_boolean_node (fixed_compare (code, c0, c1), type);
15207     }
15208
15209   /* Handle equality/inequality of complex constants.  */
15210   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15211     {
15212       tree rcond = fold_relational_const (code, type,
15213                                           TREE_REALPART (op0),
15214                                           TREE_REALPART (op1));
15215       tree icond = fold_relational_const (code, type,
15216                                           TREE_IMAGPART (op0),
15217                                           TREE_IMAGPART (op1));
15218       if (code == EQ_EXPR)
15219         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15220       else if (code == NE_EXPR)
15221         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15222       else
15223         return NULL_TREE;
15224     }
15225
15226   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15227
15228      To compute GT, swap the arguments and do LT.
15229      To compute GE, do LT and invert the result.
15230      To compute LE, swap the arguments, do LT and invert the result.
15231      To compute NE, do EQ and invert the result.
15232
15233      Therefore, the code below must handle only EQ and LT.  */
15234
15235   if (code == LE_EXPR || code == GT_EXPR)
15236     {
15237       tree tem = op0;
15238       op0 = op1;
15239       op1 = tem;
15240       code = swap_tree_comparison (code);
15241     }
15242
15243   /* Note that it is safe to invert for real values here because we
15244      have already handled the one case that it matters.  */
15245
15246   invert = 0;
15247   if (code == NE_EXPR || code == GE_EXPR)
15248     {
15249       invert = 1;
15250       code = invert_tree_comparison (code, false);
15251     }
15252
15253   /* Compute a result for LT or EQ if args permit;
15254      Otherwise return T.  */
15255   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15256     {
15257       if (code == EQ_EXPR)
15258         result = tree_int_cst_equal (op0, op1);
15259       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15260         result = INT_CST_LT_UNSIGNED (op0, op1);
15261       else
15262         result = INT_CST_LT (op0, op1);
15263     }
15264   else
15265     return NULL_TREE;
15266
15267   if (invert)
15268     result ^= 1;
15269   return constant_boolean_node (result, type);
15270 }
15271
15272 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15273    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
15274    itself.  */
15275
15276 tree
15277 fold_build_cleanup_point_expr (tree type, tree expr)
15278 {
15279   /* If the expression does not have side effects then we don't have to wrap
15280      it with a cleanup point expression.  */
15281   if (!TREE_SIDE_EFFECTS (expr))
15282     return expr;
15283
15284   /* If the expression is a return, check to see if the expression inside the
15285      return has no side effects or the right hand side of the modify expression
15286      inside the return. If either don't have side effects set we don't need to
15287      wrap the expression in a cleanup point expression.  Note we don't check the
15288      left hand side of the modify because it should always be a return decl.  */
15289   if (TREE_CODE (expr) == RETURN_EXPR)
15290     {
15291       tree op = TREE_OPERAND (expr, 0);
15292       if (!op || !TREE_SIDE_EFFECTS (op))
15293         return expr;
15294       op = TREE_OPERAND (op, 1);
15295       if (!TREE_SIDE_EFFECTS (op))
15296         return expr;
15297     }
15298
15299   return build1 (CLEANUP_POINT_EXPR, type, expr);
15300 }
15301
15302 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15303    of an indirection through OP0, or NULL_TREE if no simplification is
15304    possible.  */
15305
15306 tree
15307 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15308 {
15309   tree sub = op0;
15310   tree subtype;
15311
15312   STRIP_NOPS (sub);
15313   subtype = TREE_TYPE (sub);
15314   if (!POINTER_TYPE_P (subtype))
15315     return NULL_TREE;
15316
15317   if (TREE_CODE (sub) == ADDR_EXPR)
15318     {
15319       tree op = TREE_OPERAND (sub, 0);
15320       tree optype = TREE_TYPE (op);
15321       /* *&CONST_DECL -> to the value of the const decl.  */
15322       if (TREE_CODE (op) == CONST_DECL)
15323         return DECL_INITIAL (op);
15324       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
15325       if (type == optype)
15326         {
15327           tree fop = fold_read_from_constant_string (op);
15328           if (fop)
15329             return fop;
15330           else
15331             return op;
15332         }
15333       /* *(foo *)&fooarray => fooarray[0] */
15334       else if (TREE_CODE (optype) == ARRAY_TYPE
15335                && type == TREE_TYPE (optype))
15336         {
15337           tree type_domain = TYPE_DOMAIN (optype);
15338           tree min_val = size_zero_node;
15339           if (type_domain && TYPE_MIN_VALUE (type_domain))
15340             min_val = TYPE_MIN_VALUE (type_domain);
15341           op0 = build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15342           SET_EXPR_LOCATION (op0, loc);
15343           return op0;
15344         }
15345       /* *(foo *)&complexfoo => __real__ complexfoo */
15346       else if (TREE_CODE (optype) == COMPLEX_TYPE
15347                && type == TREE_TYPE (optype))
15348         return fold_build1_loc (loc, REALPART_EXPR, type, op);
15349       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15350       else if (TREE_CODE (optype) == VECTOR_TYPE
15351                && type == TREE_TYPE (optype))
15352         {
15353           tree part_width = TYPE_SIZE (type);
15354           tree index = bitsize_int (0);
15355           return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
15356         }
15357     }
15358
15359   /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15360   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15361       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15362     {
15363       tree op00 = TREE_OPERAND (sub, 0);
15364       tree op01 = TREE_OPERAND (sub, 1);
15365       tree op00type;
15366
15367       STRIP_NOPS (op00);
15368       op00type = TREE_TYPE (op00);
15369       if (TREE_CODE (op00) == ADDR_EXPR
15370           && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
15371           && type == TREE_TYPE (TREE_TYPE (op00type)))
15372         {
15373           HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15374           tree part_width = TYPE_SIZE (type);
15375           unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15376           unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15377           tree index = bitsize_int (indexi);
15378
15379           if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
15380             return fold_build3_loc (loc,
15381                                 BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
15382                                 part_width, index);
15383
15384         }
15385     }
15386
15387
15388   /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15389   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15390       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15391     {
15392       tree op00 = TREE_OPERAND (sub, 0);
15393       tree op01 = TREE_OPERAND (sub, 1);
15394       tree op00type;
15395
15396       STRIP_NOPS (op00);
15397       op00type = TREE_TYPE (op00);
15398       if (TREE_CODE (op00) == ADDR_EXPR
15399           && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
15400           && type == TREE_TYPE (TREE_TYPE (op00type)))
15401         {
15402           tree size = TYPE_SIZE_UNIT (type);
15403           if (tree_int_cst_equal (size, op01))
15404             return fold_build1_loc (loc, IMAGPART_EXPR, type,
15405                                 TREE_OPERAND (op00, 0));
15406         }
15407     }
15408
15409   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15410   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15411       && type == TREE_TYPE (TREE_TYPE (subtype)))
15412     {
15413       tree type_domain;
15414       tree min_val = size_zero_node;
15415       sub = build_fold_indirect_ref_loc (loc, sub);
15416       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15417       if (type_domain && TYPE_MIN_VALUE (type_domain))
15418         min_val = TYPE_MIN_VALUE (type_domain);
15419       op0 = build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15420       SET_EXPR_LOCATION (op0, loc);
15421       return op0;
15422     }
15423
15424   return NULL_TREE;
15425 }
15426
15427 /* Builds an expression for an indirection through T, simplifying some
15428    cases.  */
15429
15430 tree
15431 build_fold_indirect_ref_loc (location_t loc, tree t)
15432 {
15433   tree type = TREE_TYPE (TREE_TYPE (t));
15434   tree sub = fold_indirect_ref_1 (loc, type, t);
15435
15436   if (sub)
15437     return sub;
15438
15439   t = build1 (INDIRECT_REF, type, t);
15440   SET_EXPR_LOCATION (t, loc);
15441   return t;
15442 }
15443
15444 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
15445
15446 tree
15447 fold_indirect_ref_loc (location_t loc, tree t)
15448 {
15449   tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15450
15451   if (sub)
15452     return sub;
15453   else
15454     return t;
15455 }
15456
15457 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15458    whose result is ignored.  The type of the returned tree need not be
15459    the same as the original expression.  */
15460
15461 tree
15462 fold_ignored_result (tree t)
15463 {
15464   if (!TREE_SIDE_EFFECTS (t))
15465     return integer_zero_node;
15466
15467   for (;;)
15468     switch (TREE_CODE_CLASS (TREE_CODE (t)))
15469       {
15470       case tcc_unary:
15471         t = TREE_OPERAND (t, 0);
15472         break;
15473
15474       case tcc_binary:
15475       case tcc_comparison:
15476         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15477           t = TREE_OPERAND (t, 0);
15478         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15479           t = TREE_OPERAND (t, 1);
15480         else
15481           return t;
15482         break;
15483
15484       case tcc_expression:
15485         switch (TREE_CODE (t))
15486           {
15487           case COMPOUND_EXPR:
15488             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15489               return t;
15490             t = TREE_OPERAND (t, 0);
15491             break;
15492
15493           case COND_EXPR:
15494             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15495                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15496               return t;
15497             t = TREE_OPERAND (t, 0);
15498             break;
15499
15500           default:
15501             return t;
15502           }
15503         break;
15504
15505       default:
15506         return t;
15507       }
15508 }
15509
15510 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15511    This can only be applied to objects of a sizetype.  */
15512
15513 tree
15514 round_up_loc (location_t loc, tree value, int divisor)
15515 {
15516   tree div = NULL_TREE;
15517
15518   gcc_assert (divisor > 0);
15519   if (divisor == 1)
15520     return value;
15521
15522   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15523      have to do anything.  Only do this when we are not given a const,
15524      because in that case, this check is more expensive than just
15525      doing it.  */
15526   if (TREE_CODE (value) != INTEGER_CST)
15527     {
15528       div = build_int_cst (TREE_TYPE (value), divisor);
15529
15530       if (multiple_of_p (TREE_TYPE (value), value, div))
15531         return value;
15532     }
15533
15534   /* If divisor is a power of two, simplify this to bit manipulation.  */
15535   if (divisor == (divisor & -divisor))
15536     {
15537       if (TREE_CODE (value) == INTEGER_CST)
15538         {
15539           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
15540           unsigned HOST_WIDE_INT high;
15541           bool overflow_p;
15542
15543           if ((low & (divisor - 1)) == 0)
15544             return value;
15545
15546           overflow_p = TREE_OVERFLOW (value);
15547           high = TREE_INT_CST_HIGH (value);
15548           low &= ~(divisor - 1);
15549           low += divisor;
15550           if (low == 0)
15551             {
15552               high++;
15553               if (high == 0)
15554                 overflow_p = true;
15555             }
15556
15557           return force_fit_type_double (TREE_TYPE (value), low, high,
15558                                         -1, overflow_p);
15559         }
15560       else
15561         {
15562           tree t;
15563
15564           t = build_int_cst (TREE_TYPE (value), divisor - 1);
15565           value = size_binop_loc (loc, PLUS_EXPR, value, t);
15566           t = build_int_cst (TREE_TYPE (value), -divisor);
15567           value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15568         }
15569     }
15570   else
15571     {
15572       if (!div)
15573         div = build_int_cst (TREE_TYPE (value), divisor);
15574       value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
15575       value = size_binop_loc (loc, MULT_EXPR, value, div);
15576     }
15577
15578   return value;
15579 }
15580
15581 /* Likewise, but round down.  */
15582
15583 tree
15584 round_down_loc (location_t loc, tree value, int divisor)
15585 {
15586   tree div = NULL_TREE;
15587
15588   gcc_assert (divisor > 0);
15589   if (divisor == 1)
15590     return value;
15591
15592   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15593      have to do anything.  Only do this when we are not given a const,
15594      because in that case, this check is more expensive than just
15595      doing it.  */
15596   if (TREE_CODE (value) != INTEGER_CST)
15597     {
15598       div = build_int_cst (TREE_TYPE (value), divisor);
15599
15600       if (multiple_of_p (TREE_TYPE (value), value, div))
15601         return value;
15602     }
15603
15604   /* If divisor is a power of two, simplify this to bit manipulation.  */
15605   if (divisor == (divisor & -divisor))
15606     {
15607       tree t;
15608
15609       t = build_int_cst (TREE_TYPE (value), -divisor);
15610       value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15611     }
15612   else
15613     {
15614       if (!div)
15615         div = build_int_cst (TREE_TYPE (value), divisor);
15616       value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
15617       value = size_binop_loc (loc, MULT_EXPR, value, div);
15618     }
15619
15620   return value;
15621 }
15622
15623 /* Returns the pointer to the base of the object addressed by EXP and
15624    extracts the information about the offset of the access, storing it
15625    to PBITPOS and POFFSET.  */
15626
15627 static tree
15628 split_address_to_core_and_offset (tree exp,
15629                                   HOST_WIDE_INT *pbitpos, tree *poffset)
15630 {
15631   tree core;
15632   enum machine_mode mode;
15633   int unsignedp, volatilep;
15634   HOST_WIDE_INT bitsize;
15635   location_t loc = EXPR_LOCATION (exp);
15636
15637   if (TREE_CODE (exp) == ADDR_EXPR)
15638     {
15639       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15640                                   poffset, &mode, &unsignedp, &volatilep,
15641                                   false);
15642       core = build_fold_addr_expr_loc (loc, core);
15643     }
15644   else
15645     {
15646       core = exp;
15647       *pbitpos = 0;
15648       *poffset = NULL_TREE;
15649     }
15650
15651   return core;
15652 }
15653
15654 /* Returns true if addresses of E1 and E2 differ by a constant, false
15655    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
15656
15657 bool
15658 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15659 {
15660   tree core1, core2;
15661   HOST_WIDE_INT bitpos1, bitpos2;
15662   tree toffset1, toffset2, tdiff, type;
15663
15664   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15665   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15666
15667   if (bitpos1 % BITS_PER_UNIT != 0
15668       || bitpos2 % BITS_PER_UNIT != 0
15669       || !operand_equal_p (core1, core2, 0))
15670     return false;
15671
15672   if (toffset1 && toffset2)
15673     {
15674       type = TREE_TYPE (toffset1);
15675       if (type != TREE_TYPE (toffset2))
15676         toffset2 = fold_convert (type, toffset2);
15677
15678       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15679       if (!cst_and_fits_in_hwi (tdiff))
15680         return false;
15681
15682       *diff = int_cst_value (tdiff);
15683     }
15684   else if (toffset1 || toffset2)
15685     {
15686       /* If only one of the offsets is non-constant, the difference cannot
15687          be a constant.  */
15688       return false;
15689     }
15690   else
15691     *diff = 0;
15692
15693   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15694   return true;
15695 }
15696
15697 /* Simplify the floating point expression EXP when the sign of the
15698    result is not significant.  Return NULL_TREE if no simplification
15699    is possible.  */
15700
15701 tree
15702 fold_strip_sign_ops (tree exp)
15703 {
15704   tree arg0, arg1;
15705   location_t loc = EXPR_LOCATION (exp);
15706
15707   switch (TREE_CODE (exp))
15708     {
15709     case ABS_EXPR:
15710     case NEGATE_EXPR:
15711       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15712       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15713
15714     case MULT_EXPR:
15715     case RDIV_EXPR:
15716       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15717         return NULL_TREE;
15718       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15719       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15720       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15721         return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
15722                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
15723                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
15724       break;
15725
15726     case COMPOUND_EXPR:
15727       arg0 = TREE_OPERAND (exp, 0);
15728       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15729       if (arg1)
15730         return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15731       break;
15732
15733     case COND_EXPR:
15734       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15735       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15736       if (arg0 || arg1)
15737         return fold_build3_loc (loc,
15738                             COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15739                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
15740                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
15741       break;
15742
15743     case CALL_EXPR:
15744       {
15745         const enum built_in_function fcode = builtin_mathfn_code (exp);
15746         switch (fcode)
15747         {
15748         CASE_FLT_FN (BUILT_IN_COPYSIGN):
15749           /* Strip copysign function call, return the 1st argument. */
15750           arg0 = CALL_EXPR_ARG (exp, 0);
15751           arg1 = CALL_EXPR_ARG (exp, 1);
15752           return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
15753
15754         default:
15755           /* Strip sign ops from the argument of "odd" math functions.  */
15756           if (negate_mathfn_p (fcode))
15757             {
15758               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15759               if (arg0)
15760                 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
15761             }
15762           break;
15763         }
15764       }
15765       break;
15766
15767     default:
15768       break;
15769     }
15770   return NULL_TREE;
15771 }