public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/109931] New: Knowledge on literal not used in optimization
@ 2023-05-22 12:33 antoshkka at gmail dot com
  2023-05-22 12:37 ` [Bug tree-optimization/109931] " rguenth at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: antoshkka at gmail dot com @ 2023-05-22 12:33 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109931
           Summary: Knowledge on literal not used in optimization
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

Function for comparing a lower-cased string with runtime string of known size:


constexpr bool ICaseEqualLowercase(const char* lowercase, const char* y,
                                   unsigned size) noexcept {
    constexpr char kLowerToUpperMask = static_cast<char>(~unsigned{32});
    for (unsigned i = 0; i < size; ++i) {
        const auto lowercase_c = lowercase[i];
        if (lowercase_c != y[i]) {
            if (!('a' <= lowercase_c && lowercase_c <= 'z') ||
                (lowercase_c & kLowerToUpperMask) != y[i]) {
                return false;
            }
        }
    }

    return true;
}
bool test2(const char* y) {
    return ICaseEqualLowercase("hello", y, 5);
}


With GCC trunk and -O2 flags the GCC fails to understand that all the
characters of `lowercase` are lowercase ASCII and the expression `!('a' <=
lowercase_c && lowercase_c <= 'z')` is always `false`.

Because of that, additional instructions in loop are emitted:
        lea     esi, [rdx-97]
        cmp     sil, 25
        ja      .L6


Godbolt playground: https://godbolt.org/z/xrc1T4oeW

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

* [Bug tree-optimization/109931] Knowledge on literal not used in optimization
  2023-05-22 12:33 [Bug tree-optimization/109931] New: Knowledge on literal not used in optimization antoshkka at gmail dot com
@ 2023-05-22 12:37 ` rguenth at gcc dot gnu.org
  2023-05-22 12:38 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-05-22 12:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
But that's because nothing in the function asserts this?  Without fully
specializing and unrolling on the constant "hello" argument at least.

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

* [Bug tree-optimization/109931] Knowledge on literal not used in optimization
  2023-05-22 12:33 [Bug tree-optimization/109931] New: Knowledge on literal not used in optimization antoshkka at gmail dot com
  2023-05-22 12:37 ` [Bug tree-optimization/109931] " rguenth at gcc dot gnu.org
@ 2023-05-22 12:38 ` jakub at gcc dot gnu.org
  2023-05-22 12:57 ` antoshkka at gmail dot com
  2023-05-22 14:53 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-05-22 12:38 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |aldyh at gcc dot gnu.org,
                   |                            |jakub at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
This is another case of the missing range info for loads from read-only
constants.

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

* [Bug tree-optimization/109931] Knowledge on literal not used in optimization
  2023-05-22 12:33 [Bug tree-optimization/109931] New: Knowledge on literal not used in optimization antoshkka at gmail dot com
  2023-05-22 12:37 ` [Bug tree-optimization/109931] " rguenth at gcc dot gnu.org
  2023-05-22 12:38 ` jakub at gcc dot gnu.org
@ 2023-05-22 12:57 ` antoshkka at gmail dot com
  2023-05-22 14:53 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: antoshkka at gmail dot com @ 2023-05-22 12:57 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Antony Polukhin <antoshkka at gmail dot com> ---
> But that's because nothing in the function asserts this?  Without fully
> specializing and unrolling on the constant "hello" argument at least.

Yes, I was hoping for that unrolling to happen

Probably a more simplified case:


constexpr bool EqualICase(const char* lowercase, const char* y) noexcept {
    for (;;) {
        const auto lowercase_c = *lowercase;
        if (!lowercase_c) return true;
        if (lowercase_c != *y) {
            return false;
        }
        ++lowercase;
        ++y;
    }
}
bool test2(const char* y) {
    return EqualICase("he", y);
}

With range info for loads from read-only constants I'd expect this to become
just a

test2(char const*):
        cmp     BYTE PTR [rdi], 104
        jne     .L3
        cmp     BYTE PTR [rdi+1], 101
        sete    al
        ret
.L3:
        xor     eax, eax
        ret


rather than a fair loop with checks for \0

Godbolt: https://godbolt.org/z/z6rTYEzWx

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

* [Bug tree-optimization/109931] Knowledge on literal not used in optimization
  2023-05-22 12:33 [Bug tree-optimization/109931] New: Knowledge on literal not used in optimization antoshkka at gmail dot com
                   ` (2 preceding siblings ...)
  2023-05-22 12:57 ` antoshkka at gmail dot com
@ 2023-05-22 14:53 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-22 14:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
There is another bug about this.
In this case we know that bit 5 is set for the range ['a','z'] .

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

end of thread, other threads:[~2023-05-22 14:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-22 12:33 [Bug tree-optimization/109931] New: Knowledge on literal not used in optimization antoshkka at gmail dot com
2023-05-22 12:37 ` [Bug tree-optimization/109931] " rguenth at gcc dot gnu.org
2023-05-22 12:38 ` jakub at gcc dot gnu.org
2023-05-22 12:57 ` antoshkka at gmail dot com
2023-05-22 14:53 ` 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).