Bug 52070 - missing integer comparison optimization
Summary: missing integer comparison optimization
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.6.2
: P3 enhancement
Target Milestone: 13.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2012-01-31 19:39 UTC by Ulrich Drepper
Modified: 2023-07-01 11:21 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-07-26 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ulrich Drepper 2012-01-31 19:39:49 UTC
Compile this code with gcc 4.6.2:

#include <stddef.h>
size_t b;
int f(size_t a)
{
  return b == 0 || a < b;
}

For x86-64 I see this result:

f:	movq	b(%rip), %rdx
	movl	$1, %eax
	testq	%rdx, %rdx
	je	.L2
	xorl	%eax, %eax
	cmpq	%rdi, %rdx
	seta	%al
.L2:	rep ret

This can be more done without a conditional jump:

f:	movq	b(%rip), %rdx
	xorl	%eax, %eax
	subq	$1, %rdx
	cmpq	%rdi, %rdx
	setae	%al
	rep ret

Unless the b==0 test is marked as likely I'd say this code is performing better on all architectures.
Comment 1 Andrew Pinski 2012-02-01 02:31:51 UTC
Confirmed, I have a few patches which might improve this but maybe not for x86_64; it does help MIPS right now.
Comment 2 Andrew Pinski 2021-07-26 20:13:57 UTC
  _6 = b.1_1 > a_4(D);
  _7 = b.1_1 == 0;
  _8 = _6 | _7;

So this is an expansion issue I think.
Comment 3 Roger Sayle 2023-07-01 11:21:01 UTC
This has been fixed (optimally) since GCC 13, by Eugene Rozenfeld's fix for PR tree-optimization/96674.  The conditional jump was eliminated back in GCC 4.9, but the combination of the two tests into a subtraction and a single comparison was implemented (at the tree level) by Eugene.