Sign in



Don't have an account?

Signing up is free and easy
Home -> Our Services -> Programming Tools BU -> White Papers -> Part 1: Code Movement Transformation

Part 1: Code Movement Transformation

Code movement transformation is unification of the common C statements / assembly instructions, which are computed in multiple code paths and moved to a common point.

Code movement transformations can be classified under two categories:

  • Code Sinking Transformation
  • Code Hoisting Transformation

Code Sinking Transformation

In Code Sinking Transformation the common C statements / assembly instructions are moved down to a common exit point.

Figure 1: C Statement Sinking
Original (Un-optimized) Source
Transformed (Optimized) Source

void

Code_Sinking ()

{

if(x > 5)

{

y = a + b;

x = a * x;

}

else

{

x = fn_call (a);

y = a + b;

}

a = x * z * y;

}

void

Code_Sinking ()

{

if(x > 5)

{

x = a * x;

}

else

{

x = fn_call (a);

}

y = a + b;

a = x * z * y;

}

In the code shown in Figure 1, the statement "y = a + b" is common in both "if" and "else" block. However the statement will be computed only once since the program control enters either "if" or "else" block. Hence, code sinking transformation will move the statement "y = a + b" to a point below the "else" block. This reduces the number of static evaluations of "y = a + b" (but does not reduce the number of dynamic evaluations of "y = a + b").

Figure 2: Assembly Instruction Sinking
Keil C51 Assembly Output
Transformed Assembly Output

; if(DecCng.PastFtyp == 1) Ftyp = 1;

; SOURCE LINE # 137

MOV DPTR,#DecCng+02H

MOVX A,@DPTR

JNZ ?C0062

INC DPTR

MOVX A,@DPTR

XRL A,#01H

?C0062:

JNZ ?C0006

MOV DPTR,#Ftyp?155

MOVX @DPTR,A

INC DPTR

INC A

MOVX @DPTR,A

SJMP ?C0005

?C0006:

; else Ftyp = 0;

; SOURCE LINE # 138

CLR A

MOV DPTR,#Ftyp?155

MOVX @DPTR,A

INC DPTR

MOVX @DPTR,A

; }

; SOURCE LINE # 139

?C0005:

; if(DecCng.PastFtyp == 1) Ftyp = 1;

; SOURCE LINE # 137

MOV DPTR,#DecCng+02H

MOVX A,@DPTR

JNZ ?C0062

INC DPTR

MOVX A,@DPTR

XRL A,#01H

?C0062:

JNZ ?C0006

MOV DPTR,#Ftyp?155

MOVX @DPTR,A

INC DPTR

INC A

SJMP ?C0005

?C0006:

; else Ftyp = 0;

; SOURCE LINE # 138

CLR A

MOV DPTR,#Ftyp?155

MOVX @DPTR,A

INC DPTR

; }

; SOURCE LINE # 139

?C0005:

MOVX @DPTR,A

Keil C51 assembly output for 8051 as shown in LHS column of Figure 2 is generated for the C source code snip from open source file ‘Decod.c’ of ITU-T G.723 Speech Coder.

Figure 3: Snip from Decod.c

/*

* Update the frame erasure count (Text: Section 3.10)

*/

if ( Line.Crc != (Word16) 0 ) {

if(DecCng.PastFtyp == 1) Ftyp = 1; /* active */

else Ftyp = 0; /* untransmitted */

}

In the code shown in Figure 3, storing of value to 'Ftyp' is a candidate for code sinking transformation at instruction-level. Hence, the assembly instruction for store should be sunk as shown in the RHS column of Figure 2.