Avoid unnecessarily saving $lr on Thumb-1

Bug #625228 reported by Andrew Stubbs
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Linaro GCC
Won't Fix
Wishlist
Michael Collison

Bug Description

GCC produces sub-optimal code for ARM Thumb1 due to the limited range of the Thumb1 branch instruction.

The long story is that Thumb-1 branches have extremely limited range: O(256) bytes. The way we "solve" this is to use the branch-and-link instruction as a long branch.

Unfortunately we need to know register usage for reload to caluclate frame offsets, and this is way before we know how big all the instructions are. The current solution is to assume that any branch may be big.

Currently we have no way of fixing up out of range branches, so this estimare must be conservatively correct.

It may be able to do slightly better if we assume that we will never get more than say 10x code expansion between freezing frame offsets and final assembly.

One possibility is to teach gcc how to generate branch islands within a function, chaining if necessary to extend the range. This would allow us to make a less conservative guess on branch ranges, and fixup out of range branches when LR is not saved. It may also have secondary code size benefits by allowing multiple long branches/calls to be chained using smaller instructions.

How to reproduce:

  void foo(int *i) { if (*i>1) *i = 1; else *i = 2; }

With thumb2, all is well:

foo:
        ldr r3, [r0, #0]
        cmp r3, #1
        ble .L2
        movs r3, #1
        str r3, [r0, #0]
        bx lr
.L2:
        movs r3, #2
        str r3, [r0, #0]
        bx lr

But, with thumb1, there's an unnecessary stack push of lr:

foo:
        push {lr}
        ldr r3, [r0]
        cmp r3, #1
        ble .L2
        mov r3, #1
        str r3, [r0]
.L4:
        pop {r1}
        bx r1
.L2:
        mov r3, #2
        str r3, [r0]
        b .L4

[CodeSourcery Tracker ID #797]

Changed in gcc-linaro:
importance: Undecided → Low
tags: added: armel toolchain
Revision history for this message
Loïc Minier (lool) wrote :

Is this just for tracking, or do we want to spend time improving Thumb-1 support?!

Revision history for this message
Andrew Stubbs (ams-codesourcery) wrote :

Just for tracking probably. I've submitted it as part of my task to transfer relevant CS bugs to Linaro.

Thumb-1 may not be interesting for ARM Ubuntu, nor for the Linaro release this year, but Linaro's stated aim is to make general ARM improvements, and this may be of interest in future.

Revision history for this message
Michael Hope (michaelh1) wrote :

Linaro is focused on the Cortex-A series where Thumb-2 is always available. I'll get this confirmed then we shoud 'wontfix' this bug from a Linaro point of view.

Revision history for this message
Loïc Minier (lool) wrote :

If that only shows up when thumb-2 is disabled, I certainly agree we wont do anything about it; there's the question of what we do with bugs which would be good targets for community contributions.

Revision history for this message
Andrew Stubbs (ams-codesourcery) wrote :

Setting it to "Won't Fix" is throwing it away. I'm not sure that's appropriate.

Perhaps we should assign it to a milestone "Won't fix" or something?

Revision history for this message
Loïc Minier (lool) wrote :

Let's mark it as wishlist priority, which is effectively what it is: something we're unlikely to ever work on, unless we really have copious amount of free time, or someone contributes a patch candidate

Changed in gcc-linaro:
importance: Low → Wishlist
status: New → Triaged
Michael Hope (michaelh1)
tags: added: speed task
Revision history for this message
Michael Collison (michael-collison) wrote :

Will not fix due to lower priority of Thumb-1

Changed in gcc-linaro:
assignee: nobody → Michael Collison (michael-collison)
status: Triaged → Won't Fix
Revision history for this message
Joey Ye (jinyun-ye) wrote :

It is actually fixed in 4.8.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.