1 //===-- RegisterAliasingTest.cpp --------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 #include "RegisterAliasing.h"
16 #include "X86InstrInfo.h"
17 #include "llvm/Support/TargetRegistry.h"
18 #include "llvm/Support/TargetSelect.h"
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
26 class RegisterAliasingTest
: public X86TestBase
{};
28 TEST_F(RegisterAliasingTest
, TrackSimpleRegister
) {
29 const auto &RegInfo
= State
.getRegInfo();
30 const RegisterAliasingTracker
tracker(RegInfo
, X86::EAX
);
31 std::set
<MCPhysReg
> ActualAliasedRegisters
;
32 for (unsigned I
: tracker
.aliasedBits().set_bits())
33 ActualAliasedRegisters
.insert(static_cast<MCPhysReg
>(I
));
34 const std::set
<MCPhysReg
> ExpectedAliasedRegisters
= {
35 X86::AL
, X86::AH
, X86::AX
, X86::EAX
, X86::HAX
, X86::RAX
};
36 ASSERT_THAT(ActualAliasedRegisters
, ExpectedAliasedRegisters
);
37 for (MCPhysReg aliased
: ExpectedAliasedRegisters
) {
38 ASSERT_THAT(tracker
.getOrigin(aliased
), X86::EAX
);
42 TEST_F(RegisterAliasingTest
, TrackRegisterClass
) {
43 // The alias bits for GR8_ABCD_LRegClassID are the union of the alias bits for
45 const auto &RegInfo
= State
.getRegInfo();
46 const BitVector
NoReservedReg(RegInfo
.getNumRegs());
48 const RegisterAliasingTracker
RegClassTracker(
49 RegInfo
, NoReservedReg
, RegInfo
.getRegClass(X86::GR8_ABCD_LRegClassID
));
51 BitVector
sum(RegInfo
.getNumRegs());
52 sum
|= RegisterAliasingTracker(RegInfo
, X86::AL
).aliasedBits();
53 sum
|= RegisterAliasingTracker(RegInfo
, X86::BL
).aliasedBits();
54 sum
|= RegisterAliasingTracker(RegInfo
, X86::CL
).aliasedBits();
55 sum
|= RegisterAliasingTracker(RegInfo
, X86::DL
).aliasedBits();
57 ASSERT_THAT(RegClassTracker
.aliasedBits(), sum
);
60 TEST_F(RegisterAliasingTest
, TrackRegisterClassCache
) {
61 // Fetching twice the same tracker yields the same pointers.
62 const auto &RegInfo
= State
.getRegInfo();
63 const BitVector
NoReservedReg(RegInfo
.getNumRegs());
64 RegisterAliasingTrackerCache
Cache(RegInfo
, NoReservedReg
);
65 ASSERT_THAT(&Cache
.getRegister(X86::AX
), &Cache
.getRegister(X86::AX
));
67 ASSERT_THAT(&Cache
.getRegisterClass(X86::GR8_ABCD_LRegClassID
),
68 &Cache
.getRegisterClass(X86::GR8_ABCD_LRegClassID
));
72 } // namespace exegesis