Wolfgang Pieb | e018bbd | 2017-07-19 19:36:40 +0000 | [diff] [blame] | 1 | //===------ LiveDebugValues.cpp - Tracking Debug Value MIs ----------------===//G |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// This pass implements a data flow analysis that propagates debug location |
| 11 | /// information by inserting additional DBG_VALUE instructions into the machine |
| 12 | /// instruction stream. The pass internally builds debug location liveness |
| 13 | /// ranges to determine the points where additional DBG_VALUEs need to be |
| 14 | /// inserted. |
| 15 | /// |
| 16 | /// This is a separate pass from DbgValueHistoryCalculator to facilitate |
| 17 | /// testing and improve modularity. |
| 18 | /// |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/PostOrderIterator.h" |
| 22 | #include "llvm/ADT/SmallPtrSet.h" |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SparseBitVector.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Statistic.h" |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/UniqueVector.h" |
Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/LexicalScopes.h" |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineFunction.h" |
| 29 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 30 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineMemOperand.h" |
Adrian Prantl | 83ca4fc | 2017-08-02 00:16:56 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/Passes.h" |
Reid Kleckner | 2886580 | 2016-04-14 18:29:59 +0000 | [diff] [blame] | 34 | #include "llvm/IR/DebugInfo.h" |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
| 36 | #include "llvm/Support/raw_ostream.h" |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetFrameLowering.h" |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetInstrInfo.h" |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetLowering.h" |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 40 | #include "llvm/Target/TargetRegisterInfo.h" |
| 41 | #include "llvm/Target/TargetSubtargetInfo.h" |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 42 | #include <list> |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 43 | #include <queue> |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 44 | |
| 45 | using namespace llvm; |
| 46 | |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 47 | #define DEBUG_TYPE "livedebugvalues" |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 48 | |
| 49 | STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted"); |
| 50 | |
| 51 | namespace { |
| 52 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 53 | // \brief If @MI is a DBG_VALUE with debug value described by a defined |
| 54 | // register, returns the number of this register. In the other case, returns 0. |
Adrian Prantl | 0069873 | 2016-05-25 22:37:29 +0000 | [diff] [blame] | 55 | static unsigned isDbgValueDescribedByReg(const MachineInstr &MI) { |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 56 | assert(MI.isDebugValue() && "expected a DBG_VALUE"); |
| 57 | assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE"); |
| 58 | // If location of variable is described using a register (directly |
| 59 | // or indirectly), this register is always a first operand. |
| 60 | return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0; |
| 61 | } |
| 62 | |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 63 | class LiveDebugValues : public MachineFunctionPass { |
| 64 | |
| 65 | private: |
| 66 | const TargetRegisterInfo *TRI; |
| 67 | const TargetInstrInfo *TII; |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 68 | const TargetFrameLowering *TFI; |
Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 69 | LexicalScopes LS; |
| 70 | |
| 71 | /// Keeps track of lexical scopes associated with a user value's source |
| 72 | /// location. |
| 73 | class UserValueScopes { |
| 74 | DebugLoc DL; |
| 75 | LexicalScopes &LS; |
| 76 | SmallPtrSet<const MachineBasicBlock *, 4> LBlocks; |
| 77 | |
| 78 | public: |
| 79 | UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(std::move(D)), LS(L) {} |
| 80 | |
| 81 | /// Return true if current scope dominates at least one machine |
| 82 | /// instruction in a given machine basic block. |
| 83 | bool dominates(MachineBasicBlock *MBB) { |
| 84 | if (LBlocks.empty()) |
| 85 | LS.getMachineBasicBlocks(DL, LBlocks); |
| 86 | return LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB); |
| 87 | } |
| 88 | }; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 89 | |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 90 | /// Based on std::pair so it can be used as an index into a DenseMap. |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 91 | typedef std::pair<const DILocalVariable *, const DILocation *> |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 92 | DebugVariableBase; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 93 | /// A potentially inlined instance of a variable. |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 94 | struct DebugVariable : public DebugVariableBase { |
| 95 | DebugVariable(const DILocalVariable *Var, const DILocation *InlinedAt) |
| 96 | : DebugVariableBase(Var, InlinedAt) {} |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 97 | |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 98 | const DILocalVariable *getVar() const { return this->first; }; |
| 99 | const DILocation *getInlinedAt() const { return this->second; }; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 100 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 101 | bool operator<(const DebugVariable &DV) const { |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 102 | if (getVar() == DV.getVar()) |
| 103 | return getInlinedAt() < DV.getInlinedAt(); |
| 104 | return getVar() < DV.getVar(); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 105 | } |
| 106 | }; |
| 107 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 108 | /// A pair of debug variable and value location. |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 109 | struct VarLoc { |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 110 | const DebugVariable Var; |
| 111 | const MachineInstr &MI; ///< Only used for cloning a new DBG_VALUE. |
Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 112 | mutable UserValueScopes UVS; |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 113 | enum { InvalidKind = 0, RegisterKind } Kind; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 114 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 115 | /// The value location. Stored separately to avoid repeatedly |
| 116 | /// extracting it from MI. |
| 117 | union { |
Adrian Prantl | 359846f | 2017-07-28 23:25:51 +0000 | [diff] [blame] | 118 | uint64_t RegNo; |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 119 | uint64_t Hash; |
| 120 | } Loc; |
| 121 | |
Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 122 | VarLoc(const MachineInstr &MI, LexicalScopes &LS) |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 123 | : Var(MI.getDebugVariable(), MI.getDebugLoc()->getInlinedAt()), MI(MI), |
Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 124 | UVS(MI.getDebugLoc(), LS), Kind(InvalidKind) { |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 125 | static_assert((sizeof(Loc) == sizeof(uint64_t)), |
| 126 | "hash does not cover all members of Loc"); |
| 127 | assert(MI.isDebugValue() && "not a DBG_VALUE"); |
| 128 | assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE"); |
Adrian Prantl | 0069873 | 2016-05-25 22:37:29 +0000 | [diff] [blame] | 129 | if (int RegNo = isDbgValueDescribedByReg(MI)) { |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 130 | Kind = RegisterKind; |
Adrian Prantl | 359846f | 2017-07-28 23:25:51 +0000 | [diff] [blame] | 131 | Loc.RegNo = RegNo; |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | /// If this variable is described by a register, return it, |
| 136 | /// otherwise return 0. |
| 137 | unsigned isDescribedByReg() const { |
| 138 | if (Kind == RegisterKind) |
Adrian Prantl | 359846f | 2017-07-28 23:25:51 +0000 | [diff] [blame] | 139 | return Loc.RegNo; |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 143 | /// Determine whether the lexical scope of this value's debug location |
| 144 | /// dominates MBB. |
| 145 | bool dominates(MachineBasicBlock &MBB) const { return UVS.dominates(&MBB); } |
| 146 | |
Matthias Braun | 194ded5 | 2017-01-28 06:53:55 +0000 | [diff] [blame] | 147 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 148 | LLVM_DUMP_METHOD void dump() const { MI.dump(); } |
| 149 | #endif |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 150 | |
| 151 | bool operator==(const VarLoc &Other) const { |
| 152 | return Var == Other.Var && Loc.Hash == Other.Loc.Hash; |
| 153 | } |
| 154 | |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 155 | /// This operator guarantees that VarLocs are sorted by Variable first. |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 156 | bool operator<(const VarLoc &Other) const { |
| 157 | if (Var == Other.Var) |
| 158 | return Loc.Hash < Other.Loc.Hash; |
| 159 | return Var < Other.Var; |
| 160 | } |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 163 | typedef UniqueVector<VarLoc> VarLocMap; |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 164 | typedef SparseBitVector<> VarLocSet; |
| 165 | typedef SmallDenseMap<const MachineBasicBlock *, VarLocSet> VarLocInMBB; |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 166 | struct SpillDebugPair { |
| 167 | MachineInstr *SpillInst; |
| 168 | MachineInstr *DebugInst; |
| 169 | }; |
| 170 | typedef SmallVector<SpillDebugPair, 4> SpillMap; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 171 | |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 172 | /// This holds the working set of currently open ranges. For fast |
| 173 | /// access, this is done both as a set of VarLocIDs, and a map of |
| 174 | /// DebugVariable to recent VarLocID. Note that a DBG_VALUE ends all |
| 175 | /// previous open ranges for the same variable. |
| 176 | class OpenRangesSet { |
| 177 | VarLocSet VarLocs; |
| 178 | SmallDenseMap<DebugVariableBase, unsigned, 8> Vars; |
| 179 | |
| 180 | public: |
| 181 | const VarLocSet &getVarLocs() const { return VarLocs; } |
| 182 | |
| 183 | /// Terminate all open ranges for Var by removing it from the set. |
| 184 | void erase(DebugVariable Var) { |
| 185 | auto It = Vars.find(Var); |
| 186 | if (It != Vars.end()) { |
| 187 | unsigned ID = It->second; |
| 188 | VarLocs.reset(ID); |
| 189 | Vars.erase(It); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /// Terminate all open ranges listed in \c KillSet by removing |
| 194 | /// them from the set. |
| 195 | void erase(const VarLocSet &KillSet, const VarLocMap &VarLocIDs) { |
| 196 | VarLocs.intersectWithComplement(KillSet); |
| 197 | for (unsigned ID : KillSet) |
| 198 | Vars.erase(VarLocIDs[ID].Var); |
| 199 | } |
| 200 | |
| 201 | /// Insert a new range into the set. |
| 202 | void insert(unsigned VarLocID, DebugVariableBase Var) { |
| 203 | VarLocs.set(VarLocID); |
| 204 | Vars.insert({Var, VarLocID}); |
| 205 | } |
| 206 | |
| 207 | /// Empty the set. |
| 208 | void clear() { |
| 209 | VarLocs.clear(); |
| 210 | Vars.clear(); |
| 211 | } |
| 212 | |
| 213 | /// Return whether the set is empty or not. |
| 214 | bool empty() const { |
| 215 | assert(Vars.empty() == VarLocs.empty() && "open ranges are inconsistent"); |
| 216 | return VarLocs.empty(); |
| 217 | } |
| 218 | }; |
| 219 | |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 220 | bool isSpillInstruction(const MachineInstr &MI, MachineFunction *MF, |
| 221 | unsigned &Reg); |
| 222 | int extractSpillBaseRegAndOffset(const MachineInstr &MI, unsigned &Reg); |
| 223 | |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 224 | void transferDebugValue(const MachineInstr &MI, OpenRangesSet &OpenRanges, |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 225 | VarLocMap &VarLocIDs); |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 226 | void transferSpillInst(MachineInstr &MI, OpenRangesSet &OpenRanges, |
| 227 | VarLocMap &VarLocIDs, SpillMap &Spills); |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 228 | void transferRegisterDef(MachineInstr &MI, OpenRangesSet &OpenRanges, |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 229 | const VarLocMap &VarLocIDs); |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 230 | bool transferTerminatorInst(MachineInstr &MI, OpenRangesSet &OpenRanges, |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 231 | VarLocInMBB &OutLocs, const VarLocMap &VarLocIDs); |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 232 | bool transfer(MachineInstr &MI, OpenRangesSet &OpenRanges, |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 233 | VarLocInMBB &OutLocs, VarLocMap &VarLocIDs, SpillMap &Spills, |
| 234 | bool transferSpills); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 235 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 236 | bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs, |
Keith Walker | 83ebef5 | 2016-09-27 16:46:07 +0000 | [diff] [blame] | 237 | const VarLocMap &VarLocIDs, |
| 238 | SmallPtrSet<const MachineBasicBlock *, 16> &Visited); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 239 | |
| 240 | bool ExtendRanges(MachineFunction &MF); |
| 241 | |
| 242 | public: |
| 243 | static char ID; |
| 244 | |
| 245 | /// Default construct and initialize the pass. |
| 246 | LiveDebugValues(); |
| 247 | |
| 248 | /// Tell the pass manager which passes we depend on and what |
| 249 | /// information we preserve. |
| 250 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 251 | |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 252 | MachineFunctionProperties getRequiredProperties() const override { |
| 253 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 254 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 257 | /// Print to ostream with a message. |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 258 | void printVarLocInMBB(const MachineFunction &MF, const VarLocInMBB &V, |
| 259 | const VarLocMap &VarLocIDs, const char *msg, |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 260 | raw_ostream &Out) const; |
| 261 | |
| 262 | /// Calculate the liveness information for the given machine function. |
| 263 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 264 | }; |
Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 265 | |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 266 | } // namespace |
| 267 | |
| 268 | //===----------------------------------------------------------------------===// |
| 269 | // Implementation |
| 270 | //===----------------------------------------------------------------------===// |
| 271 | |
| 272 | char LiveDebugValues::ID = 0; |
| 273 | char &llvm::LiveDebugValuesID = LiveDebugValues::ID; |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 274 | INITIALIZE_PASS(LiveDebugValues, DEBUG_TYPE, "Live DEBUG_VALUE analysis", |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 275 | false, false) |
| 276 | |
| 277 | /// Default construct and initialize the pass. |
| 278 | LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) { |
| 279 | initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry()); |
| 280 | } |
| 281 | |
| 282 | /// Tell the pass manager which passes we depend on and what information we |
| 283 | /// preserve. |
| 284 | void LiveDebugValues::getAnalysisUsage(AnalysisUsage &AU) const { |
Matt Arsenault | b1630a1 | 2016-06-08 05:18:01 +0000 | [diff] [blame] | 285 | AU.setPreservesCFG(); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 286 | MachineFunctionPass::getAnalysisUsage(AU); |
| 287 | } |
| 288 | |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 289 | //===----------------------------------------------------------------------===// |
| 290 | // Debug Range Extension Implementation |
| 291 | //===----------------------------------------------------------------------===// |
| 292 | |
Matthias Braun | 194ded5 | 2017-01-28 06:53:55 +0000 | [diff] [blame] | 293 | #ifndef NDEBUG |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 294 | void LiveDebugValues::printVarLocInMBB(const MachineFunction &MF, |
| 295 | const VarLocInMBB &V, |
| 296 | const VarLocMap &VarLocIDs, |
| 297 | const char *msg, |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 298 | raw_ostream &Out) const { |
Keith Walker | f83a19f | 2016-09-20 16:04:31 +0000 | [diff] [blame] | 299 | Out << '\n' << msg << '\n'; |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 300 | for (const MachineBasicBlock &BB : MF) { |
| 301 | const auto &L = V.lookup(&BB); |
| 302 | Out << "MBB: " << BB.getName() << ":\n"; |
| 303 | for (unsigned VLL : L) { |
| 304 | const VarLoc &VL = VarLocIDs[VLL]; |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 305 | Out << " Var: " << VL.Var.getVar()->getName(); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 306 | Out << " MI: "; |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 307 | VL.dump(); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | Out << "\n"; |
| 311 | } |
Matthias Braun | 194ded5 | 2017-01-28 06:53:55 +0000 | [diff] [blame] | 312 | #endif |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 313 | |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 314 | /// Given a spill instruction, extract the register and offset used to |
| 315 | /// address the spill location in a target independent way. |
| 316 | int LiveDebugValues::extractSpillBaseRegAndOffset(const MachineInstr &MI, |
| 317 | unsigned &Reg) { |
| 318 | assert(MI.hasOneMemOperand() && |
| 319 | "Spill instruction does not have exactly one memory operand?"); |
| 320 | auto MMOI = MI.memoperands_begin(); |
| 321 | const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue(); |
| 322 | assert(PVal->kind() == PseudoSourceValue::FixedStack && |
| 323 | "Inconsistent memory operand in spill instruction"); |
| 324 | int FI = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex(); |
| 325 | const MachineBasicBlock *MBB = MI.getParent(); |
| 326 | return TFI->getFrameIndexReference(*MBB->getParent(), FI, Reg); |
| 327 | } |
| 328 | |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 329 | /// End all previous ranges related to @MI and start a new range from @MI |
| 330 | /// if it is a DBG_VALUE instr. |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 331 | void LiveDebugValues::transferDebugValue(const MachineInstr &MI, |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 332 | OpenRangesSet &OpenRanges, |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 333 | VarLocMap &VarLocIDs) { |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 334 | if (!MI.isDebugValue()) |
| 335 | return; |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 336 | const DILocalVariable *Var = MI.getDebugVariable(); |
| 337 | const DILocation *DebugLoc = MI.getDebugLoc(); |
| 338 | const DILocation *InlinedAt = DebugLoc->getInlinedAt(); |
| 339 | assert(Var->isValidLocationForIntrinsic(DebugLoc) && |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 340 | "Expected inlined-at fields to agree"); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 341 | |
| 342 | // End all previous ranges of Var. |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 343 | DebugVariable V(Var, InlinedAt); |
| 344 | OpenRanges.erase(V); |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 345 | |
| 346 | // Add the VarLoc to OpenRanges from this DBG_VALUE. |
| 347 | // TODO: Currently handles DBG_VALUE which has only reg as location. |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 348 | if (isDbgValueDescribedByReg(MI)) { |
Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 349 | VarLoc VL(MI, LS); |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 350 | unsigned ID = VarLocIDs.insert(VL); |
| 351 | OpenRanges.insert(ID, VL.Var); |
| 352 | } |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /// A definition of a register may mark the end of a range. |
| 356 | void LiveDebugValues::transferRegisterDef(MachineInstr &MI, |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 357 | OpenRangesSet &OpenRanges, |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 358 | const VarLocMap &VarLocIDs) { |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 359 | MachineFunction *MF = MI.getParent()->getParent(); |
| 360 | const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); |
| 361 | unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 362 | SparseBitVector<> KillSet; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 363 | for (const MachineOperand &MO : MI.operands()) { |
Adrian Prantl | ea8880b | 2017-03-03 01:08:25 +0000 | [diff] [blame] | 364 | // Determine whether the operand is a register def. Assume that call |
| 365 | // instructions never clobber SP, because some backends (e.g., AArch64) |
| 366 | // never list SP in the regmask. |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 367 | if (MO.isReg() && MO.isDef() && MO.getReg() && |
Adrian Prantl | ea8880b | 2017-03-03 01:08:25 +0000 | [diff] [blame] | 368 | TRI->isPhysicalRegister(MO.getReg()) && |
| 369 | !(MI.isCall() && MO.getReg() == SP)) { |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 370 | // Remove ranges of all aliased registers. |
| 371 | for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI) |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 372 | for (unsigned ID : OpenRanges.getVarLocs()) |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 373 | if (VarLocIDs[ID].isDescribedByReg() == *RAI) |
| 374 | KillSet.set(ID); |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 375 | } else if (MO.isRegMask()) { |
| 376 | // Remove ranges of all clobbered registers. Register masks don't usually |
| 377 | // list SP as preserved. While the debug info may be off for an |
| 378 | // instruction or two around callee-cleanup calls, transferring the |
| 379 | // DEBUG_VALUE across the call is still a better user experience. |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 380 | for (unsigned ID : OpenRanges.getVarLocs()) { |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 381 | unsigned Reg = VarLocIDs[ID].isDescribedByReg(); |
| 382 | if (Reg && Reg != SP && MO.clobbersPhysReg(Reg)) |
| 383 | KillSet.set(ID); |
| 384 | } |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 385 | } |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 386 | } |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 387 | OpenRanges.erase(KillSet, VarLocIDs); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 390 | /// Decide if @MI is a spill instruction and return true if it is. We use 2 |
| 391 | /// criteria to make this decision: |
| 392 | /// - Is this instruction a store to a spill slot? |
| 393 | /// - Is there a register operand that is both used and killed? |
| 394 | /// TODO: Store optimization can fold spills into other stores (including |
| 395 | /// other spills). We do not handle this yet (more than one memory operand). |
| 396 | bool LiveDebugValues::isSpillInstruction(const MachineInstr &MI, |
| 397 | MachineFunction *MF, unsigned &Reg) { |
| 398 | const MachineFrameInfo &FrameInfo = MF->getFrameInfo(); |
| 399 | int FI; |
| 400 | const MachineMemOperand *MMO; |
| 401 | |
| 402 | // TODO: Handle multiple stores folded into one. |
| 403 | if (!MI.hasOneMemOperand()) |
| 404 | return false; |
| 405 | |
| 406 | // To identify a spill instruction, use the same criteria as in AsmPrinter. |
| 407 | if (!((TII->isStoreToStackSlotPostFE(MI, FI) || |
| 408 | TII->hasStoreToStackSlot(MI, MMO, FI)) && |
| 409 | FrameInfo.isSpillSlotObjectIndex(FI))) |
| 410 | return false; |
| 411 | |
| 412 | // In a spill instruction generated by the InlineSpiller the spilled register |
| 413 | // has its kill flag set. Return false if we don't find such a register. |
| 414 | Reg = 0; |
| 415 | for (const MachineOperand &MO : MI.operands()) { |
| 416 | if (MO.isReg() && MO.isUse() && MO.isKill()) { |
| 417 | Reg = MO.getReg(); |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 | return Reg != 0; |
| 422 | } |
| 423 | |
| 424 | /// A spilled register may indicate that we have to end the current range of |
| 425 | /// a variable and create a new one for the spill location. |
| 426 | /// We don't want to insert any instructions in transfer(), so we just create |
| 427 | /// the DBG_VALUE witout inserting it and keep track of it in @Spills. |
| 428 | /// It will be inserted into the BB when we're done iterating over the |
| 429 | /// instructions. |
| 430 | void LiveDebugValues::transferSpillInst(MachineInstr &MI, |
| 431 | OpenRangesSet &OpenRanges, |
| 432 | VarLocMap &VarLocIDs, |
| 433 | SpillMap &Spills) { |
| 434 | unsigned Reg; |
| 435 | MachineFunction *MF = MI.getParent()->getParent(); |
| 436 | if (!isSpillInstruction(MI, MF, Reg)) |
| 437 | return; |
| 438 | |
| 439 | // Check if the register is the location of a debug value. |
| 440 | for (unsigned ID : OpenRanges.getVarLocs()) { |
| 441 | if (VarLocIDs[ID].isDescribedByReg() == Reg) { |
| 442 | DEBUG(dbgs() << "Spilling Register " << PrintReg(Reg, TRI) << '(' |
| 443 | << VarLocIDs[ID].Var.getVar()->getName() << ")\n"); |
| 444 | |
| 445 | // Create a DBG_VALUE instruction to describe the Var in its spilled |
| 446 | // location, but don't insert it yet to avoid invalidating the |
| 447 | // iterator in our caller. |
| 448 | unsigned SpillBase; |
| 449 | int SpillOffset = extractSpillBaseRegAndOffset(MI, SpillBase); |
| 450 | const MachineInstr *DMI = &VarLocIDs[ID].MI; |
Adrian Prantl | 83ca4fc | 2017-08-02 00:16:56 +0000 | [diff] [blame] | 451 | auto *SpillExpr = DIExpression::prepend( |
| 452 | DMI->getDebugExpression(), DIExpression::NoDeref, SpillOffset); |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 453 | MachineInstr *SpDMI = |
Adrian Prantl | 8b9bb53 | 2017-07-28 23:00:45 +0000 | [diff] [blame] | 454 | BuildMI(*MF, DMI->getDebugLoc(), DMI->getDesc(), true, SpillBase, |
Adrian Prantl | 83ca4fc | 2017-08-02 00:16:56 +0000 | [diff] [blame] | 455 | DMI->getDebugVariable(), SpillExpr); |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 456 | DEBUG(dbgs() << "Creating DBG_VALUE inst for spill: "; |
| 457 | SpDMI->print(dbgs(), false, TII)); |
| 458 | |
| 459 | // The newly created DBG_VALUE instruction SpDMI must be inserted after |
| 460 | // MI. Keep track of the pairing. |
| 461 | SpillDebugPair MIP = {&MI, SpDMI}; |
| 462 | Spills.push_back(MIP); |
| 463 | |
| 464 | // End all previous ranges of Var. |
| 465 | OpenRanges.erase(VarLocIDs[ID].Var); |
| 466 | |
| 467 | // Add the VarLoc to OpenRanges. |
| 468 | VarLoc VL(*SpDMI, LS); |
| 469 | unsigned SpillLocID = VarLocIDs.insert(VL); |
| 470 | OpenRanges.insert(SpillLocID, VL.Var); |
| 471 | return; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 476 | /// Terminate all open ranges at the end of the current basic block. |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 477 | bool LiveDebugValues::transferTerminatorInst(MachineInstr &MI, |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 478 | OpenRangesSet &OpenRanges, |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 479 | VarLocInMBB &OutLocs, |
| 480 | const VarLocMap &VarLocIDs) { |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 481 | bool Changed = false; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 482 | const MachineBasicBlock *CurMBB = MI.getParent(); |
| 483 | if (!(MI.isTerminator() || (&MI == &CurMBB->instr_back()))) |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 484 | return false; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 485 | |
| 486 | if (OpenRanges.empty()) |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 487 | return false; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 488 | |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 489 | DEBUG(for (unsigned ID : OpenRanges.getVarLocs()) { |
| 490 | // Copy OpenRanges to OutLocs, if not already present. |
| 491 | dbgs() << "Add to OutLocs: "; VarLocIDs[ID].dump(); |
| 492 | }); |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 493 | VarLocSet &VLS = OutLocs[CurMBB]; |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 494 | Changed = VLS |= OpenRanges.getVarLocs(); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 495 | OpenRanges.clear(); |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 496 | return Changed; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /// This routine creates OpenRanges and OutLocs. |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 500 | bool LiveDebugValues::transfer(MachineInstr &MI, OpenRangesSet &OpenRanges, |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 501 | VarLocInMBB &OutLocs, VarLocMap &VarLocIDs, |
| 502 | SpillMap &Spills, bool transferSpills) { |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 503 | bool Changed = false; |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 504 | transferDebugValue(MI, OpenRanges, VarLocIDs); |
| 505 | transferRegisterDef(MI, OpenRanges, VarLocIDs); |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 506 | if (transferSpills) |
| 507 | transferSpillInst(MI, OpenRanges, VarLocIDs, Spills); |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 508 | Changed = transferTerminatorInst(MI, OpenRanges, OutLocs, VarLocIDs); |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 509 | return Changed; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /// This routine joins the analysis results of all incoming edges in @MBB by |
| 513 | /// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same |
| 514 | /// source variable in all the predecessors of @MBB reside in the same location. |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 515 | bool LiveDebugValues::join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, |
Keith Walker | 83ebef5 | 2016-09-27 16:46:07 +0000 | [diff] [blame] | 516 | VarLocInMBB &InLocs, const VarLocMap &VarLocIDs, |
| 517 | SmallPtrSet<const MachineBasicBlock *, 16> &Visited) { |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 518 | DEBUG(dbgs() << "join MBB: " << MBB.getName() << "\n"); |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 519 | bool Changed = false; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 520 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 521 | VarLocSet InLocsT; // Temporary incoming locations. |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 522 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 523 | // For all predecessors of this MBB, find the set of VarLocs that |
| 524 | // can be joined. |
Keith Walker | 83ebef5 | 2016-09-27 16:46:07 +0000 | [diff] [blame] | 525 | int NumVisited = 0; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 526 | for (auto p : MBB.predecessors()) { |
Keith Walker | 83ebef5 | 2016-09-27 16:46:07 +0000 | [diff] [blame] | 527 | // Ignore unvisited predecessor blocks. As we are processing |
| 528 | // the blocks in reverse post-order any unvisited block can |
| 529 | // be considered to not remove any incoming values. |
| 530 | if (!Visited.count(p)) |
| 531 | continue; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 532 | auto OL = OutLocs.find(p); |
| 533 | // Join is null in case of empty OutLocs from any of the pred. |
| 534 | if (OL == OutLocs.end()) |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 535 | return false; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 536 | |
Keith Walker | 83ebef5 | 2016-09-27 16:46:07 +0000 | [diff] [blame] | 537 | // Just copy over the Out locs to incoming locs for the first visited |
| 538 | // predecessor, and for all other predecessors join the Out locs. |
| 539 | if (!NumVisited) |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 540 | InLocsT = OL->second; |
Keith Walker | 83ebef5 | 2016-09-27 16:46:07 +0000 | [diff] [blame] | 541 | else |
| 542 | InLocsT &= OL->second; |
| 543 | NumVisited++; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 546 | // Filter out DBG_VALUES that are out of scope. |
| 547 | VarLocSet KillSet; |
| 548 | for (auto ID : InLocsT) |
| 549 | if (!VarLocIDs[ID].dominates(MBB)) |
| 550 | KillSet.set(ID); |
| 551 | InLocsT.intersectWithComplement(KillSet); |
| 552 | |
Keith Walker | 83ebef5 | 2016-09-27 16:46:07 +0000 | [diff] [blame] | 553 | // As we are processing blocks in reverse post-order we |
| 554 | // should have processed at least one predecessor, unless it |
| 555 | // is the entry block which has no predecessor. |
| 556 | assert((NumVisited || MBB.pred_empty()) && |
| 557 | "Should have processed at least one predecessor"); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 558 | if (InLocsT.empty()) |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 559 | return false; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 560 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 561 | VarLocSet &ILS = InLocs[&MBB]; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 562 | |
| 563 | // Insert DBG_VALUE instructions, if not already inserted. |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 564 | VarLocSet Diff = InLocsT; |
| 565 | Diff.intersectWithComplement(ILS); |
| 566 | for (auto ID : Diff) { |
| 567 | // This VarLoc is not found in InLocs i.e. it is not yet inserted. So, a |
| 568 | // new range is started for the var from the mbb's beginning by inserting |
| 569 | // a new DBG_VALUE. transfer() will end this range however appropriate. |
| 570 | const VarLoc &DiffIt = VarLocIDs[ID]; |
| 571 | const MachineInstr *DMI = &DiffIt.MI; |
| 572 | MachineInstr *MI = |
| 573 | BuildMI(MBB, MBB.instr_begin(), DMI->getDebugLoc(), DMI->getDesc(), |
Adrian Prantl | 8b9bb53 | 2017-07-28 23:00:45 +0000 | [diff] [blame] | 574 | DMI->isIndirectDebugValue(), DMI->getOperand(0).getReg(), |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 575 | DMI->getDebugVariable(), DMI->getDebugExpression()); |
| 576 | if (DMI->isIndirectDebugValue()) |
| 577 | MI->getOperand(1).setImm(DMI->getOperand(1).getImm()); |
| 578 | DEBUG(dbgs() << "Inserted: "; MI->dump();); |
| 579 | ILS.set(ID); |
| 580 | ++NumInserted; |
| 581 | Changed = true; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 582 | } |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 583 | return Changed; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /// Calculate the liveness information for the given machine function and |
| 587 | /// extend ranges across basic blocks. |
| 588 | bool LiveDebugValues::ExtendRanges(MachineFunction &MF) { |
| 589 | |
| 590 | DEBUG(dbgs() << "\nDebug Range Extension\n"); |
| 591 | |
| 592 | bool Changed = false; |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 593 | bool OLChanged = false; |
| 594 | bool MBBJoined = false; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 595 | |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 596 | VarLocMap VarLocIDs; // Map VarLoc<>unique ID for use in bitvectors. |
Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 597 | OpenRangesSet OpenRanges; // Ranges that are open until end of bb. |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 598 | VarLocInMBB OutLocs; // Ranges that exist beyond bb. |
| 599 | VarLocInMBB InLocs; // Ranges that are incoming after joining. |
| 600 | SpillMap Spills; // DBG_VALUEs associated with spills. |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 601 | |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 602 | DenseMap<unsigned int, MachineBasicBlock *> OrderToBB; |
| 603 | DenseMap<MachineBasicBlock *, unsigned int> BBToOrder; |
| 604 | std::priority_queue<unsigned int, std::vector<unsigned int>, |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 605 | std::greater<unsigned int>> |
| 606 | Worklist; |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 607 | std::priority_queue<unsigned int, std::vector<unsigned int>, |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 608 | std::greater<unsigned int>> |
| 609 | Pending; |
| 610 | |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 611 | // Initialize every mbb with OutLocs. |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 612 | // We are not looking at any spill instructions during the initial pass |
| 613 | // over the BBs. The LiveDebugVariables pass has already created DBG_VALUE |
| 614 | // instructions for spills of registers that are known to be user variables |
| 615 | // within the BB in which the spill occurs. |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 616 | for (auto &MBB : MF) |
| 617 | for (auto &MI : MBB) |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 618 | transfer(MI, OpenRanges, OutLocs, VarLocIDs, Spills, |
| 619 | /*transferSpills=*/false); |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 620 | |
| 621 | DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "OutLocs after initialization", |
| 622 | dbgs())); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 623 | |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 624 | ReversePostOrderTraversal<MachineFunction *> RPOT(&MF); |
| 625 | unsigned int RPONumber = 0; |
| 626 | for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) { |
| 627 | OrderToBB[RPONumber] = *RI; |
| 628 | BBToOrder[*RI] = RPONumber; |
| 629 | Worklist.push(RPONumber); |
| 630 | ++RPONumber; |
| 631 | } |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 632 | // This is a standard "union of predecessor outs" dataflow problem. |
| 633 | // To solve it, we perform join() and transfer() using the two worklist method |
| 634 | // until the ranges converge. |
| 635 | // Ranges have converged when both worklists are empty. |
Keith Walker | 83ebef5 | 2016-09-27 16:46:07 +0000 | [diff] [blame] | 636 | SmallPtrSet<const MachineBasicBlock *, 16> Visited; |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 637 | while (!Worklist.empty() || !Pending.empty()) { |
| 638 | // We track what is on the pending worklist to avoid inserting the same |
| 639 | // thing twice. We could avoid this with a custom priority queue, but this |
| 640 | // is probably not worth it. |
| 641 | SmallPtrSet<MachineBasicBlock *, 16> OnPending; |
Keith Walker | f83a19f | 2016-09-20 16:04:31 +0000 | [diff] [blame] | 642 | DEBUG(dbgs() << "Processing Worklist\n"); |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 643 | while (!Worklist.empty()) { |
| 644 | MachineBasicBlock *MBB = OrderToBB[Worklist.top()]; |
| 645 | Worklist.pop(); |
Keith Walker | 83ebef5 | 2016-09-27 16:46:07 +0000 | [diff] [blame] | 646 | MBBJoined = join(*MBB, OutLocs, InLocs, VarLocIDs, Visited); |
| 647 | Visited.insert(MBB); |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 648 | if (MBBJoined) { |
| 649 | MBBJoined = false; |
| 650 | Changed = true; |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 651 | // Now that we have started to extend ranges across BBs we need to |
| 652 | // examine spill instructions to see whether they spill registers that |
| 653 | // correspond to user variables. |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 654 | for (auto &MI : *MBB) |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 655 | OLChanged |= transfer(MI, OpenRanges, OutLocs, VarLocIDs, Spills, |
| 656 | /*transferSpills=*/true); |
| 657 | |
| 658 | // Add any DBG_VALUE instructions necessitated by spills. |
| 659 | for (auto &SP : Spills) |
| 660 | MBB->insertAfter(MachineBasicBlock::iterator(*SP.SpillInst), |
| 661 | SP.DebugInst); |
| 662 | Spills.clear(); |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 663 | |
| 664 | DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, |
| 665 | "OutLocs after propagating", dbgs())); |
| 666 | DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, |
| 667 | "InLocs after propagating", dbgs())); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 668 | |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 669 | if (OLChanged) { |
| 670 | OLChanged = false; |
| 671 | for (auto s : MBB->successors()) |
Benjamin Kramer | 4dea8f5 | 2016-06-17 18:59:41 +0000 | [diff] [blame] | 672 | if (OnPending.insert(s).second) { |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 673 | Pending.push(BBToOrder[s]); |
| 674 | } |
| 675 | } |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 678 | Worklist.swap(Pending); |
| 679 | // At this point, pending must be empty, since it was just the empty |
| 680 | // worklist |
| 681 | assert(Pending.empty() && "Pending should be empty"); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 682 | } |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 683 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 684 | DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "Final OutLocs", dbgs())); |
| 685 | DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, "Final InLocs", dbgs())); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 686 | return Changed; |
| 687 | } |
| 688 | |
| 689 | bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) { |
Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 690 | if (!MF.getFunction()->getSubprogram()) |
| 691 | // LiveDebugValues will already have removed all DBG_VALUEs. |
| 692 | return false; |
| 693 | |
Wolfgang Pieb | e018bbd | 2017-07-19 19:36:40 +0000 | [diff] [blame] | 694 | // Skip functions from NoDebug compilation units. |
| 695 | if (MF.getFunction()->getSubprogram()->getUnit()->getEmissionKind() == |
| 696 | DICompileUnit::NoDebug) |
| 697 | return false; |
| 698 | |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 699 | TRI = MF.getSubtarget().getRegisterInfo(); |
| 700 | TII = MF.getSubtarget().getInstrInfo(); |
Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 701 | TFI = MF.getSubtarget().getFrameLowering(); |
Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 702 | LS.initialize(MF); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 703 | |
Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 704 | bool Changed = ExtendRanges(MF); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 705 | return Changed; |
| 706 | } |