]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/expmed.c
Optimize ODR enum streaming
[thirdparty/gcc.git] / gcc / expmed.c
index 3839c7622430b22a2ec7eee424c56b0452a39059..e7c03fbf92c04b46d7697aad96ad2fa14be4e8e1 100644 (file)
@@ -1,6 +1,6 @@
 /* Medium-level subroutines: convert bit-field store and extract
    and shifts, multiplies and divides to rtl instructions.
-   Copyright (C) 1987-2017 Free Software Foundation, Inc.
+   Copyright (C) 1987-2020 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -18,6 +18,8 @@ You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+/* Work around tree-optimization/91825.  */
+#pragma GCC diagnostic warning "-Wmaybe-uninitialized"
 
 #include "config.h"
 #include "system.h"
@@ -31,6 +33,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "tm_p.h"
 #include "expmed.h"
 #include "optabs.h"
+#include "regs.h"
 #include "emit-rtl.h"
 #include "diagnostic-core.h"
 #include "fold-const.h"
@@ -39,17 +42,22 @@ along with GCC; see the file COPYING3.  If not see
 #include "explow.h"
 #include "expr.h"
 #include "langhooks.h"
+#include "tree-vector-builder.h"
 
 struct target_expmed default_target_expmed;
 #if SWITCHABLE_TARGET
 struct target_expmed *this_target_expmed = &default_target_expmed;
 #endif
 
