diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 795baf1f3048ec3a7d9ed79038540bb7afa84dcc..a0eb6f1ece5a42e1febb297da9994d1a38987617 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,13 @@
+2005-08-20  Jakub Jelinek  <jakub@redhat.com>
+
+	* tree-pass.h (TDF_GRAPH): Define.
+	* tree-dump.c (dump_options): Don't set TDF_GRAPH in "all".
+	* passes.c (finish_optimization_passes): Only call
+	finish_graph_dump_file if TDF_GRAPH is set.
+	(execute_one_pass): Only call clean_graph_dump_file if dump_file !=
+	NULL.  Set TDF_GRAPH bit.
+	(execute_todo): Call print_rtl_graph_with_bb if TDF_GRAPH is set.
+
 2005-08-20  Richard Earnshaw  <richard.earnshaw@arm.com>
 
 	* arm.h (arm_stack_offsets): Add locals_base field.
diff --git a/gcc/passes.c b/gcc/passes.c
index f199989f9862c8a628e9f10282fae252e1a05505..6728e105c96f0a748f0794133e203655165bd22d 100644
--- a/gcc/passes.c
+++ b/gcc/passes.c
@@ -237,12 +237,12 @@ finish_optimization_passes (void)
   if (graph_dump_format != no_graph)
     for (i = TDI_end; (dfi = get_dump_file_info (i)) != NULL; ++i)
       if (dump_initialized_p (i)
-	  && (dfi->flags & TDF_RTL) != 0
+	  && (dfi->flags & TDF_GRAPH) != 0
 	  && (name = get_dump_file_name (i)) != NULL)
-        {
-          finish_graph_dump_file (name);
-          free (name);
-        }
+	{
+	  finish_graph_dump_file (name);
+	  free (name);
+	}
 
   timevar_pop (TV_DUMP);
 }
@@ -708,7 +708,13 @@ execute_todo (struct tree_opt_pass *pass, unsigned int flags, bool use_required)
         dump_function_to_file (current_function_decl,
                                dump_file, dump_flags);
       else if (properties & PROP_cfg)
-        print_rtl_with_bb (dump_file, get_insns ());
+	{
+	  print_rtl_with_bb (dump_file, get_insns ());
+
+	  if (graph_dump_format != no_graph
+	      && (dump_flags & TDF_GRAPH))
+	    print_rtl_graph_with_bb (dump_file_name, get_insns ());
+	}
       else
         print_rtl (dump_file, get_insns ());
 
@@ -782,10 +788,15 @@ execute_one_pass (struct tree_opt_pass *pass)
 	}
 
       if (initializing_dump
-          && graph_dump_format != no_graph
+	  && dump_file
+	  && graph_dump_format != no_graph
 	  && (pass->properties_provided & (PROP_cfg | PROP_rtl))
 	      == (PROP_cfg | PROP_rtl))
-        clean_graph_dump_file (dump_file_name);
+	{
+	  get_dump_file_info (pass->static_pass_number)->flags |= TDF_GRAPH;
+	  dump_flags |= TDF_GRAPH;
+	  clean_graph_dump_file (dump_file_name);
+	}
     }
 
   /* If a timevar is present, start it.  */
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 110168e15ab2f698953ca91ff1e3cbac45519908..39f7cd74cbc14f4961b2a8583ca36a548572bb42 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2005-08-20  Jakub Jelinek  <jakub@redhat.com>
+
+	* gcc.dg/20050811-1.c: New test.
+	* gcc.dg/20050811-2.c: New test.
+
 2005-08-20  Richard Earnshaw  <richard.earnshaw@arm.com>
 
 	* gcc.dg/arm-eabi1.c (main): Exit with 0 on success.
diff --git a/gcc/testsuite/gcc.dg/20050811-1.c b/gcc/testsuite/gcc.dg/20050811-1.c
new file mode 100644
index 0000000000000000000000000000000000000000..cfc181692c1274ded3833066262c025d1352e7d7
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/20050811-1.c
@@ -0,0 +1,20 @@
+/* Test whether -dav doesn't crash.  */
+/* { dg-do compile } */
+/* { dg-options "-O2 -dav" } */
+
+int foo (void)
+{
+  return 6;
+}
+
+int bar (int x)
+{
+  if (x < 0)
+    return foo () + 8;
+  else if (x > 0)
+    return 2 * foo ();
+  else
+    return foo ();
+}
+
+// { dg-final { cleanup-rtl-dump "*" } }
diff --git a/gcc/testsuite/gcc.dg/20050811-2.c b/gcc/testsuite/gcc.dg/20050811-2.c
new file mode 100644
index 0000000000000000000000000000000000000000..2f1d48b350e80b9fc5a61315684cd27b84f80f29
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/20050811-2.c
@@ -0,0 +1,20 @@
+/* Test whether -dov doesn't crash.  */
+/* { dg-do compile } */
+/* { dg-options "-O2 -dov" } */
+
+int foo (void)
+{
+  return 6;
+}
+
+int bar (int x)
+{
+  if (x < 0)
+    return foo () + 8;
+  else if (x > 0)
+    return 2 * foo ();
+  else
+    return foo ();
+}
+
+// { dg-final { cleanup-rtl-dump "postreload*" } }
diff --git a/gcc/tree-dump.c b/gcc/tree-dump.c
index 2beecc744568a34ad8561d4c7345b9901aa9b08b..632687108e81a9afe0ca3c19918ea2e947e4f7c9 100644
--- a/gcc/tree-dump.c
+++ b/gcc/tree-dump.c
@@ -752,7 +752,7 @@ static const struct dump_option_value_info dump_options[] =
   {"uid", TDF_UID},
   {"stmtaddr", TDF_STMTADDR},
   {"all", ~(TDF_RAW | TDF_SLIM | TDF_LINENO | TDF_TREE | TDF_RTL | TDF_IPA 
-	    | TDF_STMTADDR)},
+	    | TDF_STMTADDR | TDF_GRAPH)},
   {NULL, 0}
 };
 
diff --git a/gcc/tree-pass.h b/gcc/tree-pass.h
index 36b122c612793f731b9f2d6ffca4833a136d01e6..57dc029438eeea05da849250a3cebb600819dc22 100644
--- a/gcc/tree-pass.h
+++ b/gcc/tree-pass.h
@@ -67,6 +67,8 @@ enum tree_dump_index
 #define TDF_IPA		(1 << 11)	/* is an IPA dump */
 #define TDF_STMTADDR	(1 << 12)	/* Address of stmt.  */
 
+#define TDF_GRAPH	(1 << 13)	/* a graph dump is being emitted */
+
 extern char *get_dump_file_name (enum tree_dump_index);
 extern int dump_enabled_p (enum tree_dump_index);
 extern int dump_initialized_p (enum tree_dump_index);