diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index ab31eee859ea8031857ac922b7e5c97207921863..3a63255668009675d9ea5dbed5a44b8f4465f2da 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,15 @@
+2016-11-03  Martin Liska  <mliska@suse.cz>
+
+	* profile.c (instrument_values): Fix coding style.
+	(branch_prob): Use renamed function.
+	* tree-profile.c (init_ic_make_global_vars): Likewise.
+	(gimple_init_edge_profiler): Rename to
+	gimple_init_gcov_profiler.
+	tree_time_profiler_counter variable declaration.
+	(gimple_gen_time_profiler): Rewrite to do a direct gimple code
+	emission.
+	* value-prof.h: Remove an argument.
+
 2016-11-03  Richard Biener  <rguenther@suse.de>
 
 	* config/rs6000/rs6000.c (rs6000_xcoff_declare_object_name): Use
diff --git a/gcc/profile.c b/gcc/profile.c
index 2564f07abf7bd1c4ad70441e445b1b9128530c3b..ef38f988a3414e40b064e2866f1201a54b854f50 100644
--- a/gcc/profile.c
+++ b/gcc/profile.c
@@ -192,15 +192,9 @@ instrument_values (histogram_values values)
 	  gimple_gen_ior_profiler (hist, t, 0);
 	  break;
 
-  case HIST_TYPE_TIME_PROFILE:
-    {
-      basic_block bb =
-     split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
-      gimple_stmt_iterator gsi = gsi_start_bb (bb);
-
-      gimple_gen_time_profiler (t, 0, gsi);
-      break;
-    }
+	case HIST_TYPE_TIME_PROFILE:
+	  gimple_gen_time_profiler (t, 0);
+	  break;
 
 	default:
 	  gcc_unreachable ();
