OSDN Git Service

* config/avr/avr.h (PREFERRED_RELOAD_CLASS): Remove.
[pf3gnuchains/gcc-fork.git] / gcc / fold-const.c
1 /* Fold a constant sub-tree into a single node for C-compiler
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /*@@ This file should be rewritten to use an arbitrary precision
23   @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
24   @@ Perhaps the routines could also be used for bc/dc, and made a lib.
25   @@ The routines that translate from the ap rep should
26   @@ warn if precision et. al. is lost.
27   @@ This would also make life easier when this technology is used
28   @@ for cross-compilers.  */
29
30 /* The entry points in this file are fold, size_int_wide and size_binop.
31
32    fold takes a tree as argument and returns a simplified tree.
33
34    size_binop takes a tree code for an arithmetic operation
35    and two operands that are trees, and produces a tree for the
36    result, assuming the type comes from `sizetype'.
37
38    size_int takes an integer value, and creates a tree constant
39    with type from `sizetype'.
40
41    Note: Since the folders get called on non-gimple code as well as
42    gimple code, we need to handle GIMPLE tuples as well as their
43    corresponding tree equivalents.  */
44
45 #include "config.h"
46 #include "system.h"
47 #include "coretypes.h"
48 #include "tm.h"
49 #include "flags.h"
50 #include "tree.h"
51 #include "realmpfr.h"
52 #include "rtl.h"
53 #include "expr.h"
54 #include "tm_p.h"
55 #include "target.h"
56 #include "diagnostic-core.h"
57 #include "toplev.h"
58 #include "intl.h"
59 #include "ggc.h"
60 #include "hashtab.h"
61 #include "langhooks.h"
62 #include "md5.h"
63 #include "gimple.h"
64 #include "tree-flow.h"
65
66 /* Nonzero if we are folding constants inside an initializer; zero
67    otherwise.  */
68 int folding_initializer = 0;
69
70 /* The following constants represent a bit based encoding of GCC's
71    comparison operators.  This encoding simplifies transformations
72    on relational comparison operators, such as AND and OR.  */
73 enum comparison_code {
74   COMPCODE_FALSE = 0,
75   COMPCODE_LT = 1,
76   COMPCODE_EQ = 2,
77   COMPCODE_LE = 3,
78   COMPCODE_GT = 4,
79   COMPCODE_LTGT = 5,
80   COMPCODE_GE = 6,
81   COMPCODE_ORD = 7,
82   COMPCODE_UNORD = 8,
83   COMPCODE_UNLT = 9,
84   COMPCODE_UNEQ = 10,
85   COMPCODE_UNLE = 11,
86   COMPCODE_UNGT = 12,
87   COMPCODE_NE = 13,
88   COMPCODE_UNGE = 14,
89   COMPCODE_TRUE = 15
90 };
91
92 static bool negate_mathfn_p (enum built_in_function);
93 static bool negate_expr_p (tree);
94 static tree negate_expr (tree);
95 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
96 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
97 static tree const_binop (enum tree_code, tree, tree);
98 static enum comparison_code comparison_to_compcode (enum tree_code);
99 static enum tree_code compcode_to_comparison (enum comparison_code);
100 static int operand_equal_for_comparison_p (tree, tree, tree);
101 static int twoval_comparison_p (tree, tree *, tree *, int *);
102 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
103 static tree pedantic_omit_one_operand_loc (location_t, tree, tree, tree);
104 static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree);
105 static tree make_bit_field_ref (location_t, tree, tree,
106                                 HOST_WIDE_INT, HOST_WIDE_INT, int);
107 static tree optimize_bit_field_compare (location_t, enum tree_code,
108                                         tree, tree, tree);
109 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
110                                     HOST_WIDE_INT *,
111                                     enum machine_mode *, int *, int *,
112                                     tree *, tree *);
113 static int all_ones_mask_p (const_tree, int);
114 static tree sign_bit_p (tree, const_tree);
115 static int simple_operand_p (const_tree);
116 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
117 static tree range_predecessor (tree);
118 static tree range_successor (tree);
119 extern tree make_range (tree, int *, tree *, tree *, bool *);
120 extern bool merge_ranges (int *, tree *, tree *, int, tree, tree, int,
121                           tree, tree);
122 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
123 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
124 static tree unextend (tree, int, int, tree);
125 static tree fold_truthop (location_t, enum tree_code, tree, tree, tree);
126 static tree optimize_minmax_comparison (location_t, enum tree_code,
127                                         tree, tree, tree);
128 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
129 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
130 static tree fold_binary_op_with_conditional_arg (location_t,
131                                                  enum tree_code, tree,
132                                                  tree, tree,
133                                                  tree, tree, int);
134 static tree fold_mathfn_compare (location_t,
135                                  enum built_in_function, enum tree_code,
136                                  tree, tree, tree);
137 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
138 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
139 static bool reorder_operands_p (const_tree, const_tree);
140 static tree fold_negate_const (tree, tree);
141 static tree fold_not_const (const_tree, tree);
142 static tree fold_relational_const (enum tree_code, tree, tree, tree);
143 static tree fold_convert_const (enum tree_code, tree, tree);
144
145
146 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
147    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
148    and SUM1.  Then this yields nonzero if overflow occurred during the
149    addition.
150
151    Overflow occurs if A and B have the same sign, but A and SUM differ in
152    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
153    sign.  */
154 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
155 \f
156 /* If ARG2 divides ARG1 with zero remainder, carries out the division
157    of type CODE and returns the quotient.
158    Otherwise returns NULL_TREE.  */
159
160 tree
161 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
162 {
163   double_int quo, rem;
164   int uns;
165
166   /* The sign of the division is according to operand two, that
167      does the correct thing for POINTER_PLUS_EXPR where we want
168      a signed division.  */
169   uns = TYPE_UNSIGNED (TREE_TYPE (arg2));
170   if (TREE_CODE (TREE_TYPE (arg2)) == INTEGER_TYPE
171       && TYPE_IS_SIZETYPE (TREE_TYPE (arg2)))
172     uns = false;
173
174   quo = double_int_divmod (tree_to_double_int (arg1),
175                            tree_to_double_int (arg2),
176                            uns, code, &rem);
177
178   if (double_int_zero_p (rem))
179     return build_int_cst_wide (TREE_TYPE (arg1), quo.low, quo.high);
180
181   return NULL_TREE; 
182 }
183 \f
184 /* This is nonzero if we should defer warnings about undefined
185    overflow.  This facility exists because these warnings are a
186    special case.  The code to estimate loop iterations does not want
187    to issue any warnings, since it works with expressions which do not
188    occur in user code.  Various bits of cleanup code call fold(), but
189    only use the result if it has certain characteristics (e.g., is a
190    constant); that code only wants to issue a warning if the result is
191    used.  */
192
193 static int fold_deferring_overflow_warnings;
194
195 /* If a warning about undefined overflow is deferred, this is the
196    warning.  Note that this may cause us to turn two warnings into
197    one, but that is fine since it is sufficient to only give one
198    warning per expression.  */
199
200 static const char* fold_deferred_overflow_warning;
201
202 /* If a warning about undefined overflow is deferred, this is the
203    level at which the warning should be emitted.  */
204
205 static enum warn_strict_overflow_code fold_deferred_overflow_code;
206
207 /* Start deferring overflow warnings.  We could use a stack here to
208    permit nested calls, but at present it is not necessary.  */
209
210 void
211 fold_defer_overflow_warnings (void)
212 {
213   ++fold_deferring_overflow_warnings;
214 }
215
216 /* Stop deferring overflow warnings.  If there is a pending warning,
217    and ISSUE is true, then issue the warning if appropriate.  STMT is
218    the statement with which the warning should be associated (used for
219    location information); STMT may be NULL.  CODE is the level of the
220    warning--a warn_strict_overflow_code value.  This function will use
221    the smaller of CODE and the deferred code when deciding whether to
222    issue the warning.  CODE may be zero to mean to always use the
223    deferred code.  */
224
225 void
226 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
227 {
228   const char *warnmsg;
229   location_t locus;
230
231   gcc_assert (fold_deferring_overflow_warnings > 0);
232   --fold_deferring_overflow_warnings;
233   if (fold_deferring_overflow_warnings > 0)
234     {
235       if (fold_deferred_overflow_warning != NULL
236           && code != 0
237           && code < (int) fold_deferred_overflow_code)
238         fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
239       return;
240     }
241
242   warnmsg = fold_deferred_overflow_warning;
243   fold_deferred_overflow_warning = NULL;
244
245   if (!issue || warnmsg == NULL)
246     return;
247
248   if (gimple_no_warning_p (stmt))
249     return;
250
251   /* Use the smallest code level when deciding to issue the
252      warning.  */
253   if (code == 0 || code > (int) fold_deferred_overflow_code)
254     code = fold_deferred_overflow_code;
255
256   if (!issue_strict_overflow_warning (code))
257     return;
258
259   if (stmt == NULL)
260     locus = input_location;
261   else
262     locus = gimple_location (stmt);
263   warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
264 }
265
266 /* Stop deferring overflow warnings, ignoring any deferred
267    warnings.  */
268
269 void
270 fold_undefer_and_ignore_overflow_warnings (void)
271 {
272   fold_undefer_overflow_warnings (false, NULL, 0);
273 }
274
275 /* Whether we are deferring overflow warnings.  */
276
277 bool
278 fold_deferring_overflow_warnings_p (void)
279 {
280   return fold_deferring_overflow_warnings > 0;
281 }
282
283 /* This is called when we fold something based on the fact that signed
284    overflow is undefined.  */
285
286 static void
287 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
288 {
289   if (fold_deferring_overflow_warnings > 0)
290     {
291       if (fold_deferred_overflow_warning == NULL
292           || wc < fold_deferred_overflow_code)
293         {
294           fold_deferred_overflow_warning = gmsgid;
295           fold_deferred_overflow_code = wc;
296         }
297     }
298   else if (issue_strict_overflow_warning (wc))
299     warning (OPT_Wstrict_overflow, gmsgid);
300 }
301 \f
302 /* Return true if the built-in mathematical function specified by CODE
303    is odd, i.e. -f(x) == f(-x).  */
304
305 static bool
306 negate_mathfn_p (enum built_in_function code)
307 {
308   switch (code)
309     {
310     CASE_FLT_FN (BUILT_IN_ASIN):
311     CASE_FLT_FN (BUILT_IN_ASINH):
312     CASE_FLT_FN (BUILT_IN_ATAN):
313     CASE_FLT_FN (BUILT_IN_ATANH):
314     CASE_FLT_FN (BUILT_IN_CASIN):
315     CASE_FLT_FN (BUILT_IN_CASINH):
316     CASE_FLT_FN (BUILT_IN_CATAN):
317     CASE_FLT_FN (BUILT_IN_CATANH):
318     CASE_FLT_FN (BUILT_IN_CBRT):
319     CASE_FLT_FN (BUILT_IN_CPROJ):
320     CASE_FLT_FN (BUILT_IN_CSIN):
321     CASE_FLT_FN (BUILT_IN_CSINH):
322     CASE_FLT_FN (BUILT_IN_CTAN):
323     CASE_FLT_FN (BUILT_IN_CTANH):
324     CASE_FLT_FN (BUILT_IN_ERF):
325     CASE_FLT_FN (BUILT_IN_LLROUND):
326     CASE_FLT_FN (BUILT_IN_LROUND):
327     CASE_FLT_FN (BUILT_IN_ROUND):
328     CASE_FLT_FN (BUILT_IN_SIN):
329     CASE_FLT_FN (BUILT_IN_SINH):
330     CASE_FLT_FN (BUILT_IN_TAN):
331     CASE_FLT_FN (BUILT_IN_TANH):
332     CASE_FLT_FN (BUILT_IN_TRUNC):
333       return true;
334
335     CASE_FLT_FN (BUILT_IN_LLRINT):
336     CASE_FLT_FN (BUILT_IN_LRINT):
337     CASE_FLT_FN (BUILT_IN_NEARBYINT):
338     CASE_FLT_FN (BUILT_IN_RINT):
339       return !flag_rounding_math;
340
341     default:
342       break;
343     }
344   return false;
345 }
346
347 /* Check whether we may negate an integer constant T without causing
348    overflow.  */
349
350 bool
351 may_negate_without_overflow_p (const_tree t)
352 {
353   unsigned HOST_WIDE_INT val;
354   unsigned int prec;
355   tree type;
356
357   gcc_assert (TREE_CODE (t) == INTEGER_CST);
358
359   type = TREE_TYPE (t);
360   if (TYPE_UNSIGNED (type))
361     return false;
362
363   prec = TYPE_PRECISION (type);
364   if (prec > HOST_BITS_PER_WIDE_INT)
365     {
366       if (TREE_INT_CST_LOW (t) != 0)
367         return true;
368       prec -= HOST_BITS_PER_WIDE_INT;
369       val = TREE_INT_CST_HIGH (t);
370     }
371   else
372     val = TREE_INT_CST_LOW (t);
373   if (prec < HOST_BITS_PER_WIDE_INT)
374     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
375   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
376 }
377
378 /* Determine whether an expression T can be cheaply negated using
379    the function negate_expr without introducing undefined overflow.  */
380
381 static bool
382 negate_expr_p (tree t)
383 {
384   tree type;
385
386   if (t == 0)
387     return false;
388
389   type = TREE_TYPE (t);
390
391   STRIP_SIGN_NOPS (t);
392   switch (TREE_CODE (t))
393     {
394     case INTEGER_CST:
395       if (TYPE_OVERFLOW_WRAPS (type))
396         return true;
397
398       /* Check that -CST will not overflow type.  */
399       return may_negate_without_overflow_p (t);
400     case BIT_NOT_EXPR:
401       return (INTEGRAL_TYPE_P (type)
402               && TYPE_OVERFLOW_WRAPS (type));
403
404     case FIXED_CST:
405     case NEGATE_EXPR:
406       return true;
407
408     case REAL_CST:
409       /* We want to canonicalize to positive real constants.  Pretend
410          that only negative ones can be easily negated.  */
411       return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
412
413     case COMPLEX_CST:
414       return negate_expr_p (TREE_REALPART (t))
415              && negate_expr_p (TREE_IMAGPART (t));
416
417     case COMPLEX_EXPR:
418       return negate_expr_p (TREE_OPERAND (t, 0))
419              && negate_expr_p (TREE_OPERAND (t, 1));
420
421     case CONJ_EXPR:
422       return negate_expr_p (TREE_OPERAND (t, 0));
423
424     case PLUS_EXPR:
425       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
426           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
427         return false;
428       /* -(A + B) -> (-B) - A.  */
429       if (negate_expr_p (TREE_OPERAND (t, 1))
430           && reorder_operands_p (TREE_OPERAND (t, 0),
431                                  TREE_OPERAND (t, 1)))
432         return true;
433       /* -(A + B) -> (-A) - B.  */
434       return negate_expr_p (TREE_OPERAND (t, 0));
435
436     case MINUS_EXPR:
437       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
438       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
439              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
440              && reorder_operands_p (TREE_OPERAND (t, 0),
441                                     TREE_OPERAND (t, 1));
442
443     case MULT_EXPR:
444       if (TYPE_UNSIGNED (TREE_TYPE (t)))
445         break;
446
447       /* Fall through.  */
448
449     case RDIV_EXPR:
450       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
451         return negate_expr_p (TREE_OPERAND (t, 1))
452                || negate_expr_p (TREE_OPERAND (t, 0));
453       break;
454
455     case TRUNC_DIV_EXPR:
456     case ROUND_DIV_EXPR:
457     case FLOOR_DIV_EXPR:
458     case CEIL_DIV_EXPR:
459     case EXACT_DIV_EXPR:
460       /* In general we can't negate A / B, because if A is INT_MIN and
461          B is 1, we may turn this into INT_MIN / -1 which is undefined
462          and actually traps on some architectures.  But if overflow is
463          undefined, we can negate, because - (INT_MIN / 1) is an
464          overflow.  */
465       if (INTEGRAL_TYPE_P (TREE_TYPE (t))
466           && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
467         break;
468       return negate_expr_p (TREE_OPERAND (t, 1))
469              || negate_expr_p (TREE_OPERAND (t, 0));
470
471     case NOP_EXPR:
472       /* Negate -((double)float) as (double)(-float).  */
473       if (TREE_CODE (type) == REAL_TYPE)
474         {
475           tree tem = strip_float_extensions (t);
476           if (tem != t)
477             return negate_expr_p (tem);
478         }
479       break;
480
481     case CALL_EXPR:
482       /* Negate -f(x) as f(-x).  */
483       if (negate_mathfn_p (builtin_mathfn_code (t)))
484         return negate_expr_p (CALL_EXPR_ARG (t, 0));
485       break;
486
487     case RSHIFT_EXPR:
488       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
489       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
490         {
491           tree op1 = TREE_OPERAND (t, 1);
492           if (TREE_INT_CST_HIGH (op1) == 0
493               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
494                  == TREE_INT_CST_LOW (op1))
495             return true;
496         }
497       break;
498
499     default:
500       break;
501     }
502   return false;
503 }
504
505 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
506    simplification is possible.
507    If negate_expr_p would return true for T, NULL_TREE will never be
508    returned.  */
509
510 static tree
511 fold_negate_expr (location_t loc, tree t)
512 {
513   tree type = TREE_TYPE (t);
514   tree tem;
515
516   switch (TREE_CODE (t))
517     {
518     /* Convert - (~A) to A + 1.  */
519     case BIT_NOT_EXPR:
520       if (INTEGRAL_TYPE_P (type))
521         return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
522                             build_int_cst (type, 1));
523       break;
524
525     case INTEGER_CST:
526       tem = fold_negate_const (t, type);
527       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
528           || !TYPE_OVERFLOW_TRAPS (type))
529         return tem;
530       break;
531
532     case REAL_CST:
533       tem = fold_negate_const (t, type);
534       /* Two's complement FP formats, such as c4x, may overflow.  */
535       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
536         return tem;
537       break;
538
539     case FIXED_CST:
540       tem = fold_negate_const (t, type);
541       return tem;
542
543     case COMPLEX_CST:
544       {
545         tree rpart = negate_expr (TREE_REALPART (t));
546         tree ipart = negate_expr (TREE_IMAGPART (t));
547
548         if ((TREE_CODE (rpart) == REAL_CST
549              && TREE_CODE (ipart) == REAL_CST)
550             || (TREE_CODE (rpart) == INTEGER_CST
551                 && TREE_CODE (ipart) == INTEGER_CST))
552           return build_complex (type, rpart, ipart);
553       }
554       break;
555
556     case COMPLEX_EXPR:
557       if (negate_expr_p (t))
558         return fold_build2_loc (loc, COMPLEX_EXPR, type,
559                             fold_negate_expr (loc, TREE_OPERAND (t, 0)),
560                             fold_negate_expr (loc, TREE_OPERAND (t, 1)));
561       break;
562
563     case CONJ_EXPR:
564       if (negate_expr_p (t))
565         return fold_build1_loc (loc, CONJ_EXPR, type,
566                             fold_negate_expr (loc, TREE_OPERAND (t, 0)));
567       break;
568
569     case NEGATE_EXPR:
570       return TREE_OPERAND (t, 0);
571
572     case PLUS_EXPR:
573       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
574           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
575         {
576           /* -(A + B) -> (-B) - A.  */
577           if (negate_expr_p (TREE_OPERAND (t, 1))
578               && reorder_operands_p (TREE_OPERAND (t, 0),
579                                      TREE_OPERAND (t, 1)))
580             {
581               tem = negate_expr (TREE_OPERAND (t, 1));
582               return fold_build2_loc (loc, MINUS_EXPR, type,
583                                   tem, TREE_OPERAND (t, 0));
584             }
585
586           /* -(A + B) -> (-A) - B.  */
587           if (negate_expr_p (TREE_OPERAND (t, 0)))
588             {
589               tem = negate_expr (TREE_OPERAND (t, 0));
590               return fold_build2_loc (loc, MINUS_EXPR, type,
591                                   tem, TREE_OPERAND (t, 1));
592             }
593         }
594       break;
595
596     case MINUS_EXPR:
597       /* - (A - B) -> B - A  */
598       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
599           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
600           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
601         return fold_build2_loc (loc, MINUS_EXPR, type,
602                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
603       break;
604
605     case MULT_EXPR:
606       if (TYPE_UNSIGNED (type))
607         break;
608
609       /* Fall through.  */
610
611     case RDIV_EXPR:
612       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
613         {
614           tem = TREE_OPERAND (t, 1);
615           if (negate_expr_p (tem))
616             return fold_build2_loc (loc, TREE_CODE (t), type,
617                                 TREE_OPERAND (t, 0), negate_expr (tem));
618           tem = TREE_OPERAND (t, 0);
619           if (negate_expr_p (tem))
620             return fold_build2_loc (loc, TREE_CODE (t), type,
621                                 negate_expr (tem), TREE_OPERAND (t, 1));
622         }
623       break;
624
625     case TRUNC_DIV_EXPR:
626     case ROUND_DIV_EXPR:
627     case FLOOR_DIV_EXPR:
628     case CEIL_DIV_EXPR:
629     case EXACT_DIV_EXPR:
630       /* In general we can't negate A / B, because if A is INT_MIN and
631          B is 1, we may turn this into INT_MIN / -1 which is undefined
632          and actually traps on some architectures.  But if overflow is
633          undefined, we can negate, because - (INT_MIN / 1) is an
634          overflow.  */
635       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
636         {
637           const char * const warnmsg = G_("assuming signed overflow does not "
638                                           "occur when negating a division");
639           tem = TREE_OPERAND (t, 1);
640           if (negate_expr_p (tem))
641             {
642               if (INTEGRAL_TYPE_P (type)
643                   && (TREE_CODE (tem) != INTEGER_CST
644                       || integer_onep (tem)))
645                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
646               return fold_build2_loc (loc, TREE_CODE (t), type,
647                                   TREE_OPERAND (t, 0), negate_expr (tem));
648             }
649           tem = TREE_OPERAND (t, 0);
650           if (negate_expr_p (tem))
651             {
652               if (INTEGRAL_TYPE_P (type)
653                   && (TREE_CODE (tem) != INTEGER_CST
654                       || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
655                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
656               return fold_build2_loc (loc, TREE_CODE (t), type,
657                                   negate_expr (tem), TREE_OPERAND (t, 1));
658             }
659         }
660       break;
661
662     case NOP_EXPR:
663       /* Convert -((double)float) into (double)(-float).  */
664       if (TREE_CODE (type) == REAL_TYPE)
665         {
666           tem = strip_float_extensions (t);
667           if (tem != t && negate_expr_p (tem))
668             return fold_convert_loc (loc, type, negate_expr (tem));
669         }
670       break;
671
672     case CALL_EXPR:
673       /* Negate -f(x) as f(-x).  */
674       if (negate_mathfn_p (builtin_mathfn_code (t))
675           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
676         {
677           tree fndecl, arg;
678
679           fndecl = get_callee_fndecl (t);
680           arg = negate_expr (CALL_EXPR_ARG (t, 0));
681           return build_call_expr_loc (loc, fndecl, 1, arg);
682         }
683       break;
684
685     case RSHIFT_EXPR:
686       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
687       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
688         {
689           tree op1 = TREE_OPERAND (t, 1);
690           if (TREE_INT_CST_HIGH (op1) == 0
691               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
692                  == TREE_INT_CST_LOW (op1))
693             {
694               tree ntype = TYPE_UNSIGNED (type)
695                            ? signed_type_for (type)
696                            : unsigned_type_for (type);
697               tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
698               temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
699               return fold_convert_loc (loc, type, temp);
700             }
701         }
702       break;
703
704     default:
705       break;
706     }
707
708   return NULL_TREE;
709 }
710
711 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
712    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
713    return NULL_TREE. */
714
715 static tree
716 negate_expr (tree t)
717 {
718   tree type, tem;
719   location_t loc;
720
721   if (t == NULL_TREE)
722     return NULL_TREE;
723
724   loc = EXPR_LOCATION (t);
725   type = TREE_TYPE (t);
726   STRIP_SIGN_NOPS (t);
727
728   tem = fold_negate_expr (loc, t);
729   if (!tem)
730     {
731       tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
732       SET_EXPR_LOCATION (tem, loc);
733     }
734   return fold_convert_loc (loc, type, tem);
735 }
736 \f
737 /* Split a tree IN into a constant, literal and variable parts that could be
738    combined with CODE to make IN.  "constant" means an expression with
739    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
740    commutative arithmetic operation.  Store the constant part into *CONP,
741    the literal in *LITP and return the variable part.  If a part isn't
742    present, set it to null.  If the tree does not decompose in this way,
743    return the entire tree as the variable part and the other parts as null.
744
745    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
746    case, we negate an operand that was subtracted.  Except if it is a
747    literal for which we use *MINUS_LITP instead.
748
749    If NEGATE_P is true, we are negating all of IN, again except a literal
750    for which we use *MINUS_LITP instead.
751
752    If IN is itself a literal or constant, return it as appropriate.
753
754    Note that we do not guarantee that any of the three values will be the
755    same type as IN, but they will have the same signedness and mode.  */
756
757 static tree
758 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
759             tree *minus_litp, int negate_p)
760 {
761   tree var = 0;
762
763   *conp = 0;
764   *litp = 0;
765   *minus_litp = 0;
766
767   /* Strip any conversions that don't change the machine mode or signedness.  */
768   STRIP_SIGN_NOPS (in);
769
770   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
771       || TREE_CODE (in) == FIXED_CST)
772     *litp = in;
773   else if (TREE_CODE (in) == code
774            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
775                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
776                /* We can associate addition and subtraction together (even
777                   though the C standard doesn't say so) for integers because
778                   the value is not affected.  For reals, the value might be
779                   affected, so we can't.  */
780                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
781                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
782     {
783       tree op0 = TREE_OPERAND (in, 0);
784       tree op1 = TREE_OPERAND (in, 1);
785       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
786       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
787
788       /* First see if either of the operands is a literal, then a constant.  */
789       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
790           || TREE_CODE (op0) == FIXED_CST)
791         *litp = op0, op0 = 0;
792       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
793                || TREE_CODE (op1) == FIXED_CST)
794         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
795
796       if (op0 != 0 && TREE_CONSTANT (op0))
797         *conp = op0, op0 = 0;
798       else if (op1 != 0 && TREE_CONSTANT (op1))
799         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
800
801       /* If we haven't dealt with either operand, this is not a case we can
802          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
803       if (op0 != 0 && op1 != 0)
804         var = in;
805       else if (op0 != 0)
806         var = op0;
807       else
808         var = op1, neg_var_p = neg1_p;
809
810       /* Now do any needed negations.  */
811       if (neg_litp_p)
812         *minus_litp = *litp, *litp = 0;
813       if (neg_conp_p)
814         *conp = negate_expr (*conp);
815       if (neg_var_p)
816         var = negate_expr (var);
817     }
818   else if (TREE_CONSTANT (in))
819     *conp = in;
820   else
821     var = in;
822
823   if (negate_p)
824     {
825       if (*litp)
826         *minus_litp = *litp, *litp = 0;
827       else if (*minus_litp)
828         *litp = *minus_litp, *minus_litp = 0;
829       *conp = negate_expr (*conp);
830       var = negate_expr (var);
831     }
832
833   return var;
834 }
835
836 /* Re-associate trees split by the above function.  T1 and T2 are
837    either expressions to associate or null.  Return the new
838    expression, if any.  LOC is the location of the new expression.  If
839    we build an operation, do it in TYPE and with CODE.  */
840
841 static tree
842 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
843 {
844   tree tem;
845
846   if (t1 == 0)
847     return t2;
848   else if (t2 == 0)
849     return t1;
850
851   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
852      try to fold this since we will have infinite recursion.  But do
853      deal with any NEGATE_EXPRs.  */
854   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
855       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
856     {
857       if (code == PLUS_EXPR)
858         {
859           if (TREE_CODE (t1) == NEGATE_EXPR)
860             tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t2),
861                           fold_convert_loc (loc, type, TREE_OPERAND (t1, 0)));
862           else if (TREE_CODE (t2) == NEGATE_EXPR)
863             tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t1),
864                           fold_convert_loc (loc, type, TREE_OPERAND (t2, 0)));
865           else if (integer_zerop (t2))
866             return fold_convert_loc (loc, type, t1);
867         }
868       else if (code == MINUS_EXPR)
869         {
870           if (integer_zerop (t2))
871             return fold_convert_loc (loc, type, t1);
872         }
873
874       tem = build2 (code, type, fold_convert_loc (loc, type, t1),
875                     fold_convert_loc (loc, type, t2));
876       goto associate_trees_exit;
877     }
878
879   return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
880                       fold_convert_loc (loc, type, t2));
881  associate_trees_exit:
882   protected_set_expr_location (tem, loc);
883   return tem;
884 }
885 \f
886 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
887    for use in int_const_binop, size_binop and size_diffop.  */
888
889 static bool
890 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
891 {
892   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
893     return false;
894   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
895     return false;
896
897   switch (code)
898     {
899     case LSHIFT_EXPR:
900     case RSHIFT_EXPR:
901     case LROTATE_EXPR:
902     case RROTATE_EXPR:
903       return true;
904
905     default:
906       break;
907     }
908
909   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
910          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
911          && TYPE_MODE (type1) == TYPE_MODE (type2);
912 }
913
914
915 /* Combine two integer constants ARG1 and ARG2 under operation CODE
916    to produce a new constant.  Return NULL_TREE if we don't know how
917    to evaluate CODE at compile-time.
918
919    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
920
921 tree
922 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
923 {
924   double_int op1, op2, res, tmp;
925   tree t;
926   tree type = TREE_TYPE (arg1);
927   bool uns = TYPE_UNSIGNED (type);
928   bool is_sizetype
929     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
930   bool overflow = false;
931
932   op1 = tree_to_double_int (arg1);
933   op2 = tree_to_double_int (arg2);
934
935   switch (code)
936     {
937     case BIT_IOR_EXPR:
938       res = double_int_ior (op1, op2);
939       break;
940
941     case BIT_XOR_EXPR:
942       res = double_int_xor (op1, op2);
943       break;
944
945     case BIT_AND_EXPR:
946       res = double_int_and (op1, op2);
947       break;
948
949     case RSHIFT_EXPR:
950       res = double_int_rshift (op1, double_int_to_shwi (op2),
951                                TYPE_PRECISION (type), !uns);
952       break;
953
954     case LSHIFT_EXPR:
955       /* It's unclear from the C standard whether shifts can overflow.
956          The following code ignores overflow; perhaps a C standard
957          interpretation ruling is needed.  */
958       res = double_int_lshift (op1, double_int_to_shwi (op2),
959                                TYPE_PRECISION (type), !uns);
960       break;
961
962     case RROTATE_EXPR:
963       res = double_int_rrotate (op1, double_int_to_shwi (op2),
964                                 TYPE_PRECISION (type));
965       break;
966
967     case LROTATE_EXPR:
968       res = double_int_lrotate (op1, double_int_to_shwi (op2),
969                                 TYPE_PRECISION (type));
970       break;
971
972     case PLUS_EXPR:
973       overflow = add_double (op1.low, op1.high, op2.low, op2.high,
974                              &res.low, &res.high);
975       break;
976
977     case MINUS_EXPR:
978       neg_double (op2.low, op2.high, &res.low, &res.high);
979       add_double (op1.low, op1.high, res.low, res.high,
980                   &res.low, &res.high);
981       overflow = OVERFLOW_SUM_SIGN (res.high, op2.high, op1.high);
982       break;
983
984     case MULT_EXPR:
985       overflow = mul_double (op1.low, op1.high, op2.low, op2.high,
986                              &res.low, &res.high);
987       break;
988
989     case TRUNC_DIV_EXPR:
990     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
991     case EXACT_DIV_EXPR:
992       /* This is a shortcut for a common special case.  */
993       if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
994           && !TREE_OVERFLOW (arg1)
995           && !TREE_OVERFLOW (arg2)
996           && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
997         {
998           if (code == CEIL_DIV_EXPR)
999             op1.low += op2.low - 1;
1000
1001           res.low = op1.low / op2.low, res.high = 0;
1002           break;
1003         }
1004
1005       /* ... fall through ...  */
1006
1007     case ROUND_DIV_EXPR:
1008       if (double_int_zero_p (op2))
1009         return NULL_TREE;
1010       if (double_int_one_p (op2))
1011         {
1012           res = op1;
1013           break;
1014         }
1015       if (double_int_equal_p (op1, op2)
1016           && ! double_int_zero_p (op1))
1017         {
1018           res = double_int_one;
1019           break;
1020         }
1021       overflow = div_and_round_double (code, uns,
1022                                        op1.low, op1.high, op2.low, op2.high,
1023                                        &res.low, &res.high,
1024                                        &tmp.low, &tmp.high);
1025       break;
1026
1027     case TRUNC_MOD_EXPR:
1028     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1029       /* This is a shortcut for a common special case.  */
1030       if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1031           && !TREE_OVERFLOW (arg1)
1032           && !TREE_OVERFLOW (arg2)
1033           && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1034         {
1035           if (code == CEIL_MOD_EXPR)
1036             op1.low += op2.low - 1;
1037           res.low = op1.low % op2.low, res.high = 0;
1038           break;
1039         }
1040
1041       /* ... fall through ...  */
1042
1043     case ROUND_MOD_EXPR:
1044       if (double_int_zero_p (op2))
1045         return NULL_TREE;
1046       overflow = div_and_round_double (code, uns,
1047                                        op1.low, op1.high, op2.low, op2.high,
1048                                        &tmp.low, &tmp.high,
1049                                        &res.low, &res.high);
1050       break;
1051
1052     case MIN_EXPR:
1053       res = double_int_min (op1, op2, uns);
1054       break;
1055
1056     case MAX_EXPR:
1057       res = double_int_max (op1, op2, uns);
1058       break;
1059
1060     default:
1061       return NULL_TREE;
1062     }
1063
1064   if (notrunc)
1065     {
1066       t = build_int_cst_wide (TREE_TYPE (arg1), res.low, res.high);
1067
1068       /* Propagate overflow flags ourselves.  */
1069       if (((!uns || is_sizetype) && overflow)
1070           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1071         {
1072           t = copy_node (t);
1073           TREE_OVERFLOW (t) = 1;
1074         }
1075     }
1076   else
1077     t = force_fit_type_double (TREE_TYPE (arg1), res, 1,
1078                                ((!uns || is_sizetype) && overflow)
1079                                | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1080
1081   return t;
1082 }
1083
1084 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1085    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1086    are the same kind of constant and the same machine mode.  Return zero if
1087    combining the constants is not allowed in the current operating mode.  */
1088
1089 static tree
1090 const_binop (enum tree_code code, tree arg1, tree arg2)
1091 {
1092   /* Sanity check for the recursive cases.  */
1093   if (!arg1 || !arg2)
1094     return NULL_TREE;
1095
1096   STRIP_NOPS (arg1);
1097   STRIP_NOPS (arg2);
1098
1099   if (TREE_CODE (arg1) == INTEGER_CST)
1100     return int_const_binop (code, arg1, arg2, 0);
1101
1102   if (TREE_CODE (arg1) == REAL_CST)
1103     {
1104       enum machine_mode mode;
1105       REAL_VALUE_TYPE d1;
1106       REAL_VALUE_TYPE d2;
1107       REAL_VALUE_TYPE value;
1108       REAL_VALUE_TYPE result;
1109       bool inexact;
1110       tree t, type;
1111
1112       /* The following codes are handled by real_arithmetic.  */
1113       switch (code)
1114         {
1115         case PLUS_EXPR:
1116         case MINUS_EXPR:
1117         case MULT_EXPR:
1118         case RDIV_EXPR:
1119         case MIN_EXPR:
1120         case MAX_EXPR:
1121           break;
1122
1123         default:
1124           return NULL_TREE;
1125         }
1126
1127       d1 = TREE_REAL_CST (arg1);
1128       d2 = TREE_REAL_CST (arg2);
1129
1130       type = TREE_TYPE (arg1);
1131       mode = TYPE_MODE (type);
1132
1133       /* Don't perform operation if we honor signaling NaNs and
1134          either operand is a NaN.  */
1135       if (HONOR_SNANS (mode)
1136           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1137         return NULL_TREE;
1138
1139       /* Don't perform operation if it would raise a division
1140          by zero exception.  */
1141       if (code == RDIV_EXPR
1142           && REAL_VALUES_EQUAL (d2, dconst0)
1143           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1144         return NULL_TREE;
1145
1146       /* If either operand is a NaN, just return it.  Otherwise, set up
1147          for floating-point trap; we return an overflow.  */
1148       if (REAL_VALUE_ISNAN (d1))
1149         return arg1;
1150       else if (REAL_VALUE_ISNAN (d2))
1151         return arg2;
1152
1153       inexact = real_arithmetic (&value, code, &d1, &d2);
1154       real_convert (&result, mode, &value);
1155
1156       /* Don't constant fold this floating point operation if
1157          the result has overflowed and flag_trapping_math.  */
1158       if (flag_trapping_math
1159           && MODE_HAS_INFINITIES (mode)
1160           && REAL_VALUE_ISINF (result)
1161           && !REAL_VALUE_ISINF (d1)
1162           && !REAL_VALUE_ISINF (d2))
1163         return NULL_TREE;
1164
1165       /* Don't constant fold this floating point operation if the
1166          result may dependent upon the run-time rounding mode and
1167          flag_rounding_math is set, or if GCC's software emulation
1168          is unable to accurately represent the result.  */
1169       if ((flag_rounding_math
1170            || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1171           && (inexact || !real_identical (&result, &value)))
1172         return NULL_TREE;
1173
1174       t = build_real (type, result);
1175
1176       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1177       return t;
1178     }
1179
1180   if (TREE_CODE (arg1) == FIXED_CST)
1181     {
1182       FIXED_VALUE_TYPE f1;
1183       FIXED_VALUE_TYPE f2;
1184       FIXED_VALUE_TYPE result;
1185       tree t, type;
1186       int sat_p;
1187       bool overflow_p;
1188
1189       /* The following codes are handled by fixed_arithmetic.  */
1190       switch (code)
1191         {
1192         case PLUS_EXPR:
1193         case MINUS_EXPR:
1194         case MULT_EXPR:
1195         case TRUNC_DIV_EXPR:
1196           f2 = TREE_FIXED_CST (arg2);
1197           break;
1198
1199         case LSHIFT_EXPR:
1200         case RSHIFT_EXPR:
1201           f2.data.high = TREE_INT_CST_HIGH (arg2);
1202           f2.data.low = TREE_INT_CST_LOW (arg2);
1203           f2.mode = SImode;
1204           break;
1205
1206         default:
1207           return NULL_TREE;
1208         }
1209
1210       f1 = TREE_FIXED_CST (arg1);
1211       type = TREE_TYPE (arg1);
1212       sat_p = TYPE_SATURATING (type);
1213       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1214       t = build_fixed (type, result);
1215       /* Propagate overflow flags.  */
1216       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1217         TREE_OVERFLOW (t) = 1;
1218       return t;
1219     }
1220
1221   if (TREE_CODE (arg1) == COMPLEX_CST)
1222     {
1223       tree type = TREE_TYPE (arg1);
1224       tree r1 = TREE_REALPART (arg1);
1225       tree i1 = TREE_IMAGPART (arg1);
1226       tree r2 = TREE_REALPART (arg2);
1227       tree i2 = TREE_IMAGPART (arg2);
1228       tree real, imag;
1229
1230       switch (code)
1231         {
1232         case PLUS_EXPR:
1233         case MINUS_EXPR:
1234           real = const_binop (code, r1, r2);
1235           imag = const_binop (code, i1, i2);
1236           break;
1237
1238         case MULT_EXPR:
1239           if (COMPLEX_FLOAT_TYPE_P (type))
1240             return do_mpc_arg2 (arg1, arg2, type,
1241                                 /* do_nonfinite= */ folding_initializer,
1242                                 mpc_mul);
1243
1244           real = const_binop (MINUS_EXPR,
1245                               const_binop (MULT_EXPR, r1, r2),
1246                               const_binop (MULT_EXPR, i1, i2));
1247           imag = const_binop (PLUS_EXPR,
1248                               const_binop (MULT_EXPR, r1, i2),
1249                               const_binop (MULT_EXPR, i1, r2));
1250           break;
1251
1252         case RDIV_EXPR:
1253           if (COMPLEX_FLOAT_TYPE_P (type))
1254             return do_mpc_arg2 (arg1, arg2, type,
1255                                 /* do_nonfinite= */ folding_initializer,
1256                                 mpc_div);
1257           /* Fallthru ... */
1258         case TRUNC_DIV_EXPR:
1259         case CEIL_DIV_EXPR:
1260         case FLOOR_DIV_EXPR:
1261         case ROUND_DIV_EXPR:
1262           if (flag_complex_method == 0)
1263           {
1264             /* Keep this algorithm in sync with
1265                tree-complex.c:expand_complex_div_straight().
1266
1267                Expand complex division to scalars, straightforward algorithm.
1268                a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1269                t = br*br + bi*bi
1270             */
1271             tree magsquared
1272               = const_binop (PLUS_EXPR,
1273                              const_binop (MULT_EXPR, r2, r2),
1274                              const_binop (MULT_EXPR, i2, i2));
1275             tree t1
1276               = const_binop (PLUS_EXPR,
1277                              const_binop (MULT_EXPR, r1, r2),
1278                              const_binop (MULT_EXPR, i1, i2));
1279             tree t2
1280               = const_binop (MINUS_EXPR,
1281                              const_binop (MULT_EXPR, i1, r2),
1282                              const_binop (MULT_EXPR, r1, i2));
1283
1284             real = const_binop (code, t1, magsquared);
1285             imag = const_binop (code, t2, magsquared);
1286           }
1287           else
1288           {
1289             /* Keep this algorithm in sync with
1290                tree-complex.c:expand_complex_div_wide().
1291
1292                Expand complex division to scalars, modified algorithm to minimize
1293                overflow with wide input ranges.  */
1294             tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1295                                         fold_abs_const (r2, TREE_TYPE (type)),
1296                                         fold_abs_const (i2, TREE_TYPE (type)));
1297
1298             if (integer_nonzerop (compare))
1299               {
1300                 /* In the TRUE branch, we compute
1301                    ratio = br/bi;
1302                    div = (br * ratio) + bi;
1303                    tr = (ar * ratio) + ai;
1304                    ti = (ai * ratio) - ar;
1305                    tr = tr / div;
1306                    ti = ti / div;  */
1307                 tree ratio = const_binop (code, r2, i2);
1308                 tree div = const_binop (PLUS_EXPR, i2,
1309                                         const_binop (MULT_EXPR, r2, ratio));
1310                 real = const_binop (MULT_EXPR, r1, ratio);
1311                 real = const_binop (PLUS_EXPR, real, i1);
1312                 real = const_binop (code, real, div);
1313
1314                 imag = const_binop (MULT_EXPR, i1, ratio);
1315                 imag = const_binop (MINUS_EXPR, imag, r1);
1316                 imag = const_binop (code, imag, div);
1317               }
1318             else
1319               {
1320                 /* In the FALSE branch, we compute
1321                    ratio = d/c;
1322                    divisor = (d * ratio) + c;
1323                    tr = (b * ratio) + a;
1324                    ti = b - (a * ratio);
1325                    tr = tr / div;
1326                    ti = ti / div;  */
1327                 tree ratio = const_binop (code, i2, r2);
1328                 tree div = const_binop (PLUS_EXPR, r2,
1329                                         const_binop (MULT_EXPR, i2, ratio));
1330
1331                 real = const_binop (MULT_EXPR, i1, ratio);
1332                 real = const_binop (PLUS_EXPR, real, r1);
1333                 real = const_binop (code, real, div);
1334
1335                 imag = const_binop (MULT_EXPR, r1, ratio);
1336                 imag = const_binop (MINUS_EXPR, i1, imag);
1337                 imag = const_binop (code, imag, div);
1338               }
1339           }
1340           break;
1341
1342         default:
1343           return NULL_TREE;
1344         }
1345
1346       if (real && imag)
1347         return build_complex (type, real, imag);
1348     }
1349
1350   if (TREE_CODE (arg1) == VECTOR_CST)
1351     {
1352       tree type = TREE_TYPE(arg1);
1353       int count = TYPE_VECTOR_SUBPARTS (type), i;
1354       tree elements1, elements2, list = NULL_TREE;
1355
1356       if(TREE_CODE(arg2) != VECTOR_CST)
1357         return NULL_TREE;
1358
1359       elements1 = TREE_VECTOR_CST_ELTS (arg1);
1360       elements2 = TREE_VECTOR_CST_ELTS (arg2);
1361
1362       for (i = 0; i < count; i++)
1363         {
1364           tree elem1, elem2, elem;
1365
1366           /* The trailing elements can be empty and should be treated as 0 */
1367           if(!elements1)
1368             elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1369           else
1370             {
1371               elem1 = TREE_VALUE(elements1);
1372               elements1 = TREE_CHAIN (elements1);
1373             }
1374
1375           if(!elements2)
1376             elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1377           else
1378             {
1379               elem2 = TREE_VALUE(elements2);
1380               elements2 = TREE_CHAIN (elements2);
1381             }
1382
1383           elem = const_binop (code, elem1, elem2);
1384
1385           /* It is possible that const_binop cannot handle the given
1386             code and return NULL_TREE */
1387           if(elem == NULL_TREE)
1388             return NULL_TREE;
1389
1390           list = tree_cons (NULL_TREE, elem, list);
1391         }
1392       return build_vector(type, nreverse(list));
1393     }
1394   return NULL_TREE;
1395 }
1396
1397 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
1398    indicates which particular sizetype to create.  */
1399
1400 tree
1401 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1402 {
1403   return build_int_cst (sizetype_tab[(int) kind], number);
1404 }
1405 \f
1406 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
1407    is a tree code.  The type of the result is taken from the operands.
1408    Both must be equivalent integer types, ala int_binop_types_match_p.
1409    If the operands are constant, so is the result.  */
1410
1411 tree
1412 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1413 {
1414   tree type = TREE_TYPE (arg0);
1415
1416   if (arg0 == error_mark_node || arg1 == error_mark_node)
1417     return error_mark_node;
1418
1419   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1420                                        TREE_TYPE (arg1)));
1421
1422   /* Handle the special case of two integer constants faster.  */
1423   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1424     {
1425       /* And some specific cases even faster than that.  */
1426       if (code == PLUS_EXPR)
1427         {
1428           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1429             return arg1;
1430           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1431             return arg0;
1432         }
1433       else if (code == MINUS_EXPR)
1434         {
1435           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1436             return arg0;
1437         }
1438       else if (code == MULT_EXPR)
1439         {
1440           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1441             return arg1;
1442         }
1443
1444       /* Handle general case of two integer constants.  */
1445       return int_const_binop (code, arg0, arg1, 0);
1446     }
1447
1448   return fold_build2_loc (loc, code, type, arg0, arg1);
1449 }
1450
1451 /* Given two values, either both of sizetype or both of bitsizetype,
1452    compute the difference between the two values.  Return the value
1453    in signed type corresponding to the type of the operands.  */
1454
1455 tree
1456 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1457 {
1458   tree type = TREE_TYPE (arg0);
1459   tree ctype;
1460
1461   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1462                                        TREE_TYPE (arg1)));
1463
1464   /* If the type is already signed, just do the simple thing.  */
1465   if (!TYPE_UNSIGNED (type))
1466     return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1467
1468   if (type == sizetype)
1469     ctype = ssizetype;
1470   else if (type == bitsizetype)
1471     ctype = sbitsizetype;
1472   else
1473     ctype = signed_type_for (type);
1474
1475   /* If either operand is not a constant, do the conversions to the signed
1476      type and subtract.  The hardware will do the right thing with any
1477      overflow in the subtraction.  */
1478   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1479     return size_binop_loc (loc, MINUS_EXPR,
1480                            fold_convert_loc (loc, ctype, arg0),
1481                            fold_convert_loc (loc, ctype, arg1));
1482
1483   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1484      Otherwise, subtract the other way, convert to CTYPE (we know that can't
1485      overflow) and negate (which can't either).  Special-case a result
1486      of zero while we're here.  */
1487   if (tree_int_cst_equal (arg0, arg1))
1488     return build_int_cst (ctype, 0);
1489   else if (tree_int_cst_lt (arg1, arg0))
1490     return fold_convert_loc (loc, ctype,
1491                              size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1492   else
1493     return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1494                            fold_convert_loc (loc, ctype,
1495                                              size_binop_loc (loc,
1496                                                              MINUS_EXPR,
1497                                                              arg1, arg0)));
1498 }
1499 \f
1500 /* A subroutine of fold_convert_const handling conversions of an
1501    INTEGER_CST to another integer type.  */
1502
1503 static tree
1504 fold_convert_const_int_from_int (tree type, const_tree arg1)
1505 {
1506   tree t;
1507
1508   /* Given an integer constant, make new constant with new type,
1509      appropriately sign-extended or truncated.  */
1510   t = force_fit_type_double (type, tree_to_double_int (arg1),
1511                              !POINTER_TYPE_P (TREE_TYPE (arg1)),
1512                              (TREE_INT_CST_HIGH (arg1) < 0
1513                               && (TYPE_UNSIGNED (type)
1514                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1515                              | TREE_OVERFLOW (arg1));
1516
1517   return t;
1518 }
1519
1520 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1521    to an integer type.  */
1522
1523 static tree
1524 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1525 {
1526   int overflow = 0;
1527   tree t;
1528
1529   /* The following code implements the floating point to integer
1530      conversion rules required by the Java Language Specification,
1531      that IEEE NaNs are mapped to zero and values that overflow
1532      the target precision saturate, i.e. values greater than
1533      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1534      are mapped to INT_MIN.  These semantics are allowed by the
1535      C and C++ standards that simply state that the behavior of
1536      FP-to-integer conversion is unspecified upon overflow.  */
1537
1538   double_int val;
1539   REAL_VALUE_TYPE r;
1540   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1541
1542   switch (code)
1543     {
1544     case FIX_TRUNC_EXPR:
1545       real_trunc (&r, VOIDmode, &x);
1546       break;
1547
1548     default:
1549       gcc_unreachable ();
1550     }
1551
1552   /* If R is NaN, return zero and show we have an overflow.  */
1553   if (REAL_VALUE_ISNAN (r))
1554     {
1555       overflow = 1;
1556       val = double_int_zero;
1557     }
1558
1559   /* See if R is less than the lower bound or greater than the
1560      upper bound.  */
1561
1562   if (! overflow)
1563     {
1564       tree lt = TYPE_MIN_VALUE (type);
1565       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1566       if (REAL_VALUES_LESS (r, l))
1567         {
1568           overflow = 1;
1569           val = tree_to_double_int (lt);
1570         }
1571     }
1572
1573   if (! overflow)
1574     {
1575       tree ut = TYPE_MAX_VALUE (type);
1576       if (ut)
1577         {
1578           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1579           if (REAL_VALUES_LESS (u, r))
1580             {
1581               overflow = 1;
1582               val = tree_to_double_int (ut);
1583             }
1584         }
1585     }
1586
1587   if (! overflow)
1588     real_to_integer2 ((HOST_WIDE_INT *) &val.low, &val.high, &r);
1589
1590   t = force_fit_type_double (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1591   return t;
1592 }
1593
1594 /* A subroutine of fold_convert_const handling conversions of a
1595    FIXED_CST to an integer type.  */
1596
1597 static tree
1598 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1599 {
1600   tree t;
1601   double_int temp, temp_trunc;
1602   unsigned int mode;
1603
1604   /* Right shift FIXED_CST to temp by fbit.  */
1605   temp = TREE_FIXED_CST (arg1).data;
1606   mode = TREE_FIXED_CST (arg1).mode;
1607   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
1608     {
1609       temp = double_int_rshift (temp, GET_MODE_FBIT (mode),
1610                                 HOST_BITS_PER_DOUBLE_INT,
1611                                 SIGNED_FIXED_POINT_MODE_P (mode));
1612
1613       /* Left shift temp to temp_trunc by fbit.  */
1614       temp_trunc = double_int_lshift (temp, GET_MODE_FBIT (mode),
1615                                       HOST_BITS_PER_DOUBLE_INT,
1616                                       SIGNED_FIXED_POINT_MODE_P (mode));
1617     }
1618   else
1619     {
1620       temp = double_int_zero;
1621       temp_trunc = double_int_zero;
1622     }
1623
1624   /* If FIXED_CST is negative, we need to round the value toward 0.
1625      By checking if the fractional bits are not zero to add 1 to temp.  */
1626   if (SIGNED_FIXED_POINT_MODE_P (mode)
1627       && double_int_negative_p (temp_trunc)
1628       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
1629     temp = double_int_add (temp, double_int_one);
1630
1631   /* Given a fixed-point constant, make new constant with new type,
1632      appropriately sign-extended or truncated.  */
1633   t = force_fit_type_double (type, temp, -1,
1634                              (double_int_negative_p (temp)
1635                               && (TYPE_UNSIGNED (type)
1636                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1637                              | TREE_OVERFLOW (arg1));
1638
1639   return t;
1640 }
1641
1642 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1643    to another floating point type.  */
1644
1645 static tree
1646 fold_convert_const_real_from_real (tree type, const_tree arg1)
1647 {
1648   REAL_VALUE_TYPE value;
1649   tree t;
1650
1651   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1652   t = build_real (type, value);
1653
1654   /* If converting an infinity or NAN to a representation that doesn't
1655      have one, set the overflow bit so that we can produce some kind of
1656      error message at the appropriate point if necessary.  It's not the
1657      most user-friendly message, but it's better than nothing.  */
1658   if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1659       && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1660     TREE_OVERFLOW (t) = 1;
1661   else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1662            && !MODE_HAS_NANS (TYPE_MODE (type)))
1663     TREE_OVERFLOW (t) = 1;
1664   /* Regular overflow, conversion produced an infinity in a mode that
1665      can't represent them.  */
1666   else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1667            && REAL_VALUE_ISINF (value)
1668            && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1669     TREE_OVERFLOW (t) = 1;
1670   else
1671     TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1672   return t;
1673 }
1674
1675 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1676    to a floating point type.  */
1677
1678 static tree
1679 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
1680 {
1681   REAL_VALUE_TYPE value;
1682   tree t;
1683
1684   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
1685   t = build_real (type, value);
1686
1687   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1688   return t;
1689 }
1690
1691 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1692    to another fixed-point type.  */
1693
1694 static tree
1695 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
1696 {
1697   FIXED_VALUE_TYPE value;
1698   tree t;
1699   bool overflow_p;
1700
1701   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
1702                               TYPE_SATURATING (type));
1703   t = build_fixed (type, value);
1704
1705   /* Propagate overflow flags.  */
1706   if (overflow_p | TREE_OVERFLOW (arg1))
1707     TREE_OVERFLOW (t) = 1;
1708   return t;
1709 }
1710
1711 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
1712    to a fixed-point type.  */
1713
1714 static tree
1715 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
1716 {
1717   FIXED_VALUE_TYPE value;
1718   tree t;
1719   bool overflow_p;
1720
1721   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
1722                                        TREE_INT_CST (arg1),
1723                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
1724                                        TYPE_SATURATING (type));
1725   t = build_fixed (type, value);
1726
1727   /* Propagate overflow flags.  */
1728   if (overflow_p | TREE_OVERFLOW (arg1))
1729     TREE_OVERFLOW (t) = 1;
1730   return t;
1731 }
1732
1733 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1734    to a fixed-point type.  */
1735
1736 static tree
1737 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
1738 {
1739   FIXED_VALUE_TYPE value;
1740   tree t;
1741   bool overflow_p;
1742
1743   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
1744                                         &TREE_REAL_CST (arg1),
1745                                         TYPE_SATURATING (type));
1746   t = build_fixed (type, value);
1747
1748   /* Propagate overflow flags.  */
1749   if (overflow_p | TREE_OVERFLOW (arg1))
1750     TREE_OVERFLOW (t) = 1;
1751   return t;
1752 }
1753
1754 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1755    type TYPE.  If no simplification can be done return NULL_TREE.  */
1756
1757 static tree
1758 fold_convert_const (enum tree_code code, tree type, tree arg1)
1759 {
1760   if (TREE_TYPE (arg1) == type)
1761     return arg1;
1762
1763   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
1764       || TREE_CODE (type) == OFFSET_TYPE)
1765     {
1766       if (TREE_CODE (arg1) == INTEGER_CST)
1767         return fold_convert_const_int_from_int (type, arg1);
1768       else if (TREE_CODE (arg1) == REAL_CST)
1769         return fold_convert_const_int_from_real (code, type, arg1);
1770       else if (TREE_CODE (arg1) == FIXED_CST)
1771         return fold_convert_const_int_from_fixed (type, arg1);
1772     }
1773   else if (TREE_CODE (type) == REAL_TYPE)
1774     {
1775       if (TREE_CODE (arg1) == INTEGER_CST)
1776         return build_real_from_int_cst (type, arg1);
1777       else if (TREE_CODE (arg1) == REAL_CST)
1778         return fold_convert_const_real_from_real (type, arg1);
1779       else if (TREE_CODE (arg1) == FIXED_CST)
1780         return fold_convert_const_real_from_fixed (type, arg1);
1781     }
1782   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
1783     {
1784       if (TREE_CODE (arg1) == FIXED_CST)
1785         return fold_convert_const_fixed_from_fixed (type, arg1);
1786       else if (TREE_CODE (arg1) == INTEGER_CST)
1787         return fold_convert_const_fixed_from_int (type, arg1);
1788       else if (TREE_CODE (arg1) == REAL_CST)
1789         return fold_convert_const_fixed_from_real (type, arg1);
1790     }
1791   return NULL_TREE;
1792 }
1793
1794 /* Construct a vector of zero elements of vector type TYPE.  */
1795
1796 static tree
1797 build_zero_vector (tree type)
1798 {
1799   tree elem, list;
1800   int i, units;
1801
1802   elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1803   units = TYPE_VECTOR_SUBPARTS (type);
1804
1805   list = NULL_TREE;
1806   for (i = 0; i < units; i++)
1807     list = tree_cons (NULL_TREE, elem, list);
1808   return build_vector (type, list);
1809 }
1810
1811 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
1812
1813 bool
1814 fold_convertible_p (const_tree type, const_tree arg)
1815 {
1816   tree orig = TREE_TYPE (arg);
1817
1818   if (type == orig)
1819     return true;
1820
1821   if (TREE_CODE (arg) == ERROR_MARK
1822       || TREE_CODE (type) == ERROR_MARK
1823       || TREE_CODE (orig) == ERROR_MARK)
1824     return false;
1825
1826   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1827     return true;
1828
1829   switch (TREE_CODE (type))
1830     {
1831     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1832     case POINTER_TYPE: case REFERENCE_TYPE:
1833     case OFFSET_TYPE:
1834       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1835           || TREE_CODE (orig) == OFFSET_TYPE)
1836         return true;
1837       return (TREE_CODE (orig) == VECTOR_TYPE
1838               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1839
1840     case REAL_TYPE:
1841     case FIXED_POINT_TYPE:
1842     case COMPLEX_TYPE:
1843     case VECTOR_TYPE:
1844     case VOID_TYPE:
1845       return TREE_CODE (type) == TREE_CODE (orig);
1846
1847     default:
1848       return false;
1849     }
1850 }
1851
1852 /* Convert expression ARG to type TYPE.  Used by the middle-end for
1853    simple conversions in preference to calling the front-end's convert.  */
1854
1855 tree
1856 fold_convert_loc (location_t loc, tree type, tree arg)
1857 {
1858   tree orig = TREE_TYPE (arg);
1859   tree tem;
1860
1861   if (type == orig)
1862     return arg;
1863
1864   if (TREE_CODE (arg) == ERROR_MARK
1865       || TREE_CODE (type) == ERROR_MARK
1866       || TREE_CODE (orig) == ERROR_MARK)
1867     return error_mark_node;
1868
1869   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1870     return fold_build1_loc (loc, NOP_EXPR, type, arg);
1871
1872   switch (TREE_CODE (type))
1873     {
1874     case POINTER_TYPE:
1875     case REFERENCE_TYPE:
1876       /* Handle conversions between pointers to different address spaces.  */
1877       if (POINTER_TYPE_P (orig)
1878           && (TYPE_ADDR_SPACE (TREE_TYPE (type))
1879               != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
1880         return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
1881       /* fall through */
1882
1883     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1884     case OFFSET_TYPE:
1885       if (TREE_CODE (arg) == INTEGER_CST)
1886         {
1887           tem = fold_convert_const (NOP_EXPR, type, arg);
1888           if (tem != NULL_TREE)
1889             return tem;
1890         }
1891       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1892           || TREE_CODE (orig) == OFFSET_TYPE)
1893         return fold_build1_loc (loc, NOP_EXPR, type, arg);
1894       if (TREE_CODE (orig) == COMPLEX_TYPE)
1895         return fold_convert_loc (loc, type,
1896                              fold_build1_loc (loc, REALPART_EXPR,
1897                                           TREE_TYPE (orig), arg));
1898       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
1899                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1900       return fold_build1_loc (loc, NOP_EXPR, type, arg);
1901
1902     case REAL_TYPE:
1903       if (TREE_CODE (arg) == INTEGER_CST)
1904         {
1905           tem = fold_convert_const (FLOAT_EXPR, type, arg);
1906           if (tem != NULL_TREE)
1907             return tem;
1908         }
1909       else if (TREE_CODE (arg) == REAL_CST)
1910         {
1911           tem = fold_convert_const (NOP_EXPR, type, arg);
1912           if (tem != NULL_TREE)
1913             return tem;
1914         }
1915       else if (TREE_CODE (arg) == FIXED_CST)
1916         {
1917           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1918           if (tem != NULL_TREE)
1919             return tem;
1920         }
1921
1922       switch (TREE_CODE (orig))
1923         {
1924         case INTEGER_TYPE:
1925         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1926         case POINTER_TYPE: case REFERENCE_TYPE:
1927           return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
1928
1929         case REAL_TYPE:
1930           return fold_build1_loc (loc, NOP_EXPR, type, arg);
1931
1932         case FIXED_POINT_TYPE:
1933           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1934
1935         case COMPLEX_TYPE:
1936           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1937           return fold_convert_loc (loc, type, tem);
1938
1939         default:
1940           gcc_unreachable ();
1941         }
1942
1943     case FIXED_POINT_TYPE:
1944       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
1945           || TREE_CODE (arg) == REAL_CST)
1946         {
1947           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1948           if (tem != NULL_TREE)
1949             goto fold_convert_exit;
1950         }
1951
1952       switch (TREE_CODE (orig))
1953         {
1954         case FIXED_POINT_TYPE:
1955         case INTEGER_TYPE:
1956         case ENUMERAL_TYPE:
1957         case BOOLEAN_TYPE:
1958         case REAL_TYPE:
1959           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1960
1961         case COMPLEX_TYPE:
1962           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1963           return fold_convert_loc (loc, type, tem);
1964
1965         default:
1966           gcc_unreachable ();
1967         }
1968
1969     case COMPLEX_TYPE:
1970       switch (TREE_CODE (orig))
1971         {
1972         case INTEGER_TYPE:
1973         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1974         case POINTER_TYPE: case REFERENCE_TYPE:
1975         case REAL_TYPE:
1976         case FIXED_POINT_TYPE:
1977           return fold_build2_loc (loc, COMPLEX_EXPR, type,
1978                               fold_convert_loc (loc, TREE_TYPE (type), arg),
1979                               fold_convert_loc (loc, TREE_TYPE (type),
1980                                             integer_zero_node));
1981         case COMPLEX_TYPE:
1982           {
1983             tree rpart, ipart;
1984
1985             if (TREE_CODE (arg) == COMPLEX_EXPR)
1986               {
1987                 rpart = fold_convert_loc (loc, TREE_TYPE (type),
1988                                       TREE_OPERAND (arg, 0));
1989                 ipart = fold_convert_loc (loc, TREE_TYPE (type),
1990                                       TREE_OPERAND (arg, 1));
1991                 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
1992               }
1993
1994             arg = save_expr (arg);
1995             rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1996             ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
1997             rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
1998             ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
1999             return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2000           }
2001
2002         default:
2003           gcc_unreachable ();
2004         }
2005
2006     case VECTOR_TYPE:
2007       if (integer_zerop (arg))
2008         return build_zero_vector (type);
2009       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2010       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2011                   || TREE_CODE (orig) == VECTOR_TYPE);
2012       return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2013
2014     case VOID_TYPE:
2015       tem = fold_ignored_result (arg);
2016       if (TREE_CODE (tem) == MODIFY_EXPR)
2017         goto fold_convert_exit;
2018       return fold_build1_loc (loc, NOP_EXPR, type, tem);
2019
2020     default:
2021       gcc_unreachable ();
2022     }
2023  fold_convert_exit:
2024   protected_set_expr_location (tem, loc);
2025   return tem;
2026 }
2027 \f
2028 /* Return false if expr can be assumed not to be an lvalue, true
2029    otherwise.  */
2030
2031 static bool
2032 maybe_lvalue_p (const_tree x)
2033 {
2034   /* We only need to wrap lvalue tree codes.  */
2035   switch (TREE_CODE (x))
2036   {
2037   case VAR_DECL:
2038   case PARM_DECL:
2039   case RESULT_DECL:
2040   case LABEL_DECL:
2041   case FUNCTION_DECL:
2042   case SSA_NAME:
2043
2044   case COMPONENT_REF:
2045   case MEM_REF:
2046   case INDIRECT_REF:
2047   case ARRAY_REF:
2048   case ARRAY_RANGE_REF:
2049   case BIT_FIELD_REF:
2050   case OBJ_TYPE_REF:
2051
2052   case REALPART_EXPR:
2053   case IMAGPART_EXPR:
2054   case PREINCREMENT_EXPR:
2055   case PREDECREMENT_EXPR:
2056   case SAVE_EXPR:
2057   case TRY_CATCH_EXPR:
2058   case WITH_CLEANUP_EXPR:
2059   case COMPOUND_EXPR:
2060   case MODIFY_EXPR:
2061   case TARGET_EXPR:
2062   case COND_EXPR:
2063   case BIND_EXPR:
2064     break;
2065
2066   default:
2067     /* Assume the worst for front-end tree codes.  */
2068     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2069       break;
2070     return false;
2071   }
2072
2073   return true;
2074 }
2075
2076 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2077
2078 tree
2079 non_lvalue_loc (location_t loc, tree x)
2080 {
2081   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2082      us.  */
2083   if (in_gimple_form)
2084     return x;
2085
2086   if (! maybe_lvalue_p (x))
2087     return x;
2088   x = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2089   SET_EXPR_LOCATION (x, loc);
2090   return x;
2091 }
2092
2093 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2094    Zero means allow extended lvalues.  */
2095
2096 int pedantic_lvalues;
2097
2098 /* When pedantic, return an expr equal to X but certainly not valid as a
2099    pedantic lvalue.  Otherwise, return X.  */
2100
2101 static tree
2102 pedantic_non_lvalue_loc (location_t loc, tree x)
2103 {
2104   if (pedantic_lvalues)
2105     return non_lvalue_loc (loc, x);
2106   protected_set_expr_location (x, loc);
2107   return x;
2108 }
2109 \f
2110 /* Given a tree comparison code, return the code that is the logical inverse
2111    of the given code.  It is not safe to do this for floating-point
2112    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2113    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2114
2115 enum tree_code
2116 invert_tree_comparison (enum tree_code code, bool honor_nans)
2117 {
2118   if (honor_nans && flag_trapping_math)
2119     return ERROR_MARK;
2120
2121   switch (code)
2122     {
2123     case EQ_EXPR:
2124       return NE_EXPR;
2125     case NE_EXPR:
2126       return EQ_EXPR;
2127     case GT_EXPR:
2128       return honor_nans ? UNLE_EXPR : LE_EXPR;
2129     case GE_EXPR:
2130       return honor_nans ? UNLT_EXPR : LT_EXPR;
2131     case LT_EXPR:
2132       return honor_nans ? UNGE_EXPR : GE_EXPR;
2133     case LE_EXPR:
2134       return honor_nans ? UNGT_EXPR : GT_EXPR;
2135     case LTGT_EXPR:
2136       return UNEQ_EXPR;
2137     case UNEQ_EXPR:
2138       return LTGT_EXPR;
2139     case UNGT_EXPR:
2140       return LE_EXPR;
2141     case UNGE_EXPR:
2142       return LT_EXPR;
2143     case UNLT_EXPR:
2144       return GE_EXPR;
2145     case UNLE_EXPR:
2146       return GT_EXPR;
2147     case ORDERED_EXPR:
2148       return UNORDERED_EXPR;
2149     case UNORDERED_EXPR:
2150       return ORDERED_EXPR;
2151     default:
2152       gcc_unreachable ();
2153     }
2154 }
2155
2156 /* Similar, but return the comparison that results if the operands are
2157    swapped.  This is safe for floating-point.  */
2158
2159 enum tree_code
2160 swap_tree_comparison (enum tree_code code)
2161 {
2162   switch (code)
2163     {
2164     case EQ_EXPR:
2165     case NE_EXPR:
2166     case ORDERED_EXPR:
2167     case UNORDERED_EXPR:
2168     case LTGT_EXPR:
2169     case UNEQ_EXPR:
2170       return code;
2171     case GT_EXPR:
2172       return LT_EXPR;
2173     case GE_EXPR:
2174       return LE_EXPR;
2175     case LT_EXPR:
2176       return GT_EXPR;
2177     case LE_EXPR:
2178       return GE_EXPR;
2179     case UNGT_EXPR:
2180       return UNLT_EXPR;
2181     case UNGE_EXPR:
2182       return UNLE_EXPR;
2183     case UNLT_EXPR:
2184       return UNGT_EXPR;
2185     case UNLE_EXPR:
2186       return UNGE_EXPR;
2187     default:
2188       gcc_unreachable ();
2189     }
2190 }
2191
2192
2193 /* Convert a comparison tree code from an enum tree_code representation
2194    into a compcode bit-based encoding.  This function is the inverse of
2195    compcode_to_comparison.  */
2196
2197 static enum comparison_code
2198 comparison_to_compcode (enum tree_code code)
2199 {
2200   switch (code)
2201     {
2202     case LT_EXPR:
2203       return COMPCODE_LT;
2204     case EQ_EXPR:
2205       return COMPCODE_EQ;
2206     case LE_EXPR:
2207       return COMPCODE_LE;
2208     case GT_EXPR:
2209       return COMPCODE_GT;
2210     case NE_EXPR:
2211       return COMPCODE_NE;
2212     case GE_EXPR:
2213       return COMPCODE_GE;
2214     case ORDERED_EXPR:
2215       return COMPCODE_ORD;
2216     case UNORDERED_EXPR:
2217       return COMPCODE_UNORD;
2218     case UNLT_EXPR:
2219       return COMPCODE_UNLT;
2220     case UNEQ_EXPR:
2221       return COMPCODE_UNEQ;
2222     case UNLE_EXPR:
2223       return COMPCODE_UNLE;
2224     case UNGT_EXPR:
2225       return COMPCODE_UNGT;
2226     case LTGT_EXPR:
2227       return COMPCODE_LTGT;
2228     case UNGE_EXPR:
2229       return COMPCODE_UNGE;
2230     default:
2231       gcc_unreachable ();
2232     }
2233 }
2234
2235 /* Convert a compcode bit-based encoding of a comparison operator back
2236    to GCC's enum tree_code representation.  This function is the
2237    inverse of comparison_to_compcode.  */
2238
2239 static enum tree_code
2240 compcode_to_comparison (enum comparison_code code)
2241 {
2242   switch (code)
2243     {
2244     case COMPCODE_LT:
2245       return LT_EXPR;
2246     case COMPCODE_EQ:
2247       return EQ_EXPR;
2248     case COMPCODE_LE:
2249       return LE_EXPR;
2250     case COMPCODE_GT:
2251       return GT_EXPR;
2252     case COMPCODE_NE:
2253       return NE_EXPR;
2254     case COMPCODE_GE:
2255       return GE_EXPR;
2256     case COMPCODE_ORD:
2257       return ORDERED_EXPR;
2258     case COMPCODE_UNORD:
2259       return UNORDERED_EXPR;
2260     case COMPCODE_UNLT:
2261       return UNLT_EXPR;
2262     case COMPCODE_UNEQ:
2263       return UNEQ_EXPR;
2264     case COMPCODE_UNLE:
2265       return UNLE_EXPR;
2266     case COMPCODE_UNGT:
2267       return UNGT_EXPR;
2268     case COMPCODE_LTGT:
2269       return LTGT_EXPR;
2270     case COMPCODE_UNGE:
2271       return UNGE_EXPR;
2272     default:
2273       gcc_unreachable ();
2274     }
2275 }
2276
2277 /* Return a tree for the comparison which is the combination of
2278    doing the AND or OR (depending on CODE) of the two operations LCODE
2279    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2280    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2281    if this makes the transformation invalid.  */
2282
2283 tree
2284 combine_comparisons (location_t loc,
2285                      enum tree_code code, enum tree_code lcode,
2286                      enum tree_code rcode, tree truth_type,
2287                      tree ll_arg, tree lr_arg)
2288 {
2289   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2290   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2291   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2292   int compcode;
2293
2294   switch (code)
2295     {
2296     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2297       compcode = lcompcode & rcompcode;
2298       break;
2299
2300     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2301       compcode = lcompcode | rcompcode;
2302       break;
2303
2304     default:
2305       return NULL_TREE;
2306     }
2307
2308   if (!honor_nans)
2309     {
2310       /* Eliminate unordered comparisons, as well as LTGT and ORD
2311          which are not used unless the mode has NaNs.  */
2312       compcode &= ~COMPCODE_UNORD;
2313       if (compcode == COMPCODE_LTGT)
2314         compcode = COMPCODE_NE;
2315       else if (compcode == COMPCODE_ORD)
2316         compcode = COMPCODE_TRUE;
2317     }
2318    else if (flag_trapping_math)
2319      {
2320         /* Check that the original operation and the optimized ones will trap
2321            under the same condition.  */
2322         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2323                      && (lcompcode != COMPCODE_EQ)
2324                      && (lcompcode != COMPCODE_ORD);
2325         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2326                      && (rcompcode != COMPCODE_EQ)
2327                      && (rcompcode != COMPCODE_ORD);
2328         bool trap = (compcode & COMPCODE_UNORD) == 0
2329                     && (compcode != COMPCODE_EQ)
2330                     && (compcode != COMPCODE_ORD);
2331
2332         /* In a short-circuited boolean expression the LHS might be
2333            such that the RHS, if evaluated, will never trap.  For
2334            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2335            if neither x nor y is NaN.  (This is a mixed blessing: for
2336            example, the expression above will never trap, hence
2337            optimizing it to x < y would be invalid).  */
2338         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2339             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2340           rtrap = false;
2341
2342         /* If the comparison was short-circuited, and only the RHS
2343            trapped, we may now generate a spurious trap.  */
2344         if (rtrap && !ltrap
2345             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2346           return NULL_TREE;
2347
2348         /* If we changed the conditions that cause a trap, we lose.  */
2349         if ((ltrap || rtrap) != trap)
2350           return NULL_TREE;
2351       }
2352
2353   if (compcode == COMPCODE_TRUE)
2354     return constant_boolean_node (true, truth_type);
2355   else if (compcode == COMPCODE_FALSE)
2356     return constant_boolean_node (false, truth_type);
2357   else
2358     {
2359       enum tree_code tcode;
2360
2361       tcode = compcode_to_comparison ((enum comparison_code) compcode);
2362       return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2363     }
2364 }
2365 \f
2366 /* Return nonzero if two operands (typically of the same tree node)
2367    are necessarily equal.  If either argument has side-effects this
2368    function returns zero.  FLAGS modifies behavior as follows:
2369
2370    If OEP_ONLY_CONST is set, only return nonzero for constants.
2371    This function tests whether the operands are indistinguishable;
2372    it does not test whether they are equal using C's == operation.
2373    The distinction is important for IEEE floating point, because
2374    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2375    (2) two NaNs may be indistinguishable, but NaN!=NaN.
2376
2377    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2378    even though it may hold multiple values during a function.
2379    This is because a GCC tree node guarantees that nothing else is
2380    executed between the evaluation of its "operands" (which may often
2381    be evaluated in arbitrary order).  Hence if the operands themselves
2382    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2383    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
2384    unset means assuming isochronic (or instantaneous) tree equivalence.
2385    Unless comparing arbitrary expression trees, such as from different
2386    statements, this flag can usually be left unset.
2387
2388    If OEP_PURE_SAME is set, then pure functions with identical arguments
2389    are considered the same.  It is used when the caller has other ways
2390    to ensure that global memory is unchanged in between.  */
2391
2392 int
2393 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2394 {
2395   /* If either is ERROR_MARK, they aren't equal.  */
2396   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2397       || TREE_TYPE (arg0) == error_mark_node
2398       || TREE_TYPE (arg1) == error_mark_node)
2399     return 0;
2400
2401   /* Similar, if either does not have a type (like a released SSA name), 
2402      they aren't equal.  */
2403   if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2404     return 0;
2405
2406   /* Check equality of integer constants before bailing out due to
2407      precision differences.  */
2408   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2409     return tree_int_cst_equal (arg0, arg1);
2410
2411   /* If both types don't have the same signedness, then we can't consider
2412      them equal.  We must check this before the STRIP_NOPS calls
2413      because they may change the signedness of the arguments.  As pointers
2414      strictly don't have a signedness, require either two pointers or
2415      two non-pointers as well.  */
2416   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2417       || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2418     return 0;
2419
2420   /* We cannot consider pointers to different address space equal.  */
2421   if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2422       && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2423           != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2424     return 0;
2425
2426   /* If both types don't have the same precision, then it is not safe
2427      to strip NOPs.  */
2428   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
2429     return 0;
2430
2431   STRIP_NOPS (arg0);
2432   STRIP_NOPS (arg1);
2433
2434   /* In case both args are comparisons but with different comparison
2435      code, try to swap the comparison operands of one arg to produce
2436      a match and compare that variant.  */
2437   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2438       && COMPARISON_CLASS_P (arg0)
2439       && COMPARISON_CLASS_P (arg1))
2440     {
2441       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2442
2443       if (TREE_CODE (arg0) == swap_code)
2444         return operand_equal_p (TREE_OPERAND (arg0, 0),
2445                                 TREE_OPERAND (arg1, 1), flags)
2446                && operand_equal_p (TREE_OPERAND (arg0, 1),
2447                                    TREE_OPERAND (arg1, 0), flags);
2448     }
2449
2450   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2451       /* This is needed for conversions and for COMPONENT_REF.
2452          Might as well play it safe and always test this.  */
2453       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2454       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2455       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2456     return 0;
2457
2458   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2459      We don't care about side effects in that case because the SAVE_EXPR
2460      takes care of that for us. In all other cases, two expressions are
2461      equal if they have no side effects.  If we have two identical
2462      expressions with side effects that should be treated the same due
2463      to the only side effects being identical SAVE_EXPR's, that will
2464      be detected in the recursive calls below.  */
2465   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2466       && (TREE_CODE (arg0) == SAVE_EXPR
2467           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2468     return 1;
2469
2470   /* Next handle constant cases, those for which we can return 1 even
2471      if ONLY_CONST is set.  */
2472   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2473     switch (TREE_CODE (arg0))
2474       {
2475       case INTEGER_CST:
2476         return tree_int_cst_equal (arg0, arg1);
2477
2478       case FIXED_CST:
2479         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2480                                        TREE_FIXED_CST (arg1));
2481
2482       case REAL_CST:
2483         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2484                                    TREE_REAL_CST (arg1)))
2485           return 1;
2486
2487
2488         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
2489           {
2490             /* If we do not distinguish between signed and unsigned zero,
2491                consider them equal.  */
2492             if (real_zerop (arg0) && real_zerop (arg1))
2493               return 1;
2494           }
2495         return 0;
2496
2497       case VECTOR_CST:
2498         {
2499           tree v1, v2;
2500
2501           v1 = TREE_VECTOR_CST_ELTS (arg0);
2502           v2 = TREE_VECTOR_CST_ELTS (arg1);
2503           while (v1 && v2)
2504             {
2505               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2506                                     flags))
2507                 return 0;
2508               v1 = TREE_CHAIN (v1);
2509               v2 = TREE_CHAIN (v2);
2510             }
2511
2512           return v1 == v2;
2513         }
2514
2515       case COMPLEX_CST:
2516         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2517                                  flags)
2518                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2519                                     flags));
2520
2521       case STRING_CST:
2522         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2523                 && ! memcmp (TREE_STRING_POINTER (arg0),
2524                               TREE_STRING_POINTER (arg1),
2525                               TREE_STRING_LENGTH (arg0)));
2526
2527       case ADDR_EXPR:
2528         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2529                                 0);
2530       default:
2531         break;
2532       }
2533
2534   if (flags & OEP_ONLY_CONST)
2535     return 0;
2536
2537 /* Define macros to test an operand from arg0 and arg1 for equality and a
2538    variant that allows null and views null as being different from any
2539    non-null value.  In the latter case, if either is null, the both
2540    must be; otherwise, do the normal comparison.  */
2541 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
2542                                     TREE_OPERAND (arg1, N), flags)
2543
2544 #define OP_SAME_WITH_NULL(N)                            \
2545   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2546    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2547
2548   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2549     {
2550     case tcc_unary:
2551       /* Two conversions are equal only if signedness and modes match.  */
2552       switch (TREE_CODE (arg0))
2553         {
2554         CASE_CONVERT:
2555         case FIX_TRUNC_EXPR:
2556           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2557               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2558             return 0;
2559           break;
2560         default:
2561           break;
2562         }
2563
2564       return OP_SAME (0);
2565
2566
2567     case tcc_comparison:
2568     case tcc_binary:
2569       if (OP_SAME (0) && OP_SAME (1))
2570         return 1;
2571
2572       /* For commutative ops, allow the other order.  */
2573       return (commutative_tree_code (TREE_CODE (arg0))
2574               && operand_equal_p (TREE_OPERAND (arg0, 0),
2575                                   TREE_OPERAND (arg1, 1), flags)
2576               && operand_equal_p (TREE_OPERAND (arg0, 1),
2577                                   TREE_OPERAND (arg1, 0), flags));
2578
2579     case tcc_reference:
2580       /* If either of the pointer (or reference) expressions we are
2581          dereferencing contain a side effect, these cannot be equal.  */
2582       if (TREE_SIDE_EFFECTS (arg0)
2583           || TREE_SIDE_EFFECTS (arg1))
2584         return 0;
2585
2586       switch (TREE_CODE (arg0))
2587         {
2588         case INDIRECT_REF:
2589         case REALPART_EXPR:
2590         case IMAGPART_EXPR:
2591           return OP_SAME (0);
2592
2593         case MEM_REF:
2594           /* Require equal access sizes, and similar pointer types.
2595              We can have incomplete types for array references of
2596              variable-sized arrays from the Fortran frontent
2597              though.  */
2598           return ((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1))
2599                    || (TYPE_SIZE (TREE_TYPE (arg0))
2600                        && TYPE_SIZE (TREE_TYPE (arg1))
2601                        && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
2602                                            TYPE_SIZE (TREE_TYPE (arg1)), flags)))
2603                   && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg0, 1)))
2604                       == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg1, 1))))
2605                   && OP_SAME (0) && OP_SAME (1));
2606
2607         case ARRAY_REF:
2608         case ARRAY_RANGE_REF:
2609           /* Operands 2 and 3 may be null.
2610              Compare the array index by value if it is constant first as we
2611              may have different types but same value here.  */
2612           return (OP_SAME (0)
2613                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2614                                           TREE_OPERAND (arg1, 1))
2615                       || OP_SAME (1))
2616                   && OP_SAME_WITH_NULL (2)
2617                   && OP_SAME_WITH_NULL (3));
2618
2619         case COMPONENT_REF:
2620           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
2621              may be NULL when we're called to compare MEM_EXPRs.  */
2622           return OP_SAME_WITH_NULL (0)
2623                  && OP_SAME (1)
2624                  && OP_SAME_WITH_NULL (2);
2625
2626         case BIT_FIELD_REF:
2627           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2628
2629         default:
2630           return 0;
2631         }
2632
2633     case tcc_expression:
2634       switch (TREE_CODE (arg0))
2635         {
2636         case ADDR_EXPR:
2637         case TRUTH_NOT_EXPR:
2638           return OP_SAME (0);
2639
2640         case TRUTH_ANDIF_EXPR:
2641         case TRUTH_ORIF_EXPR:
2642           return OP_SAME (0) && OP_SAME (1);
2643
2644         case TRUTH_AND_EXPR:
2645         case TRUTH_OR_EXPR:
2646         case TRUTH_XOR_EXPR:
2647           if (OP_SAME (0) && OP_SAME (1))
2648             return 1;
2649
2650           /* Otherwise take into account this is a commutative operation.  */
2651           return (operand_equal_p (TREE_OPERAND (arg0, 0),
2652                                    TREE_OPERAND (arg1, 1), flags)
2653                   && operand_equal_p (TREE_OPERAND (arg0, 1),
2654                                       TREE_OPERAND (arg1, 0), flags));
2655
2656         case COND_EXPR:
2657           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2658
2659         default:
2660           return 0;
2661         }
2662
2663     case tcc_vl_exp:
2664       switch (TREE_CODE (arg0))
2665         {
2666         case CALL_EXPR:
2667           /* If the CALL_EXPRs call different functions, then they
2668              clearly can not be equal.  */
2669           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
2670                                  flags))
2671             return 0;
2672
2673           {
2674             unsigned int cef = call_expr_flags (arg0);
2675             if (flags & OEP_PURE_SAME)
2676               cef &= ECF_CONST | ECF_PURE;
2677             else
2678               cef &= ECF_CONST;
2679             if (!cef)
2680               return 0;
2681           }
2682
2683           /* Now see if all the arguments are the same.  */
2684           {
2685             const_call_expr_arg_iterator iter0, iter1;
2686             const_tree a0, a1;
2687             for (a0 = first_const_call_expr_arg (arg0, &iter0),
2688                    a1 = first_const_call_expr_arg (arg1, &iter1);
2689                  a0 && a1;
2690                  a0 = next_const_call_expr_arg (&iter0),
2691                    a1 = next_const_call_expr_arg (&iter1))
2692               if (! operand_equal_p (a0, a1, flags))
2693                 return 0;
2694
2695             /* If we get here and both argument lists are exhausted
2696                then the CALL_EXPRs are equal.  */
2697             return ! (a0 || a1);
2698           }
2699         default:
2700           return 0;
2701         }
2702
2703     case tcc_declaration:
2704       /* Consider __builtin_sqrt equal to sqrt.  */
2705       return (TREE_CODE (arg0) == FUNCTION_DECL
2706               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2707               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2708               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2709
2710     default:
2711       return 0;
2712     }
2713
2714 #undef OP_SAME
2715 #undef OP_SAME_WITH_NULL
2716 }
2717 \f
2718 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2719    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2720
2721    When in doubt, return 0.  */
2722
2723 static int
2724 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2725 {
2726   int unsignedp1, unsignedpo;
2727   tree primarg0, primarg1, primother;
2728   unsigned int correct_width;
2729
2730   if (operand_equal_p (arg0, arg1, 0))
2731     return 1;
2732
2733   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2734       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2735     return 0;
2736
2737   /* Discard any conversions that don't change the modes of ARG0 and ARG1
2738      and see if the inner values are the same.  This removes any
2739      signedness comparison, which doesn't matter here.  */
2740   primarg0 = arg0, primarg1 = arg1;
2741   STRIP_NOPS (primarg0);
2742   STRIP_NOPS (primarg1);
2743   if (operand_equal_p (primarg0, primarg1, 0))
2744     return 1;
2745
2746   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2747      actual comparison operand, ARG0.
2748
2749      First throw away any conversions to wider types
2750      already present in the operands.  */
2751
2752   primarg1 = get_narrower (arg1, &unsignedp1);
2753   primother = get_narrower (other, &unsignedpo);
2754
2755   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2756   if (unsignedp1 == unsignedpo
2757       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2758       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2759     {
2760       tree type = TREE_TYPE (arg0);
2761
2762       /* Make sure shorter operand is extended the right way
2763          to match the longer operand.  */
2764       primarg1 = fold_convert (signed_or_unsigned_type_for
2765                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2766
2767       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2768         return 1;
2769     }
2770
2771   return 0;
2772 }
2773 \f
2774 /* See if ARG is an expression that is either a comparison or is performing
2775    arithmetic on comparisons.  The comparisons must only be comparing
2776    two different values, which will be stored in *CVAL1 and *CVAL2; if
2777    they are nonzero it means that some operands have already been found.
2778    No variables may be used anywhere else in the expression except in the
2779    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
2780    the expression and save_expr needs to be called with CVAL1 and CVAL2.
2781
2782    If this is true, return 1.  Otherwise, return zero.  */
2783
2784 static int
2785 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2786 {
2787   enum tree_code code = TREE_CODE (arg);
2788   enum tree_code_class tclass = TREE_CODE_CLASS (code);
2789
2790   /* We can handle some of the tcc_expression cases here.  */
2791   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2792     tclass = tcc_unary;
2793   else if (tclass == tcc_expression
2794            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2795                || code == COMPOUND_EXPR))
2796     tclass = tcc_binary;
2797
2798   else if (tclass == tcc_expression && code == SAVE_EXPR
2799            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2800     {
2801       /* If we've already found a CVAL1 or CVAL2, this expression is
2802          two complex to handle.  */
2803       if (*cval1 || *cval2)
2804         return 0;
2805
2806       tclass = tcc_unary;
2807       *save_p = 1;
2808     }
2809
2810   switch (tclass)
2811     {
2812     case tcc_unary:
2813       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2814
2815     case tcc_binary:
2816       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2817               && twoval_comparison_p (TREE_OPERAND (arg, 1),
2818                                       cval1, cval2, save_p));
2819
2820     case tcc_constant:
2821       return 1;
2822
2823     case tcc_expression:
2824       if (code == COND_EXPR)
2825         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2826                                      cval1, cval2, save_p)
2827                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2828                                         cval1, cval2, save_p)
2829                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2830                                         cval1, cval2, save_p));
2831       return 0;
2832
2833     case tcc_comparison:
2834       /* First see if we can handle the first operand, then the second.  For
2835          the second operand, we know *CVAL1 can't be zero.  It must be that
2836          one side of the comparison is each of the values; test for the
2837          case where this isn't true by failing if the two operands
2838          are the same.  */
2839
2840       if (operand_equal_p (TREE_OPERAND (arg, 0),
2841                            TREE_OPERAND (arg, 1), 0))
2842         return 0;
2843
2844       if (*cval1 == 0)
2845         *cval1 = TREE_OPERAND (arg, 0);
2846       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2847         ;
2848       else if (*cval2 == 0)
2849         *cval2 = TREE_OPERAND (arg, 0);
2850       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2851         ;
2852       else
2853         return 0;
2854
2855       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2856         ;
2857       else if (*cval2 == 0)
2858         *cval2 = TREE_OPERAND (arg, 1);
2859       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2860         ;
2861       else
2862         return 0;
2863
2864       return 1;
2865
2866     default:
2867       return 0;
2868     }
2869 }
2870 \f
2871 /* ARG is a tree that is known to contain just arithmetic operations and
2872    comparisons.  Evaluate the operations in the tree substituting NEW0 for
2873    any occurrence of OLD0 as an operand of a comparison and likewise for
2874    NEW1 and OLD1.  */
2875
2876 static tree
2877 eval_subst (location_t loc, tree arg, tree old0, tree new0,
2878             tree old1, tree new1)
2879 {
2880   tree type = TREE_TYPE (arg);
2881   enum tree_code code = TREE_CODE (arg);
2882   enum tree_code_class tclass = TREE_CODE_CLASS (code);
2883
2884   /* We can handle some of the tcc_expression cases here.  */
2885   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2886     tclass = tcc_unary;
2887   else if (tclass == tcc_expression
2888            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2889     tclass = tcc_binary;
2890
2891   switch (tclass)
2892     {
2893     case tcc_unary:
2894       return fold_build1_loc (loc, code, type,
2895                           eval_subst (loc, TREE_OPERAND (arg, 0),
2896                                       old0, new0, old1, new1));
2897
2898     case tcc_binary:
2899       return fold_build2_loc (loc, code, type,
2900                           eval_subst (loc, TREE_OPERAND (arg, 0),
2901                                       old0, new0, old1, new1),
2902                           eval_subst (loc, TREE_OPERAND (arg, 1),
2903                                       old0, new0, old1, new1));
2904
2905     case tcc_expression:
2906       switch (code)
2907         {
2908         case SAVE_EXPR:
2909           return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
2910                              old1, new1);
2911
2912         case COMPOUND_EXPR:
2913           return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
2914                              old1, new1);
2915
2916         case COND_EXPR:
2917           return fold_build3_loc (loc, code, type,
2918                               eval_subst (loc, TREE_OPERAND (arg, 0),
2919                                           old0, new0, old1, new1),
2920                               eval_subst (loc, TREE_OPERAND (arg, 1),
2921                                           old0, new0, old1, new1),
2922                               eval_subst (loc, TREE_OPERAND (arg, 2),
2923                                           old0, new0, old1, new1));
2924         default:
2925           break;
2926         }
2927       /* Fall through - ???  */
2928
2929     case tcc_comparison:
2930       {
2931         tree arg0 = TREE_OPERAND (arg, 0);
2932         tree arg1 = TREE_OPERAND (arg, 1);
2933
2934         /* We need to check both for exact equality and tree equality.  The
2935            former will be true if the operand has a side-effect.  In that
2936            case, we know the operand occurred exactly once.  */
2937
2938         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2939           arg0 = new0;
2940         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2941           arg0 = new1;
2942
2943         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2944           arg1 = new0;
2945         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2946           arg1 = new1;
2947
2948         return fold_build2_loc (loc, code, type, arg0, arg1);
2949       }
2950
2951     default:
2952       return arg;
2953     }
2954 }
2955 \f
2956 /* Return a tree for the case when the result of an expression is RESULT
2957    converted to TYPE and OMITTED was previously an operand of the expression
2958    but is now not needed (e.g., we folded OMITTED * 0).
2959
2960    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
2961    the conversion of RESULT to TYPE.  */
2962
2963 tree
2964 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
2965 {
2966   tree t = fold_convert_loc (loc, type, result);
2967
2968   /* If the resulting operand is an empty statement, just return the omitted
2969      statement casted to void. */
2970   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
2971     {
2972       t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
2973       goto omit_one_operand_exit;
2974     }
2975
2976   if (TREE_SIDE_EFFECTS (omitted))
2977     {
2978       t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
2979       goto omit_one_operand_exit;
2980     }
2981
2982   return non_lvalue_loc (loc, t);
2983
2984  omit_one_operand_exit:
2985   protected_set_expr_location (t, loc);
2986   return t;
2987 }
2988
2989 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
2990
2991 static tree
2992 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
2993                                tree omitted)
2994 {
2995   tree t = fold_convert_loc (loc, type, result);
2996
2997   /* If the resulting operand is an empty statement, just return the omitted
2998      statement casted to void. */
2999   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3000     {
3001       t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3002       goto pedantic_omit_one_operand_exit;
3003     }
3004
3005   if (TREE_SIDE_EFFECTS (omitted))
3006     {
3007       t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3008       goto pedantic_omit_one_operand_exit;
3009     }
3010
3011   return pedantic_non_lvalue_loc (loc, t);
3012
3013  pedantic_omit_one_operand_exit:
3014   protected_set_expr_location (t, loc);
3015   return t;
3016 }
3017
3018 /* Return a tree for the case when the result of an expression is RESULT
3019    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3020    of the expression but are now not needed.
3021
3022    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3023    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3024    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3025    just do the conversion of RESULT to TYPE.  */
3026
3027 tree
3028 omit_two_operands_loc (location_t loc, tree type, tree result,
3029                    tree omitted1, tree omitted2)
3030 {
3031   tree t = fold_convert_loc (loc, type, result);
3032
3033   if (TREE_SIDE_EFFECTS (omitted2))
3034     {
3035       t = build2 (COMPOUND_EXPR, type, omitted2, t);
3036       SET_EXPR_LOCATION (t, loc);
3037     }
3038   if (TREE_SIDE_EFFECTS (omitted1))
3039     {
3040       t = build2 (COMPOUND_EXPR, type, omitted1, t);
3041       SET_EXPR_LOCATION (t, loc);
3042     }
3043
3044   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3045 }
3046
3047 \f
3048 /* Return a simplified tree node for the truth-negation of ARG.  This
3049    never alters ARG itself.  We assume that ARG is an operation that
3050    returns a truth value (0 or 1).
3051
3052    FIXME: one would think we would fold the result, but it causes
3053    problems with the dominator optimizer.  */
3054
3055 tree
3056 fold_truth_not_expr (location_t loc, tree arg)
3057 {
3058   tree t, type = TREE_TYPE (arg);
3059   enum tree_code code = TREE_CODE (arg);
3060   location_t loc1, loc2;
3061
3062   /* If this is a comparison, we can simply invert it, except for
3063      floating-point non-equality comparisons, in which case we just
3064      enclose a TRUTH_NOT_EXPR around what we have.  */
3065
3066   if (TREE_CODE_CLASS (code) == tcc_comparison)
3067     {
3068       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3069       if (FLOAT_TYPE_P (op_type)
3070           && flag_trapping_math
3071           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3072           && code != NE_EXPR && code != EQ_EXPR)
3073         return NULL_TREE;
3074
3075       code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3076       if (code == ERROR_MARK)
3077         return NULL_TREE;
3078
3079       t = build2 (code, type, TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3080       SET_EXPR_LOCATION (t, loc);
3081       return t;
3082     }
3083
3084   switch (code)
3085     {
3086     case INTEGER_CST:
3087       return constant_boolean_node (integer_zerop (arg), type);
3088
3089     case TRUTH_AND_EXPR:
3090       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3091       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3092       if (loc1 == UNKNOWN_LOCATION)
3093         loc1 = loc;
3094       if (loc2 == UNKNOWN_LOCATION)
3095         loc2 = loc;
3096       t = build2 (TRUTH_OR_EXPR, type,
3097                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3098                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3099       break;
3100
3101     case TRUTH_OR_EXPR:
3102       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3103       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3104       if (loc1 == UNKNOWN_LOCATION)
3105         loc1 = loc;
3106       if (loc2 == UNKNOWN_LOCATION)
3107         loc2 = loc;
3108       t = build2 (TRUTH_AND_EXPR, type,
3109                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3110                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3111       break;
3112
3113     case TRUTH_XOR_EXPR:
3114       /* Here we can invert either operand.  We invert the first operand
3115          unless the second operand is a TRUTH_NOT_EXPR in which case our
3116          result is the XOR of the first operand with the inside of the
3117          negation of the second operand.  */
3118
3119       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3120         t = build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3121                     TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3122       else
3123         t = build2 (TRUTH_XOR_EXPR, type,
3124                     invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3125                     TREE_OPERAND (arg, 1));
3126       break;
3127
3128     case TRUTH_ANDIF_EXPR:
3129       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3130       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3131       if (loc1 == UNKNOWN_LOCATION)
3132         loc1 = loc;
3133       if (loc2 == UNKNOWN_LOCATION)
3134         loc2 = loc;
3135       t = build2 (TRUTH_ORIF_EXPR, type,
3136                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3137                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3138       break;
3139
3140     case TRUTH_ORIF_EXPR:
3141       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3142       loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3143       if (loc1 == UNKNOWN_LOCATION)
3144         loc1 = loc;
3145       if (loc2 == UNKNOWN_LOCATION)
3146         loc2 = loc;
3147       t = build2 (TRUTH_ANDIF_EXPR, type,
3148                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3149                   invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3150       break;
3151
3152     case TRUTH_NOT_EXPR:
3153       return TREE_OPERAND (arg, 0);
3154
3155     case COND_EXPR:
3156       {
3157         tree arg1 = TREE_OPERAND (arg, 1);
3158         tree arg2 = TREE_OPERAND (arg, 2);
3159
3160         loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3161         loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 2));
3162         if (loc1 == UNKNOWN_LOCATION)
3163           loc1 = loc;
3164         if (loc2 == UNKNOWN_LOCATION)
3165           loc2 = loc;
3166
3167         /* A COND_EXPR may have a throw as one operand, which
3168            then has void type.  Just leave void operands
3169            as they are.  */
3170         t = build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3171                     VOID_TYPE_P (TREE_TYPE (arg1))
3172                     ? arg1 : invert_truthvalue_loc (loc1, arg1),
3173                     VOID_TYPE_P (TREE_TYPE (arg2))
3174                     ? arg2 : invert_truthvalue_loc (loc2, arg2));
3175         break;
3176       }
3177
3178     case COMPOUND_EXPR:
3179       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3180       if (loc1 == UNKNOWN_LOCATION)
3181         loc1 = loc;
3182       t = build2 (COMPOUND_EXPR, type,
3183                   TREE_OPERAND (arg, 0),
3184                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3185       break;
3186
3187     case NON_LVALUE_EXPR:
3188       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3189       if (loc1 == UNKNOWN_LOCATION)
3190         loc1 = loc;
3191       return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3192
3193     CASE_CONVERT:
3194       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3195         {
3196           t = build1 (TRUTH_NOT_EXPR, type, arg);
3197           break;
3198         }
3199
3200       /* ... fall through ...  */
3201
3202     case FLOAT_EXPR:
3203       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3204       if (loc1 == UNKNOWN_LOCATION)
3205         loc1 = loc;
3206       t = build1 (TREE_CODE (arg), type,
3207                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3208       break;
3209
3210     case BIT_AND_EXPR:
3211       if (!integer_onep (TREE_OPERAND (arg, 1)))
3212         return NULL_TREE;
3213       t = build2 (EQ_EXPR, type, arg, build_int_cst (type, 0));
3214       break;
3215
3216     case SAVE_EXPR:
3217       t = build1 (TRUTH_NOT_EXPR, type, arg);
3218       break;
3219
3220     case CLEANUP_POINT_EXPR:
3221       loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3222       if (loc1 == UNKNOWN_LOCATION)
3223         loc1 = loc;
3224       t = build1 (CLEANUP_POINT_EXPR, type,
3225                   invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3226       break;
3227
3228     default:
3229       t = NULL_TREE;
3230       break;
3231     }
3232
3233   if (t)
3234     SET_EXPR_LOCATION (t, loc);
3235
3236   return t;
3237 }
3238
3239 /* Return a simplified tree node for the truth-negation of ARG.  This
3240    never alters ARG itself.  We assume that ARG is an operation that
3241    returns a truth value (0 or 1).
3242
3243    FIXME: one would think we would fold the result, but it causes
3244    problems with the dominator optimizer.  */
3245
3246 tree
3247 invert_truthvalue_loc (location_t loc, tree arg)
3248 {
3249   tree tem;
3250
3251   if (TREE_CODE (arg) == ERROR_MARK)
3252     return arg;
3253
3254   tem = fold_truth_not_expr (loc, arg);
3255   if (!tem)
3256     {
3257       tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3258       SET_EXPR_LOCATION (tem, loc);
3259     }
3260
3261   return tem;
3262 }
3263
3264 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3265    operands are another bit-wise operation with a common input.  If so,
3266    distribute the bit operations to save an operation and possibly two if
3267    constants are involved.  For example, convert
3268         (A | B) & (A | C) into A | (B & C)
3269    Further simplification will occur if B and C are constants.
3270
3271    If this optimization cannot be done, 0 will be returned.  */
3272
3273 static tree
3274 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
3275                      tree arg0, tree arg1)
3276 {
3277   tree common;
3278   tree left, right;
3279
3280   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3281       || TREE_CODE (arg0) == code
3282       || (TREE_CODE (arg0) != BIT_AND_EXPR
3283           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3284     return 0;
3285
3286   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3287     {
3288       common = TREE_OPERAND (arg0, 0);
3289       left = TREE_OPERAND (arg0, 1);
3290       right = TREE_OPERAND (arg1, 1);
3291     }
3292   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3293     {
3294       common = TREE_OPERAND (arg0, 0);
3295       left = TREE_OPERAND (arg0, 1);
3296       right = TREE_OPERAND (arg1, 0);
3297     }
3298   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3299     {
3300       common = TREE_OPERAND (arg0, 1);
3301       left = TREE_OPERAND (arg0, 0);
3302       right = TREE_OPERAND (arg1, 1);
3303     }
3304   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3305     {
3306       common = TREE_OPERAND (arg0, 1);
3307       left = TREE_OPERAND (arg0, 0);
3308       right = TREE_OPERAND (arg1, 0);
3309     }
3310   else
3311     return 0;
3312
3313   common = fold_convert_loc (loc, type, common);
3314   left = fold_convert_loc (loc, type, left);
3315   right = fold_convert_loc (loc, type, right);
3316   return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
3317                       fold_build2_loc (loc, code, type, left, right));
3318 }
3319
3320 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3321    with code CODE.  This optimization is unsafe.  */
3322 static tree
3323 distribute_real_division (location_t loc, enum tree_code code, tree type,
3324                           tree arg0, tree arg1)
3325 {
3326   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3327   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3328
3329   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3330   if (mul0 == mul1
3331       && operand_equal_p (TREE_OPERAND (arg0, 1),
3332                        TREE_OPERAND (arg1, 1), 0))
3333     return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3334                         fold_build2_loc (loc, code, type,
3335                                      TREE_OPERAND (arg0, 0),
3336                                      TREE_OPERAND (arg1, 0)),
3337                         TREE_OPERAND (arg0, 1));
3338
3339   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3340   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3341                        TREE_OPERAND (arg1, 0), 0)
3342       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3343       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3344     {
3345       REAL_VALUE_TYPE r0, r1;
3346       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3347       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3348       if (!mul0)
3349         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3350       if (!mul1)
3351         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3352       real_arithmetic (&r0, code, &r0, &r1);
3353       return fold_build2_loc (loc, MULT_EXPR, type,
3354                           TREE_OPERAND (arg0, 0),
3355                           build_real (type, r0));
3356     }
3357
3358   return NULL_TREE;
3359 }
3360 \f
3361 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3362    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3363
3364 static tree
3365 make_bit_field_ref (location_t loc, tree inner, tree type,
3366                     HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3367 {
3368   tree result, bftype;
3369
3370   if (bitpos == 0)
3371     {
3372       tree size = TYPE_SIZE (TREE_TYPE (inner));
3373       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3374            || POINTER_TYPE_P (TREE_TYPE (inner)))
3375           && host_integerp (size, 0)
3376           && tree_low_cst (size, 0) == bitsize)
3377         return fold_convert_loc (loc, type, inner);
3378     }
3379
3380   bftype = type;
3381   if (TYPE_PRECISION (bftype) != bitsize
3382       || TYPE_UNSIGNED (bftype) == !unsignedp)
3383     bftype = build_nonstandard_integer_type (bitsize, 0);
3384
3385   result = build3 (BIT_FIELD_REF, bftype, inner,
3386                    size_int (bitsize), bitsize_int (bitpos));
3387   SET_EXPR_LOCATION (result, loc);
3388
3389   if (bftype != type)
3390     result = fold_convert_loc (loc, type, result);
3391
3392   return result;
3393 }
3394
3395 /* Optimize a bit-field compare.
3396
3397    There are two cases:  First is a compare against a constant and the
3398    second is a comparison of two items where the fields are at the same
3399    bit position relative to the start of a chunk (byte, halfword, word)
3400    large enough to contain it.  In these cases we can avoid the shift
3401    implicit in bitfield extractions.
3402
3403    For constants, we emit a compare of the shifted constant with the
3404    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3405    compared.  For two fields at the same position, we do the ANDs with the
3406    similar mask and compare the result of the ANDs.
3407
3408    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3409    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3410    are the left and right operands of the comparison, respectively.
3411
3412    If the optimization described above can be done, we return the resulting
3413    tree.  Otherwise we return zero.  */
3414
3415 static tree
3416 optimize_bit_field_compare (location_t loc, enum tree_code code,
3417                             tree compare_type, tree lhs, tree rhs)
3418 {
3419   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3420   tree type = TREE_TYPE (lhs);
3421   tree signed_type, unsigned_type;
3422   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3423   enum machine_mode lmode, rmode, nmode;
3424   int lunsignedp, runsignedp;
3425   int lvolatilep = 0, rvolatilep = 0;
3426   tree linner, rinner = NULL_TREE;
3427   tree mask;
3428   tree offset;
3429
3430   /* Get all the information about the extractions being done.  If the bit size
3431      if the same as the size of the underlying object, we aren't doing an
3432      extraction at all and so can do nothing.  We also don't want to
3433      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3434      then will no longer be able to replace it.  */
3435   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3436                                 &lunsignedp, &lvolatilep, false);
3437   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3438       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3439     return 0;
3440
3441  if (!const_p)
3442    {
3443      /* If this is not a constant, we can only do something if bit positions,
3444         sizes, and signedness are the same.  */
3445      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3446                                    &runsignedp, &rvolatilep, false);
3447
3448      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3449          || lunsignedp != runsignedp || offset != 0
3450          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3451        return 0;
3452    }
3453
3454   /* See if we can find a mode to refer to this field.  We should be able to,
3455      but fail if we can't.  */
3456   if (lvolatilep
3457       && GET_MODE_BITSIZE (lmode) > 0
3458       && flag_strict_volatile_bitfields > 0)
3459     nmode = lmode;
3460   else
3461     nmode = get_best_mode (lbitsize, lbitpos,
3462                            const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3463                            : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3464                                   TYPE_ALIGN (TREE_TYPE (rinner))),
3465                            word_mode, lvolatilep || rvolatilep);
3466   if (nmode == VOIDmode)
3467     return 0;
3468
3469   /* Set signed and unsigned types of the precision of this mode for the
3470      shifts below.  */
3471   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3472   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3473
3474   /* Compute the bit position and size for the new reference and our offset
3475      within it. If the new reference is the same size as the original, we
3476      won't optimize anything, so return zero.  */
3477   nbitsize = GET_MODE_BITSIZE (nmode);
3478   nbitpos = lbitpos & ~ (nbitsize - 1);
3479   lbitpos -= nbitpos;
3480   if (nbitsize == lbitsize)
3481     return 0;
3482
3483   if (BYTES_BIG_ENDIAN)
3484     lbitpos = nbitsize - lbitsize - lbitpos;
3485
3486   /* Make the mask to be used against the extracted field.  */
3487   mask = build_int_cst_type (unsigned_type, -1);
3488   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3489   mask = const_binop (RSHIFT_EXPR, mask,
3490                       size_int (nbitsize - lbitsize - lbitpos));
3491
3492   if (! const_p)
3493     /* If not comparing with constant, just rework the comparison
3494        and return.  */
3495     return fold_build2_loc (loc, code, compare_type,
3496                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3497                                      make_bit_field_ref (loc, linner,
3498                                                          unsigned_type,
3499                                                          nbitsize, nbitpos,
3500                                                          1),
3501                                      mask),
3502                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3503                                      make_bit_field_ref (loc, rinner,
3504                                                          unsigned_type,
3505                                                          nbitsize, nbitpos,
3506                                                          1),
3507                                      mask));
3508
3509   /* Otherwise, we are handling the constant case. See if the constant is too
3510      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
3511      this not only for its own sake, but to avoid having to test for this
3512      error case below.  If we didn't, we might generate wrong code.
3513
3514      For unsigned fields, the constant shifted right by the field length should
3515      be all zero.  For signed fields, the high-order bits should agree with
3516      the sign bit.  */
3517
3518   if (lunsignedp)
3519     {
3520       if (! integer_zerop (const_binop (RSHIFT_EXPR,
3521                                         fold_convert_loc (loc,
3522                                                           unsigned_type, rhs),
3523                                         size_int (lbitsize))))
3524         {
3525           warning (0, "comparison is always %d due to width of bit-field",
3526                    code == NE_EXPR);
3527           return constant_boolean_node (code == NE_EXPR, compare_type);
3528         }
3529     }
3530   else
3531     {
3532       tree tem = const_binop (RSHIFT_EXPR,
3533                               fold_convert_loc (loc, signed_type, rhs),
3534                               size_int (lbitsize - 1));
3535       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3536         {
3537           warning (0, "comparison is always %d due to width of bit-field",
3538                    code == NE_EXPR);
3539           return constant_boolean_node (code == NE_EXPR, compare_type);
3540         }
3541     }
3542
3543   /* Single-bit compares should always be against zero.  */
3544   if (lbitsize == 1 && ! integer_zerop (rhs))
3545     {
3546       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3547       rhs = build_int_cst (type, 0);
3548     }
3549
3550   /* Make a new bitfield reference, shift the constant over the
3551      appropriate number of bits and mask it with the computed mask
3552      (in case this was a signed field).  If we changed it, make a new one.  */
3553   lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3554   if (lvolatilep)
3555     {
3556       TREE_SIDE_EFFECTS (lhs) = 1;
3557       TREE_THIS_VOLATILE (lhs) = 1;
3558     }
3559
3560   rhs = const_binop (BIT_AND_EXPR,
3561                      const_binop (LSHIFT_EXPR,
3562                                   fold_convert_loc (loc, unsigned_type, rhs),
3563                                   size_int (lbitpos)),
3564                      mask);
3565
3566   lhs = build2 (code, compare_type,
3567                 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
3568                 rhs);
3569   SET_EXPR_LOCATION (lhs, loc);
3570   return lhs;
3571 }
3572 \f
3573 /* Subroutine for fold_truthop: decode a field reference.
3574
3575    If EXP is a comparison reference, we return the innermost reference.
3576
3577    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3578    set to the starting bit number.
3579
3580    If the innermost field can be completely contained in a mode-sized
3581    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
3582
3583    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3584    otherwise it is not changed.
3585
3586    *PUNSIGNEDP is set to the signedness of the field.
3587
3588    *PMASK is set to the mask used.  This is either contained in a
3589    BIT_AND_EXPR or derived from the width of the field.
3590
3591    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3592
3593    Return 0 if this is not a component reference or is one that we can't
3594    do anything with.  */
3595
3596 static tree
3597 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3598                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3599                         int *punsignedp, int *pvolatilep,
3600                         tree *pmask, tree *pand_mask)
3601 {
3602   tree outer_type = 0;
3603   tree and_mask = 0;
3604   tree mask, inner, offset;
3605   tree unsigned_type;
3606   unsigned int precision;
3607
3608   /* All the optimizations using this function assume integer fields.
3609      There are problems with FP fields since the type_for_size call
3610      below can fail for, e.g., XFmode.  */
3611   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3612     return 0;
3613
3614   /* We are interested in the bare arrangement of bits, so strip everything
3615      that doesn't affect the machine mode.  However, record the type of the
3616      outermost expression if it may matter below.  */
3617   if (CONVERT_EXPR_P (exp)
3618       || TREE_CODE (exp) == NON_LVALUE_EXPR)
3619     outer_type = TREE_TYPE (exp);
3620   STRIP_NOPS (exp);
3621
3622   if (TREE_CODE (exp) == BIT_AND_EXPR)
3623     {
3624       and_mask = TREE_OPERAND (exp, 1);
3625       exp = TREE_OPERAND (exp, 0);
3626       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3627       if (TREE_CODE (and_mask) != INTEGER_CST)
3628         return 0;
3629     }
3630
3631   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3632                                punsignedp, pvolatilep, false);
3633   if ((inner == exp && and_mask == 0)
3634       || *pbitsize < 0 || offset != 0
3635       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3636     return 0;
3637
3638   /* If the number of bits in the reference is the same as the bitsize of
3639      the outer type, then the outer type gives the signedness. Otherwise
3640      (in case of a small bitfield) the signedness is unchanged.  */
3641   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3642     *punsignedp = TYPE_UNSIGNED (outer_type);
3643
3644   /* Compute the mask to access the bitfield.  */
3645   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3646   precision = TYPE_PRECISION (unsigned_type);
3647
3648   mask = build_int_cst_type (unsigned_type, -1);
3649
3650   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3651   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3652
3653   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
3654   if (and_mask != 0)
3655     mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3656                         fold_convert_loc (loc, unsigned_type, and_mask), mask);
3657
3658   *pmask = mask;
3659   *pand_mask = and_mask;
3660   return inner;
3661 }
3662
3663 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3664    bit positions.  */
3665
3666 static int
3667 all_ones_mask_p (const_tree mask, int size)
3668 {
3669   tree type = TREE_TYPE (mask);
3670   unsigned int precision = TYPE_PRECISION (type);
3671   tree tmask;
3672
3673   tmask = build_int_cst_type (signed_type_for (type), -1);
3674
3675   return
3676     tree_int_cst_equal (mask,
3677                         const_binop (RSHIFT_EXPR,
3678                                      const_binop (LSHIFT_EXPR, tmask,
3679                                                   size_int (precision - size)),
3680                                      size_int (precision - size)));
3681 }
3682
3683 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3684    represents the sign bit of EXP's type.  If EXP represents a sign
3685    or zero extension, also test VAL against the unextended type.
3686    The return value is the (sub)expression whose sign bit is VAL,
3687    or NULL_TREE otherwise.  */
3688
3689 static tree
3690 sign_bit_p (tree exp, const_tree val)
3691 {
3692   unsigned HOST_WIDE_INT mask_lo, lo;
3693   HOST_WIDE_INT mask_hi, hi;
3694   int width;
3695   tree t;
3696
3697   /* Tree EXP must have an integral type.  */
3698   t = TREE_TYPE (exp);
3699   if (! INTEGRAL_TYPE_P (t))
3700     return NULL_TREE;
3701
3702   /* Tree VAL must be an integer constant.  */
3703   if (TREE_CODE (val) != INTEGER_CST
3704       || TREE_OVERFLOW (val))
3705     return NULL_TREE;
3706
3707   width = TYPE_PRECISION (t);
3708   if (width > HOST_BITS_PER_WIDE_INT)
3709     {
3710       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3711       lo = 0;
3712
3713       mask_hi = ((unsigned HOST_WIDE_INT) -1
3714                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
3715       mask_lo = -1;
3716     }
3717   else
3718     {
3719       hi = 0;
3720       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3721
3722       mask_hi = 0;
3723       mask_lo = ((unsigned HOST_WIDE_INT) -1
3724                  >> (HOST_BITS_PER_WIDE_INT - width));
3725     }
3726
3727   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3728      treat VAL as if it were unsigned.  */
3729   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3730       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3731     return exp;
3732
3733   /* Handle extension from a narrower type.  */
3734   if (TREE_CODE (exp) == NOP_EXPR
3735       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3736     return sign_bit_p (TREE_OPERAND (exp, 0), val);
3737
3738   return NULL_TREE;
3739 }
3740
3741 /* Subroutine for fold_truthop: determine if an operand is simple enough
3742    to be evaluated unconditionally.  */
3743
3744 static int
3745 simple_operand_p (const_tree exp)
3746 {
3747   /* Strip any conversions that don't change the machine mode.  */
3748   STRIP_NOPS (exp);
3749
3750   return (CONSTANT_CLASS_P (exp)
3751           || TREE_CODE (exp) == SSA_NAME
3752           || (DECL_P (exp)
3753               && ! TREE_ADDRESSABLE (exp)
3754               && ! TREE_THIS_VOLATILE (exp)
3755               && ! DECL_NONLOCAL (exp)
3756               /* Don't regard global variables as simple.  They may be
3757                  allocated in ways unknown to the compiler (shared memory,
3758                  #pragma weak, etc).  */
3759               && ! TREE_PUBLIC (exp)
3760               && ! DECL_EXTERNAL (exp)
3761               /* Loading a static variable is unduly expensive, but global
3762                  registers aren't expensive.  */
3763               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3764 }
3765 \f
3766 /* The following functions are subroutines to fold_range_test and allow it to
3767    try to change a logical combination of comparisons into a range test.
3768
3769    For example, both
3770         X == 2 || X == 3 || X == 4 || X == 5
3771    and
3772         X >= 2 && X <= 5
3773    are converted to
3774         (unsigned) (X - 2) <= 3
3775
3776    We describe each set of comparisons as being either inside or outside
3777    a range, using a variable named like IN_P, and then describe the
3778    range with a lower and upper bound.  If one of the bounds is omitted,
3779    it represents either the highest or lowest value of the type.
3780
3781    In the comments below, we represent a range by two numbers in brackets
3782    preceded by a "+" to designate being inside that range, or a "-" to
3783    designate being outside that range, so the condition can be inverted by
3784    flipping the prefix.  An omitted bound is represented by a "-".  For
3785    example, "- [-, 10]" means being outside the range starting at the lowest
3786    possible value and ending at 10, in other words, being greater than 10.
3787    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3788    always false.
3789
3790    We set up things so that the missing bounds are handled in a consistent
3791    manner so neither a missing bound nor "true" and "false" need to be
3792    handled using a special case.  */
3793
3794 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3795    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3796    and UPPER1_P are nonzero if the respective argument is an upper bound
3797    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
3798    must be specified for a comparison.  ARG1 will be converted to ARG0's
3799    type if both are specified.  */
3800
3801 static tree
3802 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3803              tree arg1, int upper1_p)
3804 {
3805   tree tem;
3806   int result;
3807   int sgn0, sgn1;
3808
3809   /* If neither arg represents infinity, do the normal operation.
3810      Else, if not a comparison, return infinity.  Else handle the special
3811      comparison rules. Note that most of the cases below won't occur, but
3812      are handled for consistency.  */
3813
3814   if (arg0 != 0 && arg1 != 0)
3815     {
3816       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3817                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
3818       STRIP_NOPS (tem);
3819       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3820     }
3821
3822   if (TREE_CODE_CLASS (code) != tcc_comparison)
3823     return 0;
3824
3825   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3826      for neither.  In real maths, we cannot assume open ended ranges are
3827      the same. But, this is computer arithmetic, where numbers are finite.
3828      We can therefore make the transformation of any unbounded range with
3829      the value Z, Z being greater than any representable number. This permits
3830      us to treat unbounded ranges as equal.  */
3831   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3832   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3833   switch (code)
3834     {
3835     case EQ_EXPR:
3836       result = sgn0 == sgn1;
3837       break;
3838     case NE_EXPR:
3839       result = sgn0 != sgn1;
3840       break;
3841     case LT_EXPR:
3842       result = sgn0 < sgn1;
3843       break;
3844     case LE_EXPR:
3845       result = sgn0 <= sgn1;
3846       break;
3847     case GT_EXPR:
3848       result = sgn0 > sgn1;
3849       break;
3850     case GE_EXPR:
3851       result = sgn0 >= sgn1;
3852       break;
3853     default:
3854       gcc_unreachable ();
3855     }
3856
3857   return constant_boolean_node (result, type);
3858 }
3859 \f
3860 /* Given EXP, a logical expression, set the range it is testing into
3861    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
3862    actually being tested.  *PLOW and *PHIGH will be made of the same
3863    type as the returned expression.  If EXP is not a comparison, we
3864    will most likely not be returning a useful value and range.  Set
3865    *STRICT_OVERFLOW_P to true if the return value is only valid
3866    because signed overflow is undefined; otherwise, do not change
3867    *STRICT_OVERFLOW_P.  */
3868
3869 tree
3870 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
3871             bool *strict_overflow_p)
3872 {
3873   enum tree_code code;
3874   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
3875   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
3876   int in_p, n_in_p;
3877   tree low, high, n_low, n_high;
3878   location_t loc = EXPR_LOCATION (exp);
3879
3880   /* Start with simply saying "EXP != 0" and then look at the code of EXP
3881      and see if we can refine the range.  Some of the cases below may not
3882      happen, but it doesn't seem worth worrying about this.  We "continue"
3883      the outer loop when we've changed something; otherwise we "break"
3884      the switch, which will "break" the while.  */
3885
3886   in_p = 0;
3887   low = high = build_int_cst (TREE_TYPE (exp), 0);
3888
3889   while (1)
3890     {
3891       code = TREE_CODE (exp);
3892       exp_type = TREE_TYPE (exp);
3893
3894       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3895         {
3896           if (TREE_OPERAND_LENGTH (exp) > 0)
3897             arg0 = TREE_OPERAND (exp, 0);
3898           if (TREE_CODE_CLASS (code) == tcc_comparison
3899               || TREE_CODE_CLASS (code) == tcc_unary
3900               || TREE_CODE_CLASS (code) == tcc_binary)
3901             arg0_type = TREE_TYPE (arg0);
3902           if (TREE_CODE_CLASS (code) == tcc_binary
3903               || TREE_CODE_CLASS (code) == tcc_comparison
3904               || (TREE_CODE_CLASS (code) == tcc_expression
3905                   && TREE_OPERAND_LENGTH (exp) > 1))
3906             arg1 = TREE_OPERAND (exp, 1);
3907         }
3908
3909       switch (code)
3910         {
3911         case TRUTH_NOT_EXPR:
3912           in_p = ! in_p, exp = arg0;
3913           continue;
3914
3915         case EQ_EXPR: case NE_EXPR:
3916         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3917           /* We can only do something if the range is testing for zero
3918              and if the second operand is an integer constant.  Note that
3919              saying something is "in" the range we make is done by
3920              complementing IN_P since it will set in the initial case of
3921              being not equal to zero; "out" is leaving it alone.  */
3922           if (low == 0 || high == 0
3923               || ! integer_zerop (low) || ! integer_zerop (high)
3924               || TREE_CODE (arg1) != INTEGER_CST)
3925             break;
3926
3927           switch (code)
3928             {
3929             case NE_EXPR:  /* - [c, c]  */
3930               low = high = arg1;
3931               break;
3932             case EQ_EXPR:  /* + [c, c]  */
3933               in_p = ! in_p, low = high = arg1;
3934               break;
3935             case GT_EXPR:  /* - [-, c] */
3936               low = 0, high = arg1;
3937               break;
3938             case GE_EXPR:  /* + [c, -] */
3939               in_p = ! in_p, low = arg1, high = 0;
3940               break;
3941             case LT_EXPR:  /* - [c, -] */
3942               low = arg1, high = 0;
3943               break;
3944             case LE_EXPR:  /* + [-, c] */
3945               in_p = ! in_p, low = 0, high = arg1;
3946               break;
3947             default:
3948               gcc_unreachable ();
3949             }
3950
3951           /* If this is an unsigned comparison, we also know that EXP is
3952              greater than or equal to zero.  We base the range tests we make
3953              on that fact, so we record it here so we can parse existing
3954              range tests.  We test arg0_type since often the return type
3955              of, e.g. EQ_EXPR, is boolean.  */
3956           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3957             {
3958               if (! merge_ranges (&n_in_p, &n_low, &n_high,
3959                                   in_p, low, high, 1,
3960                                   build_int_cst (arg0_type, 0),
3961                                   NULL_TREE))
3962                 break;
3963
3964               in_p = n_in_p, low = n_low, high = n_high;
3965
3966               /* If the high bound is missing, but we have a nonzero low
3967                  bound, reverse the range so it goes from zero to the low bound
3968                  minus 1.  */
3969               if (high == 0 && low && ! integer_zerop (low))
3970                 {
3971                   in_p = ! in_p;
3972                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3973                                       integer_one_node, 0);
3974                   low = build_int_cst (arg0_type, 0);
3975                 }
3976             }
3977
3978           exp = arg0;
3979           continue;
3980
3981         case NEGATE_EXPR:
3982           /* (-x) IN [a,b] -> x in [-b, -a]  */
3983           n_low = range_binop (MINUS_EXPR, exp_type,
3984                                build_int_cst (exp_type, 0),
3985                                0, high, 1);
3986           n_high = range_binop (MINUS_EXPR, exp_type,
3987                                 build_int_cst (exp_type, 0),
3988                                 0, low, 0);
3989           if (n_high != 0 && TREE_OVERFLOW (n_high))
3990             break;
3991           goto normalize;
3992
3993         case BIT_NOT_EXPR:
3994           /* ~ X -> -X - 1  */
3995           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
3996                         build_int_cst (exp_type, 1));
3997           SET_EXPR_LOCATION (exp, loc);
3998           continue;
3999
4000         case PLUS_EXPR:  case MINUS_EXPR:
4001           if (TREE_CODE (arg1) != INTEGER_CST)
4002             break;
4003
4004           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4005              move a constant to the other side.  */
4006           if (!TYPE_UNSIGNED (arg0_type)
4007               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4008             break;
4009
4010           /* If EXP is signed, any overflow in the computation is undefined,
4011              so we don't worry about it so long as our computations on
4012              the bounds don't overflow.  For unsigned, overflow is defined
4013              and this is exactly the right thing.  */
4014           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4015                                arg0_type, low, 0, arg1, 0);
4016           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4017                                 arg0_type, high, 1, arg1, 0);
4018           if ((n_low != 0 && TREE_OVERFLOW (n_low))
4019               || (n_high != 0 && TREE_OVERFLOW (n_high)))
4020             break;
4021
4022           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4023             *strict_overflow_p = true;
4024
4025         normalize:
4026           /* Check for an unsigned range which has wrapped around the maximum
4027              value thus making n_high < n_low, and normalize it.  */
4028           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4029             {
4030               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4031                                  integer_one_node, 0);
4032               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4033                                   integer_one_node, 0);
4034
4035               /* If the range is of the form +/- [ x+1, x ], we won't
4036                  be able to normalize it.  But then, it represents the
4037                  whole range or the empty set, so make it
4038                  +/- [ -, - ].  */
4039               if (tree_int_cst_equal (n_low, low)
4040                   && tree_int_cst_equal (n_high, high))
4041                 low = high = 0;
4042               else
4043                 in_p = ! in_p;
4044             }
4045           else
4046             low = n_low, high = n_high;
4047
4048           exp = arg0;
4049           continue;
4050
4051         CASE_CONVERT: case NON_LVALUE_EXPR:
4052           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4053             break;
4054
4055           if (! INTEGRAL_TYPE_P (arg0_type)
4056               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4057               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4058             break;
4059
4060           n_low = low, n_high = high;
4061
4062           if (n_low != 0)
4063             n_low = fold_convert_loc (loc, arg0_type, n_low);
4064
4065           if (n_high != 0)
4066             n_high = fold_convert_loc (loc, arg0_type, n_high);
4067
4068
4069           /* If we're converting arg0 from an unsigned type, to exp,
4070              a signed type,  we will be doing the comparison as unsigned.
4071              The tests above have already verified that LOW and HIGH
4072              are both positive.
4073
4074              So we have to ensure that we will handle large unsigned
4075              values the same way that the current signed bounds treat
4076              negative values.  */
4077
4078           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4079             {
4080               tree high_positive;
4081               tree equiv_type;
4082               /* For fixed-point modes, we need to pass the saturating flag
4083                  as the 2nd parameter.  */
4084               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4085                 equiv_type = lang_hooks.types.type_for_mode
4086                              (TYPE_MODE (arg0_type),
4087                               TYPE_SATURATING (arg0_type));
4088               else
4089                 equiv_type = lang_hooks.types.type_for_mode
4090                              (TYPE_MODE (arg0_type), 1);
4091
4092               /* A range without an upper bound is, naturally, unbounded.
4093                  Since convert would have cropped a very large value, use
4094                  the max value for the destination type.  */
4095               high_positive
4096                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4097                 : TYPE_MAX_VALUE (arg0_type);
4098
4099               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4100                 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4101                                              fold_convert_loc (loc, arg0_type,
4102                                                                high_positive),
4103                                              build_int_cst (arg0_type, 1));
4104
4105               /* If the low bound is specified, "and" the range with the
4106                  range for which the original unsigned value will be
4107                  positive.  */
4108               if (low != 0)
4109                 {
4110                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4111                                       1, n_low, n_high, 1,
4112                                       fold_convert_loc (loc, arg0_type,
4113                                                         integer_zero_node),
4114                                       high_positive))
4115                     break;
4116
4117                   in_p = (n_in_p == in_p);
4118                 }
4119               else
4120                 {
4121                   /* Otherwise, "or" the range with the range of the input
4122                      that will be interpreted as negative.  */
4123                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4124                                       0, n_low, n_high, 1,
4125                                       fold_convert_loc (loc, arg0_type,
4126                                                         integer_zero_node),
4127                                       high_positive))
4128                     break;
4129
4130                   in_p = (in_p != n_in_p);
4131                 }
4132             }
4133
4134           exp = arg0;
4135           low = n_low, high = n_high;
4136           continue;
4137
4138         default:
4139           break;
4140         }
4141
4142       break;
4143     }
4144
4145   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4146   if (TREE_CODE (exp) == INTEGER_CST)
4147     {
4148       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4149                                                  exp, 0, low, 0))
4150                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4151                                                     exp, 1, high, 1)));
4152       low = high = 0;
4153       exp = 0;
4154     }
4155
4156   *pin_p = in_p, *plow = low, *phigh = high;
4157   return exp;
4158 }
4159 \f
4160 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4161    type, TYPE, return an expression to test if EXP is in (or out of, depending
4162    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4163
4164 tree
4165 build_range_check (location_t loc, tree type, tree exp, int in_p,
4166                    tree low, tree high)
4167 {
4168   tree etype = TREE_TYPE (exp), value;
4169
4170 #ifdef HAVE_canonicalize_funcptr_for_compare
4171   /* Disable this optimization for function pointer expressions
4172      on targets that require function pointer canonicalization.  */
4173   if (HAVE_canonicalize_funcptr_for_compare
4174       && TREE_CODE (etype) == POINTER_TYPE
4175       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4176     return NULL_TREE;
4177 #endif
4178
4179   if (! in_p)
4180     {
4181       value = build_range_check (loc, type, exp, 1, low, high);
4182       if (value != 0)
4183         return invert_truthvalue_loc (loc, value);
4184
4185       return 0;
4186     }
4187
4188   if (low == 0 && high == 0)
4189     return build_int_cst (type, 1);
4190
4191   if (low == 0)
4192     return fold_build2_loc (loc, LE_EXPR, type, exp,
4193                         fold_convert_loc (loc, etype, high));
4194
4195   if (high == 0)
4196     return fold_build2_loc (loc, GE_EXPR, type, exp,
4197                         fold_convert_loc (loc, etype, low));
4198
4199   if (operand_equal_p (low, high, 0))
4200     return fold_build2_loc (loc, EQ_EXPR, type, exp,
4201                         fold_convert_loc (loc, etype, low));
4202
4203   if (integer_zerop (low))
4204     {
4205       if (! TYPE_UNSIGNED (etype))
4206         {
4207           etype = unsigned_type_for (etype);
4208           high = fold_convert_loc (loc, etype, high);
4209           exp = fold_convert_loc (loc, etype, exp);
4210         }
4211       return build_range_check (loc, type, exp, 1, 0, high);
4212     }
4213
4214   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4215   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4216     {
4217       unsigned HOST_WIDE_INT lo;
4218       HOST_WIDE_INT hi;
4219       int prec;
4220
4221       prec = TYPE_PRECISION (etype);
4222       if (prec <= HOST_BITS_PER_WIDE_INT)
4223         {
4224           hi = 0;
4225           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4226         }
4227       else
4228         {
4229           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4230           lo = (unsigned HOST_WIDE_INT) -1;
4231         }
4232
4233       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4234         {
4235           if (TYPE_UNSIGNED (etype))
4236             {
4237               tree signed_etype = signed_type_for (etype);
4238               if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4239                 etype
4240                   = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4241               else
4242                 etype = signed_etype;
4243               exp = fold_convert_loc (loc, etype, exp);
4244             }
4245           return fold_build2_loc (loc, GT_EXPR, type, exp,
4246                               build_int_cst (etype, 0));
4247         }
4248     }
4249
4250   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4251      This requires wrap-around arithmetics for the type of the expression.
4252      First make sure that arithmetics in this type is valid, then make sure
4253      that it wraps around.  */
4254   if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4255     etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4256                                             TYPE_UNSIGNED (etype));
4257
4258   if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4259     {
4260       tree utype, minv, maxv;
4261
4262       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4263          for the type in question, as we rely on this here.  */
4264       utype = unsigned_type_for (etype);
4265       maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4266       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4267                           integer_one_node, 1);
4268       minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4269
4270       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4271                                       minv, 1, maxv, 1)))
4272         etype = utype;
4273       else
4274         return 0;
4275     }
4276
4277   high = fold_convert_loc (loc, etype, high);
4278   low = fold_convert_loc (loc, etype, low);
4279   exp = fold_convert_loc (loc, etype, exp);
4280
4281   value = const_binop (MINUS_EXPR, high, low);
4282
4283
4284   if (POINTER_TYPE_P (etype))
4285     {
4286       if (value != 0 && !TREE_OVERFLOW (value))
4287         {
4288           low = fold_convert_loc (loc, sizetype, low);
4289           low = fold_build1_loc (loc, NEGATE_EXPR, sizetype, low);
4290           return build_range_check (loc, type,
4291                                     fold_build2_loc (loc, POINTER_PLUS_EXPR,
4292                                                  etype, exp, low),
4293                                     1, build_int_cst (etype, 0), value);
4294         }
4295       return 0;
4296     }
4297
4298   if (value != 0 && !TREE_OVERFLOW (value))
4299     return build_range_check (loc, type,
4300                               fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4301                               1, build_int_cst (etype, 0), value);
4302
4303   return 0;
4304 }
4305 \f
4306 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4307
4308 static tree
4309 range_predecessor (tree val)
4310 {
4311   tree type = TREE_TYPE (val);
4312
4313   if (INTEGRAL_TYPE_P (type)
4314       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4315     return 0;
4316   else
4317     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4318 }
4319
4320 /* Return the successor of VAL in its type, handling the infinite case.  */
4321
4322 static tree
4323 range_successor (tree val)
4324 {
4325   tree type = TREE_TYPE (val);
4326
4327   if (INTEGRAL_TYPE_P (type)
4328       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4329     return 0;
4330   else
4331     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4332 }
4333
4334 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4335    can, 0 if we can't.  Set the output range into the specified parameters.  */
4336
4337 bool
4338 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4339               tree high0, int in1_p, tree low1, tree high1)
4340 {
4341   int no_overlap;
4342   int subset;
4343   int temp;
4344   tree tem;
4345   int in_p;
4346   tree low, high;
4347   int lowequal = ((low0 == 0 && low1 == 0)
4348                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4349                                                 low0, 0, low1, 0)));
4350   int highequal = ((high0 == 0 && high1 == 0)
4351                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4352                                                  high0, 1, high1, 1)));
4353
4354   /* Make range 0 be the range that starts first, or ends last if they
4355      start at the same value.  Swap them if it isn't.  */
4356   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4357                                  low0, 0, low1, 0))
4358       || (lowequal
4359           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4360                                         high1, 1, high0, 1))))
4361     {
4362       temp = in0_p, in0_p = in1_p, in1_p = temp;
4363       tem = low0, low0 = low1, low1 = tem;
4364       tem = high0, high0 = high1, high1 = tem;
4365     }
4366
4367   /* Now flag two cases, whether the ranges are disjoint or whether the
4368      second range is totally subsumed in the first.  Note that the tests
4369      below are simplified by the ones above.  */
4370   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4371                                           high0, 1, low1, 0));
4372   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4373                                       high1, 1, high0, 1));
4374
4375   /* We now have four cases, depending on whether we are including or
4376      excluding the two ranges.  */
4377   if (in0_p && in1_p)
4378     {
4379       /* If they don't overlap, the result is false.  If the second range
4380          is a subset it is the result.  Otherwise, the range is from the start
4381          of the second to the end of the first.  */
4382       if (no_overlap)
4383         in_p = 0, low = high = 0;
4384       else if (subset)
4385         in_p = 1, low = low1, high = high1;
4386       else
4387         in_p = 1, low = low1, high = high0;
4388     }
4389
4390   else if (in0_p && ! in1_p)
4391     {
4392       /* If they don't overlap, the result is the first range.  If they are
4393          equal, the result is false.  If the second range is a subset of the
4394          first, and the ranges begin at the same place, we go from just after
4395          the end of the second range to the end of the first.  If the second
4396          range is not a subset of the first, or if it is a subset and both
4397          ranges end at the same place, the range starts at the start of the
4398          first range and ends just before the second range.
4399          Otherwise, we can't describe this as a single range.  */
4400       if (no_overlap)
4401         in_p = 1, low = low0, high = high0;
4402       else if (lowequal && highequal)
4403         in_p = 0, low = high = 0;
4404       else if (subset && lowequal)
4405         {
4406           low = range_successor (high1);
4407           high = high0;
4408           in_p = 1;
4409           if (low == 0)
4410             {
4411               /* We are in the weird situation where high0 > high1 but
4412                  high1 has no successor.  Punt.  */
4413               return 0;
4414             }
4415         }
4416       else if (! subset || highequal)
4417         {
4418           low = low0;
4419           high = range_predecessor (low1);
4420           in_p = 1;
4421           if (high == 0)
4422             {
4423               /* low0 < low1 but low1 has no predecessor.  Punt.  */
4424               return 0;
4425             }
4426         }
4427       else
4428         return 0;
4429     }
4430
4431   else if (! in0_p && in1_p)
4432     {
4433       /* If they don't overlap, the result is the second range.  If the second
4434          is a subset of the first, the result is false.  Otherwise,
4435          the range starts just after the first range and ends at the
4436          end of the second.  */
4437       if (no_overlap)
4438         in_p = 1, low = low1, high = high1;
4439       else if (subset || highequal)
4440         in_p = 0, low = high = 0;
4441       else
4442         {
4443           low = range_successor (high0);
4444           high = high1;
4445           in_p = 1;
4446           if (low == 0)
4447             {
4448               /* high1 > high0 but high0 has no successor.  Punt.  */
4449               return 0;
4450             }
4451         }
4452     }
4453
4454   else
4455     {
4456       /* The case where we are excluding both ranges.  Here the complex case
4457          is if they don't overlap.  In that case, the only time we have a
4458          range is if they are adjacent.  If the second is a subset of the
4459          first, the result is the first.  Otherwise, the range to exclude
4460          starts at the beginning of the first range and ends at the end of the
4461          second.  */
4462       if (no_overlap)
4463         {
4464           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4465                                          range_successor (high0),
4466                                          1, low1, 0)))
4467             in_p = 0, low = low0, high = high1;
4468           else
4469             {
4470               /* Canonicalize - [min, x] into - [-, x].  */
4471               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4472                 switch (TREE_CODE (TREE_TYPE (low0)))
4473                   {
4474                   case ENUMERAL_TYPE:
4475                     if (TYPE_PRECISION (TREE_TYPE (low0))
4476                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4477                       break;
4478                     /* FALLTHROUGH */
4479                   case INTEGER_TYPE:
4480                     if (tree_int_cst_equal (low0,
4481                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4482                       low0 = 0;
4483                     break;
4484                   case POINTER_TYPE:
4485                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4486                         && integer_zerop (low0))
4487                       low0 = 0;
4488                     break;
4489                   default:
4490                     break;
4491                   }
4492
4493               /* Canonicalize - [x, max] into - [x, -].  */
4494               if (high1 && TREE_CODE (high1) == INTEGER_CST)
4495                 switch (TREE_CODE (TREE_TYPE (high1)))
4496                   {
4497                   case ENUMERAL_TYPE:
4498                     if (TYPE_PRECISION (TREE_TYPE (high1))
4499                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4500                       break;
4501                     /* FALLTHROUGH */
4502                   case INTEGER_TYPE:
4503                     if (tree_int_cst_equal (high1,
4504                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
4505                       high1 = 0;
4506                     break;
4507                   case POINTER_TYPE:
4508                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
4509                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4510                                                        high1, 1,
4511                                                        integer_one_node, 1)))
4512                       high1 = 0;
4513                     break;
4514                   default:
4515                     break;
4516                   }
4517
4518               /* The ranges might be also adjacent between the maximum and
4519                  minimum values of the given type.  For
4520                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4521                  return + [x + 1, y - 1].  */
4522               if (low0 == 0 && high1 == 0)
4523                 {
4524                   low = range_successor (high0);
4525                   high = range_predecessor (low1);
4526                   if (low == 0 || high == 0)
4527                     return 0;
4528
4529                   in_p = 1;
4530                 }
4531               else
4532                 return 0;
4533             }
4534         }
4535       else if (subset)
4536         in_p = 0, low = low0, high = high0;
4537       else
4538         in_p = 0, low = low0, high = high1;
4539     }
4540
4541   *pin_p = in_p, *plow = low, *phigh = high;
4542   return 1;
4543 }
4544 \f
4545
4546 /* Subroutine of fold, looking inside expressions of the form
4547    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4548    of the COND_EXPR.  This function is being used also to optimize
4549    A op B ? C : A, by reversing the comparison first.
4550
4551    Return a folded expression whose code is not a COND_EXPR
4552    anymore, or NULL_TREE if no folding opportunity is found.  */
4553
4554 static tree
4555 fold_cond_expr_with_comparison (location_t loc, tree type,
4556                                 tree arg0, tree arg1, tree arg2)
4557 {
4558   enum tree_code comp_code = TREE_CODE (arg0);
4559   tree arg00 = TREE_OPERAND (arg0, 0);
4560   tree arg01 = TREE_OPERAND (arg0, 1);
4561   tree arg1_type = TREE_TYPE (arg1);
4562   tree tem;
4563
4564   STRIP_NOPS (arg1);
4565   STRIP_NOPS (arg2);
4566
4567   /* If we have A op 0 ? A : -A, consider applying the following
4568      transformations:
4569
4570      A == 0? A : -A    same as -A
4571      A != 0? A : -A    same as A
4572      A >= 0? A : -A    same as abs (A)
4573      A > 0?  A : -A    same as abs (A)
4574      A <= 0? A : -A    same as -abs (A)
4575      A < 0?  A : -A    same as -abs (A)
4576
4577      None of these transformations work for modes with signed
4578      zeros.  If A is +/-0, the first two transformations will
4579      change the sign of the result (from +0 to -0, or vice
4580      versa).  The last four will fix the sign of the result,
4581      even though the original expressions could be positive or
4582      negative, depending on the sign of A.
4583
4584      Note that all these transformations are correct if A is
4585      NaN, since the two alternatives (A and -A) are also NaNs.  */
4586   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4587       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4588           ? real_zerop (arg01)
4589           : integer_zerop (arg01))
4590       && ((TREE_CODE (arg2) == NEGATE_EXPR
4591            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4592              /* In the case that A is of the form X-Y, '-A' (arg2) may
4593                 have already been folded to Y-X, check for that. */
4594           || (TREE_CODE (arg1) == MINUS_EXPR
4595               && TREE_CODE (arg2) == MINUS_EXPR
4596               && operand_equal_p (TREE_OPERAND (arg1, 0),
4597                                   TREE_OPERAND (arg2, 1), 0)
4598               && operand_equal_p (TREE_OPERAND (arg1, 1),
4599                                   TREE_OPERAND (arg2, 0), 0))))
4600     switch (comp_code)
4601       {
4602       case EQ_EXPR:
4603       case UNEQ_EXPR:
4604         tem = fold_convert_loc (loc, arg1_type, arg1);
4605         return pedantic_non_lvalue_loc (loc,
4606                                     fold_convert_loc (loc, type,
4607                                                   negate_expr (tem)));
4608       case NE_EXPR:
4609       case LTGT_EXPR:
4610         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4611       case UNGE_EXPR:
4612       case UNGT_EXPR:
4613         if (flag_trapping_math)
4614           break;
4615         /* Fall through.  */
4616       case GE_EXPR:
4617       case GT_EXPR:
4618         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4619           arg1 = fold_convert_loc (loc, signed_type_for
4620                                (TREE_TYPE (arg1)), arg1);
4621         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4622         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4623       case UNLE_EXPR:
4624       case UNLT_EXPR:
4625         if (flag_trapping_math)
4626           break;
4627       case LE_EXPR:
4628       case LT_EXPR:
4629         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4630           arg1 = fold_convert_loc (loc, signed_type_for
4631                                (TREE_TYPE (arg1)), arg1);
4632         tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4633         return negate_expr (fold_convert_loc (loc, type, tem));
4634       default:
4635         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4636         break;
4637       }
4638
4639   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
4640      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
4641      both transformations are correct when A is NaN: A != 0
4642      is then true, and A == 0 is false.  */
4643
4644   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4645       && integer_zerop (arg01) && integer_zerop (arg2))
4646     {
4647       if (comp_code == NE_EXPR)
4648         return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4649       else if (comp_code == EQ_EXPR)
4650         return build_int_cst (type, 0);
4651     }
4652
4653   /* Try some transformations of A op B ? A : B.
4654
4655      A == B? A : B    same as B
4656      A != B? A : B    same as A
4657      A >= B? A : B    same as max (A, B)
4658      A > B?  A : B    same as max (B, A)
4659      A <= B? A : B    same as min (A, B)
4660      A < B?  A : B    same as min (B, A)
4661
4662      As above, these transformations don't work in the presence
4663      of signed zeros.  For example, if A and B are zeros of
4664      opposite sign, the first two transformations will change
4665      the sign of the result.  In the last four, the original
4666      expressions give different results for (A=+0, B=-0) and
4667      (A=-0, B=+0), but the transformed expressions do not.
4668
4669      The first two transformations are correct if either A or B
4670      is a NaN.  In the first transformation, the condition will
4671      be false, and B will indeed be chosen.  In the case of the
4672      second transformation, the condition A != B will be true,
4673      and A will be chosen.
4674
4675      The conversions to max() and min() are not correct if B is
4676      a number and A is not.  The conditions in the original
4677      expressions will be false, so all four give B.  The min()
4678      and max() versions would give a NaN instead.  */
4679   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4680       && operand_equal_for_comparison_p (arg01, arg2, arg00)
4681       /* Avoid these transformations if the COND_EXPR may be used
4682          as an lvalue in the C++ front-end.  PR c++/19199.  */
4683       && (in_gimple_form
4684           || (strcmp (lang_hooks.name, "GNU C++") != 0
4685               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4686           || ! maybe_lvalue_p (arg1)
4687           || ! maybe_lvalue_p (arg2)))
4688     {
4689       tree comp_op0 = arg00;
4690       tree comp_op1 = arg01;
4691       tree comp_type = TREE_TYPE (comp_op0);
4692
4693       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
4694       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4695         {
4696           comp_type = type;
4697           comp_op0 = arg1;
4698           comp_op1 = arg2;
4699         }
4700
4701       switch (comp_code)
4702         {
4703         case EQ_EXPR:
4704           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
4705         case NE_EXPR:
4706           return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4707         case LE_EXPR:
4708         case LT_EXPR:
4709         case UNLE_EXPR:
4710         case UNLT_EXPR:
4711           /* In C++ a ?: expression can be an lvalue, so put the
4712              operand which will be used if they are equal first
4713              so that we can convert this back to the
4714              corresponding COND_EXPR.  */
4715           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4716             {
4717               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4718               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4719               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4720                     ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
4721                     : fold_build2_loc (loc, MIN_EXPR, comp_type,
4722                                    comp_op1, comp_op0);
4723               return pedantic_non_lvalue_loc (loc,
4724                                           fold_convert_loc (loc, type, tem));
4725             }
4726           break;
4727         case GE_EXPR:
4728         case GT_EXPR:
4729         case UNGE_EXPR:
4730         case UNGT_EXPR:
4731           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4732             {
4733               comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4734               comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4735               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4736                     ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
4737                     : fold_build2_loc (loc, MAX_EXPR, comp_type,
4738                                    comp_op1, comp_op0);
4739               return pedantic_non_lvalue_loc (loc,
4740                                           fold_convert_loc (loc, type, tem));
4741             }
4742           break;
4743         case UNEQ_EXPR:
4744           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4745             return pedantic_non_lvalue_loc (loc,
4746                                         fold_convert_loc (loc, type, arg2));
4747           break;
4748         case LTGT_EXPR:
4749           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4750             return pedantic_non_lvalue_loc (loc,
4751                                         fold_convert_loc (loc, type, arg1));
4752           break;
4753         default:
4754           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4755           break;
4756         }
4757     }
4758
4759   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4760      we might still be able to simplify this.  For example,
4761      if C1 is one less or one more than C2, this might have started
4762      out as a MIN or MAX and been transformed by this function.
4763      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
4764
4765   if (INTEGRAL_TYPE_P (type)
4766       && TREE_CODE (arg01) == INTEGER_CST
4767       && TREE_CODE (arg2) == INTEGER_CST)
4768     switch (comp_code)
4769       {
4770       case EQ_EXPR:
4771         if (TREE_CODE (arg1) == INTEGER_CST)
4772           break;
4773         /* We can replace A with C1 in this case.  */
4774         arg1 = fold_convert_loc (loc, type, arg01);
4775         return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
4776
4777       case LT_EXPR:
4778         /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
4779            MIN_EXPR, to preserve the signedness of the comparison.  */
4780         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4781                                OEP_ONLY_CONST)
4782             && operand_equal_p (arg01,
4783                                 const_binop (PLUS_EXPR, arg2,
4784                                              build_int_cst (type, 1)),
4785                                 OEP_ONLY_CONST))
4786           {
4787             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4788                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4789                                                      arg2));
4790             return pedantic_non_lvalue_loc (loc,
4791                                             fold_convert_loc (loc, type, tem));
4792           }
4793         break;
4794
4795       case LE_EXPR:
4796         /* If C1 is C2 - 1, this is min(A, C2), with the same care
4797            as above.  */
4798         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4799                                OEP_ONLY_CONST)
4800             && operand_equal_p (arg01,
4801                                 const_binop (MINUS_EXPR, arg2,
4802                                              build_int_cst (type, 1)),
4803                                 OEP_ONLY_CONST))
4804           {
4805             tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4806                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4807                                                      arg2));
4808             return pedantic_non_lvalue_loc (loc,
4809                                             fold_convert_loc (loc, type, tem));
4810           }
4811         break;
4812
4813       case GT_EXPR:
4814         /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
4815            MAX_EXPR, to preserve the signedness of the comparison.  */
4816         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4817                                OEP_ONLY_CONST)
4818             && operand_equal_p (arg01,
4819                                 const_binop (MINUS_EXPR, arg2,
4820                                              build_int_cst (type, 1)),
4821                                 OEP_ONLY_CONST))
4822           {
4823             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4824                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4825                                                      arg2));
4826             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4827           }
4828         break;
4829
4830       case GE_EXPR:
4831         /* If C1 is C2 + 1, this is max(A, C2), with the same care as above.  */
4832         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4833                                OEP_ONLY_CONST)
4834             && operand_equal_p (arg01,
4835                                 const_binop (PLUS_EXPR, arg2,
4836                                              build_int_cst (type, 1)),
4837                                 OEP_ONLY_CONST))
4838           {
4839             tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4840                                    fold_convert_loc (loc, TREE_TYPE (arg00),
4841                                                      arg2));
4842             return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4843           }
4844         break;
4845       case NE_EXPR:
4846         break;
4847       default:
4848         gcc_unreachable ();
4849       }
4850
4851   return NULL_TREE;
4852 }
4853
4854
4855 \f
4856 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4857 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
4858   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
4859                 false) >= 2)
4860 #endif
4861
4862 /* EXP is some logical combination of boolean tests.  See if we can
4863    merge it into some range test.  Return the new tree if so.  */
4864
4865 static tree
4866 fold_range_test (location_t loc, enum tree_code code, tree type,
4867                  tree op0, tree op1)
4868 {
4869   int or_op = (code == TRUTH_ORIF_EXPR
4870                || code == TRUTH_OR_EXPR);
4871   int in0_p, in1_p, in_p;
4872   tree low0, low1, low, high0, high1, high;
4873   bool strict_overflow_p = false;
4874   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
4875   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
4876   tree tem;
4877   const char * const warnmsg = G_("assuming signed overflow does not occur "
4878                                   "when simplifying range test");
4879
4880   /* If this is an OR operation, invert both sides; we will invert
4881      again at the end.  */
4882   if (or_op)
4883     in0_p = ! in0_p, in1_p = ! in1_p;
4884
4885   /* If both expressions are the same, if we can merge the ranges, and we
4886      can build the range test, return it or it inverted.  If one of the
4887      ranges is always true or always false, consider it to be the same
4888      expression as the other.  */
4889   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4890       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4891                        in1_p, low1, high1)
4892       && 0 != (tem = (build_range_check (UNKNOWN_LOCATION, type,
4893                                          lhs != 0 ? lhs
4894                                          : rhs != 0 ? rhs : integer_zero_node,
4895                                          in_p, low, high))))
4896     {
4897       if (strict_overflow_p)
4898         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
4899       return or_op ? invert_truthvalue_loc (loc, tem) : tem;
4900     }
4901
4902   /* On machines where the branch cost is expensive, if this is a
4903      short-circuited branch and the underlying object on both sides
4904      is the same, make a non-short-circuit operation.  */
4905   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4906            && lhs != 0 && rhs != 0
4907            && (code == TRUTH_ANDIF_EXPR
4908                || code == TRUTH_ORIF_EXPR)
4909            && operand_equal_p (lhs, rhs, 0))
4910     {
4911       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
4912          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4913          which cases we can't do this.  */
4914       if (simple_operand_p (lhs))
4915         {
4916           tem = build2 (code == TRUTH_ANDIF_EXPR
4917                         ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4918                         type, op0, op1);
4919           SET_EXPR_LOCATION (tem, loc);
4920           return tem;
4921         }
4922
4923       else if (lang_hooks.decls.global_bindings_p () == 0
4924                && ! CONTAINS_PLACEHOLDER_P (lhs))
4925         {
4926           tree common = save_expr (lhs);
4927
4928           if (0 != (lhs = build_range_check (loc, type, common,
4929                                              or_op ? ! in0_p : in0_p,
4930                                              low0, high0))
4931               && (0 != (rhs = build_range_check (loc, type, common,
4932                                                  or_op ? ! in1_p : in1_p,
4933                                                  low1, high1))))
4934             {
4935               if (strict_overflow_p)
4936                 fold_overflow_warning (warnmsg,
4937                                        WARN_STRICT_OVERFLOW_COMPARISON);
4938               tem = build2 (code == TRUTH_ANDIF_EXPR
4939                             ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4940                             type, lhs, rhs);
4941               SET_EXPR_LOCATION (tem, loc);
4942               return tem;
4943             }
4944         }
4945     }
4946
4947   return 0;
4948 }
4949 \f
4950 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4951    bit value.  Arrange things so the extra bits will be set to zero if and
4952    only if C is signed-extended to its full width.  If MASK is nonzero,
4953    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
4954
4955 static tree
4956 unextend (tree c, int p, int unsignedp, tree mask)
4957 {
4958   tree type = TREE_TYPE (c);
4959   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4960   tree temp;
4961
4962   if (p == modesize || unsignedp)
4963     return c;
4964
4965   /* We work by getting just the sign bit into the low-order bit, then
4966      into the high-order bit, then sign-extend.  We then XOR that value
4967      with C.  */
4968   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1));
4969   temp = const_binop (BIT_AND_EXPR, temp, size_int (1));
4970
4971   /* We must use a signed type in order to get an arithmetic right shift.
4972      However, we must also avoid introducing accidental overflows, so that
4973      a subsequent call to integer_zerop will work.  Hence we must
4974      do the type conversion here.  At this point, the constant is either
4975      zero or one, and the conversion to a signed type can never overflow.
4976      We could get an overflow if this conversion is done anywhere else.  */
4977   if (TYPE_UNSIGNED (type))
4978     temp = fold_convert (signed_type_for (type), temp);
4979
4980   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
4981   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
4982   if (mask != 0)
4983     temp = const_binop (BIT_AND_EXPR, temp,
4984                         fold_convert (TREE_TYPE (c), mask));
4985   /* If necessary, convert the type back to match the type of C.  */
4986   if (TYPE_UNSIGNED (type))
4987     temp = fold_convert (type, temp);
4988
4989   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
4990 }
4991 \f
4992 /* For an expression that has the form
4993      (A && B) || ~B
4994    or
4995      (A || B) && ~B,
4996    we can drop one of the inner expressions and simplify to
4997      A || ~B
4998    or
4999      A && ~B
5000    LOC is the location of the resulting expression.  OP is the inner 
5001    logical operation; the left-hand side in the examples above, while CMPOP
5002    is the right-hand side.  RHS_ONLY is used to prevent us from accidentally
5003    removing a condition that guards another, as in
5004      (A != NULL && A->...) || A == NULL
5005    which we must not transform.  If RHS_ONLY is true, only eliminate the
5006    right-most operand of the inner logical operation.  */
5007
5008 static tree
5009 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5010                                  bool rhs_only)
5011 {
5012   tree type = TREE_TYPE (cmpop);
5013   enum tree_code code = TREE_CODE (cmpop);
5014   enum tree_code truthop_code = TREE_CODE (op);
5015   tree lhs = TREE_OPERAND (op, 0);
5016   tree rhs = TREE_OPERAND (op, 1);
5017   tree orig_lhs = lhs, orig_rhs = rhs;
5018   enum tree_code rhs_code = TREE_CODE (rhs);
5019   enum tree_code lhs_code = TREE_CODE (lhs);
5020   enum tree_code inv_code;
5021
5022   if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5023     return NULL_TREE;
5024
5025   if (TREE_CODE_CLASS (code) != tcc_comparison)
5026     return NULL_TREE;
5027
5028   if (rhs_code == truthop_code)
5029     {
5030       tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5031       if (newrhs != NULL_TREE)
5032         {
5033           rhs = newrhs;
5034           rhs_code = TREE_CODE (rhs);
5035         }
5036     }
5037   if (lhs_code == truthop_code && !rhs_only)
5038     {
5039       tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5040       if (newlhs != NULL_TREE)
5041         {
5042           lhs = newlhs;
5043           lhs_code = TREE_CODE (lhs);
5044         }
5045     }
5046
5047   inv_code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
5048   if (inv_code == rhs_code
5049       && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5050       && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5051     return lhs;
5052   if (!rhs_only && inv_code == lhs_code
5053       && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5054       && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5055     return rhs;
5056   if (rhs != orig_rhs || lhs != orig_lhs)
5057     return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5058                             lhs, rhs);
5059   return NULL_TREE;
5060 }
5061
5062 /* Find ways of folding logical expressions of LHS and RHS:
5063    Try to merge two comparisons to the same innermost item.
5064    Look for range tests like "ch >= '0' && ch <= '9'".
5065    Look for combinations of simple terms on machines with expensive branches
5066    and evaluate the RHS unconditionally.
5067
5068    For example, if we have p->a == 2 && p->b == 4 and we can make an
5069    object large enough to span both A and B, we can do this with a comparison
5070    against the object ANDed with the a mask.
5071
5072    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5073    operations to do this with one comparison.
5074
5075    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5076    function and the one above.
5077
5078    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5079    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5080
5081    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5082    two operands.
5083
5084    We return the simplified tree or 0 if no optimization is possible.  */
5085
5086 static tree
5087 fold_truthop (location_t loc, enum tree_code code, tree truth_type,
5088               tree lhs, tree rhs)
5089 {
5090   /* If this is the "or" of two comparisons, we can do something if
5091      the comparisons are NE_EXPR.  If this is the "and", we can do something
5092      if the comparisons are EQ_EXPR.  I.e.,
5093         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5094
5095      WANTED_CODE is this operation code.  For single bit fields, we can
5096      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5097      comparison for one-bit fields.  */
5098
5099   enum tree_code wanted_code;
5100   enum tree_code lcode, rcode;
5101   tree ll_arg, lr_arg, rl_arg, rr_arg;
5102   tree ll_inner, lr_inner, rl_inner, rr_inner;
5103   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5104   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5105   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5106   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5107   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5108   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5109   enum machine_mode lnmode, rnmode;
5110   tree ll_mask, lr_mask, rl_mask, rr_mask;
5111   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5112   tree l_const, r_const;
5113   tree lntype, rntype, result;
5114   HOST_WIDE_INT first_bit, end_bit;
5115   int volatilep;
5116   tree orig_lhs = lhs, orig_rhs = rhs;
5117   enum tree_code orig_code = code;
5118
5119   /* Start by getting the comparison codes.  Fail if anything is volatile.
5120      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5121      it were surrounded with a NE_EXPR.  */
5122
5123   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5124     return 0;
5125
5126   lcode = TREE_CODE (lhs);
5127   rcode = TREE_CODE (rhs);
5128
5129   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5130     {
5131       lhs = build2 (NE_EXPR, truth_type, lhs,
5132                     build_int_cst (TREE_TYPE (lhs), 0));
5133       lcode = NE_EXPR;
5134     }
5135
5136   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5137     {
5138       rhs = build2 (NE_EXPR, truth_type, rhs,
5139                     build_int_cst (TREE_TYPE (rhs), 0));
5140       rcode = NE_EXPR;
5141     }
5142
5143   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5144       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5145     return 0;
5146
5147   ll_arg = TREE_OPERAND (lhs, 0);
5148   lr_arg = TREE_OPERAND (lhs, 1);
5149   rl_arg = TREE_OPERAND (rhs, 0);
5150   rr_arg = TREE_OPERAND (rhs, 1);
5151
5152   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5153   if (simple_operand_p (ll_arg)
5154       && simple_operand_p (lr_arg))
5155     {
5156       tree result;
5157       if (operand_equal_p (ll_arg, rl_arg, 0)
5158           && operand_equal_p (lr_arg, rr_arg, 0))
5159         {
5160           result = combine_comparisons (loc, code, lcode, rcode,
5161                                         truth_type, ll_arg, lr_arg);
5162           if (result)
5163             return result;
5164         }
5165       else if (operand_equal_p (ll_arg, rr_arg, 0)
5166                && operand_equal_p (lr_arg, rl_arg, 0))
5167         {
5168           result = combine_comparisons (loc, code, lcode,
5169                                         swap_tree_comparison (rcode),
5170                                         truth_type, ll_arg, lr_arg);
5171           if (result)
5172             return result;
5173         }
5174     }
5175
5176   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5177           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5178
5179   /* If the RHS can be evaluated unconditionally and its operands are
5180      simple, it wins to evaluate the RHS unconditionally on machines
5181      with expensive branches.  In this case, this isn't a comparison
5182      that can be merged.  Avoid doing this if the RHS is a floating-point
5183      comparison since those can trap.  */
5184
5185   if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5186                    false) >= 2
5187       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5188       && simple_operand_p (rl_arg)
5189       && simple_operand_p (rr_arg))
5190     {
5191       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5192       if (code == TRUTH_OR_EXPR
5193           && lcode == NE_EXPR && integer_zerop (lr_arg)
5194           && rcode == NE_EXPR && integer_zerop (rr_arg)
5195           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5196           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5197         {
5198           result = build2 (NE_EXPR, truth_type,
5199                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5200                                    ll_arg, rl_arg),
5201                            build_int_cst (TREE_TYPE (ll_arg), 0));
5202           goto fold_truthop_exit;
5203         }
5204
5205       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5206       if (code == TRUTH_AND_EXPR
5207           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5208           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5209           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5210           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5211         {
5212           result = build2 (EQ_EXPR, truth_type,
5213                            build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5214                                    ll_arg, rl_arg),
5215                            build_int_cst (TREE_TYPE (ll_arg), 0));
5216           goto fold_truthop_exit;
5217         }
5218
5219       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5220         {
5221           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5222             {
5223               result = build2 (code, truth_type, lhs, rhs);
5224               goto fold_truthop_exit;
5225             }
5226           return NULL_TREE;
5227         }
5228     }
5229
5230   /* See if the comparisons can be merged.  Then get all the parameters for
5231      each side.  */
5232
5233   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5234       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5235     return 0;
5236
5237   volatilep = 0;
5238   ll_inner = decode_field_reference (loc, ll_arg,
5239                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5240                                      &ll_unsignedp, &volatilep, &ll_mask,
5241                                      &ll_and_mask);
5242   lr_inner = decode_field_reference (loc, lr_arg,
5243                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5244                                      &lr_unsignedp, &volatilep, &lr_mask,
5245                                      &lr_and_mask);
5246   rl_inner = decode_field_reference (loc, rl_arg,
5247                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5248                                      &rl_unsignedp, &volatilep, &rl_mask,
5249                                      &rl_and_mask);
5250   rr_inner = decode_field_reference (loc, rr_arg,
5251                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5252                                      &rr_unsignedp, &volatilep, &rr_mask,
5253                                      &rr_and_mask);
5254
5255   /* It must be true that the inner operation on the lhs of each
5256      comparison must be the same if we are to be able to do anything.
5257      Then see if we have constants.  If not, the same must be true for
5258      the rhs's.  */
5259   if (volatilep || ll_inner == 0 || rl_inner == 0
5260       || ! operand_equal_p (ll_inner, rl_inner, 0))
5261     return 0;
5262
5263   if (TREE_CODE (lr_arg) == INTEGER_CST
5264       && TREE_CODE (rr_arg) == INTEGER_CST)
5265     l_const = lr_arg, r_const = rr_arg;
5266   else if (lr_inner == 0 || rr_inner == 0
5267            || ! operand_equal_p (lr_inner, rr_inner, 0))
5268     return 0;
5269   else
5270     l_const = r_const = 0;
5271
5272   /* If either comparison code is not correct for our logical operation,
5273      fail.  However, we can convert a one-bit comparison against zero into
5274      the opposite comparison against that bit being set in the field.  */
5275
5276   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5277   if (lcode != wanted_code)
5278     {
5279       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5280         {
5281           /* Make the left operand unsigned, since we are only interested
5282              in the value of one bit.  Otherwise we are doing the wrong
5283              thing below.  */
5284           ll_unsignedp = 1;
5285           l_const = ll_mask;
5286         }
5287       else
5288         return 0;
5289     }
5290
5291   /* This is analogous to the code for l_const above.  */
5292   if (rcode != wanted_code)
5293     {
5294       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5295         {
5296           rl_unsignedp = 1;
5297           r_const = rl_mask;
5298         }
5299       else
5300         return 0;
5301     }
5302
5303   /* See if we can find a mode that contains both fields being compared on
5304      the left.  If we can't, fail.  Otherwise, update all constants and masks
5305      to be relative to a field of that size.  */
5306   first_bit = MIN (ll_bitpos, rl_bitpos);
5307   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5308   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5309                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5310                           volatilep);
5311   if (lnmode == VOIDmode)
5312     return 0;
5313
5314   lnbitsize = GET_MODE_BITSIZE (lnmode);
5315   lnbitpos = first_bit & ~ (lnbitsize - 1);
5316   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5317   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5318
5319   if (BYTES_BIG_ENDIAN)
5320     {
5321       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5322       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5323     }
5324
5325   ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5326                          size_int (xll_bitpos));
5327   rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5328                          size_int (xrl_bitpos));
5329
5330   if (l_const)
5331     {
5332       l_const = fold_convert_loc (loc, lntype, l_const);
5333       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5334       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5335       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5336                                         fold_build1_loc (loc, BIT_NOT_EXPR,
5337                                                      lntype, ll_mask))))
5338         {
5339           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5340
5341           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5342         }
5343     }
5344   if (r_const)
5345     {
5346       r_const = fold_convert_loc (loc, lntype, r_const);
5347       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5348       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5349       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5350                                         fold_build1_loc (loc, BIT_NOT_EXPR,
5351                                                      lntype, rl_mask))))
5352         {
5353           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5354
5355           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5356         }
5357     }
5358
5359   /* If the right sides are not constant, do the same for it.  Also,
5360      disallow this optimization if a size or signedness mismatch occurs
5361      between the left and right sides.  */
5362   if (l_const == 0)
5363     {
5364       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5365           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5366           /* Make sure the two fields on the right
5367              correspond to the left without being swapped.  */
5368           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5369         return 0;
5370
5371       first_bit = MIN (lr_bitpos, rr_bitpos);
5372       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5373       rnmode = get_best_mode (end_bit - first_bit, first_bit,
5374                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5375                               volatilep);
5376       if (rnmode == VOIDmode)
5377         return 0;
5378
5379       rnbitsize = GET_MODE_BITSIZE (rnmode);
5380       rnbitpos = first_bit & ~ (rnbitsize - 1);
5381       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5382       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5383
5384       if (BYTES_BIG_ENDIAN)
5385         {
5386           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5387           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5388         }
5389
5390       lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5391                                                             rntype, lr_mask),
5392                              size_int (xlr_bitpos));
5393       rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5394                                                             rntype, rr_mask),
5395                              size_int (xrr_bitpos));
5396
5397       /* Make a mask that corresponds to both fields being compared.
5398          Do this for both items being compared.  If the operands are the
5399          same size and the bits being compared are in the same position
5400          then we can do this by masking both and comparing the masked
5401          results.  */
5402       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5403       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5404       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5405         {
5406           lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5407                                     ll_unsignedp || rl_unsignedp);
5408           if (! all_ones_mask_p (ll_mask, lnbitsize))
5409             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5410
5411           rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5412                                     lr_unsignedp || rr_unsignedp);
5413           if (! all_ones_mask_p (lr_mask, rnbitsize))
5414             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5415
5416           result = build2 (wanted_code, truth_type, lhs, rhs);
5417           goto fold_truthop_exit;
5418         }
5419
5420       /* There is still another way we can do something:  If both pairs of
5421          fields being compared are adjacent, we may be able to make a wider
5422          field containing them both.
5423
5424          Note that we still must mask the lhs/rhs expressions.  Furthermore,
5425          the mask must be shifted to account for the shift done by
5426          make_bit_field_ref.  */
5427       if ((ll_bitsize + ll_bitpos == rl_bitpos
5428            && lr_bitsize + lr_bitpos == rr_bitpos)
5429           || (ll_bitpos == rl_bitpos + rl_bitsize
5430               && lr_bitpos == rr_bitpos + rr_bitsize))
5431         {
5432           tree type;
5433
5434           lhs = make_bit_field_ref (loc, ll_inner, lntype,
5435                                     ll_bitsize + rl_bitsize,
5436                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5437           rhs = make_bit_field_ref (loc, lr_inner, rntype,
5438                                     lr_bitsize + rr_bitsize,
5439                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5440
5441           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5442                                  size_int (MIN (xll_bitpos, xrl_bitpos)));
5443           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5444                                  size_int (MIN (xlr_bitpos, xrr_bitpos)));
5445
5446           /* Convert to the smaller type before masking out unwanted bits.  */
5447           type = lntype;
5448           if (lntype != rntype)
5449             {
5450               if (lnbitsize > rnbitsize)
5451                 {
5452                   lhs = fold_convert_loc (loc, rntype, lhs);
5453                   ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5454                   type = rntype;
5455                 }
5456               else if (lnbitsize < rnbitsize)
5457                 {
5458                   rhs = fold_convert_loc (loc, lntype, rhs);
5459                   lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5460                   type = lntype;
5461                 }
5462             }
5463
5464           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5465             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5466
5467           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5468             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5469
5470           result = build2 (wanted_code, truth_type, lhs, rhs);
5471           goto fold_truthop_exit;
5472         }
5473
5474       return 0;
5475     }
5476
5477   /* Handle the case of comparisons with constants.  If there is something in
5478      common between the masks, those bits of the constants must be the same.
5479      If not, the condition is always false.  Test for this to avoid generating
5480      incorrect code below.  */
5481   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5482   if (! integer_zerop (result)
5483       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5484                            const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5485     {
5486       if (wanted_code == NE_EXPR)
5487         {
5488           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5489           return constant_boolean_node (true, truth_type);
5490         }
5491       else
5492         {
5493           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5494           return constant_boolean_node (false, truth_type);
5495         }
5496     }
5497
5498   /* Construct the expression we will return.  First get the component
5499      reference we will make.  Unless the mask is all ones the width of
5500      that field, perform the mask operation.  Then compare with the
5501      merged constant.  */
5502   result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5503                                ll_unsignedp || rl_unsignedp);
5504
5505   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5506   if (! all_ones_mask_p (ll_mask, lnbitsize))
5507     {
5508       result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5509       SET_EXPR_LOCATION (result, loc);
5510     }
5511
5512   result = build2 (wanted_code, truth_type, result,
5513                    const_binop (BIT_IOR_EXPR, l_const, r_const));
5514
5515  fold_truthop_exit:
5516   SET_EXPR_LOCATION (result, loc);
5517   return result;
5518 }
5519 \f
5520 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5521    constant.  */
5522
5523 static tree
5524 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5525                             tree op0, tree op1)
5526 {
5527   tree arg0 = op0;
5528   enum tree_code op_code;
5529   tree comp_const;
5530   tree minmax_const;
5531   int consts_equal, consts_lt;
5532   tree inner;
5533
5534   STRIP_SIGN_NOPS (arg0);
5535
5536   op_code = TREE_CODE (arg0);
5537   minmax_const = TREE_OPERAND (arg0, 1);
5538   comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5539   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5540   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5541   inner = TREE_OPERAND (arg0, 0);
5542
5543   /* If something does not permit us to optimize, return the original tree.  */
5544   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5545       || TREE_CODE (comp_const) != INTEGER_CST
5546       || TREE_OVERFLOW (comp_const)
5547       || TREE_CODE (minmax_const) != INTEGER_CST
5548       || TREE_OVERFLOW (minmax_const))
5549     return NULL_TREE;
5550
5551   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5552      and GT_EXPR, doing the rest with recursive calls using logical
5553      simplifications.  */
5554   switch (code)
5555     {
5556     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5557       {
5558         tree tem
5559           = optimize_minmax_comparison (loc,
5560                                         invert_tree_comparison (code, false),
5561                                         type, op0, op1);
5562         if (tem)
5563           return invert_truthvalue_loc (loc, tem);
5564         return NULL_TREE;
5565       }
5566
5567     case GE_EXPR:
5568       return
5569         fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5570                      optimize_minmax_comparison
5571                      (loc, EQ_EXPR, type, arg0, comp_const),
5572                      optimize_minmax_comparison
5573                      (loc, GT_EXPR, type, arg0, comp_const));
5574
5575     case EQ_EXPR:
5576       if (op_code == MAX_EXPR && consts_equal)
5577         /* MAX (X, 0) == 0  ->  X <= 0  */
5578         return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5579
5580       else if (op_code == MAX_EXPR && consts_lt)
5581         /* MAX (X, 0) == 5  ->  X == 5   */
5582         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5583
5584       else if (op_code == MAX_EXPR)
5585         /* MAX (X, 0) == -1  ->  false  */
5586         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5587
5588       else if (consts_equal)
5589         /* MIN (X, 0) == 0  ->  X >= 0  */
5590         return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5591
5592       else if (consts_lt)
5593         /* MIN (X, 0) == 5  ->  false  */
5594         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5595
5596       else
5597         /* MIN (X, 0) == -1  ->  X == -1  */
5598         return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5599
5600     case GT_EXPR:
5601       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5602         /* MAX (X, 0) > 0  ->  X > 0
5603            MAX (X, 0) > 5  ->  X > 5  */
5604         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5605
5606       else if (op_code == MAX_EXPR)
5607         /* MAX (X, 0) > -1  ->  true  */
5608         return omit_one_operand_loc (loc, type, integer_one_node, inner);
5609
5610       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5611         /* MIN (X, 0) > 0  ->  false
5612            MIN (X, 0) > 5  ->  false  */
5613         return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5614
5615       else
5616         /* MIN (X, 0) > -1  ->  X > -1  */
5617         return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5618
5619     default:
5620       return NULL_TREE;
5621     }
5622 }
5623 \f
5624 /* T is an integer expression that is being multiplied, divided, or taken a
5625    modulus (CODE says which and what kind of divide or modulus) by a
5626    constant C.  See if we can eliminate that operation by folding it with
5627    other operations already in T.  WIDE_TYPE, if non-null, is a type that
5628    should be used for the computation if wider than our type.
5629
5630    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5631    (X * 2) + (Y * 4).  We must, however, be assured that either the original
5632    expression would not overflow or that overflow is undefined for the type
5633    in the language in question.
5634
5635    If we return a non-null expression, it is an equivalent form of the
5636    original computation, but need not be in the original type.
5637
5638    We set *STRICT_OVERFLOW_P to true if the return values depends on
5639    signed overflow being undefined.  Otherwise we do not change
5640    *STRICT_OVERFLOW_P.  */
5641
5642 static tree
5643 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5644                 bool *strict_overflow_p)
5645 {
5646   /* To avoid exponential search depth, refuse to allow recursion past
5647      three levels.  Beyond that (1) it's highly unlikely that we'll find
5648      something interesting and (2) we've probably processed it before
5649      when we built the inner expression.  */
5650
5651   static int depth;
5652   tree ret;
5653
5654   if (depth > 3)
5655     return NULL;
5656
5657   depth++;
5658   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5659   depth--;
5660
5661   return ret;
5662 }
5663
5664 static tree
5665 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5666                   bool *strict_overflow_p)
5667 {
5668   tree type = TREE_TYPE (t);
5669   enum tree_code tcode = TREE_CODE (t);
5670   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5671                                    > GET_MODE_SIZE (TYPE_MODE (type)))
5672                 ? wide_type : type);
5673   tree t1, t2;
5674   int same_p = tcode == code;
5675   tree op0 = NULL_TREE, op1 = NULL_TREE;
5676   bool sub_strict_overflow_p;
5677
5678   /* Don't deal with constants of zero here; they confuse the code below.  */
5679   if (integer_zerop (c))
5680     return NULL_TREE;
5681
5682   if (TREE_CODE_CLASS (tcode) == tcc_unary)
5683     op0 = TREE_OPERAND (t, 0);
5684
5685   if (TREE_CODE_CLASS (tcode) == tcc_binary)
5686     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5687
5688   /* Note that we need not handle conditional operations here since fold
5689      already handles those cases.  So just do arithmetic here.  */
5690   switch (tcode)
5691     {
5692     case INTEGER_CST:
5693       /* For a constant, we can always simplify if we are a multiply
5694          or (for divide and modulus) if it is a multiple of our constant.  */
5695       if (code == MULT_EXPR
5696           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c)))
5697         return const_binop (code, fold_convert (ctype, t),
5698                             fold_convert (ctype, c));
5699       break;
5700
5701     CASE_CONVERT: case NON_LVALUE_EXPR:
5702       /* If op0 is an expression ...  */
5703       if ((COMPARISON_CLASS_P (op0)
5704            || UNARY_CLASS_P (op0)
5705            || BINARY_CLASS_P (op0)
5706            || VL_EXP_CLASS_P (op0)
5707            || EXPRESSION_CLASS_P (op0))
5708           /* ... and has wrapping overflow, and its type is smaller
5709              than ctype, then we cannot pass through as widening.  */
5710           && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
5711                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5712                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5713                && (TYPE_PRECISION (ctype)
5714                    > TYPE_PRECISION (TREE_TYPE (op0))))
5715               /* ... or this is a truncation (t is narrower than op0),
5716                  then we cannot pass through this narrowing.  */
5717               || (TYPE_PRECISION (type)
5718                   < TYPE_PRECISION (TREE_TYPE (op0)))
5719               /* ... or signedness changes for division or modulus,
5720                  then we cannot pass through this conversion.  */
5721               || (code != MULT_EXPR
5722                   && (TYPE_UNSIGNED (ctype)
5723                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
5724               /* ... or has undefined overflow while the converted to
5725                  type has not, we cannot do the operation in the inner type
5726                  as that would introduce undefined overflow.  */
5727               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5728                   && !TYPE_OVERFLOW_UNDEFINED (type))))
5729         break;
5730
5731       /* Pass the constant down and see if we can make a simplification.  If
5732          we can, replace this expression with the inner simplification for
5733          possible later conversion to our or some other type.  */
5734       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5735           && TREE_CODE (t2) == INTEGER_CST
5736           && !TREE_OVERFLOW (t2)
5737           && (0 != (t1 = extract_muldiv (op0, t2, code,
5738                                          code == MULT_EXPR
5739                                          ? ctype : NULL_TREE,
5740                                          strict_overflow_p))))
5741         return t1;
5742       break;
5743
5744     case ABS_EXPR:
5745       /* If widening the type changes it from signed to unsigned, then we
5746          must avoid building ABS_EXPR itself as unsigned.  */
5747       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5748         {
5749           tree cstype = (*signed_type_for) (ctype);
5750           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5751               != 0)
5752             {
5753               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5754               return fold_convert (ctype, t1);
5755             }
5756           break;
5757         }
5758       /* If the constant is negative, we cannot simplify this.  */
5759       if (tree_int_cst_sgn (c) == -1)
5760         break;
5761       /* FALLTHROUGH */
5762     case NEGATE_EXPR:
5763       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5764           != 0)
5765         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5766       break;
5767
5768     case MIN_EXPR:  case MAX_EXPR:
5769       /* If widening the type changes the signedness, then we can't perform
5770          this optimization as that changes the result.  */
5771       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5772         break;
5773
5774       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
5775       sub_strict_overflow_p = false;
5776       if ((t1 = extract_muldiv (op0, c, code, wide_type,
5777                                 &sub_strict_overflow_p)) != 0
5778           && (t2 = extract_muldiv (op1, c, code, wide_type,
5779                                    &sub_strict_overflow_p)) != 0)
5780         {
5781           if (tree_int_cst_sgn (c) < 0)
5782             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5783           if (sub_strict_overflow_p)
5784             *strict_overflow_p = true;
5785           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5786                               fold_convert (ctype, t2));
5787         }
5788       break;
5789
5790     case LSHIFT_EXPR:  case RSHIFT_EXPR:
5791       /* If the second operand is constant, this is a multiplication
5792          or floor division, by a power of two, so we can treat it that
5793          way unless the multiplier or divisor overflows.  Signed
5794          left-shift overflow is implementation-defined rather than
5795          undefined in C90, so do not convert signed left shift into
5796          multiplication.  */
5797       if (TREE_CODE (op1) == INTEGER_CST
5798           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5799           /* const_binop may not detect overflow correctly,
5800              so check for it explicitly here.  */
5801           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5802           && TREE_INT_CST_HIGH (op1) == 0
5803           && 0 != (t1 = fold_convert (ctype,
5804                                       const_binop (LSHIFT_EXPR,
5805                                                    size_one_node,
5806                                                    op1)))
5807           && !TREE_OVERFLOW (t1))
5808         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5809                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
5810                                        ctype,
5811                                        fold_convert (ctype, op0),
5812                                        t1),
5813                                c, code, wide_type, strict_overflow_p);
5814       break;
5815
5816     case PLUS_EXPR:  case MINUS_EXPR:
5817       /* See if we can eliminate the operation on both sides.  If we can, we
5818          can return a new PLUS or MINUS.  If we can't, the only remaining
5819          cases where we can do anything are if the second operand is a
5820          constant.  */
5821       sub_strict_overflow_p = false;
5822       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5823       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5824       if (t1 != 0 && t2 != 0
5825           && (code == MULT_EXPR
5826               /* If not multiplication, we can only do this if both operands
5827                  are divisible by c.  */
5828               || (multiple_of_p (ctype, op0, c)
5829                   && multiple_of_p (ctype, op1, c))))
5830         {
5831           if (sub_strict_overflow_p)
5832             *strict_overflow_p = true;
5833           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5834                               fold_convert (ctype, t2));
5835         }
5836
5837       /* If this was a subtraction, negate OP1 and set it to be an addition.
5838          This simplifies the logic below.  */
5839       if (tcode == MINUS_EXPR)
5840         {
5841           tcode = PLUS_EXPR, op1 = negate_expr (op1);
5842           /* If OP1 was not easily negatable, the constant may be OP0.  */
5843           if (TREE_CODE (op0) == INTEGER_CST)
5844             {
5845               tree tem = op0;
5846               op0 = op1;
5847               op1 = tem;
5848               tem = t1;
5849               t1 = t2;
5850               t2 = tem;
5851             }
5852         }
5853
5854       if (TREE_CODE (op1) != INTEGER_CST)
5855         break;
5856
5857       /* If either OP1 or C are negative, this optimization is not safe for
5858          some of the division and remainder types while for others we need
5859          to change the code.  */
5860       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5861         {
5862           if (code == CEIL_DIV_EXPR)
5863             code = FLOOR_DIV_EXPR;
5864           else if (code == FLOOR_DIV_EXPR)
5865             code = CEIL_DIV_EXPR;
5866           else if (code != MULT_EXPR
5867                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5868             break;
5869         }
5870
5871       /* If it's a multiply or a division/modulus operation of a multiple
5872          of our constant, do the operation and verify it doesn't overflow.  */
5873       if (code == MULT_EXPR
5874           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5875         {
5876           op1 = const_binop (code, fold_convert (ctype, op1),
5877                              fold_convert (ctype, c));
5878           /* We allow the constant to overflow with wrapping semantics.  */
5879           if (op1 == 0
5880               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5881             break;
5882         }
5883       else
5884         break;
5885
5886       /* If we have an unsigned type is not a sizetype, we cannot widen
5887          the operation since it will change the result if the original
5888          computation overflowed.  */
5889       if (TYPE_UNSIGNED (ctype)
5890           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5891           && ctype != type)
5892         break;
5893
5894       /* If we were able to eliminate our operation from the first side,
5895          apply our operation to the second side and reform the PLUS.  */
5896       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5897         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5898
5899       /* The last case is if we are a multiply.  In that case, we can
5900          apply the distributive law to commute the multiply and addition
5901          if the multiplication of the constants doesn't overflow.  */
5902       if (code == MULT_EXPR)
5903         return fold_build2 (tcode, ctype,
5904                             fold_build2 (code, ctype,
5905                                          fold_convert (ctype, op0),
5906                                          fold_convert (ctype, c)),
5907                             op1);
5908
5909       break;
5910
5911     case MULT_EXPR:
5912       /* We have a special case here if we are doing something like
5913          (C * 8) % 4 since we know that's zero.  */
5914       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5915            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5916           /* If the multiplication can overflow we cannot optimize this.
5917              ???  Until we can properly mark individual operations as
5918              not overflowing we need to treat sizetype special here as
5919              stor-layout relies on this opimization to make
5920              DECL_FIELD_BIT_OFFSET always a constant.  */
5921           && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
5922               || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
5923                   && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
5924           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5925           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5926         {
5927           *strict_overflow_p = true;
5928           return omit_one_operand (type, integer_zero_node, op0);
5929         }
5930
5931       /* ... fall through ...  */
5932
5933     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
5934     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
5935       /* If we can extract our operation from the LHS, do so and return a
5936          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
5937          do something only if the second operand is a constant.  */
5938       if (same_p
5939           && (t1 = extract_muldiv (op0, c, code, wide_type,
5940                                    strict_overflow_p)) != 0)
5941         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5942                             fold_convert (ctype, op1));
5943       else if (tcode == MULT_EXPR && code == MULT_EXPR
5944                && (t1 = extract_muldiv (op1, c, code, wide_type,
5945                                         strict_overflow_p)) != 0)
5946         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5947                             fold_convert (ctype, t1));
5948       else if (TREE_CODE (op1) != INTEGER_CST)
5949         return 0;
5950
5951       /* If these are the same operation types, we can associate them
5952          assuming no overflow.  */
5953       if (tcode == code
5954           && 0 != (t1 = int_const_binop (MULT_EXPR,
5955                                          fold_convert (ctype, op1),
5956                                          fold_convert (ctype, c), 1))
5957           && 0 != (t1 = force_fit_type_double (ctype, tree_to_double_int (t1),
5958                                                (TYPE_UNSIGNED (ctype)
5959                                                 && tcode != MULT_EXPR) ? -1 : 1,
5960                                                TREE_OVERFLOW (t1)))
5961           && !TREE_OVERFLOW (t1))
5962         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
5963
5964       /* If these operations "cancel" each other, we have the main
5965          optimizations of this pass, which occur when either constant is a
5966          multiple of the other, in which case we replace this with either an
5967          operation or CODE or TCODE.
5968
5969          If we have an unsigned type that is not a sizetype, we cannot do
5970          this since it will change the result if the original computation
5971          overflowed.  */
5972       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
5973            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5974           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5975               || (tcode == MULT_EXPR
5976                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5977                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
5978                   && code != MULT_EXPR)))
5979         {
5980           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5981             {
5982               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5983                 *strict_overflow_p = true;
5984               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5985                                   fold_convert (ctype,
5986                                                 const_binop (TRUNC_DIV_EXPR,
5987                                                              op1, c)));
5988             }
5989           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1)))
5990             {
5991               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5992                 *strict_overflow_p = true;
5993               return fold_build2 (code, ctype, fold_convert (ctype, op0),
5994                                   fold_convert (ctype,
5995                                                 const_binop (TRUNC_DIV_EXPR,
5996                                                              c, op1)));
5997             }
5998         }
5999       break;
6000
6001     default:
6002       break;
6003     }
6004
6005   return 0;
6006 }
6007 \f
6008 /* Return a node which has the indicated constant VALUE (either 0 or
6009    1), and is of the indicated TYPE.  */
6010
6011 tree
6012 constant_boolean_node (int value, tree type)
6013 {
6014   if (type == integer_type_node)
6015     return value ? integer_one_node : integer_zero_node;
6016   else if (type == boolean_type_node)
6017     return value ? boolean_true_node : boolean_false_node;
6018   else
6019     return build_int_cst (type, value);
6020 }
6021
6022
6023 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6024    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
6025    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6026    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
6027    COND is the first argument to CODE; otherwise (as in the example
6028    given here), it is the second argument.  TYPE is the type of the
6029    original expression.  Return NULL_TREE if no simplification is
6030    possible.  */
6031
6032 static tree
6033 fold_binary_op_with_conditional_arg (location_t loc,
6034                                      enum tree_code code,
6035                                      tree type, tree op0, tree op1,
6036                                      tree cond, tree arg, int cond_first_p)
6037 {
6038   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6039   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6040   tree test, true_value, false_value;
6041   tree lhs = NULL_TREE;
6042   tree rhs = NULL_TREE;
6043
6044   if (TREE_CODE (cond) == COND_EXPR)
6045     {
6046       test = TREE_OPERAND (cond, 0);
6047       true_value = TREE_OPERAND (cond, 1);
6048       false_value = TREE_OPERAND (cond, 2);
6049       /* If this operand throws an expression, then it does not make
6050          sense to try to perform a logical or arithmetic operation
6051          involving it.  */
6052       if (VOID_TYPE_P (TREE_TYPE (true_value)))
6053         lhs = true_value;
6054       if (VOID_TYPE_P (TREE_TYPE (false_value)))
6055         rhs = false_value;
6056     }
6057   else
6058     {
6059       tree testtype = TREE_TYPE (cond);
6060       test = cond;
6061       true_value = constant_boolean_node (true, testtype);
6062       false_value = constant_boolean_node (false, testtype);
6063     }
6064
6065   /* This transformation is only worthwhile if we don't have to wrap ARG
6066      in a SAVE_EXPR and the operation can be simplified on at least one
6067      of the branches once its pushed inside the COND_EXPR.  */
6068   if (!TREE_CONSTANT (arg)
6069       && (TREE_SIDE_EFFECTS (arg)
6070           || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6071     return NULL_TREE;
6072
6073   arg = fold_convert_loc (loc, arg_type, arg);
6074   if (lhs == 0)
6075     {
6076       true_value = fold_convert_loc (loc, cond_type, true_value);
6077       if (cond_first_p)
6078         lhs = fold_build2_loc (loc, code, type, true_value, arg);
6079       else
6080         lhs = fold_build2_loc (loc, code, type, arg, true_value);
6081     }
6082   if (rhs == 0)
6083     {
6084       false_value = fold_convert_loc (loc, cond_type, false_value);
6085       if (cond_first_p)
6086         rhs = fold_build2_loc (loc, code, type, false_value, arg);
6087       else
6088         rhs = fold_build2_loc (loc, code, type, arg, false_value);
6089     }
6090
6091   /* Check that we have simplified at least one of the branches.  */
6092   if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6093     return NULL_TREE;
6094
6095   return fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs);
6096 }
6097
6098 \f
6099 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6100
6101    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6102    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6103    ADDEND is the same as X.
6104
6105    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6106    and finite.  The problematic cases are when X is zero, and its mode
6107    has signed zeros.  In the case of rounding towards -infinity,
6108    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6109    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6110
6111 bool
6112 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6113 {
6114   if (!real_zerop (addend))
6115     return false;
6116
6117   /* Don't allow the fold with -fsignaling-nans.  */
6118   if (HONOR_SNANS (TYPE_MODE (type)))
6119     return false;
6120
6121   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6122   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6123     return true;
6124
6125   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6126   if (TREE_CODE (addend) == REAL_CST
6127       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6128     negate = !negate;
6129
6130   /* The mode has signed zeros, and we have to honor their sign.
6131      In this situation, there is only one case we can return true for.
6132      X - 0 is the same as X unless rounding towards -infinity is
6133      supported.  */
6134   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6135 }
6136
6137 /* Subroutine of fold() that checks comparisons of built-in math
6138    functions against real constants.
6139
6140    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6141    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6142    is the type of the result and ARG0 and ARG1 are the operands of the
6143    comparison.  ARG1 must be a TREE_REAL_CST.
6144
6145    The function returns the constant folded tree if a simplification
6146    can be made, and NULL_TREE otherwise.  */
6147
6148 static tree
6149 fold_mathfn_compare (location_t loc,
6150                      enum built_in_function fcode, enum tree_code code,
6151                      tree type, tree arg0, tree arg1)
6152 {
6153   REAL_VALUE_TYPE c;
6154
6155   if (BUILTIN_SQRT_P (fcode))
6156     {
6157       tree arg = CALL_EXPR_ARG (arg0, 0);
6158       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6159
6160       c = TREE_REAL_CST (arg1);
6161       if (REAL_VALUE_NEGATIVE (c))
6162         {
6163           /* sqrt(x) < y is always false, if y is negative.  */
6164           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6165             return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6166
6167           /* sqrt(x) > y is always true, if y is negative and we
6168              don't care about NaNs, i.e. negative values of x.  */
6169           if (code == NE_EXPR || !HONOR_NANS (mode))
6170             return omit_one_operand_loc (loc, type, integer_one_node, arg);
6171
6172           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6173           return fold_build2_loc (loc, GE_EXPR, type, arg,
6174                               build_real (TREE_TYPE (arg), dconst0));
6175         }
6176       else if (code == GT_EXPR || code == GE_EXPR)
6177         {
6178           REAL_VALUE_TYPE c2;
6179
6180           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6181           real_convert (&c2, mode, &c2);
6182
6183           if (REAL_VALUE_ISINF (c2))
6184             {
6185               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6186               if (HONOR_INFINITIES (mode))
6187                 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6188                                     build_real (TREE_TYPE (arg), c2));
6189
6190               /* sqrt(x) > y is always false, when y is very large
6191                  and we don't care about infinities.  */
6192               return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6193             }
6194
6195           /* sqrt(x) > c is the same as x > c*c.  */
6196           return fold_build2_loc (loc, code, type, arg,
6197                               build_real (TREE_TYPE (arg), c2));
6198         }
6199       else if (code == LT_EXPR || code == LE_EXPR)
6200         {
6201           REAL_VALUE_TYPE c2;
6202
6203           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6204           real_convert (&c2, mode, &c2);
6205
6206           if (REAL_VALUE_ISINF (c2))
6207             {
6208               /* sqrt(x) < y is always true, when y is a very large
6209                  value and we don't care about NaNs or Infinities.  */
6210               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6211                 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6212
6213               /* sqrt(x) < y is x != +Inf when y is very large and we
6214                  don't care about NaNs.  */
6215               if (! HONOR_NANS (mode))
6216                 return fold_build2_loc (loc, NE_EXPR, type, arg,
6217                                     build_real (TREE_TYPE (arg), c2));
6218
6219               /* sqrt(x) < y is x >= 0 when y is very large and we
6220                  don't care about Infinities.  */
6221               if (! HONOR_INFINITIES (mode))
6222                 return fold_build2_loc (loc, GE_EXPR, type, arg,
6223                                     build_real (TREE_TYPE (arg), dconst0));
6224
6225               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6226               if (lang_hooks.decls.global_bindings_p () != 0
6227                   || CONTAINS_PLACEHOLDER_P (arg))
6228                 return NULL_TREE;
6229
6230               arg = save_expr (arg);
6231               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6232                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6233                                                build_real (TREE_TYPE (arg),
6234                                                            dconst0)),
6235                                   fold_build2_loc (loc, NE_EXPR, type, arg,
6236                                                build_real (TREE_TYPE (arg),
6237                                                            c2)));
6238             }
6239
6240           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6241           if (! HONOR_NANS (mode))
6242             return fold_build2_loc (loc, code, type, arg,
6243                                 build_real (TREE_TYPE (arg), c2));
6244
6245           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6246           if (lang_hooks.decls.global_bindings_p () == 0
6247               && ! CONTAINS_PLACEHOLDER_P (arg))
6248             {
6249               arg = save_expr (arg);
6250               return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6251                                   fold_build2_loc (loc, GE_EXPR, type, arg,
6252                                                build_real (TREE_TYPE (arg),
6253                                                            dconst0)),
6254                                   fold_build2_loc (loc, code, type, arg,
6255                                                build_real (TREE_TYPE (arg),
6256                                                            c2)));
6257             }
6258         }
6259     }
6260
6261   return NULL_TREE;
6262 }
6263
6264 /* Subroutine of fold() that optimizes comparisons against Infinities,
6265    either +Inf or -Inf.
6266
6267    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6268    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6269    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6270
6271    The function returns the constant folded tree if a simplification
6272    can be made, and NULL_TREE otherwise.  */
6273
6274 static tree
6275 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6276                   tree arg0, tree arg1)
6277 {
6278   enum machine_mode mode;
6279   REAL_VALUE_TYPE max;
6280   tree temp;
6281   bool neg;
6282
6283   mode = TYPE_MODE (TREE_TYPE (arg0));
6284
6285   /* For negative infinity swap the sense of the comparison.  */
6286   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6287   if (neg)
6288     code = swap_tree_comparison (code);
6289
6290   switch (code)
6291     {
6292     case GT_EXPR:
6293       /* x > +Inf is always false, if with ignore sNANs.  */
6294       if (HONOR_SNANS (mode))
6295         return NULL_TREE;
6296       return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6297
6298     case LE_EXPR:
6299       /* x <= +Inf is always true, if we don't case about NaNs.  */
6300       if (! HONOR_NANS (mode))
6301         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6302
6303       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6304       if (lang_hooks.decls.global_bindings_p () == 0
6305           && ! CONTAINS_PLACEHOLDER_P (arg0))
6306         {
6307           arg0 = save_expr (arg0);
6308           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6309         }
6310       break;
6311
6312     case EQ_EXPR:
6313     case GE_EXPR:
6314       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6315       real_maxval (&max, neg, mode);
6316       return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6317                           arg0, build_real (TREE_TYPE (arg0), max));
6318
6319     case LT_EXPR:
6320       /* x < +Inf is always equal to x <= DBL_MAX.  */
6321       real_maxval (&max, neg, mode);
6322       return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6323                           arg0, build_real (TREE_TYPE (arg0), max));
6324
6325     case NE_EXPR:
6326       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6327       real_maxval (&max, neg, mode);
6328       if (! HONOR_NANS (mode))
6329         return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6330                             arg0, build_real (TREE_TYPE (arg0), max));
6331
6332       temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6333                           arg0, build_real (TREE_TYPE (arg0), max));
6334       return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
6335
6336     default:
6337       break;
6338     }
6339
6340   return NULL_TREE;
6341 }
6342
6343 /* Subroutine of fold() that optimizes comparisons of a division by
6344    a nonzero integer constant against an integer constant, i.e.
6345    X/C1 op C2.
6346
6347    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6348    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6349    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6350
6351    The function returns the constant folded tree if a simplification
6352    can be made, and NULL_TREE otherwise.  */
6353
6354 static tree
6355 fold_div_compare (location_t loc,
6356                   enum tree_code code, tree type, tree arg0, tree arg1)
6357 {
6358   tree prod, tmp, hi, lo;
6359   tree arg00 = TREE_OPERAND (arg0, 0);
6360   tree arg01 = TREE_OPERAND (arg0, 1);
6361   double_int val;
6362   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6363   bool neg_overflow;
6364   int overflow;
6365
6366   /* We have to do this the hard way to detect unsigned overflow.
6367      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
6368   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6369                                    TREE_INT_CST_HIGH (arg01),
6370                                    TREE_INT_CST_LOW (arg1),
6371                                    TREE_INT_CST_HIGH (arg1),
6372                                    &val.low, &val.high, unsigned_p);
6373   prod = force_fit_type_double (TREE_TYPE (arg00), val, -1, overflow);
6374   neg_overflow = false;
6375
6376   if (unsigned_p)
6377     {
6378       tmp = int_const_binop (MINUS_EXPR, arg01,
6379                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6380       lo = prod;
6381
6382       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
6383       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6384                                        TREE_INT_CST_HIGH (prod),
6385                                        TREE_INT_CST_LOW (tmp),
6386                                        TREE_INT_CST_HIGH (tmp),
6387                                        &val.low, &val.high, unsigned_p);
6388       hi = force_fit_type_double (TREE_TYPE (arg00), val,
6389                                   -1, overflow | TREE_OVERFLOW (prod));
6390     }
6391   else if (tree_int_cst_sgn (arg01) >= 0)
6392     {
6393       tmp = int_const_binop (MINUS_EXPR, arg01,
6394                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6395       switch (tree_int_cst_sgn (arg1))
6396         {
6397         case -1:
6398           neg_overflow = true;
6399           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6400           hi = prod;
6401           break;
6402
6403         case  0:
6404           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6405           hi = tmp;
6406           break;
6407
6408         case  1:
6409           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6410           lo = prod;
6411           break;
6412
6413         default:
6414           gcc_unreachable ();
6415         }
6416     }
6417   else
6418     {
6419       /* A negative divisor reverses the relational operators.  */
6420       code = swap_tree_comparison (code);
6421
6422       tmp = int_const_binop (PLUS_EXPR, arg01,
6423                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6424       switch (tree_int_cst_sgn (arg1))
6425         {
6426         case -1:
6427           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6428           lo = prod;
6429           break;
6430
6431         case  0:
6432           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6433           lo = tmp;
6434           break;
6435
6436         case  1:
6437           neg_overflow = true;
6438           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6439           hi = prod;
6440           break;
6441
6442         default:
6443           gcc_unreachable ();
6444         }
6445     }
6446
6447   switch (code)
6448     {
6449     case EQ_EXPR:
6450       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6451         return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6452       if (TREE_OVERFLOW (hi))
6453         return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6454       if (TREE_OVERFLOW (lo))
6455         return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6456       return build_range_check (loc, type, arg00, 1, lo, hi);
6457
6458     case NE_EXPR:
6459       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6460         return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6461       if (TREE_OVERFLOW (hi))
6462         return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6463       if (TREE_OVERFLOW (lo))
6464         return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6465       return build_range_check (loc, type, arg00, 0, lo, hi);
6466
6467     case LT_EXPR:
6468       if (TREE_OVERFLOW (lo))
6469         {
6470           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6471           return omit_one_operand_loc (loc, type, tmp, arg00);
6472         }
6473       return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6474
6475     case LE_EXPR:
6476       if (TREE_OVERFLOW (hi))
6477         {
6478           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6479           return omit_one_operand_loc (loc, type, tmp, arg00);
6480         }
6481       return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6482
6483     case GT_EXPR:
6484       if (TREE_OVERFLOW (hi))
6485         {
6486           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6487           return omit_one_operand_loc (loc, type, tmp, arg00);
6488         }
6489       return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6490
6491     case GE_EXPR:
6492       if (TREE_OVERFLOW (lo))
6493         {
6494           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6495           return omit_one_operand_loc (loc, type, tmp, arg00);
6496         }
6497       return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6498
6499     default:
6500       break;
6501     }
6502
6503   return NULL_TREE;
6504 }
6505
6506
6507 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6508    equality/inequality test, then return a simplified form of the test
6509    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6510    result type.  */
6511
6512 static tree
6513 fold_single_bit_test_into_sign_test (location_t loc,
6514                                      enum tree_code code, tree arg0, tree arg1,
6515                                      tree result_type)
6516 {
6517   /* If this is testing a single bit, we can optimize the test.  */
6518   if ((code == NE_EXPR || code == EQ_EXPR)
6519       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6520       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6521     {
6522       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6523          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6524       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6525
6526       if (arg00 != NULL_TREE
6527           /* This is only a win if casting to a signed type is cheap,
6528              i.e. when arg00's type is not a partial mode.  */
6529           && TYPE_PRECISION (TREE_TYPE (arg00))
6530              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6531         {
6532           tree stype = signed_type_for (TREE_TYPE (arg00));
6533           return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6534                               result_type,
6535                               fold_convert_loc (loc, stype, arg00),
6536                               build_int_cst (stype, 0));
6537         }
6538     }
6539
6540   return NULL_TREE;
6541 }
6542
6543 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6544    equality/inequality test, then return a simplified form of
6545    the test using shifts and logical operations.  Otherwise return
6546    NULL.  TYPE is the desired result type.  */
6547
6548 tree
6549 fold_single_bit_test (location_t loc, enum tree_code code,
6550                       tree arg0, tree arg1, tree result_type)
6551 {
6552   /* If this is testing a single bit, we can optimize the test.  */
6553   if ((code == NE_EXPR || code == EQ_EXPR)
6554       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6555       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6556     {
6557       tree inner = TREE_OPERAND (arg0, 0);
6558       tree type = TREE_TYPE (arg0);
6559       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6560       enum machine_mode operand_mode = TYPE_MODE (type);
6561       int ops_unsigned;
6562       tree signed_type, unsigned_type, intermediate_type;
6563       tree tem, one;
6564
6565       /* First, see if we can fold the single bit test into a sign-bit
6566          test.  */
6567       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6568                                                  result_type);
6569       if (tem)
6570         return tem;
6571
6572       /* Otherwise we have (A & C) != 0 where C is a single bit,
6573          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6574          Similarly for (A & C) == 0.  */
6575
6576       /* If INNER is a right shift of a constant and it plus BITNUM does
6577          not overflow, adjust BITNUM and INNER.  */
6578       if (TREE_CODE (inner) == RSHIFT_EXPR
6579           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6580           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6581           && bitnum < TYPE_PRECISION (type)
6582           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6583                                    bitnum - TYPE_PRECISION (type)))
6584         {
6585           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6586           inner = TREE_OPERAND (inner, 0);
6587         }
6588
6589       /* If we are going to be able to omit the AND below, we must do our
6590          operations as unsigned.  If we must use the AND, we have a choice.
6591          Normally unsigned is faster, but for some machines signed is.  */
6592 #ifdef LOAD_EXTEND_OP
6593       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6594                       && !flag_syntax_only) ? 0 : 1;
6595 #else
6596       ops_unsigned = 1;
6597 #endif
6598
6599       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6600       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6601       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6602       inner = fold_convert_loc (loc, intermediate_type, inner);
6603
6604       if (bitnum != 0)
6605         inner = build2 (RSHIFT_EXPR, intermediate_type,
6606                         inner, size_int (bitnum));
6607
6608       one = build_int_cst (intermediate_type, 1);
6609
6610       if (code == EQ_EXPR)
6611         inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6612
6613       /* Put the AND last so it can combine with more things.  */
6614       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6615
6616       /* Make sure to return the proper type.  */
6617       inner = fold_convert_loc (loc, result_type, inner);
6618
6619       return inner;
6620     }
6621   return NULL_TREE;
6622 }
6623
6624 /* Check whether we are allowed to reorder operands arg0 and arg1,
6625    such that the evaluation of arg1 occurs before arg0.  */
6626
6627 static bool
6628 reorder_operands_p (const_tree arg0, const_tree arg1)
6629 {
6630   if (! flag_evaluation_order)
6631       return true;
6632   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6633     return true;
6634   return ! TREE_SIDE_EFFECTS (arg0)
6635          && ! TREE_SIDE_EFFECTS (arg1);
6636 }
6637
6638 /* Test whether it is preferable two swap two operands, ARG0 and
6639    ARG1, for example because ARG0 is an integer constant and ARG1
6640    isn't.  If REORDER is true, only recommend swapping if we can
6641    evaluate the operands in reverse order.  */
6642
6643 bool
6644 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6645 {
6646   STRIP_SIGN_NOPS (arg0);
6647   STRIP_SIGN_NOPS (arg1);
6648
6649   if (TREE_CODE (arg1) == INTEGER_CST)
6650     return 0;
6651   if (TREE_CODE (arg0) == INTEGER_CST)
6652     return 1;
6653
6654   if (TREE_CODE (arg1) == REAL_CST)
6655     return 0;
6656   if (TREE_CODE (arg0) == REAL_CST)
6657     return 1;
6658
6659   if (TREE_CODE (arg1) == FIXED_CST)
6660     return 0;
6661   if (TREE_CODE (arg0) == FIXED_CST)
6662     return 1;
6663
6664   if (TREE_CODE (arg1) == COMPLEX_CST)
6665     return 0;
6666   if (TREE_CODE (arg0) == COMPLEX_CST)
6667     return 1;
6668
6669   if (TREE_CONSTANT (arg1))
6670     return 0;
6671   if (TREE_CONSTANT (arg0))
6672     return 1;
6673
6674   if (optimize_function_for_size_p (cfun))
6675     return 0;
6676
6677   if (reorder && flag_evaluation_order
6678       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6679     return 0;
6680
6681   /* It is preferable to swap two SSA_NAME to ensure a canonical form
6682      for commutative and comparison operators.  Ensuring a canonical
6683      form allows the optimizers to find additional redundancies without
6684      having to explicitly check for both orderings.  */
6685   if (TREE_CODE (arg0) == SSA_NAME
6686       && TREE_CODE (arg1) == SSA_NAME
6687       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6688     return 1;
6689
6690   /* Put SSA_NAMEs last.  */
6691   if (TREE_CODE (arg1) == SSA_NAME)
6692     return 0;
6693   if (TREE_CODE (arg0) == SSA_NAME)
6694     return 1;
6695
6696   /* Put variables last.  */
6697   if (DECL_P (arg1))
6698     return 0;
6699   if (DECL_P (arg0))
6700     return 1;
6701
6702   return 0;
6703 }
6704
6705 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6706    ARG0 is extended to a wider type.  */
6707
6708 static tree
6709 fold_widened_comparison (location_t loc, enum tree_code code,
6710                          tree type, tree arg0, tree arg1)
6711 {
6712   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6713   tree arg1_unw;
6714   tree shorter_type, outer_type;
6715   tree min, max;
6716   bool above, below;
6717
6718   if (arg0_unw == arg0)
6719     return NULL_TREE;
6720   shorter_type = TREE_TYPE (arg0_unw);
6721
6722 #ifdef HAVE_canonicalize_funcptr_for_compare
6723   /* Disable this optimization if we're casting a function pointer
6724      type on targets that require function pointer canonicalization.  */
6725   if (HAVE_canonicalize_funcptr_for_compare
6726       && TREE_CODE (shorter_type) == POINTER_TYPE
6727       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6728     return NULL_TREE;
6729 #endif
6730
6731   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6732     return NULL_TREE;
6733
6734   arg1_unw = get_unwidened (arg1, NULL_TREE);
6735
6736   /* If possible, express the comparison in the shorter mode.  */
6737   if ((code == EQ_EXPR || code == NE_EXPR
6738        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6739       && (TREE_TYPE (arg1_unw) == shorter_type
6740           || ((TYPE_PRECISION (shorter_type)
6741                >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6742               && (TYPE_UNSIGNED (shorter_type)
6743                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6744           || (TREE_CODE (arg1_unw) == INTEGER_CST
6745               && (TREE_CODE (shorter_type) == INTEGER_TYPE
6746                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6747               && int_fits_type_p (arg1_unw, shorter_type))))
6748     return fold_build2_loc (loc, code, type, arg0_unw,
6749                         fold_convert_loc (loc, shorter_type, arg1_unw));
6750
6751   if (TREE_CODE (arg1_unw) != INTEGER_CST
6752       || TREE_CODE (shorter_type) != INTEGER_TYPE
6753       || !int_fits_type_p (arg1_unw, shorter_type))
6754     return NULL_TREE;
6755
6756   /* If we are comparing with the integer that does not fit into the range
6757      of the shorter type, the result is known.  */
6758   outer_type = TREE_TYPE (arg1_unw);
6759   min = lower_bound_in_type (outer_type, shorter_type);
6760   max = upper_bound_in_type (outer_type, shorter_type);
6761
6762   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6763                                                    max, arg1_unw));
6764   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6765                                                    arg1_unw, min));
6766
6767   switch (code)
6768     {
6769     case EQ_EXPR:
6770       if (above || below)
6771         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6772       break;
6773
6774     case NE_EXPR:
6775       if (above || below)
6776         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6777       break;
6778
6779     case LT_EXPR:
6780     case LE_EXPR:
6781       if (above)
6782         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6783       else if (below)
6784         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6785
6786     case GT_EXPR:
6787     case GE_EXPR:
6788       if (above)
6789         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6790       else if (below)
6791         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6792
6793     default:
6794       break;
6795     }
6796
6797   return NULL_TREE;
6798 }
6799
6800 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6801    ARG0 just the signedness is changed.  */
6802
6803 static tree
6804 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
6805                               tree arg0, tree arg1)
6806 {
6807   tree arg0_inner;
6808   tree inner_type, outer_type;
6809
6810   if (!CONVERT_EXPR_P (arg0))
6811     return NULL_TREE;
6812
6813   outer_type = TREE_TYPE (arg0);
6814   arg0_inner = TREE_OPERAND (arg0, 0);
6815   inner_type = TREE_TYPE (arg0_inner);
6816
6817 #ifdef HAVE_canonicalize_funcptr_for_compare
6818   /* Disable this optimization if we're casting a function pointer
6819      type on targets that require function pointer canonicalization.  */
6820   if (HAVE_canonicalize_funcptr_for_compare
6821       && TREE_CODE (inner_type) == POINTER_TYPE
6822       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6823     return NULL_TREE;
6824 #endif
6825
6826   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6827     return NULL_TREE;
6828
6829   if (TREE_CODE (arg1) != INTEGER_CST
6830       && !(CONVERT_EXPR_P (arg1)
6831            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6832     return NULL_TREE;
6833
6834   if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6835        || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
6836       && code != NE_EXPR
6837       && code != EQ_EXPR)
6838     return NULL_TREE;
6839
6840   if (TREE_CODE (arg1) == INTEGER_CST)
6841     arg1 = force_fit_type_double (inner_type, tree_to_double_int (arg1),
6842                                   0, TREE_OVERFLOW (arg1));
6843   else
6844     arg1 = fold_convert_loc (loc, inner_type, arg1);
6845
6846   return fold_build2_loc (loc, code, type, arg0_inner, arg1);
6847 }
6848
6849 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6850    step of the array.  Reconstructs s and delta in the case of s *
6851    delta being an integer constant (and thus already folded).  ADDR is
6852    the address. MULT is the multiplicative expression.  If the
6853    function succeeds, the new address expression is returned.
6854    Otherwise NULL_TREE is returned.  LOC is the location of the
6855    resulting expression.  */
6856
6857 static tree
6858 try_move_mult_to_index (location_t loc, tree addr, tree op1)
6859 {
6860   tree s, delta, step;
6861   tree ref = TREE_OPERAND (addr, 0), pref;
6862   tree ret, pos;
6863   tree itype;
6864   bool mdim = false;
6865
6866   /*  Strip the nops that might be added when converting op1 to sizetype. */
6867   STRIP_NOPS (op1);
6868
6869   /* Canonicalize op1 into a possibly non-constant delta
6870      and an INTEGER_CST s.  */
6871   if (TREE_CODE (op1) == MULT_EXPR)
6872     {
6873       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6874
6875       STRIP_NOPS (arg0);
6876       STRIP_NOPS (arg1);
6877
6878       if (TREE_CODE (arg0) == INTEGER_CST)
6879         {
6880           s = arg0;
6881           delta = arg1;
6882         }
6883       else if (TREE_CODE (arg1) == INTEGER_CST)
6884         {
6885           s = arg1;
6886           delta = arg0;
6887         }
6888       else
6889         return NULL_TREE;
6890     }
6891   else if (TREE_CODE (op1) == INTEGER_CST)
6892     {
6893       delta = op1;
6894       s = NULL_TREE;
6895     }
6896   else
6897     {
6898       /* Simulate we are delta * 1.  */
6899       delta = op1;
6900       s = integer_one_node;
6901     }
6902
6903   for (;; ref = TREE_OPERAND (ref, 0))
6904     {
6905       if (TREE_CODE (ref) == ARRAY_REF)
6906         {
6907           tree domain;
6908
6909           /* Remember if this was a multi-dimensional array.  */
6910           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6911             mdim = true;
6912
6913           domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6914           if (! domain)
6915             continue;
6916           itype = TREE_TYPE (domain);
6917
6918           step = array_ref_element_size (ref);
6919           if (TREE_CODE (step) != INTEGER_CST)
6920             continue;
6921
6922           if (s)
6923             {
6924               if (! tree_int_cst_equal (step, s))
6925                 continue;
6926             }
6927           else
6928             {
6929               /* Try if delta is a multiple of step.  */
6930               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6931               if (! tmp)
6932                 continue;
6933               delta = tmp;
6934             }
6935
6936           /* Only fold here if we can verify we do not overflow one
6937              dimension of a multi-dimensional array.  */
6938           if (mdim)
6939             {
6940               tree tmp;
6941
6942               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
6943                   || !TYPE_MAX_VALUE (domain)
6944                   || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
6945                 continue;
6946
6947               tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
6948                                      fold_convert_loc (loc, itype,
6949                                                        TREE_OPERAND (ref, 1)),
6950                                      fold_convert_loc (loc, itype, delta));
6951               if (!tmp
6952                   || TREE_CODE (tmp) != INTEGER_CST
6953                   || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
6954                 continue;
6955             }
6956
6957           break;
6958         }
6959       else
6960         mdim = false;
6961
6962       if (!handled_component_p (ref))
6963         return NULL_TREE;
6964     }
6965
6966   /* We found the suitable array reference.  So copy everything up to it,
6967      and replace the index.  */
6968
6969   pref = TREE_OPERAND (addr, 0);
6970   ret = copy_node (pref);
6971   SET_EXPR_LOCATION (ret, loc);
6972   pos = ret;
6973
6974   while (pref != ref)
6975     {
6976       pref = TREE_OPERAND (pref, 0);
6977       TREE_OPERAND (pos, 0) = copy_node (pref);
6978       pos = TREE_OPERAND (pos, 0);
6979     }
6980
6981   TREE_OPERAND (pos, 1) = fold_build2_loc (loc, PLUS_EXPR, itype,
6982                                        fold_convert_loc (loc, itype,
6983                                                          TREE_OPERAND (pos, 1)),
6984                                        fold_convert_loc (loc, itype, delta));
6985
6986   return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
6987 }
6988
6989
6990 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
6991    means A >= Y && A != MAX, but in this case we know that
6992    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
6993
6994 static tree
6995 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6996 {
6997   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6998
6999   if (TREE_CODE (bound) == LT_EXPR)
7000     a = TREE_OPERAND (bound, 0);
7001   else if (TREE_CODE (bound) == GT_EXPR)
7002     a = TREE_OPERAND (bound, 1);
7003   else
7004     return NULL_TREE;
7005
7006   typea = TREE_TYPE (a);
7007   if (!INTEGRAL_TYPE_P (typea)
7008       && !POINTER_TYPE_P (typea))
7009     return NULL_TREE;
7010
7011   if (TREE_CODE (ineq) == LT_EXPR)
7012     {
7013       a1 = TREE_OPERAND (ineq, 1);
7014       y = TREE_OPERAND (ineq, 0);
7015     }
7016   else if (TREE_CODE (ineq) == GT_EXPR)
7017     {
7018       a1 = TREE_OPERAND (ineq, 0);
7019       y = TREE_OPERAND (ineq, 1);
7020     }
7021   else
7022     return NULL_TREE;
7023
7024   if (TREE_TYPE (a1) != typea)
7025     return NULL_TREE;
7026
7027   if (POINTER_TYPE_P (typea))
7028     {
7029       /* Convert the pointer types into integer before taking the difference.  */
7030       tree ta = fold_convert_loc (loc, ssizetype, a);
7031       tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7032       diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7033     }
7034   else
7035     diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7036
7037   if (!diff || !integer_onep (diff))
7038    return NULL_TREE;
7039
7040   return fold_build2_loc (loc, GE_EXPR, type, a, y);
7041 }
7042
7043 /* Fold a sum or difference of at least one multiplication.
7044    Returns the folded tree or NULL if no simplification could be made.  */
7045
7046 static tree
7047 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7048                           tree arg0, tree arg1)
7049 {
7050   tree arg00, arg01, arg10, arg11;
7051   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7052
7053   /* (A * C) +- (B * C) -> (A+-B) * C.
7054      (A * C) +- A -> A * (C+-1).
7055      We are most concerned about the case where C is a constant,
7056      but other combinations show up during loop reduction.  Since
7057      it is not difficult, try all four possibilities.  */
7058
7059   if (TREE_CODE (arg0) == MULT_EXPR)
7060     {
7061       arg00 = TREE_OPERAND (arg0, 0);
7062       arg01 = TREE_OPERAND (arg0, 1);
7063     }
7064   else if (TREE_CODE (arg0) == INTEGER_CST)
7065     {
7066       arg00 = build_one_cst (type);
7067       arg01 = arg0;
7068     }
7069   else
7070     {
7071       /* We cannot generate constant 1 for fract.  */
7072       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7073         return NULL_TREE;
7074       arg00 = arg0;
7075       arg01 = build_one_cst (type);
7076     }
7077   if (TREE_CODE (arg1) == MULT_EXPR)
7078     {
7079       arg10 = TREE_OPERAND (arg1, 0);
7080       arg11 = TREE_OPERAND (arg1, 1);
7081     }
7082   else if (TREE_CODE (arg1) == INTEGER_CST)
7083     {
7084       arg10 = build_one_cst (type);
7085       /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7086          the purpose of this canonicalization.  */
7087       if (TREE_INT_CST_HIGH (arg1) == -1
7088           && negate_expr_p (arg1)
7089           && code == PLUS_EXPR)
7090         {
7091           arg11 = negate_expr (arg1);
7092           code = MINUS_EXPR;
7093         }
7094       else
7095         arg11 = arg1;
7096     }
7097   else
7098     {
7099       /* We cannot generate constant 1 for fract.  */
7100       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7101         return NULL_TREE;
7102       arg10 = arg1;
7103       arg11 = build_one_cst (type);
7104     }
7105   same = NULL_TREE;
7106
7107   if (operand_equal_p (arg01, arg11, 0))
7108     same = arg01, alt0 = arg00, alt1 = arg10;
7109   else if (operand_equal_p (arg00, arg10, 0))
7110     same = arg00, alt0 = arg01, alt1 = arg11;
7111   else if (operand_equal_p (arg00, arg11, 0))
7112     same = arg00, alt0 = arg01, alt1 = arg10;
7113   else if (operand_equal_p (arg01, arg10, 0))
7114     same = arg01, alt0 = arg00, alt1 = arg11;
7115
7116   /* No identical multiplicands; see if we can find a common
7117      power-of-two factor in non-power-of-two multiplies.  This
7118      can help in multi-dimensional array access.  */
7119   else if (host_integerp (arg01, 0)
7120            && host_integerp (arg11, 0))
7121     {
7122       HOST_WIDE_INT int01, int11, tmp;
7123       bool swap = false;
7124       tree maybe_same;
7125       int01 = TREE_INT_CST_LOW (arg01);
7126       int11 = TREE_INT_CST_LOW (arg11);
7127
7128       /* Move min of absolute values to int11.  */
7129       if ((int01 >= 0 ? int01 : -int01)
7130           < (int11 >= 0 ? int11 : -int11))
7131         {
7132           tmp = int01, int01 = int11, int11 = tmp;
7133           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7134           maybe_same = arg01;
7135           swap = true;
7136         }
7137       else
7138         maybe_same = arg11;
7139
7140       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
7141           /* The remainder should not be a constant, otherwise we
7142              end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7143              increased the number of multiplications necessary.  */
7144           && TREE_CODE (arg10) != INTEGER_CST)
7145         {
7146           alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7147                               build_int_cst (TREE_TYPE (arg00),
7148                                              int01 / int11));
7149           alt1 = arg10;
7150           same = maybe_same;
7151           if (swap)
7152             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7153         }
7154     }
7155
7156   if (same)
7157     return fold_build2_loc (loc, MULT_EXPR, type,
7158                         fold_build2_loc (loc, code, type,
7159                                      fold_convert_loc (loc, type, alt0),
7160                                      fold_convert_loc (loc, type, alt1)),
7161                         fold_convert_loc (loc, type, same));
7162
7163   return NULL_TREE;
7164 }
7165
7166 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7167    specified by EXPR into the buffer PTR of length LEN bytes.
7168    Return the number of bytes placed in the buffer, or zero
7169    upon failure.  */
7170
7171 static int
7172 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7173 {
7174   tree type = TREE_TYPE (expr);
7175   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7176   int byte, offset, word, words;
7177   unsigned char value;
7178
7179   if (total_bytes > len)
7180     return 0;
7181   words = total_bytes / UNITS_PER_WORD;
7182
7183   for (byte = 0; byte < total_bytes; byte++)
7184     {
7185       int bitpos = byte * BITS_PER_UNIT;
7186       if (bitpos < HOST_BITS_PER_WIDE_INT)
7187         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7188       else
7189         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7190                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7191
7192       if (total_bytes > UNITS_PER_WORD)
7193         {
7194           word = byte / UNITS_PER_WORD;
7195           if (WORDS_BIG_ENDIAN)
7196             word = (words - 1) - word;
7197           offset = word * UNITS_PER_WORD;
7198           if (BYTES_BIG_ENDIAN)
7199             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7200           else
7201             offset += byte % UNITS_PER_WORD;
7202         }
7203       else
7204         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7205       ptr[offset] = value;
7206     }
7207   return total_bytes;
7208 }
7209
7210
7211 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7212    specified by EXPR into the buffer PTR of length LEN bytes.
7213    Return the number of bytes placed in the buffer, or zero
7214    upon failure.  */
7215
7216 static int
7217 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7218 {
7219   tree type = TREE_TYPE (expr);
7220   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7221   int byte, offset, word, words, bitpos;
7222   unsigned char value;
7223
7224   /* There are always 32 bits in each long, no matter the size of
7225      the hosts long.  We handle floating point representations with
7226      up to 192 bits.  */
7227   long tmp[6];
7228
7229   if (total_bytes > len)
7230     return 0;
7231   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7232
7233   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7234
7235   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7236        bitpos += BITS_PER_UNIT)
7237     {
7238       byte = (bitpos / BITS_PER_UNIT) & 3;
7239       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7240
7241       if (UNITS_PER_WORD < 4)
7242         {
7243           word = byte / UNITS_PER_WORD;
7244           if (WORDS_BIG_ENDIAN)
7245             word = (words - 1) - word;
7246           offset = word * UNITS_PER_WORD;
7247           if (BYTES_BIG_ENDIAN)
7248             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7249           else
7250             offset += byte % UNITS_PER_WORD;
7251         }
7252       else
7253         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7254       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7255     }
7256   return total_bytes;
7257 }
7258
7259 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7260    specified by EXPR into the buffer PTR of length LEN bytes.
7261    Return the number of bytes placed in the buffer, or zero
7262    upon failure.  */
7263
7264 static int
7265 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7266 {
7267   int rsize, isize;
7268   tree part;
7269
7270   part = TREE_REALPART (expr);
7271   rsize = native_encode_expr (part, ptr, len);
7272   if (rsize == 0)
7273     return 0;
7274   part = TREE_IMAGPART (expr);
7275   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7276   if (isize != rsize)
7277     return 0;
7278   return rsize + isize;
7279 }
7280
7281
7282 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7283    specified by EXPR into the buffer PTR of length LEN bytes.
7284    Return the number of bytes placed in the buffer, or zero
7285    upon failure.  */
7286
7287 static int
7288 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7289 {
7290   int i, size, offset, count;
7291   tree itype, elem, elements;
7292
7293   offset = 0;
7294   elements = TREE_VECTOR_CST_ELTS (expr);
7295   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7296   itype = TREE_TYPE (TREE_TYPE (expr));
7297   size = GET_MODE_SIZE (TYPE_MODE (itype));
7298   for (i = 0; i < count; i++)
7299     {
7300       if (elements)
7301         {
7302           elem = TREE_VALUE (elements);
7303           elements = TREE_CHAIN (elements);
7304         }
7305       else
7306         elem = NULL_TREE;
7307
7308       if (elem)
7309         {
7310           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7311             return 0;
7312         }
7313       else
7314         {
7315           if (offset + size > len)
7316             return 0;
7317           memset (ptr+offset, 0, size);
7318         }
7319       offset += size;
7320     }
7321   return offset;
7322 }
7323
7324
7325 /* Subroutine of native_encode_expr.  Encode the STRING_CST
7326    specified by EXPR into the buffer PTR of length LEN bytes.
7327    Return the number of bytes placed in the buffer, or zero
7328    upon failure.  */
7329
7330 static int
7331 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7332 {
7333   tree type = TREE_TYPE (expr);
7334   HOST_WIDE_INT total_bytes;
7335
7336   if (TREE_CODE (type) != ARRAY_TYPE
7337       || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7338       || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7339       || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7340     return 0;
7341   total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7342   if (total_bytes > len)
7343     return 0;
7344   if (TREE_STRING_LENGTH (expr) < total_bytes)
7345     {
7346       memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7347       memset (ptr + TREE_STRING_LENGTH (expr), 0,
7348               total_bytes - TREE_STRING_LENGTH (expr));
7349     }
7350   else
7351     memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7352   return total_bytes;
7353 }
7354
7355
7356 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7357    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7358    buffer PTR of length LEN bytes.  Return the number of bytes
7359    placed in the buffer, or zero upon failure.  */
7360
7361 int
7362 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7363 {
7364   switch (TREE_CODE (expr))
7365     {
7366     case INTEGER_CST:
7367       return native_encode_int (expr, ptr, len);
7368
7369     case REAL_CST:
7370       return native_encode_real (expr, ptr, len);
7371
7372     case COMPLEX_CST:
7373       return native_encode_complex (expr, ptr, len);
7374
7375     case VECTOR_CST:
7376       return native_encode_vector (expr, ptr, len);
7377
7378     case STRING_CST:
7379       return native_encode_string (expr, ptr, len);
7380
7381     default:
7382       return 0;
7383     }
7384 }
7385
7386
7387 /* Subroutine of native_interpret_expr.  Interpret the contents of
7388    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7389    If the buffer cannot be interpreted, return NULL_TREE.  */
7390
7391 static tree
7392 native_interpret_int (tree type, const unsigned char *ptr, int len)
7393 {
7394   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7395   int byte, offset, word, words;
7396   unsigned char value;
7397   double_int result;
7398
7399   if (total_bytes > len)
7400     return NULL_TREE;
7401   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7402     return NULL_TREE;
7403
7404   result = double_int_zero;
7405   words = total_bytes / UNITS_PER_WORD;
7406
7407   for (byte = 0; byte < total_bytes; byte++)
7408     {
7409       int bitpos = byte * BITS_PER_UNIT;
7410       if (total_bytes > UNITS_PER_WORD)
7411         {
7412           word = byte / UNITS_PER_WORD;
7413           if (WORDS_BIG_ENDIAN)
7414             word = (words - 1) - word;
7415           offset = word * UNITS_PER_WORD;
7416           if (BYTES_BIG_ENDIAN)
7417             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7418           else
7419             offset += byte % UNITS_PER_WORD;
7420         }
7421       else
7422         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7423       value = ptr[offset];
7424
7425       if (bitpos < HOST_BITS_PER_WIDE_INT)
7426         result.low |= (unsigned HOST_WIDE_INT) value << bitpos;
7427       else
7428         result.high |= (unsigned HOST_WIDE_INT) value
7429                        << (bitpos - HOST_BITS_PER_WIDE_INT);
7430     }
7431
7432   return double_int_to_tree (type, result);
7433 }
7434
7435
7436 /* Subroutine of native_interpret_expr.  Interpret the contents of
7437    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7438    If the buffer cannot be interpreted, return NULL_TREE.  */
7439
7440 static tree
7441 native_interpret_real (tree type, const unsigned char *ptr, int len)
7442 {
7443   enum machine_mode mode = TYPE_MODE (type);
7444   int total_bytes = GET_MODE_SIZE (mode);
7445   int byte, offset, word, words, bitpos;
7446   unsigned char value;
7447   /* There are always 32 bits in each long, no matter the size of
7448      the hosts long.  We handle floating point representations with
7449      up to 192 bits.  */
7450   REAL_VALUE_TYPE r;
7451   long tmp[6];
7452
7453   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7454   if (total_bytes > len || total_bytes > 24)
7455     return NULL_TREE;
7456   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7457
7458   memset (tmp, 0, sizeof (tmp));
7459   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7460        bitpos += BITS_PER_UNIT)
7461     {
7462       byte = (bitpos / BITS_PER_UNIT) & 3;
7463       if (UNITS_PER_WORD < 4)
7464         {
7465           word = byte / UNITS_PER_WORD;
7466           if (WORDS_BIG_ENDIAN)
7467             word = (words - 1) - word;
7468           offset = word * UNITS_PER_WORD;
7469           if (BYTES_BIG_ENDIAN)
7470             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7471           else
7472             offset += byte % UNITS_PER_WORD;
7473         }
7474       else
7475         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7476       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7477
7478       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7479     }
7480
7481   real_from_target (&r, tmp, mode);
7482   return build_real (type, r);
7483 }
7484
7485
7486 /* Subroutine of native_interpret_expr.  Interpret the contents of
7487    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7488    If the buffer cannot be interpreted, return NULL_TREE.  */
7489
7490 static tree
7491 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7492 {
7493   tree etype, rpart, ipart;
7494   int size;
7495
7496   etype = TREE_TYPE (type);
7497   size = GET_MODE_SIZE (TYPE_MODE (etype));
7498   if (size * 2 > len)
7499     return NULL_TREE;
7500   rpart = native_interpret_expr (etype, ptr, size);
7501   if (!rpart)
7502     return NULL_TREE;
7503   ipart = native_interpret_expr (etype, ptr+size, size);
7504   if (!ipart)
7505     return NULL_TREE;
7506   return build_complex (type, rpart, ipart);
7507 }
7508
7509
7510 /* Subroutine of native_interpret_expr.  Interpret the contents of
7511    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7512    If the buffer cannot be interpreted, return NULL_TREE.  */
7513
7514 static tree
7515 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7516 {
7517   tree etype, elem, elements;
7518   int i, size, count;
7519
7520   etype = TREE_TYPE (type);
7521   size = GET_MODE_SIZE (TYPE_MODE (etype));
7522   count = TYPE_VECTOR_SUBPARTS (type);
7523   if (size * count > len)
7524     return NULL_TREE;
7525
7526   elements = NULL_TREE;
7527   for (i = count - 1; i >= 0; i--)
7528     {
7529       elem = native_interpret_expr (etype, ptr+(i*size), size);
7530       if (!elem)
7531         return NULL_TREE;
7532       elements = tree_cons (NULL_TREE, elem, elements);
7533     }
7534   return build_vector (type, elements);
7535 }
7536
7537
7538 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7539    the buffer PTR of length LEN as a constant of type TYPE.  For
7540    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7541    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7542    return NULL_TREE.  */
7543
7544 tree
7545 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7546 {
7547   switch (TREE_CODE (type))
7548     {
7549     case INTEGER_TYPE:
7550     case ENUMERAL_TYPE:
7551     case BOOLEAN_TYPE:
7552       return native_interpret_int (type, ptr, len);
7553
7554     case REAL_TYPE:
7555       return native_interpret_real (type, ptr, len);
7556
7557     case COMPLEX_TYPE:
7558       return native_interpret_complex (type, ptr, len);
7559
7560     case VECTOR_TYPE:
7561       return native_interpret_vector (type, ptr, len);
7562
7563     default:
7564       return NULL_TREE;
7565     }
7566 }
7567
7568
7569 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7570    TYPE at compile-time.  If we're unable to perform the conversion
7571    return NULL_TREE.  */
7572
7573 static tree
7574 fold_view_convert_expr (tree type, tree expr)
7575 {
7576   /* We support up to 512-bit values (for V8DFmode).  */
7577   unsigned char buffer[64];
7578   int len;
7579
7580   /* Check that the host and target are sane.  */
7581   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7582     return NULL_TREE;
7583
7584   len = native_encode_expr (expr, buffer, sizeof (buffer));
7585   if (len == 0)
7586     return NULL_TREE;
7587
7588   return native_interpret_expr (type, buffer, len);
7589 }
7590
7591 /* Build an expression for the address of T.  Folds away INDIRECT_REF
7592    to avoid confusing the gimplify process.  */
7593
7594 tree
7595 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7596 {
7597   /* The size of the object is not relevant when talking about its address.  */
7598   if (TREE_CODE (t) == WITH_SIZE_EXPR)
7599     t = TREE_OPERAND (t, 0);
7600
7601   if (TREE_CODE (t) == INDIRECT_REF)
7602     {
7603       t = TREE_OPERAND (t, 0);
7604
7605       if (TREE_TYPE (t) != ptrtype)
7606         {
7607           t = build1 (NOP_EXPR, ptrtype, t);
7608           SET_EXPR_LOCATION (t, loc);
7609         }
7610     }
7611   else if (TREE_CODE (t) == MEM_REF
7612       && integer_zerop (TREE_OPERAND (t, 1)))
7613     return TREE_OPERAND (t, 0);
7614   else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7615     {
7616       t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7617
7618       if (TREE_TYPE (t) != ptrtype)
7619         t = fold_convert_loc (loc, ptrtype, t);
7620     }
7621   else
7622     {
7623       t = build1 (ADDR_EXPR, ptrtype, t);
7624       SET_EXPR_LOCATION (t, loc);
7625     }
7626
7627   return t;
7628 }
7629
7630 /* Build an expression for the address of T.  */
7631
7632 tree
7633 build_fold_addr_expr_loc (location_t loc, tree t)
7634 {
7635   tree ptrtype = build_pointer_type (TREE_TYPE (t));
7636
7637   return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7638 }
7639
7640 /* Fold a unary expression of code CODE and type TYPE with operand
7641    OP0.  Return the folded expression if folding is successful.
7642    Otherwise, return NULL_TREE.  */
7643
7644 tree
7645 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7646 {
7647   tree tem;
7648   tree arg0;
7649   enum tree_code_class kind = TREE_CODE_CLASS (code);
7650
7651   gcc_assert (IS_EXPR_CODE_CLASS (kind)
7652               && TREE_CODE_LENGTH (code) == 1);
7653
7654   arg0 = op0;
7655   if (arg0)
7656     {
7657       if (CONVERT_EXPR_CODE_P (code)
7658           || code == FLOAT_EXPR || code == ABS_EXPR)
7659         {
7660           /* Don't use STRIP_NOPS, because signedness of argument type
7661              matters.  */
7662           STRIP_SIGN_NOPS (arg0);
7663         }
7664       else
7665         {
7666           /* Strip any conversions that don't change the mode.  This
7667              is safe for every expression, except for a comparison
7668              expression because its signedness is derived from its
7669              operands.
7670
7671              Note that this is done as an internal manipulation within
7672              the constant folder, in order to find the simplest
7673              representation of the arguments so that their form can be
7674              studied.  In any cases, the appropriate type conversions
7675              should be put back in the tree that will get out of the
7676              constant folder.  */
7677           STRIP_NOPS (arg0);
7678         }
7679     }
7680
7681   if (TREE_CODE_CLASS (code) == tcc_unary)
7682     {
7683       if (TREE_CODE (arg0) == COMPOUND_EXPR)
7684         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7685                        fold_build1_loc (loc, code, type,
7686                                     fold_convert_loc (loc, TREE_TYPE (op0),
7687                                                       TREE_OPERAND (arg0, 1))));
7688       else if (TREE_CODE (arg0) == COND_EXPR)
7689         {
7690           tree arg01 = TREE_OPERAND (arg0, 1);
7691           tree arg02 = TREE_OPERAND (arg0, 2);
7692           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7693             arg01 = fold_build1_loc (loc, code, type,
7694                                  fold_convert_loc (loc,
7695                                                    TREE_TYPE (op0), arg01));
7696           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7697             arg02 = fold_build1_loc (loc, code, type,
7698                                  fold_convert_loc (loc,
7699                                                    TREE_TYPE (op0), arg02));
7700           tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7701                              arg01, arg02);
7702
7703           /* If this was a conversion, and all we did was to move into
7704              inside the COND_EXPR, bring it back out.  But leave it if
7705              it is a conversion from integer to integer and the
7706              result precision is no wider than a word since such a
7707              conversion is cheap and may be optimized away by combine,
7708              while it couldn't if it were outside the COND_EXPR.  Then return
7709              so we don't get into an infinite recursion loop taking the
7710              conversion out and then back in.  */
7711
7712           if ((CONVERT_EXPR_CODE_P (code)
7713                || code == NON_LVALUE_EXPR)
7714               && TREE_CODE (tem) == COND_EXPR
7715               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7716               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7717               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7718               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7719               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7720                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7721               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7722                      && (INTEGRAL_TYPE_P
7723                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7724                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7725                   || flag_syntax_only))
7726             {
7727               tem = build1 (code, type,
7728                             build3 (COND_EXPR,
7729                                     TREE_TYPE (TREE_OPERAND
7730                                                (TREE_OPERAND (tem, 1), 0)),
7731                                     TREE_OPERAND (tem, 0),
7732                                     TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7733                                     TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
7734               SET_EXPR_LOCATION (tem, loc);
7735             }
7736           return tem;
7737         }
7738       else if (COMPARISON_CLASS_P (arg0))
7739         {
7740           if (TREE_CODE (type) == BOOLEAN_TYPE)
7741             {
7742               arg0 = copy_node (arg0);
7743               TREE_TYPE (arg0) = type;
7744               return arg0;
7745             }
7746           else if (TREE_CODE (type) != INTEGER_TYPE)
7747             return fold_build3_loc (loc, COND_EXPR, type, arg0,
7748                                 fold_build1_loc (loc, code, type,
7749                                              integer_one_node),
7750                                 fold_build1_loc (loc, code, type,
7751                                              integer_zero_node));
7752         }
7753    }
7754
7755   switch (code)
7756     {
7757     case PAREN_EXPR:
7758       /* Re-association barriers around constants and other re-association
7759          barriers can be removed.  */
7760       if (CONSTANT_CLASS_P (op0)
7761           || TREE_CODE (op0) == PAREN_EXPR)
7762         return fold_convert_loc (loc, type, op0);
7763       return NULL_TREE;
7764
7765     CASE_CONVERT:
7766     case FLOAT_EXPR:
7767     case FIX_TRUNC_EXPR:
7768       if (TREE_TYPE (op0) == type)
7769         return op0;
7770
7771       /* If we have (type) (a CMP b) and type is an integral type, return
7772          new expression involving the new type.  */
7773       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
7774         return fold_build2_loc (loc, TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
7775                             TREE_OPERAND (op0, 1));
7776
7777       /* Handle cases of two conversions in a row.  */
7778       if (CONVERT_EXPR_P (op0))
7779         {
7780           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7781           tree inter_type = TREE_TYPE (op0);
7782           int inside_int = INTEGRAL_TYPE_P (inside_type);
7783           int inside_ptr = POINTER_TYPE_P (inside_type);
7784           int inside_float = FLOAT_TYPE_P (inside_type);
7785           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7786           unsigned int inside_prec = TYPE_PRECISION (inside_type);
7787           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7788           int inter_int = INTEGRAL_TYPE_P (inter_type);
7789           int inter_ptr = POINTER_TYPE_P (inter_type);
7790           int inter_float = FLOAT_TYPE_P (inter_type);
7791           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7792           unsigned int inter_prec = TYPE_PRECISION (inter_type);
7793           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7794           int final_int = INTEGRAL_TYPE_P (type);
7795           int final_ptr = POINTER_TYPE_P (type);
7796           int final_float = FLOAT_TYPE_P (type);
7797           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7798           unsigned int final_prec = TYPE_PRECISION (type);
7799           int final_unsignedp = TYPE_UNSIGNED (type);
7800
7801           /* In addition to the cases of two conversions in a row
7802              handled below, if we are converting something to its own
7803              type via an object of identical or wider precision, neither
7804              conversion is needed.  */
7805           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7806               && (((inter_int || inter_ptr) && final_int)
7807                   || (inter_float && final_float))
7808               && inter_prec >= final_prec)
7809             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7810
7811           /* Likewise, if the intermediate and initial types are either both
7812              float or both integer, we don't need the middle conversion if the
7813              former is wider than the latter and doesn't change the signedness
7814              (for integers).  Avoid this if the final type is a pointer since
7815              then we sometimes need the middle conversion.  Likewise if the
7816              final type has a precision not equal to the size of its mode.  */
7817           if (((inter_int && inside_int)
7818                || (inter_float && inside_float)
7819                || (inter_vec && inside_vec))
7820               && inter_prec >= inside_prec
7821               && (inter_float || inter_vec
7822                   || inter_unsignedp == inside_unsignedp)
7823               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7824                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
7825               && ! final_ptr
7826               && (! final_vec || inter_prec == inside_prec))
7827             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7828
7829           /* If we have a sign-extension of a zero-extended value, we can
7830              replace that by a single zero-extension.  */
7831           if (inside_int && inter_int && final_int
7832               && inside_prec < inter_prec && inter_prec < final_prec
7833               && inside_unsignedp && !inter_unsignedp)
7834             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7835
7836           /* Two conversions in a row are not needed unless:
7837              - some conversion is floating-point (overstrict for now), or
7838              - some conversion is a vector (overstrict for now), or
7839              - the intermediate type is narrower than both initial and
7840                final, or
7841              - the intermediate type and innermost type differ in signedness,
7842                and the outermost type is wider than the intermediate, or
7843              - the initial type is a pointer type and the precisions of the
7844                intermediate and final types differ, or
7845              - the final type is a pointer type and the precisions of the
7846                initial and intermediate types differ.  */
7847           if (! inside_float && ! inter_float && ! final_float
7848               && ! inside_vec && ! inter_vec && ! final_vec
7849               && (inter_prec >= inside_prec || inter_prec >= final_prec)
7850               && ! (inside_int && inter_int
7851                     && inter_unsignedp != inside_unsignedp
7852                     && inter_prec < final_prec)
7853               && ((inter_unsignedp && inter_prec > inside_prec)
7854                   == (final_unsignedp && final_prec > inter_prec))
7855               && ! (inside_ptr && inter_prec != final_prec)
7856               && ! (final_ptr && inside_prec != inter_prec)
7857               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7858                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7859             return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7860         }
7861
7862       /* Handle (T *)&A.B.C for A being of type T and B and C
7863          living at offset zero.  This occurs frequently in
7864          C++ upcasting and then accessing the base.  */
7865       if (TREE_CODE (op0) == ADDR_EXPR
7866           && POINTER_TYPE_P (type)
7867           && handled_component_p (TREE_OPERAND (op0, 0)))
7868         {
7869           HOST_WIDE_INT bitsize, bitpos;
7870           tree offset;
7871           enum machine_mode mode;
7872           int unsignedp, volatilep;
7873           tree base = TREE_OPERAND (op0, 0);
7874           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7875                                       &mode, &unsignedp, &volatilep, false);
7876           /* If the reference was to a (constant) zero offset, we can use
7877              the address of the base if it has the same base type
7878              as the result type and the pointer type is unqualified.  */
7879           if (! offset && bitpos == 0
7880               && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7881                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7882               && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7883             return fold_convert_loc (loc, type,
7884                                      build_fold_addr_expr_loc (loc, base));
7885         }
7886
7887       if (TREE_CODE (op0) == MODIFY_EXPR
7888           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7889           /* Detect assigning a bitfield.  */
7890           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7891                && DECL_BIT_FIELD
7892                (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7893         {
7894           /* Don't leave an assignment inside a conversion
7895              unless assigning a bitfield.  */
7896           tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7897           /* First do the assignment, then return converted constant.  */
7898           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7899           TREE_NO_WARNING (tem) = 1;
7900           TREE_USED (tem) = 1;
7901           SET_EXPR_LOCATION (tem, loc);
7902           return tem;
7903         }
7904
7905       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7906          constants (if x has signed type, the sign bit cannot be set
7907          in c).  This folds extension into the BIT_AND_EXPR.
7908          ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7909          very likely don't have maximal range for their precision and this
7910          transformation effectively doesn't preserve non-maximal ranges.  */
7911       if (TREE_CODE (type) == INTEGER_TYPE
7912           && TREE_CODE (op0) == BIT_AND_EXPR
7913           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7914         {
7915           tree and_expr = op0;
7916           tree and0 = TREE_OPERAND (and_expr, 0);
7917           tree and1 = TREE_OPERAND (and_expr, 1);
7918           int change = 0;
7919
7920           if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7921               || (TYPE_PRECISION (type)
7922                   <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7923             change = 1;
7924           else if (TYPE_PRECISION (TREE_TYPE (and1))
7925                    <= HOST_BITS_PER_WIDE_INT
7926                    && host_integerp (and1, 1))
7927             {
7928               unsigned HOST_WIDE_INT cst;
7929
7930               cst = tree_low_cst (and1, 1);
7931               cst &= (HOST_WIDE_INT) -1
7932                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7933               change = (cst == 0);
7934 #ifdef LOAD_EXTEND_OP
7935               if (change
7936                   && !flag_syntax_only
7937                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7938                       == ZERO_EXTEND))
7939                 {
7940                   tree uns = unsigned_type_for (TREE_TYPE (and0));
7941                   and0 = fold_convert_loc (loc, uns, and0);
7942                   and1 = fold_convert_loc (loc, uns, and1);
7943                 }
7944 #endif
7945             }
7946           if (change)
7947             {
7948               tem = force_fit_type_double (type, tree_to_double_int (and1),
7949                                            0, TREE_OVERFLOW (and1));
7950               return fold_build2_loc (loc, BIT_AND_EXPR, type,
7951                                   fold_convert_loc (loc, type, and0), tem);
7952             }
7953         }
7954
7955       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7956          when one of the new casts will fold away. Conservatively we assume
7957          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7958       if (POINTER_TYPE_P (type)
7959           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7960           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7961               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7962               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7963         {
7964           tree arg00 = TREE_OPERAND (arg0, 0);
7965           tree arg01 = TREE_OPERAND (arg0, 1);
7966
7967           return fold_build2_loc (loc,
7968                               TREE_CODE (arg0), type,
7969                               fold_convert_loc (loc, type, arg00),
7970                               fold_convert_loc (loc, sizetype, arg01));
7971         }
7972
7973       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7974          of the same precision, and X is an integer type not narrower than
7975          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
7976       if (INTEGRAL_TYPE_P (type)
7977           && TREE_CODE (op0) == BIT_NOT_EXPR
7978           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7979           && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7980           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7981         {
7982           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7983           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7984               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7985             return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7986                                 fold_convert_loc (loc, type, tem));
7987         }
7988
7989       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7990          type of X and Y (integer types only).  */
7991       if (INTEGRAL_TYPE_P (type)
7992           && TREE_CODE (op0) == MULT_EXPR
7993           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7994           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7995         {
7996           /* Be careful not to introduce new overflows.  */
7997           tree mult_type;
7998           if (TYPE_OVERFLOW_WRAPS (type))
7999             mult_type = type;
8000           else
8001             mult_type = unsigned_type_for (type);
8002
8003           if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8004             {
8005               tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8006                                  fold_convert_loc (loc, mult_type,
8007                                                    TREE_OPERAND (op0, 0)),
8008                                  fold_convert_loc (loc, mult_type,
8009                                                    TREE_OPERAND (op0, 1)));
8010               return fold_convert_loc (loc, type, tem);
8011             }
8012         }
8013
8014       tem = fold_convert_const (code, type, op0);
8015       return tem ? tem : NULL_TREE;
8016
8017     case ADDR_SPACE_CONVERT_EXPR:
8018       if (integer_zerop (arg0))
8019         return fold_convert_const (code, type, arg0);
8020       return NULL_TREE;
8021
8022     case FIXED_CONVERT_EXPR:
8023       tem = fold_convert_const (code, type, arg0);
8024       return tem ? tem : NULL_TREE;
8025
8026     case VIEW_CONVERT_EXPR:
8027       if (TREE_TYPE (op0) == type)
8028         return op0;
8029       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8030         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8031                             type, TREE_OPERAND (op0, 0));
8032       if (TREE_CODE (op0) == MEM_REF)
8033         return fold_build2_loc (loc, MEM_REF, type,
8034                                 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
8035
8036       /* For integral conversions with the same precision or pointer
8037          conversions use a NOP_EXPR instead.  */
8038       if ((INTEGRAL_TYPE_P (type)
8039            || POINTER_TYPE_P (type))
8040           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8041               || POINTER_TYPE_P (TREE_TYPE (op0)))
8042           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8043         return fold_convert_loc (loc, type, op0);
8044
8045       /* Strip inner integral conversions that do not change the precision.  */
8046       if (CONVERT_EXPR_P (op0)
8047           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8048               || POINTER_TYPE_P (TREE_TYPE (op0)))
8049           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8050               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8051           && (TYPE_PRECISION (TREE_TYPE (op0))
8052               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8053         return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8054                             type, TREE_OPERAND (op0, 0));
8055
8056       return fold_view_convert_expr (type, op0);
8057
8058     case NEGATE_EXPR:
8059       tem = fold_negate_expr (loc, arg0);
8060       if (tem)
8061         return fold_convert_loc (loc, type, tem);
8062       return NULL_TREE;
8063
8064     case ABS_EXPR:
8065       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8066         return fold_abs_const (arg0, type);
8067       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8068         return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8069       /* Convert fabs((double)float) into (double)fabsf(float).  */
8070       else if (TREE_CODE (arg0) == NOP_EXPR
8071                && TREE_CODE (type) == REAL_TYPE)
8072         {
8073           tree targ0 = strip_float_extensions (arg0);
8074           if (targ0 != arg0)
8075             return fold_convert_loc (loc, type,
8076                                      fold_build1_loc (loc, ABS_EXPR,
8077                                                   TREE_TYPE (targ0),
8078                                                   targ0));
8079         }
8080       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8081       else if (TREE_CODE (arg0) == ABS_EXPR)
8082         return arg0;
8083       else if (tree_expr_nonnegative_p (arg0))
8084         return arg0;
8085
8086       /* Strip sign ops from argument.  */
8087       if (TREE_CODE (type) == REAL_TYPE)
8088         {
8089           tem = fold_strip_sign_ops (arg0);
8090           if (tem)
8091             return fold_build1_loc (loc, ABS_EXPR, type,
8092                                 fold_convert_loc (loc, type, tem));
8093         }
8094       return NULL_TREE;
8095
8096     case CONJ_EXPR:
8097       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8098         return fold_convert_loc (loc, type, arg0);
8099       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8100         {
8101           tree itype = TREE_TYPE (type);
8102           tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8103           tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8104           return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8105                               negate_expr (ipart));
8106         }
8107       if (TREE_CODE (arg0) == COMPLEX_CST)
8108         {
8109           tree itype = TREE_TYPE (type);
8110           tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8111           tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8112           return build_complex (type, rpart, negate_expr (ipart));
8113         }
8114       if (TREE_CODE (arg0) == CONJ_EXPR)
8115         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8116       return NULL_TREE;
8117
8118     case BIT_NOT_EXPR:
8119       if (TREE_CODE (arg0) == INTEGER_CST)
8120         return fold_not_const (arg0, type);
8121       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8122         return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8123       /* Convert ~ (-A) to A - 1.  */
8124       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8125         return fold_build2_loc (loc, MINUS_EXPR, type,
8126                             fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8127                             build_int_cst (type, 1));
8128       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8129       else if (INTEGRAL_TYPE_P (type)
8130                && ((TREE_CODE (arg0) == MINUS_EXPR
8131                     && integer_onep (TREE_OPERAND (arg0, 1)))
8132                    || (TREE_CODE (arg0) == PLUS_EXPR
8133                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8134         return fold_build1_loc (loc, NEGATE_EXPR, type,
8135                             fold_convert_loc (loc, type,
8136                                               TREE_OPERAND (arg0, 0)));
8137       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8138       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8139                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8140                                      fold_convert_loc (loc, type,
8141                                                        TREE_OPERAND (arg0, 0)))))
8142         return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8143                             fold_convert_loc (loc, type,
8144                                               TREE_OPERAND (arg0, 1)));
8145       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8146                && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8147                                      fold_convert_loc (loc, type,
8148                                                        TREE_OPERAND (arg0, 1)))))
8149         return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8150                             fold_convert_loc (loc, type,
8151                                               TREE_OPERAND (arg0, 0)), tem);
8152       /* Perform BIT_NOT_EXPR on each element individually.  */
8153       else if (TREE_CODE (arg0) == VECTOR_CST)
8154         {
8155           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8156           int count = TYPE_VECTOR_SUBPARTS (type), i;
8157
8158           for (i = 0; i < count; i++)
8159             {
8160               if (elements)
8161                 {
8162                   elem = TREE_VALUE (elements);
8163                   elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8164                   if (elem == NULL_TREE)
8165                     break;
8166                   elements = TREE_CHAIN (elements);
8167                 }
8168               else
8169                 elem = build_int_cst (TREE_TYPE (type), -1);
8170               list = tree_cons (NULL_TREE, elem, list);
8171             }
8172           if (i == count)
8173             return build_vector (type, nreverse (list));
8174         }
8175
8176       return NULL_TREE;
8177
8178     case TRUTH_NOT_EXPR:
8179       /* The argument to invert_truthvalue must have Boolean type.  */
8180       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8181           arg0 = fold_convert_loc (loc, boolean_type_node, arg0);
8182
8183       /* Note that the operand of this must be an int
8184          and its values must be 0 or 1.
8185          ("true" is a fixed value perhaps depending on the language,
8186          but we don't handle values other than 1 correctly yet.)  */
8187       tem = fold_truth_not_expr (loc, arg0);
8188       if (!tem)
8189         return NULL_TREE;
8190       return fold_convert_loc (loc, type, tem);
8191
8192     case REALPART_EXPR:
8193       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8194         return fold_convert_loc (loc, type, arg0);
8195       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8196         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8197                                  TREE_OPERAND (arg0, 1));
8198       if (TREE_CODE (arg0) == COMPLEX_CST)
8199         return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8200       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8201         {
8202           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8203           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8204                              fold_build1_loc (loc, REALPART_EXPR, itype,
8205                                           TREE_OPERAND (arg0, 0)),
8206                              fold_build1_loc (loc, REALPART_EXPR, itype,
8207                                           TREE_OPERAND (arg0, 1)));
8208           return fold_convert_loc (loc, type, tem);
8209         }
8210       if (TREE_CODE (arg0) == CONJ_EXPR)
8211         {
8212           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8213           tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8214                              TREE_OPERAND (arg0, 0));
8215           return fold_convert_loc (loc, type, tem);
8216         }
8217       if (TREE_CODE (arg0) == CALL_EXPR)
8218         {
8219           tree fn = get_callee_fndecl (arg0);
8220           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8221             switch (DECL_FUNCTION_CODE (fn))
8222               {
8223               CASE_FLT_FN (BUILT_IN_CEXPI):
8224                 fn = mathfn_built_in (type, BUILT_IN_COS);
8225                 if (fn)
8226                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8227                 break;
8228
8229               default:
8230                 break;
8231               }
8232         }
8233       return NULL_TREE;
8234
8235     case IMAGPART_EXPR:
8236       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8237         return fold_convert_loc (loc, type, integer_zero_node);
8238       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8239         return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8240                                  TREE_OPERAND (arg0, 0));
8241       if (TREE_CODE (arg0) == COMPLEX_CST)
8242         return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8243       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8244         {
8245           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8246           tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8247                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8248                                           TREE_OPERAND (arg0, 0)),
8249                              fold_build1_loc (loc, IMAGPART_EXPR, itype,
8250                                           TREE_OPERAND (arg0, 1)));
8251           return fold_convert_loc (loc, type, tem);
8252         }
8253       if (TREE_CODE (arg0) == CONJ_EXPR)
8254         {
8255           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8256           tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8257           return fold_convert_loc (loc, type, negate_expr (tem));
8258         }
8259       if (TREE_CODE (arg0) == CALL_EXPR)
8260         {
8261           tree fn = get_callee_fndecl (arg0);
8262           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8263             switch (DECL_FUNCTION_CODE (fn))
8264               {
8265               CASE_FLT_FN (BUILT_IN_CEXPI):
8266                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8267                 if (fn)
8268                   return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8269                 break;
8270
8271               default:
8272                 break;
8273               }
8274         }
8275       return NULL_TREE;
8276
8277     case INDIRECT_REF:
8278       /* Fold *&X to X if X is an lvalue.  */
8279       if (TREE_CODE (op0) == ADDR_EXPR)
8280         {
8281           tree op00 = TREE_OPERAND (op0, 0);
8282           if ((TREE_CODE (op00) == VAR_DECL
8283                || TREE_CODE (op00) == PARM_DECL
8284                || TREE_CODE (op00) == RESULT_DECL)
8285               && !TREE_READONLY (op00))
8286             return op00;
8287         }
8288       return NULL_TREE;
8289
8290     default:
8291       return NULL_TREE;
8292     } /* switch (code) */
8293 }
8294
8295
8296 /* If the operation was a conversion do _not_ mark a resulting constant
8297    with TREE_OVERFLOW if the original constant was not.  These conversions
8298    have implementation defined behavior and retaining the TREE_OVERFLOW
8299    flag here would confuse later passes such as VRP.  */
8300 tree
8301 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8302                                 tree type, tree op0)
8303 {
8304   tree res = fold_unary_loc (loc, code, type, op0);
8305   if (res
8306       && TREE_CODE (res) == INTEGER_CST
8307       && TREE_CODE (op0) == INTEGER_CST
8308       && CONVERT_EXPR_CODE_P (code))
8309     TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8310
8311   return res;
8312 }
8313
8314 /* Fold a binary expression of code CODE and type TYPE with operands
8315    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8316    Return the folded expression if folding is successful.  Otherwise,
8317    return NULL_TREE.  */
8318
8319 static tree
8320 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8321 {
8322   enum tree_code compl_code;
8323
8324   if (code == MIN_EXPR)
8325     compl_code = MAX_EXPR;
8326   else if (code == MAX_EXPR)
8327     compl_code = MIN_EXPR;
8328   else
8329     gcc_unreachable ();
8330
8331   /* MIN (MAX (a, b), b) == b.  */
8332   if (TREE_CODE (op0) == compl_code
8333       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8334     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8335
8336   /* MIN (MAX (b, a), b) == b.  */
8337   if (TREE_CODE (op0) == compl_code
8338       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8339       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8340     return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8341
8342   /* MIN (a, MAX (a, b)) == a.  */
8343   if (TREE_CODE (op1) == compl_code
8344       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8345       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8346     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8347
8348   /* MIN (a, MAX (b, a)) == a.  */
8349   if (TREE_CODE (op1) == compl_code
8350       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8351       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8352     return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8353
8354   return NULL_TREE;
8355 }
8356
8357 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8358    by changing CODE to reduce the magnitude of constants involved in
8359    ARG0 of the comparison.
8360    Returns a canonicalized comparison tree if a simplification was
8361    possible, otherwise returns NULL_TREE.
8362    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8363    valid if signed overflow is undefined.  */
8364
8365 static tree
8366 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8367                                  tree arg0, tree arg1,
8368                                  bool *strict_overflow_p)
8369 {
8370   enum tree_code code0 = TREE_CODE (arg0);
8371   tree t, cst0 = NULL_TREE;
8372   int sgn0;
8373   bool swap = false;
8374
8375   /* Match A +- CST code arg1 and CST code arg1.  We can change the
8376      first form only if overflow is undefined.  */
8377   if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8378          /* In principle pointers also have undefined overflow behavior,
8379             but that causes problems elsewhere.  */
8380          && !POINTER_TYPE_P (TREE_TYPE (arg0))
8381          && (code0 == MINUS_EXPR
8382              || code0 == PLUS_EXPR)
8383          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8384         || code0 == INTEGER_CST))
8385     return NULL_TREE;
8386
8387   /* Identify the constant in arg0 and its sign.  */
8388   if (code0 == INTEGER_CST)
8389     cst0 = arg0;
8390   else
8391     cst0 = TREE_OPERAND (arg0, 1);
8392   sgn0 = tree_int_cst_sgn (cst0);
8393
8394   /* Overflowed constants and zero will cause problems.  */
8395   if (integer_zerop (cst0)
8396       || TREE_OVERFLOW (cst0))
8397     return NULL_TREE;
8398
8399   /* See if we can reduce the magnitude of the constant in
8400      arg0 by changing the comparison code.  */
8401   if (code0 == INTEGER_CST)
8402     {
8403       /* CST <= arg1  ->  CST-1 < arg1.  */
8404       if (code == LE_EXPR && sgn0 == 1)
8405         code = LT_EXPR;
8406       /* -CST < arg1  ->  -CST-1 <= arg1.  */
8407       else if (code == LT_EXPR && sgn0 == -1)
8408         code = LE_EXPR;
8409       /* CST > arg1  ->  CST-1 >= arg1.  */
8410       else if (code == GT_EXPR && sgn0 == 1)
8411         code = GE_EXPR;
8412       /* -CST >= arg1  ->  -CST-1 > arg1.  */
8413       else if (code == GE_EXPR && sgn0 == -1)
8414         code = GT_EXPR;
8415       else
8416         return NULL_TREE;
8417       /* arg1 code' CST' might be more canonical.  */
8418       swap = true;
8419     }
8420   else
8421     {
8422       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
8423       if (code == LT_EXPR
8424           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8425         code = LE_EXPR;
8426       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
8427       else if (code == GT_EXPR
8428                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8429         code = GE_EXPR;
8430       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
8431       else if (code == LE_EXPR
8432                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8433         code = LT_EXPR;
8434       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
8435       else if (code == GE_EXPR
8436                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8437         code = GT_EXPR;
8438       else
8439         return NULL_TREE;
8440       *strict_overflow_p = true;
8441     }
8442
8443   /* Now build the constant reduced in magnitude.  But not if that
8444      would produce one outside of its types range.  */
8445   if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8446       && ((sgn0 == 1
8447            && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8448            && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8449           || (sgn0 == -1
8450               && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8451               && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8452     /* We cannot swap the comparison here as that would cause us to
8453        endlessly recurse.  */
8454     return NULL_TREE;
8455
8456   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8457                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8458   if (code0 != INTEGER_CST)
8459     t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8460
8461   /* If swapping might yield to a more canonical form, do so.  */
8462   if (swap)
8463     return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8464   else
8465     return fold_build2_loc (loc, code, type, t, arg1);
8466 }
8467
8468 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8469    overflow further.  Try to decrease the magnitude of constants involved
8470    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8471    and put sole constants at the second argument position.
8472    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
8473
8474 static tree
8475 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8476                                tree arg0, tree arg1)
8477 {
8478   tree t;
8479   bool strict_overflow_p;
8480   const char * const warnmsg = G_("assuming signed overflow does not occur "
8481                                   "when reducing constant in comparison");
8482
8483   /* Try canonicalization by simplifying arg0.  */
8484   strict_overflow_p = false;
8485   t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8486                                        &strict_overflow_p);
8487   if (t)
8488     {
8489       if (strict_overflow_p)
8490         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8491       return t;
8492     }
8493
8494   /* Try canonicalization by simplifying arg1 using the swapped
8495      comparison.  */
8496   code = swap_tree_comparison (code);
8497   strict_overflow_p = false;
8498   t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8499                                        &strict_overflow_p);
8500   if (t && strict_overflow_p)
8501     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8502   return t;
8503 }
8504
8505 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8506    space.  This is used to avoid issuing overflow warnings for
8507    expressions like &p->x which can not wrap.  */
8508
8509 static bool
8510 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8511 {
8512   unsigned HOST_WIDE_INT offset_low, total_low;
8513   HOST_WIDE_INT size, offset_high, total_high;
8514
8515   if (!POINTER_TYPE_P (TREE_TYPE (base)))
8516     return true;
8517
8518   if (bitpos < 0)
8519     return true;
8520
8521   if (offset == NULL_TREE)
8522     {
8523       offset_low = 0;
8524       offset_high = 0;
8525     }
8526   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8527     return true;
8528   else
8529     {
8530       offset_low = TREE_INT_CST_LOW (offset);
8531       offset_high = TREE_INT_CST_HIGH (offset);
8532     }
8533
8534   if (add_double_with_sign (offset_low, offset_high,
8535                             bitpos / BITS_PER_UNIT, 0,
8536                             &total_low, &total_high,
8537                             true))
8538     return true;
8539
8540   if (total_high != 0)
8541     return true;
8542
8543   size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8544   if (size <= 0)
8545     return true;
8546
8547   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8548      array.  */
8549   if (TREE_CODE (base) == ADDR_EXPR)
8550     {
8551       HOST_WIDE_INT base_size;
8552
8553       base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8554       if (base_size > 0 && size < base_size)
8555         size = base_size;
8556     }
8557
8558   return total_low > (unsigned HOST_WIDE_INT) size;
8559 }
8560
8561 /* Subroutine of fold_binary.  This routine performs all of the
8562    transformations that are common to the equality/inequality
8563    operators (EQ_EXPR and NE_EXPR) and the ordering operators
8564    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
8565    fold_binary should call fold_binary.  Fold a comparison with
8566    tree code CODE and type TYPE with operands OP0 and OP1.  Return
8567    the folded comparison or NULL_TREE.  */
8568
8569 static tree
8570 fold_comparison (location_t loc, enum tree_code code, tree type,
8571                  tree op0, tree op1)
8572 {
8573   tree arg0, arg1, tem;
8574
8575   arg0 = op0;
8576   arg1 = op1;
8577
8578   STRIP_SIGN_NOPS (arg0);
8579   STRIP_SIGN_NOPS (arg1);
8580
8581   tem = fold_relational_const (code, type, arg0, arg1);
8582   if (tem != NULL_TREE)
8583     return tem;
8584
8585   /* If one arg is a real or integer constant, put it last.  */
8586   if (tree_swap_operands_p (arg0, arg1, true))
8587     return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
8588
8589   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
8590   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8591       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8592           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8593           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8594       && (TREE_CODE (arg1) == INTEGER_CST
8595           && !TREE_OVERFLOW (arg1)))
8596     {
8597       tree const1 = TREE_OPERAND (arg0, 1);
8598       tree const2 = arg1;
8599       tree variable = TREE_OPERAND (arg0, 0);
8600       tree lhs;
8601       int lhs_add;
8602       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8603
8604       lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
8605                          TREE_TYPE (arg1), const2, const1);
8606
8607       /* If the constant operation overflowed this can be
8608          simplified as a comparison against INT_MAX/INT_MIN.  */
8609       if (TREE_CODE (lhs) == INTEGER_CST
8610           && TREE_OVERFLOW (lhs))
8611         {
8612           int const1_sgn = tree_int_cst_sgn (const1);
8613           enum tree_code code2 = code;
8614
8615           /* Get the sign of the constant on the lhs if the
8616              operation were VARIABLE + CONST1.  */
8617           if (TREE_CODE (arg0) == MINUS_EXPR)
8618             const1_sgn = -const1_sgn;
8619
8620           /* The sign of the constant determines if we overflowed
8621              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8622              Canonicalize to the INT_MIN overflow by swapping the comparison
8623              if necessary.  */
8624           if (const1_sgn == -1)
8625             code2 = swap_tree_comparison (code);
8626
8627           /* We now can look at the canonicalized case
8628                VARIABLE + 1  CODE2  INT_MIN
8629              and decide on the result.  */
8630           if (code2 == LT_EXPR
8631               || code2 == LE_EXPR
8632               || code2 == EQ_EXPR)
8633             return omit_one_operand_loc (loc, type, boolean_false_node, variable);
8634           else if (code2 == NE_EXPR
8635                    || code2 == GE_EXPR
8636                    || code2 == GT_EXPR)
8637             return omit_one_operand_loc (loc, type, boolean_true_node, variable);
8638         }
8639
8640       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8641           && (TREE_CODE (lhs) != INTEGER_CST
8642               || !TREE_OVERFLOW (lhs)))
8643         {
8644           fold_overflow_warning ("assuming signed overflow does not occur "
8645                                   "when changing X +- C1 cmp C2 to "
8646                                   "X cmp C1 +- C2",
8647                                  WARN_STRICT_OVERFLOW_COMPARISON);
8648           return fold_build2_loc (loc, code, type, variable, lhs);
8649         }
8650     }
8651
8652   /* For comparisons of pointers we can decompose it to a compile time
8653      comparison of the base objects and the offsets into the object.
8654      This requires at least one operand being an ADDR_EXPR or a
8655      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
8656   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8657       && (TREE_CODE (arg0) == ADDR_EXPR
8658           || TREE_CODE (arg1) == ADDR_EXPR
8659           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8660           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8661     {
8662       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8663       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8664       enum machine_mode mode;
8665       int volatilep, unsignedp;
8666       bool indirect_base0 = false, indirect_base1 = false;
8667
8668       /* Get base and offset for the access.  Strip ADDR_EXPR for
8669          get_inner_reference, but put it back by stripping INDIRECT_REF
8670          off the base object if possible.  indirect_baseN will be true
8671          if baseN is not an address but refers to the object itself.  */
8672       base0 = arg0;
8673       if (TREE_CODE (arg0) == ADDR_EXPR)
8674         {
8675           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8676                                        &bitsize, &bitpos0, &offset0, &mode,
8677                                        &unsignedp, &volatilep, false);
8678           if (TREE_CODE (base0) == INDIRECT_REF)
8679             base0 = TREE_OPERAND (base0, 0);
8680           else
8681             indirect_base0 = true;
8682         }
8683       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8684         {
8685           base0 = TREE_OPERAND (arg0, 0);
8686           STRIP_SIGN_NOPS (base0);
8687           if (TREE_CODE (base0) == ADDR_EXPR)
8688             {
8689               base0 = TREE_OPERAND (base0, 0);
8690               indirect_base0 = true;
8691             }
8692           offset0 = TREE_OPERAND (arg0, 1);
8693         }
8694
8695       base1 = arg1;
8696       if (TREE_CODE (arg1) == ADDR_EXPR)
8697         {
8698           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8699                                        &bitsize, &bitpos1, &offset1, &mode,
8700                                        &unsignedp, &volatilep, false);
8701           if (TREE_CODE (base1) == INDIRECT_REF)
8702             base1 = TREE_OPERAND (base1, 0);
8703           else
8704             indirect_base1 = true;
8705         }
8706       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8707         {
8708           base1 = TREE_OPERAND (arg1, 0);
8709           STRIP_SIGN_NOPS (base1);
8710           if (TREE_CODE (base1) == ADDR_EXPR)
8711             {
8712               base1 = TREE_OPERAND (base1, 0);
8713               indirect_base1 = true;
8714             }
8715           offset1 = TREE_OPERAND (arg1, 1);
8716         }
8717
8718       /* A local variable can never be pointed to by
8719          the default SSA name of an incoming parameter.  */
8720       if ((TREE_CODE (arg0) == ADDR_EXPR
8721            && indirect_base0
8722            && TREE_CODE (base0) == VAR_DECL
8723            && auto_var_in_fn_p (base0, current_function_decl)
8724            && !indirect_base1
8725            && TREE_CODE (base1) == SSA_NAME
8726            && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL
8727            && SSA_NAME_IS_DEFAULT_DEF (base1))
8728           || (TREE_CODE (arg1) == ADDR_EXPR
8729               && indirect_base1
8730               && TREE_CODE (base1) == VAR_DECL
8731               && auto_var_in_fn_p (base1, current_function_decl)
8732               && !indirect_base0
8733               && TREE_CODE (base0) == SSA_NAME
8734               && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL
8735               && SSA_NAME_IS_DEFAULT_DEF (base0)))
8736         {
8737           if (code == NE_EXPR)
8738             return constant_boolean_node (1, type);
8739           else if (code == EQ_EXPR)
8740             return constant_boolean_node (0, type);
8741         }
8742       /* If we have equivalent bases we might be able to simplify.  */
8743       else if (indirect_base0 == indirect_base1
8744                && operand_equal_p (base0, base1, 0))
8745         {
8746           /* We can fold this expression to a constant if the non-constant
8747              offset parts are equal.  */
8748           if ((offset0 == offset1
8749                || (offset0 && offset1
8750                    && operand_equal_p (offset0, offset1, 0)))
8751               && (code == EQ_EXPR
8752                   || code == NE_EXPR
8753                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
8754
8755             {
8756               if (code != EQ_EXPR
8757                   && code != NE_EXPR
8758                   && bitpos0 != bitpos1
8759                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
8760                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
8761                 fold_overflow_warning (("assuming pointer wraparound does not "
8762                                         "occur when comparing P +- C1 with "
8763                                         "P +- C2"),
8764                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
8765
8766               switch (code)
8767                 {
8768                 case EQ_EXPR:
8769                   return constant_boolean_node (bitpos0 == bitpos1, type);
8770                 case NE_EXPR:
8771                   return constant_boolean_node (bitpos0 != bitpos1, type);
8772                 case LT_EXPR:
8773                   return constant_boolean_node (bitpos0 < bitpos1, type);
8774                 case LE_EXPR:
8775                   return constant_boolean_node (bitpos0 <= bitpos1, type);
8776                 case GE_EXPR:
8777                   return constant_boolean_node (bitpos0 >= bitpos1, type);
8778                 case GT_EXPR:
8779                   return constant_boolean_node (bitpos0 > bitpos1, type);
8780                 default:;
8781                 }
8782             }
8783           /* We can simplify the comparison to a comparison of the variable
8784              offset parts if the constant offset parts are equal.
8785              Be careful to use signed size type here because otherwise we
8786              mess with array offsets in the wrong way.  This is possible
8787              because pointer arithmetic is restricted to retain within an
8788              object and overflow on pointer differences is undefined as of
8789              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
8790           else if (bitpos0 == bitpos1
8791                    && ((code == EQ_EXPR || code == NE_EXPR)
8792                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
8793             {
8794               /* By converting to signed size type we cover middle-end pointer
8795                  arithmetic which operates on unsigned pointer types of size
8796                  type size and ARRAY_REF offsets which are properly sign or
8797                  zero extended from their type in case it is narrower than
8798                  size type.  */
8799               if (offset0 == NULL_TREE)
8800                 offset0 = build_int_cst (ssizetype, 0);
8801               else
8802                 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8803               if (offset1 == NULL_TREE)
8804                 offset1 = build_int_cst (ssizetype, 0);
8805               else
8806                 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8807
8808               if (code != EQ_EXPR
8809                   && code != NE_EXPR
8810                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
8811                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
8812                 fold_overflow_warning (("assuming pointer wraparound does not "
8813                                         "occur when comparing P +- C1 with "
8814                                         "P +- C2"),
8815                                        WARN_STRICT_OVERFLOW_COMPARISON);
8816
8817               return fold_build2_loc (loc, code, type, offset0, offset1);
8818             }
8819         }
8820       /* For non-equal bases we can simplify if they are addresses
8821          of local binding decls or constants.  */
8822       else if (indirect_base0 && indirect_base1
8823                /* We know that !operand_equal_p (base0, base1, 0)
8824                   because the if condition was false.  But make
8825                   sure two decls are not the same.  */
8826                && base0 != base1
8827                && TREE_CODE (arg0) == ADDR_EXPR
8828                && TREE_CODE (arg1) == ADDR_EXPR
8829                && (((TREE_CODE (base0) == VAR_DECL
8830                      || TREE_CODE (base0) == PARM_DECL)
8831                     && (targetm.binds_local_p (base0)
8832                         || CONSTANT_CLASS_P (base1)))
8833                    || CONSTANT_CLASS_P (base0))
8834                && (((TREE_CODE (base1) == VAR_DECL
8835                      || TREE_CODE (base1) == PARM_DECL)
8836                     && (targetm.binds_local_p (base1)
8837                         || CONSTANT_CLASS_P (base0)))
8838                    || CONSTANT_CLASS_P (base1)))
8839         {
8840           if (code == EQ_EXPR)
8841             return omit_two_operands_loc (loc, type, boolean_false_node,
8842                                       arg0, arg1);
8843           else if (code == NE_EXPR)
8844             return omit_two_operands_loc (loc, type, boolean_true_node,
8845                                       arg0, arg1);
8846         }
8847       /* For equal offsets we can simplify to a comparison of the
8848          base addresses.  */
8849       else if (bitpos0 == bitpos1
8850                && (indirect_base0
8851                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8852                && (indirect_base1
8853                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8854                && ((offset0 == offset1)
8855                    || (offset0 && offset1
8856                        && operand_equal_p (offset0, offset1, 0))))
8857         {
8858           if (indirect_base0)
8859             base0 = build_fold_addr_expr_loc (loc, base0);
8860           if (indirect_base1)
8861             base1 = build_fold_addr_expr_loc (loc, base1);
8862           return fold_build2_loc (loc, code, type, base0, base1);
8863         }
8864     }
8865
8866   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8867      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
8868      the resulting offset is smaller in absolute value than the
8869      original one.  */
8870   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8871       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8872       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8873           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8874       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8875       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8876           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8877     {
8878       tree const1 = TREE_OPERAND (arg0, 1);
8879       tree const2 = TREE_OPERAND (arg1, 1);
8880       tree variable1 = TREE_OPERAND (arg0, 0);
8881       tree variable2 = TREE_OPERAND (arg1, 0);
8882       tree cst;
8883       const char * const warnmsg = G_("assuming signed overflow does not "
8884                                       "occur when combining constants around "
8885                                       "a comparison");
8886
8887       /* Put the constant on the side where it doesn't overflow and is
8888          of lower absolute value than before.  */
8889       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8890                              ? MINUS_EXPR : PLUS_EXPR,
8891                              const2, const1, 0);
8892       if (!TREE_OVERFLOW (cst)
8893           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8894         {
8895           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8896           return fold_build2_loc (loc, code, type,
8897                               variable1,
8898                               fold_build2_loc (loc,
8899                                            TREE_CODE (arg1), TREE_TYPE (arg1),
8900                                            variable2, cst));
8901         }
8902
8903       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8904                              ? MINUS_EXPR : PLUS_EXPR,
8905                              const1, const2, 0);
8906       if (!TREE_OVERFLOW (cst)
8907           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8908         {
8909           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8910           return fold_build2_loc (loc, code, type,
8911                               fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
8912                                            variable1, cst),
8913                               variable2);
8914         }
8915     }
8916
8917   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8918      signed arithmetic case.  That form is created by the compiler
8919      often enough for folding it to be of value.  One example is in
8920      computing loop trip counts after Operator Strength Reduction.  */
8921   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8922       && TREE_CODE (arg0) == MULT_EXPR
8923       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8924           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8925       && integer_zerop (arg1))
8926     {
8927       tree const1 = TREE_OPERAND (arg0, 1);
8928       tree const2 = arg1;                       /* zero */
8929       tree variable1 = TREE_OPERAND (arg0, 0);
8930       enum tree_code cmp_code = code;
8931
8932       /* Handle unfolded multiplication by zero.  */
8933       if (integer_zerop (const1))
8934         return fold_build2_loc (loc, cmp_code, type, const1, const2);
8935
8936       fold_overflow_warning (("assuming signed overflow does not occur when "
8937                               "eliminating multiplication in comparison "
8938                               "with zero"),
8939                              WARN_STRICT_OVERFLOW_COMPARISON);
8940
8941       /* If const1 is negative we swap the sense of the comparison.  */
8942       if (tree_int_cst_sgn (const1) < 0)
8943         cmp_code = swap_tree_comparison (cmp_code);
8944
8945       return fold_build2_loc (loc, cmp_code, type, variable1, const2);
8946     }
8947
8948   tem = maybe_canonicalize_comparison (loc, code, type, op0, op1);
8949   if (tem)
8950     return tem;
8951
8952   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8953     {
8954       tree targ0 = strip_float_extensions (arg0);
8955       tree targ1 = strip_float_extensions (arg1);
8956       tree newtype = TREE_TYPE (targ0);
8957
8958       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8959         newtype = TREE_TYPE (targ1);
8960
8961       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
8962       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8963         return fold_build2_loc (loc, code, type,
8964                             fold_convert_loc (loc, newtype, targ0),
8965                             fold_convert_loc (loc, newtype, targ1));
8966
8967       /* (-a) CMP (-b) -> b CMP a  */
8968       if (TREE_CODE (arg0) == NEGATE_EXPR
8969           && TREE_CODE (arg1) == NEGATE_EXPR)
8970         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
8971                             TREE_OPERAND (arg0, 0));
8972
8973       if (TREE_CODE (arg1) == REAL_CST)
8974         {
8975           REAL_VALUE_TYPE cst;
8976           cst = TREE_REAL_CST (arg1);
8977
8978           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
8979           if (TREE_CODE (arg0) == NEGATE_EXPR)
8980             return fold_build2_loc (loc, swap_tree_comparison (code), type,
8981                                 TREE_OPERAND (arg0, 0),
8982                                 build_real (TREE_TYPE (arg1),
8983                                             real_value_negate (&cst)));
8984
8985           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
8986           /* a CMP (-0) -> a CMP 0  */
8987           if (REAL_VALUE_MINUS_ZERO (cst))
8988             return fold_build2_loc (loc, code, type, arg0,
8989                                 build_real (TREE_TYPE (arg1), dconst0));
8990
8991           /* x != NaN is always true, other ops are always false.  */
8992           if (REAL_VALUE_ISNAN (cst)
8993               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8994             {
8995               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8996               return omit_one_operand_loc (loc, type, tem, arg0);
8997             }
8998
8999           /* Fold comparisons against infinity.  */
9000           if (REAL_VALUE_ISINF (cst)
9001               && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9002             {
9003               tem = fold_inf_compare (loc, code, type, arg0, arg1);
9004               if (tem != NULL_TREE)
9005                 return tem;
9006             }
9007         }
9008
9009       /* If this is a comparison of a real constant with a PLUS_EXPR
9010          or a MINUS_EXPR of a real constant, we can convert it into a
9011          comparison with a revised real constant as long as no overflow
9012          occurs when unsafe_math_optimizations are enabled.  */
9013       if (flag_unsafe_math_optimizations
9014           && TREE_CODE (arg1) == REAL_CST
9015           && (TREE_CODE (arg0) == PLUS_EXPR
9016               || TREE_CODE (arg0) == MINUS_EXPR)
9017           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9018           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9019                                       ? MINUS_EXPR : PLUS_EXPR,
9020                                       arg1, TREE_OPERAND (arg0, 1)))
9021           && !TREE_OVERFLOW (tem))
9022         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9023
9024       /* Likewise, we can simplify a comparison of a real constant with
9025          a MINUS_EXPR whose first operand is also a real constant, i.e.
9026          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
9027          floating-point types only if -fassociative-math is set.  */
9028       if (flag_associative_math
9029           && TREE_CODE (arg1) == REAL_CST
9030           && TREE_CODE (arg0) == MINUS_EXPR
9031           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9032           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9033                                       arg1))
9034           && !TREE_OVERFLOW (tem))
9035         return fold_build2_loc (loc, swap_tree_comparison (code), type,
9036                             TREE_OPERAND (arg0, 1), tem);
9037
9038       /* Fold comparisons against built-in math functions.  */
9039       if (TREE_CODE (arg1) == REAL_CST
9040           && flag_unsafe_math_optimizations
9041           && ! flag_errno_math)
9042         {
9043           enum built_in_function fcode = builtin_mathfn_code (arg0);
9044
9045           if (fcode != END_BUILTINS)
9046             {
9047               tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
9048               if (tem != NULL_TREE)
9049                 return tem;
9050             }
9051         }
9052     }
9053
9054   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9055       && CONVERT_EXPR_P (arg0))
9056     {
9057       /* If we are widening one operand of an integer comparison,
9058          see if the other operand is similarly being widened.  Perhaps we
9059          can do the comparison in the narrower type.  */
9060       tem = fold_widened_comparison (loc, code, type, arg0, arg1);
9061       if (tem)
9062         return tem;
9063
9064       /* Or if we are changing signedness.  */
9065       tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
9066       if (tem)
9067         return tem;
9068     }
9069
9070   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9071      constant, we can simplify it.  */
9072   if (TREE_CODE (arg1) == INTEGER_CST
9073       && (TREE_CODE (arg0) == MIN_EXPR
9074           || TREE_CODE (arg0) == MAX_EXPR)
9075       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9076     {
9077       tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9078       if (tem)
9079         return tem;
9080     }
9081
9082   /* Simplify comparison of something with itself.  (For IEEE
9083      floating-point, we can only do some of these simplifications.)  */
9084   if (operand_equal_p (arg0, arg1, 0))
9085     {
9086       switch (code)
9087         {
9088         case EQ_EXPR:
9089           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9090               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9091             return constant_boolean_node (1, type);
9092           break;
9093
9094         case GE_EXPR:
9095         case LE_EXPR:
9096           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9097               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9098             return constant_boolean_node (1, type);
9099           return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9100
9101         case NE_EXPR:
9102           /* For NE, we can only do this simplification if integer
9103              or we don't honor IEEE floating point NaNs.  */
9104           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9105               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9106             break;
9107           /* ... fall through ...  */
9108         case GT_EXPR:
9109         case LT_EXPR:
9110           return constant_boolean_node (0, type);
9111         default:
9112           gcc_unreachable ();
9113         }
9114     }
9115
9116   /* If we are comparing an expression that just has comparisons
9117      of two integer values, arithmetic expressions of those comparisons,
9118      and constants, we can simplify it.  There are only three cases
9119      to check: the two values can either be equal, the first can be
9120      greater, or the second can be greater.  Fold the expression for
9121      those three values.  Since each value must be 0 or 1, we have
9122      eight possibilities, each of which corresponds to the constant 0
9123      or 1 or one of the six possible comparisons.
9124
9125      This handles common cases like (a > b) == 0 but also handles
9126      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9127      occur in macroized code.  */
9128
9129   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9130     {
9131       tree cval1 = 0, cval2 = 0;
9132       int save_p = 0;
9133
9134       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9135           /* Don't handle degenerate cases here; they should already
9136              have been handled anyway.  */
9137           && cval1 != 0 && cval2 != 0
9138           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9139           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9140           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9141           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9142           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9143           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9144                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9145         {
9146           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9147           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9148
9149           /* We can't just pass T to eval_subst in case cval1 or cval2
9150              was the same as ARG1.  */
9151
9152           tree high_result
9153                 = fold_build2_loc (loc, code, type,
9154                                eval_subst (loc, arg0, cval1, maxval,
9155                                            cval2, minval),
9156                                arg1);
9157           tree equal_result
9158                 = fold_build2_loc (loc, code, type,
9159                                eval_subst (loc, arg0, cval1, maxval,
9160                                            cval2, maxval),
9161                                arg1);
9162           tree low_result
9163                 = fold_build2_loc (loc, code, type,
9164                                eval_subst (loc, arg0, cval1, minval,
9165                                            cval2, maxval),
9166                                arg1);
9167
9168           /* All three of these results should be 0 or 1.  Confirm they are.
9169              Then use those values to select the proper code to use.  */
9170
9171           if (TREE_CODE (high_result) == INTEGER_CST
9172               && TREE_CODE (equal_result) == INTEGER_CST
9173               && TREE_CODE (low_result) == INTEGER_CST)
9174             {
9175               /* Make a 3-bit mask with the high-order bit being the
9176                  value for `>', the next for '=', and the low for '<'.  */
9177               switch ((integer_onep (high_result) * 4)
9178                       + (integer_onep (equal_result) * 2)
9179                       + integer_onep (low_result))
9180                 {
9181                 case 0:
9182                   /* Always false.  */
9183                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9184                 case 1:
9185                   code = LT_EXPR;
9186                   break;
9187                 case 2:
9188                   code = EQ_EXPR;
9189                   break;
9190                 case 3:
9191                   code = LE_EXPR;
9192                   break;
9193                 case 4:
9194                   code = GT_EXPR;
9195                   break;
9196                 case 5:
9197                   code = NE_EXPR;
9198                   break;
9199                 case 6:
9200                   code = GE_EXPR;
9201                   break;
9202                 case 7:
9203                   /* Always true.  */
9204                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9205                 }
9206
9207               if (save_p)
9208                 {
9209                   tem = save_expr (build2 (code, type, cval1, cval2));
9210                   SET_EXPR_LOCATION (tem, loc);
9211                   return tem;
9212                 }
9213               return fold_build2_loc (loc, code, type, cval1, cval2);
9214             }
9215         }
9216     }
9217
9218   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9219      into a single range test.  */
9220   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9221        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9222       && TREE_CODE (arg1) == INTEGER_CST
9223       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9224       && !integer_zerop (TREE_OPERAND (arg0, 1))
9225       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9226       && !TREE_OVERFLOW (arg1))
9227     {
9228       tem = fold_div_compare (loc, code, type, arg0, arg1);
9229       if (tem != NULL_TREE)
9230         return tem;
9231     }
9232
9233   /* Fold ~X op ~Y as Y op X.  */
9234   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9235       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9236     {
9237       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9238       return fold_build2_loc (loc, code, type,
9239                           fold_convert_loc (loc, cmp_type,
9240                                             TREE_OPERAND (arg1, 0)),
9241                           TREE_OPERAND (arg0, 0));
9242     }
9243
9244   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9245   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9246       && TREE_CODE (arg1) == INTEGER_CST)
9247     {
9248       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9249       return fold_build2_loc (loc, swap_tree_comparison (code), type,
9250                           TREE_OPERAND (arg0, 0),
9251                           fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9252                                        fold_convert_loc (loc, cmp_type, arg1)));
9253     }
9254
9255   return NULL_TREE;
9256 }
9257
9258
9259 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9260    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9261    argument EXPR represents the expression "z" of type TYPE.  */
9262
9263 static tree
9264 fold_mult_zconjz (location_t loc, tree type, tree expr)
9265 {
9266   tree itype = TREE_TYPE (type);
9267   tree rpart, ipart, tem;
9268
9269   if (TREE_CODE (expr) == COMPLEX_EXPR)
9270     {
9271       rpart = TREE_OPERAND (expr, 0);
9272       ipart = TREE_OPERAND (expr, 1);
9273     }
9274   else if (TREE_CODE (expr) == COMPLEX_CST)
9275     {
9276       rpart = TREE_REALPART (expr);
9277       ipart = TREE_IMAGPART (expr);
9278     }
9279   else
9280     {
9281       expr = save_expr (expr);
9282       rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9283       ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9284     }
9285
9286   rpart = save_expr (rpart);
9287   ipart = save_expr (ipart);
9288   tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9289                      fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9290                      fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9291   return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9292                       fold_convert_loc (loc, itype, integer_zero_node));
9293 }
9294
9295
9296 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9297    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9298    guarantees that P and N have the same least significant log2(M) bits.
9299    N is not otherwise constrained.  In particular, N is not normalized to
9300    0 <= N < M as is common.  In general, the precise value of P is unknown.
9301    M is chosen as large as possible such that constant N can be determined.
9302
9303    Returns M and sets *RESIDUE to N.
9304
9305    If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9306    account.  This is not always possible due to PR 35705.
9307  */
9308
9309 static unsigned HOST_WIDE_INT
9310 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9311                                  bool allow_func_align)
9312 {
9313   enum tree_code code;
9314
9315   *residue = 0;
9316
9317   code = TREE_CODE (expr);
9318   if (code == ADDR_EXPR)
9319     {
9320       expr = TREE_OPERAND (expr, 0);
9321       if (handled_component_p (expr))
9322         {
9323           HOST_WIDE_INT bitsize, bitpos;
9324           tree offset;
9325           enum machine_mode mode;
9326           int unsignedp, volatilep;
9327
9328           expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9329                                       &mode, &unsignedp, &volatilep, false);
9330           *residue = bitpos / BITS_PER_UNIT;
9331           if (offset)
9332             {
9333               if (TREE_CODE (offset) == INTEGER_CST)
9334                 *residue += TREE_INT_CST_LOW (offset);
9335               else
9336                 /* We don't handle more complicated offset expressions.  */
9337                 return 1;
9338             }
9339         }
9340
9341       if (DECL_P (expr)
9342           && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL))
9343         return DECL_ALIGN_UNIT (expr);
9344     }
9345   else if (code == POINTER_PLUS_EXPR)
9346     {
9347       tree op0, op1;
9348       unsigned HOST_WIDE_INT modulus;
9349       enum tree_code inner_code;
9350
9351       op0 = TREE_OPERAND (expr, 0);
9352       STRIP_NOPS (op0);
9353       modulus = get_pointer_modulus_and_residue (op0, residue,
9354                                                  allow_func_align);
9355
9356       op1 = TREE_OPERAND (expr, 1);
9357       STRIP_NOPS (op1);
9358       inner_code = TREE_CODE (op1);
9359       if (inner_code == INTEGER_CST)
9360         {
9361           *residue += TREE_INT_CST_LOW (op1);
9362           return modulus;
9363         }
9364       else if (inner_code == MULT_EXPR)
9365         {
9366           op1 = TREE_OPERAND (op1, 1);
9367           if (TREE_CODE (op1) == INTEGER_CST)
9368             {
9369               unsigned HOST_WIDE_INT align;
9370
9371               /* Compute the greatest power-of-2 divisor of op1.  */
9372               align = TREE_INT_CST_LOW (op1);
9373               align &= -align;
9374
9375               /* If align is non-zero and less than *modulus, replace
9376                  *modulus with align., If align is 0, then either op1 is 0
9377                  or the greatest power-of-2 divisor of op1 doesn't fit in an
9378                  unsigned HOST_WIDE_INT.  In either case, no additional
9379                  constraint is imposed.  */
9380               if (align)
9381                 modulus = MIN (modulus, align);
9382
9383               return modulus;
9384             }
9385         }
9386     }
9387
9388     /* If we get here, we were unable to determine anything useful about the
9389        expression.  */
9390     return 1;
9391 }
9392
9393
9394 /* Fold a binary expression of code CODE and type TYPE with operands
9395    OP0 and OP1.  LOC is the location of the resulting expression.
9396    Return the folded expression if folding is successful.  Otherwise,
9397    return NULL_TREE.  */
9398
9399 tree
9400 fold_binary_loc (location_t loc,
9401              enum tree_code code, tree type, tree op0, tree op1)
9402 {
9403   enum tree_code_class kind = TREE_CODE_CLASS (code);
9404   tree arg0, arg1, tem;
9405   tree t1 = NULL_TREE;
9406   bool strict_overflow_p;
9407
9408   gcc_assert (IS_EXPR_CODE_CLASS (kind)
9409               && TREE_CODE_LENGTH (code) == 2
9410               && op0 != NULL_TREE
9411               && op1 != NULL_TREE);
9412
9413   arg0 = op0;
9414   arg1 = op1;
9415
9416   /* Strip any conversions that don't change the mode.  This is
9417      safe for every expression, except for a comparison expression
9418      because its signedness is derived from its operands.  So, in
9419      the latter case, only strip conversions that don't change the
9420      signedness.  MIN_EXPR/MAX_EXPR also need signedness of arguments
9421      preserved.
9422
9423      Note that this is done as an internal manipulation within the
9424      constant folder, in order to find the simplest representation
9425      of the arguments so that their form can be studied.  In any
9426      cases, the appropriate type conversions should be put back in
9427      the tree that will get out of the constant folder.  */
9428
9429   if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9430     {
9431       STRIP_SIGN_NOPS (arg0);
9432       STRIP_SIGN_NOPS (arg1);
9433     }
9434   else
9435     {
9436       STRIP_NOPS (arg0);
9437       STRIP_NOPS (arg1);
9438     }
9439
9440   /* Note that TREE_CONSTANT isn't enough: static var addresses are
9441      constant but we can't do arithmetic on them.  */
9442   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9443       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9444       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9445       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9446       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9447       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9448     {
9449       if (kind == tcc_binary)
9450         {
9451           /* Make sure type and arg0 have the same saturating flag.  */
9452           gcc_assert (TYPE_SATURATING (type)
9453                       == TYPE_SATURATING (TREE_TYPE (arg0)));
9454           tem = const_binop (code, arg0, arg1);
9455         }
9456       else if (kind == tcc_comparison)
9457         tem = fold_relational_const (code, type, arg0, arg1);
9458       else
9459         tem = NULL_TREE;
9460
9461       if (tem != NULL_TREE)
9462         {
9463           if (TREE_TYPE (tem) != type)
9464             tem = fold_convert_loc (loc, type, tem);
9465           return tem;
9466         }
9467     }
9468
9469   /* If this is a commutative operation, and ARG0 is a constant, move it
9470      to ARG1 to reduce the number of tests below.  */
9471   if (commutative_tree_code (code)
9472       && tree_swap_operands_p (arg0, arg1, true))
9473     return fold_build2_loc (loc, code, type, op1, op0);
9474
9475   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9476
9477      First check for cases where an arithmetic operation is applied to a
9478      compound, conditional, or comparison operation.  Push the arithmetic
9479      operation inside the compound or conditional to see if any folding
9480      can then be done.  Convert comparison to conditional for this purpose.
9481      The also optimizes non-constant cases that used to be done in
9482      expand_expr.
9483
9484      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9485      one of the operands is a comparison and the other is a comparison, a
9486      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
9487      code below would make the expression more complex.  Change it to a
9488      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
9489      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
9490
9491   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9492        || code == EQ_EXPR || code == NE_EXPR)
9493       && ((truth_value_p (TREE_CODE (arg0))
9494            && (truth_value_p (TREE_CODE (arg1))
9495                || (TREE_CODE (arg1) == BIT_AND_EXPR
9496                    && integer_onep (TREE_OPERAND (arg1, 1)))))
9497           || (truth_value_p (TREE_CODE (arg1))
9498               && (truth_value_p (TREE_CODE (arg0))
9499                   || (TREE_CODE (arg0) == BIT_AND_EXPR
9500                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
9501     {
9502       tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9503                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9504                          : TRUTH_XOR_EXPR,
9505                          boolean_type_node,
9506                          fold_convert_loc (loc, boolean_type_node, arg0),
9507                          fold_convert_loc (loc, boolean_type_node, arg1));
9508
9509       if (code == EQ_EXPR)
9510         tem = invert_truthvalue_loc (loc, tem);
9511
9512       return fold_convert_loc (loc, type, tem);
9513     }
9514
9515   if (TREE_CODE_CLASS (code) == tcc_binary
9516       || TREE_CODE_CLASS (code) == tcc_comparison)
9517     {
9518       if (TREE_CODE (arg0) == COMPOUND_EXPR)
9519         {
9520           tem = fold_build2_loc (loc, code, type,
9521                              fold_convert_loc (loc, TREE_TYPE (op0),
9522                                                TREE_OPERAND (arg0, 1)), op1);
9523           tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), tem);
9524           goto fold_binary_exit;
9525         }
9526       if (TREE_CODE (arg1) == COMPOUND_EXPR
9527           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9528         {
9529           tem = fold_build2_loc (loc, code, type, op0,
9530                              fold_convert_loc (loc, TREE_TYPE (op1),
9531                                                TREE_OPERAND (arg1, 1)));
9532           tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0), tem);
9533           goto fold_binary_exit;
9534         }
9535
9536       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9537         {
9538           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9539                                                      arg0, arg1,
9540                                                      /*cond_first_p=*/1);
9541           if (tem != NULL_TREE)
9542             return tem;
9543         }
9544
9545       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9546         {
9547           tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9548                                                      arg1, arg0,
9549                                                      /*cond_first_p=*/0);
9550           if (tem != NULL_TREE)
9551             return tem;
9552         }
9553     }
9554
9555   switch (code)
9556     {
9557     case MEM_REF:
9558       /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2].  */
9559       if (TREE_CODE (arg0) == ADDR_EXPR
9560           && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9561         {
9562           tree iref = TREE_OPERAND (arg0, 0);
9563           return fold_build2 (MEM_REF, type,
9564                               TREE_OPERAND (iref, 0),
9565                               int_const_binop (PLUS_EXPR, arg1,
9566                                                TREE_OPERAND (iref, 1), 0));
9567         }
9568
9569       /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2].  */
9570       if (TREE_CODE (arg0) == ADDR_EXPR
9571           && handled_component_p (TREE_OPERAND (arg0, 0)))
9572         {
9573           tree base;
9574           HOST_WIDE_INT coffset;
9575           base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9576                                                 &coffset);
9577           if (!base)
9578             return NULL_TREE;
9579           return fold_build2 (MEM_REF, type,
9580                               build_fold_addr_expr (base),
9581                               int_const_binop (PLUS_EXPR, arg1,
9582                                                size_int (coffset), 0));
9583         }
9584
9585       return NULL_TREE;
9586
9587     case POINTER_PLUS_EXPR:
9588       /* 0 +p index -> (type)index */
9589       if (integer_zerop (arg0))
9590         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9591
9592       /* PTR +p 0 -> PTR */
9593       if (integer_zerop (arg1))
9594         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9595
9596       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
9597       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9598            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9599         return fold_convert_loc (loc, type,
9600                                  fold_build2_loc (loc, PLUS_EXPR, sizetype,
9601                                               fold_convert_loc (loc, sizetype,
9602                                                                 arg1),
9603                                               fold_convert_loc (loc, sizetype,
9604                                                                 arg0)));
9605
9606       /* index +p PTR -> PTR +p index */
9607       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9608           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9609         return fold_build2_loc (loc, POINTER_PLUS_EXPR, type,
9610                             fold_convert_loc (loc, type, arg1),
9611                             fold_convert_loc (loc, sizetype, arg0));
9612
9613       /* (PTR +p B) +p A -> PTR +p (B + A) */
9614       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9615         {
9616           tree inner;
9617           tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
9618           tree arg00 = TREE_OPERAND (arg0, 0);
9619           inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
9620                                arg01, fold_convert_loc (loc, sizetype, arg1));
9621           return fold_convert_loc (loc, type,
9622                                    fold_build2_loc (loc, POINTER_PLUS_EXPR,
9623                                                 TREE_TYPE (arg00),
9624                                                 arg00, inner));
9625         }
9626
9627       /* PTR_CST +p CST -> CST1 */
9628       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9629         return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
9630                             fold_convert_loc (loc, type, arg1));
9631
9632      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9633         of the array.  Loop optimizer sometimes produce this type of
9634         expressions.  */
9635       if (TREE_CODE (arg0) == ADDR_EXPR)
9636         {
9637           tem = try_move_mult_to_index (loc, arg0,
9638                                         fold_convert_loc (loc, sizetype, arg1));
9639           if (tem)
9640             return fold_convert_loc (loc, type, tem);
9641         }
9642
9643       return NULL_TREE;
9644
9645     case PLUS_EXPR:
9646       /* A + (-B) -> A - B */
9647       if (TREE_CODE (arg1) == NEGATE_EXPR)
9648         return fold_build2_loc (loc, MINUS_EXPR, type,
9649                             fold_convert_loc (loc, type, arg0),
9650                             fold_convert_loc (loc, type,
9651                                               TREE_OPERAND (arg1, 0)));
9652       /* (-A) + B -> B - A */
9653       if (TREE_CODE (arg0) == NEGATE_EXPR
9654           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9655         return fold_build2_loc (loc, MINUS_EXPR, type,
9656                             fold_convert_loc (loc, type, arg1),
9657                             fold_convert_loc (loc, type,
9658                                               TREE_OPERAND (arg0, 0)));
9659
9660       if (INTEGRAL_TYPE_P (type))
9661         {
9662           /* Convert ~A + 1 to -A.  */
9663           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9664               && integer_onep (arg1))
9665             return fold_build1_loc (loc, NEGATE_EXPR, type,
9666                                 fold_convert_loc (loc, type,
9667                                                   TREE_OPERAND (arg0, 0)));
9668
9669           /* ~X + X is -1.  */
9670           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9671               && !TYPE_OVERFLOW_TRAPS (type))
9672             {
9673               tree tem = TREE_OPERAND (arg0, 0);
9674
9675               STRIP_NOPS (tem);
9676               if (operand_equal_p (tem, arg1, 0))
9677                 {
9678                   t1 = build_int_cst_type (type, -1);
9679                   return omit_one_operand_loc (loc, type, t1, arg1);
9680                 }
9681             }
9682
9683           /* X + ~X is -1.  */
9684           if (TREE_CODE (arg1) == BIT_NOT_EXPR
9685               && !TYPE_OVERFLOW_TRAPS (type))
9686             {
9687               tree tem = TREE_OPERAND (arg1, 0);
9688
9689               STRIP_NOPS (tem);
9690               if (operand_equal_p (arg0, tem, 0))
9691                 {
9692                   t1 = build_int_cst_type (type, -1);
9693                   return omit_one_operand_loc (loc, type, t1, arg0);
9694                 }
9695             }
9696
9697           /* X + (X / CST) * -CST is X % CST.  */
9698           if (TREE_CODE (arg1) == MULT_EXPR
9699               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9700               && operand_equal_p (arg0,
9701                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9702             {
9703               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9704               tree cst1 = TREE_OPERAND (arg1, 1);
9705               tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9706                                       cst1, cst0);
9707               if (sum && integer_zerop (sum))
9708                 return fold_convert_loc (loc, type,
9709                                          fold_build2_loc (loc, TRUNC_MOD_EXPR,
9710                                                       TREE_TYPE (arg0), arg0,
9711                                                       cst0));
9712             }
9713         }
9714
9715       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9716          same or one.  Make sure type is not saturating.
9717          fold_plusminus_mult_expr will re-associate.  */
9718       if ((TREE_CODE (arg0) == MULT_EXPR
9719            || TREE_CODE (arg1) == MULT_EXPR)
9720           && !TYPE_SATURATING (type)
9721           && (!FLOAT_TYPE_P (type) || flag_associative_math))
9722         {
9723           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9724           if (tem)
9725             return tem;
9726         }
9727
9728       if (! FLOAT_TYPE_P (type))
9729         {
9730           if (integer_zerop (arg1))
9731             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9732
9733           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9734              with a constant, and the two constants have no bits in common,
9735              we should treat this as a BIT_IOR_EXPR since this may produce more
9736              simplifications.  */
9737           if (TREE_CODE (arg0) == BIT_AND_EXPR
9738               && TREE_CODE (arg1) == BIT_AND_EXPR
9739               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9740               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9741               && integer_zerop (const_binop (BIT_AND_EXPR,
9742                                              TREE_OPERAND (arg0, 1),
9743                                              TREE_OPERAND (arg1, 1))))
9744             {
9745               code = BIT_IOR_EXPR;
9746               goto bit_ior;
9747             }
9748
9749           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9750              (plus (plus (mult) (mult)) (foo)) so that we can
9751              take advantage of the factoring cases below.  */
9752           if (((TREE_CODE (arg0) == PLUS_EXPR
9753                 || TREE_CODE (arg0) == MINUS_EXPR)
9754                && TREE_CODE (arg1) == MULT_EXPR)
9755               || ((TREE_CODE (arg1) == PLUS_EXPR
9756                    || TREE_CODE (arg1) == MINUS_EXPR)
9757                   && TREE_CODE (arg0) == MULT_EXPR))
9758             {
9759               tree parg0, parg1, parg, marg;
9760               enum tree_code pcode;
9761
9762               if (TREE_CODE (arg1) == MULT_EXPR)
9763                 parg = arg0, marg = arg1;
9764               else
9765                 parg = arg1, marg = arg0;
9766               pcode = TREE_CODE (parg);
9767               parg0 = TREE_OPERAND (parg, 0);
9768               parg1 = TREE_OPERAND (parg, 1);
9769               STRIP_NOPS (parg0);
9770               STRIP_NOPS (parg1);
9771
9772               if (TREE_CODE (parg0) == MULT_EXPR
9773                   && TREE_CODE (parg1) != MULT_EXPR)
9774                 return fold_build2_loc (loc, pcode, type,
9775                                     fold_build2_loc (loc, PLUS_EXPR, type,
9776                                                  fold_convert_loc (loc, type,
9777                                                                    parg0),
9778                                                  fold_convert_loc (loc, type,
9779                                                                    marg)),
9780                                     fold_convert_loc (loc, type, parg1));
9781               if (TREE_CODE (parg0) != MULT_EXPR
9782                   && TREE_CODE (parg1) == MULT_EXPR)
9783                 return
9784                   fold_build2_loc (loc, PLUS_EXPR, type,
9785                                fold_convert_loc (loc, type, parg0),
9786                                fold_build2_loc (loc, pcode, type,
9787                                             fold_convert_loc (loc, type, marg),
9788                                             fold_convert_loc (loc, type,
9789                                                               parg1)));
9790             }
9791         }
9792       else
9793         {
9794           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
9795           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9796             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9797
9798           /* Likewise if the operands are reversed.  */
9799           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9800             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9801
9802           /* Convert X + -C into X - C.  */
9803           if (TREE_CODE (arg1) == REAL_CST
9804               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9805             {
9806               tem = fold_negate_const (arg1, type);
9807               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9808                 return fold_build2_loc (loc, MINUS_EXPR, type,
9809                                     fold_convert_loc (loc, type, arg0),
9810                                     fold_convert_loc (loc, type, tem));
9811             }
9812
9813           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9814              to __complex__ ( x, y ).  This is not the same for SNaNs or
9815              if signed zeros are involved.  */
9816           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9817               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9818               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9819             {
9820               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9821               tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9822               tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9823               bool arg0rz = false, arg0iz = false;
9824               if ((arg0r && (arg0rz = real_zerop (arg0r)))
9825                   || (arg0i && (arg0iz = real_zerop (arg0i))))
9826                 {
9827                   tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9828                   tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9829                   if (arg0rz && arg1i && real_zerop (arg1i))
9830                     {
9831                       tree rp = arg1r ? arg1r
9832                                   : build1 (REALPART_EXPR, rtype, arg1);
9833                       tree ip = arg0i ? arg0i
9834                                   : build1 (IMAGPART_EXPR, rtype, arg0);
9835                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9836                     }
9837                   else if (arg0iz && arg1r && real_zerop (arg1r))
9838                     {
9839                       tree rp = arg0r ? arg0r
9840                                   : build1 (REALPART_EXPR, rtype, arg0);
9841                       tree ip = arg1i ? arg1i
9842                                   : build1 (IMAGPART_EXPR, rtype, arg1);
9843                       return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9844                     }
9845                 }
9846             }
9847
9848           if (flag_unsafe_math_optimizations
9849               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9850               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9851               && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9852             return tem;
9853
9854           /* Convert x+x into x*2.0.  */
9855           if (operand_equal_p (arg0, arg1, 0)
9856               && SCALAR_FLOAT_TYPE_P (type))
9857             return fold_build2_loc (loc, MULT_EXPR, type, arg0,
9858                                 build_real (type, dconst2));
9859
9860           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9861              We associate floats only if the user has specified
9862              -fassociative-math.  */
9863           if (flag_associative_math
9864               && TREE_CODE (arg1) == PLUS_EXPR
9865               && TREE_CODE (arg0) != MULT_EXPR)
9866             {
9867               tree tree10 = TREE_OPERAND (arg1, 0);
9868               tree tree11 = TREE_OPERAND (arg1, 1);
9869               if (TREE_CODE (tree11) == MULT_EXPR
9870                   && TREE_CODE (tree10) == MULT_EXPR)
9871                 {
9872                   tree tree0;
9873                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9874                   return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9875                 }
9876             }
9877           /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9878              We associate floats only if the user has specified
9879              -fassociative-math.  */
9880           if (flag_associative_math
9881               && TREE_CODE (arg0) == PLUS_EXPR
9882               && TREE_CODE (arg1) != MULT_EXPR)
9883             {
9884               tree tree00 = TREE_OPERAND (arg0, 0);
9885               tree tree01 = TREE_OPERAND (arg0, 1);
9886               if (TREE_CODE (tree01) == MULT_EXPR
9887                   && TREE_CODE (tree00) == MULT_EXPR)
9888                 {
9889                   tree tree0;
9890                   tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9891                   return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9892                 }
9893             }
9894         }
9895
9896      bit_rotate:
9897       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9898          is a rotate of A by C1 bits.  */
9899       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9900          is a rotate of A by B bits.  */
9901       {
9902         enum tree_code code0, code1;
9903         tree rtype;
9904         code0 = TREE_CODE (arg0);
9905         code1 = TREE_CODE (arg1);
9906         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9907              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9908             && operand_equal_p (TREE_OPERAND (arg0, 0),
9909                                 TREE_OPERAND (arg1, 0), 0)
9910             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9911                 TYPE_UNSIGNED (rtype))
9912             /* Only create rotates in complete modes.  Other cases are not
9913                expanded properly.  */
9914             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
9915           {
9916             tree tree01, tree11;
9917             enum tree_code code01, code11;
9918
9919             tree01 = TREE_OPERAND (arg0, 1);
9920             tree11 = TREE_OPERAND (arg1, 1);
9921             STRIP_NOPS (tree01);
9922             STRIP_NOPS (tree11);
9923             code01 = TREE_CODE (tree01);
9924             code11 = TREE_CODE (tree11);
9925             if (code01 == INTEGER_CST
9926                 && code11 == INTEGER_CST
9927                 && TREE_INT_CST_HIGH (tree01) == 0
9928                 && TREE_INT_CST_HIGH (tree11) == 0
9929                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9930                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9931               {
9932                 tem = build2 (LROTATE_EXPR,
9933                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
9934                               TREE_OPERAND (arg0, 0),
9935                               code0 == LSHIFT_EXPR
9936                               ? tree01 : tree11);
9937                 SET_EXPR_LOCATION (tem, loc);
9938                 return fold_convert_loc (loc, type, tem);
9939               }
9940             else if (code11 == MINUS_EXPR)
9941               {
9942                 tree tree110, tree111;
9943                 tree110 = TREE_OPERAND (tree11, 0);
9944                 tree111 = TREE_OPERAND (tree11, 1);
9945                 STRIP_NOPS (tree110);
9946                 STRIP_NOPS (tree111);
9947                 if (TREE_CODE (tree110) == INTEGER_CST
9948                     && 0 == compare_tree_int (tree110,
9949                                               TYPE_PRECISION
9950                                               (TREE_TYPE (TREE_OPERAND
9951                                                           (arg0, 0))))
9952                     && operand_equal_p (tree01, tree111, 0))
9953                   return
9954                     fold_convert_loc (loc, type,
9955                                       build2 ((code0 == LSHIFT_EXPR
9956                                                ? LROTATE_EXPR
9957                                                : RROTATE_EXPR),
9958                                               TREE_TYPE (TREE_OPERAND (arg0, 0)),
9959                                               TREE_OPERAND (arg0, 0), tree01));
9960               }
9961             else if (code01 == MINUS_EXPR)
9962               {
9963                 tree tree010, tree011;
9964                 tree010 = TREE_OPERAND (tree01, 0);
9965                 tree011 = TREE_OPERAND (tree01, 1);
9966                 STRIP_NOPS (tree010);
9967                 STRIP_NOPS (tree011);
9968                 if (TREE_CODE (tree010) == INTEGER_CST
9969                     && 0 == compare_tree_int (tree010,
9970                                               TYPE_PRECISION
9971                                               (TREE_TYPE (TREE_OPERAND
9972                                                           (arg0, 0))))
9973                     && operand_equal_p (tree11, tree011, 0))
9974                     return fold_convert_loc
9975                       (loc, type,
9976                        build2 ((code0 != LSHIFT_EXPR
9977                                 ? LROTATE_EXPR
9978                                 : RROTATE_EXPR),
9979                                TREE_TYPE (TREE_OPERAND (arg0, 0)),
9980                                TREE_OPERAND (arg0, 0), tree11));
9981               }
9982           }
9983       }
9984
9985     associate:
9986       /* In most languages, can't associate operations on floats through
9987          parentheses.  Rather than remember where the parentheses were, we
9988          don't associate floats at all, unless the user has specified
9989          -fassociative-math.
9990          And, we need to make sure type is not saturating.  */
9991
9992       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9993           && !TYPE_SATURATING (type))
9994         {
9995           tree var0, con0, lit0, minus_lit0;
9996           tree var1, con1, lit1, minus_lit1;
9997           bool ok = true;
9998
9999           /* Split both trees into variables, constants, and literals.  Then
10000              associate each group together, the constants with literals,
10001              then the result with variables.  This increases the chances of
10002              literals being recombined later and of generating relocatable
10003              expressions for the sum of a constant and literal.  */
10004           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10005           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10006                              code == MINUS_EXPR);
10007
10008           /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
10009           if (code == MINUS_EXPR)
10010             code = PLUS_EXPR;
10011
10012           /* With undefined overflow we can only associate constants with one
10013              variable, and constants whose association doesn't overflow.  */
10014           if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10015               || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10016             {
10017               if (var0 && var1)
10018                 {
10019                   tree tmp0 = var0;
10020                   tree tmp1 = var1;
10021
10022                   if (TREE_CODE (tmp0) == NEGATE_EXPR)
10023                     tmp0 = TREE_OPERAND (tmp0, 0);
10024                   if (TREE_CODE (tmp1) == NEGATE_EXPR)
10025                     tmp1 = TREE_OPERAND (tmp1, 0);
10026                   /* The only case we can still associate with two variables
10027                      is if they are the same, modulo negation.  */
10028                   if (!operand_equal_p (tmp0, tmp1, 0))
10029                     ok = false;
10030                 }
10031
10032               if (ok && lit0 && lit1)
10033                 {
10034                   tree tmp0 = fold_convert (type, lit0);
10035                   tree tmp1 = fold_convert (type, lit1);
10036
10037                   if (!TREE_OVERFLOW (tmp0) && !TREE_OVERFLOW (tmp1)
10038                       && TREE_OVERFLOW (fold_build2 (code, type, tmp0, tmp1)))
10039                     ok = false;
10040                 }
10041             }
10042
10043           /* Only do something if we found more than two objects.  Otherwise,
10044              nothing has changed and we risk infinite recursion.  */
10045           if (ok
10046               && (2 < ((var0 != 0) + (var1 != 0)
10047                        + (con0 != 0) + (con1 != 0)
10048                        + (lit0 != 0) + (lit1 != 0)
10049                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
10050             {
10051               var0 = associate_trees (loc, var0, var1, code, type);
10052               con0 = associate_trees (loc, con0, con1, code, type);
10053               lit0 = associate_trees (loc, lit0, lit1, code, type);
10054               minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type);
10055
10056               /* Preserve the MINUS_EXPR if the negative part of the literal is
10057                  greater than the positive part.  Otherwise, the multiplicative
10058                  folding code (i.e extract_muldiv) may be fooled in case
10059                  unsigned constants are subtracted, like in the following
10060                  example: ((X*2 + 4) - 8U)/2.  */
10061               if (minus_lit0 && lit0)
10062                 {
10063                   if (TREE_CODE (lit0) == INTEGER_CST
10064                       && TREE_CODE (minus_lit0) == INTEGER_CST
10065                       && tree_int_cst_lt (lit0, minus_lit0))
10066                     {
10067                       minus_lit0 = associate_trees (loc, minus_lit0, lit0,
10068                                                     MINUS_EXPR, type);
10069                       lit0 = 0;
10070                     }
10071                   else
10072                     {
10073                       lit0 = associate_trees (loc, lit0, minus_lit0,
10074                                               MINUS_EXPR, type);
10075                       minus_lit0 = 0;
10076                     }
10077                 }
10078               if (minus_lit0)
10079                 {
10080                   if (con0 == 0)
10081                     return
10082                       fold_convert_loc (loc, type,
10083                                         associate_trees (loc, var0, minus_lit0,
10084                                                          MINUS_EXPR, type));
10085                   else
10086                     {
10087                       con0 = associate_trees (loc, con0, minus_lit0,
10088                                               MINUS_EXPR, type);
10089                       return
10090                         fold_convert_loc (loc, type,
10091                                           associate_trees (loc, var0, con0,
10092                                                            PLUS_EXPR, type));
10093                     }
10094                 }
10095
10096               con0 = associate_trees (loc, con0, lit0, code, type);
10097               return
10098                 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10099                                                               code, type));
10100             }
10101         }
10102
10103       return NULL_TREE;
10104
10105     case MINUS_EXPR:
10106       /* Pointer simplifications for subtraction, simple reassociations. */
10107       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10108         {
10109           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10110           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10111               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10112             {
10113               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10114               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10115               tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10116               tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10117               return fold_build2_loc (loc, PLUS_EXPR, type,
10118                                   fold_build2_loc (loc, MINUS_EXPR, type,
10119                                                arg00, arg10),
10120                                   fold_build2_loc (loc, MINUS_EXPR, type,
10121                                                arg01, arg11));
10122             }
10123           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10124           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10125             {
10126               tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10127               tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10128               tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10129                                       fold_convert_loc (loc, type, arg1));
10130               if (tmp)
10131                 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10132             }
10133         }
10134       /* A - (-B) -> A + B */
10135       if (TREE_CODE (arg1) == NEGATE_EXPR)
10136         return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10137                             fold_convert_loc (loc, type,
10138                                               TREE_OPERAND (arg1, 0)));
10139       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10140       if (TREE_CODE (arg0) == NEGATE_EXPR
10141           && (FLOAT_TYPE_P (type)
10142               || INTEGRAL_TYPE_P (type))
10143           && negate_expr_p (arg1)
10144           && reorder_operands_p (arg0, arg1))
10145         return fold_build2_loc (loc, MINUS_EXPR, type,
10146                             fold_convert_loc (loc, type,
10147                                               negate_expr (arg1)),
10148                             fold_convert_loc (loc, type,
10149                                               TREE_OPERAND (arg0, 0)));
10150       /* Convert -A - 1 to ~A.  */
10151       if (INTEGRAL_TYPE_P (type)
10152           && TREE_CODE (arg0) == NEGATE_EXPR
10153           && integer_onep (arg1)
10154           && !TYPE_OVERFLOW_TRAPS (type))
10155         return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10156                             fold_convert_loc (loc, type,
10157                                               TREE_OPERAND (arg0, 0)));
10158
10159       /* Convert -1 - A to ~A.  */
10160       if (INTEGRAL_TYPE_P (type)
10161           && integer_all_onesp (arg0))
10162         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10163
10164
10165       /* X - (X / CST) * CST is X % CST.  */
10166       if (INTEGRAL_TYPE_P (type)
10167           && TREE_CODE (arg1) == MULT_EXPR
10168           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10169           && operand_equal_p (arg0,
10170                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10171           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10172                               TREE_OPERAND (arg1, 1), 0))
10173         return
10174           fold_convert_loc (loc, type,
10175                             fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10176                                          arg0, TREE_OPERAND (arg1, 1)));
10177
10178       if (! FLOAT_TYPE_P (type))
10179         {
10180           if (integer_zerop (arg0))
10181             return negate_expr (fold_convert_loc (loc, type, arg1));
10182           if (integer_zerop (arg1))
10183             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10184
10185           /* Fold A - (A & B) into ~B & A.  */
10186           if (!TREE_SIDE_EFFECTS (arg0)
10187               && TREE_CODE (arg1) == BIT_AND_EXPR)
10188             {
10189               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10190                 {
10191                   tree arg10 = fold_convert_loc (loc, type,
10192                                                  TREE_OPERAND (arg1, 0));
10193                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10194                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10195                                                    type, arg10),
10196                                       fold_convert_loc (loc, type, arg0));
10197                 }
10198               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10199                 {
10200                   tree arg11 = fold_convert_loc (loc,
10201                                                  type, TREE_OPERAND (arg1, 1));
10202                   return fold_build2_loc (loc, BIT_AND_EXPR, type,
10203                                       fold_build1_loc (loc, BIT_NOT_EXPR,
10204                                                    type, arg11),
10205                                       fold_convert_loc (loc, type, arg0));
10206                 }
10207             }
10208
10209           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10210              any power of 2 minus 1.  */
10211           if (TREE_CODE (arg0) == BIT_AND_EXPR
10212               && TREE_CODE (arg1) == BIT_AND_EXPR
10213               && operand_equal_p (TREE_OPERAND (arg0, 0),
10214                                   TREE_OPERAND (arg1, 0), 0))
10215             {
10216               tree mask0 = TREE_OPERAND (arg0, 1);
10217               tree mask1 = TREE_OPERAND (arg1, 1);
10218               tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10219
10220               if (operand_equal_p (tem, mask1, 0))
10221                 {
10222                   tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10223                                      TREE_OPERAND (arg0, 0), mask1);
10224                   return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10225                 }
10226             }
10227         }
10228
10229       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10230       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10231         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10232
10233       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10234          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10235          (-ARG1 + ARG0) reduces to -ARG1.  */
10236       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10237         return negate_expr (fold_convert_loc (loc, type, arg1));
10238
10239       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10240          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10241          signed zeros are involved.  */
10242       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10243           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10244           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10245         {
10246           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10247           tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10248           tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10249           bool arg0rz = false, arg0iz = false;
10250           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10251               || (arg0i && (arg0iz = real_zerop (arg0i))))
10252             {
10253               tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10254               tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10255               if (arg0rz && arg1i && real_zerop (arg1i))
10256                 {
10257                   tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10258                                          arg1r ? arg1r
10259                                          : build1 (REALPART_EXPR, rtype, arg1));
10260                   tree ip = arg0i ? arg0i
10261                     : build1 (IMAGPART_EXPR, rtype, arg0);
10262                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10263                 }
10264               else if (arg0iz && arg1r && real_zerop (arg1r))
10265                 {
10266                   tree rp = arg0r ? arg0r
10267                     : build1 (REALPART_EXPR, rtype, arg0);
10268                   tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10269                                          arg1i ? arg1i
10270                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10271                   return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10272                 }
10273             }
10274         }
10275
10276       /* Fold &x - &x.  This can happen from &x.foo - &x.
10277          This is unsafe for certain floats even in non-IEEE formats.
10278          In IEEE, it is unsafe because it does wrong for NaNs.
10279          Also note that operand_equal_p is always false if an operand
10280          is volatile.  */
10281
10282       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10283           && operand_equal_p (arg0, arg1, 0))
10284         return fold_convert_loc (loc, type, integer_zero_node);
10285
10286       /* A - B -> A + (-B) if B is easily negatable.  */
10287       if (negate_expr_p (arg1)
10288           && ((FLOAT_TYPE_P (type)
10289                /* Avoid this transformation if B is a positive REAL_CST.  */
10290                && (TREE_CODE (arg1) != REAL_CST
10291                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10292               || INTEGRAL_TYPE_P (type)))
10293         return fold_build2_loc (loc, PLUS_EXPR, type,
10294                             fold_convert_loc (loc, type, arg0),
10295                             fold_convert_loc (loc, type,
10296                                               negate_expr (arg1)));
10297
10298       /* Try folding difference of addresses.  */
10299       {
10300         HOST_WIDE_INT diff;
10301
10302         if ((TREE_CODE (arg0) == ADDR_EXPR
10303              || TREE_CODE (arg1) == ADDR_EXPR)
10304             && ptr_difference_const (arg0, arg1, &diff))
10305           return build_int_cst_type (type, diff);
10306       }
10307
10308       /* Fold &a[i] - &a[j] to i-j.  */
10309       if (TREE_CODE (arg0) == ADDR_EXPR
10310           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10311           && TREE_CODE (arg1) == ADDR_EXPR
10312           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10313         {
10314           tree aref0 = TREE_OPERAND (arg0, 0);
10315           tree aref1 = TREE_OPERAND (arg1, 0);
10316           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10317                                TREE_OPERAND (aref1, 0), 0))
10318             {
10319               tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10320               tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10321               tree esz = array_ref_element_size (aref0);
10322               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10323               return fold_build2_loc (loc, MULT_EXPR, type, diff,
10324                                   fold_convert_loc (loc, type, esz));
10325
10326             }
10327         }
10328
10329       if (FLOAT_TYPE_P (type)
10330           && flag_unsafe_math_optimizations
10331           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10332           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10333           && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10334         return tem;
10335
10336       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10337          same or one.  Make sure type is not saturating.
10338          fold_plusminus_mult_expr will re-associate.  */
10339       if ((TREE_CODE (arg0) == MULT_EXPR
10340            || TREE_CODE (arg1) == MULT_EXPR)
10341           && !TYPE_SATURATING (type)
10342           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10343         {
10344           tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10345           if (tem)
10346             return tem;
10347         }
10348
10349       goto associate;
10350
10351     case MULT_EXPR:
10352       /* (-A) * (-B) -> A * B  */
10353       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10354         return fold_build2_loc (loc, MULT_EXPR, type,
10355                             fold_convert_loc (loc, type,
10356                                               TREE_OPERAND (arg0, 0)),
10357                             fold_convert_loc (loc, type,
10358                                               negate_expr (arg1)));
10359       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10360         return fold_build2_loc (loc, MULT_EXPR, type,
10361                             fold_convert_loc (loc, type,
10362                                               negate_expr (arg0)),
10363                             fold_convert_loc (loc, type,
10364                                               TREE_OPERAND (arg1, 0)));
10365
10366       if (! FLOAT_TYPE_P (type))
10367         {
10368           if (integer_zerop (arg1))
10369             return omit_one_operand_loc (loc, type, arg1, arg0);
10370           if (integer_onep (arg1))
10371             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10372           /* Transform x * -1 into -x.  Make sure to do the negation
10373              on the original operand with conversions not stripped
10374              because we can only strip non-sign-changing conversions.  */
10375           if (integer_all_onesp (arg1))
10376             return fold_convert_loc (loc, type, negate_expr (op0));
10377           /* Transform x * -C into -x * C if x is easily negatable.  */
10378           if (TREE_CODE (arg1) == INTEGER_CST
10379               && tree_int_cst_sgn (arg1) == -1
10380               && negate_expr_p (arg0)
10381               && (tem = negate_expr (arg1)) != arg1
10382               && !TREE_OVERFLOW (tem))
10383             return fold_build2_loc (loc, MULT_EXPR, type,
10384                                 fold_convert_loc (loc, type,
10385                                                   negate_expr (arg0)),
10386                                 tem);
10387
10388           /* (a * (1 << b)) is (a << b)  */
10389           if (TREE_CODE (arg1) == LSHIFT_EXPR
10390               && integer_onep (TREE_OPERAND (arg1, 0)))
10391             return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10392                                 TREE_OPERAND (arg1, 1));
10393           if (TREE_CODE (arg0) == LSHIFT_EXPR
10394               && integer_onep (TREE_OPERAND (arg0, 0)))
10395             return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10396                                 TREE_OPERAND (arg0, 1));
10397
10398           /* (A + A) * C -> A * 2 * C  */
10399           if (TREE_CODE (arg0) == PLUS_EXPR
10400               && TREE_CODE (arg1) == INTEGER_CST
10401               && operand_equal_p (TREE_OPERAND (arg0, 0),
10402                                   TREE_OPERAND (arg0, 1), 0))
10403             return fold_build2_loc (loc, MULT_EXPR, type,
10404                                 omit_one_operand_loc (loc, type,
10405                                                   TREE_OPERAND (arg0, 0),
10406                                                   TREE_OPERAND (arg0, 1)),
10407                                 fold_build2_loc (loc, MULT_EXPR, type,
10408                                              build_int_cst (type, 2) , arg1));
10409
10410           strict_overflow_p = false;
10411           if (TREE_CODE (arg1) == INTEGER_CST
10412               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10413                                              &strict_overflow_p)))
10414             {
10415               if (strict_overflow_p)
10416                 fold_overflow_warning (("assuming signed overflow does not "
10417                                         "occur when simplifying "
10418                                         "multiplication"),
10419                                        WARN_STRICT_OVERFLOW_MISC);
10420               return fold_convert_loc (loc, type, tem);
10421             }
10422
10423           /* Optimize z * conj(z) for integer complex numbers.  */
10424           if (TREE_CODE (arg0) == CONJ_EXPR
10425               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10426             return fold_mult_zconjz (loc, type, arg1);
10427           if (TREE_CODE (arg1) == CONJ_EXPR
10428               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10429             return fold_mult_zconjz (loc, type, arg0);
10430         }
10431       else
10432         {
10433           /* Maybe fold x * 0 to 0.  The expressions aren't the same
10434              when x is NaN, since x * 0 is also NaN.  Nor are they the
10435              same in modes with signed zeros, since multiplying a
10436              negative value by 0 gives -0, not +0.  */
10437           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10438               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10439               && real_zerop (arg1))
10440             return omit_one_operand_loc (loc, type, arg1, arg0);
10441           /* In IEEE floating point, x*1 is not equivalent to x for snans.
10442              Likewise for complex arithmetic with signed zeros.  */
10443           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10444               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10445                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10446               && real_onep (arg1))
10447             return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10448
10449           /* Transform x * -1.0 into -x.  */
10450           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10451               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10452                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10453               && real_minus_onep (arg1))
10454             return fold_convert_loc (loc, type, negate_expr (arg0));
10455
10456           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
10457              the result for floating point types due to rounding so it is applied
10458              only if -fassociative-math was specify.  */
10459           if (flag_associative_math
10460               && TREE_CODE (arg0) == RDIV_EXPR
10461               && TREE_CODE (arg1) == REAL_CST
10462               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10463             {
10464               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10465                                       arg1);
10466               if (tem)
10467                 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10468                                     TREE_OPERAND (arg0, 1));
10469             }
10470
10471           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
10472           if (operand_equal_p (arg0, arg1, 0))
10473             {
10474               tree tem = fold_strip_sign_ops (arg0);
10475               if (tem != NULL_TREE)
10476                 {
10477                   tem = fold_convert_loc (loc, type, tem);
10478                   return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
10479                 }
10480             }
10481
10482           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10483              This is not the same for NaNs or if signed zeros are
10484              involved.  */
10485           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10486               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10487               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10488               && TREE_CODE (arg1) == COMPLEX_CST
10489               && real_zerop (TREE_REALPART (arg1)))
10490             {
10491               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10492               if (real_onep (TREE_IMAGPART (arg1)))
10493                 return
10494                   fold_build2_loc (loc, COMPLEX_EXPR, type,
10495                                negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10496                                                              rtype, arg0)),
10497                                fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10498               else if (real_minus_onep (TREE_IMAGPART (arg1)))
10499                 return
10500                   fold_build2_loc (loc, COMPLEX_EXPR, type,
10501                                fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10502                                negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10503                                                              rtype, arg0)));
10504             }
10505
10506           /* Optimize z * conj(z) for floating point complex numbers.
10507              Guarded by flag_unsafe_math_optimizations as non-finite
10508              imaginary components don't produce scalar results.  */
10509           if (flag_unsafe_math_optimizations
10510               && TREE_CODE (arg0) == CONJ_EXPR
10511               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10512             return fold_mult_zconjz (loc, type, arg1);
10513           if (flag_unsafe_math_optimizations
10514               && TREE_CODE (arg1) == CONJ_EXPR
10515               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10516             return fold_mult_zconjz (loc, type, arg0);
10517
10518           if (flag_unsafe_math_optimizations)
10519             {
10520               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10521               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10522
10523               /* Optimizations of root(...)*root(...).  */
10524               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10525                 {
10526                   tree rootfn, arg;
10527                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10528                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10529
10530                   /* Optimize sqrt(x)*sqrt(x) as x.  */
10531                   if (BUILTIN_SQRT_P (fcode0)
10532                       && operand_equal_p (arg00, arg10, 0)
10533                       && ! HONOR_SNANS (TYPE_MODE (type)))
10534                     return arg00;
10535
10536                   /* Optimize root(x)*root(y) as root(x*y).  */
10537                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10538                   arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
10539                   return build_call_expr_loc (loc, rootfn, 1, arg);
10540                 }
10541
10542               /* Optimize expN(x)*expN(y) as expN(x+y).  */
10543               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10544                 {
10545                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10546                   tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10547                                           CALL_EXPR_ARG (arg0, 0),
10548                                           CALL_EXPR_ARG (arg1, 0));
10549                   return build_call_expr_loc (loc, expfn, 1, arg);
10550                 }
10551
10552               /* Optimizations of pow(...)*pow(...).  */
10553               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10554                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10555                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10556                 {
10557                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10558                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10559                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10560                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10561
10562                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
10563                   if (operand_equal_p (arg01, arg11, 0))
10564                     {
10565                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10566                       tree arg = fold_build2_loc (loc, MULT_EXPR, type,
10567                                               arg00, arg10);
10568                       return build_call_expr_loc (loc, powfn, 2, arg, arg01);
10569                     }
10570
10571                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
10572                   if (operand_equal_p (arg00, arg10, 0))
10573                     {
10574                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10575                       tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10576                                               arg01, arg11);
10577                       return build_call_expr_loc (loc, powfn, 2, arg00, arg);
10578                     }
10579                 }
10580
10581               /* Optimize tan(x)*cos(x) as sin(x).  */
10582               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10583                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10584                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10585                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10586                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10587                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10588                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10589                                       CALL_EXPR_ARG (arg1, 0), 0))
10590                 {
10591                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10592
10593                   if (sinfn != NULL_TREE)
10594                     return build_call_expr_loc (loc, sinfn, 1,
10595                                             CALL_EXPR_ARG (arg0, 0));
10596                 }
10597
10598               /* Optimize x*pow(x,c) as pow(x,c+1).  */
10599               if (fcode1 == BUILT_IN_POW
10600                   || fcode1 == BUILT_IN_POWF
10601                   || fcode1 == BUILT_IN_POWL)
10602                 {
10603                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10604                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10605                   if (TREE_CODE (arg11) == REAL_CST
10606                       && !TREE_OVERFLOW (arg11)
10607                       && operand_equal_p (arg0, arg10, 0))
10608                     {
10609                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10610                       REAL_VALUE_TYPE c;
10611                       tree arg;
10612
10613                       c = TREE_REAL_CST (arg11);
10614                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10615                       arg = build_real (type, c);
10616                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10617                     }
10618                 }
10619
10620               /* Optimize pow(x,c)*x as pow(x,c+1).  */
10621               if (fcode0 == BUILT_IN_POW
10622                   || fcode0 == BUILT_IN_POWF
10623                   || fcode0 == BUILT_IN_POWL)
10624                 {
10625                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10626                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10627                   if (TREE_CODE (arg01) == REAL_CST
10628                       && !TREE_OVERFLOW (arg01)
10629                       && operand_equal_p (arg1, arg00, 0))
10630                     {
10631                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10632                       REAL_VALUE_TYPE c;
10633                       tree arg;
10634
10635                       c = TREE_REAL_CST (arg01);
10636                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10637                       arg = build_real (type, c);
10638                       return build_call_expr_loc (loc, powfn, 2, arg1, arg);
10639                     }
10640                 }
10641
10642               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
10643               if (optimize_function_for_speed_p (cfun)
10644                   && operand_equal_p (arg0, arg1, 0))
10645                 {
10646                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10647
10648                   if (powfn)
10649                     {
10650                       tree arg = build_real (type, dconst2);
10651                       return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10652                     }
10653                 }
10654             }
10655         }
10656       goto associate;
10657
10658     case BIT_IOR_EXPR:
10659     bit_ior:
10660       if (integer_all_onesp (arg1))
10661         return omit_one_operand_loc (loc, type, arg1, arg0);
10662       if (integer_zerop (arg1))
10663         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10664       if (operand_equal_p (arg0, arg1, 0))
10665         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10666
10667       /* ~X | X is -1.  */
10668       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10669           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10670         {
10671           t1 = fold_convert_loc (loc, type, integer_zero_node);
10672           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10673           return omit_one_operand_loc (loc, type, t1, arg1);
10674         }
10675
10676       /* X | ~X is -1.  */
10677       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10678           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10679         {
10680           t1 = fold_convert_loc (loc, type, integer_zero_node);
10681           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10682           return omit_one_operand_loc (loc, type, t1, arg0);
10683         }
10684
10685       /* Canonicalize (X & C1) | C2.  */
10686       if (TREE_CODE (arg0) == BIT_AND_EXPR
10687           && TREE_CODE (arg1) == INTEGER_CST
10688           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10689         {
10690           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10691           int width = TYPE_PRECISION (type), w;
10692           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10693           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10694           hi2 = TREE_INT_CST_HIGH (arg1);
10695           lo2 = TREE_INT_CST_LOW (arg1);
10696
10697           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
10698           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10699             return omit_one_operand_loc (loc, type, arg1,
10700                                      TREE_OPERAND (arg0, 0));
10701
10702           if (width > HOST_BITS_PER_WIDE_INT)
10703             {
10704               mhi = (unsigned HOST_WIDE_INT) -1
10705                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
10706               mlo = -1;
10707             }
10708           else
10709             {
10710               mhi = 0;
10711               mlo = (unsigned HOST_WIDE_INT) -1
10712                     >> (HOST_BITS_PER_WIDE_INT - width);
10713             }
10714
10715           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
10716           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10717             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10718                                 TREE_OPERAND (arg0, 0), arg1);
10719
10720           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10721              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10722              mode which allows further optimizations.  */
10723           hi1 &= mhi;
10724           lo1 &= mlo;
10725           hi2 &= mhi;
10726           lo2 &= mlo;
10727           hi3 = hi1 & ~hi2;
10728           lo3 = lo1 & ~lo2;
10729           for (w = BITS_PER_UNIT;
10730                w <= width && w <= HOST_BITS_PER_WIDE_INT;
10731                w <<= 1)
10732             {
10733               unsigned HOST_WIDE_INT mask
10734                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10735               if (((lo1 | lo2) & mask) == mask
10736                   && (lo1 & ~mask) == 0 && hi1 == 0)
10737                 {
10738                   hi3 = 0;
10739                   lo3 = mask;
10740                   break;
10741                 }
10742             }
10743           if (hi3 != hi1 || lo3 != lo1)
10744             return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10745                                 fold_build2_loc (loc, BIT_AND_EXPR, type,
10746                                              TREE_OPERAND (arg0, 0),
10747                                              build_int_cst_wide (type,
10748                                                                  lo3, hi3)),
10749                                 arg1);
10750         }
10751
10752       /* (X & Y) | Y is (X, Y).  */
10753       if (TREE_CODE (arg0) == BIT_AND_EXPR
10754           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10755         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10756       /* (X & Y) | X is (Y, X).  */
10757       if (TREE_CODE (arg0) == BIT_AND_EXPR
10758           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10759           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10760         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
10761       /* X | (X & Y) is (Y, X).  */
10762       if (TREE_CODE (arg1) == BIT_AND_EXPR
10763           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10764           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10765         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
10766       /* X | (Y & X) is (Y, X).  */
10767       if (TREE_CODE (arg1) == BIT_AND_EXPR
10768           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10769           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10770         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
10771
10772       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
10773       if (t1 != NULL_TREE)
10774         return t1;
10775
10776       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10777
10778          This results in more efficient code for machines without a NAND
10779          instruction.  Combine will canonicalize to the first form
10780          which will allow use of NAND instructions provided by the
10781          backend if they exist.  */
10782       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10783           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10784         {
10785           return
10786             fold_build1_loc (loc, BIT_NOT_EXPR, type,
10787                          build2 (BIT_AND_EXPR, type,
10788                                  fold_convert_loc (loc, type,
10789                                                    TREE_OPERAND (arg0, 0)),
10790                                  fold_convert_loc (loc, type,
10791                                                    TREE_OPERAND (arg1, 0))));
10792         }
10793
10794       /* See if this can be simplified into a rotate first.  If that
10795          is unsuccessful continue in the association code.  */
10796       goto bit_rotate;
10797
10798     case BIT_XOR_EXPR:
10799       if (integer_zerop (arg1))
10800         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10801       if (integer_all_onesp (arg1))
10802         return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
10803       if (operand_equal_p (arg0, arg1, 0))
10804         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10805
10806       /* ~X ^ X is -1.  */
10807       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10808           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10809         {
10810           t1 = fold_convert_loc (loc, type, integer_zero_node);
10811           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10812           return omit_one_operand_loc (loc, type, t1, arg1);
10813         }
10814
10815       /* X ^ ~X is -1.  */
10816       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10817           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10818         {
10819           t1 = fold_convert_loc (loc, type, integer_zero_node);
10820           t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10821           return omit_one_operand_loc (loc, type, t1, arg0);
10822         }
10823
10824       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10825          with a constant, and the two constants have no bits in common,
10826          we should treat this as a BIT_IOR_EXPR since this may produce more
10827          simplifications.  */
10828       if (TREE_CODE (arg0) == BIT_AND_EXPR
10829           && TREE_CODE (arg1) == BIT_AND_EXPR
10830           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10831           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10832           && integer_zerop (const_binop (BIT_AND_EXPR,
10833                                          TREE_OPERAND (arg0, 1),
10834                                          TREE_OPERAND (arg1, 1))))
10835         {
10836           code = BIT_IOR_EXPR;
10837           goto bit_ior;
10838         }
10839
10840       /* (X | Y) ^ X -> Y & ~ X*/
10841       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10842           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10843         {
10844           tree t2 = TREE_OPERAND (arg0, 1);
10845           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10846                             arg1);
10847           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10848                             fold_convert_loc (loc, type, t2),
10849                             fold_convert_loc (loc, type, t1));
10850           return t1;
10851         }
10852
10853       /* (Y | X) ^ X -> Y & ~ X*/
10854       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10855           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10856         {
10857           tree t2 = TREE_OPERAND (arg0, 0);
10858           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10859                             arg1);
10860           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10861                             fold_convert_loc (loc, type, t2),
10862                             fold_convert_loc (loc, type, t1));
10863           return t1;
10864         }
10865
10866       /* X ^ (X | Y) -> Y & ~ X*/
10867       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10868           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10869         {
10870           tree t2 = TREE_OPERAND (arg1, 1);
10871           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10872                             arg0);
10873           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10874                             fold_convert_loc (loc, type, t2),
10875                             fold_convert_loc (loc, type, t1));
10876           return t1;
10877         }
10878
10879       /* X ^ (Y | X) -> Y & ~ X*/
10880       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10881           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10882         {
10883           tree t2 = TREE_OPERAND (arg1, 0);
10884           t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10885                             arg0);
10886           t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10887                             fold_convert_loc (loc, type, t2),
10888                             fold_convert_loc (loc, type, t1));
10889           return t1;
10890         }
10891
10892       /* Convert ~X ^ ~Y to X ^ Y.  */
10893       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10894           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10895         return fold_build2_loc (loc, code, type,
10896                             fold_convert_loc (loc, type,
10897                                               TREE_OPERAND (arg0, 0)),
10898                             fold_convert_loc (loc, type,
10899                                               TREE_OPERAND (arg1, 0)));
10900
10901       /* Convert ~X ^ C to X ^ ~C.  */
10902       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10903           && TREE_CODE (arg1) == INTEGER_CST)
10904         return fold_build2_loc (loc, code, type,
10905                             fold_convert_loc (loc, type,
10906                                               TREE_OPERAND (arg0, 0)),
10907                             fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
10908
10909       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
10910       if (TREE_CODE (arg0) == BIT_AND_EXPR
10911           && integer_onep (TREE_OPERAND (arg0, 1))
10912           && integer_onep (arg1))
10913         return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10914                             build_int_cst (TREE_TYPE (arg0), 0));
10915
10916       /* Fold (X & Y) ^ Y as ~X & Y.  */
10917       if (TREE_CODE (arg0) == BIT_AND_EXPR
10918           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10919         {
10920           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10921           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10922                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10923                               fold_convert_loc (loc, type, arg1));
10924         }
10925       /* Fold (X & Y) ^ X as ~Y & X.  */
10926       if (TREE_CODE (arg0) == BIT_AND_EXPR
10927           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10928           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10929         {
10930           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10931           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10932                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10933                               fold_convert_loc (loc, type, arg1));
10934         }
10935       /* Fold X ^ (X & Y) as X & ~Y.  */
10936       if (TREE_CODE (arg1) == BIT_AND_EXPR
10937           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10938         {
10939           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10940           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10941                               fold_convert_loc (loc, type, arg0),
10942                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10943         }
10944       /* Fold X ^ (Y & X) as ~Y & X.  */
10945       if (TREE_CODE (arg1) == BIT_AND_EXPR
10946           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10947           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10948         {
10949           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10950           return fold_build2_loc (loc, BIT_AND_EXPR, type,
10951                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10952                               fold_convert_loc (loc, type, arg0));
10953         }
10954
10955       /* See if this can be simplified into a rotate first.  If that
10956          is unsuccessful continue in the association code.  */
10957       goto bit_rotate;
10958
10959     case BIT_AND_EXPR:
10960       if (integer_all_onesp (arg1))
10961         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10962       if (integer_zerop (arg1))
10963         return omit_one_operand_loc (loc, type, arg1, arg0);
10964       if (operand_equal_p (arg0, arg1, 0))
10965         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10966
10967       /* ~X & X is always zero.  */
10968       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10969           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10970         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10971
10972       /* X & ~X is always zero.  */
10973       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10974           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10975         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10976
10977       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
10978       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10979           && TREE_CODE (arg1) == INTEGER_CST
10980           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10981         {
10982           tree tmp1 = fold_convert_loc (loc, type, arg1);
10983           tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10984           tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10985           tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
10986           tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
10987           return
10988             fold_convert_loc (loc, type,
10989                               fold_build2_loc (loc, BIT_IOR_EXPR,
10990                                            type, tmp2, tmp3));
10991         }
10992
10993       /* (X | Y) & Y is (X, Y).  */
10994       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10995           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10996         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10997       /* (X | Y) & X is (Y, X).  */
10998       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10999           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11000           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11001         return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11002       /* X & (X | Y) is (Y, X).  */
11003       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11004           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11005           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11006         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11007       /* X & (Y | X) is (Y, X).  */
11008       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11009           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11010           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11011         return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11012
11013       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
11014       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11015           && integer_onep (TREE_OPERAND (arg0, 1))
11016           && integer_onep (arg1))
11017         {
11018           tem = TREE_OPERAND (arg0, 0);
11019           return fold_build2_loc (loc, EQ_EXPR, type,
11020                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11021                                            build_int_cst (TREE_TYPE (tem), 1)),
11022                               build_int_cst (TREE_TYPE (tem), 0));
11023         }
11024       /* Fold ~X & 1 as (X & 1) == 0.  */
11025       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11026           && integer_onep (arg1))
11027         {
11028           tem = TREE_OPERAND (arg0, 0);
11029           return fold_build2_loc (loc, EQ_EXPR, type,
11030                               fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11031                                            build_int_cst (TREE_TYPE (tem), 1)),
11032                               build_int_cst (TREE_TYPE (tem), 0));
11033         }
11034
11035       /* Fold (X ^ Y) & Y as ~X & Y.  */
11036       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11037           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11038         {
11039           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11040           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11041                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11042                               fold_convert_loc (loc, type, arg1));
11043         }
11044       /* Fold (X ^ Y) & X as ~Y & X.  */
11045       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11046           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11047           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11048         {
11049           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11050           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11051                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11052                               fold_convert_loc (loc, type, arg1));
11053         }
11054       /* Fold X & (X ^ Y) as X & ~Y.  */
11055       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11056           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11057         {
11058           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11059           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11060                               fold_convert_loc (loc, type, arg0),
11061                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11062         }
11063       /* Fold X & (Y ^ X) as ~Y & X.  */
11064       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11065           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11066           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11067         {
11068           tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11069           return fold_build2_loc (loc, BIT_AND_EXPR, type,
11070                               fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11071                               fold_convert_loc (loc, type, arg0));
11072         }
11073
11074       /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11075          ((A & N) + B) & M -> (A + B) & M
11076          Similarly if (N & M) == 0,
11077          ((A | N) + B) & M -> (A + B) & M
11078          and for - instead of + (or unary - instead of +)
11079          and/or ^ instead of |.
11080          If B is constant and (B & M) == 0, fold into A & M.  */
11081       if (host_integerp (arg1, 1))
11082         {
11083           unsigned HOST_WIDE_INT cst1 = tree_low_cst (arg1, 1);
11084           if (~cst1 && (cst1 & (cst1 + 1)) == 0
11085               && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11086               && (TREE_CODE (arg0) == PLUS_EXPR
11087                   || TREE_CODE (arg0) == MINUS_EXPR
11088                   || TREE_CODE (arg0) == NEGATE_EXPR)
11089               && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
11090                   || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
11091             {
11092               tree pmop[2];
11093               int which = 0;
11094               unsigned HOST_WIDE_INT cst0;
11095
11096               /* Now we know that arg0 is (C + D) or (C - D) or
11097                  -C and arg1 (M) is == (1LL << cst) - 1.
11098                  Store C into PMOP[0] and D into PMOP[1].  */
11099               pmop[0] = TREE_OPERAND (arg0, 0);
11100               pmop[1] = NULL;
11101               if (TREE_CODE (arg0) != NEGATE_EXPR)
11102                 {
11103                   pmop[1] = TREE_OPERAND (arg0, 1);
11104                   which = 1;
11105                 }
11106
11107               if (!host_integerp (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11108                   || (tree_low_cst (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11109                       & cst1) != cst1)
11110                 which = -1;
11111
11112               for (; which >= 0; which--)
11113                 switch (TREE_CODE (pmop[which]))
11114                   {
11115                   case BIT_AND_EXPR:
11116                   case BIT_IOR_EXPR:
11117                   case BIT_XOR_EXPR:
11118                     if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
11119                         != INTEGER_CST)
11120                       break;
11121                     /* tree_low_cst not used, because we don't care about
11122                        the upper bits.  */
11123                     cst0 = TREE_INT_CST_LOW (TREE_OPERAND (pmop[which], 1));
11124                     cst0 &= cst1;
11125                     if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
11126                       {
11127                         if (cst0 != cst1)
11128                           break;
11129                       }
11130                     else if (cst0 != 0)
11131                       break;
11132                     /* If C or D is of the form (A & N) where
11133                        (N & M) == M, or of the form (A | N) or
11134                        (A ^ N) where (N & M) == 0, replace it with A.  */
11135                     pmop[which] = TREE_OPERAND (pmop[which], 0);
11136                     break;
11137                   case INTEGER_CST:
11138                     /* If C or D is a N where (N & M) == 0, it can be
11139                        omitted (assumed 0).  */
11140                     if ((TREE_CODE (arg0) == PLUS_EXPR
11141                          || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
11142                         && (TREE_INT_CST_LOW (pmop[which]) & cst1) == 0)
11143                       pmop[which] = NULL;
11144                     break;
11145                   default:
11146                     break;
11147                   }
11148
11149               /* Only build anything new if we optimized one or both arguments
11150                  above.  */
11151               if (pmop[0] != TREE_OPERAND (arg0, 0)
11152                   || (TREE_CODE (arg0) != NEGATE_EXPR
11153                       && pmop[1] != TREE_OPERAND (arg0, 1)))
11154                 {
11155                   tree utype = TREE_TYPE (arg0);
11156                   if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11157                     {
11158                       /* Perform the operations in a type that has defined
11159                          overflow behavior.  */
11160                       utype = unsigned_type_for (TREE_TYPE (arg0));
11161                       if (pmop[0] != NULL)
11162                         pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
11163                       if (pmop[1] != NULL)
11164                         pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
11165                     }
11166
11167                   if (TREE_CODE (arg0) == NEGATE_EXPR)
11168                     tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
11169                   else if (TREE_CODE (arg0) == PLUS_EXPR)
11170                     {
11171                       if (pmop[0] != NULL && pmop[1] != NULL)
11172                         tem = fold_build2_loc (loc, PLUS_EXPR, utype,
11173                                                pmop[0], pmop[1]);
11174                       else if (pmop[0] != NULL)
11175                         tem = pmop[0];
11176                       else if (pmop[1] != NULL)
11177                         tem = pmop[1];
11178                       else
11179                         return build_int_cst (type, 0);
11180                     }
11181                   else if (pmop[0] == NULL)
11182                     tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
11183                   else
11184                     tem = fold_build2_loc (loc, MINUS_EXPR, utype,
11185                                            pmop[0], pmop[1]);
11186                   /* TEM is now the new binary +, - or unary - replacement.  */
11187                   tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
11188                                          fold_convert_loc (loc, utype, arg1));
11189                   return fold_convert_loc (loc, type, tem);
11190                 }
11191             }
11192         }
11193
11194       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11195       if (t1 != NULL_TREE)
11196         return t1;
11197       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
11198       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11199           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11200         {
11201           unsigned int prec
11202             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11203
11204           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11205               && (~TREE_INT_CST_LOW (arg1)
11206                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11207             return
11208               fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11209         }
11210
11211       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11212
11213          This results in more efficient code for machines without a NOR
11214          instruction.  Combine will canonicalize to the first form
11215          which will allow use of NOR instructions provided by the
11216          backend if they exist.  */
11217       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11218           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11219         {
11220           return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11221                               build2 (BIT_IOR_EXPR, type,
11222                                       fold_convert_loc (loc, type,
11223                                                         TREE_OPERAND (arg0, 0)),
11224                                       fold_convert_loc (loc, type,
11225                                                         TREE_OPERAND (arg1, 0))));
11226         }
11227
11228       /* If arg0 is derived from the address of an object or function, we may
11229          be able to fold this expression using the object or function's
11230          alignment.  */
11231       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11232         {
11233           unsigned HOST_WIDE_INT modulus, residue;
11234           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11235
11236           modulus = get_pointer_modulus_and_residue (arg0, &residue,
11237                                                      integer_onep (arg1));
11238
11239           /* This works because modulus is a power of 2.  If this weren't the
11240              case, we'd have to replace it by its greatest power-of-2
11241              divisor: modulus & -modulus.  */
11242           if (low < modulus)
11243             return build_int_cst (type, residue & low);
11244         }
11245
11246       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11247               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11248          if the new mask might be further optimized.  */
11249       if ((TREE_CODE (arg0) == LSHIFT_EXPR
11250            || TREE_CODE (arg0) == RSHIFT_EXPR)
11251           && host_integerp (TREE_OPERAND (arg0, 1), 1)
11252           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11253           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11254              < TYPE_PRECISION (TREE_TYPE (arg0))
11255           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11256           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11257         {
11258           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11259           unsigned HOST_WIDE_INT mask
11260             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11261           unsigned HOST_WIDE_INT newmask, zerobits = 0;
11262           tree shift_type = TREE_TYPE (arg0);
11263
11264           if (TREE_CODE (arg0) == LSHIFT_EXPR)
11265             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11266           else if (TREE_CODE (arg0) == RSHIFT_EXPR
11267                    && TYPE_PRECISION (TREE_TYPE (arg0))
11268                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11269             {
11270               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11271               tree arg00 = TREE_OPERAND (arg0, 0);
11272               /* See if more bits can be proven as zero because of
11273                  zero extension.  */
11274               if (TREE_CODE (arg00) == NOP_EXPR
11275                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11276                 {
11277                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11278                   if (TYPE_PRECISION (inner_type)
11279                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11280                       && TYPE_PRECISION (inner_type) < prec)
11281                     {
11282                       prec = TYPE_PRECISION (inner_type);
11283                       /* See if we can shorten the right shift.  */
11284                       if (shiftc < prec)
11285                         shift_type = inner_type;
11286                     }
11287                 }
11288               zerobits = ~(unsigned HOST_WIDE_INT) 0;
11289               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11290               zerobits <<= prec - shiftc;
11291               /* For arithmetic shift if sign bit could be set, zerobits
11292                  can contain actually sign bits, so no transformation is
11293                  possible, unless MASK masks them all away.  In that
11294                  case the shift needs to be converted into logical shift.  */
11295               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11296                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11297                 {
11298                   if ((mask & zerobits) == 0)
11299                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
11300                   else
11301                     zerobits = 0;
11302                 }
11303             }
11304
11305           /* ((X << 16) & 0xff00) is (X, 0).  */
11306           if ((mask & zerobits) == mask)
11307             return omit_one_operand_loc (loc, type,
11308                                      build_int_cst (type, 0), arg0);
11309
11310           newmask = mask | zerobits;
11311           if (newmask != mask && (newmask & (newmask + 1)) == 0)
11312             {
11313               unsigned int prec;
11314
11315               /* Only do the transformation if NEWMASK is some integer
11316                  mode's mask.  */
11317               for (prec = BITS_PER_UNIT;
11318                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11319                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11320                   break;
11321               if (prec < HOST_BITS_PER_WIDE_INT
11322                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
11323                 {
11324                   tree newmaskt;
11325
11326                   if (shift_type != TREE_TYPE (arg0))
11327                     {
11328                       tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11329                                          fold_convert_loc (loc, shift_type,
11330                                                            TREE_OPERAND (arg0, 0)),
11331                                          TREE_OPERAND (arg0, 1));
11332                       tem = fold_convert_loc (loc, type, tem);
11333                     }
11334                   else
11335                     tem = op0;
11336                   newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11337                   if (!tree_int_cst_equal (newmaskt, arg1))
11338                     return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11339                 }
11340             }
11341         }
11342
11343       goto associate;
11344
11345     case RDIV_EXPR:
11346       /* Don't touch a floating-point divide by zero unless the mode
11347          of the constant can represent infinity.  */
11348       if (TREE_CODE (arg1) == REAL_CST
11349           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11350           && real_zerop (arg1))
11351         return NULL_TREE;
11352
11353       /* Optimize A / A to 1.0 if we don't care about
11354          NaNs or Infinities.  Skip the transformation
11355          for non-real operands.  */
11356       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11357           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11358           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11359           && operand_equal_p (arg0, arg1, 0))
11360         {
11361           tree r = build_real (TREE_TYPE (arg0), dconst1);
11362
11363           return omit_two_operands_loc (loc, type, r, arg0, arg1);
11364         }
11365
11366       /* The complex version of the above A / A optimization.  */
11367       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11368           && operand_equal_p (arg0, arg1, 0))
11369         {
11370           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11371           if (! HONOR_NANS (TYPE_MODE (elem_type))
11372               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11373             {
11374               tree r = build_real (elem_type, dconst1);
11375               /* omit_two_operands will call fold_convert for us.  */
11376               return omit_two_operands_loc (loc, type, r, arg0, arg1);
11377             }
11378         }
11379
11380       /* (-A) / (-B) -> A / B  */
11381       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11382         return fold_build2_loc (loc, RDIV_EXPR, type,
11383                             TREE_OPERAND (arg0, 0),
11384                             negate_expr (arg1));
11385       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11386         return fold_build2_loc (loc, RDIV_EXPR, type,
11387                             negate_expr (arg0),
11388                             TREE_OPERAND (arg1, 0));
11389
11390       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
11391       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11392           && real_onep (arg1))
11393         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11394
11395       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
11396       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11397           && real_minus_onep (arg1))
11398         return non_lvalue_loc (loc, fold_convert_loc (loc, type,
11399                                                   negate_expr (arg0)));
11400
11401       /* If ARG1 is a constant, we can convert this to a multiply by the
11402          reciprocal.  This does not have the same rounding properties,
11403          so only do this if -freciprocal-math.  We can actually
11404          always safely do it if ARG1 is a power of two, but it's hard to
11405          tell if it is or not in a portable manner.  */
11406       if (TREE_CODE (arg1) == REAL_CST)
11407         {
11408           if (flag_reciprocal_math
11409               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11410                                           arg1)))
11411             return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
11412           /* Find the reciprocal if optimizing and the result is exact.  */
11413           if (optimize)
11414             {
11415               REAL_VALUE_TYPE r;
11416               r = TREE_REAL_CST (arg1);
11417               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11418                 {
11419                   tem = build_real (type, r);
11420                   return fold_build2_loc (loc, MULT_EXPR, type,
11421                                       fold_convert_loc (loc, type, arg0), tem);
11422                 }
11423             }
11424         }
11425       /* Convert A/B/C to A/(B*C).  */
11426       if (flag_reciprocal_math
11427           && TREE_CODE (arg0) == RDIV_EXPR)
11428         return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11429                             fold_build2_loc (loc, MULT_EXPR, type,
11430                                          TREE_OPERAND (arg0, 1), arg1));
11431
11432       /* Convert A/(B/C) to (A/B)*C.  */
11433       if (flag_reciprocal_math
11434           && TREE_CODE (arg1) == RDIV_EXPR)
11435         return fold_build2_loc (loc, MULT_EXPR, type,
11436                             fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11437                                          TREE_OPERAND (arg1, 0)),
11438                             TREE_OPERAND (arg1, 1));
11439
11440       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11441       if (flag_reciprocal_math
11442           && TREE_CODE (arg1) == MULT_EXPR
11443           && TREE_CODE (arg0) == REAL_CST
11444           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11445         {
11446           tree tem = const_binop (RDIV_EXPR, arg0,
11447                                   TREE_OPERAND (arg1, 1));
11448           if (tem)
11449             return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11450                                 TREE_OPERAND (arg1, 0));
11451         }
11452
11453       if (flag_unsafe_math_optimizations)
11454         {
11455           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11456           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11457
11458           /* Optimize sin(x)/cos(x) as tan(x).  */
11459           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11460                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11461                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11462               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11463                                   CALL_EXPR_ARG (arg1, 0), 0))
11464             {
11465               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11466
11467               if (tanfn != NULL_TREE)
11468                 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11469             }
11470
11471           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11472           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11473                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11474                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11475               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11476                                   CALL_EXPR_ARG (arg1, 0), 0))
11477             {
11478               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11479
11480               if (tanfn != NULL_TREE)
11481                 {
11482                   tree tmp = build_call_expr_loc (loc, tanfn, 1,
11483                                               CALL_EXPR_ARG (arg0, 0));
11484                   return fold_build2_loc (loc, RDIV_EXPR, type,
11485                                       build_real (type, dconst1), tmp);
11486                 }
11487             }
11488
11489           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11490              NaNs or Infinities.  */
11491           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11492                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11493                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11494             {
11495               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11496               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11497
11498               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11499                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11500                   && operand_equal_p (arg00, arg01, 0))
11501                 {
11502                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11503
11504                   if (cosfn != NULL_TREE)
11505                     return build_call_expr_loc (loc, cosfn, 1, arg00);
11506                 }
11507             }
11508
11509           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11510              NaNs or Infinities.  */
11511           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11512                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11513                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11514             {
11515               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11516               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11517
11518               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11519                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11520                   && operand_equal_p (arg00, arg01, 0))
11521                 {
11522                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11523
11524                   if (cosfn != NULL_TREE)
11525                     {
11526                       tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11527                       return fold_build2_loc (loc, RDIV_EXPR, type,
11528                                           build_real (type, dconst1),
11529                                           tmp);
11530                     }
11531                 }
11532             }
11533
11534           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11535           if (fcode0 == BUILT_IN_POW
11536               || fcode0 == BUILT_IN_POWF
11537               || fcode0 == BUILT_IN_POWL)
11538             {
11539               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11540               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11541               if (TREE_CODE (arg01) == REAL_CST
11542                   && !TREE_OVERFLOW (arg01)
11543                   && operand_equal_p (arg1, arg00, 0))
11544                 {
11545                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11546                   REAL_VALUE_TYPE c;
11547                   tree arg;
11548
11549                   c = TREE_REAL_CST (arg01);
11550                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11551                   arg = build_real (type, c);
11552                   return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11553                 }
11554             }
11555
11556           /* Optimize a/root(b/c) into a*root(c/b).  */
11557           if (BUILTIN_ROOT_P (fcode1))
11558             {
11559               tree rootarg = CALL_EXPR_ARG (arg1, 0);
11560
11561               if (TREE_CODE (rootarg) == RDIV_EXPR)
11562                 {
11563                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11564                   tree b = TREE_OPERAND (rootarg, 0);
11565                   tree c = TREE_OPERAND (rootarg, 1);
11566
11567                   tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
11568
11569                   tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
11570                   return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
11571                 }
11572             }
11573
11574           /* Optimize x/expN(y) into x*expN(-y).  */
11575           if (BUILTIN_EXPONENT_P (fcode1))
11576             {
11577               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11578               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11579               arg1 = build_call_expr_loc (loc,
11580                                       expfn, 1,
11581                                       fold_convert_loc (loc, type, arg));
11582               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11583             }
11584
11585           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
11586           if (fcode1 == BUILT_IN_POW
11587               || fcode1 == BUILT_IN_POWF
11588               || fcode1 == BUILT_IN_POWL)
11589             {
11590               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11591               tree arg10 = CALL_EXPR_ARG (arg1, 0);
11592               tree arg11 = CALL_EXPR_ARG (arg1, 1);
11593               tree neg11 = fold_convert_loc (loc, type,
11594                                              negate_expr (arg11));
11595               arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
11596               return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11597             }
11598         }
11599       return NULL_TREE;
11600
11601     case TRUNC_DIV_EXPR:
11602     case FLOOR_DIV_EXPR:
11603       /* Simplify A / (B << N) where A and B are positive and B is
11604          a power of 2, to A >> (N + log2(B)).  */
11605       strict_overflow_p = false;
11606       if (TREE_CODE (arg1) == LSHIFT_EXPR
11607           && (TYPE_UNSIGNED (type)
11608               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11609         {
11610           tree sval = TREE_OPERAND (arg1, 0);
11611           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11612             {
11613               tree sh_cnt = TREE_OPERAND (arg1, 1);
11614               unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11615
11616               if (strict_overflow_p)
11617                 fold_overflow_warning (("assuming signed overflow does not "
11618                                         "occur when simplifying A / (B << N)"),
11619                                        WARN_STRICT_OVERFLOW_MISC);
11620
11621               sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11622                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
11623               return fold_build2_loc (loc, RSHIFT_EXPR, type,
11624                                   fold_convert_loc (loc, type, arg0), sh_cnt);
11625             }
11626         }
11627
11628       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11629          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
11630       if (INTEGRAL_TYPE_P (type)
11631           && TYPE_UNSIGNED (type)
11632           && code == FLOOR_DIV_EXPR)
11633         return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
11634
11635       /* Fall thru */
11636
11637     case ROUND_DIV_EXPR:
11638     case CEIL_DIV_EXPR:
11639     case EXACT_DIV_EXPR:
11640       if (integer_onep (arg1))
11641         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11642       if (integer_zerop (arg1))
11643         return NULL_TREE;
11644       /* X / -1 is -X.  */
11645       if (!TYPE_UNSIGNED (type)
11646           && TREE_CODE (arg1) == INTEGER_CST
11647           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11648           && TREE_INT_CST_HIGH (arg1) == -1)
11649         return fold_convert_loc (loc, type, negate_expr (arg0));
11650
11651       /* Convert -A / -B to A / B when the type is signed and overflow is
11652          undefined.  */
11653       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11654           && TREE_CODE (arg0) == NEGATE_EXPR
11655           && negate_expr_p (arg1))
11656         {
11657           if (INTEGRAL_TYPE_P (type))
11658             fold_overflow_warning (("assuming signed overflow does not occur "
11659                                     "when distributing negation across "
11660                                     "division"),
11661                                    WARN_STRICT_OVERFLOW_MISC);
11662           return fold_build2_loc (loc, code, type,
11663                               fold_convert_loc (loc, type,
11664                                                 TREE_OPERAND (arg0, 0)),
11665                               fold_convert_loc (loc, type,
11666                                                 negate_expr (arg1)));
11667         }
11668       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11669           && TREE_CODE (arg1) == NEGATE_EXPR
11670           && negate_expr_p (arg0))
11671         {
11672           if (INTEGRAL_TYPE_P (type))
11673             fold_overflow_warning (("assuming signed overflow does not occur "
11674                                     "when distributing negation across "
11675                                     "division"),
11676                                    WARN_STRICT_OVERFLOW_MISC);
11677           return fold_build2_loc (loc, code, type,
11678                               fold_convert_loc (loc, type,
11679                                                 negate_expr (arg0)),
11680                               fold_convert_loc (loc, type,
11681                                                 TREE_OPERAND (arg1, 0)));
11682         }
11683
11684       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11685          operation, EXACT_DIV_EXPR.
11686
11687          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11688          At one time others generated faster code, it's not clear if they do
11689          after the last round to changes to the DIV code in expmed.c.  */
11690       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11691           && multiple_of_p (type, arg0, arg1))
11692         return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
11693
11694       strict_overflow_p = false;
11695       if (TREE_CODE (arg1) == INTEGER_CST
11696           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11697                                          &strict_overflow_p)))
11698         {
11699           if (strict_overflow_p)
11700             fold_overflow_warning (("assuming signed overflow does not occur "
11701                                     "when simplifying division"),
11702                                    WARN_STRICT_OVERFLOW_MISC);
11703           return fold_convert_loc (loc, type, tem);
11704         }
11705
11706       return NULL_TREE;
11707
11708     case CEIL_MOD_EXPR:
11709     case FLOOR_MOD_EXPR:
11710     case ROUND_MOD_EXPR:
11711     case TRUNC_MOD_EXPR:
11712       /* X % 1 is always zero, but be sure to preserve any side
11713          effects in X.  */
11714       if (integer_onep (arg1))
11715         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11716
11717       /* X % 0, return X % 0 unchanged so that we can get the
11718          proper warnings and errors.  */
11719       if (integer_zerop (arg1))
11720         return NULL_TREE;
11721
11722       /* 0 % X is always zero, but be sure to preserve any side
11723          effects in X.  Place this after checking for X == 0.  */
11724       if (integer_zerop (arg0))
11725         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11726
11727       /* X % -1 is zero.  */
11728       if (!TYPE_UNSIGNED (type)
11729           && TREE_CODE (arg1) == INTEGER_CST
11730           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11731           && TREE_INT_CST_HIGH (arg1) == -1)
11732         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11733
11734       /* X % -C is the same as X % C.  */
11735       if (code == TRUNC_MOD_EXPR
11736           && !TYPE_UNSIGNED (type)
11737           && TREE_CODE (arg1) == INTEGER_CST
11738           && !TREE_OVERFLOW (arg1)
11739           && TREE_INT_CST_HIGH (arg1) < 0
11740           && !TYPE_OVERFLOW_TRAPS (type)
11741           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
11742           && !sign_bit_p (arg1, arg1))
11743         return fold_build2_loc (loc, code, type,
11744                             fold_convert_loc (loc, type, arg0),
11745                             fold_convert_loc (loc, type,
11746                                               negate_expr (arg1)));
11747
11748       /* X % -Y is the same as X % Y.  */
11749       if (code == TRUNC_MOD_EXPR
11750           && !TYPE_UNSIGNED (type)
11751           && TREE_CODE (arg1) == NEGATE_EXPR
11752           && !TYPE_OVERFLOW_TRAPS (type))
11753         return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
11754                             fold_convert_loc (loc, type,
11755                                               TREE_OPERAND (arg1, 0)));
11756
11757       strict_overflow_p = false;
11758       if (TREE_CODE (arg1) == INTEGER_CST
11759           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11760                                          &strict_overflow_p)))
11761         {
11762           if (strict_overflow_p)
11763             fold_overflow_warning (("assuming signed overflow does not occur "
11764                                     "when simplifying modulus"),
11765                                    WARN_STRICT_OVERFLOW_MISC);
11766           return fold_convert_loc (loc, type, tem);
11767         }
11768
11769       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11770          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
11771       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11772           && (TYPE_UNSIGNED (type)
11773               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11774         {
11775           tree c = arg1;
11776           /* Also optimize A % (C << N)  where C is a power of 2,
11777              to A & ((C << N) - 1).  */
11778           if (TREE_CODE (arg1) == LSHIFT_EXPR)
11779             c = TREE_OPERAND (arg1, 0);
11780
11781           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11782             {
11783               tree mask
11784                 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
11785                                    build_int_cst (TREE_TYPE (arg1), 1));
11786               if (strict_overflow_p)
11787                 fold_overflow_warning (("assuming signed overflow does not "
11788                                         "occur when simplifying "
11789                                         "X % (power of two)"),
11790                                        WARN_STRICT_OVERFLOW_MISC);
11791               return fold_build2_loc (loc, BIT_AND_EXPR, type,
11792                                       fold_convert_loc (loc, type, arg0),
11793                                       fold_convert_loc (loc, type, mask));
11794             }
11795         }
11796
11797       return NULL_TREE;
11798
11799     case LROTATE_EXPR:
11800     case RROTATE_EXPR:
11801       if (integer_all_onesp (arg0))
11802         return omit_one_operand_loc (loc, type, arg0, arg1);
11803       goto shift;
11804
11805     case RSHIFT_EXPR:
11806       /* Optimize -1 >> x for arithmetic right shifts.  */
11807       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11808           && tree_expr_nonnegative_p (arg1))
11809         return omit_one_operand_loc (loc, type, arg0, arg1);
11810       /* ... fall through ...  */
11811
11812     case LSHIFT_EXPR:
11813     shift:
11814       if (integer_zerop (arg1))
11815         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11816       if (integer_zerop (arg0))
11817         return omit_one_operand_loc (loc, type, arg0, arg1);
11818
11819       /* Since negative shift count is not well-defined,
11820          don't try to compute it in the compiler.  */
11821       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11822         return NULL_TREE;
11823
11824       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
11825       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11826           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11827           && host_integerp (TREE_OPERAND (arg0, 1), false)
11828           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11829         {
11830           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11831                                + TREE_INT_CST_LOW (arg1));
11832
11833           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11834              being well defined.  */
11835           if (low >= TYPE_PRECISION (type))
11836             {
11837               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11838                 low = low % TYPE_PRECISION (type);
11839               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11840                 return omit_one_operand_loc (loc, type, build_int_cst (type, 0),
11841                                          TREE_OPERAND (arg0, 0));
11842               else
11843                 low = TYPE_PRECISION (type) - 1;
11844             }
11845
11846           return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11847                               build_int_cst (type, low));
11848         }
11849
11850       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11851          into x & ((unsigned)-1 >> c) for unsigned types.  */
11852       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11853            || (TYPE_UNSIGNED (type)
11854                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11855           && host_integerp (arg1, false)
11856           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11857           && host_integerp (TREE_OPERAND (arg0, 1), false)
11858           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11859         {
11860           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11861           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11862           tree lshift;
11863           tree arg00;
11864
11865           if (low0 == low1)
11866             {
11867               arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11868
11869               lshift = build_int_cst (type, -1);
11870               lshift = int_const_binop (code, lshift, arg1, 0);
11871
11872               return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
11873             }
11874         }
11875
11876       /* Rewrite an LROTATE_EXPR by a constant into an
11877          RROTATE_EXPR by a new constant.  */
11878       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11879         {
11880           tree tem = build_int_cst (TREE_TYPE (arg1),
11881                                     TYPE_PRECISION (type));
11882           tem = const_binop (MINUS_EXPR, tem, arg1);
11883           return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
11884         }
11885
11886       /* If we have a rotate of a bit operation with the rotate count and
11887          the second operand of the bit operation both constant,
11888          permute the two operations.  */
11889       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11890           && (TREE_CODE (arg0) == BIT_AND_EXPR
11891               || TREE_CODE (arg0) == BIT_IOR_EXPR
11892               || TREE_CODE (arg0) == BIT_XOR_EXPR)
11893           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11894         return fold_build2_loc (loc, TREE_CODE (arg0), type,
11895                             fold_build2_loc (loc, code, type,
11896                                          TREE_OPERAND (arg0, 0), arg1),
11897                             fold_build2_loc (loc, code, type,
11898                                          TREE_OPERAND (arg0, 1), arg1));
11899
11900       /* Two consecutive rotates adding up to the precision of the
11901          type can be ignored.  */
11902       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11903           && TREE_CODE (arg0) == RROTATE_EXPR
11904           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11905           && TREE_INT_CST_HIGH (arg1) == 0
11906           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11907           && ((TREE_INT_CST_LOW (arg1)
11908                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11909               == (unsigned int) TYPE_PRECISION (type)))
11910         return TREE_OPERAND (arg0, 0);
11911
11912       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11913               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11914          if the latter can be further optimized.  */
11915       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11916           && TREE_CODE (arg0) == BIT_AND_EXPR
11917           && TREE_CODE (arg1) == INTEGER_CST
11918           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11919         {
11920           tree mask = fold_build2_loc (loc, code, type,
11921                                    fold_convert_loc (loc, type,
11922                                                      TREE_OPERAND (arg0, 1)),
11923                                    arg1);
11924           tree shift = fold_build2_loc (loc, code, type,
11925                                     fold_convert_loc (loc, type,
11926                                                       TREE_OPERAND (arg0, 0)),
11927                                     arg1);
11928           tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
11929           if (tem)
11930             return tem;
11931         }
11932
11933       return NULL_TREE;
11934
11935     case MIN_EXPR:
11936       if (operand_equal_p (arg0, arg1, 0))
11937         return omit_one_operand_loc (loc, type, arg0, arg1);
11938       if (INTEGRAL_TYPE_P (type)
11939           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11940         return omit_one_operand_loc (loc, type, arg1, arg0);
11941       tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
11942       if (tem)
11943         return tem;
11944       goto associate;
11945
11946     case MAX_EXPR:
11947       if (operand_equal_p (arg0, arg1, 0))
11948         return omit_one_operand_loc (loc, type, arg0, arg1);
11949       if (INTEGRAL_TYPE_P (type)
11950           && TYPE_MAX_VALUE (type)
11951           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11952         return omit_one_operand_loc (loc, type, arg1, arg0);
11953       tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
11954       if (tem)
11955         return tem;
11956       goto associate;
11957
11958     case TRUTH_ANDIF_EXPR:
11959       /* Note that the operands of this must be ints
11960          and their values must be 0 or 1.
11961          ("true" is a fixed value perhaps depending on the language.)  */
11962       /* If first arg is constant zero, return it.  */
11963       if (integer_zerop (arg0))
11964         return fold_convert_loc (loc, type, arg0);
11965     case TRUTH_AND_EXPR:
11966       /* If either arg is constant true, drop it.  */
11967       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11968         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11969       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11970           /* Preserve sequence points.  */
11971           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11972         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11973       /* If second arg is constant zero, result is zero, but first arg
11974          must be evaluated.  */
11975       if (integer_zerop (arg1))
11976         return omit_one_operand_loc (loc, type, arg1, arg0);
11977       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11978          case will be handled here.  */
11979       if (integer_zerop (arg0))
11980         return omit_one_operand_loc (loc, type, arg0, arg1);
11981
11982       /* !X && X is always false.  */
11983       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11984           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11985         return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11986       /* X && !X is always false.  */
11987       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11988           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11989         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11990
11991       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
11992          means A >= Y && A != MAX, but in this case we know that
11993          A < X <= MAX.  */
11994
11995       if (!TREE_SIDE_EFFECTS (arg0)
11996           && !TREE_SIDE_EFFECTS (arg1))
11997         {
11998           tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
11999           if (tem && !operand_equal_p (tem, arg0, 0))
12000             return fold_build2_loc (loc, code, type, tem, arg1);
12001
12002           tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12003           if (tem && !operand_equal_p (tem, arg1, 0))
12004             return fold_build2_loc (loc, code, type, arg0, tem);
12005         }
12006
12007     truth_andor:
12008       /* We only do these simplifications if we are optimizing.  */
12009       if (!optimize)
12010         return NULL_TREE;
12011
12012       /* Check for things like (A || B) && (A || C).  We can convert this
12013          to A || (B && C).  Note that either operator can be any of the four
12014          truth and/or operations and the transformation will still be
12015          valid.   Also note that we only care about order for the
12016          ANDIF and ORIF operators.  If B contains side effects, this
12017          might change the truth-value of A.  */
12018       if (TREE_CODE (arg0) == TREE_CODE (arg1)
12019           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
12020               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
12021               || TREE_CODE (arg0) == TRUTH_AND_EXPR
12022               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
12023           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
12024         {
12025           tree a00 = TREE_OPERAND (arg0, 0);
12026           tree a01 = TREE_OPERAND (arg0, 1);
12027           tree a10 = TREE_OPERAND (arg1, 0);
12028           tree a11 = TREE_OPERAND (arg1, 1);
12029           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
12030                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
12031                              && (code == TRUTH_AND_EXPR
12032                                  || code == TRUTH_OR_EXPR));
12033
12034           if (operand_equal_p (a00, a10, 0))
12035             return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
12036                                 fold_build2_loc (loc, code, type, a01, a11));
12037           else if (commutative && operand_equal_p (a00, a11, 0))
12038             return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
12039                                 fold_build2_loc (loc, code, type, a01, a10));
12040           else if (commutative && operand_equal_p (a01, a10, 0))
12041             return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
12042                                 fold_build2_loc (loc, code, type, a00, a11));
12043
12044           /* This case if tricky because we must either have commutative
12045              operators or else A10 must not have side-effects.  */
12046
12047           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12048                    && operand_equal_p (a01, a11, 0))
12049             return fold_build2_loc (loc, TREE_CODE (arg0), type,
12050                                 fold_build2_loc (loc, code, type, a00, a10),
12051                                 a01);
12052         }
12053
12054       /* See if we can build a range comparison.  */
12055       if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
12056         return tem;
12057
12058       if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
12059           || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
12060         {
12061           tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
12062           if (tem)
12063             return fold_build2_loc (loc, code, type, tem, arg1);
12064         }
12065
12066       if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
12067           || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
12068         {
12069           tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
12070           if (tem)
12071             return fold_build2_loc (loc, code, type, arg0, tem);
12072         }
12073
12074       /* Check for the possibility of merging component references.  If our
12075          lhs is another similar operation, try to merge its rhs with our
12076          rhs.  Then try to merge our lhs and rhs.  */
12077       if (TREE_CODE (arg0) == code
12078           && 0 != (tem = fold_truthop (loc, code, type,
12079                                        TREE_OPERAND (arg0, 1), arg1)))
12080         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12081
12082       if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0)
12083         return tem;
12084
12085       return NULL_TREE;
12086
12087     case TRUTH_ORIF_EXPR:
12088       /* Note that the operands of this must be ints
12089          and their values must be 0 or true.
12090          ("true" is a fixed value perhaps depending on the language.)  */
12091       /* If first arg is constant true, return it.  */
12092       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12093         return fold_convert_loc (loc, type, arg0);
12094     case TRUTH_OR_EXPR:
12095       /* If either arg is constant zero, drop it.  */
12096       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12097         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12098       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12099           /* Preserve sequence points.  */
12100           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12101         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12102       /* If second arg is constant true, result is true, but we must
12103          evaluate first arg.  */
12104       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12105         return omit_one_operand_loc (loc, type, arg1, arg0);
12106       /* Likewise for first arg, but note this only occurs here for
12107          TRUTH_OR_EXPR.  */
12108       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12109         return omit_one_operand_loc (loc, type, arg0, arg1);
12110
12111       /* !X || X is always true.  */
12112       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12113           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12114         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12115       /* X || !X is always true.  */
12116       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12117           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12118         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12119
12120       goto truth_andor;
12121
12122     case TRUTH_XOR_EXPR:
12123       /* If the second arg is constant zero, drop it.  */
12124       if (integer_zerop (arg1))
12125         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12126       /* If the second arg is constant true, this is a logical inversion.  */
12127       if (integer_onep (arg1))
12128         {
12129           /* Only call invert_truthvalue if operand is a truth value.  */
12130           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12131             tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12132           else
12133             tem = invert_truthvalue_loc (loc, arg0);
12134           return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12135         }
12136       /* Identical arguments cancel to zero.  */
12137       if (operand_equal_p (arg0, arg1, 0))
12138         return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12139
12140       /* !X ^ X is always true.  */
12141       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12142           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12143         return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12144
12145       /* X ^ !X is always true.  */
12146       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12147           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12148         return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12149
12150       return NULL_TREE;
12151
12152     case EQ_EXPR:
12153     case NE_EXPR:
12154       tem = fold_comparison (loc, code, type, op0, op1);
12155       if (tem != NULL_TREE)
12156         return tem;
12157
12158       /* bool_var != 0 becomes bool_var. */
12159       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12160           && code == NE_EXPR)
12161         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12162
12163       /* bool_var == 1 becomes bool_var. */
12164       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12165           && code == EQ_EXPR)
12166         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12167
12168       /* bool_var != 1 becomes !bool_var. */
12169       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12170           && code == NE_EXPR)
12171         return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12172                             fold_convert_loc (loc, type, arg0));
12173
12174       /* bool_var == 0 becomes !bool_var. */
12175       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12176           && code == EQ_EXPR)
12177         return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12178                             fold_convert_loc (loc, type, arg0));
12179
12180       /* !exp != 0 becomes !exp */
12181       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12182           && code == NE_EXPR)
12183         return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12184
12185       /* If this is an equality comparison of the address of two non-weak,
12186          unaliased symbols neither of which are extern (since we do not
12187          have access to attributes for externs), then we know the result.  */
12188       if (TREE_CODE (arg0) == ADDR_EXPR
12189           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12190           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12191           && ! lookup_attribute ("alias",
12192                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12193           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12194           && TREE_CODE (arg1) == ADDR_EXPR
12195           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12196           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12197           && ! lookup_attribute ("alias",
12198                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12199           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12200         {
12201           /* We know that we're looking at the address of two
12202              non-weak, unaliased, static _DECL nodes.
12203
12204              It is both wasteful and incorrect to call operand_equal_p
12205              to compare the two ADDR_EXPR nodes.  It is wasteful in that
12206              all we need to do is test pointer equality for the arguments
12207              to the two ADDR_EXPR nodes.  It is incorrect to use
12208              operand_equal_p as that function is NOT equivalent to a
12209              C equality test.  It can in fact return false for two
12210              objects which would test as equal using the C equality
12211              operator.  */
12212           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12213           return constant_boolean_node (equal
12214                                         ? code == EQ_EXPR : code != EQ_EXPR,
12215                                         type);
12216         }
12217
12218       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12219          a MINUS_EXPR of a constant, we can convert it into a comparison with
12220          a revised constant as long as no overflow occurs.  */
12221       if (TREE_CODE (arg1) == INTEGER_CST
12222           && (TREE_CODE (arg0) == PLUS_EXPR
12223               || TREE_CODE (arg0) == MINUS_EXPR)
12224           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12225           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12226                                       ? MINUS_EXPR : PLUS_EXPR,
12227                                       fold_convert_loc (loc, TREE_TYPE (arg0),
12228                                                         arg1),
12229                                       TREE_OPERAND (arg0, 1)))
12230           && !TREE_OVERFLOW (tem))
12231         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12232
12233       /* Similarly for a NEGATE_EXPR.  */
12234       if (TREE_CODE (arg0) == NEGATE_EXPR
12235           && TREE_CODE (arg1) == INTEGER_CST
12236           && 0 != (tem = negate_expr (arg1))
12237           && TREE_CODE (tem) == INTEGER_CST
12238           && !TREE_OVERFLOW (tem))
12239         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12240
12241       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
12242       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12243           && TREE_CODE (arg1) == INTEGER_CST
12244           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12245         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12246                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12247                                          fold_convert_loc (loc,
12248                                                            TREE_TYPE (arg0),
12249                                                            arg1),
12250                                          TREE_OPERAND (arg0, 1)));
12251
12252       /* Transform comparisons of the form X +- Y CMP X to Y CMP 0.  */
12253       if ((TREE_CODE (arg0) == PLUS_EXPR
12254            || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12255            || TREE_CODE (arg0) == MINUS_EXPR)
12256           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12257           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12258               || POINTER_TYPE_P (TREE_TYPE (arg0))))
12259         {
12260           tree val = TREE_OPERAND (arg0, 1);
12261           return omit_two_operands_loc (loc, type,
12262                                     fold_build2_loc (loc, code, type,
12263                                                  val,
12264                                                  build_int_cst (TREE_TYPE (val),
12265                                                                 0)),
12266                                     TREE_OPERAND (arg0, 0), arg1);
12267         }
12268
12269       /* Transform comparisons of the form C - X CMP X if C % 2 == 1.  */
12270       if (TREE_CODE (arg0) == MINUS_EXPR
12271           && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12272           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)
12273           && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12274         {
12275           return omit_two_operands_loc (loc, type,
12276                                     code == NE_EXPR
12277                                     ? boolean_true_node : boolean_false_node,
12278                                     TREE_OPERAND (arg0, 1), arg1);
12279         }
12280
12281       /* If we have X - Y == 0, we can convert that to X == Y and similarly
12282          for !=.  Don't do this for ordered comparisons due to overflow.  */
12283       if (TREE_CODE (arg0) == MINUS_EXPR
12284           && integer_zerop (arg1))
12285         return fold_build2_loc (loc, code, type,
12286                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12287
12288       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
12289       if (TREE_CODE (arg0) == ABS_EXPR
12290           && (integer_zerop (arg1) || real_zerop (arg1)))
12291         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12292
12293       /* If this is an EQ or NE comparison with zero and ARG0 is
12294          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
12295          two operations, but the latter can be done in one less insn
12296          on machines that have only two-operand insns or on which a
12297          constant cannot be the first operand.  */
12298       if (TREE_CODE (arg0) == BIT_AND_EXPR
12299           && integer_zerop (arg1))
12300         {
12301           tree arg00 = TREE_OPERAND (arg0, 0);
12302           tree arg01 = TREE_OPERAND (arg0, 1);
12303           if (TREE_CODE (arg00) == LSHIFT_EXPR
12304               && integer_onep (TREE_OPERAND (arg00, 0)))
12305             {
12306               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12307                                       arg01, TREE_OPERAND (arg00, 1));
12308               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12309                                  build_int_cst (TREE_TYPE (arg0), 1));
12310               return fold_build2_loc (loc, code, type,
12311                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12312                                   arg1);
12313             }
12314           else if (TREE_CODE (arg01) == LSHIFT_EXPR
12315                    && integer_onep (TREE_OPERAND (arg01, 0)))
12316             {
12317               tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12318                                       arg00, TREE_OPERAND (arg01, 1));
12319               tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12320                                  build_int_cst (TREE_TYPE (arg0), 1));
12321               return fold_build2_loc (loc, code, type,
12322                                   fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12323                                   arg1);
12324             }
12325         }
12326
12327       /* If this is an NE or EQ comparison of zero against the result of a
12328          signed MOD operation whose second operand is a power of 2, make
12329          the MOD operation unsigned since it is simpler and equivalent.  */
12330       if (integer_zerop (arg1)
12331           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12332           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12333               || TREE_CODE (arg0) == CEIL_MOD_EXPR
12334               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12335               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12336           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12337         {
12338           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12339           tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12340                                      fold_convert_loc (loc, newtype,
12341                                                        TREE_OPERAND (arg0, 0)),
12342                                      fold_convert_loc (loc, newtype,
12343                                                        TREE_OPERAND (arg0, 1)));
12344
12345           return fold_build2_loc (loc, code, type, newmod,
12346                               fold_convert_loc (loc, newtype, arg1));
12347         }
12348
12349       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12350          C1 is a valid shift constant, and C2 is a power of two, i.e.
12351          a single bit.  */
12352       if (TREE_CODE (arg0) == BIT_AND_EXPR
12353           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12354           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12355              == INTEGER_CST
12356           && integer_pow2p (TREE_OPERAND (arg0, 1))
12357           && integer_zerop (arg1))
12358         {
12359           tree itype = TREE_TYPE (arg0);
12360           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12361           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12362
12363           /* Check for a valid shift count.  */
12364           if (TREE_INT_CST_HIGH (arg001) == 0
12365               && TREE_INT_CST_LOW (arg001) < prec)
12366             {
12367               tree arg01 = TREE_OPERAND (arg0, 1);
12368               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12369               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12370               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12371                  can be rewritten as (X & (C2 << C1)) != 0.  */
12372               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12373                 {
12374                   tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12375                   tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12376                   return fold_build2_loc (loc, code, type, tem, arg1);
12377                 }
12378               /* Otherwise, for signed (arithmetic) shifts,
12379                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12380                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
12381               else if (!TYPE_UNSIGNED (itype))
12382                 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12383                                     arg000, build_int_cst (itype, 0));
12384               /* Otherwise, of unsigned (logical) shifts,
12385                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12386                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
12387               else
12388                 return omit_one_operand_loc (loc, type,
12389                                          code == EQ_EXPR ? integer_one_node
12390                                                          : integer_zero_node,
12391                                          arg000);
12392             }
12393         }
12394
12395       /* If this is an NE comparison of zero with an AND of one, remove the
12396          comparison since the AND will give the correct value.  */
12397       if (code == NE_EXPR
12398           && integer_zerop (arg1)
12399           && TREE_CODE (arg0) == BIT_AND_EXPR
12400           && integer_onep (TREE_OPERAND (arg0, 1)))
12401         return fold_convert_loc (loc, type, arg0);
12402
12403       /* If we have (A & C) == C where C is a power of 2, convert this into
12404          (A & C) != 0.  Similarly for NE_EXPR.  */
12405       if (TREE_CODE (arg0) == BIT_AND_EXPR
12406           && integer_pow2p (TREE_OPERAND (arg0, 1))
12407           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12408         return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12409                             arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12410                                                     integer_zero_node));
12411
12412       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12413          bit, then fold the expression into A < 0 or A >= 0.  */
12414       tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12415       if (tem)
12416         return tem;
12417
12418       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12419          Similarly for NE_EXPR.  */
12420       if (TREE_CODE (arg0) == BIT_AND_EXPR
12421           && TREE_CODE (arg1) == INTEGER_CST
12422           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12423         {
12424           tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12425                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
12426                                    TREE_OPERAND (arg0, 1));
12427           tree dandnotc = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12428                                        arg1, notc);
12429           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12430           if (integer_nonzerop (dandnotc))
12431             return omit_one_operand_loc (loc, type, rslt, arg0);
12432         }
12433
12434       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12435          Similarly for NE_EXPR.  */
12436       if (TREE_CODE (arg0) == BIT_IOR_EXPR
12437           && TREE_CODE (arg1) == INTEGER_CST
12438           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12439         {
12440           tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12441           tree candnotd = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12442                                        TREE_OPERAND (arg0, 1), notd);
12443           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12444           if (integer_nonzerop (candnotd))
12445             return omit_one_operand_loc (loc, type, rslt, arg0);
12446         }
12447
12448       /* If this is a comparison of a field, we may be able to simplify it.  */
12449       if ((TREE_CODE (arg0) == COMPONENT_REF
12450            || TREE_CODE (arg0) == BIT_FIELD_REF)
12451           /* Handle the constant case even without -O
12452              to make sure the warnings are given.  */
12453           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12454         {
12455           t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12456           if (t1)
12457             return t1;
12458         }
12459
12460       /* Optimize comparisons of strlen vs zero to a compare of the
12461          first character of the string vs zero.  To wit,
12462                 strlen(ptr) == 0   =>  *ptr == 0
12463                 strlen(ptr) != 0   =>  *ptr != 0
12464          Other cases should reduce to one of these two (or a constant)
12465          due to the return value of strlen being unsigned.  */
12466       if (TREE_CODE (arg0) == CALL_EXPR
12467           && integer_zerop (arg1))
12468         {
12469           tree fndecl = get_callee_fndecl (arg0);
12470
12471           if (fndecl
12472               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12473               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12474               && call_expr_nargs (arg0) == 1
12475               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12476             {
12477               tree iref = build_fold_indirect_ref_loc (loc,
12478                                                    CALL_EXPR_ARG (arg0, 0));
12479               return fold_build2_loc (loc, code, type, iref,
12480                                   build_int_cst (TREE_TYPE (iref), 0));
12481             }
12482         }
12483
12484       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12485          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12486       if (TREE_CODE (arg0) == RSHIFT_EXPR
12487           && integer_zerop (arg1)
12488           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12489         {
12490           tree arg00 = TREE_OPERAND (arg0, 0);
12491           tree arg01 = TREE_OPERAND (arg0, 1);
12492           tree itype = TREE_TYPE (arg00);
12493           if (TREE_INT_CST_HIGH (arg01) == 0
12494               && TREE_INT_CST_LOW (arg01)
12495                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12496             {
12497               if (TYPE_UNSIGNED (itype))
12498                 {
12499                   itype = signed_type_for (itype);
12500                   arg00 = fold_convert_loc (loc, itype, arg00);
12501                 }
12502               return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12503                                   type, arg00, build_int_cst (itype, 0));
12504             }
12505         }
12506
12507       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12508       if (integer_zerop (arg1)
12509           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12510         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12511                             TREE_OPERAND (arg0, 1));
12512
12513       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12514       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12515           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12516         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12517                             build_int_cst (TREE_TYPE (arg1), 0));
12518       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12519       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12520           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12521           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12522         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12523                             build_int_cst (TREE_TYPE (arg1), 0));
12524
12525       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12526       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12527           && TREE_CODE (arg1) == INTEGER_CST
12528           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12529         return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12530                             fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12531                                          TREE_OPERAND (arg0, 1), arg1));
12532
12533       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12534          (X & C) == 0 when C is a single bit.  */
12535       if (TREE_CODE (arg0) == BIT_AND_EXPR
12536           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12537           && integer_zerop (arg1)
12538           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12539         {
12540           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12541                              TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12542                              TREE_OPERAND (arg0, 1));
12543           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12544                               type, tem, arg1);
12545         }
12546
12547       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12548          constant C is a power of two, i.e. a single bit.  */
12549       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12550           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12551           && integer_zerop (arg1)
12552           && integer_pow2p (TREE_OPERAND (arg0, 1))
12553           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12554                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12555         {
12556           tree arg00 = TREE_OPERAND (arg0, 0);
12557           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12558                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
12559         }
12560
12561       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12562          when is C is a power of two, i.e. a single bit.  */
12563       if (TREE_CODE (arg0) == BIT_AND_EXPR
12564           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12565           && integer_zerop (arg1)
12566           && integer_pow2p (TREE_OPERAND (arg0, 1))
12567           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12568                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12569         {
12570           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12571           tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12572                              arg000, TREE_OPERAND (arg0, 1));
12573           return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12574                               tem, build_int_cst (TREE_TYPE (tem), 0));
12575         }
12576
12577       if (integer_zerop (arg1)
12578           && tree_expr_nonzero_p (arg0))
12579         {
12580           tree res = constant_boolean_node (code==NE_EXPR, type);
12581           return omit_one_operand_loc (loc, type, res, arg0);
12582         }
12583
12584       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
12585       if (TREE_CODE (arg0) == NEGATE_EXPR
12586           && TREE_CODE (arg1) == NEGATE_EXPR)
12587         return fold_build2_loc (loc, code, type,
12588                             TREE_OPERAND (arg0, 0),
12589                             TREE_OPERAND (arg1, 0));
12590
12591       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
12592       if (TREE_CODE (arg0) == BIT_AND_EXPR
12593           && TREE_CODE (arg1) == BIT_AND_EXPR)
12594         {
12595           tree arg00 = TREE_OPERAND (arg0, 0);
12596           tree arg01 = TREE_OPERAND (arg0, 1);
12597           tree arg10 = TREE_OPERAND (arg1, 0);
12598           tree arg11 = TREE_OPERAND (arg1, 1);
12599           tree itype = TREE_TYPE (arg0);
12600
12601           if (operand_equal_p (arg01, arg11, 0))
12602             return fold_build2_loc (loc, code, type,
12603                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12604                                              fold_build2_loc (loc,
12605                                                           BIT_XOR_EXPR, itype,
12606                                                           arg00, arg10),
12607                                              arg01),
12608                                 build_int_cst (itype, 0));
12609
12610           if (operand_equal_p (arg01, arg10, 0))
12611             return fold_build2_loc (loc, code, type,
12612                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12613                                              fold_build2_loc (loc,
12614                                                           BIT_XOR_EXPR, itype,
12615                                                           arg00, arg11),
12616                                              arg01),
12617                                 build_int_cst (itype, 0));
12618
12619           if (operand_equal_p (arg00, arg11, 0))
12620             return fold_build2_loc (loc, code, type,
12621                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12622                                              fold_build2_loc (loc,
12623                                                           BIT_XOR_EXPR, itype,
12624                                                           arg01, arg10),
12625                                              arg00),
12626                                 build_int_cst (itype, 0));
12627
12628           if (operand_equal_p (arg00, arg10, 0))
12629             return fold_build2_loc (loc, code, type,
12630                                 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12631                                              fold_build2_loc (loc,
12632                                                           BIT_XOR_EXPR, itype,
12633                                                           arg01, arg11),
12634                                              arg00),
12635                                 build_int_cst (itype, 0));
12636         }
12637
12638       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12639           && TREE_CODE (arg1) == BIT_XOR_EXPR)
12640         {
12641           tree arg00 = TREE_OPERAND (arg0, 0);
12642           tree arg01 = TREE_OPERAND (arg0, 1);
12643           tree arg10 = TREE_OPERAND (arg1, 0);
12644           tree arg11 = TREE_OPERAND (arg1, 1);
12645           tree itype = TREE_TYPE (arg0);
12646
12647           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12648              operand_equal_p guarantees no side-effects so we don't need
12649              to use omit_one_operand on Z.  */
12650           if (operand_equal_p (arg01, arg11, 0))
12651             return fold_build2_loc (loc, code, type, arg00, arg10);
12652           if (operand_equal_p (arg01, arg10, 0))
12653             return fold_build2_loc (loc, code, type, arg00, arg11);
12654           if (operand_equal_p (arg00, arg11, 0))
12655             return fold_build2_loc (loc, code, type, arg01, arg10);
12656           if (operand_equal_p (arg00, arg10, 0))
12657             return fold_build2_loc (loc, code, type, arg01, arg11);
12658
12659           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
12660           if (TREE_CODE (arg01) == INTEGER_CST
12661               && TREE_CODE (arg11) == INTEGER_CST)
12662             return fold_build2_loc (loc, code, type,
12663                                 fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00,
12664                                              fold_build2_loc (loc,
12665                                                           BIT_XOR_EXPR, itype,
12666                                                           arg01, arg11)),
12667                                 arg10);
12668         }
12669
12670       /* Attempt to simplify equality/inequality comparisons of complex
12671          values.  Only lower the comparison if the result is known or
12672          can be simplified to a single scalar comparison.  */
12673       if ((TREE_CODE (arg0) == COMPLEX_EXPR
12674            || TREE_CODE (arg0) == COMPLEX_CST)
12675           && (TREE_CODE (arg1) == COMPLEX_EXPR
12676               || TREE_CODE (arg1) == COMPLEX_CST))
12677         {
12678           tree real0, imag0, real1, imag1;
12679           tree rcond, icond;
12680
12681           if (TREE_CODE (arg0) == COMPLEX_EXPR)
12682             {
12683               real0 = TREE_OPERAND (arg0, 0);
12684               imag0 = TREE_OPERAND (arg0, 1);
12685             }
12686           else
12687             {
12688               real0 = TREE_REALPART (arg0);
12689               imag0 = TREE_IMAGPART (arg0);
12690             }
12691
12692           if (TREE_CODE (arg1) == COMPLEX_EXPR)
12693             {
12694               real1 = TREE_OPERAND (arg1, 0);
12695               imag1 = TREE_OPERAND (arg1, 1);
12696             }
12697           else
12698             {
12699               real1 = TREE_REALPART (arg1);
12700               imag1 = TREE_IMAGPART (arg1);
12701             }
12702
12703           rcond = fold_binary_loc (loc, code, type, real0, real1);
12704           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12705             {
12706               if (integer_zerop (rcond))
12707                 {
12708                   if (code == EQ_EXPR)
12709                     return omit_two_operands_loc (loc, type, boolean_false_node,
12710                                               imag0, imag1);
12711                   return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12712                 }
12713               else
12714                 {
12715                   if (code == NE_EXPR)
12716                     return omit_two_operands_loc (loc, type, boolean_true_node,
12717                                               imag0, imag1);
12718                   return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12719                 }
12720             }
12721
12722           icond = fold_binary_loc (loc, code, type, imag0, imag1);
12723           if (icond && TREE_CODE (icond) == INTEGER_CST)
12724             {
12725               if (integer_zerop (icond))
12726                 {
12727                   if (code == EQ_EXPR)
12728                     return omit_two_operands_loc (loc, type, boolean_false_node,
12729                                               real0, real1);
12730                   return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12731                 }
12732               else
12733                 {
12734                   if (code == NE_EXPR)
12735                     return omit_two_operands_loc (loc, type, boolean_true_node,
12736                                               real0, real1);
12737                   return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12738                 }
12739             }
12740         }
12741
12742       return NULL_TREE;
12743
12744     case LT_EXPR:
12745     case GT_EXPR:
12746     case LE_EXPR:
12747     case GE_EXPR:
12748       tem = fold_comparison (loc, code, type, op0, op1);
12749       if (tem != NULL_TREE)
12750         return tem;
12751
12752       /* Transform comparisons of the form X +- C CMP X.  */
12753       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12754           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12755           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12756                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12757               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12758                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12759         {
12760           tree arg01 = TREE_OPERAND (arg0, 1);
12761           enum tree_code code0 = TREE_CODE (arg0);
12762           int is_positive;
12763
12764           if (TREE_CODE (arg01) == REAL_CST)
12765             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12766           else
12767             is_positive = tree_int_cst_sgn (arg01);
12768
12769           /* (X - c) > X becomes false.  */
12770           if (code == GT_EXPR
12771               && ((code0 == MINUS_EXPR && is_positive >= 0)
12772                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12773             {
12774               if (TREE_CODE (arg01) == INTEGER_CST
12775                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12776                 fold_overflow_warning (("assuming signed overflow does not "
12777                                         "occur when assuming that (X - c) > X "
12778                                         "is always false"),
12779                                        WARN_STRICT_OVERFLOW_ALL);
12780               return constant_boolean_node (0, type);
12781             }
12782
12783           /* Likewise (X + c) < X becomes false.  */
12784           if (code == LT_EXPR
12785               && ((code0 == PLUS_EXPR && is_positive >= 0)
12786                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12787             {
12788               if (TREE_CODE (arg01) == INTEGER_CST
12789                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12790                 fold_overflow_warning (("assuming signed overflow does not "
12791                                         "occur when assuming that "
12792                                         "(X + c) < X is always false"),
12793                                        WARN_STRICT_OVERFLOW_ALL);
12794               return constant_boolean_node (0, type);
12795             }
12796
12797           /* Convert (X - c) <= X to true.  */
12798           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12799               && code == LE_EXPR
12800               && ((code0 == MINUS_EXPR && is_positive >= 0)
12801                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12802             {
12803               if (TREE_CODE (arg01) == INTEGER_CST
12804                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12805                 fold_overflow_warning (("assuming signed overflow does not "
12806                                         "occur when assuming that "
12807                                         "(X - c) <= X is always true"),
12808                                        WARN_STRICT_OVERFLOW_ALL);
12809               return constant_boolean_node (1, type);
12810             }
12811
12812           /* Convert (X + c) >= X to true.  */
12813           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12814               && code == GE_EXPR
12815               && ((code0 == PLUS_EXPR && is_positive >= 0)
12816                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12817             {
12818               if (TREE_CODE (arg01) == INTEGER_CST
12819                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12820                 fold_overflow_warning (("assuming signed overflow does not "
12821                                         "occur when assuming that "
12822                                         "(X + c) >= X is always true"),
12823                                        WARN_STRICT_OVERFLOW_ALL);
12824               return constant_boolean_node (1, type);
12825             }
12826
12827           if (TREE_CODE (arg01) == INTEGER_CST)
12828             {
12829               /* Convert X + c > X and X - c < X to true for integers.  */
12830               if (code == GT_EXPR
12831                   && ((code0 == PLUS_EXPR && is_positive > 0)
12832                       || (code0 == MINUS_EXPR && is_positive < 0)))
12833                 {
12834                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12835                     fold_overflow_warning (("assuming signed overflow does "
12836                                             "not occur when assuming that "
12837                                             "(X + c) > X is always true"),
12838                                            WARN_STRICT_OVERFLOW_ALL);
12839                   return constant_boolean_node (1, type);
12840                 }
12841
12842               if (code == LT_EXPR
12843                   && ((code0 == MINUS_EXPR && is_positive > 0)
12844                       || (code0 == PLUS_EXPR && is_positive < 0)))
12845                 {
12846                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12847                     fold_overflow_warning (("assuming signed overflow does "
12848                                             "not occur when assuming that "
12849                                             "(X - c) < X is always true"),
12850                                            WARN_STRICT_OVERFLOW_ALL);
12851                   return constant_boolean_node (1, type);
12852                 }
12853
12854               /* Convert X + c <= X and X - c >= X to false for integers.  */
12855               if (code == LE_EXPR
12856                   && ((code0 == PLUS_EXPR && is_positive > 0)
12857                       || (code0 == MINUS_EXPR && is_positive < 0)))
12858                 {
12859                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12860                     fold_overflow_warning (("assuming signed overflow does "
12861                                             "not occur when assuming that "
12862                                             "(X + c) <= X is always false"),
12863                                            WARN_STRICT_OVERFLOW_ALL);
12864                   return constant_boolean_node (0, type);
12865                 }
12866
12867               if (code == GE_EXPR
12868                   && ((code0 == MINUS_EXPR && is_positive > 0)
12869                       || (code0 == PLUS_EXPR && is_positive < 0)))
12870                 {
12871                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12872                     fold_overflow_warning (("assuming signed overflow does "
12873                                             "not occur when assuming that "
12874                                             "(X - c) >= X is always false"),
12875                                            WARN_STRICT_OVERFLOW_ALL);
12876                   return constant_boolean_node (0, type);
12877                 }
12878             }
12879         }
12880
12881       /* Comparisons with the highest or lowest possible integer of
12882          the specified precision will have known values.  */
12883       {
12884         tree arg1_type = TREE_TYPE (arg1);
12885         unsigned int width = TYPE_PRECISION (arg1_type);
12886
12887         if (TREE_CODE (arg1) == INTEGER_CST
12888             && width <= 2 * HOST_BITS_PER_WIDE_INT
12889             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12890           {
12891             HOST_WIDE_INT signed_max_hi;
12892             unsigned HOST_WIDE_INT signed_max_lo;
12893             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12894
12895             if (width <= HOST_BITS_PER_WIDE_INT)
12896               {
12897                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12898                                 - 1;
12899                 signed_max_hi = 0;
12900                 max_hi = 0;
12901
12902                 if (TYPE_UNSIGNED (arg1_type))
12903                   {
12904                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12905                     min_lo = 0;
12906                     min_hi = 0;
12907                   }
12908                 else
12909                   {
12910                     max_lo = signed_max_lo;
12911                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12912                     min_hi = -1;
12913                   }
12914               }
12915             else
12916               {
12917                 width -= HOST_BITS_PER_WIDE_INT;
12918                 signed_max_lo = -1;
12919                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12920                                 - 1;
12921                 max_lo = -1;
12922                 min_lo = 0;
12923
12924                 if (TYPE_UNSIGNED (arg1_type))
12925                   {
12926                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12927                     min_hi = 0;
12928                   }
12929                 else
12930                   {
12931                     max_hi = signed_max_hi;
12932                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12933                   }
12934               }
12935
12936             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12937                 && TREE_INT_CST_LOW (arg1) == max_lo)
12938               switch (code)
12939                 {
12940                 case GT_EXPR:
12941                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12942
12943                 case GE_EXPR:
12944                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12945
12946                 case LE_EXPR:
12947                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12948
12949                 case LT_EXPR:
12950                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12951
12952                 /* The GE_EXPR and LT_EXPR cases above are not normally
12953                    reached because of previous transformations.  */
12954
12955                 default:
12956                   break;
12957                 }
12958             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12959                      == max_hi
12960                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12961               switch (code)
12962                 {
12963                 case GT_EXPR:
12964                   arg1 = const_binop (PLUS_EXPR, arg1,
12965                                       build_int_cst (TREE_TYPE (arg1), 1));
12966                   return fold_build2_loc (loc, EQ_EXPR, type,
12967                                       fold_convert_loc (loc,
12968                                                         TREE_TYPE (arg1), arg0),
12969                                       arg1);
12970                 case LE_EXPR:
12971                   arg1 = const_binop (PLUS_EXPR, arg1,
12972                                       build_int_cst (TREE_TYPE (arg1), 1));
12973                   return fold_build2_loc (loc, NE_EXPR, type,
12974                                       fold_convert_loc (loc, TREE_TYPE (arg1),
12975                                                         arg0),
12976                                       arg1);
12977                 default:
12978                   break;
12979                 }
12980             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12981                      == min_hi
12982                      && TREE_INT_CST_LOW (arg1) == min_lo)
12983               switch (code)
12984                 {
12985                 case LT_EXPR:
12986                   return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12987
12988                 case LE_EXPR:
12989                   return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12990
12991                 case GE_EXPR:
12992                   return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12993
12994                 case GT_EXPR:
12995                   return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12996
12997                 default:
12998                   break;
12999                 }
13000             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13001                      == min_hi
13002                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
13003               switch (code)
13004                 {
13005                 case GE_EXPR:
13006                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
13007                   return fold_build2_loc (loc, NE_EXPR, type,
13008                                       fold_convert_loc (loc,
13009                                                         TREE_TYPE (arg1), arg0),
13010                                       arg1);
13011                 case LT_EXPR:
13012                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
13013                   return fold_build2_loc (loc, EQ_EXPR, type,
13014                                       fold_convert_loc (loc, TREE_TYPE (arg1),
13015                                                         arg0),
13016                                       arg1);
13017                 default:
13018                   break;
13019                 }
13020
13021             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13022                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
13023                      && TYPE_UNSIGNED (arg1_type)
13024                      /* We will flip the signedness of the comparison operator
13025                         associated with the mode of arg1, so the sign bit is
13026                         specified by this mode.  Check that arg1 is the signed
13027                         max associated with this sign bit.  */
13028                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13029                      /* signed_type does not work on pointer types.  */
13030                      && INTEGRAL_TYPE_P (arg1_type))
13031               {
13032                 /* The following case also applies to X < signed_max+1
13033                    and X >= signed_max+1 because previous transformations.  */
13034                 if (code == LE_EXPR || code == GT_EXPR)
13035                   {
13036                     tree st;
13037                     st = signed_type_for (TREE_TYPE (arg1));
13038                     return fold_build2_loc (loc,
13039                                         code == LE_EXPR ? GE_EXPR : LT_EXPR,
13040                                         type, fold_convert_loc (loc, st, arg0),
13041                                         build_int_cst (st, 0));
13042                   }
13043               }
13044           }
13045       }
13046
13047       /* If we are comparing an ABS_EXPR with a constant, we can
13048          convert all the cases into explicit comparisons, but they may
13049          well not be faster than doing the ABS and one comparison.
13050          But ABS (X) <= C is a range comparison, which becomes a subtraction
13051          and a comparison, and is probably faster.  */
13052       if (code == LE_EXPR
13053           && TREE_CODE (arg1) == INTEGER_CST
13054           && TREE_CODE (arg0) == ABS_EXPR
13055           && ! TREE_SIDE_EFFECTS (arg0)
13056           && (0 != (tem = negate_expr (arg1)))
13057           && TREE_CODE (tem) == INTEGER_CST
13058           && !TREE_OVERFLOW (tem))
13059         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13060                             build2 (GE_EXPR, type,
13061                                     TREE_OPERAND (arg0, 0), tem),
13062                             build2 (LE_EXPR, type,
13063                                     TREE_OPERAND (arg0, 0), arg1));
13064
13065       /* Convert ABS_EXPR<x> >= 0 to true.  */
13066       strict_overflow_p = false;
13067       if (code == GE_EXPR
13068           && (integer_zerop (arg1)
13069               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13070                   && real_zerop (arg1)))
13071           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13072         {
13073           if (strict_overflow_p)
13074             fold_overflow_warning (("assuming signed overflow does not occur "
13075                                     "when simplifying comparison of "
13076                                     "absolute value and zero"),
13077                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13078           return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13079         }
13080
13081       /* Convert ABS_EXPR<x> < 0 to false.  */
13082       strict_overflow_p = false;
13083       if (code == LT_EXPR
13084           && (integer_zerop (arg1) || real_zerop (arg1))
13085           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13086         {
13087           if (strict_overflow_p)
13088             fold_overflow_warning (("assuming signed overflow does not occur "
13089                                     "when simplifying comparison of "
13090                                     "absolute value and zero"),
13091                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13092           return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13093         }
13094
13095       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13096          and similarly for >= into !=.  */
13097       if ((code == LT_EXPR || code == GE_EXPR)
13098           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13099           && TREE_CODE (arg1) == LSHIFT_EXPR
13100           && integer_onep (TREE_OPERAND (arg1, 0)))
13101         {
13102           tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13103                         build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13104                                 TREE_OPERAND (arg1, 1)),
13105                         build_int_cst (TREE_TYPE (arg0), 0));
13106           goto fold_binary_exit;
13107         }
13108
13109       if ((code == LT_EXPR || code == GE_EXPR)
13110           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13111           && CONVERT_EXPR_P (arg1)
13112           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13113           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13114         {
13115           tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13116                         fold_convert_loc (loc, TREE_TYPE (arg0),
13117                                           build2 (RSHIFT_EXPR,
13118                                                   TREE_TYPE (arg0), arg0,
13119                                                   TREE_OPERAND (TREE_OPERAND (arg1, 0),
13120                                                                 1))),
13121                         build_int_cst (TREE_TYPE (arg0), 0));
13122           goto fold_binary_exit;
13123         }
13124
13125       return NULL_TREE;
13126
13127     case UNORDERED_EXPR:
13128     case ORDERED_EXPR:
13129     case UNLT_EXPR:
13130     case UNLE_EXPR:
13131     case UNGT_EXPR:
13132     case UNGE_EXPR:
13133     case UNEQ_EXPR:
13134     case LTGT_EXPR:
13135       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13136         {
13137           t1 = fold_relational_const (code, type, arg0, arg1);
13138           if (t1 != NULL_TREE)
13139             return t1;
13140         }
13141
13142       /* If the first operand is NaN, the result is constant.  */
13143       if (TREE_CODE (arg0) == REAL_CST
13144           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13145           && (code != LTGT_EXPR || ! flag_trapping_math))
13146         {
13147           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13148                ? integer_zero_node
13149                : integer_one_node;
13150           return omit_one_operand_loc (loc, type, t1, arg1);
13151         }
13152
13153       /* If the second operand is NaN, the result is constant.  */
13154       if (TREE_CODE (arg1) == REAL_CST
13155           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13156           && (code != LTGT_EXPR || ! flag_trapping_math))
13157         {
13158           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13159                ? integer_zero_node
13160                : integer_one_node;
13161           return omit_one_operand_loc (loc, type, t1, arg0);
13162         }
13163
13164       /* Simplify unordered comparison of something with itself.  */
13165       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13166           && operand_equal_p (arg0, arg1, 0))
13167         return constant_boolean_node (1, type);
13168
13169       if (code == LTGT_EXPR
13170           && !flag_trapping_math
13171           && operand_equal_p (arg0, arg1, 0))
13172         return constant_boolean_node (0, type);
13173
13174       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
13175       {
13176         tree targ0 = strip_float_extensions (arg0);
13177         tree targ1 = strip_float_extensions (arg1);
13178         tree newtype = TREE_TYPE (targ0);
13179
13180         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13181           newtype = TREE_TYPE (targ1);
13182
13183         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13184           return fold_build2_loc (loc, code, type,
13185                               fold_convert_loc (loc, newtype, targ0),
13186                               fold_convert_loc (loc, newtype, targ1));
13187       }
13188
13189       return NULL_TREE;
13190
13191     case COMPOUND_EXPR:
13192       /* When pedantic, a compound expression can be neither an lvalue
13193          nor an integer constant expression.  */
13194       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13195         return NULL_TREE;
13196       /* Don't let (0, 0) be null pointer constant.  */
13197       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13198                                  : fold_convert_loc (loc, type, arg1);
13199       return pedantic_non_lvalue_loc (loc, tem);
13200
13201     case COMPLEX_EXPR:
13202       if ((TREE_CODE (arg0) == REAL_CST
13203            && TREE_CODE (arg1) == REAL_CST)
13204           || (TREE_CODE (arg0) == INTEGER_CST
13205               && TREE_CODE (arg1) == INTEGER_CST))
13206         return build_complex (type, arg0, arg1);
13207       return NULL_TREE;
13208
13209     case ASSERT_EXPR:
13210       /* An ASSERT_EXPR should never be passed to fold_binary.  */
13211       gcc_unreachable ();
13212
13213     default:
13214       return NULL_TREE;
13215     } /* switch (code) */
13216  fold_binary_exit:
13217   protected_set_expr_location (tem, loc);
13218   return tem;
13219 }
13220
13221 /* Callback for walk_tree, looking for LABEL_EXPR.  Return *TP if it is
13222    a LABEL_EXPR; otherwise return NULL_TREE.  Do not check the subtrees
13223    of GOTO_EXPR.  */
13224
13225 static tree
13226 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13227 {
13228   switch (TREE_CODE (*tp))
13229     {
13230     case LABEL_EXPR:
13231       return *tp;
13232
13233     case GOTO_EXPR:
13234       *walk_subtrees = 0;
13235
13236       /* ... fall through ...  */
13237
13238     default:
13239       return NULL_TREE;
13240     }
13241 }
13242
13243 /* Return whether the sub-tree ST contains a label which is accessible from
13244    outside the sub-tree.  */
13245
13246 static bool
13247 contains_label_p (tree st)
13248 {
13249   return
13250    (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13251 }
13252
13253 /* Fold a ternary expression of code CODE and type TYPE with operands
13254    OP0, OP1, and OP2.  Return the folded expression if folding is
13255    successful.  Otherwise, return NULL_TREE.  */
13256
13257 tree
13258 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13259               tree op0, tree op1, tree op2)
13260 {
13261   tree tem;
13262   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13263   enum tree_code_class kind = TREE_CODE_CLASS (code);
13264
13265   gcc_assert (IS_EXPR_CODE_CLASS (kind)
13266               && TREE_CODE_LENGTH (code) == 3);
13267
13268   /* Strip any conversions that don't change the mode.  This is safe
13269      for every expression, except for a comparison expression because
13270      its signedness is derived from its operands.  So, in the latter
13271      case, only strip conversions that don't change the signedness.
13272
13273      Note that this is done as an internal manipulation within the
13274      constant folder, in order to find the simplest representation of
13275      the arguments so that their form can be studied.  In any cases,
13276      the appropriate type conversions should be put back in the tree
13277      that will get out of the constant folder.  */
13278   if (op0)
13279     {
13280       arg0 = op0;
13281       STRIP_NOPS (arg0);
13282     }
13283
13284   if (op1)
13285     {
13286       arg1 = op1;
13287       STRIP_NOPS (arg1);
13288     }
13289
13290   switch (code)
13291     {
13292     case COMPONENT_REF:
13293       if (TREE_CODE (arg0) == CONSTRUCTOR
13294           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13295         {
13296           unsigned HOST_WIDE_INT idx;
13297           tree field, value;
13298           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13299             if (field == arg1)
13300               return value;
13301         }
13302       return NULL_TREE;
13303
13304     case COND_EXPR:
13305       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13306          so all simple results must be passed through pedantic_non_lvalue.  */
13307       if (TREE_CODE (arg0) == INTEGER_CST)
13308         {
13309           tree unused_op = integer_zerop (arg0) ? op1 : op2;
13310           tem = integer_zerop (arg0) ? op2 : op1;
13311           /* Only optimize constant conditions when the selected branch
13312              has the same type as the COND_EXPR.  This avoids optimizing
13313              away "c ? x : throw", where the throw has a void type.
13314              Avoid throwing away that operand which contains label.  */
13315           if ((!TREE_SIDE_EFFECTS (unused_op)
13316                || !contains_label_p (unused_op))
13317               && (! VOID_TYPE_P (TREE_TYPE (tem))
13318                   || VOID_TYPE_P (type)))
13319             return pedantic_non_lvalue_loc (loc, tem);
13320           return NULL_TREE;
13321         }
13322       if (operand_equal_p (arg1, op2, 0))
13323         return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13324
13325       /* If we have A op B ? A : C, we may be able to convert this to a
13326          simpler expression, depending on the operation and the values
13327          of B and C.  Signed zeros prevent all of these transformations,
13328          for reasons given above each one.
13329
13330          Also try swapping the arguments and inverting the conditional.  */
13331       if (COMPARISON_CLASS_P (arg0)
13332           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13333                                              arg1, TREE_OPERAND (arg0, 1))
13334           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13335         {
13336           tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13337           if (tem)
13338             return tem;
13339         }
13340
13341       if (COMPARISON_CLASS_P (arg0)
13342           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13343                                              op2,
13344                                              TREE_OPERAND (arg0, 1))
13345           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13346         {
13347           tem = fold_truth_not_expr (loc, arg0);
13348           if (tem && COMPARISON_CLASS_P (tem))
13349             {
13350               tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13351               if (tem)
13352                 return tem;
13353             }
13354         }
13355
13356       /* If the second operand is simpler than the third, swap them
13357          since that produces better jump optimization results.  */
13358       if (truth_value_p (TREE_CODE (arg0))
13359           && tree_swap_operands_p (op1, op2, false))
13360         {
13361           /* See if this can be inverted.  If it can't, possibly because
13362              it was a floating-point inequality comparison, don't do
13363              anything.  */
13364           tem = fold_truth_not_expr (loc, arg0);
13365           if (tem)
13366             return fold_build3_loc (loc, code, type, tem, op2, op1);
13367         }
13368
13369       /* Convert A ? 1 : 0 to simply A.  */
13370       if (integer_onep (op1)
13371           && integer_zerop (op2)
13372           /* If we try to convert OP0 to our type, the
13373              call to fold will try to move the conversion inside
13374              a COND, which will recurse.  In that case, the COND_EXPR
13375              is probably the best choice, so leave it alone.  */
13376           && type == TREE_TYPE (arg0))
13377         return pedantic_non_lvalue_loc (loc, arg0);
13378
13379       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
13380          over COND_EXPR in cases such as floating point comparisons.  */
13381       if (integer_zerop (op1)
13382           && integer_onep (op2)
13383           && truth_value_p (TREE_CODE (arg0)))
13384         return pedantic_non_lvalue_loc (loc,
13385                                     fold_convert_loc (loc, type,
13386                                               invert_truthvalue_loc (loc,
13387                                                                      arg0)));
13388
13389       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
13390       if (TREE_CODE (arg0) == LT_EXPR
13391           && integer_zerop (TREE_OPERAND (arg0, 1))
13392           && integer_zerop (op2)
13393           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13394         {
13395           /* sign_bit_p only checks ARG1 bits within A's precision.
13396              If <sign bit of A> has wider type than A, bits outside
13397              of A's precision in <sign bit of A> need to be checked.
13398              If they are all 0, this optimization needs to be done
13399              in unsigned A's type, if they are all 1 in signed A's type,
13400              otherwise this can't be done.  */
13401           if (TYPE_PRECISION (TREE_TYPE (tem))
13402               < TYPE_PRECISION (TREE_TYPE (arg1))
13403               && TYPE_PRECISION (TREE_TYPE (tem))
13404                  < TYPE_PRECISION (type))
13405             {
13406               unsigned HOST_WIDE_INT mask_lo;
13407               HOST_WIDE_INT mask_hi;
13408               int inner_width, outer_width;
13409               tree tem_type;
13410
13411               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13412               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13413               if (outer_width > TYPE_PRECISION (type))
13414                 outer_width = TYPE_PRECISION (type);
13415
13416               if (outer_width > HOST_BITS_PER_WIDE_INT)
13417                 {
13418                   mask_hi = ((unsigned HOST_WIDE_INT) -1
13419                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13420                   mask_lo = -1;
13421                 }
13422               else
13423                 {
13424                   mask_hi = 0;
13425                   mask_lo = ((unsigned HOST_WIDE_INT) -1
13426                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
13427                 }
13428               if (inner_width > HOST_BITS_PER_WIDE_INT)
13429                 {
13430                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13431                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
13432                   mask_lo = 0;
13433                 }
13434               else
13435                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13436                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
13437
13438               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13439                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13440                 {
13441                   tem_type = signed_type_for (TREE_TYPE (tem));
13442                   tem = fold_convert_loc (loc, tem_type, tem);
13443                 }
13444               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13445                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13446                 {
13447                   tem_type = unsigned_type_for (TREE_TYPE (tem));
13448                   tem = fold_convert_loc (loc, tem_type, tem);
13449                 }
13450               else
13451                 tem = NULL;
13452             }
13453
13454           if (tem)
13455             return
13456               fold_convert_loc (loc, type,
13457                                 fold_build2_loc (loc, BIT_AND_EXPR,
13458                                              TREE_TYPE (tem), tem,
13459                                              fold_convert_loc (loc,
13460                                                                TREE_TYPE (tem),
13461                                                                arg1)));
13462         }
13463
13464       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
13465          already handled above.  */
13466       if (TREE_CODE (arg0) == BIT_AND_EXPR
13467           && integer_onep (TREE_OPERAND (arg0, 1))
13468           && integer_zerop (op2)
13469           && integer_pow2p (arg1))
13470         {
13471           tree tem = TREE_OPERAND (arg0, 0);
13472           STRIP_NOPS (tem);
13473           if (TREE_CODE (tem) == RSHIFT_EXPR
13474               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13475               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13476                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13477             return fold_build2_loc (loc, BIT_AND_EXPR, type,
13478                                 TREE_OPERAND (tem, 0), arg1);
13479         }
13480
13481       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13482          is probably obsolete because the first operand should be a
13483          truth value (that's why we have the two cases above), but let's
13484          leave it in until we can confirm this for all front-ends.  */
13485       if (integer_zerop (op2)
13486           && TREE_CODE (arg0) == NE_EXPR
13487           && integer_zerop (TREE_OPERAND (arg0, 1))
13488           && integer_pow2p (arg1)
13489           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13490           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13491                               arg1, OEP_ONLY_CONST))
13492         return pedantic_non_lvalue_loc (loc,
13493                                     fold_convert_loc (loc, type,
13494                                                       TREE_OPERAND (arg0, 0)));
13495
13496       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13497       if (integer_zerop (op2)
13498           && truth_value_p (TREE_CODE (arg0))
13499           && truth_value_p (TREE_CODE (arg1)))
13500         return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13501                             fold_convert_loc (loc, type, arg0),
13502                             arg1);
13503
13504       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13505       if (integer_onep (op2)
13506           && truth_value_p (TREE_CODE (arg0))
13507           && truth_value_p (TREE_CODE (arg1)))
13508         {
13509           /* Only perform transformation if ARG0 is easily inverted.  */
13510           tem = fold_truth_not_expr (loc, arg0);
13511           if (tem)
13512             return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13513                                 fold_convert_loc (loc, type, tem),
13514                                 arg1);
13515         }
13516
13517       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13518       if (integer_zerop (arg1)
13519           && truth_value_p (TREE_CODE (arg0))
13520           && truth_value_p (TREE_CODE (op2)))
13521         {
13522           /* Only perform transformation if ARG0 is easily inverted.  */
13523           tem = fold_truth_not_expr (loc, arg0);
13524           if (tem)
13525             return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13526                                 fold_convert_loc (loc, type, tem),
13527                                 op2);
13528         }
13529
13530       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13531       if (integer_onep (arg1)
13532           && truth_value_p (TREE_CODE (arg0))
13533           && truth_value_p (TREE_CODE (op2)))
13534         return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13535                             fold_convert_loc (loc, type, arg0),
13536                             op2);
13537
13538       return NULL_TREE;
13539
13540     case CALL_EXPR:
13541       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
13542          of fold_ternary on them.  */
13543       gcc_unreachable ();
13544
13545     case BIT_FIELD_REF:
13546       if ((TREE_CODE (arg0) == VECTOR_CST
13547            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13548           && type == TREE_TYPE (TREE_TYPE (arg0)))
13549         {
13550           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13551           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13552
13553           if (width != 0
13554               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13555               && (idx % width) == 0
13556               && (idx = idx / width)
13557                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13558             {
13559               tree elements = NULL_TREE;
13560
13561               if (TREE_CODE (arg0) == VECTOR_CST)
13562                 elements = TREE_VECTOR_CST_ELTS (arg0);
13563               else
13564                 {
13565                   unsigned HOST_WIDE_INT idx;
13566                   tree value;
13567
13568                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13569                     elements = tree_cons (NULL_TREE, value, elements);
13570                 }
13571               while (idx-- > 0 && elements)
13572                 elements = TREE_CHAIN (elements);
13573               if (elements)
13574                 return TREE_VALUE (elements);
13575               else
13576                 return fold_convert_loc (loc, type, integer_zero_node);
13577             }
13578         }
13579
13580       /* A bit-field-ref that referenced the full argument can be stripped.  */
13581       if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13582           && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13583           && integer_zerop (op2))
13584         return fold_convert_loc (loc, type, arg0);
13585
13586       return NULL_TREE;
13587
13588     default:
13589       return NULL_TREE;
13590     } /* switch (code) */
13591 }
13592
13593 /* Perform constant folding and related simplification of EXPR.
13594    The related simplifications include x*1 => x, x*0 => 0, etc.,
13595    and application of the associative law.
13596    NOP_EXPR conversions may be removed freely (as long as we
13597    are careful not to change the type of the overall expression).
13598    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13599    but we can constant-fold them if they have constant operands.  */
13600
13601 #ifdef ENABLE_FOLD_CHECKING
13602 # define fold(x) fold_1 (x)
13603 static tree fold_1 (tree);
13604 static
13605 #endif
13606 tree
13607 fold (tree expr)
13608 {
13609   const tree t = expr;
13610   enum tree_code code = TREE_CODE (t);
13611   enum tree_code_class kind = TREE_CODE_CLASS (code);
13612   tree tem;
13613   location_t loc = EXPR_LOCATION (expr);
13614
13615   /* Return right away if a constant.  */
13616   if (kind == tcc_constant)
13617     return t;
13618
13619   /* CALL_EXPR-like objects with variable numbers of operands are
13620      treated specially.  */
13621   if (kind == tcc_vl_exp)
13622     {
13623       if (code == CALL_EXPR)
13624         {
13625           tem = fold_call_expr (loc, expr, false);
13626           return tem ? tem : expr;
13627         }
13628       return expr;
13629     }
13630
13631   if (IS_EXPR_CODE_CLASS (kind))
13632     {
13633       tree type = TREE_TYPE (t);
13634       tree op0, op1, op2;
13635
13636       switch (TREE_CODE_LENGTH (code))
13637         {
13638         case 1:
13639           op0 = TREE_OPERAND (t, 0);
13640           tem = fold_unary_loc (loc, code, type, op0);
13641           return tem ? tem : expr;
13642         case 2:
13643           op0 = TREE_OPERAND (t, 0);
13644           op1 = TREE_OPERAND (t, 1);
13645           tem = fold_binary_loc (loc, code, type, op0, op1);
13646           return tem ? tem : expr;
13647         case 3:
13648           op0 = TREE_OPERAND (t, 0);
13649           op1 = TREE_OPERAND (t, 1);
13650           op2 = TREE_OPERAND (t, 2);
13651           tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13652           return tem ? tem : expr;
13653         default:
13654           break;
13655         }
13656     }
13657
13658   switch (code)
13659     {
13660     case ARRAY_REF:
13661       {
13662         tree op0 = TREE_OPERAND (t, 0);
13663         tree op1 = TREE_OPERAND (t, 1);
13664
13665         if (TREE_CODE (op1) == INTEGER_CST
13666             && TREE_CODE (op0) == CONSTRUCTOR
13667             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13668           {
13669             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13670             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13671             unsigned HOST_WIDE_INT begin = 0;
13672
13673             /* Find a matching index by means of a binary search.  */
13674             while (begin != end)
13675               {
13676                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13677                 tree index = VEC_index (constructor_elt, elts, middle)->index;
13678
13679                 if (TREE_CODE (index) == INTEGER_CST
13680                     && tree_int_cst_lt (index, op1))
13681                   begin = middle + 1;
13682                 else if (TREE_CODE (index) == INTEGER_CST
13683                          && tree_int_cst_lt (op1, index))
13684                   end = middle;
13685                 else if (TREE_CODE (index) == RANGE_EXPR
13686                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13687                   begin = middle + 1;
13688                 else if (TREE_CODE (index) == RANGE_EXPR
13689                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13690                   end = middle;
13691                 else
13692                   return VEC_index (constructor_elt, elts, middle)->value;
13693               }
13694           }
13695
13696         return t;
13697       }
13698
13699     case CONST_DECL:
13700       return fold (DECL_INITIAL (t));
13701
13702     default:
13703       return t;
13704     } /* switch (code) */
13705 }
13706
13707 #ifdef ENABLE_FOLD_CHECKING
13708 #undef fold
13709
13710 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13711 static void fold_check_failed (const_tree, const_tree);
13712 void print_fold_checksum (const_tree);
13713
13714 /* When --enable-checking=fold, compute a digest of expr before
13715    and after actual fold call to see if fold did not accidentally
13716    change original expr.  */
13717
13718 tree
13719 fold (tree expr)
13720 {
13721   tree ret;
13722   struct md5_ctx ctx;
13723   unsigned char checksum_before[16], checksum_after[16];
13724   htab_t ht;
13725
13726   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13727   md5_init_ctx (&ctx);
13728   fold_checksum_tree (expr, &ctx, ht);
13729   md5_finish_ctx (&ctx, checksum_before);
13730   htab_empty (ht);
13731
13732   ret = fold_1 (expr);
13733
13734   md5_init_ctx (&ctx);
13735   fold_checksum_tree (expr, &ctx, ht);
13736   md5_finish_ctx (&ctx, checksum_after);
13737   htab_delete (ht);
13738
13739   if (memcmp (checksum_before, checksum_after, 16))
13740     fold_check_failed (expr, ret);
13741
13742   return ret;
13743 }
13744
13745 void
13746 print_fold_checksum (const_tree expr)
13747 {
13748   struct md5_ctx ctx;
13749   unsigned char checksum[16], cnt;
13750   htab_t ht;
13751
13752   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13753   md5_init_ctx (&ctx);
13754   fold_checksum_tree (expr, &ctx, ht);
13755   md5_finish_ctx (&ctx, checksum);
13756   htab_delete (ht);
13757   for (cnt = 0; cnt < 16; ++cnt)
13758     fprintf (stderr, "%02x", checksum[cnt]);
13759   putc ('\n', stderr);
13760 }
13761
13762 static void
13763 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13764 {
13765   internal_error ("fold check: original tree changed by fold");
13766 }
13767
13768 static void
13769 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13770 {
13771   void **slot;
13772   enum tree_code code;
13773   union tree_node buf;
13774   int i, len;
13775
13776 recursive_label:
13777
13778   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13779                <= sizeof (struct tree_function_decl))
13780               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13781   if (expr == NULL)
13782     return;
13783   slot = (void **) htab_find_slot (ht, expr, INSERT);
13784   if (*slot != NULL)
13785     return;
13786   *slot = CONST_CAST_TREE (expr);
13787   code = TREE_CODE (expr);
13788   if (TREE_CODE_CLASS (code) == tcc_declaration
13789       && DECL_ASSEMBLER_NAME_SET_P (expr))
13790     {
13791       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
13792       memcpy ((char *) &buf, expr, tree_size (expr));
13793       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13794       expr = (tree) &buf;
13795     }
13796   else if (TREE_CODE_CLASS (code) == tcc_type
13797            && (TYPE_POINTER_TO (expr)
13798                || TYPE_REFERENCE_TO (expr)
13799                || TYPE_CACHED_VALUES_P (expr)
13800                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13801                || TYPE_NEXT_VARIANT (expr)))
13802     {
13803       /* Allow these fields to be modified.  */
13804       tree tmp;
13805       memcpy ((char *) &buf, expr, tree_size (expr));
13806       expr = tmp = (tree) &buf;
13807       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13808       TYPE_POINTER_TO (tmp) = NULL;
13809       TYPE_REFERENCE_TO (tmp) = NULL;
13810       TYPE_NEXT_VARIANT (tmp) = NULL;
13811       if (TYPE_CACHED_VALUES_P (tmp))
13812         {
13813           TYPE_CACHED_VALUES_P (tmp) = 0;
13814           TYPE_CACHED_VALUES (tmp) = NULL;
13815         }
13816     }
13817   md5_process_bytes (expr, tree_size (expr), ctx);
13818   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13819   if (TREE_CODE_CLASS (code) != tcc_type
13820       && TREE_CODE_CLASS (code) != tcc_declaration
13821       && code != TREE_LIST
13822       && code != SSA_NAME)
13823     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13824   switch (TREE_CODE_CLASS (code))
13825     {
13826     case tcc_constant:
13827       switch (code)
13828         {
13829         case STRING_CST:
13830           md5_process_bytes (TREE_STRING_POINTER (expr),
13831                              TREE_STRING_LENGTH (expr), ctx);
13832           break;
13833         case COMPLEX_CST:
13834           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13835           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13836           break;
13837         case VECTOR_CST:
13838           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13839           break;
13840         default:
13841           break;
13842         }
13843       break;
13844     case tcc_exceptional:
13845       switch (code)
13846         {
13847         case TREE_LIST:
13848           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13849           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13850           expr = TREE_CHAIN (expr);
13851           goto recursive_label;
13852           break;
13853         case TREE_VEC:
13854           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13855             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13856           break;
13857         default:
13858           break;
13859         }
13860       break;
13861     case tcc_expression:
13862     case tcc_reference:
13863     case tcc_comparison:
13864     case tcc_unary:
13865     case tcc_binary:
13866     case tcc_statement:
13867     case tcc_vl_exp:
13868       len = TREE_OPERAND_LENGTH (expr);
13869       for (i = 0; i < len; ++i)
13870         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13871       break;
13872     case tcc_declaration:
13873       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13874       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13875       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13876         {
13877           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13878           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13879           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13880           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13881           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13882         }
13883       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13884         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13885
13886       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13887         {
13888           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13889           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13890           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13891         }
13892       break;
13893     case tcc_type:
13894       if (TREE_CODE (expr) == ENUMERAL_TYPE)
13895         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13896       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13897       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13898       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13899       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13900       if (INTEGRAL_TYPE_P (expr)
13901           || SCALAR_FLOAT_TYPE_P (expr))
13902         {
13903           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13904           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13905         }
13906       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13907       if (TREE_CODE (expr) == RECORD_TYPE
13908           || TREE_CODE (expr) == UNION_TYPE
13909           || TREE_CODE (expr) == QUAL_UNION_TYPE)
13910         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13911       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13912       break;
13913     default:
13914       break;
13915     }
13916 }
13917
13918 /* Helper function for outputting the checksum of a tree T.  When
13919    debugging with gdb, you can "define mynext" to be "next" followed
13920    by "call debug_fold_checksum (op0)", then just trace down till the
13921    outputs differ.  */
13922
13923 DEBUG_FUNCTION void
13924 debug_fold_checksum (const_tree t)
13925 {
13926   int i;
13927   unsigned char checksum[16];
13928   struct md5_ctx ctx;
13929   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13930
13931   md5_init_ctx (&ctx);
13932   fold_checksum_tree (t, &ctx, ht);
13933   md5_finish_ctx (&ctx, checksum);
13934   htab_empty (ht);
13935
13936   for (i = 0; i < 16; i++)
13937     fprintf (stderr, "%d ", checksum[i]);
13938
13939   fprintf (stderr, "\n");
13940 }
13941
13942 #endif
13943
13944 /* Fold a unary tree expression with code CODE of type TYPE with an
13945    operand OP0.  LOC is the location of the resulting expression.
13946    Return a folded expression if successful.  Otherwise, return a tree
13947    expression with code CODE of type TYPE with an operand OP0.  */
13948
13949 tree
13950 fold_build1_stat_loc (location_t loc,
13951                       enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13952 {
13953   tree tem;
13954 #ifdef ENABLE_FOLD_CHECKING
13955   unsigned char checksum_before[16], checksum_after[16];
13956   struct md5_ctx ctx;
13957   htab_t ht;
13958
13959   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13960   md5_init_ctx (&ctx);
13961   fold_checksum_tree (op0, &ctx, ht);
13962   md5_finish_ctx (&ctx, checksum_before);
13963   htab_empty (ht);
13964 #endif
13965
13966   tem = fold_unary_loc (loc, code, type, op0);
13967   if (!tem)
13968     {
13969       tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13970       SET_EXPR_LOCATION (tem, loc);
13971     }
13972
13973 #ifdef ENABLE_FOLD_CHECKING
13974   md5_init_ctx (&ctx);
13975   fold_checksum_tree (op0, &ctx, ht);
13976   md5_finish_ctx (&ctx, checksum_after);
13977   htab_delete (ht);
13978
13979   if (memcmp (checksum_before, checksum_after, 16))
13980     fold_check_failed (op0, tem);
13981 #endif
13982   return tem;
13983 }
13984
13985 /* Fold a binary tree expression with code CODE of type TYPE with
13986    operands OP0 and OP1.  LOC is the location of the resulting
13987    expression.  Return a folded expression if successful.  Otherwise,
13988    return a tree expression with code CODE of type TYPE with operands
13989    OP0 and OP1.  */
13990
13991 tree
13992 fold_build2_stat_loc (location_t loc,
13993                       enum tree_code code, tree type, tree op0, tree op1
13994                       MEM_STAT_DECL)
13995 {
13996   tree tem;
13997 #ifdef ENABLE_FOLD_CHECKING
13998   unsigned char checksum_before_op0[16],
13999                 checksum_before_op1[16],
14000                 checksum_after_op0[16],
14001                 checksum_after_op1[16];
14002   struct md5_ctx ctx;
14003   htab_t ht;
14004
14005   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14006   md5_init_ctx (&ctx);
14007   fold_checksum_tree (op0, &ctx, ht);
14008   md5_finish_ctx (&ctx, checksum_before_op0);
14009   htab_empty (ht);
14010
14011   md5_init_ctx (&ctx);
14012   fold_checksum_tree (op1, &ctx, ht);
14013   md5_finish_ctx (&ctx, checksum_before_op1);
14014   htab_empty (ht);
14015 #endif
14016
14017   tem = fold_binary_loc (loc, code, type, op0, op1);
14018   if (!tem)
14019     {
14020       tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
14021       SET_EXPR_LOCATION (tem, loc);
14022     }
14023
14024 #ifdef ENABLE_FOLD_CHECKING
14025   md5_init_ctx (&ctx);
14026   fold_checksum_tree (op0, &ctx, ht);
14027   md5_finish_ctx (&ctx, checksum_after_op0);
14028   htab_empty (ht);
14029
14030   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14031     fold_check_failed (op0, tem);
14032
14033   md5_init_ctx (&ctx);
14034   fold_checksum_tree (op1, &ctx, ht);
14035   md5_finish_ctx (&ctx, checksum_after_op1);
14036   htab_delete (ht);
14037
14038   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14039     fold_check_failed (op1, tem);
14040 #endif
14041   return tem;
14042 }
14043
14044 /* Fold a ternary tree expression with code CODE of type TYPE with
14045    operands OP0, OP1, and OP2.  Return a folded expression if
14046    successful.  Otherwise, return a tree expression with code CODE of
14047    type TYPE with operands OP0, OP1, and OP2.  */
14048
14049 tree
14050 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14051                       tree op0, tree op1, tree op2 MEM_STAT_DECL)
14052 {
14053   tree tem;
14054 #ifdef ENABLE_FOLD_CHECKING
14055   unsigned char checksum_before_op0[16],
14056                 checksum_before_op1[16],
14057                 checksum_before_op2[16],
14058                 checksum_after_op0[16],
14059                 checksum_after_op1[16],
14060                 checksum_after_op2[16];
14061   struct md5_ctx ctx;
14062   htab_t ht;
14063
14064   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14065   md5_init_ctx (&ctx);
14066   fold_checksum_tree (op0, &ctx, ht);
14067   md5_finish_ctx (&ctx, checksum_before_op0);
14068   htab_empty (ht);
14069
14070   md5_init_ctx (&ctx);
14071   fold_checksum_tree (op1, &ctx, ht);
14072   md5_finish_ctx (&ctx, checksum_before_op1);
14073   htab_empty (ht);
14074
14075   md5_init_ctx (&ctx);
14076   fold_checksum_tree (op2, &ctx, ht);
14077   md5_finish_ctx (&ctx, checksum_before_op2);
14078   htab_empty (ht);
14079 #endif
14080
14081   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14082   tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14083   if (!tem)
14084     {
14085       tem =  build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
14086       SET_EXPR_LOCATION (tem, loc);
14087     }
14088
14089 #ifdef ENABLE_FOLD_CHECKING
14090   md5_init_ctx (&ctx);
14091   fold_checksum_tree (op0, &ctx, ht);
14092   md5_finish_ctx (&ctx, checksum_after_op0);
14093   htab_empty (ht);
14094
14095   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14096     fold_check_failed (op0, tem);
14097
14098   md5_init_ctx (&ctx);
14099   fold_checksum_tree (op1, &ctx, ht);
14100   md5_finish_ctx (&ctx, checksum_after_op1);
14101   htab_empty (ht);
14102
14103   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14104     fold_check_failed (op1, tem);
14105
14106   md5_init_ctx (&ctx);
14107   fold_checksum_tree (op2, &ctx, ht);
14108   md5_finish_ctx (&ctx, checksum_after_op2);
14109   htab_delete (ht);
14110
14111   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14112     fold_check_failed (op2, tem);
14113 #endif
14114   return tem;
14115 }
14116
14117 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14118    arguments in ARGARRAY, and a null static chain.
14119    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
14120    of type TYPE from the given operands as constructed by build_call_array.  */
14121
14122 tree
14123 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14124                            int nargs, tree *argarray)
14125 {
14126   tree tem;
14127 #ifdef ENABLE_FOLD_CHECKING
14128   unsigned char checksum_before_fn[16],
14129                 checksum_before_arglist[16],
14130                 checksum_after_fn[16],
14131                 checksum_after_arglist[16];
14132   struct md5_ctx ctx;
14133   htab_t ht;
14134   int i;
14135
14136   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14137   md5_init_ctx (&ctx);
14138   fold_checksum_tree (fn, &ctx, ht);
14139   md5_finish_ctx (&ctx, checksum_before_fn);
14140   htab_empty (ht);
14141
14142   md5_init_ctx (&ctx);
14143   for (i = 0; i < nargs; i++)
14144     fold_checksum_tree (argarray[i], &ctx, ht);
14145   md5_finish_ctx (&ctx, checksum_before_arglist);
14146   htab_empty (ht);
14147 #endif
14148
14149   tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14150
14151 #ifdef ENABLE_FOLD_CHECKING
14152   md5_init_ctx (&ctx);
14153   fold_checksum_tree (fn, &ctx, ht);
14154   md5_finish_ctx (&ctx, checksum_after_fn);
14155   htab_empty (ht);
14156
14157   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14158     fold_check_failed (fn, tem);
14159
14160   md5_init_ctx (&ctx);
14161   for (i = 0; i < nargs; i++)
14162     fold_checksum_tree (argarray[i], &ctx, ht);
14163   md5_finish_ctx (&ctx, checksum_after_arglist);
14164   htab_delete (ht);
14165
14166   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14167     fold_check_failed (NULL_TREE, tem);
14168 #endif
14169   return tem;
14170 }
14171
14172 /* Perform constant folding and related simplification of initializer
14173    expression EXPR.  These behave identically to "fold_buildN" but ignore
14174    potential run-time traps and exceptions that fold must preserve.  */
14175
14176 #define START_FOLD_INIT \
14177   int saved_signaling_nans = flag_signaling_nans;\
14178   int saved_trapping_math = flag_trapping_math;\
14179   int saved_rounding_math = flag_rounding_math;\
14180   int saved_trapv = flag_trapv;\
14181   int saved_folding_initializer = folding_initializer;\
14182   flag_signaling_nans = 0;\
14183   flag_trapping_math = 0;\
14184   flag_rounding_math = 0;\
14185   flag_trapv = 0;\
14186   folding_initializer = 1;
14187
14188 #define END_FOLD_INIT \
14189   flag_signaling_nans = saved_signaling_nans;\
14190   flag_trapping_math = saved_trapping_math;\
14191   flag_rounding_math = saved_rounding_math;\
14192   flag_trapv = saved_trapv;\
14193   folding_initializer = saved_folding_initializer;
14194
14195 tree
14196 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14197                              tree type, tree op)
14198 {
14199   tree result;
14200   START_FOLD_INIT;
14201
14202   result = fold_build1_loc (loc, code, type, op);
14203
14204   END_FOLD_INIT;
14205   return result;
14206 }
14207
14208 tree
14209 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14210                              tree type, tree op0, tree op1)
14211 {
14212   tree result;
14213   START_FOLD_INIT;
14214
14215   result = fold_build2_loc (loc, code, type, op0, op1);
14216
14217   END_FOLD_INIT;
14218   return result;
14219 }
14220
14221 tree
14222 fold_build3_initializer_loc (location_t loc, enum tree_code code,
14223                              tree type, tree op0, tree op1, tree op2)
14224 {
14225   tree result;
14226   START_FOLD_INIT;
14227
14228   result = fold_build3_loc (loc, code, type, op0, op1, op2);
14229
14230   END_FOLD_INIT;
14231   return result;
14232 }
14233
14234 tree
14235 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14236                                        int nargs, tree *argarray)
14237 {
14238   tree result;
14239   START_FOLD_INIT;
14240
14241   result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14242
14243   END_FOLD_INIT;
14244   return result;
14245 }
14246
14247 #undef START_FOLD_INIT
14248 #undef END_FOLD_INIT
14249
14250 /* Determine if first argument is a multiple of second argument.  Return 0 if
14251    it is not, or we cannot easily determined it to be.
14252
14253    An example of the sort of thing we care about (at this point; this routine
14254    could surely be made more general, and expanded to do what the *_DIV_EXPR's
14255    fold cases do now) is discovering that
14256
14257      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14258
14259    is a multiple of
14260
14261      SAVE_EXPR (J * 8)
14262
14263    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14264
14265    This code also handles discovering that
14266
14267      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14268
14269    is a multiple of 8 so we don't have to worry about dealing with a
14270    possible remainder.
14271
14272    Note that we *look* inside a SAVE_EXPR only to determine how it was
14273    calculated; it is not safe for fold to do much of anything else with the
14274    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14275    at run time.  For example, the latter example above *cannot* be implemented
14276    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14277    evaluation time of the original SAVE_EXPR is not necessarily the same at
14278    the time the new expression is evaluated.  The only optimization of this
14279    sort that would be valid is changing
14280
14281      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14282
14283    divided by 8 to
14284
14285      SAVE_EXPR (I) * SAVE_EXPR (J)
14286
14287    (where the same SAVE_EXPR (J) is used in the original and the
14288    transformed version).  */
14289
14290 int
14291 multiple_of_p (tree type, const_tree top, const_tree bottom)
14292 {
14293   if (operand_equal_p (top, bottom, 0))
14294     return 1;
14295
14296   if (TREE_CODE (type) != INTEGER_TYPE)
14297     return 0;
14298
14299   switch (TREE_CODE (top))
14300     {
14301     case BIT_AND_EXPR:
14302       /* Bitwise and provides a power of two multiple.  If the mask is
14303          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
14304       if (!integer_pow2p (bottom))
14305         return 0;
14306       /* FALLTHRU */
14307
14308     case MULT_EXPR:
14309       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14310               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14311
14312     case PLUS_EXPR:
14313     case MINUS_EXPR:
14314       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14315               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14316
14317     case LSHIFT_EXPR:
14318       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14319         {
14320           tree op1, t1;
14321
14322           op1 = TREE_OPERAND (top, 1);
14323           /* const_binop may not detect overflow correctly,
14324              so check for it explicitly here.  */
14325           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14326               > TREE_INT_CST_LOW (op1)
14327               && TREE_INT_CST_HIGH (op1) == 0
14328               && 0 != (t1 = fold_convert (type,
14329                                           const_binop (LSHIFT_EXPR,
14330                                                        size_one_node,
14331                                                        op1)))
14332               && !TREE_OVERFLOW (t1))
14333             return multiple_of_p (type, t1, bottom);
14334         }
14335       return 0;
14336
14337     case NOP_EXPR:
14338       /* Can't handle conversions from non-integral or wider integral type.  */
14339       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14340           || (TYPE_PRECISION (type)
14341               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14342         return 0;
14343
14344       /* .. fall through ...  */
14345
14346     case SAVE_EXPR:
14347       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14348
14349     case COND_EXPR:
14350       return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
14351               && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
14352
14353     case INTEGER_CST:
14354       if (TREE_CODE (bottom) != INTEGER_CST
14355           || integer_zerop (bottom)
14356           || (TYPE_UNSIGNED (type)
14357               && (tree_int_cst_sgn (top) < 0
14358                   || tree_int_cst_sgn (bottom) < 0)))
14359         return 0;
14360       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14361                                              top, bottom, 0));
14362
14363     default:
14364       return 0;
14365     }
14366 }
14367
14368 /* Return true if CODE or TYPE is known to be non-negative. */
14369
14370 static bool
14371 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14372 {
14373   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14374       && truth_value_p (code))
14375     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14376        have a signed:1 type (where the value is -1 and 0).  */
14377     return true;
14378   return false;
14379 }
14380
14381 /* Return true if (CODE OP0) is known to be non-negative.  If the return
14382    value is based on the assumption that signed overflow is undefined,
14383    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14384    *STRICT_OVERFLOW_P.  */
14385
14386 bool
14387 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14388                                 bool *strict_overflow_p)
14389 {
14390   if (TYPE_UNSIGNED (type))
14391     return true;
14392
14393   switch (code)
14394     {
14395     case ABS_EXPR:
14396       /* We can't return 1 if flag_wrapv is set because
14397          ABS_EXPR<INT_MIN> = INT_MIN.  */
14398       if (!INTEGRAL_TYPE_P (type))
14399         return true;
14400       if (TYPE_OVERFLOW_UNDEFINED (type))
14401         {
14402           *strict_overflow_p = true;
14403           return true;
14404         }
14405       break;
14406
14407     case NON_LVALUE_EXPR:
14408     case FLOAT_EXPR:
14409     case FIX_TRUNC_EXPR:
14410       return tree_expr_nonnegative_warnv_p (op0,
14411                                             strict_overflow_p);
14412
14413     case NOP_EXPR:
14414       {
14415         tree inner_type = TREE_TYPE (op0);
14416         tree outer_type = type;
14417
14418         if (TREE_CODE (outer_type) == REAL_TYPE)
14419           {
14420             if (TREE_CODE (inner_type) == REAL_TYPE)
14421               return tree_expr_nonnegative_warnv_p (op0,
14422                                                     strict_overflow_p);
14423             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14424               {
14425                 if (TYPE_UNSIGNED (inner_type))
14426                   return true;
14427                 return tree_expr_nonnegative_warnv_p (op0,
14428                                                       strict_overflow_p);
14429               }
14430           }
14431         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14432           {
14433             if (TREE_CODE (inner_type) == REAL_TYPE)
14434               return tree_expr_nonnegative_warnv_p (op0,
14435                                                     strict_overflow_p);
14436             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14437               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14438                       && TYPE_UNSIGNED (inner_type);
14439           }
14440       }
14441       break;
14442
14443     default:
14444       return tree_simple_nonnegative_warnv_p (code, type);
14445     }
14446
14447   /* We don't know sign of `t', so be conservative and return false.  */
14448   return false;
14449 }
14450
14451 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
14452    value is based on the assumption that signed overflow is undefined,
14453    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14454    *STRICT_OVERFLOW_P.  */
14455
14456 bool
14457 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14458                                       tree op1, bool *strict_overflow_p)
14459 {
14460   if (TYPE_UNSIGNED (type))
14461     return true;
14462
14463   switch (code)
14464     {
14465     case POINTER_PLUS_EXPR:
14466     case PLUS_EXPR:
14467       if (FLOAT_TYPE_P (type))
14468         return (tree_expr_nonnegative_warnv_p (op0,
14469                                                strict_overflow_p)
14470                 && tree_expr_nonnegative_warnv_p (op1,
14471                                                   strict_overflow_p));
14472
14473       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14474          both unsigned and at least 2 bits shorter than the result.  */
14475       if (TREE_CODE (type) == INTEGER_TYPE
14476           && TREE_CODE (op0) == NOP_EXPR
14477           && TREE_CODE (op1) == NOP_EXPR)
14478         {
14479           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14480           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14481           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14482               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14483             {
14484               unsigned int prec = MAX (TYPE_PRECISION (inner1),
14485                                        TYPE_PRECISION (inner2)) + 1;
14486               return prec < TYPE_PRECISION (type);
14487             }
14488         }
14489       break;
14490
14491     case MULT_EXPR:
14492       if (FLOAT_TYPE_P (type))
14493         {
14494           /* x * x for floating point x is always non-negative.  */
14495           if (operand_equal_p (op0, op1, 0))
14496             return true;
14497           return (tree_expr_nonnegative_warnv_p (op0,
14498                                                  strict_overflow_p)
14499                   && tree_expr_nonnegative_warnv_p (op1,
14500                                                     strict_overflow_p));
14501         }
14502
14503       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14504          both unsigned and their total bits is shorter than the result.  */
14505       if (TREE_CODE (type) == INTEGER_TYPE
14506           && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14507           && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14508         {
14509           tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14510             ? TREE_TYPE (TREE_OPERAND (op0, 0))
14511             : TREE_TYPE (op0);
14512           tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14513             ? TREE_TYPE (TREE_OPERAND (op1, 0))
14514             : TREE_TYPE (op1);
14515
14516           bool unsigned0 = TYPE_UNSIGNED (inner0);
14517           bool unsigned1 = TYPE_UNSIGNED (inner1);
14518
14519           if (TREE_CODE (op0) == INTEGER_CST)
14520             unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14521
14522           if (TREE_CODE (op1) == INTEGER_CST)
14523             unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14524
14525           if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14526               && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14527             {
14528               unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14529                 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14530                 : TYPE_PRECISION (inner0);
14531
14532               unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14533                 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14534                 : TYPE_PRECISION (inner1);
14535
14536               return precision0 + precision1 < TYPE_PRECISION (type);
14537             }
14538         }
14539       return false;
14540
14541     case BIT_AND_EXPR:
14542     case MAX_EXPR:
14543       return (tree_expr_nonnegative_warnv_p (op0,
14544                                              strict_overflow_p)
14545               || tree_expr_nonnegative_warnv_p (op1,
14546                                                 strict_overflow_p));
14547
14548     case BIT_IOR_EXPR:
14549     case BIT_XOR_EXPR:
14550     case MIN_EXPR:
14551     case RDIV_EXPR:
14552     case TRUNC_DIV_EXPR:
14553     case CEIL_DIV_EXPR:
14554     case FLOOR_DIV_EXPR:
14555     case ROUND_DIV_EXPR:
14556       return (tree_expr_nonnegative_warnv_p (op0,
14557                                              strict_overflow_p)
14558               && tree_expr_nonnegative_warnv_p (op1,
14559                                                 strict_overflow_p));
14560
14561     case TRUNC_MOD_EXPR:
14562     case CEIL_MOD_EXPR:
14563     case FLOOR_MOD_EXPR:
14564     case ROUND_MOD_EXPR:
14565       return tree_expr_nonnegative_warnv_p (op0,
14566                                             strict_overflow_p);
14567     default:
14568       return tree_simple_nonnegative_warnv_p (code, type);
14569     }
14570
14571   /* We don't know sign of `t', so be conservative and return false.  */
14572   return false;
14573 }
14574
14575 /* Return true if T is known to be non-negative.  If the return
14576    value is based on the assumption that signed overflow is undefined,
14577    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14578    *STRICT_OVERFLOW_P.  */
14579
14580 bool
14581 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14582 {
14583   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14584     return true;
14585
14586   switch (TREE_CODE (t))
14587     {
14588     case INTEGER_CST:
14589       return tree_int_cst_sgn (t) >= 0;
14590
14591     case REAL_CST:
14592       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14593
14594     case FIXED_CST:
14595       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14596
14597     case COND_EXPR:
14598       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14599                                              strict_overflow_p)
14600               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14601                                                 strict_overflow_p));
14602     default:
14603       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14604                                                    TREE_TYPE (t));
14605     }
14606   /* We don't know sign of `t', so be conservative and return false.  */
14607   return false;
14608 }
14609
14610 /* Return true if T is known to be non-negative.  If the return
14611    value is based on the assumption that signed overflow is undefined,
14612    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14613    *STRICT_OVERFLOW_P.  */
14614
14615 bool
14616 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14617                                tree arg0, tree arg1, bool *strict_overflow_p)
14618 {
14619   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14620     switch (DECL_FUNCTION_CODE (fndecl))
14621       {
14622         CASE_FLT_FN (BUILT_IN_ACOS):
14623         CASE_FLT_FN (BUILT_IN_ACOSH):
14624         CASE_FLT_FN (BUILT_IN_CABS):
14625         CASE_FLT_FN (BUILT_IN_COSH):
14626         CASE_FLT_FN (BUILT_IN_ERFC):
14627         CASE_FLT_FN (BUILT_IN_EXP):
14628         CASE_FLT_FN (BUILT_IN_EXP10):
14629         CASE_FLT_FN (BUILT_IN_EXP2):
14630         CASE_FLT_FN (BUILT_IN_FABS):
14631         CASE_FLT_FN (BUILT_IN_FDIM):
14632         CASE_FLT_FN (BUILT_IN_HYPOT):
14633         CASE_FLT_FN (BUILT_IN_POW10):
14634         CASE_INT_FN (BUILT_IN_FFS):
14635         CASE_INT_FN (BUILT_IN_PARITY):
14636         CASE_INT_FN (BUILT_IN_POPCOUNT):
14637       case BUILT_IN_BSWAP32:
14638       case BUILT_IN_BSWAP64:
14639         /* Always true.  */
14640         return true;
14641
14642         CASE_FLT_FN (BUILT_IN_SQRT):
14643         /* sqrt(-0.0) is -0.0.  */
14644         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14645           return true;
14646         return tree_expr_nonnegative_warnv_p (arg0,
14647                                               strict_overflow_p);
14648
14649         CASE_FLT_FN (BUILT_IN_ASINH):
14650         CASE_FLT_FN (BUILT_IN_ATAN):
14651         CASE_FLT_FN (BUILT_IN_ATANH):
14652         CASE_FLT_FN (BUILT_IN_CBRT):
14653         CASE_FLT_FN (BUILT_IN_CEIL):
14654         CASE_FLT_FN (BUILT_IN_ERF):
14655         CASE_FLT_FN (BUILT_IN_EXPM1):
14656         CASE_FLT_FN (BUILT_IN_FLOOR):
14657         CASE_FLT_FN (BUILT_IN_FMOD):
14658         CASE_FLT_FN (BUILT_IN_FREXP):
14659         CASE_FLT_FN (BUILT_IN_LCEIL):
14660         CASE_FLT_FN (BUILT_IN_LDEXP):
14661         CASE_FLT_FN (BUILT_IN_LFLOOR):
14662         CASE_FLT_FN (BUILT_IN_LLCEIL):
14663         CASE_FLT_FN (BUILT_IN_LLFLOOR):
14664         CASE_FLT_FN (BUILT_IN_LLRINT):
14665         CASE_FLT_FN (BUILT_IN_LLROUND):
14666         CASE_FLT_FN (BUILT_IN_LRINT):
14667         CASE_FLT_FN (BUILT_IN_LROUND):
14668         CASE_FLT_FN (BUILT_IN_MODF):
14669         CASE_FLT_FN (BUILT_IN_NEARBYINT):
14670         CASE_FLT_FN (BUILT_IN_RINT):
14671         CASE_FLT_FN (BUILT_IN_ROUND):
14672         CASE_FLT_FN (BUILT_IN_SCALB):
14673         CASE_FLT_FN (BUILT_IN_SCALBLN):
14674         CASE_FLT_FN (BUILT_IN_SCALBN):
14675         CASE_FLT_FN (BUILT_IN_SIGNBIT):
14676         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14677         CASE_FLT_FN (BUILT_IN_SINH):
14678         CASE_FLT_FN (BUILT_IN_TANH):
14679         CASE_FLT_FN (BUILT_IN_TRUNC):
14680         /* True if the 1st argument is nonnegative.  */
14681         return tree_expr_nonnegative_warnv_p (arg0,
14682                                               strict_overflow_p);
14683
14684         CASE_FLT_FN (BUILT_IN_FMAX):
14685         /* True if the 1st OR 2nd arguments are nonnegative.  */
14686         return (tree_expr_nonnegative_warnv_p (arg0,
14687                                                strict_overflow_p)
14688                 || (tree_expr_nonnegative_warnv_p (arg1,
14689                                                    strict_overflow_p)));
14690
14691         CASE_FLT_FN (BUILT_IN_FMIN):
14692         /* True if the 1st AND 2nd arguments are nonnegative.  */
14693         return (tree_expr_nonnegative_warnv_p (arg0,
14694                                                strict_overflow_p)
14695                 && (tree_expr_nonnegative_warnv_p (arg1,
14696                                                    strict_overflow_p)));
14697
14698         CASE_FLT_FN (BUILT_IN_COPYSIGN):
14699         /* True if the 2nd argument is nonnegative.  */
14700         return tree_expr_nonnegative_warnv_p (arg1,
14701                                               strict_overflow_p);
14702
14703         CASE_FLT_FN (BUILT_IN_POWI):
14704         /* True if the 1st argument is nonnegative or the second
14705            argument is an even integer.  */
14706         if (TREE_CODE (arg1) == INTEGER_CST
14707             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14708           return true;
14709         return tree_expr_nonnegative_warnv_p (arg0,
14710                                               strict_overflow_p);
14711
14712         CASE_FLT_FN (BUILT_IN_POW):
14713         /* True if the 1st argument is nonnegative or the second
14714            argument is an even integer valued real.  */
14715         if (TREE_CODE (arg1) == REAL_CST)
14716           {
14717             REAL_VALUE_TYPE c;
14718             HOST_WIDE_INT n;
14719
14720             c = TREE_REAL_CST (arg1);
14721             n = real_to_integer (&c);
14722             if ((n & 1) == 0)
14723               {
14724                 REAL_VALUE_TYPE cint;
14725                 real_from_integer (&cint, VOIDmode, n,
14726                                    n < 0 ? -1 : 0, 0);
14727                 if (real_identical (&c, &cint))
14728                   return true;
14729               }
14730           }
14731         return tree_expr_nonnegative_warnv_p (arg0,
14732                                               strict_overflow_p);
14733
14734       default:
14735         break;
14736       }
14737   return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14738                                           type);
14739 }
14740
14741 /* Return true if T is known to be non-negative.  If the return
14742    value is based on the assumption that signed overflow is undefined,
14743    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14744    *STRICT_OVERFLOW_P.  */
14745
14746 bool
14747 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14748 {
14749   enum tree_code code = TREE_CODE (t);
14750   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14751     return true;
14752
14753   switch (code)
14754     {
14755     case TARGET_EXPR:
14756       {
14757         tree temp = TARGET_EXPR_SLOT (t);
14758         t = TARGET_EXPR_INITIAL (t);
14759
14760         /* If the initializer is non-void, then it's a normal expression
14761            that will be assigned to the slot.  */
14762         if (!VOID_TYPE_P (t))
14763           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14764
14765         /* Otherwise, the initializer sets the slot in some way.  One common
14766            way is an assignment statement at the end of the initializer.  */
14767         while (1)
14768           {
14769             if (TREE_CODE (t) == BIND_EXPR)
14770               t = expr_last (BIND_EXPR_BODY (t));
14771             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14772                      || TREE_CODE (t) == TRY_CATCH_EXPR)
14773               t = expr_last (TREE_OPERAND (t, 0));
14774             else if (TREE_CODE (t) == STATEMENT_LIST)
14775               t = expr_last (t);
14776             else
14777               break;
14778           }
14779         if (TREE_CODE (t) == MODIFY_EXPR
14780             && TREE_OPERAND (t, 0) == temp)
14781           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14782                                                 strict_overflow_p);
14783
14784         return false;
14785       }
14786
14787     case CALL_EXPR:
14788       {
14789         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
14790         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
14791
14792         return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14793                                               get_callee_fndecl (t),
14794                                               arg0,
14795                                               arg1,
14796                                               strict_overflow_p);
14797       }
14798     case COMPOUND_EXPR:
14799     case MODIFY_EXPR:
14800       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14801                                             strict_overflow_p);
14802     case BIND_EXPR:
14803       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14804                                             strict_overflow_p);
14805     case SAVE_EXPR:
14806       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14807                                             strict_overflow_p);
14808
14809     default:
14810       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14811                                                    TREE_TYPE (t));
14812     }
14813
14814   /* We don't know sign of `t', so be conservative and return false.  */
14815   return false;
14816 }
14817
14818 /* Return true if T is known to be non-negative.  If the return
14819    value is based on the assumption that signed overflow is undefined,
14820    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14821    *STRICT_OVERFLOW_P.  */
14822
14823 bool
14824 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14825 {
14826   enum tree_code code;
14827   if (t == error_mark_node)
14828     return false;
14829
14830   code = TREE_CODE (t);
14831   switch (TREE_CODE_CLASS (code))
14832     {
14833     case tcc_binary:
14834     case tcc_comparison:
14835       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14836                                               TREE_TYPE (t),
14837                                               TREE_OPERAND (t, 0),
14838                                               TREE_OPERAND (t, 1),
14839                                               strict_overflow_p);
14840
14841     case tcc_unary:
14842       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14843                                              TREE_TYPE (t),
14844                                              TREE_OPERAND (t, 0),
14845                                              strict_overflow_p);
14846
14847     case tcc_constant:
14848     case tcc_declaration:
14849     case tcc_reference:
14850       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14851
14852     default:
14853       break;
14854     }
14855
14856   switch (code)
14857     {
14858     case TRUTH_AND_EXPR:
14859     case TRUTH_OR_EXPR:
14860     case TRUTH_XOR_EXPR:
14861       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14862                                               TREE_TYPE (t),
14863                                               TREE_OPERAND (t, 0),
14864                                               TREE_OPERAND (t, 1),
14865                                               strict_overflow_p);
14866     case TRUTH_NOT_EXPR:
14867       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14868                                              TREE_TYPE (t),
14869                                              TREE_OPERAND (t, 0),
14870                                              strict_overflow_p);
14871
14872     case COND_EXPR:
14873     case CONSTRUCTOR:
14874     case OBJ_TYPE_REF:
14875     case ASSERT_EXPR:
14876     case ADDR_EXPR:
14877     case WITH_SIZE_EXPR:
14878     case SSA_NAME:
14879       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14880
14881     default:
14882       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14883     }
14884 }
14885
14886 /* Return true if `t' is known to be non-negative.  Handle warnings
14887    about undefined signed overflow.  */
14888
14889 bool
14890 tree_expr_nonnegative_p (tree t)
14891 {
14892   bool ret, strict_overflow_p;
14893
14894   strict_overflow_p = false;
14895   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14896   if (strict_overflow_p)
14897     fold_overflow_warning (("assuming signed overflow does not occur when "
14898                             "determining that expression is always "
14899                             "non-negative"),
14900                            WARN_STRICT_OVERFLOW_MISC);
14901   return ret;
14902 }
14903
14904
14905 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14906    For floating point we further ensure that T is not denormal.
14907    Similar logic is present in nonzero_address in rtlanal.h.
14908
14909    If the return value is based on the assumption that signed overflow
14910    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14911    change *STRICT_OVERFLOW_P.  */
14912
14913 bool
14914 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14915                                  bool *strict_overflow_p)
14916 {
14917   switch (code)
14918     {
14919     case ABS_EXPR:
14920       return tree_expr_nonzero_warnv_p (op0,
14921                                         strict_overflow_p);
14922
14923     case NOP_EXPR:
14924       {
14925         tree inner_type = TREE_TYPE (op0);
14926         tree outer_type = type;
14927
14928         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14929                 && tree_expr_nonzero_warnv_p (op0,
14930                                               strict_overflow_p));
14931       }
14932       break;
14933
14934     case NON_LVALUE_EXPR:
14935       return tree_expr_nonzero_warnv_p (op0,
14936                                         strict_overflow_p);
14937
14938     default:
14939       break;
14940   }
14941
14942   return false;
14943 }
14944
14945 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14946    For floating point we further ensure that T is not denormal.
14947    Similar logic is present in nonzero_address in rtlanal.h.
14948
14949    If the return value is based on the assumption that signed overflow
14950    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14951    change *STRICT_OVERFLOW_P.  */
14952
14953 bool
14954 tree_binary_nonzero_warnv_p (enum tree_code code,
14955                              tree type,
14956                              tree op0,
14957                              tree op1, bool *strict_overflow_p)
14958 {
14959   bool sub_strict_overflow_p;
14960   switch (code)
14961     {
14962     case POINTER_PLUS_EXPR:
14963     case PLUS_EXPR:
14964       if (TYPE_OVERFLOW_UNDEFINED (type))
14965         {
14966           /* With the presence of negative values it is hard
14967              to say something.  */
14968           sub_strict_overflow_p = false;
14969           if (!tree_expr_nonnegative_warnv_p (op0,
14970                                               &sub_strict_overflow_p)
14971               || !tree_expr_nonnegative_warnv_p (op1,
14972                                                  &sub_strict_overflow_p))
14973             return false;
14974           /* One of operands must be positive and the other non-negative.  */
14975           /* We don't set *STRICT_OVERFLOW_P here: even if this value
14976              overflows, on a twos-complement machine the sum of two
14977              nonnegative numbers can never be zero.  */
14978           return (tree_expr_nonzero_warnv_p (op0,
14979                                              strict_overflow_p)
14980                   || tree_expr_nonzero_warnv_p (op1,
14981                                                 strict_overflow_p));
14982         }
14983       break;
14984
14985     case MULT_EXPR:
14986       if (TYPE_OVERFLOW_UNDEFINED (type))
14987         {
14988           if (tree_expr_nonzero_warnv_p (op0,
14989                                          strict_overflow_p)
14990               && tree_expr_nonzero_warnv_p (op1,
14991                                             strict_overflow_p))
14992             {
14993               *strict_overflow_p = true;
14994               return true;
14995             }
14996         }
14997       break;
14998
14999     case MIN_EXPR:
15000       sub_strict_overflow_p = false;
15001       if (tree_expr_nonzero_warnv_p (op0,
15002                                      &sub_strict_overflow_p)
15003           && tree_expr_nonzero_warnv_p (op1,
15004                                         &sub_strict_overflow_p))
15005         {
15006           if (sub_strict_overflow_p)
15007             *strict_overflow_p = true;
15008         }
15009       break;
15010
15011     case MAX_EXPR:
15012       sub_strict_overflow_p = false;
15013       if (tree_expr_nonzero_warnv_p (op0,
15014                                      &sub_strict_overflow_p))
15015         {
15016           if (sub_strict_overflow_p)
15017             *strict_overflow_p = true;
15018
15019           /* When both operands are nonzero, then MAX must be too.  */
15020           if (tree_expr_nonzero_warnv_p (op1,
15021                                          strict_overflow_p))
15022             return true;
15023
15024           /* MAX where operand 0 is positive is positive.  */
15025           return tree_expr_nonnegative_warnv_p (op0,
15026                                                strict_overflow_p);
15027         }
15028       /* MAX where operand 1 is positive is positive.  */
15029       else if (tree_expr_nonzero_warnv_p (op1,
15030                                           &sub_strict_overflow_p)
15031                && tree_expr_nonnegative_warnv_p (op1,
15032                                                  &sub_strict_overflow_p))
15033         {
15034           if (sub_strict_overflow_p)
15035             *strict_overflow_p = true;
15036           return true;
15037         }
15038       break;
15039
15040     case BIT_IOR_EXPR:
15041       return (tree_expr_nonzero_warnv_p (op1,
15042                                          strict_overflow_p)
15043               || tree_expr_nonzero_warnv_p (op0,
15044                                             strict_overflow_p));
15045
15046     default:
15047       break;
15048   }
15049
15050   return false;
15051 }
15052
15053 /* Return true when T is an address and is known to be nonzero.
15054    For floating point we further ensure that T is not denormal.
15055    Similar logic is present in nonzero_address in rtlanal.h.
15056
15057    If the return value is based on the assumption that signed overflow
15058    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15059    change *STRICT_OVERFLOW_P.  */
15060
15061 bool
15062 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15063 {
15064   bool sub_strict_overflow_p;
15065   switch (TREE_CODE (t))
15066     {
15067     case INTEGER_CST:
15068       return !integer_zerop (t);
15069
15070     case ADDR_EXPR:
15071       {
15072         tree base = TREE_OPERAND (t, 0);
15073         if (!DECL_P (base))
15074           base = get_base_address (base);
15075
15076         if (!base)
15077           return false;
15078
15079         /* Weak declarations may link to NULL.  Other things may also be NULL
15080            so protect with -fdelete-null-pointer-checks; but not variables
15081            allocated on the stack.  */
15082         if (DECL_P (base)
15083             && (flag_delete_null_pointer_checks
15084                 || (DECL_CONTEXT (base)
15085                     && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
15086                     && auto_var_in_fn_p (base, DECL_CONTEXT (base)))))
15087           return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
15088
15089         /* Constants are never weak.  */
15090         if (CONSTANT_CLASS_P (base))
15091           return true;
15092
15093         return false;
15094       }
15095
15096     case COND_EXPR:
15097       sub_strict_overflow_p = false;
15098       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15099                                      &sub_strict_overflow_p)
15100           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15101                                         &sub_strict_overflow_p))
15102         {
15103           if (sub_strict_overflow_p)
15104             *strict_overflow_p = true;
15105           return true;
15106         }
15107       break;
15108
15109     default:
15110       break;
15111     }
15112   return false;
15113 }
15114
15115 /* Return true when T is an address and is known to be nonzero.
15116    For floating point we further ensure that T is not denormal.
15117    Similar logic is present in nonzero_address in rtlanal.h.
15118
15119    If the return value is based on the assumption that signed overflow
15120    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15121    change *STRICT_OVERFLOW_P.  */
15122
15123 bool
15124 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15125 {
15126   tree type = TREE_TYPE (t);
15127   enum tree_code code;
15128
15129   /* Doing something useful for floating point would need more work.  */
15130   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15131     return false;
15132
15133   code = TREE_CODE (t);
15134   switch (TREE_CODE_CLASS (code))
15135     {
15136     case tcc_unary:
15137       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15138                                               strict_overflow_p);
15139     case tcc_binary:
15140     case tcc_comparison:
15141       return tree_binary_nonzero_warnv_p (code, type,
15142                                                TREE_OPERAND (t, 0),
15143                                                TREE_OPERAND (t, 1),
15144                                                strict_overflow_p);
15145     case tcc_constant:
15146     case tcc_declaration:
15147     case tcc_reference:
15148       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15149
15150     default:
15151       break;
15152     }
15153
15154   switch (code)
15155     {
15156     case TRUTH_NOT_EXPR:
15157       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15158                                               strict_overflow_p);
15159
15160     case TRUTH_AND_EXPR:
15161     case TRUTH_OR_EXPR:
15162     case TRUTH_XOR_EXPR:
15163       return tree_binary_nonzero_warnv_p (code, type,
15164                                                TREE_OPERAND (t, 0),
15165                                                TREE_OPERAND (t, 1),
15166                                                strict_overflow_p);
15167
15168     case COND_EXPR:
15169     case CONSTRUCTOR:
15170     case OBJ_TYPE_REF:
15171     case ASSERT_EXPR:
15172     case ADDR_EXPR:
15173     case WITH_SIZE_EXPR:
15174     case SSA_NAME:
15175       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15176
15177     case COMPOUND_EXPR:
15178     case MODIFY_EXPR:
15179     case BIND_EXPR:
15180       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15181                                         strict_overflow_p);
15182
15183     case SAVE_EXPR:
15184       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15185                                         strict_overflow_p);
15186
15187     case CALL_EXPR:
15188       return alloca_call_p (t);
15189
15190     default:
15191       break;
15192     }
15193   return false;
15194 }
15195
15196 /* Return true when T is an address and is known to be nonzero.
15197    Handle warnings about undefined signed overflow.  */
15198
15199 bool
15200 tree_expr_nonzero_p (tree t)
15201 {
15202   bool ret, strict_overflow_p;
15203
15204   strict_overflow_p = false;
15205   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15206   if (strict_overflow_p)
15207     fold_overflow_warning (("assuming signed overflow does not occur when "
15208                             "determining that expression is always "
15209                             "non-zero"),
15210                            WARN_STRICT_OVERFLOW_MISC);
15211   return ret;
15212 }
15213
15214 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15215    attempt to fold the expression to a constant without modifying TYPE,
15216    OP0 or OP1.
15217
15218    If the expression could be simplified to a constant, then return
15219    the constant.  If the expression would not be simplified to a
15220    constant, then return NULL_TREE.  */
15221
15222 tree
15223 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15224 {
15225   tree tem = fold_binary (code, type, op0, op1);
15226   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15227 }
15228
15229 /* Given the components of a unary expression CODE, TYPE and OP0,
15230    attempt to fold the expression to a constant without modifying
15231    TYPE or OP0.
15232
15233    If the expression could be simplified to a constant, then return
15234    the constant.  If the expression would not be simplified to a
15235    constant, then return NULL_TREE.  */
15236
15237 tree
15238 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15239 {
15240   tree tem = fold_unary (code, type, op0);
15241   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15242 }
15243
15244 /* If EXP represents referencing an element in a constant string
15245    (either via pointer arithmetic or array indexing), return the
15246    tree representing the value accessed, otherwise return NULL.  */
15247
15248 tree
15249 fold_read_from_constant_string (tree exp)
15250 {
15251   if ((TREE_CODE (exp) == INDIRECT_REF
15252        || TREE_CODE (exp) == ARRAY_REF)
15253       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15254     {
15255       tree exp1 = TREE_OPERAND (exp, 0);
15256       tree index;
15257       tree string;
15258       location_t loc = EXPR_LOCATION (exp);
15259
15260       if (TREE_CODE (exp) == INDIRECT_REF)
15261         string = string_constant (exp1, &index);
15262       else
15263         {
15264           tree low_bound = array_ref_low_bound (exp);
15265           index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15266
15267           /* Optimize the special-case of a zero lower bound.
15268
15269              We convert the low_bound to sizetype to avoid some problems
15270              with constant folding.  (E.g. suppose the lower bound is 1,
15271              and its mode is QI.  Without the conversion,l (ARRAY
15272              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15273              +INDEX), which becomes (ARRAY+255+INDEX).  Oops!)  */
15274           if (! integer_zerop (low_bound))
15275             index = size_diffop_loc (loc, index,
15276                                  fold_convert_loc (loc, sizetype, low_bound));
15277
15278           string = exp1;
15279         }
15280
15281       if (string
15282           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15283           && TREE_CODE (string) == STRING_CST
15284           && TREE_CODE (index) == INTEGER_CST
15285           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15286           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15287               == MODE_INT)
15288           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15289         return build_int_cst_type (TREE_TYPE (exp),
15290                                    (TREE_STRING_POINTER (string)
15291                                     [TREE_INT_CST_LOW (index)]));
15292     }
15293   return NULL;
15294 }
15295
15296 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15297    an integer constant, real, or fixed-point constant.
15298
15299    TYPE is the type of the result.  */
15300
15301 static tree
15302 fold_negate_const (tree arg0, tree type)
15303 {
15304   tree t = NULL_TREE;
15305
15306   switch (TREE_CODE (arg0))
15307     {
15308     case INTEGER_CST:
15309       {
15310         double_int val = tree_to_double_int (arg0);
15311         int overflow = neg_double (val.low, val.high, &val.low, &val.high);
15312
15313         t = force_fit_type_double (type, val, 1,
15314                                    (overflow | TREE_OVERFLOW (arg0))
15315                                    && !TYPE_UNSIGNED (type));
15316         break;
15317       }
15318
15319     case REAL_CST:
15320       t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15321       break;
15322
15323     case FIXED_CST:
15324       {
15325         FIXED_VALUE_TYPE f;
15326         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15327                                             &(TREE_FIXED_CST (arg0)), NULL,
15328                                             TYPE_SATURATING (type));
15329         t = build_fixed (type, f);
15330         /* Propagate overflow flags.  */
15331         if (overflow_p | TREE_OVERFLOW (arg0))
15332           TREE_OVERFLOW (t) = 1;
15333         break;
15334       }
15335
15336     default:
15337       gcc_unreachable ();
15338     }
15339
15340   return t;
15341 }
15342
15343 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15344    an integer constant or real constant.
15345
15346    TYPE is the type of the result.  */
15347
15348 tree
15349 fold_abs_const (tree arg0, tree type)
15350 {
15351   tree t = NULL_TREE;
15352
15353   switch (TREE_CODE (arg0))
15354     {
15355     case INTEGER_CST:
15356       {
15357         double_int val = tree_to_double_int (arg0);
15358
15359         /* If the value is unsigned or non-negative, then the absolute value
15360            is the same as the ordinary value.  */
15361         if (TYPE_UNSIGNED (type)
15362             || !double_int_negative_p (val))
15363           t = arg0;
15364
15365         /* If the value is negative, then the absolute value is
15366            its negation.  */
15367         else
15368           {
15369             int overflow;
15370
15371             overflow = neg_double (val.low, val.high, &val.low, &val.high);
15372             t = force_fit_type_double (type, val, -1,
15373                                        overflow | TREE_OVERFLOW (arg0));
15374           }
15375       }
15376       break;
15377
15378     case REAL_CST:
15379       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15380         t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15381       else
15382         t =  arg0;
15383       break;
15384
15385     default:
15386       gcc_unreachable ();
15387     }
15388
15389   return t;
15390 }
15391
15392 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15393    constant.  TYPE is the type of the result.  */
15394
15395 static tree
15396 fold_not_const (const_tree arg0, tree type)
15397 {
15398   double_int val;  
15399
15400   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15401
15402   val = double_int_not (tree_to_double_int (arg0));
15403   return force_fit_type_double (type, val, 0, TREE_OVERFLOW (arg0));
15404 }
15405
15406 /* Given CODE, a relational operator, the target type, TYPE and two
15407    constant operands OP0 and OP1, return the result of the
15408    relational operation.  If the result is not a compile time
15409    constant, then return NULL_TREE.  */
15410
15411 static tree
15412 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15413 {
15414   int result, invert;
15415
15416   /* From here on, the only cases we handle are when the result is
15417      known to be a constant.  */
15418
15419   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15420     {
15421       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15422       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15423
15424       /* Handle the cases where either operand is a NaN.  */
15425       if (real_isnan (c0) || real_isnan (c1))
15426         {
15427           switch (code)
15428             {
15429             case EQ_EXPR:
15430             case ORDERED_EXPR:
15431               result = 0;
15432               break;
15433
15434             case NE_EXPR:
15435             case UNORDERED_EXPR:
15436             case UNLT_EXPR:
15437             case UNLE_EXPR:
15438             case UNGT_EXPR:
15439             case UNGE_EXPR:
15440             case UNEQ_EXPR:
15441               result = 1;
15442               break;
15443
15444             case LT_EXPR:
15445             case LE_EXPR:
15446             case GT_EXPR:
15447             case GE_EXPR:
15448             case LTGT_EXPR:
15449               if (flag_trapping_math)
15450                 return NULL_TREE;
15451               result = 0;
15452               break;
15453
15454             default:
15455               gcc_unreachable ();
15456             }
15457
15458           return constant_boolean_node (result, type);
15459         }
15460
15461       return constant_boolean_node (real_compare (code, c0, c1), type);
15462     }
15463
15464   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15465     {
15466       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15467       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15468       return constant_boolean_node (fixed_compare (code, c0, c1), type);
15469     }
15470
15471   /* Handle equality/inequality of complex constants.  */
15472   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15473     {
15474       tree rcond = fold_relational_const (code, type,
15475                                           TREE_REALPART (op0),
15476                                           TREE_REALPART (op1));
15477       tree icond = fold_relational_const (code, type,
15478                                           TREE_IMAGPART (op0),
15479                                           TREE_IMAGPART (op1));
15480       if (code == EQ_EXPR)
15481         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15482       else if (code == NE_EXPR)
15483         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15484       else
15485         return NULL_TREE;
15486     }
15487
15488   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15489
15490      To compute GT, swap the arguments and do LT.
15491      To compute GE, do LT and invert the result.
15492      To compute LE, swap the arguments, do LT and invert the result.
15493      To compute NE, do EQ and invert the result.
15494
15495      Therefore, the code below must handle only EQ and LT.  */
15496
15497   if (code == LE_EXPR || code == GT_EXPR)
15498     {
15499       tree tem = op0;
15500       op0 = op1;
15501       op1 = tem;
15502       code = swap_tree_comparison (code);
15503     }
15504
15505   /* Note that it is safe to invert for real values here because we
15506      have already handled the one case that it matters.  */
15507
15508   invert = 0;
15509   if (code == NE_EXPR || code == GE_EXPR)
15510     {
15511       invert = 1;
15512       code = invert_tree_comparison (code, false);
15513     }
15514
15515   /* Compute a result for LT or EQ if args permit;
15516      Otherwise return T.  */
15517   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15518     {
15519       if (code == EQ_EXPR)
15520         result = tree_int_cst_equal (op0, op1);
15521       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15522         result = INT_CST_LT_UNSIGNED (op0, op1);
15523       else
15524         result = INT_CST_LT (op0, op1);
15525     }
15526   else
15527     return NULL_TREE;
15528
15529   if (invert)
15530     result ^= 1;
15531   return constant_boolean_node (result, type);
15532 }
15533
15534 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15535    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
15536    itself.  */
15537
15538 tree
15539 fold_build_cleanup_point_expr (tree type, tree expr)
15540 {
15541   /* If the expression does not have side effects then we don't have to wrap
15542      it with a cleanup point expression.  */
15543   if (!TREE_SIDE_EFFECTS (expr))
15544     return expr;
15545
15546   /* If the expression is a return, check to see if the expression inside the
15547      return has no side effects or the right hand side of the modify expression
15548      inside the return. If either don't have side effects set we don't need to
15549      wrap the expression in a cleanup point expression.  Note we don't check the
15550      left hand side of the modify because it should always be a return decl.  */
15551   if (TREE_CODE (expr) == RETURN_EXPR)
15552     {
15553       tree op = TREE_OPERAND (expr, 0);
15554       if (!op || !TREE_SIDE_EFFECTS (op))
15555         return expr;
15556       op = TREE_OPERAND (op, 1);
15557       if (!TREE_SIDE_EFFECTS (op))
15558         return expr;
15559     }
15560
15561   return build1 (CLEANUP_POINT_EXPR, type, expr);
15562 }
15563
15564 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15565    of an indirection through OP0, or NULL_TREE if no simplification is
15566    possible.  */
15567
15568 tree
15569 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15570 {
15571   tree sub = op0;
15572   tree subtype;
15573
15574   STRIP_NOPS (sub);
15575   subtype = TREE_TYPE (sub);
15576   if (!POINTER_TYPE_P (subtype))
15577     return NULL_TREE;
15578
15579   if (TREE_CODE (sub) == ADDR_EXPR)
15580     {
15581       tree op = TREE_OPERAND (sub, 0);
15582       tree optype = TREE_TYPE (op);
15583       /* *&CONST_DECL -> to the value of the const decl.  */
15584       if (TREE_CODE (op) == CONST_DECL)
15585         return DECL_INITIAL (op);
15586       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
15587       if (type == optype)
15588         {
15589           tree fop = fold_read_from_constant_string (op);
15590           if (fop)
15591             return fop;
15592           else
15593             return op;
15594         }
15595       /* *(foo *)&fooarray => fooarray[0] */
15596       else if (TREE_CODE (optype) == ARRAY_TYPE
15597                && type == TREE_TYPE (optype))
15598         {
15599           tree type_domain = TYPE_DOMAIN (optype);
15600           tree min_val = size_zero_node;
15601           if (type_domain && TYPE_MIN_VALUE (type_domain))
15602             min_val = TYPE_MIN_VALUE (type_domain);
15603           op0 = build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15604           SET_EXPR_LOCATION (op0, loc);
15605           return op0;
15606         }
15607       /* *(foo *)&complexfoo => __real__ complexfoo */
15608       else if (TREE_CODE (optype) == COMPLEX_TYPE
15609                && type == TREE_TYPE (optype))
15610         return fold_build1_loc (loc, REALPART_EXPR, type, op);
15611       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15612       else if (TREE_CODE (optype) == VECTOR_TYPE
15613                && type == TREE_TYPE (optype))
15614         {
15615           tree part_width = TYPE_SIZE (type);
15616           tree index = bitsize_int (0);
15617           return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
15618         }
15619     }
15620
15621   /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15622   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15623       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15624     {
15625       tree op00 = TREE_OPERAND (sub, 0);
15626       tree op01 = TREE_OPERAND (sub, 1);
15627       tree op00type;
15628
15629       STRIP_NOPS (op00);
15630       op00type = TREE_TYPE (op00);
15631       if (TREE_CODE (op00) == ADDR_EXPR
15632           && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
15633           && type == TREE_TYPE (TREE_TYPE (op00type)))
15634         {
15635           HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15636           tree part_width = TYPE_SIZE (type);
15637           unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15638           unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15639           tree index = bitsize_int (indexi);
15640
15641           if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
15642             return fold_build3_loc (loc,
15643                                 BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
15644                                 part_width, index);
15645
15646         }
15647     }
15648
15649
15650   /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15651   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15652       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15653     {
15654       tree op00 = TREE_OPERAND (sub, 0);
15655       tree op01 = TREE_OPERAND (sub, 1);
15656       tree op00type;
15657
15658       STRIP_NOPS (op00);
15659       op00type = TREE_TYPE (op00);
15660       if (TREE_CODE (op00) == ADDR_EXPR
15661           && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
15662           && type == TREE_TYPE (TREE_TYPE (op00type)))
15663         {
15664           tree size = TYPE_SIZE_UNIT (type);
15665           if (tree_int_cst_equal (size, op01))
15666             return fold_build1_loc (loc, IMAGPART_EXPR, type,
15667                                 TREE_OPERAND (op00, 0));
15668         }
15669     }
15670
15671   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15672   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15673       && type == TREE_TYPE (TREE_TYPE (subtype)))
15674     {
15675       tree type_domain;
15676       tree min_val = size_zero_node;
15677       sub = build_fold_indirect_ref_loc (loc, sub);
15678       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15679       if (type_domain && TYPE_MIN_VALUE (type_domain))
15680         min_val = TYPE_MIN_VALUE (type_domain);
15681       op0 = build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15682       SET_EXPR_LOCATION (op0, loc);
15683       return op0;
15684     }
15685
15686   return NULL_TREE;
15687 }
15688
15689 /* Builds an expression for an indirection through T, simplifying some
15690    cases.  */
15691
15692 tree
15693 build_fold_indirect_ref_loc (location_t loc, tree t)
15694 {
15695   tree type = TREE_TYPE (TREE_TYPE (t));
15696   tree sub = fold_indirect_ref_1 (loc, type, t);
15697
15698   if (sub)
15699     return sub;
15700
15701   t = build1 (INDIRECT_REF, type, t);
15702   SET_EXPR_LOCATION (t, loc);
15703   return t;
15704 }
15705
15706 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
15707
15708 tree
15709 fold_indirect_ref_loc (location_t loc, tree t)
15710 {
15711   tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15712
15713   if (sub)
15714     return sub;
15715   else
15716     return t;
15717 }
15718
15719 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15720    whose result is ignored.  The type of the returned tree need not be
15721    the same as the original expression.  */
15722
15723 tree
15724 fold_ignored_result (tree t)
15725 {
15726   if (!TREE_SIDE_EFFECTS (t))
15727     return integer_zero_node;
15728
15729   for (;;)
15730     switch (TREE_CODE_CLASS (TREE_CODE (t)))
15731       {
15732       case tcc_unary:
15733         t = TREE_OPERAND (t, 0);
15734         break;
15735
15736       case tcc_binary:
15737       case tcc_comparison:
15738         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15739           t = TREE_OPERAND (t, 0);
15740         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15741           t = TREE_OPERAND (t, 1);
15742         else
15743           return t;
15744         break;
15745
15746       case tcc_expression:
15747         switch (TREE_CODE (t))
15748           {
15749           case COMPOUND_EXPR:
15750             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15751               return t;
15752             t = TREE_OPERAND (t, 0);
15753             break;
15754
15755           case COND_EXPR:
15756             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15757                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15758               return t;
15759             t = TREE_OPERAND (t, 0);
15760             break;
15761
15762           default:
15763             return t;
15764           }
15765         break;
15766
15767       default:
15768         return t;
15769       }
15770 }
15771
15772 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15773    This can only be applied to objects of a sizetype.  */
15774
15775 tree
15776 round_up_loc (location_t loc, tree value, int divisor)
15777 {
15778   tree div = NULL_TREE;
15779
15780   gcc_assert (divisor > 0);
15781   if (divisor == 1)
15782     return value;
15783
15784   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15785      have to do anything.  Only do this when we are not given a const,
15786      because in that case, this check is more expensive than just
15787      doing it.  */
15788   if (TREE_CODE (value) != INTEGER_CST)
15789     {
15790       div = build_int_cst (TREE_TYPE (value), divisor);
15791
15792       if (multiple_of_p (TREE_TYPE (value), value, div))
15793         return value;
15794     }
15795
15796   /* If divisor is a power of two, simplify this to bit manipulation.  */
15797   if (divisor == (divisor & -divisor))
15798     {
15799       if (TREE_CODE (value) == INTEGER_CST)
15800         {
15801           double_int val = tree_to_double_int (value);
15802           bool overflow_p;
15803
15804           if ((val.low & (divisor - 1)) == 0)
15805             return value;
15806
15807           overflow_p = TREE_OVERFLOW (value);
15808           val.low &= ~(divisor - 1);
15809           val.low += divisor;
15810           if (val.low == 0)
15811             {
15812               val.high++;
15813               if (val.high == 0)
15814                 overflow_p = true;
15815             }
15816
15817           return force_fit_type_double (TREE_TYPE (value), val,
15818                                         -1, overflow_p);
15819         }
15820       else
15821         {
15822           tree t;
15823
15824           t = build_int_cst (TREE_TYPE (value), divisor - 1);
15825           value = size_binop_loc (loc, PLUS_EXPR, value, t);
15826           t = build_int_cst (TREE_TYPE (value), -divisor);
15827           value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15828         }
15829     }
15830   else
15831     {
15832       if (!div)
15833         div = build_int_cst (TREE_TYPE (value), divisor);
15834       value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
15835       value = size_binop_loc (loc, MULT_EXPR, value, div);
15836     }
15837
15838   return value;
15839 }
15840
15841 /* Likewise, but round down.  */
15842
15843 tree
15844 round_down_loc (location_t loc, tree value, int divisor)
15845 {
15846   tree div = NULL_TREE;
15847
15848   gcc_assert (divisor > 0);
15849   if (divisor == 1)
15850     return value;
15851
15852   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15853      have to do anything.  Only do this when we are not given a const,
15854      because in that case, this check is more expensive than just
15855      doing it.  */
15856   if (TREE_CODE (value) != INTEGER_CST)
15857     {
15858       div = build_int_cst (TREE_TYPE (value), divisor);
15859
15860       if (multiple_of_p (TREE_TYPE (value), value, div))
15861         return value;
15862     }
15863
15864   /* If divisor is a power of two, simplify this to bit manipulation.  */
15865   if (divisor == (divisor & -divisor))
15866     {
15867       tree t;
15868
15869       t = build_int_cst (TREE_TYPE (value), -divisor);
15870       value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15871     }
15872   else
15873     {
15874       if (!div)
15875         div = build_int_cst (TREE_TYPE (value), divisor);
15876       value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
15877       value = size_binop_loc (loc, MULT_EXPR, value, div);
15878     }
15879
15880   return value;
15881 }
15882
15883 /* Returns the pointer to the base of the object addressed by EXP and
15884    extracts the information about the offset of the access, storing it
15885    to PBITPOS and POFFSET.  */
15886
15887 static tree
15888 split_address_to_core_and_offset (tree exp,
15889                                   HOST_WIDE_INT *pbitpos, tree *poffset)
15890 {
15891   tree core;
15892   enum machine_mode mode;
15893   int unsignedp, volatilep;
15894   HOST_WIDE_INT bitsize;
15895   location_t loc = EXPR_LOCATION (exp);
15896
15897   if (TREE_CODE (exp) == ADDR_EXPR)
15898     {
15899       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15900                                   poffset, &mode, &unsignedp, &volatilep,
15901                                   false);
15902       core = build_fold_addr_expr_loc (loc, core);
15903     }
15904   else
15905     {
15906       core = exp;
15907       *pbitpos = 0;
15908       *poffset = NULL_TREE;
15909     }
15910
15911   return core;
15912 }
15913
15914 /* Returns true if addresses of E1 and E2 differ by a constant, false
15915    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
15916
15917 bool
15918 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15919 {
15920   tree core1, core2;
15921   HOST_WIDE_INT bitpos1, bitpos2;
15922   tree toffset1, toffset2, tdiff, type;
15923
15924   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15925   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15926
15927   if (bitpos1 % BITS_PER_UNIT != 0
15928       || bitpos2 % BITS_PER_UNIT != 0
15929       || !operand_equal_p (core1, core2, 0))
15930     return false;
15931
15932   if (toffset1 && toffset2)
15933     {
15934       type = TREE_TYPE (toffset1);
15935       if (type != TREE_TYPE (toffset2))
15936         toffset2 = fold_convert (type, toffset2);
15937
15938       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15939       if (!cst_and_fits_in_hwi (tdiff))
15940         return false;
15941
15942       *diff = int_cst_value (tdiff);
15943     }
15944   else if (toffset1 || toffset2)
15945     {
15946       /* If only one of the offsets is non-constant, the difference cannot
15947          be a constant.  */
15948       return false;
15949     }
15950   else
15951     *diff = 0;
15952
15953   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15954   return true;
15955 }
15956
15957 /* Simplify the floating point expression EXP when the sign of the
15958    result is not significant.  Return NULL_TREE if no simplification
15959    is possible.  */
15960
15961 tree
15962 fold_strip_sign_ops (tree exp)
15963 {
15964   tree arg0, arg1;
15965   location_t loc = EXPR_LOCATION (exp);
15966
15967   switch (TREE_CODE (exp))
15968     {
15969     case ABS_EXPR:
15970     case NEGATE_EXPR:
15971       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15972       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15973
15974     case MULT_EXPR:
15975     case RDIV_EXPR:
15976       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15977         return NULL_TREE;
15978       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15979       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15980       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15981         return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
15982                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
15983                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
15984       break;
15985
15986     case COMPOUND_EXPR:
15987       arg0 = TREE_OPERAND (exp, 0);
15988       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15989       if (arg1)
15990         return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15991       break;
15992
15993     case COND_EXPR:
15994       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15995       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15996       if (arg0 || arg1)
15997         return fold_build3_loc (loc,
15998                             COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15999                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
16000                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
16001       break;
16002
16003     case CALL_EXPR:
16004       {
16005         const enum built_in_function fcode = builtin_mathfn_code (exp);
16006         switch (fcode)
16007         {
16008         CASE_FLT_FN (BUILT_IN_COPYSIGN):
16009           /* Strip copysign function call, return the 1st argument. */
16010           arg0 = CALL_EXPR_ARG (exp, 0);
16011           arg1 = CALL_EXPR_ARG (exp, 1);
16012           return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
16013
16014         default:
16015           /* Strip sign ops from the argument of "odd" math functions.  */
16016           if (negate_mathfn_p (fcode))
16017             {
16018               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
16019               if (arg0)
16020                 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
16021             }
16022           break;
16023         }
16024       }
16025       break;
16026
16027     default:
16028       break;
16029     }
16030   return NULL_TREE;
16031 }