Bug 94913 - Failure to optimize not+cmp into overflow check
Summary: Failure to optimize not+cmp into overflow check
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2020-05-02 02:15 UTC by Gabriel Ravier
Modified: 2023-08-24 21:44 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-05-05 00:00:00


Attachments
Prototype patch (620 bytes, patch)
2020-05-05 20:03 UTC, Uroš Bizjak
Details | Diff
gcc11-pr94913.patch (798 bytes, patch)
2020-05-06 13:23 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Gabriel Ravier 2020-05-02 02:15:33 UTC
bool f(unsigned x, unsigned y)
{
    return ~x < y;
}

With -O3, LLVM outputs this :

f(unsigned int, unsigned int):
  add edi, esi
  setb al
  ret

GCC outputs this :

f(unsigned int, unsigned int):
  not edi
  cmp edi, esi
  setb al
  ret
Comment 1 Gabriel Ravier 2020-05-02 02:18:45 UTC
The same thing happens for this code :

bool f(unsigned x, unsigned y)
{
    return (x - y - 1) >= x;
}

LLVM outputs this :

f(unsigned int, unsigned int):
  cmp esi, edi
  setae al
  ret

GCC outputs this :

f(unsigned int, unsigned int):
  mov eax, edi
  sub eax, esi
  sub eax, 1
  cmp eax, edi
  setnb al
  ret
Comment 2 Uroš Bizjak 2020-05-05 20:03:44 UTC
Created attachment 48458 [details]
Prototype patch

Prototype patch for missed optimization, described in Comment #0.

Following testcase:

--cut here--
int
foo (unsigned int x, unsigned int y)
{
  return ~x < y;
}

void f1 (void);
void f2 (void);

void
bar (unsigned int x, unsigned int y)
{
  if (~x < y)
    f1 ();
  else
    f2 ();
}
--cut here--

compiles (-O2) to:

foo:
        xorl    %eax, %eax
        addl    %esi, %edi
        setc    %al
        ret

bar:
        addl    %esi, %edi
        jnc     .L4
        jmp     f1
.L4:
        jmp     f2
Comment 3 Uroš Bizjak 2020-05-05 20:06:05 UTC
(In reply to Gabriel Ravier from comment #1)
> The same thing happens for this code :
> 
> bool f(unsigned x, unsigned y)
> {
>     return (x - y - 1) >= x;
> }

This transformation is the job for tree combiner.
Comment 4 Jakub Jelinek 2020-05-06 13:23:33 UTC
Created attachment 48467 [details]
gcc11-pr94913.patch

Untested fix for that part.
Comment 5 GCC Commits 2020-05-06 15:35:07 UTC
The master branch has been updated by Uros Bizjak <uros@gcc.gnu.org>:

https://gcc.gnu.org/g:7c2879301d3b027a1ba427a5d5c7557decb8a7ab

commit r11-145-g7c2879301d3b027a1ba427a5d5c7557decb8a7ab
Author: Uros Bizjak <ubizjak@gmail.com>
Date:   Wed May 6 17:33:51 2020 +0200

    i386: Use ADD to implement compares with negated operand [PR94913]
    
    Use carry flag from addition to implement GEU/LTU compares
    with negated operand, so e.g.
    
            ~x < y
    
    compiles to:
    
            addq    %rsi, %rdi
            setc    %al
    
    instead of:
    
            notq    %rdi
            cmpq    %rsi, %rdi
            setb    %al
    
            PR target/94913
            * config/i386/predicates.md (add_comparison_operator): New predicate.
            * config/i386/i386.md (compare->add splitter): New splitters.
    
    testsuite/ChangeLog:
    
            PR target/94913
            * gcc.target/i386/pr94913-1.c: New test.
            * gcc.target/i386/pr94913-2.c: Ditto.
Comment 6 GCC Commits 2020-05-08 07:33:09 UTC
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:ff33680165346cb291667f38dd2e9f25a74cc3c3

commit r11-193-gff33680165346cb291667f38dd2e9f25a74cc3c3
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri May 8 09:32:20 2020 +0200

    match.pd: Simplify unsigned A - B - 1 >= A to B >= A [PR94913]
    
    Implemented thusly.  The TYPE_OVERFLOW_WRAPS is there just because the
    pattern above it has it too, if you want, I can throw it away from both.
    
    2020-05-08  Jakub Jelinek  <jakub@redhat.com>
    
            PR tree-optimization/94913
            * match.pd (A - B + -1 >= A to B >= A): New simplification.
            (A - B > A to A < B): Don't test TYPE_OVERFLOW_WRAPS which is always
            true for TYPE_UNSIGNED integral types.
    
            * gcc.dg/tree-ssa/pr94913.c: New test.
Comment 7 Jakub Jelinek 2020-05-08 07:51:28 UTC
Should be fixed with both of the commits.