2 ms·
Nah, 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 t
by TheLoneWolfling 12y ago
Nah, 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.