blob: 7a51386aa9caaaa7cc22d3fb3aadc8cced7d92b4 [file] [log] [blame]
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +00001//===-- LiveRegMatrix.cpp - Track register interference -------------------===//
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// This file defines the LiveRegMatrix analysis pass.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/CodeGen/LiveRegMatrix.h"
Jakob Stoklund Olesen866908c2012-09-06 18:15:23 +000015#include "RegisterCoalescer.h"
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000016#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000017#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000018#include "llvm/CodeGen/VirtRegMap.h"
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000019#include "llvm/Support/Debug.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000020#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Target/TargetRegisterInfo.h"
Matthias Braun9912bb82015-07-14 17:52:07 +000022#include "llvm/Target/TargetSubtargetInfo.h"
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000023
24using namespace llvm;
25
Chandler Carruth1b9dde02014-04-22 02:02:50 +000026#define DEBUG_TYPE "regalloc"
27
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000028STATISTIC(NumAssigned , "Number of registers assigned");
29STATISTIC(NumUnassigned , "Number of registers unassigned");
30
31char LiveRegMatrix::ID = 0;
32INITIALIZE_PASS_BEGIN(LiveRegMatrix, "liveregmatrix",
33 "Live Register Matrix", false, false)
34INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
35INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
36INITIALIZE_PASS_END(LiveRegMatrix, "liveregmatrix",
37 "Live Register Matrix", false, false)
38
39LiveRegMatrix::LiveRegMatrix() : MachineFunctionPass(ID),
40 UserTag(0), RegMaskTag(0), RegMaskVirtReg(0) {}
41
42void LiveRegMatrix::getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.setPreservesAll();
44 AU.addRequiredTransitive<LiveIntervals>();
45 AU.addRequiredTransitive<VirtRegMap>();
46 MachineFunctionPass::getAnalysisUsage(AU);
47}
48
49bool LiveRegMatrix::runOnMachineFunction(MachineFunction &MF) {
Eric Christopherfc6de422014-08-05 02:39:49 +000050 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000051 LIS = &getAnalysis<LiveIntervals>();
52 VRM = &getAnalysis<VirtRegMap>();
53
54 unsigned NumRegUnits = TRI->getNumRegUnits();
55 if (NumRegUnits != Matrix.size())
56 Queries.reset(new LiveIntervalUnion::Query[NumRegUnits]);
57 Matrix.init(LIUAlloc, NumRegUnits);
58
59 // Make sure no stale queries get reused.
60 invalidateVirtRegs();
61 return false;
62}
63
64void LiveRegMatrix::releaseMemory() {
65 for (unsigned i = 0, e = Matrix.size(); i != e; ++i) {
66 Matrix[i].clear();
Puyan Lotfi12ae04b2014-02-06 08:42:01 +000067 // No need to clear Queries here, since LiveIntervalUnion::Query doesn't
68 // have anything important to clear and LiveRegMatrix's runOnFunction()
Ahmed Charles56440fd2014-03-06 05:51:42 +000069 // does a std::unique_ptr::reset anyways.
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000070 }
71}
72
Benjamin Kramerb7d33112016-08-06 11:13:10 +000073template <typename Callable>
74static bool foreachUnit(const TargetRegisterInfo *TRI,
75 LiveInterval &VRegInterval, unsigned PhysReg,
76 Callable Func) {
Matthias Braun587e2742014-12-10 01:13:01 +000077 if (VRegInterval.hasSubRanges()) {
78 for (MCRegUnitMaskIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
79 unsigned Unit = (*Units).first;
Matthias Braune6a24852015-09-25 21:51:14 +000080 LaneBitmask Mask = (*Units).second;
Matthias Braun09afa1e2014-12-11 00:59:06 +000081 for (LiveInterval::SubRange &S : VRegInterval.subranges()) {
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +000082 if ((S.LaneMask & Mask).any()) {
Matthias Braun09afa1e2014-12-11 00:59:06 +000083 if (Func(Unit, S))
Matthias Braun587e2742014-12-10 01:13:01 +000084 return true;
85 break;
86 }
87 }
88 }
89 } else {
90 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
91 if (Func(*Units, VRegInterval))
92 return true;
93 }
94 }
95 return false;
96}
97
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000098void LiveRegMatrix::assign(LiveInterval &VirtReg, unsigned PhysReg) {
99 DEBUG(dbgs() << "assigning " << PrintReg(VirtReg.reg, TRI)
100 << " to " << PrintReg(PhysReg, TRI) << ':');
101 assert(!VRM->hasPhys(VirtReg.reg) && "Duplicate VirtReg assignment");
102 VRM->assignVirt2Phys(VirtReg.reg, PhysReg);
Matthias Braun587e2742014-12-10 01:13:01 +0000103
104 foreachUnit(TRI, VirtReg, PhysReg, [&](unsigned Unit,
105 const LiveRange &Range) {
106 DEBUG(dbgs() << ' ' << PrintRegUnit(Unit, TRI) << ' ' << Range);
107 Matrix[Unit].unify(VirtReg, Range);
108 return false;
109 });
110
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +0000111 ++NumAssigned;
112 DEBUG(dbgs() << '\n');
113}
114
115void LiveRegMatrix::unassign(LiveInterval &VirtReg) {
116 unsigned PhysReg = VRM->getPhys(VirtReg.reg);
117 DEBUG(dbgs() << "unassigning " << PrintReg(VirtReg.reg, TRI)
118 << " from " << PrintReg(PhysReg, TRI) << ':');
119 VRM->clearVirt(VirtReg.reg);
Matthias Braun587e2742014-12-10 01:13:01 +0000120
121 foreachUnit(TRI, VirtReg, PhysReg, [&](unsigned Unit,
122 const LiveRange &Range) {
123 DEBUG(dbgs() << ' ' << PrintRegUnit(Unit, TRI));
124 Matrix[Unit].extract(VirtReg, Range);
125 return false;
126 });
127
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +0000128 ++NumUnassigned;
129 DEBUG(dbgs() << '\n');
130}
131
Matthias Braun953393a2015-07-14 17:38:17 +0000132bool LiveRegMatrix::isPhysRegUsed(unsigned PhysReg) const {
133 for (MCRegUnitIterator Unit(PhysReg, TRI); Unit.isValid(); ++Unit) {
134 if (!Matrix[*Unit].empty())
135 return true;
136 }
137 return false;
138}
139
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +0000140bool LiveRegMatrix::checkRegMaskInterference(LiveInterval &VirtReg,
141 unsigned PhysReg) {
142 // Check if the cached information is valid.
143 // The same BitVector can be reused for all PhysRegs.
144 // We could cache multiple VirtRegs if it becomes necessary.
145 if (RegMaskVirtReg != VirtReg.reg || RegMaskTag != UserTag) {
146 RegMaskVirtReg = VirtReg.reg;
147 RegMaskTag = UserTag;
148 RegMaskUsable.clear();
149 LIS->checkRegMaskInterference(VirtReg, RegMaskUsable);
150 }
151
152 // The BitVector is indexed by PhysReg, not register unit.
153 // Regmask interference is more fine grained than regunits.
154 // For example, a Win64 call can clobber %ymm8 yet preserve %xmm8.
Jakob Stoklund Olesen13dffcb2012-06-15 22:24:22 +0000155 return !RegMaskUsable.empty() && (!PhysReg || !RegMaskUsable.test(PhysReg));
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +0000156}
157
158bool LiveRegMatrix::checkRegUnitInterference(LiveInterval &VirtReg,
159 unsigned PhysReg) {
160 if (VirtReg.empty())
161 return false;
Jakob Stoklund Olesen866908c2012-09-06 18:15:23 +0000162 CoalescerPair CP(VirtReg.reg, PhysReg, *TRI);
Matthias Braun587e2742014-12-10 01:13:01 +0000163
164 bool Result = foreachUnit(TRI, VirtReg, PhysReg, [&](unsigned Unit,
165 const LiveRange &Range) {
166 const LiveRange &UnitRange = LIS->getRegUnit(Unit);
167 return Range.overlaps(UnitRange, CP, *LIS->getSlotIndexes());
168 });
169 return Result;
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +0000170}
171
172LiveIntervalUnion::Query &LiveRegMatrix::query(LiveInterval &VirtReg,
173 unsigned RegUnit) {
174 LiveIntervalUnion::Query &Q = Queries[RegUnit];
175 Q.init(UserTag, &VirtReg, &Matrix[RegUnit]);
176 return Q;
177}
178
179LiveRegMatrix::InterferenceKind
180LiveRegMatrix::checkInterference(LiveInterval &VirtReg, unsigned PhysReg) {
181 if (VirtReg.empty())
182 return IK_Free;
183
184 // Regmask interference is the fastest check.
185 if (checkRegMaskInterference(VirtReg, PhysReg))
186 return IK_RegMask;
187
188 // Check for fixed interference.
189 if (checkRegUnitInterference(VirtReg, PhysReg))
190 return IK_RegUnit;
191
192 // Check the matrix for virtual register interference.
193 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
194 if (query(VirtReg, *Units).checkInterference())
195 return IK_VirtReg;
196
197 return IK_Free;
198}