diff --git a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp --- a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp @@ -303,6 +303,16 @@ .addReg(SrcReg) .addImm(Val) .setMIFlag(Flag); + } else if (isInt<12>(Val / 2) && isInt<12>(Val - Val / 2)) { + // Split the offset across 2 instructions. + BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), DestReg) + .addReg(SrcReg) + .addImm(Val / 2) + .setMIFlag(Flag); + BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), DestReg) + .addReg(DestReg, RegState::Kill) + .addImm(Val - Val / 2) + .setMIFlag(Flag); } else { unsigned Opc = RISCV::ADD; bool IsSub = Val < 0; diff --git a/llvm/test/CodeGen/RISCV/rvv/emergency-slot.mir b/llvm/test/CodeGen/RISCV/rvv/emergency-slot.mir --- a/llvm/test/CodeGen/RISCV/rvv/emergency-slot.mir +++ b/llvm/test/CodeGen/RISCV/rvv/emergency-slot.mir @@ -153,9 +153,8 @@ ; CHECK-NEXT: PseudoBR %bb.2 ; CHECK-NEXT: {{ $}} ; CHECK-NEXT: bb.2: - ; CHECK-NEXT: $x10 = frame-destroy LUI 1 - ; CHECK-NEXT: $x10 = frame-destroy ADDIW killed $x10, -1792 - ; CHECK-NEXT: $x2 = frame-destroy SUB $x8, killed $x10 + ; CHECK-NEXT: $x2 = frame-destroy ADDI $x8, -1152 + ; CHECK-NEXT: $x2 = frame-destroy ADDI killed $x2, -1152 ; CHECK-NEXT: $x2 = frame-destroy ADDI $x2, 272 ; CHECK-NEXT: $x1 = LD $x2, 2024 :: (load (s64) from %stack.3) ; CHECK-NEXT: $x8 = LD $x2, 2016 :: (load (s64) from %stack.4) diff --git a/llvm/test/CodeGen/RISCV/rvv/large-rvv-stack-size.mir b/llvm/test/CodeGen/RISCV/rvv/large-rvv-stack-size.mir --- a/llvm/test/CodeGen/RISCV/rvv/large-rvv-stack-size.mir +++ b/llvm/test/CodeGen/RISCV/rvv/large-rvv-stack-size.mir @@ -32,9 +32,8 @@ ; CHECK-NEXT: vs1r.v v25, (a0) # Unknown-size Folded Spill ; CHECK-NEXT: ld a0, 8(sp) ; CHECK-NEXT: call spillslot@plt - ; CHECK-NEXT: lui a0, 1 - ; CHECK-NEXT: addiw a0, a0, -1792 - ; CHECK-NEXT: sub sp, s0, a0 + ; CHECK-NEXT: addi sp, s0, -1152 + ; CHECK-NEXT: addi sp, sp, -1152 ; CHECK-NEXT: addi sp, sp, 272 ; CHECK-NEXT: ld ra, 2024(sp) # 8-byte Folded Reload ; CHECK-NEXT: ld s0, 2016(sp) # 8-byte Folded Reload diff --git a/llvm/test/CodeGen/RISCV/stack-realignment.ll b/llvm/test/CodeGen/RISCV/stack-realignment.ll --- a/llvm/test/CodeGen/RISCV/stack-realignment.ll +++ b/llvm/test/CodeGen/RISCV/stack-realignment.ll @@ -453,19 +453,17 @@ ; RV32I-NEXT: .cfi_offset s0, -8 ; RV32I-NEXT: addi s0, sp, 2032 ; RV32I-NEXT: .cfi_def_cfa s0, 0 -; RV32I-NEXT: lui a0, 1 -; RV32I-NEXT: addi a0, a0, -2032 -; RV32I-NEXT: sub sp, sp, a0 +; RV32I-NEXT: addi sp, sp, -1032 +; RV32I-NEXT: addi sp, sp, -1032 ; RV32I-NEXT: andi sp, sp, -2048 ; RV32I-NEXT: lui a0, 1 ; RV32I-NEXT: addi a0, a0, -2048 ; RV32I-NEXT: add a0, sp, a0 ; RV32I-NEXT: call callee@plt -; RV32I-NEXT: lui a0, 1 -; RV32I-NEXT: sub sp, s0, a0 -; RV32I-NEXT: lui a0, 1 -; RV32I-NEXT: addi a0, a0, -2032 -; RV32I-NEXT: add sp, sp, a0 +; RV32I-NEXT: addi sp, s0, -2048 +; RV32I-NEXT: addi sp, sp, -2048 +; RV32I-NEXT: addi sp, sp, 1032 +; RV32I-NEXT: addi sp, sp, 1032 ; RV32I-NEXT: lw ra, 2028(sp) # 4-byte Folded Reload ; RV32I-NEXT: lw s0, 2024(sp) # 4-byte Folded Reload ; RV32I-NEXT: addi sp, sp, 2032 @@ -481,19 +479,17 @@ ; RV64I-NEXT: .cfi_offset s0, -16 ; RV64I-NEXT: addi s0, sp, 2032 ; RV64I-NEXT: .cfi_def_cfa s0, 0 -; RV64I-NEXT: lui a0, 1 -; RV64I-NEXT: addiw a0, a0, -2032 -; RV64I-NEXT: sub sp, sp, a0 +; RV64I-NEXT: addi sp, sp, -1032 +; RV64I-NEXT: addi sp, sp, -1032 ; RV64I-NEXT: andi sp, sp, -2048 ; RV64I-NEXT: lui a0, 1 ; RV64I-NEXT: addiw a0, a0, -2048 ; RV64I-NEXT: add a0, sp, a0 ; RV64I-NEXT: call callee@plt -; RV64I-NEXT: lui a0, 1 -; RV64I-NEXT: sub sp, s0, a0 -; RV64I-NEXT: lui a0, 1 -; RV64I-NEXT: addiw a0, a0, -2032 -; RV64I-NEXT: add sp, sp, a0 +; RV64I-NEXT: addi sp, s0, -2048 +; RV64I-NEXT: addi sp, sp, -2048 +; RV64I-NEXT: addi sp, sp, 1032 +; RV64I-NEXT: addi sp, sp, 1032 ; RV64I-NEXT: ld ra, 2024(sp) # 8-byte Folded Reload ; RV64I-NEXT: ld s0, 2016(sp) # 8-byte Folded Reload ; RV64I-NEXT: addi sp, sp, 2032