This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Teach RISCVMergeBaseOffset to merge %lo/%pcrel_lo into load/store after folding arithmetic.
ClosedPublic

Authored by craig.topper on Aug 1 2022, 12:52 PM.

Details

Summary

It's possible we have:
lui a0, %hi(sym)
addi a0, %lo(sym)
addi a0, <offset1>
lw a0, <offset2>(a0)

We want to arrive at
lui a0, %hi(sym+offset1+offset2)
lw a0, %lo(sym+offset1+offset2)

We currently fail to do this because we only consider loads/stores
if we didn't find any arithmetic.

This patch splits arithmetic folding and load/store folding into
two separate phases. The load/store folding can no longer assume
the offset in hi/lo is 0 so we must combine the offsets. I've applied
the same simm32 limit that we applied in the arithmetic folding.

Diff Detail

Event Timeline

craig.topper created this revision.Aug 1 2022, 12:52 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 1 2022, 12:52 PM
craig.topper requested review of this revision.Aug 1 2022, 12:52 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 1 2022, 12:52 PM
luismarques accepted this revision.Aug 1 2022, 2:58 PM

LGTM.

(I wonder if it's worth adding a read-modify-write-addi-addi test case as well?)

llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp
324–332

Nit: needs updating

This revision is now accepted and ready to land.Aug 1 2022, 2:58 PM
This revision was landed with ongoing or failed builds.Aug 1 2022, 3:35 PM
This revision was automatically updated to reflect the committed changes.
luismarques added inline comments.Aug 1 2022, 3:49 PM
llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll
392–402

Why was the branch needed here? Was there an issue with something simpler, like this?

%x = load i8, i8* getelementptr inbounds ([0 x i8], [0 x i8]* @bar, i32 0, i64 3211)
%y = add i8 %x, 10
store i8 %y, i8* getelementptr inbounds ([0 x i8], [0 x i8]* @bar, i32 0, i64 3211)
craig.topper added inline comments.Aug 1 2022, 4:05 PM
llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll
392–402

No. I'll simplify it.