blob: 20003dded7000cd53c82c30289e8612f6763e83f [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;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +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;
Wei Mi9a16d652016-04-13 03:08:27 +000066 unsigned Original = VRM->getOriginal(getReg());
67 LiveInterval &OrigLI = LIS.getInterval(Original);
68 VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
69 MachineInstr *DefMI = LIS.getInstructionFromIndex(OrigVNI->def);
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000070 if (!DefMI)
71 continue;
Wei Mi9a16d652016-04-13 03:08:27 +000072 checkRematerializable(OrigVNI, DefMI, aa);
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000073 }
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000074 ScannedRemattable = true;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000075}
76
Pete Cooper2bde2f42012-04-02 22:22:53 +000077bool LiveRangeEdit::anyRematerializable(AliasAnalysis *aa) {
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000078 if (!ScannedRemattable)
Pete Cooper2bde2f42012-04-02 22:22:53 +000079 scanRemattable(aa);
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000080 return !Remattable.empty();
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000081}
82
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000083/// allUsesAvailableAt - Return true if all registers used by OrigMI at
84/// OrigIdx are also available with the same value at UseIdx.
85bool LiveRangeEdit::allUsesAvailableAt(const MachineInstr *OrigMI,
86 SlotIndex OrigIdx,
Jakub Staszak26ac8a72013-03-18 23:40:46 +000087 SlotIndex UseIdx) const {
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +000088 OrigIdx = OrigIdx.getRegSlot(true);
89 UseIdx = UseIdx.getRegSlot(true);
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000090 for (unsigned i = 0, e = OrigMI->getNumOperands(); i != e; ++i) {
91 const MachineOperand &MO = OrigMI->getOperand(i);
Jakob Stoklund Olesen78095782012-06-22 17:31:01 +000092 if (!MO.isReg() || !MO.getReg() || !MO.readsReg())
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000093 continue;
Jakob Stoklund Olesen78095782012-06-22 17:31:01 +000094
95 // We can't remat physreg uses, unless it is a constant.
96 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Jakob Stoklund Olesen1d195822012-09-27 16:34:19 +000097 if (MRI.isConstantPhysReg(MO.getReg(), *OrigMI->getParent()->getParent()))
Jakob Stoklund Olesen78095782012-06-22 17:31:01 +000098 continue;
99 return false;
100 }
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000101
Pete Cooper2bde2f42012-04-02 22:22:53 +0000102 LiveInterval &li = LIS.getInterval(MO.getReg());
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000103 const VNInfo *OVNI = li.getVNInfoAt(OrigIdx);
104 if (!OVNI)
105 continue;
Jakob Stoklund Olesen4df59a92012-10-16 22:51:58 +0000106
107 // Don't allow rematerialization immediately after the original def.
108 // It would be incorrect if OrigMI redefines the register.
109 // See PR14098.
110 if (SlotIndex::isSameInstr(OrigIdx, UseIdx))
111 return false;
112
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000113 if (OVNI != li.getVNInfoAt(UseIdx))
114 return false;
115 }
116 return true;
117}
118
Wei Mi9a16d652016-04-13 03:08:27 +0000119bool LiveRangeEdit::canRematerializeAt(Remat &RM, VNInfo *OrigVNI,
120 SlotIndex UseIdx, bool cheapAsAMove) {
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000121 assert(ScannedRemattable && "Call anyRematerializable first");
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000122
123 // Use scanRemattable info.
Wei Mi9a16d652016-04-13 03:08:27 +0000124 if (!Remattable.count(OrigVNI))
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000125 return false;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000126
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000127 // No defining instruction provided.
128 SlotIndex DefIdx;
Wei Mi9a16d652016-04-13 03:08:27 +0000129 assert(RM.OrigMI && "No defining instruction for remattable value");
130 DefIdx = LIS.getInstructionIndex(*RM.OrigMI);
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000131
132 // If only cheap remats were requested, bail out early.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000133 if (cheapAsAMove && !TII.isAsCheapAsAMove(*RM.OrigMI))
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000134 return false;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000135
136 // Verify that all used registers are available with the same values.
Pete Cooper2bde2f42012-04-02 22:22:53 +0000137 if (!allUsesAvailableAt(RM.OrigMI, DefIdx, UseIdx))
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000138 return false;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000139
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000140 return true;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000141}
142
143SlotIndex LiveRangeEdit::rematerializeAt(MachineBasicBlock &MBB,
144 MachineBasicBlock::iterator MI,
145 unsigned DestReg,
146 const Remat &RM,
Jakob Stoklund Olesen7d406792011-05-02 05:29:58 +0000147 const TargetRegisterInfo &tri,
148 bool Late) {
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000149 assert(RM.OrigMI && "Invalid remat");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000150 TII.reMaterialize(MBB, MI, DestReg, 0, *RM.OrigMI, tri);
Wei Mi9a16d652016-04-13 03:08:27 +0000151 // DestReg of the cloned instruction cannot be Dead. Set isDead of DestReg
152 // to false anyway in case the isDead flag of RM.OrigMI's dest register
153 // is true.
154 (*--MI).getOperand(0).setIsDead(false);
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000155 Rematted.insert(RM.ParentVNI);
Wei Mi9a16d652016-04-13 03:08:27 +0000156 return LIS.getSlotIndexes()->insertMachineInstrInMaps(*MI, Late).getRegSlot();
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000157}
158
Pete Cooper2bde2f42012-04-02 22:22:53 +0000159void LiveRangeEdit::eraseVirtReg(unsigned Reg) {
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000160 if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg))
Jakob Stoklund Olesen43a87502011-03-13 01:23:11 +0000161 LIS.removeInterval(Reg);
162}
163
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000164bool LiveRangeEdit::foldAsLoad(LiveInterval *LI,
Pete Cooper2bde2f42012-04-02 22:22:53 +0000165 SmallVectorImpl<MachineInstr*> &Dead) {
Craig Topperc0196b12014-04-14 00:51:57 +0000166 MachineInstr *DefMI = nullptr, *UseMI = nullptr;
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000167
168 // Check that there is a single def and a single use.
Owen Andersonb36376e2014-03-17 19:36:09 +0000169 for (MachineOperand &MO : MRI.reg_nodbg_operands(LI->reg)) {
170 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000171 if (MO.isDef()) {
172 if (DefMI && DefMI != MI)
173 return false;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000174 if (!MI->canFoldAsLoad())
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000175 return false;
176 DefMI = MI;
177 } else if (!MO.isUndef()) {
178 if (UseMI && UseMI != MI)
179 return false;
180 // FIXME: Targets don't know how to fold subreg uses.
181 if (MO.getSubReg())
182 return false;
183 UseMI = MI;
184 }
185 }
186 if (!DefMI || !UseMI)
187 return false;
188
Jakob Stoklund Olesene2cfd0d2012-07-20 21:29:31 +0000189 // Since we're moving the DefMI load, make sure we're not extending any live
190 // ranges.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000191 if (!allUsesAvailableAt(DefMI, LIS.getInstructionIndex(*DefMI),
192 LIS.getInstructionIndex(*UseMI)))
Jakob Stoklund Olesene2cfd0d2012-07-20 21:29:31 +0000193 return false;
194
195 // We also need to make sure it is safe to move the load.
196 // Assume there are stores between DefMI and UseMI.
197 bool SawStore = true;
Matthias Braun07066cc2015-05-19 21:22:20 +0000198 if (!DefMI->isSafeToMove(nullptr, SawStore))
Jakob Stoklund Olesene2cfd0d2012-07-20 21:29:31 +0000199 return false;
200
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000201 DEBUG(dbgs() << "Try to fold single def: " << *DefMI
202 << " into single use: " << *UseMI);
203
204 SmallVector<unsigned, 8> Ops;
205 if (UseMI->readsWritesVirtualRegister(LI->reg, &Ops).second)
206 return false;
207
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000208 MachineInstr *FoldMI = TII.foldMemoryOperand(*UseMI, Ops, *DefMI, &LIS);
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000209 if (!FoldMI)
210 return false;
211 DEBUG(dbgs() << " folded: " << *FoldMI);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000212 LIS.ReplaceMachineInstrInMaps(*UseMI, *FoldMI);
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000213 UseMI->eraseFromParent();
Craig Topperc0196b12014-04-14 00:51:57 +0000214 DefMI->addRegisterDead(LI->reg, nullptr);
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000215 Dead.push_back(DefMI);
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000216 ++NumDCEFoldedLoads;
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000217 return true;
218}
219
Matthias Braunc1e029e2015-06-01 21:26:26 +0000220bool LiveRangeEdit::useIsKill(const LiveInterval &LI,
221 const MachineOperand &MO) const {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000222 const MachineInstr &MI = *MO.getParent();
Matthias Braunc1e029e2015-06-01 21:26:26 +0000223 SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
224 if (LI.Query(Idx).isKill())
225 return true;
226 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
227 unsigned SubReg = MO.getSubReg();
Matthias Braune6a24852015-09-25 21:51:14 +0000228 LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubReg);
Matthias Braunc1e029e2015-06-01 21:26:26 +0000229 for (const LiveInterval::SubRange &S : LI.subranges()) {
230 if ((S.LaneMask & LaneMask) != 0 && S.Query(Idx).isKill())
231 return true;
232 }
233 return false;
234}
235
Andrew Trick530fc1f2013-06-21 18:33:17 +0000236/// Find all live intervals that need to shrink, then remove the instruction.
237void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink) {
238 assert(MI->allDefsAreDead() && "Def isn't really dead");
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000239 SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
Andrew Trick530fc1f2013-06-21 18:33:17 +0000240
Andrew Trickcbd73052013-06-22 00:33:48 +0000241 // Never delete a bundled instruction.
242 if (MI->isBundled()) {
243 return;
244 }
Andrew Trick530fc1f2013-06-21 18:33:17 +0000245 // Never delete inline asm.
246 if (MI->isInlineAsm()) {
247 DEBUG(dbgs() << "Won't delete: " << Idx << '\t' << *MI);
248 return;
249 }
250
251 // Use the same criteria as DeadMachineInstructionElim.
252 bool SawStore = false;
Matthias Braun07066cc2015-05-19 21:22:20 +0000253 if (!MI->isSafeToMove(nullptr, SawStore)) {
Andrew Trick530fc1f2013-06-21 18:33:17 +0000254 DEBUG(dbgs() << "Can't delete: " << Idx << '\t' << *MI);
255 return;
256 }
257
258 DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI);
259
260 // Collect virtual registers to be erased after MI is gone.
261 SmallVector<unsigned, 8> RegsToErase;
262 bool ReadsPhysRegs = false;
Wei Mi9a16d652016-04-13 03:08:27 +0000263 bool isOrigDef = false;
264 unsigned Dest;
265 if (VRM && MI->getOperand(0).isReg()) {
266 Dest = MI->getOperand(0).getReg();
267 unsigned Original = VRM->getOriginal(Dest);
268 LiveInterval &OrigLI = LIS.getInterval(Original);
269 VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
Quentin Colombetd3079092016-06-09 21:34:31 +0000270 // The original live-range may have been shrunk to
271 // an empty live-range. It happens when it is dead, but
272 // we still keep it around to be able to rematerialize
273 // other values that depend on it.
274 if (OrigVNI)
275 isOrigDef = SlotIndex::isSameInstr(OrigVNI->def, Idx);
Wei Mi9a16d652016-04-13 03:08:27 +0000276 }
Andrew Trick530fc1f2013-06-21 18:33:17 +0000277
278 // Check for live intervals that may shrink
279 for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
280 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
281 if (!MOI->isReg())
282 continue;
283 unsigned Reg = MOI->getReg();
284 if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
285 // Check if MI reads any unreserved physregs.
286 if (Reg && MOI->readsReg() && !MRI.isReserved(Reg))
287 ReadsPhysRegs = true;
Matthias Brauncfb8ad22015-01-21 18:50:21 +0000288 else if (MOI->isDef())
289 LIS.removePhysRegDefAt(Reg, Idx);
Andrew Trick530fc1f2013-06-21 18:33:17 +0000290 continue;
291 }
292 LiveInterval &LI = LIS.getInterval(Reg);
293
294 // Shrink read registers, unless it is likely to be expensive and
295 // unlikely to change anything. We typically don't want to shrink the
296 // PIC base register that has lots of uses everywhere.
297 // Always shrink COPY uses that probably come from live range splitting.
Matthias Braunc1e029e2015-06-01 21:26:26 +0000298 if ((MI->readsVirtualRegister(Reg) && (MI->isCopy() || MOI->isDef())) ||
299 (MOI->readsReg() && (MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, *MOI))))
Andrew Trick530fc1f2013-06-21 18:33:17 +0000300 ToShrink.insert(&LI);
301
302 // Remove defined value.
303 if (MOI->isDef()) {
Matthias Braun311730a2015-01-21 19:02:30 +0000304 if (TheDelegate && LI.getVNInfoAt(Idx) != nullptr)
305 TheDelegate->LRE_WillShrinkVirtReg(LI.reg);
306 LIS.removeVRegDefAt(LI, Idx);
307 if (LI.empty())
308 RegsToErase.push_back(Reg);
Andrew Trick530fc1f2013-06-21 18:33:17 +0000309 }
310 }
311
312 // Currently, we don't support DCE of physreg live ranges. If MI reads
313 // any unreserved physregs, don't erase the instruction, but turn it into
314 // a KILL instead. This way, the physreg live ranges don't end up
315 // dangling.
316 // FIXME: It would be better to have something like shrinkToUses() for
317 // physregs. That could potentially enable more DCE and it would free up
318 // the physreg. It would not happen often, though.
319 if (ReadsPhysRegs) {
320 MI->setDesc(TII.get(TargetOpcode::KILL));
321 // Remove all operands that aren't physregs.
322 for (unsigned i = MI->getNumOperands(); i; --i) {
323 const MachineOperand &MO = MI->getOperand(i-1);
324 if (MO.isReg() && TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
325 continue;
326 MI->RemoveOperand(i-1);
327 }
328 DEBUG(dbgs() << "Converted physregs to:\t" << *MI);
329 } else {
Wei Mi9a16d652016-04-13 03:08:27 +0000330 // If the dest of MI is an original reg, don't delete the inst. Replace
331 // the dest with a new reg, keep the inst for remat of other siblings.
332 // The inst is saved in LiveRangeEdit::DeadRemats and will be deleted
333 // after all the allocations of the func are done.
334 if (isOrigDef) {
335 LiveInterval &NewLI = createEmptyIntervalFrom(Dest);
336 VNInfo *VNI = NewLI.getNextValue(Idx, LIS.getVNInfoAllocator());
337 NewLI.addSegment(LiveInterval::Segment(Idx, Idx.getDeadSlot(), VNI));
338 pop_back();
339 markDeadRemat(MI);
340 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
341 MI->substituteRegister(Dest, NewLI.reg, 0, TRI);
342 MI->getOperand(0).setIsDead(true);
343 } else {
344 if (TheDelegate)
345 TheDelegate->LRE_WillEraseInstruction(MI);
346 LIS.RemoveMachineInstrFromMaps(*MI);
347 MI->eraseFromParent();
348 ++NumDCEDeleted;
349 }
Andrew Trick530fc1f2013-06-21 18:33:17 +0000350 }
351
352 // Erase any virtregs that are now empty and unused. There may be <undef>
353 // uses around. Keep the empty live range in that case.
354 for (unsigned i = 0, e = RegsToErase.size(); i != e; ++i) {
355 unsigned Reg = RegsToErase[i];
356 if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) {
357 ToShrink.remove(&LIS.getInterval(Reg));
358 eraseVirtReg(Reg);
359 }
360 }
361}
362
Wei Mi9a16d652016-04-13 03:08:27 +0000363void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr *> &Dead,
Wei Mi963f2df2016-04-15 23:16:44 +0000364 ArrayRef<unsigned> RegsBeingSpilled) {
Andrew Trick530fc1f2013-06-21 18:33:17 +0000365 ToShrinkSet ToShrink;
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000366
367 for (;;) {
368 // Erase all dead defs.
Andrew Trick530fc1f2013-06-21 18:33:17 +0000369 while (!Dead.empty())
370 eliminateDeadDef(Dead.pop_back_val(), ToShrink);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000371
372 if (ToShrink.empty())
373 break;
374
375 // Shrink just one live interval. Then delete new dead defs.
Jakob Stoklund Olesene14b2b22011-03-16 22:56:16 +0000376 LiveInterval *LI = ToShrink.back();
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000377 ToShrink.pop_back();
Pete Cooper2bde2f42012-04-02 22:22:53 +0000378 if (foldAsLoad(LI, Dead))
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000379 continue;
Matthias Braund3dd1352015-09-22 03:44:41 +0000380 unsigned VReg = LI->reg;
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000381 if (TheDelegate)
Matthias Braund3dd1352015-09-22 03:44:41 +0000382 TheDelegate->LRE_WillShrinkVirtReg(VReg);
Jakob Stoklund Olesen86308402011-03-17 20:37:07 +0000383 if (!LIS.shrinkToUses(LI, &Dead))
384 continue;
Andrew Trick7df3f012013-06-21 18:33:14 +0000385
Pete Cooper76e4bc42011-12-12 22:16:27 +0000386 // Don't create new intervals for a register being spilled.
387 // The new intervals would have to be spilled anyway so its not worth it.
388 // Also they currently aren't spilled so creating them and not spilling
389 // them results in incorrect code.
390 bool BeingSpilled = false;
391 for (unsigned i = 0, e = RegsBeingSpilled.size(); i != e; ++i) {
Matthias Braund3dd1352015-09-22 03:44:41 +0000392 if (VReg == RegsBeingSpilled[i]) {
Pete Cooper76e4bc42011-12-12 22:16:27 +0000393 BeingSpilled = true;
394 break;
395 }
396 }
Andrew Trick7df3f012013-06-21 18:33:14 +0000397
Pete Cooper76e4bc42011-12-12 22:16:27 +0000398 if (BeingSpilled) continue;
Jakob Stoklund Olesen86308402011-03-17 20:37:07 +0000399
400 // LI may have been separated, create new intervals.
Jakob Stoklund Olesen4417c7b2013-08-14 17:28:52 +0000401 LI->RenumberValues();
Matthias Braund3dd1352015-09-22 03:44:41 +0000402 SmallVector<LiveInterval*, 8> SplitLIs;
403 LIS.splitSeparateComponents(*LI, SplitLIs);
404 if (!SplitLIs.empty())
405 ++NumFracRanges;
406
407 unsigned Original = VRM ? VRM->getOriginal(VReg) : 0;
408 for (const LiveInterval *SplitLI : SplitLIs) {
Jakob Stoklund Olesenbbad3bc2011-07-05 15:38:41 +0000409 // If LI is an original interval that hasn't been split yet, make the new
410 // intervals their own originals instead of referring to LI. The original
411 // interval must contain all the split products, and LI doesn't.
Matthias Braund3dd1352015-09-22 03:44:41 +0000412 if (Original != VReg && Original != 0)
413 VRM->setIsSplitFromReg(SplitLI->reg, Original);
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000414 if (TheDelegate)
Matthias Braund3dd1352015-09-22 03:44:41 +0000415 TheDelegate->LRE_DidCloneVirtReg(SplitLI->reg, VReg);
Jakob Stoklund Olesendd9a2ec2011-03-30 02:52:39 +0000416 }
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000417 }
418}
419
Mark Laceyf367cd92013-08-14 23:50:09 +0000420// Keep track of new virtual registers created via
421// MachineRegisterInfo::createVirtualRegister.
422void
423LiveRangeEdit::MRI_NoteNewVirtualRegister(unsigned VReg)
424{
425 if (VRM)
426 VRM->grow();
427
428 NewRegs.push_back(VReg);
429}
430
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000431void
432LiveRangeEdit::calculateRegClassAndHint(MachineFunction &MF,
433 const MachineLoopInfo &Loops,
434 const MachineBlockFrequencyInfo &MBFI) {
Robert Lougher11a44b72015-08-10 11:59:44 +0000435 VirtRegAuxInfo VRAI(MF, LIS, VRM, Loops, MBFI);
Mark Laceyf9ea8852013-08-14 23:50:04 +0000436 for (unsigned I = 0, Size = size(); I < Size; ++I) {
437 LiveInterval &LI = LIS.getInterval(get(I));
Eric Christopher349d5882015-01-27 01:15:16 +0000438 if (MRI.recomputeRegClass(LI.reg))
Craig Toppercf0444b2014-11-17 05:50:14 +0000439 DEBUG({
440 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
441 dbgs() << "Inflated " << PrintReg(LI.reg) << " to "
Craig Topperf98c6062014-11-17 05:58:26 +0000442 << TRI->getRegClassName(MRI.getRegClass(LI.reg)) << '\n';
Craig Toppercf0444b2014-11-17 05:50:14 +0000443 });
Arnaud A. de Grandmaisonea3ac162013-11-11 19:04:45 +0000444 VRAI.calculateSpillWeightAndHint(LI);
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000445 }
446}