summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwangpc <pc.wang@linux.alibaba.com>2022-01-11 18:19:05 +0800
committerwangpc <pc.wang@linux.alibaba.com>2022-01-11 18:20:37 +0800
commitc6430fade34449b5450848b25ebb5ee9596f7424 (patch)
tree976a083b8e2e79be3f48b7ad73f6e2f8a0e9fc3f
parent4d3ba4e6c8eacd39ba224b6ccb8934856ecb6264 (diff)
downloadllvm-c6430fade34449b5450848b25ebb5ee9596f7424.tar.gz
[RISCV] Generate 32 bits jumptable entries when code model is small
The code can only address the whole RV32 address space or the lower 2 GiB of the RV64 address space in small code model, so 32 bits entry is enough. Cache hit ratio and code size have some improvements. Reviewed By: asb Differential Revision: https://reviews.llvm.org/D116435
-rw-r--r--llvm/lib/Target/RISCV/RISCVISelLowering.cpp19
-rw-r--r--llvm/lib/Target/RISCV/RISCVISelLowering.h7
-rw-r--r--llvm/test/CodeGen/RISCV/jumptable.ll4
3 files changed, 28 insertions, 2 deletions
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index 5ad708be75be..fd295fedbb9e 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -24,6 +24,7 @@
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
+#include "llvm/CodeGen/MachineJumpTableInfo.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
#include "llvm/CodeGen/ValueTypes.h"
@@ -10127,6 +10128,24 @@ bool RISCVTargetLowering::shouldConvertFpToSat(unsigned Op, EVT FPVT,
}
}
+unsigned RISCVTargetLowering::getJumpTableEncoding() const {
+ // If we are using the small code model, we can reduce size of jump table
+ // entry to 4 bytes.
+ if (Subtarget.is64Bit() && !isPositionIndependent() &&
+ getTargetMachine().getCodeModel() == CodeModel::Small) {
+ return MachineJumpTableInfo::EK_Custom32;
+ }
+ return TargetLowering::getJumpTableEncoding();
+}
+
+const MCExpr *RISCVTargetLowering::LowerCustomJumpTableEntry(
+ const MachineJumpTableInfo *MJTI, const MachineBasicBlock *MBB,
+ unsigned uid, MCContext &Ctx) const {
+ assert(Subtarget.is64Bit() && !isPositionIndependent() &&
+ getTargetMachine().getCodeModel() == CodeModel::Small);
+ return MCSymbolRefExpr::create(MBB->getSymbol(), Ctx);
+}
+
bool RISCVTargetLowering::isFMAFasterThanFMulAndFAdd(const MachineFunction &MF,
EVT VT) const {
VT = VT.getScalarType();
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.h b/llvm/lib/Target/RISCV/RISCVISelLowering.h
index 23e5d89af081..4537dd7ba426 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.h
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.h
@@ -531,6 +531,13 @@ public:
SDValue BuildSDIVPow2(SDNode *N, const APInt &Divisor, SelectionDAG &DAG,
SmallVectorImpl<SDNode *> &Created) const override;
+ unsigned getJumpTableEncoding() const override;
+
+ const MCExpr *LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
+ const MachineBasicBlock *MBB,
+ unsigned uid,
+ MCContext &Ctx) const override;
+
private:
/// RISCVCCAssignFn - This target-specific function extends the default
/// CCValAssign with additional information used to lower RISC-V calling
diff --git a/llvm/test/CodeGen/RISCV/jumptable.ll b/llvm/test/CodeGen/RISCV/jumptable.ll
index 10a7938c652a..ab638aaefead 100644
--- a/llvm/test/CodeGen/RISCV/jumptable.ll
+++ b/llvm/test/CodeGen/RISCV/jumptable.ll
@@ -239,11 +239,11 @@ define void @above_threshold(i32 %in, i32* %out) nounwind {
; RV64I-SMALL-NEXT: li a2, 5
; RV64I-SMALL-NEXT: bltu a2, a0, .LBB1_9
; RV64I-SMALL-NEXT: # %bb.1: # %entry
-; RV64I-SMALL-NEXT: slli a0, a0, 3
+; RV64I-SMALL-NEXT: slli a0, a0, 2
; RV64I-SMALL-NEXT: lui a2, %hi(.LJTI1_0)
; RV64I-SMALL-NEXT: addi a2, a2, %lo(.LJTI1_0)
; RV64I-SMALL-NEXT: add a0, a0, a2
-; RV64I-SMALL-NEXT: ld a0, 0(a0)
+; RV64I-SMALL-NEXT: lw a0, 0(a0)
; RV64I-SMALL-NEXT: jr a0
; RV64I-SMALL-NEXT: .LBB1_2: # %bb1
; RV64I-SMALL-NEXT: li a0, 4