[PATCH] D126843: [RISCV] Support (addi (addi globaladdr, C1), C2) in RISCVMergeBaseOffset.
Craig Topper via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 1 17:59:43 PDT 2022
craig.topper created this revision.
craig.topper added reviewers: asb, luismarques, jrtc27, reames.
Herald added subscribers: sunshaoce, VincentWu, luke957, StephenFan, vkmr, frasercrmck, evandro, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, shiva0217, kito-cheng, niosHD, sabuasal, simoncook, johnrusso, rbar, hiraditya, arichardson.
Herald added a project: All.
craig.topper requested review of this revision.
Herald added subscribers: pcwang-thead, eopXD, MaskRay.
Herald added a project: LLVM.
Add with immediates in the range [-4096, -2049] or [2048, 4095] get
convert to two ADDIs. Teach RISCVMergeBaseOffset to recognize this
pattern as well.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D126843
Files:
llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp
llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll
Index: llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll
===================================================================
--- llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll
+++ llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll
@@ -248,3 +248,21 @@
; RV64-NEXT: ret
ret i8* getelementptr inbounds ([0 x i8], [0 x i8]* @bar, i32 0, i64 -2147485013)
}
+
+define i8* @offset_addi_addi() {
+; CHECK-LABEL: offset_addi_addi:
+; CHECK: # %bb.0:
+; CHECK-NEXT: lui a0, %hi(bar+3211)
+; CHECK-NEXT: addi a0, a0, %lo(bar+3211)
+; CHECK-NEXT: ret
+ ret i8* getelementptr inbounds ([0 x i8], [0 x i8]* @bar, i32 0, i64 3211)
+}
+
+define i8* @offset_addi_addi_neg() {
+; CHECK-LABEL: offset_addi_addi_neg:
+; CHECK: # %bb.0:
+; CHECK-NEXT: lui a0, %hi(bar-4000)
+; CHECK-NEXT: addi a0, a0, %lo(bar-4000)
+; CHECK-NEXT: ret
+ ret i8* getelementptr inbounds ([0 x i8], [0 x i8]* @bar, i32 0, i64 -4000)
+}
Index: llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp
===================================================================
--- llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp
+++ llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp
@@ -206,6 +206,19 @@
case RISCV::ADDI: {
// Offset is simply an immediate operand.
int64_t Offset = Tail.getOperand(2).getImm();
+
+ // We might have two ADDIs in a row.
+ Register TailDestReg = Tail.getOperand(0).getReg();
+ if (MRI->hasOneUse(TailDestReg)) {
+ MachineInstr &TailTail = *MRI->use_instr_begin(TailDestReg);
+ if (TailTail.getOpcode() == RISCV::ADDI) {
+ Offset += TailTail.getOperand(2).getImm();
+ DeadInstrs.insert(&Tail);
+ foldOffset(HiLUI, LoADDI, TailTail, Offset);
+ return true;
+ }
+ }
+
LLVM_DEBUG(dbgs() << " Offset Instr: " << Tail);
foldOffset(HiLUI, LoADDI, Tail, Offset);
return true;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126843.433602.patch
Type: text/x-patch
Size: 1869 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220602/058a1576/attachment.bin>
More information about the llvm-commits
mailing list