Primitive Code Motion
Primitive Code Motion is one of the techniques used to improve performance in Global Code Scheduling. As the name suggests, Code motion is performed in this. Code segments are moved outside of the basic blocks or loops which helps in reducing memory accesses, thus improving the performance.
Goals of Primitive code motion:
- Eliminates repeated calculations
- Reduces redundant computations
- Improving performance by reducing number of operations
Primitive Code Motion can be done in 3 ways as follows
Code Hoisting: In this technique, the code segment is moved from inside a loop to outside the loop. It is done when the output of the code segment does not change with loop’s iteration. It reduces loop overhead and redundant computation.
C++
//before code hoisting int main() { int x,y,b,a; x=1,y=2,a=0; while (a<10) { b=x+y; cout<<a; a++; } } //after code hoisting int main() { int x,y,b,a; x=1,y=2,a=0; b=x+y; while (a<10) { cout<<a; a++; } } |
Code Sinking: In this technique, the code segment is moved from outside to inside the loop. It is performed when the code’s output changes with each iteration of the loop. It reduces the number of computation.
C++
//before code sinking int main() { int a,b; a=0,b=1; for ( int i=0;i<5;i++) { cout<<a++; } for ( int i=0;i<5;i++) { cout<<b++; } } //after code sinking int main() { int a,b; a=0,b=1; for ( int i=0;i<5;i++) { cout<<a++; cout<<b++; } } |
Memory Access Optimization: In this technique, the memory’s read or write operation is moved out from the loops or blocks. This method eliminates redundant memory accesses and enhances cache utilization.
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