blob: e5ae855a9f68df4de7fbac6020d248ad896c5d1b [file] [log] [blame]
Eugene Zelenko5db84df2017-02-17 21:43:25 +00001//===- LiveRegMatrix.cpp - Track register interference --------------------===//
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +00002//
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
Jakob Stoklund Olesen866908c2012-09-06 18:15:23 +000014#include "RegisterCoalescer.h"
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000015#include "llvm/ADT/Statistic.h"
Eugene Zelenko5db84df2017-02-17 21:43:25 +000016#include "llvm/CodeGen/LiveInterval.h"
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000017#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Eugene Zelenko5db84df2017-02-17 21:43:25 +000018#include "llvm/CodeGen/LiveRegMatrix.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000019#include "llvm/CodeGen/VirtRegMap.h"
Eugene Zelenko5db84df2017-02-17 21:43:25 +000020#include "llvm/CodeGen/LiveIntervalUnion.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/Pass.h"
23#include "llvm/MC/LaneBitmask.h"
24#include "llvm/MC/MCRegisterInfo.h"
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000025#include "llvm/Support/Debug.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000026#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Target/TargetRegisterInfo.h"
Matthias Braun9912bb82015-07-14 17:52:07 +000028#include "llvm/Target/TargetSubtargetInfo.h"
Eugene Zelenko5db84df2017-02-17 21:43:25 +000029#include <cassert>
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000030
31using namespace llvm;
32
Chandler Carruth1b9dde02014-04-22 02:02:50 +000033#define DEBUG_TYPE "regalloc"
34
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000035STATISTIC(NumAssigned , "Number of registers assigned");
36STATISTIC(NumUnassigned , "Number of registers unassigned");
37
38char LiveRegMatrix::ID = 0;
39INITIALIZE_PASS_BEGIN(LiveRegMatrix, "liveregmatrix",
40 "Live Register Matrix", false, false)
41INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
42INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
43INITIALIZE_PASS_END(LiveRegMatrix, "liveregmatrix",
44 "Live Register Matrix", false, false)
45
Eugene Zelenko5db84df2017-02-17 21:43:25 +000046LiveRegMatrix::LiveRegMatrix() : MachineFunctionPass(ID) {}
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000047
48void LiveRegMatrix::getAnalysisUsage(AnalysisUsage &AU) const {
49 AU.setPreservesAll();
50 AU.addRequiredTransitive<LiveIntervals>();
51 AU.addRequiredTransitive<VirtRegMap>();
52 MachineFunctionPass::getAnalysisUsage(AU);
53}
54
55bool LiveRegMatrix::runOnMachineFunction(MachineFunction &MF) {
Eric Christopherfc6de422014-08-05 02:39:49 +000056 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000057 LIS = &getAnalysis<LiveIntervals>();
58 VRM = &getAnalysis<VirtRegMap>();
59
60 unsigned NumRegUnits = TRI->getNumRegUnits();
61 if (NumRegUnits != Matrix.size())
62 Queries.reset(new LiveIntervalUnion::Query[NumRegUnits]);
63 Matrix.init(LIUAlloc, NumRegUnits);
64
65 // Make sure no stale queries get reused.
66 invalidateVirtRegs();
67 return false;
68}
69
70void LiveRegMatrix::releaseMemory() {
71 for (unsigned i = 0, e = Matrix.size(); i != e; ++i) {
72 Matrix[i].clear();
Puyan Lotfi12ae04b2014-02-06 08:42:01 +000073 // No need to clear Queries here, since LiveIntervalUnion::Query doesn't
74 // have anything important to clear and LiveRegMatrix's runOnFunction()
Ahmed Charles56440fd2014-03-06 05:51:42 +000075 // does a std::unique_ptr::reset anyways.
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000076 }
77}
78
Benjamin Kramerb7d33112016-08-06 11:13:10 +000079template <typename Callable>
80static bool foreachUnit(const TargetRegisterInfo *TRI,
81 LiveInterval &VRegInterval, unsigned PhysReg,
82 Callable Func) {
Matthias Braun587e2742014-12-10 01:13:01 +000083 if (VRegInterval.hasSubRanges()) {
84 for (MCRegUnitMaskIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
85 unsigned Unit = (*Units).first;
Matthias Braune6a24852015-09-25 21:51:14 +000086 LaneBitmask Mask = (*Units).second;
Matthias Braun09afa1e2014-12-11 00:59:06 +000087 for (LiveInterval::SubRange &S : VRegInterval.subranges()) {
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +000088 if ((S.LaneMask & Mask).any()) {
Matthias Braun09afa1e2014-12-11 00:59:06 +000089 if (Func(Unit, S))
Matthias Braun587e2742014-12-10 01:13:01 +000090 return true;
91 break;
92 }
93 }
94 }
95 } else {
96 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
97 if (Func(*Units, VRegInterval))
98 return true;
99 }
100 }
101 return false;
102}
103
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +0000104void LiveRegMatrix::assign(LiveInterval &VirtReg, unsigned PhysReg) {
105 DEBUG(dbgs() << "assigning " << PrintReg(VirtReg.reg, TRI)
106 << " to " << PrintReg(PhysReg, TRI) << ':');
107 assert(!VRM->hasPhys(VirtReg.reg) && "Duplicate VirtReg assignment");
108 VRM->assignVirt2Phys(VirtReg.reg, PhysReg);
Matthias Braun587e2742014-12-10 01:13:01 +0000109
110 foreachUnit(TRI, VirtReg, PhysReg, [&](unsigned Unit,
111 const LiveRange &Range) {
112 DEBUG(dbgs() << ' ' << PrintRegUnit(Unit, TRI) << ' ' << Range);
113 Matrix[Unit].unify(VirtReg, Range);
114 return false;
115 });
116
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +0000117 ++NumAssigned;
118 DEBUG(dbgs() << '\n');
119}
120
121void LiveRegMatrix::unassign(LiveInterval &VirtReg) {
122 unsigned PhysReg = VRM->getPhys(VirtReg.reg);
123 DEBUG(dbgs() << "unassigning " << PrintReg(VirtReg.reg, TRI)
124 << " from " << PrintReg(PhysReg, TRI) << ':');
125 VRM->clearVirt(VirtReg.reg);
Matthias Braun587e2742014-12-10 01:13:01 +0000126
127 foreachUnit(TRI, VirtReg, PhysReg, [&](unsigned Unit,
128 const LiveRange &Range) {
129 DEBUG(dbgs() << ' ' << PrintRegUnit(Unit, TRI));
130 Matrix[Unit].extract(VirtReg, Range);
131 return false;
132 });
133
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +0000134 ++NumUnassigned;
135 DEBUG(dbgs() << '\n');
136}
137
Matthias Braun953393a2015-07-14 17:38:17 +0000138bool LiveRegMatrix::isPhysRegUsed(unsigned PhysReg) const {
139 for (MCRegUnitIterator Unit(PhysReg, TRI); Unit.isValid(); ++Unit) {
140 if (!Matrix[*Unit].empty())
141 return true;
142 }
143 return false;
144}
145
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +0000146bool LiveRegMatrix::checkRegMaskInterference(LiveInterval &VirtReg,
147 unsigned PhysReg) {
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);
156 }
157
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.
Jakob Stoklund Olesen13dffcb2012-06-15 22:24:22 +0000161 return !RegMaskUsable.empty() && (!PhysReg || !RegMaskUsable.test(PhysReg));
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +0000162}
163
164bool LiveRegMatrix::checkRegUnitInterference(LiveInterval &VirtReg,
165 unsigned PhysReg) {
166 if (VirtReg.empty())
167 return false;
Jakob Stoklund Olesen866908c2012-09-06 18:15:23 +0000168 CoalescerPair CP(VirtReg.reg, PhysReg, *TRI);
Matthias Braun587e2742014-12-10 01:13:01 +0000169
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());
174 });
175 return Result;
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +0000176}
177
178LiveIntervalUnion::Query &LiveRegMatrix::query(LiveInterval &VirtReg,
179 unsigned RegUnit) {
180 LiveIntervalUnion::Query &Q = Queries[RegUnit];
181 Q.init(UserTag, &VirtReg, &Matrix[RegUnit]);
182 return Q;
183}
184
185LiveRegMatrix::InterferenceKind
186LiveRegMatrix::checkInterference(LiveInterval &VirtReg, unsigned PhysReg) {
187 if (VirtReg.empty())
188 return IK_Free;
189
190 // Regmask interference is the fastest check.
191 if (checkRegMaskInterference(VirtReg, PhysReg))
192 return IK_RegMask;
193
194 // Check for fixed interference.
195 if (checkRegUnitInterference(VirtReg, PhysReg))
196 return IK_RegUnit;
197
198 // Check the matrix for virtual register interference.
199 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
200 if (query(VirtReg, *Units).checkInterference())
201 return IK_VirtReg;
202
203 return IK_Free;
204}