• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- RegisterAliasingTracker.h -------------------------------*- 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 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// Defines classes to keep track of register aliasing.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_ALIASINGTRACKER_H
16 #define LLVM_TOOLS_LLVM_EXEGESIS_ALIASINGTRACKER_H
17 
18 #include <memory>
19 #include <unordered_map>
20 
21 #include "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/PackedVector.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 
25 namespace exegesis {
26 
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);
30 
31 // Keeps track of a mapping from one register (or a register class) to its
32 // aliased registers.
33 //
34 // e.g.
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);
46 
47   // Construct a tracker from an MCPhysReg.
48   RegisterAliasingTracker(const llvm::MCRegisterInfo &RegInfo,
49                           const llvm::MCPhysReg Register);
50 
sourceBitsRegisterAliasingTracker51   const llvm::BitVector &sourceBits() const { return SourceBits; }
52 
53   // Retrieves all the touched registers as a BitVector.
aliasedBitsRegisterAliasingTracker54   const llvm::BitVector &aliasedBits() const { return AliasedBits; }
55 
56   // Returns the origin of this register or -1.
getOriginRegisterAliasingTracker57   int getOrigin(llvm::MCPhysReg Aliased) const {
58     if (!AliasedBits[Aliased])
59       return -1;
60     return Origins[Aliased];
61   }
62 
63 private:
64   RegisterAliasingTracker(const llvm::MCRegisterInfo &RegInfo);
65 
66   void FillOriginAndAliasedBits(const llvm::MCRegisterInfo &RegInfo,
67                                 const llvm::BitVector &OriginalBits);
68 
69   llvm::BitVector SourceBits;
70   llvm::BitVector AliasedBits;
71   llvm::PackedVector<size_t, 10> Origins; // Max 1024 physical registers.
72 };
73 
74 // A cache of existing trackers.
75 struct RegisterAliasingTrackerCache {
76   // RegInfo must outlive the cache.
77   RegisterAliasingTrackerCache(const llvm::MCRegisterInfo &RegInfo,
78                                const llvm::BitVector &ReservedReg);
79 
80   // Convenient function to retrieve a BitVector of the right size.
emptyRegistersRegisterAliasingTrackerCache81   const llvm::BitVector &emptyRegisters() const { return EmptyRegisters; }
82 
83   // Convenient function to retrieve the registers the function body can't use.
reservedRegistersRegisterAliasingTrackerCache84   const llvm::BitVector &reservedRegisters() const { return ReservedReg; }
85 
86   // Convenient function to retrieve the underlying MCRegInfo.
regInfoRegisterAliasingTrackerCache87   const llvm::MCRegisterInfo &regInfo() const { return RegInfo; }
88 
89   // Retrieves the RegisterAliasingTracker for this particular register.
90   const RegisterAliasingTracker &getRegister(llvm::MCPhysReg Reg) const;
91 
92   // Retrieves the RegisterAliasingTracker for this particular register class.
93   const RegisterAliasingTracker &getRegisterClass(unsigned RegClassIndex) const;
94 
95 private:
96   const llvm::MCRegisterInfo &RegInfo;
97   const llvm::BitVector ReservedReg;
98   const llvm::BitVector EmptyRegisters;
99   mutable std::unordered_map<unsigned, std::unique_ptr<RegisterAliasingTracker>>
100       Registers;
101   mutable std::unordered_map<unsigned, std::unique_ptr<RegisterAliasingTracker>>
102       RegisterClasses;
103 };
104 
105 } // namespace exegesis
106 
107 #endif // LLVM_TOOLS_LLVM_EXEGESIS_ALIASINGTRACKER_H
108