The C Standard explicitly specifies signed integer overflow as having undefined behavior. Yet most CPUs implement signed arithmetics with defined semantics for overflow (except maybe for division overflow: x / 0
and INT_MIN / -1
).
Compilers writers have been taking advantage of the undefinedness of such overflows to add more aggressive optimisations that tend to break legacy code in very subtle ways. For example this code may have worked on older compilers but does not anymore on current versions of gcc
and clang
:
/* Tncrement a by a value in 0..255, clamp a to positive integers.
The code relies on 32-bit wrap-around, but the C Standard makes
signed integer overflow undefined behavior, so sum_max can now
return values less than a. There are Standard compliant ways to
implement this, but legacy code is what it is... */
int sum_max(int a, unsigned char b) {
int res = a + b;
return (res >= a) ? res : INT_MAX;
}
Is there hard evidence that these optimisations are worthwhile? Are there comparative studies documenting the actual improvements on real life examples or even on classical benchmarks?
I came up with this question as I was watching this: C++Now 2018: John Regehr “Closing Keynote: Undefined Behavior and Compiler Optimizations”
I am tagging c and c++ as the problem is similar in both languages but the answers might be different.
The answer is actually in your question:
I can't think of a CPU that you can buy today that does not use twos-compliment arithmetic for signed integers, but that wasn't always the case.
The C language was invented in 1972. Back then, IBM 7090 mainframes still existed. Not all computers were twos-compliment.
To have defined the language (and overflow behaviour) around 2s-compliment would have been prejudicial to code generation on machines that weren't.
Furthermore, as it has already been said, specifying that signed overflow is to be UB allows the compiler to produce better code, because it can discount code paths that result from signed overflow, assuming that this will never happen.
If I understand correctly that it's intended to clamp the sum of a and b to 0....INT_MAX without wraparound, I can think of two ways to write this function in a compliant way.
First, the inefficient general case that will work on all cpus:
Second, the surprisingly efficient 2s-compliment specific way:
Resulting assembler can be seen here: https://godbolt.org/z/F42IXV
Not quite an example of optimization, but one useful consequence of undefined behaviour is
-ftrapv
command line switch of GCC/clang. It inserts code which crashes your program on integer overflow.It won't work on unsigned integers, in accordance with the idea that unsigned overflow is intentional.
The Standard's wording on signed integer overflow ensures that people won't write overflowing code on purpose, so
ftrapv
is a useful tool to discover unintentional overflow.Here's an actual little benchmark, bubble sort. I've compared timings without/with
-fwrapv
(which means the overflow is UB/not UB). Here are the results (seconds):As you can see, the not-UB (
-fwrapv
) version is almost always slower, the largest difference is pretty big, 1.85x.Here's the code. Note, that I intentionally chose an implementation, which should produce a larger difference for this test.
I don't know about studies and statistics, but yes, there are definitely optimizations taking this into account that compilers actually do. And yes, they are very important (tldr loop vectorization for example).
Besides the compiler optimizations, there is another aspect to be taken into account. With UB you get C/C++ signed integers to behave arithmetically as you would expect mathematically. For instance
x + 10 > x
holds true now (for valid code of course), but would not on a wrap-around behavior.I've found an excellent article How undefined signed overflow enables optimizations in GCC from Krister Walfridsson’s blog listing some optimizations that take signed overflow UB into account. The following examples are from it. I am adding c++ and assembly examples to them.
If the optimizations look too simple, uninteresting or unimpactful, remember that these optimization are just steps in a much much larger chain of optimizations. And the butterfly effect does happen as a seemingly unimportant optimization at an earlier step can trigger a much more impactful optimization at a later step.
If the examples look nonsensical (who would write
x * 10 > 0
) keep in mind that you can very easily get to this kind of examples in C and C++ with constants, macros, templates. Besides the compiler can get to this kind of examples when applying transformations and optimizations in its IR.Signed integer expression simplification
Eliminate multiplication in comparison with 0
Eliminate division after multiplication
Eliminate negation
Simplify comparisons that are always true or false
Eliminate negation in comparisons
Reduce magnitude of constants
Eliminate constants in comparisons
Pointer arithmetic and type promotion
This is a very important optimization as loop vectorization one of the most efficient and effective optimization algorithms.
It is trickier to demonstrate. But I remember actually encountering a situation when changing an index from
unsigned
tosigned
drastically improved the generated assembly. Unfortunately I cannot remember or replicate it now. Will come back later if I figure it out.Value range calculations
Loop analysis and optimization