[llvm] [RISCV] Add a pass to remove ADDI by reassociating to fold into load/store address. (PR #127151)
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 18 17:47:51 PST 2025
https://github.com/topperc updated https://github.com/llvm/llvm-project/pull/127151
>From a381a213d73d2ed46624fd9b6fb652449aaf224d Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Thu, 13 Feb 2025 14:12:49 -0800
Subject: [PATCH 1/6] [RISCV] Add test cases showing opportunities to
reassociate ADDI into load/store address. NFC
---
llvm/test/CodeGen/RISCV/fold-mem-offset.ll | 772 +++++++++++++++++++++
1 file changed, 772 insertions(+)
create mode 100644 llvm/test/CodeGen/RISCV/fold-mem-offset.ll
diff --git a/llvm/test/CodeGen/RISCV/fold-mem-offset.ll b/llvm/test/CodeGen/RISCV/fold-mem-offset.ll
new file mode 100644
index 0000000000000..25d7faebe1615
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/fold-mem-offset.ll
@@ -0,0 +1,772 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: sed 's/iXLen/i32/g' %s | llc -mtriple=riscv32 | FileCheck %s --check-prefixes=CHECK,RV32I
+; RUN: sed 's/iXLen/i64/g' %s | llc -mtriple=riscv64 | FileCheck %s --check-prefixes=CHECK,RV64I
+; RUN: sed 's/iXLen/i32/g' %s | llc -mtriple=riscv32 -mattr=+zba | FileCheck %s --check-prefixes=ZBA,RV32ZBA
+; RUN: sed 's/iXLen/i64/g' %s | llc -mtriple=riscv64 -mattr=+zba | FileCheck %s --check-prefixes=ZBA,RV64ZBA
+
+define i64 @test_sh3add(ptr %p, iXLen %x, iXLen %y) {
+; RV32I-LABEL: test_sh3add:
+; RV32I: # %bb.0: # %entry
+; RV32I-NEXT: addi a0, a0, 400
+; RV32I-NEXT: slli a1, a1, 3
+; RV32I-NEXT: slli a2, a2, 3
+; RV32I-NEXT: add a1, a1, a0
+; RV32I-NEXT: add a0, a0, a2
+; RV32I-NEXT: lw a2, 80(a1)
+; RV32I-NEXT: lw a1, 84(a1)
+; RV32I-NEXT: lw a3, 0(a0)
+; RV32I-NEXT: lw a0, 4(a0)
+; RV32I-NEXT: add a1, a0, a1
+; RV32I-NEXT: add a0, a3, a2
+; RV32I-NEXT: sltu a2, a0, a3
+; RV32I-NEXT: add a1, a1, a2
+; RV32I-NEXT: ret
+;
+; RV64I-LABEL: test_sh3add:
+; RV64I: # %bb.0: # %entry
+; RV64I-NEXT: addi a0, a0, 400
+; RV64I-NEXT: slli a1, a1, 3
+; RV64I-NEXT: slli a2, a2, 3
+; RV64I-NEXT: add a1, a1, a0
+; RV64I-NEXT: add a0, a0, a2
+; RV64I-NEXT: ld a1, 80(a1)
+; RV64I-NEXT: ld a0, 0(a0)
+; RV64I-NEXT: add a0, a0, a1
+; RV64I-NEXT: ret
+;
+; RV32ZBA-LABEL: test_sh3add:
+; RV32ZBA: # %bb.0: # %entry
+; RV32ZBA-NEXT: addi a0, a0, 400
+; RV32ZBA-NEXT: sh3add a1, a1, a0
+; RV32ZBA-NEXT: sh3add a0, a2, a0
+; RV32ZBA-NEXT: lw a2, 80(a1)
+; RV32ZBA-NEXT: lw a1, 84(a1)
+; RV32ZBA-NEXT: lw a3, 0(a0)
+; RV32ZBA-NEXT: lw a0, 4(a0)
+; RV32ZBA-NEXT: add a1, a0, a1
+; RV32ZBA-NEXT: add a0, a3, a2
+; RV32ZBA-NEXT: sltu a2, a0, a3
+; RV32ZBA-NEXT: add a1, a1, a2
+; RV32ZBA-NEXT: ret
+;
+; RV64ZBA-LABEL: test_sh3add:
+; RV64ZBA: # %bb.0: # %entry
+; RV64ZBA-NEXT: addi a0, a0, 400
+; RV64ZBA-NEXT: sh3add a1, a1, a0
+; RV64ZBA-NEXT: sh3add a0, a2, a0
+; RV64ZBA-NEXT: ld a1, 80(a1)
+; RV64ZBA-NEXT: ld a0, 0(a0)
+; RV64ZBA-NEXT: add a0, a0, a1
+; RV64ZBA-NEXT: ret
+entry:
+ %b = getelementptr inbounds nuw i8, ptr %p, i64 400
+ %add = add iXLen %x, 10
+ %arrayidx = getelementptr inbounds nuw [100 x i64], ptr %b, i64 0, iXLen %add
+ %0 = load i64, ptr %arrayidx, align 8
+ %arrayidx2 = getelementptr inbounds nuw [100 x i64], ptr %b, i64 0, iXLen %y
+ %1 = load i64, ptr %arrayidx2, align 8
+ %add3 = add nsw i64 %1, %0
+ ret i64 %add3
+}
+
+define signext i32 @test_sh2add(ptr %p, iXLen %x, iXLen %y) {
+; RV32I-LABEL: test_sh2add:
+; RV32I: # %bb.0: # %entry
+; RV32I-NEXT: addi a0, a0, 1200
+; RV32I-NEXT: slli a1, a1, 2
+; RV32I-NEXT: slli a2, a2, 2
+; RV32I-NEXT: add a1, a0, a1
+; RV32I-NEXT: add a0, a2, a0
+; RV32I-NEXT: lw a1, 0(a1)
+; RV32I-NEXT: lw a0, 40(a0)
+; RV32I-NEXT: add a0, a0, a1
+; RV32I-NEXT: ret
+;
+; RV64I-LABEL: test_sh2add:
+; RV64I: # %bb.0: # %entry
+; RV64I-NEXT: addi a0, a0, 1200
+; RV64I-NEXT: slli a1, a1, 2
+; RV64I-NEXT: slli a2, a2, 2
+; RV64I-NEXT: add a1, a0, a1
+; RV64I-NEXT: add a0, a2, a0
+; RV64I-NEXT: lw a1, 0(a1)
+; RV64I-NEXT: lw a0, 40(a0)
+; RV64I-NEXT: addw a0, a0, a1
+; RV64I-NEXT: ret
+;
+; RV32ZBA-LABEL: test_sh2add:
+; RV32ZBA: # %bb.0: # %entry
+; RV32ZBA-NEXT: addi a0, a0, 1200
+; RV32ZBA-NEXT: sh2add a1, a1, a0
+; RV32ZBA-NEXT: sh2add a0, a2, a0
+; RV32ZBA-NEXT: lw a1, 0(a1)
+; RV32ZBA-NEXT: lw a0, 40(a0)
+; RV32ZBA-NEXT: add a0, a0, a1
+; RV32ZBA-NEXT: ret
+;
+; RV64ZBA-LABEL: test_sh2add:
+; RV64ZBA: # %bb.0: # %entry
+; RV64ZBA-NEXT: addi a0, a0, 1200
+; RV64ZBA-NEXT: sh2add a1, a1, a0
+; RV64ZBA-NEXT: sh2add a0, a2, a0
+; RV64ZBA-NEXT: lw a1, 0(a1)
+; RV64ZBA-NEXT: lw a0, 40(a0)
+; RV64ZBA-NEXT: addw a0, a0, a1
+; RV64ZBA-NEXT: ret
+entry:
+ %c = getelementptr inbounds nuw i8, ptr %p, i64 1200
+ %arrayidx = getelementptr inbounds nuw [100 x i32], ptr %c, i64 0, iXLen %x
+ %0 = load i32, ptr %arrayidx, align 4
+ %add = add iXLen %y, 10
+ %arrayidx2 = getelementptr inbounds nuw [100 x i32], ptr %c, i64 0, iXLen %add
+ %1 = load i32, ptr %arrayidx2, align 4
+ %add3 = add nsw i32 %1, %0
+ ret i32 %add3
+}
+
+define signext i16 @test_sh1add(ptr %p, iXLen %x, iXLen %y) {
+; RV32I-LABEL: test_sh1add:
+; RV32I: # %bb.0: # %entry
+; RV32I-NEXT: addi a0, a0, 1600
+; RV32I-NEXT: slli a1, a1, 1
+; RV32I-NEXT: slli a2, a2, 1
+; RV32I-NEXT: add a1, a0, a1
+; RV32I-NEXT: add a0, a2, a0
+; RV32I-NEXT: lh a1, 0(a1)
+; RV32I-NEXT: lh a0, 20(a0)
+; RV32I-NEXT: add a0, a0, a1
+; RV32I-NEXT: slli a0, a0, 16
+; RV32I-NEXT: srai a0, a0, 16
+; RV32I-NEXT: ret
+;
+; RV64I-LABEL: test_sh1add:
+; RV64I: # %bb.0: # %entry
+; RV64I-NEXT: addi a0, a0, 1600
+; RV64I-NEXT: slli a1, a1, 1
+; RV64I-NEXT: slli a2, a2, 1
+; RV64I-NEXT: add a1, a0, a1
+; RV64I-NEXT: add a0, a2, a0
+; RV64I-NEXT: lh a1, 0(a1)
+; RV64I-NEXT: lh a0, 20(a0)
+; RV64I-NEXT: add a0, a0, a1
+; RV64I-NEXT: slli a0, a0, 48
+; RV64I-NEXT: srai a0, a0, 48
+; RV64I-NEXT: ret
+;
+; RV32ZBA-LABEL: test_sh1add:
+; RV32ZBA: # %bb.0: # %entry
+; RV32ZBA-NEXT: addi a0, a0, 1600
+; RV32ZBA-NEXT: sh1add a1, a1, a0
+; RV32ZBA-NEXT: sh1add a0, a2, a0
+; RV32ZBA-NEXT: lh a1, 0(a1)
+; RV32ZBA-NEXT: lh a0, 20(a0)
+; RV32ZBA-NEXT: add a0, a0, a1
+; RV32ZBA-NEXT: slli a0, a0, 16
+; RV32ZBA-NEXT: srai a0, a0, 16
+; RV32ZBA-NEXT: ret
+;
+; RV64ZBA-LABEL: test_sh1add:
+; RV64ZBA: # %bb.0: # %entry
+; RV64ZBA-NEXT: addi a0, a0, 1600
+; RV64ZBA-NEXT: sh1add a1, a1, a0
+; RV64ZBA-NEXT: sh1add a0, a2, a0
+; RV64ZBA-NEXT: lh a1, 0(a1)
+; RV64ZBA-NEXT: lh a0, 20(a0)
+; RV64ZBA-NEXT: add a0, a0, a1
+; RV64ZBA-NEXT: slli a0, a0, 48
+; RV64ZBA-NEXT: srai a0, a0, 48
+; RV64ZBA-NEXT: ret
+entry:
+ %d = getelementptr inbounds nuw i8, ptr %p, i64 1600
+ %arrayidx = getelementptr inbounds nuw [100 x i16], ptr %d, i64 0, iXLen %x
+ %0 = load i16, ptr %arrayidx, align 2
+ %add = add iXLen %y, 10
+ %arrayidx2 = getelementptr inbounds nuw [100 x i16], ptr %d, i64 0, iXLen %add
+ %1 = load i16, ptr %arrayidx2, align 2
+ %add4 = add i16 %1, %0
+ ret i16 %add4
+}
+
+define zeroext i8 @test_add(ptr %p, iXLen %x, iXLen %y) {
+; CHECK-LABEL: test_add:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: addi a0, a0, 1800
+; CHECK-NEXT: add a1, a0, a1
+; CHECK-NEXT: add a0, a2, a0
+; CHECK-NEXT: lbu a1, 0(a1)
+; CHECK-NEXT: lbu a0, 10(a0)
+; CHECK-NEXT: add a0, a0, a1
+; CHECK-NEXT: andi a0, a0, 255
+; CHECK-NEXT: ret
+;
+; ZBA-LABEL: test_add:
+; ZBA: # %bb.0: # %entry
+; ZBA-NEXT: addi a0, a0, 1800
+; ZBA-NEXT: add a1, a0, a1
+; ZBA-NEXT: add a0, a2, a0
+; ZBA-NEXT: lbu a1, 0(a1)
+; ZBA-NEXT: lbu a0, 10(a0)
+; ZBA-NEXT: add a0, a0, a1
+; ZBA-NEXT: andi a0, a0, 255
+; ZBA-NEXT: ret
+entry:
+ %e = getelementptr inbounds nuw i8, ptr %p, i64 1800
+ %arrayidx = getelementptr inbounds nuw [1000 x i8], ptr %e, i64 0, iXLen %x
+ %0 = load i8, ptr %arrayidx, align 1
+ %add = add iXLen %y, 10
+ %arrayidx2 = getelementptr inbounds nuw [1000 x i8], ptr %e, i64 0, iXLen %add
+ %1 = load i8, ptr %arrayidx2, align 1
+ %add4 = add i8 %1, %0
+ ret i8 %add4
+}
+
+define i64 @test_sh3add_uw(ptr %p, i32 signext %x, i32 signext %y) {
+; RV32I-LABEL: test_sh3add_uw:
+; RV32I: # %bb.0: # %entry
+; RV32I-NEXT: addi a0, a0, 400
+; RV32I-NEXT: slli a1, a1, 3
+; RV32I-NEXT: slli a2, a2, 3
+; RV32I-NEXT: add a1, a0, a1
+; RV32I-NEXT: add a0, a0, a2
+; RV32I-NEXT: lw a2, 0(a1)
+; RV32I-NEXT: lw a1, 4(a1)
+; RV32I-NEXT: lw a3, 0(a0)
+; RV32I-NEXT: lw a0, 4(a0)
+; RV32I-NEXT: add a1, a0, a1
+; RV32I-NEXT: add a0, a3, a2
+; RV32I-NEXT: sltu a2, a0, a3
+; RV32I-NEXT: add a1, a1, a2
+; RV32I-NEXT: ret
+;
+; RV64I-LABEL: test_sh3add_uw:
+; RV64I: # %bb.0: # %entry
+; RV64I-NEXT: addi a0, a0, 400
+; RV64I-NEXT: slli a1, a1, 32
+; RV64I-NEXT: slli a2, a2, 32
+; RV64I-NEXT: srli a1, a1, 29
+; RV64I-NEXT: srli a2, a2, 29
+; RV64I-NEXT: add a1, a0, a1
+; RV64I-NEXT: add a0, a0, a2
+; RV64I-NEXT: ld a1, 0(a1)
+; RV64I-NEXT: ld a0, 0(a0)
+; RV64I-NEXT: add a0, a0, a1
+; RV64I-NEXT: ret
+;
+; RV32ZBA-LABEL: test_sh3add_uw:
+; RV32ZBA: # %bb.0: # %entry
+; RV32ZBA-NEXT: addi a0, a0, 400
+; RV32ZBA-NEXT: sh3add a1, a1, a0
+; RV32ZBA-NEXT: sh3add a0, a2, a0
+; RV32ZBA-NEXT: lw a2, 0(a1)
+; RV32ZBA-NEXT: lw a1, 4(a1)
+; RV32ZBA-NEXT: lw a3, 0(a0)
+; RV32ZBA-NEXT: lw a0, 4(a0)
+; RV32ZBA-NEXT: add a1, a0, a1
+; RV32ZBA-NEXT: add a0, a3, a2
+; RV32ZBA-NEXT: sltu a2, a0, a3
+; RV32ZBA-NEXT: add a1, a1, a2
+; RV32ZBA-NEXT: ret
+;
+; RV64ZBA-LABEL: test_sh3add_uw:
+; RV64ZBA: # %bb.0: # %entry
+; RV64ZBA-NEXT: addi a0, a0, 400
+; RV64ZBA-NEXT: sh3add.uw a1, a1, a0
+; RV64ZBA-NEXT: sh3add.uw a0, a2, a0
+; RV64ZBA-NEXT: ld a1, 0(a1)
+; RV64ZBA-NEXT: ld a0, 0(a0)
+; RV64ZBA-NEXT: add a0, a0, a1
+; RV64ZBA-NEXT: ret
+entry:
+ %b = getelementptr inbounds nuw i8, ptr %p, i64 400
+ %idxprom = zext i32 %x to i64
+ %arrayidx = getelementptr inbounds nuw [100 x i64], ptr %b, i64 0, i64 %idxprom
+ %0 = load i64, ptr %arrayidx, align 8
+ %idxprom2 = zext i32 %y to i64
+ %arrayidx3 = getelementptr inbounds nuw [100 x i64], ptr %b, i64 0, i64 %idxprom2
+ %1 = load i64, ptr %arrayidx3, align 8
+ %add4 = add nsw i64 %1, %0
+ ret i64 %add4
+}
+
+define signext i32 @test_sh2add_uw(ptr %p, i32 signext %x, i32 signext %y) {
+; RV32I-LABEL: test_sh2add_uw:
+; RV32I: # %bb.0: # %entry
+; RV32I-NEXT: addi a0, a0, 1200
+; RV32I-NEXT: slli a1, a1, 2
+; RV32I-NEXT: slli a2, a2, 2
+; RV32I-NEXT: add a1, a0, a1
+; RV32I-NEXT: add a0, a0, a2
+; RV32I-NEXT: lw a1, 0(a1)
+; RV32I-NEXT: lw a0, 0(a0)
+; RV32I-NEXT: add a0, a0, a1
+; RV32I-NEXT: ret
+;
+; RV64I-LABEL: test_sh2add_uw:
+; RV64I: # %bb.0: # %entry
+; RV64I-NEXT: addi a0, a0, 1200
+; RV64I-NEXT: slli a1, a1, 32
+; RV64I-NEXT: slli a2, a2, 32
+; RV64I-NEXT: srli a1, a1, 30
+; RV64I-NEXT: srli a2, a2, 30
+; RV64I-NEXT: add a1, a0, a1
+; RV64I-NEXT: add a0, a0, a2
+; RV64I-NEXT: lw a1, 0(a1)
+; RV64I-NEXT: lw a0, 0(a0)
+; RV64I-NEXT: addw a0, a0, a1
+; RV64I-NEXT: ret
+;
+; RV32ZBA-LABEL: test_sh2add_uw:
+; RV32ZBA: # %bb.0: # %entry
+; RV32ZBA-NEXT: addi a0, a0, 1200
+; RV32ZBA-NEXT: sh2add a1, a1, a0
+; RV32ZBA-NEXT: sh2add a0, a2, a0
+; RV32ZBA-NEXT: lw a1, 0(a1)
+; RV32ZBA-NEXT: lw a0, 0(a0)
+; RV32ZBA-NEXT: add a0, a0, a1
+; RV32ZBA-NEXT: ret
+;
+; RV64ZBA-LABEL: test_sh2add_uw:
+; RV64ZBA: # %bb.0: # %entry
+; RV64ZBA-NEXT: addi a0, a0, 1200
+; RV64ZBA-NEXT: sh2add.uw a1, a1, a0
+; RV64ZBA-NEXT: sh2add.uw a0, a2, a0
+; RV64ZBA-NEXT: lw a1, 0(a1)
+; RV64ZBA-NEXT: lw a0, 0(a0)
+; RV64ZBA-NEXT: addw a0, a0, a1
+; RV64ZBA-NEXT: ret
+entry:
+ %c = getelementptr inbounds nuw i8, ptr %p, i64 1200
+ %idxprom = zext i32 %x to i64
+ %arrayidx = getelementptr inbounds nuw [100 x i32], ptr %c, i64 0, i64 %idxprom
+ %0 = load i32, ptr %arrayidx, align 4
+ %idxprom2 = zext i32 %y to i64
+ %arrayidx3 = getelementptr inbounds nuw [100 x i32], ptr %c, i64 0, i64 %idxprom2
+ %1 = load i32, ptr %arrayidx3, align 4
+ %add4 = add nsw i32 %1, %0
+ ret i32 %add4
+}
+
+define signext i16 @test_sh1add_uw(ptr %p, i32 signext %x, i32 signext %y) {
+; RV32I-LABEL: test_sh1add_uw:
+; RV32I: # %bb.0: # %entry
+; RV32I-NEXT: addi a0, a0, 1600
+; RV32I-NEXT: slli a1, a1, 1
+; RV32I-NEXT: slli a2, a2, 1
+; RV32I-NEXT: add a1, a0, a1
+; RV32I-NEXT: add a0, a2, a0
+; RV32I-NEXT: lh a1, 0(a1)
+; RV32I-NEXT: lh a0, 20(a0)
+; RV32I-NEXT: add a0, a0, a1
+; RV32I-NEXT: slli a0, a0, 16
+; RV32I-NEXT: srai a0, a0, 16
+; RV32I-NEXT: ret
+;
+; RV64I-LABEL: test_sh1add_uw:
+; RV64I: # %bb.0: # %entry
+; RV64I-NEXT: addi a0, a0, 1600
+; RV64I-NEXT: slli a1, a1, 32
+; RV64I-NEXT: addi a2, a2, 10
+; RV64I-NEXT: srli a1, a1, 31
+; RV64I-NEXT: slli a2, a2, 32
+; RV64I-NEXT: add a1, a0, a1
+; RV64I-NEXT: srli a2, a2, 31
+; RV64I-NEXT: add a0, a0, a2
+; RV64I-NEXT: lh a1, 0(a1)
+; RV64I-NEXT: lh a0, 0(a0)
+; RV64I-NEXT: add a0, a0, a1
+; RV64I-NEXT: slli a0, a0, 48
+; RV64I-NEXT: srai a0, a0, 48
+; RV64I-NEXT: ret
+;
+; RV32ZBA-LABEL: test_sh1add_uw:
+; RV32ZBA: # %bb.0: # %entry
+; RV32ZBA-NEXT: addi a0, a0, 1600
+; RV32ZBA-NEXT: sh1add a1, a1, a0
+; RV32ZBA-NEXT: sh1add a0, a2, a0
+; RV32ZBA-NEXT: lh a1, 0(a1)
+; RV32ZBA-NEXT: lh a0, 20(a0)
+; RV32ZBA-NEXT: add a0, a0, a1
+; RV32ZBA-NEXT: slli a0, a0, 16
+; RV32ZBA-NEXT: srai a0, a0, 16
+; RV32ZBA-NEXT: ret
+;
+; RV64ZBA-LABEL: test_sh1add_uw:
+; RV64ZBA: # %bb.0: # %entry
+; RV64ZBA-NEXT: addi a0, a0, 1600
+; RV64ZBA-NEXT: addi a2, a2, 10
+; RV64ZBA-NEXT: sh1add.uw a1, a1, a0
+; RV64ZBA-NEXT: sh1add.uw a0, a2, a0
+; RV64ZBA-NEXT: lh a1, 0(a1)
+; RV64ZBA-NEXT: lh a0, 0(a0)
+; RV64ZBA-NEXT: add a0, a0, a1
+; RV64ZBA-NEXT: slli a0, a0, 48
+; RV64ZBA-NEXT: srai a0, a0, 48
+; RV64ZBA-NEXT: ret
+entry:
+ %d = getelementptr inbounds nuw i8, ptr %p, i64 1600
+ %idxprom = zext i32 %x to i64
+ %arrayidx = getelementptr inbounds nuw [100 x i16], ptr %d, i64 0, i64 %idxprom
+ %0 = load i16, ptr %arrayidx, align 2
+ %add = add i32 %y, 10
+ %idxprom2 = zext i32 %add to i64
+ %arrayidx3 = getelementptr inbounds nuw [100 x i16], ptr %d, i64 0, i64 %idxprom2
+ %1 = load i16, ptr %arrayidx3, align 2
+ %add5 = add i16 %1, %0
+ ret i16 %add5
+}
+
+define zeroext i8 @test_add_uw(ptr %p, i32 signext %x, i32 signext %y) {
+; RV32I-LABEL: test_add_uw:
+; RV32I: # %bb.0: # %entry
+; RV32I-NEXT: addi a0, a0, 1800
+; RV32I-NEXT: add a1, a0, a1
+; RV32I-NEXT: add a0, a0, a2
+; RV32I-NEXT: lbu a1, 0(a1)
+; RV32I-NEXT: lbu a0, 0(a0)
+; RV32I-NEXT: add a0, a0, a1
+; RV32I-NEXT: andi a0, a0, 255
+; RV32I-NEXT: ret
+;
+; RV64I-LABEL: test_add_uw:
+; RV64I: # %bb.0: # %entry
+; RV64I-NEXT: addi a0, a0, 1800
+; RV64I-NEXT: slli a1, a1, 32
+; RV64I-NEXT: slli a2, a2, 32
+; RV64I-NEXT: srli a1, a1, 32
+; RV64I-NEXT: srli a2, a2, 32
+; RV64I-NEXT: add a1, a0, a1
+; RV64I-NEXT: add a0, a0, a2
+; RV64I-NEXT: lbu a1, 0(a1)
+; RV64I-NEXT: lbu a0, 0(a0)
+; RV64I-NEXT: add a0, a0, a1
+; RV64I-NEXT: andi a0, a0, 255
+; RV64I-NEXT: ret
+;
+; RV32ZBA-LABEL: test_add_uw:
+; RV32ZBA: # %bb.0: # %entry
+; RV32ZBA-NEXT: addi a0, a0, 1800
+; RV32ZBA-NEXT: add a1, a0, a1
+; RV32ZBA-NEXT: add a0, a0, a2
+; RV32ZBA-NEXT: lbu a1, 0(a1)
+; RV32ZBA-NEXT: lbu a0, 0(a0)
+; RV32ZBA-NEXT: add a0, a0, a1
+; RV32ZBA-NEXT: andi a0, a0, 255
+; RV32ZBA-NEXT: ret
+;
+; RV64ZBA-LABEL: test_add_uw:
+; RV64ZBA: # %bb.0: # %entry
+; RV64ZBA-NEXT: addi a0, a0, 1800
+; RV64ZBA-NEXT: add.uw a1, a1, a0
+; RV64ZBA-NEXT: add.uw a0, a2, a0
+; RV64ZBA-NEXT: lbu a1, 0(a1)
+; RV64ZBA-NEXT: lbu a0, 0(a0)
+; RV64ZBA-NEXT: add a0, a0, a1
+; RV64ZBA-NEXT: andi a0, a0, 255
+; RV64ZBA-NEXT: ret
+entry:
+ %e = getelementptr inbounds nuw i8, ptr %p, i64 1800
+ %idxprom = zext i32 %x to i64
+ %arrayidx = getelementptr inbounds nuw [1000 x i8], ptr %e, i64 0, i64 %idxprom
+ %0 = load i8, ptr %arrayidx, align 1
+ %idxprom2 = zext i32 %y to i64
+ %arrayidx3 = getelementptr inbounds nuw [1000 x i8], ptr %e, i64 0, i64 %idxprom2
+ %1 = load i8, ptr %arrayidx3, align 1
+ %add5 = add i8 %1, %0
+ ret i8 %add5
+}
+
+; The addi is part of the index and used with 2 different scales.
+define signext i32 @test_scaled_index_addi(ptr %p, iXLen %x) {
+; RV32I-LABEL: test_scaled_index_addi:
+; RV32I: # %bb.0: # %entry
+; RV32I-NEXT: addi a1, a1, -1
+; RV32I-NEXT: slli a2, a1, 2
+; RV32I-NEXT: slli a1, a1, 1
+; RV32I-NEXT: add a2, a0, a2
+; RV32I-NEXT: add a0, a0, a1
+; RV32I-NEXT: lw a1, 1200(a2)
+; RV32I-NEXT: lh a0, 1600(a0)
+; RV32I-NEXT: add a0, a1, a0
+; RV32I-NEXT: ret
+;
+; RV64I-LABEL: test_scaled_index_addi:
+; RV64I: # %bb.0: # %entry
+; RV64I-NEXT: addi a1, a1, -1
+; RV64I-NEXT: slli a2, a1, 2
+; RV64I-NEXT: slli a1, a1, 1
+; RV64I-NEXT: add a2, a0, a2
+; RV64I-NEXT: add a0, a0, a1
+; RV64I-NEXT: lw a1, 1200(a2)
+; RV64I-NEXT: lh a0, 1600(a0)
+; RV64I-NEXT: addw a0, a1, a0
+; RV64I-NEXT: ret
+;
+; RV32ZBA-LABEL: test_scaled_index_addi:
+; RV32ZBA: # %bb.0: # %entry
+; RV32ZBA-NEXT: addi a1, a1, -1
+; RV32ZBA-NEXT: sh2add a2, a1, a0
+; RV32ZBA-NEXT: sh1add a0, a1, a0
+; RV32ZBA-NEXT: lw a1, 1200(a2)
+; RV32ZBA-NEXT: lh a0, 1600(a0)
+; RV32ZBA-NEXT: add a0, a1, a0
+; RV32ZBA-NEXT: ret
+;
+; RV64ZBA-LABEL: test_scaled_index_addi:
+; RV64ZBA: # %bb.0: # %entry
+; RV64ZBA-NEXT: addi a1, a1, -1
+; RV64ZBA-NEXT: sh2add a2, a1, a0
+; RV64ZBA-NEXT: sh1add a0, a1, a0
+; RV64ZBA-NEXT: lw a1, 1200(a2)
+; RV64ZBA-NEXT: lh a0, 1600(a0)
+; RV64ZBA-NEXT: addw a0, a1, a0
+; RV64ZBA-NEXT: ret
+entry:
+ %c = getelementptr inbounds nuw i8, ptr %p, i64 1200
+ %sub = add iXLen %x, -1
+ %arrayidx = getelementptr inbounds nuw [100 x i32], ptr %c, i64 0, iXLen %sub
+ %0 = load i32, ptr %arrayidx, align 4
+ %d = getelementptr inbounds nuw i8, ptr %p, i64 1600
+ %arrayidx2 = getelementptr inbounds nuw [100 x i16], ptr %d, i64 0, iXLen %sub
+ %1 = load i16, ptr %arrayidx2, align 2
+ %conv = sext i16 %1 to i32
+ %add = add nsw i32 %0, %conv
+ ret i32 %add
+}
+
+; Offset is a pair of addis. We can fold one of them.
+define signext i32 @test_medium_offset(ptr %p, iXLen %x, iXLen %y) {
+; RV32I-LABEL: test_medium_offset:
+; RV32I: # %bb.0: # %entry
+; RV32I-NEXT: addi a0, a0, 2047
+; RV32I-NEXT: slli a1, a1, 2
+; RV32I-NEXT: slli a2, a2, 2
+; RV32I-NEXT: addi a0, a0, 753
+; RV32I-NEXT: add a1, a0, a1
+; RV32I-NEXT: add a0, a2, a0
+; RV32I-NEXT: lw a1, 0(a1)
+; RV32I-NEXT: lw a0, 40(a0)
+; RV32I-NEXT: add a0, a0, a1
+; RV32I-NEXT: ret
+;
+; RV64I-LABEL: test_medium_offset:
+; RV64I: # %bb.0: # %entry
+; RV64I-NEXT: addi a0, a0, 2047
+; RV64I-NEXT: slli a1, a1, 2
+; RV64I-NEXT: slli a2, a2, 2
+; RV64I-NEXT: addi a0, a0, 753
+; RV64I-NEXT: add a1, a0, a1
+; RV64I-NEXT: add a0, a2, a0
+; RV64I-NEXT: lw a1, 0(a1)
+; RV64I-NEXT: lw a0, 40(a0)
+; RV64I-NEXT: addw a0, a0, a1
+; RV64I-NEXT: ret
+;
+; RV32ZBA-LABEL: test_medium_offset:
+; RV32ZBA: # %bb.0: # %entry
+; RV32ZBA-NEXT: addi a0, a0, 2047
+; RV32ZBA-NEXT: addi a0, a0, 753
+; RV32ZBA-NEXT: sh2add a1, a1, a0
+; RV32ZBA-NEXT: sh2add a0, a2, a0
+; RV32ZBA-NEXT: lw a1, 0(a1)
+; RV32ZBA-NEXT: lw a0, 40(a0)
+; RV32ZBA-NEXT: add a0, a0, a1
+; RV32ZBA-NEXT: ret
+;
+; RV64ZBA-LABEL: test_medium_offset:
+; RV64ZBA: # %bb.0: # %entry
+; RV64ZBA-NEXT: addi a0, a0, 2047
+; RV64ZBA-NEXT: addi a0, a0, 753
+; RV64ZBA-NEXT: sh2add a1, a1, a0
+; RV64ZBA-NEXT: sh2add a0, a2, a0
+; RV64ZBA-NEXT: lw a1, 0(a1)
+; RV64ZBA-NEXT: lw a0, 40(a0)
+; RV64ZBA-NEXT: addw a0, a0, a1
+; RV64ZBA-NEXT: ret
+entry:
+ %f = getelementptr inbounds nuw i8, ptr %p, i64 2800
+ %arrayidx = getelementptr inbounds nuw [1000 x i32], ptr %f, i64 0, iXLen %x
+ %0 = load i32, ptr %arrayidx, align 4
+ %add = add iXLen %y, 10
+ %arrayidx2 = getelementptr inbounds nuw [1000 x i32], ptr %f, i64 0, iXLen %add
+ %1 = load i32, ptr %arrayidx2, align 4
+ %add3 = add nsw i32 %1, %0
+ ret i32 %add3
+}
+
+; Offset is a lui+addiw. We can't fold this on RV64.
+define signext i32 @test_large_offset(ptr %p, iXLen %x, iXLen %y) {
+; RV32I-LABEL: test_large_offset:
+; RV32I: # %bb.0: # %entry
+; RV32I-NEXT: lui a3, 2
+; RV32I-NEXT: slli a1, a1, 2
+; RV32I-NEXT: slli a2, a2, 2
+; RV32I-NEXT: addi a3, a3, -1392
+; RV32I-NEXT: add a0, a0, a3
+; RV32I-NEXT: add a1, a0, a1
+; RV32I-NEXT: add a0, a2, a0
+; RV32I-NEXT: lw a1, 0(a1)
+; RV32I-NEXT: lw a0, 40(a0)
+; RV32I-NEXT: add a0, a0, a1
+; RV32I-NEXT: ret
+;
+; RV64I-LABEL: test_large_offset:
+; RV64I: # %bb.0: # %entry
+; RV64I-NEXT: lui a3, 2
+; RV64I-NEXT: slli a1, a1, 2
+; RV64I-NEXT: slli a2, a2, 2
+; RV64I-NEXT: addiw a3, a3, -1392
+; RV64I-NEXT: add a0, a0, a3
+; RV64I-NEXT: add a1, a0, a1
+; RV64I-NEXT: add a0, a2, a0
+; RV64I-NEXT: lw a1, 0(a1)
+; RV64I-NEXT: lw a0, 40(a0)
+; RV64I-NEXT: addw a0, a0, a1
+; RV64I-NEXT: ret
+;
+; RV32ZBA-LABEL: test_large_offset:
+; RV32ZBA: # %bb.0: # %entry
+; RV32ZBA-NEXT: li a3, 1700
+; RV32ZBA-NEXT: sh2add a0, a3, a0
+; RV32ZBA-NEXT: sh2add a1, a1, a0
+; RV32ZBA-NEXT: sh2add a0, a2, a0
+; RV32ZBA-NEXT: lw a1, 0(a1)
+; RV32ZBA-NEXT: lw a0, 40(a0)
+; RV32ZBA-NEXT: add a0, a0, a1
+; RV32ZBA-NEXT: ret
+;
+; RV64ZBA-LABEL: test_large_offset:
+; RV64ZBA: # %bb.0: # %entry
+; RV64ZBA-NEXT: li a3, 1700
+; RV64ZBA-NEXT: sh2add a0, a3, a0
+; RV64ZBA-NEXT: sh2add a1, a1, a0
+; RV64ZBA-NEXT: sh2add a0, a2, a0
+; RV64ZBA-NEXT: lw a1, 0(a1)
+; RV64ZBA-NEXT: lw a0, 40(a0)
+; RV64ZBA-NEXT: addw a0, a0, a1
+; RV64ZBA-NEXT: ret
+entry:
+ %g = getelementptr inbounds nuw i8, ptr %p, i64 6800
+ %arrayidx = getelementptr inbounds nuw [200 x i32], ptr %g, i64 0, iXLen %x
+ %0 = load i32, ptr %arrayidx, align 4
+ %add = add iXLen %y, 10
+ %arrayidx2 = getelementptr inbounds nuw [200 x i32], ptr %g, i64 0, iXLen %add
+ %1 = load i32, ptr %arrayidx2, align 4
+ %add3 = add nsw i32 %1, %0
+ ret i32 %add3
+}
+
+; After folding we can CSE the sh2add
+define signext i32 @test_cse(ptr %p, iXLen %x) {
+; RV32I-LABEL: test_cse:
+; RV32I: # %bb.0: # %entry
+; RV32I-NEXT: slli a1, a1, 2
+; RV32I-NEXT: add a0, a0, a1
+; RV32I-NEXT: lw a1, 1200(a0)
+; RV32I-NEXT: addi a0, a0, 2047
+; RV32I-NEXT: lw a0, 753(a0)
+; RV32I-NEXT: add a0, a0, a1
+; RV32I-NEXT: ret
+;
+; RV64I-LABEL: test_cse:
+; RV64I: # %bb.0: # %entry
+; RV64I-NEXT: slli a1, a1, 2
+; RV64I-NEXT: add a0, a0, a1
+; RV64I-NEXT: lw a1, 1200(a0)
+; RV64I-NEXT: addi a0, a0, 2047
+; RV64I-NEXT: lw a0, 753(a0)
+; RV64I-NEXT: addw a0, a0, a1
+; RV64I-NEXT: ret
+;
+; RV32ZBA-LABEL: test_cse:
+; RV32ZBA: # %bb.0: # %entry
+; RV32ZBA-NEXT: sh2add a0, a1, a0
+; RV32ZBA-NEXT: lw a1, 1200(a0)
+; RV32ZBA-NEXT: addi a0, a0, 2047
+; RV32ZBA-NEXT: lw a0, 753(a0)
+; RV32ZBA-NEXT: add a0, a0, a1
+; RV32ZBA-NEXT: ret
+;
+; RV64ZBA-LABEL: test_cse:
+; RV64ZBA: # %bb.0: # %entry
+; RV64ZBA-NEXT: sh2add a0, a1, a0
+; RV64ZBA-NEXT: lw a1, 1200(a0)
+; RV64ZBA-NEXT: addi a0, a0, 2047
+; RV64ZBA-NEXT: lw a0, 753(a0)
+; RV64ZBA-NEXT: addw a0, a0, a1
+; RV64ZBA-NEXT: ret
+entry:
+ %c = getelementptr inbounds nuw i8, ptr %p, i64 1200
+ %arrayidx = getelementptr inbounds nuw [100 x i32], ptr %c, i64 0, iXLen %x
+ %0 = load i32, ptr %arrayidx, align 4
+ %f = getelementptr inbounds nuw i8, ptr %p, i64 2800
+ %arrayidx1 = getelementptr inbounds nuw [1000 x i32], ptr %f, i64 0, iXLen %x
+ %1 = load i32, ptr %arrayidx1, align 4
+ %add = add nsw i32 %1, %0
+ ret i32 %add
+}
+
+define zeroext i8 @test_optsize(ptr %p, iXLen %x, iXLen %y) optsize {
+; CHECK-LABEL: test_optsize:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: addi a0, a0, 1800
+; CHECK-NEXT: add a1, a0, a1
+; CHECK-NEXT: add a0, a2, a0
+; CHECK-NEXT: lbu a1, 0(a1)
+; CHECK-NEXT: lbu a0, 10(a0)
+; CHECK-NEXT: add a0, a0, a1
+; CHECK-NEXT: andi a0, a0, 255
+; CHECK-NEXT: ret
+;
+; ZBA-LABEL: test_optsize:
+; ZBA: # %bb.0: # %entry
+; ZBA-NEXT: addi a0, a0, 1800
+; ZBA-NEXT: add a1, a0, a1
+; ZBA-NEXT: add a0, a2, a0
+; ZBA-NEXT: lbu a1, 0(a1)
+; ZBA-NEXT: lbu a0, 10(a0)
+; ZBA-NEXT: add a0, a0, a1
+; ZBA-NEXT: andi a0, a0, 255
+; ZBA-NEXT: ret
+entry:
+ %e = getelementptr inbounds nuw i8, ptr %p, i64 1800
+ %arrayidx = getelementptr inbounds nuw [1000 x i8], ptr %e, i64 0, iXLen %x
+ %0 = load i8, ptr %arrayidx, align 1
+ %add = add iXLen %y, 10
+ %arrayidx2 = getelementptr inbounds nuw [1000 x i8], ptr %e, i64 0, iXLen %add
+ %1 = load i8, ptr %arrayidx2, align 1
+ %add4 = add i8 %1, %0
+ ret i8 %add4
+}
+
+define zeroext i8 @test_minsize(ptr %p, iXLen %x, iXLen %y) minsize {
+; CHECK-LABEL: test_minsize:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: addi a0, a0, 1800
+; CHECK-NEXT: add a1, a0, a1
+; CHECK-NEXT: add a0, a2, a0
+; CHECK-NEXT: lbu a1, 0(a1)
+; CHECK-NEXT: lbu a0, 10(a0)
+; CHECK-NEXT: add a0, a0, a1
+; CHECK-NEXT: andi a0, a0, 255
+; CHECK-NEXT: ret
+;
+; ZBA-LABEL: test_minsize:
+; ZBA: # %bb.0: # %entry
+; ZBA-NEXT: addi a0, a0, 1800
+; ZBA-NEXT: add a1, a0, a1
+; ZBA-NEXT: add a0, a2, a0
+; ZBA-NEXT: lbu a1, 0(a1)
+; ZBA-NEXT: lbu a0, 10(a0)
+; ZBA-NEXT: add a0, a0, a1
+; ZBA-NEXT: andi a0, a0, 255
+; ZBA-NEXT: ret
+entry:
+ %e = getelementptr inbounds nuw i8, ptr %p, i64 1800
+ %arrayidx = getelementptr inbounds nuw [1000 x i8], ptr %e, i64 0, iXLen %x
+ %0 = load i8, ptr %arrayidx, align 1
+ %add = add iXLen %y, 10
+ %arrayidx2 = getelementptr inbounds nuw [1000 x i8], ptr %e, i64 0, iXLen %add
+ %1 = load i8, ptr %arrayidx2, align 1
+ %add4 = add i8 %1, %0
+ ret i8 %add4
+}
>From 294a67864ca1a3627de6bf49397ce6782556997f Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Wed, 5 Feb 2025 12:40:51 -0800
Subject: [PATCH 2/6] [RISCV] Add a pass to remove ADDI by reassociating to
fold into load/store address.
SelectionDAG will not reassociate adds to the end of a chain if
there are multiple users of later additions. This prevents isel
from folding the immediate into a load/store address.
One easy way to see this is accessing an array in a struct with
two different indices. An ADDI will be used to get to the start
of the array then 2 different SHXADD instructions will be used to
add the scaled indices. Finally the SHXADD will be used by different
load instructions. We can remove the ADDI by folding the offset into
each load.
This patch adds a new pass that analyzes how an ADDI constant
propagates through address arithmetic. If the arithmetic is only
used by a load/store and the offset is small enough, we can adjust
the load/store offset and remove the ADDI. For simplicit, we replace
the ADDI with a COPY from its input register which can be cleaned up
by other passes.
This pass is placed before MachineCSE to allow cleanups if some
instructions become common after removing offsets from their inputs.
RISCVMergeBaseOffset is modified to prevent a regression by handling
an ADDI becoming a COPY from X0.
---
llvm/lib/Target/RISCV/CMakeLists.txt | 1 +
llvm/lib/Target/RISCV/RISCV.h | 3 +
llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp | 289 ++++++++++++++++++
.../lib/Target/RISCV/RISCVMergeBaseOffset.cpp | 7 +
llvm/lib/Target/RISCV/RISCVTargetMachine.cpp | 2 +
llvm/test/CodeGen/RISCV/O3-pipeline.ll | 1 +
.../test/CodeGen/RISCV/fold-addi-loadstore.ll | 9 +-
llvm/test/CodeGen/RISCV/fold-mem-offset.ll | 213 ++++++-------
llvm/test/CodeGen/RISCV/split-offsets.ll | 23 +-
llvm/test/CodeGen/RISCV/xtheadmemidx.ll | 5 +-
10 files changed, 406 insertions(+), 147 deletions(-)
create mode 100644 llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp
diff --git a/llvm/lib/Target/RISCV/CMakeLists.txt b/llvm/lib/Target/RISCV/CMakeLists.txt
index 9b23a5ab521c8..5d1ea50eba494 100644
--- a/llvm/lib/Target/RISCV/CMakeLists.txt
+++ b/llvm/lib/Target/RISCV/CMakeLists.txt
@@ -37,6 +37,7 @@ add_llvm_target(RISCVCodeGen
RISCVMakeCompressible.cpp
RISCVExpandAtomicPseudoInsts.cpp
RISCVExpandPseudoInsts.cpp
+ RISCVFoldMemOffset.cpp
RISCVFrameLowering.cpp
RISCVGatherScatterLowering.cpp
RISCVIndirectBranchTracking.cpp
diff --git a/llvm/lib/Target/RISCV/RISCV.h b/llvm/lib/Target/RISCV/RISCV.h
index 851eea1352852..641e2eb4094f9 100644
--- a/llvm/lib/Target/RISCV/RISCV.h
+++ b/llvm/lib/Target/RISCV/RISCV.h
@@ -52,6 +52,9 @@ void initializeRISCVVectorPeepholePass(PassRegistry &);
FunctionPass *createRISCVOptWInstrsPass();
void initializeRISCVOptWInstrsPass(PassRegistry &);
+FunctionPass *createRISCVFoldMemOffsetPass();
+void initializeRISCVFoldMemOffsetPass(PassRegistry &);
+
FunctionPass *createRISCVMergeBaseOffsetOptPass();
void initializeRISCVMergeBaseOffsetOptPass(PassRegistry &);
diff --git a/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp b/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp
new file mode 100644
index 0000000000000..b61eda499fcd0
--- /dev/null
+++ b/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp
@@ -0,0 +1,289 @@
+//===- RISCVFoldMemOffset.cpp - Fold ADDI into memory offsets ------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===---------------------------------------------------------------------===//
+//
+// Look for ADDIs that can be removed by folding their immediate into later
+// load/store addresses. There may be other arithmetic instructions between the
+// addi and load/store that we need to reassociate through. If the final result
+// of the arithmetic is only used by load/store addresses, we can fold the
+// offset into the all the load/store as long as it doesn't create an offset
+// that is too large.
+//
+//===---------------------------------------------------------------------===//
+
+#include "RISCV.h"
+#include "RISCVSubtarget.h"
+#include "llvm/CodeGen/MachineFunctionPass.h"
+#include <queue>
+
+using namespace llvm;
+
+#define DEBUG_TYPE "riscv-fold-mem-offset"
+#define RISCV_FOLD_MEM_OFFSET_NAME "RISC-V Fold Memory Offset"
+
+namespace {
+
+class RISCVFoldMemOffset : public MachineFunctionPass {
+public:
+ static char ID;
+
+ RISCVFoldMemOffset() : MachineFunctionPass(ID) {}
+
+ bool runOnMachineFunction(MachineFunction &MF) override;
+
+ bool foldOffset(Register OrigReg, int64_t InitialOffset,
+ const MachineRegisterInfo &MRI,
+ DenseMap<MachineInstr *, int64_t> &FoldableInstrs);
+
+ void getAnalysisUsage(AnalysisUsage &AU) const override {
+ AU.setPreservesCFG();
+ MachineFunctionPass::getAnalysisUsage(AU);
+ }
+
+ StringRef getPassName() const override { return RISCV_FOLD_MEM_OFFSET_NAME; }
+};
+
+// Wrapper class around a std::optional to allow accumulation.
+class FoldableOffset {
+ std::optional<int64_t> Offset;
+
+public:
+ bool hasValue() const { return Offset.has_value(); }
+ int64_t getValue() const { return *Offset; }
+
+ FoldableOffset &operator=(int64_t RHS) {
+ Offset = RHS;
+ return *this;
+ }
+
+ FoldableOffset &operator+=(int64_t RHS) {
+ if (!Offset)
+ Offset = RHS;
+ else
+ Offset = (uint64_t)*Offset + (uint64_t)RHS;
+ return *this;
+ }
+
+ FoldableOffset &operator-=(int64_t RHS) {
+ if (!Offset)
+ Offset = -(uint64_t)RHS;
+ else
+ Offset = (uint64_t)*Offset - (uint64_t)RHS;
+ return *this;
+ }
+
+ int64_t operator*() { return *Offset; }
+};
+
+} // end anonymous namespace
+
+char RISCVFoldMemOffset::ID = 0;
+INITIALIZE_PASS(RISCVFoldMemOffset, DEBUG_TYPE, RISCV_FOLD_MEM_OFFSET_NAME,
+ false, false)
+
+FunctionPass *llvm::createRISCVFoldMemOffsetPass() {
+ return new RISCVFoldMemOffset();
+}
+
+// Walk forward from the ADDI looking for arithmetic instructions we can
+// analyze or memory instructions that use it as part of their address
+// calculation. For each arithmetic instruction we lookup how the offset
+// contributes to the value in that register use that information to
+// calculate the contribution to the output of this instruction.
+// Only addition and left shift are supported.
+// FIXME: Add multiplication by constant. The constant will be in a register.
+bool RISCVFoldMemOffset::foldOffset(
+ Register OrigReg, int64_t InitialOffset, const MachineRegisterInfo &MRI,
+ DenseMap<MachineInstr *, int64_t> &FoldableInstrs) {
+ // Map to hold how much the offset contributes to the value of this register.
+ DenseMap<Register, int64_t> RegToOffsetMap;
+
+ // Insert root offset into the map.
+ RegToOffsetMap[OrigReg] = InitialOffset;
+
+ std::queue<Register> Worklist;
+ Worklist.push(OrigReg);
+
+ while (!Worklist.empty()) {
+ Register Reg = Worklist.front();
+ Worklist.pop();
+
+ for (auto &User : MRI.use_nodbg_instructions(Reg)) {
+ FoldableOffset Offset;
+
+ switch (User.getOpcode()) {
+ default:
+ return false;
+ case RISCV::ADD:
+ if (auto I = RegToOffsetMap.find(User.getOperand(1).getReg());
+ I != RegToOffsetMap.end())
+ Offset = I->second;
+ if (auto I = RegToOffsetMap.find(User.getOperand(2).getReg());
+ I != RegToOffsetMap.end())
+ Offset += I->second;
+ break;
+ case RISCV::SH1ADD:
+ if (auto I = RegToOffsetMap.find(User.getOperand(1).getReg());
+ I != RegToOffsetMap.end())
+ Offset = (uint64_t)I->second << 1;
+ if (auto I = RegToOffsetMap.find(User.getOperand(2).getReg());
+ I != RegToOffsetMap.end())
+ Offset += I->second;
+ break;
+ case RISCV::SH2ADD:
+ if (auto I = RegToOffsetMap.find(User.getOperand(1).getReg());
+ I != RegToOffsetMap.end())
+ Offset = (uint64_t)I->second << 2;
+ if (auto I = RegToOffsetMap.find(User.getOperand(2).getReg());
+ I != RegToOffsetMap.end())
+ Offset += I->second;
+ break;
+ case RISCV::SH3ADD:
+ if (auto I = RegToOffsetMap.find(User.getOperand(1).getReg());
+ I != RegToOffsetMap.end())
+ Offset = (uint64_t)I->second << 3;
+ if (auto I = RegToOffsetMap.find(User.getOperand(2).getReg());
+ I != RegToOffsetMap.end())
+ Offset += I->second;
+ break;
+ case RISCV::ADD_UW:
+ case RISCV::SH1ADD_UW:
+ case RISCV::SH2ADD_UW:
+ case RISCV::SH3ADD_UW:
+ // Don't fold through the zero extended input.
+ if (User.getOperand(1).getReg() == Reg)
+ return false;
+ if (auto I = RegToOffsetMap.find(User.getOperand(2).getReg());
+ I != RegToOffsetMap.end())
+ Offset = I->second;
+ break;
+ case RISCV::SLLI: {
+ unsigned ShAmt = User.getOperand(2).getImm();
+ if (auto I = RegToOffsetMap.find(User.getOperand(1).getReg());
+ I != RegToOffsetMap.end())
+ Offset = (uint64_t)I->second << ShAmt;
+ break;
+ }
+ case RISCV::LB:
+ case RISCV::LBU:
+ case RISCV::SB:
+ case RISCV::LH:
+ case RISCV::LH_INX:
+ case RISCV::LHU:
+ case RISCV::FLH:
+ case RISCV::SH:
+ case RISCV::SH_INX:
+ case RISCV::FSH:
+ case RISCV::LW:
+ case RISCV::LW_INX:
+ case RISCV::LWU:
+ case RISCV::FLW:
+ case RISCV::SW:
+ case RISCV::SW_INX:
+ case RISCV::FSW:
+ case RISCV::LD:
+ case RISCV::FLD:
+ case RISCV::SD:
+ case RISCV::FSD: {
+ // Can't fold into store value.
+ if (User.getOperand(0).getReg() == Reg)
+ return false;
+
+ // Existing offset must be immediate.
+ if (!User.getOperand(2).isImm())
+ return false;
+
+ // Require at least one operation between the ADDI and the load/store.
+ // We have other optimizations that should handle the simple case.
+ if (User.getOperand(1).getReg() == OrigReg)
+ return false;
+
+ auto I = RegToOffsetMap.find(User.getOperand(1).getReg());
+ if (I == RegToOffsetMap.end())
+ return false;
+
+ int64_t LocalOffset = User.getOperand(2).getImm();
+ assert(isInt<12>(LocalOffset));
+ int64_t CombinedOffset = (uint64_t)LocalOffset + (uint64_t)I->second;
+ if (!isInt<12>(CombinedOffset))
+ return false;
+
+ FoldableInstrs[&User] = CombinedOffset;
+ continue;
+ }
+ }
+
+ // If we reach here we should have an accumulated offset.
+ assert(Offset.hasValue() && "Expected an offset");
+
+ // If the offset is new or changed, add the destination register to the
+ // work list.
+ int64_t OffsetVal = Offset.getValue();
+ auto P = RegToOffsetMap.try_emplace(User.getOperand(0).getReg(),
+ OffsetVal);
+ if (P.second) {
+ Worklist.push(User.getOperand(0).getReg());
+ } else if (P.first->second != OffsetVal) {
+ P.first->second = OffsetVal;
+ Worklist.push(User.getOperand(0).getReg());
+ }
+ }
+ }
+
+ return true;
+}
+
+bool RISCVFoldMemOffset::runOnMachineFunction(MachineFunction &MF) {
+ if (skipFunction(MF.getFunction()))
+ return false;
+
+ // This optimization may increase size by preventing compression.
+ if (MF.getFunction().hasOptSize())
+ return false;
+
+ const MachineRegisterInfo &MRI = MF.getRegInfo();
+ const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>();
+ const RISCVInstrInfo &TII = *ST.getInstrInfo();
+
+ bool MadeChange = false;
+ for (MachineBasicBlock &MBB : MF) {
+ for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
+ // FIXME: We can support ADDIW from an LUI+ADDIW pair if the result is
+ // equivalent to LUI+ADDI.
+ if (MI.getOpcode() != RISCV::ADDI)
+ continue;
+
+ // We only want to optimize register ADDIs.
+ if (!MI.getOperand(1).isReg() || !MI.getOperand(2).isImm())
+ continue;
+
+ int64_t Offset = MI.getOperand(2).getImm();
+ assert(isInt<12>(Offset));
+
+ DenseMap<MachineInstr *, int64_t> FoldableInstrs;
+
+ if (!foldOffset(MI.getOperand(0).getReg(), Offset, MRI, FoldableInstrs))
+ continue;
+
+ if (FoldableInstrs.empty())
+ continue;
+
+ // We can fold this ADDI.
+ // Rewrite all the instructions.
+ for (auto [MemMI, NewOffset] : FoldableInstrs)
+ MemMI->getOperand(2).setImm(NewOffset);
+
+ // Replace ADDI with a copy.
+ BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(RISCV::COPY))
+ .add(MI.getOperand(0))
+ .add(MI.getOperand(1));
+ MI.eraseFromParent();
+ }
+ }
+
+ return MadeChange;
+}
diff --git a/llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp b/llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp
index bbbb1e1595982..3dab7d9bb0912 100644
--- a/llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp
+++ b/llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp
@@ -238,6 +238,13 @@ bool RISCVMergeBaseOffsetOpt::foldLargeOffset(MachineInstr &Hi,
foldOffset(Hi, Lo, TailAdd, Offset);
OffsetTail.eraseFromParent();
return true;
+ } else if (OffsetTail.getOpcode() == RISCV::COPY &&
+ OffsetTail.getOperand(1).getReg() == RISCV::X0) {
+ // Fold mem offset can leave copies from X0 in place of an ADDI and they
+ // might not have been eliminated yet.
+ foldOffset(Hi, Lo, TailAdd, 0);
+ OffsetTail.eraseFromParent();
+ return true;
}
return false;
}
diff --git a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
index 167dbb53c5950..89e017807363b 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
@@ -133,6 +133,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTarget() {
initializeRISCVPostRAExpandPseudoPass(*PR);
initializeRISCVMergeBaseOffsetOptPass(*PR);
initializeRISCVOptWInstrsPass(*PR);
+ initializeRISCVFoldMemOffsetPass(*PR);
initializeRISCVPreRAExpandPseudoPass(*PR);
initializeRISCVExpandPseudoPass(*PR);
initializeRISCVVectorPeepholePass(*PR);
@@ -590,6 +591,7 @@ void RISCVPassConfig::addMachineSSAOptimization() {
addPass(createRISCVVectorPeepholePass());
// TODO: Move this to pre regalloc
addPass(createRISCVVMV0EliminationPass());
+ addPass(createRISCVFoldMemOffsetPass());
TargetPassConfig::addMachineSSAOptimization();
diff --git a/llvm/test/CodeGen/RISCV/O3-pipeline.ll b/llvm/test/CodeGen/RISCV/O3-pipeline.ll
index 2646dfeca4eb6..194223eee69eb 100644
--- a/llvm/test/CodeGen/RISCV/O3-pipeline.ll
+++ b/llvm/test/CodeGen/RISCV/O3-pipeline.ll
@@ -98,6 +98,7 @@
; CHECK-NEXT: Finalize ISel and expand pseudo-instructions
; CHECK-NEXT: RISC-V Vector Peephole Optimization
; CHECK-NEXT: RISC-V VMV0 Elimination
+; CHECK-NEXT: RISC-V Fold Memory Offset
; CHECK-NEXT: Lazy Machine Block Frequency Analysis
; CHECK-NEXT: Early Tail Duplication
; CHECK-NEXT: Optimize machine instruction PHIs
diff --git a/llvm/test/CodeGen/RISCV/fold-addi-loadstore.ll b/llvm/test/CodeGen/RISCV/fold-addi-loadstore.ll
index 59ba3652c89e9..80ee5776f76f1 100644
--- a/llvm/test/CodeGen/RISCV/fold-addi-loadstore.ll
+++ b/llvm/test/CodeGen/RISCV/fold-addi-loadstore.ll
@@ -1205,12 +1205,11 @@ define i32 @crash() {
;
; RV64I-LARGE-LABEL: crash:
; RV64I-LARGE: # %bb.0: # %entry
-; RV64I-LARGE-NEXT: li a0, 1
; RV64I-LARGE-NEXT: .Lpcrel_hi15:
-; RV64I-LARGE-NEXT: auipc a1, %pcrel_hi(.LCPI21_0)
-; RV64I-LARGE-NEXT: ld a1, %pcrel_lo(.Lpcrel_hi15)(a1)
-; RV64I-LARGE-NEXT: add a0, a1, a0
-; RV64I-LARGE-NEXT: lbu a0, 400(a0)
+; RV64I-LARGE-NEXT: auipc a0, %pcrel_hi(.LCPI21_0)
+; RV64I-LARGE-NEXT: ld a0, %pcrel_lo(.Lpcrel_hi15)(a0)
+; RV64I-LARGE-NEXT: add a0, a0, zero
+; RV64I-LARGE-NEXT: lbu a0, 401(a0)
; RV64I-LARGE-NEXT: seqz a0, a0
; RV64I-LARGE-NEXT: sw a0, 0(zero)
; RV64I-LARGE-NEXT: li a0, 0
diff --git a/llvm/test/CodeGen/RISCV/fold-mem-offset.ll b/llvm/test/CodeGen/RISCV/fold-mem-offset.ll
index 25d7faebe1615..b12fa509b0bea 100644
--- a/llvm/test/CodeGen/RISCV/fold-mem-offset.ll
+++ b/llvm/test/CodeGen/RISCV/fold-mem-offset.ll
@@ -7,15 +7,14 @@
define i64 @test_sh3add(ptr %p, iXLen %x, iXLen %y) {
; RV32I-LABEL: test_sh3add:
; RV32I: # %bb.0: # %entry
-; RV32I-NEXT: addi a0, a0, 400
; RV32I-NEXT: slli a1, a1, 3
; RV32I-NEXT: slli a2, a2, 3
; RV32I-NEXT: add a1, a1, a0
; RV32I-NEXT: add a0, a0, a2
-; RV32I-NEXT: lw a2, 80(a1)
-; RV32I-NEXT: lw a1, 84(a1)
-; RV32I-NEXT: lw a3, 0(a0)
-; RV32I-NEXT: lw a0, 4(a0)
+; RV32I-NEXT: lw a2, 480(a1)
+; RV32I-NEXT: lw a1, 484(a1)
+; RV32I-NEXT: lw a3, 400(a0)
+; RV32I-NEXT: lw a0, 404(a0)
; RV32I-NEXT: add a1, a0, a1
; RV32I-NEXT: add a0, a3, a2
; RV32I-NEXT: sltu a2, a0, a3
@@ -24,25 +23,23 @@ define i64 @test_sh3add(ptr %p, iXLen %x, iXLen %y) {
;
; RV64I-LABEL: test_sh3add:
; RV64I: # %bb.0: # %entry
-; RV64I-NEXT: addi a0, a0, 400
; RV64I-NEXT: slli a1, a1, 3
; RV64I-NEXT: slli a2, a2, 3
; RV64I-NEXT: add a1, a1, a0
; RV64I-NEXT: add a0, a0, a2
-; RV64I-NEXT: ld a1, 80(a1)
-; RV64I-NEXT: ld a0, 0(a0)
+; RV64I-NEXT: ld a1, 480(a1)
+; RV64I-NEXT: ld a0, 400(a0)
; RV64I-NEXT: add a0, a0, a1
; RV64I-NEXT: ret
;
; RV32ZBA-LABEL: test_sh3add:
; RV32ZBA: # %bb.0: # %entry
-; RV32ZBA-NEXT: addi a0, a0, 400
; RV32ZBA-NEXT: sh3add a1, a1, a0
; RV32ZBA-NEXT: sh3add a0, a2, a0
-; RV32ZBA-NEXT: lw a2, 80(a1)
-; RV32ZBA-NEXT: lw a1, 84(a1)
-; RV32ZBA-NEXT: lw a3, 0(a0)
-; RV32ZBA-NEXT: lw a0, 4(a0)
+; RV32ZBA-NEXT: lw a2, 480(a1)
+; RV32ZBA-NEXT: lw a1, 484(a1)
+; RV32ZBA-NEXT: lw a3, 400(a0)
+; RV32ZBA-NEXT: lw a0, 404(a0)
; RV32ZBA-NEXT: add a1, a0, a1
; RV32ZBA-NEXT: add a0, a3, a2
; RV32ZBA-NEXT: sltu a2, a0, a3
@@ -51,11 +48,10 @@ define i64 @test_sh3add(ptr %p, iXLen %x, iXLen %y) {
;
; RV64ZBA-LABEL: test_sh3add:
; RV64ZBA: # %bb.0: # %entry
-; RV64ZBA-NEXT: addi a0, a0, 400
; RV64ZBA-NEXT: sh3add a1, a1, a0
; RV64ZBA-NEXT: sh3add a0, a2, a0
-; RV64ZBA-NEXT: ld a1, 80(a1)
-; RV64ZBA-NEXT: ld a0, 0(a0)
+; RV64ZBA-NEXT: ld a1, 480(a1)
+; RV64ZBA-NEXT: ld a0, 400(a0)
; RV64ZBA-NEXT: add a0, a0, a1
; RV64ZBA-NEXT: ret
entry:
@@ -72,45 +68,41 @@ entry:
define signext i32 @test_sh2add(ptr %p, iXLen %x, iXLen %y) {
; RV32I-LABEL: test_sh2add:
; RV32I: # %bb.0: # %entry
-; RV32I-NEXT: addi a0, a0, 1200
; RV32I-NEXT: slli a1, a1, 2
; RV32I-NEXT: slli a2, a2, 2
; RV32I-NEXT: add a1, a0, a1
; RV32I-NEXT: add a0, a2, a0
-; RV32I-NEXT: lw a1, 0(a1)
-; RV32I-NEXT: lw a0, 40(a0)
+; RV32I-NEXT: lw a1, 1200(a1)
+; RV32I-NEXT: lw a0, 1240(a0)
; RV32I-NEXT: add a0, a0, a1
; RV32I-NEXT: ret
;
; RV64I-LABEL: test_sh2add:
; RV64I: # %bb.0: # %entry
-; RV64I-NEXT: addi a0, a0, 1200
; RV64I-NEXT: slli a1, a1, 2
; RV64I-NEXT: slli a2, a2, 2
; RV64I-NEXT: add a1, a0, a1
; RV64I-NEXT: add a0, a2, a0
-; RV64I-NEXT: lw a1, 0(a1)
-; RV64I-NEXT: lw a0, 40(a0)
+; RV64I-NEXT: lw a1, 1200(a1)
+; RV64I-NEXT: lw a0, 1240(a0)
; RV64I-NEXT: addw a0, a0, a1
; RV64I-NEXT: ret
;
; RV32ZBA-LABEL: test_sh2add:
; RV32ZBA: # %bb.0: # %entry
-; RV32ZBA-NEXT: addi a0, a0, 1200
; RV32ZBA-NEXT: sh2add a1, a1, a0
; RV32ZBA-NEXT: sh2add a0, a2, a0
-; RV32ZBA-NEXT: lw a1, 0(a1)
-; RV32ZBA-NEXT: lw a0, 40(a0)
+; RV32ZBA-NEXT: lw a1, 1200(a1)
+; RV32ZBA-NEXT: lw a0, 1240(a0)
; RV32ZBA-NEXT: add a0, a0, a1
; RV32ZBA-NEXT: ret
;
; RV64ZBA-LABEL: test_sh2add:
; RV64ZBA: # %bb.0: # %entry
-; RV64ZBA-NEXT: addi a0, a0, 1200
; RV64ZBA-NEXT: sh2add a1, a1, a0
; RV64ZBA-NEXT: sh2add a0, a2, a0
-; RV64ZBA-NEXT: lw a1, 0(a1)
-; RV64ZBA-NEXT: lw a0, 40(a0)
+; RV64ZBA-NEXT: lw a1, 1200(a1)
+; RV64ZBA-NEXT: lw a0, 1240(a0)
; RV64ZBA-NEXT: addw a0, a0, a1
; RV64ZBA-NEXT: ret
entry:
@@ -127,13 +119,12 @@ entry:
define signext i16 @test_sh1add(ptr %p, iXLen %x, iXLen %y) {
; RV32I-LABEL: test_sh1add:
; RV32I: # %bb.0: # %entry
-; RV32I-NEXT: addi a0, a0, 1600
; RV32I-NEXT: slli a1, a1, 1
; RV32I-NEXT: slli a2, a2, 1
; RV32I-NEXT: add a1, a0, a1
; RV32I-NEXT: add a0, a2, a0
-; RV32I-NEXT: lh a1, 0(a1)
-; RV32I-NEXT: lh a0, 20(a0)
+; RV32I-NEXT: lh a1, 1600(a1)
+; RV32I-NEXT: lh a0, 1620(a0)
; RV32I-NEXT: add a0, a0, a1
; RV32I-NEXT: slli a0, a0, 16
; RV32I-NEXT: srai a0, a0, 16
@@ -141,13 +132,12 @@ define signext i16 @test_sh1add(ptr %p, iXLen %x, iXLen %y) {
;
; RV64I-LABEL: test_sh1add:
; RV64I: # %bb.0: # %entry
-; RV64I-NEXT: addi a0, a0, 1600
; RV64I-NEXT: slli a1, a1, 1
; RV64I-NEXT: slli a2, a2, 1
; RV64I-NEXT: add a1, a0, a1
; RV64I-NEXT: add a0, a2, a0
-; RV64I-NEXT: lh a1, 0(a1)
-; RV64I-NEXT: lh a0, 20(a0)
+; RV64I-NEXT: lh a1, 1600(a1)
+; RV64I-NEXT: lh a0, 1620(a0)
; RV64I-NEXT: add a0, a0, a1
; RV64I-NEXT: slli a0, a0, 48
; RV64I-NEXT: srai a0, a0, 48
@@ -155,11 +145,10 @@ define signext i16 @test_sh1add(ptr %p, iXLen %x, iXLen %y) {
;
; RV32ZBA-LABEL: test_sh1add:
; RV32ZBA: # %bb.0: # %entry
-; RV32ZBA-NEXT: addi a0, a0, 1600
; RV32ZBA-NEXT: sh1add a1, a1, a0
; RV32ZBA-NEXT: sh1add a0, a2, a0
-; RV32ZBA-NEXT: lh a1, 0(a1)
-; RV32ZBA-NEXT: lh a0, 20(a0)
+; RV32ZBA-NEXT: lh a1, 1600(a1)
+; RV32ZBA-NEXT: lh a0, 1620(a0)
; RV32ZBA-NEXT: add a0, a0, a1
; RV32ZBA-NEXT: slli a0, a0, 16
; RV32ZBA-NEXT: srai a0, a0, 16
@@ -167,11 +156,10 @@ define signext i16 @test_sh1add(ptr %p, iXLen %x, iXLen %y) {
;
; RV64ZBA-LABEL: test_sh1add:
; RV64ZBA: # %bb.0: # %entry
-; RV64ZBA-NEXT: addi a0, a0, 1600
; RV64ZBA-NEXT: sh1add a1, a1, a0
; RV64ZBA-NEXT: sh1add a0, a2, a0
-; RV64ZBA-NEXT: lh a1, 0(a1)
-; RV64ZBA-NEXT: lh a0, 20(a0)
+; RV64ZBA-NEXT: lh a1, 1600(a1)
+; RV64ZBA-NEXT: lh a0, 1620(a0)
; RV64ZBA-NEXT: add a0, a0, a1
; RV64ZBA-NEXT: slli a0, a0, 48
; RV64ZBA-NEXT: srai a0, a0, 48
@@ -190,22 +178,20 @@ entry:
define zeroext i8 @test_add(ptr %p, iXLen %x, iXLen %y) {
; CHECK-LABEL: test_add:
; CHECK: # %bb.0: # %entry
-; CHECK-NEXT: addi a0, a0, 1800
; CHECK-NEXT: add a1, a0, a1
; CHECK-NEXT: add a0, a2, a0
-; CHECK-NEXT: lbu a1, 0(a1)
-; CHECK-NEXT: lbu a0, 10(a0)
+; CHECK-NEXT: lbu a1, 1800(a1)
+; CHECK-NEXT: lbu a0, 1810(a0)
; CHECK-NEXT: add a0, a0, a1
; CHECK-NEXT: andi a0, a0, 255
; CHECK-NEXT: ret
;
; ZBA-LABEL: test_add:
; ZBA: # %bb.0: # %entry
-; ZBA-NEXT: addi a0, a0, 1800
; ZBA-NEXT: add a1, a0, a1
; ZBA-NEXT: add a0, a2, a0
-; ZBA-NEXT: lbu a1, 0(a1)
-; ZBA-NEXT: lbu a0, 10(a0)
+; ZBA-NEXT: lbu a1, 1800(a1)
+; ZBA-NEXT: lbu a0, 1810(a0)
; ZBA-NEXT: add a0, a0, a1
; ZBA-NEXT: andi a0, a0, 255
; ZBA-NEXT: ret
@@ -223,15 +209,14 @@ entry:
define i64 @test_sh3add_uw(ptr %p, i32 signext %x, i32 signext %y) {
; RV32I-LABEL: test_sh3add_uw:
; RV32I: # %bb.0: # %entry
-; RV32I-NEXT: addi a0, a0, 400
; RV32I-NEXT: slli a1, a1, 3
; RV32I-NEXT: slli a2, a2, 3
; RV32I-NEXT: add a1, a0, a1
; RV32I-NEXT: add a0, a0, a2
-; RV32I-NEXT: lw a2, 0(a1)
-; RV32I-NEXT: lw a1, 4(a1)
-; RV32I-NEXT: lw a3, 0(a0)
-; RV32I-NEXT: lw a0, 4(a0)
+; RV32I-NEXT: lw a2, 400(a1)
+; RV32I-NEXT: lw a1, 404(a1)
+; RV32I-NEXT: lw a3, 400(a0)
+; RV32I-NEXT: lw a0, 404(a0)
; RV32I-NEXT: add a1, a0, a1
; RV32I-NEXT: add a0, a3, a2
; RV32I-NEXT: sltu a2, a0, a3
@@ -240,27 +225,25 @@ define i64 @test_sh3add_uw(ptr %p, i32 signext %x, i32 signext %y) {
;
; RV64I-LABEL: test_sh3add_uw:
; RV64I: # %bb.0: # %entry
-; RV64I-NEXT: addi a0, a0, 400
; RV64I-NEXT: slli a1, a1, 32
; RV64I-NEXT: slli a2, a2, 32
; RV64I-NEXT: srli a1, a1, 29
; RV64I-NEXT: srli a2, a2, 29
; RV64I-NEXT: add a1, a0, a1
; RV64I-NEXT: add a0, a0, a2
-; RV64I-NEXT: ld a1, 0(a1)
-; RV64I-NEXT: ld a0, 0(a0)
+; RV64I-NEXT: ld a1, 400(a1)
+; RV64I-NEXT: ld a0, 400(a0)
; RV64I-NEXT: add a0, a0, a1
; RV64I-NEXT: ret
;
; RV32ZBA-LABEL: test_sh3add_uw:
; RV32ZBA: # %bb.0: # %entry
-; RV32ZBA-NEXT: addi a0, a0, 400
; RV32ZBA-NEXT: sh3add a1, a1, a0
; RV32ZBA-NEXT: sh3add a0, a2, a0
-; RV32ZBA-NEXT: lw a2, 0(a1)
-; RV32ZBA-NEXT: lw a1, 4(a1)
-; RV32ZBA-NEXT: lw a3, 0(a0)
-; RV32ZBA-NEXT: lw a0, 4(a0)
+; RV32ZBA-NEXT: lw a2, 400(a1)
+; RV32ZBA-NEXT: lw a1, 404(a1)
+; RV32ZBA-NEXT: lw a3, 400(a0)
+; RV32ZBA-NEXT: lw a0, 404(a0)
; RV32ZBA-NEXT: add a1, a0, a1
; RV32ZBA-NEXT: add a0, a3, a2
; RV32ZBA-NEXT: sltu a2, a0, a3
@@ -269,11 +252,10 @@ define i64 @test_sh3add_uw(ptr %p, i32 signext %x, i32 signext %y) {
;
; RV64ZBA-LABEL: test_sh3add_uw:
; RV64ZBA: # %bb.0: # %entry
-; RV64ZBA-NEXT: addi a0, a0, 400
; RV64ZBA-NEXT: sh3add.uw a1, a1, a0
; RV64ZBA-NEXT: sh3add.uw a0, a2, a0
-; RV64ZBA-NEXT: ld a1, 0(a1)
-; RV64ZBA-NEXT: ld a0, 0(a0)
+; RV64ZBA-NEXT: ld a1, 400(a1)
+; RV64ZBA-NEXT: ld a0, 400(a0)
; RV64ZBA-NEXT: add a0, a0, a1
; RV64ZBA-NEXT: ret
entry:
@@ -291,47 +273,43 @@ entry:
define signext i32 @test_sh2add_uw(ptr %p, i32 signext %x, i32 signext %y) {
; RV32I-LABEL: test_sh2add_uw:
; RV32I: # %bb.0: # %entry
-; RV32I-NEXT: addi a0, a0, 1200
; RV32I-NEXT: slli a1, a1, 2
; RV32I-NEXT: slli a2, a2, 2
; RV32I-NEXT: add a1, a0, a1
; RV32I-NEXT: add a0, a0, a2
-; RV32I-NEXT: lw a1, 0(a1)
-; RV32I-NEXT: lw a0, 0(a0)
+; RV32I-NEXT: lw a1, 1200(a1)
+; RV32I-NEXT: lw a0, 1200(a0)
; RV32I-NEXT: add a0, a0, a1
; RV32I-NEXT: ret
;
; RV64I-LABEL: test_sh2add_uw:
; RV64I: # %bb.0: # %entry
-; RV64I-NEXT: addi a0, a0, 1200
; RV64I-NEXT: slli a1, a1, 32
; RV64I-NEXT: slli a2, a2, 32
; RV64I-NEXT: srli a1, a1, 30
; RV64I-NEXT: srli a2, a2, 30
; RV64I-NEXT: add a1, a0, a1
; RV64I-NEXT: add a0, a0, a2
-; RV64I-NEXT: lw a1, 0(a1)
-; RV64I-NEXT: lw a0, 0(a0)
+; RV64I-NEXT: lw a1, 1200(a1)
+; RV64I-NEXT: lw a0, 1200(a0)
; RV64I-NEXT: addw a0, a0, a1
; RV64I-NEXT: ret
;
; RV32ZBA-LABEL: test_sh2add_uw:
; RV32ZBA: # %bb.0: # %entry
-; RV32ZBA-NEXT: addi a0, a0, 1200
; RV32ZBA-NEXT: sh2add a1, a1, a0
; RV32ZBA-NEXT: sh2add a0, a2, a0
-; RV32ZBA-NEXT: lw a1, 0(a1)
-; RV32ZBA-NEXT: lw a0, 0(a0)
+; RV32ZBA-NEXT: lw a1, 1200(a1)
+; RV32ZBA-NEXT: lw a0, 1200(a0)
; RV32ZBA-NEXT: add a0, a0, a1
; RV32ZBA-NEXT: ret
;
; RV64ZBA-LABEL: test_sh2add_uw:
; RV64ZBA: # %bb.0: # %entry
-; RV64ZBA-NEXT: addi a0, a0, 1200
; RV64ZBA-NEXT: sh2add.uw a1, a1, a0
; RV64ZBA-NEXT: sh2add.uw a0, a2, a0
-; RV64ZBA-NEXT: lw a1, 0(a1)
-; RV64ZBA-NEXT: lw a0, 0(a0)
+; RV64ZBA-NEXT: lw a1, 1200(a1)
+; RV64ZBA-NEXT: lw a0, 1200(a0)
; RV64ZBA-NEXT: addw a0, a0, a1
; RV64ZBA-NEXT: ret
entry:
@@ -349,13 +327,12 @@ entry:
define signext i16 @test_sh1add_uw(ptr %p, i32 signext %x, i32 signext %y) {
; RV32I-LABEL: test_sh1add_uw:
; RV32I: # %bb.0: # %entry
-; RV32I-NEXT: addi a0, a0, 1600
; RV32I-NEXT: slli a1, a1, 1
; RV32I-NEXT: slli a2, a2, 1
; RV32I-NEXT: add a1, a0, a1
; RV32I-NEXT: add a0, a2, a0
-; RV32I-NEXT: lh a1, 0(a1)
-; RV32I-NEXT: lh a0, 20(a0)
+; RV32I-NEXT: lh a1, 1600(a1)
+; RV32I-NEXT: lh a0, 1620(a0)
; RV32I-NEXT: add a0, a0, a1
; RV32I-NEXT: slli a0, a0, 16
; RV32I-NEXT: srai a0, a0, 16
@@ -363,7 +340,6 @@ define signext i16 @test_sh1add_uw(ptr %p, i32 signext %x, i32 signext %y) {
;
; RV64I-LABEL: test_sh1add_uw:
; RV64I: # %bb.0: # %entry
-; RV64I-NEXT: addi a0, a0, 1600
; RV64I-NEXT: slli a1, a1, 32
; RV64I-NEXT: addi a2, a2, 10
; RV64I-NEXT: srli a1, a1, 31
@@ -371,8 +347,8 @@ define signext i16 @test_sh1add_uw(ptr %p, i32 signext %x, i32 signext %y) {
; RV64I-NEXT: add a1, a0, a1
; RV64I-NEXT: srli a2, a2, 31
; RV64I-NEXT: add a0, a0, a2
-; RV64I-NEXT: lh a1, 0(a1)
-; RV64I-NEXT: lh a0, 0(a0)
+; RV64I-NEXT: lh a1, 1600(a1)
+; RV64I-NEXT: lh a0, 1600(a0)
; RV64I-NEXT: add a0, a0, a1
; RV64I-NEXT: slli a0, a0, 48
; RV64I-NEXT: srai a0, a0, 48
@@ -380,11 +356,10 @@ define signext i16 @test_sh1add_uw(ptr %p, i32 signext %x, i32 signext %y) {
;
; RV32ZBA-LABEL: test_sh1add_uw:
; RV32ZBA: # %bb.0: # %entry
-; RV32ZBA-NEXT: addi a0, a0, 1600
; RV32ZBA-NEXT: sh1add a1, a1, a0
; RV32ZBA-NEXT: sh1add a0, a2, a0
-; RV32ZBA-NEXT: lh a1, 0(a1)
-; RV32ZBA-NEXT: lh a0, 20(a0)
+; RV32ZBA-NEXT: lh a1, 1600(a1)
+; RV32ZBA-NEXT: lh a0, 1620(a0)
; RV32ZBA-NEXT: add a0, a0, a1
; RV32ZBA-NEXT: slli a0, a0, 16
; RV32ZBA-NEXT: srai a0, a0, 16
@@ -392,12 +367,11 @@ define signext i16 @test_sh1add_uw(ptr %p, i32 signext %x, i32 signext %y) {
;
; RV64ZBA-LABEL: test_sh1add_uw:
; RV64ZBA: # %bb.0: # %entry
-; RV64ZBA-NEXT: addi a0, a0, 1600
-; RV64ZBA-NEXT: addi a2, a2, 10
; RV64ZBA-NEXT: sh1add.uw a1, a1, a0
+; RV64ZBA-NEXT: addi a2, a2, 10
; RV64ZBA-NEXT: sh1add.uw a0, a2, a0
-; RV64ZBA-NEXT: lh a1, 0(a1)
-; RV64ZBA-NEXT: lh a0, 0(a0)
+; RV64ZBA-NEXT: lh a1, 1600(a1)
+; RV64ZBA-NEXT: lh a0, 1600(a0)
; RV64ZBA-NEXT: add a0, a0, a1
; RV64ZBA-NEXT: slli a0, a0, 48
; RV64ZBA-NEXT: srai a0, a0, 48
@@ -418,48 +392,44 @@ entry:
define zeroext i8 @test_add_uw(ptr %p, i32 signext %x, i32 signext %y) {
; RV32I-LABEL: test_add_uw:
; RV32I: # %bb.0: # %entry
-; RV32I-NEXT: addi a0, a0, 1800
; RV32I-NEXT: add a1, a0, a1
; RV32I-NEXT: add a0, a0, a2
-; RV32I-NEXT: lbu a1, 0(a1)
-; RV32I-NEXT: lbu a0, 0(a0)
+; RV32I-NEXT: lbu a1, 1800(a1)
+; RV32I-NEXT: lbu a0, 1800(a0)
; RV32I-NEXT: add a0, a0, a1
; RV32I-NEXT: andi a0, a0, 255
; RV32I-NEXT: ret
;
; RV64I-LABEL: test_add_uw:
; RV64I: # %bb.0: # %entry
-; RV64I-NEXT: addi a0, a0, 1800
; RV64I-NEXT: slli a1, a1, 32
; RV64I-NEXT: slli a2, a2, 32
; RV64I-NEXT: srli a1, a1, 32
; RV64I-NEXT: srli a2, a2, 32
; RV64I-NEXT: add a1, a0, a1
; RV64I-NEXT: add a0, a0, a2
-; RV64I-NEXT: lbu a1, 0(a1)
-; RV64I-NEXT: lbu a0, 0(a0)
+; RV64I-NEXT: lbu a1, 1800(a1)
+; RV64I-NEXT: lbu a0, 1800(a0)
; RV64I-NEXT: add a0, a0, a1
; RV64I-NEXT: andi a0, a0, 255
; RV64I-NEXT: ret
;
; RV32ZBA-LABEL: test_add_uw:
; RV32ZBA: # %bb.0: # %entry
-; RV32ZBA-NEXT: addi a0, a0, 1800
; RV32ZBA-NEXT: add a1, a0, a1
; RV32ZBA-NEXT: add a0, a0, a2
-; RV32ZBA-NEXT: lbu a1, 0(a1)
-; RV32ZBA-NEXT: lbu a0, 0(a0)
+; RV32ZBA-NEXT: lbu a1, 1800(a1)
+; RV32ZBA-NEXT: lbu a0, 1800(a0)
; RV32ZBA-NEXT: add a0, a0, a1
; RV32ZBA-NEXT: andi a0, a0, 255
; RV32ZBA-NEXT: ret
;
; RV64ZBA-LABEL: test_add_uw:
; RV64ZBA: # %bb.0: # %entry
-; RV64ZBA-NEXT: addi a0, a0, 1800
; RV64ZBA-NEXT: add.uw a1, a1, a0
; RV64ZBA-NEXT: add.uw a0, a2, a0
-; RV64ZBA-NEXT: lbu a1, 0(a1)
-; RV64ZBA-NEXT: lbu a0, 0(a0)
+; RV64ZBA-NEXT: lbu a1, 1800(a1)
+; RV64ZBA-NEXT: lbu a0, 1800(a0)
; RV64ZBA-NEXT: add a0, a0, a1
; RV64ZBA-NEXT: andi a0, a0, 255
; RV64ZBA-NEXT: ret
@@ -479,45 +449,41 @@ entry:
define signext i32 @test_scaled_index_addi(ptr %p, iXLen %x) {
; RV32I-LABEL: test_scaled_index_addi:
; RV32I: # %bb.0: # %entry
-; RV32I-NEXT: addi a1, a1, -1
; RV32I-NEXT: slli a2, a1, 2
; RV32I-NEXT: slli a1, a1, 1
; RV32I-NEXT: add a2, a0, a2
; RV32I-NEXT: add a0, a0, a1
-; RV32I-NEXT: lw a1, 1200(a2)
-; RV32I-NEXT: lh a0, 1600(a0)
+; RV32I-NEXT: lw a1, 1196(a2)
+; RV32I-NEXT: lh a0, 1598(a0)
; RV32I-NEXT: add a0, a1, a0
; RV32I-NEXT: ret
;
; RV64I-LABEL: test_scaled_index_addi:
; RV64I: # %bb.0: # %entry
-; RV64I-NEXT: addi a1, a1, -1
; RV64I-NEXT: slli a2, a1, 2
; RV64I-NEXT: slli a1, a1, 1
; RV64I-NEXT: add a2, a0, a2
; RV64I-NEXT: add a0, a0, a1
-; RV64I-NEXT: lw a1, 1200(a2)
-; RV64I-NEXT: lh a0, 1600(a0)
+; RV64I-NEXT: lw a1, 1196(a2)
+; RV64I-NEXT: lh a0, 1598(a0)
; RV64I-NEXT: addw a0, a1, a0
; RV64I-NEXT: ret
;
; RV32ZBA-LABEL: test_scaled_index_addi:
; RV32ZBA: # %bb.0: # %entry
-; RV32ZBA-NEXT: addi a1, a1, -1
; RV32ZBA-NEXT: sh2add a2, a1, a0
; RV32ZBA-NEXT: sh1add a0, a1, a0
-; RV32ZBA-NEXT: lw a1, 1200(a2)
-; RV32ZBA-NEXT: lh a0, 1600(a0)
+; RV32ZBA-NEXT: lw a1, 1196(a2)
+; RV32ZBA-NEXT: lh a0, 1598(a0)
; RV32ZBA-NEXT: add a0, a1, a0
; RV32ZBA-NEXT: ret
;
; RV64ZBA-LABEL: test_scaled_index_addi:
; RV64ZBA: # %bb.0: # %entry
-; RV64ZBA-NEXT: addi a1, a1, -1
; RV64ZBA-NEXT: sh2add a2, a1, a0
; RV64ZBA-NEXT: sh1add a0, a1, a0
-; RV64ZBA-NEXT: lw a1, 1200(a2)
-; RV64ZBA-NEXT: lh a0, 1600(a0)
+; RV64ZBA-NEXT: lw a1, 1196(a2)
+; RV64ZBA-NEXT: lh a0, 1598(a0)
; RV64ZBA-NEXT: addw a0, a1, a0
; RV64ZBA-NEXT: ret
entry:
@@ -540,11 +506,10 @@ define signext i32 @test_medium_offset(ptr %p, iXLen %x, iXLen %y) {
; RV32I-NEXT: addi a0, a0, 2047
; RV32I-NEXT: slli a1, a1, 2
; RV32I-NEXT: slli a2, a2, 2
-; RV32I-NEXT: addi a0, a0, 753
; RV32I-NEXT: add a1, a0, a1
; RV32I-NEXT: add a0, a2, a0
-; RV32I-NEXT: lw a1, 0(a1)
-; RV32I-NEXT: lw a0, 40(a0)
+; RV32I-NEXT: lw a1, 753(a1)
+; RV32I-NEXT: lw a0, 793(a0)
; RV32I-NEXT: add a0, a0, a1
; RV32I-NEXT: ret
;
@@ -553,33 +518,30 @@ define signext i32 @test_medium_offset(ptr %p, iXLen %x, iXLen %y) {
; RV64I-NEXT: addi a0, a0, 2047
; RV64I-NEXT: slli a1, a1, 2
; RV64I-NEXT: slli a2, a2, 2
-; RV64I-NEXT: addi a0, a0, 753
; RV64I-NEXT: add a1, a0, a1
; RV64I-NEXT: add a0, a2, a0
-; RV64I-NEXT: lw a1, 0(a1)
-; RV64I-NEXT: lw a0, 40(a0)
+; RV64I-NEXT: lw a1, 753(a1)
+; RV64I-NEXT: lw a0, 793(a0)
; RV64I-NEXT: addw a0, a0, a1
; RV64I-NEXT: ret
;
; RV32ZBA-LABEL: test_medium_offset:
; RV32ZBA: # %bb.0: # %entry
; RV32ZBA-NEXT: addi a0, a0, 2047
-; RV32ZBA-NEXT: addi a0, a0, 753
; RV32ZBA-NEXT: sh2add a1, a1, a0
; RV32ZBA-NEXT: sh2add a0, a2, a0
-; RV32ZBA-NEXT: lw a1, 0(a1)
-; RV32ZBA-NEXT: lw a0, 40(a0)
+; RV32ZBA-NEXT: lw a1, 753(a1)
+; RV32ZBA-NEXT: lw a0, 793(a0)
; RV32ZBA-NEXT: add a0, a0, a1
; RV32ZBA-NEXT: ret
;
; RV64ZBA-LABEL: test_medium_offset:
; RV64ZBA: # %bb.0: # %entry
; RV64ZBA-NEXT: addi a0, a0, 2047
-; RV64ZBA-NEXT: addi a0, a0, 753
; RV64ZBA-NEXT: sh2add a1, a1, a0
; RV64ZBA-NEXT: sh2add a0, a2, a0
-; RV64ZBA-NEXT: lw a1, 0(a1)
-; RV64ZBA-NEXT: lw a0, 40(a0)
+; RV64ZBA-NEXT: lw a1, 753(a1)
+; RV64ZBA-NEXT: lw a0, 793(a0)
; RV64ZBA-NEXT: addw a0, a0, a1
; RV64ZBA-NEXT: ret
entry:
@@ -600,12 +562,11 @@ define signext i32 @test_large_offset(ptr %p, iXLen %x, iXLen %y) {
; RV32I-NEXT: lui a3, 2
; RV32I-NEXT: slli a1, a1, 2
; RV32I-NEXT: slli a2, a2, 2
-; RV32I-NEXT: addi a3, a3, -1392
; RV32I-NEXT: add a0, a0, a3
; RV32I-NEXT: add a1, a0, a1
; RV32I-NEXT: add a0, a2, a0
-; RV32I-NEXT: lw a1, 0(a1)
-; RV32I-NEXT: lw a0, 40(a0)
+; RV32I-NEXT: lw a1, -1392(a1)
+; RV32I-NEXT: lw a0, -1352(a0)
; RV32I-NEXT: add a0, a0, a1
; RV32I-NEXT: ret
;
diff --git a/llvm/test/CodeGen/RISCV/split-offsets.ll b/llvm/test/CodeGen/RISCV/split-offsets.ll
index 6d14c0d76a45c..8b80f0140a88a 100644
--- a/llvm/test/CodeGen/RISCV/split-offsets.ll
+++ b/llvm/test/CodeGen/RISCV/split-offsets.ll
@@ -14,14 +14,13 @@ define void @test1(ptr %sp, ptr %t, i32 %n) {
; RV32I-NEXT: lui a2, 20
; RV32I-NEXT: lw a0, 0(a0)
; RV32I-NEXT: li a3, 2
-; RV32I-NEXT: addi a2, a2, -1920
; RV32I-NEXT: add a1, a1, a2
; RV32I-NEXT: add a0, a0, a2
; RV32I-NEXT: li a2, 1
-; RV32I-NEXT: sw a3, 0(a0)
-; RV32I-NEXT: sw a2, 4(a0)
-; RV32I-NEXT: sw a2, 0(a1)
-; RV32I-NEXT: sw a3, 4(a1)
+; RV32I-NEXT: sw a3, -1920(a0)
+; RV32I-NEXT: sw a2, -1916(a0)
+; RV32I-NEXT: sw a2, -1920(a1)
+; RV32I-NEXT: sw a3, -1916(a1)
; RV32I-NEXT: ret
;
; RV64I-LABEL: test1:
@@ -57,7 +56,6 @@ define void @test2(ptr %sp, ptr %t, i32 %n) {
; RV32I: # %bb.0: # %entry
; RV32I-NEXT: li a3, 0
; RV32I-NEXT: lui a4, 20
-; RV32I-NEXT: addi a4, a4, -1920
; RV32I-NEXT: add a1, a1, a4
; RV32I-NEXT: lw a0, 0(a0)
; RV32I-NEXT: add a0, a0, a4
@@ -65,10 +63,10 @@ define void @test2(ptr %sp, ptr %t, i32 %n) {
; RV32I-NEXT: .LBB1_1: # %while_body
; RV32I-NEXT: # =>This Inner Loop Header: Depth=1
; RV32I-NEXT: addi a4, a3, 1
-; RV32I-NEXT: sw a4, 0(a0)
-; RV32I-NEXT: sw a3, 4(a0)
-; RV32I-NEXT: sw a4, 0(a1)
-; RV32I-NEXT: sw a3, 4(a1)
+; RV32I-NEXT: sw a4, -1920(a0)
+; RV32I-NEXT: sw a3, -1916(a0)
+; RV32I-NEXT: sw a4, -1920(a1)
+; RV32I-NEXT: sw a3, -1916(a1)
; RV32I-NEXT: mv a3, a4
; RV32I-NEXT: blt a4, a2, .LBB1_1
; RV32I-NEXT: .LBB1_2: # %while_end
@@ -126,11 +124,10 @@ define void @test3(ptr %t) {
; RV32I: # %bb.0: # %entry
; RV32I-NEXT: lui a1, 20
; RV32I-NEXT: li a2, 2
-; RV32I-NEXT: addi a1, a1, -1920
; RV32I-NEXT: add a0, a0, a1
; RV32I-NEXT: li a1, 3
-; RV32I-NEXT: sw a2, 4(a0)
-; RV32I-NEXT: sw a1, 8(a0)
+; RV32I-NEXT: sw a2, -1916(a0)
+; RV32I-NEXT: sw a1, -1912(a0)
; RV32I-NEXT: ret
;
; RV64I-LABEL: test3:
diff --git a/llvm/test/CodeGen/RISCV/xtheadmemidx.ll b/llvm/test/CodeGen/RISCV/xtheadmemidx.ll
index f6b7f97f6525c..0708838223cf3 100644
--- a/llvm/test/CodeGen/RISCV/xtheadmemidx.ll
+++ b/llvm/test/CodeGen/RISCV/xtheadmemidx.ll
@@ -1136,10 +1136,9 @@ define i64 @lrd_large_offset(ptr %a, i64 %b) {
; RV32XTHEADMEMIDX-NEXT: slli a1, a1, 3
; RV32XTHEADMEMIDX-NEXT: add a0, a1, a0
; RV32XTHEADMEMIDX-NEXT: lui a1, 23
-; RV32XTHEADMEMIDX-NEXT: addi a1, a1, 1792
; RV32XTHEADMEMIDX-NEXT: add a1, a0, a1
-; RV32XTHEADMEMIDX-NEXT: lw a0, 0(a1)
-; RV32XTHEADMEMIDX-NEXT: lw a1, 4(a1)
+; RV32XTHEADMEMIDX-NEXT: lw a0, 1792(a1)
+; RV32XTHEADMEMIDX-NEXT: lw a1, 1796(a1)
; RV32XTHEADMEMIDX-NEXT: ret
;
; RV64XTHEADMEMIDX-LABEL: lrd_large_offset:
>From 19ad1bc7c322ecadd59d934230b03a52f048e40a Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Thu, 13 Feb 2025 17:09:36 -0800
Subject: [PATCH 3/6] fixup! clang-format
---
llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp b/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp
index b61eda499fcd0..5c457bbd727fc 100644
--- a/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp
+++ b/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp
@@ -223,8 +223,8 @@ bool RISCVFoldMemOffset::foldOffset(
// If the offset is new or changed, add the destination register to the
// work list.
int64_t OffsetVal = Offset.getValue();
- auto P = RegToOffsetMap.try_emplace(User.getOperand(0).getReg(),
- OffsetVal);
+ auto P =
+ RegToOffsetMap.try_emplace(User.getOperand(0).getReg(), OffsetVal);
if (P.second) {
Worklist.push(User.getOperand(0).getReg());
} else if (P.first->second != OffsetVal) {
>From 283c9f3e977714c0d2fba6fd01ed0542834ee63a Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 18 Feb 2025 11:16:34 -0800
Subject: [PATCH 4/6] fixup! Address review comments.
---
llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp | 19 +++++----------
.../lib/Target/RISCV/RISCVMergeBaseOffset.cpp | 7 ------
.../test/CodeGen/RISCV/fold-addi-loadstore.ll | 24 ++++++++++++-------
3 files changed, 22 insertions(+), 28 deletions(-)
diff --git a/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp b/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp
index 5c457bbd727fc..cff640986e947 100644
--- a/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp
+++ b/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp
@@ -62,17 +62,15 @@ class FoldableOffset {
FoldableOffset &operator+=(int64_t RHS) {
if (!Offset)
- Offset = RHS;
- else
- Offset = (uint64_t)*Offset + (uint64_t)RHS;
+ Offset = 0;
+ Offset = (uint64_t)*Offset + (uint64_t)RHS;
return *this;
}
FoldableOffset &operator-=(int64_t RHS) {
if (!Offset)
- Offset = -(uint64_t)RHS;
- else
- Offset = (uint64_t)*Offset - (uint64_t)RHS;
+ Offset = 0;
+ Offset = (uint64_t)*Offset - (uint64_t)RHS;
return *this;
}
@@ -245,9 +243,7 @@ bool RISCVFoldMemOffset::runOnMachineFunction(MachineFunction &MF) {
if (MF.getFunction().hasOptSize())
return false;
- const MachineRegisterInfo &MRI = MF.getRegInfo();
- const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>();
- const RISCVInstrInfo &TII = *ST.getInstrInfo();
+ MachineRegisterInfo &MRI = MF.getRegInfo();
bool MadeChange = false;
for (MachineBasicBlock &MBB : MF) {
@@ -277,10 +273,7 @@ bool RISCVFoldMemOffset::runOnMachineFunction(MachineFunction &MF) {
for (auto [MemMI, NewOffset] : FoldableInstrs)
MemMI->getOperand(2).setImm(NewOffset);
- // Replace ADDI with a copy.
- BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(RISCV::COPY))
- .add(MI.getOperand(0))
- .add(MI.getOperand(1));
+ MRI.replaceRegWith(MI.getOperand(0).getReg(), MI.getOperand(1).getReg());
MI.eraseFromParent();
}
}
diff --git a/llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp b/llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp
index 3dab7d9bb0912..bbbb1e1595982 100644
--- a/llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp
+++ b/llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp
@@ -238,13 +238,6 @@ bool RISCVMergeBaseOffsetOpt::foldLargeOffset(MachineInstr &Hi,
foldOffset(Hi, Lo, TailAdd, Offset);
OffsetTail.eraseFromParent();
return true;
- } else if (OffsetTail.getOpcode() == RISCV::COPY &&
- OffsetTail.getOperand(1).getReg() == RISCV::X0) {
- // Fold mem offset can leave copies from X0 in place of an ADDI and they
- // might not have been eliminated yet.
- foldOffset(Hi, Lo, TailAdd, 0);
- OffsetTail.eraseFromParent();
- return true;
}
return false;
}
diff --git a/llvm/test/CodeGen/RISCV/fold-addi-loadstore.ll b/llvm/test/CodeGen/RISCV/fold-addi-loadstore.ll
index 80ee5776f76f1..d9ef0557c991c 100644
--- a/llvm/test/CodeGen/RISCV/fold-addi-loadstore.ll
+++ b/llvm/test/CodeGen/RISCV/fold-addi-loadstore.ll
@@ -1167,8 +1167,10 @@ declare void @f(ptr)
define i32 @crash() {
; RV32I-LABEL: crash:
; RV32I: # %bb.0: # %entry
-; RV32I-NEXT: lui a0, %hi(g+401)
-; RV32I-NEXT: lbu a0, %lo(g+401)(a0)
+; RV32I-NEXT: lui a0, %hi(g)
+; RV32I-NEXT: addi a0, a0, %lo(g)
+; RV32I-NEXT: add a0, a0, zero
+; RV32I-NEXT: lbu a0, 401(a0)
; RV32I-NEXT: seqz a0, a0
; RV32I-NEXT: sw a0, 0(zero)
; RV32I-NEXT: li a0, 0
@@ -1177,8 +1179,10 @@ define i32 @crash() {
; RV32I-MEDIUM-LABEL: crash:
; RV32I-MEDIUM: # %bb.0: # %entry
; RV32I-MEDIUM-NEXT: .Lpcrel_hi14:
-; RV32I-MEDIUM-NEXT: auipc a0, %pcrel_hi(g+401)
-; RV32I-MEDIUM-NEXT: lbu a0, %pcrel_lo(.Lpcrel_hi14)(a0)
+; RV32I-MEDIUM-NEXT: auipc a0, %pcrel_hi(g)
+; RV32I-MEDIUM-NEXT: addi a0, a0, %pcrel_lo(.Lpcrel_hi14)
+; RV32I-MEDIUM-NEXT: add a0, a0, zero
+; RV32I-MEDIUM-NEXT: lbu a0, 401(a0)
; RV32I-MEDIUM-NEXT: seqz a0, a0
; RV32I-MEDIUM-NEXT: sw a0, 0(zero)
; RV32I-MEDIUM-NEXT: li a0, 0
@@ -1186,8 +1190,10 @@ define i32 @crash() {
;
; RV64I-LABEL: crash:
; RV64I: # %bb.0: # %entry
-; RV64I-NEXT: lui a0, %hi(g+401)
-; RV64I-NEXT: lbu a0, %lo(g+401)(a0)
+; RV64I-NEXT: lui a0, %hi(g)
+; RV64I-NEXT: addi a0, a0, %lo(g)
+; RV64I-NEXT: add a0, a0, zero
+; RV64I-NEXT: lbu a0, 401(a0)
; RV64I-NEXT: seqz a0, a0
; RV64I-NEXT: sw a0, 0(zero)
; RV64I-NEXT: li a0, 0
@@ -1196,8 +1202,10 @@ define i32 @crash() {
; RV64I-MEDIUM-LABEL: crash:
; RV64I-MEDIUM: # %bb.0: # %entry
; RV64I-MEDIUM-NEXT: .Lpcrel_hi14:
-; RV64I-MEDIUM-NEXT: auipc a0, %pcrel_hi(g+401)
-; RV64I-MEDIUM-NEXT: lbu a0, %pcrel_lo(.Lpcrel_hi14)(a0)
+; RV64I-MEDIUM-NEXT: auipc a0, %pcrel_hi(g)
+; RV64I-MEDIUM-NEXT: addi a0, a0, %pcrel_lo(.Lpcrel_hi14)
+; RV64I-MEDIUM-NEXT: add a0, a0, zero
+; RV64I-MEDIUM-NEXT: lbu a0, 401(a0)
; RV64I-MEDIUM-NEXT: seqz a0, a0
; RV64I-MEDIUM-NEXT: sw a0, 0(zero)
; RV64I-MEDIUM-NEXT: li a0, 0
>From d38cdaea7eb5ef53141368ab3e0edfba20eb326c Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 18 Feb 2025 11:30:10 -0800
Subject: [PATCH 5/6] fixup! remove unused function.
---
llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp | 7 -------
1 file changed, 7 deletions(-)
diff --git a/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp b/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp
index cff640986e947..a5de12f256185 100644
--- a/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp
+++ b/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp
@@ -67,13 +67,6 @@ class FoldableOffset {
return *this;
}
- FoldableOffset &operator-=(int64_t RHS) {
- if (!Offset)
- Offset = 0;
- Offset = (uint64_t)*Offset - (uint64_t)RHS;
- return *this;
- }
-
int64_t operator*() { return *Offset; }
};
>From 0fb7fefadc6768f2526c43e1c7527114cffdd7d2 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 18 Feb 2025 17:47:32 -0800
Subject: [PATCH 6/6] fixup! ensure we only check for users of virtual
registers. Don't optimize addi with x0.
---
llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp | 7 ++++
.../test/CodeGen/RISCV/fold-addi-loadstore.ll | 33 ++++++++-----------
2 files changed, 20 insertions(+), 20 deletions(-)
diff --git a/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp b/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp
index a5de12f256185..989e9d859d64f 100644
--- a/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp
+++ b/llvm/lib/Target/RISCV/RISCVFoldMemOffset.cpp
@@ -103,6 +103,9 @@ bool RISCVFoldMemOffset::foldOffset(
Register Reg = Worklist.front();
Worklist.pop();
+ if (!Reg.isVirtual())
+ return false;
+
for (auto &User : MRI.use_nodbg_instructions(Reg)) {
FoldableOffset Offset;
@@ -250,6 +253,10 @@ bool RISCVFoldMemOffset::runOnMachineFunction(MachineFunction &MF) {
if (!MI.getOperand(1).isReg() || !MI.getOperand(2).isImm())
continue;
+ // Ignore 'li'.
+ if (MI.getOperand(1).getReg() == RISCV::X0)
+ continue;
+
int64_t Offset = MI.getOperand(2).getImm();
assert(isInt<12>(Offset));
diff --git a/llvm/test/CodeGen/RISCV/fold-addi-loadstore.ll b/llvm/test/CodeGen/RISCV/fold-addi-loadstore.ll
index d9ef0557c991c..59ba3652c89e9 100644
--- a/llvm/test/CodeGen/RISCV/fold-addi-loadstore.ll
+++ b/llvm/test/CodeGen/RISCV/fold-addi-loadstore.ll
@@ -1167,10 +1167,8 @@ declare void @f(ptr)
define i32 @crash() {
; RV32I-LABEL: crash:
; RV32I: # %bb.0: # %entry
-; RV32I-NEXT: lui a0, %hi(g)
-; RV32I-NEXT: addi a0, a0, %lo(g)
-; RV32I-NEXT: add a0, a0, zero
-; RV32I-NEXT: lbu a0, 401(a0)
+; RV32I-NEXT: lui a0, %hi(g+401)
+; RV32I-NEXT: lbu a0, %lo(g+401)(a0)
; RV32I-NEXT: seqz a0, a0
; RV32I-NEXT: sw a0, 0(zero)
; RV32I-NEXT: li a0, 0
@@ -1179,10 +1177,8 @@ define i32 @crash() {
; RV32I-MEDIUM-LABEL: crash:
; RV32I-MEDIUM: # %bb.0: # %entry
; RV32I-MEDIUM-NEXT: .Lpcrel_hi14:
-; RV32I-MEDIUM-NEXT: auipc a0, %pcrel_hi(g)
-; RV32I-MEDIUM-NEXT: addi a0, a0, %pcrel_lo(.Lpcrel_hi14)
-; RV32I-MEDIUM-NEXT: add a0, a0, zero
-; RV32I-MEDIUM-NEXT: lbu a0, 401(a0)
+; RV32I-MEDIUM-NEXT: auipc a0, %pcrel_hi(g+401)
+; RV32I-MEDIUM-NEXT: lbu a0, %pcrel_lo(.Lpcrel_hi14)(a0)
; RV32I-MEDIUM-NEXT: seqz a0, a0
; RV32I-MEDIUM-NEXT: sw a0, 0(zero)
; RV32I-MEDIUM-NEXT: li a0, 0
@@ -1190,10 +1186,8 @@ define i32 @crash() {
;
; RV64I-LABEL: crash:
; RV64I: # %bb.0: # %entry
-; RV64I-NEXT: lui a0, %hi(g)
-; RV64I-NEXT: addi a0, a0, %lo(g)
-; RV64I-NEXT: add a0, a0, zero
-; RV64I-NEXT: lbu a0, 401(a0)
+; RV64I-NEXT: lui a0, %hi(g+401)
+; RV64I-NEXT: lbu a0, %lo(g+401)(a0)
; RV64I-NEXT: seqz a0, a0
; RV64I-NEXT: sw a0, 0(zero)
; RV64I-NEXT: li a0, 0
@@ -1202,10 +1196,8 @@ define i32 @crash() {
; RV64I-MEDIUM-LABEL: crash:
; RV64I-MEDIUM: # %bb.0: # %entry
; RV64I-MEDIUM-NEXT: .Lpcrel_hi14:
-; RV64I-MEDIUM-NEXT: auipc a0, %pcrel_hi(g)
-; RV64I-MEDIUM-NEXT: addi a0, a0, %pcrel_lo(.Lpcrel_hi14)
-; RV64I-MEDIUM-NEXT: add a0, a0, zero
-; RV64I-MEDIUM-NEXT: lbu a0, 401(a0)
+; RV64I-MEDIUM-NEXT: auipc a0, %pcrel_hi(g+401)
+; RV64I-MEDIUM-NEXT: lbu a0, %pcrel_lo(.Lpcrel_hi14)(a0)
; RV64I-MEDIUM-NEXT: seqz a0, a0
; RV64I-MEDIUM-NEXT: sw a0, 0(zero)
; RV64I-MEDIUM-NEXT: li a0, 0
@@ -1213,11 +1205,12 @@ define i32 @crash() {
;
; RV64I-LARGE-LABEL: crash:
; RV64I-LARGE: # %bb.0: # %entry
+; RV64I-LARGE-NEXT: li a0, 1
; RV64I-LARGE-NEXT: .Lpcrel_hi15:
-; RV64I-LARGE-NEXT: auipc a0, %pcrel_hi(.LCPI21_0)
-; RV64I-LARGE-NEXT: ld a0, %pcrel_lo(.Lpcrel_hi15)(a0)
-; RV64I-LARGE-NEXT: add a0, a0, zero
-; RV64I-LARGE-NEXT: lbu a0, 401(a0)
+; RV64I-LARGE-NEXT: auipc a1, %pcrel_hi(.LCPI21_0)
+; RV64I-LARGE-NEXT: ld a1, %pcrel_lo(.Lpcrel_hi15)(a1)
+; RV64I-LARGE-NEXT: add a0, a1, a0
+; RV64I-LARGE-NEXT: lbu a0, 400(a0)
; RV64I-LARGE-NEXT: seqz a0, a0
; RV64I-LARGE-NEXT: sw a0, 0(zero)
; RV64I-LARGE-NEXT: li a0, 0
More information about the llvm-commits
mailing list