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/LiveIntervalAnalysis.h"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/CodeGen/VirtRegMap.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Target/TargetRegisterInfo.h"
26 #define DEBUG_TYPE "regalloc"
28 STATISTIC(NumAssigned , "Number of registers assigned");
29 STATISTIC(NumUnassigned , "Number of registers unassigned");
31 char LiveRegMatrix::ID = 0;
32 INITIALIZE_PASS_BEGIN(LiveRegMatrix, "liveregmatrix",
33 "Live Register Matrix", false, false)
34 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
35 INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
36 INITIALIZE_PASS_END(LiveRegMatrix, "liveregmatrix",
37 "Live Register Matrix", false, false)
39 LiveRegMatrix::LiveRegMatrix() : MachineFunctionPass(ID),
40 UserTag(0), RegMaskTag(0), RegMaskVirtReg(0) {}
42 void LiveRegMatrix::getAnalysisUsage(AnalysisUsage &AU) const {
44 AU.addRequiredTransitive<LiveIntervals>();
45 AU.addRequiredTransitive<VirtRegMap>();
46 MachineFunctionPass::getAnalysisUsage(AU);
49 bool LiveRegMatrix::runOnMachineFunction(MachineFunction &MF) {
50 TRI = MF.getSubtarget().getRegisterInfo();
51 MRI = &MF.getRegInfo();
52 LIS = &getAnalysis<LiveIntervals>();
53 VRM = &getAnalysis<VirtRegMap>();
55 unsigned NumRegUnits = TRI->getNumRegUnits();
56 if (NumRegUnits != Matrix.size())
57 Queries.reset(new LiveIntervalUnion::Query[NumRegUnits]);
58 Matrix.init(LIUAlloc, NumRegUnits);
60 // Make sure no stale queries get reused.
65 void LiveRegMatrix::releaseMemory() {
66 for (unsigned i = 0, e = Matrix.size(); i != e; ++i) {
68 // No need to clear Queries here, since LiveIntervalUnion::Query doesn't
69 // have anything important to clear and LiveRegMatrix's runOnFunction()
70 // does a std::unique_ptr::reset anyways.
74 void LiveRegMatrix::assign(LiveInterval &VirtReg, unsigned PhysReg) {
75 DEBUG(dbgs() << "assigning " << PrintReg(VirtReg.reg, TRI)
76 << " to " << PrintReg(PhysReg, TRI) << ':');
77 assert(!VRM->hasPhys(VirtReg.reg) && "Duplicate VirtReg assignment");
78 VRM->assignVirt2Phys(VirtReg.reg, PhysReg);
79 MRI->setPhysRegUsed(PhysReg);
80 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
81 DEBUG(dbgs() << ' ' << PrintRegUnit(*Units, TRI));
82 Matrix[*Units].unify(VirtReg);
85 DEBUG(dbgs() << '\n');
88 void LiveRegMatrix::unassign(LiveInterval &VirtReg) {
89 unsigned PhysReg = VRM->getPhys(VirtReg.reg);
90 DEBUG(dbgs() << "unassigning " << PrintReg(VirtReg.reg, TRI)
91 << " from " << PrintReg(PhysReg, TRI) << ':');
92 VRM->clearVirt(VirtReg.reg);
93 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
94 DEBUG(dbgs() << ' ' << PrintRegUnit(*Units, TRI));
95 Matrix[*Units].extract(VirtReg);
98 DEBUG(dbgs() << '\n');
101 bool LiveRegMatrix::checkRegMaskInterference(LiveInterval &VirtReg,
103 // Check if the cached information is valid.
104 // The same BitVector can be reused for all PhysRegs.
105 // We could cache multiple VirtRegs if it becomes necessary.
106 if (RegMaskVirtReg != VirtReg.reg || RegMaskTag != UserTag) {
107 RegMaskVirtReg = VirtReg.reg;
108 RegMaskTag = UserTag;
109 RegMaskUsable.clear();
110 LIS->checkRegMaskInterference(VirtReg, RegMaskUsable);
113 // The BitVector is indexed by PhysReg, not register unit.
114 // Regmask interference is more fine grained than regunits.
115 // For example, a Win64 call can clobber %ymm8 yet preserve %xmm8.
116 return !RegMaskUsable.empty() && (!PhysReg || !RegMaskUsable.test(PhysReg));
119 bool LiveRegMatrix::checkRegUnitInterference(LiveInterval &VirtReg,
123 CoalescerPair CP(VirtReg.reg, PhysReg, *TRI);
124 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
125 const LiveRange &UnitRange = LIS->getRegUnit(*Units);
126 if (VirtReg.overlaps(UnitRange, CP, *LIS->getSlotIndexes()))
132 LiveIntervalUnion::Query &LiveRegMatrix::query(LiveInterval &VirtReg,
134 LiveIntervalUnion::Query &Q = Queries[RegUnit];
135 Q.init(UserTag, &VirtReg, &Matrix[RegUnit]);
139 LiveRegMatrix::InterferenceKind
140 LiveRegMatrix::checkInterference(LiveInterval &VirtReg, unsigned PhysReg) {
144 // Regmask interference is the fastest check.
145 if (checkRegMaskInterference(VirtReg, PhysReg))
148 // Check for fixed interference.
149 if (checkRegUnitInterference(VirtReg, PhysReg))
152 // Check the matrix for virtual register interference.
153 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
154 if (query(VirtReg, *Units).checkInterference())