Code Sinking
Code sinking is an optimization technique used in source-to-source compilers to improve execution efficiency by moving computations to less frequently executed parts of the code. This transformation reduces redundant calculations by relocating expressions outside loops or behind conditional statements, thereby minimizing execution overhead. By strategically repositioning code, code sinking enhances performance without altering program behavior.
Code Sinking Transformation in emmtrix Studio
emmtrix Studio can implement code sinking using #pragma directives or via the GUI. Code sinking is a transformation that tries to move code parts to valid positions that are executed less often.
Typical Usage and Benefits
The transformation is used to optimize the runtime of the application by moving code parts out of loops or behind conditions to reduce the number of times the code is executed.
Example
The following code tests code sinking transformation applied to main function.
#include <stdio.h>
#pragma EMX_TRANSFORMATION CodeSinking
int main(void) {
int n = 10;
for (int i = 0; i < 2; ++i) {
int x = n * n;
int y = n * n * n;
int z;
if (i == 0) {
z = x;
} else {
z = y;
}
printf(” % d\ n”, z);
}
}
Parameters
Following parameters can be set (each description is followed by keyword in pragma-syntax and default value):
Id | Default Value | Description |
---|---|---|
threshold
|
1.45 | Threshold defines the factor by which the number of executions should be reduced in order for an expressions to be moved; |
conditional
|
false | Add conditional variables if necessary adds additional if blocks that are required for some code movements |
expected_moved_exprs
|
-1 | Expected moved expressions can be used for testing purposes by inserting the number of expected moved expressions. Creates an error if the numbers differ, does nothing when set to -1; |
Note
- To move functions from math.h, use the Idiom Recognizer transformation to identify the functions and apply the code sinking transformation afterwards.