Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 1 | //===-- SnippetGeneratorTest.cpp --------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "../Common/AssemblerUtils.h" |
| 11 | #include "Latency.h" |
| 12 | #include "LlvmState.h" |
| 13 | #include "MCInstrDescView.h" |
| 14 | #include "RegisterAliasing.h" |
| 15 | #include "Uops.h" |
| 16 | #include "X86InstrInfo.h" |
| 17 | |
| 18 | #include <unordered_set> |
| 19 | |
| 20 | namespace exegesis { |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 21 | |
| 22 | void InitializeX86ExegesisTarget(); |
| 23 | |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | |
Guillaume Chatelet | 1ebb675 | 2018-06-20 11:09:36 +0000 | [diff] [blame] | 26 | using testing::AnyOf; |
| 27 | using testing::ElementsAre; |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 28 | using testing::HasSubstr; |
| 29 | using testing::Not; |
| 30 | using testing::SizeIs; |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 31 | using testing::UnorderedElementsAre; |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 32 | |
| 33 | MATCHER(IsInvalid, "") { return !arg.isValid(); } |
| 34 | MATCHER(IsReg, "") { return arg.isReg(); } |
| 35 | |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 36 | class X86SnippetGeneratorTest : public ::testing::Test { |
| 37 | protected: |
| 38 | X86SnippetGeneratorTest() |
Guillaume Chatelet | b391f24 | 2018-06-13 14:07:36 +0000 | [diff] [blame] | 39 | : State("x86_64-unknown-linux", "haswell"), |
| 40 | MCInstrInfo(State.getInstrInfo()), MCRegisterInfo(State.getRegInfo()) {} |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 41 | |
| 42 | static void SetUpTestCase() { |
| 43 | LLVMInitializeX86TargetInfo(); |
| 44 | LLVMInitializeX86TargetMC(); |
| 45 | LLVMInitializeX86Target(); |
| 46 | LLVMInitializeX86AsmPrinter(); |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 47 | InitializeX86ExegesisTarget(); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | const LLVMState State; |
| 51 | const llvm::MCInstrInfo &MCInstrInfo; |
| 52 | const llvm::MCRegisterInfo &MCRegisterInfo; |
| 53 | }; |
| 54 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 55 | template <typename SnippetGeneratorT> |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 56 | class SnippetGeneratorTest : public X86SnippetGeneratorTest { |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 57 | protected: |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 58 | SnippetGeneratorTest() : Generator(State) {} |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 59 | |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 60 | CodeTemplate checkAndGetCodeTemplate(unsigned Opcode) { |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 61 | randomGenerator().seed(0); // Initialize seed. |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 62 | auto CodeTemplateOrError = Generator.generateCodeTemplate(Opcode); |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 63 | EXPECT_FALSE(CodeTemplateOrError.takeError()); // Valid configuration. |
| 64 | return std::move(CodeTemplateOrError.get()); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 67 | SnippetGeneratorT Generator; |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 70 | using LatencySnippetGeneratorTest = |
| 71 | SnippetGeneratorTest<LatencySnippetGenerator>; |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 72 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 73 | using UopsSnippetGeneratorTest = SnippetGeneratorTest<UopsSnippetGenerator>; |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 74 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 75 | TEST_F(LatencySnippetGeneratorTest, ImplicitSelfDependency) { |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 76 | // ADC16i16 self alias because of implicit use and def. |
| 77 | |
| 78 | // explicit use 0 : imm |
| 79 | // implicit def : AX |
| 80 | // implicit def : EFLAGS |
| 81 | // implicit use : AX |
| 82 | // implicit use : EFLAGS |
| 83 | const unsigned Opcode = llvm::X86::ADC16i16; |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 84 | EXPECT_THAT(MCInstrInfo.get(Opcode).getImplicitDefs()[0], llvm::X86::AX); |
| 85 | EXPECT_THAT(MCInstrInfo.get(Opcode).getImplicitDefs()[1], llvm::X86::EFLAGS); |
| 86 | EXPECT_THAT(MCInstrInfo.get(Opcode).getImplicitUses()[0], llvm::X86::AX); |
| 87 | EXPECT_THAT(MCInstrInfo.get(Opcode).getImplicitUses()[1], llvm::X86::EFLAGS); |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 88 | const CodeTemplate CT = checkAndGetCodeTemplate(Opcode); |
| 89 | EXPECT_THAT(CT.Info, HasSubstr("implicit")); |
| 90 | ASSERT_THAT(CT.Instructions, SizeIs(1)); |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 91 | const InstructionTemplate &IT = CT.Instructions[0]; |
| 92 | EXPECT_THAT(IT.getOpcode(), Opcode); |
| 93 | ASSERT_THAT(IT.VariableValues, SizeIs(1)); // Imm. |
| 94 | EXPECT_THAT(IT.VariableValues[0], IsInvalid()) << "Immediate is not set"; |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 97 | TEST_F(LatencySnippetGeneratorTest, ExplicitSelfDependency) { |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 98 | // ADD16ri self alias because Op0 and Op1 are tied together. |
| 99 | |
| 100 | // explicit def 0 : reg RegClass=GR16 |
| 101 | // explicit use 1 : reg RegClass=GR16 | TIED_TO:0 |
| 102 | // explicit use 2 : imm |
| 103 | // implicit def : EFLAGS |
| 104 | const unsigned Opcode = llvm::X86::ADD16ri; |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 105 | EXPECT_THAT(MCInstrInfo.get(Opcode).getImplicitDefs()[0], llvm::X86::EFLAGS); |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 106 | const CodeTemplate CT = checkAndGetCodeTemplate(Opcode); |
| 107 | EXPECT_THAT(CT.Info, HasSubstr("explicit")); |
| 108 | ASSERT_THAT(CT.Instructions, SizeIs(1)); |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 109 | const InstructionTemplate &IT = CT.Instructions[0]; |
| 110 | EXPECT_THAT(IT.getOpcode(), Opcode); |
| 111 | ASSERT_THAT(IT.VariableValues, SizeIs(2)); |
| 112 | EXPECT_THAT(IT.VariableValues[0], IsReg()) << "Operand 0 and 1"; |
| 113 | EXPECT_THAT(IT.VariableValues[1], IsInvalid()) << "Operand 2 is not set"; |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 116 | TEST_F(LatencySnippetGeneratorTest, DependencyThroughOtherOpcode) { |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 117 | // CMP64rr |
| 118 | // explicit use 0 : reg RegClass=GR64 |
| 119 | // explicit use 1 : reg RegClass=GR64 |
| 120 | // implicit def : EFLAGS |
| 121 | |
| 122 | const unsigned Opcode = llvm::X86::CMP64rr; |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 123 | const CodeTemplate CT = checkAndGetCodeTemplate(Opcode); |
| 124 | EXPECT_THAT(CT.Info, HasSubstr("cycle through")); |
| 125 | ASSERT_THAT(CT.Instructions, SizeIs(2)); |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 126 | const InstructionTemplate &IT = CT.Instructions[0]; |
| 127 | EXPECT_THAT(IT.getOpcode(), Opcode); |
| 128 | ASSERT_THAT(IT.VariableValues, SizeIs(2)); |
| 129 | EXPECT_THAT(IT.VariableValues, AnyOf(ElementsAre(IsReg(), IsInvalid()), |
Guillaume Chatelet | 1ebb675 | 2018-06-20 11:09:36 +0000 | [diff] [blame] | 130 | ElementsAre(IsInvalid(), IsReg()))); |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 131 | EXPECT_THAT(CT.Instructions[1].getOpcode(), Not(Opcode)); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 132 | // TODO: check that the two instructions alias each other. |
| 133 | } |
| 134 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 135 | TEST_F(LatencySnippetGeneratorTest, LAHF) { |
Guillaume Chatelet | 60e3d58 | 2018-06-13 13:53:56 +0000 | [diff] [blame] | 136 | const unsigned Opcode = llvm::X86::LAHF; |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 137 | const CodeTemplate CT = checkAndGetCodeTemplate(Opcode); |
| 138 | EXPECT_THAT(CT.Info, HasSubstr("cycle through")); |
| 139 | ASSERT_THAT(CT.Instructions, SizeIs(2)); |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 140 | const InstructionTemplate &IT = CT.Instructions[0]; |
| 141 | EXPECT_THAT(IT.getOpcode(), Opcode); |
| 142 | ASSERT_THAT(IT.VariableValues, SizeIs(0)); |
Guillaume Chatelet | 60e3d58 | 2018-06-13 13:53:56 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 145 | TEST_F(UopsSnippetGeneratorTest, ParallelInstruction) { |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 146 | // BNDCL32rr is parallel no matter what. |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 147 | |
| 148 | // explicit use 0 : reg RegClass=BNDR |
| 149 | // explicit use 1 : reg RegClass=GR32 |
| 150 | |
| 151 | const unsigned Opcode = llvm::X86::BNDCL32rr; |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 152 | const CodeTemplate CT = checkAndGetCodeTemplate(Opcode); |
| 153 | EXPECT_THAT(CT.Info, HasSubstr("parallel")); |
| 154 | ASSERT_THAT(CT.Instructions, SizeIs(1)); |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 155 | const InstructionTemplate &IT = CT.Instructions[0]; |
| 156 | EXPECT_THAT(IT.getOpcode(), Opcode); |
| 157 | ASSERT_THAT(IT.VariableValues, SizeIs(2)); |
| 158 | EXPECT_THAT(IT.VariableValues[0], IsInvalid()); |
| 159 | EXPECT_THAT(IT.VariableValues[1], IsInvalid()); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 162 | TEST_F(UopsSnippetGeneratorTest, SerialInstruction) { |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 163 | // CDQ is serial no matter what. |
| 164 | |
| 165 | // implicit def : EAX |
| 166 | // implicit def : EDX |
| 167 | // implicit use : EAX |
| 168 | const unsigned Opcode = llvm::X86::CDQ; |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 169 | const CodeTemplate CT = checkAndGetCodeTemplate(Opcode); |
| 170 | EXPECT_THAT(CT.Info, HasSubstr("serial")); |
| 171 | ASSERT_THAT(CT.Instructions, SizeIs(1)); |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 172 | const InstructionTemplate &IT = CT.Instructions[0]; |
| 173 | EXPECT_THAT(IT.getOpcode(), Opcode); |
| 174 | ASSERT_THAT(IT.VariableValues, SizeIs(0)); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 177 | TEST_F(UopsSnippetGeneratorTest, StaticRenaming) { |
Guillaume Chatelet | 5dab6ad | 2018-10-10 12:58:40 +0000 | [diff] [blame^] | 178 | // CMOVA32rr has tied variables, we enumerate the possible values to execute |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 179 | // as many in parallel as possible. |
| 180 | |
| 181 | // explicit def 0 : reg RegClass=GR32 |
| 182 | // explicit use 1 : reg RegClass=GR32 | TIED_TO:0 |
| 183 | // explicit use 2 : reg RegClass=GR32 |
| 184 | // implicit use : EFLAGS |
| 185 | const unsigned Opcode = llvm::X86::CMOVA32rr; |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 186 | const CodeTemplate CT = checkAndGetCodeTemplate(Opcode); |
| 187 | EXPECT_THAT(CT.Info, HasSubstr("static renaming")); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 188 | constexpr const unsigned kInstructionCount = 15; |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 189 | ASSERT_THAT(CT.Instructions, SizeIs(kInstructionCount)); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 190 | std::unordered_set<unsigned> AllDefRegisters; |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 191 | for (const auto &IT : CT.Instructions) { |
| 192 | ASSERT_THAT(IT.VariableValues, SizeIs(2)); |
| 193 | AllDefRegisters.insert(IT.VariableValues[0].getReg()); |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 194 | } |
| 195 | EXPECT_THAT(AllDefRegisters, SizeIs(kInstructionCount)) |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 196 | << "Each instruction writes to a different register"; |
| 197 | } |
| 198 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 199 | TEST_F(UopsSnippetGeneratorTest, NoTiedVariables) { |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 200 | // CMOV_GR32 has no tied variables, we make sure def and use are different |
| 201 | // from each other. |
| 202 | |
| 203 | // explicit def 0 : reg RegClass=GR32 |
| 204 | // explicit use 1 : reg RegClass=GR32 |
| 205 | // explicit use 2 : reg RegClass=GR32 |
| 206 | // explicit use 3 : imm |
| 207 | // implicit use : EFLAGS |
| 208 | const unsigned Opcode = llvm::X86::CMOV_GR32; |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 209 | const CodeTemplate CT = checkAndGetCodeTemplate(Opcode); |
| 210 | EXPECT_THAT(CT.Info, HasSubstr("no tied variables")); |
| 211 | ASSERT_THAT(CT.Instructions, SizeIs(1)); |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 212 | const InstructionTemplate &IT = CT.Instructions[0]; |
| 213 | EXPECT_THAT(IT.getOpcode(), Opcode); |
| 214 | ASSERT_THAT(IT.VariableValues, SizeIs(4)); |
| 215 | EXPECT_THAT(IT.VariableValues[0].getReg(), Not(IT.VariableValues[1].getReg())) |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 216 | << "Def is different from first Use"; |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 217 | EXPECT_THAT(IT.VariableValues[0].getReg(), Not(IT.VariableValues[2].getReg())) |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 218 | << "Def is different from second Use"; |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 219 | EXPECT_THAT(IT.VariableValues[3], IsInvalid()); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 222 | TEST_F(UopsSnippetGeneratorTest, MemoryUse) { |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 223 | // Mov32rm reads from memory. |
| 224 | const unsigned Opcode = llvm::X86::MOV32rm; |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 225 | const CodeTemplate CT = checkAndGetCodeTemplate(Opcode); |
| 226 | EXPECT_THAT(CT.Info, HasSubstr("no tied variables")); |
| 227 | ASSERT_THAT(CT.Instructions, |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 228 | SizeIs(UopsSnippetGenerator::kMinNumDifferentAddresses)); |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 229 | const InstructionTemplate &IT = CT.Instructions[0]; |
| 230 | EXPECT_THAT(IT.getOpcode(), Opcode); |
| 231 | ASSERT_THAT(IT.VariableValues, SizeIs(6)); |
| 232 | EXPECT_EQ(IT.VariableValues[2].getImm(), 1); |
| 233 | EXPECT_EQ(IT.VariableValues[3].getReg(), 0u); |
| 234 | EXPECT_EQ(IT.VariableValues[4].getImm(), 0); |
| 235 | EXPECT_EQ(IT.VariableValues[5].getReg(), 0u); |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 238 | TEST_F(UopsSnippetGeneratorTest, MemoryUse_Movsb) { |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 239 | // MOVSB writes to scratch memory register. |
| 240 | const unsigned Opcode = llvm::X86::MOVSB; |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 241 | auto Error = Generator.generateCodeTemplate(Opcode).takeError(); |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 242 | EXPECT_TRUE((bool)Error); |
| 243 | llvm::consumeError(std::move(Error)); |
| 244 | } |
| 245 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 246 | class FakeSnippetGenerator : public SnippetGenerator { |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 247 | public: |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 248 | FakeSnippetGenerator(const LLVMState &State) : SnippetGenerator(State) {} |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 249 | |
| 250 | Instruction createInstruction(unsigned Opcode) { |
Clement Courbet | 0e8bf4e | 2018-06-25 13:44:27 +0000 | [diff] [blame] | 251 | return Instruction(State.getInstrInfo().get(Opcode), RATC); |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | private: |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 255 | llvm::Expected<CodeTemplate> |
| 256 | generateCodeTemplate(unsigned Opcode) const override { |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 257 | return llvm::make_error<llvm::StringError>("not implemented", |
| 258 | llvm::inconvertibleErrorCode()); |
| 259 | } |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 260 | }; |
| 261 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 262 | using FakeSnippetGeneratorTest = SnippetGeneratorTest<FakeSnippetGenerator>; |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 263 | |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 264 | testing::Matcher<const RegisterValue &> IsRegisterValue(unsigned Reg, |
| 265 | llvm::APInt Value) { |
| 266 | return testing::AllOf(testing::Field(&RegisterValue::Register, Reg), |
| 267 | testing::Field(&RegisterValue::Value, Value)); |
| 268 | } |
| 269 | |
| 270 | TEST_F(FakeSnippetGeneratorTest, ComputeRegisterInitialValuesAdd16ri) { |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 271 | // ADD16ri: |
| 272 | // explicit def 0 : reg RegClass=GR16 |
| 273 | // explicit use 1 : reg RegClass=GR16 | TIED_TO:0 |
| 274 | // explicit use 2 : imm |
| 275 | // implicit def : EFLAGS |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 276 | InstructionTemplate IT(Generator.createInstruction(llvm::X86::ADD16ri)); |
| 277 | IT.getValueFor(IT.Instr.Variables[0]) = |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 278 | llvm::MCOperand::createReg(llvm::X86::AX); |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 279 | std::vector<InstructionTemplate> Snippet; |
| 280 | Snippet.push_back(std::move(IT)); |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 281 | const auto RIV = Generator.computeRegisterInitialValues(Snippet); |
| 282 | EXPECT_THAT(RIV, ElementsAre(IsRegisterValue(llvm::X86::AX, llvm::APInt()))); |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 285 | TEST_F(FakeSnippetGeneratorTest, ComputeRegisterInitialValuesAdd64rr) { |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 286 | // ADD64rr: |
| 287 | // mov64ri rax, 42 |
| 288 | // add64rr rax, rax, rbx |
| 289 | // -> only rbx needs defining. |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 290 | std::vector<InstructionTemplate> Snippet; |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 291 | { |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 292 | InstructionTemplate Mov(Generator.createInstruction(llvm::X86::MOV64ri)); |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 293 | Mov.getValueFor(Mov.Instr.Variables[0]) = |
| 294 | llvm::MCOperand::createReg(llvm::X86::RAX); |
| 295 | Mov.getValueFor(Mov.Instr.Variables[1]) = llvm::MCOperand::createImm(42); |
| 296 | Snippet.push_back(std::move(Mov)); |
| 297 | } |
| 298 | { |
Guillaume Chatelet | 70ac019 | 2018-09-27 09:23:04 +0000 | [diff] [blame] | 299 | InstructionTemplate Add(Generator.createInstruction(llvm::X86::ADD64rr)); |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 300 | Add.getValueFor(Add.Instr.Variables[0]) = |
| 301 | llvm::MCOperand::createReg(llvm::X86::RAX); |
| 302 | Add.getValueFor(Add.Instr.Variables[1]) = |
| 303 | llvm::MCOperand::createReg(llvm::X86::RBX); |
| 304 | Snippet.push_back(std::move(Add)); |
| 305 | } |
| 306 | |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 307 | const auto RIV = Generator.computeRegisterInitialValues(Snippet); |
| 308 | EXPECT_THAT(RIV, ElementsAre(IsRegisterValue(llvm::X86::RBX, llvm::APInt()))); |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 311 | } // namespace |
| 312 | } // namespace exegesis |