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