On Thu, 9 Sep 2010, Andrew Stubbs wrote:
On 09/09/10 16:22, Yao Qi wrote:
GCC produces code like this, 00000024<unPack>: 24: f000 000f and.w r0, r0, #15 28: 2807 cmp r0, #7 2a: d901 bls.n 30<unPack+0xc> 2c: 3810 subs r0, #16 2e: b280 uxth r0, r0<--[1] 30: b200 sxth r0, r0<--[2] 32: 4770 bx lr
Are instruction [1] and [2] redundant? Can we remove these two instructions? If they are redundant, we can remove them safely.
Yes, I'd say they were redundant.
In one code path, the result is always positive, and strictly <16, so the sign extend is a NOP.
In the other code path, UXTH followed by SXTH is always equivalent to SXTH alone, regardless of input.
I wondered for a while whether the extension or rotation did anything cunning to the status register, or something, but it seems not.
Of course, the optimal code sequence for this function would be:
lsl r0, r0, #28 asr r0, r0, #28 bx lr
But I doubt gcc could ever become that smart.
Nicolas