Compiling this simple function in thumb mode: int add_const(int x) { return x+400; } I got: mov r1, #200 lsl r3, r1, #1 add r0, r0, r3 A better code sequence should be: add r0, r0, 200 add r0, r0, 200 In order to apply this optimization, the constant should be less than 2 times of the largest immediate value in the target ISA. So this optimization should also useful to other architecture with limited immediate operand range. It can also be applied to sub instruction.
This code comes from the split2 (split insns after reload). So this is target specific. It surprises me that non-legitimate constants are accepted before reload on ARM (at least in thumb mode). It may be (and IMHO, probably is) better to split the constant early, e.g. during expand. A REG_EQUAL note can be used to remember the original value added.
FWIW it looks like the define_split at arm.c:5060 (of r147729) is triggered. Richard E., is there a reason for lowering load-immediates so late in the pipeline?
Working on a patch
Subject: Bug 40327 Author: rearnsha Date: Sat Jun 13 12:49:25 2009 New Revision: 148452 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=148452 Log: PR target/40327 * arm/constraints.md (Pa, Pb): New constraints. * arm/arm.md (thumb1_addsi3): Support more complex additions. Add a split pattern to deal with them. Modified: trunk/gcc/ChangeLog trunk/gcc/config/arm/arm.md trunk/gcc/config/arm/constraints.md
fixed
I would still like to know why these constants can't be lowered earlier. Exposing the split-up insns may allow CSE to do a better job, etc.