Index: gcc/cse.c
===================================================================
--- gcc/cse.c	2017-07-13 09:18:39.584734236 +0100
+++ gcc/cse.c	2017-07-13 09:18:46.699153232 +0100
@@ -4885,11 +4885,12 @@ cse_insn (rtx_insn *insn)
 	  && GET_CODE (src) == AND && CONST_INT_P (XEXP (src, 1))
 	  && GET_MODE_SIZE (int_mode) < UNITS_PER_WORD)
 	{
-	  machine_mode tmode;
+	  opt_scalar_int_mode tmode_iter;
 	  rtx new_and = gen_rtx_AND (VOIDmode, NULL_RTX, XEXP (src, 1));
 
-	  FOR_EACH_WIDER_MODE (tmode, int_mode)
+	  FOR_EACH_WIDER_MODE (tmode_iter, int_mode)
 	    {
+	      scalar_int_mode tmode = *tmode_iter;
 	      if (GET_MODE_SIZE (tmode) > UNITS_PER_WORD)
 		break;
 
@@ -4932,7 +4933,6 @@ cse_insn (rtx_insn *insn)
 	{
 	  struct rtx_def memory_extend_buf;
 	  rtx memory_extend_rtx = &memory_extend_buf;
-	  machine_mode tmode;
 
 	  /* Set what we are trying to extend and the operation it might
 	     have been extended with.  */
@@ -4940,10 +4940,12 @@ cse_insn (rtx_insn *insn)
 	  PUT_CODE (memory_extend_rtx, extend_op);
 	  XEXP (memory_extend_rtx, 0) = src;
 
-	  FOR_EACH_WIDER_MODE (tmode, int_mode)
+	  opt_scalar_int_mode tmode_iter;
+	  FOR_EACH_WIDER_MODE (tmode_iter, int_mode)
 	    {
 	      struct table_elt *larger_elt;
 
+	      scalar_int_mode tmode = *tmode_iter;
 	      if (GET_MODE_SIZE (tmode) > UNITS_PER_WORD)
 		break;
 
Index: gcc/explow.c
===================================================================
--- gcc/explow.c	2017-07-13 09:18:21.531429258 +0100
+++ gcc/explow.c	2017-07-13 09:18:46.700153153 +0100
@@ -1906,7 +1906,7 @@ hard_function_value (const_tree valtype,
       && GET_MODE (val) == BLKmode)
     {
       unsigned HOST_WIDE_INT bytes = int_size_in_bytes (valtype);
-      machine_mode tmpmode;
+      opt_scalar_int_mode tmpmode;
 
       /* int_size_in_bytes can return -1.  We don't need a check here
 	 since the value of bytes will then be large enough that no
@@ -1915,14 +1915,11 @@ hard_function_value (const_tree valtype,
       FOR_EACH_MODE_IN_CLASS (tmpmode, MODE_INT)
 	{
 	  /* Have we found a large enough mode?  */
-	  if (GET_MODE_SIZE (tmpmode) >= bytes)
+	  if (GET_MODE_SIZE (*tmpmode) >= bytes)
 	    break;
 	}
 
-      /* No suitable mode found.  */
-      gcc_assert (tmpmode != VOIDmode);
-
-      PUT_MODE (val, tmpmode);
+      PUT_MODE (val, *tmpmode);
     }
   return val;
 }
Index: gcc/expmed.c
===================================================================
--- gcc/expmed.c	2017-07-13 09:18:42.161519072 +0100
+++ gcc/expmed.c	2017-07-13 09:18:46.700153153 +0100
@@ -147,7 +147,6 @@ init_expmed_one_mode (struct init_expmed
 		      machine_mode mode, int speed)
 {
   int m, n, mode_bitsize;
-  machine_mode mode_from;
 
   mode_bitsize = GET_MODE_UNIT_BITSIZE (mode);
 
@@ -205,8 +204,9 @@ init_expmed_one_mode (struct init_expmed
   scalar_int_mode int_mode_to;
   if (is_a <scalar_int_mode> (mode, &int_mode_to))
     {
-      FOR_EACH_MODE_IN_CLASS (mode_from, MODE_INT)
-	init_expmed_one_conv (all, int_mode_to, mode_from, speed);
+      opt_scalar_int_mode int_mode_from;
+      FOR_EACH_MODE_IN_CLASS (int_mode_from, MODE_INT)
+	init_expmed_one_conv (all, int_mode_to, *int_mode_from, speed);
 
       scalar_int_mode wider_mode;
       if (GET_MODE_CLASS (int_mode_to) == MODE_INT
@@ -2019,12 +2019,13 @@ extract_fixed_bit_field_1 (machine_mode
 
   /* Find the narrowest integer mode that contains the field.  */
 
-  FOR_EACH_MODE_IN_CLASS (mode, MODE_INT)
-    if (GET_MODE_BITSIZE (mode) >= bitsize + bitnum)
-      {
-	op0 = convert_to_mode (mode, op0, 0);
-	break;
-      }
+  opt_scalar_int_mode mode_iter;
+  FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
+    if (GET_MODE_BITSIZE (*mode_iter) >= bitsize + bitnum)
+      break;
+
+  mode = *mode_iter;
+  op0 = convert_to_mode (mode, op0, 0);
 
   if (mode != tmode)
     target = 0;
Index: gcc/expr.c
===================================================================
--- gcc/expr.c	2017-07-13 09:18:46.226190608 +0100
+++ gcc/expr.c	2017-07-13 09:18:46.702152995 +0100
@@ -699,18 +699,17 @@ convert_modes (machine_mode mode, machin
 static unsigned int
 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
 {
-  machine_mode tmode;
+  scalar_int_mode tmode = *int_mode_for_size (max_pieces * BITS_PER_UNIT, 1);
 
-  tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
   if (align >= GET_MODE_ALIGNMENT (tmode))
     align = GET_MODE_ALIGNMENT (tmode);
   else
     {
-      machine_mode tmode, xmode;
-
-      xmode = NARROWEST_INT_MODE;
-      FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
+      scalar_int_mode xmode = NARROWEST_INT_MODE;
+      opt_scalar_int_mode mode_iter;
+      FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
 	{
+	  tmode = *mode_iter;
 	  if (GET_MODE_SIZE (tmode) > max_pieces
 	      || SLOW_UNALIGNED_ACCESS (tmode, align))
 	    break;
@@ -1707,7 +1706,6 @@ emit_block_move_via_movmem (rtx x, rtx y
 			    unsigned HOST_WIDE_INT probable_max_size)
 {
   int save_volatile_ok = volatile_ok;
-  machine_mode mode;
 
   if (expected_align < align)
     expected_align = align;
@@ -1726,8 +1724,10 @@ emit_block_move_via_movmem (rtx x, rtx y
      including more than one in the machine description unless
      the more limited one has some advantage.  */
 
-  FOR_EACH_MODE_IN_CLASS (mode, MODE_INT)
+  opt_scalar_int_mode mode_iter;
+  FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
     {
+      scalar_int_mode mode = *mode_iter;
       enum insn_code code = direct_optab_handler (movmem_optab, mode);
 
       if (code != CODE_FOR_nothing
@@ -2791,13 +2791,13 @@ copy_blkmode_to_reg (machine_mode mode,
     {
       /* Find the smallest integer mode large enough to hold the
 	 entire structure.  */
-      FOR_EACH_MODE_IN_CLASS (mode, MODE_INT)
-	/* Have we found a large enough mode?  */
-	if (GET_MODE_SIZE (mode) >= bytes)
+      opt_scalar_int_mode mode_iter;
+      FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
+	if (GET_MODE_SIZE (*mode_iter) >= bytes)
 	  break;
 
       /* A suitable mode should have been found.  */
-      gcc_assert (mode != VOIDmode);
+      mode = *mode_iter;
     }
 
   if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
@@ -3035,8 +3035,6 @@ set_storage_via_setmem (rtx object, rtx
      including more than one in the machine description unless
      the more limited one has some advantage.  */
 
-  machine_mode mode;
-
   if (expected_align < align)
     expected_align = align;
   if (expected_size != -1)
@@ -3047,8 +3045,10 @@ set_storage_via_setmem (rtx object, rtx
 	expected_size = min_size;
     }
 
-  FOR_EACH_MODE_IN_CLASS (mode, MODE_INT)
+  opt_scalar_int_mode mode_iter;
+  FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
     {
+      scalar_int_mode mode = *mode_iter;
       enum insn_code code = direct_optab_handler (setmem_optab, mode);
 
       if (code != CODE_FOR_nothing
Index: gcc/optabs.c
===================================================================
--- gcc/optabs.c	2017-07-13 09:18:39.591733644 +0100
+++ gcc/optabs.c	2017-07-13 09:18:46.703152916 +0100
@@ -3799,8 +3799,10 @@ prepare_cmp_insn (rtx x, rtx y, enum rtx
 
       /* Try to use a memory block compare insn - either cmpstr
 	 or cmpmem will do.  */
-      FOR_EACH_MODE_IN_CLASS (cmp_mode, MODE_INT)
+      opt_scalar_int_mode cmp_mode_iter;
+      FOR_EACH_MODE_IN_CLASS (cmp_mode_iter, MODE_INT)
 	{
+	  scalar_int_mode cmp_mode = *cmp_mode_iter;
 	  cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
 	  if (cmp_code == CODE_FOR_nothing)
 	    cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
Index: gcc/rtlanal.c
===================================================================
--- gcc/rtlanal.c	2017-07-13 09:18:45.762227456 +0100
+++ gcc/rtlanal.c	2017-07-13 09:18:46.703152916 +0100
@@ -5660,12 +5660,14 @@ get_condition (rtx_insn *jump, rtx_insn
 static void
 init_num_sign_bit_copies_in_rep (void)
 {
-  machine_mode mode, in_mode;
+  opt_scalar_int_mode in_mode_iter;
+  scalar_int_mode mode;
 
-  FOR_EACH_MODE_IN_CLASS (in_mode, MODE_INT)
-    FOR_EACH_MODE_UNTIL (mode, in_mode)
+  FOR_EACH_MODE_IN_CLASS (in_mode_iter, MODE_INT)
+    FOR_EACH_MODE_UNTIL (mode, *in_mode_iter)
       {
-	machine_mode i;
+	scalar_int_mode in_mode = *in_mode_iter;
+	scalar_int_mode i;
 
 	/* Currently, it is assumed that TARGET_MODE_REP_EXTENDED
 	   extends to the next widest mode.  */
@@ -5678,7 +5680,7 @@ init_num_sign_bit_copies_in_rep (void)
 	  {
 	    /* This must always exist (for the last iteration it will be
 	       IN_MODE).  */
-	    machine_mode wider = *GET_MODE_WIDER_MODE (i);
+	    scalar_int_mode wider = *GET_MODE_WIDER_MODE (i);
 
 	    if (targetm.mode_rep_extended (i, wider) == SIGN_EXTEND
 		/* We can only check sign-bit copies starting from the
Index: gcc/stor-layout.c
===================================================================
--- gcc/stor-layout.c	2017-07-13 09:18:41.680558844 +0100
+++ gcc/stor-layout.c	2017-07-13 09:18:46.704152837 +0100
@@ -1827,7 +1827,6 @@ start_bitfield_representative (tree fiel
 finish_bitfield_representative (tree repr, tree field)
 {
   unsigned HOST_WIDE_INT bitsize, maxbitsize;
-  machine_mode mode;
   tree nextf, size;
 
   size = size_diffop (DECL_FIELD_OFFSET (field),
@@ -1892,15 +1891,15 @@ finish_bitfield_representative (tree rep
   gcc_assert (maxbitsize % BITS_PER_UNIT == 0);
 
   /* Find the smallest nice mode to use.  */
-  FOR_EACH_MODE_IN_CLASS (mode, MODE_INT)
-    if (GET_MODE_BITSIZE (mode) >= bitsize)
+  opt_scalar_int_mode mode_iter;
+  FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
+    if (GET_MODE_BITSIZE (*mode_iter) >= bitsize)
       break;
-  if (mode != VOIDmode
-      && (GET_MODE_BITSIZE (mode) > maxbitsize
-	  || GET_MODE_BITSIZE (mode) > MAX_FIXED_MODE_SIZE))
-    mode = VOIDmode;
 
-  if (mode == VOIDmode)
+  scalar_int_mode mode;
+  if (!mode_iter.exists (&mode)
+      || GET_MODE_BITSIZE (mode) > maxbitsize
+      || GET_MODE_BITSIZE (mode) > MAX_FIXED_MODE_SIZE)
     {
       /* We really want a BLKmode representative only as a last resort,
          considering the member b in
Index: gcc/fortran/trans-types.c
===================================================================
--- gcc/fortran/trans-types.c	2017-07-13 09:18:32.525352977 +0100
+++ gcc/fortran/trans-types.c	2017-07-13 09:18:46.702152995 +0100
@@ -362,15 +362,16 @@ #define NAMED_SUBROUTINE(a,b,c,d) \
 void
 gfc_init_kinds (void)
 {
-  machine_mode mode;
+  opt_scalar_int_mode int_mode_iter;
   opt_scalar_float_mode float_mode_iter;
   int i_index, r_index, kind;
   bool saw_i4 = false, saw_i8 = false;
   bool saw_r4 = false, saw_r8 = false, saw_r10 = false, saw_r16 = false;
 
-  for (i_index = 0, mode = MIN_MODE_INT; mode <= MAX_MODE_INT;
-       mode = (machine_mode) ((int) mode + 1))
+  i_index = 0;
+  FOR_EACH_MODE_IN_CLASS (int_mode_iter, MODE_INT)
     {
+      scalar_int_mode mode = *int_mode_iter;
       int kind, bitsize;
 
       if (!targetm.scalar_mode_supported_p (mode))
