diff mbox series

[v2] Do not emit a redundant DW_TAG_lexical_block for inlined subroutines

Message ID AS1PR01MB9465DB93EAB5A26C78580E87E48F2@AS1PR01MB9465.eurprd01.prod.exchangelabs.com
State New
Headers show
Series [v2] Do not emit a redundant DW_TAG_lexical_block for inlined subroutines | expand

Commit Message

Bernd Edlinger Aug. 22, 2024, 5:38 a.m. UTC
While this already works correctly for the case when an inlined
subroutine contains only one subrange, a redundant DW_TAG_lexical_block
is still emitted when the subroutine has multiple blocks.

Fixes: ac02e5b75451 ("re PR debug/37801 (DWARF output for inlined functions
                      doesn't always use DW_TAG_inlined_subroutine)")

gcc/ChangeLog:

        PR debug/87440
        * dwarf2out.cc (gen_inlined_subroutine_die): Handle the case
	of multiple subranges correctly.

gcc/testsuite/ChangeLog:

        * gcc.dg/debug/dwarf2/inline7.c: New test.
---
 gcc/dwarf2out.cc                            | 15 ++++++++++++---
 gcc/testsuite/gcc.dg/debug/dwarf2/inline7.c | 20 ++++++++++++++++++++
 2 files changed, 32 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/debug/dwarf2/inline7.c

v2: Addressed review commments, added a testcase.

Bootstrapped and regression-tested on x86_64-pc-linux-gnu, OK for trunk?

Comments

Richard Biener Aug. 22, 2024, 7:01 a.m. UTC | #1
On Thu, 22 Aug 2024, Bernd Edlinger wrote:

> While this already works correctly for the case when an inlined
> subroutine contains only one subrange, a redundant DW_TAG_lexical_block
> is still emitted when the subroutine has multiple blocks.
> 
> Fixes: ac02e5b75451 ("re PR debug/37801 (DWARF output for inlined functions
>                       doesn't always use DW_TAG_inlined_subroutine)")

OK.

Thanks,
Richard.

> gcc/ChangeLog:
> 
>         PR debug/87440
>         * dwarf2out.cc (gen_inlined_subroutine_die): Handle the case
> 	of multiple subranges correctly.
> 
> gcc/testsuite/ChangeLog:
> 
>         * gcc.dg/debug/dwarf2/inline7.c: New test.
> ---
>  gcc/dwarf2out.cc                            | 15 ++++++++++++---
>  gcc/testsuite/gcc.dg/debug/dwarf2/inline7.c | 20 ++++++++++++++++++++
>  2 files changed, 32 insertions(+), 3 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/debug/dwarf2/inline7.c
> 
> v2: Addressed review commments, added a testcase.
> 
> Bootstrapped and regression-tested on x86_64-pc-linux-gnu, OK for trunk?
> 
> diff --git a/gcc/dwarf2out.cc b/gcc/dwarf2out.cc
> index 357efaa5990..362d695674b 100644
> --- a/gcc/dwarf2out.cc
> +++ b/gcc/dwarf2out.cc
> @@ -25171,17 +25171,26 @@ gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
>       Do that by doing the recursion to subblocks on the single subblock
>       of STMT.  */
>    bool unwrap_one = false;
> -  if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
> +  tree sub = BLOCK_SUBBLOCKS (stmt);
> +  if (sub)
>      {
> -      tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
> +      tree origin = block_ultimate_origin (sub);
>        if (origin
>  	  && TREE_CODE (origin) == BLOCK
>  	  && BLOCK_SUPERCONTEXT (origin) == decl)
>  	unwrap_one = true;
> +      for (tree next = BLOCK_CHAIN (sub); unwrap_one && next;
> +	   next = BLOCK_CHAIN (next))
> +	if (BLOCK_FRAGMENT_ORIGIN (next) != sub)
> +	  unwrap_one = false;
>      }
>    decls_for_scope (stmt, subr_die, !unwrap_one);
>    if (unwrap_one)
> -    decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
> +    {
> +      decls_for_scope (sub, subr_die);
> +      for (sub = BLOCK_CHAIN (sub); sub; sub = BLOCK_CHAIN (sub))
> +	gen_block_die (sub, subr_die);
> +    }
>  }
>  
>  /* Generate a DIE for a field in a record, or structure.  CTX is required: see
> diff --git a/gcc/testsuite/gcc.dg/debug/dwarf2/inline7.c b/gcc/testsuite/gcc.dg/debug/dwarf2/inline7.c
> new file mode 100644
> index 00000000000..48d457216b1
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/debug/dwarf2/inline7.c
> @@ -0,0 +1,20 @@
> +/* Verify that both inline instances have a DW_AT_ranges but
> +   no extra DW_TAG_lexical_block.  */
> +/* { dg-options "-O -gdwarf -dA" } */
> +/* { dg-do compile } */
> +/* { dg-final { scan-assembler-times "\\(DIE \\(\[^\n\]*\\) DW_TAG_inlined_subroutine" 2 } } */
> +/* { dg-final { scan-assembler-times " DW_AT_ranges" 2 } } */
> +/* { dg-final { scan-assembler-times "\\(DIE \\(\[^\n\]*\\) DW_TAG_lexical_block" 0 } } */
> +
> +static int foo (int i)
> +{
> +  volatile int j = i + 3;
> +  if (j == 3)
> +    return 0;
> +  return j - 2;
> +}
> +int main()
> +{
> +  volatile int z = foo (-2) && foo (-1);
> +  return z;
> +}
>
diff mbox series

Patch

diff --git a/gcc/dwarf2out.cc b/gcc/dwarf2out.cc
index 357efaa5990..362d695674b 100644
--- a/gcc/dwarf2out.cc
+++ b/gcc/dwarf2out.cc
@@ -25171,17 +25171,26 @@  gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
      Do that by doing the recursion to subblocks on the single subblock
      of STMT.  */
   bool unwrap_one = false;
-  if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
+  tree sub = BLOCK_SUBBLOCKS (stmt);
+  if (sub)
     {
-      tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
+      tree origin = block_ultimate_origin (sub);
       if (origin
 	  && TREE_CODE (origin) == BLOCK
 	  && BLOCK_SUPERCONTEXT (origin) == decl)
 	unwrap_one = true;
+      for (tree next = BLOCK_CHAIN (sub); unwrap_one && next;
+	   next = BLOCK_CHAIN (next))
+	if (BLOCK_FRAGMENT_ORIGIN (next) != sub)
+	  unwrap_one = false;
     }
   decls_for_scope (stmt, subr_die, !unwrap_one);
   if (unwrap_one)
-    decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
+    {
+      decls_for_scope (sub, subr_die);
+      for (sub = BLOCK_CHAIN (sub); sub; sub = BLOCK_CHAIN (sub))
+	gen_block_die (sub, subr_die);
+    }
 }
 
 /* Generate a DIE for a field in a record, or structure.  CTX is required: see
diff --git a/gcc/testsuite/gcc.dg/debug/dwarf2/inline7.c b/gcc/testsuite/gcc.dg/debug/dwarf2/inline7.c
new file mode 100644
index 00000000000..48d457216b1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/dwarf2/inline7.c
@@ -0,0 +1,20 @@ 
+/* Verify that both inline instances have a DW_AT_ranges but
+   no extra DW_TAG_lexical_block.  */
+/* { dg-options "-O -gdwarf -dA" } */
+/* { dg-do compile } */
+/* { dg-final { scan-assembler-times "\\(DIE \\(\[^\n\]*\\) DW_TAG_inlined_subroutine" 2 } } */
+/* { dg-final { scan-assembler-times " DW_AT_ranges" 2 } } */
+/* { dg-final { scan-assembler-times "\\(DIE \\(\[^\n\]*\\) DW_TAG_lexical_block" 0 } } */
+
+static int foo (int i)
+{
+  volatile int j = i + 3;
+  if (j == 3)
+    return 0;
+  return j - 2;
+}
+int main()
+{
+  volatile int z = foo (-2) && foo (-1);
+  return z;
+}