I've been using gcc for the last couple of months to program MSP430 microcontrollers, and have noticed that the compiler often produces suboptimal code. For example, something as simple as
if (value & 0x80) {}
gets compiled (-O3 or -Os) into
- a right shift by 7
- a logical AND with 0xff
Of course, because MSP430 does not support shifts, gcc links about 200 bytes of extra code, and executes a total of ~30 instructions.
The straightforward implementation would be to just do the AND with 0x80 and compare the result to zero, or if one wanted to be clever, use the BIT instruction.
It is my understanding that GCC decides what instructions to use partially based on the TARGET_RTX_COSTS function in msp430.c:
#define TARGET_RTX_COSTS msp430_rtx_costs static bool msp430_rtx_costs (rtx x ATTRIBUTE_UNUSED, int code, int outer_code ATTRIBUTE_UNUSED, int opno ATTRIBUTE_UNUSED, int * total, bool speed ATTRIBUTE_UNUSED) { switch (code) { case SIGN_EXTEND: if (GET_MODE (x) == SImode && outer_code == SET) { *total = COSTS_N_INSNS (4); return true; } break; case ASHIFT: case ASHIFTRT: case LSHIFTRT: if (!msp430x) { *total = COSTS_N_INSNS (100); return true; } break; } return false; }
This seems to penalize shifts, and I would expect gcc to avoid them in the case I mentioned above. However, if I change the costs in that function to anything else, such as 1 or 100000, and recompile my binary, there is absolutely no change (same md5 sum). So somehow it appears that the TARGET_RTX_COSTS function has no effect. Does anyone know why this might be happening?
Also, even if TARGET_RTX_COSTS worked correctlly, it is virtually empty. For example, it makes no mention of the fact that multiplication is not supported in hardware and should be avoided. The function is 19 lines long; by comparison, for AVR microcontrollers, the equivalent is 822 lines long!
Are the maintainers of MSP430-GCC planning to invest in this area in the near future? A quick start would be to take the TARGET_RTX_COSTS function from mspgcc (MSP430-GCC's predecessor), which is about 130 lines long.
Just to be clear, I realize that this project just started recently, is in beta, and is open source. I do not mean to criticize it in any way.
Thanks!