Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1 | //===------ LiveDebugValues.cpp - Tracking Debug Value MIs ----------------===// |
| 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" |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineFunction.h" |
| 27 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 28 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 29 | #include "llvm/CodeGen/Passes.h" |
Reid Kleckner | 2886580 | 2016-04-14 18:29:59 +0000 | [diff] [blame] | 30 | #include "llvm/IR/DebugInfo.h" |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
| 33 | #include "llvm/Target/TargetInstrInfo.h" |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetLowering.h" |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetRegisterInfo.h" |
| 36 | #include "llvm/Target/TargetSubtargetInfo.h" |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 37 | #include <list> |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 38 | #include <queue> |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace llvm; |
| 41 | |
| 42 | #define DEBUG_TYPE "live-debug-values" |
| 43 | |
| 44 | STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted"); |
| 45 | |
| 46 | namespace { |
| 47 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 48 | // \brief If @MI is a DBG_VALUE with debug value described by a defined |
| 49 | // 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^] | 50 | static unsigned isDbgValueDescribedByReg(const MachineInstr &MI) { |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 51 | assert(MI.isDebugValue() && "expected a DBG_VALUE"); |
| 52 | assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE"); |
| 53 | // If location of variable is described using a register (directly |
| 54 | // or indirectly), this register is always a first operand. |
| 55 | return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0; |
| 56 | } |
| 57 | |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 58 | class LiveDebugValues : public MachineFunctionPass { |
| 59 | |
| 60 | private: |
| 61 | const TargetRegisterInfo *TRI; |
| 62 | const TargetInstrInfo *TII; |
| 63 | |
| 64 | typedef std::pair<const DILocalVariable *, const DILocation *> |
| 65 | InlinedVariable; |
| 66 | |
| 67 | /// A potentially inlined instance of a variable. |
| 68 | struct DebugVariable { |
| 69 | const DILocalVariable *Var; |
| 70 | const DILocation *InlinedAt; |
| 71 | |
| 72 | DebugVariable(const DILocalVariable *_var, const DILocation *_inlinedAt) |
| 73 | : Var(_var), InlinedAt(_inlinedAt) {} |
| 74 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 75 | bool operator<(const DebugVariable &DV) const { |
| 76 | if (Var == DV.Var) |
| 77 | return InlinedAt < DV.InlinedAt; |
| 78 | return Var < DV.Var; |
| 79 | } |
| 80 | |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 81 | bool operator==(const DebugVariable &DV) const { |
| 82 | return (Var == DV.Var) && (InlinedAt == DV.InlinedAt); |
| 83 | } |
| 84 | }; |
| 85 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 86 | /// A pair of debug variable and value location. |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 87 | struct VarLoc { |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 88 | const DebugVariable Var; |
| 89 | const MachineInstr &MI; ///< Only used for cloning a new DBG_VALUE. |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 90 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 91 | enum { InvalidKind = 0, RegisterKind } Kind; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 92 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 93 | /// The value location. Stored separately to avoid repeatedly |
| 94 | /// extracting it from MI. |
| 95 | union { |
| 96 | struct { |
| 97 | uint32_t RegNo; |
| 98 | uint32_t Offset; |
| 99 | } RegisterLoc; |
| 100 | uint64_t Hash; |
| 101 | } Loc; |
| 102 | |
| 103 | VarLoc(const MachineInstr &MI) |
| 104 | : Var(MI.getDebugVariable(), MI.getDebugLoc()->getInlinedAt()), MI(MI), |
| 105 | Kind(InvalidKind) { |
| 106 | static_assert((sizeof(Loc) == sizeof(uint64_t)), |
| 107 | "hash does not cover all members of Loc"); |
| 108 | assert(MI.isDebugValue() && "not a DBG_VALUE"); |
| 109 | assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE"); |
Adrian Prantl | 0069873 | 2016-05-25 22:37:29 +0000 | [diff] [blame^] | 110 | if (int RegNo = isDbgValueDescribedByReg(MI)) { |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 111 | Kind = RegisterKind; |
| 112 | Loc.RegisterLoc.RegNo = RegNo; |
| 113 | uint64_t Offset = |
| 114 | MI.isIndirectDebugValue() ? MI.getOperand(1).getImm() : 0; |
| 115 | // We don't support offsets larger than 4GiB here. They are |
| 116 | // slated to be replaced with DIExpressions anyway. |
| 117 | if (Offset >= (1ULL << 32)) |
| 118 | Kind = InvalidKind; |
| 119 | else |
| 120 | Loc.RegisterLoc.Offset = Offset; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /// If this variable is described by a register, return it, |
| 125 | /// otherwise return 0. |
| 126 | unsigned isDescribedByReg() const { |
| 127 | if (Kind == RegisterKind) |
| 128 | return Loc.RegisterLoc.RegNo; |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | void dump() const { MI.dump(); } |
| 133 | |
| 134 | bool operator==(const VarLoc &Other) const { |
| 135 | return Var == Other.Var && Loc.Hash == Other.Loc.Hash; |
| 136 | } |
| 137 | |
| 138 | bool operator<(const VarLoc &Other) const { |
| 139 | if (Var == Other.Var) |
| 140 | return Loc.Hash < Other.Loc.Hash; |
| 141 | return Var < Other.Var; |
| 142 | } |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 145 | typedef UniqueVector<VarLoc> VarLocMap; |
| 146 | typedef SparseBitVector<> VarLocList; |
| 147 | typedef SparseBitVector<> VarLocSet; |
| 148 | typedef SmallDenseMap<const MachineBasicBlock *, VarLocSet> VarLocInMBB; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 149 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 150 | void transferDebugValue(const MachineInstr &MI, VarLocList &OpenRanges, |
| 151 | VarLocMap &VarLocIDs); |
| 152 | void transferRegisterDef(MachineInstr &MI, VarLocList &OpenRanges, |
| 153 | const VarLocMap &VarLocIDs); |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 154 | bool transferTerminatorInst(MachineInstr &MI, VarLocList &OpenRanges, |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 155 | VarLocInMBB &OutLocs, const VarLocMap &VarLocIDs); |
| 156 | bool transfer(MachineInstr &MI, VarLocList &OpenRanges, VarLocInMBB &OutLocs, |
| 157 | VarLocMap &VarLocIDs); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 158 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 159 | bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs, |
| 160 | const VarLocMap &VarLocIDs); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 161 | |
| 162 | bool ExtendRanges(MachineFunction &MF); |
| 163 | |
| 164 | public: |
| 165 | static char ID; |
| 166 | |
| 167 | /// Default construct and initialize the pass. |
| 168 | LiveDebugValues(); |
| 169 | |
| 170 | /// Tell the pass manager which passes we depend on and what |
| 171 | /// information we preserve. |
| 172 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 173 | |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 174 | MachineFunctionProperties getRequiredProperties() const override { |
| 175 | return MachineFunctionProperties().set( |
| 176 | MachineFunctionProperties::Property::AllVRegsAllocated); |
| 177 | } |
| 178 | |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 179 | /// Print to ostream with a message. |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 180 | void printVarLocInMBB(const MachineFunction &MF, const VarLocInMBB &V, |
| 181 | const VarLocMap &VarLocIDs, const char *msg, |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 182 | raw_ostream &Out) const; |
| 183 | |
| 184 | /// Calculate the liveness information for the given machine function. |
| 185 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 186 | }; |
| 187 | } // namespace |
| 188 | |
| 189 | //===----------------------------------------------------------------------===// |
| 190 | // Implementation |
| 191 | //===----------------------------------------------------------------------===// |
| 192 | |
| 193 | char LiveDebugValues::ID = 0; |
| 194 | char &llvm::LiveDebugValuesID = LiveDebugValues::ID; |
| 195 | INITIALIZE_PASS(LiveDebugValues, "livedebugvalues", "Live DEBUG_VALUE analysis", |
| 196 | false, false) |
| 197 | |
| 198 | /// Default construct and initialize the pass. |
| 199 | LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) { |
| 200 | initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry()); |
| 201 | } |
| 202 | |
| 203 | /// Tell the pass manager which passes we depend on and what information we |
| 204 | /// preserve. |
| 205 | void LiveDebugValues::getAnalysisUsage(AnalysisUsage &AU) const { |
| 206 | MachineFunctionPass::getAnalysisUsage(AU); |
| 207 | } |
| 208 | |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 209 | //===----------------------------------------------------------------------===// |
| 210 | // Debug Range Extension Implementation |
| 211 | //===----------------------------------------------------------------------===// |
| 212 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 213 | void LiveDebugValues::printVarLocInMBB(const MachineFunction &MF, |
| 214 | const VarLocInMBB &V, |
| 215 | const VarLocMap &VarLocIDs, |
| 216 | const char *msg, |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 217 | raw_ostream &Out) const { |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 218 | for (const MachineBasicBlock &BB : MF) { |
| 219 | const auto &L = V.lookup(&BB); |
| 220 | Out << "MBB: " << BB.getName() << ":\n"; |
| 221 | for (unsigned VLL : L) { |
| 222 | const VarLoc &VL = VarLocIDs[VLL]; |
| 223 | Out << " Var: " << VL.Var.Var->getName(); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 224 | Out << " MI: "; |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 225 | VL.dump(); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 226 | Out << "\n"; |
| 227 | } |
| 228 | } |
| 229 | Out << "\n"; |
| 230 | } |
| 231 | |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 232 | /// End all previous ranges related to @MI and start a new range from @MI |
| 233 | /// if it is a DBG_VALUE instr. |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 234 | void LiveDebugValues::transferDebugValue(const MachineInstr &MI, |
| 235 | VarLocList &OpenRanges, |
| 236 | VarLocMap &VarLocIDs) { |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 237 | if (!MI.isDebugValue()) |
| 238 | return; |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 239 | const DILocalVariable *Var = MI.getDebugVariable(); |
| 240 | const DILocation *DebugLoc = MI.getDebugLoc(); |
| 241 | const DILocation *InlinedAt = DebugLoc->getInlinedAt(); |
| 242 | assert(Var->isValidLocationForIntrinsic(DebugLoc) && |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 243 | "Expected inlined-at fields to agree"); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 244 | |
| 245 | // End all previous ranges of Var. |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 246 | SparseBitVector<> KillSet; |
| 247 | for (unsigned ID : OpenRanges) { |
| 248 | auto &ORVar = VarLocIDs[ID].Var; |
| 249 | if (ORVar.Var == Var && ORVar.InlinedAt == InlinedAt) |
| 250 | KillSet.set(ID); |
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 | OpenRanges.intersectWithComplement(KillSet); |
| 253 | |
| 254 | // Add the VarLoc to OpenRanges from this DBG_VALUE. |
| 255 | // TODO: Currently handles DBG_VALUE which has only reg as location. |
Adrian Prantl | 0069873 | 2016-05-25 22:37:29 +0000 | [diff] [blame^] | 256 | if (isDbgValueDescribedByReg(MI)) |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 257 | OpenRanges.set(VarLocIDs.insert(MI)); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /// A definition of a register may mark the end of a range. |
| 261 | void LiveDebugValues::transferRegisterDef(MachineInstr &MI, |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 262 | VarLocList &OpenRanges, |
| 263 | const VarLocMap &VarLocIDs) { |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 264 | MachineFunction *MF = MI.getParent()->getParent(); |
| 265 | const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); |
| 266 | unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 267 | SparseBitVector<> KillSet; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 268 | for (const MachineOperand &MO : MI.operands()) { |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 269 | if (MO.isReg() && MO.isDef() && MO.getReg() && |
| 270 | TRI->isPhysicalRegister(MO.getReg())) { |
| 271 | // Remove ranges of all aliased registers. |
| 272 | for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI) |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 273 | for (unsigned ID : OpenRanges) |
| 274 | if (VarLocIDs[ID].isDescribedByReg() == *RAI) |
| 275 | KillSet.set(ID); |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 276 | } else if (MO.isRegMask()) { |
| 277 | // Remove ranges of all clobbered registers. Register masks don't usually |
| 278 | // list SP as preserved. While the debug info may be off for an |
| 279 | // instruction or two around callee-cleanup calls, transferring the |
| 280 | // DEBUG_VALUE across the call is still a better user experience. |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 281 | for (unsigned ID : OpenRanges) { |
| 282 | unsigned Reg = VarLocIDs[ID].isDescribedByReg(); |
| 283 | if (Reg && Reg != SP && MO.clobbersPhysReg(Reg)) |
| 284 | KillSet.set(ID); |
| 285 | } |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 286 | } |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 287 | } |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 288 | OpenRanges.intersectWithComplement(KillSet); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /// Terminate all open ranges at the end of the current basic block. |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 292 | bool LiveDebugValues::transferTerminatorInst(MachineInstr &MI, |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 293 | VarLocList &OpenRanges, |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 294 | VarLocInMBB &OutLocs, |
| 295 | const VarLocMap &VarLocIDs) { |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 296 | bool Changed = false; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 297 | const MachineBasicBlock *CurMBB = MI.getParent(); |
| 298 | if (!(MI.isTerminator() || (&MI == &CurMBB->instr_back()))) |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 299 | return false; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 300 | |
| 301 | if (OpenRanges.empty()) |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 302 | return false; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 303 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 304 | DEBUG(for (unsigned ID |
| 305 | : OpenRanges) { |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 306 | // Copy OpenRanges to OutLocs, if not already present. |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 307 | dbgs() << "Add to OutLocs: "; |
| 308 | VarLocIDs[ID].dump(); |
| 309 | }); |
| 310 | VarLocSet &VLS = OutLocs[CurMBB]; |
| 311 | Changed = VLS |= OpenRanges; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 312 | OpenRanges.clear(); |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 313 | return Changed; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /// This routine creates OpenRanges and OutLocs. |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 317 | bool LiveDebugValues::transfer(MachineInstr &MI, VarLocList &OpenRanges, |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 318 | VarLocInMBB &OutLocs, VarLocMap &VarLocIDs) { |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 319 | bool Changed = false; |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 320 | transferDebugValue(MI, OpenRanges, VarLocIDs); |
| 321 | transferRegisterDef(MI, OpenRanges, VarLocIDs); |
| 322 | Changed = transferTerminatorInst(MI, OpenRanges, OutLocs, VarLocIDs); |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 323 | return Changed; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /// This routine joins the analysis results of all incoming edges in @MBB by |
| 327 | /// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same |
| 328 | /// 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] | 329 | bool LiveDebugValues::join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 330 | VarLocInMBB &InLocs, const VarLocMap &VarLocIDs) { |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 331 | DEBUG(dbgs() << "join MBB: " << MBB.getName() << "\n"); |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 332 | bool Changed = false; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 333 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 334 | VarLocSet InLocsT; // Temporary incoming locations. |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 335 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 336 | // For all predecessors of this MBB, find the set of VarLocs that |
| 337 | // can be joined. |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 338 | for (auto p : MBB.predecessors()) { |
| 339 | auto OL = OutLocs.find(p); |
| 340 | // Join is null in case of empty OutLocs from any of the pred. |
| 341 | if (OL == OutLocs.end()) |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 342 | return false; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 343 | |
| 344 | // Just copy over the Out locs to incoming locs for the first predecessor. |
| 345 | if (p == *MBB.pred_begin()) { |
| 346 | InLocsT = OL->second; |
| 347 | continue; |
| 348 | } |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 349 | // Join with this predecessor. |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 350 | InLocsT &= OL->second; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | if (InLocsT.empty()) |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 354 | return false; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 355 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 356 | VarLocSet &ILS = InLocs[&MBB]; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 357 | |
| 358 | // Insert DBG_VALUE instructions, if not already inserted. |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 359 | VarLocSet Diff = InLocsT; |
| 360 | Diff.intersectWithComplement(ILS); |
| 361 | for (auto ID : Diff) { |
| 362 | // This VarLoc is not found in InLocs i.e. it is not yet inserted. So, a |
| 363 | // new range is started for the var from the mbb's beginning by inserting |
| 364 | // a new DBG_VALUE. transfer() will end this range however appropriate. |
| 365 | const VarLoc &DiffIt = VarLocIDs[ID]; |
| 366 | const MachineInstr *DMI = &DiffIt.MI; |
| 367 | MachineInstr *MI = |
| 368 | BuildMI(MBB, MBB.instr_begin(), DMI->getDebugLoc(), DMI->getDesc(), |
| 369 | DMI->isIndirectDebugValue(), DMI->getOperand(0).getReg(), 0, |
| 370 | DMI->getDebugVariable(), DMI->getDebugExpression()); |
| 371 | if (DMI->isIndirectDebugValue()) |
| 372 | MI->getOperand(1).setImm(DMI->getOperand(1).getImm()); |
| 373 | DEBUG(dbgs() << "Inserted: "; MI->dump();); |
| 374 | ILS.set(ID); |
| 375 | ++NumInserted; |
| 376 | Changed = true; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 377 | } |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 378 | return Changed; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /// Calculate the liveness information for the given machine function and |
| 382 | /// extend ranges across basic blocks. |
| 383 | bool LiveDebugValues::ExtendRanges(MachineFunction &MF) { |
| 384 | |
| 385 | DEBUG(dbgs() << "\nDebug Range Extension\n"); |
| 386 | |
| 387 | bool Changed = false; |
Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 388 | bool OLChanged = false; |
| 389 | bool MBBJoined = false; |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 390 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 391 | VarLocMap VarLocIDs; // Map VarLoc<>unique ID for use in bitvectors. |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 392 | VarLocList OpenRanges; // Ranges that are open until end of bb. |
| 393 | VarLocInMBB OutLocs; // Ranges that exist beyond bb. |
| 394 | VarLocInMBB InLocs; // Ranges that are incoming after joining. |
| 395 | |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 396 | DenseMap<unsigned int, MachineBasicBlock *> OrderToBB; |
| 397 | DenseMap<MachineBasicBlock *, unsigned int> BBToOrder; |
| 398 | std::priority_queue<unsigned int, std::vector<unsigned int>, |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 399 | std::greater<unsigned int>> |
| 400 | Worklist; |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 401 | std::priority_queue<unsigned int, std::vector<unsigned int>, |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 402 | std::greater<unsigned int>> |
| 403 | Pending; |
| 404 | |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 405 | // Initialize every mbb with OutLocs. |
| 406 | for (auto &MBB : MF) |
| 407 | for (auto &MI : MBB) |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 408 | transfer(MI, OpenRanges, OutLocs, VarLocIDs); |
| 409 | |
| 410 | DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "OutLocs after initialization", |
| 411 | dbgs())); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 412 | |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 413 | ReversePostOrderTraversal<MachineFunction *> RPOT(&MF); |
| 414 | unsigned int RPONumber = 0; |
| 415 | for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) { |
| 416 | OrderToBB[RPONumber] = *RI; |
| 417 | BBToOrder[*RI] = RPONumber; |
| 418 | Worklist.push(RPONumber); |
| 419 | ++RPONumber; |
| 420 | } |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 421 | // This is a standard "union of predecessor outs" dataflow problem. |
| 422 | // To solve it, we perform join() and transfer() using the two worklist method |
| 423 | // until the ranges converge. |
| 424 | // Ranges have converged when both worklists are empty. |
| 425 | while (!Worklist.empty() || !Pending.empty()) { |
| 426 | // We track what is on the pending worklist to avoid inserting the same |
| 427 | // thing twice. We could avoid this with a custom priority queue, but this |
| 428 | // is probably not worth it. |
| 429 | SmallPtrSet<MachineBasicBlock *, 16> OnPending; |
| 430 | while (!Worklist.empty()) { |
| 431 | MachineBasicBlock *MBB = OrderToBB[Worklist.top()]; |
| 432 | Worklist.pop(); |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 433 | MBBJoined = join(*MBB, OutLocs, InLocs, VarLocIDs); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 434 | |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 435 | if (MBBJoined) { |
| 436 | MBBJoined = false; |
| 437 | Changed = true; |
| 438 | for (auto &MI : *MBB) |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 439 | OLChanged |= transfer(MI, OpenRanges, OutLocs, VarLocIDs); |
| 440 | |
| 441 | DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, |
| 442 | "OutLocs after propagating", dbgs())); |
| 443 | DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, |
| 444 | "InLocs after propagating", dbgs())); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 445 | |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 446 | if (OLChanged) { |
| 447 | OLChanged = false; |
| 448 | for (auto s : MBB->successors()) |
| 449 | if (!OnPending.count(s)) { |
| 450 | OnPending.insert(s); |
| 451 | Pending.push(BBToOrder[s]); |
| 452 | } |
| 453 | } |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 456 | Worklist.swap(Pending); |
| 457 | // At this point, pending must be empty, since it was just the empty |
| 458 | // worklist |
| 459 | assert(Pending.empty() && "Pending should be empty"); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 460 | } |
Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 461 | |
Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 462 | DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "Final OutLocs", dbgs())); |
| 463 | DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, "Final InLocs", dbgs())); |
Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 464 | return Changed; |
| 465 | } |
| 466 | |
| 467 | bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) { |
| 468 | TRI = MF.getSubtarget().getRegisterInfo(); |
| 469 | TII = MF.getSubtarget().getInstrInfo(); |
| 470 | |
| 471 | bool Changed = false; |
| 472 | |
| 473 | Changed |= ExtendRanges(MF); |
| 474 | |
| 475 | return Changed; |
| 476 | } |