diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index e1725808033..f3de515f6c9 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -2623,7 +2623,7 @@ gfc_build_constant_array_constructor (gfc_expr * expr, tree type)
   TREE_CONSTANT (init) = 1;
   TREE_STATIC (init) = 1;
 
-  tmp = build_decl (input_location, VAR_DECL, create_tmp_var_name ("A"),
+  tmp = build_decl (input_location, VAR_DECL, create_tmp_var_name ("A", true),
 		    tmptype);
   DECL_ARTIFICIAL (tmp) = 1;
   DECL_IGNORED_P (tmp) = 1;
@@ -6699,7 +6699,7 @@ gfc_trans_auto_array_allocation (tree decl, gfc_symbol * sym,
     {
       gcc_assert (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE);
       space = build_decl (gfc_get_location (&sym->declared_at),
-			  VAR_DECL, create_tmp_var_name ("A"),
+			  VAR_DECL, create_tmp_var_name ("A", false),
 			  TREE_TYPE (TREE_TYPE (decl)));
       gfc_trans_vla_type_sizes (sym, &init);
     }
diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc
index 299764b08b2..4c59e4dc0f4 100644
--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -1417,10 +1417,10 @@ gfc_add_assign_aux_vars (gfc_symbol * sym)
   gfc_allocate_lang_decl (decl);
   GFC_DECL_ASSIGN (decl) = 1;
   length = build_decl (input_location,
-		       VAR_DECL, create_tmp_var_name (sym->name),
+		       VAR_DECL, create_tmp_var_name (sym->name, false),
 		       gfc_charlen_type_node);
   addr = build_decl (input_location,
-		     VAR_DECL, create_tmp_var_name (sym->name),
+		     VAR_DECL, create_tmp_var_name (sym->name, false),
 		     pvoid_type_node);
   gfc_finish_var_decl (length, sym);
   gfc_finish_var_decl (addr, sym);
@@ -2835,7 +2835,7 @@ create_function_arglist (gfc_symbol * sym)
 		     : TREE_TYPE (f->sym->backend_decl);
 
 	  token = build_decl (input_location, PARM_DECL,
-			      create_tmp_var_name ("caf_token"),
+			      create_tmp_var_name ("caf_token", false),
 			      build_qualified_type (pvoid_type_node,
 						    TYPE_QUAL_RESTRICT));
 	  if ((f->sym->ts.type != BT_CLASS
@@ -2864,7 +2864,7 @@ create_function_arglist (gfc_symbol * sym)
 	  gfc_finish_decl (token);
 
 	  offset = build_decl (input_location, PARM_DECL,
-			       create_tmp_var_name ("caf_offset"),
+			       create_tmp_var_name ("caf_offset", false),
 			       gfc_array_index_type);
 
 	  if ((f->sym->ts.type != BT_CLASS
@@ -5713,7 +5713,7 @@ generate_coarray_init (gfc_namespace * ns __attribute((unused)))
 
   tmp = build_function_type_list (void_type_node, NULL_TREE);
   fndecl = build_decl (input_location, FUNCTION_DECL,
-		       create_tmp_var_name ("_caf_init"), tmp);
+		       create_tmp_var_name ("_caf_init", true), tmp);
 
   DECL_STATIC_CONSTRUCTOR (fndecl) = 1;
   SET_DECL_INIT_PRIORITY (fndecl, DEFAULT_INIT_PRIORITY);
@@ -6395,7 +6395,7 @@ create_main_function (tree fndecl)
 
     /* Create a static variable to hold the jump table.  */
     var = build_decl (input_location, VAR_DECL,
-		      create_tmp_var_name ("options"), array_type);
+		      create_tmp_var_name ("options", true), array_type);
     DECL_ARTIFICIAL (var) = 1;
     DECL_IGNORED_P (var) = 1;
     TREE_CONSTANT (var) = 1;
diff --git a/gcc/function.h b/gcc/function.h
index d4ce8a7c6c6..dbcfe0bc97e 100644
--- a/gcc/function.h
+++ b/gcc/function.h
@@ -338,6 +338,9 @@ struct GTY(()) function {
      flags for the actions that they have taken.  */
   unsigned int pending_TODOs;
 
+  /* ID used by create_tmp_var_name.  */
+  unsigned int tmp_var_id_num;
+
   /* Non-null if the function does something that would prevent it from
      being copied; this applies to both versioning and inlining.  Set to
      a string describing the reason for failure.  */
diff --git a/gcc/gimple-expr.cc b/gcc/gimple-expr.cc
index 83dc340a35d..ddbb2b48bef 100644
--- a/gcc/gimple-expr.cc
+++ b/gcc/gimple-expr.cc
@@ -415,7 +415,7 @@ remove_suffix (char *name, int len)
 static GTY(()) unsigned int tmp_var_id_num;
 
 tree
-create_tmp_var_name (const char *prefix)
+create_tmp_var_name (const char *prefix, bool global)
 {
   char *tmp_name;
 
@@ -429,7 +429,9 @@ create_tmp_var_name (const char *prefix)
       prefix = preftmp;
     }
 
-  ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
+  /* During self-tests we create tmp variables cfun being NULL.  */
+  ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T",
+			   !global && cfun ? cfun->tmp_var_id_num++ : tmp_var_id_num++);
   return get_identifier (tmp_name);
 }
 
@@ -442,7 +444,7 @@ create_tmp_var_raw (tree type, const char *prefix)
   tree tmp_var;
 
   tmp_var = build_decl (input_location,
-			VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
+			VAR_DECL, prefix ? create_tmp_var_name (prefix, false) : NULL,
 			type);
 
   /* The variable was declared by the compiler.  */
diff --git a/gcc/gimple-expr.h b/gcc/gimple-expr.h
index e89f7e20fc8..0ac1fc2cfe1 100644
--- a/gcc/gimple-expr.h
+++ b/gcc/gimple-expr.h
@@ -28,7 +28,7 @@ extern gimple_seq gimple_body (tree);
 extern bool gimple_has_body_p (tree);
 extern const char *gimple_decl_printable_name (tree, int);
 extern tree copy_var_decl (tree, tree, tree);
-extern tree create_tmp_var_name (const char *);
+extern tree create_tmp_var_name (const char *, bool global);
 extern tree create_tmp_var_raw (tree, const char * = NULL);
 extern tree create_tmp_var (tree, const char * = NULL);
 extern tree create_tmp_reg (tree, const char * = NULL);
diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc
index 5a8340504d0..241fdc922de 100644
--- a/gcc/gimplify.cc
+++ b/gcc/gimplify.cc
@@ -637,7 +637,7 @@ internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
 	{
 	  const char *name = get_name (val);
 	  if (name)
-	    SET_SSA_NAME_VAR_OR_IDENTIFIER (t, create_tmp_var_name (name));
+	    SET_SSA_NAME_VAR_OR_IDENTIFIER (t, create_tmp_var_name (name, false));
 	}
     }
   else
@@ -5250,7 +5250,7 @@ gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
 	    DECL_INITIAL (object) = ctor;
 	    TREE_STATIC (object) = 1;
 	    if (!DECL_NAME (object))
-	      DECL_NAME (object) = create_tmp_var_name ("C");
+	      DECL_NAME (object) = create_tmp_var_name ("C", true);
 	    walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
 
 	    /* ??? C++ doesn't automatically append a .<number> to the
@@ -6250,7 +6250,7 @@ gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
     {
       if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
 	DECL_NAME (*from_p)
-	  = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
+	  = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)), false);
       DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
       SET_DECL_DEBUG_EXPR (*from_p, *to_p);
    }
@@ -17881,7 +17881,7 @@ gimplify_function_tree (tree fndecl)
 	  tree first_var
 	    = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
 			  VAR_DECL,
-			  create_tmp_var_name ("C"),
+			  create_tmp_var_name ("C", true),
 			  boolean_type_node);
 	  DECL_ARTIFICIAL (first_var) = 1;
 	  DECL_IGNORED_P (first_var) = 1;
diff --git a/gcc/ipa-param-manipulation.cc b/gcc/ipa-param-manipulation.cc
index 42488ee09c3..314a7a5aa20 100644
--- a/gcc/ipa-param-manipulation.cc
+++ b/gcc/ipa-param-manipulation.cc
@@ -1378,7 +1378,7 @@ ipa_param_body_adjustments::common_initialization (tree old_fndecl,
 	  new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
 				 new_type);
 	  const char *prefix = ipa_param_prefixes[apm->param_prefix_index];
-	  DECL_NAME (new_parm) = create_tmp_var_name (prefix);
+	  DECL_NAME (new_parm) = create_tmp_var_name (prefix, false);
 	  DECL_ARTIFICIAL (new_parm) = 1;
 	  DECL_ARG_TYPE (new_parm) = new_type;
 	  DECL_CONTEXT (new_parm) = m_fndecl;
diff --git a/gcc/lto-streamer-in.cc b/gcc/lto-streamer-in.cc
index 03cb41cfa16..5a0f156eb27 100644
--- a/gcc/lto-streamer-in.cc
+++ b/gcc/lto-streamer-in.cc
@@ -1300,6 +1300,8 @@ input_struct_function_base (struct function *fn, class data_in *data_in,
   /* Input the current IL state of the function.  */
   fn->curr_properties = streamer_read_uhwi (ib);
 
+  fn->tmp_var_id_num = streamer_read_uhwi (ib);
+
   /* Read all the attributes for FN.  */
   bp = streamer_read_bitpack (ib);
   fn->is_thunk = bp_unpack_value (&bp, 1);
diff --git a/gcc/lto-streamer-out.cc b/gcc/lto-streamer-out.cc
index 0bca530313c..27f9edd3fcd 100644
--- a/gcc/lto-streamer-out.cc
+++ b/gcc/lto-streamer-out.cc
@@ -2260,6 +2256,8 @@ output_struct_function_base (struct output_block *ob, struct function *fn)
   /* Output current IL state of the function.  */
   streamer_write_uhwi (ob, fn->curr_properties);
 
+  streamer_write_uhwi (ob, fn->tmp_var_id_num);
+
   /* Write all the attributes for FN.  */
   bp = bitpack_create (ob->main_stream);
   bp_pack_value (&bp, fn->is_thunk, 1);
diff --git a/gcc/omp-low.cc b/gcc/omp-low.cc
index dddf5b59d8f..5ae35260a28 100644
--- a/gcc/omp-low.cc
+++ b/gcc/omp-low.cc
@@ -2324,7 +2324,7 @@ scan_omp_parallel (gimple_stmt_iterator *gsi, omp_context *outer_ctx)
     ctx->is_nested = true;
   ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
   ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
-  name = create_tmp_var_name (".omp_data_s");
+  name = create_tmp_var_name (".omp_data_s", false);
   name = build_decl (gimple_location (stmt),
 		     TYPE_DECL, name, ctx->record_type);
   DECL_ARTIFICIAL (name) = 1;
@@ -2377,7 +2377,7 @@ scan_omp_task (gimple_stmt_iterator *gsi, omp_context *outer_ctx)
     ctx->is_nested = true;
   ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
   ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
-  name = create_tmp_var_name (".omp_data_s");
+  name = create_tmp_var_name (".omp_data_s", false);
   name = build_decl (gimple_location (stmt),
 		     TYPE_DECL, name, ctx->record_type);
   DECL_ARTIFICIAL (name) = 1;
@@ -2391,7 +2391,7 @@ scan_omp_task (gimple_stmt_iterator *gsi, omp_context *outer_ctx)
 
   if (ctx->srecord_type)
     {
-      name = create_tmp_var_name (".omp_data_a");
+      name = create_tmp_var_name (".omp_data_a", false);
       name = build_decl (gimple_location (stmt),
 			 TYPE_DECL, name, ctx->srecord_type);
       DECL_ARTIFICIAL (name) = 1;
@@ -3067,7 +3067,7 @@ scan_omp_single (gomp_single *stmt, omp_context *outer_ctx)
   ctx = new_omp_context (stmt, outer_ctx);
   ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
   ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
-  name = create_tmp_var_name (".omp_copy_s");
+  name = create_tmp_var_name (".omp_copy_s", false);
   name = build_decl (gimple_location (stmt),
 		     TYPE_DECL, name, ctx->record_type);
   TYPE_NAME (ctx->record_type) = name;
@@ -3094,7 +3094,7 @@ scan_omp_target (gomp_target *stmt, omp_context *outer_ctx)
   ctx = new_omp_context (stmt, outer_ctx);
   ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
   ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
-  name = create_tmp_var_name (".omp_data_t");
+  name = create_tmp_var_name (".omp_data_t", false);
   name = build_decl (gimple_location (stmt),
 		     TYPE_DECL, name, ctx->record_type);
   DECL_ARTIFICIAL (name) = 1;
@@ -3156,7 +3156,7 @@ scan_omp_teams (gomp_teams *stmt, omp_context *outer_ctx)
   gcc_assert (taskreg_nesting_level == 1);
   ctx->field_map = splay_tree_new (splay_tree_compare_pointers, 0, 0);
   ctx->record_type = lang_hooks.types.make_type (RECORD_TYPE);
-  tree name = create_tmp_var_name (".omp_data_s");
+  tree name = create_tmp_var_name (".omp_data_s", false);
   name = build_decl (gimple_location (stmt),
 		     TYPE_DECL, name, ctx->record_type);
   DECL_ARTIFICIAL (name) = 1;
diff --git a/gcc/omp-oacc-neuter-broadcast.cc b/gcc/omp-oacc-neuter-broadcast.cc
index 6328253018e..9b94502671d 100644
--- a/gcc/omp-oacc-neuter-broadcast.cc
+++ b/gcc/omp-oacc-neuter-broadcast.cc
@@ -1647,7 +1647,7 @@ oacc_do_neutering (unsigned HOST_WIDE_INT bounds_lo,
       if (ws_prop)
 	{
 	  tree record_type = lang_hooks.types.make_type (RECORD_TYPE);
-	  tree name = create_tmp_var_name (".oacc_ws_data_s");
+	  tree name = create_tmp_var_name (".oacc_ws_data_s", false);
 	  name = build_decl (UNKNOWN_LOCATION, TYPE_DECL, name, record_type);
 	  DECL_ARTIFICIAL (name) = 1;
 	  DECL_NAMELESS (name) = 1;
diff --git a/gcc/symtab-thunks.cc b/gcc/symtab-thunks.cc
index 4c04235c41b..d79edb4c5d6 100644
--- a/gcc/symtab-thunks.cc
+++ b/gcc/symtab-thunks.cc
@@ -534,7 +534,7 @@ expand_thunk (cgraph_node *node, bool output_asm_thunks,
 	  tree p = DECL_STRUCT_FUNCTION (alias)->static_chain_decl;
 	  tree type = TREE_TYPE (p);
 	  tree decl = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
-				  PARM_DECL, create_tmp_var_name ("CHAIN"),
+				  PARM_DECL, create_tmp_var_name ("CHAIN", false),
 				  type);
 	  DECL_ARTIFICIAL (decl) = 1;
 	  DECL_IGNORED_P (decl) = 1;
diff --git a/gcc/testsuite/gfortran.dg/char_cast_1.f90 b/gcc/testsuite/gfortran.dg/char_cast_1.f90
index 02e695d2d7b..b5e0aeb6ecb 100644
--- a/gcc/testsuite/gfortran.dg/char_cast_1.f90
+++ b/gcc/testsuite/gfortran.dg/char_cast_1.f90
@@ -27,4 +27,4 @@
 end
 ! The sign that all is well is that [S.6][1] appears twice.
 ! Platform dependent variations are [S$6][1], [__S_6][1], [S___6][1]
-! { dg-final { scan-tree-dump-times "6\\\]\\\[1\\\]" 2 "original" } }
+! { dg-final { scan-tree-dump-times "2\\\]\\\[1\\\]" 2 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/vector_subscript_4.f90 b/gcc/testsuite/gfortran.dg/vector_subscript_4.f90
index d726d424ab7..382745fbb2c 100644
--- a/gcc/testsuite/gfortran.dg/vector_subscript_4.f90
+++ b/gcc/testsuite/gfortran.dg/vector_subscript_4.f90
@@ -9,4 +9,4 @@
      integer :: i(-1:1) = 1, j(3) = 1, k(3)
       k = j((/1,1,1/)+i)
       end
-! { dg-final { scan-tree-dump-times "A\.2\\\[3\\\]" 1 "original" } }
+! { dg-final { scan-tree-dump-times "A\.1\\\[3\\\]" 1 "original" } }
diff --git a/gcc/tree-nested.cc b/gcc/tree-nested.cc
index 1418e1f7f56..f6ec2997e61 100644
--- a/gcc/tree-nested.cc
+++ b/gcc/tree-nested.cc
@@ -473,7 +473,7 @@ get_chain_decl (struct nesting_info *info)
 	 close to the truth, since the initial value does come from
 	 the caller.  */
       decl = build_decl (DECL_SOURCE_LOCATION (info->context),
-			 PARM_DECL, create_tmp_var_name ("CHAIN"), type);
+			 PARM_DECL, create_tmp_var_name ("CHAIN", false), type);
       DECL_ARTIFICIAL (decl) = 1;
       DECL_IGNORED_P (decl) = 1;
       TREE_USED (decl) = 1;
diff --git a/gcc/tree-parloops.cc b/gcc/tree-parloops.cc
index dfb75c369d6..8d0ea9fb941 100644
--- a/gcc/tree-parloops.cc
+++ b/gcc/tree-parloops.cc
@@ -2109,7 +2109,7 @@ separate_decls_in_region (edge entry, edge exit,
       /* Create the type for the structure to store the ssa names to.  */
       type = lang_hooks.types.make_type (RECORD_TYPE);
       type_name = build_decl (UNKNOWN_LOCATION,
-			      TYPE_DECL, create_tmp_var_name (".paral_data"),
+			      TYPE_DECL, create_tmp_var_name (".paral_data", false),
 			      type);
       TYPE_NAME (type) = type_name;
 
diff --git a/gcc/tree-switch-conversion.cc b/gcc/tree-switch-conversion.cc
index c08c22039c9..caa1c43bc14 100644
--- a/gcc/tree-switch-conversion.cc
+++ b/gcc/tree-switch-conversion.cc
@@ -671,7 +671,7 @@ switch_conversion::build_one_array (int num, tree arr_index_type,
       TREE_STATIC (decl) = 1;
       DECL_INITIAL (decl) = ctor;
 
-      DECL_NAME (decl) = create_tmp_var_name ("CSWTCH");
+      DECL_NAME (decl) = create_tmp_var_name ("CSWTCH", true);
       DECL_ARTIFICIAL (decl) = 1;
       DECL_IGNORED_P (decl) = 1;
       TREE_CONSTANT (decl) = 1;
