Re: Optimisations and undefined behaviour

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 06/11/15 16:48, Andrew Haley wrote:
On 11/06/2015 02:44 PM, David Brown wrote:

Incidentally, what do you think of the possibility of a "dont_care()"
expression?  I believe it would allow programmers to be clearer to the
compiler that they are not interested in the exact results, but they
don't want /undefined/ behaviour.

It would be interesting to see if such a thing can be defined in a
semantically rigorous way.  And explained to "ordinary" programmers!
:-)


I got close with a dont_care() :


static inline int dont_care(void) {
    int x;
    asm ("" : "=r" (x) : );
    return x;
}

This lets gcc "generate" an int without any instructions. But the compiler doesn't know that you don't care what value it has, so this won't let the compiler eliminate the conditional in :

int foo(int x) {
	if (x < 1000) {
		return x * x * x;
	} else {
		return dont_care();
	}
}

Another option is:

static inline int dont_care2(void) {
	int x = x;
	return x;
}

But that sometimes ends up with an unnecessary setting of x to 0.


Thinking about these things is fun - but finding a method that is safe and well-defined to use in code is far from easy!







[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux