I'm using arm-none-eabi-gcc 4.7.3 or 4.8.3 from launchpad.net, compiling
for an M4 with the following options:
1 | -mcpu=cortex-m4
|
2 | -mthumb
|
3 | -mfloat-abi=hard
|
4 | -mfpu=fpv4-sp-d16
|
5 | -g3
|
6 | -gdwarf-2
|
7 | -gstrict-dwarf
|
8 | -O3
|
9 | -ffunction-sections
|
10 | -fdata-sections
|
11 | -std=gnu99
|
12 | -fsigned-char
|
13 | -D__VFPV4__
|
Here's a small code fragment, part of a FIR filter:
1 | i = T00 + T10 + T20 + T30 + T40 + T50 + T60 + T70
|
2 | + T80 + T90 + TA0 + TB0 + TC0 + TD0 + TE0 + TF0
|
3 | - ((2 * T00) & -((s >> 0) & 1))
|
4 | - ((2 * T10) & -((s >> 2) & 1))
|
5 | - ((2 * T20) & -((s >> 4) & 1))
|
6 | - ((2 * T30) & -((s >> 6) & 1))
|
7 | - ((2 * T40) & -((s >> 8) & 1))
|
8 | - ((2 * T50) & -((s >> 10) & 1))
|
9 | - ((2 * T60) & -((s >> 12) & 1))
|
10 | - ((2 * T70) & -((s >> 14) & 1))
|
11 | - ((2 * T80) & -((s >> 16) & 1))
|
12 | - ((2 * T90) & -((s >> 18) & 1))
|
13 | - ((2 * TA0) & -((s >> 20) & 1))
|
14 | - ((2 * TB0) & -((s >> 22) & 1))
|
15 | - ((2 * TC0) & -((s >> 24) & 1))
|
16 | - ((2 * TD0) & -((s >> 26) & 1))
|
17 | - ((2 * TE0) & -((s >> 28) & 1))
|
18 | - ((2 * TF0) & -((s >> 30) & 1));
|
s is an unsigned int containing bits to be filtered. The T* symbols are
#defined constants. The compiler cleverly compiles -((s >> #) & 1 into a
signed bit-field extract instruction, which picks out the bit, right
justifies it, and propagates it through all 32 bits. For a while, it was
sane enough to load the initial constant (the sum of all the T* symbols)
into a register, then for each bit, compute the mask, AND each one with
the corresponding constant, and subtract it from the register. Then, all
of a sudden, some other change prompted it to compute each mask and
store it into a local variable on the stack, and then use it later.
Since there are actually eight pieces of code like this, the result is
huge, memory-intensive, and slow. This code previously ran at about 3x
real time, now it's on the edge of underrunning (on a Kinetis K70).
What mechanism would prompt the compiler to do such a dumb thing? Is
there any optimization option that relates to this? I've tried both
compiler versions, -O1, -O2, -O3 and -Os, tried various "register"
declarations, tried a bunch of the -fno-blahblah optimization options
listed in the docs, but there are a ton of them. Any ideas?
--
Ciao, Paul D. DeRocco
Paul mailto:pderocco@ix.netcom.com