I am interestent in (partial) compile-time evaluation for c/c++ (not with template parameters like in c++). Lets consider the following case (taken from [1]):
double mypower(double x, int n) {
int i;
double ret = x;
for (i = 1; i < n; i++) {
ret *= x;
}
return ret;
}
Then you call this function somewhere in the code with:
mypower(x,3); // y varies all the time,
Then the compiler could optimize this (e.g. loop unrolling). Some often used function I use could really benefit from that optimization (tested by the creating specialized function manually). The presentation [1] descibes a process where the function is searched and is replaced by a specialised version of the function. This seems to work. But it does not seem to be very universal, code needs to be written for the functions which should be replaced.
The presentation seems to be from 2008, i could not find anything substantial more information than in this source. So has anything improved since then? I would prefer some kind of automatism, which does the same for all function possibly controlled by attribute syntax (e.g. __attribute__(peval)
...). Further I would like the same to work for object-oriented code, creating specialized classes for different objects ([2] seems to suggest that this is not possible).
Additionally, I would like this specialization not only work for constants found in code. I am thinking about a program compiled to LLVM IR (bytecode) could do the following:
Running the programm during an initialization phase in an interpreter, during that initialization phase the program could read some configuration from a file. After the initialization the interpreter is stopped.
Some variables (including member variables) are fixed from that point on. Extract these variables (e.g. marked by attribute during compilation).
Create specialized functions and classes. Clone these into the bytecode.
Run the JIT to create native machine code.
This is a lot I ask for and only few computation intensive programs would benifit from such a optimization. But some people must be working on that. I probably just don't know the right search terms to feed google.
Note: Please don't suggest template classes with non-type parameters or manual specialization, I already do that. I just would prefer the compiler doing the work for me.
Links: