home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cc-61.0.1 / cc / fold-const.c < prev    next >
C/C++ Source or Header  |  1992-07-16  |  92KB  |  3,123 lines

  1. /* Fold a constant sub-tree into a single node for C-compiler
  2.    Copyright (C) 1987, 1988 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. /*@@ Fix lossage on folding division of big integers.  */
  21.  
  22. /*@@ This file should be rewritten to use an arbitary precision
  23.   @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
  24.   @@ Perhaps the routines could also be used for bc/dc, and made a lib.
  25.   @@ The routines that translate from the ap rep should
  26.   @@ warn if precision et. al. is lost.
  27.   @@ This would also make life easier when this technology is used
  28.   @@ for cross-compilers.  */
  29.  
  30.  
  31. /* The entry points in this file are fold, size_int and size_binop.
  32.  
  33.    fold takes a tree as argument and returns a simplified tree.
  34.  
  35.    size_binop takes a tree code for an arithmetic operation
  36.    and two operands that are trees, and produces a tree for the
  37.    result, assuming the type comes from `sizetype'.
  38.  
  39.    size_int takes an integer value, and creates a tree constant
  40.    with type from `sizetype'.  */
  41.    
  42. #include <stdio.h>
  43. #include <setjmp.h>
  44. #include "config.h"
  45. #include "flags.h"
  46. #include "tree.h"
  47.  
  48. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  49. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  50.  
  51. /* Strip any NON_LVALUE_EXPRs and NOP_EXPRs that don't change the
  52.    machine mode.  */
  53.  
  54. #define STRIP_NOPS(EXP)                    \
  55.   while ((TREE_CODE (EXP) == NOP_EXPR                \
  56.       || TREE_CODE (EXP) == NON_LVALUE_EXPR)        \
  57.      && (TYPE_MODE (TREE_TYPE (EXP))            \
  58.          == TYPE_MODE (TREE_TYPE (TREE_OPERAND (EXP, 0)))))    \
  59.     (EXP) = TREE_OPERAND (EXP, 0);
  60.  
  61. static void lshift_double ();
  62. static void rshift_double ();
  63. static void lrotate_double ();
  64. static void rrotate_double ();
  65. static tree const_binop ();
  66.  
  67. /* To do constant folding on INTEGER_CST nodes requires 64-bit arithmetic.
  68.    We do that by representing the 64-bit integer as 8 shorts,
  69.    with only 8 bits stored in each short, as a positive number.  */
  70.  
  71. /* Unpack a 64-bit integer into 8 shorts.
  72.    LOW and HI are the integer, as two `int' pieces.
  73.    SHORTS points to the array of shorts.  */
  74.  
  75. static void
  76. encode (shorts, low, hi)
  77.      short *shorts;
  78.      int low, hi;
  79. {
  80.   shorts[0] = low & 0xff;
  81.   shorts[1] = (low >> 8) & 0xff;
  82.   shorts[2] = (low >> 16) & 0xff;
  83.   shorts[3] = (low >> 24) & 0xff;
  84.   shorts[4] = hi & 0xff;
  85.   shorts[5] = (hi >> 8) & 0xff;
  86.   shorts[6] = (hi >> 16) & 0xff;
  87.   shorts[7] = (hi >> 24) & 0xff;
  88. }
  89.  
  90. /* Pack an array of 8 shorts into a 64-bit integer.
  91.    SHORTS points to the array of shorts.
  92.    The integer is stored into *LOW and *HI as two `int' pieces.  */
  93.  
  94. static void
  95. decode (shorts, low, hi)
  96.      short *shorts;
  97.      int *low, *hi;
  98. {
  99.   *low = (shorts[3] << 24) | (shorts[2] << 16) | (shorts[1] << 8) | shorts[0];
  100.   *hi = (shorts[7] << 24) | (shorts[6] << 16) | (shorts[5] << 8) | shorts[4];
  101. }
  102.  
  103. /* Make the integer constant T valid for its type
  104.    by setting to 0 or 1 all the bits in the constant
  105.    that don't belong in the type.  */
  106.  
  107. static void
  108. force_fit_type (t)
  109.      tree t;
  110. {
  111.   register int prec = TYPE_PRECISION (TREE_TYPE (t));
  112.  
  113.   if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
  114.     prec = POINTER_SIZE;
  115.  
  116.   /* First clear all bits that are beyond the type's precision.  */
  117.  
  118.   if (prec == 2 * HOST_BITS_PER_INT)
  119.     ;
  120.   else if (prec > HOST_BITS_PER_INT)
  121.     {
  122.       TREE_INT_CST_HIGH (t)
  123.     &= ~((-1) << (prec - HOST_BITS_PER_INT));
  124.     }
  125.   else
  126.     {
  127.       TREE_INT_CST_HIGH (t) = 0;
  128.       if (prec < HOST_BITS_PER_INT)
  129.     TREE_INT_CST_LOW (t)
  130.       &= ~((-1) << prec);
  131.     }
  132.  
  133.   /* If it's a signed type and value's sign bit is set, extend the sign.  */
  134.  
  135.   if (! TREE_UNSIGNED (TREE_TYPE (t))
  136.       && prec != 2 * HOST_BITS_PER_INT
  137.       && (prec > HOST_BITS_PER_INT
  138.       ? TREE_INT_CST_HIGH (t) & (1 << (prec - HOST_BITS_PER_INT - 1))
  139.       : TREE_INT_CST_LOW (t) & (1 << (prec - 1))))
  140.     {
  141.       /* Value is negative:
  142.      set to 1 all the bits that are outside this type's precision.  */
  143.       if (prec > HOST_BITS_PER_INT)
  144.     {
  145.       TREE_INT_CST_HIGH (t)
  146.         |= ((-1) << (prec - HOST_BITS_PER_INT));
  147.     }
  148.       else
  149.     {
  150.       TREE_INT_CST_HIGH (t) = -1;
  151.       if (prec < HOST_BITS_PER_INT)
  152.         TREE_INT_CST_LOW (t)
  153.           |= ((-1) << prec);
  154.     }
  155.     }
  156. }
  157.  
  158. /* Add two 64-bit integers with 64-bit result.
  159.    Each argument is given as two `int' pieces.
  160.    One argument is L1 and H1; the other, L2 and H2.
  161.    The value is stored as two `int' pieces in *LV and *HV.
  162.    We use the 8-shorts representation internally.  */
  163.  
  164. void
  165. add_double (l1, h1, l2, h2, lv, hv)
  166.      int l1, h1, l2, h2;
  167.      int *lv, *hv;
  168. {
  169.   short arg1[8];
  170.   short arg2[8];
  171.   register int carry = 0;
  172.   register int i;
  173.  
  174.   encode (arg1, l1, h1);
  175.   encode (arg2, l2, h2);
  176.  
  177.   for (i = 0; i < 8; i++)
  178.     {
  179.       carry += arg1[i] + arg2[i];
  180.       arg1[i] = carry & 0xff;
  181.       carry >>= 8;
  182.     }
  183.  
  184.   decode (arg1, lv, hv);
  185. }
  186.  
  187. /* Negate a 64-bit integers with 64-bit result.
  188.    The argument is given as two `int' pieces in L1 and H1.
  189.    The value is stored as two `int' pieces in *LV and *HV.
  190.    We use the 8-shorts representation internally.  */
  191.  
  192. static void
  193. neg_double (l1, h1, lv, hv)
  194.      int l1, h1;
  195.      int *lv, *hv;
  196. {
  197.   if (l1 == 0)
  198.     {
  199.       *lv = 0;
  200.       *hv = - h1;
  201.     }
  202.   else
  203.     {
  204.       *lv = - l1;
  205.       *hv = ~ h1;
  206.     }
  207. }
  208.  
  209. /* Multiply two 64-bit integers with 64-bit result.
  210.    Each argument is given as two `int' pieces.
  211.    One argument is L1 and H1; the other, L2 and H2.
  212.    The value is stored as two `int' pieces in *LV and *HV.
  213.    We use the 8-shorts representation internally.  */
  214.  
  215. static void
  216. mul_double (l1, h1, l2, h2, lv, hv)
  217.      int l1, h1, l2, h2;
  218.      int *lv, *hv;
  219. {
  220.   short arg1[8];
  221.   short arg2[8];
  222.   short prod[16];
  223.   register int carry = 0;
  224.   register int i, j, k;
  225.  
  226.   /* These two cases are used extensively, arising from pointer
  227.      combinations.  */
  228.   if (h2 == 0)
  229.     {
  230.       if (l2 == 2)
  231.     {
  232.       unsigned temp = l1 + l1;
  233.       *hv = h1 * 2 + (temp < l1);
  234.       *lv = temp;
  235.       return;
  236.     }
  237.       if (l2 == 4)
  238.     {
  239.       unsigned temp = l1 + l1;
  240.       h1 = h1 * 4 + ((temp < l1) << 1);
  241.       l1 = temp;
  242.       temp += temp;
  243.       h1 += (temp < l1);
  244.       *lv = temp;
  245.       *hv = h1;
  246.       return;
  247.     }
  248.       if (l2 == 8)
  249.     {
  250.       unsigned temp = l1 + l1;
  251.       h1 = h1 * 8 + ((temp < l1) << 2);
  252.       l1 = temp;
  253.       temp += temp;
  254.       h1 += (temp < l1) << 1;
  255.       l1 = temp;
  256.       temp += temp;
  257.       h1 += (temp < l1);
  258.       *lv = temp;
  259.       *hv = h1;
  260.       return;
  261.     }
  262.     }
  263.  
  264.   encode (arg1, l1, h1);
  265.   encode (arg2, l2, h2);
  266.  
  267.   bzero (prod, sizeof prod);
  268.  
  269.   for (i = 0; i < 8; i++)
  270.     for (j = 0; j < 8; j++)
  271.       {
  272.     k = i + j;
  273.     carry = arg1[i] * arg2[j];
  274.     while (carry)
  275.       {
  276.         carry += prod[k];
  277.         prod[k] = carry & 0xff;
  278.         carry >>= 8;
  279.         k++;
  280.       }
  281.       }
  282.  
  283.   decode (prod, lv, hv);    /* @@decode ignores prod[8] -> prod[15] */
  284. }
  285.  
  286. /* Shift the 64-bit integer in L1, H1 left by COUNT places
  287.    keeping only PREC bits of result.
  288.    Shift right if COUNT is negative.
  289.    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
  290.    Store the value as two `int' pieces in *LV and *HV.  */
  291.  
  292. static void
  293. lshift_double (l1, h1, count, prec, lv, hv, arith)
  294.      int l1, h1, count, prec;
  295.      int *lv, *hv;
  296.      int arith;
  297. {
  298.   short arg1[8];
  299.   register int i;
  300.   register int carry;
  301.  
  302.   if (count < 0)
  303.     {
  304.       rshift_double (l1, h1, - count, prec, lv, hv, arith);
  305.       return;
  306.     }
  307.  
  308.   encode (arg1, l1, h1);
  309.  
  310.   if (count > prec)
  311.     count = prec;
  312.  
  313.   while (count > 0)
  314.     {
  315.       carry = 0;
  316.       for (i = 0; i < 8; i++)
  317.     {
  318.       carry += arg1[i] << 1;
  319.       arg1[i] = carry & 0xff;
  320.       carry >>= 8;
  321.     }
  322.       count--;
  323.     }
  324.  
  325.   decode (arg1, lv, hv);
  326. }
  327.  
  328. /* Shift the 64-bit integer in L1, H1 right by COUNT places
  329.    keeping only PREC bits of result.  COUNT must be positive.
  330.    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
  331.    Store the value as two `int' pieces in *LV and *HV.  */
  332.  
  333. static void
  334. rshift_double (l1, h1, count, prec, lv, hv, arith)
  335.      int l1, h1, count, prec;
  336.      int *lv, *hv;
  337.      int arith;
  338. {
  339.   short arg1[8];
  340.   register int i;
  341.   register int carry;
  342.  
  343.   encode (arg1, l1, h1);
  344.  
  345.   if (count > prec)
  346.     count = prec;
  347.  
  348.   while (count > 0)
  349.     {
  350.       carry = arith && arg1[7] >> 7; 
  351.      for (i = 7; i >= 0; i--)
  352.     {
  353.       carry <<= 8;
  354.       carry += arg1[i];
  355.       arg1[i] = (carry >> 1) & 0xff;
  356.     }
  357.       count--;
  358.     }
  359.  
  360.   decode (arg1, lv, hv);
  361. }
  362.  
  363. /* Rotate the 64-bit integer in L1, H1 left by COUNT places
  364.    keeping only PREC bits of result.
  365.    Rotate right if COUNT is negative.
  366.    Store the value as two `int' pieces in *LV and *HV.  */
  367.  
  368. static void
  369. lrotate_double (l1, h1, count, prec, lv, hv)
  370.      int l1, h1, count, prec;
  371.      int *lv, *hv;
  372. {
  373.   short arg1[8];
  374.   register int i;
  375.   register int carry;
  376.  
  377.   if (count < 0)
  378.     {
  379.       rrotate_double (l1, h1, - count, prec, lv, hv);
  380.       return;
  381.     }
  382.  
  383.   encode (arg1, l1, h1);
  384.  
  385.   if (count > prec)
  386.     count = prec;
  387.  
  388.   carry = arg1[7] >> 7;
  389.   while (count > 0)
  390.     {
  391.       for (i = 0; i < 8; i++)
  392.     {
  393.       carry += arg1[i] << 1;
  394.       arg1[i] = carry & 0xff;
  395.       carry >>= 8;
  396.     }
  397.       count--;
  398.     }
  399.  
  400.   decode (arg1, lv, hv);
  401. }
  402.  
  403. /* Rotate the 64-bit integer in L1, H1 left by COUNT places
  404.    keeping only PREC bits of result.  COUNT must be positive.
  405.    Store the value as two `int' pieces in *LV and *HV.  */
  406.  
  407. static void
  408. rrotate_double (l1, h1, count, prec, lv, hv)
  409.      int l1, h1, count, prec;
  410.      int *lv, *hv;
  411. {
  412.   short arg1[8];
  413.   register int i;
  414.   register int carry;
  415.  
  416.   encode (arg1, l1, h1);
  417.  
  418.   if (count > prec)
  419.     count = prec;
  420.  
  421.   carry = arg1[0] & 1;
  422.   while (count > 0)
  423.     {
  424.       for (i = 7; i >= 0; i--)
  425.     {
  426.       carry <<= 8;
  427.       carry += arg1[i];
  428.       arg1[i] = (carry >> 1) & 0xff;
  429.     }
  430.       count--;
  431.     }
  432.  
  433.   decode (arg1, lv, hv);
  434. }
  435.  
  436. /* Divide 64 bit integer LNUM, HNUM by 64 bit integer LDEN, HDEN
  437.    for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
  438.    CODE is a tree code for a kind of division, one of
  439.    TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
  440.    or EXACT_DIV_EXPR
  441.    It controls how the quotient is rounded to a integer.
  442.    UNS nonzero says do unsigned division.  */
  443.  
  444. static void
  445. div_and_round_double (code, uns,
  446.               lnum_orig, hnum_orig, lden_orig, hden_orig,
  447.               lquo, hquo, lrem, hrem)
  448.      enum tree_code code;
  449.      int uns;
  450.      int lnum_orig, hnum_orig;        /* num == numerator == dividend */
  451.      int lden_orig, hden_orig;        /* den == denominator == divisor */
  452.      int *lquo, *hquo, *lrem, *hrem;
  453. {
  454.   int quo_neg = 0;
  455.   short num[9], den[8], quo[8];    /* extra element for scaling.  */
  456.   register int i, j, work;
  457.   register int carry = 0;
  458.   int lnum = lnum_orig, hnum = hnum_orig;
  459.   int lden = lden_orig, hden = hden_orig;
  460.  
  461.   if ((hden == 0) && (lden == 0))
  462.     abort ();
  463.  
  464.   /* calculate quotient sign and convert operands to unsigned.  */
  465.   if (!uns) 
  466.     {
  467.       if (hden < 0) 
  468.     {
  469.       quo_neg = ~ quo_neg;
  470.       neg_double (lden, hden, &lden, &hden);
  471.     }
  472.       if (hnum < 0)
  473.     {
  474.       quo_neg = ~ quo_neg;
  475.       neg_double (lnum, hnum, &lnum, &hnum);
  476.     }
  477.     }
  478.  
  479.   if (hnum == 0 && hden == 0)
  480.     {                /* single precision */
  481.       *hquo = *hrem = 0;
  482.       *lquo = (unsigned) lnum / lden;    /* rounds toward zero since positive args */
  483.       goto finish_up;
  484.     }
  485.  
  486.   if (hnum == 0)
  487.     {                /* trivial case: dividend < divisor */
  488.       /* hden != 0 already checked.  */
  489.       *hquo = *lquo = 0;
  490.       *hrem = hnum;
  491.       *lrem = lnum;
  492.       goto finish_up;
  493.     }
  494.  
  495.   bzero (quo, sizeof quo);
  496.  
  497.   bzero (num, sizeof num);    /* to zero 9th element */
  498.   bzero (den, sizeof den);
  499.  
  500.   encode (num, lnum, hnum); 
  501.   encode (den, lden, hden);
  502.  
  503.   if (hden == 0)
  504.     {                /* simpler algorithm */
  505.       /* hnum != 0 already checked.  */
  506.       for (i = 7; i >= 0; i--)
  507.     {
  508.       work = num[i] + (carry << 8);
  509.       quo[i] = work / lden;
  510.       carry = work % lden;
  511.     }
  512.     }
  513.   else {            /* full double precision,
  514.                    with thanks to Don Knuth's
  515.                    "Semi-Numericial Algorithms".  */
  516. #define BASE 256
  517.     int quo_est, scale, num_hi_sig, den_hi_sig, quo_hi_sig;
  518.  
  519.     /* Find the highest non-zero divisor digit.  */
  520.     for (i = 7; ; i--)
  521.       if (den[i] != 0) {
  522.     den_hi_sig = i;
  523.     break;
  524.       }
  525.     for (i = 7; ; i--)
  526.       if (num[i] != 0) {
  527.     num_hi_sig = i;
  528.     break;
  529.       }
  530.     quo_hi_sig = num_hi_sig - den_hi_sig + 1;
  531.  
  532.     /* Insure that the first digit of the divisor is at least BASE/2.
  533.        This is required by the quotient digit estimation algorithm.  */
  534.  
  535.     scale = BASE / (den[den_hi_sig] + 1);
  536.     if (scale > 1) {        /* scale divisor and dividend */
  537.       carry = 0;
  538.       for (i = 0; i <= 8; i++) {
  539.     work = (num[i] * scale) + carry;
  540.     num[i] = work & 0xff;
  541.     carry = work >> 8;
  542.     if (num[i] != 0) num_hi_sig = i;
  543.       }
  544.       carry = 0;
  545.       for (i = 0; i <= 7; i++) {
  546.     work = (den[i] * scale) + carry;
  547.     den[i] = work & 0xff;
  548.     carry = work >> 8;
  549.     if (den[i] != 0) den_hi_sig = i;
  550.       }
  551.     }
  552.  
  553.     /* Main loop */
  554.     for (i = quo_hi_sig; i > 0; i--) {
  555.       /* quess the next quotient digit, quo_est, by dividing the first
  556.      two remaining dividend digits by the high order quotient digit.
  557.      quo_est is never low and is at most 2 high.  */
  558.  
  559.       int num_hi;        /* index of highest remaining dividend digit */
  560.  
  561.       num_hi = i + den_hi_sig;
  562.  
  563.       work = (num[num_hi] * BASE) + (num_hi ? 0 : num[num_hi - 1]);
  564.       if (num[num_hi] != den[den_hi_sig]) {
  565.     quo_est = work / den[den_hi_sig];
  566.       }
  567.       else {
  568.     quo_est = BASE - 1;
  569.       }
  570.  
  571.       /* refine quo_est so it's usually correct, and at most one high.   */
  572.       while ((den[den_hi_sig - 1] * quo_est)
  573.          > (((work - (quo_est * den[den_hi_sig])) * BASE)
  574.          + ((num_hi - 1) ? 0 : num[num_hi - 2]))) {
  575.     quo_est--;
  576.       }
  577.  
  578.       /* try quo_est as the quotient digit, by multiplying the
  579.          divisor by quo_est and subtracting from the remaining dividend.  */
  580.  
  581.       carry = 0;
  582.  
  583.       for (j = 0; j <= den_hi_sig; j++) {
  584.     int digit;
  585.  
  586.     work = num[i + j] - (quo_est * den[j]) + carry;
  587.     digit = work & 0xff;
  588.     carry = work >> 8;
  589.     if (digit < 0) {
  590.       digit += BASE;
  591.       carry--;
  592.     }
  593.     num[i + j] = digit;
  594.       }
  595.  
  596.       /* if quo_est was high by one, then num[i] went negative and
  597.      we need to correct things.  */
  598.  
  599.       if (num[num_hi] < 0) {
  600.     quo_est--;
  601.     carry = 0;        /* add divisor back in */
  602.     for (j = 0; j <= den_hi_sig; j++) {
  603.       work = num[i + j] + den[j] + carry;
  604.       if (work > BASE) {
  605.         work -= BASE;
  606.         carry = 1;
  607.       }
  608.       else {
  609.         carry = 0;
  610.       }
  611.       num[i + j] = work;
  612.     }
  613.     num [num_hi] += carry;
  614.       }
  615.  
  616.       /* store the quotient digit.  */
  617.       quo[i - 1] = quo_est;
  618.     }
  619.   }
  620.  
  621.   decode (quo, lquo, hquo);
  622.  
  623.  finish_up:
  624.   /* if result is negative, make it so.  */
  625.   if (quo_neg)
  626.     neg_double (*lquo, *hquo, lquo, hquo);
  627.  
  628.   /* compute trial remainder:  rem = num - (quo * den)  */
  629.   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
  630.   neg_double (*lrem, *hrem, lrem, hrem);
  631.   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
  632.  
  633.   switch (code)
  634.     {
  635.     case TRUNC_DIV_EXPR:
  636.     case TRUNC_MOD_EXPR:    /* round toward zero */
  637.     case EXACT_DIV_EXPR:    /* for this one, it shouldn't matter */
  638.       return;
  639.  
  640.     case FLOOR_DIV_EXPR:
  641.     case FLOOR_MOD_EXPR:    /* round toward negative infinity */
  642.       if (quo_neg && (*lrem != 0 || *hrem != 0))   /* ratio < 0 && rem != 0 */
  643.     {
  644.       /* quo = quo - 1;  */
  645.       add_double (*lquo, *hquo, -1, -1, lquo, hquo);
  646.     }
  647.       else return;
  648.       break;
  649.  
  650.     case CEIL_DIV_EXPR:
  651.     case CEIL_MOD_EXPR:        /* round toward positive infinity */
  652.       if (!quo_neg && (*lrem != 0 || *hrem != 0))  /* ratio > 0 && rem != 0 */
  653.     {
  654.       add_double (*lquo, *hquo, 1, 0, lquo, hquo);
  655.     }
  656.       else return;
  657.       break;
  658.     
  659.     case ROUND_DIV_EXPR:
  660.     case ROUND_MOD_EXPR:    /* round to closest integer */
  661.       {
  662.     int labs_rem = *lrem, habs_rem = *hrem;
  663.     int labs_den = lden, habs_den = hden, ltwice, htwice;
  664.  
  665.     /* get absolute values */
  666.     if (*hrem < 0) neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
  667.     if (hden < 0) neg_double (lden, hden, &labs_den, &habs_den);
  668.  
  669.     /* if (2 * abs (lrem) >= abs (lden)) */
  670.     mul_double (2, 0, labs_rem, habs_rem, <wice, &htwice);
  671.     if (((unsigned) habs_den < (unsigned) htwice)
  672.         || (((unsigned) habs_den == (unsigned) htwice)
  673.         && ((unsigned) labs_den < (unsigned) ltwice)))
  674.       {
  675.         if (*hquo < 0)
  676.           /* quo = quo - 1;  */
  677.           add_double (*lquo, *hquo, -1, -1, lquo, hquo);
  678.         else
  679.           /* quo = quo + 1; */
  680.           add_double (*lquo, *hquo, 1, 0, lquo, hquo);
  681.       }
  682.     else return;
  683.       }
  684.       break;
  685.  
  686.     default:
  687.       abort ();
  688.     }
  689.  
  690.   /* compute true remainder:  rem = num - (quo * den)  */
  691.   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
  692.   neg_double (*lrem, *hrem, lrem, hrem);
  693.   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
  694. }
  695.  
  696. #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
  697.  
  698. /* Check for infinity in an IEEE double precision number.  */
  699.  
  700. int
  701. target_isinf (x)
  702.      REAL_VALUE_TYPE x;
  703. {
  704.   /* The IEEE 64-bit double format.  */
  705.   union {
  706.     REAL_VALUE_TYPE d;
  707.     struct {
  708.       unsigned sign      :  1;
  709.       unsigned exponent  : 11;
  710.       unsigned mantissa1 : 20;
  711.       unsigned mantissa2;
  712.     } little_endian;
  713.     struct {
  714.       unsigned mantissa2;
  715.       unsigned mantissa1 : 20;
  716.       unsigned exponent  : 11;
  717.       unsigned sign      :  1;
  718.     } big_endian;    
  719.   } u;
  720.  
  721.   u.d = dconstm1;
  722.   if (u.big_endian.sign == 1)
  723.     {
  724.       u.d = x;
  725.       return (u.big_endian.exponent == 2047
  726.           && u.big_endian.mantissa1 == 0
  727.           && u.big_endian.mantissa2 == 0);
  728.     }
  729.   else
  730.     {
  731.       u.d = x;
  732.       return (u.little_endian.exponent == 2047
  733.           && u.little_endian.mantissa1 == 0
  734.           && u.little_endian.mantissa2 == 0);
  735.     }
  736. }
  737. #else /* Target not IEEE */
  738.  
  739. /* Let's assume other float formats don't have infinity.
  740.    (This can be overridden by redefining REAL_VALUE_ISINF.)  */
  741.  
  742. target_isinf (x)
  743.      REAL_VALUE_TYPE x;
  744. {
  745.   return 0;
  746. }
  747. #endif /* Target not IEEE */
  748.  
  749. /* Split a tree IN into a constant and a variable part
  750.    that could be combined with CODE to make IN.
  751.    CODE must be a commutative arithmetic operation.
  752.    Store the constant part into *CONP and the variable in &VARP.
  753.    Return 1 if this was done; zero means the tree IN did not decompose
  754.    this way.
  755.  
  756.    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.
  757.    Therefore, we must tell the caller whether the variable part
  758.    was subtracted.  We do this by storing 1 or -1 into *VARSIGNP.
  759.    The value stored is the coefficient for the variable term.
  760.    The constant term we return should always be added;
  761.    we negate it if necessary.  */
  762.  
  763. static int
  764. split_tree (in, code, varp, conp, varsignp)
  765.      tree in;
  766.      enum tree_code code;
  767.      tree *varp, *conp;
  768.      int *varsignp;
  769. {
  770.   register tree outtype = TREE_TYPE (in);
  771.   *varp = 0;
  772.   *conp = 0;
  773.  
  774.   /* Strip any conversions that don't change the machine mode.  */
  775.   while ((TREE_CODE (in) == NOP_EXPR
  776.       || TREE_CODE (in) == CONVERT_EXPR)
  777.      && (TYPE_MODE (TREE_TYPE (in))
  778.          == TYPE_MODE (TREE_TYPE (TREE_OPERAND (in, 0)))))
  779.     in = TREE_OPERAND (in, 0);
  780.  
  781.   if (TREE_CODE (in) == code
  782.       || (TREE_CODE (TREE_TYPE (in)) != REAL_TYPE
  783.       /* We can associate addition and subtraction together
  784.          (even though the C standard doesn't say so)
  785.          for integers because the value is not affected.
  786.          For reals, the value might be affected, so we can't.  */
  787.       &&
  788.       ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
  789.        || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
  790.     {
  791.       enum tree_code code = TREE_CODE (TREE_OPERAND (in, 0));
  792.       if (code == INTEGER_CST)
  793.     {
  794.       *conp = TREE_OPERAND (in, 0);
  795.       *varp = TREE_OPERAND (in, 1);
  796.       if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
  797.           && TREE_TYPE (*varp) != outtype)
  798.         *varp = convert (outtype, *varp);
  799.       *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
  800.       return 1;
  801.     }
  802.       if (TREE_CONSTANT (TREE_OPERAND (in, 1)))
  803.     {
  804.       *conp = TREE_OPERAND (in, 1);
  805.       *varp = TREE_OPERAND (in, 0);
  806.       *varsignp = 1;
  807.       if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
  808.           && TREE_TYPE (*varp) != outtype)
  809.         *varp = convert (outtype, *varp);
  810.       if (TREE_CODE (in) == MINUS_EXPR)
  811.         {
  812.           /* If operation is subtraction and constant is second,
  813.          must negate it to get an additive constant.
  814.          And this cannot be done unless it is a manifest constant.
  815.          It could also be the address of a static variable.
  816.          We cannot negate that, so give up.  */
  817.           if (TREE_CODE (*conp) == INTEGER_CST)
  818.         /* Subtracting from integer_zero_node loses for long long.  */
  819.         *conp = fold (build1 (NEGATE_EXPR, TREE_TYPE (*conp), *conp));
  820.           else
  821.         return 0;
  822.         }
  823.       return 1;
  824.     }
  825.       if (TREE_CONSTANT (TREE_OPERAND (in, 0)))
  826.     {
  827.       *conp = TREE_OPERAND (in, 0);
  828.       *varp = TREE_OPERAND (in, 1);
  829.       if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
  830.           && TREE_TYPE (*varp) != outtype)
  831.         *varp = convert (outtype, *varp);
  832.       *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
  833.       return 1;
  834.     }
  835.     }
  836.   return 0;
  837. }
  838.  
  839. /* Combine two constants NUM and ARG2 under operation CODE
  840.    to produce a new constant.
  841.    We assume ARG1 and ARG2 have the same data type,
  842.    or at least are the same kind of constant and the same machine mode.  */
  843.  
  844. /* Handle floating overflow for `const_binop'.  */
  845. static jmp_buf const_binop_error;
  846.  
  847. static tree
  848. const_binop (code, arg1, arg2)
  849.      enum tree_code code;
  850.      register tree arg1, arg2;
  851. {
  852.   if (TREE_CODE (arg1) == INTEGER_CST)
  853.     {
  854.       register int int1l = TREE_INT_CST_LOW (arg1);
  855.       register int int1h = TREE_INT_CST_HIGH (arg1);
  856.       int int2l = TREE_INT_CST_LOW (arg2);
  857.       int int2h = TREE_INT_CST_HIGH (arg2);
  858.       int low, hi;
  859.       int garbagel, garbageh;
  860.       register tree t;
  861.       int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
  862.  
  863.       switch (code)
  864.     {
  865.     case BIT_IOR_EXPR:
  866.       t = build_int_2 (int1l | int2l, int1h | int2h);
  867.       break;
  868.  
  869.     case BIT_XOR_EXPR:
  870.       t = build_int_2 (int1l ^ int2l, int1h ^ int2h);
  871.       break;
  872.  
  873.     case BIT_AND_EXPR:
  874.       t = build_int_2 (int1l & int2l, int1h & int2h);
  875.       break;
  876.  
  877.     case BIT_ANDTC_EXPR:
  878.       t = build_int_2 (int1l & ~int2l, int1h & ~int2h);
  879.       break;
  880.  
  881.     case RSHIFT_EXPR:
  882.       int2l = - int2l;
  883.     case LSHIFT_EXPR:
  884.       lshift_double (int1l, int1h, int2l,
  885.              TYPE_PRECISION (TREE_TYPE (arg1)),
  886.              &low, &hi,
  887.              !uns);
  888.       t = build_int_2 (low, hi);
  889.       break;
  890.  
  891.     case RROTATE_EXPR:
  892.       int2l = - int2l;
  893.     case LROTATE_EXPR:
  894.       lrotate_double (int1l, int1h, int2l,
  895.               TYPE_PRECISION (TREE_TYPE (arg1)),
  896.               &low, &hi);
  897.       t = build_int_2 (low, hi);
  898.       break;
  899.  
  900.     case PLUS_EXPR:
  901.       if (int1h == 0)
  902.         {
  903.           int2l += int1l;
  904.           if ((unsigned) int2l < int1l)
  905.         int2h += 1;
  906.           t = build_int_2 (int2l, int2h);
  907.           break;
  908.         }
  909.       if (int2h == 0)
  910.         {
  911.           int1l += int2l;
  912.           if ((unsigned) int1l < int2l)
  913.         int1h += 1;
  914.           t = build_int_2 (int1l, int1h);
  915.           break;
  916.         }
  917.       add_double (int1l, int1h, int2l, int2h, &low, &hi);
  918.       t = build_int_2 (low, hi);
  919.       break;
  920.  
  921.     case MINUS_EXPR:
  922.       if (int1h == 0 && int1l == 0)
  923.         {
  924.           t = build_int_2 (- int2l, - int2h);
  925.           break;
  926.         }
  927.       if (int2h == 0 && int2l == 0)
  928.         {
  929.           t = build_int_2 (int1l, int1h);
  930.           break;
  931.         }
  932.       neg_double (int2l, int2h, &int2l, &int2h);
  933.       add_double (int1l, int1h, int2l, int2h, &low, &hi);
  934.       t = build_int_2 (low, hi);
  935.       break;
  936.  
  937.     case MULT_EXPR:
  938.   /* Optimize simple cases.  */
  939.       if (int1h == 0)
  940.         {
  941.           unsigned temp;
  942.  
  943.           switch (int1l)
  944.         {
  945.         case 0:
  946.           t = build_int_2 (0, 0);
  947.           goto got_it;
  948.         case 1:
  949.           t = build_int_2 (int2l, int2h);
  950.           goto got_it;
  951.         case 2:
  952.           temp = int2l + int2l;
  953.           int2h = int2h * 2 + (temp < int2l);
  954.           t = build_int_2 (temp, int2h);
  955.           goto got_it;
  956.         case 3:
  957.           temp = int2l + int2l + int2l;
  958.           int2h = int2h * 3 + (temp < int2l);
  959.           t = build_int_2 (temp, int2h);
  960.           goto got_it;
  961.         case 4:
  962.           temp = int2l + int2l;
  963.           int2h = int2h * 4 + ((temp < int2l) << 1);
  964.           int2l = temp;
  965.           temp += temp;
  966.           int2h += (temp < int2l);
  967.           t = build_int_2 (temp, int2h);
  968.           goto got_it;
  969.         case 8:
  970.           temp = int2l + int2l;
  971.           int2h = int2h * 8 + ((temp < int2l) << 2);
  972.           int2l = temp;
  973.           temp += temp;
  974.           int2h += (temp < int2l) << 1;
  975.           int2l = temp;
  976.           temp += temp;
  977.           int2h += (temp < int2l);
  978.           t = build_int_2 (temp, int2h);
  979.           goto got_it;
  980.         default:
  981.           break;
  982.         }
  983.         }
  984.  
  985.       if (int2h == 0)
  986.         {
  987.           if (int2l == 0)
  988.         {
  989.           t = build_int_2 (0, 0);
  990.           break;
  991.         }
  992.           if (int2l == 1)
  993.         {
  994.           t = build_int_2 (int1l, int1h);
  995.           break;
  996.         }
  997.         }
  998.  
  999.       mul_double (int1l, int1h, int2l, int2h, &low, &hi);
  1000.       t = build_int_2 (low, hi);
  1001.       break;
  1002.  
  1003.     case TRUNC_DIV_EXPR: case ROUND_DIV_EXPR: 
  1004.     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
  1005.     case EXACT_DIV_EXPR:
  1006.       if (int2h == 0 && int2l == 1)
  1007.         {
  1008.           t = build_int_2 (int1l, int1h);
  1009.           break;
  1010.         }
  1011.       if (int1l == int2l && int1h == int2h)
  1012.         {
  1013.           if ((int1l | int1h) == 0)
  1014.         abort ();
  1015.           t = build_int_2 (1, 0);
  1016.           break;
  1017.         }
  1018.       div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
  1019.                 &low, &hi, &garbagel, &garbageh);
  1020.       t = build_int_2 (low, hi);
  1021.       break;
  1022.  
  1023.     case TRUNC_MOD_EXPR: case ROUND_MOD_EXPR: 
  1024.     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
  1025.       div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
  1026.                 &garbagel, &garbageh, &low, &hi);
  1027.       t = build_int_2 (low, hi);
  1028.       break;
  1029.  
  1030.     case MIN_EXPR:
  1031.     case MAX_EXPR:
  1032.       if (uns)
  1033.         {
  1034.           low = (((unsigned) int1h < (unsigned) int2h)
  1035.              || (((unsigned) int1h == (unsigned) int2h)
  1036.              && ((unsigned) int1l < (unsigned) int2l)));
  1037.         }
  1038.       else
  1039.         {
  1040.           low = ((int1h < int2h)
  1041.              || ((int1h == int2h)
  1042.              && ((unsigned) int1l < (unsigned) int2l)));
  1043.         }
  1044.       if (low == (code == MIN_EXPR))
  1045.         t = build_int_2 (int1l, int1h);
  1046.       else
  1047.         t = build_int_2 (int2l, int2h);
  1048.       break;
  1049.  
  1050.     default:
  1051.       abort ();
  1052.     }
  1053.     got_it:
  1054.       TREE_TYPE (t) = TREE_TYPE (arg1);
  1055.       force_fit_type (t);
  1056.       return t;
  1057.     }
  1058. #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  1059.   if (TREE_CODE (arg1) == REAL_CST)
  1060.     {
  1061.       register REAL_VALUE_TYPE d1;
  1062.       register REAL_VALUE_TYPE d2;
  1063.       register REAL_VALUE_TYPE value;
  1064.  
  1065.       d1 = TREE_REAL_CST (arg1);
  1066.       d2 = TREE_REAL_CST (arg2);
  1067.       if (setjmp (const_binop_error))
  1068.     {
  1069.       warning ("floating overflow in constant folding");
  1070.       return build (code, TREE_TYPE (arg1), arg1, arg2);
  1071.     }
  1072.       set_float_handler (const_binop_error);
  1073.  
  1074. #ifdef REAL_ARITHMETIC
  1075.       REAL_ARITHMETIC (value, code, d1, d2);
  1076. #else
  1077.       switch (code)
  1078.     {
  1079.     case PLUS_EXPR:
  1080.       value = d1 + d2;
  1081.       break;
  1082.  
  1083.     case MINUS_EXPR:
  1084.       value = d1 - d2;
  1085.       break;
  1086.  
  1087.     case MULT_EXPR:
  1088.       value = d1 * d2;
  1089.       break;
  1090.  
  1091.     case RDIV_EXPR:
  1092. #ifndef REAL_INFINITY
  1093.       if (d2 == 0)
  1094.         abort ();
  1095. #endif
  1096.  
  1097.       value = d1 / d2;
  1098.       break;
  1099.  
  1100.     case MIN_EXPR:
  1101.       value = d1 < d2 ? d1 : d2;
  1102.       break;
  1103.  
  1104.     case MAX_EXPR:
  1105.       value = d1 > d2 ? d1 : d2;
  1106.       break;
  1107.  
  1108.     default:
  1109.       abort ();
  1110.     }
  1111. #endif /* no REAL_ARITHMETIC */
  1112.       set_float_handler (0);
  1113.       return build_real (TREE_TYPE (arg1), value);
  1114.     }
  1115. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  1116.   if (TREE_CODE (arg1) == COMPLEX_CST)
  1117.     {
  1118.       register tree r1 = TREE_REALPART (arg1);
  1119.       register tree i1 = TREE_IMAGPART (arg1);
  1120.       register tree r2 = TREE_REALPART (arg2);
  1121.       register tree i2 = TREE_IMAGPART (arg2);
  1122.       register tree t;
  1123.  
  1124.       switch (code)
  1125.     {
  1126.     case PLUS_EXPR:
  1127.       t = build_complex (const_binop (PLUS_EXPR, r1, r2),
  1128.                  const_binop (PLUS_EXPR, i1, i2));
  1129.       break;
  1130.  
  1131.     case MINUS_EXPR:
  1132.       t = build_complex (const_binop (MINUS_EXPR, r1, r2),
  1133.                  const_binop (MINUS_EXPR, i1, i2));
  1134.       break;
  1135.  
  1136.     case MULT_EXPR:
  1137.       t = build_complex (const_binop (MINUS_EXPR,
  1138.                       const_binop (MULT_EXPR, r1, r2),
  1139.                       const_binop (MULT_EXPR, i1, i2)),
  1140.                  const_binop (PLUS_EXPR,
  1141.                       const_binop (MULT_EXPR, r1, i2),
  1142.                       const_binop (MULT_EXPR, i1, r2)));
  1143.       break;
  1144.  
  1145.     case RDIV_EXPR:
  1146.       {
  1147.         register tree magsquared
  1148.           = const_binop (PLUS_EXPR,
  1149.                  const_binop (MULT_EXPR, r2, r2),
  1150.                  const_binop (MULT_EXPR, i2, i2));
  1151.         t = build_complex (const_binop (RDIV_EXPR,
  1152.                         const_binop (PLUS_EXPR,
  1153.                              const_binop (MULT_EXPR, r1, r2),
  1154.                              const_binop (MULT_EXPR, i1, i2)),
  1155.                         magsquared),
  1156.                    const_binop (RDIV_EXPR,
  1157.                         const_binop (MINUS_EXPR,
  1158.                              const_binop (MULT_EXPR, i1, r2),
  1159.                              const_binop (MULT_EXPR, r1, i2)),
  1160.                         magsquared));
  1161.       }
  1162.       break;
  1163.  
  1164.     default:
  1165.       abort ();
  1166.     }
  1167.       TREE_TYPE (t) = TREE_TYPE (arg1);
  1168.       return t;
  1169.     }
  1170.   return 0;
  1171. }
  1172.  
  1173. /* Return an INTEGER_CST with value V and type from `sizetype'.  */
  1174.  
  1175. tree
  1176. size_int (number)
  1177.      unsigned int number;
  1178. {
  1179.   register tree t;
  1180.   /* Type-size nodes already made for small sizes.  */
  1181.   static tree size_table[HOST_BITS_PER_INT];
  1182.  
  1183.   if (number >= 0 && number < HOST_BITS_PER_INT && size_table[number] != 0)
  1184.     return size_table[number];
  1185.   if (number >= 0 && number < HOST_BITS_PER_INT)
  1186.     {
  1187.       int temp = allocation_temporary_p ();
  1188.       /* Make this a permanent node.  */
  1189.       if (temp)
  1190.     end_temporary_allocation ();
  1191.       t = build_int_2 (number, 0);
  1192.       TREE_TYPE (t) = sizetype;
  1193.       size_table[number] = t;
  1194.       if (temp)
  1195.     resume_temporary_allocation ();
  1196.     }
  1197.   else
  1198.     {
  1199.       t = build_int_2 (number, 0);
  1200.       TREE_TYPE (t) = sizetype;
  1201.     }
  1202.   return t;
  1203. }
  1204.  
  1205. /* Combine operands OP1 and OP2 with arithmetic operation CODE.
  1206.    CODE is a tree code.  Data type is taken from `sizetype',
  1207.    If the operands are constant, so is the result.  */
  1208.  
  1209. tree
  1210. size_binop (code, arg0, arg1)
  1211.      enum tree_code code;
  1212.      tree arg0, arg1;
  1213. {
  1214.   /* Handle the special case of two integer constants faster.  */
  1215.   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
  1216.     {
  1217.       /* And some specific cases even faster than that.  */
  1218.       if (code == PLUS_EXPR
  1219.       && TREE_INT_CST_LOW (arg0) == 0
  1220.       && TREE_INT_CST_HIGH (arg0) == 0)
  1221.     return arg1;
  1222.       if (code == MINUS_EXPR
  1223.       && TREE_INT_CST_LOW (arg1) == 0
  1224.       && TREE_INT_CST_HIGH (arg1) == 0)
  1225.     return arg0;
  1226.       if (code == MULT_EXPR
  1227.       && TREE_INT_CST_LOW (arg0) == 1
  1228.       && TREE_INT_CST_HIGH (arg0) == 0)
  1229.     return arg1;
  1230.       /* Handle general case of two integer constants.  */
  1231.       return const_binop (code, arg0, arg1);
  1232.     }
  1233.  
  1234.   if (arg0 == error_mark_node || arg1 == error_mark_node)
  1235.     return error_mark_node;
  1236.  
  1237.   return fold (build (code, sizetype, arg0, arg1));
  1238. }
  1239.  
  1240. /* Given T, a tree representing type conversion of ARG1, a constant,
  1241.    return a constant tree representing the result of conversion.  */
  1242.  
  1243. static tree
  1244. fold_convert (t, arg1)
  1245.      register tree t;
  1246.      register tree arg1;
  1247. {
  1248.   register tree type = TREE_TYPE (t);
  1249.  
  1250.   if (TREE_CODE (type) == POINTER_TYPE
  1251.       || TREE_CODE (type) == INTEGER_TYPE
  1252.       || TREE_CODE (type) == ENUMERAL_TYPE)
  1253.     {
  1254.       if (TREE_CODE (arg1) == INTEGER_CST)
  1255.     {
  1256.       /* Given an integer constant, make new constant with new type,
  1257.          appropriately sign-extended or truncated.  */
  1258.       t = build_int_2 (TREE_INT_CST_LOW (arg1),
  1259.                TREE_INT_CST_HIGH (arg1));
  1260.       TREE_TYPE (t) = type;
  1261.       force_fit_type (t);
  1262.     }
  1263. #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  1264.       else if (TREE_CODE (arg1) == REAL_CST)
  1265.     {
  1266.       if (REAL_VALUES_LESS (real_value_from_int_cst (TYPE_MAX_VALUE (type)),
  1267.                 TREE_REAL_CST (arg1))
  1268.           || REAL_VALUES_LESS (TREE_REAL_CST (arg1),
  1269.                    real_value_from_int_cst (TYPE_MIN_VALUE (type))))
  1270.         {
  1271.           warning ("real constant out of range for integer conversion");
  1272.           return t;
  1273.         }
  1274. #ifndef REAL_ARITHMETIC
  1275.       {
  1276.         REAL_VALUE_TYPE d;
  1277.         int low, high;
  1278.         int half_word = 1 << (HOST_BITS_PER_INT / 2);
  1279.  
  1280.         d = TREE_REAL_CST (arg1);
  1281.         if (d < 0)
  1282.           d = -d;
  1283.  
  1284.         high = (int) (d / half_word / half_word);
  1285.         d -= (REAL_VALUE_TYPE) high * half_word * half_word;
  1286.         low = (unsigned) d;
  1287.         if (TREE_REAL_CST (arg1) < 0)
  1288.           neg_double (low, high, &low, &high);
  1289.         t = build_int_2 (low, high);
  1290.       }
  1291. #else
  1292.       {
  1293.         int low, high;
  1294.         REAL_VALUE_TO_INT (low, high, TREE_REAL_CST (arg1));
  1295.         t = build_int_2 (low, high);
  1296.       }
  1297. #endif
  1298.       TREE_TYPE (t) = type;
  1299.       force_fit_type (t);
  1300.     }
  1301. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  1302.       TREE_TYPE (t) = type;
  1303.     }
  1304.   else if (TREE_CODE (type) == REAL_TYPE)
  1305.     {
  1306. #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  1307.       if (TREE_CODE (arg1) == INTEGER_CST)
  1308.     return build_real_from_int_cst (type, arg1);
  1309. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  1310.       if (TREE_CODE (arg1) == REAL_CST)
  1311.     return build_real (type, TREE_REAL_CST (arg1));
  1312.     }
  1313.   TREE_CONSTANT (t) = 1;
  1314.   return t;
  1315. }
  1316.  
  1317. /* Return an expr equal to X but certainly not valid as an lvalue.  */
  1318.  
  1319. tree
  1320. non_lvalue (x)
  1321.      tree x;
  1322. {
  1323.   tree result;
  1324.  
  1325.   /* These things are certainly not lvalues.  */
  1326.   if (TREE_CODE (x) == NON_LVALUE_EXPR
  1327.       || TREE_CODE (x) == INTEGER_CST
  1328.       || TREE_CODE (x) == REAL_CST
  1329.       || TREE_CODE (x) == STRING_CST
  1330.       || TREE_CODE (x) == ADDR_EXPR)
  1331.     return x;
  1332.  
  1333.   result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
  1334.   TREE_CONSTANT (result) = TREE_CONSTANT (x);
  1335.   return result;
  1336. }
  1337.  
  1338. /* Return nonzero if two operands are necessarily equal. 
  1339.    If ONLY_CONST is non-zero, only return non-zero for constants.  */
  1340.  
  1341. int
  1342. operand_equal_p (arg0, arg1, only_const)
  1343.      tree arg0, arg1;
  1344.      int only_const;
  1345. {
  1346.   STRIP_NOPS (arg0);
  1347.   STRIP_NOPS (arg1);
  1348.  
  1349.   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
  1350.      We don't care about side effects in that case because the SAVE_EXPR
  1351.      takes care of that for us.  */
  1352.   if (TREE_CODE (arg0) == SAVE_EXPR && arg0 == arg1)
  1353.     return ! only_const;
  1354.  
  1355.   if (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1))
  1356.     return 0;
  1357.  
  1358.   if (TREE_CODE (arg0) == TREE_CODE (arg1)
  1359.       && TREE_CODE (arg0) == ADDR_EXPR
  1360.       && TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0))
  1361.     return 1;
  1362.  
  1363.   if (TREE_CODE (arg0) == TREE_CODE (arg1)
  1364.       && TREE_CODE (arg0) == INTEGER_CST
  1365.       && TREE_INT_CST_LOW (arg0) == TREE_INT_CST_LOW (arg1)
  1366.       && TREE_INT_CST_HIGH (arg0) == TREE_INT_CST_HIGH (arg1))
  1367.     return 1;
  1368.  
  1369.   if (TREE_CODE (arg0) == TREE_CODE (arg1)
  1370.       && TREE_CODE (arg0) == REAL_CST
  1371.       && REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), TREE_REAL_CST (arg1)))
  1372.     return 1;
  1373.  
  1374.   if (only_const)
  1375.     return 0;
  1376.  
  1377.   if (arg0 == arg1)
  1378.     return 1;
  1379.  
  1380.   if (TREE_CODE (arg0) != TREE_CODE (arg1))
  1381.     return 0;
  1382.  
  1383.   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
  1384.     {
  1385.     case '1':
  1386.       return operand_equal_p (TREE_OPERAND (arg0, 0),
  1387.                   TREE_OPERAND (arg1, 0), 0);
  1388.  
  1389.     case '<':
  1390.     case '2':
  1391.       return (operand_equal_p (TREE_OPERAND (arg0, 0),
  1392.                    TREE_OPERAND (arg1, 0), 0)
  1393.           && operand_equal_p (TREE_OPERAND (arg0, 1),
  1394.                   TREE_OPERAND (arg1, 1), 0));
  1395.  
  1396.     case 'r':
  1397.       switch (TREE_CODE (arg0))
  1398.     {
  1399.     case INDIRECT_REF:
  1400.       return operand_equal_p (TREE_OPERAND (arg0, 0),
  1401.                   TREE_OPERAND (arg1, 0), 0);
  1402.  
  1403.     case COMPONENT_REF:
  1404.     case ARRAY_REF:
  1405.       return (operand_equal_p (TREE_OPERAND (arg0, 0),
  1406.                    TREE_OPERAND (arg1, 0), 0)
  1407.           && operand_equal_p (TREE_OPERAND (arg0, 1),
  1408.                       TREE_OPERAND (arg1, 1), 0));
  1409.  
  1410.     case BIT_FIELD_REF:
  1411.       return (operand_equal_p (TREE_OPERAND (arg0, 0),
  1412.                    TREE_OPERAND (arg1, 0), 0)
  1413.           && operand_equal_p (TREE_OPERAND (arg0, 1),
  1414.                       TREE_OPERAND (arg1, 1), 0)
  1415.           && operand_equal_p (TREE_OPERAND (arg0, 2),
  1416.                       TREE_OPERAND (arg1, 2), 0));
  1417.     }
  1418.       break;
  1419.     }
  1420.  
  1421.   return 0;
  1422. }
  1423.  
  1424. /* Return a tree for the case when the result of an expression is RESULT
  1425.    converted to TYPE and OMITTED was previously an operand of the expression
  1426.    but is now not needed (e.g., we folded OMITTED * 0).
  1427.  
  1428.    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
  1429.    the conversion of RESULT to TYPE.  */
  1430.  
  1431. static tree
  1432. omit_one_operand (type, result, omitted)
  1433.      tree type, result, omitted;
  1434. {
  1435.   tree t = convert (type, result);
  1436.  
  1437.   if (TREE_SIDE_EFFECTS (omitted))
  1438.     return build (COMPOUND_EXPR, type, omitted, t);
  1439.  
  1440.   return t;
  1441. }
  1442.  
  1443. /* Return a simplified tree node for the truth-negation of ARG
  1444.    (perhaps by altering ARG).  It is known that ARG is an operation that
  1445.    returns a truth value (0 or 1).  */
  1446.  
  1447. tree
  1448. invert_truthvalue (arg)
  1449.      tree arg;
  1450. {
  1451.   tree type = TREE_TYPE (arg);
  1452.  
  1453.   /* For floating-point comparisons, it isn't safe to invert the condition.
  1454.      So just enclose a TRUTH_NOT_EXPR around what we have.  */
  1455.   if (TREE_CODE (type) == REAL_TYPE
  1456.       && TREE_CODE_CLASS (TREE_CODE (arg)) == '<')
  1457.     return build1 (TRUTH_NOT_EXPR, type, arg);
  1458.  
  1459.   switch (TREE_CODE (arg))
  1460.     {
  1461.     case NE_EXPR:
  1462.       TREE_SET_CODE (arg, EQ_EXPR);
  1463.       return arg;
  1464.  
  1465.     case EQ_EXPR:
  1466.       TREE_SET_CODE (arg, NE_EXPR);
  1467.       return arg;
  1468.  
  1469.     case GE_EXPR:
  1470.       TREE_SET_CODE (arg, LT_EXPR);
  1471.       return arg;
  1472.  
  1473.     case GT_EXPR:
  1474.       TREE_SET_CODE (arg, LE_EXPR);
  1475.       return arg;
  1476.  
  1477.     case LE_EXPR:
  1478.       TREE_SET_CODE (arg, GT_EXPR);
  1479.       return arg;
  1480.  
  1481.     case LT_EXPR:
  1482.       TREE_SET_CODE (arg, GE_EXPR);
  1483.       return arg;
  1484.  
  1485.     case INTEGER_CST:
  1486.       return convert (type, build_int_2 (TREE_INT_CST_LOW (arg) == 0
  1487.                      && TREE_INT_CST_HIGH (arg) == 0, 0));
  1488.  
  1489.     case TRUTH_AND_EXPR:
  1490.       return build (TRUTH_OR_EXPR, type,
  1491.             invert_truthvalue (TREE_OPERAND (arg, 0)),
  1492.             invert_truthvalue (TREE_OPERAND (arg, 1)));
  1493.  
  1494.     case TRUTH_OR_EXPR:
  1495.       return build (TRUTH_AND_EXPR, type,
  1496.             invert_truthvalue (TREE_OPERAND (arg, 0)),
  1497.             invert_truthvalue (TREE_OPERAND (arg, 1)));
  1498.  
  1499.     case TRUTH_ANDIF_EXPR:
  1500.       return build (TRUTH_ORIF_EXPR, type,
  1501.             invert_truthvalue (TREE_OPERAND (arg, 0)),
  1502.             invert_truthvalue (TREE_OPERAND (arg, 1)));
  1503.  
  1504.     case TRUTH_ORIF_EXPR:
  1505.       return build (TRUTH_ANDIF_EXPR, type,
  1506.             invert_truthvalue (TREE_OPERAND (arg, 0)),
  1507.             invert_truthvalue (TREE_OPERAND (arg, 1)));
  1508.  
  1509.     case TRUTH_NOT_EXPR:
  1510.       return TREE_OPERAND (arg, 0);
  1511.  
  1512.     case COND_EXPR:
  1513.       return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
  1514.             invert_truthvalue (TREE_OPERAND (arg, 1)),
  1515.             invert_truthvalue (TREE_OPERAND (arg, 2)));
  1516.  
  1517.     case NON_LVALUE_EXPR:
  1518.       return invert_truthvalue (TREE_OPERAND (arg, 0));
  1519.  
  1520.     case NOP_EXPR:
  1521.     case CONVERT_EXPR:
  1522.     case FLOAT_EXPR:
  1523.       return build1 (TREE_CODE (arg), type,
  1524.              invert_truthvalue (TREE_OPERAND (arg, 0)));
  1525.  
  1526.     case BIT_AND_EXPR:
  1527.       if (! integer_onep (TREE_OPERAND (arg, 1)))
  1528.     abort ();
  1529.       return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
  1530.     }
  1531.  
  1532.   abort ();
  1533. }
  1534.  
  1535. /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
  1536.    operands are another bit-wise operation with a common input.  If so,
  1537.    distribute the bit operations to save an operation and possibly two if
  1538.    constants are involved.  For example, convert
  1539.        (A | B) & (A | C) into A | (B & C)
  1540.    Further simplification will occur if B and C are constants.
  1541.  
  1542.    If this optimization cannot be done, 0 will be returned.  */
  1543.  
  1544. static tree
  1545. distribute_bit_expr (code, type, arg0, arg1)
  1546.      enum tree_code code;
  1547.      tree type;
  1548.      tree arg0, arg1;
  1549. {
  1550.   tree common;
  1551.   tree left, right;
  1552.  
  1553.   if (TREE_CODE (arg0) != TREE_CODE (arg1)
  1554.       || TREE_CODE (arg0) == code
  1555.       || (TREE_CODE (arg0) != BIT_AND_EXPR
  1556.       && TREE_CODE (arg0) != BIT_IOR_EXPR))
  1557.     return 0;
  1558.  
  1559.   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
  1560.     {
  1561.       common = TREE_OPERAND (arg0, 0);
  1562.       left = TREE_OPERAND (arg0, 1);
  1563.       right = TREE_OPERAND (arg1, 1);
  1564.     }
  1565.   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
  1566.     {
  1567.       common = TREE_OPERAND (arg0, 0);
  1568.       left = TREE_OPERAND (arg0, 1);
  1569.       right = TREE_OPERAND (arg1, 0);
  1570.     }
  1571.   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
  1572.     {
  1573.       common = TREE_OPERAND (arg0, 1);
  1574.       left = TREE_OPERAND (arg0, 0);
  1575.       right = TREE_OPERAND (arg1, 1);
  1576.     }
  1577.   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
  1578.     {
  1579.       common = TREE_OPERAND (arg0, 1);
  1580.       left = TREE_OPERAND (arg0, 0);
  1581.       right = TREE_OPERAND (arg1, 0);
  1582.     }
  1583.   else
  1584.     return 0;
  1585.  
  1586.   return fold (build (TREE_CODE (arg0), type, common,
  1587.               fold (build (code, type, left, right))));
  1588. }
  1589.  
  1590. /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
  1591.    starting at BITPOS.  The field is unsigned if UNSIGNEDP is non-zero.  */
  1592.  
  1593. static tree
  1594. make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp)
  1595.      tree inner;
  1596.      tree type;
  1597.      int bitsize, bitpos;
  1598.      int unsignedp;
  1599. {
  1600.   tree result = build (BIT_FIELD_REF, type, inner,
  1601.                size_int (bitsize), size_int (bitpos));
  1602.  
  1603.   TREE_UNSIGNED (result) = unsignedp;
  1604.  
  1605.   return result;
  1606. }
  1607.  
  1608. /* Optimize a bit-field compare.
  1609.  
  1610.    There are two cases:  First is a compare against a constant and the
  1611.    second is a comparison of two items where the fields are at the same
  1612.    bit position relative to the start of a chunk (byte, halfword, word)
  1613.    large enough to contain it.  In these cases we can avoid the shift
  1614.    implicit in bitfield extractions.
  1615.  
  1616.    For constants, we emit a compare of the shifted constant with the
  1617.    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
  1618.    compared.  For two fields at the same position, we do the ANDs with the
  1619.    similar mask and compare the result of the ANDs.
  1620.  
  1621.    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
  1622.    COMPARE_TYPE is the type of the comparison, and LHS and RHS
  1623.    are the left and right operands of the comparison, respectively.
  1624.  
  1625.    If the optimization described above can be done, we return the resuling
  1626.    tree.  Otherwise we return zero.  */
  1627.  
  1628. static tree
  1629. optimize_bit_field_compare (code, compare_type, lhs, rhs)
  1630.      enum tree_code code;
  1631.      tree compare_type;
  1632.      tree lhs, rhs;
  1633. {
  1634.   int lbitpos, lbitsize, rbitpos, rbitsize;
  1635.   int lnbitpos, lnbitsize, rnbitpos, rnbitsize;
  1636.   tree type = TREE_TYPE (lhs);
  1637.   tree signed_type, unsigned_type;
  1638.   int const_p = TREE_CODE (rhs) == INTEGER_CST;
  1639.   enum machine_mode lmode, rmode, lnmode, rnmode;
  1640.   int lunsignedp, runsignedp;
  1641.   int lvolatilep, rvolatilep;
  1642.   tree linner, rinner;
  1643.   tree mask;
  1644.  
  1645.   /* Get all the information about the extractions being done.  If the bit size
  1646.      if the same as the size of the underlying object, we aren't doing an
  1647.      extraction at all and so can do nothing.  */
  1648.   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &lmode,
  1649.                 &lunsignedp, &lvolatilep);
  1650.   if (lbitsize == GET_MODE_BITSIZE (lmode))
  1651.     return 0;
  1652.  
  1653.  if (!const_p)
  1654.    {
  1655.      /* If this is not a constant, we can only do something if bit positions,
  1656.     sizes, and signedness are the same.   */
  1657.      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos,
  1658.                    &rmode, &runsignedp, &rvolatilep);
  1659.  
  1660.      if (lbitpos != rbitpos || lbitsize != rbitsize
  1661.      || lunsignedp != runsignedp)
  1662.        return 0;
  1663.    }
  1664.  
  1665.   /* See if we can find a mode to refer to this field.  We should be able to,
  1666.      but fail if we can't.  */
  1667.   lnmode = get_best_mode (lbitsize, lbitpos,
  1668.               TYPE_ALIGN (TREE_TYPE (linner)), SImode);
  1669.   if (lnmode == VOIDmode)
  1670.     return 0;
  1671.  
  1672.   /* Set signed and unsigned types of the precision of this mode for the
  1673.      shifts below.  */
  1674.   signed_type = type_for_mode (lnmode, 0);
  1675.   unsigned_type = type_for_mode (lnmode, 1);
  1676.  
  1677.   if (! const_p)
  1678.     {
  1679.       rnmode = get_best_mode (rbitsize, rbitpos, 
  1680.                   TYPE_ALIGN (TREE_TYPE (rinner)), SImode);
  1681.       if (rnmode == VOIDmode)
  1682.     return 0;
  1683.     }
  1684.     
  1685.   /* Compute the bit position and size for the new reference and our offset
  1686.      within it. If the new reference is the same size as the original, we
  1687.      won't optimize anything, so return zero.  */
  1688.   lnbitsize = GET_MODE_BITSIZE (lnmode);
  1689.   lnbitpos = lbitpos & ~ (lnbitsize - 1);
  1690.   lbitpos -= lnbitpos;
  1691.   if (lnbitsize == lbitsize)
  1692.     return 0;
  1693.  
  1694.   if (! const_p)
  1695.     {
  1696.       rnbitsize = GET_MODE_BITSIZE (rnmode);
  1697.       rnbitpos = rbitpos & ~ (rnbitsize - 1);
  1698.       rbitpos -= rnbitpos;
  1699.       if (rnbitsize == rbitsize)
  1700.     return 0;
  1701.     }
  1702.  
  1703. #if BYTES_BIG_ENDIAN
  1704.   lbitpos = lnbitsize - lbitsize - lbitpos;
  1705.   rbitpos = rnbitsize - rbitsize - rbitpos;
  1706. #endif
  1707.  
  1708.   /* Make the mask to be used against the extracted field.  */
  1709.   mask = convert (unsigned_type, build_int_2 (~0, ~0));
  1710.   mask = const_binop (LSHIFT_EXPR, mask, size_int (lnbitsize - lbitsize));
  1711.   mask = const_binop (RSHIFT_EXPR, mask,
  1712.               size_int (lnbitsize - lbitsize - lbitpos));
  1713.  
  1714.   if (! const_p)
  1715.     /* If not comparing with constant, just rework the comparison
  1716.        and return.  */
  1717.     return build (code, compare_type,
  1718.           build (BIT_AND_EXPR, type,
  1719.              make_bit_field_ref (linner, type,
  1720.                          lnbitsize, lnbitpos, lunsignedp),
  1721.              mask),
  1722.           build (BIT_AND_EXPR, type,
  1723.              make_bit_field_ref (rinner, type,
  1724.                          rnbitsize, rnbitpos, runsignedp),
  1725.              mask));
  1726.  
  1727.   /* Otherwise, we are handling the constant case. See if the constant is too
  1728.      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
  1729.      this not only for its own sake, but to avoid having to test for this
  1730.      error case below.  If we didn't, we might generate wrong code.
  1731.  
  1732.      For unsigned fields, the constant shifted right by the field length should
  1733.      be all zero.  For signed fields, the high-order bits should agree with 
  1734.      the sign bit.  */
  1735.  
  1736.   if (lunsignedp)
  1737.     {
  1738.       if (! integer_zerop (const_binop (RSHIFT_EXPR,
  1739.                     convert (unsigned_type, rhs),
  1740.                     size_int (lbitsize))))
  1741.     {
  1742. #ifdef NeXT
  1743.       if (code == EQ_EXPR)
  1744.         {
  1745.           warning ("comparison is always zero due to width of bitfield");
  1746.           return convert (compare_type, integer_zero_node);
  1747.         }
  1748.       else
  1749.         {
  1750.           warning ("comparison is always one due to width of bitfield");
  1751.           return convert (compare_type, integer_one_node);
  1752.         }
  1753. #else /* NeXT */
  1754.       warning ("comparison is always zero due to width of bitfield");
  1755.       return convert (compare_type, integer_zero_node);
  1756. #endif /* NeXT */
  1757.     }
  1758.     }
  1759.   else
  1760.     {
  1761.       tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs),
  1762.                   size_int (lbitsize - 1));
  1763.       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
  1764.     {
  1765. #ifdef NeXT
  1766.       if (code == EQ_EXPR)
  1767.         {
  1768.           warning ("comparison is always zero due to width of bitfield");
  1769.           return convert (compare_type, integer_zero_node);
  1770.         }
  1771.       else
  1772.         {
  1773.           warning ("comparison is always one due to width of bitfield");
  1774.           return convert (compare_type, integer_one_node);
  1775.         }
  1776. #else /* NeXT */
  1777.       warning ("comparison is always zero due to width of bitfield");
  1778.       return convert (compare_type, integer_zero_node);
  1779. #endif /* NeXT */
  1780.     }
  1781.     }
  1782.  
  1783.   /* Single-bit compares should always be against zero.  */
  1784.   if (lbitsize == 1 && ! integer_zerop (rhs))
  1785.     {
  1786.       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
  1787.       rhs = convert (type, integer_zero_node);
  1788.     }
  1789.  
  1790.   /* Make a new bitfield reference, shift the constant over the
  1791.      appropriate number of bits and mask it with the computed mask
  1792.      (in case this was a signed field).  If we changed it, make a new one.  */
  1793.   lhs = make_bit_field_ref (linner, TREE_TYPE (lhs), lnbitsize, lnbitpos, 
  1794.                 lunsignedp);
  1795.  
  1796.   rhs = convert (type, const_binop (BIT_AND_EXPR,
  1797.                     const_binop (LSHIFT_EXPR,
  1798.                          convert (unsigned_type, rhs),
  1799.                          size_int (lbitpos)), mask));
  1800.  
  1801.   return build (code, compare_type,
  1802.         build (BIT_AND_EXPR, type, lhs, mask),
  1803.         rhs);
  1804. }
  1805.  
  1806. /* Subroutine for the following routine: decode a field reference.
  1807.  
  1808.    If EXP is a comparison reference, we return the innermost reference.
  1809.  
  1810.    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
  1811.    set to the starting bit number.
  1812.  
  1813.    If the innermost field can be completely contained in a mode-sized
  1814.    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
  1815.  
  1816.    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
  1817.    otherwise it is not changed.
  1818.  
  1819.    *PUNSIGNEDP is set to the signedness of the field.
  1820.  
  1821.    *PMASK is set to the mask used.  This is either contained in a
  1822.    BIT_AND_EXPR or derived from the width of the field.
  1823.  
  1824.    Return 0 if this is not a component reference or is one that we can't
  1825.    do anything with.  */
  1826.  
  1827. static tree
  1828. decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
  1829.             pvolatilep, pmask)
  1830.      tree exp;
  1831.      int *pbitsize, *pbitpos;
  1832.      enum machine_mode *pmode;
  1833.      int *punsignedp, *pvolatilep;
  1834.      tree *pmask;
  1835. {
  1836.   tree mask = 0;
  1837.   tree inner;
  1838.  
  1839.   STRIP_NOPS (exp);
  1840.  
  1841.   if (TREE_CODE (exp) == BIT_AND_EXPR)
  1842.     {
  1843.       mask = TREE_OPERAND (exp, 1);
  1844.       exp = TREE_OPERAND (exp, 0);
  1845.       STRIP_NOPS (exp); STRIP_NOPS (mask);
  1846.       if (TREE_CODE (mask) != INTEGER_CST)
  1847.     return 0;
  1848.     }
  1849.  
  1850.   if (TREE_CODE (exp) != COMPONENT_REF && TREE_CODE (exp) != ARRAY_REF
  1851.       && TREE_CODE (exp) != BIT_FIELD_REF)
  1852.     return 0;
  1853.  
  1854.   inner = get_inner_reference (exp, pbitsize, pbitpos, pmode,
  1855.                    punsignedp, pvolatilep);
  1856.   
  1857.   if (mask == 0)
  1858.     {
  1859.       tree unsigned_type = type_for_size (*pbitsize, 1);
  1860.       int precision = TYPE_PRECISION (unsigned_type);
  1861.  
  1862.       mask = convert (unsigned_type, build_int_2 (~0, ~0));
  1863.       mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
  1864.       mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
  1865.     }
  1866.  
  1867.   *pmask = mask;
  1868.   return inner;
  1869. }
  1870.  
  1871. /* Return non-zero if MASK respresents a mask of SIZE ones in the low-order
  1872.    bit positions.  */
  1873.  
  1874. static int
  1875. all_ones_mask_p (mask, size)
  1876.      tree mask;
  1877.      int size;
  1878. {
  1879.   tree type = TREE_TYPE (mask);
  1880.   int precision = TYPE_PRECISION (type);
  1881.  
  1882.   return
  1883.     operand_equal_p (mask, 
  1884.              const_binop (RSHIFT_EXPR,
  1885.                   const_binop (LSHIFT_EXPR,
  1886.                            convert (signed_type (type),
  1887.                             build_int_2 (~0, ~0)),
  1888.                            size_int (precision - size)),
  1889.                   size_int (precision - size)), 0);
  1890. }
  1891.  
  1892. /* Try to merge two comparisons to the same innermost item.
  1893.  
  1894.    For example, if we have p->a == 2 && p->b == 4 and we can make an
  1895.    object large enough to span both A and B, we can do this with a comparison
  1896.    against the object ANDed with the a mask.
  1897.  
  1898.    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
  1899.    operations to do this with one comparison.
  1900.  
  1901.    We check for both normal comparisons and the BIT_AND_EXPRs made this by
  1902.    function and the one above.
  1903.  
  1904.    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
  1905.    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
  1906.  
  1907.    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
  1908.    two operands.
  1909.  
  1910.    We return the simplified tree or 0 if no optimization is possible.  */
  1911.  
  1912. static tree
  1913. merge_component_references (code, truth_type, lhs, rhs)
  1914.      enum tree_code code;
  1915.      tree truth_type, lhs, rhs;
  1916. {
  1917.   /* If this is the "or" of two comparisons, we can do something if we
  1918.      the comparisons are NE_EXPR.  If this is the "and", we can do something
  1919.      if the comparisons are EQ_EXPR.  I.e., 
  1920.          (a->b == 2 && a->c == 4) can become (a->new == NEW).
  1921.  
  1922.      WANTED_CODE is this operation code.  For single bit fields, we can
  1923.      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
  1924.      comparison for one-bit fields.  */
  1925.  
  1926.   enum tree_code wanted_code
  1927.     = (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR) ? EQ_EXPR : NE_EXPR;
  1928.   enum tree_code lcode, rcode;
  1929.   tree ll_inner, lr_inner, rl_inner, rr_inner;
  1930.   int ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
  1931.   int rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
  1932.   int xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
  1933.   int lnbitsize, lnbitpos, rnbitsize, rnbitpos;
  1934.   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
  1935.   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
  1936.   enum machine_mode lnmode, rnmode;
  1937.   tree ll_mask, lr_mask, rl_mask, rr_mask;
  1938.   tree l_const = 0, r_const = 0;
  1939.   tree type, result;
  1940.   int first_bit, end_bit;
  1941.   int volatilep = 0;
  1942.  
  1943.   /* Start by getting the comparison codes and seeing if we may be able
  1944.      to do something.  Then get all the parameters for each side.  Fail
  1945.      if anything is volatile.  */
  1946.  
  1947.   lcode = TREE_CODE (lhs);
  1948.   rcode = TREE_CODE (rhs);
  1949.   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
  1950.       || (rcode != EQ_EXPR && rcode != NE_EXPR)
  1951.       || TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
  1952.     return 0;
  1953.  
  1954.   ll_inner = decode_field_reference (TREE_OPERAND (lhs, 0),
  1955.                      &ll_bitsize, &ll_bitpos, &ll_mode,
  1956.                      &ll_unsignedp, &volatilep, &ll_mask);
  1957.   lr_inner = decode_field_reference (TREE_OPERAND (lhs, 1),
  1958.                      &lr_bitsize, &lr_bitpos, &lr_mode,
  1959.                      &lr_unsignedp, &volatilep, &lr_mask);
  1960.   rl_inner = decode_field_reference (TREE_OPERAND (rhs, 0),
  1961.                      &rl_bitsize, &rl_bitpos, &rl_mode,
  1962.                      &rl_unsignedp, &volatilep, &rl_mask);
  1963.   rr_inner = decode_field_reference (TREE_OPERAND (rhs, 1),
  1964.                      &rr_bitsize, &rr_bitpos, &rr_mode,
  1965.                      &rr_unsignedp, &volatilep, &rr_mask);
  1966.  
  1967.   /* It must be true that the inner operation on the lhs of each
  1968.      comparison must be the same if we are to be able to do anything.
  1969.      Then see if we have constants.  If not, the same must be true for
  1970.      the rhs's.  */
  1971.   if (volatilep || ll_inner == 0 || rl_inner == 0 ||
  1972.       ! operand_equal_p (ll_inner, rl_inner, 0))
  1973.     return 0;
  1974.  
  1975.   if (TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
  1976.       && TREE_CODE (TREE_OPERAND (rhs, 1)) == INTEGER_CST)
  1977.     l_const = TREE_OPERAND (lhs, 1), r_const = TREE_OPERAND (rhs, 1);
  1978.   else if (lr_inner == 0 || rr_inner == 0
  1979.        || ! operand_equal_p (lr_inner, rr_inner, 0))
  1980.     return 0;
  1981.  
  1982.   /* If either comparison code is not correct for our logical operation,
  1983.      fail.  However, we can convert a one-bit comparison against zero into
  1984.      the opposite comparison against that bit being set in the field.  */
  1985.   if (lcode != wanted_code)
  1986.     {
  1987.       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
  1988.     l_const = ll_mask;
  1989.       else
  1990.     return 0;
  1991.     }
  1992.  
  1993.   if (rcode != wanted_code)
  1994.     {
  1995.       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
  1996.     r_const = rl_mask;
  1997.       else
  1998.     return 0;
  1999.     }
  2000.  
  2001.   /* See if we can find a mode that contains both fields being compared on
  2002.      the left.  If we can't, fail.  Otherwise, update all constants and masks
  2003.      to be relative to a field of that size.  */
  2004.   first_bit = MIN (ll_bitpos, rl_bitpos);
  2005.   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
  2006.   lnmode = get_best_mode (end_bit - first_bit, first_bit,
  2007.               TYPE_ALIGN (TREE_TYPE (ll_inner)), SImode);
  2008.   if (lnmode == VOIDmode)
  2009.     return 0;
  2010.  
  2011.   lnbitsize = GET_MODE_BITSIZE (lnmode);
  2012.   lnbitpos = first_bit & ~ (lnbitsize - 1);
  2013.   type = type_for_size (lnbitsize, 1);
  2014.   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
  2015.  
  2016. #if BYTES_BIG_ENDIAN
  2017.   xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
  2018.   xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
  2019. #endif
  2020.  
  2021.   ll_mask = const_binop (LSHIFT_EXPR, convert (type, ll_mask),
  2022.              size_int (xll_bitpos));
  2023.   rl_mask = const_binop (LSHIFT_EXPR, convert (type, rl_mask),
  2024.              size_int (xrl_bitpos));
  2025.  
  2026.   if (l_const)
  2027.     l_const = const_binop (LSHIFT_EXPR, convert (type, l_const),
  2028.                size_int (xll_bitpos));
  2029.   if (r_const)
  2030.     r_const = const_binop (LSHIFT_EXPR, convert (type, r_const),
  2031.                size_int (xrl_bitpos));
  2032.  
  2033.   /* If the right sides are not constant, do the same for it.  Also,
  2034.      disallow this optimization if a size or signedness mismatch occurs
  2035.      between the left and right sides.  */
  2036.   if (l_const == 0)
  2037.     {
  2038.       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
  2039.       || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp)
  2040.     return 0;
  2041.  
  2042.       first_bit = MIN (lr_bitpos, rr_bitpos);
  2043.       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
  2044.       rnmode = get_best_mode (end_bit - first_bit, first_bit,
  2045.                   TYPE_ALIGN (TREE_TYPE (lr_inner)), SImode);
  2046.       if (rnmode == VOIDmode)
  2047.     return 0;
  2048.  
  2049.       rnbitsize = GET_MODE_BITSIZE (rnmode);
  2050.       rnbitpos = first_bit & ~ (rnbitsize - 1);
  2051.       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
  2052.  
  2053. #if BYTES_BIG_ENDIAN
  2054.       xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
  2055.       xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
  2056. #endif
  2057.  
  2058.       lr_mask = const_binop (LSHIFT_EXPR, convert (type, lr_mask),
  2059.                  size_int (xlr_bitpos));
  2060.       rr_mask = const_binop (LSHIFT_EXPR, convert (type, rr_mask),
  2061.                  size_int (xrr_bitpos));
  2062.  
  2063.       /* Make a mask that corresponds to both fields being compared.
  2064.      Do this for both items being compared.  If the masks agree,
  2065.      we can do this by masking both and comparing the masked
  2066.      results.  */
  2067.       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
  2068.       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
  2069.       if (operand_equal_p (ll_mask, lr_mask, 0) && lnbitsize == rnbitsize)
  2070.     {
  2071.       lhs = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos,
  2072.                     ll_unsignedp || rl_unsignedp);
  2073.       rhs = make_bit_field_ref (lr_inner, type, rnbitsize, rnbitpos,
  2074.                     lr_unsignedp || rr_unsignedp);
  2075.       if (! all_ones_mask_p (ll_mask, lnbitsize))
  2076.         {
  2077.           lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
  2078.           rhs = build (BIT_AND_EXPR, type, rhs, ll_mask);
  2079.         }
  2080.       return build (wanted_code, truth_type, lhs, rhs);
  2081.     }
  2082.  
  2083.       /* There is still another way we can do something:  If both pairs of
  2084.      fields being compared are adjacent, we may be able to make a wider
  2085.      field containing them both.  */
  2086.       if ((ll_bitsize + ll_bitpos == rl_bitpos
  2087.        && lr_bitsize + lr_bitpos == rr_bitpos)
  2088.       || (ll_bitpos == rl_bitpos + rl_bitsize
  2089.           && lr_bitpos == rr_bitpos + rr_bitsize))
  2090.     return build (wanted_code, truth_type,
  2091.               make_bit_field_ref (ll_inner, type,
  2092.                       ll_bitsize + rl_bitsize,
  2093.                       MIN (ll_bitpos, rl_bitpos),
  2094.                       ll_unsignedp),
  2095.               make_bit_field_ref (lr_inner, type,
  2096.                       lr_bitsize + rr_bitsize,
  2097.                       MIN (lr_bitpos, rr_bitpos),
  2098.                       lr_unsignedp));
  2099.  
  2100.       return 0;
  2101.     }
  2102.  
  2103.   /* Handle the case of comparisons with constants.  If there is something in
  2104.      common between the masks, those bits of the constants must be the same.
  2105.      If not, the condition is always false.  Test for this to avoid generating
  2106.      incorrect code below.  */
  2107.   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
  2108.   if (! integer_zerop (result)
  2109.       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
  2110.                const_binop (BIT_AND_EXPR, result, r_const)) != 1)
  2111.     {
  2112.       warning ("comparison is always zero due to mutually exclusive conditions");
  2113.       return convert (truth_type, integer_zero_node);
  2114.     }
  2115.  
  2116.   /* Construct the expression we will return.  First get the component
  2117.      reference we will make.  Unless the mask is all ones the width of
  2118.      that field, perform the mask operation.  Then compare with the
  2119.      merged constant.  */
  2120.   result = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos,
  2121.                    ll_unsignedp || rl_unsignedp);
  2122.  
  2123.   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
  2124.   if (! all_ones_mask_p (ll_mask, lnbitsize))
  2125.     result = build (BIT_AND_EXPR, type, result, ll_mask);
  2126.  
  2127.   return build (wanted_code, truth_type, result,
  2128.         const_binop (BIT_IOR_EXPR, l_const, r_const));
  2129. }
  2130.  
  2131. /* Perform constant folding and related simplification of EXPR.
  2132.    The related simplifications include x*1 => x, x*0 => 0, etc.,
  2133.    and application of the associative law.
  2134.    NOP_EXPR conversions may be removed freely (as long as we
  2135.    are careful not to change the C type of the overall expression)
  2136.    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
  2137.    but we can constant-fold them if they have constant operands.  */
  2138.  
  2139. tree
  2140. fold (expr) 
  2141.      tree expr;
  2142. {
  2143.   register tree t = expr;
  2144.   tree t1 = NULL_TREE;
  2145.   tree type = TREE_TYPE (expr);
  2146.   register tree arg0, arg1;
  2147.   register enum tree_code code = TREE_CODE (t);
  2148.   register int kind;
  2149.  
  2150.   /* WINS will be nonzero when the switch is done
  2151.      if all operands are constant.  */
  2152.  
  2153.   int wins = 1;
  2154.  
  2155.   /* Return right away if already constant.  */
  2156.   if (TREE_CONSTANT (t))
  2157.     {
  2158.       if (code == CONST_DECL)
  2159.     return DECL_INITIAL (t);
  2160.       return t;
  2161.     }
  2162.   
  2163.   kind = TREE_CODE_CLASS (code);
  2164.   if (kind == 'e' || kind == '<' || kind == '1' || kind == '2' || kind == 'r')
  2165.     {
  2166.       register int len = tree_code_length[(int) code];
  2167.       register int i;
  2168.       for (i = 0; i < len; i++)
  2169.     {
  2170.       tree op = TREE_OPERAND (t, i);
  2171.  
  2172.       if (op == 0)
  2173.         continue;        /* Valid for CALL_EXPR, at least.  */
  2174.  
  2175.       /* Strip any conversions that don't change the mode.  */
  2176.       STRIP_NOPS (op);
  2177.       
  2178.       if (TREE_CODE (op) != INTEGER_CST
  2179. #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  2180.           && TREE_CODE (op) != REAL_CST
  2181. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  2182.           )
  2183.         /* Note that TREE_CONSTANT isn't enough:
  2184.            static var addresses are constant but we can't
  2185.            do arithmetic on them.  */
  2186.         wins = 0;
  2187.  
  2188.       if (i == 0)
  2189.         arg0 = op;
  2190.       else if (i == 1)
  2191.         arg1 = op;
  2192.     }
  2193.     }
  2194.  
  2195.   /* If this is a commutative operation, and ARG0 is a constant, move it
  2196.      to ARG1 to reduce the number of tests below.  */
  2197.   if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR
  2198.        || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR
  2199.        || code == BIT_AND_EXPR)
  2200.       && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
  2201.     {
  2202.       tree tem = arg0;
  2203.       arg0 = arg1; arg1 = tem;
  2204.  
  2205.       TREE_OPERAND (t, 0) = arg0;
  2206.       TREE_OPERAND (t, 1) = arg1;
  2207.     }
  2208.  
  2209.   /* Now WINS is set as described above,
  2210.      ARG0 is the first operand of EXPR,
  2211.      and ARG1 is the second operand (if it has more than one operand).
  2212.  
  2213.      First check for cases where an arithmetic operation is applied to a
  2214.      compound, conditional, or comparison operation.  Push the arithmetic
  2215.      operation inside the compound or conditional to see if any folding
  2216.      can then be done.  Convert comparison to conditional for this purpose.
  2217.      The also optimizes non-constant cases that used to be done in
  2218.      expand_expr.  */
  2219.   if (TREE_CODE_CLASS (code) == '1')
  2220.     {
  2221.       if (TREE_CODE (arg0) == COMPOUND_EXPR)
  2222.     return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
  2223.               fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
  2224.       else if (TREE_CODE (arg0) == COND_EXPR)
  2225.     return fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
  2226.                 fold (build1 (code, type, TREE_OPERAND (arg0, 1))),
  2227.                 fold (build1 (code, type, TREE_OPERAND (arg0, 2)))));
  2228.       else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<') 
  2229.     return fold (build (COND_EXPR, type, arg0,
  2230.                 fold (build1 (code, type, integer_one_node)),
  2231.                 fold (build1 (code, type, integer_zero_node))));
  2232.    }
  2233.   else if (TREE_CODE_CLASS (code) == '2')
  2234.     {
  2235.       if (TREE_CODE (arg1) == COMPOUND_EXPR)
  2236.     return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
  2237.               fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
  2238.       else if (TREE_CODE (arg1) == COND_EXPR
  2239.            || TREE_CODE_CLASS (TREE_CODE (arg1)) == '<')
  2240.     {
  2241.       tree test, true_value, false_value;
  2242.  
  2243.       if (TREE_CODE (arg1) == COND_EXPR)
  2244.         {
  2245.           test = TREE_OPERAND (arg1, 0);
  2246.           true_value = TREE_OPERAND (arg1, 1);
  2247.           false_value = TREE_OPERAND (arg1, 2);
  2248.         }
  2249.       else
  2250.         {
  2251.           test = arg1;
  2252.           true_value = integer_one_node;
  2253.           false_value = integer_zero_node;
  2254.         }
  2255.  
  2256.       if (TREE_CODE (arg0) != VAR_DECL && TREE_CODE (arg0) != PARM_DECL)
  2257.         arg0 = save_expr (arg0);
  2258.       test = fold (build (COND_EXPR, type, test,
  2259.                   fold (build (code, type, arg0, true_value)),
  2260.                   fold (build (code, type, arg0, false_value))));
  2261.       if (TREE_CODE (arg0) == SAVE_EXPR)
  2262.         return build (COMPOUND_EXPR, type,
  2263.               convert (void_type_node, arg0), test);
  2264.       else
  2265.         return test;
  2266.     }
  2267.  
  2268.       else if (TREE_CODE (arg0) == COMPOUND_EXPR)
  2269.     return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
  2270.               fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
  2271.       else if (TREE_CODE (arg0) == COND_EXPR
  2272.            || TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
  2273.     {
  2274.       tree test, true_value, false_value;
  2275.  
  2276.       if (TREE_CODE (arg0) == COND_EXPR)
  2277.         {
  2278.           test = TREE_OPERAND (arg0, 0);
  2279.           true_value = TREE_OPERAND (arg0, 1);
  2280.           false_value = TREE_OPERAND (arg0, 2);
  2281.         }
  2282.       else
  2283.         {
  2284.           test = arg0;
  2285.           true_value = integer_one_node;
  2286.           false_value = integer_zero_node;
  2287.         }
  2288.  
  2289.       if (TREE_CODE (arg1) != VAR_DECL && TREE_CODE (arg1) != PARM_DECL)
  2290.         arg1 = save_expr (arg1);
  2291.       test = fold (build (COND_EXPR, type, test,
  2292.                   fold (build (code, type, true_value, arg1)),
  2293.                   fold (build (code, type, false_value, arg1))));
  2294.       if (TREE_CODE (arg1) == SAVE_EXPR)
  2295.         return build (COMPOUND_EXPR, type,
  2296.               convert (void_type_node, arg1), test);
  2297.       else
  2298.         return test;
  2299.     }
  2300.     }
  2301.       
  2302.   switch (code)
  2303.     {
  2304.     case INTEGER_CST:
  2305.     case REAL_CST:
  2306.     case STRING_CST:
  2307.     case COMPLEX_CST:
  2308.     case CONSTRUCTOR:
  2309.       return t;
  2310.  
  2311.     case CONST_DECL:
  2312.       return fold (DECL_INITIAL (t));
  2313.  
  2314.     case NOP_EXPR:
  2315.     case FLOAT_EXPR:
  2316.     case CONVERT_EXPR:
  2317.     case FIX_TRUNC_EXPR:
  2318.       /* Other kinds of FIX are not handled properly by fold_convert.  */
  2319.       /* Two conversions in a row are not needed unless:
  2320.      - the intermediate type is narrower than both initial and final, or
  2321.      - the initial type is a pointer type and the precisions of the
  2322.        intermediate and final types differ, or
  2323.      - the final type is a pointer type and the precisions of the 
  2324.       initial and intermediate types differ.  */
  2325.       if ((TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
  2326.        || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
  2327.       && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
  2328.           > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
  2329.           ||
  2330.           TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
  2331.           > TYPE_PRECISION (TREE_TYPE (t)))
  2332.       && ((TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t, 0)))
  2333.            && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
  2334.            > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))))
  2335.           ==
  2336.           (TREE_UNSIGNED (TREE_TYPE (t))
  2337.            && (TYPE_PRECISION (TREE_TYPE (t))
  2338.            > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))))))
  2339.       && ! ((TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
  2340.          == POINTER_TYPE)
  2341.         && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
  2342.             != TYPE_PRECISION (TREE_TYPE (t))))
  2343.       && ! (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
  2344.         && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
  2345.             != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))))))
  2346.     return convert (TREE_TYPE (t), TREE_OPERAND (TREE_OPERAND (t, 0), 0));
  2347.  
  2348.       if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
  2349.       && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1)))
  2350.     {
  2351.       /* Don't leave an assignment inside a conversion.  */
  2352.       tree prev = TREE_OPERAND (t, 0);
  2353.       TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1);
  2354.       /* First do the assignment, then return converted constant.  */
  2355.       t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t));
  2356.       TREE_USED (t) = 1;
  2357.       return t;
  2358.     }
  2359.       if (!wins)
  2360.     {
  2361.       TREE_CONSTANT (t) = TREE_CONSTANT (arg0);
  2362.       return t;
  2363.     }
  2364.       return fold_convert (t, arg0);
  2365.  
  2366. #if 0  /* This loses on &"foo"[0].  */
  2367.     case ARRAY_REF:
  2368.     {
  2369.       int i;
  2370.  
  2371.       /* Fold an expression like: "foo"[2] */
  2372.       if (TREE_CODE (arg0) == STRING_CST
  2373.           && TREE_CODE (arg1) == INTEGER_CST
  2374.           && !TREE_INT_CST_HIGH (arg1)
  2375.           && (i = TREE_INT_CST_LOW (arg1)) < TREE_STRING_LENGTH (arg0))
  2376.         {
  2377.           t = build_int_2 (TREE_STRING_POINTER (arg0)[i], 0);
  2378.           TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (arg0));
  2379.           force_fit_type (t);
  2380.         }
  2381.     }
  2382.       return t;
  2383. #endif /* 0 */
  2384.  
  2385.     case RANGE_EXPR:
  2386.       TREE_CONSTANT (t) = wins;
  2387.       return t;
  2388.  
  2389.     case NEGATE_EXPR:
  2390.       if (wins)
  2391.     {
  2392.       if (TREE_CODE (arg0) == INTEGER_CST)
  2393.         {
  2394.           if (TREE_INT_CST_LOW (arg0) == 0)
  2395.         t = build_int_2 (0, - TREE_INT_CST_HIGH (arg0));
  2396.           else
  2397.         t = build_int_2 (- TREE_INT_CST_LOW (arg0),
  2398.                  ~ TREE_INT_CST_HIGH (arg0));
  2399.           TREE_TYPE (t) = type;
  2400.           force_fit_type (t);
  2401.         }
  2402.       else if (TREE_CODE (arg0) == REAL_CST)
  2403.         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
  2404.       TREE_TYPE (t) = type;
  2405.     }
  2406.       else if (TREE_CODE (arg0) == NEGATE_EXPR)
  2407.     return TREE_OPERAND (arg0, 0);
  2408.  
  2409.       /* Convert - (a - b) to (b - a) for non-floating-point.  */
  2410.       else if (TREE_CODE (arg0) == MINUS_EXPR && TREE_CODE (type) != REAL_TYPE)
  2411.     return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1),
  2412.               TREE_OPERAND (arg0, 0));
  2413.  
  2414.       return t;
  2415.  
  2416.     case ABS_EXPR:
  2417.       if (wins)
  2418.     {
  2419.       if (TREE_CODE (arg0) == INTEGER_CST)
  2420.         {
  2421.           if (! TREE_UNSIGNED (type)
  2422.           && TREE_INT_CST_HIGH (arg0) < 0)
  2423.         {
  2424.           if (TREE_INT_CST_LOW (arg0) == 0)
  2425.             t = build_int_2 (0, - TREE_INT_CST_HIGH (arg0));
  2426.           else
  2427.             t = build_int_2 (- TREE_INT_CST_LOW (arg0),
  2428.                      ~ TREE_INT_CST_HIGH (arg0));
  2429.         }
  2430.         }
  2431.       else if (TREE_CODE (arg0) == REAL_CST)
  2432.         {
  2433.           if (REAL_VALUES_LESS (TREE_REAL_CST (arg0), dconst0))
  2434.         t = build_real (type,
  2435.                 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
  2436.         }
  2437.       TREE_TYPE (t) = type;
  2438.     }
  2439.       else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR)
  2440.     return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
  2441.       return t;
  2442.  
  2443.     case BIT_NOT_EXPR:
  2444.       if (wins)
  2445.     {
  2446.       if (TREE_CODE (arg0) == INTEGER_CST)
  2447.         t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
  2448.                  ~ TREE_INT_CST_HIGH (arg0));
  2449.       TREE_TYPE (t) = type;
  2450.       force_fit_type (t);
  2451.     }
  2452.       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
  2453.     return TREE_OPERAND (arg0, 0);
  2454.       return t;
  2455.  
  2456.     case PLUS_EXPR:
  2457.       /* A + (-B) -> A - B */
  2458.       if (TREE_CODE (arg1) == NEGATE_EXPR)
  2459.     return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
  2460.       else if (TREE_CODE (type) != REAL_TYPE)
  2461.     {
  2462.       if (integer_zerop (arg1))
  2463.         return non_lvalue (convert (type, arg0));
  2464.  
  2465.       /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
  2466.          with a constant, and the two constants have no bits in common,
  2467.          we should treat this as a BIT_IOR_EXPR since this may produce more
  2468.          simplifications.  */
  2469.       if (TREE_CODE (arg0) == BIT_AND_EXPR
  2470.           && TREE_CODE (arg1) == BIT_AND_EXPR
  2471.           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
  2472.           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
  2473.           && integer_zerop (const_binop (BIT_AND_EXPR,
  2474.                          TREE_OPERAND (arg0, 1),
  2475.                          TREE_OPERAND (arg1, 1))))
  2476.         {
  2477.           code = BIT_IOR_EXPR;
  2478.           goto bit_ior;
  2479.         }
  2480.     }
  2481.       /* In IEEE floating point, x+0 may not equal x.  */
  2482.       else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  2483.            && real_zerop (arg1))
  2484.     return non_lvalue (convert (type, arg0));
  2485.     associate:
  2486.       /* In most languages, can't associate operations on floats
  2487.      through parentheses.  Rather than remember where the parentheses
  2488.      were, we don't associate floats at all.  It shouldn't matter much.  */
  2489.       if (TREE_CODE (type) == REAL_TYPE)
  2490.     goto binary;
  2491.       /* The varsign == -1 cases happen only for addition and subtraction.
  2492.      It says that the arg that was split was really CON minus VAR.
  2493.      The rest of the code applies to all associative operations.  */
  2494.       if (!wins)
  2495.     {
  2496.       tree var, con, tem;
  2497.       int varsign;
  2498.  
  2499.       if (split_tree (arg0, code, &var, &con, &varsign))
  2500.         {
  2501.           if (varsign == -1)
  2502.         {
  2503.           /* EXPR is (CON-VAR) +- ARG1.  */
  2504.           /* If it is + and VAR==ARG1, return just CONST.  */
  2505.           if (code == PLUS_EXPR && operand_equal_p (var, arg1, 0))
  2506.             return convert (TREE_TYPE (t), con);
  2507.             
  2508.           /* Otherwise return (CON +- ARG1) - VAR.  */
  2509.           TREE_SET_CODE (t, MINUS_EXPR);
  2510.           TREE_OPERAND (t, 1) = var;
  2511.           TREE_OPERAND (t, 0)
  2512.             = fold (build (code, TREE_TYPE (t), con, arg1));
  2513.         }
  2514.           else
  2515.         {
  2516.           /* EXPR is (VAR+CON) +- ARG1.  */
  2517.           /* If it is - and VAR==ARG1, return just CONST.  */
  2518.           if (code == MINUS_EXPR && operand_equal_p (var, arg1, 0))
  2519.             return convert (TREE_TYPE (t), con);
  2520.             
  2521.           /* Otherwise return VAR +- (ARG1 +- CON).  */
  2522.           TREE_OPERAND (t, 1) = tem
  2523.             = fold (build (code, TREE_TYPE (t), arg1, con));
  2524.           TREE_OPERAND (t, 0) = var;
  2525.           if (integer_zerop (tem)
  2526.               && (code == PLUS_EXPR || code == MINUS_EXPR))
  2527.             return var;
  2528.           /* If we have x +/- (c - d) [c an explicit integer]
  2529.              change it to x -/+ (d - c) since if d is relocatable
  2530.              then the latter can be a single immediate insn
  2531.              and the former cannot.  */
  2532.           if (TREE_CODE (tem) == MINUS_EXPR
  2533.               && TREE_CODE (TREE_OPERAND (tem, 0)) == INTEGER_CST)
  2534.             {
  2535.               tree tem1 = TREE_OPERAND (tem, 1);
  2536.               TREE_OPERAND (tem, 1) = TREE_OPERAND (tem, 0);
  2537.               TREE_OPERAND (tem, 0) = tem1;
  2538.               TREE_SET_CODE (t,
  2539.                      (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
  2540.             }
  2541.         }
  2542.           return t;
  2543.         }
  2544.  
  2545.       if (split_tree (arg1, code, &var, &con, &varsign))
  2546.         {
  2547.           /* EXPR is ARG0 +- (CON +- VAR).  */
  2548.           if (varsign == -1)
  2549.         TREE_SET_CODE (t,
  2550.                    (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
  2551.           if (TREE_CODE (t) == MINUS_EXPR
  2552.           && operand_equal_p (var, arg0, 0))
  2553.         {
  2554.           /* If VAR and ARG0 cancel, return just CON or -CON.  */
  2555.           if (code == PLUS_EXPR)
  2556.             return convert (TREE_TYPE (t), con);
  2557.           return fold (build1 (NEGATE_EXPR, TREE_TYPE (t),
  2558.                        convert (TREE_TYPE (t), con)));
  2559.         }
  2560.           TREE_OPERAND (t, 0)
  2561.         = fold (build (code, TREE_TYPE (t), arg0, con));
  2562.           TREE_OPERAND (t, 1) = var;
  2563.           if (integer_zerop (TREE_OPERAND (t, 0))
  2564.           && TREE_CODE (t) == PLUS_EXPR)
  2565.         return convert (TREE_TYPE (t), var);
  2566.           return t;
  2567.         }
  2568.     }
  2569.     binary:
  2570. #if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC)
  2571.       if (TREE_CODE (arg1) == REAL_CST)
  2572.     return t;
  2573. #endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */
  2574.       if (wins)
  2575.     t1 = const_binop (code, arg0, arg1);
  2576.       if (t1 != NULL_TREE) return t1;
  2577.       return t;
  2578.  
  2579.     case MINUS_EXPR:
  2580.       if (TREE_CODE (type) != REAL_TYPE)
  2581.     {
  2582.       if (! wins && integer_zerop (arg0))
  2583.         return build1 (NEGATE_EXPR, type, arg1);
  2584.       if (integer_zerop (arg1))
  2585.         return non_lvalue (convert (type, arg0));
  2586.     }
  2587.       /* Convert A - (-B) to A + B.  */
  2588.       else if (TREE_CODE (arg1) == NEGATE_EXPR)
  2589.     return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
  2590.       else
  2591.     {
  2592.       if (! wins && real_zerop (arg0))
  2593.         return build1 (NEGATE_EXPR, type, arg1);
  2594.       /* In IEEE floating point, x-0 may not equal x.  */
  2595.       if (real_zerop (arg1) && TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
  2596.         return non_lvalue (convert (type, arg0));
  2597.     }
  2598.       /* Fold &x - &x.  This can happen from &x.foo - &x. 
  2599.      Note that can't be done for certain floats even in non-IEEE formats.
  2600.      Also note that operand_equal_p is always false is an operand
  2601.      is volatile.  */
  2602.  
  2603.       if (operand_equal_p (arg0, arg1,
  2604.                TREE_CODE (type) == REAL_TYPE))
  2605.     return convert (type, integer_zero_node);
  2606.       goto associate;
  2607.  
  2608.     case MULT_EXPR:
  2609.       if (TREE_CODE (type) != REAL_TYPE)
  2610.     {
  2611.       if (integer_zerop (arg1))
  2612.         return omit_one_operand (type, arg1, arg0);
  2613.       if (integer_onep (arg1))
  2614.         return non_lvalue (convert (type, arg0));
  2615.     }
  2616.       /* In IEEE floating point, these optimizations are not correct.  */
  2617.       else
  2618.     {
  2619.       if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
  2620.           && real_zerop (arg1))
  2621.         return omit_one_operand (type, arg1, arg0);
  2622.       /* In IEEE floating point, x*1 is not equivalent to x for nans.
  2623.          However, ANSI says we can drop signals,
  2624.          so we can do this anyway.  */
  2625.       if (real_onep (arg1))
  2626.         return non_lvalue (convert (type, arg0));
  2627.       /* x*2 is x+x */
  2628.       if (! wins && real_twop (arg1))
  2629.         return build (PLUS_EXPR, type, arg0, arg0);
  2630.     }
  2631.       goto associate;
  2632.  
  2633.     case BIT_IOR_EXPR:
  2634.     bit_ior:
  2635.       if (integer_all_onesp (arg1))
  2636.     return omit_one_operand (type, arg1, arg0);
  2637.       if (integer_zerop (arg1))
  2638.     return non_lvalue (convert (type, arg0));
  2639.       t1 = distribute_bit_expr (code, type, arg0, arg1);
  2640.       if (t1 != NULL_TREE)
  2641.     return t1;
  2642.       goto associate;
  2643.  
  2644.     case BIT_XOR_EXPR:
  2645.       if (integer_zerop (arg1))
  2646.     return non_lvalue (convert (type, arg0));
  2647.       if (integer_all_onesp (arg1))
  2648.     return fold (build1 (BIT_NOT_EXPR, type, arg0));
  2649.       goto associate;
  2650.  
  2651.     case BIT_AND_EXPR:
  2652.     bit_and:
  2653.       if (integer_all_onesp (arg1))
  2654.     return non_lvalue (convert (type, arg0));
  2655.       if (integer_zerop (arg1))
  2656.     return omit_one_operand (type, arg1, arg0);
  2657.       t1 = distribute_bit_expr (code, type, arg0, arg1);
  2658.       if (t1 != NULL_TREE)
  2659.     return t1;
  2660.       /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char.  */
  2661.       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR
  2662.       && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))))
  2663.     {
  2664.       int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)));
  2665.       if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_INT
  2666.           && (~TREE_INT_CST_LOW (arg0) & ((1 << prec) - 1)) == 0)
  2667.         return build1 (NOP_EXPR, type, TREE_OPERAND (arg1, 0));
  2668.     }
  2669.       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
  2670.       && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
  2671.     {
  2672.       int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
  2673.       if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_INT
  2674.           && (~TREE_INT_CST_LOW (arg1) & ((1 << prec) - 1)) == 0)
  2675.         return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0));
  2676.     }
  2677.       goto associate;
  2678.  
  2679.     case BIT_ANDTC_EXPR:
  2680.       if (integer_all_onesp (arg0))
  2681.     return non_lvalue (convert (type, arg1));
  2682.       if (integer_zerop (arg0))
  2683.     return omit_one_operand (type, arg0, arg1);
  2684.       if (TREE_CODE (arg1) == INTEGER_CST)
  2685.     {
  2686.       arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1));
  2687.       code = BIT_AND_EXPR;
  2688.       goto bit_and;
  2689.     }
  2690.       goto binary;
  2691.  
  2692.     case TRUNC_DIV_EXPR:
  2693.     case ROUND_DIV_EXPR:
  2694.     case FLOOR_DIV_EXPR:
  2695.     case CEIL_DIV_EXPR:
  2696.     case EXACT_DIV_EXPR:
  2697.     case RDIV_EXPR:
  2698.       if (integer_onep (arg1))
  2699.     return non_lvalue (convert (type, arg0));
  2700.       if (integer_zerop (arg1))
  2701.     return t;
  2702. #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
  2703. #ifndef REAL_INFINITY
  2704.       if (TREE_CODE (arg1) == REAL_CST
  2705.       && real_zerop (arg1))
  2706.     return t;
  2707. #endif
  2708. #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
  2709.  
  2710.       goto binary;
  2711.  
  2712.     case CEIL_MOD_EXPR:
  2713.     case FLOOR_MOD_EXPR:
  2714.     case ROUND_MOD_EXPR:
  2715.     case TRUNC_MOD_EXPR:
  2716.       if (integer_onep (arg1))
  2717.     return omit_one_operand (type, integer_zero_node, arg0);
  2718.       if (integer_zerop (arg1))
  2719.     return t;
  2720.       goto binary;
  2721.  
  2722.     case LSHIFT_EXPR:
  2723.     case RSHIFT_EXPR:
  2724.     case LROTATE_EXPR:
  2725.     case RROTATE_EXPR:
  2726.       if (integer_zerop (arg1))
  2727.     return non_lvalue (convert (type, arg0));
  2728.       goto binary;
  2729.  
  2730.     case MIN_EXPR:
  2731.     case MAX_EXPR:
  2732.       goto associate;
  2733.  
  2734.     case TRUTH_NOT_EXPR:
  2735.       /* Note that the operand of this must be an int
  2736.      and its values must be 0 or 1.
  2737.      ("true" is a fixed value perhaps depending on the language,
  2738.      but we don't handle values other than 1 correctly yet.)  */
  2739.       return invert_truthvalue (arg0);
  2740.  
  2741.     case TRUTH_ANDIF_EXPR:
  2742.       /* Note that the operands of this must be ints
  2743.      and their values must be 0 or 1.
  2744.      ("true" is a fixed value perhaps depending on the language.)  */
  2745.       /* If first arg is constant zero, return it.  */
  2746.       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
  2747.     return arg0;
  2748.     case TRUTH_AND_EXPR:
  2749.       /* If either arg is constant true, drop it.  */
  2750.       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
  2751.     return non_lvalue (arg1);
  2752.       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
  2753.     return non_lvalue (arg0);
  2754.       /* Both known to be zero => return zero.  */
  2755.       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
  2756.     return arg0;
  2757.  
  2758.     truth_andor:
  2759.       /* Check for the possibility of merging component references.  If our
  2760.      lhs is another similar operation, try to merge its rhs with our
  2761.      rhs.  Then try to merge our lhs and rhs.  */
  2762.       if (optimize)
  2763.     {
  2764.       tree tem;
  2765.  
  2766.       if (TREE_CODE (arg0) == code)
  2767.         {
  2768.           tem = merge_component_references (code, type,
  2769.                         TREE_OPERAND (arg0, 1), arg1);
  2770.           if (tem)
  2771.         return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
  2772.         }
  2773.  
  2774.       tem = merge_component_references (code, type, arg0, arg1);
  2775.       if (tem)
  2776.         return tem;
  2777.     }
  2778.       return t;
  2779.  
  2780.     case TRUTH_ORIF_EXPR:
  2781.       /* Note that the operands of this must be ints
  2782.      and their values must be 0 or true.
  2783.      ("true" is a fixed value perhaps depending on the language.)  */
  2784.       /* If first arg is constant true, return it.  */
  2785.       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
  2786.     return arg0;
  2787.     case TRUTH_OR_EXPR:
  2788.       /* If either arg is constant zero, drop it.  */
  2789.       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
  2790.     return non_lvalue (arg1);
  2791.       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1))
  2792.     return non_lvalue (arg0);
  2793.       /* Both known to be true => return true.  */
  2794.       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
  2795.     return arg0;
  2796.       goto truth_andor;
  2797.  
  2798.     case EQ_EXPR:
  2799.     case NE_EXPR:
  2800.     case LT_EXPR:
  2801.     case GT_EXPR:
  2802.     case LE_EXPR:
  2803.     case GE_EXPR:
  2804.       /* If one arg is a constant integer, put it last.  */
  2805.       if (TREE_CODE (arg0) == INTEGER_CST
  2806.       && TREE_CODE (arg1) != INTEGER_CST)
  2807.     {
  2808.       TREE_OPERAND (t, 0) = arg1;
  2809.       TREE_OPERAND (t, 1) = arg0;
  2810.       arg0 = TREE_OPERAND (t, 0);
  2811.       arg1 = TREE_OPERAND (t, 1);
  2812.       switch (code)
  2813.         {
  2814.         case GT_EXPR:
  2815.           code = LT_EXPR;
  2816.           break;
  2817.         case GE_EXPR:
  2818.           code = LE_EXPR;
  2819.           break;
  2820.         case LT_EXPR:
  2821.           code = GT_EXPR;
  2822.           break;
  2823.         case LE_EXPR:
  2824.           code = GE_EXPR;
  2825.           break;
  2826.         }
  2827.       TREE_SET_CODE (t, code);
  2828.     }
  2829.  
  2830.       /* Convert foo++ == CONST into ++foo == CONST + INCR.
  2831.      First, see if one arg is constant; find the constant arg
  2832.      and the other one.  */
  2833.       {
  2834.     tree constop = 0, varop;
  2835.     tree *constoploc;
  2836.  
  2837.     if (TREE_CONSTANT (arg1))
  2838.       constoploc = &TREE_OPERAND (t, 1), constop = arg1, varop = arg0;
  2839.     if (TREE_CONSTANT (arg0))
  2840.       constoploc = &TREE_OPERAND (t, 0), constop = arg0, varop = arg1;
  2841.  
  2842.     if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
  2843.       {
  2844.         tree newconst
  2845.           = fold (build (PLUS_EXPR, TREE_TYPE (t),
  2846.                  constop, TREE_OPERAND (varop, 1)));
  2847.         /* This optimization is invalid for ordered comparisons
  2848.            if CONST+INCR overflows or if foo+incr might overflow.
  2849.            For pointer types we assume overflow doesn't happen.  */
  2850.         if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE
  2851.         || code == EQ_EXPR || code == NE_EXPR)
  2852.           {
  2853.         TREE_SET_CODE (varop, PREINCREMENT_EXPR);
  2854.         *constoploc = newconst;
  2855.         return t;
  2856.           }
  2857.       }
  2858.     else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
  2859.       {
  2860.         tree newconst
  2861.           = fold (build (MINUS_EXPR, TREE_TYPE (t),
  2862.                  constop, TREE_OPERAND (varop, 1)));
  2863.         if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE
  2864.         || code == EQ_EXPR || code == NE_EXPR)
  2865.           {
  2866.         TREE_SET_CODE (varop, PREDECREMENT_EXPR);
  2867.         *constoploc = newconst;
  2868.         return t;
  2869.           }
  2870.       }
  2871.       }
  2872.  
  2873.       /* Change X >= CST to X > (CST - 1) if CST is positive.  */
  2874.       if (TREE_CODE (arg1) == INTEGER_CST
  2875.       && TREE_CODE (arg0) != INTEGER_CST
  2876.       && ! tree_int_cst_lt (arg1, integer_one_node))
  2877.     {
  2878.       switch (TREE_CODE (t))
  2879.         {
  2880.         case GE_EXPR:
  2881.           code = GT_EXPR;
  2882.           TREE_SET_CODE (t, code);
  2883.           arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
  2884.           TREE_OPERAND (t, 1) = arg1;
  2885.           break;
  2886.  
  2887.         case LT_EXPR:
  2888.           code = LE_EXPR;
  2889.           TREE_SET_CODE (t, code);
  2890.           arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
  2891.           TREE_OPERAND (t, 1) = arg1;
  2892.         }
  2893.     }
  2894.  
  2895.       /* If this is an EQ or NE comparison with zero and ARG0 is
  2896.      (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
  2897.      two operations, but the latter can be done in one less insn
  2898.      one machine that have only two-operand insns or on which a
  2899.      constant cannot be the first operand.  */
  2900.       if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
  2901.       && TREE_CODE (arg0) == BIT_AND_EXPR)
  2902.     {
  2903.       if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
  2904.           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
  2905.         return
  2906.           fold (build (code, type,
  2907.                build (BIT_AND_EXPR, TREE_TYPE (arg0),
  2908.                   build (RSHIFT_EXPR,
  2909.                      TREE_TYPE (TREE_OPERAND (arg0, 0)),
  2910.                      TREE_OPERAND (arg0, 1),
  2911.                      TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
  2912.                   convert (TREE_TYPE (arg0),
  2913.                        integer_one_node)),
  2914.                arg1));
  2915.       else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
  2916.            && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
  2917.         return
  2918.           fold (build (code, type,
  2919.                build (BIT_AND_EXPR, TREE_TYPE (arg0),
  2920.                   build (RSHIFT_EXPR,
  2921.                      TREE_TYPE (TREE_OPERAND (arg0, 1)),
  2922.                      TREE_OPERAND (arg0, 0),
  2923.                      TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
  2924.                   convert (TREE_TYPE (arg0),
  2925.                        integer_one_node)),
  2926.                arg1));
  2927.     }
  2928.  
  2929.       /* If this is an NE comparison of zero with an AND of one, remove the
  2930.      comparison since the AND will give the correct value.  */
  2931.       if (code == NE_EXPR && integer_zerop (arg1)
  2932.       && TREE_CODE (arg0) == BIT_AND_EXPR
  2933.       && integer_onep (TREE_OPERAND (arg0, 1)))
  2934.     return convert (type, arg0);
  2935.  
  2936.       /* If we have (A & C) == C where C is a power of 2, convert this into
  2937.      (A & C) != 0.  Similarly for NE_EXPR.  */
  2938.       if ((code == EQ_EXPR || code == NE_EXPR)
  2939.       && TREE_CODE (arg0) == BIT_AND_EXPR
  2940.       && integer_pow2p (TREE_OPERAND (arg0, 1))
  2941.       && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
  2942.     return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
  2943.               arg0, integer_zero_node);
  2944.  
  2945.       /* Simplify comparison of an integer with itself.
  2946.      (This may not be safe with IEEE floats if they are nans.)  */
  2947.       if (operand_equal_p (arg0, arg1, 0)
  2948.       && TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE)
  2949.     {
  2950.       switch (code)
  2951.         {
  2952.         case EQ_EXPR:
  2953.         case GE_EXPR:
  2954.         case LE_EXPR:
  2955.           t = build_int_2 (1, 0);
  2956.           TREE_TYPE (t) = type;
  2957.           return t;
  2958.         case NE_EXPR:
  2959.         case GT_EXPR:
  2960.         case LT_EXPR:
  2961.           t = build_int_2 (0, 0);
  2962.           TREE_TYPE (t) = type;
  2963.           return t;
  2964.         }
  2965.     }
  2966.  
  2967.       /* An unsigned comparison against 0 can be simplified.  */
  2968.       if (integer_zerop (arg1)
  2969.       && (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
  2970.           || TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE)
  2971.       && TREE_UNSIGNED (TREE_TYPE (arg1)))
  2972.     {
  2973.       switch (TREE_CODE (t))
  2974.         {
  2975.         case GT_EXPR:
  2976.           TREE_SET_CODE (t, NE_EXPR);
  2977.           break;
  2978.         case LE_EXPR:
  2979.           TREE_SET_CODE (t, EQ_EXPR);
  2980.           break;
  2981.         case GE_EXPR:
  2982.           return omit_one_operand (integer_type_node,
  2983.                        integer_one_node, arg0);
  2984.         case LT_EXPR:
  2985.           return omit_one_operand (integer_type_node,
  2986.                        integer_zero_node, arg0);
  2987.         }
  2988.     }
  2989.  
  2990.       /* To compute GT, swap the arguments and do LT.
  2991.      To compute GE, do LT and invert the result.
  2992.      To compute LE, swap the arguments, do LT and invert the result.
  2993.      To compute NE, do EQ and invert the result.  */
  2994.       if (code == LE_EXPR || code == GT_EXPR)
  2995.     {
  2996.       register tree temp = arg0;
  2997.       arg0 = arg1;
  2998.       arg1 = temp;
  2999.     }
  3000.  
  3001.       /* Compute a result for LT or EQ if args permit;
  3002.      otherwise return T.  */
  3003.       if (TREE_CODE (arg0) == INTEGER_CST
  3004.       && TREE_CODE (arg1) == INTEGER_CST)
  3005.     {
  3006.       if (code == EQ_EXPR || code == NE_EXPR)
  3007.         t = build_int_2
  3008.           (TREE_INT_CST_LOW (arg0) == TREE_INT_CST_LOW (arg1)
  3009.            && TREE_INT_CST_HIGH (arg0) == TREE_INT_CST_HIGH (arg1),
  3010.            0);
  3011.       else
  3012.         t = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
  3013.                   ? INT_CST_LT_UNSIGNED (arg0, arg1)
  3014.                   : INT_CST_LT (arg0, arg1)),
  3015.                  0);
  3016.     }
  3017.       /* Assume a nonexplicit constant cannot equal an explicit one,
  3018.      since such code would be undefined anyway.
  3019.      Exception: on sysvr4, using #pragma weak,
  3020.      a label can come out as 0.  */
  3021.       else if (TREE_CODE (arg1) == INTEGER_CST
  3022.            && !integer_zerop (arg1)
  3023.            && TREE_CONSTANT (arg0)
  3024.            && TREE_CODE (arg0) == ADDR_EXPR
  3025.            && (code == EQ_EXPR || code == NE_EXPR))
  3026.     {
  3027.       t = build_int_2 (0, 0);
  3028.     }
  3029.       /* Two real constants can be compared explicitly.  */
  3030.       else if (TREE_CODE (arg0) == REAL_CST
  3031.            && TREE_CODE (arg1) == REAL_CST)
  3032.     {
  3033.       if (code == EQ_EXPR || code == NE_EXPR)
  3034.         t = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
  3035.                         TREE_REAL_CST (arg1)),
  3036.                  0);
  3037.       else
  3038.         t = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0),
  3039.                            TREE_REAL_CST (arg1)),
  3040.                  0);
  3041.     }
  3042.       else if (optimize && (TREE_CODE (arg0) == COMPONENT_REF
  3043.                 || TREE_CODE (arg0) == BIT_FIELD_REF)
  3044.            && (code == EQ_EXPR || code == NE_EXPR))
  3045.     {
  3046.       tree tem = optimize_bit_field_compare (code, type, arg0, arg1);
  3047.       return tem ? tem : t;
  3048.     }
  3049.  
  3050.       /* If what we want is other than LT or EQ, invert the result.  */
  3051.       if (code == GE_EXPR || code == LE_EXPR || code == NE_EXPR)
  3052.     TREE_INT_CST_LOW (t) ^= 1;
  3053.       TREE_TYPE (t) = type;
  3054.       return t;
  3055.  
  3056.     case COND_EXPR:
  3057.       if (TREE_CODE (arg0) == INTEGER_CST)
  3058.     return TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1));
  3059.       else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
  3060.     return omit_one_operand (type, arg1, arg0);
  3061. #ifdef NeXT
  3062.       else if (integer_onep (TREE_OPERAND (t, 1))
  3063.            && integer_zerop (TREE_OPERAND (t, 2))
  3064.            /* If we try to convert TREE_OPERAND (t, 0) to our type, the
  3065.           call to fold will try to move the conversion inside 
  3066.           a COND, which will recurse.  In that case, the COND_EXPR
  3067.           is probably the best choice, so leave it alone.  */
  3068.            && type == TREE_TYPE (TREE_OPERAND (t, 0)))
  3069. #else /* NeXT */
  3070.       else if (integer_onep (TREE_OPERAND (t, 1))
  3071.            && integer_zerop (TREE_OPERAND (t, 2)))
  3072. #endif /* NeXT */
  3073.     return TREE_OPERAND (t, 0);
  3074.       else if (integer_zerop (TREE_OPERAND (t, 1))
  3075.            && integer_onep (TREE_OPERAND (t, 2)))
  3076.     return invert_truthvalue (TREE_OPERAND (t, 0));
  3077.  
  3078.       /* Look for a more general case of the above, namely when we are
  3079.      comparing some expression A for equality with zero and the result
  3080.      is to be zero if A is zero.  In that case, check to see if the value
  3081.      of A is the same as the value to be returned when A is non-zero.
  3082.  
  3083.      There are two cases:  One is where we have (A ? A : 0) and the
  3084.      other is when a single bit is tested (e.g., A & 2 ? 2 : 0).
  3085.      In these cases, the result of the conditional is simply A. 
  3086.  
  3087.      Start by setting ARG1 to be the true value and ARG0 to be the thing
  3088.      compared with zero.  Then check for the two cases above.  */
  3089.  
  3090.       if (integer_zerop (TREE_OPERAND (t, 2))
  3091.       && TREE_CODE (arg0) == NE_EXPR
  3092.       && integer_zerop (TREE_OPERAND (arg0, 1))
  3093.       && ! TREE_SIDE_EFFECTS (arg1))
  3094.     ;
  3095.       else if (integer_zerop (arg1)
  3096.            && TREE_CODE (arg0) == EQ_EXPR
  3097.            && integer_zerop (TREE_OPERAND (arg0, 1))
  3098.            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
  3099.     arg1 = TREE_OPERAND (t, 2);
  3100.       else
  3101.     return t;
  3102.  
  3103.       arg0 = TREE_OPERAND (arg0, 0);
  3104.  
  3105.       STRIP_NOPS (arg1);
  3106.       if (operand_equal_p (arg0, arg1, 0)
  3107.       || (TREE_CODE (arg1) == INTEGER_CST
  3108.           && integer_pow2p (arg1)
  3109.           && TREE_CODE (arg0) == BIT_AND_EXPR
  3110.           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)))
  3111.     return arg0;
  3112.       return t;
  3113.  
  3114.     case COMPOUND_EXPR:
  3115.       if (!TREE_SIDE_EFFECTS (arg0))
  3116.     return arg1;
  3117.       return t;
  3118.  
  3119.     default:
  3120.       return t;
  3121.     } /* switch (code) */
  3122. }
  3123.