blob: 7f1c69c0b4a2a19a7e9dffe6c5e2c107fb6c5126 [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);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000040 // Create empty subranges if the OldReg's interval has them. Do not create
41 // the main range here---it will be constructed later after the subranges
42 // have been finalized.
43 LiveInterval &OldLI = LIS.getInterval(OldReg);
44 VNInfo::Allocator &Alloc = LIS.getVNInfoAllocator();
45 for (LiveInterval::SubRange &S : OldLI.subranges())
46 LI.createSubRange(Alloc, S.LaneMask);
Jakob Stoklund Olesen86308402011-03-17 20:37:07 +000047 return LI;
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000048}
49
Mark Lacey9d8103d2013-08-14 23:50:16 +000050unsigned LiveRangeEdit::createFrom(unsigned OldReg) {
51 unsigned VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
52 if (VRM) {
53 VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
54 }
55 return VReg;
56}
57
Jakob Stoklund Olesen86e53ce2011-04-20 22:14:20 +000058bool LiveRangeEdit::checkRematerializable(VNInfo *VNI,
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +000059 const MachineInstr *DefMI,
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +000060 AliasAnalysis *aa) {
61 assert(DefMI && "Missing instruction");
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000062 ScannedRemattable = true;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +000063 if (!TII.isTriviallyReMaterializable(*DefMI, aa))
Jakob Stoklund Olesen86e53ce2011-04-20 22:14:20 +000064 return false;
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000065 Remattable.insert(VNI);
Jakob Stoklund Olesen86e53ce2011-04-20 22:14:20 +000066 return true;
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +000067}
68
Pete Cooper2bde2f42012-04-02 22:22:53 +000069void LiveRangeEdit::scanRemattable(AliasAnalysis *aa) {
Matthias Braun96761952014-12-10 23:07:54 +000070 for (VNInfo *VNI : getParent().valnos) {
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000071 if (VNI->isUnused())
72 continue;
Wei Mi9a16d652016-04-13 03:08:27 +000073 unsigned Original = VRM->getOriginal(getReg());
74 LiveInterval &OrigLI = LIS.getInterval(Original);
75 VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000076 if (!OrigVNI)
77 continue;
Wei Mi9a16d652016-04-13 03:08:27 +000078 MachineInstr *DefMI = LIS.getInstructionFromIndex(OrigVNI->def);
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000079 if (!DefMI)
80 continue;
Wei Mi9a16d652016-04-13 03:08:27 +000081 checkRematerializable(OrigVNI, DefMI, aa);
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000082 }
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000083 ScannedRemattable = true;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000084}
85
Pete Cooper2bde2f42012-04-02 22:22:53 +000086bool LiveRangeEdit::anyRematerializable(AliasAnalysis *aa) {
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000087 if (!ScannedRemattable)
Pete Cooper2bde2f42012-04-02 22:22:53 +000088 scanRemattable(aa);
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000089 return !Remattable.empty();
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000090}
91
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000092/// allUsesAvailableAt - Return true if all registers used by OrigMI at
93/// OrigIdx are also available with the same value at UseIdx.
94bool LiveRangeEdit::allUsesAvailableAt(const MachineInstr *OrigMI,
95 SlotIndex OrigIdx,
Jakub Staszak26ac8a72013-03-18 23:40:46 +000096 SlotIndex UseIdx) const {
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +000097 OrigIdx = OrigIdx.getRegSlot(true);
98 UseIdx = UseIdx.getRegSlot(true);
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000099 for (unsigned i = 0, e = OrigMI->getNumOperands(); i != e; ++i) {
100 const MachineOperand &MO = OrigMI->getOperand(i);
Jakob Stoklund Olesen78095782012-06-22 17:31:01 +0000101 if (!MO.isReg() || !MO.getReg() || !MO.readsReg())
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000102 continue;
Jakob Stoklund Olesen78095782012-06-22 17:31:01 +0000103
104 // We can't remat physreg uses, unless it is a constant.
105 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Matthias Braunde8c1b32016-10-28 18:05:09 +0000106 if (MRI.isConstantPhysReg(MO.getReg()))
Jakob Stoklund Olesen78095782012-06-22 17:31:01 +0000107 continue;
108 return false;
109 }
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000110
Pete Cooper2bde2f42012-04-02 22:22:53 +0000111 LiveInterval &li = LIS.getInterval(MO.getReg());
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000112 const VNInfo *OVNI = li.getVNInfoAt(OrigIdx);
113 if (!OVNI)
114 continue;
Jakob Stoklund Olesen4df59a92012-10-16 22:51:58 +0000115
116 // Don't allow rematerialization immediately after the original def.
117 // It would be incorrect if OrigMI redefines the register.
118 // See PR14098.
119 if (SlotIndex::isSameInstr(OrigIdx, UseIdx))
120 return false;
121
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000122 if (OVNI != li.getVNInfoAt(UseIdx))
123 return false;
124 }
125 return true;
126}
127
Wei Mi9a16d652016-04-13 03:08:27 +0000128bool LiveRangeEdit::canRematerializeAt(Remat &RM, VNInfo *OrigVNI,
129 SlotIndex UseIdx, bool cheapAsAMove) {
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000130 assert(ScannedRemattable && "Call anyRematerializable first");
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000131
132 // Use scanRemattable info.
Wei Mi9a16d652016-04-13 03:08:27 +0000133 if (!Remattable.count(OrigVNI))
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000134 return false;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000135
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000136 // No defining instruction provided.
137 SlotIndex DefIdx;
Wei Mi9a16d652016-04-13 03:08:27 +0000138 assert(RM.OrigMI && "No defining instruction for remattable value");
139 DefIdx = LIS.getInstructionIndex(*RM.OrigMI);
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000140
141 // If only cheap remats were requested, bail out early.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000142 if (cheapAsAMove && !TII.isAsCheapAsAMove(*RM.OrigMI))
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000143 return false;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000144
145 // Verify that all used registers are available with the same values.
Pete Cooper2bde2f42012-04-02 22:22:53 +0000146 if (!allUsesAvailableAt(RM.OrigMI, DefIdx, UseIdx))
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000147 return false;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000148
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000149 return true;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000150}
151
152SlotIndex LiveRangeEdit::rematerializeAt(MachineBasicBlock &MBB,
153 MachineBasicBlock::iterator MI,
154 unsigned DestReg,
155 const Remat &RM,
Jakob Stoklund Olesen7d406792011-05-02 05:29:58 +0000156 const TargetRegisterInfo &tri,
157 bool Late) {
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000158 assert(RM.OrigMI && "Invalid remat");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000159 TII.reMaterialize(MBB, MI, DestReg, 0, *RM.OrigMI, tri);
Wei Mi9a16d652016-04-13 03:08:27 +0000160 // DestReg of the cloned instruction cannot be Dead. Set isDead of DestReg
161 // to false anyway in case the isDead flag of RM.OrigMI's dest register
162 // is true.
163 (*--MI).getOperand(0).setIsDead(false);
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000164 Rematted.insert(RM.ParentVNI);
Wei Mi9a16d652016-04-13 03:08:27 +0000165 return LIS.getSlotIndexes()->insertMachineInstrInMaps(*MI, Late).getRegSlot();
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000166}
167
Pete Cooper2bde2f42012-04-02 22:22:53 +0000168void LiveRangeEdit::eraseVirtReg(unsigned Reg) {
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000169 if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg))
Jakob Stoklund Olesen43a87502011-03-13 01:23:11 +0000170 LIS.removeInterval(Reg);
171}
172
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000173bool LiveRangeEdit::foldAsLoad(LiveInterval *LI,
Pete Cooper2bde2f42012-04-02 22:22:53 +0000174 SmallVectorImpl<MachineInstr*> &Dead) {
Craig Topperc0196b12014-04-14 00:51:57 +0000175 MachineInstr *DefMI = nullptr, *UseMI = nullptr;
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000176
177 // Check that there is a single def and a single use.
Owen Andersonb36376e2014-03-17 19:36:09 +0000178 for (MachineOperand &MO : MRI.reg_nodbg_operands(LI->reg)) {
179 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000180 if (MO.isDef()) {
181 if (DefMI && DefMI != MI)
182 return false;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000183 if (!MI->canFoldAsLoad())
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000184 return false;
185 DefMI = MI;
186 } else if (!MO.isUndef()) {
187 if (UseMI && UseMI != MI)
188 return false;
189 // FIXME: Targets don't know how to fold subreg uses.
190 if (MO.getSubReg())
191 return false;
192 UseMI = MI;
193 }
194 }
195 if (!DefMI || !UseMI)
196 return false;
197
Jakob Stoklund Olesene2cfd0d2012-07-20 21:29:31 +0000198 // Since we're moving the DefMI load, make sure we're not extending any live
199 // ranges.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000200 if (!allUsesAvailableAt(DefMI, LIS.getInstructionIndex(*DefMI),
201 LIS.getInstructionIndex(*UseMI)))
Jakob Stoklund Olesene2cfd0d2012-07-20 21:29:31 +0000202 return false;
203
204 // We also need to make sure it is safe to move the load.
205 // Assume there are stores between DefMI and UseMI.
206 bool SawStore = true;
Matthias Braun07066cc2015-05-19 21:22:20 +0000207 if (!DefMI->isSafeToMove(nullptr, SawStore))
Jakob Stoklund Olesene2cfd0d2012-07-20 21:29:31 +0000208 return false;
209
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000210 DEBUG(dbgs() << "Try to fold single def: " << *DefMI
211 << " into single use: " << *UseMI);
212
213 SmallVector<unsigned, 8> Ops;
214 if (UseMI->readsWritesVirtualRegister(LI->reg, &Ops).second)
215 return false;
216
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000217 MachineInstr *FoldMI = TII.foldMemoryOperand(*UseMI, Ops, *DefMI, &LIS);
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000218 if (!FoldMI)
219 return false;
220 DEBUG(dbgs() << " folded: " << *FoldMI);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000221 LIS.ReplaceMachineInstrInMaps(*UseMI, *FoldMI);
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000222 UseMI->eraseFromParent();
Craig Topperc0196b12014-04-14 00:51:57 +0000223 DefMI->addRegisterDead(LI->reg, nullptr);
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000224 Dead.push_back(DefMI);
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000225 ++NumDCEFoldedLoads;
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000226 return true;
227}
228
Matthias Braunc1e029e2015-06-01 21:26:26 +0000229bool LiveRangeEdit::useIsKill(const LiveInterval &LI,
230 const MachineOperand &MO) const {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000231 const MachineInstr &MI = *MO.getParent();
Matthias Braunc1e029e2015-06-01 21:26:26 +0000232 SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
233 if (LI.Query(Idx).isKill())
234 return true;
235 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
236 unsigned SubReg = MO.getSubReg();
Matthias Braune6a24852015-09-25 21:51:14 +0000237 LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubReg);
Matthias Braunc1e029e2015-06-01 21:26:26 +0000238 for (const LiveInterval::SubRange &S : LI.subranges()) {
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000239 if ((S.LaneMask & LaneMask).any() && S.Query(Idx).isKill())
Matthias Braunc1e029e2015-06-01 21:26:26 +0000240 return true;
241 }
242 return false;
243}
244
Andrew Trick530fc1f2013-06-21 18:33:17 +0000245/// Find all live intervals that need to shrink, then remove the instruction.
Wei Mic0223702016-07-08 21:08:09 +0000246void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink,
247 AliasAnalysis *AA) {
Andrew Trick530fc1f2013-06-21 18:33:17 +0000248 assert(MI->allDefsAreDead() && "Def isn't really dead");
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000249 SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
Andrew Trick530fc1f2013-06-21 18:33:17 +0000250
Andrew Trickcbd73052013-06-22 00:33:48 +0000251 // Never delete a bundled instruction.
252 if (MI->isBundled()) {
253 return;
254 }
Andrew Trick530fc1f2013-06-21 18:33:17 +0000255 // Never delete inline asm.
256 if (MI->isInlineAsm()) {
257 DEBUG(dbgs() << "Won't delete: " << Idx << '\t' << *MI);
258 return;
259 }
260
261 // Use the same criteria as DeadMachineInstructionElim.
262 bool SawStore = false;
Matthias Braun07066cc2015-05-19 21:22:20 +0000263 if (!MI->isSafeToMove(nullptr, SawStore)) {
Andrew Trick530fc1f2013-06-21 18:33:17 +0000264 DEBUG(dbgs() << "Can't delete: " << Idx << '\t' << *MI);
265 return;
266 }
267
268 DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI);
269
270 // Collect virtual registers to be erased after MI is gone.
271 SmallVector<unsigned, 8> RegsToErase;
272 bool ReadsPhysRegs = false;
Wei Mi9a16d652016-04-13 03:08:27 +0000273 bool isOrigDef = false;
274 unsigned Dest;
Geoff Berry66d1f0f2016-12-15 19:55:19 +0000275 // Only optimize rematerialize case when the instruction has one def, since
276 // otherwise we could leave some dead defs in the code. This case is
277 // extremely rare.
278 if (VRM && MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
279 MI->getDesc().getNumDefs() == 1) {
Wei Mi9a16d652016-04-13 03:08:27 +0000280 Dest = MI->getOperand(0).getReg();
281 unsigned Original = VRM->getOriginal(Dest);
282 LiveInterval &OrigLI = LIS.getInterval(Original);
283 VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
Quentin Colombetd3079092016-06-09 21:34:31 +0000284 // The original live-range may have been shrunk to
285 // an empty live-range. It happens when it is dead, but
286 // we still keep it around to be able to rematerialize
287 // other values that depend on it.
288 if (OrigVNI)
289 isOrigDef = SlotIndex::isSameInstr(OrigVNI->def, Idx);
Wei Mi9a16d652016-04-13 03:08:27 +0000290 }
Andrew Trick530fc1f2013-06-21 18:33:17 +0000291
292 // Check for live intervals that may shrink
293 for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
294 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
295 if (!MOI->isReg())
296 continue;
297 unsigned Reg = MOI->getReg();
298 if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
299 // Check if MI reads any unreserved physregs.
300 if (Reg && MOI->readsReg() && !MRI.isReserved(Reg))
301 ReadsPhysRegs = true;
Matthias Brauncfb8ad22015-01-21 18:50:21 +0000302 else if (MOI->isDef())
303 LIS.removePhysRegDefAt(Reg, Idx);
Andrew Trick530fc1f2013-06-21 18:33:17 +0000304 continue;
305 }
306 LiveInterval &LI = LIS.getInterval(Reg);
307
308 // Shrink read registers, unless it is likely to be expensive and
309 // unlikely to change anything. We typically don't want to shrink the
310 // PIC base register that has lots of uses everywhere.
311 // Always shrink COPY uses that probably come from live range splitting.
Matthias Braunc1e029e2015-06-01 21:26:26 +0000312 if ((MI->readsVirtualRegister(Reg) && (MI->isCopy() || MOI->isDef())) ||
313 (MOI->readsReg() && (MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, *MOI))))
Andrew Trick530fc1f2013-06-21 18:33:17 +0000314 ToShrink.insert(&LI);
315
316 // Remove defined value.
317 if (MOI->isDef()) {
Matthias Braun311730a2015-01-21 19:02:30 +0000318 if (TheDelegate && LI.getVNInfoAt(Idx) != nullptr)
319 TheDelegate->LRE_WillShrinkVirtReg(LI.reg);
320 LIS.removeVRegDefAt(LI, Idx);
321 if (LI.empty())
322 RegsToErase.push_back(Reg);
Andrew Trick530fc1f2013-06-21 18:33:17 +0000323 }
324 }
325
326 // Currently, we don't support DCE of physreg live ranges. If MI reads
327 // any unreserved physregs, don't erase the instruction, but turn it into
328 // a KILL instead. This way, the physreg live ranges don't end up
329 // dangling.
330 // FIXME: It would be better to have something like shrinkToUses() for
331 // physregs. That could potentially enable more DCE and it would free up
332 // the physreg. It would not happen often, though.
333 if (ReadsPhysRegs) {
334 MI->setDesc(TII.get(TargetOpcode::KILL));
335 // Remove all operands that aren't physregs.
336 for (unsigned i = MI->getNumOperands(); i; --i) {
337 const MachineOperand &MO = MI->getOperand(i-1);
338 if (MO.isReg() && TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
339 continue;
340 MI->RemoveOperand(i-1);
341 }
342 DEBUG(dbgs() << "Converted physregs to:\t" << *MI);
343 } else {
Wei Mic0223702016-07-08 21:08:09 +0000344 // If the dest of MI is an original reg and MI is reMaterializable,
345 // don't delete the inst. Replace the dest with a new reg, and keep
346 // the inst for remat of other siblings. The inst is saved in
347 // LiveRangeEdit::DeadRemats and will be deleted after all the
348 // allocations of the func are done.
349 if (isOrigDef && DeadRemats && TII.isTriviallyReMaterializable(*MI, AA)) {
Wei Mi9a16d652016-04-13 03:08:27 +0000350 LiveInterval &NewLI = createEmptyIntervalFrom(Dest);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000351 NewLI.removeEmptySubRanges();
Wei Mi9a16d652016-04-13 03:08:27 +0000352 VNInfo *VNI = NewLI.getNextValue(Idx, LIS.getVNInfoAllocator());
353 NewLI.addSegment(LiveInterval::Segment(Idx, Idx.getDeadSlot(), VNI));
354 pop_back();
355 markDeadRemat(MI);
356 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
357 MI->substituteRegister(Dest, NewLI.reg, 0, TRI);
358 MI->getOperand(0).setIsDead(true);
359 } else {
360 if (TheDelegate)
361 TheDelegate->LRE_WillEraseInstruction(MI);
362 LIS.RemoveMachineInstrFromMaps(*MI);
363 MI->eraseFromParent();
364 ++NumDCEDeleted;
365 }
Andrew Trick530fc1f2013-06-21 18:33:17 +0000366 }
367
368 // Erase any virtregs that are now empty and unused. There may be <undef>
369 // uses around. Keep the empty live range in that case.
370 for (unsigned i = 0, e = RegsToErase.size(); i != e; ++i) {
371 unsigned Reg = RegsToErase[i];
372 if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) {
373 ToShrink.remove(&LIS.getInterval(Reg));
374 eraseVirtReg(Reg);
375 }
376 }
377}
378
Wei Mi9a16d652016-04-13 03:08:27 +0000379void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr *> &Dead,
Wei Mic0223702016-07-08 21:08:09 +0000380 ArrayRef<unsigned> RegsBeingSpilled,
381 AliasAnalysis *AA) {
Andrew Trick530fc1f2013-06-21 18:33:17 +0000382 ToShrinkSet ToShrink;
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000383
384 for (;;) {
385 // Erase all dead defs.
Andrew Trick530fc1f2013-06-21 18:33:17 +0000386 while (!Dead.empty())
Wei Mic0223702016-07-08 21:08:09 +0000387 eliminateDeadDef(Dead.pop_back_val(), ToShrink, AA);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000388
389 if (ToShrink.empty())
390 break;
391
392 // Shrink just one live interval. Then delete new dead defs.
Jakob Stoklund Olesene14b2b22011-03-16 22:56:16 +0000393 LiveInterval *LI = ToShrink.back();
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000394 ToShrink.pop_back();
Pete Cooper2bde2f42012-04-02 22:22:53 +0000395 if (foldAsLoad(LI, Dead))
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000396 continue;
Matthias Braund3dd1352015-09-22 03:44:41 +0000397 unsigned VReg = LI->reg;
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000398 if (TheDelegate)
Matthias Braund3dd1352015-09-22 03:44:41 +0000399 TheDelegate->LRE_WillShrinkVirtReg(VReg);
Jakob Stoklund Olesen86308402011-03-17 20:37:07 +0000400 if (!LIS.shrinkToUses(LI, &Dead))
401 continue;
Andrew Trick7df3f012013-06-21 18:33:14 +0000402
Pete Cooper76e4bc42011-12-12 22:16:27 +0000403 // Don't create new intervals for a register being spilled.
404 // The new intervals would have to be spilled anyway so its not worth it.
405 // Also they currently aren't spilled so creating them and not spilling
406 // them results in incorrect code.
407 bool BeingSpilled = false;
408 for (unsigned i = 0, e = RegsBeingSpilled.size(); i != e; ++i) {
Matthias Braund3dd1352015-09-22 03:44:41 +0000409 if (VReg == RegsBeingSpilled[i]) {
Pete Cooper76e4bc42011-12-12 22:16:27 +0000410 BeingSpilled = true;
411 break;
412 }
413 }
Andrew Trick7df3f012013-06-21 18:33:14 +0000414
Pete Cooper76e4bc42011-12-12 22:16:27 +0000415 if (BeingSpilled) continue;
Jakob Stoklund Olesen86308402011-03-17 20:37:07 +0000416
417 // LI may have been separated, create new intervals.
Jakob Stoklund Olesen4417c7b2013-08-14 17:28:52 +0000418 LI->RenumberValues();
Matthias Braund3dd1352015-09-22 03:44:41 +0000419 SmallVector<LiveInterval*, 8> SplitLIs;
420 LIS.splitSeparateComponents(*LI, SplitLIs);
421 if (!SplitLIs.empty())
422 ++NumFracRanges;
423
424 unsigned Original = VRM ? VRM->getOriginal(VReg) : 0;
425 for (const LiveInterval *SplitLI : SplitLIs) {
Jakob Stoklund Olesenbbad3bc2011-07-05 15:38:41 +0000426 // If LI is an original interval that hasn't been split yet, make the new
427 // intervals their own originals instead of referring to LI. The original
428 // interval must contain all the split products, and LI doesn't.
Matthias Braund3dd1352015-09-22 03:44:41 +0000429 if (Original != VReg && Original != 0)
430 VRM->setIsSplitFromReg(SplitLI->reg, Original);
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000431 if (TheDelegate)
Matthias Braund3dd1352015-09-22 03:44:41 +0000432 TheDelegate->LRE_DidCloneVirtReg(SplitLI->reg, VReg);
Jakob Stoklund Olesendd9a2ec2011-03-30 02:52:39 +0000433 }
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000434 }
435}
436
Mark Laceyf367cd92013-08-14 23:50:09 +0000437// Keep track of new virtual registers created via
438// MachineRegisterInfo::createVirtualRegister.
439void
440LiveRangeEdit::MRI_NoteNewVirtualRegister(unsigned VReg)
441{
442 if (VRM)
443 VRM->grow();
444
Dylan McKayc328fe52016-10-11 01:04:36 +0000445 if (Parent && !Parent->isSpillable())
446 LIS.getInterval(VReg).markNotSpillable();
447
Mark Laceyf367cd92013-08-14 23:50:09 +0000448 NewRegs.push_back(VReg);
449}
450
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000451void
452LiveRangeEdit::calculateRegClassAndHint(MachineFunction &MF,
453 const MachineLoopInfo &Loops,
454 const MachineBlockFrequencyInfo &MBFI) {
Robert Lougher11a44b72015-08-10 11:59:44 +0000455 VirtRegAuxInfo VRAI(MF, LIS, VRM, Loops, MBFI);
Mark Laceyf9ea8852013-08-14 23:50:04 +0000456 for (unsigned I = 0, Size = size(); I < Size; ++I) {
457 LiveInterval &LI = LIS.getInterval(get(I));
Eric Christopher349d5882015-01-27 01:15:16 +0000458 if (MRI.recomputeRegClass(LI.reg))
Craig Toppercf0444b2014-11-17 05:50:14 +0000459 DEBUG({
460 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
461 dbgs() << "Inflated " << PrintReg(LI.reg) << " to "
Craig Topperf98c6062014-11-17 05:58:26 +0000462 << TRI->getRegClassName(MRI.getRegClass(LI.reg)) << '\n';
Craig Toppercf0444b2014-11-17 05:50:14 +0000463 });
Arnaud A. de Grandmaisonea3ac162013-11-11 19:04:45 +0000464 VRAI.calculateSpillWeightAndHint(LI);
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000465 }
466}