68 points · by pmarin · 8y ago · 98 comments
if (cond) {
A[1] = X;
} else {
A[0] = X;
}
> A total license implementation could determine that, in the absence of any undefined behavior, the condition cond must have value 0 or 1.> If undefined behavior occurred somewhere in the integer arithmetic of cond, then cond could end up evaluating to a value other than 0 or 1
I don't even follow this very first example. Neither C nor C++ have any requirement that their condition statements be given 0 or 1.
In C:
> the first substatement is executed if the expression compares unequal to 0.
In C++:
> The value of a condition that is an expression is the value of the expression, contextually converted to bool
Where conversion to bool is defined as:
> A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true
I'm sure such a thing could occur if cond had a boolean type but contained uninitialized data. That would be similar to the situation talked about in this link: https://markshroyer.com/2012/06/c-both-true-and-false/
I think what they mean is that if the compiler has analyzed the code, not shown in the example, that comes before the if statement, and concluded that unless something undefined happens, cond can only be 0 or 1, then it can optimize out the condition.
They are not saying that the code actually shown is enough to conclude that cond must be 0 or 1.
> could determine that, in the absence of any undefined behavior
"could determine that" based on the code example shown
vs
"could determine that" based on static analysis performed on some preceding code
It would have been a lot easier to wrap my head around if it were an example where cond could be 0 or 4 or something along those lines. It would really underscore the compiler's desire to reuse the cond as the index.
People just want to "optimize" the code.
(Also, different optimizations can have surprising effects when combined that isn't clear at all from the individual levers.)
That's actually not completely true.
Take the restrict keyword for instance. Super painful to use in C/C++ and very dangerous. In Rust since the language has constrains around single mutable pointers they can turn it on globally and you get it "for free". If I recall correctly it's going live in one of the upcoming stable releases.
Sometimes constraints can actually let you get better performance than a wild-west of pointers everywhere.
If I remember correctly, it was originally enabled but had to be disabled because LLVM had some bugs around how it was handled. These arose because `restrict` is comparatively rarely used in C / C++ code.
I might not code daily in C++ anymore, but it would be nice that the software I rely on, specially in devices I am not able to upgrade, wouldn't suffer from such issues.
Butcher knifes are deadly sharp, yet those conscious ones do use proper gloves when handling them.
Having had to chase down stray aliased pointers in something marked restrict I can tell you it's not trivial to root-cause.
In C++ there is no restrict keyword. So either you're using and abusing compiler-specific behavior that you decided to do so without any test at all or you're confusing programming languages.
Either way, you're trying to pin the blame on C++ for something that has nothing to do with C++.
There is no "invalid C++, but valid C++-noX-noY-noZ". It's one or the other. The former is a bug in the code; the latter is a bug in the compiler.
On a large codebase this is very hard to do, and introduces an additional layer of complexity for very little reward.
That speaks of the complexity of C++.
When you say: "you can select what you're comfortable with", I am guessing that you are not using c++ in production environment.
That is not a questioning, but to point out that if that's true, you generally have no experience to support such claims.
(x * 2000) / 1000 can be reduced to x * 2 but only if we make some assumptions about x (about if x can overflow)
The reason why this is such a problem in C languages is because most of the time one is working in a mixed abstraction environment. The underlying model for many parts of the language is assembly. In order to get away from these issues and to produce more optional code we should move away from mixing models of abstraction.
IMO the wierdist thing to read in C++ though is X / A
A. Its roughly the same as X -= X % A. They values may differ if X or A are negative.Good practice as per my experience is to check MCDC and code coverage at unit test level
Those are reserved for people who value speed over correctness.
> C has perfecly well defined semantics for (eg) null pointer dereference: read or write memory location zero, and consequently probably crash.
C does have "perfectly well-defined semantics" for null pointer dereference, but it's undefined behavior. Sure, the null pointer happens to be 0 on the vast majority of architectures most programmers work with, but apparently non-zero null pointers are still used these days (at least in the admgcn LLVM target, from what I understand after a quick glance at the linked page), so it's not even a "all sane modern machines" thing [0].
In any case, I'd love to see some good benchmarks showing the effect of the more "controversial" optimizations. Compiler writers say they're important, people who don't like these optimizations tend to say that they don't make a significant difference. I don't really feel comfortable taking a side until I at least see what these optimizations enable (or fail to enable). I lean towards agreeing with the compiler writers, but perhaps that's because I haven't been bitten by one of these bugs yet...
[0]: https://reviews.llvm.org/D26196... no, C explicitely has no well defined semantics since it's undefined behaviour. You may believe that C do due to habit but that's not the case.
#include <stdio.h>
int main() {
int a, b, c;
printf("Please enter two numbers: ");
scanf("%d %d", &a, &b);
c = a + b;
printf("The sum of the numbers you entered is %d\n", c);
return 0;
}
This is the problem. There is no non-trivial C code that doesn't use some undefined behavior somewhere. And it works just fine, right now. But who says it will on the next version of the compiler?Reference: N1570 (C11) 7.21.6.2 paragraph 10: "If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined."
strtol() avoids this problem.
in this precise case, (in C++ because I can't be arsed to search the modifier for int64), fairly easily :
#include <fmt.h>
#include <cstdio>
int main() {
int a{}, b{};
int64_t c{};
fmt::print("Please enter two numbers: ");
scanf("%d %d", &a, &b);
c = a + b;
fmt::printf("The sum of the numbers you entered is {}\n", c);
return 0;
}
in the case where a, b, were also of the largest int type available, you could do : #include <stdio.h>
int main() {
int a, b, c;
printf("Please enter two numbers: ");
scanf("%d %d", &a, &b);
if(__builtin_add_overflow(a, b, &c))
{
printf("The sum of the numbers you entered is %d\n", c);
}
else
{
printf("You are in a twisty maze of compiler features, all different");
exec("/usr/bin/0ad");
}
return 0;
}
but you could also use the more portable macros provided in emacs (100 % independent of any other code) : https://github.com/jwiegley/emacs-release/blob/adfd5933358fd...(And you're missing a few error checks.)
So yes, they kept the verified code small, so that they could focus on all of the tooling. I'm unconvinced that formal methods intrinsically can't scale.
"C is for speed only"... Do you not realize there is an entire embedded world that runs on C?