Upward Code Motion
Upward Code Motion is another technique used to improve performance in Global Code Scheduling. In this, the code segment is moved outside of the block or loop to a position above the block.
Goals of Upward Code Motion:
- Reduces computational overheads
- Eliminates repeated calculations
- Improves performance
Steps involved in Upward Code Motion:
- Identification of loop invariant code
- Moving the invariant code upward
- Updating variable dependencies accordingly
C++
//before upward code motion void sum( int a, int b) { int ans= 0; for ( int i=0;i<4;i++) { ans+=a+b; } cout<<ans; } //after upward code motion void sum( int a, int b) { int z=a+b; //a+b is assigned to a variable z to avoid //repeated computaion of a+b inside the for loop int ans= 0; for ( int i=0;i<4;i++) { ans+=z; } cout<<ans; } |
Global Code Scheduling in Compiler Design
In the fifth phase of compiler design, code optimization is performed. There are various code optimization techniques. But the order of execution of code in a computer program also matters in code optimization. Global Code Scheduling in compiler design is the process that is performed to rearrange the order of execution of code which improves performance. It comprises the analysis of different code segments and finding out the dependency among them.
The goals of Global Code Scheduling are:
- Optimize the execution order
- Improving the performance
- Reducing the idle time
- Maximize the utilization of resources
There are various techniques to perform Global Code Scheduling:
- Primitive Code Motion
- Upward Code Motion
- Downward Code Motion