http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58621
Bug ID: 58621 Summary: With -fsection-anchors, a superfluous 'add' is performed Product: gcc Version: 4.9.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: b.grayson at samsung dot com Target: AArch64 In some scenarios, the code emitted with -fsection-anchors is suboptimal. Here is the generated assembly for some code, compiled with -O3 -fsection-anchors -fno-common: int a; char b; int foo() { return a+b; } 0000000000000000 <foo>: 0: 90000000 adrp x0, 0 <foo> 4: 91000002 add x2, x0, #0x0 8: 39400001 ldrb w1, [x0] c: b9400440 ldr w0, [x2,#4] 10: 0b000020 add w0, w1, w0 14: d65f03c0 ret Here is the source assembly: foo: adrp x0, .LANCHOR0 add x2, x0, :lo12:.LANCHOR0 ldrb w1, [x0,#:lo12:.LANCHOR0] ldr w0, [x2,4] add w0, w1, w0 ret Note the add. It is computing the address of variable b, and using that as the section anchor, so effectively the ldr is using the address x0 + #0 + #4. If the ldr instead used #:lo12:.LANCHOR0 + 4, it would eliminate the extra add instruction: foo: adrp x0, .LANCHOR0 ldrb w1, [x0,#:lo12:.LANCHOR0] ldr w0, [x2,#:lo12:.LANCHOR0+4] add w0, w1, w0 ret Of course, the offset must be small enough to not cross a page boundary from .LANCHOR0's page address, but that restriction is already in there that the offset from address of the chosen section anchor to address of 'a' must be less than a page. (Yes, in this case the add looks amazingly redundant since it is adding 0, but I've seen other cases where the ANCHOR is not at offset 0, in which case we have an add plus an offsetted load, where it could be done with just an offsetted load.)