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" |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallVector.h" |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 13 | #include "llvm/CodeGen/MachineFunction.h" |
| 14 | #include "llvm/Support/Debug.h" |
| 15 | #include "llvm/Target/TargetRegisterInfo.h" |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | #include <map> |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 18 | |
| 19 | #define DEBUG_TYPE "dwarfdebug" |
| 20 | |
| 21 | namespace llvm { |
| 22 | |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 23 | // \brief If @MI is a DBG_VALUE with debug value described by a |
| 24 | // defined register, returns the number of this register. |
| 25 | // In the other case, returns 0. |
| 26 | static unsigned isDescribedByReg(const MachineInstr &MI) { |
| 27 | assert(MI.isDebugValue()); |
| 28 | assert(MI.getNumOperands() == 3); |
| 29 | // If location of variable is described using a register (directly or |
| 30 | // indirecltly), this register is always a first operand. |
| 31 | return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0; |
| 32 | } |
| 33 | |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 34 | void DbgValueHistoryMap::startInstrRange(const MDNode *Var, |
| 35 | const MachineInstr &MI) { |
| 36 | // Instruction range should start with a DBG_VALUE instruction for the |
| 37 | // variable. |
| 38 | assert(MI.isDebugValue() && MI.getDebugVariable() == Var); |
| 39 | auto &Ranges = VarInstrRanges[Var]; |
| 40 | if (!Ranges.empty() && Ranges.back().second == nullptr && |
| 41 | Ranges.back().first->isIdenticalTo(&MI)) { |
| 42 | DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n" |
| 43 | << "\t" << Ranges.back().first << "\t" << MI << "\n"); |
| 44 | return; |
| 45 | } |
| 46 | Ranges.push_back(std::make_pair(&MI, nullptr)); |
| 47 | } |
| 48 | |
| 49 | void DbgValueHistoryMap::endInstrRange(const MDNode *Var, |
| 50 | const MachineInstr &MI) { |
| 51 | auto &Ranges = VarInstrRanges[Var]; |
| 52 | // Verify that the current instruction range is not yet closed. |
| 53 | assert(!Ranges.empty() && Ranges.back().second == nullptr); |
| 54 | // For now, instruction ranges are not allowed to cross basic block |
| 55 | // boundaries. |
| 56 | assert(Ranges.back().first->getParent() == MI.getParent()); |
| 57 | Ranges.back().second = &MI; |
| 58 | } |
| 59 | |
| 60 | unsigned DbgValueHistoryMap::getRegisterForVar(const MDNode *Var) const { |
| 61 | const auto &I = VarInstrRanges.find(Var); |
| 62 | if (I == VarInstrRanges.end()) |
| 63 | return 0; |
| 64 | const auto &Ranges = I->second; |
| 65 | if (Ranges.empty() || Ranges.back().second != nullptr) |
| 66 | return 0; |
| 67 | return isDescribedByReg(*Ranges.back().first); |
| 68 | } |
| 69 | |
| 70 | namespace { |
| 71 | // Maps physreg numbers to the variables they describe. |
| 72 | typedef std::map<unsigned, SmallVector<const MDNode *, 1>> RegDescribedVarsMap; |
| 73 | } |
| 74 | |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 75 | // \brief Claim that @Var is not described by @RegNo anymore. |
| 76 | static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, |
| 77 | unsigned RegNo, const MDNode *Var) { |
| 78 | const auto &I = RegVars.find(RegNo); |
| 79 | assert(RegNo != 0U && I != RegVars.end()); |
| 80 | auto &VarSet = I->second; |
| 81 | const auto &VarPos = std::find(VarSet.begin(), VarSet.end(), Var); |
| 82 | assert(VarPos != VarSet.end()); |
| 83 | VarSet.erase(VarPos); |
| 84 | // Don't keep empty sets in a map to keep it as small as possible. |
| 85 | if (VarSet.empty()) |
| 86 | RegVars.erase(I); |
| 87 | } |
| 88 | |
| 89 | // \brief Claim that @Var is now described by @RegNo. |
| 90 | static void addRegDescribedVar(RegDescribedVarsMap &RegVars, |
| 91 | unsigned RegNo, const MDNode *Var) { |
| 92 | assert(RegNo != 0U); |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 93 | auto &VarSet = RegVars[RegNo]; |
| 94 | assert(std::find(VarSet.begin(), VarSet.end(), Var) == VarSet.end()); |
| 95 | VarSet.push_back(Var); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // \brief Terminate the location range for variables described by register |
| 99 | // @RegNo by inserting @ClobberingInstr to their history. |
| 100 | static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo, |
| 101 | DbgValueHistoryMap &HistMap, |
| 102 | const MachineInstr &ClobberingInstr) { |
| 103 | const auto &I = RegVars.find(RegNo); |
| 104 | if (I == RegVars.end()) |
| 105 | return; |
| 106 | // Iterate over all variables described by this register and add this |
| 107 | // instruction to their history, clobbering it. |
| 108 | for (const auto &Var : I->second) |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 109 | HistMap.endInstrRange(Var, ClobberingInstr); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 110 | RegVars.erase(I); |
| 111 | } |
| 112 | |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 113 | // \brief Terminate location ranges for all variables, described by registers |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 114 | // clobbered by @MI. |
| 115 | static void clobberRegisterUses(RegDescribedVarsMap &RegVars, |
| 116 | const MachineInstr &MI, |
| 117 | const TargetRegisterInfo *TRI, |
| 118 | DbgValueHistoryMap &HistMap) { |
| 119 | for (const MachineOperand &MO : MI.operands()) { |
| 120 | if (!MO.isReg() || !MO.isDef() || !MO.getReg()) |
| 121 | continue; |
| 122 | for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); |
| 123 | ++AI) { |
| 124 | unsigned RegNo = *AI; |
| 125 | clobberRegisterUses(RegVars, RegNo, HistMap, MI); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // \brief Terminate the location range for all register-described variables |
| 131 | // by inserting @ClobberingInstr to their history. |
| 132 | static void clobberAllRegistersUses(RegDescribedVarsMap &RegVars, |
| 133 | DbgValueHistoryMap &HistMap, |
| 134 | const MachineInstr &ClobberingInstr) { |
| 135 | for (const auto &I : RegVars) |
| 136 | for (const auto &Var : I.second) |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 137 | HistMap.endInstrRange(Var, ClobberingInstr); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 138 | RegVars.clear(); |
| 139 | } |
| 140 | |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 141 | void calculateDbgValueHistory(const MachineFunction *MF, |
| 142 | const TargetRegisterInfo *TRI, |
| 143 | DbgValueHistoryMap &Result) { |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 144 | RegDescribedVarsMap RegVars; |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 145 | |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 146 | for (const auto &MBB : *MF) { |
| 147 | for (const auto &MI : MBB) { |
| 148 | if (!MI.isDebugValue()) { |
| 149 | // Not a DBG_VALUE instruction. It may clobber registers which describe |
| 150 | // some variables. |
| 151 | clobberRegisterUses(RegVars, MI, TRI, Result); |
| 152 | continue; |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 153 | } |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 154 | |
Alexey Samsonov | f0e0cca | 2014-05-27 22:35:00 +0000 | [diff] [blame] | 155 | assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!"); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 156 | const MDNode *Var = MI.getDebugVariable(); |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 157 | |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 158 | if (unsigned PrevReg = Result.getRegisterForVar(Var)) |
| 159 | dropRegDescribedVar(RegVars, PrevReg, Var); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 160 | |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 161 | Result.startInstrRange(Var, MI); |
| 162 | |
| 163 | if (unsigned NewReg = isDescribedByReg(MI)) |
| 164 | addRegDescribedVar(RegVars, NewReg, Var); |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 165 | } |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 166 | |
| 167 | // Make sure locations for register-described variables are valid only |
| 168 | // until the end of the basic block (unless it's the last basic block, in |
| 169 | // which case let their liveness run off to the end of the function). |
| 170 | if (!MBB.empty() && &MBB != &MF->back()) |
| 171 | clobberAllRegistersUses(RegVars, Result, MBB.back()); |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | } |