Advanced Code Motion Techniques
In Code optimization phase of compiler, the main aim is to increase the overall performance of the code. Code Motion Techniques are used to improve the performance of the program.
Loop Invariant Code Motion
C++
int main() { int sum=0; for ( int i=0;i<5;i++) { sum+=5; cout<<i; } } //moving loop invariant code out of loop int main() { int sum=0; sum+=5; for ( int i=0;i<5;i++) { cout<<i; } } |
Partial Redundancy Elimination
C++
//before void prod( int a, int b) { int ans= 0; for ( int i=0;i<4;i++) { ans+=a*b; } cout<<ans; } //after void prod( int a, int b) { int z=a*b; int ans= 0; for ( int i=0;i<4;i++) { ans+=z; } cout<<ans; } |
In this example, we remove the partial redundancy by computing a*b only one time instead of computing it repeatedly inside a loop.
Dead Code Elimination: It is the code that is unreachable or has no effect on the program.
C++
//before dead code elimination int sum( int a, int b) { int sum = 0; if (a>0) { sum=a+b; } return sum; else { ... } } //after dead code elimination int sum( int a, int b) { int sum = 0; if (a>0) { sum=a+b; } return sum; } |
In this example, the else part is removed as it is unreachable part of the code.
Loop Carried Code Motion: It is same as loop invariant code motion. In this, the loop invariant part of the loop is moved out of the loop to reduce the number of computations.
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
Contact Us