blob: e72977b02675acd67dff4b73a37f0252e349e987 [file] [log] [blame]
Eugene Zelenkoad3a5402017-02-17 21:43:25 +00001//===- LiveRegMatrix.cpp - Track register interference --------------------===//
Jakob Stoklund Olesen88794802012-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
Chandler Carruthe3e43d92017-06-06 11:49:48 +000014#include "llvm/CodeGen/LiveRegMatrix.h"
Jakob Stoklund Olesen45c5c572012-09-06 18:15:23 +000015#include "RegisterCoalescer.h"
Jakob Stoklund Olesen88794802012-06-09 02:13:10 +000016#include "llvm/ADT/Statistic.h"
Eugene Zelenkoad3a5402017-02-17 21:43:25 +000017#include "llvm/CodeGen/LiveInterval.h"
Eugene Zelenkoad3a5402017-02-17 21:43:25 +000018#include "llvm/CodeGen/LiveIntervalUnion.h"
Matthias Braunfa621d22017-12-13 02:51:04 +000019#include "llvm/CodeGen/LiveIntervals.h"
Eugene Zelenkoad3a5402017-02-17 21:43:25 +000020#include "llvm/CodeGen/MachineFunction.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000021#include "llvm/CodeGen/TargetRegisterInfo.h"
22#include "llvm/CodeGen/TargetSubtargetInfo.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000023#include "llvm/CodeGen/VirtRegMap.h"
Eugene Zelenkoad3a5402017-02-17 21:43:25 +000024#include "llvm/MC/LaneBitmask.h"
25#include "llvm/MC/MCRegisterInfo.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000026#include "llvm/Pass.h"
Jakob Stoklund Olesen88794802012-06-09 02:13:10 +000027#include "llvm/Support/Debug.h"
Chandler Carruth1b279142015-01-14 11:23:27 +000028#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoad3a5402017-02-17 21:43:25 +000029#include <cassert>
Jakob Stoklund Olesen88794802012-06-09 02:13:10 +000030
31using namespace llvm;
32
Chandler Carruth8677f2f2014-04-22 02:02:50 +000033#define DEBUG_TYPE "regalloc"
34
Jakob Stoklund Olesen88794802012-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 Zelenkoad3a5402017-02-17 21:43:25 +000046LiveRegMatrix::LiveRegMatrix() : MachineFunctionPass(ID) {}
Jakob Stoklund Olesen88794802012-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 Christopher60355182014-08-05 02:39:49 +000056 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen88794802012-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 Lotfif15735e2014-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 Charlesf4ccd112014-03-06 05:51:42 +000075 // does a std::unique_ptr::reset anyways.
Jakob Stoklund Olesen88794802012-06-09 02:13:10 +000076 }
77}
78
Benjamin Kramer284030a2016-08-06 11:13:10 +000079template <typename Callable>
80static bool foreachUnit(const TargetRegisterInfo *TRI,
81 LiveInterval &VRegInterval, unsigned PhysReg,
82 Callable Func) {
Matthias Braun0fc28992014-12-10 01:13:01 +000083 if (VRegInterval.hasSubRanges()) {
84 for (MCRegUnitMaskIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
85 unsigned Unit = (*Units).first;
Matthias Braundfc5b652015-09-25 21:51:14 +000086 LaneBitmask Mask = (*Units).second;
Matthias Braun1bfcc2d2014-12-11 00:59:06 +000087 for (LiveInterval::SubRange &S : VRegInterval.subranges()) {
Krzysztof Parzyszek308c60d2016-12-16 19:11:56 +000088 if ((S.LaneMask & Mask).any()) {
Matthias Braun1bfcc2d2014-12-11 00:59:06 +000089 if (Func(Unit, S))
Matthias Braun0fc28992014-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 Olesen88794802012-06-09 02:13:10 +0000104void LiveRegMatrix::assign(LiveInterval &VirtReg, unsigned PhysReg) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000105 LLVM_DEBUG(dbgs() << "assigning " << printReg(VirtReg.reg, TRI) << " to "
106 << printReg(PhysReg, TRI) << ':');
Jakob Stoklund Olesen88794802012-06-09 02:13:10 +0000107 assert(!VRM->hasPhys(VirtReg.reg) && "Duplicate VirtReg assignment");
108 VRM->assignVirt2Phys(VirtReg.reg, PhysReg);
Matthias Braun0fc28992014-12-10 01:13:01 +0000109
Nicola Zaghen0818e782018-05-14 12:53:11 +0000110 foreachUnit(
111 TRI, VirtReg, PhysReg, [&](unsigned Unit, const LiveRange &Range) {
112 LLVM_DEBUG(dbgs() << ' ' << printRegUnit(Unit, TRI) << ' ' << Range);
113 Matrix[Unit].unify(VirtReg, Range);
114 return false;
115 });
Matthias Braun0fc28992014-12-10 01:13:01 +0000116
Jakob Stoklund Olesen88794802012-06-09 02:13:10 +0000117 ++NumAssigned;
Nicola Zaghen0818e782018-05-14 12:53:11 +0000118 LLVM_DEBUG(dbgs() << '\n');
Jakob Stoklund Olesen88794802012-06-09 02:13:10 +0000119}
120
121void LiveRegMatrix::unassign(LiveInterval &VirtReg) {
122 unsigned PhysReg = VRM->getPhys(VirtReg.reg);
Nicola Zaghen0818e782018-05-14 12:53:11 +0000123 LLVM_DEBUG(dbgs() << "unassigning " << printReg(VirtReg.reg, TRI) << " from "
124 << printReg(PhysReg, TRI) << ':');
Jakob Stoklund Olesen88794802012-06-09 02:13:10 +0000125 VRM->clearVirt(VirtReg.reg);
Matthias Braun0fc28992014-12-10 01:13:01 +0000126
Nicola Zaghen0818e782018-05-14 12:53:11 +0000127 foreachUnit(TRI, VirtReg, PhysReg,
128 [&](unsigned Unit, const LiveRange &Range) {
129 LLVM_DEBUG(dbgs() << ' ' << printRegUnit(Unit, TRI));
130 Matrix[Unit].extract(VirtReg, Range);
131 return false;
132 });
Matthias Braun0fc28992014-12-10 01:13:01 +0000133
Jakob Stoklund Olesen88794802012-06-09 02:13:10 +0000134 ++NumUnassigned;
Nicola Zaghen0818e782018-05-14 12:53:11 +0000135 LLVM_DEBUG(dbgs() << '\n');
Jakob Stoklund Olesen88794802012-06-09 02:13:10 +0000136}
137
Matthias Braun65a40532015-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 Olesen88794802012-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 Oleseneb06b0b2012-06-15 22:24:22 +0000161 return !RegMaskUsable.empty() && (!PhysReg || !RegMaskUsable.test(PhysReg));
Jakob Stoklund Olesen88794802012-06-09 02:13:10 +0000162}
163
164bool LiveRegMatrix::checkRegUnitInterference(LiveInterval &VirtReg,
165 unsigned PhysReg) {
166 if (VirtReg.empty())
167 return false;
Jakob Stoklund Olesen45c5c572012-09-06 18:15:23 +0000168 CoalescerPair CP(VirtReg.reg, PhysReg, *TRI);
Matthias Braun0fc28992014-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 Olesen88794802012-06-09 02:13:10 +0000176}
177
Matthias Braun0e90d422017-03-02 00:35:08 +0000178LiveIntervalUnion::Query &LiveRegMatrix::query(const LiveRange &LR,
Jakob Stoklund Olesen88794802012-06-09 02:13:10 +0000179 unsigned RegUnit) {
180 LiveIntervalUnion::Query &Q = Queries[RegUnit];
Matthias Braun0e90d422017-03-02 00:35:08 +0000181 Q.init(UserTag, LR, Matrix[RegUnit]);
Jakob Stoklund Olesen88794802012-06-09 02:13:10 +0000182 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.
Matthias Braun0e90d422017-03-02 00:35:08 +0000199 bool Interference = foreachUnit(TRI, VirtReg, PhysReg,
200 [&](unsigned Unit, const LiveRange &LR) {
201 return query(LR, Unit).checkInterference();
202 });
203 if (Interference)
204 return IK_VirtReg;
Jakob Stoklund Olesen88794802012-06-09 02:13:10 +0000205
206 return IK_Free;
207}
Marina Yatsina1eecb872018-01-31 13:31:08 +0000208
209bool LiveRegMatrix::checkInterference(SlotIndex Start, SlotIndex End,
210 unsigned PhysReg) {
211 // Construct artificial live range containing only one segment [Start, End).
212 VNInfo valno(0, Start);
213 LiveRange::Segment Seg(Start, End, &valno);
214 LiveRange LR;
215 LR.addSegment(Seg);
216
217 // Check for interference with that segment
218 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
219 if (query(LR, *Units).checkInterference())
220 return true;
221 }
222 return false;
223}