1 //===- LiveRegMatrix.cpp - Track register interference --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the LiveRegMatrix analysis pass.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/LiveRegMatrix.h"
15 #include "RegisterCoalescer.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/CodeGen/LiveInterval.h"
18 #include "llvm/CodeGen/LiveIntervalUnion.h"
19 #include "llvm/CodeGen/LiveIntervals.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/TargetRegisterInfo.h"
22 #include "llvm/CodeGen/TargetSubtargetInfo.h"
23 #include "llvm/CodeGen/VirtRegMap.h"
24 #include "llvm/MC/LaneBitmask.h"
25 #include "llvm/MC/MCRegisterInfo.h"
26 #include "llvm/Pass.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/raw_ostream.h"
33 #define DEBUG_TYPE "regalloc"
35 STATISTIC(NumAssigned , "Number of registers assigned");
36 STATISTIC(NumUnassigned , "Number of registers unassigned");
38 char LiveRegMatrix::ID = 0;
39 INITIALIZE_PASS_BEGIN(LiveRegMatrix, "liveregmatrix",
40 "Live Register Matrix", false, false)
41 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
42 INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
43 INITIALIZE_PASS_END(LiveRegMatrix, "liveregmatrix",
44 "Live Register Matrix", false, false)
46 LiveRegMatrix::LiveRegMatrix() : MachineFunctionPass(ID) {}
48 void LiveRegMatrix::getAnalysisUsage(AnalysisUsage &AU) const {
50 AU.addRequiredTransitive<LiveIntervals>();
51 AU.addRequiredTransitive<VirtRegMap>();
52 MachineFunctionPass::getAnalysisUsage(AU);
55 bool LiveRegMatrix::runOnMachineFunction(MachineFunction &MF) {
56 TRI = MF.getSubtarget().getRegisterInfo();
57 LIS = &getAnalysis<LiveIntervals>();
58 VRM = &getAnalysis<VirtRegMap>();
60 unsigned NumRegUnits = TRI->getNumRegUnits();
61 if (NumRegUnits != Matrix.size())
62 Queries.reset(new LiveIntervalUnion::Query[NumRegUnits]);
63 Matrix.init(LIUAlloc, NumRegUnits);
65 // Make sure no stale queries get reused.
70 void LiveRegMatrix::releaseMemory() {
71 for (unsigned i = 0, e = Matrix.size(); i != e; ++i) {
73 // No need to clear Queries here, since LiveIntervalUnion::Query doesn't
74 // have anything important to clear and LiveRegMatrix's runOnFunction()
75 // does a std::unique_ptr::reset anyways.
79 template <typename Callable>
80 static bool foreachUnit(const TargetRegisterInfo *TRI,
81 LiveInterval &VRegInterval, unsigned PhysReg,
83 if (VRegInterval.hasSubRanges()) {
84 for (MCRegUnitMaskIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
85 unsigned Unit = (*Units).first;
86 LaneBitmask Mask = (*Units).second;
87 for (LiveInterval::SubRange &S : VRegInterval.subranges()) {
88 if ((S.LaneMask & Mask).any()) {
96 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
97 if (Func(*Units, VRegInterval))
104 void LiveRegMatrix::assign(LiveInterval &VirtReg, unsigned PhysReg) {
105 LLVM_DEBUG(dbgs() << "assigning " << printReg(VirtReg.reg, TRI) << " to "
106 << printReg(PhysReg, TRI) << ':');
107 assert(!VRM->hasPhys(VirtReg.reg) && "Duplicate VirtReg assignment");
108 VRM->assignVirt2Phys(VirtReg.reg, PhysReg);
111 TRI, VirtReg, PhysReg, [&](unsigned Unit, const LiveRange &Range) {
112 LLVM_DEBUG(dbgs() << ' ' << printRegUnit(Unit, TRI) << ' ' << Range);
113 Matrix[Unit].unify(VirtReg, Range);
118 LLVM_DEBUG(dbgs() << '\n');
121 void LiveRegMatrix::unassign(LiveInterval &VirtReg) {
122 unsigned PhysReg = VRM->getPhys(VirtReg.reg);
123 LLVM_DEBUG(dbgs() << "unassigning " << printReg(VirtReg.reg, TRI) << " from "
124 << printReg(PhysReg, TRI) << ':');
125 VRM->clearVirt(VirtReg.reg);
127 foreachUnit(TRI, VirtReg, PhysReg,
128 [&](unsigned Unit, const LiveRange &Range) {
129 LLVM_DEBUG(dbgs() << ' ' << printRegUnit(Unit, TRI));
130 Matrix[Unit].extract(VirtReg, Range);
135 LLVM_DEBUG(dbgs() << '\n');
138 bool LiveRegMatrix::isPhysRegUsed(unsigned PhysReg) const {
139 for (MCRegUnitIterator Unit(PhysReg, TRI); Unit.isValid(); ++Unit) {
140 if (!Matrix[*Unit].empty())
146 bool LiveRegMatrix::checkRegMaskInterference(LiveInterval &VirtReg,
148 // Check if the cached information is valid.
149 // The same BitVector can be reused for all PhysRegs.
150 // We could cache multiple VirtRegs if it becomes necessary.
151 if (RegMaskVirtReg != VirtReg.reg || RegMaskTag != UserTag) {
152 RegMaskVirtReg = VirtReg.reg;
153 RegMaskTag = UserTag;
154 RegMaskUsable.clear();
155 LIS->checkRegMaskInterference(VirtReg, RegMaskUsable);
158 // The BitVector is indexed by PhysReg, not register unit.
159 // Regmask interference is more fine grained than regunits.
160 // For example, a Win64 call can clobber %ymm8 yet preserve %xmm8.
161 return !RegMaskUsable.empty() && (!PhysReg || !RegMaskUsable.test(PhysReg));
164 bool LiveRegMatrix::checkRegUnitInterference(LiveInterval &VirtReg,
168 CoalescerPair CP(VirtReg.reg, PhysReg, *TRI);
170 bool Result = foreachUnit(TRI, VirtReg, PhysReg, [&](unsigned Unit,
171 const LiveRange &Range) {
172 const LiveRange &UnitRange = LIS->getRegUnit(Unit);
173 return Range.overlaps(UnitRange, CP, *LIS->getSlotIndexes());
178 LiveIntervalUnion::Query &LiveRegMatrix::query(const LiveRange &LR,
180 LiveIntervalUnion::Query &Q = Queries[RegUnit];
181 Q.init(UserTag, LR, Matrix[RegUnit]);
185 LiveRegMatrix::InterferenceKind
186 LiveRegMatrix::checkInterference(LiveInterval &VirtReg, unsigned PhysReg) {
190 // Regmask interference is the fastest check.
191 if (checkRegMaskInterference(VirtReg, PhysReg))
194 // Check for fixed interference.
195 if (checkRegUnitInterference(VirtReg, PhysReg))
198 // Check the matrix for virtual register interference.
199 bool Interference = foreachUnit(TRI, VirtReg, PhysReg,
200 [&](unsigned Unit, const LiveRange &LR) {
201 return query(LR, Unit).checkInterference();
209 bool LiveRegMatrix::checkInterference(SlotIndex Start, SlotIndex End,
211 // Construct artificial live range containing only one segment [Start, End).
212 VNInfo valno(0, Start);
213 LiveRange::Segment Seg(Start, End, &valno);
217 // Check for interference with that segment
218 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
219 if (query(LR, *Units).checkInterference())