llvm-project
73 строки · 2.6 Кб
1//===-- X86RegisterAliasingTest.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
10#include "RegisterAliasing.h"
11
12#include <cassert>
13#include <memory>
14
15#include "TestBase.h"
16#include "X86InstrInfo.h"
17#include "llvm/MC/TargetRegistry.h"
18#include "llvm/Support/TargetSelect.h"
19#include "gmock/gmock.h"
20#include "gtest/gtest.h"
21
22namespace llvm {
23namespace exegesis {
24namespace {
25
26class X86RegisterAliasingTest : public X86TestBase {};
27
28TEST_F(X86RegisterAliasingTest, TrackSimpleRegister) {
29const auto &RegInfo = State.getRegInfo();
30const RegisterAliasingTracker tracker(RegInfo, X86::EAX);
31std::set<MCPhysReg> ActualAliasedRegisters;
32for (unsigned I : tracker.aliasedBits().set_bits())
33ActualAliasedRegisters.insert(static_cast<MCPhysReg>(I));
34const std::set<MCPhysReg> ExpectedAliasedRegisters = {
35X86::AL, X86::AH, X86::AX, X86::EAX, X86::HAX, X86::RAX};
36ASSERT_THAT(ActualAliasedRegisters, ExpectedAliasedRegisters);
37for (MCPhysReg aliased : ExpectedAliasedRegisters) {
38ASSERT_THAT(tracker.getOrigin(aliased), X86::EAX);
39}
40}
41
42TEST_F(X86RegisterAliasingTest, TrackRegisterClass) {
43// The alias bits for GR8_ABCD_LRegClassID are the union of the alias bits for
44// AL, BL, CL and DL.
45const auto &RegInfo = State.getRegInfo();
46const BitVector NoReservedReg(RegInfo.getNumRegs());
47
48const RegisterAliasingTracker RegClassTracker(
49RegInfo, NoReservedReg, RegInfo.getRegClass(X86::GR8_ABCD_LRegClassID));
50
51BitVector sum(RegInfo.getNumRegs());
52sum |= RegisterAliasingTracker(RegInfo, X86::AL).aliasedBits();
53sum |= RegisterAliasingTracker(RegInfo, X86::BL).aliasedBits();
54sum |= RegisterAliasingTracker(RegInfo, X86::CL).aliasedBits();
55sum |= RegisterAliasingTracker(RegInfo, X86::DL).aliasedBits();
56
57ASSERT_THAT(RegClassTracker.aliasedBits(), sum);
58}
59
60TEST_F(X86RegisterAliasingTest, TrackRegisterClassCache) {
61// Fetching twice the same tracker yields the same pointers.
62const auto &RegInfo = State.getRegInfo();
63const BitVector NoReservedReg(RegInfo.getNumRegs());
64RegisterAliasingTrackerCache Cache(RegInfo, NoReservedReg);
65ASSERT_THAT(&Cache.getRegister(X86::AX), &Cache.getRegister(X86::AX));
66
67ASSERT_THAT(&Cache.getRegisterClass(X86::GR8_ABCD_LRegClassID),
68&Cache.getRegisterClass(X86::GR8_ABCD_LRegClassID));
69}
70
71} // namespace
72} // namespace exegesis
73} // namespace llvm
74