public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Missed optimization question
@ 2019-12-01 18:22 Arvind Sankar
  2019-12-01 18:58 ` Marc Glisse
  0 siblings, 1 reply; 2+ messages in thread
From: Arvind Sankar @ 2019-12-01 18:22 UTC (permalink / raw)
  To: gcc-help

Hi, for the below example, gcc (9.2.0) at -O and above generates
identical code for f1 and f2 (on x86_64 at least). So one would think
that the compiler "knows" that the min(m,b) can be reduced to m, however
f3 doesn't get optimized away into return 1; at any level of
optimization.

f4 is closest to the real example, where there's a bounds check on a
variable that was already min'd with the bound, but the check doesn't
get optimized away.

Why would this be the case?

Listing:

#define min(a,b) ((a)<(b)?(a):(b))

int f1(int a,int b)
{
	int m = min(a,b);
	return m;
}

int f2(int a,int b)
{
	int m = min(a,b);
	return min(m,b);
}

int f3(int a,int b)
{
	int m = min(a,b);
	return m == min(m,b);
}

int f4(int a,int b)
{
	int m = min(a,b);
	return m <= b;
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Missed optimization question
  2019-12-01 18:22 Missed optimization question Arvind Sankar
@ 2019-12-01 18:58 ` Marc Glisse
  0 siblings, 0 replies; 2+ messages in thread
From: Marc Glisse @ 2019-12-01 18:58 UTC (permalink / raw)
  To: Arvind Sankar; +Cc: gcc-help

On Sun, 1 Dec 2019, Arvind Sankar wrote:

> Hi, for the below example, gcc (9.2.0) at -O and above generates
> identical code for f1 and f2 (on x86_64 at least). So one would think
> that the compiler "knows" that the min(m,b) can be reduced to m, however
> f3 doesn't get optimized away into return 1; at any level of
> optimization.
>
> f4 is closest to the real example, where there's a bounds check on a
> variable that was already min'd with the bound, but the check doesn't
> get optimized away.
>
> Why would this be the case?

Please file a report in gcc's bugzilla.

min(a,min(a,b)) is only simplified during reassoc. min(a,b)<=b is not 
simplified. min(a,b)==b is simplified to a>=b before reassoc.

Adding 2 transformations to match.pd would be quite simple, but is 
unlikely to happen until there is a bug report in the right place.

-- 
Marc Glisse

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-12-01 18:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-01 18:22 Missed optimization question Arvind Sankar
2019-12-01 18:58 ` Marc Glisse

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).