Missed optimization question

Arvind Sankar nivedita@alum.mit.edu
Sun Dec 1 18:22:00 GMT 2019


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;
}



More information about the Gcc-help mailing list