I'm writing something like a compiler. The problem is following: I have a code, consisting of a sequence of assignments:
t1=a+b+c
t2=t1*d
t3=sqrt(t1+t2)
t4=t2+5
...
most of "t"-variables are temporary. I want to reduce the number of temporary variables, re-using them as much, as possible. So, I need to rearrange the code, grouping expressions, having some variable as close to the variable assignment, so after computing those expressions the variable can be re-used. Of course, I want to preserve code logic during this procedure. Which is the best algorithm to do this?