public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "assaiante at diag dot uniroma1.it" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug debug/105248] New: Missing value or location attribute for constant live variable likely caused by tree-dse at -O1/-O2/-O3
Date: Tue, 12 Apr 2022 17:44:18 +0000	[thread overview]
Message-ID: <bug-105248-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105248

            Bug ID: 105248
           Summary: Missing value or location attribute for constant live
                    variable likely caused by tree-dse at -O1/-O2/-O3
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: debug
          Assignee: unassigned at gcc dot gnu.org
          Reporter: assaiante at diag dot uniroma1.it
  Target Milestone: ---

In this minimized C example, variable l_56 is marked as optimized out when
stepping on a call to an external function that takes it as an argument. This
happens then the optimization level is -O1/-O2/-O3 while at -Og the debug
information is correctly defined. The variable has constant value.

Apparently, tree-dse optimization is responsible for a missing location
attribute in the DWARF DIE associated with variable l_56. If we prevent the
compiler from performing such optimization, the generated ASM will be identical
but the DWARF location info will be complete, making the variable available in
its uses.

Please find below a detailed analysis for -O1 on x64 and a quick assessment on
past gcc versions where the issue is sometimes not present.

$ cat a.c
void a()
{
    int *b;
    short l_47 = 5;
    int l_56 = 6;
    int **c = &b;
    test(l_47, l_56);
    *c = &l_56;
}
int main ()
{
    a();
}

$ cat lib.c
#include <stdio.h>

void test(int l_47, int l_56) {
   printf("%d %d", l_47, l_56);
}

GCC and GDB version:
- gcc (GCC) 12.0.0 20211227 (experimental) - commit id: 500d3f0a302
- GNU gdb (GDB) 11.2

GDB trace:
$ gcc -O1 -g a.c lib.c -o opt
$ gdb -q opt
Reading symbols from opt...
(gdb) b 7
Breakpoint 1 at 0x40054a: file a.c, line 7.
(gdb) r
Starting program: /tmp/opt 

Breakpoint 1, a () at a.c:7
7           test(l_47, l_56);
(gdb) info locals
b = <optimized out>
l_47 = 5
l_56 = <optimized out>
c = <synthetic pointer>

ASM at -O1:
0000000000400546 <a>:
  400546:       48 83 ec 08             sub    $0x8,%rsp
  40054a:       be 06 00 00 00          mov    $0x6,%esi
  40054f:       bf 05 00 00 00          mov    $0x5,%edi
  400554:       b8 00 00 00 00          mov    $0x0,%eax
  400559:       e8 30 00 00 00          callq  40058e <test>
  40055e:       48 83 c4 08             add    $0x8,%rsp
  400562:       c3                      retq

DWARF info at -O1:
0x000000a7:     DW_TAG_variable
                  DW_AT_name    ("l_56")
                  DW_AT_decl_file       ("/tmp/a.c")
                  DW_AT_decl_line       (5)
                  DW_AT_decl_column     (0x09)
                  DW_AT_type    (0x0000003d "int")

>From DWARF info we can see how the variable l_56 DIE is completely missing
either a const value or a location attribute and this lack of information
causes the unavailability of its value during debugging.

Through some testing we found out that the optimization that makes the variable
not available and that introduces the location overlapping is -ftree-dse. If we
add -fno-tree-dse to the previous compilation command line, the issue is not
present anymore since the DWARF location is correctly computed with the correct
value associated with it.


ASM at -O1 with -fno-tree-dse:
0000000000400546 <a>:
  400546:       48 83 ec 08             sub    $0x8,%rsp
  40054a:       be 06 00 00 00          mov    $0x6,%esi
  40054f:       bf 05 00 00 00          mov    $0x5,%edi
  400554:       b8 00 00 00 00          mov    $0x0,%eax
  400559:       e8 30 00 00 00          callq  40058e <test>
  40055e:       48 83 c4 08             add    $0x8,%rsp
  400562:       c3                      retq

DWARF info at -O1 with -fno-tree-dse:
0x000000a7:     DW_TAG_variable
                  DW_AT_name    ("l_56")
                  DW_AT_decl_file       ("/tmp/a.c")
                  DW_AT_decl_line       (5)
                  DW_AT_decl_column     (0x09)
                  DW_AT_type    (0x0000003d "int")
                  DW_AT_location        (0x0000000e: 
                     [0x000000000040054a, 0x000000000040055e): DW_OP_lit6,
DW_OP_stack_value)
                  DW_AT_GNU_locviews    (0x0000000c)


We have also tested older gcc versions (6.4, 7.5, 8.4, 9.3, 10.3, 11.1) and the
results are identical to the git version.

             reply	other threads:[~2022-04-12 17:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-12 17:44 assaiante at diag dot uniroma1.it [this message]
2022-11-07 23:17 ` [Bug tree-optimization/105248] gimple level DSE does not add DEBUG statement when deleting store to ADDRESSABLE local decl pinskia at gcc dot gnu.org
2022-11-28  3:20 ` pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-105248-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).