Bug 106724 - logical-op-non-short-circuit maybe should be 1
Summary: logical-op-non-short-circuit maybe should be 1
Status: RESOLVED DUPLICATE of bug 116615
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2022-08-23 15:18 UTC by Andrew Pinski
Modified: 2024-09-24 18:08 UTC (History)
1 user (show)

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


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2022-08-23 15:18:16 UTC
Take:
int f(int a, int b, int c, int d)
{
  return a > b && c > d;
}
---- CUT ---
Currently on riscv32 at -O2 produces:
f:
        ble     a0,a1,.L3
        sgt     a0,a2,a3
        ret
.L3:
        li      a0,0
        ret

But add --param logical-op-non-short-circuit=1, produces:
f:
        sgt     a0,a0,a1
        sgt     a2,a2,a3
        and     a0,a0,a2
        ret

Which is much better, especially on in-order cores.
Comment 1 Andrew Pinski 2022-08-23 15:34:40 UTC
Note clang/LLVM produces the branch-less version also.
Comment 2 Andrew Pinski 2022-08-23 15:42:11 UTC
Well branch cost should be more tuned too.
Here is an example where BRANCH_COST=4 is needed to get one branch:
```
int g(void);

int f(int a, int b, int c, int d)
{
  if (a > b && c > d)
    return g();
  return 1;
}

```


Note clang/LLVM produces much worse code (two xori which are not needed).
Comment 3 Richard Biener 2022-08-24 07:12:02 UTC
we usually define logical-op-non-short-circuit based on branch cost
Comment 4 Andrew Pinski 2022-08-24 15:47:46 UTC
(In reply to Richard Biener from comment #3)
> we usually define logical-op-non-short-circuit based on branch cost

Right, I think this definition was copied from the MIPS backend even which is wrong there too which was done back in 2005 way before the main uses of LOGICAL_OP_NON_SHORT_CIRCUIT was done.
Comment 5 Andrew Pinski 2024-09-24 18:08:34 UTC
Dup.

*** This bug has been marked as a duplicate of bug 116615 ***