109467b48Spatrick //===---- ReachingDefAnalysis.cpp - Reaching Def Analysis ---*- C++ -*-----===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick 
9097a140dSpatrick #include "llvm/ADT/SmallSet.h"
1073471bf0Spatrick #include "llvm/ADT/SetOperations.h"
1109467b48Spatrick #include "llvm/CodeGen/LivePhysRegs.h"
1209467b48Spatrick #include "llvm/CodeGen/ReachingDefAnalysis.h"
1309467b48Spatrick #include "llvm/CodeGen/TargetRegisterInfo.h"
1409467b48Spatrick #include "llvm/CodeGen/TargetSubtargetInfo.h"
1509467b48Spatrick #include "llvm/Support/Debug.h"
1609467b48Spatrick 
1709467b48Spatrick using namespace llvm;
1809467b48Spatrick 
1909467b48Spatrick #define DEBUG_TYPE "reaching-deps-analysis"
2009467b48Spatrick 
2109467b48Spatrick char ReachingDefAnalysis::ID = 0;
2209467b48Spatrick INITIALIZE_PASS(ReachingDefAnalysis, DEBUG_TYPE, "ReachingDefAnalysis", false,
2309467b48Spatrick                 true)
2409467b48Spatrick 
isValidReg(const MachineOperand & MO)25097a140dSpatrick static bool isValidReg(const MachineOperand &MO) {
26097a140dSpatrick   return MO.isReg() && MO.getReg();
27097a140dSpatrick }
2809467b48Spatrick 
isValidRegUse(const MachineOperand & MO)29097a140dSpatrick static bool isValidRegUse(const MachineOperand &MO) {
30097a140dSpatrick   return isValidReg(MO) && MO.isUse();
31097a140dSpatrick }
32097a140dSpatrick 
isValidRegUseOf(const MachineOperand & MO,MCRegister PhysReg,const TargetRegisterInfo * TRI)33*d415bd75Srobert static bool isValidRegUseOf(const MachineOperand &MO, MCRegister PhysReg,
34*d415bd75Srobert                             const TargetRegisterInfo *TRI) {
35*d415bd75Srobert   if (!isValidRegUse(MO))
36*d415bd75Srobert     return false;
37*d415bd75Srobert   return TRI->regsOverlap(MO.getReg(), PhysReg);
38097a140dSpatrick }
39097a140dSpatrick 
isValidRegDef(const MachineOperand & MO)40097a140dSpatrick static bool isValidRegDef(const MachineOperand &MO) {
41097a140dSpatrick   return isValidReg(MO) && MO.isDef();
42097a140dSpatrick }
43097a140dSpatrick 
isValidRegDefOf(const MachineOperand & MO,MCRegister PhysReg,const TargetRegisterInfo * TRI)44*d415bd75Srobert static bool isValidRegDefOf(const MachineOperand &MO, MCRegister PhysReg,
45*d415bd75Srobert                             const TargetRegisterInfo *TRI) {
46*d415bd75Srobert   if (!isValidRegDef(MO))
47*d415bd75Srobert     return false;
48*d415bd75Srobert   return TRI->regsOverlap(MO.getReg(), PhysReg);
49097a140dSpatrick }
50097a140dSpatrick 
enterBasicBlock(MachineBasicBlock * MBB)51097a140dSpatrick void ReachingDefAnalysis::enterBasicBlock(MachineBasicBlock *MBB) {
5209467b48Spatrick   unsigned MBBNumber = MBB->getNumber();
5309467b48Spatrick   assert(MBBNumber < MBBReachingDefs.size() &&
5409467b48Spatrick          "Unexpected basic block number.");
5509467b48Spatrick   MBBReachingDefs[MBBNumber].resize(NumRegUnits);
5609467b48Spatrick 
5709467b48Spatrick   // Reset instruction counter in each basic block.
5809467b48Spatrick   CurInstr = 0;
5909467b48Spatrick 
6009467b48Spatrick   // Set up LiveRegs to represent registers entering MBB.
6109467b48Spatrick   // Default values are 'nothing happened a long time ago'.
6209467b48Spatrick   if (LiveRegs.empty())
6309467b48Spatrick     LiveRegs.assign(NumRegUnits, ReachingDefDefaultVal);
6409467b48Spatrick 
6509467b48Spatrick   // This is the entry block.
6609467b48Spatrick   if (MBB->pred_empty()) {
6709467b48Spatrick     for (const auto &LI : MBB->liveins()) {
6809467b48Spatrick       for (MCRegUnitIterator Unit(LI.PhysReg, TRI); Unit.isValid(); ++Unit) {
6909467b48Spatrick         // Treat function live-ins as if they were defined just before the first
7009467b48Spatrick         // instruction.  Usually, function arguments are set up immediately
7109467b48Spatrick         // before the call.
72097a140dSpatrick         if (LiveRegs[*Unit] != -1) {
7309467b48Spatrick           LiveRegs[*Unit] = -1;
74097a140dSpatrick           MBBReachingDefs[MBBNumber][*Unit].push_back(-1);
75097a140dSpatrick         }
7609467b48Spatrick       }
7709467b48Spatrick     }
7809467b48Spatrick     LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << ": entry\n");
7909467b48Spatrick     return;
8009467b48Spatrick   }
8109467b48Spatrick 
8209467b48Spatrick   // Try to coalesce live-out registers from predecessors.
8309467b48Spatrick   for (MachineBasicBlock *pred : MBB->predecessors()) {
8409467b48Spatrick     assert(unsigned(pred->getNumber()) < MBBOutRegsInfos.size() &&
8509467b48Spatrick            "Should have pre-allocated MBBInfos for all MBBs");
8609467b48Spatrick     const LiveRegsDefInfo &Incoming = MBBOutRegsInfos[pred->getNumber()];
8709467b48Spatrick     // Incoming is null if this is a backedge from a BB
8809467b48Spatrick     // we haven't processed yet
8909467b48Spatrick     if (Incoming.empty())
9009467b48Spatrick       continue;
9109467b48Spatrick 
92097a140dSpatrick     // Find the most recent reaching definition from a predecessor.
93097a140dSpatrick     for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit)
9409467b48Spatrick       LiveRegs[Unit] = std::max(LiveRegs[Unit], Incoming[Unit]);
95097a140dSpatrick   }
96097a140dSpatrick 
97097a140dSpatrick   // Insert the most recent reaching definition we found.
98097a140dSpatrick   for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit)
99097a140dSpatrick     if (LiveRegs[Unit] != ReachingDefDefaultVal)
10009467b48Spatrick       MBBReachingDefs[MBBNumber][Unit].push_back(LiveRegs[Unit]);
10109467b48Spatrick }
10209467b48Spatrick 
leaveBasicBlock(MachineBasicBlock * MBB)103097a140dSpatrick void ReachingDefAnalysis::leaveBasicBlock(MachineBasicBlock *MBB) {
10409467b48Spatrick   assert(!LiveRegs.empty() && "Must enter basic block first.");
105097a140dSpatrick   unsigned MBBNumber = MBB->getNumber();
10609467b48Spatrick   assert(MBBNumber < MBBOutRegsInfos.size() &&
10709467b48Spatrick          "Unexpected basic block number.");
10809467b48Spatrick   // Save register clearances at end of MBB - used by enterBasicBlock().
10909467b48Spatrick   MBBOutRegsInfos[MBBNumber] = LiveRegs;
11009467b48Spatrick 
11109467b48Spatrick   // While processing the basic block, we kept `Def` relative to the start
11209467b48Spatrick   // of the basic block for convenience. However, future use of this information
11309467b48Spatrick   // only cares about the clearance from the end of the block, so adjust
11409467b48Spatrick   // everything to be relative to the end of the basic block.
11509467b48Spatrick   for (int &OutLiveReg : MBBOutRegsInfos[MBBNumber])
116097a140dSpatrick     if (OutLiveReg != ReachingDefDefaultVal)
11709467b48Spatrick       OutLiveReg -= CurInstr;
11809467b48Spatrick   LiveRegs.clear();
11909467b48Spatrick }
12009467b48Spatrick 
processDefs(MachineInstr * MI)12109467b48Spatrick void ReachingDefAnalysis::processDefs(MachineInstr *MI) {
12209467b48Spatrick   assert(!MI->isDebugInstr() && "Won't process debug instructions");
12309467b48Spatrick 
12409467b48Spatrick   unsigned MBBNumber = MI->getParent()->getNumber();
12509467b48Spatrick   assert(MBBNumber < MBBReachingDefs.size() &&
12609467b48Spatrick          "Unexpected basic block number.");
127097a140dSpatrick 
128097a140dSpatrick   for (auto &MO : MI->operands()) {
129097a140dSpatrick     if (!isValidRegDef(MO))
13009467b48Spatrick       continue;
13173471bf0Spatrick     for (MCRegUnitIterator Unit(MO.getReg().asMCReg(), TRI); Unit.isValid();
13273471bf0Spatrick          ++Unit) {
13309467b48Spatrick       // This instruction explicitly defines the current reg unit.
13473471bf0Spatrick       LLVM_DEBUG(dbgs() << printRegUnit(*Unit, TRI) << ":\t" << CurInstr
13509467b48Spatrick                         << '\t' << *MI);
13609467b48Spatrick 
13709467b48Spatrick       // How many instructions since this reg unit was last written?
138097a140dSpatrick       if (LiveRegs[*Unit] != CurInstr) {
13909467b48Spatrick         LiveRegs[*Unit] = CurInstr;
14009467b48Spatrick         MBBReachingDefs[MBBNumber][*Unit].push_back(CurInstr);
14109467b48Spatrick       }
14209467b48Spatrick     }
143097a140dSpatrick   }
14409467b48Spatrick   InstIds[MI] = CurInstr;
14509467b48Spatrick   ++CurInstr;
14609467b48Spatrick }
14709467b48Spatrick 
reprocessBasicBlock(MachineBasicBlock * MBB)148097a140dSpatrick void ReachingDefAnalysis::reprocessBasicBlock(MachineBasicBlock *MBB) {
149097a140dSpatrick   unsigned MBBNumber = MBB->getNumber();
150097a140dSpatrick   assert(MBBNumber < MBBReachingDefs.size() &&
151097a140dSpatrick          "Unexpected basic block number.");
152097a140dSpatrick 
153097a140dSpatrick   // Count number of non-debug instructions for end of block adjustment.
15473471bf0Spatrick   auto NonDbgInsts =
15573471bf0Spatrick     instructionsWithoutDebug(MBB->instr_begin(), MBB->instr_end());
15673471bf0Spatrick   int NumInsts = std::distance(NonDbgInsts.begin(), NonDbgInsts.end());
157097a140dSpatrick 
158097a140dSpatrick   // When reprocessing a block, the only thing we need to do is check whether
159097a140dSpatrick   // there is now a more recent incoming reaching definition from a predecessor.
160097a140dSpatrick   for (MachineBasicBlock *pred : MBB->predecessors()) {
161097a140dSpatrick     assert(unsigned(pred->getNumber()) < MBBOutRegsInfos.size() &&
162097a140dSpatrick            "Should have pre-allocated MBBInfos for all MBBs");
163097a140dSpatrick     const LiveRegsDefInfo &Incoming = MBBOutRegsInfos[pred->getNumber()];
164097a140dSpatrick     // Incoming may be empty for dead predecessors.
165097a140dSpatrick     if (Incoming.empty())
166097a140dSpatrick       continue;
167097a140dSpatrick 
168097a140dSpatrick     for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit) {
169097a140dSpatrick       int Def = Incoming[Unit];
170097a140dSpatrick       if (Def == ReachingDefDefaultVal)
171097a140dSpatrick         continue;
172097a140dSpatrick 
173097a140dSpatrick       auto Start = MBBReachingDefs[MBBNumber][Unit].begin();
174097a140dSpatrick       if (Start != MBBReachingDefs[MBBNumber][Unit].end() && *Start < 0) {
175097a140dSpatrick         if (*Start >= Def)
176097a140dSpatrick           continue;
177097a140dSpatrick 
178097a140dSpatrick         // Update existing reaching def from predecessor to a more recent one.
179097a140dSpatrick         *Start = Def;
180097a140dSpatrick       } else {
181097a140dSpatrick         // Insert new reaching def from predecessor.
182097a140dSpatrick         MBBReachingDefs[MBBNumber][Unit].insert(Start, Def);
183097a140dSpatrick       }
184097a140dSpatrick 
185097a140dSpatrick       // Update reaching def at end of of BB. Keep in mind that these are
186097a140dSpatrick       // adjusted relative to the end of the basic block.
187097a140dSpatrick       if (MBBOutRegsInfos[MBBNumber][Unit] < Def - NumInsts)
188097a140dSpatrick         MBBOutRegsInfos[MBBNumber][Unit] = Def - NumInsts;
189097a140dSpatrick     }
190097a140dSpatrick   }
191097a140dSpatrick }
192097a140dSpatrick 
processBasicBlock(const LoopTraversal::TraversedMBBInfo & TraversedMBB)19309467b48Spatrick void ReachingDefAnalysis::processBasicBlock(
19409467b48Spatrick     const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
195097a140dSpatrick   MachineBasicBlock *MBB = TraversedMBB.MBB;
196097a140dSpatrick   LLVM_DEBUG(dbgs() << printMBBReference(*MBB)
197097a140dSpatrick                     << (!TraversedMBB.IsDone ? ": incomplete\n"
198097a140dSpatrick                                              : ": all preds known\n"));
199097a140dSpatrick 
200097a140dSpatrick   if (!TraversedMBB.PrimaryPass) {
201097a140dSpatrick     // Reprocess MBB that is part of a loop.
202097a140dSpatrick     reprocessBasicBlock(MBB);
203097a140dSpatrick     return;
204097a140dSpatrick   }
205097a140dSpatrick 
206097a140dSpatrick   enterBasicBlock(MBB);
20773471bf0Spatrick   for (MachineInstr &MI :
20873471bf0Spatrick        instructionsWithoutDebug(MBB->instr_begin(), MBB->instr_end()))
20909467b48Spatrick     processDefs(&MI);
210097a140dSpatrick   leaveBasicBlock(MBB);
21109467b48Spatrick }
21209467b48Spatrick 
runOnMachineFunction(MachineFunction & mf)21309467b48Spatrick bool ReachingDefAnalysis::runOnMachineFunction(MachineFunction &mf) {
21409467b48Spatrick   MF = &mf;
21509467b48Spatrick   TRI = MF->getSubtarget().getRegisterInfo();
21609467b48Spatrick   LLVM_DEBUG(dbgs() << "********** REACHING DEFINITION ANALYSIS **********\n");
217097a140dSpatrick   init();
218097a140dSpatrick   traverse();
21909467b48Spatrick   return false;
22009467b48Spatrick }
22109467b48Spatrick 
releaseMemory()22209467b48Spatrick void ReachingDefAnalysis::releaseMemory() {
22309467b48Spatrick   // Clear the internal vectors.
22409467b48Spatrick   MBBOutRegsInfos.clear();
22509467b48Spatrick   MBBReachingDefs.clear();
22609467b48Spatrick   InstIds.clear();
227097a140dSpatrick   LiveRegs.clear();
22809467b48Spatrick }
22909467b48Spatrick 
reset()230097a140dSpatrick void ReachingDefAnalysis::reset() {
231097a140dSpatrick   releaseMemory();
232097a140dSpatrick   init();
233097a140dSpatrick   traverse();
234097a140dSpatrick }
235097a140dSpatrick 
init()236097a140dSpatrick void ReachingDefAnalysis::init() {
237097a140dSpatrick   NumRegUnits = TRI->getNumRegUnits();
238097a140dSpatrick   MBBReachingDefs.resize(MF->getNumBlockIDs());
239097a140dSpatrick   // Initialize the MBBOutRegsInfos
240097a140dSpatrick   MBBOutRegsInfos.resize(MF->getNumBlockIDs());
241097a140dSpatrick   LoopTraversal Traversal;
242097a140dSpatrick   TraversedMBBOrder = Traversal.traverse(*MF);
243097a140dSpatrick }
244097a140dSpatrick 
traverse()245097a140dSpatrick void ReachingDefAnalysis::traverse() {
246097a140dSpatrick   // Traverse the basic blocks.
247097a140dSpatrick   for (LoopTraversal::TraversedMBBInfo TraversedMBB : TraversedMBBOrder)
248097a140dSpatrick     processBasicBlock(TraversedMBB);
249097a140dSpatrick #ifndef NDEBUG
250097a140dSpatrick   // Make sure reaching defs are sorted and unique.
251097a140dSpatrick   for (MBBDefsInfo &MBBDefs : MBBReachingDefs) {
252097a140dSpatrick     for (MBBRegUnitDefs &RegUnitDefs : MBBDefs) {
253097a140dSpatrick       int LastDef = ReachingDefDefaultVal;
254097a140dSpatrick       for (int Def : RegUnitDefs) {
255097a140dSpatrick         assert(Def > LastDef && "Defs must be sorted and unique");
256097a140dSpatrick         LastDef = Def;
257097a140dSpatrick       }
258097a140dSpatrick     }
259097a140dSpatrick   }
260097a140dSpatrick #endif
261097a140dSpatrick }
262097a140dSpatrick 
getReachingDef(MachineInstr * MI,MCRegister PhysReg) const26373471bf0Spatrick int ReachingDefAnalysis::getReachingDef(MachineInstr *MI,
26473471bf0Spatrick                                         MCRegister PhysReg) const {
26509467b48Spatrick   assert(InstIds.count(MI) && "Unexpected machine instuction.");
266097a140dSpatrick   int InstId = InstIds.lookup(MI);
26709467b48Spatrick   int DefRes = ReachingDefDefaultVal;
26809467b48Spatrick   unsigned MBBNumber = MI->getParent()->getNumber();
26909467b48Spatrick   assert(MBBNumber < MBBReachingDefs.size() &&
27009467b48Spatrick          "Unexpected basic block number.");
27109467b48Spatrick   int LatestDef = ReachingDefDefaultVal;
27209467b48Spatrick   for (MCRegUnitIterator Unit(PhysReg, TRI); Unit.isValid(); ++Unit) {
27309467b48Spatrick     for (int Def : MBBReachingDefs[MBBNumber][*Unit]) {
27409467b48Spatrick       if (Def >= InstId)
27509467b48Spatrick         break;
27609467b48Spatrick       DefRes = Def;
27709467b48Spatrick     }
27809467b48Spatrick     LatestDef = std::max(LatestDef, DefRes);
27909467b48Spatrick   }
28009467b48Spatrick   return LatestDef;
28109467b48Spatrick }
28209467b48Spatrick 
28373471bf0Spatrick MachineInstr *
getReachingLocalMIDef(MachineInstr * MI,MCRegister PhysReg) const28473471bf0Spatrick ReachingDefAnalysis::getReachingLocalMIDef(MachineInstr *MI,
28573471bf0Spatrick                                            MCRegister PhysReg) const {
28673471bf0Spatrick   return hasLocalDefBefore(MI, PhysReg)
28773471bf0Spatrick     ? getInstFromId(MI->getParent(), getReachingDef(MI, PhysReg))
28873471bf0Spatrick     : nullptr;
28909467b48Spatrick }
29009467b48Spatrick 
hasSameReachingDef(MachineInstr * A,MachineInstr * B,MCRegister PhysReg) const29109467b48Spatrick bool ReachingDefAnalysis::hasSameReachingDef(MachineInstr *A, MachineInstr *B,
29273471bf0Spatrick                                              MCRegister PhysReg) const {
29309467b48Spatrick   MachineBasicBlock *ParentA = A->getParent();
29409467b48Spatrick   MachineBasicBlock *ParentB = B->getParent();
29509467b48Spatrick   if (ParentA != ParentB)
29609467b48Spatrick     return false;
29709467b48Spatrick 
29809467b48Spatrick   return getReachingDef(A, PhysReg) == getReachingDef(B, PhysReg);
29909467b48Spatrick }
30009467b48Spatrick 
getInstFromId(MachineBasicBlock * MBB,int InstId) const30109467b48Spatrick MachineInstr *ReachingDefAnalysis::getInstFromId(MachineBasicBlock *MBB,
302097a140dSpatrick                                                  int InstId) const {
30309467b48Spatrick   assert(static_cast<size_t>(MBB->getNumber()) < MBBReachingDefs.size() &&
30409467b48Spatrick          "Unexpected basic block number.");
30509467b48Spatrick   assert(InstId < static_cast<int>(MBB->size()) &&
30609467b48Spatrick          "Unexpected instruction id.");
30709467b48Spatrick 
30809467b48Spatrick   if (InstId < 0)
30909467b48Spatrick     return nullptr;
31009467b48Spatrick 
31109467b48Spatrick   for (auto &MI : *MBB) {
312097a140dSpatrick     auto F = InstIds.find(&MI);
313097a140dSpatrick     if (F != InstIds.end() && F->second == InstId)
31409467b48Spatrick       return &MI;
31509467b48Spatrick   }
316097a140dSpatrick 
31709467b48Spatrick   return nullptr;
31809467b48Spatrick }
31909467b48Spatrick 
getClearance(MachineInstr * MI,MCRegister PhysReg) const32073471bf0Spatrick int ReachingDefAnalysis::getClearance(MachineInstr *MI,
32173471bf0Spatrick                                       MCRegister PhysReg) const {
32209467b48Spatrick   assert(InstIds.count(MI) && "Unexpected machine instuction.");
323097a140dSpatrick   return InstIds.lookup(MI) - getReachingDef(MI, PhysReg);
324097a140dSpatrick }
325097a140dSpatrick 
hasLocalDefBefore(MachineInstr * MI,MCRegister PhysReg) const32673471bf0Spatrick bool ReachingDefAnalysis::hasLocalDefBefore(MachineInstr *MI,
32773471bf0Spatrick                                             MCRegister PhysReg) const {
328097a140dSpatrick   return getReachingDef(MI, PhysReg) >= 0;
32909467b48Spatrick }
33009467b48Spatrick 
getReachingLocalUses(MachineInstr * Def,MCRegister PhysReg,InstSet & Uses) const33173471bf0Spatrick void ReachingDefAnalysis::getReachingLocalUses(MachineInstr *Def,
33273471bf0Spatrick                                                MCRegister PhysReg,
333097a140dSpatrick                                                InstSet &Uses) const {
33409467b48Spatrick   MachineBasicBlock *MBB = Def->getParent();
33509467b48Spatrick   MachineBasicBlock::iterator MI = MachineBasicBlock::iterator(Def);
33609467b48Spatrick   while (++MI != MBB->end()) {
337097a140dSpatrick     if (MI->isDebugInstr())
338097a140dSpatrick       continue;
339097a140dSpatrick 
34009467b48Spatrick     // If/when we find a new reaching def, we know that there's no more uses
34109467b48Spatrick     // of 'Def'.
342097a140dSpatrick     if (getReachingLocalMIDef(&*MI, PhysReg) != Def)
34309467b48Spatrick       return;
34409467b48Spatrick 
34509467b48Spatrick     for (auto &MO : MI->operands()) {
346*d415bd75Srobert       if (!isValidRegUseOf(MO, PhysReg, TRI))
34709467b48Spatrick         continue;
34809467b48Spatrick 
349097a140dSpatrick       Uses.insert(&*MI);
35009467b48Spatrick       if (MO.isKill())
35109467b48Spatrick         return;
35209467b48Spatrick     }
35309467b48Spatrick   }
35409467b48Spatrick }
35509467b48Spatrick 
getLiveInUses(MachineBasicBlock * MBB,MCRegister PhysReg,InstSet & Uses) const35673471bf0Spatrick bool ReachingDefAnalysis::getLiveInUses(MachineBasicBlock *MBB,
35773471bf0Spatrick                                         MCRegister PhysReg,
358097a140dSpatrick                                         InstSet &Uses) const {
35973471bf0Spatrick   for (MachineInstr &MI :
36073471bf0Spatrick        instructionsWithoutDebug(MBB->instr_begin(), MBB->instr_end())) {
361097a140dSpatrick     for (auto &MO : MI.operands()) {
362*d415bd75Srobert       if (!isValidRegUseOf(MO, PhysReg, TRI))
363097a140dSpatrick         continue;
364097a140dSpatrick       if (getReachingDef(&MI, PhysReg) >= 0)
365097a140dSpatrick         return false;
366097a140dSpatrick       Uses.insert(&MI);
367097a140dSpatrick     }
368097a140dSpatrick   }
36973471bf0Spatrick   auto Last = MBB->getLastNonDebugInstr();
37073471bf0Spatrick   if (Last == MBB->end())
37173471bf0Spatrick     return true;
37273471bf0Spatrick   return isReachingDefLiveOut(&*Last, PhysReg);
37309467b48Spatrick }
37409467b48Spatrick 
getGlobalUses(MachineInstr * MI,MCRegister PhysReg,InstSet & Uses) const37573471bf0Spatrick void ReachingDefAnalysis::getGlobalUses(MachineInstr *MI, MCRegister PhysReg,
376097a140dSpatrick                                         InstSet &Uses) const {
377097a140dSpatrick   MachineBasicBlock *MBB = MI->getParent();
378097a140dSpatrick 
379097a140dSpatrick   // Collect the uses that each def touches within the block.
380097a140dSpatrick   getReachingLocalUses(MI, PhysReg, Uses);
381097a140dSpatrick 
382097a140dSpatrick   // Handle live-out values.
383097a140dSpatrick   if (auto *LiveOut = getLocalLiveOutMIDef(MI->getParent(), PhysReg)) {
384097a140dSpatrick     if (LiveOut != MI)
385097a140dSpatrick       return;
386097a140dSpatrick 
38773471bf0Spatrick     SmallVector<MachineBasicBlock *, 4> ToVisit(MBB->successors());
388097a140dSpatrick     SmallPtrSet<MachineBasicBlock*, 4>Visited;
389097a140dSpatrick     while (!ToVisit.empty()) {
390*d415bd75Srobert       MachineBasicBlock *MBB = ToVisit.pop_back_val();
391097a140dSpatrick       if (Visited.count(MBB) || !MBB->isLiveIn(PhysReg))
392097a140dSpatrick         continue;
393097a140dSpatrick       if (getLiveInUses(MBB, PhysReg, Uses))
39473471bf0Spatrick         llvm::append_range(ToVisit, MBB->successors());
395097a140dSpatrick       Visited.insert(MBB);
396097a140dSpatrick     }
397097a140dSpatrick   }
398097a140dSpatrick }
399097a140dSpatrick 
getGlobalReachingDefs(MachineInstr * MI,MCRegister PhysReg,InstSet & Defs) const40073471bf0Spatrick void ReachingDefAnalysis::getGlobalReachingDefs(MachineInstr *MI,
40173471bf0Spatrick                                                 MCRegister PhysReg,
402097a140dSpatrick                                                 InstSet &Defs) const {
40373471bf0Spatrick   if (auto *Def = getUniqueReachingMIDef(MI, PhysReg)) {
40473471bf0Spatrick     Defs.insert(Def);
40573471bf0Spatrick     return;
40673471bf0Spatrick   }
40773471bf0Spatrick 
40873471bf0Spatrick   for (auto *MBB : MI->getParent()->predecessors())
40973471bf0Spatrick     getLiveOuts(MBB, PhysReg, Defs);
41073471bf0Spatrick }
41173471bf0Spatrick 
getLiveOuts(MachineBasicBlock * MBB,MCRegister PhysReg,InstSet & Defs) const41273471bf0Spatrick void ReachingDefAnalysis::getLiveOuts(MachineBasicBlock *MBB,
41373471bf0Spatrick                                       MCRegister PhysReg, InstSet &Defs) const {
414097a140dSpatrick   SmallPtrSet<MachineBasicBlock*, 2> VisitedBBs;
415097a140dSpatrick   getLiveOuts(MBB, PhysReg, Defs, VisitedBBs);
416097a140dSpatrick }
417097a140dSpatrick 
getLiveOuts(MachineBasicBlock * MBB,MCRegister PhysReg,InstSet & Defs,BlockSet & VisitedBBs) const41873471bf0Spatrick void ReachingDefAnalysis::getLiveOuts(MachineBasicBlock *MBB,
41973471bf0Spatrick                                       MCRegister PhysReg, InstSet &Defs,
42073471bf0Spatrick                                       BlockSet &VisitedBBs) const {
421097a140dSpatrick   if (VisitedBBs.count(MBB))
422097a140dSpatrick     return;
423097a140dSpatrick 
424097a140dSpatrick   VisitedBBs.insert(MBB);
425097a140dSpatrick   LivePhysRegs LiveRegs(*TRI);
426097a140dSpatrick   LiveRegs.addLiveOuts(*MBB);
427*d415bd75Srobert   if (LiveRegs.available(MBB->getParent()->getRegInfo(), PhysReg))
428097a140dSpatrick     return;
429097a140dSpatrick 
430097a140dSpatrick   if (auto *Def = getLocalLiveOutMIDef(MBB, PhysReg))
431097a140dSpatrick     Defs.insert(Def);
432097a140dSpatrick   else
433097a140dSpatrick     for (auto *Pred : MBB->predecessors())
434097a140dSpatrick       getLiveOuts(Pred, PhysReg, Defs, VisitedBBs);
435097a140dSpatrick }
436097a140dSpatrick 
43773471bf0Spatrick MachineInstr *
getUniqueReachingMIDef(MachineInstr * MI,MCRegister PhysReg) const43873471bf0Spatrick ReachingDefAnalysis::getUniqueReachingMIDef(MachineInstr *MI,
43973471bf0Spatrick                                             MCRegister PhysReg) const {
440097a140dSpatrick   // If there's a local def before MI, return it.
441097a140dSpatrick   MachineInstr *LocalDef = getReachingLocalMIDef(MI, PhysReg);
442097a140dSpatrick   if (LocalDef && InstIds.lookup(LocalDef) < InstIds.lookup(MI))
443097a140dSpatrick     return LocalDef;
444097a140dSpatrick 
445097a140dSpatrick   SmallPtrSet<MachineInstr*, 2> Incoming;
44673471bf0Spatrick   MachineBasicBlock *Parent = MI->getParent();
44773471bf0Spatrick   for (auto *Pred : Parent->predecessors())
44873471bf0Spatrick     getLiveOuts(Pred, PhysReg, Incoming);
449097a140dSpatrick 
45073471bf0Spatrick   // Check that we have a single incoming value and that it does not
45173471bf0Spatrick   // come from the same block as MI - since it would mean that the def
45273471bf0Spatrick   // is executed after MI.
45373471bf0Spatrick   if (Incoming.size() == 1 && (*Incoming.begin())->getParent() != Parent)
454097a140dSpatrick     return *Incoming.begin();
45573471bf0Spatrick   return nullptr;
456097a140dSpatrick }
457097a140dSpatrick 
getMIOperand(MachineInstr * MI,unsigned Idx) const458097a140dSpatrick MachineInstr *ReachingDefAnalysis::getMIOperand(MachineInstr *MI,
459097a140dSpatrick                                                 unsigned Idx) const {
460097a140dSpatrick   assert(MI->getOperand(Idx).isReg() && "Expected register operand");
461097a140dSpatrick   return getUniqueReachingMIDef(MI, MI->getOperand(Idx).getReg());
462097a140dSpatrick }
463097a140dSpatrick 
getMIOperand(MachineInstr * MI,MachineOperand & MO) const464097a140dSpatrick MachineInstr *ReachingDefAnalysis::getMIOperand(MachineInstr *MI,
465097a140dSpatrick                                                 MachineOperand &MO) const {
466097a140dSpatrick   assert(MO.isReg() && "Expected register operand");
467097a140dSpatrick   return getUniqueReachingMIDef(MI, MO.getReg());
468097a140dSpatrick }
469097a140dSpatrick 
isRegUsedAfter(MachineInstr * MI,MCRegister PhysReg) const47073471bf0Spatrick bool ReachingDefAnalysis::isRegUsedAfter(MachineInstr *MI,
47173471bf0Spatrick                                          MCRegister PhysReg) const {
47209467b48Spatrick   MachineBasicBlock *MBB = MI->getParent();
47309467b48Spatrick   LivePhysRegs LiveRegs(*TRI);
47409467b48Spatrick   LiveRegs.addLiveOuts(*MBB);
47509467b48Spatrick 
47609467b48Spatrick   // Yes if the register is live out of the basic block.
477*d415bd75Srobert   if (!LiveRegs.available(MBB->getParent()->getRegInfo(), PhysReg))
47809467b48Spatrick     return true;
47909467b48Spatrick 
48009467b48Spatrick   // Walk backwards through the block to see if the register is live at some
48109467b48Spatrick   // point.
48273471bf0Spatrick   for (MachineInstr &Last :
48373471bf0Spatrick        instructionsWithoutDebug(MBB->instr_rbegin(), MBB->instr_rend())) {
48473471bf0Spatrick     LiveRegs.stepBackward(Last);
485*d415bd75Srobert     if (!LiveRegs.available(MBB->getParent()->getRegInfo(), PhysReg))
48673471bf0Spatrick       return InstIds.lookup(&Last) > InstIds.lookup(MI);
48709467b48Spatrick   }
48809467b48Spatrick   return false;
48909467b48Spatrick }
49009467b48Spatrick 
isRegDefinedAfter(MachineInstr * MI,MCRegister PhysReg) const491097a140dSpatrick bool ReachingDefAnalysis::isRegDefinedAfter(MachineInstr *MI,
49273471bf0Spatrick                                             MCRegister PhysReg) const {
493097a140dSpatrick   MachineBasicBlock *MBB = MI->getParent();
49473471bf0Spatrick   auto Last = MBB->getLastNonDebugInstr();
49573471bf0Spatrick   if (Last != MBB->end() &&
49673471bf0Spatrick       getReachingDef(MI, PhysReg) != getReachingDef(&*Last, PhysReg))
497097a140dSpatrick     return true;
498097a140dSpatrick 
499097a140dSpatrick   if (auto *Def = getLocalLiveOutMIDef(MBB, PhysReg))
500097a140dSpatrick     return Def == getReachingLocalMIDef(MI, PhysReg);
501097a140dSpatrick 
502097a140dSpatrick   return false;
503097a140dSpatrick }
504097a140dSpatrick 
isReachingDefLiveOut(MachineInstr * MI,MCRegister PhysReg) const50573471bf0Spatrick bool ReachingDefAnalysis::isReachingDefLiveOut(MachineInstr *MI,
50673471bf0Spatrick                                                MCRegister PhysReg) const {
50709467b48Spatrick   MachineBasicBlock *MBB = MI->getParent();
50809467b48Spatrick   LivePhysRegs LiveRegs(*TRI);
50909467b48Spatrick   LiveRegs.addLiveOuts(*MBB);
510*d415bd75Srobert   if (LiveRegs.available(MBB->getParent()->getRegInfo(), PhysReg))
51109467b48Spatrick     return false;
51209467b48Spatrick 
51373471bf0Spatrick   auto Last = MBB->getLastNonDebugInstr();
51409467b48Spatrick   int Def = getReachingDef(MI, PhysReg);
51573471bf0Spatrick   if (Last != MBB->end() && getReachingDef(&*Last, PhysReg) != Def)
51609467b48Spatrick     return false;
51709467b48Spatrick 
51809467b48Spatrick   // Finally check that the last instruction doesn't redefine the register.
51909467b48Spatrick   for (auto &MO : Last->operands())
520*d415bd75Srobert     if (isValidRegDefOf(MO, PhysReg, TRI))
52109467b48Spatrick       return false;
52209467b48Spatrick 
52309467b48Spatrick   return true;
52409467b48Spatrick }
52509467b48Spatrick 
52673471bf0Spatrick MachineInstr *
getLocalLiveOutMIDef(MachineBasicBlock * MBB,MCRegister PhysReg) const52773471bf0Spatrick ReachingDefAnalysis::getLocalLiveOutMIDef(MachineBasicBlock *MBB,
52873471bf0Spatrick                                           MCRegister PhysReg) const {
52909467b48Spatrick   LivePhysRegs LiveRegs(*TRI);
53009467b48Spatrick   LiveRegs.addLiveOuts(*MBB);
531*d415bd75Srobert   if (LiveRegs.available(MBB->getParent()->getRegInfo(), PhysReg))
53209467b48Spatrick     return nullptr;
53309467b48Spatrick 
53473471bf0Spatrick   auto Last = MBB->getLastNonDebugInstr();
53573471bf0Spatrick   if (Last == MBB->end())
53673471bf0Spatrick     return nullptr;
53773471bf0Spatrick 
53873471bf0Spatrick   int Def = getReachingDef(&*Last, PhysReg);
53909467b48Spatrick   for (auto &MO : Last->operands())
540*d415bd75Srobert     if (isValidRegDefOf(MO, PhysReg, TRI))
54173471bf0Spatrick       return &*Last;
54209467b48Spatrick 
54309467b48Spatrick   return Def < 0 ? nullptr : getInstFromId(MBB, Def);
54409467b48Spatrick }
54509467b48Spatrick 
mayHaveSideEffects(MachineInstr & MI)546097a140dSpatrick static bool mayHaveSideEffects(MachineInstr &MI) {
547097a140dSpatrick   return MI.mayLoadOrStore() || MI.mayRaiseFPException() ||
548097a140dSpatrick          MI.hasUnmodeledSideEffects() || MI.isTerminator() ||
549097a140dSpatrick          MI.isCall() || MI.isBarrier() || MI.isBranch() || MI.isReturn();
550097a140dSpatrick }
55109467b48Spatrick 
552097a140dSpatrick // Can we safely move 'From' to just before 'To'? To satisfy this, 'From' must
553097a140dSpatrick // not define a register that is used by any instructions, after and including,
554097a140dSpatrick // 'To'. These instructions also must not redefine any of Froms operands.
555097a140dSpatrick template<typename Iterator>
isSafeToMove(MachineInstr * From,MachineInstr * To) const556097a140dSpatrick bool ReachingDefAnalysis::isSafeToMove(MachineInstr *From,
557097a140dSpatrick                                        MachineInstr *To) const {
55873471bf0Spatrick   if (From->getParent() != To->getParent() || From == To)
559097a140dSpatrick     return false;
560097a140dSpatrick 
561097a140dSpatrick   SmallSet<int, 2> Defs;
562097a140dSpatrick   // First check that From would compute the same value if moved.
563097a140dSpatrick   for (auto &MO : From->operands()) {
564097a140dSpatrick     if (!isValidReg(MO))
565097a140dSpatrick       continue;
566097a140dSpatrick     if (MO.isDef())
567097a140dSpatrick       Defs.insert(MO.getReg());
568097a140dSpatrick     else if (!hasSameReachingDef(From, To, MO.getReg()))
569097a140dSpatrick       return false;
570097a140dSpatrick   }
571097a140dSpatrick 
572097a140dSpatrick   // Now walk checking that the rest of the instructions will compute the same
573097a140dSpatrick   // value and that we're not overwriting anything. Don't move the instruction
574097a140dSpatrick   // past any memory, control-flow or other ambiguous instructions.
575097a140dSpatrick   for (auto I = ++Iterator(From), E = Iterator(To); I != E; ++I) {
576097a140dSpatrick     if (mayHaveSideEffects(*I))
577097a140dSpatrick       return false;
57809467b48Spatrick     for (auto &MO : I->operands())
579097a140dSpatrick       if (MO.isReg() && MO.getReg() && Defs.count(MO.getReg()))
580097a140dSpatrick         return false;
581097a140dSpatrick   }
582097a140dSpatrick   return true;
58309467b48Spatrick }
58409467b48Spatrick 
isSafeToMoveForwards(MachineInstr * From,MachineInstr * To) const585097a140dSpatrick bool ReachingDefAnalysis::isSafeToMoveForwards(MachineInstr *From,
586097a140dSpatrick                                                MachineInstr *To) const {
58773471bf0Spatrick   using Iterator = MachineBasicBlock::iterator;
58873471bf0Spatrick   // Walk forwards until we find the instruction.
58973471bf0Spatrick   for (auto I = Iterator(From), E = From->getParent()->end(); I != E; ++I)
59073471bf0Spatrick     if (&*I == To)
59173471bf0Spatrick       return isSafeToMove<Iterator>(From, To);
59273471bf0Spatrick   return false;
59309467b48Spatrick }
594097a140dSpatrick 
isSafeToMoveBackwards(MachineInstr * From,MachineInstr * To) const595097a140dSpatrick bool ReachingDefAnalysis::isSafeToMoveBackwards(MachineInstr *From,
596097a140dSpatrick                                                 MachineInstr *To) const {
59773471bf0Spatrick   using Iterator = MachineBasicBlock::reverse_iterator;
59873471bf0Spatrick   // Walk backwards until we find the instruction.
59973471bf0Spatrick   for (auto I = Iterator(From), E = From->getParent()->rend(); I != E; ++I)
60073471bf0Spatrick     if (&*I == To)
60173471bf0Spatrick       return isSafeToMove<Iterator>(From, To);
60273471bf0Spatrick   return false;
603097a140dSpatrick }
604097a140dSpatrick 
isSafeToRemove(MachineInstr * MI,InstSet & ToRemove) const605097a140dSpatrick bool ReachingDefAnalysis::isSafeToRemove(MachineInstr *MI,
606097a140dSpatrick                                          InstSet &ToRemove) const {
607097a140dSpatrick   SmallPtrSet<MachineInstr*, 1> Ignore;
608097a140dSpatrick   SmallPtrSet<MachineInstr*, 2> Visited;
609097a140dSpatrick   return isSafeToRemove(MI, Visited, ToRemove, Ignore);
610097a140dSpatrick }
611097a140dSpatrick 
612097a140dSpatrick bool
isSafeToRemove(MachineInstr * MI,InstSet & ToRemove,InstSet & Ignore) const613097a140dSpatrick ReachingDefAnalysis::isSafeToRemove(MachineInstr *MI, InstSet &ToRemove,
614097a140dSpatrick                                     InstSet &Ignore) const {
615097a140dSpatrick   SmallPtrSet<MachineInstr*, 2> Visited;
616097a140dSpatrick   return isSafeToRemove(MI, Visited, ToRemove, Ignore);
617097a140dSpatrick }
618097a140dSpatrick 
619097a140dSpatrick bool
isSafeToRemove(MachineInstr * MI,InstSet & Visited,InstSet & ToRemove,InstSet & Ignore) const620097a140dSpatrick ReachingDefAnalysis::isSafeToRemove(MachineInstr *MI, InstSet &Visited,
621097a140dSpatrick                                     InstSet &ToRemove, InstSet &Ignore) const {
622097a140dSpatrick   if (Visited.count(MI) || Ignore.count(MI))
623097a140dSpatrick     return true;
624097a140dSpatrick   else if (mayHaveSideEffects(*MI)) {
625097a140dSpatrick     // Unless told to ignore the instruction, don't remove anything which has
626097a140dSpatrick     // side effects.
627097a140dSpatrick     return false;
628097a140dSpatrick   }
629097a140dSpatrick 
630097a140dSpatrick   Visited.insert(MI);
631097a140dSpatrick   for (auto &MO : MI->operands()) {
632097a140dSpatrick     if (!isValidRegDef(MO))
633097a140dSpatrick       continue;
634097a140dSpatrick 
635097a140dSpatrick     SmallPtrSet<MachineInstr*, 4> Uses;
636097a140dSpatrick     getGlobalUses(MI, MO.getReg(), Uses);
637097a140dSpatrick 
638*d415bd75Srobert     for (auto *I : Uses) {
639097a140dSpatrick       if (Ignore.count(I) || ToRemove.count(I))
640097a140dSpatrick         continue;
641097a140dSpatrick       if (!isSafeToRemove(I, Visited, ToRemove, Ignore))
642097a140dSpatrick         return false;
643097a140dSpatrick     }
644097a140dSpatrick   }
645097a140dSpatrick   ToRemove.insert(MI);
646097a140dSpatrick   return true;
647097a140dSpatrick }
648097a140dSpatrick 
collectKilledOperands(MachineInstr * MI,InstSet & Dead) const649097a140dSpatrick void ReachingDefAnalysis::collectKilledOperands(MachineInstr *MI,
650097a140dSpatrick                                                 InstSet &Dead) const {
651097a140dSpatrick   Dead.insert(MI);
65273471bf0Spatrick   auto IsDead = [this, &Dead](MachineInstr *Def, MCRegister PhysReg) {
65373471bf0Spatrick     if (mayHaveSideEffects(*Def))
65473471bf0Spatrick       return false;
65573471bf0Spatrick 
656097a140dSpatrick     unsigned LiveDefs = 0;
657097a140dSpatrick     for (auto &MO : Def->operands()) {
658097a140dSpatrick       if (!isValidRegDef(MO))
659097a140dSpatrick         continue;
660097a140dSpatrick       if (!MO.isDead())
661097a140dSpatrick         ++LiveDefs;
662097a140dSpatrick     }
663097a140dSpatrick 
664097a140dSpatrick     if (LiveDefs > 1)
665097a140dSpatrick       return false;
666097a140dSpatrick 
667097a140dSpatrick     SmallPtrSet<MachineInstr*, 4> Uses;
668097a140dSpatrick     getGlobalUses(Def, PhysReg, Uses);
66973471bf0Spatrick     return llvm::set_is_subset(Uses, Dead);
670097a140dSpatrick   };
671097a140dSpatrick 
672097a140dSpatrick   for (auto &MO : MI->operands()) {
673097a140dSpatrick     if (!isValidRegUse(MO))
674097a140dSpatrick       continue;
675097a140dSpatrick     if (MachineInstr *Def = getMIOperand(MI, MO))
676097a140dSpatrick       if (IsDead(Def, MO.getReg()))
677097a140dSpatrick         collectKilledOperands(Def, Dead);
678097a140dSpatrick   }
679097a140dSpatrick }
680097a140dSpatrick 
isSafeToDefRegAt(MachineInstr * MI,MCRegister PhysReg) const681097a140dSpatrick bool ReachingDefAnalysis::isSafeToDefRegAt(MachineInstr *MI,
68273471bf0Spatrick                                            MCRegister PhysReg) const {
683097a140dSpatrick   SmallPtrSet<MachineInstr*, 1> Ignore;
684097a140dSpatrick   return isSafeToDefRegAt(MI, PhysReg, Ignore);
685097a140dSpatrick }
686097a140dSpatrick 
isSafeToDefRegAt(MachineInstr * MI,MCRegister PhysReg,InstSet & Ignore) const68773471bf0Spatrick bool ReachingDefAnalysis::isSafeToDefRegAt(MachineInstr *MI, MCRegister PhysReg,
688097a140dSpatrick                                            InstSet &Ignore) const {
689097a140dSpatrick   // Check for any uses of the register after MI.
690097a140dSpatrick   if (isRegUsedAfter(MI, PhysReg)) {
691097a140dSpatrick     if (auto *Def = getReachingLocalMIDef(MI, PhysReg)) {
692097a140dSpatrick       SmallPtrSet<MachineInstr*, 2> Uses;
69373471bf0Spatrick       getGlobalUses(Def, PhysReg, Uses);
69473471bf0Spatrick       if (!llvm::set_is_subset(Uses, Ignore))
695097a140dSpatrick         return false;
696097a140dSpatrick     } else
697097a140dSpatrick       return false;
698097a140dSpatrick   }
699097a140dSpatrick 
700097a140dSpatrick   MachineBasicBlock *MBB = MI->getParent();
701097a140dSpatrick   // Check for any defs after MI.
702097a140dSpatrick   if (isRegDefinedAfter(MI, PhysReg)) {
703097a140dSpatrick     auto I = MachineBasicBlock::iterator(MI);
704097a140dSpatrick     for (auto E = MBB->end(); I != E; ++I) {
705097a140dSpatrick       if (Ignore.count(&*I))
706097a140dSpatrick         continue;
707097a140dSpatrick       for (auto &MO : I->operands())
708*d415bd75Srobert         if (isValidRegDefOf(MO, PhysReg, TRI))
709097a140dSpatrick           return false;
710097a140dSpatrick     }
711097a140dSpatrick   }
712097a140dSpatrick   return true;
71309467b48Spatrick }
714