1 //===-- SnippetRepetitorTest.cpp --------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "../Common/AssemblerUtils.h"
10 #include "LlvmState.h"
11 #include "MCInstrDescView.h"
12 #include "RegisterAliasing.h"
13 #include "TestBase.h"
14 #include "X86InstrInfo.h"
15 #include "llvm/CodeGen/MachineBasicBlock.h"
16 
17 namespace llvm {
18 namespace exegesis {
19 
20 void InitializeX86ExegesisTarget();
21 
22 namespace {
23 
24 using testing::ElementsAre;
25 using testing::Eq;
26 using testing::Field;
27 using testing::Property;
28 using testing::UnorderedElementsAre;
29 
30 class X86SnippetRepetitorTest : public X86TestBase {
31 protected:
SetUp()32   void SetUp() override {
33     TM = State.createTargetMachine();
34     Context = std::make_unique<LLVMContext>();
35     Mod = std::make_unique<Module>("X86SnippetRepetitorTest", *Context);
36     Mod->setDataLayout(TM->createDataLayout());
37     MMI = std::make_unique<MachineModuleInfo>(TM.get());
38     MF = &createVoidVoidPtrMachineFunction("TestFn", Mod.get(), MMI.get());
39   }
40 
TestCommon(InstructionBenchmark::RepetitionModeE RepetitionMode)41   void TestCommon(InstructionBenchmark::RepetitionModeE RepetitionMode) {
42     const auto Repetitor = SnippetRepetitor::Create(RepetitionMode, State);
43     const std::vector<MCInst> Instructions = {MCInstBuilder(X86::NOOP)};
44     FunctionFiller Sink(*MF, {X86::EAX});
45     const auto Fill = Repetitor->Repeat(Instructions, kMinInstructions);
46     Fill(Sink);
47   }
48 
49   static constexpr const unsigned kMinInstructions = 3;
50 
51   std::unique_ptr<LLVMTargetMachine> TM;
52   std::unique_ptr<LLVMContext> Context;
53   std::unique_ptr<Module> Mod;
54   std::unique_ptr<MachineModuleInfo> MMI;
55   MachineFunction *MF = nullptr;
56 };
57 
__anon246cab060202(unsigned Opcode) 58 static auto HasOpcode = [](unsigned Opcode) {
59   return Property(&MachineInstr::getOpcode, Eq(Opcode));
60 };
61 
__anon246cab060302(unsigned Reg) 62 static auto LiveReg = [](unsigned Reg) {
63   return Field(&MachineBasicBlock::RegisterMaskPair::PhysReg, Eq(Reg));
64 };
65 
TEST_F(X86SnippetRepetitorTest,Duplicate)66 TEST_F(X86SnippetRepetitorTest, Duplicate) {
67   TestCommon(InstructionBenchmark::Duplicate);
68   // Duplicating creates a single basic block that repeats the instructions.
69   ASSERT_EQ(MF->getNumBlockIDs(), 1u);
70   EXPECT_THAT(MF->getBlockNumbered(0)->instrs(),
71               ElementsAre(HasOpcode(X86::NOOP), HasOpcode(X86::NOOP),
72                           HasOpcode(X86::NOOP), HasOpcode(X86::RETQ)));
73 }
74 
TEST_F(X86SnippetRepetitorTest,Loop)75 TEST_F(X86SnippetRepetitorTest, Loop) {
76   TestCommon(InstructionBenchmark::Loop);
77   // Duplicating creates an entry block, a loop body and a ret block.
78   ASSERT_EQ(MF->getNumBlockIDs(), 3u);
79   const auto &LoopBlock = *MF->getBlockNumbered(1);
80   EXPECT_THAT(LoopBlock.instrs(),
81               ElementsAre(HasOpcode(X86::NOOP), HasOpcode(X86::ADD64ri8),
82                           HasOpcode(X86::JCC_1)));
83   EXPECT_THAT(LoopBlock.liveins(),
84               UnorderedElementsAre(
85                   LiveReg(X86::EAX),
86                   LiveReg(State.getExegesisTarget().getLoopCounterRegister(
87                       State.getTargetMachine().getTargetTriple()))));
88   EXPECT_THAT(MF->getBlockNumbered(2)->instrs(),
89               ElementsAre(HasOpcode(X86::RETQ)));
90 }
91 
92 } // namespace
93 } // namespace exegesis
94 } // namespace llvm
95