Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/AsmPrinter/DbgValueHistoryCalculator.cpp -------------===// |
| 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 | #include "DbgValueHistoryCalculator.h" |
| 11 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 12 | #include "llvm/CodeGen/MachineFunction.h" |
| 13 | #include "llvm/Support/Debug.h" |
| 14 | #include "llvm/Target/TargetRegisterInfo.h" |
| 15 | |
| 16 | #define DEBUG_TYPE "dwarfdebug" |
| 17 | |
| 18 | namespace llvm { |
| 19 | |
| 20 | // Return true if debug value, encoded by DBG_VALUE instruction, is in a |
| 21 | // defined reg. |
| 22 | static bool isDbgValueInDefinedReg(const MachineInstr *MI) { |
| 23 | assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!"); |
| 24 | return MI->getNumOperands() == 3 && MI->getOperand(0).isReg() && |
| 25 | MI->getOperand(0).getReg() && |
| 26 | (MI->getOperand(1).isImm() || |
| 27 | (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == 0U)); |
| 28 | } |
| 29 | |
| 30 | void calculateDbgValueHistory(const MachineFunction *MF, |
| 31 | const TargetRegisterInfo *TRI, |
| 32 | DbgValueHistoryMap &Result) { |
| 33 | // LiveUserVar - Map physreg numbers to the MDNode they contain. |
| 34 | std::vector<const MDNode *> LiveUserVar(TRI->getNumRegs()); |
| 35 | |
| 36 | for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E; |
| 37 | ++I) { |
| 38 | bool AtBlockEntry = true; |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 39 | for (const auto &MI : *I) { |
| 40 | if (MI.isDebugValue()) { |
| 41 | assert(MI.getNumOperands() > 1 && "Invalid machine instruction!"); |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 42 | |
| 43 | // Keep track of user variables. |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 44 | const MDNode *Var = MI.getDebugVariable(); |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 45 | |
| 46 | // Variable is in a register, we need to check for clobbers. |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 47 | if (isDbgValueInDefinedReg(&MI)) |
| 48 | LiveUserVar[MI.getOperand(0).getReg()] = Var; |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 49 | |
| 50 | // Check the history of this variable. |
| 51 | SmallVectorImpl<const MachineInstr *> &History = Result[Var]; |
| 52 | if (!History.empty()) { |
| 53 | // We have seen this variable before. Try to coalesce DBG_VALUEs. |
| 54 | const MachineInstr *Prev = History.back(); |
| 55 | if (Prev->isDebugValue()) { |
| 56 | // Coalesce identical entries at the end of History. |
| 57 | if (History.size() >= 2 && |
| 58 | Prev->isIdenticalTo(History[History.size() - 2])) { |
| 59 | DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n" |
| 60 | << "\t" << *Prev << "\t" |
| 61 | << *History[History.size() - 2] << "\n"); |
| 62 | History.pop_back(); |
| 63 | } |
| 64 | |
| 65 | // Terminate old register assignments that don't reach MI; |
| 66 | MachineFunction::const_iterator PrevMBB = Prev->getParent(); |
| 67 | if (PrevMBB != I && (!AtBlockEntry || std::next(PrevMBB) != I) && |
| 68 | isDbgValueInDefinedReg(Prev)) { |
| 69 | // Previous register assignment needs to terminate at the end of |
| 70 | // its basic block. |
| 71 | MachineBasicBlock::const_iterator LastMI = |
| 72 | PrevMBB->getLastNonDebugInstr(); |
| 73 | if (LastMI == PrevMBB->end()) { |
| 74 | // Drop DBG_VALUE for empty range. |
| 75 | DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n" |
| 76 | << "\t" << *Prev << "\n"); |
| 77 | History.pop_back(); |
| 78 | } else if (std::next(PrevMBB) != PrevMBB->getParent()->end()) |
| 79 | // Terminate after LastMI. |
| 80 | History.push_back(LastMI); |
| 81 | } |
| 82 | } |
| 83 | } |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 84 | History.push_back(&MI); |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 85 | } else { |
| 86 | // Not a DBG_VALUE instruction. |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 87 | if (!MI.isPosition()) |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 88 | AtBlockEntry = false; |
| 89 | |
| 90 | // Check if the instruction clobbers any registers with debug vars. |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 91 | for (const MachineOperand &MO : MI.operands()) { |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 92 | if (!MO.isReg() || !MO.isDef() || !MO.getReg()) |
| 93 | continue; |
| 94 | for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); |
| 95 | ++AI) { |
| 96 | unsigned Reg = *AI; |
| 97 | const MDNode *Var = LiveUserVar[Reg]; |
| 98 | if (!Var) |
| 99 | continue; |
| 100 | // Reg is now clobbered. |
| 101 | LiveUserVar[Reg] = nullptr; |
| 102 | |
| 103 | // Was MD last defined by a DBG_VALUE referring to Reg? |
| 104 | auto HistI = Result.find(Var); |
| 105 | if (HistI == Result.end()) |
| 106 | continue; |
| 107 | SmallVectorImpl<const MachineInstr *> &History = HistI->second; |
| 108 | if (History.empty()) |
| 109 | continue; |
| 110 | const MachineInstr *Prev = History.back(); |
| 111 | // Sanity-check: Register assignments are terminated at the end of |
| 112 | // their block. |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 113 | if (!Prev->isDebugValue() || Prev->getParent() != MI.getParent()) |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 114 | continue; |
| 115 | // Is the variable still in Reg? |
| 116 | if (!isDbgValueInDefinedReg(Prev) || |
| 117 | Prev->getOperand(0).getReg() != Reg) |
| 118 | continue; |
| 119 | // Var is clobbered. Make sure the next instruction gets a label. |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 120 | History.push_back(&MI); |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Make sure the final register assignments are terminated. |
| 128 | for (auto &I : Result) { |
| 129 | SmallVectorImpl<const MachineInstr *> &History = I.second; |
| 130 | if (History.empty()) |
| 131 | continue; |
| 132 | |
| 133 | const MachineInstr *Prev = History.back(); |
| 134 | if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) { |
| 135 | const MachineBasicBlock *PrevMBB = Prev->getParent(); |
| 136 | MachineBasicBlock::const_iterator LastMI = |
| 137 | PrevMBB->getLastNonDebugInstr(); |
| 138 | if (LastMI == PrevMBB->end()) |
| 139 | // Drop DBG_VALUE for empty range. |
| 140 | History.pop_back(); |
| 141 | else if (PrevMBB != &PrevMBB->getParent()->back()) { |
| 142 | // Terminate after LastMI. |
| 143 | History.push_back(LastMI); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | } |