blob: 92cca1a54951e4e6f31c59ce2147580ac38aefcb [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);
Quentin Colombet5725f562017-02-02 20:44:36 +000040 if (Parent && !Parent->isSpillable())
41 LI.markNotSpillable();
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000042 // Create empty subranges if the OldReg's interval has them. Do not create
43 // the main range here---it will be constructed later after the subranges
44 // have been finalized.
45 LiveInterval &OldLI = LIS.getInterval(OldReg);
46 VNInfo::Allocator &Alloc = LIS.getVNInfoAllocator();
47 for (LiveInterval::SubRange &S : OldLI.subranges())
48 LI.createSubRange(Alloc, S.LaneMask);
Jakob Stoklund Olesen86308402011-03-17 20:37:07 +000049 return LI;
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000050}
51
Mark Lacey9d8103d2013-08-14 23:50:16 +000052unsigned LiveRangeEdit::createFrom(unsigned OldReg) {
53 unsigned VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
54 if (VRM) {
55 VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
56 }
Quentin Colombet5725f562017-02-02 20:44:36 +000057 // FIXME: Getting the interval here actually computes it.
58 // In theory, this may not be what we want, but in practice
59 // the createEmptyIntervalFrom API is used when this is not
60 // the case. Generally speaking we just want to annotate the
61 // LiveInterval when it gets created but we cannot do that at
62 // the moment.
63 if (Parent && !Parent->isSpillable())
64 LIS.getInterval(VReg).markNotSpillable();
Mark Lacey9d8103d2013-08-14 23:50:16 +000065 return VReg;
66}
67
Jakob Stoklund Olesen86e53ce2011-04-20 22:14:20 +000068bool LiveRangeEdit::checkRematerializable(VNInfo *VNI,
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +000069 const MachineInstr *DefMI,
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +000070 AliasAnalysis *aa) {
71 assert(DefMI && "Missing instruction");
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000072 ScannedRemattable = true;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +000073 if (!TII.isTriviallyReMaterializable(*DefMI, aa))
Jakob Stoklund Olesen86e53ce2011-04-20 22:14:20 +000074 return false;
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000075 Remattable.insert(VNI);
Jakob Stoklund Olesen86e53ce2011-04-20 22:14:20 +000076 return true;
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +000077}
78
Pete Cooper2bde2f42012-04-02 22:22:53 +000079void LiveRangeEdit::scanRemattable(AliasAnalysis *aa) {
Matthias Braun96761952014-12-10 23:07:54 +000080 for (VNInfo *VNI : getParent().valnos) {
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000081 if (VNI->isUnused())
82 continue;
Wei Mi9a16d652016-04-13 03:08:27 +000083 unsigned Original = VRM->getOriginal(getReg());
84 LiveInterval &OrigLI = LIS.getInterval(Original);
85 VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000086 if (!OrigVNI)
87 continue;
Wei Mi9a16d652016-04-13 03:08:27 +000088 MachineInstr *DefMI = LIS.getInstructionFromIndex(OrigVNI->def);
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000089 if (!DefMI)
90 continue;
Wei Mi9a16d652016-04-13 03:08:27 +000091 checkRematerializable(OrigVNI, DefMI, aa);
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000092 }
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000093 ScannedRemattable = true;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000094}
95
Pete Cooper2bde2f42012-04-02 22:22:53 +000096bool LiveRangeEdit::anyRematerializable(AliasAnalysis *aa) {
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000097 if (!ScannedRemattable)
Pete Cooper2bde2f42012-04-02 22:22:53 +000098 scanRemattable(aa);
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000099 return !Remattable.empty();
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000100}
101
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000102/// allUsesAvailableAt - Return true if all registers used by OrigMI at
103/// OrigIdx are also available with the same value at UseIdx.
104bool LiveRangeEdit::allUsesAvailableAt(const MachineInstr *OrigMI,
105 SlotIndex OrigIdx,
Jakub Staszak26ac8a72013-03-18 23:40:46 +0000106 SlotIndex UseIdx) const {
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000107 OrigIdx = OrigIdx.getRegSlot(true);
108 UseIdx = UseIdx.getRegSlot(true);
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000109 for (unsigned i = 0, e = OrigMI->getNumOperands(); i != e; ++i) {
110 const MachineOperand &MO = OrigMI->getOperand(i);
Jakob Stoklund Olesen78095782012-06-22 17:31:01 +0000111 if (!MO.isReg() || !MO.getReg() || !MO.readsReg())
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000112 continue;
Jakob Stoklund Olesen78095782012-06-22 17:31:01 +0000113
114 // We can't remat physreg uses, unless it is a constant.
115 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Matthias Braunde8c1b32016-10-28 18:05:09 +0000116 if (MRI.isConstantPhysReg(MO.getReg()))
Jakob Stoklund Olesen78095782012-06-22 17:31:01 +0000117 continue;
118 return false;
119 }
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000120
Pete Cooper2bde2f42012-04-02 22:22:53 +0000121 LiveInterval &li = LIS.getInterval(MO.getReg());
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000122 const VNInfo *OVNI = li.getVNInfoAt(OrigIdx);
123 if (!OVNI)
124 continue;
Jakob Stoklund Olesen4df59a92012-10-16 22:51:58 +0000125
126 // Don't allow rematerialization immediately after the original def.
127 // It would be incorrect if OrigMI redefines the register.
128 // See PR14098.
129 if (SlotIndex::isSameInstr(OrigIdx, UseIdx))
130 return false;
131
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000132 if (OVNI != li.getVNInfoAt(UseIdx))
133 return false;
134 }
135 return true;
136}
137
Wei Mi9a16d652016-04-13 03:08:27 +0000138bool LiveRangeEdit::canRematerializeAt(Remat &RM, VNInfo *OrigVNI,
139 SlotIndex UseIdx, bool cheapAsAMove) {
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000140 assert(ScannedRemattable && "Call anyRematerializable first");
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000141
142 // Use scanRemattable info.
Wei Mi9a16d652016-04-13 03:08:27 +0000143 if (!Remattable.count(OrigVNI))
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000144 return false;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000145
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000146 // No defining instruction provided.
147 SlotIndex DefIdx;
Wei Mi9a16d652016-04-13 03:08:27 +0000148 assert(RM.OrigMI && "No defining instruction for remattable value");
149 DefIdx = LIS.getInstructionIndex(*RM.OrigMI);
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000150
151 // If only cheap remats were requested, bail out early.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000152 if (cheapAsAMove && !TII.isAsCheapAsAMove(*RM.OrigMI))
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000153 return false;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000154
155 // Verify that all used registers are available with the same values.
Pete Cooper2bde2f42012-04-02 22:22:53 +0000156 if (!allUsesAvailableAt(RM.OrigMI, DefIdx, UseIdx))
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000157 return false;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000158
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000159 return true;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000160}
161
162SlotIndex LiveRangeEdit::rematerializeAt(MachineBasicBlock &MBB,
163 MachineBasicBlock::iterator MI,
164 unsigned DestReg,
165 const Remat &RM,
Jakob Stoklund Olesen7d406792011-05-02 05:29:58 +0000166 const TargetRegisterInfo &tri,
167 bool Late) {
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000168 assert(RM.OrigMI && "Invalid remat");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000169 TII.reMaterialize(MBB, MI, DestReg, 0, *RM.OrigMI, tri);
Wei Mi9a16d652016-04-13 03:08:27 +0000170 // DestReg of the cloned instruction cannot be Dead. Set isDead of DestReg
171 // to false anyway in case the isDead flag of RM.OrigMI's dest register
172 // is true.
173 (*--MI).getOperand(0).setIsDead(false);
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000174 Rematted.insert(RM.ParentVNI);
Wei Mi9a16d652016-04-13 03:08:27 +0000175 return LIS.getSlotIndexes()->insertMachineInstrInMaps(*MI, Late).getRegSlot();
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000176}
177
Pete Cooper2bde2f42012-04-02 22:22:53 +0000178void LiveRangeEdit::eraseVirtReg(unsigned Reg) {
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000179 if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg))
Jakob Stoklund Olesen43a87502011-03-13 01:23:11 +0000180 LIS.removeInterval(Reg);
181}
182
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000183bool LiveRangeEdit::foldAsLoad(LiveInterval *LI,
Pete Cooper2bde2f42012-04-02 22:22:53 +0000184 SmallVectorImpl<MachineInstr*> &Dead) {
Craig Topperc0196b12014-04-14 00:51:57 +0000185 MachineInstr *DefMI = nullptr, *UseMI = nullptr;
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000186
187 // Check that there is a single def and a single use.
Owen Andersonb36376e2014-03-17 19:36:09 +0000188 for (MachineOperand &MO : MRI.reg_nodbg_operands(LI->reg)) {
189 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000190 if (MO.isDef()) {
191 if (DefMI && DefMI != MI)
192 return false;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000193 if (!MI->canFoldAsLoad())
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000194 return false;
195 DefMI = MI;
196 } else if (!MO.isUndef()) {
197 if (UseMI && UseMI != MI)
198 return false;
199 // FIXME: Targets don't know how to fold subreg uses.
200 if (MO.getSubReg())
201 return false;
202 UseMI = MI;
203 }
204 }
205 if (!DefMI || !UseMI)
206 return false;
207
Jakob Stoklund Olesene2cfd0d2012-07-20 21:29:31 +0000208 // Since we're moving the DefMI load, make sure we're not extending any live
209 // ranges.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000210 if (!allUsesAvailableAt(DefMI, LIS.getInstructionIndex(*DefMI),
211 LIS.getInstructionIndex(*UseMI)))
Jakob Stoklund Olesene2cfd0d2012-07-20 21:29:31 +0000212 return false;
213
214 // We also need to make sure it is safe to move the load.
215 // Assume there are stores between DefMI and UseMI.
216 bool SawStore = true;
Matthias Braun07066cc2015-05-19 21:22:20 +0000217 if (!DefMI->isSafeToMove(nullptr, SawStore))
Jakob Stoklund Olesene2cfd0d2012-07-20 21:29:31 +0000218 return false;
219
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000220 DEBUG(dbgs() << "Try to fold single def: " << *DefMI
221 << " into single use: " << *UseMI);
222
223 SmallVector<unsigned, 8> Ops;
224 if (UseMI->readsWritesVirtualRegister(LI->reg, &Ops).second)
225 return false;
226
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000227 MachineInstr *FoldMI = TII.foldMemoryOperand(*UseMI, Ops, *DefMI, &LIS);
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000228 if (!FoldMI)
229 return false;
230 DEBUG(dbgs() << " folded: " << *FoldMI);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000231 LIS.ReplaceMachineInstrInMaps(*UseMI, *FoldMI);
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000232 UseMI->eraseFromParent();
Craig Topperc0196b12014-04-14 00:51:57 +0000233 DefMI->addRegisterDead(LI->reg, nullptr);
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000234 Dead.push_back(DefMI);
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000235 ++NumDCEFoldedLoads;
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000236 return true;
237}
238
Matthias Braunc1e029e2015-06-01 21:26:26 +0000239bool LiveRangeEdit::useIsKill(const LiveInterval &LI,
240 const MachineOperand &MO) const {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000241 const MachineInstr &MI = *MO.getParent();
Matthias Braunc1e029e2015-06-01 21:26:26 +0000242 SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
243 if (LI.Query(Idx).isKill())
244 return true;
245 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
246 unsigned SubReg = MO.getSubReg();
Matthias Braune6a24852015-09-25 21:51:14 +0000247 LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubReg);
Matthias Braunc1e029e2015-06-01 21:26:26 +0000248 for (const LiveInterval::SubRange &S : LI.subranges()) {
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000249 if ((S.LaneMask & LaneMask).any() && S.Query(Idx).isKill())
Matthias Braunc1e029e2015-06-01 21:26:26 +0000250 return true;
251 }
252 return false;
253}
254
Andrew Trick530fc1f2013-06-21 18:33:17 +0000255/// Find all live intervals that need to shrink, then remove the instruction.
Wei Mic0223702016-07-08 21:08:09 +0000256void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink,
257 AliasAnalysis *AA) {
Andrew Trick530fc1f2013-06-21 18:33:17 +0000258 assert(MI->allDefsAreDead() && "Def isn't really dead");
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000259 SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
Andrew Trick530fc1f2013-06-21 18:33:17 +0000260
Andrew Trickcbd73052013-06-22 00:33:48 +0000261 // Never delete a bundled instruction.
262 if (MI->isBundled()) {
263 return;
264 }
Andrew Trick530fc1f2013-06-21 18:33:17 +0000265 // Never delete inline asm.
266 if (MI->isInlineAsm()) {
267 DEBUG(dbgs() << "Won't delete: " << Idx << '\t' << *MI);
268 return;
269 }
270
271 // Use the same criteria as DeadMachineInstructionElim.
272 bool SawStore = false;
Matthias Braun07066cc2015-05-19 21:22:20 +0000273 if (!MI->isSafeToMove(nullptr, SawStore)) {
Andrew Trick530fc1f2013-06-21 18:33:17 +0000274 DEBUG(dbgs() << "Can't delete: " << Idx << '\t' << *MI);
275 return;
276 }
277
278 DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI);
279
280 // Collect virtual registers to be erased after MI is gone.
281 SmallVector<unsigned, 8> RegsToErase;
282 bool ReadsPhysRegs = false;
Wei Mi9a16d652016-04-13 03:08:27 +0000283 bool isOrigDef = false;
284 unsigned Dest;
Geoff Berry66d1f0f2016-12-15 19:55:19 +0000285 // Only optimize rematerialize case when the instruction has one def, since
286 // otherwise we could leave some dead defs in the code. This case is
287 // extremely rare.
288 if (VRM && MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
289 MI->getDesc().getNumDefs() == 1) {
Wei Mi9a16d652016-04-13 03:08:27 +0000290 Dest = MI->getOperand(0).getReg();
291 unsigned Original = VRM->getOriginal(Dest);
292 LiveInterval &OrigLI = LIS.getInterval(Original);
293 VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
Quentin Colombetd3079092016-06-09 21:34:31 +0000294 // The original live-range may have been shrunk to
295 // an empty live-range. It happens when it is dead, but
296 // we still keep it around to be able to rematerialize
297 // other values that depend on it.
298 if (OrigVNI)
299 isOrigDef = SlotIndex::isSameInstr(OrigVNI->def, Idx);
Wei Mi9a16d652016-04-13 03:08:27 +0000300 }
Andrew Trick530fc1f2013-06-21 18:33:17 +0000301
302 // Check for live intervals that may shrink
303 for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
304 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
305 if (!MOI->isReg())
306 continue;
307 unsigned Reg = MOI->getReg();
308 if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
309 // Check if MI reads any unreserved physregs.
310 if (Reg && MOI->readsReg() && !MRI.isReserved(Reg))
311 ReadsPhysRegs = true;
Matthias Brauncfb8ad22015-01-21 18:50:21 +0000312 else if (MOI->isDef())
313 LIS.removePhysRegDefAt(Reg, Idx);
Andrew Trick530fc1f2013-06-21 18:33:17 +0000314 continue;
315 }
316 LiveInterval &LI = LIS.getInterval(Reg);
317
318 // Shrink read registers, unless it is likely to be expensive and
319 // unlikely to change anything. We typically don't want to shrink the
320 // PIC base register that has lots of uses everywhere.
321 // Always shrink COPY uses that probably come from live range splitting.
Matthias Braunc1e029e2015-06-01 21:26:26 +0000322 if ((MI->readsVirtualRegister(Reg) && (MI->isCopy() || MOI->isDef())) ||
323 (MOI->readsReg() && (MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, *MOI))))
Andrew Trick530fc1f2013-06-21 18:33:17 +0000324 ToShrink.insert(&LI);
325
326 // Remove defined value.
327 if (MOI->isDef()) {
Matthias Braun311730a2015-01-21 19:02:30 +0000328 if (TheDelegate && LI.getVNInfoAt(Idx) != nullptr)
329 TheDelegate->LRE_WillShrinkVirtReg(LI.reg);
330 LIS.removeVRegDefAt(LI, Idx);
331 if (LI.empty())
332 RegsToErase.push_back(Reg);
Andrew Trick530fc1f2013-06-21 18:33:17 +0000333 }
334 }
335
336 // Currently, we don't support DCE of physreg live ranges. If MI reads
337 // any unreserved physregs, don't erase the instruction, but turn it into
338 // a KILL instead. This way, the physreg live ranges don't end up
339 // dangling.
340 // FIXME: It would be better to have something like shrinkToUses() for
341 // physregs. That could potentially enable more DCE and it would free up
342 // the physreg. It would not happen often, though.
343 if (ReadsPhysRegs) {
344 MI->setDesc(TII.get(TargetOpcode::KILL));
345 // Remove all operands that aren't physregs.
346 for (unsigned i = MI->getNumOperands(); i; --i) {
347 const MachineOperand &MO = MI->getOperand(i-1);
348 if (MO.isReg() && TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
349 continue;
350 MI->RemoveOperand(i-1);
351 }
352 DEBUG(dbgs() << "Converted physregs to:\t" << *MI);
353 } else {
Wei Mic0223702016-07-08 21:08:09 +0000354 // If the dest of MI is an original reg and MI is reMaterializable,
355 // don't delete the inst. Replace the dest with a new reg, and keep
356 // the inst for remat of other siblings. The inst is saved in
357 // LiveRangeEdit::DeadRemats and will be deleted after all the
358 // allocations of the func are done.
359 if (isOrigDef && DeadRemats && TII.isTriviallyReMaterializable(*MI, AA)) {
Wei Mi9a16d652016-04-13 03:08:27 +0000360 LiveInterval &NewLI = createEmptyIntervalFrom(Dest);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000361 NewLI.removeEmptySubRanges();
Wei Mi9a16d652016-04-13 03:08:27 +0000362 VNInfo *VNI = NewLI.getNextValue(Idx, LIS.getVNInfoAllocator());
363 NewLI.addSegment(LiveInterval::Segment(Idx, Idx.getDeadSlot(), VNI));
364 pop_back();
365 markDeadRemat(MI);
366 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
367 MI->substituteRegister(Dest, NewLI.reg, 0, TRI);
368 MI->getOperand(0).setIsDead(true);
369 } else {
370 if (TheDelegate)
371 TheDelegate->LRE_WillEraseInstruction(MI);
372 LIS.RemoveMachineInstrFromMaps(*MI);
373 MI->eraseFromParent();
374 ++NumDCEDeleted;
375 }
Andrew Trick530fc1f2013-06-21 18:33:17 +0000376 }
377
378 // Erase any virtregs that are now empty and unused. There may be <undef>
379 // uses around. Keep the empty live range in that case.
380 for (unsigned i = 0, e = RegsToErase.size(); i != e; ++i) {
381 unsigned Reg = RegsToErase[i];
382 if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) {
383 ToShrink.remove(&LIS.getInterval(Reg));
384 eraseVirtReg(Reg);
385 }
386 }
387}
388
Wei Mi9a16d652016-04-13 03:08:27 +0000389void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr *> &Dead,
Wei Mic0223702016-07-08 21:08:09 +0000390 ArrayRef<unsigned> RegsBeingSpilled,
391 AliasAnalysis *AA) {
Andrew Trick530fc1f2013-06-21 18:33:17 +0000392 ToShrinkSet ToShrink;
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000393
394 for (;;) {
395 // Erase all dead defs.
Andrew Trick530fc1f2013-06-21 18:33:17 +0000396 while (!Dead.empty())
Wei Mic0223702016-07-08 21:08:09 +0000397 eliminateDeadDef(Dead.pop_back_val(), ToShrink, AA);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000398
399 if (ToShrink.empty())
400 break;
401
402 // Shrink just one live interval. Then delete new dead defs.
Jakob Stoklund Olesene14b2b22011-03-16 22:56:16 +0000403 LiveInterval *LI = ToShrink.back();
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000404 ToShrink.pop_back();
Pete Cooper2bde2f42012-04-02 22:22:53 +0000405 if (foldAsLoad(LI, Dead))
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000406 continue;
Matthias Braund3dd1352015-09-22 03:44:41 +0000407 unsigned VReg = LI->reg;
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000408 if (TheDelegate)
Matthias Braund3dd1352015-09-22 03:44:41 +0000409 TheDelegate->LRE_WillShrinkVirtReg(VReg);
Jakob Stoklund Olesen86308402011-03-17 20:37:07 +0000410 if (!LIS.shrinkToUses(LI, &Dead))
411 continue;
Andrew Trick7df3f012013-06-21 18:33:14 +0000412
Pete Cooper76e4bc42011-12-12 22:16:27 +0000413 // Don't create new intervals for a register being spilled.
414 // The new intervals would have to be spilled anyway so its not worth it.
415 // Also they currently aren't spilled so creating them and not spilling
416 // them results in incorrect code.
417 bool BeingSpilled = false;
418 for (unsigned i = 0, e = RegsBeingSpilled.size(); i != e; ++i) {
Matthias Braund3dd1352015-09-22 03:44:41 +0000419 if (VReg == RegsBeingSpilled[i]) {
Pete Cooper76e4bc42011-12-12 22:16:27 +0000420 BeingSpilled = true;
421 break;
422 }
423 }
Andrew Trick7df3f012013-06-21 18:33:14 +0000424
Pete Cooper76e4bc42011-12-12 22:16:27 +0000425 if (BeingSpilled) continue;
Jakob Stoklund Olesen86308402011-03-17 20:37:07 +0000426
427 // LI may have been separated, create new intervals.
Jakob Stoklund Olesen4417c7b2013-08-14 17:28:52 +0000428 LI->RenumberValues();
Matthias Braund3dd1352015-09-22 03:44:41 +0000429 SmallVector<LiveInterval*, 8> SplitLIs;
430 LIS.splitSeparateComponents(*LI, SplitLIs);
431 if (!SplitLIs.empty())
432 ++NumFracRanges;
433
434 unsigned Original = VRM ? VRM->getOriginal(VReg) : 0;
435 for (const LiveInterval *SplitLI : SplitLIs) {
Jakob Stoklund Olesenbbad3bc2011-07-05 15:38:41 +0000436 // If LI is an original interval that hasn't been split yet, make the new
437 // intervals their own originals instead of referring to LI. The original
438 // interval must contain all the split products, and LI doesn't.
Matthias Braund3dd1352015-09-22 03:44:41 +0000439 if (Original != VReg && Original != 0)
440 VRM->setIsSplitFromReg(SplitLI->reg, Original);
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000441 if (TheDelegate)
Matthias Braund3dd1352015-09-22 03:44:41 +0000442 TheDelegate->LRE_DidCloneVirtReg(SplitLI->reg, VReg);
Jakob Stoklund Olesendd9a2ec2011-03-30 02:52:39 +0000443 }
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000444 }
445}
446
Mark Laceyf367cd92013-08-14 23:50:09 +0000447// Keep track of new virtual registers created via
448// MachineRegisterInfo::createVirtualRegister.
449void
450LiveRangeEdit::MRI_NoteNewVirtualRegister(unsigned VReg)
451{
452 if (VRM)
453 VRM->grow();
454
455 NewRegs.push_back(VReg);
456}
457
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000458void
459LiveRangeEdit::calculateRegClassAndHint(MachineFunction &MF,
460 const MachineLoopInfo &Loops,
461 const MachineBlockFrequencyInfo &MBFI) {
Robert Lougher11a44b72015-08-10 11:59:44 +0000462 VirtRegAuxInfo VRAI(MF, LIS, VRM, Loops, MBFI);
Mark Laceyf9ea8852013-08-14 23:50:04 +0000463 for (unsigned I = 0, Size = size(); I < Size; ++I) {
464 LiveInterval &LI = LIS.getInterval(get(I));
Eric Christopher349d5882015-01-27 01:15:16 +0000465 if (MRI.recomputeRegClass(LI.reg))
Craig Toppercf0444b2014-11-17 05:50:14 +0000466 DEBUG({
467 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
468 dbgs() << "Inflated " << PrintReg(LI.reg) << " to "
Craig Topperf98c6062014-11-17 05:58:26 +0000469 << TRI->getRegClassName(MRI.getRegClass(LI.reg)) << '\n';
Craig Toppercf0444b2014-11-17 05:50:14 +0000470 });
Arnaud A. de Grandmaisonea3ac162013-11-11 19:04:45 +0000471 VRAI.calculateSpillWeightAndHint(LI);
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000472 }
473}