I have come to realise that there will be more problems than I had initially thought. In particular, it seems that working out whether a given object is guaranteed to remain constant throughout a loop is difficult, as any other object could potentially hold a reference to it through which to change it opaquely. Even if we assume that there is no concurrency, any call within the loop to a non-pure method on any object could change it.
Presumably people have realised this problem and others before, so I am trying to find out what work has already been done in the area. I have looked at a number of compilers textbooks (listed below) and found some general information about code motion, induction variable elimination, strength reduction and factoring along with lots of data flow analysis to support these, but nothing about the problems with my approach.
- Alfred V Aho, Compilers, Principles, Techniques, and Tools (Reading, Mass: Addison-Wesley Pub. Co, 1986).
- Charles N Fischer, Crafting a Compiler (Menlo Park, Calif: Benjamin/Cummings, 1988).
- 1. Keith D Cooper, Engineering a Compiler (San Francisco, Calif: Morgan Kaufmann, 2004).