public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/110318] New: Unused string literal is retained in assembler file
@ 2023-06-20 10:38 roland.illig at gmx dot de
  2023-06-20 10:45 ` [Bug c/110318] " roland.illig at gmx dot de
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: roland.illig at gmx dot de @ 2023-06-20 10:38 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110318
           Summary: Unused string literal is retained in assembler file
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: roland.illig at gmx dot de
  Target Milestone: ---

~~~c
typedef typeof(sizeof 0) size_t;

int memcmp(const void *, const void *, size_t);

int demo(const char *);

int demo(const char *p) {
        const char *start = p;
        while (*p == 'C')
                p++;
        if (p - start == 2 && memcmp(start, "if", 2) == 0)
                return 2;
        if (p - start == 6 && memcmp(start, "ifndef", 6) == 0)
                return 6;
        return 0;
}
~~~

$ uname -a
NetBSD nbcurr.roland-illig.de 10.99.3 NetBSD 10.99.3 (GENERIC) #0: Fri Apr 21
02:17:32 UTC 2023
$ gcc-12.2.0 -O2 -S code.c
$ grep string code.s
        .string "ifndef"


The string "ifndef" is retained, even though it is not used in the code,
because the comparison is inlined. Curiously, the string "if" is removed.

Both strings should be removed, as they are unnecessary.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/110318] Unused string literal is retained in assembler file
  2023-06-20 10:38 [Bug c/110318] New: Unused string literal is retained in assembler file roland.illig at gmx dot de
@ 2023-06-20 10:45 ` roland.illig at gmx dot de
  2023-06-20 10:48 ` [Bug middle-end/110318] " pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: roland.illig at gmx dot de @ 2023-06-20 10:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Roland Illig <roland.illig at gmx dot de> ---
A variant on the same theme:
~~~c
typedef typeof(sizeof 0) size_t;
int memcmp(const void *, const void *, size_t);

int demo(const char *s) {
        if (memcmp(s, "12345678", 8) == 0)
                return 8;
        if (memcmp(s, "1234567", 7) == 0)
                return 7;
        if (memcmp(s, "123456", 6) == 0)
                return 6;
        if (memcmp(s, "12345", 5) == 0)
                return 5;
        if (memcmp(s, "1234", 4) == 0)
                return 4;
        if (memcmp(s, "123", 3) == 0)
                return 3;
        if (memcmp(s, "12", 2) == 0)
                return 2;
        if (memcmp(s, "1", 1) == 0)
                return 1;
        return 0;
}
~~~

It looks as if the string literals are retained if their length is not a power
of two.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug middle-end/110318] Unused string literal is retained in assembler file
  2023-06-20 10:38 [Bug c/110318] New: Unused string literal is retained in assembler file roland.illig at gmx dot de
  2023-06-20 10:45 ` [Bug c/110318] " roland.illig at gmx dot de
@ 2023-06-20 10:48 ` pinskia at gcc dot gnu.org
  2023-06-20 11:10 ` xry111 at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-20 10:48 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note I suspect the linker will remove unused strings in the final executable
though.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug middle-end/110318] Unused string literal is retained in assembler file
  2023-06-20 10:38 [Bug c/110318] New: Unused string literal is retained in assembler file roland.illig at gmx dot de
  2023-06-20 10:45 ` [Bug c/110318] " roland.illig at gmx dot de
  2023-06-20 10:48 ` [Bug middle-end/110318] " pinskia at gcc dot gnu.org
