Skip to content

Commit f9795f3

Browse files
authored
[GlobalISel] Add build methods for FP environment intrinsics (#96607)
This change adds methods like buildGetFPEnv and similar for opcodes that represent manipulation on floating-point state.
1 parent 5feb32b commit f9795f3

File tree

3 files changed

+67
-10
lines changed

3 files changed

+67
-10
lines changed

llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,6 +2173,36 @@ class MachineIRBuilder {
21732173
return buildInstr(TargetOpcode::G_BITREVERSE, {Dst}, {Src});
21742174
}
21752175

2176+
/// Build and insert \p Dst = G_GET_FPENV
2177+
MachineInstrBuilder buildGetFPEnv(const DstOp &Dst) {
2178+
return buildInstr(TargetOpcode::G_GET_FPENV, {Dst}, {});
2179+
}
2180+
2181+
/// Build and insert G_SET_FPENV \p Src
2182+
MachineInstrBuilder buildSetFPEnv(const SrcOp &Src) {
2183+
return buildInstr(TargetOpcode::G_SET_FPENV, {}, {Src});
2184+
}
2185+
2186+
/// Build and insert G_RESET_FPENV
2187+
MachineInstrBuilder buildResetFPEnv() {
2188+
return buildInstr(TargetOpcode::G_RESET_FPENV, {}, {});
2189+
}
2190+
2191+
/// Build and insert \p Dst = G_GET_FPMODE
2192+
MachineInstrBuilder buildGetFPMode(const DstOp &Dst) {
2193+
return buildInstr(TargetOpcode::G_GET_FPMODE, {Dst}, {});
2194+
}
2195+
2196+
/// Build and insert G_SET_FPMODE \p Src
2197+
MachineInstrBuilder buildSetFPMode(const SrcOp &Src) {
2198+
return buildInstr(TargetOpcode::G_SET_FPMODE, {}, {Src});
2199+
}
2200+
2201+
/// Build and insert G_RESET_FPMODE
2202+
MachineInstrBuilder buildResetFPMode() {
2203+
return buildInstr(TargetOpcode::G_RESET_FPMODE, {}, {});
2204+
}
2205+
21762206
virtual MachineInstrBuilder
21772207
buildInstr(unsigned Opc, ArrayRef<DstOp> DstOps, ArrayRef<SrcOp> SrcOps,
21782208
std::optional<unsigned> Flags = std::nullopt);

llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,24 +2541,20 @@ bool IRTranslator::translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
25412541
}
25422542
case Intrinsic::set_fpenv: {
25432543
Value *FPEnv = CI.getOperand(0);
2544-
MIRBuilder.buildInstr(TargetOpcode::G_SET_FPENV, {},
2545-
{getOrCreateVReg(*FPEnv)});
2544+
MIRBuilder.buildSetFPEnv(getOrCreateVReg(*FPEnv));
25462545
return true;
25472546
}
2548-
case Intrinsic::reset_fpenv: {
2549-
MIRBuilder.buildInstr(TargetOpcode::G_RESET_FPENV, {}, {});
2547+
case Intrinsic::reset_fpenv:
2548+
MIRBuilder.buildResetFPEnv();
25502549
return true;
2551-
}
25522550
case Intrinsic::set_fpmode: {
25532551
Value *FPState = CI.getOperand(0);
2554-
MIRBuilder.buildInstr(TargetOpcode::G_SET_FPMODE, {},
2555-
{ getOrCreateVReg(*FPState) });
2552+
MIRBuilder.buildSetFPMode(getOrCreateVReg(*FPState));
25562553
return true;
25572554
}
2558-
case Intrinsic::reset_fpmode: {
2559-
MIRBuilder.buildInstr(TargetOpcode::G_RESET_FPMODE, {}, {});
2555+
case Intrinsic::reset_fpmode:
2556+
MIRBuilder.buildResetFPMode();
25602557
return true;
2561-
}
25622558
case Intrinsic::vscale: {
25632559
MIRBuilder.buildVScale(getOrCreateVReg(CI), 1);
25642560
return true;

llvm/unittests/CodeGen/GlobalISel/MachineIRBuilderTest.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,34 @@ TEST_F(AArch64GISelMITest, BuildBitfieldExtract) {
449449

450450
EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
451451
}
452+
453+
TEST_F(AArch64GISelMITest, BuildFPEnv) {
454+
setUp();
455+
if (!TM)
456+
GTEST_SKIP();
457+
458+
LLT S32 = LLT::scalar(32);
459+
SmallVector<Register, 4> Copies;
460+
collectCopies(Copies, MF);
461+
462+
B.buildGetFPEnv(Copies[0]);
463+
B.buildSetFPEnv(Copies[1]);
464+
B.buildResetFPEnv();
465+
auto GetFPMode = B.buildGetFPMode(S32);
466+
B.buildSetFPMode(GetFPMode);
467+
B.buildResetFPMode();
468+
469+
auto CheckStr = R"(
470+
; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
471+
; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
472+
; CHECK: [[COPY2:%[0-9]+]]:_(s64) = COPY $x2
473+
; CHECK: [[COPY0]]:_(s64) = G_GET_FPENV
474+
; CHECK: G_SET_FPENV [[COPY1]]:_(s64)
475+
; CHECK: G_RESET_FPENV
476+
; CHECK: [[FPMODE:%[0-9]+]]:_(s32) = G_GET_FPMODE
477+
; CHECK: G_SET_FPMODE [[FPMODE]]:_(s32)
478+
; CHECK: G_RESET_FPMODE
479+
)";
480+
481+
EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
482+
}

0 commit comments

Comments
 (0)