1 //===-- RegisterAliasingTracker.h -------------------------------*- 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 /// Defines classes to keep track of register aliasing.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_ALIASINGTRACKER_H
15 #define LLVM_TOOLS_LLVM_EXEGESIS_ALIASINGTRACKER_H
18 #include <unordered_map>
20 #include "llvm/ADT/BitVector.h"
21 #include "llvm/ADT/PackedVector.h"
22 #include "llvm/MC/MCRegisterInfo.h"
27 // Returns the registers that are aliased by the ones set in SourceBits.
28 llvm::BitVector
getAliasedBits(const llvm::MCRegisterInfo
&RegInfo
,
29 const llvm::BitVector
&SourceBits
);
31 // Keeps track of a mapping from one register (or a register class) to its
35 // RegisterAliasingTracker Tracker(RegInfo, llvm::X86::EAX);
36 // Tracker.sourceBits() == { llvm::X86::EAX }
37 // Tracker.aliasedBits() == { llvm::X86::AL, llvm::X86::AH, llvm::X86::AX,
38 // llvm::X86::EAX,llvm::X86::HAX, llvm::X86::RAX }
39 // Tracker.getOrigin(llvm::X86::AL) == llvm::X86::EAX;
40 // Tracker.getOrigin(llvm::X86::BX) == -1;
41 struct RegisterAliasingTracker
{
42 // Construct a tracker from an MCRegisterClass.
43 RegisterAliasingTracker(const llvm::MCRegisterInfo
&RegInfo
,
44 const llvm::BitVector
&ReservedReg
,
45 const llvm::MCRegisterClass
&RegClass
);
47 // Construct a tracker from an MCPhysReg.
48 RegisterAliasingTracker(const llvm::MCRegisterInfo
&RegInfo
,
49 const llvm::MCPhysReg Register
);
51 const llvm::BitVector
&sourceBits() const { return SourceBits
; }
53 // Retrieves all the touched registers as a BitVector.
54 const llvm::BitVector
&aliasedBits() const { return AliasedBits
; }
56 // Returns the origin of this register or -1.
57 int getOrigin(llvm::MCPhysReg Aliased
) const {
58 if (!AliasedBits
[Aliased
])
60 return Origins
[Aliased
];
64 RegisterAliasingTracker(const llvm::MCRegisterInfo
&RegInfo
);
65 RegisterAliasingTracker(const RegisterAliasingTracker
&) = delete;
67 void FillOriginAndAliasedBits(const llvm::MCRegisterInfo
&RegInfo
,
68 const llvm::BitVector
&OriginalBits
);
70 llvm::BitVector SourceBits
;
71 llvm::BitVector AliasedBits
;
72 llvm::PackedVector
<size_t, 10> Origins
; // Max 1024 physical registers.
75 // A cache of existing trackers.
76 struct RegisterAliasingTrackerCache
{
77 // RegInfo must outlive the cache.
78 RegisterAliasingTrackerCache(const llvm::MCRegisterInfo
&RegInfo
,
79 const llvm::BitVector
&ReservedReg
);
81 // Convenient function to retrieve a BitVector of the right size.
82 const llvm::BitVector
&emptyRegisters() const { return EmptyRegisters
; }
84 // Convenient function to retrieve the registers the function body can't use.
85 const llvm::BitVector
&reservedRegisters() const { return ReservedReg
; }
87 // Convenient function to retrieve the underlying MCRegInfo.
88 const llvm::MCRegisterInfo
®Info() const { return RegInfo
; }
90 // Retrieves the RegisterAliasingTracker for this particular register.
91 const RegisterAliasingTracker
&getRegister(llvm::MCPhysReg Reg
) const;
93 // Retrieves the RegisterAliasingTracker for this particular register class.
94 const RegisterAliasingTracker
&getRegisterClass(unsigned RegClassIndex
) const;
97 const llvm::MCRegisterInfo
&RegInfo
;
98 const llvm::BitVector ReservedReg
;
99 const llvm::BitVector EmptyRegisters
;
100 mutable std::unordered_map
<unsigned, std::unique_ptr
<RegisterAliasingTracker
>>
102 mutable std::unordered_map
<unsigned, std::unique_ptr
<RegisterAliasingTracker
>>
106 } // namespace exegesis
109 #endif // LLVM_TOOLS_LLVM_EXEGESIS_ALIASINGTRACKER_H