[clang] [llvm] [Clang][RISC-V] Fix big-endian 2*XLEN ABI register ordering (PR #243854)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 18 11:53:04 PST 2026
https://github.com/oeldar updated https://github.com/llvm/llvm-project/pull/243854
>From dd0e3e65b74ddd37d4f2655a13a8411bc3d4f056 Mon Sep 17 00:00:00 2001
From: Djordje Todorovic <djordje.todorovic at htecgroup.com>
Date: Thu, 5 Nov 2026 12:05:53 +0100
Subject: [PATCH] [RISCV] Fix big-endian 2*XLEN ABI register ordering
The RISC-V psABI requires named 2*XLEN scalars and return values to
use significance ordering regardless of endianness. The low-order
XLEN bits must be placed in the lower-numbered GPR, including when
the value is split between the last GPR and the stack.
Aggregates, floating-point values passed by the integer calling
convention, and variadic arguments continue to use memory-layout
ordering.
Clang previously coerced aligned 2*XLEN aggregates to bare integer
types, losing their aggregate identity. On big-endian targets,
coerce such values to single-element arrays instead, preserving both
their alignment and memory-layout ABI ordering.
Add tests for named scalars, aggregates, returns, variadic arguments,
register/stack splits, floating-point values, RV32 and RV64.
The relevant big-endian calling-convention rules were introduced by the
RISC-V ELF psABI change recently [1].
[1] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/470
Co-authored-by: Eldar Osmanovic <eldar.osmanovic at htecgroup.com>
---
clang/lib/CodeGen/Targets/RISCV.cpp | 14 ++-
clang/test/CodeGen/RISCV/riscv-be-abi.c | 42 ++++++++
llvm/lib/Target/RISCV/RISCVCallingConv.cpp | 57 +++++++++--
llvm/lib/Target/RISCV/RISCVISelLowering.cpp | 87 +++++++++++++++--
.../test/CodeGen/RISCV/bigendian-abi-2xlen.ll | 96 +++++++++++++++++++
.../RISCV/bigendian-double-bitmanip.ll | 4 +
llvm/test/CodeGen/RISCV/bigendian-f64-call.ll | 51 +++++++++-
.../CodeGen/RISCV/bigendian-load-store.ll | 21 ++--
8 files changed, 341 insertions(+), 31 deletions(-)
create mode 100644 clang/test/CodeGen/RISCV/riscv-be-abi.c
create mode 100644 llvm/test/CodeGen/RISCV/bigendian-abi-2xlen.ll
diff --git a/clang/lib/CodeGen/Targets/RISCV.cpp b/clang/lib/CodeGen/Targets/RISCV.cpp
index 96dad3d044c05f..a75584fe03656a 100644
--- a/clang/lib/CodeGen/Targets/RISCV.cpp
+++ b/clang/lib/CodeGen/Targets/RISCV.cpp
@@ -870,10 +870,16 @@ ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
return ABIArgInfo::getDirect(
llvm::IntegerType::get(getVMContext(), Size));
}
- // Use 2*XLen if 2*XLen alignment is required.
- if (Alignment == 2 * XLen)
- return ABIArgInfo::getDirect(
- llvm::IntegerType::get(getVMContext(), 2 * XLen));
+ // Use 2*XLen if 2*XLen alignment is required. On big-endian targets,
+ // preserve the fact that this is an aggregate so the backend can apply
+ // memory-layout ordering rather than the significance ordering used for
+ // 2*XLen scalars.
+ if (Alignment == 2 * XLen) {
+ llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), 2 * XLen);
+ if (getDataLayout().isBigEndian())
+ CoerceTy = llvm::ArrayType::get(CoerceTy, 1);
+ return ABIArgInfo::getDirect(CoerceTy);
+ }
// Use 2-element XLen array if only XLen alignment is required.
return ABIArgInfo::getDirect(
llvm::ArrayType::get(llvm::IntegerType::get(getVMContext(), XLen), 2));
diff --git a/clang/test/CodeGen/RISCV/riscv-be-abi.c b/clang/test/CodeGen/RISCV/riscv-be-abi.c
new file mode 100644
index 00000000000000..8d82806d345eb4
--- /dev/null
+++ b/clang/test/CodeGen/RISCV/riscv-be-abi.c
@@ -0,0 +1,42 @@
+// RUN: %clang_cc1 -triple riscv32be -target-abi ilp32 -emit-llvm %s -o - \
+// RUN: | FileCheck %s --check-prefix=RV32BE
+// RUN: %clang_cc1 -triple riscv32 -target-abi ilp32 -emit-llvm %s -o - \
+// RUN: | FileCheck %s --check-prefix=RV32LE
+// RUN: %clang_cc1 -triple riscv64be -target-abi lp64 -emit-llvm %s -o - \
+// RUN: | FileCheck %s --check-prefix=RV64BE
+// RUN: %clang_cc1 -triple riscv64 -target-abi lp64 -emit-llvm %s -o - \
+// RUN: | FileCheck %s --check-prefix=RV64LE
+
+typedef unsigned long long u64;
+
+struct aligned_2xlen_rv32 {
+ u64 value;
+};
+
+// Preserve aggregate identity on big-endian targets. A bare i64 would be
+// indistinguishable from a 2*XLen scalar in the backend, even though aggregates
+// use memory-layout ordering while named scalars use significance ordering.
+
+// RV32BE-LABEL: define dso_local [1 x i64] @pass_rv32(
+// RV32BE-SAME: [1 x i64] [[X:%.*]])
+// RV32LE-LABEL: define dso_local i64 @pass_rv32(
+// RV32LE-SAME: i64 [[X:%.*]])
+struct aligned_2xlen_rv32 pass_rv32(struct aligned_2xlen_rv32 x) {
+ return x;
+}
+
+#if __riscv_xlen == 64
+typedef unsigned __int128 u128;
+
+struct aligned_2xlen_rv64 {
+ u128 value;
+};
+
+// RV64BE-LABEL: define dso_local [1 x i128] @pass_rv64(
+// RV64BE-SAME: [1 x i128] [[X:%.*]])
+// RV64LE-LABEL: define dso_local i128 @pass_rv64(
+// RV64LE-SAME: i128 [[X:%.*]])
+struct aligned_2xlen_rv64 pass_rv64(struct aligned_2xlen_rv64 x) {
+ return x;
+}
+#endif
diff --git a/llvm/lib/Target/RISCV/RISCVCallingConv.cpp b/llvm/lib/Target/RISCV/RISCVCallingConv.cpp
index fd15b58a2387b1..d259ea975043a9 100644
--- a/llvm/lib/Target/RISCV/RISCVCallingConv.cpp
+++ b/llvm/lib/Target/RISCV/RISCVCallingConv.cpp
@@ -294,23 +294,33 @@ static ArrayRef<MCPhysReg> getFastCCArgGPRF32s(const RISCVABI::ABI ABI) {
return ArrayRef(FastCCIGPRs);
}
+static bool is2XLenScalar(Type *OrigTy, const DataLayout &DL, unsigned XLen) {
+ if (!OrigTy)
+ return false;
+
+ // Floating-point reals use aggregate-style, memory-layout ordering under
+ // the base integer calling convention.
+ if (!OrigTy->isIntegerTy() && !OrigTy->isPointerTy())
+ return false;
+
+ return DL.getTypeAllocSize(OrigTy) == (2 * XLen) / 8;
+}
+
// Pass a 2*XLEN argument that has been split into two XLEN values through
// registers or the stack as necessary.
static bool CC_RISCVAssign2XLen(CCState &State, CCValAssign VA1,
ISD::ArgFlagsTy ArgFlags1, unsigned ValNo2,
MVT ValVT2, MVT LocVT2,
ISD::ArgFlagsTy ArgFlags2,
- const RISCVSubtarget &Subtarget) {
+ const RISCVSubtarget &Subtarget,
+ bool UseSignificanceOrder) {
unsigned XLen = Subtarget.getXLen();
unsigned XLenInBytes = XLen / 8;
ArrayRef<MCPhysReg> ArgGPRs = RISCV::getArgGPRs(Subtarget);
- if (MCRegister Reg = State.AllocateReg(ArgGPRs)) {
- // At least one half can be passed via register.
- State.addLoc(CCValAssign::getReg(VA1.getValNo(), VA1.getValVT(), Reg,
- VA1.getLocVT(), CCValAssign::Full));
- } else {
+ MCRegister Reg = State.AllocateReg(ArgGPRs);
+ if (!Reg) {
// Both halves must be passed on the stack, with proper alignment.
// TODO: To be compatible with GCC's behaviors, we clamp the alignment by
// the ABI stack alignment. This behavior may be changed when RV32E/ILP32E
@@ -328,10 +338,36 @@ static bool CC_RISCVAssign2XLen(CCState &State, CCValAssign VA1,
return false;
}
- if (MCRegister Reg = State.AllocateReg(ArgGPRs)) {
+ MCRegister Reg2 = State.AllocateReg(ArgGPRs);
+ if (UseSignificanceOrder) {
+ // On big-endian targets, legal splitting presents the high-order XLEN bits
+ // first. Named scalar arguments and return values still use
+ // significance-based ordering, so the low-order XLEN bits go in the
+ // lower-numbered register when one is available.
+ if (Reg2) {
+ State.addLoc(CCValAssign::getReg(VA1.getValNo(), VA1.getValVT(), Reg2,
+ VA1.getLocVT(), CCValAssign::Full));
+ State.addLoc(
+ CCValAssign::getReg(ValNo2, ValVT2, Reg, LocVT2, CCValAssign::Full));
+ } else {
+ State.addLoc(CCValAssign::getMem(
+ VA1.getValNo(), VA1.getValVT(),
+ State.AllocateStack(XLenInBytes, Align(XLenInBytes)), VA1.getLocVT(),
+ CCValAssign::Full));
+ State.addLoc(
+ CCValAssign::getReg(ValNo2, ValVT2, Reg, LocVT2, CCValAssign::Full));
+ }
+ return false;
+ }
+
+ // At least one half can be passed via register.
+ State.addLoc(CCValAssign::getReg(VA1.getValNo(), VA1.getValVT(), Reg,
+ VA1.getLocVT(), CCValAssign::Full));
+
+ if (Reg2) {
// The second half can also be passed via register.
State.addLoc(
- CCValAssign::getReg(ValNo2, ValVT2, Reg, LocVT2, CCValAssign::Full));
+ CCValAssign::getReg(ValNo2, ValVT2, Reg2, LocVT2, CCValAssign::Full));
} else {
// The second half is passed via the stack, without additional alignment.
State.addLoc(CCValAssign::getMem(
@@ -585,8 +621,11 @@ static bool CC_RISCV_Impl(unsigned ValNo, MVT ValVT, MVT LocVT,
ISD::ArgFlagsTy AF = PendingArgFlags[0];
PendingLocs.clear();
PendingArgFlags.clear();
+ bool UseSignificanceOrder = !Subtarget.isLittleEndian() &&
+ !ArgFlags.isVarArg() &&
+ is2XLenScalar(OrigTy, DL, XLen);
return CC_RISCVAssign2XLen(State, VA, AF, ValNo, ValVT, LocVT, ArgFlags,
- Subtarget);
+ Subtarget, UseSignificanceOrder);
}
// Split arguments might be passed indirectly, so keep track of the pending
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index b80d509c043c13..dd7ca102e51a88 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -18094,9 +18094,8 @@ void RISCVTargetLowering::ReplaceNodeResults(SDNode *N,
DAG.getVTList(MVT::i32, MVT::i32), Op0);
SDValue Lo = NewReg.getValue(0);
SDValue Hi = NewReg.getValue(1);
- // For big-endian, swap the order when building the i64 pair.
- if (!Subtarget.isLittleEndian())
- std::swap(Lo, Hi);
+ // SplitF64 produces significance-ordered halves regardless of
+ // endianness, matching BUILD_PAIR's operand order.
SDValue RetReg = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
Results.push_back(RetReg);
} else if (!VT.isVector() && Op0VT.isFixedLengthVector() &&
@@ -28904,6 +28903,51 @@ void RISCVTargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
MachineOperand::CreateReg(RISCV::FRM, /*isDef*/ false, /*isImp*/ true));
}
+static bool is2XLenScalarType(const Type *Ty, const DataLayout &DL,
+ unsigned XLen) {
+ if (!Ty)
+ return false;
+
+ // Floating-point reals use aggregate-style, memory-layout ordering under
+ // the base integer calling convention.
+ if (!Ty->isIntegerTy() && !Ty->isPointerTy())
+ return false;
+
+ return DL.getTypeAllocSize(const_cast<Type *>(Ty)) == (2 * XLen) / 8;
+}
+
+template <typename ArgT>
+static void setRetOrigTy(SmallVectorImpl<ArgT> &Args, const Type *RetTy) {
+ if (!RetTy)
+ return;
+
+ Type *NonConstRetTy = const_cast<Type *>(RetTy);
+ for (ArgT &Arg : Args)
+ if (!Arg.Flags.isSwiftError())
+ Arg.OrigTy = NonConstRetTy;
+}
+
+template <typename ArgT>
+static void mark2XLenScalarRetSplit(SmallVectorImpl<ArgT> &Args,
+ const Type *RetTy, const DataLayout &DL,
+ unsigned XLen) {
+ unsigned XLenInBytes = XLen / 8;
+ unsigned TwoXLenInBytes = (2 * XLen) / 8;
+
+ if (!is2XLenScalarType(RetTy, DL, XLen) || Args.size() < 2)
+ return;
+
+ if (Args[0].VT.getStoreSize() != XLenInBytes ||
+ Args[1].VT.getStoreSize() != XLenInBytes ||
+ Args[0].ArgVT.getStoreSize() != TwoXLenInBytes ||
+ Args[1].ArgVT.getStoreSize() != TwoXLenInBytes)
+ return;
+
+ Args[0].Flags.setSplit();
+ Args[1].Flags.setOrigAlign(Align(1));
+ Args[1].Flags.setSplitEnd();
+}
+
// Convert Val to a ValVT. Should not be called for CCValAssign::Indirect
// values.
static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDValue Val,
@@ -29182,7 +29226,11 @@ SDValue RISCVTargetLowering::LowerFormalArguments(
SmallVector<CCValAssign, 16> ArgLocs;
CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
- CCInfo.AnalyzeFormalArguments(Ins, CC_RISCV);
+ SmallVector<ISD::InputArg, 16> CCIns(Ins.begin(), Ins.end());
+ for (ISD::InputArg &In : CCIns)
+ if (In.isOrigArg())
+ In.OrigTy = Func.getArg(In.getOrigArgIndex())->getType();
+ CCInfo.AnalyzeFormalArguments(CCIns, CC_RISCV);
for (unsigned i = 0, e = ArgLocs.size(), InsIdx = 0; i != e; ++i, ++InsIdx) {
CCValAssign &VA = ArgLocs[i];
@@ -29432,7 +29480,11 @@ SDValue RISCVTargetLowering::LowerCall(CallLoweringInfo &CLI,
break;
}
- ArgCCInfo.AnalyzeCallOperands(Outs, CC_RISCV);
+ SmallVector<ISD::OutputArg, 16> CCOuts(Outs.begin(), Outs.end());
+ for (ISD::OutputArg &Out : CCOuts)
+ if (Out.OrigArgIndex < CLI.getArgs().size())
+ Out.OrigTy = CLI.getArgs()[Out.OrigArgIndex].OrigTy;
+ ArgCCInfo.AnalyzeCallOperands(CCOuts, CC_RISCV);
// Check if it's really possible to do a tail call.
if (IsTailCall)
@@ -29870,7 +29922,13 @@ SDValue RISCVTargetLowering::LowerCall(CallLoweringInfo &CLI,
// Assign locations to each value returned by this call.
SmallVector<CCValAssign, 16> RVLocs;
CCState RetCCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
- RetCCInfo.AnalyzeFormalArguments(Ins, RetCC_RISCV);
+ SmallVector<ISD::InputArg, 4> RetIns(Ins.begin(), Ins.end());
+ if (!Subtarget.isLittleEndian()) {
+ setRetOrigTy(RetIns, CLI.OrigRetTy);
+ mark2XLenScalarRetSplit(RetIns, CLI.OrigRetTy, DAG.getDataLayout(),
+ Subtarget.getXLen());
+ }
+ RetCCInfo.AnalyzeFormalArguments(RetIns, RetCC_RISCV);
// Copy all of the result registers out of their specified physreg.
for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
@@ -29921,7 +29979,13 @@ bool RISCVTargetLowering::CanLowerReturn(
const Type *RetTy) const {
SmallVector<CCValAssign, 16> RVLocs;
CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
- return CCInfo.CheckReturn(Outs, RetCC_RISCV);
+ SmallVector<ISD::OutputArg, 4> RetOuts(Outs.begin(), Outs.end());
+ if (!Subtarget.isLittleEndian()) {
+ setRetOrigTy(RetOuts, RetTy);
+ mark2XLenScalarRetSplit(RetOuts, RetTy, MF.getDataLayout(),
+ Subtarget.getXLen());
+ }
+ return CCInfo.CheckReturn(RetOuts, RetCC_RISCV);
}
SDValue
@@ -29938,8 +30002,15 @@ RISCVTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
// Info about the registers and stack slot.
CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
*DAG.getContext());
+ SmallVector<ISD::OutputArg, 4> RetOuts(Outs.begin(), Outs.end());
+ if (!Subtarget.isLittleEndian()) {
+ const Function &Func = DAG.getMachineFunction().getFunction();
+ setRetOrigTy(RetOuts, Func.getReturnType());
+ mark2XLenScalarRetSplit(RetOuts, Func.getReturnType(), DAG.getDataLayout(),
+ Subtarget.getXLen());
+ }
- CCInfo.AnalyzeCallOperands(Outs, RetCC_RISCV);
+ CCInfo.AnalyzeCallOperands(RetOuts, RetCC_RISCV);
if (CallConv == CallingConv::GHC && !RVLocs.empty())
reportFatalUsageError("GHC functions return void only");
diff --git a/llvm/test/CodeGen/RISCV/bigendian-abi-2xlen.ll b/llvm/test/CodeGen/RISCV/bigendian-abi-2xlen.ll
new file mode 100644
index 00000000000000..91acaed763f975
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/bigendian-abi-2xlen.ll
@@ -0,0 +1,96 @@
+; RUN: llc -mtriple=riscv32be -target-abi=ilp32 -verify-machineinstrs < %s | FileCheck %s
+
+target triple = "riscv32-unknown-unknown"
+
+declare void @consume_named_many(i32, i32, i32, i32, i32, i32, i32, i64)
+declare void @consume_aggregate_only({ i64 })
+declare void @consume_var(ptr, ...)
+
+define void @consume_named_i64(i64 %x, ptr %lo_p, ptr %hi_p) nounwind {
+; CHECK-LABEL: consume_named_i64:
+; CHECK: # %bb.0:
+; CHECK-NEXT: sw a0, 0(a2)
+; CHECK-NEXT: sw a1, 0(a3)
+; CHECK-NEXT: ret
+ %lo = trunc i64 %x to i32
+ %hi64 = lshr i64 %x, 32
+ %hi = trunc i64 %hi64 to i32
+ store i32 %lo, ptr %lo_p
+ store i32 %hi, ptr %hi_p
+ ret void
+}
+
+define i64 @return_i64_one() nounwind {
+; CHECK-LABEL: return_i64_one:
+; CHECK: # %bb.0:
+; CHECK-NEXT: li a0, 1
+; CHECK-NEXT: li a1, 0
+; CHECK-NEXT: ret
+ ret i64 1
+}
+
+define void @consume_aggregate_i64({ i64 } %x, ptr %lo_p, ptr %hi_p) nounwind {
+; CHECK-LABEL: consume_aggregate_i64:
+; CHECK: # %bb.0:
+; CHECK-NEXT: sw a1, 0(a2)
+; CHECK-NEXT: sw a0, 0(a3)
+; CHECK-NEXT: ret
+ %v = extractvalue { i64 } %x, 0
+ %lo = trunc i64 %v to i32
+ %hi64 = lshr i64 %v, 32
+ %hi = trunc i64 %hi64 to i32
+ store i32 %lo, ptr %lo_p
+ store i32 %hi, ptr %hi_p
+ ret void
+}
+
+define { i64 } @return_aggregate_i64_one() nounwind {
+; CHECK-LABEL: return_aggregate_i64_one:
+; CHECK: # %bb.0:
+; CHECK-NEXT: li a1, 1
+; CHECK-NEXT: li a0, 0
+; CHECK-NEXT: ret
+ ret { i64 } { i64 1 }
+}
+
+define i32 @call_aggregate_i64() nounwind {
+; CHECK-LABEL: call_aggregate_i64:
+; CHECK: # %bb.0:
+; CHECK: li a0, 2
+; CHECK-NEXT: li a1, 1
+; CHECK-NEXT: call consume_aggregate_only
+ call void @consume_aggregate_only({ i64 } { i64 8589934593 })
+ ret i32 0
+}
+
+define i32 @use_ret_i64() nounwind {
+; CHECK-LABEL: use_ret_i64:
+; CHECK: # %bb.0:
+; CHECK: call return_i64_one
+; CHECK-NOT: mv a0, a1
+; CHECK: ret
+ %x = call i64 @return_i64_one()
+ %lo = trunc i64 %x to i32
+ ret i32 %lo
+}
+
+define void @call_named_i64_one_reg() nounwind {
+; CHECK-LABEL: call_named_i64_one_reg:
+; CHECK: # %bb.0:
+; CHECK: li [[HI:[a-z][0-9]+]], 2
+; CHECK-NEXT: li a7, 1
+; CHECK: sw [[HI]], 0(sp)
+; CHECK: call consume_named_many
+ call void @consume_named_many(i32 0, i32 0, i32 0, i32 0, i32 0, i32 0,
+ i32 0, i64 8589934593)
+ ret void
+}
+
+define void @call_var_i64(ptr %fmt, i64 %x) nounwind {
+; CHECK-LABEL: call_var_i64:
+; CHECK: # %bb.0:
+; CHECK: mv a3, a1
+; CHECK-NEXT: call consume_var
+ call void (ptr, ...) @consume_var(ptr %fmt, i64 %x)
+ ret void
+}
diff --git a/llvm/test/CodeGen/RISCV/bigendian-double-bitmanip.ll b/llvm/test/CodeGen/RISCV/bigendian-double-bitmanip.ll
index c85fd5d4c55da2..0b2a0fa04ab454 100644
--- a/llvm/test/CodeGen/RISCV/bigendian-double-bitmanip.ll
+++ b/llvm/test/CodeGen/RISCV/bigendian-double-bitmanip.ll
@@ -6,6 +6,10 @@
; but soft-float ABI. This configuration triggers the special handling for
; big-endian.
+; Under the base integer calling convention, floating-point reals use
+; aggregate-style memory-layout ordering. Thus, on RV32BE, an f64 value has
+; its high word in the lower-numbered GPR and its low word in the next GPR.
+
define double @fneg(double %a) nounwind {
; RV32IFD-LE-LABEL: fneg:
; RV32IFD-LE: # %bb.0:
diff --git a/llvm/test/CodeGen/RISCV/bigendian-f64-call.ll b/llvm/test/CodeGen/RISCV/bigendian-f64-call.ll
index 83057e23a0d855..085472dad79d3f 100644
--- a/llvm/test/CodeGen/RISCV/bigendian-f64-call.ll
+++ b/llvm/test/CodeGen/RISCV/bigendian-f64-call.ll
@@ -2,8 +2,9 @@
; RUN: llc -mtriple=riscv32 -target-abi=ilp32 -mattr=+d -verify-machineinstrs < %s | FileCheck -check-prefix=RV32LE %s
; RUN: llc -mtriple=riscv32be -target-abi=ilp32 -mattr=+d -verify-machineinstrs < %s | FileCheck -check-prefix=RV32BE %s
-; Test f64 function calls with D extension and soft-float ABI
-; This specifically tests the LowerCall path that needs to swap Lo/Hi for BE
+; Test f64 calls with the D extension available but using the base integer ABI.
+; The fadd case forces GPR-to-FPR and FPR-to-GPR conversion, making RV32BE
+; memory-layout observable.
declare double @external_func(double, double)
@@ -92,3 +93,49 @@ define double @test_f64_call_with_fadd(double %a, double %b) {
%result = call double @external_func(double %sum, double %sum)
ret double %result
}
+
+; Floating-point values use memory-layout ordering under the base integer
+; calling convention. On RV32BE, the high word is therefore in the
+; lower-numbered GPR.
+define i32 @bitcast_f64_low(double %x) {
+; RV32BE-LABEL: bitcast_f64_low:
+; RV32BE: # %bb.0:
+; RV32BE-NEXT: mv a0, a1
+; RV32BE-NEXT: ret
+ %bits = bitcast double %x to i64
+ %low = trunc i64 %bits to i32
+ ret i32 %low
+}
+
+define i32 @bitcast_f64_high(double %x) {
+; RV32BE-LABEL: bitcast_f64_high:
+; RV32BE: # %bb.0:
+; RV32BE-NEXT: ret
+ %bits = bitcast double %x to i64
+ %shift = lshr i64 %bits, 32
+ %high = trunc i64 %shift to i32
+ ret i32 %high
+}
+
+define i32 @bitcast_split_f64_low(i32 %a0, i32 %a1, i32 %a2, i32 %a3,
+ i32 %a4, i32 %a5, i32 %a6, double %x) {
+; RV32BE-LABEL: bitcast_split_f64_low:
+; RV32BE: # %bb.0:
+; RV32BE-NEXT: lw a0, 0(sp)
+; RV32BE-NEXT: ret
+ %bits = bitcast double %x to i64
+ %low = trunc i64 %bits to i32
+ ret i32 %low
+}
+
+define i32 @bitcast_split_f64_high(i32 %a0, i32 %a1, i32 %a2, i32 %a3,
+ i32 %a4, i32 %a5, i32 %a6, double %x) {
+; RV32BE-LABEL: bitcast_split_f64_high:
+; RV32BE: # %bb.0:
+; RV32BE-NEXT: mv a0, a7
+; RV32BE-NEXT: ret
+ %bits = bitcast double %x to i64
+ %shift = lshr i64 %bits, 32
+ %high = trunc i64 %shift to i32
+ ret i32 %high
+}
diff --git a/llvm/test/CodeGen/RISCV/bigendian-load-store.ll b/llvm/test/CodeGen/RISCV/bigendian-load-store.ll
index 175346d5ab0f55..ed70eff03639a3 100644
--- a/llvm/test/CodeGen/RISCV/bigendian-load-store.ll
+++ b/llvm/test/CodeGen/RISCV/bigendian-load-store.ll
@@ -160,9 +160,8 @@ define i64 @load_i64(ptr %p) {
;
; RV32BE-LABEL: load_i64:
; RV32BE: # %bb.0:
-; RV32BE-NEXT: lw a2, 0(a0)
-; RV32BE-NEXT: lw a1, 4(a0)
-; RV32BE-NEXT: mv a0, a2
+; RV32BE-NEXT: lw a1, 0(a0)
+; RV32BE-NEXT: lw a0, 4(a0)
; RV32BE-NEXT: ret
;
; RV64LE-LABEL: load_i64:
@@ -187,8 +186,8 @@ define void @store_i64(ptr %p, i64 %v) {
;
; RV32BE-LABEL: store_i64:
; RV32BE: # %bb.0:
-; RV32BE-NEXT: sw a1, 0(a0)
-; RV32BE-NEXT: sw a2, 4(a0)
+; RV32BE-NEXT: sw a2, 0(a0)
+; RV32BE-NEXT: sw a1, 4(a0)
; RV32BE-NEXT: ret
;
; RV64LE-LABEL: store_i64:
@@ -374,6 +373,9 @@ define i64 @bitcast_f64_to_i64(double %x) {
;
; RV32BE-LABEL: bitcast_f64_to_i64:
; RV32BE: # %bb.0:
+; RV32BE-NEXT: mv a2, a1
+; RV32BE-NEXT: mv a1, a0
+; RV32BE-NEXT: mv a0, a2
; RV32BE-NEXT: ret
;
; RV64LE-LABEL: bitcast_f64_to_i64:
@@ -395,6 +397,9 @@ define double @bitcast_i64_to_f64(i64 %x) {
;
; RV32BE-LABEL: bitcast_i64_to_f64:
; RV32BE: # %bb.0:
+; RV32BE-NEXT: mv a2, a0
+; RV32BE-NEXT: mv a0, a1
+; RV32BE-NEXT: mv a1, a2
; RV32BE-NEXT: ret
;
; RV64LE-LABEL: bitcast_i64_to_f64:
@@ -408,7 +413,7 @@ define double @bitcast_i64_to_f64(i64 %x) {
ret double %y
}
-; Test i64 return value register order (a0=low/a1=high for LE, a0=high/a1=low for BE)
+; Test i64 return value register order (a0=low/a1=high for both LE and BE)
define i64 @return_i64_const() {
; RV32LE-LABEL: return_i64_const:
; RV32LE: # %bb.0:
@@ -418,8 +423,8 @@ define i64 @return_i64_const() {
;
; RV32BE-LABEL: return_i64_const:
; RV32BE: # %bb.0:
-; RV32BE-NEXT: li a1, 1
-; RV32BE-NEXT: li a0, 0
+; RV32BE-NEXT: li a0, 1
+; RV32BE-NEXT: li a1, 0
; RV32BE-NEXT: ret
;
; RV64LE-LABEL: return_i64_const:
More information about the cfe-commits
mailing list