1 //===- LiveRegMatrix.cpp - Track register interference --------------------===//
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 //===----------------------------------------------------------------------===//
9 // This file defines the LiveRegMatrix analysis pass.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/CodeGen/LiveRegMatrix.h"
14 #include "RegisterCoalescer.h"
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/CodeGen/LiveInterval.h"
17 #include "llvm/CodeGen/LiveIntervalUnion.h"
18 #include "llvm/CodeGen/LiveIntervals.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/TargetRegisterInfo.h"
21 #include "llvm/CodeGen/TargetSubtargetInfo.h"
22 #include "llvm/CodeGen/VirtRegMap.h"
23 #include "llvm/MC/LaneBitmask.h"
24 #include "llvm/MC/MCRegisterInfo.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/raw_ostream.h"
32 #define DEBUG_TYPE "regalloc"
34 STATISTIC(NumAssigned
, "Number of registers assigned");
35 STATISTIC(NumUnassigned
, "Number of registers unassigned");
37 char LiveRegMatrix::ID
= 0;
38 INITIALIZE_PASS_BEGIN(LiveRegMatrix
, "liveregmatrix",
39 "Live Register Matrix", false, false)
40 INITIALIZE_PASS_DEPENDENCY(LiveIntervals
)
41 INITIALIZE_PASS_DEPENDENCY(VirtRegMap
)
42 INITIALIZE_PASS_END(LiveRegMatrix
, "liveregmatrix",
43 "Live Register Matrix", false, false)
45 LiveRegMatrix::LiveRegMatrix() : MachineFunctionPass(ID
) {}
47 void LiveRegMatrix::getAnalysisUsage(AnalysisUsage
&AU
) const {
49 AU
.addRequiredTransitive
<LiveIntervals
>();
50 AU
.addRequiredTransitive
<VirtRegMap
>();
51 MachineFunctionPass::getAnalysisUsage(AU
);
54 bool LiveRegMatrix::runOnMachineFunction(MachineFunction
&MF
) {
55 TRI
= MF
.getSubtarget().getRegisterInfo();
56 LIS
= &getAnalysis
<LiveIntervals
>();
57 VRM
= &getAnalysis
<VirtRegMap
>();
59 unsigned NumRegUnits
= TRI
->getNumRegUnits();
60 if (NumRegUnits
!= Matrix
.size())
61 Queries
.reset(new LiveIntervalUnion::Query
[NumRegUnits
]);
62 Matrix
.init(LIUAlloc
, NumRegUnits
);
64 // Make sure no stale queries get reused.
69 void LiveRegMatrix::releaseMemory() {
70 for (unsigned i
= 0, e
= Matrix
.size(); i
!= e
; ++i
) {
72 // No need to clear Queries here, since LiveIntervalUnion::Query doesn't
73 // have anything important to clear and LiveRegMatrix's runOnFunction()
74 // does a std::unique_ptr::reset anyways.
78 template <typename Callable
>
79 static bool foreachUnit(const TargetRegisterInfo
*TRI
,
80 LiveInterval
&VRegInterval
, unsigned PhysReg
,
82 if (VRegInterval
.hasSubRanges()) {
83 for (MCRegUnitMaskIterator
Units(PhysReg
, TRI
); Units
.isValid(); ++Units
) {
84 unsigned Unit
= (*Units
).first
;
85 LaneBitmask Mask
= (*Units
).second
;
86 for (LiveInterval::SubRange
&S
: VRegInterval
.subranges()) {
87 if ((S
.LaneMask
& Mask
).any()) {
95 for (MCRegUnitIterator
Units(PhysReg
, TRI
); Units
.isValid(); ++Units
) {
96 if (Func(*Units
, VRegInterval
))
103 void LiveRegMatrix::assign(LiveInterval
&VirtReg
, unsigned PhysReg
) {
104 LLVM_DEBUG(dbgs() << "assigning " << printReg(VirtReg
.reg
, TRI
) << " to "
105 << printReg(PhysReg
, TRI
) << ':');
106 assert(!VRM
->hasPhys(VirtReg
.reg
) && "Duplicate VirtReg assignment");
107 VRM
->assignVirt2Phys(VirtReg
.reg
, PhysReg
);
110 TRI
, VirtReg
, PhysReg
, [&](unsigned Unit
, const LiveRange
&Range
) {
111 LLVM_DEBUG(dbgs() << ' ' << printRegUnit(Unit
, TRI
) << ' ' << Range
);
112 Matrix
[Unit
].unify(VirtReg
, Range
);
117 LLVM_DEBUG(dbgs() << '\n');
120 void LiveRegMatrix::unassign(LiveInterval
&VirtReg
) {
121 Register PhysReg
= VRM
->getPhys(VirtReg
.reg
);
122 LLVM_DEBUG(dbgs() << "unassigning " << printReg(VirtReg
.reg
, TRI
) << " from "
123 << printReg(PhysReg
, TRI
) << ':');
124 VRM
->clearVirt(VirtReg
.reg
);
126 foreachUnit(TRI
, VirtReg
, PhysReg
,
127 [&](unsigned Unit
, const LiveRange
&Range
) {
128 LLVM_DEBUG(dbgs() << ' ' << printRegUnit(Unit
, TRI
));
129 Matrix
[Unit
].extract(VirtReg
, Range
);
134 LLVM_DEBUG(dbgs() << '\n');
137 bool LiveRegMatrix::isPhysRegUsed(unsigned PhysReg
) const {
138 for (MCRegUnitIterator
Unit(PhysReg
, TRI
); Unit
.isValid(); ++Unit
) {
139 if (!Matrix
[*Unit
].empty())
145 bool LiveRegMatrix::checkRegMaskInterference(LiveInterval
&VirtReg
,
147 // Check if the cached information is valid.
148 // The same BitVector can be reused for all PhysRegs.
149 // We could cache multiple VirtRegs if it becomes necessary.
150 if (RegMaskVirtReg
!= VirtReg
.reg
|| RegMaskTag
!= UserTag
) {
151 RegMaskVirtReg
= VirtReg
.reg
;
152 RegMaskTag
= UserTag
;
153 RegMaskUsable
.clear();
154 LIS
->checkRegMaskInterference(VirtReg
, RegMaskUsable
);
157 // The BitVector is indexed by PhysReg, not register unit.
158 // Regmask interference is more fine grained than regunits.
159 // For example, a Win64 call can clobber %ymm8 yet preserve %xmm8.
160 return !RegMaskUsable
.empty() && (!PhysReg
|| !RegMaskUsable
.test(PhysReg
));
163 bool LiveRegMatrix::checkRegUnitInterference(LiveInterval
&VirtReg
,
167 CoalescerPair
CP(VirtReg
.reg
, PhysReg
, *TRI
);
169 bool Result
= foreachUnit(TRI
, VirtReg
, PhysReg
, [&](unsigned Unit
,
170 const LiveRange
&Range
) {
171 const LiveRange
&UnitRange
= LIS
->getRegUnit(Unit
);
172 return Range
.overlaps(UnitRange
, CP
, *LIS
->getSlotIndexes());
177 LiveIntervalUnion::Query
&LiveRegMatrix::query(const LiveRange
&LR
,
179 LiveIntervalUnion::Query
&Q
= Queries
[RegUnit
];
180 Q
.init(UserTag
, LR
, Matrix
[RegUnit
]);
184 LiveRegMatrix::InterferenceKind
185 LiveRegMatrix::checkInterference(LiveInterval
&VirtReg
, unsigned PhysReg
) {
189 // Regmask interference is the fastest check.
190 if (checkRegMaskInterference(VirtReg
, PhysReg
))
193 // Check for fixed interference.
194 if (checkRegUnitInterference(VirtReg
, PhysReg
))
197 // Check the matrix for virtual register interference.
198 bool Interference
= foreachUnit(TRI
, VirtReg
, PhysReg
,
199 [&](unsigned Unit
, const LiveRange
&LR
) {
200 return query(LR
, Unit
).checkInterference();
208 bool LiveRegMatrix::checkInterference(SlotIndex Start
, SlotIndex End
,
210 // Construct artificial live range containing only one segment [Start, End).
211 VNInfo
valno(0, Start
);
212 LiveRange::Segment
Seg(Start
, End
, &valno
);
216 // Check for interference with that segment
217 for (MCRegUnitIterator
Units(PhysReg
, TRI
); Units
.isValid(); ++Units
) {
218 if (query(LR
, *Units
).checkInterference())