@@ -1305,7 +1299,7 @@ branch_prob (void)
     {
       unsigned n_instrumented;
 
-      gimple_init_edge_profiler ();
+      gimple_init_gcov_profiler ();
 
       n_instrumented = instrument_edges (el);
 
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 5f59bd839620a1adb5750e34dd33f4935588ba5d..5a0467fadaa735061a96395d0dd6af1760aa9530 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,9 @@
+2016-11-03  Martin Liska  <mliska@suse.cz>
+
+	* gcc.dg/no_profile_instrument_function-attr-1.c: Update scanned
+	output.
+	* gcc.dg/tree-prof/time-profiler-3.c: New test.
+
 2016-11-03  Bernd Edlinger  <bernd.edlinger@hotmail.de>
 
 	PR libgcc/78067
diff --git a/gcc/testsuite/gcc.dg/no_profile_instrument_function-attr-1.c b/gcc/testsuite/gcc.dg/no_profile_instrument_function-attr-1.c
index c93d1718b642feea42324a8b83539abc92a315fd..e0c2600b400c8612062fb0a18f3646d64e2e64a9 100644
--- a/gcc/testsuite/gcc.dg/no_profile_instrument_function-attr-1.c
+++ b/gcc/testsuite/gcc.dg/no_profile_instrument_function-attr-1.c
@@ -19,5 +19,5 @@ int main ()
 
 /* { dg-final { scan-tree-dump-times "__gcov0\\.main.* = PROF_edge_counter" 1 "optimized"} } */
 /* { dg-final { scan-tree-dump-times "__gcov_indirect_call_profiler_v2" 1 "optimized" } } */
-/* { dg-final { scan-tree-dump-times "__gcov_time_profiler" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "__gcov_time_profiler_counter = " 1 "optimized" } } */
 /* { dg-final { scan-tree-dump-times "__gcov_init" 1 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-prof/time-profiler-3.c b/gcc/testsuite/gcc.dg/tree-prof/time-profiler-3.c
new file mode 100644
index 0000000000000000000000000000000000000000..69ce026082851cfea723c80be463517e9f93498a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-prof/time-profiler-3.c
@@ -0,0 +1,22 @@
+/* { dg-options "-O2 -fdump-ipa-profile -fprofile-update=atomic" } */
+/* { dg-require-effective-target profile_update_atomic } */
+
+__attribute__ ((noinline))
+int foo()
+{
+  return 0;
+}
+
+__attribute__ ((noinline))
+int bar()
+{
+  return 1;
+}
+
+int main ()
+{
+  return foo ();
+}
+/* { dg-final-use-not-autofdo { scan-ipa-dump-times "Read tp_first_run: 0" 1 "profile"} } */
+/* { dg-final-use-not-autofdo { scan-ipa-dump-times "Read tp_first_run: 1" 1 "profile"} } */
+/* { dg-final-use-not-autofdo { scan-ipa-dump-times "Read tp_first_run: 2" 1 "profile"} } */
diff --git a/gcc/tree-profile.c b/gcc/tree-profile.c
index abeee92204e227fa5752ebf5aefa9fd961286c69..09a702f8bb0d2738cb9b5e0ea71e9e44506f5bae 100644
--- a/gcc/tree-profile.c
+++ b/gcc/tree-profile.c
@@ -56,9 +56,9 @@ static GTY(()) tree tree_interval_profiler_fn;
 static GTY(()) tree tree_pow2_profiler_fn;
 static GTY(()) tree tree_one_value_profiler_fn;
 static GTY(()) tree tree_indirect_call_profiler_fn;
-static GTY(()) tree tree_time_profiler_fn;
 static GTY(()) tree tree_average_profiler_fn;
 static GTY(()) tree tree_ior_profiler_fn;
+static GTY(()) tree tree_time_profiler_counter;
 
 
 static GTY(()) tree ic_void_ptr_var;
@@ -75,7 +75,7 @@ static GTY(()) tree ptr_void;
 static void
 init_ic_make_global_vars (void)
 {
-  tree  gcov_type_ptr;
+  tree gcov_type_ptr;
 
   ptr_void = build_pointer_type (void_type_node);
 
@@ -119,7 +119,7 @@ init_ic_make_global_vars (void)
 /* Create the type and function decls for the interface with gcov.  */
 
 void
-gimple_init_edge_profiler (void)
+gimple_init_gcov_profiler (void)
 {
   tree interval_profiler_fn_type;
   tree pow2_profiler_fn_type;
@@ -127,7 +127,6 @@ gimple_init_edge_profiler (void)
   tree gcov_type_ptr;
   tree ic_profiler_fn_type;
   tree average_profiler_fn_type;
-  tree time_profiler_fn_type;
   const char *profiler_fn_name;
   const char *fn_name;
 
@@ -201,17 +200,17 @@ gimple_init_edge_profiler (void)
 	= tree_cons (get_identifier ("leaf"), NULL,
 		     DECL_ATTRIBUTES (tree_indirect_call_profiler_fn));
 
-      /* void (*) (gcov_type *, gcov_type, void *)  */
-      time_profiler_fn_type
-	       = build_function_type_list (void_type_node,
-					  gcov_type_ptr, NULL_TREE);
-      fn_name = concat ("__gcov_time_profiler", fn_suffix, NULL);
-      tree_time_profiler_fn = build_fn_decl (fn_name, time_profiler_fn_type);
-      free (CONST_CAST (char *, fn_name));
-      TREE_NOTHROW (tree_time_profiler_fn) = 1;
-      DECL_ATTRIBUTES (tree_time_profiler_fn)
-	= tree_cons (get_identifier ("leaf"), NULL,
-		     DECL_ATTRIBUTES (tree_time_profiler_fn));
+      tree_time_profiler_counter
+	= build_decl (UNKNOWN_LOCATION, VAR_DECL,
+		      get_identifier ("__gcov_time_profiler_counter"),
+		      get_gcov_type ());
+      TREE_PUBLIC (tree_time_profiler_counter) = 1;
+      DECL_EXTERNAL (tree_time_profiler_counter) = 1;
+      TREE_STATIC (tree_time_profiler_counter) = 1;
+      DECL_ARTIFICIAL (tree_time_profiler_counter) = 1;
+      DECL_INITIAL (tree_time_profiler_counter) = NULL;
+
+      varpool_node::finalize_decl (tree_time_profiler_counter);
 
       /* void (*) (gcov_type *, gcov_type)  */
       average_profiler_fn_type
@@ -239,7 +238,6 @@ gimple_init_edge_profiler (void)
       DECL_ASSEMBLER_NAME (tree_pow2_profiler_fn);
       DECL_ASSEMBLER_NAME (tree_one_value_profiler_fn);
       DECL_ASSEMBLER_NAME (tree_indirect_call_profiler_fn);
-      DECL_ASSEMBLER_NAME (tree_time_profiler_fn);
       DECL_ASSEMBLER_NAME (tree_average_profiler_fn);
       DECL_ASSEMBLER_NAME (tree_ior_profiler_fn);
     }
@@ -426,7 +424,7 @@ gimple_gen_ic_func_profiler (void)
   if (c_node->only_called_directly_p ())
     return;
 
-  gimple_init_edge_profiler ();
+  gimple_init_gcov_profiler ();
 
   /* Insert code:
 
@@ -460,16 +458,74 @@ gimple_gen_ic_func_profiler (void)
    counter position and GSI is the iterator we place the counter.  */
 
 void
-gimple_gen_time_profiler (unsigned tag, unsigned base,
-                          gimple_stmt_iterator &gsi)
+gimple_gen_time_profiler (unsigned tag, unsigned base)
 {
-  tree ref_ptr = tree_coverage_counter_addr (tag, base);
-  gcall *call;
+  tree type = get_gcov_type ();
+  basic_block cond_bb
+    = split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
+
+  basic_block update_bb = split_edge (single_succ_edge (cond_bb));
+
+  edge true_edge = single_succ_edge (cond_bb);
+  true_edge->flags = EDGE_TRUE_VALUE;
+  true_edge->probability = PROB_VERY_UNLIKELY;
+  edge e
+    = make_edge (cond_bb, single_succ_edge (update_bb)->dest, EDGE_FALSE_VALUE);
+  e->probability = REG_BR_PROB_BASE - true_edge->probability;
+
+  gimple_stmt_iterator gsi = gsi_start_bb (cond_bb);
+  tree original_ref = tree_coverage_counter_ref (tag, base);
+  tree ref = force_gimple_operand_gsi (&gsi, original_ref, true, NULL_TREE,
+				       true, GSI_SAME_STMT);
+  tree one = build_int_cst (type, 1);
 
-  ref_ptr = force_gimple_operand_gsi (&gsi, ref_ptr,
-				      true, NULL_TREE, true, GSI_SAME_STMT);
-  call = gimple_build_call (tree_time_profiler_fn, 1, ref_ptr);
-  gsi_insert_before (&gsi, call, GSI_NEW_STMT);
+  /* Emit: if (counters[0] != 0).  */
+  gcond *cond = gimple_build_cond (EQ_EXPR, ref, build_int_cst (type, 0),
+				   NULL, NULL);
+  gsi_insert_before (&gsi, cond, GSI_NEW_STMT);
+
+  gsi = gsi_start_bb (update_bb);
+
+  /* Emit: counters[0] = ++__gcov_time_profiler_counter.  */
+  if (flag_profile_update == PROFILE_UPDATE_ATOMIC)
+    {
+      tree ptr = make_temp_ssa_name (type, NULL, "time_profiler_counter_ptr");
+      tree addr = build1 (ADDR_EXPR, build_pointer_type (type),
+			  tree_time_profiler_counter);
+      gassign *assign = gimple_build_assign (ptr, NOP_EXPR, addr);
+      gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
+      tree f = builtin_decl_explicit (LONG_LONG_TYPE_SIZE > 32
+				      ? BUILT_IN_ATOMIC_ADD_FETCH_8:
+				      BUILT_IN_ATOMIC_ADD_FETCH_4);
+      gcall *stmt = gimple_build_call (f, 3, ptr, one,
+				       build_int_cst (integer_type_node,
+						      MEMMODEL_RELAXED));
+      tree result_type = TREE_TYPE (TREE_TYPE (f));
+      tree tmp = make_temp_ssa_name (result_type, NULL, "time_profile");
+      gimple_set_lhs (stmt, tmp);
+      gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
+      tmp = make_temp_ssa_name (type, NULL, "time_profile");
+      assign = gimple_build_assign (tmp, NOP_EXPR,
+				    gimple_call_lhs (stmt));
+      gsi_insert_after (&gsi, assign, GSI_NEW_STMT);
+      assign = gimple_build_assign (original_ref, tmp);
+      gsi_insert_after (&gsi, assign, GSI_NEW_STMT);
+    }
+  else
+    {
+      tree tmp = make_temp_ssa_name (type, NULL, "time_profile");
+      gassign *assign = gimple_build_assign (tmp, tree_time_profiler_counter);
+      gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
+
+      tmp = make_temp_ssa_name (type, NULL, "time_profile");
+      assign = gimple_build_assign (tmp, PLUS_EXPR, gimple_assign_lhs (assign),
+				    one);
+      gsi_insert_after (&gsi, assign, GSI_NEW_STMT);
+      assign = gimple_build_assign (original_ref, tmp);
+      gsi_insert_after (&gsi, assign, GSI_NEW_STMT);
+      assign = gimple_build_assign (tree_time_profiler_counter, tmp);
+      gsi_insert_after (&gsi, assign, GSI_NEW_STMT);
+    }
 }
 
 /* Output instructions as GIMPLE trees to increment the average histogram
diff --git a/gcc/value-prof.h b/gcc/value-prof.h
index 07e2b3b584085361c0d8c0ddda660f0120d78272..02220acb206ea68a5d41e1d4cf2eb728ec69970e 100644
--- a/gcc/value-prof.h
+++ b/gcc/value-prof.h
@@ -96,15 +96,14 @@ bool check_ic_target (gcall *, struct cgraph_node *);
 
 
 /* In tree-profile.c.  */
-extern void gimple_init_edge_profiler (void);
+extern void gimple_init_gcov_profiler (void);
 extern void gimple_gen_edge_profiler (int, edge);
 extern void gimple_gen_interval_profiler (histogram_value, unsigned, unsigned);
 extern void gimple_gen_pow2_profiler (histogram_value, unsigned, unsigned);
 extern void gimple_gen_one_value_profiler (histogram_value, unsigned, unsigned);
 extern void gimple_gen_ic_profiler (histogram_value, unsigned, unsigned);
 extern void gimple_gen_ic_func_profiler (void);
-extern void gimple_gen_time_profiler (unsigned, unsigned,
-                                      gimple_stmt_iterator &);
+extern void gimple_gen_time_profiler (unsigned, unsigned);
 extern void gimple_gen_average_profiler (histogram_value, unsigned, unsigned);
 extern void gimple_gen_ior_profiler (histogram_value, unsigned, unsigned);
 extern void stream_out_histogram_value (struct output_block *, histogram_value);
diff --git a/libgcc/ChangeLog b/libgcc/ChangeLog
index e81444c841c0ef283e68c4d100bad89ffb2a62c6..e3a81f6b0261249149ba6cf75acb421a6dfdc891 100644
--- a/libgcc/ChangeLog
+++ b/libgcc/ChangeLog
@@ -1,3 +1,8 @@
+2016-11-03  Martin Liska  <mliska@suse.cz>
+
+	* libgcov-profiler.c (__gcov_time_profiler): Remove.
+	(__gcov_time_profiler_atomic): Likewise.
+
 2016-11-03  Bernd Edlinger  <bernd.edlinger@hotmail.de>
 
 	PR libgcc/78067
diff --git a/libgcc/libgcov-profiler.c b/libgcc/libgcov-profiler.c
index 38ed5f113e75e8244c059afbe2990acf2ebf5afb..4f0a40675a26778c39a878b2f4199124f89cb49d 100644
--- a/libgcc/libgcov-profiler.c
+++ b/libgcc/libgcov-profiler.c
@@ -342,30 +342,9 @@ __gcov_indirect_call_profiler_v2 (gcov_type value, void* cur_func)
 #ifdef L_gcov_time_profiler
 
 /* Counter for first visit of each function.  */
-static gcov_type function_counter;
+gcov_type __gcov_time_profiler_counter ATTRIBUTE_HIDDEN;
 
-/* Sets corresponding COUNTERS if there is no value.  */
-
-void
-__gcov_time_profiler (gcov_type* counters)
-{
-  if (!counters[0])
-    counters[0] = ++function_counter;
-}
-
-#if GCOV_SUPPORTS_ATOMIC
-/* Sets corresponding COUNTERS if there is no value.
-   Function is thread-safe.  */
-
-void
-__gcov_time_profiler_atomic (gcov_type* counters)
-{
-  if (!counters[0])
-    counters[0] = __atomic_add_fetch (&function_counter, 1, __ATOMIC_RELAXED);
-}
 #endif
-#endif
-
 
 #ifdef L_gcov_average_profiler
 /* Increase corresponding COUNTER by VALUE.  FIXME: Perhaps we want