blob: 72eafcd0792c6aa2b1d864de93a7a9c925f68fa0 [file] [log] [blame]
Jim Grosbach905c9522012-02-17 17:35:10 +00001//===-- LiveRangeEdit.cpp - Basic tools for editing a register live range -===//
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +00002//
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//
10// The LiveRangeEdit class represents changes done to a virtual register when it
11// is spilled or split.
12//===----------------------------------------------------------------------===//
13
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/CodeGen/LiveRangeEdit.h"
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +000015#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +000016#include "llvm/CodeGen/CalcSpillWeights.h"
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000017#include "llvm/CodeGen/LiveIntervalAnalysis.h"
18#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000019#include "llvm/CodeGen/VirtRegMap.h"
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Target/TargetInstrInfo.h"
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000023
24using namespace llvm;
25
Chandler Carruth1b9dde02014-04-22 02:02:50 +000026#define DEBUG_TYPE "regalloc"
27
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +000028STATISTIC(NumDCEDeleted, "Number of instructions deleted by DCE");
29STATISTIC(NumDCEFoldedLoads, "Number of single use loads folded after DCE");
30STATISTIC(NumFracRanges, "Number of live ranges fractured by DCE");
31
David Blaikiea379b1812011-12-20 02:50:00 +000032void LiveRangeEdit::Delegate::anchor() { }
33
Mark Lacey9d8103d2013-08-14 23:50:16 +000034LiveInterval &LiveRangeEdit::createEmptyIntervalFrom(unsigned OldReg) {
Jakob Stoklund Olesen86308402011-03-17 20:37:07 +000035 unsigned VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
Pete Cooper4f0dbb22012-04-03 00:28:46 +000036 if (VRM) {
Pete Cooper4f0dbb22012-04-03 00:28:46 +000037 VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
38 }
Mark Lacey9d8103d2013-08-14 23:50:16 +000039 LiveInterval &LI = LIS.createEmptyInterval(VReg);
Jakob Stoklund Olesen86308402011-03-17 20:37:07 +000040 return LI;
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000041}
42
Mark Lacey9d8103d2013-08-14 23:50:16 +000043unsigned LiveRangeEdit::createFrom(unsigned OldReg) {
44 unsigned VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
45 if (VRM) {
46 VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
47 }
48 return VReg;
49}
50
Jakob Stoklund Olesen86e53ce2011-04-20 22:14:20 +000051bool LiveRangeEdit::checkRematerializable(VNInfo *VNI,
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +000052 const MachineInstr *DefMI,
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +000053 AliasAnalysis *aa) {
54 assert(DefMI && "Missing instruction");
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000055 ScannedRemattable = true;
Pete Cooper2bde2f42012-04-02 22:22:53 +000056 if (!TII.isTriviallyReMaterializable(DefMI, aa))
Jakob Stoklund Olesen86e53ce2011-04-20 22:14:20 +000057 return false;
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000058 Remattable.insert(VNI);
Jakob Stoklund Olesen86e53ce2011-04-20 22:14:20 +000059 return true;
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +000060}
61
Pete Cooper2bde2f42012-04-02 22:22:53 +000062void LiveRangeEdit::scanRemattable(AliasAnalysis *aa) {
Matthias Braun96761952014-12-10 23:07:54 +000063 for (VNInfo *VNI : getParent().valnos) {
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000064 if (VNI->isUnused())
65 continue;
Pete Cooper2bde2f42012-04-02 22:22:53 +000066 MachineInstr *DefMI = LIS.getInstructionFromIndex(VNI->def);
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000067 if (!DefMI)
68 continue;
Pete Cooper2bde2f42012-04-02 22:22:53 +000069 checkRematerializable(VNI, DefMI, aa);
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000070 }
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000071 ScannedRemattable = true;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000072}
73
Pete Cooper2bde2f42012-04-02 22:22:53 +000074bool LiveRangeEdit::anyRematerializable(AliasAnalysis *aa) {
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000075 if (!ScannedRemattable)
Pete Cooper2bde2f42012-04-02 22:22:53 +000076 scanRemattable(aa);
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000077 return !Remattable.empty();
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000078}
79
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000080/// allUsesAvailableAt - Return true if all registers used by OrigMI at
81/// OrigIdx are also available with the same value at UseIdx.
82bool LiveRangeEdit::allUsesAvailableAt(const MachineInstr *OrigMI,
83 SlotIndex OrigIdx,
Jakub Staszak26ac8a72013-03-18 23:40:46 +000084 SlotIndex UseIdx) const {
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +000085 OrigIdx = OrigIdx.getRegSlot(true);
86 UseIdx = UseIdx.getRegSlot(true);
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000087 for (unsigned i = 0, e = OrigMI->getNumOperands(); i != e; ++i) {
88 const MachineOperand &MO = OrigMI->getOperand(i);
Jakob Stoklund Olesen78095782012-06-22 17:31:01 +000089 if (!MO.isReg() || !MO.getReg() || !MO.readsReg())
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000090 continue;
Jakob Stoklund Olesen78095782012-06-22 17:31:01 +000091
92 // We can't remat physreg uses, unless it is a constant.
93 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Jakob Stoklund Olesen1d195822012-09-27 16:34:19 +000094 if (MRI.isConstantPhysReg(MO.getReg(), *OrigMI->getParent()->getParent()))
Jakob Stoklund Olesen78095782012-06-22 17:31:01 +000095 continue;
96 return false;
97 }
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000098
Pete Cooper2bde2f42012-04-02 22:22:53 +000099 LiveInterval &li = LIS.getInterval(MO.getReg());
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000100 const VNInfo *OVNI = li.getVNInfoAt(OrigIdx);
101 if (!OVNI)
102 continue;
Jakob Stoklund Olesen4df59a92012-10-16 22:51:58 +0000103
104 // Don't allow rematerialization immediately after the original def.
105 // It would be incorrect if OrigMI redefines the register.
106 // See PR14098.
107 if (SlotIndex::isSameInstr(OrigIdx, UseIdx))
108 return false;
109
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000110 if (OVNI != li.getVNInfoAt(UseIdx))
111 return false;
112 }
113 return true;
114}
115
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000116bool LiveRangeEdit::canRematerializeAt(Remat &RM,
117 SlotIndex UseIdx,
Pete Cooper2bde2f42012-04-02 22:22:53 +0000118 bool cheapAsAMove) {
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000119 assert(ScannedRemattable && "Call anyRematerializable first");
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000120
121 // Use scanRemattable info.
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000122 if (!Remattable.count(RM.ParentVNI))
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000123 return false;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000124
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000125 // No defining instruction provided.
126 SlotIndex DefIdx;
127 if (RM.OrigMI)
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000128 DefIdx = LIS.getInstructionIndex(*RM.OrigMI);
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000129 else {
130 DefIdx = RM.ParentVNI->def;
Pete Cooper2bde2f42012-04-02 22:22:53 +0000131 RM.OrigMI = LIS.getInstructionFromIndex(DefIdx);
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000132 assert(RM.OrigMI && "No defining instruction for remattable value");
133 }
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000134
135 // If only cheap remats were requested, bail out early.
Jiangning Liuc3053122014-07-29 01:55:19 +0000136 if (cheapAsAMove && !TII.isAsCheapAsAMove(RM.OrigMI))
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000137 return false;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000138
139 // Verify that all used registers are available with the same values.
Pete Cooper2bde2f42012-04-02 22:22:53 +0000140 if (!allUsesAvailableAt(RM.OrigMI, DefIdx, UseIdx))
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000141 return false;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000142
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000143 return true;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000144}
145
146SlotIndex LiveRangeEdit::rematerializeAt(MachineBasicBlock &MBB,
147 MachineBasicBlock::iterator MI,
148 unsigned DestReg,
149 const Remat &RM,
Jakob Stoklund Olesen7d406792011-05-02 05:29:58 +0000150 const TargetRegisterInfo &tri,
151 bool Late) {
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000152 assert(RM.OrigMI && "Invalid remat");
Pete Cooper2bde2f42012-04-02 22:22:53 +0000153 TII.reMaterialize(MBB, MI, DestReg, 0, RM.OrigMI, tri);
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000154 Rematted.insert(RM.ParentVNI);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000155 return LIS.getSlotIndexes()
156 ->insertMachineInstrInMaps(*--MI, Late)
157 .getRegSlot();
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000158}
159
Pete Cooper2bde2f42012-04-02 22:22:53 +0000160void LiveRangeEdit::eraseVirtReg(unsigned Reg) {
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000161 if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg))
Jakob Stoklund Olesen43a87502011-03-13 01:23:11 +0000162 LIS.removeInterval(Reg);
163}
164
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000165bool LiveRangeEdit::foldAsLoad(LiveInterval *LI,
Pete Cooper2bde2f42012-04-02 22:22:53 +0000166 SmallVectorImpl<MachineInstr*> &Dead) {
Craig Topperc0196b12014-04-14 00:51:57 +0000167 MachineInstr *DefMI = nullptr, *UseMI = nullptr;
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000168
169 // Check that there is a single def and a single use.
Owen Andersonb36376e2014-03-17 19:36:09 +0000170 for (MachineOperand &MO : MRI.reg_nodbg_operands(LI->reg)) {
171 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000172 if (MO.isDef()) {
173 if (DefMI && DefMI != MI)
174 return false;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000175 if (!MI->canFoldAsLoad())
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000176 return false;
177 DefMI = MI;
178 } else if (!MO.isUndef()) {
179 if (UseMI && UseMI != MI)
180 return false;
181 // FIXME: Targets don't know how to fold subreg uses.
182 if (MO.getSubReg())
183 return false;
184 UseMI = MI;
185 }
186 }
187 if (!DefMI || !UseMI)
188 return false;
189
Jakob Stoklund Olesene2cfd0d2012-07-20 21:29:31 +0000190 // Since we're moving the DefMI load, make sure we're not extending any live
191 // ranges.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000192 if (!allUsesAvailableAt(DefMI, LIS.getInstructionIndex(*DefMI),
193 LIS.getInstructionIndex(*UseMI)))
Jakob Stoklund Olesene2cfd0d2012-07-20 21:29:31 +0000194 return false;
195
196 // We also need to make sure it is safe to move the load.
197 // Assume there are stores between DefMI and UseMI.
198 bool SawStore = true;
Matthias Braun07066cc2015-05-19 21:22:20 +0000199 if (!DefMI->isSafeToMove(nullptr, SawStore))
Jakob Stoklund Olesene2cfd0d2012-07-20 21:29:31 +0000200 return false;
201
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000202 DEBUG(dbgs() << "Try to fold single def: " << *DefMI
203 << " into single use: " << *UseMI);
204
205 SmallVector<unsigned, 8> Ops;
206 if (UseMI->readsWritesVirtualRegister(LI->reg, &Ops).second)
207 return false;
208
209 MachineInstr *FoldMI = TII.foldMemoryOperand(UseMI, Ops, DefMI);
210 if (!FoldMI)
211 return false;
212 DEBUG(dbgs() << " folded: " << *FoldMI);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000213 LIS.ReplaceMachineInstrInMaps(*UseMI, *FoldMI);
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000214 UseMI->eraseFromParent();
Craig Topperc0196b12014-04-14 00:51:57 +0000215 DefMI->addRegisterDead(LI->reg, nullptr);
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000216 Dead.push_back(DefMI);
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000217 ++NumDCEFoldedLoads;
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000218 return true;
219}
220
Matthias Braunc1e029e2015-06-01 21:26:26 +0000221bool LiveRangeEdit::useIsKill(const LiveInterval &LI,
222 const MachineOperand &MO) const {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000223 const MachineInstr &MI = *MO.getParent();
Matthias Braunc1e029e2015-06-01 21:26:26 +0000224 SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
225 if (LI.Query(Idx).isKill())
226 return true;
227 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
228 unsigned SubReg = MO.getSubReg();
Matthias Braune6a24852015-09-25 21:51:14 +0000229 LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubReg);
Matthias Braunc1e029e2015-06-01 21:26:26 +0000230 for (const LiveInterval::SubRange &S : LI.subranges()) {
231 if ((S.LaneMask & LaneMask) != 0 && S.Query(Idx).isKill())
232 return true;
233 }
234 return false;
235}
236
Andrew Trick530fc1f2013-06-21 18:33:17 +0000237/// Find all live intervals that need to shrink, then remove the instruction.
238void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink) {
239 assert(MI->allDefsAreDead() && "Def isn't really dead");
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000240 SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
Andrew Trick530fc1f2013-06-21 18:33:17 +0000241
Andrew Trickcbd73052013-06-22 00:33:48 +0000242 // Never delete a bundled instruction.
243 if (MI->isBundled()) {
244 return;
245 }
Andrew Trick530fc1f2013-06-21 18:33:17 +0000246 // Never delete inline asm.
247 if (MI->isInlineAsm()) {
248 DEBUG(dbgs() << "Won't delete: " << Idx << '\t' << *MI);
249 return;
250 }
251
252 // Use the same criteria as DeadMachineInstructionElim.
253 bool SawStore = false;
Matthias Braun07066cc2015-05-19 21:22:20 +0000254 if (!MI->isSafeToMove(nullptr, SawStore)) {
Andrew Trick530fc1f2013-06-21 18:33:17 +0000255 DEBUG(dbgs() << "Can't delete: " << Idx << '\t' << *MI);
256 return;
257 }
258
259 DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI);
260
261 // Collect virtual registers to be erased after MI is gone.
262 SmallVector<unsigned, 8> RegsToErase;
263 bool ReadsPhysRegs = false;
264
265 // Check for live intervals that may shrink
266 for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
267 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
268 if (!MOI->isReg())
269 continue;
270 unsigned Reg = MOI->getReg();
271 if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
272 // Check if MI reads any unreserved physregs.
273 if (Reg && MOI->readsReg() && !MRI.isReserved(Reg))
274 ReadsPhysRegs = true;
Matthias Brauncfb8ad22015-01-21 18:50:21 +0000275 else if (MOI->isDef())
276 LIS.removePhysRegDefAt(Reg, Idx);
Andrew Trick530fc1f2013-06-21 18:33:17 +0000277 continue;
278 }
279 LiveInterval &LI = LIS.getInterval(Reg);
280
281 // Shrink read registers, unless it is likely to be expensive and
282 // unlikely to change anything. We typically don't want to shrink the
283 // PIC base register that has lots of uses everywhere.
284 // Always shrink COPY uses that probably come from live range splitting.
Matthias Braunc1e029e2015-06-01 21:26:26 +0000285 if ((MI->readsVirtualRegister(Reg) && (MI->isCopy() || MOI->isDef())) ||
286 (MOI->readsReg() && (MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, *MOI))))
Andrew Trick530fc1f2013-06-21 18:33:17 +0000287 ToShrink.insert(&LI);
288
289 // Remove defined value.
290 if (MOI->isDef()) {
Matthias Braun311730a2015-01-21 19:02:30 +0000291 if (TheDelegate && LI.getVNInfoAt(Idx) != nullptr)
292 TheDelegate->LRE_WillShrinkVirtReg(LI.reg);
293 LIS.removeVRegDefAt(LI, Idx);
294 if (LI.empty())
295 RegsToErase.push_back(Reg);
Andrew Trick530fc1f2013-06-21 18:33:17 +0000296 }
297 }
298
299 // Currently, we don't support DCE of physreg live ranges. If MI reads
300 // any unreserved physregs, don't erase the instruction, but turn it into
301 // a KILL instead. This way, the physreg live ranges don't end up
302 // dangling.
303 // FIXME: It would be better to have something like shrinkToUses() for
304 // physregs. That could potentially enable more DCE and it would free up
305 // the physreg. It would not happen often, though.
306 if (ReadsPhysRegs) {
307 MI->setDesc(TII.get(TargetOpcode::KILL));
308 // Remove all operands that aren't physregs.
309 for (unsigned i = MI->getNumOperands(); i; --i) {
310 const MachineOperand &MO = MI->getOperand(i-1);
311 if (MO.isReg() && TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
312 continue;
313 MI->RemoveOperand(i-1);
314 }
315 DEBUG(dbgs() << "Converted physregs to:\t" << *MI);
316 } else {
317 if (TheDelegate)
318 TheDelegate->LRE_WillEraseInstruction(MI);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000319 LIS.RemoveMachineInstrFromMaps(*MI);
Andrew Trick530fc1f2013-06-21 18:33:17 +0000320 MI->eraseFromParent();
321 ++NumDCEDeleted;
322 }
323
324 // Erase any virtregs that are now empty and unused. There may be <undef>
325 // uses around. Keep the empty live range in that case.
326 for (unsigned i = 0, e = RegsToErase.size(); i != e; ++i) {
327 unsigned Reg = RegsToErase[i];
328 if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) {
329 ToShrink.remove(&LIS.getInterval(Reg));
330 eraseVirtReg(Reg);
331 }
332 }
333}
334
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000335void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr*> &Dead,
Pete Cooper76e4bc42011-12-12 22:16:27 +0000336 ArrayRef<unsigned> RegsBeingSpilled) {
Andrew Trick530fc1f2013-06-21 18:33:17 +0000337 ToShrinkSet ToShrink;
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000338
339 for (;;) {
340 // Erase all dead defs.
Andrew Trick530fc1f2013-06-21 18:33:17 +0000341 while (!Dead.empty())
342 eliminateDeadDef(Dead.pop_back_val(), ToShrink);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000343
344 if (ToShrink.empty())
345 break;
346
347 // Shrink just one live interval. Then delete new dead defs.
Jakob Stoklund Olesene14b2b22011-03-16 22:56:16 +0000348 LiveInterval *LI = ToShrink.back();
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000349 ToShrink.pop_back();
Pete Cooper2bde2f42012-04-02 22:22:53 +0000350 if (foldAsLoad(LI, Dead))
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000351 continue;
Matthias Braund3dd1352015-09-22 03:44:41 +0000352 unsigned VReg = LI->reg;
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000353 if (TheDelegate)
Matthias Braund3dd1352015-09-22 03:44:41 +0000354 TheDelegate->LRE_WillShrinkVirtReg(VReg);
Jakob Stoklund Olesen86308402011-03-17 20:37:07 +0000355 if (!LIS.shrinkToUses(LI, &Dead))
356 continue;
Andrew Trick7df3f012013-06-21 18:33:14 +0000357
Pete Cooper76e4bc42011-12-12 22:16:27 +0000358 // Don't create new intervals for a register being spilled.
359 // The new intervals would have to be spilled anyway so its not worth it.
360 // Also they currently aren't spilled so creating them and not spilling
361 // them results in incorrect code.
362 bool BeingSpilled = false;
363 for (unsigned i = 0, e = RegsBeingSpilled.size(); i != e; ++i) {
Matthias Braund3dd1352015-09-22 03:44:41 +0000364 if (VReg == RegsBeingSpilled[i]) {
Pete Cooper76e4bc42011-12-12 22:16:27 +0000365 BeingSpilled = true;
366 break;
367 }
368 }
Andrew Trick7df3f012013-06-21 18:33:14 +0000369
Pete Cooper76e4bc42011-12-12 22:16:27 +0000370 if (BeingSpilled) continue;
Jakob Stoklund Olesen86308402011-03-17 20:37:07 +0000371
372 // LI may have been separated, create new intervals.
Jakob Stoklund Olesen4417c7b2013-08-14 17:28:52 +0000373 LI->RenumberValues();
Matthias Braund3dd1352015-09-22 03:44:41 +0000374 SmallVector<LiveInterval*, 8> SplitLIs;
375 LIS.splitSeparateComponents(*LI, SplitLIs);
376 if (!SplitLIs.empty())
377 ++NumFracRanges;
378
379 unsigned Original = VRM ? VRM->getOriginal(VReg) : 0;
380 for (const LiveInterval *SplitLI : SplitLIs) {
Jakob Stoklund Olesenbbad3bc2011-07-05 15:38:41 +0000381 // If LI is an original interval that hasn't been split yet, make the new
382 // intervals their own originals instead of referring to LI. The original
383 // interval must contain all the split products, and LI doesn't.
Matthias Braund3dd1352015-09-22 03:44:41 +0000384 if (Original != VReg && Original != 0)
385 VRM->setIsSplitFromReg(SplitLI->reg, Original);
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000386 if (TheDelegate)
Matthias Braund3dd1352015-09-22 03:44:41 +0000387 TheDelegate->LRE_DidCloneVirtReg(SplitLI->reg, VReg);
Jakob Stoklund Olesendd9a2ec2011-03-30 02:52:39 +0000388 }
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000389 }
390}
391
Mark Laceyf367cd92013-08-14 23:50:09 +0000392// Keep track of new virtual registers created via
393// MachineRegisterInfo::createVirtualRegister.
394void
395LiveRangeEdit::MRI_NoteNewVirtualRegister(unsigned VReg)
396{
397 if (VRM)
398 VRM->grow();
399
400 NewRegs.push_back(VReg);
401}
402
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000403void
404LiveRangeEdit::calculateRegClassAndHint(MachineFunction &MF,
405 const MachineLoopInfo &Loops,
406 const MachineBlockFrequencyInfo &MBFI) {
Robert Lougher11a44b72015-08-10 11:59:44 +0000407 VirtRegAuxInfo VRAI(MF, LIS, VRM, Loops, MBFI);
Mark Laceyf9ea8852013-08-14 23:50:04 +0000408 for (unsigned I = 0, Size = size(); I < Size; ++I) {
409 LiveInterval &LI = LIS.getInterval(get(I));
Eric Christopher349d5882015-01-27 01:15:16 +0000410 if (MRI.recomputeRegClass(LI.reg))
Craig Toppercf0444b2014-11-17 05:50:14 +0000411 DEBUG({
412 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
413 dbgs() << "Inflated " << PrintReg(LI.reg) << " to "
Craig Topperf98c6062014-11-17 05:58:26 +0000414 << TRI->getRegClassName(MRI.getRegClass(LI.reg)) << '\n';
Craig Toppercf0444b2014-11-17 05:50:14 +0000415 });
Arnaud A. de Grandmaisonea3ac162013-11-11 19:04:45 +0000416 VRAI.calculateSpillWeightAndHint(LI);
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000417 }
418}