home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cplusplus-8 / expmed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-21  |  58.9 KB  |  1,864 lines

  1. /* Medium-level subroutines: convert bit-field store and extract
  2.    and shifts, multiplies and divides to rtl instructions.
  3.    Copyright (C) 1987, 1988, 1989 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. #include "config.h"
  23. #include "rtl.h"
  24. #include "tree.h"
  25. #include "flags.h"
  26. #include "insn-flags.h"
  27. #include "insn-codes.h"
  28. #include "insn-config.h"
  29. #include "expr.h"
  30. #include "recog.h"
  31.  
  32. static rtx extract_split_bit_field ();
  33. static rtx extract_fixed_bit_field ();
  34. static void store_split_bit_field ();
  35. static void store_fixed_bit_field ();
  36.  
  37. /* Return an rtx representing minus the value of X.
  38.    MODE is the intended mode of the result,
  39.    useful if X is a CONST_INT.  */
  40.  
  41. rtx
  42. negate_rtx (mode, x)
  43.      enum machine_mode mode;
  44.      rtx x;
  45. {
  46.   if (GET_CODE (x) == CONST_INT)
  47.     {
  48.       int val = - INTVAL (x);
  49.       if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_INT)
  50.     {
  51.       /* Sign extend the value from the bits that are significant.  */
  52.       if (val & (1 << (GET_MODE_BITSIZE (mode) - 1)))
  53.         val |= (-1) << GET_MODE_BITSIZE (mode);
  54.       else
  55.         val &= (1 << GET_MODE_BITSIZE (mode)) - 1;
  56.     }
  57.       return gen_rtx (CONST_INT, VOIDmode, val);
  58.     }
  59.   else
  60.     return expand_unop (GET_MODE (x), neg_optab, x, 0, 0);
  61. }
  62.  
  63. /* Generate code to store value from rtx VALUE
  64.    into a bit-field within structure STR_RTX
  65.    containing BITSIZE bits starting at bit BITNUM.
  66.    FIELDMODE is the machine-mode of the FIELD_DECL node for this field.
  67.    ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
  68.    TOTAL_SIZE is the size of the structure in bytes, or -1 if unknown.  */
  69.  
  70. rtx
  71. store_bit_field (str_rtx, bitsize, bitnum, fieldmode, value, align, total_size)
  72.      rtx str_rtx;
  73.      register int bitsize;
  74.      int bitnum;
  75.      enum machine_mode fieldmode;
  76.      rtx value;
  77.      int align;
  78.      int total_size;
  79. {
  80.   int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
  81.   register int offset = bitnum / unit;
  82.   register int bitpos = bitnum % unit;
  83.   register rtx op0 = str_rtx;
  84.   rtx value1;
  85.  
  86.   /* At this point, BITPOS counts within UNIT for a memref.
  87.      For a register or a subreg, it actually counts within the width
  88.      of the mode of OP0.  However, BITNUM never exceeds that width,
  89.      so the % operation above never really does anything.
  90.  
  91.      We will adjust BITPOS later to count properly within UNIT
  92.      in the case of a register.  */
  93.  
  94.   /* Discount the part of the structure before the desired byte.
  95.      We need to know how many bytes are safe to reference after it.  */
  96.   if (total_size >= 0)
  97.     total_size -= (bitpos / BIGGEST_ALIGNMENT
  98.            * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
  99.  
  100.   while (GET_CODE (op0) == SUBREG)
  101.     {
  102. #ifdef BYTES_BIG_ENDIAN
  103.       /* Keep BITPOS counting within the size of op0.  */
  104.       bitpos += (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
  105.          - GET_MODE_BITSIZE (GET_MODE (op0)));
  106. #endif
  107.       offset += SUBREG_WORD (op0);
  108.       op0 = SUBREG_REG (op0);
  109.     }
  110.  
  111.   value = protect_from_queue (value, 0);
  112.  
  113.   if (flag_force_mem)
  114.     value = force_not_mem (value);
  115.  
  116.   if (GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD
  117.       && GET_MODE_BITSIZE (fieldmode) == bitsize
  118.       && bitpos % BITS_PER_WORD == 0
  119.       && GET_CODE (op0) == REG)
  120.     {
  121.       /* Storing in a full-word or multi-word field in a register
  122.      can be done with just SUBREG.  */
  123.       if (GET_MODE (op0) != fieldmode)
  124.     op0 = gen_rtx (SUBREG, fieldmode, op0, offset);
  125.       emit_move_insn (op0, value);
  126.       return value;
  127.     }
  128.  
  129. #ifdef BYTES_BIG_ENDIAN
  130.   /* If OP0 is a register, BITPOS must count within UNIT, which should be SI.
  131.      But as we have it, it counts within whatever size OP0 now has.
  132.      These are not the same, so convert if big-endian.  */
  133.   if (GET_CODE (op0) != MEM && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
  134.     {
  135.       bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
  136.       /* Change the mode now so we don't adjust BITPOS again.  */
  137.       if (GET_CODE (op0) == SUBREG)
  138.     PUT_MODE (op0, SImode);
  139.       else
  140.     op0 = gen_rtx (SUBREG, SImode, op0, 0);
  141.     }
  142. #endif
  143.  
  144.   /* Storing an lsb-aligned field in a register
  145.      can be done with a movestrict instruction.  */
  146.  
  147.   if (GET_CODE (op0) != MEM
  148. #ifdef BYTES_BIG_ENDIAN
  149.       && bitpos + bitsize == unit
  150. #else
  151.       && bitpos == 0
  152. #endif
  153.       && (GET_MODE (op0) == fieldmode
  154.       || (movstrict_optab->handlers[(int) fieldmode].insn_code
  155.           != CODE_FOR_nothing)))
  156.     {
  157.       /* Get appropriate low part of the value being stored.  */
  158.       if (GET_CODE (value) == CONST_INT || GET_CODE (value) == REG)
  159.     value = gen_lowpart (fieldmode, value);
  160.       else if (!(GET_CODE (value) == SYMBOL_REF
  161.          || GET_CODE (value) == LABEL_REF
  162.          || GET_CODE (value) == CONST))
  163.     value = convert_to_mode (fieldmode, value, 0);
  164.  
  165.       if (GET_MODE (op0) == fieldmode)
  166.     emit_move_insn (op0, value);
  167.       else
  168.     {
  169.       if (GET_CODE (op0) == SUBREG)
  170.         PUT_MODE (op0, fieldmode);
  171.       else
  172.         op0 = gen_rtx (SUBREG, fieldmode, op0, offset);
  173.       emit_insn (GEN_FCN (movstrict_optab->handlers[(int) fieldmode].insn_code)
  174.              (op0, value));
  175.     }
  176.  
  177.       return value;
  178.     }
  179.  
  180.   /* Handle fields bigger than a word.  */
  181.  
  182.   if (bitsize > BITS_PER_WORD)
  183.     {
  184.       int low_size = BITS_PER_WORD;
  185.       int low_pos = bitpos + offset * unit;
  186.       int high_size = bitsize - low_size;
  187.       int high_pos;
  188. #ifdef BYTES_BIG_ENDIAN
  189.       high_pos = low_pos;
  190.       low_pos += high_size;
  191. #else
  192.       high_pos = low_pos + low_size;
  193. #endif
  194.  
  195.       value = force_reg (GET_MODE (value), value); 
  196.       store_bit_field (op0, low_size, low_pos, SImode,
  197.                gen_lowpart (SImode, value), align, total_size);
  198.       store_bit_field (op0, high_size, high_pos, SImode,
  199.                gen_highpart (SImode, value), align, total_size);
  200.       return value;
  201.     }
  202.  
  203.   /* From here on we can assume that the field to be stored in is an integer,
  204.      since it is shorter than a word.  */
  205.  
  206.   /* OFFSET is the number of words or bytes (UNIT says which)
  207.      from STR_RTX to the first word or byte containing part of the field.  */
  208.  
  209.   if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
  210.     {
  211.       /* If not in memory, merge in the offset now.  */
  212.       if (offset != 0
  213.       || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (SImode))
  214.     {
  215.       if (GET_CODE (op0) == SUBREG)
  216.         SUBREG_WORD (op0) += offset;
  217.       else
  218.         op0 = gen_rtx (SUBREG, SImode, op0, offset);
  219.     }
  220.       offset = 0;
  221.     }
  222.   else
  223.     {
  224.       op0 = protect_from_queue (op0, 1);
  225.     }
  226.  
  227.   /* Now OFFSET is nonzero only if OP0 is memory
  228.      and is therefore always measured in bytes.  */
  229.  
  230. #ifdef HAVE_insv
  231.   if (HAVE_insv
  232.       && !(bitsize == 1 && GET_CODE (value) == CONST_INT))
  233.     {
  234.       int xbitpos = bitpos;
  235.       rtx xop0 = op0;
  236.       rtx last = get_last_insn ();
  237.       rtx pat;
  238.  
  239.       /* If this machine's insv can only insert into a register,
  240.      copy OP0 into a register and save it back later.  */
  241.       if (GET_CODE (op0) == MEM
  242.       && ! (*insn_operand_predicate[(int) CODE_FOR_insv][0]) (op0, VOIDmode))
  243.     {
  244.       rtx tempreg;
  245.       enum machine_mode trymode, bestmode = VOIDmode, insn_mode;
  246.       /* Don't use a mode bigger than the one of the value to be stored.
  247.          That mode must be okay, since a bit field can be that big.  */
  248.       int maxsize
  249.         = GET_MODE_SIZE (insn_operand_mode[(int) CODE_FOR_insv][3]);
  250.       /* This used to use the mode desired for operand 0,
  251.          but that is normally QImode on most machines,
  252.          and QImode won't work for fields that cross byte
  253.          boundaries.  */
  254.  
  255.       /* Also don't use a mode bigger than the structure.  */
  256.       if (total_size >= 0 && maxsize > total_size)
  257.         maxsize = total_size;
  258.  
  259.       /* Find biggest machine mode we can safely use
  260.          to fetch from this structure.
  261.          But don't use a bigger mode than the insn wants.  */
  262.       for (trymode = QImode;
  263.            trymode && GET_MODE_SIZE (trymode) <= maxsize;
  264.            trymode = GET_MODE_WIDER_MODE (trymode))
  265.         if (GET_MODE_SIZE (trymode) <= align
  266.         || align == BIGGEST_ALIGNMENT / BITS_PER_UNIT)
  267.           bestmode = trymode;
  268.       if (! bestmode)
  269.         abort ();
  270.       /* Adjust address to point to the containing unit of that mode.  */
  271.       unit = GET_MODE_BITSIZE (bestmode);
  272.       /* Compute offset as multiple of this unit, counting in bytes.  */
  273.       offset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
  274.       bitpos = bitnum % unit;
  275.       op0 = change_address (op0, bestmode, 
  276.                 plus_constant (XEXP (op0, 0), offset));
  277.  
  278.       /* Fetch that unit, store the bitfield in it, then store the unit.  */
  279.       tempreg = copy_to_reg (op0);
  280.       /* To actually store in TEMPREG,
  281.          look at it in the mode this insn calls for.
  282.          (Probably SImode.)  */
  283.       insn_mode = SImode;
  284. #ifdef BYTES_BIG_ENDIAN
  285.       if (GET_MODE_BITSIZE (insn_mode) > unit)
  286.         bitpos += GET_MODE_BITSIZE (insn_mode) - unit;
  287. #endif
  288.       store_bit_field (gen_rtx (SUBREG, insn_mode, tempreg, 0),
  289.                bitsize, bitpos, fieldmode, value,
  290.                align, total_size);
  291.       emit_move_insn (op0, tempreg);
  292.       return value;
  293.     }
  294.  
  295.       /* Add OFFSET into OP0's address.  */
  296.       if (GET_CODE (xop0) == MEM)
  297.     xop0 = change_address (xop0, QImode,
  298.                    plus_constant (XEXP (xop0, 0), offset));
  299.  
  300.       /* If xop0 is a register, we need it in SImode
  301.      to make it acceptable to the format of insv.  */
  302.       if (GET_CODE (xop0) == SUBREG)
  303.     PUT_MODE (xop0, SImode);
  304.       if (GET_CODE (xop0) == REG && GET_MODE (xop0) != SImode)
  305.     {
  306. #ifdef BYTES_BIG_ENDIAN
  307.       xbitpos += (GET_MODE_BITSIZE (SImode)
  308.               - GET_MODE_BITSIZE (GET_MODE (xop0)));
  309. #endif
  310.       xop0 = gen_rtx (SUBREG, SImode, xop0, 0);
  311.     }
  312.  
  313.       /* Convert VALUE to SImode (which insv insn wants) in VALUE1.  */
  314.       value1 = value;
  315.       if (GET_MODE (value) != SImode)
  316.     {
  317.       if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
  318.         {
  319.           /* Optimization: Don't bother really extending VALUE
  320.          if it has all the bits we will actually use.  */
  321.  
  322.           /* Avoid making subreg of a subreg, or of a mem.  */
  323.           if (GET_CODE (value1) != REG)
  324.         value1 = copy_to_reg (value1);
  325.           value1 = gen_rtx (SUBREG, SImode, value1, 0);
  326.         }
  327.       else if (!CONSTANT_P (value))
  328.         /* Parse phase is supposed to make VALUE's data type
  329.            match that of the component reference, which is a type
  330.            at least as wide as the field; so VALUE should have
  331.            a mode that corresponds to that type.  */
  332.         abort ();
  333.     }
  334.  
  335.       /* If this machine's insv insists on a register,
  336.      get VALUE1 into a register.  */
  337.       if (! (*insn_operand_predicate[(int) CODE_FOR_insv][3]) (value1, SImode))
  338.     value1 = force_reg (SImode, value1);
  339.  
  340.       /* On big-endian machines, we count bits from the most significant.
  341.      If the bit field insn does not, we must invert.  */
  342.  
  343. #if defined (BITS_BIG_ENDIAN) != defined (BYTES_BIG_ENDIAN)
  344.       xbitpos = unit - 1 - xbitpos;
  345. #endif
  346.  
  347.       pat = gen_insv (xop0,
  348.               gen_rtx (CONST_INT, VOIDmode, bitsize),
  349.               gen_rtx (CONST_INT, VOIDmode, xbitpos),
  350.               value1);
  351.       if (pat)
  352.     emit_insn (pat);
  353.       else
  354.         {
  355.       delete_insns_since (last);
  356.       store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
  357.     }
  358.     }
  359.   else
  360. #endif
  361.     /* Insv is not available; store using shifts and boolean ops.  */
  362.     store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
  363.   return value;
  364. }
  365.  
  366. /* Use shifts and boolean operations to store VALUE
  367.    into a bit field of width BITSIZE
  368.    in a memory location specified by OP0 except offset by OFFSET bytes.
  369.      (OFFSET must be 0 if OP0 is a register.)
  370.    The field starts at position BITPOS within the byte.
  371.     (If OP0 is a register, it may be SImode or a narrower mode,
  372.      but BITPOS still counts within a full word,
  373.      which is significant on bigendian machines.)
  374.    STRUCT_ALIGN is the alignment the structure is known to have (in bytes).
  375.  
  376.    Note that protect_from_queue has already been done on OP0 and VALUE.  */
  377.  
  378. static void
  379. store_fixed_bit_field (op0, offset, bitsize, bitpos, value, struct_align)
  380.      register rtx op0;
  381.      register int offset, bitsize, bitpos;
  382.      register rtx value;
  383.      int struct_align;
  384. {
  385.   register enum machine_mode mode;
  386.   int total_bits = BITS_PER_WORD;
  387.   rtx subtarget;
  388.   int all_zero = 0;
  389.   int all_one = 0;
  390.  
  391.   /* Add OFFSET to OP0's address (if it is in memory)
  392.      and if a single byte contains the whole bit field
  393.      change OP0 to a byte.  */
  394.  
  395.   /* There is a case not handled here:
  396.      a structure with a known alignment of just a halfword
  397.      and a field split across two aligned halfwords within the structure.
  398.      Or likewise a structure with a known alignment of just a byte
  399.      and a field split across two bytes.
  400.      Such cases are not supposed to be able to occur.  */
  401.  
  402.   if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
  403.     {
  404.       if (offset != 0)
  405.     abort ();
  406.       /* Special treatment for a bit field split across two registers.  */
  407.       if (bitsize + bitpos > BITS_PER_WORD)
  408.     {
  409.       store_split_bit_field (op0, bitsize, bitpos, value, BITS_PER_WORD);
  410.       return;
  411.     }
  412.     }
  413.   else if (bitsize + bitpos <= BITS_PER_UNIT
  414.        && (! SLOW_BYTE_ACCESS
  415.            || (struct_align == 1
  416.            && BIGGEST_ALIGNMENT > 1)))
  417.     {
  418.       /* It fits in one byte, and either bytes are fast
  419.      or the alignment won't let us use anything bigger.  */
  420.       total_bits = BITS_PER_UNIT;
  421.       op0 = change_address (op0, QImode, 
  422.                 plus_constant (XEXP (op0, 0), offset));
  423.     }
  424.   else if ((bitsize + bitpos + (offset % GET_MODE_SIZE (HImode)) * BITS_PER_UNIT
  425.         <= GET_MODE_BITSIZE (HImode))
  426.        /* If halfwords are fast, use them whenever valid.  */
  427.        && (! SLOW_BYTE_ACCESS
  428.            /* Use halfwords if larger is invalid due to alignment.  */
  429.            || (struct_align == GET_MODE_SIZE (HImode)
  430.            && BIGGEST_ALIGNMENT > GET_MODE_SIZE (HImode))))
  431.     {
  432.       /* It fits in an aligned halfword within the structure,
  433.      and either halfwords are fast
  434.      or the alignment won't let us use anything bigger.  */
  435.       total_bits = GET_MODE_BITSIZE (HImode);
  436.  
  437.       /* Get ref to halfword containing the field.  */
  438.       bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
  439.       offset -= (offset % (total_bits / BITS_PER_UNIT));
  440.       op0 = change_address (op0, HImode, 
  441.                 plus_constant (XEXP (op0, 0), offset));
  442.     }
  443.   else
  444.     {
  445.       /* Get ref to an aligned word containing the field.  */
  446.       /* Adjust BITPOS to be position within a word,
  447.      and OFFSET to be the offset of that word.
  448.      Then alter OP0 to refer to that word.  */
  449.       bitpos += (offset % (BITS_PER_WORD / BITS_PER_UNIT)) * BITS_PER_UNIT;
  450.       offset -= (offset % (BITS_PER_WORD / BITS_PER_UNIT));
  451.       op0 = change_address (op0, SImode,
  452.                 plus_constant (XEXP (op0, 0), offset));
  453.  
  454.       /* Special treatment for a bit field split across two aligned words.  */
  455.       if (bitsize + bitpos > BITS_PER_WORD)
  456.     {
  457.       store_split_bit_field (op0, bitsize, bitpos, value, struct_align);
  458.       return;
  459.     }
  460.     }
  461.  
  462.   mode = GET_MODE (op0);
  463.  
  464.   /* Now MODE is either QImode, HImode or SImode for a MEM as OP0,
  465.      or is SImode for a REG as OP0.  TOTAL_BITS corresponds.
  466.      The bit field is contained entirely within OP0.
  467.      BITPOS is the starting bit number within OP0.
  468.      (OP0's mode may actually be narrower than MODE.)  */
  469.  
  470. #ifdef BYTES_BIG_ENDIAN
  471.   /* BITPOS is the distance between our msb
  472.      and that of the containing datum.
  473.      Convert it to the distance from the lsb.  */
  474.  
  475.   bitpos = total_bits - bitsize - bitpos;
  476. #endif
  477.   /* Now BITPOS is always the distance between our lsb
  478.      and that of OP0.  */
  479.  
  480.   /* Shift VALUE left by BITPOS bits.  If VALUE is not constant,
  481.      we must first convert its mode to MODE.  */
  482.  
  483.   if (GET_CODE (value) == CONST_INT)
  484.     {
  485.       register int v = INTVAL (value);
  486.  
  487.       if (bitsize < HOST_BITS_PER_INT)
  488.     v &= (1 << bitsize) - 1;
  489.  
  490.       if (v == 0)
  491.     all_zero = 1;
  492.       else if (bitsize < HOST_BITS_PER_INT && v == (1 << bitsize) - 1)
  493.     all_one = 1;
  494.  
  495.       value = gen_rtx (CONST_INT, VOIDmode, v << bitpos);
  496.     }
  497.   else
  498.     {
  499.       int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize);
  500.  
  501.       if (GET_MODE (value) != mode)
  502.     {
  503.       if ((GET_CODE (value) == REG || GET_CODE (value) == SUBREG)
  504.           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (value)))
  505.         value = gen_lowpart (mode, value);
  506.       else
  507.         value = convert_to_mode (mode, value, 1);
  508.     }
  509.  
  510.       if (must_and && bitsize < HOST_BITS_PER_INT)
  511.     value = expand_bit_and (mode, value,
  512.                 gen_rtx (CONST_INT, VOIDmode,
  513.                      (1 << bitsize) - 1),
  514.                 0);
  515.       if (bitpos > 0)
  516.     value = expand_shift (LSHIFT_EXPR, mode, value,
  517.                   build_int_2 (bitpos, 0), 0, 1);
  518.     }
  519.  
  520.   /* Now clear the chosen bits in OP0,
  521.      except that if VALUE is -1 we need not bother.  */
  522.  
  523.   subtarget = op0;
  524.  
  525.   if (! all_one)
  526.     subtarget = expand_bit_and (mode, op0,
  527.                 gen_rtx (CONST_INT, VOIDmode, 
  528.                      (~ (((unsigned) ~0
  529.                           >> (HOST_BITS_PER_INT - bitsize))
  530.                          << bitpos))
  531.                      & ((GET_MODE_BITSIZE (mode)
  532.                          == HOST_BITS_PER_INT)
  533.                         ? -1
  534.                         : ((1 << GET_MODE_BITSIZE (mode)) - 1))),
  535.                 subtarget);
  536.  
  537.   /* Now logical-or VALUE into OP0, unless it is zero.  */
  538.  
  539.   if (! all_zero)
  540.     subtarget = expand_binop (mode, ior_optab, subtarget, value,
  541.                   op0, 1, OPTAB_LIB_WIDEN);
  542.   if (op0 != subtarget)
  543.     emit_move_insn (op0, subtarget);
  544. }
  545.  
  546. /* Store a bit field that is split across two words.
  547.  
  548.    OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
  549.    BITSIZE is the field width; BITPOS the position of its first bit
  550.    (within the word).
  551.    VALUE is the value to store.  */
  552.  
  553. static void
  554. store_split_bit_field (op0, bitsize, bitpos, value, align)
  555.      rtx op0;
  556.      int bitsize, bitpos;
  557.      rtx value;
  558.      int align;
  559. {
  560.   /* BITSIZE_1 is size of the part in the first word.  */
  561.   int bitsize_1 = BITS_PER_WORD - bitpos;
  562.   /* BITSIZE_2 is size of the rest (in the following word).  */
  563.   int bitsize_2 = bitsize - bitsize_1;
  564.   rtx part1, part2;
  565.  
  566.   /* Alignment of VALUE, after conversion.  */
  567.   int valalign = GET_MODE_SIZE (SImode);
  568.  
  569.   if (GET_MODE (value) != VOIDmode)
  570.     value = convert_to_mode (SImode, value, 1);
  571.   if (CONSTANT_P (value) && GET_CODE (value) != CONST_INT)
  572.     value = copy_to_reg (value);
  573.  
  574.   /* Split the value into two parts:
  575.      PART1 gets that which goes in the first word; PART2 the other.  */
  576. #ifdef BYTES_BIG_ENDIAN
  577.   /* PART1 gets the more significant part.  */
  578.   if (GET_CODE (value) == CONST_INT)
  579.     {
  580.       part1 = gen_rtx (CONST_INT, VOIDmode,
  581.                (unsigned) (INTVAL (value)) >> bitsize_2);
  582.       part2 = gen_rtx (CONST_INT, VOIDmode,
  583.                (unsigned) (INTVAL (value)) & ((1 << bitsize_2) - 1));
  584.     }
  585.   else
  586.     {
  587.       part1 = extract_fixed_bit_field (SImode, value, 0, bitsize_1,
  588.                        BITS_PER_WORD - bitsize, 0, 1, valalign);
  589.       part2 = extract_fixed_bit_field (SImode, value, 0, bitsize_2,
  590.                        BITS_PER_WORD - bitsize_2, 0, 1, valalign);
  591.     }
  592. #else
  593.   /* PART1 gets the less significant part.  */
  594.   if (GET_CODE (value) == CONST_INT)
  595.     {
  596.       part1 = gen_rtx (CONST_INT, VOIDmode,
  597.                (unsigned) (INTVAL (value)) & ((1 << bitsize_1) - 1));
  598.       part2 = gen_rtx (CONST_INT, VOIDmode,
  599.                (unsigned) (INTVAL (value)) >> bitsize_1);
  600.     }
  601.   else
  602.     {
  603.       part1 = extract_fixed_bit_field (SImode, value, 0, bitsize_1, 0,
  604.                        0, 1, valalign);
  605.       part2 = extract_fixed_bit_field (SImode, value, 0, bitsize_2,
  606.                        bitsize_1, 0, 1, valalign);
  607.     }
  608. #endif
  609.  
  610.   /* Store PART1 into the first word.  */
  611.   store_fixed_bit_field (op0, 0, bitsize_1, bitpos, part1, align);
  612.  
  613.   /* Offset op0 to get to the following word.  */
  614.   if (GET_CODE (op0) == MEM)
  615.     op0 = change_address (op0, SImode,
  616.               plus_constant (XEXP (op0, 0), UNITS_PER_WORD));
  617.   else if (GET_CODE (op0) == REG)
  618.     op0 = gen_rtx (SUBREG, SImode, op0, 1);
  619.   else if (GET_CODE (op0) == SUBREG)
  620.     op0 = gen_rtx (SUBREG, SImode, SUBREG_REG (op0), SUBREG_WORD (op0) + 1);
  621.  
  622.   /* Store PART2 into the second word.  */
  623.   store_fixed_bit_field (op0, 0, bitsize_2, 0, part2, align);
  624. }
  625.  
  626. /* Generate code to extract a byte-field from STR_RTX
  627.    containing BITSIZE bits, starting at BITNUM,
  628.    and put it in TARGET if possible (if TARGET is nonzero).
  629.    Regardless of TARGET, we return the rtx for where the value is placed.
  630.    It may be a QUEUED.
  631.  
  632.    STR_RTX is the structure containing the byte (a REG or MEM).
  633.    UNSIGNEDP is nonzero if this is an unsigned bit field.
  634.    MODE is the natural mode of the field value once extracted.
  635.    TMODE is the mode the caller would like the value to have;
  636.    but the value may be returned with type MODE instead.
  637.  
  638.    ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
  639.    TOTAL_SIZE is the total size in bytes of the structure, if known.
  640.    Otherwise it is -1.
  641.  
  642.    If a TARGET is specified and we can store in it at no extra cost,
  643.    we do so, and return TARGET.
  644.    Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
  645.    if they are equally easy.  */
  646.  
  647. rtx
  648. extract_bit_field (str_rtx, bitsize, bitnum, unsignedp,
  649.            target, mode, tmode, align, total_size)
  650.      rtx str_rtx;
  651.      register int bitsize;
  652.      int bitnum;
  653.      int unsignedp;
  654.      rtx target;
  655.      enum machine_mode mode, tmode;
  656.      int align;
  657.      int total_size;
  658. {
  659.   int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
  660.   register int offset = bitnum / unit;
  661.   register int bitpos = bitnum % unit;
  662.   register rtx op0 = str_rtx;
  663.   rtx spec_target = target;
  664.   rtx bitsize_rtx, bitpos_rtx;
  665.   rtx spec_target_subreg = 0;
  666.  
  667.   /* Discount the part of the structure before the desired byte.
  668.      We need to know how many bytes are safe to reference after it.  */
  669.   if (total_size >= 0)
  670.     total_size -= (bitpos / BIGGEST_ALIGNMENT
  671.            * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
  672.  
  673.   if (tmode == VOIDmode)
  674.     tmode = mode;
  675.  
  676.   while (GET_CODE (op0) == SUBREG)
  677.     {
  678. #ifdef BYTES_BIG_ENDIAN
  679.       /* Keep BITPOS counting within the size of op0.  */
  680.       bitpos += (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
  681.          - GET_MODE_BITSIZE (GET_MODE (op0)));
  682. #endif
  683.       offset += SUBREG_WORD (op0);
  684.       op0 = SUBREG_REG (op0);
  685.     }
  686.   
  687. #ifdef BYTES_BIG_ENDIAN
  688.   /* If OP0 is a register, BITPOS must count within a word.
  689.      But as we have it, it counts within whatever size OP0 now has.
  690.      On a bigendian machine, these are not the same, so convert.  */
  691.   if (GET_CODE (op0) != MEM && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
  692.     {
  693.       bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
  694.       /* Change the mode now so we don't adjust BITPOS again.  */
  695.       if (GET_CODE (op0) == SUBREG)
  696.     PUT_MODE (op0, SImode);
  697.       else
  698.     op0 = gen_rtx (SUBREG, SImode, op0, 0);
  699.     }
  700. #endif
  701.  
  702.   /* Extracting a full-word or multi-word value
  703.      from a structure in a register.
  704.      This can be done with just SUBREG.
  705.      So too extracting a subword value in
  706.      the least significant part of the register.  */
  707.  
  708.   if (GET_CODE (op0) == REG
  709.       && ((bitsize >= BITS_PER_WORD && bitsize == GET_MODE_BITSIZE (mode)
  710.        && bitpos % BITS_PER_WORD == 0)
  711.       || ((bitsize == GET_MODE_BITSIZE (mode)
  712.            || bitsize == GET_MODE_BITSIZE (QImode)
  713.            || bitsize == GET_MODE_BITSIZE (HImode))
  714. #ifdef BYTES_BIG_ENDIAN
  715.           && bitpos + bitsize == BITS_PER_WORD
  716. #else
  717.           && bitpos == 0
  718. #endif
  719.           )))
  720.     {
  721.       enum machine_mode mode1 = mode;
  722.  
  723.       if (bitsize == GET_MODE_BITSIZE (QImode))
  724.     mode1 = QImode;
  725.       if (bitsize == GET_MODE_BITSIZE (HImode))
  726.     mode1 = HImode;
  727.  
  728.       if (mode1 != GET_MODE (op0))
  729.     {
  730.       if (GET_CODE (op0) == SUBREG)
  731.         PUT_MODE (op0, mode1);
  732.       else
  733.         op0 = gen_rtx (SUBREG, mode1, op0, offset);
  734.     }
  735.  
  736.       if (mode1 != mode)
  737.     return convert_to_mode (tmode, op0, unsignedp);
  738.       return op0;
  739.     }
  740.  
  741.   /* Handle fields bigger than a word.  */
  742.   
  743.   if (bitsize > BITS_PER_WORD)
  744.     {
  745.       int low_size = BITS_PER_WORD;
  746.       int low_pos = bitpos + offset * unit;
  747.       rtx target_low_part, low_part;
  748.       int high_size = bitsize - low_size;
  749.       int high_pos;
  750.       rtx target_high_part, high_part;
  751. #ifdef BYTES_BIG_ENDIAN
  752.       high_pos = low_pos;
  753.       low_pos += high_size;
  754. #else
  755.       high_pos = low_pos + low_size;
  756. #endif
  757.  
  758.       if (target == 0 || GET_CODE (target) != REG)
  759.     target = gen_reg_rtx (mode);
  760.  
  761.       /* Extract the low part of the bitfield, and make sure
  762.      to store it in the low part of TARGET.  */
  763.       target_low_part = gen_lowpart (SImode, target);
  764.       low_part = extract_bit_field (op0, low_size, low_pos, 1,
  765.                     target_low_part, SImode, SImode,
  766.                     align, total_size);
  767.       if (low_part != target_low_part)
  768.     emit_move_insn (target_low_part, low_part);
  769.  
  770.       /* Likewise for the high part.  */
  771.       target_high_part = gen_highpart (SImode, target);
  772.       high_part = extract_bit_field (op0, high_size, high_pos, unsignedp,
  773.                      target_high_part, SImode, SImode,
  774.                      align, total_size);
  775.       if (high_part != target_high_part)
  776.     emit_move_insn (target_high_part, high_part);
  777.  
  778.       return target;
  779.     }
  780.  
  781.   /* From here on we know the desired field is smaller than a word
  782.      so we can assume it is an integer.  So we can safely extract it as one
  783.      size of integer, if necessary, and then truncate or extend
  784.      to the size that is wanted.  */
  785.  
  786.   /* OFFSET is the number of words or bytes (UNIT says which)
  787.      from STR_RTX to the first word or byte containing part of the field.  */
  788.  
  789.   if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
  790.     {
  791.       /* If not in memory, merge in the offset now.  */
  792.       if (offset != 0
  793.       || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (SImode))
  794.     {
  795.       if (GET_CODE (op0) == SUBREG)
  796.         SUBREG_WORD (op0) += offset;
  797.       else
  798.         op0 = gen_rtx (SUBREG, SImode, op0, offset);
  799.     }
  800.       offset = 0;
  801.     }
  802.   else
  803.     {
  804.       op0 = protect_from_queue (str_rtx, 1);
  805.     }
  806.  
  807.   /* Now OFFSET is nonzero only for memory operands.  */
  808.  
  809.   if (unsignedp)
  810.     {
  811. #ifdef HAVE_extzv
  812.       if (HAVE_extzv)
  813.     {
  814.       int xbitpos = bitpos, xoffset = offset;
  815.       rtx last = get_last_insn();
  816.       rtx xop0 = op0;
  817.       rtx xtarget = target;
  818.       rtx xspec_target = spec_target;
  819.       rtx xspec_target_subreg = spec_target_subreg;
  820.       rtx pat;
  821.  
  822.       if (GET_CODE (xop0) == MEM)
  823.         {
  824.           /* Is the memory operand acceptable?  */
  825.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][1])
  826.              (xop0, GET_MODE (xop0))))
  827.         {
  828.           /* No, load into a reg and extract from there.  */
  829.           enum machine_mode bestmode = VOIDmode, trymode;
  830.           /* Don't use a mode bigger than the one of the value
  831.              to be fetched.  That mode must be okay,
  832.              since a bit field can be that big.  */
  833.           int maxsize
  834.             = GET_MODE_SIZE (insn_operand_mode[(int) CODE_FOR_extzv][0]);
  835.           /* This used to use the mode desired for operand 1,
  836.              but that is normally QImode on most machines,
  837.              and QImode won't work for fields that cross byte
  838.              boundaries.  */
  839.  
  840.           /* Also don't use a mode bigger than the structure.  */
  841.           if (total_size >= 0 && maxsize > total_size)
  842.             maxsize = total_size;
  843.  
  844.           /* Find biggest machine mode we can safely use
  845.              to fetch from this structure.
  846.              But don't use a bigger mode than the insn wants.  */
  847.           for (trymode = QImode;
  848.                trymode && GET_MODE_SIZE (trymode) <= maxsize;
  849.                trymode = GET_MODE_WIDER_MODE (trymode))
  850.             if (GET_MODE_SIZE (trymode) <= align
  851.             || align == BIGGEST_ALIGNMENT / BITS_PER_UNIT)
  852.               bestmode = trymode;
  853.           if (! bestmode)
  854.             abort ();
  855.           unit = GET_MODE_BITSIZE (bestmode);
  856.  
  857.           /* Compute offset as multiple of this unit,
  858.              counting in bytes.  */
  859.           xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
  860.           xbitpos = bitnum % unit;
  861.           xop0 = change_address (xop0, bestmode,
  862.                      plus_constant (XEXP (xop0, 0),
  863.                             xoffset));
  864.           /* Fetch it to a register in that size.  */
  865.           xop0 = force_reg (bestmode, xop0);
  866.  
  867.           /* Now ref the register in the mode extzv wants.  */
  868.           /* We used to use the mode from operand 1 in the md,
  869.              but that is often QImode because that's needed for MEM.
  870.              Here we need SImode instead.  */
  871.           if (bestmode != SImode)
  872.             xop0 = gen_rtx (SUBREG, SImode, xop0, 0);
  873. #ifdef BYTES_BIG_ENDIAN
  874.           if (GET_MODE_BITSIZE (GET_MODE (xop0)) > unit)
  875.             xbitpos += GET_MODE_BITSIZE (GET_MODE (xop0)) - unit;
  876. #endif
  877.         }
  878.           else
  879.         /* Get ref to first byte containing part of the field.  */
  880.         xop0 = change_address (xop0, QImode,
  881.                        plus_constant (XEXP (xop0, 0), xoffset));
  882.         }
  883.  
  884.       /* If op0 is a register, we need it in SImode
  885.          to make it acceptable to the format of extzv.  */
  886.       if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != SImode)
  887.         abort ();
  888.       if (GET_CODE (xop0) == REG && GET_MODE (xop0) != SImode)
  889.         {
  890. #ifdef BYTES_BIG_ENDIAN
  891.           xbitpos += (GET_MODE_BITSIZE (SImode)
  892.               - GET_MODE_BITSIZE (GET_MODE (xop0)));
  893. #endif
  894.           xop0 = gen_rtx (SUBREG, SImode, xop0, 0);
  895.         }
  896.  
  897.       if (xtarget == 0
  898.           || (flag_force_mem && GET_CODE (xtarget) == MEM))
  899.         xtarget = xspec_target = gen_reg_rtx (tmode);
  900.  
  901.       if (GET_MODE (xtarget) != SImode)
  902.         {
  903.           if (GET_CODE (xtarget) == REG)
  904.         xspec_target_subreg = xtarget = gen_lowpart (SImode, xtarget);
  905.           else
  906.         xtarget = gen_reg_rtx (SImode);
  907.         }
  908.  
  909.       /* If this machine's extzv insists on a register target,
  910.          make sure we have one.  */
  911.       if (! (*insn_operand_predicate[(int) CODE_FOR_extzv][0]) (xtarget, SImode))
  912.         xtarget = gen_reg_rtx (SImode);
  913.  
  914.       /* On big-endian machines, we count bits from the most significant.
  915.          If the bit field insn does not, we must invert.  */
  916. #if defined (BITS_BIG_ENDIAN) != defined (BYTES_BIG_ENDIAN)
  917.       xbitpos = unit - 1 - xbitpos;
  918. #endif
  919.  
  920.       bitsize_rtx = gen_rtx (CONST_INT, VOIDmode, bitsize);
  921.       bitpos_rtx = gen_rtx (CONST_INT, VOIDmode, xbitpos);
  922.  
  923.       pat = gen_extzv (protect_from_queue (xtarget, 1),
  924.                xop0, bitsize_rtx, bitpos_rtx);
  925.       if (pat)
  926.         {
  927.           emit_insn (pat);
  928.           target = xtarget;
  929.           spec_target = xspec_target;
  930.           spec_target_subreg = xspec_target_subreg;
  931.         }
  932.       else
  933.         {
  934.           delete_insns_since (last);
  935.           target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
  936.                         bitpos, target, 1, align);
  937.         }
  938.     }
  939.       else
  940. #endif
  941.     target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  942.                       target, 1, align);
  943.     }
  944.   else
  945.     {
  946. #ifdef HAVE_extv
  947.       if (HAVE_extv)
  948.     {
  949.       int xbitpos = bitpos, xoffset = offset;
  950.       rtx last = get_last_insn();
  951.       rtx xop0 = op0, xtarget = target;
  952.       rtx xspec_target = spec_target;
  953.       rtx xspec_target_subreg = spec_target_subreg;
  954.       rtx pat;
  955.  
  956.       if (GET_CODE (xop0) == MEM)
  957.         {
  958.           /* Is the memory operand acceptable?  */
  959.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extv][1])
  960.              (xop0, GET_MODE (xop0))))
  961.         {
  962.           /* No, load into a reg and extract from there.  */
  963.           enum machine_mode bestmode = VOIDmode, trymode;
  964.           /* Don't use a mode bigger than the one of the value
  965.              to be fetched.  That mode must be okay,
  966.              since a bit field can be that big.  */
  967.           int maxsize
  968.             = GET_MODE_SIZE (insn_operand_mode[(int) CODE_FOR_extv][0]);
  969.           /* This used to use the mode desired for operand 1,
  970.              but that is normally QImode on most machines,
  971.              and QImode won't work for fields that cross byte
  972.              boundaries.  */
  973.  
  974.           /* Also don't use a mode bigger than the structure.  */
  975.           if (total_size >= 0 && maxsize > total_size)
  976.             maxsize = total_size;
  977.  
  978.           /* Find biggest machine mode we can safely use
  979.              to fetch from this structure.
  980.              But don't use a bigger mode than the insn wants.  */
  981.           for (trymode = QImode;
  982.                trymode && GET_MODE_SIZE (trymode) <= maxsize;
  983.                trymode = GET_MODE_WIDER_MODE (trymode))
  984.             if (GET_MODE_SIZE (trymode) <= align
  985.             || align == BIGGEST_ALIGNMENT / BITS_PER_UNIT)
  986.               bestmode = trymode;
  987.           if (! bestmode)
  988.             abort ();
  989.           unit = GET_MODE_BITSIZE (bestmode);
  990.  
  991.           /* Compute offset as multiple of this unit,
  992.              counting in bytes.  */
  993.           xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
  994.           xbitpos = bitnum % unit;
  995.           xop0 = change_address (xop0, bestmode,
  996.                      plus_constant (XEXP (xop0, 0),
  997.                             xoffset));
  998.           /* Fetch it to a register in that size.  */
  999.           xop0 = force_reg (bestmode, xop0);
  1000.  
  1001.           /* Now ref the register in the mode extv wants.  */
  1002.           /* We used to use the mode from operand 1 in the md,
  1003.              but that is often QImode because that's needed for MEM.
  1004.              Here we need SImode instead.  */
  1005.           if (bestmode != SImode)
  1006.             xop0 = gen_rtx (SUBREG, SImode, xop0, 0);
  1007. #ifdef BYTES_BIG_ENDIAN
  1008.           if (GET_MODE_BITSIZE (GET_MODE (xop0)) > unit)
  1009.             xbitpos += GET_MODE_BITSIZE (GET_MODE (xop0)) - unit;
  1010. #endif
  1011.         }
  1012.           else
  1013.         /* Get ref to first byte containing part of the field.  */
  1014.         xop0 = change_address (xop0, QImode,
  1015.                        plus_constant (XEXP (xop0, 0), xoffset));
  1016.         }
  1017.  
  1018.       /* If op0 is a register, we need it in SImode
  1019.          to make it acceptable to the format of extv.  */
  1020.       if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != SImode)
  1021.         abort ();
  1022.       if (GET_CODE (xop0) == REG && GET_MODE (xop0) != SImode)
  1023.         {
  1024. #ifdef BYTES_BIG_ENDIAN
  1025.           xbitpos += (GET_MODE_BITSIZE (SImode)
  1026.               - GET_MODE_BITSIZE (GET_MODE (xop0)));
  1027. #endif
  1028.           xop0 = gen_rtx (SUBREG, SImode, xop0, 0);
  1029.         }
  1030.  
  1031.       if (xtarget == 0
  1032.           || (flag_force_mem && GET_CODE (xtarget) == MEM))
  1033.         xtarget = xspec_target = gen_reg_rtx (tmode);
  1034.  
  1035.       if (GET_MODE (xtarget) != SImode)
  1036.         {
  1037.           if (GET_CODE (xtarget) == REG)
  1038.         xspec_target_subreg = xtarget = gen_lowpart (SImode, xtarget);
  1039.           else
  1040.         xtarget = gen_reg_rtx (SImode);
  1041.         }
  1042.  
  1043.       /* If this machine's extv insists on a register target,
  1044.          make sure we have one.  */
  1045.       if (! (*insn_operand_predicate[(int) CODE_FOR_extv][0]) (xtarget, SImode))
  1046.         xtarget = gen_reg_rtx (SImode);
  1047.  
  1048.       /* On big-endian machines, we count bits from the most significant.
  1049.          If the bit field insn does not, we must invert.  */
  1050. #if defined (BITS_BIG_ENDIAN) != defined (BYTES_BIG_ENDIAN)
  1051.       xbitpos = unit - 1 - xbitpos;
  1052. #endif
  1053.  
  1054.       bitsize_rtx = gen_rtx (CONST_INT, VOIDmode, bitsize);
  1055.       bitpos_rtx = gen_rtx (CONST_INT, VOIDmode, xbitpos);
  1056.  
  1057.       pat = gen_extv (protect_from_queue (xtarget, 1),
  1058.               xop0, bitsize_rtx, bitpos_rtx);
  1059.       if (pat)
  1060.         {
  1061.           emit_insn (pat);
  1062.           target = xtarget;
  1063.           spec_target = xspec_target;
  1064.           spec_target_subreg = xspec_target_subreg;
  1065.         }
  1066.       else
  1067.         {
  1068.           delete_insns_since (last);
  1069.           target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
  1070.                         bitpos, target, 0, align);
  1071.         }
  1072.     } 
  1073.       else
  1074. #endif
  1075.     target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  1076.                       target, 0, align);
  1077.     }
  1078.   if (target == spec_target)
  1079.     return target;
  1080.   if (target == spec_target_subreg)
  1081.     return spec_target;
  1082.   if (GET_MODE (target) != tmode && GET_MODE (target) != mode)
  1083.     return convert_to_mode (tmode, target, unsignedp);
  1084.   return target;
  1085. }
  1086.  
  1087. /* Extract a bit field using shifts and boolean operations
  1088.    Returns an rtx to represent the value.
  1089.    OP0 addresses a register (word) or memory (byte).
  1090.    BITPOS says which bit within the word or byte the bit field starts in.
  1091.    OFFSET says how many bytes farther the bit field starts;
  1092.     it is 0 if OP0 is a register.
  1093.    BITSIZE says how many bits long the bit field is.
  1094.     (If OP0 is a register, it may be narrower than SImode,
  1095.      but BITPOS still counts within a full word,
  1096.      which is significant on bigendian machines.)
  1097.  
  1098.    UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
  1099.    If TARGET is nonzero, attempts to store the value there
  1100.    and return TARGET, but this is not guaranteed.
  1101.    If TARGET is not used, create a pseudo-reg of mode TMODE for the value.
  1102.  
  1103.    ALIGN is the alignment that STR_RTX is known to have, measured in bytes.  */
  1104.  
  1105. static rtx
  1106. extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  1107.              target, unsignedp, align)
  1108.      enum machine_mode tmode;
  1109.      register rtx op0, target;
  1110.      register int offset, bitsize, bitpos;
  1111.      int unsignedp;
  1112.      int align;
  1113. {
  1114.   int total_bits = BITS_PER_WORD;
  1115.   enum machine_mode mode;
  1116.  
  1117.   if (GET_CODE (op0) == SUBREG || GET_CODE (op0) == REG)
  1118.     {
  1119.       /* Special treatment for a bit field split across two registers.  */
  1120.       if (bitsize + bitpos > BITS_PER_WORD)
  1121.     return extract_split_bit_field (op0, bitsize, bitpos,
  1122.                     unsignedp, align);
  1123.     }
  1124.   else if (bitsize + bitpos <= BITS_PER_UNIT
  1125.        && (! SLOW_BYTE_ACCESS
  1126.            || (align == 1
  1127.            && BIGGEST_ALIGNMENT > 1)))
  1128.     {
  1129.       /* It fits in one byte, and either bytes are fast
  1130.      or the alignment won't let us use anything bigger.  */
  1131.       total_bits = BITS_PER_UNIT;
  1132.       op0 = change_address (op0, QImode, 
  1133.                 plus_constant (XEXP (op0, 0), offset));
  1134.     }
  1135.   else if ((bitsize + bitpos + (offset % GET_MODE_SIZE (HImode)) * BITS_PER_UNIT
  1136.         <= GET_MODE_BITSIZE (HImode))
  1137.        /* If halfwords are fast, use them whenever valid.  */
  1138.        && (! SLOW_BYTE_ACCESS
  1139.            /* Use halfwords if larger is invalid due to alignment.  */
  1140.            || (align == GET_MODE_SIZE (HImode)
  1141.            && BIGGEST_ALIGNMENT > GET_MODE_SIZE (HImode))))
  1142.     {
  1143.       /* It fits in an aligned halfword, and either halfwords are fast
  1144.      or the alignment won't let us use anything bigger.  */
  1145.       total_bits = GET_MODE_BITSIZE (HImode);
  1146.  
  1147.       /* Get ref to halfword containing the field.  */
  1148.       bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
  1149.       offset -= (offset % (total_bits / BITS_PER_UNIT));
  1150.       op0 = change_address (op0, HImode, 
  1151.                 plus_constant (XEXP (op0, 0), offset));
  1152.     }
  1153.   else
  1154.     {
  1155.       /* Get ref to word containing the field.  */
  1156.       /* Adjust BITPOS to be position within a word,
  1157.      and OFFSET to be the offset of that word.  */
  1158.       bitpos += (offset % (BITS_PER_WORD / BITS_PER_UNIT)) * BITS_PER_UNIT;
  1159.       offset -= (offset % (BITS_PER_WORD / BITS_PER_UNIT));
  1160.       op0 = change_address (op0, SImode,
  1161.                 plus_constant (XEXP (op0, 0), offset));
  1162.  
  1163.       /* Special treatment for a bit field split across two words.  */
  1164.       if (bitsize + bitpos > BITS_PER_WORD)
  1165.     return extract_split_bit_field (op0, bitsize, bitpos,
  1166.                     unsignedp, align);
  1167.     }
  1168.  
  1169.   mode = GET_MODE (op0);
  1170.  
  1171. #ifdef BYTES_BIG_ENDIAN
  1172.   /* BITPOS is the distance between our msb and that of OP0.
  1173.      Convert it to the distance from the lsb.  */
  1174.  
  1175.   bitpos = total_bits - bitsize - bitpos;
  1176. #endif
  1177.   /* Now BITPOS is always the distance between the field's lsb and that of OP0.
  1178.      We have reduced the big-endian case to the little-endian case.  */
  1179.  
  1180.   if (unsignedp)
  1181.     {
  1182.       if (bitpos)
  1183.     {
  1184.       /* If the field does not already start at the lsb,
  1185.          shift it so it does.  */
  1186.       tree amount = build_int_2 (bitpos, 0);
  1187.       /* Maybe propagate the target for the shift.  */
  1188.       /* But not if we will return it--could confuse integrate.c.  */
  1189.       rtx subtarget = (target != 0 && GET_CODE (target) == REG
  1190.                && !REG_FUNCTION_VALUE_P (target)
  1191.                ? target : 0);
  1192.       if (tmode != mode) subtarget = 0;
  1193.       op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
  1194.     }
  1195.       /* Convert the value to the desired mode.  */
  1196.       if (mode != tmode)
  1197.     op0 = convert_to_mode (tmode, op0, 1);
  1198.  
  1199.       /* Unless the msb of the field used to be the msb when we shifted,
  1200.      mask out the upper bits.  */
  1201.  
  1202.       if ((GET_MODE_BITSIZE (mode) != bitpos + bitsize
  1203. #if 0
  1204. #ifdef SLOW_ZERO_EXTEND
  1205.        /* Always generate an `and' if
  1206.           we just zero-extended op0 and SLOW_ZERO_EXTEND, since it
  1207.           will combine fruitfully with the zero-extend. */
  1208.        || tmode != mode
  1209. #endif
  1210. #endif
  1211.        )
  1212.       && bitsize < HOST_BITS_PER_INT)
  1213.     return expand_bit_and (GET_MODE (op0), op0,
  1214.                    gen_rtx (CONST_INT, VOIDmode, (1 << bitsize) - 1),
  1215.                    target);
  1216.       return op0;
  1217.     }
  1218.  
  1219.   /* To extract a signed bit-field, first shift its msb to the msb of the word,
  1220.      then arithmetic-shift its lsb to the lsb of the word.  */
  1221.   op0 = force_reg (mode, op0);
  1222.   if (mode != tmode)
  1223.     target = 0;
  1224.   if (GET_MODE_BITSIZE (QImode) < GET_MODE_BITSIZE (mode)
  1225.       && GET_MODE_BITSIZE (QImode) >= bitsize + bitpos)
  1226.     mode = QImode, op0 = convert_to_mode (QImode, op0, 0);
  1227.   if (GET_MODE_BITSIZE (HImode) < GET_MODE_BITSIZE (mode)
  1228.       && GET_MODE_BITSIZE (HImode) >= bitsize + bitpos)
  1229.     mode = HImode, op0 = convert_to_mode (HImode, op0, 0);
  1230.   if (GET_MODE_BITSIZE (mode) != (bitsize + bitpos))
  1231.     {
  1232.       tree amount = build_int_2 (GET_MODE_BITSIZE (mode) - (bitsize + bitpos), 0);
  1233.       /* Maybe propagate the target for the shift.  */
  1234.       /* But not if we will return the result--could confuse integrate.c.  */
  1235.       rtx subtarget = (target != 0 && GET_CODE (target) == REG
  1236.                && ! REG_FUNCTION_VALUE_P (target)
  1237.                ? target : 0);
  1238.       op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
  1239.     }
  1240.  
  1241.   return expand_shift (RSHIFT_EXPR, mode, op0,
  1242.                build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0), 
  1243.                target, 0);
  1244. }
  1245.  
  1246. /* Extract a bit field that is split across two words
  1247.    and return an RTX for the result.
  1248.  
  1249.    OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
  1250.    BITSIZE is the field width; BITPOS, position of its first bit, in the word.
  1251.    UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend.  */
  1252.  
  1253. static rtx
  1254. extract_split_bit_field (op0, bitsize, bitpos, unsignedp, align)
  1255.      rtx op0;
  1256.      int bitsize, bitpos, unsignedp, align;
  1257. {
  1258.   /* BITSIZE_1 is size of the part in the first word.  */
  1259.   int bitsize_1 = BITS_PER_WORD - bitpos;
  1260.   /* BITSIZE_2 is size of the rest (in the following word).  */
  1261.   int bitsize_2 = bitsize - bitsize_1;
  1262.   rtx part1, part2, result;
  1263.  
  1264.   /* Get the part of the bit field from the first word.  */
  1265.   part1 = extract_fixed_bit_field (SImode, op0, 0, bitsize_1, bitpos,
  1266.                    0, 1, align);
  1267.  
  1268.   /* Offset op0 by 1 word to get to the following one.  */
  1269.   if (GET_CODE (op0) == MEM)
  1270.     op0 = change_address (op0, SImode,
  1271.               plus_constant (XEXP (op0, 0), UNITS_PER_WORD));
  1272.   else if (GET_CODE (op0) == REG)
  1273.     op0 = gen_rtx (SUBREG, SImode, op0, 1);
  1274.   else
  1275.     op0 = gen_rtx (SUBREG, SImode, SUBREG_REG (op0), SUBREG_WORD (op0) + 1);
  1276.  
  1277.   /* Get the part of the bit field from the second word.  */
  1278.   part2 = extract_fixed_bit_field (SImode, op0, 0, bitsize_2, 0, 0, 1, align);
  1279.  
  1280.   /* Shift the more significant part up to fit above the other part.  */
  1281. #ifdef BYTES_BIG_ENDIAN
  1282.   part1 = expand_shift (LSHIFT_EXPR, SImode, part1,
  1283.             build_int_2 (bitsize_2, 0), 0, 1);
  1284. #else
  1285.   part2 = expand_shift (LSHIFT_EXPR, SImode, part2,
  1286.             build_int_2 (bitsize_1, 0), 0, 1);
  1287. #endif
  1288.  
  1289.   /* Combine the two parts with bitwise or.  This works
  1290.      because we extracted both parts as unsigned bit fields.  */
  1291.   result = expand_binop (SImode, ior_optab, part1, part2, 0, 1,
  1292.              OPTAB_LIB_WIDEN);
  1293.  
  1294.   /* Unsigned bit field: we are done.  */
  1295.   if (unsignedp)
  1296.     return result;
  1297.   /* Signed bit field: sign-extend with two arithmetic shifts.  */
  1298.   result = expand_shift (LSHIFT_EXPR, SImode, result,
  1299.              build_int_2 (BITS_PER_WORD - bitsize, 0), 0, 0);
  1300.   return expand_shift (RSHIFT_EXPR, SImode, result,
  1301.                build_int_2 (BITS_PER_WORD - bitsize, 0), 0, 0);
  1302. }
  1303.  
  1304. /* Add INC into TARGET.  */
  1305.  
  1306. void
  1307. expand_inc (target, inc)
  1308.      rtx target, inc;
  1309. {
  1310.   rtx value = expand_binop (GET_MODE (target), add_optab,
  1311.                 target, inc,
  1312.                 target, 0, OPTAB_LIB_WIDEN);
  1313.   if (value != target)
  1314.     emit_move_insn (target, value);
  1315. }
  1316.  
  1317. /* Subtract INC from TARGET.  */
  1318.  
  1319. void
  1320. expand_dec (target, dec)
  1321.      rtx target, dec;
  1322. {
  1323.   rtx value = expand_binop (GET_MODE (target), sub_optab,
  1324.                 target, dec,
  1325.                 target, 0, OPTAB_LIB_WIDEN);
  1326.   if (value != target)
  1327.     emit_move_insn (target, value);
  1328. }
  1329.  
  1330. /* Output a shift instruction for expression code CODE,
  1331.    with SHIFTED being the rtx for the value to shift,
  1332.    and AMOUNT the tree for the amount to shift by.
  1333.    Store the result in the rtx TARGET, if that is convenient.
  1334.    If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
  1335.    Return the rtx for where the value is.  */
  1336.  
  1337. /* Pastel, for shifts, converts shift count to SImode here
  1338.    independent of the mode being shifted.
  1339.    Should that be done in an earlier pass?
  1340.    It turns out not to matter for C.  */
  1341.  
  1342. rtx
  1343. expand_shift (code, mode, shifted, amount, target, unsignedp)
  1344.      enum tree_code code;
  1345.      register enum machine_mode mode;
  1346.      rtx shifted;
  1347.      tree amount;
  1348.      register rtx target;
  1349.      int unsignedp;
  1350. {
  1351.   register rtx op1, temp = 0;
  1352.   register int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
  1353.   int try;
  1354.   int rotate = code == LROTATE_EXPR || code == RROTATE_EXPR;
  1355.   rtx last;
  1356.  
  1357.   /* Previously detected shift-counts computed by NEGATE_EXPR
  1358.      and shifted in the other direction; but that does not work
  1359.      on all machines.  */
  1360.  
  1361.   op1 = expand_expr (amount, 0, VOIDmode, 0);
  1362.  
  1363.   last = get_last_insn ();
  1364.  
  1365.   for (try = 0; temp == 0 && try < 3; try++)
  1366.     {
  1367.       enum optab_methods methods;
  1368.       delete_insns_since (last);
  1369.  
  1370.       if (try == 0)
  1371.     methods = OPTAB_DIRECT;
  1372.       else if (try == 1)
  1373.     methods = OPTAB_WIDEN;
  1374.       else
  1375.     methods = OPTAB_LIB_WIDEN;
  1376.  
  1377.       if (rotate)
  1378.     {
  1379.       /* Widening does not work for rotation.  */
  1380.       if (methods != OPTAB_DIRECT)
  1381.         methods = OPTAB_LIB;
  1382.  
  1383.       temp = expand_binop (mode,
  1384.                    left ? rotl_optab : rotr_optab,
  1385.                    shifted, op1, target, -1, methods);
  1386.     }
  1387.       else if (unsignedp)
  1388.     {
  1389.       temp = expand_binop (mode,
  1390.                    left ? lshl_optab : lshr_optab,
  1391.                    shifted, op1, target, unsignedp, methods);
  1392.       if (temp == 0 && left)
  1393.         temp = expand_binop (mode, ashl_optab,
  1394.                  shifted, op1, target, unsignedp, methods);
  1395.       if (temp != 0)
  1396.         return temp;
  1397.     }
  1398.       /* Do arithmetic shifts.
  1399.      Also, if we are going to widen the operand, we can just as well
  1400.      use an arithmetic right-shift instead of a logical one.  */
  1401.       if (! rotate && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
  1402.     {
  1403.       enum optab_methods methods1 = methods;
  1404.  
  1405.       /* If trying to widen a log shift to an arithmetic shift,
  1406.          don't accept an arithmetic shift of the same size.  */
  1407.       if (unsignedp)
  1408.         methods1 = OPTAB_MUST_WIDEN;
  1409.  
  1410.       /* Arithmetic shift */
  1411.  
  1412.       temp = expand_binop (mode,
  1413.                    left ? ashl_optab : ashr_optab,
  1414.                    shifted, op1, target, unsignedp, methods1);
  1415.       if (temp != 0)
  1416.         return temp;
  1417.     }
  1418.  
  1419.       if (unsignedp)
  1420.     {
  1421.       /* No logical shift insn in either direction =>
  1422.          try a bit-field extract instruction if we have one.  */
  1423. #ifdef HAVE_extzv
  1424. #ifndef BITS_BIG_ENDIAN
  1425.       if (HAVE_extzv && !left
  1426.           && ((methods == OPTAB_DIRECT && mode == SImode)
  1427.           || (methods == OPTAB_WIDEN
  1428.               && GET_MODE_SIZE (mode) < GET_MODE_SIZE (SImode))))
  1429.         {
  1430.           rtx shifted1 = convert_to_mode (SImode, shifted, 1);
  1431.           rtx target1 = target;
  1432.  
  1433.           /* If -fforce-mem, don't let the operand be in memory.  */
  1434.           if (flag_force_mem && GET_CODE (shifted1) == MEM)
  1435.         shifted1 = force_not_mem (shifted1);
  1436.  
  1437.           /* If this machine's extzv insists on a register for
  1438.          operand 1, arrange for that.  */
  1439.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][1])
  1440.              (shifted1, SImode)))
  1441.         shifted1 = force_reg (SImode, shifted1);
  1442.  
  1443.           /* If we don't have or cannot use a suggested target,
  1444.          make a place for the result, in the proper mode.  */
  1445.           if (methods == OPTAB_WIDEN || target1 == 0
  1446.           || ! ((*insn_operand_predicate[(int) CODE_FOR_extzv][0])
  1447.             (target1, SImode)))
  1448.         target1 = gen_reg_rtx (SImode);
  1449.  
  1450.           op1 = convert_to_mode (SImode, op1, 0);
  1451.  
  1452.           /* If this machine's extzv insists on a register for
  1453.          operand 3, arrange for that.  */
  1454.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][3])
  1455.              (op1, SImode)))
  1456.         op1 = force_reg (SImode, op1);
  1457.  
  1458.           op1 = protect_from_queue (op1, 1);
  1459.  
  1460.           /* TEMP gets the width of the bit field to extract:
  1461.          wordsize minus # bits to shift by.  */
  1462.           if (GET_CODE (op1) == CONST_INT)
  1463.         temp = gen_rtx (CONST_INT, VOIDmode,
  1464.                 (GET_MODE_BITSIZE (mode) - INTVAL (op1)));
  1465.           else
  1466.         temp = expand_binop (SImode, sub_optab,
  1467.                      gen_rtx (CONST_INT, VOIDmode,
  1468.                           GET_MODE_BITSIZE (mode)),
  1469.                      op1, gen_reg_rtx (SImode),
  1470.                      0, OPTAB_LIB_WIDEN);
  1471.           /* Now extract with width TEMP, omitting OP1 least sig bits.  */
  1472.           emit_insn (gen_extzv (protect_from_queue (target1, 1),
  1473.                     protect_from_queue (shifted1, 0),
  1474.                     temp, op1));
  1475.           return convert_to_mode (mode, target1, 1);
  1476.         }
  1477.       /* Can also do logical shift with signed bit-field extract
  1478.          followed by inserting the bit-field at a different position.
  1479.          That strategy is not yet implemented.  */
  1480. #endif /* not BITS_BIG_ENDIAN */
  1481. #endif /* HAVE_extzv */
  1482.       /* We have failed to generate the logical shift and will abort.  */
  1483.     }
  1484.     }
  1485.   if (temp == 0)
  1486.     abort ();
  1487.   return temp;
  1488. }
  1489.  
  1490. /* Output an instruction or two to bitwise-and OP0 with OP1
  1491.    in mode MODE, with output to TARGET if convenient and TARGET is not zero.
  1492.    Returns where the result is.  */
  1493. /* This function used to do more; now it could be eliminated.  */
  1494.  
  1495. rtx
  1496. expand_bit_and (mode, op0, op1, target)
  1497.      enum machine_mode mode;
  1498.      rtx op0, op1, target;
  1499. {
  1500.   register rtx temp;
  1501.  
  1502.   /* First try to open-code it directly.  */
  1503.   temp = expand_binop (mode, and_optab, op0, op1, target, 1, OPTAB_LIB_WIDEN);
  1504.   if (temp == 0)
  1505.     abort ();
  1506.   return temp;
  1507. }
  1508.  
  1509. /* Perform a multiplication and return an rtx for the result.
  1510.    MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
  1511.    TARGET is a suggestion for where to store the result (an rtx).
  1512.  
  1513.    We check specially for a constant integer as OP1.
  1514.    If you want this check for OP0 as well, then before calling
  1515.    you should swap the two operands if OP0 would be constant.  */
  1516.  
  1517. rtx
  1518. expand_mult (mode, op0, op1, target, unsignedp)
  1519.      enum machine_mode mode;
  1520.      register rtx op0, op1, target;
  1521.      int unsignedp;
  1522. {
  1523.   /* Don't use the function value register as a target
  1524.      since we have to read it as well as write it,
  1525.      and function-inlining gets confused by this.  */
  1526.   if (target && REG_P (target) && REG_FUNCTION_VALUE_P (target))
  1527.     target = 0;
  1528.  
  1529.   if (GET_CODE (op1) == CONST_INT)
  1530.     {
  1531.       register int foo;
  1532.       int bar;
  1533.       int negate = INTVAL (op1) < 0;
  1534.       int absval = INTVAL (op1) * (negate ? -1 : 1);
  1535.  
  1536.       /* Is multiplier a power of 2, or minus that?  */
  1537.       foo = exact_log2 (absval);
  1538.       if (foo >= 0)
  1539.     {
  1540.       rtx tem;
  1541.       if (foo == 0)
  1542.         tem = op0;
  1543.       else
  1544.         tem = expand_shift (LSHIFT_EXPR, mode, op0,
  1545.                 build_int_2 (foo, 0),
  1546.                 target, 0);
  1547.       return (negate
  1548.           ? expand_unop (mode, neg_optab, tem, target, 0)
  1549.           : tem);
  1550.     }
  1551.       /* Is multiplier a sum of two powers of 2, or minus that?  */
  1552.       bar = floor_log2 (absval);
  1553.       foo = exact_log2 (absval - (1 << bar));
  1554.       if (bar >= 0 && foo >= 0)
  1555.     {
  1556.       rtx tem =
  1557.         force_operand (gen_rtx (PLUS, mode,
  1558.                     expand_shift (LSHIFT_EXPR, mode, op0,
  1559.                              build_int_2 (bar - foo, 0),
  1560.                              0, 0),
  1561.                     op0),
  1562.                ((foo == 0 && ! negate) ? target : 0));
  1563.  
  1564.       if (foo != 0)
  1565.         tem = expand_shift (LSHIFT_EXPR, mode, tem,
  1566.                 build_int_2 (foo, 0),
  1567.                 negate ? 0 : target, 0);
  1568.  
  1569.       return negate ? expand_unop (mode, neg_optab, tem, target, 0) : tem;
  1570.     }
  1571.     }
  1572.   /* This used to use umul_optab if unsigned,
  1573.      but I think that for non-widening multiply there is no difference
  1574.      between signed and unsigned.  */
  1575.   op0 = expand_binop (mode, smul_optab,
  1576.               op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
  1577.   if (op0 == 0)
  1578.     abort ();
  1579.   return op0;
  1580. }
  1581.  
  1582. /* Emit the code to divide OP0 by OP1, putting the result in TARGET
  1583.    if that is convenient, and returning where the result is.
  1584.    You may request either the quotient or the remainder as the result;
  1585.    specify REM_FLAG nonzero to get the remainder.
  1586.  
  1587.    CODE is the expression code for which kind of division this is;
  1588.    it controls how rounding is done.  MODE is the machine mode to use.
  1589.    UNSIGNEDP nonzero means do unsigned division.  */
  1590.  
  1591. /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
  1592.    and then correct it by or'ing in missing high bits
  1593.    if result of ANDI is nonzero.
  1594.    For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
  1595.    This could optimize to a bfexts instruction.
  1596.    But C doesn't use these operations, so their optimizations are
  1597.    left for later.  */
  1598.  
  1599. rtx
  1600. expand_divmod (rem_flag, code, mode, op0, op1, target, unsignedp)
  1601.      int rem_flag;
  1602.      enum tree_code code;
  1603.      enum machine_mode mode;
  1604.      register rtx op0, op1, target;
  1605.      int unsignedp;
  1606. {
  1607.   register rtx temp;
  1608.   int log = -1;
  1609.   int can_clobber_op0;
  1610.   int mod_insn_no_good = 0;
  1611.   rtx adjusted_op0 = op0;
  1612.  
  1613.   /* Don't use the function value register as a target
  1614.      since we have to read it as well as write it,
  1615.      and function-inlining gets confused by this.  */
  1616.   if (target && REG_P (target) && REG_FUNCTION_VALUE_P (target))
  1617.     target = 0;
  1618.  
  1619.   /* Don't clobber an operand while doing a multi-step calculation.  */
  1620.   if (target)
  1621.     if ((rem_flag && (reg_mentioned_p (target, op0)
  1622.               || (GET_CODE (op0) == MEM && GET_CODE (target) == MEM)))
  1623.     || reg_mentioned_p (target, op1)
  1624.     || (GET_CODE (op1) == MEM && GET_CODE (target) == MEM))
  1625.       target = 0;
  1626.  
  1627.   if (target == 0)
  1628.     target = gen_reg_rtx (mode);
  1629.  
  1630.   can_clobber_op0 = (GET_CODE (op0) == REG && op0 == target);
  1631.  
  1632.   if (GET_CODE (op1) == CONST_INT)
  1633.     log = exact_log2 (INTVAL (op1));
  1634.  
  1635.   /* If log is >= 0, we are dividing by 2**log, and will do it by shifting,
  1636.      which is really floor-division.  Otherwise we will really do a divide,
  1637.      and we assume that is trunc-division.
  1638.  
  1639.      We must correct the dividend by adding or subtracting something
  1640.      based on the divisor, in order to do the kind of rounding specified
  1641.      by CODE.  The correction depends on what kind of rounding is actually
  1642.      available, and that depends on whether we will shift or divide.  */
  1643.  
  1644.   switch (code)
  1645.     {
  1646.     case TRUNC_MOD_EXPR:
  1647.     case TRUNC_DIV_EXPR:
  1648.       if (log >= 0 && ! unsignedp)
  1649.     {
  1650.       rtx label = gen_label_rtx ();
  1651.       if (! can_clobber_op0)
  1652.         adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  1653.       emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0, 0);
  1654.       emit_jump_insn (gen_bge (label));
  1655.       expand_inc (adjusted_op0, plus_constant (op1, -1));
  1656.       emit_label (label);
  1657.       mod_insn_no_good = 1;
  1658.     }
  1659.       break;
  1660.  
  1661.     case FLOOR_DIV_EXPR:
  1662.     case FLOOR_MOD_EXPR:
  1663.       if (log < 0 && ! unsignedp)
  1664.     {
  1665.       rtx label = gen_label_rtx ();
  1666.       if (! can_clobber_op0)
  1667.         adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  1668.       emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0, 0);
  1669.       emit_jump_insn (gen_bge (label));
  1670.       expand_dec (adjusted_op0, op1);
  1671.       expand_inc (adjusted_op0, const1_rtx);
  1672.       emit_label (label);
  1673.       mod_insn_no_good = 1;
  1674.     }
  1675.       break;
  1676.  
  1677.     case CEIL_DIV_EXPR:
  1678.     case CEIL_MOD_EXPR:
  1679.       if (! can_clobber_op0)
  1680.     adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  1681.       if (log < 0)
  1682.     {
  1683.       rtx label = 0;
  1684.       if (! unsignedp)
  1685.         {
  1686.           label = gen_label_rtx ();
  1687.           emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0, 0);
  1688.           emit_jump_insn (gen_ble (label));
  1689.         }
  1690.       expand_inc (adjusted_op0, op1);
  1691.       expand_dec (adjusted_op0, const1_rtx);
  1692.       if (! unsignedp)
  1693.         emit_label (label);
  1694.     }
  1695.       else
  1696.     {
  1697.       adjusted_op0 = expand_binop (GET_MODE (target), add_optab,
  1698.                        adjusted_op0, plus_constant (op1, -1),
  1699.                        0, 0, OPTAB_LIB_WIDEN);
  1700.     }
  1701.       mod_insn_no_good = 1;
  1702.       break;
  1703.  
  1704.     case ROUND_DIV_EXPR:
  1705.     case ROUND_MOD_EXPR:
  1706.       if (! can_clobber_op0)
  1707.     adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  1708.       if (log < 0)
  1709.     {
  1710.       op1 = expand_shift (RSHIFT_EXPR, mode, op1, integer_one_node, 0, 0);
  1711.       if (! unsignedp)
  1712.         {
  1713.           rtx label = gen_label_rtx ();
  1714.           emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0, 0);
  1715.           emit_jump_insn (gen_bge (label));
  1716.           expand_unop (mode, neg_optab, op1, op1, 0);
  1717.           emit_label (label);
  1718.         }
  1719.       expand_inc (adjusted_op0, op1);
  1720.     }
  1721.       else
  1722.     {
  1723.       op1 = gen_rtx (CONST_INT, VOIDmode, INTVAL (op1) / 2);
  1724.       expand_inc (adjusted_op0, op1);
  1725.     }
  1726.       mod_insn_no_good = 1;
  1727.       break;
  1728.     }
  1729.  
  1730.   if (rem_flag && !mod_insn_no_good)
  1731.     {
  1732.       /* Try to produce the remainder directly */
  1733.       if (log >= 0)
  1734.     {
  1735.       return expand_bit_and (mode, adjusted_op0,
  1736.                  gen_rtx (CONST_INT, VOIDmode,
  1737.                       INTVAL (op1) - 1),
  1738.                  target);
  1739.     }
  1740.       else
  1741.     {
  1742.       /* See if we can do remainder without a library call.  */
  1743.       temp = sign_expand_binop (mode, umod_optab, smod_optab,
  1744.                     adjusted_op0, op1, target,
  1745.                     unsignedp, OPTAB_WIDEN);
  1746.       if (temp != 0)
  1747.         return temp;
  1748.       /* No luck there.
  1749.          Can we do remainder and divide at once without a library call?  */
  1750.       temp = gen_reg_rtx (mode);
  1751.       if (expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
  1752.                    adjusted_op0, op1,
  1753.                    0, temp, unsignedp))
  1754.         return temp;
  1755.       temp = 0;
  1756.     }
  1757.     }
  1758.  
  1759.   /* Produce the quotient.  */
  1760.   if (log >= 0)
  1761.     temp = expand_shift (RSHIFT_EXPR, mode, adjusted_op0,
  1762.              build_int_2 (exact_log2 (INTVAL (op1)), 0),
  1763.              target, unsignedp);
  1764.   else if (rem_flag && !mod_insn_no_good)
  1765.     /* If producing quotient in order to subtract for remainder,
  1766.        and a remainder subroutine would be ok,
  1767.        don't use a divide subroutine.  */
  1768.     temp = sign_expand_binop (mode, udiv_optab, sdiv_optab,
  1769.                   adjusted_op0, op1, target,
  1770.                   unsignedp, OPTAB_WIDEN);
  1771.   else
  1772.     {
  1773.       /* Try a quotient insn, but not a library call.  */
  1774.       temp = sign_expand_binop (mode, udiv_optab, sdiv_optab,
  1775.                 adjusted_op0, op1, target,
  1776.                 unsignedp, OPTAB_WIDEN);
  1777.       if (temp == 0)
  1778.     {
  1779.       /* No luck there.  Try a quotient-and-remainder insn,
  1780.          keeping the quotient alone.  */
  1781.       temp = gen_reg_rtx (mode);
  1782.       if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
  1783.                      adjusted_op0, op1,
  1784.                      temp, 0, unsignedp))
  1785.         temp = 0;
  1786.     }
  1787.  
  1788.       /* If still no luck, use a library call.  */
  1789.       if (temp == 0)
  1790.     temp = sign_expand_binop (mode, udiv_optab, sdiv_optab,
  1791.                   adjusted_op0, op1, target,
  1792.                   unsignedp, OPTAB_LIB_WIDEN);
  1793.     }
  1794.  
  1795.   /* If we really want the remainder, get it by subtraction.  */
  1796.   if (rem_flag)
  1797.     {
  1798.       if (temp == 0)
  1799.     {
  1800.       /* No divide instruction either.  Use library for remainder.  */
  1801.       temp = sign_expand_binop (mode, umod_optab, smod_optab,
  1802.                     op0, op1, target,
  1803.                     unsignedp, OPTAB_LIB_WIDEN);
  1804.     }
  1805.       else
  1806.     {
  1807.       /* We divided.  Now finish doing X - Y * (X / Y).  */
  1808.       temp = expand_mult (mode, temp, op1, temp, unsignedp);
  1809.       if (! temp) abort ();
  1810.       temp = expand_binop (mode, sub_optab, op0,
  1811.                    temp, target, unsignedp, OPTAB_LIB_WIDEN);
  1812.     }
  1813.     }
  1814.  
  1815.   if (temp == 0)
  1816.     abort ();
  1817.   return temp;
  1818. }
  1819.  
  1820. /* Return a tree node with data type TYPE, describing the value of X.
  1821.    Usually this is an RTL_EXPR, if there is no obvious better choice.  */
  1822.  
  1823. static tree
  1824. make_tree (type, x)
  1825.      tree type;
  1826.      rtx x;
  1827. {
  1828.   tree t;
  1829.   switch (GET_CODE (x))
  1830.     {
  1831.     case CONST_INT:
  1832.       t = build_int_2 (INTVAL (x), 0);
  1833.       TREE_TYPE (t) = type;
  1834.       return fold (t);
  1835.  
  1836.     default:
  1837.       t = make_node (RTL_EXPR);
  1838.       TREE_TYPE (t) = type;
  1839.       RTL_EXPR_RTL (t) = x;
  1840.       /* There are no insns to be output
  1841.      when this rtl_expr is used.  */
  1842.       RTL_EXPR_SEQUENCE (t) = 0;
  1843.       return t;
  1844.     }
  1845. }
  1846.  
  1847. /* Return an rtx representing the value of X * MULT + ADD.
  1848.    MODE is the machine mode for the computation.
  1849.    UNSIGNEDP is non-zero to do unsigned multiplication.
  1850.    This may emit insns.  */
  1851.  
  1852. rtx
  1853. expand_mult_add (x, mult, add, mode, unsignedp)
  1854.      rtx x, mult, add;
  1855.      enum machine_mode mode;
  1856.      int unsignedp;
  1857. {
  1858.   tree type = type_for_size (GET_MODE_BITSIZE (mode), unsignedp);
  1859.   tree prod = fold (build (MULT_EXPR, type, make_tree (type, x),
  1860.                make_tree (type, mult)));
  1861.   tree sum = fold (build (PLUS_EXPR, type, prod, make_tree (type, add)));
  1862.   return expand_expr (sum, 0, VOIDmode, 0);
  1863. }
  1864.