xref: /llvm-project/llvm/lib/CodeGen/ReachingDefAnalysis.cpp (revision 247a177cf78f0e9a51e351e630dd2a78c8f82859)
1 //===---- ReachingDefAnalysis.cpp - Reaching Def Analysis ---*- C++ -*-----===//
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 #include "llvm/ADT/SmallSet.h"
10 #include "llvm/CodeGen/LivePhysRegs.h"
11 #include "llvm/CodeGen/ReachingDefAnalysis.h"
12 #include "llvm/CodeGen/TargetRegisterInfo.h"
13 #include "llvm/CodeGen/TargetSubtargetInfo.h"
14 #include "llvm/Support/Debug.h"
15 
16 using namespace llvm;
17 
18 #define DEBUG_TYPE "reaching-deps-analysis"
19 
20 char ReachingDefAnalysis::ID = 0;
21 INITIALIZE_PASS(ReachingDefAnalysis, DEBUG_TYPE, "ReachingDefAnalysis", false,
22                 true)
23 
24 static bool isValidReg(const MachineOperand &MO) {
25   return MO.isReg() && MO.getReg();
26 }
27 
28 static bool isValidRegUse(const MachineOperand &MO) {
29   return isValidReg(MO) && MO.isUse();
30 }
31 
32 static bool isValidRegUseOf(const MachineOperand &MO, int PhysReg) {
33   return isValidRegUse(MO) && MO.getReg() == PhysReg;
34 }
35 
36 static bool isValidRegDef(const MachineOperand &MO) {
37   return isValidReg(MO) && MO.isDef();
38 }
39 
40 static bool isValidRegDefOf(const MachineOperand &MO, int PhysReg) {
41   return isValidRegDef(MO) && MO.getReg() == PhysReg;
42 }
43 
44 void ReachingDefAnalysis::enterBasicBlock(
45     const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
46 
47   MachineBasicBlock *MBB = TraversedMBB.MBB;
48   unsigned MBBNumber = MBB->getNumber();
49   assert(MBBNumber < MBBReachingDefs.size() &&
50          "Unexpected basic block number.");
51   MBBReachingDefs[MBBNumber].resize(NumRegUnits);
52 
53   // Reset instruction counter in each basic block.
54   CurInstr = 0;
55 
56   // Set up LiveRegs to represent registers entering MBB.
57   // Default values are 'nothing happened a long time ago'.
58   if (LiveRegs.empty())
59     LiveRegs.assign(NumRegUnits, ReachingDefDefaultVal);
60 
61   // This is the entry block.
62   if (MBB->pred_empty()) {
63     for (const auto &LI : MBB->liveins()) {
64       for (MCRegUnitIterator Unit(LI.PhysReg, TRI); Unit.isValid(); ++Unit) {
65         // Treat function live-ins as if they were defined just before the first
66         // instruction.  Usually, function arguments are set up immediately
67         // before the call.
68         LiveRegs[*Unit] = -1;
69         MBBReachingDefs[MBBNumber][*Unit].push_back(LiveRegs[*Unit]);
70       }
71     }
72     LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << ": entry\n");
73     return;
74   }
75 
76   // Try to coalesce live-out registers from predecessors.
77   for (MachineBasicBlock *pred : MBB->predecessors()) {
78     assert(unsigned(pred->getNumber()) < MBBOutRegsInfos.size() &&
79            "Should have pre-allocated MBBInfos for all MBBs");
80     const LiveRegsDefInfo &Incoming = MBBOutRegsInfos[pred->getNumber()];
81     // Incoming is null if this is a backedge from a BB
82     // we haven't processed yet
83     if (Incoming.empty())
84       continue;
85 
86     for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit) {
87       // Use the most recent predecessor def for each register.
88       LiveRegs[Unit] = std::max(LiveRegs[Unit], Incoming[Unit]);
89       if ((LiveRegs[Unit] != ReachingDefDefaultVal))
90         MBBReachingDefs[MBBNumber][Unit].push_back(LiveRegs[Unit]);
91     }
92   }
93 
94   LLVM_DEBUG(dbgs() << printMBBReference(*MBB)
95                     << (!TraversedMBB.IsDone ? ": incomplete\n"
96                                              : ": all preds known\n"));
97 }
98 
99 void ReachingDefAnalysis::leaveBasicBlock(
100     const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
101   assert(!LiveRegs.empty() && "Must enter basic block first.");
102   unsigned MBBNumber = TraversedMBB.MBB->getNumber();
103   assert(MBBNumber < MBBOutRegsInfos.size() &&
104          "Unexpected basic block number.");
105   // Save register clearances at end of MBB - used by enterBasicBlock().
106   MBBOutRegsInfos[MBBNumber] = LiveRegs;
107 
108   // While processing the basic block, we kept `Def` relative to the start
109   // of the basic block for convenience. However, future use of this information
110   // only cares about the clearance from the end of the block, so adjust
111   // everything to be relative to the end of the basic block.
112   for (int &OutLiveReg : MBBOutRegsInfos[MBBNumber])
113     OutLiveReg -= CurInstr;
114   LiveRegs.clear();
115 }
116 
117 void ReachingDefAnalysis::processDefs(MachineInstr *MI) {
118   assert(!MI->isDebugInstr() && "Won't process debug instructions");
119 
120   unsigned MBBNumber = MI->getParent()->getNumber();
121   assert(MBBNumber < MBBReachingDefs.size() &&
122          "Unexpected basic block number.");
123 
124   for (auto &MO : MI->operands()) {
125     if (!isValidRegDef(MO))
126       continue;
127     for (MCRegUnitIterator Unit(MO.getReg(), TRI); Unit.isValid(); ++Unit) {
128       // This instruction explicitly defines the current reg unit.
129       LLVM_DEBUG(dbgs() << printReg(MO.getReg(), TRI) << ":\t" << CurInstr
130                         << '\t' << *MI);
131 
132       // How many instructions since this reg unit was last written?
133       LiveRegs[*Unit] = CurInstr;
134       MBBReachingDefs[MBBNumber][*Unit].push_back(CurInstr);
135     }
136   }
137   InstIds[MI] = CurInstr;
138   ++CurInstr;
139 }
140 
141 void ReachingDefAnalysis::processBasicBlock(
142     const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
143   enterBasicBlock(TraversedMBB);
144   for (MachineInstr &MI : *TraversedMBB.MBB) {
145     if (!MI.isDebugInstr())
146       processDefs(&MI);
147   }
148   leaveBasicBlock(TraversedMBB);
149 }
150 
151 bool ReachingDefAnalysis::runOnMachineFunction(MachineFunction &mf) {
152   MF = &mf;
153   TRI = MF->getSubtarget().getRegisterInfo();
154   LLVM_DEBUG(dbgs() << "********** REACHING DEFINITION ANALYSIS **********\n");
155   init();
156   traverse();
157   return false;
158 }
159 
160 void ReachingDefAnalysis::releaseMemory() {
161   // Clear the internal vectors.
162   MBBOutRegsInfos.clear();
163   MBBReachingDefs.clear();
164   InstIds.clear();
165   LiveRegs.clear();
166 }
167 
168 void ReachingDefAnalysis::reset() {
169   releaseMemory();
170   init();
171   traverse();
172 }
173 
174 void ReachingDefAnalysis::init() {
175   NumRegUnits = TRI->getNumRegUnits();
176   MBBReachingDefs.resize(MF->getNumBlockIDs());
177   // Initialize the MBBOutRegsInfos
178   MBBOutRegsInfos.resize(MF->getNumBlockIDs());
179   LoopTraversal Traversal;
180   TraversedMBBOrder = Traversal.traverse(*MF);
181 }
182 
183 void ReachingDefAnalysis::traverse() {
184   // Traverse the basic blocks.
185   for (LoopTraversal::TraversedMBBInfo TraversedMBB : TraversedMBBOrder)
186     processBasicBlock(TraversedMBB);
187   // Sorting all reaching defs found for a ceartin reg unit in a given BB.
188   for (MBBDefsInfo &MBBDefs : MBBReachingDefs) {
189     for (MBBRegUnitDefs &RegUnitDefs : MBBDefs)
190       llvm::sort(RegUnitDefs);
191   }
192 }
193 
194 int ReachingDefAnalysis::getReachingDef(MachineInstr *MI, int PhysReg) const {
195   assert(InstIds.count(MI) && "Unexpected machine instuction.");
196   int InstId = InstIds.lookup(MI);
197   int DefRes = ReachingDefDefaultVal;
198   unsigned MBBNumber = MI->getParent()->getNumber();
199   assert(MBBNumber < MBBReachingDefs.size() &&
200          "Unexpected basic block number.");
201   int LatestDef = ReachingDefDefaultVal;
202   for (MCRegUnitIterator Unit(PhysReg, TRI); Unit.isValid(); ++Unit) {
203     for (int Def : MBBReachingDefs[MBBNumber][*Unit]) {
204       if (Def >= InstId)
205         break;
206       DefRes = Def;
207     }
208     LatestDef = std::max(LatestDef, DefRes);
209   }
210   return LatestDef;
211 }
212 
213 MachineInstr* ReachingDefAnalysis::getReachingLocalMIDef(MachineInstr *MI,
214                                                     int PhysReg) const {
215   return getInstFromId(MI->getParent(), getReachingDef(MI, PhysReg));
216 }
217 
218 bool ReachingDefAnalysis::hasSameReachingDef(MachineInstr *A, MachineInstr *B,
219                                              int PhysReg) const {
220   MachineBasicBlock *ParentA = A->getParent();
221   MachineBasicBlock *ParentB = B->getParent();
222   if (ParentA != ParentB)
223     return false;
224 
225   return getReachingDef(A, PhysReg) == getReachingDef(B, PhysReg);
226 }
227 
228 MachineInstr *ReachingDefAnalysis::getInstFromId(MachineBasicBlock *MBB,
229                                                  int InstId) const {
230   assert(static_cast<size_t>(MBB->getNumber()) < MBBReachingDefs.size() &&
231          "Unexpected basic block number.");
232   assert(InstId < static_cast<int>(MBB->size()) &&
233          "Unexpected instruction id.");
234 
235   if (InstId < 0)
236     return nullptr;
237 
238   for (auto &MI : *MBB) {
239     auto F = InstIds.find(&MI);
240     if (F != InstIds.end() && F->second == InstId)
241       return &MI;
242   }
243 
244   return nullptr;
245 }
246 
247 int
248 ReachingDefAnalysis::getClearance(MachineInstr *MI, MCPhysReg PhysReg) const {
249   assert(InstIds.count(MI) && "Unexpected machine instuction.");
250   return InstIds.lookup(MI) - getReachingDef(MI, PhysReg);
251 }
252 
253 bool
254 ReachingDefAnalysis::hasLocalDefBefore(MachineInstr *MI, int PhysReg) const {
255   return getReachingDef(MI, PhysReg) >= 0;
256 }
257 
258 void ReachingDefAnalysis::getReachingLocalUses(MachineInstr *Def, int PhysReg,
259                                                InstSet &Uses) const {
260   MachineBasicBlock *MBB = Def->getParent();
261   MachineBasicBlock::iterator MI = MachineBasicBlock::iterator(Def);
262   while (++MI != MBB->end()) {
263     if (MI->isDebugInstr())
264       continue;
265 
266     // If/when we find a new reaching def, we know that there's no more uses
267     // of 'Def'.
268     if (getReachingLocalMIDef(&*MI, PhysReg) != Def)
269       return;
270 
271     for (auto &MO : MI->operands()) {
272       if (!isValidRegUseOf(MO, PhysReg))
273         continue;
274 
275       Uses.insert(&*MI);
276       if (MO.isKill())
277         return;
278     }
279   }
280 }
281 
282 bool
283 ReachingDefAnalysis::getLiveInUses(MachineBasicBlock *MBB, int PhysReg,
284                                    InstSet &Uses) const {
285   for (auto &MI : *MBB) {
286     if (MI.isDebugInstr())
287       continue;
288     for (auto &MO : MI.operands()) {
289       if (!isValidRegUseOf(MO, PhysReg))
290         continue;
291       if (getReachingDef(&MI, PhysReg) >= 0)
292         return false;
293       Uses.insert(&MI);
294     }
295   }
296   return isReachingDefLiveOut(&MBB->back(), PhysReg);
297 }
298 
299 void
300 ReachingDefAnalysis::getGlobalUses(MachineInstr *MI, int PhysReg,
301                                    InstSet &Uses) const {
302   MachineBasicBlock *MBB = MI->getParent();
303 
304   // Collect the uses that each def touches within the block.
305   getReachingLocalUses(MI, PhysReg, Uses);
306 
307   // Handle live-out values.
308   if (auto *LiveOut = getLocalLiveOutMIDef(MI->getParent(), PhysReg)) {
309     if (LiveOut != MI)
310       return;
311 
312     SmallVector<MachineBasicBlock*, 4> ToVisit;
313     ToVisit.insert(ToVisit.begin(), MBB->successors().begin(),
314                    MBB->successors().end());
315     SmallPtrSet<MachineBasicBlock*, 4>Visited;
316     while (!ToVisit.empty()) {
317       MachineBasicBlock *MBB = ToVisit.back();
318       ToVisit.pop_back();
319       if (Visited.count(MBB) || !MBB->isLiveIn(PhysReg))
320         continue;
321       if (getLiveInUses(MBB, PhysReg, Uses))
322         ToVisit.insert(ToVisit.end(), MBB->successors().begin(),
323                        MBB->successors().end());
324       Visited.insert(MBB);
325     }
326   }
327 }
328 
329 void
330 ReachingDefAnalysis::getLiveOuts(MachineBasicBlock *MBB, int PhysReg,
331                                  InstSet &Defs, BlockSet &VisitedBBs) const {
332   if (VisitedBBs.count(MBB))
333     return;
334 
335   VisitedBBs.insert(MBB);
336   LivePhysRegs LiveRegs(*TRI);
337   LiveRegs.addLiveOuts(*MBB);
338   if (!LiveRegs.contains(PhysReg))
339     return;
340 
341   if (auto *Def = getLocalLiveOutMIDef(MBB, PhysReg))
342     Defs.insert(Def);
343   else
344     for (auto *Pred : MBB->predecessors())
345       getLiveOuts(Pred, PhysReg, Defs, VisitedBBs);
346 }
347 
348 MachineInstr *ReachingDefAnalysis::getUniqueReachingMIDef(MachineInstr *MI,
349                                                           int PhysReg) const {
350   // If there's a local def before MI, return it.
351   MachineInstr *LocalDef = getReachingLocalMIDef(MI, PhysReg);
352   if (LocalDef && InstIds.lookup(LocalDef) < InstIds.lookup(MI))
353     return LocalDef;
354 
355   SmallPtrSet<MachineBasicBlock*, 4> VisitedBBs;
356   SmallPtrSet<MachineInstr*, 2> Incoming;
357   for (auto *Pred : MI->getParent()->predecessors())
358     getLiveOuts(Pred, PhysReg, Incoming, VisitedBBs);
359 
360   // If we have a local def and an incoming instruction, then there's not a
361   // unique instruction def.
362   if (!Incoming.empty() && LocalDef)
363     return nullptr;
364   else if (Incoming.size() == 1)
365     return *Incoming.begin();
366   else
367     return LocalDef;
368 }
369 
370 MachineInstr *ReachingDefAnalysis::getMIOperand(MachineInstr *MI,
371                                                 unsigned Idx) const {
372   assert(MI->getOperand(Idx).isReg() && "Expected register operand");
373   return getUniqueReachingMIDef(MI, MI->getOperand(Idx).getReg());
374 }
375 
376 MachineInstr *ReachingDefAnalysis::getMIOperand(MachineInstr *MI,
377                                                 MachineOperand &MO) const {
378   assert(MO.isReg() && "Expected register operand");
379   return getUniqueReachingMIDef(MI, MO.getReg());
380 }
381 
382 bool ReachingDefAnalysis::isRegUsedAfter(MachineInstr *MI, int PhysReg) const {
383   MachineBasicBlock *MBB = MI->getParent();
384   LivePhysRegs LiveRegs(*TRI);
385   LiveRegs.addLiveOuts(*MBB);
386 
387   // Yes if the register is live out of the basic block.
388   if (LiveRegs.contains(PhysReg))
389     return true;
390 
391   // Walk backwards through the block to see if the register is live at some
392   // point.
393   for (auto Last = MBB->rbegin(), End = MBB->rend(); Last != End; ++Last) {
394     LiveRegs.stepBackward(*Last);
395     if (LiveRegs.contains(PhysReg))
396       return InstIds.lookup(&*Last) > InstIds.lookup(MI);
397   }
398   return false;
399 }
400 
401 bool ReachingDefAnalysis::isRegDefinedAfter(MachineInstr *MI,
402                                             int PhysReg) const {
403   MachineBasicBlock *MBB = MI->getParent();
404   if (getReachingDef(MI, PhysReg) != getReachingDef(&MBB->back(), PhysReg))
405     return true;
406 
407   if (auto *Def = getLocalLiveOutMIDef(MBB, PhysReg))
408     return Def == getReachingLocalMIDef(MI, PhysReg);
409 
410   return false;
411 }
412 
413 bool
414 ReachingDefAnalysis::isReachingDefLiveOut(MachineInstr *MI, int PhysReg) const {
415   MachineBasicBlock *MBB = MI->getParent();
416   LivePhysRegs LiveRegs(*TRI);
417   LiveRegs.addLiveOuts(*MBB);
418   if (!LiveRegs.contains(PhysReg))
419     return false;
420 
421   MachineInstr *Last = &MBB->back();
422   int Def = getReachingDef(MI, PhysReg);
423   if (getReachingDef(Last, PhysReg) != Def)
424     return false;
425 
426   // Finally check that the last instruction doesn't redefine the register.
427   for (auto &MO : Last->operands())
428     if (isValidRegDefOf(MO, PhysReg))
429       return false;
430 
431   return true;
432 }
433 
434 MachineInstr* ReachingDefAnalysis::getLocalLiveOutMIDef(MachineBasicBlock *MBB,
435                                                         int PhysReg) const {
436   LivePhysRegs LiveRegs(*TRI);
437   LiveRegs.addLiveOuts(*MBB);
438   if (!LiveRegs.contains(PhysReg))
439     return nullptr;
440 
441   MachineInstr *Last = &MBB->back();
442   int Def = getReachingDef(Last, PhysReg);
443   for (auto &MO : Last->operands())
444     if (isValidRegDefOf(MO, PhysReg))
445       return Last;
446 
447   return Def < 0 ? nullptr : getInstFromId(MBB, Def);
448 }
449 
450 static bool mayHaveSideEffects(MachineInstr &MI) {
451   return MI.mayLoadOrStore() || MI.mayRaiseFPException() ||
452          MI.hasUnmodeledSideEffects() || MI.isTerminator() ||
453          MI.isCall() || MI.isBarrier() || MI.isBranch() || MI.isReturn();
454 }
455 
456 // Can we safely move 'From' to just before 'To'? To satisfy this, 'From' must
457 // not define a register that is used by any instructions, after and including,
458 // 'To'. These instructions also must not redefine any of Froms operands.
459 template<typename Iterator>
460 bool ReachingDefAnalysis::isSafeToMove(MachineInstr *From,
461                                        MachineInstr *To) const {
462   if (From->getParent() != To->getParent())
463     return false;
464 
465   SmallSet<int, 2> Defs;
466   // First check that From would compute the same value if moved.
467   for (auto &MO : From->operands()) {
468     if (!isValidReg(MO))
469       continue;
470     if (MO.isDef())
471       Defs.insert(MO.getReg());
472     else if (!hasSameReachingDef(From, To, MO.getReg()))
473       return false;
474   }
475 
476   // Now walk checking that the rest of the instructions will compute the same
477   // value and that we're not overwriting anything. Don't move the instruction
478   // past any memory, control-flow or other ambigious instructions.
479   for (auto I = ++Iterator(From), E = Iterator(To); I != E; ++I) {
480     if (mayHaveSideEffects(*I))
481       return false;
482     for (auto &MO : I->operands())
483       if (MO.isReg() && MO.getReg() && Defs.count(MO.getReg()))
484         return false;
485   }
486   return true;
487 }
488 
489 bool ReachingDefAnalysis::isSafeToMoveForwards(MachineInstr *From,
490                                                MachineInstr *To) const {
491   return isSafeToMove<MachineBasicBlock::reverse_iterator>(From, To);
492 }
493 
494 bool ReachingDefAnalysis::isSafeToMoveBackwards(MachineInstr *From,
495                                                 MachineInstr *To) const {
496   return isSafeToMove<MachineBasicBlock::iterator>(From, To);
497 }
498 
499 bool ReachingDefAnalysis::isSafeToRemove(MachineInstr *MI,
500                                          InstSet &ToRemove) const {
501   SmallPtrSet<MachineInstr*, 1> Ignore;
502   SmallPtrSet<MachineInstr*, 2> Visited;
503   return isSafeToRemove(MI, Visited, ToRemove, Ignore);
504 }
505 
506 bool
507 ReachingDefAnalysis::isSafeToRemove(MachineInstr *MI, InstSet &ToRemove,
508                                     InstSet &Ignore) const {
509   SmallPtrSet<MachineInstr*, 2> Visited;
510   return isSafeToRemove(MI, Visited, ToRemove, Ignore);
511 }
512 
513 bool
514 ReachingDefAnalysis::isSafeToRemove(MachineInstr *MI, InstSet &Visited,
515                                     InstSet &ToRemove, InstSet &Ignore) const {
516   if (Visited.count(MI) || Ignore.count(MI))
517     return true;
518   else if (mayHaveSideEffects(*MI)) {
519     // Unless told to ignore the instruction, don't remove anything which has
520     // side effects.
521     return false;
522   }
523 
524   Visited.insert(MI);
525   for (auto &MO : MI->operands()) {
526     if (!isValidRegDef(MO))
527       continue;
528 
529     SmallPtrSet<MachineInstr*, 4> Uses;
530     getGlobalUses(MI, MO.getReg(), Uses);
531 
532     for (auto I : Uses) {
533       if (Ignore.count(I) || ToRemove.count(I))
534         continue;
535       if (!isSafeToRemove(I, Visited, ToRemove, Ignore))
536         return false;
537     }
538   }
539   ToRemove.insert(MI);
540   return true;
541 }
542 
543 void ReachingDefAnalysis::collectKilledOperands(MachineInstr *MI,
544                                                 InstSet &Dead) const {
545   Dead.insert(MI);
546   auto IsDead = [this, &Dead](MachineInstr *Def, int PhysReg) {
547     unsigned LiveDefs = 0;
548     for (auto &MO : Def->operands()) {
549       if (!isValidRegDef(MO))
550         continue;
551       if (!MO.isDead())
552         ++LiveDefs;
553     }
554 
555     if (LiveDefs > 1)
556       return false;
557 
558     SmallPtrSet<MachineInstr*, 4> Uses;
559     getGlobalUses(Def, PhysReg, Uses);
560     for (auto *Use : Uses)
561       if (!Dead.count(Use))
562         return false;
563     return true;
564   };
565 
566   for (auto &MO : MI->operands()) {
567     if (!isValidRegUse(MO))
568       continue;
569     if (MachineInstr *Def = getMIOperand(MI, MO))
570       if (IsDead(Def, MO.getReg()))
571         collectKilledOperands(Def, Dead);
572   }
573 }
574 
575 bool ReachingDefAnalysis::isSafeToDefRegAt(MachineInstr *MI,
576                                            int PhysReg) const {
577   SmallPtrSet<MachineInstr*, 1> Ignore;
578   return isSafeToDefRegAt(MI, PhysReg, Ignore);
579 }
580 
581 bool ReachingDefAnalysis::isSafeToDefRegAt(MachineInstr *MI, int PhysReg,
582                                            InstSet &Ignore) const {
583   // Check for any uses of the register after MI.
584   if (isRegUsedAfter(MI, PhysReg)) {
585     if (auto *Def = getReachingLocalMIDef(MI, PhysReg)) {
586       SmallPtrSet<MachineInstr*, 2> Uses;
587       getReachingLocalUses(Def, PhysReg, Uses);
588       for (auto *Use : Uses)
589         if (!Ignore.count(Use))
590           return false;
591     } else
592       return false;
593   }
594 
595   MachineBasicBlock *MBB = MI->getParent();
596   // Check for any defs after MI.
597   if (isRegDefinedAfter(MI, PhysReg)) {
598     auto I = MachineBasicBlock::iterator(MI);
599     for (auto E = MBB->end(); I != E; ++I) {
600       if (Ignore.count(&*I))
601         continue;
602       for (auto &MO : I->operands())
603         if (isValidRegDefOf(MO, PhysReg))
604           return false;
605     }
606   }
607   return true;
608 }
609