public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/116013] New: Missed optimization opportunity with andn involving consts
@ 2024-07-20 10:46 bisqwit at iki dot fi
  2024-07-20 14:19 ` [Bug middle-end/116013] " bisqwit at iki dot fi
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: bisqwit at iki dot fi @ 2024-07-20 10:46 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 116013
           Summary: Missed optimization opportunity with andn involving
                    consts
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bisqwit at iki dot fi
  Target Milestone: ---

Below are two short functions which work identically. While GCC utilizes the
ANDN instruction (of Intel BMI1) for test2, it fails to see that it could do
the same with test1.

    #include <stdint.h>
    uint64_t test1(uint64_t value)
    {
        return ~(value | 0x7F7F7F7F7F7F7F7F);
    }
    uint64_t test2(uint64_t value)
    {
        return ~value & ~0x7F7F7F7F7F7F7F7F;
    }

Assembler listings of both functions are below (-Ofast -mbmi):

    test1:
        movabsq $9187201950435737471, %rdx
        movq    %rdi, %rax
        orq     %rdx, %rax
        notq    %rax
        ret
    test2:
        movabsq $-9187201950435737472, %rax
        andn    %rax, %rdi, %rax
        ret

Tested compiler version:
GCC: (Debian 14-20240330-1) 14.0.1 20240330 (experimental) [master
r14-9728-g6fc84f680d0]

This optimization makes only sense if one of the operands is a compile-time
constant. If neither operand is a compile-time constant, then the opposite
optimization makes more sense — which GCC already does.
It is also worth noting, that GCC already compiles ~(var1 | ~var2) into ~var1 &
var2, utilizing ANDN. This is good.

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

end of thread, other threads:[~2024-07-23  5:32 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-20 10:46 [Bug tree-optimization/116013] New: Missed optimization opportunity with andn involving consts bisqwit at iki dot fi
2024-07-20 14:19 ` [Bug middle-end/116013] " bisqwit at iki dot fi
2024-07-22  7:06 ` [Bug target/116013] " rguenth at gcc dot gnu.org
2024-07-23  2:27 ` pinskia at gcc dot gnu.org
2024-07-23  2:29 ` pinskia at gcc dot gnu.org
2024-07-23  2:45 ` pinskia at gcc dot gnu.org
2024-07-23  5:32 ` 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).