OSDN Git Service

2012-01-04 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / fold-const.c
1 /* Fold a constant sub-tree into a single node for C-compiler
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /*@@ This file should be rewritten to use an arbitrary precision
23   @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
24   @@ Perhaps the routines could also be used for bc/dc, and made a lib.
25   @@ The routines that translate from the ap rep should
26   @@ warn if precision et. al. is lost.
27   @@ This would also make life easier when this technology is used
28   @@ for cross-compilers.  */
29
30 /* The entry points in this file are fold, size_int_wide and size_binop.
31
32    fold takes a tree as argument and returns a simplified tree.
33
34    size_binop takes a tree code for an arithmetic operation
35    and two operands that are trees, and produces a tree for the
36    result, assuming the type comes from `sizetype'.
37
38    size_int takes an integer value, and creates a tree constant
39    with type from `sizetype'.
40
41    Note: Since the folders get called on non-gimple code as well as
42    gimple code, we need to handle GIMPLE tuples as well as their
43    corresponding tree equivalents.  */
44
45 #include "config.h"
46 #include "system.h"
47 #include "coretypes.h"
48 #include "tm.h"
49 #include "flags.h"
50 #include "tree.h"
51 #include "realmpfr.h"
52 #include "rtl.h"
53 #include "expr.h"
54 #include "tm_p.h"
55 #include "target.h"
56 #include "diagnostic-core.h"
57 #include "intl.h"
58 #include "ggc.h"
59 #include "hashtab.h"
60 #include "langhooks.h"
61 #include "md5.h"
62 #include "gimple.h"
63 #include "tree-flow.h"
64
65 /* Nonzero if we are folding constants inside an initializer; zero
66    otherwise.  */
67 int folding_initializer = 0;
68
69 /* The following constants represent a bit based encoding of GCC's
70    comparison operators.  This encoding simplifies transformations
71    on relational comparison operators, such as AND and OR.  */
72 enum comparison_code {
73   COMPCODE_FALSE = 0,
74   COMPCODE_LT = 1,
75   COMPCODE_EQ = 2,
76   COMPCODE_LE = 3,
77   COMPCODE_GT = 4,
78   COMPCODE_LTGT = 5,
79   COMPCODE_GE = 6,
80   COMPCODE_ORD = 7,
81   COMPCODE_UNORD = 8,
82   COMPCODE_UNLT = 9,
83   COMPCODE_UNEQ = 10,
84   COMPCODE_UNLE = 11,
85   COMPCODE_UNGT = 12,
86   COMPCODE_NE = 13,
87   COMPCODE_UNGE = 14,
88   COMPCODE_TRUE = 15
89 };
90
91 static bool negate_mathfn_p (enum built_in_function);
92 static bool negate_expr_p (tree);
93 static tree negate_expr (tree);
94 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
95 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
96 static tree const_binop (enum tree_code, tree, tree);
97 static enum comparison_code comparison_to_compcode (enum tree_code);
98 static enum tree_code compcode_to_comparison (enum comparison_code);
99 static int operand_equal_for_comparison_p (tree, tree, tree);
100 static int twoval_comparison_p (tree, tree *, tree *, int *);
101 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
102 static tree pedantic_omit_one_operand_loc (location_t, tree, tree, tree);
103 static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree);
104 static tree make_bit_field_ref (location_t, tree, tree,
105                                 HOST_WIDE_INT, HOST_WIDE_INT, int);
106 static tree optimize_bit_field_compare (location_t, enum tree_code,
107                                         tree, tree, tree);
108 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
109                                     HOST_WIDE_INT *,
110                                     enum machine_mode *, int *, int *,
111                                     tree *, tree *);
112 static int all_ones_mask_p (const_tree, int);
113 static tree sign_bit_p (tree, const_tree);
114 static int simple_operand_p (const_tree);
115 static bool simple_operand_p_2 (tree);
116 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
117 static tree range_predecessor (tree);
118 static tree range_successor (tree);
119 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
120 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
121 static tree unextend (tree, int, int, tree);
122 static tree optimize_minmax_comparison (location_t, enum tree_code,
123                                         tree, tree, tree);
124 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
125 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
126 static tree fold_binary_op_with_conditional_arg (location_t,
127                                                  enum tree_code, tree,
128                                                  tree, tree,
129                                                  tree, tree, int);
130 static tree fold_mathfn_compare (location_t,
131                                  enum built_in_function, enum tree_code,
132                                  tree, tree, tree);
133 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
134 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
135 static bool reorder_operands_p (const_tree, const_tree);
136 static tree fold_negate_const (tree, tree);
137 static tree fold_not_const (const_tree, tree);
138 static tree fold_relational_const (enum tree_code, tree, tree, tree);
139 static tree fold_convert_const (enum tree_code, tree, tree);
140
141 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
142    Otherwise, return LOC.  */
143
144 static location_t
145 expr_location_or (tree t, location_t loc)
146 {
147   location_t tloc = EXPR_LOCATION (t);
148   return tloc != UNKNOWN_LOCATION ? tloc : loc;
149 }
150
151 /* Similar to protected_set_expr_location, but never modify x in place,
152    if location can and needs to be set, unshare it.  */
153
154 static inline tree
155 protected_set_expr_location_unshare (tree x, location_t loc)
156 {
157   if (CAN_HAVE_LOCATION_P (x)
158       && EXPR_LOCATION (x) != loc
159       && !(TREE_CODE (x) == SAVE_EXPR
160            || TREE_CODE (x) == TARGET_EXPR
161            || TREE_CODE (x) == BIND_EXPR))
162     {
163       x = copy_node (x);
164       SET_EXPR_LOCATION (x, loc);
165     }
166   return x;
167 }
168
169
170 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
171    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
172    and SUM1.  Then this yields nonzero if overflow occurred during the
173    addition.
174
175    Overflow occurs if A and B have the same sign, but A and SUM differ in
176    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
177    sign.  */
178 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
179 \f
180 /* If ARG2 divides ARG1 with zero remainder, carries out the division
181    of type CODE and returns the quotient.
182    Otherwise returns NULL_TREE.  */
183
184 tree
185 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
186 {
187   double_int quo, rem;
188   int uns;
189
190   /* The sign of the division is according to operand two, that
191      does the correct thing for POINTER_PLUS_EXPR where we want
192      a signed division.  */
193   uns = TYPE_UNSIGNED (TREE_TYPE (arg2));
194   if (TREE_CODE (TREE_TYPE (arg2)) == INTEGER_TYPE
195       && TYPE_IS_SIZETYPE (TREE_TYPE (arg2)))
196     uns = false;
197
198   quo = double_int_divmod (tree_to_double_int (arg1),
199                            tree_to_double_int (arg2),
200                            uns, code, &rem);
201
202   if (double_int_zero_p (rem))
203     return build_int_cst_wide (TREE_TYPE (arg1), quo.low, quo.high);
204
205   return NULL_TREE; 
206 }
207 \f
208 /* This is nonzero if we should defer warnings about undefined
209    overflow.  This facility exists because these warnings are a
210    special case.  The code to estimate loop iterations does not want
211    to issue any warnings, since it works with expressions which do not
212    occur in user code.  Various bits of cleanup code call fold(), but
213    only use the result if it has certain characteristics (e.g., is a
214    constant); that code only wants to issue a warning if the result is
215    used.  */
216
217 static int fold_deferring_overflow_warnings;
218
219 /* If a warning about undefined overflow is deferred, this is the
220    warning.  Note that this may cause us to turn two warnings into
221    one, but that is fine since it is sufficient to only give one
222    warning per expression.  */
223
224 static const char* fold_deferred_overflow_warning;
225
226 /* If a warning about undefined overflow is deferred, this is the
227    level at which the warning should be emitted.  */
228
229 static enum warn_strict_overflow_code fold_deferred_overflow_code;
230
231 /* Start deferring overflow warnings.  We could use a stack here to
232    permit nested calls, but at present it is not necessary.  */
233
234 void
235 fold_defer_overflow_warnings (void)
236 {
237   ++fold_deferring_overflow_warnings;
238 }
239
240 /* Stop deferring overflow warnings.  If there is a pending warning,
241    and ISSUE is true, then issue the warning if appropriate.  STMT is
242    the statement with which the warning should be associated (used for
243    location information); STMT may be NULL.  CODE is the level of the
244    warning--a warn_strict_overflow_code value.  This function will use
245    the smaller of CODE and the deferred code when deciding whether to
246    issue the warning.  CODE may be zero to mean to always use the
247    deferred code.  */
248
249 void
250 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
251 {
252   const char *warnmsg;
253   location_t locus;
254
255   gcc_assert (fold_deferring_overflow_warnings > 0);
256   --fold_deferring_overflow_warnings;
257   if (fold_deferring_overflow_warnings > 0)
258     {
259       if (fold_deferred_overflow_warning != NULL
260           && code != 0
261           && code < (int) fold_deferred_overflow_code)
262         fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
263       return;
264     }
265
266   warnmsg = fold_deferred_overflow_warning;
267   fold_deferred_overflow_warning = NULL;
268
269   if (!issue || warnmsg == NULL)
270     return;
271
272   if (gimple_no_warning_p (stmt))
273     return;
274
275   /* Use the smallest code level when deciding to issue the
276      warning.  */
277   if (code == 0 || code > (int) fold_deferred_overflow_code)
278     code = fold_deferred_overflow_code;
279
280   if (!issue_strict_overflow_warning (code))
281     return;
282
283   if (stmt == NULL)
284     locus = input_location;
285   else
286     locus = gimple_location (stmt);
287   warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
288 }
289
290 /* Stop deferring overflow warnings, ignoring any deferred
291    warnings.  */
292
293 void
294 fold_undefer_and_ignore_overflow_warnings (void)
295 {
296   fold_undefer_overflow_warnings (false, NULL, 0);
297 }
298
299 /* Whether we are deferring overflow warnings.  */
300
301 bool
302 fold_deferring_overflow_warnings_p (void)
303 {
304   return fold_deferring_overflow_warnings > 0;
305 }
306
307 /* This is called when we fold something based on the fact that signed
308    overflow is undefined.  */
309
310 static void
311 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
312 {
313   if (fold_deferring_overflow_warnings > 0)
314     {
315       if (fold_deferred_overflow_warning == NULL
316           || wc < fold_deferred_overflow_code)
317         {
318           fold_deferred_overflow_warning = gmsgid;
319           fold_deferred_overflow_code = wc;
320         }
321     }
322   else if (issue_strict_overflow_warning (wc))
323     warning (OPT_Wstrict_overflow, gmsgid);
324 }
325 \f
326 /* Return true if the built-in mathematical function specified by CODE
327    is odd, i.e. -f(x) == f(-x).  */
328
329 static bool
330 negate_mathfn_p (enum built_in_function code)
331 {
332   switch (code)
333     {
334     CASE_FLT_FN (BUILT_IN_ASIN):
335     CASE_FLT_FN (BUILT_IN_ASINH):
336     CASE_FLT_FN (BUILT_IN_ATAN):
337     CASE_FLT_FN (BUILT_IN_ATANH):
338     CASE_FLT_FN (BUILT_IN_CASIN):
339     CASE_FLT_FN (BUILT_IN_CASINH):
340     CASE_FLT_FN (BUILT_IN_CATAN):
341     CASE_FLT_FN (BUILT_IN_CATANH):
342     CASE_FLT_FN (BUILT_IN_CBRT):
343     CASE_FLT_FN (BUILT_IN_CPROJ):
344     CASE_FLT_FN (BUILT_IN_CSIN):
345     CASE_FLT_FN (BUILT_IN_CSINH):
346     CASE_FLT_FN (BUILT_IN_CTAN):
347     CASE_FLT_FN (BUILT_IN_CTANH):
348     CASE_FLT_FN (BUILT_IN_ERF):
349     CASE_FLT_FN (BUILT_IN_LLROUND):
350     CASE_FLT_FN (BUILT_IN_LROUND):
351     CASE_FLT_FN (BUILT_IN_ROUND):
352     CASE_FLT_FN (BUILT_IN_SIN):
353     CASE_FLT_FN (BUILT_IN_SINH):
354     CASE_FLT_FN (BUILT_IN_TAN):
355     CASE_FLT_FN (BUILT_IN_TANH):
356     CASE_FLT_FN (BUILT_IN_TRUNC):
357       return true;
358
359     CASE_FLT_FN (BUILT_IN_LLRINT):
360     CASE_FLT_FN (BUILT_IN_LRINT):
361     CASE_FLT_FN (BUILT_IN_NEARBYINT):
362     CASE_FLT_FN (BUILT_IN_RINT):
363       return !flag_rounding_math;
364
365     default:
366       break;
367     }
368   return false;
369 }
370
371 /* Check whether we may negate an integer constant T without causing
372    overflow.  */
373
374 bool
375 may_negate_without_overflow_p (const_tree t)
376 {
377   unsigned HOST_WIDE_INT val;
378   unsigned int prec;
379   tree type;
380
381   gcc_assert (TREE_CODE (t) == INTEGER_CST);
382
383   type = TREE_TYPE (t);
384   if (TYPE_UNSIGNED (type))
385     return false;
386
387   prec = TYPE_PRECISION (type);
388   if (prec > HOST_BITS_PER_WIDE_INT)
389     {
390       if (TREE_INT_CST_LOW (t) != 0)
391         return true;
392       prec -= HOST_BITS_PER_WIDE_INT;
393       val = TREE_INT_CST_HIGH (t);
394     }
395   else
396     val = TREE_INT_CST_LOW (t);
397   if (prec < HOST_BITS_PER_WIDE_INT)
398     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
399   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
400 }
401
402 /* Determine whether an expression T can be cheaply negated using
403    the function negate_expr without introducing undefined overflow.  */
404
405 static bool
406 negate_expr_p (tree t)
407 {
408   tree type;
409
410   if (t == 0)
411     return false;
412
413   type = TREE_TYPE (t);
414
415   STRIP_SIGN_NOPS (t);
416   switch (TREE_CODE (t))
417     {
418     case INTEGER_CST:
419       if (TYPE_OVERFLOW_WRAPS (type))
420         return true;
421
422       /* Check that -CST will not overflow type.  */
423       return may_negate_without_overflow_p (t);
424     case BIT_NOT_EXPR:
425       return (INTEGRAL_TYPE_P (type)
426               && TYPE_OVERFLOW_WRAPS (type));
427
428     case FIXED_CST:
429     case NEGATE_EXPR:
430       return true;
431
432     case REAL_CST:
433       /* We want to canonicalize to positive real constants.  Pretend
434          that only negative ones can be easily negated.  */
435       return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
436
437     case COMPLEX_CST:
438       return negate_expr_p (TREE_REALPART (t))
439              && negate_expr_p (TREE_IMAGPART (t));
440
441     case COMPLEX_EXPR:
442       return negate_expr_p (TREE_OPERAND (t, 0))
443              && negate_expr_p (TREE_OPERAND (t, 1));
444
445     case CONJ_EXPR:
446       return negate_expr_p (TREE_OPERAND (t, 0));
447
448     case PLUS_EXPR:
449       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
450           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
451         return false;
452       /* -(A + B) -> (-B) - A.  */
453       if (negate_expr_p (TREE_OPERAND (t, 1))
454           && reorder_operands_p (TREE_OPERAND (t, 0),
455                                  TREE_OPERAND (t, 1)))
456         return true;
457       /* -(A + B) -> (-A) - B.  */
458       return negate_expr_p (TREE_OPERAND (t, 0));
459
460     case MINUS_EXPR:
461       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
462       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
463              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
464              && reorder_operands_p (TREE_OPERAND (t, 0),
465                                     TREE_OPERAND (t, 1));
466
467     case MULT_EXPR:
468       if (TYPE_UNSIGNED (TREE_TYPE (t)))
469         break;
470
471       /* Fall through.  */
472
473     case RDIV_EXPR:
474       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
475         return negate_expr_p (TREE_OPERAND (t, 1))
476                || negate_expr_p (TREE_OPERAND (t, 0));
477       break;
478
479     case TRUNC_DIV_EXPR:
480     case ROUND_DIV_EXPR:
481     case FLOOR_DIV_EXPR:
482     case CEIL_DIV_EXPR:
483     case EXACT_DIV_EXPR:
484       /* In general we can't negate A / B, because if A is INT_MIN and
485          B is 1, we may turn this into INT_MIN / -1 which is undefined
486          and actually traps on some architectures.  But if overflow is
487          undefined, we can negate, because - (INT_MIN / 1) is an
488          overflow.  */
489       if (INTEGRAL_TYPE_P (TREE_TYPE (t))
490           && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
491         break;
492       return negate_expr_p (TREE_OPERAND (t, 1))
493              || negate_expr_p (TREE_OPERAND (t, 0));
494
495     case NOP_EXPR:
496       /* Negate -((double)float) as (double)(-float).  */
497       if (TREE_CODE (type) == REAL_TYPE)
498         {
499           tree tem = strip_float_extensions (t);
500           if (tem != t)
501             return negate_expr_p (tem);
502         }
503       break;
504
505     case CALL_EXPR:
506       /* Negate -f(x) as f(-x).  */
507       if (negate_mathfn_p (builtin_mathfn_code (t)))
508         return negate_expr_p (CALL_EXPR_ARG (t, 0));
509       break;
510
511     case RSHIFT_EXPR:
512       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
513       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
514         {
515           tree op1 = TREE_OPERAND (t, 1);
516           if (TREE_INT_CST_HIGH (op1) == 0
517               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
518                  == TREE_INT_CST_LOW (op1))
519             return true;
520         }
521       break;
522
523     default:
524       break;
525     }
526   return false;
527 }
528
529 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
530    simplification is possible.
531    If negate_expr_p would return true for T, NULL_TREE will never be
532    returned.  */
533
534 static tree
535 fold_negate_expr (location_t loc, tree t)
536 {
537   tree type = TREE_TYPE (t);
538   tree tem;
539
540   switch (TREE_CODE (t))
541     {
542     /* Convert - (~A) to A + 1.  */
543     case BIT_NOT_EXPR:
544       if (INTEGRAL_TYPE_P (type))
545         return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
546                             build_int_cst (type, 1));
547       break;
548
549     case INTEGER_CST:
550       tem = fold_negate_const (t, type);
551       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
552           || !TYPE_OVERFLOW_TRAPS (type))
553         return tem;
554       break;
555
556     case REAL_CST:
557       tem = fold_negate_const (t, type);
558       /* Two's complement FP formats, such as c4x, may overflow.  */
559       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
560         return tem;
561       break;
562
563     case FIXED_CST:
564       tem = fold_negate_const (t, type);
565       return tem;
566
567     case COMPLEX_CST:
568       {
569         tree rpart = negate_expr (TREE_REALPART (t));
570         tree ipart = negate_expr (TREE_IMAGPART (t));
571
572         if ((TREE_CODE (rpart) == REAL_CST
573              && TREE_CODE (ipart) == REAL_CST)
574             || (TREE_CODE (rpart) == INTEGER_CST
575                 && TREE_CODE (ipart) == INTEGER_CST))
576           return build_complex (type, rpart, ipart);
577       }
578       break;
579
580     case COMPLEX_EXPR:
581       if (negate_expr_p (t))
582         return fold_build2_loc (loc, COMPLEX_EXPR, type,
583                             fold_negate_expr (loc, TREE_OPERAND (t, 0)),
584                             fold_negate_expr (loc, TREE_OPERAND (t, 1)));
585       break;
586
587     case CONJ_EXPR:
588       if (negate_expr_p (t))
589         return fold_build1_loc (loc, CONJ_EXPR, type,
590                             fold_negate_expr (loc, TREE_OPERAND (t, 0)));
591       break;
592
593     case NEGATE_EXPR:
594       return TREE_OPERAND (t, 0);
595
596     case PLUS_EXPR:
597       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
598           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
599         {
600           /* -(A + B) -> (-B) - A.  */
601           if (negate_expr_p (TREE_OPERAND (t, 1))
602               && reorder_operands_p (TREE_OPERAND (t, 0),
603                                      TREE_OPERAND (t, 1)))
604             {
605               tem = negate_expr (TREE_OPERAND (t, 1));
606               return fold_build2_loc (loc, MINUS_EXPR, type,
607                                   tem, TREE_OPERAND (t, 0));
608             }
609
610           /* -(A + B) -> (-A) - B.  */
611           if (negate_expr_p (TREE_OPERAND (t, 0)))
612             {
613               tem = negate_expr (TREE_OPERAND (t, 0));
614               return fold_build2_loc (loc, MINUS_EXPR, type,
615                                   tem, TREE_OPERAND (t, 1));
616             }
617         }
618       break;
619
620     case MINUS_EXPR:
621       /* - (A - B) -> B - A  */
622       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
623           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
624           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
625         return fold_build2_loc (loc, MINUS_EXPR, type,
626                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
627       break;
628
629     case MULT_EXPR:
630       if (TYPE_UNSIGNED (type))
631         break;
632
633       /* Fall through.  */
634
635     case RDIV_EXPR:
636       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
637         {
638           tem = TREE_OPERAND (t, 1);
639           if (negate_expr_p (tem))
640             return fold_build2_loc (loc, TREE_CODE (t), type,
641                                 TREE_OPERAND (t, 0), negate_expr (tem));
642           tem = TREE_OPERAND (t, 0);
643           if (negate_expr_p (tem))
644             return fold_build2_loc (loc, TREE_CODE (t), type,
645                                 negate_expr (tem), TREE_OPERAND (t, 1));
646         }
647       break;
648
649     case TRUNC_DIV_EXPR:
650     case ROUND_DIV_EXPR:
651     case FLOOR_DIV_EXPR:
652     case CEIL_DIV_EXPR:
653     case EXACT_DIV_EXPR:
654       /* In general we can't negate A / B, because if A is INT_MIN and
655          B is 1, we may turn this into INT_MIN / -1 which is undefined
656          and actually traps on some architectures.  But if overflow is
657          undefined, we can negate, because - (INT_MIN / 1) is an
658          overflow.  */
659       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
660         {
661           const char * const warnmsg = G_("assuming signed overflow does not "
662                                           "occur when negating a division");
663           tem = TREE_OPERAND (t, 1);
664           if (negate_expr_p (tem))
665             {
666               if (INTEGRAL_TYPE_P (type)
667                   && (TREE_CODE (tem) != INTEGER_CST
668                       || integer_onep (tem)))
669                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
670               return fold_build2_loc (loc, TREE_CODE (t), type,
671                                   TREE_OPERAND (t, 0), negate_expr (tem));
672             }
673           tem = TREE_OPERAND (t, 0);
674           if (negate_expr_p (tem))
675             {
676               if (INTEGRAL_TYPE_P (type)
677                   && (TREE_CODE (tem) != INTEGER_CST
678                       || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
679                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
680               return fold_build2_loc (loc, TREE_CODE (t), type,
681                                   negate_expr (tem), TREE_OPERAND (t, 1));
682             }
683         }
684       break;
685
686     case NOP_EXPR:
687       /* Convert -((double)float) into (double)(-float).  */
688       if (TREE_CODE (type) == REAL_TYPE)
689         {
690           tem = strip_float_extensions (t);
691           if (tem != t && negate_expr_p (tem))
692             return fold_convert_loc (loc, type, negate_expr (tem));
693         }
694       break;
695
696     case CALL_EXPR:
697       /* Negate -f(x) as f(-x).  */
698       if (negate_mathfn_p (builtin_mathfn_code (t))
699           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
700         {
701           tree fndecl, arg;
702
703           fndecl = get_callee_fndecl (t);
704           arg = negate_expr (CALL_EXPR_ARG (t, 0));
705           return build_call_expr_loc (loc, fndecl, 1, arg);
706         }
707       break;
708
709     case RSHIFT_EXPR:
710       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
711       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
712         {
713           tree op1 = TREE_OPERAND (t, 1);
714           if (TREE_INT_CST_HIGH (op1) == 0
715               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
716                  == TREE_INT_CST_LOW (op1))
717             {
718               tree ntype = TYPE_UNSIGNED (type)
719                            ? signed_type_for (type)
720                            : unsigned_type_for (type);
721               tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
722               temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
723               return fold_convert_loc (loc, type, temp);
724             }
725         }
726       break;
727
728     default:
729       break;
730     }
731
732   return NULL_TREE;
733 }
734
735 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
736    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
737    return NULL_TREE. */
738
739 static tree
740 negate_expr (tree t)
741 {
742   tree type, tem;
743   location_t loc;
744
745   if (t == NULL_TREE)
746     return NULL_TREE;
747
748   loc = EXPR_LOCATION (t);
749   type = TREE_TYPE (t);
750   STRIP_SIGN_NOPS (t);
751
752   tem = fold_negate_expr (loc, t);
753   if (!tem)
754     tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
755   return fold_convert_loc (loc, type, tem);
756 }
757 \f
758 /* Split a tree IN into a constant, literal and variable parts that could be
759    combined with CODE to make IN.  "constant" means an expression with
760    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
761    commutative arithmetic operation.  Store the constant part into *CONP,
762    the literal in *LITP and return the variable part.  If a part isn't
763    present, set it to null.  If the tree does not decompose in this way,
764    return the entire tree as the variable part and the other parts as null.
765
766    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
767    case, we negate an operand that was subtracted.  Except if it is a
768    literal for which we use *MINUS_LITP instead.
769
770    If NEGATE_P is true, we are negating all of IN, again except a literal
771    for which we use *MINUS_LITP instead.
772
773    If IN is itself a literal or constant, return it as appropriate.
774
775    Note that we do not guarantee that any of the three values will be the
776    same type as IN, but they will have the same signedness and mode.  */
777
778 static tree
779 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
780             tree *minus_litp, int negate_p)
781 {
782   tree var = 0;
783
784   *conp = 0;
785   *litp = 0;
786   *minus_litp = 0;
787
788   /* Strip any conversions that don't change the machine mode or signedness.  */
789   STRIP_SIGN_NOPS (in);
790
791   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
792       || TREE_CODE (in) == FIXED_CST)
793     *litp = in;
794   else if (TREE_CODE (in) == code
795            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
796                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
797                /* We can associate addition and subtraction together (even
798                   though the C standard doesn't say so) for integers because
799                   the value is not affected.  For reals, the value might be
800                   affected, so we can't.  */
801                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
802                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
803     {
804       tree op0 = TREE_OPERAND (in, 0);
805       tree op1 = TREE_OPERAND (in, 1);
806       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
807       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
808
809       /* First see if either of the operands is a literal, then a constant.  */
810       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
811           || TREE_CODE (op0) == FIXED_CST)
812         *litp = op0, op0 = 0;
813       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
814                || TREE_CODE (op1) == FIXED_CST)
815         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
816
817       if (op0 != 0 && TREE_CONSTANT (op0))
818         *conp = op0, op0 = 0;
819       else if (op1 != 0 && TREE_CONSTANT (op1))
820         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
821
822       /* If we haven't dealt with either operand, this is not a case we can
823          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
824       if (op0 != 0 && op1 != 0)
825         var = in;
826       else if (op0 != 0)
827         var = op0;
828       else
829         var = op1, neg_var_p = neg1_p;
830
831       /* Now do any needed negations.  */
832       if (neg_litp_p)
833         *minus_litp = *litp, *litp = 0;
834       if (neg_conp_p)
835         *conp = negate_expr (*conp);
836       if (neg_var_p)
837         var = negate_expr (var);
838     }
839   else if (TREE_CONSTANT (in))
840     *conp = in;
841   else
842     var = in;
843
844   if (negate_p)
845     {
846       if (*litp)
847         *minus_litp = *litp, *litp = 0;
848       else if (*minus_litp)
849         *litp = *minus_litp, *minus_litp = 0;
850       *conp = negate_expr (*conp);
851       var = negate_expr (var);
852     }
853
854   return var;
855 }
856
857 /* Re-associate trees split by the above function.  T1 and T2 are
858    either expressions to associate or null.  Return the new
859    expression, if any.  LOC is the location of the new expression.  If
860    we build an operation, do it in TYPE and with CODE.  */
861
862 static tree
863 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
864 {
865   if (t1 == 0)
866     return t2;
867   else if (t2 == 0)
868     return t1;
869
870   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
871      try to fold this since we will have infinite recursion.  But do
872      deal with any NEGATE_EXPRs.  */
873   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
874       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
875     {
876       if (code == PLUS_EXPR)
877         {
878           if (TREE_CODE (t1) == NEGATE_EXPR)
879             return build2_loc (loc, MINUS_EXPR, type,
880                                fold_convert_loc (loc, type, t2),
881                                fold_convert_loc (loc, type,
882                                                  TREE_OPERAND (t1, 0)));
883           else if (TREE_CODE (t2) == NEGATE_EXPR)
884             return build2_loc (loc, MINUS_EXPR, type,
885                                fold_convert_loc (loc, type, t1),
886                                fold_convert_loc (loc, type,
887                                                  TREE_OPERAND (t2, 0)));
888           else if (integer_zerop (t2))
889             return fold_convert_loc (loc, type, t1);
890         }
891       else if (code == MINUS_EXPR)
892         {
893           if (integer_zerop (t2))
894             return fold_convert_loc (loc, type, t1);
895         }
896
897       return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
898                          fold_convert_loc (loc, type, t2));
899     }
900
901   return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
902                           fold_convert_loc (loc, type, t2));
903 }
904 \f
905 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
906    for use in int_const_binop, size_binop and size_diffop.  */
907
908 static bool
909 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
910 {
911   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
912     return false;
913   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
914     return false;
915
916   switch (code)
917     {
918     case LSHIFT_EXPR:
919     case RSHIFT_EXPR:
920     case LROTATE_EXPR:
921     case RROTATE_EXPR:
922       return true;
923
924     default:
925       break;
926     }
927
928   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
929          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
930          && TYPE_MODE (type1) == TYPE_MODE (type2);
931 }
932
933
934 /* Combine two integer constants ARG1 and ARG2 under operation CODE
935    to produce a new constant.  Return NULL_TREE if we don't know how
936    to evaluate CODE at compile-time.  */
937
938 tree
939 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
940 {
941   double_int op1, op2, res, tmp;
942   tree t;
943   tree type = TREE_TYPE (arg1);
944   bool uns = TYPE_UNSIGNED (type);
945   bool is_sizetype
946     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
947   bool overflow = false;
948
949   op1 = tree_to_double_int (arg1);
950   op2 = tree_to_double_int (arg2);
951
952   switch (code)
953     {
954     case BIT_IOR_EXPR:
955       res = double_int_ior (op1, op2);
956       break;
957
958     case BIT_XOR_EXPR:
959       res = double_int_xor (op1, op2);
960       break;
961
962     case BIT_AND_EXPR:
963       res = double_int_and (op1, op2);
964       break;
965
966     case RSHIFT_EXPR:
967       res = double_int_rshift (op1, double_int_to_shwi (op2),
968                                TYPE_PRECISION (type), !uns);
969       break;
970
971     case LSHIFT_EXPR:
972       /* It's unclear from the C standard whether shifts can overflow.
973          The following code ignores overflow; perhaps a C standard
974          interpretation ruling is needed.  */
975       res = double_int_lshift (op1, double_int_to_shwi (op2),
976                                TYPE_PRECISION (type), !uns);
977       break;
978
979     case RROTATE_EXPR:
980       res = double_int_rrotate (op1, double_int_to_shwi (op2),
981                                 TYPE_PRECISION (type));
982       break;
983
984     case LROTATE_EXPR:
985       res = double_int_lrotate (op1, double_int_to_shwi (op2),
986                                 TYPE_PRECISION (type));
987       break;
988
989     case PLUS_EXPR:
990       overflow = add_double (op1.low, op1.high, op2.low, op2.high,
991                              &res.low, &res.high);
992       break;
993
994     case MINUS_EXPR:
995       neg_double (op2.low, op2.high, &res.low, &res.high);
996       add_double (op1.low, op1.high, res.low, res.high,
997                   &res.low, &res.high);
998       overflow = OVERFLOW_SUM_SIGN (res.high, op2.high, op1.high);
999       break;
1000
1001     case MULT_EXPR:
1002       overflow = mul_double (op1.low, op1.high, op2.low, op2.high,
1003                              &res.low, &res.high);
1004       break;
1005
1006     case TRUNC_DIV_EXPR:
1007     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1008     case EXACT_DIV_EXPR:
1009       /* This is a shortcut for a common special case.  */
1010       if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1011           && !TREE_OVERFLOW (arg1)
1012           && !TREE_OVERFLOW (arg2)
1013           && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1014         {
1015           if (code == CEIL_DIV_EXPR)
1016             op1.low += op2.low - 1;
1017
1018           res.low = op1.low / op2.low, res.high = 0;
1019           break;
1020         }
1021
1022       /* ... fall through ...  */
1023
1024     case ROUND_DIV_EXPR:
1025       if (double_int_zero_p (op2))
1026         return NULL_TREE;
1027       if (double_int_one_p (op2))
1028         {
1029           res = op1;
1030           break;
1031         }
1032       if (double_int_equal_p (op1, op2)
1033           && ! double_int_zero_p (op1))
1034         {
1035           res = double_int_one;
1036           break;
1037         }
1038       overflow = div_and_round_double (code, uns,
1039                                        op1.low, op1.high, op2.low, op2.high,
1040                                        &res.low, &res.high,
1041                                        &tmp.low, &tmp.high);
1042       break;
1043
1044     case TRUNC_MOD_EXPR:
1045     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1046       /* This is a shortcut for a common special case.  */
1047       if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1048           && !TREE_OVERFLOW (arg1)
1049           && !TREE_OVERFLOW (arg2)
1050           && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1051         {
1052           if (code == CEIL_MOD_EXPR)
1053             op1.low += op2.low - 1;
1054           res.low = op1.low % op2.low, res.high = 0;
1055           break;
1056         }
1057
1058       /* ... fall through ...  */
1059
1060     case ROUND_MOD_EXPR:
1061       if (double_int_zero_p (op2))
1062         return NULL_TREE;
1063       overflow = div_and_round_double (code, uns,
1064                                        op1.low, op1.high, op2.low, op2.high,
1065                                        &tmp.low, &tmp.high,
1066                                        &res.low, &res.high);
1067       break;
1068
1069     case MIN_EXPR:
1070       res = double_int_min (op1, op2, uns);
1071       break;
1072
1073     case MAX_EXPR:
1074       res = double_int_max (op1, op2, uns);
1075       break;
1076
1077     default:
1078       return NULL_TREE;
1079     }
1080
1081   t = force_fit_type_double (TREE_TYPE (arg1), res, 1,
1082                              ((!uns || is_sizetype) && overflow)
1083                              | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1084
1085   return t;
1086 }
1087
1088 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1089    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1090    are the same kind of constant and the same machine mode.  Return zero if
1091    combining the constants is not allowed in the current operating mode.  */
1092
1093 static tree
1094 const_binop (enum tree_code code, tree arg1, tree arg2)
1095 {
1096   /* Sanity check for the recursive cases.  */
1097   if (!arg1 || !arg2)
1098     return NULL_TREE;
1099
1100   STRIP_NOPS (arg1);
1101   STRIP_NOPS (arg2);
1102
1103   if (TREE_CODE (arg1) == INTEGER_CST)
1104     return int_const_binop (code, arg1, arg2);
1105
1106   if (TREE_CODE (arg1) == REAL_CST)
1107     {
1108       enum machine_mode mode;
1109       REAL_VALUE_TYPE d1;
1110       REAL_VALUE_TYPE d2;
1111       REAL_VALUE_TYPE value;
1112       REAL_VALUE_TYPE result;
1113       bool inexact;
1114       tree t, type;
1115
1116       /* The following codes are handled by real_arithmetic.  */
1117       switch (code)
1118         {
1119         case PLUS_EXPR:
1120         case MINUS_EXPR:
1121         case MULT_EXPR:
1122         case RDIV_EXPR:
1123         case MIN_EXPR:
1124         case MAX_EXPR:
1125           break;
1126
1127         default:
1128           return NULL_TREE;
1129         }
1130
1131       d1 = TREE_REAL_CST (arg1);
1132       d2 = TREE_REAL_CST (arg2);
1133
1134       type = TREE_TYPE (arg1);
1135       mode = TYPE_MODE (type);
1136
1137       /* Don't perform operation if we honor signaling NaNs and
1138          either operand is a NaN.  */
1139       if (HONOR_SNANS (mode)
1140           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1141         return NULL_TREE;
1142
1143       /* Don't perform operation if it would raise a division
1144          by zero exception.  */
1145       if (code == RDIV_EXPR
1146           && REAL_VALUES_EQUAL (d2, dconst0)
1147           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1148         return NULL_TREE;
1149
1150       /* If either operand is a NaN, just return it.  Otherwise, set up
1151          for floating-point trap; we return an overflow.  */
1152       if (REAL_VALUE_ISNAN (d1))
1153         return arg1;
1154       else if (REAL_VALUE_ISNAN (d2))
1155         return arg2;
1156
1157       inexact = real_arithmetic (&value, code, &d1, &d2);
1158       real_convert (&result, mode, &value);
1159
1160       /* Don't constant fold this floating point operation if
1161          the result has overflowed and flag_trapping_math.  */
1162       if (flag_trapping_math
1163           && MODE_HAS_INFINITIES (mode)
1164           && REAL_VALUE_ISINF (result)
1165           && !REAL_VALUE_ISINF (d1)
1166           && !REAL_VALUE_ISINF (d2))
1167         return NULL_TREE;
1168
1169       /* Don't constant fold this floating point operation if the
1170          result may dependent upon the run-time rounding mode and
1171          flag_rounding_math is set, or if GCC's software emulation
1172          is unable to accurately represent the result.  */
1173       if ((flag_rounding_math
1174            || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1175           && (inexact || !real_identical (&result, &value)))
1176         return NULL_TREE;
1177
1178       t = build_real (type, result);
1179
1180       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1181       return t;
1182     }
1183
1184   if (TREE_CODE (arg1) == FIXED_CST)
1185     {
1186       FIXED_VALUE_TYPE f1;
1187       FIXED_VALUE_TYPE f2;
1188       FIXED_VALUE_TYPE result;
1189       tree t, type;
1190       int sat_p;
1191       bool overflow_p;
1192
1193       /* The following codes are handled by fixed_arithmetic.  */
1194       switch (code)
1195         {
1196         case PLUS_EXPR:
1197         case MINUS_EXPR:
1198         case MULT_EXPR:
1199         case TRUNC_DIV_EXPR:
1200           f2 = TREE_FIXED_CST (arg2);
1201           break;
1202
1203         case LSHIFT_EXPR:
1204         case RSHIFT_EXPR:
1205           f2.data.high = TREE_INT_CST_HIGH (arg2);
1206           f2.data.low = TREE_INT_CST_LOW (arg2);
1207           f2.mode = SImode;
1208           break;
1209
1210         default:
1211           return NULL_TREE;
1212         }
1213
1214       f1 = TREE_FIXED_CST (arg1);
1215       type = TREE_TYPE (arg1);
1216       sat_p = TYPE_SATURATING (type);
1217       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1218       t = build_fixed (type, result);
1219       /* Propagate overflow flags.  */
1220       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1221         TREE_OVERFLOW (t) = 1;
1222       return t;
1223     }
1224
1225   if (TREE_CODE (arg1) == COMPLEX_CST)
1226     {
1227       tree type = TREE_TYPE (arg1);
1228       tree r1 = TREE_REALPART (arg1);
1229       tree i1 = TREE_IMAGPART (arg1);
1230       tree r2 = TREE_REALPART (arg2);
1231       tree i2 = TREE_IMAGPART (arg2);
1232       tree real, imag;
1233
1234       switch (code)
1235         {
1236         case PLUS_EXPR:
1237         case MINUS_EXPR:
1238           real = const_binop (code, r1, r2);
1239           imag = const_binop (code, i1, i2);
1240           break;
1241
1242         case MULT_EXPR:
1243           if (COMPLEX_FLOAT_TYPE_P (type))
1244             return do_mpc_arg2 (arg1, arg2, type,
1245                                 /* do_nonfinite= */ folding_initializer,
1246                                 mpc_mul);
1247
1248           real = const_binop (MINUS_EXPR,
1249                               const_binop (MULT_EXPR, r1, r2),
1250                               const_binop (MULT_EXPR, i1, i2));
1251           imag = const_binop (PLUS_EXPR,
1252                               const_binop (MULT_EXPR, r1, i2),
1253                               const_binop (MULT_EXPR, i1, r2));
1254           break;
1255
1256         case RDIV_EXPR:
1257           if (COMPLEX_FLOAT_TYPE_P (type))
1258             return do_mpc_arg2 (arg1, arg2, type,
1259                                 /* do_nonfinite= */ folding_initializer,
1260                                 mpc_div);
1261           /* Fallthru ... */
1262         case TRUNC_DIV_EXPR:
1263         case CEIL_DIV_EXPR:
1264         case FLOOR_DIV_EXPR:
1265         case ROUND_DIV_EXPR:
1266           if (flag_complex_method == 0)
1267           {
1268             /* Keep this algorithm in sync with
1269                tree-complex.c:expand_complex_div_straight().
1270
1271                Expand complex division to scalars, straightforward algorithm.
1272                a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1273                t = br*br + bi*bi
1274             */
1275             tree magsquared
1276               = const_binop (PLUS_EXPR,
1277                              const_binop (MULT_EXPR, r2, r2),
1278                              const_binop (MULT_EXPR, i2, i2));
1279             tree t1
1280               = const_binop (PLUS_EXPR,
1281                              const_binop (MULT_EXPR, r1, r2),
1282                              const_binop (MULT_EXPR, i1, i2));
1283             tree t2
1284               = const_binop (MINUS_EXPR,
1285                              const_binop (MULT_EXPR, i1, r2),
1286                              const_binop (MULT_EXPR, r1, i2));
1287
1288             real = const_binop (code, t1, magsquared);
1289             imag = const_binop (code, t2, magsquared);
1290           }
1291           else
1292           {
1293             /* Keep this algorithm in sync with
1294                tree-complex.c:expand_complex_div_wide().
1295
1296                Expand complex division to scalars, modified algorithm to minimize
1297                overflow with wide input ranges.  */
1298             tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1299                                         fold_abs_const (r2, TREE_TYPE (type)),
1300                                         fold_abs_const (i2, TREE_TYPE (type)));
1301
1302             if (integer_nonzerop (compare))
1303               {
1304                 /* In the TRUE branch, we compute
1305                    ratio = br/bi;
1306                    div = (br * ratio) + bi;
1307                    tr = (ar * ratio) + ai;
1308                    ti = (ai * ratio) - ar;
1309                    tr = tr / div;
1310                    ti = ti / div;  */
1311                 tree ratio = const_binop (code, r2, i2);
1312                 tree div = const_binop (PLUS_EXPR, i2,
1313                                         const_binop (MULT_EXPR, r2, ratio));
1314                 real = const_binop (MULT_EXPR, r1, ratio);
1315                 real = const_binop (PLUS_EXPR, real, i1);
1316                 real = const_binop (code, real, div);
1317
1318                 imag = const_binop (MULT_EXPR, i1, ratio);
1319                 imag = const_binop (MINUS_EXPR, imag, r1);
1320                 imag = const_binop (code, imag, div);
1321               }
1322             else
1323               {
1324                 /* In the FALSE branch, we compute
1325                    ratio = d/c;
1326                    divisor = (d * ratio) + c;
1327                    tr = (b * ratio) + a;
1328                    ti = b - (a * ratio);
1329                    tr = tr / div;
1330                    ti = ti / div;  */
1331                 tree ratio = const_binop (code, i2, r2);
1332                 tree div = const_binop (PLUS_EXPR, r2,
1333                                         const_binop (MULT_EXPR, i2, ratio));
1334
1335                 real = const_binop (MULT_EXPR, i1, ratio);
1336                 real = const_binop (PLUS_EXPR, real, r1);
1337                 real = const_binop (code, real, div);
1338
1339                 imag = const_binop (MULT_EXPR, r1, ratio);
1340                 imag = const_binop (MINUS_EXPR, i1, imag);
1341                 imag = const_binop (code, imag, div);
1342               }
1343           }
1344           break;
1345
1346         default:
1347           return NULL_TREE;
1348         }
1349
1350       if (real && imag)
1351         return build_complex (type, real, imag);
1352     }
1353
1354   if (TREE_CODE (arg1) == VECTOR_CST)
1355     {
1356       tree type = TREE_TYPE(arg1);
1357       int count = TYPE_VECTOR_SUBPARTS (type), i;
1358       tree elements1, elements2, list = NULL_TREE;
1359
1360       if(TREE_CODE(arg2) != VECTOR_CST)
1361         return NULL_TREE;
1362
1363       elements1 = TREE_VECTOR_CST_ELTS (arg1);
1364       elements2 = TREE_VECTOR_CST_ELTS (arg2);
1365
1366       for (i = 0; i < count; i++)
1367         {
1368           tree elem1, elem2, elem;
1369
1370           /* The trailing elements can be empty and should be treated as 0 */
1371           if(!elements1)
1372             elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1373           else
1374             {
1375               elem1 = TREE_VALUE(elements1);
1376               elements1 = TREE_CHAIN (elements1);
1377             }
1378
1379           if(!elements2)
1380             elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1381           else
1382             {
1383               elem2 = TREE_VALUE(elements2);
1384               elements2 = TREE_CHAIN (elements2);
1385             }
1386
1387           elem = const_binop (code, elem1, elem2);
1388
1389           /* It is possible that const_binop cannot handle the given
1390             code and return NULL_TREE */
1391           if(elem == NULL_TREE)
1392             return NULL_TREE;
1393
1394           list = tree_cons (NULL_TREE, elem, list);
1395         }
1396       return build_vector(type, nreverse(list));
1397     }
1398   return NULL_TREE;
1399 }
1400
1401 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
1402    indicates which particular sizetype to create.  */
1403
1404 tree
1405 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1406 {
1407   return build_int_cst (sizetype_tab[(int) kind], number);
1408 }
1409 \f
1410 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
1411    is a tree code.  The type of the result is taken from the operands.
1412    Both must be equivalent integer types, ala int_binop_types_match_p.
1413    If the operands are constant, so is the result.  */
1414
1415 tree
1416 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1417 {
1418   tree type = TREE_TYPE (arg0);
1419
1420   if (arg0 == error_mark_node || arg1 == error_mark_node)
1421     return error_mark_node;
1422
1423   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1424                                        TREE_TYPE (arg1)));
1425
1426   /* Handle the special case of two integer constants faster.  */
1427   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1428     {
1429       /* And some specific cases even faster than that.  */
1430       if (code == PLUS_EXPR)
1431         {
1432           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1433             return arg1;
1434           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1435             return arg0;
1436         }
1437       else if (code == MINUS_EXPR)
1438         {
1439           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1440             return arg0;
1441         }
1442       else if (code == MULT_EXPR)
1443         {
1444           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1445             return arg1;
1446         }
1447
1448       /* Handle general case of two integer constants.  */
1449       return int_const_binop (code, arg0, arg1);
1450     }
1451
1452   return fold_build2_loc (loc, code, type, arg0, arg1);
1453 }
1454
1455 /* Given two values, either both of sizetype or both of bitsizetype,
1456    compute the difference between the two values.  Return the value
1457    in signed type corresponding to the type of the operands.  */
1458
1459 tree
1460 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1461 {
1462   tree type = TREE_TYPE (arg0);
1463   tree ctype;
1464
1465   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1466                                        TREE_TYPE (arg1)));
1467
1468   /* If the type is already signed, just do the simple thing.  */
1469   if (!TYPE_UNSIGNED (type))
1470     return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1471
1472   if (type == sizetype)
1473     ctype = ssizetype;
1474   else if (type == bitsizetype)
1475     ctype = sbitsizetype;
1476   else
1477     ctype = signed_type_for (type);
1478
1479   /* If either operand is not a constant, do the conversions to the signed
1480      type and subtract.  The hardware will do the right thing with any
1481      overflow in the subtraction.  */
1482   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1483     return size_binop_loc (loc, MINUS_EXPR,
1484                            fold_convert_loc (loc, ctype, arg0),
1485                            fold_convert_loc (loc, ctype, arg1));
1486
1487   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1488      Otherwise, subtract the other way, convert to CTYPE (we know that can't
1489      overflow) and negate (which can't either).  Special-case a result
1490      of zero while we're here.  */
1491   if (tree_int_cst_equal (arg0, arg1))
1492     return build_int_cst (ctype, 0);
1493   else if (tree_int_cst_lt (arg1, arg0))
1494     return fold_convert_loc (loc, ctype,
1495                              size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1496   else
1497     return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1498                            fold_convert_loc (loc, ctype,
1499                                              size_binop_loc (loc,
1500                                                              MINUS_EXPR,
1501                                                              arg1, arg0)));
1502 }
1503 \f
1504 /* A subroutine of fold_convert_const handling conversions of an
1505    INTEGER_CST to another integer type.  */
1506
1507 static tree
1508 fold_convert_const_int_from_int (tree type, const_tree arg1)
1509 {
1510   tree t;
1511
1512   /* Given an integer constant, make new constant with new type,
1513      appropriately sign-extended or truncated.  */
1514   t = force_fit_type_double (type, tree_to_double_int (arg1),
1515                              !POINTER_TYPE_P (TREE_TYPE (arg1)),
1516                              (TREE_INT_CST_HIGH (arg1) < 0
1517                               && (TYPE_UNSIGNED (type)
1518                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1519                              | TREE_OVERFLOW (arg1));
1520
1521   return t;
1522 }
1523
1524 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1525    to an integer type.  */
1526
1527 static tree
1528 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1529 {
1530   int overflow = 0;
1531   tree t;
1532
1533   /* The following code implements the floating point to integer
1534      conversion rules required by the Java Language Specification,
1535      that IEEE NaNs are mapped to zero and values that overflow
1536      the target precision saturate, i.e. values greater than
1537      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1538      are mapped to INT_MIN.  These semantics are allowed by the
1539      C and C++ standards that simply state that the behavior of
1540      FP-to-integer conversion is unspecified upon overflow.  */
1541
1542   double_int val;
1543   REAL_VALUE_TYPE r;
1544   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1545
1546   switch (code)
1547     {
1548     case FIX_TRUNC_EXPR:
1549       real_trunc (&r, VOIDmode, &x);
1550       break;
1551
1552     default:
1553       gcc_unreachable ();
1554     }
1555
1556   /* If R is NaN, return zero and show we have an overflow.  */
1557   if (REAL_VALUE_ISNAN (r))
1558     {
1559       overflow = 1;
1560       val = double_int_zero;
1561     }
1562
1563   /* See if R is less than the lower bound or greater than the
1564      upper bound.  */
1565
1566   if (! overflow)
1567     {
1568       tree lt = TYPE_MIN_VALUE (type);
1569       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1570       if (REAL_VALUES_LESS (r, l))
1571         {
1572           overflow = 1;
1573           val = tree_to_double_int (lt);
1574         }
1575     }
1576
1577   if (! overflow)
1578     {
1579       tree ut = TYPE_MAX_VALUE (type);
1580       if (ut)
1581         {
1582           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1583           if (REAL_VALUES_LESS (u, r))
1584             {
1585               overflow = 1;
1586               val = tree_to_double_int (ut);
1587             }
1588         }
1589     }
1590
1591   if (! overflow)
1592     real_to_integer2 ((HOST_WIDE_INT *) &val.low, &val.high, &r);
1593
1594   t = force_fit_type_double (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1595   return t;
1596 }
1597
1598 /* A subroutine of fold_convert_const handling conversions of a
1599    FIXED_CST to an integer type.  */
1600
1601 static tree
1602 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1603 {
1604   tree t;
1605   double_int temp, temp_trunc;
1606   unsigned int mode;
1607
1608   /* Right shift FIXED_CST to temp by fbit.  */
1609   temp = TREE_FIXED_CST (arg1).data;
1610   mode = TREE_FIXED_CST (arg1).mode;
1611   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
1612     {
1613       temp = double_int_rshift (temp, GET_MODE_FBIT (mode),
1614                                 HOST_BITS_PER_DOUBLE_INT,
1615                                 SIGNED_FIXED_POINT_MODE_P (mode));
1616
1617       /* Left shift temp to temp_trunc by fbit.  */
1618       temp_trunc = double_int_lshift (temp, GET_MODE_FBIT (mode),
1619                                       HOST_BITS_PER_DOUBLE_INT,
1620                                       SIGNED_FIXED_POINT_MODE_P (mode));
1621     }
1622   else
1623     {
1624       temp = double_int_zero;
1625       temp_trunc = double_int_zero;
1626     }
1627
1628   /* If FIXED_CST is negative, we need to round the value toward 0.
1629      By checking if the fractional bits are not zero to add 1 to temp.  */
1630   if (SIGNED_FIXED_POINT_MODE_P (mode)
1631       && double_int_negative_p (temp_trunc)
1632       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
1633     temp = double_int_add (temp, double_int_one);
1634
1635   /* Given a fixed-point constant, make new constant with new type,
1636      appropriately sign-extended or truncated.  */
1637   t = force_fit_type_double (type, temp, -1,
1638                              (double_int_negative_p (temp)
1639                               && (TYPE_UNSIGNED (type)
1640                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1641                              | TREE_OVERFLOW (arg1));
1642
1643   return t;
1644 }
1645
1646 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1647    to another floating point type.  */
1648
1649 static tree
1650 fold_convert_const_real_from_real (tree type, const_tree arg1)
1651 {
1652   REAL_VALUE_TYPE value;
1653   tree t;
1654
1655   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1656   t = build_real (type, value);
1657
1658   /* If converting an infinity or NAN to a representation that doesn't
1659      have one, set the overflow bit so that we can produce some kind of
1660      error message at the appropriate point if necessary.  It's not the
1661      most user-friendly message, but it's better than nothing.  */
1662   if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1663       && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1664     TREE_OVERFLOW (t) = 1;
1665   else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1666            && !MODE_HAS_NANS (TYPE_MODE (type)))
1667     TREE_OVERFLOW (t) = 1;
1668   /* Regular overflow, conversion produced an infinity in a mode that
1669      can't represent them.  */
1670   else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1671            && REAL_VALUE_ISINF (value)
1672            && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1673     TREE_OVERFLOW (t) = 1;
1674   else
1675     TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1676   return t;
1677 }
1678
1679 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1680    to a floating point type.  */
1681
1682 static tree
1683 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
1684 {
1685   REAL_VALUE_TYPE value;
1686   tree t;
1687
1688   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
1689   t = build_real (type, value);
1690
1691   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1692   return t;
1693 }
1694
1695 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1696    to another fixed-point type.  */
1697
1698 static tree
1699 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
1700 {
1701   FIXED_VALUE_TYPE value;
1702   tree t;
1703   bool overflow_p;
1704
1705   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
1706                               TYPE_SATURATING (type));
1707   t = build_fixed (type, value);
1708
1709   /* Propagate overflow flags.  */
1710   if (overflow_p | TREE_OVERFLOW (arg1))
1711     TREE_OVERFLOW (t) = 1;
1712   return t;
1713 }
1714
1715 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
1716    to a fixed-point type.  */
1717
1718 static tree
1719 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
1720 {
1721   FIXED_VALUE_TYPE value;
1722   tree t;
1723   bool overflow_p;
1724
1725   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
1726                                        TREE_INT_CST (arg1),
1727                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
1728                                        TYPE_SATURATING (type));
1729   t = build_fixed (type, value);
1730
1731   /* Propagate overflow flags.  */
1732   if (overflow_p | TREE_OVERFLOW (arg1))
1733     TREE_OVERFLOW (t) = 1;
1734   return t;
1735 }
1736
1737 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1738    to a fixed-point type.  */
1739
1740 static tree
1741 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
1742 {
1743   FIXED_VALUE_TYPE value;
1744   tree t;
1745   bool overflow_p;
1746
1747   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
1748                                         &TREE_REAL_CST (arg1),
1749                                         TYPE_SATURATING (type));
1750   t = build_fixed (type, value);
1751
1752   /* Propagate overflow flags.  */
1753   if (overflow_p | TREE_OVERFLOW (arg1))
1754     TREE_OVERFLOW (t) = 1;
1755   return t;
1756 }
1757
1758 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1759    type TYPE.  If no simplification can be done return NULL_TREE.  */
1760
1761 static tree
1762 fold_convert_const (enum tree_code code, tree type, tree arg1)
1763 {
1764   if (TREE_TYPE (arg1) == type)
1765     return arg1;
1766
1767   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
1768       || TREE_CODE (type) == OFFSET_TYPE)
1769     {
1770       if (TREE_CODE (arg1) == INTEGER_CST)
1771         return fold_convert_const_int_from_int (type, arg1);
1772       else if (TREE_CODE (arg1) == REAL_CST)
1773         return fold_convert_const_int_from_real (code, type, arg1);
1774       else if (TREE_CODE (arg1) == FIXED_CST)
1775         return fold_convert_const_int_from_fixed (type, arg1);
1776     }
1777   else if (TREE_CODE (type) == REAL_TYPE)
1778     {
1779       if (TREE_CODE (arg1) == INTEGER_CST)
1780         return build_real_from_int_cst (type, arg1);
1781       else if (TREE_CODE (arg1) == REAL_CST)
1782         return fold_convert_const_real_from_real (type, arg1);
1783       else if (TREE_CODE (arg1) == FIXED_CST)
1784         return fold_convert_const_real_from_fixed (type, arg1);
1785     }
1786   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
1787     {
1788       if (TREE_CODE (arg1) == FIXED_CST)
1789         return fold_convert_const_fixed_from_fixed (type, arg1);
1790       else if (TREE_CODE (arg1) == INTEGER_CST)
1791         return fold_convert_const_fixed_from_int (type, arg1);
1792       else if (TREE_CODE (arg1) == REAL_CST)
1793         return fold_convert_const_fixed_from_real (type, arg1);
1794     }
1795   return NULL_TREE;
1796 }
1797
1798 /* Construct a vector of zero elements of vector type TYPE.  */
1799
1800 static tree
1801 build_zero_vector (tree type)
1802 {
1803   tree t;
1804
1805   t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1806   return build_vector_from_val (type, t);
1807 }
1808
1809 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
1810
1811 bool
1812 fold_convertible_p (const_tree type, const_tree arg)
1813 {
1814   tree orig = TREE_TYPE (arg);
1815
1816   if (type == orig)
1817     return true;
1818
1819   if (TREE_CODE (arg) == ERROR_MARK
1820       || TREE_CODE (type) == ERROR_MARK
1821       || TREE_CODE (orig) == ERROR_MARK)
1822     return false;
1823
1824   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1825     return true;
1826
1827   switch (TREE_CODE (type))
1828     {
1829     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1830     case POINTER_TYPE: case REFERENCE_TYPE:
1831     case OFFSET_TYPE:
1832       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1833           || TREE_CODE (orig) == OFFSET_TYPE)
1834         return true;
1835       return (TREE_CODE (orig) == VECTOR_TYPE
1836               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1837
1838     case REAL_TYPE:
1839     case FIXED_POINT_TYPE:
1840     case COMPLEX_TYPE:
1841     case VECTOR_TYPE:
1842     case VOID_TYPE:
1843       return TREE_CODE (type) == TREE_CODE (orig);
1844
1845     default:
1846       return false;
1847     }
1848 }
1849
1850 /* Convert expression ARG to type TYPE.  Used by the middle-end for
1851    simple conversions in preference to calling the front-end's convert.  */
1852
1853 tree
1854 fold_convert_loc (location_t loc, tree type, tree arg)
1855 {
1856   tree orig = TREE_TYPE (arg);
1857   tree tem;
1858
1859   if (type == orig)
1860     return arg;
1861
1862   if (TREE_CODE (arg) == ERROR_MARK
1863       || TREE_CODE (type) == ERROR_MARK
1864       || TREE_CODE (orig) == ERROR_MARK)
1865     return error_mark_node;
1866
1867   switch (TREE_CODE (type))
1868     {
1869     case POINTER_TYPE:
1870     case REFERENCE_TYPE:
1871       /* Handle conversions between pointers to different address spaces.  */
1872       if (POINTER_TYPE_P (orig)
1873           && (TYPE_ADDR_SPACE (TREE_TYPE (type))
1874               != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
1875         return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
1876       /* fall through */
1877
1878     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1879     case OFFSET_TYPE:
1880       if (TREE_CODE (arg) == INTEGER_CST)
1881         {
1882           tem = fold_convert_const (NOP_EXPR, type, arg);
1883           if (tem != NULL_TREE)
1884             return tem;
1885         }
1886       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1887           || TREE_CODE (orig) == OFFSET_TYPE)
1888         return fold_build1_loc (loc, NOP_EXPR, type, arg);
1889       if (TREE_CODE (orig) == COMPLEX_TYPE)
1890         return fold_convert_loc (loc, type,
1891                              fold_build1_loc (loc, REALPART_EXPR,
1892                                           TREE_TYPE (orig), arg));
1893       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
1894                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1895       return fold_build1_loc (loc, NOP_EXPR, type, arg);
1896
1897     case REAL_TYPE:
1898       if (TREE_CODE (arg) == INTEGER_CST)
1899         {
1900           tem = fold_convert_const (FLOAT_EXPR, type, arg);
1901           if (tem != NULL_TREE)
1902             return tem;
1903         }
1904       else if (TREE_CODE (arg) == REAL_CST)
1905         {
1906           tem = fold_convert_const (NOP_EXPR, type, arg);
1907           if (tem != NULL_TREE)
1908             return tem;
1909         }
1910       else if (TREE_CODE (arg) == FIXED_CST)
1911         {
1912           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1913           if (tem != NULL_TREE)
1914             return tem;
1915         }
1916
1917       switch (TREE_CODE (orig))
1918         {
1919         case INTEGER_TYPE:
1920         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1921         case POINTER_TYPE: case REFERENCE_TYPE:
1922           return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
1923
1924         case REAL_TYPE:
1925           return fold_build1_loc (loc, NOP_EXPR, type, arg);
1926
1927         case FIXED_POINT_TYPE:
1928           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1929
1930         case COMPLEX_TYPE:
1931           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1932           return fold_convert_loc (loc, type, tem);
1933
1934         default:
1935           gcc_unreachable ();
1936         }
1937
1938     case FIXED_POINT_TYPE:
1939       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
1940           || TREE_CODE (arg) == REAL_CST)
1941         {
1942           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1943           if (tem != NULL_TREE)
1944             goto fold_convert_exit;
1945         }
1946
1947       switch (TREE_CODE (orig))
1948         {
1949         case FIXED_POINT_TYPE:
1950         case INTEGER_TYPE:
1951         case ENUMERAL_TYPE:
1952         case BOOLEAN_TYPE:
1953         case REAL_TYPE:
1954           return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1955
1956         case COMPLEX_TYPE:
1957           tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1958           return fold_convert_loc (loc, type, tem);
1959
1960         default:
1961           gcc_unreachable ();
1962         }
1963
1964     case COMPLEX_TYPE:
1965       switch (TREE_CODE (orig))
1966         {
1967         case INTEGER_TYPE:
1968         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1969         case POINTER_TYPE: case REFERENCE_TYPE:
1970         case REAL_TYPE:
1971         case FIXED_POINT_TYPE:
1972           return fold_build2_loc (loc, COMPLEX_EXPR, type,
1973                               fold_convert_loc (loc, TREE_TYPE (type), arg),
1974                               fold_convert_loc (loc, TREE_TYPE (type),
1975                                             integer_zero_node));
1976         case COMPLEX_TYPE:
1977           {
1978             tree rpart, ipart;
1979
1980             if (TREE_CODE (arg) == COMPLEX_EXPR)
1981               {
1982                 rpart = fold_convert_loc (loc, TREE_TYPE (type),
1983                                       TREE_OPERAND (arg, 0));
1984                 ipart = fold_convert_loc (loc, TREE_TYPE (type),
1985                                       TREE_OPERAND (arg, 1));
1986                 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
1987               }
1988
1989             arg = save_expr (arg);
1990             rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1991             ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
1992             rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
1993             ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
1994             return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
1995           }
1996
1997         default:
1998           gcc_unreachable ();
1999         }
2000
2001     case VECTOR_TYPE:
2002       if (integer_zerop (arg))
2003         return build_zero_vector (type);
2004       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2005       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2006                   || TREE_CODE (orig) == VECTOR_TYPE);
2007       return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2008
2009     case VOID_TYPE:
2010       tem = fold_ignored_result (arg);
2011       return fold_build1_loc (loc, NOP_EXPR, type, tem);
2012
2013     default:
2014       if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2015         return fold_build1_loc (loc, NOP_EXPR, type, arg);
2016       gcc_unreachable ();
2017     }
2018  fold_convert_exit:
2019   protected_set_expr_location_unshare (tem, loc);
2020   return tem;
2021 }
2022 \f
2023 /* Return false if expr can be assumed not to be an lvalue, true
2024    otherwise.  */
2025
2026 static bool
2027 maybe_lvalue_p (const_tree x)
2028 {
2029   /* We only need to wrap lvalue tree codes.  */
2030   switch (TREE_CODE (x))
2031   {
2032   case VAR_DECL:
2033   case PARM_DECL:
2034   case RESULT_DECL:
2035   case LABEL_DECL:
2036   case FUNCTION_DECL:
2037   case SSA_NAME:
2038
2039   case COMPONENT_REF:
2040   case MEM_REF:
2041   case INDIRECT_REF:
2042   case ARRAY_REF:
2043   case ARRAY_RANGE_REF:
2044   case BIT_FIELD_REF:
2045   case OBJ_TYPE_REF:
2046
2047   case REALPART_EXPR:
2048   case IMAGPART_EXPR:
2049   case PREINCREMENT_EXPR:
2050   case PREDECREMENT_EXPR:
2051   case SAVE_EXPR:
2052   case TRY_CATCH_EXPR:
2053   case WITH_CLEANUP_EXPR:
2054   case COMPOUND_EXPR:
2055   case MODIFY_EXPR:
2056   case TARGET_EXPR:
2057   case COND_EXPR:
2058   case BIND_EXPR:
2059     break;
2060
2061   default:
2062     /* Assume the worst for front-end tree codes.  */
2063     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2064       break;
2065     return false;
2066   }
2067
2068   return true;
2069 }
2070
2071 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2072
2073 tree
2074 non_lvalue_loc (location_t loc, tree x)
2075 {
2076   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2077      us.  */
2078   if (in_gimple_form)
2079     return x;
2080
2081   if (! maybe_lvalue_p (x))
2082     return x;
2083   return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2084 }
2085
2086 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2087    Zero means allow extended lvalues.  */
2088
2089 int pedantic_lvalues;
2090
2091 /* When pedantic, return an expr equal to X but certainly not valid as a
2092    pedantic lvalue.  Otherwise, return X.  */
2093
2094 static tree
2095 pedantic_non_lvalue_loc (location_t loc, tree x)
2096 {
2097   if (pedantic_lvalues)
2098     return non_lvalue_loc (loc, x);
2099
2100   return protected_set_expr_location_unshare (x, loc);
2101 }
2102 \f
2103 /* Given a tree comparison code, return the code that is the logical inverse.
2104    It is generally not safe to do this for floating-point comparisons, except
2105    for EQ_EXPR and NE_EXPR, so we return ERROR_MARK in this case.  */
2106
2107 enum tree_code
2108 invert_tree_comparison (enum tree_code code, bool honor_nans)
2109 {
2110   if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR)
2111     return ERROR_MARK;
2112
2113   switch (code)
2114     {
2115     case EQ_EXPR:
2116       return NE_EXPR;
2117     case NE_EXPR:
2118       return EQ_EXPR;
2119     case GT_EXPR:
2120       return honor_nans ? UNLE_EXPR : LE_EXPR;
2121     case GE_EXPR:
2122       return honor_nans ? UNLT_EXPR : LT_EXPR;
2123     case LT_EXPR:
2124       return honor_nans ? UNGE_EXPR : GE_EXPR;
2125     case LE_EXPR:
2126       return honor_nans ? UNGT_EXPR : GT_EXPR;
2127     case LTGT_EXPR:
2128       return UNEQ_EXPR;
2129     case UNEQ_EXPR:
2130       return LTGT_EXPR;
2131     case UNGT_EXPR:
2132       return LE_EXPR;
2133     case UNGE_EXPR:
2134       return LT_EXPR;
2135     case UNLT_EXPR:
2136       return GE_EXPR;
2137     case UNLE_EXPR:
2138       return GT_EXPR;
2139     case ORDERED_EXPR:
2140       return UNORDERED_EXPR;
2141     case UNORDERED_EXPR:
2142       return ORDERED_EXPR;
2143     default:
2144       gcc_unreachable ();
2145     }
2146 }
2147
2148 /* Similar, but return the comparison that results if the operands are
2149    swapped.  This is safe for floating-point.  */
2150
2151 enum tree_code
2152 swap_tree_comparison (enum tree_code code)
2153 {
2154   switch (code)
2155     {
2156     case EQ_EXPR:
2157     case NE_EXPR:
2158     case ORDERED_EXPR:
2159     case UNORDERED_EXPR:
2160     case LTGT_EXPR:
2161     case UNEQ_EXPR:
2162       return code;
2163     case GT_EXPR:
2164       return LT_EXPR;
2165     case GE_EXPR:
2166       return LE_EXPR;
2167     case LT_EXPR:
2168       return GT_EXPR;
2169     case LE_EXPR:
2170       return GE_EXPR;
2171     case UNGT_EXPR:
2172       return UNLT_EXPR;
2173     case UNGE_EXPR:
2174       return UNLE_EXPR;
2175     case UNLT_EXPR:
2176       return UNGT_EXPR;
2177     case UNLE_EXPR:
2178       return UNGE_EXPR;
2179     default:
2180       gcc_unreachable ();
2181     }
2182 }
2183
2184
2185 /* Convert a comparison tree code from an enum tree_code representation
2186    into a compcode bit-based encoding.  This function is the inverse of
2187    compcode_to_comparison.  */
2188
2189 static enum comparison_code
2190 comparison_to_compcode (enum tree_code code)
2191 {
2192   switch (code)
2193     {
2194     case LT_EXPR:
2195       return COMPCODE_LT;
2196     case EQ_EXPR:
2197       return COMPCODE_EQ;
2198     case LE_EXPR:
2199       return COMPCODE_LE;
2200     case GT_EXPR:
2201       return COMPCODE_GT;
2202     case NE_EXPR:
2203       return COMPCODE_NE;
2204     case GE_EXPR:
2205       return COMPCODE_GE;
2206     case ORDERED_EXPR:
2207       return COMPCODE_ORD;
2208     case UNORDERED_EXPR:
2209       return COMPCODE_UNORD;
2210     case UNLT_EXPR:
2211       return COMPCODE_UNLT;
2212     case UNEQ_EXPR:
2213       return COMPCODE_UNEQ;
2214     case UNLE_EXPR:
2215       return COMPCODE_UNLE;
2216     case UNGT_EXPR:
2217       return COMPCODE_UNGT;
2218     case LTGT_EXPR:
2219       return COMPCODE_LTGT;
2220     case UNGE_EXPR:
2221       return COMPCODE_UNGE;
2222     default:
2223       gcc_unreachable ();
2224     }
2225 }
2226
2227 /* Convert a compcode bit-based encoding of a comparison operator back
2228    to GCC's enum tree_code representation.  This function is the
2229    inverse of comparison_to_compcode.  */
2230
2231 static enum tree_code
2232 compcode_to_comparison (enum comparison_code code)
2233 {
2234   switch (code)
2235     {
2236     case COMPCODE_LT:
2237       return LT_EXPR;
2238     case COMPCODE_EQ:
2239       return EQ_EXPR;
2240     case COMPCODE_LE:
2241       return LE_EXPR;
2242     case COMPCODE_GT:
2243       return GT_EXPR;
2244     case COMPCODE_NE:
2245       return NE_EXPR;
2246     case COMPCODE_GE:
2247       return GE_EXPR;
2248     case COMPCODE_ORD:
2249       return ORDERED_EXPR;
2250     case COMPCODE_UNORD:
2251       return UNORDERED_EXPR;
2252     case COMPCODE_UNLT:
2253       return UNLT_EXPR;
2254     case COMPCODE_UNEQ:
2255       return UNEQ_EXPR;
2256     case COMPCODE_UNLE:
2257       return UNLE_EXPR;
2258     case COMPCODE_UNGT:
2259       return UNGT_EXPR;
2260     case COMPCODE_LTGT:
2261       return LTGT_EXPR;
2262     case COMPCODE_UNGE:
2263       return UNGE_EXPR;
2264     default:
2265       gcc_unreachable ();
2266     }
2267 }
2268
2269 /* Return a tree for the comparison which is the combination of
2270    doing the AND or OR (depending on CODE) of the two operations LCODE
2271    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2272    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2273    if this makes the transformation invalid.  */
2274
2275 tree
2276 combine_comparisons (location_t loc,
2277                      enum tree_code code, enum tree_code lcode,
2278                      enum tree_code rcode, tree truth_type,
2279                      tree ll_arg, tree lr_arg)
2280 {
2281   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2282   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2283   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2284   int compcode;
2285
2286   switch (code)
2287     {
2288     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2289       compcode = lcompcode & rcompcode;
2290       break;
2291
2292     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2293       compcode = lcompcode | rcompcode;
2294       break;
2295
2296     default:
2297       return NULL_TREE;
2298     }
2299
2300   if (!honor_nans)
2301     {
2302       /* Eliminate unordered comparisons, as well as LTGT and ORD
2303          which are not used unless the mode has NaNs.  */
2304       compcode &= ~COMPCODE_UNORD;
2305       if (compcode == COMPCODE_LTGT)
2306         compcode = COMPCODE_NE;
2307       else if (compcode == COMPCODE_ORD)
2308         compcode = COMPCODE_TRUE;
2309     }
2310    else if (flag_trapping_math)
2311      {
2312         /* Check that the original operation and the optimized ones will trap
2313            under the same condition.  */
2314         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2315                      && (lcompcode != COMPCODE_EQ)
2316                      && (lcompcode != COMPCODE_ORD);
2317         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2318                      && (rcompcode != COMPCODE_EQ)
2319                      && (rcompcode != COMPCODE_ORD);
2320         bool trap = (compcode & COMPCODE_UNORD) == 0
2321                     && (compcode != COMPCODE_EQ)
2322                     && (compcode != COMPCODE_ORD);
2323
2324         /* In a short-circuited boolean expression the LHS might be
2325            such that the RHS, if evaluated, will never trap.  For
2326            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2327            if neither x nor y is NaN.  (This is a mixed blessing: for
2328            example, the expression above will never trap, hence
2329            optimizing it to x < y would be invalid).  */
2330         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2331             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2332           rtrap = false;
2333
2334         /* If the comparison was short-circuited, and only the RHS
2335            trapped, we may now generate a spurious trap.  */
2336         if (rtrap && !ltrap
2337             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2338           return NULL_TREE;
2339
2340         /* If we changed the conditions that cause a trap, we lose.  */
2341         if ((ltrap || rtrap) != trap)
2342           return NULL_TREE;
2343       }
2344
2345   if (compcode == COMPCODE_TRUE)
2346     return constant_boolean_node (true, truth_type);
2347   else if (compcode == COMPCODE_FALSE)
2348     return constant_boolean_node (false, truth_type);
2349   else
2350     {
2351       enum tree_code tcode;
2352
2353       tcode = compcode_to_comparison ((enum comparison_code) compcode);
2354       return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2355     }
2356 }
2357 \f
2358 /* Return nonzero if two operands (typically of the same tree node)
2359    are necessarily equal.  If either argument has side-effects this
2360    function returns zero.  FLAGS modifies behavior as follows:
2361
2362    If OEP_ONLY_CONST is set, only return nonzero for constants.
2363    This function tests whether the operands are indistinguishable;
2364    it does not test whether they are equal using C's == operation.
2365    The distinction is important for IEEE floating point, because
2366    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2367    (2) two NaNs may be indistinguishable, but NaN!=NaN.
2368
2369    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2370    even though it may hold multiple values during a function.
2371    This is because a GCC tree node guarantees that nothing else is
2372    executed between the evaluation of its "operands" (which may often
2373    be evaluated in arbitrary order).  Hence if the operands themselves
2374    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2375    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
2376    unset means assuming isochronic (or instantaneous) tree equivalence.
2377    Unless comparing arbitrary expression trees, such as from different
2378    statements, this flag can usually be left unset.
2379
2380    If OEP_PURE_SAME is set, then pure functions with identical arguments
2381    are considered the same.  It is used when the caller has other ways
2382    to ensure that global memory is unchanged in between.  */
2383
2384 int
2385 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2386 {
2387   /* If either is ERROR_MARK, they aren't equal.  */
2388   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2389       || TREE_TYPE (arg0) == error_mark_node
2390       || TREE_TYPE (arg1) == error_mark_node)
2391     return 0;
2392
2393   /* Similar, if either does not have a type (like a released SSA name), 
2394      they aren't equal.  */
2395   if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2396     return 0;
2397
2398   /* Check equality of integer constants before bailing out due to
2399      precision differences.  */
2400   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2401     return tree_int_cst_equal (arg0, arg1);
2402
2403   /* If both types don't have the same signedness, then we can't consider
2404      them equal.  We must check this before the STRIP_NOPS calls
2405      because they may change the signedness of the arguments.  As pointers
2406      strictly don't have a signedness, require either two pointers or
2407      two non-pointers as well.  */
2408   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2409       || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2410     return 0;
2411
2412   /* We cannot consider pointers to different address space equal.  */
2413   if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2414       && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2415           != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2416     return 0;
2417
2418   /* If both types don't have the same precision, then it is not safe
2419      to strip NOPs.  */
2420   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
2421     return 0;
2422
2423   STRIP_NOPS (arg0);
2424   STRIP_NOPS (arg1);
2425
2426   /* In case both args are comparisons but with different comparison
2427      code, try to swap the comparison operands of one arg to produce
2428      a match and compare that variant.  */
2429   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2430       && COMPARISON_CLASS_P (arg0)
2431       && COMPARISON_CLASS_P (arg1))
2432     {
2433       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2434
2435       if (TREE_CODE (arg0) == swap_code)
2436         return operand_equal_p (TREE_OPERAND (arg0, 0),
2437                                 TREE_OPERAND (arg1, 1), flags)
2438                && operand_equal_p (TREE_OPERAND (arg0, 1),
2439                                    TREE_OPERAND (arg1, 0), flags);
2440     }
2441
2442   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2443       /* This is needed for conversions and for COMPONENT_REF.
2444          Might as well play it safe and always test this.  */
2445       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2446       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2447       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2448     return 0;
2449
2450   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2451      We don't care about side effects in that case because the SAVE_EXPR
2452      takes care of that for us. In all other cases, two expressions are
2453      equal if they have no side effects.  If we have two identical
2454      expressions with side effects that should be treated the same due
2455      to the only side effects being identical SAVE_EXPR's, that will
2456      be detected in the recursive calls below.
2457      If we are taking an invariant address of two identical objects
2458      they are necessarily equal as well.  */
2459   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2460       && (TREE_CODE (arg0) == SAVE_EXPR
2461           || (flags & OEP_CONSTANT_ADDRESS_OF)
2462           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2463     return 1;
2464
2465   /* Next handle constant cases, those for which we can return 1 even
2466      if ONLY_CONST is set.  */
2467   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2468     switch (TREE_CODE (arg0))
2469       {
2470       case INTEGER_CST:
2471         return tree_int_cst_equal (arg0, arg1);
2472
2473       case FIXED_CST:
2474         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2475                                        TREE_FIXED_CST (arg1));
2476
2477       case REAL_CST:
2478         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2479                                    TREE_REAL_CST (arg1)))
2480           return 1;
2481
2482
2483         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
2484           {
2485             /* If we do not distinguish between signed and unsigned zero,
2486                consider them equal.  */
2487             if (real_zerop (arg0) && real_zerop (arg1))
2488               return 1;
2489           }
2490         return 0;
2491
2492       case VECTOR_CST:
2493         {
2494           tree v1, v2;
2495
2496           v1 = TREE_VECTOR_CST_ELTS (arg0);
2497           v2 = TREE_VECTOR_CST_ELTS (arg1);
2498           while (v1 && v2)
2499             {
2500               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2501                                     flags))
2502                 return 0;
2503               v1 = TREE_CHAIN (v1);
2504               v2 = TREE_CHAIN (v2);
2505             }
2506
2507           return v1 == v2;
2508         }
2509
2510       case COMPLEX_CST:
2511         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2512                                  flags)
2513                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2514                                     flags));
2515
2516       case STRING_CST:
2517         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2518                 && ! memcmp (TREE_STRING_POINTER (arg0),
2519                               TREE_STRING_POINTER (arg1),
2520                               TREE_STRING_LENGTH (arg0)));
2521
2522       case ADDR_EXPR:
2523         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2524                                 TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1)
2525                                 ? OEP_CONSTANT_ADDRESS_OF : 0);
2526       default:
2527         break;
2528       }
2529
2530   if (flags & OEP_ONLY_CONST)
2531     return 0;
2532
2533 /* Define macros to test an operand from arg0 and arg1 for equality and a
2534    variant that allows null and views null as being different from any
2535    non-null value.  In the latter case, if either is null, the both
2536    must be; otherwise, do the normal comparison.  */
2537 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
2538                                     TREE_OPERAND (arg1, N), flags)
2539
2540 #define OP_SAME_WITH_NULL(N)                            \
2541   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2542    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2543
2544   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2545     {
2546     case tcc_unary:
2547       /* Two conversions are equal only if signedness and modes match.  */
2548       switch (TREE_CODE (arg0))
2549         {
2550         CASE_CONVERT:
2551         case FIX_TRUNC_EXPR:
2552           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2553               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2554             return 0;
2555           break;
2556         default:
2557           break;
2558         }
2559
2560       return OP_SAME (0);
2561
2562
2563     case tcc_comparison:
2564     case tcc_binary:
2565       if (OP_SAME (0) && OP_SAME (1))
2566         return 1;
2567
2568       /* For commutative ops, allow the other order.  */
2569       return (commutative_tree_code (TREE_CODE (arg0))
2570               && operand_equal_p (TREE_OPERAND (arg0, 0),
2571                                   TREE_OPERAND (arg1, 1), flags)
2572               && operand_equal_p (TREE_OPERAND (arg0, 1),
2573                                   TREE_OPERAND (arg1, 0), flags));
2574
2575     case tcc_reference:
2576       /* If either of the pointer (or reference) expressions we are
2577          dereferencing contain a side effect, these cannot be equal.  */
2578       if (TREE_SIDE_EFFECTS (arg0)
2579           || TREE_SIDE_EFFECTS (arg1))
2580         return 0;
2581
2582       switch (TREE_CODE (arg0))
2583         {
2584         case INDIRECT_REF:
2585         case REALPART_EXPR:
2586         case IMAGPART_EXPR:
2587           return OP_SAME (0);
2588
2589         case MEM_REF:
2590           /* Require equal access sizes, and similar pointer types.
2591              We can have incomplete types for array references of
2592              variable-sized arrays from the Fortran frontent
2593              though.  */
2594           return ((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1))
2595                    || (TYPE_SIZE (TREE_TYPE (arg0))
2596                        && TYPE_SIZE (TREE_TYPE (arg1))
2597                        && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
2598                                            TYPE_SIZE (TREE_TYPE (arg1)), flags)))
2599                   && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg0, 1)))
2600                       == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg1, 1))))
2601                   && OP_SAME (0) && OP_SAME (1));
2602
2603         case ARRAY_REF:
2604         case ARRAY_RANGE_REF:
2605           /* Operands 2 and 3 may be null.
2606              Compare the array index by value if it is constant first as we
2607              may have different types but same value here.  */
2608           return (OP_SAME (0)
2609                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2610                                           TREE_OPERAND (arg1, 1))
2611                       || OP_SAME (1))
2612                   && OP_SAME_WITH_NULL (2)
2613                   && OP_SAME_WITH_NULL (3));
2614
2615         case COMPONENT_REF:
2616           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
2617              may be NULL when we're called to compare MEM_EXPRs.  */
2618           return OP_SAME_WITH_NULL (0)
2619                  && OP_SAME (1)
2620                  && OP_SAME_WITH_NULL (2);
2621
2622         case BIT_FIELD_REF:
2623           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2624
2625         default:
2626           return 0;
2627         }
2628
2629     case tcc_expression:
2630       switch (TREE_CODE (arg0))
2631         {
2632         case ADDR_EXPR:
2633         case TRUTH_NOT_EXPR:
2634           return OP_SAME (0);
2635
2636         case TRUTH_ANDIF_EXPR:
2637         case TRUTH_ORIF_EXPR:
2638           return OP_SAME (0) && OP_SAME (1);
2639
2640         case FMA_EXPR:
2641         case WIDEN_MULT_PLUS_EXPR:
2642         case WIDEN_MULT_MINUS_EXPR:
2643           if (!OP_SAME (2))
2644             return 0;
2645           /* The multiplcation operands are commutative.  */
2646           /* FALLTHRU */
2647
2648         case TRUTH_AND_EXPR:
2649         case TRUTH_OR_EXPR:
2650         case TRUTH_XOR_EXPR:
2651           if (OP_SAME (0) && OP_SAME (1))
2652             return 1;
2653
2654           /* Otherwise take into account this is a commutative operation.  */
2655           return (operand_equal_p (TREE_OPERAND (arg0, 0),
2656                                    TREE_OPERAND (arg1, 1), flags)
2657                   && operand_equal_p (TREE_OPERAND (arg0, 1),
2658                                       TREE_OPERAND (arg1, 0), flags));
2659
2660         case COND_EXPR:
2661         case VEC_COND_EXPR:
2662         case DOT_PROD_EXPR:
2663           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2664
2665         default:
2666           return 0;
2667         }
2668
2669     case tcc_vl_exp:
2670       switch (TREE_CODE (arg0))
2671         {
2672         case CALL_EXPR:
2673           /* If the CALL_EXPRs call different functions, then they
2674              clearly can not be equal.  */
2675           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
2676                                  flags))
2677             return 0;
2678
2679           {
2680             unsigned int cef = call_expr_flags (arg0);
2681             if (flags & OEP_PURE_SAME)
2682               cef &= ECF_CONST | ECF_PURE;
2683             else
2684               cef &= ECF_CONST;
2685             if (!cef)
2686               return 0;
2687           }
2688
2689           /* Now see if all the arguments are the same.  */
2690           {
2691             const_call_expr_arg_iterator iter0, iter1;
2692             const_tree a0, a1;
2693             for (a0 = first_const_call_expr_arg (arg0, &iter0),
2694                    a1 = first_const_call_expr_arg (arg1, &iter1);
2695                  a0 && a1;
2696                  a0 = next_const_call_expr_arg (&iter0),
2697                    a1 = next_const_call_expr_arg (&iter1))
2698               if (! operand_equal_p (a0, a1, flags))
2699                 return 0;
2700
2701             /* If we get here and both argument lists are exhausted
2702                then the CALL_EXPRs are equal.  */
2703             return ! (a0 || a1);
2704           }
2705         default:
2706           return 0;
2707         }
2708
2709     case tcc_declaration:
2710       /* Consider __builtin_sqrt equal to sqrt.  */
2711       return (TREE_CODE (arg0) == FUNCTION_DECL
2712               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2713               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2714               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2715
2716     default:
2717       return 0;
2718     }
2719
2720 #undef OP_SAME
2721 #undef OP_SAME_WITH_NULL
2722 }
2723 \f
2724 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2725    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2726
2727    When in doubt, return 0.  */
2728
2729 static int
2730 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2731 {
2732   int unsignedp1, unsignedpo;
2733   tree primarg0, primarg1, primother;
2734   unsigned int correct_width;
2735
2736   if (operand_equal_p (arg0, arg1, 0))
2737     return 1;
2738
2739   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2740       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2741     return 0;
2742
2743   /* Discard any conversions that don't change the modes of ARG0 and ARG1
2744      and see if the inner values are the same.  This removes any
2745      signedness comparison, which doesn't matter here.  */
2746   primarg0 = arg0, primarg1 = arg1;
2747   STRIP_NOPS (primarg0);
2748   STRIP_NOPS (primarg1);
2749   if (operand_equal_p (primarg0, primarg1, 0))
2750     return 1;
2751
2752   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2753      actual comparison operand, ARG0.
2754
2755      First throw away any conversions to wider types
2756      already present in the operands.  */
2757
2758   primarg1 = get_narrower (arg1, &unsignedp1);
2759   primother = get_narrower (other, &unsignedpo);
2760
2761   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2762   if (unsignedp1 == unsignedpo
2763       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2764       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2765     {
2766       tree type = TREE_TYPE (arg0);
2767
2768       /* Make sure shorter operand is extended the right way
2769          to match the longer operand.  */
2770       primarg1 = fold_convert (signed_or_unsigned_type_for
2771                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2772
2773       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2774         return 1;
2775     }
2776
2777   return 0;
2778 }
2779 \f
2780 /* See if ARG is an expression that is either a comparison or is performing
2781    arithmetic on comparisons.  The comparisons must only be comparing
2782    two different values, which will be stored in *CVAL1 and *CVAL2; if
2783    they are nonzero it means that some operands have already been found.
2784    No variables may be used anywhere else in the expression except in the
2785    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
2786    the expression and save_expr needs to be called with CVAL1 and CVAL2.
2787
2788    If this is true, return 1.  Otherwise, return zero.  */
2789
2790 static int
2791 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2792 {
2793   enum tree_code code = TREE_CODE (arg);
2794   enum tree_code_class tclass = TREE_CODE_CLASS (code);
2795
2796   /* We can handle some of the tcc_expression cases here.  */
2797   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2798     tclass = tcc_unary;
2799   else if (tclass == tcc_expression
2800            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2801                || code == COMPOUND_EXPR))
2802     tclass = tcc_binary;
2803
2804   else if (tclass == tcc_expression && code == SAVE_EXPR
2805            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2806     {
2807       /* If we've already found a CVAL1 or CVAL2, this expression is
2808          two complex to handle.  */
2809       if (*cval1 || *cval2)
2810         return 0;
2811
2812       tclass = tcc_unary;
2813       *save_p = 1;
2814     }
2815
2816   switch (tclass)
2817     {
2818     case tcc_unary:
2819       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2820
2821     case tcc_binary:
2822       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2823               && twoval_comparison_p (TREE_OPERAND (arg, 1),
2824                                       cval1, cval2, save_p));
2825
2826     case tcc_constant:
2827       return 1;
2828
2829     case tcc_expression:
2830       if (code == COND_EXPR)
2831         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2832                                      cval1, cval2, save_p)
2833                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2834                                         cval1, cval2, save_p)
2835                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2836                                         cval1, cval2, save_p));
2837       return 0;
2838
2839     case tcc_comparison:
2840       /* First see if we can handle the first operand, then the second.  For
2841          the second operand, we know *CVAL1 can't be zero.  It must be that
2842          one side of the comparison is each of the values; test for the
2843          case where this isn't true by failing if the two operands
2844          are the same.  */
2845
2846       if (operand_equal_p (TREE_OPERAND (arg, 0),
2847                            TREE_OPERAND (arg, 1), 0))
2848         return 0;
2849
2850       if (*cval1 == 0)
2851         *cval1 = TREE_OPERAND (arg, 0);
2852       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2853         ;
2854       else if (*cval2 == 0)
2855         *cval2 = TREE_OPERAND (arg, 0);
2856       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2857         ;
2858       else
2859         return 0;
2860
2861       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2862         ;
2863       else if (*cval2 == 0)
2864         *cval2 = TREE_OPERAND (arg, 1);
2865       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2866         ;
2867       else
2868         return 0;
2869
2870       return 1;
2871
2872     default:
2873       return 0;
2874     }
2875 }
2876 \f
2877 /* ARG is a tree that is known to contain just arithmetic operations and
2878    comparisons.  Evaluate the operations in the tree substituting NEW0 for
2879    any occurrence of OLD0 as an operand of a comparison and likewise for
2880    NEW1 and OLD1.  */
2881
2882 static tree
2883 eval_subst (location_t loc, tree arg, tree old0, tree new0,
2884             tree old1, tree new1)
2885 {
2886   tree type = TREE_TYPE (arg);
2887   enum tree_code code = TREE_CODE (arg);
2888   enum tree_code_class tclass = TREE_CODE_CLASS (code);
2889
2890   /* We can handle some of the tcc_expression cases here.  */
2891   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2892     tclass = tcc_unary;
2893   else if (tclass == tcc_expression
2894            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2895     tclass = tcc_binary;
2896
2897   switch (tclass)
2898     {
2899     case tcc_unary:
2900       return fold_build1_loc (loc, code, type,
2901                           eval_subst (loc, TREE_OPERAND (arg, 0),
2902                                       old0, new0, old1, new1));
2903
2904     case tcc_binary:
2905       return fold_build2_loc (loc, code, type,
2906                           eval_subst (loc, TREE_OPERAND (arg, 0),
2907                                       old0, new0, old1, new1),
2908                           eval_subst (loc, TREE_OPERAND (arg, 1),
2909                                       old0, new0, old1, new1));
2910
2911     case tcc_expression:
2912       switch (code)
2913         {
2914         case SAVE_EXPR:
2915           return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
2916                              old1, new1);
2917
2918         case COMPOUND_EXPR:
2919           return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
2920                              old1, new1);
2921
2922         case COND_EXPR:
2923           return fold_build3_loc (loc, code, type,
2924                               eval_subst (loc, TREE_OPERAND (arg, 0),
2925                                           old0, new0, old1, new1),
2926                               eval_subst (loc, TREE_OPERAND (arg, 1),
2927                                           old0, new0, old1, new1),
2928                               eval_subst (loc, TREE_OPERAND (arg, 2),
2929                                           old0, new0, old1, new1));
2930         default:
2931           break;
2932         }
2933       /* Fall through - ???  */
2934
2935     case tcc_comparison:
2936       {
2937         tree arg0 = TREE_OPERAND (arg, 0);
2938         tree arg1 = TREE_OPERAND (arg, 1);
2939
2940         /* We need to check both for exact equality and tree equality.  The
2941            former will be true if the operand has a side-effect.  In that
2942            case, we know the operand occurred exactly once.  */
2943
2944         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2945           arg0 = new0;
2946         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2947           arg0 = new1;
2948
2949         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2950           arg1 = new0;
2951         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2952           arg1 = new1;
2953
2954         return fold_build2_loc (loc, code, type, arg0, arg1);
2955       }
2956
2957     default:
2958       return arg;
2959     }
2960 }
2961 \f
2962 /* Return a tree for the case when the result of an expression is RESULT
2963    converted to TYPE and OMITTED was previously an operand of the expression
2964    but is now not needed (e.g., we folded OMITTED * 0).
2965
2966    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
2967    the conversion of RESULT to TYPE.  */
2968
2969 tree
2970 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
2971 {
2972   tree t = fold_convert_loc (loc, type, result);
2973
2974   /* If the resulting operand is an empty statement, just return the omitted
2975      statement casted to void. */
2976   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
2977     return build1_loc (loc, NOP_EXPR, void_type_node,
2978                        fold_ignored_result (omitted));
2979
2980   if (TREE_SIDE_EFFECTS (omitted))
2981     return build2_loc (loc, COMPOUND_EXPR, type,
2982                        fold_ignored_result (omitted), t);
2983
2984   return non_lvalue_loc (loc, t);
2985 }
2986
2987 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
2988
2989 static tree
2990 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
2991                                tree omitted)
2992 {
2993   tree t = fold_convert_loc (loc, type, result);
2994
2995   /* If the resulting operand is an empty statement, just return the omitted
2996      statement casted to void. */
2997   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
2998     return build1_loc (loc, NOP_EXPR, void_type_node,
2999                        fold_ignored_result (omitted));
3000
3001   if (TREE_SIDE_EFFECTS (omitted))
3002     return build2_loc (loc, COMPOUND_EXPR, type,
3003                        fold_ignored_result (omitted), t);
3004
3005   return pedantic_non_lvalue_loc (loc, t);
3006 }
3007
3008 /* Return a tree for the case when the result of an expression is RESULT
3009    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3010    of the expression but are now not needed.
3011
3012    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3013    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3014    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3015    just do the conversion of RESULT to TYPE.  */
3016
3017 tree
3018 omit_two_operands_loc (location_t loc, tree type, tree result,
3019                        tree omitted1, tree omitted2)
3020 {
3021   tree t = fold_convert_loc (loc, type, result);
3022
3023   if (TREE_SIDE_EFFECTS (omitted2))
3024     t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3025   if (TREE_SIDE_EFFECTS (omitted1))
3026     t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3027
3028   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3029 }
3030
3031 \f
3032 /* Return a simplified tree node for the truth-negation of ARG.  This
3033    never alters ARG itself.  We assume that ARG is an operation that
3034    returns a truth value (0 or 1).
3035
3036    FIXME: one would think we would fold the result, but it causes
3037    problems with the dominator optimizer.  */
3038
3039 tree
3040 fold_truth_not_expr (location_t loc, tree arg)
3041 {
3042   tree type = TREE_TYPE (arg);
3043   enum tree_code code = TREE_CODE (arg);
3044   location_t loc1, loc2;
3045
3046   /* If this is a comparison, we can simply invert it, except for
3047      floating-point non-equality comparisons, in which case we just
3048      enclose a TRUTH_NOT_EXPR around what we have.  */
3049
3050   if (TREE_CODE_CLASS (code) == tcc_comparison)
3051     {
3052       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3053       if (FLOAT_TYPE_P (op_type)
3054           && flag_trapping_math
3055           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3056           && code != NE_EXPR && code != EQ_EXPR)
3057         return NULL_TREE;
3058
3059       code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3060       if (code == ERROR_MARK)
3061         return NULL_TREE;
3062
3063       return build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3064                          TREE_OPERAND (arg, 1));
3065     }
3066
3067   switch (code)
3068     {
3069     case INTEGER_CST:
3070       return constant_boolean_node (integer_zerop (arg), type);
3071
3072     case TRUTH_AND_EXPR:
3073       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3074       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3075       return build2_loc (loc, TRUTH_OR_EXPR, type,
3076                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3077                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3078
3079     case TRUTH_OR_EXPR:
3080       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3081       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3082       return build2_loc (loc, TRUTH_AND_EXPR, type,
3083                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3084                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3085
3086     case TRUTH_XOR_EXPR:
3087       /* Here we can invert either operand.  We invert the first operand
3088          unless the second operand is a TRUTH_NOT_EXPR in which case our
3089          result is the XOR of the first operand with the inside of the
3090          negation of the second operand.  */
3091
3092       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3093         return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3094                            TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3095       else
3096         return build2_loc (loc, TRUTH_XOR_EXPR, type,
3097                            invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3098                            TREE_OPERAND (arg, 1));
3099
3100     case TRUTH_ANDIF_EXPR:
3101       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3102       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3103       return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3104                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3105                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3106
3107     case TRUTH_ORIF_EXPR:
3108       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3109       loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3110       return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3111                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3112                          invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3113
3114     case TRUTH_NOT_EXPR:
3115       return TREE_OPERAND (arg, 0);
3116
3117     case COND_EXPR:
3118       {
3119         tree arg1 = TREE_OPERAND (arg, 1);
3120         tree arg2 = TREE_OPERAND (arg, 2);
3121
3122         loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3123         loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3124
3125         /* A COND_EXPR may have a throw as one operand, which
3126            then has void type.  Just leave void operands
3127            as they are.  */
3128         return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3129                            VOID_TYPE_P (TREE_TYPE (arg1))
3130                            ? arg1 : invert_truthvalue_loc (loc1, arg1),
3131                            VOID_TYPE_P (TREE_TYPE (arg2))
3132                            ? arg2 : invert_truthvalue_loc (loc2, arg2));
3133       }
3134
3135     case COMPOUND_EXPR:
3136       loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3137       return build2_loc (loc, COMPOUND_EXPR, type,
3138                          TREE_OPERAND (arg, 0),
3139                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3140
3141     case NON_LVALUE_EXPR:
3142       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3143       return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3144
3145     CASE_CONVERT:
3146       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3147         return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3148
3149       /* ... fall through ...  */
3150
3151     case FLOAT_EXPR:
3152       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3153       return build1_loc (loc, TREE_CODE (arg), type,
3154                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3155
3156     case BIT_AND_EXPR:
3157       if (!integer_onep (TREE_OPERAND (arg, 1)))
3158         return NULL_TREE;
3159       return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3160
3161     case SAVE_EXPR:
3162       return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3163
3164     case CLEANUP_POINT_EXPR:
3165       loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3166       return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3167                          invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3168
3169     default:
3170       return NULL_TREE;
3171     }
3172 }
3173
3174 /* Return a simplified tree node for the truth-negation of ARG.  This
3175    never alters ARG itself.  We assume that ARG is an operation that
3176    returns a truth value (0 or 1).
3177
3178    FIXME: one would think we would fold the result, but it causes
3179    problems with the dominator optimizer.  */
3180
3181 tree
3182 invert_truthvalue_loc (location_t loc, tree arg)
3183 {
3184   tree tem;
3185
3186   if (TREE_CODE (arg) == ERROR_MARK)
3187     return arg;
3188
3189   tem = fold_truth_not_expr (loc, arg);
3190   if (!tem)
3191     tem = build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3192
3193   return tem;
3194 }
3195
3196 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3197    operands are another bit-wise operation with a common input.  If so,
3198    distribute the bit operations to save an operation and possibly two if
3199    constants are involved.  For example, convert
3200         (A | B) & (A | C) into A | (B & C)
3201    Further simplification will occur if B and C are constants.
3202
3203    If this optimization cannot be done, 0 will be returned.  */
3204
3205 static tree
3206 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
3207                      tree arg0, tree arg1)
3208 {
3209   tree common;
3210   tree left, right;
3211
3212   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3213       || TREE_CODE (arg0) == code
3214       || (TREE_CODE (arg0) != BIT_AND_EXPR
3215           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3216     return 0;
3217
3218   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3219     {
3220       common = TREE_OPERAND (arg0, 0);
3221       left = TREE_OPERAND (arg0, 1);
3222       right = TREE_OPERAND (arg1, 1);
3223     }
3224   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3225     {
3226       common = TREE_OPERAND (arg0, 0);
3227       left = TREE_OPERAND (arg0, 1);
3228       right = TREE_OPERAND (arg1, 0);
3229     }
3230   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3231     {
3232       common = TREE_OPERAND (arg0, 1);
3233       left = TREE_OPERAND (arg0, 0);
3234       right = TREE_OPERAND (arg1, 1);
3235     }
3236   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3237     {
3238       common = TREE_OPERAND (arg0, 1);
3239       left = TREE_OPERAND (arg0, 0);
3240       right = TREE_OPERAND (arg1, 0);
3241     }
3242   else
3243     return 0;
3244
3245   common = fold_convert_loc (loc, type, common);
3246   left = fold_convert_loc (loc, type, left);
3247   right = fold_convert_loc (loc, type, right);
3248   return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
3249                       fold_build2_loc (loc, code, type, left, right));
3250 }
3251
3252 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3253    with code CODE.  This optimization is unsafe.  */
3254 static tree
3255 distribute_real_division (location_t loc, enum tree_code code, tree type,
3256                           tree arg0, tree arg1)
3257 {
3258   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3259   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3260
3261   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3262   if (mul0 == mul1
3263       && operand_equal_p (TREE_OPERAND (arg0, 1),
3264                        TREE_OPERAND (arg1, 1), 0))
3265     return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3266                         fold_build2_loc (loc, code, type,
3267                                      TREE_OPERAND (arg0, 0),
3268                                      TREE_OPERAND (arg1, 0)),
3269                         TREE_OPERAND (arg0, 1));
3270
3271   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3272   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3273                        TREE_OPERAND (arg1, 0), 0)
3274       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3275       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3276     {
3277       REAL_VALUE_TYPE r0, r1;
3278       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3279       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3280       if (!mul0)
3281         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3282       if (!mul1)
3283         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3284       real_arithmetic (&r0, code, &r0, &r1);
3285       return fold_build2_loc (loc, MULT_EXPR, type,
3286                           TREE_OPERAND (arg0, 0),
3287                           build_real (type, r0));
3288     }
3289
3290   return NULL_TREE;
3291 }
3292 \f
3293 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3294    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3295
3296 static tree
3297 make_bit_field_ref (location_t loc, tree inner, tree type,
3298                     HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3299 {
3300   tree result, bftype;
3301
3302   if (bitpos == 0)
3303     {
3304       tree size = TYPE_SIZE (TREE_TYPE (inner));
3305       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3306            || POINTER_TYPE_P (TREE_TYPE (inner)))
3307           && host_integerp (size, 0)
3308           && tree_low_cst (size, 0) == bitsize)
3309         return fold_convert_loc (loc, type, inner);
3310     }
3311
3312   bftype = type;
3313   if (TYPE_PRECISION (bftype) != bitsize
3314       || TYPE_UNSIGNED (bftype) == !unsignedp)
3315     bftype = build_nonstandard_integer_type (bitsize, 0);
3316
3317   result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3318                        size_int (bitsize), bitsize_int (bitpos));
3319
3320   if (bftype != type)
3321     result = fold_convert_loc (loc, type, result);
3322
3323   return result;
3324 }
3325
3326 /* Optimize a bit-field compare.
3327
3328    There are two cases:  First is a compare against a constant and the
3329    second is a comparison of two items where the fields are at the same
3330    bit position relative to the start of a chunk (byte, halfword, word)
3331    large enough to contain it.  In these cases we can avoid the shift
3332    implicit in bitfield extractions.
3333
3334    For constants, we emit a compare of the shifted constant with the
3335    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3336    compared.  For two fields at the same position, we do the ANDs with the
3337    similar mask and compare the result of the ANDs.
3338
3339    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3340    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3341    are the left and right operands of the comparison, respectively.
3342
3343    If the optimization described above can be done, we return the resulting
3344    tree.  Otherwise we return zero.  */
3345
3346 static tree
3347 optimize_bit_field_compare (location_t loc, enum tree_code code,
3348                             tree compare_type, tree lhs, tree rhs)
3349 {
3350   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3351   tree type = TREE_TYPE (lhs);
3352   tree signed_type, unsigned_type;
3353   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3354   enum machine_mode lmode, rmode, nmode;
3355   int lunsignedp, runsignedp;
3356   int lvolatilep = 0, rvolatilep = 0;
3357   tree linner, rinner = NULL_TREE;
3358   tree mask;
3359   tree offset;
3360
3361   /* Get all the information about the extractions being done.  If the bit size
3362      if the same as the size of the underlying object, we aren't doing an
3363      extraction at all and so can do nothing.  We also don't want to
3364      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3365      then will no longer be able to replace it.  */
3366   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3367                                 &lunsignedp, &lvolatilep, false);
3368   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3369       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3370     return 0;
3371
3372  if (!const_p)
3373    {
3374      /* If this is not a constant, we can only do something if bit positions,
3375         sizes, and signedness are the same.  */
3376      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3377                                    &runsignedp, &rvolatilep, false);
3378
3379      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3380          || lunsignedp != runsignedp || offset != 0
3381          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3382        return 0;
3383    }
3384
3385   /* See if we can find a mode to refer to this field.  We should be able to,
3386      but fail if we can't.  */
3387   if (lvolatilep
3388       && GET_MODE_BITSIZE (lmode) > 0
3389       && flag_strict_volatile_bitfields > 0)
3390     nmode = lmode;
3391   else
3392     nmode = get_best_mode (lbitsize, lbitpos, 0, 0,
3393                            const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3394                            : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3395                                   TYPE_ALIGN (TREE_TYPE (rinner))),
3396                            word_mode, lvolatilep || rvolatilep);
3397   if (nmode == VOIDmode)
3398     return 0;
3399
3400   /* Set signed and unsigned types of the precision of this mode for the
3401      shifts below.  */
3402   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3403   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3404
3405   /* Compute the bit position and size for the new reference and our offset
3406      within it. If the new reference is the same size as the original, we
3407      won't optimize anything, so return zero.  */
3408   nbitsize = GET_MODE_BITSIZE (nmode);
3409   nbitpos = lbitpos & ~ (nbitsize - 1);
3410   lbitpos -= nbitpos;
3411   if (nbitsize == lbitsize)
3412     return 0;
3413
3414   if (BYTES_BIG_ENDIAN)
3415     lbitpos = nbitsize - lbitsize - lbitpos;
3416
3417   /* Make the mask to be used against the extracted field.  */
3418   mask = build_int_cst_type (unsigned_type, -1);
3419   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3420   mask = const_binop (RSHIFT_EXPR, mask,
3421                       size_int (nbitsize - lbitsize - lbitpos));
3422
3423   if (! const_p)
3424     /* If not comparing with constant, just rework the comparison
3425        and return.  */
3426     return fold_build2_loc (loc, code, compare_type,
3427                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3428                                      make_bit_field_ref (loc, linner,
3429                                                          unsigned_type,
3430                                                          nbitsize, nbitpos,
3431                                                          1),
3432                                      mask),
3433                         fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3434                                      make_bit_field_ref (loc, rinner,
3435                                                          unsigned_type,
3436                                                          nbitsize, nbitpos,
3437                                                          1),
3438                                      mask));
3439
3440   /* Otherwise, we are handling the constant case. See if the constant is too
3441      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
3442      this not only for its own sake, but to avoid having to test for this
3443      error case below.  If we didn't, we might generate wrong code.
3444
3445      For unsigned fields, the constant shifted right by the field length should
3446      be all zero.  For signed fields, the high-order bits should agree with
3447      the sign bit.  */
3448
3449   if (lunsignedp)
3450     {
3451       if (! integer_zerop (const_binop (RSHIFT_EXPR,
3452                                         fold_convert_loc (loc,
3453                                                           unsigned_type, rhs),
3454                                         size_int (lbitsize))))
3455         {
3456           warning (0, "comparison is always %d due to width of bit-field",
3457                    code == NE_EXPR);
3458           return constant_boolean_node (code == NE_EXPR, compare_type);
3459         }
3460     }
3461   else
3462     {
3463       tree tem = const_binop (RSHIFT_EXPR,
3464              &nb