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"
434ba319b5SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h"
444ba319b5SDimitry Andric #include "llvm/Config/llvm-config.h"
452cab237bSDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
462cab237bSDimitry Andric #include "llvm/IR/DebugLoc.h"
472cab237bSDimitry Andric #include "llvm/IR/Function.h"
482cab237bSDimitry Andric #include "llvm/IR/Module.h"
492cab237bSDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
502cab237bSDimitry Andric #include "llvm/Pass.h"
512cab237bSDimitry Andric #include "llvm/Support/Casting.h"
522cab237bSDimitry Andric #include "llvm/Support/Compiler.h"
537d523365SDimitry Andric #include "llvm/Support/Debug.h"
547d523365SDimitry Andric #include "llvm/Support/raw_ostream.h"
552cab237bSDimitry Andric #include <algorithm>
562cab237bSDimitry Andric #include <cassert>
572cab237bSDimitry Andric #include <cstdint>
582cab237bSDimitry Andric #include <functional>
593ca95b02SDimitry Andric #include <queue>
602cab237bSDimitry Andric #include <utility>
612cab237bSDimitry Andric #include <vector>
627d523365SDimitry Andric
637d523365SDimitry Andric using namespace llvm;
647d523365SDimitry Andric
65302affcbSDimitry Andric #define DEBUG_TYPE "livedebugvalues"
667d523365SDimitry Andric
677d523365SDimitry Andric STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted");
687d523365SDimitry Andric
694ba319b5SDimitry Andric // If @MI is a DBG_VALUE with debug value described by a defined
703ca95b02SDimitry Andric // register, returns the number of this register. In the other case, returns 0.
isDbgValueDescribedByReg(const MachineInstr & MI)713ca95b02SDimitry Andric static unsigned isDbgValueDescribedByReg(const MachineInstr &MI) {
723ca95b02SDimitry Andric assert(MI.isDebugValue() && "expected a DBG_VALUE");
733ca95b02SDimitry Andric assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE");
743ca95b02SDimitry Andric // If location of variable is described using a register (directly
753ca95b02SDimitry Andric // or indirectly), this register is always a first operand.
763ca95b02SDimitry Andric return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
773ca95b02SDimitry Andric }
783ca95b02SDimitry Andric
792cab237bSDimitry Andric namespace {
807d523365SDimitry Andric
812cab237bSDimitry Andric class LiveDebugValues : public MachineFunctionPass {
827d523365SDimitry Andric private:
837d523365SDimitry Andric const TargetRegisterInfo *TRI;
847d523365SDimitry Andric const TargetInstrInfo *TII;
857a7e6055SDimitry Andric const TargetFrameLowering *TFI;
864ba319b5SDimitry Andric BitVector CalleeSavedRegs;
87d88c1a5aSDimitry Andric LexicalScopes LS;
88d88c1a5aSDimitry Andric
89d88c1a5aSDimitry Andric /// Keeps track of lexical scopes associated with a user value's source
90d88c1a5aSDimitry Andric /// location.
91d88c1a5aSDimitry Andric class UserValueScopes {
92d88c1a5aSDimitry Andric DebugLoc DL;
93d88c1a5aSDimitry Andric LexicalScopes &LS;
94d88c1a5aSDimitry Andric SmallPtrSet<const MachineBasicBlock *, 4> LBlocks;
95d88c1a5aSDimitry Andric
96d88c1a5aSDimitry Andric public:
UserValueScopes(DebugLoc D,LexicalScopes & L)97d88c1a5aSDimitry Andric UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(std::move(D)), LS(L) {}
98d88c1a5aSDimitry Andric
99d88c1a5aSDimitry Andric /// Return true if current scope dominates at least one machine
100d88c1a5aSDimitry Andric /// instruction in a given machine basic block.
dominates(MachineBasicBlock * MBB)101d88c1a5aSDimitry Andric bool dominates(MachineBasicBlock *MBB) {
102d88c1a5aSDimitry Andric if (LBlocks.empty())
103d88c1a5aSDimitry Andric LS.getMachineBasicBlocks(DL, LBlocks);
104d88c1a5aSDimitry Andric return LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB);
105d88c1a5aSDimitry Andric }
106d88c1a5aSDimitry Andric };
1077d523365SDimitry Andric
1083ca95b02SDimitry Andric /// Based on std::pair so it can be used as an index into a DenseMap.
1092cab237bSDimitry Andric using DebugVariableBase =
1102cab237bSDimitry Andric std::pair<const DILocalVariable *, const DILocation *>;
1117d523365SDimitry Andric /// A potentially inlined instance of a variable.
1123ca95b02SDimitry Andric struct DebugVariable : public DebugVariableBase {
DebugVariable__anon560902200111::LiveDebugValues::DebugVariable1133ca95b02SDimitry Andric DebugVariable(const DILocalVariable *Var, const DILocation *InlinedAt)
1143ca95b02SDimitry Andric : DebugVariableBase(Var, InlinedAt) {}
1157d523365SDimitry Andric
getVar__anon560902200111::LiveDebugValues::DebugVariable1162cab237bSDimitry Andric const DILocalVariable *getVar() const { return this->first; }
getInlinedAt__anon560902200111::LiveDebugValues::DebugVariable1172cab237bSDimitry Andric const DILocation *getInlinedAt() const { return this->second; }
1187d523365SDimitry Andric
operator <__anon560902200111::LiveDebugValues::DebugVariable1193ca95b02SDimitry Andric bool operator<(const DebugVariable &DV) const {
1203ca95b02SDimitry Andric if (getVar() == DV.getVar())
1213ca95b02SDimitry Andric return getInlinedAt() < DV.getInlinedAt();
1223ca95b02SDimitry Andric return getVar() < DV.getVar();
1237d523365SDimitry Andric }
1247d523365SDimitry Andric };
1257d523365SDimitry Andric
1263ca95b02SDimitry Andric /// A pair of debug variable and value location.
1277d523365SDimitry Andric struct VarLoc {
1283ca95b02SDimitry Andric const DebugVariable Var;
1293ca95b02SDimitry Andric const MachineInstr &MI; ///< Only used for cloning a new DBG_VALUE.
130d88c1a5aSDimitry Andric mutable UserValueScopes UVS;
1312cab237bSDimitry Andric enum { InvalidKind = 0, RegisterKind } Kind = InvalidKind;
1327d523365SDimitry Andric
1333ca95b02SDimitry Andric /// The value location. Stored separately to avoid repeatedly
1343ca95b02SDimitry Andric /// extracting it from MI.
1353ca95b02SDimitry Andric union {
1362cab237bSDimitry Andric uint64_t RegNo;
1373ca95b02SDimitry Andric uint64_t Hash;
1383ca95b02SDimitry Andric } Loc;
1393ca95b02SDimitry Andric
VarLoc__anon560902200111::LiveDebugValues::VarLoc140d88c1a5aSDimitry Andric VarLoc(const MachineInstr &MI, LexicalScopes &LS)
1413ca95b02SDimitry Andric : Var(MI.getDebugVariable(), MI.getDebugLoc()->getInlinedAt()), MI(MI),
1422cab237bSDimitry Andric UVS(MI.getDebugLoc(), LS) {
1433ca95b02SDimitry Andric static_assert((sizeof(Loc) == sizeof(uint64_t)),
1443ca95b02SDimitry Andric "hash does not cover all members of Loc");
1453ca95b02SDimitry Andric assert(MI.isDebugValue() && "not a DBG_VALUE");
1463ca95b02SDimitry Andric assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE");
1473ca95b02SDimitry Andric if (int RegNo = isDbgValueDescribedByReg(MI)) {
1483ca95b02SDimitry Andric Kind = RegisterKind;
1492cab237bSDimitry Andric Loc.RegNo = RegNo;
1503ca95b02SDimitry Andric }
1513ca95b02SDimitry Andric }
1523ca95b02SDimitry Andric
1533ca95b02SDimitry Andric /// If this variable is described by a register, return it,
1543ca95b02SDimitry Andric /// otherwise return 0.
isDescribedByReg__anon560902200111::LiveDebugValues::VarLoc1553ca95b02SDimitry Andric unsigned isDescribedByReg() const {
1563ca95b02SDimitry Andric if (Kind == RegisterKind)
1572cab237bSDimitry Andric return Loc.RegNo;
1583ca95b02SDimitry Andric return 0;
1593ca95b02SDimitry Andric }
1603ca95b02SDimitry Andric
161d88c1a5aSDimitry Andric /// Determine whether the lexical scope of this value's debug location
162d88c1a5aSDimitry Andric /// dominates MBB.
dominates__anon560902200111::LiveDebugValues::VarLoc163d88c1a5aSDimitry Andric bool dominates(MachineBasicBlock &MBB) const { return UVS.dominates(&MBB); }
164d88c1a5aSDimitry Andric
1657a7e6055SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump__anon560902200111::LiveDebugValues::VarLoc1667a7e6055SDimitry Andric LLVM_DUMP_METHOD void dump() const { MI.dump(); }
1677a7e6055SDimitry Andric #endif
1683ca95b02SDimitry Andric
operator ==__anon560902200111::LiveDebugValues::VarLoc1693ca95b02SDimitry Andric bool operator==(const VarLoc &Other) const {
1703ca95b02SDimitry Andric return Var == Other.Var && Loc.Hash == Other.Loc.Hash;
1713ca95b02SDimitry Andric }
1723ca95b02SDimitry Andric
1733ca95b02SDimitry Andric /// This operator guarantees that VarLocs are sorted by Variable first.
operator <__anon560902200111::LiveDebugValues::VarLoc1743ca95b02SDimitry Andric bool operator<(const VarLoc &Other) const {
1753ca95b02SDimitry Andric if (Var == Other.Var)
1763ca95b02SDimitry Andric return Loc.Hash < Other.Loc.Hash;
1773ca95b02SDimitry Andric return Var < Other.Var;
1783ca95b02SDimitry Andric }
1797d523365SDimitry Andric };
1807d523365SDimitry Andric
1812cab237bSDimitry Andric using VarLocMap = UniqueVector<VarLoc>;
1822cab237bSDimitry Andric using VarLocSet = SparseBitVector<>;
1832cab237bSDimitry Andric using VarLocInMBB = SmallDenseMap<const MachineBasicBlock *, VarLocSet>;
1844ba319b5SDimitry Andric struct TransferDebugPair {
1854ba319b5SDimitry Andric MachineInstr *TransferInst;
1867a7e6055SDimitry Andric MachineInstr *DebugInst;
1877a7e6055SDimitry Andric };
1884ba319b5SDimitry Andric using TransferMap = SmallVector<TransferDebugPair, 4>;
1897d523365SDimitry Andric
1903ca95b02SDimitry Andric /// This holds the working set of currently open ranges. For fast
1913ca95b02SDimitry Andric /// access, this is done both as a set of VarLocIDs, and a map of
1923ca95b02SDimitry Andric /// DebugVariable to recent VarLocID. Note that a DBG_VALUE ends all
1933ca95b02SDimitry Andric /// previous open ranges for the same variable.
1943ca95b02SDimitry Andric class OpenRangesSet {
1953ca95b02SDimitry Andric VarLocSet VarLocs;
1963ca95b02SDimitry Andric SmallDenseMap<DebugVariableBase, unsigned, 8> Vars;
1977d523365SDimitry Andric
1983ca95b02SDimitry Andric public:
getVarLocs() const1993ca95b02SDimitry Andric const VarLocSet &getVarLocs() const { return VarLocs; }
2003ca95b02SDimitry Andric
2013ca95b02SDimitry Andric /// Terminate all open ranges for Var by removing it from the set.
erase(DebugVariable Var)2023ca95b02SDimitry Andric void erase(DebugVariable Var) {
2033ca95b02SDimitry Andric auto It = Vars.find(Var);
2043ca95b02SDimitry Andric if (It != Vars.end()) {
2053ca95b02SDimitry Andric unsigned ID = It->second;
2063ca95b02SDimitry Andric VarLocs.reset(ID);
2073ca95b02SDimitry Andric Vars.erase(It);
2083ca95b02SDimitry Andric }
2093ca95b02SDimitry Andric }
2103ca95b02SDimitry Andric
2113ca95b02SDimitry Andric /// Terminate all open ranges listed in \c KillSet by removing
2123ca95b02SDimitry Andric /// them from the set.
erase(const VarLocSet & KillSet,const VarLocMap & VarLocIDs)2133ca95b02SDimitry Andric void erase(const VarLocSet &KillSet, const VarLocMap &VarLocIDs) {
2143ca95b02SDimitry Andric VarLocs.intersectWithComplement(KillSet);
2153ca95b02SDimitry Andric for (unsigned ID : KillSet)
2163ca95b02SDimitry Andric Vars.erase(VarLocIDs[ID].Var);
2173ca95b02SDimitry Andric }
2183ca95b02SDimitry Andric
2193ca95b02SDimitry Andric /// Insert a new range into the set.
insert(unsigned VarLocID,DebugVariableBase Var)2203ca95b02SDimitry Andric void insert(unsigned VarLocID, DebugVariableBase Var) {
2213ca95b02SDimitry Andric VarLocs.set(VarLocID);
2223ca95b02SDimitry Andric Vars.insert({Var, VarLocID});
2233ca95b02SDimitry Andric }
2243ca95b02SDimitry Andric
2253ca95b02SDimitry Andric /// Empty the set.
clear()2263ca95b02SDimitry Andric void clear() {
2273ca95b02SDimitry Andric VarLocs.clear();
2283ca95b02SDimitry Andric Vars.clear();
2293ca95b02SDimitry Andric }
2303ca95b02SDimitry Andric
2313ca95b02SDimitry Andric /// Return whether the set is empty or not.
empty() const2323ca95b02SDimitry Andric bool empty() const {
2333ca95b02SDimitry Andric assert(Vars.empty() == VarLocs.empty() && "open ranges are inconsistent");
2343ca95b02SDimitry Andric return VarLocs.empty();
2353ca95b02SDimitry Andric }
2363ca95b02SDimitry Andric };
2373ca95b02SDimitry Andric
2387a7e6055SDimitry Andric bool isSpillInstruction(const MachineInstr &MI, MachineFunction *MF,
2397a7e6055SDimitry Andric unsigned &Reg);
2407a7e6055SDimitry Andric int extractSpillBaseRegAndOffset(const MachineInstr &MI, unsigned &Reg);
2414ba319b5SDimitry Andric void insertTransferDebugPair(MachineInstr &MI, OpenRangesSet &OpenRanges,
2424ba319b5SDimitry Andric TransferMap &Transfers, VarLocMap &VarLocIDs,
2434ba319b5SDimitry Andric unsigned OldVarID, unsigned NewReg = 0);
2447a7e6055SDimitry Andric
2453ca95b02SDimitry Andric void transferDebugValue(const MachineInstr &MI, OpenRangesSet &OpenRanges,
2463ca95b02SDimitry Andric VarLocMap &VarLocIDs);
2477a7e6055SDimitry Andric void transferSpillInst(MachineInstr &MI, OpenRangesSet &OpenRanges,
2484ba319b5SDimitry Andric VarLocMap &VarLocIDs, TransferMap &Transfers);
2494ba319b5SDimitry Andric void transferRegisterCopy(MachineInstr &MI, OpenRangesSet &OpenRanges,
2504ba319b5SDimitry Andric VarLocMap &VarLocIDs, TransferMap &Transfers);
2513ca95b02SDimitry Andric void transferRegisterDef(MachineInstr &MI, OpenRangesSet &OpenRanges,
2523ca95b02SDimitry Andric const VarLocMap &VarLocIDs);
2533ca95b02SDimitry Andric bool transferTerminatorInst(MachineInstr &MI, OpenRangesSet &OpenRanges,
2543ca95b02SDimitry Andric VarLocInMBB &OutLocs, const VarLocMap &VarLocIDs);
2554ba319b5SDimitry Andric bool process(MachineInstr &MI, OpenRangesSet &OpenRanges,
2564ba319b5SDimitry Andric VarLocInMBB &OutLocs, VarLocMap &VarLocIDs,
2574ba319b5SDimitry Andric TransferMap &Transfers, bool transferChanges);
2583ca95b02SDimitry Andric
2593ca95b02SDimitry Andric bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs,
260d88c1a5aSDimitry Andric const VarLocMap &VarLocIDs,
261*b5893f02SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> &Visited,
262*b5893f02SDimitry Andric SmallPtrSetImpl<const MachineBasicBlock *> &ArtificialBlocks);
2637d523365SDimitry Andric
2647d523365SDimitry Andric bool ExtendRanges(MachineFunction &MF);
2657d523365SDimitry Andric
2667d523365SDimitry Andric public:
2677d523365SDimitry Andric static char ID;
2687d523365SDimitry Andric
2697d523365SDimitry Andric /// Default construct and initialize the pass.
2707d523365SDimitry Andric LiveDebugValues();
2717d523365SDimitry Andric
2727d523365SDimitry Andric /// Tell the pass manager which passes we depend on and what
2737d523365SDimitry Andric /// information we preserve.
2747d523365SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override;
2757d523365SDimitry Andric
getRequiredProperties() const2763ca95b02SDimitry Andric MachineFunctionProperties getRequiredProperties() const override {
2773ca95b02SDimitry Andric return MachineFunctionProperties().set(
278d88c1a5aSDimitry Andric MachineFunctionProperties::Property::NoVRegs);
2793ca95b02SDimitry Andric }
2803ca95b02SDimitry Andric
2817d523365SDimitry Andric /// Print to ostream with a message.
2823ca95b02SDimitry Andric void printVarLocInMBB(const MachineFunction &MF, const VarLocInMBB &V,
2833ca95b02SDimitry Andric const VarLocMap &VarLocIDs, const char *msg,
2847d523365SDimitry Andric raw_ostream &Out) const;
2857d523365SDimitry Andric
2867d523365SDimitry Andric /// Calculate the liveness information for the given machine function.
2877d523365SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
2887d523365SDimitry Andric };
289d88c1a5aSDimitry Andric
2902cab237bSDimitry Andric } // end anonymous namespace
2917d523365SDimitry Andric
2927d523365SDimitry Andric //===----------------------------------------------------------------------===//
2937d523365SDimitry Andric // Implementation
2947d523365SDimitry Andric //===----------------------------------------------------------------------===//
2957d523365SDimitry Andric
2967d523365SDimitry Andric char LiveDebugValues::ID = 0;
2972cab237bSDimitry Andric
2987d523365SDimitry Andric char &llvm::LiveDebugValuesID = LiveDebugValues::ID;
2992cab237bSDimitry Andric
300302affcbSDimitry Andric INITIALIZE_PASS(LiveDebugValues, DEBUG_TYPE, "Live DEBUG_VALUE analysis",
3017d523365SDimitry Andric false, false)
3027d523365SDimitry Andric
3037d523365SDimitry Andric /// Default construct and initialize the pass.
LiveDebugValues()3047d523365SDimitry Andric LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) {
3057d523365SDimitry Andric initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry());
3067d523365SDimitry Andric }
3077d523365SDimitry Andric
3087d523365SDimitry Andric /// Tell the pass manager which passes we depend on and what information we
3097d523365SDimitry Andric /// preserve.
getAnalysisUsage(AnalysisUsage & AU) const3107d523365SDimitry Andric void LiveDebugValues::getAnalysisUsage(AnalysisUsage &AU) const {
3113ca95b02SDimitry Andric AU.setPreservesCFG();
3127d523365SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
3137d523365SDimitry Andric }
3147d523365SDimitry Andric
3157d523365SDimitry Andric //===----------------------------------------------------------------------===//
3167d523365SDimitry Andric // Debug Range Extension Implementation
3177d523365SDimitry Andric //===----------------------------------------------------------------------===//
3187d523365SDimitry Andric
3197a7e6055SDimitry Andric #ifndef NDEBUG
printVarLocInMBB(const MachineFunction & MF,const VarLocInMBB & V,const VarLocMap & VarLocIDs,const char * msg,raw_ostream & Out) const3203ca95b02SDimitry Andric void LiveDebugValues::printVarLocInMBB(const MachineFunction &MF,
3213ca95b02SDimitry Andric const VarLocInMBB &V,
3223ca95b02SDimitry Andric const VarLocMap &VarLocIDs,
3233ca95b02SDimitry Andric const char *msg,
3247d523365SDimitry Andric raw_ostream &Out) const {
325d88c1a5aSDimitry Andric Out << '\n' << msg << '\n';
3263ca95b02SDimitry Andric for (const MachineBasicBlock &BB : MF) {
327*b5893f02SDimitry Andric const VarLocSet &L = V.lookup(&BB);
328*b5893f02SDimitry Andric if (L.empty())
329*b5893f02SDimitry Andric continue;
330*b5893f02SDimitry Andric Out << "MBB: " << BB.getNumber() << ":\n";
3313ca95b02SDimitry Andric for (unsigned VLL : L) {
3323ca95b02SDimitry Andric const VarLoc &VL = VarLocIDs[VLL];
3333ca95b02SDimitry Andric Out << " Var: " << VL.Var.getVar()->getName();
3347d523365SDimitry Andric Out << " MI: ";
3353ca95b02SDimitry Andric VL.dump();
3367d523365SDimitry Andric }
3377d523365SDimitry Andric }
3387d523365SDimitry Andric Out << "\n";
3397d523365SDimitry Andric }
3407a7e6055SDimitry Andric #endif
3417a7e6055SDimitry Andric
3427a7e6055SDimitry Andric /// Given a spill instruction, extract the register and offset used to
3437a7e6055SDimitry Andric /// address the spill location in a target independent way.
extractSpillBaseRegAndOffset(const MachineInstr & MI,unsigned & Reg)3447a7e6055SDimitry Andric int LiveDebugValues::extractSpillBaseRegAndOffset(const MachineInstr &MI,
3457a7e6055SDimitry Andric unsigned &Reg) {
3467a7e6055SDimitry Andric assert(MI.hasOneMemOperand() &&
3477a7e6055SDimitry Andric "Spill instruction does not have exactly one memory operand?");
3487a7e6055SDimitry Andric auto MMOI = MI.memoperands_begin();
3497a7e6055SDimitry Andric const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue();
3507a7e6055SDimitry Andric assert(PVal->kind() == PseudoSourceValue::FixedStack &&
3517a7e6055SDimitry Andric "Inconsistent memory operand in spill instruction");
3527a7e6055SDimitry Andric int FI = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex();
3537a7e6055SDimitry Andric const MachineBasicBlock *MBB = MI.getParent();
3547a7e6055SDimitry Andric return TFI->getFrameIndexReference(*MBB->getParent(), FI, Reg);
3557a7e6055SDimitry Andric }
3567d523365SDimitry Andric
3577d523365SDimitry Andric /// End all previous ranges related to @MI and start a new range from @MI
3587d523365SDimitry Andric /// if it is a DBG_VALUE instr.
transferDebugValue(const MachineInstr & MI,OpenRangesSet & OpenRanges,VarLocMap & VarLocIDs)3593ca95b02SDimitry Andric void LiveDebugValues::transferDebugValue(const MachineInstr &MI,
3603ca95b02SDimitry Andric OpenRangesSet &OpenRanges,
3613ca95b02SDimitry Andric VarLocMap &VarLocIDs) {
3627d523365SDimitry Andric if (!MI.isDebugValue())
3637d523365SDimitry Andric return;
3643ca95b02SDimitry Andric const DILocalVariable *Var = MI.getDebugVariable();
3653ca95b02SDimitry Andric const DILocation *DebugLoc = MI.getDebugLoc();
3663ca95b02SDimitry Andric const DILocation *InlinedAt = DebugLoc->getInlinedAt();
3673ca95b02SDimitry Andric assert(Var->isValidLocationForIntrinsic(DebugLoc) &&
3687d523365SDimitry Andric "Expected inlined-at fields to agree");
3697d523365SDimitry Andric
3707d523365SDimitry Andric // End all previous ranges of Var.
3713ca95b02SDimitry Andric DebugVariable V(Var, InlinedAt);
3723ca95b02SDimitry Andric OpenRanges.erase(V);
3737d523365SDimitry Andric
3743ca95b02SDimitry Andric // Add the VarLoc to OpenRanges from this DBG_VALUE.
3757d523365SDimitry Andric // TODO: Currently handles DBG_VALUE which has only reg as location.
3763ca95b02SDimitry Andric if (isDbgValueDescribedByReg(MI)) {
377d88c1a5aSDimitry Andric VarLoc VL(MI, LS);
3783ca95b02SDimitry Andric unsigned ID = VarLocIDs.insert(VL);
3793ca95b02SDimitry Andric OpenRanges.insert(ID, VL.Var);
3807d523365SDimitry Andric }
3817d523365SDimitry Andric }
3827d523365SDimitry Andric
3834ba319b5SDimitry Andric /// Create new TransferDebugPair and insert it in \p Transfers. The VarLoc
3844ba319b5SDimitry Andric /// with \p OldVarID should be deleted form \p OpenRanges and replaced with
3854ba319b5SDimitry Andric /// new VarLoc. If \p NewReg is different than default zero value then the
3864ba319b5SDimitry Andric /// new location will be register location created by the copy like instruction,
3874ba319b5SDimitry Andric /// otherwise it is variable's location on the stack.
insertTransferDebugPair(MachineInstr & MI,OpenRangesSet & OpenRanges,TransferMap & Transfers,VarLocMap & VarLocIDs,unsigned OldVarID,unsigned NewReg)3884ba319b5SDimitry Andric void LiveDebugValues::insertTransferDebugPair(
3894ba319b5SDimitry Andric MachineInstr &MI, OpenRangesSet &OpenRanges, TransferMap &Transfers,
3904ba319b5SDimitry Andric VarLocMap &VarLocIDs, unsigned OldVarID, unsigned NewReg) {
3914ba319b5SDimitry Andric const MachineInstr *DMI = &VarLocIDs[OldVarID].MI;
3924ba319b5SDimitry Andric MachineFunction *MF = MI.getParent()->getParent();
3934ba319b5SDimitry Andric MachineInstr *NewDMI;
3944ba319b5SDimitry Andric if (NewReg) {
3954ba319b5SDimitry Andric // Create a DBG_VALUE instruction to describe the Var in its new
3964ba319b5SDimitry Andric // register location.
3974ba319b5SDimitry Andric NewDMI = BuildMI(*MF, DMI->getDebugLoc(), DMI->getDesc(),
3984ba319b5SDimitry Andric DMI->isIndirectDebugValue(), NewReg,
3994ba319b5SDimitry Andric DMI->getDebugVariable(), DMI->getDebugExpression());
4004ba319b5SDimitry Andric if (DMI->isIndirectDebugValue())
4014ba319b5SDimitry Andric NewDMI->getOperand(1).setImm(DMI->getOperand(1).getImm());
4024ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Creating DBG_VALUE inst for register copy: ";
4034ba319b5SDimitry Andric NewDMI->print(dbgs(), false, false, false, TII));
4044ba319b5SDimitry Andric } else {
4054ba319b5SDimitry Andric // Create a DBG_VALUE instruction to describe the Var in its spilled
4064ba319b5SDimitry Andric // location.
4074ba319b5SDimitry Andric unsigned SpillBase;
4084ba319b5SDimitry Andric int SpillOffset = extractSpillBaseRegAndOffset(MI, SpillBase);
4094ba319b5SDimitry Andric auto *SpillExpr = DIExpression::prepend(DMI->getDebugExpression(),
4104ba319b5SDimitry Andric DIExpression::NoDeref, SpillOffset);
4114ba319b5SDimitry Andric NewDMI = BuildMI(*MF, DMI->getDebugLoc(), DMI->getDesc(), true, SpillBase,
4124ba319b5SDimitry Andric DMI->getDebugVariable(), SpillExpr);
4134ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Creating DBG_VALUE inst for spill: ";
4144ba319b5SDimitry Andric NewDMI->print(dbgs(), false, false, false, TII));
4154ba319b5SDimitry Andric }
4164ba319b5SDimitry Andric
4174ba319b5SDimitry Andric // The newly created DBG_VALUE instruction NewDMI must be inserted after
4184ba319b5SDimitry Andric // MI. Keep track of the pairing.
4194ba319b5SDimitry Andric TransferDebugPair MIP = {&MI, NewDMI};
4204ba319b5SDimitry Andric Transfers.push_back(MIP);
4214ba319b5SDimitry Andric
4224ba319b5SDimitry Andric // End all previous ranges of Var.
4234ba319b5SDimitry Andric OpenRanges.erase(VarLocIDs[OldVarID].Var);
4244ba319b5SDimitry Andric
4254ba319b5SDimitry Andric // Add the VarLoc to OpenRanges.
4264ba319b5SDimitry Andric VarLoc VL(*NewDMI, LS);
4274ba319b5SDimitry Andric unsigned LocID = VarLocIDs.insert(VL);
4284ba319b5SDimitry Andric OpenRanges.insert(LocID, VL.Var);
4294ba319b5SDimitry Andric }
4304ba319b5SDimitry Andric
4317d523365SDimitry Andric /// A definition of a register may mark the end of a range.
transferRegisterDef(MachineInstr & MI,OpenRangesSet & OpenRanges,const VarLocMap & VarLocIDs)4327d523365SDimitry Andric void LiveDebugValues::transferRegisterDef(MachineInstr &MI,
4333ca95b02SDimitry Andric OpenRangesSet &OpenRanges,
4343ca95b02SDimitry Andric const VarLocMap &VarLocIDs) {
4352cab237bSDimitry Andric MachineFunction *MF = MI.getMF();
4363ca95b02SDimitry Andric const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
4373ca95b02SDimitry Andric unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
4383ca95b02SDimitry Andric SparseBitVector<> KillSet;
4397d523365SDimitry Andric for (const MachineOperand &MO : MI.operands()) {
4407a7e6055SDimitry Andric // Determine whether the operand is a register def. Assume that call
4417a7e6055SDimitry Andric // instructions never clobber SP, because some backends (e.g., AArch64)
4427a7e6055SDimitry Andric // never list SP in the regmask.
4433ca95b02SDimitry Andric if (MO.isReg() && MO.isDef() && MO.getReg() &&
4447a7e6055SDimitry Andric TRI->isPhysicalRegister(MO.getReg()) &&
4457a7e6055SDimitry Andric !(MI.isCall() && MO.getReg() == SP)) {
4467d523365SDimitry Andric // Remove ranges of all aliased registers.
4477d523365SDimitry Andric for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI)
4483ca95b02SDimitry Andric for (unsigned ID : OpenRanges.getVarLocs())
4493ca95b02SDimitry Andric if (VarLocIDs[ID].isDescribedByReg() == *RAI)
4503ca95b02SDimitry Andric KillSet.set(ID);
4513ca95b02SDimitry Andric } else if (MO.isRegMask()) {
4523ca95b02SDimitry Andric // Remove ranges of all clobbered registers. Register masks don't usually
4533ca95b02SDimitry Andric // list SP as preserved. While the debug info may be off for an
4543ca95b02SDimitry Andric // instruction or two around callee-cleanup calls, transferring the
4553ca95b02SDimitry Andric // DEBUG_VALUE across the call is still a better user experience.
4563ca95b02SDimitry Andric for (unsigned ID : OpenRanges.getVarLocs()) {
4573ca95b02SDimitry Andric unsigned Reg = VarLocIDs[ID].isDescribedByReg();
4583ca95b02SDimitry Andric if (Reg && Reg != SP && MO.clobbersPhysReg(Reg))
4593ca95b02SDimitry Andric KillSet.set(ID);
4607d523365SDimitry Andric }
4617d523365SDimitry Andric }
4623ca95b02SDimitry Andric }
4633ca95b02SDimitry Andric OpenRanges.erase(KillSet, VarLocIDs);
4643ca95b02SDimitry Andric }
4657d523365SDimitry Andric
4667a7e6055SDimitry Andric /// Decide if @MI is a spill instruction and return true if it is. We use 2
4677a7e6055SDimitry Andric /// criteria to make this decision:
4687a7e6055SDimitry Andric /// - Is this instruction a store to a spill slot?
4697a7e6055SDimitry Andric /// - Is there a register operand that is both used and killed?
4707a7e6055SDimitry Andric /// TODO: Store optimization can fold spills into other stores (including
4717a7e6055SDimitry Andric /// other spills). We do not handle this yet (more than one memory operand).
isSpillInstruction(const MachineInstr & MI,MachineFunction * MF,unsigned & Reg)4727a7e6055SDimitry Andric bool LiveDebugValues::isSpillInstruction(const MachineInstr &MI,
4737a7e6055SDimitry Andric MachineFunction *MF, unsigned &Reg) {
4747a7e6055SDimitry Andric const MachineFrameInfo &FrameInfo = MF->getFrameInfo();
4757a7e6055SDimitry Andric int FI;
476*b5893f02SDimitry Andric SmallVector<const MachineMemOperand*, 1> Accesses;
4777a7e6055SDimitry Andric
4787a7e6055SDimitry Andric // TODO: Handle multiple stores folded into one.
4797a7e6055SDimitry Andric if (!MI.hasOneMemOperand())
4807a7e6055SDimitry Andric return false;
4817a7e6055SDimitry Andric
4827a7e6055SDimitry Andric // To identify a spill instruction, use the same criteria as in AsmPrinter.
483*b5893f02SDimitry Andric if (!((TII->isStoreToStackSlotPostFE(MI, FI) &&
484*b5893f02SDimitry Andric FrameInfo.isSpillSlotObjectIndex(FI)) ||
485*b5893f02SDimitry Andric (TII->hasStoreToStackSlot(MI, Accesses) &&
486*b5893f02SDimitry Andric llvm::any_of(Accesses, [&FrameInfo](const MachineMemOperand *MMO) {
487*b5893f02SDimitry Andric return FrameInfo.isSpillSlotObjectIndex(
488*b5893f02SDimitry Andric cast<FixedStackPseudoSourceValue>(MMO->getPseudoValue())
489*b5893f02SDimitry Andric ->getFrameIndex());
490*b5893f02SDimitry Andric }))))
4917a7e6055SDimitry Andric return false;
4927a7e6055SDimitry Andric
4934ba319b5SDimitry Andric auto isKilledReg = [&](const MachineOperand MO, unsigned &Reg) {
4944ba319b5SDimitry Andric if (!MO.isReg() || !MO.isUse()) {
4957a7e6055SDimitry Andric Reg = 0;
4964ba319b5SDimitry Andric return false;
4974ba319b5SDimitry Andric }
4987a7e6055SDimitry Andric Reg = MO.getReg();
4994ba319b5SDimitry Andric return MO.isKill();
5004ba319b5SDimitry Andric };
5014ba319b5SDimitry Andric
5024ba319b5SDimitry Andric for (const MachineOperand &MO : MI.operands()) {
5034ba319b5SDimitry Andric // In a spill instruction generated by the InlineSpiller the spilled
5044ba319b5SDimitry Andric // register has its kill flag set.
5054ba319b5SDimitry Andric if (isKilledReg(MO, Reg))
5064ba319b5SDimitry Andric return true;
5074ba319b5SDimitry Andric if (Reg != 0) {
5084ba319b5SDimitry Andric // Check whether next instruction kills the spilled register.
5094ba319b5SDimitry Andric // FIXME: Current solution does not cover search for killed register in
5104ba319b5SDimitry Andric // bundles and instructions further down the chain.
5114ba319b5SDimitry Andric auto NextI = std::next(MI.getIterator());
5124ba319b5SDimitry Andric // Skip next instruction that points to basic block end iterator.
5134ba319b5SDimitry Andric if (MI.getParent()->end() == NextI)
5144ba319b5SDimitry Andric continue;
5154ba319b5SDimitry Andric unsigned RegNext;
5164ba319b5SDimitry Andric for (const MachineOperand &MONext : NextI->operands()) {
5174ba319b5SDimitry Andric // Return true if we came across the register from the
5184ba319b5SDimitry Andric // previous spill instruction that is killed in NextI.
5194ba319b5SDimitry Andric if (isKilledReg(MONext, RegNext) && RegNext == Reg)
5204ba319b5SDimitry Andric return true;
5217a7e6055SDimitry Andric }
5227a7e6055SDimitry Andric }
5234ba319b5SDimitry Andric }
5244ba319b5SDimitry Andric // Return false if we didn't find spilled register.
5254ba319b5SDimitry Andric return false;
5267a7e6055SDimitry Andric }
5277a7e6055SDimitry Andric
5287a7e6055SDimitry Andric /// A spilled register may indicate that we have to end the current range of
5297a7e6055SDimitry Andric /// a variable and create a new one for the spill location.
5304ba319b5SDimitry Andric /// We don't want to insert any instructions in process(), so we just create
5314ba319b5SDimitry Andric /// the DBG_VALUE without inserting it and keep track of it in \p Transfers.
5327a7e6055SDimitry Andric /// It will be inserted into the BB when we're done iterating over the
5337a7e6055SDimitry Andric /// instructions.
transferSpillInst(MachineInstr & MI,OpenRangesSet & OpenRanges,VarLocMap & VarLocIDs,TransferMap & Transfers)5347a7e6055SDimitry Andric void LiveDebugValues::transferSpillInst(MachineInstr &MI,
5357a7e6055SDimitry Andric OpenRangesSet &OpenRanges,
5367a7e6055SDimitry Andric VarLocMap &VarLocIDs,
5374ba319b5SDimitry Andric TransferMap &Transfers) {
5387a7e6055SDimitry Andric unsigned Reg;
5392cab237bSDimitry Andric MachineFunction *MF = MI.getMF();
5407a7e6055SDimitry Andric if (!isSpillInstruction(MI, MF, Reg))
5417a7e6055SDimitry Andric return;
5427a7e6055SDimitry Andric
5437a7e6055SDimitry Andric // Check if the register is the location of a debug value.
5447a7e6055SDimitry Andric for (unsigned ID : OpenRanges.getVarLocs()) {
5457a7e6055SDimitry Andric if (VarLocIDs[ID].isDescribedByReg() == Reg) {
5464ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Spilling Register " << printReg(Reg, TRI) << '('
5477a7e6055SDimitry Andric << VarLocIDs[ID].Var.getVar()->getName() << ")\n");
5484ba319b5SDimitry Andric insertTransferDebugPair(MI, OpenRanges, Transfers, VarLocIDs, ID);
5494ba319b5SDimitry Andric return;
5504ba319b5SDimitry Andric }
5514ba319b5SDimitry Andric }
5524ba319b5SDimitry Andric }
5537a7e6055SDimitry Andric
5544ba319b5SDimitry Andric /// If \p MI is a register copy instruction, that copies a previously tracked
5554ba319b5SDimitry Andric /// value from one register to another register that is callee saved, we
5564ba319b5SDimitry Andric /// create new DBG_VALUE instruction described with copy destination register.
transferRegisterCopy(MachineInstr & MI,OpenRangesSet & OpenRanges,VarLocMap & VarLocIDs,TransferMap & Transfers)5574ba319b5SDimitry Andric void LiveDebugValues::transferRegisterCopy(MachineInstr &MI,
5584ba319b5SDimitry Andric OpenRangesSet &OpenRanges,
5594ba319b5SDimitry Andric VarLocMap &VarLocIDs,
5604ba319b5SDimitry Andric TransferMap &Transfers) {
5614ba319b5SDimitry Andric const MachineOperand *SrcRegOp, *DestRegOp;
5627a7e6055SDimitry Andric
5634ba319b5SDimitry Andric if (!TII->isCopyInstr(MI, SrcRegOp, DestRegOp) || !SrcRegOp->isKill() ||
5644ba319b5SDimitry Andric !DestRegOp->isDef())
5654ba319b5SDimitry Andric return;
5667a7e6055SDimitry Andric
5674ba319b5SDimitry Andric auto isCalleSavedReg = [&](unsigned Reg) {
5684ba319b5SDimitry Andric for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI)
5694ba319b5SDimitry Andric if (CalleeSavedRegs.test(*RAI))
5704ba319b5SDimitry Andric return true;
5714ba319b5SDimitry Andric return false;
5724ba319b5SDimitry Andric };
5737a7e6055SDimitry Andric
5744ba319b5SDimitry Andric unsigned SrcReg = SrcRegOp->getReg();
5754ba319b5SDimitry Andric unsigned DestReg = DestRegOp->getReg();
5764ba319b5SDimitry Andric
5774ba319b5SDimitry Andric // We want to recognize instructions where destination register is callee
5784ba319b5SDimitry Andric // saved register. If register that could be clobbered by the call is
5794ba319b5SDimitry Andric // included, there would be a great chance that it is going to be clobbered
5804ba319b5SDimitry Andric // soon. It is more likely that previous register location, which is callee
5814ba319b5SDimitry Andric // saved, is going to stay unclobbered longer, even if it is killed.
5824ba319b5SDimitry Andric if (!isCalleSavedReg(DestReg))
5834ba319b5SDimitry Andric return;
5844ba319b5SDimitry Andric
5854ba319b5SDimitry Andric for (unsigned ID : OpenRanges.getVarLocs()) {
5864ba319b5SDimitry Andric if (VarLocIDs[ID].isDescribedByReg() == SrcReg) {
5874ba319b5SDimitry Andric insertTransferDebugPair(MI, OpenRanges, Transfers, VarLocIDs, ID,
5884ba319b5SDimitry Andric DestReg);
5897a7e6055SDimitry Andric return;
5907a7e6055SDimitry Andric }
5917a7e6055SDimitry Andric }
5927a7e6055SDimitry Andric }
5937a7e6055SDimitry Andric
5947d523365SDimitry Andric /// Terminate all open ranges at the end of the current basic block.
transferTerminatorInst(MachineInstr & MI,OpenRangesSet & OpenRanges,VarLocInMBB & OutLocs,const VarLocMap & VarLocIDs)595444ed5c5SDimitry Andric bool LiveDebugValues::transferTerminatorInst(MachineInstr &MI,
5963ca95b02SDimitry Andric OpenRangesSet &OpenRanges,
5973ca95b02SDimitry Andric VarLocInMBB &OutLocs,
5983ca95b02SDimitry Andric const VarLocMap &VarLocIDs) {
599444ed5c5SDimitry Andric bool Changed = false;
6007d523365SDimitry Andric const MachineBasicBlock *CurMBB = MI.getParent();
6014ba319b5SDimitry Andric if (!(MI.isTerminator() || (&MI == &CurMBB->back())))
602444ed5c5SDimitry Andric return false;
6037d523365SDimitry Andric
6047d523365SDimitry Andric if (OpenRanges.empty())
605444ed5c5SDimitry Andric return false;
6067d523365SDimitry Andric
6074ba319b5SDimitry Andric LLVM_DEBUG(for (unsigned ID
6084ba319b5SDimitry Andric : OpenRanges.getVarLocs()) {
6097d523365SDimitry Andric // Copy OpenRanges to OutLocs, if not already present.
610*b5893f02SDimitry Andric dbgs() << "Add to OutLocs in MBB #" << CurMBB->getNumber() << ": ";
6114ba319b5SDimitry Andric VarLocIDs[ID].dump();
6123ca95b02SDimitry Andric });
6133ca95b02SDimitry Andric VarLocSet &VLS = OutLocs[CurMBB];
6143ca95b02SDimitry Andric Changed = VLS |= OpenRanges.getVarLocs();
6157d523365SDimitry Andric OpenRanges.clear();
616444ed5c5SDimitry Andric return Changed;
6177d523365SDimitry Andric }
6187d523365SDimitry Andric
6197d523365SDimitry Andric /// This routine creates OpenRanges and OutLocs.
process(MachineInstr & MI,OpenRangesSet & OpenRanges,VarLocInMBB & OutLocs,VarLocMap & VarLocIDs,TransferMap & Transfers,bool transferChanges)6204ba319b5SDimitry Andric bool LiveDebugValues::process(MachineInstr &MI, OpenRangesSet &OpenRanges,
6217a7e6055SDimitry Andric VarLocInMBB &OutLocs, VarLocMap &VarLocIDs,
6224ba319b5SDimitry Andric TransferMap &Transfers, bool transferChanges) {
623444ed5c5SDimitry Andric bool Changed = false;
6243ca95b02SDimitry Andric transferDebugValue(MI, OpenRanges, VarLocIDs);
6253ca95b02SDimitry Andric transferRegisterDef(MI, OpenRanges, VarLocIDs);
6264ba319b5SDimitry Andric if (transferChanges) {
6274ba319b5SDimitry Andric transferRegisterCopy(MI, OpenRanges, VarLocIDs, Transfers);
6284ba319b5SDimitry Andric transferSpillInst(MI, OpenRanges, VarLocIDs, Transfers);
6294ba319b5SDimitry Andric }
6303ca95b02SDimitry Andric Changed = transferTerminatorInst(MI, OpenRanges, OutLocs, VarLocIDs);
631444ed5c5SDimitry Andric return Changed;
6327d523365SDimitry Andric }
6337d523365SDimitry Andric
6347d523365SDimitry Andric /// This routine joins the analysis results of all incoming edges in @MBB by
6357d523365SDimitry Andric /// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same
6367d523365SDimitry Andric /// source variable in all the predecessors of @MBB reside in the same location.
join(MachineBasicBlock & MBB,VarLocInMBB & OutLocs,VarLocInMBB & InLocs,const VarLocMap & VarLocIDs,SmallPtrSet<const MachineBasicBlock *,16> & Visited,SmallPtrSetImpl<const MachineBasicBlock * > & ArtificialBlocks)637*b5893f02SDimitry Andric bool LiveDebugValues::join(
638*b5893f02SDimitry Andric MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs,
639*b5893f02SDimitry Andric const VarLocMap &VarLocIDs,
640*b5893f02SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> &Visited,
641*b5893f02SDimitry Andric SmallPtrSetImpl<const MachineBasicBlock *> &ArtificialBlocks) {
642*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "join MBB: " << MBB.getNumber() << "\n");
643444ed5c5SDimitry Andric bool Changed = false;
6447d523365SDimitry Andric
6453ca95b02SDimitry Andric VarLocSet InLocsT; // Temporary incoming locations.
6467d523365SDimitry Andric
6473ca95b02SDimitry Andric // For all predecessors of this MBB, find the set of VarLocs that
6483ca95b02SDimitry Andric // can be joined.
649d88c1a5aSDimitry Andric int NumVisited = 0;
6507d523365SDimitry Andric for (auto p : MBB.predecessors()) {
651d88c1a5aSDimitry Andric // Ignore unvisited predecessor blocks. As we are processing
652d88c1a5aSDimitry Andric // the blocks in reverse post-order any unvisited block can
653d88c1a5aSDimitry Andric // be considered to not remove any incoming values.
654*b5893f02SDimitry Andric if (!Visited.count(p)) {
655*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << " ignoring unvisited pred MBB: " << p->getNumber()
656*b5893f02SDimitry Andric << "\n");
657d88c1a5aSDimitry Andric continue;
658*b5893f02SDimitry Andric }
6597d523365SDimitry Andric auto OL = OutLocs.find(p);
6607d523365SDimitry Andric // Join is null in case of empty OutLocs from any of the pred.
6617d523365SDimitry Andric if (OL == OutLocs.end())
662444ed5c5SDimitry Andric return false;
6637d523365SDimitry Andric
664d88c1a5aSDimitry Andric // Just copy over the Out locs to incoming locs for the first visited
665d88c1a5aSDimitry Andric // predecessor, and for all other predecessors join the Out locs.
666d88c1a5aSDimitry Andric if (!NumVisited)
6677d523365SDimitry Andric InLocsT = OL->second;
668d88c1a5aSDimitry Andric else
6693ca95b02SDimitry Andric InLocsT &= OL->second;
670*b5893f02SDimitry Andric
671*b5893f02SDimitry Andric LLVM_DEBUG({
672*b5893f02SDimitry Andric if (!InLocsT.empty()) {
673*b5893f02SDimitry Andric for (auto ID : InLocsT)
674*b5893f02SDimitry Andric dbgs() << " gathered candidate incoming var: "
675*b5893f02SDimitry Andric << VarLocIDs[ID].Var.getVar()->getName() << "\n";
676*b5893f02SDimitry Andric }
677*b5893f02SDimitry Andric });
678*b5893f02SDimitry Andric
679d88c1a5aSDimitry Andric NumVisited++;
6807d523365SDimitry Andric }
6817d523365SDimitry Andric
682d88c1a5aSDimitry Andric // Filter out DBG_VALUES that are out of scope.
683d88c1a5aSDimitry Andric VarLocSet KillSet;
684*b5893f02SDimitry Andric bool IsArtificial = ArtificialBlocks.count(&MBB);
685*b5893f02SDimitry Andric if (!IsArtificial) {
686*b5893f02SDimitry Andric for (auto ID : InLocsT) {
687*b5893f02SDimitry Andric if (!VarLocIDs[ID].dominates(MBB)) {
688d88c1a5aSDimitry Andric KillSet.set(ID);
689*b5893f02SDimitry Andric LLVM_DEBUG({
690*b5893f02SDimitry Andric auto Name = VarLocIDs[ID].Var.getVar()->getName();
691*b5893f02SDimitry Andric dbgs() << " killing " << Name << ", it doesn't dominate MBB\n";
692*b5893f02SDimitry Andric });
693*b5893f02SDimitry Andric }
694*b5893f02SDimitry Andric }
695*b5893f02SDimitry Andric }
696d88c1a5aSDimitry Andric InLocsT.intersectWithComplement(KillSet);
697d88c1a5aSDimitry Andric
698d88c1a5aSDimitry Andric // As we are processing blocks in reverse post-order we
699d88c1a5aSDimitry Andric // should have processed at least one predecessor, unless it
700d88c1a5aSDimitry Andric // is the entry block which has no predecessor.
701d88c1a5aSDimitry Andric assert((NumVisited || MBB.pred_empty()) &&
702d88c1a5aSDimitry Andric "Should have processed at least one predecessor");
7037d523365SDimitry Andric if (InLocsT.empty())
704444ed5c5SDimitry Andric return false;
7057d523365SDimitry Andric
7063ca95b02SDimitry Andric VarLocSet &ILS = InLocs[&MBB];
7077d523365SDimitry Andric
7087d523365SDimitry Andric // Insert DBG_VALUE instructions, if not already inserted.
7093ca95b02SDimitry Andric VarLocSet Diff = InLocsT;
7103ca95b02SDimitry Andric Diff.intersectWithComplement(ILS);
7113ca95b02SDimitry Andric for (auto ID : Diff) {
7127d523365SDimitry Andric // This VarLoc is not found in InLocs i.e. it is not yet inserted. So, a
7137d523365SDimitry Andric // new range is started for the var from the mbb's beginning by inserting
7144ba319b5SDimitry Andric // a new DBG_VALUE. process() will end this range however appropriate.
7153ca95b02SDimitry Andric const VarLoc &DiffIt = VarLocIDs[ID];
7163ca95b02SDimitry Andric const MachineInstr *DMI = &DiffIt.MI;
7177d523365SDimitry Andric MachineInstr *MI =
7187d523365SDimitry Andric BuildMI(MBB, MBB.instr_begin(), DMI->getDebugLoc(), DMI->getDesc(),
7192cab237bSDimitry Andric DMI->isIndirectDebugValue(), DMI->getOperand(0).getReg(),
7207d523365SDimitry Andric DMI->getDebugVariable(), DMI->getDebugExpression());
7217d523365SDimitry Andric if (DMI->isIndirectDebugValue())
7227d523365SDimitry Andric MI->getOperand(1).setImm(DMI->getOperand(1).getImm());
7234ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Inserted: "; MI->dump(););
7243ca95b02SDimitry Andric ILS.set(ID);
7257d523365SDimitry Andric ++NumInserted;
726444ed5c5SDimitry Andric Changed = true;
7277d523365SDimitry Andric }
728444ed5c5SDimitry Andric return Changed;
7297d523365SDimitry Andric }
7307d523365SDimitry Andric
7317d523365SDimitry Andric /// Calculate the liveness information for the given machine function and
7327d523365SDimitry Andric /// extend ranges across basic blocks.
ExtendRanges(MachineFunction & MF)7337d523365SDimitry Andric bool LiveDebugValues::ExtendRanges(MachineFunction &MF) {
7344ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "\nDebug Range Extension\n");
7357d523365SDimitry Andric
7367d523365SDimitry Andric bool Changed = false;
737444ed5c5SDimitry Andric bool OLChanged = false;
738444ed5c5SDimitry Andric bool MBBJoined = false;
7397d523365SDimitry Andric
7403ca95b02SDimitry Andric VarLocMap VarLocIDs; // Map VarLoc<>unique ID for use in bitvectors.
7413ca95b02SDimitry Andric OpenRangesSet OpenRanges; // Ranges that are open until end of bb.
7427d523365SDimitry Andric VarLocInMBB OutLocs; // Ranges that exist beyond bb.
7437d523365SDimitry Andric VarLocInMBB InLocs; // Ranges that are incoming after joining.
7444ba319b5SDimitry Andric TransferMap Transfers; // DBG_VALUEs associated with spills.
7457d523365SDimitry Andric
746*b5893f02SDimitry Andric // Blocks which are artificial, i.e. blocks which exclusively contain
747*b5893f02SDimitry Andric // instructions without locations, or with line 0 locations.
748*b5893f02SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> ArtificialBlocks;
749*b5893f02SDimitry Andric
750444ed5c5SDimitry Andric DenseMap<unsigned int, MachineBasicBlock *> OrderToBB;
751444ed5c5SDimitry Andric DenseMap<MachineBasicBlock *, unsigned int> BBToOrder;
752444ed5c5SDimitry Andric std::priority_queue<unsigned int, std::vector<unsigned int>,
7533ca95b02SDimitry Andric std::greater<unsigned int>>
7543ca95b02SDimitry Andric Worklist;
755444ed5c5SDimitry Andric std::priority_queue<unsigned int, std::vector<unsigned int>,
7563ca95b02SDimitry Andric std::greater<unsigned int>>
7573ca95b02SDimitry Andric Pending;
7583ca95b02SDimitry Andric
7594ba319b5SDimitry Andric enum : bool { dontTransferChanges = false, transferChanges = true };
7604ba319b5SDimitry Andric
7617d523365SDimitry Andric // Initialize every mbb with OutLocs.
7627a7e6055SDimitry Andric // We are not looking at any spill instructions during the initial pass
7637a7e6055SDimitry Andric // over the BBs. The LiveDebugVariables pass has already created DBG_VALUE
7647a7e6055SDimitry Andric // instructions for spills of registers that are known to be user variables
7657a7e6055SDimitry Andric // within the BB in which the spill occurs.
7667d523365SDimitry Andric for (auto &MBB : MF)
7677d523365SDimitry Andric for (auto &MI : MBB)
7684ba319b5SDimitry Andric process(MI, OpenRanges, OutLocs, VarLocIDs, Transfers,
7694ba319b5SDimitry Andric dontTransferChanges);
7703ca95b02SDimitry Andric
771*b5893f02SDimitry Andric auto hasNonArtificialLocation = [](const MachineInstr &MI) -> bool {
772*b5893f02SDimitry Andric if (const DebugLoc &DL = MI.getDebugLoc())
773*b5893f02SDimitry Andric return DL.getLine() != 0;
774*b5893f02SDimitry Andric return false;
775*b5893f02SDimitry Andric };
776*b5893f02SDimitry Andric for (auto &MBB : MF)
777*b5893f02SDimitry Andric if (none_of(MBB.instrs(), hasNonArtificialLocation))
778*b5893f02SDimitry Andric ArtificialBlocks.insert(&MBB);
779*b5893f02SDimitry Andric
7804ba319b5SDimitry Andric LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,
7814ba319b5SDimitry Andric "OutLocs after initialization", dbgs()));
7827d523365SDimitry Andric
783444ed5c5SDimitry Andric ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
784444ed5c5SDimitry Andric unsigned int RPONumber = 0;
785444ed5c5SDimitry Andric for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
786444ed5c5SDimitry Andric OrderToBB[RPONumber] = *RI;
787444ed5c5SDimitry Andric BBToOrder[*RI] = RPONumber;
788444ed5c5SDimitry Andric Worklist.push(RPONumber);
789444ed5c5SDimitry Andric ++RPONumber;
790444ed5c5SDimitry Andric }
791444ed5c5SDimitry Andric // This is a standard "union of predecessor outs" dataflow problem.
7924ba319b5SDimitry Andric // To solve it, we perform join() and process() using the two worklist method
793444ed5c5SDimitry Andric // until the ranges converge.
794444ed5c5SDimitry Andric // Ranges have converged when both worklists are empty.
795d88c1a5aSDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> Visited;
796444ed5c5SDimitry Andric while (!Worklist.empty() || !Pending.empty()) {
797444ed5c5SDimitry Andric // We track what is on the pending worklist to avoid inserting the same
798444ed5c5SDimitry Andric // thing twice. We could avoid this with a custom priority queue, but this
799444ed5c5SDimitry Andric // is probably not worth it.
800444ed5c5SDimitry Andric SmallPtrSet<MachineBasicBlock *, 16> OnPending;
8014ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Processing Worklist\n");
802444ed5c5SDimitry Andric while (!Worklist.empty()) {
803444ed5c5SDimitry Andric MachineBasicBlock *MBB = OrderToBB[Worklist.top()];
804444ed5c5SDimitry Andric Worklist.pop();
805*b5893f02SDimitry Andric MBBJoined =
806*b5893f02SDimitry Andric join(*MBB, OutLocs, InLocs, VarLocIDs, Visited, ArtificialBlocks);
807d88c1a5aSDimitry Andric Visited.insert(MBB);
8087d523365SDimitry Andric if (MBBJoined) {
809444ed5c5SDimitry Andric MBBJoined = false;
8107d523365SDimitry Andric Changed = true;
8117a7e6055SDimitry Andric // Now that we have started to extend ranges across BBs we need to
8127a7e6055SDimitry Andric // examine spill instructions to see whether they spill registers that
8137a7e6055SDimitry Andric // correspond to user variables.
8147d523365SDimitry Andric for (auto &MI : *MBB)
8154ba319b5SDimitry Andric OLChanged |= process(MI, OpenRanges, OutLocs, VarLocIDs, Transfers,
8164ba319b5SDimitry Andric transferChanges);
8177a7e6055SDimitry Andric
8187a7e6055SDimitry Andric // Add any DBG_VALUE instructions necessitated by spills.
8194ba319b5SDimitry Andric for (auto &TR : Transfers)
8204ba319b5SDimitry Andric MBB->insertAfter(MachineBasicBlock::iterator(*TR.TransferInst),
8214ba319b5SDimitry Andric TR.DebugInst);
8224ba319b5SDimitry Andric Transfers.clear();
8233ca95b02SDimitry Andric
8244ba319b5SDimitry Andric LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,
8253ca95b02SDimitry Andric "OutLocs after propagating", dbgs()));
8264ba319b5SDimitry Andric LLVM_DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs,
8273ca95b02SDimitry Andric "InLocs after propagating", dbgs()));
8287d523365SDimitry Andric
8297d523365SDimitry Andric if (OLChanged) {
8307d523365SDimitry Andric OLChanged = false;
8317d523365SDimitry Andric for (auto s : MBB->successors())
8323ca95b02SDimitry Andric if (OnPending.insert(s).second) {
833444ed5c5SDimitry Andric Pending.push(BBToOrder[s]);
8347d523365SDimitry Andric }
8357d523365SDimitry Andric }
8367d523365SDimitry Andric }
837444ed5c5SDimitry Andric }
838444ed5c5SDimitry Andric Worklist.swap(Pending);
839444ed5c5SDimitry Andric // At this point, pending must be empty, since it was just the empty
840444ed5c5SDimitry Andric // worklist
841444ed5c5SDimitry Andric assert(Pending.empty() && "Pending should be empty");
842444ed5c5SDimitry Andric }
843444ed5c5SDimitry Andric
8444ba319b5SDimitry Andric LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "Final OutLocs", dbgs()));
8454ba319b5SDimitry Andric LLVM_DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, "Final InLocs", dbgs()));
8467d523365SDimitry Andric return Changed;
8477d523365SDimitry Andric }
8487d523365SDimitry Andric
runOnMachineFunction(MachineFunction & MF)8497d523365SDimitry Andric bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
8502cab237bSDimitry Andric if (!MF.getFunction().getSubprogram())
851d88c1a5aSDimitry Andric // LiveDebugValues will already have removed all DBG_VALUEs.
852d88c1a5aSDimitry Andric return false;
853d88c1a5aSDimitry Andric
8542cab237bSDimitry Andric // Skip functions from NoDebug compilation units.
8552cab237bSDimitry Andric if (MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() ==
8562cab237bSDimitry Andric DICompileUnit::NoDebug)
8572cab237bSDimitry Andric return false;
8582cab237bSDimitry Andric
8597d523365SDimitry Andric TRI = MF.getSubtarget().getRegisterInfo();
8607d523365SDimitry Andric TII = MF.getSubtarget().getInstrInfo();
8617a7e6055SDimitry Andric TFI = MF.getSubtarget().getFrameLowering();
8624ba319b5SDimitry Andric TFI->determineCalleeSaves(MF, CalleeSavedRegs,
8634ba319b5SDimitry Andric make_unique<RegScavenger>().get());
864d88c1a5aSDimitry Andric LS.initialize(MF);
8657d523365SDimitry Andric
866d88c1a5aSDimitry Andric bool Changed = ExtendRanges(MF);
8677d523365SDimitry Andric return Changed;
8687d523365SDimitry Andric }
869