Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,9 @@ RISCVLegalizerInfo::RISCVLegalizerInfo(const RISCVSubtarget &ST)
.legalFor(ST.hasStdExtF(), {s32})
.legalFor(ST.hasStdExtD(), {s64})
.legalFor(ST.hasStdExtZfh(), {s16})
.lowerFor({s32, s64, s128});
.customFor(!ST.is64Bit(), {s32})
.customFor(ST.is64Bit(), {s32, s64})
.lowerFor({s64, s128});

getActionDefinitionsBuilder({G_FPTOSI, G_FPTOUI})
.legalFor(ST.hasStdExtF(), {{sXLen, s32}})
Expand Down Expand Up @@ -869,6 +871,17 @@ bool RISCVLegalizerInfo::shouldBeInConstantPool(const APInt &APImm,
return !(!SeqLo.empty() && (SeqLo.size() + 2) <= STI.getMaxBuildIntsCost());
}

bool RISCVLegalizerInfo::shouldBeInFConstantPool(const APFloat &APF) const {
[[maybe_unused]] unsigned Size = APF.getSizeInBits(APF.getSemantics());
assert((Size == 32 || Size == 64) && "Only support f32 and f64");

int64_t Imm = APF.bitcastToAPInt().getSExtValue();
RISCVMatInt::InstSeq Seq = RISCVMatInt::generateInstSeq(Imm, STI);
if (Seq.size() <= STI.getMaxBuildIntsCost())
return false;
return true;
}

