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
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
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
(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.
Created attachment 48467 [details] gcc11-pr94913.patch Untested fix for that part.
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.
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.
Should be fixed with both of the commits.