LLVM 20.0.0git
LiveRegMatrix.h
Go to the documentation of this file.
1//===- LiveRegMatrix.h - Track register interference ----------*- 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// The LiveRegMatrix analysis pass keeps track of virtual register interference
10// along two dimensions: Slot indexes and register units. The matrix is used by
11// register allocators to ensure that no interfering virtual registers get
12// assigned to overlapping physical registers.
13//
14// Register units are defined in MCRegisterInfo.h, they represent the smallest
15// unit of interference when dealing with overlapping physical registers. The
16// LiveRegMatrix is represented as a LiveIntervalUnion per register unit. When
17// a virtual register is assigned to a physical register, the live range for
18// the virtual register is inserted into the LiveIntervalUnion for each regunit
19// in the physreg.
20//
21//===----------------------------------------------------------------------===//
22
23#ifndef LLVM_CODEGEN_LIVEREGMATRIX_H
24#define LLVM_CODEGEN_LIVEREGMATRIX_H
25
26#include "llvm/ADT/BitVector.h"
29#include <memory>
30
31namespace llvm {
32
33class AnalysisUsage;
34class LiveInterval;
35class LiveIntervals;
36class MachineFunction;
37class TargetRegisterInfo;
38class VirtRegMap;
39
43 const TargetRegisterInfo *TRI = nullptr;
44 LiveIntervals *LIS = nullptr;
45 VirtRegMap *VRM = nullptr;
46
47 // UserTag changes whenever virtual registers have been modified.
48 unsigned UserTag = 0;
49
50 // The matrix is represented as a LiveIntervalUnion per register unit.
53
54 // Cached queries per register unit.
55 std::unique_ptr<LiveIntervalUnion::Query[]> Queries;
56
57 // Cached register mask interference info.
58 unsigned RegMaskTag = 0;
59 unsigned RegMaskVirtReg = 0;
60 BitVector RegMaskUsable;
61
62 LiveRegMatrix() = default;
63 void releaseMemory();
64
65public:
67 : TRI(Other.TRI), LIS(Other.LIS), VRM(Other.VRM), UserTag(Other.UserTag),
68 Matrix(std::move(Other.Matrix)), Queries(std::move(Other.Queries)),
69 RegMaskTag(Other.RegMaskTag), RegMaskVirtReg(Other.RegMaskVirtReg),
70 RegMaskUsable(std::move(Other.RegMaskUsable)) {}
71
72 void init(MachineFunction &MF, LiveIntervals &LIS, VirtRegMap &VRM);
73
74 //===--------------------------------------------------------------------===//
75 // High-level interface.
76 //===--------------------------------------------------------------------===//
77 //
78 // Check for interference before assigning virtual registers to physical
79 // registers.
80 //
81
82 /// Invalidate cached interference queries after modifying virtual register
83 /// live ranges. Interference checks may return stale information unless
84 /// caches are invalidated.
85 void invalidateVirtRegs() { ++UserTag; }
86
88 /// No interference, go ahead and assign.
90
91 /// Virtual register interference. There are interfering virtual registers
92 /// assigned to PhysReg or its aliases. This interference could be resolved
93 /// by unassigning those other virtual registers.
95
96 /// Register unit interference. A fixed live range is in the way, typically
97 /// argument registers for a call. This can't be resolved by unassigning
98 /// other virtual registers.
100
101 /// RegMask interference. The live range is crossing an instruction with a
102 /// regmask operand that doesn't preserve PhysReg. This typically means
103 /// VirtReg is live across a call, and PhysReg isn't call-preserved.
105 };
106
107 /// Check for interference before assigning VirtReg to PhysReg.
108 /// If this function returns IK_Free, it is legal to assign(VirtReg, PhysReg).
109 /// When there is more than one kind of interference, the InterferenceKind
110 /// with the highest enum value is returned.
112 MCRegister PhysReg);
113
114 /// Check for interference in the segment [Start, End) that may prevent
115 /// assignment to PhysReg. If this function returns true, there is
116 /// interference in the segment [Start, End) of some other interval already
117 /// assigned to PhysReg. If this function returns false, PhysReg is free at
118 /// the segment [Start, End).
119 bool checkInterference(SlotIndex Start, SlotIndex End, MCRegister PhysReg);
120
121 /// Check for interference in the segment [Start, End) that may prevent
122 /// assignment to PhysReg, like checkInterference. Returns a lane mask of
123 /// which lanes of the physical register interfere in the segment [Start, End)
124 /// of some other interval already assigned to PhysReg.
125 ///
126 /// If this function returns LaneBitmask::getNone(), PhysReg is completely
127 /// free at the segment [Start, End).
129 MCRegister PhysReg);
130
131 /// Assign VirtReg to PhysReg.
132 /// This will mark VirtReg's live range as occupied in the LiveRegMatrix and
133 /// update VirtRegMap. The live range is expected to be available in PhysReg.
134 void assign(const LiveInterval &VirtReg, MCRegister PhysReg);
135
136 /// Unassign VirtReg from its PhysReg.
137 /// Assuming that VirtReg was previously assigned to a PhysReg, this undoes
138 /// the assignment and updates VirtRegMap accordingly.
139 void unassign(const LiveInterval &VirtReg);
140
141 /// Returns true if the given \p PhysReg has any live intervals assigned.
142 bool isPhysRegUsed(MCRegister PhysReg) const;
143
144 //===--------------------------------------------------------------------===//
145 // Low-level interface.
146 //===--------------------------------------------------------------------===//
147 //
148 // Provide access to the underlying LiveIntervalUnions.
149 //
150
151 /// Check for regmask interference only.
152 /// Return true if VirtReg crosses a regmask operand that clobbers PhysReg.
153 /// If PhysReg is null, check if VirtReg crosses any regmask operands.
154 bool checkRegMaskInterference(const LiveInterval &VirtReg,
156
157 /// Check for regunit interference only.
158 /// Return true if VirtReg overlaps a fixed assignment of one of PhysRegs's
159 /// register units.
160 bool checkRegUnitInterference(const LiveInterval &VirtReg,
161 MCRegister PhysReg);
162
163 /// Query a line of the assigned virtual register matrix directly.
164 /// Use MCRegUnitIterator to enumerate all regunits in the desired PhysReg.
165 /// This returns a reference to an internal Query data structure that is only
166 /// valid until the next query() call.
168
169 /// Directly access the live interval unions per regunit.
170 /// This returns an array indexed by the regunit number.
172
173 Register getOneVReg(unsigned PhysReg) const;
174};
175
177 LiveRegMatrix LRM;
178
179public:
180 static char ID;
181
183
184 LiveRegMatrix &getLRM() { return LRM; }
185 const LiveRegMatrix &getLRM() const { return LRM; }
186
187 void getAnalysisUsage(AnalysisUsage &AU) const override;
188 bool runOnMachineFunction(MachineFunction &MF) override;
189 void releaseMemory() override;
190};
191
192class LiveRegMatrixAnalysis : public AnalysisInfoMixin<LiveRegMatrixAnalysis> {
194 static AnalysisKey Key;
195
196public:
198
200};
201
202} // end namespace llvm
203
204#endif // LLVM_CODEGEN_LIVEREGMATRIX_H
This file implements the BitVector class.
bool End
Definition: ELF_riscv.cpp:480
Live Register Matrix
unsigned const TargetRegisterInfo * TRI
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
Represent the analysis usage information of a pass.
Query interferences between a single live virtual register and a live interval union.
Union of live intervals that are strong candidates for coalescing into a single register (either phys...
LiveSegments::Allocator Allocator
LiveInterval - This class represents the liveness of a register, or stack slot.
Definition: LiveInterval.h:687
This class represents the liveness of a register, stack slot, etc.
Definition: LiveInterval.h:157
LiveRegMatrix run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
const LiveRegMatrix & getLRM() const
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
bool checkRegMaskInterference(const LiveInterval &VirtReg, MCRegister PhysReg=MCRegister::NoRegister)
Check for regmask interference only.
void unassign(const LiveInterval &VirtReg)
Unassign VirtReg from its PhysReg.
LiveIntervalUnion::Query & query(const LiveRange &LR, MCRegister RegUnit)
Query a line of the assigned virtual register matrix directly.
bool isPhysRegUsed(MCRegister PhysReg) const
Returns true if the given PhysReg has any live intervals assigned.
void invalidateVirtRegs()
Invalidate cached interference queries after modifying virtual register live ranges.
Definition: LiveRegMatrix.h:85
Register getOneVReg(unsigned PhysReg) const
@ IK_VirtReg
Virtual register interference.
Definition: LiveRegMatrix.h:94
@ IK_RegUnit
Register unit interference.
Definition: LiveRegMatrix.h:99
@ IK_Free
No interference, go ahead and assign.
Definition: LiveRegMatrix.h:89
@ IK_RegMask
RegMask interference.
void init(MachineFunction &MF, LiveIntervals &LIS, VirtRegMap &VRM)
void assign(const LiveInterval &VirtReg, MCRegister PhysReg)
Assign VirtReg to PhysReg.
InterferenceKind checkInterference(const LiveInterval &VirtReg, MCRegister PhysReg)
Check for interference before assigning VirtReg to PhysReg.
bool checkRegUnitInterference(const LiveInterval &VirtReg, MCRegister PhysReg)
Check for regunit interference only.
LiveIntervalUnion * getLiveUnions()
Directly access the live interval unions per regunit.
LiveRegMatrix(LiveRegMatrix &&Other)
Definition: LiveRegMatrix.h:66
LaneBitmask checkInterferenceLanes(SlotIndex Start, SlotIndex End, MCRegister PhysReg)
Check for interference in the segment [Start, End) that may prevent assignment to PhysReg,...
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
static constexpr unsigned NoRegister
Definition: MCRegister.h:52
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
SlotIndex - An opaque wrapper around machine indexes.
Definition: SlotIndexes.h:65
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Other
Any other memory.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1873
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:92
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28