This is the first patch for planned improvements to dump
infrastructure.  Please reference the discussion in
http://gcc.gnu.org/ml/gcc-patches/2011-10/msg02088.html.

The following small patch allows selective tree and rtl dumps on
stderr instead of named files.  Later -fopt-info can be implemented in
form of -fdump-xxx-stderr.

Bootstrapped and tested on x86_64 with one added testcase.  Okay for
trunk?

Thanks,
Sharad

2012-05-07   Sharad Singhai  <sing...@google.com>

        * doc/invoke.texi: Add documentation for new option.
        * tree-dump.c (dump_begin): Handle stderr appropriately.
        (dump_end): Likewise.
        (dump_switch_p_1): Likewise.
        * tree-pass.h (enum tree_dump_index): Add new constant.
        * testsuite/g++.dg/other/dump-stderr-1.C: New test.

Index: doc/invoke.texi
===================================================================
--- doc/invoke.texi     (revision 187265)
+++ doc/invoke.texi     (working copy)
@@ -5322,20 +5322,23 @@ Here are some examples showing uses of these optio
 
 @item -d@var{letters}
 @itemx -fdump-rtl-@var{pass}
+@itemx -fdump-rtl-@var{pass-stderr}
 @opindex d
 Says to make debugging dumps during compilation at times specified by
 @var{letters}.  This is used for debugging the RTL-based passes of the
 compiler.  The file names for most of the dumps are made by appending
 a pass number and a word to the @var{dumpname}, and the files are
-created in the directory of the output file.  Note that the pass
-number is computed statically as passes get registered into the pass
-manager.  Thus the numbering is not related to the dynamic order of
-execution of passes.  In particular, a pass installed by a plugin
-could have a number over 200 even if it executed quite early.
-@var{dumpname} is generated from the name of the output file, if
-explicitly specified and it is not an executable, otherwise it is the
-basename of the source file. These switches may have different effects
-when @option{-E} is used for preprocessing.
+created in the directory of the output file. If the @option{-stderr} is
+appended to the longer form of the dump option then the dump is
+done on stderr instead of named files. Note that the pass number is
+computed statically as passes get registered into the pass manager.
+Thus the numbering is not related to the dynamic order of execution of
+passes.  In particular, a pass installed by a plugin could have a
+number over 200 even if it executed quite early.  @var{dumpname} is
+generated from the name of the output file, if explicitly specified
+and it is not an executable, otherwise it is the basename of the
+source file. These switches may have different effects when
+@option{-E} is used for preprocessing.
 
 Debug dumps can be enabled with a @option{-fdump-rtl} switch or some
 @option{-d} option @var{letters}.  Here are the possible
@@ -5599,6 +5602,10 @@ These dumps are defined but always produce empty f
 @opindex fdump-rtl-all
 Produce all the dumps listed above.
 
+@item -fdump-rtl-all-stderr
+@opindex fdump-rtl-all-stderr
+Produce all the dumps on stderr.
+
 @item -dA
 @opindex dA
 Annotate the assembler output with miscellaneous debugging information.
@@ -5723,11 +5730,13 @@ counters for each function compiled.
 Control the dumping at various stages of processing the intermediate
 language tree to a file.  The file name is generated by appending a
 switch specific suffix to the source file name, and the file is
-created in the same directory as the output file.  If the
-@samp{-@var{options}} form is used, @var{options} is a list of
-@samp{-} separated options which control the details of the dump.  Not
-all options are applicable to all dumps; those that are not
-meaningful are ignored.  The following options are available
+created in the same directory as the output file unless the option
+@option{stderr} is given.  In case of @option{stderr}, the dump output is on
+the stderr. If the @samp{-@var{options}} form is used, @var{options}
+is a list of @samp{-} separated options which control the details or
+location of the dump.  Not all options are applicable to all dumps;
+those that are not meaningful are ignored.  The following options are
+available
 
 @table @samp
 @item address
@@ -5765,9 +5774,42 @@ Enable showing the tree dump for each statement.
 Enable showing the EH region number holding each statement.
 @item scev
 Enable showing scalar evolution analysis details.
+@item slim
+Inhibit dumping of members of a scope or body of a function merely
+because that scope has been reached.  Only dump such items when they
+are directly reachable by some other path.  When dumping pretty-printed
+trees, this option inhibits dumping the bodies of control structures.
+@item stderr
+Instead of dumping into a named file, dump on to stderr. However, note
+that the @option{stderr} is considered weak, so in case of two
+conflicting dump options, the option to dump into file takes
+precedence regardless of the command line ordering. For instance,
+
+@smallexample
+gcc -fdump-tree-pre-stderr -fdump-tree-pre ...
+gcc -fdump-tree-pre -fdump-tree-pre-stderr ...
+@end smallexample
+
+Both produce the PRE dump in a file. Similarly, for the following
+
+@smallexample
+gcc -fdump-tree-all-stderr -fdump-tree-pre ...
+@end smallexample
+
+The PRE dump goes into a file while all other dumps are on the
+stderr. In the following example,
+
+@smallexample
+gcc -fdump-tree-all -fdump-tree-pre-stderr ...
+@end smallexample
+
+all the dumps are written into named files. The PRE dump is not dumped
+on to stderr, because @option{-fdump-tree-pre-stderr} is overridden by
+the @option{-fdump-tree-all} on the command line.
+
 @item all
