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