What Kind of Optimization Is Being Performed?
3-20
3.8.8 Branch Optimizations and Control-Flow Simplification
The compiler analyzes the branching behavior of a program and rearranges
the linear sequences of operations (basic blocks) to remove branches or
redundant conditions. Unreachable code is deleted, branches to branches are
bypassed, and conditional branches over unconditional branches are simpli-
fied to a single conditional branch. When the value of a condition can be deter-
mined at compile time (through copy propagation or other data flow analysis),
a conditional branch can be deleted. Switch case lists are analyzed in the
same way as conditional branches and are sometimes eliminated entirely.
Some simple control-flow constructs can be reduced to conditional instruc-
tions, totally eliminating the need for branches. See Example 3
Example 3
−
4. Copy Propagation and Control-Flow Simplification
fsm()
{
enum { ALPHA, BETA, GAMMA, OMEGA } state = ALPHA;
int *input;
while (state != OMEGA)
switch (state)
{
case ALPHA: state = ( *input++ == 0 ) ? BETA: GAMMA; break;
case BETA : state = ( *input++ == 0 ) ? GAMMA: ALPHA; break;
case GAMMA: state = ( *input++ == 0 ) ? GAMMA: OMEGA; break;
}
}
TMS320C2x/C2xx/C5x C Compiler Output:
_fsm:
. . .
*
* AR5 assigned to variable ’input’
*
LAC
*+
; initial state == ALPHA
BNZ
L5
; if (input != 0) go to state GAMMA
L2:
LAC
*+
; state == BETA
BZ
L4
; if (input == 0) go to state GAMMA
LAC
*+
; state == ALPHA
BZ
L2
; if (input == 0) go to state BETA
B
L5
; else go to state GAMMA
L4:
LAC
*+
; state == GAMMA
BNZ
EPI0_1
; if (input != 0) go to state OMEGA
L5:
LARP
AR5
L6:
LAC
*+
; state = GAMMA
BZ
L6
; if (input == 0) go to state GAMMA
EPI0_1:
; state == OMEGA
. . .
The switch statement and the state variable from this simple finite state machine example
are optimized completely away, leaving a streamlined series of conditional branches.
Summary of Contents for TMS320C2x
Page 8: ...viii...
Page 69: ...2 47 C Compiler Description...
Page 159: ...6 36...
Page 226: ...8 6...