-Turn on all options, except @option{raw}, @option{slim}, @option{verbose}
-and @option{lineno}.
+Turn on all options, except @option{raw}, @option{slim}, @option{verbose},
+@option{lineno}, and @option{stderr}.
 @end table
 
 The following tree dumps are possible:
@@ -5913,6 +5955,10 @@ is made by appending @file{.vrp} to the source fil
 @item all
 @opindex fdump-tree-all
 Enable all the available tree dumps with the flags provided in this option.
+
+@item all-stderr
+@opindex fdump-tree-all-stderr
+Enable all the available tree dumps on the stderr.
 @end table
 
 @item -ftree-vectorizer-verbose=@var{n}
Index: tree-dump.c
===================================================================
--- tree-dump.c (revision 187265)
+++ tree-dump.c (working copy)
@@ -802,7 +802,7 @@ struct dump_option_value_info
 };
 
 /* Table of dump options. This must be consistent with the TDF_* flags
-   in tree.h */
+   in tree-dump.h */
 static const struct dump_option_value_info dump_options[] =
 {
   {"address", TDF_ADDRESS},
@@ -825,6 +825,7 @@ static const struct dump_option_value_info dump_op
   {"nouid", TDF_NOUID},
   {"enumerate_locals", TDF_ENUMERATE_LOCALS},
   {"scev", TDF_SCEV},
+  {"stderr", TDF_STDERR},
   {"all", ~(TDF_RAW | TDF_SLIM | TDF_LINENO | TDF_TREE | TDF_RTL | TDF_IPA
            | TDF_STMTADDR | TDF_GRAPH | TDF_DIAGNOSTIC | TDF_VERBOSE
            | TDF_RHS_ONLY | TDF_NOUID | TDF_ENUMERATE_LOCALS | TDF_SCEV)},
@@ -926,15 +927,25 @@ dump_begin (int phase, int *flag_ptr)
   if (phase == TDI_none || !dump_enabled_p (phase))
     return NULL;
 
-  name = get_dump_file_name (phase);
   dfi = get_dump_file_info (phase);
-  stream = fopen (name, dfi->state < 0 ? "w" : "a");
-  if (!stream)
-    error ("could not open dump file %qs: %m", name);
+
+  if (dfi->flags & TDF_STDERR)
+    {
+      stream = stderr;
+      dfi->state = 1;
+    }
   else
-    dfi->state = 1;
-  free (name);
+    {
+      name = get_dump_file_name (phase);
+      stream = fopen (name, dfi->state < 0 ? "w" : "a");
 
+      if (!stream)
+        error ("could not open dump file %qs: %m", name);
+      else
+        dfi->state = 1;
+      free (name);
+    }
+
   if (flag_ptr)
     *flag_ptr = dfi->flags;
 
@@ -989,7 +1000,8 @@ dump_flag_name (int phase)
 void
 dump_end (int phase ATTRIBUTE_UNUSED, FILE *stream)
 {
-  fclose (stream);
+  if (stream != stderr)
+    fclose (stream);
 }
 
 /* Enable all tree dumps.  Return number of enabled tree dumps.  */
@@ -1060,7 +1072,10 @@ dump_switch_p_1 (const char *arg, struct dump_file
        if (strlen (option_ptr->name) == length
            && !memcmp (option_ptr->name, ptr, length))
          {
-           flags |= option_ptr->value;
+            /* Consider TDF_STDERR weak and use it only if the state
+               is uninitialized. */
+            if (!((option_ptr->value & TDF_STDERR) && dfi->state))
+              flags |= option_ptr->value;
            goto found;
          }
       warning (0, "ignoring unknown option %q.*s in %<-fdump-%s%>",
@@ -1069,6 +1084,10 @@ dump_switch_p_1 (const char *arg, struct dump_file
       ptr = end_ptr;
     }
 
+  /* Clear TDF_STDERR in case of same dump without stderr option.  */
+  if ((dfi->flags & TDF_STDERR) && !(flags & TDF_STDERR))
+    dfi->flags &= ~TDF_STDERR;
+
   dfi->state = -1;
   dfi->flags |= flags;
 
Index: tree-pass.h
===================================================================
--- tree-pass.h (revision 187265)
+++ tree-pass.h (working copy)
@@ -84,6 +84,7 @@ enum tree_dump_index
 #define TDF_ENUMERATE_LOCALS (1 << 22) /* Enumerate locals by uid.  */
 #define TDF_CSELIB     (1 << 23)       /* Dump cselib details.  */
 #define TDF_SCEV       (1 << 24)       /* Dump SCEV details.  */
+#define TDF_STDERR     (1 << 25)       /* Dump on stderr.  */
 
 
 /* In tree-dump.c */
Index: testsuite/g++.dg/other/dump-stderr-1.C
===================================================================
--- testsuite/g++.dg/other/dump-stderr-1.C      (revision 0)
+++ testsuite/g++.dg/other/dump-stderr-1.C      (revision 0)
@@ -0,0 +1,12 @@
+// Test that if we specify both dump to stderr and to a file, there is
+// no output on the stderr but a dump file is still produced.
+/* { dg-options "-O2 -fdump-tree-pre -fdump-tree-pre-stderr" } */
+
+void test (int *b, int *e, int stride)
+  {
+    for (int *p = b; p != e; p += stride)
+      *p = 1;
+  }
+
+/* { dg-final { scan-tree-dump-times ";; Function void test" 1 "pre"  } } */
+/* { dg-final { cleanup-tree-dump "pre" } } */

--
This patch is available for review at http://codereview.appspot.com/6190057

Reply via email to