re PR debug/66691 (ICE on valid code at -O3 with -g enabled in simplify_subreg, at...
[gcc.git] / gcc / ipa-split.c
index 98dbc63e2fb08e6738a864f847448ac2ef05fc34..13d9a6447e5248a03bdb2b8ea0faeafbf94dd918 100644 (file)
@@ -1,5 +1,5 @@
 /* Function splitting pass
-   Copyright (C) 2010-2014 Free Software Foundation, Inc.
+   Copyright (C) 2010-2015 Free Software Foundation, Inc.
    Contributed by Jan Hubicka  <jh@suse.cz>
 
 This file is part of GCC.
@@ -77,15 +77,14 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "alias.h"
+#include "symtab.h"
+#include "options.h"
 #include "tree.h"
+#include "fold-const.h"
 #include "predict.h"
-#include "vec.h"
-#include "hashtab.h"
-#include "hash-set.h"
-#include "machmode.h"
 #include "tm.h"
 #include "hard-reg-set.h"
-#include "input.h"
 #include "function.h"
 #include "dominance.h"
 #include "cfg.h"
@@ -94,32 +93,36 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
 #include "gimple-expr.h"
-#include "is-a.h"
 #include "gimple.h"
 #include "stringpool.h"
-#include "expr.h"
+#include "rtl.h"
+#include "flags.h"
+#include "insn-config.h"
+#include "expmed.h"
+#include "dojump.h"
+#include "explow.h"
 #include "calls.h"
+#include "emit-rtl.h"
+#include "varasm.h"
+#include "stmt.h"
+#include "expr.h"
 #include "gimplify.h"
 #include "gimple-iterator.h"
 #include "gimplify-me.h"
 #include "gimple-walk.h"
 #include "target.h"
-#include "hash-map.h"
-#include "plugin-api.h"
-#include "ipa-ref.h"
 #include "cgraph.h"
 #include "alloc-pool.h"
+#include "symbol-summary.h"
 #include "ipa-prop.h"
 #include "gimple-ssa.h"
 #include "tree-cfg.h"
 #include "tree-phinodes.h"
 #include "ssa-iterators.h"
-#include "stringpool.h"
 #include "tree-ssanames.h"
 #include "tree-into-ssa.h"
 #include "tree-dfa.h"
 #include "tree-pass.h"
-#include "flags.h"
 #include "diagnostic.h"
 #include "tree-dump.h"
 #include "tree-inline.h"
@@ -248,8 +251,6 @@ verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
 
   while (!worklist.is_empty ())
     {
-      gimple_stmt_iterator bsi;
-
       bb = worklist.pop ();
       FOR_EACH_EDGE (e, ei, bb->preds)
        if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
@@ -259,7 +260,8 @@ verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
                                                e->src->index));
            worklist.safe_push (e->src);
          }
-      for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
+      for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
+          gsi_next (&bsi))
        {
          gimple stmt = gsi_stmt (bsi);
          if (is_gimple_debug (stmt))
@@ -271,15 +273,16 @@ verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
              ok = false;
              goto done;
            }
-         if (gimple_code (stmt) == GIMPLE_LABEL
-             && test_nonssa_use (stmt, gimple_label_label (stmt),
-                                 NULL_TREE, non_ssa_vars))
-           {
-             ok = false;
-             goto done;
-           }
+         if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
+           if (test_nonssa_use (stmt, gimple_label_label (label_stmt),
+                                NULL_TREE, non_ssa_vars))
+             {
+               ok = false;
+               goto done;
+             }
        }
-      for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
+      for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
+          gsi_next (&bsi))
        {
          if (walk_stmt_load_store_addr_ops
              (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
@@ -293,10 +296,11 @@ verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
        {
          if (e->dest != return_bb)
            continue;
-         for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi);
+         for (gphi_iterator bsi = gsi_start_phis (return_bb);
+              !gsi_end_p (bsi);
               gsi_next (&bsi))
            {
-             gimple stmt = gsi_stmt (bsi);
+             gphi *stmt = bsi.phi ();
              tree op = gimple_phi_arg_def (stmt, e->dest_idx);
 
              if (virtual_operand_p (gimple_phi_result (stmt)))
@@ -319,15 +323,17 @@ verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
       {
         gimple_stmt_iterator bsi;
         for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
-         if (gimple_code (gsi_stmt (bsi)) == GIMPLE_LABEL
-             && test_nonssa_use (gsi_stmt (bsi),
-                                 gimple_label_label (gsi_stmt (bsi)),
-                                 NULL_TREE, non_ssa_vars))
+         if (glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (bsi)))
            {
-             ok = false;
-             goto done;
+             if (test_nonssa_use (label_stmt,
+                                  gimple_label_label (label_stmt),
+                                  NULL_TREE, non_ssa_vars))
+               {
+                 ok = false;
+                 goto done;
+               }
            }
-         else if (gimple_code (gsi_stmt (bsi)) != GIMPLE_LABEL)
+         else
            break;
       }
     
@@ -369,9 +375,10 @@ check_forbidden_calls (gimple stmt)
       basic_block use_bb, forbidden_bb;
       enum tree_code code;
       edge true_edge, false_edge;
-      gimple use_stmt = USE_STMT (use_p);
+      gcond *use_stmt;
 
-      if (gimple_code (use_stmt) != GIMPLE_COND)
+      use_stmt = dyn_cast <gcond *> (USE_STMT (use_p));
+      if (!use_stmt)
        continue;
 
       /* Assuming canonical form for GIMPLE_COND here, with constant
@@ -443,7 +450,7 @@ consider_split (struct split_point *current, bitmap non_ssa_vars,
   unsigned int call_overhead;
   edge e;
   edge_iterator ei;
-  gimple_stmt_iterator bsi;
+  gphi_iterator bsi;
   unsigned int i;
   int incoming_freq = 0;
   tree retval;
@@ -501,7 +508,7 @@ consider_split (struct split_point *current, bitmap non_ssa_vars,
      incoming from header are the same.  */
   for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
     {
-      gimple stmt = gsi_stmt (bsi);
+      gphi *stmt = bsi.phi ();
       tree val = NULL;
 
       if (virtual_operand_p (gimple_phi_result (stmt)))
@@ -576,6 +583,31 @@ consider_split (struct split_point *current, bitmap non_ssa_vars,
       return;
     }
 
+  /* Splitting functions brings the target out of comdat group; this will
+     lead to code duplication if the function is reused by other unit.
+     Limit this duplication.  This is consistent with limit in tree-sra.c  
+     FIXME: with LTO we ought to be able to do better!  */
+  if (DECL_ONE_ONLY (current_function_decl)
+      && current->split_size >= (unsigned int) MAX_INLINE_INSNS_AUTO)
+    {
+      if (dump_file && (dump_flags & TDF_DETAILS))
+       fprintf (dump_file,
+                "  Refused: function is COMDAT and tail is too large\n");
+      return;
+    }
+  /* For comdat functions also reject very small tails; those will likely get
+     inlined back and we do not want to risk the duplication overhead.
+     FIXME: with LTO we ought to be able to do better!  */
+  if (DECL_ONE_ONLY (current_function_decl)
+      && current->split_size
+        <= (unsigned int) PARAM_VALUE (PARAM_EARLY_INLINING_INSNS) / 2)
+    {
+      if (dump_file && (dump_flags & TDF_DETAILS))
+       fprintf (dump_file,
+                "  Refused: function is COMDAT and tail is too small\n");
+      return;
+    }
+
   /* FIXME: we currently can pass only SSA function parameters to the split
      arguments.  Once parm_adjustment infrastructure is supported by cloning,
      we can pass more than that.  */
@@ -673,15 +705,15 @@ consider_split (struct split_point *current, bitmap non_ssa_vars,
      for the return value.  If there are other PHIs, give up.  */
   if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
     {
-      gimple_stmt_iterator psi;
+      gphi_iterator psi;
 
       for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
-       if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi)))
+       if (!virtual_operand_p (gimple_phi_result (psi.phi ()))
            && !(retval
                 && current->split_part_set_retval
                 && TREE_CODE (retval) == SSA_NAME
                 && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
-                && SSA_NAME_DEF_STMT (retval) == gsi_stmt (psi)))
+                && SSA_NAME_DEF_STMT (retval) == psi.phi ()))
          {
            if (dump_file && (dump_flags & TDF_DETAILS))
              fprintf (dump_file,
@@ -722,7 +754,8 @@ consider_split (struct split_point *current, bitmap non_ssa_vars,
    of the form:
    <retval> = tmp_var;
    return <retval>
-   but return_bb can not be more complex than this.
+   but return_bb can not be more complex than this (except for
+   -fsanitize=thread we allow TSAN_FUNC_EXIT () internal call in there).
    If nothing is found, return the exit block.
 
    When there are multiple RETURN statement, chose one with return value,
@@ -762,11 +795,18 @@ find_return_bb (void)
                   || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
               && retval == gimple_assign_lhs (stmt))
        ;
-      else if (gimple_code (stmt) == GIMPLE_RETURN)
+      else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
        {
          found_return = true;
-         retval = gimple_return_retval (stmt);
+         retval = gimple_return_retval (return_stmt);
        }
+      /* For -fsanitize=thread, allow also TSAN_FUNC_EXIT () in the return
+        bb.  */
+      else if ((flag_sanitize & SANITIZE_THREAD)
+              && is_gimple_call (stmt)
+              && gimple_call_internal_p (stmt)
+              && gimple_call_internal_fn (stmt) == IFN_TSAN_FUNC_EXIT)
+       ;
       else
        break;
     }
@@ -783,8 +823,8 @@ find_retval (basic_block return_bb)
 {
   gimple_stmt_iterator bsi;
   for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
-    if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
-      return gimple_return_retval (gsi_stmt (bsi));
+    if (greturn *return_stmt = dyn_cast <greturn *> (gsi_stmt (bsi)))
+      return gimple_return_retval (return_stmt);
     else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
             && !gimple_clobber_p (gsi_stmt (bsi)))
       return gimple_assign_rhs1 (gsi_stmt (bsi));
@@ -861,12 +901,12 @@ visit_bb (basic_block bb, basic_block return_bb,
          bitmap set_ssa_names, bitmap used_ssa_names,
          bitmap non_ssa_vars)
 {
-  gimple_stmt_iterator bsi;
   edge e;
   edge_iterator ei;
   bool can_split = true;
 
-  for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
+  for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
+       gsi_next (&bsi))
     {
       gimple stmt = gsi_stmt (bsi);
       tree op;
@@ -935,9 +975,10 @@ visit_bb (basic_block bb, basic_block return_bb,
                                                   mark_nonssa_use,
                                                   mark_nonssa_use);
     }
-  for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
+  for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
+       gsi_next (&bsi))
     {
-      gimple stmt = gsi_stmt (bsi);
+      gphi *stmt = bsi.phi ();
       unsigned int i;
 
       if (virtual_operand_p (gimple_phi_result (stmt)))
@@ -959,9 +1000,11 @@ visit_bb (basic_block bb, basic_block return_bb,
   FOR_EACH_EDGE (e, ei, bb->succs)
     if (e->dest == return_bb)
       {
-       for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
+       for (gphi_iterator bsi = gsi_start_phis (return_bb);
+            !gsi_end_p (bsi);
+            gsi_next (&bsi))
          {
-           gimple stmt = gsi_stmt (bsi);
+           gphi *stmt = bsi.phi ();
            tree op = gimple_phi_arg_def (stmt, e->dest_idx);
 
            if (virtual_operand_p (gimple_phi_result (stmt)))
@@ -1024,12 +1067,11 @@ typedef struct
    the component used by consider_split.  */
 
 static void
-find_split_points (int overall_time, int overall_size)
+find_split_points (basic_block return_bb, int overall_time, int overall_size)
 {
   stack_entry first;
   vec<stack_entry> stack = vNULL;
   basic_block bb;
-  basic_block return_bb = find_return_bb ();
   struct split_point current;
 
   current.header_time = overall_time;
@@ -1173,33 +1215,19 @@ find_split_points (int overall_time, int overall_size)
   BITMAP_FREE (current.ssa_names_to_pass);
 }
 
-/* Build and insert initialization of returned bounds RETBND
-   for returned value RETVAL.  Statements are inserted after
-   a statement pointed by GSI and GSI is modified to point to
-   the last inserted statement.  */
-
-static void
-insert_bndret_call_after (tree retbnd, tree retval, gimple_stmt_iterator *gsi)
-{
-  tree fndecl = targetm.builtin_chkp_function (BUILT_IN_CHKP_BNDRET);
-  gimple bndret = gimple_build_call (fndecl, 1, retval);
-  gimple_call_set_lhs (bndret, retbnd);
-  gsi_insert_after (gsi, bndret, GSI_CONTINUE_LINKING);
-}
 /* Split function at SPLIT_POINT.  */
 
 static void
-split_function (struct split_point *split_point)
+split_function (basic_block return_bb, struct split_point *split_point,
+               bool add_tsan_func_exit)
 {
   vec<tree> args_to_pass = vNULL;
   bitmap args_to_skip;
   tree parm;
   int num = 0;
   cgraph_node *node, *cur_node = cgraph_node::get (current_function_decl);
-  basic_block return_bb = find_return_bb ();
   basic_block call_bb;
-  gimple_stmt_iterator gsi;
-  gimple call;
+  gcall *call, *tsan_func_exit_call = NULL;
   edge e;
   edge_iterator ei;
   tree retval = NULL, real_retval = NULL, retbnd = NULL;
@@ -1302,9 +1330,10 @@ split_function (struct split_point *split_point)
   if (return_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
     {
       bool phi_p = false;
-      for (gsi = gsi_start_phis (return_bb); !gsi_end_p (gsi);)
+      for (gphi_iterator gsi = gsi_start_phis (return_bb);
+          !gsi_end_p (gsi);)
        {
-         gimple stmt = gsi_stmt (gsi);
+         gphi *stmt = gsi.phi ();
          if (!virtual_operand_p (gimple_phi_result (stmt)))
            {
              gsi_next (&gsi);
@@ -1323,7 +1352,9 @@ split_function (struct split_point *split_point)
          entry of the SESE region as the vuse of the call and the reaching
         vdef of the exit of the SESE region as the vdef of the call.  */
       if (!phi_p)
-       for (gsi = gsi_start_bb (return_bb); !gsi_end_p (gsi); gsi_next (&gsi))
+       for (gimple_stmt_iterator gsi = gsi_start_bb (return_bb);
+            !gsi_end_p (gsi);
+            gsi_next (&gsi))
          {
            gimple stmt = gsi_stmt (gsi);
            if (gimple_vuse (stmt))
@@ -1342,6 +1373,8 @@ split_function (struct split_point *split_point)
     (vNULL, NULL, args_to_skip, !split_part_return_p, split_point->split_bbs,
      split_point->entry_bb, "part");
 
+  node->split_part = true;
+
   /* Let's take a time profile for splitted function.  */
   node->tp_first_run = cur_node->tp_first_run + 1;
 
@@ -1372,7 +1405,7 @@ split_function (struct split_point *split_point)
   /* Create the basic block we place call into.  It is the entry basic block
      split after last label.  */
   call_bb = split_point->entry_bb;
-  for (gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
+  for (gimple_stmt_iterator gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
     if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
       {
        last_stmt = gsi_stmt (gsi);
@@ -1384,7 +1417,7 @@ split_function (struct split_point *split_point)
   remove_edge (e);
 
   /* Produce the call statement.  */
-  gsi = gsi_last_bb (call_bb);
+  gimple_stmt_iterator gsi = gsi_last_bb (call_bb);
   FOR_EACH_VEC_ELT (args_to_pass, i, arg)
     if (!is_gimple_val (arg))
       {
@@ -1482,11 +1515,18 @@ split_function (struct split_point *split_point)
          || DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))))
     gimple_call_set_return_slot_opt (call, true);
 
+  if (add_tsan_func_exit)
+    tsan_func_exit_call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
+
   /* Update return value.  This is bit tricky.  When we do not return,
      do nothing.  When we return we might need to update return_bb
      or produce a new return statement.  */
   if (!split_part_return_p)
-    gsi_insert_after (&gsi, call, GSI_NEW_STMT);
+    {
+      gsi_insert_after (&gsi, call, GSI_NEW_STMT);
+      if (tsan_func_exit_call)
+       gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
+    }
   else
     {
       e = make_edge (call_bb, return_bb,
@@ -1504,7 +1544,7 @@ split_function (struct split_point *split_point)
 
          if (real_retval && split_point->split_part_set_retval)
            {
-             gimple_stmt_iterator psi;
+             gphi_iterator psi;
 
              /* See if we need new SSA_NAME for the result.
                 When DECL_BY_REFERENCE is true, retval is actually pointer to
@@ -1517,13 +1557,13 @@ split_function (struct split_point *split_point)
                  /* See if there is PHI defining return value.  */
                  for (psi = gsi_start_phis (return_bb);
                       !gsi_end_p (psi); gsi_next (&psi))
-                   if (!virtual_operand_p (gimple_phi_result (gsi_stmt (psi))))
+                   if (!virtual_operand_p (gimple_phi_result (psi.phi ())))
                      break;
 
                  /* When there is PHI, just update its value.  */
                  if (TREE_CODE (retval) == SSA_NAME
                      && !gsi_end_p (psi))
-                   add_phi_arg (gsi_stmt (psi), retval, e, UNKNOWN_LOCATION);
+                   add_phi_arg (psi.phi (), retval, e, UNKNOWN_LOCATION);
                  /* Otherwise update the return BB itself.
                     find_return_bb allows at most one assignment to return value,
                     so update first statement.  */
@@ -1532,9 +1572,10 @@ split_function (struct split_point *split_point)
                      gimple_stmt_iterator bsi;
                      for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
                           gsi_next (&bsi))
-                       if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
+                       if (greturn *return_stmt
+                             = dyn_cast <greturn *> (gsi_stmt (bsi)))
                          {
-                           gimple_return_set_retval (gsi_stmt (bsi), retval);
+                           gimple_return_set_retval (return_stmt, retval);
                            break;
                          }
                        else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN
@@ -1574,29 +1615,30 @@ split_function (struct split_point *split_point)
                  if (!useless_type_conversion_p (TREE_TYPE (retval), restype))
                    {
                      gimple cpy;
-                     tree tem = create_tmp_reg (restype, NULL);
+                     tree tem = create_tmp_reg (restype);
                      tem = make_ssa_name (tem, call);
-                     cpy = gimple_build_assign_with_ops (NOP_EXPR, retval,
-                                                         tem, NULL_TREE);
+                     cpy = gimple_build_assign (retval, NOP_EXPR, tem);
                      gsi_insert_after (&gsi, cpy, GSI_NEW_STMT);
                      retval = tem;
                    }
                  /* Build bndret call to obtain returned bounds.  */
                  if (retbnd)
-                   insert_bndret_call_after (retbnd, retval, &gsi);
+                   chkp_insert_retbnd_call (retbnd, retval, &gsi);
                  gimple_call_set_lhs (call, retval);
                  update_stmt (call);
                }
            }
          else
            gsi_insert_after (&gsi, call, GSI_NEW_STMT);
+         if (tsan_func_exit_call)
+           gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
        }
       /* We don't use return block (there is either no return in function or
         multiple of them).  So create new basic block with return statement.
         */
       else
        {
-         gimple ret;
+         greturn *ret;
          if (split_point->split_part_set_retval
              && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
            {
@@ -1604,14 +1646,14 @@ split_function (struct split_point *split_point)
 
              if (chkp_function_instrumented_p (current_function_decl)
                  && BOUNDED_P (retval))
-               retbnd = create_tmp_reg (pointer_bounds_type_node, NULL);
+               retbnd = create_tmp_reg (pointer_bounds_type_node);
 
              /* We use temporary register to hold value when aggregate_value_p
                 is false.  Similarly for DECL_BY_REFERENCE we must avoid extra
                 copy.  */
              if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
                  && !DECL_BY_REFERENCE (retval))
-               retval = create_tmp_reg (TREE_TYPE (retval), NULL);
+               retval = create_tmp_reg (TREE_TYPE (retval));
              if (is_gimple_reg (retval))
                {
                  /* When returning by reference, there is only one SSA name
@@ -1631,7 +1673,9 @@ split_function (struct split_point *split_point)
           gsi_insert_after (&gsi, call, GSI_NEW_STMT);
          /* Build bndret call to obtain returned bounds.  */
          if (retbnd)
-           insert_bndret_call_after (retbnd, retval, &gsi);
+           chkp_insert_retbnd_call (retbnd, retval, &gsi);
+         if (tsan_func_exit_call)
+           gsi_insert_after (&gsi, tsan_func_exit_call, GSI_NEW_STMT);
          ret = gimple_build_return (retval);
          gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
        }
@@ -1668,7 +1712,7 @@ execute_split_functions (void)
   /* This can be relaxed; function might become inlinable after splitting
      away the uninlinable part.  */
   if (inline_edge_summary_vec.exists ()
-      && !inline_summary (node)->inlinable)
+      && !inline_summaries->get (node)->inlinable)
     {
       if (dump_file)
        fprintf (dump_file, "Not splitting: not inlinable.\n");
@@ -1709,6 +1753,7 @@ execute_split_functions (void)
        /* Local functions called once will be completely inlined most of time.  */
        || (!node->callers->next_caller && node->local.local))
       && !node->address_taken
+      && !node->has_aliases_p ()
       && (!flag_lto || !node->externally_visible))
     {
       if (dump_file)
@@ -1739,6 +1784,8 @@ execute_split_functions (void)
   /* Compute local info about basic blocks and determine function size/time.  */
   bb_info_vec.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
   memset (&best_split_point, 0, sizeof (best_split_point));
+  basic_block return_bb = find_return_bb ();
+  int tsan_exit_found = -1;
   FOR_EACH_BB_FN (bb, cfun)
     {
       int time = 0;
@@ -1765,16 +1812,37 @@ execute_split_functions (void)
                       freq, this_size, this_time);
              print_gimple_stmt (dump_file, stmt, 0, 0);
            }
+
+         if ((flag_sanitize & SANITIZE_THREAD)
+             && is_gimple_call (stmt)
+             && gimple_call_internal_p (stmt)
+             && gimple_call_internal_fn (stmt) == IFN_TSAN_FUNC_EXIT)
+           {
+             /* We handle TSAN_FUNC_EXIT for splitting either in the
+                return_bb, or in its immediate predecessors.  */
+             if ((bb != return_bb && !find_edge (bb, return_bb))
+                 || (tsan_exit_found != -1
+                     && tsan_exit_found != (bb != return_bb)))
+               {
+                 if (dump_file)
+                   fprintf (dump_file, "Not splitting: TSAN_FUNC_EXIT"
+                            " in unexpected basic block.\n");
+                 BITMAP_FREE (forbidden_dominators);
+                 bb_info_vec.release ();
+                 return 0;
+               }
+             tsan_exit_found = bb != return_bb;
+           }
        }
       overall_time += time;
       overall_size += size;
       bb_info_vec[bb->index].time = time;
       bb_info_vec[bb->index].size = size;
     }
-  find_split_points (overall_time, overall_size);
+  find_split_points (return_bb, overall_time, overall_size);
   if (best_split_point.split_bbs)
     {
-      split_function (&best_split_point);
+      split_function (return_bb, &best_split_point, tsan_exit_found == 1);
       BITMAP_FREE (best_split_point.ssa_names_to_pass);
       BITMAP_FREE (best_split_point.split_bbs);
       todo = TODO_update_ssa | TODO_cleanup_cfg;