diff --git a/gcc/tree-ssa-math-opts.c b/gcc/tree-ssa-math-opts.c
index 65e7ffe..930d612 100644
--- a/gcc/tree-ssa-math-opts.c
+++ b/gcc/tree-ssa-math-opts.c
@@ -2028,9 +2028,6 @@ find_bswap (gimple stmt, tree *alias_set, tree *vuse, int *size)
   return source_expr;
 }
 
-/* Find manual byte swap implementations and turn them into a bswap
-   builtin invokation.  */
-
 namespace {
 
 const pass_data pass_data_optimize_bswap =
@@ -2064,6 +2061,102 @@ public:
 
 }; // class pass_optimize_bswap
 
+/* Perform the bswap optimization: replace the statement STMT at GSI
+   by a load of type LOAD_TYPE with VUSE and set-alias ALIAS_TYPE if
+   a memory source is involved (ALIAS_TYPE is non null), followed by
+   the builtin bswap invocation in FNDECL. SRC gives the source on
+   which STMT is operating a byteswap and TYPE_SIZE gives the size of
+   the expression involved for maintaining some statistics.  */
+
+static void
+bswap_replace (gimple stmt, gimple_stmt_iterator *gsi, tree bswap_src,
+	       tree fndecl, tree bswap_type, tree load_type, tree alias_type,
+	       tree vuse, int type_size)
+{
+  tree bswap_tmp;
+  gimple call;
+
+  /* Need to load the value from memory first.  */
+  if (alias_type)
+    {
+      tree addr_expr, addr_tmp, val_expr, val_tmp;
+      tree load_offset_ptr;
+      gimple addr_stmt, load_stmt;
+
+      /*  Compute address to load from and cast according to the size
+	  of the load.  */
+      addr_expr = build_fold_addr_expr (unshare_expr (bswap_src));
+      if (is_gimple_min_invariant (addr_expr))
+	addr_tmp = addr_expr;
+      else
+	{
+	  addr_tmp = make_temp_ssa_name (TREE_TYPE (addr_expr), NULL,
+					 "load_src");
+	  addr_stmt = gimple_build_assign (addr_tmp, addr_expr);
+	  gsi_insert_before (gsi, addr_stmt, GSI_SAME_STMT);
+	}
+
+      /* Perform the load.  */
+      load_offset_ptr = build_int_cst (alias_type, 0);
+      val_tmp = make_temp_ssa_name (load_type, NULL, "load_dst");
+      val_expr = fold_build2 (MEM_REF, load_type, addr_tmp,
+			      load_offset_ptr);
+      load_stmt = gimple_build_assign (val_tmp, val_expr);
+      gimple_set_vuse (load_stmt, vuse);
+      gsi_insert_before (gsi, load_stmt, GSI_SAME_STMT);
+      bswap_src = val_tmp;
+    }
+
+  if (type_size == 16)
+    bswap_stats.found_16bit++;
+  else if (type_size == 32)
+    bswap_stats.found_32bit++;
+  else
+    bswap_stats.found_64bit++;
+
+  bswap_tmp = bswap_src;
+
+  /* Convert the src expression if necessary.  */
+  if (!useless_type_conversion_p (TREE_TYPE (bswap_tmp), bswap_type))
+    {
+      gimple convert_stmt;
+      bswap_tmp = make_temp_ssa_name (bswap_type, NULL, "bswapsrc");
+      convert_stmt = gimple_build_assign_with_ops (NOP_EXPR, bswap_tmp,
+						   bswap_src, NULL);
+      gsi_insert_before (gsi, convert_stmt, GSI_SAME_STMT);
+    }
+
+  call = gimple_build_call (fndecl, 1, bswap_tmp);
+
+  bswap_tmp = gimple_assign_lhs (stmt);
+
+  /* Convert the result if necessary.  */
+  if (!useless_type_conversion_p (TREE_TYPE (bswap_tmp), bswap_type))
+    {
+      gimple convert_stmt;
+      bswap_tmp = make_temp_ssa_name (bswap_type, NULL, "bswapdst");
+      convert_stmt = gimple_build_assign_with_ops (NOP_EXPR,
+						   gimple_assign_lhs (stmt),
+						   bswap_tmp, NULL);
+      gsi_insert_after (gsi, convert_stmt, GSI_SAME_STMT);
+    }
+
+  gimple_call_set_lhs (call, bswap_tmp);
+
+  if (dump_file)
+    {
+      fprintf (dump_file, "%d bit bswap implementation found at: ",
+	       (int)type_size);
+      print_gimple_stmt (dump_file, stmt, 0, 0);
+    }
+
+  gsi_insert_after (gsi, call, GSI_SAME_STMT);
+  gsi_remove (gsi, true);
+}
+
+/* Find manual byte swap implementations and turn them into a bswap
+   builtin invokation.  */
+
 unsigned int
 pass_optimize_bswap::execute (function *fun)
 {
@@ -2123,10 +2216,8 @@ pass_optimize_bswap::execute (function *fun)
         {
 	  gimple stmt = gsi_stmt (gsi);
 	  tree bswap_src, bswap_type, load_type, alias_type, vuse = NULL_TREE;
-	  tree bswap_tmp;
 	  tree fndecl = NULL_TREE;
 	  int type_size;
-	  gimple call;
 
 	  if (!is_gimple_assign (stmt)
 	      || gimple_assign_rhs_code (stmt) != BIT_IOR_EXPR)
@@ -2170,85 +2261,9 @@ pass_optimize_bswap::execute (function *fun)
 	  if (!fndecl)
 	    continue;
 
-
-	  /* Need to load the value from memory first.  */
-	  if (alias_type)
-	    {
-	      tree addr_expr, addr_tmp, val_expr, val_tmp;
-	      tree load_offset_ptr;
-	      gimple addr_stmt, load_stmt;
-
-	      changed = true;
-
-	      /*  Compute address to load from and cast according to the size
-		  of the load.  */
-	      addr_expr = build_fold_addr_expr (unshare_expr (bswap_src));
-	      if (is_gimple_min_invariant (addr_expr))
-		addr_tmp = addr_expr;
-	      else
-		{
-		  addr_tmp = make_temp_ssa_name (TREE_TYPE (addr_expr), NULL,
-						 "load_src");
-		  addr_stmt = gimple_build_assign (addr_tmp, addr_expr);
-		  gsi_insert_before (&gsi, addr_stmt, GSI_SAME_STMT);
-		}
-
-	      /* Perform the load.  */
-	      load_offset_ptr = build_int_cst (alias_type, 0);
-	      val_tmp = make_temp_ssa_name (load_type, NULL, "load_dst");
-	      val_expr = fold_build2 (MEM_REF, load_type, addr_tmp,
-				      load_offset_ptr);
-	      load_stmt = gimple_build_assign (val_tmp, val_expr);
-	      gimple_set_vuse (load_stmt, vuse);
-	      gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
-	      bswap_src = val_tmp;
-	    }
-
+	  bswap_replace (stmt, &gsi, bswap_src, fndecl, bswap_type, load_type,
+			 alias_type, vuse, type_size);
 	  changed = true;
-	  if (type_size == 16)
-	    bswap_stats.found_16bit++;
-	  else if (type_size == 32)
-	    bswap_stats.found_32bit++;
-	  else
-	    bswap_stats.found_64bit++;
-
-	  bswap_tmp = bswap_src;
-
-	  /* Convert the src expression if necessary.  */
-	  if (!useless_type_conversion_p (TREE_TYPE (bswap_tmp), bswap_type))
-	    {
-	      gimple convert_stmt;
-	      bswap_tmp = make_temp_ssa_name (bswap_type, NULL, "bswapsrc");
-	      convert_stmt = gimple_build_assign_with_ops
-		  		(NOP_EXPR, bswap_tmp, bswap_src, NULL);
-	      gsi_insert_before (&gsi, convert_stmt, GSI_SAME_STMT);
-	    }
-
-	  call = gimple_build_call (fndecl, 1, bswap_tmp);
-
-	  bswap_tmp = gimple_assign_lhs (stmt);
-
-	  /* Convert the result if necessary.  */
-	  if (!useless_type_conversion_p (TREE_TYPE (bswap_tmp), bswap_type))
-	    {
-	      gimple convert_stmt;
-	      bswap_tmp = make_temp_ssa_name (bswap_type, NULL, "bswapdst");
-	      convert_stmt = gimple_build_assign_with_ops
-			(NOP_EXPR, gimple_assign_lhs (stmt), bswap_tmp, NULL);
-	      gsi_insert_after (&gsi, convert_stmt, GSI_SAME_STMT);
-	    }
-
-	  gimple_call_set_lhs (call, bswap_tmp);
-
-	  if (dump_file)
-	    {
-	      fprintf (dump_file, "%d bit bswap implementation found at: ",
-		       (int)type_size);
-	      print_gimple_stmt (dump_file, stmt, 0, 0);
-	    }
-
-	  gsi_insert_after (&gsi, call, GSI_SAME_STMT);
-	  gsi_remove (&gsi, true);
 	}
     }
 
