home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Source / GNU / cc / cc / expmed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-04  |  92.3 KB  |  3,005 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, 1992 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 2, 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 "real.h"
  31. #include "recog.h"
  32.  
  33. static rtx extract_split_bit_field ();
  34. static rtx extract_fixed_bit_field ();
  35. static void store_split_bit_field ();
  36. static void store_fixed_bit_field ();
  37. static rtx mask_rtx ();
  38. static rtx lshift_value ();
  39.  
  40. #define CEIL(x,y) (((x) + (y) - 1) / (y))
  41.  
  42. /* Non-zero means multiply instructions are cheaper than shifts.  */
  43. int mult_is_very_cheap;
  44.  
  45. /* Non-zero means divides or modulus operations are relatively cheap for
  46.    powers of two, so don't use branches; emit the operation instead. 
  47.    Usually, this will mean that the MD file will emit non-branch
  48.    sequences.  */
  49.  
  50. static int sdiv_pow2_cheap, smod_pow2_cheap;
  51.  
  52. /* Cost of various pieces of RTL.  */
  53. static int add_cost, shift_cost, mult_cost, negate_cost, lea_cost;
  54.  
  55. /* Max scale factor for scaled address in lea instruction.  */
  56. static int lea_max_mul;
  57.  
  58. void
  59. init_expmed ()
  60. {
  61.   char *free_point = (char *) oballoc (1);
  62.   /* This is "some random pseudo register" for purposes of calling recog
  63.      to see what insns exist.  */
  64.   rtx reg = gen_rtx (REG, word_mode, FIRST_PSEUDO_REGISTER);
  65.   rtx pow2 = gen_rtx (CONST_INT, VOIDmode, 32);
  66.   rtx lea;
  67.   int i, dummy;
  68.  
  69.   add_cost = rtx_cost (gen_rtx (PLUS, word_mode, reg, reg), SET);
  70.   shift_cost = rtx_cost (gen_rtx (LSHIFT, word_mode, reg,
  71.                   /* Using a constant gives better
  72.                      estimate of typical costs.
  73.                      1 or 2 might have quirks.  */
  74.                   gen_rtx (CONST_INT, VOIDmode, 3)), SET);
  75.   mult_cost = rtx_cost (gen_rtx (MULT, word_mode, reg, reg), SET);
  76.   negate_cost = rtx_cost (gen_rtx (NEG, word_mode, reg), SET);
  77.  
  78.   /* 999999 is chosen to avoid any plausible faster special case.  */
  79.   mult_is_very_cheap
  80.     = (rtx_cost (gen_rtx (MULT, word_mode, reg,
  81.               gen_rtx (CONST_INT, VOIDmode, 999999)), SET)
  82.        < rtx_cost (gen_rtx (LSHIFT, word_mode, reg,
  83.                 gen_rtx (CONST_INT, VOIDmode, 7)), SET));
  84.  
  85.   sdiv_pow2_cheap
  86.     = rtx_cost (gen_rtx (DIV, word_mode, reg, pow2), SET) <= 2 * add_cost;
  87.   smod_pow2_cheap
  88.     = rtx_cost (gen_rtx (MOD, word_mode, reg, pow2), SET) <= 2 * add_cost;
  89.  
  90.   init_recog ();
  91.   for (i = 2;; i <<= 1)
  92.     {
  93.       lea = gen_rtx (SET, VOIDmode, reg,
  94.              gen_rtx (PLUS, word_mode,
  95.                   gen_rtx (MULT, word_mode, reg,
  96.                        gen_rtx (CONST_INT, VOIDmode, i)),
  97.                   reg));
  98.       /* Using 0 as second argument is not quite right,
  99.      but what else is there to do?  */
  100.       if (recog (lea, 0, &dummy) < 0)
  101.     break;
  102.       lea_max_mul = i;
  103.       lea_cost = rtx_cost (SET_SRC (lea), SET);
  104.     }
  105.  
  106.   /* Free the objects we just allocated.  */
  107.   obfree (free_point);
  108. }
  109.  
  110. /* Return an rtx representing minus the value of X.
  111.    MODE is the intended mode of the result,
  112.    useful if X is a CONST_INT.  */
  113.  
  114. rtx
  115. negate_rtx (mode, x)
  116.      enum machine_mode mode;
  117.      rtx x;
  118. {
  119.   if (GET_CODE (x) == CONST_INT)
  120.     {
  121.       int val = - INTVAL (x);
  122.       if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_INT)
  123.     {
  124.       /* Sign extend the value from the bits that are significant.  */
  125.       if (val & (1 << (GET_MODE_BITSIZE (mode) - 1)))
  126.         val |= (-1) << GET_MODE_BITSIZE (mode);
  127.       else
  128.         val &= (1 << GET_MODE_BITSIZE (mode)) - 1;
  129.     }
  130.       return gen_rtx (CONST_INT, VOIDmode, val);
  131.     }
  132.   else
  133.     return expand_unop (GET_MODE (x), neg_optab, x, 0, 0);
  134. }
  135.  
  136. /* Generate code to store value from rtx VALUE
  137.    into a bit-field within structure STR_RTX
  138.    containing BITSIZE bits starting at bit BITNUM.
  139.    FIELDMODE is the machine-mode of the FIELD_DECL node for this field.
  140.    ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
  141.    TOTAL_SIZE is the size of the structure in bytes, or -1 if varying.  */
  142.  
  143. /* ??? Note that there are two different ideas here for how
  144.    to determine the size to count bits within, for a register.
  145.    One is BITS_PER_WORD, and the other is the size of operand 3
  146.    of the insv pattern.  (The latter assumes that an n-bit machine
  147.    will be able to insert bit fields up to n bits wide.)
  148.    It isn't certain that either of these is right.
  149.    extract_bit_field has the same quandary.  */
  150.  
  151. rtx
  152. store_bit_field (str_rtx, bitsize, bitnum, fieldmode, value, align, total_size)
  153.      rtx str_rtx;
  154.      register int bitsize;
  155.      int bitnum;
  156.      enum machine_mode fieldmode;
  157.      rtx value;
  158.      int align;
  159.      int total_size;
  160. {
  161.   int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
  162.   register int offset = bitnum / unit;
  163.   register int bitpos = bitnum % unit;
  164.   register rtx op0 = str_rtx;
  165.  
  166.   if (GET_CODE (str_rtx) == MEM && ! MEM_IN_STRUCT_P (str_rtx))
  167.     abort ();
  168.  
  169.   /* Discount the part of the structure before the desired byte.
  170.      We need to know how many bytes are safe to reference after it.  */
  171.   if (total_size >= 0)
  172.     total_size -= (bitpos / BIGGEST_ALIGNMENT
  173.            * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
  174.  
  175.   while (GET_CODE (op0) == SUBREG)
  176.     {
  177.       /* The following line once was done only if WORDS_BIG_ENDIAN,
  178.      but I think that is a mistake.  WORDS_BIG_ENDIAN is
  179.      meaningful at a much higher level; when structures are copied
  180.      between memory and regs, the higher-numbered regs
  181.      always get higher addresses.  */
  182.       offset += SUBREG_WORD (op0);
  183.       /* We used to adjust BITPOS here, but now we do the whole adjustment
  184.      right after the loop.  */
  185.       op0 = SUBREG_REG (op0);
  186.     }
  187.  
  188. #if BYTES_BIG_ENDIAN
  189.   /* If OP0 is a register, BITPOS must count within a word.
  190.      But as we have it, it counts within whatever size OP0 now has.
  191.      On a bigendian machine, these are not the same, so convert.  */
  192.   if (GET_CODE (op0) != MEM && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
  193.     bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
  194. #endif
  195.  
  196.   value = protect_from_queue (value, 0);
  197.  
  198.   if (flag_force_mem)
  199.     value = force_not_mem (value);
  200.  
  201.   /* Note that the adjustment of BITPOS above has no effect on whether
  202.      BITPOS is 0 in a REG bigger than a word.  */
  203.   if (GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD
  204.       && (! STRICT_ALIGNMENT || GET_CODE (op0) != MEM)
  205.       && bitpos == 0 && bitsize == GET_MODE_BITSIZE (fieldmode))
  206.     {
  207.       /* Storing in a full-word or multi-word field in a register
  208.      can be done with just SUBREG.  */
  209.       if (GET_MODE (op0) != fieldmode)
  210.     if (GET_CODE (op0) == REG)
  211.       op0 = gen_rtx (SUBREG, fieldmode, op0, offset);
  212.     else
  213.       op0 = change_address (op0, fieldmode,
  214.                 plus_constant (XEXP (op0, 0), offset));
  215.       emit_move_insn (op0, value);
  216.       return value;
  217.     }
  218.  
  219.   /* Storing an lsb-aligned field in a register
  220.      can be done with a movestrict instruction.  */
  221.  
  222.   if (GET_CODE (op0) != MEM
  223. #if BYTES_BIG_ENDIAN
  224.       && bitpos + bitsize == unit
  225. #else
  226.       && bitpos == 0
  227. #endif
  228.       && bitsize == GET_MODE_BITSIZE (fieldmode)
  229.       && (GET_MODE (op0) == fieldmode
  230.       || (movstrict_optab->handlers[(int) fieldmode].insn_code
  231.           != CODE_FOR_nothing)))
  232.     {
  233.       /* Get appropriate low part of the value being stored.  */
  234.       if (GET_CODE (value) == CONST_INT || GET_CODE (value) == REG)
  235.     value = gen_lowpart (fieldmode, value);
  236.       else if (!(GET_CODE (value) == SYMBOL_REF
  237.          || GET_CODE (value) == LABEL_REF
  238.          || GET_CODE (value) == CONST))
  239.     value = convert_to_mode (fieldmode, value, 0);
  240.  
  241.       if (GET_MODE (op0) == fieldmode)
  242.     emit_move_insn (op0, value);
  243.       else
  244.     {
  245.       int icode = movstrict_optab->handlers[(int) fieldmode].insn_code;
  246.       if(! (*insn_operand_predicate[icode][1]) (value, fieldmode))
  247.         value = copy_to_mode_reg (fieldmode, value);
  248.       emit_insn (GEN_FCN (icode)
  249.            (gen_rtx (SUBREG, fieldmode, op0, offset), value));
  250.     }
  251.       return value;
  252.     }
  253.  
  254.   /* Handle fields bigger than a word.  */
  255.  
  256.   if (bitsize > BITS_PER_WORD)
  257.     {
  258.       /* Here we transfer the words of the field
  259.      in the order least significant first.
  260.      This is because the most significant word is the one which may
  261.      be less than full.  */
  262.  
  263.       int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
  264.       int i;
  265.  
  266.       /* This is the mode we must force value to, so that there will be enough
  267.      subwords to extract.  Note that fieldmode will often (always?) be
  268.      VOIDmode, because that is what store_field uses to indicate that this
  269.      is a bit field, but passing VOIDmode to operand_subword_force will
  270.      result in an abort.  */
  271.       fieldmode = mode_for_size (nwords * BITS_PER_WORD, MODE_INT, 0);
  272.  
  273.       for (i = 0; i < nwords; i++)
  274.     {
  275.       /* If I is 0, use the low-order word in both field and target;
  276.          if I is 1, use the next to lowest word; and so on.  */
  277.       int wordnum = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
  278.       int bit_offset = (WORDS_BIG_ENDIAN
  279.                 ? MAX (bitsize - (i + 1) * BITS_PER_WORD, 0)
  280.                 : i * BITS_PER_WORD);
  281.       store_bit_field (op0, MIN (BITS_PER_WORD,
  282.                      bitsize - i * BITS_PER_WORD),
  283.                bitnum + bit_offset, word_mode,
  284.                operand_subword_force (value, wordnum, fieldmode),
  285.                align, total_size);
  286.     }
  287.       return value;
  288.     }
  289.  
  290.   /* From here on we can assume that the field to be stored in is
  291.      a full-word (whatever type that is), since it is shorter than a word.  */
  292.  
  293.   /* OFFSET is the number of words or bytes (UNIT says which)
  294.      from STR_RTX to the first word or byte containing part of the field.  */
  295.  
  296.   if (GET_CODE (op0) == REG)
  297.     {
  298.       if (offset != 0
  299.       || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
  300.     op0 = gen_rtx (SUBREG, TYPE_MODE (type_for_size (BITS_PER_WORD, 0)),
  301.                op0, offset);
  302.       offset = 0;
  303.     }
  304.   else
  305.     {
  306.       op0 = protect_from_queue (op0, 1);
  307.     }
  308.  
  309.   /* Now OFFSET is nonzero only if OP0 is memory
  310.      and is therefore always measured in bytes.  */
  311.  
  312. #ifdef HAVE_insv
  313.   if (HAVE_insv
  314.       && !(bitsize == 1 && GET_CODE (value) == CONST_INT)
  315.       /* Ensure insv's size is wide enough for this field.  */
  316.       && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_insv][3])
  317.       >= bitsize))
  318.     {
  319.       int xbitpos = bitpos;
  320.       rtx value1;
  321.       rtx xop0 = op0;
  322.       rtx last = get_last_insn ();
  323.       rtx pat;
  324.       enum machine_mode maxmode
  325.     = insn_operand_mode[(int) CODE_FOR_insv][3];
  326.  
  327.       int save_volatile_ok = volatile_ok;
  328.       volatile_ok = 1;
  329.  
  330.       /* If this machine's insv can only insert into a register, or if we
  331.      are to force MEMs into a register, copy OP0 into a register and
  332.      save it back later.  */
  333.       if (GET_CODE (op0) == MEM
  334.       && (flag_force_mem
  335.           || ! ((*insn_operand_predicate[(int) CODE_FOR_insv][0])
  336.             (op0, VOIDmode))))
  337.     {
  338.       rtx tempreg;
  339.       enum machine_mode bestmode;
  340.  
  341.       /* Get the mode to use for inserting into this field.  If OP0 is
  342.          BLKmode, get the smallest mode consistent with the alignment. If
  343.          OP0 is a non-BLKmode object that is no wider than MAXMODE, use its
  344.          mode. Otherwise, use the smallest mode containing the field.  */
  345.  
  346.       if (GET_MODE (op0) == BLKmode
  347.           || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (maxmode))
  348.         bestmode
  349.           = get_best_mode (bitsize, bitnum, align * BITS_PER_UNIT, maxmode,
  350.                    MEM_VOLATILE_P (op0));
  351.       else
  352.         bestmode = GET_MODE (op0);
  353.  
  354.       if (bestmode == VOIDmode)
  355.         goto insv_loses;
  356.  
  357.       /* Adjust address to point to the containing unit of that mode.  */
  358.       unit = GET_MODE_BITSIZE (bestmode);
  359.       /* Compute offset as multiple of this unit, counting in bytes.  */
  360.       offset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
  361.       bitpos = bitnum % unit;
  362.       op0 = change_address (op0, bestmode, 
  363.                 plus_constant (XEXP (op0, 0), offset));
  364.  
  365.       /* Fetch that unit, store the bitfield in it, then store the unit.  */
  366.       tempreg = copy_to_reg (op0);
  367.       store_bit_field (tempreg, bitsize, bitpos, fieldmode, value,
  368.                align, total_size);
  369.       emit_move_insn (op0, tempreg);
  370.       return value;
  371.     }
  372.       volatile_ok = save_volatile_ok;
  373.  
  374.       /* Add OFFSET into OP0's address.  */
  375.       if (GET_CODE (xop0) == MEM)
  376.     xop0 = change_address (xop0, byte_mode,
  377.                    plus_constant (XEXP (xop0, 0), offset));
  378.  
  379.       /* If xop0 is a register, we need it in MAXMODE
  380.      to make it acceptable to the format of insv.  */
  381.       if (GET_CODE (xop0) == SUBREG)
  382.     PUT_MODE (xop0, maxmode);
  383.       if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
  384.     xop0 = gen_rtx (SUBREG, maxmode, xop0, 0);
  385.  
  386.       /* On big-endian machines, we count bits from the most significant.
  387.      If the bit field insn does not, we must invert.  */
  388.  
  389. #if BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN
  390.       xbitpos = unit - bitsize - xbitpos;
  391. #endif
  392.       /* We have been counting XBITPOS within UNIT.
  393.      Count instead within the size of the register.  */
  394. #if BITS_BIG_ENDIAN
  395.       if (GET_CODE (xop0) != MEM)
  396.     xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
  397. #endif
  398.       unit = GET_MODE_BITSIZE (maxmode);
  399.  
  400.       /* Convert VALUE to maxmode (which insv insn wants) in VALUE1.  */
  401.       value1 = value;
  402.       if (GET_MODE (value) != maxmode)
  403.     {
  404.       if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
  405.           {
  406.             /* Optimization: Don't bother really extending VALUE
  407.           if it has all the bits we will actually use.  However,
  408.           if we must narrow it, be sure we do it correctly.  */
  409.   
  410.            if (GET_MODE_SIZE (GET_MODE (value)) < GET_MODE_SIZE (maxmode))
  411.          {
  412.            /* Avoid making subreg of a subreg, or of a mem.  */
  413.            if (GET_CODE (value1) != REG)
  414.           value1 = copy_to_reg (value1);
  415.            value1 = gen_rtx (SUBREG, maxmode, value1, 0);
  416.          }
  417.            else
  418.          value1 = gen_lowpart (maxmode, value1);
  419.           }
  420.       else if (!CONSTANT_P (value))
  421.         /* Parse phase is supposed to make VALUE's data type
  422.            match that of the component reference, which is a type
  423.            at least as wide as the field; so VALUE should have
  424.            a mode that corresponds to that type.  */
  425.         abort ();
  426.     }
  427.  
  428.       /* If this machine's insv insists on a register,
  429.      get VALUE1 into a register.  */
  430.       if (! ((*insn_operand_predicate[(int) CODE_FOR_insv][3])
  431.          (value1, maxmode)))
  432.     value1 = force_reg (maxmode, value1);
  433.  
  434.       pat = gen_insv (xop0,
  435.               gen_rtx (CONST_INT, VOIDmode, bitsize),
  436.               gen_rtx (CONST_INT, VOIDmode, xbitpos),
  437.               value1);
  438.       if (pat)
  439.     emit_insn (pat);
  440.       else
  441.         {
  442.       delete_insns_since (last);
  443.       store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
  444.     }
  445.     }
  446.   else
  447.     insv_loses:
  448. #endif
  449.     /* Insv is not available; store using shifts and boolean ops.  */
  450.     store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
  451.   return value;
  452. }
  453.  
  454. /* Use shifts and boolean operations to store VALUE
  455.    into a bit field of width BITSIZE
  456.    in a memory location specified by OP0 except offset by OFFSET bytes.
  457.      (OFFSET must be 0 if OP0 is a register.)
  458.    The field starts at position BITPOS within the byte.
  459.     (If OP0 is a register, it may be a full word or a narrower mode,
  460.      but BITPOS still counts within a full word,
  461.      which is significant on bigendian machines.)
  462.    STRUCT_ALIGN is the alignment the structure is known to have (in bytes).
  463.  
  464.    Note that protect_from_queue has already been done on OP0 and VALUE.  */
  465.  
  466. static void
  467. store_fixed_bit_field (op0, offset, bitsize, bitpos, value, struct_align)
  468.      register rtx op0;
  469.      register int offset, bitsize, bitpos;
  470.      register rtx value;
  471.      int struct_align;
  472. {
  473.   register enum machine_mode mode;
  474.   int total_bits = BITS_PER_WORD;
  475.   rtx subtarget, temp;
  476.   int all_zero = 0;
  477.   int all_one = 0;
  478.  
  479.   /* Add OFFSET to OP0's address (if it is in memory)
  480.      and if a single byte contains the whole bit field
  481.      change OP0 to a byte.  */
  482.  
  483.   /* There is a case not handled here:
  484.      a structure with a known alignment of just a halfword
  485.      and a field split across two aligned halfwords within the structure.
  486.      Or likewise a structure with a known alignment of just a byte
  487.      and a field split across two bytes.
  488.      Such cases are not supposed to be able to occur.  */
  489.  
  490.   if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
  491.     {
  492.       if (offset != 0)
  493.     abort ();
  494.       /* Special treatment for a bit field split across two registers.  */
  495.       if (bitsize + bitpos > BITS_PER_WORD)
  496.     {
  497.       store_split_bit_field (op0, bitsize, bitpos, value, BITS_PER_WORD);
  498.       return;
  499.     }
  500.     }
  501.   else
  502.     {
  503.       /* Get the proper mode to use for this field.  We want a mode that
  504.      includes the entire field.  If such a mode would be larger than
  505.      a word, we won't be doing the extraction the normal way.  */
  506.  
  507.       mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
  508.                 struct_align * BITS_PER_UNIT, word_mode,
  509.                 GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0));
  510.  
  511.       if (mode == VOIDmode)
  512.     {
  513.       /* The only way this should occur is if the field spans word
  514.          boundaries.  */
  515.       store_split_bit_field (op0, bitsize, bitpos + offset * BITS_PER_UNIT,
  516.                  value, struct_align);
  517.       return;
  518.     }
  519.  
  520.       total_bits = GET_MODE_BITSIZE (mode);
  521.  
  522.       /* Get ref to an aligned byte, halfword, or word containing the field.
  523.      Adjust BITPOS to be position within a word,
  524.      and OFFSET to be the offset of that word.
  525.      Then alter OP0 to refer to that word.  */
  526.       bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
  527.       offset -= (offset % (total_bits / BITS_PER_UNIT));
  528.       op0 = change_address (op0, mode,
  529.                 plus_constant (XEXP (op0, 0), offset));
  530.     }
  531.  
  532.   mode = GET_MODE (op0);
  533.  
  534.   /* Now MODE is either some integral mode for a MEM as OP0,
  535.      or is a full-word for a REG as OP0.  TOTAL_BITS corresponds.
  536.      The bit field is contained entirely within OP0.
  537.      BITPOS is the starting bit number within OP0.
  538.      (OP0's mode may actually be narrower than MODE.)  */
  539.  
  540. #if BYTES_BIG_ENDIAN
  541.   /* BITPOS is the distance between our msb
  542.      and that of the containing datum.
  543.      Convert it to the distance from the lsb.  */
  544.  
  545.   bitpos = total_bits - bitsize - bitpos;
  546. #endif
  547.   /* Now BITPOS is always the distance between our lsb
  548.      and that of OP0.  */
  549.  
  550.   /* Shift VALUE left by BITPOS bits.  If VALUE is not constant,
  551.      we must first convert its mode to MODE.  */
  552.  
  553.   if (GET_CODE (value) == CONST_INT)
  554.     {
  555.       register int v = INTVAL (value);
  556.  
  557.       if (bitsize < HOST_BITS_PER_INT)
  558.     v &= (1 << bitsize) - 1;
  559.  
  560.       if (v == 0)
  561.     all_zero = 1;
  562.       else if ((bitsize < HOST_BITS_PER_INT && v == (1 << bitsize) - 1)
  563.            || (bitsize == HOST_BITS_PER_INT && v == -1))
  564.     all_one = 1;
  565.  
  566.       value = lshift_value (mode, value, bitpos, bitsize);
  567.     }
  568.   else
  569.     {
  570.       int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize
  571.               && bitpos + bitsize != GET_MODE_BITSIZE (mode));
  572.  
  573.       if (GET_MODE (value) != mode)
  574.     {
  575.       /* If VALUE is a floating-point mode, access it as an integer
  576.          of the corresponding size, then convert it.  This can occur on
  577.          a machine with 64 bit registers that uses SFmode for float.  */
  578.       if (GET_MODE_CLASS (GET_MODE (value)) == MODE_FLOAT)
  579.         {
  580.           if (GET_CODE (value) != REG)
  581.         value = copy_to_reg (value);
  582.           value
  583.         = gen_rtx (SUBREG, word_mode, value, 0);
  584.         }
  585.  
  586.       if ((GET_CODE (value) == REG || GET_CODE (value) == SUBREG)
  587.           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (value)))
  588.         value = gen_lowpart (mode, value);
  589.       else
  590.         value = convert_to_mode (mode, value, 1);
  591.     }
  592.  
  593.       if (must_and)
  594.     value = expand_binop (mode, and_optab, value,
  595.                   mask_rtx (mode, 0, bitsize, 0),
  596.                   0, 1, OPTAB_LIB_WIDEN);
  597.       if (bitpos > 0)
  598.     value = expand_shift (LSHIFT_EXPR, mode, value,
  599.                   build_int_2 (bitpos, 0), 0, 1);
  600.     }
  601.  
  602.   /* Now clear the chosen bits in OP0,
  603.      except that if VALUE is -1 we need not bother.  */
  604.  
  605.   subtarget = (GET_CODE (op0) == REG || ! flag_force_mem) ? op0 : 0;
  606.  
  607.   if (! all_one)
  608.     {
  609.       temp = expand_binop (mode, and_optab, op0,
  610.                mask_rtx (mode, bitpos, bitsize, 1),
  611.                subtarget, 1, OPTAB_LIB_WIDEN);
  612.       subtarget = temp;
  613.     }
  614.   else
  615.     temp = op0;
  616.  
  617.   /* Now logical-or VALUE into OP0, unless it is zero.  */
  618.  
  619.   if (! all_zero)
  620.     temp = expand_binop (mode, ior_optab, temp, value,
  621.              subtarget, 1, OPTAB_LIB_WIDEN);
  622.   if (op0 != temp)
  623.     emit_move_insn (op0, temp);
  624. }
  625.  
  626. /* Store a bit field that is split across two words.
  627.  
  628.    OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
  629.    BITSIZE is the field width; BITPOS the position of its first bit
  630.    (within the word).
  631.    VALUE is the value to store.  */
  632.  
  633. static void
  634. store_split_bit_field (op0, bitsize, bitpos, value, align)
  635.      rtx op0;
  636.      int bitsize, bitpos;
  637.      rtx value;
  638.      int align;
  639. {
  640.   /* BITSIZE_1 is size of the part in the first word.  */
  641.   int bitsize_1 = BITS_PER_WORD - bitpos % BITS_PER_WORD;
  642.   /* BITSIZE_2 is size of the rest (in the following word).  */
  643.   int bitsize_2 = bitsize - bitsize_1;
  644.   rtx part1, part2;
  645.   int unit = GET_CODE (op0) == MEM ? BITS_PER_UNIT : BITS_PER_WORD;
  646.   int offset = bitpos / unit;
  647.   rtx word;
  648.  
  649.   /* The field must span exactly one word boundary.  */
  650.   if (bitpos / BITS_PER_WORD != (bitpos + bitsize - 1) / BITS_PER_WORD - 1)
  651.     abort ();
  652.  
  653.   if (GET_MODE (value) != VOIDmode)
  654.     value = convert_to_mode (word_mode, value, 1);
  655.   if (CONSTANT_P (value) && GET_CODE (value) != CONST_INT)
  656.     value = copy_to_reg (value);
  657.  
  658.   /* Split the value into two parts:
  659.      PART1 gets that which goes in the first word; PART2 the other.  */
  660. #if BYTES_BIG_ENDIAN
  661.   /* PART1 gets the more significant part.  */
  662.   if (GET_CODE (value) == CONST_INT)
  663.     {
  664.       part1 = gen_rtx (CONST_INT, VOIDmode,
  665.                (unsigned) (INTVAL (value)) >> bitsize_2);
  666.       part2 = gen_rtx (CONST_INT, VOIDmode,
  667.                (unsigned) (INTVAL (value)) & ((1 << bitsize_2) - 1));
  668.     }
  669.   else
  670.     {
  671.       part1 = extract_fixed_bit_field (word_mode, value, 0, bitsize_1,
  672.                        BITS_PER_WORD - bitsize, 0, 1,
  673.                        BITS_PER_WORD);
  674.       part2 = extract_fixed_bit_field (word_mode, value, 0, bitsize_2,
  675.                        BITS_PER_WORD - bitsize_2, 0, 1,
  676.                        BITS_PER_WORD);
  677.     }
  678. #else
  679.   /* PART1 gets the less significant part.  */
  680.   if (GET_CODE (value) == CONST_INT)
  681.     {
  682.       part1 = gen_rtx (CONST_INT, VOIDmode,
  683.                (unsigned) (INTVAL (value)) & ((1 << bitsize_1) - 1));
  684.       part2 = gen_rtx (CONST_INT, VOIDmode,
  685.                (unsigned) (INTVAL (value)) >> bitsize_1);
  686.     }
  687.   else
  688.     {
  689.       part1 = extract_fixed_bit_field (word_mode, value, 0, bitsize_1, 0,
  690.                        0, 1, BITS_PER_WORD);
  691.       part2 = extract_fixed_bit_field (word_mode, value, 0, bitsize_2,
  692.                        bitsize_1, 0, 1, BITS_PER_WORD);
  693.     }
  694. #endif
  695.  
  696.   /* Store PART1 into the first word.  If OP0 is a MEM, pass OP0 and the
  697.      offset computed above.  Otherwise, get the proper word and pass an
  698.      offset of zero.  */
  699.   word = (GET_CODE (op0) == MEM ? op0
  700.       : operand_subword (op0, offset, 1, GET_MODE (op0)));
  701.   if (word == 0)
  702.     abort ();
  703.  
  704.   store_fixed_bit_field (word, GET_CODE (op0) == MEM ? offset : 0,
  705.              bitsize_1, bitpos % unit, part1, align);
  706.  
  707.   /* Offset op0 by 1 word to get to the following one.  */
  708.   if (GET_CODE (op0) == SUBREG)
  709.     word = operand_subword (SUBREG_REG (op0), SUBREG_WORD (op0) + offset + 1,
  710.                 1, VOIDmode);
  711.   else if (GET_CODE (op0) == MEM)
  712.     word = op0;
  713.   else
  714.     word = operand_subword (op0, offset + 1, 1, GET_MODE (op0));
  715.  
  716.   if (word == 0)
  717.     abort ();
  718.  
  719.   /* Store PART2 into the second word.  */
  720.   store_fixed_bit_field (word,
  721.              (GET_CODE (op0) == MEM
  722.               ? CEIL (offset + 1, UNITS_PER_WORD) * UNITS_PER_WORD
  723.               : 0),
  724.              bitsize_2, 0, part2, align);
  725. }
  726.  
  727. /* Generate code to extract a byte-field from STR_RTX
  728.    containing BITSIZE bits, starting at BITNUM,
  729.    and put it in TARGET if possible (if TARGET is nonzero).
  730.    Regardless of TARGET, we return the rtx for where the value is placed.
  731.    It may be a QUEUED.
  732.  
  733.    STR_RTX is the structure containing the byte (a REG or MEM).
  734.    UNSIGNEDP is nonzero if this is an unsigned bit field.
  735.    MODE is the natural mode of the field value once extracted.
  736.    TMODE is the mode the caller would like the value to have;
  737.    but the value may be returned with type MODE instead.
  738.  
  739.    ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
  740.    TOTAL_SIZE is the size in bytes of the containing structure,
  741.    or -1 if varying.
  742.  
  743.    If a TARGET is specified and we can store in it at no extra cost,
  744.    we do so, and return TARGET.
  745.    Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
  746.    if they are equally easy.  */
  747.  
  748. rtx
  749. extract_bit_field (str_rtx, bitsize, bitnum, unsignedp,
  750.            target, mode, tmode, align, total_size)
  751.      rtx str_rtx;
  752.      register int bitsize;
  753.      int bitnum;
  754.      int unsignedp;
  755.      rtx target;
  756.      enum machine_mode mode, tmode;
  757.      int align;
  758.      int total_size;
  759. {
  760.   int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
  761.   register int offset = bitnum / unit;
  762.   register int bitpos = bitnum % unit;
  763.   register rtx op0 = str_rtx;
  764.   rtx spec_target = target;
  765.   rtx spec_target_subreg = 0;
  766.  
  767.   if (GET_CODE (str_rtx) == MEM && ! MEM_IN_STRUCT_P (str_rtx))
  768.     abort ();
  769.  
  770.   /* Discount the part of the structure before the desired byte.
  771.      We need to know how many bytes are safe to reference after it.  */
  772.   if (total_size >= 0)
  773.     total_size -= (bitpos / BIGGEST_ALIGNMENT
  774.            * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
  775.  
  776.   if (tmode == VOIDmode)
  777.     tmode = mode;
  778.  
  779.   while (GET_CODE (op0) == SUBREG)
  780.     {
  781.       offset += SUBREG_WORD (op0);
  782.       op0 = SUBREG_REG (op0);
  783.     }
  784.   
  785. #if BYTES_BIG_ENDIAN
  786.   /* If OP0 is a register, BITPOS must count within a word.
  787.      But as we have it, it counts within whatever size OP0 now has.
  788.      On a bigendian machine, these are not the same, so convert.  */
  789.   if (GET_CODE (op0) != MEM && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
  790.     bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
  791. #endif
  792.  
  793.   /* Extracting a full-word or multi-word value
  794.      from a structure in a register.
  795.      This can be done with just SUBREG.
  796.      So too extracting a subword value in
  797.      the least significant part of the register.  */
  798.  
  799.   if (GET_CODE (op0) == REG
  800.       && ((bitsize >= BITS_PER_WORD && bitsize == GET_MODE_BITSIZE (mode)
  801.        && bitpos % BITS_PER_WORD == 0)
  802.       || (mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0) != BLKmode
  803. #if BYTES_BIG_ENDIAN
  804.           && bitpos + bitsize == BITS_PER_WORD
  805. #else
  806.           && bitpos == 0
  807. #endif
  808.           )))
  809.     {
  810.       enum machine_mode mode1
  811.     = mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0);
  812.  
  813.       if (mode1 != GET_MODE (op0))
  814.     op0 = gen_rtx (SUBREG, mode1, op0, offset);
  815.  
  816.       if (mode1 != mode)
  817.     return convert_to_mode (tmode, op0, unsignedp);
  818.       return op0;
  819.     }
  820.  
  821.   /* Handle fields bigger than a word.  */
  822.   
  823.   if (bitsize > BITS_PER_WORD)
  824.     {
  825.       /* Here we transfer the words of the field
  826.      in the order least significant first.
  827.      This is because the most significant word is the one which may
  828.      be less than full.  */
  829.  
  830.       int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
  831.       int i;
  832.  
  833.       if (target == 0 || GET_CODE (target) != REG)
  834.     target = gen_reg_rtx (mode);
  835.  
  836.       for (i = 0; i < nwords; i++)
  837.     {
  838.       /* If I is 0, use the low-order word in both field and target;
  839.          if I is 1, use the next to lowest word; and so on.  */
  840.       int wordnum = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
  841.       int bit_offset = (WORDS_BIG_ENDIAN
  842.                 ? MAX (0, bitsize - (i + 1) * BITS_PER_WORD)
  843.                 : i * BITS_PER_WORD);
  844.       rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
  845.       rtx result_part
  846.         = extract_bit_field (op0, MIN (BITS_PER_WORD,
  847.                        bitsize - i * BITS_PER_WORD),
  848.                  bitnum + bit_offset,
  849.                  1, target_part, mode, word_mode,
  850.                  align, total_size);
  851.  
  852.       if (target_part == 0)
  853.         abort ();
  854.  
  855.       if (result_part != target_part)
  856.         emit_move_insn (target_part, result_part);
  857.     }
  858.  
  859.       return target;
  860.     }
  861.   
  862.   /* From here on we know the desired field is smaller than a word
  863.      so we can assume it is an integer.  So we can safely extract it as one
  864.      size of integer, if necessary, and then truncate or extend
  865.      to the size that is wanted.  */
  866.  
  867.   /* OFFSET is the number of words or bytes (UNIT says which)
  868.      from STR_RTX to the first word or byte containing part of the field.  */
  869.  
  870.   if (GET_CODE (op0) == REG)
  871.     {
  872.       if (offset != 0
  873.       || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
  874.     op0 = gen_rtx (SUBREG, TYPE_MODE (type_for_size (BITS_PER_WORD, 0)),
  875.                op0, offset);
  876.       offset = 0;
  877.     }
  878.   else
  879.     {
  880.       op0 = protect_from_queue (str_rtx, 1);
  881.     }
  882.  
  883.   /* Now OFFSET is nonzero only for memory operands.  */
  884.  
  885.   if (unsignedp)
  886.     {
  887. #ifdef HAVE_extzv
  888.       if (HAVE_extzv
  889.       && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_extzv][0])
  890.           >= bitsize))
  891.     {
  892.       int xbitpos = bitpos, xoffset = offset;
  893.       rtx bitsize_rtx, bitpos_rtx;
  894.       rtx last = get_last_insn();
  895.       rtx xop0 = op0;
  896.       rtx xtarget = target;
  897.       rtx xspec_target = spec_target;
  898.       rtx xspec_target_subreg = spec_target_subreg;
  899.       rtx pat;
  900.       enum machine_mode maxmode
  901.         = insn_operand_mode[(int) CODE_FOR_extzv][0];
  902.  
  903.       if (GET_CODE (xop0) == MEM)
  904.         {
  905.           int save_volatile_ok = volatile_ok;
  906.           volatile_ok = 1;
  907.  
  908.           /* Is the memory operand acceptable?  */
  909.           if (flag_force_mem
  910.           || ! ((*insn_operand_predicate[(int) CODE_FOR_extzv][1])
  911.             (xop0, GET_MODE (xop0))))
  912.         {
  913.           /* No, load into a reg and extract from there.  */
  914.           enum machine_mode bestmode;
  915.  
  916.           /* Get the mode to use for inserting into this field.  If
  917.              OP0 is BLKmode, get the smallest mode consistent with the
  918.              alignment. If OP0 is a non-BLKmode object that is no
  919.              wider than MAXMODE, use its mode. Otherwise, use the
  920.              smallest mode containing the field.  */
  921.  
  922.           if (GET_MODE (xop0) == BLKmode
  923.               || (GET_MODE_SIZE (GET_MODE (op0))
  924.               > GET_MODE_SIZE (maxmode)))
  925.             bestmode = get_best_mode (bitsize, bitnum,
  926.                           align * BITS_PER_UNIT, maxmode,
  927.                           MEM_VOLATILE_P (xop0));
  928.           else
  929.             bestmode = GET_MODE (xop0);
  930.  
  931.           if (bestmode == VOIDmode)
  932.             goto extzv_loses;
  933.  
  934.           /* Compute offset as multiple of this unit,
  935.              counting in bytes.  */
  936.           unit = GET_MODE_BITSIZE (bestmode);
  937.           xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
  938.           xbitpos = bitnum % unit;
  939.           xop0 = change_address (xop0, bestmode,
  940.                      plus_constant (XEXP (xop0, 0),
  941.                             xoffset));
  942.           /* Fetch it to a register in that size.  */
  943.           xop0 = force_reg (bestmode, xop0);
  944.  
  945.           /* XBITPOS counts within UNIT, which is what is expected.  */
  946.         }
  947.           else
  948.         /* Get ref to first byte containing part of the field.  */
  949.         xop0 = change_address (xop0, byte_mode,
  950.                        plus_constant (XEXP (xop0, 0), xoffset));
  951.  
  952.           volatile_ok = save_volatile_ok;
  953.         }
  954.  
  955.       /* If op0 is a register, we need it in MAXMODE (which is usually
  956.          SImode). to make it acceptable to the format of extzv.  */
  957.       if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
  958.         abort ();
  959.       if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
  960.         xop0 = gen_rtx (SUBREG, maxmode, xop0, 0);
  961.  
  962.       /* On big-endian machines, we count bits from the most significant.
  963.          If the bit field insn does not, we must invert.  */
  964. #if BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN
  965.       xbitpos = unit - bitsize - xbitpos;
  966. #endif
  967.       /* Now convert from counting within UNIT to counting in MAXMODE.  */
  968. #if BITS_BIG_ENDIAN
  969.       if (GET_CODE (xop0) != MEM)
  970.         xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
  971. #endif
  972.       unit = GET_MODE_BITSIZE (maxmode);
  973.  
  974.       if (xtarget == 0
  975.           || (flag_force_mem && GET_CODE (xtarget) == MEM))
  976.         xtarget = xspec_target = gen_reg_rtx (tmode);
  977.  
  978.       if (GET_MODE (xtarget) != maxmode)
  979.         {
  980.           if (GET_CODE (xtarget) == REG)
  981.         {
  982.           int wider = (GET_MODE_SIZE (maxmode)
  983.                    > GET_MODE_SIZE (GET_MODE (xtarget)));
  984.           xtarget = gen_lowpart (maxmode, xtarget);
  985.           if (wider)
  986.             xspec_target_subreg = xtarget;
  987.         }
  988.           else
  989.         xtarget = gen_reg_rtx (maxmode);
  990.         }
  991.  
  992.       /* If this machine's extzv insists on a register target,
  993.          make sure we have one.  */
  994.       if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][0])
  995.          (xtarget, maxmode)))
  996.         xtarget = gen_reg_rtx (maxmode);
  997.  
  998.       bitsize_rtx = gen_rtx (CONST_INT, VOIDmode, bitsize);
  999.       bitpos_rtx = gen_rtx (CONST_INT, VOIDmode, xbitpos);
  1000.  
  1001.       pat = gen_extzv (protect_from_queue (xtarget, 1),
  1002.                xop0, bitsize_rtx, bitpos_rtx);
  1003.       if (pat)
  1004.         {
  1005.           emit_insn (pat);
  1006.           target = xtarget;
  1007.           spec_target = xspec_target;
  1008.           spec_target_subreg = xspec_target_subreg;
  1009.         }
  1010.       else
  1011.         {
  1012.           delete_insns_since (last);
  1013.           target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
  1014.                         bitpos, target, 1, align);
  1015.         }
  1016.     }
  1017.       else
  1018.         extzv_loses:
  1019. #endif
  1020.     target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  1021.                       target, 1, align);
  1022.     }
  1023.   else
  1024.     {
  1025. #ifdef HAVE_extv
  1026.       if (HAVE_extv
  1027.       && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_extv][0])
  1028.           >= bitsize))
  1029.     {
  1030.       int xbitpos = bitpos, xoffset = offset;
  1031.       rtx bitsize_rtx, bitpos_rtx;
  1032.       rtx last = get_last_insn();
  1033.       rtx xop0 = op0, xtarget = target;
  1034.       rtx xspec_target = spec_target;
  1035.       rtx xspec_target_subreg = spec_target_subreg;
  1036.       rtx pat;
  1037.       enum machine_mode maxmode
  1038.         = insn_operand_mode[(int) CODE_FOR_extv][0];
  1039.  
  1040.       if (GET_CODE (xop0) == MEM)
  1041.         {
  1042.           /* Is the memory operand acceptable?  */
  1043.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extv][1])
  1044.              (xop0, GET_MODE (xop0))))
  1045.         {
  1046.           /* No, load into a reg and extract from there.  */
  1047.           enum machine_mode bestmode;
  1048.  
  1049.           /* Get the mode to use for inserting into this field.  If
  1050.              OP0 is BLKmode, get the smallest mode consistent with the
  1051.              alignment. If OP0 is a non-BLKmode object that is no
  1052.              wider than MAXMODE, use its mode. Otherwise, use the
  1053.              smallest mode containing the field.  */
  1054.  
  1055.           if (GET_MODE (xop0) == BLKmode
  1056.               || (GET_MODE_SIZE (GET_MODE (op0))
  1057.               > GET_MODE_SIZE (maxmode)))
  1058.             bestmode = get_best_mode (bitsize, bitnum,
  1059.                           align * BITS_PER_UNIT, maxmode,
  1060.                           MEM_VOLATILE_P (xop0));
  1061.           else
  1062.             bestmode = GET_MODE (xop0);
  1063.  
  1064.           if (bestmode == VOIDmode)
  1065.             goto extv_loses;
  1066.  
  1067.           /* Compute offset as multiple of this unit,
  1068.              counting in bytes.  */
  1069.           unit = GET_MODE_BITSIZE (bestmode);
  1070.           xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
  1071.           xbitpos = bitnum % unit;
  1072.           xop0 = change_address (xop0, bestmode,
  1073.                      plus_constant (XEXP (xop0, 0),
  1074.                             xoffset));
  1075.           /* Fetch it to a register in that size.  */
  1076.           xop0 = force_reg (bestmode, xop0);
  1077.  
  1078.           /* XBITPOS counts within UNIT, which is what is expected.  */
  1079.         }
  1080.           else
  1081.         /* Get ref to first byte containing part of the field.  */
  1082.         xop0 = change_address (xop0, byte_mode,
  1083.                        plus_constant (XEXP (xop0, 0), xoffset));
  1084.         }
  1085.  
  1086.       /* If op0 is a register, we need it in MAXMODE (which is usually
  1087.          SImode) to make it acceptable to the format of extv.  */
  1088.       if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
  1089.         abort ();
  1090.       if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
  1091.         xop0 = gen_rtx (SUBREG, maxmode, xop0, 0);
  1092.  
  1093.       /* On big-endian machines, we count bits from the most significant.
  1094.          If the bit field insn does not, we must invert.  */
  1095. #if BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN
  1096.       xbitpos = unit - bitsize - xbitpos;
  1097. #endif
  1098.       /* XBITPOS counts within a size of UNIT.
  1099.          Adjust to count within a size of MAXMODE.  */
  1100. #if BITS_BIG_ENDIAN
  1101.       if (GET_CODE (xop0) != MEM)
  1102.         xbitpos += (GET_MODE_BITSIZE (maxmode) - unit);
  1103. #endif
  1104.       unit = GET_MODE_BITSIZE (maxmode);
  1105.  
  1106.       if (xtarget == 0
  1107.           || (flag_force_mem && GET_CODE (xtarget) == MEM))
  1108.         xtarget = xspec_target = gen_reg_rtx (tmode);
  1109.  
  1110.       if (GET_MODE (xtarget) != maxmode)
  1111.         {
  1112.           if (GET_CODE (xtarget) == REG)
  1113.         {
  1114.           int wider = (GET_MODE_SIZE (maxmode)
  1115.                    > GET_MODE_SIZE (GET_MODE (xtarget)));
  1116.           xtarget = gen_lowpart (maxmode, xtarget);
  1117.           if (wider)
  1118.             xspec_target_subreg = xtarget;
  1119.         }
  1120.           else
  1121.         xtarget = gen_reg_rtx (maxmode);
  1122.         }
  1123.  
  1124.       /* If this machine's extv insists on a register target,
  1125.          make sure we have one.  */
  1126.       if (! ((*insn_operand_predicate[(int) CODE_FOR_extv][0])
  1127.          (xtarget, maxmode)))
  1128.         xtarget = gen_reg_rtx (maxmode);
  1129.  
  1130.       bitsize_rtx = gen_rtx (CONST_INT, VOIDmode, bitsize);
  1131.       bitpos_rtx = gen_rtx (CONST_INT, VOIDmode, xbitpos);
  1132.  
  1133.       pat = gen_extv (protect_from_queue (xtarget, 1),
  1134.               xop0, bitsize_rtx, bitpos_rtx);
  1135.       if (pat)
  1136.         {
  1137.           emit_insn (pat);
  1138.           target = xtarget;
  1139.           spec_target = xspec_target;
  1140.           spec_target_subreg = xspec_target_subreg;
  1141.         }
  1142.       else
  1143.         {
  1144.           delete_insns_since (last);
  1145.           target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
  1146.                         bitpos, target, 0, align);
  1147.         }
  1148.     } 
  1149.       else
  1150.     extv_loses:
  1151. #endif
  1152.     target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  1153.                       target, 0, align);
  1154.     }
  1155.   if (target == spec_target)
  1156.     return target;
  1157.   if (target == spec_target_subreg)
  1158.     return spec_target;
  1159.   if (GET_MODE (target) != tmode && GET_MODE (target) != mode)
  1160.     {
  1161.       /* If the target mode is floating-point, first convert to the
  1162.      integer mode of that size and then access it as a floating-point
  1163.      value via a SUBREG.  */
  1164.       if (GET_MODE_CLASS (tmode) == MODE_FLOAT)
  1165.     {
  1166.       target = convert_to_mode (mode_for_size (GET_MODE_BITSIZE (tmode),
  1167.                            MODE_INT, 0),
  1168.                     target, unsignedp);
  1169.       if (GET_CODE (target) != REG)
  1170.         target = copy_to_reg (target);
  1171.       return gen_rtx (SUBREG, tmode, target, 0);
  1172.     }
  1173.       else
  1174.     return convert_to_mode (tmode, target, unsignedp);
  1175.     }
  1176.   return target;
  1177. }
  1178.  
  1179. /* Extract a bit field using shifts and boolean operations
  1180.    Returns an rtx to represent the value.
  1181.    OP0 addresses a register (word) or memory (byte).
  1182.    BITPOS says which bit within the word or byte the bit field starts in.
  1183.    OFFSET says how many bytes farther the bit field starts;
  1184.     it is 0 if OP0 is a register.
  1185.    BITSIZE says how many bits long the bit field is.
  1186.     (If OP0 is a register, it may be narrower than a full word,
  1187.      but BITPOS still counts within a full word,
  1188.      which is significant on bigendian machines.)
  1189.  
  1190.    UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
  1191.    If TARGET is nonzero, attempts to store the value there
  1192.    and return TARGET, but this is not guaranteed.
  1193.    If TARGET is not used, create a pseudo-reg of mode TMODE for the value.
  1194.  
  1195.    ALIGN is the alignment that STR_RTX is known to have, measured in bytes.  */
  1196.  
  1197. static rtx
  1198. extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
  1199.              target, unsignedp, align)
  1200.      enum machine_mode tmode;
  1201.      register rtx op0, target;
  1202.      register int offset, bitsize, bitpos;
  1203.      int unsignedp;
  1204.      int align;
  1205. {
  1206.   int total_bits = BITS_PER_WORD;
  1207.   enum machine_mode mode;
  1208.  
  1209.   if (GET_CODE (op0) == SUBREG || GET_CODE (op0) == REG)
  1210.     {
  1211.       /* Special treatment for a bit field split across two registers.  */
  1212.       if (bitsize + bitpos > BITS_PER_WORD)
  1213.     return extract_split_bit_field (op0, bitsize, bitpos,
  1214.                     unsignedp, align);
  1215.     }
  1216.   else
  1217.     {
  1218.       /* Get the proper mode to use for this field.  We want a mode that
  1219.      includes the entire field.  If such a mode would be larger than
  1220.      a word, we won't be doing the extraction the normal way.  */
  1221.  
  1222.       mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
  1223.                 align * BITS_PER_UNIT, word_mode,
  1224.                 GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0));
  1225.  
  1226.       if (mode == VOIDmode)
  1227.     /* The only way this should occur is if the field spans word
  1228.        boundaries.  */
  1229.     return extract_split_bit_field (op0, bitsize,
  1230.                     bitpos + offset * BITS_PER_UNIT,
  1231.                     unsignedp, align);
  1232.  
  1233.       total_bits = GET_MODE_BITSIZE (mode);
  1234.  
  1235.       /* Get ref to an aligned byte, halfword, or word containing the field.
  1236.      Adjust BITPOS to be position within a word,
  1237.      and OFFSET to be the offset of that word.
  1238.      Then alter OP0 to refer to that word.  */
  1239.       bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
  1240.       offset -= (offset % (total_bits / BITS_PER_UNIT));
  1241.       op0 = change_address (op0, mode,
  1242.                 plus_constant (XEXP (op0, 0), offset));
  1243.     }
  1244.  
  1245.   mode = GET_MODE (op0);
  1246.  
  1247. #if BYTES_BIG_ENDIAN
  1248.   /* BITPOS is the distance between our msb and that of OP0.
  1249.      Convert it to the distance from the lsb.  */
  1250.  
  1251.   bitpos = total_bits - bitsize - bitpos;
  1252. #endif
  1253.   /* Now BITPOS is always the distance between the field's lsb and that of OP0.
  1254.      We have reduced the big-endian case to the little-endian case.  */
  1255.  
  1256.   if (unsignedp)
  1257.     {
  1258.       if (bitpos)
  1259.     {
  1260.       /* If the field does not already start at the lsb,
  1261.          shift it so it does.  */
  1262.       tree amount = build_int_2 (bitpos, 0);
  1263.       /* Maybe propagate the target for the shift.  */
  1264.       /* But not if we will return it--could confuse integrate.c.  */
  1265.       rtx subtarget = (target != 0 && GET_CODE (target) == REG
  1266.                && !REG_FUNCTION_VALUE_P (target)
  1267.                ? target : 0);
  1268.       if (tmode != mode) subtarget = 0;
  1269.       op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
  1270.     }
  1271.       /* Convert the value to the desired mode.  */
  1272.       if (mode != tmode)
  1273.     op0 = convert_to_mode (tmode, op0, 1);
  1274.  
  1275.       /* Unless the msb of the field used to be the msb when we shifted,
  1276.      mask out the upper bits.  */
  1277.  
  1278.       if (GET_MODE_BITSIZE (mode) != bitpos + bitsize
  1279. #if 0
  1280. #ifdef SLOW_ZERO_EXTEND
  1281.       /* Always generate an `and' if
  1282.          we just zero-extended op0 and SLOW_ZERO_EXTEND, since it
  1283.          will combine fruitfully with the zero-extend. */
  1284.       || tmode != mode
  1285. #endif
  1286. #endif
  1287.       )
  1288.     return expand_binop (GET_MODE (op0), and_optab, op0,
  1289.                  mask_rtx (GET_MODE (op0), 0, bitsize, 0),
  1290.                  target, 1, OPTAB_LIB_WIDEN);
  1291.       return op0;
  1292.     }
  1293.  
  1294.   /* To extract a signed bit-field, first shift its msb to the msb of the word,
  1295.      then arithmetic-shift its lsb to the lsb of the word.  */
  1296.   op0 = force_reg (mode, op0);
  1297.   if (mode != tmode)
  1298.     target = 0;
  1299.  
  1300.   /* Find the narrowest integer mode that contains the field.  */
  1301.  
  1302.   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
  1303.        mode = GET_MODE_WIDER_MODE (mode))
  1304.     if (GET_MODE_BITSIZE (mode) >= bitsize + bitpos)
  1305.       {
  1306.     op0 = convert_to_mode (mode, op0, 0);
  1307.     break;
  1308.       }
  1309.  
  1310.   if (GET_MODE_BITSIZE (mode) != (bitsize + bitpos))
  1311.     {
  1312.       tree amount = build_int_2 (GET_MODE_BITSIZE (mode) - (bitsize + bitpos), 0);
  1313.       /* Maybe propagate the target for the shift.  */
  1314.       /* But not if we will return the result--could confuse integrate.c.  */
  1315.       rtx subtarget = (target != 0 && GET_CODE (target) == REG
  1316.                && ! REG_FUNCTION_VALUE_P (target)
  1317.                ? target : 0);
  1318.       op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
  1319.     }
  1320.  
  1321.   return expand_shift (RSHIFT_EXPR, mode, op0,
  1322.                build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0), 
  1323.                target, 0);
  1324. }
  1325.  
  1326. /* Return a constant integer (CONST_INT or CONST_DOUBLE) mask value
  1327.    of mode MODE with BITSIZE ones followed by BITPOS zeros, or the
  1328.    complement of that if COMPLEMENT.  The mask is truncated if
  1329.    necessary to the width of mode MODE.  */
  1330.  
  1331. static rtx
  1332. mask_rtx (mode, bitpos, bitsize, complement)
  1333.      enum machine_mode mode;
  1334.      int bitpos, bitsize, complement;
  1335. {
  1336.   int masklow, maskhigh;
  1337.  
  1338.   if (bitpos < HOST_BITS_PER_INT)
  1339.     masklow = -1 << bitpos;
  1340.   else
  1341.     masklow = 0;
  1342.  
  1343.   if (bitpos + bitsize < HOST_BITS_PER_INT)
  1344.     masklow &= (unsigned) -1 >> (HOST_BITS_PER_INT - bitpos - bitsize);
  1345.   
  1346.   if (bitpos <= HOST_BITS_PER_INT)
  1347.     maskhigh = -1;
  1348.   else
  1349.     maskhigh = -1 << (bitpos - HOST_BITS_PER_INT);
  1350.  
  1351.   if (bitpos + bitsize > HOST_BITS_PER_INT)
  1352.     maskhigh &= (unsigned) -1 >> (2 * HOST_BITS_PER_INT - bitpos - bitsize);
  1353.   else
  1354.     maskhigh = 0;
  1355.  
  1356.   if (complement)
  1357.     {
  1358.       maskhigh = ~maskhigh;
  1359.       masklow = ~masklow;
  1360.     }
  1361.  
  1362.   return immed_double_const (masklow, maskhigh, mode);
  1363. }
  1364.  
  1365. /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
  1366.    VALUE truncated to BITSIZE bits and then shifted left BITPOS bits.  */
  1367.  
  1368. static rtx
  1369. lshift_value (mode, value, bitpos, bitsize)
  1370.      enum machine_mode mode;
  1371.      rtx value;
  1372.      int bitpos, bitsize;
  1373. {
  1374.   unsigned v = INTVAL (value);
  1375.   int low, high;
  1376.  
  1377.   if (bitsize < HOST_BITS_PER_INT)
  1378.     v &= ~(-1 << bitsize);
  1379.  
  1380.   if (bitpos < HOST_BITS_PER_INT)
  1381.     {
  1382.       low = v << bitpos;
  1383.       high = (bitpos > 0 ? (v >> (HOST_BITS_PER_INT - bitpos)) : 0);
  1384.     }
  1385.   else
  1386.     {
  1387.       low = 0;
  1388.       high = v << (bitpos - HOST_BITS_PER_INT);
  1389.     }
  1390.  
  1391.   return immed_double_const (low, high, mode);
  1392. }
  1393.  
  1394. /* Extract a bit field that is split across two words
  1395.    and return an RTX for the result.
  1396.  
  1397.    OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
  1398.    BITSIZE is the field width; BITPOS, position of its first bit, in the word.
  1399.    UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend.  */
  1400.  
  1401. static rtx
  1402. extract_split_bit_field (op0, bitsize, bitpos, unsignedp, align)
  1403.      rtx op0;
  1404.      int bitsize, bitpos, unsignedp, align;
  1405. {
  1406.   /* BITSIZE_1 is size of the part in the first word.  */
  1407.   int bitsize_1 = BITS_PER_WORD - bitpos % BITS_PER_WORD;
  1408.   /* BITSIZE_2 is size of the rest (in the following word).  */
  1409.   int bitsize_2 = bitsize - bitsize_1;
  1410.   rtx part1, part2, result;
  1411.   int unit = GET_CODE (op0) == MEM ? BITS_PER_UNIT : BITS_PER_WORD;
  1412.   int offset = bitpos / unit;
  1413.   rtx word;
  1414.  
  1415.   /* The field must span exactly one word boundary.  */
  1416.   if (bitpos / BITS_PER_WORD != (bitpos + bitsize - 1) / BITS_PER_WORD - 1)
  1417.     abort ();
  1418.  
  1419.   /* Get the part of the bit field from the first word.  If OP0 is a MEM,
  1420.      pass OP0 and the offset computed above.  Otherwise, get the proper
  1421.      word and pass an offset of zero.  */
  1422.   word = (GET_CODE (op0) == MEM ? op0
  1423.       : operand_subword_force (op0, offset, GET_MODE (op0)));
  1424.   part1 = extract_fixed_bit_field (word_mode, word,
  1425.                    GET_CODE (op0) == MEM ? offset : 0,
  1426.                    bitsize_1, bitpos % unit, 0, 1, align);
  1427.  
  1428.   /* Offset op0 by 1 word to get to the following one.  */
  1429.   if (GET_CODE (op0) == SUBREG)
  1430.     word = operand_subword_force (SUBREG_REG (op0),
  1431.                   SUBREG_WORD (op0) + offset + 1, VOIDmode);
  1432.   else if (GET_CODE (op0) == MEM)
  1433.     word = op0;
  1434.   else
  1435.     word = operand_subword_force (op0, offset + 1, GET_MODE (op0));
  1436.  
  1437.   /* Get the part of the bit field from the second word.  */
  1438.   part2 = extract_fixed_bit_field (word_mode, word,
  1439.                    (GET_CODE (op0) == MEM
  1440.                     ? CEIL (offset + 1, UNITS_PER_WORD) * UNITS_PER_WORD
  1441.                     : 0),
  1442.                    bitsize_2, 0, 0, 1, align);
  1443.  
  1444.   /* Shift the more significant part up to fit above the other part.  */
  1445. #if BYTES_BIG_ENDIAN
  1446.   part1 = expand_shift (LSHIFT_EXPR, word_mode, part1,
  1447.             build_int_2 (bitsize_2, 0), 0, 1);
  1448. #else
  1449.   part2 = expand_shift (LSHIFT_EXPR, word_mode, part2,
  1450.             build_int_2 (bitsize_1, 0), 0, 1);
  1451. #endif
  1452.  
  1453.   /* Combine the two parts with bitwise or.  This works
  1454.      because we extracted both parts as unsigned bit fields.  */
  1455.   result = expand_binop (word_mode, ior_optab, part1, part2, 0, 1,
  1456.              OPTAB_LIB_WIDEN);
  1457.  
  1458.   /* Unsigned bit field: we are done.  */
  1459.   if (unsignedp)
  1460.     return result;
  1461.   /* Signed bit field: sign-extend with two arithmetic shifts.  */
  1462.   result = expand_shift (LSHIFT_EXPR, word_mode, result,
  1463.              build_int_2 (BITS_PER_WORD - bitsize, 0), 0, 0);
  1464.   return expand_shift (RSHIFT_EXPR, word_mode, result,
  1465.                build_int_2 (BITS_PER_WORD - bitsize, 0), 0, 0);
  1466. }
  1467.  
  1468. /* Add INC into TARGET.  */
  1469.  
  1470. void
  1471. expand_inc (target, inc)
  1472.      rtx target, inc;
  1473. {
  1474.   rtx value = expand_binop (GET_MODE (target), add_optab,
  1475.                 target, inc,
  1476.                 target, 0, OPTAB_LIB_WIDEN);
  1477.   if (value != target)
  1478.     emit_move_insn (target, value);
  1479. }
  1480.  
  1481. /* Subtract DEC from TARGET.  */
  1482.  
  1483. void
  1484. expand_dec (target, dec)
  1485.      rtx target, dec;
  1486. {
  1487.   rtx value = expand_binop (GET_MODE (target), sub_optab,
  1488.                 target, dec,
  1489.                 target, 0, OPTAB_LIB_WIDEN);
  1490.   if (value != target)
  1491.     emit_move_insn (target, value);
  1492. }
  1493.  
  1494. /* Output a shift instruction for expression code CODE,
  1495.    with SHIFTED being the rtx for the value to shift,
  1496.    and AMOUNT the tree for the amount to shift by.
  1497.    Store the result in the rtx TARGET, if that is convenient.
  1498.    If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
  1499.    Return the rtx for where the value is.  */
  1500.  
  1501. rtx
  1502. expand_shift (code, mode, shifted, amount, target, unsignedp)
  1503.      enum tree_code code;
  1504.      register enum machine_mode mode;
  1505.      rtx shifted;
  1506.      tree amount;
  1507.      register rtx target;
  1508.      int unsignedp;
  1509. {
  1510.   register rtx op1, temp = 0;
  1511.   register int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
  1512.   register int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
  1513.   int try;
  1514.  
  1515.   /* Previously detected shift-counts computed by NEGATE_EXPR
  1516.      and shifted in the other direction; but that does not work
  1517.      on all machines.  */
  1518.  
  1519.   op1 = expand_expr (amount, 0, VOIDmode, 0);
  1520.  
  1521.   if (op1 == const0_rtx)
  1522.     return shifted;
  1523.  
  1524.   for (try = 0; temp == 0 && try < 3; try++)
  1525.     {
  1526.       enum optab_methods methods;
  1527.  
  1528.       if (try == 0)
  1529.     methods = OPTAB_DIRECT;
  1530.       else if (try == 1)
  1531.     methods = OPTAB_WIDEN;
  1532.       else
  1533.     methods = OPTAB_LIB_WIDEN;
  1534.  
  1535.       if (rotate)
  1536.     {
  1537.       /* Widening does not work for rotation.  */
  1538.       if (methods == OPTAB_WIDEN)
  1539.         continue;
  1540.       else if (methods == OPTAB_LIB_WIDEN)
  1541.         methods = OPTAB_LIB;
  1542.  
  1543.       temp = expand_binop (mode,
  1544.                    left ? rotl_optab : rotr_optab,
  1545.                    shifted, op1, target, unsignedp, methods);
  1546.     }
  1547.       else if (unsignedp)
  1548.     {
  1549.       temp = expand_binop (mode,
  1550.                    left ? lshl_optab : lshr_optab,
  1551.                    shifted, op1, target, unsignedp, methods);
  1552.       if (temp == 0 && left)
  1553.         temp = expand_binop (mode, ashl_optab,
  1554.                  shifted, op1, target, unsignedp, methods);
  1555.     }
  1556.  
  1557.       /* Do arithmetic shifts.
  1558.      Also, if we are going to widen the operand, we can just as well
  1559.      use an arithmetic right-shift instead of a logical one.  */
  1560.       if (temp == 0 && ! rotate
  1561.       && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
  1562.     {
  1563.       enum optab_methods methods1 = methods;
  1564.  
  1565.       /* If trying to widen a log shift to an arithmetic shift,
  1566.          don't accept an arithmetic shift of the same size.  */
  1567.       if (unsignedp)
  1568.         methods1 = OPTAB_MUST_WIDEN;
  1569.  
  1570.       /* Arithmetic shift */
  1571.  
  1572.       temp = expand_binop (mode,
  1573.                    left ? ashl_optab : ashr_optab,
  1574.                    shifted, op1, target, unsignedp, methods1);
  1575.     }
  1576.  
  1577. #ifdef HAVE_extzv
  1578.       /* We can do a logical (unsigned) right shift with a bit-field
  1579.      extract insn.  But first check if one of the above methods worked.  */
  1580.       if (temp != 0)
  1581.     return temp;
  1582.  
  1583.       if (unsignedp && code == RSHIFT_EXPR && ! BITS_BIG_ENDIAN && HAVE_extzv)
  1584.     {
  1585.       enum machine_mode output_mode
  1586.         = insn_operand_mode[(int) CODE_FOR_extzv][0];
  1587.  
  1588.       if ((methods == OPTAB_DIRECT && mode == output_mode)
  1589.           || (methods == OPTAB_WIDEN
  1590.           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (output_mode)))
  1591.         {
  1592. #ifdef NeXT
  1593.           rtx shifted1 = convert_to_mode (output_mode,
  1594.                           protect_from_queue(shifted, 0),
  1595.                           1);
  1596. #else
  1597.           /* Note convert_to_mode does protect_from_queue.  */
  1598.           rtx shifted1 = convert_to_mode (output_mode, shifted, 1);
  1599. #endif /* NeXT */
  1600.           enum machine_mode length_mode
  1601.         = insn_operand_mode[(int) CODE_FOR_extzv][2];
  1602.           enum machine_mode pos_mode
  1603.         = insn_operand_mode[(int) CODE_FOR_extzv][3];
  1604.           rtx target1 = 0;
  1605.           rtx last = get_last_insn ();
  1606.           rtx width;
  1607.           rtx xop1 = op1;
  1608.           rtx pat;
  1609.  
  1610.           if (target != 0)
  1611.         target1 = protect_from_queue (target, 1);
  1612.  
  1613.           /* We define extract insns as having OUTPUT_MODE in a register
  1614.          and the mode of operand 1 in memory.  Since we want
  1615.          OUTPUT_MODE, we will always force the operand into a
  1616.          register.  At some point we might want to support MEM
  1617.          directly. */
  1618.           shifted1 = force_reg (output_mode, shifted1);
  1619.  
  1620.           /* If we don't have or cannot use a suggested target,
  1621.          make a place for the result, in the proper mode.  */
  1622.           if (methods == OPTAB_WIDEN || target1 == 0
  1623.           || ! ((*insn_operand_predicate[(int) CODE_FOR_extzv][0])
  1624.             (target1, output_mode)))
  1625.         target1 = gen_reg_rtx (output_mode);
  1626.  
  1627. #ifdef NeXT
  1628.           xop1 = protect_from_queue (xop1, 0);
  1629. #endif /* NeXT */
  1630.           xop1 = convert_to_mode (pos_mode, xop1,
  1631.                       TREE_UNSIGNED (TREE_TYPE (amount)));
  1632.  
  1633.           /* If this machine's extzv insists on a register for
  1634.          operand 3 (position), arrange for that.  */
  1635.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][3])
  1636.              (xop1, pos_mode)))
  1637.         xop1 = force_reg (pos_mode, xop1);
  1638.  
  1639.           /* WIDTH gets the width of the bit field to extract:
  1640.          wordsize minus # bits to shift by.  */
  1641.           if (GET_CODE (xop1) == CONST_INT)
  1642.         width = gen_rtx (CONST_INT, VOIDmode,
  1643.                  (GET_MODE_BITSIZE (mode) - INTVAL (op1)));
  1644.           else
  1645.         {
  1646.           /* Now get the width in the proper mode.  */
  1647. #ifdef NeXT
  1648.           op1 = protect_from_queue (op1, 0);
  1649. #endif /* NeXT */
  1650.           width = convert_to_mode (length_mode, op1,
  1651.                        TREE_UNSIGNED (TREE_TYPE (amount)));
  1652.  
  1653.           width = expand_binop (length_mode, sub_optab,
  1654.                     gen_rtx (CONST_INT, VOIDmode,
  1655.                          GET_MODE_BITSIZE (mode)),
  1656.                     width, 0, 0, OPTAB_LIB_WIDEN);
  1657.         }
  1658.  
  1659.           /* If this machine's extzv insists on a register for
  1660.          operand 2 (length), arrange for that.  */
  1661.           if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][2])
  1662.              (width, length_mode)))
  1663.         width = force_reg (length_mode, width);
  1664.  
  1665.           /* Now extract with WIDTH, omitting OP1 least sig bits.  */
  1666.           pat = gen_extzv (target1, shifted1, width, xop1);
  1667.           if (pat)
  1668.         {
  1669.           emit_insn (pat);
  1670.           temp = convert_to_mode (mode, target1, 1);
  1671.         }
  1672.           else
  1673.         delete_insns_since (last);
  1674.         }
  1675.  
  1676.       /* Can also do logical shift with signed bit-field extract
  1677.          followed by inserting the bit-field at a different position.
  1678.          That strategy is not yet implemented.  */
  1679.     }
  1680. #endif /* HAVE_extzv */
  1681.     }
  1682.  
  1683.   if (temp == 0)
  1684.     abort ();
  1685.   return temp;
  1686. }
  1687.  
  1688. enum alg_code { alg_add, alg_subtract, alg_compound };
  1689.  
  1690. /* This structure records a sequence of operations.
  1691.    `ops' is the number of operations recorded.
  1692.    `cost' is their total cost.
  1693.    The operations are stored in `op' and the corresponding
  1694.    integer coefficients in `coeff'.
  1695.    These are the operations:
  1696.    alg_add       Add to the total the multiplicand times the coefficient.
  1697.    alg_subtract  Subtract the multiplicand times the coefficient.
  1698.    alg_compound  This coefficient plus or minus the following one
  1699.                  is multiplied into the total.  The following operation
  1700.                  is alg_add or alg_subtract to indicate whether to add
  1701.          or subtract the two coefficients.  */
  1702.  
  1703. #ifndef MAX_BITS_PER_WORD
  1704. #define MAX_BITS_PER_WORD BITS_PER_WORD
  1705. #endif
  1706.  
  1707. struct algorithm
  1708. {
  1709.   int cost;
  1710.   unsigned int ops;
  1711.   enum alg_code op[MAX_BITS_PER_WORD];
  1712.   unsigned int coeff[MAX_BITS_PER_WORD];
  1713. };
  1714.  
  1715. /* Compute and return the best algorithm for multiplying by T.
  1716.    Assume that add insns cost ADD_COST and shifts cost SHIFT_COST.
  1717.    Return cost -1 if would cost more than MAX_COST.  */
  1718.  
  1719. static struct algorithm
  1720. synth_mult (t, add_cost, shift_cost, max_cost)
  1721.      unsigned int t;
  1722.      int add_cost, shift_cost;
  1723.      int max_cost;
  1724. {
  1725.   int m, n;
  1726.   struct algorithm *best_alg = (struct algorithm *)alloca (sizeof (struct algorithm));
  1727.   struct algorithm *alg_in = (struct algorithm *)alloca (sizeof (struct algorithm));
  1728.   unsigned int cost;
  1729.  
  1730.   /* No matter what happens, we want to return a valid algorithm.  */
  1731.   best_alg->cost = max_cost;
  1732.   best_alg->ops = 0;
  1733.  
  1734.   /* Is t an exponent of 2, so we can just do a shift?  */
  1735.  
  1736.   if ((t & -t) == t)
  1737.     {
  1738.       if (t > 1)
  1739.     {
  1740.       if (max_cost >= shift_cost)
  1741.         {
  1742.           best_alg->cost = shift_cost;
  1743.           best_alg->ops = 1;
  1744.           best_alg->op[0] = alg_add;
  1745.           best_alg->coeff[0] = t;
  1746.         }
  1747.       else
  1748.         best_alg->cost = -1;
  1749.     }
  1750.       else if (t == 1)
  1751.     {
  1752.       if (max_cost >= 0)
  1753.         best_alg->cost = 0;
  1754.     }
  1755.       else
  1756.     best_alg->cost = 0;
  1757.  
  1758.       return *best_alg;
  1759.     }
  1760.  
  1761.   /* If MAX_COST just permits as little as an addition (or less), we won't
  1762.      succeed in synthesizing an algorithm for t.  Return immediately with
  1763.      an indication of failure.  */
  1764.   if (max_cost <= add_cost)
  1765.     {
  1766.       best_alg->cost = -1;
  1767.       return *best_alg;
  1768.     }
  1769.  
  1770.   /* Look for factors of t of the form
  1771.      t = q(2**m +- 1), 2 <= m <= floor(log2(t)) - 1.
  1772.      If we find such a factor, we can multiply by t using an algorithm that
  1773.      multiplies by q, shift the result by m and add/subtract it to itself.  */
  1774.  
  1775.   for (m = floor_log2 (t) - 1; m >= 2; m--)
  1776.     {
  1777.       int m_exp_2 = 1 << m;
  1778.       int d;
  1779.  
  1780.       d = m_exp_2 + 1;
  1781.       if (t % d == 0)
  1782.     {
  1783.       int q = t / d;
  1784.  
  1785.       cost = add_cost + shift_cost * 2;
  1786.  
  1787.       *alg_in = synth_mult (q, add_cost, shift_cost,
  1788.                 MIN (max_cost, best_alg->cost) - cost);
  1789.  
  1790.       if (alg_in->cost >= 0)
  1791.         {
  1792.           cost += alg_in->cost;
  1793.  
  1794.           if (cost < best_alg->cost)
  1795.         {
  1796.           struct algorithm *x;
  1797.           x = alg_in;
  1798.           alg_in = best_alg;
  1799.           best_alg = x;
  1800.           best_alg->coeff[best_alg->ops] = m_exp_2;
  1801.           best_alg->op[best_alg->ops++] = alg_compound;
  1802.           best_alg->coeff[best_alg->ops] = 1;
  1803.           best_alg->op[best_alg->ops++] = alg_add;
  1804.           best_alg->cost = cost;
  1805.         }
  1806.         }
  1807.     }
  1808.  
  1809.       d = m_exp_2 - 1;
  1810.       if (t % d == 0)
  1811.     {
  1812.       int q = t / d;
  1813.  
  1814.       cost = add_cost + shift_cost * 2;
  1815.  
  1816.       *alg_in = synth_mult (q, add_cost, shift_cost,
  1817.                 MIN (max_cost, best_alg->cost) - cost);
  1818.  
  1819.       if (alg_in->cost >= 0)
  1820.         {
  1821.           cost += alg_in->cost;
  1822.  
  1823.           if (cost < best_alg->cost)
  1824.         {
  1825.           struct algorithm *x;
  1826.           x = alg_in;
  1827.           alg_in = best_alg;
  1828.           best_alg = x;
  1829.           best_alg->coeff[best_alg->ops] = m_exp_2;
  1830.           best_alg->op[best_alg->ops++] = alg_compound;
  1831.           best_alg->coeff[best_alg->ops] = 1;
  1832.           best_alg->op[best_alg->ops++] = alg_subtract;
  1833.           best_alg->cost = cost;
  1834.         }
  1835.         }
  1836.     }
  1837.     }
  1838.  
  1839.   /* Try load effective address instructions, i.e. do a*3, a*5, a*9.  */
  1840.  
  1841.   {
  1842.     int q;
  1843.     int w;
  1844.  
  1845.     q = t & -t;            /* get out lsb */
  1846.     w = (t - q) & -(t - q);    /* get out next lsb */
  1847.  
  1848.     if (w / q <= lea_max_mul)
  1849.       {
  1850.     cost = lea_cost + (q != 1 ? shift_cost : 0);
  1851.  
  1852.     *alg_in = synth_mult (t - q - w, add_cost, shift_cost,
  1853.                   MIN (max_cost, best_alg->cost) - cost);
  1854.  
  1855.     if (alg_in->cost >= 0)
  1856.       {
  1857.         cost += alg_in->cost;
  1858.  
  1859.         /* Use <= to prefer this method to the factoring method
  1860.            when the cost appears the same, because this method
  1861.            uses fewer temporary registers.  */
  1862.         if (cost <= best_alg->cost)
  1863.           {
  1864.         struct algorithm *x;
  1865.         x = alg_in;
  1866.         alg_in = best_alg;
  1867.         best_alg = x;
  1868.         best_alg->coeff[best_alg->ops] = w;
  1869.         best_alg->op[best_alg->ops++] = alg_add;
  1870.         best_alg->coeff[best_alg->ops] = q;
  1871.         best_alg->op[best_alg->ops++] = alg_add;
  1872.         best_alg->cost = cost;
  1873.           }
  1874.       }
  1875.       }
  1876.   }
  1877.  
  1878.   /* Now, use the good old method to add or subtract at the leftmost
  1879.      1-bit.  */
  1880.  
  1881.   {
  1882.     int q;
  1883.     int w;
  1884.  
  1885.     q = t & -t;            /* get out lsb */
  1886.     for (w = q; (w & t) != 0; w <<= 1)
  1887.       ;
  1888.     if ((w > q << 1)
  1889.     /* Reject the case where t has only two bits.
  1890.        Thus we prefer addition in that case.  */
  1891.     && !(t < w && w == q << 2))
  1892.       {
  1893.     /* There are many bits in a row.  Make 'em by subtraction.  */
  1894.  
  1895.     cost = add_cost;
  1896.     if (q != 1)
  1897.       cost += shift_cost;
  1898.  
  1899.     *alg_in = synth_mult (t + q, add_cost, shift_cost,
  1900.                   MIN (max_cost, best_alg->cost) - cost);
  1901.  
  1902.     if (alg_in->cost >= 0)
  1903.       {
  1904.         cost += alg_in->cost;
  1905.  
  1906.         /* Use <= to prefer this method to the factoring method
  1907.            when the cost appears the same, because this method
  1908.            uses fewer temporary registers.  */
  1909.         if (cost <= best_alg->cost)
  1910.           {
  1911.         struct algorithm *x;
  1912.         x = alg_in;
  1913.         alg_in = best_alg;
  1914.         best_alg = x;
  1915.         best_alg->coeff[best_alg->ops] = q;
  1916.         best_alg->op[best_alg->ops++] = alg_subtract;
  1917.         best_alg->cost = cost;
  1918.           }
  1919.       }
  1920.       }
  1921.     else
  1922.       {
  1923.     /* There's only one bit at the left.  Make it by addition.  */
  1924.  
  1925.     cost = add_cost;
  1926.     if (q != 1)
  1927.       cost += shift_cost;
  1928.  
  1929.     *alg_in = synth_mult (t - q, add_cost, shift_cost,
  1930.                   MIN (max_cost, best_alg->cost) - cost);
  1931.  
  1932.     if (alg_in->cost >= 0)
  1933.       {
  1934.         cost += alg_in->cost;
  1935.  
  1936.         if (cost <= best_alg->cost)
  1937.           {
  1938.         struct algorithm *x;
  1939.         x = alg_in;
  1940.         alg_in = best_alg;
  1941.         best_alg = x;
  1942.         best_alg->coeff[best_alg->ops] = q;
  1943.         best_alg->op[best_alg->ops++] = alg_add;
  1944.         best_alg->cost = cost;
  1945.           }
  1946.       }
  1947.       }
  1948.   }
  1949.  
  1950.   if (best_alg->cost >= max_cost)
  1951.     best_alg->cost = -1;
  1952.   return *best_alg;
  1953. }
  1954.  
  1955. /* Perform a multiplication and return an rtx for the result.
  1956.    MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
  1957.    TARGET is a suggestion for where to store the result (an rtx).
  1958.  
  1959.    We check specially for a constant integer as OP1.
  1960.    If you want this check for OP0 as well, then before calling
  1961.    you should swap the two operands if OP0 would be constant.  */
  1962.  
  1963. rtx
  1964. expand_mult (mode, op0, op1, target, unsignedp)
  1965.      enum machine_mode mode;
  1966.      register rtx op0, op1, target;
  1967.      int unsignedp;
  1968. {
  1969.   rtx const_op1 = op1;
  1970.  
  1971.   /* If we are multiplying in DImode, it may still be a win
  1972.      to try to work with shifts and adds.  */
  1973.   if (GET_CODE (op1) == CONST_DOUBLE
  1974.       && GET_MODE_CLASS (GET_MODE (op1)) == MODE_INT
  1975.       && HOST_BITS_PER_INT <= BITS_PER_WORD)
  1976.     {
  1977.       if ((CONST_DOUBLE_HIGH (op1) == 0 && CONST_DOUBLE_LOW (op1) >= 0)
  1978.       || (CONST_DOUBLE_HIGH (op1) == -1 && CONST_DOUBLE_LOW (op1) < 0))
  1979.     const_op1 = gen_rtx (CONST_INT, VOIDmode, CONST_DOUBLE_LOW (op1));
  1980.     }
  1981.  
  1982.   /* We used to test optimize here, on the grounds that it's better to
  1983.      produce a smaller program when -O is not used.
  1984.      But this causes such a terrible slowdown sometimes
  1985.      that it seems better to use synth_mult always.  */
  1986.   if (GET_CODE (const_op1) == CONST_INT && ! mult_is_very_cheap)
  1987.     {
  1988.       struct algorithm alg;
  1989.       struct algorithm neg_alg;
  1990.       int negate = 0;
  1991.       int absval = INTVAL (op1);
  1992.       rtx last;
  1993.  
  1994.       /* Try to do the computation two ways: multiply by the negative of OP1
  1995.      and then negate, or do the multiplication directly.  The latter is
  1996.      usually faster for positive numbers and the former for negative
  1997.      numbers, but the opposite can be faster if the original value
  1998.      has a factor of 2**m +/- 1, while the negated value does not or
  1999.      vice versa.  */
  2000.  
  2001.       alg = synth_mult (absval, add_cost, shift_cost, mult_cost);
  2002.       neg_alg = synth_mult (- absval, add_cost, shift_cost,
  2003.                 mult_cost - negate_cost);
  2004.  
  2005.       if (neg_alg.cost >= 0 && neg_alg.cost + negate_cost < alg.cost)
  2006.     alg = neg_alg, negate = 1, absval = - absval;
  2007.  
  2008.       if (alg.cost >= 0)
  2009.     {
  2010.       /* If we found something, it must be cheaper than multiply.
  2011.          So use it.  */
  2012.       int opno = 0;
  2013.       rtx accum, tem;
  2014.       int factors_seen = 0;
  2015.  
  2016.       op0 = protect_from_queue (op0, 0);
  2017.  
  2018.       /* Avoid referencing memory over and over.
  2019.          For speed, but also for correctness when mem is volatile.  */
  2020.       if (GET_CODE (op0) == MEM)
  2021.         op0 = force_reg (mode, op0);
  2022.  
  2023.       if (alg.ops == 0)
  2024.         accum = copy_to_mode_reg (mode, op0);
  2025.       else
  2026.         {
  2027.           /* 1 if this is the last in a series of adds and subtracts.  */
  2028.           int last = (1 == alg.ops || alg.op[1] == alg_compound);
  2029.           int log = floor_log2 (alg.coeff[0]);
  2030.           if (! factors_seen && ! last)
  2031.         log -= floor_log2 (alg.coeff[1]);
  2032.  
  2033.           if (alg.op[0] != alg_add)
  2034.         abort ();
  2035.           accum = expand_shift (LSHIFT_EXPR, mode, op0,
  2036.                     build_int_2 (log, 0),
  2037.                     0, 0);
  2038.         }
  2039.    
  2040.       while (++opno < alg.ops)
  2041.         {
  2042.           int log = floor_log2 (alg.coeff[opno]);
  2043.           /* 1 if this is the last in a series of adds and subtracts.  */
  2044.           int last = (opno + 1 == alg.ops
  2045.               || alg.op[opno + 1] == alg_compound);
  2046.  
  2047.           /* If we have not yet seen any separate factors (alg_compound)
  2048.          then turn op0<<a1 + op0<<a2 + op0<<a3... into
  2049.          (op0<<(a1-a2) + op0)<<(a2-a3) + op0...  */
  2050.           switch (alg.op[opno])
  2051.         {
  2052.         case alg_add:
  2053.           if (factors_seen)
  2054.             {
  2055.               tem = expand_shift (LSHIFT_EXPR, mode, op0,
  2056.                       build_int_2 (log, 0), 0, 0);
  2057.               accum = force_operand (gen_rtx (PLUS, mode, accum, tem),
  2058.                          accum);
  2059.             }
  2060.           else
  2061.             {
  2062.               if (! last)
  2063.             log -= floor_log2 (alg.coeff[opno + 1]);
  2064.               accum = force_operand (gen_rtx (PLUS, mode, accum, op0),
  2065.                          accum);
  2066.               accum = expand_shift (LSHIFT_EXPR, mode, accum,
  2067.                         build_int_2 (log, 0), accum, 0);
  2068.             }
  2069.           break;
  2070.  
  2071.         case alg_subtract:
  2072.           if (factors_seen)
  2073.             {
  2074.               tem = expand_shift (LSHIFT_EXPR, mode, op0,
  2075.                       build_int_2 (log, 0), 0, 0);
  2076.               accum = force_operand (gen_rtx (MINUS, mode, accum, tem),
  2077.                          accum);
  2078.             }
  2079.           else
  2080.             {
  2081.               if (! last)
  2082.             log -= floor_log2 (alg.coeff[opno + 1]);
  2083.               accum = force_operand (gen_rtx (MINUS, mode, accum, op0),
  2084.                          accum);
  2085.               accum = expand_shift (LSHIFT_EXPR, mode, accum,
  2086.                         build_int_2 (log, 0), accum, 0);
  2087.             }
  2088.  
  2089.           break;
  2090.  
  2091.         case alg_compound:
  2092.           factors_seen = 1;
  2093.           tem = expand_shift (LSHIFT_EXPR, mode, accum,
  2094.                       build_int_2 (log, 0), 0, 0);
  2095.  
  2096.           log = floor_log2 (alg.coeff[opno + 1]);
  2097.           accum = expand_shift (LSHIFT_EXPR, mode, accum,
  2098.                     build_int_2 (log, 0), 0, 0);
  2099.           opno++;
  2100.           if (alg.op[opno] == alg_add)
  2101.             accum = force_operand (gen_rtx (PLUS, mode, tem, accum),
  2102.                        tem);
  2103.           else
  2104.             accum = force_operand (gen_rtx (MINUS, mode, tem, accum),
  2105.                        tem);
  2106.         }
  2107.         }
  2108.  
  2109.       /* Write a REG_EQUAL note on the last insn so that we can cse 
  2110.          multiplication sequences.  We need not do this if we were
  2111.          multiplying by a power of two, since only one insn would have
  2112.          been generated.
  2113.  
  2114.          ??? We could also write REG_EQUAL notes on the last insn of
  2115.          each sequence that uses a single temporary, but it is not
  2116.          clear how to calculate the partial product so far.
  2117.  
  2118.          Torbjorn: Can you do this?  */
  2119.  
  2120.       if (exact_log2 (absval) < 0)
  2121.         {
  2122.           last = get_last_insn ();
  2123.           REG_NOTES (last)
  2124.         = gen_rtx (EXPR_LIST, REG_EQUAL,
  2125.                gen_rtx (MULT, mode, op0, 
  2126.                     negate ? gen_rtx (CONST_INT,
  2127.                               VOIDmode, absval)
  2128.                     : op1),
  2129.                REG_NOTES (last));
  2130.         }
  2131.  
  2132.       return (negate ? expand_unop (mode, neg_optab, accum, target, 0)
  2133.           : accum);
  2134.     }
  2135.     }
  2136.  
  2137.   /* This used to use umul_optab if unsigned,
  2138.      but I think that for non-widening multiply there is no difference
  2139.      between signed and unsigned.  */
  2140.   op0 = expand_binop (mode, smul_optab,
  2141.               op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
  2142.   if (op0 == 0)
  2143.     abort ();
  2144.   return op0;
  2145. }
  2146.  
  2147. /* Emit the code to divide OP0 by OP1, putting the result in TARGET
  2148.    if that is convenient, and returning where the result is.
  2149.    You may request either the quotient or the remainder as the result;
  2150.    specify REM_FLAG nonzero to get the remainder.
  2151.  
  2152.    CODE is the expression code for which kind of division this is;
  2153.    it controls how rounding is done.  MODE is the machine mode to use.
  2154.    UNSIGNEDP nonzero means do unsigned division.  */
  2155.  
  2156. /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
  2157.    and then correct it by or'ing in missing high bits
  2158.    if result of ANDI is nonzero.
  2159.    For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
  2160.    This could optimize to a bfexts instruction.
  2161.    But C doesn't use these operations, so their optimizations are
  2162.    left for later.  */
  2163.  
  2164. rtx
  2165. expand_divmod (rem_flag, code, mode, op0, op1, target, unsignedp)
  2166.      int rem_flag;
  2167.      enum tree_code code;
  2168.      enum machine_mode mode;
  2169.      register rtx op0, op1, target;
  2170.      int unsignedp;
  2171. {
  2172.   register rtx result = 0;
  2173.   enum machine_mode compute_mode;
  2174.   int log = -1;
  2175.   int can_clobber_op0;
  2176.   int mod_insn_no_good = 0;
  2177.   rtx adjusted_op0 = op0;
  2178.   optab optab1, optab2;
  2179.  
  2180.   /* Don't use the function value register as a target
  2181.      since we have to read it as well as write it,
  2182.      and function-inlining gets confused by this.  */
  2183.   if (target && REG_P (target) && REG_FUNCTION_VALUE_P (target))
  2184.     target = 0;
  2185.  
  2186.   /* Don't clobber an operand while doing a multi-step calculation.  */
  2187.   if (target)
  2188.     if ((rem_flag && (reg_mentioned_p (target, op0)
  2189.               || (GET_CODE (op0) == MEM && GET_CODE (target) == MEM)))
  2190.     || reg_mentioned_p (target, op1)
  2191.     || (GET_CODE (op1) == MEM && GET_CODE (target) == MEM))
  2192.       target = 0;
  2193.  
  2194.   can_clobber_op0 = (GET_CODE (op0) == REG && op0 == target);
  2195.  
  2196.   if (GET_CODE (op1) == CONST_INT)
  2197.     log = exact_log2 (INTVAL (op1));
  2198.  
  2199.   /* If log is >= 0, we are dividing by 2**log, and will do it by shifting,
  2200.      which is really floor-division.  Otherwise we will really do a divide,
  2201.      and we assume that is trunc-division.
  2202.  
  2203.      We must correct the dividend by adding or subtracting something
  2204.      based on the divisor, in order to do the kind of rounding specified
  2205.      by CODE.  The correction depends on what kind of rounding is actually
  2206.      available, and that depends on whether we will shift or divide.
  2207.  
  2208.      In many of these cases it is possible to perform the operation by a
  2209.      clever series of logical operations (shifts and/or exclusive-ors).
  2210.      Although avoiding the jump has the advantage that it extends the basic
  2211.      block and allows further optimization, the branch-free code is normally
  2212.      at least one instruction longer in the (most common) case where the
  2213.      dividend is non-negative.  Performance measurements of the two
  2214.      alternatives show that the branch-free code is slightly faster on the
  2215.      IBM ROMP but slower on CISC processors (significantly slower on the
  2216.      VAX).  Accordingly, the jump code has been retained.
  2217.  
  2218.      On machines where the jump code is slower, the cost of a DIV or MOD
  2219.      operation can be set small (less than twice that of an addition); in 
  2220.      that case, we pretend that we don't have a power of two and perform
  2221.      a normal division or modulus operation.  */
  2222.  
  2223.   if ((code == TRUNC_MOD_EXPR || code == TRUNC_DIV_EXPR)
  2224.       && ! unsignedp
  2225.       && (rem_flag ? smod_pow2_cheap : sdiv_pow2_cheap))
  2226.     log = -1;
  2227.  
  2228.   /* Get the mode in which to perform this computation.  Normally it will
  2229.      be MODE, but sometimes we can't do the desired operation in MODE.
  2230.      If so, pick a wider mode in which we can do the operation.  Convert
  2231.      to that mode at the start to avoid repeated conversions.
  2232.  
  2233.      First see what operations we need.  These depend on the expression
  2234.      we are evaluating.  (We assume that divxx3 insns exist under the
  2235.      same conditions that modxx3 insns and that these insns don't normally
  2236.      fail.  If these assumptions are not correct, we may generate less
  2237.      efficient code in some cases.)
  2238.  
  2239.      Then see if we find a mode in which we can open-code that operation
  2240.      (either a division, modulus, or shift).  Finally, check for the smallest
  2241.      mode for which we can do the operation with a library call.  */
  2242.  
  2243.   optab1 = (log >= 0 ? (unsignedp ? lshr_optab : ashr_optab)
  2244.         : (unsignedp ? udiv_optab : sdiv_optab));
  2245.   optab2 = (log >= 0 ? optab1 : (unsignedp ? udivmod_optab : sdivmod_optab));
  2246.  
  2247.   for (compute_mode = mode; compute_mode != VOIDmode;
  2248.        compute_mode = GET_MODE_WIDER_MODE (compute_mode))
  2249.     if (optab1->handlers[(int) compute_mode].insn_code != CODE_FOR_nothing
  2250.     || optab2->handlers[(int) compute_mode].insn_code != CODE_FOR_nothing)
  2251.       break;
  2252.  
  2253.   if (compute_mode == VOIDmode)
  2254.     for (compute_mode = mode; compute_mode != VOIDmode;
  2255.      compute_mode = GET_MODE_WIDER_MODE (compute_mode))
  2256.       if (optab1->handlers[(int) compute_mode].libfunc
  2257.       || optab2->handlers[(int) compute_mode].libfunc)
  2258.     break;
  2259.  
  2260.   /* If we still couldn't find a mode, use MODE; we'll probably abort in
  2261.      expand_binop.  */
  2262.   if (compute_mode == VOIDmode)
  2263.     compute_mode = mode;
  2264.  
  2265.   /* Now convert to the best mode to use.  Show we made a copy of OP0
  2266.      and hence we can clobber it (we cannot use a SUBREG to widen
  2267.      something.  */
  2268.   if (compute_mode != mode)
  2269.     {
  2270.       adjusted_op0 = op0 = convert_to_mode (compute_mode, op0, unsignedp);
  2271.       can_clobber_op0 = 1;
  2272.       op1 = convert_to_mode (compute_mode, op1, unsignedp);
  2273.     }
  2274.  
  2275.   /* If we are computing the remainder and one of the operands is a volatile
  2276.      MEM, copy it into a register.  */
  2277.  
  2278.   if (rem_flag && GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0))
  2279.     adjusted_op0 = op0 = force_reg (compute_mode, op0), can_clobber_op0 = 1;
  2280.   if (rem_flag && GET_CODE (op1) == MEM && MEM_VOLATILE_P (op1))
  2281.     op1 = force_reg (compute_mode, op1);
  2282.  
  2283.   /* If we are computing the remainder, op0 will be needed later to calculate
  2284.      X - Y * (X / Y), therefore cannot be clobbered. */
  2285.   if (rem_flag)
  2286.     can_clobber_op0 = 0;
  2287.  
  2288.   if (target == 0 || GET_MODE (target) != compute_mode)
  2289.     target = gen_reg_rtx (compute_mode);
  2290.  
  2291.   switch (code)
  2292.     {
  2293.     case TRUNC_MOD_EXPR:
  2294.     case TRUNC_DIV_EXPR:
  2295.       if (log >= 0 && ! unsignedp)
  2296.     {
  2297.       rtx label = gen_label_rtx ();
  2298.       if (! can_clobber_op0)
  2299.         {
  2300.           adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target,
  2301.                             compute_mode);
  2302.           /* Copy op0 to a reg, since emit_cmp_insn will call emit_queue
  2303.          which will screw up mem refs for autoincrements.  */
  2304.           op0 = force_reg (compute_mode, op0);
  2305.         }
  2306.       emit_cmp_insn (adjusted_op0, const0_rtx, GE, 0, compute_mode, 0, 0);
  2307.       emit_jump_insn (gen_bge (label));
  2308.       expand_inc (adjusted_op0, plus_constant (op1, -1));
  2309.       emit_label (label);
  2310.       mod_insn_no_good = 1;
  2311.     }
  2312.       break;
  2313.  
  2314.     case FLOOR_DIV_EXPR:
  2315.     case FLOOR_MOD_EXPR:
  2316.       if (log < 0 && ! unsignedp)
  2317.     {
  2318.       rtx label = gen_label_rtx ();
  2319.       if (! can_clobber_op0)
  2320.         {
  2321.           adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target,
  2322.                             compute_mode);
  2323.           /* Copy op0 to a reg, since emit_cmp_insn will call emit_queue
  2324.          which will screw up mem refs for autoincrements.  */
  2325.           op0 = force_reg (compute_mode, op0);
  2326.         }
  2327.       emit_cmp_insn (adjusted_op0, const0_rtx, GE, 0, compute_mode, 0, 0);
  2328.       emit_jump_insn (gen_bge (label));
  2329.       expand_dec (adjusted_op0, op1);
  2330.       expand_inc (adjusted_op0, const1_rtx);
  2331.       emit_label (label);
  2332.       mod_insn_no_good = 1;
  2333.     }
  2334.       break;
  2335.  
  2336.     case CEIL_DIV_EXPR:
  2337.     case CEIL_MOD_EXPR:
  2338.       if (! can_clobber_op0)
  2339.     {
  2340.       adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target,
  2341.                         compute_mode);
  2342.       /* Copy op0 to a reg, since emit_cmp_insn will call emit_queue
  2343.          which will screw up mem refs for autoincrements.  */
  2344.       op0 = force_reg (compute_mode, op0);
  2345.     }
  2346.       if (log < 0)
  2347.     {
  2348.       rtx label = 0;
  2349.       if (! unsignedp)
  2350.         {
  2351.           label = gen_label_rtx ();
  2352.           emit_cmp_insn (adjusted_op0, const0_rtx, LE, 0, compute_mode, 0, 0);
  2353.           emit_jump_insn (gen_ble (label));
  2354.         }
  2355.       expand_inc (adjusted_op0, op1);
  2356.       expand_dec (adjusted_op0, const1_rtx);
  2357.       if (! unsignedp)
  2358.         emit_label (label);
  2359.     }
  2360.       else
  2361.     {
  2362.       adjusted_op0 = expand_binop (compute_mode, add_optab,
  2363.                        adjusted_op0, plus_constant (op1, -1),
  2364.                        0, 0, OPTAB_LIB_WIDEN);
  2365.     }
  2366.       mod_insn_no_good = 1;
  2367.       break;
  2368.  
  2369.     case ROUND_DIV_EXPR:
  2370.     case ROUND_MOD_EXPR:
  2371.       if (! can_clobber_op0)
  2372.     {
  2373.       adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target,
  2374.                         compute_mode);
  2375.       /* Copy op0 to a reg, since emit_cmp_insn will call emit_queue
  2376.          which will screw up mem refs for autoincrements.  */
  2377.       op0 = force_reg (compute_mode, op0);
  2378.     }
  2379.       if (log < 0)
  2380.     {
  2381.       op1 = expand_shift (RSHIFT_EXPR, compute_mode, op1,
  2382.                   integer_one_node, 0, 0);
  2383.       if (! unsignedp)
  2384.         {
  2385.           rtx label = gen_label_rtx ();
  2386.           emit_cmp_insn (adjusted_op0, const0_rtx, GE, 0, compute_mode, 0, 0);
  2387.           emit_jump_insn (gen_bge (label));
  2388.           expand_unop (compute_mode, neg_optab, op1, op1, 0);
  2389.           emit_label (label);
  2390.         }
  2391.       expand_inc (adjusted_op0, op1);
  2392.     }
  2393.       else
  2394.     {
  2395.       op1 = gen_rtx (CONST_INT, VOIDmode, (1 << log) / 2);
  2396.       expand_inc (adjusted_op0, op1);
  2397.     }
  2398.       mod_insn_no_good = 1;
  2399.       break;
  2400.     }
  2401.  
  2402.   if (rem_flag && !mod_insn_no_good)
  2403.     {
  2404.       /* Try to produce the remainder directly */
  2405.       if (log >= 0)
  2406.     result = expand_binop (compute_mode, and_optab, adjusted_op0,
  2407.                    gen_rtx (CONST_INT, VOIDmode,
  2408.                     (1 << log) - 1),
  2409.                    target, 1, OPTAB_LIB_WIDEN);
  2410.       else
  2411.     {
  2412.       /* See if we can do remainder without a library call.  */
  2413.       result = sign_expand_binop (mode, umod_optab, smod_optab,
  2414.                       adjusted_op0, op1, target,
  2415.                       unsignedp, OPTAB_WIDEN);
  2416.       if (result == 0)
  2417.         {
  2418.           /* No luck there.  Can we do remainder and divide at once
  2419.          without a library call?  */
  2420.           result = gen_reg_rtx (compute_mode);
  2421.           if (! expand_twoval_binop (unsignedp
  2422.                      ? udivmod_optab : sdivmod_optab,
  2423.                      adjusted_op0, op1,
  2424.                      0, result, unsignedp))
  2425.         result = 0;
  2426.         }
  2427.     }
  2428.     }
  2429.  
  2430.   if (result)
  2431.     return gen_lowpart (mode, result);
  2432.  
  2433.   /* Produce the quotient.  */
  2434.   if (log >= 0)
  2435.     result = expand_shift (RSHIFT_EXPR, compute_mode, adjusted_op0,
  2436.                build_int_2 (log, 0), target, unsignedp);
  2437.   else if (rem_flag && !mod_insn_no_good)
  2438.     /* If producing quotient in order to subtract for remainder,
  2439.        and a remainder subroutine would be ok,
  2440.        don't use a divide subroutine.  */
  2441.     result = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
  2442.                 adjusted_op0, op1, 0, unsignedp, OPTAB_WIDEN);
  2443.   else
  2444.     {
  2445.       /* Try a quotient insn, but not a library call.  */
  2446.       result = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
  2447.                   adjusted_op0, op1, rem_flag ? 0 : target,
  2448.                   unsignedp, OPTAB_WIDEN);
  2449.       if (result == 0)
  2450.     {
  2451.       /* No luck there.  Try a quotient-and-remainder insn,
  2452.          keeping the quotient alone.  */
  2453.       result = gen_reg_rtx (mode);
  2454.       if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
  2455.                      adjusted_op0, op1,
  2456.                      result, 0, unsignedp))
  2457.         result = 0;
  2458.     }
  2459.  
  2460.       /* If still no luck, use a library call.  */
  2461.       if (result == 0)
  2462.     result = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
  2463.                     adjusted_op0, op1, rem_flag ? 0 : target,
  2464.                     unsignedp, OPTAB_LIB_WIDEN);
  2465.     }
  2466.  
  2467.   /* If we really want the remainder, get it by subtraction.  */
  2468.   if (rem_flag)
  2469.     {
  2470.       if (result == 0)
  2471.     /* No divide instruction either.  Use library for remainder.  */
  2472.     result = sign_expand_binop (compute_mode, umod_optab, smod_optab,
  2473.                     op0, op1, target,
  2474.                     unsignedp, OPTAB_LIB_WIDEN);
  2475.       else
  2476.     {
  2477.       /* We divided.  Now finish doing X - Y * (X / Y).  */
  2478.       result = expand_mult (compute_mode, result, op1, target, unsignedp);
  2479.       if (! result) abort ();
  2480.       result = expand_binop (compute_mode, sub_optab, op0,
  2481.                  result, target, unsignedp, OPTAB_LIB_WIDEN);
  2482.     }
  2483.     }
  2484.  
  2485.   if (result == 0)
  2486.     abort ();
  2487.  
  2488.   return gen_lowpart (mode, result);
  2489. }
  2490.  
  2491. /* Return a tree node with data type TYPE, describing the value of X.
  2492.    Usually this is an RTL_EXPR, if there is no obvious better choice.
  2493.    X may be an expression, however we only support those expressions
  2494.    generated by loop.c.   */
  2495.  
  2496. tree
  2497. make_tree (type, x)
  2498.      tree type;
  2499.      rtx x;
  2500. {
  2501.   tree t;
  2502.  
  2503.   switch (GET_CODE (x))
  2504.     {
  2505.     case CONST_INT:
  2506.       t = build_int_2 (INTVAL (x),
  2507.                ! TREE_UNSIGNED (type) && INTVAL (x) >= 0 ? 0 : -1);
  2508.       TREE_TYPE (t) = type;
  2509.       return t;
  2510.  
  2511.     case CONST_DOUBLE:
  2512.       if (GET_MODE (x) == VOIDmode)
  2513.     {
  2514.       t = build_int_2 (CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
  2515.       TREE_TYPE (t) = type;
  2516.     }
  2517.       else
  2518.     {
  2519.       REAL_VALUE_TYPE d;
  2520.  
  2521.       REAL_VALUE_FROM_CONST_DOUBLE (d, x);
  2522.       t = build_real (type, d);
  2523.     }
  2524.  
  2525.       return t;
  2526.       
  2527.     case PLUS:
  2528.       return fold (build (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
  2529.               make_tree (type, XEXP (x, 1))));
  2530.                                
  2531.     case MINUS:
  2532.       return fold (build (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
  2533.               make_tree (type, XEXP (x, 1))));
  2534.                                
  2535.     case NEG:
  2536.       return fold (build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0))));
  2537.  
  2538.     case MULT:
  2539.       return fold (build (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
  2540.               make_tree (type, XEXP (x, 1))));
  2541.                               
  2542.     case ASHIFT:
  2543.       return fold (build (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
  2544.               make_tree (type, XEXP (x, 1))));
  2545.                               
  2546.     case LSHIFTRT:
  2547.       return fold (convert (type,
  2548.                 build (RSHIFT_EXPR, unsigned_type (type),
  2549.                    make_tree (unsigned_type (type),
  2550.                           XEXP (x, 0)),
  2551.                    make_tree (type, XEXP (x, 1)))));
  2552.                               
  2553.     case ASHIFTRT:
  2554.       return fold (convert (type,
  2555.                 build (RSHIFT_EXPR, signed_type (type),
  2556.                    make_tree (signed_type (type), XEXP (x, 0)),
  2557.                    make_tree (type, XEXP (x, 1)))));
  2558.                               
  2559.     case DIV:
  2560.       if (TREE_CODE (type) != REAL_TYPE)
  2561.     t = signed_type (type);
  2562.       else
  2563.     t = type;
  2564.  
  2565.       return fold (convert (type,
  2566.                 build (TRUNC_DIV_EXPR, t,
  2567.                    make_tree (t, XEXP (x, 0)),
  2568.                    make_tree (t, XEXP (x, 1)))));
  2569.     case UDIV:
  2570.       t = unsigned_type (type);
  2571.       return fold (convert (type,
  2572.                 build (TRUNC_DIV_EXPR, t,
  2573.                    make_tree (t, XEXP (x, 0)),
  2574.                    make_tree (t, XEXP (x, 1)))));
  2575.    default:
  2576.       t = make_node (RTL_EXPR);
  2577.       TREE_TYPE (t) = type;
  2578.       RTL_EXPR_RTL (t) = x;
  2579.       /* There are no insns to be output
  2580.      when this rtl_expr is used.  */
  2581.       RTL_EXPR_SEQUENCE (t) = 0;
  2582.       return t;
  2583.     }
  2584. }
  2585.  
  2586. /* Return an rtx representing the value of X * MULT + ADD.
  2587.    TARGET is a suggestion for where to store the result (an rtx).
  2588.    MODE is the machine mode for the computation.
  2589.    X and MULT must have mode MODE.  ADD may have a different mode.
  2590.    So can X (defaults to same as MODE).
  2591.    UNSIGNEDP is non-zero to do unsigned multiplication.
  2592.    This may emit insns.  */
  2593.  
  2594. rtx
  2595. expand_mult_add (x, target, mult, add, mode, unsignedp)
  2596.      rtx x, target, mult, add;
  2597.      enum machine_mode mode;
  2598.      int unsignedp;
  2599. {
  2600.   tree type = type_for_mode (mode, unsignedp);
  2601.   tree add_type = (GET_MODE (add) == VOIDmode
  2602.            ? type : type_for_mode (GET_MODE (add), unsignedp));
  2603.   tree result =  fold (build (PLUS_EXPR, type,
  2604.                   fold (build (MULT_EXPR, type,
  2605.                        make_tree (type, x),
  2606.                        make_tree (type, mult))),
  2607.                   make_tree (add_type, add)));
  2608.  
  2609.   return expand_expr (result, target, VOIDmode, 0);
  2610. }
  2611.  
  2612. /* Compute the logical-and of OP0 and OP1, storing it in TARGET
  2613.    and returning TARGET.
  2614.  
  2615.    If TARGET is 0, a pseudo-register or constant is returned.  */
  2616.  
  2617. rtx
  2618. expand_and (op0, op1, target)
  2619.      rtx op0, op1, target;
  2620. {
  2621.   enum machine_mode mode = VOIDmode;
  2622.   rtx tem;
  2623.  
  2624.   if (GET_MODE (op0) != VOIDmode)
  2625.     mode = GET_MODE (op0);
  2626.   else if (GET_MODE (op1) != VOIDmode)
  2627.     mode = GET_MODE (op1);
  2628.  
  2629.   if (mode != VOIDmode)
  2630.     tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
  2631.   else if (GET_CODE (op0) == CONST_INT && GET_CODE (op1) == CONST_INT)
  2632.     tem = gen_rtx (CONST_INT, VOIDmode, INTVAL (op0) & INTVAL (op1));
  2633.   else
  2634.     abort ();
  2635.  
  2636.   if (target == 0)
  2637.     target = tem;
  2638.   else if (tem != target)
  2639.     emit_move_insn (target, tem);
  2640.   return target;
  2641. }
  2642.  
  2643. /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
  2644.    and storing in TARGET.  Normally return TARGET.
  2645.    Return 0 if that cannot be done.
  2646.  
  2647.    MODE is the mode to use for OP0 and OP1 should they be CONST_INTs.  If
  2648.    it is VOIDmode, they cannot both be CONST_INT.  
  2649.  
  2650.    UNSIGNEDP is for the case where we have to widen the operands
  2651.    to perform the operation.  It says to use zero-extension.
  2652.  
  2653.    NORMALIZEP is 1 if we should convert the result to be either zero
  2654.    or one one.  Normalize is -1 if we should convert the result to be
  2655.    either zero or -1.  If NORMALIZEP is zero, the result will be left
  2656.    "raw" out of the scc insn.  */
  2657.  
  2658. rtx
  2659. emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep)
  2660.      rtx target;
  2661.      enum rtx_code code;
  2662.      rtx op0, op1;
  2663.      enum machine_mode mode;
  2664.      int unsignedp;
  2665.      int normalizep;
  2666. {
  2667.   rtx subtarget;
  2668.   enum insn_code icode;
  2669.   enum machine_mode compare_mode;
  2670.   enum machine_mode target_mode = GET_MODE (target);
  2671.   rtx tem;
  2672.   rtx last = 0;
  2673.   rtx pattern, comparison;
  2674.  
  2675.   if (mode == VOIDmode)
  2676.     mode = GET_MODE (op0);
  2677.  
  2678.   /* For some comparisons with 1 and -1, we can convert this to 
  2679.      comparisons with zero.  This will often produce more opportunities for
  2680.      store-flag insns. */
  2681.  
  2682.   switch (code)
  2683.     {
  2684.     case LT:
  2685.       if (op1 == const1_rtx)
  2686.     op1 = const0_rtx, code = LE;
  2687.       break;
  2688.     case LE:
  2689.       if (op1 == constm1_rtx)
  2690.     op1 = const0_rtx, code = LT;
  2691.       break;
  2692.     case GE:
  2693.       if (op1 == const1_rtx)
  2694.     op1 = const0_rtx, code = GT;
  2695.       break;
  2696.     case GT:
  2697.       if (op1 == constm1_rtx)
  2698.     op1 = const0_rtx, code = GE;
  2699.       break;
  2700.     case GEU:
  2701.       if (op1 == const1_rtx)
  2702.     op1 = const0_rtx, code = NE;
  2703.       break;
  2704.     case LTU:
  2705.       if (op1 == const1_rtx)
  2706.     op1 = const0_rtx, code = EQ;
  2707.       break;
  2708.     }
  2709.  
  2710.   /* From now on, we won't change CODE, so set ICODE now.  */
  2711.   icode = setcc_gen_code[(int) code];
  2712.  
  2713.   /* If this is A < 0 or A >= 0, we can do this by taking the ones
  2714.      complement of A (for GE) and shifting the sign bit to the low bit.  */
  2715.   if (op1 == const0_rtx && (code == LT || code == GE)
  2716.       && GET_MODE_CLASS (mode) == MODE_INT
  2717.       && (normalizep || STORE_FLAG_VALUE == 1
  2718.       || (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_INT
  2719.           && STORE_FLAG_VALUE == 1 << (GET_MODE_BITSIZE (mode) - 1))))
  2720.     {
  2721.       rtx subtarget = target;
  2722.  
  2723.       /* If the result is to be wider than OP0, it is best to convert it
  2724.      first.  If it is to be narrower, it is *incorrect* to convert it
  2725.      first.  */
  2726.       if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
  2727.     {
  2728. #ifdef NeXT
  2729.       op0 = protect_from_queue (op0, 0);
  2730. #endif /* NeXT */
  2731.       op0 = convert_to_mode (target_mode, op0, 0);
  2732.       mode = target_mode;
  2733.     }
  2734.  
  2735.       if (target_mode != mode)
  2736.     subtarget = 0;
  2737.  
  2738.       if (code == GE)
  2739.     op0 = expand_unop (mode, one_cmpl_optab, op0, subtarget, 0);
  2740.  
  2741.       if (normalizep || STORE_FLAG_VALUE == 1)
  2742.     /* If we are supposed to produce a 0/1 value, we want to do
  2743.        a logical shift from the sign bit to the low-order bit; for
  2744.        a -1/0 value, we do an arithmetic shift.  */
  2745.     op0 = expand_shift (RSHIFT_EXPR, mode, op0,
  2746.                 size_int (GET_MODE_BITSIZE (mode) - 1),
  2747.                 subtarget, normalizep != -1);
  2748.  
  2749.       if (mode != target_mode)
  2750.     op0 = convert_to_mode (target_mode, op0, 0);
  2751.  
  2752.       return op0;
  2753.     }
  2754.  
  2755.   if (icode != CODE_FOR_nothing)
  2756.     {
  2757.       /* We think we may be able to do this with a scc insn.  Emit the
  2758.      comparison and then the scc insn.
  2759.  
  2760.      compare_from_rtx may call emit_queue, which would be deleted below
  2761.      if the scc insn fails.  So call it ourselves before setting LAST.  */
  2762.  
  2763.       emit_queue ();
  2764.       last = get_last_insn ();
  2765.  
  2766.       comparison = compare_from_rtx (op0, op1, code, unsignedp, mode, 0, 0);
  2767.       if (GET_CODE (comparison) == CONST_INT)
  2768.     return (comparison == const0_rtx ? const0_rtx
  2769.         : normalizep == 1 ? const1_rtx
  2770.         : normalizep == -1 ? constm1_rtx
  2771.         : const_true_rtx);
  2772.  
  2773.       /* Get a reference to the target in the proper mode for this insn.  */
  2774.       compare_mode = insn_operand_mode[(int) icode][0];
  2775.       subtarget = target;
  2776.       if (preserve_subexpressions_p ()
  2777.       || ! (*insn_operand_predicate[(int) icode][0]) (subtarget, compare_mode))
  2778.     subtarget = gen_reg_rtx (compare_mode);
  2779.  
  2780.       pattern = GEN_FCN (icode) (subtarget);
  2781.       if (pattern)
  2782.     {
  2783.       emit_insn (pattern);
  2784.  
  2785.       /* If we are converting to a wider mode, first convert to
  2786.          TARGET_MODE, then normalize.  This produces better combining
  2787.          opportunities on machines that have a SIGN_EXTRACT when we are
  2788.          testing a single bit.  This mostly benefits the 68k.
  2789.  
  2790.          If STORE_FLAG_VALUE does not have the sign bit set when
  2791.          interpreted in COMPARE_MODE, we can do this conversion as
  2792.          unsigned, which is usually more efficient.  */
  2793.       if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (compare_mode))
  2794.         {
  2795.           convert_move (target, subtarget,
  2796.                 (GET_MODE_BITSIZE (compare_mode)
  2797.                  <= HOST_BITS_PER_INT)
  2798.                 && 0 == (STORE_FLAG_VALUE
  2799.                      & (1 << (GET_MODE_BITSIZE (compare_mode) -1))));
  2800.           op0 = target;
  2801.           compare_mode = target_mode;
  2802.         }
  2803.       else
  2804.         op0 = subtarget;
  2805.  
  2806.       /* Now normalize to the proper value in COMPARE_MODE.  Sometimes
  2807.          we don't have to do anything.  */
  2808.       if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
  2809.         ;
  2810.       else if (normalizep == - STORE_FLAG_VALUE)
  2811.         op0 = expand_unop (compare_mode, neg_optab, op0, subtarget, 0);
  2812.  
  2813.       /* We don't want to use STORE_FLAG_VALUE < 0 below since this
  2814.          makes it hard to use a value of just the sign bit due to
  2815.          ANSI integer constant typing rules.  */
  2816.       else if (GET_MODE_BITSIZE (compare_mode) <= HOST_BITS_PER_INT
  2817.            && (STORE_FLAG_VALUE
  2818.                & (1 << (GET_MODE_BITSIZE (compare_mode) - 1))))
  2819.         op0 = expand_shift (RSHIFT_EXPR, compare_mode, op0,
  2820.                 size_int (GET_MODE_BITSIZE (compare_mode) - 1),
  2821.                 subtarget, normalizep == 1);
  2822.       else if (STORE_FLAG_VALUE & 1)
  2823.         {
  2824.           op0 = expand_and (op0, const1_rtx, subtarget);
  2825.           if (normalizep == -1)
  2826.         op0 = expand_unop (compare_mode, neg_optab, op0, op0, 0);
  2827.         }
  2828.       else
  2829.         abort ();
  2830.  
  2831.       /* If we were converting to a smaller mode, do the 
  2832.          conversion now.  */
  2833.       if (target_mode != compare_mode)
  2834.         {
  2835.           convert_move (target, op0);
  2836.           return target;
  2837.         }
  2838.       else
  2839.         return op0;
  2840.     }
  2841.     }
  2842.  
  2843.   if (last)
  2844.     delete_insns_since (last);
  2845.  
  2846.   subtarget = target_mode == mode ? target : 0;
  2847.  
  2848.   /* If we reached here, we can't do this with a scc insn.  However, there
  2849.      are some comparisons that can be done directly.  For example, if
  2850.      this is an equality comparison of integers, we can try to exclusive-or
  2851.      (or subtract) the two operands and use a recursive call to try the
  2852.      comparison with zero.  Don't do any of these cases if branches are
  2853.      very cheap.  */
  2854.  
  2855.   if (BRANCH_COST >= 0
  2856.       && GET_MODE_CLASS (mode) == MODE_INT && (code == EQ || code == NE)
  2857.       && op1 != const0_rtx)
  2858.     {
  2859.       tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
  2860.               OPTAB_WIDEN);
  2861.  
  2862.       if (tem == 0)
  2863.     tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
  2864.                 OPTAB_WIDEN);
  2865.       if (tem != 0)
  2866.     tem = emit_store_flag (target, code, tem, const0_rtx,
  2867.                    mode, unsignedp, normalizep);
  2868.       if (tem == 0)
  2869.     delete_insns_since (last);
  2870.       return tem;
  2871.     }
  2872.  
  2873.   /* Some other cases we can do are EQ, NE, LE, and GT comparisons with 
  2874.      the constant zero.  Reject all other comparisons at this point.  Only
  2875.      do LE and GT if branches are expensive since they are expensive on
  2876.      2-operand machines.  */
  2877.  
  2878.   if (BRANCH_COST == 0
  2879.       || GET_MODE_CLASS (mode) != MODE_INT || op1 != const0_rtx
  2880.       || (code != EQ && code != NE
  2881.       && (BRANCH_COST <= 1 || (code != LE && code != GT))))
  2882.     return 0;
  2883.  
  2884.   /* See what we need to return.  We can only return a 1, -1, or the
  2885.      sign bit.  */
  2886.  
  2887.   if (normalizep == 0)
  2888.     {
  2889.       if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
  2890.     normalizep = STORE_FLAG_VALUE;
  2891.  
  2892.       else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_INT
  2893.            && STORE_FLAG_VALUE == 1 << (GET_MODE_BITSIZE (mode) - 1))
  2894.     ;
  2895.       else
  2896.     return 0;
  2897.     }
  2898.  
  2899.   /* Try to put the result of the comparison in the sign bit.  Assume we can't
  2900.      do the necessary operation below.  */
  2901.  
  2902.   tem = 0;
  2903.  
  2904.   /* To see if A <= 0, compute (A | (A - 1)).  A <= 0 iff that result has
  2905.      the sign bit set.  */
  2906.  
  2907.   if (code == LE)
  2908.     {
  2909.       /* This is destructive, so SUBTARGET can't be OP0.  */
  2910.       if (rtx_equal_p (subtarget, op0))
  2911.     subtarget = 0;
  2912.  
  2913.       tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
  2914.               OPTAB_WIDEN);
  2915.       if (tem)
  2916.     tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
  2917.                 OPTAB_WIDEN);
  2918.     }
  2919.  
  2920.   /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
  2921.      number of bits in the mode of OP0, minus one.  */
  2922.  
  2923.   if (code == GT)
  2924.     {
  2925.       if (rtx_equal_p (subtarget, op0))
  2926.     subtarget = 0;
  2927.  
  2928.       tem = expand_shift (RSHIFT_EXPR, mode, op0,
  2929.               size_int (GET_MODE_BITSIZE (mode) - 1),
  2930.               subtarget, 0);
  2931.       tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
  2932.               OPTAB_WIDEN);
  2933.     }
  2934.                     
  2935.   if (code == EQ || code == NE)
  2936.     {
  2937.       /* For EQ or NE, one way to do the comparison is to apply an operation
  2938.      that converts the operand into a positive number if it is non-zero
  2939.      or zero if it was originally zero.  Then, for EQ, we subtract 1 and
  2940.      for NE we negate.  This puts the result in the sign bit.  Then we
  2941.      normalize with a shift, if needed. 
  2942.  
  2943.      Two operations that can do the above actions are ABS and FFS, so try
  2944.      them.  If that doesn't work, and MODE is smaller than a full word,
  2945.      we can use zero-extension to the wider mode (an unsigned conversion)
  2946.      as the operation.  */
  2947.  
  2948.       if (abs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
  2949.     tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
  2950.       else if (ffs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
  2951.     tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
  2952.       else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
  2953.     {
  2954.       mode = word_mode;
  2955. #ifdef NeXNeXTNOTT
  2956.       op0 = protect_from_queue (op0, 0);
  2957. #endif /* NeXT */
  2958.       tem = convert_to_mode (mode, op0, 1);
  2959.     }
  2960.  
  2961.       if (tem != 0)
  2962.     {
  2963.       if (code == EQ)
  2964.         tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
  2965.                 0, OPTAB_WIDEN);
  2966.       else
  2967.         tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
  2968.     }
  2969.  
  2970.       /* If we couldn't do it that way, for NE we can "or" the two's complement
  2971.      of the value with itself.  For EQ, we take the one's complement of
  2972.      that "or", which is an extra insn, so we only handle EQ if branches
  2973.      are expensive.  */
  2974.  
  2975.       if (tem == 0 && (code == NE || BRANCH_COST > 1))
  2976.     {
  2977.       if (rtx_equal_p (subtarget, op0))
  2978.         subtarget = 0;
  2979.  
  2980.       tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
  2981.       tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
  2982.                   OPTAB_WIDEN);
  2983.  
  2984.       if (tem && code == EQ)
  2985.         tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
  2986.     }
  2987.     }
  2988.  
  2989.   if (tem && normalizep)
  2990.     tem = expand_shift (RSHIFT_EXPR, mode, tem,
  2991.             size_int (GET_MODE_BITSIZE (mode) - 1),
  2992.             tem, normalizep == 1);
  2993.  
  2994.   if (tem && GET_MODE (tem) != target_mode)
  2995.     {
  2996.       convert_move (target, tem, 0);
  2997.       tem = target;
  2998.     }
  2999.  
  3000.   if (tem == 0)
  3001.     delete_insns_since (last);
  3002.  
  3003.   return tem;
  3004. }
  3005.