@ 2023-06-20 11:10 ` xry111 at gcc dot gnu.org
  2023-06-20 12:51 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-06-20 11:10 UTC (permalink / raw)
  To: gcc-bugs

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

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |xry111 at gcc dot gnu.org

--- Comment #3 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #2)
> Note I suspect the linker will remove unused strings in the final executable
> though.

With --gc-sections sometimes the entire .rodata.str1.1 section can be removed,
but if it contains an used string it won't happen.  -fdata-sections won't
separate the strings into multiple sections either.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug middle-end/110318] Unused string literal is retained in assembler file
  2023-06-20 10:38 [Bug c/110318] New: Unused string literal is retained in assembler file roland.illig at gmx dot de
                   ` (2 preceding siblings ...)
  2023-06-20 11:10 ` xry111 at gcc dot gnu.org
@ 2023-06-20 12:51 ` rguenth at gcc dot gnu.org
  2023-06-20 17:02 ` pinskia at gcc dot gnu.org
  2023-06-20 17:08 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-06-20 12:51 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2023-06-20

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.  The issue is that we create a constant pool entry at RTL expansion
time and when the last use vanishes during RTL optimization the unused constant
pool entries are not pruned.

In this case we initially emit

;; _5 = __builtin_memcmp_eq (p_8(D), "ifndef", 6);

(insn 34 33 35 (set (reg/f:DI 94)
        (symbol_ref/f:DI ("*.LC0") [flags 0x2]  <var_decl 0x7fdd8d0643f0
*.LC0>)) "t.c":13:24 -1 
     (nil))       

(insn 35 34 36 (set (reg:CCZ 17 flags)
        (compare:CCZ (mem:SI (reg/v/f:DI 91 [ p ]) [0 MEM <char[1:6]> [(void
*)p_8(D)]+0 S4 A8])
            (const_int 1684956777 [0x646e6669]))) "t.c":13:24 -1
     (nil))

(jump_insn 36 35 37 (set (pc)
        (if_then_else (ne (reg:CCZ 17 flags)
                (const_int 0 [0]))
            (label_ref 43)
            (pc))) "t.c":13:24 -1
     (nil))

(insn 37 36 38 (set (reg:CCZ 17 flags)
        (compare:CCZ (mem:HI (plus:DI (reg/v/f:DI 91 [ p ])
                    (const_int 4 [0x4])) [0 MEM <char[1:6]> [(void *)p_8(D)]+4
S2 A8])
            (const_int 26213 [0x6665]))) "t.c":13:24 -1
     (nil))

(jump_insn 38 37 39 (set (pc)
        (if_then_else (ne (reg:CCZ 17 flags)
                (const_int 0 [0]))
            (label_ref 43)
            (pc))) "t.c":13:24 -1
     (nil))

(insn 39 38 40 (set (reg:SI 85 [ _5 ])
        (const_int 0 [0])) "t.c":13:24 -1
     (nil))

... so even RTL expansion doesn't end up using the string literal so
maybe this particular case can be catched early.  Pruning the constant
pool would be nice of course but we emit that quite early.

I'm quite sure there's duplicate bugreports about this.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug middle-end/110318] Unused string literal is retained in assembler file
  2023-06-20 10:38 [Bug c/110318] New: Unused string literal is retained in assembler file roland.illig at gmx dot de
                   ` (3 preceding siblings ...)
  2023-06-20 12:51 ` rguenth at gcc dot gnu.org
@ 2023-06-20 17:02 ` pinskia at gcc dot gnu.org
  2023-06-20 17:08 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-20 17:02 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |7.1.0, 9.1.0

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note the behavior changed between GCC 6 and GCC 7 where in GCC 6, it would just
emit a call to memcmp but in GCC 7, GCC starts to inline things.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug middle-end/110318] Unused string literal is retained in assembler file
  2023-06-20 10:38 [Bug c/110318] New: Unused string literal is retained in assembler file roland.illig at gmx dot de
                   ` (4 preceding siblings ...)
  2023-06-20 17:02 ` pinskia at gcc dot gnu.org
@ 2023-06-20 17:08 ` pinskia at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-20 17:08 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-06-20 17:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-20 10:38 [Bug c/110318] New: Unused string literal is retained in assembler file roland.illig at gmx dot de
2023-06-20 10:45 ` [Bug c/110318] " roland.illig at gmx dot de
2023-06-20 10:48 ` [Bug middle-end/110318] " pinskia at gcc dot gnu.org
2023-06-20 11:10 ` xry111 at gcc dot gnu.org
2023-06-20 12:51 ` rguenth at gcc dot gnu.org
2023-06-20 17:02 ` pinskia at gcc dot gnu.org
2023-06-20 17:08 ` pinskia at gcc dot gnu.org

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).