[llvm-exegesis] Cleaner design without mutable data.
[llvm-complete.git] / tools / llvm-exegesis / lib / RegisterAliasing.cpp
blob039f78db985faf0dd99a4e0011e0345f269a47f3
1 //===-- RegisterAliasingTracker.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 //===----------------------------------------------------------------------===//
10 #include "RegisterAliasing.h"
12 namespace exegesis {
14 llvm::BitVector getAliasedBits(const llvm::MCRegisterInfo &RegInfo,
15 const llvm::BitVector &SourceBits) {
16 llvm::BitVector AliasedBits(RegInfo.getNumRegs());
17 for (const size_t PhysReg : SourceBits.set_bits()) {
18 using RegAliasItr = llvm::MCRegAliasIterator;
19 for (auto Itr = RegAliasItr(PhysReg, &RegInfo, true); Itr.isValid();
20 ++Itr) {
21 AliasedBits.set(*Itr);
24 return AliasedBits;
27 RegisterAliasingTracker::RegisterAliasingTracker(
28 const llvm::MCRegisterInfo &RegInfo)
29 : SourceBits(RegInfo.getNumRegs()), AliasedBits(RegInfo.getNumRegs()),
30 Origins(RegInfo.getNumRegs()) {}
32 RegisterAliasingTracker::RegisterAliasingTracker(
33 const llvm::MCRegisterInfo &RegInfo, const llvm::BitVector &ReservedReg,
34 const llvm::MCRegisterClass &RegClass)
35 : RegisterAliasingTracker(RegInfo) {
36 for (llvm::MCPhysReg PhysReg : RegClass)
37 if (!ReservedReg[PhysReg]) // Removing reserved registers.
38 SourceBits.set(PhysReg);
39 FillOriginAndAliasedBits(RegInfo, SourceBits);
42 RegisterAliasingTracker::RegisterAliasingTracker(
43 const llvm::MCRegisterInfo &RegInfo, const llvm::MCPhysReg PhysReg)
44 : RegisterAliasingTracker(RegInfo) {
45 SourceBits.set(PhysReg);
46 FillOriginAndAliasedBits(RegInfo, SourceBits);
49 void RegisterAliasingTracker::FillOriginAndAliasedBits(
50 const llvm::MCRegisterInfo &RegInfo, const llvm::BitVector &SourceBits) {
51 using RegAliasItr = llvm::MCRegAliasIterator;
52 for (const size_t PhysReg : SourceBits.set_bits()) {
53 for (auto Itr = RegAliasItr(PhysReg, &RegInfo, true); Itr.isValid();
54 ++Itr) {
55 AliasedBits.set(*Itr);
56 Origins[*Itr] = PhysReg;
61 RegisterAliasingTrackerCache::RegisterAliasingTrackerCache(
62 const llvm::MCRegisterInfo &RegInfo, const llvm::BitVector &ReservedReg)
63 : RegInfo(RegInfo), ReservedReg(ReservedReg),
64 EmptyRegisters(RegInfo.getNumRegs()) {}
66 const RegisterAliasingTracker &
67 RegisterAliasingTrackerCache::getRegister(llvm::MCPhysReg PhysReg) const {
68 auto &Found = Registers[PhysReg];
69 if (!Found)
70 Found.reset(new RegisterAliasingTracker(RegInfo, PhysReg));
71 return *Found;
74 const RegisterAliasingTracker &
75 RegisterAliasingTrackerCache::getRegisterClass(unsigned RegClassIndex) const {
76 auto &Found = RegisterClasses[RegClassIndex];
77 const auto &RegClass = RegInfo.getRegClass(RegClassIndex);
78 if (!Found)
79 Found.reset(new RegisterAliasingTracker(RegInfo, ReservedReg, RegClass));
80 return *Found;
83 } // namespace exegesis