blob: 34bac082bcd7d5ccc24914500aacb86f82b6f68c [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +00006//
7//===----------------------------------------------------------------------===//
8//
9// The LiveRangeEdit class represents changes done to a virtual register when it
10// is spilled or split.
11//===----------------------------------------------------------------------===//
12
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "llvm/CodeGen/LiveRangeEdit.h"
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +000014#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +000015#include "llvm/CodeGen/CalcSpillWeights.h"
Matthias Braunf8422972017-12-13 02:51:04 +000016#include "llvm/CodeGen/LiveIntervals.h"
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000017#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000018#include "llvm/CodeGen/TargetInstrInfo.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"
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000022
23using namespace llvm;
24
Chandler Carruth1b9dde02014-04-22 02:02:50 +000025#define DEBUG_TYPE "regalloc"
26
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +000027STATISTIC(NumDCEDeleted, "Number of instructions deleted by DCE");
28STATISTIC(NumDCEFoldedLoads, "Number of single use loads folded after DCE");
29STATISTIC(NumFracRanges, "Number of live ranges fractured by DCE");
30
David Blaikiea379b1812011-12-20 02:50:00 +000031void LiveRangeEdit::Delegate::anchor() { }
32
Matthias Braun63449f92018-01-10 21:41:02 +000033LiveInterval &LiveRangeEdit::createEmptyIntervalFrom(unsigned OldReg,
34 bool createSubRanges) {
Daniel Sanders0c476112019-08-15 19:22:08 +000035 Register VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
Matthias Braun63449f92018-01-10 21:41:02 +000036 if (VRM)
Pete Cooper4f0dbb22012-04-03 00:28:46 +000037 VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
Matthias Braun63449f92018-01-10 21:41:02 +000038
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();
Matthias Braun63449f92018-01-10 21:41:02 +000042 if (createSubRanges) {
43 // Create empty subranges if the OldReg's interval has them. Do not create
44 // the main range here---it will be constructed later after the subranges
45 // have been finalized.
46 LiveInterval &OldLI = LIS.getInterval(OldReg);
47 VNInfo::Allocator &Alloc = LIS.getVNInfoAllocator();
48 for (LiveInterval::SubRange &S : OldLI.subranges())
49 LI.createSubRange(Alloc, S.LaneMask);
50 }
Jakob Stoklund Olesen86308402011-03-17 20:37:07 +000051 return LI;
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +000052}
53
Mark Lacey9d8103d2013-08-14 23:50:16 +000054unsigned LiveRangeEdit::createFrom(unsigned OldReg) {
Daniel Sanders0c476112019-08-15 19:22:08 +000055 Register VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
Mark Lacey9d8103d2013-08-14 23:50:16 +000056 if (VRM) {
57 VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
58 }
Quentin Colombet5725f562017-02-02 20:44:36 +000059 // FIXME: Getting the interval here actually computes it.
60 // In theory, this may not be what we want, but in practice
61 // the createEmptyIntervalFrom API is used when this is not
62 // the case. Generally speaking we just want to annotate the
63 // LiveInterval when it gets created but we cannot do that at
64 // the moment.
65 if (Parent && !Parent->isSpillable())
66 LIS.getInterval(VReg).markNotSpillable();
Mark Lacey9d8103d2013-08-14 23:50:16 +000067 return VReg;
68}
69
Jakob Stoklund Olesen86e53ce2011-04-20 22:14:20 +000070bool LiveRangeEdit::checkRematerializable(VNInfo *VNI,
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +000071 const MachineInstr *DefMI,
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +000072 AliasAnalysis *aa) {
73 assert(DefMI && "Missing instruction");
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000074 ScannedRemattable = true;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +000075 if (!TII.isTriviallyReMaterializable(*DefMI, aa))
Jakob Stoklund Olesen86e53ce2011-04-20 22:14:20 +000076 return false;
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000077 Remattable.insert(VNI);
Jakob Stoklund Olesen86e53ce2011-04-20 22:14:20 +000078 return true;
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +000079}
80
Pete Cooper2bde2f42012-04-02 22:22:53 +000081void LiveRangeEdit::scanRemattable(AliasAnalysis *aa) {
Matthias Braun96761952014-12-10 23:07:54 +000082 for (VNInfo *VNI : getParent().valnos) {
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000083 if (VNI->isUnused())
84 continue;
Wei Mi9a16d652016-04-13 03:08:27 +000085 unsigned Original = VRM->getOriginal(getReg());
86 LiveInterval &OrigLI = LIS.getInterval(Original);
87 VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000088 if (!OrigVNI)
89 continue;
Wei Mi9a16d652016-04-13 03:08:27 +000090 MachineInstr *DefMI = LIS.getInstructionFromIndex(OrigVNI->def);
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000091 if (!DefMI)
92 continue;
Wei Mi9a16d652016-04-13 03:08:27 +000093 checkRematerializable(OrigVNI, DefMI, aa);
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000094 }
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000095 ScannedRemattable = true;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +000096}
97
Pete Cooper2bde2f42012-04-02 22:22:53 +000098bool LiveRangeEdit::anyRematerializable(AliasAnalysis *aa) {
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +000099 if (!ScannedRemattable)
Pete Cooper2bde2f42012-04-02 22:22:53 +0000100 scanRemattable(aa);
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000101 return !Remattable.empty();
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000102}
103
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000104/// allUsesAvailableAt - Return true if all registers used by OrigMI at
105/// OrigIdx are also available with the same value at UseIdx.
106bool LiveRangeEdit::allUsesAvailableAt(const MachineInstr *OrigMI,
107 SlotIndex OrigIdx,
Jakub Staszak26ac8a72013-03-18 23:40:46 +0000108 SlotIndex UseIdx) const {
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000109 OrigIdx = OrigIdx.getRegSlot(true);
110 UseIdx = UseIdx.getRegSlot(true);
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000111 for (unsigned i = 0, e = OrigMI->getNumOperands(); i != e; ++i) {
112 const MachineOperand &MO = OrigMI->getOperand(i);
Jakob Stoklund Olesen78095782012-06-22 17:31:01 +0000113 if (!MO.isReg() || !MO.getReg() || !MO.readsReg())
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000114 continue;
Jakob Stoklund Olesen78095782012-06-22 17:31:01 +0000115
116 // We can't remat physreg uses, unless it is a constant.
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000117 if (Register::isPhysicalRegister(MO.getReg())) {
Matthias Braunde8c1b32016-10-28 18:05:09 +0000118 if (MRI.isConstantPhysReg(MO.getReg()))
Jakob Stoklund Olesen78095782012-06-22 17:31:01 +0000119 continue;
120 return false;
121 }
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000122
Pete Cooper2bde2f42012-04-02 22:22:53 +0000123 LiveInterval &li = LIS.getInterval(MO.getReg());
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000124 const VNInfo *OVNI = li.getVNInfoAt(OrigIdx);
125 if (!OVNI)
126 continue;
Jakob Stoklund Olesen4df59a92012-10-16 22:51:58 +0000127
128 // Don't allow rematerialization immediately after the original def.
129 // It would be incorrect if OrigMI redefines the register.
130 // See PR14098.
131 if (SlotIndex::isSameInstr(OrigIdx, UseIdx))
132 return false;
133
Jakob Stoklund Olesen72911e42010-10-14 23:49:52 +0000134 if (OVNI != li.getVNInfoAt(UseIdx))
135 return false;
136 }
137 return true;
138}
139
Wei Mi9a16d652016-04-13 03:08:27 +0000140bool LiveRangeEdit::canRematerializeAt(Remat &RM, VNInfo *OrigVNI,
141 SlotIndex UseIdx, bool cheapAsAMove) {
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000142 assert(ScannedRemattable && "Call anyRematerializable first");
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000143
144 // Use scanRemattable info.
Wei Mi9a16d652016-04-13 03:08:27 +0000145 if (!Remattable.count(OrigVNI))
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000146 return false;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000147
Jakob Stoklund Olesend8af5292011-03-29 03:12:02 +0000148 // No defining instruction provided.
149 SlotIndex DefIdx;
Wei Mi9a16d652016-04-13 03:08:27 +0000150 assert(RM.OrigMI && "No defining instruction for remattable value");
151 DefIdx = LIS.getInstructionIndex(*RM.OrigMI);
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000152
153 // If only cheap remats were requested, bail out early.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000154 if (cheapAsAMove && !TII.isAsCheapAsAMove(*RM.OrigMI))
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000155 return false;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000156
157 // Verify that all used registers are available with the same values.
Pete Cooper2bde2f42012-04-02 22:22:53 +0000158 if (!allUsesAvailableAt(RM.OrigMI, DefIdx, UseIdx))
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000159 return false;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000160
Jakob Stoklund Olesende5c4dc2010-11-10 01:05:12 +0000161 return true;
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000162}
163
164SlotIndex LiveRangeEdit::rematerializeAt(MachineBasicBlock &MBB,
165 MachineBasicBlock::iterator MI,
166 unsigned DestReg,
167 const Remat &RM,
Jakob Stoklund Olesen7d406792011-05-02 05:29:58 +0000168 const TargetRegisterInfo &tri,
169 bool Late) {
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000170 assert(RM.OrigMI && "Invalid remat");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000171 TII.reMaterialize(MBB, MI, DestReg, 0, *RM.OrigMI, tri);
Wei Mi9a16d652016-04-13 03:08:27 +0000172 // DestReg of the cloned instruction cannot be Dead. Set isDead of DestReg
173 // to false anyway in case the isDead flag of RM.OrigMI's dest register
174 // is true.
175 (*--MI).getOperand(0).setIsDead(false);
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000176 Rematted.insert(RM.ParentVNI);
Wei Mi9a16d652016-04-13 03:08:27 +0000177 return LIS.getSlotIndexes()->insertMachineInstrInMaps(*MI, Late).getRegSlot();
Jakob Stoklund Olesen2edaa2f2010-10-20 22:00:51 +0000178}
179
Pete Cooper2bde2f42012-04-02 22:22:53 +0000180void LiveRangeEdit::eraseVirtReg(unsigned Reg) {
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000181 if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg))
Jakob Stoklund Olesen43a87502011-03-13 01:23:11 +0000182 LIS.removeInterval(Reg);
183}
184
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000185bool LiveRangeEdit::foldAsLoad(LiveInterval *LI,
Pete Cooper2bde2f42012-04-02 22:22:53 +0000186 SmallVectorImpl<MachineInstr*> &Dead) {
Craig Topperc0196b12014-04-14 00:51:57 +0000187 MachineInstr *DefMI = nullptr, *UseMI = nullptr;
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000188
189 // Check that there is a single def and a single use.
Owen Andersonb36376e2014-03-17 19:36:09 +0000190 for (MachineOperand &MO : MRI.reg_nodbg_operands(LI->reg)) {
191 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000192 if (MO.isDef()) {
193 if (DefMI && DefMI != MI)
194 return false;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000195 if (!MI->canFoldAsLoad())
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000196 return false;
197 DefMI = MI;
198 } else if (!MO.isUndef()) {
199 if (UseMI && UseMI != MI)
200 return false;
201 // FIXME: Targets don't know how to fold subreg uses.
202 if (MO.getSubReg())
203 return false;
204 UseMI = MI;
205 }
206 }
207 if (!DefMI || !UseMI)
208 return false;
209
Jakob Stoklund Olesene2cfd0d2012-07-20 21:29:31 +0000210 // Since we're moving the DefMI load, make sure we're not extending any live
211 // ranges.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000212 if (!allUsesAvailableAt(DefMI, LIS.getInstructionIndex(*DefMI),
213 LIS.getInstructionIndex(*UseMI)))
Jakob Stoklund Olesene2cfd0d2012-07-20 21:29:31 +0000214 return false;
215
216 // We also need to make sure it is safe to move the load.
217 // Assume there are stores between DefMI and UseMI.
218 bool SawStore = true;
Matthias Braun07066cc2015-05-19 21:22:20 +0000219 if (!DefMI->isSafeToMove(nullptr, SawStore))
Jakob Stoklund Olesene2cfd0d2012-07-20 21:29:31 +0000220 return false;
221
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000222 LLVM_DEBUG(dbgs() << "Try to fold single def: " << *DefMI
223 << " into single use: " << *UseMI);
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000224
225 SmallVector<unsigned, 8> Ops;
226 if (UseMI->readsWritesVirtualRegister(LI->reg, &Ops).second)
227 return false;
228
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000229 MachineInstr *FoldMI = TII.foldMemoryOperand(*UseMI, Ops, *DefMI, &LIS);
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000230 if (!FoldMI)
231 return false;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000232 LLVM_DEBUG(dbgs() << " folded: " << *FoldMI);
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000233 LIS.ReplaceMachineInstrInMaps(*UseMI, *FoldMI);
Djordje Todorovic71d38692019-06-27 13:10:29 +0000234 if (UseMI->isCall())
Nikola Prica98603a82019-10-08 15:43:12 +0000235 UseMI->getMF()->moveCallSiteInfo(UseMI, FoldMI);
Djordje Todorovic7a9ca672019-06-27 14:31:52 +0000236 UseMI->eraseFromParent();
Craig Topperc0196b12014-04-14 00:51:57 +0000237 DefMI->addRegisterDead(LI->reg, nullptr);
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000238 Dead.push_back(DefMI);
Jakob Stoklund Olesenc5a8c082011-05-05 17:22:53 +0000239 ++NumDCEFoldedLoads;
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000240 return true;
241}
242
Matthias Braunc1e029e2015-06-01 21:26:26 +0000243bool LiveRangeEdit::useIsKill(const LiveInterval &LI,
244 const MachineOperand &MO) const {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000245 const MachineInstr &MI = *MO.getParent();
Matthias Braunc1e029e2015-06-01 21:26:26 +0000246 SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
247 if (LI.Query(Idx).isKill())
248 return true;
249 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
250 unsigned SubReg = MO.getSubReg();
Matthias Braune6a24852015-09-25 21:51:14 +0000251 LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubReg);
Matthias Braunc1e029e2015-06-01 21:26:26 +0000252 for (const LiveInterval::SubRange &S : LI.subranges()) {
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000253 if ((S.LaneMask & LaneMask).any() && S.Query(Idx).isKill())
Matthias Braunc1e029e2015-06-01 21:26:26 +0000254 return true;
255 }
256 return false;
257}
258
Andrew Trick530fc1f2013-06-21 18:33:17 +0000259/// Find all live intervals that need to shrink, then remove the instruction.
Wei Mic0223702016-07-08 21:08:09 +0000260void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink,
261 AliasAnalysis *AA) {
Andrew Trick530fc1f2013-06-21 18:33:17 +0000262 assert(MI->allDefsAreDead() && "Def isn't really dead");
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000263 SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
Andrew Trick530fc1f2013-06-21 18:33:17 +0000264
Andrew Trickcbd73052013-06-22 00:33:48 +0000265 // Never delete a bundled instruction.
266 if (MI->isBundled()) {
267 return;
268 }
Andrew Trick530fc1f2013-06-21 18:33:17 +0000269 // Never delete inline asm.
270 if (MI->isInlineAsm()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000271 LLVM_DEBUG(dbgs() << "Won't delete: " << Idx << '\t' << *MI);
Andrew Trick530fc1f2013-06-21 18:33:17 +0000272 return;
273 }
274
275 // Use the same criteria as DeadMachineInstructionElim.
276 bool SawStore = false;
Matthias Braun07066cc2015-05-19 21:22:20 +0000277 if (!MI->isSafeToMove(nullptr, SawStore)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000278 LLVM_DEBUG(dbgs() << "Can't delete: " << Idx << '\t' << *MI);
Andrew Trick530fc1f2013-06-21 18:33:17 +0000279 return;
280 }
281
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000282 LLVM_DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI);
Andrew Trick530fc1f2013-06-21 18:33:17 +0000283
284 // Collect virtual registers to be erased after MI is gone.
285 SmallVector<unsigned, 8> RegsToErase;
286 bool ReadsPhysRegs = false;
Wei Mi9a16d652016-04-13 03:08:27 +0000287 bool isOrigDef = false;
288 unsigned Dest;
Geoff Berry66d1f0f2016-12-15 19:55:19 +0000289 // Only optimize rematerialize case when the instruction has one def, since
290 // otherwise we could leave some dead defs in the code. This case is
291 // extremely rare.
292 if (VRM && MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
293 MI->getDesc().getNumDefs() == 1) {
Wei Mi9a16d652016-04-13 03:08:27 +0000294 Dest = MI->getOperand(0).getReg();
295 unsigned Original = VRM->getOriginal(Dest);
296 LiveInterval &OrigLI = LIS.getInterval(Original);
297 VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
Quentin Colombetd3079092016-06-09 21:34:31 +0000298 // The original live-range may have been shrunk to
299 // an empty live-range. It happens when it is dead, but
300 // we still keep it around to be able to rematerialize
301 // other values that depend on it.
302 if (OrigVNI)
303 isOrigDef = SlotIndex::isSameInstr(OrigVNI->def, Idx);
Wei Mi9a16d652016-04-13 03:08:27 +0000304 }
Andrew Trick530fc1f2013-06-21 18:33:17 +0000305
306 // Check for live intervals that may shrink
307 for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
308 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
309 if (!MOI->isReg())
310 continue;
Daniel Sanders0c476112019-08-15 19:22:08 +0000311 Register Reg = MOI->getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000312 if (!Register::isVirtualRegister(Reg)) {
Andrew Trick530fc1f2013-06-21 18:33:17 +0000313 // Check if MI reads any unreserved physregs.
314 if (Reg && MOI->readsReg() && !MRI.isReserved(Reg))
315 ReadsPhysRegs = true;
Matthias Brauncfb8ad22015-01-21 18:50:21 +0000316 else if (MOI->isDef())
317 LIS.removePhysRegDefAt(Reg, Idx);
Andrew Trick530fc1f2013-06-21 18:33:17 +0000318 continue;
319 }
320 LiveInterval &LI = LIS.getInterval(Reg);
321
322 // Shrink read registers, unless it is likely to be expensive and
323 // unlikely to change anything. We typically don't want to shrink the
324 // PIC base register that has lots of uses everywhere.
325 // Always shrink COPY uses that probably come from live range splitting.
Matthias Braunc1e029e2015-06-01 21:26:26 +0000326 if ((MI->readsVirtualRegister(Reg) && (MI->isCopy() || MOI->isDef())) ||
327 (MOI->readsReg() && (MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, *MOI))))
Andrew Trick530fc1f2013-06-21 18:33:17 +0000328 ToShrink.insert(&LI);
329
330 // Remove defined value.
331 if (MOI->isDef()) {
Matthias Braun311730a2015-01-21 19:02:30 +0000332 if (TheDelegate && LI.getVNInfoAt(Idx) != nullptr)
333 TheDelegate->LRE_WillShrinkVirtReg(LI.reg);
334 LIS.removeVRegDefAt(LI, Idx);
335 if (LI.empty())
336 RegsToErase.push_back(Reg);
Andrew Trick530fc1f2013-06-21 18:33:17 +0000337 }
338 }
339
340 // Currently, we don't support DCE of physreg live ranges. If MI reads
341 // any unreserved physregs, don't erase the instruction, but turn it into
342 // a KILL instead. This way, the physreg live ranges don't end up
343 // dangling.
344 // FIXME: It would be better to have something like shrinkToUses() for
345 // physregs. That could potentially enable more DCE and it would free up
346 // the physreg. It would not happen often, though.
347 if (ReadsPhysRegs) {
348 MI->setDesc(TII.get(TargetOpcode::KILL));
349 // Remove all operands that aren't physregs.
350 for (unsigned i = MI->getNumOperands(); i; --i) {
351 const MachineOperand &MO = MI->getOperand(i-1);
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000352 if (MO.isReg() && Register::isPhysicalRegister(MO.getReg()))
Andrew Trick530fc1f2013-06-21 18:33:17 +0000353 continue;
354 MI->RemoveOperand(i-1);
355 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000356 LLVM_DEBUG(dbgs() << "Converted physregs to:\t" << *MI);
Andrew Trick530fc1f2013-06-21 18:33:17 +0000357 } else {
Wei Mic0223702016-07-08 21:08:09 +0000358 // If the dest of MI is an original reg and MI is reMaterializable,
359 // don't delete the inst. Replace the dest with a new reg, and keep
360 // the inst for remat of other siblings. The inst is saved in
361 // LiveRangeEdit::DeadRemats and will be deleted after all the
362 // allocations of the func are done.
363 if (isOrigDef && DeadRemats && TII.isTriviallyReMaterializable(*MI, AA)) {
Matthias Braun63449f92018-01-10 21:41:02 +0000364 LiveInterval &NewLI = createEmptyIntervalFrom(Dest, false);
Wei Mi9a16d652016-04-13 03:08:27 +0000365 VNInfo *VNI = NewLI.getNextValue(Idx, LIS.getVNInfoAllocator());
366 NewLI.addSegment(LiveInterval::Segment(Idx, Idx.getDeadSlot(), VNI));
367 pop_back();
Matthias Braunf23ccf02018-01-10 22:36:26 +0000368 DeadRemats->insert(MI);
Wei Mi9a16d652016-04-13 03:08:27 +0000369 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
370 MI->substituteRegister(Dest, NewLI.reg, 0, TRI);
371 MI->getOperand(0).setIsDead(true);
372 } else {
373 if (TheDelegate)
374 TheDelegate->LRE_WillEraseInstruction(MI);
375 LIS.RemoveMachineInstrFromMaps(*MI);
376 MI->eraseFromParent();
377 ++NumDCEDeleted;
378 }
Andrew Trick530fc1f2013-06-21 18:33:17 +0000379 }
380
381 // Erase any virtregs that are now empty and unused. There may be <undef>
382 // uses around. Keep the empty live range in that case.
383 for (unsigned i = 0, e = RegsToErase.size(); i != e; ++i) {
384 unsigned Reg = RegsToErase[i];
385 if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) {
386 ToShrink.remove(&LIS.getInterval(Reg));
387 eraseVirtReg(Reg);
388 }
389 }
390}
391
Wei Mi9a16d652016-04-13 03:08:27 +0000392void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr *> &Dead,
Wei Mic0223702016-07-08 21:08:09 +0000393 ArrayRef<unsigned> RegsBeingSpilled,
394 AliasAnalysis *AA) {
Andrew Trick530fc1f2013-06-21 18:33:17 +0000395 ToShrinkSet ToShrink;
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000396
397 for (;;) {
398 // Erase all dead defs.
Andrew Trick530fc1f2013-06-21 18:33:17 +0000399 while (!Dead.empty())
Wei Mic0223702016-07-08 21:08:09 +0000400 eliminateDeadDef(Dead.pop_back_val(), ToShrink, AA);
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000401
402 if (ToShrink.empty())
403 break;
404
405 // Shrink just one live interval. Then delete new dead defs.
Jakob Stoklund Olesene14b2b22011-03-16 22:56:16 +0000406 LiveInterval *LI = ToShrink.back();
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000407 ToShrink.pop_back();
Pete Cooper2bde2f42012-04-02 22:22:53 +0000408 if (foldAsLoad(LI, Dead))
Jakob Stoklund Olesen18fd84c2011-04-05 20:20:26 +0000409 continue;
Matthias Braund3dd1352015-09-22 03:44:41 +0000410 unsigned VReg = LI->reg;
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000411 if (TheDelegate)
Matthias Braund3dd1352015-09-22 03:44:41 +0000412 TheDelegate->LRE_WillShrinkVirtReg(VReg);
Jakob Stoklund Olesen86308402011-03-17 20:37:07 +0000413 if (!LIS.shrinkToUses(LI, &Dead))
414 continue;
Andrew Trick7df3f012013-06-21 18:33:14 +0000415
Pete Cooper76e4bc42011-12-12 22:16:27 +0000416 // Don't create new intervals for a register being spilled.
417 // The new intervals would have to be spilled anyway so its not worth it.
418 // Also they currently aren't spilled so creating them and not spilling
419 // them results in incorrect code.
420 bool BeingSpilled = false;
421 for (unsigned i = 0, e = RegsBeingSpilled.size(); i != e; ++i) {
Matthias Braund3dd1352015-09-22 03:44:41 +0000422 if (VReg == RegsBeingSpilled[i]) {
Pete Cooper76e4bc42011-12-12 22:16:27 +0000423 BeingSpilled = true;
424 break;
425 }
426 }
Andrew Trick7df3f012013-06-21 18:33:14 +0000427
Pete Cooper76e4bc42011-12-12 22:16:27 +0000428 if (BeingSpilled) continue;
Jakob Stoklund Olesen86308402011-03-17 20:37:07 +0000429
430 // LI may have been separated, create new intervals.
Jakob Stoklund Olesen4417c7b2013-08-14 17:28:52 +0000431 LI->RenumberValues();
Matthias Braund3dd1352015-09-22 03:44:41 +0000432 SmallVector<LiveInterval*, 8> SplitLIs;
433 LIS.splitSeparateComponents(*LI, SplitLIs);
434 if (!SplitLIs.empty())
435 ++NumFracRanges;
436
437 unsigned Original = VRM ? VRM->getOriginal(VReg) : 0;
438 for (const LiveInterval *SplitLI : SplitLIs) {
Jakob Stoklund Olesenbbad3bc2011-07-05 15:38:41 +0000439 // If LI is an original interval that hasn't been split yet, make the new
440 // intervals their own originals instead of referring to LI. The original
441 // interval must contain all the split products, and LI doesn't.
Matthias Braund3dd1352015-09-22 03:44:41 +0000442 if (Original != VReg && Original != 0)
443 VRM->setIsSplitFromReg(SplitLI->reg, Original);
Jakob Stoklund Olesen3834dae2012-05-18 22:10:15 +0000444 if (TheDelegate)
Matthias Braund3dd1352015-09-22 03:44:41 +0000445 TheDelegate->LRE_DidCloneVirtReg(SplitLI->reg, VReg);
Jakob Stoklund Olesendd9a2ec2011-03-30 02:52:39 +0000446 }
Jakob Stoklund Olesenea5ebfe2011-03-08 22:46:11 +0000447 }
448}
449
Mark Laceyf367cd92013-08-14 23:50:09 +0000450// Keep track of new virtual registers created via
451// MachineRegisterInfo::createVirtualRegister.
452void
453LiveRangeEdit::MRI_NoteNewVirtualRegister(unsigned VReg)
454{
455 if (VRM)
456 VRM->grow();
457
458 NewRegs.push_back(VReg);
459}
460
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000461void
462LiveRangeEdit::calculateRegClassAndHint(MachineFunction &MF,
463 const MachineLoopInfo &Loops,
464 const MachineBlockFrequencyInfo &MBFI) {
Robert Lougher11a44b72015-08-10 11:59:44 +0000465 VirtRegAuxInfo VRAI(MF, LIS, VRM, Loops, MBFI);
Mark Laceyf9ea8852013-08-14 23:50:04 +0000466 for (unsigned I = 0, Size = size(); I < Size; ++I) {
467 LiveInterval &LI = LIS.getInterval(get(I));
Eric Christopher349d5882015-01-27 01:15:16 +0000468 if (MRI.recomputeRegClass(LI.reg))
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000469 LLVM_DEBUG({
Craig Toppercf0444b2014-11-17 05:50:14 +0000470 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000471 dbgs() << "Inflated " << printReg(LI.reg) << " to "
Craig Topperf98c6062014-11-17 05:58:26 +0000472 << TRI->getRegClassName(MRI.getRegClass(LI.reg)) << '\n';
Craig Toppercf0444b2014-11-17 05:50:14 +0000473 });
Arnaud A. de Grandmaisonea3ac162013-11-11 19:04:45 +0000474 VRAI.calculateSpillWeightAndHint(LI);
Jakob Stoklund Olesene991f722011-03-29 21:20:19 +0000475 }
476}