0

I am implementing a compiler for a proprietary language.

The language has one built-in integer type, with unlimited range. Sometimes variables are represented using smaller types, for example if a and b are integer variables but b is only ever assigned the value of the expression a % 100000 or a & 0xFFFFFF, then b can be represented as an Int32 instead.

I am considering implementing the following optimization. Suppose it sees the equivalent of this C# method:

public static void Main(string[] args)
{
    BigInt i = 0;
    while (true)
    {
        DoStuff(i++);
    }
}

Mathematically speaking, transforming into the following is not valid:

public static void Main(string[] args)
{
    Int64 i = 0;
    while (true)
    {
        DoStuff(i++);
    }
}

Because I have replaced a BigInt with an Int64, which will eventually overflow if the loop runs forever. However I suspect I can ignore this possibility because:

  • i is initialized to 0 and is modified only by repeatedly adding 1 to it, which means that will take 263 iterations of the loop to make it overflow
  • If DoStuff does any useful work, it will take centuries (extrapolated from my very crude tests) for i to overflow. The machine the program runs on will not last that long. Not only that but its architecture probably won't last that long either, so I also don't need to worry about it running on a VM that is migrated to new hardware.
  • If DoStuff does not do any useful work, an operator will eventually notice that it is wasting CPU cycles and kill the process

So what scenarios do I need to worry about?
Do any compilers already use this hack?

finnw
  • 47,861
  • 24
  • 143
  • 221
  • Does your BigInt have a small int optimization? If so then I doubt your optimization is all that exciting, although I guess it still avoids a couple of tests, and depending on your proprietary language maybe avoids a call out-of-line to perform the increment. If not then I'd say get on with implementing one first, before going around looking for narrow situations where you can apply one outside the BigInt code :-) – Steve Jessop Jan 25 '11 at 17:28

1 Answers1

0

Well.. It seems to me you already answered your question.

But I doubt the question really has any useful outcome.

If the only built-in integer has unlimited range by default it should not inefficient for typical usage such as a loop counter.

I think expanding value range (and allocate more memory to the variable) only after actual overflow occur won't that hard for such language.

9dan
  • 4,222
  • 2
  • 29
  • 44