This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Separate base from offset in lowerGlobalAddress
ClosedPublic

Authored by sabuasal on May 16 2018, 4:16 PM.

Details

Summary

When lowering global address, lower the base as a TargetGlobal first then
create an SDNode for the offset separately and chain it to the address calculation

This optimization will create a DAG where the base address of a global access will
be reused between different access. The offset can later be folded into the immediate
part of the memory access instruction.

  
With this optimization we generate:
 
  lui a0, %hi(s)
  addi a0, a0, %lo(s) ; shared base address.

  addi a1, zero, 20 ; 2 instructions per access.
  sw a1, 44(a0)

  addi a1, zero, 10
  sw a1, 8(a0)

  addi a1, zero, 30
  sw a1, 80(a0)

  Instead of:

  lui a0, %hi(s+44) ; 3 instructions per access.
  addi a1, zero, 20
  sw a1, %lo(s+44)(a0)
 
  lui a0, %hi(s+8)
  addi a1, zero, 10
  sw a1, %lo(s+8)(a0)

  lui a0, %hi(s+80)
  addi a1, zero, 30
  sw a1, %lo(s+80)(a0)
 
  Which will save one instruction per access.

Diff Detail

Repository
rL LLVM

Event Timeline

sabuasal created this revision.May 16 2018, 4:16 PM
sabuasal edited the summary of this revision. (Show Details)
sabuasal edited the summary of this revision. (Show Details)
sabuasal retitled this revision from [RISCV] Separate base from offset in lowerGlobalAddress to [RISCV] Separate base from offset in lowerGlobalAddress (no peephole).May 16 2018, 5:05 PM
asb accepted this revision.May 17 2018, 2:25 AM

Thanks Sameer, this looks good to me. It would be worth adding some sort of comment to lowerGlobalAddress about the decision to emit a separate ADD node rather than folding the offset into the global address.

lib/Target/RISCV/RISCVISelLowering.cpp
300 ↗(On Diff #147211)

It would be good to add a comment here documenting the decision not to fold-the offset into the global address. e.g. "In order to maximise the opportunity for common subexpression elimination, emit a separate ADD node for the global address offset instead of folding it in the global address node. Later peephole optimisations may choose to fold it back in when profitable."

This revision is now accepted and ready to land.May 17 2018, 2:25 AM
sabuasal updated this revision to Diff 147358.May 17 2018, 11:12 AM
sabuasal marked an inline comment as done.
sabuasal retitled this revision from [RISCV] Separate base from offset in lowerGlobalAddress (no peephole) to [RISCV] Separate base from offset in lowerGlobalAddress.
This revision was automatically updated to reflect the committed changes.