home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gnat-2.06-src.tgz / tar.out / fsf / gnat / fold-const.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  156KB  |  4,920 lines

  1. /* Fold a constant sub-tree into a single node for C-compiler
  2.    Copyright (C) 1987, 1988, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*@@ This file should be rewritten to use an arbitrary precision
  21.   @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
  22.   @@ Perhaps the routines could also be used for bc/dc, and made a lib.
  23.   @@ The routines that translate from the ap rep should
  24.   @@ warn if precision et. al. is lost.
  25.   @@ This would also make life easier when this technology is used
  26.   @@ for cross-compilers.  */
  27.  
  28.  
  29. /* The entry points in this file are fold, size_int and size_binop.
  30.  
  31.    fold takes a tree as argument and returns a simplified tree.
  32.  
  33.    size_binop takes a tree code for an arithmetic operation
  34.    and two operands that are trees, and produces a tree for the
  35.    result, assuming the type comes from `sizetype'.
  36.  
  37.    size_int takes an integer value, and creates a tree constant
  38.    with type from `sizetype'.  */
  39.    
  40. #include <stdio.h>
  41. #include <setjmp.h>
  42. #include "config.h"
  43. #include "flags.h"
  44. #include "tree.h"
  45.  
  46. /* Handle floating overflow for `const_binop'.  */
  47. static jmp_buf float_error;
  48.  
  49. static void encode    PROTO((HOST_WIDE_INT *, HOST_WIDE_INT, HOST_WIDE_INT));
  50. static void decode    PROTO((HOST_WIDE_INT *, HOST_WIDE_INT *, HOST_WIDE_INT *));
  51. int div_and_round_double PROTO((enum tree_code, int, HOST_WIDE_INT,
  52.                        HOST_WIDE_INT, HOST_WIDE_INT,
  53.                        HOST_WIDE_INT, HOST_WIDE_INT *,
  54.                        HOST_WIDE_INT *, HOST_WIDE_INT *,
  55.                        HOST_WIDE_INT *));
  56. static int split_tree    PROTO((tree, enum tree_code, tree *, tree *, int *));
  57. static tree const_binop PROTO((enum tree_code, tree, tree, int));
  58. static tree fold_convert PROTO((tree, tree));
  59. static enum tree_code invert_tree_comparison PROTO((enum tree_code));
  60. static enum tree_code swap_tree_comparison PROTO((enum tree_code));
  61. static int truth_value_p PROTO((enum tree_code));
  62. static int operand_equal_for_comparison_p PROTO((tree, tree, tree));
  63. static int twoval_comparison_p PROTO((tree, tree *, tree *, int *));
  64. static tree eval_subst    PROTO((tree, tree, tree, tree, tree));
  65. static tree omit_one_operand PROTO((tree, tree, tree));
  66. static tree distribute_bit_expr PROTO((enum tree_code, tree, tree, tree));
  67. static tree make_bit_field_ref PROTO((tree, tree, int, int, int));
  68. static tree optimize_bit_field_compare PROTO((enum tree_code, tree,
  69.                           tree, tree));
  70. static tree decode_field_reference PROTO((tree, int *, int *,
  71.                       enum machine_mode *, int *,
  72.                       int *, tree *));
  73. static int all_ones_mask_p PROTO((tree, int));
  74. static int simple_operand_p PROTO((tree));
  75. static tree range_test    PROTO((enum tree_code, tree, enum tree_code,
  76.                    enum tree_code, tree, tree, tree));
  77. static tree fold_truthop PROTO((enum tree_code, tree, tree, tree));
  78. static tree strip_compound_expr PROTO((tree, tree));
  79.  
  80. #ifndef BRANCH_COST
  81. #define BRANCH_COST 1
  82. #endif
  83.  
  84. /* Yield nonzero if a signed left shift of A by B bits overflows.  */
  85. #define left_shift_overflows(a, b)  ((a)  !=  ((a) << (b)) >> (b))
  86.  
  87. /* Suppose A1 + B1 = SUM1, using 2's complement arithmetic ignoring overflow.
  88.    Suppose A, B and SUM have the same respective signs as A1, B1, and SUM1.
  89.    Then this yields nonzero if overflow occurred during the addition.
  90.    Overflow occurs if A and B have the same sign, but A and SUM differ in sign.
  91.    Use `^' to test whether signs differ, and `< 0' to isolate the sign.  */
  92. #define overflow_sum_sign(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
  93.  
  94. /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
  95.    We do that by representing the two-word integer in 4 words, with only
  96.    HOST_BITS_PER_WIDE_INT/2 bits stored in each word, as a positive number.  */
  97.  
  98. #define LOWPART(x) \
  99.   ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT/2)) - 1))
  100. #define HIGHPART(x) \
  101.   ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT/2)
  102. #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT/2)
  103.  
  104. /* Unpack a two-word integer into 4 words.
  105.    LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
  106.    WORDS points to the array of HOST_WIDE_INTs.  */
  107.  
  108. static void
  109. encode (words, low, hi)
  110.      HOST_WIDE_INT *words;
  111.      HOST_WIDE_INT low, hi;
  112. {
  113.   words[0] = LOWPART (low);
  114.   words[1] = HIGHPART (low);
  115.   words[2] = LOWPART (hi);
  116.   words[3] = HIGHPART (hi);
  117. }
  118.  
  119. /* Pack an array of 4 words into a two-word integer.
  120.    WORDS points to the array of words.
  121.    The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces.  */
  122.  
  123. static void
  124. decode (words, low, hi)
  125.      HOST_WIDE_INT *words;
  126.      HOST_WIDE_INT *low, *hi;
  127. {
  128.   *low = words[0] | words[1] * BASE;
  129.   *hi = words[2] | words[3] * BASE;
  130. }
  131.  
  132. /* Make the integer constant T valid for its type
  133.    by setting to 0 or 1 all the bits in the constant
  134.    that don't belong in the type.
  135.    Yield 1 if a signed overflow occurs, 0 otherwise.
  136.    If OVERFLOW is nonzero, a signed overflow has already occurred
  137.    in calculating T, so propagate it.
  138.  
  139.    Make the real constant T valid for its type by calling CHECK_FLOAT_VALUE,
  140.    if it exists.  */
  141.  
  142. int
  143. force_fit_type (t, overflow)
  144.      tree t;
  145.      int overflow;
  146. {
  147.   HOST_WIDE_INT low, high;
  148.   register int prec;
  149.  
  150.   if (TREE_CODE (t) == REAL_CST)
  151.     {
  152. #ifdef CHECK_FLOAT_VALUE
  153.       CHECK_FLOAT_VALUE (TYPE_MODE (TREE_TYPE (t)), TREE_REAL_CST (t),
  154.              overflow);
  155. #endif
  156.       return overflow;
  157.     }
  158.  
  159.   else if (TREE_CODE (t) != INTEGER_CST)
  160.     return overflow;
  161.  
  162.   low = TREE_INT_CST_LOW (t);
  163.   high = TREE_INT_CST_HIGH (t);
  164.  
  165.   if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
  166.     prec = POINTER_SIZE;
  167.   else
  168.     prec = TYPE_PRECISION (TREE_TYPE (t));
  169.  
  170.   /* First clear all bits that are beyond the type's precision.  */
  171.  
  172.   if (prec == 2 * HOST_BITS_PER_WIDE_INT)
  173.     ;
  174.   else if (prec > HOST_BITS_PER_WIDE_INT)
  175.     {
  176.       TREE_INT_CST_HIGH (t)
  177.     &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
  178.     }
  179.   else
  180.     {
  181.       TREE_INT_CST_HIGH (t) = 0;
  182.       if (prec < HOST_BITS_PER_WIDE_INT)
  183.     TREE_INT_CST_LOW (t) &= ~((HOST_WIDE_INT) (-1) << prec);
  184.     }
  185.  
  186.   /* Unsigned types do not suffer sign extension or overflow.  */
  187.   if (TREE_UNSIGNED (TREE_TYPE (t)))
  188.     return 0;
  189.  
  190.   /* If the value's sign bit is set, extend the sign.  */
  191.   if (prec != 2 * HOST_BITS_PER_WIDE_INT
  192.       && (prec > HOST_BITS_PER_WIDE_INT
  193.       ? (TREE_INT_CST_HIGH (t)
  194.          & ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
  195.       : TREE_INT_CST_LOW (t) & ((HOST_WIDE_INT) 1 << (prec - 1))))
  196.     {
  197.       /* Value is negative:
  198.      set to 1 all the bits that are outside this type's precision.  */
  199.       if (prec > HOST_BITS_PER_WIDE_INT)
  200.     {
  201.       TREE_INT_CST_HIGH (t)
  202.         |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
  203.     }
  204.       else
  205.     {
  206.       TREE_INT_CST_HIGH (t) = -1;
  207.       if (prec < HOST_BITS_PER_WIDE_INT)
  208.         TREE_INT_CST_LOW (t) |= ((HOST_WIDE_INT) (-1) << prec);
  209.     }
  210.     }
  211.  
  212.   /* Yield nonzero if signed overflow occurred.  */
  213.   return
  214.     ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
  215.      != 0);
  216. }
  217.  
  218. /* Add two doubleword integers with doubleword result.
  219.    Each argument is given as two `HOST_WIDE_INT' pieces.
  220.    One argument is L1 and H1; the other, L2 and H2.
  221.    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
  222.  
  223. int
  224. add_double (l1, h1, l2, h2, lv, hv)
  225.      HOST_WIDE_INT l1, h1, l2, h2;
  226.      HOST_WIDE_INT *lv, *hv;
  227. {
  228.   HOST_WIDE_INT l, h;
  229.  
  230.   l = l1 + l2;
  231.   h = h1 + h2 + ((unsigned HOST_WIDE_INT) l < l1);
  232.  
  233.   *lv = l;
  234.   *hv = h;
  235.   return overflow_sum_sign (h1, h2, h);
  236. }
  237.  
  238. /* Negate a doubleword integer with doubleword result.
  239.    Return nonzero if the operation overflows, assuming it's signed.
  240.    The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
  241.    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
  242.  
  243. int
  244. neg_double (l1, h1, lv, hv)
  245.      HOST_WIDE_INT l1, h1;
  246.      HOST_WIDE_INT *lv, *hv;
  247. {
  248.   if (l1 == 0)
  249.     {
  250.       *lv = 0;
  251.       *hv = - h1;
  252.       return (*hv & h1) < 0;
  253.     }
  254.   else
  255.     {
  256.       *lv = - l1;
  257.       *hv = ~ h1;
  258.       return 0;
  259.     }
  260. }
  261.  
  262. /* Multiply two doubleword integers with doubleword result.
  263.    Return nonzero if the operation overflows, assuming it's signed.
  264.    Each argument is given as two `HOST_WIDE_INT' pieces.
  265.    One argument is L1 and H1; the other, L2 and H2.
  266.    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
  267.  
  268. int
  269. mul_double (l1, h1, l2, h2, lv, hv)
  270.      HOST_WIDE_INT l1, h1, l2, h2;
  271.      HOST_WIDE_INT *lv, *hv;
  272. {
  273.   HOST_WIDE_INT arg1[4];
  274.   HOST_WIDE_INT arg2[4];
  275.   HOST_WIDE_INT prod[4 * 2];
  276.   register unsigned HOST_WIDE_INT carry;
  277.   register int i, j, k;
  278.   HOST_WIDE_INT toplow, tophigh, neglow, neghigh;
  279.  
  280.   encode (arg1, l1, h1);
  281.   encode (arg2, l2, h2);
  282.  
  283.   bzero ((char *) prod, sizeof prod);
  284.  
  285.   for (i = 0; i < 4; i++)
  286.     {
  287.       carry = 0;
  288.       for (j = 0; j < 4; j++)
  289.     {
  290.       k = i + j;
  291.       /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000.  */
  292.       carry += arg1[i] * arg2[j];
  293.       /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF.  */
  294.       carry += prod[k];
  295.       prod[k] = LOWPART (carry);
  296.       carry = HIGHPART (carry);
  297.     }
  298.       prod[i + 4] = carry;
  299.     }
  300.  
  301.   decode (prod, lv, hv);    /* This ignores prod[4] through prod[4*2-1] */
  302.  
  303.   /* Check for overflow by calculating the top half of the answer in full;
  304.      it should agree with the low half's sign bit.  */
  305.   decode (prod+4, &toplow, &tophigh);
  306.   if (h1 < 0)
  307.     {
  308.       neg_double (l2, h2, &neglow, &neghigh);
  309.       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
  310.     }
  311.   if (h2 < 0)
  312.     {
  313.       neg_double (l1, h1, &neglow, &neghigh);
  314.       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
  315.     }
  316.   return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
  317. }
  318.  
  319. /* Shift the doubleword integer in L1, H1 left by COUNT places
  320.    keeping only PREC bits of result.
  321.    Shift right if COUNT is negative.
  322.    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
  323.    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
  324.  
  325. void
  326. lshift_double (l1, h1, count, prec, lv, hv, arith)
  327.      HOST_WIDE_INT l1, h1, count;
  328.      int prec;
  329.      HOST_WIDE_INT *lv, *hv;
  330.      int arith;
  331. {
  332.   if (count < 0)
  333.     {
  334.       rshift_double (l1, h1, - count, prec, lv, hv, arith);
  335.       return;
  336.     }
  337.   
  338.   if (count >= prec)
  339.     count = (unsigned HOST_WIDE_INT) count & prec;
  340.  
  341.   if (count >= HOST_BITS_PER_WIDE_INT)
  342.     {
  343.       *hv = (unsigned HOST_WIDE_INT) l1 << count - HOST_BITS_PER_WIDE_INT;
  344.       *lv = 0;
  345.     }
  346.   else
  347.     {
  348.       *hv = (((unsigned HOST_WIDE_INT) h1 << count)
  349.          | ((unsigned HOST_WIDE_INT) l1 >> HOST_BITS_PER_WIDE_INT - count - 1 >> 1));
  350.       *lv = (unsigned HOST_WIDE_INT) l1 << count;
  351.     }
  352. }
  353.  
  354. /* Shift the doubleword integer in L1, H1 right by COUNT places
  355.    keeping only PREC bits of result.  COUNT must be positive.
  356.    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
  357.    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
  358.  
  359. void
  360. rshift_double (l1, h1, count, prec, lv, hv, arith)
  361.      HOST_WIDE_INT l1, h1, count;
  362.      int prec;
  363.      HOST_WIDE_INT *lv, *hv;
  364.      int arith;
  365. {
  366.   unsigned HOST_WIDE_INT signmask;
  367.   signmask = (arith
  368.           ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
  369.           : 0);
  370.  
  371.   if (count >= prec)
  372.     count = (unsigned HOST_WIDE_INT) count % prec;
  373.  
  374.   if (count >= HOST_BITS_PER_WIDE_INT)
  375.     {
  376.       *hv = signmask;
  377.       *lv = ((signmask << 2 * HOST_BITS_PER_WIDE_INT - count - 1 << 1)
  378.          | ((unsigned HOST_WIDE_INT) h1 >> count - HOST_BITS_PER_WIDE_INT));
  379.     }
  380.   else
  381.     {
  382.       *lv = (((unsigned HOST_WIDE_INT) l1 >> count)
  383.          | ((unsigned HOST_WIDE_INT) h1 << HOST_BITS_PER_WIDE_INT - count - 1 << 1));
  384.       *hv = ((signmask << HOST_BITS_PER_WIDE_INT - count)
  385.          | ((unsigned HOST_WIDE_INT) h1 >> count));
  386.     }
  387. }
  388.  
  389. /* Rotate the doubleword integer in L1, H1 left by COUNT places
  390.    keeping only PREC bits of result.
  391.    Rotate right if COUNT is negative.
  392.    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
  393.  
  394. void
  395. lrotate_double (l1, h1, count, prec, lv, hv)
  396.      HOST_WIDE_INT l1, h1, count;
  397.      int prec;
  398.      HOST_WIDE_INT *lv, *hv;
  399. {
  400.   HOST_WIDE_INT s1l, s1h, s2l, s2h;
  401.  
  402.   count %= prec;
  403.   if (count < 0)
  404.     count += prec;
  405.  
  406.   lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
  407.   rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
  408.   *lv = s1l | s2l;
  409.   *hv = s1h | s2h;
  410. }
  411.  
  412. /* Rotate the doubleword integer in L1, H1 left by COUNT places
  413.    keeping only PREC bits of result.  COUNT must be positive.
  414.    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
  415.  
  416. void
  417. rrotate_double (l1, h1, count, prec, lv, hv)
  418.      HOST_WIDE_INT l1, h1, count;
  419.      int prec;
  420.      HOST_WIDE_INT *lv, *hv;
  421. {
  422.   HOST_WIDE_INT s1l, s1h, s2l, s2h;
  423.  
  424.   count %= prec;
  425.   if (count < 0)
  426.     count += prec;
  427.  
  428.   rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
  429.   lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
  430.   *lv = s1l | s2l;
  431.   *hv = s1h | s2h;
  432. }
  433.  
  434. /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
  435.    for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
  436.    CODE is a tree code for a kind of division, one of
  437.    TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
  438.    or EXACT_DIV_EXPR
  439.    It controls how the quotient is rounded to a integer.
  440.    Return nonzero if the operation overflows.
  441.    UNS nonzero says do unsigned division.  */
  442.  
  443. int
  444. div_and_round_double (code, uns,
  445.               lnum_orig, hnum_orig, lden_orig, hden_orig,
  446.               lquo, hquo, lrem, hrem)
  447.      enum tree_code code;
  448.      int uns;
  449.      HOST_WIDE_INT lnum_orig, hnum_orig; /* num == numerator == dividend */
  450.      HOST_WIDE_INT lden_orig, hden_orig; /* den == denominator == divisor */
  451.      HOST_WIDE_INT *lquo, *hquo, *lrem, *hrem;
  452. {
  453.   int quo_neg = 0;
  454.   HOST_WIDE_INT num[4 + 1];    /* extra element for scaling.  */
  455.   HOST_WIDE_INT den[4], quo[4];
  456.   register int i, j;
  457.   unsigned HOST_WIDE_INT work;
  458.   register int carry = 0;
  459.   HOST_WIDE_INT lnum = lnum_orig;
  460.   HOST_WIDE_INT hnum = hnum_orig;
  461.   HOST_WIDE_INT lden = lden_orig;
  462.   HOST_WIDE_INT hden = hden_orig;
  463.   int overflow = 0;
  464.  
  465.   if ((hden == 0) && (lden == 0))
  466.     abort ();
  467.  
  468.   /* calculate quotient sign and convert operands to unsigned.  */
  469.   if (!uns) 
  470.     {
  471.       if (hnum < 0)
  472.     {
  473.       quo_neg = ~ quo_neg;
  474.       /* (minimum integer) / (-1) is the only overflow case.  */
  475.       if (neg_double (lnum, hnum, &lnum, &hnum) && (lden & hden) == -1)
  476.         overflow = 1;
  477.     }
  478.       if (hden < 0) 
  479.     {
  480.       quo_neg = ~ quo_neg;
  481.       neg_double (lden, hden, &lden, &hden);
  482.     }
  483.     }
  484.  
  485.   if (hnum == 0 && hden == 0)
  486.     {                /* single precision */
  487.       *hquo = *hrem = 0;
  488.       /* This unsigned division rounds toward zero.  */
  489.       *lquo = lnum / (unsigned HOST_WIDE_INT) lden;
  490.       goto finish_up;
  491.     }
  492.  
  493.   if (hnum == 0)
  494.     {                /* trivial case: dividend < divisor */
  495.       /* hden != 0 already checked.  */
  496.       *hquo = *lquo = 0;
  497.       *hrem = hnum;
  498.       *lrem = lnum;
  499.       goto finish_up;
  500.     }
  501.  
  502.   bzero ((char *) quo, sizeof quo);
  503.  
  504.   bzero ((char *) num, sizeof num);    /* to zero 9th element */
  505.   bzero ((char *) den, sizeof den);
  506.  
  507.   encode (num, lnum, hnum); 
  508.   encode (den, lden, hden);
  509.  
  510.   /* Special code for when the divisor < BASE.  */
  511.   if (hden == 0 && lden < BASE)
  512.     {
  513.       /* hnum != 0 already checked.  */
  514.       for (i = 4 - 1; i >= 0; i--)
  515.     {
  516.       work = num[i] + carry * BASE;
  517.       quo[i] = work / (unsigned HOST_WIDE_INT) lden;
  518.       carry = work % (unsigned HOST_WIDE_INT) lden;
  519.     }
  520.     }
  521.   else
  522.     {
  523.       /* Full double precision division,
  524.      with thanks to Don Knuth's "Seminumerical Algorithms".  */
  525.     int quo_est, scale, num_hi_sig, den_hi_sig;
  526.  
  527.     /* Find the highest non-zero divisor digit.  */
  528.     for (i = 4 - 1; ; i--)
  529.       if (den[i] != 0) {
  530.     den_hi_sig = i;
  531.     break;
  532.       }
  533.  
  534.     /* Insure that the first digit of the divisor is at least BASE/2.
  535.        This is required by the quotient digit estimation algorithm.  */
  536.  
  537.     scale = BASE / (den[den_hi_sig] + 1);
  538.     if (scale > 1) {        /* scale divisor and dividend */
  539.       carry = 0;
  540.       for (i = 0; i <= 4 - 1; i++) {
  541.     work = (num[i] * scale) + carry;
  542.     num[i] = LOWPART (work);
  543.     carry = HIGHPART (work);
  544.       } num[4] = carry;
  545.       carry = 0;
  546.       for (i = 0; i <= 4 - 1; i++) {
  547.     work = (den[i] * scale) + carry;
  548.     den[i] = LOWPART (work);
  549.     carry = HIGHPART (work);
  550.     if (den[i] != 0) den_hi_sig = i;
  551.       }
  552.     }
  553.  
  554.     num_hi_sig = 4;
  555.  
  556.     /* Main loop */
  557.     for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--) {
  558.       /* guess the next quotient digit, quo_est, by dividing the first
  559.      two remaining dividend digits by the high order quotient digit.
  560.      quo_est is never low and is at most 2 high.  */
  561.       unsigned HOST_WIDE_INT tmp;
  562.  
  563.       num_hi_sig = i + den_hi_sig + 1;
  564.       work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
  565.       if (num[num_hi_sig] != den[den_hi_sig])
  566.     quo_est = work / den[den_hi_sig];
  567.       else
  568.     quo_est = BASE - 1;
  569.  
  570.       /* refine quo_est so it's usually correct, and at most one high.   */
  571.       tmp = work - quo_est * den[den_hi_sig];
  572.       if (tmp < BASE
  573.       && den[den_hi_sig - 1] * quo_est > (tmp * BASE + num[num_hi_sig - 2]))
  574.     quo_est--;
  575.  
  576.       /* Try QUO_EST as the quotient digit, by multiplying the
  577.          divisor by QUO_EST and subtracting from the remaining dividend.
  578.      Keep in mind that QUO_EST is the I - 1st digit.  */
  579.  
  580.       carry = 0;
  581.       for (j = 0; j <= den_hi_sig; j++)
  582.     {
  583.       work = quo_est * den[j] + carry;
  584.       carry = HIGHPART (work);
  585.       work = num[i + j] - LOWPART (work);
  586.       num[i + j] = LOWPART (work);
  587.       carry += HIGHPART (work) != 0;
  588.     }
  589.  
  590.       /* if quo_est was high by one, then num[i] went negative and
  591.      we need to correct things.  */
  592.  
  593.       if (num[num_hi_sig] < carry)
  594.     {
  595.       quo_est--;
  596.       carry = 0;        /* add divisor back in */
  597.       for (j = 0; j <= den_hi_sig; j++)
  598.         {
  599.           work = num[i + j] + den[j] + carry;
  600.           carry = HIGHPART (work);
  601.           num[i + j] = LOWPART (work);
  602.         }
  603.       num [num_hi_sig] += carry;
  604.     }
  605.  
  606.       /* store the quotient digit.  */
  607.       quo[i] = quo_est;
  608.     }
  609.   }
  610.  
  611.   decode (quo, lquo, hquo);
  612.  
  613.  finish_up:
  614.   /* if result is negative, make it so.  */
  615.   if (quo_neg)
  616.     neg_double (*lquo, *hquo, lquo, hquo);
  617.  
  618.   /* compute trial remainder:  rem = num - (quo * den)  */
  619.   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
  620.   neg_double (*lrem, *hrem, lrem, hrem);
  621.   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
  622.  
  623.   switch (code)
  624.     {
  625.     case TRUNC_DIV_EXPR:
  626.     case TRUNC_MOD_EXPR:    /* round toward zero */
  627.     case EXACT_DIV_EXPR:    /* for this one, it shouldn't matter */
  628.       return overflow;
  629.  
  630.     case FLOOR_DIV_EXPR:
  631.     case FLOOR_MOD_EXPR:    /* round toward negative infinity */
  632.       if (quo_neg && (*lrem != 0 || *hrem != 0))   /* ratio < 0 && rem != 0 */
  633.     {
  634.       /* quo = quo - 1;  */
  635.       add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT)  -1,
  636.               lquo, hquo);
  637.     }
  638.       else return overflow;
  639.       break;
  640.  
  641.     case CEIL_DIV_EXPR:
  642.     case CEIL_MOD_EXPR:        /* round toward positive infinity */
  643.       if (!quo_neg && (*lrem != 0 || *hrem != 0))  /* ratio > 0 && rem != 0 */
  644.     {
  645.       add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
  646.               lquo, hquo);
  647.     }
  648.       else return overflow;
  649.       break;
  650.     
  651.     case ROUND_DIV_EXPR:
  652.     case ROUND_MOD_EXPR:    /* round to closest integer */
  653.       {
  654.     HOST_WIDE_INT labs_rem = *lrem, habs_rem = *hrem;
  655.     HOST_WIDE_INT labs_den = lden, habs_den = hden, ltwice, htwice;
  656.  
  657.     /* get absolute values */
  658.     if (*hrem < 0) neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
  659.     if (hden < 0) neg_double (lden, hden, &labs_den, &habs_den);
  660.  
  661.     /* if (2 * abs (lrem) >= abs (lden)) */
  662.     mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
  663.             labs_rem, habs_rem, <wice, &htwice);
  664.     if (((unsigned HOST_WIDE_INT) habs_den
  665.          < (unsigned HOST_WIDE_INT) htwice)
  666.         || (((unsigned HOST_WIDE_INT) habs_den
  667.          == (unsigned HOST_WIDE_INT) htwice)
  668.         && ((HOST_WIDE_INT unsigned) labs_den
  669.             < (unsigned HOST_WIDE_INT) ltwice)))
  670.       {
  671.         if (*hquo < 0)
  672.           /* quo = quo - 1;  */
  673.           add_double (*lquo, *hquo,
  674.               (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
  675.         else
  676.           /* quo = quo + 1; */
  677.           add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
  678.               lquo, hquo);
  679.       }
  680.     else return overflow;
  681.       }
  682.       break;
  683.  
  684.     default:
  685.       abort ();
  686.     }
  687.  
  688.   /* compute true remainder:  rem = num - (quo * den)  */
  689.   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
  690.   neg_double (*lrem, *hrem, lrem, hrem);
  691.   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
  692.   return overflow;
  693. }
  694.  
  695. #ifndef REAL_ARITHMETIC
  696. /* Effectively truncate a real value to represent the nearest possible value
  697.    in a narrower mode.  The result is actually represented in the same data
  698.    type as the argument, but its value is usually different.
  699.  
  700.    A trap may occur during the FP operations and it is the responsibility
  701.    of the calling function to have a handler established.  */
  702.  
  703. REAL_VALUE_TYPE
  704. real_value_truncate (mode, arg)
  705.      enum machine_mode mode;
  706.      REAL_VALUE_TYPE arg;
  707. {
  708.   return REAL_VALUE_TRUNCATE (mode, arg);
  709. }
  710.  
  711. #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
  712.  
  713. /* Check for infinity in an IEEE double precision number.  */
  714.  
  715. int
  716. target_isinf (x)
  717.      REAL_VALUE_TYPE x;
  718. {
  719.   /* The IEEE 64-bit double format.  */
  720.   union {
  721.     REAL_VALUE_TYPE d;
  722.     struct {
  723.       unsigned sign      :  1;
  724.       unsigned exponent  : 11;
  725.       unsigned mantissa1 : 20;
  726.       unsigned mantissa2;
  727.     } little_endian;
  728.     struct {
  729.       unsigned mantissa2;
  730.       unsigned mantissa1 : 20;
  731.       unsigned exponent  : 11;
  732.       unsigned sign      :  1;
  733.     } big_endian;    
  734.   } u;
  735.  
  736.   u.d = dconstm1;
  737.   if (u.big_endian.sign == 1)
  738.     {
  739.       u.d = x;
  740.       return (u.big_endian.exponent == 2047
  741.           && u.big_endian.mantissa1 == 0
  742.           && u.big_endian.mantissa2 == 0);
  743.     }
  744.   else
  745.     {
  746.       u.d = x;
  747.       return (u.little_endian.exponent == 2047
  748.           && u.little_endian.mantissa1 == 0
  749.           && u.little_endian.mantissa2 == 0);
  750.     }
  751. }
  752.  
  753. /* Check whether an IEEE double precision number is a NaN.  */
  754.  
  755. int
  756. target_isnan (x)
  757.      REAL_VALUE_TYPE x;
  758. {
  759.   /* The IEEE 64-bit double format.  */
  760.   union {
  761.     REAL_VALUE_TYPE d;
  762.     struct {
  763.       unsigned sign      :  1;
  764.       unsigned exponent  : 11;
  765.       unsigned mantissa1 : 20;
  766.       unsigned mantissa2;
  767.     } little_endian;
  768.     struct {
  769.       unsigned mantissa2;
  770.       unsigned mantissa1 : 20;
  771.       unsigned exponent  : 11;
  772.       unsigned sign      :  1;
  773.     } big_endian;    
  774.   } u;
  775.  
  776.   u.d = dconstm1;
  777.   if (u.big_endian.sign == 1)
  778.     {
  779.       u.d = x;
  780.       return (u.big_endian.exponent == 2047
  781.           && (u.big_endian.mantissa1 != 0
  782.           || u.big_endian.mantissa2 != 0));
  783.     }
  784.   else
  785.     {
  786.       u.d = x;
  787.       return (u.little_endian.exponent == 2047
  788.           && (u.little_endian.mantissa1 != 0
  789.           || u.little_endian.mantissa2 != 0));
  790.     }
  791. }
  792.  
  793. /* Check for a negative IEEE double precision number.  */
  794.  
  795. int
  796. target_negative (x)
  797.      REAL_VALUE_TYPE x;
  798. {
  799.   /* The IEEE 64-bit double format.  */
  800.   union {
  801.     REAL_VALUE_TYPE d;
  802.     struct {
  803.       unsigned sign      :  1;
  804.       unsigned exponent  : 11;
  805.       unsigned mantissa1 : 20;
  806.       unsigned mantissa2;
  807.     } little_endian;
  808.     struct {
  809.       unsigned mantissa2;
  810.       unsigned mantissa1 : 20;
  811.       unsigned exponent  : 11;
  812.       unsigned sign      :  1;
  813.     } big_endian;    
  814.   } u;
  815.  
  816.   u.d = dconstm1;
  817.   if (u.big_endian.sign == 1)
  818.     {
  819.       u.d = x;
  820.       return u.big_endian.sign;
  821.     }
  822.   else
  823.     {
  824.       u.d = x;
  825.       return u.little_endian.sign;
  826.     }
  827. }
  828. #else /* Target not IEEE */
  829.  
  830. /* Let's assume other float formats don't have infinity.
  831.    (This can be overridden by redefining REAL_VALUE_ISINF.)  */
  832.  
  833. target_isinf (x)
  834.      REAL_VALUE_TYPE x;
  835. {
  836.   return 0;
  837. }
  838.  
  839. /* Let's assume other float formats don't have NaNs.
  840.    (This can be overridden by redefining REAL_VALUE_ISNAN.)  */
  841.  
  842. target_isnan (x)
  843.      REAL_VALUE_TYPE x;
  844. {
  845.   return 0;
  846. }
  847.  
  848. /* Let's assume other float formats don't have minus zero.
  849.    (This can be overridden by redefining REAL_VALUE_NEGATIVE.)  */
  850.  
  851. target_negative (x)
  852.      REAL_VALUE_TYPE x;
  853. {
  854.   return x < 0;
  855. }
  856. #endif /* Target not IEEE */
  857. #endif /* no REAL_ARITHMETIC */
  858.  
  859. /* Split a tree IN into a constant and a variable part
  860.    that could be combined with CODE to make IN.
  861.    CODE must be a commutative arithmetic operation.
  862.    Store the constant part into *CONP and the variable in &VARP.
  863.    Return 1 if this was done; zero means the tree IN did not decompose
  864.    this way.
  865.  
  866.    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.
  867.    Therefore, we must tell the caller whether the variable part
  868.    was subtracted.  We do this by storing 1 or -1 into *VARSIGNP.
  869.    The value stored is the coefficient for the variable term.
  870.    The constant term we return should always be added;
  871.    we negate it if necessary.  */
  872.  
  873. static int
  874. split_tree (in, code, varp, conp, varsignp)
  875.      tree in;
  876.      enum tree_code code;
  877.      tree *varp, *conp;
  878.      int *varsignp;
  879. {
  880.   register tree outtype = TREE_TYPE (in);
  881.   *varp = 0;
  882.   *conp = 0;
  883.  
  884.   /* Strip any conversions that don't change the machine mode.  */
  885.   while ((TREE_CODE (in) == NOP_EXPR
  886.       || TREE_CODE (in) == CONVERT_EXPR)
  887.      && (TYPE_MODE (TREE_TYPE (in))
  888.          == TYPE_MODE (TREE_TYPE (TREE_OPERAND (in, 0)))))
  889.     in = TREE_OPERAND (in, 0);
  890.  
  891.   if (TREE_CODE (in) == code
  892.       || (! FLOAT_TYPE_P (TREE_TYPE (in))
  893.       /* We can associate addition and subtraction together
  894.          (even though the C standard doesn't say so)
  895.          for integers because the value is not affected.
  896.          For reals, the value might be affected, so we can't.  */
  897.       && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
  898.           || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
  899.     {
  900.       enum tree_code code = TREE_CODE (TREE_OPERAND (in, 0));
  901.       if (code == INTEGER_CST)
  902.     {
  903.       *conp = TREE_OPERAND (in, 0);
  904.       *varp = TREE_OPERAND (in, 1);
  905.       if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
  906.           && TREE_TYPE (*varp) != outtype)
  907.         *varp = convert (outtype, *varp);
  908.       *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
  909.       return 1;
  910.     }
  911.       if (TREE_CONSTANT (TREE_OPERAND (in, 1)))
  912.     {
  913.       *conp = TREE_OPERAND (in, 1);
  914.       *varp = TREE_OPERAND (in, 0);
  915.       *varsignp = 1;
  916.       if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
  917.           && TREE_TYPE (*varp) != outtype)
  918.         *varp = convert (outtype, *varp);
  919.       if (TREE_CODE (in) == MINUS_EXPR)
  920.         {
  921.           /* If operation is subtraction and constant is second,
  922.          must negate it to get an additive constant.
  923.          And this cannot be done unless it is a manifest constant.
  924.          It could also be the address of a static variable.
  925.          We cannot negate that, so give up.  */
  926.           if (TREE_CODE (*conp) == INTEGER_CST)
  927.         /* Subtracting from integer_zero_node loses for long long.  */
  928.         *conp = fold (build1 (NEGATE_EXPR, TREE_TYPE (*conp), *conp));
  929.           else
  930.         return 0;
  931.         }
  932.       return 1;
  933.     }
  934.       if (TREE_CONSTANT (TREE_OPERAND (in, 0)))
  935.     {
  936.       *conp = TREE_OPERAND (in, 0);
  937.       *varp = TREE_OPERAND (in, 1);
  938.       if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
  939.           && TREE_TYPE (*varp) != outtype)
  940.         *varp = convert (outtype, *varp);
  941.       *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
  942.       return 1;
  943.     }
  944.     }
  945.   return 0;
  946. }
  947.  
  948. /* Combine two constants NUM and ARG2 under operation CODE
  949.    to produce a new constant.
  950.    We assume ARG1 and ARG2 have the same data type,
  951.    or at least are the same kind of constant and the same machine mode.
  952.  
  953.    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
  954.  
  955. static tree
  956. const_binop (code, arg1, arg2, notrunc)
  957.      enum tree_code code;
  958.      register tree arg1, arg2;
  959.      int notrunc;
  960. {
  961.   if (TREE_CODE (arg1) == INTEGER_CST)
  962.     {
  963.       register HOST_WIDE_INT int1l = TREE_INT_CST_LOW (arg1);
  964.       register HOST_WIDE_INT int1h = TREE_INT_CST_HIGH (arg1);
  965.       HOST_WIDE_INT int2l = TREE_INT_CST_LOW (arg2);
  966.       HOST_WIDE_INT int2h = TREE_INT_CST_HIGH (arg2);
  967.       HOST_WIDE_INT low, hi;
  968.       HOST_WIDE_INT garbagel, garbageh;
  969.       register tree t;
  970.       int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
  971.       int overflow = 0;
  972.  
  973.       switch (code)
  974.     {
  975.     case BIT_IOR_EXPR:
  976.       t = build_int_2 (int1l | int2l, int1h | int2h);
  977.       break;
  978.  
  979.     case BIT_XOR_EXPR:
  980.       t = build_int_2 (int1l ^ int2l, int1h ^ int2h);
  981.       break;
  982.  
  983.     case BIT_AND_EXPR:
  984.       t = build_int_2 (int1l & int2l, int1h & int2h);
  985.       break;
  986.  
  987.     case BIT_ANDTC_EXPR:
  988.       t = build_int_2 (int1l & ~int2l, int1h & ~int2h);
  989.       break;
  990.  
  991.     case RSHIFT_EXPR:
  992.       int2l = - int2l;
  993.     case LSHIFT_EXPR:
  994.       /* It's unclear from the C standard whether shifts can overflow.
  995.          The following code ignores overflow; perhaps a C standard
  996.          interpretation ruling is needed.  */
  997.       lshift_double (int1l, int1h, int2l,
  998.              TYPE_PRECISION (TREE_TYPE (arg1)),
  999.              &low, &hi,
  1000.              !uns);
  1001.       t = build_int_2 (low, hi);
  1002.       TREE_TYPE (t) = TREE_TYPE (arg1);
  1003.       if (!notrunc)
  1004.         force_fit_type (t, 0);
  1005.       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
  1006.       TREE_CONSTANT_OVERFLOW (t)
  1007.         = TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2);
  1008.       return t;
  1009.  
  1010.     case RROTATE_EXPR:
  1011.       int2l = - int2l;
  1012.     case LROTATE_EXPR:
  1013.       lrotate_double (int1l, int1h, int2l,
  1014.               TYPE_PRECISION (TREE_TYPE (arg1)),
  1015.               &low, &hi);
  1016.       t = build_int_2 (low, hi);
  1017.       break;
  1018.  
  1019.     case PLUS_EXPR:
  1020.       if (int1h == 0)
  1021.         {
  1022.           int2l += int1l;
  1023.           if ((unsigned HOST_WIDE_INT) int2l < int1l)
  1024.         {
  1025.           hi = int2h++;
  1026.           overflow = int2h < hi;
  1027.         }
  1028.           t = build_int_2 (int2l, int2h);
  1029.           break;
  1030.         }
  1031.       if (int2h == 0)
  1032.         {
  1033.           int1l += int2l;
  1034.           if ((unsigned HOST_WIDE_INT) int1l < int2l)
  1035.         {
  1036.           hi = int1h++;
  1037.           overflow = int1h < hi;
  1038.         }
  1039.           t = build_int_2 (int1l, int1h);
  1040.           break;
  1041.         }
  1042.       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
  1043.       t = build_int_2 (low, hi);
  1044.       break;
  1045.  
  1046.     case MINUS_EXPR:
  1047.       if (int2h == 0 && int2l == 0)
  1048.         {
  1049.           t = build_int_2 (int1l, int1h);
  1050.           break;
  1051.         }
  1052.       neg_double (int2l, int2h, &low, &hi);
  1053.       add_double (int1l, int1h, low, hi, &low, &hi);
  1054.       overflow = overflow_sum_sign (hi, int2h, int1h);
  1055.       t = build_int_2 (low, hi);
  1056.       break;
  1057.  
  1058.     case MULT_EXPR:
  1059.       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
  1060.       t = build_int_2 (low, hi);
  1061.       break;
  1062.  
  1063.     case TRUNC_DIV_EXPR:
  1064.     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
  1065.     case EXACT_DIV_EXPR:
  1066.       /* This is a shortcut for a common special case.
  1067.          It reduces the number of tree nodes generated
  1068.          and saves time.  */
  1069.       if (int2h == 0 && int2l > 0
  1070.           && TREE_TYPE (arg1) == sizetype
  1071.           && int1h == 0 && int1l >= 0)
  1072.         {
  1073.           if (code == CEIL_DIV_EXPR)
  1074.         int1l += int2l-1;
  1075.           return size_int (int1l / int2l);
  1076.         }
  1077.     case ROUND_DIV_EXPR: 
  1078.       if (int2h == 0 && int2l == 1)
  1079.         {
  1080.           t = build_int_2 (int1l, int1h);
  1081.           break;
  1082.         }
  1083.       if (int1l == int2l && int1h == int2h)
  1084.         {
  1085.           if ((int1l | int1h) == 0)
  1086.         abort ();
  1087.           t = build_int_2 (1, 0);
  1088.           break;
  1089.         }
  1090.       overflow = div_and_round_double (code, uns,
  1091.                        int1l, int1h, int2l, int2h,
  1092.                        &low, &hi, &garbagel, &garbageh);
  1093.       t = build_int_2 (low, hi);
  1094.       break;
  1095.  
  1096.     case TRUNC_MOD_EXPR: case ROUND_MOD_EXPR: 
  1097.     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
  1098.       overflow = div_and_round_double (code, uns,
  1099.                        int1l, int1h, int2l, int2h,
  1100.                        &garbagel, &garbageh, &low, &hi);
  1101.       t = build_int_2 (low, hi);
  1102.       break;
  1103.  
  1104.     case MIN_EXPR:
  1105.     case MAX_EXPR:
  1106.       if (uns)
  1107.         {
  1108.           low = (((unsigned HOST_WIDE_INT) int1h
  1109.               < (unsigned HOST_WIDE_INT) int2h)
  1110.              || (((unsigned HOST_WIDE_INT) int1h
  1111.               == (unsigned HOST_WIDE_INT) int2h)
  1112.              && ((unsigned HOST_WIDE_INT) int1l
  1113.                  < (unsigned HOST_WIDE_INT) int2l)));
  1114.         }
  1115.       else
  1116.         {
  1117.           low = ((int1h < int2h)
  1118.              || ((int1h == int2h)
  1119.              && ((unsigned HOST_WIDE_INT) int1l
  1120.                  < (unsigned HOST_WIDE_INT) int2l)));
  1121.         }
  1122.       if (low == (code == MIN_EXPR))
  1123.         t = build_int_2 (int1l, int1h);
  1124.       else
  1125.         t = build_int_2 (int2l, int2h);
  1126.       break;
  1127.  
  1128.     default:
  1129.       abort ();
  1130.     }
  1131.     got_it:
  1132.       TREE_TYPE (t) = TREE_TYPE (arg1);
  1133.       TREE_OVERFLOW (t)
  1134.     = ((notrunc ? !uns && overflow : force_fit_type (t, overflow))
  1135.        | TREE_OVERFLOW (arg1)
  1136.        | TREE_OVERFLOW (arg2));
  1137.       TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
  1138.                     | TREE_CONSTANT_OVERFLOW (arg1)
  1139.                     | TREE_CONSTANT_OVERFLOW (arg2));
  1140.       return t;
  1141.     }
  1142. #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  1143.   if (TREE_CODE (arg1) == REAL_CST)
  1144.     {
  1145.       REAL_VALUE_TYPE d1;
  1146.       REAL_VALUE_TYPE d2;
  1147.       int overflow = 0;
  1148.       REAL_VALUE_TYPE value;
  1149.       tree t;
  1150.  
  1151.       d1 = TREE_REAL_CST (arg1);
  1152.       d2 = TREE_REAL_CST (arg2);
  1153.  
  1154.       /* If either operand is a NaN, just return it.  Otherwise, set up
  1155.      for floating-point trap; we return an overflow.  */
  1156.       if (REAL_VALUE_ISNAN (d1))
  1157.     return arg1;
  1158.       else if (REAL_VALUE_ISNAN (d2))
  1159.     return arg2;
  1160.       else if (setjmp (float_error))
  1161.     {
  1162.       t = copy_node (arg1);
  1163.       overflow = 1;
  1164.       goto got_float;
  1165.     }
  1166.  
  1167.       set_float_handler (float_error);
  1168.  
  1169. #ifdef REAL_ARITHMETIC
  1170.       REAL_ARITHMETIC (value, code, d1, d2);
  1171. #else
  1172.       switch (code)
  1173.     {
  1174.     case PLUS_EXPR:
  1175.       value = d1 + d2;
  1176.       break;
  1177.  
  1178.     case MINUS_EXPR:
  1179.       value = d1 - d2;
  1180.       break;
  1181.  
  1182.     case MULT_EXPR:
  1183.       value = d1 * d2;
  1184.       break;
  1185.  
  1186.     case RDIV_EXPR:
  1187. #ifndef REAL_INFINITY
  1188.       if (d2 == 0)
  1189.         abort ();
  1190. #endif
  1191.  
  1192.       value = d1 / d2;
  1193.       break;
  1194.  
  1195.     case MIN_EXPR:
  1196.       value = MIN (d1, d2);
  1197.       break;
  1198.  
  1199.     case MAX_EXPR:
  1200.       value = MAX (d1, d2);
  1201.       break;
  1202.  
  1203.     default:
  1204.       abort ();
  1205.     }
  1206. #endif /* no REAL_ARITHMETIC */
  1207.       t = build_real (TREE_TYPE (arg1),
  1208.               real_value_truncate (TYPE_MODE (TREE_TYPE (arg1)), value));
  1209.     got_float:
  1210.       set_float_handler (NULL_PTR);
  1211.  
  1212.       TREE_OVERFLOW (t)
  1213.     = (force_fit_type (t, overflow)
  1214.        | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
  1215.       TREE_CONSTANT_OVERFLOW (t)
  1216.     = TREE_OVERFLOW (t)
  1217.       | TREE_CONSTANT_OVERFLOW (arg1)
  1218.       | TREE_CONSTANT_OVERFLOW (arg2);
  1219.       return t;
  1220.     }
  1221. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  1222.   if (TREE_CODE (arg1) == COMPLEX_CST)
  1223.     {
  1224.       register tree r1 = TREE_REALPART (arg1);
  1225.       register tree i1 = TREE_IMAGPART (arg1);
  1226.       register tree r2 = TREE_REALPART (arg2);
  1227.       register tree i2 = TREE_IMAGPART (arg2);
  1228.       register tree t;
  1229.  
  1230.       switch (code)
  1231.     {
  1232.     case PLUS_EXPR:
  1233.       t = build_complex (const_binop (PLUS_EXPR, r1, r2, notrunc),
  1234.                  const_binop (PLUS_EXPR, i1, i2, notrunc));
  1235.       break;
  1236.  
  1237.     case MINUS_EXPR:
  1238.       t = build_complex (const_binop (MINUS_EXPR, r1, r2, notrunc),
  1239.                  const_binop (MINUS_EXPR, i1, i2, notrunc));
  1240.       break;
  1241.  
  1242.     case MULT_EXPR:
  1243.       t = build_complex (const_binop (MINUS_EXPR,
  1244.                       const_binop (MULT_EXPR,
  1245.                                r1, r2, notrunc),
  1246.                       const_binop (MULT_EXPR,
  1247.                                i1, i2, notrunc),
  1248.                       notrunc),
  1249.                  const_binop (PLUS_EXPR,
  1250.                       const_binop (MULT_EXPR,
  1251.                                r1, i2, notrunc),
  1252.                       const_binop (MULT_EXPR,
  1253.                                i1, r2, notrunc),
  1254.                       notrunc));
  1255.       break;
  1256.  
  1257.     case RDIV_EXPR:
  1258.       {
  1259.         register tree magsquared
  1260.           = const_binop (PLUS_EXPR,
  1261.                  const_binop (MULT_EXPR, r2, r2, notrunc),
  1262.                  const_binop (MULT_EXPR, i2, i2, notrunc),
  1263.                  notrunc);
  1264.  
  1265.         t = build_complex
  1266.           (const_binop (INTEGRAL_TYPE_P (TREE_TYPE (r1))
  1267.                 ? TRUNC_DIV_EXPR : RDIV_EXPR,
  1268.                 const_binop (PLUS_EXPR,
  1269.                      const_binop (MULT_EXPR, r1, r2,
  1270.                               notrunc),
  1271.                      const_binop (MULT_EXPR, i1, i2,
  1272.                               notrunc),
  1273.                      notrunc),
  1274.                 magsquared, notrunc),
  1275.            const_binop (INTEGRAL_TYPE_P (TREE_TYPE (r1))
  1276.                 ? TRUNC_DIV_EXPR : RDIV_EXPR,
  1277.                 const_binop (MINUS_EXPR,
  1278.                      const_binop (MULT_EXPR, i1, r2,
  1279.                               notrunc),
  1280.                      const_binop (MULT_EXPR, r1, i2,
  1281.                               notrunc),
  1282.                      notrunc),
  1283.                 magsquared, notrunc));
  1284.       }
  1285.       break;
  1286.  
  1287.     default:
  1288.       abort ();
  1289.     }
  1290.       TREE_TYPE (t) = TREE_TYPE (arg1);
  1291.       return t;
  1292.     }
  1293.   return 0;
  1294. }
  1295.  
  1296. /* Return an INTEGER_CST with value V and type from `sizetype'.  */
  1297.  
  1298. tree
  1299. size_int (number)
  1300.      unsigned int number;
  1301. {
  1302.   register tree t;
  1303.   /* Type-size nodes already made for small sizes.  */
  1304.   static tree size_table[2*HOST_BITS_PER_WIDE_INT + 1];
  1305.  
  1306.   if (number < 2*HOST_BITS_PER_WIDE_INT + 1
  1307.       && size_table[number] != 0)
  1308.     return size_table[number];
  1309.   if (number < 2*HOST_BITS_PER_WIDE_INT + 1)
  1310.     {
  1311.       push_obstacks_nochange ();
  1312.       /* Make this a permanent node.  */
  1313.       end_temporary_allocation ();
  1314.       t = build_int_2 (number, 0);
  1315.       TREE_TYPE (t) = sizetype;
  1316.       size_table[number] = t;
  1317.       pop_obstacks ();
  1318.     }
  1319.   else
  1320.     {
  1321.       t = build_int_2 (number, 0);
  1322.       TREE_TYPE (t) = sizetype;
  1323.     }
  1324.   return t;
  1325. }
  1326.  
  1327. /* Combine operands OP1 and OP2 with arithmetic operation CODE.
  1328.    CODE is a tree code.  Data type is taken from `sizetype',
  1329.    If the operands are constant, so is the result.  */
  1330.  
  1331. tree
  1332. size_binop (code, arg0, arg1)
  1333.      enum tree_code code;
  1334.      tree arg0, arg1;
  1335. {
  1336.   /* Handle the special case of two integer constants faster.  */
  1337.   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
  1338.     {
  1339.       /* And some specific cases even faster than that.  */
  1340.       if (code == PLUS_EXPR
  1341.       && TREE_INT_CST_LOW (arg0) == 0
  1342.       && TREE_INT_CST_HIGH (arg0) == 0)
  1343.     return arg1;
  1344.       if (code == MINUS_EXPR
  1345.       && TREE_INT_CST_LOW (arg1) == 0
  1346.       && TREE_INT_CST_HIGH (arg1) == 0)
  1347.     return arg0;
  1348.       if (code == MULT_EXPR
  1349.       && TREE_INT_CST_LOW (arg0) == 1
  1350.       && TREE_INT_CST_HIGH (arg0) == 0)
  1351.     return arg1;
  1352.       /* Handle general case of two integer constants.  */
  1353.       return const_binop (code, arg0, arg1, 1);
  1354.     }
  1355.  
  1356.   if (arg0 == error_mark_node || arg1 == error_mark_node)
  1357.     return error_mark_node;
  1358.  
  1359.   return fold (build (code, sizetype, arg0, arg1));
  1360. }
  1361.  
  1362. /* Given T, a tree representing type conversion of ARG1, a constant,
  1363.    return a constant tree representing the result of conversion.  */
  1364.  
  1365. static tree
  1366. fold_convert (t, arg1)
  1367.      register tree t;
  1368.      register tree arg1;
  1369. {
  1370.   register tree type = TREE_TYPE (t);
  1371.   int overflow = 0;
  1372.  
  1373.   if (TREE_CODE (type) == POINTER_TYPE || INTEGRAL_TYPE_P (type))
  1374.     {
  1375.       if (TREE_CODE (arg1) == INTEGER_CST)
  1376.     {
  1377.       /* If we would build a constant wider than GCC supports,
  1378.          leave the conversion unfolded.  */
  1379.       if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
  1380.         return t;
  1381.  
  1382.       /* Given an integer constant, make new constant with new type,
  1383.          appropriately sign-extended or truncated.  */
  1384.       t = build_int_2 (TREE_INT_CST_LOW (arg1),
  1385.                TREE_INT_CST_HIGH (arg1));
  1386.       TREE_TYPE (t) = type;
  1387.       /* Indicate an overflow if (1) ARG1 already overflowed,
  1388.          or (2) force_fit_type indicates an overflow.
  1389.          Tell force_fit_type that an overflow has already occurred
  1390.          if ARG1 is a too-large unsigned value and T is signed.  */
  1391.       TREE_OVERFLOW (t)
  1392.         = (TREE_OVERFLOW (arg1)
  1393.            | force_fit_type (t,
  1394.                  (TREE_INT_CST_HIGH (arg1) < 0
  1395.                   & (TREE_UNSIGNED (type)
  1396.                      < TREE_UNSIGNED (TREE_TYPE (arg1))))));
  1397.       TREE_CONSTANT_OVERFLOW (t)
  1398.         = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
  1399.     }
  1400. #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  1401.       else if (TREE_CODE (arg1) == REAL_CST)
  1402.     {
  1403.       /* Don't initialize these, use assignments.
  1404.          Initialized local aggregates don't work on old compilers.  */
  1405.       REAL_VALUE_TYPE x;
  1406.       REAL_VALUE_TYPE l;
  1407.       REAL_VALUE_TYPE u;
  1408.  
  1409.       x = TREE_REAL_CST (arg1);
  1410.       l = real_value_from_int_cst (TYPE_MIN_VALUE (type));
  1411.       u = real_value_from_int_cst (TYPE_MAX_VALUE (type));
  1412.       /* See if X will be in range after truncation towards 0.
  1413.          To compensate for truncation, move the bounds away from 0,
  1414.          but reject if X exactly equals the adjusted bounds.  */
  1415. #ifdef REAL_ARITHMETIC
  1416.       REAL_ARITHMETIC (l, MINUS_EXPR, l, dconst1);
  1417.       REAL_ARITHMETIC (u, PLUS_EXPR, u, dconst1);
  1418. #else
  1419.       l--;
  1420.       u++;
  1421. #endif
  1422.       /* If X is a NaN, use zero instead and show we have an overflow.
  1423.          Otherwise, range check.  */
  1424.       if (REAL_VALUE_ISNAN (x))
  1425.         overflow = 1, x = dconst0;
  1426.       else if (! (REAL_VALUES_LESS (l, x) && REAL_VALUES_LESS (x, u)))
  1427.         overflow = 1;
  1428.  
  1429. #ifndef REAL_ARITHMETIC
  1430.       {
  1431.         HOST_WIDE_INT low, high;
  1432.         HOST_WIDE_INT half_word
  1433.           = (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2);
  1434.  
  1435.         if (x < 0)
  1436.           x = -x;
  1437.  
  1438.         high = (HOST_WIDE_INT) (x / half_word / half_word);
  1439.         x -= (REAL_VALUE_TYPE) high * half_word * half_word;
  1440.         if (x >= (REAL_VALUE_TYPE) half_word * half_word / 2)
  1441.           {
  1442.         low = x - (REAL_VALUE_TYPE) half_word * half_word / 2;
  1443.         low |= (HOST_WIDE_INT) -1 << (HOST_BITS_PER_WIDE_INT - 1);
  1444.           }
  1445.         else
  1446.           low = (HOST_WIDE_INT) x;
  1447.         if (TREE_REAL_CST (arg1) < 0)
  1448.           neg_double (low, high, &low, &high);
  1449.         t = build_int_2 (low, high);
  1450.       }
  1451. #else
  1452.       {
  1453.         HOST_WIDE_INT low, high;
  1454.         REAL_VALUE_TO_INT (&low, &high, x);
  1455.         t = build_int_2 (low, high);
  1456.       }
  1457. #endif
  1458.       TREE_TYPE (t) = type;
  1459.       TREE_OVERFLOW (t)
  1460.         = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
  1461.       TREE_CONSTANT_OVERFLOW (t)
  1462.         = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
  1463.     }
  1464. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  1465.       TREE_TYPE (t) = type;
  1466.     }
  1467.   else if (TREE_CODE (type) == REAL_TYPE)
  1468.     {
  1469. #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  1470.       if (TREE_CODE (arg1) == INTEGER_CST)
  1471.     return build_real_from_int_cst (type, arg1);
  1472. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  1473.       if (TREE_CODE (arg1) == REAL_CST)
  1474.     {
  1475.       if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
  1476.         return arg1;
  1477.       else if (setjmp (float_error))
  1478.         {
  1479.           overflow = 1;
  1480.           t = copy_node (arg1);
  1481.           goto got_it;
  1482.         }
  1483.       set_float_handler (float_error);
  1484.  
  1485.       t = build_real (type, real_value_truncate (TYPE_MODE (type),
  1486.                              TREE_REAL_CST (arg1)));
  1487.       set_float_handler (NULL_PTR);
  1488.  
  1489.     got_it:
  1490.       TREE_OVERFLOW (t)
  1491.         = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
  1492.       TREE_CONSTANT_OVERFLOW (t)
  1493.         = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
  1494.       return t;
  1495.     }
  1496.     }
  1497.   TREE_CONSTANT (t) = 1;
  1498.   return t;
  1499. }
  1500.  
  1501. /* Return an expr equal to X but certainly not valid as an lvalue.
  1502.    Also make sure it is not valid as an null pointer constant.  */
  1503.  
  1504. tree
  1505. non_lvalue (x)
  1506.      tree x;
  1507. {
  1508.   tree result;
  1509.  
  1510.   /* These things are certainly not lvalues.  */
  1511.   if (TREE_CODE (x) == NON_LVALUE_EXPR
  1512.       || TREE_CODE (x) == INTEGER_CST
  1513.       || TREE_CODE (x) == REAL_CST
  1514.       || TREE_CODE (x) == STRING_CST
  1515.       || TREE_CODE (x) == ADDR_EXPR)
  1516.     {
  1517.       if (TREE_CODE (x) == INTEGER_CST && integer_zerop (x))
  1518.     {
  1519.       /* Use NOP_EXPR instead of NON_LVALUE_EXPR
  1520.          so convert_for_assignment won't strip it.
  1521.          This is so this 0 won't be treated as a null pointer constant.  */
  1522.       result = build1 (NOP_EXPR, TREE_TYPE (x), x);
  1523.       TREE_CONSTANT (result) = TREE_CONSTANT (x);
  1524.       return result;
  1525.     }
  1526.       return x;
  1527.     }
  1528.  
  1529.   result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
  1530.   TREE_CONSTANT (result) = TREE_CONSTANT (x);
  1531.   return result;
  1532. }
  1533.  
  1534. /* When pedantic, return an expr equal to X but certainly not valid as a
  1535.    pedantic lvalue.  Otherwise, return X.  */
  1536.  
  1537. tree
  1538. pedantic_non_lvalue (x)
  1539.      tree x;
  1540. {
  1541.   if (pedantic)
  1542.     return non_lvalue (x);
  1543.   else
  1544.     return x;
  1545. }
  1546.  
  1547. /* Given a tree comparison code, return the code that is the logical inverse
  1548.    of the given code.  It is not safe to do this for floating-point
  1549.    comparisons, except for NE_EXPR and EQ_EXPR.  */
  1550.  
  1551. static enum tree_code
  1552. invert_tree_comparison (code)
  1553.      enum tree_code code;
  1554. {
  1555.   switch (code)
  1556.     {
  1557.     case EQ_EXPR:
  1558.       return NE_EXPR;
  1559.     case NE_EXPR:
  1560.       return EQ_EXPR;
  1561.     case GT_EXPR:
  1562.       return LE_EXPR;
  1563.     case GE_EXPR:
  1564.       return LT_EXPR;
  1565.     case LT_EXPR:
  1566.       return GE_EXPR;
  1567.     case LE_EXPR:
  1568.       return GT_EXPR;
  1569.     default:
  1570.       abort ();
  1571.     }
  1572. }
  1573.  
  1574. /* Similar, but return the comparison that results if the operands are
  1575.    swapped.  This is safe for floating-point.  */
  1576.  
  1577. static enum tree_code
  1578. swap_tree_comparison (code)
  1579.      enum tree_code code;
  1580. {
  1581.   switch (code)
  1582.     {
  1583.     case EQ_EXPR:
  1584.     case NE_EXPR:
  1585.       return code;
  1586.     case GT_EXPR:
  1587.       return LT_EXPR;
  1588.     case GE_EXPR:
  1589.       return LE_EXPR;
  1590.     case LT_EXPR:
  1591.       return GT_EXPR;
  1592.     case LE_EXPR:
  1593.       return GE_EXPR;
  1594.     default:
  1595.       abort ();
  1596.     }
  1597. }
  1598.  
  1599. /* Return nonzero if CODE is a tree code that represents a truth value.  */
  1600.  
  1601. static int
  1602. truth_value_p (code)
  1603.      enum tree_code code;
  1604. {
  1605.   return (TREE_CODE_CLASS (code) == '<'
  1606.       || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
  1607.       || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
  1608.       || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
  1609. }
  1610.  
  1611. /* Return nonzero if two operands are necessarily equal.
  1612.    If ONLY_CONST is non-zero, only return non-zero for constants.
  1613.    This function tests whether the operands are indistinguishable;
  1614.    it does not test whether they are equal using C's == operation.
  1615.    The distinction is important for IEEE floating point, because
  1616.    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
  1617.    (2) two NaNs may be indistinguishable, but NaN!=NaN.  */
  1618.  
  1619. int
  1620. operand_equal_p (arg0, arg1, only_const)
  1621.      tree arg0, arg1;
  1622.      int only_const;
  1623. {
  1624.   /* If both types don't have the same signedness, then we can't consider
  1625.      them equal.  We must check this before the STRIP_NOPS calls
  1626.      because they may change the signedness of the arguments.  */
  1627.   if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1)))
  1628.     return 0;
  1629.  
  1630.   STRIP_NOPS (arg0);
  1631.   STRIP_NOPS (arg1);
  1632.  
  1633.   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
  1634.      We don't care about side effects in that case because the SAVE_EXPR
  1635.      takes care of that for us.  */
  1636.   if (TREE_CODE (arg0) == SAVE_EXPR && arg0 == arg1)
  1637.     return ! only_const;
  1638.  
  1639.   if (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1))
  1640.     return 0;
  1641.  
  1642.   if (TREE_CODE (arg0) == TREE_CODE (arg1)
  1643.       && TREE_CODE (arg0) == ADDR_EXPR
  1644.       && TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0))
  1645.     return 1;
  1646.  
  1647.   if (TREE_CODE (arg0) == TREE_CODE (arg1)
  1648.       && TREE_CODE (arg0) == INTEGER_CST
  1649.       && TREE_INT_CST_LOW (arg0) == TREE_INT_CST_LOW (arg1)
  1650.       && TREE_INT_CST_HIGH (arg0) == TREE_INT_CST_HIGH (arg1))
  1651.     return 1;
  1652.  
  1653.   /* Detect when real constants are equal.  */
  1654.   if (TREE_CODE (arg0) == TREE_CODE (arg1)
  1655.       && TREE_CODE (arg0) == REAL_CST)
  1656.     return !bcmp ((char *) &TREE_REAL_CST (arg0),
  1657.           (char *) &TREE_REAL_CST (arg1),
  1658.           sizeof (REAL_VALUE_TYPE));
  1659.  
  1660.   if (only_const)
  1661.     return 0;
  1662.  
  1663.   if (arg0 == arg1)
  1664.     return 1;
  1665.  
  1666.   if (TREE_CODE (arg0) != TREE_CODE (arg1))
  1667.     return 0;
  1668.   /* This is needed for conversions and for COMPONENT_REF.
  1669.      Might as well play it safe and always test this.  */
  1670.   if (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
  1671.     return 0;
  1672.  
  1673.   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
  1674.     {
  1675.     case '1':
  1676.       /* Two conversions are equal only if signedness and modes match.  */
  1677.       if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
  1678.       && (TREE_UNSIGNED (TREE_TYPE (arg0))
  1679.           != TREE_UNSIGNED (TREE_TYPE (arg1))))
  1680.     return 0;
  1681.  
  1682.       return operand_equal_p (TREE_OPERAND (arg0, 0),
  1683.                   TREE_OPERAND (arg1, 0), 0);
  1684.  
  1685.     case '<':
  1686.     case '2':
  1687.       return (operand_equal_p (TREE_OPERAND (arg0, 0),
  1688.                    TREE_OPERAND (arg1, 0), 0)
  1689.           && operand_equal_p (TREE_OPERAND (arg0, 1),
  1690.                   TREE_OPERAND (arg1, 1), 0));
  1691.  
  1692.     case 'r':
  1693.       switch (TREE_CODE (arg0))
  1694.     {
  1695.     case INDIRECT_REF:
  1696.       return operand_equal_p (TREE_OPERAND (arg0, 0),
  1697.                   TREE_OPERAND (arg1, 0), 0);
  1698.  
  1699.     case COMPONENT_REF:
  1700.     case ARRAY_REF:
  1701.       return (operand_equal_p (TREE_OPERAND (arg0, 0),
  1702.                    TREE_OPERAND (arg1, 0), 0)
  1703.           && operand_equal_p (TREE_OPERAND (arg0, 1),
  1704.                       TREE_OPERAND (arg1, 1), 0));
  1705.  
  1706.     case BIT_FIELD_REF:
  1707.       return (operand_equal_p (TREE_OPERAND (arg0, 0),
  1708.                    TREE_OPERAND (arg1, 0), 0)
  1709.           && operand_equal_p (TREE_OPERAND (arg0, 1),
  1710.                       TREE_OPERAND (arg1, 1), 0)
  1711.           && operand_equal_p (TREE_OPERAND (arg0, 2),
  1712.                       TREE_OPERAND (arg1, 2), 0));
  1713.     }
  1714.       break;
  1715.     }
  1716.  
  1717.   return 0;
  1718. }
  1719.  
  1720. /* Similar to operand_equal_p, but see if ARG0 might have been made by
  1721.    shorten_compare from ARG1 when ARG1 was being compared with OTHER. 
  1722.  
  1723.    When in doubt, return 0.  */
  1724.  
  1725. static int 
  1726. operand_equal_for_comparison_p (arg0, arg1, other)
  1727.      tree arg0, arg1;
  1728.      tree other;
  1729. {
  1730.   int unsignedp1, unsignedpo;
  1731.   tree primarg1, primother;
  1732.   unsigned correct_width;
  1733.  
  1734.   if (operand_equal_p (arg0, arg1, 0))
  1735.     return 1;
  1736.  
  1737.   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
  1738.     return 0;
  1739.  
  1740.   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
  1741.      actual comparison operand, ARG0.
  1742.  
  1743.      First throw away any conversions to wider types
  1744.      already present in the operands.  */
  1745.  
  1746.   primarg1 = get_narrower (arg1, &unsignedp1);
  1747.   primother = get_narrower (other, &unsignedpo);
  1748.  
  1749.   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
  1750.   if (unsignedp1 == unsignedpo
  1751.       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
  1752.       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
  1753.     {
  1754.       tree type = TREE_TYPE (arg0);
  1755.  
  1756.       /* Make sure shorter operand is extended the right way
  1757.      to match the longer operand.  */
  1758.       primarg1 = convert (signed_or_unsigned_type (unsignedp1,
  1759.                           TREE_TYPE (primarg1)),
  1760.              primarg1);
  1761.  
  1762.       if (operand_equal_p (arg0, convert (type, primarg1), 0))
  1763.     return 1;
  1764.     }
  1765.  
  1766.   return 0;
  1767. }
  1768.  
  1769. /* See if ARG is an expression that is either a comparison or is performing
  1770.    arithmetic on comparisons.  The comparisons must only be comparing
  1771.    two different values, which will be stored in *CVAL1 and *CVAL2; if
  1772.    they are non-zero it means that some operands have already been found.
  1773.    No variables may be used anywhere else in the expression except in the
  1774.    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
  1775.    the expression and save_expr needs to be called with CVAL1 and CVAL2.
  1776.  
  1777.    If this is true, return 1.  Otherwise, return zero.  */
  1778.  
  1779. static int
  1780. twoval_comparison_p (arg, cval1, cval2, save_p)
  1781.      tree arg;
  1782.      tree *cval1, *cval2;
  1783.      int *save_p;
  1784. {
  1785.   enum tree_code code = TREE_CODE (arg);
  1786.   char class = TREE_CODE_CLASS (code);
  1787.  
  1788.   /* We can handle some of the 'e' cases here.  */
  1789.   if (class == 'e' && code == TRUTH_NOT_EXPR)
  1790.     class = '1';
  1791.   else if (class == 'e'
  1792.        && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
  1793.            || code == COMPOUND_EXPR))
  1794.     class = '2';
  1795.  
  1796.   /* ??? Disable this since the SAVE_EXPR might already be in use outside
  1797.      the expression.  There may be no way to make this work, but it needs
  1798.      to be looked at again for 2.6.  */
  1799. #if 0
  1800.   else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0)
  1801.     {
  1802.       /* If we've already found a CVAL1 or CVAL2, this expression is
  1803.      two complex to handle.  */
  1804.       if (*cval1 || *cval2)
  1805.     return 0;
  1806.  
  1807.       class = '1';
  1808.       *save_p = 1;
  1809.     }
  1810. #endif
  1811.  
  1812.   switch (class)
  1813.     {
  1814.     case '1':
  1815.       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
  1816.  
  1817.     case '2':
  1818.       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
  1819.           && twoval_comparison_p (TREE_OPERAND (arg, 1),
  1820.                       cval1, cval2, save_p));
  1821.  
  1822.     case 'c':
  1823.       return 1;
  1824.  
  1825.     case 'e':
  1826.       if (code == COND_EXPR)
  1827.     return (twoval_comparison_p (TREE_OPERAND (arg, 0),
  1828.                      cval1, cval2, save_p)
  1829.         && twoval_comparison_p (TREE_OPERAND (arg, 1),
  1830.                     cval1, cval2, save_p)
  1831.         && twoval_comparison_p (TREE_OPERAND (arg, 2),
  1832.                     cval1, cval2, save_p));
  1833.       return 0;
  1834.       
  1835.     case '<':
  1836.       /* First see if we can handle the first operand, then the second.  For
  1837.      the second operand, we know *CVAL1 can't be zero.  It must be that
  1838.      one side of the comparison is each of the values; test for the
  1839.      case where this isn't true by failing if the two operands
  1840.      are the same.  */
  1841.  
  1842.       if (operand_equal_p (TREE_OPERAND (arg, 0),
  1843.                TREE_OPERAND (arg, 1), 0))
  1844.     return 0;
  1845.  
  1846.       if (*cval1 == 0)
  1847.     *cval1 = TREE_OPERAND (arg, 0);
  1848.       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
  1849.     ;
  1850.       else if (*cval2 == 0)
  1851.     *cval2 = TREE_OPERAND (arg, 0);
  1852.       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
  1853.     ;
  1854.       else
  1855.     return 0;
  1856.  
  1857.       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
  1858.     ;
  1859.       else if (*cval2 == 0)
  1860.     *cval2 = TREE_OPERAND (arg, 1);
  1861.       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
  1862.     ;
  1863.       else
  1864.     return 0;
  1865.  
  1866.       return 1;
  1867.     }
  1868.  
  1869.   return 0;
  1870. }
  1871.  
  1872. /* ARG is a tree that is known to contain just arithmetic operations and
  1873.    comparisons.  Evaluate the operations in the tree substituting NEW0 for
  1874.    any occurrence of OLD0 as an operand of a comparison and likewise for
  1875.    NEW1 and OLD1.  */
  1876.  
  1877. static tree
  1878. eval_subst (arg, old0, new0, old1, new1)
  1879.      tree arg;
  1880.      tree old0, new0, old1, new1;
  1881. {
  1882.   tree type = TREE_TYPE (arg);
  1883.   enum tree_code code = TREE_CODE (arg);
  1884.   char class = TREE_CODE_CLASS (code);
  1885.  
  1886.   /* We can handle some of the 'e' cases here.  */
  1887.   if (class == 'e' && code == TRUTH_NOT_EXPR)
  1888.     class = '1';
  1889.   else if (class == 'e'
  1890.        && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
  1891.     class = '2';
  1892.  
  1893.   switch (class)
  1894.     {
  1895.     case '1':
  1896.       return fold (build1 (code, type,
  1897.                eval_subst (TREE_OPERAND (arg, 0),
  1898.                        old0, new0, old1, new1)));
  1899.  
  1900.     case '2':
  1901.       return fold (build (code, type,
  1902.               eval_subst (TREE_OPERAND (arg, 0),
  1903.                       old0, new0, old1, new1),
  1904.               eval_subst (TREE_OPERAND (arg, 1),
  1905.                       old0, new0, old1, new1)));
  1906.  
  1907.     case 'e':
  1908.       switch (code)
  1909.     {
  1910.     case SAVE_EXPR:
  1911.       return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
  1912.  
  1913.     case COMPOUND_EXPR:
  1914.       return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
  1915.  
  1916.     case COND_EXPR:
  1917.       return fold (build (code, type,
  1918.                   eval_subst (TREE_OPERAND (arg, 0),
  1919.                       old0, new0, old1, new1),
  1920.                   eval_subst (TREE_OPERAND (arg, 1),
  1921.                       old0, new0, old1, new1),
  1922.                   eval_subst (TREE_OPERAND (arg, 2),
  1923.                       old0, new0, old1, new1)));
  1924.     }
  1925.  
  1926.     case '<':
  1927.       {
  1928.     tree arg0 = TREE_OPERAND (arg, 0);
  1929.     tree arg1 = TREE_OPERAND (arg, 1);
  1930.  
  1931.     /* We need to check both for exact equality and tree equality.  The
  1932.        former will be true if the operand has a side-effect.  In that
  1933.        case, we know the operand occurred exactly once.  */
  1934.  
  1935.     if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
  1936.       arg0 = new0;
  1937.     else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
  1938.       arg0 = new1;
  1939.  
  1940.     if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
  1941.       arg1 = new0;
  1942.     else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
  1943.       arg1 = new1;
  1944.  
  1945.     return fold (build (code, type, arg0, arg1));
  1946.       }
  1947.     }
  1948.  
  1949.   return arg;
  1950. }
  1951.  
  1952. /* Return a tree for the case when the result of an expression is RESULT
  1953.    converted to TYPE and OMITTED was previously an operand of the expression
  1954.    but is now not needed (e.g., we folded OMITTED * 0).
  1955.  
  1956.    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
  1957.    the conversion of RESULT to TYPE.  */
  1958.  
  1959. static tree
  1960. omit_one_operand (type, result, omitted)
  1961.      tree type, result, omitted;
  1962. {
  1963.   tree t = convert (type, result);
  1964.  
  1965.   if (TREE_SIDE_EFFECTS (omitted))
  1966.     return build (COMPOUND_EXPR, type, omitted, t);
  1967.  
  1968.   return non_lvalue (t);
  1969. }
  1970.  
  1971. /* Return a simplified tree node for the truth-negation of ARG.  This
  1972.    never alters ARG itself.  We assume that ARG is an operation that
  1973.    returns a truth value (0 or 1).  */
  1974.  
  1975. tree
  1976. invert_truthvalue (arg)
  1977.      tree arg;
  1978. {
  1979.   tree type = TREE_TYPE (arg);
  1980.   enum tree_code code = TREE_CODE (arg);
  1981.  
  1982.   if (code == ERROR_MARK)
  1983.     return arg;
  1984.  
  1985.   /* If this is a comparison, we can simply invert it, except for
  1986.      floating-point non-equality comparisons, in which case we just
  1987.      enclose a TRUTH_NOT_EXPR around what we have.  */
  1988.  
  1989.   if (TREE_CODE_CLASS (code) == '<')
  1990.     {
  1991.       if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
  1992.       && code != NE_EXPR && code != EQ_EXPR)
  1993.     return build1 (TRUTH_NOT_EXPR, type, arg);
  1994.       else
  1995.     return build (invert_tree_comparison (code), type,
  1996.               TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
  1997.     }
  1998.  
  1999.   switch (code)
  2000.     {
  2001.     case INTEGER_CST:
  2002.       return convert (type, build_int_2 (TREE_INT_CST_LOW (arg) == 0
  2003.                      && TREE_INT_CST_HIGH (arg) == 0, 0));
  2004.  
  2005.     case TRUTH_AND_EXPR:
  2006.       return build (TRUTH_OR_EXPR, type,
  2007.             invert_truthvalue (TREE_OPERAND (arg, 0)),
  2008.             invert_truthvalue (TREE_OPERAND (arg, 1)));
  2009.  
  2010.     case TRUTH_OR_EXPR:
  2011.       return build (TRUTH_AND_EXPR, type,
  2012.             invert_truthvalue (TREE_OPERAND (arg, 0)),
  2013.             invert_truthvalue (TREE_OPERAND (arg, 1)));
  2014.  
  2015.     case TRUTH_XOR_EXPR:
  2016.       /* Here we can invert either operand.  We invert the first operand
  2017.      unless the second operand is a TRUTH_NOT_EXPR in which case our
  2018.      result is the XOR of the first operand with the inside of the
  2019.      negation of the second operand.  */
  2020.  
  2021.       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
  2022.     return build (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
  2023.               TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
  2024.       else
  2025.     return build (TRUTH_XOR_EXPR, type,
  2026.               invert_truthvalue (TREE_OPERAND (arg, 0)),
  2027.               TREE_OPERAND (arg, 1));
  2028.  
  2029.     case TRUTH_ANDIF_EXPR:
  2030.       return build (TRUTH_ORIF_EXPR, type,
  2031.             invert_truthvalue (TREE_OPERAND (arg, 0)),
  2032.             invert_truthvalue (TREE_OPERAND (arg, 1)));
  2033.  
  2034.     case TRUTH_ORIF_EXPR:
  2035.       return build (TRUTH_ANDIF_EXPR, type,
  2036.             invert_truthvalue (TREE_OPERAND (arg, 0)),
  2037.             invert_truthvalue (TREE_OPERAND (arg, 1)));
  2038.  
  2039.     case TRUTH_NOT_EXPR:
  2040.       return TREE_OPERAND (arg, 0);
  2041.  
  2042.     case COND_EXPR:
  2043.       return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
  2044.             invert_truthvalue (TREE_OPERAND (arg, 1)),
  2045.             invert_truthvalue (TREE_OPERAND (arg, 2)));
  2046.  
  2047.     case COMPOUND_EXPR:
  2048.       return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
  2049.             invert_truthvalue (TREE_OPERAND (arg, 1)));
  2050.  
  2051.     case NON_LVALUE_EXPR:
  2052.       return invert_truthvalue (TREE_OPERAND (arg, 0));
  2053.  
  2054.     case NOP_EXPR:
  2055.     case CONVERT_EXPR:
  2056.     case FLOAT_EXPR:
  2057.       return build1 (TREE_CODE (arg), type,
  2058.              invert_truthvalue (TREE_OPERAND (arg, 0)));
  2059.  
  2060.     case BIT_AND_EXPR:
  2061.       if (!integer_onep (TREE_OPERAND (arg, 1)))
  2062.     break;
  2063.       return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
  2064.  
  2065.     case SAVE_EXPR:
  2066.       return build1 (TRUTH_NOT_EXPR, type, arg);
  2067.     }
  2068.   if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
  2069.     abort ();
  2070.   return build1 (TRUTH_NOT_EXPR, type, arg);
  2071. }
  2072.  
  2073. /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
  2074.    operands are another bit-wise operation with a common input.  If so,
  2075.    distribute the bit operations to save an operation and possibly two if
  2076.    constants are involved.  For example, convert
  2077.        (A | B) & (A | C) into A | (B & C)
  2078.    Further simplification will occur if B and C are constants.
  2079.  
  2080.    If this optimization cannot be done, 0 will be returned.  */
  2081.  
  2082. static tree
  2083. distribute_bit_expr (code, type, arg0, arg1)
  2084.      enum tree_code code;
  2085.      tree type;
  2086.      tree arg0, arg1;
  2087. {
  2088.   tree common;
  2089.   tree left, right;
  2090.  
  2091.   if (TREE_CODE (arg0) != TREE_CODE (arg1)
  2092.       || TREE_CODE (arg0) == code
  2093.       || (TREE_CODE (arg0) != BIT_AND_EXPR
  2094.       && TREE_CODE (arg0) != BIT_IOR_EXPR))
  2095.     return 0;
  2096.  
  2097.   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
  2098.     {
  2099.       common = TREE_OPERAND (arg0, 0);
  2100.       left = TREE_OPERAND (arg0, 1);
  2101.       right = TREE_OPERAND (arg1, 1);
  2102.     }
  2103.   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
  2104.     {
  2105.       common = TREE_OPERAND (arg0, 0);
  2106.       left = TREE_OPERAND (arg0, 1);
  2107.       right = TREE_OPERAND (arg1, 0);
  2108.     }
  2109.   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
  2110.     {
  2111.       common = TREE_OPERAND (arg0, 1);
  2112.       left = TREE_OPERAND (arg0, 0);
  2113.       right = TREE_OPERAND (arg1, 1);
  2114.     }
  2115.   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
  2116.     {
  2117.       common = TREE_OPERAND (arg0, 1);
  2118.       left = TREE_OPERAND (arg0, 0);
  2119.       right = TREE_OPERAND (arg1, 0);
  2120.     }
  2121.   else
  2122.     return 0;
  2123.  
  2124.   return fold (build (TREE_CODE (arg0), type, common,
  2125.               fold (build (code, type, left, right))));
  2126. }
  2127.  
  2128. /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
  2129.    starting at BITPOS.  The field is unsigned if UNSIGNEDP is non-zero.  */
  2130.  
  2131. static tree
  2132. make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp)
  2133.      tree inner;
  2134.      tree type;
  2135.      int bitsize, bitpos;
  2136.      int unsignedp;
  2137. {
  2138.   tree result = build (BIT_FIELD_REF, type, inner,
  2139.                size_int (bitsize), size_int (bitpos));
  2140.  
  2141.   TREE_UNSIGNED (result) = unsignedp;
  2142.  
  2143.   return result;
  2144. }
  2145.  
  2146. /* Optimize a bit-field compare.
  2147.  
  2148.    There are two cases:  First is a compare against a constant and the
  2149.    second is a comparison of two items where the fields are at the same
  2150.    bit position relative to the start of a chunk (byte, halfword, word)
  2151.    large enough to contain it.  In these cases we can avoid the shift
  2152.    implicit in bitfield extractions.
  2153.  
  2154.    For constants, we emit a compare of the shifted constant with the
  2155.    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
  2156.    compared.  For two fields at the same position, we do the ANDs with the
  2157.    similar mask and compare the result of the ANDs.
  2158.  
  2159.    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
  2160.    COMPARE_TYPE is the type of the comparison, and LHS and RHS
  2161.    are the left and right operands of the comparison, respectively.
  2162.  
  2163.    If the optimization described above can be done, we return the resulting
  2164.    tree.  Otherwise we return zero.  */
  2165.  
  2166. static tree
  2167. optimize_bit_field_compare (code, compare_type, lhs, rhs)
  2168.      enum tree_code code;
  2169.      tree compare_type;
  2170.      tree lhs, rhs;
  2171. {
  2172.   int lbitpos, lbitsize, rbitpos, rbitsize;
  2173.   int lnbitpos, lnbitsize, rnbitpos, rnbitsize;
  2174.   tree type = TREE_TYPE (lhs);
  2175.   tree signed_type, unsigned_type;
  2176.   int const_p = TREE_CODE (rhs) == INTEGER_CST;
  2177.   enum machine_mode lmode, rmode, lnmode, rnmode;
  2178.   int lunsignedp, runsignedp;
  2179.   int lvolatilep = 0, rvolatilep = 0;
  2180.   tree linner, rinner;
  2181.   tree mask;
  2182.   tree offset;
  2183.  
  2184.   /* Get all the information about the extractions being done.  If the bit size
  2185.      if the same as the size of the underlying object, we aren't doing an
  2186.      extraction at all and so can do nothing.  */
  2187.   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
  2188.                 &lunsignedp, &lvolatilep);
  2189.   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
  2190.       || offset != 0)
  2191.     return 0;
  2192.  
  2193.  if (!const_p)
  2194.    {
  2195.      /* If this is not a constant, we can only do something if bit positions,
  2196.     sizes, and signedness are the same.   */
  2197.      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset,
  2198.                    &rmode, &runsignedp, &rvolatilep);
  2199.  
  2200.      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
  2201.      || lunsignedp != runsignedp || offset != 0)
  2202.        return 0;
  2203.    }
  2204.  
  2205.   /* See if we can find a mode to refer to this field.  We should be able to,
  2206.      but fail if we can't.  */
  2207.   lnmode = get_best_mode (lbitsize, lbitpos,
  2208.               TYPE_ALIGN (TREE_TYPE (linner)), word_mode,
  2209.               lvolatilep);
  2210.   if (lnmode == VOIDmode)
  2211.     return 0;
  2212.  
  2213.   /* Set signed and unsigned types of the precision of this mode for the
  2214.      shifts below.  */
  2215.   signed_type = type_for_mode (lnmode, 0);
  2216.   unsigned_type = type_for_mode (lnmode, 1);
  2217.  
  2218.   if (! const_p)
  2219.     {
  2220.       rnmode = get_best_mode (rbitsize, rbitpos, 
  2221.                   TYPE_ALIGN (TREE_TYPE (rinner)), word_mode,
  2222.                   rvolatilep);
  2223.       if (rnmode == VOIDmode)
  2224.     return 0;
  2225.     }
  2226.     
  2227.   /* Compute the bit position and size for the new reference and our offset
  2228.      within it. If the new reference is the same size as the original, we
  2229.      won't optimize anything, so return zero.  */
  2230.   lnbitsize = GET_MODE_BITSIZE (lnmode);
  2231.   lnbitpos = lbitpos & ~ (lnbitsize - 1);
  2232.   lbitpos -= lnbitpos;
  2233.   if (lnbitsize == lbitsize)
  2234.     return 0;
  2235.  
  2236.   if (! const_p)
  2237.     {
  2238.       rnbitsize = GET_MODE_BITSIZE (rnmode);
  2239.       rnbitpos = rbitpos & ~ (rnbitsize - 1);
  2240.       rbitpos -= rnbitpos;
  2241.       if (rnbitsize == rbitsize)
  2242.     return 0;
  2243.     }
  2244.  
  2245. #if BYTES_BIG_ENDIAN
  2246.   lbitpos = lnbitsize - lbitsize - lbitpos;
  2247. #endif
  2248.  
  2249.   /* Make the mask to be used against the extracted field.  */
  2250.   mask = build_int_2 (~0, ~0);
  2251.   TREE_TYPE (mask) = unsigned_type;
  2252.   force_fit_type (mask, 0);
  2253.   mask = convert (unsigned_type, mask);
  2254.   mask = const_binop (LSHIFT_EXPR, mask, size_int (lnbitsize - lbitsize), 0);
  2255.   mask = const_binop (RSHIFT_EXPR, mask,
  2256.               size_int (lnbitsize - lbitsize - lbitpos), 0);
  2257.  
  2258.   if (! const_p)
  2259.     /* If not comparing with constant, just rework the comparison
  2260.        and return.  */
  2261.     return build (code, compare_type,
  2262.           build (BIT_AND_EXPR, unsigned_type,
  2263.              make_bit_field_ref (linner, unsigned_type,
  2264.                          lnbitsize, lnbitpos, 1),
  2265.              mask),
  2266.           build (BIT_AND_EXPR, unsigned_type,
  2267.              make_bit_field_ref (rinner, unsigned_type,
  2268.                          rnbitsize, rnbitpos, 1),
  2269.              mask));
  2270.  
  2271.   /* Otherwise, we are handling the constant case. See if the constant is too
  2272.      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
  2273.      this not only for its own sake, but to avoid having to test for this
  2274.      error case below.  If we didn't, we might generate wrong code.
  2275.  
  2276.      For unsigned fields, the constant shifted right by the field length should
  2277.      be all zero.  For signed fields, the high-order bits should agree with 
  2278.      the sign bit.  */
  2279.  
  2280.   if (lunsignedp)
  2281.     {
  2282.       if (! integer_zerop (const_binop (RSHIFT_EXPR,
  2283.                     convert (unsigned_type, rhs),
  2284.                     size_int (lbitsize), 0)))
  2285.     {
  2286.       warning ("comparison is always %s due to width of bitfield",
  2287.            code == NE_EXPR ? "one" : "zero");
  2288.       return convert (compare_type,
  2289.               (code == NE_EXPR
  2290.                ? integer_one_node : integer_zero_node));
  2291.     }
  2292.     }
  2293.   else
  2294.     {
  2295.       tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs),
  2296.                   size_int (lbitsize - 1), 0);
  2297.       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
  2298.     {
  2299.       warning ("comparison is always %s due to width of bitfield",
  2300.            code == NE_EXPR ? "one" : "zero");
  2301.       return convert (compare_type,
  2302.               (code == NE_EXPR
  2303.                ? integer_one_node : integer_zero_node));
  2304.     }
  2305.     }
  2306.  
  2307.   /* Single-bit compares should always be against zero.  */
  2308.   if (lbitsize == 1 && ! integer_zerop (rhs))
  2309.     {
  2310.       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
  2311.       rhs = convert (type, integer_zero_node);
  2312.     }
  2313.  
  2314.   /* Make a new bitfield reference, shift the constant over the
  2315.      appropriate number of bits and mask it with the computed mask
  2316.      (in case this was a signed field).  If we changed it, make a new one.  */
  2317.   lhs = make_bit_field_ref (linner, unsigned_type, lnbitsize, lnbitpos, 1);
  2318.   if (lvolatilep)
  2319.     {
  2320.       TREE_SIDE_EFFECTS (lhs) = 1;
  2321.       TREE_THIS_VOLATILE (lhs) = 1;
  2322.     }
  2323.  
  2324.   rhs = fold (const_binop (BIT_AND_EXPR,
  2325.                const_binop (LSHIFT_EXPR,
  2326.                     convert (unsigned_type, rhs),
  2327.                     size_int (lbitpos), 0),
  2328.                mask, 0));
  2329.  
  2330.   return build (code, compare_type,
  2331.         build (BIT_AND_EXPR, unsigned_type, lhs, mask),
  2332.         rhs);
  2333. }
  2334.  
  2335. /* Subroutine for fold_truthop: decode a field reference.
  2336.  
  2337.    If EXP is a comparison reference, we return the innermost reference.
  2338.  
  2339.    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
  2340.    set to the starting bit number.
  2341.  
  2342.    If the innermost field can be completely contained in a mode-sized
  2343.    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
  2344.  
  2345.    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
  2346.    otherwise it is not changed.
  2347.  
  2348.    *PUNSIGNEDP is set to the signedness of the field.
  2349.  
  2350.    *PMASK is set to the mask used.  This is either contained in a
  2351.    BIT_AND_EXPR or derived from the width of the field.
  2352.  
  2353.    Return 0 if this is not a component reference or is one that we can't
  2354.    do anything with.  */
  2355.  
  2356. static tree
  2357. decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
  2358.             pvolatilep, pmask)
  2359.      tree exp;
  2360.      int *pbitsize, *pbitpos;
  2361.      enum machine_mode *pmode;
  2362.      int *punsignedp, *pvolatilep;
  2363.      tree *pmask;
  2364. {
  2365.   tree and_mask = 0;
  2366.   tree mask, inner, offset;
  2367.   tree unsigned_type;
  2368.   int precision;
  2369.  
  2370.   /* All the optimizations using this function assume integer fields.  
  2371.      There are problems with FP fields since the type_for_size call
  2372.      below can fail for, e.g., XFmode.  */
  2373.   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
  2374.     return 0;
  2375.  
  2376.   STRIP_NOPS (exp);
  2377.  
  2378.   if (TREE_CODE (exp) == BIT_AND_EXPR)
  2379.     {
  2380.       and_mask = TREE_OPERAND (exp, 1);
  2381.       exp = TREE_OPERAND (exp, 0);
  2382.       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
  2383.       if (TREE_CODE (and_mask) != INTEGER_CST)
  2384.     return 0;
  2385.     }
  2386.  
  2387.   if (TREE_CODE (exp) != COMPONENT_REF && TREE_CODE (exp) != ARRAY_REF
  2388.       && TREE_CODE (exp) != BIT_FIELD_REF)
  2389.     return 0;
  2390.  
  2391.   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
  2392.                    punsignedp, pvolatilep);
  2393.   if (inner == exp || *pbitsize < 0 || offset != 0)
  2394.     return 0;
  2395.   
  2396.   /* Compute the mask to access the bitfield.  */
  2397.   unsigned_type = type_for_size (*pbitsize, 1);
  2398.   precision = TYPE_PRECISION (unsigned_type);
  2399.  
  2400.   mask = build_int_2 (~0, ~0);
  2401.   TREE_TYPE (mask) = unsigned_type;
  2402.   force_fit_type (mask, 0);
  2403.   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
  2404.   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
  2405.  
  2406.   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
  2407.   if (and_mask != 0)
  2408.     mask = fold (build (BIT_AND_EXPR, unsigned_type,
  2409.             convert (unsigned_type, and_mask), mask));
  2410.  
  2411.   *pmask = mask;
  2412.   return inner;
  2413. }
  2414.  
  2415. /* Return non-zero if MASK represents a mask of SIZE ones in the low-order
  2416.    bit positions.  */
  2417.  
  2418. static int
  2419. all_ones_mask_p (mask, size)
  2420.      tree mask;
  2421.      int size;
  2422. {
  2423.   tree type = TREE_TYPE (mask);
  2424.   int precision = TYPE_PRECISION (type);
  2425.   tree tmask;
  2426.  
  2427.   tmask = build_int_2 (~0, ~0);
  2428.   TREE_TYPE (tmask) = signed_type (type);
  2429.   force_fit_type (tmask, 0);
  2430.   return
  2431.     operand_equal_p (mask, 
  2432.              const_binop (RSHIFT_EXPR,
  2433.                   const_binop (LSHIFT_EXPR, tmask,
  2434.                            size_int (precision - size), 0),
  2435.                   size_int (precision - size), 0),
  2436.              0);
  2437. }
  2438.  
  2439. /* Subroutine for fold_truthop: determine if an operand is simple enough
  2440.    to be evaluated unconditionally.  */
  2441.  
  2442. static int 
  2443. simple_operand_p (exp)
  2444.      tree exp;
  2445. {
  2446.   /* Strip any conversions that don't change the machine mode.  */
  2447.   while ((TREE_CODE (exp) == NOP_EXPR
  2448.       || TREE_CODE (exp) == CONVERT_EXPR)
  2449.      && (TYPE_MODE (TREE_TYPE (exp))
  2450.          == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
  2451.     exp = TREE_OPERAND (exp, 0);
  2452.  
  2453.   return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
  2454.       || (TREE_CODE_CLASS (TREE_CODE (exp)) == 'd'
  2455.           && ! TREE_ADDRESSABLE (exp)
  2456.           && ! TREE_THIS_VOLATILE (exp)
  2457.           && ! DECL_NONLOCAL (exp)
  2458.           /* Don't regard global variables as simple.  They may be
  2459.          allocated in ways unknown to the compiler (shared memory,
  2460.          #pragma weak, etc).  */
  2461.           && ! TREE_PUBLIC (exp)
  2462.           && ! DECL_EXTERNAL (exp)
  2463.           /* Loading a static variable is unduly expensive, but global
  2464.          registers aren't expensive.  */
  2465.           && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
  2466. }
  2467.  
  2468. /* Subroutine for fold_truthop: try to optimize a range test.
  2469.  
  2470.    For example, "i >= 2 && i =< 9" can be done as "(unsigned) (i - 2) <= 7".
  2471.  
  2472.    JCODE is the logical combination of the two terms.  It is TRUTH_AND_EXPR
  2473.    (representing TRUTH_ANDIF_EXPR and TRUTH_AND_EXPR) or TRUTH_OR_EXPR
  2474.    (representing TRUTH_ORIF_EXPR and TRUTH_OR_EXPR).  TYPE is the type of
  2475.    the result.
  2476.  
  2477.    VAR is the value being tested.  LO_CODE and HI_CODE are the comparison
  2478.    operators comparing VAR to LO_CST and HI_CST.  LO_CST is known to be no
  2479.    larger than HI_CST (they may be equal).
  2480.  
  2481.    We return the simplified tree or 0 if no optimization is possible.  */
  2482.  
  2483. static tree
  2484. range_test (jcode, type, lo_code, hi_code, var, lo_cst, hi_cst)
  2485.      enum tree_code jcode, lo_code, hi_code;
  2486.      tree type, var, lo_cst, hi_cst;
  2487. {
  2488.   tree utype;
  2489.   enum tree_code rcode;
  2490.  
  2491.   /* See if this is a range test and normalize the constant terms.  */
  2492.  
  2493.   if (jcode == TRUTH_AND_EXPR)
  2494.     {
  2495.       switch (lo_code)
  2496.     {
  2497.     case NE_EXPR:
  2498.       /* See if we have VAR != CST && VAR != CST+1.  */
  2499.       if (! (hi_code == NE_EXPR
  2500.          && TREE_INT_CST_LOW (hi_cst) - TREE_INT_CST_LOW (lo_cst) == 1
  2501.          && tree_int_cst_equal (integer_one_node,
  2502.                     const_binop (MINUS_EXPR,
  2503.                              hi_cst, lo_cst, 0))))
  2504.         return 0;
  2505.  
  2506.       rcode = GT_EXPR;
  2507.       break;
  2508.  
  2509.     case GT_EXPR:
  2510.     case GE_EXPR:
  2511.       if (hi_code == LT_EXPR)
  2512.         hi_cst = const_binop (MINUS_EXPR, hi_cst, integer_one_node, 0);
  2513.       else if (hi_code != LE_EXPR)
  2514.         return 0;
  2515.  
  2516.       if (lo_code == GT_EXPR)
  2517.         lo_cst = const_binop (PLUS_EXPR, lo_cst, integer_one_node, 0);
  2518.  
  2519.       /* We now have VAR >= LO_CST && VAR <= HI_CST.  */
  2520.       rcode = LE_EXPR;
  2521.       break;
  2522.  
  2523.     default:
  2524.       return 0;
  2525.     }
  2526.     }
  2527.   else
  2528.     {
  2529.       switch (lo_code)
  2530.     {
  2531.     case EQ_EXPR:
  2532.       /* See if we have VAR == CST || VAR == CST+1.  */
  2533.       if (! (hi_code == EQ_EXPR
  2534.          && TREE_INT_CST_LOW (hi_cst) - TREE_INT_CST_LOW (lo_cst) == 1
  2535.          && tree_int_cst_equal (integer_one_node,
  2536.                     const_binop (MINUS_EXPR,
  2537.                              hi_cst, lo_cst, 0))))
  2538.         return 0;
  2539.  
  2540.       rcode = LE_EXPR;
  2541.       break;
  2542.  
  2543.     case LE_EXPR:
  2544.     case LT_EXPR:
  2545.       if (hi_code == GE_EXPR)
  2546.         hi_cst = const_binop (MINUS_EXPR, hi_cst, integer_one_node, 0);
  2547.       else if (hi_code != GT_EXPR)
  2548.         return 0;
  2549.  
  2550.       if (lo_code == LE_EXPR)
  2551.         lo_cst = const_binop (PLUS_EXPR, lo_cst, integer_one_node, 0);
  2552.  
  2553.       /* We now have VAR < LO_CST || VAR > HI_CST.  */
  2554.       rcode = GT_EXPR;
  2555.       break;
  2556.  
  2557.     default:
  2558.       return 0;
  2559.     }
  2560.     }
  2561.  
  2562.   /* When normalizing, it is possible to both increment the smaller constant
  2563.      and decrement the larger constant.  See if they are still ordered.  */
  2564.   if (tree_int_cst_lt (hi_cst, lo_cst))
  2565.     return 0;
  2566.  
  2567.   /* Fail if VAR isn't an integer.  */
  2568.   utype = TREE_TYPE (var);
  2569.   if (! INTEGRAL_TYPE_P (utype))
  2570.     return 0;
  2571.  
  2572.   /* The range test is invalid if subtracting the two constants results
  2573.      in overflow.  This can happen in traditional mode.  */
  2574.   if (! int_fits_type_p (hi_cst, TREE_TYPE (var))
  2575.       || ! int_fits_type_p (lo_cst, TREE_TYPE (var)))
  2576.     return 0;
  2577.  
  2578.   if (! TREE_UNSIGNED (utype))
  2579.     {
  2580.       utype = unsigned_type (utype);
  2581.       var = convert (utype, var);
  2582.       lo_cst = convert (utype, lo_cst);
  2583.       hi_cst = convert (utype, hi_cst);
  2584.     }
  2585.  
  2586.   return fold (convert (type,
  2587.             build (rcode, utype,
  2588.                    build (MINUS_EXPR, utype, var, lo_cst),
  2589.                    const_binop (MINUS_EXPR, hi_cst, lo_cst, 0))));
  2590. }
  2591.  
  2592. /* Find ways of folding logical expressions of LHS and RHS:
  2593.    Try to merge two comparisons to the same innermost item.
  2594.    Look for range tests like "ch >= '0' && ch <= '9'".
  2595.    Look for combinations of simple terms on machines with expensive branches
  2596.    and evaluate the RHS unconditionally.
  2597.  
  2598.    For example, if we have p->a == 2 && p->b == 4 and we can make an
  2599.    object large enough to span both A and B, we can do this with a comparison
  2600.    against the object ANDed with the a mask.
  2601.  
  2602.    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
  2603.    operations to do this with one comparison.
  2604.  
  2605.    We check for both normal comparisons and the BIT_AND_EXPRs made this by
  2606.    function and the one above.
  2607.  
  2608.    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
  2609.    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
  2610.  
  2611.    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
  2612.    two operands.
  2613.  
  2614.    We return the simplified tree or 0 if no optimization is possible.  */
  2615.  
  2616. static tree
  2617. fold_truthop (code, truth_type, lhs, rhs)
  2618.      enum tree_code code;
  2619.      tree truth_type, lhs, rhs;
  2620. {
  2621.   /* If this is the "or" of two comparisons, we can do something if we
  2622.      the comparisons are NE_EXPR.  If this is the "and", we can do something
  2623.      if the comparisons are EQ_EXPR.  I.e., 
  2624.          (a->b == 2 && a->c == 4) can become (a->new == NEW).
  2625.  
  2626.      WANTED_CODE is this operation code.  For single bit fields, we can
  2627.      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
  2628.      comparison for one-bit fields.  */
  2629.  
  2630.   enum tree_code wanted_code;
  2631.   enum tree_code lcode, rcode;
  2632.   tree ll_arg, lr_arg, rl_arg, rr_arg;
  2633.   tree ll_inner, lr_inner, rl_inner, rr_inner;
  2634.   int ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
  2635.   int rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
  2636.   int xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
  2637.   int lnbitsize, lnbitpos, rnbitsize, rnbitpos;
  2638.   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
  2639.   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
  2640.   enum machine_mode lnmode, rnmode;
  2641.   tree ll_mask, lr_mask, rl_mask, rr_mask;
  2642.   tree l_const, r_const;
  2643.   tree type, result;
  2644.   int first_bit, end_bit;
  2645.   int volatilep;
  2646.  
  2647.   /* Start by getting the comparison codes and seeing if this looks like
  2648.      a range test.  Fail if anything is volatile.  If one operand is a
  2649.      BIT_AND_EXPR with the constant one, treat it as if it were surrounded
  2650.      with a NE_EXPR.  */
  2651.  
  2652.   if (TREE_SIDE_EFFECTS (lhs)
  2653.       || TREE_SIDE_EFFECTS (rhs))
  2654.     return 0;
  2655.  
  2656.   lcode = TREE_CODE (lhs);
  2657.   rcode = TREE_CODE (rhs);
  2658.  
  2659.   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
  2660.     lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node);
  2661.  
  2662.   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
  2663.     rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node);
  2664.  
  2665.   if (TREE_CODE_CLASS (lcode) != '<'
  2666.       || TREE_CODE_CLASS (rcode) != '<')
  2667.     return 0;
  2668.  
  2669.   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
  2670.       ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
  2671.  
  2672.   ll_arg = TREE_OPERAND (lhs, 0);
  2673.   lr_arg = TREE_OPERAND (lhs, 1);
  2674.   rl_arg = TREE_OPERAND (rhs, 0);
  2675.   rr_arg = TREE_OPERAND (rhs, 1);
  2676.   
  2677.   if (TREE_CODE (lr_arg) == INTEGER_CST
  2678.       && TREE_CODE (rr_arg) == INTEGER_CST
  2679.       && operand_equal_p (ll_arg, rl_arg, 0))
  2680.     {
  2681.       if (tree_int_cst_lt (lr_arg, rr_arg))
  2682.     result = range_test (code, truth_type, lcode, rcode,
  2683.                  ll_arg, lr_arg, rr_arg);
  2684.       else
  2685.     result = range_test (code, truth_type, rcode, lcode,
  2686.                  ll_arg, rr_arg, lr_arg);
  2687.  
  2688.       /* If this isn't a range test, it also isn't a comparison that
  2689.      can be merged.  However, it wins to evaluate the RHS unconditionally
  2690.      on machines with expensive branches.   */
  2691.  
  2692.       if (result == 0 && BRANCH_COST >= 2)
  2693.     {
  2694.       if (TREE_CODE (ll_arg) != VAR_DECL
  2695.           && TREE_CODE (ll_arg) != PARM_DECL)
  2696.         {
  2697.           /* Avoid evaluating the variable part twice.  */
  2698.           ll_arg = save_expr (ll_arg);
  2699.           lhs = build (lcode, TREE_TYPE (lhs), ll_arg, lr_arg);
  2700.           rhs = build (rcode, TREE_TYPE (rhs), ll_arg, rr_arg);
  2701.         }
  2702.       return build (code, truth_type, lhs, rhs);
  2703.     }
  2704.       return result;
  2705.     }
  2706.  
  2707.   /* If the RHS can be evaluated unconditionally and its operands are
  2708.      simple, it wins to evaluate the RHS unconditionally on machines
  2709.      with expensive branches.  In this case, this isn't a comparison
  2710.      that can be merged.  */
  2711.  
  2712.   /* @@ I'm not sure it wins on the m88110 to do this if the comparisons
  2713.      are with zero (tmw).  */
  2714.  
  2715.   if (BRANCH_COST >= 2
  2716.       && INTEGRAL_TYPE_P (TREE_TYPE (rhs))
  2717.       && simple_operand_p (rl_arg)
  2718.       && simple_operand_p (rr_arg))
  2719.     return build (code, truth_type, lhs, rhs);
  2720.  
  2721.   /* See if the comparisons can be merged.  Then get all the parameters for
  2722.      each side.  */
  2723.  
  2724.   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
  2725.       || (rcode != EQ_EXPR && rcode != NE_EXPR))
  2726.     return 0;
  2727.  
  2728.   volatilep = 0;
  2729.   ll_inner = decode_field_reference (ll_arg,
  2730.                      &ll_bitsize, &ll_bitpos, &ll_mode,
  2731.                      &ll_unsignedp, &volatilep, &ll_mask);
  2732.   lr_inner = decode_field_reference (lr_arg,
  2733.                      &lr_bitsize, &lr_bitpos, &lr_mode,
  2734.                      &lr_unsignedp, &volatilep, &lr_mask);
  2735.   rl_inner = decode_field_reference (rl_arg,
  2736.                      &rl_bitsize, &rl_bitpos, &rl_mode,
  2737.                      &rl_unsignedp, &volatilep, &rl_mask);
  2738.   rr_inner = decode_field_reference (rr_arg,
  2739.                      &rr_bitsize, &rr_bitpos, &rr_mode,
  2740.                      &rr_unsignedp, &volatilep, &rr_mask);
  2741.  
  2742.   /* It must be true that the inner operation on the lhs of each
  2743.      comparison must be the same if we are to be able to do anything.
  2744.      Then see if we have constants.  If not, the same must be true for
  2745.      the rhs's.  */
  2746.   if (volatilep || ll_inner == 0 || rl_inner == 0
  2747.       || ! operand_equal_p (ll_inner, rl_inner, 0))
  2748.     return 0;
  2749.  
  2750.   if (TREE_CODE (lr_arg) == INTEGER_CST
  2751.       && TREE_CODE (rr_arg) == INTEGER_CST)
  2752.     l_const = lr_arg, r_const = rr_arg;
  2753.   else if (lr_inner == 0 || rr_inner == 0
  2754.        || ! operand_equal_p (lr_inner, rr_inner, 0))
  2755.     return 0;
  2756.   else
  2757.     l_const = r_const = 0;
  2758.  
  2759.   /* If either comparison code is not correct for our logical operation,
  2760.      fail.  However, we can convert a one-bit comparison against zero into
  2761.      the opposite comparison against that bit being set in the field.  */
  2762.  
  2763.   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
  2764.   if (lcode != wanted_code)
  2765.     {
  2766.       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
  2767.     l_const = ll_mask;
  2768.       else
  2769.     return 0;
  2770.     }
  2771.  
  2772.   if (rcode != wanted_code)
  2773.     {
  2774.       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
  2775.     r_const = rl_mask;
  2776.       else
  2777.     return 0;
  2778.     }
  2779.  
  2780.   /* See if we can find a mode that contains both fields being compared on
  2781.      the left.  If we can't, fail.  Otherwise, update all constants and masks
  2782.      to be relative to a field of that size.  */
  2783.   first_bit = MIN (ll_bitpos, rl_bitpos);
  2784.   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
  2785.   lnmode = get_best_mode (end_bit - first_bit, first_bit,
  2786.               TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
  2787.               volatilep);
  2788.   if (lnmode == VOIDmode)
  2789.     return 0;
  2790.  
  2791.   lnbitsize = GET_MODE_BITSIZE (lnmode);
  2792.   lnbitpos = first_bit & ~ (lnbitsize - 1);
  2793.   type = type_for_size (lnbitsize, 1);
  2794.   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
  2795.  
  2796. #if BYTES_BIG_ENDIAN
  2797.   xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
  2798.   xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
  2799. #endif
  2800.  
  2801.   ll_mask = const_binop (LSHIFT_EXPR, convert (type, ll_mask),
  2802.              size_int (xll_bitpos), 0);
  2803.   rl_mask = const_binop (LSHIFT_EXPR, convert (type, rl_mask),
  2804.              size_int (xrl_bitpos), 0);
  2805.  
  2806.   /* Make sure the constants are interpreted as unsigned, so we
  2807.      don't have sign bits outside the range of their type.  */
  2808.  
  2809.   if (l_const)
  2810.     {
  2811.       l_const = convert (unsigned_type (TREE_TYPE (l_const)), l_const);
  2812.       l_const = const_binop (LSHIFT_EXPR, convert (type, l_const),
  2813.                  size_int (xll_bitpos), 0);
  2814.     }
  2815.   if (r_const)
  2816.     {
  2817.       r_const = convert (unsigned_type (TREE_TYPE (r_const)), r_const);
  2818.       r_const = const_binop (LSHIFT_EXPR, convert (type, r_const),
  2819.                  size_int (xrl_bitpos), 0);
  2820.     }
  2821.  
  2822.   /* If the right sides are not constant, do the same for it.  Also,
  2823.      disallow this optimization if a size or signedness mismatch occurs
  2824.      between the left and right sides.  */
  2825.   if (l_const == 0)
  2826.     {
  2827.       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
  2828.       || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
  2829.       /* Make sure the two fields on the right
  2830.          correspond to the left without being swapped.  */
  2831.       || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
  2832.     return 0;
  2833.  
  2834.       first_bit = MIN (lr_bitpos, rr_bitpos);
  2835.       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
  2836.       rnmode = get_best_mode (end_bit - first_bit, first_bit,
  2837.                   TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
  2838.                   volatilep);
  2839.       if (rnmode == VOIDmode)
  2840.     return 0;
  2841.  
  2842.       rnbitsize = GET_MODE_BITSIZE (rnmode);
  2843.       rnbitpos = first_bit & ~ (rnbitsize - 1);
  2844.       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
  2845.  
  2846. #if BYTES_BIG_ENDIAN
  2847.       xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
  2848.       xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
  2849. #endif
  2850.  
  2851.       lr_mask = const_binop (LSHIFT_EXPR, convert (type, lr_mask),
  2852.                  size_int (xlr_bitpos), 0);
  2853.       rr_mask = const_binop (LSHIFT_EXPR, convert (type, rr_mask),
  2854.                  size_int (xrr_bitpos), 0);
  2855.  
  2856.       /* Make a mask that corresponds to both fields being compared.
  2857.      Do this for both items being compared.  If the masks agree,
  2858.      we can do this by masking both and comparing the masked
  2859.      results.  */
  2860.       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
  2861.       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
  2862.       if (operand_equal_p (ll_mask, lr_mask, 0) && lnbitsize == rnbitsize)
  2863.     {
  2864.       lhs = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos,
  2865.                     ll_unsignedp || rl_unsignedp);
  2866.       rhs = make_bit_field_ref (lr_inner, type, rnbitsize, rnbitpos,
  2867.                     lr_unsignedp || rr_unsignedp);
  2868.       if (! all_ones_mask_p (ll_mask, lnbitsize))
  2869.         {
  2870.           lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
  2871.           rhs = build (BIT_AND_EXPR, type, rhs, ll_mask);
  2872.         }
  2873.       return build (wanted_code, truth_type, lhs, rhs);
  2874.     }
  2875.  
  2876.       /* There is still another way we can do something:  If both pairs of
  2877.      fields being compared are adjacent, we may be able to make a wider
  2878.      field containing them both.  */
  2879.       if ((ll_bitsize + ll_bitpos == rl_bitpos
  2880.        && lr_bitsize + lr_bitpos == rr_bitpos)
  2881.       || (ll_bitpos == rl_bitpos + rl_bitsize
  2882.           && lr_bitpos == rr_bitpos + rr_bitsize))
  2883.     return build (wanted_code, truth_type,
  2884.               make_bit_field_ref (ll_inner, type,
  2885.                       ll_bitsize + rl_bitsize,
  2886.                       MIN (ll_bitpos, rl_bitpos),
  2887.                       ll_unsignedp),
  2888.               make_bit_field_ref (lr_inner, type,
  2889.                       lr_bitsize + rr_bitsize,
  2890.                       MIN (lr_bitpos, rr_bitpos),
  2891.                       lr_unsignedp));
  2892.  
  2893.       return 0;
  2894.     }
  2895.  
  2896.   /* Handle the case of comparisons with constants.  If there is something in
  2897.      common between the masks, those bits of the constants must be the same.
  2898.      If not, the condition is always false.  Test for this to avoid generating
  2899.      incorrect code below.  */
  2900.   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
  2901.   if (! integer_zerop (result)
  2902.       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
  2903.                const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
  2904.     {
  2905.       if (wanted_code == NE_EXPR)
  2906.     {
  2907.       warning ("`or' of unmatched not-equal tests is always 1");
  2908.       return convert (truth_type, integer_one_node);
  2909.     }
  2910.       else
  2911.     {
  2912.       warning ("`and' of mutually exclusive equal-tests is always zero");
  2913.       return convert (truth_type, integer_zero_node);
  2914.     }
  2915.     }
  2916.  
  2917.   /* Construct the expression we will return.  First get the component
  2918.      reference we will make.  Unless the mask is all ones the width of
  2919.      that field, perform the mask operation.  Then compare with the
  2920.      merged constant.  */
  2921.   result = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos,
  2922.                    ll_unsignedp || rl_unsignedp);
  2923.  
  2924.   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
  2925.   if (! all_ones_mask_p (ll_mask, lnbitsize))
  2926.     result = build (BIT_AND_EXPR, type, result, ll_mask);
  2927.  
  2928.   return build (wanted_code, truth_type, result,
  2929.         const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
  2930. }
  2931.  
  2932. /* If T contains a COMPOUND_EXPR which was inserted merely to evaluate
  2933.    S, a SAVE_EXPR, return the expression actually being evaluated.   Note
  2934.    that we may sometimes modify the tree.  */
  2935.  
  2936. static tree
  2937. strip_compound_expr (t, s)
  2938.      tree t;
  2939.      tree s;
  2940. {
  2941.   tree type = TREE_TYPE (t);
  2942.   enum tree_code code = TREE_CODE (t);
  2943.  
  2944.   /* See if this is the COMPOUND_EXPR we want to eliminate.  */
  2945.   if (code == COMPOUND_EXPR && TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR
  2946.       && TREE_OPERAND (TREE_OPERAND (t, 0), 0) == s)
  2947.     return TREE_OPERAND (t, 1);
  2948.  
  2949.   /* See if this is a COND_EXPR or a simple arithmetic operator.   We
  2950.      don't bother handling any other types.  */
  2951.   else if (code == COND_EXPR)
  2952.     {
  2953.       TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
  2954.       TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
  2955.       TREE_OPERAND (t, 2) = strip_compound_expr (TREE_OPERAND (t, 2), s);
  2956.     }
  2957.   else if (TREE_CODE_CLASS (code) == '1')
  2958.     TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
  2959.   else if (TREE_CODE_CLASS (code) == '<'
  2960.        || TREE_CODE_CLASS (code) == '2')
  2961.     {
  2962.       TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
  2963.       TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
  2964.     }
  2965.  
  2966.   return t;
  2967. }
  2968.  
  2969. /* Perform constant folding and related simplification of EXPR.
  2970.    The related simplifications include x*1 => x, x*0 => 0, etc.,
  2971.    and application of the associative law.
  2972.    NOP_EXPR conversions may be removed freely (as long as we
  2973.    are careful not to change the C type of the overall expression)
  2974.    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
  2975.    but we can constant-fold them if they have constant operands.  */
  2976.  
  2977. tree
  2978. fold (expr) 
  2979.      tree expr;
  2980. {
  2981.   register tree t = expr;
  2982.   tree t1 = NULL_TREE;
  2983.   tree tem;
  2984.   tree type = TREE_TYPE (expr);
  2985.   register tree arg0, arg1;
  2986.   register enum tree_code code = TREE_CODE (t);
  2987.   register int kind;
  2988.   int invert;
  2989.  
  2990.   /* WINS will be nonzero when the switch is done
  2991.      if all operands are constant.  */
  2992.  
  2993.   int wins = 1;
  2994.  
  2995.   /* Don't try to process an RTL_EXPR since its operands aren't trees.  */
  2996.   if (code == RTL_EXPR)
  2997.     return t;
  2998.  
  2999.   /* Return right away if already constant.  */
  3000.   if (TREE_CONSTANT (t))
  3001.     {
  3002.       if (code == CONST_DECL)
  3003.     return DECL_INITIAL (t);
  3004.       return t;
  3005.     }
  3006.   
  3007.   kind = TREE_CODE_CLASS (code);
  3008.   if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
  3009.     {
  3010.       tree subop;
  3011.  
  3012.       /* Special case for conversion ops that can have fixed point args.  */
  3013.       arg0 = TREE_OPERAND (t, 0);
  3014.  
  3015.       /* Don't use STRIP_NOPS, because signedness of argument type matters.  */
  3016.       if (arg0 != 0)
  3017.     STRIP_TYPE_NOPS (arg0);
  3018.  
  3019.       if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
  3020.     subop = TREE_REALPART (arg0);
  3021.       else
  3022.     subop = arg0;
  3023.  
  3024.       if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
  3025. #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  3026.       && TREE_CODE (subop) != REAL_CST
  3027. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  3028.       )
  3029.     /* Note that TREE_CONSTANT isn't enough:
  3030.        static var addresses are constant but we can't
  3031.        do arithmetic on them.  */
  3032.     wins = 0;
  3033.     }
  3034.   else if (kind == 'e' || kind == '<'
  3035.        || kind == '1' || kind == '2' || kind == 'r')
  3036.     {
  3037.       register int len = tree_code_length[(int) code];
  3038.       register int i;
  3039.       for (i = 0; i < len; i++)
  3040.     {
  3041.       tree op = TREE_OPERAND (t, i);
  3042.       tree subop;
  3043.  
  3044.       if (op == 0)
  3045.         continue;        /* Valid for CALL_EXPR, at least.  */
  3046.  
  3047.       if (kind == '<' || code == RSHIFT_EXPR)
  3048.         {
  3049.           /* Signedness matters here.  Perhaps we can refine this
  3050.          later.  */
  3051.           STRIP_TYPE_NOPS (op);
  3052.         }
  3053.       else
  3054.         {
  3055.           /* Strip any conversions that don't change the mode.  */
  3056.           STRIP_NOPS (op);
  3057.         }
  3058.       
  3059.       if (TREE_CODE (op) == COMPLEX_CST)
  3060.         subop = TREE_REALPART (op);
  3061.       else
  3062.         subop = op;
  3063.  
  3064.       if (TREE_CODE (subop) != INTEGER_CST
  3065. #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  3066.           && TREE_CODE (subop) != REAL_CST
  3067. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  3068.           )
  3069.         /* Note that TREE_CONSTANT isn't enough:
  3070.            static var addresses are constant but we can't
  3071.            do arithmetic on them.  */
  3072.         wins = 0;
  3073.  
  3074.       if (i == 0)
  3075.         arg0 = op;
  3076.       else if (i == 1)
  3077.         arg1 = op;
  3078.     }
  3079.     }
  3080.  
  3081.   /* If this is a commutative operation, and ARG0 is a constant, move it
  3082.      to ARG1 to reduce the number of tests below.  */
  3083.   if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR
  3084.        || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR
  3085.        || code == BIT_AND_EXPR)
  3086.       && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
  3087.     {
  3088.       tem = arg0; arg0 = arg1; arg1 = tem;
  3089.  
  3090.       tem = TREE_OPERAND (t, 0); TREE_OPERAND (t, 0) = TREE_OPERAND (t, 1);
  3091.       TREE_OPERAND (t, 1) = tem;
  3092.     }
  3093.  
  3094.   /* Now WINS is set as described above,
  3095.      ARG0 is the first operand of EXPR,
  3096.      and ARG1 is the second operand (if it has more than one operand).
  3097.  
  3098.      First check for cases where an arithmetic operation is applied to a
  3099.      compound, conditional, or comparison operation.  Push the arithmetic
  3100.      operation inside the compound or conditional to see if any folding
  3101.      can then be done.  Convert comparison to conditional for this purpose.
  3102.      The also optimizes non-constant cases that used to be done in
  3103.      expand_expr.
  3104.  
  3105.      Before we do that, see if this is a BIT_AND_EXPR or a BIT_OR_EXPR,
  3106.      one of the operands is a comparison and the other is a comparison, a
  3107.      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
  3108.      code below would make the expression more complex.  Change it to a
  3109.      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to 
  3110.      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
  3111.  
  3112.   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
  3113.        || code == EQ_EXPR || code == NE_EXPR)
  3114.       && ((truth_value_p (TREE_CODE (arg0))
  3115.        && (truth_value_p (TREE_CODE (arg1))
  3116.            || (TREE_CODE (arg1) == BIT_AND_EXPR
  3117.            && integer_onep (TREE_OPERAND (arg1, 1)))))
  3118.       || (truth_value_p (TREE_CODE (arg1))
  3119.           && (truth_value_p (TREE_CODE (arg0))
  3120.           || (TREE_CODE (arg0) == BIT_AND_EXPR
  3121.               && integer_onep (TREE_OPERAND (arg0, 1)))))))
  3122.     {
  3123.       t = fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
  3124.                : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
  3125.                : TRUTH_XOR_EXPR,
  3126.                type, arg0, arg1));
  3127.  
  3128.       if (code == EQ_EXPR)
  3129.     t = invert_truthvalue (t);
  3130.  
  3131.       return t;
  3132.     }
  3133.  
  3134.   if (TREE_CODE_CLASS (code) == '1')
  3135.     {
  3136.       if (TREE_CODE (arg0) == COMPOUND_EXPR)
  3137.     return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
  3138.               fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
  3139.       else if (TREE_CODE (arg0) == COND_EXPR)
  3140.     {
  3141.       t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
  3142.                fold (build1 (code, type, TREE_OPERAND (arg0, 1))),
  3143.                fold (build1 (code, type, TREE_OPERAND (arg0, 2)))));
  3144.  
  3145.       /* If this was a conversion, and all we did was to move into
  3146.          inside the COND_EXPR, bring it back out.  Then return so we
  3147.          don't get into an infinite recursion loop taking the conversion
  3148.          out and then back in.  */
  3149.  
  3150.       if ((code == NOP_EXPR || code == CONVERT_EXPR
  3151.            || code == NON_LVALUE_EXPR)
  3152.           && TREE_CODE (t) == COND_EXPR
  3153.           && TREE_CODE (TREE_OPERAND (t, 1)) == code
  3154.           && TREE_CODE (TREE_OPERAND (t, 2)) == code
  3155.           && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))
  3156.           == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0))))
  3157.         t = build1 (code, type,
  3158.             build (COND_EXPR,
  3159.                    TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0)),
  3160.                    TREE_OPERAND (t, 0),
  3161.                    TREE_OPERAND (TREE_OPERAND (t, 1), 0),
  3162.                    TREE_OPERAND (TREE_OPERAND (t, 2), 0)));
  3163.       return t;
  3164.     }
  3165.       else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<') 
  3166.     return fold (build (COND_EXPR, type, arg0,
  3167.                 fold (build1 (code, type, integer_one_node)),
  3168.                 fold (build1 (code, type, integer_zero_node))));
  3169.    }
  3170.   else if (TREE_CODE_CLASS (code) == '2'
  3171.        || TREE_CODE_CLASS (code) == '<')
  3172.     {
  3173.       if (TREE_CODE (arg1) == COMPOUND_EXPR)
  3174.     return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
  3175.               fold (build (code, type,
  3176.                    arg0, TREE_OPERAND (arg1, 1))));
  3177.       else if (TREE_CODE (arg1) == COND_EXPR
  3178.            || TREE_CODE_CLASS (TREE_CODE (arg1)) == '<')
  3179.     {
  3180.       tree test, true_value, false_value;
  3181.  
  3182.       if (TREE_CODE (arg1) == COND_EXPR)
  3183.         {
  3184.           test = TREE_OPERAND (arg1, 0);
  3185.           true_value = TREE_OPERAND (arg1, 1);
  3186.           false_value = TREE_OPERAND (arg1, 2);
  3187.         }
  3188.       else
  3189.         {
  3190.           test = arg1;
  3191.           true_value = integer_one_node;
  3192.           false_value = integer_zero_node;
  3193.         }
  3194.  
  3195.       /* If ARG0 is complex we want to make sure we only evaluate
  3196.          it once.  Though this is only required if it is volatile, it
  3197.          might be more efficient even if it is not.  However, if we
  3198.          succeed in folding one part to a constant, we do not need
  3199.          to make this SAVE_EXPR.  Since we do this optimization
  3200.          primarily to see if we do end up with constant and this
  3201.          SAVE_EXPR interfers with later optimizations, suppressing
  3202.          it when we can is important.  */
  3203.  
  3204.       if (TREE_CODE (arg0) != SAVE_EXPR
  3205.           && ((TREE_CODE (arg0) != VAR_DECL
  3206.            && TREE_CODE (arg0) != PARM_DECL)
  3207.           || TREE_SIDE_EFFECTS (arg0)))
  3208.         {
  3209.           tree lhs = fold (build (code, type, arg0, true_value));
  3210.           tree rhs = fold (build (code, type, arg0, false_value));
  3211.  
  3212.           if (TREE_CONSTANT (lhs) || TREE_CONSTANT (rhs))
  3213.         return fold (build (COND_EXPR, type, test, lhs, rhs));
  3214.  
  3215.           arg0 = save_expr (arg0);
  3216.         }
  3217.  
  3218.       test = fold (build (COND_EXPR, type, test,
  3219.                   fold (build (code, type, arg0, true_value)),
  3220.                   fold (build (code, type, arg0, false_value))));
  3221.       if (TREE_CODE (arg0) == SAVE_EXPR)
  3222.         return build (COMPOUND_EXPR, type,
  3223.               convert (void_type_node, arg0),
  3224.               strip_compound_expr (test, arg0));
  3225.       else
  3226.         return convert (type, test);
  3227.     }
  3228.  
  3229.       else if (TREE_CODE (arg0) == COMPOUND_EXPR)
  3230.     return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
  3231.               fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
  3232.       else if (TREE_CODE (arg0) == COND_EXPR
  3233.            || TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
  3234.     {
  3235.       tree test, true_value, false_value;
  3236.  
  3237.       if (TREE_CODE (arg0) == COND_EXPR)
  3238.         {
  3239.           test = TREE_OPERAND (arg0, 0);
  3240.           true_value = TREE_OPERAND (arg0, 1);
  3241.           false_value = TREE_OPERAND (arg0, 2);
  3242.         }
  3243.       else
  3244.         {
  3245.           test = arg0;
  3246.           true_value = integer_one_node;
  3247.           false_value = integer_zero_node;
  3248.         }
  3249.  
  3250.       if (TREE_CODE (arg1) != SAVE_EXPR
  3251.           && ((TREE_CODE (arg1) != VAR_DECL
  3252.            && TREE_CODE (arg1) != PARM_DECL)
  3253.           || TREE_SIDE_EFFECTS (arg1)))
  3254.         {
  3255.           tree lhs = fold (build (code, type, true_value, arg1));
  3256.           tree rhs = fold (build (code, type, false_value, arg1));
  3257.  
  3258.           if (TREE_CONSTANT (lhs) || TREE_CONSTANT (rhs)
  3259.           || TREE_CONSTANT (arg1))
  3260.         return fold (build (COND_EXPR, type, test, lhs, rhs));
  3261.  
  3262.           arg1 = save_expr (arg1);
  3263.         }
  3264.  
  3265.       test = fold (build (COND_EXPR, type, test,
  3266.                   fold (build (code, type, true_value, arg1)),
  3267.                   fold (build (code, type, false_value, arg1))));
  3268.       if (TREE_CODE (arg1) == SAVE_EXPR)
  3269.         return build (COMPOUND_EXPR, type,
  3270.               convert (void_type_node, arg1),
  3271.               strip_compound_expr (test, arg1));
  3272.       else
  3273.         return convert (type, test);
  3274.     }
  3275.     }
  3276.   else if (TREE_CODE_CLASS (code) == '<'
  3277.        && TREE_CODE (arg0) == COMPOUND_EXPR)
  3278.     return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
  3279.           fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
  3280.   else if (TREE_CODE_CLASS (code) == '<'
  3281.        && TREE_CODE (arg1) == COMPOUND_EXPR)
  3282.     return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
  3283.           fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
  3284.       
  3285.   switch (code)
  3286.     {
  3287.     case INTEGER_CST:
  3288.     case REAL_CST:
  3289.     case STRING_CST:
  3290.     case COMPLEX_CST:
  3291.     case CONSTRUCTOR:
  3292.       return t;
  3293.  
  3294.     case CONST_DECL:
  3295.       return fold (DECL_INITIAL (t));
  3296.  
  3297.     case NOP_EXPR:
  3298.     case FLOAT_EXPR:
  3299.     case CONVERT_EXPR:
  3300.     case FIX_TRUNC_EXPR:
  3301.       /* Other kinds of FIX are not handled properly by fold_convert.  */
  3302.  
  3303.       /* In addition to the cases of two conversions in a row 
  3304.      handled below, if we are converting something to its own
  3305.      type via an object of identical or wider precision, neither
  3306.      conversion is needed.  */
  3307.       if ((TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
  3308.        || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
  3309.       && TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == TREE_TYPE (t)
  3310.       && ((INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0)))
  3311.            && INTEGRAL_TYPE_P (TREE_TYPE (t)))
  3312.           || (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0)))
  3313.           && FLOAT_TYPE_P (TREE_TYPE (t))))
  3314.       && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
  3315.           >= TYPE_PRECISION (TREE_TYPE (t))))
  3316.     return TREE_OPERAND (TREE_OPERAND (t, 0), 0);
  3317.  
  3318.       /* Two conversions in a row are not needed unless:
  3319.      - the intermediate type is narrower than both initial and final, or
  3320.      - the intermediate type and innermost type differ in signedness,
  3321.        and the outermost type is wider than the intermediate, or
  3322.      - the initial type is a pointer type and the precisions of the
  3323.        intermediate and final types differ, or
  3324.      - the final type is a pointer type and the precisions of the 
  3325.       initial and intermediate types differ.  */
  3326.       if ((TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
  3327.        || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
  3328.       && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
  3329.           > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
  3330.           ||
  3331.           TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
  3332.           > TYPE_PRECISION (TREE_TYPE (t)))
  3333.       && ! ((TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
  3334.          == INTEGER_TYPE)
  3335.         && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
  3336.             == INTEGER_TYPE)
  3337.         && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t, 0)))
  3338.             != TREE_UNSIGNED (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
  3339.         && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
  3340.             < TYPE_PRECISION (TREE_TYPE (t))))
  3341.       && ((TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t, 0)))
  3342.            && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
  3343.            > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))))
  3344.           ==
  3345.           (TREE_UNSIGNED (TREE_TYPE (t))
  3346.            && (TYPE_PRECISION (TREE_TYPE (t))
  3347.            > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))))))
  3348.       && ! ((TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
  3349.          == POINTER_TYPE)
  3350.         && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
  3351.             != TYPE_PRECISION (TREE_TYPE (t))))
  3352.       && ! (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
  3353.         && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
  3354.             != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))))))
  3355.     return convert (TREE_TYPE (t), TREE_OPERAND (TREE_OPERAND (t, 0), 0));
  3356.  
  3357.       if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
  3358.       && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
  3359.       /* Detect assigning a bitfield.  */
  3360.       && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
  3361.            && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
  3362.     {
  3363.       /* Don't leave an assignment inside a conversion
  3364.          unless assigning a bitfield.  */
  3365.       tree prev = TREE_OPERAND (t, 0);
  3366.       TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1);
  3367.       /* First do the assignment, then return converted constant.  */
  3368.       t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t));
  3369.       TREE_USED (t) = 1;
  3370.       return t;
  3371.     }
  3372.       if (!wins)
  3373.     {
  3374.       TREE_CONSTANT (t) = TREE_CONSTANT (arg0);
  3375.       return t;
  3376.     }
  3377.       return fold_convert (t, arg0);
  3378.  
  3379. #if 0  /* This loses on &"foo"[0].  */
  3380.     case ARRAY_REF:
  3381.     {
  3382.       int i;
  3383.  
  3384.       /* Fold an expression like: "foo"[2] */
  3385.       if (TREE_CODE (arg0) == STRING_CST
  3386.           && TREE_CODE (arg1) == INTEGER_CST
  3387.           && !TREE_INT_CST_HIGH (arg1)
  3388.           && (i = TREE_INT_CST_LOW (arg1)) < TREE_STRING_LENGTH (arg0))
  3389.         {
  3390.           t = build_int_2 (TREE_STRING_POINTER (arg0)[i], 0);
  3391.           TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (arg0));
  3392.           force_fit_type (t, 0);
  3393.         }
  3394.     }
  3395.       return t;
  3396. #endif /* 0 */
  3397.  
  3398.     case COMPONENT_REF:
  3399.       if (TREE_CODE (arg0) == CONSTRUCTOR)
  3400.     {
  3401.       tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
  3402.       if (m)
  3403.         t = TREE_VALUE (m);
  3404.     }
  3405.       return t;
  3406.  
  3407.     case RANGE_EXPR:
  3408.       TREE_CONSTANT (t) = wins;
  3409.       return t;
  3410.  
  3411.     case NEGATE_EXPR:
  3412.       if (wins)
  3413.     {
  3414.       if (TREE_CODE (arg0) == INTEGER_CST)
  3415.         {
  3416.           HOST_WIDE_INT low, high;
  3417.           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
  3418.                      TREE_INT_CST_HIGH (arg0),
  3419.                      &low, &high);
  3420.           t = build_int_2 (low, high);
  3421.           TREE_TYPE (t) = type;
  3422.           TREE_OVERFLOW (t)
  3423.         = (TREE_OVERFLOW (arg0)
  3424.            | force_fit_type (t, overflow));
  3425.           TREE_CONSTANT_OVERFLOW (t)
  3426.         = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
  3427.         }
  3428.       else if (TREE_CODE (arg0) == REAL_CST)
  3429.         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
  3430.       TREE_TYPE (t) = type;
  3431.     }
  3432.       else if (TREE_CODE (arg0) == NEGATE_EXPR)
  3433.     return TREE_OPERAND (arg0, 0);
  3434.  
  3435.       /* Convert - (a - b) to (b - a) for non-floating-point.  */
  3436.       else if (TREE_CODE (arg0) == MINUS_EXPR && ! FLOAT_TYPE_P (type))
  3437.     return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1),
  3438.               TREE_OPERAND (arg0, 0));
  3439.  
  3440.       return t;
  3441.  
  3442.     case ABS_EXPR:
  3443.       if (wins)
  3444.     {
  3445.       if (TREE_CODE (arg0) == INTEGER_CST)
  3446.         {
  3447.           if (! TREE_UNSIGNED (type)
  3448.           && TREE_INT_CST_HIGH (arg0) < 0)
  3449.         {
  3450.           HOST_WIDE_INT low, high;
  3451.           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
  3452.                          TREE_INT_CST_HIGH (arg0),
  3453.                          &low, &high);
  3454.           t = build_int_2 (low, high);
  3455.           TREE_TYPE (t) = type;
  3456.           TREE_OVERFLOW (t)
  3457.             = (TREE_OVERFLOW (arg0)
  3458.                | force_fit_type (t, overflow));
  3459.           TREE_CONSTANT_OVERFLOW (t)
  3460.             = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
  3461.         }
  3462.         }
  3463.       else if (TREE_CODE (arg0) == REAL_CST)
  3464.         {
  3465.           if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
  3466.         t = build_real (type,
  3467.                 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
  3468.         }
  3469.       TREE_TYPE (t) = type;
  3470.     }
  3471.       else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR)
  3472.     return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
  3473.       return t;
  3474.  
  3475.     case CONJ_EXPR:
  3476.       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
  3477.     return arg0;
  3478.       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
  3479.     return build (COMPLEX_EXPR, TREE_TYPE (arg0),
  3480.               TREE_OPERAND (arg0, 0),
  3481.               fold (build1 (NEGATE_EXPR,
  3482.                     TREE_TYPE (TREE_TYPE (arg0)),
  3483.                     TREE_OPERAND (arg0, 1))));
  3484.       else if (TREE_CODE (arg0) == COMPLEX_CST)
  3485.     return build_complex (TREE_OPERAND (arg0, 0),
  3486.                   fold (build1 (NEGATE_EXPR,
  3487.                         TREE_TYPE (TREE_TYPE (arg0)),
  3488.                         TREE_OPERAND (arg0, 1))));
  3489.       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
  3490.     return fold (build (TREE_CODE (arg0), type,
  3491.                 fold (build1 (CONJ_EXPR, type,
  3492.                       TREE_OPERAND (arg0, 0))),
  3493.                 fold (build1 (CONJ_EXPR,
  3494.                       type, TREE_OPERAND (arg0, 1)))));
  3495.       else if (TREE_CODE (arg0) == CONJ_EXPR)
  3496.     return TREE_OPERAND (arg0, 0);
  3497.       return t;
  3498.  
  3499.     case BIT_NOT_EXPR:
  3500.       if (wins)
  3501.     {
  3502.       if (TREE_CODE (arg0) == INTEGER_CST)
  3503.         t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
  3504.                  ~ TREE_INT_CST_HIGH (arg0));
  3505.       TREE_TYPE (t) = type;
  3506.       force_fit_type (t, 0);
  3507.       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
  3508.       TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
  3509.     }
  3510.       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
  3511.     return TREE_OPERAND (arg0, 0);
  3512.       return t;
  3513.  
  3514.     case PLUS_EXPR:
  3515.       /* A + (-B) -> A - B */
  3516.       if (TREE_CODE (arg1) == NEGATE_EXPR)
  3517.     return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
  3518.       else if (! FLOAT_TYPE_P (type))
  3519.     {
  3520.       if (integer_zerop (arg1))
  3521.         return non_lvalue (convert (type, arg0));
  3522.  
  3523.       /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
  3524.          with a constant, and the two constants have no bits in common,
  3525.          we should treat this as a BIT_IOR_EXPR since this may produce more
  3526.          simplifications.  */
  3527.       if (TREE_CODE (arg0) == BIT_AND_EXPR
  3528.           && TREE_CODE (arg1) == BIT_AND_EXPR
  3529.           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
  3530.           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
  3531.           && integer_zerop (const_binop (BIT_AND_EXPR,
  3532.                          TREE_OPERAND (arg0, 1),
  3533.                          TREE_OPERAND (arg1, 1), 0)))
  3534.         {
  3535.           code = BIT_IOR_EXPR;
  3536.           goto bit_ior;
  3537.         }
  3538.  
  3539.       /* (A * C) + (B * C) -> (A+B) * C.  Since we are most concerned
  3540.          about the case where C is a constant, just try one of the
  3541.          four possibilities.  */
  3542.  
  3543.       if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
  3544.           && operand_equal_p (TREE_OPERAND (arg0, 1),
  3545.                   TREE_OPERAND (arg1, 1), 0))
  3546.         return fold (build (MULT_EXPR, type,
  3547.                 fold (build (PLUS_EXPR, type,
  3548.                          TREE_OPERAND (arg0, 0),
  3549.                          TREE_OPERAND (arg1, 0))),
  3550.                 TREE_OPERAND (arg0, 1)));
  3551.     }
  3552.       /* In IEEE floating point, x+0 may not equal x.  */
  3553.       else if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  3554.         || flag_fast_math)
  3555.            && real_zerop (arg1))
  3556.     return non_lvalue (convert (type, arg0));
  3557.     associate:
  3558.       /* In most languages, can't associate operations on floats
  3559.      through parentheses.  Rather than remember where the parentheses
  3560.      were, we don't associate floats at all.  It shouldn't matter much.
  3561.      However, associating multiplications is only very slightly
  3562.      inaccurate, so do that if -ffast-math is specified.  */
  3563.       if (FLOAT_TYPE_P (type)
  3564.       && ! (flag_fast_math && code == MULT_EXPR))
  3565.     goto binary;
  3566.  
  3567.       /* The varsign == -1 cases happen only for addition and subtraction.
  3568.      It says that the arg that was split was really CON minus VAR.
  3569.      The rest of the code applies to all associative operations.  */
  3570.       if (!wins)
  3571.     {
  3572.       tree var, con;
  3573.       int varsign;
  3574.  
  3575.       if (split_tree (arg0, code, &var, &con, &varsign))
  3576.         {
  3577.           if (varsign == -1)
  3578.         {
  3579.           /* EXPR is (CON-VAR) +- ARG1.  */
  3580.           /* If it is + and VAR==ARG1, return just CONST.  */
  3581.           if (code == PLUS_EXPR && operand_equal_p (var, arg1, 0))
  3582.             return convert (TREE_TYPE (t), con);
  3583.             
  3584.           /* If ARG0 is a constant, don't change things around;
  3585.              instead keep all the constant computations together.  */
  3586.  
  3587.           if (TREE_CONSTANT (arg0))
  3588.             return t;
  3589.  
  3590.           /* Otherwise return (CON +- ARG1) - VAR.  */
  3591.           t = build (MINUS_EXPR, type,
  3592.                  fold (build (code, type, con, arg1)), var);
  3593.         }
  3594.           else
  3595.         {
  3596.           /* EXPR is (VAR+CON) +- ARG1.  */
  3597.           /* If it is - and VAR==ARG1, return just CONST.  */
  3598.           if (code == MINUS_EXPR && operand_equal_p (var, arg1, 0))
  3599.             return convert (TREE_TYPE (t), con);
  3600.             
  3601.           /* If ARG0 is a constant, don't change things around;
  3602.              instead keep all the constant computations together.  */
  3603.  
  3604.           if (TREE_CONSTANT (arg0))
  3605.             return t;
  3606.  
  3607.           /* Otherwise return VAR +- (ARG1 +- CON).  */
  3608.           tem = fold (build (code, type, arg1, con));
  3609.           t = build (code, type, var, tem);
  3610.  
  3611.           if (integer_zerop (tem)
  3612.               && (code == PLUS_EXPR || code == MINUS_EXPR))
  3613.             return convert (type, var);
  3614.           /* If we have x +/- (c - d) [c an explicit integer]
  3615.              change it to x -/+ (d - c) since if d is relocatable
  3616.              then the latter can be a single immediate insn
  3617.              and the former cannot.  */
  3618.           if (TREE_CODE (tem) == MINUS_EXPR
  3619.               && TREE_CODE (TREE_OPERAND (tem, 0)) == INTEGER_CST)
  3620.             {
  3621.               tree tem1 = TREE_OPERAND (tem, 1);
  3622.               TREE_OPERAND (tem, 1) = TREE_OPERAND (tem, 0);
  3623.               TREE_OPERAND (tem, 0) = tem1;
  3624.               TREE_SET_CODE (t,
  3625.                      (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
  3626.             }
  3627.         }
  3628.           return t;
  3629.         }
  3630.  
  3631.       if (split_tree (arg1, code, &var, &con, &varsign))
  3632.         {
  3633.           if (TREE_CONSTANT (arg1))
  3634.         return t;
  3635.  
  3636.           if (varsign == -1)
  3637.         TREE_SET_CODE (t,
  3638.                    (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
  3639.  
  3640.           /* EXPR is ARG0 +- (CON +- VAR).  */
  3641.           if (TREE_CODE (t) == MINUS_EXPR
  3642.           && operand_equal_p (var, arg0, 0))
  3643.         {
  3644.           /* If VAR and ARG0 cancel, return just CON or -CON.  */
  3645.           if (code == PLUS_EXPR)
  3646.             return convert (TREE_TYPE (t), con);
  3647.           return fold (build1 (NEGATE_EXPR, TREE_TYPE (t),
  3648.                        convert (TREE_TYPE (t), con)));
  3649.         }
  3650.  
  3651.           t = build (TREE_CODE (t), type,
  3652.              fold (build (code, TREE_TYPE (t), arg0, con)), var);
  3653.  
  3654.           if (integer_zerop (TREE_OPERAND (t, 0))
  3655.           && TREE_CODE (t) == PLUS_EXPR)
  3656.         return convert (TREE_TYPE (t), var);
  3657.           return t;
  3658.         }
  3659.     }
  3660.     binary:
  3661. #if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC)
  3662.       if (TREE_CODE (arg1) == REAL_CST)
  3663.     return t;
  3664. #endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */
  3665.       if (wins)
  3666.     t1 = const_binop (code, arg0, arg1, 0);
  3667.       if (t1 != NULL_TREE)
  3668.     {
  3669.       /* The return value should always have
  3670.          the same type as the original expression.  */
  3671.       TREE_TYPE (t1) = TREE_TYPE (t);
  3672.       return t1;
  3673.     }
  3674.       return t;
  3675.  
  3676.     case MINUS_EXPR:
  3677.       if (! FLOAT_TYPE_P (type))
  3678.     {
  3679.       if (! wins && integer_zerop (arg0))
  3680.         return build1 (NEGATE_EXPR, type, arg1);
  3681.       if (integer_zerop (arg1))
  3682.         return non_lvalue (convert (type, arg0));
  3683.  
  3684.       /* (A * C) - (B * C) -> (A-B) * C.  Since we are most concerned
  3685.          about the case where C is a constant, just try one of the
  3686.          four possibilities.  */
  3687.  
  3688.       if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
  3689.           && operand_equal_p (TREE_OPERAND (arg0, 1),
  3690.                   TREE_OPERAND (arg1, 1), 0))
  3691.         return fold (build (MULT_EXPR, type,
  3692.                 fold (build (MINUS_EXPR, type,
  3693.                          TREE_OPERAND (arg0, 0),
  3694.                          TREE_OPERAND (arg1, 0))),
  3695.                 TREE_OPERAND (arg0, 1)));
  3696.     }
  3697.       /* Convert A - (-B) to A + B.  */
  3698.       else if (TREE_CODE (arg1) == NEGATE_EXPR)
  3699.     return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
  3700.  
  3701.       else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  3702.            || flag_fast_math)
  3703.     {
  3704.       /* Except with IEEE floating point, 0-x equals -x.  */
  3705.       if (! wins && real_zerop (arg0))
  3706.         return build1 (NEGATE_EXPR, type, arg1);
  3707.       /* Except with IEEE floating point, x-0 equals x.  */
  3708.       if (real_zerop (arg1))
  3709.         return non_lvalue (convert (type, arg0));
  3710.     }
  3711.  
  3712.       /* Fold &x - &x.  This can happen from &x.foo - &x. 
  3713.      This is unsafe for certain floats even in non-IEEE formats.
  3714.      In IEEE, it is unsafe because it does wrong for NaNs.
  3715.      Also note that operand_equal_p is always false if an operand
  3716.      is volatile.  */
  3717.  
  3718.       if ((! FLOAT_TYPE_P (type) || flag_fast_math)
  3719.       && operand_equal_p (arg0, arg1, 0))
  3720.     return convert (type, integer_zero_node);
  3721.  
  3722.       goto associate;
  3723.  
  3724.     case MULT_EXPR:
  3725.       if (! FLOAT_TYPE_P (type))
  3726.     {
  3727.       if (integer_zerop (arg1))
  3728.         return omit_one_operand (type, arg1, arg0);
  3729.       if (integer_onep (arg1))
  3730.         return non_lvalue (convert (type, arg0));
  3731.  
  3732.       /* ((A / C) * C) is A if the division is an
  3733.          EXACT_DIV_EXPR.   Since C is normally a constant,
  3734.          just check for one of the four possibilities.  */
  3735.  
  3736.       if (TREE_CODE (arg0) == EXACT_DIV_EXPR
  3737.           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
  3738.         return TREE_OPERAND (arg0, 0);
  3739.  
  3740.       /* (a * (1 << b)) is (a << b)  */
  3741.       if (TREE_CODE (arg1) == LSHIFT_EXPR
  3742.           && integer_onep (TREE_OPERAND (arg1, 0)))
  3743.         return fold (build (LSHIFT_EXPR, type, arg0,
  3744.                 TREE_OPERAND (arg1, 1)));
  3745.       if (TREE_CODE (arg0) == LSHIFT_EXPR
  3746.           && integer_onep (TREE_OPERAND (arg0, 0)))
  3747.         return fold (build (LSHIFT_EXPR, type, arg1,
  3748.                 TREE_OPERAND (arg0, 1)));
  3749.     }
  3750.       else
  3751.     {
  3752.       /* x*0 is 0, except for IEEE floating point.  */
  3753.       if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  3754.            || flag_fast_math)
  3755.           && real_zerop (arg1))
  3756.         return omit_one_operand (type, arg1, arg0);
  3757.       /* In IEEE floating point, x*1 is not equivalent to x for snans.
  3758.          However, ANSI says we can drop signals,
  3759.          so we can do this anyway.  */
  3760.       if (real_onep (arg1))
  3761.         return non_lvalue (convert (type, arg0));
  3762.       /* x*2 is x+x */
  3763.       if (! wins && real_twop (arg1))
  3764.         {
  3765.           tree arg = save_expr (arg0);
  3766.           return build (PLUS_EXPR, type, arg, arg);
  3767.         }
  3768.     }
  3769.       goto associate;
  3770.  
  3771.     case BIT_IOR_EXPR:
  3772.     bit_ior:
  3773.       if (integer_all_onesp (arg1))
  3774.     return omit_one_operand (type, arg1, arg0);
  3775.       if (integer_zerop (arg1))
  3776.     return non_lvalue (convert (type, arg0));
  3777.       t1 = distribute_bit_expr (code, type, arg0, arg1);
  3778.       if (t1 != NULL_TREE)
  3779.     return t1;
  3780.  
  3781.       /* (a << C1) | (a >> C2) if A is unsigned and C1+C2 is the size of A
  3782.      is a rotate of A by C1 bits.  */
  3783.  
  3784.       if ((TREE_CODE (arg0) == RSHIFT_EXPR
  3785.        || TREE_CODE (arg0) == LSHIFT_EXPR)
  3786.       && (TREE_CODE (arg1) == RSHIFT_EXPR
  3787.           || TREE_CODE (arg1) == LSHIFT_EXPR)
  3788.       && TREE_CODE (arg0) != TREE_CODE (arg1)
  3789.       && operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1,0), 0)
  3790.       && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0)))
  3791.       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
  3792.       && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
  3793.       && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
  3794.       && TREE_INT_CST_HIGH (TREE_OPERAND (arg1, 1)) == 0
  3795.       && ((TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
  3796.            + TREE_INT_CST_LOW (TREE_OPERAND (arg1, 1)))
  3797.           == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
  3798.     return build (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
  3799.               TREE_CODE (arg0) == LSHIFT_EXPR
  3800.               ? TREE_OPERAND (arg0, 1) : TREE_OPERAND (arg1, 1));
  3801.  
  3802.       goto associate;
  3803.  
  3804.     case BIT_XOR_EXPR:
  3805.       if (integer_zerop (arg1))
  3806.     return non_lvalue (convert (type, arg0));
  3807.       if (integer_all_onesp (arg1))
  3808.     return fold (build1 (BIT_NOT_EXPR, type, arg0));
  3809.       goto associate;
  3810.  
  3811.     case BIT_AND_EXPR:
  3812.     bit_and:
  3813.       if (integer_all_onesp (arg1))
  3814.     return non_lvalue (convert (type, arg0));
  3815.       if (integer_zerop (arg1))
  3816.     return omit_one_operand (type, arg1, arg0);
  3817.       t1 = distribute_bit_expr (code, type, arg0, arg1);
  3818.       if (t1 != NULL_TREE)
  3819.     return t1;
  3820.       /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char.  */
  3821.       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR
  3822.       && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))))
  3823.     {
  3824.       int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)));
  3825.       if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
  3826.           && (~TREE_INT_CST_LOW (arg0)
  3827.           & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
  3828.         return build1 (NOP_EXPR, type, TREE_OPERAND (arg1, 0));
  3829.     }
  3830.       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
  3831.       && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
  3832.     {
  3833.       int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
  3834.       if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
  3835.           && (~TREE_INT_CST_LOW (arg1)
  3836.           & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
  3837.         return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0));
  3838.     }
  3839.       goto associate;
  3840.  
  3841.     case BIT_ANDTC_EXPR:
  3842.       if (integer_all_onesp (arg0))
  3843.     return non_lvalue (convert (type, arg1));
  3844.       if (integer_zerop (arg0))
  3845.     return omit_one_operand (type, arg0, arg1);
  3846.       if (TREE_CODE (arg1) == INTEGER_CST)
  3847.     {
  3848.       arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1));
  3849.       code = BIT_AND_EXPR;
  3850.       goto bit_and;
  3851.     }
  3852.       goto binary;
  3853.  
  3854.     case RDIV_EXPR:
  3855.       /* In most cases, do nothing with a divide by zero.  */
  3856. #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  3857. #ifndef REAL_INFINITY
  3858.       if (TREE_CODE (arg1) == REAL_CST && real_zerop (arg1))
  3859.     return t;
  3860. #endif
  3861. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  3862.  
  3863.       /* In IEEE floating point, x/1 is not equivalent to x for snans.
  3864.      However, ANSI says we can drop signals, so we can do this anyway.  */
  3865.       if (real_onep (arg1))
  3866.     return non_lvalue (convert (type, arg0));
  3867.  
  3868.       /* If ARG1 is a constant, we can convert this to a multiply by the
  3869.      reciprocal.  This does not have the same rounding properties,
  3870.      so only do this if -ffast-math.  We can actually always safely
  3871.      do it if ARG1 is a power of two, but it's hard to tell if it is
  3872.      or not in a portable manner.  */
  3873.       if (TREE_CODE (arg1) == REAL_CST && flag_fast_math
  3874.       && 0 != (tem = const_binop (code, build_real (type, dconst1),
  3875.                       arg1, 0)))
  3876.     return fold (build (MULT_EXPR, type, arg0, tem));
  3877.  
  3878.       goto binary;
  3879.  
  3880.     case TRUNC_DIV_EXPR:
  3881.     case ROUND_DIV_EXPR:
  3882.     case FLOOR_DIV_EXPR:
  3883.     case CEIL_DIV_EXPR:
  3884.     case EXACT_DIV_EXPR:
  3885.       if (integer_onep (arg1))
  3886.     return non_lvalue (convert (type, arg0));
  3887.       if (integer_zerop (arg1))
  3888.     return t;
  3889.  
  3890.       /* If we have ((a / C1) / C2) where both division are the same type, try
  3891.      to simplify.  First see if C1 * C2 overflows or not.  */
  3892.       if (TREE_CODE (arg0) == code && TREE_CODE (arg1) == INTEGER_CST
  3893.       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
  3894.     {
  3895.       tree new_divisor;
  3896.  
  3897.       new_divisor = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 1), arg1, 0);
  3898.       tem = const_binop (FLOOR_DIV_EXPR, new_divisor, arg1, 0);
  3899.  
  3900.       if (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) == TREE_INT_CST_LOW (tem)
  3901.           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == TREE_INT_CST_HIGH (tem))
  3902.         {
  3903.           /* If no overflow, divide by C1*C2.  */
  3904.           return fold (build (code, type, TREE_OPERAND (arg0, 0), new_divisor));
  3905.         }
  3906.     }
  3907.  
  3908.       /* Look for ((a * C1) / C3) or (((a * C1) + C2) / C3),
  3909.      where C1 % C3 == 0 or C3 % C1 == 0.  We can simplify these
  3910.      expressions, which often appear in the offsets or sizes of
  3911.      objects with a varying size.  Only deal with positive divisors
  3912.      and multiplicands.   If C2 is negative, we must have C2 % C3 == 0.
  3913.  
  3914.      Look for NOPs and SAVE_EXPRs inside.  */
  3915.  
  3916.       if (TREE_CODE (arg1) == INTEGER_CST
  3917.       && tree_int_cst_sgn (arg1) >= 0)
  3918.     {
  3919.       int have_save_expr = 0;
  3920.       tree c2 = integer_zero_node;
  3921.       tree xarg0 = arg0;
  3922.  
  3923.       if (TREE_CODE (xarg0) == SAVE_EXPR)
  3924.         have_save_expr = 1, xarg0 = TREE_OPERAND (xarg0, 0);
  3925.  
  3926.       STRIP_NOPS (xarg0);
  3927.  
  3928.       if (TREE_CODE (xarg0) == PLUS_EXPR
  3929.           && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST)
  3930.         c2 = TREE_OPERAND (xarg0, 1), xarg0 = TREE_OPERAND (xarg0, 0);
  3931.       else if (TREE_CODE (xarg0) == MINUS_EXPR
  3932.            && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
  3933.            /* If we are doing this computation unsigned, the negate
  3934.               is incorrect.  */
  3935.            && ! TREE_UNSIGNED (type))
  3936.         {
  3937.           c2 = fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (xarg0, 1)));
  3938.           xarg0 = TREE_OPERAND (xarg0, 0);
  3939.         }
  3940.  
  3941.       if (TREE_CODE (xarg0) == SAVE_EXPR)
  3942.         have_save_expr = 1, xarg0 = TREE_OPERAND (xarg0, 0);
  3943.  
  3944.       STRIP_NOPS (xarg0);
  3945.  
  3946.       if (TREE_CODE (xarg0) == MULT_EXPR
  3947.           && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
  3948.           && tree_int_cst_sgn (TREE_OPERAND (xarg0, 1)) >= 0
  3949.           && (integer_zerop (const_binop (TRUNC_MOD_EXPR,
  3950.                           TREE_OPERAND (xarg0, 1), arg1, 1))
  3951.           || integer_zerop (const_binop (TRUNC_MOD_EXPR, arg1,
  3952.                          TREE_OPERAND (xarg0, 1), 1)))
  3953.           && (tree_int_cst_sgn (c2) >= 0
  3954.           || integer_zerop (const_binop (TRUNC_MOD_EXPR, c2,
  3955.                          arg1, 1))))
  3956.         {
  3957.           tree outer_div = integer_one_node;
  3958.           tree c1 = TREE_OPERAND (xarg0, 1);
  3959.           tree c3 = arg1;
  3960.  
  3961.           /* If C3 > C1, set them equal and do a divide by
  3962.          C3/C1 at the end of the operation.  */
  3963.           if (tree_int_cst_lt (c1, c3))
  3964.         outer_div = const_binop (code, c3, c1, 0), c3 = c1;
  3965.         
  3966.           /* The result is A * (C1/C3) + (C2/C3).  */
  3967.           t = fold (build (PLUS_EXPR, type,
  3968.                    fold (build (MULT_EXPR, type,
  3969.                         TREE_OPERAND (xarg0, 0),
  3970.                         const_binop (code, c1, c3, 1))),
  3971.                    const_binop (code, c2, c3, 1)));
  3972.  
  3973.           if (! integer_onep (outer_div))
  3974.         t = fold (build (code, type, t, convert (type, outer_div)));
  3975.  
  3976.           if (have_save_expr)
  3977.         t = save_expr (t);
  3978.  
  3979.           return t;
  3980.         }
  3981.     }
  3982.  
  3983.       goto binary;
  3984.  
  3985.     case CEIL_MOD_EXPR:
  3986.     case FLOOR_MOD_EXPR:
  3987.     case ROUND_MOD_EXPR:
  3988.     case TRUNC_MOD_EXPR:
  3989.       if (integer_onep (arg1))
  3990.     return omit_one_operand (type, integer_zero_node, arg0);
  3991.       if (integer_zerop (arg1))
  3992.     return t;
  3993.  
  3994.       /* Look for ((a * C1) % C3) or (((a * C1) + C2) % C3),
  3995.      where C1 % C3 == 0.  Handle similarly to the division case,
  3996.      but don't bother with SAVE_EXPRs.  */
  3997.  
  3998.       if (TREE_CODE (arg1) == INTEGER_CST
  3999.       && ! integer_zerop (arg1))
  4000.     {
  4001.       tree c2 = integer_zero_node;
  4002.       tree xarg0 = arg0;
  4003.  
  4004.       if (TREE_CODE (xarg0) == PLUS_EXPR
  4005.           && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST)
  4006.         c2 = TREE_OPERAND (xarg0, 1), xarg0 = TREE_OPERAND (xarg0, 0);
  4007.       else if (TREE_CODE (xarg0) == MINUS_EXPR
  4008.            && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
  4009.            && ! TREE_UNSIGNED (type))
  4010.         {
  4011.           c2 = fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (xarg0, 1)));
  4012.           xarg0 = TREE_OPERAND (xarg0, 0);
  4013.         }
  4014.  
  4015.       STRIP_NOPS (xarg0);
  4016.  
  4017.       if (TREE_CODE (xarg0) == MULT_EXPR
  4018.           && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
  4019.           && integer_zerop (const_binop (TRUNC_MOD_EXPR,
  4020.                          TREE_OPERAND (xarg0, 1),
  4021.                          arg1, 1))
  4022.           && tree_int_cst_sgn (c2) >= 0)
  4023.         /* The result is (C2%C3).  */
  4024.         return omit_one_operand (type, const_binop (code, c2, arg1, 1),
  4025.                      TREE_OPERAND (xarg0, 0));
  4026.     }
  4027.  
  4028.       goto binary;
  4029.  
  4030.     case LSHIFT_EXPR:
  4031.     case RSHIFT_EXPR:
  4032.     case LROTATE_EXPR:
  4033.     case RROTATE_EXPR:
  4034.       if (integer_zerop (arg1))
  4035.     return non_lvalue (convert (type, arg0));
  4036.       /* Since negative shift count is not well-defined,
  4037.      don't try to compute it in the compiler.  */
  4038.       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
  4039.     return t;
  4040.       /* Rewrite an LROTATE_EXPR by a constant into an
  4041.      RROTATE_EXPR by a new constant.  */
  4042.       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
  4043.     {
  4044.       TREE_SET_CODE (t, RROTATE_EXPR);
  4045.       code = RROTATE_EXPR;
  4046.       TREE_OPERAND (t, 1) = arg1
  4047.         = const_binop
  4048.           (MINUS_EXPR,
  4049.            convert (TREE_TYPE (arg1),
  4050.             build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0)),
  4051.            arg1, 0);
  4052.       if (tree_int_cst_sgn (arg1) < 0)
  4053.         return t;
  4054.     }
  4055.  
  4056.       /* If we have a rotate of a bit operation with the rotate count and
  4057.      the second operand of the bit operation both constant,
  4058.      permute the two operations.  */
  4059.       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
  4060.       && (TREE_CODE (arg0) == BIT_AND_EXPR
  4061.           || TREE_CODE (arg0) == BIT_ANDTC_EXPR
  4062.           || TREE_CODE (arg0) == BIT_IOR_EXPR
  4063.           || TREE_CODE (arg0) == BIT_XOR_EXPR)
  4064.       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
  4065.     return fold (build (TREE_CODE (arg0), type,
  4066.                 fold (build (code, type,
  4067.                      TREE_OPERAND (arg0, 0), arg1)),
  4068.                 fold (build (code, type,
  4069.                      TREE_OPERAND (arg0, 1), arg1))));
  4070.  
  4071.       /* Two consecutive rotates adding up to the width of the mode can
  4072.      be ignored.  */
  4073.       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
  4074.       && TREE_CODE (arg0) == RROTATE_EXPR
  4075.       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
  4076.       && TREE_INT_CST_HIGH (arg1) == 0
  4077.       && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
  4078.       && ((TREE_INT_CST_LOW (arg1)
  4079.            + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
  4080.           == GET_MODE_BITSIZE (TYPE_MODE (type))))
  4081.     return TREE_OPERAND (arg0, 0);
  4082.  
  4083.       goto binary;
  4084.  
  4085.     case MIN_EXPR:
  4086.       if (operand_equal_p (arg0, arg1, 0))
  4087.     return arg0;
  4088.       if (INTEGRAL_TYPE_P (type)
  4089.       && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1))
  4090.     return omit_one_operand (type, arg1, arg0);
  4091.       goto associate;
  4092.  
  4093.     case MAX_EXPR:
  4094.       if (operand_equal_p (arg0, arg1, 0))
  4095.     return arg0;
  4096.       if (INTEGRAL_TYPE_P (type)
  4097.       && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1))
  4098.     return omit_one_operand (type, arg1, arg0);
  4099.       goto associate;
  4100.  
  4101.     case TRUTH_NOT_EXPR:
  4102.       /* Note that the operand of this must be an int
  4103.      and its values must be 0 or 1.
  4104.      ("true" is a fixed value perhaps depending on the language,
  4105.      but we don't handle values other than 1 correctly yet.)  */
  4106.       return invert_truthvalue (arg0);
  4107.  
  4108.     case TRUTH_ANDIF_EXPR:
  4109.       /* Note that the operands of this must be ints
  4110.      and their values must be 0 or 1.
  4111.      ("true" is a fixed value perhaps depending on the language.)  */
  4112.       /* If first arg is constant zero, return it.  */
  4113.       if (integer_zerop (arg0))
  4114.     return arg0;
  4115.     case TRUTH_AND_EXPR:
  4116.       /* If either arg is constant true, drop it.  */
  4117.       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
  4118.     return non_lvalue (arg1);
  4119.       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
  4120.     return non_lvalue (arg0);
  4121.       /* If second arg is constant zero, result is zero, but first arg
  4122.      must be evaluated.  */
  4123.       if (integer_zerop (arg1))
  4124.     return omit_one_operand (type, arg1, arg0);
  4125.  
  4126.     truth_andor:
  4127.       /* We only do these simplifications if we are optimizing.  */
  4128.       if (!optimize)
  4129.     return t;
  4130.  
  4131.       /* Check for things like (A || B) && (A || C).  We can convert this
  4132.      to A || (B && C).  Note that either operator can be any of the four
  4133.      truth and/or operations and the transformation will still be
  4134.      valid.   Also note that we only care about order for the
  4135.      ANDIF and ORIF operators.  */
  4136.       if (TREE_CODE (arg0) == TREE_CODE (arg1)
  4137.       && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
  4138.           || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
  4139.           || TREE_CODE (arg0) == TRUTH_AND_EXPR
  4140.           || TREE_CODE (arg0) == TRUTH_OR_EXPR))
  4141.     {
  4142.       tree a00 = TREE_OPERAND (arg0, 0);
  4143.       tree a01 = TREE_OPERAND (arg0, 1);
  4144.       tree a10 = TREE_OPERAND (arg1, 0);
  4145.       tree a11 = TREE_OPERAND (arg1, 1);
  4146.       int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
  4147.                   || TREE_CODE (arg0) == TRUTH_AND_EXPR)
  4148.                  && (code == TRUTH_AND_EXPR
  4149.                  || code == TRUTH_OR_EXPR));
  4150.  
  4151.       if (operand_equal_p (a00, a10, 0))
  4152.         return fold (build (TREE_CODE (arg0), type, a00,
  4153.                 fold (build (code, type, a01, a11))));
  4154.       else if (commutative && operand_equal_p (a00, a11, 0))
  4155.         return fold (build (TREE_CODE (arg0), type, a00,
  4156.                 fold (build (code, type, a01, a10))));
  4157.       else if (commutative && operand_equal_p (a01, a10, 0))
  4158.         return fold (build (TREE_CODE (arg0), type, a01,
  4159.                 fold (build (code, type, a00, a11))));
  4160.  
  4161.       /* This case if tricky because we must either have commutative
  4162.          operators or else A10 must not have side-effects.  */
  4163.  
  4164.       else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
  4165.            && operand_equal_p (a01, a11, 0))
  4166.         return fold (build (TREE_CODE (arg0), type,
  4167.                 fold (build (code, type, a00, a10)),
  4168.                 a01));
  4169.     }
  4170.  
  4171.       /* Check for the possibility of merging component references.  If our
  4172.      lhs is another similar operation, try to merge its rhs with our
  4173.      rhs.  Then try to merge our lhs and rhs.  */
  4174.       if (TREE_CODE (arg0) == code
  4175.       && 0 != (tem = fold_truthop (code, type,
  4176.                        TREE_OPERAND (arg0, 1), arg1)))
  4177.     return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
  4178.  
  4179.       if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
  4180.     return tem;
  4181.  
  4182.       return t;
  4183.  
  4184.     case TRUTH_ORIF_EXPR:
  4185.       /* Note that the operands of this must be ints
  4186.      and their values must be 0 or true.
  4187.      ("true" is a fixed value perhaps depending on the language.)  */
  4188.       /* If first arg is constant true, return it.  */
  4189.       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
  4190.     return arg0;
  4191.     case TRUTH_OR_EXPR:
  4192.       /* If either arg is constant zero, drop it.  */
  4193.       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
  4194.     return non_lvalue (arg1);
  4195.       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1))
  4196.     return non_lvalue (arg0);
  4197.       /* If second arg is constant true, result is true, but we must
  4198.      evaluate first arg.  */
  4199.       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
  4200.     return omit_one_operand (type, arg1, arg0);
  4201.       goto truth_andor;
  4202.  
  4203.     case TRUTH_XOR_EXPR:
  4204.       /* If either arg is constant zero, drop it.  */
  4205.       if (integer_zerop (arg0))
  4206.     return non_lvalue (arg1);
  4207.       if (integer_zerop (arg1))
  4208.     return non_lvalue (arg0);
  4209.       /* If either arg is constant true, this is a logical inversion.  */
  4210.       if (integer_onep (arg0))
  4211.     return non_lvalue (invert_truthvalue (arg1));
  4212.       if (integer_onep (arg1))
  4213.     return non_lvalue (invert_truthvalue (arg0));
  4214.       return t;
  4215.  
  4216.     case EQ_EXPR:
  4217.     case NE_EXPR:
  4218.     case LT_EXPR:
  4219.     case GT_EXPR:
  4220.     case LE_EXPR:
  4221.     case GE_EXPR:
  4222.       /* If one arg is a constant integer, put it last.  */
  4223.       if (TREE_CODE (arg0) == INTEGER_CST
  4224.       && TREE_CODE (arg1) != INTEGER_CST)
  4225.     {
  4226.       TREE_OPERAND (t, 0) = arg1;
  4227.       TREE_OPERAND (t, 1) = arg0;
  4228.       arg0 = TREE_OPERAND (t, 0);
  4229.       arg1 = TREE_OPERAND (t, 1);
  4230.       code = swap_tree_comparison (code);
  4231.       TREE_SET_CODE (t, code);
  4232.     }
  4233.  
  4234.       /* Convert foo++ == CONST into ++foo == CONST + INCR.
  4235.      First, see if one arg is constant; find the constant arg
  4236.      and the other one.  */
  4237.       {
  4238.     tree constop = 0, varop;
  4239.     int constopnum = -1;
  4240.  
  4241.     if (TREE_CONSTANT (arg1))
  4242.       constopnum = 1, constop = arg1, varop = arg0;
  4243.     if (TREE_CONSTANT (arg0))
  4244.       constopnum = 0, constop = arg0, varop = arg1;
  4245.  
  4246.     if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
  4247.       {
  4248.         /* This optimization is invalid for ordered comparisons
  4249.            if CONST+INCR overflows or if foo+incr might overflow.
  4250.            This optimization is invalid for floating point due to rounding.
  4251.            For pointer types we assume overflow doesn't happen.  */
  4252.         if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE
  4253.         || (! FLOAT_TYPE_P (TREE_TYPE (varop))
  4254.             && (code == EQ_EXPR || code == NE_EXPR)))
  4255.           {
  4256.         tree newconst
  4257.           = fold (build (PLUS_EXPR, TREE_TYPE (varop),
  4258.                  constop, TREE_OPERAND (varop, 1)));
  4259.         TREE_SET_CODE (varop, PREINCREMENT_EXPR);
  4260.  
  4261.         t = build (code, type, TREE_OPERAND (t, 0),
  4262.                TREE_OPERAND (t, 1));
  4263.         TREE_OPERAND (t, constopnum) = newconst;
  4264.         return t;
  4265.           }
  4266.       }
  4267.     else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
  4268.       {
  4269.         if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE
  4270.         || (! FLOAT_TYPE_P (TREE_TYPE (varop))
  4271.             && (code == EQ_EXPR || code == NE_EXPR)))
  4272.           {
  4273.         tree newconst
  4274.           = fold (build (MINUS_EXPR, TREE_TYPE (varop),
  4275.                  constop, TREE_OPERAND (varop, 1)));
  4276.         TREE_SET_CODE (varop, PREDECREMENT_EXPR);
  4277.         t = build (code, type, TREE_OPERAND (t, 0),
  4278.                TREE_OPERAND (t, 1));
  4279.         TREE_OPERAND (t, constopnum) = newconst;
  4280.         return t;
  4281.           }
  4282.       }
  4283.       }
  4284.  
  4285.       /* Change X >= CST to X > (CST - 1) if CST is positive.  */
  4286.       if (TREE_CODE (arg1) == INTEGER_CST
  4287.       && TREE_CODE (arg0) != INTEGER_CST
  4288.       && tree_int_cst_sgn (arg1) > 0)
  4289.     {
  4290.       switch (TREE_CODE (t))
  4291.         {
  4292.         case GE_EXPR:
  4293.           code = GT_EXPR;
  4294.           arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
  4295.           t = build (code, type, TREE_OPERAND (t, 0), arg1);
  4296.           break;
  4297.  
  4298.         case LT_EXPR:
  4299.           code = LE_EXPR;
  4300.           arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
  4301.           t = build (code, type, TREE_OPERAND (t, 0), arg1);
  4302.           break;
  4303.         }
  4304.     }
  4305.  
  4306.       /* If this is an EQ or NE comparison with zero and ARG0 is
  4307.      (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
  4308.      two operations, but the latter can be done in one less insn
  4309.      one machine that have only two-operand insns or on which a
  4310.      constant cannot be the first operand.  */
  4311.       if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
  4312.       && TREE_CODE (arg0) == BIT_AND_EXPR)
  4313.     {
  4314.       if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
  4315.           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
  4316.         return
  4317.           fold (build (code, type,
  4318.                build (BIT_AND_EXPR, TREE_TYPE (arg0),
  4319.                   build (RSHIFT_EXPR,
  4320.                      TREE_TYPE (TREE_OPERAND (arg0, 0)),
  4321.                      TREE_OPERAND (arg0, 1),
  4322.                      TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
  4323.                   convert (TREE_TYPE (arg0),
  4324.                        integer_one_node)),
  4325.                arg1));
  4326.       else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
  4327.            && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
  4328.         return
  4329.           fold (build (code, type,
  4330.                build (BIT_AND_EXPR, TREE_TYPE (arg0),
  4331.                   build (RSHIFT_EXPR,
  4332.                      TREE_TYPE (TREE_OPERAND (arg0, 1)),
  4333.                      TREE_OPERAND (arg0, 0),
  4334.                      TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
  4335.                   convert (TREE_TYPE (arg0),
  4336.                        integer_one_node)),
  4337.                arg1));
  4338.     }
  4339.  
  4340.       /* If this is an NE or EQ comparison of zero against the result of a
  4341.      signed MOD operation whose second operand is a power of 2, make
  4342.      the MOD operation unsigned since it is simpler and equivalent.  */
  4343.       if ((code == NE_EXPR || code == EQ_EXPR)
  4344.       && integer_zerop (arg1)
  4345.       && ! TREE_UNSIGNED (TREE_TYPE (arg0))
  4346.       && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
  4347.           || TREE_CODE (arg0) == CEIL_MOD_EXPR
  4348.           || TREE_CODE (arg0) == FLOOR_MOD_EXPR
  4349.           || TREE_CODE (arg0) == ROUND_MOD_EXPR)
  4350.       && integer_pow2p (TREE_OPERAND (arg0, 1)))
  4351.     {
  4352.       tree newtype = unsigned_type (TREE_TYPE (arg0));
  4353.       tree newmod = build (TREE_CODE (arg0), newtype,
  4354.                    convert (newtype, TREE_OPERAND (arg0, 0)),
  4355.                    convert (newtype, TREE_OPERAND (arg0, 1)));
  4356.  
  4357.       return build (code, type, newmod, convert (newtype, arg1));
  4358.     }
  4359.  
  4360.       /* If this is an NE comparison of zero with an AND of one, remove the
  4361.      comparison since the AND will give the correct value.  */
  4362.       if (code == NE_EXPR && integer_zerop (arg1)
  4363.       && TREE_CODE (arg0) == BIT_AND_EXPR
  4364.       && integer_onep (TREE_OPERAND (arg0, 1)))
  4365.     return convert (type, arg0);
  4366.  
  4367.       /* If we have (A & C) == C where C is a power of 2, convert this into
  4368.      (A & C) != 0.  Similarly for NE_EXPR.  */
  4369.       if ((code == EQ_EXPR || code == NE_EXPR)
  4370.       && TREE_CODE (arg0) == BIT_AND_EXPR
  4371.       && integer_pow2p (TREE_OPERAND (arg0, 1))
  4372.       && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
  4373.     return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
  4374.               arg0, integer_zero_node);
  4375.  
  4376.       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
  4377.      and similarly for >= into !=.  */
  4378.       if ((code == LT_EXPR || code == GE_EXPR)
  4379.       && TREE_UNSIGNED (TREE_TYPE (arg0))
  4380.       && TREE_CODE (arg1) == LSHIFT_EXPR
  4381.       && integer_onep (TREE_OPERAND (arg1, 0)))
  4382.     return build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type, 
  4383.               build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
  4384.                  TREE_OPERAND (arg1, 1)),
  4385.               convert (TREE_TYPE (arg0), integer_zero_node));
  4386.  
  4387.       else if ((code == LT_EXPR || code == GE_EXPR)
  4388.            && TREE_UNSIGNED (TREE_TYPE (arg0))
  4389.            && (TREE_CODE (arg1) == NOP_EXPR
  4390.            || TREE_CODE (arg1) == CONVERT_EXPR)
  4391.            && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
  4392.            && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
  4393.     return
  4394.       build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
  4395.          convert (TREE_TYPE (arg0),
  4396.               build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
  4397.                  TREE_OPERAND (TREE_OPERAND (arg1, 0), 1))),
  4398.          convert (TREE_TYPE (arg0), integer_zero_node));
  4399.  
  4400.       /* Simplify comparison of something with itself.  (For IEEE
  4401.      floating-point, we can only do some of these simplifications.)  */
  4402.       if (operand_equal_p (arg0, arg1, 0))
  4403.     {
  4404.       switch (code)
  4405.         {
  4406.         case EQ_EXPR:
  4407.         case GE_EXPR:
  4408.         case LE_EXPR:
  4409.           if (INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
  4410.         {
  4411.           t = build_int_2 (1, 0);
  4412.           TREE_TYPE (t) = type;
  4413.           return t;
  4414.         }
  4415.           code = EQ_EXPR;
  4416.           TREE_SET_CODE (t, code);
  4417.           break;
  4418.  
  4419.         case NE_EXPR:
  4420.           /* For NE, we can only do this simplification if integer.  */
  4421.           if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
  4422.         break;
  4423.           /* ... fall through ... */
  4424.         case GT_EXPR:
  4425.         case LT_EXPR:
  4426.           t = build_int_2 (0, 0);
  4427.           TREE_TYPE (t) = type;
  4428.           return t;
  4429.         }
  4430.     }
  4431.  
  4432.       /* An unsigned comparison against 0 can be simplified.  */
  4433.       if (integer_zerop (arg1)
  4434.       && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
  4435.           || TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE)
  4436.       && TREE_UNSIGNED (TREE_TYPE (arg1)))
  4437.     {
  4438.       switch (TREE_CODE (t))
  4439.         {
  4440.         case GT_EXPR:
  4441.           code = NE_EXPR;
  4442.           TREE_SET_CODE (t, NE_EXPR);
  4443.           break;
  4444.         case LE_EXPR:
  4445.           code = EQ_EXPR;
  4446.           TREE_SET_CODE (t, EQ_EXPR);
  4447.           break;
  4448.         case GE_EXPR:
  4449.           return omit_one_operand (type,
  4450.                        convert (type, integer_one_node),
  4451.                        arg0);
  4452.         case LT_EXPR:
  4453.           return omit_one_operand (type,
  4454.                        convert (type, integer_zero_node),
  4455.                        arg0);
  4456.         }
  4457.     }
  4458.  
  4459.       /* If we are comparing an expression that just has comparisons
  4460.      of two integer values, arithmetic expressions of those comparisons,
  4461.      and constants, we can simplify it.  There are only three cases
  4462.      to check: the two values can either be equal, the first can be
  4463.      greater, or the second can be greater.  Fold the expression for
  4464.      those three values.  Since each value must be 0 or 1, we have
  4465.      eight possibilities, each of which corresponds to the constant 0
  4466.      or 1 or one of the six possible comparisons.
  4467.  
  4468.      This handles common cases like (a > b) == 0 but also handles
  4469.      expressions like  ((x > y) - (y > x)) > 0, which supposedly
  4470.      occur in macroized code.  */
  4471.  
  4472.       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
  4473.     {
  4474.       tree cval1 = 0, cval2 = 0;
  4475.       int save_p = 0;
  4476.  
  4477.       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
  4478.           /* Don't handle degenerate cases here; they should already
  4479.          have been handled anyway.  */
  4480.           && cval1 != 0 && cval2 != 0
  4481.           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
  4482.           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
  4483.           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
  4484.           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
  4485.                     TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
  4486.         {
  4487.           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
  4488.           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
  4489.  
  4490.           /* We can't just pass T to eval_subst in case cval1 or cval2
  4491.          was the same as ARG1.  */
  4492.  
  4493.           tree high_result
  4494.         = fold (build (code, type,
  4495.                    eval_subst (arg0, cval1, maxval, cval2, minval),
  4496.                    arg1));
  4497.           tree equal_result
  4498.         = fold (build (code, type,
  4499.                    eval_subst (arg0, cval1, maxval, cval2, maxval),
  4500.                    arg1));
  4501.           tree low_result
  4502.         = fold (build (code, type,
  4503.                    eval_subst (arg0, cval1, minval, cval2, maxval),
  4504.                    arg1));
  4505.  
  4506.           /* All three of these results should be 0 or 1.  Confirm they
  4507.          are.  Then use those values to select the proper code
  4508.          to use.  */
  4509.  
  4510.           if ((integer_zerop (high_result)
  4511.            || integer_onep (high_result))
  4512.           && (integer_zerop (equal_result)
  4513.               || integer_onep (equal_result))
  4514.           && (integer_zerop (low_result)
  4515.               || integer_onep (low_result)))
  4516.         {
  4517.           /* Make a 3-bit mask with the high-order bit being the
  4518.              value for `>', the next for '=', and the low for '<'.  */
  4519.           switch ((integer_onep (high_result) * 4)
  4520.               + (integer_onep (equal_result) * 2)
  4521.               + integer_onep (low_result))
  4522.             {
  4523.             case 0:
  4524.               /* Always false.  */
  4525.               return omit_one_operand (type, integer_zero_node, arg0);
  4526.             case 1:
  4527.               code = LT_EXPR;
  4528.               break;
  4529.             case 2:
  4530.               code = EQ_EXPR;
  4531.               break;
  4532.             case 3:
  4533.               code = LE_EXPR;
  4534.               break;
  4535.             case 4:
  4536.               code = GT_EXPR;
  4537.               break;
  4538.             case 5:
  4539.               code = NE_EXPR;
  4540.               break;
  4541.             case 6:
  4542.               code = GE_EXPR;
  4543.               break;
  4544.             case 7:
  4545.               /* Always true.  */
  4546.               return omit_one_operand (type, integer_one_node, arg0);
  4547.             }
  4548.  
  4549.           t = build (code, type, cval1, cval2);
  4550.           if (save_p)
  4551.             return save_expr (t);
  4552.           else
  4553.             return fold (t);
  4554.         }
  4555.         }
  4556.     }
  4557.  
  4558.       /* If this is a comparison of a field, we may be able to simplify it.  */
  4559.       if ((TREE_CODE (arg0) == COMPONENT_REF
  4560.         || TREE_CODE (arg0) == BIT_FIELD_REF)
  4561.            && (code == EQ_EXPR || code == NE_EXPR)
  4562.            /* Handle the constant case even without -O
  4563.           to make sure the warnings are given.  */
  4564.            && (optimize || TREE_CODE (arg1) == INTEGER_CST))
  4565.     {
  4566.       t1 = optimize_bit_field_compare (code, type, arg0, arg1);
  4567.       return t1 ? t1 : t;
  4568.     }
  4569.  
  4570.       /* If this is a comparison of complex values and either or both
  4571.      sizes are a COMPLEX_EXPR, it is best to split up the comparisons
  4572.      and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.  This
  4573.      may prevent needless evaluations.  */
  4574.       if ((code == EQ_EXPR || code == NE_EXPR)
  4575.       && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
  4576.       && (TREE_CODE (arg0) == COMPLEX_EXPR
  4577.           || TREE_CODE (arg1) == COMPLEX_EXPR))
  4578.     {
  4579.       tree subtype = TREE_TYPE (TREE_TYPE (arg0));
  4580.       tree real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
  4581.       tree imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
  4582.       tree real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
  4583.       tree imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
  4584.  
  4585.       return fold (build ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
  4586.                    : TRUTH_ORIF_EXPR),
  4587.                   type,
  4588.                   fold (build (code, type, real0, real1)),
  4589.                   fold (build (code, type, imag0, imag1))));
  4590.     }
  4591.  
  4592.       /* From here on, the only cases we handle are when the result is
  4593.      known to be a constant.
  4594.  
  4595.      To compute GT, swap the arguments and do LT.
  4596.      To compute GE, do LT and invert the result.
  4597.      To compute LE, swap the arguments, do LT and invert the result.
  4598.      To compute NE, do EQ and invert the result.
  4599.  
  4600.      Therefore, the code below must handle only EQ and LT.  */
  4601.  
  4602.       if (code == LE_EXPR || code == GT_EXPR)
  4603.     {
  4604.       tem = arg0, arg0 = arg1, arg1 = tem;
  4605.       code = swap_tree_comparison (code);
  4606.     }
  4607.  
  4608.       /* Note that it is safe to invert for real values here because we
  4609.      will check below in the one case that it matters.  */
  4610.  
  4611.       invert = 0;
  4612.       if (code == NE_EXPR || code == GE_EXPR)
  4613.     {
  4614.       invert = 1;
  4615.       code = invert_tree_comparison (code);
  4616.     }
  4617.  
  4618.       /* Compute a result for LT or EQ if args permit;
  4619.      otherwise return T.  */
  4620.       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
  4621.     {
  4622.       if (code == EQ_EXPR)
  4623.         t1 = build_int_2 ((TREE_INT_CST_LOW (arg0)
  4624.                    == TREE_INT_CST_LOW (arg1))
  4625.                   && (TREE_INT_CST_HIGH (arg0)
  4626.                   == TREE_INT_CST_HIGH (arg1)),
  4627.                   0);
  4628.       else
  4629.         t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
  4630.                    ? INT_CST_LT_UNSIGNED (arg0, arg1)
  4631.                    : INT_CST_LT (arg0, arg1)),
  4632.                   0);
  4633.     }
  4634.  
  4635.       /* Assume a nonexplicit constant cannot equal an explicit one,
  4636.      since such code would be undefined anyway.
  4637.      Exception: on sysvr4, using #pragma weak,
  4638.      a label can come out as 0.  */
  4639.       else if (TREE_CODE (arg1) == INTEGER_CST
  4640.            && !integer_zerop (arg1)
  4641.            && TREE_CONSTANT (arg0)
  4642.            && TREE_CODE (arg0) == ADDR_EXPR
  4643.            && code == EQ_EXPR)
  4644.     t1 = build_int_2 (0, 0);
  4645.  
  4646.       /* Two real constants can be compared explicitly.  */
  4647.       else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
  4648.     {
  4649.       /* If either operand is a NaN, the result is false with two
  4650.          exceptions: First, an NE_EXPR is true on NaNs, but that case
  4651.          is already handled correctly since we will be inverting the
  4652.          result for NE_EXPR.  Second, if we had inverted a LE_EXPR
  4653.          or a GE_EXPR into a LT_EXPR, we must return true so that it
  4654.          will be inverted into false.  */
  4655.  
  4656.       if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
  4657.           || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
  4658.         t1 = build_int_2 (invert && code == LT_EXPR, 0);
  4659.  
  4660.       else if (code == EQ_EXPR)
  4661.         t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
  4662.                          TREE_REAL_CST (arg1)),
  4663.                   0);
  4664.       else
  4665.         t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0),
  4666.                         TREE_REAL_CST (arg1)),
  4667.                   0);
  4668.     }
  4669.  
  4670.       if (t1 == NULL_TREE)
  4671.     return t;
  4672.  
  4673.       if (invert)
  4674.     TREE_INT_CST_LOW (t1) ^= 1;
  4675.  
  4676.       TREE_TYPE (t1) = type;
  4677.       return t1;
  4678.  
  4679.     case COND_EXPR:
  4680.       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
  4681.      so all simple results must be passed through pedantic_non_lvalue.  */
  4682.       if (TREE_CODE (arg0) == INTEGER_CST)
  4683.     return pedantic_non_lvalue
  4684.       (TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1)));
  4685.       else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
  4686.     return pedantic_non_lvalue (omit_one_operand (type, arg1, arg0));
  4687.  
  4688.       /* If the second operand is zero, invert the comparison and swap
  4689.      the second and third operands.  Likewise if the second operand
  4690.      is constant and the third is not or if the third operand is
  4691.      equivalent to the first operand of the comparison.  */
  4692.  
  4693.       if (integer_zerop (arg1)
  4694.       || (TREE_CONSTANT (arg1) && ! TREE_CONSTANT (TREE_OPERAND (t, 2)))
  4695.       || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
  4696.           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
  4697.                          TREE_OPERAND (t, 2),
  4698.                          TREE_OPERAND (arg0, 1))))
  4699.     {
  4700.       /* See if this can be inverted.  If it can't, possibly because
  4701.          it was a floating-point inequality comparison, don't do
  4702.          anything.  */
  4703.       tem = invert_truthvalue (arg0);
  4704.  
  4705.       if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
  4706.         {
  4707.           t = build (code, type, tem,
  4708.              TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
  4709.           arg0 = tem;
  4710.             arg1 = TREE_OPERAND (t, 2);
  4711.         }
  4712.     }
  4713.  
  4714.       /* If we have A op B ? A : C, we may be able to convert this to a
  4715.      simpler expression, depending on the operation and the values
  4716.      of B and C.  IEEE floating point prevents this though,
  4717.      because A or B might be -0.0 or a NaN.  */
  4718.  
  4719.       if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
  4720.       && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  4721.           || ! FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
  4722.           || flag_fast_math)
  4723.       && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
  4724.                          arg1, TREE_OPERAND (arg0, 1)))
  4725.     {
  4726.       tree arg2 = TREE_OPERAND (t, 2);
  4727.       enum tree_code comp_code = TREE_CODE (arg0);
  4728.  
  4729.       /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or abs (-A),
  4730.          depending on the comparison operation.  */
  4731.       if (integer_zerop (TREE_OPERAND (arg0, 1))
  4732.           && TREE_CODE (arg2) == NEGATE_EXPR
  4733.           && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
  4734.         switch (comp_code)
  4735.           {
  4736.           case EQ_EXPR:
  4737.         return pedantic_non_lvalue
  4738.           (fold (build1 (NEGATE_EXPR, type, arg1)));
  4739.           case NE_EXPR:
  4740.         return pedantic_non_lvalue (convert (type, arg1));
  4741.           case GE_EXPR:
  4742.           case GT_EXPR:
  4743.         return pedantic_non_lvalue
  4744.           (fold (build1 (ABS_EXPR, type, arg1)));
  4745.           case LE_EXPR:
  4746.           case LT_EXPR:
  4747.         return pedantic_non_lvalue
  4748.           (fold (build1 (NEGATE_EXPR, type,
  4749.                  fold (build1 (ABS_EXPR, type, arg1)))));
  4750.           }
  4751.  
  4752.       /* If this is A != 0 ? A : 0, this is simply A.  For ==, it is
  4753.          always zero.  */
  4754.  
  4755.       if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
  4756.         {
  4757.           if (comp_code == NE_EXPR)
  4758.         return pedantic_non_lvalue (convert (type, arg1));
  4759.           else if (comp_code == EQ_EXPR)
  4760.         return pedantic_non_lvalue (convert (type, integer_zero_node));
  4761.         }
  4762.  
  4763.       /* If this is A op B ? A : B, this is either A, B, min (A, B),
  4764.          or max (A, B), depending on the operation.  */
  4765.  
  4766.       if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
  4767.                           arg2, TREE_OPERAND (arg0, 0)))
  4768.         switch (comp_code)
  4769.           {
  4770.           case EQ_EXPR:
  4771.         return pedantic_non_lvalue (convert (type, arg2));
  4772.           case NE_EXPR:
  4773.         return pedantic_non_lvalue (convert (type, arg1));
  4774.           case LE_EXPR:
  4775.           case LT_EXPR:
  4776.         return pedantic_non_lvalue
  4777.           (fold (build (MIN_EXPR, type, arg1, arg2)));
  4778.           case GE_EXPR:
  4779.           case GT_EXPR:
  4780.         return pedantic_non_lvalue
  4781.           (fold (build (MAX_EXPR, type, arg1, arg2)));
  4782.           }
  4783.  
  4784.       /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
  4785.          we might still be able to simplify this.  For example,
  4786.          if C1 is one less or one more than C2, this might have started
  4787.          out as a MIN or MAX and been transformed by this function.
  4788.          Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
  4789.  
  4790.       if (INTEGRAL_TYPE_P (type)
  4791.           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
  4792.           && TREE_CODE (arg2) == INTEGER_CST)
  4793.         switch (comp_code)
  4794.           {
  4795.           case EQ_EXPR:
  4796.         /* We can replace A with C1 in this case.  */
  4797.         arg1 = convert (type, TREE_OPERAND (arg0, 1));
  4798.         t = build (code, type, TREE_OPERAND (t, 0), arg1,
  4799.                TREE_OPERAND (t, 2));
  4800.         break;
  4801.  
  4802.           case LT_EXPR:
  4803.         /* If C1 is C2 + 1, this is min(A, C2).  */
  4804.         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
  4805.             && operand_equal_p (TREE_OPERAND (arg0, 1),
  4806.                     const_binop (PLUS_EXPR, arg2,
  4807.                              integer_one_node, 0), 1))
  4808.           return pedantic_non_lvalue
  4809.             (fold (build (MIN_EXPR, type, arg1, arg2)));
  4810.         break;
  4811.  
  4812.           case LE_EXPR:
  4813.         /* If C1 is C2 - 1, this is min(A, C2).  */
  4814.         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
  4815.             && operand_equal_p (TREE_OPERAND (arg0, 1),
  4816.                     const_binop (MINUS_EXPR, arg2,
  4817.                              integer_one_node, 0), 1))
  4818.           return pedantic_non_lvalue
  4819.             (fold (build (MIN_EXPR, type, arg1, arg2)));
  4820.         break;
  4821.  
  4822.           case GT_EXPR:
  4823.         /* If C1 is C2 - 1, this is max(A, C2).  */
  4824.         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
  4825.             && operand_equal_p (TREE_OPERAND (arg0, 1),
  4826.                     const_binop (MINUS_EXPR, arg2,
  4827.                              integer_one_node, 0), 1))
  4828.           return pedantic_non_lvalue
  4829.             (fold (build (MAX_EXPR, type, arg1, arg2)));
  4830.         break;
  4831.  
  4832.           case GE_EXPR:
  4833.         /* If C1 is C2 + 1, this is max(A, C2).  */
  4834.         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
  4835.             && operand_equal_p (TREE_OPERAND (arg0, 1),
  4836.                     const_binop (PLUS_EXPR, arg2,
  4837.                              integer_one_node, 0), 1))
  4838.           return pedantic_non_lvalue
  4839.             (fold (build (MAX_EXPR, type, arg1, arg2)));
  4840.         break;
  4841.           }
  4842.     }
  4843.  
  4844.       /* Convert A ? 1 : 0 to simply A.  */
  4845.       if (integer_onep (TREE_OPERAND (t, 1))
  4846.       && integer_zerop (TREE_OPERAND (t, 2))
  4847.       /* If we try to convert TREE_OPERAND (t, 0) to our type, the
  4848.          call to fold will try to move the conversion inside 
  4849.          a COND, which will recurse.  In that case, the COND_EXPR
  4850.          is probably the best choice, so leave it alone.  */
  4851.       && type == TREE_TYPE (arg0))
  4852.     return pedantic_non_lvalue (arg0);
  4853.  
  4854.  
  4855.       /* Look for expressions of the form A & 2 ? 2 : 0.  The result of this
  4856.      operation is simply A & 2.  */
  4857.  
  4858.       if (integer_zerop (TREE_OPERAND (t, 2))
  4859.       && TREE_CODE (arg0) == NE_EXPR
  4860.       && integer_zerop (TREE_OPERAND (arg0, 1))
  4861.       && integer_pow2p (arg1)
  4862.       && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
  4863.       && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
  4864.                   arg1, 1))
  4865.     return pedantic_non_lvalue (convert (type, TREE_OPERAND (arg0, 0)));
  4866.  
  4867.       return t;
  4868.  
  4869.     case COMPOUND_EXPR:
  4870.       /* When pedantic, a compound expression can be neither an lvalue
  4871.      nor an integer constant expression.  */
  4872.       if (TREE_SIDE_EFFECTS (arg0) || pedantic)
  4873.     return t;
  4874.       /* Don't let (0, 0) be null pointer constant.  */
  4875.       if (integer_zerop (arg1))
  4876.     return non_lvalue (arg1);
  4877.       return arg1;
  4878.  
  4879.     case COMPLEX_EXPR:
  4880.       if (wins)
  4881.     return build_complex (arg0, arg1);
  4882.       return t;
  4883.  
  4884.     case REALPART_EXPR:
  4885.       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
  4886.     return t;
  4887.       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
  4888.     return omit_one_operand (type, TREE_OPERAND (arg0, 0),
  4889.                  TREE_OPERAND (arg0, 1));
  4890.       else if (TREE_CODE (arg0) == COMPLEX_CST)
  4891.     return TREE_REALPART (arg0);
  4892.       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
  4893.     return fold (build (TREE_CODE (arg0), type,
  4894.                 fold (build1 (REALPART_EXPR, type,
  4895.                       TREE_OPERAND (arg0, 0))),
  4896.                 fold (build1 (REALPART_EXPR,
  4897.                       type, TREE_OPERAND (arg0, 1)))));
  4898.       return t;
  4899.  
  4900.     case IMAGPART_EXPR:
  4901.       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
  4902.     return convert (type, integer_zero_node);
  4903.       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
  4904.     return omit_one_operand (type, TREE_OPERAND (arg0, 1),
  4905.                  TREE_OPERAND (arg0, 0));
  4906.       else if (TREE_CODE (arg0) == COMPLEX_CST)
  4907.     return TREE_IMAGPART (arg0);
  4908.       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
  4909.     return fold (build (TREE_CODE (arg0), type,
  4910.                 fold (build1 (IMAGPART_EXPR, type,
  4911.                       TREE_OPERAND (arg0, 0))),
  4912.                 fold (build1 (IMAGPART_EXPR, type,
  4913.                       TREE_OPERAND (arg0, 1)))));
  4914.       return t;
  4915.  
  4916.     default:
  4917.       return t;
  4918.     } /* switch (code) */
  4919. }
  4920.