Bug 40327 - Use fewer instructions to add some constants to register
Summary: Use fewer instructions to add some constants to register
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.5.0
: P3 normal
Target Milestone: ---
Assignee: Richard Earnshaw
URL:
Keywords:
Depends on:
Blocks: 16996
  Show dependency treegraph
 
Reported: 2009-06-03 07:39 UTC by Carrot
Modified: 2009-06-13 23:48 UTC (History)
4 users (show)

See Also:
Host:
Target: arm-eabi
Build:
Known to work:
Known to fail:
Last reconfirmed: 2009-06-09 22:06:44


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Carrot 2009-06-03 07:39:18 UTC
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.
Comment 1 Steven Bosscher 2009-06-03 10:36:13 UTC
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.

Comment 2 Steven Bosscher 2009-06-03 10:57:46 UTC
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?
Comment 3 Richard Earnshaw 2009-06-09 22:06:44 UTC
Working on a patch
Comment 4 Richard Earnshaw 2009-06-13 12:49:39 UTC
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

Comment 5 Richard Earnshaw 2009-06-13 13:04:09 UTC
fixed
Comment 6 Steven Bosscher 2009-06-13 23:45:01 UTC
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.