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