+static bool store_integral_bit_field (rtx, opt_scalar_int_mode,
+                                     unsigned HOST_WIDE_INT,
+                                     unsigned HOST_WIDE_INT,
+                                     poly_uint64, poly_uint64,
+                                     machine_mode, rtx, bool, bool);
 static void store_fixed_bit_field (rtx, opt_scalar_int_mode,
                                   unsigned HOST_WIDE_INT,
                                   unsigned HOST_WIDE_INT,
-                                  unsigned HOST_WIDE_INT,
-                                  unsigned HOST_WIDE_INT,
+                                  poly_uint64, poly_uint64,
                                   rtx, scalar_int_mode, bool);
 static void store_fixed_bit_field_1 (rtx, scalar_int_mode,
                                     unsigned HOST_WIDE_INT,
@@ -58,9 +66,12 @@ static void store_fixed_bit_field_1 (rtx, scalar_int_mode,
 static void store_split_bit_field (rtx, opt_scalar_int_mode,
                                   unsigned HOST_WIDE_INT,
                                   unsigned HOST_WIDE_INT,
-                                  unsigned HOST_WIDE_INT,
-                                  unsigned HOST_WIDE_INT,
+                                  poly_uint64, poly_uint64,
                                   rtx, scalar_int_mode, bool);
+static rtx extract_integral_bit_field (rtx, opt_scalar_int_mode,
+                                      unsigned HOST_WIDE_INT,
+                                      unsigned HOST_WIDE_INT, int, rtx,
+                                      machine_mode, machine_mode, bool, bool);
 static rtx extract_fixed_bit_field (machine_mode, rtx, opt_scalar_int_mode,
                                    unsigned HOST_WIDE_INT,
                                    unsigned HOST_WIDE_INT, rtx, int, bool);
@@ -72,8 +83,8 @@ static rtx extract_split_bit_field (rtx, opt_scalar_int_mode,
                                    unsigned HOST_WIDE_INT,
                                    unsigned HOST_WIDE_INT, int, bool);
 static void do_cmp_and_jump (rtx, rtx, enum rtx_code, machine_mode, rtx_code_label *);
-static rtx expand_smod_pow2 (machine_mode, rtx, HOST_WIDE_INT);
-static rtx expand_sdiv_pow2 (machine_mode, rtx, HOST_WIDE_INT);
+static rtx expand_smod_pow2 (scalar_int_mode, rtx, HOST_WIDE_INT);
+static rtx expand_sdiv_pow2 (scalar_int_mode, rtx, HOST_WIDE_INT);
 
 /* Return a constant integer mask value of mode MODE with BITSIZE ones
    followed by BITPOS zeros, or the complement of that if COMPLEMENT.
@@ -81,7 +92,7 @@ static rtx expand_sdiv_pow2 (machine_mode, rtx, HOST_WIDE_INT);
    mask is zero-extended if BITSIZE+BITPOS is too small for MODE.  */
 
 static inline rtx
-mask_rtx (machine_mode mode, int bitpos, int bitsize, bool complement)
+mask_rtx (scalar_int_mode mode, int bitpos, int bitsize, bool complement)
 {
   return immed_wide_int_const
     (wi::shifted_mask (bitpos, bitsize, complement,
@@ -118,8 +129,8 @@ struct init_expmed_rtl
 };
 
 static void
-init_expmed_one_conv (struct init_expmed_rtl *all, machine_mode to_mode,
-                     machine_mode from_mode, bool speed)
+init_expmed_one_conv (struct init_expmed_rtl *all, scalar_int_mode to_mode,
+                     scalar_int_mode from_mode, bool speed)
 {
   int to_size, from_size;
   rtx which;
@@ -221,7 +232,8 @@ init_expmed_one_mode (struct init_expmed_rtl *all,
          PUT_MODE (all->zext, wider_mode);
          PUT_MODE (all->wide_mult, wider_mode);
          PUT_MODE (all->wide_lshr, wider_mode);
-         XEXP (all->wide_lshr, 1) = GEN_INT (mode_bitsize);
+         XEXP (all->wide_lshr, 1)
+           = gen_int_shift_amount (wider_mode, mode_bitsize);
 
          set_mul_widen_cost (speed, wider_mode,
                              set_src_cost (all->wide_mult, wider_mode, speed));
@@ -459,8 +471,8 @@ static rtx
 adjust_bit_field_mem_for_reg (enum extraction_pattern pattern,
                              rtx op0, HOST_WIDE_INT bitsize,
                              HOST_WIDE_INT bitnum,
-                             unsigned HOST_WIDE_INT bitregion_start,
-                             unsigned HOST_WIDE_INT bitregion_end,
+                             poly_uint64 bitregion_start,
+                             poly_uint64 bitregion_end,
                              machine_mode fieldmode,
                              unsigned HOST_WIDE_INT *new_bitnum)
 {
@@ -478,7 +490,7 @@ adjust_bit_field_mem_for_reg (enum extraction_pattern pattern,
        {
          /* Limit the search to the mode required by the corresponding
             register insertion or extraction instruction, if any.  */
-         machine_mode limit_mode = word_mode;
+         scalar_int_mode limit_mode = word_mode;
          extraction_insn insn;
          if (get_best_reg_extraction_insn (&insn, pattern,
                                            GET_MODE_BITSIZE (best_mode),
@@ -501,16 +513,17 @@ adjust_bit_field_mem_for_reg (enum extraction_pattern pattern,
    offset is then BITNUM / BITS_PER_UNIT.  */
 
 static bool
-lowpart_bit_field_p (unsigned HOST_WIDE_INT bitnum,
-                    unsigned HOST_WIDE_INT bitsize,
+lowpart_bit_field_p (poly_uint64 bitnum, poly_uint64 bitsize,
                     machine_mode struct_mode)
 {
+  poly_uint64 regsize = REGMODE_NATURAL_SIZE (struct_mode);
   if (BYTES_BIG_ENDIAN)
-    return (bitnum % BITS_PER_UNIT == 0
-           && (bitnum + bitsize == GET_MODE_BITSIZE (struct_mode)
-               || (bitnum + bitsize) % BITS_PER_WORD == 0));
+    return (multiple_p (bitnum, BITS_PER_UNIT)
+           && (known_eq (bitnum + bitsize, GET_MODE_BITSIZE (struct_mode))
+               || multiple_p (bitnum + bitsize,
+                              regsize * BITS_PER_UNIT)));
   else
-    return bitnum % BITS_PER_WORD == 0;
+    return multiple_p (bitnum, regsize * BITS_PER_UNIT);
 }
 
 /* Return true if -fstrict-volatile-bitfields applies to an access of OP0
@@ -523,8 +536,8 @@ static bool
 strict_volatile_bitfield_p (rtx op0, unsigned HOST_WIDE_INT bitsize,
                            unsigned HOST_WIDE_INT bitnum,
                            scalar_int_mode fieldmode,
-                           unsigned HOST_WIDE_INT bitregion_start,
-                           unsigned HOST_WIDE_INT bitregion_end)
+                           poly_uint64 bitregion_start,
+                           poly_uint64 bitregion_end)
 {
   unsigned HOST_WIDE_INT modesize = GET_MODE_BITSIZE (fieldmode);
 
@@ -551,26 +564,28 @@ strict_volatile_bitfield_p (rtx op0, unsigned HOST_WIDE_INT bitsize,
     return false;
 
   /* Check for cases where the C++ memory model applies.  */
-  if (bitregion_end != 0
-      && (bitnum - bitnum % modesize < bitregion_start
-         || bitnum - bitnum % modesize + modesize - 1 > bitregion_end))
+  if (maybe_ne (bitregion_end, 0U)
+      && (maybe_lt (bitnum - bitnum % modesize, bitregion_start)
+         || maybe_gt (bitnum - bitnum % modesize + modesize - 1,
+                      bitregion_end)))
     return false;
 
   return true;
 }
 
 /* Return true if OP is a memory and if a bitfield of size BITSIZE at
-   bit number BITNUM can be treated as a simple value of mode MODE.  */
+   bit number BITNUM can be treated as a simple value of mode MODE.
+   Store the byte offset in *BYTENUM if so.  */
 
 static bool
-simple_mem_bitfield_p (rtx op0, unsigned HOST_WIDE_INT bitsize,
-                      unsigned HOST_WIDE_INT bitnum, machine_mode mode)
+simple_mem_bitfield_p (rtx op0, poly_uint64 bitsize, poly_uint64 bitnum,
+                      machine_mode mode, poly_uint64 *bytenum)
 {
   return (MEM_P (op0)
-         && bitnum % BITS_PER_UNIT == 0
-         && bitsize == GET_MODE_BITSIZE (mode)
-         && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (op0))
-             || (bitnum % GET_MODE_ALIGNMENT (mode) == 0
+         && multiple_p (bitnum, BITS_PER_UNIT, bytenum)
+         && known_eq (bitsize, GET_MODE_BITSIZE (mode))
+         && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (op0))
+             || (multiple_p (bitnum, GET_MODE_ALIGNMENT (mode))
                  && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode))));
 }
 \f
@@ -586,7 +601,7 @@ store_bit_field_using_insv (const extraction_insn *insv, rtx op0,
                            unsigned HOST_WIDE_INT bitnum,
                            rtx value, scalar_int_mode value_mode)
 {
-  struct expand_operand ops[4];
+  class expand_operand ops[4];
   rtx value1;
   rtx xop0 = op0;
   rtx_insn *last = get_last_insn ();
@@ -715,64 +730,39 @@ store_bit_field_using_insv (const extraction_insn *insv, rtx op0,
    return false instead.  */
 
 static bool
-store_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
-                  unsigned HOST_WIDE_INT bitnum,
-                  unsigned HOST_WIDE_INT bitregion_start,
-                  unsigned HOST_WIDE_INT bitregion_end,
+store_bit_field_1 (rtx str_rtx, poly_uint64 bitsize, poly_uint64 bitnum,
+                  poly_uint64 bitregion_start, poly_uint64 bitregion_end,
                   machine_mode fieldmode,
                   rtx value, bool reverse, bool fallback_p)
 {
   rtx op0 = str_rtx;
-  rtx orig_value;
 
   while (GET_CODE (op0) == SUBREG)
     {
-      /* The following line once was done only if WORDS_BIG_ENDIAN,
-        but I think that is a mistake.  WORDS_BIG_ENDIAN is
-        meaningful at a much higher level; when structures are copied
-        between memory and regs, the higher-numbered regs
-        always get higher addresses.  */
-      int inner_mode_size = GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)));
-      int outer_mode_size = GET_MODE_SIZE (GET_MODE (op0));
-      int byte_offset = 0;
-
-      /* Paradoxical subregs need special handling on big-endian machines.  */
-      if (paradoxical_subreg_p (op0))
-       {
-         int difference = inner_mode_size - outer_mode_size;
-
-         if (WORDS_BIG_ENDIAN)
-           byte_offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
-         if (BYTES_BIG_ENDIAN)
-           byte_offset += difference % UNITS_PER_WORD;
-       }
-      else
-       byte_offset = SUBREG_BYTE (op0);
-
-      bitnum += byte_offset * BITS_PER_UNIT;
+      bitnum += subreg_memory_offset (op0) * BITS_PER_UNIT;
       op0 = SUBREG_REG (op0);
     }
 
   /* No action is needed if the target is a register and if the field
      lies completely outside that register.  This can occur if the source
      code contains an out-of-bounds access to a small array.  */
-  if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
+  if (REG_P (op0) && known_ge (bitnum, GET_MODE_BITSIZE (GET_MODE (op0))))
     return true;
 
   /* Use vec_set patterns for inserting parts of vectors whenever
      available.  */
-  if (VECTOR_MODE_P (GET_MODE (op0))
+  machine_mode outermode = GET_MODE (op0);
+  scalar_mode innermode = GET_MODE_INNER (outermode);
+  poly_uint64 pos;
+  if (VECTOR_MODE_P (outermode)
       && !MEM_P (op0)
-      && optab_handler (vec_set_optab, GET_MODE (op0)) != CODE_FOR_nothing
-      && fieldmode == GET_MODE_INNER (GET_MODE (op0))
-      && bitsize == GET_MODE_UNIT_BITSIZE (GET_MODE (op0))
-      && !(bitnum % GET_MODE_UNIT_BITSIZE (GET_MODE (op0))))
-    {
-      struct expand_operand ops[3];
-      machine_mode outermode = GET_MODE (op0);
-      machine_mode innermode = GET_MODE_INNER (outermode);
+      && optab_handler (vec_set_optab, outermode) != CODE_FOR_nothing
+      && fieldmode == innermode
+      && known_eq (bitsize, GET_MODE_BITSIZE (innermode))
+      && multiple_p (bitnum, GET_MODE_BITSIZE (innermode), &pos))
+    {
+      class expand_operand ops[3];
       enum insn_code icode = optab_handler (vec_set_optab, outermode);
-      int pos = bitnum / GET_MODE_BITSIZE (innermode);
 
       create_fixed_operand (&ops[0], op0);
       create_input_operand (&ops[1], value, innermode);
@@ -784,16 +774,16 @@ store_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
   /* If the target is a register, overwriting the entire object, or storing
      a full-word or multi-word field can be done with just a SUBREG.  */
   if (!MEM_P (op0)
-      && bitsize == GET_MODE_BITSIZE (fieldmode)
-      && ((bitsize == GET_MODE_BITSIZE (GET_MODE (op0)) && bitnum == 0)
-         || (bitsize % BITS_PER_WORD == 0 && bitnum % BITS_PER_WORD == 0)))
+      && known_eq (bitsize, GET_MODE_BITSIZE (fieldmode)))
     {
       /* Use the subreg machinery either to narrow OP0 to the required
         words or to cope with mode punning between equal-sized modes.
         In the latter case, use subreg on the rhs side, not lhs.  */
       rtx sub;
-
-      if (bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
+      HOST_WIDE_INT regnum;
+      poly_uint64 regsize = REGMODE_NATURAL_SIZE (GET_MODE (op0));
+      if (known_eq (bitnum, 0U)
+         && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0))))
        {
          sub = simplify_gen_subreg (GET_MODE (op0), value, fieldmode, 0);
          if (sub)
@@ -804,10 +794,11 @@ store_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
              return true;
            }
        }
-      else
+      else if (constant_multiple_p (bitnum, regsize * BITS_PER_UNIT, &regnum)
+              && multiple_p (bitsize, regsize * BITS_PER_UNIT))
        {
          sub = simplify_gen_subreg (fieldmode, op0, GET_MODE (op0),
-                                    bitnum / BITS_PER_UNIT);
+                                    regnum * regsize);
          if (sub)
            {
              if (reverse)
@@ -821,15 +812,23 @@ store_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
   /* If the target is memory, storing any naturally aligned field can be
      done with a simple store.  For targets that support fast unaligned
      memory, any naturally sized, unit aligned field can be done directly.  */
-  if (simple_mem_bitfield_p (op0, bitsize, bitnum, fieldmode))
+  poly_uint64 bytenum;
+  if (simple_mem_bitfield_p (op0, bitsize, bitnum, fieldmode, &bytenum))
     {
-      op0 = adjust_bitfield_address (op0, fieldmode, bitnum / BITS_PER_UNIT);
+      op0 = adjust_bitfield_address (op0, fieldmode, bytenum);
       if (reverse)
        value = flip_storage_order (fieldmode, value);
       emit_move_insn (op0, value);
       return true;
     }
 
+  /* It's possible we'll need to handle other cases here for
+     polynomial bitnum and bitsize.  */
+
+  /* From here on we need to be looking at a fixed-size insertion.  */
+  unsigned HOST_WIDE_INT ibitsize = bitsize.to_constant ();
+  unsigned HOST_WIDE_INT ibitnum = bitnum.to_constant ();
+
   /* Make sure we are playing with integral modes.  Pun with subregs
      if we aren't.  This must come after the entire register case above,
      since that case is valid for any mode.  The following cases are only
@@ -841,20 +840,60 @@ store_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
       if (MEM_P (op0))
        op0 = adjust_bitfield_address_size (op0, op0_mode.else_blk (),
                                            0, MEM_SIZE (op0));
+      else if (!op0_mode.exists ())
+       {
+         if (ibitnum == 0
+             && known_eq (ibitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
+             && MEM_P (value)
+             && !reverse)
+           {
+             value = adjust_address (value, GET_MODE (op0), 0);
+             emit_move_insn (op0, value);
+             return true;
+           }
+         if (!fallback_p)
+           return false;
+         rtx temp = assign_stack_temp (GET_MODE (op0),
+                                       GET_MODE_SIZE (GET_MODE (op0)));
+         emit_move_insn (temp, op0);
+         store_bit_field_1 (temp, bitsize, bitnum, 0, 0, fieldmode, value,
+                            reverse, fallback_p);
+         emit_move_insn (op0, temp);
+         return true;
+       }
       else
        op0 = gen_lowpart (op0_mode.require (), op0);
     }
 
+  return store_integral_bit_field (op0, op0_mode, ibitsize, ibitnum,
+                                  bitregion_start, bitregion_end,
+                                  fieldmode, value, reverse, fallback_p);
+}
+
+/* Subroutine of store_bit_field_1, with the same arguments, except
+   that BITSIZE and BITNUM are constant.  Handle cases specific to
+   integral modes.  If OP0_MODE is defined, it is the mode of OP0,
+   otherwise OP0 is a BLKmode MEM.  */
+
+static bool
+store_integral_bit_field (rtx op0, opt_scalar_int_mode op0_mode,
+                         unsigned HOST_WIDE_INT bitsize,
+                         unsigned HOST_WIDE_INT bitnum,
+                         poly_uint64 bitregion_start,
+                         poly_uint64 bitregion_end,
+                         machine_mode fieldmode,
+                         rtx value, bool reverse, bool fallback_p)
+{
   /* Storing an lsb-aligned field in a register
      can be done with a movstrict instruction.  */
 
   if (!MEM_P (op0)
       && !reverse
-      && lowpart_bit_field_p (bitnum, bitsize, GET_MODE (op0))
-      && bitsize == GET_MODE_BITSIZE (fieldmode)
+      && lowpart_bit_field_p (bitnum, bitsize, op0_mode.require ())
+      && known_eq (bitsize, GET_MODE_BITSIZE (fieldmode))
       && optab_handler (movstrict_optab, fieldmode) != CODE_FOR_nothing)
     {
-      struct expand_operand ops[2];
+      class expand_operand ops[2];
       enum insn_code icode = optab_handler (movstrict_optab, fieldmode);
       rtx arg0 = op0;
       unsigned HOST_WIDE_INT subreg_off;
@@ -894,47 +933,47 @@ store_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
         However, only do that if the value is not BLKmode.  */
 
       const bool backwards = WORDS_BIG_ENDIAN && fieldmode != BLKmode;
-      unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
-      unsigned int i;
+      const int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
       rtx_insn *last;
 
       /* This is the mode we must force value to, so that there will be enough
         subwords to extract.  Note that fieldmode will often (always?) be
         VOIDmode, because that is what store_field uses to indicate that this
         is a bit field, but passing VOIDmode to operand_subword_force
-        is not allowed.  */
-      fieldmode = GET_MODE (value);
-      if (fieldmode == VOIDmode)
-       fieldmode = smallest_int_mode_for_size (nwords * BITS_PER_WORD);
+        is not allowed.
+
+        The mode must be fixed-size, since insertions into variable-sized
+        objects are meant to be handled before calling this function.  */
+      fixed_size_mode value_mode = as_a <fixed_size_mode> (GET_MODE (value));
+      if (value_mode == VOIDmode)
+       value_mode = smallest_int_mode_for_size (nwords * BITS_PER_WORD);
 
       last = get_last_insn ();
-      for (i = 0; i < nwords; i++)
+      for (int i = 0; i < nwords; i++)
        {
-         /* If I is 0, use the low-order word in both field and target;
-            if I is 1, use the next to lowest word; and so on.  */
-         unsigned int wordnum = (backwards
-                                 ? GET_MODE_SIZE (fieldmode) / UNITS_PER_WORD
-                                 - i - 1
-                                 : i);
-         unsigned int bit_offset = (backwards ^ reverse
-                                    ? MAX ((int) bitsize - ((int) i + 1)
-                                           * BITS_PER_WORD,
-                                           0)
-                                    : (int) i * BITS_PER_WORD);
-         rtx value_word = operand_subword_force (value, wordnum, fieldmode);
-         unsigned HOST_WIDE_INT new_bitsize =
-           MIN (BITS_PER_WORD, bitsize - i * BITS_PER_WORD);
-
-         /* If the remaining chunk doesn't have full wordsize we have
-            to make sure that for big-endian machines the higher order
-            bits are used.  */
-         if (new_bitsize < BITS_PER_WORD && BYTES_BIG_ENDIAN && !backwards)
-           value_word = simplify_expand_binop (word_mode, lshr_optab,
-                                               value_word,
-                                               GEN_INT (BITS_PER_WORD
-                                                        - new_bitsize),
-                                               NULL_RTX, true,
-                                               OPTAB_LIB_WIDEN);
+         /* Number of bits to be stored in this iteration, i.e. BITS_PER_WORD
+            except maybe for the last iteration.  */
+         const unsigned HOST_WIDE_INT new_bitsize
+           = MIN (BITS_PER_WORD, bitsize - i * BITS_PER_WORD);
+         /* Bit offset from the starting bit number in the target.  */
+         const unsigned int bit_offset
+           = backwards ^ reverse
+             ? MAX ((int) bitsize - (i + 1) * BITS_PER_WORD, 0)
+             : i * BITS_PER_WORD;
+         /* Starting word number in the value.  */
+         const unsigned int wordnum
+           = backwards
+             ? GET_MODE_SIZE (value_mode) / UNITS_PER_WORD - (i + 1)
+             : i;
+         /* The chunk of the value in word_mode.  We use bit-field extraction
+             in BLKmode to handle unaligned memory references and to shift the
+             last chunk right on big-endian machines if need be.  */
+         rtx value_word
+           = fieldmode == BLKmode
+             ? extract_bit_field (value, new_bitsize, wordnum * BITS_PER_WORD,
+                                  1, NULL_RTX, word_mode, word_mode, false,
+                                  NULL)
+             : operand_subword_force (value, wordnum, value_mode);
 
          if (!store_bit_field_1 (op0, new_bitsize,
                                  bitnum + bit_offset,
@@ -953,7 +992,7 @@ store_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
      integer of the corresponding size.  This can occur on a machine
      with 64 bit registers that uses SFmode for float.  It can also
      occur for unaligned float or complex fields.  */
-  orig_value = value;
+  rtx orig_value = value;
   scalar_int_mode value_mode;
   if (GET_MODE (value) == VOIDmode)
     /* By this point we've dealt with values that are bigger than a word,
@@ -974,7 +1013,7 @@ store_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
       && GET_MODE_SIZE (op0_mode.require ()) > UNITS_PER_WORD
       && (!REG_P (op0)
          || !HARD_REGISTER_P (op0)
-         || HARD_REGNO_NREGS (REGNO (op0), op0_mode.require ()) != 1))
+         || hard_regno_nregs (REGNO (op0), op0_mode.require ()) != 1))
     {
       if (bitnum % BITS_PER_WORD + bitsize > BITS_PER_WORD)
        {
@@ -1061,41 +1100,42 @@ store_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
    If REVERSE is true, the store is to be done in reverse order.  */
 
 void
-store_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
-                unsigned HOST_WIDE_INT bitnum,
-                unsigned HOST_WIDE_INT bitregion_start,
-                unsigned HOST_WIDE_INT bitregion_end,
+store_bit_field (rtx str_rtx, poly_uint64 bitsize, poly_uint64 bitnum,
+                poly_uint64 bitregion_start, poly_uint64 bitregion_end,
                 machine_mode fieldmode,
                 rtx value, bool reverse)
 {
   /* Handle -fstrict-volatile-bitfields in the cases where it applies.  */
+  unsigned HOST_WIDE_INT ibitsize = 0, ibitnum = 0;
   scalar_int_mode int_mode;
-  if (is_a <scalar_int_mode> (fieldmode, &int_mode)
-      && strict_volatile_bitfield_p (str_rtx, bitsize, bitnum, int_mode,
+  if (bitsize.is_constant (&ibitsize)
+      && bitnum.is_constant (&ibitnum)
+      && is_a <scalar_int_mode> (fieldmode, &int_mode)
+      && strict_volatile_bitfield_p (str_rtx, ibitsize, ibitnum, int_mode,
                                     bitregion_start, bitregion_end))
     {
       /* Storing of a full word can be done with a simple store.
         We know here that the field can be accessed with one single
         instruction.  For targets that support unaligned memory,
         an unaligned access may be necessary.  */
-      if (bitsize == GET_MODE_BITSIZE (int_mode))
+      if (ibitsize == GET_MODE_BITSIZE (int_mode))
        {
          str_rtx = adjust_bitfield_address (str_rtx, int_mode,
-                                            bitnum / BITS_PER_UNIT);
+                                            ibitnum / BITS_PER_UNIT);
          if (reverse)
            value = flip_storage_order (int_mode, value);
-         gcc_assert (bitnum % BITS_PER_UNIT == 0);
+         gcc_assert (ibitnum % BITS_PER_UNIT == 0);
          emit_move_insn (str_rtx, value);
        }
       else
        {
          rtx temp;
 
-         str_rtx = narrow_bit_field_mem (str_rtx, int_mode, bitsize, bitnum,
-                                         &bitnum);
-         gcc_assert (bitnum + bitsize <= GET_MODE_BITSIZE (int_mode));
+         str_rtx = narrow_bit_field_mem (str_rtx, int_mode, ibitsize,
+                                         ibitnum, &ibitnum);
+         gcc_assert (ibitnum + ibitsize <= GET_MODE_BITSIZE (int_mode));
          temp = copy_to_reg (str_rtx);
-         if (!store_bit_field_1 (temp, bitsize, bitnum, 0, 0,
+         if (!store_bit_field_1 (temp, ibitsize, ibitnum, 0, 0,
                                  int_mode, value, reverse, true))
            gcc_unreachable ();
 
@@ -1108,23 +1148,22 @@ store_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
   /* Under the C++0x memory model, we must not touch bits outside the
      bit region.  Adjust the address to start at the beginning of the
      bit region.  */
-  if (MEM_P (str_rtx) && bitregion_start > 0)
+  if (MEM_P (str_rtx) && maybe_ne (bitregion_start, 0U))
     {
       scalar_int_mode best_mode;
       machine_mode addr_mode = VOIDmode;
-      HOST_WIDE_INT offset, size;
 
-      gcc_assert ((bitregion_start % BITS_PER_UNIT) == 0);
-
-      offset = bitregion_start / BITS_PER_UNIT;
+      poly_uint64 offset = exact_div (bitregion_start, BITS_PER_UNIT);
       bitnum -= bitregion_start;
-      size = (bitnum + bitsize + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
+      poly_int64 size = bits_to_bytes_round_up (bitnum + bitsize);
       bitregion_end -= bitregion_start;
       bitregion_start = 0;
-      if (get_best_mode (bitsize, bitnum,
-                        bitregion_start, bitregion_end,
-                        MEM_ALIGN (str_rtx), INT_MAX,
-                        MEM_VOLATILE_P (str_rtx), &best_mode))
+      if (bitsize.is_constant (&ibitsize)
+         && bitnum.is_constant (&ibitnum)
+         && get_best_mode (ibitsize, ibitnum,
+                           bitregion_start, bitregion_end,
+                           MEM_ALIGN (str_rtx), INT_MAX,
+                           MEM_VOLATILE_P (str_rtx), &best_mode))
        addr_mode = best_mode;
       str_rtx = adjust_bitfield_address_size (str_rtx, addr_mode,
                                              offset, size);
@@ -1147,8 +1186,7 @@ static void
 store_fixed_bit_field (rtx op0, opt_scalar_int_mode op0_mode,
                       unsigned HOST_WIDE_INT bitsize,
                       unsigned HOST_WIDE_INT bitnum,
-                      unsigned HOST_WIDE_INT bitregion_start,
-                      unsigned HOST_WIDE_INT bitregion_end,
+                      poly_uint64 bitregion_start, poly_uint64 bitregion_end,
                       rtx value, scalar_int_mode value_mode, bool reverse)
 {
   /* There is a case not handled here:
@@ -1303,8 +1341,7 @@ static void
 store_split_bit_field (rtx op0, opt_scalar_int_mode op0_mode,
                       unsigned HOST_WIDE_INT bitsize,
                       unsigned HOST_WIDE_INT bitpos,
-                      unsigned HOST_WIDE_INT bitregion_start,
-                      unsigned HOST_WIDE_INT bitregion_end,
+                      poly_uint64 bitregion_start, poly_uint64 bitregion_end,
                       rtx value, scalar_int_mode value_mode, bool reverse)
 {
   unsigned int unit, total_bits, bitsdone = 0;
@@ -1352,9 +1389,9 @@ store_split_bit_field (rtx op0, opt_scalar_int_mode op0_mode,
         UNIT close to the end of the region as needed.  If op0 is a REG
         or SUBREG of REG, don't do this, as there can't be data races
         on a register and we can expand shorter code in some cases.  */
-      if (bitregion_end
+      if (maybe_ne (bitregion_end, 0U)
          && unit > BITS_PER_UNIT
-         && bitpos + bitsdone - thispos + unit > bitregion_end + 1
+         && maybe_gt (bitpos + bitsdone - thispos + unit, bitregion_end + 1)
          && !REG_P (op0)
          && (GET_CODE (op0) != SUBREG || !REG_P (SUBREG_REG (op0))))
        {
@@ -1480,7 +1517,7 @@ extract_bit_field_using_extv (const extraction_insn *extv, rtx op0,
                              int unsignedp, rtx target,
                              machine_mode mode, machine_mode tmode)
 {
-  struct expand_operand ops[4];
+  class expand_operand ops[4];
   rtx spec_target = target;
   rtx spec_target_subreg = 0;
   scalar_int_mode ext_mode = extv->field_mode;
@@ -1528,8 +1565,7 @@ extract_bit_field_using_extv (const extraction_insn *extv, rtx op0,
          && TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (target), ext_mode))
        {
          target = gen_lowpart (ext_mode, target);
-         if (GET_MODE_PRECISION (ext_mode)
-             > GET_MODE_PRECISION (GET_MODE (spec_target)))
+         if (partial_subreg_p (GET_MODE (spec_target), ext_mode))
            spec_target_subreg = target;
        }
       else
@@ -1552,16 +1588,33 @@ extract_bit_field_using_extv (const extraction_insn *extv, rtx op0,
   return NULL_RTX;
 }
 
+/* See whether it would be valid to extract the part of OP0 described
+   by BITNUM and BITSIZE into a value of mode MODE using a subreg
+   operation.  Return the subreg if so, otherwise return null.  */
+
+static rtx
+extract_bit_field_as_subreg (machine_mode mode, rtx op0,
+                            poly_uint64 bitsize, poly_uint64 bitnum)
+{
+  poly_uint64 bytenum;
+  if (multiple_p (bitnum, BITS_PER_UNIT, &bytenum)
+      && known_eq (bitsize, GET_MODE_BITSIZE (mode))
+      && lowpart_bit_field_p (bitnum, bitsize, GET_MODE (op0))
+      && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (op0)))
+    return simplify_gen_subreg (mode, op0, GET_MODE (op0), bytenum);
+  return NULL_RTX;
+}
+
 /* A subroutine of extract_bit_field, with the same arguments.
    If FALLBACK_P is true, fall back to extract_fixed_bit_field
    if we can find no other means of implementing the operation.
    if FALLBACK_P is false, return NULL instead.  */
 
 static rtx
-extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
-                    unsigned HOST_WIDE_INT bitnum, int unsignedp, rtx target,
-                    machine_mode mode, machine_mode tmode,
-                    bool reverse, bool fallback_p, rtx *alt_rtl)
+extract_bit_field_1 (rtx str_rtx, poly_uint64 bitsize, poly_uint64 bitnum,
+                    int unsignedp, rtx target, machine_mode mode,
+                    machine_mode tmode, bool reverse, bool fallback_p,
+                    rtx *alt_rtl)
 {
   rtx op0 = str_rtx;
   machine_mode mode1;
@@ -1578,13 +1631,13 @@ extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
   /* If we have an out-of-bounds access to a register, just return an
      uninitialized register of the required mode.  This can occur if the
      source code contains an out-of-bounds access to a small array.  */
-  if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
+  if (REG_P (op0) && known_ge (bitnum, GET_MODE_BITSIZE (GET_MODE (op0))))
     return gen_reg_rtx (tmode);
 
   if (REG_P (op0)
       && mode == GET_MODE (op0)
-      && bitnum == 0
-      && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
+      && known_eq (bitnum, 0U)
+      && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0))))
     {
       if (reverse)
        op0 = flip_storage_order (mode, op0);
@@ -1596,32 +1649,33 @@ extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
   if (VECTOR_MODE_P (GET_MODE (op0))
       && !MEM_P (op0)
       && VECTOR_MODE_P (tmode)
-      && GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (tmode))
+      && known_eq (bitsize, GET_MODE_BITSIZE (tmode))
+      && maybe_gt (GET_MODE_SIZE (GET_MODE (op0)), GET_MODE_SIZE (tmode)))
     {
       machine_mode new_mode = GET_MODE (op0);
       if (GET_MODE_INNER (new_mode) != GET_MODE_INNER (tmode))
        {
-         new_mode = mode_for_vector (GET_MODE_INNER (tmode),
-                                     GET_MODE_BITSIZE (GET_MODE (op0))
-                                     / GET_MODE_UNIT_BITSIZE (tmode));
-         if (!VECTOR_MODE_P (new_mode)
-             || GET_MODE_SIZE (new_mode) != GET_MODE_SIZE (GET_MODE (op0))
-             || GET_MODE_INNER (new_mode) != GET_MODE_INNER (tmode)
-             || !targetm.vector_mode_supported_p (new_mode))
+         scalar_mode inner_mode = GET_MODE_INNER (tmode);
+         poly_uint64 nunits;
+         if (!multiple_p (GET_MODE_BITSIZE (GET_MODE (op0)),
+                          GET_MODE_UNIT_BITSIZE (tmode), &nunits)
+             || !related_vector_mode (tmode, inner_mode,
+                                      nunits).exists (&new_mode)
+             || maybe_ne (GET_MODE_SIZE (new_mode),
+                          GET_MODE_SIZE (GET_MODE (op0))))
            new_mode = VOIDmode;
        }
+      poly_uint64 pos;
       if (new_mode != VOIDmode
          && (convert_optab_handler (vec_extract_optab, new_mode, tmode)
              != CODE_FOR_nothing)
-         && ((bitnum + bitsize - 1) / GET_MODE_BITSIZE (tmode)
-             == bitnum / GET_MODE_BITSIZE (tmode)))
+         && multiple_p (bitnum, GET_MODE_BITSIZE (tmode), &pos))
        {
-         struct expand_operand ops[3];
+         class expand_operand ops[3];
          machine_mode outermode = new_mode;
          machine_mode innermode = tmode;
          enum insn_code icode
            = convert_optab_handler (vec_extract_optab, outermode, innermode);
-         unsigned HOST_WIDE_INT pos = bitnum / GET_MODE_BITSIZE (innermode);
 
          if (new_mode != GET_MODE (op0))
            op0 = gen_lowpart (new_mode, op0);
@@ -1662,8 +1716,8 @@ extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
        new_mode = MIN_MODE_VECTOR_INT;
 
       FOR_EACH_MODE_FROM (new_mode, new_mode)
-       if (GET_MODE_SIZE (new_mode) == GET_MODE_SIZE (GET_MODE (op0))
-           && GET_MODE_UNIT_SIZE (new_mode) == GET_MODE_SIZE (tmode)
+       if (known_eq (GET_MODE_SIZE (new_mode), GET_MODE_SIZE (GET_MODE (op0)))
+           && known_eq (GET_MODE_UNIT_SIZE (new_mode), GET_MODE_SIZE (tmode))
            && targetm.vector_mode_supported_p (new_mode))
          break;
       if (new_mode != VOIDmode)
@@ -1671,34 +1725,43 @@ extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
     }
 
   /* Use vec_extract patterns for extracting parts of vectors whenever
-     available.  */
-  if (VECTOR_MODE_P (GET_MODE (op0))
-      && !MEM_P (op0)
-      && (convert_optab_handler (vec_extract_optab, GET_MODE (op0),
-                                GET_MODE_INNER (GET_MODE (op0)))
-         != CODE_FOR_nothing)
-      && ((bitnum + bitsize - 1) / GET_MODE_UNIT_BITSIZE (GET_MODE (op0))
-         == bitnum / GET_MODE_UNIT_BITSIZE (GET_MODE (op0))))
-    {
-      struct expand_operand ops[3];
-      machine_mode outermode = GET_MODE (op0);
-      machine_mode innermode = GET_MODE_INNER (outermode);
+     available.  If that fails, see whether the current modes and bitregion
+     give a natural subreg.  */
+  machine_mode outermode = GET_MODE (op0);
+  if (VECTOR_MODE_P (outermode) && !MEM_P (op0))
+    {
+      scalar_mode innermode = GET_MODE_INNER (outermode);
       enum insn_code icode
        = convert_optab_handler (vec_extract_optab, outermode, innermode);
-      unsigned HOST_WIDE_INT pos = bitnum / GET_MODE_BITSIZE (innermode);
+      poly_uint64 pos;
+      if (icode != CODE_FOR_nothing
+         && known_eq (bitsize, GET_MODE_BITSIZE (innermode))
+         && multiple_p (bitnum, GET_MODE_BITSIZE (innermode), &pos))
+       {
+         class expand_operand ops[3];
 
-      create_output_operand (&ops[0], target, innermode);
-      ops[0].target = 1;
-      create_input_operand (&ops[1], op0, outermode);
-      create_integer_operand (&ops[2], pos);
-      if (maybe_expand_insn (icode, 3, ops))
+         create_output_operand (&ops[0], target, innermode);
+         ops[0].target = 1;
+         create_input_operand (&ops[1], op0, outermode);
+         create_integer_operand (&ops[2], pos);
+         if (maybe_expand_insn (icode, 3, ops))
+           {
+             if (alt_rtl && ops[0].target)
+               *alt_rtl = target;
+             target = ops[0].value;
+             if (GET_MODE (target) != mode)
+               return gen_lowpart (tmode, target);
+             return target;
+           }
+       }
+      /* Using subregs is useful if we're extracting one register vector
+        from a multi-register vector.  extract_bit_field_as_subreg checks
+        for valid bitsize and bitnum, so we don't need to do that here.  */
+      if (VECTOR_MODE_P (mode))
        {
-         if (alt_rtl && ops[0].target)
-           *alt_rtl = target;
-         target = ops[0].value;
-         if (GET_MODE (target) != mode)
-           return gen_lowpart (tmode, target);
-         return target;
+         rtx sub = extract_bit_field_as_subreg (mode, op0, bitsize, bitnum);
+         if (sub)
+           return sub;
        }
     }
 
@@ -1722,7 +1785,7 @@ extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
        }
       else
        {
-         HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (op0));
+         poly_int64 size = GET_MODE_SIZE (GET_MODE (op0));
          rtx mem = assign_stack_temp (GET_MODE (op0), size);
          emit_move_insn (mem, op0);
          op0 = adjust_bitfield_address_size (mem, BLKmode, 0, size);
@@ -1735,41 +1798,65 @@ extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
 
   /* Get the mode of the field to use for atomic access or subreg
      conversion.  */
-  mode1 = mode;
-  if (SCALAR_INT_MODE_P (tmode))
-    {
-      machine_mode try_mode = mode_for_size (bitsize,
-                                                 GET_MODE_CLASS (tmode), 0);
-      if (try_mode != BLKmode)
-       mode1 = try_mode;
-    }
+  if (!SCALAR_INT_MODE_P (tmode)
+      || !mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0).exists (&mode1))
+    mode1 = mode;
   gcc_assert (mode1 != BLKmode);
 
   /* Extraction of a full MODE1 value can be done with a subreg as long
      as the least significant bit of the value is the least significant
      bit of either OP0 or a word of OP0.  */
-  if (!MEM_P (op0)
-      && !reverse
-      && lowpart_bit_field_p (bitnum, bitsize, op0_mode.require ())
-      && bitsize == GET_MODE_BITSIZE (mode1)
-      && TRULY_NOOP_TRUNCATION_MODES_P (mode1, op0_mode.require ()))
+  if (!MEM_P (op0) && !reverse)
     {
-      rtx sub = simplify_gen_subreg (mode1, op0, op0_mode.require (),
-                                    bitnum / BITS_PER_UNIT);
+      rtx sub = extract_bit_field_as_subreg (mode1, op0, bitsize, bitnum);
       if (sub)
        return convert_extracted_bit_field (sub, mode, tmode, unsignedp);
     }
 
   /* Extraction of a full MODE1 value can be done with a load as long as
      the field is on a byte boundary and is sufficiently aligned.  */
-  if (simple_mem_bitfield_p (op0, bitsize, bitnum, mode1))
+  poly_uint64 bytenum;
+  if (simple_mem_bitfield_p (op0, bitsize, bitnum, mode1, &bytenum))
     {
-      op0 = adjust_bitfield_address (op0, mode1, bitnum / BITS_PER_UNIT);
+      op0 = adjust_bitfield_address (op0, mode1, bytenum);
       if (reverse)
        op0 = flip_storage_order (mode1, op0);
       return convert_extracted_bit_field (op0, mode, tmode, unsignedp);
     }
 
+  /* If we have a memory source and a non-constant bit offset, restrict
+     the memory to the referenced bytes.  This is a worst-case fallback
+     but is useful for things like vector booleans.  */
+  if (MEM_P (op0) && !bitnum.is_constant ())
+    {
+      bytenum = bits_to_bytes_round_down (bitnum);
+      bitnum = num_trailing_bits (bitnum);
+      poly_uint64 bytesize = bits_to_bytes_round_up (bitnum + bitsize);
+      op0 = adjust_bitfield_address_size (op0, BLKmode, bytenum, bytesize);
+      op0_mode = opt_scalar_int_mode ();
+    }
+
+  /* It's possible we'll need to handle other cases here for
+     polynomial bitnum and bitsize.  */
+
+  /* From here on we need to be looking at a fixed-size insertion.  */
+  return extract_integral_bit_field (op0, op0_mode, bitsize.to_constant (),
+                                    bitnum.to_constant (), unsignedp,
+                                    target, mode, tmode, reverse, fallback_p);
+}
+
+/* Subroutine of extract_bit_field_1, with the same arguments, except
+   that BITSIZE and BITNUM are constant.  Handle cases specific to
+   integral modes.  If OP0_MODE is defined, it is the mode of OP0,
+   otherwise OP0 is a BLKmode MEM.  */
+
+static rtx
+extract_integral_bit_field (rtx op0, opt_scalar_int_mode op0_mode,
+                           unsigned HOST_WIDE_INT bitsize,
+                           unsigned HOST_WIDE_INT bitnum, int unsignedp,
+                           rtx target, machine_mode mode, machine_mode tmode,
+                           bool reverse, bool fallback_p)
+{
   /* Handle fields bigger than a word.  */
 
   if (bitsize > BITS_PER_WORD)
@@ -1789,12 +1876,17 @@ extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
 
       /* In case we're about to clobber a base register or something 
         (see gcc.c-torture/execute/20040625-1.c).   */
-      if (reg_mentioned_p (target, str_rtx))
+      if (reg_mentioned_p (target, op0))
        target = gen_reg_rtx (mode);
 
       /* Indicate for flow that the entire target reg is being set.  */
       emit_clobber (target);
 
+      /* The mode must be fixed-size, since extract_bit_field_1 handles
+        extractions from variable-sized objects before calling this
+        function.  */
+      unsigned int target_size
+       = GET_MODE_SIZE (GET_MODE (target)).to_constant ();
       last = get_last_insn ();
       for (i = 0; i < nwords; i++)
        {
@@ -1802,9 +1894,7 @@ extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
             if I is 1, use the next to lowest word; and so on.  */
          /* Word number in TARGET to use.  */
          unsigned int wordnum
-           = (backwards
-              ? GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD - i - 1
-              : i);
+           = (backwards ? target_size / UNITS_PER_WORD - i - 1 : i);
          /* Offset from start of field in OP0.  */
          unsigned int bit_offset = (backwards ^ reverse
                                     ? MAX ((int) bitsize - ((int) i + 1)
@@ -1833,11 +1923,11 @@ extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
        {
          /* Unless we've filled TARGET, the upper regs in a multi-reg value
             need to be zero'd out.  */
-         if (GET_MODE_SIZE (GET_MODE (target)) > nwords * UNITS_PER_WORD)
+         if (target_size > nwords * UNITS_PER_WORD)
            {
              unsigned int i, total_words;
 
-             total_words = GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD;
+             total_words = target_size / UNITS_PER_WORD;
              for (i = nwords; i < total_words; i++)
                emit_move_insn
                  (operand_subword (target,
@@ -1881,7 +1971,7 @@ extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
       && !reverse
       /* ??? We could limit the structure size to the part of OP0 that
         contains the field, with appropriate checks for endianness
-        and TRULY_NOOP_TRUNCATION.  */
+        and TARGET_TRULY_NOOP_TRUNCATION.  */
       && get_best_reg_extraction_insn (&extv, pattern,
                                       GET_MODE_BITSIZE (op0_mode.require ()),
                                       tmode))
@@ -1972,46 +2062,54 @@ extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
    If a TARGET is specified and we can store in it at no extra cost,
    we do so, and return TARGET.
    Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
-   if they are equally easy.  */
+   if they are equally easy.
+
+   If the result can be stored at TARGET, and ALT_RTL is non-NULL,
+   then *ALT_RTL is set to TARGET (before legitimziation).  */
 
 rtx
-extract_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
-                  unsigned HOST_WIDE_INT bitnum, int unsignedp, rtx target,
-                  machine_mode mode, machine_mode tmode, bool reverse,
-                  rtx *alt_rtl)
+extract_bit_field (rtx str_rtx, poly_uint64 bitsize, poly_uint64 bitnum,
+                  int unsignedp, rtx target, machine_mode mode,
+                  machine_mode tmode, bool reverse, rtx *alt_rtl)
 {
   machine_mode mode1;
 
   /* Handle -fstrict-volatile-bitfields in the cases where it applies.  */
-  if (GET_MODE_BITSIZE (GET_MODE (str_rtx)) > 0)
+  if (maybe_ne (GET_MODE_BITSIZE (GET_MODE (str_rtx)), 0))
     mode1 = GET_MODE (str_rtx);
-  else if (target && GET_MODE_BITSIZE (GET_MODE (target)) > 0)
+  else if (target && maybe_ne (GET_MODE_BITSIZE (GET_MODE (target)), 0))
     mode1 = GET_MODE (target);
   else
     mode1 = tmode;
 
+  unsigned HOST_WIDE_INT ibitsize, ibitnum;
   scalar_int_mode int_mode;
-  if (is_a <scalar_int_mode> (mode1, &int_mode)
-      && strict_volatile_bitfield_p (str_rtx, bitsize, bitnum, int_mode, 0, 0))
+  if (bitsize.is_constant (&ibitsize)
+      && bitnum.is_constant (&ibitnum)
+      && is_a <scalar_int_mode> (mode1, &int_mode)
+      && strict_volatile_bitfield_p (str_rtx, ibitsize, ibitnum,
+                                    int_mode, 0, 0))
     {
       /* Extraction of a full INT_MODE value can be done with a simple load.
         We know here that the field can be accessed with one single
         instruction.  For targets that support unaligned memory,
         an unaligned access may be necessary.  */
-      if (bitsize == GET_MODE_BITSIZE (int_mode))
+      if (ibitsize == GET_MODE_BITSIZE (int_mode))
        {
          rtx result = adjust_bitfield_address (str_rtx, int_mode,
-                                               bitnum / BITS_PER_UNIT);
+                                               ibitnum / BITS_PER_UNIT);
          if (reverse)
            result = flip_storage_order (int_mode, result);
-         gcc_assert (bitnum % BITS_PER_UNIT == 0);
+         gcc_assert (ibitnum % BITS_PER_UNIT == 0);
          return convert_extracted_bit_field (result, mode, tmode, unsignedp);
        }
 
-      str_rtx = narrow_bit_field_mem (str_rtx, int_mode, bitsize, bitnum,
-                                     &bitnum);
-      gcc_assert (bitnum + bitsize <= GET_MODE_BITSIZE (int_mode));
+      str_rtx = narrow_bit_field_mem (str_rtx, int_mode, ibitsize, ibitnum,
+                                     &ibitnum);
+      gcc_assert (ibitnum + ibitsize <= GET_MODE_BITSIZE (int_mode));
       str_rtx = copy_to_reg (str_rtx);
+      return extract_bit_field_1 (str_rtx, ibitsize, ibitnum, unsignedp,
+                                 target, mode, tmode, reverse, true, alt_rtl);
     }
 
   return extract_bit_field_1 (str_rtx, bitsize, bitnum, unsignedp,
@@ -2260,7 +2358,7 @@ extract_split_bit_field (rtx op0, opt_scalar_int_mode op0_mode,
        a zero extension
 
      - when MODE is smaller than SRC_MODE, the extraction involves
-       a truncation (and is thus subject to TRULY_NOOP_TRUNCATION).
+       a truncation (and is thus subject to TARGET_TRULY_NOOP_TRUNCATION).
 
    In other words, this routine performs a computation, whereas the
    gen_lowpart* routines are conceptually lvalue or rvalue subreg
@@ -2269,7 +2367,7 @@ extract_split_bit_field (rtx op0, opt_scalar_int_mode op0_mode,
 rtx
 extract_low_bits (machine_mode mode, machine_mode src_mode, rtx src)
 {
-  machine_mode int_mode, src_int_mode;
+  scalar_int_mode int_mode, src_int_mode;
 
   if (mode == src_mode)
     return src;
@@ -2279,7 +2377,7 @@ extract_low_bits (machine_mode mode, machine_mode src_mode, rtx src)
       /* simplify_gen_subreg can't be used here, as if simplify_subreg
         fails, it will happily create (subreg (symbol_ref)) or similar
         invalid SUBREGs.  */
-      unsigned int byte = subreg_lowpart_offset (mode, src_mode);
+      poly_uint64 byte = subreg_lowpart_offset (mode, src_mode);
       rtx ret = simplify_subreg (mode, src, src_mode, byte);
       if (ret)
        return ret;
@@ -2295,8 +2393,8 @@ extract_low_bits (machine_mode mode, machine_mode src_mode, rtx src)
   if (GET_MODE_CLASS (mode) == MODE_CC || GET_MODE_CLASS (src_mode) == MODE_CC)
     return NULL_RTX;
 
-  if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (src_mode)
-      && MODES_TIEABLE_P (mode, src_mode))
+  if (known_eq (GET_MODE_BITSIZE (mode), GET_MODE_BITSIZE (src_mode))
+      && targetm.modes_tieable_p (mode, src_mode))
     {
       rtx x = gen_lowpart_common (mode, src);
       if (x)
@@ -2307,12 +2405,16 @@ extract_low_bits (machine_mode mode, machine_mode src_mode, rtx src)
       || !int_mode_for_mode (mode).exists (&int_mode))
     return NULL_RTX;
 
-  if (!MODES_TIEABLE_P (src_int_mode, src_mode))
+  if (!targetm.modes_tieable_p (src_int_mode, src_mode))
     return NULL_RTX;
-  if (!MODES_TIEABLE_P (int_mode, mode))
+  if (!targetm.modes_tieable_p (int_mode, mode))
     return NULL_RTX;
 
   src = gen_lowpart (src_int_mode, src);
+  if (!validate_subreg (int_mode, src_int_mode, src,
+                       subreg_lowpart_offset (int_mode, src_int_mode)))
+    return NULL_RTX;
+
   src = convert_modes (int_mode, src_int_mode, src, true);
   src = gen_lowpart (mode, src);
   return src;
@@ -2364,12 +2466,10 @@ expand_shift_1 (enum tree_code code, machine_mode mode, rtx shifted,
   optab lrotate_optab = rotl_optab;
   optab rrotate_optab = rotr_optab;
   machine_mode op1_mode;
-  machine_mode scalar_mode = mode;
+  scalar_mode scalar_mode = GET_MODE_INNER (mode);
   int attempt;
   bool speed = optimize_insn_for_speed_p ();
 
-  if (VECTOR_MODE_P (mode))
-    scalar_mode = GET_MODE_INNER (mode);
   op1 = amount;
   op1_mode = GET_MODE (op1);
 
@@ -2393,8 +2493,9 @@ expand_shift_1 (enum tree_code code, machine_mode mode, rtx shifted,
       if (CONST_INT_P (op1)
          && ((unsigned HOST_WIDE_INT) INTVAL (op1) >=
              (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (scalar_mode)))
-       op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
-                      % GET_MODE_BITSIZE (scalar_mode));
+       op1 = gen_int_shift_amount (mode,
+                                   (unsigned HOST_WIDE_INT) INTVAL (op1)
+                                   % GET_MODE_BITSIZE (scalar_mode));
       else if (GET_CODE (op1) == SUBREG
               && subreg_lowpart_p (op1)
               && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (op1)))
@@ -2411,7 +2512,8 @@ expand_shift_1 (enum tree_code code, machine_mode mode, rtx shifted,
       && IN_RANGE (INTVAL (op1), GET_MODE_BITSIZE (scalar_mode) / 2 + left,
                   GET_MODE_BITSIZE (scalar_mode) - 1))
     {
-      op1 = GEN_INT (GET_MODE_BITSIZE (scalar_mode) - INTVAL (op1));
+      op1 = gen_int_shift_amount (mode, (GET_MODE_BITSIZE (scalar_mode)
+                                        - INTVAL (op1)));
       left = !left;
       code = left ? LROTATE_EXPR : RROTATE_EXPR;
     }
@@ -2424,9 +2526,8 @@ expand_shift_1 (enum tree_code code, machine_mode mode, rtx shifted,
       && CONST_INT_P (op1)
       && INTVAL (op1) == BITS_PER_UNIT
       && GET_MODE_SIZE (scalar_mode) == 2
-      && optab_handler (bswap_optab, HImode) != CODE_FOR_nothing)
-    return expand_unop (HImode, bswap_optab, shifted, NULL_RTX,
-                                 unsignedp);
+      && optab_handler (bswap_optab, mode) != CODE_FOR_nothing)
+    return expand_unop (mode, bswap_optab, shifted, NULL_RTX, unsignedp);
 
   if (op1 == const0_rtx)
     return shifted;
@@ -2491,8 +2592,8 @@ expand_shift_1 (enum tree_code code, machine_mode mode, rtx shifted,
              if (op1 == const0_rtx)
                return shifted;
              else if (CONST_INT_P (op1))
-               other_amount = GEN_INT (GET_MODE_BITSIZE (scalar_mode)
-                                       - INTVAL (op1));
+               other_amount = gen_int_shift_amount
+                 (mode, GET_MODE_BITSIZE (scalar_mode) - INTVAL (op1));
              else
                {
                  other_amount
@@ -2563,10 +2664,11 @@ expand_shift_1 (enum tree_code code, machine_mode mode, rtx shifted,
 
 rtx
 expand_shift (enum tree_code code, machine_mode mode, rtx shifted,
-             int amount, rtx target, int unsignedp)
+             poly_int64 amount, rtx target, int unsignedp)
 {
-  return expand_shift_1 (code, mode,
-                        shifted, GEN_INT (amount), target, unsignedp);
+  return expand_shift_1 (code, mode, shifted,
+                        gen_int_shift_amount (mode, amount),
+                        target, unsignedp);
 }
 
 /* Likewise, but return 0 if that cannot be done.  */
@@ -2600,9 +2702,9 @@ static void synth_mult (struct algorithm *, unsigned HOST_WIDE_INT,
 static rtx expand_mult_const (machine_mode, rtx, HOST_WIDE_INT, rtx,
                              const struct algorithm *, enum mult_variant);
 static unsigned HOST_WIDE_INT invert_mod2n (unsigned HOST_WIDE_INT, int);
-static rtx extract_high_half (machine_mode, rtx);
-static rtx expmed_mult_highpart (machine_mode, rtx, rtx, rtx, int, int);
-static rtx expmed_mult_highpart_optab (machine_mode, rtx, rtx, rtx,
+static rtx extract_high_half (scalar_int_mode, rtx);
+static rtx expmed_mult_highpart (scalar_int_mode, rtx, rtx, rtx, int, int);
+static rtx expmed_mult_highpart_optab (scalar_int_mode, rtx, rtx, rtx,
                                       int, int);
 /* Compute and return the best algorithm for multiplying by T.
    The algorithm must cost less than cost_limit
@@ -3264,20 +3366,29 @@ expand_mult_const (machine_mode mode, rtx op0, HOST_WIDE_INT val,
          /* Write a REG_EQUAL note on the last insn so that we can cse
             multiplication sequences.  Note that if ACCUM is a SUBREG,
             we've set the inner register and must properly indicate that.  */
-          tem = op0, nmode = mode;
-          accum_inner = accum;
-          if (GET_CODE (accum) == SUBREG)
+         tem = op0, nmode = mode;
+         accum_inner = accum;
+         if (GET_CODE (accum) == SUBREG)
            {
              accum_inner = SUBREG_REG (accum);
              nmode = GET_MODE (accum_inner);
              tem = gen_lowpart (nmode, op0);
            }
 
-          insn = get_last_insn ();
-          set_dst_reg_note (insn, REG_EQUAL,
-                           gen_rtx_MULT (nmode, tem,
-                                         gen_int_mode (val_so_far, nmode)),
-                           accum_inner);
+         /* Don't add a REG_EQUAL note if tem is a paradoxical SUBREG.
+            In that case, only the low bits of accum would be guaranteed to
+            be equal to the content of the REG_EQUAL note, the upper bits
+            can be anything.  */
+         if (!paradoxical_subreg_p (tem))
+           {
+             insn = get_last_insn ();
+             wide_int wval_so_far
+               = wi::uhwi (val_so_far,
+                           GET_MODE_PRECISION (as_a <scalar_mode> (nmode)));
+             rtx c = immed_wide_int_const (wval_so_far, nmode);
+             set_dst_reg_note (insn, REG_EQUAL, gen_rtx_MULT (nmode, tem, c),
+                               accum_inner);
+           }
        }
     }
 
@@ -3312,7 +3423,7 @@ expand_mult_const (machine_mode mode, rtx op0, HOST_WIDE_INT val,
 
 rtx
 expand_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
-            int unsignedp)
+            int unsignedp, bool no_libcall)
 {
   enum mult_variant variant;
   struct algorithm algorithm;
@@ -3448,14 +3559,16 @@ expand_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
     {
       op0 = force_reg (GET_MODE (op0), op0);
       return expand_binop (mode, add_optab, op0, op0,
-                          target, unsignedp, OPTAB_LIB_WIDEN);
+                          target, unsignedp,
+                          no_libcall ? OPTAB_WIDEN : OPTAB_LIB_WIDEN);
     }
 
   /* This used to use umul_optab if unsigned, but for non-widening multiply
      there is no difference between signed and unsigned.  */
   op0 = expand_binop (mode, do_trapv ? smulv_optab : smul_optab,
-                     op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
-  gcc_assert (op0);
+                     op0, op1, target, unsignedp,
+                     no_libcall ? OPTAB_WIDEN : OPTAB_LIB_WIDEN);
+  gcc_assert (op0 || no_libcall);
   return op0;
 }
 
@@ -3597,7 +3710,7 @@ choose_multiplier (unsigned HOST_WIDE_INT d, int n, int precision,
     {
       unsigned HOST_WIDE_INT mask = (HOST_WIDE_INT_1U << n) - 1;
       *multiplier_ptr = mhigh.to_uhwi () & mask;
-      return mhigh.to_uhwi () >= mask;
+      return mhigh.to_uhwi () > mask;
     }
   else
     {
@@ -3645,7 +3758,7 @@ invert_mod2n (unsigned HOST_WIDE_INT x, int n)
    MODE is the mode of operation.  */
 
 rtx
-expand_mult_highpart_adjust (machine_mode mode, rtx adj_operand, rtx op0,
+expand_mult_highpart_adjust (scalar_int_mode mode, rtx adj_operand, rtx op0,
                             rtx op1, rtx target, int unsignedp)
 {
   rtx tem;
@@ -3670,16 +3783,13 @@ expand_mult_highpart_adjust (machine_mode mode, rtx adj_operand, rtx op0,
 /* Subroutine of expmed_mult_highpart.  Return the MODE high part of OP.  */
 
 static rtx
-extract_high_half (machine_mode mode, rtx op)
+extract_high_half (scalar_int_mode mode, rtx op)
 {
-  machine_mode wider_mode;
-
   if (mode == word_mode)
     return gen_highpart (mode, op);
 
-  gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
+  scalar_int_mode wider_mode = GET_MODE_WIDER_MODE (mode).require ();
 
-  wider_mode = GET_MODE_WIDER_MODE (mode).require ();
   op = expand_shift (RSHIFT_EXPR, wider_mode, op,
                     GET_MODE_BITSIZE (mode), 0, 1);
   return convert_modes (mode, wider_mode, op, 0);
@@ -3689,19 +3799,17 @@ extract_high_half (machine_mode mode, rtx op)
    optab.  OP1 is an rtx for the constant operand.  */
 
 static rtx
-expmed_mult_highpart_optab (machine_mode mode, rtx op0, rtx op1,
+expmed_mult_highpart_optab (scalar_int_mode mode, rtx op0, rtx op1,
                            rtx target, int unsignedp, int max_cost)
 {
   rtx narrow_op1 = gen_int_mode (INTVAL (op1), mode);
-  machine_mode wider_mode;
   optab moptab;
   rtx tem;
   int size;
   bool speed = optimize_insn_for_speed_p ();
 
-  gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
+  scalar_int_mode wider_mode = GET_MODE_WIDER_MODE (mode).require ();
 
-  wider_mode = GET_MODE_WIDER_MODE (mode).require ();
   size = GET_MODE_BITSIZE (mode);
 
   /* Firstly, try using a multiplication insn that only generates the needed
@@ -3733,7 +3841,7 @@ expmed_mult_highpart_optab (machine_mode mode, rtx op0, rtx op1,
 
   /* Try widening multiplication.  */
   moptab = unsignedp ? umul_widen_optab : smul_widen_optab;
-  if (widening_optab_handler (moptab, wider_mode, mode) != CODE_FOR_nothing
+  if (convert_optab_handler (moptab, wider_mode, mode) != CODE_FOR_nothing
       && mul_widen_cost (speed, wider_mode) < max_cost)
     {
       tem = expand_binop (wider_mode, moptab, op0, narrow_op1, 0,
@@ -3772,7 +3880,7 @@ expmed_mult_highpart_optab (machine_mode mode, rtx op0, rtx op1,
 
   /* Try widening multiplication of opposite signedness, and adjust.  */
   moptab = unsignedp ? smul_widen_optab : umul_widen_optab;
-  if (widening_optab_handler (moptab, wider_mode, mode) != CODE_FOR_nothing
+  if (convert_optab_handler (moptab, wider_mode, mode) != CODE_FOR_nothing
       && size - 1 < BITS_PER_WORD
       && (mul_widen_cost (speed, wider_mode)
          + 2 * shift_cost (speed, mode, size-1)
@@ -3794,7 +3902,7 @@ expmed_mult_highpart_optab (machine_mode mode, rtx op0, rtx op1,
 
 /* Emit code to multiply OP0 and OP1 (where OP1 is an integer constant),
    putting the high half of the result in TARGET if that is convenient,
-   and return where the result is.  If the operation can not be performed,
+   and return where the result is.  If the operation cannot be performed,
    0 is returned.
 
    MODE is the mode of operation and result.
@@ -3804,10 +3912,9 @@ expmed_mult_highpart_optab (machine_mode mode, rtx op0, rtx op1,
    MAX_COST is the total allowed cost for the expanded RTL.  */
 
 static rtx
-expmed_mult_highpart (machine_mode mode, rtx op0, rtx op1,
+expmed_mult_highpart (scalar_int_mode mode, rtx op0, rtx op1,
                      rtx target, int unsignedp, int max_cost)
 {
-  machine_mode wider_mode = GET_MODE_WIDER_MODE (mode).require ();
   unsigned HOST_WIDE_INT cnst1;
   int extra_cost;
   bool sign_adjust = false;
@@ -3816,7 +3923,6 @@ expmed_mult_highpart (machine_mode mode, rtx op0, rtx op1,
   rtx tem;
   bool speed = optimize_insn_for_speed_p ();
 
-  gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
   /* We can't support modes wider than HOST_BITS_PER_INT.  */
   gcc_assert (HWI_COMPUTABLE_MODE_P (mode));
 
@@ -3826,6 +3932,7 @@ expmed_mult_highpart (machine_mode mode, rtx op0, rtx op1,
      ??? We might be able to perform double-word arithmetic if
      mode == word_mode, however all the cost calculations in
      synth_mult etc. assume single-word operations.  */
+  scalar_int_mode wider_mode = GET_MODE_WIDER_MODE (mode).require ();
   if (GET_MODE_BITSIZE (wider_mode) > BITS_PER_WORD)
     return expmed_mult_highpart_optab (mode, op0, op1, target,
                                       unsignedp, max_cost);
@@ -3868,7 +3975,7 @@ expmed_mult_highpart (machine_mode mode, rtx op0, rtx op1,
 /* Expand signed modulus of OP0 by a power of two D in mode MODE.  */
 
 static rtx
-expand_smod_pow2 (machine_mode mode, rtx op0, HOST_WIDE_INT d)
+expand_smod_pow2 (scalar_int_mode mode, rtx op0, HOST_WIDE_INT d)
 {
   rtx result, temp, shift;
   rtx_code_label *label;
@@ -3888,7 +3995,7 @@ expand_smod_pow2 (machine_mode mode, rtx op0, HOST_WIDE_INT d)
        {
          HOST_WIDE_INT masklow = (HOST_WIDE_INT_1 << logd) - 1;
          signmask = force_reg (mode, signmask);
-         shift = GEN_INT (GET_MODE_BITSIZE (mode) - logd);
+         shift = gen_int_shift_amount (mode, GET_MODE_BITSIZE (mode) - logd);
 
          /* Use the rtx_cost of a LSHIFTRT instruction to determine
             which instruction sequence to use.  If logical right shifts
@@ -3965,7 +4072,7 @@ expand_smod_pow2 (machine_mode mode, rtx op0, HOST_WIDE_INT d)
    This routine is only called for positive values of D.  */
 
 static rtx
-expand_sdiv_pow2 (machine_mode mode, rtx op0, HOST_WIDE_INT d)
+expand_sdiv_pow2 (scalar_int_mode mode, rtx op0, HOST_WIDE_INT d)
 {
   rtx temp;
   rtx_code_label *label;
@@ -4405,6 +4512,11 @@ expand_divmod (int rem_flag, enum tree_code code, machine_mode mode,
                HOST_WIDE_INT d = INTVAL (op1);
                unsigned HOST_WIDE_INT abs_d;
 
+               /* Not prepared to handle division/remainder by
+                  0xffffffffffffffff8000000000000000 etc.  */
+               if (d == HOST_WIDE_INT_MIN && size > HOST_BITS_PER_WIDE_INT)
+                 break;
+
                /* Since d might be INT_MIN, we have to cast to
                   unsigned HOST_WIDE_INT before negating to avoid
                   undefined signed overflow.  */
@@ -4447,9 +4559,7 @@ expand_divmod (int rem_flag, enum tree_code code, machine_mode mode,
                             || (optab_handler (sdivmod_optab, int_mode)
                                 != CODE_FOR_nothing)))
                  ;
-               else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d)
-                        && (size <= HOST_BITS_PER_WIDE_INT
-                            || abs_d != (unsigned HOST_WIDE_INT) d))
+               else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d))
                  {
                    if (rem_flag)
                      {
@@ -5211,20 +5321,20 @@ make_tree (tree type, rtx x)
 
     case CONST_VECTOR:
       {
-       int units = CONST_VECTOR_NUNITS (x);
+       unsigned int npatterns = CONST_VECTOR_NPATTERNS (x);
+       unsigned int nelts_per_pattern = CONST_VECTOR_NELTS_PER_PATTERN (x);
        tree itype = TREE_TYPE (type);
-       tree *elts;
-       int i;
 
        /* Build a tree with vector elements.  */
-       elts = XALLOCAVEC (tree, units);
-       for (i = units - 1; i >= 0; --i)
+       tree_vector_builder elts (type, npatterns, nelts_per_pattern);
+       unsigned int count = elts.encoded_nelts ();
+       for (unsigned int i = 0; i < count; ++i)
          {
            rtx elt = CONST_VECTOR_ELT (x, i);
-           elts[i] = make_tree (itype, elt);
+           elts.quick_push (make_tree (itype, elt));
          }
 
-       return build_vector (type, elts);
+       return elts.build ();
       }
 
     case PLUS:
@@ -5289,6 +5399,9 @@ make_tree (tree type, rtx x)
       /* fall through.  */
 
     default:
+      if (CONST_POLY_INT_P (x))
+       return wide_int_to_tree (t, const_poly_int_value (x));
+
       t = build_decl (RTL_LOCATION (x), VAR_DECL, NULL_TREE, type);
 
       /* If TYPE is a POINTER_TYPE, we might need to convert X from
@@ -5334,7 +5447,7 @@ emit_cstore (rtx target, enum insn_code icode, enum rtx_code code,
             int unsignedp, rtx x, rtx y, int normalizep,
             machine_mode target_mode)
 {
-  struct expand_operand ops[4];
+  class expand_operand ops[4];
   rtx op0, comparison, subtarget;
   rtx_insn *last;
   scalar_int_mode result_mode = targetm.cstore_mode (icode);
@@ -5377,11 +5490,14 @@ emit_cstore (rtx target, enum insn_code icode, enum rtx_code code,
      If STORE_FLAG_VALUE does not have the sign bit set when
      interpreted in MODE, we can do this conversion as unsigned, which
      is usually more efficient.  */
-  if (GET_MODE_SIZE (int_target_mode) > GET_MODE_SIZE (result_mode))
+  if (GET_MODE_PRECISION (int_target_mode) > GET_MODE_PRECISION (result_mode))
     {
-      convert_move (target, subtarget,
-                   val_signbit_known_clear_p (result_mode,
-                                              STORE_FLAG_VALUE));
+      gcc_assert (GET_MODE_PRECISION (result_mode) != 1
+                 || STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1);
+
+      bool unsignedp = (STORE_FLAG_VALUE >= 0);
+      convert_move (target, subtarget, unsignedp);
+
       op0 = target;
       result_mode = int_target_mode;
     }
@@ -5460,6 +5576,9 @@ emit_store_flag_1 (rtx target, enum rtx_code code, rtx op0, rtx op1,
   if (mode == VOIDmode)
     mode = GET_MODE (op0);
 
+  if (CONST_SCALAR_INT_P (op1))
+    canonicalize_comparison (mode, &code, &op1);
+
   /* For some comparisons with 1 and -1, we can convert this to
      comparisons with zero.  This will often produce more opportunities for
      store-flag insns.  */
@@ -5630,12 +5749,11 @@ emit_store_flag_1 (rtx target, enum rtx_code code, rtx op0, rtx op1,
 
 rtx
 emit_store_flag_int (rtx target, rtx subtarget, enum rtx_code code, rtx op0,
-                    rtx op1, machine_mode mode, int unsignedp,
+                    rtx op1, scalar_int_mode mode, int unsignedp,
                     int normalizep, rtx trueval)
 {
   machine_mode target_mode = target ? GET_MODE (target) : VOIDmode;
   rtx_insn *last = get_last_insn ();
-  rtx tem;
 
   /* If this is an equality comparison of integers, we can try to exclusive-or
      (or subtract) the two operands and use a recursive call to try the
@@ -5644,8 +5762,8 @@ emit_store_flag_int (rtx target, rtx subtarget, enum rtx_code code, rtx op0,
 
   if ((code == EQ || code == NE) && op1 != const0_rtx)
     {
-      tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
-                         OPTAB_WIDEN);
+      rtx tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
+                             OPTAB_WIDEN);
 
       if (tem == 0)
        tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
@@ -5677,26 +5795,28 @@ emit_store_flag_int (rtx target, rtx subtarget, enum rtx_code code, rtx op0,
          && rtx_cost (GEN_INT (normalizep), mode, PLUS, 1,
                       optimize_insn_for_speed_p ()) == 0)
        {
-         tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
-                                  STORE_FLAG_VALUE, target_mode);
+         rtx tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
+                                      STORE_FLAG_VALUE, target_mode);
          if (tem != 0)
            tem = expand_binop (target_mode, add_optab, tem,
                                gen_int_mode (normalizep, target_mode),
                                target, 0, OPTAB_WIDEN);
+         if (tem != 0)
+           return tem;
        }
       else if (!want_add
               && rtx_cost (trueval, mode, XOR, 1,
                            optimize_insn_for_speed_p ()) == 0)
        {
-         tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
-                                  normalizep, target_mode);
+         rtx tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
+                                      normalizep, target_mode);
          if (tem != 0)
            tem = expand_binop (target_mode, xor_optab, tem, trueval, target,
                                INTVAL (trueval) >= 0, OPTAB_WIDEN);
+         if (tem != 0)
+           return tem;
        }
 
-      if (tem != 0)
-       return tem;
       delete_insns_since (last);
     }
 
@@ -5714,7 +5834,7 @@ emit_store_flag_int (rtx target, rtx subtarget, enum rtx_code code, rtx op0,
   /* Try to put the result of the comparison in the sign bit.  Assume we can't
      do the necessary operation below.  */
 
-  tem = 0;
+  rtx tem = 0;
 
   /* To see if A <= 0, compute (A | (A - 1)).  A <= 0 iff that result has
      the sign bit set.  */
@@ -5959,6 +6079,11 @@ emit_store_flag (rtx target, enum rtx_code code, rtx op0, rtx op1,
       if (!HAVE_conditional_move)
        return 0;
 
+      /* Do not turn a trapping comparison into a non-trapping one.  */
+      if ((code != EQ && code != NE && code != UNEQ && code != LTGT)
+         && flag_trapping_math)
+       return 0;
+
       /* Try using a setcc instruction for ORDERED/UNORDERED, followed by a
         conditional move.  */
       tem = emit_store_flag_1 (subtarget, first_code, op0, op1, mode, 0,
@@ -6003,6 +6128,17 @@ emit_store_flag_force (rtx target, enum rtx_code code, rtx op0, rtx op1,
   if (tem != 0)
     return tem;
 
+  /* If one operand is constant, make it the second one.  Only do this
+     if the other operand is not constant as well.  */
+  if (swap_commutative_operands_p (op0, op1))
+    {
+      std::swap (op0, op1);
+      code = swap_condition (code);
+    }
+
+  if (mode == VOIDmode)
+    mode = GET_MODE (op0);
+
   if (!target)
     target = gen_reg_rtx (word_mode);
 
@@ -6063,6 +6199,106 @@ emit_store_flag_force (rtx target, enum rtx_code code, rtx op0, rtx op1,
 
   return target;
 }
+
+/* Helper function for canonicalize_cmp_for_target.  Swap between inclusive
+   and exclusive ranges in order to create an equivalent comparison.  See
+   canonicalize_cmp_for_target for the possible cases.  */
+
+static enum rtx_code
+equivalent_cmp_code (enum rtx_code code)
+{
+  switch (code)
+    {
+    case GT:
+      return GE;
+    case GE:
+      return GT;
+    case LT:
+      return LE;
+    case LE:
+      return LT;
+    case GTU:
+      return GEU;
+    case GEU:
+      return GTU;
+    case LTU:
+      return LEU;
+    case LEU:
+      return LTU;
+
+    default:
+      return code;
+    }
+}
+
+/* Choose the more appropiate immediate in scalar integer comparisons.  The
+   purpose of this is to end up with an immediate which can be loaded into a
+   register in fewer moves, if possible.
+
+   For each integer comparison there exists an equivalent choice:
+     i)   a >  b or a >= b + 1
+     ii)  a <= b or a <  b + 1
+     iii) a >= b or a >  b - 1
+     iv)  a <  b or a <= b - 1
+
+   MODE is the mode of the first operand.
+   CODE points to the comparison code.
+   IMM points to the rtx containing the immediate.  *IMM must satisfy
+   CONST_SCALAR_INT_P on entry and continues to satisfy CONST_SCALAR_INT_P
+   on exit.  */
+
+void
+canonicalize_comparison (machine_mode mode, enum rtx_code *code, rtx *imm)
+{
+  if (!SCALAR_INT_MODE_P (mode))
+    return;
+
+  int to_add = 0;
+  enum signop sgn = unsigned_condition_p (*code) ? UNSIGNED : SIGNED;
+
+  /* Extract the immediate value from the rtx.  */
+  wide_int imm_val = rtx_mode_t (*imm, mode);
+
+  if (*code == GT || *code == GTU || *code == LE || *code == LEU)
+    to_add = 1;
+  else if (*code == GE || *code == GEU || *code == LT || *code == LTU)
+    to_add = -1;
+  else
+    return;
+
+  /* Check for overflow/underflow in the case of signed values and
+     wrapping around in the case of unsigned values.  If any occur
+     cancel the optimization.  */
+  wi::overflow_type overflow = wi::OVF_NONE;
+  wide_int imm_modif;
+
+  if (to_add == 1)
+    imm_modif = wi::add (imm_val, 1, sgn, &overflow);
+  else
+    imm_modif = wi::sub (imm_val, 1, sgn, &overflow);
+
+  if (overflow)
+    return;
+
+  /* The following creates a pseudo; if we cannot do that, bail out.  */
+  if (!can_create_pseudo_p ())
+    return;
+
+  rtx reg = gen_rtx_REG (mode, LAST_VIRTUAL_REGISTER + 1);
+  rtx new_imm = immed_wide_int_const (imm_modif, mode);
+
+  rtx_insn *old_rtx = gen_move_insn (reg, *imm);
+  rtx_insn *new_rtx = gen_move_insn (reg, new_imm);
+
+  /* Update the immediate and the code.  */
+  if (insn_cost (old_rtx, true) > insn_cost (new_rtx, true))
+    {
+      *code = equivalent_cmp_code (*code);
+      *imm = new_imm;
+    }
+}
+
+
 \f
 /* Perform possibly multi-word comparison and conditional jump to LABEL
    if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE.  This is