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