Slide 50 of 67
Notes:
Level 2 performs all level 0 and level 1 optimizations, plus more.
It does a number of loop optimizations. The algorithms associated with loop optimization are fairly complicated, and several books have been written on these techniques. However, the goal is simple enough: to make the loop as efficient as possible.
In level 1 optimization, local common sub-expressions and dead assignments were eliminated. Now we're going to eliminate similar things globally in Level 2 optimization.
Finally, one of the most important optimizations in level two is loop unrolling. Consider a loop that's not very long, two or three iterations. By simply rewriting that code two or three times, all of the overhead associated with initializing the loop could be eliminated. If the loop was 1,000 iterations, it would probably be inefficient to unroll that loop. But if the loop was short, we could improve the efficiency of the code. We would increase the code size slightly, but the program would run faster.