xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/LiveVariables.cpp (revision 5e801ac66d24704442eba426ed13c3effb8a34e7)
1 //===-- LiveVariables.cpp - Live Variable Analysis for Machine Code -------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the LiveVariable analysis pass.  For each machine
10 // instruction in the function, this pass calculates the set of registers that
11 // are immediately dead after the instruction (i.e., the instruction calculates
12 // the value, but it is never used) and the set of registers that are used by
13 // the instruction, but are never used after the instruction (i.e., they are
14 // killed).
15 //
16 // This class computes live variables using a sparse implementation based on
17 // the machine code SSA form.  This class computes live variable information for
18 // each virtual and _register allocatable_ physical register in a function.  It
19 // uses the dominance properties of SSA form to efficiently compute live
20 // variables for virtual registers, and assumes that physical registers are only
21 // live within a single basic block (allowing it to do a single local analysis
22 // to resolve physical register lifetimes in each basic block).  If a physical
23 // register is not register allocatable, it is not tracked.  This is useful for
24 // things like the stack pointer and condition codes.
25 //
26 //===----------------------------------------------------------------------===//
27 
28 #include "llvm/CodeGen/LiveVariables.h"
29 #include "llvm/ADT/DenseSet.h"
30 #include "llvm/ADT/DepthFirstIterator.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/ADT/SmallPtrSet.h"
33 #include "llvm/ADT/SmallSet.h"
34 #include "llvm/CodeGen/MachineInstr.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/CodeGen/Passes.h"
37 #include "llvm/Config/llvm-config.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include <algorithm>
42 using namespace llvm;
43 
44 char LiveVariables::ID = 0;
45 char &llvm::LiveVariablesID = LiveVariables::ID;
46 INITIALIZE_PASS_BEGIN(LiveVariables, "livevars",
47                 "Live Variable Analysis", false, false)
48 INITIALIZE_PASS_DEPENDENCY(UnreachableMachineBlockElim)
49 INITIALIZE_PASS_END(LiveVariables, "livevars",
50                 "Live Variable Analysis", false, false)
51 
52 
53 void LiveVariables::getAnalysisUsage(AnalysisUsage &AU) const {
54   AU.addRequiredID(UnreachableMachineBlockElimID);
55   AU.setPreservesAll();
56   MachineFunctionPass::getAnalysisUsage(AU);
57 }
58 
59 MachineInstr *
60 LiveVariables::VarInfo::findKill(const MachineBasicBlock *MBB) const {
61   for (unsigned i = 0, e = Kills.size(); i != e; ++i)
62     if (Kills[i]->getParent() == MBB)
63       return Kills[i];
64   return nullptr;
65 }
66 
67 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
68 LLVM_DUMP_METHOD void LiveVariables::VarInfo::dump() const {
69   dbgs() << "  Alive in blocks: ";
70   for (unsigned AB : AliveBlocks)
71     dbgs() << AB << ", ";
72   dbgs() << "\n  Killed by:";
73   if (Kills.empty())
74     dbgs() << " No instructions.\n";
75   else {
76     for (unsigned i = 0, e = Kills.size(); i != e; ++i)
77       dbgs() << "\n    #" << i << ": " << *Kills[i];
78     dbgs() << "\n";
79   }
80 }
81 #endif
82 
83 /// getVarInfo - Get (possibly creating) a VarInfo object for the given vreg.
84 LiveVariables::VarInfo &LiveVariables::getVarInfo(Register Reg) {
85   assert(Reg.isVirtual() && "getVarInfo: not a virtual register!");
86   VirtRegInfo.grow(Reg);
87   return VirtRegInfo[Reg];
88 }
89 
90 void LiveVariables::MarkVirtRegAliveInBlock(
91     VarInfo &VRInfo, MachineBasicBlock *DefBlock, MachineBasicBlock *MBB,
92     SmallVectorImpl<MachineBasicBlock *> &WorkList) {
93   unsigned BBNum = MBB->getNumber();
94 
95   // Check to see if this basic block is one of the killing blocks.  If so,
96   // remove it.
97   for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
98     if (VRInfo.Kills[i]->getParent() == MBB) {
99       VRInfo.Kills.erase(VRInfo.Kills.begin()+i);  // Erase entry
100       break;
101     }
102 
103   if (MBB == DefBlock) return;  // Terminate recursion
104 
105   if (VRInfo.AliveBlocks.test(BBNum))
106     return;  // We already know the block is live
107 
108   // Mark the variable known alive in this bb
109   VRInfo.AliveBlocks.set(BBNum);
110 
111   assert(MBB != &MF->front() && "Can't find reaching def for virtreg");
112   WorkList.insert(WorkList.end(), MBB->pred_rbegin(), MBB->pred_rend());
113 }
114 
115 void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
116                                             MachineBasicBlock *DefBlock,
117                                             MachineBasicBlock *MBB) {
118   SmallVector<MachineBasicBlock *, 16> WorkList;
119   MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList);
120 
121   while (!WorkList.empty()) {
122     MachineBasicBlock *Pred = WorkList.pop_back_val();
123     MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList);
124   }
125 }
126 
127 void LiveVariables::HandleVirtRegUse(Register Reg, MachineBasicBlock *MBB,
128                                      MachineInstr &MI) {
129   assert(MRI->getVRegDef(Reg) && "Register use before def!");
130 
131   unsigned BBNum = MBB->getNumber();
132 
133   VarInfo &VRInfo = getVarInfo(Reg);
134 
135   // Check to see if this basic block is already a kill block.
136   if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
137     // Yes, this register is killed in this basic block already. Increase the
138     // live range by updating the kill instruction.
139     VRInfo.Kills.back() = &MI;
140     return;
141   }
142 
143 #ifndef NDEBUG
144   for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
145     assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
146 #endif
147 
148   // This situation can occur:
149   //
150   //     ,------.
151   //     |      |
152   //     |      v
153   //     |   t2 = phi ... t1 ...
154   //     |      |
155   //     |      v
156   //     |   t1 = ...
157   //     |  ... = ... t1 ...
158   //     |      |
159   //     `------'
160   //
161   // where there is a use in a PHI node that's a predecessor to the defining
162   // block. We don't want to mark all predecessors as having the value "alive"
163   // in this case.
164   if (MBB == MRI->getVRegDef(Reg)->getParent())
165     return;
166 
167   // Add a new kill entry for this basic block. If this virtual register is
168   // already marked as alive in this basic block, that means it is alive in at
169   // least one of the successor blocks, it's not a kill.
170   if (!VRInfo.AliveBlocks.test(BBNum))
171     VRInfo.Kills.push_back(&MI);
172 
173   // Update all dominating blocks to mark them as "known live".
174   for (MachineBasicBlock *Pred : MBB->predecessors())
175     MarkVirtRegAliveInBlock(VRInfo, MRI->getVRegDef(Reg)->getParent(), Pred);
176 }
177 
178 void LiveVariables::HandleVirtRegDef(Register Reg, MachineInstr &MI) {
179   VarInfo &VRInfo = getVarInfo(Reg);
180 
181   if (VRInfo.AliveBlocks.empty())
182     // If vr is not alive in any block, then defaults to dead.
183     VRInfo.Kills.push_back(&MI);
184 }
185 
186 /// FindLastPartialDef - Return the last partial def of the specified register.
187 /// Also returns the sub-registers that're defined by the instruction.
188 MachineInstr *
189 LiveVariables::FindLastPartialDef(Register Reg,
190                                   SmallSet<unsigned, 4> &PartDefRegs) {
191   unsigned LastDefReg = 0;
192   unsigned LastDefDist = 0;
193   MachineInstr *LastDef = nullptr;
194   for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
195     unsigned SubReg = *SubRegs;
196     MachineInstr *Def = PhysRegDef[SubReg];
197     if (!Def)
198       continue;
199     unsigned Dist = DistanceMap[Def];
200     if (Dist > LastDefDist) {
201       LastDefReg  = SubReg;
202       LastDef     = Def;
203       LastDefDist = Dist;
204     }
205   }
206 
207   if (!LastDef)
208     return nullptr;
209 
210   PartDefRegs.insert(LastDefReg);
211   for (unsigned i = 0, e = LastDef->getNumOperands(); i != e; ++i) {
212     MachineOperand &MO = LastDef->getOperand(i);
213     if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
214       continue;
215     Register DefReg = MO.getReg();
216     if (TRI->isSubRegister(Reg, DefReg)) {
217       for (MCSubRegIterator SubRegs(DefReg, TRI, /*IncludeSelf=*/true);
218            SubRegs.isValid(); ++SubRegs)
219         PartDefRegs.insert(*SubRegs);
220     }
221   }
222   return LastDef;
223 }
224 
225 /// HandlePhysRegUse - Turn previous partial def's into read/mod/writes. Add
226 /// implicit defs to a machine instruction if there was an earlier def of its
227 /// super-register.
228 void LiveVariables::HandlePhysRegUse(Register Reg, MachineInstr &MI) {
229   MachineInstr *LastDef = PhysRegDef[Reg];
230   // If there was a previous use or a "full" def all is well.
231   if (!LastDef && !PhysRegUse[Reg]) {
232     // Otherwise, the last sub-register def implicitly defines this register.
233     // e.g.
234     // AH =
235     // AL = ... implicit-def EAX, implicit killed AH
236     //    = AH
237     // ...
238     //    = EAX
239     // All of the sub-registers must have been defined before the use of Reg!
240     SmallSet<unsigned, 4> PartDefRegs;
241     MachineInstr *LastPartialDef = FindLastPartialDef(Reg, PartDefRegs);
242     // If LastPartialDef is NULL, it must be using a livein register.
243     if (LastPartialDef) {
244       LastPartialDef->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/,
245                                                            true/*IsImp*/));
246       PhysRegDef[Reg] = LastPartialDef;
247       SmallSet<unsigned, 8> Processed;
248       for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
249         unsigned SubReg = *SubRegs;
250         if (Processed.count(SubReg))
251           continue;
252         if (PartDefRegs.count(SubReg))
253           continue;
254         // This part of Reg was defined before the last partial def. It's killed
255         // here.
256         LastPartialDef->addOperand(MachineOperand::CreateReg(SubReg,
257                                                              false/*IsDef*/,
258                                                              true/*IsImp*/));
259         PhysRegDef[SubReg] = LastPartialDef;
260         for (MCSubRegIterator SS(SubReg, TRI); SS.isValid(); ++SS)
261           Processed.insert(*SS);
262       }
263     }
264   } else if (LastDef && !PhysRegUse[Reg] &&
265              !LastDef->findRegisterDefOperand(Reg))
266     // Last def defines the super register, add an implicit def of reg.
267     LastDef->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/,
268                                                   true/*IsImp*/));
269 
270   // Remember this use.
271   for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
272        SubRegs.isValid(); ++SubRegs)
273     PhysRegUse[*SubRegs] = &MI;
274 }
275 
276 /// FindLastRefOrPartRef - Return the last reference or partial reference of
277 /// the specified register.
278 MachineInstr *LiveVariables::FindLastRefOrPartRef(Register Reg) {
279   MachineInstr *LastDef = PhysRegDef[Reg];
280   MachineInstr *LastUse = PhysRegUse[Reg];
281   if (!LastDef && !LastUse)
282     return nullptr;
283 
284   MachineInstr *LastRefOrPartRef = LastUse ? LastUse : LastDef;
285   unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef];
286   unsigned LastPartDefDist = 0;
287   for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
288     unsigned SubReg = *SubRegs;
289     MachineInstr *Def = PhysRegDef[SubReg];
290     if (Def && Def != LastDef) {
291       // There was a def of this sub-register in between. This is a partial
292       // def, keep track of the last one.
293       unsigned Dist = DistanceMap[Def];
294       if (Dist > LastPartDefDist)
295         LastPartDefDist = Dist;
296     } else if (MachineInstr *Use = PhysRegUse[SubReg]) {
297       unsigned Dist = DistanceMap[Use];
298       if (Dist > LastRefOrPartRefDist) {
299         LastRefOrPartRefDist = Dist;
300         LastRefOrPartRef = Use;
301       }
302     }
303   }
304 
305   return LastRefOrPartRef;
306 }
307 
308 bool LiveVariables::HandlePhysRegKill(Register Reg, MachineInstr *MI) {
309   MachineInstr *LastDef = PhysRegDef[Reg];
310   MachineInstr *LastUse = PhysRegUse[Reg];
311   if (!LastDef && !LastUse)
312     return false;
313 
314   MachineInstr *LastRefOrPartRef = LastUse ? LastUse : LastDef;
315   unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef];
316   // The whole register is used.
317   // AL =
318   // AH =
319   //
320   //    = AX
321   //    = AL, implicit killed AX
322   // AX =
323   //
324   // Or whole register is defined, but not used at all.
325   // dead AX =
326   // ...
327   // AX =
328   //
329   // Or whole register is defined, but only partly used.
330   // dead AX = implicit-def AL
331   //    = killed AL
332   // AX =
333   MachineInstr *LastPartDef = nullptr;
334   unsigned LastPartDefDist = 0;
335   SmallSet<unsigned, 8> PartUses;
336   for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
337     unsigned SubReg = *SubRegs;
338     MachineInstr *Def = PhysRegDef[SubReg];
339     if (Def && Def != LastDef) {
340       // There was a def of this sub-register in between. This is a partial
341       // def, keep track of the last one.
342       unsigned Dist = DistanceMap[Def];
343       if (Dist > LastPartDefDist) {
344         LastPartDefDist = Dist;
345         LastPartDef = Def;
346       }
347       continue;
348     }
349     if (MachineInstr *Use = PhysRegUse[SubReg]) {
350       for (MCSubRegIterator SS(SubReg, TRI, /*IncludeSelf=*/true); SS.isValid();
351            ++SS)
352         PartUses.insert(*SS);
353       unsigned Dist = DistanceMap[Use];
354       if (Dist > LastRefOrPartRefDist) {
355         LastRefOrPartRefDist = Dist;
356         LastRefOrPartRef = Use;
357       }
358     }
359   }
360 
361   if (!PhysRegUse[Reg]) {
362     // Partial uses. Mark register def dead and add implicit def of
363     // sub-registers which are used.
364     // dead EAX  = op  implicit-def AL
365     // That is, EAX def is dead but AL def extends pass it.
366     PhysRegDef[Reg]->addRegisterDead(Reg, TRI, true);
367     for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
368       unsigned SubReg = *SubRegs;
369       if (!PartUses.count(SubReg))
370         continue;
371       bool NeedDef = true;
372       if (PhysRegDef[Reg] == PhysRegDef[SubReg]) {
373         MachineOperand *MO = PhysRegDef[Reg]->findRegisterDefOperand(SubReg);
374         if (MO) {
375           NeedDef = false;
376           assert(!MO->isDead());
377         }
378       }
379       if (NeedDef)
380         PhysRegDef[Reg]->addOperand(MachineOperand::CreateReg(SubReg,
381                                                  true/*IsDef*/, true/*IsImp*/));
382       MachineInstr *LastSubRef = FindLastRefOrPartRef(SubReg);
383       if (LastSubRef)
384         LastSubRef->addRegisterKilled(SubReg, TRI, true);
385       else {
386         LastRefOrPartRef->addRegisterKilled(SubReg, TRI, true);
387         for (MCSubRegIterator SS(SubReg, TRI, /*IncludeSelf=*/true);
388              SS.isValid(); ++SS)
389           PhysRegUse[*SS] = LastRefOrPartRef;
390       }
391       for (MCSubRegIterator SS(SubReg, TRI); SS.isValid(); ++SS)
392         PartUses.erase(*SS);
393     }
394   } else if (LastRefOrPartRef == PhysRegDef[Reg] && LastRefOrPartRef != MI) {
395     if (LastPartDef)
396       // The last partial def kills the register.
397       LastPartDef->addOperand(MachineOperand::CreateReg(Reg, false/*IsDef*/,
398                                                 true/*IsImp*/, true/*IsKill*/));
399     else {
400       MachineOperand *MO =
401         LastRefOrPartRef->findRegisterDefOperand(Reg, false, false, TRI);
402       bool NeedEC = MO->isEarlyClobber() && MO->getReg() != Reg;
403       // If the last reference is the last def, then it's not used at all.
404       // That is, unless we are currently processing the last reference itself.
405       LastRefOrPartRef->addRegisterDead(Reg, TRI, true);
406       if (NeedEC) {
407         // If we are adding a subreg def and the superreg def is marked early
408         // clobber, add an early clobber marker to the subreg def.
409         MO = LastRefOrPartRef->findRegisterDefOperand(Reg);
410         if (MO)
411           MO->setIsEarlyClobber();
412       }
413     }
414   } else
415     LastRefOrPartRef->addRegisterKilled(Reg, TRI, true);
416   return true;
417 }
418 
419 void LiveVariables::HandleRegMask(const MachineOperand &MO) {
420   // Call HandlePhysRegKill() for all live registers clobbered by Mask.
421   // Clobbered registers are always dead, sp there is no need to use
422   // HandlePhysRegDef().
423   for (unsigned Reg = 1, NumRegs = TRI->getNumRegs(); Reg != NumRegs; ++Reg) {
424     // Skip dead regs.
425     if (!PhysRegDef[Reg] && !PhysRegUse[Reg])
426       continue;
427     // Skip mask-preserved regs.
428     if (!MO.clobbersPhysReg(Reg))
429       continue;
430     // Kill the largest clobbered super-register.
431     // This avoids needless implicit operands.
432     unsigned Super = Reg;
433     for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR)
434       if ((PhysRegDef[*SR] || PhysRegUse[*SR]) && MO.clobbersPhysReg(*SR))
435         Super = *SR;
436     HandlePhysRegKill(Super, nullptr);
437   }
438 }
439 
440 void LiveVariables::HandlePhysRegDef(Register Reg, MachineInstr *MI,
441                                      SmallVectorImpl<unsigned> &Defs) {
442   // What parts of the register are previously defined?
443   SmallSet<unsigned, 32> Live;
444   if (PhysRegDef[Reg] || PhysRegUse[Reg]) {
445     for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
446          SubRegs.isValid(); ++SubRegs)
447       Live.insert(*SubRegs);
448   } else {
449     for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
450       unsigned SubReg = *SubRegs;
451       // If a register isn't itself defined, but all parts that make up of it
452       // are defined, then consider it also defined.
453       // e.g.
454       // AL =
455       // AH =
456       //    = AX
457       if (Live.count(SubReg))
458         continue;
459       if (PhysRegDef[SubReg] || PhysRegUse[SubReg]) {
460         for (MCSubRegIterator SS(SubReg, TRI, /*IncludeSelf=*/true);
461              SS.isValid(); ++SS)
462           Live.insert(*SS);
463       }
464     }
465   }
466 
467   // Start from the largest piece, find the last time any part of the register
468   // is referenced.
469   HandlePhysRegKill(Reg, MI);
470   // Only some of the sub-registers are used.
471   for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
472     unsigned SubReg = *SubRegs;
473     if (!Live.count(SubReg))
474       // Skip if this sub-register isn't defined.
475       continue;
476     HandlePhysRegKill(SubReg, MI);
477   }
478 
479   if (MI)
480     Defs.push_back(Reg);  // Remember this def.
481 }
482 
483 void LiveVariables::UpdatePhysRegDefs(MachineInstr &MI,
484                                       SmallVectorImpl<unsigned> &Defs) {
485   while (!Defs.empty()) {
486     Register Reg = Defs.pop_back_val();
487     for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
488          SubRegs.isValid(); ++SubRegs) {
489       unsigned SubReg = *SubRegs;
490       PhysRegDef[SubReg] = &MI;
491       PhysRegUse[SubReg]  = nullptr;
492     }
493   }
494 }
495 
496 void LiveVariables::runOnInstr(MachineInstr &MI,
497                                SmallVectorImpl<unsigned> &Defs) {
498   assert(!MI.isDebugOrPseudoInstr());
499   // Process all of the operands of the instruction...
500   unsigned NumOperandsToProcess = MI.getNumOperands();
501 
502   // Unless it is a PHI node.  In this case, ONLY process the DEF, not any
503   // of the uses.  They will be handled in other basic blocks.
504   if (MI.isPHI())
505     NumOperandsToProcess = 1;
506 
507   // Clear kill and dead markers. LV will recompute them.
508   SmallVector<unsigned, 4> UseRegs;
509   SmallVector<unsigned, 4> DefRegs;
510   SmallVector<unsigned, 1> RegMasks;
511   for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
512     MachineOperand &MO = MI.getOperand(i);
513     if (MO.isRegMask()) {
514       RegMasks.push_back(i);
515       continue;
516     }
517     if (!MO.isReg() || MO.getReg() == 0)
518       continue;
519     Register MOReg = MO.getReg();
520     if (MO.isUse()) {
521       if (!(Register::isPhysicalRegister(MOReg) && MRI->isReserved(MOReg)))
522         MO.setIsKill(false);
523       if (MO.readsReg())
524         UseRegs.push_back(MOReg);
525     } else {
526       assert(MO.isDef());
527       // FIXME: We should not remove any dead flags. However the MIPS RDDSP
528       // instruction needs it at the moment: http://llvm.org/PR27116.
529       if (Register::isPhysicalRegister(MOReg) && !MRI->isReserved(MOReg))
530         MO.setIsDead(false);
531       DefRegs.push_back(MOReg);
532     }
533   }
534 
535   MachineBasicBlock *MBB = MI.getParent();
536   // Process all uses.
537   for (unsigned i = 0, e = UseRegs.size(); i != e; ++i) {
538     unsigned MOReg = UseRegs[i];
539     if (Register::isVirtualRegister(MOReg))
540       HandleVirtRegUse(MOReg, MBB, MI);
541     else if (!MRI->isReserved(MOReg))
542       HandlePhysRegUse(MOReg, MI);
543   }
544 
545   // Process all masked registers. (Call clobbers).
546   for (unsigned i = 0, e = RegMasks.size(); i != e; ++i)
547     HandleRegMask(MI.getOperand(RegMasks[i]));
548 
549   // Process all defs.
550   for (unsigned i = 0, e = DefRegs.size(); i != e; ++i) {
551     unsigned MOReg = DefRegs[i];
552     if (Register::isVirtualRegister(MOReg))
553       HandleVirtRegDef(MOReg, MI);
554     else if (!MRI->isReserved(MOReg))
555       HandlePhysRegDef(MOReg, &MI, Defs);
556   }
557   UpdatePhysRegDefs(MI, Defs);
558 }
559 
560 void LiveVariables::runOnBlock(MachineBasicBlock *MBB, const unsigned NumRegs) {
561   // Mark live-in registers as live-in.
562   SmallVector<unsigned, 4> Defs;
563   for (const auto &LI : MBB->liveins()) {
564     assert(Register::isPhysicalRegister(LI.PhysReg) &&
565            "Cannot have a live-in virtual register!");
566     HandlePhysRegDef(LI.PhysReg, nullptr, Defs);
567   }
568 
569   // Loop over all of the instructions, processing them.
570   DistanceMap.clear();
571   unsigned Dist = 0;
572   for (MachineInstr &MI : *MBB) {
573     if (MI.isDebugOrPseudoInstr())
574       continue;
575     DistanceMap.insert(std::make_pair(&MI, Dist++));
576 
577     runOnInstr(MI, Defs);
578   }
579 
580   // Handle any virtual assignments from PHI nodes which might be at the
581   // bottom of this basic block.  We check all of our successor blocks to see
582   // if they have PHI nodes, and if so, we simulate an assignment at the end
583   // of the current block.
584   if (!PHIVarInfo[MBB->getNumber()].empty()) {
585     SmallVectorImpl<unsigned> &VarInfoVec = PHIVarInfo[MBB->getNumber()];
586 
587     for (unsigned I : VarInfoVec)
588       // Mark it alive only in the block we are representing.
589       MarkVirtRegAliveInBlock(getVarInfo(I), MRI->getVRegDef(I)->getParent(),
590                               MBB);
591   }
592 
593   // MachineCSE may CSE instructions which write to non-allocatable physical
594   // registers across MBBs. Remember if any reserved register is liveout.
595   SmallSet<unsigned, 4> LiveOuts;
596   for (const MachineBasicBlock *SuccMBB : MBB->successors()) {
597     if (SuccMBB->isEHPad())
598       continue;
599     for (const auto &LI : SuccMBB->liveins()) {
600       if (!TRI->isInAllocatableClass(LI.PhysReg))
601         // Ignore other live-ins, e.g. those that are live into landing pads.
602         LiveOuts.insert(LI.PhysReg);
603     }
604   }
605 
606   // Loop over PhysRegDef / PhysRegUse, killing any registers that are
607   // available at the end of the basic block.
608   for (unsigned i = 0; i != NumRegs; ++i)
609     if ((PhysRegDef[i] || PhysRegUse[i]) && !LiveOuts.count(i))
610       HandlePhysRegDef(i, nullptr, Defs);
611 }
612 
613 bool LiveVariables::runOnMachineFunction(MachineFunction &mf) {
614   MF = &mf;
615   MRI = &mf.getRegInfo();
616   TRI = MF->getSubtarget().getRegisterInfo();
617 
618   const unsigned NumRegs = TRI->getNumRegs();
619   PhysRegDef.assign(NumRegs, nullptr);
620   PhysRegUse.assign(NumRegs, nullptr);
621   PHIVarInfo.resize(MF->getNumBlockIDs());
622   PHIJoins.clear();
623 
624   // FIXME: LiveIntervals will be updated to remove its dependence on
625   // LiveVariables to improve compilation time and eliminate bizarre pass
626   // dependencies. Until then, we can't change much in -O0.
627   if (!MRI->isSSA())
628     report_fatal_error("regalloc=... not currently supported with -O0");
629 
630   analyzePHINodes(mf);
631 
632   // Calculate live variable information in depth first order on the CFG of the
633   // function.  This guarantees that we will see the definition of a virtual
634   // register before its uses due to dominance properties of SSA (except for PHI
635   // nodes, which are treated as a special case).
636   MachineBasicBlock *Entry = &MF->front();
637   df_iterator_default_set<MachineBasicBlock*,16> Visited;
638 
639   for (MachineBasicBlock *MBB : depth_first_ext(Entry, Visited)) {
640     runOnBlock(MBB, NumRegs);
641 
642     PhysRegDef.assign(NumRegs, nullptr);
643     PhysRegUse.assign(NumRegs, nullptr);
644   }
645 
646   // Convert and transfer the dead / killed information we have gathered into
647   // VirtRegInfo onto MI's.
648   for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i) {
649     const Register Reg = Register::index2VirtReg(i);
650     for (unsigned j = 0, e2 = VirtRegInfo[Reg].Kills.size(); j != e2; ++j)
651       if (VirtRegInfo[Reg].Kills[j] == MRI->getVRegDef(Reg))
652         VirtRegInfo[Reg].Kills[j]->addRegisterDead(Reg, TRI);
653       else
654         VirtRegInfo[Reg].Kills[j]->addRegisterKilled(Reg, TRI);
655   }
656 
657   // Check to make sure there are no unreachable blocks in the MC CFG for the
658   // function.  If so, it is due to a bug in the instruction selector or some
659   // other part of the code generator if this happens.
660 #ifndef NDEBUG
661   for (const MachineBasicBlock &MBB : *MF)
662     assert(Visited.contains(&MBB) && "unreachable basic block found");
663 #endif
664 
665   PhysRegDef.clear();
666   PhysRegUse.clear();
667   PHIVarInfo.clear();
668 
669   return false;
670 }
671 
672 void LiveVariables::recomputeForSingleDefVirtReg(Register Reg) {
673   assert(Reg.isVirtual());
674 
675   VarInfo &VI = getVarInfo(Reg);
676   VI.AliveBlocks.clear();
677   VI.Kills.clear();
678 
679   MachineInstr &DefMI = *MRI->getUniqueVRegDef(Reg);
680   MachineBasicBlock &DefBB = *DefMI.getParent();
681 
682   // Handle the case where all uses have been removed.
683   if (MRI->use_nodbg_empty(Reg)) {
684     VI.Kills.push_back(&DefMI);
685     DefMI.addRegisterDead(Reg, nullptr);
686     return;
687   }
688   DefMI.clearRegisterDeads(Reg);
689 
690   // Initialize a worklist of BBs that Reg is live-to-end of. (Here
691   // "live-to-end" means Reg is live at the end of a block even if it is only
692   // live because of phi uses in a successor. This is different from isLiveOut()
693   // which does not consider phi uses.)
694   SmallVector<MachineBasicBlock *> LiveToEndBlocks;
695   SparseBitVector<> UseBlocks;
696   for (auto &UseMO : MRI->use_nodbg_operands(Reg)) {
697     UseMO.setIsKill(false);
698     MachineInstr &UseMI = *UseMO.getParent();
699     MachineBasicBlock &UseBB = *UseMI.getParent();
700     UseBlocks.set(UseBB.getNumber());
701     if (UseMI.isPHI()) {
702       // If Reg is used in a phi then it is live-to-end of the corresponding
703       // predecessor.
704       unsigned Idx = UseMI.getOperandNo(&UseMO);
705       LiveToEndBlocks.push_back(UseMI.getOperand(Idx + 1).getMBB());
706     } else if (&UseBB == &DefBB) {
707       // A non-phi use in the same BB as the single def must come after the def.
708     } else {
709       // Otherwise Reg must be live-to-end of all predecessors.
710       LiveToEndBlocks.append(UseBB.pred_begin(), UseBB.pred_end());
711     }
712   }
713 
714   // Iterate over the worklist adding blocks to AliveBlocks.
715   bool LiveToEndOfDefBB = false;
716   while (!LiveToEndBlocks.empty()) {
717     MachineBasicBlock &BB = *LiveToEndBlocks.pop_back_val();
718     if (&BB == &DefBB) {
719       LiveToEndOfDefBB = true;
720       continue;
721     }
722     if (VI.AliveBlocks.test(BB.getNumber()))
723       continue;
724     VI.AliveBlocks.set(BB.getNumber());
725     LiveToEndBlocks.append(BB.pred_begin(), BB.pred_end());
726   }
727 
728   // Recompute kill flags. For each block in which Reg is used but is not
729   // live-through, find the last instruction that uses Reg. Ignore phi nodes
730   // because they should not be included in Kills.
731   for (unsigned UseBBNum : UseBlocks) {
732     if (VI.AliveBlocks.test(UseBBNum))
733       continue;
734     MachineBasicBlock &UseBB = *MF->getBlockNumbered(UseBBNum);
735     if (&UseBB == &DefBB && LiveToEndOfDefBB)
736       continue;
737     for (auto &MI : reverse(UseBB)) {
738       if (MI.isDebugOrPseudoInstr())
739         continue;
740       if (MI.isPHI())
741         break;
742       if (MI.readsRegister(Reg)) {
743         assert(!MI.killsRegister(Reg));
744         MI.addRegisterKilled(Reg, nullptr);
745         VI.Kills.push_back(&MI);
746         break;
747       }
748     }
749   }
750 }
751 
752 /// replaceKillInstruction - Update register kill info by replacing a kill
753 /// instruction with a new one.
754 void LiveVariables::replaceKillInstruction(Register Reg, MachineInstr &OldMI,
755                                            MachineInstr &NewMI) {
756   VarInfo &VI = getVarInfo(Reg);
757   std::replace(VI.Kills.begin(), VI.Kills.end(), &OldMI, &NewMI);
758 }
759 
760 /// removeVirtualRegistersKilled - Remove all killed info for the specified
761 /// instruction.
762 void LiveVariables::removeVirtualRegistersKilled(MachineInstr &MI) {
763   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
764     MachineOperand &MO = MI.getOperand(i);
765     if (MO.isReg() && MO.isKill()) {
766       MO.setIsKill(false);
767       Register Reg = MO.getReg();
768       if (Register::isVirtualRegister(Reg)) {
769         bool removed = getVarInfo(Reg).removeKill(MI);
770         assert(removed && "kill not in register's VarInfo?");
771         (void)removed;
772       }
773     }
774   }
775 }
776 
777 /// analyzePHINodes - Gather information about the PHI nodes in here. In
778 /// particular, we want to map the variable information of a virtual register
779 /// which is used in a PHI node. We map that to the BB the vreg is coming from.
780 ///
781 void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
782   for (const auto &MBB : Fn)
783     for (const auto &BBI : MBB) {
784       if (!BBI.isPHI())
785         break;
786       for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2)
787         if (BBI.getOperand(i).readsReg())
788           PHIVarInfo[BBI.getOperand(i + 1).getMBB()->getNumber()]
789             .push_back(BBI.getOperand(i).getReg());
790     }
791 }
792 
793 bool LiveVariables::VarInfo::isLiveIn(const MachineBasicBlock &MBB,
794                                       Register Reg, MachineRegisterInfo &MRI) {
795   unsigned Num = MBB.getNumber();
796 
797   // Reg is live-through.
798   if (AliveBlocks.test(Num))
799     return true;
800 
801   // Registers defined in MBB cannot be live in.
802   const MachineInstr *Def = MRI.getVRegDef(Reg);
803   if (Def && Def->getParent() == &MBB)
804     return false;
805 
806  // Reg was not defined in MBB, was it killed here?
807   return findKill(&MBB);
808 }
809 
810 bool LiveVariables::isLiveOut(Register Reg, const MachineBasicBlock &MBB) {
811   LiveVariables::VarInfo &VI = getVarInfo(Reg);
812 
813   SmallPtrSet<const MachineBasicBlock *, 8> Kills;
814   for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
815     Kills.insert(VI.Kills[i]->getParent());
816 
817   // Loop over all of the successors of the basic block, checking to see if
818   // the value is either live in the block, or if it is killed in the block.
819   for (const MachineBasicBlock *SuccMBB : MBB.successors()) {
820     // Is it alive in this successor?
821     unsigned SuccIdx = SuccMBB->getNumber();
822     if (VI.AliveBlocks.test(SuccIdx))
823       return true;
824     // Or is it live because there is a use in a successor that kills it?
825     if (Kills.count(SuccMBB))
826       return true;
827   }
828 
829   return false;
830 }
831 
832 /// addNewBlock - Add a new basic block BB as an empty succcessor to DomBB. All
833 /// variables that are live out of DomBB will be marked as passing live through
834 /// BB.
835 void LiveVariables::addNewBlock(MachineBasicBlock *BB,
836                                 MachineBasicBlock *DomBB,
837                                 MachineBasicBlock *SuccBB) {
838   const unsigned NumNew = BB->getNumber();
839 
840   DenseSet<unsigned> Defs, Kills;
841 
842   MachineBasicBlock::iterator BBI = SuccBB->begin(), BBE = SuccBB->end();
843   for (; BBI != BBE && BBI->isPHI(); ++BBI) {
844     // Record the def of the PHI node.
845     Defs.insert(BBI->getOperand(0).getReg());
846 
847     // All registers used by PHI nodes in SuccBB must be live through BB.
848     for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
849       if (BBI->getOperand(i+1).getMBB() == BB)
850         getVarInfo(BBI->getOperand(i).getReg()).AliveBlocks.set(NumNew);
851   }
852 
853   // Record all vreg defs and kills of all instructions in SuccBB.
854   for (; BBI != BBE; ++BBI) {
855     for (const MachineOperand &Op : BBI->operands()) {
856       if (Op.isReg() && Register::isVirtualRegister(Op.getReg())) {
857         if (Op.isDef())
858           Defs.insert(Op.getReg());
859         else if (Op.isKill())
860           Kills.insert(Op.getReg());
861       }
862     }
863   }
864 
865   // Update info for all live variables
866   for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
867     Register Reg = Register::index2VirtReg(i);
868 
869     // If the Defs is defined in the successor it can't be live in BB.
870     if (Defs.count(Reg))
871       continue;
872 
873     // If the register is either killed in or live through SuccBB it's also live
874     // through BB.
875     VarInfo &VI = getVarInfo(Reg);
876     if (Kills.count(Reg) || VI.AliveBlocks.test(SuccBB->getNumber()))
877       VI.AliveBlocks.set(NumNew);
878   }
879 }
880 
881 /// addNewBlock - Add a new basic block BB as an empty succcessor to DomBB. All
882 /// variables that are live out of DomBB will be marked as passing live through
883 /// BB. LiveInSets[BB] is *not* updated (because it is not needed during
884 /// PHIElimination).
885 void LiveVariables::addNewBlock(MachineBasicBlock *BB,
886                                 MachineBasicBlock *DomBB,
887                                 MachineBasicBlock *SuccBB,
888                                 std::vector<SparseBitVector<>> &LiveInSets) {
889   const unsigned NumNew = BB->getNumber();
890 
891   SparseBitVector<> &BV = LiveInSets[SuccBB->getNumber()];
892   for (unsigned R : BV) {
893     Register VirtReg = Register::index2VirtReg(R);
894     LiveVariables::VarInfo &VI = getVarInfo(VirtReg);
895     VI.AliveBlocks.set(NumNew);
896   }
897   // All registers used by PHI nodes in SuccBB must be live through BB.
898   for (MachineBasicBlock::iterator BBI = SuccBB->begin(),
899          BBE = SuccBB->end();
900        BBI != BBE && BBI->isPHI(); ++BBI) {
901     for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
902       if (BBI->getOperand(i + 1).getMBB() == BB &&
903           BBI->getOperand(i).readsReg())
904         getVarInfo(BBI->getOperand(i).getReg())
905           .AliveBlocks.set(NumNew);
906   }
907 }
908