On 7/1/2013 4:30 PM, Andrei Alexandrescu wrote:
Yes, and in fact it's already done. Consider:

if (expr)
{
     int a;
     ...
}
else
{
     int b;
     ...
}

In some C implementations, a and b have the same physical address. In some
others, they have distinct addresses. This appears to not be related, but it is
insofar as a and b have non-overlapping lifetimes.

What is happening with (modern) compilers is the "live range" of each variable is computed. A live range is nothing more than a bitmap across the instructions for a function, with a bit set meaning "the variable is in play at this point".

The compiler then uses a "tetris" style algorithm to try to fit as many variables as possible into the limited register set, and to use as little stack space as possible.

The usual algorithms do not use scoping to determine the live range, but look at actual usage. A variable that, for example, that has no usage is considered 'dead' and is removed.

The proposal here neither adds nor subtracts from this.

Reply via email to