Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 1394e6d

Browse files
committed
Use the new TRI->getLargestLegalSuperClass hook to constrain register class inflation.
This has two effects: 1. We never inflate to a larger register class than what the sub-target can handle. 2. Completely unconstrained virtual registers get the largest possible register class. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@130229 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent c9e5015 commit 1394e6d

File tree

2 files changed

+34
-55
lines changed

2 files changed

+34
-55
lines changed

include/llvm/CodeGen/CalcSpillWeights.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ namespace llvm {
4040
/// VirtRegAuxInfo - Calculate auxiliary information for a virtual
4141
/// register such as its spill weight and allocation hint.
4242
class VirtRegAuxInfo {
43-
MachineFunction &mf_;
44-
LiveIntervals &lis_;
45-
const MachineLoopInfo &loops_;
46-
DenseMap<unsigned, float> hint_;
43+
MachineFunction &MF;
44+
LiveIntervals &LIS;
45+
const MachineLoopInfo &Loops;
46+
DenseMap<unsigned, float> Hint;
4747
public:
4848
VirtRegAuxInfo(MachineFunction &mf, LiveIntervals &lis,
4949
const MachineLoopInfo &loops) :
50-
mf_(mf), lis_(lis), loops_(loops) {}
50+
MF(mf), LIS(lis), Loops(loops) {}
5151

5252
/// CalculateRegClass - recompute the register class for reg from its uses.
5353
/// Since the register class can affect the allocation hint, this function

lib/CodeGen/CalcSpillWeights.cpp

+29-50
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ static unsigned copyHint(const MachineInstr *mi, unsigned reg,
8787
}
8888

8989
void VirtRegAuxInfo::CalculateWeightAndHint(LiveInterval &li) {
90-
MachineRegisterInfo &mri = mf_.getRegInfo();
91-
const TargetRegisterInfo &tri = *mf_.getTarget().getRegisterInfo();
90+
MachineRegisterInfo &mri = MF.getRegInfo();
91+
const TargetRegisterInfo &tri = *MF.getTarget().getRegisterInfo();
9292
MachineBasicBlock *mbb = 0;
9393
MachineLoop *loop = 0;
9494
unsigned loopDepth = 0;
@@ -118,7 +118,7 @@ void VirtRegAuxInfo::CalculateWeightAndHint(LiveInterval &li) {
118118
// Get loop info for mi.
119119
if (mi->getParent() != mbb) {
120120
mbb = mi->getParent();
121-
loop = loops_.getLoopFor(mbb);
121+
loop = Loops.getLoopFor(mbb);
122122
loopDepth = loop ? loop->getLoopDepth() : 0;
123123
isExiting = loop ? loop->isLoopExiting(mbb) : false;
124124
}
@@ -129,7 +129,7 @@ void VirtRegAuxInfo::CalculateWeightAndHint(LiveInterval &li) {
129129
weight = LiveIntervals::getSpillWeight(writes, reads, loopDepth);
130130

131131
// Give extra weight to what looks like a loop induction variable update.
132-
if (writes && isExiting && lis_.isLiveOutOfMBB(li, mbb))
132+
if (writes && isExiting && LIS.isLiveOutOfMBB(li, mbb))
133133
weight *= 3;
134134

135135
totalWeight += weight;
@@ -141,17 +141,17 @@ void VirtRegAuxInfo::CalculateWeightAndHint(LiveInterval &li) {
141141
unsigned hint = copyHint(mi, li.reg, tri, mri);
142142
if (!hint)
143143
continue;
144-
float hweight = hint_[hint] += weight;
144+
float hweight = Hint[hint] += weight;
145145
if (TargetRegisterInfo::isPhysicalRegister(hint)) {
146-
if (hweight > bestPhys && lis_.isAllocatable(hint))
146+
if (hweight > bestPhys && LIS.isAllocatable(hint))
147147
bestPhys = hweight, hintPhys = hint;
148148
} else {
149149
if (hweight > bestVirt)
150150
bestVirt = hweight, hintVirt = hint;
151151
}
152152
}
153153

154-
hint_.clear();
154+
Hint.clear();
155155

156156
// Always prefer the physreg hint.
157157
if (unsigned hint = hintPhys ? hintPhys : hintVirt) {
@@ -176,7 +176,7 @@ void VirtRegAuxInfo::CalculateWeightAndHint(LiveInterval &li) {
176176
// FIXME: this gets much more complicated once we support non-trivial
177177
// re-materialization.
178178
bool isLoad = false;
179-
if (lis_.isReMaterializable(li, 0, isLoad)) {
179+
if (LIS.isReMaterializable(li, 0, isLoad)) {
180180
if (isLoad)
181181
totalWeight *= 0.9F;
182182
else
@@ -187,50 +187,29 @@ void VirtRegAuxInfo::CalculateWeightAndHint(LiveInterval &li) {
187187
}
188188

189189
void VirtRegAuxInfo::CalculateRegClass(unsigned reg) {
190-
MachineRegisterInfo &mri = mf_.getRegInfo();
191-
const TargetRegisterInfo *tri = mf_.getTarget().getRegisterInfo();
192-
const TargetRegisterClass *orc = mri.getRegClass(reg);
193-
SmallPtrSet<const TargetRegisterClass*,8> rcs;
194-
195-
for (MachineRegisterInfo::reg_nodbg_iterator I = mri.reg_nodbg_begin(reg),
196-
E = mri.reg_nodbg_end(); I != E; ++I) {
197-
// The targets don't have accurate enough regclass descriptions that we can
198-
// handle subregs. We need something similar to
199-
// TRI::getMatchingSuperRegClass, but returning a super class instead of a
200-
// sub class.
201-
if (I.getOperand().getSubReg()) {
202-
DEBUG(dbgs() << "Cannot handle subregs: " << I.getOperand() << '\n');
203-
return;
204-
}
205-
if (const TargetRegisterClass *rc =
206-
I->getDesc().getRegClass(I.getOperandNo(), tri))
207-
rcs.insert(rc);
208-
}
190+
MachineRegisterInfo &MRI = MF.getRegInfo();
191+
const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
192+
const TargetRegisterClass *OldRC = MRI.getRegClass(reg);
193+
const TargetRegisterClass *NewRC = TRI->getLargestLegalSuperClass(OldRC);
209194

210-
// If we found no regclass constraints, just leave reg as is.
211-
// In theory, we could inflate to the largest superclass of reg's existing
212-
// class, but that might not be legal for the current cpu setting.
213-
// This could happen if reg is only used by COPY instructions, so we may need
214-
// to improve on this.
215-
if (rcs.empty()) {
195+
// Stop early if there is no room to grow.
196+
if (NewRC == OldRC)
216197
return;
217-
}
218198

219-
// Compute the intersection of all classes in rcs.
220-
// This ought to be independent of iteration order, but if the target register
221-
// classes don't form a proper algebra, it is possible to get different
222-
// results. The solution is to make sure the intersection of any two register
223-
// classes is also a register class or the null set.
224-
const TargetRegisterClass *rc = 0;
225-
for (SmallPtrSet<const TargetRegisterClass*,8>::iterator I = rcs.begin(),
226-
E = rcs.end(); I != E; ++I) {
227-
rc = rc ? getCommonSubClass(rc, *I) : *I;
228-
assert(rc && "Incompatible regclass constraints found");
199+
// Accumulate constraints from all uses.
200+
for (MachineRegisterInfo::reg_nodbg_iterator I = MRI.reg_nodbg_begin(reg),
201+
E = MRI.reg_nodbg_end(); I != E; ++I) {
202+
// TRI doesn't have accurate enough information to model this yet.
203+
if (I.getOperand().getSubReg())
204+
return;
205+
const TargetRegisterClass *OpRC =
206+
I->getDesc().getRegClass(I.getOperandNo(), TRI);
207+
if (OpRC)
208+
NewRC = getCommonSubClass(NewRC, OpRC);
209+
if (!NewRC || NewRC == OldRC)
210+
return;
229211
}
230-
231-
if (rc == orc)
232-
return;
233-
DEBUG(dbgs() << "Inflating " << orc->getName() << ':' << PrintReg(reg)
234-
<< " to " << rc->getName() <<".\n");
235-
mri.setRegClass(reg, rc);
212+
DEBUG(dbgs() << "Inflating " << OldRC->getName() << ':' << PrintReg(reg)
213+
<< " to " << NewRC->getName() <<".\n");
214+
MRI.setRegClass(reg, NewRC);
236215
}

0 commit comments

Comments
 (0)