blob: d08fae09323ca16c4a6bb2c3b614a86cc1ba894c [file] [log] [blame]
Lang Hamesd17e2962009-12-14 06:49:42 +00001//===------------------------ CalcSpillWeights.cpp ------------------------===//
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
Lang Hamesd17e2962009-12-14 06:49:42 +000010#include "llvm/CodeGen/CalcSpillWeights.h"
11#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Benjamin Kramere2a1d892013-06-17 19:00:36 +000012#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Lang Hamesd17e2962009-12-14 06:49:42 +000013#include "llvm/CodeGen/MachineFunction.h"
14#include "llvm/CodeGen/MachineLoopInfo.h"
15#include "llvm/CodeGen/MachineRegisterInfo.h"
Lang Hamesd17e2962009-12-14 06:49:42 +000016#include "llvm/Support/Debug.h"
17#include "llvm/Support/raw_ostream.h"
18#include "llvm/Target/TargetInstrInfo.h"
19#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000020#include "llvm/Target/TargetSubtargetInfo.h"
Lang Hamesd17e2962009-12-14 06:49:42 +000021using namespace llvm;
22
Chandler Carruth1b9dde02014-04-22 02:02:50 +000023#define DEBUG_TYPE "calcspillweights"
24
Arnaud A. de Grandmaisonea3ac162013-11-11 19:04:45 +000025void llvm::calculateSpillWeightsAndHints(LiveIntervals &LIS,
Arnaud A. de Grandmaison760c1e02013-11-10 17:46:31 +000026 MachineFunction &MF,
27 const MachineLoopInfo &MLI,
Arnaud A. de Grandmaisonf5f040f2013-11-11 19:56:14 +000028 const MachineBlockFrequencyInfo &MBFI,
29 VirtRegAuxInfo::NormalizingFn norm) {
David Greenee40730d2009-12-24 00:39:02 +000030 DEBUG(dbgs() << "********** Compute Spill Weights **********\n"
David Blaikiec8c29202012-08-22 17:18:53 +000031 << "********** Function: " << MF.getName() << '\n');
Lang Hamesd17e2962009-12-14 06:49:42 +000032
Jakob Stoklund Olesena1f43dc2012-06-20 21:25:05 +000033 MachineRegisterInfo &MRI = MF.getRegInfo();
Arnaud A. de Grandmaisonf5f040f2013-11-11 19:56:14 +000034 VirtRegAuxInfo VRAI(MF, LIS, MLI, MBFI, norm);
Jakob Stoklund Olesena1f43dc2012-06-20 21:25:05 +000035 for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
36 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
37 if (MRI.reg_nodbg_empty(Reg))
38 continue;
Arnaud A. de Grandmaisonea3ac162013-11-11 19:04:45 +000039 VRAI.calculateSpillWeightAndHint(LIS.getInterval(Reg));
Lang Hamesd17e2962009-12-14 06:49:42 +000040 }
Lang Hamesd17e2962009-12-14 06:49:42 +000041}
42
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +000043// Return the preferred allocation register for reg, given a COPY instruction.
44static unsigned copyHint(const MachineInstr *mi, unsigned reg,
45 const TargetRegisterInfo &tri,
46 const MachineRegisterInfo &mri) {
47 unsigned sub, hreg, hsub;
48 if (mi->getOperand(0).getReg() == reg) {
49 sub = mi->getOperand(0).getSubReg();
50 hreg = mi->getOperand(1).getReg();
51 hsub = mi->getOperand(1).getSubReg();
52 } else {
53 sub = mi->getOperand(1).getSubReg();
54 hreg = mi->getOperand(0).getReg();
55 hsub = mi->getOperand(0).getSubReg();
56 }
57
58 if (!hreg)
59 return 0;
60
61 if (TargetRegisterInfo::isVirtualRegister(hreg))
62 return sub == hsub ? hreg : 0;
63
64 const TargetRegisterClass *rc = mri.getRegClass(reg);
65
66 // Only allow physreg hints in rc.
67 if (sub == 0)
68 return rc->contains(hreg) ? hreg : 0;
69
70 // reg:sub should match the physreg hreg.
71 return tri.getMatchingSuperReg(hreg, sub, rc);
Lang Hamesd17e2962009-12-14 06:49:42 +000072}
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +000073
Jakob Stoklund Olesen9e27e262012-06-05 01:06:12 +000074// Check if all values in LI are rematerializable
75static bool isRematerializable(const LiveInterval &LI,
76 const LiveIntervals &LIS,
77 const TargetInstrInfo &TII) {
78 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
79 I != E; ++I) {
80 const VNInfo *VNI = *I;
81 if (VNI->isUnused())
82 continue;
83 if (VNI->isPHIDef())
84 return false;
85
86 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
87 assert(MI && "Dead valno in interval");
88
89 if (!TII.isTriviallyReMaterializable(MI, LIS.getAliasAnalysis()))
90 return false;
91 }
92 return true;
93}
94
Benjamin Kramere2a1d892013-06-17 19:00:36 +000095void
Arnaud A. de Grandmaisonea3ac162013-11-11 19:04:45 +000096VirtRegAuxInfo::calculateSpillWeightAndHint(LiveInterval &li) {
Jakob Stoklund Olesen3b7b7bc2011-04-26 18:52:36 +000097 MachineRegisterInfo &mri = MF.getRegInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +000098 const TargetRegisterInfo &tri = *MF.getSubtarget().getRegisterInfo();
Craig Topperc0196b12014-04-14 00:51:57 +000099 MachineBasicBlock *mbb = nullptr;
100 MachineLoop *loop = nullptr;
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000101 bool isExiting = false;
102 float totalWeight = 0;
Arnaud A. de Grandmaison829dd812014-11-04 20:51:24 +0000103 unsigned numInstr = 0; // Number of instructions using li
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000104 SmallPtrSet<MachineInstr*, 8> visited;
105
Nadav Rotemc4bd84c2013-04-06 04:24:12 +0000106 // Find the best physreg hint and the best virtreg hint.
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000107 float bestPhys = 0, bestVirt = 0;
108 unsigned hintPhys = 0, hintVirt = 0;
109
110 // Don't recompute a target specific hint.
111 bool noHint = mri.getRegAllocationHint(li.reg).first != 0;
112
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000113 // Don't recompute spill weight for an unspillable register.
114 bool Spillable = li.isSpillable();
115
Owen Andersonabb90c92014-03-13 06:02:25 +0000116 for (MachineRegisterInfo::reg_instr_iterator
117 I = mri.reg_instr_begin(li.reg), E = mri.reg_instr_end();
118 I != E; ) {
119 MachineInstr *mi = &*(I++);
Arnaud A. de Grandmaison829dd812014-11-04 20:51:24 +0000120 numInstr++;
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000121 if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue())
122 continue;
David Blaikie70573dc2014-11-19 07:49:26 +0000123 if (!visited.insert(mi).second)
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000124 continue;
125
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000126 float weight = 1.0f;
127 if (Spillable) {
128 // Get loop info for mi.
129 if (mi->getParent() != mbb) {
130 mbb = mi->getParent();
Jakob Stoklund Olesen3b7b7bc2011-04-26 18:52:36 +0000131 loop = Loops.getLoopFor(mbb);
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000132 isExiting = loop ? loop->isLoopExiting(mbb) : false;
133 }
134
135 // Calculate instr weight.
136 bool reads, writes;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000137 std::tie(reads, writes) = mi->readsWritesVirtualRegister(li.reg);
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000138 weight = LiveIntervals::getSpillWeight(
Michael Gottesman9f49d742013-12-14 00:53:32 +0000139 writes, reads, &MBFI, mi);
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000140
141 // Give extra weight to what looks like a loop induction variable update.
Jakob Stoklund Olesen3b7b7bc2011-04-26 18:52:36 +0000142 if (writes && isExiting && LIS.isLiveOutOfMBB(li, mbb))
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000143 weight *= 3;
144
145 totalWeight += weight;
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000146 }
147
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000148 // Get allocation hints from copies.
149 if (noHint || !mi->isCopy())
150 continue;
151 unsigned hint = copyHint(mi, li.reg, tri, mri);
152 if (!hint)
153 continue;
Duncan P. N. Exon Smith7af34322014-04-21 17:57:01 +0000154 // Force hweight onto the stack so that x86 doesn't add hidden precision,
155 // making the comparison incorrectly pass (i.e., 1 > 1 == true??).
156 //
157 // FIXME: we probably shouldn't use floats at all.
158 volatile float hweight = Hint[hint] += weight;
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000159 if (TargetRegisterInfo::isPhysicalRegister(hint)) {
Jakob Stoklund Olesencea596a2012-10-15 22:14:34 +0000160 if (hweight > bestPhys && mri.isAllocatable(hint))
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000161 bestPhys = hweight, hintPhys = hint;
162 } else {
163 if (hweight > bestVirt)
164 bestVirt = hweight, hintVirt = hint;
165 }
166 }
167
Jakob Stoklund Olesen3b7b7bc2011-04-26 18:52:36 +0000168 Hint.clear();
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000169
170 // Always prefer the physreg hint.
171 if (unsigned hint = hintPhys ? hintPhys : hintVirt) {
172 mri.setRegAllocationHint(li.reg, 0, hint);
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000173 // Weakly boost the spill weight of hinted registers.
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000174 totalWeight *= 1.01F;
175 }
176
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000177 // If the live interval was already unspillable, leave it that way.
178 if (!Spillable)
179 return;
180
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000181 // Mark li as unspillable if all live ranges are tiny.
Jakob Stoklund Olesen4edf17d2011-05-16 23:50:05 +0000182 if (li.isZeroLength(LIS.getSlotIndexes())) {
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000183 li.markNotSpillable();
184 return;
185 }
186
187 // If all of the definitions of the interval are re-materializable,
Jakob Stoklund Olesen9e27e262012-06-05 01:06:12 +0000188 // it is a preferred candidate for spilling.
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000189 // FIXME: this gets much more complicated once we support non-trivial
190 // re-materialization.
Eric Christopherfc6de422014-08-05 02:39:49 +0000191 if (isRematerializable(li, LIS, *MF.getSubtarget().getInstrInfo()))
Jakob Stoklund Olesen9e27e262012-06-05 01:06:12 +0000192 totalWeight *= 0.5F;
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000193
Arnaud A. de Grandmaison829dd812014-11-04 20:51:24 +0000194 li.weight = normalize(totalWeight, li.getSize(), numInstr);
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000195}