home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__src1 / expmed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  47.1 KB  |  1,504 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.  
  69. /* ??? This should really have the ability to copy a word into a register
  70.    in order to store the bit-field into it, on machines whose insv insns
  71.    work that way.  */
  72.  
  73. rtx
  74. store_bit_field (str_rtx, bitsize, bitnum, fieldmode, value, align)
  75.      rtx str_rtx;
  76.      register int bitsize;
  77.      int bitnum;
  78.      enum machine_mode fieldmode;
  79.      rtx value;
  80.      int align;
  81. {
  82.   int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
  83.   register int offset = bitnum / unit;
  84.   register int bitpos = bitnum % unit;
  85.   register rtx op0 = str_rtx;
  86.   rtx value1;
  87.  
  88.   while (GET_CODE (op0) == SUBREG)
  89.     {
  90.       offset += SUBREG_WORD (op0);
  91.       op0 = SUBREG_REG (op0);
  92.     }
  93.  
  94.   value = protect_from_queue (value, 0);
  95.  
  96.   if (flag_force_mem)
  97.     value = force_not_mem (value);
  98.  
  99.   if (GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD)
  100.     {
  101.       /* Storing in a full-word or multi-word field in a register
  102.      can be done with just SUBREG.  */
  103.       if (GET_MODE (op0) != fieldmode)
  104.     op0 = gen_rtx (SUBREG, fieldmode, op0, offset);
  105.       emit_move_insn (op0, value);
  106.       return value;
  107.     }
  108.  
  109. #ifdef BYTES_BIG_ENDIAN
  110.   /* If OP0 is a register, BITPOS must count within a word.
  111.      But as we have it, it counts within whatever size OP0 now has.
  112.      On a bigendian machine, these are not the same, so convert.  */
  113.   if (GET_CODE (op0) != MEM && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
  114.     bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
  115. #endif
  116.  
  117.   /* Storing an lsb-aligned field in a register
  118.      can be done with a movestrict instruction.  */
  119.  
  120.   if (GET_CODE (op0) != MEM
  121. #ifdef BYTES_BIG_ENDIAN
  122.       && bitpos + bitsize == unit
  123. #else
  124.       && bitpos == 0
  125. #endif
  126.       && (GET_MODE (op0) == fieldmode
  127.       || (movstrict_optab->handlers[(int) fieldmode].insn_code
  128.           != CODE_FOR_nothing)))
  129.     {
  130.       /* Get appropriate low part of the value being stored.  */
  131.       if (GET_CODE (value) == CONST_INT || GET_CODE (value) == REG)
  132.     value = gen_lowpart (fieldmode, value);
  133.       else if (!(GET_CODE (value) == SYMBOL_REF
  134.          || GET_CODE (value) == LABEL_REF
  135.          || GET_CODE (value) == CONST))
  136.     value = convert_to_mode (fieldmode, value, 0);
  137.  
  138.       if (GET_MODE (op0) == fieldmode)
  139.     emit_move_insn (op0, value);
  140.       else
  141.     emit_insn (GEN_FCN (movstrict_optab->handlers[(int) fieldmode].insn_code)
  142.            (gen_rtx (SUBREG, fieldmode, op0, offset), value));
  143.  
  144.       return value;
  145.     }
  146.  
  147.   /* From here on we can assume that the field to be stored in is an integer,
  148.      since it is shorter than a word.  */
  149.  
  150.   /* OFFSET is the number of words or bytes (UNIT says which)
  151.      from STR_RTX to the first word or byte containing part of the field.  */
  152.  
  153.   if (GET_CODE (op0) == REG)
  154.     {
  155.       if (offset != 0
  156.       || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (SImode))
  157.     op0 = gen_rtx (SUBREG, SImode, op0, offset);
  158.       offset = 0;
  159.     }
  160.   else
  161.     {
  162.       op0 = protect_from_queue (op0, 1);
  163.     }
  164.  
  165.   /* Now OFFSET is nonzero only if OP0 is memory
  166.      and is therefore always measured in bytes.  */
  167.  
  168. #ifdef HAVE_insv
  169.   if (HAVE_insv
  170.       && !(bitsize == 1 && GET_CODE (value) == CONST_INT))
  171.     {
  172.       int xbitpos = bitpos;
  173.       rtx xop0 = op0;
  174.       rtx last = get_last_insn ();
  175.       rtx pat;
  176.  
  177.       /* Add OFFSET into OP0's address.  */
  178.       if (GET_CODE (xop0) == MEM)
  179.     xop0 = change_address (xop0, QImode,
  180.                    plus_constant (XEXP (xop0, 0), offset));
  181.  
  182.       /* If xop0 is a register, we need it in SImode
  183.      to make it acceptable to the format of insv.  */
  184.       if (GET_CODE (xop0) == SUBREG)
  185.     PUT_MODE (xop0, SImode);
  186.       if (GET_CODE (xop0) == REG && GET_MODE (xop0) != SImode)
  187.     xop0 = gen_rtx (SUBREG, SImode, xop0, 0);
  188.  
  189.       /* Convert VALUE to SImode (which insv insn wants) in VALUE1.  */
  190.       value1 = value;
  191.       if (GET_MODE (value) != SImode)
  192.     {
  193.       if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
  194.         {
  195.           /* Optimization: Don't bother really extending VALUE
  196.          if it has all the bits we will actually use.  */
  197.  
  198.           /* Avoid making subreg of a subreg, or of a mem.  */
  199.           if (GET_CODE (value1) != REG)
  200.         value1 = copy_to_reg (value1);
  201.           value1 = gen_rtx (SUBREG, SImode, value1, 0);
  202.         }
  203.       else if (!CONSTANT_P (value))
  204.         /* Parse phase is supposed to make VALUE's data type
  205.            match that of the component reference, which is a type
  206.            at least as wide as the field; so VALUE should have
  207.            a mode that corresponds to that type.  */
  208.         abort ();
  209.     }
  210.  
  211.       /* If this machine's insv insists on a register,
  212.      get VALUE1 into a register.  */
  213.       if (! (*insn_operand_predicate[(int) CODE_FOR_insv][3]) (value1, SImode))
  214.     value1 = force_reg (SImode, value1);
  215.  
  216.       /* On big-endian machines, we count bits from the most significant.
  217.      If the bit field insn does not, we must invert.  */
  218.  
  219. #if defined (BITS_BIG_ENDIAN) != defined (BYTES_BIG_ENDIAN)
  220.       xbitpos = unit - 1 - xbitpos;
  221. #endif
  222.  
  223.       pat = gen_insv (xop0,
  224.               gen_rtx (CONST_INT, VOIDmode, bitsize),
  225.               gen_rtx (CONST_INT, VOIDmode, xbitpos),
  226.               value1);
  227.       if (pat)
  228.     emit_insn (pat);
  229.       else
  230.         {
  231.       delete_insns_since (last);
  232.       store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
  233.     }
  234.     }
  235.   else
  236. #endif
  237.     /* Insv is not available; store using shifts and boolean ops.  */
  238.     store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
  239.   return value;
  240. }
  241.  
  242. /* Use shifts and boolean operations to store VALUE
  243.    into a bit field of width BITSIZE
  244.    in a memory location specified by OP0 except offset by OFFSET bytes.
  245.      (OFFSET must be 0 if OP0 is a register.)
  246.    The field starts at position BITPOS within the byte.
  247.     (If OP0 is a register, it may be SImode or a narrower mode,
  248.      but BITPOS still counts within a full word,
  249.      which is significant on bigendian machines.)
  250.    STRUCT_ALIGN is the alignment the structure is known to have (in bytes).
  251.  
  252.    Note that protect_from_queue has already been done on OP0 and VALUE.  */
  253.  
  254. static void
  255. store_fixed_bit_field (op0, offset, bitsize, bitpos, value, struct_align)
  256.      register rtx op0;
  257.      register int offset, bitsize, bitpos;
  258.      register rtx value;
  259.      int struct_align;
  260. {
  261.   register enum machine_mode mode;
  262.   int total_bits = BITS_PER_WORD;
  263.   rtx subtarget;
  264.   int all_zero = 0;
  265.   int all_one = 0;
  266.  
  267.   /* Add OFFSET to OP0's address (if it is in memory)
  268.      and if a single byte contains the whole bit field
  269.      change OP0 to a byte.  */
  270.  
  271.   /* There is a case not handled here:
  272.      a structure with a known alignment of just a halfword
  273.      and a field split across two aligned halfwords within the structure.
  274.      Or likewise a structure with a known alignment of just a byte
  275.      and a field split across two bytes.
  276.      Such cases are not supposed to be able to occur.  */
  277.  
  278.   if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
  279.     {
  280.       if (offset != 0)
  281.     abort ();
  282.       /* Special treatment for a bit field split across two registers.  */
  283.       if (bitsize + bitpos > BITS_PER_WORD)
  284.     {
  285.       store_split_bit_field (op0, bitsize, bitpos, value, BITS_PER_WORD);
  286.       return;
  287.     }
  288.     }
  289.   else if (bitsize + bitpos <= BITS_PER_UNIT
  290.        && (! SLOW_BYTE_ACCESS
  291.            || (struct_align == 1
  292.            && BIGGEST_ALIGNMENT > 1)))
  293.     {
  294.       /* It fits in one byte, and either bytes are fast
  295.      or the alignment won't let us use anything bigger.  */
  296.       total_bits = BITS_PER_UNIT;
  297.       op0 = change_address (op0, QImode, 
  298.                 plus_constant (XEXP (op0, 0), offset));
  299.     }
  300.   else if ((bitsize + bitpos + (offset % GET_MODE_SIZE (HImode)) * BITS_PER_UNIT
  301.         <= GET_MODE_BITSIZE (HImode))
  302.        /* If halfwords are fast, use them whenever valid.  */
  303.        && (! SLOW_BYTE_ACCESS
  304.            /* Use halfwords if larger is invalid due to alignment.  */
  305.            || (struct_align == GET_MODE_SIZE (HImode)
  306.            && BIGGEST_ALIGNMENT > GET_MODE_SIZE (HImode))))
  307.     {
  308.       /* It fits in an aligned halfword within the structure,
  309.      and either halfwords are fast
  310.      or the alignment won't let us use anything bigger.  */
  311.       total_bits = GET_MODE_BITSIZE (HImode);
  312.  
  313.       /* Get ref to halfword containing the field.  */
  314.       bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
  315.       offset -= (offset % (total_bits / BITS_PER_UNIT));
  316.       op0 = change_address (op0, HImode, 
  317.                 plus_constant (XEXP (op0, 0), offset));
  318.     }
  319.   else
  320.     {
  321.       /* Get ref to an aligned word containing the field.  */
  322.       /* Adjust BITPOS to be position within a word,
  323.      and OFFSET to be the offset of that word.
  324.      Then alter OP0 to refer to that word.  */
  325.       bitpos += (offset % (BITS_PER_WORD / BITS_PER_UNIT)) * BITS_PER_UNIT;
  326.       offset -= (offset % (BITS_PER_WORD / BITS_PER_UNIT));
  327.       op0 = change_address (op0, SImode,
  328.                 plus_constant (XEXP (op0, 0), offset));
  329.  
  330.       /* Special treatment for a bit field split across two aligned words.  */
  331.       if (bitsize + bitpos > BITS_PER_WORD)
  332.     {
  333.       store_split_bit_field (op0, bitsize, bitpos, value, struct_align);
  334.       return;
  335.     }
  336.     }
  337.  
  338.   mode = GET_MODE (op0);
  339.  
  340.   /* Now MODE is either QImode, HImode or SImode for a MEM as OP0,
  341.      or is SImode for a REG as OP0.  TOTAL_BITS corresponds.
  342.      The bit field is contained entirely within OP0.
  343.      BITPOS is the starting bit number within OP0.
  344.      (OP0's mode may actually be narrower than MODE.)  */
  345.  
  346. #ifdef BYTES_BIG_ENDIAN
  347.   /* BITPOS is the distance between our msb
  348.      and that of the containing datum.
  349.      Convert it to the distance from the lsb.  */
  350.  
  351.   bitpos = total_bits - bitsize - bitpos;
  352. #endif
  353.   /* Now BITPOS is always the distance between our lsb
  354.      and that of OP0.  */
  355.  
  356.   /* Shift VALUE left by BITPOS bits.  If VALUE is not constant,
  357.      we must first convert its mode to MODE.  */
  358.  
  359.   if (GET_CODE (value) == CONST_INT)
  360.     {
  361.       register int v = INTVAL (value);
  362.  
  363.       if (bitsize < HOST_BITS_PER_INT)
  364.     v &= (1 << bitsize) - 1;
  365.  
  366.       if (v == 0)
  367.     all_zero = 1;
  368.       else if (bitsize < HOST_BITS_PER_INT && v == (1 << bitsize) - 1)
  369.     all_one = 1;
  370.  
  371.       value = gen_rtx (CONST_INT, VOIDmode, v << bitpos);
  372.     }
  373.   else
  374.     {
  375.       int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize);
  376.  
  377.       if (GET_MODE (value) != mode)
  378.     {
  379.       if ((GET_CODE (value) == REG || GET_CODE (value) == SUBREG)
  380.           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (value)))
  381.         value = gen_lowpart (mode, value);
  382.       else
  383.         value = convert_to_mode (mode, value, 1);
  384.     }
  385.  
  386.       if (must_and && bitsize < HOST_BITS_PER_INT)
  387.     value = expand_bit_and (mode, value,
  388.                 gen_rtx (CONST_INT, VOIDmode,
  389.                      (1 << bitsize) - 1),
  390.                 0);
  391.       if (bitpos > 0)
  392.     value = expand_shift (LSHIFT_EXPR, mode, value,
  393.                   build_int_2 (bitpos, 0), 0, 1);
  394.     }
  395.  
  396.   /* Now clear the chosen bits in OP0,
  397.      except that if VALUE is -1 we need not bother.  */
  398.  
  399.   subtarget = op0;
  400.  
  401.   if (! all_one)
  402.     subtarget = expand_bit_and (mode, op0,
  403.                 gen_rtx (CONST_INT, VOIDmode, 
  404.                      (~ (((1 << bitsize) - 1) << bitpos))
  405.                      & ((GET_MODE_BITSIZE (mode)
  406.                          == HOST_BITS_PER_INT)
  407.                         ? -1
  408.                         : ((1 << GET_MODE_BITSIZE (mode)) - 1))),
  409.                 subtarget);
  410.  
  411.   /* Now logical-or VALUE into OP0, unless it is zero.  */
  412.  
  413.   if (! all_zero)
  414.     subtarget = expand_binop (mode, ior_optab, subtarget, value,
  415.                   op0, 1, OPTAB_LIB_WIDEN);
  416.   if (op0 != subtarget)
  417.     emit_move_insn (op0, subtarget);
  418. }
  419.  
  420. /* Store a bit field that is split across two words.
  421.  
  422.    OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
  423.    BITSIZE is the field width; BITPOS the position of its first bit
  424.    (within the word).
  425.    VALUE is the value to store.  */
  426.  
  427. static void
  428. store_split_bit_field (op0, bitsize, bitpos, value, align)
  429.      rtx op0;
  430.      int bitsize, bitpos;
  431.      rtx value;
  432.      int align;
  433. {
  434.   /* BITSIZE_1 is size of the part in the first word.  */
  435.   int bitsize_1 = BITS_PER_WORD - bitpos;
  436.   /* BITSIZE_2 is size of the rest (in the following word).  */
  437.   int bitsize_2 = bitsize - bitsize_1;
  438.   rtx part1, part2;
  439.  
  440.   if (GET_MODE (value) != VOIDmode)
  441.     value = convert_to_mode (SImode, value, 1);
  442.   if (CONSTANT_P (value) && GET_CODE (value) != CONST_INT)
  443.     value = copy_to_reg (value);
  444.  
  445.   /* Split the value into two parts:
  446.      PART1 gets that which goes in the first word; PART2 the other.  */
  447. #ifdef BYTES_BIG_ENDIAN
  448.   /* PART1 gets the more significant part.  */
  449.   if (GET_CODE (value) == CONST_INT)
  450.     {
  451.       part1 = gen_rtx (CONST_INT, VOIDmode,
  452.                (unsigned) (INTVAL (value)) >> bitsize_2);
  453.       part2 = gen_rtx (CONST_INT, VOIDmode,
  454.                (unsigned) (INTVAL (value)) & ((1 << bitsize_2) - 1));
  455.     }
  456.   else
  457.     {
  458.       part1 = extract_fixed_bit_field (SImode, value, 0, bitsize_1,
  459.                        BITS_PER_WORD - bitsize, 0, 1);
  460.       part2 = extract_fixed_bit_field (SImode, value, 0, bitsize_2,
  461.                        BITS_PER_WORD - bitsize_2, 0, 1);
  462.     }
  463. #else
  464.   /* PART1 gets the less significant part.  */
  465.   if (GET_CODE (value) == CONST_INT)
  466.     {
  467.       part1 = gen_rtx (CONST_INT, VOIDmode,
  468.                (unsigned) (INTVAL (value)) & ((1 << bitsize_1) - 1));
  469.       part2 = gen_rtx (CONST_INT, VOIDmode,
  470.                (unsigned) (INTVAL (value)) >> bitsize_1);
  471.     }
  472.   else
  473.     {
  474.       part1 = extract_fixed_bit_field (SImode, value, 0, bitsize_1, 0, 0, 1);
  475.       part2 = extract_fixed_bit_field (SImode, value, 0, bitsize_2,
  476.                        bitsize_1, 0, 1);
  477.     }
  478. #endif
  479.  
  480.   /* Store PART1 into the first word.  */
  481.   store_fixed_bit_field (op0, 0, bitsize_1, bitpos, part1, align);
  482.  
  483.   /* Offset op0 to get to the following word.  */
  484.   if (GET_CODE (op0) == MEM)
  485.     op0 = change_address (op0, SImode,
  486.               plus_constant (XEXP (op0, 0), UNITS_PER_WORD));
  487.   else if (GET_CODE (op0) == REG)
  488.     op0 = gen_rtx (SUBREG, SImode, op0, 1);
  489.   else if (GET_CODE (op0) == SUBREG)
  490.     op0 = gen_rtx (SUBREG, SImode, SUBREG_REG (op0), SUBREG_WORD (op0) + 1);
  491.  
  492.   /* Store PART2 into the second word.  */
  493.   store_fixed_bit_field (op0, 0, bitsize_2, 0, part2, align);
  494. }
  495.  
  496. /* Generate code to extract a byte-field from STR_RTX
  497.    containing BITSIZE bits, starting at BITNUM,
  498.    and put it in TARGET if possible (if TARGET is nonzero).
  499.    Regardless of TARGET, we return the rtx for where the value is placed.
  500.    It may be a QUEUED.
  501.  
  502.    STR_RTX is the structure containing the byte (a REG or MEM).
  503.    UNSIGNEDP is nonzero if this is an unsigned bit field.
  504.    MODE is the natural mode of the field value once extracted.
  505.    TMODE is the mode the caller would like the value to have;
  506.    but the value may be returned with type MODE instead.
  507.  
  508.    ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
  509.  
  510.    If a TARGET is specified and we can store in it at no extra cost,
  511.    we do so, and return TARGET.
  512.    Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
  513.    if they are equally easy.  */
  514.  
  515. rtx
  516. extract_bit_field (str_rtx, bitsize, bitnum, unsignedp,
  517.            target, mode, tmode, align)
  518.      rtx str_rtx;
  519.      register int bitsize;
  520.      int bitnum;
  521.      int unsignedp;
  522.      rtx target;
  523.      enum machine_mode mode, tmode;
  524.      int align;
  525. {
  526.   int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
  527.   register int offset = bitnum / unit;
  528.   register int bitpos = bitnum % unit;
  529.   register rtx op0 = str_rtx;
  530.   rtx spec_target = target;
  531.   rtx bitsize_rtx, bitpos_rtx;
  532.   rtx spec_target_subreg = 0;
  533.  
  534.   if (tmode == VOIDmode)
  535.     tmode = mode;
  536.  
  537.   while (GET_CODE (op0) == SUBREG)
  538.     {
  539.       offset += SUBREG_WORD (op0);
  540.       op0 = SUBREG_REG (op0);
  541.     }
  542.   
  543. #ifdef BYTES_BIG_ENDIAN
  544.   /* If OP0 is a register, BITPOS must count within a word.
  545.      But as we have it, it counts within whatever size OP0 now has.
  546.      On a bigendian machine, these are not the same, so convert.  */
  547.   if (GET_CODE (op0) != MEM && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
  548.     bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
  549. #endif
  550.  
  551.   /* Extracting a full-word or multi-word value
  552.      from a structure in a register.
  553.      This can be done with just SUBREG.
  554.      So too extracting a subword value in
  555.      the least significant part of the register.  */
  556.  
  557.   if (GET_CODE (op0) == REG
  558.       && (bitsize >= BITS_PER_WORD
  559.       || ((bitsize == GET_MODE_BITSIZE (mode)
  560.            || bitsize == GET_MODE_BITSIZE (QImode)
  561.            || bitsize == GET_MODE_BITSIZE (HImode))
  562. #ifdef BYTES_BIG_ENDIAN
  563.           && bitpos + bitsize == BITS_PER_WORD
  564. #else
  565.           && bitpos == 0
  566. #endif
  567.           )))
  568.     {
  569.       enum machine_mode mode1 = mode;
  570.  
  571.       if (bitsize == GET_MODE_BITSIZE (QImode))
  572.     mode1 = QImode;
  573.       if (bitsize == GET_MODE_BITSIZE (HImode))
  574.     mode1 = HImode;
  575.  
  576.       if (mode1 != GET_MODE (op0))
  577.     op0 = gen_rtx (SUBREG, mode1, op0, offset);
  578.  
  579.       if (mode1 != mode)
  580.     return convert_to_mode (tmode, op0, unsignedp);
  581.       return op0;
  582.     }
  583.   
  584.   /* From here on we know the desired field is smaller than a word
  585.      so we can assume it is an integer.  So we can safely extract it as one
  586.      size of integer, if necessary, and then truncate or extend
  587.      to the size that is wanted.  */
  588.  
  589.   /* OFFSET is the number of words or bytes (UNIT says which)
  590.      from STR_RTX to the first word or byte containing part of the field.  */
  591.  
  592.   if (GET_CODE (op0) == REG)
  593.     {
  594.       if (offset != 0
  595.       || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (SImode))
  596.     op0 = gen_rtx (SUBREG, SImode, op0, offset);
  597.       offset = 0;
  598.     }
  599.   else
  600.     {
  601.       op0 = protect_from_queue (str_rtx, 1);
  602.     }
  603.  
  604.   /* Now OFFSET is nonzero only for memory operands.  */
  605.  
  606.   if (unsignedp)
  607.     {
  608. #ifdef HAVE_extzv
  609.       if (HAVE_extzv)
  610.     {
  611.       int xbitpos = bitpos, xoffset = offset;
  612.       rtx last = get_last_insn();
  613.       rtx xop0 = op0;
  614.       rtx xtarget = target;
  615.       rtx xspec_target = spec_target;
  616.       rtx xspec_target_subreg = spec_target_subreg;
  617.       rtx pat;
  618.  
  619.       /* Get ref to first byte containing part of the field.  */
  620.       if (GET_CODE (xop0) == MEM)
  621.         {
  622.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][1])
  623.              (xop0, GET_MODE (xop0))))
  624.         {
  625.           /* If memory isn't acceptable for this operand,
  626.              copy it to a register.  */
  627.           unit = BITS_PER_WORD;
  628.           xoffset = bitnum / unit;
  629.           xbitpos = bitnum % unit;
  630.           xop0 = change_address (xop0, SImode,
  631.                      plus_constant (XEXP (xop0, 0),
  632.                             xoffset * UNITS_PER_WORD));
  633.           xop0 = force_reg (GET_MODE (xop0), xop0);
  634. #ifdef BITS_BIG_ENDIAN
  635.           if (unit > GET_MODE_BITSIZE (GET_MODE (xop0)))
  636.             xbitpos += unit - GET_MODE_BITSIZE (GET_MODE (xop0));
  637. #endif
  638.         }
  639.           else
  640.         xop0 = change_address (xop0, QImode,
  641.                        plus_constant (XEXP (xop0, 0), xoffset));
  642.         }
  643.  
  644.       /* If op0 is a register, we need it in SImode
  645.          to make it acceptable to the format of extzv.  */
  646.       if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != SImode)
  647.         abort ();
  648.       if (GET_CODE (xop0) == REG && GET_MODE (xop0) != SImode)
  649.         xop0 = gen_rtx (SUBREG, SImode, xop0, 0);
  650.  
  651.       if (xtarget == 0
  652.           || (flag_force_mem && GET_CODE (xtarget) == MEM))
  653.         xtarget = xspec_target = gen_reg_rtx (tmode);
  654.  
  655.       if (GET_MODE (xtarget) != SImode)
  656.         {
  657.           if (GET_CODE (xtarget) == REG)
  658.         xspec_target_subreg = xtarget = gen_lowpart (SImode, xtarget);
  659.           else
  660.         xtarget = gen_reg_rtx (SImode);
  661.         }
  662.  
  663.       /* If this machine's extzv insists on a register target,
  664.          make sure we have one.  */
  665.       if (! (*insn_operand_predicate[(int) CODE_FOR_extzv][0]) (xtarget, SImode))
  666.         xtarget = gen_reg_rtx (SImode);
  667.  
  668.       /* On big-endian machines, we count bits from the most significant.
  669.          If the bit field insn does not, we must invert.  */
  670. #if defined (BITS_BIG_ENDIAN) != defined (BYTES_BIG_ENDIAN)
  671.       xbitpos = unit - 1 - xbitpos;
  672. #endif
  673.  
  674.       bitsize_rtx = gen_rtx (CONST_INT, VOIDmode, bitsize);
  675.       bitpos_rtx = gen_rtx (CONST_INT, VOIDmode, xbitpos);
  676.  
  677.       pat = gen_extzv (protect_from_queue (xtarget, 1),
  678.                xop0, bitsize_rtx, bitpos_rtx);
  679.       if (pat)
  680.         {
  681.           emit_insn (pat);
  682.           target = xtarget;
  683.           spec_target = xspec_target;
  684.           spec_target_subreg = xspec_target_subreg;
  685.         }
  686.       else
  687.         {
  688.           delete_insns_since (last);
  689.           target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
  690.                         bitpos, target, 1, align);
  691.         }
  692.     }
  693.       else
  694. #endif
  695.     target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  696.                       target, 1, align);
  697.     }
  698.   else
  699.     {
  700. #ifdef HAVE_extv
  701.       if (HAVE_extv)
  702.     {
  703.       int xbitpos = bitpos, xoffset = offset;
  704.       rtx last = get_last_insn();
  705.       rtx xop0 = op0, xtarget = target;
  706.       rtx xspec_target = spec_target;
  707.       rtx xspec_target_subreg = spec_target_subreg;
  708.       rtx pat;
  709.  
  710.       /* Get ref to first byte containing part of the field.  */
  711.       if (GET_CODE (xop0) == MEM)
  712.         {
  713.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extv][1])
  714.              (xop0, GET_MODE (xop0))))
  715.         {
  716.           /* If memory isn't acceptable for this operand,
  717.              copy it to a register.  */
  718.           unit = BITS_PER_WORD;
  719.           xoffset = bitnum / unit;
  720.           xbitpos = bitnum % unit;
  721.           xop0 = change_address (xop0, SImode,
  722.                      plus_constant (XEXP (xop0, 0),
  723.                             xoffset * UNITS_PER_WORD));
  724.           xop0 = force_reg (GET_MODE (xop0), xop0);
  725. #ifdef BITS_BIG_ENDIAN
  726.           if (unit > GET_MODE_BITSIZE (GET_MODE (xop0)))
  727.             xbitpos += unit - GET_MODE_BITSIZE (GET_MODE (xop0));
  728. #endif
  729.         }
  730.           else
  731.         xop0 = change_address (xop0, QImode,
  732.                        plus_constant (XEXP (xop0, 0), xoffset));
  733.         }
  734.  
  735.       /* If op0 is a register, we need it in SImode
  736.          to make it acceptable to the format of extv.  */
  737.       if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != SImode)
  738.         abort ();
  739.       if (GET_CODE (xop0) == REG && GET_MODE (xop0) != SImode)
  740.         xop0 = gen_rtx (SUBREG, SImode, xop0, 0);
  741.  
  742.       if (xtarget == 0
  743.           || (flag_force_mem && GET_CODE (xtarget) == MEM))
  744.         xtarget = xspec_target = gen_reg_rtx (tmode);
  745.  
  746.       if (GET_MODE (xtarget) != SImode)
  747.         {
  748.           if (GET_CODE (xtarget) == REG)
  749.         xspec_target_subreg = xtarget = gen_lowpart (SImode, xtarget);
  750.           else
  751.         xtarget = gen_reg_rtx (SImode);
  752.         }
  753.  
  754.       /* If this machine's extv insists on a register target,
  755.          make sure we have one.  */
  756.       if (! (*insn_operand_predicate[(int) CODE_FOR_extv][0]) (xtarget, SImode))
  757.         xtarget = gen_reg_rtx (SImode);
  758.  
  759.       /* On big-endian machines, we count bits from the most significant.
  760.          If the bit field insn does not, we must invert.  */
  761. #if defined (BITS_BIG_ENDIAN) != defined (BYTES_BIG_ENDIAN)
  762.       xbitpos = unit - 1 - xbitpos;
  763. #endif
  764.  
  765.       bitsize_rtx = gen_rtx (CONST_INT, VOIDmode, bitsize);
  766.       bitpos_rtx = gen_rtx (CONST_INT, VOIDmode, xbitpos);
  767.  
  768.       pat = gen_extv (protect_from_queue (xtarget, 1),
  769.               xop0, bitsize_rtx, bitpos_rtx);
  770.       if (pat)
  771.         {
  772.           emit_insn (pat);
  773.           target = xtarget;
  774.           spec_target = xspec_target;
  775.           spec_target_subreg = xspec_target_subreg;
  776.         }
  777.       else
  778.         {
  779.           delete_insns_since (last);
  780.           target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
  781.                         bitpos, target, 0, align);
  782.         }
  783.     } 
  784.       else
  785. #endif
  786.     target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  787.                       target, 0, align);
  788.     }
  789.   if (target == spec_target)
  790.     return target;
  791.   if (target == spec_target_subreg)
  792.     return spec_target;
  793.   if (GET_MODE (target) != tmode && GET_MODE (target) != mode)
  794.     return convert_to_mode (tmode, target, unsignedp);
  795.   return target;
  796. }
  797.  
  798. /* Extract a bit field using shifts and boolean operations
  799.    Returns an rtx to represent the value.
  800.    OP0 addresses a register (word) or memory (byte).
  801.    BITPOS says which bit within the word or byte the bit field starts in.
  802.    OFFSET says how many bytes farther the bit field starts;
  803.     it is 0 if OP0 is a register.
  804.    BITSIZE says how many bits long the bit field is.
  805.     (If OP0 is a register, it may be narrower than SImode,
  806.      but BITPOS still counts within a full word,
  807.      which is significant on bigendian machines.)
  808.  
  809.    UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
  810.    If TARGET is nonzero, attempts to store the value there
  811.    and return TARGET, but this is not guaranteed.
  812.    If TARGET is not used, create a pseudo-reg of mode TMODE for the value.
  813.  
  814.    ALIGN is the alignment that STR_RTX is known to have, measured in bytes.  */
  815.  
  816. static rtx
  817. extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  818.              target, unsignedp, align)
  819.      enum machine_mode tmode;
  820.      register rtx op0, target;
  821.      register int offset, bitsize, bitpos;
  822.      int unsignedp;
  823.      int align;
  824. {
  825.   int total_bits = BITS_PER_WORD;
  826.   enum machine_mode mode;
  827.  
  828.   if (GET_CODE (op0) == SUBREG || GET_CODE (op0) == REG)
  829.     {
  830.       /* Special treatment for a bit field split across two registers.  */
  831.       if (bitsize + bitpos > BITS_PER_WORD)
  832.     return extract_split_bit_field (op0, bitsize, bitpos,
  833.                     unsignedp, align);
  834.     }
  835.   else if (bitsize + bitpos <= BITS_PER_UNIT
  836.        && (! SLOW_BYTE_ACCESS
  837.            || (align == 1
  838.            && BIGGEST_ALIGNMENT > 1)))
  839.     {
  840.       /* It fits in one byte, and either bytes are fast
  841.      or the alignment won't let us use anything bigger.  */
  842.       total_bits = BITS_PER_UNIT;
  843.       op0 = change_address (op0, QImode, 
  844.                 plus_constant (XEXP (op0, 0), offset));
  845.     }
  846.   else if ((bitsize + bitpos + (offset % GET_MODE_SIZE (HImode)) * BITS_PER_UNIT
  847.         <= GET_MODE_BITSIZE (HImode))
  848.        /* If halfwords are fast, use them whenever valid.  */
  849.        && (! SLOW_BYTE_ACCESS
  850.            /* Use halfwords if larger is invalid due to alignment.  */
  851.            || (align == GET_MODE_SIZE (HImode)
  852.            && BIGGEST_ALIGNMENT > GET_MODE_SIZE (HImode))))
  853.     {
  854.       /* It fits in an aligned halfword, and either halfwords are fast
  855.      or the alignment won't let us use anything bigger.  */
  856.       total_bits = GET_MODE_BITSIZE (HImode);
  857.  
  858.       /* Get ref to halfword containing the field.  */
  859.       bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
  860.       offset -= (offset % (total_bits / BITS_PER_UNIT));
  861.       op0 = change_address (op0, HImode, 
  862.                 plus_constant (XEXP (op0, 0), offset));
  863.     }
  864.   else
  865.     {
  866.       /* Get ref to word containing the field.  */
  867.       /* Adjust BITPOS to be position within a word,
  868.      and OFFSET to be the offset of that word.  */
  869.       bitpos += (offset % (BITS_PER_WORD / BITS_PER_UNIT)) * BITS_PER_UNIT;
  870.       offset -= (offset % (BITS_PER_WORD / BITS_PER_UNIT));
  871.       op0 = change_address (op0, SImode,
  872.                 plus_constant (XEXP (op0, 0), offset));
  873.  
  874.       /* Special treatment for a bit field split across two words.  */
  875.       if (bitsize + bitpos > BITS_PER_WORD)
  876.     return extract_split_bit_field (op0, bitsize, bitpos,
  877.                     unsignedp, align);
  878.     }
  879.  
  880.   mode = GET_MODE (op0);
  881.  
  882. #ifdef BYTES_BIG_ENDIAN
  883.   /* BITPOS is the distance between our msb and that of OP0.
  884.      Convert it to the distance from the lsb.  */
  885.  
  886.   bitpos = total_bits - bitsize - bitpos;
  887. #endif
  888.   /* Now BITPOS is always the distance between the field's lsb and that of OP0.
  889.      We have reduced the big-endian case to the little-endian case.  */
  890.  
  891.   if (unsignedp)
  892.     {
  893.       if (bitpos)
  894.     {
  895.       /* If the field does not already start at the lsb,
  896.          shift it so it does.  */
  897.       tree amount = build_int_2 (bitpos, 0);
  898.       /* Maybe propagate the target for the shift.  */
  899.       /* But not if we will return it--could confuse integrate.c.  */
  900.       rtx subtarget = (target != 0 && GET_CODE (target) == REG
  901.                && !REG_FUNCTION_VALUE_P (target)
  902.                ? target : 0);
  903.       if (tmode != mode) subtarget = 0;
  904.       op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
  905.     }
  906.       /* Convert the value to the desired mode.  */
  907.       if (mode != tmode)
  908.     op0 = convert_to_mode (tmode, op0, 1);
  909.  
  910.       /* Unless the msb of the field used to be the msb when we shifted,
  911.      mask out the upper bits.  */
  912.  
  913.       if ((GET_MODE_BITSIZE (mode) != bitpos + bitsize
  914. #if 0
  915. #ifdef SLOW_ZERO_EXTEND
  916.        /* Always generate an `and' if
  917.           we just zero-extended op0 and SLOW_ZERO_EXTEND, since it
  918.           will combine fruitfully with the zero-extend. */
  919.        || tmode != mode
  920. #endif
  921. #endif
  922.        )
  923.       && bitsize < HOST_BITS_PER_INT)
  924.     return expand_bit_and (GET_MODE (op0), op0,
  925.                    gen_rtx (CONST_INT, VOIDmode, (1 << bitsize) - 1),
  926.                    target);
  927.       return op0;
  928.     }
  929.  
  930.   /* To extract a signed bit-field, first shift its msb to the msb of the word,
  931.      then arithmetic-shift its lsb to the lsb of the word.  */
  932.   op0 = force_reg (mode, op0);
  933.   if (mode != tmode)
  934.     target = 0;
  935.   if (GET_MODE_BITSIZE (QImode) < GET_MODE_BITSIZE (mode)
  936.       && GET_MODE_BITSIZE (QImode) >= bitsize + bitpos)
  937.     mode = QImode, op0 = convert_to_mode (QImode, op0, 0);
  938.   if (GET_MODE_BITSIZE (HImode) < GET_MODE_BITSIZE (mode)
  939.       && GET_MODE_BITSIZE (HImode) >= bitsize + bitpos)
  940.     mode = HImode, op0 = convert_to_mode (HImode, op0, 0);
  941.   if (GET_MODE_BITSIZE (mode) != (bitsize + bitpos))
  942.     {
  943.       tree amount = build_int_2 (GET_MODE_BITSIZE (mode) - (bitsize + bitpos), 0);
  944.       /* Maybe propagate the target for the shift.  */
  945.       /* But not if we will return the result--could confuse integrate.c.  */
  946.       rtx subtarget = (target != 0 && GET_CODE (target) == REG
  947.                && ! REG_FUNCTION_VALUE_P (target)
  948.                ? target : 0);
  949.       op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
  950.     }
  951.  
  952.   return expand_shift (RSHIFT_EXPR, mode, op0,
  953.                build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0), 
  954.                target, 0);
  955. }
  956.  
  957. /* Extract a bit field that is split across two words
  958.    and return an RTX for the result.
  959.  
  960.    OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
  961.    BITSIZE is the field width; BITPOS, position of its first bit, in the word.
  962.    UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend.  */
  963.  
  964. static rtx
  965. extract_split_bit_field (op0, bitsize, bitpos, unsignedp, align)
  966.      rtx op0;
  967.      int bitsize, bitpos, unsignedp, align;
  968. {
  969.   /* BITSIZE_1 is size of the part in the first word.  */
  970.   int bitsize_1 = BITS_PER_WORD - bitpos;
  971.   /* BITSIZE_2 is size of the rest (in the following word).  */
  972.   int bitsize_2 = bitsize - bitsize_1;
  973.   rtx part1, part2, result;
  974.  
  975.   /* Get the part of the bit field from the first word.  */
  976.   part1 = extract_fixed_bit_field (SImode, op0, 0, bitsize_1, bitpos,
  977.                    0, 1, align);
  978.  
  979.   /* Offset op0 by 1 word to get to the following one.  */
  980.   if (GET_CODE (op0) == MEM)
  981.     op0 = change_address (op0, SImode,
  982.               plus_constant (XEXP (op0, 0), UNITS_PER_WORD));
  983.   else if (GET_CODE (op0) == REG)
  984.     op0 = gen_rtx (SUBREG, SImode, op0, 1);
  985.   else
  986.     op0 = gen_rtx (SUBREG, SImode, SUBREG_REG (op0), SUBREG_WORD (op0) + 1);
  987.  
  988.   /* Get the part of the bit field from the second word.  */
  989.   part2 = extract_fixed_bit_field (SImode, op0, 0, bitsize_2, 0, 0, 1, align);
  990.  
  991.   /* Shift the more significant part up to fit above the other part.  */
  992. #ifdef BYTES_BIG_ENDIAN
  993.   part1 = expand_shift (LSHIFT_EXPR, SImode, part1,
  994.             build_int_2 (bitsize_2, 0), 0, 1);
  995. #else
  996.   part2 = expand_shift (LSHIFT_EXPR, SImode, part2,
  997.             build_int_2 (bitsize_1, 0), 0, 1);
  998. #endif
  999.  
  1000.   /* Combine the two parts with bitwise or.  This works
  1001.      because we extracted both parts as unsigned bit fields.  */
  1002.   result = expand_binop (SImode, ior_optab, part1, part2, 0, 1,
  1003.              OPTAB_LIB_WIDEN);
  1004.  
  1005.   /* Unsigned bit field: we are done.  */
  1006.   if (unsignedp)
  1007.     return result;
  1008.   /* Signed bit field: sign-extend with two arithmetic shifts.  */
  1009.   result = expand_shift (LSHIFT_EXPR, SImode, result,
  1010.              build_int_2 (BITS_PER_WORD - bitsize, 0), 0, 0);
  1011.   return expand_shift (RSHIFT_EXPR, SImode, result,
  1012.                build_int_2 (BITS_PER_WORD - bitsize, 0), 0, 0);
  1013. }
  1014.  
  1015. /* Add INC into TARGET.  */
  1016.  
  1017. void
  1018. expand_inc (target, inc)
  1019.      rtx target, inc;
  1020. {
  1021.   rtx value = expand_binop (GET_MODE (target), add_optab,
  1022.                 target, inc,
  1023.                 target, 0, OPTAB_LIB_WIDEN);
  1024.   if (value != target)
  1025.     emit_move_insn (target, value);
  1026. }
  1027.  
  1028. /* Subtract INC from TARGET.  */
  1029.  
  1030. void
  1031. expand_dec (target, dec)
  1032.      rtx target, dec;
  1033. {
  1034.   rtx value = expand_binop (GET_MODE (target), sub_optab,
  1035.                 target, dec,
  1036.                 target, 0, OPTAB_LIB_WIDEN);
  1037.   if (value != target)
  1038.     emit_move_insn (target, value);
  1039. }
  1040.  
  1041. /* Output a shift instruction for expression code CODE,
  1042.    with SHIFTED being the rtx for the value to shift,
  1043.    and AMOUNT the tree for the amount to shift by.
  1044.    Store the result in the rtx TARGET, if that is convenient.
  1045.    If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
  1046.    Return the rtx for where the value is.  */
  1047.  
  1048. /* Pastel, for shifts, converts shift count to SImode here
  1049.    independent of the mode being shifted.
  1050.    Should that be done in an earlier pass?
  1051.    It turns out not to matter for C.  */
  1052.  
  1053. rtx
  1054. expand_shift (code, mode, shifted, amount, target, unsignedp)
  1055.      enum tree_code code;
  1056.      register enum machine_mode mode;
  1057.      rtx shifted;
  1058.      tree amount;
  1059.      register rtx target;
  1060.      int unsignedp;
  1061. {
  1062.   register rtx op1, temp = 0;
  1063.   register int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
  1064.   int try;
  1065.   int rotate = code == LROTATE_EXPR || code == RROTATE_EXPR;
  1066.   rtx last;
  1067.  
  1068.   /* Previously detected shift-counts computed by NEGATE_EXPR
  1069.      and shifted in the other direction; but that does not work
  1070.      on all machines.  */
  1071.  
  1072.   op1 = expand_expr (amount, 0, VOIDmode, 0);
  1073.  
  1074.   last = get_last_insn ();
  1075.  
  1076.   for (try = 0; temp == 0 && try < 3; try++)
  1077.     {
  1078.       enum optab_methods methods;
  1079.       delete_insns_since (last);
  1080.  
  1081.       if (try == 0)
  1082.     methods = OPTAB_DIRECT;
  1083.       else if (try == 1)
  1084.     methods = OPTAB_WIDEN;
  1085.       else
  1086.     methods = OPTAB_LIB_WIDEN;
  1087.  
  1088.       if (rotate)
  1089.     {
  1090.       /* Widening does not work for rotation.  */
  1091.       if (methods != OPTAB_DIRECT)
  1092.         methods = OPTAB_LIB;
  1093.  
  1094.       temp = expand_binop (mode,
  1095.                    left ? rotl_optab : rotr_optab,
  1096.                    shifted, op1, target, -1, methods);
  1097.     }
  1098.       else if (unsignedp)
  1099.     {
  1100.       temp = expand_binop (mode,
  1101.                    left ? lshl_optab : lshr_optab,
  1102.                    shifted, op1, target, unsignedp, methods);
  1103.       if (temp == 0 && left)
  1104.         temp = expand_binop (mode, ashl_optab,
  1105.                  shifted, op1, target, unsignedp, methods);
  1106.       if (temp != 0)
  1107.         return temp;
  1108.     }
  1109.       /* Do arithmetic shifts.
  1110.      Also, if we are going to widen the operand, we can just as well
  1111.      use an arithmetic right-shift instead of a logical one.  */
  1112.       if (! rotate && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
  1113.     {
  1114.       /* Arithmetic shift */
  1115.  
  1116.       temp = expand_binop (mode,
  1117.                    left ? ashl_optab : ashr_optab,
  1118.                    shifted, op1, target, unsignedp, methods);
  1119.       if (temp != 0)
  1120.         return temp;
  1121.     }
  1122.  
  1123.       if (unsignedp)
  1124.     {
  1125.       /* No logical shift insn in either direction =>
  1126.          try a bit-field extract instruction if we have one.  */
  1127. #ifdef HAVE_extzv
  1128. #ifndef BITS_BIG_ENDIAN
  1129.       if (HAVE_extzv && !left
  1130.           && ((methods == OPTAB_DIRECT && mode == SImode)
  1131.           || (methods == OPTAB_WIDEN
  1132.               && GET_MODE_SIZE (mode) < GET_MODE_SIZE (SImode))))
  1133.         {
  1134.           rtx shifted1 = convert_to_mode (SImode, shifted, 1);
  1135.           rtx target1 = target;
  1136.  
  1137.           /* If -fforce-mem, don't let the operand be in memory.  */
  1138.           if (flag_force_mem && GET_CODE (shifted1) == MEM)
  1139.         shifted1 = force_not_mem (shifted1);
  1140.  
  1141.           /* If this machine's extzv insists on a register for
  1142.          operand 1, arrange for that.  */
  1143.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][1])
  1144.              (shifted1, SImode)))
  1145.         shifted1 = force_reg (SImode, shifted1);
  1146.  
  1147.           /* If we don't have or cannot use a suggested target,
  1148.          make a place for the result, in the proper mode.  */
  1149.           if (methods == OPTAB_WIDEN || target1 == 0
  1150.           || ! ((*insn_operand_predicate[(int) CODE_FOR_extzv][0])
  1151.             (target1, SImode)))
  1152.         target1 = gen_reg_rtx (SImode);
  1153.  
  1154.           op1 = convert_to_mode (SImode, op1, 0);
  1155.  
  1156.           /* If this machine's extzv insists on a register for
  1157.          operand 3, arrange for that.  */
  1158.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][3])
  1159.              (op1, SImode)))
  1160.         op1 = force_reg (SImode, op1);
  1161.  
  1162.           op1 = protect_from_queue (op1, 1);
  1163.  
  1164.           /* TEMP gets the width of the bit field to extract:
  1165.          wordsize minus # bits to shift by.  */
  1166.           if (GET_CODE (op1) == CONST_INT)
  1167.         temp = gen_rtx (CONST_INT, VOIDmode,
  1168.                 (GET_MODE_BITSIZE (mode) - INTVAL (op1)));
  1169.           else
  1170.         temp = expand_binop (SImode, sub_optab,
  1171.                      gen_rtx (CONST_INT, VOIDmode,
  1172.                           GET_MODE_BITSIZE (mode)),
  1173.                      op1, gen_reg_rtx (SImode),
  1174.                      0, OPTAB_LIB_WIDEN);
  1175.           /* Now extract with width TEMP, omitting OP1 least sig bits.  */
  1176.           emit_insn (gen_extzv (protect_from_queue (target1, 1),
  1177.                     protect_from_queue (shifted1, 0),
  1178.                     temp, op1));
  1179.           return convert_to_mode (mode, target1, 1);
  1180.         }
  1181.       /* Can also do logical shift with signed bit-field extract
  1182.          followed by inserting the bit-field at a different position.
  1183.          That strategy is not yet implemented.  */
  1184. #endif /* not BITS_BIG_ENDIAN */
  1185. #endif /* HAVE_extzv */
  1186.       /* We have failed to generate the logical shift and will abort.  */
  1187.     }
  1188.     }
  1189.   if (temp == 0)
  1190.     abort ();
  1191.   return temp;
  1192. }
  1193.  
  1194. /* Output an instruction or two to bitwise-and OP0 with OP1
  1195.    in mode MODE, with output to TARGET if convenient and TARGET is not zero.
  1196.    Returns where the result is.  */
  1197. /* This function used to do more; now it could be eliminated.  */
  1198.  
  1199. rtx
  1200. expand_bit_and (mode, op0, op1, target)
  1201.      enum machine_mode mode;
  1202.      rtx op0, op1, target;
  1203. {
  1204.   register rtx temp;
  1205.  
  1206.   /* First try to open-code it directly.  */
  1207.   temp = expand_binop (mode, and_optab, op0, op1, target, 1, OPTAB_LIB_WIDEN);
  1208.   if (temp == 0)
  1209.     abort ();
  1210.   return temp;
  1211. }
  1212.  
  1213. /* Perform a multiplication and return an rtx for the result.
  1214.    MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
  1215.    TARGET is a suggestion for where to store the result (an rtx).
  1216.  
  1217.    We check specially for a constant integer as OP1.
  1218.    If you want this check for OP0 as well, then before calling
  1219.    you should swap the two operands if OP0 would be constant.  */
  1220.  
  1221. rtx
  1222. expand_mult (mode, op0, op1, target, unsignedp)
  1223.      enum machine_mode mode;
  1224.      register rtx op0, op1, target;
  1225.      int unsignedp;
  1226. {
  1227.   /* Don't use the function value register as a target
  1228.      since we have to read it as well as write it,
  1229.      and function-inlining gets confused by this.  */
  1230.   if (target && REG_P (target) && REG_FUNCTION_VALUE_P (target))
  1231.     target = 0;
  1232.  
  1233.   if (GET_CODE (op1) == CONST_INT)
  1234.     {
  1235.       register int foo;
  1236.       int bar;
  1237.       int negate = INTVAL (op1) < 0;
  1238.       int absval = INTVAL (op1) * (negate ? -1 : 1);
  1239.  
  1240.       /* Is multiplier a power of 2, or minus that?  */
  1241.       foo = exact_log2 (absval);
  1242.       if (foo >= 0)
  1243.     {
  1244.       rtx tem;
  1245.       if (foo == 0)
  1246.         tem = op0;
  1247.       else
  1248.         tem = expand_shift (LSHIFT_EXPR, mode, op0,
  1249.                 build_int_2 (foo, 0),
  1250.                 target, 0);
  1251.       return (negate
  1252.           ? expand_unop (mode, neg_optab, tem, target, 0)
  1253.           : tem);
  1254.     }
  1255.       /* Is multiplier a sum of two powers of 2, or minus that?  */
  1256.       bar = floor_log2 (absval);
  1257.       foo = exact_log2 (absval - (1 << bar));
  1258.       if (bar >= 0 && foo >= 0)
  1259.     {
  1260.       rtx tem =
  1261.         force_operand (gen_rtx (PLUS, mode,
  1262.                     expand_shift (LSHIFT_EXPR, mode, op0,
  1263.                              build_int_2 (bar - foo, 0),
  1264.                              0, 0),
  1265.                     op0),
  1266.                ((foo == 0 && ! negate) ? target : 0));
  1267.  
  1268.       if (foo != 0)
  1269.         tem = expand_shift (LSHIFT_EXPR, mode, tem,
  1270.                 build_int_2 (foo, 0),
  1271.                 negate ? 0 : target, 0);
  1272.  
  1273.       return negate ? expand_unop (mode, neg_optab, tem, target, 0) : tem;
  1274.     }
  1275.     }
  1276.   /* This used to use umul_optab if unsigned,
  1277.      but I think that for non-widening multiply there is no difference
  1278.      between signed and unsigned.  */
  1279.   op0 = expand_binop (mode, smul_optab,
  1280.               op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
  1281.   if (op0 == 0)
  1282.     abort ();
  1283.   return op0;
  1284. }
  1285.  
  1286. /* Emit the code to divide OP0 by OP1, putting the result in TARGET
  1287.    if that is convenient, and returning where the result is.
  1288.    You may request either the quotient or the remainder as the result;
  1289.    specify REM_FLAG nonzero to get the remainder.
  1290.  
  1291.    CODE is the expression code for which kind of division this is;
  1292.    it controls how rounding is done.  MODE is the machine mode to use.
  1293.    UNSIGNEDP nonzero means do unsigned division.  */
  1294.  
  1295. /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
  1296.    and then correct it by or'ing in missing high bits
  1297.    if result of ANDI is nonzero.
  1298.    For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
  1299.    This could optimize to a bfexts instruction.
  1300.    But C doesn't use these operations, so their optimizations are
  1301.    left for later.  */
  1302.  
  1303. rtx
  1304. expand_divmod (rem_flag, code, mode, op0, op1, target, unsignedp)
  1305.      int rem_flag;
  1306.      enum tree_code code;
  1307.      enum machine_mode mode;
  1308.      register rtx op0, op1, target;
  1309.      int unsignedp;
  1310. {
  1311.   register rtx temp;
  1312.   int log = -1;
  1313.   int can_clobber_op0;
  1314.   int mod_insn_no_good = 0;
  1315.   rtx adjusted_op0 = op0;
  1316.  
  1317.   /* Don't use the function value register as a target
  1318.      since we have to read it as well as write it,
  1319.      and function-inlining gets confused by this.  */
  1320.   if (target && REG_P (target) && REG_FUNCTION_VALUE_P (target))
  1321.     target = 0;
  1322.  
  1323.   /* Don't clobber an operand while doing a multi-step calculation.  */
  1324.   if (target)
  1325.     if ((rem_flag && (reg_mentioned_p (target, op0)
  1326.               || (GET_CODE (op0) == MEM && GET_CODE (target) == MEM)))
  1327.     || reg_mentioned_p (target, op1)
  1328.     || (GET_CODE (op1) == MEM && GET_CODE (target) == MEM))
  1329.       target = 0;
  1330.  
  1331.   if (target == 0)
  1332.     target = gen_reg_rtx (mode);
  1333.  
  1334.   can_clobber_op0 = (GET_CODE (op0) == REG && op0 == target);
  1335.  
  1336.   if (GET_CODE (op1) == CONST_INT)
  1337.     log = exact_log2 (INTVAL (op1));
  1338.  
  1339.   /* If log is >= 0, we are dividing by 2**log, and will do it by shifting,
  1340.      which is really floor-division.  Otherwise we will really do a divide,
  1341.      and we assume that is trunc-division.
  1342.  
  1343.      We must correct the dividend by adding or subtracting something
  1344.      based on the divisor, in order to do the kind of rounding specified
  1345.      by CODE.  The correction depends on what kind of rounding is actually
  1346.      available, and that depends on whether we will shift or divide.  */
  1347.  
  1348.   switch (code)
  1349.     {
  1350.     case TRUNC_MOD_EXPR:
  1351.     case TRUNC_DIV_EXPR:
  1352.       if (log >= 0 && ! unsignedp)
  1353.     {
  1354.       rtx label = gen_label_rtx ();
  1355.       if (! can_clobber_op0)
  1356.         adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  1357.       emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0, 0);
  1358.       emit_jump_insn (gen_bge (label));
  1359.       expand_inc (adjusted_op0, plus_constant (op1, -1));
  1360.       emit_label (label);
  1361.       mod_insn_no_good = 1;
  1362.     }
  1363.       break;
  1364.  
  1365.     case FLOOR_DIV_EXPR:
  1366.     case FLOOR_MOD_EXPR:
  1367.       if (log < 0 && ! unsignedp)
  1368.     {
  1369.       rtx label = gen_label_rtx ();
  1370.       if (! can_clobber_op0)
  1371.         adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  1372.       emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0, 0);
  1373.       emit_jump_insn (gen_bge (label));
  1374.       expand_dec (adjusted_op0, op1);
  1375.       expand_inc (adjusted_op0, const1_rtx);
  1376.       emit_label (label);
  1377.       mod_insn_no_good = 1;
  1378.     }
  1379.       break;
  1380.  
  1381.     case CEIL_DIV_EXPR:
  1382.     case CEIL_MOD_EXPR:
  1383.       if (! can_clobber_op0)
  1384.     adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  1385.       if (log < 0)
  1386.     {
  1387.       rtx label = 0;
  1388.       if (! unsignedp)
  1389.         {
  1390.           label = gen_label_rtx ();
  1391.           emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0, 0);
  1392.           emit_jump_insn (gen_ble (label));
  1393.         }
  1394.       expand_inc (adjusted_op0, op1);
  1395.       expand_dec (adjusted_op0, const1_rtx);
  1396.       if (! unsignedp)
  1397.         emit_label (label);
  1398.     }
  1399.       else
  1400.     {
  1401.       adjusted_op0 = expand_binop (GET_MODE (target), add_optab,
  1402.                        adjusted_op0, plus_constant (op1, -1),
  1403.                        0, 0, OPTAB_LIB_WIDEN);
  1404.     }
  1405.       mod_insn_no_good = 1;
  1406.       break;
  1407.  
  1408.     case ROUND_DIV_EXPR:
  1409.     case ROUND_MOD_EXPR:
  1410.       if (! can_clobber_op0)
  1411.     adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target);
  1412.       if (log < 0)
  1413.     {
  1414.       op1 = expand_shift (RSHIFT_EXPR, mode, op1, integer_one_node, 0, 0);
  1415.       if (! unsignedp)
  1416.         {
  1417.           rtx label = gen_label_rtx ();
  1418.           emit_cmp_insn (adjusted_op0, const0_rtx, 0, 0, 0);
  1419.           emit_jump_insn (gen_bge (label));
  1420.           expand_unop (mode, neg_optab, op1, op1, 0);
  1421.           emit_label (label);
  1422.         }
  1423.       expand_inc (adjusted_op0, op1);
  1424.     }
  1425.       else
  1426.     {
  1427.       op1 = gen_rtx (CONST_INT, VOIDmode, INTVAL (op1) / 2);
  1428.       expand_inc (adjusted_op0, op1);
  1429.     }
  1430.       mod_insn_no_good = 1;
  1431.       break;
  1432.     }
  1433.  
  1434.   if (rem_flag && !mod_insn_no_good)
  1435.     {
  1436.       /* Try to produce the remainder directly */
  1437.       if (log >= 0)
  1438.     {
  1439.       return expand_bit_and (mode, adjusted_op0,
  1440.                  gen_rtx (CONST_INT, VOIDmode,
  1441.                       INTVAL (op1) - 1),
  1442.                  target);
  1443.     }
  1444.       else
  1445.     {
  1446.       /* See if we can do remainder without a library call.  */
  1447.       temp = sign_expand_binop (mode, umod_optab, smod_optab,
  1448.                     adjusted_op0, op1, target,
  1449.                     unsignedp, OPTAB_WIDEN);
  1450.       if (temp != 0)
  1451.         return temp;
  1452.       /* No luck there.
  1453.          Can we do remainder and divide at once without a library call?  */
  1454.       temp = gen_reg_rtx (mode);
  1455.       if (expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
  1456.                    adjusted_op0, op1,
  1457.                    0, temp, unsignedp))
  1458.         return temp;
  1459.       temp = 0;
  1460.     }
  1461.     }
  1462.  
  1463.   /* Produce the quotient.  */
  1464.   if (log >= 0)
  1465.     temp = expand_shift (RSHIFT_EXPR, mode, adjusted_op0,
  1466.              build_int_2 (exact_log2 (INTVAL (op1)), 0),
  1467.              target, unsignedp);
  1468.   else if (rem_flag && !mod_insn_no_good)
  1469.     /* If producing quotient in order to subtract for remainder,
  1470.        and a remainder subroutine would be ok,
  1471.        don't use a divide subroutine.  */
  1472.     temp = sign_expand_binop (mode, udiv_optab, sdiv_optab,
  1473.                   adjusted_op0, op1, target,
  1474.                   unsignedp, OPTAB_WIDEN);
  1475.   else
  1476.     temp = sign_expand_binop (mode, udiv_optab, sdiv_optab,
  1477.                   adjusted_op0, op1, target,
  1478.                   unsignedp, OPTAB_LIB_WIDEN);
  1479.  
  1480.   /* If we really want the remainder, get it by subtraction.  */
  1481.   if (rem_flag)
  1482.     {
  1483.       if (temp == 0)
  1484.     {
  1485.       /* No divide instruction either.  Use library for remainder.  */
  1486.       temp = sign_expand_binop (mode, umod_optab, smod_optab,
  1487.                     op0, op1, target,
  1488.                     unsignedp, OPTAB_LIB_WIDEN);
  1489.     }
  1490.       else
  1491.     {
  1492.       /* We divided.  Now finish doing X - Y * (X / Y).  */
  1493.       temp = expand_mult (mode, temp, op1, temp, unsignedp);
  1494.       if (! temp) abort ();
  1495.       temp = expand_binop (mode, sub_optab, op0,
  1496.                    temp, target, unsignedp, OPTAB_LIB_WIDEN);
  1497.     }
  1498.     }
  1499.  
  1500.   if (temp == 0)
  1501.     abort ();
  1502.   return temp;
  1503. }
  1504.