The definition of a stage boundary is that the working set changes, either for the code or for the data, sometimes for both. The stages of a particular program depend on the problem domain and the implementation. I believe that the importance of these stages to optimization has been under-appreciated. First the algorithm need to identify stages and then it needs to take advantage of their existence. I have chosen to use loops as the definition of a stage .
Natural loops have a nice property.
We want to find the stage-level nesting relationship for an entire program and to label each stage. The Procedure-CSL of a procedure is the MAX of the CSLs of all calls to that procedure. The CSL is a static estimate of the execution frequency of a basic block and is therefore a numeric value for the amount of code reuse. The interesting case to handle is call graphs with cycles. Cycles indicate recursive procedures.