blob: 1f6909028eab862ee569d49dc52f60f6a99f603c [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"
Evan Cheng3ebd5512010-02-10 01:21:02 +000019#include "llvm/Target/TargetMachine.h"
Lang Hamesd17e2962009-12-14 06:49:42 +000020#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000021#include "llvm/Target/TargetSubtargetInfo.h"
Lang Hamesd17e2962009-12-14 06:49:42 +000022using namespace llvm;
23
Chandler Carruth1b9dde02014-04-22 02:02:50 +000024#define DEBUG_TYPE "calcspillweights"
25
Arnaud A. de Grandmaisonea3ac162013-11-11 19:04:45 +000026void llvm::calculateSpillWeightsAndHints(LiveIntervals &LIS,
Arnaud A. de Grandmaison760c1e02013-11-10 17:46:31 +000027 MachineFunction &MF,
28 const MachineLoopInfo &MLI,
Arnaud A. de Grandmaisonf5f040f2013-11-11 19:56:14 +000029 const MachineBlockFrequencyInfo &MBFI,
30 VirtRegAuxInfo::NormalizingFn norm) {
David Greenee40730d2009-12-24 00:39:02 +000031 DEBUG(dbgs() << "********** Compute Spill Weights **********\n"
David Blaikiec8c29202012-08-22 17:18:53 +000032 << "********** Function: " << MF.getName() << '\n');
Lang Hamesd17e2962009-12-14 06:49:42 +000033
Jakob Stoklund Olesena1f43dc2012-06-20 21:25:05 +000034 MachineRegisterInfo &MRI = MF.getRegInfo();
Arnaud A. de Grandmaisonf5f040f2013-11-11 19:56:14 +000035 VirtRegAuxInfo VRAI(MF, LIS, MLI, MBFI, norm);
Jakob Stoklund Olesena1f43dc2012-06-20 21:25:05 +000036 for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
37 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
38 if (MRI.reg_nodbg_empty(Reg))
39 continue;
Arnaud A. de Grandmaisonea3ac162013-11-11 19:04:45 +000040 VRAI.calculateSpillWeightAndHint(LIS.getInterval(Reg));
Lang Hamesd17e2962009-12-14 06:49:42 +000041 }
Lang Hamesd17e2962009-12-14 06:49:42 +000042}
43
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +000044// Return the preferred allocation register for reg, given a COPY instruction.
45static unsigned copyHint(const MachineInstr *mi, unsigned reg,
46 const TargetRegisterInfo &tri,
47 const MachineRegisterInfo &mri) {
48 unsigned sub, hreg, hsub;
49 if (mi->getOperand(0).getReg() == reg) {
50 sub = mi->getOperand(0).getSubReg();
51 hreg = mi->getOperand(1).getReg();
52 hsub = mi->getOperand(1).getSubReg();
53 } else {
54 sub = mi->getOperand(1).getSubReg();
55 hreg = mi->getOperand(0).getReg();
56 hsub = mi->getOperand(0).getSubReg();
57 }
58
59 if (!hreg)
60 return 0;
61
62 if (TargetRegisterInfo::isVirtualRegister(hreg))
63 return sub == hsub ? hreg : 0;
64
65 const TargetRegisterClass *rc = mri.getRegClass(reg);
66
67 // Only allow physreg hints in rc.
68 if (sub == 0)
69 return rc->contains(hreg) ? hreg : 0;
70
71 // reg:sub should match the physreg hreg.
72 return tri.getMatchingSuperReg(hreg, sub, rc);
Lang Hamesd17e2962009-12-14 06:49:42 +000073}
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +000074
Jakob Stoklund Olesen9e27e262012-06-05 01:06:12 +000075// Check if all values in LI are rematerializable
76static bool isRematerializable(const LiveInterval &LI,
77 const LiveIntervals &LIS,
78 const TargetInstrInfo &TII) {
79 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
80 I != E; ++I) {
81 const VNInfo *VNI = *I;
82 if (VNI->isUnused())
83 continue;
84 if (VNI->isPHIDef())
85 return false;
86
87 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
88 assert(MI && "Dead valno in interval");
89
90 if (!TII.isTriviallyReMaterializable(MI, LIS.getAliasAnalysis()))
91 return false;
92 }
93 return true;
94}
95
Benjamin Kramere2a1d892013-06-17 19:00:36 +000096void
Arnaud A. de Grandmaisonea3ac162013-11-11 19:04:45 +000097VirtRegAuxInfo::calculateSpillWeightAndHint(LiveInterval &li) {
Jakob Stoklund Olesen3b7b7bc2011-04-26 18:52:36 +000098 MachineRegisterInfo &mri = MF.getRegInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +000099 const TargetRegisterInfo &tri = *MF.getSubtarget().getRegisterInfo();
Craig Topperc0196b12014-04-14 00:51:57 +0000100 MachineBasicBlock *mbb = nullptr;
101 MachineLoop *loop = nullptr;
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000102 bool isExiting = false;
103 float totalWeight = 0;
104 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++);
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000120 if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue())
121 continue;
122 if (!visited.insert(mi))
123 continue;
124
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000125 float weight = 1.0f;
126 if (Spillable) {
127 // Get loop info for mi.
128 if (mi->getParent() != mbb) {
129 mbb = mi->getParent();
Jakob Stoklund Olesen3b7b7bc2011-04-26 18:52:36 +0000130 loop = Loops.getLoopFor(mbb);
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000131 isExiting = loop ? loop->isLoopExiting(mbb) : false;
132 }
133
134 // Calculate instr weight.
135 bool reads, writes;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000136 std::tie(reads, writes) = mi->readsWritesVirtualRegister(li.reg);
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000137 weight = LiveIntervals::getSpillWeight(
Michael Gottesman9f49d742013-12-14 00:53:32 +0000138 writes, reads, &MBFI, mi);
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000139
140 // Give extra weight to what looks like a loop induction variable update.
Jakob Stoklund Olesen3b7b7bc2011-04-26 18:52:36 +0000141 if (writes && isExiting && LIS.isLiveOutOfMBB(li, mbb))
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000142 weight *= 3;
143
144 totalWeight += weight;
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000145 }
146
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000147 // Get allocation hints from copies.
148 if (noHint || !mi->isCopy())
149 continue;
150 unsigned hint = copyHint(mi, li.reg, tri, mri);
151 if (!hint)
152 continue;
Duncan P. N. Exon Smith7af34322014-04-21 17:57:01 +0000153 // Force hweight onto the stack so that x86 doesn't add hidden precision,
154 // making the comparison incorrectly pass (i.e., 1 > 1 == true??).
155 //
156 // FIXME: we probably shouldn't use floats at all.
157 volatile float hweight = Hint[hint] += weight;
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000158 if (TargetRegisterInfo::isPhysicalRegister(hint)) {
Jakob Stoklund Olesencea596a2012-10-15 22:14:34 +0000159 if (hweight > bestPhys && mri.isAllocatable(hint))
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000160 bestPhys = hweight, hintPhys = hint;
161 } else {
162 if (hweight > bestVirt)
163 bestVirt = hweight, hintVirt = hint;
164 }
165 }
166
Jakob Stoklund Olesen3b7b7bc2011-04-26 18:52:36 +0000167 Hint.clear();
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000168
169 // Always prefer the physreg hint.
170 if (unsigned hint = hintPhys ? hintPhys : hintVirt) {
171 mri.setRegAllocationHint(li.reg, 0, hint);
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000172 // Weakly boost the spill weight of hinted registers.
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000173 totalWeight *= 1.01F;
174 }
175
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000176 // If the live interval was already unspillable, leave it that way.
177 if (!Spillable)
178 return;
179
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000180 // Mark li as unspillable if all live ranges are tiny.
Jakob Stoklund Olesen4edf17d2011-05-16 23:50:05 +0000181 if (li.isZeroLength(LIS.getSlotIndexes())) {
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000182 li.markNotSpillable();
183 return;
184 }
185
186 // If all of the definitions of the interval are re-materializable,
Jakob Stoklund Olesen9e27e262012-06-05 01:06:12 +0000187 // it is a preferred candidate for spilling.
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000188 // FIXME: this gets much more complicated once we support non-trivial
189 // re-materialization.
Eric Christopherfc6de422014-08-05 02:39:49 +0000190 if (isRematerializable(li, LIS, *MF.getSubtarget().getInstrInfo()))
Jakob Stoklund Olesen9e27e262012-06-05 01:06:12 +0000191 totalWeight *= 0.5F;
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000192
Arnaud A. de Grandmaisonf5f040f2013-11-11 19:56:14 +0000193 li.weight = normalize(totalWeight, li.getSize());
Jakob Stoklund Olesene00c49d2010-08-10 00:02:26 +0000194}