Skip to content

Commit 953393a

Browse files
committedJul 14, 2015
RAGreedy: Keep track of allocated PhysRegs internally
Do not use MachineRegisterInfo::setPhysRegUsed()/isPhysRegUsed() anymore. This bitset changes function-global state and is set by the VirtRegRewriter anyway. Simply use a bitvector private to RAGreedy. Differential Revision: http://reviews.llvm.org/D10910 llvm-svn: 242169
1 parent 4ba525b commit 953393a

File tree

4 files changed

+40
-18
lines changed

4 files changed

+40
-18
lines changed
 

‎llvm/include/llvm/CodeGen/LiveIntervalUnion.h

+5
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ class LiveIntervalUnion {
203203
assert(idx < Size && "idx out of bounds");
204204
return LIUs[idx];
205205
}
206+
207+
const LiveIntervalUnion& operator[](unsigned Idx) const {
208+
assert(Idx < Size && "Idx out of bounds");
209+
return LIUs[Idx];
210+
}
206211
};
207212
};
208213

‎llvm/include/llvm/CodeGen/LiveRegMatrix.h

+3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ class LiveRegMatrix : public MachineFunctionPass {
114114
/// the assignment and updates VirtRegMap accordingly.
115115
void unassign(LiveInterval &VirtReg);
116116

117+
/// Returns true if the given \p PhysReg has any live intervals assigned.
118+
bool isPhysRegUsed(unsigned PhysReg) const;
119+
117120
//===--------------------------------------------------------------------===//
118121
// Low-level interface.
119122
//===--------------------------------------------------------------------===//

‎llvm/lib/CodeGen/LiveRegMatrix.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ void LiveRegMatrix::unassign(LiveInterval &VirtReg) {
131131
DEBUG(dbgs() << '\n');
132132
}
133133

134+
bool LiveRegMatrix::isPhysRegUsed(unsigned PhysReg) const {
135+
for (MCRegUnitIterator Unit(PhysReg, TRI); Unit.isValid(); ++Unit) {
136+
if (!Matrix[*Unit].empty())
137+
return true;
138+
}
139+
return false;
140+
}
141+
134142
bool LiveRegMatrix::checkRegMaskInterference(LiveInterval &VirtReg,
135143
unsigned PhysReg) {
136144
// Check if the cached information is valid.

‎llvm/lib/CodeGen/RegAllocGreedy.cpp

+24-18
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ class RAGreedy : public MachineFunctionPass,
400400
typedef SmallVector<HintInfo, 4> HintsInfo;
401401
BlockFrequency getBrokenHintFreq(const HintsInfo &, unsigned);
402402
void collectHintInfo(unsigned, HintsInfo &);
403+
404+
bool isUnusedCalleeSavedReg(unsigned PhysReg) const;
403405
};
404406
} // end anonymous namespace
405407

@@ -816,6 +818,16 @@ void RAGreedy::evictInterference(LiveInterval &VirtReg, unsigned PhysReg,
816818
}
817819
}
818820

821+
/// Returns true if the given \p PhysReg is a callee saved register and has not
822+
/// been used for allocation yet.
823+
bool RAGreedy::isUnusedCalleeSavedReg(unsigned PhysReg) const {
824+
unsigned CSR = RegClassInfo.getLastCalleeSavedAlias(PhysReg);
825+
if (CSR == 0)
826+
return false;
827+
828+
return !Matrix->isPhysRegUsed(PhysReg);
829+
}
830+
819831
/// tryEvict - Try to evict all interferences for a physreg.
820832
/// @param VirtReg Currently unassigned virtual register.
821833
/// @param Order Physregs to try.
@@ -861,13 +873,12 @@ unsigned RAGreedy::tryEvict(LiveInterval &VirtReg,
861873
continue;
862874
// The first use of a callee-saved register in a function has cost 1.
863875
// Don't start using a CSR when the CostPerUseLimit is low.
864-
if (CostPerUseLimit == 1)
865-
if (unsigned CSR = RegClassInfo.getLastCalleeSavedAlias(PhysReg))
866-
if (!MRI->isPhysRegUsed(CSR)) {
867-
DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " would clobber CSR "
868-
<< PrintReg(CSR, TRI) << '\n');
869-
continue;
870-
}
876+
if (CostPerUseLimit == 1 && isUnusedCalleeSavedReg(PhysReg)) {
877+
DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " would clobber CSR "
878+
<< PrintReg(RegClassInfo.getLastCalleeSavedAlias(PhysReg), TRI)
879+
<< '\n');
880+
continue;
881+
}
871882

872883
if (!canEvictInterference(VirtReg, PhysReg, false, BestCost))
873884
continue;
@@ -1348,9 +1359,8 @@ unsigned RAGreedy::calculateRegionSplitCost(LiveInterval &VirtReg,
13481359
unsigned BestCand = NoCand;
13491360
Order.rewind();
13501361
while (unsigned PhysReg = Order.next()) {
1351-
if (unsigned CSR = RegClassInfo.getLastCalleeSavedAlias(PhysReg))
1352-
if (IgnoreCSR && !MRI->isPhysRegUsed(CSR))
1353-
continue;
1362+
if (IgnoreCSR && isUnusedCalleeSavedReg(PhysReg))
1363+
continue;
13541364

13551365
// Discard bad candidates before we run out of interference cache cursors.
13561366
// This will only affect register classes with a lot of registers (>32).
@@ -2134,7 +2144,8 @@ unsigned RAGreedy::tryLastChanceRecoloring(LiveInterval &VirtReg,
21342144
unsigned ItVirtReg = (*It)->reg;
21352145
if (VRM->hasPhys(ItVirtReg))
21362146
Matrix->unassign(**It);
2137-
Matrix->assign(**It, VirtRegToPhysReg[ItVirtReg]);
2147+
unsigned ItPhysReg = VirtRegToPhysReg[ItVirtReg];
2148+
Matrix->assign(**It, ItPhysReg);
21382149
}
21392150
}
21402151

@@ -2441,16 +2452,11 @@ unsigned RAGreedy::selectOrSplitImpl(LiveInterval &VirtReg,
24412452
// First try assigning a free register.
24422453
AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo);
24432454
if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs)) {
2444-
// We check other options if we are using a CSR for the first time.
2445-
bool CSRFirstUse = false;
2446-
if (unsigned CSR = RegClassInfo.getLastCalleeSavedAlias(PhysReg))
2447-
if (!MRI->isPhysRegUsed(CSR))
2448-
CSRFirstUse = true;
2449-
24502455
// When NewVRegs is not empty, we may have made decisions such as evicting
24512456
// a virtual register, go with the earlier decisions and use the physical
24522457
// register.
2453-
if (CSRCost.getFrequency() && CSRFirstUse && NewVRegs.empty()) {
2458+
if (CSRCost.getFrequency() && isUnusedCalleeSavedReg(PhysReg) &&
2459+
NewVRegs.empty()) {
24542460
unsigned CSRReg = tryAssignCSRFirstTime(VirtReg, Order, PhysReg,
24552461
CostPerUseLimit, NewVRegs);
24562462
if (CSRReg || !NewVRegs.empty())

0 commit comments

Comments
 (0)
Please sign in to comment.