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