bool RISCVLegalizerInfo::legalizeVScale(MachineInstr &MI,
MachineIRBuilder &MIB) const {
const LLT XLenTy(STI.getXLenVT());
Expand Down Expand Up @@ -1358,7 +1371,18 @@ bool RISCVLegalizerInfo::legalizeCustom(
return false;
case TargetOpcode::G_ABS:
return Helper.lowerAbsToMaxNeg(MI);
// TODO: G_FCONSTANT
case TargetOpcode::G_FCONSTANT: {
const APFloat FVal = MI.getOperand(1).getFPImm()->getValueAPF();
if (shouldBeInFConstantPool(FVal))
return Helper.lowerFConstant(MI);

// Convert G_FCONSTANT to G_CONSTANT.
Register DstReg = MI.getOperand(0).getReg();
MIRBuilder.buildConstant(DstReg, FVal.bitcastToAPInt());

MI.eraseFromParent();
return true;
}
case TargetOpcode::G_CONSTANT: {
const Function &F = MF.getFunction();
// TODO: if PSI and BFI are present, add " ||
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class RISCVLegalizerInfo : public LegalizerInfo {

private:
bool shouldBeInConstantPool(const APInt &APImm, bool ShouldOptForSize) const;
bool shouldBeInFConstantPool(const APFloat &APImm) const;
bool legalizeShlAshrLshr(MachineInstr &MI, MachineIRBuilder &MIRBuilder,
GISelChangeObserver &Observer) const;

Expand Down
144 changes: 116 additions & 28 deletions llvm/test/CodeGen/RISCV/GlobalISel/constantpool.ll
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,37 @@
define void @constpool_f32(ptr %p) {
; RV32-SMALL-LABEL: constpool_f32:
; RV32-SMALL: # %bb.0:
; RV32-SMALL-NEXT: lui a1, %hi(.LCPI0_0)
; RV32-SMALL-NEXT: lw a1, %lo(.LCPI0_0)(a1)
; RV32-SMALL-NEXT: lui a1, 260096
; RV32-SMALL-NEXT: sw a1, 0(a0)
; RV32-SMALL-NEXT: ret
;
; RV32-MEDIUM-LABEL: constpool_f32:
; RV32-MEDIUM: # %bb.0:
; RV32-MEDIUM-NEXT: .Lpcrel_hi0:
; RV32-MEDIUM-NEXT: auipc a1, %pcrel_hi(.LCPI0_0)
; RV32-MEDIUM-NEXT: lw a1, %pcrel_lo(.Lpcrel_hi0)(a1)
; RV32-MEDIUM-NEXT: lui a1, 260096
; RV32-MEDIUM-NEXT: sw a1, 0(a0)
; RV32-MEDIUM-NEXT: ret
;
; RV32-PIC-LABEL: constpool_f32:
; RV32-PIC: # %bb.0:
; RV32-PIC-NEXT: .Lpcrel_hi0:
; RV32-PIC-NEXT: auipc a1, %pcrel_hi(.LCPI0_0)
; RV32-PIC-NEXT: lw a1, %pcrel_lo(.Lpcrel_hi0)(a1)
; RV32-PIC-NEXT: lui a1, 260096
; RV32-PIC-NEXT: sw a1, 0(a0)
; RV32-PIC-NEXT: ret
;
; RV64-SMALL-LABEL: constpool_f32:
; RV64-SMALL: # %bb.0:
; RV64-SMALL-NEXT: lui a1, %hi(.LCPI0_0)
; RV64-SMALL-NEXT: lw a1, %lo(.LCPI0_0)(a1)
; RV64-SMALL-NEXT: lui a1, 260096
; RV64-SMALL-NEXT: sw a1, 0(a0)
; RV64-SMALL-NEXT: ret
;
; RV64-MEDIUM-LABEL: constpool_f32:
; RV64-MEDIUM: # %bb.0:
; RV64-MEDIUM-NEXT: .Lpcrel_hi0:
; RV64-MEDIUM-NEXT: auipc a1, %pcrel_hi(.LCPI0_0)
; RV64-MEDIUM-NEXT: lw a1, %pcrel_lo(.Lpcrel_hi0)(a1)
; RV64-MEDIUM-NEXT: lui a1, 260096
; RV64-MEDIUM-NEXT: sw a1, 0(a0)
; RV64-MEDIUM-NEXT: ret
;
; RV64-PIC-LABEL: constpool_f32:
; RV64-PIC: # %bb.0:
; RV64-PIC-NEXT: .Lpcrel_hi0:
; RV64-PIC-NEXT: auipc a1, %pcrel_hi(.LCPI0_0)
; RV64-PIC-NEXT: lw a1, %pcrel_lo(.Lpcrel_hi0)(a1)
; RV64-PIC-NEXT: lui a1, 260096
; RV64-PIC-NEXT: sw a1, 0(a0)
; RV64-PIC-NEXT: ret
store float 1.0, ptr %p
Expand All @@ -75,9 +65,9 @@ define void @constpool_f64(ptr %p) {
;
; RV32-MEDIUM-LABEL: constpool_f64:
; RV32-MEDIUM: # %bb.0:
; RV32-MEDIUM-NEXT: .Lpcrel_hi1:
; RV32-MEDIUM-NEXT: .Lpcrel_hi0:
; RV32-MEDIUM-NEXT: auipc a1, %pcrel_hi(.LCPI1_0)
; RV32-MEDIUM-NEXT: addi a1, a1, %pcrel_lo(.Lpcrel_hi1)
; RV32-MEDIUM-NEXT: addi a1, a1, %pcrel_lo(.Lpcrel_hi0)
; RV32-MEDIUM-NEXT: lw a2, 0(a1)
; RV32-MEDIUM-NEXT: lw a1, 4(a1)
; RV32-MEDIUM-NEXT: sw a2, 0(a0)
Expand All @@ -86,9 +76,9 @@ define void @constpool_f64(ptr %p) {
;
; RV32-PIC-LABEL: constpool_f64:
; RV32-PIC: # %bb.0:
; RV32-PIC-NEXT: .Lpcrel_hi1:
; RV32-PIC-NEXT: .Lpcrel_hi0:
; RV32-PIC-NEXT: auipc a1, %pcrel_hi(.LCPI1_0)
; RV32-PIC-NEXT: addi a1, a1, %pcrel_lo(.Lpcrel_hi1)
; RV32-PIC-NEXT: addi a1, a1, %pcrel_lo(.Lpcrel_hi0)
; RV32-PIC-NEXT: lw a2, 0(a1)
; RV32-PIC-NEXT: lw a1, 4(a1)
; RV32-PIC-NEXT: sw a2, 0(a0)
Expand All @@ -97,26 +87,124 @@ define void @constpool_f64(ptr %p) {
;
; RV64-SMALL-LABEL: constpool_f64:
; RV64-SMALL: # %bb.0:
; RV64-SMALL-NEXT: lui a1, %hi(.LCPI1_0)
; RV64-SMALL-NEXT: ld a1, %lo(.LCPI1_0)(a1)
; RV64-SMALL-NEXT: li a1, 1023
; RV64-SMALL-NEXT: slli a1, a1, 52
; RV64-SMALL-NEXT: sd a1, 0(a0)
; RV64-SMALL-NEXT: ret
;
; RV64-MEDIUM-LABEL: constpool_f64:
; RV64-MEDIUM: # %bb.0:
; RV64-MEDIUM-NEXT: .Lpcrel_hi1:
; RV64-MEDIUM-NEXT: auipc a1, %pcrel_hi(.LCPI1_0)
; RV64-MEDIUM-NEXT: ld a1, %pcrel_lo(.Lpcrel_hi1)(a1)
; RV64-MEDIUM-NEXT: li a1, 1023
; RV64-MEDIUM-NEXT: slli a1, a1, 52
; RV64-MEDIUM-NEXT: sd a1, 0(a0)
; RV64-MEDIUM-NEXT: ret
;
; RV64-PIC-LABEL: constpool_f64:
; RV64-PIC: # %bb.0:
; RV64-PIC-NEXT: .Lpcrel_hi1:
; RV64-PIC-NEXT: auipc a1, %pcrel_hi(.LCPI1_0)
; RV64-PIC-NEXT: ld a1, %pcrel_lo(.Lpcrel_hi1)(a1)
; RV64-PIC-NEXT: li a1, 1023
; RV64-PIC-NEXT: slli a1, a1, 52
; RV64-PIC-NEXT: sd a1, 0(a0)
; RV64-PIC-NEXT: ret
store double 1.0, ptr %p
ret void
}

define void @constpool_f32_1234_5(ptr %p) {
; RV32-SMALL-LABEL: constpool_f32_1234_5:
; RV32-SMALL: # %bb.0:
; RV32-SMALL-NEXT: lui a1, 280997
; RV32-SMALL-NEXT: sw a1, 0(a0)
; RV32-SMALL-NEXT: ret
;
; RV32-MEDIUM-LABEL: constpool_f32_1234_5:
; RV32-MEDIUM: # %bb.0:
; RV32-MEDIUM-NEXT: lui a1, 280997
; RV32-MEDIUM-NEXT: sw a1, 0(a0)
; RV32-MEDIUM-NEXT: ret
;
; RV32-PIC-LABEL: constpool_f32_1234_5:
; RV32-PIC: # %bb.0:
; RV32-PIC-NEXT: lui a1, 280997
; RV32-PIC-NEXT: sw a1, 0(a0)
; RV32-PIC-NEXT: ret
;
; RV64-SMALL-LABEL: constpool_f32_1234_5:
; RV64-SMALL: # %bb.0:
; RV64-SMALL-NEXT: lui a1, 280997
; RV64-SMALL-NEXT: sw a1, 0(a0)
; RV64-SMALL-NEXT: ret
;
; RV64-MEDIUM-LABEL: constpool_f32_1234_5:
; RV64-MEDIUM: # %bb.0:
; RV64-MEDIUM-NEXT: lui a1, 280997
; RV64-MEDIUM-NEXT: sw a1, 0(a0)
; RV64-MEDIUM-NEXT: ret
;
; RV64-PIC-LABEL: constpool_f32_1234_5:
; RV64-PIC: # %bb.0:
; RV64-PIC-NEXT: lui a1, 280997
; RV64-PIC-NEXT: sw a1, 0(a0)
; RV64-PIC-NEXT: ret
store float 1.234500e+03, ptr %p
ret void
}

define void @constpool_f64_1234_5(ptr %p) {
; RV32-SMALL-LABEL: constpool_f64_1234_5:
; RV32-SMALL: # %bb.0:
; RV32-SMALL-NEXT: lui a1, %hi(.LCPI3_0)
; RV32-SMALL-NEXT: addi a1, a1, %lo(.LCPI3_0)
; RV32-SMALL-NEXT: lw a2, 0(a1)
; RV32-SMALL-NEXT: lw a1, 4(a1)
; RV32-SMALL-NEXT: sw a2, 0(a0)
; RV32-SMALL-NEXT: sw a1, 4(a0)
; RV32-SMALL-NEXT: ret
;
; RV32-MEDIUM-LABEL: constpool_f64_1234_5:
; RV32-MEDIUM: # %bb.0:
; RV32-MEDIUM-NEXT: .Lpcrel_hi1:
; RV32-MEDIUM-NEXT: auipc a1, %pcrel_hi(.LCPI3_0)
; RV32-MEDIUM-NEXT: addi a1, a1, %pcrel_lo(.Lpcrel_hi1)
; RV32-MEDIUM-NEXT: lw a2, 0(a1)
; RV32-MEDIUM-NEXT: lw a1, 4(a1)
; RV32-MEDIUM-NEXT: sw a2, 0(a0)
; RV32-MEDIUM-NEXT: sw a1, 4(a0)
; RV32-MEDIUM-NEXT: ret
;
; RV32-PIC-LABEL: constpool_f64_1234_5:
; RV32-PIC: # %bb.0:
; RV32-PIC-NEXT: .Lpcrel_hi1:
; RV32-PIC-NEXT: auipc a1, %pcrel_hi(.LCPI3_0)
; RV32-PIC-NEXT: addi a1, a1, %pcrel_lo(.Lpcrel_hi1)
; RV32-PIC-NEXT: lw a2, 0(a1)
; RV32-PIC-NEXT: lw a1, 4(a1)
; RV32-PIC-NEXT: sw a2, 0(a0)
; RV32-PIC-NEXT: sw a1, 4(a0)
; RV32-PIC-NEXT: ret
;
; RV64-SMALL-LABEL: constpool_f64_1234_5:
; RV64-SMALL: # %bb.0:
; RV64-SMALL-NEXT: lui a1, 517
; RV64-SMALL-NEXT: addi a1, a1, -1627
; RV64-SMALL-NEXT: slli a1, a1, 41
; RV64-SMALL-NEXT: sd a1, 0(a0)
; RV64-SMALL-NEXT: ret
;
; RV64-MEDIUM-LABEL: constpool_f64_1234_5:
; RV64-MEDIUM: # %bb.0:
; RV64-MEDIUM-NEXT: lui a1, 517
; RV64-MEDIUM-NEXT: addi a1, a1, -1627
; RV64-MEDIUM-NEXT: slli a1, a1, 41
; RV64-MEDIUM-NEXT: sd a1, 0(a0)
; RV64-MEDIUM-NEXT: ret
;
; RV64-PIC-LABEL: constpool_f64_1234_5:
; RV64-PIC: # %bb.0:
; RV64-PIC-NEXT: lui a1, 517
; RV64-PIC-NEXT: addi a1, a1, -1627
; RV64-PIC-NEXT: slli a1, a1, 41
; RV64-PIC-NEXT: sd a1, 0(a0)
; RV64-PIC-NEXT: ret
store double 1.234500e+03, ptr %p
ret void
}
Loading
Loading