xref: /minix3/external/bsd/llvm/dist/llvm/lib/CodeGen/LiveRangeEdit.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===-- LiveRangeEdit.cpp - Basic tools for editing a register live range -===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // The LiveRangeEdit class represents changes done to a virtual register when it
11f4a2713aSLionel Sambuc // is spilled or split.
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "llvm/CodeGen/LiveRangeEdit.h"
15f4a2713aSLionel Sambuc #include "llvm/ADT/Statistic.h"
16f4a2713aSLionel Sambuc #include "llvm/CodeGen/CalcSpillWeights.h"
17f4a2713aSLionel Sambuc #include "llvm/CodeGen/LiveIntervalAnalysis.h"
18f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineRegisterInfo.h"
19f4a2713aSLionel Sambuc #include "llvm/CodeGen/VirtRegMap.h"
20f4a2713aSLionel Sambuc #include "llvm/Support/Debug.h"
21f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
22f4a2713aSLionel Sambuc #include "llvm/Target/TargetInstrInfo.h"
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc using namespace llvm;
25f4a2713aSLionel Sambuc 
26*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "regalloc"
27*0a6a1f1dSLionel Sambuc 
28f4a2713aSLionel Sambuc STATISTIC(NumDCEDeleted,     "Number of instructions deleted by DCE");
29f4a2713aSLionel Sambuc STATISTIC(NumDCEFoldedLoads, "Number of single use loads folded after DCE");
30f4a2713aSLionel Sambuc STATISTIC(NumFracRanges,     "Number of live ranges fractured by DCE");
31f4a2713aSLionel Sambuc 
anchor()32f4a2713aSLionel Sambuc void LiveRangeEdit::Delegate::anchor() { }
33f4a2713aSLionel Sambuc 
createEmptyIntervalFrom(unsigned OldReg)34f4a2713aSLionel Sambuc LiveInterval &LiveRangeEdit::createEmptyIntervalFrom(unsigned OldReg) {
35f4a2713aSLionel Sambuc   unsigned VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
36f4a2713aSLionel Sambuc   if (VRM) {
37f4a2713aSLionel Sambuc     VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
38f4a2713aSLionel Sambuc   }
39f4a2713aSLionel Sambuc   LiveInterval &LI = LIS.createEmptyInterval(VReg);
40f4a2713aSLionel Sambuc   return LI;
41f4a2713aSLionel Sambuc }
42f4a2713aSLionel Sambuc 
createFrom(unsigned OldReg)43f4a2713aSLionel Sambuc unsigned LiveRangeEdit::createFrom(unsigned OldReg) {
44f4a2713aSLionel Sambuc   unsigned VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
45f4a2713aSLionel Sambuc   if (VRM) {
46f4a2713aSLionel Sambuc     VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
47f4a2713aSLionel Sambuc   }
48f4a2713aSLionel Sambuc   return VReg;
49f4a2713aSLionel Sambuc }
50f4a2713aSLionel Sambuc 
checkRematerializable(VNInfo * VNI,const MachineInstr * DefMI,AliasAnalysis * aa)51f4a2713aSLionel Sambuc bool LiveRangeEdit::checkRematerializable(VNInfo *VNI,
52f4a2713aSLionel Sambuc                                           const MachineInstr *DefMI,
53f4a2713aSLionel Sambuc                                           AliasAnalysis *aa) {
54f4a2713aSLionel Sambuc   assert(DefMI && "Missing instruction");
55f4a2713aSLionel Sambuc   ScannedRemattable = true;
56f4a2713aSLionel Sambuc   if (!TII.isTriviallyReMaterializable(DefMI, aa))
57f4a2713aSLionel Sambuc     return false;
58f4a2713aSLionel Sambuc   Remattable.insert(VNI);
59f4a2713aSLionel Sambuc   return true;
60f4a2713aSLionel Sambuc }
61f4a2713aSLionel Sambuc 
scanRemattable(AliasAnalysis * aa)62f4a2713aSLionel Sambuc void LiveRangeEdit::scanRemattable(AliasAnalysis *aa) {
63*0a6a1f1dSLionel Sambuc   for (VNInfo *VNI : getParent().valnos) {
64f4a2713aSLionel Sambuc     if (VNI->isUnused())
65f4a2713aSLionel Sambuc       continue;
66f4a2713aSLionel Sambuc     MachineInstr *DefMI = LIS.getInstructionFromIndex(VNI->def);
67f4a2713aSLionel Sambuc     if (!DefMI)
68f4a2713aSLionel Sambuc       continue;
69f4a2713aSLionel Sambuc     checkRematerializable(VNI, DefMI, aa);
70f4a2713aSLionel Sambuc   }
71f4a2713aSLionel Sambuc   ScannedRemattable = true;
72f4a2713aSLionel Sambuc }
73f4a2713aSLionel Sambuc 
anyRematerializable(AliasAnalysis * aa)74f4a2713aSLionel Sambuc bool LiveRangeEdit::anyRematerializable(AliasAnalysis *aa) {
75f4a2713aSLionel Sambuc   if (!ScannedRemattable)
76f4a2713aSLionel Sambuc     scanRemattable(aa);
77f4a2713aSLionel Sambuc   return !Remattable.empty();
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc 
80f4a2713aSLionel Sambuc /// allUsesAvailableAt - Return true if all registers used by OrigMI at
81f4a2713aSLionel Sambuc /// OrigIdx are also available with the same value at UseIdx.
allUsesAvailableAt(const MachineInstr * OrigMI,SlotIndex OrigIdx,SlotIndex UseIdx) const82f4a2713aSLionel Sambuc bool LiveRangeEdit::allUsesAvailableAt(const MachineInstr *OrigMI,
83f4a2713aSLionel Sambuc                                        SlotIndex OrigIdx,
84f4a2713aSLionel Sambuc                                        SlotIndex UseIdx) const {
85f4a2713aSLionel Sambuc   OrigIdx = OrigIdx.getRegSlot(true);
86f4a2713aSLionel Sambuc   UseIdx = UseIdx.getRegSlot(true);
87f4a2713aSLionel Sambuc   for (unsigned i = 0, e = OrigMI->getNumOperands(); i != e; ++i) {
88f4a2713aSLionel Sambuc     const MachineOperand &MO = OrigMI->getOperand(i);
89f4a2713aSLionel Sambuc     if (!MO.isReg() || !MO.getReg() || !MO.readsReg())
90f4a2713aSLionel Sambuc       continue;
91f4a2713aSLionel Sambuc 
92f4a2713aSLionel Sambuc     // We can't remat physreg uses, unless it is a constant.
93f4a2713aSLionel Sambuc     if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
94f4a2713aSLionel Sambuc       if (MRI.isConstantPhysReg(MO.getReg(), *OrigMI->getParent()->getParent()))
95f4a2713aSLionel Sambuc         continue;
96f4a2713aSLionel Sambuc       return false;
97f4a2713aSLionel Sambuc     }
98f4a2713aSLionel Sambuc 
99f4a2713aSLionel Sambuc     LiveInterval &li = LIS.getInterval(MO.getReg());
100f4a2713aSLionel Sambuc     const VNInfo *OVNI = li.getVNInfoAt(OrigIdx);
101f4a2713aSLionel Sambuc     if (!OVNI)
102f4a2713aSLionel Sambuc       continue;
103f4a2713aSLionel Sambuc 
104f4a2713aSLionel Sambuc     // Don't allow rematerialization immediately after the original def.
105f4a2713aSLionel Sambuc     // It would be incorrect if OrigMI redefines the register.
106f4a2713aSLionel Sambuc     // See PR14098.
107f4a2713aSLionel Sambuc     if (SlotIndex::isSameInstr(OrigIdx, UseIdx))
108f4a2713aSLionel Sambuc       return false;
109f4a2713aSLionel Sambuc 
110f4a2713aSLionel Sambuc     if (OVNI != li.getVNInfoAt(UseIdx))
111f4a2713aSLionel Sambuc       return false;
112f4a2713aSLionel Sambuc   }
113f4a2713aSLionel Sambuc   return true;
114f4a2713aSLionel Sambuc }
115f4a2713aSLionel Sambuc 
canRematerializeAt(Remat & RM,SlotIndex UseIdx,bool cheapAsAMove)116f4a2713aSLionel Sambuc bool LiveRangeEdit::canRematerializeAt(Remat &RM,
117f4a2713aSLionel Sambuc                                        SlotIndex UseIdx,
118f4a2713aSLionel Sambuc                                        bool cheapAsAMove) {
119f4a2713aSLionel Sambuc   assert(ScannedRemattable && "Call anyRematerializable first");
120f4a2713aSLionel Sambuc 
121f4a2713aSLionel Sambuc   // Use scanRemattable info.
122f4a2713aSLionel Sambuc   if (!Remattable.count(RM.ParentVNI))
123f4a2713aSLionel Sambuc     return false;
124f4a2713aSLionel Sambuc 
125f4a2713aSLionel Sambuc   // No defining instruction provided.
126f4a2713aSLionel Sambuc   SlotIndex DefIdx;
127f4a2713aSLionel Sambuc   if (RM.OrigMI)
128f4a2713aSLionel Sambuc     DefIdx = LIS.getInstructionIndex(RM.OrigMI);
129f4a2713aSLionel Sambuc   else {
130f4a2713aSLionel Sambuc     DefIdx = RM.ParentVNI->def;
131f4a2713aSLionel Sambuc     RM.OrigMI = LIS.getInstructionFromIndex(DefIdx);
132f4a2713aSLionel Sambuc     assert(RM.OrigMI && "No defining instruction for remattable value");
133f4a2713aSLionel Sambuc   }
134f4a2713aSLionel Sambuc 
135f4a2713aSLionel Sambuc   // If only cheap remats were requested, bail out early.
136*0a6a1f1dSLionel Sambuc   if (cheapAsAMove && !TII.isAsCheapAsAMove(RM.OrigMI))
137f4a2713aSLionel Sambuc     return false;
138f4a2713aSLionel Sambuc 
139f4a2713aSLionel Sambuc   // Verify that all used registers are available with the same values.
140f4a2713aSLionel Sambuc   if (!allUsesAvailableAt(RM.OrigMI, DefIdx, UseIdx))
141f4a2713aSLionel Sambuc     return false;
142f4a2713aSLionel Sambuc 
143f4a2713aSLionel Sambuc   return true;
144f4a2713aSLionel Sambuc }
145f4a2713aSLionel Sambuc 
rematerializeAt(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,unsigned DestReg,const Remat & RM,const TargetRegisterInfo & tri,bool Late)146f4a2713aSLionel Sambuc SlotIndex LiveRangeEdit::rematerializeAt(MachineBasicBlock &MBB,
147f4a2713aSLionel Sambuc                                          MachineBasicBlock::iterator MI,
148f4a2713aSLionel Sambuc                                          unsigned DestReg,
149f4a2713aSLionel Sambuc                                          const Remat &RM,
150f4a2713aSLionel Sambuc                                          const TargetRegisterInfo &tri,
151f4a2713aSLionel Sambuc                                          bool Late) {
152f4a2713aSLionel Sambuc   assert(RM.OrigMI && "Invalid remat");
153f4a2713aSLionel Sambuc   TII.reMaterialize(MBB, MI, DestReg, 0, RM.OrigMI, tri);
154f4a2713aSLionel Sambuc   Rematted.insert(RM.ParentVNI);
155f4a2713aSLionel Sambuc   return LIS.getSlotIndexes()->insertMachineInstrInMaps(--MI, Late)
156f4a2713aSLionel Sambuc            .getRegSlot();
157f4a2713aSLionel Sambuc }
158f4a2713aSLionel Sambuc 
eraseVirtReg(unsigned Reg)159f4a2713aSLionel Sambuc void LiveRangeEdit::eraseVirtReg(unsigned Reg) {
160f4a2713aSLionel Sambuc   if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg))
161f4a2713aSLionel Sambuc     LIS.removeInterval(Reg);
162f4a2713aSLionel Sambuc }
163f4a2713aSLionel Sambuc 
foldAsLoad(LiveInterval * LI,SmallVectorImpl<MachineInstr * > & Dead)164f4a2713aSLionel Sambuc bool LiveRangeEdit::foldAsLoad(LiveInterval *LI,
165f4a2713aSLionel Sambuc                                SmallVectorImpl<MachineInstr*> &Dead) {
166*0a6a1f1dSLionel Sambuc   MachineInstr *DefMI = nullptr, *UseMI = nullptr;
167f4a2713aSLionel Sambuc 
168f4a2713aSLionel Sambuc   // Check that there is a single def and a single use.
169*0a6a1f1dSLionel Sambuc   for (MachineOperand &MO : MRI.reg_nodbg_operands(LI->reg)) {
170f4a2713aSLionel Sambuc     MachineInstr *MI = MO.getParent();
171f4a2713aSLionel Sambuc     if (MO.isDef()) {
172f4a2713aSLionel Sambuc       if (DefMI && DefMI != MI)
173f4a2713aSLionel Sambuc         return false;
174f4a2713aSLionel Sambuc       if (!MI->canFoldAsLoad())
175f4a2713aSLionel Sambuc         return false;
176f4a2713aSLionel Sambuc       DefMI = MI;
177f4a2713aSLionel Sambuc     } else if (!MO.isUndef()) {
178f4a2713aSLionel Sambuc       if (UseMI && UseMI != MI)
179f4a2713aSLionel Sambuc         return false;
180f4a2713aSLionel Sambuc       // FIXME: Targets don't know how to fold subreg uses.
181f4a2713aSLionel Sambuc       if (MO.getSubReg())
182f4a2713aSLionel Sambuc         return false;
183f4a2713aSLionel Sambuc       UseMI = MI;
184f4a2713aSLionel Sambuc     }
185f4a2713aSLionel Sambuc   }
186f4a2713aSLionel Sambuc   if (!DefMI || !UseMI)
187f4a2713aSLionel Sambuc     return false;
188f4a2713aSLionel Sambuc 
189f4a2713aSLionel Sambuc   // Since we're moving the DefMI load, make sure we're not extending any live
190f4a2713aSLionel Sambuc   // ranges.
191f4a2713aSLionel Sambuc   if (!allUsesAvailableAt(DefMI,
192f4a2713aSLionel Sambuc                           LIS.getInstructionIndex(DefMI),
193f4a2713aSLionel Sambuc                           LIS.getInstructionIndex(UseMI)))
194f4a2713aSLionel Sambuc     return false;
195f4a2713aSLionel Sambuc 
196f4a2713aSLionel Sambuc   // We also need to make sure it is safe to move the load.
197f4a2713aSLionel Sambuc   // Assume there are stores between DefMI and UseMI.
198f4a2713aSLionel Sambuc   bool SawStore = true;
199*0a6a1f1dSLionel Sambuc   if (!DefMI->isSafeToMove(&TII, nullptr, SawStore))
200f4a2713aSLionel Sambuc     return false;
201f4a2713aSLionel Sambuc 
202f4a2713aSLionel Sambuc   DEBUG(dbgs() << "Try to fold single def: " << *DefMI
203f4a2713aSLionel Sambuc                << "       into single use: " << *UseMI);
204f4a2713aSLionel Sambuc 
205f4a2713aSLionel Sambuc   SmallVector<unsigned, 8> Ops;
206f4a2713aSLionel Sambuc   if (UseMI->readsWritesVirtualRegister(LI->reg, &Ops).second)
207f4a2713aSLionel Sambuc     return false;
208f4a2713aSLionel Sambuc 
209f4a2713aSLionel Sambuc   MachineInstr *FoldMI = TII.foldMemoryOperand(UseMI, Ops, DefMI);
210f4a2713aSLionel Sambuc   if (!FoldMI)
211f4a2713aSLionel Sambuc     return false;
212f4a2713aSLionel Sambuc   DEBUG(dbgs() << "                folded: " << *FoldMI);
213f4a2713aSLionel Sambuc   LIS.ReplaceMachineInstrInMaps(UseMI, FoldMI);
214f4a2713aSLionel Sambuc   UseMI->eraseFromParent();
215*0a6a1f1dSLionel Sambuc   DefMI->addRegisterDead(LI->reg, nullptr);
216f4a2713aSLionel Sambuc   Dead.push_back(DefMI);
217f4a2713aSLionel Sambuc   ++NumDCEFoldedLoads;
218f4a2713aSLionel Sambuc   return true;
219f4a2713aSLionel Sambuc }
220f4a2713aSLionel Sambuc 
221f4a2713aSLionel Sambuc /// Find all live intervals that need to shrink, then remove the instruction.
eliminateDeadDef(MachineInstr * MI,ToShrinkSet & ToShrink)222f4a2713aSLionel Sambuc void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink) {
223f4a2713aSLionel Sambuc   assert(MI->allDefsAreDead() && "Def isn't really dead");
224f4a2713aSLionel Sambuc   SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
225f4a2713aSLionel Sambuc 
226f4a2713aSLionel Sambuc   // Never delete a bundled instruction.
227f4a2713aSLionel Sambuc   if (MI->isBundled()) {
228f4a2713aSLionel Sambuc     return;
229f4a2713aSLionel Sambuc   }
230f4a2713aSLionel Sambuc   // Never delete inline asm.
231f4a2713aSLionel Sambuc   if (MI->isInlineAsm()) {
232f4a2713aSLionel Sambuc     DEBUG(dbgs() << "Won't delete: " << Idx << '\t' << *MI);
233f4a2713aSLionel Sambuc     return;
234f4a2713aSLionel Sambuc   }
235f4a2713aSLionel Sambuc 
236f4a2713aSLionel Sambuc   // Use the same criteria as DeadMachineInstructionElim.
237f4a2713aSLionel Sambuc   bool SawStore = false;
238*0a6a1f1dSLionel Sambuc   if (!MI->isSafeToMove(&TII, nullptr, SawStore)) {
239f4a2713aSLionel Sambuc     DEBUG(dbgs() << "Can't delete: " << Idx << '\t' << *MI);
240f4a2713aSLionel Sambuc     return;
241f4a2713aSLionel Sambuc   }
242f4a2713aSLionel Sambuc 
243f4a2713aSLionel Sambuc   DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI);
244f4a2713aSLionel Sambuc 
245f4a2713aSLionel Sambuc   // Collect virtual registers to be erased after MI is gone.
246f4a2713aSLionel Sambuc   SmallVector<unsigned, 8> RegsToErase;
247f4a2713aSLionel Sambuc   bool ReadsPhysRegs = false;
248f4a2713aSLionel Sambuc 
249f4a2713aSLionel Sambuc   // Check for live intervals that may shrink
250f4a2713aSLionel Sambuc   for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
251f4a2713aSLionel Sambuc          MOE = MI->operands_end(); MOI != MOE; ++MOI) {
252f4a2713aSLionel Sambuc     if (!MOI->isReg())
253f4a2713aSLionel Sambuc       continue;
254f4a2713aSLionel Sambuc     unsigned Reg = MOI->getReg();
255f4a2713aSLionel Sambuc     if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
256f4a2713aSLionel Sambuc       // Check if MI reads any unreserved physregs.
257f4a2713aSLionel Sambuc       if (Reg && MOI->readsReg() && !MRI.isReserved(Reg))
258f4a2713aSLionel Sambuc         ReadsPhysRegs = true;
259f4a2713aSLionel Sambuc       else if (MOI->isDef()) {
260f4a2713aSLionel Sambuc         for (MCRegUnitIterator Units(Reg, MRI.getTargetRegisterInfo());
261f4a2713aSLionel Sambuc              Units.isValid(); ++Units) {
262f4a2713aSLionel Sambuc           if (LiveRange *LR = LIS.getCachedRegUnit(*Units)) {
263f4a2713aSLionel Sambuc             if (VNInfo *VNI = LR->getVNInfoAt(Idx))
264f4a2713aSLionel Sambuc               LR->removeValNo(VNI);
265f4a2713aSLionel Sambuc           }
266f4a2713aSLionel Sambuc         }
267f4a2713aSLionel Sambuc       }
268f4a2713aSLionel Sambuc       continue;
269f4a2713aSLionel Sambuc     }
270f4a2713aSLionel Sambuc     LiveInterval &LI = LIS.getInterval(Reg);
271f4a2713aSLionel Sambuc 
272f4a2713aSLionel Sambuc     // Shrink read registers, unless it is likely to be expensive and
273f4a2713aSLionel Sambuc     // unlikely to change anything. We typically don't want to shrink the
274f4a2713aSLionel Sambuc     // PIC base register that has lots of uses everywhere.
275f4a2713aSLionel Sambuc     // Always shrink COPY uses that probably come from live range splitting.
276f4a2713aSLionel Sambuc     if (MI->readsVirtualRegister(Reg) &&
277f4a2713aSLionel Sambuc         (MI->isCopy() || MOI->isDef() || MRI.hasOneNonDBGUse(Reg) ||
278f4a2713aSLionel Sambuc          LI.Query(Idx).isKill()))
279f4a2713aSLionel Sambuc       ToShrink.insert(&LI);
280f4a2713aSLionel Sambuc 
281f4a2713aSLionel Sambuc     // Remove defined value.
282f4a2713aSLionel Sambuc     if (MOI->isDef()) {
283f4a2713aSLionel Sambuc       if (VNInfo *VNI = LI.getVNInfoAt(Idx)) {
284f4a2713aSLionel Sambuc         if (TheDelegate)
285f4a2713aSLionel Sambuc           TheDelegate->LRE_WillShrinkVirtReg(LI.reg);
286f4a2713aSLionel Sambuc         LI.removeValNo(VNI);
287*0a6a1f1dSLionel Sambuc         if (LI.empty()) {
288f4a2713aSLionel Sambuc           RegsToErase.push_back(Reg);
289*0a6a1f1dSLionel Sambuc         } else {
290*0a6a1f1dSLionel Sambuc           // Also remove the value in subranges.
291*0a6a1f1dSLionel Sambuc           for (LiveInterval::SubRange &S : LI.subranges()) {
292*0a6a1f1dSLionel Sambuc             if (VNInfo *SVNI = S.getVNInfoAt(Idx))
293*0a6a1f1dSLionel Sambuc               S.removeValNo(SVNI);
294*0a6a1f1dSLionel Sambuc           }
295*0a6a1f1dSLionel Sambuc           LI.removeEmptySubRanges();
296*0a6a1f1dSLionel Sambuc         }
297f4a2713aSLionel Sambuc       }
298f4a2713aSLionel Sambuc     }
299f4a2713aSLionel Sambuc   }
300f4a2713aSLionel Sambuc 
301f4a2713aSLionel Sambuc   // Currently, we don't support DCE of physreg live ranges. If MI reads
302f4a2713aSLionel Sambuc   // any unreserved physregs, don't erase the instruction, but turn it into
303f4a2713aSLionel Sambuc   // a KILL instead. This way, the physreg live ranges don't end up
304f4a2713aSLionel Sambuc   // dangling.
305f4a2713aSLionel Sambuc   // FIXME: It would be better to have something like shrinkToUses() for
306f4a2713aSLionel Sambuc   // physregs. That could potentially enable more DCE and it would free up
307f4a2713aSLionel Sambuc   // the physreg. It would not happen often, though.
308f4a2713aSLionel Sambuc   if (ReadsPhysRegs) {
309f4a2713aSLionel Sambuc     MI->setDesc(TII.get(TargetOpcode::KILL));
310f4a2713aSLionel Sambuc     // Remove all operands that aren't physregs.
311f4a2713aSLionel Sambuc     for (unsigned i = MI->getNumOperands(); i; --i) {
312f4a2713aSLionel Sambuc       const MachineOperand &MO = MI->getOperand(i-1);
313f4a2713aSLionel Sambuc       if (MO.isReg() && TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
314f4a2713aSLionel Sambuc         continue;
315f4a2713aSLionel Sambuc       MI->RemoveOperand(i-1);
316f4a2713aSLionel Sambuc     }
317f4a2713aSLionel Sambuc     DEBUG(dbgs() << "Converted physregs to:\t" << *MI);
318f4a2713aSLionel Sambuc   } else {
319f4a2713aSLionel Sambuc     if (TheDelegate)
320f4a2713aSLionel Sambuc       TheDelegate->LRE_WillEraseInstruction(MI);
321f4a2713aSLionel Sambuc     LIS.RemoveMachineInstrFromMaps(MI);
322f4a2713aSLionel Sambuc     MI->eraseFromParent();
323f4a2713aSLionel Sambuc     ++NumDCEDeleted;
324f4a2713aSLionel Sambuc   }
325f4a2713aSLionel Sambuc 
326f4a2713aSLionel Sambuc   // Erase any virtregs that are now empty and unused. There may be <undef>
327f4a2713aSLionel Sambuc   // uses around. Keep the empty live range in that case.
328f4a2713aSLionel Sambuc   for (unsigned i = 0, e = RegsToErase.size(); i != e; ++i) {
329f4a2713aSLionel Sambuc     unsigned Reg = RegsToErase[i];
330f4a2713aSLionel Sambuc     if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) {
331f4a2713aSLionel Sambuc       ToShrink.remove(&LIS.getInterval(Reg));
332f4a2713aSLionel Sambuc       eraseVirtReg(Reg);
333f4a2713aSLionel Sambuc     }
334f4a2713aSLionel Sambuc   }
335f4a2713aSLionel Sambuc }
336f4a2713aSLionel Sambuc 
eliminateDeadDefs(SmallVectorImpl<MachineInstr * > & Dead,ArrayRef<unsigned> RegsBeingSpilled)337f4a2713aSLionel Sambuc void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr*> &Dead,
338f4a2713aSLionel Sambuc                                       ArrayRef<unsigned> RegsBeingSpilled) {
339f4a2713aSLionel Sambuc   ToShrinkSet ToShrink;
340f4a2713aSLionel Sambuc 
341f4a2713aSLionel Sambuc   for (;;) {
342f4a2713aSLionel Sambuc     // Erase all dead defs.
343f4a2713aSLionel Sambuc     while (!Dead.empty())
344f4a2713aSLionel Sambuc       eliminateDeadDef(Dead.pop_back_val(), ToShrink);
345f4a2713aSLionel Sambuc 
346f4a2713aSLionel Sambuc     if (ToShrink.empty())
347f4a2713aSLionel Sambuc       break;
348f4a2713aSLionel Sambuc 
349f4a2713aSLionel Sambuc     // Shrink just one live interval. Then delete new dead defs.
350f4a2713aSLionel Sambuc     LiveInterval *LI = ToShrink.back();
351f4a2713aSLionel Sambuc     ToShrink.pop_back();
352f4a2713aSLionel Sambuc     if (foldAsLoad(LI, Dead))
353f4a2713aSLionel Sambuc       continue;
354f4a2713aSLionel Sambuc     if (TheDelegate)
355f4a2713aSLionel Sambuc       TheDelegate->LRE_WillShrinkVirtReg(LI->reg);
356f4a2713aSLionel Sambuc     if (!LIS.shrinkToUses(LI, &Dead))
357f4a2713aSLionel Sambuc       continue;
358f4a2713aSLionel Sambuc 
359f4a2713aSLionel Sambuc     // Don't create new intervals for a register being spilled.
360f4a2713aSLionel Sambuc     // The new intervals would have to be spilled anyway so its not worth it.
361f4a2713aSLionel Sambuc     // Also they currently aren't spilled so creating them and not spilling
362f4a2713aSLionel Sambuc     // them results in incorrect code.
363f4a2713aSLionel Sambuc     bool BeingSpilled = false;
364f4a2713aSLionel Sambuc     for (unsigned i = 0, e = RegsBeingSpilled.size(); i != e; ++i) {
365f4a2713aSLionel Sambuc       if (LI->reg == RegsBeingSpilled[i]) {
366f4a2713aSLionel Sambuc         BeingSpilled = true;
367f4a2713aSLionel Sambuc         break;
368f4a2713aSLionel Sambuc       }
369f4a2713aSLionel Sambuc     }
370f4a2713aSLionel Sambuc 
371f4a2713aSLionel Sambuc     if (BeingSpilled) continue;
372f4a2713aSLionel Sambuc 
373f4a2713aSLionel Sambuc     // LI may have been separated, create new intervals.
374f4a2713aSLionel Sambuc     LI->RenumberValues();
375f4a2713aSLionel Sambuc     ConnectedVNInfoEqClasses ConEQ(LIS);
376f4a2713aSLionel Sambuc     unsigned NumComp = ConEQ.Classify(LI);
377f4a2713aSLionel Sambuc     if (NumComp <= 1)
378f4a2713aSLionel Sambuc       continue;
379f4a2713aSLionel Sambuc     ++NumFracRanges;
380f4a2713aSLionel Sambuc     bool IsOriginal = VRM && VRM->getOriginal(LI->reg) == LI->reg;
381f4a2713aSLionel Sambuc     DEBUG(dbgs() << NumComp << " components: " << *LI << '\n');
382f4a2713aSLionel Sambuc     SmallVector<LiveInterval*, 8> Dups(1, LI);
383f4a2713aSLionel Sambuc     for (unsigned i = 1; i != NumComp; ++i) {
384f4a2713aSLionel Sambuc       Dups.push_back(&createEmptyIntervalFrom(LI->reg));
385f4a2713aSLionel Sambuc       // If LI is an original interval that hasn't been split yet, make the new
386f4a2713aSLionel Sambuc       // intervals their own originals instead of referring to LI. The original
387f4a2713aSLionel Sambuc       // interval must contain all the split products, and LI doesn't.
388f4a2713aSLionel Sambuc       if (IsOriginal)
389f4a2713aSLionel Sambuc         VRM->setIsSplitFromReg(Dups.back()->reg, 0);
390f4a2713aSLionel Sambuc       if (TheDelegate)
391f4a2713aSLionel Sambuc         TheDelegate->LRE_DidCloneVirtReg(Dups.back()->reg, LI->reg);
392f4a2713aSLionel Sambuc     }
393f4a2713aSLionel Sambuc     ConEQ.Distribute(&Dups[0], MRI);
394f4a2713aSLionel Sambuc     DEBUG({
395f4a2713aSLionel Sambuc       for (unsigned i = 0; i != NumComp; ++i)
396f4a2713aSLionel Sambuc         dbgs() << '\t' << *Dups[i] << '\n';
397f4a2713aSLionel Sambuc     });
398f4a2713aSLionel Sambuc   }
399f4a2713aSLionel Sambuc }
400f4a2713aSLionel Sambuc 
401f4a2713aSLionel Sambuc // Keep track of new virtual registers created via
402f4a2713aSLionel Sambuc // MachineRegisterInfo::createVirtualRegister.
403f4a2713aSLionel Sambuc void
MRI_NoteNewVirtualRegister(unsigned VReg)404f4a2713aSLionel Sambuc LiveRangeEdit::MRI_NoteNewVirtualRegister(unsigned VReg)
405f4a2713aSLionel Sambuc {
406f4a2713aSLionel Sambuc   if (VRM)
407f4a2713aSLionel Sambuc     VRM->grow();
408f4a2713aSLionel Sambuc 
409f4a2713aSLionel Sambuc   NewRegs.push_back(VReg);
410f4a2713aSLionel Sambuc }
411f4a2713aSLionel Sambuc 
412f4a2713aSLionel Sambuc void
calculateRegClassAndHint(MachineFunction & MF,const MachineLoopInfo & Loops,const MachineBlockFrequencyInfo & MBFI)413f4a2713aSLionel Sambuc LiveRangeEdit::calculateRegClassAndHint(MachineFunction &MF,
414f4a2713aSLionel Sambuc                                         const MachineLoopInfo &Loops,
415f4a2713aSLionel Sambuc                                         const MachineBlockFrequencyInfo &MBFI) {
416f4a2713aSLionel Sambuc   VirtRegAuxInfo VRAI(MF, LIS, Loops, MBFI);
417f4a2713aSLionel Sambuc   for (unsigned I = 0, Size = size(); I < Size; ++I) {
418f4a2713aSLionel Sambuc     LiveInterval &LI = LIS.getInterval(get(I));
419f4a2713aSLionel Sambuc     if (MRI.recomputeRegClass(LI.reg, MF.getTarget()))
420*0a6a1f1dSLionel Sambuc       DEBUG({
421*0a6a1f1dSLionel Sambuc         const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
422*0a6a1f1dSLionel Sambuc         dbgs() << "Inflated " << PrintReg(LI.reg) << " to "
423*0a6a1f1dSLionel Sambuc                << TRI->getRegClassName(MRI.getRegClass(LI.reg)) << '\n';
424*0a6a1f1dSLionel Sambuc       });
425f4a2713aSLionel Sambuc     VRAI.calculateSpillWeightAndHint(LI);
426f4a2713aSLionel Sambuc   }
427f4a2713aSLionel Sambuc }
428