diff --git a/gcc/gimple-streamer-out.c b/gcc/gimple-streamer-out.c
index 22bcbca..b044f35 100644
--- a/gcc/gimple-streamer-out.c
+++ b/gcc/gimple-streamer-out.c
@@ -47,6 +47,27 @@ output_phi (struct output_block *ob, gimple phi)
     }
 }

+static tree
+clear_gimple_op_block_r (tree *tp, int *walk_subtrees, void *data)
+{
+  if (EXPR_P (*tp))
+    {
+      TREE_SET_BLOCK (*tp, NULL);
+    }
+  return NULL_TREE;
+}
+
+static void
+clear_block_for_debug_stmt (gimple stmt)
+{
+  gcc_assert (is_gimple_debug (stmt));
+  if (gimple_debug_source_bind_p (stmt))
+    walk_tree (gimple_debug_source_bind_get_value_ptr (stmt),
+              clear_gimple_op_block_r, NULL, NULL);
+  else
+    walk_tree (gimple_debug_bind_get_value_ptr (stmt),
+              clear_gimple_op_block_r, NULL, NULL);
+}

 /* Emit statement STMT on the main stream of output block OB.  */

@@ -77,7 +98,13 @@ output_gimple_stmt (struct output_block *ob, gimple stmt)
   lto_output_location (ob, LOCATION_LOCUS (gimple_location (stmt)));

   /* Emit the lexical block holding STMT.  */
-  stream_write_tree (ob, gimple_block (stmt), true);
+  if (!is_gimple_debug (stmt))
+    stream_write_tree (ob, gimple_block (stmt), true);
+  else
+    {
+      clear_block_for_debug_stmt (stmt);
+      stream_write_tree (ob, NULL, true);
+    }

   /* Emit the operands.  */
   switch (gimple_code (stmt))

diff --git a/gcc/tree-streamer-out.c b/gcc/tree-streamer-out.c
index ab86b1e..fa89519 100644
--- a/gcc/tree-streamer-out.c
+++ b/gcc/tree-streamer-out.c
@@ -474,6 +474,15 @@ write_ts_decl_minimal_tree_pointers (struct
output_block *ob, tree expr,
   lto_output_location (ob, LOCATION_LOCUS (DECL_SOURCE_LOCATION (expr)));
 }

+static tree
+clear_gimple_op_block_r (tree *tp, int *walk_subtrees, void *data)
+{
+  if (EXPR_P (*tp))
+    {
+      TREE_SET_BLOCK (*tp, NULL);
+    }
+  return NULL_TREE;
+}

 /* Write all pointer fields in the TS_DECL_COMMON structure of EXPR to
