Hi
I'm getting strange "optimized" compiler output. I've made a minimal test case below to demonstrate (being a struct doesn't matter):
struct
{
double a, b, c, d;
} teststruct;
int main(void)
{
teststruct.a = 0;
teststruct.b = 0;
teststruct.c = 0;
teststruct.d = 0;
return 0;
}
With optimization disable or -o0 the assembly is
ZERO R0H ; [CPU_FPU] |8|
MOVW DP,#_teststruct ; [CPU_ARAU]
MOV32 @_teststruct,R0H ; [CPU_FPU] |8|
MOV32 @_teststruct+2,R0H ; [CPU_FPU] |9|
MOV32 @_teststruct+4,R0H ; [CPU_FPU] |10|
MOV32 @_teststruct+6,R0H ; [CPU_FPU] |11|
MOVB AL,#0 ; [CPU_ALU] |13|
LRETR ; [CPU_ALU]
With optimization at -o1 or above the assembly is
ZERO R3H ; [CPU_FPU] |8|
MOVW DP,#_teststruct ; [CPU_ARAU]
ZERO R2H ; [CPU_FPU] |9|
ZERO R1H ; [CPU_FPU] |10|
ZERO R0H ; [CPU_FPU] |11|
MOV32 @_teststruct,R3H ; [CPU_FPU] |8|
MOVB AL,#0 ; [CPU_ALU] |13|
MOV32 @_teststruct+2,R2H ; [CPU_FPU] |9|
MOV32 @_teststruct+4,R1H ; [CPU_FPU] |10|
MOV32 @_teststruct+6,R0H ; [CPU_FPU] |11|
LRETR ; [CPU_ALU]
In the unoptimized output zero is assigned to a single register than that is used for all variables. In the optimized version zero is first assigned to four registers then each register is uniquely assigned to a variable. This slows things down a lot when you want to zero a bunch of variables in a row in your time critical interrupt!
Any explanation or help in writing the C to get a better compiler output?