Skip to content

Commit f866fde

Browse files
authored
[RISCV][GISel] Lower G_FCONSTANT to constant pool load without F or D. (#73034)
I used an IR test because it was easier than constructing different MIR test for each type of addressing.
1 parent 9584f58 commit f866fde

File tree

3 files changed

+130
-3
lines changed

3 files changed

+130
-3
lines changed

llvm/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ bool RISCVInstructionSelector::select(MachineInstr &MI) {
540540
GV->hasExternalWeakLinkage());
541541
}
542542
case TargetOpcode::G_JUMP_TABLE:
543+
case TargetOpcode::G_CONSTANT_POOL:
543544
return selectAddr(MI, MIB, MRI);
544545
case TargetOpcode::G_BRCOND: {
545546
Register LHS, RHS;
@@ -875,7 +876,8 @@ bool RISCVInstructionSelector::selectAddr(MachineInstr &MI,
875876
bool IsLocal,
876877
bool IsExternWeak) const {
877878
assert((MI.getOpcode() == TargetOpcode::G_GLOBAL_VALUE ||
878-
MI.getOpcode() == TargetOpcode::G_JUMP_TABLE) &&
879+
MI.getOpcode() == TargetOpcode::G_JUMP_TABLE ||
880+
MI.getOpcode() == TargetOpcode::G_CONSTANT_POOL) &&
879881
"Unexpected opcode");
880882

881883
const MachineOperand &DispMO = MI.getOperand(1);

llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ RISCVLegalizerInfo::RISCVLegalizerInfo(const RISCVSubtarget &ST)
195195
.widenScalarToNextPow2(0)
196196
.clampScalar(0, sXLen, sXLen);
197197

198-
getActionDefinitionsBuilder({G_GLOBAL_VALUE, G_JUMP_TABLE}).legalFor({p0});
198+
getActionDefinitionsBuilder({G_GLOBAL_VALUE, G_JUMP_TABLE, G_CONSTANT_POOL})
199+
.legalFor({p0});
199200

200201
if (ST.hasStdExtM() || ST.hasStdExtZmmul()) {
201202
getActionDefinitionsBuilder(G_MUL)
@@ -283,7 +284,9 @@ RISCVLegalizerInfo::RISCVLegalizerInfo(const RISCVSubtarget &ST)
283284
getActionDefinitionsBuilder(G_IS_FPCLASS)
284285
.customIf(all(typeIs(0, s1), typeIsScalarFPArith(1, ST)));
285286

286-
getActionDefinitionsBuilder(G_FCONSTANT).legalIf(typeIsScalarFPArith(0, ST));
287+
getActionDefinitionsBuilder(G_FCONSTANT)
288+
.legalIf(typeIsScalarFPArith(0, ST))
289+
.lowerFor({s32, s64});
287290

288291
getActionDefinitionsBuilder({G_FPTOSI, G_FPTOUI})
289292
.legalIf(all(typeInSet(0, {s32, sXLen}), typeIsScalarFPArith(1, ST)))
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
2+
; RUN: llc < %s -mtriple=riscv32 -global-isel -code-model=small \
3+
; RUN: -verify-machineinstrs | FileCheck %s --check-prefix=RV32-SMALL
4+
; RUN: llc < %s -mtriple=riscv32 -global-isel -code-model=medium \
5+
; RUN: -verify-machineinstrs | FileCheck %s --check-prefix=RV32-MEDIUM
6+
; RUN: llc < %s -mtriple=riscv32 -global-isel -relocation-model=pic \
7+
; RUN: -verify-machineinstrs | FileCheck %s --check-prefix=RV32-PIC
8+
; RUN: llc < %s -mtriple=riscv64 -global-isel -code-model=small \
9+
; RUN: -verify-machineinstrs | FileCheck %s --check-prefix=RV64-SMALL
10+
; RUN: llc < %s -mtriple=riscv64 -global-isel -code-model=medium \
11+
; RUN: -verify-machineinstrs | FileCheck %s --check-prefix=RV64-MEDIUM
12+
; RUN: llc < %s -mtriple=riscv64 -global-isel -relocation-model=pic \
13+
; RUN: -verify-machineinstrs | FileCheck %s --check-prefix=RV64-PIC
14+
15+
define void @constpool_f32(ptr %p) {
16+
; RV32-SMALL-LABEL: constpool_f32:
17+
; RV32-SMALL: # %bb.0:
18+
; RV32-SMALL-NEXT: lui a1, %hi(.LCPI0_0)
19+
; RV32-SMALL-NEXT: lw a1, %lo(.LCPI0_0)(a1)
20+
; RV32-SMALL-NEXT: sw a1, 0(a0)
21+
; RV32-SMALL-NEXT: ret
22+
;
23+
; RV32-MEDIUM-LABEL: constpool_f32:
24+
; RV32-MEDIUM: # %bb.0:
25+
; RV32-MEDIUM-NEXT: .Lpcrel_hi0:
26+
; RV32-MEDIUM-NEXT: auipc a1, %pcrel_hi(.LCPI0_0)
27+
; RV32-MEDIUM-NEXT: lw a1, %pcrel_lo(.Lpcrel_hi0)(a1)
28+
; RV32-MEDIUM-NEXT: sw a1, 0(a0)
29+
; RV32-MEDIUM-NEXT: ret
30+
;
31+
; RV32-PIC-LABEL: constpool_f32:
32+
; RV32-PIC: # %bb.0:
33+
; RV32-PIC-NEXT: .Lpcrel_hi0:
34+
; RV32-PIC-NEXT: auipc a1, %pcrel_hi(.LCPI0_0)
35+
; RV32-PIC-NEXT: lw a1, %pcrel_lo(.Lpcrel_hi0)(a1)
36+
; RV32-PIC-NEXT: sw a1, 0(a0)
37+
; RV32-PIC-NEXT: ret
38+
;
39+
; RV64-SMALL-LABEL: constpool_f32:
40+
; RV64-SMALL: # %bb.0:
41+
; RV64-SMALL-NEXT: lui a1, %hi(.LCPI0_0)
42+
; RV64-SMALL-NEXT: lw a1, %lo(.LCPI0_0)(a1)
43+
; RV64-SMALL-NEXT: sw a1, 0(a0)
44+
; RV64-SMALL-NEXT: ret
45+
;
46+
; RV64-MEDIUM-LABEL: constpool_f32:
47+
; RV64-MEDIUM: # %bb.0:
48+
; RV64-MEDIUM-NEXT: .Lpcrel_hi0:
49+
; RV64-MEDIUM-NEXT: auipc a1, %pcrel_hi(.LCPI0_0)
50+
; RV64-MEDIUM-NEXT: lw a1, %pcrel_lo(.Lpcrel_hi0)(a1)
51+
; RV64-MEDIUM-NEXT: sw a1, 0(a0)
52+
; RV64-MEDIUM-NEXT: ret
53+
;
54+
; RV64-PIC-LABEL: constpool_f32:
55+
; RV64-PIC: # %bb.0:
56+
; RV64-PIC-NEXT: .Lpcrel_hi0:
57+
; RV64-PIC-NEXT: auipc a1, %pcrel_hi(.LCPI0_0)
58+
; RV64-PIC-NEXT: lw a1, %pcrel_lo(.Lpcrel_hi0)(a1)
59+
; RV64-PIC-NEXT: sw a1, 0(a0)
60+
; RV64-PIC-NEXT: ret
61+
store float 1.0, ptr %p
62+
ret void
63+
}
64+
65+
define void @constpool_f64(ptr %p) {
66+
; RV32-SMALL-LABEL: constpool_f64:
67+
; RV32-SMALL: # %bb.0:
68+
; RV32-SMALL-NEXT: lui a1, %hi(.LCPI1_0)
69+
; RV32-SMALL-NEXT: addi a1, a1, %lo(.LCPI1_0)
70+
; RV32-SMALL-NEXT: lw a2, 0(a1)
71+
; RV32-SMALL-NEXT: lw a1, 4(a1)
72+
; RV32-SMALL-NEXT: sw a2, 0(a0)
73+
; RV32-SMALL-NEXT: sw a1, 4(a0)
74+
; RV32-SMALL-NEXT: ret
75+
;
76+
; RV32-MEDIUM-LABEL: constpool_f64:
77+
; RV32-MEDIUM: # %bb.0:
78+
; RV32-MEDIUM-NEXT: .Lpcrel_hi1:
79+
; RV32-MEDIUM-NEXT: auipc a1, %pcrel_hi(.LCPI1_0)
80+
; RV32-MEDIUM-NEXT: addi a1, a1, %pcrel_lo(.Lpcrel_hi1)
81+
; RV32-MEDIUM-NEXT: lw a2, 0(a1)
82+
; RV32-MEDIUM-NEXT: lw a1, 4(a1)
83+
; RV32-MEDIUM-NEXT: sw a2, 0(a0)
84+
; RV32-MEDIUM-NEXT: sw a1, 4(a0)
85+
; RV32-MEDIUM-NEXT: ret
86+
;
87+
; RV32-PIC-LABEL: constpool_f64:
88+
; RV32-PIC: # %bb.0:
89+
; RV32-PIC-NEXT: .Lpcrel_hi1:
90+
; RV32-PIC-NEXT: auipc a1, %pcrel_hi(.LCPI1_0)
91+
; RV32-PIC-NEXT: addi a1, a1, %pcrel_lo(.Lpcrel_hi1)
92+
; RV32-PIC-NEXT: lw a2, 0(a1)
93+
; RV32-PIC-NEXT: lw a1, 4(a1)
94+
; RV32-PIC-NEXT: sw a2, 0(a0)
95+
; RV32-PIC-NEXT: sw a1, 4(a0)
96+
; RV32-PIC-NEXT: ret
97+
;
98+
; RV64-SMALL-LABEL: constpool_f64:
99+
; RV64-SMALL: # %bb.0:
100+
; RV64-SMALL-NEXT: lui a1, %hi(.LCPI1_0)
101+
; RV64-SMALL-NEXT: ld a1, %lo(.LCPI1_0)(a1)
102+
; RV64-SMALL-NEXT: sd a1, 0(a0)
103+
; RV64-SMALL-NEXT: ret
104+
;
105+
; RV64-MEDIUM-LABEL: constpool_f64:
106+
; RV64-MEDIUM: # %bb.0:
107+
; RV64-MEDIUM-NEXT: .Lpcrel_hi1:
108+
; RV64-MEDIUM-NEXT: auipc a1, %pcrel_hi(.LCPI1_0)
109+
; RV64-MEDIUM-NEXT: ld a1, %pcrel_lo(.Lpcrel_hi1)(a1)
110+
; RV64-MEDIUM-NEXT: sd a1, 0(a0)
111+
; RV64-MEDIUM-NEXT: ret
112+
;
113+
; RV64-PIC-LABEL: constpool_f64:
114+
; RV64-PIC: # %bb.0:
115+
; RV64-PIC-NEXT: .Lpcrel_hi1:
116+
; RV64-PIC-NEXT: auipc a1, %pcrel_hi(.LCPI1_0)
117+
; RV64-PIC-NEXT: ld a1, %pcrel_lo(.Lpcrel_hi1)(a1)
118+
; RV64-PIC-NEXT: sd a1, 0(a0)
119+
; RV64-PIC-NEXT: ret
120+
store double 1.0, ptr %p
121+
ret void
122+
}

0 commit comments

Comments
 (0)