12cab237bSDimitry Andric //===- LiveDebugValues.cpp - Tracking Debug Value MIs ---------------------===// 27d523365SDimitry Andric // 37d523365SDimitry Andric // The LLVM Compiler Infrastructure 47d523365SDimitry Andric // 57d523365SDimitry Andric // This file is distributed under the University of Illinois Open Source 67d523365SDimitry Andric // License. See LICENSE.TXT for details. 77d523365SDimitry Andric // 87d523365SDimitry Andric //===----------------------------------------------------------------------===// 97d523365SDimitry Andric /// 107d523365SDimitry Andric /// This pass implements a data flow analysis that propagates debug location 117d523365SDimitry Andric /// information by inserting additional DBG_VALUE instructions into the machine 127d523365SDimitry Andric /// instruction stream. The pass internally builds debug location liveness 137d523365SDimitry Andric /// ranges to determine the points where additional DBG_VALUEs need to be 147d523365SDimitry Andric /// inserted. 157d523365SDimitry Andric /// 167d523365SDimitry Andric /// This is a separate pass from DbgValueHistoryCalculator to facilitate 177d523365SDimitry Andric /// testing and improve modularity. 187d523365SDimitry Andric /// 197d523365SDimitry Andric //===----------------------------------------------------------------------===// 207d523365SDimitry Andric 212cab237bSDimitry Andric #include "llvm/ADT/DenseMap.h" 22444ed5c5SDimitry Andric #include "llvm/ADT/PostOrderIterator.h" 23444ed5c5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 242cab237bSDimitry Andric #include "llvm/ADT/SmallVector.h" 253ca95b02SDimitry Andric #include "llvm/ADT/SparseBitVector.h" 263ca95b02SDimitry Andric #include "llvm/ADT/Statistic.h" 273ca95b02SDimitry Andric #include "llvm/ADT/UniqueVector.h" 28d88c1a5aSDimitry Andric #include "llvm/CodeGen/LexicalScopes.h" 292cab237bSDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 307a7e6055SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 317d523365SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 327d523365SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 332cab237bSDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 347d523365SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 357a7e6055SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h" 362cab237bSDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 372cab237bSDimitry Andric #include "llvm/CodeGen/PseudoSourceValue.h" 382cab237bSDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 392cab237bSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 402cab237bSDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 412cab237bSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 422cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 432cab237bSDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 442cab237bSDimitry Andric #include "llvm/IR/DebugLoc.h" 452cab237bSDimitry Andric #include "llvm/IR/Function.h" 462cab237bSDimitry Andric #include "llvm/IR/Module.h" 472cab237bSDimitry Andric #include "llvm/MC/MCRegisterInfo.h" 482cab237bSDimitry Andric #include "llvm/Pass.h" 492cab237bSDimitry Andric #include "llvm/Support/Casting.h" 502cab237bSDimitry Andric #include "llvm/Support/Compiler.h" 517d523365SDimitry Andric #include "llvm/Support/Debug.h" 527d523365SDimitry Andric #include "llvm/Support/raw_ostream.h" 532cab237bSDimitry Andric #include <algorithm> 542cab237bSDimitry Andric #include <cassert> 552cab237bSDimitry Andric #include <cstdint> 562cab237bSDimitry Andric #include <functional> 573ca95b02SDimitry Andric #include <queue> 582cab237bSDimitry Andric #include <utility> 592cab237bSDimitry Andric #include <vector> 607d523365SDimitry Andric 617d523365SDimitry Andric using namespace llvm; 627d523365SDimitry Andric 63302affcbSDimitry Andric #define DEBUG_TYPE "livedebugvalues" 647d523365SDimitry Andric 657d523365SDimitry Andric STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted"); 667d523365SDimitry Andric 673ca95b02SDimitry Andric // \brief If @MI is a DBG_VALUE with debug value described by a defined 683ca95b02SDimitry Andric // register, returns the number of this register. In the other case, returns 0. 693ca95b02SDimitry Andric static unsigned isDbgValueDescribedByReg(const MachineInstr &MI) { 703ca95b02SDimitry Andric assert(MI.isDebugValue() && "expected a DBG_VALUE"); 713ca95b02SDimitry Andric assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE"); 723ca95b02SDimitry Andric // If location of variable is described using a register (directly 733ca95b02SDimitry Andric // or indirectly), this register is always a first operand. 743ca95b02SDimitry Andric return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0; 753ca95b02SDimitry Andric } 763ca95b02SDimitry Andric 772cab237bSDimitry Andric namespace { 787d523365SDimitry Andric 792cab237bSDimitry Andric class LiveDebugValues : public MachineFunctionPass { 807d523365SDimitry Andric private: 817d523365SDimitry Andric const TargetRegisterInfo *TRI; 827d523365SDimitry Andric const TargetInstrInfo *TII; 837a7e6055SDimitry Andric const TargetFrameLowering *TFI; 84d88c1a5aSDimitry Andric LexicalScopes LS; 85d88c1a5aSDimitry Andric 86d88c1a5aSDimitry Andric /// Keeps track of lexical scopes associated with a user value's source 87d88c1a5aSDimitry Andric /// location. 88d88c1a5aSDimitry Andric class UserValueScopes { 89d88c1a5aSDimitry Andric DebugLoc DL; 90d88c1a5aSDimitry Andric LexicalScopes &LS; 91d88c1a5aSDimitry Andric SmallPtrSet<const MachineBasicBlock *, 4> LBlocks; 92d88c1a5aSDimitry Andric 93d88c1a5aSDimitry Andric public: 94d88c1a5aSDimitry Andric UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(std::move(D)), LS(L) {} 95d88c1a5aSDimitry Andric 96d88c1a5aSDimitry Andric /// Return true if current scope dominates at least one machine 97d88c1a5aSDimitry Andric /// instruction in a given machine basic block. 98d88c1a5aSDimitry Andric bool dominates(MachineBasicBlock *MBB) { 99d88c1a5aSDimitry Andric if (LBlocks.empty()) 100d88c1a5aSDimitry Andric LS.getMachineBasicBlocks(DL, LBlocks); 101d88c1a5aSDimitry Andric return LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB); 102d88c1a5aSDimitry Andric } 103d88c1a5aSDimitry Andric }; 1047d523365SDimitry Andric 1053ca95b02SDimitry Andric /// Based on std::pair so it can be used as an index into a DenseMap. 1062cab237bSDimitry Andric using DebugVariableBase = 1072cab237bSDimitry Andric std::pair<const DILocalVariable *, const DILocation *>; 1087d523365SDimitry Andric /// A potentially inlined instance of a variable. 1093ca95b02SDimitry Andric struct DebugVariable : public DebugVariableBase { 1103ca95b02SDimitry Andric DebugVariable(const DILocalVariable *Var, const DILocation *InlinedAt) 1113ca95b02SDimitry Andric : DebugVariableBase(Var, InlinedAt) {} 1127d523365SDimitry Andric 1132cab237bSDimitry Andric const DILocalVariable *getVar() const { return this->first; } 1142cab237bSDimitry Andric const DILocation *getInlinedAt() const { return this->second; } 1157d523365SDimitry Andric 1163ca95b02SDimitry Andric bool operator<(const DebugVariable &DV) const { 1173ca95b02SDimitry Andric if (getVar() == DV.getVar()) 1183ca95b02SDimitry Andric return getInlinedAt() < DV.getInlinedAt(); 1193ca95b02SDimitry Andric return getVar() < DV.getVar(); 1207d523365SDimitry Andric } 1217d523365SDimitry Andric }; 1227d523365SDimitry Andric 1233ca95b02SDimitry Andric /// A pair of debug variable and value location. 1247d523365SDimitry Andric struct VarLoc { 1253ca95b02SDimitry Andric const DebugVariable Var; 1263ca95b02SDimitry Andric const MachineInstr &MI; ///< Only used for cloning a new DBG_VALUE. 127d88c1a5aSDimitry Andric mutable UserValueScopes UVS; 1282cab237bSDimitry Andric enum { InvalidKind = 0, RegisterKind } Kind = InvalidKind; 1297d523365SDimitry Andric 1303ca95b02SDimitry Andric /// The value location. Stored separately to avoid repeatedly 1313ca95b02SDimitry Andric /// extracting it from MI. 1323ca95b02SDimitry Andric union { 1332cab237bSDimitry Andric uint64_t RegNo; 1343ca95b02SDimitry Andric uint64_t Hash; 1353ca95b02SDimitry Andric } Loc; 1363ca95b02SDimitry Andric 137d88c1a5aSDimitry Andric VarLoc(const MachineInstr &MI, LexicalScopes &LS) 1383ca95b02SDimitry Andric : Var(MI.getDebugVariable(), MI.getDebugLoc()->getInlinedAt()), MI(MI), 1392cab237bSDimitry Andric UVS(MI.getDebugLoc(), LS) { 1403ca95b02SDimitry Andric static_assert((sizeof(Loc) == sizeof(uint64_t)), 1413ca95b02SDimitry Andric "hash does not cover all members of Loc"); 1423ca95b02SDimitry Andric assert(MI.isDebugValue() && "not a DBG_VALUE"); 1433ca95b02SDimitry Andric assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE"); 1443ca95b02SDimitry Andric if (int RegNo = isDbgValueDescribedByReg(MI)) { 1453ca95b02SDimitry Andric Kind = RegisterKind; 1462cab237bSDimitry Andric Loc.RegNo = RegNo; 1473ca95b02SDimitry Andric } 1483ca95b02SDimitry Andric } 1493ca95b02SDimitry Andric 1503ca95b02SDimitry Andric /// If this variable is described by a register, return it, 1513ca95b02SDimitry Andric /// otherwise return 0. 1523ca95b02SDimitry Andric unsigned isDescribedByReg() const { 1533ca95b02SDimitry Andric if (Kind == RegisterKind) 1542cab237bSDimitry Andric return Loc.RegNo; 1553ca95b02SDimitry Andric return 0; 1563ca95b02SDimitry Andric } 1573ca95b02SDimitry Andric 158d88c1a5aSDimitry Andric /// Determine whether the lexical scope of this value's debug location 159d88c1a5aSDimitry Andric /// dominates MBB. 160d88c1a5aSDimitry Andric bool dominates(MachineBasicBlock &MBB) const { return UVS.dominates(&MBB); } 161d88c1a5aSDimitry Andric 1627a7e6055SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1637a7e6055SDimitry Andric LLVM_DUMP_METHOD void dump() const { MI.dump(); } 1647a7e6055SDimitry Andric #endif 1653ca95b02SDimitry Andric 1663ca95b02SDimitry Andric bool operator==(const VarLoc &Other) const { 1673ca95b02SDimitry Andric return Var == Other.Var && Loc.Hash == Other.Loc.Hash; 1683ca95b02SDimitry Andric } 1693ca95b02SDimitry Andric 1703ca95b02SDimitry Andric /// This operator guarantees that VarLocs are sorted by Variable first. 1713ca95b02SDimitry Andric bool operator<(const VarLoc &Other) const { 1723ca95b02SDimitry Andric if (Var == Other.Var) 1733ca95b02SDimitry Andric return Loc.Hash < Other.Loc.Hash; 1743ca95b02SDimitry Andric return Var < Other.Var; 1753ca95b02SDimitry Andric } 1767d523365SDimitry Andric }; 1777d523365SDimitry Andric 1782cab237bSDimitry Andric using VarLocMap = UniqueVector<VarLoc>; 1792cab237bSDimitry Andric using VarLocSet = SparseBitVector<>; 1802cab237bSDimitry Andric using VarLocInMBB = SmallDenseMap<const MachineBasicBlock *, VarLocSet>; 1817a7e6055SDimitry Andric struct SpillDebugPair { 1827a7e6055SDimitry Andric MachineInstr *SpillInst; 1837a7e6055SDimitry Andric MachineInstr *DebugInst; 1847a7e6055SDimitry Andric }; 1852cab237bSDimitry Andric using SpillMap = SmallVector<SpillDebugPair, 4>; 1867d523365SDimitry Andric 1873ca95b02SDimitry Andric /// This holds the working set of currently open ranges. For fast 1883ca95b02SDimitry Andric /// access, this is done both as a set of VarLocIDs, and a map of 1893ca95b02SDimitry Andric /// DebugVariable to recent VarLocID. Note that a DBG_VALUE ends all 1903ca95b02SDimitry Andric /// previous open ranges for the same variable. 1913ca95b02SDimitry Andric class OpenRangesSet { 1923ca95b02SDimitry Andric VarLocSet VarLocs; 1933ca95b02SDimitry Andric SmallDenseMap<DebugVariableBase, unsigned, 8> Vars; 1947d523365SDimitry Andric 1953ca95b02SDimitry Andric public: 1963ca95b02SDimitry Andric const VarLocSet &getVarLocs() const { return VarLocs; } 1973ca95b02SDimitry Andric 1983ca95b02SDimitry Andric /// Terminate all open ranges for Var by removing it from the set. 1993ca95b02SDimitry Andric void erase(DebugVariable Var) { 2003ca95b02SDimitry Andric auto It = Vars.find(Var); 2013ca95b02SDimitry Andric if (It != Vars.end()) { 2023ca95b02SDimitry Andric unsigned ID = It->second; 2033ca95b02SDimitry Andric VarLocs.reset(ID); 2043ca95b02SDimitry Andric Vars.erase(It); 2053ca95b02SDimitry Andric } 2063ca95b02SDimitry Andric } 2073ca95b02SDimitry Andric 2083ca95b02SDimitry Andric /// Terminate all open ranges listed in \c KillSet by removing 2093ca95b02SDimitry Andric /// them from the set. 2103ca95b02SDimitry Andric void erase(const VarLocSet &KillSet, const VarLocMap &VarLocIDs) { 2113ca95b02SDimitry Andric VarLocs.intersectWithComplement(KillSet); 2123ca95b02SDimitry Andric for (unsigned ID : KillSet) 2133ca95b02SDimitry Andric Vars.erase(VarLocIDs[ID].Var); 2143ca95b02SDimitry Andric } 2153ca95b02SDimitry Andric 2163ca95b02SDimitry Andric /// Insert a new range into the set. 2173ca95b02SDimitry Andric void insert(unsigned VarLocID, DebugVariableBase Var) { 2183ca95b02SDimitry Andric VarLocs.set(VarLocID); 2193ca95b02SDimitry Andric Vars.insert({Var, VarLocID}); 2203ca95b02SDimitry Andric } 2213ca95b02SDimitry Andric 2223ca95b02SDimitry Andric /// Empty the set. 2233ca95b02SDimitry Andric void clear() { 2243ca95b02SDimitry Andric VarLocs.clear(); 2253ca95b02SDimitry Andric Vars.clear(); 2263ca95b02SDimitry Andric } 2273ca95b02SDimitry Andric 2283ca95b02SDimitry Andric /// Return whether the set is empty or not. 2293ca95b02SDimitry Andric bool empty() const { 2303ca95b02SDimitry Andric assert(Vars.empty() == VarLocs.empty() && "open ranges are inconsistent"); 2313ca95b02SDimitry Andric return VarLocs.empty(); 2323ca95b02SDimitry Andric } 2333ca95b02SDimitry Andric }; 2343ca95b02SDimitry Andric 2357a7e6055SDimitry Andric bool isSpillInstruction(const MachineInstr &MI, MachineFunction *MF, 2367a7e6055SDimitry Andric unsigned &Reg); 2377a7e6055SDimitry Andric int extractSpillBaseRegAndOffset(const MachineInstr &MI, unsigned &Reg); 2387a7e6055SDimitry Andric 2393ca95b02SDimitry Andric void transferDebugValue(const MachineInstr &MI, OpenRangesSet &OpenRanges, 2403ca95b02SDimitry Andric VarLocMap &VarLocIDs); 2417a7e6055SDimitry Andric void transferSpillInst(MachineInstr &MI, OpenRangesSet &OpenRanges, 2427a7e6055SDimitry Andric VarLocMap &VarLocIDs, SpillMap &Spills); 2433ca95b02SDimitry Andric void transferRegisterDef(MachineInstr &MI, OpenRangesSet &OpenRanges, 2443ca95b02SDimitry Andric const VarLocMap &VarLocIDs); 2453ca95b02SDimitry Andric bool transferTerminatorInst(MachineInstr &MI, OpenRangesSet &OpenRanges, 2463ca95b02SDimitry Andric VarLocInMBB &OutLocs, const VarLocMap &VarLocIDs); 2473ca95b02SDimitry Andric bool transfer(MachineInstr &MI, OpenRangesSet &OpenRanges, 2487a7e6055SDimitry Andric VarLocInMBB &OutLocs, VarLocMap &VarLocIDs, SpillMap &Spills, 2497a7e6055SDimitry Andric bool transferSpills); 2503ca95b02SDimitry Andric 2513ca95b02SDimitry Andric bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs, 252d88c1a5aSDimitry Andric const VarLocMap &VarLocIDs, 253d88c1a5aSDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> &Visited); 2547d523365SDimitry Andric 2557d523365SDimitry Andric bool ExtendRanges(MachineFunction &MF); 2567d523365SDimitry Andric 2577d523365SDimitry Andric public: 2587d523365SDimitry Andric static char ID; 2597d523365SDimitry Andric 2607d523365SDimitry Andric /// Default construct and initialize the pass. 2617d523365SDimitry Andric LiveDebugValues(); 2627d523365SDimitry Andric 2637d523365SDimitry Andric /// Tell the pass manager which passes we depend on and what 2647d523365SDimitry Andric /// information we preserve. 2657d523365SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override; 2667d523365SDimitry Andric 2673ca95b02SDimitry Andric MachineFunctionProperties getRequiredProperties() const override { 2683ca95b02SDimitry Andric return MachineFunctionProperties().set( 269d88c1a5aSDimitry Andric MachineFunctionProperties::Property::NoVRegs); 2703ca95b02SDimitry Andric } 2713ca95b02SDimitry Andric 2727d523365SDimitry Andric /// Print to ostream with a message. 2733ca95b02SDimitry Andric void printVarLocInMBB(const MachineFunction &MF, const VarLocInMBB &V, 2743ca95b02SDimitry Andric const VarLocMap &VarLocIDs, const char *msg, 2757d523365SDimitry Andric raw_ostream &Out) const; 2767d523365SDimitry Andric 2777d523365SDimitry Andric /// Calculate the liveness information for the given machine function. 2787d523365SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 2797d523365SDimitry Andric }; 280d88c1a5aSDimitry Andric 2812cab237bSDimitry Andric } // end anonymous namespace 2827d523365SDimitry Andric 2837d523365SDimitry Andric //===----------------------------------------------------------------------===// 2847d523365SDimitry Andric // Implementation 2857d523365SDimitry Andric //===----------------------------------------------------------------------===// 2867d523365SDimitry Andric 2877d523365SDimitry Andric char LiveDebugValues::ID = 0; 2882cab237bSDimitry Andric 2897d523365SDimitry Andric char &llvm::LiveDebugValuesID = LiveDebugValues::ID; 2902cab237bSDimitry Andric 291302affcbSDimitry Andric INITIALIZE_PASS(LiveDebugValues, DEBUG_TYPE, "Live DEBUG_VALUE analysis", 2927d523365SDimitry Andric false, false) 2937d523365SDimitry Andric 2947d523365SDimitry Andric /// Default construct and initialize the pass. 2957d523365SDimitry Andric LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) { 2967d523365SDimitry Andric initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry()); 2977d523365SDimitry Andric } 2987d523365SDimitry Andric 2997d523365SDimitry Andric /// Tell the pass manager which passes we depend on and what information we 3007d523365SDimitry Andric /// preserve. 3017d523365SDimitry Andric void LiveDebugValues::getAnalysisUsage(AnalysisUsage &AU) const { 3023ca95b02SDimitry Andric AU.setPreservesCFG(); 3037d523365SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 3047d523365SDimitry Andric } 3057d523365SDimitry Andric 3067d523365SDimitry Andric //===----------------------------------------------------------------------===// 3077d523365SDimitry Andric // Debug Range Extension Implementation 3087d523365SDimitry Andric //===----------------------------------------------------------------------===// 3097d523365SDimitry Andric 3107a7e6055SDimitry Andric #ifndef NDEBUG 3113ca95b02SDimitry Andric void LiveDebugValues::printVarLocInMBB(const MachineFunction &MF, 3123ca95b02SDimitry Andric const VarLocInMBB &V, 3133ca95b02SDimitry Andric const VarLocMap &VarLocIDs, 3143ca95b02SDimitry Andric const char *msg, 3157d523365SDimitry Andric raw_ostream &Out) const { 316d88c1a5aSDimitry Andric Out << '\n' << msg << '\n'; 3173ca95b02SDimitry Andric for (const MachineBasicBlock &BB : MF) { 3183ca95b02SDimitry Andric const auto &L = V.lookup(&BB); 3193ca95b02SDimitry Andric Out << "MBB: " << BB.getName() << ":\n"; 3203ca95b02SDimitry Andric for (unsigned VLL : L) { 3213ca95b02SDimitry Andric const VarLoc &VL = VarLocIDs[VLL]; 3223ca95b02SDimitry Andric Out << " Var: " << VL.Var.getVar()->getName(); 3237d523365SDimitry Andric Out << " MI: "; 3243ca95b02SDimitry Andric VL.dump(); 3257d523365SDimitry Andric } 3267d523365SDimitry Andric } 3277d523365SDimitry Andric Out << "\n"; 3287d523365SDimitry Andric } 3297a7e6055SDimitry Andric #endif 3307a7e6055SDimitry Andric 3317a7e6055SDimitry Andric /// Given a spill instruction, extract the register and offset used to 3327a7e6055SDimitry Andric /// address the spill location in a target independent way. 3337a7e6055SDimitry Andric int LiveDebugValues::extractSpillBaseRegAndOffset(const MachineInstr &MI, 3347a7e6055SDimitry Andric unsigned &Reg) { 3357a7e6055SDimitry Andric assert(MI.hasOneMemOperand() && 3367a7e6055SDimitry Andric "Spill instruction does not have exactly one memory operand?"); 3377a7e6055SDimitry Andric auto MMOI = MI.memoperands_begin(); 3387a7e6055SDimitry Andric const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue(); 3397a7e6055SDimitry Andric assert(PVal->kind() == PseudoSourceValue::FixedStack && 3407a7e6055SDimitry Andric "Inconsistent memory operand in spill instruction"); 3417a7e6055SDimitry Andric int FI = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex(); 3427a7e6055SDimitry Andric const MachineBasicBlock *MBB = MI.getParent(); 3437a7e6055SDimitry Andric return TFI->getFrameIndexReference(*MBB->getParent(), FI, Reg); 3447a7e6055SDimitry Andric } 3457d523365SDimitry Andric 3467d523365SDimitry Andric /// End all previous ranges related to @MI and start a new range from @MI 3477d523365SDimitry Andric /// if it is a DBG_VALUE instr. 3483ca95b02SDimitry Andric void LiveDebugValues::transferDebugValue(const MachineInstr &MI, 3493ca95b02SDimitry Andric OpenRangesSet &OpenRanges, 3503ca95b02SDimitry Andric VarLocMap &VarLocIDs) { 3517d523365SDimitry Andric if (!MI.isDebugValue()) 3527d523365SDimitry Andric return; 3533ca95b02SDimitry Andric const DILocalVariable *Var = MI.getDebugVariable(); 3543ca95b02SDimitry Andric const DILocation *DebugLoc = MI.getDebugLoc(); 3553ca95b02SDimitry Andric const DILocation *InlinedAt = DebugLoc->getInlinedAt(); 3563ca95b02SDimitry Andric assert(Var->isValidLocationForIntrinsic(DebugLoc) && 3577d523365SDimitry Andric "Expected inlined-at fields to agree"); 3587d523365SDimitry Andric 3597d523365SDimitry Andric // End all previous ranges of Var. 3603ca95b02SDimitry Andric DebugVariable V(Var, InlinedAt); 3613ca95b02SDimitry Andric OpenRanges.erase(V); 3627d523365SDimitry Andric 3633ca95b02SDimitry Andric // Add the VarLoc to OpenRanges from this DBG_VALUE. 3647d523365SDimitry Andric // TODO: Currently handles DBG_VALUE which has only reg as location. 3653ca95b02SDimitry Andric if (isDbgValueDescribedByReg(MI)) { 366d88c1a5aSDimitry Andric VarLoc VL(MI, LS); 3673ca95b02SDimitry Andric unsigned ID = VarLocIDs.insert(VL); 3683ca95b02SDimitry Andric OpenRanges.insert(ID, VL.Var); 3697d523365SDimitry Andric } 3707d523365SDimitry Andric } 3717d523365SDimitry Andric 3727d523365SDimitry Andric /// A definition of a register may mark the end of a range. 3737d523365SDimitry Andric void LiveDebugValues::transferRegisterDef(MachineInstr &MI, 3743ca95b02SDimitry Andric OpenRangesSet &OpenRanges, 3753ca95b02SDimitry Andric const VarLocMap &VarLocIDs) { 3762cab237bSDimitry Andric MachineFunction *MF = MI.getMF(); 3773ca95b02SDimitry Andric const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); 3783ca95b02SDimitry Andric unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); 3793ca95b02SDimitry Andric SparseBitVector<> KillSet; 3807d523365SDimitry Andric for (const MachineOperand &MO : MI.operands()) { 3817a7e6055SDimitry Andric // Determine whether the operand is a register def. Assume that call 3827a7e6055SDimitry Andric // instructions never clobber SP, because some backends (e.g., AArch64) 3837a7e6055SDimitry Andric // never list SP in the regmask. 3843ca95b02SDimitry Andric if (MO.isReg() && MO.isDef() && MO.getReg() && 3857a7e6055SDimitry Andric TRI->isPhysicalRegister(MO.getReg()) && 3867a7e6055SDimitry Andric !(MI.isCall() && MO.getReg() == SP)) { 3877d523365SDimitry Andric // Remove ranges of all aliased registers. 3887d523365SDimitry Andric for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI) 3893ca95b02SDimitry Andric for (unsigned ID : OpenRanges.getVarLocs()) 3903ca95b02SDimitry Andric if (VarLocIDs[ID].isDescribedByReg() == *RAI) 3913ca95b02SDimitry Andric KillSet.set(ID); 3923ca95b02SDimitry Andric } else if (MO.isRegMask()) { 3933ca95b02SDimitry Andric // Remove ranges of all clobbered registers. Register masks don't usually 3943ca95b02SDimitry Andric // list SP as preserved. While the debug info may be off for an 3953ca95b02SDimitry Andric // instruction or two around callee-cleanup calls, transferring the 3963ca95b02SDimitry Andric // DEBUG_VALUE across the call is still a better user experience. 3973ca95b02SDimitry Andric for (unsigned ID : OpenRanges.getVarLocs()) { 3983ca95b02SDimitry Andric unsigned Reg = VarLocIDs[ID].isDescribedByReg(); 3993ca95b02SDimitry Andric if (Reg && Reg != SP && MO.clobbersPhysReg(Reg)) 4003ca95b02SDimitry Andric KillSet.set(ID); 4017d523365SDimitry Andric } 4027d523365SDimitry Andric } 4033ca95b02SDimitry Andric } 4043ca95b02SDimitry Andric OpenRanges.erase(KillSet, VarLocIDs); 4053ca95b02SDimitry Andric } 4067d523365SDimitry Andric 4077a7e6055SDimitry Andric /// Decide if @MI is a spill instruction and return true if it is. We use 2 4087a7e6055SDimitry Andric /// criteria to make this decision: 4097a7e6055SDimitry Andric /// - Is this instruction a store to a spill slot? 4107a7e6055SDimitry Andric /// - Is there a register operand that is both used and killed? 4117a7e6055SDimitry Andric /// TODO: Store optimization can fold spills into other stores (including 4127a7e6055SDimitry Andric /// other spills). We do not handle this yet (more than one memory operand). 4137a7e6055SDimitry Andric bool LiveDebugValues::isSpillInstruction(const MachineInstr &MI, 4147a7e6055SDimitry Andric MachineFunction *MF, unsigned &Reg) { 4157a7e6055SDimitry Andric const MachineFrameInfo &FrameInfo = MF->getFrameInfo(); 4167a7e6055SDimitry Andric int FI; 4177a7e6055SDimitry Andric const MachineMemOperand *MMO; 4187a7e6055SDimitry Andric 4197a7e6055SDimitry Andric // TODO: Handle multiple stores folded into one. 4207a7e6055SDimitry Andric if (!MI.hasOneMemOperand()) 4217a7e6055SDimitry Andric return false; 4227a7e6055SDimitry Andric 4237a7e6055SDimitry Andric // To identify a spill instruction, use the same criteria as in AsmPrinter. 4247a7e6055SDimitry Andric if (!((TII->isStoreToStackSlotPostFE(MI, FI) || 4257a7e6055SDimitry Andric TII->hasStoreToStackSlot(MI, MMO, FI)) && 4267a7e6055SDimitry Andric FrameInfo.isSpillSlotObjectIndex(FI))) 4277a7e6055SDimitry Andric return false; 4287a7e6055SDimitry Andric 4297a7e6055SDimitry Andric // In a spill instruction generated by the InlineSpiller the spilled register 4307a7e6055SDimitry Andric // has its kill flag set. Return false if we don't find such a register. 4317a7e6055SDimitry Andric Reg = 0; 4327a7e6055SDimitry Andric for (const MachineOperand &MO : MI.operands()) { 4337a7e6055SDimitry Andric if (MO.isReg() && MO.isUse() && MO.isKill()) { 4347a7e6055SDimitry Andric Reg = MO.getReg(); 4357a7e6055SDimitry Andric break; 4367a7e6055SDimitry Andric } 4377a7e6055SDimitry Andric } 4387a7e6055SDimitry Andric return Reg != 0; 4397a7e6055SDimitry Andric } 4407a7e6055SDimitry Andric 4417a7e6055SDimitry Andric /// A spilled register may indicate that we have to end the current range of 4427a7e6055SDimitry Andric /// a variable and create a new one for the spill location. 4437a7e6055SDimitry Andric /// We don't want to insert any instructions in transfer(), so we just create 4447a7e6055SDimitry Andric /// the DBG_VALUE witout inserting it and keep track of it in @Spills. 4457a7e6055SDimitry Andric /// It will be inserted into the BB when we're done iterating over the 4467a7e6055SDimitry Andric /// instructions. 4477a7e6055SDimitry Andric void LiveDebugValues::transferSpillInst(MachineInstr &MI, 4487a7e6055SDimitry Andric OpenRangesSet &OpenRanges, 4497a7e6055SDimitry Andric VarLocMap &VarLocIDs, 4507a7e6055SDimitry Andric SpillMap &Spills) { 4517a7e6055SDimitry Andric unsigned Reg; 4522cab237bSDimitry Andric MachineFunction *MF = MI.getMF(); 4537a7e6055SDimitry Andric if (!isSpillInstruction(MI, MF, Reg)) 4547a7e6055SDimitry Andric return; 4557a7e6055SDimitry Andric 4567a7e6055SDimitry Andric // Check if the register is the location of a debug value. 4577a7e6055SDimitry Andric for (unsigned ID : OpenRanges.getVarLocs()) { 4587a7e6055SDimitry Andric if (VarLocIDs[ID].isDescribedByReg() == Reg) { 4592cab237bSDimitry Andric DEBUG(dbgs() << "Spilling Register " << printReg(Reg, TRI) << '(' 4607a7e6055SDimitry Andric << VarLocIDs[ID].Var.getVar()->getName() << ")\n"); 4617a7e6055SDimitry Andric 4627a7e6055SDimitry Andric // Create a DBG_VALUE instruction to describe the Var in its spilled 4637a7e6055SDimitry Andric // location, but don't insert it yet to avoid invalidating the 4647a7e6055SDimitry Andric // iterator in our caller. 4657a7e6055SDimitry Andric unsigned SpillBase; 4667a7e6055SDimitry Andric int SpillOffset = extractSpillBaseRegAndOffset(MI, SpillBase); 4677a7e6055SDimitry Andric const MachineInstr *DMI = &VarLocIDs[ID].MI; 4682cab237bSDimitry Andric auto *SpillExpr = DIExpression::prepend( 4692cab237bSDimitry Andric DMI->getDebugExpression(), DIExpression::NoDeref, SpillOffset); 4707a7e6055SDimitry Andric MachineInstr *SpDMI = 4712cab237bSDimitry Andric BuildMI(*MF, DMI->getDebugLoc(), DMI->getDesc(), true, SpillBase, 4722cab237bSDimitry Andric DMI->getDebugVariable(), SpillExpr); 4737a7e6055SDimitry Andric DEBUG(dbgs() << "Creating DBG_VALUE inst for spill: "; 4747a7e6055SDimitry Andric SpDMI->print(dbgs(), false, TII)); 4757a7e6055SDimitry Andric 4767a7e6055SDimitry Andric // The newly created DBG_VALUE instruction SpDMI must be inserted after 4777a7e6055SDimitry Andric // MI. Keep track of the pairing. 4787a7e6055SDimitry Andric SpillDebugPair MIP = {&MI, SpDMI}; 4797a7e6055SDimitry Andric Spills.push_back(MIP); 4807a7e6055SDimitry Andric 4817a7e6055SDimitry Andric // End all previous ranges of Var. 4827a7e6055SDimitry Andric OpenRanges.erase(VarLocIDs[ID].Var); 4837a7e6055SDimitry Andric 4847a7e6055SDimitry Andric // Add the VarLoc to OpenRanges. 4857a7e6055SDimitry Andric VarLoc VL(*SpDMI, LS); 4867a7e6055SDimitry Andric unsigned SpillLocID = VarLocIDs.insert(VL); 4877a7e6055SDimitry Andric OpenRanges.insert(SpillLocID, VL.Var); 4887a7e6055SDimitry Andric return; 4897a7e6055SDimitry Andric } 4907a7e6055SDimitry Andric } 4917a7e6055SDimitry Andric } 4927a7e6055SDimitry Andric 4937d523365SDimitry Andric /// Terminate all open ranges at the end of the current basic block. 494444ed5c5SDimitry Andric bool LiveDebugValues::transferTerminatorInst(MachineInstr &MI, 4953ca95b02SDimitry Andric OpenRangesSet &OpenRanges, 4963ca95b02SDimitry Andric VarLocInMBB &OutLocs, 4973ca95b02SDimitry Andric const VarLocMap &VarLocIDs) { 498444ed5c5SDimitry Andric bool Changed = false; 4997d523365SDimitry Andric const MachineBasicBlock *CurMBB = MI.getParent(); 5007d523365SDimitry Andric if (!(MI.isTerminator() || (&MI == &CurMBB->instr_back()))) 501444ed5c5SDimitry Andric return false; 5027d523365SDimitry Andric 5037d523365SDimitry Andric if (OpenRanges.empty()) 504444ed5c5SDimitry Andric return false; 5057d523365SDimitry Andric 5063ca95b02SDimitry Andric DEBUG(for (unsigned ID : OpenRanges.getVarLocs()) { 5077d523365SDimitry Andric // Copy OpenRanges to OutLocs, if not already present. 5083ca95b02SDimitry Andric dbgs() << "Add to OutLocs: "; VarLocIDs[ID].dump(); 5093ca95b02SDimitry Andric }); 5103ca95b02SDimitry Andric VarLocSet &VLS = OutLocs[CurMBB]; 5113ca95b02SDimitry Andric Changed = VLS |= OpenRanges.getVarLocs(); 5127d523365SDimitry Andric OpenRanges.clear(); 513444ed5c5SDimitry Andric return Changed; 5147d523365SDimitry Andric } 5157d523365SDimitry Andric 5167d523365SDimitry Andric /// This routine creates OpenRanges and OutLocs. 5173ca95b02SDimitry Andric bool LiveDebugValues::transfer(MachineInstr &MI, OpenRangesSet &OpenRanges, 5187a7e6055SDimitry Andric VarLocInMBB &OutLocs, VarLocMap &VarLocIDs, 5197a7e6055SDimitry Andric SpillMap &Spills, bool transferSpills) { 520444ed5c5SDimitry Andric bool Changed = false; 5213ca95b02SDimitry Andric transferDebugValue(MI, OpenRanges, VarLocIDs); 5223ca95b02SDimitry Andric transferRegisterDef(MI, OpenRanges, VarLocIDs); 5237a7e6055SDimitry Andric if (transferSpills) 5247a7e6055SDimitry Andric transferSpillInst(MI, OpenRanges, VarLocIDs, Spills); 5253ca95b02SDimitry Andric Changed = transferTerminatorInst(MI, OpenRanges, OutLocs, VarLocIDs); 526444ed5c5SDimitry Andric return Changed; 5277d523365SDimitry Andric } 5287d523365SDimitry Andric 5297d523365SDimitry Andric /// This routine joins the analysis results of all incoming edges in @MBB by 5307d523365SDimitry Andric /// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same 5317d523365SDimitry Andric /// source variable in all the predecessors of @MBB reside in the same location. 532444ed5c5SDimitry Andric bool LiveDebugValues::join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, 533d88c1a5aSDimitry Andric VarLocInMBB &InLocs, const VarLocMap &VarLocIDs, 534d88c1a5aSDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> &Visited) { 5357d523365SDimitry Andric DEBUG(dbgs() << "join MBB: " << MBB.getName() << "\n"); 536444ed5c5SDimitry Andric bool Changed = false; 5377d523365SDimitry Andric 5383ca95b02SDimitry Andric VarLocSet InLocsT; // Temporary incoming locations. 5397d523365SDimitry Andric 5403ca95b02SDimitry Andric // For all predecessors of this MBB, find the set of VarLocs that 5413ca95b02SDimitry Andric // can be joined. 542d88c1a5aSDimitry Andric int NumVisited = 0; 5437d523365SDimitry Andric for (auto p : MBB.predecessors()) { 544d88c1a5aSDimitry Andric // Ignore unvisited predecessor blocks. As we are processing 545d88c1a5aSDimitry Andric // the blocks in reverse post-order any unvisited block can 546d88c1a5aSDimitry Andric // be considered to not remove any incoming values. 547d88c1a5aSDimitry Andric if (!Visited.count(p)) 548d88c1a5aSDimitry Andric continue; 5497d523365SDimitry Andric auto OL = OutLocs.find(p); 5507d523365SDimitry Andric // Join is null in case of empty OutLocs from any of the pred. 5517d523365SDimitry Andric if (OL == OutLocs.end()) 552444ed5c5SDimitry Andric return false; 5537d523365SDimitry Andric 554d88c1a5aSDimitry Andric // Just copy over the Out locs to incoming locs for the first visited 555d88c1a5aSDimitry Andric // predecessor, and for all other predecessors join the Out locs. 556d88c1a5aSDimitry Andric if (!NumVisited) 5577d523365SDimitry Andric InLocsT = OL->second; 558d88c1a5aSDimitry Andric else 5593ca95b02SDimitry Andric InLocsT &= OL->second; 560d88c1a5aSDimitry Andric NumVisited++; 5617d523365SDimitry Andric } 5627d523365SDimitry Andric 563d88c1a5aSDimitry Andric // Filter out DBG_VALUES that are out of scope. 564d88c1a5aSDimitry Andric VarLocSet KillSet; 565d88c1a5aSDimitry Andric for (auto ID : InLocsT) 566d88c1a5aSDimitry Andric if (!VarLocIDs[ID].dominates(MBB)) 567d88c1a5aSDimitry Andric KillSet.set(ID); 568d88c1a5aSDimitry Andric InLocsT.intersectWithComplement(KillSet); 569d88c1a5aSDimitry Andric 570d88c1a5aSDimitry Andric // As we are processing blocks in reverse post-order we 571d88c1a5aSDimitry Andric // should have processed at least one predecessor, unless it 572d88c1a5aSDimitry Andric // is the entry block which has no predecessor. 573d88c1a5aSDimitry Andric assert((NumVisited || MBB.pred_empty()) && 574d88c1a5aSDimitry Andric "Should have processed at least one predecessor"); 5757d523365SDimitry Andric if (InLocsT.empty()) 576444ed5c5SDimitry Andric return false; 5777d523365SDimitry Andric 5783ca95b02SDimitry Andric VarLocSet &ILS = InLocs[&MBB]; 5797d523365SDimitry Andric 5807d523365SDimitry Andric // Insert DBG_VALUE instructions, if not already inserted. 5813ca95b02SDimitry Andric VarLocSet Diff = InLocsT; 5823ca95b02SDimitry Andric Diff.intersectWithComplement(ILS); 5833ca95b02SDimitry Andric for (auto ID : Diff) { 5847d523365SDimitry Andric // This VarLoc is not found in InLocs i.e. it is not yet inserted. So, a 5857d523365SDimitry Andric // new range is started for the var from the mbb's beginning by inserting 5867d523365SDimitry Andric // a new DBG_VALUE. transfer() will end this range however appropriate. 5873ca95b02SDimitry Andric const VarLoc &DiffIt = VarLocIDs[ID]; 5883ca95b02SDimitry Andric const MachineInstr *DMI = &DiffIt.MI; 5897d523365SDimitry Andric MachineInstr *MI = 5907d523365SDimitry Andric BuildMI(MBB, MBB.instr_begin(), DMI->getDebugLoc(), DMI->getDesc(), 5912cab237bSDimitry Andric DMI->isIndirectDebugValue(), DMI->getOperand(0).getReg(), 5927d523365SDimitry Andric DMI->getDebugVariable(), DMI->getDebugExpression()); 5937d523365SDimitry Andric if (DMI->isIndirectDebugValue()) 5947d523365SDimitry Andric MI->getOperand(1).setImm(DMI->getOperand(1).getImm()); 5957d523365SDimitry Andric DEBUG(dbgs() << "Inserted: "; MI->dump();); 5963ca95b02SDimitry Andric ILS.set(ID); 5977d523365SDimitry Andric ++NumInserted; 598444ed5c5SDimitry Andric Changed = true; 5997d523365SDimitry Andric } 600444ed5c5SDimitry Andric return Changed; 6017d523365SDimitry Andric } 6027d523365SDimitry Andric 6037d523365SDimitry Andric /// Calculate the liveness information for the given machine function and 6047d523365SDimitry Andric /// extend ranges across basic blocks. 6057d523365SDimitry Andric bool LiveDebugValues::ExtendRanges(MachineFunction &MF) { 6067d523365SDimitry Andric DEBUG(dbgs() << "\nDebug Range Extension\n"); 6077d523365SDimitry Andric 6087d523365SDimitry Andric bool Changed = false; 609444ed5c5SDimitry Andric bool OLChanged = false; 610444ed5c5SDimitry Andric bool MBBJoined = false; 6117d523365SDimitry Andric 6123ca95b02SDimitry Andric VarLocMap VarLocIDs; // Map VarLoc<>unique ID for use in bitvectors. 6133ca95b02SDimitry Andric OpenRangesSet OpenRanges; // Ranges that are open until end of bb. 6147d523365SDimitry Andric VarLocInMBB OutLocs; // Ranges that exist beyond bb. 6157d523365SDimitry Andric VarLocInMBB InLocs; // Ranges that are incoming after joining. 6167a7e6055SDimitry Andric SpillMap Spills; // DBG_VALUEs associated with spills. 6177d523365SDimitry Andric 618444ed5c5SDimitry Andric DenseMap<unsigned int, MachineBasicBlock *> OrderToBB; 619444ed5c5SDimitry Andric DenseMap<MachineBasicBlock *, unsigned int> BBToOrder; 620444ed5c5SDimitry Andric std::priority_queue<unsigned int, std::vector<unsigned int>, 6213ca95b02SDimitry Andric std::greater<unsigned int>> 6223ca95b02SDimitry Andric Worklist; 623444ed5c5SDimitry Andric std::priority_queue<unsigned int, std::vector<unsigned int>, 6243ca95b02SDimitry Andric std::greater<unsigned int>> 6253ca95b02SDimitry Andric Pending; 6263ca95b02SDimitry Andric 6277d523365SDimitry Andric // Initialize every mbb with OutLocs. 6287a7e6055SDimitry Andric // We are not looking at any spill instructions during the initial pass 6297a7e6055SDimitry Andric // over the BBs. The LiveDebugVariables pass has already created DBG_VALUE 6307a7e6055SDimitry Andric // instructions for spills of registers that are known to be user variables 6317a7e6055SDimitry Andric // within the BB in which the spill occurs. 6327d523365SDimitry Andric for (auto &MBB : MF) 6337d523365SDimitry Andric for (auto &MI : MBB) 6347a7e6055SDimitry Andric transfer(MI, OpenRanges, OutLocs, VarLocIDs, Spills, 6357a7e6055SDimitry Andric /*transferSpills=*/false); 6363ca95b02SDimitry Andric 6373ca95b02SDimitry Andric DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "OutLocs after initialization", 6383ca95b02SDimitry Andric dbgs())); 6397d523365SDimitry Andric 640444ed5c5SDimitry Andric ReversePostOrderTraversal<MachineFunction *> RPOT(&MF); 641444ed5c5SDimitry Andric unsigned int RPONumber = 0; 642444ed5c5SDimitry Andric for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) { 643444ed5c5SDimitry Andric OrderToBB[RPONumber] = *RI; 644444ed5c5SDimitry Andric BBToOrder[*RI] = RPONumber; 645444ed5c5SDimitry Andric Worklist.push(RPONumber); 646444ed5c5SDimitry Andric ++RPONumber; 647444ed5c5SDimitry Andric } 648444ed5c5SDimitry Andric // This is a standard "union of predecessor outs" dataflow problem. 649444ed5c5SDimitry Andric // To solve it, we perform join() and transfer() using the two worklist method 650444ed5c5SDimitry Andric // until the ranges converge. 651444ed5c5SDimitry Andric // Ranges have converged when both worklists are empty. 652d88c1a5aSDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> Visited; 653444ed5c5SDimitry Andric while (!Worklist.empty() || !Pending.empty()) { 654444ed5c5SDimitry Andric // We track what is on the pending worklist to avoid inserting the same 655444ed5c5SDimitry Andric // thing twice. We could avoid this with a custom priority queue, but this 656444ed5c5SDimitry Andric // is probably not worth it. 657444ed5c5SDimitry Andric SmallPtrSet<MachineBasicBlock *, 16> OnPending; 658d88c1a5aSDimitry Andric DEBUG(dbgs() << "Processing Worklist\n"); 659444ed5c5SDimitry Andric while (!Worklist.empty()) { 660444ed5c5SDimitry Andric MachineBasicBlock *MBB = OrderToBB[Worklist.top()]; 661444ed5c5SDimitry Andric Worklist.pop(); 662d88c1a5aSDimitry Andric MBBJoined = join(*MBB, OutLocs, InLocs, VarLocIDs, Visited); 663d88c1a5aSDimitry Andric Visited.insert(MBB); 6647d523365SDimitry Andric if (MBBJoined) { 665444ed5c5SDimitry Andric MBBJoined = false; 6667d523365SDimitry Andric Changed = true; 6677a7e6055SDimitry Andric // Now that we have started to extend ranges across BBs we need to 6687a7e6055SDimitry Andric // examine spill instructions to see whether they spill registers that 6697a7e6055SDimitry Andric // correspond to user variables. 6707d523365SDimitry Andric for (auto &MI : *MBB) 6717a7e6055SDimitry Andric OLChanged |= transfer(MI, OpenRanges, OutLocs, VarLocIDs, Spills, 6727a7e6055SDimitry Andric /*transferSpills=*/true); 6737a7e6055SDimitry Andric 6747a7e6055SDimitry Andric // Add any DBG_VALUE instructions necessitated by spills. 6757a7e6055SDimitry Andric for (auto &SP : Spills) 6767a7e6055SDimitry Andric MBB->insertAfter(MachineBasicBlock::iterator(*SP.SpillInst), 6777a7e6055SDimitry Andric SP.DebugInst); 6787a7e6055SDimitry Andric Spills.clear(); 6793ca95b02SDimitry Andric 6803ca95b02SDimitry Andric DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, 6813ca95b02SDimitry Andric "OutLocs after propagating", dbgs())); 6823ca95b02SDimitry Andric DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, 6833ca95b02SDimitry Andric "InLocs after propagating", dbgs())); 6847d523365SDimitry Andric 6857d523365SDimitry Andric if (OLChanged) { 6867d523365SDimitry Andric OLChanged = false; 6877d523365SDimitry Andric for (auto s : MBB->successors()) 6883ca95b02SDimitry Andric if (OnPending.insert(s).second) { 689444ed5c5SDimitry Andric Pending.push(BBToOrder[s]); 6907d523365SDimitry Andric } 6917d523365SDimitry Andric } 6927d523365SDimitry Andric } 693444ed5c5SDimitry Andric } 694444ed5c5SDimitry Andric Worklist.swap(Pending); 695444ed5c5SDimitry Andric // At this point, pending must be empty, since it was just the empty 696444ed5c5SDimitry Andric // worklist 697444ed5c5SDimitry Andric assert(Pending.empty() && "Pending should be empty"); 698444ed5c5SDimitry Andric } 699444ed5c5SDimitry Andric 7003ca95b02SDimitry Andric DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "Final OutLocs", dbgs())); 7013ca95b02SDimitry Andric DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, "Final InLocs", dbgs())); 7027d523365SDimitry Andric return Changed; 7037d523365SDimitry Andric } 7047d523365SDimitry Andric 7057d523365SDimitry Andric bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) { 7062cab237bSDimitry Andric if (!MF.getFunction().getSubprogram()) 707d88c1a5aSDimitry Andric // LiveDebugValues will already have removed all DBG_VALUEs. 708d88c1a5aSDimitry Andric return false; 709d88c1a5aSDimitry Andric 7102cab237bSDimitry Andric // Skip functions from NoDebug compilation units. 7112cab237bSDimitry Andric if (MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() == 7122cab237bSDimitry Andric DICompileUnit::NoDebug) 7132cab237bSDimitry Andric return false; 7142cab237bSDimitry Andric 7157d523365SDimitry Andric TRI = MF.getSubtarget().getRegisterInfo(); 7167d523365SDimitry Andric TII = MF.getSubtarget().getInstrInfo(); 7177a7e6055SDimitry Andric TFI = MF.getSubtarget().getFrameLowering(); 718d88c1a5aSDimitry Andric LS.initialize(MF); 7197d523365SDimitry Andric 720d88c1a5aSDimitry Andric bool Changed = ExtendRanges(MF); 7217d523365SDimitry Andric return Changed; 7227d523365SDimitry Andric } 723