blob: 7791e2a13c7d352c4a1bcdb320592369a22c04f7 [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"
Matthias Braun587e2742014-12-10 01:13:01 +000020#include "llvm/Support/Format.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000021#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Target/TargetRegisterInfo.h"
Matthias Braun9912bb82015-07-14 17:52:07 +000023#include "llvm/Target/TargetSubtargetInfo.h"
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000024
25using namespace llvm;
26
Chandler Carruth1b9dde02014-04-22 02:02:50 +000027#define DEBUG_TYPE "regalloc"
28
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000029STATISTIC(NumAssigned , "Number of registers assigned");
30STATISTIC(NumUnassigned , "Number of registers unassigned");
31
32char LiveRegMatrix::ID = 0;
33INITIALIZE_PASS_BEGIN(LiveRegMatrix, "liveregmatrix",
34 "Live Register Matrix", false, false)
35INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
36INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
37INITIALIZE_PASS_END(LiveRegMatrix, "liveregmatrix",
38 "Live Register Matrix", false, false)
39
40LiveRegMatrix::LiveRegMatrix() : MachineFunctionPass(ID),
41 UserTag(0), RegMaskTag(0), RegMaskVirtReg(0) {}
42
43void LiveRegMatrix::getAnalysisUsage(AnalysisUsage &AU) const {
44 AU.setPreservesAll();
45 AU.addRequiredTransitive<LiveIntervals>();
46 AU.addRequiredTransitive<VirtRegMap>();
47 MachineFunctionPass::getAnalysisUsage(AU);
48}
49
50bool LiveRegMatrix::runOnMachineFunction(MachineFunction &MF) {
Eric Christopherfc6de422014-08-05 02:39:49 +000051 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000052 LIS = &getAnalysis<LiveIntervals>();
53 VRM = &getAnalysis<VirtRegMap>();
54
55 unsigned NumRegUnits = TRI->getNumRegUnits();
56 if (NumRegUnits != Matrix.size())
57 Queries.reset(new LiveIntervalUnion::Query[NumRegUnits]);
58 Matrix.init(LIUAlloc, NumRegUnits);
59
60 // Make sure no stale queries get reused.
61 invalidateVirtRegs();
62 return false;
63}
64
65void LiveRegMatrix::releaseMemory() {
66 for (unsigned i = 0, e = Matrix.size(); i != e; ++i) {
67 Matrix[i].clear();
Puyan Lotfi12ae04b2014-02-06 08:42:01 +000068 // No need to clear Queries here, since LiveIntervalUnion::Query doesn't
69 // have anything important to clear and LiveRegMatrix's runOnFunction()
Ahmed Charles56440fd2014-03-06 05:51:42 +000070 // does a std::unique_ptr::reset anyways.
Jakob Stoklund Olesenc26fbbf2012-06-09 02:13:10 +000071 }
72}
73
Matthias Braun587e2742014-12-10 01:13:01 +000074template<typename Callable>
75bool foreachUnit(const TargetRegisterInfo *TRI, LiveInterval &VRegInterval,
76 unsigned PhysReg, Callable Func) {
77 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()) {
82 if (S.LaneMask & Mask) {
83 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}