4 ms·
No it doesn't. For example, in LLVM "x - x" for int x isn't always zero if x is undef. (Skipping over syntax here) Quoting from the linked document: > This c
by TheLoneWolfling 12y ago
No it doesn't.
For example, in LLVM "x - x" for int x isn't always zero if x is undef. (Skipping over syntax here)
Quoting from the linked document:
> This can be surprising to people (and also matches C semantics) where they assume that “X^X” is always zero, even if X is undefined.
> the short answer is that an ‘undef‘ “variable” can arbitrarily change its value over its “live range”.
- pcwalton 12y agoOh, you actually want a live range to be allocated for the value. No, I'm not aware of any programming language that works like this (nor do I think the benefits would be worth it since you'd be increasing register pressure).
- TheLoneWolfling 12y agoI'm not so sure. The number of debugging hours that have been spent because of UB causing globally logically inconstant behavior... And it's better in terms of register pressure than the standard alternative - namely implicitly initializing everything.
- chongli 12y agoSo you want a language with a computer algebra system (CAS) that reduces expressions such as "x - x" at compile time. This has been doable in Lisp for a long time using macros. It'd be a bit trickier to do in a C-like language due to the syntax but still reasonable.
- TheLoneWolfling 12y agoNah, nothing quite that bad. Most of it could be done with standard optimization techniques that the compiler's already doing. Though you could use a CAS for this, I suppose. Mind you, if you've got a CAS you may as well start using it for more than just this part of the compiler - and that way lies madness. It's relatively easy. For example: int foo(int a) { int x; if (bar()) x = 0; return a*(x - x) + x; } You start with a first pass by just treating variables that may not being defined as though they are parameters to the function. (I'm doing this all in c-ish syntax, just imagine it's actually in the compiler's IR.) So, the above gets mangled into: int foo(int a, int x) { if (bar()) x = 0; return a*(x - x) + x; } And optimized at this stage as normal into int foo(int a, int x) { if (bar()) x = 0; return x; } And... Here's where a CAS comes in handy. Though for most cases you should be able to do pretty well by just choosing any potentially undefined variables to be the first conditional assignment to it. (Or, more likely, searching among "all" (with a limit) possible first assignments to the variable. This gets complex if you've got many variables that are potentially undefined, but that shouldn't happen often. And again, standard optimization techniques.) Which in this case turns into: int foo(int a) { x = 0; if (bar()) x = 0; return x; } And then into int foo(int a) { return 0; } as expected.