Skip to content

Commit 842fd15

Browse files
[llvm-exegesis] Add explicit support for setting DF in X86 (#115644)
While llvm-exegesis has explicit support for setting EFLAGS which contains DF, it can be nice sometimes to explicitly set DF, especially given that it is modeled as a separate register within LLVM. This patch adds the ability to do that by lowering setting the value to 0 or 1 to cld and std respectively.
1 parent 7b52549 commit 842fd15

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

llvm/tools/llvm-exegesis/lib/X86/Target.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,8 @@ struct ConstantInliner {
537537
std::vector<MCInst> loadImplicitRegAndFinalize(unsigned Opcode,
538538
unsigned Value);
539539

540+
std::vector<MCInst> loadDirectionFlagAndFinalize();
541+
540542
private:
541543
ConstantInliner &add(const MCInst &Inst) {
542544
Instructions.push_back(Inst);
@@ -612,6 +614,15 @@ ConstantInliner::loadImplicitRegAndFinalize(unsigned Opcode, unsigned Value) {
612614
return std::move(Instructions);
613615
}
614616

617+
std::vector<MCInst> ConstantInliner::loadDirectionFlagAndFinalize() {
618+
if (Constant_.isZero())
619+
add(MCInstBuilder(X86::CLD));
620+
else if (Constant_.isOne())
621+
add(MCInstBuilder(X86::STD));
622+
623+
return std::move(Instructions);
624+
}
625+
615626
void ConstantInliner::initStack(unsigned Bytes) {
616627
assert(Constant_.getBitWidth() <= Bytes * 8 &&
617628
"Value does not have the correct size");
@@ -1089,6 +1100,8 @@ std::vector<MCInst> ExegesisX86Target::setRegTo(const MCSubtargetInfo &STI,
10891100
0x1f80);
10901101
if (Reg == X86::FPCW)
10911102
return CI.loadImplicitRegAndFinalize(X86::FLDCW16m, 0x37f);
1103+
if (Reg == X86::DF)
1104+
return CI.loadDirectionFlagAndFinalize();
10921105
return {}; // Not yet implemented.
10931106
}
10941107

llvm/unittests/tools/llvm-exegesis/X86/TargetTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,14 @@ TEST_F(X86Core2TargetTest, SetRegToFP1_4Bits) {
585585
OpcodeIs(X86::LD_Fp80m), IsStackDeallocate(10)));
586586
}
587587

588+
TEST_F(X86Core2TargetTest, SetRegToDf1) {
589+
EXPECT_THAT(setRegTo(X86::DF, APInt(1, 1)), ElementsAre(OpcodeIs(X86::STD)));
590+
}
591+
592+
TEST_F(X86Core2TargetTest, SetRegToDf0) {
593+
EXPECT_THAT(setRegTo(X86::DF, APInt(1, 0)), ElementsAre(OpcodeIs(X86::CLD)));
594+
}
595+
588596
TEST_F(X86Core2Avx512TargetTest, FillMemoryOperands_ADD64rm) {
589597
const Instruction &I = getInstr(X86::ADD64rm);
590598
InstructionTemplate IT(&I);

0 commit comments

Comments
 (0)