Next: Optimizations for the compiler performances
Up: Compiler Theory: Intermediate Code Generation
Previous: Boolean Expressions and Control Flow
REDUCING THE NUMBER OF TEMPORARIES. Temporaries are used to hold intermediate values
when evaluated expressions need to be stored in the symbol-table.
- They can require a lot of space for their values.
- Moreover wasting space implies wasting time.
Indeed they have to be loaded into registers, written back, etc.
- For nowdays compilers, time performance is a crucial issue
since space is almost illimited.
Temporaries can be re-used!
- There are a lot of different approaches to doing this
- and a lot of discussion about when during compilation it should be done.
A simple approach is to change newtemp.
- Consider the rule
E E1 + E2
- Assume E1 and E2 are evaluated into the temporaries
t1 and t2 respectively.
- From the rules for the synthesized attribute E.place
it follows that t1 and t2 are not used anywhere else in the program.
- Moreover, if we do not use DAGS for arithmetic expressions
then the lifetime of each temporary used in the evaluation of Ei
is contained in the lifetime of ti.
- Hence we can modify newtemp
- so that it assigns names for temporaries in a last-in, first-out manner.
- To do so we could store each temporary t together with its scope s(t)
such that we can reuse t in another scope from which s(t) is not visible.
Scopes are naturally organized as a tree T and it is natural to associate
a scope s with its depth d (s) as a node of T (the length of a shortest path
from the root of T to s).
For most languages (including MOOL, ALLCOT),
if the current scope s' (entered after exiting s(t)) satisfies
d (s') < d (s(t))
then we can reuse t.
We will come back to the topic of code optimization in the next chapter.
Next: Optimizations for the compiler performances
Up: Compiler Theory: Intermediate Code Generation
Previous: Boolean Expressions and Control Flow
Marc Moreno Maza
2004-12-02