Bug 111576 - gcc generates conditional branch for bitwise "&"
Summary: gcc generates conditional branch for bitwise "&"
Status: RESOLVED DUPLICATE of bug 79045
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 13.2.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-09-24 21:50 UTC by Paul Eggert
Modified: 2023-09-25 01:35 UTC (History)
0 users

See Also:
Host:
Target: x86_64
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
source code for branch-free test for "." or ".." (147 bytes, text/plain)
2023-09-24 21:50 UTC, Paul Eggert
Details
Generated assembly language for the program (546 bytes, text/plain)
2023-09-24 21:51 UTC, Paul Eggert
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Paul Eggert 2023-09-24 21:50:28 UTC
Created attachment 55983 [details]
source code for branch-free test for "." or ".."

Although this is low prioriy for me, I thought I'd mention it in case it would help GCC optimize better for others.

I looked into implementing a test for "." or ".." that was branch free. In other words, implement "strcmp (p, ".") == 0 || strcmp (p, "..") == 0" without using conditional branches. I came up with an expression that should do this, but GCC translates a bitwise "&" into code that involves conditional branches. Normally I would think conditional branches would be better avoided for bitwise "&".

To see the situation, compile the attached program t.c with 'gcc -O2 -S t.c' using gcc (GCC) 13.2.1 20230728 (Red Hat 13.2.1-1). It generates the attached assembly language output t.s. The code generated for 'f' contains two conditional branches, even though the source uses '&'. Furthermore, the generated code evaluates the more complicated side of the '&' first, to see whether it should evaluate the easy part, and this is not likely to be faster than just evaluating the whole thing.

The code generated for the logically equivalent function 'g' is branch free, but g's source code is trickier as it substitutes "~+!!" for plain "!". (The "+" is present to work around GCC bug 111715.)
Comment 1 Paul Eggert 2023-09-24 21:51:45 UTC
Created attachment 55984 [details]
Generated assembly language for the program
Comment 2 Andrew Pinski 2023-09-24 22:04:44 UTC
There is a dup of this bug.
Comment 3 Andrew Pinski 2023-09-24 22:05:21 UTC
Dup of bug 79045.

*** This bug has been marked as a duplicate of bug 79045 ***
Comment 4 Andrew Pinski 2023-09-24 22:09:16 UTC
>111715

That is not a valid bug # either.
Comment 5 Paul Eggert 2023-09-25 01:35:24 UTC
(In reply to Andrew Pinski from comment #4)
> >111715
> 
> That is not a valid bug # either.

Sorry, I meant Bug 111575.